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;
137 bool validate_afpinfo;
140 * Additional options, all enabled by default,
141 * possibly useful for analyzing performance. The associated
142 * operations with each of them may be expensive, so having
143 * the chance to disable them individually gives a chance
144 * tweaking the setup for the particular usecase.
146 bool readdir_attr_rsize;
147 bool readdir_attr_finder_info;
148 bool readdir_attr_max_access;
149 /* Recursion guard. Will go away when we have STATX. */
150 bool in_openat_pathref_fsp;
153 static const struct enum_list fruit_rsrc[] = {
154 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
155 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
156 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
160 static const struct enum_list fruit_meta[] = {
161 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
162 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
166 static const struct enum_list fruit_locking[] = {
167 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
168 {FRUIT_LOCKING_NONE, "none"},
172 static const struct enum_list fruit_encoding[] = {
173 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
174 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
179 vfs_handle_struct *handle;
180 files_struct *fsp; /* backlink to itself */
182 /* tcon config handle */
183 struct fruit_config_data *config;
185 /* Backend fsp for AppleDouble file, can be NULL */
186 files_struct *ad_fsp;
187 /* link from adouble_open_from_base_fsp() to fio */
188 struct fio *real_fio;
190 /* Denote stream type, meta or rsrc */
194 * AFP_AfpInfo stream created, but not written yet, thus still a fake
195 * pipe fd. This is set to true in fruit_open_meta if there was no
196 * existing stream but the caller requested O_CREAT. It is later set to
197 * false when we get a write on the stream that then does open and
205 /*****************************************************************************
207 *****************************************************************************/
209 static struct adouble *ad_get_meta_fsp(TALLOC_CTX *ctx,
210 vfs_handle_struct *handle,
211 const struct smb_filename *smb_fname)
214 struct adouble *ad = NULL;
215 struct smb_filename *smb_fname_cp = NULL;
216 struct fruit_config_data *config = NULL;
218 if (smb_fname->fsp != NULL) {
219 return ad_get(ctx, handle, smb_fname, ADOUBLE_META);
222 SMB_VFS_HANDLE_GET_DATA(handle,
224 struct fruit_config_data,
227 if (config->in_openat_pathref_fsp) {
231 smb_fname_cp = cp_smb_filename(ctx,
233 if (smb_fname_cp == NULL) {
236 TALLOC_FREE(smb_fname_cp->stream_name);
237 config->in_openat_pathref_fsp = true;
238 status = openat_pathref_fsp(handle->conn->cwd_fsp,
240 config->in_openat_pathref_fsp = false;
241 if (!NT_STATUS_IS_OK(status)) {
242 TALLOC_FREE(smb_fname_cp);
246 ad = ad_get(ctx, handle, smb_fname_cp, ADOUBLE_META);
247 TALLOC_FREE(smb_fname_cp);
251 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
254 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
260 if (fio->real_fio != NULL) {
262 * This is an fsp from adouble_open_from_base_fsp()
263 * we should just pass this to the next
273 * Initialize config struct from our smb.conf config parameters
275 static int init_fruit_config(vfs_handle_struct *handle)
277 struct fruit_config_data *config;
279 const char *tm_size_str = NULL;
281 config = talloc_zero(handle->conn, struct fruit_config_data);
283 DEBUG(1, ("talloc_zero() failed\n"));
288 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
289 "resource", fruit_rsrc, FRUIT_RSRC_ADFILE);
291 DEBUG(1, ("value for %s: resource type unknown\n",
292 FRUIT_PARAM_TYPE_NAME));
295 config->rsrc = (enum fruit_rsrc)enumval;
297 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
298 "metadata", fruit_meta, FRUIT_META_NETATALK);
300 DEBUG(1, ("value for %s: metadata type unknown\n",
301 FRUIT_PARAM_TYPE_NAME));
304 config->meta = (enum fruit_meta)enumval;
306 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
307 "locking", fruit_locking, FRUIT_LOCKING_NONE);
309 DEBUG(1, ("value for %s: locking type unknown\n",
310 FRUIT_PARAM_TYPE_NAME));
313 config->locking = (enum fruit_locking)enumval;
315 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
316 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
318 DEBUG(1, ("value for %s: encoding type unknown\n",
319 FRUIT_PARAM_TYPE_NAME));
322 config->encoding = (enum fruit_encoding)enumval;
324 if (config->rsrc == FRUIT_RSRC_ADFILE) {
325 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
326 FRUIT_PARAM_TYPE_NAME,
331 config->use_aapl = lp_parm_bool(
332 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
334 config->time_machine = lp_parm_bool(
335 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
337 config->unix_info_enabled = lp_parm_bool(
338 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
340 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
343 config->posix_rename = lp_parm_bool(
344 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
346 config->aapl_zero_file_id =
347 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
348 "zero_file_id", true);
350 config->readdir_attr_rsize = lp_parm_bool(
351 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
353 config->readdir_attr_finder_info = lp_parm_bool(
354 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
356 config->readdir_attr_max_access = lp_parm_bool(
357 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
359 config->model = lp_parm_const_string(
360 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
362 tm_size_str = lp_parm_const_string(
363 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
364 "time machine max size", NULL);
365 if (tm_size_str != NULL) {
366 config->time_machine_max_size = conv_str_size(tm_size_str);
369 config->convert_adouble = lp_parm_bool(
370 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
371 "convert_adouble", true);
373 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
374 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
375 "wipe_intentionally_left_blank_rfork", false);
377 config->delete_empty_adfiles = lp_parm_bool(
378 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
379 "delete_empty_adfiles", false);
381 config->validate_afpinfo = lp_parm_bool(
382 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
383 "validate_afpinfo", true);
385 SMB_VFS_HANDLE_SET_DATA(handle, config,
386 NULL, struct fruit_config_data,
392 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
393 struct stream_struct **streams,
394 const char *name, off_t size,
397 struct stream_struct *tmp;
399 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
405 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
406 if (tmp[*num_streams].name == NULL) {
410 tmp[*num_streams].size = size;
411 tmp[*num_streams].alloc_size = alloc_size;
418 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
419 struct stream_struct **streams)
421 struct stream_struct *tmp = *streams;
424 if (*num_streams == 0) {
428 for (i = 0; i < *num_streams; i++) {
429 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
434 if (i == *num_streams) {
438 if (tmp[i].size > 0) {
442 TALLOC_FREE(tmp[i].name);
443 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
448 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
449 struct stream_struct **streams,
452 struct stream_struct *tmp = *streams;
455 if (*num_streams == 0) {
459 for (i = 0; i < *num_streams; i++) {
460 if (strequal_m(tmp[i].name, name)) {
465 if (i == *num_streams) {
469 TALLOC_FREE(tmp[i].name);
470 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
475 static bool ad_empty_finderinfo(const struct adouble *ad)
478 char emptybuf[ADEDLEN_FINDERI] = {0};
481 fi = ad_get_entry(ad, ADEID_FINDERI);
483 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
487 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
491 static bool ai_empty_finderinfo(const AfpInfo *ai)
494 char emptybuf[ADEDLEN_FINDERI] = {0};
496 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
501 * Update btime with btime from Netatalk
503 static void update_btime(vfs_handle_struct *handle,
504 struct smb_filename *smb_fname)
507 struct timespec creation_time = {0};
509 struct fruit_config_data *config = NULL;
511 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
514 switch (config->meta) {
515 case FRUIT_META_STREAM:
517 case FRUIT_META_NETATALK:
521 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
525 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
529 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
535 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
536 update_stat_ex_create_time(&smb_fname->st, creation_time);
542 * Map an access mask to a Netatalk single byte byte range lock
544 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
545 uint32_t access_mask)
549 switch (access_mask) {
551 offset = AD_FILELOCK_OPEN_RD;
554 case FILE_WRITE_DATA:
555 case FILE_APPEND_DATA:
556 offset = AD_FILELOCK_OPEN_WR;
560 offset = AD_FILELOCK_OPEN_NONE;
564 if (fork_type == APPLE_FORK_RSRC) {
565 if (offset == AD_FILELOCK_OPEN_NONE) {
566 offset = AD_FILELOCK_RSRC_OPEN_NONE;
576 * Map a deny mode to a Netatalk brl
578 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
585 offset = AD_FILELOCK_DENY_RD;
589 offset = AD_FILELOCK_DENY_WR;
593 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
596 if (fork_type == APPLE_FORK_RSRC) {
604 * Call fcntl() with an exclusive F_GETLK request in order to
605 * determine if there's an existing shared lock
607 * @return true if the requested lock was found or any error occurred
608 * false if the lock was not found
610 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
613 off_t offset = in_offset;
618 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
619 if (result == false) {
623 if (type != F_UNLCK) {
630 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
632 uint32_t access_mask,
635 NTSTATUS status = NT_STATUS_OK;
637 bool share_for_read = (share_mode & FILE_SHARE_READ);
638 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
639 bool netatalk_already_open_for_reading = false;
640 bool netatalk_already_open_for_writing = false;
641 bool netatalk_already_open_with_deny_read = false;
642 bool netatalk_already_open_with_deny_write = false;
643 struct GUID req_guid = GUID_random();
645 /* FIXME: hardcoded data fork, add resource fork */
646 enum apple_fork fork_type = APPLE_FORK_DATA;
648 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
650 access_mask & FILE_READ_DATA ? "READ" :"-",
651 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
654 if (fsp_get_io_fd(fsp) == -1) {
658 /* Read NetATalk opens and deny modes on the file. */
659 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
660 access_to_netatalk_brl(fork_type,
663 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
664 denymode_to_netatalk_brl(fork_type,
667 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
668 access_to_netatalk_brl(fork_type,
671 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
672 denymode_to_netatalk_brl(fork_type,
675 /* If there are any conflicts - sharing violation. */
676 if ((access_mask & FILE_READ_DATA) &&
677 netatalk_already_open_with_deny_read) {
678 return NT_STATUS_SHARING_VIOLATION;
681 if (!share_for_read &&
682 netatalk_already_open_for_reading) {
683 return NT_STATUS_SHARING_VIOLATION;
686 if ((access_mask & FILE_WRITE_DATA) &&
687 netatalk_already_open_with_deny_write) {
688 return NT_STATUS_SHARING_VIOLATION;
691 if (!share_for_write &&
692 netatalk_already_open_for_writing) {
693 return NT_STATUS_SHARING_VIOLATION;
696 if (!(access_mask & FILE_READ_DATA)) {
698 * Nothing we can do here, we need read access
704 /* Set NetAtalk locks matching our access */
705 if (access_mask & FILE_READ_DATA) {
706 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
707 req_guid.time_hi_and_version = __LINE__;
712 fsp->op->global->open_persistent_id,
720 if (!NT_STATUS_IS_OK(status)) {
725 if (!share_for_read) {
726 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
727 req_guid.time_hi_and_version = __LINE__;
732 fsp->op->global->open_persistent_id,
740 if (!NT_STATUS_IS_OK(status)) {
745 if (access_mask & FILE_WRITE_DATA) {
746 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
747 req_guid.time_hi_and_version = __LINE__;
752 fsp->op->global->open_persistent_id,
760 if (!NT_STATUS_IS_OK(status)) {
765 if (!share_for_write) {
766 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
767 req_guid.time_hi_and_version = __LINE__;
772 fsp->op->global->open_persistent_id,
780 if (!NT_STATUS_IS_OK(status)) {
788 static NTSTATUS check_aapl(vfs_handle_struct *handle,
789 struct smb_request *req,
790 const struct smb2_create_blobs *in_context_blobs,
791 struct smb2_create_blobs *out_context_blobs)
793 struct fruit_config_data *config;
795 struct smb2_create_blob *aapl = NULL;
799 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
800 uint64_t req_bitmap, client_caps;
801 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
805 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
806 return NT_STATUS_UNSUCCESSFUL);
808 if (!config->use_aapl
809 || in_context_blobs == NULL
810 || out_context_blobs == NULL) {
814 aapl = smb2_create_blob_find(in_context_blobs,
815 SMB2_CREATE_TAG_AAPL);
820 if (aapl->data.length != 24) {
821 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
822 (uintmax_t)aapl->data.length));
823 return NT_STATUS_INVALID_PARAMETER;
826 cmd = IVAL(aapl->data.data, 0);
827 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
828 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
829 return NT_STATUS_INVALID_PARAMETER;
832 req_bitmap = BVAL(aapl->data.data, 8);
833 client_caps = BVAL(aapl->data.data, 16);
835 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
837 SBVAL(p, 8, req_bitmap);
838 ok = data_blob_append(req, &blob, p, 16);
840 return NT_STATUS_UNSUCCESSFUL;
843 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
844 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
845 (handle->conn->fs_capabilities & FILE_NAMED_STREAMS)) {
846 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
847 config->readdir_attr_enabled = true;
850 if (config->use_copyfile) {
851 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
852 config->copyfile_enabled = true;
856 * The client doesn't set the flag, so we can't check
857 * for it and just set it unconditionally
859 if (config->unix_info_enabled) {
860 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
863 SBVAL(p, 0, server_caps);
864 ok = data_blob_append(req, &blob, p, 8);
866 return NT_STATUS_UNSUCCESSFUL;
870 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
871 int val = lp_case_sensitive(SNUM(handle->conn));
879 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
886 if (config->time_machine) {
887 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
892 ok = data_blob_append(req, &blob, p, 8);
894 return NT_STATUS_UNSUCCESSFUL;
898 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
899 ok = convert_string_talloc(req,
901 config->model, strlen(config->model),
904 return NT_STATUS_UNSUCCESSFUL;
908 SIVAL(p + 4, 0, modellen);
909 ok = data_blob_append(req, &blob, p, 8);
912 return NT_STATUS_UNSUCCESSFUL;
915 ok = data_blob_append(req, &blob, model, modellen);
918 return NT_STATUS_UNSUCCESSFUL;
922 status = smb2_create_blob_add(out_context_blobs,
924 SMB2_CREATE_TAG_AAPL,
926 if (NT_STATUS_IS_OK(status)) {
927 global_fruit_config.nego_aapl = true;
933 static bool readdir_attr_meta_finderi_stream(
934 struct vfs_handle_struct *handle,
935 const struct smb_filename *smb_fname,
938 struct smb_filename *stream_name = NULL;
939 files_struct *fsp = NULL;
943 uint8_t buf[AFP_INFO_SIZE];
945 status = synthetic_pathref(talloc_tos(),
946 handle->conn->cwd_fsp,
947 smb_fname->base_name,
953 if (!NT_STATUS_IS_OK(status)) {
957 status = SMB_VFS_CREATE_FILE(
958 handle->conn, /* conn */
961 stream_name, /* fname */
962 FILE_READ_DATA, /* access_mask */
963 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
965 FILE_OPEN, /* create_disposition*/
966 0, /* create_options */
967 0, /* file_attributes */
968 INTERNAL_OPEN_ONLY, /* oplock_request */
970 0, /* allocation_size */
971 0, /* private_flags */
976 NULL, NULL); /* create context */
978 TALLOC_FREE(stream_name);
980 if (!NT_STATUS_IS_OK(status)) {
984 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
985 if (nread != AFP_INFO_SIZE) {
986 DBG_ERR("short read [%s] [%zd/%d]\n",
987 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
992 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
999 close_file_free(NULL, &fsp, NORMAL_CLOSE);
1005 static bool readdir_attr_meta_finderi_netatalk(
1006 struct vfs_handle_struct *handle,
1007 const struct smb_filename *smb_fname,
1010 struct adouble *ad = NULL;
1013 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
1018 p = ad_get_entry(ad, ADEID_FINDERI);
1020 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
1025 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1030 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1031 const struct smb_filename *smb_fname,
1032 struct readdir_attr_data *attr_data)
1034 struct fruit_config_data *config = NULL;
1035 uint32_t date_added;
1039 SMB_VFS_HANDLE_GET_DATA(handle, config,
1040 struct fruit_config_data,
1043 switch (config->meta) {
1044 case FRUIT_META_NETATALK:
1045 ok = readdir_attr_meta_finderi_netatalk(
1046 handle, smb_fname, &ai);
1049 case FRUIT_META_STREAM:
1050 ok = readdir_attr_meta_finderi_stream(
1051 handle, smb_fname, &ai);
1055 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1060 /* Don't bother with errors, it's likely ENOENT */
1064 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1066 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1067 &ai.afpi_FinderInfo[0], 4);
1069 /* finder_creator */
1070 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1071 &ai.afpi_FinderInfo[4], 4);
1075 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1076 &ai.afpi_FinderInfo[8], 2);
1078 /* finder_ext_flags */
1079 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1080 &ai.afpi_FinderInfo[24], 2);
1083 date_added = convert_time_t_to_uint32_t(
1084 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1086 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1091 static uint64_t readdir_attr_rfork_size_adouble(
1092 struct vfs_handle_struct *handle,
1093 const struct smb_filename *smb_fname)
1095 struct adouble *ad = NULL;
1096 uint64_t rfork_size;
1098 ad = ad_get(talloc_tos(), handle, smb_fname,
1104 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1110 static uint64_t readdir_attr_rfork_size_stream(
1111 struct vfs_handle_struct *handle,
1112 const struct smb_filename *smb_fname)
1114 struct smb_filename *stream_name = NULL;
1116 uint64_t rfork_size;
1118 stream_name = synthetic_smb_fname(talloc_tos(),
1119 smb_fname->base_name,
1120 AFPRESOURCE_STREAM_NAME,
1124 if (stream_name == NULL) {
1128 ret = SMB_VFS_STAT(handle->conn, stream_name);
1130 TALLOC_FREE(stream_name);
1134 rfork_size = stream_name->st.st_ex_size;
1135 TALLOC_FREE(stream_name);
1140 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1141 const struct smb_filename *smb_fname)
1143 struct fruit_config_data *config = NULL;
1144 uint64_t rfork_size;
1146 SMB_VFS_HANDLE_GET_DATA(handle, config,
1147 struct fruit_config_data,
1150 switch (config->rsrc) {
1151 case FRUIT_RSRC_ADFILE:
1152 rfork_size = readdir_attr_rfork_size_adouble(handle,
1156 case FRUIT_RSRC_XATTR:
1157 case FRUIT_RSRC_STREAM:
1158 rfork_size = readdir_attr_rfork_size_stream(handle,
1163 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1171 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1172 const struct smb_filename *smb_fname,
1173 struct readdir_attr_data *attr_data)
1175 NTSTATUS status = NT_STATUS_OK;
1176 struct fruit_config_data *config = NULL;
1179 SMB_VFS_HANDLE_GET_DATA(handle, config,
1180 struct fruit_config_data,
1181 return NT_STATUS_UNSUCCESSFUL);
1184 /* Ensure we return a default value in the creation_date field */
1185 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1188 * Resource fork length
1191 if (config->readdir_attr_rsize) {
1192 uint64_t rfork_size;
1194 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1195 attr_data->attr_data.aapl.rfork_size = rfork_size;
1202 if (config->readdir_attr_finder_info) {
1203 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1205 status = NT_STATUS_INTERNAL_ERROR;
1212 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1217 if (psd->dacl == NULL) {
1218 return NT_STATUS_OK;
1221 for (i = 0; i < psd->dacl->num_aces; i++) {
1222 /* MS NFS style mode/uid/gid */
1223 int cmp = dom_sid_compare_domain(
1224 &global_sid_Unix_NFS,
1225 &psd->dacl->aces[i].trustee);
1227 /* Normal ACE entry. */
1232 * security_descriptor_dacl_del()
1233 * *must* return NT_STATUS_OK as we know
1234 * we have something to remove.
1237 status = security_descriptor_dacl_del(psd,
1238 &psd->dacl->aces[i].trustee);
1239 if (!NT_STATUS_IS_OK(status)) {
1240 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1246 * security_descriptor_dacl_del() may delete more
1247 * then one entry subsequent to this one if the
1248 * SID matches, but we only need to ensure that
1249 * we stay looking at the same element in the array.
1253 return NT_STATUS_OK;
1256 /* Search MS NFS style ACE with UNIX mode */
1257 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1259 struct security_descriptor *psd,
1264 struct fruit_config_data *config = NULL;
1268 SMB_VFS_HANDLE_GET_DATA(handle, config,
1269 struct fruit_config_data,
1270 return NT_STATUS_UNSUCCESSFUL);
1272 if (!global_fruit_config.nego_aapl) {
1273 return NT_STATUS_OK;
1275 if (psd->dacl == NULL || !config->unix_info_enabled) {
1276 return NT_STATUS_OK;
1279 for (i = 0; i < psd->dacl->num_aces; i++) {
1280 if (dom_sid_compare_domain(
1281 &global_sid_Unix_NFS_Mode,
1282 &psd->dacl->aces[i].trustee) == 0) {
1283 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1284 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1287 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1288 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1294 * Remove any incoming virtual ACE entries generated by
1295 * fruit_fget_nt_acl().
1298 return remove_virtual_nfs_aces(psd);
1301 /****************************************************************************
1303 ****************************************************************************/
1305 static int fruit_connect(vfs_handle_struct *handle,
1306 const char *service,
1310 char *list = NULL, *newlist = NULL;
1311 struct fruit_config_data *config;
1312 const struct loadparm_substitution *lp_sub =
1313 loadparm_s3_global_substitution();
1315 DEBUG(10, ("fruit_connect\n"));
1317 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1322 rc = init_fruit_config(handle);
1327 SMB_VFS_HANDLE_GET_DATA(handle, config,
1328 struct fruit_config_data, return -1);
1330 if (config->veto_appledouble) {
1331 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1334 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1335 newlist = talloc_asprintf(
1337 "%s/" ADOUBLE_NAME_PREFIX "*/",
1339 lp_do_parameter(SNUM(handle->conn),
1344 lp_do_parameter(SNUM(handle->conn),
1346 "/" ADOUBLE_NAME_PREFIX "*/");
1352 if (config->encoding == FRUIT_ENC_NATIVE) {
1353 lp_do_parameter(SNUM(handle->conn),
1355 macos_string_replace_map);
1358 if (config->time_machine) {
1359 DBG_NOTICE("Enabling durable handles for Time Machine "
1360 "support on [%s]\n", service);
1361 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1362 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1363 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1364 if (!lp_strict_sync(SNUM(handle->conn))) {
1365 DBG_WARNING("Time Machine without strict sync is not "
1368 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1374 static void fio_ref_destroy_fn(void *p_data)
1376 struct fio *ref_fio = (struct fio *)p_data;
1377 if (ref_fio->real_fio != NULL) {
1378 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1379 ref_fio->real_fio->ad_fsp = NULL;
1380 ref_fio->real_fio = NULL;
1384 static void fio_close_ad_fsp(struct fio *fio)
1386 if (fio->ad_fsp != NULL) {
1387 fd_close(fio->ad_fsp);
1388 file_free(NULL, fio->ad_fsp);
1389 /* fio_ref_destroy_fn() should have cleared this */
1390 SMB_ASSERT(fio->ad_fsp == NULL);
1394 static void fio_destroy_fn(void *p_data)
1396 struct fio *fio = (struct fio *)p_data;
1397 fio_close_ad_fsp(fio);
1400 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1401 const struct files_struct *dirfsp,
1402 const struct smb_filename *smb_fname,
1407 struct fruit_config_data *config = NULL;
1408 struct fio *fio = NULL;
1409 struct vfs_open_how how = {
1410 .flags = flags & ~O_CREAT,
1415 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1417 SMB_VFS_HANDLE_GET_DATA(handle, config,
1418 struct fruit_config_data, return -1);
1420 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1421 fio->handle = handle;
1423 fio->type = ADOUBLE_META;
1424 fio->config = config;
1426 fd = SMB_VFS_NEXT_OPENAT(handle,
1435 if (!(flags & O_CREAT)) {
1436 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1442 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1446 fio->fake_fd = true;
1453 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1454 const struct files_struct *dirfsp,
1455 const struct smb_filename *smb_fname,
1460 struct fruit_config_data *config = NULL;
1461 struct fio *fio = NULL;
1462 struct adouble *ad = NULL;
1463 bool meta_exists = false;
1466 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1469 * We know this is a stream open, so fsp->base_fsp must
1472 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1473 SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1475 ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1482 if (!meta_exists && !(flags & O_CREAT)) {
1492 SMB_VFS_HANDLE_GET_DATA(handle, config,
1493 struct fruit_config_data, return -1);
1495 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1496 fio->handle = handle;
1498 fio->type = ADOUBLE_META;
1499 fio->config = config;
1500 fio->fake_fd = true;
1507 static int fruit_open_meta(vfs_handle_struct *handle,
1508 const struct files_struct *dirfsp,
1509 const struct smb_filename *smb_fname,
1510 files_struct *fsp, int flags, mode_t mode)
1513 struct fruit_config_data *config = NULL;
1515 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1517 SMB_VFS_HANDLE_GET_DATA(handle, config,
1518 struct fruit_config_data, return -1);
1520 switch (config->meta) {
1521 case FRUIT_META_STREAM:
1522 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1526 case FRUIT_META_NETATALK:
1527 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1532 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1536 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1541 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1542 const struct files_struct *dirfsp,
1543 const struct smb_filename *smb_fname,
1549 struct fruit_config_data *config = NULL;
1550 struct files_struct *ad_fsp = NULL;
1551 struct fio *fio = NULL;
1552 struct fio *ref_fio = NULL;
1556 SMB_VFS_HANDLE_GET_DATA(handle, config,
1557 struct fruit_config_data, return -1);
1559 if ((!(flags & O_CREAT)) &&
1560 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1562 /* sorry, but directories don't have a resource fork */
1569 * We return a fake_fd to the vfs modules above,
1570 * while we open an internal backend fsp for the
1571 * '._' file for the next vfs modules.
1573 * Note that adouble_open_from_base_fsp() recurses
1574 * into fruit_openat(), but it'll just pass to
1575 * the next module as just opens a flat file on
1585 status = adouble_open_from_base_fsp(fsp->conn->cwd_fsp,
1591 if (!NT_STATUS_IS_OK(status)) {
1592 errno = map_errno_from_nt_status(status);
1598 * Now we need to glue both handles together,
1599 * so that they automatically detach each other
1602 fio = fruit_get_complete_fio(handle, fsp);
1604 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1610 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1612 fio_ref_destroy_fn);
1613 if (ref_fio == NULL) {
1614 int saved_errno = errno;
1616 file_free(NULL, ad_fsp);
1618 errno = saved_errno;
1623 SMB_ASSERT(ref_fio->fsp == NULL);
1624 ref_fio->handle = handle;
1625 ref_fio->fsp = ad_fsp;
1626 ref_fio->type = ADOUBLE_RSRC;
1627 ref_fio->config = config;
1628 ref_fio->real_fio = fio;
1629 SMB_ASSERT(fio->ad_fsp == NULL);
1630 fio->ad_fsp = ad_fsp;
1631 fio->fake_fd = true;
1635 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1637 int saved_errno = errno;
1639 vfs_fake_fd_close(fd);
1641 errno = saved_errno;
1647 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1648 const struct files_struct *dirfsp,
1649 const struct smb_filename *smb_fname,
1654 #ifdef HAVE_ATTROPEN
1658 * As there's no attropenat() this is only going to work with AT_FDCWD.
1660 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1662 fd = attropen(smb_fname->base_name,
1663 AFPRESOURCE_EA_NETATALK,
1678 static int fruit_open_rsrc(vfs_handle_struct *handle,
1679 const struct files_struct *dirfsp,
1680 const struct smb_filename *smb_fname,
1681 files_struct *fsp, int flags, mode_t mode)
1684 struct fruit_config_data *config = NULL;
1685 struct fio *fio = NULL;
1687 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1689 SMB_VFS_HANDLE_GET_DATA(handle, config,
1690 struct fruit_config_data, return -1);
1692 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1693 fio->handle = handle;
1695 fio->type = ADOUBLE_RSRC;
1696 fio->config = config;
1698 switch (config->rsrc) {
1699 case FRUIT_RSRC_STREAM: {
1700 struct vfs_open_how how = {
1701 .flags = flags, .mode = mode,
1703 fd = SMB_VFS_NEXT_OPENAT(handle,
1711 case FRUIT_RSRC_ADFILE:
1712 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1716 case FRUIT_RSRC_XATTR:
1717 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1722 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1727 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1736 static int fruit_openat(vfs_handle_struct *handle,
1737 const struct files_struct *dirfsp,
1738 const struct smb_filename *smb_fname,
1740 const struct vfs_open_how *how)
1744 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1746 if (!is_named_stream(smb_fname)) {
1747 return SMB_VFS_NEXT_OPENAT(handle,
1754 if (how->resolve != 0) {
1759 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1761 if (is_afpinfo_stream(smb_fname->stream_name)) {
1762 fd = fruit_open_meta(handle,
1768 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1769 fd = fruit_open_rsrc(handle,
1776 fd = SMB_VFS_NEXT_OPENAT(handle,
1783 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1785 /* Prevent reopen optimisation */
1786 fsp->fsp_flags.have_proc_fds = false;
1790 static int fruit_close_meta(vfs_handle_struct *handle,
1794 struct fruit_config_data *config = NULL;
1796 SMB_VFS_HANDLE_GET_DATA(handle, config,
1797 struct fruit_config_data, return -1);
1799 switch (config->meta) {
1800 case FRUIT_META_STREAM:
1802 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1807 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1808 fsp_set_fd(fsp, -1);
1810 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1814 case FRUIT_META_NETATALK:
1815 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1816 fsp_set_fd(fsp, -1);
1820 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1828 static int fruit_close_rsrc(vfs_handle_struct *handle,
1832 struct fruit_config_data *config = NULL;
1834 SMB_VFS_HANDLE_GET_DATA(handle, config,
1835 struct fruit_config_data, return -1);
1837 switch (config->rsrc) {
1838 case FRUIT_RSRC_STREAM:
1839 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1842 case FRUIT_RSRC_ADFILE:
1844 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1848 fio_close_ad_fsp(fio);
1849 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1850 fsp_set_fd(fsp, -1);
1854 case FRUIT_RSRC_XATTR:
1855 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1856 fsp_set_fd(fsp, -1);
1860 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1867 static int fruit_close(vfs_handle_struct *handle,
1873 fd = fsp_get_pathref_fd(fsp);
1875 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1877 if (!fsp_is_alternate_stream(fsp)) {
1878 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1881 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1882 ret = fruit_close_meta(handle, fsp);
1883 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1884 ret = fruit_close_rsrc(handle, fsp);
1886 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1892 static int fruit_renameat(struct vfs_handle_struct *handle,
1893 files_struct *srcfsp,
1894 const struct smb_filename *smb_fname_src,
1895 files_struct *dstfsp,
1896 const struct smb_filename *smb_fname_dst)
1899 struct fruit_config_data *config = NULL;
1900 struct smb_filename *src_adp_smb_fname = NULL;
1901 struct smb_filename *dst_adp_smb_fname = NULL;
1903 SMB_VFS_HANDLE_GET_DATA(handle, config,
1904 struct fruit_config_data, return -1);
1906 if (!VALID_STAT(smb_fname_src->st)) {
1907 DBG_ERR("Need valid stat for [%s]\n",
1908 smb_fname_str_dbg(smb_fname_src));
1912 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1921 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1922 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1927 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1932 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1937 DBG_DEBUG("%s -> %s\n",
1938 smb_fname_str_dbg(src_adp_smb_fname),
1939 smb_fname_str_dbg(dst_adp_smb_fname));
1941 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1946 if (errno == ENOENT) {
1951 TALLOC_FREE(src_adp_smb_fname);
1952 TALLOC_FREE(dst_adp_smb_fname);
1956 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1957 struct files_struct *dirfsp,
1958 const struct smb_filename *smb_fname)
1960 return SMB_VFS_NEXT_UNLINKAT(handle,
1966 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1967 const struct smb_filename *smb_fname)
1969 SMB_ASSERT(smb_fname->fsp != NULL);
1970 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1971 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1972 AFPINFO_EA_NETATALK);
1975 static int fruit_unlink_meta(vfs_handle_struct *handle,
1976 struct files_struct *dirfsp,
1977 const struct smb_filename *smb_fname)
1979 struct fruit_config_data *config = NULL;
1982 SMB_VFS_HANDLE_GET_DATA(handle, config,
1983 struct fruit_config_data, return -1);
1985 switch (config->meta) {
1986 case FRUIT_META_STREAM:
1987 rc = fruit_unlink_meta_stream(handle,
1992 case FRUIT_META_NETATALK:
1993 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1997 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2004 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2005 struct files_struct *dirfsp,
2006 const struct smb_filename *smb_fname,
2011 if (!force_unlink) {
2012 struct smb_filename *full_fname = NULL;
2016 * TODO: use SMB_VFS_STATX() once we have it.
2019 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2022 if (full_fname == NULL) {
2027 * 0 byte resource fork streams are not listed by
2028 * vfs_streaminfo, as a result stream cleanup/deletion of file
2029 * deletion doesn't remove the resourcefork stream.
2032 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2034 TALLOC_FREE(full_fname);
2035 DBG_ERR("stat [%s] failed [%s]\n",
2036 smb_fname_str_dbg(full_fname), strerror(errno));
2040 size = full_fname->st.st_ex_size;
2041 TALLOC_FREE(full_fname);
2044 /* OS X ignores resource fork stream delete requests */
2049 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2053 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2060 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2061 struct files_struct *dirfsp,
2062 const struct smb_filename *smb_fname,
2066 struct adouble *ad = NULL;
2067 struct smb_filename *adp_smb_fname = NULL;
2069 if (!force_unlink) {
2070 struct smb_filename *full_fname = NULL;
2072 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2075 if (full_fname == NULL) {
2079 ad = ad_get(talloc_tos(), handle, full_fname,
2081 TALLOC_FREE(full_fname);
2089 * 0 byte resource fork streams are not listed by
2090 * vfs_streaminfo, as a result stream cleanup/deletion of file
2091 * deletion doesn't remove the resourcefork stream.
2094 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2095 /* OS X ignores resource fork stream delete requests */
2103 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2108 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2112 TALLOC_FREE(adp_smb_fname);
2113 if ((rc != 0) && (errno == ENOENT || errno == ENAMETOOLONG) && force_unlink) {
2120 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2121 const struct smb_filename *smb_fname,
2125 * OS X ignores resource fork stream delete requests, so nothing to do
2126 * here. Removing the file will remove the xattr anyway, so we don't
2127 * have to take care of removing 0 byte resource forks that could be
2133 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2134 struct files_struct *dirfsp,
2135 const struct smb_filename *smb_fname,
2138 struct fruit_config_data *config = NULL;
2141 SMB_VFS_HANDLE_GET_DATA(handle, config,
2142 struct fruit_config_data, return -1);
2144 switch (config->rsrc) {
2145 case FRUIT_RSRC_STREAM:
2146 rc = fruit_unlink_rsrc_stream(handle,
2152 case FRUIT_RSRC_ADFILE:
2153 rc = fruit_unlink_rsrc_adouble(handle,
2159 case FRUIT_RSRC_XATTR:
2160 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2164 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2171 static int fruit_fchmod(vfs_handle_struct *handle,
2172 struct files_struct *fsp,
2176 struct fruit_config_data *config = NULL;
2177 struct smb_filename *smb_fname_adp = NULL;
2178 const struct smb_filename *smb_fname = NULL;
2181 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2186 smb_fname = fsp->fsp_name;
2187 SMB_VFS_HANDLE_GET_DATA(handle, config,
2188 struct fruit_config_data, return -1);
2190 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2194 if (!VALID_STAT(smb_fname->st)) {
2198 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2202 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2207 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2209 if (!NT_STATUS_IS_OK(status)) {
2210 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2211 if (NT_STATUS_EQUAL(status,
2212 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2220 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2222 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2223 if (errno == ENOENT) {
2227 TALLOC_FREE(smb_fname_adp);
2231 static int fruit_unlinkat(vfs_handle_struct *handle,
2232 struct files_struct *dirfsp,
2233 const struct smb_filename *smb_fname,
2236 struct fruit_config_data *config = NULL;
2237 struct smb_filename *rsrc_smb_fname = NULL;
2240 if (flags & AT_REMOVEDIR) {
2241 return SMB_VFS_NEXT_UNLINKAT(handle,
2247 SMB_VFS_HANDLE_GET_DATA(handle, config,
2248 struct fruit_config_data, return -1);
2250 if (is_afpinfo_stream(smb_fname->stream_name)) {
2251 return fruit_unlink_meta(handle,
2254 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2255 return fruit_unlink_rsrc(handle,
2259 } else if (is_named_stream(smb_fname)) {
2260 return SMB_VFS_NEXT_UNLINKAT(handle,
2264 } else if (is_adouble_file(smb_fname->base_name)) {
2265 return SMB_VFS_NEXT_UNLINKAT(handle,
2272 * A request to delete the base file. Because 0 byte resource
2273 * fork streams are not listed by fruit_streaminfo,
2274 * delete_all_streams() can't remove 0 byte resource fork
2275 * streams, so we have to cleanup this here.
2277 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2278 smb_fname->base_name,
2279 AFPRESOURCE_STREAM_NAME,
2283 if (rsrc_smb_fname == NULL) {
2287 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2288 if ((ret != 0) && (errno != ENOENT)) {
2289 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2290 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2291 TALLOC_FREE(rsrc_smb_fname);
2294 TALLOC_FREE(rsrc_smb_fname);
2296 return SMB_VFS_NEXT_UNLINKAT(handle,
2302 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2303 files_struct *fsp, void *data,
2304 size_t n, off_t offset)
2306 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2310 if ((fio == NULL) || fio->fake_fd) {
2314 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2315 if (nread == -1 || nread == n) {
2319 DBG_ERR("Removing [%s] after short read [%zd]\n",
2320 fsp_str_dbg(fsp), nread);
2322 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2327 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2335 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2336 files_struct *fsp, void *data,
2337 size_t n, off_t offset)
2340 struct adouble *ad = NULL;
2341 char afpinfo_buf[AFP_INFO_SIZE];
2345 ai = afpinfo_new(talloc_tos());
2350 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2356 p = ad_get_entry(ad, ADEID_FINDERI);
2358 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2363 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2365 nread = afpinfo_pack(ai, afpinfo_buf);
2366 if (nread != AFP_INFO_SIZE) {
2371 memcpy(data, afpinfo_buf, n);
2379 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2380 files_struct *fsp, void *data,
2381 size_t n, off_t offset)
2383 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2388 * OS X has a off-by-1 error in the offset calculation, so we're
2389 * bug compatible here. It won't hurt, as any relevant real
2390 * world read requests from the AFP_AfpInfo stream will be
2391 * offset=0 n=60. offset is ignored anyway, see below.
2393 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2398 DBG_ERR("Failed to fetch fsp extension\n");
2402 /* Yes, macOS always reads from offset 0 */
2404 to_return = MIN(n, AFP_INFO_SIZE);
2406 switch (fio->config->meta) {
2407 case FRUIT_META_STREAM:
2408 nread = fruit_pread_meta_stream(handle, fsp, data,
2412 case FRUIT_META_NETATALK:
2413 nread = fruit_pread_meta_adouble(handle, fsp, data,
2418 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2422 if (nread == -1 && fio->fake_fd) {
2424 char afpinfo_buf[AFP_INFO_SIZE];
2426 ai = afpinfo_new(talloc_tos());
2431 nread = afpinfo_pack(ai, afpinfo_buf);
2433 if (nread != AFP_INFO_SIZE) {
2437 memcpy(data, afpinfo_buf, to_return);
2444 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2445 files_struct *fsp, void *data,
2446 size_t n, off_t offset)
2448 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2451 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2452 files_struct *fsp, void *data,
2453 size_t n, off_t offset)
2455 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2458 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2459 files_struct *fsp, void *data,
2460 size_t n, off_t offset)
2462 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2463 struct adouble *ad = NULL;
2466 if (fio == NULL || fio->ad_fsp == NULL) {
2467 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2472 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2474 DBG_ERR("ad_fget [%s] failed [%s]\n",
2475 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2479 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2480 offset + ad_getentryoff(ad, ADEID_RFORK));
2486 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2487 files_struct *fsp, void *data,
2488 size_t n, off_t offset)
2490 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2498 switch (fio->config->rsrc) {
2499 case FRUIT_RSRC_STREAM:
2500 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2503 case FRUIT_RSRC_ADFILE:
2504 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2507 case FRUIT_RSRC_XATTR:
2508 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2512 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2519 static ssize_t fruit_pread(vfs_handle_struct *handle,
2520 files_struct *fsp, void *data,
2521 size_t n, off_t offset)
2523 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2526 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2527 fsp_str_dbg(fsp), (intmax_t)offset, n);
2530 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2533 if (fio->type == ADOUBLE_META) {
2534 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2536 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2539 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2543 static bool fruit_must_handle_aio_stream(struct fio *fio)
2549 if (fio->type == ADOUBLE_META) {
2553 if ((fio->type == ADOUBLE_RSRC) &&
2554 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2562 struct fruit_pread_state {
2564 struct vfs_aio_state vfs_aio_state;
2567 static void fruit_pread_done(struct tevent_req *subreq);
2569 static struct tevent_req *fruit_pread_send(
2570 struct vfs_handle_struct *handle,
2571 TALLOC_CTX *mem_ctx,
2572 struct tevent_context *ev,
2573 struct files_struct *fsp,
2575 size_t n, off_t offset)
2577 struct tevent_req *req = NULL;
2578 struct tevent_req *subreq = NULL;
2579 struct fruit_pread_state *state = NULL;
2580 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2582 req = tevent_req_create(mem_ctx, &state,
2583 struct fruit_pread_state);
2588 if (fruit_must_handle_aio_stream(fio)) {
2589 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2590 if (state->nread != n) {
2591 if (state->nread != -1) {
2594 tevent_req_error(req, errno);
2595 return tevent_req_post(req, ev);
2597 tevent_req_done(req);
2598 return tevent_req_post(req, ev);
2601 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2603 if (tevent_req_nomem(req, subreq)) {
2604 return tevent_req_post(req, ev);
2606 tevent_req_set_callback(subreq, fruit_pread_done, req);
2610 static void fruit_pread_done(struct tevent_req *subreq)
2612 struct tevent_req *req = tevent_req_callback_data(
2613 subreq, struct tevent_req);
2614 struct fruit_pread_state *state = tevent_req_data(
2615 req, struct fruit_pread_state);
2617 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2618 TALLOC_FREE(subreq);
2620 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2623 tevent_req_done(req);
2626 static ssize_t fruit_pread_recv(struct tevent_req *req,
2627 struct vfs_aio_state *vfs_aio_state)
2629 struct fruit_pread_state *state = tevent_req_data(
2630 req, struct fruit_pread_state);
2631 ssize_t retval = -1;
2633 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2634 tevent_req_received(req);
2638 *vfs_aio_state = state->vfs_aio_state;
2639 retval = state->nread;
2640 tevent_req_received(req);
2644 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2645 files_struct *fsp, const void *indata,
2646 size_t n, off_t offset)
2648 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2649 const void *data = indata;
2650 char afpinfo_buf[AFP_INFO_SIZE];
2656 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2657 fsp_str_dbg(fsp), (intmax_t)offset, n);
2664 struct vfs_open_how how = {
2665 .flags = fio->flags, .mode = fio->mode,
2667 int fd = fsp_get_pathref_fd(fsp);
2669 ret = vfs_fake_fd_close(fd);
2670 fsp_set_fd(fsp, -1);
2672 DBG_ERR("Close [%s] failed: %s\n",
2673 fsp_str_dbg(fsp), strerror(errno));
2677 fd = SMB_VFS_NEXT_OPENAT(handle,
2678 NULL, /* opening a stream */
2683 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2684 fsp_str_dbg(fsp), strerror(errno));
2687 fsp_set_fd(fsp, fd);
2688 fio->fake_fd = false;
2691 ai = afpinfo_unpack(talloc_tos(), data, fio->config->validate_afpinfo);
2696 if (ai_empty_finderinfo(ai)) {
2698 * Writing an all 0 blob to the metadata stream results in the
2699 * stream being removed on a macOS server. This ensures we
2700 * behave the same and it verified by the "delete AFP_AfpInfo by
2701 * writing all 0" test.
2703 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2705 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2710 ok = set_delete_on_close(
2713 handle->conn->session_info->security_token,
2714 handle->conn->session_info->unix_token);
2716 DBG_ERR("set_delete_on_close on [%s] failed\n",
2723 if (!fio->config->validate_afpinfo) {
2725 * Ensure the buffer contains a valid header, so marshall
2726 * the data from the afpinfo struck back into a buffer
2727 * and write that instead of the possibly malformed data
2728 * we got from the client.
2730 nwritten = afpinfo_pack(ai, afpinfo_buf);
2731 if (nwritten != AFP_INFO_SIZE) {
2738 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2739 if (nwritten != n) {
2746 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2747 files_struct *fsp, const void *data,
2748 size_t n, off_t offset)
2750 struct fruit_config_data *config = NULL;
2751 struct adouble *ad = NULL;
2757 SMB_VFS_HANDLE_GET_DATA(handle, config,
2758 struct fruit_config_data, return -1);
2760 ai = afpinfo_unpack(talloc_tos(), data, config->validate_afpinfo);
2765 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2767 ad = ad_init(talloc_tos(), ADOUBLE_META);
2772 p = ad_get_entry(ad, ADEID_FINDERI);
2774 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2779 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2781 ret = ad_fset(handle, ad, fsp);
2783 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2790 if (!ai_empty_finderinfo(ai)) {
2795 * Writing an all 0 blob to the metadata stream results in the stream
2796 * being removed on a macOS server. This ensures we behave the same and
2797 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2800 ok = set_delete_on_close(
2803 handle->conn->session_info->security_token,
2804 handle->conn->session_info->unix_token);
2806 DBG_ERR("set_delete_on_close on [%s] failed\n",
2814 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2815 files_struct *fsp, const void *data,
2816 size_t n, off_t offset)
2818 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2820 uint8_t buf[AFP_INFO_SIZE];
2826 DBG_ERR("Failed to fetch fsp extension\n");
2835 if (offset != 0 && n < 60) {
2840 if (fio->config->validate_afpinfo) {
2841 cmp = memcmp(data, "AFP", 3);
2848 if (n <= AFP_OFF_FinderInfo) {
2850 * Nothing to do here really, just return
2858 if (to_copy > AFP_INFO_SIZE) {
2859 to_copy = AFP_INFO_SIZE;
2861 memcpy(buf, data, to_copy);
2864 if (to_write != AFP_INFO_SIZE) {
2865 to_write = AFP_INFO_SIZE;
2868 switch (fio->config->meta) {
2869 case FRUIT_META_STREAM:
2870 nwritten = fruit_pwrite_meta_stream(handle,
2877 case FRUIT_META_NETATALK:
2878 nwritten = fruit_pwrite_meta_netatalk(handle,
2886 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2890 if (nwritten != to_write) {
2895 * Return the requested amount, verified against macOS SMB server
2900 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2901 files_struct *fsp, const void *data,
2902 size_t n, off_t offset)
2904 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2907 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2908 files_struct *fsp, const void *data,
2909 size_t n, off_t offset)
2911 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2914 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2915 files_struct *fsp, const void *data,
2916 size_t n, off_t offset)
2918 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2919 struct adouble *ad = NULL;
2923 if (fio == NULL || fio->ad_fsp == NULL) {
2924 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2929 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2931 DBG_ERR("ad_fget [%s] failed [%s]\n",
2932 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2936 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2937 offset + ad_getentryoff(ad, ADEID_RFORK));
2938 if (nwritten != n) {
2939 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2940 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2945 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2946 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2947 ret = ad_fset(handle, ad, fio->ad_fsp);
2949 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2959 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2960 files_struct *fsp, const void *data,
2961 size_t n, off_t offset)
2963 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2967 DBG_ERR("Failed to fetch fsp extension\n");
2971 switch (fio->config->rsrc) {
2972 case FRUIT_RSRC_STREAM:
2973 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2976 case FRUIT_RSRC_ADFILE:
2977 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2980 case FRUIT_RSRC_XATTR:
2981 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2985 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2992 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2993 files_struct *fsp, const void *data,
2994 size_t n, off_t offset)
2996 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2999 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3000 fsp_str_dbg(fsp), (intmax_t)offset, n);
3003 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3006 if (fio->type == ADOUBLE_META) {
3007 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
3009 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
3012 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
3016 struct fruit_pwrite_state {
3018 struct vfs_aio_state vfs_aio_state;
3021 static void fruit_pwrite_done(struct tevent_req *subreq);
3023 static struct tevent_req *fruit_pwrite_send(
3024 struct vfs_handle_struct *handle,
3025 TALLOC_CTX *mem_ctx,
3026 struct tevent_context *ev,
3027 struct files_struct *fsp,
3029 size_t n, off_t offset)
3031 struct tevent_req *req = NULL;
3032 struct tevent_req *subreq = NULL;
3033 struct fruit_pwrite_state *state = NULL;
3034 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3036 req = tevent_req_create(mem_ctx, &state,
3037 struct fruit_pwrite_state);
3042 if (fruit_must_handle_aio_stream(fio)) {
3043 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3044 if (state->nwritten != n) {
3045 if (state->nwritten != -1) {
3048 tevent_req_error(req, errno);
3049 return tevent_req_post(req, ev);
3051 tevent_req_done(req);
3052 return tevent_req_post(req, ev);
3055 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3057 if (tevent_req_nomem(req, subreq)) {
3058 return tevent_req_post(req, ev);
3060 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3064 static void fruit_pwrite_done(struct tevent_req *subreq)
3066 struct tevent_req *req = tevent_req_callback_data(
3067 subreq, struct tevent_req);
3068 struct fruit_pwrite_state *state = tevent_req_data(
3069 req, struct fruit_pwrite_state);
3071 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3072 TALLOC_FREE(subreq);
3074 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3077 tevent_req_done(req);
3080 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3081 struct vfs_aio_state *vfs_aio_state)
3083 struct fruit_pwrite_state *state = tevent_req_data(
3084 req, struct fruit_pwrite_state);
3085 ssize_t retval = -1;
3087 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3088 tevent_req_received(req);
3092 *vfs_aio_state = state->vfs_aio_state;
3093 retval = state->nwritten;
3094 tevent_req_received(req);
3098 struct fruit_fsync_state {
3100 struct vfs_aio_state vfs_aio_state;
3103 static void fruit_fsync_done(struct tevent_req *subreq);
3105 static struct tevent_req *fruit_fsync_send(
3106 struct vfs_handle_struct *handle,
3107 TALLOC_CTX *mem_ctx,
3108 struct tevent_context *ev,
3109 struct files_struct *fsp)
3111 struct tevent_req *req = NULL;
3112 struct tevent_req *subreq = NULL;
3113 struct fruit_fsync_state *state = NULL;
3114 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3116 req = tevent_req_create(mem_ctx, &state,
3117 struct fruit_fsync_state);
3122 if (fruit_must_handle_aio_stream(fio)) {
3123 struct adouble *ad = NULL;
3125 if (fio->type == ADOUBLE_META) {
3127 * We must never pass a fake_fd
3128 * to lower level fsync calls.
3129 * Everything is already done
3130 * synchronously, so just return
3133 SMB_ASSERT(fio->fake_fd);
3134 tevent_req_done(req);
3135 return tevent_req_post(req, ev);
3139 * We know the following must be true,
3140 * as it's the condition for fruit_must_handle_aio_stream()
3141 * to return true if fio->type == ADOUBLE_RSRC.
3143 SMB_ASSERT(fio->config->rsrc == FRUIT_RSRC_ADFILE);
3144 if (fio->ad_fsp == NULL) {
3145 tevent_req_error(req, EBADF);
3146 return tevent_req_post(req, ev);
3148 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3150 tevent_req_error(req, ENOMEM);
3151 return tevent_req_post(req, ev);
3156 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
3157 if (tevent_req_nomem(req, subreq)) {
3158 return tevent_req_post(req, ev);
3160 tevent_req_set_callback(subreq, fruit_fsync_done, req);
3164 static void fruit_fsync_done(struct tevent_req *subreq)
3166 struct tevent_req *req = tevent_req_callback_data(
3167 subreq, struct tevent_req);
3168 struct fruit_fsync_state *state = tevent_req_data(
3169 req, struct fruit_fsync_state);
3171 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
3172 TALLOC_FREE(subreq);
3173 if (state->ret != 0) {
3174 tevent_req_error(req, errno);
3177 tevent_req_done(req);
3180 static int fruit_fsync_recv(struct tevent_req *req,
3181 struct vfs_aio_state *vfs_aio_state)
3183 struct fruit_fsync_state *state = tevent_req_data(
3184 req, struct fruit_fsync_state);
3187 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3188 tevent_req_received(req);
3192 *vfs_aio_state = state->vfs_aio_state;
3193 retval = state->ret;
3194 tevent_req_received(req);
3199 * Helper to stat/lstat the base file of an smb_fname.
3201 static int fruit_stat_base(vfs_handle_struct *handle,
3202 struct smb_filename *smb_fname,
3205 char *tmp_stream_name;
3208 tmp_stream_name = smb_fname->stream_name;
3209 smb_fname->stream_name = NULL;
3211 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3213 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3215 smb_fname->stream_name = tmp_stream_name;
3217 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3218 smb_fname->base_name,
3219 (uintmax_t)smb_fname->st.st_ex_dev,
3220 (uintmax_t)smb_fname->st.st_ex_ino);
3224 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3225 struct smb_filename *smb_fname,
3231 ret = fruit_stat_base(handle, smb_fname, false);
3236 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3239 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3241 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3244 smb_fname->st.st_ex_ino = ino;
3249 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3250 struct smb_filename *smb_fname,
3253 struct adouble *ad = NULL;
3255 /* Populate the stat struct with info from the base file. */
3256 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3260 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3262 DBG_INFO("fruit_stat_meta %s: %s\n",
3263 smb_fname_str_dbg(smb_fname), strerror(errno));
3269 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3270 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3271 smb_fname->stream_name);
3275 static int fruit_stat_meta(vfs_handle_struct *handle,
3276 struct smb_filename *smb_fname,
3279 struct fruit_config_data *config = NULL;
3282 SMB_VFS_HANDLE_GET_DATA(handle, config,
3283 struct fruit_config_data, return -1);
3285 switch (config->meta) {
3286 case FRUIT_META_STREAM:
3287 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3290 case FRUIT_META_NETATALK:
3291 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3295 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3302 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3303 struct smb_filename *smb_fname,
3306 struct adouble *ad = NULL;
3309 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3315 /* Populate the stat struct with info from the base file. */
3316 ret = fruit_stat_base(handle, smb_fname, follow_links);
3322 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3323 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3324 smb_fname->stream_name);
3329 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3330 struct smb_filename *smb_fname,
3336 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3338 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3344 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3345 struct smb_filename *smb_fname,
3348 #ifdef HAVE_ATTROPEN
3352 /* Populate the stat struct with info from the base file. */
3353 ret = fruit_stat_base(handle, smb_fname, follow_links);
3358 fd = attropen(smb_fname->base_name,
3359 AFPRESOURCE_EA_NETATALK,
3365 ret = sys_fstat(fd, &smb_fname->st, false);
3368 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3369 AFPRESOURCE_EA_NETATALK);
3375 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3376 smb_fname->stream_name);
3386 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3387 struct smb_filename *smb_fname,
3390 struct fruit_config_data *config = NULL;
3393 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3395 SMB_VFS_HANDLE_GET_DATA(handle, config,
3396 struct fruit_config_data, return -1);
3398 switch (config->rsrc) {
3399 case FRUIT_RSRC_STREAM:
3400 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3403 case FRUIT_RSRC_XATTR:
3404 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3407 case FRUIT_RSRC_ADFILE:
3408 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3412 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3419 static int fruit_stat(vfs_handle_struct *handle,
3420 struct smb_filename *smb_fname)
3424 DEBUG(10, ("fruit_stat called for %s\n",
3425 smb_fname_str_dbg(smb_fname)));
3427 if (!is_named_stream(smb_fname)) {
3428 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3430 update_btime(handle, smb_fname);
3436 * Note if lp_posix_paths() is true, we can never
3437 * get here as is_ntfs_stream_smb_fname() is
3438 * always false. So we never need worry about
3439 * not following links here.
3442 if (is_afpinfo_stream(smb_fname->stream_name)) {
3443 rc = fruit_stat_meta(handle, smb_fname, true);
3444 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3445 rc = fruit_stat_rsrc(handle, smb_fname, true);
3447 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3451 update_btime(handle, smb_fname);
3452 smb_fname->st.st_ex_mode &= ~S_IFMT;
3453 smb_fname->st.st_ex_mode |= S_IFREG;
3454 smb_fname->st.st_ex_blocks =
3455 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3460 static int fruit_lstat(vfs_handle_struct *handle,
3461 struct smb_filename *smb_fname)
3465 DEBUG(10, ("fruit_lstat called for %s\n",
3466 smb_fname_str_dbg(smb_fname)));
3468 if (!is_named_stream(smb_fname)) {
3469 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3471 update_btime(handle, smb_fname);
3476 if (is_afpinfo_stream(smb_fname->stream_name)) {
3477 rc = fruit_stat_meta(handle, smb_fname, false);
3478 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3479 rc = fruit_stat_rsrc(handle, smb_fname, false);
3481 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3485 update_btime(handle, smb_fname);
3486 smb_fname->st.st_ex_mode &= ~S_IFMT;
3487 smb_fname->st.st_ex_mode |= S_IFREG;
3488 smb_fname->st.st_ex_blocks =
3489 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3494 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3496 SMB_STRUCT_STAT *sbuf)
3498 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3499 struct smb_filename smb_fname;
3508 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3513 *sbuf = fsp->base_fsp->fsp_name->st;
3514 sbuf->st_ex_size = AFP_INFO_SIZE;
3515 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3519 smb_fname = (struct smb_filename) {
3520 .base_name = fsp->fsp_name->base_name,
3521 .twrp = fsp->fsp_name->twrp,
3524 ret = fruit_stat_base(handle, &smb_fname, false);
3528 *sbuf = smb_fname.st;
3530 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3532 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3537 sbuf->st_ex_ino = ino;
3541 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3543 SMB_STRUCT_STAT *sbuf)
3547 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3552 *sbuf = fsp->base_fsp->fsp_name->st;
3553 sbuf->st_ex_size = AFP_INFO_SIZE;
3554 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3559 static int fruit_fstat_meta(vfs_handle_struct *handle,
3561 SMB_STRUCT_STAT *sbuf,
3566 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3568 switch (fio->config->meta) {
3569 case FRUIT_META_STREAM:
3570 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3573 case FRUIT_META_NETATALK:
3574 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3578 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3582 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3586 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3588 SMB_STRUCT_STAT *sbuf)
3590 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3593 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3595 SMB_STRUCT_STAT *sbuf)
3597 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3600 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3602 SMB_STRUCT_STAT *sbuf)
3604 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3605 struct adouble *ad = NULL;
3608 if (fio == NULL || fio->ad_fsp == NULL) {
3609 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3614 /* Populate the stat struct with info from the base file. */
3615 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3620 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3622 DBG_ERR("ad_fget [%s] failed [%s]\n",
3623 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3627 *sbuf = fsp->base_fsp->fsp_name->st;
3628 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3629 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3635 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3636 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3640 switch (fio->config->rsrc) {
3641 case FRUIT_RSRC_STREAM:
3642 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3645 case FRUIT_RSRC_ADFILE:
3646 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3649 case FRUIT_RSRC_XATTR:
3650 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3654 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3661 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3662 SMB_STRUCT_STAT *sbuf)
3664 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3668 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3671 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3673 if (fio->type == ADOUBLE_META) {
3674 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3676 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3680 sbuf->st_ex_mode &= ~S_IFMT;
3681 sbuf->st_ex_mode |= S_IFREG;
3682 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3685 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3686 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3690 static NTSTATUS delete_invalid_meta_stream(
3691 vfs_handle_struct *handle,
3692 const struct smb_filename *smb_fname,
3693 TALLOC_CTX *mem_ctx,
3694 unsigned int *pnum_streams,
3695 struct stream_struct **pstreams,
3698 struct smb_filename *sname = NULL;
3703 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3705 return NT_STATUS_INTERNAL_ERROR;
3709 return NT_STATUS_OK;
3712 status = synthetic_pathref(talloc_tos(),
3713 handle->conn->cwd_fsp,
3714 smb_fname->base_name,
3715 AFPINFO_STREAM_NAME,
3720 if (!NT_STATUS_IS_OK(status)) {
3721 return NT_STATUS_NO_MEMORY;
3724 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3725 handle->conn->cwd_fsp,
3729 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3731 return map_nt_error_from_unix(errno);
3735 return NT_STATUS_OK;
3738 static NTSTATUS fruit_streaminfo_meta_stream(
3739 vfs_handle_struct *handle,
3740 struct files_struct *fsp,
3741 const struct smb_filename *smb_fname,
3742 TALLOC_CTX *mem_ctx,
3743 unsigned int *pnum_streams,
3744 struct stream_struct **pstreams)
3746 struct stream_struct *stream = *pstreams;
3747 unsigned int num_streams = *pnum_streams;
3750 for (i = 0; i < num_streams; i++) {
3751 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3756 if (i == num_streams) {
3757 return NT_STATUS_OK;
3760 if (stream[i].size != AFP_INFO_SIZE) {
3761 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3762 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3764 return delete_invalid_meta_stream(handle,
3773 return NT_STATUS_OK;
3776 static NTSTATUS fruit_streaminfo_meta_netatalk(
3777 vfs_handle_struct *handle,
3778 struct files_struct *fsp,
3779 const struct smb_filename *smb_fname,
3780 TALLOC_CTX *mem_ctx,
3781 unsigned int *pnum_streams,
3782 struct stream_struct **pstreams)
3784 struct stream_struct *stream = *pstreams;
3785 unsigned int num_streams = *pnum_streams;
3786 struct adouble *ad = NULL;
3791 /* Remove the Netatalk xattr from the list */
3792 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3793 ":" NETATALK_META_XATTR ":$DATA");
3795 return NT_STATUS_NO_MEMORY;
3799 * Check if there's a AFPINFO_STREAM from the VFS streams
3800 * backend and if yes, remove it from the list
3802 for (i = 0; i < num_streams; i++) {
3803 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3808 if (i < num_streams) {
3809 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3810 smb_fname_str_dbg(smb_fname));
3812 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3815 return NT_STATUS_INTERNAL_ERROR;
3819 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3821 return NT_STATUS_OK;
3824 is_fi_empty = ad_empty_finderinfo(ad);
3828 return NT_STATUS_OK;
3831 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3832 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3833 smb_roundup(handle->conn, AFP_INFO_SIZE));
3835 return NT_STATUS_NO_MEMORY;
3838 return NT_STATUS_OK;
3841 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3842 struct files_struct *fsp,
3843 const struct smb_filename *smb_fname,
3844 TALLOC_CTX *mem_ctx,
3845 unsigned int *pnum_streams,
3846 struct stream_struct **pstreams)
3848 struct fruit_config_data *config = NULL;
3851 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3852 return NT_STATUS_INTERNAL_ERROR);
3854 switch (config->meta) {
3855 case FRUIT_META_NETATALK:
3856 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3857 mem_ctx, pnum_streams,
3861 case FRUIT_META_STREAM:
3862 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3863 mem_ctx, pnum_streams,
3868 return NT_STATUS_INTERNAL_ERROR;
3874 static NTSTATUS fruit_streaminfo_rsrc_stream(
3875 vfs_handle_struct *handle,
3876 struct files_struct *fsp,
3877 const struct smb_filename *smb_fname,
3878 TALLOC_CTX *mem_ctx,
3879 unsigned int *pnum_streams,
3880 struct stream_struct **pstreams)
3884 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3886 DBG_ERR("Filtering resource stream failed\n");
3887 return NT_STATUS_INTERNAL_ERROR;
3889 return NT_STATUS_OK;
3892 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3893 vfs_handle_struct *handle,
3894 struct files_struct *fsp,
3895 const struct smb_filename *smb_fname,
3896 TALLOC_CTX *mem_ctx,
3897 unsigned int *pnum_streams,
3898 struct stream_struct **pstreams)
3902 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3904 DBG_ERR("Filtering resource stream failed\n");
3905 return NT_STATUS_INTERNAL_ERROR;
3907 return NT_STATUS_OK;
3910 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3911 vfs_handle_struct *handle,
3912 struct files_struct *fsp,
3913 const struct smb_filename *smb_fname,
3914 TALLOC_CTX *mem_ctx,
3915 unsigned int *pnum_streams,
3916 struct stream_struct **pstreams)
3918 struct stream_struct *stream = *pstreams;
3919 unsigned int num_streams = *pnum_streams;
3920 struct adouble *ad = NULL;
3926 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3927 * and if yes, remove it from the list
3929 for (i = 0; i < num_streams; i++) {
3930 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3935 if (i < num_streams) {
3936 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3937 smb_fname_str_dbg(smb_fname));
3939 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3940 AFPRESOURCE_STREAM);
3942 return NT_STATUS_INTERNAL_ERROR;
3946 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3948 return NT_STATUS_OK;
3951 rlen = ad_getentrylen(ad, ADEID_RFORK);
3955 return NT_STATUS_OK;
3958 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3959 AFPRESOURCE_STREAM_NAME, rlen,
3960 smb_roundup(handle->conn, rlen));
3962 return NT_STATUS_NO_MEMORY;
3965 return NT_STATUS_OK;
3968 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3969 struct files_struct *fsp,
3970 const struct smb_filename *smb_fname,
3971 TALLOC_CTX *mem_ctx,
3972 unsigned int *pnum_streams,
3973 struct stream_struct **pstreams)
3975 struct fruit_config_data *config = NULL;
3978 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
3979 return NT_STATUS_OK;
3982 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3983 return NT_STATUS_INTERNAL_ERROR);
3985 switch (config->rsrc) {
3986 case FRUIT_RSRC_STREAM:
3987 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3988 mem_ctx, pnum_streams,
3992 case FRUIT_RSRC_XATTR:
3993 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3994 mem_ctx, pnum_streams,
3998 case FRUIT_RSRC_ADFILE:
3999 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4000 mem_ctx, pnum_streams,
4005 return NT_STATUS_INTERNAL_ERROR;
4011 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
4012 struct stream_struct **pstreams)
4014 unsigned num_streams = *pnum_streams;
4015 struct stream_struct *streams = *pstreams;
4018 if (!global_fruit_config.nego_aapl) {
4022 while (i < num_streams) {
4023 struct smb_filename smb_fname = (struct smb_filename) {
4024 .stream_name = streams[i].name,
4027 if (is_ntfs_default_stream_smb_fname(&smb_fname)
4028 || streams[i].size > 0)
4034 streams[i] = streams[num_streams - 1];
4038 *pnum_streams = num_streams;
4041 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
4042 struct files_struct *fsp,
4043 TALLOC_CTX *mem_ctx,
4044 unsigned int *pnum_streams,
4045 struct stream_struct **pstreams)
4047 struct fruit_config_data *config = NULL;
4048 const struct smb_filename *smb_fname = NULL;
4051 smb_fname = fsp->fsp_name;
4053 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4054 return NT_STATUS_UNSUCCESSFUL);
4056 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4058 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
4059 pnum_streams, pstreams);
4060 if (!NT_STATUS_IS_OK(status)) {
4064 fruit_filter_empty_streams(pnum_streams, pstreams);
4066 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4067 mem_ctx, pnum_streams, pstreams);
4068 if (!NT_STATUS_IS_OK(status)) {
4072 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4073 mem_ctx, pnum_streams, pstreams);
4074 if (!NT_STATUS_IS_OK(status)) {
4078 return NT_STATUS_OK;
4081 static int fruit_fntimes(vfs_handle_struct *handle,
4083 struct smb_file_time *ft)
4086 struct adouble *ad = NULL;
4087 struct fruit_config_data *config = NULL;
4089 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4092 if ((config->meta != FRUIT_META_NETATALK) ||
4093 is_omit_timespec(&ft->create_time))
4095 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4098 DBG_DEBUG("set btime for %s to %s", fsp_str_dbg(fsp),
4099 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
4101 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4106 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4107 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4109 rc = ad_fset(handle, ad, fsp);
4115 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
4118 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4121 static int fruit_fallocate(struct vfs_handle_struct *handle,
4122 struct files_struct *fsp,
4127 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4130 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4133 /* Let the pwrite code path handle it. */
4138 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4139 struct files_struct *fsp,
4142 #ifdef HAVE_ATTROPEN
4143 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4148 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4149 struct files_struct *fsp,
4152 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4154 struct adouble *ad = NULL;
4157 if (fio == NULL || fio->ad_fsp == NULL) {
4158 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4163 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4165 DBG_ERR("ad_fget [%s] failed [%s]\n",
4166 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4170 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4172 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4178 ad_setentrylen(ad, ADEID_RFORK, offset);
4180 rc = ad_fset(handle, ad, fio->ad_fsp);
4182 DBG_ERR("ad_fset [%s] failed [%s]\n",
4183 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4192 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4193 struct files_struct *fsp,
4196 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4199 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4200 struct files_struct *fsp,
4203 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4207 DBG_ERR("Failed to fetch fsp extension\n");
4211 switch (fio->config->rsrc) {
4212 case FRUIT_RSRC_XATTR:
4213 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4216 case FRUIT_RSRC_ADFILE:
4217 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4220 case FRUIT_RSRC_STREAM:
4221 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4225 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4233 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4234 struct files_struct *fsp,
4238 DBG_WARNING("ftruncate %s to %jd\n",
4239 fsp_str_dbg(fsp), (intmax_t)offset);
4240 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4245 /* OS X returns success but does nothing */
4246 DBG_INFO("ignoring ftruncate %s to %jd\n",
4247 fsp_str_dbg(fsp), (intmax_t)offset);
4251 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4252 struct files_struct *fsp,
4255 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4258 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4262 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4265 if (fio->type == ADOUBLE_META) {
4266 ret = fruit_ftruncate_meta(handle, fsp, offset);
4268 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4271 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4275 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4276 struct smb_request *req,
4277 struct files_struct *dirfsp,
4278 struct smb_filename *smb_fname,
4279 uint32_t access_mask,
4280 uint32_t share_access,
4281 uint32_t create_disposition,
4282 uint32_t create_options,
4283 uint32_t file_attributes,
4284 uint32_t oplock_request,
4285 const struct smb2_lease *lease,
4286 uint64_t allocation_size,
4287 uint32_t private_flags,
4288 struct security_descriptor *sd,
4289 struct ea_list *ea_list,
4290 files_struct **result,
4292 const struct smb2_create_blobs *in_context_blobs,
4293 struct smb2_create_blobs *out_context_blobs)
4296 struct fruit_config_data *config = NULL;
4297 files_struct *fsp = NULL;
4298 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4301 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4302 if (!NT_STATUS_IS_OK(status)) {
4306 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4307 return NT_STATUS_UNSUCCESSFUL);
4309 if (is_apple_stream(smb_fname->stream_name) &&
4311 config->convert_adouble)
4313 uint32_t conv_flags = 0;
4315 if (config->wipe_intentionally_left_blank_rfork) {
4316 conv_flags |= AD_CONV_WIPE_BLANK;
4318 if (config->delete_empty_adfiles) {
4319 conv_flags |= AD_CONV_DELETE;
4322 ret = ad_convert(handle,
4324 macos_string_replace_map,
4327 DBG_ERR("ad_convert(\"%s\") failed\n",
4328 smb_fname_str_dbg(smb_fname));
4332 status = SMB_VFS_NEXT_CREATE_FILE(
4333 handle, req, dirfsp, smb_fname,
4334 access_mask, share_access,
4335 create_disposition, create_options,
4336 file_attributes, oplock_request,
4338 allocation_size, private_flags,
4339 sd, ea_list, result,
4340 pinfo, in_context_blobs, out_context_blobs);
4341 if (!NT_STATUS_IS_OK(status)) {
4347 if (global_fruit_config.nego_aapl) {
4348 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4350 * Enable POSIX directory rename behaviour
4352 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4357 * If this is a plain open for existing files, opening an 0
4358 * byte size resource fork MUST fail with
4359 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4361 * Cf the vfs_fruit torture tests in test_rfork_create().
4363 if (global_fruit_config.nego_aapl &&
4364 create_disposition == FILE_OPEN &&
4365 smb_fname->st.st_ex_size == 0 &&
4366 is_named_stream(smb_fname))
4368 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4372 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4376 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4377 (fsp->op != NULL) &&
4378 !fsp->fsp_flags.is_pathref)
4380 status = fruit_check_access(
4384 if (!NT_STATUS_IS_OK(status)) {
4392 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4395 close_file_free(req, &fsp, ERROR_CLOSE);
4402 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4403 struct files_struct *fsp,
4404 TALLOC_CTX *mem_ctx,
4405 struct readdir_attr_data **pattr_data)
4407 struct fruit_config_data *config = NULL;
4408 struct readdir_attr_data *attr_data;
4409 uint32_t conv_flags = 0;
4413 SMB_VFS_HANDLE_GET_DATA(handle, config,
4414 struct fruit_config_data,
4415 return NT_STATUS_UNSUCCESSFUL);
4417 if (!global_fruit_config.nego_aapl) {
4418 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4424 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4426 if (config->convert_adouble) {
4427 if (config->wipe_intentionally_left_blank_rfork) {
4428 conv_flags |= AD_CONV_WIPE_BLANK;
4430 if (config->delete_empty_adfiles) {
4431 conv_flags |= AD_CONV_DELETE;
4434 ret = ad_convert(handle,
4436 macos_string_replace_map,
4439 DBG_ERR("ad_convert(\"%s\") failed\n",
4444 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4445 if (*pattr_data == NULL) {
4446 return NT_STATUS_NO_MEMORY;
4448 attr_data = *pattr_data;
4449 attr_data->type = RDATTR_AAPL;
4452 * Mac metadata: compressed FinderInfo, resource fork length
4455 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4456 if (!NT_STATUS_IS_OK(status)) {
4458 * Error handling is tricky: if we return failure from
4459 * this function, the corresponding directory entry
4460 * will to be passed to the client, so we really just
4461 * want to error out on fatal errors.
4463 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4471 if (config->unix_info_enabled) {
4472 attr_data->attr_data.aapl.unix_mode =
4473 fsp->fsp_name->st.st_ex_mode;
4479 if (!config->readdir_attr_max_access) {
4480 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4482 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4485 SEC_FLAG_MAXIMUM_ALLOWED,
4486 &attr_data->attr_data.aapl.max_access);
4487 if (!NT_STATUS_IS_OK(status)) {
4492 return NT_STATUS_OK;
4495 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4497 TALLOC_FREE(*pattr_data);
4501 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4503 uint32_t security_info,
4504 TALLOC_CTX *mem_ctx,
4505 struct security_descriptor **ppdesc)
4508 struct security_ace ace;
4510 struct fruit_config_data *config;
4512 SMB_VFS_HANDLE_GET_DATA(handle, config,
4513 struct fruit_config_data,
4514 return NT_STATUS_UNSUCCESSFUL);
4516 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4518 if (!NT_STATUS_IS_OK(status)) {
4523 * Add MS NFS style ACEs with uid, gid and mode
4525 if (!global_fruit_config.nego_aapl) {
4526 return NT_STATUS_OK;
4528 if (!config->unix_info_enabled) {
4529 return NT_STATUS_OK;
4532 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4533 status = remove_virtual_nfs_aces(*ppdesc);
4534 if (!NT_STATUS_IS_OK(status)) {
4535 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4539 /* MS NFS style mode */
4540 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4541 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4542 status = security_descriptor_dacl_add(*ppdesc, &ace);
4543 if (!NT_STATUS_IS_OK(status)) {
4544 DEBUG(1,("failed to add MS NFS style ACE\n"));
4548 /* MS NFS style uid */
4549 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4550 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4551 status = security_descriptor_dacl_add(*ppdesc, &ace);
4552 if (!NT_STATUS_IS_OK(status)) {
4553 DEBUG(1,("failed to add MS NFS style ACE\n"));
4557 /* MS NFS style gid */
4558 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4559 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4560 status = security_descriptor_dacl_add(*ppdesc, &ace);
4561 if (!NT_STATUS_IS_OK(status)) {
4562 DEBUG(1,("failed to add MS NFS style ACE\n"));
4566 return NT_STATUS_OK;
4569 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4571 uint32_t security_info_sent,
4572 const struct security_descriptor *orig_psd)
4576 mode_t ms_nfs_mode = 0;
4578 struct security_descriptor *psd = NULL;
4579 uint32_t orig_num_aces = 0;
4581 if (orig_psd->dacl != NULL) {
4582 orig_num_aces = orig_psd->dacl->num_aces;
4585 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4587 return NT_STATUS_NO_MEMORY;
4590 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4592 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4593 if (!NT_STATUS_IS_OK(status)) {
4594 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4600 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4601 * sent/present flags correctly now we've removed them.
4604 if (orig_num_aces != 0) {
4606 * Are there any ACE's left ?
4608 if (psd->dacl->num_aces == 0) {
4609 /* No - clear the DACL sent/present flags. */
4610 security_info_sent &= ~SECINFO_DACL;
4611 psd->type &= ~SEC_DESC_DACL_PRESENT;
4615 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4616 if (!NT_STATUS_IS_OK(status)) {
4617 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4623 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4625 DBG_WARNING("%s, result: %d, %04o error %s\n",
4628 (unsigned)ms_nfs_mode,
4630 status = map_nt_error_from_unix(errno);
4637 return NT_STATUS_OK;
4640 static struct vfs_offload_ctx *fruit_offload_ctx;
4642 struct fruit_offload_read_state {
4643 struct vfs_handle_struct *handle;
4644 struct tevent_context *ev;
4652 static void fruit_offload_read_done(struct tevent_req *subreq);
4654 static struct tevent_req *fruit_offload_read_send(
4655 TALLOC_CTX *mem_ctx,
4656 struct tevent_context *ev,
4657 struct vfs_handle_struct *handle,
4664 struct tevent_req *req = NULL;
4665 struct tevent_req *subreq = NULL;
4666 struct fruit_offload_read_state *state = NULL;
4668 req = tevent_req_create(mem_ctx, &state,
4669 struct fruit_offload_read_state);
4673 *state = (struct fruit_offload_read_state) {
4680 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4681 fsctl, ttl, offset, to_copy);
4682 if (tevent_req_nomem(subreq, req)) {
4683 return tevent_req_post(req, ev);
4685 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4689 static void fruit_offload_read_done(struct tevent_req *subreq)
4691 struct tevent_req *req = tevent_req_callback_data(
4692 subreq, struct tevent_req);
4693 struct fruit_offload_read_state *state = tevent_req_data(
4694 req, struct fruit_offload_read_state);
4697 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4703 TALLOC_FREE(subreq);
4704 if (tevent_req_nterror(req, status)) {
4708 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4709 tevent_req_done(req);
4713 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4714 &fruit_offload_ctx);
4715 if (tevent_req_nterror(req, status)) {
4719 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4722 if (tevent_req_nterror(req, status)) {
4726 tevent_req_done(req);
4730 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4731 struct vfs_handle_struct *handle,
4732 TALLOC_CTX *mem_ctx,
4737 struct fruit_offload_read_state *state = tevent_req_data(
4738 req, struct fruit_offload_read_state);
4741 if (tevent_req_is_nterror(req, &status)) {
4742 tevent_req_received(req);
4746 *flags = state->flags;
4747 *xferlen = state->xferlen;
4748 token->length = state->token.length;
4749 token->data = talloc_move(mem_ctx, &state->token.data);
4751 tevent_req_received(req);
4752 return NT_STATUS_OK;
4755 struct fruit_offload_write_state {
4756 struct vfs_handle_struct *handle;
4758 struct files_struct *src_fsp;
4759 struct files_struct *dst_fsp;
4763 static void fruit_offload_write_done(struct tevent_req *subreq);
4764 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4765 TALLOC_CTX *mem_ctx,
4766 struct tevent_context *ev,
4769 off_t transfer_offset,
4770 struct files_struct *dest_fsp,
4774 struct tevent_req *req, *subreq;
4775 struct fruit_offload_write_state *state;
4777 struct fruit_config_data *config;
4778 off_t src_off = transfer_offset;
4779 files_struct *src_fsp = NULL;
4780 off_t to_copy = num;
4781 bool copyfile_enabled = false;
4783 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4784 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4786 SMB_VFS_HANDLE_GET_DATA(handle, config,
4787 struct fruit_config_data,
4790 req = tevent_req_create(mem_ctx, &state,
4791 struct fruit_offload_write_state);
4795 state->handle = handle;
4796 state->dst_fsp = dest_fsp;
4799 case FSCTL_SRV_COPYCHUNK:
4800 case FSCTL_SRV_COPYCHUNK_WRITE:
4801 copyfile_enabled = config->copyfile_enabled;
4808 * Check if this a OS X copyfile style copychunk request with
4809 * a requested chunk count of 0 that was translated to a
4810 * offload_write_send VFS call overloading the parameters src_off
4811 * = dest_off = num = 0.
4813 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4814 status = vfs_offload_token_db_fetch_fsp(
4815 fruit_offload_ctx, token, &src_fsp);
4816 if (tevent_req_nterror(req, status)) {
4817 return tevent_req_post(req, ev);
4819 state->src_fsp = src_fsp;
4821 status = vfs_stat_fsp(src_fsp);
4822 if (tevent_req_nterror(req, status)) {
4823 return tevent_req_post(req, ev);
4826 to_copy = src_fsp->fsp_name->st.st_ex_size;
4827 state->is_copyfile = true;
4830 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4839 if (tevent_req_nomem(subreq, req)) {
4840 return tevent_req_post(req, ev);
4843 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4847 static void fruit_offload_write_done(struct tevent_req *subreq)
4849 struct tevent_req *req = tevent_req_callback_data(
4850 subreq, struct tevent_req);
4851 struct fruit_offload_write_state *state = tevent_req_data(
4852 req, struct fruit_offload_write_state);
4854 unsigned int num_streams = 0;
4855 struct stream_struct *streams = NULL;
4857 struct smb_filename *src_fname_tmp = NULL;
4858 struct smb_filename *dst_fname_tmp = NULL;
4860 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4863 TALLOC_FREE(subreq);
4864 if (tevent_req_nterror(req, status)) {
4868 if (!state->is_copyfile) {
4869 tevent_req_done(req);
4874 * Now copy all remaining streams. We know the share supports
4875 * streams, because we're in vfs_fruit. We don't do this async
4876 * because streams are few and small.
4878 status = vfs_fstreaminfo(state->src_fsp,
4879 req, &num_streams, &streams);
4880 if (tevent_req_nterror(req, status)) {
4884 if (num_streams == 1) {
4885 /* There is always one stream, ::$DATA. */
4886 tevent_req_done(req);
4890 for (i = 0; i < num_streams; i++) {
4891 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4892 __func__, streams[i].name, (size_t)streams[i].size));
4894 src_fname_tmp = synthetic_smb_fname(
4896 state->src_fsp->fsp_name->base_name,
4899 state->src_fsp->fsp_name->twrp,
4900 state->src_fsp->fsp_name->flags);
4901 if (tevent_req_nomem(src_fname_tmp, req)) {
4905 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4906 TALLOC_FREE(src_fname_tmp);
4910 dst_fname_tmp = synthetic_smb_fname(
4912 state->dst_fsp->fsp_name->base_name,
4915 state->dst_fsp->fsp_name->twrp,
4916 state->dst_fsp->fsp_name->flags);
4917 if (tevent_req_nomem(dst_fname_tmp, req)) {
4918 TALLOC_FREE(src_fname_tmp);
4922 status = copy_file(req,
4923 state->handle->conn,
4927 if (!NT_STATUS_IS_OK(status)) {
4928 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4929 smb_fname_str_dbg(src_fname_tmp),
4930 smb_fname_str_dbg(dst_fname_tmp),
4931 nt_errstr(status)));
4932 TALLOC_FREE(src_fname_tmp);
4933 TALLOC_FREE(dst_fname_tmp);
4934 tevent_req_nterror(req, status);
4938 TALLOC_FREE(src_fname_tmp);
4939 TALLOC_FREE(dst_fname_tmp);
4942 TALLOC_FREE(streams);
4943 TALLOC_FREE(src_fname_tmp);
4944 TALLOC_FREE(dst_fname_tmp);
4945 tevent_req_done(req);
4948 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4949 struct tevent_req *req,
4952 struct fruit_offload_write_state *state = tevent_req_data(
4953 req, struct fruit_offload_write_state);
4956 if (tevent_req_is_nterror(req, &status)) {
4957 DEBUG(1, ("server side copy chunk failed: %s\n",
4958 nt_errstr(status)));
4960 tevent_req_received(req);
4964 *copied = state->copied;
4965 tevent_req_received(req);
4967 return NT_STATUS_OK;
4970 static char *fruit_get_bandsize_line(char **lines, int numlines)
4973 static bool re_initialized = false;
4977 if (!re_initialized) {
4978 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4982 re_initialized = true;
4985 for (i = 0; i < numlines; i++) {
4986 regmatch_t matches[1];
4988 ret = regexec(&re, lines[i], 1, matches, 0);
4991 * Check if the match was on the last line, sa we want
4992 * the subsequent line.
4994 if (i + 1 == numlines) {
4997 return lines[i + 1];
4999 if (ret != REG_NOMATCH) {
5007 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
5010 static bool re_initialized = false;
5011 regmatch_t matches[2];
5016 if (!re_initialized) {
5019 "<integer>\\([[:digit:]]*\\)</integer>$",
5024 re_initialized = true;
5027 ret = regexec(&re, line, 2, matches, 0);
5029 DBG_ERR("regex failed [%s]\n", line);
5033 line[matches[1].rm_eo] = '\0';
5035 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
5039 *_band_size = (size_t)band_size;
5044 * This reads and parses an Info.plist from a TM sparsebundle looking for the
5045 * "band-size" key and value.
5047 static bool fruit_get_bandsize(vfs_handle_struct *handle,
5051 #define INFO_PLIST_MAX_SIZE 64*1024
5053 struct smb_filename *smb_fname = NULL;
5054 files_struct *fsp = NULL;
5055 uint8_t *file_data = NULL;
5056 char **lines = NULL;
5057 char *band_size_line = NULL;
5058 size_t plist_file_size;
5065 plist = talloc_asprintf(talloc_tos(),
5067 handle->conn->connectpath,
5069 if (plist == NULL) {
5074 smb_fname = synthetic_smb_fname(talloc_tos(),
5080 if (smb_fname == NULL) {
5085 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5087 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
5092 plist_file_size = smb_fname->st.st_ex_size;
5094 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
5095 DBG_INFO("%s is too large, ignoring\n", plist);
5100 status = SMB_VFS_NEXT_CREATE_FILE(
5104 smb_fname, /* fname */
5105 FILE_GENERIC_READ, /* access_mask */
5106 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
5107 FILE_OPEN, /* create_disposition */
5108 0, /* create_options */
5109 0, /* file_attributes */
5110 INTERNAL_OPEN_ONLY, /* oplock_request */
5112 0, /* allocation_size */
5113 0, /* private_flags */
5118 NULL, NULL); /* create context */
5119 if (!NT_STATUS_IS_OK(status)) {
5120 DBG_INFO("Opening [%s] failed [%s]\n",
5121 smb_fname_str_dbg(smb_fname), nt_errstr(status));
5126 file_data = talloc_zero_array(talloc_tos(),
5128 plist_file_size + 1);
5129 if (file_data == NULL) {
5134 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
5135 if (nread != plist_file_size) {
5136 DBG_ERR("Short read on [%s]: %zu/%zd\n",
5137 fsp_str_dbg(fsp), nread, plist_file_size);
5143 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5144 if (!NT_STATUS_IS_OK(status)) {
5145 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5150 lines = file_lines_parse((char *)file_data,
5154 if (lines == NULL) {
5159 band_size_line = fruit_get_bandsize_line(lines, numlines);
5160 if (band_size_line == NULL) {
5161 DBG_ERR("Didn't find band-size key in [%s]\n",
5162 smb_fname_str_dbg(smb_fname));
5167 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5169 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5173 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5177 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5178 if (!NT_STATUS_IS_OK(status)) {
5179 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5183 TALLOC_FREE(smb_fname);
5184 TALLOC_FREE(file_data);
5189 struct fruit_disk_free_state {
5193 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5198 struct smb_filename *bands_dir = NULL;
5199 struct smb_Dir *dir_hnd = NULL;
5200 const char *dname = NULL;
5201 char *talloced = NULL;
5205 path = talloc_asprintf(talloc_tos(),
5207 handle->conn->connectpath,
5213 bands_dir = synthetic_smb_fname(talloc_tos(),
5220 if (bands_dir == NULL) {
5224 status = OpenDir(talloc_tos(),
5230 if (!NT_STATUS_IS_OK(status)) {
5231 TALLOC_FREE(bands_dir);
5232 errno = map_errno_from_nt_status(status);
5238 while ((dname = ReadDirName(dir_hnd, &talloced)) != NULL) {
5239 if (ISDOT(dname) || ISDOTDOT(dname)) {
5244 TALLOC_FREE(dir_hnd);
5246 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5248 TALLOC_FREE(bands_dir);
5254 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5255 struct fruit_disk_free_state *state,
5260 size_t sparsebundle_strlen = strlen("sparsebundle");
5261 size_t bandsize = 0;
5265 p = strstr(name, "sparsebundle");
5270 if (p[sparsebundle_strlen] != '\0') {
5274 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5276 ok = fruit_get_bandsize(handle, name, &bandsize);
5279 * Beware of race conditions: this may be an uninitialized
5280 * Info.plist that a client is just creating. We don't want let
5281 * this to trigger complete failure.
5283 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5287 ok = fruit_get_num_bands(handle, name, &nbands);
5290 * Beware of race conditions: this may be a backup sparsebundle
5291 * in an early stage lacking a bands subdirectory. We don't want
5292 * let this to trigger complete failure.
5294 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5299 * Arithmetic on 32-bit systems may cause overflow, depending on
5300 * size_t precision. First we check its unlikely, then we
5301 * force the precision into target off_t, then we check that
5302 * the total did not overflow either.
5304 if (bandsize > SIZE_MAX/nbands) {
5305 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5309 tm_size = (off_t)bandsize * (off_t)nbands;
5311 if (state->total_size + tm_size < state->total_size) {
5312 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5317 state->total_size += tm_size;
5319 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5320 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5326 * Calculate used size of a TimeMachine volume
5328 * This assumes that the volume is used only for TimeMachine.
5330 * - readdir(basedir of share), then
5331 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5332 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5333 * - count band files in "\1.sparsebundle/bands/"
5334 * - calculate used size of all bands: band_count * band_size
5336 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5337 const struct smb_filename *smb_fname,
5342 struct fruit_config_data *config = NULL;
5343 struct fruit_disk_free_state state = {0};
5344 struct smb_Dir *dir_hnd = NULL;
5345 const char *dname = NULL;
5346 char *talloced = NULL;
5352 SMB_VFS_HANDLE_GET_DATA(handle, config,
5353 struct fruit_config_data,
5356 if (!config->time_machine ||
5357 config->time_machine_max_size == 0)
5359 return SMB_VFS_NEXT_DISK_FREE(handle,
5366 status = OpenDir(talloc_tos(),
5372 if (!NT_STATUS_IS_OK(status)) {
5373 errno = map_errno_from_nt_status(status);
5377 while ((dname = ReadDirName(dir_hnd, &talloced)) != NULL) {
5378 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5380 TALLOC_FREE(talloced);
5381 TALLOC_FREE(dir_hnd);
5384 TALLOC_FREE(talloced);
5387 TALLOC_FREE(dir_hnd);
5389 dsize = config->time_machine_max_size / 512;
5390 dfree = dsize - (state.total_size / 512);
5391 if (dfree > dsize) {
5401 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5402 const SMB_STRUCT_STAT *psbuf)
5404 struct fruit_config_data *config = NULL;
5406 SMB_VFS_HANDLE_GET_DATA(handle, config,
5407 struct fruit_config_data,
5410 if (global_fruit_config.nego_aapl &&
5411 config->aapl_zero_file_id)
5416 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5419 static struct vfs_fn_pointers vfs_fruit_fns = {
5420 .connect_fn = fruit_connect,
5421 .disk_free_fn = fruit_disk_free,
5423 /* File operations */
5424 .fchmod_fn = fruit_fchmod,
5425 .unlinkat_fn = fruit_unlinkat,
5426 .renameat_fn = fruit_renameat,
5427 .openat_fn = fruit_openat,
5428 .close_fn = fruit_close,
5429 .pread_fn = fruit_pread,
5430 .pwrite_fn = fruit_pwrite,
5431 .pread_send_fn = fruit_pread_send,
5432 .pread_recv_fn = fruit_pread_recv,
5433 .pwrite_send_fn = fruit_pwrite_send,
5434 .pwrite_recv_fn = fruit_pwrite_recv,
5435 .fsync_send_fn = fruit_fsync_send,
5436 .fsync_recv_fn = fruit_fsync_recv,
5437 .stat_fn = fruit_stat,
5438 .lstat_fn = fruit_lstat,
5439 .fstat_fn = fruit_fstat,
5440 .fstreaminfo_fn = fruit_fstreaminfo,
5441 .fntimes_fn = fruit_fntimes,
5442 .ftruncate_fn = fruit_ftruncate,
5443 .fallocate_fn = fruit_fallocate,
5444 .create_file_fn = fruit_create_file,
5445 .freaddir_attr_fn = fruit_freaddir_attr,
5446 .offload_read_send_fn = fruit_offload_read_send,
5447 .offload_read_recv_fn = fruit_offload_read_recv,
5448 .offload_write_send_fn = fruit_offload_write_send,
5449 .offload_write_recv_fn = fruit_offload_write_recv,
5450 .fs_file_id_fn = fruit_fs_file_id,
5452 /* NT ACL operations */
5453 .fget_nt_acl_fn = fruit_fget_nt_acl,
5454 .fset_nt_acl_fn = fruit_fset_nt_acl,
5458 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5460 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5462 if (!NT_STATUS_IS_OK(ret)) {
5466 vfs_fruit_debug_level = debug_add_class("fruit");
5467 if (vfs_fruit_debug_level == -1) {
5468 vfs_fruit_debug_level = DBGC_VFS;
5469 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5472 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5473 "vfs_fruit_init","fruit",vfs_fruit_debug_level));