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 convert_adouble;
135 bool wipe_intentionally_left_blank_rfork;
136 bool delete_empty_adfiles;
139 * Additional options, all enabled by default,
140 * possibly useful for analyzing performance. The associated
141 * operations with each of them may be expensive, so having
142 * the chance to disable them individually gives a chance
143 * tweaking the setup for the particular usecase.
145 bool readdir_attr_rsize;
146 bool readdir_attr_finder_info;
147 bool readdir_attr_max_access;
148 /* Recursion guard. Will go away when we have STATX. */
149 bool in_openat_pathref_fsp;
152 static const struct enum_list fruit_rsrc[] = {
153 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
154 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
155 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
159 static const struct enum_list fruit_meta[] = {
160 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
161 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
165 static const struct enum_list fruit_locking[] = {
166 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
167 {FRUIT_LOCKING_NONE, "none"},
171 static const struct enum_list fruit_encoding[] = {
172 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
173 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
178 vfs_handle_struct *handle;
179 files_struct *fsp; /* backlink to itself */
181 /* tcon config handle */
182 struct fruit_config_data *config;
184 /* Backend fsp for AppleDouble file, can be NULL */
185 files_struct *ad_fsp;
186 /* link from adouble_open_from_base_fsp() to fio */
187 struct fio *real_fio;
189 /* Denote stream type, meta or rsrc */
193 * AFP_AfpInfo stream created, but not written yet, thus still a fake
194 * pipe fd. This is set to true in fruit_open_meta if there was no
195 * existing stream but the caller requested O_CREAT. It is later set to
196 * false when we get a write on the stream that then does open and
204 /*****************************************************************************
206 *****************************************************************************/
208 static struct adouble *ad_get_meta_fsp(TALLOC_CTX *ctx,
209 vfs_handle_struct *handle,
210 const struct smb_filename *smb_fname)
213 struct adouble *ad = NULL;
214 struct smb_filename *smb_fname_cp = NULL;
215 struct fruit_config_data *config = NULL;
217 if (smb_fname->fsp != NULL) {
218 return ad_get(ctx, handle, smb_fname, ADOUBLE_META);
221 SMB_VFS_HANDLE_GET_DATA(handle,
223 struct fruit_config_data,
226 if (config->in_openat_pathref_fsp) {
230 smb_fname_cp = cp_smb_filename(ctx,
232 if (smb_fname_cp == NULL) {
235 TALLOC_FREE(smb_fname_cp->stream_name);
236 config->in_openat_pathref_fsp = true;
237 status = openat_pathref_fsp(handle->conn->cwd_fsp,
239 config->in_openat_pathref_fsp = false;
240 if (!NT_STATUS_IS_OK(status)) {
241 TALLOC_FREE(smb_fname_cp);
245 ad = ad_get(ctx, handle, smb_fname_cp, ADOUBLE_META);
246 TALLOC_FREE(smb_fname_cp);
250 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
253 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
259 if (fio->real_fio != NULL) {
261 * This is an fsp from adouble_open_from_base_fsp()
262 * we should just pass this to the next
272 * Initialize config struct from our smb.conf config parameters
274 static int init_fruit_config(vfs_handle_struct *handle)
276 struct fruit_config_data *config;
278 const char *tm_size_str = NULL;
280 config = talloc_zero(handle->conn, struct fruit_config_data);
282 DEBUG(1, ("talloc_zero() failed\n"));
288 * Versions up to Samba 4.5.x had a spelling bug in the
289 * fruit:resource option calling lp_parm_enum with
290 * "res*s*ource" (ie two s).
292 * In Samba 4.6 we accept both the wrong and the correct
293 * spelling, in Samba 4.7 the bad spelling will be removed.
295 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
296 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
298 DEBUG(1, ("value for %s: resource type unknown\n",
299 FRUIT_PARAM_TYPE_NAME));
302 config->rsrc = (enum fruit_rsrc)enumval;
304 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
305 "resource", fruit_rsrc, enumval);
307 DEBUG(1, ("value for %s: resource type unknown\n",
308 FRUIT_PARAM_TYPE_NAME));
311 config->rsrc = (enum fruit_rsrc)enumval;
313 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
314 "metadata", fruit_meta, FRUIT_META_NETATALK);
316 DEBUG(1, ("value for %s: metadata type unknown\n",
317 FRUIT_PARAM_TYPE_NAME));
320 config->meta = (enum fruit_meta)enumval;
322 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
323 "locking", fruit_locking, FRUIT_LOCKING_NONE);
325 DEBUG(1, ("value for %s: locking type unknown\n",
326 FRUIT_PARAM_TYPE_NAME));
329 config->locking = (enum fruit_locking)enumval;
331 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
332 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
334 DEBUG(1, ("value for %s: encoding type unknown\n",
335 FRUIT_PARAM_TYPE_NAME));
338 config->encoding = (enum fruit_encoding)enumval;
340 if (config->rsrc == FRUIT_RSRC_ADFILE) {
341 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
342 FRUIT_PARAM_TYPE_NAME,
347 config->use_aapl = lp_parm_bool(
348 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
350 config->time_machine = lp_parm_bool(
351 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
353 config->unix_info_enabled = lp_parm_bool(
354 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
356 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
359 config->posix_rename = lp_parm_bool(
360 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
362 config->aapl_zero_file_id =
363 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
364 "zero_file_id", true);
366 config->readdir_attr_rsize = lp_parm_bool(
367 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
369 config->readdir_attr_finder_info = lp_parm_bool(
370 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
372 config->readdir_attr_max_access = lp_parm_bool(
373 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
375 config->model = lp_parm_const_string(
376 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
378 tm_size_str = lp_parm_const_string(
379 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
380 "time machine max size", NULL);
381 if (tm_size_str != NULL) {
382 config->time_machine_max_size = conv_str_size(tm_size_str);
385 config->convert_adouble = lp_parm_bool(
386 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
387 "convert_adouble", true);
389 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
390 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
391 "wipe_intentionally_left_blank_rfork", false);
393 config->delete_empty_adfiles = lp_parm_bool(
394 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
395 "delete_empty_adfiles", false);
397 SMB_VFS_HANDLE_SET_DATA(handle, config,
398 NULL, struct fruit_config_data,
404 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
405 struct stream_struct **streams,
406 const char *name, off_t size,
409 struct stream_struct *tmp;
411 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
417 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
418 if (tmp[*num_streams].name == NULL) {
422 tmp[*num_streams].size = size;
423 tmp[*num_streams].alloc_size = alloc_size;
430 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
431 struct stream_struct **streams)
433 struct stream_struct *tmp = *streams;
436 if (*num_streams == 0) {
440 for (i = 0; i < *num_streams; i++) {
441 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
446 if (i == *num_streams) {
450 if (tmp[i].size > 0) {
454 TALLOC_FREE(tmp[i].name);
455 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
460 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
461 struct stream_struct **streams,
464 struct stream_struct *tmp = *streams;
467 if (*num_streams == 0) {
471 for (i = 0; i < *num_streams; i++) {
472 if (strequal_m(tmp[i].name, name)) {
477 if (i == *num_streams) {
481 TALLOC_FREE(tmp[i].name);
482 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
487 static bool ad_empty_finderinfo(const struct adouble *ad)
490 char emptybuf[ADEDLEN_FINDERI] = {0};
493 fi = ad_get_entry(ad, ADEID_FINDERI);
495 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
499 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
503 static bool ai_empty_finderinfo(const AfpInfo *ai)
506 char emptybuf[ADEDLEN_FINDERI] = {0};
508 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
513 * Update btime with btime from Netatalk
515 static void update_btime(vfs_handle_struct *handle,
516 struct smb_filename *smb_fname)
519 struct timespec creation_time = {0};
521 struct fruit_config_data *config = NULL;
523 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
526 switch (config->meta) {
527 case FRUIT_META_STREAM:
529 case FRUIT_META_NETATALK:
533 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
537 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
541 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
547 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
548 update_stat_ex_create_time(&smb_fname->st, creation_time);
554 * Map an access mask to a Netatalk single byte byte range lock
556 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
557 uint32_t access_mask)
561 switch (access_mask) {
563 offset = AD_FILELOCK_OPEN_RD;
566 case FILE_WRITE_DATA:
567 case FILE_APPEND_DATA:
568 offset = AD_FILELOCK_OPEN_WR;
572 offset = AD_FILELOCK_OPEN_NONE;
576 if (fork_type == APPLE_FORK_RSRC) {
577 if (offset == AD_FILELOCK_OPEN_NONE) {
578 offset = AD_FILELOCK_RSRC_OPEN_NONE;
588 * Map a deny mode to a Netatalk brl
590 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
597 offset = AD_FILELOCK_DENY_RD;
601 offset = AD_FILELOCK_DENY_WR;
605 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
608 if (fork_type == APPLE_FORK_RSRC) {
616 * Call fcntl() with an exclusive F_GETLK request in order to
617 * determine if there's an existing shared lock
619 * @return true if the requested lock was found or any error occurred
620 * false if the lock was not found
622 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
625 off_t offset = in_offset;
630 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
631 if (result == false) {
635 if (type != F_UNLCK) {
642 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
644 uint32_t access_mask,
647 NTSTATUS status = NT_STATUS_OK;
649 bool share_for_read = (share_mode & FILE_SHARE_READ);
650 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
651 bool netatalk_already_open_for_reading = false;
652 bool netatalk_already_open_for_writing = false;
653 bool netatalk_already_open_with_deny_read = false;
654 bool netatalk_already_open_with_deny_write = false;
655 struct GUID req_guid = GUID_random();
657 /* FIXME: hardcoded data fork, add resource fork */
658 enum apple_fork fork_type = APPLE_FORK_DATA;
660 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
662 access_mask & FILE_READ_DATA ? "READ" :"-",
663 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
666 if (fsp_get_io_fd(fsp) == -1) {
670 /* Read NetATalk opens and deny modes on the file. */
671 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
672 access_to_netatalk_brl(fork_type,
675 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
676 denymode_to_netatalk_brl(fork_type,
679 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
680 access_to_netatalk_brl(fork_type,
683 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
684 denymode_to_netatalk_brl(fork_type,
687 /* If there are any conflicts - sharing violation. */
688 if ((access_mask & FILE_READ_DATA) &&
689 netatalk_already_open_with_deny_read) {
690 return NT_STATUS_SHARING_VIOLATION;
693 if (!share_for_read &&
694 netatalk_already_open_for_reading) {
695 return NT_STATUS_SHARING_VIOLATION;
698 if ((access_mask & FILE_WRITE_DATA) &&
699 netatalk_already_open_with_deny_write) {
700 return NT_STATUS_SHARING_VIOLATION;
703 if (!share_for_write &&
704 netatalk_already_open_for_writing) {
705 return NT_STATUS_SHARING_VIOLATION;
708 if (!(access_mask & FILE_READ_DATA)) {
710 * Nothing we can do here, we need read access
716 /* Set NetAtalk locks matching our access */
717 if (access_mask & FILE_READ_DATA) {
718 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
719 req_guid.time_hi_and_version = __LINE__;
724 fsp->op->global->open_persistent_id,
732 if (!NT_STATUS_IS_OK(status)) {
737 if (!share_for_read) {
738 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
739 req_guid.time_hi_and_version = __LINE__;
744 fsp->op->global->open_persistent_id,
752 if (!NT_STATUS_IS_OK(status)) {
757 if (access_mask & FILE_WRITE_DATA) {
758 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
759 req_guid.time_hi_and_version = __LINE__;
764 fsp->op->global->open_persistent_id,
772 if (!NT_STATUS_IS_OK(status)) {
777 if (!share_for_write) {
778 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
779 req_guid.time_hi_and_version = __LINE__;
784 fsp->op->global->open_persistent_id,
792 if (!NT_STATUS_IS_OK(status)) {
800 static NTSTATUS check_aapl(vfs_handle_struct *handle,
801 struct smb_request *req,
802 const struct smb2_create_blobs *in_context_blobs,
803 struct smb2_create_blobs *out_context_blobs)
805 struct fruit_config_data *config;
807 struct smb2_create_blob *aapl = NULL;
811 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
812 uint64_t req_bitmap, client_caps;
813 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
817 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
818 return NT_STATUS_UNSUCCESSFUL);
820 if (!config->use_aapl
821 || in_context_blobs == NULL
822 || out_context_blobs == NULL) {
826 aapl = smb2_create_blob_find(in_context_blobs,
827 SMB2_CREATE_TAG_AAPL);
832 if (aapl->data.length != 24) {
833 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
834 (uintmax_t)aapl->data.length));
835 return NT_STATUS_INVALID_PARAMETER;
838 cmd = IVAL(aapl->data.data, 0);
839 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
840 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
841 return NT_STATUS_INVALID_PARAMETER;
844 req_bitmap = BVAL(aapl->data.data, 8);
845 client_caps = BVAL(aapl->data.data, 16);
847 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
849 SBVAL(p, 8, req_bitmap);
850 ok = data_blob_append(req, &blob, p, 16);
852 return NT_STATUS_UNSUCCESSFUL;
855 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
856 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
857 (handle->conn->fs_capabilities & FILE_NAMED_STREAMS)) {
858 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
859 config->readdir_attr_enabled = true;
862 if (config->use_copyfile) {
863 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
864 config->copyfile_enabled = true;
868 * The client doesn't set the flag, so we can't check
869 * for it and just set it unconditionally
871 if (config->unix_info_enabled) {
872 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
875 SBVAL(p, 0, server_caps);
876 ok = data_blob_append(req, &blob, p, 8);
878 return NT_STATUS_UNSUCCESSFUL;
882 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
883 int val = lp_case_sensitive(SNUM(handle->conn));
891 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
898 if (config->time_machine) {
899 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
904 ok = data_blob_append(req, &blob, p, 8);
906 return NT_STATUS_UNSUCCESSFUL;
910 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
911 ok = convert_string_talloc(req,
913 config->model, strlen(config->model),
916 return NT_STATUS_UNSUCCESSFUL;
920 SIVAL(p + 4, 0, modellen);
921 ok = data_blob_append(req, &blob, p, 8);
924 return NT_STATUS_UNSUCCESSFUL;
927 ok = data_blob_append(req, &blob, model, modellen);
930 return NT_STATUS_UNSUCCESSFUL;
934 status = smb2_create_blob_add(out_context_blobs,
936 SMB2_CREATE_TAG_AAPL,
938 if (NT_STATUS_IS_OK(status)) {
939 global_fruit_config.nego_aapl = true;
945 static bool readdir_attr_meta_finderi_stream(
946 struct vfs_handle_struct *handle,
947 const struct smb_filename *smb_fname,
950 struct smb_filename *stream_name = NULL;
951 files_struct *fsp = NULL;
955 uint8_t buf[AFP_INFO_SIZE];
957 status = synthetic_pathref(talloc_tos(),
958 handle->conn->cwd_fsp,
959 smb_fname->base_name,
965 if (!NT_STATUS_IS_OK(status)) {
969 status = SMB_VFS_CREATE_FILE(
970 handle->conn, /* conn */
973 stream_name, /* fname */
974 FILE_READ_DATA, /* access_mask */
975 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
977 FILE_OPEN, /* create_disposition*/
978 0, /* create_options */
979 0, /* file_attributes */
980 INTERNAL_OPEN_ONLY, /* oplock_request */
982 0, /* allocation_size */
983 0, /* private_flags */
988 NULL, NULL); /* create context */
990 TALLOC_FREE(stream_name);
992 if (!NT_STATUS_IS_OK(status)) {
996 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
997 if (nread != AFP_INFO_SIZE) {
998 DBG_ERR("short read [%s] [%zd/%d]\n",
999 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
1004 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
1011 close_file_free(NULL, &fsp, NORMAL_CLOSE);
1017 static bool readdir_attr_meta_finderi_netatalk(
1018 struct vfs_handle_struct *handle,
1019 const struct smb_filename *smb_fname,
1022 struct adouble *ad = NULL;
1025 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
1030 p = ad_get_entry(ad, ADEID_FINDERI);
1032 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
1037 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1042 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1043 const struct smb_filename *smb_fname,
1044 struct readdir_attr_data *attr_data)
1046 struct fruit_config_data *config = NULL;
1047 uint32_t date_added;
1051 SMB_VFS_HANDLE_GET_DATA(handle, config,
1052 struct fruit_config_data,
1055 switch (config->meta) {
1056 case FRUIT_META_NETATALK:
1057 ok = readdir_attr_meta_finderi_netatalk(
1058 handle, smb_fname, &ai);
1061 case FRUIT_META_STREAM:
1062 ok = readdir_attr_meta_finderi_stream(
1063 handle, smb_fname, &ai);
1067 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1072 /* Don't bother with errors, it's likely ENOENT */
1076 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1078 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1079 &ai.afpi_FinderInfo[0], 4);
1081 /* finder_creator */
1082 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1083 &ai.afpi_FinderInfo[4], 4);
1087 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1088 &ai.afpi_FinderInfo[8], 2);
1090 /* finder_ext_flags */
1091 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1092 &ai.afpi_FinderInfo[24], 2);
1095 date_added = convert_time_t_to_uint32_t(
1096 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1098 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1103 static uint64_t readdir_attr_rfork_size_adouble(
1104 struct vfs_handle_struct *handle,
1105 const struct smb_filename *smb_fname)
1107 struct adouble *ad = NULL;
1108 uint64_t rfork_size;
1110 ad = ad_get(talloc_tos(), handle, smb_fname,
1116 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1122 static uint64_t readdir_attr_rfork_size_stream(
1123 struct vfs_handle_struct *handle,
1124 const struct smb_filename *smb_fname)
1126 struct smb_filename *stream_name = NULL;
1128 uint64_t rfork_size;
1130 stream_name = synthetic_smb_fname(talloc_tos(),
1131 smb_fname->base_name,
1132 AFPRESOURCE_STREAM_NAME,
1136 if (stream_name == NULL) {
1140 ret = SMB_VFS_STAT(handle->conn, stream_name);
1142 TALLOC_FREE(stream_name);
1146 rfork_size = stream_name->st.st_ex_size;
1147 TALLOC_FREE(stream_name);
1152 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1153 const struct smb_filename *smb_fname)
1155 struct fruit_config_data *config = NULL;
1156 uint64_t rfork_size;
1158 SMB_VFS_HANDLE_GET_DATA(handle, config,
1159 struct fruit_config_data,
1162 switch (config->rsrc) {
1163 case FRUIT_RSRC_ADFILE:
1164 rfork_size = readdir_attr_rfork_size_adouble(handle,
1168 case FRUIT_RSRC_XATTR:
1169 case FRUIT_RSRC_STREAM:
1170 rfork_size = readdir_attr_rfork_size_stream(handle,
1175 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1183 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1184 const struct smb_filename *smb_fname,
1185 struct readdir_attr_data *attr_data)
1187 NTSTATUS status = NT_STATUS_OK;
1188 struct fruit_config_data *config = NULL;
1191 SMB_VFS_HANDLE_GET_DATA(handle, config,
1192 struct fruit_config_data,
1193 return NT_STATUS_UNSUCCESSFUL);
1196 /* Ensure we return a default value in the creation_date field */
1197 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1200 * Resource fork length
1203 if (config->readdir_attr_rsize) {
1204 uint64_t rfork_size;
1206 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1207 attr_data->attr_data.aapl.rfork_size = rfork_size;
1214 if (config->readdir_attr_finder_info) {
1215 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1217 status = NT_STATUS_INTERNAL_ERROR;
1224 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1229 if (psd->dacl == NULL) {
1230 return NT_STATUS_OK;
1233 for (i = 0; i < psd->dacl->num_aces; i++) {
1234 /* MS NFS style mode/uid/gid */
1235 int cmp = dom_sid_compare_domain(
1236 &global_sid_Unix_NFS,
1237 &psd->dacl->aces[i].trustee);
1239 /* Normal ACE entry. */
1244 * security_descriptor_dacl_del()
1245 * *must* return NT_STATUS_OK as we know
1246 * we have something to remove.
1249 status = security_descriptor_dacl_del(psd,
1250 &psd->dacl->aces[i].trustee);
1251 if (!NT_STATUS_IS_OK(status)) {
1252 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1258 * security_descriptor_dacl_del() may delete more
1259 * then one entry subsequent to this one if the
1260 * SID matches, but we only need to ensure that
1261 * we stay looking at the same element in the array.
1265 return NT_STATUS_OK;
1268 /* Search MS NFS style ACE with UNIX mode */
1269 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1271 struct security_descriptor *psd,
1276 struct fruit_config_data *config = NULL;
1280 SMB_VFS_HANDLE_GET_DATA(handle, config,
1281 struct fruit_config_data,
1282 return NT_STATUS_UNSUCCESSFUL);
1284 if (!global_fruit_config.nego_aapl) {
1285 return NT_STATUS_OK;
1287 if (psd->dacl == NULL || !config->unix_info_enabled) {
1288 return NT_STATUS_OK;
1291 for (i = 0; i < psd->dacl->num_aces; i++) {
1292 if (dom_sid_compare_domain(
1293 &global_sid_Unix_NFS_Mode,
1294 &psd->dacl->aces[i].trustee) == 0) {
1295 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1296 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1299 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1300 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1306 * Remove any incoming virtual ACE entries generated by
1307 * fruit_fget_nt_acl().
1310 return remove_virtual_nfs_aces(psd);
1313 /****************************************************************************
1315 ****************************************************************************/
1317 static int fruit_connect(vfs_handle_struct *handle,
1318 const char *service,
1322 char *list = NULL, *newlist = NULL;
1323 struct fruit_config_data *config;
1324 const struct loadparm_substitution *lp_sub =
1325 loadparm_s3_global_substitution();
1327 DEBUG(10, ("fruit_connect\n"));
1329 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1334 rc = init_fruit_config(handle);
1339 SMB_VFS_HANDLE_GET_DATA(handle, config,
1340 struct fruit_config_data, return -1);
1342 if (config->veto_appledouble) {
1343 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1346 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1347 newlist = talloc_asprintf(
1349 "%s/" ADOUBLE_NAME_PREFIX "*/",
1351 lp_do_parameter(SNUM(handle->conn),
1356 lp_do_parameter(SNUM(handle->conn),
1358 "/" ADOUBLE_NAME_PREFIX "*/");
1364 if (config->encoding == FRUIT_ENC_NATIVE) {
1365 lp_do_parameter(SNUM(handle->conn),
1367 macos_string_replace_map);
1370 if (config->time_machine) {
1371 DBG_NOTICE("Enabling durable handles for Time Machine "
1372 "support on [%s]\n", service);
1373 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1374 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1375 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1376 if (!lp_strict_sync(SNUM(handle->conn))) {
1377 DBG_WARNING("Time Machine without strict sync is not "
1380 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1386 static void fio_ref_destroy_fn(void *p_data)
1388 struct fio *ref_fio = (struct fio *)p_data;
1389 if (ref_fio->real_fio != NULL) {
1390 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1391 ref_fio->real_fio->ad_fsp = NULL;
1392 ref_fio->real_fio = NULL;
1396 static void fio_close_ad_fsp(struct fio *fio)
1398 if (fio->ad_fsp != NULL) {
1399 fd_close(fio->ad_fsp);
1400 file_free(NULL, fio->ad_fsp);
1401 /* fio_ref_destroy_fn() should have cleared this */
1402 SMB_ASSERT(fio->ad_fsp == NULL);
1406 static void fio_destroy_fn(void *p_data)
1408 struct fio *fio = (struct fio *)p_data;
1409 fio_close_ad_fsp(fio);
1412 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1413 const struct files_struct *dirfsp,
1414 const struct smb_filename *smb_fname,
1419 struct fruit_config_data *config = NULL;
1420 struct fio *fio = NULL;
1421 struct vfs_open_how how = {
1422 .flags = flags & ~O_CREAT,
1427 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1429 SMB_VFS_HANDLE_GET_DATA(handle, config,
1430 struct fruit_config_data, return -1);
1432 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1433 fio->handle = handle;
1435 fio->type = ADOUBLE_META;
1436 fio->config = config;
1438 fd = SMB_VFS_NEXT_OPENAT(handle,
1447 if (!(flags & O_CREAT)) {
1448 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1454 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1458 fio->fake_fd = true;
1465 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1466 const struct files_struct *dirfsp,
1467 const struct smb_filename *smb_fname,
1472 struct fruit_config_data *config = NULL;
1473 struct fio *fio = NULL;
1474 struct adouble *ad = NULL;
1475 bool meta_exists = false;
1478 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1481 * We know this is a stream open, so fsp->base_fsp must
1484 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1485 SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1487 ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1494 if (!meta_exists && !(flags & O_CREAT)) {
1504 SMB_VFS_HANDLE_GET_DATA(handle, config,
1505 struct fruit_config_data, return -1);
1507 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1508 fio->handle = handle;
1510 fio->type = ADOUBLE_META;
1511 fio->config = config;
1512 fio->fake_fd = true;
1519 static int fruit_open_meta(vfs_handle_struct *handle,
1520 const struct files_struct *dirfsp,
1521 const struct smb_filename *smb_fname,
1522 files_struct *fsp, int flags, mode_t mode)
1525 struct fruit_config_data *config = NULL;
1527 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1529 SMB_VFS_HANDLE_GET_DATA(handle, config,
1530 struct fruit_config_data, return -1);
1532 switch (config->meta) {
1533 case FRUIT_META_STREAM:
1534 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1538 case FRUIT_META_NETATALK:
1539 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1544 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1548 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1553 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1554 const struct files_struct *dirfsp,
1555 const struct smb_filename *smb_fname,
1561 struct fruit_config_data *config = NULL;
1562 struct files_struct *ad_fsp = NULL;
1563 struct fio *fio = NULL;
1564 struct fio *ref_fio = NULL;
1568 SMB_VFS_HANDLE_GET_DATA(handle, config,
1569 struct fruit_config_data, return -1);
1571 if ((!(flags & O_CREAT)) &&
1572 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1574 /* sorry, but directories don't have a resource fork */
1581 * We return a fake_fd to the vfs modules above,
1582 * while we open an internal backend fsp for the
1583 * '._' file for the next vfs modules.
1585 * Note that adouble_open_from_base_fsp() recurses
1586 * into fruit_openat(), but it'll just pass to
1587 * the next module as just opens a flat file on
1597 status = adouble_open_from_base_fsp(fsp->conn->cwd_fsp,
1603 if (!NT_STATUS_IS_OK(status)) {
1604 errno = map_errno_from_nt_status(status);
1610 * Now we need to glue both handles together,
1611 * so that they automatically detach each other
1614 fio = fruit_get_complete_fio(handle, fsp);
1616 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1622 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1624 fio_ref_destroy_fn);
1625 if (ref_fio == NULL) {
1626 int saved_errno = errno;
1628 file_free(NULL, ad_fsp);
1630 errno = saved_errno;
1635 SMB_ASSERT(ref_fio->fsp == NULL);
1636 ref_fio->handle = handle;
1637 ref_fio->fsp = ad_fsp;
1638 ref_fio->type = ADOUBLE_RSRC;
1639 ref_fio->config = config;
1640 ref_fio->real_fio = fio;
1641 SMB_ASSERT(fio->ad_fsp == NULL);
1642 fio->ad_fsp = ad_fsp;
1643 fio->fake_fd = true;
1647 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1649 int saved_errno = errno;
1651 vfs_fake_fd_close(fd);
1653 errno = saved_errno;
1659 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1660 const struct files_struct *dirfsp,
1661 const struct smb_filename *smb_fname,
1666 #ifdef HAVE_ATTROPEN
1670 * As there's no attropenat() this is only going to work with AT_FDCWD.
1672 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1674 fd = attropen(smb_fname->base_name,
1675 AFPRESOURCE_EA_NETATALK,
1690 static int fruit_open_rsrc(vfs_handle_struct *handle,
1691 const struct files_struct *dirfsp,
1692 const struct smb_filename *smb_fname,
1693 files_struct *fsp, int flags, mode_t mode)
1696 struct fruit_config_data *config = NULL;
1697 struct fio *fio = NULL;
1699 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1701 SMB_VFS_HANDLE_GET_DATA(handle, config,
1702 struct fruit_config_data, return -1);
1704 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1705 fio->handle = handle;
1707 fio->type = ADOUBLE_RSRC;
1708 fio->config = config;
1710 switch (config->rsrc) {
1711 case FRUIT_RSRC_STREAM: {
1712 struct vfs_open_how how = {
1713 .flags = flags, .mode = mode,
1715 fd = SMB_VFS_NEXT_OPENAT(handle,
1723 case FRUIT_RSRC_ADFILE:
1724 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1728 case FRUIT_RSRC_XATTR:
1729 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1734 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1739 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1748 static int fruit_openat(vfs_handle_struct *handle,
1749 const struct files_struct *dirfsp,
1750 const struct smb_filename *smb_fname,
1752 const struct vfs_open_how *how)
1756 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1758 if (!is_named_stream(smb_fname)) {
1759 return SMB_VFS_NEXT_OPENAT(handle,
1766 if (how->resolve != 0) {
1771 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1773 if (is_afpinfo_stream(smb_fname->stream_name)) {
1774 fd = fruit_open_meta(handle,
1780 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1781 fd = fruit_open_rsrc(handle,
1788 fd = SMB_VFS_NEXT_OPENAT(handle,
1795 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1797 /* Prevent reopen optimisation */
1798 fsp->fsp_flags.have_proc_fds = false;
1802 static int fruit_close_meta(vfs_handle_struct *handle,
1806 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:
1814 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1819 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1820 fsp_set_fd(fsp, -1);
1822 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1826 case FRUIT_META_NETATALK:
1827 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1828 fsp_set_fd(fsp, -1);
1832 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1840 static int fruit_close_rsrc(vfs_handle_struct *handle,
1844 struct fruit_config_data *config = NULL;
1846 SMB_VFS_HANDLE_GET_DATA(handle, config,
1847 struct fruit_config_data, return -1);
1849 switch (config->rsrc) {
1850 case FRUIT_RSRC_STREAM:
1851 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1854 case FRUIT_RSRC_ADFILE:
1856 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1860 fio_close_ad_fsp(fio);
1861 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1862 fsp_set_fd(fsp, -1);
1866 case FRUIT_RSRC_XATTR:
1867 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1868 fsp_set_fd(fsp, -1);
1872 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1879 static int fruit_close(vfs_handle_struct *handle,
1885 fd = fsp_get_pathref_fd(fsp);
1887 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1889 if (!fsp_is_alternate_stream(fsp)) {
1890 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1893 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1894 ret = fruit_close_meta(handle, fsp);
1895 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1896 ret = fruit_close_rsrc(handle, fsp);
1898 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1904 static int fruit_renameat(struct vfs_handle_struct *handle,
1905 files_struct *srcfsp,
1906 const struct smb_filename *smb_fname_src,
1907 files_struct *dstfsp,
1908 const struct smb_filename *smb_fname_dst)
1911 struct fruit_config_data *config = NULL;
1912 struct smb_filename *src_adp_smb_fname = NULL;
1913 struct smb_filename *dst_adp_smb_fname = NULL;
1915 SMB_VFS_HANDLE_GET_DATA(handle, config,
1916 struct fruit_config_data, return -1);
1918 if (!VALID_STAT(smb_fname_src->st)) {
1919 DBG_ERR("Need valid stat for [%s]\n",
1920 smb_fname_str_dbg(smb_fname_src));
1924 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1933 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1934 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1939 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1944 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1949 DBG_DEBUG("%s -> %s\n",
1950 smb_fname_str_dbg(src_adp_smb_fname),
1951 smb_fname_str_dbg(dst_adp_smb_fname));
1953 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1958 if (errno == ENOENT) {
1963 TALLOC_FREE(src_adp_smb_fname);
1964 TALLOC_FREE(dst_adp_smb_fname);
1968 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1969 struct files_struct *dirfsp,
1970 const struct smb_filename *smb_fname)
1972 return SMB_VFS_NEXT_UNLINKAT(handle,
1978 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1979 const struct smb_filename *smb_fname)
1981 SMB_ASSERT(smb_fname->fsp != NULL);
1982 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1983 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1984 AFPINFO_EA_NETATALK);
1987 static int fruit_unlink_meta(vfs_handle_struct *handle,
1988 struct files_struct *dirfsp,
1989 const struct smb_filename *smb_fname)
1991 struct fruit_config_data *config = NULL;
1994 SMB_VFS_HANDLE_GET_DATA(handle, config,
1995 struct fruit_config_data, return -1);
1997 switch (config->meta) {
1998 case FRUIT_META_STREAM:
1999 rc = fruit_unlink_meta_stream(handle,
2004 case FRUIT_META_NETATALK:
2005 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
2009 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2016 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2017 struct files_struct *dirfsp,
2018 const struct smb_filename *smb_fname,
2023 if (!force_unlink) {
2024 struct smb_filename *full_fname = NULL;
2028 * TODO: use SMB_VFS_STATX() once we have it.
2031 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2034 if (full_fname == NULL) {
2039 * 0 byte resource fork streams are not listed by
2040 * vfs_streaminfo, as a result stream cleanup/deletion of file
2041 * deletion doesn't remove the resourcefork stream.
2044 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2046 TALLOC_FREE(full_fname);
2047 DBG_ERR("stat [%s] failed [%s]\n",
2048 smb_fname_str_dbg(full_fname), strerror(errno));
2052 size = full_fname->st.st_ex_size;
2053 TALLOC_FREE(full_fname);
2056 /* OS X ignores resource fork stream delete requests */
2061 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2065 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2072 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2073 struct files_struct *dirfsp,
2074 const struct smb_filename *smb_fname,
2078 struct adouble *ad = NULL;
2079 struct smb_filename *adp_smb_fname = NULL;
2081 if (!force_unlink) {
2082 struct smb_filename *full_fname = NULL;
2084 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2087 if (full_fname == NULL) {
2091 ad = ad_get(talloc_tos(), handle, full_fname,
2093 TALLOC_FREE(full_fname);
2101 * 0 byte resource fork streams are not listed by
2102 * vfs_streaminfo, as a result stream cleanup/deletion of file
2103 * deletion doesn't remove the resourcefork stream.
2106 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2107 /* OS X ignores resource fork stream delete requests */
2115 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2120 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2124 TALLOC_FREE(adp_smb_fname);
2125 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2132 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2133 const struct smb_filename *smb_fname,
2137 * OS X ignores resource fork stream delete requests, so nothing to do
2138 * here. Removing the file will remove the xattr anyway, so we don't
2139 * have to take care of removing 0 byte resource forks that could be
2145 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2146 struct files_struct *dirfsp,
2147 const struct smb_filename *smb_fname,
2150 struct fruit_config_data *config = NULL;
2153 SMB_VFS_HANDLE_GET_DATA(handle, config,
2154 struct fruit_config_data, return -1);
2156 switch (config->rsrc) {
2157 case FRUIT_RSRC_STREAM:
2158 rc = fruit_unlink_rsrc_stream(handle,
2164 case FRUIT_RSRC_ADFILE:
2165 rc = fruit_unlink_rsrc_adouble(handle,
2171 case FRUIT_RSRC_XATTR:
2172 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2176 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2183 static int fruit_fchmod(vfs_handle_struct *handle,
2184 struct files_struct *fsp,
2188 struct fruit_config_data *config = NULL;
2189 struct smb_filename *smb_fname_adp = NULL;
2190 const struct smb_filename *smb_fname = NULL;
2193 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2198 smb_fname = fsp->fsp_name;
2199 SMB_VFS_HANDLE_GET_DATA(handle, config,
2200 struct fruit_config_data, return -1);
2202 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2206 if (!VALID_STAT(smb_fname->st)) {
2210 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2214 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2219 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2221 if (!NT_STATUS_IS_OK(status)) {
2222 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2223 if (NT_STATUS_EQUAL(status,
2224 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2232 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2234 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2235 if (errno == ENOENT) {
2239 TALLOC_FREE(smb_fname_adp);
2243 static int fruit_unlinkat(vfs_handle_struct *handle,
2244 struct files_struct *dirfsp,
2245 const struct smb_filename *smb_fname,
2248 struct fruit_config_data *config = NULL;
2249 struct smb_filename *rsrc_smb_fname = NULL;
2252 if (flags & AT_REMOVEDIR) {
2253 return SMB_VFS_NEXT_UNLINKAT(handle,
2259 SMB_VFS_HANDLE_GET_DATA(handle, config,
2260 struct fruit_config_data, return -1);
2262 if (is_afpinfo_stream(smb_fname->stream_name)) {
2263 return fruit_unlink_meta(handle,
2266 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2267 return fruit_unlink_rsrc(handle,
2271 } else if (is_named_stream(smb_fname)) {
2272 return SMB_VFS_NEXT_UNLINKAT(handle,
2276 } else if (is_adouble_file(smb_fname->base_name)) {
2277 return SMB_VFS_NEXT_UNLINKAT(handle,
2284 * A request to delete the base file. Because 0 byte resource
2285 * fork streams are not listed by fruit_streaminfo,
2286 * delete_all_streams() can't remove 0 byte resource fork
2287 * streams, so we have to cleanup this here.
2289 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2290 smb_fname->base_name,
2291 AFPRESOURCE_STREAM_NAME,
2295 if (rsrc_smb_fname == NULL) {
2299 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2300 if ((ret != 0) && (errno != ENOENT)) {
2301 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2302 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2303 TALLOC_FREE(rsrc_smb_fname);
2306 TALLOC_FREE(rsrc_smb_fname);
2308 return SMB_VFS_NEXT_UNLINKAT(handle,
2314 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2315 files_struct *fsp, void *data,
2316 size_t n, off_t offset)
2318 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2322 if ((fio == NULL) || fio->fake_fd) {
2326 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2327 if (nread == -1 || nread == n) {
2331 DBG_ERR("Removing [%s] after short read [%zd]\n",
2332 fsp_str_dbg(fsp), nread);
2334 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2339 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2347 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2348 files_struct *fsp, void *data,
2349 size_t n, off_t offset)
2352 struct adouble *ad = NULL;
2353 char afpinfo_buf[AFP_INFO_SIZE];
2357 ai = afpinfo_new(talloc_tos());
2362 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2368 p = ad_get_entry(ad, ADEID_FINDERI);
2370 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2375 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2377 nread = afpinfo_pack(ai, afpinfo_buf);
2378 if (nread != AFP_INFO_SIZE) {
2383 memcpy(data, afpinfo_buf, n);
2391 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2392 files_struct *fsp, void *data,
2393 size_t n, off_t offset)
2395 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2400 * OS X has a off-by-1 error in the offset calculation, so we're
2401 * bug compatible here. It won't hurt, as any relevant real
2402 * world read requests from the AFP_AfpInfo stream will be
2403 * offset=0 n=60. offset is ignored anyway, see below.
2405 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2410 DBG_ERR("Failed to fetch fsp extension");
2414 /* Yes, macOS always reads from offset 0 */
2416 to_return = MIN(n, AFP_INFO_SIZE);
2418 switch (fio->config->meta) {
2419 case FRUIT_META_STREAM:
2420 nread = fruit_pread_meta_stream(handle, fsp, data,
2424 case FRUIT_META_NETATALK:
2425 nread = fruit_pread_meta_adouble(handle, fsp, data,
2430 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2434 if (nread == -1 && fio->fake_fd) {
2436 char afpinfo_buf[AFP_INFO_SIZE];
2438 ai = afpinfo_new(talloc_tos());
2443 nread = afpinfo_pack(ai, afpinfo_buf);
2445 if (nread != AFP_INFO_SIZE) {
2449 memcpy(data, afpinfo_buf, to_return);
2456 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2457 files_struct *fsp, void *data,
2458 size_t n, off_t offset)
2460 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2463 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2464 files_struct *fsp, void *data,
2465 size_t n, off_t offset)
2467 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2470 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2471 files_struct *fsp, void *data,
2472 size_t n, off_t offset)
2474 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2475 struct adouble *ad = NULL;
2478 if (fio == NULL || fio->ad_fsp == NULL) {
2479 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2484 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2486 DBG_ERR("ad_fget [%s] failed [%s]\n",
2487 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2491 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2492 offset + ad_getentryoff(ad, ADEID_RFORK));
2498 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2499 files_struct *fsp, void *data,
2500 size_t n, off_t offset)
2502 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2510 switch (fio->config->rsrc) {
2511 case FRUIT_RSRC_STREAM:
2512 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2515 case FRUIT_RSRC_ADFILE:
2516 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2519 case FRUIT_RSRC_XATTR:
2520 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2524 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2531 static ssize_t fruit_pread(vfs_handle_struct *handle,
2532 files_struct *fsp, void *data,
2533 size_t n, off_t offset)
2535 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2538 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2539 fsp_str_dbg(fsp), (intmax_t)offset, n);
2542 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2545 if (fio->type == ADOUBLE_META) {
2546 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2548 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2551 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2555 static bool fruit_must_handle_aio_stream(struct fio *fio)
2561 if (fio->type == ADOUBLE_META) {
2565 if ((fio->type == ADOUBLE_RSRC) &&
2566 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2574 struct fruit_pread_state {
2576 struct vfs_aio_state vfs_aio_state;
2579 static void fruit_pread_done(struct tevent_req *subreq);
2581 static struct tevent_req *fruit_pread_send(
2582 struct vfs_handle_struct *handle,
2583 TALLOC_CTX *mem_ctx,
2584 struct tevent_context *ev,
2585 struct files_struct *fsp,
2587 size_t n, off_t offset)
2589 struct tevent_req *req = NULL;
2590 struct tevent_req *subreq = NULL;
2591 struct fruit_pread_state *state = NULL;
2592 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2594 req = tevent_req_create(mem_ctx, &state,
2595 struct fruit_pread_state);
2600 if (fruit_must_handle_aio_stream(fio)) {
2601 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2602 if (state->nread != n) {
2603 if (state->nread != -1) {
2606 tevent_req_error(req, errno);
2607 return tevent_req_post(req, ev);
2609 tevent_req_done(req);
2610 return tevent_req_post(req, ev);
2613 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2615 if (tevent_req_nomem(req, subreq)) {
2616 return tevent_req_post(req, ev);
2618 tevent_req_set_callback(subreq, fruit_pread_done, req);
2622 static void fruit_pread_done(struct tevent_req *subreq)
2624 struct tevent_req *req = tevent_req_callback_data(
2625 subreq, struct tevent_req);
2626 struct fruit_pread_state *state = tevent_req_data(
2627 req, struct fruit_pread_state);
2629 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2630 TALLOC_FREE(subreq);
2632 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2635 tevent_req_done(req);
2638 static ssize_t fruit_pread_recv(struct tevent_req *req,
2639 struct vfs_aio_state *vfs_aio_state)
2641 struct fruit_pread_state *state = tevent_req_data(
2642 req, struct fruit_pread_state);
2643 ssize_t retval = -1;
2645 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2646 tevent_req_received(req);
2650 *vfs_aio_state = state->vfs_aio_state;
2651 retval = state->nread;
2652 tevent_req_received(req);
2656 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2657 files_struct *fsp, const void *data,
2658 size_t n, off_t offset)
2660 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2666 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2667 fsp_str_dbg(fsp), (intmax_t)offset, n);
2674 struct vfs_open_how how = {
2675 .flags = fio->flags, .mode = fio->mode,
2677 int fd = fsp_get_pathref_fd(fsp);
2679 ret = vfs_fake_fd_close(fd);
2680 fsp_set_fd(fsp, -1);
2682 DBG_ERR("Close [%s] failed: %s\n",
2683 fsp_str_dbg(fsp), strerror(errno));
2687 fd = SMB_VFS_NEXT_OPENAT(handle,
2688 NULL, /* opening a stream */
2693 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2694 fsp_str_dbg(fsp), strerror(errno));
2697 fsp_set_fd(fsp, fd);
2698 fio->fake_fd = false;
2701 ai = afpinfo_unpack(talloc_tos(), data);
2706 if (ai_empty_finderinfo(ai)) {
2708 * Writing an all 0 blob to the metadata stream results in the
2709 * stream being removed on a macOS server. This ensures we
2710 * behave the same and it verified by the "delete AFP_AfpInfo by
2711 * writing all 0" test.
2713 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2715 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2720 ok = set_delete_on_close(
2723 handle->conn->session_info->security_token,
2724 handle->conn->session_info->unix_token);
2726 DBG_ERR("set_delete_on_close on [%s] failed\n",
2733 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2734 if (nwritten != n) {
2741 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2742 files_struct *fsp, const void *data,
2743 size_t n, off_t offset)
2745 struct adouble *ad = NULL;
2751 ai = afpinfo_unpack(talloc_tos(), data);
2756 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2758 ad = ad_init(talloc_tos(), ADOUBLE_META);
2763 p = ad_get_entry(ad, ADEID_FINDERI);
2765 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2770 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2772 ret = ad_fset(handle, ad, fsp);
2774 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2781 if (!ai_empty_finderinfo(ai)) {
2786 * Writing an all 0 blob to the metadata stream results in the stream
2787 * being removed on a macOS server. This ensures we behave the same and
2788 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2791 ok = set_delete_on_close(
2794 handle->conn->session_info->security_token,
2795 handle->conn->session_info->unix_token);
2797 DBG_ERR("set_delete_on_close on [%s] failed\n",
2805 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2806 files_struct *fsp, const void *data,
2807 size_t n, off_t offset)
2809 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2811 uint8_t buf[AFP_INFO_SIZE];
2817 DBG_ERR("Failed to fetch fsp extension");
2826 if (offset != 0 && n < 60) {
2831 cmp = memcmp(data, "AFP", 3);
2837 if (n <= AFP_OFF_FinderInfo) {
2839 * Nothing to do here really, just return
2847 if (to_copy > AFP_INFO_SIZE) {
2848 to_copy = AFP_INFO_SIZE;
2850 memcpy(buf, data, to_copy);
2853 if (to_write != AFP_INFO_SIZE) {
2854 to_write = AFP_INFO_SIZE;
2857 switch (fio->config->meta) {
2858 case FRUIT_META_STREAM:
2859 nwritten = fruit_pwrite_meta_stream(handle,
2866 case FRUIT_META_NETATALK:
2867 nwritten = fruit_pwrite_meta_netatalk(handle,
2875 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2879 if (nwritten != to_write) {
2884 * Return the requested amount, verified against macOS SMB server
2889 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2890 files_struct *fsp, const void *data,
2891 size_t n, off_t offset)
2893 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2896 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2897 files_struct *fsp, const void *data,
2898 size_t n, off_t offset)
2900 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2903 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2904 files_struct *fsp, const void *data,
2905 size_t n, off_t offset)
2907 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2908 struct adouble *ad = NULL;
2912 if (fio == NULL || fio->ad_fsp == NULL) {
2913 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2918 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2920 DBG_ERR("ad_fget [%s] failed [%s]\n",
2921 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2925 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2926 offset + ad_getentryoff(ad, ADEID_RFORK));
2927 if (nwritten != n) {
2928 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2929 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2934 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2935 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2936 ret = ad_fset(handle, ad, fio->ad_fsp);
2938 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2948 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2949 files_struct *fsp, const void *data,
2950 size_t n, off_t offset)
2952 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2956 DBG_ERR("Failed to fetch fsp extension");
2960 switch (fio->config->rsrc) {
2961 case FRUIT_RSRC_STREAM:
2962 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2965 case FRUIT_RSRC_ADFILE:
2966 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2969 case FRUIT_RSRC_XATTR:
2970 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2974 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2981 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2982 files_struct *fsp, const void *data,
2983 size_t n, off_t offset)
2985 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2988 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2989 fsp_str_dbg(fsp), (intmax_t)offset, n);
2992 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2995 if (fio->type == ADOUBLE_META) {
2996 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2998 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
3001 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
3005 struct fruit_pwrite_state {
3007 struct vfs_aio_state vfs_aio_state;
3010 static void fruit_pwrite_done(struct tevent_req *subreq);
3012 static struct tevent_req *fruit_pwrite_send(
3013 struct vfs_handle_struct *handle,
3014 TALLOC_CTX *mem_ctx,
3015 struct tevent_context *ev,
3016 struct files_struct *fsp,
3018 size_t n, off_t offset)
3020 struct tevent_req *req = NULL;
3021 struct tevent_req *subreq = NULL;
3022 struct fruit_pwrite_state *state = NULL;
3023 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3025 req = tevent_req_create(mem_ctx, &state,
3026 struct fruit_pwrite_state);
3031 if (fruit_must_handle_aio_stream(fio)) {
3032 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3033 if (state->nwritten != n) {
3034 if (state->nwritten != -1) {
3037 tevent_req_error(req, errno);
3038 return tevent_req_post(req, ev);
3040 tevent_req_done(req);
3041 return tevent_req_post(req, ev);
3044 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3046 if (tevent_req_nomem(req, subreq)) {
3047 return tevent_req_post(req, ev);
3049 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3053 static void fruit_pwrite_done(struct tevent_req *subreq)
3055 struct tevent_req *req = tevent_req_callback_data(
3056 subreq, struct tevent_req);
3057 struct fruit_pwrite_state *state = tevent_req_data(
3058 req, struct fruit_pwrite_state);
3060 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3061 TALLOC_FREE(subreq);
3063 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3066 tevent_req_done(req);
3069 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3070 struct vfs_aio_state *vfs_aio_state)
3072 struct fruit_pwrite_state *state = tevent_req_data(
3073 req, struct fruit_pwrite_state);
3074 ssize_t retval = -1;
3076 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3077 tevent_req_received(req);
3081 *vfs_aio_state = state->vfs_aio_state;
3082 retval = state->nwritten;
3083 tevent_req_received(req);
3087 struct fruit_fsync_state {
3089 struct vfs_aio_state vfs_aio_state;
3092 static void fruit_fsync_done(struct tevent_req *subreq);
3094 static struct tevent_req *fruit_fsync_send(
3095 struct vfs_handle_struct *handle,
3096 TALLOC_CTX *mem_ctx,
3097 struct tevent_context *ev,
3098 struct files_struct *fsp)
3100 struct tevent_req *req = NULL;
3101 struct tevent_req *subreq = NULL;
3102 struct fruit_fsync_state *state = NULL;
3103 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3105 req = tevent_req_create(mem_ctx, &state,
3106 struct fruit_fsync_state);
3111 if (fruit_must_handle_aio_stream(fio)) {
3112 struct adouble *ad = NULL;
3114 if (fio->type == ADOUBLE_META) {
3116 * We must never pass a fake_fd
3117 * to lower level fsync calls.
3118 * Everything is already done
3119 * synchronously, so just return
3122 SMB_ASSERT(fio->fake_fd);
3123 tevent_req_done(req);
3124 return tevent_req_post(req, ev);
3128 * We know the following must be true,
3129 * as it's the condition for fruit_must_handle_aio_stream()
3130 * to return true if fio->type == ADOUBLE_RSRC.
3132 SMB_ASSERT(fio->config->rsrc == FRUIT_RSRC_ADFILE);
3133 if (fio->ad_fsp == NULL) {
3134 tevent_req_error(req, EBADF);
3135 return tevent_req_post(req, ev);
3137 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3139 tevent_req_error(req, ENOMEM);
3140 return tevent_req_post(req, ev);
3145 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
3146 if (tevent_req_nomem(req, subreq)) {
3147 return tevent_req_post(req, ev);
3149 tevent_req_set_callback(subreq, fruit_fsync_done, req);
3153 static void fruit_fsync_done(struct tevent_req *subreq)
3155 struct tevent_req *req = tevent_req_callback_data(
3156 subreq, struct tevent_req);
3157 struct fruit_fsync_state *state = tevent_req_data(
3158 req, struct fruit_fsync_state);
3160 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
3161 TALLOC_FREE(subreq);
3162 if (state->ret != 0) {
3163 tevent_req_error(req, errno);
3166 tevent_req_done(req);
3169 static int fruit_fsync_recv(struct tevent_req *req,
3170 struct vfs_aio_state *vfs_aio_state)
3172 struct fruit_fsync_state *state = tevent_req_data(
3173 req, struct fruit_fsync_state);
3176 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3177 tevent_req_received(req);
3181 *vfs_aio_state = state->vfs_aio_state;
3182 retval = state->ret;
3183 tevent_req_received(req);
3188 * Helper to stat/lstat the base file of an smb_fname.
3190 static int fruit_stat_base(vfs_handle_struct *handle,
3191 struct smb_filename *smb_fname,
3194 char *tmp_stream_name;
3197 tmp_stream_name = smb_fname->stream_name;
3198 smb_fname->stream_name = NULL;
3200 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3202 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3204 smb_fname->stream_name = tmp_stream_name;
3206 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3207 smb_fname->base_name,
3208 (uintmax_t)smb_fname->st.st_ex_dev,
3209 (uintmax_t)smb_fname->st.st_ex_ino);
3213 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3214 struct smb_filename *smb_fname,
3220 ret = fruit_stat_base(handle, smb_fname, false);
3225 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3228 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3230 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3233 smb_fname->st.st_ex_ino = ino;
3238 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3239 struct smb_filename *smb_fname,
3242 struct adouble *ad = NULL;
3244 /* Populate the stat struct with info from the base file. */
3245 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3249 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3251 DBG_INFO("fruit_stat_meta %s: %s\n",
3252 smb_fname_str_dbg(smb_fname), strerror(errno));
3258 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3259 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3260 smb_fname->stream_name);
3264 static int fruit_stat_meta(vfs_handle_struct *handle,
3265 struct smb_filename *smb_fname,
3268 struct fruit_config_data *config = NULL;
3271 SMB_VFS_HANDLE_GET_DATA(handle, config,
3272 struct fruit_config_data, return -1);
3274 switch (config->meta) {
3275 case FRUIT_META_STREAM:
3276 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3279 case FRUIT_META_NETATALK:
3280 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3284 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3291 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3292 struct smb_filename *smb_fname,
3295 struct adouble *ad = NULL;
3298 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3304 /* Populate the stat struct with info from the base file. */
3305 ret = fruit_stat_base(handle, smb_fname, follow_links);
3311 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3312 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3313 smb_fname->stream_name);
3318 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3319 struct smb_filename *smb_fname,
3325 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3327 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3333 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3334 struct smb_filename *smb_fname,
3337 #ifdef HAVE_ATTROPEN
3341 /* Populate the stat struct with info from the base file. */
3342 ret = fruit_stat_base(handle, smb_fname, follow_links);
3347 fd = attropen(smb_fname->base_name,
3348 AFPRESOURCE_EA_NETATALK,
3354 ret = sys_fstat(fd, &smb_fname->st, false);
3357 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3358 AFPRESOURCE_EA_NETATALK);
3364 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3365 smb_fname->stream_name);
3375 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3376 struct smb_filename *smb_fname,
3379 struct fruit_config_data *config = NULL;
3382 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3384 SMB_VFS_HANDLE_GET_DATA(handle, config,
3385 struct fruit_config_data, return -1);
3387 switch (config->rsrc) {
3388 case FRUIT_RSRC_STREAM:
3389 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3392 case FRUIT_RSRC_XATTR:
3393 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3396 case FRUIT_RSRC_ADFILE:
3397 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3401 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3408 static int fruit_stat(vfs_handle_struct *handle,
3409 struct smb_filename *smb_fname)
3413 DEBUG(10, ("fruit_stat called for %s\n",
3414 smb_fname_str_dbg(smb_fname)));
3416 if (!is_named_stream(smb_fname)) {
3417 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3419 update_btime(handle, smb_fname);
3425 * Note if lp_posix_paths() is true, we can never
3426 * get here as is_ntfs_stream_smb_fname() is
3427 * always false. So we never need worry about
3428 * not following links here.
3431 if (is_afpinfo_stream(smb_fname->stream_name)) {
3432 rc = fruit_stat_meta(handle, smb_fname, true);
3433 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3434 rc = fruit_stat_rsrc(handle, smb_fname, true);
3436 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3440 update_btime(handle, smb_fname);
3441 smb_fname->st.st_ex_mode &= ~S_IFMT;
3442 smb_fname->st.st_ex_mode |= S_IFREG;
3443 smb_fname->st.st_ex_blocks =
3444 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3449 static int fruit_lstat(vfs_handle_struct *handle,
3450 struct smb_filename *smb_fname)
3454 DEBUG(10, ("fruit_lstat called for %s\n",
3455 smb_fname_str_dbg(smb_fname)));
3457 if (!is_named_stream(smb_fname)) {
3458 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3460 update_btime(handle, smb_fname);
3465 if (is_afpinfo_stream(smb_fname->stream_name)) {
3466 rc = fruit_stat_meta(handle, smb_fname, false);
3467 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3468 rc = fruit_stat_rsrc(handle, smb_fname, false);
3470 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3474 update_btime(handle, smb_fname);
3475 smb_fname->st.st_ex_mode &= ~S_IFMT;
3476 smb_fname->st.st_ex_mode |= S_IFREG;
3477 smb_fname->st.st_ex_blocks =
3478 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3483 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3485 SMB_STRUCT_STAT *sbuf)
3487 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3488 struct smb_filename smb_fname;
3497 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3502 *sbuf = fsp->base_fsp->fsp_name->st;
3503 sbuf->st_ex_size = AFP_INFO_SIZE;
3504 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3508 smb_fname = (struct smb_filename) {
3509 .base_name = fsp->fsp_name->base_name,
3510 .twrp = fsp->fsp_name->twrp,
3513 ret = fruit_stat_base(handle, &smb_fname, false);
3517 *sbuf = smb_fname.st;
3519 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3521 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3526 sbuf->st_ex_ino = ino;
3530 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3532 SMB_STRUCT_STAT *sbuf)
3536 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3541 *sbuf = fsp->base_fsp->fsp_name->st;
3542 sbuf->st_ex_size = AFP_INFO_SIZE;
3543 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3548 static int fruit_fstat_meta(vfs_handle_struct *handle,
3550 SMB_STRUCT_STAT *sbuf,
3555 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3557 switch (fio->config->meta) {
3558 case FRUIT_META_STREAM:
3559 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3562 case FRUIT_META_NETATALK:
3563 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3567 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3571 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3575 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3577 SMB_STRUCT_STAT *sbuf)
3579 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3582 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3584 SMB_STRUCT_STAT *sbuf)
3586 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3589 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3591 SMB_STRUCT_STAT *sbuf)
3593 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3594 struct adouble *ad = NULL;
3597 if (fio == NULL || fio->ad_fsp == NULL) {
3598 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3603 /* Populate the stat struct with info from the base file. */
3604 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3609 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3611 DBG_ERR("ad_fget [%s] failed [%s]\n",
3612 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3616 *sbuf = fsp->base_fsp->fsp_name->st;
3617 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3618 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3624 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3625 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3629 switch (fio->config->rsrc) {
3630 case FRUIT_RSRC_STREAM:
3631 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3634 case FRUIT_RSRC_ADFILE:
3635 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3638 case FRUIT_RSRC_XATTR:
3639 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3643 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3650 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3651 SMB_STRUCT_STAT *sbuf)
3653 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3657 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3660 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3662 if (fio->type == ADOUBLE_META) {
3663 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3665 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3669 sbuf->st_ex_mode &= ~S_IFMT;
3670 sbuf->st_ex_mode |= S_IFREG;
3671 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3674 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3675 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3679 static NTSTATUS delete_invalid_meta_stream(
3680 vfs_handle_struct *handle,
3681 const struct smb_filename *smb_fname,
3682 TALLOC_CTX *mem_ctx,
3683 unsigned int *pnum_streams,
3684 struct stream_struct **pstreams,
3687 struct smb_filename *sname = NULL;
3692 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3694 return NT_STATUS_INTERNAL_ERROR;
3698 return NT_STATUS_OK;
3701 status = synthetic_pathref(talloc_tos(),
3702 handle->conn->cwd_fsp,
3703 smb_fname->base_name,
3704 AFPINFO_STREAM_NAME,
3709 if (!NT_STATUS_IS_OK(status)) {
3710 return NT_STATUS_NO_MEMORY;
3713 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3714 handle->conn->cwd_fsp,
3718 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3720 return map_nt_error_from_unix(errno);
3724 return NT_STATUS_OK;
3727 static NTSTATUS fruit_streaminfo_meta_stream(
3728 vfs_handle_struct *handle,
3729 struct files_struct *fsp,
3730 const struct smb_filename *smb_fname,
3731 TALLOC_CTX *mem_ctx,
3732 unsigned int *pnum_streams,
3733 struct stream_struct **pstreams)
3735 struct stream_struct *stream = *pstreams;
3736 unsigned int num_streams = *pnum_streams;
3739 for (i = 0; i < num_streams; i++) {
3740 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3745 if (i == num_streams) {
3746 return NT_STATUS_OK;
3749 if (stream[i].size != AFP_INFO_SIZE) {
3750 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3751 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3753 return delete_invalid_meta_stream(handle,
3762 return NT_STATUS_OK;
3765 static NTSTATUS fruit_streaminfo_meta_netatalk(
3766 vfs_handle_struct *handle,
3767 struct files_struct *fsp,
3768 const struct smb_filename *smb_fname,
3769 TALLOC_CTX *mem_ctx,
3770 unsigned int *pnum_streams,
3771 struct stream_struct **pstreams)
3773 struct stream_struct *stream = *pstreams;
3774 unsigned int num_streams = *pnum_streams;
3775 struct adouble *ad = NULL;
3780 /* Remove the Netatalk xattr from the list */
3781 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3782 ":" NETATALK_META_XATTR ":$DATA");
3784 return NT_STATUS_NO_MEMORY;
3788 * Check if there's a AFPINFO_STREAM from the VFS streams
3789 * backend and if yes, remove it from the list
3791 for (i = 0; i < num_streams; i++) {
3792 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3797 if (i < num_streams) {
3798 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3799 smb_fname_str_dbg(smb_fname));
3801 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3804 return NT_STATUS_INTERNAL_ERROR;
3808 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3810 return NT_STATUS_OK;
3813 is_fi_empty = ad_empty_finderinfo(ad);
3817 return NT_STATUS_OK;
3820 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3821 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3822 smb_roundup(handle->conn, AFP_INFO_SIZE));
3824 return NT_STATUS_NO_MEMORY;
3827 return NT_STATUS_OK;
3830 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3831 struct files_struct *fsp,
3832 const struct smb_filename *smb_fname,
3833 TALLOC_CTX *mem_ctx,
3834 unsigned int *pnum_streams,
3835 struct stream_struct **pstreams)
3837 struct fruit_config_data *config = NULL;
3840 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3841 return NT_STATUS_INTERNAL_ERROR);
3843 switch (config->meta) {
3844 case FRUIT_META_NETATALK:
3845 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3846 mem_ctx, pnum_streams,
3850 case FRUIT_META_STREAM:
3851 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3852 mem_ctx, pnum_streams,
3857 return NT_STATUS_INTERNAL_ERROR;
3863 static NTSTATUS fruit_streaminfo_rsrc_stream(
3864 vfs_handle_struct *handle,
3865 struct files_struct *fsp,
3866 const struct smb_filename *smb_fname,
3867 TALLOC_CTX *mem_ctx,
3868 unsigned int *pnum_streams,
3869 struct stream_struct **pstreams)
3873 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3875 DBG_ERR("Filtering resource stream failed\n");
3876 return NT_STATUS_INTERNAL_ERROR;
3878 return NT_STATUS_OK;
3881 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3882 vfs_handle_struct *handle,
3883 struct files_struct *fsp,
3884 const struct smb_filename *smb_fname,
3885 TALLOC_CTX *mem_ctx,
3886 unsigned int *pnum_streams,
3887 struct stream_struct **pstreams)
3891 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3893 DBG_ERR("Filtering resource stream failed\n");
3894 return NT_STATUS_INTERNAL_ERROR;
3896 return NT_STATUS_OK;
3899 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3900 vfs_handle_struct *handle,
3901 struct files_struct *fsp,
3902 const struct smb_filename *smb_fname,
3903 TALLOC_CTX *mem_ctx,
3904 unsigned int *pnum_streams,
3905 struct stream_struct **pstreams)
3907 struct stream_struct *stream = *pstreams;
3908 unsigned int num_streams = *pnum_streams;
3909 struct adouble *ad = NULL;
3915 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3916 * and if yes, remove it from the list
3918 for (i = 0; i < num_streams; i++) {
3919 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3924 if (i < num_streams) {
3925 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3926 smb_fname_str_dbg(smb_fname));
3928 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3929 AFPRESOURCE_STREAM);
3931 return NT_STATUS_INTERNAL_ERROR;
3935 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3937 return NT_STATUS_OK;
3940 rlen = ad_getentrylen(ad, ADEID_RFORK);
3944 return NT_STATUS_OK;
3947 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3948 AFPRESOURCE_STREAM_NAME, rlen,
3949 smb_roundup(handle->conn, rlen));
3951 return NT_STATUS_NO_MEMORY;
3954 return NT_STATUS_OK;
3957 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3958 struct files_struct *fsp,
3959 const struct smb_filename *smb_fname,
3960 TALLOC_CTX *mem_ctx,
3961 unsigned int *pnum_streams,
3962 struct stream_struct **pstreams)
3964 struct fruit_config_data *config = NULL;
3967 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
3968 return NT_STATUS_OK;
3971 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3972 return NT_STATUS_INTERNAL_ERROR);
3974 switch (config->rsrc) {
3975 case FRUIT_RSRC_STREAM:
3976 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3977 mem_ctx, pnum_streams,
3981 case FRUIT_RSRC_XATTR:
3982 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3983 mem_ctx, pnum_streams,
3987 case FRUIT_RSRC_ADFILE:
3988 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3989 mem_ctx, pnum_streams,
3994 return NT_STATUS_INTERNAL_ERROR;
4000 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
4001 struct stream_struct **pstreams)
4003 unsigned num_streams = *pnum_streams;
4004 struct stream_struct *streams = *pstreams;
4007 if (!global_fruit_config.nego_aapl) {
4011 while (i < num_streams) {
4012 struct smb_filename smb_fname = (struct smb_filename) {
4013 .stream_name = streams[i].name,
4016 if (is_ntfs_default_stream_smb_fname(&smb_fname)
4017 || streams[i].size > 0)
4023 streams[i] = streams[num_streams - 1];
4027 *pnum_streams = num_streams;
4030 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
4031 struct files_struct *fsp,
4032 TALLOC_CTX *mem_ctx,
4033 unsigned int *pnum_streams,
4034 struct stream_struct **pstreams)
4036 struct fruit_config_data *config = NULL;
4037 const struct smb_filename *smb_fname = NULL;
4040 smb_fname = fsp->fsp_name;
4042 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4043 return NT_STATUS_UNSUCCESSFUL);
4045 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4047 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
4048 pnum_streams, pstreams);
4049 if (!NT_STATUS_IS_OK(status)) {
4053 fruit_filter_empty_streams(pnum_streams, pstreams);
4055 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4056 mem_ctx, pnum_streams, pstreams);
4057 if (!NT_STATUS_IS_OK(status)) {
4061 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4062 mem_ctx, pnum_streams, pstreams);
4063 if (!NT_STATUS_IS_OK(status)) {
4067 return NT_STATUS_OK;
4070 static int fruit_fntimes(vfs_handle_struct *handle,
4072 struct smb_file_time *ft)
4075 struct adouble *ad = NULL;
4076 struct fruit_config_data *config = NULL;
4078 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4081 if ((config->meta != FRUIT_META_NETATALK) ||
4082 is_omit_timespec(&ft->create_time))
4084 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4087 DBG_DEBUG("set btime for %s to %s\n", fsp_str_dbg(fsp),
4088 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
4090 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4095 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4096 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4098 rc = ad_fset(handle, ad, fsp);
4104 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
4107 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4110 static int fruit_fallocate(struct vfs_handle_struct *handle,
4111 struct files_struct *fsp,
4116 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4119 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4122 /* Let the pwrite code path handle it. */
4127 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4128 struct files_struct *fsp,
4131 #ifdef HAVE_ATTROPEN
4132 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4137 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4138 struct files_struct *fsp,
4141 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4143 struct adouble *ad = NULL;
4146 if (fio == NULL || fio->ad_fsp == NULL) {
4147 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4152 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4154 DBG_ERR("ad_fget [%s] failed [%s]\n",
4155 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4159 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4161 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4167 ad_setentrylen(ad, ADEID_RFORK, offset);
4169 rc = ad_fset(handle, ad, fio->ad_fsp);
4171 DBG_ERR("ad_fset [%s] failed [%s]\n",
4172 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4181 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4182 struct files_struct *fsp,
4185 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4188 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4189 struct files_struct *fsp,
4192 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4196 DBG_ERR("Failed to fetch fsp extension");
4200 switch (fio->config->rsrc) {
4201 case FRUIT_RSRC_XATTR:
4202 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4205 case FRUIT_RSRC_ADFILE:
4206 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4209 case FRUIT_RSRC_STREAM:
4210 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4214 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4222 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4223 struct files_struct *fsp,
4227 DBG_WARNING("ftruncate %s to %jd",
4228 fsp_str_dbg(fsp), (intmax_t)offset);
4229 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4234 /* OS X returns success but does nothing */
4235 DBG_INFO("ignoring ftruncate %s to %jd\n",
4236 fsp_str_dbg(fsp), (intmax_t)offset);
4240 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4241 struct files_struct *fsp,
4244 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4247 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4251 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4254 if (fio->type == ADOUBLE_META) {
4255 ret = fruit_ftruncate_meta(handle, fsp, offset);
4257 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4260 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4264 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4265 struct smb_request *req,
4266 struct files_struct *dirfsp,
4267 struct smb_filename *smb_fname,
4268 uint32_t access_mask,
4269 uint32_t share_access,
4270 uint32_t create_disposition,
4271 uint32_t create_options,
4272 uint32_t file_attributes,
4273 uint32_t oplock_request,
4274 const struct smb2_lease *lease,
4275 uint64_t allocation_size,
4276 uint32_t private_flags,
4277 struct security_descriptor *sd,
4278 struct ea_list *ea_list,
4279 files_struct **result,
4281 const struct smb2_create_blobs *in_context_blobs,
4282 struct smb2_create_blobs *out_context_blobs)
4285 struct fruit_config_data *config = NULL;
4286 files_struct *fsp = NULL;
4287 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4290 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4291 if (!NT_STATUS_IS_OK(status)) {
4295 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4296 return NT_STATUS_UNSUCCESSFUL);
4298 if (is_apple_stream(smb_fname->stream_name) &&
4300 config->convert_adouble)
4302 uint32_t conv_flags = 0;
4304 if (config->wipe_intentionally_left_blank_rfork) {
4305 conv_flags |= AD_CONV_WIPE_BLANK;
4307 if (config->delete_empty_adfiles) {
4308 conv_flags |= AD_CONV_DELETE;
4311 ret = ad_convert(handle,
4313 macos_string_replace_map,
4316 DBG_ERR("ad_convert(\"%s\") failed\n",
4317 smb_fname_str_dbg(smb_fname));
4321 status = SMB_VFS_NEXT_CREATE_FILE(
4322 handle, req, dirfsp, smb_fname,
4323 access_mask, share_access,
4324 create_disposition, create_options,
4325 file_attributes, oplock_request,
4327 allocation_size, private_flags,
4328 sd, ea_list, result,
4329 pinfo, in_context_blobs, out_context_blobs);
4330 if (!NT_STATUS_IS_OK(status)) {
4336 if (global_fruit_config.nego_aapl) {
4337 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4339 * Enable POSIX directory rename behaviour
4341 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4346 * If this is a plain open for existing files, opening an 0
4347 * byte size resource fork MUST fail with
4348 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4350 * Cf the vfs_fruit torture tests in test_rfork_create().
4352 if (global_fruit_config.nego_aapl &&
4353 create_disposition == FILE_OPEN &&
4354 smb_fname->st.st_ex_size == 0 &&
4355 is_named_stream(smb_fname))
4357 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4361 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4365 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4366 (fsp->op != NULL) &&
4367 !fsp->fsp_flags.is_pathref)
4369 status = fruit_check_access(
4373 if (!NT_STATUS_IS_OK(status)) {
4381 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4384 close_file_free(req, &fsp, ERROR_CLOSE);
4391 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4392 struct files_struct *fsp,
4393 TALLOC_CTX *mem_ctx,
4394 struct readdir_attr_data **pattr_data)
4396 struct fruit_config_data *config = NULL;
4397 struct readdir_attr_data *attr_data;
4398 uint32_t conv_flags = 0;
4402 SMB_VFS_HANDLE_GET_DATA(handle, config,
4403 struct fruit_config_data,
4404 return NT_STATUS_UNSUCCESSFUL);
4406 if (!global_fruit_config.nego_aapl) {
4407 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4413 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4415 if (config->convert_adouble) {
4416 if (config->wipe_intentionally_left_blank_rfork) {
4417 conv_flags |= AD_CONV_WIPE_BLANK;
4419 if (config->delete_empty_adfiles) {
4420 conv_flags |= AD_CONV_DELETE;
4423 ret = ad_convert(handle,
4425 macos_string_replace_map,
4428 DBG_ERR("ad_convert(\"%s\") failed\n",
4433 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4434 if (*pattr_data == NULL) {
4435 return NT_STATUS_NO_MEMORY;
4437 attr_data = *pattr_data;
4438 attr_data->type = RDATTR_AAPL;
4441 * Mac metadata: compressed FinderInfo, resource fork length
4444 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4445 if (!NT_STATUS_IS_OK(status)) {
4447 * Error handling is tricky: if we return failure from
4448 * this function, the corresponding directory entry
4449 * will to be passed to the client, so we really just
4450 * want to error out on fatal errors.
4452 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4460 if (config->unix_info_enabled) {
4461 attr_data->attr_data.aapl.unix_mode =
4462 fsp->fsp_name->st.st_ex_mode;
4468 if (!config->readdir_attr_max_access) {
4469 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4471 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4474 SEC_FLAG_MAXIMUM_ALLOWED,
4475 &attr_data->attr_data.aapl.max_access);
4476 if (!NT_STATUS_IS_OK(status)) {
4481 return NT_STATUS_OK;
4484 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4486 TALLOC_FREE(*pattr_data);
4490 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4492 uint32_t security_info,
4493 TALLOC_CTX *mem_ctx,
4494 struct security_descriptor **ppdesc)
4497 struct security_ace ace;
4499 struct fruit_config_data *config;
4501 SMB_VFS_HANDLE_GET_DATA(handle, config,
4502 struct fruit_config_data,
4503 return NT_STATUS_UNSUCCESSFUL);
4505 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4507 if (!NT_STATUS_IS_OK(status)) {
4512 * Add MS NFS style ACEs with uid, gid and mode
4514 if (!global_fruit_config.nego_aapl) {
4515 return NT_STATUS_OK;
4517 if (!config->unix_info_enabled) {
4518 return NT_STATUS_OK;
4521 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4522 status = remove_virtual_nfs_aces(*ppdesc);
4523 if (!NT_STATUS_IS_OK(status)) {
4524 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4528 /* MS NFS style mode */
4529 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4530 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4531 status = security_descriptor_dacl_add(*ppdesc, &ace);
4532 if (!NT_STATUS_IS_OK(status)) {
4533 DEBUG(1,("failed to add MS NFS style ACE\n"));
4537 /* MS NFS style uid */
4538 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4539 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4540 status = security_descriptor_dacl_add(*ppdesc, &ace);
4541 if (!NT_STATUS_IS_OK(status)) {
4542 DEBUG(1,("failed to add MS NFS style ACE\n"));
4546 /* MS NFS style gid */
4547 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4548 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4549 status = security_descriptor_dacl_add(*ppdesc, &ace);
4550 if (!NT_STATUS_IS_OK(status)) {
4551 DEBUG(1,("failed to add MS NFS style ACE\n"));
4555 return NT_STATUS_OK;
4558 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4560 uint32_t security_info_sent,
4561 const struct security_descriptor *orig_psd)
4565 mode_t ms_nfs_mode = 0;
4567 struct security_descriptor *psd = NULL;
4568 uint32_t orig_num_aces = 0;
4570 if (orig_psd->dacl != NULL) {
4571 orig_num_aces = orig_psd->dacl->num_aces;
4574 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4576 return NT_STATUS_NO_MEMORY;
4579 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4581 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4582 if (!NT_STATUS_IS_OK(status)) {
4583 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4589 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4590 * sent/present flags correctly now we've removed them.
4593 if (orig_num_aces != 0) {
4595 * Are there any ACE's left ?
4597 if (psd->dacl->num_aces == 0) {
4598 /* No - clear the DACL sent/present flags. */
4599 security_info_sent &= ~SECINFO_DACL;
4600 psd->type &= ~SEC_DESC_DACL_PRESENT;
4604 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4605 if (!NT_STATUS_IS_OK(status)) {
4606 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4612 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4614 DBG_WARNING("%s, result: %d, %04o error %s\n",
4617 (unsigned)ms_nfs_mode,
4619 status = map_nt_error_from_unix(errno);
4626 return NT_STATUS_OK;
4629 static struct vfs_offload_ctx *fruit_offload_ctx;
4631 struct fruit_offload_read_state {
4632 struct vfs_handle_struct *handle;
4633 struct tevent_context *ev;
4641 static void fruit_offload_read_done(struct tevent_req *subreq);
4643 static struct tevent_req *fruit_offload_read_send(
4644 TALLOC_CTX *mem_ctx,
4645 struct tevent_context *ev,
4646 struct vfs_handle_struct *handle,
4653 struct tevent_req *req = NULL;
4654 struct tevent_req *subreq = NULL;
4655 struct fruit_offload_read_state *state = NULL;
4657 req = tevent_req_create(mem_ctx, &state,
4658 struct fruit_offload_read_state);
4662 *state = (struct fruit_offload_read_state) {
4669 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4670 fsctl, ttl, offset, to_copy);
4671 if (tevent_req_nomem(subreq, req)) {
4672 return tevent_req_post(req, ev);
4674 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4678 static void fruit_offload_read_done(struct tevent_req *subreq)
4680 struct tevent_req *req = tevent_req_callback_data(
4681 subreq, struct tevent_req);
4682 struct fruit_offload_read_state *state = tevent_req_data(
4683 req, struct fruit_offload_read_state);
4686 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4692 TALLOC_FREE(subreq);
4693 if (tevent_req_nterror(req, status)) {
4697 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4698 tevent_req_done(req);
4702 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4703 &fruit_offload_ctx);
4704 if (tevent_req_nterror(req, status)) {
4708 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4711 if (tevent_req_nterror(req, status)) {
4715 tevent_req_done(req);
4719 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4720 struct vfs_handle_struct *handle,
4721 TALLOC_CTX *mem_ctx,
4726 struct fruit_offload_read_state *state = tevent_req_data(
4727 req, struct fruit_offload_read_state);
4730 if (tevent_req_is_nterror(req, &status)) {
4731 tevent_req_received(req);
4735 *flags = state->flags;
4736 *xferlen = state->xferlen;
4737 token->length = state->token.length;
4738 token->data = talloc_move(mem_ctx, &state->token.data);
4740 tevent_req_received(req);
4741 return NT_STATUS_OK;
4744 struct fruit_offload_write_state {
4745 struct vfs_handle_struct *handle;
4747 struct files_struct *src_fsp;
4748 struct files_struct *dst_fsp;
4752 static void fruit_offload_write_done(struct tevent_req *subreq);
4753 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4754 TALLOC_CTX *mem_ctx,
4755 struct tevent_context *ev,
4758 off_t transfer_offset,
4759 struct files_struct *dest_fsp,
4763 struct tevent_req *req, *subreq;
4764 struct fruit_offload_write_state *state;
4766 struct fruit_config_data *config;
4767 off_t src_off = transfer_offset;
4768 files_struct *src_fsp = NULL;
4769 off_t to_copy = num;
4770 bool copyfile_enabled = false;
4772 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4773 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4775 SMB_VFS_HANDLE_GET_DATA(handle, config,
4776 struct fruit_config_data,
4779 req = tevent_req_create(mem_ctx, &state,
4780 struct fruit_offload_write_state);
4784 state->handle = handle;
4785 state->dst_fsp = dest_fsp;
4788 case FSCTL_SRV_COPYCHUNK:
4789 case FSCTL_SRV_COPYCHUNK_WRITE:
4790 copyfile_enabled = config->copyfile_enabled;
4797 * Check if this a OS X copyfile style copychunk request with
4798 * a requested chunk count of 0 that was translated to a
4799 * offload_write_send VFS call overloading the parameters src_off
4800 * = dest_off = num = 0.
4802 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4803 status = vfs_offload_token_db_fetch_fsp(
4804 fruit_offload_ctx, token, &src_fsp);
4805 if (tevent_req_nterror(req, status)) {
4806 return tevent_req_post(req, ev);
4808 state->src_fsp = src_fsp;
4810 status = vfs_stat_fsp(src_fsp);
4811 if (tevent_req_nterror(req, status)) {
4812 return tevent_req_post(req, ev);
4815 to_copy = src_fsp->fsp_name->st.st_ex_size;
4816 state->is_copyfile = true;
4819 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4828 if (tevent_req_nomem(subreq, req)) {
4829 return tevent_req_post(req, ev);
4832 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4836 static void fruit_offload_write_done(struct tevent_req *subreq)
4838 struct tevent_req *req = tevent_req_callback_data(
4839 subreq, struct tevent_req);
4840 struct fruit_offload_write_state *state = tevent_req_data(
4841 req, struct fruit_offload_write_state);
4843 unsigned int num_streams = 0;
4844 struct stream_struct *streams = NULL;
4846 struct smb_filename *src_fname_tmp = NULL;
4847 struct smb_filename *dst_fname_tmp = NULL;
4849 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4852 TALLOC_FREE(subreq);
4853 if (tevent_req_nterror(req, status)) {
4857 if (!state->is_copyfile) {
4858 tevent_req_done(req);
4863 * Now copy all remaining streams. We know the share supports
4864 * streams, because we're in vfs_fruit. We don't do this async
4865 * because streams are few and small.
4867 status = vfs_fstreaminfo(state->src_fsp,
4868 req, &num_streams, &streams);
4869 if (tevent_req_nterror(req, status)) {
4873 if (num_streams == 1) {
4874 /* There is always one stream, ::$DATA. */
4875 tevent_req_done(req);
4879 for (i = 0; i < num_streams; i++) {
4880 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4881 __func__, streams[i].name, (size_t)streams[i].size));
4883 src_fname_tmp = synthetic_smb_fname(
4885 state->src_fsp->fsp_name->base_name,
4888 state->src_fsp->fsp_name->twrp,
4889 state->src_fsp->fsp_name->flags);
4890 if (tevent_req_nomem(src_fname_tmp, req)) {
4894 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4895 TALLOC_FREE(src_fname_tmp);
4899 dst_fname_tmp = synthetic_smb_fname(
4901 state->dst_fsp->fsp_name->base_name,
4904 state->dst_fsp->fsp_name->twrp,
4905 state->dst_fsp->fsp_name->flags);
4906 if (tevent_req_nomem(dst_fname_tmp, req)) {
4907 TALLOC_FREE(src_fname_tmp);
4911 status = copy_file(req,
4912 state->handle->conn,
4916 if (!NT_STATUS_IS_OK(status)) {
4917 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4918 smb_fname_str_dbg(src_fname_tmp),
4919 smb_fname_str_dbg(dst_fname_tmp),
4920 nt_errstr(status)));
4921 TALLOC_FREE(src_fname_tmp);
4922 TALLOC_FREE(dst_fname_tmp);
4923 tevent_req_nterror(req, status);
4927 TALLOC_FREE(src_fname_tmp);
4928 TALLOC_FREE(dst_fname_tmp);
4931 TALLOC_FREE(streams);
4932 TALLOC_FREE(src_fname_tmp);
4933 TALLOC_FREE(dst_fname_tmp);
4934 tevent_req_done(req);
4937 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4938 struct tevent_req *req,
4941 struct fruit_offload_write_state *state = tevent_req_data(
4942 req, struct fruit_offload_write_state);
4945 if (tevent_req_is_nterror(req, &status)) {
4946 DEBUG(1, ("server side copy chunk failed: %s\n",
4947 nt_errstr(status)));
4949 tevent_req_received(req);
4953 *copied = state->copied;
4954 tevent_req_received(req);
4956 return NT_STATUS_OK;
4959 static char *fruit_get_bandsize_line(char **lines, int numlines)
4962 static bool re_initialized = false;
4966 if (!re_initialized) {
4967 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4971 re_initialized = true;
4974 for (i = 0; i < numlines; i++) {
4975 regmatch_t matches[1];
4977 ret = regexec(&re, lines[i], 1, matches, 0);
4980 * Check if the match was on the last line, sa we want
4981 * the subsequent line.
4983 if (i + 1 == numlines) {
4986 return lines[i + 1];
4988 if (ret != REG_NOMATCH) {
4996 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4999 static bool re_initialized = false;
5000 regmatch_t matches[2];
5005 if (!re_initialized) {
5008 "<integer>\\([[:digit:]]*\\)</integer>$",
5013 re_initialized = true;
5016 ret = regexec(&re, line, 2, matches, 0);
5018 DBG_ERR("regex failed [%s]\n", line);
5022 line[matches[1].rm_eo] = '\0';
5024 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
5028 *_band_size = (size_t)band_size;
5033 * This reads and parses an Info.plist from a TM sparsebundle looking for the
5034 * "band-size" key and value.
5036 static bool fruit_get_bandsize(vfs_handle_struct *handle,
5040 #define INFO_PLIST_MAX_SIZE 64*1024
5042 struct smb_filename *smb_fname = NULL;
5043 files_struct *fsp = NULL;
5044 uint8_t *file_data = NULL;
5045 char **lines = NULL;
5046 char *band_size_line = NULL;
5047 size_t plist_file_size;
5054 plist = talloc_asprintf(talloc_tos(),
5056 handle->conn->connectpath,
5058 if (plist == NULL) {
5063 smb_fname = synthetic_smb_fname(talloc_tos(),
5069 if (smb_fname == NULL) {
5074 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5076 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
5081 plist_file_size = smb_fname->st.st_ex_size;
5083 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
5084 DBG_INFO("%s is too large, ignoring\n", plist);
5089 status = SMB_VFS_NEXT_CREATE_FILE(
5093 smb_fname, /* fname */
5094 FILE_GENERIC_READ, /* access_mask */
5095 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
5096 FILE_OPEN, /* create_disposition */
5097 0, /* create_options */
5098 0, /* file_attributes */
5099 INTERNAL_OPEN_ONLY, /* oplock_request */
5101 0, /* allocation_size */
5102 0, /* private_flags */
5107 NULL, NULL); /* create context */
5108 if (!NT_STATUS_IS_OK(status)) {
5109 DBG_INFO("Opening [%s] failed [%s]\n",
5110 smb_fname_str_dbg(smb_fname), nt_errstr(status));
5115 file_data = talloc_zero_array(talloc_tos(),
5117 plist_file_size + 1);
5118 if (file_data == NULL) {
5123 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
5124 if (nread != plist_file_size) {
5125 DBG_ERR("Short read on [%s]: %zu/%zd\n",
5126 fsp_str_dbg(fsp), nread, plist_file_size);
5132 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5133 if (!NT_STATUS_IS_OK(status)) {
5134 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5139 lines = file_lines_parse((char *)file_data,
5143 if (lines == NULL) {
5148 band_size_line = fruit_get_bandsize_line(lines, numlines);
5149 if (band_size_line == NULL) {
5150 DBG_ERR("Didn't find band-size key in [%s]\n",
5151 smb_fname_str_dbg(smb_fname));
5156 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5158 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5162 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5166 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5167 if (!NT_STATUS_IS_OK(status)) {
5168 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5172 TALLOC_FREE(smb_fname);
5173 TALLOC_FREE(file_data);
5178 struct fruit_disk_free_state {
5182 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5187 struct smb_filename *bands_dir = NULL;
5188 struct smb_Dir *dir_hnd = NULL;
5189 const char *dname = NULL;
5190 char *talloced = NULL;
5194 path = talloc_asprintf(talloc_tos(),
5196 handle->conn->connectpath,
5202 bands_dir = synthetic_smb_fname(talloc_tos(),
5209 if (bands_dir == NULL) {
5213 status = OpenDir(talloc_tos(),
5219 if (!NT_STATUS_IS_OK(status)) {
5220 TALLOC_FREE(bands_dir);
5221 errno = map_errno_from_nt_status(status);
5227 while ((dname = ReadDirName(dir_hnd, NULL, &talloced))
5230 if (ISDOT(dname) || ISDOTDOT(dname)) {
5235 TALLOC_FREE(dir_hnd);
5237 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5239 TALLOC_FREE(bands_dir);
5245 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5246 struct fruit_disk_free_state *state,
5251 size_t sparsebundle_strlen = strlen("sparsebundle");
5252 size_t bandsize = 0;
5256 p = strstr(name, "sparsebundle");
5261 if (p[sparsebundle_strlen] != '\0') {
5265 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5267 ok = fruit_get_bandsize(handle, name, &bandsize);
5270 * Beware of race conditions: this may be an uninitialized
5271 * Info.plist that a client is just creating. We don't want let
5272 * this to trigger complete failure.
5274 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5278 ok = fruit_get_num_bands(handle, name, &nbands);
5281 * Beware of race conditions: this may be a backup sparsebundle
5282 * in an early stage lacking a bands subdirectory. We don't want
5283 * let this to trigger complete failure.
5285 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5290 * Arithmetic on 32-bit systems may cause overflow, depending on
5291 * size_t precision. First we check its unlikely, then we
5292 * force the precision into target off_t, then we check that
5293 * the total did not overflow either.
5295 if (bandsize > SIZE_MAX/nbands) {
5296 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5300 tm_size = (off_t)bandsize * (off_t)nbands;
5302 if (state->total_size + tm_size < state->total_size) {
5303 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5308 state->total_size += tm_size;
5310 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5311 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5317 * Calculate used size of a TimeMachine volume
5319 * This assumes that the volume is used only for TimeMachine.
5321 * - readdir(basedir of share), then
5322 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5323 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5324 * - count band files in "\1.sparsebundle/bands/"
5325 * - calculate used size of all bands: band_count * band_size
5327 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5328 const struct smb_filename *smb_fname,
5333 struct fruit_config_data *config = NULL;
5334 struct fruit_disk_free_state state = {0};
5335 struct smb_Dir *dir_hnd = NULL;
5336 const char *dname = NULL;
5337 char *talloced = NULL;
5343 SMB_VFS_HANDLE_GET_DATA(handle, config,
5344 struct fruit_config_data,
5347 if (!config->time_machine ||
5348 config->time_machine_max_size == 0)
5350 return SMB_VFS_NEXT_DISK_FREE(handle,
5357 status = OpenDir(talloc_tos(),
5363 if (!NT_STATUS_IS_OK(status)) {
5364 errno = map_errno_from_nt_status(status);
5368 while ((dname = ReadDirName(dir_hnd, NULL, &talloced))
5371 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5373 TALLOC_FREE(talloced);
5374 TALLOC_FREE(dir_hnd);
5377 TALLOC_FREE(talloced);
5380 TALLOC_FREE(dir_hnd);
5382 dsize = config->time_machine_max_size / 512;
5383 dfree = dsize - (state.total_size / 512);
5384 if (dfree > dsize) {
5394 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5395 const SMB_STRUCT_STAT *psbuf)
5397 struct fruit_config_data *config = NULL;
5399 SMB_VFS_HANDLE_GET_DATA(handle, config,
5400 struct fruit_config_data,
5403 if (global_fruit_config.nego_aapl &&
5404 config->aapl_zero_file_id)
5409 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5412 static struct vfs_fn_pointers vfs_fruit_fns = {
5413 .connect_fn = fruit_connect,
5414 .disk_free_fn = fruit_disk_free,
5416 /* File operations */
5417 .fchmod_fn = fruit_fchmod,
5418 .unlinkat_fn = fruit_unlinkat,
5419 .renameat_fn = fruit_renameat,
5420 .openat_fn = fruit_openat,
5421 .close_fn = fruit_close,
5422 .pread_fn = fruit_pread,
5423 .pwrite_fn = fruit_pwrite,
5424 .pread_send_fn = fruit_pread_send,
5425 .pread_recv_fn = fruit_pread_recv,
5426 .pwrite_send_fn = fruit_pwrite_send,
5427 .pwrite_recv_fn = fruit_pwrite_recv,
5428 .fsync_send_fn = fruit_fsync_send,
5429 .fsync_recv_fn = fruit_fsync_recv,
5430 .stat_fn = fruit_stat,
5431 .lstat_fn = fruit_lstat,
5432 .fstat_fn = fruit_fstat,
5433 .fstreaminfo_fn = fruit_fstreaminfo,
5434 .fntimes_fn = fruit_fntimes,
5435 .ftruncate_fn = fruit_ftruncate,
5436 .fallocate_fn = fruit_fallocate,
5437 .create_file_fn = fruit_create_file,
5438 .freaddir_attr_fn = fruit_freaddir_attr,
5439 .offload_read_send_fn = fruit_offload_read_send,
5440 .offload_read_recv_fn = fruit_offload_read_recv,
5441 .offload_write_send_fn = fruit_offload_write_send,
5442 .offload_write_recv_fn = fruit_offload_write_recv,
5443 .fs_file_id_fn = fruit_fs_file_id,
5445 /* NT ACL operations */
5446 .fget_nt_acl_fn = fruit_fget_nt_acl,
5447 .fset_nt_acl_fn = fruit_fset_nt_acl,
5451 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5453 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5455 if (!NT_STATUS_IS_OK(ret)) {
5459 vfs_fruit_debug_level = debug_add_class("fruit");
5460 if (vfs_fruit_debug_level == -1) {
5461 vfs_fruit_debug_level = DBGC_VFS;
5462 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5465 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5466 "vfs_fruit_init","fruit",vfs_fruit_debug_level));