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 int open_flags = flags & ~O_CREAT;
1419 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1421 SMB_VFS_HANDLE_GET_DATA(handle, config,
1422 struct fruit_config_data, return -1);
1424 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1425 fio->handle = handle;
1427 fio->type = ADOUBLE_META;
1428 fio->config = config;
1430 fd = SMB_VFS_NEXT_OPENAT(handle,
1440 if (!(flags & O_CREAT)) {
1441 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1447 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1451 fio->fake_fd = true;
1458 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1459 const struct files_struct *dirfsp,
1460 const struct smb_filename *smb_fname,
1465 struct fruit_config_data *config = NULL;
1466 struct fio *fio = NULL;
1467 struct adouble *ad = NULL;
1468 bool meta_exists = false;
1471 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1474 * We know this is a stream open, so fsp->base_fsp must
1477 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1478 SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1480 ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1487 if (!meta_exists && !(flags & O_CREAT)) {
1497 SMB_VFS_HANDLE_GET_DATA(handle, config,
1498 struct fruit_config_data, return -1);
1500 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1501 fio->handle = handle;
1503 fio->type = ADOUBLE_META;
1504 fio->config = config;
1505 fio->fake_fd = true;
1512 static int fruit_open_meta(vfs_handle_struct *handle,
1513 const struct files_struct *dirfsp,
1514 const struct smb_filename *smb_fname,
1515 files_struct *fsp, int flags, mode_t mode)
1518 struct fruit_config_data *config = NULL;
1520 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1522 SMB_VFS_HANDLE_GET_DATA(handle, config,
1523 struct fruit_config_data, return -1);
1525 switch (config->meta) {
1526 case FRUIT_META_STREAM:
1527 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1531 case FRUIT_META_NETATALK:
1532 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1537 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1541 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1546 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1547 const struct files_struct *dirfsp,
1548 const struct smb_filename *smb_fname,
1554 struct fruit_config_data *config = NULL;
1555 struct files_struct *ad_fsp = NULL;
1556 struct fio *fio = NULL;
1557 struct fio *ref_fio = NULL;
1561 SMB_VFS_HANDLE_GET_DATA(handle, config,
1562 struct fruit_config_data, return -1);
1564 if ((!(flags & O_CREAT)) &&
1565 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1567 /* sorry, but directories don't have a resource fork */
1574 * We return a fake_fd to the vfs modules above,
1575 * while we open an internal backend fsp for the
1576 * '._' file for the next vfs modules.
1578 * Note that adouble_open_from_base_fsp() recurses
1579 * into fruit_openat(), but it'll just pass to
1580 * the next module as just opens a flat file on
1590 status = adouble_open_from_base_fsp(fsp->conn->cwd_fsp,
1596 if (!NT_STATUS_IS_OK(status)) {
1597 errno = map_errno_from_nt_status(status);
1603 * Now we need to glue both handles together,
1604 * so that they automatically detach each other
1607 fio = fruit_get_complete_fio(handle, fsp);
1609 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1615 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1617 fio_ref_destroy_fn);
1618 if (ref_fio == NULL) {
1619 int saved_errno = errno;
1621 file_free(NULL, ad_fsp);
1623 errno = saved_errno;
1628 SMB_ASSERT(ref_fio->fsp == NULL);
1629 ref_fio->handle = handle;
1630 ref_fio->fsp = ad_fsp;
1631 ref_fio->type = ADOUBLE_RSRC;
1632 ref_fio->config = config;
1633 ref_fio->real_fio = fio;
1634 SMB_ASSERT(fio->ad_fsp == NULL);
1635 fio->ad_fsp = ad_fsp;
1636 fio->fake_fd = true;
1640 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1642 int saved_errno = errno;
1644 vfs_fake_fd_close(fd);
1646 errno = saved_errno;
1652 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1653 const struct files_struct *dirfsp,
1654 const struct smb_filename *smb_fname,
1659 #ifdef HAVE_ATTROPEN
1663 * As there's no attropenat() this is only going to work with AT_FDCWD.
1665 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1667 fd = attropen(smb_fname->base_name,
1668 AFPRESOURCE_EA_NETATALK,
1683 static int fruit_open_rsrc(vfs_handle_struct *handle,
1684 const struct files_struct *dirfsp,
1685 const struct smb_filename *smb_fname,
1686 files_struct *fsp, int flags, mode_t mode)
1689 struct fruit_config_data *config = NULL;
1690 struct fio *fio = NULL;
1692 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1694 SMB_VFS_HANDLE_GET_DATA(handle, config,
1695 struct fruit_config_data, return -1);
1697 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1698 fio->handle = handle;
1700 fio->type = ADOUBLE_RSRC;
1701 fio->config = config;
1703 switch (config->rsrc) {
1704 case FRUIT_RSRC_STREAM:
1705 fd = SMB_VFS_NEXT_OPENAT(handle,
1713 case FRUIT_RSRC_ADFILE:
1714 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1718 case FRUIT_RSRC_XATTR:
1719 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1724 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1729 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1738 static int fruit_openat(vfs_handle_struct *handle,
1739 const struct files_struct *dirfsp,
1740 const struct smb_filename *smb_fname,
1747 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1749 if (!is_named_stream(smb_fname)) {
1750 return SMB_VFS_NEXT_OPENAT(handle,
1758 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1760 if (is_afpinfo_stream(smb_fname->stream_name)) {
1761 fd = fruit_open_meta(handle,
1767 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1768 fd = fruit_open_rsrc(handle,
1775 fd = SMB_VFS_NEXT_OPENAT(handle,
1783 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1785 /* Prevent reopen optimisation */
1786 fsp->fsp_flags.have_proc_fds = false;
1790 static int fruit_close_meta(vfs_handle_struct *handle,
1794 struct fruit_config_data *config = NULL;
1796 SMB_VFS_HANDLE_GET_DATA(handle, config,
1797 struct fruit_config_data, return -1);
1799 switch (config->meta) {
1800 case FRUIT_META_STREAM:
1802 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1807 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1808 fsp_set_fd(fsp, -1);
1810 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1814 case FRUIT_META_NETATALK:
1815 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1816 fsp_set_fd(fsp, -1);
1820 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1828 static int fruit_close_rsrc(vfs_handle_struct *handle,
1832 struct fruit_config_data *config = NULL;
1834 SMB_VFS_HANDLE_GET_DATA(handle, config,
1835 struct fruit_config_data, return -1);
1837 switch (config->rsrc) {
1838 case FRUIT_RSRC_STREAM:
1839 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1842 case FRUIT_RSRC_ADFILE:
1844 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1848 fio_close_ad_fsp(fio);
1849 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1850 fsp_set_fd(fsp, -1);
1854 case FRUIT_RSRC_XATTR:
1855 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1856 fsp_set_fd(fsp, -1);
1860 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1867 static int fruit_close(vfs_handle_struct *handle,
1873 fd = fsp_get_pathref_fd(fsp);
1875 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1877 if (!fsp_is_alternate_stream(fsp)) {
1878 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1881 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1882 ret = fruit_close_meta(handle, fsp);
1883 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1884 ret = fruit_close_rsrc(handle, fsp);
1886 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1892 static int fruit_renameat(struct vfs_handle_struct *handle,
1893 files_struct *srcfsp,
1894 const struct smb_filename *smb_fname_src,
1895 files_struct *dstfsp,
1896 const struct smb_filename *smb_fname_dst)
1899 struct fruit_config_data *config = NULL;
1900 struct smb_filename *src_adp_smb_fname = NULL;
1901 struct smb_filename *dst_adp_smb_fname = NULL;
1903 SMB_VFS_HANDLE_GET_DATA(handle, config,
1904 struct fruit_config_data, return -1);
1906 if (!VALID_STAT(smb_fname_src->st)) {
1907 DBG_ERR("Need valid stat for [%s]\n",
1908 smb_fname_str_dbg(smb_fname_src));
1912 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1921 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1922 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1927 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1932 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1937 DBG_DEBUG("%s -> %s\n",
1938 smb_fname_str_dbg(src_adp_smb_fname),
1939 smb_fname_str_dbg(dst_adp_smb_fname));
1941 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1946 if (errno == ENOENT) {
1951 TALLOC_FREE(src_adp_smb_fname);
1952 TALLOC_FREE(dst_adp_smb_fname);
1956 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1957 struct files_struct *dirfsp,
1958 const struct smb_filename *smb_fname)
1960 return SMB_VFS_NEXT_UNLINKAT(handle,
1966 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1967 const struct smb_filename *smb_fname)
1969 SMB_ASSERT(smb_fname->fsp != NULL);
1970 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1971 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1972 AFPINFO_EA_NETATALK);
1975 static int fruit_unlink_meta(vfs_handle_struct *handle,
1976 struct files_struct *dirfsp,
1977 const struct smb_filename *smb_fname)
1979 struct fruit_config_data *config = NULL;
1982 SMB_VFS_HANDLE_GET_DATA(handle, config,
1983 struct fruit_config_data, return -1);
1985 switch (config->meta) {
1986 case FRUIT_META_STREAM:
1987 rc = fruit_unlink_meta_stream(handle,
1992 case FRUIT_META_NETATALK:
1993 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1997 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2004 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2005 struct files_struct *dirfsp,
2006 const struct smb_filename *smb_fname,
2011 if (!force_unlink) {
2012 struct smb_filename *full_fname = NULL;
2016 * TODO: use SMB_VFS_STATX() once we have it.
2019 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2022 if (full_fname == NULL) {
2027 * 0 byte resource fork streams are not listed by
2028 * vfs_streaminfo, as a result stream cleanup/deletion of file
2029 * deletion doesn't remove the resourcefork stream.
2032 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2034 TALLOC_FREE(full_fname);
2035 DBG_ERR("stat [%s] failed [%s]\n",
2036 smb_fname_str_dbg(full_fname), strerror(errno));
2040 size = full_fname->st.st_ex_size;
2041 TALLOC_FREE(full_fname);
2044 /* OS X ignores resource fork stream delete requests */
2049 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2053 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2060 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2061 struct files_struct *dirfsp,
2062 const struct smb_filename *smb_fname,
2066 struct adouble *ad = NULL;
2067 struct smb_filename *adp_smb_fname = NULL;
2069 if (!force_unlink) {
2070 struct smb_filename *full_fname = NULL;
2072 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2075 if (full_fname == NULL) {
2079 ad = ad_get(talloc_tos(), handle, full_fname,
2081 TALLOC_FREE(full_fname);
2089 * 0 byte resource fork streams are not listed by
2090 * vfs_streaminfo, as a result stream cleanup/deletion of file
2091 * deletion doesn't remove the resourcefork stream.
2094 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2095 /* OS X ignores resource fork stream delete requests */
2103 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2108 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2112 TALLOC_FREE(adp_smb_fname);
2113 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2120 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2121 const struct smb_filename *smb_fname,
2125 * OS X ignores resource fork stream delete requests, so nothing to do
2126 * here. Removing the file will remove the xattr anyway, so we don't
2127 * have to take care of removing 0 byte resource forks that could be
2133 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2134 struct files_struct *dirfsp,
2135 const struct smb_filename *smb_fname,
2138 struct fruit_config_data *config = NULL;
2141 SMB_VFS_HANDLE_GET_DATA(handle, config,
2142 struct fruit_config_data, return -1);
2144 switch (config->rsrc) {
2145 case FRUIT_RSRC_STREAM:
2146 rc = fruit_unlink_rsrc_stream(handle,
2152 case FRUIT_RSRC_ADFILE:
2153 rc = fruit_unlink_rsrc_adouble(handle,
2159 case FRUIT_RSRC_XATTR:
2160 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2164 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2171 static int fruit_fchmod(vfs_handle_struct *handle,
2172 struct files_struct *fsp,
2176 struct fruit_config_data *config = NULL;
2177 struct smb_filename *smb_fname_adp = NULL;
2178 const struct smb_filename *smb_fname = NULL;
2181 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2186 smb_fname = fsp->fsp_name;
2187 SMB_VFS_HANDLE_GET_DATA(handle, config,
2188 struct fruit_config_data, return -1);
2190 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2194 if (!VALID_STAT(smb_fname->st)) {
2198 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2202 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2207 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2209 if (!NT_STATUS_IS_OK(status)) {
2210 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2211 if (NT_STATUS_EQUAL(status,
2212 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2220 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2222 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2223 if (errno == ENOENT) {
2227 TALLOC_FREE(smb_fname_adp);
2231 static int fruit_unlinkat(vfs_handle_struct *handle,
2232 struct files_struct *dirfsp,
2233 const struct smb_filename *smb_fname,
2236 struct fruit_config_data *config = NULL;
2237 struct smb_filename *rsrc_smb_fname = NULL;
2240 if (flags & AT_REMOVEDIR) {
2241 return SMB_VFS_NEXT_UNLINKAT(handle,
2247 SMB_VFS_HANDLE_GET_DATA(handle, config,
2248 struct fruit_config_data, return -1);
2250 if (is_afpinfo_stream(smb_fname->stream_name)) {
2251 return fruit_unlink_meta(handle,
2254 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2255 return fruit_unlink_rsrc(handle,
2259 } else if (is_named_stream(smb_fname)) {
2260 return SMB_VFS_NEXT_UNLINKAT(handle,
2264 } else if (is_adouble_file(smb_fname->base_name)) {
2265 return SMB_VFS_NEXT_UNLINKAT(handle,
2272 * A request to delete the base file. Because 0 byte resource
2273 * fork streams are not listed by fruit_streaminfo,
2274 * delete_all_streams() can't remove 0 byte resource fork
2275 * streams, so we have to cleanup this here.
2277 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2278 smb_fname->base_name,
2279 AFPRESOURCE_STREAM_NAME,
2283 if (rsrc_smb_fname == NULL) {
2287 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2288 if ((ret != 0) && (errno != ENOENT)) {
2289 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2290 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2291 TALLOC_FREE(rsrc_smb_fname);
2294 TALLOC_FREE(rsrc_smb_fname);
2296 return SMB_VFS_NEXT_UNLINKAT(handle,
2302 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2303 files_struct *fsp, void *data,
2304 size_t n, off_t offset)
2306 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2310 if ((fio == NULL) || fio->fake_fd) {
2314 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2315 if (nread == -1 || nread == n) {
2319 DBG_ERR("Removing [%s] after short read [%zd]\n",
2320 fsp_str_dbg(fsp), nread);
2322 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2327 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2335 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2336 files_struct *fsp, void *data,
2337 size_t n, off_t offset)
2340 struct adouble *ad = NULL;
2341 char afpinfo_buf[AFP_INFO_SIZE];
2345 ai = afpinfo_new(talloc_tos());
2350 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2356 p = ad_get_entry(ad, ADEID_FINDERI);
2358 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2363 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2365 nread = afpinfo_pack(ai, afpinfo_buf);
2366 if (nread != AFP_INFO_SIZE) {
2371 memcpy(data, afpinfo_buf, n);
2379 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2380 files_struct *fsp, void *data,
2381 size_t n, off_t offset)
2383 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2388 * OS X has a off-by-1 error in the offset calculation, so we're
2389 * bug compatible here. It won't hurt, as any relevant real
2390 * world read requests from the AFP_AfpInfo stream will be
2391 * offset=0 n=60. offset is ignored anyway, see below.
2393 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2398 DBG_ERR("Failed to fetch fsp extension");
2402 /* Yes, macOS always reads from offset 0 */
2404 to_return = MIN(n, AFP_INFO_SIZE);
2406 switch (fio->config->meta) {
2407 case FRUIT_META_STREAM:
2408 nread = fruit_pread_meta_stream(handle, fsp, data,
2412 case FRUIT_META_NETATALK:
2413 nread = fruit_pread_meta_adouble(handle, fsp, data,
2418 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2422 if (nread == -1 && fio->fake_fd) {
2424 char afpinfo_buf[AFP_INFO_SIZE];
2426 ai = afpinfo_new(talloc_tos());
2431 nread = afpinfo_pack(ai, afpinfo_buf);
2433 if (nread != AFP_INFO_SIZE) {
2437 memcpy(data, afpinfo_buf, to_return);
2444 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2445 files_struct *fsp, void *data,
2446 size_t n, off_t offset)
2448 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2451 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2452 files_struct *fsp, void *data,
2453 size_t n, off_t offset)
2455 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2458 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2459 files_struct *fsp, void *data,
2460 size_t n, off_t offset)
2462 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2463 struct adouble *ad = NULL;
2466 if (fio == NULL || fio->ad_fsp == NULL) {
2467 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2472 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2474 DBG_ERR("ad_fget [%s] failed [%s]\n",
2475 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2479 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2480 offset + ad_getentryoff(ad, ADEID_RFORK));
2486 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2487 files_struct *fsp, void *data,
2488 size_t n, off_t offset)
2490 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2498 switch (fio->config->rsrc) {
2499 case FRUIT_RSRC_STREAM:
2500 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2503 case FRUIT_RSRC_ADFILE:
2504 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2507 case FRUIT_RSRC_XATTR:
2508 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2512 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2519 static ssize_t fruit_pread(vfs_handle_struct *handle,
2520 files_struct *fsp, void *data,
2521 size_t n, off_t offset)
2523 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2526 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2527 fsp_str_dbg(fsp), (intmax_t)offset, n);
2530 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2533 if (fio->type == ADOUBLE_META) {
2534 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2536 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2539 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2543 static bool fruit_must_handle_aio_stream(struct fio *fio)
2549 if (fio->type == ADOUBLE_META) {
2553 if ((fio->type == ADOUBLE_RSRC) &&
2554 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2562 struct fruit_pread_state {
2564 struct vfs_aio_state vfs_aio_state;
2567 static void fruit_pread_done(struct tevent_req *subreq);
2569 static struct tevent_req *fruit_pread_send(
2570 struct vfs_handle_struct *handle,
2571 TALLOC_CTX *mem_ctx,
2572 struct tevent_context *ev,
2573 struct files_struct *fsp,
2575 size_t n, off_t offset)
2577 struct tevent_req *req = NULL;
2578 struct tevent_req *subreq = NULL;
2579 struct fruit_pread_state *state = NULL;
2580 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2582 req = tevent_req_create(mem_ctx, &state,
2583 struct fruit_pread_state);
2588 if (fruit_must_handle_aio_stream(fio)) {
2589 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2590 if (state->nread != n) {
2591 if (state->nread != -1) {
2594 tevent_req_error(req, errno);
2595 return tevent_req_post(req, ev);
2597 tevent_req_done(req);
2598 return tevent_req_post(req, ev);
2601 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2603 if (tevent_req_nomem(req, subreq)) {
2604 return tevent_req_post(req, ev);
2606 tevent_req_set_callback(subreq, fruit_pread_done, req);
2610 static void fruit_pread_done(struct tevent_req *subreq)
2612 struct tevent_req *req = tevent_req_callback_data(
2613 subreq, struct tevent_req);
2614 struct fruit_pread_state *state = tevent_req_data(
2615 req, struct fruit_pread_state);
2617 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2618 TALLOC_FREE(subreq);
2620 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2623 tevent_req_done(req);
2626 static ssize_t fruit_pread_recv(struct tevent_req *req,
2627 struct vfs_aio_state *vfs_aio_state)
2629 struct fruit_pread_state *state = tevent_req_data(
2630 req, struct fruit_pread_state);
2632 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2636 *vfs_aio_state = state->vfs_aio_state;
2637 return state->nread;
2640 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2641 files_struct *fsp, const void *data,
2642 size_t n, off_t offset)
2644 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2650 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2651 fsp_str_dbg(fsp), (intmax_t)offset, n);
2658 int fd = fsp_get_pathref_fd(fsp);
2660 ret = vfs_fake_fd_close(fd);
2661 fsp_set_fd(fsp, -1);
2663 DBG_ERR("Close [%s] failed: %s\n",
2664 fsp_str_dbg(fsp), strerror(errno));
2668 fd = SMB_VFS_NEXT_OPENAT(handle,
2675 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2676 fsp_str_dbg(fsp), strerror(errno));
2679 fsp_set_fd(fsp, fd);
2680 fio->fake_fd = false;
2683 ai = afpinfo_unpack(talloc_tos(), data);
2688 if (ai_empty_finderinfo(ai)) {
2690 * Writing an all 0 blob to the metadata stream results in the
2691 * stream being removed on a macOS server. This ensures we
2692 * behave the same and it verified by the "delete AFP_AfpInfo by
2693 * writing all 0" test.
2695 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2697 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2702 ok = set_delete_on_close(
2705 handle->conn->session_info->security_token,
2706 handle->conn->session_info->unix_token);
2708 DBG_ERR("set_delete_on_close on [%s] failed\n",
2715 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2716 if (nwritten != n) {
2723 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2724 files_struct *fsp, const void *data,
2725 size_t n, off_t offset)
2727 struct adouble *ad = NULL;
2733 ai = afpinfo_unpack(talloc_tos(), data);
2738 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2740 ad = ad_init(talloc_tos(), ADOUBLE_META);
2745 p = ad_get_entry(ad, ADEID_FINDERI);
2747 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2752 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2754 ret = ad_fset(handle, ad, fsp);
2756 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2763 if (!ai_empty_finderinfo(ai)) {
2768 * Writing an all 0 blob to the metadata stream results in the stream
2769 * being removed on a macOS server. This ensures we behave the same and
2770 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2773 ok = set_delete_on_close(
2776 handle->conn->session_info->security_token,
2777 handle->conn->session_info->unix_token);
2779 DBG_ERR("set_delete_on_close on [%s] failed\n",
2787 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2788 files_struct *fsp, const void *data,
2789 size_t n, off_t offset)
2791 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2793 uint8_t buf[AFP_INFO_SIZE];
2799 DBG_ERR("Failed to fetch fsp extension");
2808 if (offset != 0 && n < 60) {
2813 cmp = memcmp(data, "AFP", 3);
2819 if (n <= AFP_OFF_FinderInfo) {
2821 * Nothing to do here really, just return
2829 if (to_copy > AFP_INFO_SIZE) {
2830 to_copy = AFP_INFO_SIZE;
2832 memcpy(buf, data, to_copy);
2835 if (to_write != AFP_INFO_SIZE) {
2836 to_write = AFP_INFO_SIZE;
2839 switch (fio->config->meta) {
2840 case FRUIT_META_STREAM:
2841 nwritten = fruit_pwrite_meta_stream(handle,
2848 case FRUIT_META_NETATALK:
2849 nwritten = fruit_pwrite_meta_netatalk(handle,
2857 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2861 if (nwritten != to_write) {
2866 * Return the requested amount, verified against macOS SMB server
2871 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2872 files_struct *fsp, const void *data,
2873 size_t n, off_t offset)
2875 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2878 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2879 files_struct *fsp, const void *data,
2880 size_t n, off_t offset)
2882 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2885 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2886 files_struct *fsp, const void *data,
2887 size_t n, off_t offset)
2889 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2890 struct adouble *ad = NULL;
2894 if (fio == NULL || fio->ad_fsp == NULL) {
2895 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2900 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2902 DBG_ERR("ad_fget [%s] failed [%s]\n",
2903 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2907 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2908 offset + ad_getentryoff(ad, ADEID_RFORK));
2909 if (nwritten != n) {
2910 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2911 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2916 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2917 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2918 ret = ad_fset(handle, ad, fio->ad_fsp);
2920 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2930 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2931 files_struct *fsp, const void *data,
2932 size_t n, off_t offset)
2934 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2938 DBG_ERR("Failed to fetch fsp extension");
2942 switch (fio->config->rsrc) {
2943 case FRUIT_RSRC_STREAM:
2944 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2947 case FRUIT_RSRC_ADFILE:
2948 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2951 case FRUIT_RSRC_XATTR:
2952 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2956 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2963 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2964 files_struct *fsp, const void *data,
2965 size_t n, off_t offset)
2967 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2970 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2971 fsp_str_dbg(fsp), (intmax_t)offset, n);
2974 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2977 if (fio->type == ADOUBLE_META) {
2978 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2980 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2983 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2987 struct fruit_pwrite_state {
2989 struct vfs_aio_state vfs_aio_state;
2992 static void fruit_pwrite_done(struct tevent_req *subreq);
2994 static struct tevent_req *fruit_pwrite_send(
2995 struct vfs_handle_struct *handle,
2996 TALLOC_CTX *mem_ctx,
2997 struct tevent_context *ev,
2998 struct files_struct *fsp,
3000 size_t n, off_t offset)
3002 struct tevent_req *req = NULL;
3003 struct tevent_req *subreq = NULL;
3004 struct fruit_pwrite_state *state = NULL;
3005 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3007 req = tevent_req_create(mem_ctx, &state,
3008 struct fruit_pwrite_state);
3013 if (fruit_must_handle_aio_stream(fio)) {
3014 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3015 if (state->nwritten != n) {
3016 if (state->nwritten != -1) {
3019 tevent_req_error(req, errno);
3020 return tevent_req_post(req, ev);
3022 tevent_req_done(req);
3023 return tevent_req_post(req, ev);
3026 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3028 if (tevent_req_nomem(req, subreq)) {
3029 return tevent_req_post(req, ev);
3031 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3035 static void fruit_pwrite_done(struct tevent_req *subreq)
3037 struct tevent_req *req = tevent_req_callback_data(
3038 subreq, struct tevent_req);
3039 struct fruit_pwrite_state *state = tevent_req_data(
3040 req, struct fruit_pwrite_state);
3042 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3043 TALLOC_FREE(subreq);
3045 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3048 tevent_req_done(req);
3051 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3052 struct vfs_aio_state *vfs_aio_state)
3054 struct fruit_pwrite_state *state = tevent_req_data(
3055 req, struct fruit_pwrite_state);
3057 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3061 *vfs_aio_state = state->vfs_aio_state;
3062 return state->nwritten;
3066 * Helper to stat/lstat the base file of an smb_fname.
3068 static int fruit_stat_base(vfs_handle_struct *handle,
3069 struct smb_filename *smb_fname,
3072 char *tmp_stream_name;
3075 tmp_stream_name = smb_fname->stream_name;
3076 smb_fname->stream_name = NULL;
3078 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3080 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3082 smb_fname->stream_name = tmp_stream_name;
3084 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3085 smb_fname->base_name,
3086 (uintmax_t)smb_fname->st.st_ex_dev,
3087 (uintmax_t)smb_fname->st.st_ex_ino);
3091 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3092 struct smb_filename *smb_fname,
3098 ret = fruit_stat_base(handle, smb_fname, false);
3103 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3106 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3108 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3111 smb_fname->st.st_ex_ino = ino;
3116 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3117 struct smb_filename *smb_fname,
3120 struct adouble *ad = NULL;
3122 /* Populate the stat struct with info from the base file. */
3123 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3127 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3129 DBG_INFO("fruit_stat_meta %s: %s\n",
3130 smb_fname_str_dbg(smb_fname), strerror(errno));
3136 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3137 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3138 smb_fname->stream_name);
3142 static int fruit_stat_meta(vfs_handle_struct *handle,
3143 struct smb_filename *smb_fname,
3146 struct fruit_config_data *config = NULL;
3149 SMB_VFS_HANDLE_GET_DATA(handle, config,
3150 struct fruit_config_data, return -1);
3152 switch (config->meta) {
3153 case FRUIT_META_STREAM:
3154 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3157 case FRUIT_META_NETATALK:
3158 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3162 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3169 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3170 struct smb_filename *smb_fname,
3173 struct adouble *ad = NULL;
3176 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3182 /* Populate the stat struct with info from the base file. */
3183 ret = fruit_stat_base(handle, smb_fname, follow_links);
3189 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3190 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3191 smb_fname->stream_name);
3196 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3197 struct smb_filename *smb_fname,
3203 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3205 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3211 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3212 struct smb_filename *smb_fname,
3215 #ifdef HAVE_ATTROPEN
3219 /* Populate the stat struct with info from the base file. */
3220 ret = fruit_stat_base(handle, smb_fname, follow_links);
3225 fd = attropen(smb_fname->base_name,
3226 AFPRESOURCE_EA_NETATALK,
3232 ret = sys_fstat(fd, &smb_fname->st, false);
3235 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3236 AFPRESOURCE_EA_NETATALK);
3242 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3243 smb_fname->stream_name);
3253 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3254 struct smb_filename *smb_fname,
3257 struct fruit_config_data *config = NULL;
3260 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3262 SMB_VFS_HANDLE_GET_DATA(handle, config,
3263 struct fruit_config_data, return -1);
3265 switch (config->rsrc) {
3266 case FRUIT_RSRC_STREAM:
3267 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3270 case FRUIT_RSRC_XATTR:
3271 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3274 case FRUIT_RSRC_ADFILE:
3275 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3279 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3286 static int fruit_stat(vfs_handle_struct *handle,
3287 struct smb_filename *smb_fname)
3291 DEBUG(10, ("fruit_stat called for %s\n",
3292 smb_fname_str_dbg(smb_fname)));
3294 if (!is_named_stream(smb_fname)) {
3295 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3297 update_btime(handle, smb_fname);
3303 * Note if lp_posix_paths() is true, we can never
3304 * get here as is_ntfs_stream_smb_fname() is
3305 * always false. So we never need worry about
3306 * not following links here.
3309 if (is_afpinfo_stream(smb_fname->stream_name)) {
3310 rc = fruit_stat_meta(handle, smb_fname, true);
3311 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3312 rc = fruit_stat_rsrc(handle, smb_fname, true);
3314 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3318 update_btime(handle, smb_fname);
3319 smb_fname->st.st_ex_mode &= ~S_IFMT;
3320 smb_fname->st.st_ex_mode |= S_IFREG;
3321 smb_fname->st.st_ex_blocks =
3322 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3327 static int fruit_lstat(vfs_handle_struct *handle,
3328 struct smb_filename *smb_fname)
3332 DEBUG(10, ("fruit_lstat called for %s\n",
3333 smb_fname_str_dbg(smb_fname)));
3335 if (!is_named_stream(smb_fname)) {
3336 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3338 update_btime(handle, smb_fname);
3343 if (is_afpinfo_stream(smb_fname->stream_name)) {
3344 rc = fruit_stat_meta(handle, smb_fname, false);
3345 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3346 rc = fruit_stat_rsrc(handle, smb_fname, false);
3348 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3352 update_btime(handle, smb_fname);
3353 smb_fname->st.st_ex_mode &= ~S_IFMT;
3354 smb_fname->st.st_ex_mode |= S_IFREG;
3355 smb_fname->st.st_ex_blocks =
3356 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3361 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3363 SMB_STRUCT_STAT *sbuf)
3365 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3366 struct smb_filename smb_fname;
3375 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3380 *sbuf = fsp->base_fsp->fsp_name->st;
3381 sbuf->st_ex_size = AFP_INFO_SIZE;
3382 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3386 smb_fname = (struct smb_filename) {
3387 .base_name = fsp->fsp_name->base_name,
3388 .twrp = fsp->fsp_name->twrp,
3391 ret = fruit_stat_base(handle, &smb_fname, false);
3395 *sbuf = smb_fname.st;
3397 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3399 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3404 sbuf->st_ex_ino = ino;
3408 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3410 SMB_STRUCT_STAT *sbuf)
3414 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3419 *sbuf = fsp->base_fsp->fsp_name->st;
3420 sbuf->st_ex_size = AFP_INFO_SIZE;
3421 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3426 static int fruit_fstat_meta(vfs_handle_struct *handle,
3428 SMB_STRUCT_STAT *sbuf,
3433 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3435 switch (fio->config->meta) {
3436 case FRUIT_META_STREAM:
3437 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3440 case FRUIT_META_NETATALK:
3441 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3445 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3449 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3453 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3455 SMB_STRUCT_STAT *sbuf)
3457 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3460 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3462 SMB_STRUCT_STAT *sbuf)
3464 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3467 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3469 SMB_STRUCT_STAT *sbuf)
3471 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3472 struct adouble *ad = NULL;
3475 if (fio == NULL || fio->ad_fsp == NULL) {
3476 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3481 /* Populate the stat struct with info from the base file. */
3482 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3487 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3489 DBG_ERR("ad_fget [%s] failed [%s]\n",
3490 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3494 *sbuf = fsp->base_fsp->fsp_name->st;
3495 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3496 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3502 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3503 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3507 switch (fio->config->rsrc) {
3508 case FRUIT_RSRC_STREAM:
3509 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3512 case FRUIT_RSRC_ADFILE:
3513 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3516 case FRUIT_RSRC_XATTR:
3517 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3521 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3528 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3529 SMB_STRUCT_STAT *sbuf)
3531 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3535 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3538 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3540 if (fio->type == ADOUBLE_META) {
3541 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3543 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3547 sbuf->st_ex_mode &= ~S_IFMT;
3548 sbuf->st_ex_mode |= S_IFREG;
3549 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3552 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3553 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3557 static NTSTATUS delete_invalid_meta_stream(
3558 vfs_handle_struct *handle,
3559 const struct smb_filename *smb_fname,
3560 TALLOC_CTX *mem_ctx,
3561 unsigned int *pnum_streams,
3562 struct stream_struct **pstreams,
3565 struct smb_filename *sname = NULL;
3570 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3572 return NT_STATUS_INTERNAL_ERROR;
3576 return NT_STATUS_OK;
3579 status = synthetic_pathref(talloc_tos(),
3580 handle->conn->cwd_fsp,
3581 smb_fname->base_name,
3582 AFPINFO_STREAM_NAME,
3587 if (!NT_STATUS_IS_OK(status)) {
3588 return NT_STATUS_NO_MEMORY;
3591 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3592 handle->conn->cwd_fsp,
3596 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3598 return map_nt_error_from_unix(errno);
3602 return NT_STATUS_OK;
3605 static NTSTATUS fruit_streaminfo_meta_stream(
3606 vfs_handle_struct *handle,
3607 struct files_struct *fsp,
3608 const struct smb_filename *smb_fname,
3609 TALLOC_CTX *mem_ctx,
3610 unsigned int *pnum_streams,
3611 struct stream_struct **pstreams)
3613 struct stream_struct *stream = *pstreams;
3614 unsigned int num_streams = *pnum_streams;
3617 for (i = 0; i < num_streams; i++) {
3618 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3623 if (i == num_streams) {
3624 return NT_STATUS_OK;
3627 if (stream[i].size != AFP_INFO_SIZE) {
3628 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3629 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3631 return delete_invalid_meta_stream(handle,
3640 return NT_STATUS_OK;
3643 static NTSTATUS fruit_streaminfo_meta_netatalk(
3644 vfs_handle_struct *handle,
3645 struct files_struct *fsp,
3646 const struct smb_filename *smb_fname,
3647 TALLOC_CTX *mem_ctx,
3648 unsigned int *pnum_streams,
3649 struct stream_struct **pstreams)
3651 struct stream_struct *stream = *pstreams;
3652 unsigned int num_streams = *pnum_streams;
3653 struct adouble *ad = NULL;
3658 /* Remove the Netatalk xattr from the list */
3659 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3660 ":" NETATALK_META_XATTR ":$DATA");
3662 return NT_STATUS_NO_MEMORY;
3666 * Check if there's a AFPINFO_STREAM from the VFS streams
3667 * backend and if yes, remove it from the list
3669 for (i = 0; i < num_streams; i++) {
3670 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3675 if (i < num_streams) {
3676 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3677 smb_fname_str_dbg(smb_fname));
3679 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3682 return NT_STATUS_INTERNAL_ERROR;
3686 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3688 return NT_STATUS_OK;
3691 is_fi_empty = ad_empty_finderinfo(ad);
3695 return NT_STATUS_OK;
3698 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3699 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3700 smb_roundup(handle->conn, AFP_INFO_SIZE));
3702 return NT_STATUS_NO_MEMORY;
3705 return NT_STATUS_OK;
3708 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3709 struct files_struct *fsp,
3710 const struct smb_filename *smb_fname,
3711 TALLOC_CTX *mem_ctx,
3712 unsigned int *pnum_streams,
3713 struct stream_struct **pstreams)
3715 struct fruit_config_data *config = NULL;
3718 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3719 return NT_STATUS_INTERNAL_ERROR);
3721 switch (config->meta) {
3722 case FRUIT_META_NETATALK:
3723 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3724 mem_ctx, pnum_streams,
3728 case FRUIT_META_STREAM:
3729 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3730 mem_ctx, pnum_streams,
3735 return NT_STATUS_INTERNAL_ERROR;
3741 static NTSTATUS fruit_streaminfo_rsrc_stream(
3742 vfs_handle_struct *handle,
3743 struct files_struct *fsp,
3744 const struct smb_filename *smb_fname,
3745 TALLOC_CTX *mem_ctx,
3746 unsigned int *pnum_streams,
3747 struct stream_struct **pstreams)
3751 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3753 DBG_ERR("Filtering resource stream failed\n");
3754 return NT_STATUS_INTERNAL_ERROR;
3756 return NT_STATUS_OK;
3759 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3760 vfs_handle_struct *handle,
3761 struct files_struct *fsp,
3762 const struct smb_filename *smb_fname,
3763 TALLOC_CTX *mem_ctx,
3764 unsigned int *pnum_streams,
3765 struct stream_struct **pstreams)
3769 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3771 DBG_ERR("Filtering resource stream failed\n");
3772 return NT_STATUS_INTERNAL_ERROR;
3774 return NT_STATUS_OK;
3777 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3778 vfs_handle_struct *handle,
3779 struct files_struct *fsp,
3780 const struct smb_filename *smb_fname,
3781 TALLOC_CTX *mem_ctx,
3782 unsigned int *pnum_streams,
3783 struct stream_struct **pstreams)
3785 struct stream_struct *stream = *pstreams;
3786 unsigned int num_streams = *pnum_streams;
3787 struct adouble *ad = NULL;
3793 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3794 * and if yes, remove it from the list
3796 for (i = 0; i < num_streams; i++) {
3797 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3802 if (i < num_streams) {
3803 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3804 smb_fname_str_dbg(smb_fname));
3806 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3807 AFPRESOURCE_STREAM);
3809 return NT_STATUS_INTERNAL_ERROR;
3813 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3815 return NT_STATUS_OK;
3818 rlen = ad_getentrylen(ad, ADEID_RFORK);
3822 return NT_STATUS_OK;
3825 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3826 AFPRESOURCE_STREAM_NAME, rlen,
3827 smb_roundup(handle->conn, rlen));
3829 return NT_STATUS_NO_MEMORY;
3832 return NT_STATUS_OK;
3835 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3836 struct files_struct *fsp,
3837 const struct smb_filename *smb_fname,
3838 TALLOC_CTX *mem_ctx,
3839 unsigned int *pnum_streams,
3840 struct stream_struct **pstreams)
3842 struct fruit_config_data *config = NULL;
3845 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3846 return NT_STATUS_INTERNAL_ERROR);
3848 switch (config->rsrc) {
3849 case FRUIT_RSRC_STREAM:
3850 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3851 mem_ctx, pnum_streams,
3855 case FRUIT_RSRC_XATTR:
3856 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3857 mem_ctx, pnum_streams,
3861 case FRUIT_RSRC_ADFILE:
3862 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3863 mem_ctx, pnum_streams,
3868 return NT_STATUS_INTERNAL_ERROR;
3874 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3875 struct stream_struct **pstreams)
3877 unsigned num_streams = *pnum_streams;
3878 struct stream_struct *streams = *pstreams;
3881 if (!global_fruit_config.nego_aapl) {
3885 while (i < num_streams) {
3886 struct smb_filename smb_fname = (struct smb_filename) {
3887 .stream_name = streams[i].name,
3890 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3891 || streams[i].size > 0)
3897 streams[i] = streams[num_streams - 1];
3901 *pnum_streams = num_streams;
3904 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
3905 struct files_struct *fsp,
3906 TALLOC_CTX *mem_ctx,
3907 unsigned int *pnum_streams,
3908 struct stream_struct **pstreams)
3910 struct fruit_config_data *config = NULL;
3911 const struct smb_filename *smb_fname = NULL;
3914 smb_fname = fsp->fsp_name;
3916 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3917 return NT_STATUS_UNSUCCESSFUL);
3919 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3921 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
3922 pnum_streams, pstreams);
3923 if (!NT_STATUS_IS_OK(status)) {
3927 fruit_filter_empty_streams(pnum_streams, pstreams);
3929 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3930 mem_ctx, pnum_streams, pstreams);
3931 if (!NT_STATUS_IS_OK(status)) {
3935 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3936 mem_ctx, pnum_streams, pstreams);
3937 if (!NT_STATUS_IS_OK(status)) {
3941 return NT_STATUS_OK;
3944 static int fruit_fntimes(vfs_handle_struct *handle,
3946 struct smb_file_time *ft)
3949 struct adouble *ad = NULL;
3950 struct fruit_config_data *config = NULL;
3952 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3955 if ((config->meta != FRUIT_META_NETATALK) ||
3956 is_omit_timespec(&ft->create_time))
3958 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3961 DBG_DEBUG("set btime for %s to %s\n", fsp_str_dbg(fsp),
3962 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
3964 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3969 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3970 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3972 rc = ad_fset(handle, ad, fsp);
3978 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
3981 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3984 static int fruit_fallocate(struct vfs_handle_struct *handle,
3985 struct files_struct *fsp,
3990 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3993 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3996 /* Let the pwrite code path handle it. */
4001 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4002 struct files_struct *fsp,
4005 #ifdef HAVE_ATTROPEN
4006 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4011 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4012 struct files_struct *fsp,
4015 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4017 struct adouble *ad = NULL;
4020 if (fio == NULL || fio->ad_fsp == NULL) {
4021 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4026 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4028 DBG_ERR("ad_fget [%s] failed [%s]\n",
4029 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4033 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4035 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4041 ad_setentrylen(ad, ADEID_RFORK, offset);
4043 rc = ad_fset(handle, ad, fio->ad_fsp);
4045 DBG_ERR("ad_fset [%s] failed [%s]\n",
4046 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4055 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4056 struct files_struct *fsp,
4059 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4062 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4063 struct files_struct *fsp,
4066 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4070 DBG_ERR("Failed to fetch fsp extension");
4074 switch (fio->config->rsrc) {
4075 case FRUIT_RSRC_XATTR:
4076 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4079 case FRUIT_RSRC_ADFILE:
4080 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4083 case FRUIT_RSRC_STREAM:
4084 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4088 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4096 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4097 struct files_struct *fsp,
4101 DBG_WARNING("ftruncate %s to %jd",
4102 fsp_str_dbg(fsp), (intmax_t)offset);
4103 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4108 /* OS X returns success but does nothing */
4109 DBG_INFO("ignoring ftruncate %s to %jd\n",
4110 fsp_str_dbg(fsp), (intmax_t)offset);
4114 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4115 struct files_struct *fsp,
4118 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4121 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4125 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4128 if (fio->type == ADOUBLE_META) {
4129 ret = fruit_ftruncate_meta(handle, fsp, offset);
4131 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4134 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4138 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4139 struct smb_request *req,
4140 struct files_struct *dirfsp,
4141 struct smb_filename *smb_fname,
4142 uint32_t access_mask,
4143 uint32_t share_access,
4144 uint32_t create_disposition,
4145 uint32_t create_options,
4146 uint32_t file_attributes,
4147 uint32_t oplock_request,
4148 const struct smb2_lease *lease,
4149 uint64_t allocation_size,
4150 uint32_t private_flags,
4151 struct security_descriptor *sd,
4152 struct ea_list *ea_list,
4153 files_struct **result,
4155 const struct smb2_create_blobs *in_context_blobs,
4156 struct smb2_create_blobs *out_context_blobs)
4159 struct fruit_config_data *config = NULL;
4160 files_struct *fsp = NULL;
4161 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4164 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4165 if (!NT_STATUS_IS_OK(status)) {
4169 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4170 return NT_STATUS_UNSUCCESSFUL);
4172 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4173 uint32_t conv_flags = 0;
4175 if (config->wipe_intentionally_left_blank_rfork) {
4176 conv_flags |= AD_CONV_WIPE_BLANK;
4178 if (config->delete_empty_adfiles) {
4179 conv_flags |= AD_CONV_DELETE;
4182 ret = ad_convert(handle,
4184 macos_string_replace_map,
4187 DBG_ERR("ad_convert() failed\n");
4188 return NT_STATUS_UNSUCCESSFUL;
4192 status = SMB_VFS_NEXT_CREATE_FILE(
4193 handle, req, dirfsp, smb_fname,
4194 access_mask, share_access,
4195 create_disposition, create_options,
4196 file_attributes, oplock_request,
4198 allocation_size, private_flags,
4199 sd, ea_list, result,
4200 pinfo, in_context_blobs, out_context_blobs);
4201 if (!NT_STATUS_IS_OK(status)) {
4207 if (global_fruit_config.nego_aapl) {
4208 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4210 * Enable POSIX directory rename behaviour
4212 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4217 * If this is a plain open for existing files, opening an 0
4218 * byte size resource fork MUST fail with
4219 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4221 * Cf the vfs_fruit torture tests in test_rfork_create().
4223 if (global_fruit_config.nego_aapl &&
4224 create_disposition == FILE_OPEN &&
4225 smb_fname->st.st_ex_size == 0 &&
4226 is_named_stream(smb_fname))
4228 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4232 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4236 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4237 (fsp->op != NULL) &&
4238 !fsp->fsp_flags.is_pathref)
4240 status = fruit_check_access(
4244 if (!NT_STATUS_IS_OK(status)) {
4252 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4255 close_file_free(req, &fsp, ERROR_CLOSE);
4262 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4263 struct files_struct *fsp,
4264 TALLOC_CTX *mem_ctx,
4265 struct readdir_attr_data **pattr_data)
4267 struct fruit_config_data *config = NULL;
4268 struct readdir_attr_data *attr_data;
4269 uint32_t conv_flags = 0;
4273 SMB_VFS_HANDLE_GET_DATA(handle, config,
4274 struct fruit_config_data,
4275 return NT_STATUS_UNSUCCESSFUL);
4277 if (!global_fruit_config.nego_aapl) {
4278 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4284 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4286 if (config->wipe_intentionally_left_blank_rfork) {
4287 conv_flags |= AD_CONV_WIPE_BLANK;
4289 if (config->delete_empty_adfiles) {
4290 conv_flags |= AD_CONV_DELETE;
4293 ret = ad_convert(handle,
4295 macos_string_replace_map,
4298 DBG_ERR("ad_convert() failed\n");
4299 return NT_STATUS_UNSUCCESSFUL;
4302 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4303 if (*pattr_data == NULL) {
4304 return NT_STATUS_NO_MEMORY;
4306 attr_data = *pattr_data;
4307 attr_data->type = RDATTR_AAPL;
4310 * Mac metadata: compressed FinderInfo, resource fork length
4313 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4314 if (!NT_STATUS_IS_OK(status)) {
4316 * Error handling is tricky: if we return failure from
4317 * this function, the corresponding directory entry
4318 * will to be passed to the client, so we really just
4319 * want to error out on fatal errors.
4321 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4329 if (config->unix_info_enabled) {
4330 attr_data->attr_data.aapl.unix_mode =
4331 fsp->fsp_name->st.st_ex_mode;
4337 if (!config->readdir_attr_max_access) {
4338 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4340 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4343 SEC_FLAG_MAXIMUM_ALLOWED,
4344 &attr_data->attr_data.aapl.max_access);
4345 if (!NT_STATUS_IS_OK(status)) {
4350 return NT_STATUS_OK;
4353 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4355 TALLOC_FREE(*pattr_data);
4359 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4361 uint32_t security_info,
4362 TALLOC_CTX *mem_ctx,
4363 struct security_descriptor **ppdesc)
4366 struct security_ace ace;
4368 struct fruit_config_data *config;
4370 SMB_VFS_HANDLE_GET_DATA(handle, config,
4371 struct fruit_config_data,
4372 return NT_STATUS_UNSUCCESSFUL);
4374 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4376 if (!NT_STATUS_IS_OK(status)) {
4381 * Add MS NFS style ACEs with uid, gid and mode
4383 if (!global_fruit_config.nego_aapl) {
4384 return NT_STATUS_OK;
4386 if (!config->unix_info_enabled) {
4387 return NT_STATUS_OK;
4390 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4391 status = remove_virtual_nfs_aces(*ppdesc);
4392 if (!NT_STATUS_IS_OK(status)) {
4393 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4397 /* MS NFS style mode */
4398 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4399 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4400 status = security_descriptor_dacl_add(*ppdesc, &ace);
4401 if (!NT_STATUS_IS_OK(status)) {
4402 DEBUG(1,("failed to add MS NFS style ACE\n"));
4406 /* MS NFS style uid */
4407 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4408 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4409 status = security_descriptor_dacl_add(*ppdesc, &ace);
4410 if (!NT_STATUS_IS_OK(status)) {
4411 DEBUG(1,("failed to add MS NFS style ACE\n"));
4415 /* MS NFS style gid */
4416 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4417 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4418 status = security_descriptor_dacl_add(*ppdesc, &ace);
4419 if (!NT_STATUS_IS_OK(status)) {
4420 DEBUG(1,("failed to add MS NFS style ACE\n"));
4424 return NT_STATUS_OK;
4427 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4429 uint32_t security_info_sent,
4430 const struct security_descriptor *orig_psd)
4434 mode_t ms_nfs_mode = 0;
4436 struct security_descriptor *psd = NULL;
4437 uint32_t orig_num_aces = 0;
4439 if (orig_psd->dacl != NULL) {
4440 orig_num_aces = orig_psd->dacl->num_aces;
4443 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4445 return NT_STATUS_NO_MEMORY;
4448 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4450 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4451 if (!NT_STATUS_IS_OK(status)) {
4452 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4458 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4459 * sent/present flags correctly now we've removed them.
4462 if (orig_num_aces != 0) {
4464 * Are there any ACE's left ?
4466 if (psd->dacl->num_aces == 0) {
4467 /* No - clear the DACL sent/present flags. */
4468 security_info_sent &= ~SECINFO_DACL;
4469 psd->type &= ~SEC_DESC_DACL_PRESENT;
4473 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4474 if (!NT_STATUS_IS_OK(status)) {
4475 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4481 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4483 DBG_WARNING("%s, result: %d, %04o error %s\n",
4486 (unsigned)ms_nfs_mode,
4488 status = map_nt_error_from_unix(errno);
4495 return NT_STATUS_OK;
4498 static struct vfs_offload_ctx *fruit_offload_ctx;
4500 struct fruit_offload_read_state {
4501 struct vfs_handle_struct *handle;
4502 struct tevent_context *ev;
4510 static void fruit_offload_read_done(struct tevent_req *subreq);
4512 static struct tevent_req *fruit_offload_read_send(
4513 TALLOC_CTX *mem_ctx,
4514 struct tevent_context *ev,
4515 struct vfs_handle_struct *handle,
4522 struct tevent_req *req = NULL;
4523 struct tevent_req *subreq = NULL;
4524 struct fruit_offload_read_state *state = NULL;
4526 req = tevent_req_create(mem_ctx, &state,
4527 struct fruit_offload_read_state);
4531 *state = (struct fruit_offload_read_state) {
4538 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4539 fsctl, ttl, offset, to_copy);
4540 if (tevent_req_nomem(subreq, req)) {
4541 return tevent_req_post(req, ev);
4543 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4547 static void fruit_offload_read_done(struct tevent_req *subreq)
4549 struct tevent_req *req = tevent_req_callback_data(
4550 subreq, struct tevent_req);
4551 struct fruit_offload_read_state *state = tevent_req_data(
4552 req, struct fruit_offload_read_state);
4555 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4561 TALLOC_FREE(subreq);
4562 if (tevent_req_nterror(req, status)) {
4566 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4567 tevent_req_done(req);
4571 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4572 &fruit_offload_ctx);
4573 if (tevent_req_nterror(req, status)) {
4577 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4580 if (tevent_req_nterror(req, status)) {
4584 tevent_req_done(req);
4588 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4589 struct vfs_handle_struct *handle,
4590 TALLOC_CTX *mem_ctx,
4595 struct fruit_offload_read_state *state = tevent_req_data(
4596 req, struct fruit_offload_read_state);
4599 if (tevent_req_is_nterror(req, &status)) {
4600 tevent_req_received(req);
4604 *flags = state->flags;
4605 *xferlen = state->xferlen;
4606 token->length = state->token.length;
4607 token->data = talloc_move(mem_ctx, &state->token.data);
4609 tevent_req_received(req);
4610 return NT_STATUS_OK;
4613 struct fruit_offload_write_state {
4614 struct vfs_handle_struct *handle;
4616 struct files_struct *src_fsp;
4617 struct files_struct *dst_fsp;
4621 static void fruit_offload_write_done(struct tevent_req *subreq);
4622 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4623 TALLOC_CTX *mem_ctx,
4624 struct tevent_context *ev,
4627 off_t transfer_offset,
4628 struct files_struct *dest_fsp,
4632 struct tevent_req *req, *subreq;
4633 struct fruit_offload_write_state *state;
4635 struct fruit_config_data *config;
4636 off_t src_off = transfer_offset;
4637 files_struct *src_fsp = NULL;
4638 off_t to_copy = num;
4639 bool copyfile_enabled = false;
4641 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4642 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4644 SMB_VFS_HANDLE_GET_DATA(handle, config,
4645 struct fruit_config_data,
4648 req = tevent_req_create(mem_ctx, &state,
4649 struct fruit_offload_write_state);
4653 state->handle = handle;
4654 state->dst_fsp = dest_fsp;
4657 case FSCTL_SRV_COPYCHUNK:
4658 case FSCTL_SRV_COPYCHUNK_WRITE:
4659 copyfile_enabled = config->copyfile_enabled;
4666 * Check if this a OS X copyfile style copychunk request with
4667 * a requested chunk count of 0 that was translated to a
4668 * offload_write_send VFS call overloading the parameters src_off
4669 * = dest_off = num = 0.
4671 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4672 status = vfs_offload_token_db_fetch_fsp(
4673 fruit_offload_ctx, token, &src_fsp);
4674 if (tevent_req_nterror(req, status)) {
4675 return tevent_req_post(req, ev);
4677 state->src_fsp = src_fsp;
4679 status = vfs_stat_fsp(src_fsp);
4680 if (tevent_req_nterror(req, status)) {
4681 return tevent_req_post(req, ev);
4684 to_copy = src_fsp->fsp_name->st.st_ex_size;
4685 state->is_copyfile = true;
4688 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4697 if (tevent_req_nomem(subreq, req)) {
4698 return tevent_req_post(req, ev);
4701 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4705 static void fruit_offload_write_done(struct tevent_req *subreq)
4707 struct tevent_req *req = tevent_req_callback_data(
4708 subreq, struct tevent_req);
4709 struct fruit_offload_write_state *state = tevent_req_data(
4710 req, struct fruit_offload_write_state);
4712 unsigned int num_streams = 0;
4713 struct stream_struct *streams = NULL;
4715 struct smb_filename *src_fname_tmp = NULL;
4716 struct smb_filename *dst_fname_tmp = NULL;
4718 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4721 TALLOC_FREE(subreq);
4722 if (tevent_req_nterror(req, status)) {
4726 if (!state->is_copyfile) {
4727 tevent_req_done(req);
4732 * Now copy all remaining streams. We know the share supports
4733 * streams, because we're in vfs_fruit. We don't do this async
4734 * because streams are few and small.
4736 status = vfs_fstreaminfo(state->src_fsp,
4737 req, &num_streams, &streams);
4738 if (tevent_req_nterror(req, status)) {
4742 if (num_streams == 1) {
4743 /* There is always one stream, ::$DATA. */
4744 tevent_req_done(req);
4748 for (i = 0; i < num_streams; i++) {
4749 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4750 __func__, streams[i].name, (size_t)streams[i].size));
4752 src_fname_tmp = synthetic_smb_fname(
4754 state->src_fsp->fsp_name->base_name,
4757 state->src_fsp->fsp_name->twrp,
4758 state->src_fsp->fsp_name->flags);
4759 if (tevent_req_nomem(src_fname_tmp, req)) {
4763 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4764 TALLOC_FREE(src_fname_tmp);
4768 dst_fname_tmp = synthetic_smb_fname(
4770 state->dst_fsp->fsp_name->base_name,
4773 state->dst_fsp->fsp_name->twrp,
4774 state->dst_fsp->fsp_name->flags);
4775 if (tevent_req_nomem(dst_fname_tmp, req)) {
4776 TALLOC_FREE(src_fname_tmp);
4780 status = copy_file(req,
4781 state->handle->conn,
4784 OPENX_FILE_CREATE_IF_NOT_EXIST);
4785 if (!NT_STATUS_IS_OK(status)) {
4786 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4787 smb_fname_str_dbg(src_fname_tmp),
4788 smb_fname_str_dbg(dst_fname_tmp),
4789 nt_errstr(status)));
4790 TALLOC_FREE(src_fname_tmp);
4791 TALLOC_FREE(dst_fname_tmp);
4792 tevent_req_nterror(req, status);
4796 TALLOC_FREE(src_fname_tmp);
4797 TALLOC_FREE(dst_fname_tmp);
4800 TALLOC_FREE(streams);
4801 TALLOC_FREE(src_fname_tmp);
4802 TALLOC_FREE(dst_fname_tmp);
4803 tevent_req_done(req);
4806 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4807 struct tevent_req *req,
4810 struct fruit_offload_write_state *state = tevent_req_data(
4811 req, struct fruit_offload_write_state);
4814 if (tevent_req_is_nterror(req, &status)) {
4815 DEBUG(1, ("server side copy chunk failed: %s\n",
4816 nt_errstr(status)));
4818 tevent_req_received(req);
4822 *copied = state->copied;
4823 tevent_req_received(req);
4825 return NT_STATUS_OK;
4828 static char *fruit_get_bandsize_line(char **lines, int numlines)
4831 static bool re_initialized = false;
4835 if (!re_initialized) {
4836 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4840 re_initialized = true;
4843 for (i = 0; i < numlines; i++) {
4844 regmatch_t matches[1];
4846 ret = regexec(&re, lines[i], 1, matches, 0);
4849 * Check if the match was on the last line, sa we want
4850 * the subsequent line.
4852 if (i + 1 == numlines) {
4855 return lines[i + 1];
4857 if (ret != REG_NOMATCH) {
4865 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4868 static bool re_initialized = false;
4869 regmatch_t matches[2];
4874 if (!re_initialized) {
4877 "<integer>\\([[:digit:]]*\\)</integer>$",
4882 re_initialized = true;
4885 ret = regexec(&re, line, 2, matches, 0);
4887 DBG_ERR("regex failed [%s]\n", line);
4891 line[matches[1].rm_eo] = '\0';
4893 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4897 *_band_size = (size_t)band_size;
4902 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4903 * "band-size" key and value.
4905 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4909 #define INFO_PLIST_MAX_SIZE 64*1024
4911 struct smb_filename *smb_fname = NULL;
4912 files_struct *fsp = NULL;
4913 uint8_t *file_data = NULL;
4914 char **lines = NULL;
4915 char *band_size_line = NULL;
4916 size_t plist_file_size;
4923 plist = talloc_asprintf(talloc_tos(),
4925 handle->conn->connectpath,
4927 if (plist == NULL) {
4932 smb_fname = synthetic_smb_fname(talloc_tos(),
4938 if (smb_fname == NULL) {
4943 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4945 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4950 plist_file_size = smb_fname->st.st_ex_size;
4952 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4953 DBG_INFO("%s is too large, ignoring\n", plist);
4958 status = SMB_VFS_NEXT_CREATE_FILE(
4962 smb_fname, /* fname */
4963 FILE_GENERIC_READ, /* access_mask */
4964 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4965 FILE_OPEN, /* create_disposition */
4966 0, /* create_options */
4967 0, /* file_attributes */
4968 INTERNAL_OPEN_ONLY, /* oplock_request */
4970 0, /* allocation_size */
4971 0, /* private_flags */
4976 NULL, NULL); /* create context */
4977 if (!NT_STATUS_IS_OK(status)) {
4978 DBG_INFO("Opening [%s] failed [%s]\n",
4979 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4984 file_data = talloc_zero_array(talloc_tos(),
4986 plist_file_size + 1);
4987 if (file_data == NULL) {
4992 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4993 if (nread != plist_file_size) {
4994 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4995 fsp_str_dbg(fsp), nread, plist_file_size);
5001 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5002 if (!NT_STATUS_IS_OK(status)) {
5003 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5008 lines = file_lines_parse((char *)file_data,
5012 if (lines == NULL) {
5017 band_size_line = fruit_get_bandsize_line(lines, numlines);
5018 if (band_size_line == NULL) {
5019 DBG_ERR("Didn't find band-size key in [%s]\n",
5020 smb_fname_str_dbg(smb_fname));
5025 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5027 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5031 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5035 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5036 if (!NT_STATUS_IS_OK(status)) {
5037 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5041 TALLOC_FREE(smb_fname);
5042 TALLOC_FREE(file_data);
5047 struct fruit_disk_free_state {
5051 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5056 struct smb_filename *bands_dir = NULL;
5057 struct smb_Dir *dir_hnd = NULL;
5058 const char *dname = NULL;
5059 char *talloced = NULL;
5064 path = talloc_asprintf(talloc_tos(),
5066 handle->conn->connectpath,
5072 bands_dir = synthetic_smb_fname(talloc_tos(),
5079 if (bands_dir == NULL) {
5083 status = OpenDir(talloc_tos(),
5089 if (!NT_STATUS_IS_OK(status)) {
5090 TALLOC_FREE(bands_dir);
5091 errno = map_errno_from_nt_status(status);
5097 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5100 if (ISDOT(dname) || ISDOTDOT(dname)) {
5105 TALLOC_FREE(dir_hnd);
5107 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5109 TALLOC_FREE(bands_dir);
5115 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5116 struct fruit_disk_free_state *state,
5121 size_t sparsebundle_strlen = strlen("sparsebundle");
5122 size_t bandsize = 0;
5126 p = strstr(name, "sparsebundle");
5131 if (p[sparsebundle_strlen] != '\0') {
5135 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5137 ok = fruit_get_bandsize(handle, name, &bandsize);
5140 * Beware of race conditions: this may be an uninitialized
5141 * Info.plist that a client is just creating. We don't want let
5142 * this to trigger complete failure.
5144 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5148 ok = fruit_get_num_bands(handle, name, &nbands);
5151 * Beware of race conditions: this may be a backup sparsebundle
5152 * in an early stage lacking a bands subdirectory. We don't want
5153 * let this to trigger complete failure.
5155 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5160 * Arithmetic on 32-bit systems may cause overflow, depending on
5161 * size_t precision. First we check its unlikely, then we
5162 * force the precision into target off_t, then we check that
5163 * the total did not overflow either.
5165 if (bandsize > SIZE_MAX/nbands) {
5166 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5170 tm_size = (off_t)bandsize * (off_t)nbands;
5172 if (state->total_size + tm_size < state->total_size) {
5173 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5178 state->total_size += tm_size;
5180 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5181 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5187 * Calculate used size of a TimeMachine volume
5189 * This assumes that the volume is used only for TimeMachine.
5191 * - readdir(basedir of share), then
5192 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5193 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5194 * - count band files in "\1.sparsebundle/bands/"
5195 * - calculate used size of all bands: band_count * band_size
5197 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5198 const struct smb_filename *smb_fname,
5203 struct fruit_config_data *config = NULL;
5204 struct fruit_disk_free_state state = {0};
5205 struct smb_Dir *dir_hnd = NULL;
5206 const char *dname = NULL;
5207 char *talloced = NULL;
5214 SMB_VFS_HANDLE_GET_DATA(handle, config,
5215 struct fruit_config_data,
5218 if (!config->time_machine ||
5219 config->time_machine_max_size == 0)
5221 return SMB_VFS_NEXT_DISK_FREE(handle,
5228 status = OpenDir(talloc_tos(),
5234 if (!NT_STATUS_IS_OK(status)) {
5235 errno = map_errno_from_nt_status(status);
5239 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5242 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5244 TALLOC_FREE(talloced);
5245 TALLOC_FREE(dir_hnd);
5248 TALLOC_FREE(talloced);
5251 TALLOC_FREE(dir_hnd);
5253 dsize = config->time_machine_max_size / 512;
5254 dfree = dsize - (state.total_size / 512);
5255 if (dfree > dsize) {
5265 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5266 const SMB_STRUCT_STAT *psbuf)
5268 struct fruit_config_data *config = NULL;
5270 SMB_VFS_HANDLE_GET_DATA(handle, config,
5271 struct fruit_config_data,
5274 if (global_fruit_config.nego_aapl &&
5275 config->aapl_zero_file_id)
5280 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5283 static struct vfs_fn_pointers vfs_fruit_fns = {
5284 .connect_fn = fruit_connect,
5285 .disk_free_fn = fruit_disk_free,
5287 /* File operations */
5288 .fchmod_fn = fruit_fchmod,
5289 .unlinkat_fn = fruit_unlinkat,
5290 .renameat_fn = fruit_renameat,
5291 .openat_fn = fruit_openat,
5292 .close_fn = fruit_close,
5293 .pread_fn = fruit_pread,
5294 .pwrite_fn = fruit_pwrite,
5295 .pread_send_fn = fruit_pread_send,
5296 .pread_recv_fn = fruit_pread_recv,
5297 .pwrite_send_fn = fruit_pwrite_send,
5298 .pwrite_recv_fn = fruit_pwrite_recv,
5299 .stat_fn = fruit_stat,
5300 .lstat_fn = fruit_lstat,
5301 .fstat_fn = fruit_fstat,
5302 .fstreaminfo_fn = fruit_fstreaminfo,
5303 .fntimes_fn = fruit_fntimes,
5304 .ftruncate_fn = fruit_ftruncate,
5305 .fallocate_fn = fruit_fallocate,
5306 .create_file_fn = fruit_create_file,
5307 .freaddir_attr_fn = fruit_freaddir_attr,
5308 .offload_read_send_fn = fruit_offload_read_send,
5309 .offload_read_recv_fn = fruit_offload_read_recv,
5310 .offload_write_send_fn = fruit_offload_write_send,
5311 .offload_write_recv_fn = fruit_offload_write_recv,
5312 .fs_file_id_fn = fruit_fs_file_id,
5314 /* NT ACL operations */
5315 .fget_nt_acl_fn = fruit_fget_nt_acl,
5316 .fset_nt_acl_fn = fruit_fset_nt_acl,
5320 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5322 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5324 if (!NT_STATUS_IS_OK(ret)) {
5328 vfs_fruit_debug_level = debug_add_class("fruit");
5329 if (vfs_fruit_debug_level == -1) {
5330 vfs_fruit_debug_level = DBGC_VFS;
5331 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5334 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5335 "vfs_fruit_init","fruit",vfs_fruit_debug_level));