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 "lib/util/util_file.h"
34 #include "offload_token.h"
35 #include "string_replace.h"
36 #include "hash_inode.h"
37 #include "lib/adouble.h"
38 #include "lib/util_macstreams.h"
39 #include "source3/smbd/dir.h"
42 * Enhanced OS X and Netatalk compatibility
43 * ========================================
45 * This modules takes advantage of vfs_streams_xattr and
46 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
47 * loaded in the correct order:
49 * vfs modules = catia fruit streams_xattr
51 * The module intercepts the OS X special streams "AFP_AfpInfo" and
52 * "AFP_Resource" and handles them in a special way. All other named
53 * streams are deferred to vfs_streams_xattr.
55 * The OS X client maps all NTFS illegal characters to the Unicode
56 * private range. This module optionally stores the characters using
57 * their native ASCII encoding using vfs_catia. If you're not enabling
58 * this feature, you can skip catia from vfs modules.
60 * Finally, open modes are optionally checked against Netatalk AFP
63 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
64 * extended metadata for files and directories. This module optionally
65 * reads and stores this metadata in a way compatible with Netatalk 3
66 * which stores the metadata in an EA "org.netatalk.metadata". Cf
67 * source3/include/MacExtensions.h for a description of the binary
70 * The "AFP_Resource" named stream may be arbitrarily large, thus it
71 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
72 * the only available filesystem where xattrs can be of any size and
73 * the OS supports using the file APIs for xattrs.
75 * The AFP_Resource stream is stored in an AppleDouble file prepending
76 * "._" to the filename. On Solaris with ZFS the stream is optionally
77 * stored in an EA "org.netatalk.resource".
83 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
84 * other protocols you may want to adjust the xattr names the VFS
85 * module vfs_streams_xattr uses for storing ADS's. This defaults to
86 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
87 * these module parameters:
89 * streams_xattr:prefix = user.
90 * streams_xattr:store_stream_type = false
96 * - log diagnostic if any needed VFS module is not loaded
97 * (eg with lp_vfs_objects())
101 static int vfs_fruit_debug_level = DBGC_VFS;
103 static struct global_fruit_config {
104 bool nego_aapl; /* client negotiated AAPL */
106 } global_fruit_config;
109 #define DBGC_CLASS vfs_fruit_debug_level
111 #define FRUIT_PARAM_TYPE_NAME "fruit"
113 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
115 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
116 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
117 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
118 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
120 struct fruit_config_data {
121 enum fruit_rsrc rsrc;
122 enum fruit_meta meta;
123 enum fruit_locking locking;
124 enum fruit_encoding encoding;
125 bool use_aapl; /* config from smb.conf */
127 bool readdir_attr_enabled;
128 bool unix_info_enabled;
129 bool copyfile_enabled;
130 bool veto_appledouble;
132 bool aapl_zero_file_id;
135 off_t time_machine_max_size;
136 bool convert_adouble;
137 bool wipe_intentionally_left_blank_rfork;
138 bool delete_empty_adfiles;
139 bool validate_afpinfo;
142 * Additional options, all enabled by default,
143 * possibly useful for analyzing performance. The associated
144 * operations with each of them may be expensive, so having
145 * the chance to disable them individually gives a chance
146 * tweaking the setup for the particular usecase.
148 bool readdir_attr_rsize;
149 bool readdir_attr_finder_info;
150 bool readdir_attr_max_access;
151 /* Recursion guard. Will go away when we have STATX. */
152 bool in_openat_pathref_fsp;
155 static const struct enum_list fruit_rsrc[] = {
156 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
157 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
158 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
162 static const struct enum_list fruit_meta[] = {
163 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
164 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
168 static const struct enum_list fruit_locking[] = {
169 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
170 {FRUIT_LOCKING_NONE, "none"},
174 static const struct enum_list fruit_encoding[] = {
175 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
176 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
181 vfs_handle_struct *handle;
182 files_struct *fsp; /* backlink to itself */
184 /* tcon config handle */
185 struct fruit_config_data *config;
187 /* Backend fsp for AppleDouble file, can be NULL */
188 files_struct *ad_fsp;
189 /* link from adouble_open_from_base_fsp() to fio */
190 struct fio *real_fio;
192 /* Denote stream type, meta or rsrc */
196 * AFP_AfpInfo stream created, but not written yet, thus still a fake
197 * pipe fd. This is set to true in fruit_open_meta if there was no
198 * existing stream but the caller requested O_CREAT. It is later set to
199 * false when we get a write on the stream that then does open and
207 /*****************************************************************************
209 *****************************************************************************/
211 static struct adouble *ad_get_meta_fsp(TALLOC_CTX *ctx,
212 vfs_handle_struct *handle,
213 const struct smb_filename *smb_fname)
216 struct adouble *ad = NULL;
217 struct smb_filename *smb_fname_cp = NULL;
218 struct fruit_config_data *config = NULL;
220 if (smb_fname->fsp != NULL) {
221 return ad_get(ctx, handle, smb_fname, ADOUBLE_META);
224 SMB_VFS_HANDLE_GET_DATA(handle,
226 struct fruit_config_data,
229 if (config->in_openat_pathref_fsp) {
233 smb_fname_cp = cp_smb_filename(ctx,
235 if (smb_fname_cp == NULL) {
238 TALLOC_FREE(smb_fname_cp->stream_name);
239 config->in_openat_pathref_fsp = true;
240 status = openat_pathref_fsp(handle->conn->cwd_fsp,
242 config->in_openat_pathref_fsp = false;
243 if (!NT_STATUS_IS_OK(status)) {
244 TALLOC_FREE(smb_fname_cp);
248 ad = ad_get(ctx, handle, smb_fname_cp, ADOUBLE_META);
249 TALLOC_FREE(smb_fname_cp);
253 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
256 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
262 if (fio->real_fio != NULL) {
264 * This is an fsp from adouble_open_from_base_fsp()
265 * we should just pass this to the next
275 * Initialize config struct from our smb.conf config parameters
277 static int init_fruit_config(vfs_handle_struct *handle)
279 struct fruit_config_data *config;
281 const char *tm_size_str = NULL;
283 config = talloc_zero(handle->conn, struct fruit_config_data);
285 DEBUG(1, ("talloc_zero() failed\n"));
290 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
291 "resource", fruit_rsrc, FRUIT_RSRC_ADFILE);
293 DEBUG(1, ("value for %s: resource type unknown\n",
294 FRUIT_PARAM_TYPE_NAME));
297 config->rsrc = (enum fruit_rsrc)enumval;
299 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
300 "metadata", fruit_meta, FRUIT_META_NETATALK);
302 DEBUG(1, ("value for %s: metadata type unknown\n",
303 FRUIT_PARAM_TYPE_NAME));
306 config->meta = (enum fruit_meta)enumval;
308 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
309 "locking", fruit_locking, FRUIT_LOCKING_NONE);
311 DEBUG(1, ("value for %s: locking type unknown\n",
312 FRUIT_PARAM_TYPE_NAME));
315 config->locking = (enum fruit_locking)enumval;
317 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
318 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
320 DEBUG(1, ("value for %s: encoding type unknown\n",
321 FRUIT_PARAM_TYPE_NAME));
324 config->encoding = (enum fruit_encoding)enumval;
326 if (config->rsrc == FRUIT_RSRC_ADFILE) {
327 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
328 FRUIT_PARAM_TYPE_NAME,
333 config->use_aapl = lp_parm_bool(
334 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
336 config->time_machine = lp_parm_bool(
337 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
339 config->unix_info_enabled = lp_parm_bool(
340 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
342 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
345 config->posix_rename = lp_parm_bool(
346 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
348 config->aapl_zero_file_id =
349 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
350 "zero_file_id", true);
352 config->readdir_attr_rsize = lp_parm_bool(
353 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
355 config->readdir_attr_finder_info = lp_parm_bool(
356 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
358 config->readdir_attr_max_access = lp_parm_bool(
359 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
361 config->model = lp_parm_const_string(
362 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
364 tm_size_str = lp_parm_const_string(
365 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
366 "time machine max size", NULL);
367 if (tm_size_str != NULL) {
368 config->time_machine_max_size = conv_str_size(tm_size_str);
371 config->convert_adouble = lp_parm_bool(
372 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
373 "convert_adouble", true);
375 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
376 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
377 "wipe_intentionally_left_blank_rfork", false);
379 config->delete_empty_adfiles = lp_parm_bool(
380 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
381 "delete_empty_adfiles", false);
383 config->validate_afpinfo = lp_parm_bool(
384 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
385 "validate_afpinfo", true);
387 SMB_VFS_HANDLE_SET_DATA(handle, config,
388 NULL, struct fruit_config_data,
394 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
395 struct stream_struct **streams,
396 const char *name, off_t size,
399 struct stream_struct *tmp;
401 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
407 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
408 if (tmp[*num_streams].name == NULL) {
412 tmp[*num_streams].size = size;
413 tmp[*num_streams].alloc_size = alloc_size;
420 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
421 struct stream_struct **streams)
423 struct stream_struct *tmp = *streams;
426 if (*num_streams == 0) {
430 for (i = 0; i < *num_streams; i++) {
431 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
436 if (i == *num_streams) {
440 if (tmp[i].size > 0) {
444 TALLOC_FREE(tmp[i].name);
445 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
450 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
451 struct stream_struct **streams,
454 struct stream_struct *tmp = *streams;
457 if (*num_streams == 0) {
461 for (i = 0; i < *num_streams; i++) {
462 if (strequal_m(tmp[i].name, name)) {
467 if (i == *num_streams) {
471 TALLOC_FREE(tmp[i].name);
472 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
477 static bool ad_empty_finderinfo(const struct adouble *ad)
480 char emptybuf[ADEDLEN_FINDERI] = {0};
483 fi = ad_get_entry(ad, ADEID_FINDERI);
485 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
489 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
493 static bool ai_empty_finderinfo(const AfpInfo *ai)
496 char emptybuf[ADEDLEN_FINDERI] = {0};
498 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
503 * Update btime with btime from Netatalk
505 static void update_btime(vfs_handle_struct *handle,
506 struct smb_filename *smb_fname)
509 struct timespec creation_time = {0};
511 struct fruit_config_data *config = NULL;
513 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
516 switch (config->meta) {
517 case FRUIT_META_STREAM:
519 case FRUIT_META_NETATALK:
523 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
527 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
531 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
537 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
538 update_stat_ex_create_time(&smb_fname->st, creation_time);
544 * Map an access mask to a Netatalk single byte byte range lock
546 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
547 uint32_t access_mask)
551 switch (access_mask) {
553 offset = AD_FILELOCK_OPEN_RD;
556 case FILE_WRITE_DATA:
557 case FILE_APPEND_DATA:
558 offset = AD_FILELOCK_OPEN_WR;
562 offset = AD_FILELOCK_OPEN_NONE;
566 if (fork_type == APPLE_FORK_RSRC) {
567 if (offset == AD_FILELOCK_OPEN_NONE) {
568 offset = AD_FILELOCK_RSRC_OPEN_NONE;
578 * Map a deny mode to a Netatalk brl
580 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
587 offset = AD_FILELOCK_DENY_RD;
591 offset = AD_FILELOCK_DENY_WR;
595 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
598 if (fork_type == APPLE_FORK_RSRC) {
606 * Call fcntl() with an exclusive F_GETLK request in order to
607 * determine if there's an existing shared lock
609 * @return true if the requested lock was found or any error occurred
610 * false if the lock was not found
612 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
615 off_t offset = in_offset;
620 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
621 if (result == false) {
625 if (type != F_UNLCK) {
632 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
634 uint32_t access_mask,
637 NTSTATUS status = NT_STATUS_OK;
639 bool share_for_read = (share_mode & FILE_SHARE_READ);
640 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
641 bool netatalk_already_open_for_reading = false;
642 bool netatalk_already_open_for_writing = false;
643 bool netatalk_already_open_with_deny_read = false;
644 bool netatalk_already_open_with_deny_write = false;
645 struct GUID req_guid = GUID_random();
647 /* FIXME: hardcoded data fork, add resource fork */
648 enum apple_fork fork_type = APPLE_FORK_DATA;
650 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
652 access_mask & FILE_READ_DATA ? "READ" :"-",
653 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
656 if (fsp_get_io_fd(fsp) == -1) {
660 /* Read NetATalk opens and deny modes on the file. */
661 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
662 access_to_netatalk_brl(fork_type,
665 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
666 denymode_to_netatalk_brl(fork_type,
669 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
670 access_to_netatalk_brl(fork_type,
673 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
674 denymode_to_netatalk_brl(fork_type,
677 /* If there are any conflicts - sharing violation. */
678 if ((access_mask & FILE_READ_DATA) &&
679 netatalk_already_open_with_deny_read) {
680 return NT_STATUS_SHARING_VIOLATION;
683 if (!share_for_read &&
684 netatalk_already_open_for_reading) {
685 return NT_STATUS_SHARING_VIOLATION;
688 if ((access_mask & FILE_WRITE_DATA) &&
689 netatalk_already_open_with_deny_write) {
690 return NT_STATUS_SHARING_VIOLATION;
693 if (!share_for_write &&
694 netatalk_already_open_for_writing) {
695 return NT_STATUS_SHARING_VIOLATION;
698 if (!(access_mask & FILE_READ_DATA)) {
700 * Nothing we can do here, we need read access
706 /* Set NetAtalk locks matching our access */
707 if (access_mask & FILE_READ_DATA) {
708 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
709 req_guid.time_hi_and_version = __LINE__;
714 fsp->op->global->open_persistent_id,
722 if (!NT_STATUS_IS_OK(status)) {
727 if (!share_for_read) {
728 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
729 req_guid.time_hi_and_version = __LINE__;
734 fsp->op->global->open_persistent_id,
742 if (!NT_STATUS_IS_OK(status)) {
747 if (access_mask & FILE_WRITE_DATA) {
748 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
749 req_guid.time_hi_and_version = __LINE__;
754 fsp->op->global->open_persistent_id,
762 if (!NT_STATUS_IS_OK(status)) {
767 if (!share_for_write) {
768 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
769 req_guid.time_hi_and_version = __LINE__;
774 fsp->op->global->open_persistent_id,
782 if (!NT_STATUS_IS_OK(status)) {
790 static NTSTATUS check_aapl(vfs_handle_struct *handle,
791 struct smb_request *req,
792 const struct smb2_create_blobs *in_context_blobs,
793 struct smb2_create_blobs *out_context_blobs)
795 struct fruit_config_data *config;
797 struct smb2_create_blob *aapl = NULL;
801 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
802 uint64_t req_bitmap, client_caps;
803 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
807 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
808 return NT_STATUS_UNSUCCESSFUL);
810 if (!config->use_aapl
811 || in_context_blobs == NULL
812 || out_context_blobs == NULL) {
816 aapl = smb2_create_blob_find(in_context_blobs,
817 SMB2_CREATE_TAG_AAPL);
822 if (aapl->data.length != 24) {
823 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
824 (uintmax_t)aapl->data.length));
825 return NT_STATUS_INVALID_PARAMETER;
828 cmd = IVAL(aapl->data.data, 0);
829 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
830 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
831 return NT_STATUS_INVALID_PARAMETER;
834 req_bitmap = BVAL(aapl->data.data, 8);
835 client_caps = BVAL(aapl->data.data, 16);
837 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
839 SBVAL(p, 8, req_bitmap);
840 ok = data_blob_append(req, &blob, p, 16);
842 return NT_STATUS_UNSUCCESSFUL;
845 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
846 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
847 (handle->conn->fs_capabilities & FILE_NAMED_STREAMS)) {
848 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
849 config->readdir_attr_enabled = true;
852 if (config->use_copyfile) {
853 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
854 config->copyfile_enabled = true;
858 * The client doesn't set the flag, so we can't check
859 * for it and just set it unconditionally
861 if (config->unix_info_enabled) {
862 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
865 SBVAL(p, 0, server_caps);
866 ok = data_blob_append(req, &blob, p, 8);
868 return NT_STATUS_UNSUCCESSFUL;
872 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
873 int val = lp_case_sensitive(SNUM(handle->conn));
881 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
888 if (config->time_machine) {
889 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
894 ok = data_blob_append(req, &blob, p, 8);
896 return NT_STATUS_UNSUCCESSFUL;
900 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
901 ok = convert_string_talloc(req,
903 config->model, strlen(config->model),
906 return NT_STATUS_UNSUCCESSFUL;
910 SIVAL(p + 4, 0, modellen);
911 ok = data_blob_append(req, &blob, p, 8);
914 return NT_STATUS_UNSUCCESSFUL;
917 ok = data_blob_append(req, &blob, model, modellen);
920 return NT_STATUS_UNSUCCESSFUL;
924 status = smb2_create_blob_add(out_context_blobs,
926 SMB2_CREATE_TAG_AAPL,
928 if (NT_STATUS_IS_OK(status)) {
929 global_fruit_config.nego_aapl = true;
935 static bool readdir_attr_meta_finderi_stream(
936 struct vfs_handle_struct *handle,
937 const struct smb_filename *smb_fname,
940 struct smb_filename *stream_name = NULL;
941 files_struct *fsp = NULL;
945 uint8_t buf[AFP_INFO_SIZE];
947 status = synthetic_pathref(talloc_tos(),
948 handle->conn->cwd_fsp,
949 smb_fname->base_name,
955 if (!NT_STATUS_IS_OK(status)) {
959 status = SMB_VFS_CREATE_FILE(
960 handle->conn, /* conn */
963 stream_name, /* fname */
964 FILE_READ_DATA, /* access_mask */
965 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
967 FILE_OPEN, /* create_disposition*/
968 0, /* create_options */
969 0, /* file_attributes */
970 INTERNAL_OPEN_ONLY, /* oplock_request */
972 0, /* allocation_size */
973 0, /* private_flags */
978 NULL, NULL); /* create context */
980 TALLOC_FREE(stream_name);
982 if (!NT_STATUS_IS_OK(status)) {
986 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
987 if (nread != AFP_INFO_SIZE) {
988 DBG_ERR("short read [%s] [%zd/%d]\n",
989 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
994 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
1001 close_file_free(NULL, &fsp, NORMAL_CLOSE);
1007 static bool readdir_attr_meta_finderi_netatalk(
1008 struct vfs_handle_struct *handle,
1009 const struct smb_filename *smb_fname,
1012 struct adouble *ad = NULL;
1015 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
1020 p = ad_get_entry(ad, ADEID_FINDERI);
1022 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
1027 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1032 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1033 const struct smb_filename *smb_fname,
1034 struct readdir_attr_data *attr_data)
1036 struct fruit_config_data *config = NULL;
1037 uint32_t date_added;
1041 SMB_VFS_HANDLE_GET_DATA(handle, config,
1042 struct fruit_config_data,
1045 switch (config->meta) {
1046 case FRUIT_META_NETATALK:
1047 ok = readdir_attr_meta_finderi_netatalk(
1048 handle, smb_fname, &ai);
1051 case FRUIT_META_STREAM:
1052 ok = readdir_attr_meta_finderi_stream(
1053 handle, smb_fname, &ai);
1057 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1062 /* Don't bother with errors, it's likely ENOENT */
1066 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1068 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1069 &ai.afpi_FinderInfo[0], 4);
1071 /* finder_creator */
1072 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1073 &ai.afpi_FinderInfo[4], 4);
1077 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1078 &ai.afpi_FinderInfo[8], 2);
1080 /* finder_ext_flags */
1081 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1082 &ai.afpi_FinderInfo[24], 2);
1085 date_added = convert_time_t_to_uint32_t(
1086 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1088 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1093 static uint64_t readdir_attr_rfork_size_adouble(
1094 struct vfs_handle_struct *handle,
1095 const struct smb_filename *smb_fname)
1097 struct adouble *ad = NULL;
1098 uint64_t rfork_size;
1100 ad = ad_get(talloc_tos(), handle, smb_fname,
1106 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1112 static uint64_t readdir_attr_rfork_size_stream(
1113 struct vfs_handle_struct *handle,
1114 const struct smb_filename *smb_fname)
1116 struct smb_filename *stream_name = NULL;
1118 uint64_t rfork_size;
1120 stream_name = synthetic_smb_fname(talloc_tos(),
1121 smb_fname->base_name,
1122 AFPRESOURCE_STREAM_NAME,
1126 if (stream_name == NULL) {
1130 ret = SMB_VFS_STAT(handle->conn, stream_name);
1132 TALLOC_FREE(stream_name);
1136 rfork_size = stream_name->st.st_ex_size;
1137 TALLOC_FREE(stream_name);
1142 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1143 const struct smb_filename *smb_fname)
1145 struct fruit_config_data *config = NULL;
1146 uint64_t rfork_size;
1148 SMB_VFS_HANDLE_GET_DATA(handle, config,
1149 struct fruit_config_data,
1152 switch (config->rsrc) {
1153 case FRUIT_RSRC_ADFILE:
1154 rfork_size = readdir_attr_rfork_size_adouble(handle,
1158 case FRUIT_RSRC_XATTR:
1159 case FRUIT_RSRC_STREAM:
1160 rfork_size = readdir_attr_rfork_size_stream(handle,
1165 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1173 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1174 const struct smb_filename *smb_fname,
1175 struct readdir_attr_data *attr_data)
1177 NTSTATUS status = NT_STATUS_OK;
1178 struct fruit_config_data *config = NULL;
1181 SMB_VFS_HANDLE_GET_DATA(handle, config,
1182 struct fruit_config_data,
1183 return NT_STATUS_UNSUCCESSFUL);
1186 /* Ensure we return a default value in the creation_date field */
1187 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1190 * Resource fork length
1193 if (config->readdir_attr_rsize) {
1194 uint64_t rfork_size;
1196 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1197 attr_data->attr_data.aapl.rfork_size = rfork_size;
1204 if (config->readdir_attr_finder_info) {
1205 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1207 status = NT_STATUS_INTERNAL_ERROR;
1214 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1219 if (psd->dacl == NULL) {
1220 return NT_STATUS_OK;
1223 for (i = 0; i < psd->dacl->num_aces; i++) {
1224 /* MS NFS style mode/uid/gid */
1225 int cmp = dom_sid_compare_domain(
1226 &global_sid_Unix_NFS,
1227 &psd->dacl->aces[i].trustee);
1229 /* Normal ACE entry. */
1234 * security_descriptor_dacl_del()
1235 * *must* return NT_STATUS_OK as we know
1236 * we have something to remove.
1239 status = security_descriptor_dacl_del(psd,
1240 &psd->dacl->aces[i].trustee);
1241 if (!NT_STATUS_IS_OK(status)) {
1242 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1248 * security_descriptor_dacl_del() may delete more
1249 * then one entry subsequent to this one if the
1250 * SID matches, but we only need to ensure that
1251 * we stay looking at the same element in the array.
1255 return NT_STATUS_OK;
1258 /* Search MS NFS style ACE with UNIX mode */
1259 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1261 struct security_descriptor *psd,
1266 struct fruit_config_data *config = NULL;
1270 SMB_VFS_HANDLE_GET_DATA(handle, config,
1271 struct fruit_config_data,
1272 return NT_STATUS_UNSUCCESSFUL);
1274 if (!global_fruit_config.nego_aapl) {
1275 return NT_STATUS_OK;
1277 if (psd->dacl == NULL || !config->unix_info_enabled) {
1278 return NT_STATUS_OK;
1281 for (i = 0; i < psd->dacl->num_aces; i++) {
1282 if (dom_sid_compare_domain(
1283 &global_sid_Unix_NFS_Mode,
1284 &psd->dacl->aces[i].trustee) == 0) {
1285 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1286 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1289 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1290 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1296 * Remove any incoming virtual ACE entries generated by
1297 * fruit_fget_nt_acl().
1300 return remove_virtual_nfs_aces(psd);
1303 /****************************************************************************
1305 ****************************************************************************/
1307 static int fruit_connect(vfs_handle_struct *handle,
1308 const char *service,
1312 char *list = NULL, *newlist = NULL;
1313 struct fruit_config_data *config;
1314 const struct loadparm_substitution *lp_sub =
1315 loadparm_s3_global_substitution();
1317 DEBUG(10, ("fruit_connect\n"));
1319 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1324 rc = init_fruit_config(handle);
1329 SMB_VFS_HANDLE_GET_DATA(handle, config,
1330 struct fruit_config_data, return -1);
1332 if (config->veto_appledouble) {
1333 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1336 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1337 newlist = talloc_asprintf(
1339 "%s/" ADOUBLE_NAME_PREFIX "*/",
1341 lp_do_parameter(SNUM(handle->conn),
1346 lp_do_parameter(SNUM(handle->conn),
1348 "/" ADOUBLE_NAME_PREFIX "*/");
1354 if (config->encoding == FRUIT_ENC_NATIVE) {
1355 lp_do_parameter(SNUM(handle->conn),
1357 macos_string_replace_map);
1360 if (config->time_machine) {
1361 DBG_NOTICE("Enabling durable handles for Time Machine "
1362 "support on [%s]\n", service);
1363 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1364 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1365 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1366 if (!lp_strict_sync(SNUM(handle->conn))) {
1367 DBG_WARNING("Time Machine without strict sync is not "
1370 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1376 static void fio_ref_destroy_fn(void *p_data)
1378 struct fio *ref_fio = (struct fio *)p_data;
1379 if (ref_fio->real_fio != NULL) {
1380 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1381 ref_fio->real_fio->ad_fsp = NULL;
1382 ref_fio->real_fio = NULL;
1386 static void fio_close_ad_fsp(struct fio *fio)
1388 if (fio->ad_fsp != NULL) {
1389 fd_close(fio->ad_fsp);
1390 file_free(NULL, fio->ad_fsp);
1391 /* fio_ref_destroy_fn() should have cleared this */
1392 SMB_ASSERT(fio->ad_fsp == NULL);
1396 static void fio_destroy_fn(void *p_data)
1398 struct fio *fio = (struct fio *)p_data;
1399 fio_close_ad_fsp(fio);
1402 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1403 const struct files_struct *dirfsp,
1404 const struct smb_filename *smb_fname,
1409 struct fruit_config_data *config = NULL;
1410 struct fio *fio = NULL;
1411 struct vfs_open_how how = {
1412 .flags = flags & ~O_CREAT,
1417 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1419 SMB_VFS_HANDLE_GET_DATA(handle, config,
1420 struct fruit_config_data, return -1);
1422 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1423 fio->handle = handle;
1425 fio->type = ADOUBLE_META;
1426 fio->config = config;
1428 fd = SMB_VFS_NEXT_OPENAT(handle,
1437 if (!(flags & O_CREAT)) {
1438 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1444 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1448 fio->fake_fd = true;
1455 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1456 const struct files_struct *dirfsp,
1457 const struct smb_filename *smb_fname,
1462 struct fruit_config_data *config = NULL;
1463 struct fio *fio = NULL;
1464 struct adouble *ad = NULL;
1465 bool meta_exists = false;
1468 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1471 * We know this is a stream open, so fsp->base_fsp must
1474 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1475 SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1477 ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1484 if (!meta_exists && !(flags & O_CREAT)) {
1494 SMB_VFS_HANDLE_GET_DATA(handle, config,
1495 struct fruit_config_data, return -1);
1497 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1498 fio->handle = handle;
1500 fio->type = ADOUBLE_META;
1501 fio->config = config;
1502 fio->fake_fd = true;
1509 static int fruit_open_meta(vfs_handle_struct *handle,
1510 const struct files_struct *dirfsp,
1511 const struct smb_filename *smb_fname,
1512 files_struct *fsp, int flags, mode_t mode)
1515 struct fruit_config_data *config = NULL;
1517 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1519 SMB_VFS_HANDLE_GET_DATA(handle, config,
1520 struct fruit_config_data, return -1);
1522 switch (config->meta) {
1523 case FRUIT_META_STREAM:
1524 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1528 case FRUIT_META_NETATALK:
1529 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1534 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1538 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1543 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1544 const struct files_struct *dirfsp,
1545 const struct smb_filename *smb_fname,
1551 struct fruit_config_data *config = NULL;
1552 struct files_struct *ad_fsp = NULL;
1553 struct fio *fio = NULL;
1554 struct fio *ref_fio = NULL;
1558 SMB_VFS_HANDLE_GET_DATA(handle, config,
1559 struct fruit_config_data, return -1);
1561 if ((!(flags & O_CREAT)) &&
1562 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1564 /* sorry, but directories don't have a resource fork */
1571 * We return a fake_fd to the vfs modules above,
1572 * while we open an internal backend fsp for the
1573 * '._' file for the next vfs modules.
1575 * Note that adouble_open_from_base_fsp() recurses
1576 * into fruit_openat(), but it'll just pass to
1577 * the next module as just opens a flat file on
1587 status = adouble_open_from_base_fsp(fsp->conn->cwd_fsp,
1593 if (!NT_STATUS_IS_OK(status)) {
1594 errno = map_errno_from_nt_status(status);
1600 * Now we need to glue both handles together,
1601 * so that they automatically detach each other
1604 fio = fruit_get_complete_fio(handle, fsp);
1606 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1612 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1614 fio_ref_destroy_fn);
1615 if (ref_fio == NULL) {
1616 int saved_errno = errno;
1618 file_free(NULL, ad_fsp);
1620 errno = saved_errno;
1625 SMB_ASSERT(ref_fio->fsp == NULL);
1626 ref_fio->handle = handle;
1627 ref_fio->fsp = ad_fsp;
1628 ref_fio->type = ADOUBLE_RSRC;
1629 ref_fio->config = config;
1630 ref_fio->real_fio = fio;
1631 SMB_ASSERT(fio->ad_fsp == NULL);
1632 fio->ad_fsp = ad_fsp;
1633 fio->fake_fd = true;
1637 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1639 int saved_errno = errno;
1641 vfs_fake_fd_close(fd);
1643 errno = saved_errno;
1649 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1650 const struct files_struct *dirfsp,
1651 const struct smb_filename *smb_fname,
1656 #ifdef HAVE_ATTROPEN
1660 * As there's no attropenat() this is only going to work with AT_FDCWD.
1662 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1664 fd = attropen(smb_fname->base_name,
1665 AFPRESOURCE_EA_NETATALK,
1680 static int fruit_open_rsrc(vfs_handle_struct *handle,
1681 const struct files_struct *dirfsp,
1682 const struct smb_filename *smb_fname,
1683 files_struct *fsp, int flags, mode_t mode)
1686 struct fruit_config_data *config = NULL;
1687 struct fio *fio = NULL;
1689 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1691 SMB_VFS_HANDLE_GET_DATA(handle, config,
1692 struct fruit_config_data, return -1);
1694 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1695 fio->handle = handle;
1697 fio->type = ADOUBLE_RSRC;
1698 fio->config = config;
1700 switch (config->rsrc) {
1701 case FRUIT_RSRC_STREAM: {
1702 struct vfs_open_how how = {
1703 .flags = flags, .mode = mode,
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,
1742 const struct vfs_open_how *how)
1746 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1748 if (!is_named_stream(smb_fname)) {
1749 return SMB_VFS_NEXT_OPENAT(handle,
1756 if (how->resolve != 0) {
1761 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1763 if (is_afpinfo_stream(smb_fname->stream_name)) {
1764 fd = fruit_open_meta(handle,
1770 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1771 fd = fruit_open_rsrc(handle,
1778 fd = SMB_VFS_NEXT_OPENAT(handle,
1785 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1787 /* Prevent reopen optimisation */
1788 fsp->fsp_flags.have_proc_fds = false;
1792 static int fruit_close_meta(vfs_handle_struct *handle,
1796 struct fruit_config_data *config = NULL;
1798 SMB_VFS_HANDLE_GET_DATA(handle, config,
1799 struct fruit_config_data, return -1);
1801 switch (config->meta) {
1802 case FRUIT_META_STREAM:
1804 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1809 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1810 fsp_set_fd(fsp, -1);
1812 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1816 case FRUIT_META_NETATALK:
1817 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1818 fsp_set_fd(fsp, -1);
1822 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1830 static int fruit_close_rsrc(vfs_handle_struct *handle,
1834 struct fruit_config_data *config = NULL;
1836 SMB_VFS_HANDLE_GET_DATA(handle, config,
1837 struct fruit_config_data, return -1);
1839 switch (config->rsrc) {
1840 case FRUIT_RSRC_STREAM:
1841 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1844 case FRUIT_RSRC_ADFILE:
1846 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1850 fio_close_ad_fsp(fio);
1851 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1852 fsp_set_fd(fsp, -1);
1856 case FRUIT_RSRC_XATTR:
1857 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1858 fsp_set_fd(fsp, -1);
1862 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1869 static int fruit_close(vfs_handle_struct *handle,
1875 fd = fsp_get_pathref_fd(fsp);
1877 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1879 if (!fsp_is_alternate_stream(fsp)) {
1880 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1883 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1884 ret = fruit_close_meta(handle, fsp);
1885 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1886 ret = fruit_close_rsrc(handle, fsp);
1888 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1894 static int fruit_renameat(struct vfs_handle_struct *handle,
1895 files_struct *srcfsp,
1896 const struct smb_filename *smb_fname_src,
1897 files_struct *dstfsp,
1898 const struct smb_filename *smb_fname_dst)
1901 struct fruit_config_data *config = NULL;
1902 struct smb_filename *src_adp_smb_fname = NULL;
1903 struct smb_filename *dst_adp_smb_fname = NULL;
1905 SMB_VFS_HANDLE_GET_DATA(handle, config,
1906 struct fruit_config_data, return -1);
1908 if (!VALID_STAT(smb_fname_src->st)) {
1909 DBG_ERR("Need valid stat for [%s]\n",
1910 smb_fname_str_dbg(smb_fname_src));
1914 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1923 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1924 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1929 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1934 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1939 DBG_DEBUG("%s -> %s\n",
1940 smb_fname_str_dbg(src_adp_smb_fname),
1941 smb_fname_str_dbg(dst_adp_smb_fname));
1943 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1948 if (errno == ENOENT) {
1953 TALLOC_FREE(src_adp_smb_fname);
1954 TALLOC_FREE(dst_adp_smb_fname);
1958 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1959 struct files_struct *dirfsp,
1960 const struct smb_filename *smb_fname)
1962 return SMB_VFS_NEXT_UNLINKAT(handle,
1968 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1969 const struct smb_filename *smb_fname)
1971 SMB_ASSERT(smb_fname->fsp != NULL);
1972 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1973 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1974 AFPINFO_EA_NETATALK);
1977 static int fruit_unlink_meta(vfs_handle_struct *handle,
1978 struct files_struct *dirfsp,
1979 const struct smb_filename *smb_fname)
1981 struct fruit_config_data *config = NULL;
1984 SMB_VFS_HANDLE_GET_DATA(handle, config,
1985 struct fruit_config_data, return -1);
1987 switch (config->meta) {
1988 case FRUIT_META_STREAM:
1989 rc = fruit_unlink_meta_stream(handle,
1994 case FRUIT_META_NETATALK:
1995 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1999 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2006 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2007 struct files_struct *dirfsp,
2008 const struct smb_filename *smb_fname,
2013 if (!force_unlink) {
2014 struct smb_filename *full_fname = NULL;
2018 * TODO: use SMB_VFS_STATX() once we have it.
2021 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2024 if (full_fname == NULL) {
2029 * 0 byte resource fork streams are not listed by
2030 * vfs_streaminfo, as a result stream cleanup/deletion of file
2031 * deletion doesn't remove the resourcefork stream.
2034 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2036 TALLOC_FREE(full_fname);
2037 DBG_ERR("stat [%s] failed [%s]\n",
2038 smb_fname_str_dbg(full_fname), strerror(errno));
2042 size = full_fname->st.st_ex_size;
2043 TALLOC_FREE(full_fname);
2046 /* OS X ignores resource fork stream delete requests */
2051 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2055 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2062 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2063 struct files_struct *dirfsp,
2064 const struct smb_filename *smb_fname,
2068 struct adouble *ad = NULL;
2069 struct smb_filename *adp_smb_fname = NULL;
2071 if (!force_unlink) {
2072 struct smb_filename *full_fname = NULL;
2074 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2077 if (full_fname == NULL) {
2081 ad = ad_get(talloc_tos(), handle, full_fname,
2083 TALLOC_FREE(full_fname);
2091 * 0 byte resource fork streams are not listed by
2092 * vfs_streaminfo, as a result stream cleanup/deletion of file
2093 * deletion doesn't remove the resourcefork stream.
2096 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2097 /* OS X ignores resource fork stream delete requests */
2105 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2110 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2114 TALLOC_FREE(adp_smb_fname);
2115 if ((rc != 0) && (errno == ENOENT || errno == ENAMETOOLONG) && force_unlink) {
2122 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2123 const struct smb_filename *smb_fname,
2127 * OS X ignores resource fork stream delete requests, so nothing to do
2128 * here. Removing the file will remove the xattr anyway, so we don't
2129 * have to take care of removing 0 byte resource forks that could be
2135 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2136 struct files_struct *dirfsp,
2137 const struct smb_filename *smb_fname,
2140 struct fruit_config_data *config = NULL;
2143 SMB_VFS_HANDLE_GET_DATA(handle, config,
2144 struct fruit_config_data, return -1);
2146 switch (config->rsrc) {
2147 case FRUIT_RSRC_STREAM:
2148 rc = fruit_unlink_rsrc_stream(handle,
2154 case FRUIT_RSRC_ADFILE:
2155 rc = fruit_unlink_rsrc_adouble(handle,
2161 case FRUIT_RSRC_XATTR:
2162 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2166 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2173 static int fruit_fchmod(vfs_handle_struct *handle,
2174 struct files_struct *fsp,
2178 struct fruit_config_data *config = NULL;
2179 struct smb_filename *smb_fname_adp = NULL;
2180 const struct smb_filename *smb_fname = NULL;
2183 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2188 smb_fname = fsp->fsp_name;
2189 SMB_VFS_HANDLE_GET_DATA(handle, config,
2190 struct fruit_config_data, return -1);
2192 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2196 if (!VALID_STAT(smb_fname->st)) {
2200 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2204 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2209 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2211 if (!NT_STATUS_IS_OK(status)) {
2212 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2213 if (NT_STATUS_EQUAL(status,
2214 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2222 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2224 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2225 if (errno == ENOENT) {
2229 TALLOC_FREE(smb_fname_adp);
2233 static int fruit_unlinkat(vfs_handle_struct *handle,
2234 struct files_struct *dirfsp,
2235 const struct smb_filename *smb_fname,
2238 struct fruit_config_data *config = NULL;
2239 struct smb_filename *rsrc_smb_fname = NULL;
2242 if (flags & AT_REMOVEDIR) {
2243 return SMB_VFS_NEXT_UNLINKAT(handle,
2249 SMB_VFS_HANDLE_GET_DATA(handle, config,
2250 struct fruit_config_data, return -1);
2252 if (is_afpinfo_stream(smb_fname->stream_name)) {
2253 return fruit_unlink_meta(handle,
2256 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2257 return fruit_unlink_rsrc(handle,
2261 } else if (is_named_stream(smb_fname)) {
2262 return SMB_VFS_NEXT_UNLINKAT(handle,
2266 } else if (is_adouble_file(smb_fname->base_name)) {
2267 return SMB_VFS_NEXT_UNLINKAT(handle,
2274 * A request to delete the base file. Because 0 byte resource
2275 * fork streams are not listed by fruit_streaminfo,
2276 * delete_all_streams() can't remove 0 byte resource fork
2277 * streams, so we have to cleanup this here.
2279 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2280 smb_fname->base_name,
2281 AFPRESOURCE_STREAM_NAME,
2285 if (rsrc_smb_fname == NULL) {
2289 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2290 if ((ret != 0) && (errno != ENOENT)) {
2291 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2292 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2293 TALLOC_FREE(rsrc_smb_fname);
2296 TALLOC_FREE(rsrc_smb_fname);
2298 return SMB_VFS_NEXT_UNLINKAT(handle,
2304 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2305 files_struct *fsp, void *data,
2306 size_t n, off_t offset)
2308 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2312 if ((fio == NULL) || fio->fake_fd) {
2316 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2317 if (nread == -1 || nread == n) {
2321 DBG_ERR("Removing [%s] after short read [%zd]\n",
2322 fsp_str_dbg(fsp), nread);
2324 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2329 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2337 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2338 files_struct *fsp, void *data,
2339 size_t n, off_t offset)
2342 struct adouble *ad = NULL;
2343 char afpinfo_buf[AFP_INFO_SIZE];
2347 ai = afpinfo_new(talloc_tos());
2352 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2358 p = ad_get_entry(ad, ADEID_FINDERI);
2360 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2365 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2367 nread = afpinfo_pack(ai, afpinfo_buf);
2368 if (nread != AFP_INFO_SIZE) {
2373 memcpy(data, afpinfo_buf, n);
2381 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2382 files_struct *fsp, void *data,
2383 size_t n, off_t offset)
2385 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2390 * OS X has a off-by-1 error in the offset calculation, so we're
2391 * bug compatible here. It won't hurt, as any relevant real
2392 * world read requests from the AFP_AfpInfo stream will be
2393 * offset=0 n=60. offset is ignored anyway, see below.
2395 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2400 DBG_ERR("Failed to fetch fsp extension\n");
2404 /* Yes, macOS always reads from offset 0 */
2406 to_return = MIN(n, AFP_INFO_SIZE);
2408 switch (fio->config->meta) {
2409 case FRUIT_META_STREAM:
2410 nread = fruit_pread_meta_stream(handle, fsp, data,
2414 case FRUIT_META_NETATALK:
2415 nread = fruit_pread_meta_adouble(handle, fsp, data,
2420 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2424 if (nread == -1 && fio->fake_fd) {
2426 char afpinfo_buf[AFP_INFO_SIZE];
2428 ai = afpinfo_new(talloc_tos());
2433 nread = afpinfo_pack(ai, afpinfo_buf);
2435 if (nread != AFP_INFO_SIZE) {
2439 memcpy(data, afpinfo_buf, to_return);
2446 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2447 files_struct *fsp, void *data,
2448 size_t n, off_t offset)
2450 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2453 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2454 files_struct *fsp, void *data,
2455 size_t n, off_t offset)
2457 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2460 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2461 files_struct *fsp, void *data,
2462 size_t n, off_t offset)
2464 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2465 struct adouble *ad = NULL;
2468 if (fio == NULL || fio->ad_fsp == NULL) {
2469 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2474 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2476 DBG_ERR("ad_fget [%s] failed [%s]\n",
2477 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2481 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2482 offset + ad_getentryoff(ad, ADEID_RFORK));
2488 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2489 files_struct *fsp, void *data,
2490 size_t n, off_t offset)
2492 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2500 switch (fio->config->rsrc) {
2501 case FRUIT_RSRC_STREAM:
2502 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2505 case FRUIT_RSRC_ADFILE:
2506 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2509 case FRUIT_RSRC_XATTR:
2510 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2514 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2521 static ssize_t fruit_pread(vfs_handle_struct *handle,
2522 files_struct *fsp, void *data,
2523 size_t n, off_t offset)
2525 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2528 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2529 fsp_str_dbg(fsp), (intmax_t)offset, n);
2532 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2535 if (fio->type == ADOUBLE_META) {
2536 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2538 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2541 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2545 static bool fruit_must_handle_aio_stream(struct fio *fio)
2551 if (fio->type == ADOUBLE_META) {
2555 if ((fio->type == ADOUBLE_RSRC) &&
2556 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2564 struct fruit_pread_state {
2566 struct vfs_aio_state vfs_aio_state;
2569 static void fruit_pread_done(struct tevent_req *subreq);
2571 static struct tevent_req *fruit_pread_send(
2572 struct vfs_handle_struct *handle,
2573 TALLOC_CTX *mem_ctx,
2574 struct tevent_context *ev,
2575 struct files_struct *fsp,
2577 size_t n, off_t offset)
2579 struct tevent_req *req = NULL;
2580 struct tevent_req *subreq = NULL;
2581 struct fruit_pread_state *state = NULL;
2582 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2584 req = tevent_req_create(mem_ctx, &state,
2585 struct fruit_pread_state);
2590 if (fruit_must_handle_aio_stream(fio)) {
2591 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2592 if (state->nread != n) {
2593 if (state->nread != -1) {
2596 tevent_req_error(req, errno);
2597 return tevent_req_post(req, ev);
2599 tevent_req_done(req);
2600 return tevent_req_post(req, ev);
2603 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2605 if (tevent_req_nomem(req, subreq)) {
2606 return tevent_req_post(req, ev);
2608 tevent_req_set_callback(subreq, fruit_pread_done, req);
2612 static void fruit_pread_done(struct tevent_req *subreq)
2614 struct tevent_req *req = tevent_req_callback_data(
2615 subreq, struct tevent_req);
2616 struct fruit_pread_state *state = tevent_req_data(
2617 req, struct fruit_pread_state);
2619 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2620 TALLOC_FREE(subreq);
2622 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2625 tevent_req_done(req);
2628 static ssize_t fruit_pread_recv(struct tevent_req *req,
2629 struct vfs_aio_state *vfs_aio_state)
2631 struct fruit_pread_state *state = tevent_req_data(
2632 req, struct fruit_pread_state);
2633 ssize_t retval = -1;
2635 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2636 tevent_req_received(req);
2640 *vfs_aio_state = state->vfs_aio_state;
2641 retval = state->nread;
2642 tevent_req_received(req);
2646 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2647 files_struct *fsp, const void *indata,
2648 size_t n, off_t offset)
2650 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2651 const void *data = indata;
2652 char afpinfo_buf[AFP_INFO_SIZE];
2658 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2659 fsp_str_dbg(fsp), (intmax_t)offset, n);
2666 struct vfs_open_how how = {
2667 .flags = fio->flags, .mode = fio->mode,
2669 int fd = fsp_get_pathref_fd(fsp);
2671 ret = vfs_fake_fd_close(fd);
2672 fsp_set_fd(fsp, -1);
2674 DBG_ERR("Close [%s] failed: %s\n",
2675 fsp_str_dbg(fsp), strerror(errno));
2679 fd = SMB_VFS_NEXT_OPENAT(handle,
2680 NULL, /* opening a stream */
2685 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2686 fsp_str_dbg(fsp), strerror(errno));
2689 fsp_set_fd(fsp, fd);
2690 fio->fake_fd = false;
2693 ai = afpinfo_unpack(talloc_tos(), data, fio->config->validate_afpinfo);
2698 if (ai_empty_finderinfo(ai)) {
2700 * Writing an all 0 blob to the metadata stream results in the
2701 * stream being removed on a macOS server. This ensures we
2702 * behave the same and it verified by the "delete AFP_AfpInfo by
2703 * writing all 0" test.
2705 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2707 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2712 ok = set_delete_on_close(
2715 handle->conn->session_info->security_token,
2716 handle->conn->session_info->unix_token);
2718 DBG_ERR("set_delete_on_close on [%s] failed\n",
2725 if (!fio->config->validate_afpinfo) {
2727 * Ensure the buffer contains a valid header, so marshall
2728 * the data from the afpinfo struck back into a buffer
2729 * and write that instead of the possibly malformed data
2730 * we got from the client.
2732 nwritten = afpinfo_pack(ai, afpinfo_buf);
2733 if (nwritten != AFP_INFO_SIZE) {
2740 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2741 if (nwritten != n) {
2748 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2749 files_struct *fsp, const void *data,
2750 size_t n, off_t offset)
2752 struct fruit_config_data *config = NULL;
2753 struct adouble *ad = NULL;
2759 SMB_VFS_HANDLE_GET_DATA(handle, config,
2760 struct fruit_config_data, return -1);
2762 ai = afpinfo_unpack(talloc_tos(), data, config->validate_afpinfo);
2767 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2769 ad = ad_init(talloc_tos(), ADOUBLE_META);
2774 p = ad_get_entry(ad, ADEID_FINDERI);
2776 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2781 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2783 ret = ad_fset(handle, ad, fsp);
2785 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2792 if (!ai_empty_finderinfo(ai)) {
2797 * Writing an all 0 blob to the metadata stream results in the stream
2798 * being removed on a macOS server. This ensures we behave the same and
2799 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2802 ok = set_delete_on_close(
2805 handle->conn->session_info->security_token,
2806 handle->conn->session_info->unix_token);
2808 DBG_ERR("set_delete_on_close on [%s] failed\n",
2816 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2817 files_struct *fsp, const void *data,
2818 size_t n, off_t offset)
2820 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2822 uint8_t buf[AFP_INFO_SIZE];
2828 DBG_ERR("Failed to fetch fsp extension\n");
2837 if (offset != 0 && n < 60) {
2842 if (fio->config->validate_afpinfo) {
2843 cmp = memcmp(data, "AFP", 3);
2850 if (n <= AFP_OFF_FinderInfo) {
2852 * Nothing to do here really, just return
2860 if (to_copy > AFP_INFO_SIZE) {
2861 to_copy = AFP_INFO_SIZE;
2863 memcpy(buf, data, to_copy);
2866 if (to_write != AFP_INFO_SIZE) {
2867 to_write = AFP_INFO_SIZE;
2870 switch (fio->config->meta) {
2871 case FRUIT_META_STREAM:
2872 nwritten = fruit_pwrite_meta_stream(handle,
2879 case FRUIT_META_NETATALK:
2880 nwritten = fruit_pwrite_meta_netatalk(handle,
2888 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2892 if (nwritten != to_write) {
2897 * Return the requested amount, verified against macOS SMB server
2902 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2903 files_struct *fsp, const void *data,
2904 size_t n, off_t offset)
2906 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2909 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2910 files_struct *fsp, const void *data,
2911 size_t n, off_t offset)
2913 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2916 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2917 files_struct *fsp, const void *data,
2918 size_t n, off_t offset)
2920 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2921 struct adouble *ad = NULL;
2925 if (fio == NULL || fio->ad_fsp == NULL) {
2926 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2931 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2933 DBG_ERR("ad_fget [%s] failed [%s]\n",
2934 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2938 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2939 offset + ad_getentryoff(ad, ADEID_RFORK));
2940 if (nwritten != n) {
2941 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2942 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2947 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2948 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2949 ret = ad_fset(handle, ad, fio->ad_fsp);
2951 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2961 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2962 files_struct *fsp, const void *data,
2963 size_t n, off_t offset)
2965 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2969 DBG_ERR("Failed to fetch fsp extension\n");
2973 switch (fio->config->rsrc) {
2974 case FRUIT_RSRC_STREAM:
2975 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2978 case FRUIT_RSRC_ADFILE:
2979 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2982 case FRUIT_RSRC_XATTR:
2983 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2987 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2994 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2995 files_struct *fsp, const void *data,
2996 size_t n, off_t offset)
2998 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3001 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3002 fsp_str_dbg(fsp), (intmax_t)offset, n);
3005 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3008 if (fio->type == ADOUBLE_META) {
3009 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
3011 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
3014 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
3018 struct fruit_pwrite_state {
3020 struct vfs_aio_state vfs_aio_state;
3023 static void fruit_pwrite_done(struct tevent_req *subreq);
3025 static struct tevent_req *fruit_pwrite_send(
3026 struct vfs_handle_struct *handle,
3027 TALLOC_CTX *mem_ctx,
3028 struct tevent_context *ev,
3029 struct files_struct *fsp,
3031 size_t n, off_t offset)
3033 struct tevent_req *req = NULL;
3034 struct tevent_req *subreq = NULL;
3035 struct fruit_pwrite_state *state = NULL;
3036 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3038 req = tevent_req_create(mem_ctx, &state,
3039 struct fruit_pwrite_state);
3044 if (fruit_must_handle_aio_stream(fio)) {
3045 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3046 if (state->nwritten != n) {
3047 if (state->nwritten != -1) {
3050 tevent_req_error(req, errno);
3051 return tevent_req_post(req, ev);
3053 tevent_req_done(req);
3054 return tevent_req_post(req, ev);
3057 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3059 if (tevent_req_nomem(req, subreq)) {
3060 return tevent_req_post(req, ev);
3062 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3066 static void fruit_pwrite_done(struct tevent_req *subreq)
3068 struct tevent_req *req = tevent_req_callback_data(
3069 subreq, struct tevent_req);
3070 struct fruit_pwrite_state *state = tevent_req_data(
3071 req, struct fruit_pwrite_state);
3073 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3074 TALLOC_FREE(subreq);
3076 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3079 tevent_req_done(req);
3082 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3083 struct vfs_aio_state *vfs_aio_state)
3085 struct fruit_pwrite_state *state = tevent_req_data(
3086 req, struct fruit_pwrite_state);
3087 ssize_t retval = -1;
3089 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3090 tevent_req_received(req);
3094 *vfs_aio_state = state->vfs_aio_state;
3095 retval = state->nwritten;
3096 tevent_req_received(req);
3100 struct fruit_fsync_state {
3102 struct vfs_aio_state vfs_aio_state;
3105 static void fruit_fsync_done(struct tevent_req *subreq);
3107 static struct tevent_req *fruit_fsync_send(
3108 struct vfs_handle_struct *handle,
3109 TALLOC_CTX *mem_ctx,
3110 struct tevent_context *ev,
3111 struct files_struct *fsp)
3113 struct tevent_req *req = NULL;
3114 struct tevent_req *subreq = NULL;
3115 struct fruit_fsync_state *state = NULL;
3116 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3118 req = tevent_req_create(mem_ctx, &state,
3119 struct fruit_fsync_state);
3124 if (fruit_must_handle_aio_stream(fio)) {
3125 struct adouble *ad = NULL;
3127 if (fio->type == ADOUBLE_META) {
3129 * We must never pass a fake_fd
3130 * to lower level fsync calls.
3131 * Everything is already done
3132 * synchronously, so just return
3135 SMB_ASSERT(fio->fake_fd);
3136 tevent_req_done(req);
3137 return tevent_req_post(req, ev);
3141 * We know the following must be true,
3142 * as it's the condition for fruit_must_handle_aio_stream()
3143 * to return true if fio->type == ADOUBLE_RSRC.
3145 SMB_ASSERT(fio->config->rsrc == FRUIT_RSRC_ADFILE);
3146 if (fio->ad_fsp == NULL) {
3147 tevent_req_error(req, EBADF);
3148 return tevent_req_post(req, ev);
3150 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3152 tevent_req_error(req, ENOMEM);
3153 return tevent_req_post(req, ev);
3158 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
3159 if (tevent_req_nomem(req, subreq)) {
3160 return tevent_req_post(req, ev);
3162 tevent_req_set_callback(subreq, fruit_fsync_done, req);
3166 static void fruit_fsync_done(struct tevent_req *subreq)
3168 struct tevent_req *req = tevent_req_callback_data(
3169 subreq, struct tevent_req);
3170 struct fruit_fsync_state *state = tevent_req_data(
3171 req, struct fruit_fsync_state);
3173 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
3174 TALLOC_FREE(subreq);
3175 if (state->ret != 0) {
3176 tevent_req_error(req, errno);
3179 tevent_req_done(req);
3182 static int fruit_fsync_recv(struct tevent_req *req,
3183 struct vfs_aio_state *vfs_aio_state)
3185 struct fruit_fsync_state *state = tevent_req_data(
3186 req, struct fruit_fsync_state);
3189 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3190 tevent_req_received(req);
3194 *vfs_aio_state = state->vfs_aio_state;
3195 retval = state->ret;
3196 tevent_req_received(req);
3201 * Helper to stat/lstat the base file of an smb_fname.
3203 static int fruit_stat_base(vfs_handle_struct *handle,
3204 struct smb_filename *smb_fname,
3207 char *tmp_stream_name;
3210 tmp_stream_name = smb_fname->stream_name;
3211 smb_fname->stream_name = NULL;
3213 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3215 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3217 smb_fname->stream_name = tmp_stream_name;
3219 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3220 smb_fname->base_name,
3221 (uintmax_t)smb_fname->st.st_ex_dev,
3222 (uintmax_t)smb_fname->st.st_ex_ino);
3226 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3227 struct smb_filename *smb_fname,
3233 ret = fruit_stat_base(handle, smb_fname, false);
3238 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3241 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3243 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3246 smb_fname->st.st_ex_ino = ino;
3251 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3252 struct smb_filename *smb_fname,
3255 struct adouble *ad = NULL;
3257 /* Populate the stat struct with info from the base file. */
3258 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3262 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3264 DBG_INFO("fruit_stat_meta %s: %s\n",
3265 smb_fname_str_dbg(smb_fname), strerror(errno));
3271 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3272 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3273 smb_fname->stream_name);
3277 static int fruit_stat_meta(vfs_handle_struct *handle,
3278 struct smb_filename *smb_fname,
3281 struct fruit_config_data *config = NULL;
3284 SMB_VFS_HANDLE_GET_DATA(handle, config,
3285 struct fruit_config_data, return -1);
3287 switch (config->meta) {
3288 case FRUIT_META_STREAM:
3289 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3292 case FRUIT_META_NETATALK:
3293 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3297 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3304 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3305 struct smb_filename *smb_fname,
3308 struct adouble *ad = NULL;
3311 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3317 /* Populate the stat struct with info from the base file. */
3318 ret = fruit_stat_base(handle, smb_fname, follow_links);
3324 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3325 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3326 smb_fname->stream_name);
3331 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3332 struct smb_filename *smb_fname,
3338 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3340 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3346 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3347 struct smb_filename *smb_fname,
3350 #ifdef HAVE_ATTROPEN
3354 /* Populate the stat struct with info from the base file. */
3355 ret = fruit_stat_base(handle, smb_fname, follow_links);
3360 fd = attropen(smb_fname->base_name,
3361 AFPRESOURCE_EA_NETATALK,
3367 ret = sys_fstat(fd, &smb_fname->st, false);
3370 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3371 AFPRESOURCE_EA_NETATALK);
3377 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3378 smb_fname->stream_name);
3388 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3389 struct smb_filename *smb_fname,
3392 struct fruit_config_data *config = NULL;
3395 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3397 SMB_VFS_HANDLE_GET_DATA(handle, config,
3398 struct fruit_config_data, return -1);
3400 switch (config->rsrc) {
3401 case FRUIT_RSRC_STREAM:
3402 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3405 case FRUIT_RSRC_XATTR:
3406 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3409 case FRUIT_RSRC_ADFILE:
3410 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3414 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3421 static int fruit_stat(vfs_handle_struct *handle,
3422 struct smb_filename *smb_fname)
3426 DEBUG(10, ("fruit_stat called for %s\n",
3427 smb_fname_str_dbg(smb_fname)));
3429 if (!is_named_stream(smb_fname)) {
3430 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3432 update_btime(handle, smb_fname);
3438 * Note if lp_posix_paths() is true, we can never
3439 * get here as is_ntfs_stream_smb_fname() is
3440 * always false. So we never need worry about
3441 * not following links here.
3444 if (is_afpinfo_stream(smb_fname->stream_name)) {
3445 rc = fruit_stat_meta(handle, smb_fname, true);
3446 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3447 rc = fruit_stat_rsrc(handle, smb_fname, true);
3449 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3453 update_btime(handle, smb_fname);
3454 smb_fname->st.st_ex_mode &= ~S_IFMT;
3455 smb_fname->st.st_ex_mode |= S_IFREG;
3456 smb_fname->st.st_ex_blocks =
3457 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3462 static int fruit_lstat(vfs_handle_struct *handle,
3463 struct smb_filename *smb_fname)
3467 DEBUG(10, ("fruit_lstat called for %s\n",
3468 smb_fname_str_dbg(smb_fname)));
3470 if (!is_named_stream(smb_fname)) {
3471 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3473 update_btime(handle, smb_fname);
3478 if (is_afpinfo_stream(smb_fname->stream_name)) {
3479 rc = fruit_stat_meta(handle, smb_fname, false);
3480 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3481 rc = fruit_stat_rsrc(handle, smb_fname, false);
3483 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3487 update_btime(handle, smb_fname);
3488 smb_fname->st.st_ex_mode &= ~S_IFMT;
3489 smb_fname->st.st_ex_mode |= S_IFREG;
3490 smb_fname->st.st_ex_blocks =
3491 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3496 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3498 SMB_STRUCT_STAT *sbuf)
3500 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3501 struct smb_filename smb_fname;
3510 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3515 *sbuf = fsp->base_fsp->fsp_name->st;
3516 sbuf->st_ex_size = AFP_INFO_SIZE;
3517 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3521 smb_fname = (struct smb_filename) {
3522 .base_name = fsp->fsp_name->base_name,
3523 .twrp = fsp->fsp_name->twrp,
3526 ret = fruit_stat_base(handle, &smb_fname, false);
3530 *sbuf = smb_fname.st;
3532 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3534 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3539 sbuf->st_ex_ino = ino;
3543 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3545 SMB_STRUCT_STAT *sbuf)
3549 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3554 *sbuf = fsp->base_fsp->fsp_name->st;
3555 sbuf->st_ex_size = AFP_INFO_SIZE;
3556 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3561 static int fruit_fstat_meta(vfs_handle_struct *handle,
3563 SMB_STRUCT_STAT *sbuf,
3568 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3570 switch (fio->config->meta) {
3571 case FRUIT_META_STREAM:
3572 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3575 case FRUIT_META_NETATALK:
3576 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3580 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3584 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3588 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3590 SMB_STRUCT_STAT *sbuf)
3592 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3595 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3597 SMB_STRUCT_STAT *sbuf)
3599 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3602 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3604 SMB_STRUCT_STAT *sbuf)
3606 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3607 struct adouble *ad = NULL;
3610 if (fio == NULL || fio->ad_fsp == NULL) {
3611 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3616 /* Populate the stat struct with info from the base file. */
3617 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3622 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3624 DBG_ERR("ad_fget [%s] failed [%s]\n",
3625 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3629 *sbuf = fsp->base_fsp->fsp_name->st;
3630 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3631 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3637 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3638 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3642 switch (fio->config->rsrc) {
3643 case FRUIT_RSRC_STREAM:
3644 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3647 case FRUIT_RSRC_ADFILE:
3648 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3651 case FRUIT_RSRC_XATTR:
3652 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3656 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3663 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3664 SMB_STRUCT_STAT *sbuf)
3666 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3670 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3673 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3675 if (fio->type == ADOUBLE_META) {
3676 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3678 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3682 sbuf->st_ex_mode &= ~S_IFMT;
3683 sbuf->st_ex_mode |= S_IFREG;
3684 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3687 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3688 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3692 static NTSTATUS delete_invalid_meta_stream(
3693 vfs_handle_struct *handle,
3694 const struct smb_filename *smb_fname,
3695 TALLOC_CTX *mem_ctx,
3696 unsigned int *pnum_streams,
3697 struct stream_struct **pstreams,
3700 struct smb_filename *sname = NULL;
3705 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3707 return NT_STATUS_INTERNAL_ERROR;
3711 return NT_STATUS_OK;
3714 status = synthetic_pathref(talloc_tos(),
3715 handle->conn->cwd_fsp,
3716 smb_fname->base_name,
3717 AFPINFO_STREAM_NAME,
3722 if (!NT_STATUS_IS_OK(status)) {
3723 return NT_STATUS_NO_MEMORY;
3726 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3727 handle->conn->cwd_fsp,
3731 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3733 return map_nt_error_from_unix(errno);
3737 return NT_STATUS_OK;
3740 static NTSTATUS fruit_streaminfo_meta_stream(
3741 vfs_handle_struct *handle,
3742 struct files_struct *fsp,
3743 const struct smb_filename *smb_fname,
3744 TALLOC_CTX *mem_ctx,
3745 unsigned int *pnum_streams,
3746 struct stream_struct **pstreams)
3748 struct stream_struct *stream = *pstreams;
3749 unsigned int num_streams = *pnum_streams;
3752 for (i = 0; i < num_streams; i++) {
3753 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3758 if (i == num_streams) {
3759 return NT_STATUS_OK;
3762 if (stream[i].size != AFP_INFO_SIZE) {
3763 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3764 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3766 return delete_invalid_meta_stream(handle,
3775 return NT_STATUS_OK;
3778 static NTSTATUS fruit_streaminfo_meta_netatalk(
3779 vfs_handle_struct *handle,
3780 struct files_struct *fsp,
3781 const struct smb_filename *smb_fname,
3782 TALLOC_CTX *mem_ctx,
3783 unsigned int *pnum_streams,
3784 struct stream_struct **pstreams)
3786 struct stream_struct *stream = *pstreams;
3787 unsigned int num_streams = *pnum_streams;
3788 struct adouble *ad = NULL;
3793 /* Remove the Netatalk xattr from the list */
3794 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3795 ":" NETATALK_META_XATTR ":$DATA");
3797 return NT_STATUS_NO_MEMORY;
3801 * Check if there's a AFPINFO_STREAM from the VFS streams
3802 * backend and if yes, remove it from the list
3804 for (i = 0; i < num_streams; i++) {
3805 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3810 if (i < num_streams) {
3811 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3812 smb_fname_str_dbg(smb_fname));
3814 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3817 return NT_STATUS_INTERNAL_ERROR;
3821 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3823 return NT_STATUS_OK;
3826 is_fi_empty = ad_empty_finderinfo(ad);
3830 return NT_STATUS_OK;
3833 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3834 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3835 smb_roundup(handle->conn, AFP_INFO_SIZE));
3837 return NT_STATUS_NO_MEMORY;
3840 return NT_STATUS_OK;
3843 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3844 struct files_struct *fsp,
3845 const struct smb_filename *smb_fname,
3846 TALLOC_CTX *mem_ctx,
3847 unsigned int *pnum_streams,
3848 struct stream_struct **pstreams)
3850 struct fruit_config_data *config = NULL;
3853 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3854 return NT_STATUS_INTERNAL_ERROR);
3856 switch (config->meta) {
3857 case FRUIT_META_NETATALK:
3858 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3859 mem_ctx, pnum_streams,
3863 case FRUIT_META_STREAM:
3864 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3865 mem_ctx, pnum_streams,
3870 return NT_STATUS_INTERNAL_ERROR;
3876 static NTSTATUS fruit_streaminfo_rsrc_stream(
3877 vfs_handle_struct *handle,
3878 struct files_struct *fsp,
3879 const struct smb_filename *smb_fname,
3880 TALLOC_CTX *mem_ctx,
3881 unsigned int *pnum_streams,
3882 struct stream_struct **pstreams)
3886 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3888 DBG_ERR("Filtering resource stream failed\n");
3889 return NT_STATUS_INTERNAL_ERROR;
3891 return NT_STATUS_OK;
3894 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3895 vfs_handle_struct *handle,
3896 struct files_struct *fsp,
3897 const struct smb_filename *smb_fname,
3898 TALLOC_CTX *mem_ctx,
3899 unsigned int *pnum_streams,
3900 struct stream_struct **pstreams)
3904 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3906 DBG_ERR("Filtering resource stream failed\n");
3907 return NT_STATUS_INTERNAL_ERROR;
3909 return NT_STATUS_OK;
3912 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3913 vfs_handle_struct *handle,
3914 struct files_struct *fsp,
3915 const struct smb_filename *smb_fname,
3916 TALLOC_CTX *mem_ctx,
3917 unsigned int *pnum_streams,
3918 struct stream_struct **pstreams)
3920 struct stream_struct *stream = *pstreams;
3921 unsigned int num_streams = *pnum_streams;
3922 struct adouble *ad = NULL;
3928 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3929 * and if yes, remove it from the list
3931 for (i = 0; i < num_streams; i++) {
3932 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3937 if (i < num_streams) {
3938 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3939 smb_fname_str_dbg(smb_fname));
3941 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3942 AFPRESOURCE_STREAM);
3944 return NT_STATUS_INTERNAL_ERROR;
3948 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3950 return NT_STATUS_OK;
3953 rlen = ad_getentrylen(ad, ADEID_RFORK);
3957 return NT_STATUS_OK;
3960 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3961 AFPRESOURCE_STREAM_NAME, rlen,
3962 smb_roundup(handle->conn, rlen));
3964 return NT_STATUS_NO_MEMORY;
3967 return NT_STATUS_OK;
3970 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3971 struct files_struct *fsp,
3972 const struct smb_filename *smb_fname,
3973 TALLOC_CTX *mem_ctx,
3974 unsigned int *pnum_streams,
3975 struct stream_struct **pstreams)
3977 struct fruit_config_data *config = NULL;
3980 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
3981 return NT_STATUS_OK;
3984 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3985 return NT_STATUS_INTERNAL_ERROR);
3987 switch (config->rsrc) {
3988 case FRUIT_RSRC_STREAM:
3989 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3990 mem_ctx, pnum_streams,
3994 case FRUIT_RSRC_XATTR:
3995 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3996 mem_ctx, pnum_streams,
4000 case FRUIT_RSRC_ADFILE:
4001 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4002 mem_ctx, pnum_streams,
4007 return NT_STATUS_INTERNAL_ERROR;
4013 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
4014 struct stream_struct **pstreams)
4016 unsigned num_streams = *pnum_streams;
4017 struct stream_struct *streams = *pstreams;
4020 if (!global_fruit_config.nego_aapl) {
4024 while (i < num_streams) {
4025 struct smb_filename smb_fname = (struct smb_filename) {
4026 .stream_name = streams[i].name,
4029 if (is_ntfs_default_stream_smb_fname(&smb_fname)
4030 || streams[i].size > 0)
4036 streams[i] = streams[num_streams - 1];
4040 *pnum_streams = num_streams;
4043 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
4044 struct files_struct *fsp,
4045 TALLOC_CTX *mem_ctx,
4046 unsigned int *pnum_streams,
4047 struct stream_struct **pstreams)
4049 struct fruit_config_data *config = NULL;
4050 const struct smb_filename *smb_fname = NULL;
4053 smb_fname = fsp->fsp_name;
4055 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4056 return NT_STATUS_UNSUCCESSFUL);
4058 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4060 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
4061 pnum_streams, pstreams);
4062 if (!NT_STATUS_IS_OK(status)) {
4066 fruit_filter_empty_streams(pnum_streams, pstreams);
4068 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4069 mem_ctx, pnum_streams, pstreams);
4070 if (!NT_STATUS_IS_OK(status)) {
4074 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4075 mem_ctx, pnum_streams, pstreams);
4076 if (!NT_STATUS_IS_OK(status)) {
4080 return NT_STATUS_OK;
4083 static int fruit_fntimes(vfs_handle_struct *handle,
4085 struct smb_file_time *ft)
4088 struct adouble *ad = NULL;
4089 struct fruit_config_data *config = NULL;
4091 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4094 if ((config->meta != FRUIT_META_NETATALK) ||
4095 is_omit_timespec(&ft->create_time))
4097 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4100 DBG_DEBUG("set btime for %s to %s", fsp_str_dbg(fsp),
4101 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
4103 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4108 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4109 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4111 rc = ad_fset(handle, ad, fsp);
4117 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
4120 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4123 static int fruit_fallocate(struct vfs_handle_struct *handle,
4124 struct files_struct *fsp,
4129 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4132 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4135 /* Let the pwrite code path handle it. */
4140 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4141 struct files_struct *fsp,
4144 #ifdef HAVE_ATTROPEN
4145 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4150 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4151 struct files_struct *fsp,
4154 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4156 struct adouble *ad = NULL;
4159 if (fio == NULL || fio->ad_fsp == NULL) {
4160 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4165 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4167 DBG_ERR("ad_fget [%s] failed [%s]\n",
4168 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4172 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4174 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4180 ad_setentrylen(ad, ADEID_RFORK, offset);
4182 rc = ad_fset(handle, ad, fio->ad_fsp);
4184 DBG_ERR("ad_fset [%s] failed [%s]\n",
4185 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4194 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4195 struct files_struct *fsp,
4198 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4201 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4202 struct files_struct *fsp,
4205 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4209 DBG_ERR("Failed to fetch fsp extension\n");
4213 switch (fio->config->rsrc) {
4214 case FRUIT_RSRC_XATTR:
4215 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4218 case FRUIT_RSRC_ADFILE:
4219 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4222 case FRUIT_RSRC_STREAM:
4223 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4227 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4235 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4236 struct files_struct *fsp,
4240 DBG_WARNING("ftruncate %s to %jd\n",
4241 fsp_str_dbg(fsp), (intmax_t)offset);
4242 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4247 /* OS X returns success but does nothing */
4248 DBG_INFO("ignoring ftruncate %s to %jd\n",
4249 fsp_str_dbg(fsp), (intmax_t)offset);
4253 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4254 struct files_struct *fsp,
4257 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4260 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4264 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4267 if (fio->type == ADOUBLE_META) {
4268 ret = fruit_ftruncate_meta(handle, fsp, offset);
4270 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4273 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4277 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4278 struct smb_request *req,
4279 struct files_struct *dirfsp,
4280 struct smb_filename *smb_fname,
4281 uint32_t access_mask,
4282 uint32_t share_access,
4283 uint32_t create_disposition,
4284 uint32_t create_options,
4285 uint32_t file_attributes,
4286 uint32_t oplock_request,
4287 const struct smb2_lease *lease,
4288 uint64_t allocation_size,
4289 uint32_t private_flags,
4290 struct security_descriptor *sd,
4291 struct ea_list *ea_list,
4292 files_struct **result,
4294 const struct smb2_create_blobs *in_context_blobs,
4295 struct smb2_create_blobs *out_context_blobs)
4298 struct fruit_config_data *config = NULL;
4299 files_struct *fsp = NULL;
4300 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4303 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4304 if (!NT_STATUS_IS_OK(status)) {
4308 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4309 return NT_STATUS_UNSUCCESSFUL);
4311 if (is_apple_stream(smb_fname->stream_name) &&
4313 config->convert_adouble)
4315 uint32_t conv_flags = 0;
4317 if (config->wipe_intentionally_left_blank_rfork) {
4318 conv_flags |= AD_CONV_WIPE_BLANK;
4320 if (config->delete_empty_adfiles) {
4321 conv_flags |= AD_CONV_DELETE;
4324 ret = ad_convert(handle,
4326 macos_string_replace_map,
4329 DBG_ERR("ad_convert(\"%s\") failed\n",
4330 smb_fname_str_dbg(smb_fname));
4334 status = SMB_VFS_NEXT_CREATE_FILE(
4335 handle, req, dirfsp, smb_fname,
4336 access_mask, share_access,
4337 create_disposition, create_options,
4338 file_attributes, oplock_request,
4340 allocation_size, private_flags,
4341 sd, ea_list, result,
4342 pinfo, in_context_blobs, out_context_blobs);
4343 if (!NT_STATUS_IS_OK(status)) {
4349 if (global_fruit_config.nego_aapl) {
4350 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4352 * Enable POSIX directory rename behaviour
4354 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4359 * If this is a plain open for existing files, opening an 0
4360 * byte size resource fork MUST fail with
4361 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4363 * Cf the vfs_fruit torture tests in test_rfork_create().
4365 if (global_fruit_config.nego_aapl &&
4366 create_disposition == FILE_OPEN &&
4367 smb_fname->st.st_ex_size == 0 &&
4368 is_named_stream(smb_fname))
4370 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4374 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4378 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4379 (fsp->op != NULL) &&
4380 !fsp->fsp_flags.is_pathref)
4382 status = fruit_check_access(
4386 if (!NT_STATUS_IS_OK(status)) {
4394 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4397 close_file_free(req, &fsp, ERROR_CLOSE);
4404 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4405 struct files_struct *fsp,
4406 TALLOC_CTX *mem_ctx,
4407 struct readdir_attr_data **pattr_data)
4409 struct fruit_config_data *config = NULL;
4410 struct readdir_attr_data *attr_data;
4411 uint32_t conv_flags = 0;
4415 SMB_VFS_HANDLE_GET_DATA(handle, config,
4416 struct fruit_config_data,
4417 return NT_STATUS_UNSUCCESSFUL);
4419 if (!global_fruit_config.nego_aapl) {
4420 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4426 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4428 if (config->convert_adouble) {
4429 if (config->wipe_intentionally_left_blank_rfork) {
4430 conv_flags |= AD_CONV_WIPE_BLANK;
4432 if (config->delete_empty_adfiles) {
4433 conv_flags |= AD_CONV_DELETE;
4436 ret = ad_convert(handle,
4438 macos_string_replace_map,
4441 DBG_ERR("ad_convert(\"%s\") failed\n",
4446 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4447 if (*pattr_data == NULL) {
4448 return NT_STATUS_NO_MEMORY;
4450 attr_data = *pattr_data;
4451 attr_data->type = RDATTR_AAPL;
4454 * Mac metadata: compressed FinderInfo, resource fork length
4457 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4458 if (!NT_STATUS_IS_OK(status)) {
4460 * Error handling is tricky: if we return failure from
4461 * this function, the corresponding directory entry
4462 * will to be passed to the client, so we really just
4463 * want to error out on fatal errors.
4465 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4473 if (config->unix_info_enabled) {
4474 attr_data->attr_data.aapl.unix_mode =
4475 fsp->fsp_name->st.st_ex_mode;
4481 if (!config->readdir_attr_max_access) {
4482 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4484 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4487 SEC_FLAG_MAXIMUM_ALLOWED,
4488 &attr_data->attr_data.aapl.max_access);
4489 if (!NT_STATUS_IS_OK(status)) {
4494 return NT_STATUS_OK;
4497 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4499 TALLOC_FREE(*pattr_data);
4503 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4505 uint32_t security_info,
4506 TALLOC_CTX *mem_ctx,
4507 struct security_descriptor **ppdesc)
4510 struct security_ace ace;
4512 struct fruit_config_data *config;
4514 SMB_VFS_HANDLE_GET_DATA(handle, config,
4515 struct fruit_config_data,
4516 return NT_STATUS_UNSUCCESSFUL);
4518 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4520 if (!NT_STATUS_IS_OK(status)) {
4525 * Add MS NFS style ACEs with uid, gid and mode
4527 if (!global_fruit_config.nego_aapl) {
4528 return NT_STATUS_OK;
4530 if (!config->unix_info_enabled) {
4531 return NT_STATUS_OK;
4534 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4535 status = remove_virtual_nfs_aces(*ppdesc);
4536 if (!NT_STATUS_IS_OK(status)) {
4537 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4541 /* MS NFS style mode */
4542 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4543 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4544 status = security_descriptor_dacl_add(*ppdesc, &ace);
4545 if (!NT_STATUS_IS_OK(status)) {
4546 DEBUG(1,("failed to add MS NFS style ACE\n"));
4550 /* MS NFS style uid */
4551 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4552 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4553 status = security_descriptor_dacl_add(*ppdesc, &ace);
4554 if (!NT_STATUS_IS_OK(status)) {
4555 DEBUG(1,("failed to add MS NFS style ACE\n"));
4559 /* MS NFS style gid */
4560 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4561 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4562 status = security_descriptor_dacl_add(*ppdesc, &ace);
4563 if (!NT_STATUS_IS_OK(status)) {
4564 DEBUG(1,("failed to add MS NFS style ACE\n"));
4568 return NT_STATUS_OK;
4571 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4573 uint32_t security_info_sent,
4574 const struct security_descriptor *orig_psd)
4578 mode_t ms_nfs_mode = 0;
4580 struct security_descriptor *psd = NULL;
4581 uint32_t orig_num_aces = 0;
4583 if (orig_psd->dacl != NULL) {
4584 orig_num_aces = orig_psd->dacl->num_aces;
4587 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4589 return NT_STATUS_NO_MEMORY;
4592 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4594 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4595 if (!NT_STATUS_IS_OK(status)) {
4596 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4602 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4603 * sent/present flags correctly now we've removed them.
4606 if (orig_num_aces != 0) {
4608 * Are there any ACE's left ?
4610 if (psd->dacl->num_aces == 0) {
4611 /* No - clear the DACL sent/present flags. */
4612 security_info_sent &= ~SECINFO_DACL;
4613 psd->type &= ~SEC_DESC_DACL_PRESENT;
4617 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4618 if (!NT_STATUS_IS_OK(status)) {
4619 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4625 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4627 DBG_WARNING("%s, result: %d, %04o error %s\n",
4630 (unsigned)ms_nfs_mode,
4632 status = map_nt_error_from_unix(errno);
4639 return NT_STATUS_OK;
4642 static struct vfs_offload_ctx *fruit_offload_ctx;
4644 struct fruit_offload_read_state {
4645 struct vfs_handle_struct *handle;
4646 struct tevent_context *ev;
4654 static void fruit_offload_read_done(struct tevent_req *subreq);
4656 static struct tevent_req *fruit_offload_read_send(
4657 TALLOC_CTX *mem_ctx,
4658 struct tevent_context *ev,
4659 struct vfs_handle_struct *handle,
4666 struct tevent_req *req = NULL;
4667 struct tevent_req *subreq = NULL;
4668 struct fruit_offload_read_state *state = NULL;
4670 req = tevent_req_create(mem_ctx, &state,
4671 struct fruit_offload_read_state);
4675 *state = (struct fruit_offload_read_state) {
4682 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4683 fsctl, ttl, offset, to_copy);
4684 if (tevent_req_nomem(subreq, req)) {
4685 return tevent_req_post(req, ev);
4687 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4691 static void fruit_offload_read_done(struct tevent_req *subreq)
4693 struct tevent_req *req = tevent_req_callback_data(
4694 subreq, struct tevent_req);
4695 struct fruit_offload_read_state *state = tevent_req_data(
4696 req, struct fruit_offload_read_state);
4699 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4705 TALLOC_FREE(subreq);
4706 if (tevent_req_nterror(req, status)) {
4710 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4711 tevent_req_done(req);
4715 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4716 &fruit_offload_ctx);
4717 if (tevent_req_nterror(req, status)) {
4721 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4724 if (tevent_req_nterror(req, status)) {
4728 tevent_req_done(req);
4732 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4733 struct vfs_handle_struct *handle,
4734 TALLOC_CTX *mem_ctx,
4739 struct fruit_offload_read_state *state = tevent_req_data(
4740 req, struct fruit_offload_read_state);
4743 if (tevent_req_is_nterror(req, &status)) {
4744 tevent_req_received(req);
4748 *flags = state->flags;
4749 *xferlen = state->xferlen;
4750 token->length = state->token.length;
4751 token->data = talloc_move(mem_ctx, &state->token.data);
4753 tevent_req_received(req);
4754 return NT_STATUS_OK;
4757 struct fruit_offload_write_state {
4758 struct vfs_handle_struct *handle;
4760 struct files_struct *src_fsp;
4761 struct files_struct *dst_fsp;
4765 static void fruit_offload_write_done(struct tevent_req *subreq);
4766 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4767 TALLOC_CTX *mem_ctx,
4768 struct tevent_context *ev,
4771 off_t transfer_offset,
4772 struct files_struct *dest_fsp,
4776 struct tevent_req *req, *subreq;
4777 struct fruit_offload_write_state *state;
4779 struct fruit_config_data *config;
4780 off_t src_off = transfer_offset;
4781 files_struct *src_fsp = NULL;
4782 off_t to_copy = num;
4783 bool copyfile_enabled = false;
4785 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4786 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4788 SMB_VFS_HANDLE_GET_DATA(handle, config,
4789 struct fruit_config_data,
4792 req = tevent_req_create(mem_ctx, &state,
4793 struct fruit_offload_write_state);
4797 state->handle = handle;
4798 state->dst_fsp = dest_fsp;
4801 case FSCTL_SRV_COPYCHUNK:
4802 case FSCTL_SRV_COPYCHUNK_WRITE:
4803 copyfile_enabled = config->copyfile_enabled;
4810 * Check if this a OS X copyfile style copychunk request with
4811 * a requested chunk count of 0 that was translated to a
4812 * offload_write_send VFS call overloading the parameters src_off
4813 * = dest_off = num = 0.
4815 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4816 status = vfs_offload_token_db_fetch_fsp(
4817 fruit_offload_ctx, token, &src_fsp);
4818 if (tevent_req_nterror(req, status)) {
4819 return tevent_req_post(req, ev);
4821 state->src_fsp = src_fsp;
4823 status = vfs_stat_fsp(src_fsp);
4824 if (tevent_req_nterror(req, status)) {
4825 return tevent_req_post(req, ev);
4828 to_copy = src_fsp->fsp_name->st.st_ex_size;
4829 state->is_copyfile = true;
4832 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4841 if (tevent_req_nomem(subreq, req)) {
4842 return tevent_req_post(req, ev);
4845 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4849 static void fruit_offload_write_done(struct tevent_req *subreq)
4851 struct tevent_req *req = tevent_req_callback_data(
4852 subreq, struct tevent_req);
4853 struct fruit_offload_write_state *state = tevent_req_data(
4854 req, struct fruit_offload_write_state);
4856 unsigned int num_streams = 0;
4857 struct stream_struct *streams = NULL;
4859 struct smb_filename *src_fname_tmp = NULL;
4860 struct smb_filename *dst_fname_tmp = NULL;
4862 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4865 TALLOC_FREE(subreq);
4866 if (tevent_req_nterror(req, status)) {
4870 if (!state->is_copyfile) {
4871 tevent_req_done(req);
4876 * Now copy all remaining streams. We know the share supports
4877 * streams, because we're in vfs_fruit. We don't do this async
4878 * because streams are few and small.
4880 status = vfs_fstreaminfo(state->src_fsp,
4881 req, &num_streams, &streams);
4882 if (tevent_req_nterror(req, status)) {
4886 if (num_streams == 1) {
4887 /* There is always one stream, ::$DATA. */
4888 tevent_req_done(req);
4892 for (i = 0; i < num_streams; i++) {
4893 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4894 __func__, streams[i].name, (size_t)streams[i].size));
4896 src_fname_tmp = synthetic_smb_fname(
4898 state->src_fsp->fsp_name->base_name,
4901 state->src_fsp->fsp_name->twrp,
4902 state->src_fsp->fsp_name->flags);
4903 if (tevent_req_nomem(src_fname_tmp, req)) {
4907 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4908 TALLOC_FREE(src_fname_tmp);
4912 dst_fname_tmp = synthetic_smb_fname(
4914 state->dst_fsp->fsp_name->base_name,
4917 state->dst_fsp->fsp_name->twrp,
4918 state->dst_fsp->fsp_name->flags);
4919 if (tevent_req_nomem(dst_fname_tmp, req)) {
4920 TALLOC_FREE(src_fname_tmp);
4924 status = copy_file(req,
4925 state->handle->conn,
4929 if (!NT_STATUS_IS_OK(status)) {
4930 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4931 smb_fname_str_dbg(src_fname_tmp),
4932 smb_fname_str_dbg(dst_fname_tmp),
4933 nt_errstr(status)));
4934 TALLOC_FREE(src_fname_tmp);
4935 TALLOC_FREE(dst_fname_tmp);
4936 tevent_req_nterror(req, status);
4940 TALLOC_FREE(src_fname_tmp);
4941 TALLOC_FREE(dst_fname_tmp);
4944 TALLOC_FREE(streams);
4945 TALLOC_FREE(src_fname_tmp);
4946 TALLOC_FREE(dst_fname_tmp);
4947 tevent_req_done(req);
4950 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4951 struct tevent_req *req,
4954 struct fruit_offload_write_state *state = tevent_req_data(
4955 req, struct fruit_offload_write_state);
4958 if (tevent_req_is_nterror(req, &status)) {
4959 DEBUG(1, ("server side copy chunk failed: %s\n",
4960 nt_errstr(status)));
4962 tevent_req_received(req);
4966 *copied = state->copied;
4967 tevent_req_received(req);
4969 return NT_STATUS_OK;
4972 static char *fruit_get_bandsize_line(char **lines, int numlines)
4975 static bool re_initialized = false;
4979 if (!re_initialized) {
4980 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4984 re_initialized = true;
4987 for (i = 0; i < numlines; i++) {
4988 regmatch_t matches[1];
4990 ret = regexec(&re, lines[i], 1, matches, 0);
4993 * Check if the match was on the last line, sa we want
4994 * the subsequent line.
4996 if (i + 1 == numlines) {
4999 return lines[i + 1];
5001 if (ret != REG_NOMATCH) {
5009 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
5012 static bool re_initialized = false;
5013 regmatch_t matches[2];
5018 if (!re_initialized) {
5021 "<integer>\\([[:digit:]]*\\)</integer>$",
5026 re_initialized = true;
5029 ret = regexec(&re, line, 2, matches, 0);
5031 DBG_ERR("regex failed [%s]\n", line);
5035 line[matches[1].rm_eo] = '\0';
5037 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
5041 *_band_size = (size_t)band_size;
5046 * This reads and parses an Info.plist from a TM sparsebundle looking for the
5047 * "band-size" key and value.
5049 static bool fruit_get_bandsize(vfs_handle_struct *handle,
5053 #define INFO_PLIST_MAX_SIZE 64*1024
5055 struct smb_filename *smb_fname = NULL;
5056 files_struct *fsp = NULL;
5057 uint8_t *file_data = NULL;
5058 char **lines = NULL;
5059 char *band_size_line = NULL;
5060 size_t plist_file_size;
5067 plist = talloc_asprintf(talloc_tos(),
5069 handle->conn->connectpath,
5071 if (plist == NULL) {
5076 smb_fname = synthetic_smb_fname(talloc_tos(),
5082 if (smb_fname == NULL) {
5087 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5089 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
5094 plist_file_size = smb_fname->st.st_ex_size;
5096 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
5097 DBG_INFO("%s is too large, ignoring\n", plist);
5102 status = SMB_VFS_NEXT_CREATE_FILE(
5106 smb_fname, /* fname */
5107 FILE_GENERIC_READ, /* access_mask */
5108 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
5109 FILE_OPEN, /* create_disposition */
5110 0, /* create_options */
5111 0, /* file_attributes */
5112 INTERNAL_OPEN_ONLY, /* oplock_request */
5114 0, /* allocation_size */
5115 0, /* private_flags */
5120 NULL, NULL); /* create context */
5121 if (!NT_STATUS_IS_OK(status)) {
5122 DBG_INFO("Opening [%s] failed [%s]\n",
5123 smb_fname_str_dbg(smb_fname), nt_errstr(status));
5128 file_data = talloc_zero_array(talloc_tos(),
5130 plist_file_size + 1);
5131 if (file_data == NULL) {
5136 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
5137 if (nread != plist_file_size) {
5138 DBG_ERR("Short read on [%s]: %zu/%zd\n",
5139 fsp_str_dbg(fsp), nread, plist_file_size);
5145 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5146 if (!NT_STATUS_IS_OK(status)) {
5147 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5152 lines = file_lines_parse((char *)file_data,
5156 if (lines == NULL) {
5161 band_size_line = fruit_get_bandsize_line(lines, numlines);
5162 if (band_size_line == NULL) {
5163 DBG_ERR("Didn't find band-size key in [%s]\n",
5164 smb_fname_str_dbg(smb_fname));
5169 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5171 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5175 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5179 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5180 if (!NT_STATUS_IS_OK(status)) {
5181 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5185 TALLOC_FREE(smb_fname);
5186 TALLOC_FREE(file_data);
5191 struct fruit_disk_free_state {
5195 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5200 struct smb_filename *bands_dir = NULL;
5201 struct smb_Dir *dir_hnd = NULL;
5202 const char *dname = NULL;
5203 char *talloced = NULL;
5207 path = talloc_asprintf(talloc_tos(),
5209 handle->conn->connectpath,
5215 bands_dir = synthetic_smb_fname(talloc_tos(),
5222 if (bands_dir == NULL) {
5226 status = OpenDir(talloc_tos(),
5232 if (!NT_STATUS_IS_OK(status)) {
5233 TALLOC_FREE(bands_dir);
5234 errno = map_errno_from_nt_status(status);
5240 while ((dname = ReadDirName(dir_hnd, &talloced)) != NULL) {
5241 if (ISDOT(dname) || ISDOTDOT(dname)) {
5246 TALLOC_FREE(dir_hnd);
5248 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5250 TALLOC_FREE(bands_dir);
5256 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5257 struct fruit_disk_free_state *state,
5262 size_t sparsebundle_strlen = strlen("sparsebundle");
5263 size_t bandsize = 0;
5267 p = strstr(name, "sparsebundle");
5272 if (p[sparsebundle_strlen] != '\0') {
5276 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5278 ok = fruit_get_bandsize(handle, name, &bandsize);
5281 * Beware of race conditions: this may be an uninitialized
5282 * Info.plist that a client is just creating. We don't want let
5283 * this to trigger complete failure.
5285 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5289 ok = fruit_get_num_bands(handle, name, &nbands);
5292 * Beware of race conditions: this may be a backup sparsebundle
5293 * in an early stage lacking a bands subdirectory. We don't want
5294 * let this to trigger complete failure.
5296 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5301 * Arithmetic on 32-bit systems may cause overflow, depending on
5302 * size_t precision. First we check its unlikely, then we
5303 * force the precision into target off_t, then we check that
5304 * the total did not overflow either.
5306 if (bandsize > SIZE_MAX/nbands) {
5307 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5311 tm_size = (off_t)bandsize * (off_t)nbands;
5313 if (state->total_size + tm_size < state->total_size) {
5314 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5319 state->total_size += tm_size;
5321 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5322 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5328 * Calculate used size of a TimeMachine volume
5330 * This assumes that the volume is used only for TimeMachine.
5332 * - readdir(basedir of share), then
5333 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5334 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5335 * - count band files in "\1.sparsebundle/bands/"
5336 * - calculate used size of all bands: band_count * band_size
5338 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5339 const struct smb_filename *smb_fname,
5344 struct fruit_config_data *config = NULL;
5345 struct fruit_disk_free_state state = {0};
5346 struct smb_Dir *dir_hnd = NULL;
5347 const char *dname = NULL;
5348 char *talloced = NULL;
5354 SMB_VFS_HANDLE_GET_DATA(handle, config,
5355 struct fruit_config_data,
5358 if (!config->time_machine ||
5359 config->time_machine_max_size == 0)
5361 return SMB_VFS_NEXT_DISK_FREE(handle,
5368 status = OpenDir(talloc_tos(),
5374 if (!NT_STATUS_IS_OK(status)) {
5375 errno = map_errno_from_nt_status(status);
5379 while ((dname = ReadDirName(dir_hnd, &talloced)) != NULL) {
5380 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5382 TALLOC_FREE(talloced);
5383 TALLOC_FREE(dir_hnd);
5386 TALLOC_FREE(talloced);
5389 TALLOC_FREE(dir_hnd);
5391 dsize = config->time_machine_max_size / 512;
5392 dfree = dsize - (state.total_size / 512);
5393 if (dfree > dsize) {
5403 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5404 const SMB_STRUCT_STAT *psbuf)
5406 struct fruit_config_data *config = NULL;
5408 SMB_VFS_HANDLE_GET_DATA(handle, config,
5409 struct fruit_config_data,
5412 if (global_fruit_config.nego_aapl &&
5413 config->aapl_zero_file_id)
5418 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5421 static struct vfs_fn_pointers vfs_fruit_fns = {
5422 .connect_fn = fruit_connect,
5423 .disk_free_fn = fruit_disk_free,
5425 /* File operations */
5426 .fchmod_fn = fruit_fchmod,
5427 .unlinkat_fn = fruit_unlinkat,
5428 .renameat_fn = fruit_renameat,
5429 .openat_fn = fruit_openat,
5430 .close_fn = fruit_close,
5431 .pread_fn = fruit_pread,
5432 .pwrite_fn = fruit_pwrite,
5433 .pread_send_fn = fruit_pread_send,
5434 .pread_recv_fn = fruit_pread_recv,
5435 .pwrite_send_fn = fruit_pwrite_send,
5436 .pwrite_recv_fn = fruit_pwrite_recv,
5437 .fsync_send_fn = fruit_fsync_send,
5438 .fsync_recv_fn = fruit_fsync_recv,
5439 .stat_fn = fruit_stat,
5440 .lstat_fn = fruit_lstat,
5441 .fstat_fn = fruit_fstat,
5442 .fstreaminfo_fn = fruit_fstreaminfo,
5443 .fntimes_fn = fruit_fntimes,
5444 .ftruncate_fn = fruit_ftruncate,
5445 .fallocate_fn = fruit_fallocate,
5446 .create_file_fn = fruit_create_file,
5447 .freaddir_attr_fn = fruit_freaddir_attr,
5448 .offload_read_send_fn = fruit_offload_read_send,
5449 .offload_read_recv_fn = fruit_offload_read_recv,
5450 .offload_write_send_fn = fruit_offload_write_send,
5451 .offload_write_recv_fn = fruit_offload_write_recv,
5452 .fs_file_id_fn = fruit_fs_file_id,
5454 /* NT ACL operations */
5455 .fget_nt_acl_fn = fruit_fget_nt_acl,
5456 .fset_nt_acl_fn = fruit_fset_nt_acl,
5460 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5462 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5464 if (!NT_STATUS_IS_OK(ret)) {
5468 vfs_fruit_debug_level = debug_add_class("fruit");
5469 if (vfs_fruit_debug_level == -1) {
5470 vfs_fruit_debug_level = DBGC_VFS;
5471 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5474 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5475 "vfs_fruit_init","fruit",vfs_fruit_debug_level));