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 /* tcon config handle */
176 struct fruit_config_data *config;
178 /* Denote stream type, meta or rsrc */
181 /* Whether the create created the stream */
185 * AFP_AfpInfo stream created, but not written yet, thus still a fake
186 * pipe fd. This is set to true in fruit_open_meta if there was no
187 * existing stream but the caller requested O_CREAT. It is later set to
188 * false when we get a write on the stream that then does open and
196 /*****************************************************************************
198 *****************************************************************************/
201 * Initialize config struct from our smb.conf config parameters
203 static int init_fruit_config(vfs_handle_struct *handle)
205 struct fruit_config_data *config;
207 const char *tm_size_str = NULL;
209 config = talloc_zero(handle->conn, struct fruit_config_data);
211 DEBUG(1, ("talloc_zero() failed\n"));
217 * Versions up to Samba 4.5.x had a spelling bug in the
218 * fruit:resource option calling lp_parm_enum with
219 * "res*s*ource" (ie two s).
221 * In Samba 4.6 we accept both the wrong and the correct
222 * spelling, in Samba 4.7 the bad spelling will be removed.
224 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
225 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
227 DEBUG(1, ("value for %s: resource type unknown\n",
228 FRUIT_PARAM_TYPE_NAME));
231 config->rsrc = (enum fruit_rsrc)enumval;
233 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
234 "resource", fruit_rsrc, enumval);
236 DEBUG(1, ("value for %s: resource type unknown\n",
237 FRUIT_PARAM_TYPE_NAME));
240 config->rsrc = (enum fruit_rsrc)enumval;
242 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
243 "metadata", fruit_meta, FRUIT_META_NETATALK);
245 DEBUG(1, ("value for %s: metadata type unknown\n",
246 FRUIT_PARAM_TYPE_NAME));
249 config->meta = (enum fruit_meta)enumval;
251 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
252 "locking", fruit_locking, FRUIT_LOCKING_NONE);
254 DEBUG(1, ("value for %s: locking type unknown\n",
255 FRUIT_PARAM_TYPE_NAME));
258 config->locking = (enum fruit_locking)enumval;
260 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
261 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
263 DEBUG(1, ("value for %s: encoding type unknown\n",
264 FRUIT_PARAM_TYPE_NAME));
267 config->encoding = (enum fruit_encoding)enumval;
269 if (config->rsrc == FRUIT_RSRC_ADFILE) {
270 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
271 FRUIT_PARAM_TYPE_NAME,
276 config->use_aapl = lp_parm_bool(
277 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
279 config->time_machine = lp_parm_bool(
280 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
282 config->unix_info_enabled = lp_parm_bool(
283 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
285 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
288 config->posix_rename = lp_parm_bool(
289 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
291 config->aapl_zero_file_id =
292 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
293 "zero_file_id", false);
295 config->readdir_attr_rsize = lp_parm_bool(
296 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
298 config->readdir_attr_finder_info = lp_parm_bool(
299 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
301 config->readdir_attr_max_access = lp_parm_bool(
302 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
304 config->model = lp_parm_const_string(
305 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
307 tm_size_str = lp_parm_const_string(
308 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
309 "time machine max size", NULL);
310 if (tm_size_str != NULL) {
311 config->time_machine_max_size = conv_str_size(tm_size_str);
314 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
315 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
316 "wipe_intentionally_left_blank_rfork", false);
318 config->delete_empty_adfiles = lp_parm_bool(
319 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
320 "delete_empty_adfiles", false);
322 SMB_VFS_HANDLE_SET_DATA(handle, config,
323 NULL, struct fruit_config_data,
329 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
330 struct stream_struct **streams,
331 const char *name, off_t size,
334 struct stream_struct *tmp;
336 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
342 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
343 if (tmp[*num_streams].name == NULL) {
347 tmp[*num_streams].size = size;
348 tmp[*num_streams].alloc_size = alloc_size;
355 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
356 struct stream_struct **streams)
358 struct stream_struct *tmp = *streams;
361 if (*num_streams == 0) {
365 for (i = 0; i < *num_streams; i++) {
366 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
371 if (i == *num_streams) {
375 if (tmp[i].size > 0) {
379 TALLOC_FREE(tmp[i].name);
380 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
385 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
386 struct stream_struct **streams,
389 struct stream_struct *tmp = *streams;
392 if (*num_streams == 0) {
396 for (i = 0; i < *num_streams; i++) {
397 if (strequal_m(tmp[i].name, name)) {
402 if (i == *num_streams) {
406 TALLOC_FREE(tmp[i].name);
407 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
412 static bool ad_empty_finderinfo(const struct adouble *ad)
415 char emptybuf[ADEDLEN_FINDERI] = {0};
418 fi = ad_get_entry(ad, ADEID_FINDERI);
420 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
424 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
428 static bool ai_empty_finderinfo(const AfpInfo *ai)
431 char emptybuf[ADEDLEN_FINDERI] = {0};
433 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
438 * Update btime with btime from Netatalk
440 static void update_btime(vfs_handle_struct *handle,
441 struct smb_filename *smb_fname)
444 struct timespec creation_time = {0};
446 struct fruit_config_data *config = NULL;
448 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
451 switch (config->meta) {
452 case FRUIT_META_STREAM:
454 case FRUIT_META_NETATALK:
458 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
462 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
466 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
472 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
473 update_stat_ex_create_time(&smb_fname->st, creation_time);
479 * Map an access mask to a Netatalk single byte byte range lock
481 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
482 uint32_t access_mask)
486 switch (access_mask) {
488 offset = AD_FILELOCK_OPEN_RD;
491 case FILE_WRITE_DATA:
492 case FILE_APPEND_DATA:
493 offset = AD_FILELOCK_OPEN_WR;
497 offset = AD_FILELOCK_OPEN_NONE;
501 if (fork_type == APPLE_FORK_RSRC) {
502 if (offset == AD_FILELOCK_OPEN_NONE) {
503 offset = AD_FILELOCK_RSRC_OPEN_NONE;
513 * Map a deny mode to a Netatalk brl
515 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
522 offset = AD_FILELOCK_DENY_RD;
526 offset = AD_FILELOCK_DENY_WR;
530 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
533 if (fork_type == APPLE_FORK_RSRC) {
541 * Call fcntl() with an exclusive F_GETLK request in order to
542 * determine if there's an existing shared lock
544 * @return true if the requested lock was found or any error occurred
545 * false if the lock was not found
547 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
550 off_t offset = in_offset;
555 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
556 if (result == false) {
560 if (type != F_UNLCK) {
567 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
569 uint32_t access_mask,
572 NTSTATUS status = NT_STATUS_OK;
574 bool share_for_read = (share_mode & FILE_SHARE_READ);
575 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
576 bool netatalk_already_open_for_reading = false;
577 bool netatalk_already_open_for_writing = false;
578 bool netatalk_already_open_with_deny_read = false;
579 bool netatalk_already_open_with_deny_write = false;
580 struct GUID req_guid = GUID_random();
582 /* FIXME: hardcoded data fork, add resource fork */
583 enum apple_fork fork_type = APPLE_FORK_DATA;
585 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
587 access_mask & FILE_READ_DATA ? "READ" :"-",
588 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
591 if (fsp->fh->fd == -1) {
595 /* Read NetATalk opens and deny modes on the file. */
596 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
597 access_to_netatalk_brl(fork_type,
600 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
601 denymode_to_netatalk_brl(fork_type,
604 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
605 access_to_netatalk_brl(fork_type,
608 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
609 denymode_to_netatalk_brl(fork_type,
612 /* If there are any conflicts - sharing violation. */
613 if ((access_mask & FILE_READ_DATA) &&
614 netatalk_already_open_with_deny_read) {
615 return NT_STATUS_SHARING_VIOLATION;
618 if (!share_for_read &&
619 netatalk_already_open_for_reading) {
620 return NT_STATUS_SHARING_VIOLATION;
623 if ((access_mask & FILE_WRITE_DATA) &&
624 netatalk_already_open_with_deny_write) {
625 return NT_STATUS_SHARING_VIOLATION;
628 if (!share_for_write &&
629 netatalk_already_open_for_writing) {
630 return NT_STATUS_SHARING_VIOLATION;
633 if (!(access_mask & FILE_READ_DATA)) {
635 * Nothing we can do here, we need read access
641 /* Set NetAtalk locks matching our access */
642 if (access_mask & FILE_READ_DATA) {
643 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
644 req_guid.time_hi_and_version = __LINE__;
649 fsp->op->global->open_persistent_id,
657 if (!NT_STATUS_IS_OK(status)) {
662 if (!share_for_read) {
663 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
664 req_guid.time_hi_and_version = __LINE__;
669 fsp->op->global->open_persistent_id,
677 if (!NT_STATUS_IS_OK(status)) {
682 if (access_mask & FILE_WRITE_DATA) {
683 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
684 req_guid.time_hi_and_version = __LINE__;
689 fsp->op->global->open_persistent_id,
697 if (!NT_STATUS_IS_OK(status)) {
702 if (!share_for_write) {
703 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
704 req_guid.time_hi_and_version = __LINE__;
709 fsp->op->global->open_persistent_id,
717 if (!NT_STATUS_IS_OK(status)) {
725 static NTSTATUS check_aapl(vfs_handle_struct *handle,
726 struct smb_request *req,
727 const struct smb2_create_blobs *in_context_blobs,
728 struct smb2_create_blobs *out_context_blobs)
730 struct fruit_config_data *config;
732 struct smb2_create_blob *aapl = NULL;
736 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
737 uint64_t req_bitmap, client_caps;
738 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
742 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
743 return NT_STATUS_UNSUCCESSFUL);
745 if (!config->use_aapl
746 || in_context_blobs == NULL
747 || out_context_blobs == NULL) {
751 aapl = smb2_create_blob_find(in_context_blobs,
752 SMB2_CREATE_TAG_AAPL);
757 if (aapl->data.length != 24) {
758 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
759 (uintmax_t)aapl->data.length));
760 return NT_STATUS_INVALID_PARAMETER;
763 cmd = IVAL(aapl->data.data, 0);
764 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
765 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
766 return NT_STATUS_INVALID_PARAMETER;
769 req_bitmap = BVAL(aapl->data.data, 8);
770 client_caps = BVAL(aapl->data.data, 16);
772 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
774 SBVAL(p, 8, req_bitmap);
775 ok = data_blob_append(req, &blob, p, 16);
777 return NT_STATUS_UNSUCCESSFUL;
780 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
781 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
782 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
783 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
784 config->readdir_attr_enabled = true;
787 if (config->use_copyfile) {
788 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
789 config->copyfile_enabled = true;
793 * The client doesn't set the flag, so we can't check
794 * for it and just set it unconditionally
796 if (config->unix_info_enabled) {
797 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
800 SBVAL(p, 0, server_caps);
801 ok = data_blob_append(req, &blob, p, 8);
803 return NT_STATUS_UNSUCCESSFUL;
807 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
808 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
816 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
823 if (config->time_machine) {
824 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
829 ok = data_blob_append(req, &blob, p, 8);
831 return NT_STATUS_UNSUCCESSFUL;
835 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
836 ok = convert_string_talloc(req,
838 config->model, strlen(config->model),
841 return NT_STATUS_UNSUCCESSFUL;
845 SIVAL(p + 4, 0, modellen);
846 ok = data_blob_append(req, &blob, p, 8);
849 return NT_STATUS_UNSUCCESSFUL;
852 ok = data_blob_append(req, &blob, model, modellen);
855 return NT_STATUS_UNSUCCESSFUL;
859 status = smb2_create_blob_add(out_context_blobs,
861 SMB2_CREATE_TAG_AAPL,
863 if (NT_STATUS_IS_OK(status)) {
864 global_fruit_config.nego_aapl = true;
870 static bool readdir_attr_meta_finderi_stream(
871 struct vfs_handle_struct *handle,
872 const struct smb_filename *smb_fname,
875 struct smb_filename *stream_name = NULL;
876 files_struct *fsp = NULL;
881 uint8_t buf[AFP_INFO_SIZE];
883 stream_name = synthetic_smb_fname(talloc_tos(),
884 smb_fname->base_name,
889 if (stream_name == NULL) {
893 ret = SMB_VFS_STAT(handle->conn, stream_name);
898 status = SMB_VFS_CREATE_FILE(
899 handle->conn, /* conn */
901 0, /* root_dir_fid */
902 stream_name, /* fname */
903 FILE_READ_DATA, /* access_mask */
904 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
906 FILE_OPEN, /* create_disposition*/
907 0, /* create_options */
908 0, /* file_attributes */
909 INTERNAL_OPEN_ONLY, /* oplock_request */
911 0, /* allocation_size */
912 0, /* private_flags */
917 NULL, NULL); /* create context */
919 TALLOC_FREE(stream_name);
921 if (!NT_STATUS_IS_OK(status)) {
925 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
926 if (nread != AFP_INFO_SIZE) {
927 DBG_ERR("short read [%s] [%zd/%d]\n",
928 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
933 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
940 close_file(NULL, fsp, NORMAL_CLOSE);
946 static bool readdir_attr_meta_finderi_netatalk(
947 struct vfs_handle_struct *handle,
948 const struct smb_filename *smb_fname,
951 struct adouble *ad = NULL;
954 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
959 p = ad_get_entry(ad, ADEID_FINDERI);
961 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
966 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
971 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
972 const struct smb_filename *smb_fname,
973 struct readdir_attr_data *attr_data)
975 struct fruit_config_data *config = NULL;
980 SMB_VFS_HANDLE_GET_DATA(handle, config,
981 struct fruit_config_data,
984 switch (config->meta) {
985 case FRUIT_META_NETATALK:
986 ok = readdir_attr_meta_finderi_netatalk(
987 handle, smb_fname, &ai);
990 case FRUIT_META_STREAM:
991 ok = readdir_attr_meta_finderi_stream(
992 handle, smb_fname, &ai);
996 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1001 /* Don't bother with errors, it's likely ENOENT */
1005 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1007 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1008 &ai.afpi_FinderInfo[0], 4);
1010 /* finder_creator */
1011 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1012 &ai.afpi_FinderInfo[4], 4);
1016 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1017 &ai.afpi_FinderInfo[8], 2);
1019 /* finder_ext_flags */
1020 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1021 &ai.afpi_FinderInfo[24], 2);
1024 date_added = convert_time_t_to_uint32_t(
1025 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1027 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1032 static uint64_t readdir_attr_rfork_size_adouble(
1033 struct vfs_handle_struct *handle,
1034 const struct smb_filename *smb_fname)
1036 struct adouble *ad = NULL;
1037 uint64_t rfork_size;
1039 ad = ad_get(talloc_tos(), handle, smb_fname,
1045 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1051 static uint64_t readdir_attr_rfork_size_stream(
1052 struct vfs_handle_struct *handle,
1053 const struct smb_filename *smb_fname)
1055 struct smb_filename *stream_name = NULL;
1057 uint64_t rfork_size;
1059 stream_name = synthetic_smb_fname(talloc_tos(),
1060 smb_fname->base_name,
1061 AFPRESOURCE_STREAM_NAME,
1065 if (stream_name == NULL) {
1069 ret = SMB_VFS_STAT(handle->conn, stream_name);
1071 TALLOC_FREE(stream_name);
1075 rfork_size = stream_name->st.st_ex_size;
1076 TALLOC_FREE(stream_name);
1081 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1082 const struct smb_filename *smb_fname)
1084 struct fruit_config_data *config = NULL;
1085 uint64_t rfork_size;
1087 SMB_VFS_HANDLE_GET_DATA(handle, config,
1088 struct fruit_config_data,
1091 switch (config->rsrc) {
1092 case FRUIT_RSRC_ADFILE:
1093 rfork_size = readdir_attr_rfork_size_adouble(handle,
1097 case FRUIT_RSRC_XATTR:
1098 case FRUIT_RSRC_STREAM:
1099 rfork_size = readdir_attr_rfork_size_stream(handle,
1104 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1112 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1113 const struct smb_filename *smb_fname,
1114 struct readdir_attr_data *attr_data)
1116 NTSTATUS status = NT_STATUS_OK;
1117 struct fruit_config_data *config = NULL;
1120 SMB_VFS_HANDLE_GET_DATA(handle, config,
1121 struct fruit_config_data,
1122 return NT_STATUS_UNSUCCESSFUL);
1125 /* Ensure we return a default value in the creation_date field */
1126 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1129 * Resource fork length
1132 if (config->readdir_attr_rsize) {
1133 uint64_t rfork_size;
1135 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1136 attr_data->attr_data.aapl.rfork_size = rfork_size;
1143 if (config->readdir_attr_finder_info) {
1144 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1146 status = NT_STATUS_INTERNAL_ERROR;
1153 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1158 if (psd->dacl == NULL) {
1159 return NT_STATUS_OK;
1162 for (i = 0; i < psd->dacl->num_aces; i++) {
1163 /* MS NFS style mode/uid/gid */
1164 int cmp = dom_sid_compare_domain(
1165 &global_sid_Unix_NFS,
1166 &psd->dacl->aces[i].trustee);
1168 /* Normal ACE entry. */
1173 * security_descriptor_dacl_del()
1174 * *must* return NT_STATUS_OK as we know
1175 * we have something to remove.
1178 status = security_descriptor_dacl_del(psd,
1179 &psd->dacl->aces[i].trustee);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1187 * security_descriptor_dacl_del() may delete more
1188 * then one entry subsequent to this one if the
1189 * SID matches, but we only need to ensure that
1190 * we stay looking at the same element in the array.
1194 return NT_STATUS_OK;
1197 /* Search MS NFS style ACE with UNIX mode */
1198 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1200 struct security_descriptor *psd,
1205 struct fruit_config_data *config = NULL;
1209 SMB_VFS_HANDLE_GET_DATA(handle, config,
1210 struct fruit_config_data,
1211 return NT_STATUS_UNSUCCESSFUL);
1213 if (!global_fruit_config.nego_aapl) {
1214 return NT_STATUS_OK;
1216 if (psd->dacl == NULL || !config->unix_info_enabled) {
1217 return NT_STATUS_OK;
1220 for (i = 0; i < psd->dacl->num_aces; i++) {
1221 if (dom_sid_compare_domain(
1222 &global_sid_Unix_NFS_Mode,
1223 &psd->dacl->aces[i].trustee) == 0) {
1224 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1225 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1228 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1229 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1235 * Remove any incoming virtual ACE entries generated by
1236 * fruit_fget_nt_acl().
1239 return remove_virtual_nfs_aces(psd);
1242 /****************************************************************************
1244 ****************************************************************************/
1246 static int fruit_connect(vfs_handle_struct *handle,
1247 const char *service,
1251 char *list = NULL, *newlist = NULL;
1252 struct fruit_config_data *config;
1253 const struct loadparm_substitution *lp_sub =
1254 loadparm_s3_global_substitution();
1256 DEBUG(10, ("fruit_connect\n"));
1258 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1263 rc = init_fruit_config(handle);
1268 SMB_VFS_HANDLE_GET_DATA(handle, config,
1269 struct fruit_config_data, return -1);
1271 if (config->veto_appledouble) {
1272 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1275 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1276 newlist = talloc_asprintf(
1278 "%s/" ADOUBLE_NAME_PREFIX "*/",
1280 lp_do_parameter(SNUM(handle->conn),
1285 lp_do_parameter(SNUM(handle->conn),
1287 "/" ADOUBLE_NAME_PREFIX "*/");
1293 if (config->encoding == FRUIT_ENC_NATIVE) {
1294 lp_do_parameter(SNUM(handle->conn),
1296 macos_string_replace_map);
1299 if (config->time_machine) {
1300 DBG_NOTICE("Enabling durable handles for Time Machine "
1301 "support on [%s]\n", service);
1302 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1303 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1304 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1305 if (!lp_strict_sync(SNUM(handle->conn))) {
1306 DBG_WARNING("Time Machine without strict sync is not "
1309 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1315 static int fruit_fake_fd(void)
1322 * Return a valid fd, but ensure any attempt to use it returns
1323 * an error (EPIPE). Once we get a write on the handle, we open
1326 ret = pipe(pipe_fds);
1336 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1337 struct smb_filename *smb_fname,
1342 struct fruit_config_data *config = NULL;
1343 struct fio *fio = NULL;
1344 int open_flags = flags & ~O_CREAT;
1347 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1349 SMB_VFS_HANDLE_GET_DATA(handle, config,
1350 struct fruit_config_data, return -1);
1352 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1353 fio->type = ADOUBLE_META;
1354 fio->config = config;
1356 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, open_flags, mode);
1361 if (!(flags & O_CREAT)) {
1362 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1366 fd = fruit_fake_fd();
1368 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1372 fio->fake_fd = true;
1379 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1380 struct smb_filename *smb_fname,
1385 struct fruit_config_data *config = NULL;
1386 struct fio *fio = NULL;
1387 struct adouble *ad = NULL;
1388 bool meta_exists = false;
1391 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1393 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1400 if (!meta_exists && !(flags & O_CREAT)) {
1405 fd = fruit_fake_fd();
1410 SMB_VFS_HANDLE_GET_DATA(handle, config,
1411 struct fruit_config_data, return -1);
1413 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1414 fio->type = ADOUBLE_META;
1415 fio->config = config;
1416 fio->fake_fd = true;
1423 static int fruit_open_meta(vfs_handle_struct *handle,
1424 struct smb_filename *smb_fname,
1425 files_struct *fsp, int flags, mode_t mode)
1428 struct fruit_config_data *config = NULL;
1430 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1432 SMB_VFS_HANDLE_GET_DATA(handle, config,
1433 struct fruit_config_data, return -1);
1435 switch (config->meta) {
1436 case FRUIT_META_STREAM:
1437 fd = fruit_open_meta_stream(handle, smb_fname,
1441 case FRUIT_META_NETATALK:
1442 fd = fruit_open_meta_netatalk(handle, smb_fname,
1447 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1451 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1456 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1457 struct smb_filename *smb_fname,
1463 struct adouble *ad = NULL;
1464 struct smb_filename *smb_fname_base = NULL;
1465 struct fruit_config_data *config = NULL;
1468 SMB_VFS_HANDLE_GET_DATA(handle, config,
1469 struct fruit_config_data, return -1);
1471 if ((!(flags & O_CREAT)) &&
1472 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1474 /* sorry, but directories don't habe a resource fork */
1479 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
1484 /* We always need read/write access for the metadata header too */
1485 flags &= ~(O_RDONLY | O_WRONLY);
1488 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
1495 if (flags & (O_CREAT | O_TRUNC)) {
1496 ad = ad_init(fsp, ADOUBLE_RSRC);
1502 fsp->fh->fd = hostfd;
1504 rc = ad_fset(handle, ad, fsp);
1515 TALLOC_FREE(smb_fname_base);
1517 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
1519 int saved_errno = errno;
1522 * BUGBUGBUG -- we would need to call
1523 * fd_close_posix here, but we don't have a
1526 fsp->fh->fd = hostfd;
1530 errno = saved_errno;
1535 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1536 struct smb_filename *smb_fname,
1541 #ifdef HAVE_ATTROPEN
1544 fd = attropen(smb_fname->base_name,
1545 AFPRESOURCE_EA_NETATALK,
1560 static int fruit_open_rsrc(vfs_handle_struct *handle,
1561 struct smb_filename *smb_fname,
1562 files_struct *fsp, int flags, mode_t mode)
1565 struct fruit_config_data *config = NULL;
1566 struct fio *fio = NULL;
1568 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1570 SMB_VFS_HANDLE_GET_DATA(handle, config,
1571 struct fruit_config_data, return -1);
1573 switch (config->rsrc) {
1574 case FRUIT_RSRC_STREAM:
1575 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1578 case FRUIT_RSRC_ADFILE:
1579 fd = fruit_open_rsrc_adouble(handle, smb_fname,
1583 case FRUIT_RSRC_XATTR:
1584 fd = fruit_open_rsrc_xattr(handle, smb_fname,
1589 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1593 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1599 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1600 fio->type = ADOUBLE_RSRC;
1601 fio->config = config;
1606 static int fruit_open(vfs_handle_struct *handle,
1607 struct smb_filename *smb_fname,
1608 files_struct *fsp, int flags, mode_t mode)
1612 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1614 if (!is_named_stream(smb_fname)) {
1615 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1618 if (is_afpinfo_stream(smb_fname->stream_name)) {
1619 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
1620 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1621 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
1623 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1626 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1631 static int fruit_close_meta(vfs_handle_struct *handle,
1635 struct fruit_config_data *config = NULL;
1637 SMB_VFS_HANDLE_GET_DATA(handle, config,
1638 struct fruit_config_data, return -1);
1640 switch (config->meta) {
1641 case FRUIT_META_STREAM:
1642 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1645 case FRUIT_META_NETATALK:
1646 ret = close(fsp->fh->fd);
1651 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1659 static int fruit_close_rsrc(vfs_handle_struct *handle,
1663 struct fruit_config_data *config = NULL;
1665 SMB_VFS_HANDLE_GET_DATA(handle, config,
1666 struct fruit_config_data, return -1);
1668 switch (config->rsrc) {
1669 case FRUIT_RSRC_STREAM:
1670 case FRUIT_RSRC_ADFILE:
1671 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1674 case FRUIT_RSRC_XATTR:
1675 ret = close(fsp->fh->fd);
1680 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1687 static int fruit_close(vfs_handle_struct *handle,
1695 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1697 if (!is_named_stream(fsp->fsp_name)) {
1698 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1701 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1702 ret = fruit_close_meta(handle, fsp);
1703 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1704 ret = fruit_close_rsrc(handle, fsp);
1706 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1712 static int fruit_renameat(struct vfs_handle_struct *handle,
1713 files_struct *srcfsp,
1714 const struct smb_filename *smb_fname_src,
1715 files_struct *dstfsp,
1716 const struct smb_filename *smb_fname_dst)
1719 struct fruit_config_data *config = NULL;
1720 struct smb_filename *src_adp_smb_fname = NULL;
1721 struct smb_filename *dst_adp_smb_fname = NULL;
1723 SMB_VFS_HANDLE_GET_DATA(handle, config,
1724 struct fruit_config_data, return -1);
1726 if (!VALID_STAT(smb_fname_src->st)) {
1727 DBG_ERR("Need valid stat for [%s]\n",
1728 smb_fname_str_dbg(smb_fname_src));
1732 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1741 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1742 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1747 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1752 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1757 DBG_DEBUG("%s -> %s\n",
1758 smb_fname_str_dbg(src_adp_smb_fname),
1759 smb_fname_str_dbg(dst_adp_smb_fname));
1761 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1766 if (errno == ENOENT) {
1771 TALLOC_FREE(src_adp_smb_fname);
1772 TALLOC_FREE(dst_adp_smb_fname);
1776 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1777 struct files_struct *dirfsp,
1778 const struct smb_filename *smb_fname)
1780 return SMB_VFS_NEXT_UNLINKAT(handle,
1786 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1787 const struct smb_filename *smb_fname)
1789 return SMB_VFS_REMOVEXATTR(handle->conn,
1791 AFPINFO_EA_NETATALK);
1794 static int fruit_unlink_meta(vfs_handle_struct *handle,
1795 struct files_struct *dirfsp,
1796 const struct smb_filename *smb_fname)
1798 struct fruit_config_data *config = NULL;
1801 SMB_VFS_HANDLE_GET_DATA(handle, config,
1802 struct fruit_config_data, return -1);
1804 switch (config->meta) {
1805 case FRUIT_META_STREAM:
1806 rc = fruit_unlink_meta_stream(handle,
1811 case FRUIT_META_NETATALK:
1812 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1816 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
1823 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
1824 struct files_struct *dirfsp,
1825 const struct smb_filename *smb_fname,
1830 if (!force_unlink) {
1831 struct smb_filename *smb_fname_cp = NULL;
1834 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
1835 if (smb_fname_cp == NULL) {
1840 * 0 byte resource fork streams are not listed by
1841 * vfs_streaminfo, as a result stream cleanup/deletion of file
1842 * deletion doesn't remove the resourcefork stream.
1845 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
1847 TALLOC_FREE(smb_fname_cp);
1848 DBG_ERR("stat [%s] failed [%s]\n",
1849 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
1853 size = smb_fname_cp->st.st_ex_size;
1854 TALLOC_FREE(smb_fname_cp);
1857 /* OS X ignores resource fork stream delete requests */
1862 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1866 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
1873 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
1874 struct files_struct *dirfsp,
1875 const struct smb_filename *smb_fname,
1879 struct adouble *ad = NULL;
1880 struct smb_filename *adp_smb_fname = NULL;
1882 if (!force_unlink) {
1883 ad = ad_get(talloc_tos(), handle, smb_fname,
1892 * 0 byte resource fork streams are not listed by
1893 * vfs_streaminfo, as a result stream cleanup/deletion of file
1894 * deletion doesn't remove the resourcefork stream.
1897 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1898 /* OS X ignores resource fork stream delete requests */
1906 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1911 rc = SMB_VFS_NEXT_UNLINKAT(handle,
1915 TALLOC_FREE(adp_smb_fname);
1916 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
1923 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
1924 const struct smb_filename *smb_fname,
1928 * OS X ignores resource fork stream delete requests, so nothing to do
1929 * here. Removing the file will remove the xattr anyway, so we don't
1930 * have to take care of removing 0 byte resource forks that could be
1936 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
1937 struct files_struct *dirfsp,
1938 const struct smb_filename *smb_fname,
1941 struct fruit_config_data *config = NULL;
1944 SMB_VFS_HANDLE_GET_DATA(handle, config,
1945 struct fruit_config_data, return -1);
1947 switch (config->rsrc) {
1948 case FRUIT_RSRC_STREAM:
1949 rc = fruit_unlink_rsrc_stream(handle,
1955 case FRUIT_RSRC_ADFILE:
1956 rc = fruit_unlink_rsrc_adouble(handle,
1962 case FRUIT_RSRC_XATTR:
1963 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
1967 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
1974 static int fruit_chmod(vfs_handle_struct *handle,
1975 const struct smb_filename *smb_fname,
1979 struct fruit_config_data *config = NULL;
1980 struct smb_filename *smb_fname_adp = NULL;
1982 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1987 SMB_VFS_HANDLE_GET_DATA(handle, config,
1988 struct fruit_config_data, return -1);
1990 if (config->rsrc != FRUIT_RSRC_ADFILE) {
1994 if (!VALID_STAT(smb_fname->st)) {
1998 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2002 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2007 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
2009 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
2010 if (errno == ENOENT) {
2014 TALLOC_FREE(smb_fname_adp);
2018 static int fruit_unlinkat(vfs_handle_struct *handle,
2019 struct files_struct *dirfsp,
2020 const struct smb_filename *smb_fname,
2023 struct fruit_config_data *config = NULL;
2024 struct smb_filename *rsrc_smb_fname = NULL;
2027 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
2029 if (flags & AT_REMOVEDIR) {
2030 return SMB_VFS_NEXT_UNLINKAT(handle,
2036 SMB_VFS_HANDLE_GET_DATA(handle, config,
2037 struct fruit_config_data, return -1);
2039 if (is_afpinfo_stream(smb_fname->stream_name)) {
2040 return fruit_unlink_meta(handle,
2043 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2044 return fruit_unlink_rsrc(handle,
2048 } else if (is_named_stream(smb_fname)) {
2049 return SMB_VFS_NEXT_UNLINKAT(handle,
2053 } else if (is_adouble_file(smb_fname->base_name)) {
2054 return SMB_VFS_NEXT_UNLINKAT(handle,
2061 * A request to delete the base file. Because 0 byte resource
2062 * fork streams are not listed by fruit_streaminfo,
2063 * delete_all_streams() can't remove 0 byte resource fork
2064 * streams, so we have to cleanup this here.
2066 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2067 smb_fname->base_name,
2068 AFPRESOURCE_STREAM_NAME,
2072 if (rsrc_smb_fname == NULL) {
2076 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2077 if ((ret != 0) && (errno != ENOENT)) {
2078 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2079 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2080 TALLOC_FREE(rsrc_smb_fname);
2083 TALLOC_FREE(rsrc_smb_fname);
2085 return SMB_VFS_NEXT_UNLINKAT(handle,
2091 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2092 files_struct *fsp, void *data,
2093 size_t n, off_t offset)
2098 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2099 if (nread == -1 || nread == n) {
2103 DBG_ERR("Removing [%s] after short read [%zd]\n",
2104 fsp_str_dbg(fsp), nread);
2106 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2111 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2119 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2120 files_struct *fsp, void *data,
2121 size_t n, off_t offset)
2124 struct adouble *ad = NULL;
2125 char afpinfo_buf[AFP_INFO_SIZE];
2129 ai = afpinfo_new(talloc_tos());
2134 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2140 p = ad_get_entry(ad, ADEID_FINDERI);
2142 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2147 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2149 nread = afpinfo_pack(ai, afpinfo_buf);
2150 if (nread != AFP_INFO_SIZE) {
2155 memcpy(data, afpinfo_buf, n);
2163 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2164 files_struct *fsp, void *data,
2165 size_t n, off_t offset)
2167 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2172 * OS X has a off-by-1 error in the offset calculation, so we're
2173 * bug compatible here. It won't hurt, as any relevant real
2174 * world read requests from the AFP_AfpInfo stream will be
2175 * offset=0 n=60. offset is ignored anyway, see below.
2177 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2182 DBG_ERR("Failed to fetch fsp extension");
2186 /* Yes, macOS always reads from offset 0 */
2188 to_return = MIN(n, AFP_INFO_SIZE);
2190 switch (fio->config->meta) {
2191 case FRUIT_META_STREAM:
2192 nread = fruit_pread_meta_stream(handle, fsp, data,
2196 case FRUIT_META_NETATALK:
2197 nread = fruit_pread_meta_adouble(handle, fsp, data,
2202 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2206 if (nread == -1 && fio->created) {
2208 char afpinfo_buf[AFP_INFO_SIZE];
2210 ai = afpinfo_new(talloc_tos());
2215 nread = afpinfo_pack(ai, afpinfo_buf);
2217 if (nread != AFP_INFO_SIZE) {
2221 memcpy(data, afpinfo_buf, to_return);
2228 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2229 files_struct *fsp, void *data,
2230 size_t n, off_t offset)
2232 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2235 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2236 files_struct *fsp, void *data,
2237 size_t n, off_t offset)
2239 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2242 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2243 files_struct *fsp, void *data,
2244 size_t n, off_t offset)
2246 struct adouble *ad = NULL;
2249 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2254 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
2255 offset + ad_getentryoff(ad, ADEID_RFORK));
2261 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2262 files_struct *fsp, void *data,
2263 size_t n, off_t offset)
2265 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2273 switch (fio->config->rsrc) {
2274 case FRUIT_RSRC_STREAM:
2275 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2278 case FRUIT_RSRC_ADFILE:
2279 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2282 case FRUIT_RSRC_XATTR:
2283 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2287 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2294 static ssize_t fruit_pread(vfs_handle_struct *handle,
2295 files_struct *fsp, void *data,
2296 size_t n, off_t offset)
2298 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2301 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2302 fsp_str_dbg(fsp), (intmax_t)offset, n);
2305 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2308 if (fio->type == ADOUBLE_META) {
2309 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2311 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2314 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2318 static bool fruit_must_handle_aio_stream(struct fio *fio)
2324 if (fio->type == ADOUBLE_META) {
2328 if ((fio->type == ADOUBLE_RSRC) &&
2329 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2337 struct fruit_pread_state {
2339 struct vfs_aio_state vfs_aio_state;
2342 static void fruit_pread_done(struct tevent_req *subreq);
2344 static struct tevent_req *fruit_pread_send(
2345 struct vfs_handle_struct *handle,
2346 TALLOC_CTX *mem_ctx,
2347 struct tevent_context *ev,
2348 struct files_struct *fsp,
2350 size_t n, off_t offset)
2352 struct tevent_req *req = NULL;
2353 struct tevent_req *subreq = NULL;
2354 struct fruit_pread_state *state = NULL;
2355 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2357 req = tevent_req_create(mem_ctx, &state,
2358 struct fruit_pread_state);
2363 if (fruit_must_handle_aio_stream(fio)) {
2364 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2365 if (state->nread != n) {
2366 if (state->nread != -1) {
2369 tevent_req_error(req, errno);
2370 return tevent_req_post(req, ev);
2372 tevent_req_done(req);
2373 return tevent_req_post(req, ev);
2376 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2378 if (tevent_req_nomem(req, subreq)) {
2379 return tevent_req_post(req, ev);
2381 tevent_req_set_callback(subreq, fruit_pread_done, req);
2385 static void fruit_pread_done(struct tevent_req *subreq)
2387 struct tevent_req *req = tevent_req_callback_data(
2388 subreq, struct tevent_req);
2389 struct fruit_pread_state *state = tevent_req_data(
2390 req, struct fruit_pread_state);
2392 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2393 TALLOC_FREE(subreq);
2395 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2398 tevent_req_done(req);
2401 static ssize_t fruit_pread_recv(struct tevent_req *req,
2402 struct vfs_aio_state *vfs_aio_state)
2404 struct fruit_pread_state *state = tevent_req_data(
2405 req, struct fruit_pread_state);
2407 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2411 *vfs_aio_state = state->vfs_aio_state;
2412 return state->nread;
2415 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2416 files_struct *fsp, const void *data,
2417 size_t n, off_t offset)
2419 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2425 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2426 fsp_str_dbg(fsp), (intmax_t)offset, n);
2435 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
2437 DBG_ERR("Close [%s] failed: %s\n",
2438 fsp_str_dbg(fsp), strerror(errno));
2443 fd = SMB_VFS_NEXT_OPEN(handle,
2449 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2450 fsp_str_dbg(fsp), strerror(errno));
2454 fio->fake_fd = false;
2457 ai = afpinfo_unpack(talloc_tos(), data);
2462 if (ai_empty_finderinfo(ai)) {
2464 * Writing an all 0 blob to the metadata stream results in the
2465 * stream being removed on a macOS server. This ensures we
2466 * behave the same and it verified by the "delete AFP_AfpInfo by
2467 * writing all 0" test.
2469 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2471 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2476 ok = set_delete_on_close(
2479 handle->conn->session_info->security_token,
2480 handle->conn->session_info->unix_token);
2482 DBG_ERR("set_delete_on_close on [%s] failed\n",
2489 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2490 if (nwritten != n) {
2497 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2498 files_struct *fsp, const void *data,
2499 size_t n, off_t offset)
2501 struct adouble *ad = NULL;
2507 ai = afpinfo_unpack(talloc_tos(), data);
2512 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2514 ad = ad_init(talloc_tos(), ADOUBLE_META);
2519 p = ad_get_entry(ad, ADEID_FINDERI);
2521 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2526 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2528 ret = ad_fset(handle, ad, fsp);
2530 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2537 if (!ai_empty_finderinfo(ai)) {
2542 * Writing an all 0 blob to the metadata stream results in the stream
2543 * being removed on a macOS server. This ensures we behave the same and
2544 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2547 ok = set_delete_on_close(
2550 handle->conn->session_info->security_token,
2551 handle->conn->session_info->unix_token);
2553 DBG_ERR("set_delete_on_close on [%s] failed\n",
2561 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2562 files_struct *fsp, const void *data,
2563 size_t n, off_t offset)
2565 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2567 uint8_t buf[AFP_INFO_SIZE];
2573 DBG_ERR("Failed to fetch fsp extension");
2582 if (offset != 0 && n < 60) {
2587 cmp = memcmp(data, "AFP", 3);
2593 if (n <= AFP_OFF_FinderInfo) {
2595 * Nothing to do here really, just return
2603 if (to_copy > AFP_INFO_SIZE) {
2604 to_copy = AFP_INFO_SIZE;
2606 memcpy(buf, data, to_copy);
2609 if (to_write != AFP_INFO_SIZE) {
2610 to_write = AFP_INFO_SIZE;
2613 switch (fio->config->meta) {
2614 case FRUIT_META_STREAM:
2615 nwritten = fruit_pwrite_meta_stream(handle,
2622 case FRUIT_META_NETATALK:
2623 nwritten = fruit_pwrite_meta_netatalk(handle,
2631 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2635 if (nwritten != to_write) {
2640 * Return the requested amount, verified against macOS SMB server
2645 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2646 files_struct *fsp, const void *data,
2647 size_t n, off_t offset)
2649 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2652 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2653 files_struct *fsp, const void *data,
2654 size_t n, off_t offset)
2656 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2659 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2660 files_struct *fsp, const void *data,
2661 size_t n, off_t offset)
2663 struct adouble *ad = NULL;
2667 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2669 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
2673 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2674 offset + ad_getentryoff(ad, ADEID_RFORK));
2675 if (nwritten != n) {
2676 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2677 fsp_str_dbg(fsp), nwritten, n);
2682 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2683 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2684 ret = ad_fset(handle, ad, fsp);
2686 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2696 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2697 files_struct *fsp, const void *data,
2698 size_t n, off_t offset)
2700 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2704 DBG_ERR("Failed to fetch fsp extension");
2708 switch (fio->config->rsrc) {
2709 case FRUIT_RSRC_STREAM:
2710 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2713 case FRUIT_RSRC_ADFILE:
2714 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2717 case FRUIT_RSRC_XATTR:
2718 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2722 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2729 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2730 files_struct *fsp, const void *data,
2731 size_t n, off_t offset)
2733 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2736 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2737 fsp_str_dbg(fsp), (intmax_t)offset, n);
2740 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2743 if (fio->type == ADOUBLE_META) {
2744 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2746 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2749 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2753 struct fruit_pwrite_state {
2755 struct vfs_aio_state vfs_aio_state;
2758 static void fruit_pwrite_done(struct tevent_req *subreq);
2760 static struct tevent_req *fruit_pwrite_send(
2761 struct vfs_handle_struct *handle,
2762 TALLOC_CTX *mem_ctx,
2763 struct tevent_context *ev,
2764 struct files_struct *fsp,
2766 size_t n, off_t offset)
2768 struct tevent_req *req = NULL;
2769 struct tevent_req *subreq = NULL;
2770 struct fruit_pwrite_state *state = NULL;
2771 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2773 req = tevent_req_create(mem_ctx, &state,
2774 struct fruit_pwrite_state);
2779 if (fruit_must_handle_aio_stream(fio)) {
2780 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
2781 if (state->nwritten != n) {
2782 if (state->nwritten != -1) {
2785 tevent_req_error(req, errno);
2786 return tevent_req_post(req, ev);
2788 tevent_req_done(req);
2789 return tevent_req_post(req, ev);
2792 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
2794 if (tevent_req_nomem(req, subreq)) {
2795 return tevent_req_post(req, ev);
2797 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
2801 static void fruit_pwrite_done(struct tevent_req *subreq)
2803 struct tevent_req *req = tevent_req_callback_data(
2804 subreq, struct tevent_req);
2805 struct fruit_pwrite_state *state = tevent_req_data(
2806 req, struct fruit_pwrite_state);
2808 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2809 TALLOC_FREE(subreq);
2811 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2814 tevent_req_done(req);
2817 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
2818 struct vfs_aio_state *vfs_aio_state)
2820 struct fruit_pwrite_state *state = tevent_req_data(
2821 req, struct fruit_pwrite_state);
2823 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2827 *vfs_aio_state = state->vfs_aio_state;
2828 return state->nwritten;
2832 * Helper to stat/lstat the base file of an smb_fname.
2834 static int fruit_stat_base(vfs_handle_struct *handle,
2835 struct smb_filename *smb_fname,
2838 char *tmp_stream_name;
2841 tmp_stream_name = smb_fname->stream_name;
2842 smb_fname->stream_name = NULL;
2844 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2846 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2848 smb_fname->stream_name = tmp_stream_name;
2850 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
2851 smb_fname->base_name,
2852 (uintmax_t)smb_fname->st.st_ex_dev,
2853 (uintmax_t)smb_fname->st.st_ex_ino);
2857 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
2858 struct smb_filename *smb_fname,
2864 ret = fruit_stat_base(handle, smb_fname, false);
2869 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
2872 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2874 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2877 smb_fname->st.st_ex_ino = ino;
2882 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
2883 struct smb_filename *smb_fname,
2886 struct adouble *ad = NULL;
2888 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2890 DBG_INFO("fruit_stat_meta %s: %s\n",
2891 smb_fname_str_dbg(smb_fname), strerror(errno));
2897 /* Populate the stat struct with info from the base file. */
2898 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2901 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2902 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
2903 smb_fname->stream_name);
2907 static int fruit_stat_meta(vfs_handle_struct *handle,
2908 struct smb_filename *smb_fname,
2911 struct fruit_config_data *config = NULL;
2914 SMB_VFS_HANDLE_GET_DATA(handle, config,
2915 struct fruit_config_data, return -1);
2917 switch (config->meta) {
2918 case FRUIT_META_STREAM:
2919 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
2922 case FRUIT_META_NETATALK:
2923 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
2927 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2934 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
2935 struct smb_filename *smb_fname,
2938 struct adouble *ad = NULL;
2941 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
2947 /* Populate the stat struct with info from the base file. */
2948 ret = fruit_stat_base(handle, smb_fname, follow_links);
2954 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2955 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
2956 smb_fname->stream_name);
2961 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
2962 struct smb_filename *smb_fname,
2968 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2970 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2976 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
2977 struct smb_filename *smb_fname,
2980 #ifdef HAVE_ATTROPEN
2984 /* Populate the stat struct with info from the base file. */
2985 ret = fruit_stat_base(handle, smb_fname, follow_links);
2990 fd = attropen(smb_fname->base_name,
2991 AFPRESOURCE_EA_NETATALK,
2997 ret = sys_fstat(fd, &smb_fname->st, false);
3000 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3001 AFPRESOURCE_EA_NETATALK);
3007 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3008 smb_fname->stream_name);
3018 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3019 struct smb_filename *smb_fname,
3022 struct fruit_config_data *config = NULL;
3025 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3027 SMB_VFS_HANDLE_GET_DATA(handle, config,
3028 struct fruit_config_data, return -1);
3030 switch (config->rsrc) {
3031 case FRUIT_RSRC_STREAM:
3032 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3035 case FRUIT_RSRC_XATTR:
3036 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3039 case FRUIT_RSRC_ADFILE:
3040 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3044 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3051 static int fruit_stat(vfs_handle_struct *handle,
3052 struct smb_filename *smb_fname)
3056 DEBUG(10, ("fruit_stat called for %s\n",
3057 smb_fname_str_dbg(smb_fname)));
3059 if (!is_named_stream(smb_fname)) {
3060 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3062 update_btime(handle, smb_fname);
3068 * Note if lp_posix_paths() is true, we can never
3069 * get here as is_ntfs_stream_smb_fname() is
3070 * always false. So we never need worry about
3071 * not following links here.
3074 if (is_afpinfo_stream(smb_fname->stream_name)) {
3075 rc = fruit_stat_meta(handle, smb_fname, true);
3076 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3077 rc = fruit_stat_rsrc(handle, smb_fname, true);
3079 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3083 update_btime(handle, smb_fname);
3084 smb_fname->st.st_ex_mode &= ~S_IFMT;
3085 smb_fname->st.st_ex_mode |= S_IFREG;
3086 smb_fname->st.st_ex_blocks =
3087 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3092 static int fruit_lstat(vfs_handle_struct *handle,
3093 struct smb_filename *smb_fname)
3097 DEBUG(10, ("fruit_lstat called for %s\n",
3098 smb_fname_str_dbg(smb_fname)));
3100 if (!is_named_stream(smb_fname)) {
3101 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3103 update_btime(handle, smb_fname);
3108 if (is_afpinfo_stream(smb_fname->stream_name)) {
3109 rc = fruit_stat_meta(handle, smb_fname, false);
3110 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3111 rc = fruit_stat_rsrc(handle, smb_fname, false);
3113 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3117 update_btime(handle, smb_fname);
3118 smb_fname->st.st_ex_mode &= ~S_IFMT;
3119 smb_fname->st.st_ex_mode |= S_IFREG;
3120 smb_fname->st.st_ex_blocks =
3121 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3126 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3128 SMB_STRUCT_STAT *sbuf)
3130 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3131 struct smb_filename smb_fname;
3140 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3145 *sbuf = fsp->base_fsp->fsp_name->st;
3146 sbuf->st_ex_size = AFP_INFO_SIZE;
3147 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3151 smb_fname = (struct smb_filename) {
3152 .base_name = fsp->fsp_name->base_name,
3155 ret = fruit_stat_base(handle, &smb_fname, false);
3159 *sbuf = smb_fname.st;
3161 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3163 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3168 sbuf->st_ex_ino = ino;
3172 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3174 SMB_STRUCT_STAT *sbuf)
3178 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3183 *sbuf = fsp->base_fsp->fsp_name->st;
3184 sbuf->st_ex_size = AFP_INFO_SIZE;
3185 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3190 static int fruit_fstat_meta(vfs_handle_struct *handle,
3192 SMB_STRUCT_STAT *sbuf,
3197 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3199 switch (fio->config->meta) {
3200 case FRUIT_META_STREAM:
3201 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3204 case FRUIT_META_NETATALK:
3205 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3209 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3213 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3217 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3219 SMB_STRUCT_STAT *sbuf)
3221 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3224 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3226 SMB_STRUCT_STAT *sbuf)
3228 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3231 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3233 SMB_STRUCT_STAT *sbuf)
3235 struct adouble *ad = NULL;
3238 /* Populate the stat struct with info from the base file. */
3239 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3244 ad = ad_get(talloc_tos(), handle,
3245 fsp->base_fsp->fsp_name,
3248 DBG_ERR("ad_get [%s] failed [%s]\n",
3249 fsp_str_dbg(fsp), strerror(errno));
3253 *sbuf = fsp->base_fsp->fsp_name->st;
3254 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3255 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3261 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3262 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3266 switch (fio->config->rsrc) {
3267 case FRUIT_RSRC_STREAM:
3268 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3271 case FRUIT_RSRC_ADFILE:
3272 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3275 case FRUIT_RSRC_XATTR:
3276 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3280 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3287 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3288 SMB_STRUCT_STAT *sbuf)
3290 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3294 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3297 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3299 if (fio->type == ADOUBLE_META) {
3300 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3302 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3306 sbuf->st_ex_mode &= ~S_IFMT;
3307 sbuf->st_ex_mode |= S_IFREG;
3308 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3311 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3312 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3316 static NTSTATUS delete_invalid_meta_stream(
3317 vfs_handle_struct *handle,
3318 const struct smb_filename *smb_fname,
3319 TALLOC_CTX *mem_ctx,
3320 unsigned int *pnum_streams,
3321 struct stream_struct **pstreams,
3324 struct smb_filename *sname = NULL;
3328 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3330 return NT_STATUS_INTERNAL_ERROR;
3334 return NT_STATUS_OK;
3337 sname = synthetic_smb_fname(talloc_tos(),
3338 smb_fname->base_name,
3339 AFPINFO_STREAM_NAME,
3343 if (sname == NULL) {
3344 return NT_STATUS_NO_MEMORY;
3347 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3348 handle->conn->cwd_fsp,
3353 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3354 return map_nt_error_from_unix(errno);
3357 return NT_STATUS_OK;
3360 static NTSTATUS fruit_streaminfo_meta_stream(
3361 vfs_handle_struct *handle,
3362 struct files_struct *fsp,
3363 const struct smb_filename *smb_fname,
3364 TALLOC_CTX *mem_ctx,
3365 unsigned int *pnum_streams,
3366 struct stream_struct **pstreams)
3368 struct stream_struct *stream = *pstreams;
3369 unsigned int num_streams = *pnum_streams;
3372 for (i = 0; i < num_streams; i++) {
3373 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3378 if (i == num_streams) {
3379 return NT_STATUS_OK;
3382 if (stream[i].size != AFP_INFO_SIZE) {
3383 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3384 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3386 return delete_invalid_meta_stream(handle,
3395 return NT_STATUS_OK;
3398 static NTSTATUS fruit_streaminfo_meta_netatalk(
3399 vfs_handle_struct *handle,
3400 struct files_struct *fsp,
3401 const struct smb_filename *smb_fname,
3402 TALLOC_CTX *mem_ctx,
3403 unsigned int *pnum_streams,
3404 struct stream_struct **pstreams)
3406 struct stream_struct *stream = *pstreams;
3407 unsigned int num_streams = *pnum_streams;
3408 struct adouble *ad = NULL;
3413 /* Remove the Netatalk xattr from the list */
3414 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3415 ":" NETATALK_META_XATTR ":$DATA");
3417 return NT_STATUS_NO_MEMORY;
3421 * Check if there's a AFPINFO_STREAM from the VFS streams
3422 * backend and if yes, remove it from the list
3424 for (i = 0; i < num_streams; i++) {
3425 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3430 if (i < num_streams) {
3431 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3432 smb_fname_str_dbg(smb_fname));
3434 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3437 return NT_STATUS_INTERNAL_ERROR;
3441 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3443 return NT_STATUS_OK;
3446 is_fi_empty = ad_empty_finderinfo(ad);
3450 return NT_STATUS_OK;
3453 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3454 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3455 smb_roundup(handle->conn, AFP_INFO_SIZE));
3457 return NT_STATUS_NO_MEMORY;
3460 return NT_STATUS_OK;
3463 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3464 struct files_struct *fsp,
3465 const struct smb_filename *smb_fname,
3466 TALLOC_CTX *mem_ctx,
3467 unsigned int *pnum_streams,
3468 struct stream_struct **pstreams)
3470 struct fruit_config_data *config = NULL;
3473 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3474 return NT_STATUS_INTERNAL_ERROR);
3476 switch (config->meta) {
3477 case FRUIT_META_NETATALK:
3478 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3479 mem_ctx, pnum_streams,
3483 case FRUIT_META_STREAM:
3484 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3485 mem_ctx, pnum_streams,
3490 return NT_STATUS_INTERNAL_ERROR;
3496 static NTSTATUS fruit_streaminfo_rsrc_stream(
3497 vfs_handle_struct *handle,
3498 struct files_struct *fsp,
3499 const struct smb_filename *smb_fname,
3500 TALLOC_CTX *mem_ctx,
3501 unsigned int *pnum_streams,
3502 struct stream_struct **pstreams)
3506 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3508 DBG_ERR("Filtering resource stream failed\n");
3509 return NT_STATUS_INTERNAL_ERROR;
3511 return NT_STATUS_OK;
3514 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3515 vfs_handle_struct *handle,
3516 struct files_struct *fsp,
3517 const struct smb_filename *smb_fname,
3518 TALLOC_CTX *mem_ctx,
3519 unsigned int *pnum_streams,
3520 struct stream_struct **pstreams)
3524 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3526 DBG_ERR("Filtering resource stream failed\n");
3527 return NT_STATUS_INTERNAL_ERROR;
3529 return NT_STATUS_OK;
3532 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3533 vfs_handle_struct *handle,
3534 struct files_struct *fsp,
3535 const struct smb_filename *smb_fname,
3536 TALLOC_CTX *mem_ctx,
3537 unsigned int *pnum_streams,
3538 struct stream_struct **pstreams)
3540 struct stream_struct *stream = *pstreams;
3541 unsigned int num_streams = *pnum_streams;
3542 struct adouble *ad = NULL;
3548 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3549 * and if yes, remove it from the list
3551 for (i = 0; i < num_streams; i++) {
3552 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3557 if (i < num_streams) {
3558 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3559 smb_fname_str_dbg(smb_fname));
3561 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3562 AFPRESOURCE_STREAM);
3564 return NT_STATUS_INTERNAL_ERROR;
3568 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3570 return NT_STATUS_OK;
3573 rlen = ad_getentrylen(ad, ADEID_RFORK);
3577 return NT_STATUS_OK;
3580 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3581 AFPRESOURCE_STREAM_NAME, rlen,
3582 smb_roundup(handle->conn, rlen));
3584 return NT_STATUS_NO_MEMORY;
3587 return NT_STATUS_OK;
3590 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3591 struct files_struct *fsp,
3592 const struct smb_filename *smb_fname,
3593 TALLOC_CTX *mem_ctx,
3594 unsigned int *pnum_streams,
3595 struct stream_struct **pstreams)
3597 struct fruit_config_data *config = NULL;
3600 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3601 return NT_STATUS_INTERNAL_ERROR);
3603 switch (config->rsrc) {
3604 case FRUIT_RSRC_STREAM:
3605 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3606 mem_ctx, pnum_streams,
3610 case FRUIT_RSRC_XATTR:
3611 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3612 mem_ctx, pnum_streams,
3616 case FRUIT_RSRC_ADFILE:
3617 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3618 mem_ctx, pnum_streams,
3623 return NT_STATUS_INTERNAL_ERROR;
3629 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3630 struct stream_struct **pstreams)
3632 unsigned num_streams = *pnum_streams;
3633 struct stream_struct *streams = *pstreams;
3636 if (!global_fruit_config.nego_aapl) {
3640 while (i < num_streams) {
3641 struct smb_filename smb_fname = (struct smb_filename) {
3642 .stream_name = streams[i].name,
3645 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3646 || streams[i].size > 0)
3652 streams[i] = streams[num_streams - 1];
3656 *pnum_streams = num_streams;
3659 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3660 struct files_struct *fsp,
3661 const struct smb_filename *smb_fname,
3662 TALLOC_CTX *mem_ctx,
3663 unsigned int *pnum_streams,
3664 struct stream_struct **pstreams)
3666 struct fruit_config_data *config = NULL;
3669 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3670 return NT_STATUS_UNSUCCESSFUL);
3672 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3674 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3675 pnum_streams, pstreams);
3676 if (!NT_STATUS_IS_OK(status)) {
3680 fruit_filter_empty_streams(pnum_streams, pstreams);
3682 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3683 mem_ctx, pnum_streams, pstreams);
3684 if (!NT_STATUS_IS_OK(status)) {
3688 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3689 mem_ctx, pnum_streams, pstreams);
3690 if (!NT_STATUS_IS_OK(status)) {
3694 return NT_STATUS_OK;
3697 static int fruit_ntimes(vfs_handle_struct *handle,
3698 const struct smb_filename *smb_fname,
3699 struct smb_file_time *ft)
3702 struct adouble *ad = NULL;
3703 struct fruit_config_data *config = NULL;
3705 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3708 if ((config->meta != FRUIT_META_NETATALK) ||
3709 is_omit_timespec(&ft->create_time))
3711 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3714 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3715 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3717 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3722 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3723 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3725 rc = ad_set(handle, ad, smb_fname);
3731 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3734 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3737 static int fruit_fallocate(struct vfs_handle_struct *handle,
3738 struct files_struct *fsp,
3743 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3746 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3749 /* Let the pwrite code path handle it. */
3754 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
3755 struct files_struct *fsp,
3758 #ifdef HAVE_ATTROPEN
3759 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3764 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
3765 struct files_struct *fsp,
3769 struct adouble *ad = NULL;
3772 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3774 DBG_DEBUG("ad_get [%s] failed [%s]\n",
3775 fsp_str_dbg(fsp), strerror(errno));
3779 ad_off = ad_getentryoff(ad, ADEID_RFORK);
3781 rc = ftruncate(fsp->fh->fd, offset + ad_off);
3787 ad_setentrylen(ad, ADEID_RFORK, offset);
3789 rc = ad_fset(handle, ad, fsp);
3791 DBG_ERR("ad_fset [%s] failed [%s]\n",
3792 fsp_str_dbg(fsp), strerror(errno));
3801 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
3802 struct files_struct *fsp,
3805 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3808 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3809 struct files_struct *fsp,
3812 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3816 DBG_ERR("Failed to fetch fsp extension");
3820 switch (fio->config->rsrc) {
3821 case FRUIT_RSRC_XATTR:
3822 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
3825 case FRUIT_RSRC_ADFILE:
3826 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
3829 case FRUIT_RSRC_STREAM:
3830 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
3834 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3842 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3843 struct files_struct *fsp,
3847 DBG_WARNING("ftruncate %s to %jd",
3848 fsp_str_dbg(fsp), (intmax_t)offset);
3849 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
3854 /* OS X returns success but does nothing */
3855 DBG_INFO("ignoring ftruncate %s to %jd\n",
3856 fsp_str_dbg(fsp), (intmax_t)offset);
3860 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3861 struct files_struct *fsp,
3864 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3867 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
3871 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3874 if (fio->type == ADOUBLE_META) {
3875 ret = fruit_ftruncate_meta(handle, fsp, offset);
3877 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
3880 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
3884 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3885 struct smb_request *req,
3886 uint16_t root_dir_fid,
3887 struct smb_filename *smb_fname,
3888 uint32_t access_mask,
3889 uint32_t share_access,
3890 uint32_t create_disposition,
3891 uint32_t create_options,
3892 uint32_t file_attributes,
3893 uint32_t oplock_request,
3894 const struct smb2_lease *lease,
3895 uint64_t allocation_size,
3896 uint32_t private_flags,
3897 struct security_descriptor *sd,
3898 struct ea_list *ea_list,
3899 files_struct **result,
3901 const struct smb2_create_blobs *in_context_blobs,
3902 struct smb2_create_blobs *out_context_blobs)
3905 struct fruit_config_data *config = NULL;
3906 files_struct *fsp = NULL;
3907 struct fio *fio = NULL;
3908 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
3911 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3912 if (!NT_STATUS_IS_OK(status)) {
3916 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3917 return NT_STATUS_UNSUCCESSFUL);
3919 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
3920 uint32_t conv_flags = 0;
3922 if (config->wipe_intentionally_left_blank_rfork) {
3923 conv_flags |= AD_CONV_WIPE_BLANK;
3925 if (config->delete_empty_adfiles) {
3926 conv_flags |= AD_CONV_DELETE;
3929 ret = ad_convert(handle,
3930 handle->conn->cwd_fsp,
3932 macos_string_replace_map,
3935 DBG_ERR("ad_convert() failed\n");
3936 return NT_STATUS_UNSUCCESSFUL;
3940 status = SMB_VFS_NEXT_CREATE_FILE(
3941 handle, req, root_dir_fid, smb_fname,
3942 access_mask, share_access,
3943 create_disposition, create_options,
3944 file_attributes, oplock_request,
3946 allocation_size, private_flags,
3947 sd, ea_list, result,
3948 pinfo, in_context_blobs, out_context_blobs);
3949 if (!NT_STATUS_IS_OK(status)) {
3955 if (global_fruit_config.nego_aapl) {
3956 if (config->posix_rename && fsp->fsp_flags.is_directory) {
3958 * Enable POSIX directory rename behaviour
3960 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
3965 * If this is a plain open for existing files, opening an 0
3966 * byte size resource fork MUST fail with
3967 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
3969 * Cf the vfs_fruit torture tests in test_rfork_create().
3971 if (global_fruit_config.nego_aapl &&
3972 create_disposition == FILE_OPEN &&
3973 smb_fname->st.st_ex_size == 0 &&
3974 is_named_stream(smb_fname))
3976 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3980 fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3981 if (fio != NULL && pinfo != NULL && *pinfo == FILE_WAS_CREATED) {
3982 fio->created = true;
3985 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
3989 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
3992 status = fruit_check_access(
3996 if (!NT_STATUS_IS_OK(status)) {
4004 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4007 close_file(req, fsp, ERROR_CLOSE);
4008 *result = fsp = NULL;
4014 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4015 const struct smb_filename *fname,
4016 TALLOC_CTX *mem_ctx,
4017 struct readdir_attr_data **pattr_data)
4019 struct fruit_config_data *config = NULL;
4020 struct readdir_attr_data *attr_data;
4021 uint32_t conv_flags = 0;
4025 SMB_VFS_HANDLE_GET_DATA(handle, config,
4026 struct fruit_config_data,
4027 return NT_STATUS_UNSUCCESSFUL);
4029 if (!global_fruit_config.nego_aapl) {
4030 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4033 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4035 if (config->wipe_intentionally_left_blank_rfork) {
4036 conv_flags |= AD_CONV_WIPE_BLANK;
4038 if (config->delete_empty_adfiles) {
4039 conv_flags |= AD_CONV_DELETE;
4042 ret = ad_convert(handle,
4043 handle->conn->cwd_fsp,
4045 macos_string_replace_map,
4048 DBG_ERR("ad_convert() failed\n");
4049 return NT_STATUS_UNSUCCESSFUL;
4052 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4053 if (*pattr_data == NULL) {
4054 return NT_STATUS_UNSUCCESSFUL;
4056 attr_data = *pattr_data;
4057 attr_data->type = RDATTR_AAPL;
4060 * Mac metadata: compressed FinderInfo, resource fork length
4063 status = readdir_attr_macmeta(handle, fname, attr_data);
4064 if (!NT_STATUS_IS_OK(status)) {
4066 * Error handling is tricky: if we return failure from
4067 * this function, the corresponding directory entry
4068 * will to be passed to the client, so we really just
4069 * want to error out on fatal errors.
4071 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4079 if (config->unix_info_enabled) {
4080 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4086 if (!config->readdir_attr_max_access) {
4087 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4089 status = smbd_calculate_access_mask(
4091 handle->conn->cwd_fsp,
4094 SEC_FLAG_MAXIMUM_ALLOWED,
4095 &attr_data->attr_data.aapl.max_access);
4096 if (!NT_STATUS_IS_OK(status)) {
4101 return NT_STATUS_OK;
4104 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4105 fname->base_name, nt_errstr(status)));
4106 TALLOC_FREE(*pattr_data);
4110 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4112 uint32_t security_info,
4113 TALLOC_CTX *mem_ctx,
4114 struct security_descriptor **ppdesc)
4117 struct security_ace ace;
4119 struct fruit_config_data *config;
4121 SMB_VFS_HANDLE_GET_DATA(handle, config,
4122 struct fruit_config_data,
4123 return NT_STATUS_UNSUCCESSFUL);
4125 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4127 if (!NT_STATUS_IS_OK(status)) {
4132 * Add MS NFS style ACEs with uid, gid and mode
4134 if (!global_fruit_config.nego_aapl) {
4135 return NT_STATUS_OK;
4137 if (!config->unix_info_enabled) {
4138 return NT_STATUS_OK;
4141 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4142 status = remove_virtual_nfs_aces(*ppdesc);
4143 if (!NT_STATUS_IS_OK(status)) {
4144 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4148 /* MS NFS style mode */
4149 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4150 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4151 status = security_descriptor_dacl_add(*ppdesc, &ace);
4152 if (!NT_STATUS_IS_OK(status)) {
4153 DEBUG(1,("failed to add MS NFS style ACE\n"));
4157 /* MS NFS style uid */
4158 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4159 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4160 status = security_descriptor_dacl_add(*ppdesc, &ace);
4161 if (!NT_STATUS_IS_OK(status)) {
4162 DEBUG(1,("failed to add MS NFS style ACE\n"));
4166 /* MS NFS style gid */
4167 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4168 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4169 status = security_descriptor_dacl_add(*ppdesc, &ace);
4170 if (!NT_STATUS_IS_OK(status)) {
4171 DEBUG(1,("failed to add MS NFS style ACE\n"));
4175 return NT_STATUS_OK;
4178 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4180 uint32_t security_info_sent,
4181 const struct security_descriptor *orig_psd)
4185 mode_t ms_nfs_mode = 0;
4187 struct security_descriptor *psd = NULL;
4188 uint32_t orig_num_aces = 0;
4190 if (orig_psd->dacl != NULL) {
4191 orig_num_aces = orig_psd->dacl->num_aces;
4194 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4196 return NT_STATUS_NO_MEMORY;
4199 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4201 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4202 if (!NT_STATUS_IS_OK(status)) {
4203 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4209 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4210 * sent/present flags correctly now we've removed them.
4213 if (orig_num_aces != 0) {
4215 * Are there any ACE's left ?
4217 if (psd->dacl->num_aces == 0) {
4218 /* No - clear the DACL sent/present flags. */
4219 security_info_sent &= ~SECINFO_DACL;
4220 psd->type &= ~SEC_DESC_DACL_PRESENT;
4224 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4225 if (!NT_STATUS_IS_OK(status)) {
4226 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4232 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4234 DBG_WARNING("%s, result: %d, %04o error %s\n",
4237 (unsigned)ms_nfs_mode,
4239 status = map_nt_error_from_unix(errno);
4246 return NT_STATUS_OK;
4249 static struct vfs_offload_ctx *fruit_offload_ctx;
4251 struct fruit_offload_read_state {
4252 struct vfs_handle_struct *handle;
4253 struct tevent_context *ev;
4259 static void fruit_offload_read_done(struct tevent_req *subreq);
4261 static struct tevent_req *fruit_offload_read_send(
4262 TALLOC_CTX *mem_ctx,
4263 struct tevent_context *ev,
4264 struct vfs_handle_struct *handle,
4271 struct tevent_req *req = NULL;
4272 struct tevent_req *subreq = NULL;
4273 struct fruit_offload_read_state *state = NULL;
4275 req = tevent_req_create(mem_ctx, &state,
4276 struct fruit_offload_read_state);
4280 *state = (struct fruit_offload_read_state) {
4287 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4288 fsctl, ttl, offset, to_copy);
4289 if (tevent_req_nomem(subreq, req)) {
4290 return tevent_req_post(req, ev);
4292 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4296 static void fruit_offload_read_done(struct tevent_req *subreq)
4298 struct tevent_req *req = tevent_req_callback_data(
4299 subreq, struct tevent_req);
4300 struct fruit_offload_read_state *state = tevent_req_data(
4301 req, struct fruit_offload_read_state);
4304 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4308 TALLOC_FREE(subreq);
4309 if (tevent_req_nterror(req, status)) {
4313 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4314 tevent_req_done(req);
4318 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4319 &fruit_offload_ctx);
4320 if (tevent_req_nterror(req, status)) {
4324 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4327 if (tevent_req_nterror(req, status)) {
4331 tevent_req_done(req);
4335 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4336 struct vfs_handle_struct *handle,
4337 TALLOC_CTX *mem_ctx,
4340 struct fruit_offload_read_state *state = tevent_req_data(
4341 req, struct fruit_offload_read_state);
4344 if (tevent_req_is_nterror(req, &status)) {
4345 tevent_req_received(req);
4349 token->length = state->token.length;
4350 token->data = talloc_move(mem_ctx, &state->token.data);
4352 tevent_req_received(req);
4353 return NT_STATUS_OK;
4356 struct fruit_offload_write_state {
4357 struct vfs_handle_struct *handle;
4359 struct files_struct *src_fsp;
4360 struct files_struct *dst_fsp;
4364 static void fruit_offload_write_done(struct tevent_req *subreq);
4365 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4366 TALLOC_CTX *mem_ctx,
4367 struct tevent_context *ev,
4370 off_t transfer_offset,
4371 struct files_struct *dest_fsp,
4375 struct tevent_req *req, *subreq;
4376 struct fruit_offload_write_state *state;
4378 struct fruit_config_data *config;
4379 off_t src_off = transfer_offset;
4380 files_struct *src_fsp = NULL;
4381 off_t to_copy = num;
4382 bool copyfile_enabled = false;
4384 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4385 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4387 SMB_VFS_HANDLE_GET_DATA(handle, config,
4388 struct fruit_config_data,
4391 req = tevent_req_create(mem_ctx, &state,
4392 struct fruit_offload_write_state);
4396 state->handle = handle;
4397 state->dst_fsp = dest_fsp;
4400 case FSCTL_SRV_COPYCHUNK:
4401 case FSCTL_SRV_COPYCHUNK_WRITE:
4402 copyfile_enabled = config->copyfile_enabled;
4409 * Check if this a OS X copyfile style copychunk request with
4410 * a requested chunk count of 0 that was translated to a
4411 * offload_write_send VFS call overloading the parameters src_off
4412 * = dest_off = num = 0.
4414 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4415 status = vfs_offload_token_db_fetch_fsp(
4416 fruit_offload_ctx, token, &src_fsp);
4417 if (tevent_req_nterror(req, status)) {
4418 return tevent_req_post(req, ev);
4420 state->src_fsp = src_fsp;
4422 status = vfs_stat_fsp(src_fsp);
4423 if (tevent_req_nterror(req, status)) {
4424 return tevent_req_post(req, ev);
4427 to_copy = src_fsp->fsp_name->st.st_ex_size;
4428 state->is_copyfile = true;
4431 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4440 if (tevent_req_nomem(subreq, req)) {
4441 return tevent_req_post(req, ev);
4444 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4448 static void fruit_offload_write_done(struct tevent_req *subreq)
4450 struct tevent_req *req = tevent_req_callback_data(
4451 subreq, struct tevent_req);
4452 struct fruit_offload_write_state *state = tevent_req_data(
4453 req, struct fruit_offload_write_state);
4455 unsigned int num_streams = 0;
4456 struct stream_struct *streams = NULL;
4458 struct smb_filename *src_fname_tmp = NULL;
4459 struct smb_filename *dst_fname_tmp = NULL;
4461 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4464 TALLOC_FREE(subreq);
4465 if (tevent_req_nterror(req, status)) {
4469 if (!state->is_copyfile) {
4470 tevent_req_done(req);
4475 * Now copy all remaining streams. We know the share supports
4476 * streams, because we're in vfs_fruit. We don't do this async
4477 * because streams are few and small.
4479 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4480 state->src_fsp->fsp_name,
4481 req, &num_streams, &streams);
4482 if (tevent_req_nterror(req, status)) {
4486 if (num_streams == 1) {
4487 /* There is always one stream, ::$DATA. */
4488 tevent_req_done(req);
4492 for (i = 0; i < num_streams; i++) {
4493 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4494 __func__, streams[i].name, (size_t)streams[i].size));
4496 src_fname_tmp = synthetic_smb_fname(
4498 state->src_fsp->fsp_name->base_name,
4501 state->src_fsp->fsp_name->twrp,
4502 state->src_fsp->fsp_name->flags);
4503 if (tevent_req_nomem(src_fname_tmp, req)) {
4507 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4508 TALLOC_FREE(src_fname_tmp);
4512 dst_fname_tmp = synthetic_smb_fname(
4514 state->dst_fsp->fsp_name->base_name,
4517 state->dst_fsp->fsp_name->twrp,
4518 state->dst_fsp->fsp_name->flags);
4519 if (tevent_req_nomem(dst_fname_tmp, req)) {
4520 TALLOC_FREE(src_fname_tmp);
4524 status = copy_file(req,
4525 state->handle->conn,
4528 OPENX_FILE_CREATE_IF_NOT_EXIST,
4530 if (!NT_STATUS_IS_OK(status)) {
4531 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4532 smb_fname_str_dbg(src_fname_tmp),
4533 smb_fname_str_dbg(dst_fname_tmp),
4534 nt_errstr(status)));
4535 TALLOC_FREE(src_fname_tmp);
4536 TALLOC_FREE(dst_fname_tmp);
4537 tevent_req_nterror(req, status);
4541 TALLOC_FREE(src_fname_tmp);
4542 TALLOC_FREE(dst_fname_tmp);
4545 TALLOC_FREE(streams);
4546 TALLOC_FREE(src_fname_tmp);
4547 TALLOC_FREE(dst_fname_tmp);
4548 tevent_req_done(req);
4551 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4552 struct tevent_req *req,
4555 struct fruit_offload_write_state *state = tevent_req_data(
4556 req, struct fruit_offload_write_state);
4559 if (tevent_req_is_nterror(req, &status)) {
4560 DEBUG(1, ("server side copy chunk failed: %s\n",
4561 nt_errstr(status)));
4563 tevent_req_received(req);
4567 *copied = state->copied;
4568 tevent_req_received(req);
4570 return NT_STATUS_OK;
4573 static char *fruit_get_bandsize_line(char **lines, int numlines)
4576 static bool re_initialized = false;
4580 if (!re_initialized) {
4581 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4585 re_initialized = true;
4588 for (i = 0; i < numlines; i++) {
4589 regmatch_t matches[1];
4591 ret = regexec(&re, lines[i], 1, matches, 0);
4594 * Check if the match was on the last line, sa we want
4595 * the subsequent line.
4597 if (i + 1 == numlines) {
4600 return lines[i + 1];
4602 if (ret != REG_NOMATCH) {
4610 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4613 static bool re_initialized = false;
4614 regmatch_t matches[2];
4619 if (!re_initialized) {
4622 "<integer>\\([[:digit:]]*\\)</integer>$",
4627 re_initialized = true;
4630 ret = regexec(&re, line, 2, matches, 0);
4632 DBG_ERR("regex failed [%s]\n", line);
4636 line[matches[1].rm_eo] = '\0';
4638 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4642 *_band_size = (size_t)band_size;
4647 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4648 * "band-size" key and value.
4650 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4654 #define INFO_PLIST_MAX_SIZE 64*1024
4656 struct smb_filename *smb_fname = NULL;
4657 files_struct *fsp = NULL;
4658 uint8_t *file_data = NULL;
4659 char **lines = NULL;
4660 char *band_size_line = NULL;
4661 size_t plist_file_size;
4668 plist = talloc_asprintf(talloc_tos(),
4670 handle->conn->connectpath,
4672 if (plist == NULL) {
4677 smb_fname = synthetic_smb_fname(talloc_tos(),
4683 if (smb_fname == NULL) {
4688 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4690 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4695 plist_file_size = smb_fname->st.st_ex_size;
4697 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4698 DBG_INFO("%s is too large, ignoring\n", plist);
4703 status = SMB_VFS_NEXT_CREATE_FILE(
4706 0, /* root_dir_fid */
4707 smb_fname, /* fname */
4708 FILE_GENERIC_READ, /* access_mask */
4709 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4710 FILE_OPEN, /* create_disposition */
4711 0, /* create_options */
4712 0, /* file_attributes */
4713 INTERNAL_OPEN_ONLY, /* oplock_request */
4715 0, /* allocation_size */
4716 0, /* private_flags */
4721 NULL, NULL); /* create context */
4722 if (!NT_STATUS_IS_OK(status)) {
4723 DBG_INFO("Opening [%s] failed [%s]\n",
4724 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4729 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
4730 if (file_data == NULL) {
4735 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4736 if (nread != plist_file_size) {
4737 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4738 fsp_str_dbg(fsp), nread, plist_file_size);
4744 status = close_file(NULL, fsp, NORMAL_CLOSE);
4746 if (!NT_STATUS_IS_OK(status)) {
4747 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4752 lines = file_lines_parse((char *)file_data,
4756 if (lines == NULL) {
4761 band_size_line = fruit_get_bandsize_line(lines, numlines);
4762 if (band_size_line == NULL) {
4763 DBG_ERR("Didn't find band-size key in [%s]\n",
4764 smb_fname_str_dbg(smb_fname));
4769 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
4771 DBG_ERR("fruit_get_bandsize_from_line failed\n");
4775 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
4779 status = close_file(NULL, fsp, NORMAL_CLOSE);
4780 if (!NT_STATUS_IS_OK(status)) {
4781 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4786 TALLOC_FREE(smb_fname);
4787 TALLOC_FREE(file_data);
4792 struct fruit_disk_free_state {
4796 static bool fruit_get_num_bands(vfs_handle_struct *handle,
4801 struct smb_filename *bands_dir = NULL;
4802 struct smb_Dir *dir_hnd = NULL;
4803 const char *dname = NULL;
4804 char *talloced = NULL;
4808 path = talloc_asprintf(talloc_tos(),
4810 handle->conn->connectpath,
4816 bands_dir = synthetic_smb_fname(talloc_tos(),
4823 if (bands_dir == NULL) {
4827 dir_hnd = OpenDir(talloc_tos(), handle->conn, bands_dir, NULL, 0);
4828 if (dir_hnd == NULL) {
4829 TALLOC_FREE(bands_dir);
4835 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
4838 if (ISDOT(dname) || ISDOTDOT(dname)) {
4843 TALLOC_FREE(dir_hnd);
4845 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
4847 TALLOC_FREE(bands_dir);
4853 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
4854 struct fruit_disk_free_state *state,
4859 size_t sparsebundle_strlen = strlen("sparsebundle");
4860 size_t bandsize = 0;
4864 p = strstr(name, "sparsebundle");
4869 if (p[sparsebundle_strlen] != '\0') {
4873 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
4875 ok = fruit_get_bandsize(handle, name, &bandsize);
4878 * Beware of race conditions: this may be an uninitialized
4879 * Info.plist that a client is just creating. We don't want let
4880 * this to trigger complete failure.
4882 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
4886 ok = fruit_get_num_bands(handle, name, &nbands);
4889 * Beware of race conditions: this may be a backup sparsebundle
4890 * in an early stage lacking a bands subdirectory. We don't want
4891 * let this to trigger complete failure.
4893 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
4898 * Arithmetic on 32-bit systems may cause overflow, depending on
4899 * size_t precision. First we check its unlikely, then we
4900 * force the precision into target off_t, then we check that
4901 * the total did not overflow either.
4903 if (bandsize > SIZE_MAX/nbands) {
4904 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
4908 tm_size = (off_t)bandsize * (off_t)nbands;
4910 if (state->total_size + tm_size < state->total_size) {
4911 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
4916 state->total_size += tm_size;
4918 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
4919 name, (intmax_t)tm_size, (intmax_t)state->total_size);
4925 * Calculate used size of a TimeMachine volume
4927 * This assumes that the volume is used only for TimeMachine.
4929 * - readdir(basedir of share), then
4930 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
4931 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
4932 * - count band files in "\1.sparsebundle/bands/"
4933 * - calculate used size of all bands: band_count * band_size
4935 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
4936 const struct smb_filename *smb_fname,
4941 struct fruit_config_data *config = NULL;
4942 struct fruit_disk_free_state state = {0};
4943 struct smb_Dir *dir_hnd = NULL;
4944 const char *dname = NULL;
4945 char *talloced = NULL;
4951 SMB_VFS_HANDLE_GET_DATA(handle, config,
4952 struct fruit_config_data,
4955 if (!config->time_machine ||
4956 config->time_machine_max_size == 0)
4958 return SMB_VFS_NEXT_DISK_FREE(handle,
4965 dir_hnd = OpenDir(talloc_tos(), handle->conn, smb_fname, NULL, 0);
4966 if (dir_hnd == NULL) {
4970 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
4973 ok = fruit_tmsize_do_dirent(handle, &state, dname);
4975 TALLOC_FREE(talloced);
4976 TALLOC_FREE(dir_hnd);
4979 TALLOC_FREE(talloced);
4982 TALLOC_FREE(dir_hnd);
4984 dsize = config->time_machine_max_size / 512;
4985 dfree = dsize - (state.total_size / 512);
4986 if (dfree > dsize) {
4996 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
4997 const SMB_STRUCT_STAT *psbuf)
4999 struct fruit_config_data *config = NULL;
5001 SMB_VFS_HANDLE_GET_DATA(handle, config,
5002 struct fruit_config_data,
5005 if (global_fruit_config.nego_aapl &&
5006 config->aapl_zero_file_id)
5011 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5014 static struct vfs_fn_pointers vfs_fruit_fns = {
5015 .connect_fn = fruit_connect,
5016 .disk_free_fn = fruit_disk_free,
5018 /* File operations */
5019 .chmod_fn = fruit_chmod,
5020 .unlinkat_fn = fruit_unlinkat,
5021 .renameat_fn = fruit_renameat,
5022 .open_fn = fruit_open,
5023 .close_fn = fruit_close,
5024 .pread_fn = fruit_pread,
5025 .pwrite_fn = fruit_pwrite,
5026 .pread_send_fn = fruit_pread_send,
5027 .pread_recv_fn = fruit_pread_recv,
5028 .pwrite_send_fn = fruit_pwrite_send,
5029 .pwrite_recv_fn = fruit_pwrite_recv,
5030 .stat_fn = fruit_stat,
5031 .lstat_fn = fruit_lstat,
5032 .fstat_fn = fruit_fstat,
5033 .streaminfo_fn = fruit_streaminfo,
5034 .ntimes_fn = fruit_ntimes,
5035 .ftruncate_fn = fruit_ftruncate,
5036 .fallocate_fn = fruit_fallocate,
5037 .create_file_fn = fruit_create_file,
5038 .readdir_attr_fn = fruit_readdir_attr,
5039 .offload_read_send_fn = fruit_offload_read_send,
5040 .offload_read_recv_fn = fruit_offload_read_recv,
5041 .offload_write_send_fn = fruit_offload_write_send,
5042 .offload_write_recv_fn = fruit_offload_write_recv,
5043 .fs_file_id_fn = fruit_fs_file_id,
5045 /* NT ACL operations */
5046 .fget_nt_acl_fn = fruit_fget_nt_acl,
5047 .fset_nt_acl_fn = fruit_fset_nt_acl,
5051 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5053 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5055 if (!NT_STATUS_IS_OK(ret)) {
5059 vfs_fruit_debug_level = debug_add_class("fruit");
5060 if (vfs_fruit_debug_level == -1) {
5061 vfs_fruit_debug_level = DBGC_VFS;
5062 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5065 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5066 "vfs_fruit_init","fruit",vfs_fruit_debug_level));