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->tcon->compat->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->tcon->compat));
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 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1763 if (is_afpinfo_stream(smb_fname->stream_name)) {
1764 fd = fruit_open_meta(handle,
1770 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1771 fd = fruit_open_rsrc(handle,
1778 fd = SMB_VFS_NEXT_OPENAT(handle,
1785 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1787 /* Prevent reopen optimisation */
1788 fsp->fsp_flags.have_proc_fds = false;
1792 static int fruit_close_meta(vfs_handle_struct *handle,
1796 struct fruit_config_data *config = NULL;
1798 SMB_VFS_HANDLE_GET_DATA(handle, config,
1799 struct fruit_config_data, return -1);
1801 switch (config->meta) {
1802 case FRUIT_META_STREAM:
1804 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1809 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1810 fsp_set_fd(fsp, -1);
1812 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1816 case FRUIT_META_NETATALK:
1817 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1818 fsp_set_fd(fsp, -1);
1822 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1830 static int fruit_close_rsrc(vfs_handle_struct *handle,
1834 struct fruit_config_data *config = NULL;
1836 SMB_VFS_HANDLE_GET_DATA(handle, config,
1837 struct fruit_config_data, return -1);
1839 switch (config->rsrc) {
1840 case FRUIT_RSRC_STREAM:
1841 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1844 case FRUIT_RSRC_ADFILE:
1846 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1850 fio_close_ad_fsp(fio);
1851 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1852 fsp_set_fd(fsp, -1);
1856 case FRUIT_RSRC_XATTR:
1857 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1858 fsp_set_fd(fsp, -1);
1862 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1869 static int fruit_close(vfs_handle_struct *handle,
1875 fd = fsp_get_pathref_fd(fsp);
1877 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1879 if (!fsp_is_alternate_stream(fsp)) {
1880 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1883 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1884 ret = fruit_close_meta(handle, fsp);
1885 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1886 ret = fruit_close_rsrc(handle, fsp);
1888 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1894 static int fruit_renameat(struct vfs_handle_struct *handle,
1895 files_struct *srcfsp,
1896 const struct smb_filename *smb_fname_src,
1897 files_struct *dstfsp,
1898 const struct smb_filename *smb_fname_dst)
1901 struct fruit_config_data *config = NULL;
1902 struct smb_filename *src_adp_smb_fname = NULL;
1903 struct smb_filename *dst_adp_smb_fname = NULL;
1905 SMB_VFS_HANDLE_GET_DATA(handle, config,
1906 struct fruit_config_data, return -1);
1908 if (!VALID_STAT(smb_fname_src->st)) {
1909 DBG_ERR("Need valid stat for [%s]\n",
1910 smb_fname_str_dbg(smb_fname_src));
1914 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1923 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1924 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1929 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1934 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1939 DBG_DEBUG("%s -> %s\n",
1940 smb_fname_str_dbg(src_adp_smb_fname),
1941 smb_fname_str_dbg(dst_adp_smb_fname));
1943 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1948 if (errno == ENOENT) {
1953 TALLOC_FREE(src_adp_smb_fname);
1954 TALLOC_FREE(dst_adp_smb_fname);
1958 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1959 struct files_struct *dirfsp,
1960 const struct smb_filename *smb_fname)
1962 return SMB_VFS_NEXT_UNLINKAT(handle,
1968 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1969 const struct smb_filename *smb_fname)
1971 SMB_ASSERT(smb_fname->fsp != NULL);
1972 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1973 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1974 AFPINFO_EA_NETATALK);
1977 static int fruit_unlink_meta(vfs_handle_struct *handle,
1978 struct files_struct *dirfsp,
1979 const struct smb_filename *smb_fname)
1981 struct fruit_config_data *config = NULL;
1984 SMB_VFS_HANDLE_GET_DATA(handle, config,
1985 struct fruit_config_data, return -1);
1987 switch (config->meta) {
1988 case FRUIT_META_STREAM:
1989 rc = fruit_unlink_meta_stream(handle,
1994 case FRUIT_META_NETATALK:
1995 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1999 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2006 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2007 struct files_struct *dirfsp,
2008 const struct smb_filename *smb_fname,
2013 if (!force_unlink) {
2014 struct smb_filename *full_fname = NULL;
2018 * TODO: use SMB_VFS_STATX() once we have it.
2021 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2024 if (full_fname == NULL) {
2029 * 0 byte resource fork streams are not listed by
2030 * vfs_streaminfo, as a result stream cleanup/deletion of file
2031 * deletion doesn't remove the resourcefork stream.
2034 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2036 TALLOC_FREE(full_fname);
2037 DBG_ERR("stat [%s] failed [%s]\n",
2038 smb_fname_str_dbg(full_fname), strerror(errno));
2042 size = full_fname->st.st_ex_size;
2043 TALLOC_FREE(full_fname);
2046 /* OS X ignores resource fork stream delete requests */
2051 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2055 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2062 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2063 struct files_struct *dirfsp,
2064 const struct smb_filename *smb_fname,
2068 struct adouble *ad = NULL;
2069 struct smb_filename *adp_smb_fname = NULL;
2071 if (!force_unlink) {
2072 struct smb_filename *full_fname = NULL;
2074 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2077 if (full_fname == NULL) {
2081 ad = ad_get(talloc_tos(), handle, full_fname,
2083 TALLOC_FREE(full_fname);
2091 * 0 byte resource fork streams are not listed by
2092 * vfs_streaminfo, as a result stream cleanup/deletion of file
2093 * deletion doesn't remove the resourcefork stream.
2096 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2097 /* OS X ignores resource fork stream delete requests */
2105 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2110 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2114 TALLOC_FREE(adp_smb_fname);
2115 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2122 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2123 const struct smb_filename *smb_fname,
2127 * OS X ignores resource fork stream delete requests, so nothing to do
2128 * here. Removing the file will remove the xattr anyway, so we don't
2129 * have to take care of removing 0 byte resource forks that could be
2135 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2136 struct files_struct *dirfsp,
2137 const struct smb_filename *smb_fname,
2140 struct fruit_config_data *config = NULL;
2143 SMB_VFS_HANDLE_GET_DATA(handle, config,
2144 struct fruit_config_data, return -1);
2146 switch (config->rsrc) {
2147 case FRUIT_RSRC_STREAM:
2148 rc = fruit_unlink_rsrc_stream(handle,
2154 case FRUIT_RSRC_ADFILE:
2155 rc = fruit_unlink_rsrc_adouble(handle,
2161 case FRUIT_RSRC_XATTR:
2162 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2166 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2173 static int fruit_fchmod(vfs_handle_struct *handle,
2174 struct files_struct *fsp,
2178 struct fruit_config_data *config = NULL;
2179 struct smb_filename *smb_fname_adp = NULL;
2180 const struct smb_filename *smb_fname = NULL;
2183 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2188 smb_fname = fsp->fsp_name;
2189 SMB_VFS_HANDLE_GET_DATA(handle, config,
2190 struct fruit_config_data, return -1);
2192 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2196 if (!VALID_STAT(smb_fname->st)) {
2200 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2204 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2209 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2211 if (!NT_STATUS_IS_OK(status)) {
2212 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2213 if (NT_STATUS_EQUAL(status,
2214 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2222 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2224 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2225 if (errno == ENOENT) {
2229 TALLOC_FREE(smb_fname_adp);
2233 static int fruit_unlinkat(vfs_handle_struct *handle,
2234 struct files_struct *dirfsp,
2235 const struct smb_filename *smb_fname,
2238 struct fruit_config_data *config = NULL;
2239 struct smb_filename *rsrc_smb_fname = NULL;
2242 if (flags & AT_REMOVEDIR) {
2243 return SMB_VFS_NEXT_UNLINKAT(handle,
2249 SMB_VFS_HANDLE_GET_DATA(handle, config,
2250 struct fruit_config_data, return -1);
2252 if (is_afpinfo_stream(smb_fname->stream_name)) {
2253 return fruit_unlink_meta(handle,
2256 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2257 return fruit_unlink_rsrc(handle,
2261 } else if (is_named_stream(smb_fname)) {
2262 return SMB_VFS_NEXT_UNLINKAT(handle,
2266 } else if (is_adouble_file(smb_fname->base_name)) {
2267 return SMB_VFS_NEXT_UNLINKAT(handle,
2274 * A request to delete the base file. Because 0 byte resource
2275 * fork streams are not listed by fruit_streaminfo,
2276 * delete_all_streams() can't remove 0 byte resource fork
2277 * streams, so we have to cleanup this here.
2279 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2280 smb_fname->base_name,
2281 AFPRESOURCE_STREAM_NAME,
2285 if (rsrc_smb_fname == NULL) {
2289 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2290 if ((ret != 0) && (errno != ENOENT)) {
2291 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2292 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2293 TALLOC_FREE(rsrc_smb_fname);
2296 TALLOC_FREE(rsrc_smb_fname);
2298 return SMB_VFS_NEXT_UNLINKAT(handle,
2304 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2305 files_struct *fsp, void *data,
2306 size_t n, off_t offset)
2308 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2312 if ((fio == NULL) || fio->fake_fd) {
2316 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2317 if (nread == -1 || nread == n) {
2321 DBG_ERR("Removing [%s] after short read [%zd]\n",
2322 fsp_str_dbg(fsp), nread);
2324 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2329 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2337 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2338 files_struct *fsp, void *data,
2339 size_t n, off_t offset)
2342 struct adouble *ad = NULL;
2343 char afpinfo_buf[AFP_INFO_SIZE];
2347 ai = afpinfo_new(talloc_tos());
2352 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2358 p = ad_get_entry(ad, ADEID_FINDERI);
2360 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2365 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2367 nread = afpinfo_pack(ai, afpinfo_buf);
2368 if (nread != AFP_INFO_SIZE) {
2373 memcpy(data, afpinfo_buf, n);
2381 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2382 files_struct *fsp, void *data,
2383 size_t n, off_t offset)
2385 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2390 * OS X has a off-by-1 error in the offset calculation, so we're
2391 * bug compatible here. It won't hurt, as any relevant real
2392 * world read requests from the AFP_AfpInfo stream will be
2393 * offset=0 n=60. offset is ignored anyway, see below.
2395 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2400 DBG_ERR("Failed to fetch fsp extension");
2404 /* Yes, macOS always reads from offset 0 */
2406 to_return = MIN(n, AFP_INFO_SIZE);
2408 switch (fio->config->meta) {
2409 case FRUIT_META_STREAM:
2410 nread = fruit_pread_meta_stream(handle, fsp, data,
2414 case FRUIT_META_NETATALK:
2415 nread = fruit_pread_meta_adouble(handle, fsp, data,
2420 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2424 if (nread == -1 && fio->fake_fd) {
2426 char afpinfo_buf[AFP_INFO_SIZE];
2428 ai = afpinfo_new(talloc_tos());
2433 nread = afpinfo_pack(ai, afpinfo_buf);
2435 if (nread != AFP_INFO_SIZE) {
2439 memcpy(data, afpinfo_buf, to_return);
2446 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2447 files_struct *fsp, void *data,
2448 size_t n, off_t offset)
2450 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2453 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2454 files_struct *fsp, void *data,
2455 size_t n, off_t offset)
2457 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2460 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2461 files_struct *fsp, void *data,
2462 size_t n, off_t offset)
2464 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2465 struct adouble *ad = NULL;
2468 if (fio == NULL || fio->ad_fsp == NULL) {
2469 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2474 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2476 DBG_ERR("ad_fget [%s] failed [%s]\n",
2477 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2481 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2482 offset + ad_getentryoff(ad, ADEID_RFORK));
2488 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2489 files_struct *fsp, void *data,
2490 size_t n, off_t offset)
2492 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2500 switch (fio->config->rsrc) {
2501 case FRUIT_RSRC_STREAM:
2502 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2505 case FRUIT_RSRC_ADFILE:
2506 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2509 case FRUIT_RSRC_XATTR:
2510 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2514 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2521 static ssize_t fruit_pread(vfs_handle_struct *handle,
2522 files_struct *fsp, void *data,
2523 size_t n, off_t offset)
2525 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2528 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2529 fsp_str_dbg(fsp), (intmax_t)offset, n);
2532 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2535 if (fio->type == ADOUBLE_META) {
2536 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2538 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2541 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2545 static bool fruit_must_handle_aio_stream(struct fio *fio)
2551 if (fio->type == ADOUBLE_META) {
2555 if ((fio->type == ADOUBLE_RSRC) &&
2556 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2564 struct fruit_pread_state {
2566 struct vfs_aio_state vfs_aio_state;
2569 static void fruit_pread_done(struct tevent_req *subreq);
2571 static struct tevent_req *fruit_pread_send(
2572 struct vfs_handle_struct *handle,
2573 TALLOC_CTX *mem_ctx,
2574 struct tevent_context *ev,
2575 struct files_struct *fsp,
2577 size_t n, off_t offset)
2579 struct tevent_req *req = NULL;
2580 struct tevent_req *subreq = NULL;
2581 struct fruit_pread_state *state = NULL;
2582 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2584 req = tevent_req_create(mem_ctx, &state,
2585 struct fruit_pread_state);
2590 if (fruit_must_handle_aio_stream(fio)) {
2591 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2592 if (state->nread != n) {
2593 if (state->nread != -1) {
2596 tevent_req_error(req, errno);
2597 return tevent_req_post(req, ev);
2599 tevent_req_done(req);
2600 return tevent_req_post(req, ev);
2603 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2605 if (tevent_req_nomem(req, subreq)) {
2606 return tevent_req_post(req, ev);
2608 tevent_req_set_callback(subreq, fruit_pread_done, req);
2612 static void fruit_pread_done(struct tevent_req *subreq)
2614 struct tevent_req *req = tevent_req_callback_data(
2615 subreq, struct tevent_req);
2616 struct fruit_pread_state *state = tevent_req_data(
2617 req, struct fruit_pread_state);
2619 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2620 TALLOC_FREE(subreq);
2622 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2625 tevent_req_done(req);
2628 static ssize_t fruit_pread_recv(struct tevent_req *req,
2629 struct vfs_aio_state *vfs_aio_state)
2631 struct fruit_pread_state *state = tevent_req_data(
2632 req, struct fruit_pread_state);
2634 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2638 *vfs_aio_state = state->vfs_aio_state;
2639 return state->nread;
2642 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2643 files_struct *fsp, const void *data,
2644 size_t n, off_t offset)
2646 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2652 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2653 fsp_str_dbg(fsp), (intmax_t)offset, n);
2660 struct vfs_open_how how = {
2661 .flags = fio->flags, .mode = fio->mode,
2663 int fd = fsp_get_pathref_fd(fsp);
2665 ret = vfs_fake_fd_close(fd);
2666 fsp_set_fd(fsp, -1);
2668 DBG_ERR("Close [%s] failed: %s\n",
2669 fsp_str_dbg(fsp), strerror(errno));
2673 fd = SMB_VFS_NEXT_OPENAT(handle,
2674 NULL, /* opening a stream */
2679 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2680 fsp_str_dbg(fsp), strerror(errno));
2683 fsp_set_fd(fsp, fd);
2684 fio->fake_fd = false;
2687 ai = afpinfo_unpack(talloc_tos(), data);
2692 if (ai_empty_finderinfo(ai)) {
2694 * Writing an all 0 blob to the metadata stream results in the
2695 * stream being removed on a macOS server. This ensures we
2696 * behave the same and it verified by the "delete AFP_AfpInfo by
2697 * writing all 0" test.
2699 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2701 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2706 ok = set_delete_on_close(
2709 handle->conn->session_info->security_token,
2710 handle->conn->session_info->unix_token);
2712 DBG_ERR("set_delete_on_close on [%s] failed\n",
2719 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2720 if (nwritten != n) {
2727 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2728 files_struct *fsp, const void *data,
2729 size_t n, off_t offset)
2731 struct adouble *ad = NULL;
2737 ai = afpinfo_unpack(talloc_tos(), data);
2742 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2744 ad = ad_init(talloc_tos(), ADOUBLE_META);
2749 p = ad_get_entry(ad, ADEID_FINDERI);
2751 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2756 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2758 ret = ad_fset(handle, ad, fsp);
2760 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2767 if (!ai_empty_finderinfo(ai)) {
2772 * Writing an all 0 blob to the metadata stream results in the stream
2773 * being removed on a macOS server. This ensures we behave the same and
2774 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2777 ok = set_delete_on_close(
2780 handle->conn->session_info->security_token,
2781 handle->conn->session_info->unix_token);
2783 DBG_ERR("set_delete_on_close on [%s] failed\n",
2791 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2792 files_struct *fsp, const void *data,
2793 size_t n, off_t offset)
2795 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2797 uint8_t buf[AFP_INFO_SIZE];
2803 DBG_ERR("Failed to fetch fsp extension");
2812 if (offset != 0 && n < 60) {
2817 cmp = memcmp(data, "AFP", 3);
2823 if (n <= AFP_OFF_FinderInfo) {
2825 * Nothing to do here really, just return
2833 if (to_copy > AFP_INFO_SIZE) {
2834 to_copy = AFP_INFO_SIZE;
2836 memcpy(buf, data, to_copy);
2839 if (to_write != AFP_INFO_SIZE) {
2840 to_write = AFP_INFO_SIZE;
2843 switch (fio->config->meta) {
2844 case FRUIT_META_STREAM:
2845 nwritten = fruit_pwrite_meta_stream(handle,
2852 case FRUIT_META_NETATALK:
2853 nwritten = fruit_pwrite_meta_netatalk(handle,
2861 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2865 if (nwritten != to_write) {
2870 * Return the requested amount, verified against macOS SMB server
2875 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2876 files_struct *fsp, const void *data,
2877 size_t n, off_t offset)
2879 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2882 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2883 files_struct *fsp, const void *data,
2884 size_t n, off_t offset)
2886 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2889 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2890 files_struct *fsp, const void *data,
2891 size_t n, off_t offset)
2893 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2894 struct adouble *ad = NULL;
2898 if (fio == NULL || fio->ad_fsp == NULL) {
2899 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2904 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2906 DBG_ERR("ad_fget [%s] failed [%s]\n",
2907 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2911 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2912 offset + ad_getentryoff(ad, ADEID_RFORK));
2913 if (nwritten != n) {
2914 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2915 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2920 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2921 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2922 ret = ad_fset(handle, ad, fio->ad_fsp);
2924 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2934 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2935 files_struct *fsp, const void *data,
2936 size_t n, off_t offset)
2938 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2942 DBG_ERR("Failed to fetch fsp extension");
2946 switch (fio->config->rsrc) {
2947 case FRUIT_RSRC_STREAM:
2948 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2951 case FRUIT_RSRC_ADFILE:
2952 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2955 case FRUIT_RSRC_XATTR:
2956 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2960 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2967 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2968 files_struct *fsp, const void *data,
2969 size_t n, off_t offset)
2971 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2974 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2975 fsp_str_dbg(fsp), (intmax_t)offset, n);
2978 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2981 if (fio->type == ADOUBLE_META) {
2982 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2984 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2987 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2991 struct fruit_pwrite_state {
2993 struct vfs_aio_state vfs_aio_state;
2996 static void fruit_pwrite_done(struct tevent_req *subreq);
2998 static struct tevent_req *fruit_pwrite_send(
2999 struct vfs_handle_struct *handle,
3000 TALLOC_CTX *mem_ctx,
3001 struct tevent_context *ev,
3002 struct files_struct *fsp,
3004 size_t n, off_t offset)
3006 struct tevent_req *req = NULL;
3007 struct tevent_req *subreq = NULL;
3008 struct fruit_pwrite_state *state = NULL;
3009 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3011 req = tevent_req_create(mem_ctx, &state,
3012 struct fruit_pwrite_state);
3017 if (fruit_must_handle_aio_stream(fio)) {
3018 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3019 if (state->nwritten != n) {
3020 if (state->nwritten != -1) {
3023 tevent_req_error(req, errno);
3024 return tevent_req_post(req, ev);
3026 tevent_req_done(req);
3027 return tevent_req_post(req, ev);
3030 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3032 if (tevent_req_nomem(req, subreq)) {
3033 return tevent_req_post(req, ev);
3035 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3039 static void fruit_pwrite_done(struct tevent_req *subreq)
3041 struct tevent_req *req = tevent_req_callback_data(
3042 subreq, struct tevent_req);
3043 struct fruit_pwrite_state *state = tevent_req_data(
3044 req, struct fruit_pwrite_state);
3046 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3047 TALLOC_FREE(subreq);
3049 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3052 tevent_req_done(req);
3055 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3056 struct vfs_aio_state *vfs_aio_state)
3058 struct fruit_pwrite_state *state = tevent_req_data(
3059 req, struct fruit_pwrite_state);
3061 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3065 *vfs_aio_state = state->vfs_aio_state;
3066 return state->nwritten;
3070 * Helper to stat/lstat the base file of an smb_fname.
3072 static int fruit_stat_base(vfs_handle_struct *handle,
3073 struct smb_filename *smb_fname,
3076 char *tmp_stream_name;
3079 tmp_stream_name = smb_fname->stream_name;
3080 smb_fname->stream_name = NULL;
3082 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3084 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3086 smb_fname->stream_name = tmp_stream_name;
3088 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3089 smb_fname->base_name,
3090 (uintmax_t)smb_fname->st.st_ex_dev,
3091 (uintmax_t)smb_fname->st.st_ex_ino);
3095 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3096 struct smb_filename *smb_fname,
3102 ret = fruit_stat_base(handle, smb_fname, false);
3107 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3110 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3112 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3115 smb_fname->st.st_ex_ino = ino;
3120 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3121 struct smb_filename *smb_fname,
3124 struct adouble *ad = NULL;
3126 /* Populate the stat struct with info from the base file. */
3127 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3131 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3133 DBG_INFO("fruit_stat_meta %s: %s\n",
3134 smb_fname_str_dbg(smb_fname), strerror(errno));
3140 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3141 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3142 smb_fname->stream_name);
3146 static int fruit_stat_meta(vfs_handle_struct *handle,
3147 struct smb_filename *smb_fname,
3150 struct fruit_config_data *config = NULL;
3153 SMB_VFS_HANDLE_GET_DATA(handle, config,
3154 struct fruit_config_data, return -1);
3156 switch (config->meta) {
3157 case FRUIT_META_STREAM:
3158 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3161 case FRUIT_META_NETATALK:
3162 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3166 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3173 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3174 struct smb_filename *smb_fname,
3177 struct adouble *ad = NULL;
3180 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3186 /* Populate the stat struct with info from the base file. */
3187 ret = fruit_stat_base(handle, smb_fname, follow_links);
3193 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3194 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3195 smb_fname->stream_name);
3200 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3201 struct smb_filename *smb_fname,
3207 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3209 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3215 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3216 struct smb_filename *smb_fname,
3219 #ifdef HAVE_ATTROPEN
3223 /* Populate the stat struct with info from the base file. */
3224 ret = fruit_stat_base(handle, smb_fname, follow_links);
3229 fd = attropen(smb_fname->base_name,
3230 AFPRESOURCE_EA_NETATALK,
3236 ret = sys_fstat(fd, &smb_fname->st, false);
3239 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3240 AFPRESOURCE_EA_NETATALK);
3246 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3247 smb_fname->stream_name);
3257 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3258 struct smb_filename *smb_fname,
3261 struct fruit_config_data *config = NULL;
3264 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3266 SMB_VFS_HANDLE_GET_DATA(handle, config,
3267 struct fruit_config_data, return -1);
3269 switch (config->rsrc) {
3270 case FRUIT_RSRC_STREAM:
3271 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3274 case FRUIT_RSRC_XATTR:
3275 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3278 case FRUIT_RSRC_ADFILE:
3279 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3283 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3290 static int fruit_stat(vfs_handle_struct *handle,
3291 struct smb_filename *smb_fname)
3295 DEBUG(10, ("fruit_stat called for %s\n",
3296 smb_fname_str_dbg(smb_fname)));
3298 if (!is_named_stream(smb_fname)) {
3299 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3301 update_btime(handle, smb_fname);
3307 * Note if lp_posix_paths() is true, we can never
3308 * get here as is_ntfs_stream_smb_fname() is
3309 * always false. So we never need worry about
3310 * not following links here.
3313 if (is_afpinfo_stream(smb_fname->stream_name)) {
3314 rc = fruit_stat_meta(handle, smb_fname, true);
3315 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3316 rc = fruit_stat_rsrc(handle, smb_fname, true);
3318 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3322 update_btime(handle, smb_fname);
3323 smb_fname->st.st_ex_mode &= ~S_IFMT;
3324 smb_fname->st.st_ex_mode |= S_IFREG;
3325 smb_fname->st.st_ex_blocks =
3326 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3331 static int fruit_lstat(vfs_handle_struct *handle,
3332 struct smb_filename *smb_fname)
3336 DEBUG(10, ("fruit_lstat called for %s\n",
3337 smb_fname_str_dbg(smb_fname)));
3339 if (!is_named_stream(smb_fname)) {
3340 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3342 update_btime(handle, smb_fname);
3347 if (is_afpinfo_stream(smb_fname->stream_name)) {
3348 rc = fruit_stat_meta(handle, smb_fname, false);
3349 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3350 rc = fruit_stat_rsrc(handle, smb_fname, false);
3352 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3356 update_btime(handle, smb_fname);
3357 smb_fname->st.st_ex_mode &= ~S_IFMT;
3358 smb_fname->st.st_ex_mode |= S_IFREG;
3359 smb_fname->st.st_ex_blocks =
3360 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3365 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3367 SMB_STRUCT_STAT *sbuf)
3369 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3370 struct smb_filename smb_fname;
3379 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3384 *sbuf = fsp->base_fsp->fsp_name->st;
3385 sbuf->st_ex_size = AFP_INFO_SIZE;
3386 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3390 smb_fname = (struct smb_filename) {
3391 .base_name = fsp->fsp_name->base_name,
3392 .twrp = fsp->fsp_name->twrp,
3395 ret = fruit_stat_base(handle, &smb_fname, false);
3399 *sbuf = smb_fname.st;
3401 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3403 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3408 sbuf->st_ex_ino = ino;
3412 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3414 SMB_STRUCT_STAT *sbuf)
3418 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3423 *sbuf = fsp->base_fsp->fsp_name->st;
3424 sbuf->st_ex_size = AFP_INFO_SIZE;
3425 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3430 static int fruit_fstat_meta(vfs_handle_struct *handle,
3432 SMB_STRUCT_STAT *sbuf,
3437 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3439 switch (fio->config->meta) {
3440 case FRUIT_META_STREAM:
3441 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3444 case FRUIT_META_NETATALK:
3445 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3449 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3453 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3457 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3459 SMB_STRUCT_STAT *sbuf)
3461 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3464 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3466 SMB_STRUCT_STAT *sbuf)
3468 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3471 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3473 SMB_STRUCT_STAT *sbuf)
3475 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3476 struct adouble *ad = NULL;
3479 if (fio == NULL || fio->ad_fsp == NULL) {
3480 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3485 /* Populate the stat struct with info from the base file. */
3486 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3491 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3493 DBG_ERR("ad_fget [%s] failed [%s]\n",
3494 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3498 *sbuf = fsp->base_fsp->fsp_name->st;
3499 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3500 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3506 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3507 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3511 switch (fio->config->rsrc) {
3512 case FRUIT_RSRC_STREAM:
3513 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3516 case FRUIT_RSRC_ADFILE:
3517 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3520 case FRUIT_RSRC_XATTR:
3521 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3525 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3532 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3533 SMB_STRUCT_STAT *sbuf)
3535 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3539 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3542 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3544 if (fio->type == ADOUBLE_META) {
3545 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3547 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3551 sbuf->st_ex_mode &= ~S_IFMT;
3552 sbuf->st_ex_mode |= S_IFREG;
3553 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3556 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3557 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3561 static NTSTATUS delete_invalid_meta_stream(
3562 vfs_handle_struct *handle,
3563 const struct smb_filename *smb_fname,
3564 TALLOC_CTX *mem_ctx,
3565 unsigned int *pnum_streams,
3566 struct stream_struct **pstreams,
3569 struct smb_filename *sname = NULL;
3574 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3576 return NT_STATUS_INTERNAL_ERROR;
3580 return NT_STATUS_OK;
3583 status = synthetic_pathref(talloc_tos(),
3584 handle->conn->cwd_fsp,
3585 smb_fname->base_name,
3586 AFPINFO_STREAM_NAME,
3591 if (!NT_STATUS_IS_OK(status)) {
3592 return NT_STATUS_NO_MEMORY;
3595 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3596 handle->conn->cwd_fsp,
3600 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3602 return map_nt_error_from_unix(errno);
3606 return NT_STATUS_OK;
3609 static NTSTATUS fruit_streaminfo_meta_stream(
3610 vfs_handle_struct *handle,
3611 struct files_struct *fsp,
3612 const struct smb_filename *smb_fname,
3613 TALLOC_CTX *mem_ctx,
3614 unsigned int *pnum_streams,
3615 struct stream_struct **pstreams)
3617 struct stream_struct *stream = *pstreams;
3618 unsigned int num_streams = *pnum_streams;
3621 for (i = 0; i < num_streams; i++) {
3622 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3627 if (i == num_streams) {
3628 return NT_STATUS_OK;
3631 if (stream[i].size != AFP_INFO_SIZE) {
3632 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3633 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3635 return delete_invalid_meta_stream(handle,
3644 return NT_STATUS_OK;
3647 static NTSTATUS fruit_streaminfo_meta_netatalk(
3648 vfs_handle_struct *handle,
3649 struct files_struct *fsp,
3650 const struct smb_filename *smb_fname,
3651 TALLOC_CTX *mem_ctx,
3652 unsigned int *pnum_streams,
3653 struct stream_struct **pstreams)
3655 struct stream_struct *stream = *pstreams;
3656 unsigned int num_streams = *pnum_streams;
3657 struct adouble *ad = NULL;
3662 /* Remove the Netatalk xattr from the list */
3663 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3664 ":" NETATALK_META_XATTR ":$DATA");
3666 return NT_STATUS_NO_MEMORY;
3670 * Check if there's a AFPINFO_STREAM from the VFS streams
3671 * backend and if yes, remove it from the list
3673 for (i = 0; i < num_streams; i++) {
3674 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3679 if (i < num_streams) {
3680 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3681 smb_fname_str_dbg(smb_fname));
3683 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3686 return NT_STATUS_INTERNAL_ERROR;
3690 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3692 return NT_STATUS_OK;
3695 is_fi_empty = ad_empty_finderinfo(ad);
3699 return NT_STATUS_OK;
3702 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3703 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3704 smb_roundup(handle->conn, AFP_INFO_SIZE));
3706 return NT_STATUS_NO_MEMORY;
3709 return NT_STATUS_OK;
3712 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3713 struct files_struct *fsp,
3714 const struct smb_filename *smb_fname,
3715 TALLOC_CTX *mem_ctx,
3716 unsigned int *pnum_streams,
3717 struct stream_struct **pstreams)
3719 struct fruit_config_data *config = NULL;
3722 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3723 return NT_STATUS_INTERNAL_ERROR);
3725 switch (config->meta) {
3726 case FRUIT_META_NETATALK:
3727 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3728 mem_ctx, pnum_streams,
3732 case FRUIT_META_STREAM:
3733 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3734 mem_ctx, pnum_streams,
3739 return NT_STATUS_INTERNAL_ERROR;
3745 static NTSTATUS fruit_streaminfo_rsrc_stream(
3746 vfs_handle_struct *handle,
3747 struct files_struct *fsp,
3748 const struct smb_filename *smb_fname,
3749 TALLOC_CTX *mem_ctx,
3750 unsigned int *pnum_streams,
3751 struct stream_struct **pstreams)
3755 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3757 DBG_ERR("Filtering resource stream failed\n");
3758 return NT_STATUS_INTERNAL_ERROR;
3760 return NT_STATUS_OK;
3763 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3764 vfs_handle_struct *handle,
3765 struct files_struct *fsp,
3766 const struct smb_filename *smb_fname,
3767 TALLOC_CTX *mem_ctx,
3768 unsigned int *pnum_streams,
3769 struct stream_struct **pstreams)
3773 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3775 DBG_ERR("Filtering resource stream failed\n");
3776 return NT_STATUS_INTERNAL_ERROR;
3778 return NT_STATUS_OK;
3781 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3782 vfs_handle_struct *handle,
3783 struct files_struct *fsp,
3784 const struct smb_filename *smb_fname,
3785 TALLOC_CTX *mem_ctx,
3786 unsigned int *pnum_streams,
3787 struct stream_struct **pstreams)
3789 struct stream_struct *stream = *pstreams;
3790 unsigned int num_streams = *pnum_streams;
3791 struct adouble *ad = NULL;
3797 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3798 * and if yes, remove it from the list
3800 for (i = 0; i < num_streams; i++) {
3801 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3806 if (i < num_streams) {
3807 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3808 smb_fname_str_dbg(smb_fname));
3810 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3811 AFPRESOURCE_STREAM);
3813 return NT_STATUS_INTERNAL_ERROR;
3817 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3819 return NT_STATUS_OK;
3822 rlen = ad_getentrylen(ad, ADEID_RFORK);
3826 return NT_STATUS_OK;
3829 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3830 AFPRESOURCE_STREAM_NAME, rlen,
3831 smb_roundup(handle->conn, rlen));
3833 return NT_STATUS_NO_MEMORY;
3836 return NT_STATUS_OK;
3839 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3840 struct files_struct *fsp,
3841 const struct smb_filename *smb_fname,
3842 TALLOC_CTX *mem_ctx,
3843 unsigned int *pnum_streams,
3844 struct stream_struct **pstreams)
3846 struct fruit_config_data *config = NULL;
3849 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3850 return NT_STATUS_INTERNAL_ERROR);
3852 switch (config->rsrc) {
3853 case FRUIT_RSRC_STREAM:
3854 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3855 mem_ctx, pnum_streams,
3859 case FRUIT_RSRC_XATTR:
3860 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3861 mem_ctx, pnum_streams,
3865 case FRUIT_RSRC_ADFILE:
3866 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3867 mem_ctx, pnum_streams,
3872 return NT_STATUS_INTERNAL_ERROR;
3878 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3879 struct stream_struct **pstreams)
3881 unsigned num_streams = *pnum_streams;
3882 struct stream_struct *streams = *pstreams;
3885 if (!global_fruit_config.nego_aapl) {
3889 while (i < num_streams) {
3890 struct smb_filename smb_fname = (struct smb_filename) {
3891 .stream_name = streams[i].name,
3894 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3895 || streams[i].size > 0)
3901 streams[i] = streams[num_streams - 1];
3905 *pnum_streams = num_streams;
3908 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
3909 struct files_struct *fsp,
3910 TALLOC_CTX *mem_ctx,
3911 unsigned int *pnum_streams,
3912 struct stream_struct **pstreams)
3914 struct fruit_config_data *config = NULL;
3915 const struct smb_filename *smb_fname = NULL;
3918 smb_fname = fsp->fsp_name;
3920 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3921 return NT_STATUS_UNSUCCESSFUL);
3923 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3925 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
3926 pnum_streams, pstreams);
3927 if (!NT_STATUS_IS_OK(status)) {
3931 fruit_filter_empty_streams(pnum_streams, pstreams);
3933 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3934 mem_ctx, pnum_streams, pstreams);
3935 if (!NT_STATUS_IS_OK(status)) {
3939 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3940 mem_ctx, pnum_streams, pstreams);
3941 if (!NT_STATUS_IS_OK(status)) {
3945 return NT_STATUS_OK;
3948 static int fruit_fntimes(vfs_handle_struct *handle,
3950 struct smb_file_time *ft)
3953 struct adouble *ad = NULL;
3954 struct fruit_config_data *config = NULL;
3956 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3959 if ((config->meta != FRUIT_META_NETATALK) ||
3960 is_omit_timespec(&ft->create_time))
3962 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3965 DBG_DEBUG("set btime for %s to %s\n", fsp_str_dbg(fsp),
3966 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
3968 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3973 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3974 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3976 rc = ad_fset(handle, ad, fsp);
3982 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
3985 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3988 static int fruit_fallocate(struct vfs_handle_struct *handle,
3989 struct files_struct *fsp,
3994 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3997 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4000 /* Let the pwrite code path handle it. */
4005 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4006 struct files_struct *fsp,
4009 #ifdef HAVE_ATTROPEN
4010 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4015 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4016 struct files_struct *fsp,
4019 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4021 struct adouble *ad = NULL;
4024 if (fio == NULL || fio->ad_fsp == NULL) {
4025 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4030 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4032 DBG_ERR("ad_fget [%s] failed [%s]\n",
4033 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4037 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4039 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4045 ad_setentrylen(ad, ADEID_RFORK, offset);
4047 rc = ad_fset(handle, ad, fio->ad_fsp);
4049 DBG_ERR("ad_fset [%s] failed [%s]\n",
4050 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4059 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4060 struct files_struct *fsp,
4063 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4066 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4067 struct files_struct *fsp,
4070 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4074 DBG_ERR("Failed to fetch fsp extension");
4078 switch (fio->config->rsrc) {
4079 case FRUIT_RSRC_XATTR:
4080 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4083 case FRUIT_RSRC_ADFILE:
4084 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4087 case FRUIT_RSRC_STREAM:
4088 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4092 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4100 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4101 struct files_struct *fsp,
4105 DBG_WARNING("ftruncate %s to %jd",
4106 fsp_str_dbg(fsp), (intmax_t)offset);
4107 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4112 /* OS X returns success but does nothing */
4113 DBG_INFO("ignoring ftruncate %s to %jd\n",
4114 fsp_str_dbg(fsp), (intmax_t)offset);
4118 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4119 struct files_struct *fsp,
4122 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4125 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4129 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4132 if (fio->type == ADOUBLE_META) {
4133 ret = fruit_ftruncate_meta(handle, fsp, offset);
4135 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4138 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4142 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4143 struct smb_request *req,
4144 struct files_struct *dirfsp,
4145 struct smb_filename *smb_fname,
4146 uint32_t access_mask,
4147 uint32_t share_access,
4148 uint32_t create_disposition,
4149 uint32_t create_options,
4150 uint32_t file_attributes,
4151 uint32_t oplock_request,
4152 const struct smb2_lease *lease,
4153 uint64_t allocation_size,
4154 uint32_t private_flags,
4155 struct security_descriptor *sd,
4156 struct ea_list *ea_list,
4157 files_struct **result,
4159 const struct smb2_create_blobs *in_context_blobs,
4160 struct smb2_create_blobs *out_context_blobs)
4163 struct fruit_config_data *config = NULL;
4164 files_struct *fsp = NULL;
4165 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4168 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4169 if (!NT_STATUS_IS_OK(status)) {
4173 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4174 return NT_STATUS_UNSUCCESSFUL);
4176 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4177 uint32_t conv_flags = 0;
4179 if (config->wipe_intentionally_left_blank_rfork) {
4180 conv_flags |= AD_CONV_WIPE_BLANK;
4182 if (config->delete_empty_adfiles) {
4183 conv_flags |= AD_CONV_DELETE;
4186 ret = ad_convert(handle,
4188 macos_string_replace_map,
4191 DBG_ERR("ad_convert() failed\n");
4192 return NT_STATUS_UNSUCCESSFUL;
4196 status = SMB_VFS_NEXT_CREATE_FILE(
4197 handle, req, dirfsp, smb_fname,
4198 access_mask, share_access,
4199 create_disposition, create_options,
4200 file_attributes, oplock_request,
4202 allocation_size, private_flags,
4203 sd, ea_list, result,
4204 pinfo, in_context_blobs, out_context_blobs);
4205 if (!NT_STATUS_IS_OK(status)) {
4211 if (global_fruit_config.nego_aapl) {
4212 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4214 * Enable POSIX directory rename behaviour
4216 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4221 * If this is a plain open for existing files, opening an 0
4222 * byte size resource fork MUST fail with
4223 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4225 * Cf the vfs_fruit torture tests in test_rfork_create().
4227 if (global_fruit_config.nego_aapl &&
4228 create_disposition == FILE_OPEN &&
4229 smb_fname->st.st_ex_size == 0 &&
4230 is_named_stream(smb_fname))
4232 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4236 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4240 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4241 (fsp->op != NULL) &&
4242 !fsp->fsp_flags.is_pathref)
4244 status = fruit_check_access(
4248 if (!NT_STATUS_IS_OK(status)) {
4256 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4259 close_file_free(req, &fsp, ERROR_CLOSE);
4266 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4267 struct files_struct *fsp,
4268 TALLOC_CTX *mem_ctx,
4269 struct readdir_attr_data **pattr_data)
4271 struct fruit_config_data *config = NULL;
4272 struct readdir_attr_data *attr_data;
4273 uint32_t conv_flags = 0;
4277 SMB_VFS_HANDLE_GET_DATA(handle, config,
4278 struct fruit_config_data,
4279 return NT_STATUS_UNSUCCESSFUL);
4281 if (!global_fruit_config.nego_aapl) {
4282 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4288 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4290 if (config->wipe_intentionally_left_blank_rfork) {
4291 conv_flags |= AD_CONV_WIPE_BLANK;
4293 if (config->delete_empty_adfiles) {
4294 conv_flags |= AD_CONV_DELETE;
4297 ret = ad_convert(handle,
4299 macos_string_replace_map,
4302 DBG_ERR("ad_convert() failed\n");
4303 return NT_STATUS_UNSUCCESSFUL;
4306 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4307 if (*pattr_data == NULL) {
4308 return NT_STATUS_NO_MEMORY;
4310 attr_data = *pattr_data;
4311 attr_data->type = RDATTR_AAPL;
4314 * Mac metadata: compressed FinderInfo, resource fork length
4317 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4318 if (!NT_STATUS_IS_OK(status)) {
4320 * Error handling is tricky: if we return failure from
4321 * this function, the corresponding directory entry
4322 * will to be passed to the client, so we really just
4323 * want to error out on fatal errors.
4325 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4333 if (config->unix_info_enabled) {
4334 attr_data->attr_data.aapl.unix_mode =
4335 fsp->fsp_name->st.st_ex_mode;
4341 if (!config->readdir_attr_max_access) {
4342 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4344 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4347 SEC_FLAG_MAXIMUM_ALLOWED,
4348 &attr_data->attr_data.aapl.max_access);
4349 if (!NT_STATUS_IS_OK(status)) {
4354 return NT_STATUS_OK;
4357 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4359 TALLOC_FREE(*pattr_data);
4363 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4365 uint32_t security_info,
4366 TALLOC_CTX *mem_ctx,
4367 struct security_descriptor **ppdesc)
4370 struct security_ace ace;
4372 struct fruit_config_data *config;
4374 SMB_VFS_HANDLE_GET_DATA(handle, config,
4375 struct fruit_config_data,
4376 return NT_STATUS_UNSUCCESSFUL);
4378 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4380 if (!NT_STATUS_IS_OK(status)) {
4385 * Add MS NFS style ACEs with uid, gid and mode
4387 if (!global_fruit_config.nego_aapl) {
4388 return NT_STATUS_OK;
4390 if (!config->unix_info_enabled) {
4391 return NT_STATUS_OK;
4394 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4395 status = remove_virtual_nfs_aces(*ppdesc);
4396 if (!NT_STATUS_IS_OK(status)) {
4397 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4401 /* MS NFS style mode */
4402 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4403 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4404 status = security_descriptor_dacl_add(*ppdesc, &ace);
4405 if (!NT_STATUS_IS_OK(status)) {
4406 DEBUG(1,("failed to add MS NFS style ACE\n"));
4410 /* MS NFS style uid */
4411 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4412 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4413 status = security_descriptor_dacl_add(*ppdesc, &ace);
4414 if (!NT_STATUS_IS_OK(status)) {
4415 DEBUG(1,("failed to add MS NFS style ACE\n"));
4419 /* MS NFS style gid */
4420 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4421 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4422 status = security_descriptor_dacl_add(*ppdesc, &ace);
4423 if (!NT_STATUS_IS_OK(status)) {
4424 DEBUG(1,("failed to add MS NFS style ACE\n"));
4428 return NT_STATUS_OK;
4431 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4433 uint32_t security_info_sent,
4434 const struct security_descriptor *orig_psd)
4438 mode_t ms_nfs_mode = 0;
4440 struct security_descriptor *psd = NULL;
4441 uint32_t orig_num_aces = 0;
4443 if (orig_psd->dacl != NULL) {
4444 orig_num_aces = orig_psd->dacl->num_aces;
4447 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4449 return NT_STATUS_NO_MEMORY;
4452 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4454 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4455 if (!NT_STATUS_IS_OK(status)) {
4456 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4462 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4463 * sent/present flags correctly now we've removed them.
4466 if (orig_num_aces != 0) {
4468 * Are there any ACE's left ?
4470 if (psd->dacl->num_aces == 0) {
4471 /* No - clear the DACL sent/present flags. */
4472 security_info_sent &= ~SECINFO_DACL;
4473 psd->type &= ~SEC_DESC_DACL_PRESENT;
4477 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4478 if (!NT_STATUS_IS_OK(status)) {
4479 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4485 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4487 DBG_WARNING("%s, result: %d, %04o error %s\n",
4490 (unsigned)ms_nfs_mode,
4492 status = map_nt_error_from_unix(errno);
4499 return NT_STATUS_OK;
4502 static struct vfs_offload_ctx *fruit_offload_ctx;
4504 struct fruit_offload_read_state {
4505 struct vfs_handle_struct *handle;
4506 struct tevent_context *ev;
4514 static void fruit_offload_read_done(struct tevent_req *subreq);
4516 static struct tevent_req *fruit_offload_read_send(
4517 TALLOC_CTX *mem_ctx,
4518 struct tevent_context *ev,
4519 struct vfs_handle_struct *handle,
4526 struct tevent_req *req = NULL;
4527 struct tevent_req *subreq = NULL;
4528 struct fruit_offload_read_state *state = NULL;
4530 req = tevent_req_create(mem_ctx, &state,
4531 struct fruit_offload_read_state);
4535 *state = (struct fruit_offload_read_state) {
4542 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4543 fsctl, ttl, offset, to_copy);
4544 if (tevent_req_nomem(subreq, req)) {
4545 return tevent_req_post(req, ev);
4547 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4551 static void fruit_offload_read_done(struct tevent_req *subreq)
4553 struct tevent_req *req = tevent_req_callback_data(
4554 subreq, struct tevent_req);
4555 struct fruit_offload_read_state *state = tevent_req_data(
4556 req, struct fruit_offload_read_state);
4559 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4565 TALLOC_FREE(subreq);
4566 if (tevent_req_nterror(req, status)) {
4570 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4571 tevent_req_done(req);
4575 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4576 &fruit_offload_ctx);
4577 if (tevent_req_nterror(req, status)) {
4581 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4584 if (tevent_req_nterror(req, status)) {
4588 tevent_req_done(req);
4592 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4593 struct vfs_handle_struct *handle,
4594 TALLOC_CTX *mem_ctx,
4599 struct fruit_offload_read_state *state = tevent_req_data(
4600 req, struct fruit_offload_read_state);
4603 if (tevent_req_is_nterror(req, &status)) {
4604 tevent_req_received(req);
4608 *flags = state->flags;
4609 *xferlen = state->xferlen;
4610 token->length = state->token.length;
4611 token->data = talloc_move(mem_ctx, &state->token.data);
4613 tevent_req_received(req);
4614 return NT_STATUS_OK;
4617 struct fruit_offload_write_state {
4618 struct vfs_handle_struct *handle;
4620 struct files_struct *src_fsp;
4621 struct files_struct *dst_fsp;
4625 static void fruit_offload_write_done(struct tevent_req *subreq);
4626 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4627 TALLOC_CTX *mem_ctx,
4628 struct tevent_context *ev,
4631 off_t transfer_offset,
4632 struct files_struct *dest_fsp,
4636 struct tevent_req *req, *subreq;
4637 struct fruit_offload_write_state *state;
4639 struct fruit_config_data *config;
4640 off_t src_off = transfer_offset;
4641 files_struct *src_fsp = NULL;
4642 off_t to_copy = num;
4643 bool copyfile_enabled = false;
4645 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4646 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4648 SMB_VFS_HANDLE_GET_DATA(handle, config,
4649 struct fruit_config_data,
4652 req = tevent_req_create(mem_ctx, &state,
4653 struct fruit_offload_write_state);
4657 state->handle = handle;
4658 state->dst_fsp = dest_fsp;
4661 case FSCTL_SRV_COPYCHUNK:
4662 case FSCTL_SRV_COPYCHUNK_WRITE:
4663 copyfile_enabled = config->copyfile_enabled;
4670 * Check if this a OS X copyfile style copychunk request with
4671 * a requested chunk count of 0 that was translated to a
4672 * offload_write_send VFS call overloading the parameters src_off
4673 * = dest_off = num = 0.
4675 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4676 status = vfs_offload_token_db_fetch_fsp(
4677 fruit_offload_ctx, token, &src_fsp);
4678 if (tevent_req_nterror(req, status)) {
4679 return tevent_req_post(req, ev);
4681 state->src_fsp = src_fsp;
4683 status = vfs_stat_fsp(src_fsp);
4684 if (tevent_req_nterror(req, status)) {
4685 return tevent_req_post(req, ev);
4688 to_copy = src_fsp->fsp_name->st.st_ex_size;
4689 state->is_copyfile = true;
4692 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4701 if (tevent_req_nomem(subreq, req)) {
4702 return tevent_req_post(req, ev);
4705 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4709 static void fruit_offload_write_done(struct tevent_req *subreq)
4711 struct tevent_req *req = tevent_req_callback_data(
4712 subreq, struct tevent_req);
4713 struct fruit_offload_write_state *state = tevent_req_data(
4714 req, struct fruit_offload_write_state);
4716 unsigned int num_streams = 0;
4717 struct stream_struct *streams = NULL;
4719 struct smb_filename *src_fname_tmp = NULL;
4720 struct smb_filename *dst_fname_tmp = NULL;
4722 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4725 TALLOC_FREE(subreq);
4726 if (tevent_req_nterror(req, status)) {
4730 if (!state->is_copyfile) {
4731 tevent_req_done(req);
4736 * Now copy all remaining streams. We know the share supports
4737 * streams, because we're in vfs_fruit. We don't do this async
4738 * because streams are few and small.
4740 status = vfs_fstreaminfo(state->src_fsp,
4741 req, &num_streams, &streams);
4742 if (tevent_req_nterror(req, status)) {
4746 if (num_streams == 1) {
4747 /* There is always one stream, ::$DATA. */
4748 tevent_req_done(req);
4752 for (i = 0; i < num_streams; i++) {
4753 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4754 __func__, streams[i].name, (size_t)streams[i].size));
4756 src_fname_tmp = synthetic_smb_fname(
4758 state->src_fsp->fsp_name->base_name,
4761 state->src_fsp->fsp_name->twrp,
4762 state->src_fsp->fsp_name->flags);
4763 if (tevent_req_nomem(src_fname_tmp, req)) {
4767 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4768 TALLOC_FREE(src_fname_tmp);
4772 dst_fname_tmp = synthetic_smb_fname(
4774 state->dst_fsp->fsp_name->base_name,
4777 state->dst_fsp->fsp_name->twrp,
4778 state->dst_fsp->fsp_name->flags);
4779 if (tevent_req_nomem(dst_fname_tmp, req)) {
4780 TALLOC_FREE(src_fname_tmp);
4784 status = copy_file(req,
4785 state->handle->conn,
4789 if (!NT_STATUS_IS_OK(status)) {
4790 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4791 smb_fname_str_dbg(src_fname_tmp),
4792 smb_fname_str_dbg(dst_fname_tmp),
4793 nt_errstr(status)));
4794 TALLOC_FREE(src_fname_tmp);
4795 TALLOC_FREE(dst_fname_tmp);
4796 tevent_req_nterror(req, status);
4800 TALLOC_FREE(src_fname_tmp);
4801 TALLOC_FREE(dst_fname_tmp);
4804 TALLOC_FREE(streams);
4805 TALLOC_FREE(src_fname_tmp);
4806 TALLOC_FREE(dst_fname_tmp);
4807 tevent_req_done(req);
4810 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4811 struct tevent_req *req,
4814 struct fruit_offload_write_state *state = tevent_req_data(
4815 req, struct fruit_offload_write_state);
4818 if (tevent_req_is_nterror(req, &status)) {
4819 DEBUG(1, ("server side copy chunk failed: %s\n",
4820 nt_errstr(status)));
4822 tevent_req_received(req);
4826 *copied = state->copied;
4827 tevent_req_received(req);
4829 return NT_STATUS_OK;
4832 static char *fruit_get_bandsize_line(char **lines, int numlines)
4835 static bool re_initialized = false;
4839 if (!re_initialized) {
4840 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4844 re_initialized = true;
4847 for (i = 0; i < numlines; i++) {
4848 regmatch_t matches[1];
4850 ret = regexec(&re, lines[i], 1, matches, 0);
4853 * Check if the match was on the last line, sa we want
4854 * the subsequent line.
4856 if (i + 1 == numlines) {
4859 return lines[i + 1];
4861 if (ret != REG_NOMATCH) {
4869 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4872 static bool re_initialized = false;
4873 regmatch_t matches[2];
4878 if (!re_initialized) {
4881 "<integer>\\([[:digit:]]*\\)</integer>$",
4886 re_initialized = true;
4889 ret = regexec(&re, line, 2, matches, 0);
4891 DBG_ERR("regex failed [%s]\n", line);
4895 line[matches[1].rm_eo] = '\0';
4897 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4901 *_band_size = (size_t)band_size;
4906 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4907 * "band-size" key and value.
4909 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4913 #define INFO_PLIST_MAX_SIZE 64*1024
4915 struct smb_filename *smb_fname = NULL;
4916 files_struct *fsp = NULL;
4917 uint8_t *file_data = NULL;
4918 char **lines = NULL;
4919 char *band_size_line = NULL;
4920 size_t plist_file_size;
4927 plist = talloc_asprintf(talloc_tos(),
4929 handle->conn->connectpath,
4931 if (plist == NULL) {
4936 smb_fname = synthetic_smb_fname(talloc_tos(),
4942 if (smb_fname == NULL) {
4947 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4949 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4954 plist_file_size = smb_fname->st.st_ex_size;
4956 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4957 DBG_INFO("%s is too large, ignoring\n", plist);
4962 status = SMB_VFS_NEXT_CREATE_FILE(
4966 smb_fname, /* fname */
4967 FILE_GENERIC_READ, /* access_mask */
4968 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4969 FILE_OPEN, /* create_disposition */
4970 0, /* create_options */
4971 0, /* file_attributes */
4972 INTERNAL_OPEN_ONLY, /* oplock_request */
4974 0, /* allocation_size */
4975 0, /* private_flags */
4980 NULL, NULL); /* create context */
4981 if (!NT_STATUS_IS_OK(status)) {
4982 DBG_INFO("Opening [%s] failed [%s]\n",
4983 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4988 file_data = talloc_zero_array(talloc_tos(),
4990 plist_file_size + 1);
4991 if (file_data == NULL) {
4996 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4997 if (nread != plist_file_size) {
4998 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4999 fsp_str_dbg(fsp), nread, plist_file_size);
5005 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5006 if (!NT_STATUS_IS_OK(status)) {
5007 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5012 lines = file_lines_parse((char *)file_data,
5016 if (lines == NULL) {
5021 band_size_line = fruit_get_bandsize_line(lines, numlines);
5022 if (band_size_line == NULL) {
5023 DBG_ERR("Didn't find band-size key in [%s]\n",
5024 smb_fname_str_dbg(smb_fname));
5029 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5031 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5035 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5039 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5040 if (!NT_STATUS_IS_OK(status)) {
5041 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5045 TALLOC_FREE(smb_fname);
5046 TALLOC_FREE(file_data);
5051 struct fruit_disk_free_state {
5055 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5060 struct smb_filename *bands_dir = NULL;
5061 struct smb_Dir *dir_hnd = NULL;
5062 const char *dname = NULL;
5063 char *talloced = NULL;
5068 path = talloc_asprintf(talloc_tos(),
5070 handle->conn->connectpath,
5076 bands_dir = synthetic_smb_fname(talloc_tos(),
5083 if (bands_dir == NULL) {
5087 status = OpenDir(talloc_tos(),
5093 if (!NT_STATUS_IS_OK(status)) {
5094 TALLOC_FREE(bands_dir);
5095 errno = map_errno_from_nt_status(status);
5101 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5104 if (ISDOT(dname) || ISDOTDOT(dname)) {
5109 TALLOC_FREE(dir_hnd);
5111 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5113 TALLOC_FREE(bands_dir);
5119 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5120 struct fruit_disk_free_state *state,
5125 size_t sparsebundle_strlen = strlen("sparsebundle");
5126 size_t bandsize = 0;
5130 p = strstr(name, "sparsebundle");
5135 if (p[sparsebundle_strlen] != '\0') {
5139 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5141 ok = fruit_get_bandsize(handle, name, &bandsize);
5144 * Beware of race conditions: this may be an uninitialized
5145 * Info.plist that a client is just creating. We don't want let
5146 * this to trigger complete failure.
5148 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5152 ok = fruit_get_num_bands(handle, name, &nbands);
5155 * Beware of race conditions: this may be a backup sparsebundle
5156 * in an early stage lacking a bands subdirectory. We don't want
5157 * let this to trigger complete failure.
5159 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5164 * Arithmetic on 32-bit systems may cause overflow, depending on
5165 * size_t precision. First we check its unlikely, then we
5166 * force the precision into target off_t, then we check that
5167 * the total did not overflow either.
5169 if (bandsize > SIZE_MAX/nbands) {
5170 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5174 tm_size = (off_t)bandsize * (off_t)nbands;
5176 if (state->total_size + tm_size < state->total_size) {
5177 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5182 state->total_size += tm_size;
5184 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5185 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5191 * Calculate used size of a TimeMachine volume
5193 * This assumes that the volume is used only for TimeMachine.
5195 * - readdir(basedir of share), then
5196 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5197 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5198 * - count band files in "\1.sparsebundle/bands/"
5199 * - calculate used size of all bands: band_count * band_size
5201 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5202 const struct smb_filename *smb_fname,
5207 struct fruit_config_data *config = NULL;
5208 struct fruit_disk_free_state state = {0};
5209 struct smb_Dir *dir_hnd = NULL;
5210 const char *dname = NULL;
5211 char *talloced = NULL;
5218 SMB_VFS_HANDLE_GET_DATA(handle, config,
5219 struct fruit_config_data,
5222 if (!config->time_machine ||
5223 config->time_machine_max_size == 0)
5225 return SMB_VFS_NEXT_DISK_FREE(handle,
5232 status = OpenDir(talloc_tos(),
5238 if (!NT_STATUS_IS_OK(status)) {
5239 errno = map_errno_from_nt_status(status);
5243 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5246 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5248 TALLOC_FREE(talloced);
5249 TALLOC_FREE(dir_hnd);
5252 TALLOC_FREE(talloced);
5255 TALLOC_FREE(dir_hnd);
5257 dsize = config->time_machine_max_size / 512;
5258 dfree = dsize - (state.total_size / 512);
5259 if (dfree > dsize) {
5269 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5270 const SMB_STRUCT_STAT *psbuf)
5272 struct fruit_config_data *config = NULL;
5274 SMB_VFS_HANDLE_GET_DATA(handle, config,
5275 struct fruit_config_data,
5278 if (global_fruit_config.nego_aapl &&
5279 config->aapl_zero_file_id)
5284 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5287 static struct vfs_fn_pointers vfs_fruit_fns = {
5288 .connect_fn = fruit_connect,
5289 .disk_free_fn = fruit_disk_free,
5291 /* File operations */
5292 .fchmod_fn = fruit_fchmod,
5293 .unlinkat_fn = fruit_unlinkat,
5294 .renameat_fn = fruit_renameat,
5295 .openat_fn = fruit_openat,
5296 .close_fn = fruit_close,
5297 .pread_fn = fruit_pread,
5298 .pwrite_fn = fruit_pwrite,
5299 .pread_send_fn = fruit_pread_send,
5300 .pread_recv_fn = fruit_pread_recv,
5301 .pwrite_send_fn = fruit_pwrite_send,
5302 .pwrite_recv_fn = fruit_pwrite_recv,
5303 .stat_fn = fruit_stat,
5304 .lstat_fn = fruit_lstat,
5305 .fstat_fn = fruit_fstat,
5306 .fstreaminfo_fn = fruit_fstreaminfo,
5307 .fntimes_fn = fruit_fntimes,
5308 .ftruncate_fn = fruit_ftruncate,
5309 .fallocate_fn = fruit_fallocate,
5310 .create_file_fn = fruit_create_file,
5311 .freaddir_attr_fn = fruit_freaddir_attr,
5312 .offload_read_send_fn = fruit_offload_read_send,
5313 .offload_read_recv_fn = fruit_offload_read_recv,
5314 .offload_write_send_fn = fruit_offload_write_send,
5315 .offload_write_recv_fn = fruit_offload_write_recv,
5316 .fs_file_id_fn = fruit_fs_file_id,
5318 /* NT ACL operations */
5319 .fget_nt_acl_fn = fruit_fget_nt_acl,
5320 .fset_nt_acl_fn = fruit_fset_nt_acl,
5324 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5326 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5328 if (!NT_STATUS_IS_OK(ret)) {
5332 vfs_fruit_debug_level = debug_add_class("fruit");
5333 if (vfs_fruit_debug_level == -1) {
5334 vfs_fruit_debug_level = DBGC_VFS;
5335 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5338 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5339 "vfs_fruit_init","fruit",vfs_fruit_debug_level));