2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "system/shmem.h"
26 #include "locking/proto.h"
27 #include "smbd/globals.h"
29 #include "libcli/security/security.h"
30 #include "../libcli/smb/smb2_create_ctx.h"
31 #include "lib/util/tevent_ntstatus.h"
32 #include "lib/util/tevent_unix.h"
33 #include "offload_token.h"
34 #include "string_replace.h"
35 #include "hash_inode.h"
36 #include "lib/adouble.h"
37 #include "lib/util_macstreams.h"
40 * Enhanced OS X and Netatalk compatibility
41 * ========================================
43 * This modules takes advantage of vfs_streams_xattr and
44 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
45 * loaded in the correct order:
47 * vfs modules = catia fruit streams_xattr
49 * The module intercepts the OS X special streams "AFP_AfpInfo" and
50 * "AFP_Resource" and handles them in a special way. All other named
51 * streams are deferred to vfs_streams_xattr.
53 * The OS X client maps all NTFS illegal characters to the Unicode
54 * private range. This module optionally stores the characters using
55 * their native ASCII encoding using vfs_catia. If you're not enabling
56 * this feature, you can skip catia from vfs modules.
58 * Finally, open modes are optionally checked against Netatalk AFP
61 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
62 * extended metadata for files and directories. This module optionally
63 * reads and stores this metadata in a way compatible with Netatalk 3
64 * which stores the metadata in an EA "org.netatalk.metadata". Cf
65 * source3/include/MacExtensions.h for a description of the binary
68 * The "AFP_Resource" named stream may be arbitrarily large, thus it
69 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
70 * the only available filesystem where xattrs can be of any size and
71 * the OS supports using the file APIs for xattrs.
73 * The AFP_Resource stream is stored in an AppleDouble file prepending
74 * "._" to the filename. On Solaris with ZFS the stream is optionally
75 * stored in an EA "org.netatalk.resource".
81 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
82 * other protocols you may want to adjust the xattr names the VFS
83 * module vfs_streams_xattr uses for storing ADS's. This defaults to
84 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
85 * these module parameters:
87 * streams_xattr:prefix = user.
88 * streams_xattr:store_stream_type = false
94 * - log diagnostic if any needed VFS module is not loaded
95 * (eg with lp_vfs_objects())
99 static int vfs_fruit_debug_level = DBGC_VFS;
101 static struct global_fruit_config {
102 bool nego_aapl; /* client negotiated AAPL */
104 } global_fruit_config;
107 #define DBGC_CLASS vfs_fruit_debug_level
109 #define FRUIT_PARAM_TYPE_NAME "fruit"
111 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
113 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
114 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
115 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
116 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
118 struct fruit_config_data {
119 enum fruit_rsrc rsrc;
120 enum fruit_meta meta;
121 enum fruit_locking locking;
122 enum fruit_encoding encoding;
123 bool use_aapl; /* config from smb.conf */
125 bool readdir_attr_enabled;
126 bool unix_info_enabled;
127 bool copyfile_enabled;
128 bool veto_appledouble;
130 bool aapl_zero_file_id;
133 off_t time_machine_max_size;
134 bool wipe_intentionally_left_blank_rfork;
135 bool delete_empty_adfiles;
138 * Additional options, all enabled by default,
139 * possibly useful for analyzing performance. The associated
140 * operations with each of them may be expensive, so having
141 * the chance to disable them individually gives a chance
142 * tweaking the setup for the particular usecase.
144 bool readdir_attr_rsize;
145 bool readdir_attr_finder_info;
146 bool readdir_attr_max_access;
149 static const struct enum_list fruit_rsrc[] = {
150 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
151 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
152 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
156 static const struct enum_list fruit_meta[] = {
157 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
162 static const struct enum_list fruit_locking[] = {
163 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
164 {FRUIT_LOCKING_NONE, "none"},
168 static const struct enum_list fruit_encoding[] = {
169 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
170 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
175 vfs_handle_struct *handle;
176 files_struct *fsp; /* backlink to itself */
178 /* tcon config handle */
179 struct fruit_config_data *config;
181 /* Backend fsp for AppleDouble file, can be NULL */
182 files_struct *ad_fsp;
183 /* link from adouble_open_from_base_fsp() to fio */
184 struct fio *real_fio;
186 /* Denote stream type, meta or rsrc */
190 * AFP_AfpInfo stream created, but not written yet, thus still a fake
191 * pipe fd. This is set to true in fruit_open_meta if there was no
192 * existing stream but the caller requested O_CREAT. It is later set to
193 * false when we get a write on the stream that then does open and
201 /*****************************************************************************
203 *****************************************************************************/
205 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
208 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
214 if (fio->real_fio != NULL) {
216 * This is an fsp from adouble_open_from_base_fsp()
217 * we should just pass this to the next
227 * Initialize config struct from our smb.conf config parameters
229 static int init_fruit_config(vfs_handle_struct *handle)
231 struct fruit_config_data *config;
233 const char *tm_size_str = NULL;
235 config = talloc_zero(handle->conn, struct fruit_config_data);
237 DEBUG(1, ("talloc_zero() failed\n"));
243 * Versions up to Samba 4.5.x had a spelling bug in the
244 * fruit:resource option calling lp_parm_enum with
245 * "res*s*ource" (ie two s).
247 * In Samba 4.6 we accept both the wrong and the correct
248 * spelling, in Samba 4.7 the bad spelling will be removed.
250 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
251 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
253 DEBUG(1, ("value for %s: resource type unknown\n",
254 FRUIT_PARAM_TYPE_NAME));
257 config->rsrc = (enum fruit_rsrc)enumval;
259 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
260 "resource", fruit_rsrc, enumval);
262 DEBUG(1, ("value for %s: resource type unknown\n",
263 FRUIT_PARAM_TYPE_NAME));
266 config->rsrc = (enum fruit_rsrc)enumval;
268 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
269 "metadata", fruit_meta, FRUIT_META_NETATALK);
271 DEBUG(1, ("value for %s: metadata type unknown\n",
272 FRUIT_PARAM_TYPE_NAME));
275 config->meta = (enum fruit_meta)enumval;
277 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
278 "locking", fruit_locking, FRUIT_LOCKING_NONE);
280 DEBUG(1, ("value for %s: locking type unknown\n",
281 FRUIT_PARAM_TYPE_NAME));
284 config->locking = (enum fruit_locking)enumval;
286 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
287 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
289 DEBUG(1, ("value for %s: encoding type unknown\n",
290 FRUIT_PARAM_TYPE_NAME));
293 config->encoding = (enum fruit_encoding)enumval;
295 if (config->rsrc == FRUIT_RSRC_ADFILE) {
296 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
297 FRUIT_PARAM_TYPE_NAME,
302 config->use_aapl = lp_parm_bool(
303 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
305 config->time_machine = lp_parm_bool(
306 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
308 config->unix_info_enabled = lp_parm_bool(
309 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
311 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
314 config->posix_rename = lp_parm_bool(
315 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
317 config->aapl_zero_file_id =
318 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
319 "zero_file_id", false);
321 config->readdir_attr_rsize = lp_parm_bool(
322 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
324 config->readdir_attr_finder_info = lp_parm_bool(
325 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
327 config->readdir_attr_max_access = lp_parm_bool(
328 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
330 config->model = lp_parm_const_string(
331 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
333 tm_size_str = lp_parm_const_string(
334 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
335 "time machine max size", NULL);
336 if (tm_size_str != NULL) {
337 config->time_machine_max_size = conv_str_size(tm_size_str);
340 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
341 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
342 "wipe_intentionally_left_blank_rfork", false);
344 config->delete_empty_adfiles = lp_parm_bool(
345 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
346 "delete_empty_adfiles", false);
348 SMB_VFS_HANDLE_SET_DATA(handle, config,
349 NULL, struct fruit_config_data,
355 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
356 struct stream_struct **streams,
357 const char *name, off_t size,
360 struct stream_struct *tmp;
362 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
368 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
369 if (tmp[*num_streams].name == NULL) {
373 tmp[*num_streams].size = size;
374 tmp[*num_streams].alloc_size = alloc_size;
381 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
382 struct stream_struct **streams)
384 struct stream_struct *tmp = *streams;
387 if (*num_streams == 0) {
391 for (i = 0; i < *num_streams; i++) {
392 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
397 if (i == *num_streams) {
401 if (tmp[i].size > 0) {
405 TALLOC_FREE(tmp[i].name);
406 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
411 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
412 struct stream_struct **streams,
415 struct stream_struct *tmp = *streams;
418 if (*num_streams == 0) {
422 for (i = 0; i < *num_streams; i++) {
423 if (strequal_m(tmp[i].name, name)) {
428 if (i == *num_streams) {
432 TALLOC_FREE(tmp[i].name);
433 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
438 static bool ad_empty_finderinfo(const struct adouble *ad)
441 char emptybuf[ADEDLEN_FINDERI] = {0};
444 fi = ad_get_entry(ad, ADEID_FINDERI);
446 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
450 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
454 static bool ai_empty_finderinfo(const AfpInfo *ai)
457 char emptybuf[ADEDLEN_FINDERI] = {0};
459 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
464 * Update btime with btime from Netatalk
466 static void update_btime(vfs_handle_struct *handle,
467 struct smb_filename *smb_fname)
470 struct timespec creation_time = {0};
472 struct fruit_config_data *config = NULL;
474 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
477 switch (config->meta) {
478 case FRUIT_META_STREAM:
480 case FRUIT_META_NETATALK:
484 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
488 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
492 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
498 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
499 update_stat_ex_create_time(&smb_fname->st, creation_time);
505 * Map an access mask to a Netatalk single byte byte range lock
507 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
508 uint32_t access_mask)
512 switch (access_mask) {
514 offset = AD_FILELOCK_OPEN_RD;
517 case FILE_WRITE_DATA:
518 case FILE_APPEND_DATA:
519 offset = AD_FILELOCK_OPEN_WR;
523 offset = AD_FILELOCK_OPEN_NONE;
527 if (fork_type == APPLE_FORK_RSRC) {
528 if (offset == AD_FILELOCK_OPEN_NONE) {
529 offset = AD_FILELOCK_RSRC_OPEN_NONE;
539 * Map a deny mode to a Netatalk brl
541 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
548 offset = AD_FILELOCK_DENY_RD;
552 offset = AD_FILELOCK_DENY_WR;
556 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
559 if (fork_type == APPLE_FORK_RSRC) {
567 * Call fcntl() with an exclusive F_GETLK request in order to
568 * determine if there's an existing shared lock
570 * @return true if the requested lock was found or any error occurred
571 * false if the lock was not found
573 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
576 off_t offset = in_offset;
581 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
582 if (result == false) {
586 if (type != F_UNLCK) {
593 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
595 uint32_t access_mask,
598 NTSTATUS status = NT_STATUS_OK;
600 bool share_for_read = (share_mode & FILE_SHARE_READ);
601 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
602 bool netatalk_already_open_for_reading = false;
603 bool netatalk_already_open_for_writing = false;
604 bool netatalk_already_open_with_deny_read = false;
605 bool netatalk_already_open_with_deny_write = false;
606 struct GUID req_guid = GUID_random();
608 /* FIXME: hardcoded data fork, add resource fork */
609 enum apple_fork fork_type = APPLE_FORK_DATA;
611 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
613 access_mask & FILE_READ_DATA ? "READ" :"-",
614 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
617 if (fsp_get_io_fd(fsp) == -1) {
621 /* Read NetATalk opens and deny modes on the file. */
622 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
623 access_to_netatalk_brl(fork_type,
626 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
627 denymode_to_netatalk_brl(fork_type,
630 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
631 access_to_netatalk_brl(fork_type,
634 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
635 denymode_to_netatalk_brl(fork_type,
638 /* If there are any conflicts - sharing violation. */
639 if ((access_mask & FILE_READ_DATA) &&
640 netatalk_already_open_with_deny_read) {
641 return NT_STATUS_SHARING_VIOLATION;
644 if (!share_for_read &&
645 netatalk_already_open_for_reading) {
646 return NT_STATUS_SHARING_VIOLATION;
649 if ((access_mask & FILE_WRITE_DATA) &&
650 netatalk_already_open_with_deny_write) {
651 return NT_STATUS_SHARING_VIOLATION;
654 if (!share_for_write &&
655 netatalk_already_open_for_writing) {
656 return NT_STATUS_SHARING_VIOLATION;
659 if (!(access_mask & FILE_READ_DATA)) {
661 * Nothing we can do here, we need read access
667 /* Set NetAtalk locks matching our access */
668 if (access_mask & FILE_READ_DATA) {
669 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
670 req_guid.time_hi_and_version = __LINE__;
675 fsp->op->global->open_persistent_id,
683 if (!NT_STATUS_IS_OK(status)) {
688 if (!share_for_read) {
689 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
690 req_guid.time_hi_and_version = __LINE__;
695 fsp->op->global->open_persistent_id,
703 if (!NT_STATUS_IS_OK(status)) {
708 if (access_mask & FILE_WRITE_DATA) {
709 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
710 req_guid.time_hi_and_version = __LINE__;
715 fsp->op->global->open_persistent_id,
723 if (!NT_STATUS_IS_OK(status)) {
728 if (!share_for_write) {
729 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
730 req_guid.time_hi_and_version = __LINE__;
735 fsp->op->global->open_persistent_id,
743 if (!NT_STATUS_IS_OK(status)) {
751 static NTSTATUS check_aapl(vfs_handle_struct *handle,
752 struct smb_request *req,
753 const struct smb2_create_blobs *in_context_blobs,
754 struct smb2_create_blobs *out_context_blobs)
756 struct fruit_config_data *config;
758 struct smb2_create_blob *aapl = NULL;
762 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
763 uint64_t req_bitmap, client_caps;
764 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
768 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
769 return NT_STATUS_UNSUCCESSFUL);
771 if (!config->use_aapl
772 || in_context_blobs == NULL
773 || out_context_blobs == NULL) {
777 aapl = smb2_create_blob_find(in_context_blobs,
778 SMB2_CREATE_TAG_AAPL);
783 if (aapl->data.length != 24) {
784 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
785 (uintmax_t)aapl->data.length));
786 return NT_STATUS_INVALID_PARAMETER;
789 cmd = IVAL(aapl->data.data, 0);
790 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
791 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
792 return NT_STATUS_INVALID_PARAMETER;
795 req_bitmap = BVAL(aapl->data.data, 8);
796 client_caps = BVAL(aapl->data.data, 16);
798 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
800 SBVAL(p, 8, req_bitmap);
801 ok = data_blob_append(req, &blob, p, 16);
803 return NT_STATUS_UNSUCCESSFUL;
806 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
807 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
808 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
809 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
810 config->readdir_attr_enabled = true;
813 if (config->use_copyfile) {
814 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
815 config->copyfile_enabled = true;
819 * The client doesn't set the flag, so we can't check
820 * for it and just set it unconditionally
822 if (config->unix_info_enabled) {
823 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
826 SBVAL(p, 0, server_caps);
827 ok = data_blob_append(req, &blob, p, 8);
829 return NT_STATUS_UNSUCCESSFUL;
833 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
834 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
842 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
849 if (config->time_machine) {
850 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
855 ok = data_blob_append(req, &blob, p, 8);
857 return NT_STATUS_UNSUCCESSFUL;
861 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
862 ok = convert_string_talloc(req,
864 config->model, strlen(config->model),
867 return NT_STATUS_UNSUCCESSFUL;
871 SIVAL(p + 4, 0, modellen);
872 ok = data_blob_append(req, &blob, p, 8);
875 return NT_STATUS_UNSUCCESSFUL;
878 ok = data_blob_append(req, &blob, model, modellen);
881 return NT_STATUS_UNSUCCESSFUL;
885 status = smb2_create_blob_add(out_context_blobs,
887 SMB2_CREATE_TAG_AAPL,
889 if (NT_STATUS_IS_OK(status)) {
890 global_fruit_config.nego_aapl = true;
896 static bool readdir_attr_meta_finderi_stream(
897 struct vfs_handle_struct *handle,
898 const struct smb_filename *smb_fname,
901 struct smb_filename *stream_name = NULL;
902 files_struct *fsp = NULL;
906 uint8_t buf[AFP_INFO_SIZE];
908 status = synthetic_pathref(talloc_tos(),
909 handle->conn->cwd_fsp,
910 smb_fname->base_name,
916 if (!NT_STATUS_IS_OK(status)) {
920 status = SMB_VFS_CREATE_FILE(
921 handle->conn, /* conn */
923 stream_name, /* fname */
924 FILE_READ_DATA, /* access_mask */
925 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
927 FILE_OPEN, /* create_disposition*/
928 0, /* create_options */
929 0, /* file_attributes */
930 INTERNAL_OPEN_ONLY, /* oplock_request */
932 0, /* allocation_size */
933 0, /* private_flags */
938 NULL, NULL); /* create context */
940 TALLOC_FREE(stream_name);
942 if (!NT_STATUS_IS_OK(status)) {
946 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
947 if (nread != AFP_INFO_SIZE) {
948 DBG_ERR("short read [%s] [%zd/%d]\n",
949 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
954 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
961 close_file(NULL, fsp, NORMAL_CLOSE);
967 static bool readdir_attr_meta_finderi_netatalk(
968 struct vfs_handle_struct *handle,
969 const struct smb_filename *smb_fname,
972 struct adouble *ad = NULL;
975 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
980 p = ad_get_entry(ad, ADEID_FINDERI);
982 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
987 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
992 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
993 const struct smb_filename *smb_fname,
994 struct readdir_attr_data *attr_data)
996 struct fruit_config_data *config = NULL;
1001 SMB_VFS_HANDLE_GET_DATA(handle, config,
1002 struct fruit_config_data,
1005 switch (config->meta) {
1006 case FRUIT_META_NETATALK:
1007 ok = readdir_attr_meta_finderi_netatalk(
1008 handle, smb_fname, &ai);
1011 case FRUIT_META_STREAM:
1012 ok = readdir_attr_meta_finderi_stream(
1013 handle, smb_fname, &ai);
1017 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1022 /* Don't bother with errors, it's likely ENOENT */
1026 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1028 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1029 &ai.afpi_FinderInfo[0], 4);
1031 /* finder_creator */
1032 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1033 &ai.afpi_FinderInfo[4], 4);
1037 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1038 &ai.afpi_FinderInfo[8], 2);
1040 /* finder_ext_flags */
1041 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1042 &ai.afpi_FinderInfo[24], 2);
1045 date_added = convert_time_t_to_uint32_t(
1046 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1048 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1053 static uint64_t readdir_attr_rfork_size_adouble(
1054 struct vfs_handle_struct *handle,
1055 const struct smb_filename *smb_fname)
1057 struct adouble *ad = NULL;
1058 uint64_t rfork_size;
1060 ad = ad_get(talloc_tos(), handle, smb_fname,
1066 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1072 static uint64_t readdir_attr_rfork_size_stream(
1073 struct vfs_handle_struct *handle,
1074 const struct smb_filename *smb_fname)
1076 struct smb_filename *stream_name = NULL;
1078 uint64_t rfork_size;
1080 stream_name = synthetic_smb_fname(talloc_tos(),
1081 smb_fname->base_name,
1082 AFPRESOURCE_STREAM_NAME,
1086 if (stream_name == NULL) {
1090 ret = SMB_VFS_STAT(handle->conn, stream_name);
1092 TALLOC_FREE(stream_name);
1096 rfork_size = stream_name->st.st_ex_size;
1097 TALLOC_FREE(stream_name);
1102 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1103 const struct smb_filename *smb_fname)
1105 struct fruit_config_data *config = NULL;
1106 uint64_t rfork_size;
1108 SMB_VFS_HANDLE_GET_DATA(handle, config,
1109 struct fruit_config_data,
1112 switch (config->rsrc) {
1113 case FRUIT_RSRC_ADFILE:
1114 rfork_size = readdir_attr_rfork_size_adouble(handle,
1118 case FRUIT_RSRC_XATTR:
1119 case FRUIT_RSRC_STREAM:
1120 rfork_size = readdir_attr_rfork_size_stream(handle,
1125 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1133 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1134 const struct smb_filename *smb_fname,
1135 struct readdir_attr_data *attr_data)
1137 NTSTATUS status = NT_STATUS_OK;
1138 struct fruit_config_data *config = NULL;
1141 SMB_VFS_HANDLE_GET_DATA(handle, config,
1142 struct fruit_config_data,
1143 return NT_STATUS_UNSUCCESSFUL);
1146 /* Ensure we return a default value in the creation_date field */
1147 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1150 * Resource fork length
1153 if (config->readdir_attr_rsize) {
1154 uint64_t rfork_size;
1156 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1157 attr_data->attr_data.aapl.rfork_size = rfork_size;
1164 if (config->readdir_attr_finder_info) {
1165 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1167 status = NT_STATUS_INTERNAL_ERROR;
1174 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1179 if (psd->dacl == NULL) {
1180 return NT_STATUS_OK;
1183 for (i = 0; i < psd->dacl->num_aces; i++) {
1184 /* MS NFS style mode/uid/gid */
1185 int cmp = dom_sid_compare_domain(
1186 &global_sid_Unix_NFS,
1187 &psd->dacl->aces[i].trustee);
1189 /* Normal ACE entry. */
1194 * security_descriptor_dacl_del()
1195 * *must* return NT_STATUS_OK as we know
1196 * we have something to remove.
1199 status = security_descriptor_dacl_del(psd,
1200 &psd->dacl->aces[i].trustee);
1201 if (!NT_STATUS_IS_OK(status)) {
1202 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1208 * security_descriptor_dacl_del() may delete more
1209 * then one entry subsequent to this one if the
1210 * SID matches, but we only need to ensure that
1211 * we stay looking at the same element in the array.
1215 return NT_STATUS_OK;
1218 /* Search MS NFS style ACE with UNIX mode */
1219 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1221 struct security_descriptor *psd,
1226 struct fruit_config_data *config = NULL;
1230 SMB_VFS_HANDLE_GET_DATA(handle, config,
1231 struct fruit_config_data,
1232 return NT_STATUS_UNSUCCESSFUL);
1234 if (!global_fruit_config.nego_aapl) {
1235 return NT_STATUS_OK;
1237 if (psd->dacl == NULL || !config->unix_info_enabled) {
1238 return NT_STATUS_OK;
1241 for (i = 0; i < psd->dacl->num_aces; i++) {
1242 if (dom_sid_compare_domain(
1243 &global_sid_Unix_NFS_Mode,
1244 &psd->dacl->aces[i].trustee) == 0) {
1245 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1246 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1249 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1250 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1256 * Remove any incoming virtual ACE entries generated by
1257 * fruit_fget_nt_acl().
1260 return remove_virtual_nfs_aces(psd);
1263 /****************************************************************************
1265 ****************************************************************************/
1267 static int fruit_connect(vfs_handle_struct *handle,
1268 const char *service,
1272 char *list = NULL, *newlist = NULL;
1273 struct fruit_config_data *config;
1274 const struct loadparm_substitution *lp_sub =
1275 loadparm_s3_global_substitution();
1277 DEBUG(10, ("fruit_connect\n"));
1279 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1284 rc = init_fruit_config(handle);
1289 SMB_VFS_HANDLE_GET_DATA(handle, config,
1290 struct fruit_config_data, return -1);
1292 if (config->veto_appledouble) {
1293 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1296 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1297 newlist = talloc_asprintf(
1299 "%s/" ADOUBLE_NAME_PREFIX "*/",
1301 lp_do_parameter(SNUM(handle->conn),
1306 lp_do_parameter(SNUM(handle->conn),
1308 "/" ADOUBLE_NAME_PREFIX "*/");
1314 if (config->encoding == FRUIT_ENC_NATIVE) {
1315 lp_do_parameter(SNUM(handle->conn),
1317 macos_string_replace_map);
1320 if (config->time_machine) {
1321 DBG_NOTICE("Enabling durable handles for Time Machine "
1322 "support on [%s]\n", service);
1323 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1324 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1325 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1326 if (!lp_strict_sync(SNUM(handle->conn))) {
1327 DBG_WARNING("Time Machine without strict sync is not "
1330 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1336 static void fio_ref_destroy_fn(void *p_data)
1338 struct fio *ref_fio = (struct fio *)p_data;
1339 if (ref_fio->real_fio != NULL) {
1340 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1341 ref_fio->real_fio->ad_fsp = NULL;
1342 ref_fio->real_fio = NULL;
1346 static void fio_close_ad_fsp(struct fio *fio)
1348 if (fio->ad_fsp != NULL) {
1349 fd_close(fio->ad_fsp);
1350 file_free(NULL, fio->ad_fsp);
1351 /* fio_ref_destroy_fn() should have cleared this */
1352 SMB_ASSERT(fio->ad_fsp == NULL);
1356 static void fio_destroy_fn(void *p_data)
1358 struct fio *fio = (struct fio *)p_data;
1359 fio_close_ad_fsp(fio);
1362 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1363 const struct files_struct *dirfsp,
1364 const struct smb_filename *smb_fname,
1369 struct fruit_config_data *config = NULL;
1370 struct fio *fio = NULL;
1371 int open_flags = flags & ~O_CREAT;
1374 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1376 SMB_VFS_HANDLE_GET_DATA(handle, config,
1377 struct fruit_config_data, return -1);
1379 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1380 fio->handle = handle;
1382 fio->type = ADOUBLE_META;
1383 fio->config = config;
1385 fd = SMB_VFS_NEXT_OPENAT(handle,
1395 if (!(flags & O_CREAT)) {
1396 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1402 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1406 fio->fake_fd = true;
1413 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1414 const struct files_struct *dirfsp,
1415 const struct smb_filename *smb_fname,
1420 struct fruit_config_data *config = NULL;
1421 struct fio *fio = NULL;
1422 struct adouble *ad = NULL;
1423 bool meta_exists = false;
1426 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1428 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1435 if (!meta_exists && !(flags & O_CREAT)) {
1445 SMB_VFS_HANDLE_GET_DATA(handle, config,
1446 struct fruit_config_data, return -1);
1448 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1449 fio->handle = handle;
1451 fio->type = ADOUBLE_META;
1452 fio->config = config;
1453 fio->fake_fd = true;
1460 static int fruit_open_meta(vfs_handle_struct *handle,
1461 const struct files_struct *dirfsp,
1462 const struct smb_filename *smb_fname,
1463 files_struct *fsp, int flags, mode_t mode)
1466 struct fruit_config_data *config = NULL;
1468 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1470 SMB_VFS_HANDLE_GET_DATA(handle, config,
1471 struct fruit_config_data, return -1);
1473 switch (config->meta) {
1474 case FRUIT_META_STREAM:
1475 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1479 case FRUIT_META_NETATALK:
1480 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1485 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1489 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1494 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1495 const struct files_struct *dirfsp,
1496 const struct smb_filename *smb_fname,
1502 struct fruit_config_data *config = NULL;
1503 struct files_struct *ad_fsp = NULL;
1504 struct fio *fio = NULL;
1505 struct fio *ref_fio = NULL;
1509 SMB_VFS_HANDLE_GET_DATA(handle, config,
1510 struct fruit_config_data, return -1);
1512 if ((!(flags & O_CREAT)) &&
1513 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1515 /* sorry, but directories don't habe a resource fork */
1522 * We return a fake_fd to the vfs modules above,
1523 * while we open an internal backend fsp for the
1524 * '._' file for the next vfs modules.
1526 * Note that adouble_open_from_base_fsp() recurses
1527 * into fruit_openat(), but it'll just pass to
1528 * the next module as just opens a flat file on
1538 status = adouble_open_from_base_fsp(dirfsp,
1544 if (!NT_STATUS_IS_OK(status)) {
1545 errno = map_errno_from_nt_status(status);
1551 * Now we need to glue both handles together,
1552 * so that they automatically detach each other
1555 fio = fruit_get_complete_fio(handle, fsp);
1557 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1559 fio_ref_destroy_fn);
1560 if (ref_fio == NULL) {
1561 int saved_errno = errno;
1563 file_free(NULL, ad_fsp);
1565 errno = saved_errno;
1570 SMB_ASSERT(ref_fio->fsp == NULL);
1571 ref_fio->handle = handle;
1572 ref_fio->fsp = ad_fsp;
1573 ref_fio->type = ADOUBLE_RSRC;
1574 ref_fio->config = config;
1575 ref_fio->real_fio = fio;
1576 SMB_ASSERT(fio->ad_fsp == NULL);
1577 fio->ad_fsp = ad_fsp;
1578 fio->fake_fd = true;
1582 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1584 int saved_errno = errno;
1586 vfs_fake_fd_close(fd);
1588 errno = saved_errno;
1594 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1595 const struct files_struct *dirfsp,
1596 const struct smb_filename *smb_fname,
1601 #ifdef HAVE_ATTROPEN
1605 * As there's no attropenat() this is only going to work with AT_FDCWD.
1607 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1609 fd = attropen(smb_fname->base_name,
1610 AFPRESOURCE_EA_NETATALK,
1625 static int fruit_open_rsrc(vfs_handle_struct *handle,
1626 const struct files_struct *dirfsp,
1627 const struct smb_filename *smb_fname,
1628 files_struct *fsp, int flags, mode_t mode)
1631 struct fruit_config_data *config = NULL;
1632 struct fio *fio = NULL;
1634 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1636 SMB_VFS_HANDLE_GET_DATA(handle, config,
1637 struct fruit_config_data, return -1);
1639 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1640 fio->handle = handle;
1642 fio->type = ADOUBLE_RSRC;
1643 fio->config = config;
1645 switch (config->rsrc) {
1646 case FRUIT_RSRC_STREAM:
1647 fd = SMB_VFS_NEXT_OPENAT(handle,
1655 case FRUIT_RSRC_ADFILE:
1656 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1660 case FRUIT_RSRC_XATTR:
1661 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1666 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1670 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1679 static int fruit_openat(vfs_handle_struct *handle,
1680 const struct files_struct *dirfsp,
1681 const struct smb_filename *smb_fname,
1688 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1690 if (!is_named_stream(smb_fname)) {
1691 return SMB_VFS_NEXT_OPENAT(handle,
1699 if (is_afpinfo_stream(smb_fname->stream_name)) {
1700 fd = fruit_open_meta(handle,
1706 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1707 fd = fruit_open_rsrc(handle,
1714 fd = SMB_VFS_NEXT_OPENAT(handle,
1722 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1724 /* Prevent reopen optimisation */
1725 fsp->fsp_flags.have_proc_fds = false;
1729 static int fruit_close_meta(vfs_handle_struct *handle,
1732 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1734 struct fruit_config_data *config = NULL;
1736 SMB_VFS_HANDLE_GET_DATA(handle, config,
1737 struct fruit_config_data, return -1);
1743 switch (config->meta) {
1744 case FRUIT_META_STREAM:
1746 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1747 fsp_set_fd(fsp, -1);
1749 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1753 case FRUIT_META_NETATALK:
1754 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1755 fsp_set_fd(fsp, -1);
1759 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1767 static int fruit_close_rsrc(vfs_handle_struct *handle,
1770 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1772 struct fruit_config_data *config = NULL;
1774 SMB_VFS_HANDLE_GET_DATA(handle, config,
1775 struct fruit_config_data, return -1);
1777 switch (config->rsrc) {
1778 case FRUIT_RSRC_STREAM:
1779 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1782 case FRUIT_RSRC_ADFILE:
1783 fio_close_ad_fsp(fio);
1784 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1785 fsp_set_fd(fsp, -1);
1788 case FRUIT_RSRC_XATTR:
1789 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1790 fsp_set_fd(fsp, -1);
1794 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1801 static int fruit_close(vfs_handle_struct *handle,
1807 fd = fsp_get_pathref_fd(fsp);
1809 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1811 if (!is_named_stream(fsp->fsp_name)) {
1812 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1815 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1816 ret = fruit_close_meta(handle, fsp);
1817 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1818 ret = fruit_close_rsrc(handle, fsp);
1820 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1826 static int fruit_renameat(struct vfs_handle_struct *handle,
1827 files_struct *srcfsp,
1828 const struct smb_filename *smb_fname_src,
1829 files_struct *dstfsp,
1830 const struct smb_filename *smb_fname_dst)
1833 struct fruit_config_data *config = NULL;
1834 struct smb_filename *src_adp_smb_fname = NULL;
1835 struct smb_filename *dst_adp_smb_fname = NULL;
1837 SMB_VFS_HANDLE_GET_DATA(handle, config,
1838 struct fruit_config_data, return -1);
1840 if (!VALID_STAT(smb_fname_src->st)) {
1841 DBG_ERR("Need valid stat for [%s]\n",
1842 smb_fname_str_dbg(smb_fname_src));
1846 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1855 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1856 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1861 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1866 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1871 DBG_DEBUG("%s -> %s\n",
1872 smb_fname_str_dbg(src_adp_smb_fname),
1873 smb_fname_str_dbg(dst_adp_smb_fname));
1875 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1880 if (errno == ENOENT) {
1885 TALLOC_FREE(src_adp_smb_fname);
1886 TALLOC_FREE(dst_adp_smb_fname);
1890 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1891 struct files_struct *dirfsp,
1892 const struct smb_filename *smb_fname)
1894 return SMB_VFS_NEXT_UNLINKAT(handle,
1900 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1901 const struct smb_filename *smb_fname)
1903 SMB_ASSERT(smb_fname->fsp != NULL);
1904 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
1905 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1906 AFPINFO_EA_NETATALK);
1909 static int fruit_unlink_meta(vfs_handle_struct *handle,
1910 struct files_struct *dirfsp,
1911 const struct smb_filename *smb_fname)
1913 struct fruit_config_data *config = NULL;
1916 SMB_VFS_HANDLE_GET_DATA(handle, config,
1917 struct fruit_config_data, return -1);
1919 switch (config->meta) {
1920 case FRUIT_META_STREAM:
1921 rc = fruit_unlink_meta_stream(handle,
1926 case FRUIT_META_NETATALK:
1927 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1931 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
1938 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
1939 struct files_struct *dirfsp,
1940 const struct smb_filename *smb_fname,
1945 if (!force_unlink) {
1946 struct smb_filename *full_fname = NULL;
1950 * TODO: use SMB_VFS_STATX() once we have it.
1953 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1956 if (full_fname == NULL) {
1961 * 0 byte resource fork streams are not listed by
1962 * vfs_streaminfo, as a result stream cleanup/deletion of file
1963 * deletion doesn't remove the resourcefork stream.
1966 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
1968 TALLOC_FREE(full_fname);
1969 DBG_ERR("stat [%s] failed [%s]\n",
1970 smb_fname_str_dbg(full_fname), strerror(errno));
1974 size = full_fname->st.st_ex_size;
1975 TALLOC_FREE(full_fname);
1978 /* OS X ignores resource fork stream delete requests */
1983 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1987 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
1994 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
1995 struct files_struct *dirfsp,
1996 const struct smb_filename *smb_fname,
2000 struct adouble *ad = NULL;
2001 struct smb_filename *adp_smb_fname = NULL;
2003 if (!force_unlink) {
2004 struct smb_filename *full_fname = NULL;
2006 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2009 if (full_fname == NULL) {
2013 ad = ad_get(talloc_tos(), handle, full_fname,
2015 TALLOC_FREE(full_fname);
2023 * 0 byte resource fork streams are not listed by
2024 * vfs_streaminfo, as a result stream cleanup/deletion of file
2025 * deletion doesn't remove the resourcefork stream.
2028 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2029 /* OS X ignores resource fork stream delete requests */
2037 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2042 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2046 TALLOC_FREE(adp_smb_fname);
2047 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2054 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2055 const struct smb_filename *smb_fname,
2059 * OS X ignores resource fork stream delete requests, so nothing to do
2060 * here. Removing the file will remove the xattr anyway, so we don't
2061 * have to take care of removing 0 byte resource forks that could be
2067 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2068 struct files_struct *dirfsp,
2069 const struct smb_filename *smb_fname,
2072 struct fruit_config_data *config = NULL;
2075 SMB_VFS_HANDLE_GET_DATA(handle, config,
2076 struct fruit_config_data, return -1);
2078 switch (config->rsrc) {
2079 case FRUIT_RSRC_STREAM:
2080 rc = fruit_unlink_rsrc_stream(handle,
2086 case FRUIT_RSRC_ADFILE:
2087 rc = fruit_unlink_rsrc_adouble(handle,
2093 case FRUIT_RSRC_XATTR:
2094 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2098 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2105 static int fruit_fchmod(vfs_handle_struct *handle,
2106 struct files_struct *fsp,
2110 struct fruit_config_data *config = NULL;
2111 struct smb_filename *smb_fname_adp = NULL;
2112 const struct smb_filename *smb_fname = NULL;
2115 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2120 smb_fname = fsp->fsp_name;
2121 SMB_VFS_HANDLE_GET_DATA(handle, config,
2122 struct fruit_config_data, return -1);
2124 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2128 if (!VALID_STAT(smb_fname->st)) {
2132 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2136 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2141 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2143 if (!NT_STATUS_IS_OK(status)) {
2144 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2145 if (NT_STATUS_EQUAL(status,
2146 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2154 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2156 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2157 if (errno == ENOENT) {
2161 TALLOC_FREE(smb_fname_adp);
2165 static int fruit_unlinkat(vfs_handle_struct *handle,
2166 struct files_struct *dirfsp,
2167 const struct smb_filename *smb_fname,
2170 struct fruit_config_data *config = NULL;
2171 struct smb_filename *rsrc_smb_fname = NULL;
2174 if (flags & AT_REMOVEDIR) {
2175 return SMB_VFS_NEXT_UNLINKAT(handle,
2181 SMB_VFS_HANDLE_GET_DATA(handle, config,
2182 struct fruit_config_data, return -1);
2184 if (is_afpinfo_stream(smb_fname->stream_name)) {
2185 return fruit_unlink_meta(handle,
2188 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2189 return fruit_unlink_rsrc(handle,
2193 } else if (is_named_stream(smb_fname)) {
2194 return SMB_VFS_NEXT_UNLINKAT(handle,
2198 } else if (is_adouble_file(smb_fname->base_name)) {
2199 return SMB_VFS_NEXT_UNLINKAT(handle,
2206 * A request to delete the base file. Because 0 byte resource
2207 * fork streams are not listed by fruit_streaminfo,
2208 * delete_all_streams() can't remove 0 byte resource fork
2209 * streams, so we have to cleanup this here.
2211 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2212 smb_fname->base_name,
2213 AFPRESOURCE_STREAM_NAME,
2217 if (rsrc_smb_fname == NULL) {
2221 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2222 if ((ret != 0) && (errno != ENOENT)) {
2223 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2224 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2225 TALLOC_FREE(rsrc_smb_fname);
2228 TALLOC_FREE(rsrc_smb_fname);
2230 return SMB_VFS_NEXT_UNLINKAT(handle,
2236 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2237 files_struct *fsp, void *data,
2238 size_t n, off_t offset)
2240 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2244 if ((fio == NULL) || fio->fake_fd) {
2248 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2249 if (nread == -1 || nread == n) {
2253 DBG_ERR("Removing [%s] after short read [%zd]\n",
2254 fsp_str_dbg(fsp), nread);
2256 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2261 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2269 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2270 files_struct *fsp, void *data,
2271 size_t n, off_t offset)
2274 struct adouble *ad = NULL;
2275 char afpinfo_buf[AFP_INFO_SIZE];
2279 ai = afpinfo_new(talloc_tos());
2284 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2290 p = ad_get_entry(ad, ADEID_FINDERI);
2292 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2297 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2299 nread = afpinfo_pack(ai, afpinfo_buf);
2300 if (nread != AFP_INFO_SIZE) {
2305 memcpy(data, afpinfo_buf, n);
2313 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2314 files_struct *fsp, void *data,
2315 size_t n, off_t offset)
2317 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2322 * OS X has a off-by-1 error in the offset calculation, so we're
2323 * bug compatible here. It won't hurt, as any relevant real
2324 * world read requests from the AFP_AfpInfo stream will be
2325 * offset=0 n=60. offset is ignored anyway, see below.
2327 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2332 DBG_ERR("Failed to fetch fsp extension");
2336 /* Yes, macOS always reads from offset 0 */
2338 to_return = MIN(n, AFP_INFO_SIZE);
2340 switch (fio->config->meta) {
2341 case FRUIT_META_STREAM:
2342 nread = fruit_pread_meta_stream(handle, fsp, data,
2346 case FRUIT_META_NETATALK:
2347 nread = fruit_pread_meta_adouble(handle, fsp, data,
2352 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2356 if (nread == -1 && fio->fake_fd) {
2358 char afpinfo_buf[AFP_INFO_SIZE];
2360 ai = afpinfo_new(talloc_tos());
2365 nread = afpinfo_pack(ai, afpinfo_buf);
2367 if (nread != AFP_INFO_SIZE) {
2371 memcpy(data, afpinfo_buf, to_return);
2378 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2379 files_struct *fsp, void *data,
2380 size_t n, off_t offset)
2382 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2385 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2386 files_struct *fsp, void *data,
2387 size_t n, off_t offset)
2389 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2392 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2393 files_struct *fsp, void *data,
2394 size_t n, off_t offset)
2396 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2397 struct adouble *ad = NULL;
2400 if (fio->ad_fsp == NULL) {
2401 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2406 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2408 DBG_ERR("ad_fget [%s] failed [%s]\n",
2409 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2413 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2414 offset + ad_getentryoff(ad, ADEID_RFORK));
2420 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2421 files_struct *fsp, void *data,
2422 size_t n, off_t offset)
2424 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2432 switch (fio->config->rsrc) {
2433 case FRUIT_RSRC_STREAM:
2434 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2437 case FRUIT_RSRC_ADFILE:
2438 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2441 case FRUIT_RSRC_XATTR:
2442 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2446 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2453 static ssize_t fruit_pread(vfs_handle_struct *handle,
2454 files_struct *fsp, void *data,
2455 size_t n, off_t offset)
2457 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2460 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2461 fsp_str_dbg(fsp), (intmax_t)offset, n);
2464 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2467 if (fio->type == ADOUBLE_META) {
2468 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2470 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2473 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2477 static bool fruit_must_handle_aio_stream(struct fio *fio)
2483 if (fio->type == ADOUBLE_META) {
2487 if ((fio->type == ADOUBLE_RSRC) &&
2488 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2496 struct fruit_pread_state {
2498 struct vfs_aio_state vfs_aio_state;
2501 static void fruit_pread_done(struct tevent_req *subreq);
2503 static struct tevent_req *fruit_pread_send(
2504 struct vfs_handle_struct *handle,
2505 TALLOC_CTX *mem_ctx,
2506 struct tevent_context *ev,
2507 struct files_struct *fsp,
2509 size_t n, off_t offset)
2511 struct tevent_req *req = NULL;
2512 struct tevent_req *subreq = NULL;
2513 struct fruit_pread_state *state = NULL;
2514 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2516 req = tevent_req_create(mem_ctx, &state,
2517 struct fruit_pread_state);
2522 if (fruit_must_handle_aio_stream(fio)) {
2523 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2524 if (state->nread != n) {
2525 if (state->nread != -1) {
2528 tevent_req_error(req, errno);
2529 return tevent_req_post(req, ev);
2531 tevent_req_done(req);
2532 return tevent_req_post(req, ev);
2535 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2537 if (tevent_req_nomem(req, subreq)) {
2538 return tevent_req_post(req, ev);
2540 tevent_req_set_callback(subreq, fruit_pread_done, req);
2544 static void fruit_pread_done(struct tevent_req *subreq)
2546 struct tevent_req *req = tevent_req_callback_data(
2547 subreq, struct tevent_req);
2548 struct fruit_pread_state *state = tevent_req_data(
2549 req, struct fruit_pread_state);
2551 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2552 TALLOC_FREE(subreq);
2554 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2557 tevent_req_done(req);
2560 static ssize_t fruit_pread_recv(struct tevent_req *req,
2561 struct vfs_aio_state *vfs_aio_state)
2563 struct fruit_pread_state *state = tevent_req_data(
2564 req, struct fruit_pread_state);
2566 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2570 *vfs_aio_state = state->vfs_aio_state;
2571 return state->nread;
2574 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2575 files_struct *fsp, const void *data,
2576 size_t n, off_t offset)
2578 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2584 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2585 fsp_str_dbg(fsp), (intmax_t)offset, n);
2592 int fd = fsp_get_pathref_fd(fsp);
2594 ret = vfs_fake_fd_close(fd);
2595 fsp_set_fd(fsp, -1);
2597 DBG_ERR("Close [%s] failed: %s\n",
2598 fsp_str_dbg(fsp), strerror(errno));
2602 fd = SMB_VFS_NEXT_OPENAT(handle,
2609 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2610 fsp_str_dbg(fsp), strerror(errno));
2613 fsp_set_fd(fsp, fd);
2614 fio->fake_fd = false;
2617 ai = afpinfo_unpack(talloc_tos(), data);
2622 if (ai_empty_finderinfo(ai)) {
2624 * Writing an all 0 blob to the metadata stream results in the
2625 * stream being removed on a macOS server. This ensures we
2626 * behave the same and it verified by the "delete AFP_AfpInfo by
2627 * writing all 0" test.
2629 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2631 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2636 ok = set_delete_on_close(
2639 handle->conn->session_info->security_token,
2640 handle->conn->session_info->unix_token);
2642 DBG_ERR("set_delete_on_close on [%s] failed\n",
2649 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2650 if (nwritten != n) {
2657 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2658 files_struct *fsp, const void *data,
2659 size_t n, off_t offset)
2661 struct adouble *ad = NULL;
2667 ai = afpinfo_unpack(talloc_tos(), data);
2672 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2674 ad = ad_init(talloc_tos(), ADOUBLE_META);
2679 p = ad_get_entry(ad, ADEID_FINDERI);
2681 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2686 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2688 ret = ad_fset(handle, ad, fsp);
2690 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2697 if (!ai_empty_finderinfo(ai)) {
2702 * Writing an all 0 blob to the metadata stream results in the stream
2703 * being removed on a macOS server. This ensures we behave the same and
2704 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2707 ok = set_delete_on_close(
2710 handle->conn->session_info->security_token,
2711 handle->conn->session_info->unix_token);
2713 DBG_ERR("set_delete_on_close on [%s] failed\n",
2721 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2722 files_struct *fsp, const void *data,
2723 size_t n, off_t offset)
2725 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2727 uint8_t buf[AFP_INFO_SIZE];
2733 DBG_ERR("Failed to fetch fsp extension");
2742 if (offset != 0 && n < 60) {
2747 cmp = memcmp(data, "AFP", 3);
2753 if (n <= AFP_OFF_FinderInfo) {
2755 * Nothing to do here really, just return
2763 if (to_copy > AFP_INFO_SIZE) {
2764 to_copy = AFP_INFO_SIZE;
2766 memcpy(buf, data, to_copy);
2769 if (to_write != AFP_INFO_SIZE) {
2770 to_write = AFP_INFO_SIZE;
2773 switch (fio->config->meta) {
2774 case FRUIT_META_STREAM:
2775 nwritten = fruit_pwrite_meta_stream(handle,
2782 case FRUIT_META_NETATALK:
2783 nwritten = fruit_pwrite_meta_netatalk(handle,
2791 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2795 if (nwritten != to_write) {
2800 * Return the requested amount, verified against macOS SMB server
2805 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2806 files_struct *fsp, const void *data,
2807 size_t n, off_t offset)
2809 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2812 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2813 files_struct *fsp, const void *data,
2814 size_t n, off_t offset)
2816 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2819 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2820 files_struct *fsp, const void *data,
2821 size_t n, off_t offset)
2823 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2824 struct adouble *ad = NULL;
2828 if (fio->ad_fsp == NULL) {
2829 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2834 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2836 DBG_ERR("ad_fget [%s] failed [%s]\n",
2837 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2841 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2842 offset + ad_getentryoff(ad, ADEID_RFORK));
2843 if (nwritten != n) {
2844 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2845 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2850 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2851 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2852 ret = ad_fset(handle, ad, fio->ad_fsp);
2854 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2864 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2865 files_struct *fsp, const void *data,
2866 size_t n, off_t offset)
2868 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2872 DBG_ERR("Failed to fetch fsp extension");
2876 switch (fio->config->rsrc) {
2877 case FRUIT_RSRC_STREAM:
2878 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2881 case FRUIT_RSRC_ADFILE:
2882 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2885 case FRUIT_RSRC_XATTR:
2886 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2890 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2897 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2898 files_struct *fsp, const void *data,
2899 size_t n, off_t offset)
2901 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2904 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2905 fsp_str_dbg(fsp), (intmax_t)offset, n);
2908 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2911 if (fio->type == ADOUBLE_META) {
2912 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2914 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2917 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2921 struct fruit_pwrite_state {
2923 struct vfs_aio_state vfs_aio_state;
2926 static void fruit_pwrite_done(struct tevent_req *subreq);
2928 static struct tevent_req *fruit_pwrite_send(
2929 struct vfs_handle_struct *handle,
2930 TALLOC_CTX *mem_ctx,
2931 struct tevent_context *ev,
2932 struct files_struct *fsp,
2934 size_t n, off_t offset)
2936 struct tevent_req *req = NULL;
2937 struct tevent_req *subreq = NULL;
2938 struct fruit_pwrite_state *state = NULL;
2939 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2941 req = tevent_req_create(mem_ctx, &state,
2942 struct fruit_pwrite_state);
2947 if (fruit_must_handle_aio_stream(fio)) {
2948 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
2949 if (state->nwritten != n) {
2950 if (state->nwritten != -1) {
2953 tevent_req_error(req, errno);
2954 return tevent_req_post(req, ev);
2956 tevent_req_done(req);
2957 return tevent_req_post(req, ev);
2960 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
2962 if (tevent_req_nomem(req, subreq)) {
2963 return tevent_req_post(req, ev);
2965 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
2969 static void fruit_pwrite_done(struct tevent_req *subreq)
2971 struct tevent_req *req = tevent_req_callback_data(
2972 subreq, struct tevent_req);
2973 struct fruit_pwrite_state *state = tevent_req_data(
2974 req, struct fruit_pwrite_state);
2976 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2977 TALLOC_FREE(subreq);
2979 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2982 tevent_req_done(req);
2985 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
2986 struct vfs_aio_state *vfs_aio_state)
2988 struct fruit_pwrite_state *state = tevent_req_data(
2989 req, struct fruit_pwrite_state);
2991 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2995 *vfs_aio_state = state->vfs_aio_state;
2996 return state->nwritten;
3000 * Helper to stat/lstat the base file of an smb_fname.
3002 static int fruit_stat_base(vfs_handle_struct *handle,
3003 struct smb_filename *smb_fname,
3006 char *tmp_stream_name;
3009 tmp_stream_name = smb_fname->stream_name;
3010 smb_fname->stream_name = NULL;
3012 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3014 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3016 smb_fname->stream_name = tmp_stream_name;
3018 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3019 smb_fname->base_name,
3020 (uintmax_t)smb_fname->st.st_ex_dev,
3021 (uintmax_t)smb_fname->st.st_ex_ino);
3025 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3026 struct smb_filename *smb_fname,
3032 ret = fruit_stat_base(handle, smb_fname, false);
3037 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3040 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3042 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3045 smb_fname->st.st_ex_ino = ino;
3050 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3051 struct smb_filename *smb_fname,
3054 struct adouble *ad = NULL;
3056 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3058 DBG_INFO("fruit_stat_meta %s: %s\n",
3059 smb_fname_str_dbg(smb_fname), strerror(errno));
3065 /* Populate the stat struct with info from the base file. */
3066 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3069 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3070 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3071 smb_fname->stream_name);
3075 static int fruit_stat_meta(vfs_handle_struct *handle,
3076 struct smb_filename *smb_fname,
3079 struct fruit_config_data *config = NULL;
3082 SMB_VFS_HANDLE_GET_DATA(handle, config,
3083 struct fruit_config_data, return -1);
3085 switch (config->meta) {
3086 case FRUIT_META_STREAM:
3087 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3090 case FRUIT_META_NETATALK:
3091 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3095 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3102 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3103 struct smb_filename *smb_fname,
3106 struct adouble *ad = NULL;
3109 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3115 /* Populate the stat struct with info from the base file. */
3116 ret = fruit_stat_base(handle, smb_fname, follow_links);
3122 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3123 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3124 smb_fname->stream_name);
3129 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3130 struct smb_filename *smb_fname,
3136 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3138 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3144 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3145 struct smb_filename *smb_fname,
3148 #ifdef HAVE_ATTROPEN
3152 /* Populate the stat struct with info from the base file. */
3153 ret = fruit_stat_base(handle, smb_fname, follow_links);
3158 fd = attropen(smb_fname->base_name,
3159 AFPRESOURCE_EA_NETATALK,
3165 ret = sys_fstat(fd, &smb_fname->st, false);
3168 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3169 AFPRESOURCE_EA_NETATALK);
3175 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3176 smb_fname->stream_name);
3186 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3187 struct smb_filename *smb_fname,
3190 struct fruit_config_data *config = NULL;
3193 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3195 SMB_VFS_HANDLE_GET_DATA(handle, config,
3196 struct fruit_config_data, return -1);
3198 switch (config->rsrc) {
3199 case FRUIT_RSRC_STREAM:
3200 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3203 case FRUIT_RSRC_XATTR:
3204 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3207 case FRUIT_RSRC_ADFILE:
3208 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3212 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3219 static int fruit_stat(vfs_handle_struct *handle,
3220 struct smb_filename *smb_fname)
3224 DEBUG(10, ("fruit_stat called for %s\n",
3225 smb_fname_str_dbg(smb_fname)));
3227 if (!is_named_stream(smb_fname)) {
3228 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3230 update_btime(handle, smb_fname);
3236 * Note if lp_posix_paths() is true, we can never
3237 * get here as is_ntfs_stream_smb_fname() is
3238 * always false. So we never need worry about
3239 * not following links here.
3242 if (is_afpinfo_stream(smb_fname->stream_name)) {
3243 rc = fruit_stat_meta(handle, smb_fname, true);
3244 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3245 rc = fruit_stat_rsrc(handle, smb_fname, true);
3247 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3251 update_btime(handle, smb_fname);
3252 smb_fname->st.st_ex_mode &= ~S_IFMT;
3253 smb_fname->st.st_ex_mode |= S_IFREG;
3254 smb_fname->st.st_ex_blocks =
3255 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3260 static int fruit_lstat(vfs_handle_struct *handle,
3261 struct smb_filename *smb_fname)
3265 DEBUG(10, ("fruit_lstat called for %s\n",
3266 smb_fname_str_dbg(smb_fname)));
3268 if (!is_named_stream(smb_fname)) {
3269 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3271 update_btime(handle, smb_fname);
3276 if (is_afpinfo_stream(smb_fname->stream_name)) {
3277 rc = fruit_stat_meta(handle, smb_fname, false);
3278 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3279 rc = fruit_stat_rsrc(handle, smb_fname, false);
3281 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3285 update_btime(handle, smb_fname);
3286 smb_fname->st.st_ex_mode &= ~S_IFMT;
3287 smb_fname->st.st_ex_mode |= S_IFREG;
3288 smb_fname->st.st_ex_blocks =
3289 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3294 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3296 SMB_STRUCT_STAT *sbuf)
3298 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3299 struct smb_filename smb_fname;
3308 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3313 *sbuf = fsp->base_fsp->fsp_name->st;
3314 sbuf->st_ex_size = AFP_INFO_SIZE;
3315 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3319 smb_fname = (struct smb_filename) {
3320 .base_name = fsp->fsp_name->base_name,
3321 .twrp = fsp->fsp_name->twrp,
3324 ret = fruit_stat_base(handle, &smb_fname, false);
3328 *sbuf = smb_fname.st;
3330 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3332 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3337 sbuf->st_ex_ino = ino;
3341 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3343 SMB_STRUCT_STAT *sbuf)
3347 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3352 *sbuf = fsp->base_fsp->fsp_name->st;
3353 sbuf->st_ex_size = AFP_INFO_SIZE;
3354 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3359 static int fruit_fstat_meta(vfs_handle_struct *handle,
3361 SMB_STRUCT_STAT *sbuf,
3366 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3368 switch (fio->config->meta) {
3369 case FRUIT_META_STREAM:
3370 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3373 case FRUIT_META_NETATALK:
3374 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3378 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3382 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3386 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3388 SMB_STRUCT_STAT *sbuf)
3390 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3393 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3395 SMB_STRUCT_STAT *sbuf)
3397 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3400 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3402 SMB_STRUCT_STAT *sbuf)
3404 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3405 struct adouble *ad = NULL;
3408 if (fio->ad_fsp == NULL) {
3409 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3414 /* Populate the stat struct with info from the base file. */
3415 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3420 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3422 DBG_ERR("ad_fget [%s] failed [%s]\n",
3423 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3427 *sbuf = fsp->base_fsp->fsp_name->st;
3428 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3429 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3435 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3436 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3440 switch (fio->config->rsrc) {
3441 case FRUIT_RSRC_STREAM:
3442 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3445 case FRUIT_RSRC_ADFILE:
3446 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3449 case FRUIT_RSRC_XATTR:
3450 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3454 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3461 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3462 SMB_STRUCT_STAT *sbuf)
3464 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3468 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3471 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3473 if (fio->type == ADOUBLE_META) {
3474 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3476 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3480 sbuf->st_ex_mode &= ~S_IFMT;
3481 sbuf->st_ex_mode |= S_IFREG;
3482 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3485 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3486 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3490 static NTSTATUS delete_invalid_meta_stream(
3491 vfs_handle_struct *handle,
3492 const struct smb_filename *smb_fname,
3493 TALLOC_CTX *mem_ctx,
3494 unsigned int *pnum_streams,
3495 struct stream_struct **pstreams,
3498 struct smb_filename *sname = NULL;
3503 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3505 return NT_STATUS_INTERNAL_ERROR;
3509 return NT_STATUS_OK;
3512 status = synthetic_pathref(talloc_tos(),
3513 handle->conn->cwd_fsp,
3514 smb_fname->base_name,
3515 AFPINFO_STREAM_NAME,
3520 if (!NT_STATUS_IS_OK(status)) {
3521 return NT_STATUS_NO_MEMORY;
3524 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3525 handle->conn->cwd_fsp,
3529 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3531 return map_nt_error_from_unix(errno);
3535 return NT_STATUS_OK;
3538 static NTSTATUS fruit_streaminfo_meta_stream(
3539 vfs_handle_struct *handle,
3540 struct files_struct *fsp,
3541 const struct smb_filename *smb_fname,
3542 TALLOC_CTX *mem_ctx,
3543 unsigned int *pnum_streams,
3544 struct stream_struct **pstreams)
3546 struct stream_struct *stream = *pstreams;
3547 unsigned int num_streams = *pnum_streams;
3550 for (i = 0; i < num_streams; i++) {
3551 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3556 if (i == num_streams) {
3557 return NT_STATUS_OK;
3560 if (stream[i].size != AFP_INFO_SIZE) {
3561 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3562 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3564 return delete_invalid_meta_stream(handle,
3573 return NT_STATUS_OK;
3576 static NTSTATUS fruit_streaminfo_meta_netatalk(
3577 vfs_handle_struct *handle,
3578 struct files_struct *fsp,
3579 const struct smb_filename *smb_fname,
3580 TALLOC_CTX *mem_ctx,
3581 unsigned int *pnum_streams,
3582 struct stream_struct **pstreams)
3584 struct stream_struct *stream = *pstreams;
3585 unsigned int num_streams = *pnum_streams;
3586 struct adouble *ad = NULL;
3591 /* Remove the Netatalk xattr from the list */
3592 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3593 ":" NETATALK_META_XATTR ":$DATA");
3595 return NT_STATUS_NO_MEMORY;
3599 * Check if there's a AFPINFO_STREAM from the VFS streams
3600 * backend and if yes, remove it from the list
3602 for (i = 0; i < num_streams; i++) {
3603 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3608 if (i < num_streams) {
3609 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3610 smb_fname_str_dbg(smb_fname));
3612 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3615 return NT_STATUS_INTERNAL_ERROR;
3619 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3621 return NT_STATUS_OK;
3624 is_fi_empty = ad_empty_finderinfo(ad);
3628 return NT_STATUS_OK;
3631 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3632 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3633 smb_roundup(handle->conn, AFP_INFO_SIZE));
3635 return NT_STATUS_NO_MEMORY;
3638 return NT_STATUS_OK;
3641 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3642 struct files_struct *fsp,
3643 const struct smb_filename *smb_fname,
3644 TALLOC_CTX *mem_ctx,
3645 unsigned int *pnum_streams,
3646 struct stream_struct **pstreams)
3648 struct fruit_config_data *config = NULL;
3651 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3652 return NT_STATUS_INTERNAL_ERROR);
3654 switch (config->meta) {
3655 case FRUIT_META_NETATALK:
3656 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3657 mem_ctx, pnum_streams,
3661 case FRUIT_META_STREAM:
3662 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3663 mem_ctx, pnum_streams,
3668 return NT_STATUS_INTERNAL_ERROR;
3674 static NTSTATUS fruit_streaminfo_rsrc_stream(
3675 vfs_handle_struct *handle,
3676 struct files_struct *fsp,
3677 const struct smb_filename *smb_fname,
3678 TALLOC_CTX *mem_ctx,
3679 unsigned int *pnum_streams,
3680 struct stream_struct **pstreams)
3684 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3686 DBG_ERR("Filtering resource stream failed\n");
3687 return NT_STATUS_INTERNAL_ERROR;
3689 return NT_STATUS_OK;
3692 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3693 vfs_handle_struct *handle,
3694 struct files_struct *fsp,
3695 const struct smb_filename *smb_fname,
3696 TALLOC_CTX *mem_ctx,
3697 unsigned int *pnum_streams,
3698 struct stream_struct **pstreams)
3702 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3704 DBG_ERR("Filtering resource stream failed\n");
3705 return NT_STATUS_INTERNAL_ERROR;
3707 return NT_STATUS_OK;
3710 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3711 vfs_handle_struct *handle,
3712 struct files_struct *fsp,
3713 const struct smb_filename *smb_fname,
3714 TALLOC_CTX *mem_ctx,
3715 unsigned int *pnum_streams,
3716 struct stream_struct **pstreams)
3718 struct stream_struct *stream = *pstreams;
3719 unsigned int num_streams = *pnum_streams;
3720 struct adouble *ad = NULL;
3726 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3727 * and if yes, remove it from the list
3729 for (i = 0; i < num_streams; i++) {
3730 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3735 if (i < num_streams) {
3736 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3737 smb_fname_str_dbg(smb_fname));
3739 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3740 AFPRESOURCE_STREAM);
3742 return NT_STATUS_INTERNAL_ERROR;
3746 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3748 return NT_STATUS_OK;
3751 rlen = ad_getentrylen(ad, ADEID_RFORK);
3755 return NT_STATUS_OK;
3758 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3759 AFPRESOURCE_STREAM_NAME, rlen,
3760 smb_roundup(handle->conn, rlen));
3762 return NT_STATUS_NO_MEMORY;
3765 return NT_STATUS_OK;
3768 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3769 struct files_struct *fsp,
3770 const struct smb_filename *smb_fname,
3771 TALLOC_CTX *mem_ctx,
3772 unsigned int *pnum_streams,
3773 struct stream_struct **pstreams)
3775 struct fruit_config_data *config = NULL;
3778 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3779 return NT_STATUS_INTERNAL_ERROR);
3781 switch (config->rsrc) {
3782 case FRUIT_RSRC_STREAM:
3783 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3784 mem_ctx, pnum_streams,
3788 case FRUIT_RSRC_XATTR:
3789 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3790 mem_ctx, pnum_streams,
3794 case FRUIT_RSRC_ADFILE:
3795 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3796 mem_ctx, pnum_streams,
3801 return NT_STATUS_INTERNAL_ERROR;
3807 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3808 struct stream_struct **pstreams)
3810 unsigned num_streams = *pnum_streams;
3811 struct stream_struct *streams = *pstreams;
3814 if (!global_fruit_config.nego_aapl) {
3818 while (i < num_streams) {
3819 struct smb_filename smb_fname = (struct smb_filename) {
3820 .stream_name = streams[i].name,
3823 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3824 || streams[i].size > 0)
3830 streams[i] = streams[num_streams - 1];
3834 *pnum_streams = num_streams;
3837 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3838 struct files_struct *fsp,
3839 const struct smb_filename *smb_fname,
3840 TALLOC_CTX *mem_ctx,
3841 unsigned int *pnum_streams,
3842 struct stream_struct **pstreams)
3844 struct fruit_config_data *config = NULL;
3847 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3848 return NT_STATUS_UNSUCCESSFUL);
3850 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3852 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3853 pnum_streams, pstreams);
3854 if (!NT_STATUS_IS_OK(status)) {
3858 fruit_filter_empty_streams(pnum_streams, pstreams);
3860 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3861 mem_ctx, pnum_streams, pstreams);
3862 if (!NT_STATUS_IS_OK(status)) {
3866 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3867 mem_ctx, pnum_streams, pstreams);
3868 if (!NT_STATUS_IS_OK(status)) {
3872 return NT_STATUS_OK;
3875 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
3876 struct files_struct *fsp,
3877 TALLOC_CTX *mem_ctx,
3878 unsigned int *pnum_streams,
3879 struct stream_struct **pstreams)
3881 struct fruit_config_data *config = NULL;
3882 const struct smb_filename *smb_fname = NULL;
3885 smb_fname = fsp->fsp_name;
3887 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3888 return NT_STATUS_UNSUCCESSFUL);
3890 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3892 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
3893 pnum_streams, pstreams);
3894 if (!NT_STATUS_IS_OK(status)) {
3898 fruit_filter_empty_streams(pnum_streams, pstreams);
3900 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3901 mem_ctx, pnum_streams, pstreams);
3902 if (!NT_STATUS_IS_OK(status)) {
3906 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3907 mem_ctx, pnum_streams, pstreams);
3908 if (!NT_STATUS_IS_OK(status)) {
3912 return NT_STATUS_OK;
3915 static int fruit_fntimes(vfs_handle_struct *handle,
3917 struct smb_file_time *ft)
3920 struct adouble *ad = NULL;
3921 struct fruit_config_data *config = NULL;
3923 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3926 if ((config->meta != FRUIT_META_NETATALK) ||
3927 is_omit_timespec(&ft->create_time))
3929 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3932 DBG_DEBUG("set btime for %s to %s\n", fsp_str_dbg(fsp),
3933 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
3935 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3940 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3941 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3943 rc = ad_fset(handle, ad, fsp);
3949 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
3952 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3955 static int fruit_fallocate(struct vfs_handle_struct *handle,
3956 struct files_struct *fsp,
3961 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3964 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3967 /* Let the pwrite code path handle it. */
3972 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
3973 struct files_struct *fsp,
3976 #ifdef HAVE_ATTROPEN
3977 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3982 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
3983 struct files_struct *fsp,
3986 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3988 struct adouble *ad = NULL;
3991 if (fio->ad_fsp == NULL) {
3992 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3997 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3999 DBG_ERR("ad_fget [%s] failed [%s]\n",
4000 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4004 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4006 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4012 ad_setentrylen(ad, ADEID_RFORK, offset);
4014 rc = ad_fset(handle, ad, fio->ad_fsp);
4016 DBG_ERR("ad_fset [%s] failed [%s]\n",
4017 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4026 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4027 struct files_struct *fsp,
4030 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4033 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4034 struct files_struct *fsp,
4037 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4041 DBG_ERR("Failed to fetch fsp extension");
4045 switch (fio->config->rsrc) {
4046 case FRUIT_RSRC_XATTR:
4047 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4050 case FRUIT_RSRC_ADFILE:
4051 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4054 case FRUIT_RSRC_STREAM:
4055 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4059 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4067 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4068 struct files_struct *fsp,
4072 DBG_WARNING("ftruncate %s to %jd",
4073 fsp_str_dbg(fsp), (intmax_t)offset);
4074 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4079 /* OS X returns success but does nothing */
4080 DBG_INFO("ignoring ftruncate %s to %jd\n",
4081 fsp_str_dbg(fsp), (intmax_t)offset);
4085 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4086 struct files_struct *fsp,
4089 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4092 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4096 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4099 if (fio->type == ADOUBLE_META) {
4100 ret = fruit_ftruncate_meta(handle, fsp, offset);
4102 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4105 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4109 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4110 struct smb_request *req,
4111 struct smb_filename *smb_fname,
4112 uint32_t access_mask,
4113 uint32_t share_access,
4114 uint32_t create_disposition,
4115 uint32_t create_options,
4116 uint32_t file_attributes,
4117 uint32_t oplock_request,
4118 const struct smb2_lease *lease,
4119 uint64_t allocation_size,
4120 uint32_t private_flags,
4121 struct security_descriptor *sd,
4122 struct ea_list *ea_list,
4123 files_struct **result,
4125 const struct smb2_create_blobs *in_context_blobs,
4126 struct smb2_create_blobs *out_context_blobs)
4129 struct fruit_config_data *config = NULL;
4130 files_struct *fsp = NULL;
4131 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4134 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4135 if (!NT_STATUS_IS_OK(status)) {
4139 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4140 return NT_STATUS_UNSUCCESSFUL);
4142 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4143 uint32_t conv_flags = 0;
4145 if (config->wipe_intentionally_left_blank_rfork) {
4146 conv_flags |= AD_CONV_WIPE_BLANK;
4148 if (config->delete_empty_adfiles) {
4149 conv_flags |= AD_CONV_DELETE;
4152 ret = ad_convert(handle,
4154 macos_string_replace_map,
4157 DBG_ERR("ad_convert() failed\n");
4158 return NT_STATUS_UNSUCCESSFUL;
4162 status = SMB_VFS_NEXT_CREATE_FILE(
4163 handle, req, smb_fname,
4164 access_mask, share_access,
4165 create_disposition, create_options,
4166 file_attributes, oplock_request,
4168 allocation_size, private_flags,
4169 sd, ea_list, result,
4170 pinfo, in_context_blobs, out_context_blobs);
4171 if (!NT_STATUS_IS_OK(status)) {
4177 if (global_fruit_config.nego_aapl) {
4178 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4180 * Enable POSIX directory rename behaviour
4182 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4187 * If this is a plain open for existing files, opening an 0
4188 * byte size resource fork MUST fail with
4189 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4191 * Cf the vfs_fruit torture tests in test_rfork_create().
4193 if (global_fruit_config.nego_aapl &&
4194 create_disposition == FILE_OPEN &&
4195 smb_fname->st.st_ex_size == 0 &&
4196 is_named_stream(smb_fname))
4198 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4202 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4206 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4207 (fsp->op != NULL) &&
4208 !fsp->fsp_flags.is_pathref)
4210 status = fruit_check_access(
4214 if (!NT_STATUS_IS_OK(status)) {
4222 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4225 close_file(req, fsp, ERROR_CLOSE);
4226 *result = fsp = NULL;
4232 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4233 const struct smb_filename *fname,
4234 TALLOC_CTX *mem_ctx,
4235 struct readdir_attr_data **pattr_data)
4237 struct fruit_config_data *config = NULL;
4238 struct readdir_attr_data *attr_data;
4239 uint32_t conv_flags = 0;
4243 SMB_VFS_HANDLE_GET_DATA(handle, config,
4244 struct fruit_config_data,
4245 return NT_STATUS_UNSUCCESSFUL);
4247 if (!global_fruit_config.nego_aapl) {
4248 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4251 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4253 if (config->wipe_intentionally_left_blank_rfork) {
4254 conv_flags |= AD_CONV_WIPE_BLANK;
4256 if (config->delete_empty_adfiles) {
4257 conv_flags |= AD_CONV_DELETE;
4260 ret = ad_convert(handle,
4262 macos_string_replace_map,
4265 DBG_ERR("ad_convert() failed\n");
4266 return NT_STATUS_UNSUCCESSFUL;
4269 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4270 if (*pattr_data == NULL) {
4271 return NT_STATUS_UNSUCCESSFUL;
4273 attr_data = *pattr_data;
4274 attr_data->type = RDATTR_AAPL;
4277 * Mac metadata: compressed FinderInfo, resource fork length
4280 status = readdir_attr_macmeta(handle, fname, attr_data);
4281 if (!NT_STATUS_IS_OK(status)) {
4283 * Error handling is tricky: if we return failure from
4284 * this function, the corresponding directory entry
4285 * will to be passed to the client, so we really just
4286 * want to error out on fatal errors.
4288 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4296 if (config->unix_info_enabled) {
4297 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4303 if (!config->readdir_attr_max_access) {
4304 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4306 status = smbd_calculate_access_mask(
4308 handle->conn->cwd_fsp,
4311 SEC_FLAG_MAXIMUM_ALLOWED,
4312 &attr_data->attr_data.aapl.max_access);
4313 if (!NT_STATUS_IS_OK(status)) {
4318 return NT_STATUS_OK;
4321 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4322 fname->base_name, nt_errstr(status)));
4323 TALLOC_FREE(*pattr_data);
4327 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4329 uint32_t security_info,
4330 TALLOC_CTX *mem_ctx,
4331 struct security_descriptor **ppdesc)
4334 struct security_ace ace;
4336 struct fruit_config_data *config;
4338 SMB_VFS_HANDLE_GET_DATA(handle, config,
4339 struct fruit_config_data,
4340 return NT_STATUS_UNSUCCESSFUL);
4342 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4344 if (!NT_STATUS_IS_OK(status)) {
4349 * Add MS NFS style ACEs with uid, gid and mode
4351 if (!global_fruit_config.nego_aapl) {
4352 return NT_STATUS_OK;
4354 if (!config->unix_info_enabled) {
4355 return NT_STATUS_OK;
4358 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4359 status = remove_virtual_nfs_aces(*ppdesc);
4360 if (!NT_STATUS_IS_OK(status)) {
4361 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4365 /* MS NFS style mode */
4366 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4367 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4368 status = security_descriptor_dacl_add(*ppdesc, &ace);
4369 if (!NT_STATUS_IS_OK(status)) {
4370 DEBUG(1,("failed to add MS NFS style ACE\n"));
4374 /* MS NFS style uid */
4375 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4376 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4377 status = security_descriptor_dacl_add(*ppdesc, &ace);
4378 if (!NT_STATUS_IS_OK(status)) {
4379 DEBUG(1,("failed to add MS NFS style ACE\n"));
4383 /* MS NFS style gid */
4384 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4385 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4386 status = security_descriptor_dacl_add(*ppdesc, &ace);
4387 if (!NT_STATUS_IS_OK(status)) {
4388 DEBUG(1,("failed to add MS NFS style ACE\n"));
4392 return NT_STATUS_OK;
4395 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4397 uint32_t security_info_sent,
4398 const struct security_descriptor *orig_psd)
4402 mode_t ms_nfs_mode = 0;
4404 struct security_descriptor *psd = NULL;
4405 uint32_t orig_num_aces = 0;
4407 if (orig_psd->dacl != NULL) {
4408 orig_num_aces = orig_psd->dacl->num_aces;
4411 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4413 return NT_STATUS_NO_MEMORY;
4416 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4418 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4419 if (!NT_STATUS_IS_OK(status)) {
4420 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4426 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4427 * sent/present flags correctly now we've removed them.
4430 if (orig_num_aces != 0) {
4432 * Are there any ACE's left ?
4434 if (psd->dacl->num_aces == 0) {
4435 /* No - clear the DACL sent/present flags. */
4436 security_info_sent &= ~SECINFO_DACL;
4437 psd->type &= ~SEC_DESC_DACL_PRESENT;
4441 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4442 if (!NT_STATUS_IS_OK(status)) {
4443 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4449 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4451 DBG_WARNING("%s, result: %d, %04o error %s\n",
4454 (unsigned)ms_nfs_mode,
4456 status = map_nt_error_from_unix(errno);
4463 return NT_STATUS_OK;
4466 static struct vfs_offload_ctx *fruit_offload_ctx;
4468 struct fruit_offload_read_state {
4469 struct vfs_handle_struct *handle;
4470 struct tevent_context *ev;
4476 static void fruit_offload_read_done(struct tevent_req *subreq);
4478 static struct tevent_req *fruit_offload_read_send(
4479 TALLOC_CTX *mem_ctx,
4480 struct tevent_context *ev,
4481 struct vfs_handle_struct *handle,
4488 struct tevent_req *req = NULL;
4489 struct tevent_req *subreq = NULL;
4490 struct fruit_offload_read_state *state = NULL;
4492 req = tevent_req_create(mem_ctx, &state,
4493 struct fruit_offload_read_state);
4497 *state = (struct fruit_offload_read_state) {
4504 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4505 fsctl, ttl, offset, to_copy);
4506 if (tevent_req_nomem(subreq, req)) {
4507 return tevent_req_post(req, ev);
4509 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4513 static void fruit_offload_read_done(struct tevent_req *subreq)
4515 struct tevent_req *req = tevent_req_callback_data(
4516 subreq, struct tevent_req);
4517 struct fruit_offload_read_state *state = tevent_req_data(
4518 req, struct fruit_offload_read_state);
4521 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4525 TALLOC_FREE(subreq);
4526 if (tevent_req_nterror(req, status)) {
4530 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4531 tevent_req_done(req);
4535 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4536 &fruit_offload_ctx);
4537 if (tevent_req_nterror(req, status)) {
4541 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4544 if (tevent_req_nterror(req, status)) {
4548 tevent_req_done(req);
4552 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4553 struct vfs_handle_struct *handle,
4554 TALLOC_CTX *mem_ctx,
4557 struct fruit_offload_read_state *state = tevent_req_data(
4558 req, struct fruit_offload_read_state);
4561 if (tevent_req_is_nterror(req, &status)) {
4562 tevent_req_received(req);
4566 token->length = state->token.length;
4567 token->data = talloc_move(mem_ctx, &state->token.data);
4569 tevent_req_received(req);
4570 return NT_STATUS_OK;
4573 struct fruit_offload_write_state {
4574 struct vfs_handle_struct *handle;
4576 struct files_struct *src_fsp;
4577 struct files_struct *dst_fsp;
4581 static void fruit_offload_write_done(struct tevent_req *subreq);
4582 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4583 TALLOC_CTX *mem_ctx,
4584 struct tevent_context *ev,
4587 off_t transfer_offset,
4588 struct files_struct *dest_fsp,
4592 struct tevent_req *req, *subreq;
4593 struct fruit_offload_write_state *state;
4595 struct fruit_config_data *config;
4596 off_t src_off = transfer_offset;
4597 files_struct *src_fsp = NULL;
4598 off_t to_copy = num;
4599 bool copyfile_enabled = false;
4601 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4602 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4604 SMB_VFS_HANDLE_GET_DATA(handle, config,
4605 struct fruit_config_data,
4608 req = tevent_req_create(mem_ctx, &state,
4609 struct fruit_offload_write_state);
4613 state->handle = handle;
4614 state->dst_fsp = dest_fsp;
4617 case FSCTL_SRV_COPYCHUNK:
4618 case FSCTL_SRV_COPYCHUNK_WRITE:
4619 copyfile_enabled = config->copyfile_enabled;
4626 * Check if this a OS X copyfile style copychunk request with
4627 * a requested chunk count of 0 that was translated to a
4628 * offload_write_send VFS call overloading the parameters src_off
4629 * = dest_off = num = 0.
4631 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4632 status = vfs_offload_token_db_fetch_fsp(
4633 fruit_offload_ctx, token, &src_fsp);
4634 if (tevent_req_nterror(req, status)) {
4635 return tevent_req_post(req, ev);
4637 state->src_fsp = src_fsp;
4639 status = vfs_stat_fsp(src_fsp);
4640 if (tevent_req_nterror(req, status)) {
4641 return tevent_req_post(req, ev);
4644 to_copy = src_fsp->fsp_name->st.st_ex_size;
4645 state->is_copyfile = true;
4648 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4657 if (tevent_req_nomem(subreq, req)) {
4658 return tevent_req_post(req, ev);
4661 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4665 static void fruit_offload_write_done(struct tevent_req *subreq)
4667 struct tevent_req *req = tevent_req_callback_data(
4668 subreq, struct tevent_req);
4669 struct fruit_offload_write_state *state = tevent_req_data(
4670 req, struct fruit_offload_write_state);
4672 unsigned int num_streams = 0;
4673 struct stream_struct *streams = NULL;
4675 struct smb_filename *src_fname_tmp = NULL;
4676 struct smb_filename *dst_fname_tmp = NULL;
4678 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4681 TALLOC_FREE(subreq);
4682 if (tevent_req_nterror(req, status)) {
4686 if (!state->is_copyfile) {
4687 tevent_req_done(req);
4692 * Now copy all remaining streams. We know the share supports
4693 * streams, because we're in vfs_fruit. We don't do this async
4694 * because streams are few and small.
4696 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4697 state->src_fsp->fsp_name,
4698 req, &num_streams, &streams);
4699 if (tevent_req_nterror(req, status)) {
4703 if (num_streams == 1) {
4704 /* There is always one stream, ::$DATA. */
4705 tevent_req_done(req);
4709 for (i = 0; i < num_streams; i++) {
4710 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4711 __func__, streams[i].name, (size_t)streams[i].size));
4713 src_fname_tmp = synthetic_smb_fname(
4715 state->src_fsp->fsp_name->base_name,
4718 state->src_fsp->fsp_name->twrp,
4719 state->src_fsp->fsp_name->flags);
4720 if (tevent_req_nomem(src_fname_tmp, req)) {
4724 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4725 TALLOC_FREE(src_fname_tmp);
4729 dst_fname_tmp = synthetic_smb_fname(
4731 state->dst_fsp->fsp_name->base_name,
4734 state->dst_fsp->fsp_name->twrp,
4735 state->dst_fsp->fsp_name->flags);
4736 if (tevent_req_nomem(dst_fname_tmp, req)) {
4737 TALLOC_FREE(src_fname_tmp);
4741 status = copy_file(req,
4742 state->handle->conn,
4745 OPENX_FILE_CREATE_IF_NOT_EXIST,
4747 if (!NT_STATUS_IS_OK(status)) {
4748 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4749 smb_fname_str_dbg(src_fname_tmp),
4750 smb_fname_str_dbg(dst_fname_tmp),
4751 nt_errstr(status)));
4752 TALLOC_FREE(src_fname_tmp);
4753 TALLOC_FREE(dst_fname_tmp);
4754 tevent_req_nterror(req, status);
4758 TALLOC_FREE(src_fname_tmp);
4759 TALLOC_FREE(dst_fname_tmp);
4762 TALLOC_FREE(streams);
4763 TALLOC_FREE(src_fname_tmp);
4764 TALLOC_FREE(dst_fname_tmp);
4765 tevent_req_done(req);
4768 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4769 struct tevent_req *req,
4772 struct fruit_offload_write_state *state = tevent_req_data(
4773 req, struct fruit_offload_write_state);
4776 if (tevent_req_is_nterror(req, &status)) {
4777 DEBUG(1, ("server side copy chunk failed: %s\n",
4778 nt_errstr(status)));
4780 tevent_req_received(req);
4784 *copied = state->copied;
4785 tevent_req_received(req);
4787 return NT_STATUS_OK;
4790 static char *fruit_get_bandsize_line(char **lines, int numlines)
4793 static bool re_initialized = false;
4797 if (!re_initialized) {
4798 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4802 re_initialized = true;
4805 for (i = 0; i < numlines; i++) {
4806 regmatch_t matches[1];
4808 ret = regexec(&re, lines[i], 1, matches, 0);
4811 * Check if the match was on the last line, sa we want
4812 * the subsequent line.
4814 if (i + 1 == numlines) {
4817 return lines[i + 1];
4819 if (ret != REG_NOMATCH) {
4827 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4830 static bool re_initialized = false;
4831 regmatch_t matches[2];
4836 if (!re_initialized) {
4839 "<integer>\\([[:digit:]]*\\)</integer>$",
4844 re_initialized = true;
4847 ret = regexec(&re, line, 2, matches, 0);
4849 DBG_ERR("regex failed [%s]\n", line);
4853 line[matches[1].rm_eo] = '\0';
4855 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4859 *_band_size = (size_t)band_size;
4864 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4865 * "band-size" key and value.
4867 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4871 #define INFO_PLIST_MAX_SIZE 64*1024
4873 struct smb_filename *smb_fname = NULL;
4874 files_struct *fsp = NULL;
4875 uint8_t *file_data = NULL;
4876 char **lines = NULL;
4877 char *band_size_line = NULL;
4878 size_t plist_file_size;
4885 plist = talloc_asprintf(talloc_tos(),
4887 handle->conn->connectpath,
4889 if (plist == NULL) {
4894 smb_fname = synthetic_smb_fname(talloc_tos(),
4900 if (smb_fname == NULL) {
4905 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4907 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4912 plist_file_size = smb_fname->st.st_ex_size;
4914 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4915 DBG_INFO("%s is too large, ignoring\n", plist);
4920 status = SMB_VFS_NEXT_CREATE_FILE(
4923 smb_fname, /* fname */
4924 FILE_GENERIC_READ, /* access_mask */
4925 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4926 FILE_OPEN, /* create_disposition */
4927 0, /* create_options */
4928 0, /* file_attributes */
4929 INTERNAL_OPEN_ONLY, /* oplock_request */
4931 0, /* allocation_size */
4932 0, /* private_flags */
4937 NULL, NULL); /* create context */
4938 if (!NT_STATUS_IS_OK(status)) {
4939 DBG_INFO("Opening [%s] failed [%s]\n",
4940 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4945 file_data = talloc_zero_array(talloc_tos(),
4947 plist_file_size + 1);
4948 if (file_data == NULL) {
4953 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4954 if (nread != plist_file_size) {
4955 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4956 fsp_str_dbg(fsp), nread, plist_file_size);
4962 status = close_file(NULL, fsp, NORMAL_CLOSE);
4964 if (!NT_STATUS_IS_OK(status)) {
4965 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4970 lines = file_lines_parse((char *)file_data,
4974 if (lines == NULL) {
4979 band_size_line = fruit_get_bandsize_line(lines, numlines);
4980 if (band_size_line == NULL) {
4981 DBG_ERR("Didn't find band-size key in [%s]\n",
4982 smb_fname_str_dbg(smb_fname));
4987 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
4989 DBG_ERR("fruit_get_bandsize_from_line failed\n");
4993 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
4997 status = close_file(NULL, fsp, NORMAL_CLOSE);
4998 if (!NT_STATUS_IS_OK(status)) {
4999 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5004 TALLOC_FREE(smb_fname);
5005 TALLOC_FREE(file_data);
5010 struct fruit_disk_free_state {
5014 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5019 struct smb_filename *bands_dir = NULL;
5020 struct smb_Dir *dir_hnd = NULL;
5021 const char *dname = NULL;
5022 char *talloced = NULL;
5026 path = talloc_asprintf(talloc_tos(),
5028 handle->conn->connectpath,
5034 bands_dir = synthetic_smb_fname(talloc_tos(),
5041 if (bands_dir == NULL) {
5045 dir_hnd = OpenDir(talloc_tos(), handle->conn, bands_dir, NULL, 0);
5046 if (dir_hnd == NULL) {
5047 TALLOC_FREE(bands_dir);
5053 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5056 if (ISDOT(dname) || ISDOTDOT(dname)) {
5061 TALLOC_FREE(dir_hnd);
5063 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5065 TALLOC_FREE(bands_dir);
5071 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5072 struct fruit_disk_free_state *state,
5077 size_t sparsebundle_strlen = strlen("sparsebundle");
5078 size_t bandsize = 0;
5082 p = strstr(name, "sparsebundle");
5087 if (p[sparsebundle_strlen] != '\0') {
5091 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5093 ok = fruit_get_bandsize(handle, name, &bandsize);
5096 * Beware of race conditions: this may be an uninitialized
5097 * Info.plist that a client is just creating. We don't want let
5098 * this to trigger complete failure.
5100 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5104 ok = fruit_get_num_bands(handle, name, &nbands);
5107 * Beware of race conditions: this may be a backup sparsebundle
5108 * in an early stage lacking a bands subdirectory. We don't want
5109 * let this to trigger complete failure.
5111 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5116 * Arithmetic on 32-bit systems may cause overflow, depending on
5117 * size_t precision. First we check its unlikely, then we
5118 * force the precision into target off_t, then we check that
5119 * the total did not overflow either.
5121 if (bandsize > SIZE_MAX/nbands) {
5122 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5126 tm_size = (off_t)bandsize * (off_t)nbands;
5128 if (state->total_size + tm_size < state->total_size) {
5129 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5134 state->total_size += tm_size;
5136 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5137 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5143 * Calculate used size of a TimeMachine volume
5145 * This assumes that the volume is used only for TimeMachine.
5147 * - readdir(basedir of share), then
5148 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5149 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5150 * - count band files in "\1.sparsebundle/bands/"
5151 * - calculate used size of all bands: band_count * band_size
5153 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5154 const struct smb_filename *smb_fname,
5159 struct fruit_config_data *config = NULL;
5160 struct fruit_disk_free_state state = {0};
5161 struct smb_Dir *dir_hnd = NULL;
5162 const char *dname = NULL;
5163 char *talloced = NULL;
5169 SMB_VFS_HANDLE_GET_DATA(handle, config,
5170 struct fruit_config_data,
5173 if (!config->time_machine ||
5174 config->time_machine_max_size == 0)
5176 return SMB_VFS_NEXT_DISK_FREE(handle,
5183 dir_hnd = OpenDir(talloc_tos(), handle->conn, smb_fname, NULL, 0);
5184 if (dir_hnd == NULL) {
5188 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5191 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5193 TALLOC_FREE(talloced);
5194 TALLOC_FREE(dir_hnd);
5197 TALLOC_FREE(talloced);
5200 TALLOC_FREE(dir_hnd);
5202 dsize = config->time_machine_max_size / 512;
5203 dfree = dsize - (state.total_size / 512);
5204 if (dfree > dsize) {
5214 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5215 const SMB_STRUCT_STAT *psbuf)
5217 struct fruit_config_data *config = NULL;
5219 SMB_VFS_HANDLE_GET_DATA(handle, config,
5220 struct fruit_config_data,
5223 if (global_fruit_config.nego_aapl &&
5224 config->aapl_zero_file_id)
5229 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5232 static struct vfs_fn_pointers vfs_fruit_fns = {
5233 .connect_fn = fruit_connect,
5234 .disk_free_fn = fruit_disk_free,
5236 /* File operations */
5237 .fchmod_fn = fruit_fchmod,
5238 .unlinkat_fn = fruit_unlinkat,
5239 .renameat_fn = fruit_renameat,
5240 .openat_fn = fruit_openat,
5241 .close_fn = fruit_close,
5242 .pread_fn = fruit_pread,
5243 .pwrite_fn = fruit_pwrite,
5244 .pread_send_fn = fruit_pread_send,
5245 .pread_recv_fn = fruit_pread_recv,
5246 .pwrite_send_fn = fruit_pwrite_send,
5247 .pwrite_recv_fn = fruit_pwrite_recv,
5248 .stat_fn = fruit_stat,
5249 .lstat_fn = fruit_lstat,
5250 .fstat_fn = fruit_fstat,
5251 .streaminfo_fn = fruit_streaminfo,
5252 .fstreaminfo_fn = fruit_fstreaminfo,
5253 .fntimes_fn = fruit_fntimes,
5254 .ftruncate_fn = fruit_ftruncate,
5255 .fallocate_fn = fruit_fallocate,
5256 .create_file_fn = fruit_create_file,
5257 .readdir_attr_fn = fruit_readdir_attr,
5258 .offload_read_send_fn = fruit_offload_read_send,
5259 .offload_read_recv_fn = fruit_offload_read_recv,
5260 .offload_write_send_fn = fruit_offload_write_send,
5261 .offload_write_recv_fn = fruit_offload_write_recv,
5262 .fs_file_id_fn = fruit_fs_file_id,
5264 /* NT ACL operations */
5265 .fget_nt_acl_fn = fruit_fget_nt_acl,
5266 .fset_nt_acl_fn = fruit_fset_nt_acl,
5270 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5272 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5274 if (!NT_STATUS_IS_OK(ret)) {
5278 vfs_fruit_debug_level = debug_add_class("fruit");
5279 if (vfs_fruit_debug_level == -1) {
5280 vfs_fruit_debug_level = DBGC_VFS;
5281 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5284 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5285 "vfs_fruit_init","fruit",vfs_fruit_debug_level));