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;
907 uint8_t buf[AFP_INFO_SIZE];
909 stream_name = synthetic_smb_fname(talloc_tos(),
910 smb_fname->base_name,
915 if (stream_name == NULL) {
919 ret = SMB_VFS_STAT(handle->conn, stream_name);
924 status = openat_pathref_fsp(handle->conn->cwd_fsp, stream_name);
925 if (!NT_STATUS_IS_OK(status)) {
929 status = SMB_VFS_CREATE_FILE(
930 handle->conn, /* conn */
932 stream_name, /* fname */
933 FILE_READ_DATA, /* access_mask */
934 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
936 FILE_OPEN, /* create_disposition*/
937 0, /* create_options */
938 0, /* file_attributes */
939 INTERNAL_OPEN_ONLY, /* oplock_request */
941 0, /* allocation_size */
942 0, /* private_flags */
947 NULL, NULL); /* create context */
949 TALLOC_FREE(stream_name);
951 if (!NT_STATUS_IS_OK(status)) {
955 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
956 if (nread != AFP_INFO_SIZE) {
957 DBG_ERR("short read [%s] [%zd/%d]\n",
958 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
963 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
970 close_file(NULL, fsp, NORMAL_CLOSE);
976 static bool readdir_attr_meta_finderi_netatalk(
977 struct vfs_handle_struct *handle,
978 const struct smb_filename *smb_fname,
981 struct adouble *ad = NULL;
984 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
989 p = ad_get_entry(ad, ADEID_FINDERI);
991 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
996 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1001 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1002 const struct smb_filename *smb_fname,
1003 struct readdir_attr_data *attr_data)
1005 struct fruit_config_data *config = NULL;
1006 uint32_t date_added;
1010 SMB_VFS_HANDLE_GET_DATA(handle, config,
1011 struct fruit_config_data,
1014 switch (config->meta) {
1015 case FRUIT_META_NETATALK:
1016 ok = readdir_attr_meta_finderi_netatalk(
1017 handle, smb_fname, &ai);
1020 case FRUIT_META_STREAM:
1021 ok = readdir_attr_meta_finderi_stream(
1022 handle, smb_fname, &ai);
1026 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1031 /* Don't bother with errors, it's likely ENOENT */
1035 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1037 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1038 &ai.afpi_FinderInfo[0], 4);
1040 /* finder_creator */
1041 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1042 &ai.afpi_FinderInfo[4], 4);
1046 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1047 &ai.afpi_FinderInfo[8], 2);
1049 /* finder_ext_flags */
1050 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1051 &ai.afpi_FinderInfo[24], 2);
1054 date_added = convert_time_t_to_uint32_t(
1055 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1057 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1062 static uint64_t readdir_attr_rfork_size_adouble(
1063 struct vfs_handle_struct *handle,
1064 const struct smb_filename *smb_fname)
1066 struct adouble *ad = NULL;
1067 uint64_t rfork_size;
1069 ad = ad_get(talloc_tos(), handle, smb_fname,
1075 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1081 static uint64_t readdir_attr_rfork_size_stream(
1082 struct vfs_handle_struct *handle,
1083 const struct smb_filename *smb_fname)
1085 struct smb_filename *stream_name = NULL;
1087 uint64_t rfork_size;
1089 stream_name = synthetic_smb_fname(talloc_tos(),
1090 smb_fname->base_name,
1091 AFPRESOURCE_STREAM_NAME,
1095 if (stream_name == NULL) {
1099 ret = SMB_VFS_STAT(handle->conn, stream_name);
1101 TALLOC_FREE(stream_name);
1105 rfork_size = stream_name->st.st_ex_size;
1106 TALLOC_FREE(stream_name);
1111 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1112 const struct smb_filename *smb_fname)
1114 struct fruit_config_data *config = NULL;
1115 uint64_t rfork_size;
1117 SMB_VFS_HANDLE_GET_DATA(handle, config,
1118 struct fruit_config_data,
1121 switch (config->rsrc) {
1122 case FRUIT_RSRC_ADFILE:
1123 rfork_size = readdir_attr_rfork_size_adouble(handle,
1127 case FRUIT_RSRC_XATTR:
1128 case FRUIT_RSRC_STREAM:
1129 rfork_size = readdir_attr_rfork_size_stream(handle,
1134 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1142 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1143 const struct smb_filename *smb_fname,
1144 struct readdir_attr_data *attr_data)
1146 NTSTATUS status = NT_STATUS_OK;
1147 struct fruit_config_data *config = NULL;
1150 SMB_VFS_HANDLE_GET_DATA(handle, config,
1151 struct fruit_config_data,
1152 return NT_STATUS_UNSUCCESSFUL);
1155 /* Ensure we return a default value in the creation_date field */
1156 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1159 * Resource fork length
1162 if (config->readdir_attr_rsize) {
1163 uint64_t rfork_size;
1165 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1166 attr_data->attr_data.aapl.rfork_size = rfork_size;
1173 if (config->readdir_attr_finder_info) {
1174 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1176 status = NT_STATUS_INTERNAL_ERROR;
1183 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1188 if (psd->dacl == NULL) {
1189 return NT_STATUS_OK;
1192 for (i = 0; i < psd->dacl->num_aces; i++) {
1193 /* MS NFS style mode/uid/gid */
1194 int cmp = dom_sid_compare_domain(
1195 &global_sid_Unix_NFS,
1196 &psd->dacl->aces[i].trustee);
1198 /* Normal ACE entry. */
1203 * security_descriptor_dacl_del()
1204 * *must* return NT_STATUS_OK as we know
1205 * we have something to remove.
1208 status = security_descriptor_dacl_del(psd,
1209 &psd->dacl->aces[i].trustee);
1210 if (!NT_STATUS_IS_OK(status)) {
1211 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1217 * security_descriptor_dacl_del() may delete more
1218 * then one entry subsequent to this one if the
1219 * SID matches, but we only need to ensure that
1220 * we stay looking at the same element in the array.
1224 return NT_STATUS_OK;
1227 /* Search MS NFS style ACE with UNIX mode */
1228 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1230 struct security_descriptor *psd,
1235 struct fruit_config_data *config = NULL;
1239 SMB_VFS_HANDLE_GET_DATA(handle, config,
1240 struct fruit_config_data,
1241 return NT_STATUS_UNSUCCESSFUL);
1243 if (!global_fruit_config.nego_aapl) {
1244 return NT_STATUS_OK;
1246 if (psd->dacl == NULL || !config->unix_info_enabled) {
1247 return NT_STATUS_OK;
1250 for (i = 0; i < psd->dacl->num_aces; i++) {
1251 if (dom_sid_compare_domain(
1252 &global_sid_Unix_NFS_Mode,
1253 &psd->dacl->aces[i].trustee) == 0) {
1254 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1255 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1258 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1259 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1265 * Remove any incoming virtual ACE entries generated by
1266 * fruit_fget_nt_acl().
1269 return remove_virtual_nfs_aces(psd);
1272 /****************************************************************************
1274 ****************************************************************************/
1276 static int fruit_connect(vfs_handle_struct *handle,
1277 const char *service,
1281 char *list = NULL, *newlist = NULL;
1282 struct fruit_config_data *config;
1283 const struct loadparm_substitution *lp_sub =
1284 loadparm_s3_global_substitution();
1286 DEBUG(10, ("fruit_connect\n"));
1288 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1293 rc = init_fruit_config(handle);
1298 SMB_VFS_HANDLE_GET_DATA(handle, config,
1299 struct fruit_config_data, return -1);
1301 if (config->veto_appledouble) {
1302 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1305 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1306 newlist = talloc_asprintf(
1308 "%s/" ADOUBLE_NAME_PREFIX "*/",
1310 lp_do_parameter(SNUM(handle->conn),
1315 lp_do_parameter(SNUM(handle->conn),
1317 "/" ADOUBLE_NAME_PREFIX "*/");
1323 if (config->encoding == FRUIT_ENC_NATIVE) {
1324 lp_do_parameter(SNUM(handle->conn),
1326 macos_string_replace_map);
1329 if (config->time_machine) {
1330 DBG_NOTICE("Enabling durable handles for Time Machine "
1331 "support on [%s]\n", service);
1332 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1333 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1334 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1335 if (!lp_strict_sync(SNUM(handle->conn))) {
1336 DBG_WARNING("Time Machine without strict sync is not "
1339 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1345 static void fio_ref_destroy_fn(void *p_data)
1347 struct fio *ref_fio = (struct fio *)p_data;
1348 if (ref_fio->real_fio != NULL) {
1349 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1350 ref_fio->real_fio->ad_fsp = NULL;
1351 ref_fio->real_fio = NULL;
1355 static void fio_close_ad_fsp(struct fio *fio)
1357 if (fio->ad_fsp != NULL) {
1358 fd_close(fio->ad_fsp);
1359 file_free(NULL, fio->ad_fsp);
1360 /* fio_ref_destroy_fn() should have cleared this */
1361 SMB_ASSERT(fio->ad_fsp == NULL);
1365 static void fio_destroy_fn(void *p_data)
1367 struct fio *fio = (struct fio *)p_data;
1368 fio_close_ad_fsp(fio);
1371 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1372 const struct files_struct *dirfsp,
1373 const struct smb_filename *smb_fname,
1378 struct fruit_config_data *config = NULL;
1379 struct fio *fio = NULL;
1380 int open_flags = flags & ~O_CREAT;
1383 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1385 SMB_VFS_HANDLE_GET_DATA(handle, config,
1386 struct fruit_config_data, return -1);
1388 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1389 fio->handle = handle;
1391 fio->type = ADOUBLE_META;
1392 fio->config = config;
1394 fd = SMB_VFS_NEXT_OPENAT(handle,
1404 if (!(flags & O_CREAT)) {
1405 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1411 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1415 fio->fake_fd = true;
1422 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1423 const struct files_struct *dirfsp,
1424 const struct smb_filename *smb_fname,
1429 struct fruit_config_data *config = NULL;
1430 struct fio *fio = NULL;
1431 struct adouble *ad = NULL;
1432 bool meta_exists = false;
1435 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1437 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1444 if (!meta_exists && !(flags & O_CREAT)) {
1454 SMB_VFS_HANDLE_GET_DATA(handle, config,
1455 struct fruit_config_data, return -1);
1457 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1458 fio->handle = handle;
1460 fio->type = ADOUBLE_META;
1461 fio->config = config;
1462 fio->fake_fd = true;
1469 static int fruit_open_meta(vfs_handle_struct *handle,
1470 const struct files_struct *dirfsp,
1471 const struct smb_filename *smb_fname,
1472 files_struct *fsp, int flags, mode_t mode)
1475 struct fruit_config_data *config = NULL;
1477 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1479 SMB_VFS_HANDLE_GET_DATA(handle, config,
1480 struct fruit_config_data, return -1);
1482 switch (config->meta) {
1483 case FRUIT_META_STREAM:
1484 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1488 case FRUIT_META_NETATALK:
1489 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1494 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1498 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1503 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1504 const struct files_struct *dirfsp,
1505 const struct smb_filename *smb_fname,
1511 struct fruit_config_data *config = NULL;
1512 struct files_struct *ad_fsp = NULL;
1513 struct fio *fio = NULL;
1514 struct fio *ref_fio = NULL;
1518 SMB_VFS_HANDLE_GET_DATA(handle, config,
1519 struct fruit_config_data, return -1);
1521 if ((!(flags & O_CREAT)) &&
1522 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1524 /* sorry, but directories don't habe a resource fork */
1531 * We return a fake_fd to the vfs modules above,
1532 * while we open an internal backend fsp for the
1533 * '._' file for the next vfs modules.
1535 * Note that adouble_open_from_base_fsp() recurses
1536 * into fruit_openat(), but it'll just pass to
1537 * the next module as just opens a flat file on
1547 status = adouble_open_from_base_fsp(dirfsp,
1553 if (!NT_STATUS_IS_OK(status)) {
1554 errno = map_errno_from_nt_status(status);
1560 * Now we need to glue both handles together,
1561 * so that they automatically detach each other
1564 fio = fruit_get_complete_fio(handle, fsp);
1566 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1568 fio_ref_destroy_fn);
1569 if (ref_fio == NULL) {
1570 int saved_errno = errno;
1572 file_free(NULL, ad_fsp);
1574 errno = saved_errno;
1579 SMB_ASSERT(ref_fio->fsp == NULL);
1580 ref_fio->handle = handle;
1581 ref_fio->fsp = ad_fsp;
1582 ref_fio->type = ADOUBLE_RSRC;
1583 ref_fio->config = config;
1584 ref_fio->real_fio = fio;
1585 SMB_ASSERT(fio->ad_fsp == NULL);
1586 fio->ad_fsp = ad_fsp;
1587 fio->fake_fd = true;
1591 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1593 int saved_errno = errno;
1595 vfs_fake_fd_close(fd);
1597 errno = saved_errno;
1603 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1604 const struct files_struct *dirfsp,
1605 const struct smb_filename *smb_fname,
1610 #ifdef HAVE_ATTROPEN
1614 * As there's no attropenat() this is only going to work with AT_FDCWD.
1616 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1618 fd = attropen(smb_fname->base_name,
1619 AFPRESOURCE_EA_NETATALK,
1634 static int fruit_open_rsrc(vfs_handle_struct *handle,
1635 const struct files_struct *dirfsp,
1636 const struct smb_filename *smb_fname,
1637 files_struct *fsp, int flags, mode_t mode)
1640 struct fruit_config_data *config = NULL;
1641 struct fio *fio = NULL;
1643 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1645 SMB_VFS_HANDLE_GET_DATA(handle, config,
1646 struct fruit_config_data, return -1);
1648 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1649 fio->handle = handle;
1651 fio->type = ADOUBLE_RSRC;
1652 fio->config = config;
1654 switch (config->rsrc) {
1655 case FRUIT_RSRC_STREAM:
1656 fd = SMB_VFS_NEXT_OPENAT(handle,
1664 case FRUIT_RSRC_ADFILE:
1665 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1669 case FRUIT_RSRC_XATTR:
1670 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1675 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1679 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1688 static int fruit_openat(vfs_handle_struct *handle,
1689 const struct files_struct *dirfsp,
1690 const struct smb_filename *smb_fname,
1697 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1699 if (!is_named_stream(smb_fname)) {
1700 return SMB_VFS_NEXT_OPENAT(handle,
1708 if (is_afpinfo_stream(smb_fname->stream_name)) {
1709 fd = fruit_open_meta(handle,
1715 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1716 fd = fruit_open_rsrc(handle,
1723 fd = SMB_VFS_NEXT_OPENAT(handle,
1731 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1733 /* Prevent reopen optimisation */
1734 fsp->fsp_flags.have_proc_fds = false;
1738 static int fruit_close_meta(vfs_handle_struct *handle,
1741 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1743 struct fruit_config_data *config = NULL;
1745 SMB_VFS_HANDLE_GET_DATA(handle, config,
1746 struct fruit_config_data, return -1);
1752 switch (config->meta) {
1753 case FRUIT_META_STREAM:
1755 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1756 fsp_set_fd(fsp, -1);
1758 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1762 case FRUIT_META_NETATALK:
1763 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1764 fsp_set_fd(fsp, -1);
1768 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1776 static int fruit_close_rsrc(vfs_handle_struct *handle,
1779 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1781 struct fruit_config_data *config = NULL;
1783 SMB_VFS_HANDLE_GET_DATA(handle, config,
1784 struct fruit_config_data, return -1);
1786 switch (config->rsrc) {
1787 case FRUIT_RSRC_STREAM:
1788 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1791 case FRUIT_RSRC_ADFILE:
1792 fio_close_ad_fsp(fio);
1793 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1794 fsp_set_fd(fsp, -1);
1797 case FRUIT_RSRC_XATTR:
1798 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1799 fsp_set_fd(fsp, -1);
1803 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1810 static int fruit_close(vfs_handle_struct *handle,
1816 fd = fsp_get_pathref_fd(fsp);
1818 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1820 if (!is_named_stream(fsp->fsp_name)) {
1821 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1824 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1825 ret = fruit_close_meta(handle, fsp);
1826 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1827 ret = fruit_close_rsrc(handle, fsp);
1829 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1835 static int fruit_renameat(struct vfs_handle_struct *handle,
1836 files_struct *srcfsp,
1837 const struct smb_filename *smb_fname_src,
1838 files_struct *dstfsp,
1839 const struct smb_filename *smb_fname_dst)
1842 struct fruit_config_data *config = NULL;
1843 struct smb_filename *src_adp_smb_fname = NULL;
1844 struct smb_filename *dst_adp_smb_fname = NULL;
1846 SMB_VFS_HANDLE_GET_DATA(handle, config,
1847 struct fruit_config_data, return -1);
1849 if (!VALID_STAT(smb_fname_src->st)) {
1850 DBG_ERR("Need valid stat for [%s]\n",
1851 smb_fname_str_dbg(smb_fname_src));
1855 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1864 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1865 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1870 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1875 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1880 DBG_DEBUG("%s -> %s\n",
1881 smb_fname_str_dbg(src_adp_smb_fname),
1882 smb_fname_str_dbg(dst_adp_smb_fname));
1884 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1889 if (errno == ENOENT) {
1894 TALLOC_FREE(src_adp_smb_fname);
1895 TALLOC_FREE(dst_adp_smb_fname);
1899 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1900 struct files_struct *dirfsp,
1901 const struct smb_filename *smb_fname)
1903 return SMB_VFS_NEXT_UNLINKAT(handle,
1909 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1910 const struct smb_filename *smb_fname)
1912 return SMB_VFS_REMOVEXATTR(handle->conn,
1914 AFPINFO_EA_NETATALK);
1917 static int fruit_unlink_meta(vfs_handle_struct *handle,
1918 struct files_struct *dirfsp,
1919 const struct smb_filename *smb_fname)
1921 struct fruit_config_data *config = NULL;
1924 SMB_VFS_HANDLE_GET_DATA(handle, config,
1925 struct fruit_config_data, return -1);
1927 switch (config->meta) {
1928 case FRUIT_META_STREAM:
1929 rc = fruit_unlink_meta_stream(handle,
1934 case FRUIT_META_NETATALK:
1935 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1939 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
1946 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
1947 struct files_struct *dirfsp,
1948 const struct smb_filename *smb_fname,
1953 if (!force_unlink) {
1954 struct smb_filename *smb_fname_cp = NULL;
1957 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
1958 if (smb_fname_cp == NULL) {
1963 * 0 byte resource fork streams are not listed by
1964 * vfs_streaminfo, as a result stream cleanup/deletion of file
1965 * deletion doesn't remove the resourcefork stream.
1968 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
1970 TALLOC_FREE(smb_fname_cp);
1971 DBG_ERR("stat [%s] failed [%s]\n",
1972 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
1976 size = smb_fname_cp->st.st_ex_size;
1977 TALLOC_FREE(smb_fname_cp);
1980 /* OS X ignores resource fork stream delete requests */
1985 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1989 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
1996 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
1997 struct files_struct *dirfsp,
1998 const struct smb_filename *smb_fname,
2002 struct adouble *ad = NULL;
2003 struct smb_filename *adp_smb_fname = NULL;
2005 if (!force_unlink) {
2006 ad = ad_get(talloc_tos(), handle, smb_fname,
2015 * 0 byte resource fork streams are not listed by
2016 * vfs_streaminfo, as a result stream cleanup/deletion of file
2017 * deletion doesn't remove the resourcefork stream.
2020 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2021 /* OS X ignores resource fork stream delete requests */
2029 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2034 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2038 TALLOC_FREE(adp_smb_fname);
2039 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2046 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2047 const struct smb_filename *smb_fname,
2051 * OS X ignores resource fork stream delete requests, so nothing to do
2052 * here. Removing the file will remove the xattr anyway, so we don't
2053 * have to take care of removing 0 byte resource forks that could be
2059 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2060 struct files_struct *dirfsp,
2061 const struct smb_filename *smb_fname,
2064 struct fruit_config_data *config = NULL;
2067 SMB_VFS_HANDLE_GET_DATA(handle, config,
2068 struct fruit_config_data, return -1);
2070 switch (config->rsrc) {
2071 case FRUIT_RSRC_STREAM:
2072 rc = fruit_unlink_rsrc_stream(handle,
2078 case FRUIT_RSRC_ADFILE:
2079 rc = fruit_unlink_rsrc_adouble(handle,
2085 case FRUIT_RSRC_XATTR:
2086 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2090 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2097 static int fruit_chmod(vfs_handle_struct *handle,
2098 const struct smb_filename *smb_fname,
2102 struct fruit_config_data *config = NULL;
2103 struct smb_filename *smb_fname_adp = NULL;
2105 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
2110 SMB_VFS_HANDLE_GET_DATA(handle, config,
2111 struct fruit_config_data, return -1);
2113 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2117 if (!VALID_STAT(smb_fname->st)) {
2121 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2125 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2130 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
2132 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
2133 if (errno == ENOENT) {
2137 TALLOC_FREE(smb_fname_adp);
2141 static int fruit_unlinkat(vfs_handle_struct *handle,
2142 struct files_struct *dirfsp,
2143 const struct smb_filename *smb_fname,
2146 struct fruit_config_data *config = NULL;
2147 struct smb_filename *rsrc_smb_fname = NULL;
2150 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
2152 if (flags & AT_REMOVEDIR) {
2153 return SMB_VFS_NEXT_UNLINKAT(handle,
2159 SMB_VFS_HANDLE_GET_DATA(handle, config,
2160 struct fruit_config_data, return -1);
2162 if (is_afpinfo_stream(smb_fname->stream_name)) {
2163 return fruit_unlink_meta(handle,
2166 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2167 return fruit_unlink_rsrc(handle,
2171 } else if (is_named_stream(smb_fname)) {
2172 return SMB_VFS_NEXT_UNLINKAT(handle,
2176 } else if (is_adouble_file(smb_fname->base_name)) {
2177 return SMB_VFS_NEXT_UNLINKAT(handle,
2184 * A request to delete the base file. Because 0 byte resource
2185 * fork streams are not listed by fruit_streaminfo,
2186 * delete_all_streams() can't remove 0 byte resource fork
2187 * streams, so we have to cleanup this here.
2189 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2190 smb_fname->base_name,
2191 AFPRESOURCE_STREAM_NAME,
2195 if (rsrc_smb_fname == NULL) {
2199 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2200 if ((ret != 0) && (errno != ENOENT)) {
2201 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2202 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2203 TALLOC_FREE(rsrc_smb_fname);
2206 TALLOC_FREE(rsrc_smb_fname);
2208 return SMB_VFS_NEXT_UNLINKAT(handle,
2214 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2215 files_struct *fsp, void *data,
2216 size_t n, off_t offset)
2218 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2222 if ((fio == NULL) || fio->fake_fd) {
2226 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2227 if (nread == -1 || nread == n) {
2231 DBG_ERR("Removing [%s] after short read [%zd]\n",
2232 fsp_str_dbg(fsp), nread);
2234 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2239 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2247 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2248 files_struct *fsp, void *data,
2249 size_t n, off_t offset)
2252 struct adouble *ad = NULL;
2253 char afpinfo_buf[AFP_INFO_SIZE];
2257 ai = afpinfo_new(talloc_tos());
2262 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2268 p = ad_get_entry(ad, ADEID_FINDERI);
2270 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2275 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2277 nread = afpinfo_pack(ai, afpinfo_buf);
2278 if (nread != AFP_INFO_SIZE) {
2283 memcpy(data, afpinfo_buf, n);
2291 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2292 files_struct *fsp, void *data,
2293 size_t n, off_t offset)
2295 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2300 * OS X has a off-by-1 error in the offset calculation, so we're
2301 * bug compatible here. It won't hurt, as any relevant real
2302 * world read requests from the AFP_AfpInfo stream will be
2303 * offset=0 n=60. offset is ignored anyway, see below.
2305 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2310 DBG_ERR("Failed to fetch fsp extension");
2314 /* Yes, macOS always reads from offset 0 */
2316 to_return = MIN(n, AFP_INFO_SIZE);
2318 switch (fio->config->meta) {
2319 case FRUIT_META_STREAM:
2320 nread = fruit_pread_meta_stream(handle, fsp, data,
2324 case FRUIT_META_NETATALK:
2325 nread = fruit_pread_meta_adouble(handle, fsp, data,
2330 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2334 if (nread == -1 && fio->fake_fd) {
2336 char afpinfo_buf[AFP_INFO_SIZE];
2338 ai = afpinfo_new(talloc_tos());
2343 nread = afpinfo_pack(ai, afpinfo_buf);
2345 if (nread != AFP_INFO_SIZE) {
2349 memcpy(data, afpinfo_buf, to_return);
2356 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2357 files_struct *fsp, void *data,
2358 size_t n, off_t offset)
2360 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2363 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2364 files_struct *fsp, void *data,
2365 size_t n, off_t offset)
2367 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2370 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2371 files_struct *fsp, void *data,
2372 size_t n, off_t offset)
2374 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2375 struct adouble *ad = NULL;
2378 if (fio->ad_fsp == NULL) {
2379 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2384 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2386 DBG_ERR("ad_fget [%s] failed [%s]\n",
2387 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2391 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2392 offset + ad_getentryoff(ad, ADEID_RFORK));
2398 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2399 files_struct *fsp, void *data,
2400 size_t n, off_t offset)
2402 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2410 switch (fio->config->rsrc) {
2411 case FRUIT_RSRC_STREAM:
2412 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2415 case FRUIT_RSRC_ADFILE:
2416 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2419 case FRUIT_RSRC_XATTR:
2420 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2424 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2431 static ssize_t fruit_pread(vfs_handle_struct *handle,
2432 files_struct *fsp, void *data,
2433 size_t n, off_t offset)
2435 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2438 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2439 fsp_str_dbg(fsp), (intmax_t)offset, n);
2442 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2445 if (fio->type == ADOUBLE_META) {
2446 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2448 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2451 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2455 static bool fruit_must_handle_aio_stream(struct fio *fio)
2461 if (fio->type == ADOUBLE_META) {
2465 if ((fio->type == ADOUBLE_RSRC) &&
2466 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2474 struct fruit_pread_state {
2476 struct vfs_aio_state vfs_aio_state;
2479 static void fruit_pread_done(struct tevent_req *subreq);
2481 static struct tevent_req *fruit_pread_send(
2482 struct vfs_handle_struct *handle,
2483 TALLOC_CTX *mem_ctx,
2484 struct tevent_context *ev,
2485 struct files_struct *fsp,
2487 size_t n, off_t offset)
2489 struct tevent_req *req = NULL;
2490 struct tevent_req *subreq = NULL;
2491 struct fruit_pread_state *state = NULL;
2492 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2494 req = tevent_req_create(mem_ctx, &state,
2495 struct fruit_pread_state);
2500 if (fruit_must_handle_aio_stream(fio)) {
2501 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2502 if (state->nread != n) {
2503 if (state->nread != -1) {
2506 tevent_req_error(req, errno);
2507 return tevent_req_post(req, ev);
2509 tevent_req_done(req);
2510 return tevent_req_post(req, ev);
2513 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2515 if (tevent_req_nomem(req, subreq)) {
2516 return tevent_req_post(req, ev);
2518 tevent_req_set_callback(subreq, fruit_pread_done, req);
2522 static void fruit_pread_done(struct tevent_req *subreq)
2524 struct tevent_req *req = tevent_req_callback_data(
2525 subreq, struct tevent_req);
2526 struct fruit_pread_state *state = tevent_req_data(
2527 req, struct fruit_pread_state);
2529 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2530 TALLOC_FREE(subreq);
2532 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2535 tevent_req_done(req);
2538 static ssize_t fruit_pread_recv(struct tevent_req *req,
2539 struct vfs_aio_state *vfs_aio_state)
2541 struct fruit_pread_state *state = tevent_req_data(
2542 req, struct fruit_pread_state);
2544 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2548 *vfs_aio_state = state->vfs_aio_state;
2549 return state->nread;
2552 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2553 files_struct *fsp, const void *data,
2554 size_t n, off_t offset)
2556 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2562 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2563 fsp_str_dbg(fsp), (intmax_t)offset, n);
2570 int fd = fsp_get_pathref_fd(fsp);
2572 ret = vfs_fake_fd_close(fd);
2573 fsp_set_fd(fsp, -1);
2575 DBG_ERR("Close [%s] failed: %s\n",
2576 fsp_str_dbg(fsp), strerror(errno));
2580 fd = SMB_VFS_NEXT_OPENAT(handle,
2587 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2588 fsp_str_dbg(fsp), strerror(errno));
2591 fsp_set_fd(fsp, fd);
2592 fio->fake_fd = false;
2595 ai = afpinfo_unpack(talloc_tos(), data);
2600 if (ai_empty_finderinfo(ai)) {
2602 * Writing an all 0 blob to the metadata stream results in the
2603 * stream being removed on a macOS server. This ensures we
2604 * behave the same and it verified by the "delete AFP_AfpInfo by
2605 * writing all 0" test.
2607 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2609 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2614 ok = set_delete_on_close(
2617 handle->conn->session_info->security_token,
2618 handle->conn->session_info->unix_token);
2620 DBG_ERR("set_delete_on_close on [%s] failed\n",
2627 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2628 if (nwritten != n) {
2635 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2636 files_struct *fsp, const void *data,
2637 size_t n, off_t offset)
2639 struct adouble *ad = NULL;
2645 ai = afpinfo_unpack(talloc_tos(), data);
2650 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2652 ad = ad_init(talloc_tos(), ADOUBLE_META);
2657 p = ad_get_entry(ad, ADEID_FINDERI);
2659 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2664 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2666 ret = ad_fset(handle, ad, fsp);
2668 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2675 if (!ai_empty_finderinfo(ai)) {
2680 * Writing an all 0 blob to the metadata stream results in the stream
2681 * being removed on a macOS server. This ensures we behave the same and
2682 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2685 ok = set_delete_on_close(
2688 handle->conn->session_info->security_token,
2689 handle->conn->session_info->unix_token);
2691 DBG_ERR("set_delete_on_close on [%s] failed\n",
2699 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2700 files_struct *fsp, const void *data,
2701 size_t n, off_t offset)
2703 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2705 uint8_t buf[AFP_INFO_SIZE];
2711 DBG_ERR("Failed to fetch fsp extension");
2720 if (offset != 0 && n < 60) {
2725 cmp = memcmp(data, "AFP", 3);
2731 if (n <= AFP_OFF_FinderInfo) {
2733 * Nothing to do here really, just return
2741 if (to_copy > AFP_INFO_SIZE) {
2742 to_copy = AFP_INFO_SIZE;
2744 memcpy(buf, data, to_copy);
2747 if (to_write != AFP_INFO_SIZE) {
2748 to_write = AFP_INFO_SIZE;
2751 switch (fio->config->meta) {
2752 case FRUIT_META_STREAM:
2753 nwritten = fruit_pwrite_meta_stream(handle,
2760 case FRUIT_META_NETATALK:
2761 nwritten = fruit_pwrite_meta_netatalk(handle,
2769 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2773 if (nwritten != to_write) {
2778 * Return the requested amount, verified against macOS SMB server
2783 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2784 files_struct *fsp, const void *data,
2785 size_t n, off_t offset)
2787 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2790 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2791 files_struct *fsp, const void *data,
2792 size_t n, off_t offset)
2794 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2797 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2798 files_struct *fsp, const void *data,
2799 size_t n, off_t offset)
2801 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2802 struct adouble *ad = NULL;
2806 if (fio->ad_fsp == NULL) {
2807 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2812 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2814 DBG_ERR("ad_fget [%s] failed [%s]\n",
2815 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2819 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2820 offset + ad_getentryoff(ad, ADEID_RFORK));
2821 if (nwritten != n) {
2822 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2823 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2828 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2829 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2830 ret = ad_fset(handle, ad, fio->ad_fsp);
2832 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2842 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2843 files_struct *fsp, const void *data,
2844 size_t n, off_t offset)
2846 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2850 DBG_ERR("Failed to fetch fsp extension");
2854 switch (fio->config->rsrc) {
2855 case FRUIT_RSRC_STREAM:
2856 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2859 case FRUIT_RSRC_ADFILE:
2860 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2863 case FRUIT_RSRC_XATTR:
2864 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2868 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2875 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2876 files_struct *fsp, const void *data,
2877 size_t n, off_t offset)
2879 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2882 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2883 fsp_str_dbg(fsp), (intmax_t)offset, n);
2886 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2889 if (fio->type == ADOUBLE_META) {
2890 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2892 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2895 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2899 struct fruit_pwrite_state {
2901 struct vfs_aio_state vfs_aio_state;
2904 static void fruit_pwrite_done(struct tevent_req *subreq);
2906 static struct tevent_req *fruit_pwrite_send(
2907 struct vfs_handle_struct *handle,
2908 TALLOC_CTX *mem_ctx,
2909 struct tevent_context *ev,
2910 struct files_struct *fsp,
2912 size_t n, off_t offset)
2914 struct tevent_req *req = NULL;
2915 struct tevent_req *subreq = NULL;
2916 struct fruit_pwrite_state *state = NULL;
2917 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2919 req = tevent_req_create(mem_ctx, &state,
2920 struct fruit_pwrite_state);
2925 if (fruit_must_handle_aio_stream(fio)) {
2926 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
2927 if (state->nwritten != n) {
2928 if (state->nwritten != -1) {
2931 tevent_req_error(req, errno);
2932 return tevent_req_post(req, ev);
2934 tevent_req_done(req);
2935 return tevent_req_post(req, ev);
2938 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
2940 if (tevent_req_nomem(req, subreq)) {
2941 return tevent_req_post(req, ev);
2943 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
2947 static void fruit_pwrite_done(struct tevent_req *subreq)
2949 struct tevent_req *req = tevent_req_callback_data(
2950 subreq, struct tevent_req);
2951 struct fruit_pwrite_state *state = tevent_req_data(
2952 req, struct fruit_pwrite_state);
2954 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2955 TALLOC_FREE(subreq);
2957 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2960 tevent_req_done(req);
2963 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
2964 struct vfs_aio_state *vfs_aio_state)
2966 struct fruit_pwrite_state *state = tevent_req_data(
2967 req, struct fruit_pwrite_state);
2969 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2973 *vfs_aio_state = state->vfs_aio_state;
2974 return state->nwritten;
2978 * Helper to stat/lstat the base file of an smb_fname.
2980 static int fruit_stat_base(vfs_handle_struct *handle,
2981 struct smb_filename *smb_fname,
2984 char *tmp_stream_name;
2987 tmp_stream_name = smb_fname->stream_name;
2988 smb_fname->stream_name = NULL;
2990 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2992 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2994 smb_fname->stream_name = tmp_stream_name;
2996 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
2997 smb_fname->base_name,
2998 (uintmax_t)smb_fname->st.st_ex_dev,
2999 (uintmax_t)smb_fname->st.st_ex_ino);
3003 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3004 struct smb_filename *smb_fname,
3010 ret = fruit_stat_base(handle, smb_fname, false);
3015 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3018 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3020 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3023 smb_fname->st.st_ex_ino = ino;
3028 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3029 struct smb_filename *smb_fname,
3032 struct adouble *ad = NULL;
3034 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3036 DBG_INFO("fruit_stat_meta %s: %s\n",
3037 smb_fname_str_dbg(smb_fname), strerror(errno));
3043 /* Populate the stat struct with info from the base file. */
3044 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3047 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3048 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3049 smb_fname->stream_name);
3053 static int fruit_stat_meta(vfs_handle_struct *handle,
3054 struct smb_filename *smb_fname,
3057 struct fruit_config_data *config = NULL;
3060 SMB_VFS_HANDLE_GET_DATA(handle, config,
3061 struct fruit_config_data, return -1);
3063 switch (config->meta) {
3064 case FRUIT_META_STREAM:
3065 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3068 case FRUIT_META_NETATALK:
3069 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3073 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3080 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3081 struct smb_filename *smb_fname,
3084 struct adouble *ad = NULL;
3087 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3093 /* Populate the stat struct with info from the base file. */
3094 ret = fruit_stat_base(handle, smb_fname, follow_links);
3100 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3101 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3102 smb_fname->stream_name);
3107 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3108 struct smb_filename *smb_fname,
3114 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3116 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3122 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3123 struct smb_filename *smb_fname,
3126 #ifdef HAVE_ATTROPEN
3130 /* Populate the stat struct with info from the base file. */
3131 ret = fruit_stat_base(handle, smb_fname, follow_links);
3136 fd = attropen(smb_fname->base_name,
3137 AFPRESOURCE_EA_NETATALK,
3143 ret = sys_fstat(fd, &smb_fname->st, false);
3146 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3147 AFPRESOURCE_EA_NETATALK);
3153 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3154 smb_fname->stream_name);
3164 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3165 struct smb_filename *smb_fname,
3168 struct fruit_config_data *config = NULL;
3171 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3173 SMB_VFS_HANDLE_GET_DATA(handle, config,
3174 struct fruit_config_data, return -1);
3176 switch (config->rsrc) {
3177 case FRUIT_RSRC_STREAM:
3178 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3181 case FRUIT_RSRC_XATTR:
3182 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3185 case FRUIT_RSRC_ADFILE:
3186 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3190 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3197 static int fruit_stat(vfs_handle_struct *handle,
3198 struct smb_filename *smb_fname)
3202 DEBUG(10, ("fruit_stat called for %s\n",
3203 smb_fname_str_dbg(smb_fname)));
3205 if (!is_named_stream(smb_fname)) {
3206 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3208 update_btime(handle, smb_fname);
3214 * Note if lp_posix_paths() is true, we can never
3215 * get here as is_ntfs_stream_smb_fname() is
3216 * always false. So we never need worry about
3217 * not following links here.
3220 if (is_afpinfo_stream(smb_fname->stream_name)) {
3221 rc = fruit_stat_meta(handle, smb_fname, true);
3222 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3223 rc = fruit_stat_rsrc(handle, smb_fname, true);
3225 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3229 update_btime(handle, smb_fname);
3230 smb_fname->st.st_ex_mode &= ~S_IFMT;
3231 smb_fname->st.st_ex_mode |= S_IFREG;
3232 smb_fname->st.st_ex_blocks =
3233 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3238 static int fruit_lstat(vfs_handle_struct *handle,
3239 struct smb_filename *smb_fname)
3243 DEBUG(10, ("fruit_lstat called for %s\n",
3244 smb_fname_str_dbg(smb_fname)));
3246 if (!is_named_stream(smb_fname)) {
3247 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3249 update_btime(handle, smb_fname);
3254 if (is_afpinfo_stream(smb_fname->stream_name)) {
3255 rc = fruit_stat_meta(handle, smb_fname, false);
3256 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3257 rc = fruit_stat_rsrc(handle, smb_fname, false);
3259 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3263 update_btime(handle, smb_fname);
3264 smb_fname->st.st_ex_mode &= ~S_IFMT;
3265 smb_fname->st.st_ex_mode |= S_IFREG;
3266 smb_fname->st.st_ex_blocks =
3267 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3272 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3274 SMB_STRUCT_STAT *sbuf)
3276 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3277 struct smb_filename smb_fname;
3286 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3291 *sbuf = fsp->base_fsp->fsp_name->st;
3292 sbuf->st_ex_size = AFP_INFO_SIZE;
3293 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3297 smb_fname = (struct smb_filename) {
3298 .base_name = fsp->fsp_name->base_name,
3299 .twrp = fsp->fsp_name->twrp,
3302 ret = fruit_stat_base(handle, &smb_fname, false);
3306 *sbuf = smb_fname.st;
3308 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3310 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3315 sbuf->st_ex_ino = ino;
3319 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3321 SMB_STRUCT_STAT *sbuf)
3325 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3330 *sbuf = fsp->base_fsp->fsp_name->st;
3331 sbuf->st_ex_size = AFP_INFO_SIZE;
3332 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3337 static int fruit_fstat_meta(vfs_handle_struct *handle,
3339 SMB_STRUCT_STAT *sbuf,
3344 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3346 switch (fio->config->meta) {
3347 case FRUIT_META_STREAM:
3348 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3351 case FRUIT_META_NETATALK:
3352 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3356 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3360 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3364 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3366 SMB_STRUCT_STAT *sbuf)
3368 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3371 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3373 SMB_STRUCT_STAT *sbuf)
3375 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3378 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3380 SMB_STRUCT_STAT *sbuf)
3382 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3383 struct adouble *ad = NULL;
3386 if (fio->ad_fsp == NULL) {
3387 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3392 /* Populate the stat struct with info from the base file. */
3393 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3398 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3400 DBG_ERR("ad_fget [%s] failed [%s]\n",
3401 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3405 *sbuf = fsp->base_fsp->fsp_name->st;
3406 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3407 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3413 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3414 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3418 switch (fio->config->rsrc) {
3419 case FRUIT_RSRC_STREAM:
3420 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3423 case FRUIT_RSRC_ADFILE:
3424 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3427 case FRUIT_RSRC_XATTR:
3428 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3432 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3439 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3440 SMB_STRUCT_STAT *sbuf)
3442 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3446 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3449 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3451 if (fio->type == ADOUBLE_META) {
3452 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3454 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3458 sbuf->st_ex_mode &= ~S_IFMT;
3459 sbuf->st_ex_mode |= S_IFREG;
3460 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3463 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3464 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3468 static NTSTATUS delete_invalid_meta_stream(
3469 vfs_handle_struct *handle,
3470 const struct smb_filename *smb_fname,
3471 TALLOC_CTX *mem_ctx,
3472 unsigned int *pnum_streams,
3473 struct stream_struct **pstreams,
3476 struct smb_filename *sname = NULL;
3480 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3482 return NT_STATUS_INTERNAL_ERROR;
3486 return NT_STATUS_OK;
3489 sname = synthetic_smb_fname(talloc_tos(),
3490 smb_fname->base_name,
3491 AFPINFO_STREAM_NAME,
3495 if (sname == NULL) {
3496 return NT_STATUS_NO_MEMORY;
3499 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3500 handle->conn->cwd_fsp,
3505 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3506 return map_nt_error_from_unix(errno);
3509 return NT_STATUS_OK;
3512 static NTSTATUS fruit_streaminfo_meta_stream(
3513 vfs_handle_struct *handle,
3514 struct files_struct *fsp,
3515 const struct smb_filename *smb_fname,
3516 TALLOC_CTX *mem_ctx,
3517 unsigned int *pnum_streams,
3518 struct stream_struct **pstreams)
3520 struct stream_struct *stream = *pstreams;
3521 unsigned int num_streams = *pnum_streams;
3524 for (i = 0; i < num_streams; i++) {
3525 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3530 if (i == num_streams) {
3531 return NT_STATUS_OK;
3534 if (stream[i].size != AFP_INFO_SIZE) {
3535 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3536 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3538 return delete_invalid_meta_stream(handle,
3547 return NT_STATUS_OK;
3550 static NTSTATUS fruit_streaminfo_meta_netatalk(
3551 vfs_handle_struct *handle,
3552 struct files_struct *fsp,
3553 const struct smb_filename *smb_fname,
3554 TALLOC_CTX *mem_ctx,
3555 unsigned int *pnum_streams,
3556 struct stream_struct **pstreams)
3558 struct stream_struct *stream = *pstreams;
3559 unsigned int num_streams = *pnum_streams;
3560 struct adouble *ad = NULL;
3565 /* Remove the Netatalk xattr from the list */
3566 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3567 ":" NETATALK_META_XATTR ":$DATA");
3569 return NT_STATUS_NO_MEMORY;
3573 * Check if there's a AFPINFO_STREAM from the VFS streams
3574 * backend and if yes, remove it from the list
3576 for (i = 0; i < num_streams; i++) {
3577 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3582 if (i < num_streams) {
3583 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3584 smb_fname_str_dbg(smb_fname));
3586 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3589 return NT_STATUS_INTERNAL_ERROR;
3593 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3595 return NT_STATUS_OK;
3598 is_fi_empty = ad_empty_finderinfo(ad);
3602 return NT_STATUS_OK;
3605 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3606 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3607 smb_roundup(handle->conn, AFP_INFO_SIZE));
3609 return NT_STATUS_NO_MEMORY;
3612 return NT_STATUS_OK;
3615 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3616 struct files_struct *fsp,
3617 const struct smb_filename *smb_fname,
3618 TALLOC_CTX *mem_ctx,
3619 unsigned int *pnum_streams,
3620 struct stream_struct **pstreams)
3622 struct fruit_config_data *config = NULL;
3625 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3626 return NT_STATUS_INTERNAL_ERROR);
3628 switch (config->meta) {
3629 case FRUIT_META_NETATALK:
3630 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3631 mem_ctx, pnum_streams,
3635 case FRUIT_META_STREAM:
3636 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3637 mem_ctx, pnum_streams,
3642 return NT_STATUS_INTERNAL_ERROR;
3648 static NTSTATUS fruit_streaminfo_rsrc_stream(
3649 vfs_handle_struct *handle,
3650 struct files_struct *fsp,
3651 const struct smb_filename *smb_fname,
3652 TALLOC_CTX *mem_ctx,
3653 unsigned int *pnum_streams,
3654 struct stream_struct **pstreams)
3658 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3660 DBG_ERR("Filtering resource stream failed\n");
3661 return NT_STATUS_INTERNAL_ERROR;
3663 return NT_STATUS_OK;
3666 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3667 vfs_handle_struct *handle,
3668 struct files_struct *fsp,
3669 const struct smb_filename *smb_fname,
3670 TALLOC_CTX *mem_ctx,
3671 unsigned int *pnum_streams,
3672 struct stream_struct **pstreams)
3676 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3678 DBG_ERR("Filtering resource stream failed\n");
3679 return NT_STATUS_INTERNAL_ERROR;
3681 return NT_STATUS_OK;
3684 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3685 vfs_handle_struct *handle,
3686 struct files_struct *fsp,
3687 const struct smb_filename *smb_fname,
3688 TALLOC_CTX *mem_ctx,
3689 unsigned int *pnum_streams,
3690 struct stream_struct **pstreams)
3692 struct stream_struct *stream = *pstreams;
3693 unsigned int num_streams = *pnum_streams;
3694 struct adouble *ad = NULL;
3700 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3701 * and if yes, remove it from the list
3703 for (i = 0; i < num_streams; i++) {
3704 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3709 if (i < num_streams) {
3710 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3711 smb_fname_str_dbg(smb_fname));
3713 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3714 AFPRESOURCE_STREAM);
3716 return NT_STATUS_INTERNAL_ERROR;
3720 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3722 return NT_STATUS_OK;
3725 rlen = ad_getentrylen(ad, ADEID_RFORK);
3729 return NT_STATUS_OK;
3732 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3733 AFPRESOURCE_STREAM_NAME, rlen,
3734 smb_roundup(handle->conn, rlen));
3736 return NT_STATUS_NO_MEMORY;
3739 return NT_STATUS_OK;
3742 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3743 struct files_struct *fsp,
3744 const struct smb_filename *smb_fname,
3745 TALLOC_CTX *mem_ctx,
3746 unsigned int *pnum_streams,
3747 struct stream_struct **pstreams)
3749 struct fruit_config_data *config = NULL;
3752 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3753 return NT_STATUS_INTERNAL_ERROR);
3755 switch (config->rsrc) {
3756 case FRUIT_RSRC_STREAM:
3757 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3758 mem_ctx, pnum_streams,
3762 case FRUIT_RSRC_XATTR:
3763 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3764 mem_ctx, pnum_streams,
3768 case FRUIT_RSRC_ADFILE:
3769 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3770 mem_ctx, pnum_streams,
3775 return NT_STATUS_INTERNAL_ERROR;
3781 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3782 struct stream_struct **pstreams)
3784 unsigned num_streams = *pnum_streams;
3785 struct stream_struct *streams = *pstreams;
3788 if (!global_fruit_config.nego_aapl) {
3792 while (i < num_streams) {
3793 struct smb_filename smb_fname = (struct smb_filename) {
3794 .stream_name = streams[i].name,
3797 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3798 || streams[i].size > 0)
3804 streams[i] = streams[num_streams - 1];
3808 *pnum_streams = num_streams;
3811 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3812 struct files_struct *fsp,
3813 const struct smb_filename *smb_fname,
3814 TALLOC_CTX *mem_ctx,
3815 unsigned int *pnum_streams,
3816 struct stream_struct **pstreams)
3818 struct fruit_config_data *config = NULL;
3821 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3822 return NT_STATUS_UNSUCCESSFUL);
3824 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3826 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3827 pnum_streams, pstreams);
3828 if (!NT_STATUS_IS_OK(status)) {
3832 fruit_filter_empty_streams(pnum_streams, pstreams);
3834 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3835 mem_ctx, pnum_streams, pstreams);
3836 if (!NT_STATUS_IS_OK(status)) {
3840 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3841 mem_ctx, pnum_streams, pstreams);
3842 if (!NT_STATUS_IS_OK(status)) {
3846 return NT_STATUS_OK;
3849 static int fruit_ntimes(vfs_handle_struct *handle,
3850 const struct smb_filename *smb_fname,
3851 struct smb_file_time *ft)
3854 struct adouble *ad = NULL;
3855 struct fruit_config_data *config = NULL;
3857 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3860 if ((config->meta != FRUIT_META_NETATALK) ||
3861 is_omit_timespec(&ft->create_time))
3863 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3866 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3867 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3869 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3874 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3875 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3877 rc = ad_set(handle, ad, smb_fname);
3883 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3886 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3889 static int fruit_fallocate(struct vfs_handle_struct *handle,
3890 struct files_struct *fsp,
3895 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3898 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3901 /* Let the pwrite code path handle it. */
3906 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
3907 struct files_struct *fsp,
3910 #ifdef HAVE_ATTROPEN
3911 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3916 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
3917 struct files_struct *fsp,
3920 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3922 struct adouble *ad = NULL;
3925 if (fio->ad_fsp == NULL) {
3926 DBG_ERR("ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3931 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3933 DBG_ERR("ad_fget [%s] failed [%s]\n",
3934 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3938 ad_off = ad_getentryoff(ad, ADEID_RFORK);
3940 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
3946 ad_setentrylen(ad, ADEID_RFORK, offset);
3948 rc = ad_fset(handle, ad, fio->ad_fsp);
3950 DBG_ERR("ad_fset [%s] failed [%s]\n",
3951 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3960 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
3961 struct files_struct *fsp,
3964 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3967 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3968 struct files_struct *fsp,
3971 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3975 DBG_ERR("Failed to fetch fsp extension");
3979 switch (fio->config->rsrc) {
3980 case FRUIT_RSRC_XATTR:
3981 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
3984 case FRUIT_RSRC_ADFILE:
3985 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
3988 case FRUIT_RSRC_STREAM:
3989 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
3993 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4001 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4002 struct files_struct *fsp,
4006 DBG_WARNING("ftruncate %s to %jd",
4007 fsp_str_dbg(fsp), (intmax_t)offset);
4008 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4013 /* OS X returns success but does nothing */
4014 DBG_INFO("ignoring ftruncate %s to %jd\n",
4015 fsp_str_dbg(fsp), (intmax_t)offset);
4019 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4020 struct files_struct *fsp,
4023 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4026 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4030 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4033 if (fio->type == ADOUBLE_META) {
4034 ret = fruit_ftruncate_meta(handle, fsp, offset);
4036 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4039 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4043 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4044 struct smb_request *req,
4045 struct smb_filename *smb_fname,
4046 uint32_t access_mask,
4047 uint32_t share_access,
4048 uint32_t create_disposition,
4049 uint32_t create_options,
4050 uint32_t file_attributes,
4051 uint32_t oplock_request,
4052 const struct smb2_lease *lease,
4053 uint64_t allocation_size,
4054 uint32_t private_flags,
4055 struct security_descriptor *sd,
4056 struct ea_list *ea_list,
4057 files_struct **result,
4059 const struct smb2_create_blobs *in_context_blobs,
4060 struct smb2_create_blobs *out_context_blobs)
4063 struct fruit_config_data *config = NULL;
4064 files_struct *fsp = NULL;
4065 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4068 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4069 if (!NT_STATUS_IS_OK(status)) {
4073 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4074 return NT_STATUS_UNSUCCESSFUL);
4076 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4077 uint32_t conv_flags = 0;
4079 if (config->wipe_intentionally_left_blank_rfork) {
4080 conv_flags |= AD_CONV_WIPE_BLANK;
4082 if (config->delete_empty_adfiles) {
4083 conv_flags |= AD_CONV_DELETE;
4086 ret = ad_convert(handle,
4087 handle->conn->cwd_fsp,
4089 macos_string_replace_map,
4092 DBG_ERR("ad_convert() failed\n");
4093 return NT_STATUS_UNSUCCESSFUL;
4097 status = SMB_VFS_NEXT_CREATE_FILE(
4098 handle, req, smb_fname,
4099 access_mask, share_access,
4100 create_disposition, create_options,
4101 file_attributes, oplock_request,
4103 allocation_size, private_flags,
4104 sd, ea_list, result,
4105 pinfo, in_context_blobs, out_context_blobs);
4106 if (!NT_STATUS_IS_OK(status)) {
4112 if (global_fruit_config.nego_aapl) {
4113 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4115 * Enable POSIX directory rename behaviour
4117 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4122 * If this is a plain open for existing files, opening an 0
4123 * byte size resource fork MUST fail with
4124 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4126 * Cf the vfs_fruit torture tests in test_rfork_create().
4128 if (global_fruit_config.nego_aapl &&
4129 create_disposition == FILE_OPEN &&
4130 smb_fname->st.st_ex_size == 0 &&
4131 is_named_stream(smb_fname))
4133 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4137 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4141 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4142 (fsp->op != NULL) &&
4143 !fsp->fsp_flags.is_pathref)
4145 status = fruit_check_access(
4149 if (!NT_STATUS_IS_OK(status)) {
4157 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4160 close_file(req, fsp, ERROR_CLOSE);
4161 *result = fsp = NULL;
4167 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4168 const struct smb_filename *fname,
4169 TALLOC_CTX *mem_ctx,
4170 struct readdir_attr_data **pattr_data)
4172 struct fruit_config_data *config = NULL;
4173 struct readdir_attr_data *attr_data;
4174 uint32_t conv_flags = 0;
4178 SMB_VFS_HANDLE_GET_DATA(handle, config,
4179 struct fruit_config_data,
4180 return NT_STATUS_UNSUCCESSFUL);
4182 if (!global_fruit_config.nego_aapl) {
4183 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4186 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4188 if (config->wipe_intentionally_left_blank_rfork) {
4189 conv_flags |= AD_CONV_WIPE_BLANK;
4191 if (config->delete_empty_adfiles) {
4192 conv_flags |= AD_CONV_DELETE;
4195 ret = ad_convert(handle,
4196 handle->conn->cwd_fsp,
4198 macos_string_replace_map,
4201 DBG_ERR("ad_convert() failed\n");
4202 return NT_STATUS_UNSUCCESSFUL;
4205 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4206 if (*pattr_data == NULL) {
4207 return NT_STATUS_UNSUCCESSFUL;
4209 attr_data = *pattr_data;
4210 attr_data->type = RDATTR_AAPL;
4213 * Mac metadata: compressed FinderInfo, resource fork length
4216 status = readdir_attr_macmeta(handle, fname, attr_data);
4217 if (!NT_STATUS_IS_OK(status)) {
4219 * Error handling is tricky: if we return failure from
4220 * this function, the corresponding directory entry
4221 * will to be passed to the client, so we really just
4222 * want to error out on fatal errors.
4224 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4232 if (config->unix_info_enabled) {
4233 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4239 if (!config->readdir_attr_max_access) {
4240 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4242 status = smbd_calculate_access_mask(
4244 handle->conn->cwd_fsp,
4247 SEC_FLAG_MAXIMUM_ALLOWED,
4248 &attr_data->attr_data.aapl.max_access);
4249 if (!NT_STATUS_IS_OK(status)) {
4254 return NT_STATUS_OK;
4257 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4258 fname->base_name, nt_errstr(status)));
4259 TALLOC_FREE(*pattr_data);
4263 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4265 uint32_t security_info,
4266 TALLOC_CTX *mem_ctx,
4267 struct security_descriptor **ppdesc)
4270 struct security_ace ace;
4272 struct fruit_config_data *config;
4274 SMB_VFS_HANDLE_GET_DATA(handle, config,
4275 struct fruit_config_data,
4276 return NT_STATUS_UNSUCCESSFUL);
4278 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4280 if (!NT_STATUS_IS_OK(status)) {
4285 * Add MS NFS style ACEs with uid, gid and mode
4287 if (!global_fruit_config.nego_aapl) {
4288 return NT_STATUS_OK;
4290 if (!config->unix_info_enabled) {
4291 return NT_STATUS_OK;
4294 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4295 status = remove_virtual_nfs_aces(*ppdesc);
4296 if (!NT_STATUS_IS_OK(status)) {
4297 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4301 /* MS NFS style mode */
4302 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4303 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4304 status = security_descriptor_dacl_add(*ppdesc, &ace);
4305 if (!NT_STATUS_IS_OK(status)) {
4306 DEBUG(1,("failed to add MS NFS style ACE\n"));
4310 /* MS NFS style uid */
4311 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4312 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4313 status = security_descriptor_dacl_add(*ppdesc, &ace);
4314 if (!NT_STATUS_IS_OK(status)) {
4315 DEBUG(1,("failed to add MS NFS style ACE\n"));
4319 /* MS NFS style gid */
4320 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4321 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4322 status = security_descriptor_dacl_add(*ppdesc, &ace);
4323 if (!NT_STATUS_IS_OK(status)) {
4324 DEBUG(1,("failed to add MS NFS style ACE\n"));
4328 return NT_STATUS_OK;
4331 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4333 uint32_t security_info_sent,
4334 const struct security_descriptor *orig_psd)
4338 mode_t ms_nfs_mode = 0;
4340 struct security_descriptor *psd = NULL;
4341 uint32_t orig_num_aces = 0;
4343 if (orig_psd->dacl != NULL) {
4344 orig_num_aces = orig_psd->dacl->num_aces;
4347 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4349 return NT_STATUS_NO_MEMORY;
4352 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4354 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4355 if (!NT_STATUS_IS_OK(status)) {
4356 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4362 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4363 * sent/present flags correctly now we've removed them.
4366 if (orig_num_aces != 0) {
4368 * Are there any ACE's left ?
4370 if (psd->dacl->num_aces == 0) {
4371 /* No - clear the DACL sent/present flags. */
4372 security_info_sent &= ~SECINFO_DACL;
4373 psd->type &= ~SEC_DESC_DACL_PRESENT;
4377 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4378 if (!NT_STATUS_IS_OK(status)) {
4379 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4385 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4387 DBG_WARNING("%s, result: %d, %04o error %s\n",
4390 (unsigned)ms_nfs_mode,
4392 status = map_nt_error_from_unix(errno);
4399 return NT_STATUS_OK;
4402 static struct vfs_offload_ctx *fruit_offload_ctx;
4404 struct fruit_offload_read_state {
4405 struct vfs_handle_struct *handle;
4406 struct tevent_context *ev;
4412 static void fruit_offload_read_done(struct tevent_req *subreq);
4414 static struct tevent_req *fruit_offload_read_send(
4415 TALLOC_CTX *mem_ctx,
4416 struct tevent_context *ev,
4417 struct vfs_handle_struct *handle,
4424 struct tevent_req *req = NULL;
4425 struct tevent_req *subreq = NULL;
4426 struct fruit_offload_read_state *state = NULL;
4428 req = tevent_req_create(mem_ctx, &state,
4429 struct fruit_offload_read_state);
4433 *state = (struct fruit_offload_read_state) {
4440 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4441 fsctl, ttl, offset, to_copy);
4442 if (tevent_req_nomem(subreq, req)) {
4443 return tevent_req_post(req, ev);
4445 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4449 static void fruit_offload_read_done(struct tevent_req *subreq)
4451 struct tevent_req *req = tevent_req_callback_data(
4452 subreq, struct tevent_req);
4453 struct fruit_offload_read_state *state = tevent_req_data(
4454 req, struct fruit_offload_read_state);
4457 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4461 TALLOC_FREE(subreq);
4462 if (tevent_req_nterror(req, status)) {
4466 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4467 tevent_req_done(req);
4471 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4472 &fruit_offload_ctx);
4473 if (tevent_req_nterror(req, status)) {
4477 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4480 if (tevent_req_nterror(req, status)) {
4484 tevent_req_done(req);
4488 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4489 struct vfs_handle_struct *handle,
4490 TALLOC_CTX *mem_ctx,
4493 struct fruit_offload_read_state *state = tevent_req_data(
4494 req, struct fruit_offload_read_state);
4497 if (tevent_req_is_nterror(req, &status)) {
4498 tevent_req_received(req);
4502 token->length = state->token.length;
4503 token->data = talloc_move(mem_ctx, &state->token.data);
4505 tevent_req_received(req);
4506 return NT_STATUS_OK;
4509 struct fruit_offload_write_state {
4510 struct vfs_handle_struct *handle;
4512 struct files_struct *src_fsp;
4513 struct files_struct *dst_fsp;
4517 static void fruit_offload_write_done(struct tevent_req *subreq);
4518 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4519 TALLOC_CTX *mem_ctx,
4520 struct tevent_context *ev,
4523 off_t transfer_offset,
4524 struct files_struct *dest_fsp,
4528 struct tevent_req *req, *subreq;
4529 struct fruit_offload_write_state *state;
4531 struct fruit_config_data *config;
4532 off_t src_off = transfer_offset;
4533 files_struct *src_fsp = NULL;
4534 off_t to_copy = num;
4535 bool copyfile_enabled = false;
4537 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4538 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4540 SMB_VFS_HANDLE_GET_DATA(handle, config,
4541 struct fruit_config_data,
4544 req = tevent_req_create(mem_ctx, &state,
4545 struct fruit_offload_write_state);
4549 state->handle = handle;
4550 state->dst_fsp = dest_fsp;
4553 case FSCTL_SRV_COPYCHUNK:
4554 case FSCTL_SRV_COPYCHUNK_WRITE:
4555 copyfile_enabled = config->copyfile_enabled;
4562 * Check if this a OS X copyfile style copychunk request with
4563 * a requested chunk count of 0 that was translated to a
4564 * offload_write_send VFS call overloading the parameters src_off
4565 * = dest_off = num = 0.
4567 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4568 status = vfs_offload_token_db_fetch_fsp(
4569 fruit_offload_ctx, token, &src_fsp);
4570 if (tevent_req_nterror(req, status)) {
4571 return tevent_req_post(req, ev);
4573 state->src_fsp = src_fsp;
4575 status = vfs_stat_fsp(src_fsp);
4576 if (tevent_req_nterror(req, status)) {
4577 return tevent_req_post(req, ev);
4580 to_copy = src_fsp->fsp_name->st.st_ex_size;
4581 state->is_copyfile = true;
4584 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4593 if (tevent_req_nomem(subreq, req)) {
4594 return tevent_req_post(req, ev);
4597 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4601 static void fruit_offload_write_done(struct tevent_req *subreq)
4603 struct tevent_req *req = tevent_req_callback_data(
4604 subreq, struct tevent_req);
4605 struct fruit_offload_write_state *state = tevent_req_data(
4606 req, struct fruit_offload_write_state);
4608 unsigned int num_streams = 0;
4609 struct stream_struct *streams = NULL;
4611 struct smb_filename *src_fname_tmp = NULL;
4612 struct smb_filename *dst_fname_tmp = NULL;
4614 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4617 TALLOC_FREE(subreq);
4618 if (tevent_req_nterror(req, status)) {
4622 if (!state->is_copyfile) {
4623 tevent_req_done(req);
4628 * Now copy all remaining streams. We know the share supports
4629 * streams, because we're in vfs_fruit. We don't do this async
4630 * because streams are few and small.
4632 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4633 state->src_fsp->fsp_name,
4634 req, &num_streams, &streams);
4635 if (tevent_req_nterror(req, status)) {
4639 if (num_streams == 1) {
4640 /* There is always one stream, ::$DATA. */
4641 tevent_req_done(req);
4645 for (i = 0; i < num_streams; i++) {
4646 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4647 __func__, streams[i].name, (size_t)streams[i].size));
4649 src_fname_tmp = synthetic_smb_fname(
4651 state->src_fsp->fsp_name->base_name,
4654 state->src_fsp->fsp_name->twrp,
4655 state->src_fsp->fsp_name->flags);
4656 if (tevent_req_nomem(src_fname_tmp, req)) {
4660 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4661 TALLOC_FREE(src_fname_tmp);
4665 dst_fname_tmp = synthetic_smb_fname(
4667 state->dst_fsp->fsp_name->base_name,
4670 state->dst_fsp->fsp_name->twrp,
4671 state->dst_fsp->fsp_name->flags);
4672 if (tevent_req_nomem(dst_fname_tmp, req)) {
4673 TALLOC_FREE(src_fname_tmp);
4677 status = copy_file(req,
4678 state->handle->conn,
4681 OPENX_FILE_CREATE_IF_NOT_EXIST,
4683 if (!NT_STATUS_IS_OK(status)) {
4684 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4685 smb_fname_str_dbg(src_fname_tmp),
4686 smb_fname_str_dbg(dst_fname_tmp),
4687 nt_errstr(status)));
4688 TALLOC_FREE(src_fname_tmp);
4689 TALLOC_FREE(dst_fname_tmp);
4690 tevent_req_nterror(req, status);
4694 TALLOC_FREE(src_fname_tmp);
4695 TALLOC_FREE(dst_fname_tmp);
4698 TALLOC_FREE(streams);
4699 TALLOC_FREE(src_fname_tmp);
4700 TALLOC_FREE(dst_fname_tmp);
4701 tevent_req_done(req);
4704 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4705 struct tevent_req *req,
4708 struct fruit_offload_write_state *state = tevent_req_data(
4709 req, struct fruit_offload_write_state);
4712 if (tevent_req_is_nterror(req, &status)) {
4713 DEBUG(1, ("server side copy chunk failed: %s\n",
4714 nt_errstr(status)));
4716 tevent_req_received(req);
4720 *copied = state->copied;
4721 tevent_req_received(req);
4723 return NT_STATUS_OK;
4726 static char *fruit_get_bandsize_line(char **lines, int numlines)
4729 static bool re_initialized = false;
4733 if (!re_initialized) {
4734 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4738 re_initialized = true;
4741 for (i = 0; i < numlines; i++) {
4742 regmatch_t matches[1];
4744 ret = regexec(&re, lines[i], 1, matches, 0);
4747 * Check if the match was on the last line, sa we want
4748 * the subsequent line.
4750 if (i + 1 == numlines) {
4753 return lines[i + 1];
4755 if (ret != REG_NOMATCH) {
4763 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4766 static bool re_initialized = false;
4767 regmatch_t matches[2];
4772 if (!re_initialized) {
4775 "<integer>\\([[:digit:]]*\\)</integer>$",
4780 re_initialized = true;
4783 ret = regexec(&re, line, 2, matches, 0);
4785 DBG_ERR("regex failed [%s]\n", line);
4789 line[matches[1].rm_eo] = '\0';
4791 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4795 *_band_size = (size_t)band_size;
4800 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4801 * "band-size" key and value.
4803 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4807 #define INFO_PLIST_MAX_SIZE 64*1024
4809 struct smb_filename *smb_fname = NULL;
4810 files_struct *fsp = NULL;
4811 uint8_t *file_data = NULL;
4812 char **lines = NULL;
4813 char *band_size_line = NULL;
4814 size_t plist_file_size;
4821 plist = talloc_asprintf(talloc_tos(),
4823 handle->conn->connectpath,
4825 if (plist == NULL) {
4830 smb_fname = synthetic_smb_fname(talloc_tos(),
4836 if (smb_fname == NULL) {
4841 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4843 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4848 plist_file_size = smb_fname->st.st_ex_size;
4850 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4851 DBG_INFO("%s is too large, ignoring\n", plist);
4856 status = SMB_VFS_NEXT_CREATE_FILE(
4859 smb_fname, /* fname */
4860 FILE_GENERIC_READ, /* access_mask */
4861 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4862 FILE_OPEN, /* create_disposition */
4863 0, /* create_options */
4864 0, /* file_attributes */
4865 INTERNAL_OPEN_ONLY, /* oplock_request */
4867 0, /* allocation_size */
4868 0, /* private_flags */
4873 NULL, NULL); /* create context */
4874 if (!NT_STATUS_IS_OK(status)) {
4875 DBG_INFO("Opening [%s] failed [%s]\n",
4876 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4881 file_data = talloc_zero_array(talloc_tos(),
4883 plist_file_size + 1);
4884 if (file_data == NULL) {
4889 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4890 if (nread != plist_file_size) {
4891 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4892 fsp_str_dbg(fsp), nread, plist_file_size);
4898 status = close_file(NULL, fsp, NORMAL_CLOSE);
4900 if (!NT_STATUS_IS_OK(status)) {
4901 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4906 lines = file_lines_parse((char *)file_data,
4910 if (lines == NULL) {
4915 band_size_line = fruit_get_bandsize_line(lines, numlines);
4916 if (band_size_line == NULL) {
4917 DBG_ERR("Didn't find band-size key in [%s]\n",
4918 smb_fname_str_dbg(smb_fname));
4923 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
4925 DBG_ERR("fruit_get_bandsize_from_line failed\n");
4929 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
4933 status = close_file(NULL, fsp, NORMAL_CLOSE);
4934 if (!NT_STATUS_IS_OK(status)) {
4935 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4940 TALLOC_FREE(smb_fname);
4941 TALLOC_FREE(file_data);
4946 struct fruit_disk_free_state {
4950 static bool fruit_get_num_bands(vfs_handle_struct *handle,
4955 struct smb_filename *bands_dir = NULL;
4956 struct smb_Dir *dir_hnd = NULL;
4957 const char *dname = NULL;
4958 char *talloced = NULL;
4962 path = talloc_asprintf(talloc_tos(),
4964 handle->conn->connectpath,
4970 bands_dir = synthetic_smb_fname(talloc_tos(),
4977 if (bands_dir == NULL) {
4981 dir_hnd = OpenDir(talloc_tos(), handle->conn, bands_dir, NULL, 0);
4982 if (dir_hnd == NULL) {
4983 TALLOC_FREE(bands_dir);
4989 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
4992 if (ISDOT(dname) || ISDOTDOT(dname)) {
4997 TALLOC_FREE(dir_hnd);
4999 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5001 TALLOC_FREE(bands_dir);
5007 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5008 struct fruit_disk_free_state *state,
5013 size_t sparsebundle_strlen = strlen("sparsebundle");
5014 size_t bandsize = 0;
5018 p = strstr(name, "sparsebundle");
5023 if (p[sparsebundle_strlen] != '\0') {
5027 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5029 ok = fruit_get_bandsize(handle, name, &bandsize);
5032 * Beware of race conditions: this may be an uninitialized
5033 * Info.plist that a client is just creating. We don't want let
5034 * this to trigger complete failure.
5036 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5040 ok = fruit_get_num_bands(handle, name, &nbands);
5043 * Beware of race conditions: this may be a backup sparsebundle
5044 * in an early stage lacking a bands subdirectory. We don't want
5045 * let this to trigger complete failure.
5047 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5052 * Arithmetic on 32-bit systems may cause overflow, depending on
5053 * size_t precision. First we check its unlikely, then we
5054 * force the precision into target off_t, then we check that
5055 * the total did not overflow either.
5057 if (bandsize > SIZE_MAX/nbands) {
5058 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5062 tm_size = (off_t)bandsize * (off_t)nbands;
5064 if (state->total_size + tm_size < state->total_size) {
5065 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5070 state->total_size += tm_size;
5072 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5073 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5079 * Calculate used size of a TimeMachine volume
5081 * This assumes that the volume is used only for TimeMachine.
5083 * - readdir(basedir of share), then
5084 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5085 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5086 * - count band files in "\1.sparsebundle/bands/"
5087 * - calculate used size of all bands: band_count * band_size
5089 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5090 const struct smb_filename *smb_fname,
5095 struct fruit_config_data *config = NULL;
5096 struct fruit_disk_free_state state = {0};
5097 struct smb_Dir *dir_hnd = NULL;
5098 const char *dname = NULL;
5099 char *talloced = NULL;
5105 SMB_VFS_HANDLE_GET_DATA(handle, config,
5106 struct fruit_config_data,
5109 if (!config->time_machine ||
5110 config->time_machine_max_size == 0)
5112 return SMB_VFS_NEXT_DISK_FREE(handle,
5119 dir_hnd = OpenDir(talloc_tos(), handle->conn, smb_fname, NULL, 0);
5120 if (dir_hnd == NULL) {
5124 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5127 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5129 TALLOC_FREE(talloced);
5130 TALLOC_FREE(dir_hnd);
5133 TALLOC_FREE(talloced);
5136 TALLOC_FREE(dir_hnd);
5138 dsize = config->time_machine_max_size / 512;
5139 dfree = dsize - (state.total_size / 512);
5140 if (dfree > dsize) {
5150 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5151 const SMB_STRUCT_STAT *psbuf)
5153 struct fruit_config_data *config = NULL;
5155 SMB_VFS_HANDLE_GET_DATA(handle, config,
5156 struct fruit_config_data,
5159 if (global_fruit_config.nego_aapl &&
5160 config->aapl_zero_file_id)
5165 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5168 static struct vfs_fn_pointers vfs_fruit_fns = {
5169 .connect_fn = fruit_connect,
5170 .disk_free_fn = fruit_disk_free,
5172 /* File operations */
5173 .chmod_fn = fruit_chmod,
5174 .unlinkat_fn = fruit_unlinkat,
5175 .renameat_fn = fruit_renameat,
5176 .openat_fn = fruit_openat,
5177 .close_fn = fruit_close,
5178 .pread_fn = fruit_pread,
5179 .pwrite_fn = fruit_pwrite,
5180 .pread_send_fn = fruit_pread_send,
5181 .pread_recv_fn = fruit_pread_recv,
5182 .pwrite_send_fn = fruit_pwrite_send,
5183 .pwrite_recv_fn = fruit_pwrite_recv,
5184 .stat_fn = fruit_stat,
5185 .lstat_fn = fruit_lstat,
5186 .fstat_fn = fruit_fstat,
5187 .streaminfo_fn = fruit_streaminfo,
5188 .ntimes_fn = fruit_ntimes,
5189 .ftruncate_fn = fruit_ftruncate,
5190 .fallocate_fn = fruit_fallocate,
5191 .create_file_fn = fruit_create_file,
5192 .readdir_attr_fn = fruit_readdir_attr,
5193 .offload_read_send_fn = fruit_offload_read_send,
5194 .offload_read_recv_fn = fruit_offload_read_recv,
5195 .offload_write_send_fn = fruit_offload_write_send,
5196 .offload_write_recv_fn = fruit_offload_write_recv,
5197 .fs_file_id_fn = fruit_fs_file_id,
5199 /* NT ACL operations */
5200 .fget_nt_acl_fn = fruit_fget_nt_acl,
5201 .fset_nt_acl_fn = fruit_fset_nt_acl,
5205 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5207 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5209 if (!NT_STATUS_IS_OK(ret)) {
5213 vfs_fruit_debug_level = debug_add_class("fruit");
5214 if (vfs_fruit_debug_level == -1) {
5215 vfs_fruit_debug_level = DBGC_VFS;
5216 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5219 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5220 "vfs_fruit_init","fruit",vfs_fruit_debug_level));