2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "system/shmem.h"
26 #include "locking/proto.h"
27 #include "smbd/globals.h"
29 #include "libcli/security/security.h"
30 #include "../libcli/smb/smb2_create_ctx.h"
31 #include "lib/util/tevent_ntstatus.h"
32 #include "lib/util/tevent_unix.h"
33 #include "offload_token.h"
34 #include "string_replace.h"
35 #include "hash_inode.h"
36 #include "lib/adouble.h"
37 #include "lib/util_macstreams.h"
40 * Enhanced OS X and Netatalk compatibility
41 * ========================================
43 * This modules takes advantage of vfs_streams_xattr and
44 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
45 * loaded in the correct order:
47 * vfs modules = catia fruit streams_xattr
49 * The module intercepts the OS X special streams "AFP_AfpInfo" and
50 * "AFP_Resource" and handles them in a special way. All other named
51 * streams are deferred to vfs_streams_xattr.
53 * The OS X client maps all NTFS illegal characters to the Unicode
54 * private range. This module optionally stores the characters using
55 * their native ASCII encoding using vfs_catia. If you're not enabling
56 * this feature, you can skip catia from vfs modules.
58 * Finally, open modes are optionally checked against Netatalk AFP
61 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
62 * extended metadata for files and directories. This module optionally
63 * reads and stores this metadata in a way compatible with Netatalk 3
64 * which stores the metadata in an EA "org.netatalk.metadata". Cf
65 * source3/include/MacExtensions.h for a description of the binary
68 * The "AFP_Resource" named stream may be arbitrarily large, thus it
69 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
70 * the only available filesystem where xattrs can be of any size and
71 * the OS supports using the file APIs for xattrs.
73 * The AFP_Resource stream is stored in an AppleDouble file prepending
74 * "._" to the filename. On Solaris with ZFS the stream is optionally
75 * stored in an EA "org.netatalk.resource".
81 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
82 * other protocols you may want to adjust the xattr names the VFS
83 * module vfs_streams_xattr uses for storing ADS's. This defaults to
84 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
85 * these module parameters:
87 * streams_xattr:prefix = user.
88 * streams_xattr:store_stream_type = false
94 * - log diagnostic if any needed VFS module is not loaded
95 * (eg with lp_vfs_objects())
99 static int vfs_fruit_debug_level = DBGC_VFS;
101 static struct global_fruit_config {
102 bool nego_aapl; /* client negotiated AAPL */
104 } global_fruit_config;
107 #define DBGC_CLASS vfs_fruit_debug_level
109 #define FRUIT_PARAM_TYPE_NAME "fruit"
111 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
113 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
114 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
115 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
116 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
118 struct fruit_config_data {
119 enum fruit_rsrc rsrc;
120 enum fruit_meta meta;
121 enum fruit_locking locking;
122 enum fruit_encoding encoding;
123 bool use_aapl; /* config from smb.conf */
125 bool readdir_attr_enabled;
126 bool unix_info_enabled;
127 bool copyfile_enabled;
128 bool veto_appledouble;
130 bool aapl_zero_file_id;
133 off_t time_machine_max_size;
134 bool wipe_intentionally_left_blank_rfork;
135 bool delete_empty_adfiles;
138 * Additional options, all enabled by default,
139 * possibly useful for analyzing performance. The associated
140 * operations with each of them may be expensive, so having
141 * the chance to disable them individually gives a chance
142 * tweaking the setup for the particular usecase.
144 bool readdir_attr_rsize;
145 bool readdir_attr_finder_info;
146 bool readdir_attr_max_access;
147 /* Recursion guard. Will go away when we have STATX. */
148 bool in_openat_pathref_fsp;
151 static const struct enum_list fruit_rsrc[] = {
152 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
153 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
154 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
158 static const struct enum_list fruit_meta[] = {
159 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
160 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
164 static const struct enum_list fruit_locking[] = {
165 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
166 {FRUIT_LOCKING_NONE, "none"},
170 static const struct enum_list fruit_encoding[] = {
171 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
172 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
177 vfs_handle_struct *handle;
178 files_struct *fsp; /* backlink to itself */
180 /* tcon config handle */
181 struct fruit_config_data *config;
183 /* Backend fsp for AppleDouble file, can be NULL */
184 files_struct *ad_fsp;
185 /* link from adouble_open_from_base_fsp() to fio */
186 struct fio *real_fio;
188 /* Denote stream type, meta or rsrc */
192 * AFP_AfpInfo stream created, but not written yet, thus still a fake
193 * pipe fd. This is set to true in fruit_open_meta if there was no
194 * existing stream but the caller requested O_CREAT. It is later set to
195 * false when we get a write on the stream that then does open and
203 /*****************************************************************************
205 *****************************************************************************/
207 static struct adouble *ad_get_meta_fsp(TALLOC_CTX *ctx,
208 vfs_handle_struct *handle,
209 const struct smb_filename *smb_fname)
212 struct adouble *ad = NULL;
213 struct smb_filename *smb_fname_cp = NULL;
214 struct fruit_config_data *config = NULL;
216 if (smb_fname->fsp != NULL) {
217 return ad_get(ctx, handle, smb_fname, ADOUBLE_META);
220 SMB_VFS_HANDLE_GET_DATA(handle,
222 struct fruit_config_data,
225 if (config->in_openat_pathref_fsp) {
229 smb_fname_cp = cp_smb_filename(ctx,
231 if (smb_fname_cp == NULL) {
234 TALLOC_FREE(smb_fname_cp->stream_name);
235 config->in_openat_pathref_fsp = true;
236 status = openat_pathref_fsp(handle->conn->cwd_fsp,
238 config->in_openat_pathref_fsp = false;
239 if (!NT_STATUS_IS_OK(status)) {
240 TALLOC_FREE(smb_fname_cp);
244 ad = ad_get(ctx, handle, smb_fname_cp, ADOUBLE_META);
245 TALLOC_FREE(smb_fname_cp);
249 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
252 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
258 if (fio->real_fio != NULL) {
260 * This is an fsp from adouble_open_from_base_fsp()
261 * we should just pass this to the next
271 * Initialize config struct from our smb.conf config parameters
273 static int init_fruit_config(vfs_handle_struct *handle)
275 struct fruit_config_data *config;
277 const char *tm_size_str = NULL;
279 config = talloc_zero(handle->conn, struct fruit_config_data);
281 DEBUG(1, ("talloc_zero() failed\n"));
287 * Versions up to Samba 4.5.x had a spelling bug in the
288 * fruit:resource option calling lp_parm_enum with
289 * "res*s*ource" (ie two s).
291 * In Samba 4.6 we accept both the wrong and the correct
292 * spelling, in Samba 4.7 the bad spelling will be removed.
294 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
295 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
297 DEBUG(1, ("value for %s: resource type unknown\n",
298 FRUIT_PARAM_TYPE_NAME));
301 config->rsrc = (enum fruit_rsrc)enumval;
303 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
304 "resource", fruit_rsrc, enumval);
306 DEBUG(1, ("value for %s: resource type unknown\n",
307 FRUIT_PARAM_TYPE_NAME));
310 config->rsrc = (enum fruit_rsrc)enumval;
312 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
313 "metadata", fruit_meta, FRUIT_META_NETATALK);
315 DEBUG(1, ("value for %s: metadata type unknown\n",
316 FRUIT_PARAM_TYPE_NAME));
319 config->meta = (enum fruit_meta)enumval;
321 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
322 "locking", fruit_locking, FRUIT_LOCKING_NONE);
324 DEBUG(1, ("value for %s: locking type unknown\n",
325 FRUIT_PARAM_TYPE_NAME));
328 config->locking = (enum fruit_locking)enumval;
330 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
331 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
333 DEBUG(1, ("value for %s: encoding type unknown\n",
334 FRUIT_PARAM_TYPE_NAME));
337 config->encoding = (enum fruit_encoding)enumval;
339 if (config->rsrc == FRUIT_RSRC_ADFILE) {
340 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
341 FRUIT_PARAM_TYPE_NAME,
346 config->use_aapl = lp_parm_bool(
347 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
349 config->time_machine = lp_parm_bool(
350 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
352 config->unix_info_enabled = lp_parm_bool(
353 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
355 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
358 config->posix_rename = lp_parm_bool(
359 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
361 config->aapl_zero_file_id =
362 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
363 "zero_file_id", true);
365 config->readdir_attr_rsize = lp_parm_bool(
366 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
368 config->readdir_attr_finder_info = lp_parm_bool(
369 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
371 config->readdir_attr_max_access = lp_parm_bool(
372 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
374 config->model = lp_parm_const_string(
375 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
377 tm_size_str = lp_parm_const_string(
378 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
379 "time machine max size", NULL);
380 if (tm_size_str != NULL) {
381 config->time_machine_max_size = conv_str_size(tm_size_str);
384 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
385 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
386 "wipe_intentionally_left_blank_rfork", false);
388 config->delete_empty_adfiles = lp_parm_bool(
389 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
390 "delete_empty_adfiles", false);
392 SMB_VFS_HANDLE_SET_DATA(handle, config,
393 NULL, struct fruit_config_data,
399 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
400 struct stream_struct **streams,
401 const char *name, off_t size,
404 struct stream_struct *tmp;
406 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
412 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
413 if (tmp[*num_streams].name == NULL) {
417 tmp[*num_streams].size = size;
418 tmp[*num_streams].alloc_size = alloc_size;
425 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
426 struct stream_struct **streams)
428 struct stream_struct *tmp = *streams;
431 if (*num_streams == 0) {
435 for (i = 0; i < *num_streams; i++) {
436 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
441 if (i == *num_streams) {
445 if (tmp[i].size > 0) {
449 TALLOC_FREE(tmp[i].name);
450 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
455 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
456 struct stream_struct **streams,
459 struct stream_struct *tmp = *streams;
462 if (*num_streams == 0) {
466 for (i = 0; i < *num_streams; i++) {
467 if (strequal_m(tmp[i].name, name)) {
472 if (i == *num_streams) {
476 TALLOC_FREE(tmp[i].name);
477 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
482 static bool ad_empty_finderinfo(const struct adouble *ad)
485 char emptybuf[ADEDLEN_FINDERI] = {0};
488 fi = ad_get_entry(ad, ADEID_FINDERI);
490 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
494 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
498 static bool ai_empty_finderinfo(const AfpInfo *ai)
501 char emptybuf[ADEDLEN_FINDERI] = {0};
503 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
508 * Update btime with btime from Netatalk
510 static void update_btime(vfs_handle_struct *handle,
511 struct smb_filename *smb_fname)
514 struct timespec creation_time = {0};
516 struct fruit_config_data *config = NULL;
518 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
521 switch (config->meta) {
522 case FRUIT_META_STREAM:
524 case FRUIT_META_NETATALK:
528 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
532 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
536 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
542 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
543 update_stat_ex_create_time(&smb_fname->st, creation_time);
549 * Map an access mask to a Netatalk single byte byte range lock
551 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
552 uint32_t access_mask)
556 switch (access_mask) {
558 offset = AD_FILELOCK_OPEN_RD;
561 case FILE_WRITE_DATA:
562 case FILE_APPEND_DATA:
563 offset = AD_FILELOCK_OPEN_WR;
567 offset = AD_FILELOCK_OPEN_NONE;
571 if (fork_type == APPLE_FORK_RSRC) {
572 if (offset == AD_FILELOCK_OPEN_NONE) {
573 offset = AD_FILELOCK_RSRC_OPEN_NONE;
583 * Map a deny mode to a Netatalk brl
585 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
592 offset = AD_FILELOCK_DENY_RD;
596 offset = AD_FILELOCK_DENY_WR;
600 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
603 if (fork_type == APPLE_FORK_RSRC) {
611 * Call fcntl() with an exclusive F_GETLK request in order to
612 * determine if there's an existing shared lock
614 * @return true if the requested lock was found or any error occurred
615 * false if the lock was not found
617 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
620 off_t offset = in_offset;
625 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
626 if (result == false) {
630 if (type != F_UNLCK) {
637 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
639 uint32_t access_mask,
642 NTSTATUS status = NT_STATUS_OK;
644 bool share_for_read = (share_mode & FILE_SHARE_READ);
645 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
646 bool netatalk_already_open_for_reading = false;
647 bool netatalk_already_open_for_writing = false;
648 bool netatalk_already_open_with_deny_read = false;
649 bool netatalk_already_open_with_deny_write = false;
650 struct GUID req_guid = GUID_random();
652 /* FIXME: hardcoded data fork, add resource fork */
653 enum apple_fork fork_type = APPLE_FORK_DATA;
655 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
657 access_mask & FILE_READ_DATA ? "READ" :"-",
658 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
661 if (fsp_get_io_fd(fsp) == -1) {
665 /* Read NetATalk opens and deny modes on the file. */
666 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
667 access_to_netatalk_brl(fork_type,
670 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
671 denymode_to_netatalk_brl(fork_type,
674 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
675 access_to_netatalk_brl(fork_type,
678 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
679 denymode_to_netatalk_brl(fork_type,
682 /* If there are any conflicts - sharing violation. */
683 if ((access_mask & FILE_READ_DATA) &&
684 netatalk_already_open_with_deny_read) {
685 return NT_STATUS_SHARING_VIOLATION;
688 if (!share_for_read &&
689 netatalk_already_open_for_reading) {
690 return NT_STATUS_SHARING_VIOLATION;
693 if ((access_mask & FILE_WRITE_DATA) &&
694 netatalk_already_open_with_deny_write) {
695 return NT_STATUS_SHARING_VIOLATION;
698 if (!share_for_write &&
699 netatalk_already_open_for_writing) {
700 return NT_STATUS_SHARING_VIOLATION;
703 if (!(access_mask & FILE_READ_DATA)) {
705 * Nothing we can do here, we need read access
711 /* Set NetAtalk locks matching our access */
712 if (access_mask & FILE_READ_DATA) {
713 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
714 req_guid.time_hi_and_version = __LINE__;
719 fsp->op->global->open_persistent_id,
727 if (!NT_STATUS_IS_OK(status)) {
732 if (!share_for_read) {
733 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
734 req_guid.time_hi_and_version = __LINE__;
739 fsp->op->global->open_persistent_id,
747 if (!NT_STATUS_IS_OK(status)) {
752 if (access_mask & FILE_WRITE_DATA) {
753 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
754 req_guid.time_hi_and_version = __LINE__;
759 fsp->op->global->open_persistent_id,
767 if (!NT_STATUS_IS_OK(status)) {
772 if (!share_for_write) {
773 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
774 req_guid.time_hi_and_version = __LINE__;
779 fsp->op->global->open_persistent_id,
787 if (!NT_STATUS_IS_OK(status)) {
795 static NTSTATUS check_aapl(vfs_handle_struct *handle,
796 struct smb_request *req,
797 const struct smb2_create_blobs *in_context_blobs,
798 struct smb2_create_blobs *out_context_blobs)
800 struct fruit_config_data *config;
802 struct smb2_create_blob *aapl = NULL;
806 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
807 uint64_t req_bitmap, client_caps;
808 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
812 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
813 return NT_STATUS_UNSUCCESSFUL);
815 if (!config->use_aapl
816 || in_context_blobs == NULL
817 || out_context_blobs == NULL) {
821 aapl = smb2_create_blob_find(in_context_blobs,
822 SMB2_CREATE_TAG_AAPL);
827 if (aapl->data.length != 24) {
828 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
829 (uintmax_t)aapl->data.length));
830 return NT_STATUS_INVALID_PARAMETER;
833 cmd = IVAL(aapl->data.data, 0);
834 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
835 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
836 return NT_STATUS_INVALID_PARAMETER;
839 req_bitmap = BVAL(aapl->data.data, 8);
840 client_caps = BVAL(aapl->data.data, 16);
842 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
844 SBVAL(p, 8, req_bitmap);
845 ok = data_blob_append(req, &blob, p, 16);
847 return NT_STATUS_UNSUCCESSFUL;
850 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
851 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
852 (handle->conn->fs_capabilities & FILE_NAMED_STREAMS)) {
853 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
854 config->readdir_attr_enabled = true;
857 if (config->use_copyfile) {
858 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
859 config->copyfile_enabled = true;
863 * The client doesn't set the flag, so we can't check
864 * for it and just set it unconditionally
866 if (config->unix_info_enabled) {
867 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
870 SBVAL(p, 0, server_caps);
871 ok = data_blob_append(req, &blob, p, 8);
873 return NT_STATUS_UNSUCCESSFUL;
877 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
878 int val = lp_case_sensitive(SNUM(handle->conn));
886 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
893 if (config->time_machine) {
894 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
899 ok = data_blob_append(req, &blob, p, 8);
901 return NT_STATUS_UNSUCCESSFUL;
905 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
906 ok = convert_string_talloc(req,
908 config->model, strlen(config->model),
911 return NT_STATUS_UNSUCCESSFUL;
915 SIVAL(p + 4, 0, modellen);
916 ok = data_blob_append(req, &blob, p, 8);
919 return NT_STATUS_UNSUCCESSFUL;
922 ok = data_blob_append(req, &blob, model, modellen);
925 return NT_STATUS_UNSUCCESSFUL;
929 status = smb2_create_blob_add(out_context_blobs,
931 SMB2_CREATE_TAG_AAPL,
933 if (NT_STATUS_IS_OK(status)) {
934 global_fruit_config.nego_aapl = true;
940 static bool readdir_attr_meta_finderi_stream(
941 struct vfs_handle_struct *handle,
942 const struct smb_filename *smb_fname,
945 struct smb_filename *stream_name = NULL;
946 files_struct *fsp = NULL;
950 uint8_t buf[AFP_INFO_SIZE];
952 status = synthetic_pathref(talloc_tos(),
953 handle->conn->cwd_fsp,
954 smb_fname->base_name,
960 if (!NT_STATUS_IS_OK(status)) {
964 status = SMB_VFS_CREATE_FILE(
965 handle->conn, /* conn */
968 stream_name, /* fname */
969 FILE_READ_DATA, /* access_mask */
970 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
972 FILE_OPEN, /* create_disposition*/
973 0, /* create_options */
974 0, /* file_attributes */
975 INTERNAL_OPEN_ONLY, /* oplock_request */
977 0, /* allocation_size */
978 0, /* private_flags */
983 NULL, NULL); /* create context */
985 TALLOC_FREE(stream_name);
987 if (!NT_STATUS_IS_OK(status)) {
991 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
992 if (nread != AFP_INFO_SIZE) {
993 DBG_ERR("short read [%s] [%zd/%d]\n",
994 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
999 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
1006 close_file_free(NULL, &fsp, NORMAL_CLOSE);
1012 static bool readdir_attr_meta_finderi_netatalk(
1013 struct vfs_handle_struct *handle,
1014 const struct smb_filename *smb_fname,
1017 struct adouble *ad = NULL;
1020 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
1025 p = ad_get_entry(ad, ADEID_FINDERI);
1027 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
1032 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1037 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1038 const struct smb_filename *smb_fname,
1039 struct readdir_attr_data *attr_data)
1041 struct fruit_config_data *config = NULL;
1042 uint32_t date_added;
1046 SMB_VFS_HANDLE_GET_DATA(handle, config,
1047 struct fruit_config_data,
1050 switch (config->meta) {
1051 case FRUIT_META_NETATALK:
1052 ok = readdir_attr_meta_finderi_netatalk(
1053 handle, smb_fname, &ai);
1056 case FRUIT_META_STREAM:
1057 ok = readdir_attr_meta_finderi_stream(
1058 handle, smb_fname, &ai);
1062 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1067 /* Don't bother with errors, it's likely ENOENT */
1071 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1073 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1074 &ai.afpi_FinderInfo[0], 4);
1076 /* finder_creator */
1077 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1078 &ai.afpi_FinderInfo[4], 4);
1082 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1083 &ai.afpi_FinderInfo[8], 2);
1085 /* finder_ext_flags */
1086 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1087 &ai.afpi_FinderInfo[24], 2);
1090 date_added = convert_time_t_to_uint32_t(
1091 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1093 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1098 static uint64_t readdir_attr_rfork_size_adouble(
1099 struct vfs_handle_struct *handle,
1100 const struct smb_filename *smb_fname)
1102 struct adouble *ad = NULL;
1103 uint64_t rfork_size;
1105 ad = ad_get(talloc_tos(), handle, smb_fname,
1111 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1117 static uint64_t readdir_attr_rfork_size_stream(
1118 struct vfs_handle_struct *handle,
1119 const struct smb_filename *smb_fname)
1121 struct smb_filename *stream_name = NULL;
1123 uint64_t rfork_size;
1125 stream_name = synthetic_smb_fname(talloc_tos(),
1126 smb_fname->base_name,
1127 AFPRESOURCE_STREAM_NAME,
1131 if (stream_name == NULL) {
1135 ret = SMB_VFS_STAT(handle->conn, stream_name);
1137 TALLOC_FREE(stream_name);
1141 rfork_size = stream_name->st.st_ex_size;
1142 TALLOC_FREE(stream_name);
1147 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1148 const struct smb_filename *smb_fname)
1150 struct fruit_config_data *config = NULL;
1151 uint64_t rfork_size;
1153 SMB_VFS_HANDLE_GET_DATA(handle, config,
1154 struct fruit_config_data,
1157 switch (config->rsrc) {
1158 case FRUIT_RSRC_ADFILE:
1159 rfork_size = readdir_attr_rfork_size_adouble(handle,
1163 case FRUIT_RSRC_XATTR:
1164 case FRUIT_RSRC_STREAM:
1165 rfork_size = readdir_attr_rfork_size_stream(handle,
1170 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1178 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1179 const struct smb_filename *smb_fname,
1180 struct readdir_attr_data *attr_data)
1182 NTSTATUS status = NT_STATUS_OK;
1183 struct fruit_config_data *config = NULL;
1186 SMB_VFS_HANDLE_GET_DATA(handle, config,
1187 struct fruit_config_data,
1188 return NT_STATUS_UNSUCCESSFUL);
1191 /* Ensure we return a default value in the creation_date field */
1192 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1195 * Resource fork length
1198 if (config->readdir_attr_rsize) {
1199 uint64_t rfork_size;
1201 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1202 attr_data->attr_data.aapl.rfork_size = rfork_size;
1209 if (config->readdir_attr_finder_info) {
1210 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1212 status = NT_STATUS_INTERNAL_ERROR;
1219 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1224 if (psd->dacl == NULL) {
1225 return NT_STATUS_OK;
1228 for (i = 0; i < psd->dacl->num_aces; i++) {
1229 /* MS NFS style mode/uid/gid */
1230 int cmp = dom_sid_compare_domain(
1231 &global_sid_Unix_NFS,
1232 &psd->dacl->aces[i].trustee);
1234 /* Normal ACE entry. */
1239 * security_descriptor_dacl_del()
1240 * *must* return NT_STATUS_OK as we know
1241 * we have something to remove.
1244 status = security_descriptor_dacl_del(psd,
1245 &psd->dacl->aces[i].trustee);
1246 if (!NT_STATUS_IS_OK(status)) {
1247 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1253 * security_descriptor_dacl_del() may delete more
1254 * then one entry subsequent to this one if the
1255 * SID matches, but we only need to ensure that
1256 * we stay looking at the same element in the array.
1260 return NT_STATUS_OK;
1263 /* Search MS NFS style ACE with UNIX mode */
1264 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1266 struct security_descriptor *psd,
1271 struct fruit_config_data *config = NULL;
1275 SMB_VFS_HANDLE_GET_DATA(handle, config,
1276 struct fruit_config_data,
1277 return NT_STATUS_UNSUCCESSFUL);
1279 if (!global_fruit_config.nego_aapl) {
1280 return NT_STATUS_OK;
1282 if (psd->dacl == NULL || !config->unix_info_enabled) {
1283 return NT_STATUS_OK;
1286 for (i = 0; i < psd->dacl->num_aces; i++) {
1287 if (dom_sid_compare_domain(
1288 &global_sid_Unix_NFS_Mode,
1289 &psd->dacl->aces[i].trustee) == 0) {
1290 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1291 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1294 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1295 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1301 * Remove any incoming virtual ACE entries generated by
1302 * fruit_fget_nt_acl().
1305 return remove_virtual_nfs_aces(psd);
1308 /****************************************************************************
1310 ****************************************************************************/
1312 static int fruit_connect(vfs_handle_struct *handle,
1313 const char *service,
1317 char *list = NULL, *newlist = NULL;
1318 struct fruit_config_data *config;
1319 const struct loadparm_substitution *lp_sub =
1320 loadparm_s3_global_substitution();
1322 DEBUG(10, ("fruit_connect\n"));
1324 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1329 rc = init_fruit_config(handle);
1334 SMB_VFS_HANDLE_GET_DATA(handle, config,
1335 struct fruit_config_data, return -1);
1337 if (config->veto_appledouble) {
1338 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1341 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1342 newlist = talloc_asprintf(
1344 "%s/" ADOUBLE_NAME_PREFIX "*/",
1346 lp_do_parameter(SNUM(handle->conn),
1351 lp_do_parameter(SNUM(handle->conn),
1353 "/" ADOUBLE_NAME_PREFIX "*/");
1359 if (config->encoding == FRUIT_ENC_NATIVE) {
1360 lp_do_parameter(SNUM(handle->conn),
1362 macos_string_replace_map);
1365 if (config->time_machine) {
1366 DBG_NOTICE("Enabling durable handles for Time Machine "
1367 "support on [%s]\n", service);
1368 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1369 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1370 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1371 if (!lp_strict_sync(SNUM(handle->conn))) {
1372 DBG_WARNING("Time Machine without strict sync is not "
1375 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1381 static void fio_ref_destroy_fn(void *p_data)
1383 struct fio *ref_fio = (struct fio *)p_data;
1384 if (ref_fio->real_fio != NULL) {
1385 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1386 ref_fio->real_fio->ad_fsp = NULL;
1387 ref_fio->real_fio = NULL;
1391 static void fio_close_ad_fsp(struct fio *fio)
1393 if (fio->ad_fsp != NULL) {
1394 fd_close(fio->ad_fsp);
1395 file_free(NULL, fio->ad_fsp);
1396 /* fio_ref_destroy_fn() should have cleared this */
1397 SMB_ASSERT(fio->ad_fsp == NULL);
1401 static void fio_destroy_fn(void *p_data)
1403 struct fio *fio = (struct fio *)p_data;
1404 fio_close_ad_fsp(fio);
1407 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1408 const struct files_struct *dirfsp,
1409 const struct smb_filename *smb_fname,
1414 struct fruit_config_data *config = NULL;
1415 struct fio *fio = NULL;
1416 struct vfs_open_how how = {
1417 .flags = flags & ~O_CREAT,
1422 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1424 SMB_VFS_HANDLE_GET_DATA(handle, config,
1425 struct fruit_config_data, return -1);
1427 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1428 fio->handle = handle;
1430 fio->type = ADOUBLE_META;
1431 fio->config = config;
1433 fd = SMB_VFS_NEXT_OPENAT(handle,
1442 if (!(flags & O_CREAT)) {
1443 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1449 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1453 fio->fake_fd = true;
1460 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1461 const struct files_struct *dirfsp,
1462 const struct smb_filename *smb_fname,
1467 struct fruit_config_data *config = NULL;
1468 struct fio *fio = NULL;
1469 struct adouble *ad = NULL;
1470 bool meta_exists = false;
1473 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1476 * We know this is a stream open, so fsp->base_fsp must
1479 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1480 SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1482 ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1489 if (!meta_exists && !(flags & O_CREAT)) {
1499 SMB_VFS_HANDLE_GET_DATA(handle, config,
1500 struct fruit_config_data, return -1);
1502 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1503 fio->handle = handle;
1505 fio->type = ADOUBLE_META;
1506 fio->config = config;
1507 fio->fake_fd = true;
1514 static int fruit_open_meta(vfs_handle_struct *handle,
1515 const struct files_struct *dirfsp,
1516 const struct smb_filename *smb_fname,
1517 files_struct *fsp, int flags, mode_t mode)
1520 struct fruit_config_data *config = NULL;
1522 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1524 SMB_VFS_HANDLE_GET_DATA(handle, config,
1525 struct fruit_config_data, return -1);
1527 switch (config->meta) {
1528 case FRUIT_META_STREAM:
1529 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1533 case FRUIT_META_NETATALK:
1534 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1539 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1543 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1548 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1549 const struct files_struct *dirfsp,
1550 const struct smb_filename *smb_fname,
1556 struct fruit_config_data *config = NULL;
1557 struct files_struct *ad_fsp = NULL;
1558 struct fio *fio = NULL;
1559 struct fio *ref_fio = NULL;
1563 SMB_VFS_HANDLE_GET_DATA(handle, config,
1564 struct fruit_config_data, return -1);
1566 if ((!(flags & O_CREAT)) &&
1567 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1569 /* sorry, but directories don't have a resource fork */
1576 * We return a fake_fd to the vfs modules above,
1577 * while we open an internal backend fsp for the
1578 * '._' file for the next vfs modules.
1580 * Note that adouble_open_from_base_fsp() recurses
1581 * into fruit_openat(), but it'll just pass to
1582 * the next module as just opens a flat file on
1592 status = adouble_open_from_base_fsp(fsp->conn->cwd_fsp,
1598 if (!NT_STATUS_IS_OK(status)) {
1599 errno = map_errno_from_nt_status(status);
1605 * Now we need to glue both handles together,
1606 * so that they automatically detach each other
1609 fio = fruit_get_complete_fio(handle, fsp);
1611 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1617 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1619 fio_ref_destroy_fn);
1620 if (ref_fio == NULL) {
1621 int saved_errno = errno;
1623 file_free(NULL, ad_fsp);
1625 errno = saved_errno;
1630 SMB_ASSERT(ref_fio->fsp == NULL);
1631 ref_fio->handle = handle;
1632 ref_fio->fsp = ad_fsp;
1633 ref_fio->type = ADOUBLE_RSRC;
1634 ref_fio->config = config;
1635 ref_fio->real_fio = fio;
1636 SMB_ASSERT(fio->ad_fsp == NULL);
1637 fio->ad_fsp = ad_fsp;
1638 fio->fake_fd = true;
1642 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1644 int saved_errno = errno;
1646 vfs_fake_fd_close(fd);
1648 errno = saved_errno;
1654 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1655 const struct files_struct *dirfsp,
1656 const struct smb_filename *smb_fname,
1661 #ifdef HAVE_ATTROPEN
1665 * As there's no attropenat() this is only going to work with AT_FDCWD.
1667 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1669 fd = attropen(smb_fname->base_name,
1670 AFPRESOURCE_EA_NETATALK,
1685 static int fruit_open_rsrc(vfs_handle_struct *handle,
1686 const struct files_struct *dirfsp,
1687 const struct smb_filename *smb_fname,
1688 files_struct *fsp, int flags, mode_t mode)
1691 struct fruit_config_data *config = NULL;
1692 struct fio *fio = NULL;
1694 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1696 SMB_VFS_HANDLE_GET_DATA(handle, config,
1697 struct fruit_config_data, return -1);
1699 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1700 fio->handle = handle;
1702 fio->type = ADOUBLE_RSRC;
1703 fio->config = config;
1705 switch (config->rsrc) {
1706 case FRUIT_RSRC_STREAM: {
1707 struct vfs_open_how how = {
1708 .flags = flags, .mode = mode,
1710 fd = SMB_VFS_NEXT_OPENAT(handle,
1718 case FRUIT_RSRC_ADFILE:
1719 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1723 case FRUIT_RSRC_XATTR:
1724 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1729 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1734 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1743 static int fruit_openat(vfs_handle_struct *handle,
1744 const struct files_struct *dirfsp,
1745 const struct smb_filename *smb_fname,
1747 const struct vfs_open_how *how)
1751 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1753 if (!is_named_stream(smb_fname)) {
1754 return SMB_VFS_NEXT_OPENAT(handle,
1761 if (how->resolve != 0) {
1766 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1768 if (is_afpinfo_stream(smb_fname->stream_name)) {
1769 fd = fruit_open_meta(handle,
1775 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1776 fd = fruit_open_rsrc(handle,
1783 fd = SMB_VFS_NEXT_OPENAT(handle,
1790 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1792 /* Prevent reopen optimisation */
1793 fsp->fsp_flags.have_proc_fds = false;
1797 static int fruit_close_meta(vfs_handle_struct *handle,
1801 struct fruit_config_data *config = NULL;
1803 SMB_VFS_HANDLE_GET_DATA(handle, config,
1804 struct fruit_config_data, return -1);
1806 switch (config->meta) {
1807 case FRUIT_META_STREAM:
1809 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1814 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1815 fsp_set_fd(fsp, -1);
1817 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1821 case FRUIT_META_NETATALK:
1822 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1823 fsp_set_fd(fsp, -1);
1827 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1835 static int fruit_close_rsrc(vfs_handle_struct *handle,
1839 struct fruit_config_data *config = NULL;
1841 SMB_VFS_HANDLE_GET_DATA(handle, config,
1842 struct fruit_config_data, return -1);
1844 switch (config->rsrc) {
1845 case FRUIT_RSRC_STREAM:
1846 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1849 case FRUIT_RSRC_ADFILE:
1851 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1855 fio_close_ad_fsp(fio);
1856 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1857 fsp_set_fd(fsp, -1);
1861 case FRUIT_RSRC_XATTR:
1862 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1863 fsp_set_fd(fsp, -1);
1867 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1874 static int fruit_close(vfs_handle_struct *handle,
1880 fd = fsp_get_pathref_fd(fsp);
1882 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1884 if (!fsp_is_alternate_stream(fsp)) {
1885 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1888 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1889 ret = fruit_close_meta(handle, fsp);
1890 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1891 ret = fruit_close_rsrc(handle, fsp);
1893 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1899 static int fruit_renameat(struct vfs_handle_struct *handle,
1900 files_struct *srcfsp,
1901 const struct smb_filename *smb_fname_src,
1902 files_struct *dstfsp,
1903 const struct smb_filename *smb_fname_dst)
1906 struct fruit_config_data *config = NULL;
1907 struct smb_filename *src_adp_smb_fname = NULL;
1908 struct smb_filename *dst_adp_smb_fname = NULL;
1910 SMB_VFS_HANDLE_GET_DATA(handle, config,
1911 struct fruit_config_data, return -1);
1913 if (!VALID_STAT(smb_fname_src->st)) {
1914 DBG_ERR("Need valid stat for [%s]\n",
1915 smb_fname_str_dbg(smb_fname_src));
1919 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1928 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1929 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1934 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1939 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1944 DBG_DEBUG("%s -> %s\n",
1945 smb_fname_str_dbg(src_adp_smb_fname),
1946 smb_fname_str_dbg(dst_adp_smb_fname));
1948 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1953 if (errno == ENOENT) {
1958 TALLOC_FREE(src_adp_smb_fname);
1959 TALLOC_FREE(dst_adp_smb_fname);
1963 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1964 struct files_struct *dirfsp,
1965 const struct smb_filename *smb_fname)
1967 return SMB_VFS_NEXT_UNLINKAT(handle,
1973 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1974 const struct smb_filename *smb_fname)
1976 SMB_ASSERT(smb_fname->fsp != NULL);
1977 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1978 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1979 AFPINFO_EA_NETATALK);
1982 static int fruit_unlink_meta(vfs_handle_struct *handle,
1983 struct files_struct *dirfsp,
1984 const struct smb_filename *smb_fname)
1986 struct fruit_config_data *config = NULL;
1989 SMB_VFS_HANDLE_GET_DATA(handle, config,
1990 struct fruit_config_data, return -1);
1992 switch (config->meta) {
1993 case FRUIT_META_STREAM:
1994 rc = fruit_unlink_meta_stream(handle,
1999 case FRUIT_META_NETATALK:
2000 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
2004 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2011 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2012 struct files_struct *dirfsp,
2013 const struct smb_filename *smb_fname,
2018 if (!force_unlink) {
2019 struct smb_filename *full_fname = NULL;
2023 * TODO: use SMB_VFS_STATX() once we have it.
2026 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2029 if (full_fname == NULL) {
2034 * 0 byte resource fork streams are not listed by
2035 * vfs_streaminfo, as a result stream cleanup/deletion of file
2036 * deletion doesn't remove the resourcefork stream.
2039 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2041 TALLOC_FREE(full_fname);
2042 DBG_ERR("stat [%s] failed [%s]\n",
2043 smb_fname_str_dbg(full_fname), strerror(errno));
2047 size = full_fname->st.st_ex_size;
2048 TALLOC_FREE(full_fname);
2051 /* OS X ignores resource fork stream delete requests */
2056 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2060 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2067 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2068 struct files_struct *dirfsp,
2069 const struct smb_filename *smb_fname,
2073 struct adouble *ad = NULL;
2074 struct smb_filename *adp_smb_fname = NULL;
2076 if (!force_unlink) {
2077 struct smb_filename *full_fname = NULL;
2079 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2082 if (full_fname == NULL) {
2086 ad = ad_get(talloc_tos(), handle, full_fname,
2088 TALLOC_FREE(full_fname);
2096 * 0 byte resource fork streams are not listed by
2097 * vfs_streaminfo, as a result stream cleanup/deletion of file
2098 * deletion doesn't remove the resourcefork stream.
2101 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2102 /* OS X ignores resource fork stream delete requests */
2110 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2115 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2119 TALLOC_FREE(adp_smb_fname);
2120 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2127 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2128 const struct smb_filename *smb_fname,
2132 * OS X ignores resource fork stream delete requests, so nothing to do
2133 * here. Removing the file will remove the xattr anyway, so we don't
2134 * have to take care of removing 0 byte resource forks that could be
2140 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2141 struct files_struct *dirfsp,
2142 const struct smb_filename *smb_fname,
2145 struct fruit_config_data *config = NULL;
2148 SMB_VFS_HANDLE_GET_DATA(handle, config,
2149 struct fruit_config_data, return -1);
2151 switch (config->rsrc) {
2152 case FRUIT_RSRC_STREAM:
2153 rc = fruit_unlink_rsrc_stream(handle,
2159 case FRUIT_RSRC_ADFILE:
2160 rc = fruit_unlink_rsrc_adouble(handle,
2166 case FRUIT_RSRC_XATTR:
2167 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2171 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2178 static int fruit_fchmod(vfs_handle_struct *handle,
2179 struct files_struct *fsp,
2183 struct fruit_config_data *config = NULL;
2184 struct smb_filename *smb_fname_adp = NULL;
2185 const struct smb_filename *smb_fname = NULL;
2188 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2193 smb_fname = fsp->fsp_name;
2194 SMB_VFS_HANDLE_GET_DATA(handle, config,
2195 struct fruit_config_data, return -1);
2197 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2201 if (!VALID_STAT(smb_fname->st)) {
2205 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2209 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2214 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2216 if (!NT_STATUS_IS_OK(status)) {
2217 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2218 if (NT_STATUS_EQUAL(status,
2219 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2227 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2229 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2230 if (errno == ENOENT) {
2234 TALLOC_FREE(smb_fname_adp);
2238 static int fruit_unlinkat(vfs_handle_struct *handle,
2239 struct files_struct *dirfsp,
2240 const struct smb_filename *smb_fname,
2243 struct fruit_config_data *config = NULL;
2244 struct smb_filename *rsrc_smb_fname = NULL;
2247 if (flags & AT_REMOVEDIR) {
2248 return SMB_VFS_NEXT_UNLINKAT(handle,
2254 SMB_VFS_HANDLE_GET_DATA(handle, config,
2255 struct fruit_config_data, return -1);
2257 if (is_afpinfo_stream(smb_fname->stream_name)) {
2258 return fruit_unlink_meta(handle,
2261 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2262 return fruit_unlink_rsrc(handle,
2266 } else if (is_named_stream(smb_fname)) {
2267 return SMB_VFS_NEXT_UNLINKAT(handle,
2271 } else if (is_adouble_file(smb_fname->base_name)) {
2272 return SMB_VFS_NEXT_UNLINKAT(handle,
2279 * A request to delete the base file. Because 0 byte resource
2280 * fork streams are not listed by fruit_streaminfo,
2281 * delete_all_streams() can't remove 0 byte resource fork
2282 * streams, so we have to cleanup this here.
2284 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2285 smb_fname->base_name,
2286 AFPRESOURCE_STREAM_NAME,
2290 if (rsrc_smb_fname == NULL) {
2294 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2295 if ((ret != 0) && (errno != ENOENT)) {
2296 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2297 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2298 TALLOC_FREE(rsrc_smb_fname);
2301 TALLOC_FREE(rsrc_smb_fname);
2303 return SMB_VFS_NEXT_UNLINKAT(handle,
2309 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2310 files_struct *fsp, void *data,
2311 size_t n, off_t offset)
2313 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2317 if ((fio == NULL) || fio->fake_fd) {
2321 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2322 if (nread == -1 || nread == n) {
2326 DBG_ERR("Removing [%s] after short read [%zd]\n",
2327 fsp_str_dbg(fsp), nread);
2329 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2334 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2342 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2343 files_struct *fsp, void *data,
2344 size_t n, off_t offset)
2347 struct adouble *ad = NULL;
2348 char afpinfo_buf[AFP_INFO_SIZE];
2352 ai = afpinfo_new(talloc_tos());
2357 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2363 p = ad_get_entry(ad, ADEID_FINDERI);
2365 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2370 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2372 nread = afpinfo_pack(ai, afpinfo_buf);
2373 if (nread != AFP_INFO_SIZE) {
2378 memcpy(data, afpinfo_buf, n);
2386 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2387 files_struct *fsp, void *data,
2388 size_t n, off_t offset)
2390 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2395 * OS X has a off-by-1 error in the offset calculation, so we're
2396 * bug compatible here. It won't hurt, as any relevant real
2397 * world read requests from the AFP_AfpInfo stream will be
2398 * offset=0 n=60. offset is ignored anyway, see below.
2400 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2405 DBG_ERR("Failed to fetch fsp extension");
2409 /* Yes, macOS always reads from offset 0 */
2411 to_return = MIN(n, AFP_INFO_SIZE);
2413 switch (fio->config->meta) {
2414 case FRUIT_META_STREAM:
2415 nread = fruit_pread_meta_stream(handle, fsp, data,
2419 case FRUIT_META_NETATALK:
2420 nread = fruit_pread_meta_adouble(handle, fsp, data,
2425 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2429 if (nread == -1 && fio->fake_fd) {
2431 char afpinfo_buf[AFP_INFO_SIZE];
2433 ai = afpinfo_new(talloc_tos());
2438 nread = afpinfo_pack(ai, afpinfo_buf);
2440 if (nread != AFP_INFO_SIZE) {
2444 memcpy(data, afpinfo_buf, to_return);
2451 static ssize_t fruit_pread_rsrc_stream(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_xattr(vfs_handle_struct *handle,
2459 files_struct *fsp, void *data,
2460 size_t n, off_t offset)
2462 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2465 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2466 files_struct *fsp, void *data,
2467 size_t n, off_t offset)
2469 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2470 struct adouble *ad = NULL;
2473 if (fio == NULL || fio->ad_fsp == NULL) {
2474 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2479 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2481 DBG_ERR("ad_fget [%s] failed [%s]\n",
2482 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2486 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2487 offset + ad_getentryoff(ad, ADEID_RFORK));
2493 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2494 files_struct *fsp, void *data,
2495 size_t n, off_t offset)
2497 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2505 switch (fio->config->rsrc) {
2506 case FRUIT_RSRC_STREAM:
2507 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2510 case FRUIT_RSRC_ADFILE:
2511 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2514 case FRUIT_RSRC_XATTR:
2515 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2519 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2526 static ssize_t fruit_pread(vfs_handle_struct *handle,
2527 files_struct *fsp, void *data,
2528 size_t n, off_t offset)
2530 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2533 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2534 fsp_str_dbg(fsp), (intmax_t)offset, n);
2537 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2540 if (fio->type == ADOUBLE_META) {
2541 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2543 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2546 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2550 static bool fruit_must_handle_aio_stream(struct fio *fio)
2556 if (fio->type == ADOUBLE_META) {
2560 if ((fio->type == ADOUBLE_RSRC) &&
2561 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2569 struct fruit_pread_state {
2571 struct vfs_aio_state vfs_aio_state;
2574 static void fruit_pread_done(struct tevent_req *subreq);
2576 static struct tevent_req *fruit_pread_send(
2577 struct vfs_handle_struct *handle,
2578 TALLOC_CTX *mem_ctx,
2579 struct tevent_context *ev,
2580 struct files_struct *fsp,
2582 size_t n, off_t offset)
2584 struct tevent_req *req = NULL;
2585 struct tevent_req *subreq = NULL;
2586 struct fruit_pread_state *state = NULL;
2587 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2589 req = tevent_req_create(mem_ctx, &state,
2590 struct fruit_pread_state);
2595 if (fruit_must_handle_aio_stream(fio)) {
2596 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2597 if (state->nread != n) {
2598 if (state->nread != -1) {
2601 tevent_req_error(req, errno);
2602 return tevent_req_post(req, ev);
2604 tevent_req_done(req);
2605 return tevent_req_post(req, ev);
2608 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2610 if (tevent_req_nomem(req, subreq)) {
2611 return tevent_req_post(req, ev);
2613 tevent_req_set_callback(subreq, fruit_pread_done, req);
2617 static void fruit_pread_done(struct tevent_req *subreq)
2619 struct tevent_req *req = tevent_req_callback_data(
2620 subreq, struct tevent_req);
2621 struct fruit_pread_state *state = tevent_req_data(
2622 req, struct fruit_pread_state);
2624 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2625 TALLOC_FREE(subreq);
2627 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2630 tevent_req_done(req);
2633 static ssize_t fruit_pread_recv(struct tevent_req *req,
2634 struct vfs_aio_state *vfs_aio_state)
2636 struct fruit_pread_state *state = tevent_req_data(
2637 req, struct fruit_pread_state);
2638 ssize_t retval = -1;
2640 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2641 tevent_req_received(req);
2645 *vfs_aio_state = state->vfs_aio_state;
2646 retval = state->nread;
2647 tevent_req_received(req);
2651 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2652 files_struct *fsp, const void *data,
2653 size_t n, off_t offset)
2655 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2661 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2662 fsp_str_dbg(fsp), (intmax_t)offset, n);
2669 struct vfs_open_how how = {
2670 .flags = fio->flags, .mode = fio->mode,
2672 int fd = fsp_get_pathref_fd(fsp);
2674 ret = vfs_fake_fd_close(fd);
2675 fsp_set_fd(fsp, -1);
2677 DBG_ERR("Close [%s] failed: %s\n",
2678 fsp_str_dbg(fsp), strerror(errno));
2682 fd = SMB_VFS_NEXT_OPENAT(handle,
2683 NULL, /* opening a stream */
2688 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2689 fsp_str_dbg(fsp), strerror(errno));
2692 fsp_set_fd(fsp, fd);
2693 fio->fake_fd = false;
2696 ai = afpinfo_unpack(talloc_tos(), data);
2701 if (ai_empty_finderinfo(ai)) {
2703 * Writing an all 0 blob to the metadata stream results in the
2704 * stream being removed on a macOS server. This ensures we
2705 * behave the same and it verified by the "delete AFP_AfpInfo by
2706 * writing all 0" test.
2708 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2710 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2715 ok = set_delete_on_close(
2718 handle->conn->session_info->security_token,
2719 handle->conn->session_info->unix_token);
2721 DBG_ERR("set_delete_on_close on [%s] failed\n",
2728 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2729 if (nwritten != n) {
2736 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2737 files_struct *fsp, const void *data,
2738 size_t n, off_t offset)
2740 struct adouble *ad = NULL;
2746 ai = afpinfo_unpack(talloc_tos(), data);
2751 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2753 ad = ad_init(talloc_tos(), ADOUBLE_META);
2758 p = ad_get_entry(ad, ADEID_FINDERI);
2760 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2765 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2767 ret = ad_fset(handle, ad, fsp);
2769 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2776 if (!ai_empty_finderinfo(ai)) {
2781 * Writing an all 0 blob to the metadata stream results in the stream
2782 * being removed on a macOS server. This ensures we behave the same and
2783 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2786 ok = set_delete_on_close(
2789 handle->conn->session_info->security_token,
2790 handle->conn->session_info->unix_token);
2792 DBG_ERR("set_delete_on_close on [%s] failed\n",
2800 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2801 files_struct *fsp, const void *data,
2802 size_t n, off_t offset)
2804 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2806 uint8_t buf[AFP_INFO_SIZE];
2812 DBG_ERR("Failed to fetch fsp extension");
2821 if (offset != 0 && n < 60) {
2826 cmp = memcmp(data, "AFP", 3);
2832 if (n <= AFP_OFF_FinderInfo) {
2834 * Nothing to do here really, just return
2842 if (to_copy > AFP_INFO_SIZE) {
2843 to_copy = AFP_INFO_SIZE;
2845 memcpy(buf, data, to_copy);
2848 if (to_write != AFP_INFO_SIZE) {
2849 to_write = AFP_INFO_SIZE;
2852 switch (fio->config->meta) {
2853 case FRUIT_META_STREAM:
2854 nwritten = fruit_pwrite_meta_stream(handle,
2861 case FRUIT_META_NETATALK:
2862 nwritten = fruit_pwrite_meta_netatalk(handle,
2870 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2874 if (nwritten != to_write) {
2879 * Return the requested amount, verified against macOS SMB server
2884 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2885 files_struct *fsp, const void *data,
2886 size_t n, off_t offset)
2888 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2891 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2892 files_struct *fsp, const void *data,
2893 size_t n, off_t offset)
2895 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2898 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2899 files_struct *fsp, const void *data,
2900 size_t n, off_t offset)
2902 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2903 struct adouble *ad = NULL;
2907 if (fio == NULL || fio->ad_fsp == NULL) {
2908 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2913 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2915 DBG_ERR("ad_fget [%s] failed [%s]\n",
2916 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2920 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2921 offset + ad_getentryoff(ad, ADEID_RFORK));
2922 if (nwritten != n) {
2923 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2924 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2929 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2930 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2931 ret = ad_fset(handle, ad, fio->ad_fsp);
2933 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2943 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2944 files_struct *fsp, const void *data,
2945 size_t n, off_t offset)
2947 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2951 DBG_ERR("Failed to fetch fsp extension");
2955 switch (fio->config->rsrc) {
2956 case FRUIT_RSRC_STREAM:
2957 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2960 case FRUIT_RSRC_ADFILE:
2961 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2964 case FRUIT_RSRC_XATTR:
2965 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2969 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2976 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2977 files_struct *fsp, const void *data,
2978 size_t n, off_t offset)
2980 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2983 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2984 fsp_str_dbg(fsp), (intmax_t)offset, n);
2987 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2990 if (fio->type == ADOUBLE_META) {
2991 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2993 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2996 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
3000 struct fruit_pwrite_state {
3002 struct vfs_aio_state vfs_aio_state;
3005 static void fruit_pwrite_done(struct tevent_req *subreq);
3007 static struct tevent_req *fruit_pwrite_send(
3008 struct vfs_handle_struct *handle,
3009 TALLOC_CTX *mem_ctx,
3010 struct tevent_context *ev,
3011 struct files_struct *fsp,
3013 size_t n, off_t offset)
3015 struct tevent_req *req = NULL;
3016 struct tevent_req *subreq = NULL;
3017 struct fruit_pwrite_state *state = NULL;
3018 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3020 req = tevent_req_create(mem_ctx, &state,
3021 struct fruit_pwrite_state);
3026 if (fruit_must_handle_aio_stream(fio)) {
3027 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3028 if (state->nwritten != n) {
3029 if (state->nwritten != -1) {
3032 tevent_req_error(req, errno);
3033 return tevent_req_post(req, ev);
3035 tevent_req_done(req);
3036 return tevent_req_post(req, ev);
3039 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3041 if (tevent_req_nomem(req, subreq)) {
3042 return tevent_req_post(req, ev);
3044 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3048 static void fruit_pwrite_done(struct tevent_req *subreq)
3050 struct tevent_req *req = tevent_req_callback_data(
3051 subreq, struct tevent_req);
3052 struct fruit_pwrite_state *state = tevent_req_data(
3053 req, struct fruit_pwrite_state);
3055 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3056 TALLOC_FREE(subreq);
3058 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3061 tevent_req_done(req);
3064 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3065 struct vfs_aio_state *vfs_aio_state)
3067 struct fruit_pwrite_state *state = tevent_req_data(
3068 req, struct fruit_pwrite_state);
3069 ssize_t retval = -1;
3071 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3072 tevent_req_received(req);
3076 *vfs_aio_state = state->vfs_aio_state;
3077 retval = state->nwritten;
3078 tevent_req_received(req);
3082 struct fruit_fsync_state {
3084 struct vfs_aio_state vfs_aio_state;
3087 static void fruit_fsync_done(struct tevent_req *subreq);
3089 static struct tevent_req *fruit_fsync_send(
3090 struct vfs_handle_struct *handle,
3091 TALLOC_CTX *mem_ctx,
3092 struct tevent_context *ev,
3093 struct files_struct *fsp)
3095 struct tevent_req *req = NULL;
3096 struct tevent_req *subreq = NULL;
3097 struct fruit_fsync_state *state = NULL;
3098 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3100 req = tevent_req_create(mem_ctx, &state,
3101 struct fruit_fsync_state);
3106 if (fruit_must_handle_aio_stream(fio)) {
3107 struct adouble *ad = NULL;
3109 if (fio->type == ADOUBLE_META) {
3111 * We must never pass a fake_fd
3112 * to lower level fsync calls.
3113 * Everything is already done
3114 * synchronously, so just return
3117 SMB_ASSERT(fio->fake_fd);
3118 tevent_req_done(req);
3119 return tevent_req_post(req, ev);
3123 * We know the following must be true,
3124 * as it's the condition for fruit_must_handle_aio_stream()
3125 * to return true if fio->type == ADOUBLE_RSRC.
3127 SMB_ASSERT(fio->config->rsrc == FRUIT_RSRC_ADFILE);
3128 if (fio->ad_fsp == NULL) {
3129 tevent_req_error(req, EBADF);
3130 return tevent_req_post(req, ev);
3132 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3134 tevent_req_error(req, ENOMEM);
3135 return tevent_req_post(req, ev);
3140 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
3141 if (tevent_req_nomem(req, subreq)) {
3142 return tevent_req_post(req, ev);
3144 tevent_req_set_callback(subreq, fruit_fsync_done, req);
3148 static void fruit_fsync_done(struct tevent_req *subreq)
3150 struct tevent_req *req = tevent_req_callback_data(
3151 subreq, struct tevent_req);
3152 struct fruit_fsync_state *state = tevent_req_data(
3153 req, struct fruit_fsync_state);
3155 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
3156 TALLOC_FREE(subreq);
3157 if (state->ret != 0) {
3158 tevent_req_error(req, errno);
3161 tevent_req_done(req);
3164 static int fruit_fsync_recv(struct tevent_req *req,
3165 struct vfs_aio_state *vfs_aio_state)
3167 struct fruit_fsync_state *state = tevent_req_data(
3168 req, struct fruit_fsync_state);
3171 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3172 tevent_req_received(req);
3176 *vfs_aio_state = state->vfs_aio_state;
3177 retval = state->ret;
3178 tevent_req_received(req);
3183 * Helper to stat/lstat the base file of an smb_fname.
3185 static int fruit_stat_base(vfs_handle_struct *handle,
3186 struct smb_filename *smb_fname,
3189 char *tmp_stream_name;
3192 tmp_stream_name = smb_fname->stream_name;
3193 smb_fname->stream_name = NULL;
3195 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3197 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3199 smb_fname->stream_name = tmp_stream_name;
3201 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3202 smb_fname->base_name,
3203 (uintmax_t)smb_fname->st.st_ex_dev,
3204 (uintmax_t)smb_fname->st.st_ex_ino);
3208 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3209 struct smb_filename *smb_fname,
3215 ret = fruit_stat_base(handle, smb_fname, false);
3220 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3223 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3225 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3228 smb_fname->st.st_ex_ino = ino;
3233 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3234 struct smb_filename *smb_fname,
3237 struct adouble *ad = NULL;
3239 /* Populate the stat struct with info from the base file. */
3240 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3244 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3246 DBG_INFO("fruit_stat_meta %s: %s\n",
3247 smb_fname_str_dbg(smb_fname), strerror(errno));
3253 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3254 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3255 smb_fname->stream_name);
3259 static int fruit_stat_meta(vfs_handle_struct *handle,
3260 struct smb_filename *smb_fname,
3263 struct fruit_config_data *config = NULL;
3266 SMB_VFS_HANDLE_GET_DATA(handle, config,
3267 struct fruit_config_data, return -1);
3269 switch (config->meta) {
3270 case FRUIT_META_STREAM:
3271 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3274 case FRUIT_META_NETATALK:
3275 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3279 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3286 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3287 struct smb_filename *smb_fname,
3290 struct adouble *ad = NULL;
3293 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3299 /* Populate the stat struct with info from the base file. */
3300 ret = fruit_stat_base(handle, smb_fname, follow_links);
3306 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3307 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3308 smb_fname->stream_name);
3313 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3314 struct smb_filename *smb_fname,
3320 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3322 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3328 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3329 struct smb_filename *smb_fname,
3332 #ifdef HAVE_ATTROPEN
3336 /* Populate the stat struct with info from the base file. */
3337 ret = fruit_stat_base(handle, smb_fname, follow_links);
3342 fd = attropen(smb_fname->base_name,
3343 AFPRESOURCE_EA_NETATALK,
3349 ret = sys_fstat(fd, &smb_fname->st, false);
3352 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3353 AFPRESOURCE_EA_NETATALK);
3359 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3360 smb_fname->stream_name);
3370 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3371 struct smb_filename *smb_fname,
3374 struct fruit_config_data *config = NULL;
3377 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3379 SMB_VFS_HANDLE_GET_DATA(handle, config,
3380 struct fruit_config_data, return -1);
3382 switch (config->rsrc) {
3383 case FRUIT_RSRC_STREAM:
3384 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3387 case FRUIT_RSRC_XATTR:
3388 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3391 case FRUIT_RSRC_ADFILE:
3392 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3396 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3403 static int fruit_stat(vfs_handle_struct *handle,
3404 struct smb_filename *smb_fname)
3408 DEBUG(10, ("fruit_stat called for %s\n",
3409 smb_fname_str_dbg(smb_fname)));
3411 if (!is_named_stream(smb_fname)) {
3412 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3414 update_btime(handle, smb_fname);
3420 * Note if lp_posix_paths() is true, we can never
3421 * get here as is_ntfs_stream_smb_fname() is
3422 * always false. So we never need worry about
3423 * not following links here.
3426 if (is_afpinfo_stream(smb_fname->stream_name)) {
3427 rc = fruit_stat_meta(handle, smb_fname, true);
3428 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3429 rc = fruit_stat_rsrc(handle, smb_fname, true);
3431 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3435 update_btime(handle, smb_fname);
3436 smb_fname->st.st_ex_mode &= ~S_IFMT;
3437 smb_fname->st.st_ex_mode |= S_IFREG;
3438 smb_fname->st.st_ex_blocks =
3439 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3444 static int fruit_lstat(vfs_handle_struct *handle,
3445 struct smb_filename *smb_fname)
3449 DEBUG(10, ("fruit_lstat called for %s\n",
3450 smb_fname_str_dbg(smb_fname)));
3452 if (!is_named_stream(smb_fname)) {
3453 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3455 update_btime(handle, smb_fname);
3460 if (is_afpinfo_stream(smb_fname->stream_name)) {
3461 rc = fruit_stat_meta(handle, smb_fname, false);
3462 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3463 rc = fruit_stat_rsrc(handle, smb_fname, false);
3465 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3469 update_btime(handle, smb_fname);
3470 smb_fname->st.st_ex_mode &= ~S_IFMT;
3471 smb_fname->st.st_ex_mode |= S_IFREG;
3472 smb_fname->st.st_ex_blocks =
3473 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3478 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3480 SMB_STRUCT_STAT *sbuf)
3482 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3483 struct smb_filename smb_fname;
3492 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3497 *sbuf = fsp->base_fsp->fsp_name->st;
3498 sbuf->st_ex_size = AFP_INFO_SIZE;
3499 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3503 smb_fname = (struct smb_filename) {
3504 .base_name = fsp->fsp_name->base_name,
3505 .twrp = fsp->fsp_name->twrp,
3508 ret = fruit_stat_base(handle, &smb_fname, false);
3512 *sbuf = smb_fname.st;
3514 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3516 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3521 sbuf->st_ex_ino = ino;
3525 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3527 SMB_STRUCT_STAT *sbuf)
3531 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3536 *sbuf = fsp->base_fsp->fsp_name->st;
3537 sbuf->st_ex_size = AFP_INFO_SIZE;
3538 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3543 static int fruit_fstat_meta(vfs_handle_struct *handle,
3545 SMB_STRUCT_STAT *sbuf,
3550 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3552 switch (fio->config->meta) {
3553 case FRUIT_META_STREAM:
3554 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3557 case FRUIT_META_NETATALK:
3558 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3562 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3566 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3570 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3572 SMB_STRUCT_STAT *sbuf)
3574 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3577 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3579 SMB_STRUCT_STAT *sbuf)
3581 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3584 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3586 SMB_STRUCT_STAT *sbuf)
3588 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3589 struct adouble *ad = NULL;
3592 if (fio == NULL || fio->ad_fsp == NULL) {
3593 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3598 /* Populate the stat struct with info from the base file. */
3599 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3604 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3606 DBG_ERR("ad_fget [%s] failed [%s]\n",
3607 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3611 *sbuf = fsp->base_fsp->fsp_name->st;
3612 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3613 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3619 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3620 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3624 switch (fio->config->rsrc) {
3625 case FRUIT_RSRC_STREAM:
3626 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3629 case FRUIT_RSRC_ADFILE:
3630 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3633 case FRUIT_RSRC_XATTR:
3634 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3638 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3645 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3646 SMB_STRUCT_STAT *sbuf)
3648 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3652 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3655 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3657 if (fio->type == ADOUBLE_META) {
3658 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3660 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3664 sbuf->st_ex_mode &= ~S_IFMT;
3665 sbuf->st_ex_mode |= S_IFREG;
3666 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3669 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3670 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3674 static NTSTATUS delete_invalid_meta_stream(
3675 vfs_handle_struct *handle,
3676 const struct smb_filename *smb_fname,
3677 TALLOC_CTX *mem_ctx,
3678 unsigned int *pnum_streams,
3679 struct stream_struct **pstreams,
3682 struct smb_filename *sname = NULL;
3687 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3689 return NT_STATUS_INTERNAL_ERROR;
3693 return NT_STATUS_OK;
3696 status = synthetic_pathref(talloc_tos(),
3697 handle->conn->cwd_fsp,
3698 smb_fname->base_name,
3699 AFPINFO_STREAM_NAME,
3704 if (!NT_STATUS_IS_OK(status)) {
3705 return NT_STATUS_NO_MEMORY;
3708 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3709 handle->conn->cwd_fsp,
3713 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3715 return map_nt_error_from_unix(errno);
3719 return NT_STATUS_OK;
3722 static NTSTATUS fruit_streaminfo_meta_stream(
3723 vfs_handle_struct *handle,
3724 struct files_struct *fsp,
3725 const struct smb_filename *smb_fname,
3726 TALLOC_CTX *mem_ctx,
3727 unsigned int *pnum_streams,
3728 struct stream_struct **pstreams)
3730 struct stream_struct *stream = *pstreams;
3731 unsigned int num_streams = *pnum_streams;
3734 for (i = 0; i < num_streams; i++) {
3735 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3740 if (i == num_streams) {
3741 return NT_STATUS_OK;
3744 if (stream[i].size != AFP_INFO_SIZE) {
3745 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3746 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3748 return delete_invalid_meta_stream(handle,
3757 return NT_STATUS_OK;
3760 static NTSTATUS fruit_streaminfo_meta_netatalk(
3761 vfs_handle_struct *handle,
3762 struct files_struct *fsp,
3763 const struct smb_filename *smb_fname,
3764 TALLOC_CTX *mem_ctx,
3765 unsigned int *pnum_streams,
3766 struct stream_struct **pstreams)
3768 struct stream_struct *stream = *pstreams;
3769 unsigned int num_streams = *pnum_streams;
3770 struct adouble *ad = NULL;
3775 /* Remove the Netatalk xattr from the list */
3776 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3777 ":" NETATALK_META_XATTR ":$DATA");
3779 return NT_STATUS_NO_MEMORY;
3783 * Check if there's a AFPINFO_STREAM from the VFS streams
3784 * backend and if yes, remove it from the list
3786 for (i = 0; i < num_streams; i++) {
3787 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3792 if (i < num_streams) {
3793 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3794 smb_fname_str_dbg(smb_fname));
3796 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3799 return NT_STATUS_INTERNAL_ERROR;
3803 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3805 return NT_STATUS_OK;
3808 is_fi_empty = ad_empty_finderinfo(ad);
3812 return NT_STATUS_OK;
3815 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3816 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3817 smb_roundup(handle->conn, AFP_INFO_SIZE));
3819 return NT_STATUS_NO_MEMORY;
3822 return NT_STATUS_OK;
3825 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3826 struct files_struct *fsp,
3827 const struct smb_filename *smb_fname,
3828 TALLOC_CTX *mem_ctx,
3829 unsigned int *pnum_streams,
3830 struct stream_struct **pstreams)
3832 struct fruit_config_data *config = NULL;
3835 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3836 return NT_STATUS_INTERNAL_ERROR);
3838 switch (config->meta) {
3839 case FRUIT_META_NETATALK:
3840 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3841 mem_ctx, pnum_streams,
3845 case FRUIT_META_STREAM:
3846 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3847 mem_ctx, pnum_streams,
3852 return NT_STATUS_INTERNAL_ERROR;
3858 static NTSTATUS fruit_streaminfo_rsrc_stream(
3859 vfs_handle_struct *handle,
3860 struct files_struct *fsp,
3861 const struct smb_filename *smb_fname,
3862 TALLOC_CTX *mem_ctx,
3863 unsigned int *pnum_streams,
3864 struct stream_struct **pstreams)
3868 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3870 DBG_ERR("Filtering resource stream failed\n");
3871 return NT_STATUS_INTERNAL_ERROR;
3873 return NT_STATUS_OK;
3876 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3877 vfs_handle_struct *handle,
3878 struct files_struct *fsp,
3879 const struct smb_filename *smb_fname,
3880 TALLOC_CTX *mem_ctx,
3881 unsigned int *pnum_streams,
3882 struct stream_struct **pstreams)
3886 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3888 DBG_ERR("Filtering resource stream failed\n");
3889 return NT_STATUS_INTERNAL_ERROR;
3891 return NT_STATUS_OK;
3894 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3895 vfs_handle_struct *handle,
3896 struct files_struct *fsp,
3897 const struct smb_filename *smb_fname,
3898 TALLOC_CTX *mem_ctx,
3899 unsigned int *pnum_streams,
3900 struct stream_struct **pstreams)
3902 struct stream_struct *stream = *pstreams;
3903 unsigned int num_streams = *pnum_streams;
3904 struct adouble *ad = NULL;
3910 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3911 * and if yes, remove it from the list
3913 for (i = 0; i < num_streams; i++) {
3914 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3919 if (i < num_streams) {
3920 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3921 smb_fname_str_dbg(smb_fname));
3923 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3924 AFPRESOURCE_STREAM);
3926 return NT_STATUS_INTERNAL_ERROR;
3930 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3932 return NT_STATUS_OK;
3935 rlen = ad_getentrylen(ad, ADEID_RFORK);
3939 return NT_STATUS_OK;
3942 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3943 AFPRESOURCE_STREAM_NAME, rlen,
3944 smb_roundup(handle->conn, rlen));
3946 return NT_STATUS_NO_MEMORY;
3949 return NT_STATUS_OK;
3952 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3953 struct files_struct *fsp,
3954 const struct smb_filename *smb_fname,
3955 TALLOC_CTX *mem_ctx,
3956 unsigned int *pnum_streams,
3957 struct stream_struct **pstreams)
3959 struct fruit_config_data *config = NULL;
3962 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
3963 return NT_STATUS_OK;
3966 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3967 return NT_STATUS_INTERNAL_ERROR);
3969 switch (config->rsrc) {
3970 case FRUIT_RSRC_STREAM:
3971 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3972 mem_ctx, pnum_streams,
3976 case FRUIT_RSRC_XATTR:
3977 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3978 mem_ctx, pnum_streams,
3982 case FRUIT_RSRC_ADFILE:
3983 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3984 mem_ctx, pnum_streams,
3989 return NT_STATUS_INTERNAL_ERROR;
3995 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3996 struct stream_struct **pstreams)
3998 unsigned num_streams = *pnum_streams;
3999 struct stream_struct *streams = *pstreams;
4002 if (!global_fruit_config.nego_aapl) {
4006 while (i < num_streams) {
4007 struct smb_filename smb_fname = (struct smb_filename) {
4008 .stream_name = streams[i].name,
4011 if (is_ntfs_default_stream_smb_fname(&smb_fname)
4012 || streams[i].size > 0)
4018 streams[i] = streams[num_streams - 1];
4022 *pnum_streams = num_streams;
4025 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
4026 struct files_struct *fsp,
4027 TALLOC_CTX *mem_ctx,
4028 unsigned int *pnum_streams,
4029 struct stream_struct **pstreams)
4031 struct fruit_config_data *config = NULL;
4032 const struct smb_filename *smb_fname = NULL;
4035 smb_fname = fsp->fsp_name;
4037 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4038 return NT_STATUS_UNSUCCESSFUL);
4040 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4042 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
4043 pnum_streams, pstreams);
4044 if (!NT_STATUS_IS_OK(status)) {
4048 fruit_filter_empty_streams(pnum_streams, pstreams);
4050 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4051 mem_ctx, pnum_streams, pstreams);
4052 if (!NT_STATUS_IS_OK(status)) {
4056 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4057 mem_ctx, pnum_streams, pstreams);
4058 if (!NT_STATUS_IS_OK(status)) {
4062 return NT_STATUS_OK;
4065 static int fruit_fntimes(vfs_handle_struct *handle,
4067 struct smb_file_time *ft)
4070 struct adouble *ad = NULL;
4071 struct fruit_config_data *config = NULL;
4073 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4076 if ((config->meta != FRUIT_META_NETATALK) ||
4077 is_omit_timespec(&ft->create_time))
4079 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4082 DBG_DEBUG("set btime for %s to %s\n", fsp_str_dbg(fsp),
4083 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
4085 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4090 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4091 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4093 rc = ad_fset(handle, ad, fsp);
4099 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
4102 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4105 static int fruit_fallocate(struct vfs_handle_struct *handle,
4106 struct files_struct *fsp,
4111 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4114 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4117 /* Let the pwrite code path handle it. */
4122 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4123 struct files_struct *fsp,
4126 #ifdef HAVE_ATTROPEN
4127 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4132 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4133 struct files_struct *fsp,
4136 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4138 struct adouble *ad = NULL;
4141 if (fio == NULL || fio->ad_fsp == NULL) {
4142 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4147 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4149 DBG_ERR("ad_fget [%s] failed [%s]\n",
4150 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4154 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4156 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4162 ad_setentrylen(ad, ADEID_RFORK, offset);
4164 rc = ad_fset(handle, ad, fio->ad_fsp);
4166 DBG_ERR("ad_fset [%s] failed [%s]\n",
4167 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4176 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4177 struct files_struct *fsp,
4180 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4183 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4184 struct files_struct *fsp,
4187 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4191 DBG_ERR("Failed to fetch fsp extension");
4195 switch (fio->config->rsrc) {
4196 case FRUIT_RSRC_XATTR:
4197 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4200 case FRUIT_RSRC_ADFILE:
4201 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4204 case FRUIT_RSRC_STREAM:
4205 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4209 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4217 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4218 struct files_struct *fsp,
4222 DBG_WARNING("ftruncate %s to %jd",
4223 fsp_str_dbg(fsp), (intmax_t)offset);
4224 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4229 /* OS X returns success but does nothing */
4230 DBG_INFO("ignoring ftruncate %s to %jd\n",
4231 fsp_str_dbg(fsp), (intmax_t)offset);
4235 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4236 struct files_struct *fsp,
4239 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4242 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4246 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4249 if (fio->type == ADOUBLE_META) {
4250 ret = fruit_ftruncate_meta(handle, fsp, offset);
4252 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4255 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4259 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4260 struct smb_request *req,
4261 struct files_struct *dirfsp,
4262 struct smb_filename *smb_fname,
4263 uint32_t access_mask,
4264 uint32_t share_access,
4265 uint32_t create_disposition,
4266 uint32_t create_options,
4267 uint32_t file_attributes,
4268 uint32_t oplock_request,
4269 const struct smb2_lease *lease,
4270 uint64_t allocation_size,
4271 uint32_t private_flags,
4272 struct security_descriptor *sd,
4273 struct ea_list *ea_list,
4274 files_struct **result,
4276 const struct smb2_create_blobs *in_context_blobs,
4277 struct smb2_create_blobs *out_context_blobs)
4280 struct fruit_config_data *config = NULL;
4281 files_struct *fsp = NULL;
4282 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4285 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4286 if (!NT_STATUS_IS_OK(status)) {
4290 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4291 return NT_STATUS_UNSUCCESSFUL);
4293 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4294 uint32_t conv_flags = 0;
4296 if (config->wipe_intentionally_left_blank_rfork) {
4297 conv_flags |= AD_CONV_WIPE_BLANK;
4299 if (config->delete_empty_adfiles) {
4300 conv_flags |= AD_CONV_DELETE;
4303 ret = ad_convert(handle,
4305 macos_string_replace_map,
4308 DBG_ERR("ad_convert(\"%s\") failed\n",
4309 smb_fname_str_dbg(smb_fname));
4313 status = SMB_VFS_NEXT_CREATE_FILE(
4314 handle, req, dirfsp, smb_fname,
4315 access_mask, share_access,
4316 create_disposition, create_options,
4317 file_attributes, oplock_request,
4319 allocation_size, private_flags,
4320 sd, ea_list, result,
4321 pinfo, in_context_blobs, out_context_blobs);
4322 if (!NT_STATUS_IS_OK(status)) {
4328 if (global_fruit_config.nego_aapl) {
4329 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4331 * Enable POSIX directory rename behaviour
4333 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4338 * If this is a plain open for existing files, opening an 0
4339 * byte size resource fork MUST fail with
4340 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4342 * Cf the vfs_fruit torture tests in test_rfork_create().
4344 if (global_fruit_config.nego_aapl &&
4345 create_disposition == FILE_OPEN &&
4346 smb_fname->st.st_ex_size == 0 &&
4347 is_named_stream(smb_fname))
4349 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4353 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4357 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4358 (fsp->op != NULL) &&
4359 !fsp->fsp_flags.is_pathref)
4361 status = fruit_check_access(
4365 if (!NT_STATUS_IS_OK(status)) {
4373 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4376 close_file_free(req, &fsp, ERROR_CLOSE);
4383 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4384 struct files_struct *fsp,
4385 TALLOC_CTX *mem_ctx,
4386 struct readdir_attr_data **pattr_data)
4388 struct fruit_config_data *config = NULL;
4389 struct readdir_attr_data *attr_data;
4390 uint32_t conv_flags = 0;
4394 SMB_VFS_HANDLE_GET_DATA(handle, config,
4395 struct fruit_config_data,
4396 return NT_STATUS_UNSUCCESSFUL);
4398 if (!global_fruit_config.nego_aapl) {
4399 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4405 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4407 if (config->wipe_intentionally_left_blank_rfork) {
4408 conv_flags |= AD_CONV_WIPE_BLANK;
4410 if (config->delete_empty_adfiles) {
4411 conv_flags |= AD_CONV_DELETE;
4414 ret = ad_convert(handle,
4416 macos_string_replace_map,
4419 DBG_ERR("ad_convert(\"%s\") failed\n",
4423 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4424 if (*pattr_data == NULL) {
4425 return NT_STATUS_NO_MEMORY;
4427 attr_data = *pattr_data;
4428 attr_data->type = RDATTR_AAPL;
4431 * Mac metadata: compressed FinderInfo, resource fork length
4434 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4435 if (!NT_STATUS_IS_OK(status)) {
4437 * Error handling is tricky: if we return failure from
4438 * this function, the corresponding directory entry
4439 * will to be passed to the client, so we really just
4440 * want to error out on fatal errors.
4442 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4450 if (config->unix_info_enabled) {
4451 attr_data->attr_data.aapl.unix_mode =
4452 fsp->fsp_name->st.st_ex_mode;
4458 if (!config->readdir_attr_max_access) {
4459 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4461 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4464 SEC_FLAG_MAXIMUM_ALLOWED,
4465 &attr_data->attr_data.aapl.max_access);
4466 if (!NT_STATUS_IS_OK(status)) {
4471 return NT_STATUS_OK;
4474 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4476 TALLOC_FREE(*pattr_data);
4480 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4482 uint32_t security_info,
4483 TALLOC_CTX *mem_ctx,
4484 struct security_descriptor **ppdesc)
4487 struct security_ace ace;
4489 struct fruit_config_data *config;
4491 SMB_VFS_HANDLE_GET_DATA(handle, config,
4492 struct fruit_config_data,
4493 return NT_STATUS_UNSUCCESSFUL);
4495 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4497 if (!NT_STATUS_IS_OK(status)) {
4502 * Add MS NFS style ACEs with uid, gid and mode
4504 if (!global_fruit_config.nego_aapl) {
4505 return NT_STATUS_OK;
4507 if (!config->unix_info_enabled) {
4508 return NT_STATUS_OK;
4511 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4512 status = remove_virtual_nfs_aces(*ppdesc);
4513 if (!NT_STATUS_IS_OK(status)) {
4514 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4518 /* MS NFS style mode */
4519 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4520 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4521 status = security_descriptor_dacl_add(*ppdesc, &ace);
4522 if (!NT_STATUS_IS_OK(status)) {
4523 DEBUG(1,("failed to add MS NFS style ACE\n"));
4527 /* MS NFS style uid */
4528 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4529 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4530 status = security_descriptor_dacl_add(*ppdesc, &ace);
4531 if (!NT_STATUS_IS_OK(status)) {
4532 DEBUG(1,("failed to add MS NFS style ACE\n"));
4536 /* MS NFS style gid */
4537 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4538 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4539 status = security_descriptor_dacl_add(*ppdesc, &ace);
4540 if (!NT_STATUS_IS_OK(status)) {
4541 DEBUG(1,("failed to add MS NFS style ACE\n"));
4545 return NT_STATUS_OK;
4548 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4550 uint32_t security_info_sent,
4551 const struct security_descriptor *orig_psd)
4555 mode_t ms_nfs_mode = 0;
4557 struct security_descriptor *psd = NULL;
4558 uint32_t orig_num_aces = 0;
4560 if (orig_psd->dacl != NULL) {
4561 orig_num_aces = orig_psd->dacl->num_aces;
4564 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4566 return NT_STATUS_NO_MEMORY;
4569 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4571 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4572 if (!NT_STATUS_IS_OK(status)) {
4573 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4579 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4580 * sent/present flags correctly now we've removed them.
4583 if (orig_num_aces != 0) {
4585 * Are there any ACE's left ?
4587 if (psd->dacl->num_aces == 0) {
4588 /* No - clear the DACL sent/present flags. */
4589 security_info_sent &= ~SECINFO_DACL;
4590 psd->type &= ~SEC_DESC_DACL_PRESENT;
4594 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4595 if (!NT_STATUS_IS_OK(status)) {
4596 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4602 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4604 DBG_WARNING("%s, result: %d, %04o error %s\n",
4607 (unsigned)ms_nfs_mode,
4609 status = map_nt_error_from_unix(errno);
4616 return NT_STATUS_OK;
4619 static struct vfs_offload_ctx *fruit_offload_ctx;
4621 struct fruit_offload_read_state {
4622 struct vfs_handle_struct *handle;
4623 struct tevent_context *ev;
4631 static void fruit_offload_read_done(struct tevent_req *subreq);
4633 static struct tevent_req *fruit_offload_read_send(
4634 TALLOC_CTX *mem_ctx,
4635 struct tevent_context *ev,
4636 struct vfs_handle_struct *handle,
4643 struct tevent_req *req = NULL;
4644 struct tevent_req *subreq = NULL;
4645 struct fruit_offload_read_state *state = NULL;
4647 req = tevent_req_create(mem_ctx, &state,
4648 struct fruit_offload_read_state);
4652 *state = (struct fruit_offload_read_state) {
4659 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4660 fsctl, ttl, offset, to_copy);
4661 if (tevent_req_nomem(subreq, req)) {
4662 return tevent_req_post(req, ev);
4664 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4668 static void fruit_offload_read_done(struct tevent_req *subreq)
4670 struct tevent_req *req = tevent_req_callback_data(
4671 subreq, struct tevent_req);
4672 struct fruit_offload_read_state *state = tevent_req_data(
4673 req, struct fruit_offload_read_state);
4676 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4682 TALLOC_FREE(subreq);
4683 if (tevent_req_nterror(req, status)) {
4687 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4688 tevent_req_done(req);
4692 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4693 &fruit_offload_ctx);
4694 if (tevent_req_nterror(req, status)) {
4698 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4701 if (tevent_req_nterror(req, status)) {
4705 tevent_req_done(req);
4709 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4710 struct vfs_handle_struct *handle,
4711 TALLOC_CTX *mem_ctx,
4716 struct fruit_offload_read_state *state = tevent_req_data(
4717 req, struct fruit_offload_read_state);
4720 if (tevent_req_is_nterror(req, &status)) {
4721 tevent_req_received(req);
4725 *flags = state->flags;
4726 *xferlen = state->xferlen;
4727 token->length = state->token.length;
4728 token->data = talloc_move(mem_ctx, &state->token.data);
4730 tevent_req_received(req);
4731 return NT_STATUS_OK;
4734 struct fruit_offload_write_state {
4735 struct vfs_handle_struct *handle;
4737 struct files_struct *src_fsp;
4738 struct files_struct *dst_fsp;
4742 static void fruit_offload_write_done(struct tevent_req *subreq);
4743 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4744 TALLOC_CTX *mem_ctx,
4745 struct tevent_context *ev,
4748 off_t transfer_offset,
4749 struct files_struct *dest_fsp,
4753 struct tevent_req *req, *subreq;
4754 struct fruit_offload_write_state *state;
4756 struct fruit_config_data *config;
4757 off_t src_off = transfer_offset;
4758 files_struct *src_fsp = NULL;
4759 off_t to_copy = num;
4760 bool copyfile_enabled = false;
4762 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4763 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4765 SMB_VFS_HANDLE_GET_DATA(handle, config,
4766 struct fruit_config_data,
4769 req = tevent_req_create(mem_ctx, &state,
4770 struct fruit_offload_write_state);
4774 state->handle = handle;
4775 state->dst_fsp = dest_fsp;
4778 case FSCTL_SRV_COPYCHUNK:
4779 case FSCTL_SRV_COPYCHUNK_WRITE:
4780 copyfile_enabled = config->copyfile_enabled;
4787 * Check if this a OS X copyfile style copychunk request with
4788 * a requested chunk count of 0 that was translated to a
4789 * offload_write_send VFS call overloading the parameters src_off
4790 * = dest_off = num = 0.
4792 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4793 status = vfs_offload_token_db_fetch_fsp(
4794 fruit_offload_ctx, token, &src_fsp);
4795 if (tevent_req_nterror(req, status)) {
4796 return tevent_req_post(req, ev);
4798 state->src_fsp = src_fsp;
4800 status = vfs_stat_fsp(src_fsp);
4801 if (tevent_req_nterror(req, status)) {
4802 return tevent_req_post(req, ev);
4805 to_copy = src_fsp->fsp_name->st.st_ex_size;
4806 state->is_copyfile = true;
4809 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4818 if (tevent_req_nomem(subreq, req)) {
4819 return tevent_req_post(req, ev);
4822 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4826 static void fruit_offload_write_done(struct tevent_req *subreq)
4828 struct tevent_req *req = tevent_req_callback_data(
4829 subreq, struct tevent_req);
4830 struct fruit_offload_write_state *state = tevent_req_data(
4831 req, struct fruit_offload_write_state);
4833 unsigned int num_streams = 0;
4834 struct stream_struct *streams = NULL;
4836 struct smb_filename *src_fname_tmp = NULL;
4837 struct smb_filename *dst_fname_tmp = NULL;
4839 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4842 TALLOC_FREE(subreq);
4843 if (tevent_req_nterror(req, status)) {
4847 if (!state->is_copyfile) {
4848 tevent_req_done(req);
4853 * Now copy all remaining streams. We know the share supports
4854 * streams, because we're in vfs_fruit. We don't do this async
4855 * because streams are few and small.
4857 status = vfs_fstreaminfo(state->src_fsp,
4858 req, &num_streams, &streams);
4859 if (tevent_req_nterror(req, status)) {
4863 if (num_streams == 1) {
4864 /* There is always one stream, ::$DATA. */
4865 tevent_req_done(req);
4869 for (i = 0; i < num_streams; i++) {
4870 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4871 __func__, streams[i].name, (size_t)streams[i].size));
4873 src_fname_tmp = synthetic_smb_fname(
4875 state->src_fsp->fsp_name->base_name,
4878 state->src_fsp->fsp_name->twrp,
4879 state->src_fsp->fsp_name->flags);
4880 if (tevent_req_nomem(src_fname_tmp, req)) {
4884 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4885 TALLOC_FREE(src_fname_tmp);
4889 dst_fname_tmp = synthetic_smb_fname(
4891 state->dst_fsp->fsp_name->base_name,
4894 state->dst_fsp->fsp_name->twrp,
4895 state->dst_fsp->fsp_name->flags);
4896 if (tevent_req_nomem(dst_fname_tmp, req)) {
4897 TALLOC_FREE(src_fname_tmp);
4901 status = copy_file(req,
4902 state->handle->conn,
4906 if (!NT_STATUS_IS_OK(status)) {
4907 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4908 smb_fname_str_dbg(src_fname_tmp),
4909 smb_fname_str_dbg(dst_fname_tmp),
4910 nt_errstr(status)));
4911 TALLOC_FREE(src_fname_tmp);
4912 TALLOC_FREE(dst_fname_tmp);
4913 tevent_req_nterror(req, status);
4917 TALLOC_FREE(src_fname_tmp);
4918 TALLOC_FREE(dst_fname_tmp);
4921 TALLOC_FREE(streams);
4922 TALLOC_FREE(src_fname_tmp);
4923 TALLOC_FREE(dst_fname_tmp);
4924 tevent_req_done(req);
4927 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4928 struct tevent_req *req,
4931 struct fruit_offload_write_state *state = tevent_req_data(
4932 req, struct fruit_offload_write_state);
4935 if (tevent_req_is_nterror(req, &status)) {
4936 DEBUG(1, ("server side copy chunk failed: %s\n",
4937 nt_errstr(status)));
4939 tevent_req_received(req);
4943 *copied = state->copied;
4944 tevent_req_received(req);
4946 return NT_STATUS_OK;
4949 static char *fruit_get_bandsize_line(char **lines, int numlines)
4952 static bool re_initialized = false;
4956 if (!re_initialized) {
4957 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4961 re_initialized = true;
4964 for (i = 0; i < numlines; i++) {
4965 regmatch_t matches[1];
4967 ret = regexec(&re, lines[i], 1, matches, 0);
4970 * Check if the match was on the last line, sa we want
4971 * the subsequent line.
4973 if (i + 1 == numlines) {
4976 return lines[i + 1];
4978 if (ret != REG_NOMATCH) {
4986 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4989 static bool re_initialized = false;
4990 regmatch_t matches[2];
4995 if (!re_initialized) {
4998 "<integer>\\([[:digit:]]*\\)</integer>$",
5003 re_initialized = true;
5006 ret = regexec(&re, line, 2, matches, 0);
5008 DBG_ERR("regex failed [%s]\n", line);
5012 line[matches[1].rm_eo] = '\0';
5014 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
5018 *_band_size = (size_t)band_size;
5023 * This reads and parses an Info.plist from a TM sparsebundle looking for the
5024 * "band-size" key and value.
5026 static bool fruit_get_bandsize(vfs_handle_struct *handle,
5030 #define INFO_PLIST_MAX_SIZE 64*1024
5032 struct smb_filename *smb_fname = NULL;
5033 files_struct *fsp = NULL;
5034 uint8_t *file_data = NULL;
5035 char **lines = NULL;
5036 char *band_size_line = NULL;
5037 size_t plist_file_size;
5044 plist = talloc_asprintf(talloc_tos(),
5046 handle->conn->connectpath,
5048 if (plist == NULL) {
5053 smb_fname = synthetic_smb_fname(talloc_tos(),
5059 if (smb_fname == NULL) {
5064 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5066 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
5071 plist_file_size = smb_fname->st.st_ex_size;
5073 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
5074 DBG_INFO("%s is too large, ignoring\n", plist);
5079 status = SMB_VFS_NEXT_CREATE_FILE(
5083 smb_fname, /* fname */
5084 FILE_GENERIC_READ, /* access_mask */
5085 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
5086 FILE_OPEN, /* create_disposition */
5087 0, /* create_options */
5088 0, /* file_attributes */
5089 INTERNAL_OPEN_ONLY, /* oplock_request */
5091 0, /* allocation_size */
5092 0, /* private_flags */
5097 NULL, NULL); /* create context */
5098 if (!NT_STATUS_IS_OK(status)) {
5099 DBG_INFO("Opening [%s] failed [%s]\n",
5100 smb_fname_str_dbg(smb_fname), nt_errstr(status));
5105 file_data = talloc_zero_array(talloc_tos(),
5107 plist_file_size + 1);
5108 if (file_data == NULL) {
5113 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
5114 if (nread != plist_file_size) {
5115 DBG_ERR("Short read on [%s]: %zu/%zd\n",
5116 fsp_str_dbg(fsp), nread, plist_file_size);
5122 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5123 if (!NT_STATUS_IS_OK(status)) {
5124 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5129 lines = file_lines_parse((char *)file_data,
5133 if (lines == NULL) {
5138 band_size_line = fruit_get_bandsize_line(lines, numlines);
5139 if (band_size_line == NULL) {
5140 DBG_ERR("Didn't find band-size key in [%s]\n",
5141 smb_fname_str_dbg(smb_fname));
5146 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5148 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5152 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5156 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5157 if (!NT_STATUS_IS_OK(status)) {
5158 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5162 TALLOC_FREE(smb_fname);
5163 TALLOC_FREE(file_data);
5168 struct fruit_disk_free_state {
5172 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5177 struct smb_filename *bands_dir = NULL;
5178 struct smb_Dir *dir_hnd = NULL;
5179 const char *dname = NULL;
5180 char *talloced = NULL;
5185 path = talloc_asprintf(talloc_tos(),
5187 handle->conn->connectpath,
5193 bands_dir = synthetic_smb_fname(talloc_tos(),
5200 if (bands_dir == NULL) {
5204 status = OpenDir(talloc_tos(),
5210 if (!NT_STATUS_IS_OK(status)) {
5211 TALLOC_FREE(bands_dir);
5212 errno = map_errno_from_nt_status(status);
5218 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5221 if (ISDOT(dname) || ISDOTDOT(dname)) {
5226 TALLOC_FREE(dir_hnd);
5228 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5230 TALLOC_FREE(bands_dir);
5236 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5237 struct fruit_disk_free_state *state,
5242 size_t sparsebundle_strlen = strlen("sparsebundle");
5243 size_t bandsize = 0;
5247 p = strstr(name, "sparsebundle");
5252 if (p[sparsebundle_strlen] != '\0') {
5256 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5258 ok = fruit_get_bandsize(handle, name, &bandsize);
5261 * Beware of race conditions: this may be an uninitialized
5262 * Info.plist that a client is just creating. We don't want let
5263 * this to trigger complete failure.
5265 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5269 ok = fruit_get_num_bands(handle, name, &nbands);
5272 * Beware of race conditions: this may be a backup sparsebundle
5273 * in an early stage lacking a bands subdirectory. We don't want
5274 * let this to trigger complete failure.
5276 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5281 * Arithmetic on 32-bit systems may cause overflow, depending on
5282 * size_t precision. First we check its unlikely, then we
5283 * force the precision into target off_t, then we check that
5284 * the total did not overflow either.
5286 if (bandsize > SIZE_MAX/nbands) {
5287 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5291 tm_size = (off_t)bandsize * (off_t)nbands;
5293 if (state->total_size + tm_size < state->total_size) {
5294 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5299 state->total_size += tm_size;
5301 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5302 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5308 * Calculate used size of a TimeMachine volume
5310 * This assumes that the volume is used only for TimeMachine.
5312 * - readdir(basedir of share), then
5313 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5314 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5315 * - count band files in "\1.sparsebundle/bands/"
5316 * - calculate used size of all bands: band_count * band_size
5318 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5319 const struct smb_filename *smb_fname,
5324 struct fruit_config_data *config = NULL;
5325 struct fruit_disk_free_state state = {0};
5326 struct smb_Dir *dir_hnd = NULL;
5327 const char *dname = NULL;
5328 char *talloced = NULL;
5335 SMB_VFS_HANDLE_GET_DATA(handle, config,
5336 struct fruit_config_data,
5339 if (!config->time_machine ||
5340 config->time_machine_max_size == 0)
5342 return SMB_VFS_NEXT_DISK_FREE(handle,
5349 status = OpenDir(talloc_tos(),
5355 if (!NT_STATUS_IS_OK(status)) {
5356 errno = map_errno_from_nt_status(status);
5360 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5363 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5365 TALLOC_FREE(talloced);
5366 TALLOC_FREE(dir_hnd);
5369 TALLOC_FREE(talloced);
5372 TALLOC_FREE(dir_hnd);
5374 dsize = config->time_machine_max_size / 512;
5375 dfree = dsize - (state.total_size / 512);
5376 if (dfree > dsize) {
5386 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5387 const SMB_STRUCT_STAT *psbuf)
5389 struct fruit_config_data *config = NULL;
5391 SMB_VFS_HANDLE_GET_DATA(handle, config,
5392 struct fruit_config_data,
5395 if (global_fruit_config.nego_aapl &&
5396 config->aapl_zero_file_id)
5401 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5404 static struct vfs_fn_pointers vfs_fruit_fns = {
5405 .connect_fn = fruit_connect,
5406 .disk_free_fn = fruit_disk_free,
5408 /* File operations */
5409 .fchmod_fn = fruit_fchmod,
5410 .unlinkat_fn = fruit_unlinkat,
5411 .renameat_fn = fruit_renameat,
5412 .openat_fn = fruit_openat,
5413 .close_fn = fruit_close,
5414 .pread_fn = fruit_pread,
5415 .pwrite_fn = fruit_pwrite,
5416 .pread_send_fn = fruit_pread_send,
5417 .pread_recv_fn = fruit_pread_recv,
5418 .pwrite_send_fn = fruit_pwrite_send,
5419 .pwrite_recv_fn = fruit_pwrite_recv,
5420 .fsync_send_fn = fruit_fsync_send,
5421 .fsync_recv_fn = fruit_fsync_recv,
5422 .stat_fn = fruit_stat,
5423 .lstat_fn = fruit_lstat,
5424 .fstat_fn = fruit_fstat,
5425 .fstreaminfo_fn = fruit_fstreaminfo,
5426 .fntimes_fn = fruit_fntimes,
5427 .ftruncate_fn = fruit_ftruncate,
5428 .fallocate_fn = fruit_fallocate,
5429 .create_file_fn = fruit_create_file,
5430 .freaddir_attr_fn = fruit_freaddir_attr,
5431 .offload_read_send_fn = fruit_offload_read_send,
5432 .offload_read_recv_fn = fruit_offload_read_recv,
5433 .offload_write_send_fn = fruit_offload_write_send,
5434 .offload_write_recv_fn = fruit_offload_write_recv,
5435 .fs_file_id_fn = fruit_fs_file_id,
5437 /* NT ACL operations */
5438 .fget_nt_acl_fn = fruit_fget_nt_acl,
5439 .fset_nt_acl_fn = fruit_fset_nt_acl,
5443 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5445 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5447 if (!NT_STATUS_IS_OK(ret)) {
5451 vfs_fruit_debug_level = debug_add_class("fruit");
5452 if (vfs_fruit_debug_level == -1) {
5453 vfs_fruit_debug_level = DBGC_VFS;
5454 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5457 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5458 "vfs_fruit_init","fruit",vfs_fruit_debug_level));