libcli/smb: Allow smb2cli_validate_negotiate_info_done() to ignore NT_STATUS_INVALID_...
[Samba.git] / source3 / modules / vfs_fruit.c
bloba3b823fc6afbf7537b8d483e56041cb53aa804d6
1 /*
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/>.
20 #include "includes.h"
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"
28 #include "messages.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
59 * share modes.
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
66 * blobs content.
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".
78 * Extended Attributes
79 * ===================
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
91 * TODO
92 * ====
94 * - log diagnostic if any needed VFS module is not loaded
95 * (eg with lp_vfs_objects())
96 * - add tests
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;
106 #undef DBGC_CLASS
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 */
124 bool use_copyfile;
125 bool readdir_attr_enabled;
126 bool unix_info_enabled;
127 bool copyfile_enabled;
128 bool veto_appledouble;
129 bool posix_rename;
130 bool aapl_zero_file_id;
131 const char *model;
132 bool time_machine;
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) */
153 { -1, NULL}
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 */
159 { -1, NULL}
162 static const struct enum_list fruit_locking[] = {
163 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
164 {FRUIT_LOCKING_NONE, "none"},
165 { -1, NULL}
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 */
171 { -1, NULL}
174 struct fio {
175 /* tcon config handle */
176 struct fruit_config_data *config;
178 /* Denote stream type, meta or rsrc */
179 adouble_type_t type;
182 * AFP_AfpInfo stream created, but not written yet, thus still a fake
183 * pipe fd. This is set to true in fruit_open_meta if there was no
184 * existing stream but the caller requested O_CREAT. It is later set to
185 * false when we get a write on the stream that then does open and
186 * create the stream.
188 bool fake_fd;
189 int flags;
190 int mode;
193 /*****************************************************************************
194 * Helper functions
195 *****************************************************************************/
198 * Initialize config struct from our smb.conf config parameters
200 static int init_fruit_config(vfs_handle_struct *handle)
202 struct fruit_config_data *config;
203 int enumval;
204 const char *tm_size_str = NULL;
206 config = talloc_zero(handle->conn, struct fruit_config_data);
207 if (!config) {
208 DEBUG(1, ("talloc_zero() failed\n"));
209 errno = ENOMEM;
210 return -1;
214 * Versions up to Samba 4.5.x had a spelling bug in the
215 * fruit:resource option calling lp_parm_enum with
216 * "res*s*ource" (ie two s).
218 * In Samba 4.6 we accept both the wrong and the correct
219 * spelling, in Samba 4.7 the bad spelling will be removed.
221 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
222 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
223 if (enumval == -1) {
224 DEBUG(1, ("value for %s: resource type unknown\n",
225 FRUIT_PARAM_TYPE_NAME));
226 return -1;
228 config->rsrc = (enum fruit_rsrc)enumval;
230 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
231 "resource", fruit_rsrc, enumval);
232 if (enumval == -1) {
233 DEBUG(1, ("value for %s: resource type unknown\n",
234 FRUIT_PARAM_TYPE_NAME));
235 return -1;
237 config->rsrc = (enum fruit_rsrc)enumval;
239 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
240 "metadata", fruit_meta, FRUIT_META_NETATALK);
241 if (enumval == -1) {
242 DEBUG(1, ("value for %s: metadata type unknown\n",
243 FRUIT_PARAM_TYPE_NAME));
244 return -1;
246 config->meta = (enum fruit_meta)enumval;
248 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
249 "locking", fruit_locking, FRUIT_LOCKING_NONE);
250 if (enumval == -1) {
251 DEBUG(1, ("value for %s: locking type unknown\n",
252 FRUIT_PARAM_TYPE_NAME));
253 return -1;
255 config->locking = (enum fruit_locking)enumval;
257 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
258 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
259 if (enumval == -1) {
260 DEBUG(1, ("value for %s: encoding type unknown\n",
261 FRUIT_PARAM_TYPE_NAME));
262 return -1;
264 config->encoding = (enum fruit_encoding)enumval;
266 if (config->rsrc == FRUIT_RSRC_ADFILE) {
267 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
268 FRUIT_PARAM_TYPE_NAME,
269 "veto_appledouble",
270 true);
273 config->use_aapl = lp_parm_bool(
274 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
276 config->time_machine = lp_parm_bool(
277 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
279 config->unix_info_enabled = lp_parm_bool(
280 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
282 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
283 "copyfile", false);
285 config->posix_rename = lp_parm_bool(
286 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
288 config->aapl_zero_file_id =
289 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
290 "zero_file_id", false);
292 config->readdir_attr_rsize = lp_parm_bool(
293 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
295 config->readdir_attr_finder_info = lp_parm_bool(
296 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
298 config->readdir_attr_max_access = lp_parm_bool(
299 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
301 config->model = lp_parm_const_string(
302 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
304 tm_size_str = lp_parm_const_string(
305 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
306 "time machine max size", NULL);
307 if (tm_size_str != NULL) {
308 config->time_machine_max_size = conv_str_size(tm_size_str);
311 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
312 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
313 "wipe_intentionally_left_blank_rfork", false);
315 config->delete_empty_adfiles = lp_parm_bool(
316 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
317 "delete_empty_adfiles", false);
319 SMB_VFS_HANDLE_SET_DATA(handle, config,
320 NULL, struct fruit_config_data,
321 return -1);
323 return 0;
326 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
327 struct stream_struct **streams,
328 const char *name, off_t size,
329 off_t alloc_size)
331 struct stream_struct *tmp;
333 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
334 (*num_streams)+1);
335 if (tmp == NULL) {
336 return false;
339 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
340 if (tmp[*num_streams].name == NULL) {
341 return false;
344 tmp[*num_streams].size = size;
345 tmp[*num_streams].alloc_size = alloc_size;
347 *streams = tmp;
348 *num_streams += 1;
349 return true;
352 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
353 struct stream_struct **streams)
355 struct stream_struct *tmp = *streams;
356 unsigned int i;
358 if (*num_streams == 0) {
359 return true;
362 for (i = 0; i < *num_streams; i++) {
363 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
364 break;
368 if (i == *num_streams) {
369 return true;
372 if (tmp[i].size > 0) {
373 return true;
376 TALLOC_FREE(tmp[i].name);
377 if (*num_streams - 1 > i) {
378 memmove(&tmp[i], &tmp[i+1],
379 (*num_streams - i - 1) * sizeof(struct stream_struct));
382 *num_streams -= 1;
383 return true;
386 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
387 struct stream_struct **streams,
388 const char *name)
390 struct stream_struct *tmp = *streams;
391 unsigned int i;
393 if (*num_streams == 0) {
394 return true;
397 for (i = 0; i < *num_streams; i++) {
398 if (strequal_m(tmp[i].name, name)) {
399 break;
403 if (i == *num_streams) {
404 return true;
407 TALLOC_FREE(tmp[i].name);
408 if (*num_streams - 1 > i) {
409 memmove(&tmp[i], &tmp[i+1],
410 (*num_streams - i - 1) * sizeof(struct stream_struct));
413 *num_streams -= 1;
414 return true;
417 static bool ad_empty_finderinfo(const struct adouble *ad)
419 int cmp;
420 char emptybuf[ADEDLEN_FINDERI] = {0};
421 char *fi = NULL;
423 fi = ad_get_entry(ad, ADEID_FINDERI);
424 if (fi == NULL) {
425 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
426 return false;
429 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
430 return (cmp == 0);
433 static bool ai_empty_finderinfo(const AfpInfo *ai)
435 int cmp;
436 char emptybuf[ADEDLEN_FINDERI] = {0};
438 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
439 return (cmp == 0);
443 * Update btime with btime from Netatalk
445 static void update_btime(vfs_handle_struct *handle,
446 struct smb_filename *smb_fname)
448 uint32_t t;
449 struct timespec creation_time = {0};
450 struct adouble *ad;
451 struct fruit_config_data *config = NULL;
453 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
454 return);
456 switch (config->meta) {
457 case FRUIT_META_STREAM:
458 return;
459 case FRUIT_META_NETATALK:
460 /* Handled below */
461 break;
462 default:
463 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
464 return;
467 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
468 if (ad == NULL) {
469 return;
471 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
472 TALLOC_FREE(ad);
473 return;
475 TALLOC_FREE(ad);
477 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
478 update_stat_ex_create_time(&smb_fname->st, creation_time);
480 return;
484 * Map an access mask to a Netatalk single byte byte range lock
486 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
487 uint32_t access_mask)
489 off_t offset;
491 switch (access_mask) {
492 case FILE_READ_DATA:
493 offset = AD_FILELOCK_OPEN_RD;
494 break;
496 case FILE_WRITE_DATA:
497 case FILE_APPEND_DATA:
498 offset = AD_FILELOCK_OPEN_WR;
499 break;
501 default:
502 offset = AD_FILELOCK_OPEN_NONE;
503 break;
506 if (fork_type == APPLE_FORK_RSRC) {
507 if (offset == AD_FILELOCK_OPEN_NONE) {
508 offset = AD_FILELOCK_RSRC_OPEN_NONE;
509 } else {
510 offset += 2;
514 return offset;
518 * Map a deny mode to a Netatalk brl
520 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
521 uint32_t deny_mode)
523 off_t offset = 0;
525 switch (deny_mode) {
526 case DENY_READ:
527 offset = AD_FILELOCK_DENY_RD;
528 break;
530 case DENY_WRITE:
531 offset = AD_FILELOCK_DENY_WR;
532 break;
534 default:
535 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
538 if (fork_type == APPLE_FORK_RSRC) {
539 offset += 2;
542 return offset;
546 * Call fcntl() with an exclusive F_GETLK request in order to
547 * determine if there's an existing shared lock
549 * @return true if the requested lock was found or any error occurred
550 * false if the lock was not found
552 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
554 bool result;
555 off_t offset = in_offset;
556 off_t len = 1;
557 int type = F_WRLCK;
558 pid_t pid = 0;
560 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
561 if (result == false) {
562 return true;
565 if (type != F_UNLCK) {
566 return true;
569 return false;
572 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
573 files_struct *fsp,
574 uint32_t access_mask,
575 uint32_t share_mode)
577 NTSTATUS status = NT_STATUS_OK;
578 off_t off;
579 bool share_for_read = (share_mode & FILE_SHARE_READ);
580 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
581 bool netatalk_already_open_for_reading = false;
582 bool netatalk_already_open_for_writing = false;
583 bool netatalk_already_open_with_deny_read = false;
584 bool netatalk_already_open_with_deny_write = false;
585 struct GUID req_guid = GUID_random();
587 /* FIXME: hardcoded data fork, add resource fork */
588 enum apple_fork fork_type = APPLE_FORK_DATA;
590 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
591 fsp_str_dbg(fsp),
592 access_mask & FILE_READ_DATA ? "READ" :"-",
593 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
594 share_mode);
596 if (fsp->fh->fd == -1) {
597 return NT_STATUS_OK;
600 /* Read NetATalk opens and deny modes on the file. */
601 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
602 access_to_netatalk_brl(fork_type,
603 FILE_READ_DATA));
605 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
606 denymode_to_netatalk_brl(fork_type,
607 DENY_READ));
609 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
610 access_to_netatalk_brl(fork_type,
611 FILE_WRITE_DATA));
613 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
614 denymode_to_netatalk_brl(fork_type,
615 DENY_WRITE));
617 /* If there are any conflicts - sharing violation. */
618 if ((access_mask & FILE_READ_DATA) &&
619 netatalk_already_open_with_deny_read) {
620 return NT_STATUS_SHARING_VIOLATION;
623 if (!share_for_read &&
624 netatalk_already_open_for_reading) {
625 return NT_STATUS_SHARING_VIOLATION;
628 if ((access_mask & FILE_WRITE_DATA) &&
629 netatalk_already_open_with_deny_write) {
630 return NT_STATUS_SHARING_VIOLATION;
633 if (!share_for_write &&
634 netatalk_already_open_for_writing) {
635 return NT_STATUS_SHARING_VIOLATION;
638 if (!(access_mask & FILE_READ_DATA)) {
640 * Nothing we can do here, we need read access
641 * to set locks.
643 return NT_STATUS_OK;
646 /* Set NetAtalk locks matching our access */
647 if (access_mask & FILE_READ_DATA) {
648 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
649 req_guid.time_hi_and_version = __LINE__;
650 status = do_lock(
651 fsp,
652 talloc_tos(),
653 &req_guid,
654 fsp->op->global->open_persistent_id,
656 off,
657 READ_LOCK,
658 POSIX_LOCK,
659 NULL,
660 NULL);
662 if (!NT_STATUS_IS_OK(status)) {
663 return status;
667 if (!share_for_read) {
668 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
669 req_guid.time_hi_and_version = __LINE__;
670 status = do_lock(
671 fsp,
672 talloc_tos(),
673 &req_guid,
674 fsp->op->global->open_persistent_id,
676 off,
677 READ_LOCK,
678 POSIX_LOCK,
679 NULL,
680 NULL);
682 if (!NT_STATUS_IS_OK(status)) {
683 return status;
687 if (access_mask & FILE_WRITE_DATA) {
688 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
689 req_guid.time_hi_and_version = __LINE__;
690 status = do_lock(
691 fsp,
692 talloc_tos(),
693 &req_guid,
694 fsp->op->global->open_persistent_id,
696 off,
697 READ_LOCK,
698 POSIX_LOCK,
699 NULL,
700 NULL);
702 if (!NT_STATUS_IS_OK(status)) {
703 return status;
707 if (!share_for_write) {
708 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
709 req_guid.time_hi_and_version = __LINE__;
710 status = do_lock(
711 fsp,
712 talloc_tos(),
713 &req_guid,
714 fsp->op->global->open_persistent_id,
716 off,
717 READ_LOCK,
718 POSIX_LOCK,
719 NULL,
720 NULL);
722 if (!NT_STATUS_IS_OK(status)) {
723 return status;
727 return NT_STATUS_OK;
730 static NTSTATUS check_aapl(vfs_handle_struct *handle,
731 struct smb_request *req,
732 const struct smb2_create_blobs *in_context_blobs,
733 struct smb2_create_blobs *out_context_blobs)
735 struct fruit_config_data *config;
736 NTSTATUS status;
737 struct smb2_create_blob *aapl = NULL;
738 uint32_t cmd;
739 bool ok;
740 uint8_t p[16];
741 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
742 uint64_t req_bitmap, client_caps;
743 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
744 smb_ucs2_t *model;
745 size_t modellen;
747 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
748 return NT_STATUS_UNSUCCESSFUL);
750 if (!config->use_aapl
751 || in_context_blobs == NULL
752 || out_context_blobs == NULL) {
753 return NT_STATUS_OK;
756 aapl = smb2_create_blob_find(in_context_blobs,
757 SMB2_CREATE_TAG_AAPL);
758 if (aapl == NULL) {
759 return NT_STATUS_OK;
762 if (aapl->data.length != 24) {
763 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
764 (uintmax_t)aapl->data.length));
765 return NT_STATUS_INVALID_PARAMETER;
768 cmd = IVAL(aapl->data.data, 0);
769 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
770 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
771 return NT_STATUS_INVALID_PARAMETER;
774 req_bitmap = BVAL(aapl->data.data, 8);
775 client_caps = BVAL(aapl->data.data, 16);
777 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
778 SIVAL(p, 4, 0);
779 SBVAL(p, 8, req_bitmap);
780 ok = data_blob_append(req, &blob, p, 16);
781 if (!ok) {
782 return NT_STATUS_UNSUCCESSFUL;
785 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
786 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
787 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
788 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
789 config->readdir_attr_enabled = true;
792 if (config->use_copyfile) {
793 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
794 config->copyfile_enabled = true;
798 * The client doesn't set the flag, so we can't check
799 * for it and just set it unconditionally
801 if (config->unix_info_enabled) {
802 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
805 SBVAL(p, 0, server_caps);
806 ok = data_blob_append(req, &blob, p, 8);
807 if (!ok) {
808 return NT_STATUS_UNSUCCESSFUL;
812 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
813 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
814 uint64_t caps = 0;
816 switch (val) {
817 case Auto:
818 break;
820 case True:
821 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
822 break;
824 default:
825 break;
828 if (config->time_machine) {
829 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
832 SBVAL(p, 0, caps);
834 ok = data_blob_append(req, &blob, p, 8);
835 if (!ok) {
836 return NT_STATUS_UNSUCCESSFUL;
840 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
841 ok = convert_string_talloc(req,
842 CH_UNIX, CH_UTF16LE,
843 config->model, strlen(config->model),
844 &model, &modellen);
845 if (!ok) {
846 return NT_STATUS_UNSUCCESSFUL;
849 SIVAL(p, 0, 0);
850 SIVAL(p + 4, 0, modellen);
851 ok = data_blob_append(req, &blob, p, 8);
852 if (!ok) {
853 talloc_free(model);
854 return NT_STATUS_UNSUCCESSFUL;
857 ok = data_blob_append(req, &blob, model, modellen);
858 talloc_free(model);
859 if (!ok) {
860 return NT_STATUS_UNSUCCESSFUL;
864 status = smb2_create_blob_add(out_context_blobs,
865 out_context_blobs,
866 SMB2_CREATE_TAG_AAPL,
867 blob);
868 if (NT_STATUS_IS_OK(status)) {
869 global_fruit_config.nego_aapl = true;
872 return status;
875 static bool readdir_attr_meta_finderi_stream(
876 struct vfs_handle_struct *handle,
877 const struct smb_filename *smb_fname,
878 AfpInfo *ai)
880 struct smb_filename *stream_name = NULL;
881 files_struct *fsp = NULL;
882 ssize_t nread;
883 NTSTATUS status;
884 int ret;
885 bool ok;
886 uint8_t buf[AFP_INFO_SIZE];
888 stream_name = synthetic_smb_fname(talloc_tos(),
889 smb_fname->base_name,
890 AFPINFO_STREAM_NAME,
891 NULL, smb_fname->flags);
892 if (stream_name == NULL) {
893 return false;
896 ret = SMB_VFS_STAT(handle->conn, stream_name);
897 if (ret != 0) {
898 return false;
901 status = SMB_VFS_CREATE_FILE(
902 handle->conn, /* conn */
903 NULL, /* req */
904 0, /* root_dir_fid */
905 stream_name, /* fname */
906 FILE_READ_DATA, /* access_mask */
907 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
908 FILE_SHARE_DELETE),
909 FILE_OPEN, /* create_disposition*/
910 0, /* create_options */
911 0, /* file_attributes */
912 INTERNAL_OPEN_ONLY, /* oplock_request */
913 NULL, /* lease */
914 0, /* allocation_size */
915 0, /* private_flags */
916 NULL, /* sd */
917 NULL, /* ea_list */
918 &fsp, /* result */
919 NULL, /* pinfo */
920 NULL, NULL); /* create context */
922 TALLOC_FREE(stream_name);
924 if (!NT_STATUS_IS_OK(status)) {
925 return false;
928 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
929 if (nread != AFP_INFO_SIZE) {
930 DBG_ERR("short read [%s] [%zd/%d]\n",
931 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
932 ok = false;
933 goto fail;
936 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
937 AFP_FinderSize);
939 ok = true;
941 fail:
942 if (fsp != NULL) {
943 close_file(NULL, fsp, NORMAL_CLOSE);
946 return ok;
949 static bool readdir_attr_meta_finderi_netatalk(
950 struct vfs_handle_struct *handle,
951 const struct smb_filename *smb_fname,
952 AfpInfo *ai)
954 struct adouble *ad = NULL;
955 char *p = NULL;
957 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
958 if (ad == NULL) {
959 return false;
962 p = ad_get_entry(ad, ADEID_FINDERI);
963 if (p == NULL) {
964 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
965 TALLOC_FREE(ad);
966 return false;
969 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
970 TALLOC_FREE(ad);
971 return true;
974 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
975 const struct smb_filename *smb_fname,
976 struct readdir_attr_data *attr_data)
978 struct fruit_config_data *config = NULL;
979 uint32_t date_added;
980 AfpInfo ai = {0};
981 bool ok;
983 SMB_VFS_HANDLE_GET_DATA(handle, config,
984 struct fruit_config_data,
985 return false);
987 switch (config->meta) {
988 case FRUIT_META_NETATALK:
989 ok = readdir_attr_meta_finderi_netatalk(
990 handle, smb_fname, &ai);
991 break;
993 case FRUIT_META_STREAM:
994 ok = readdir_attr_meta_finderi_stream(
995 handle, smb_fname, &ai);
996 break;
998 default:
999 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1000 return false;
1003 if (!ok) {
1004 /* Don't bother with errors, it's likely ENOENT */
1005 return true;
1008 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1009 /* finder_type */
1010 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1011 &ai.afpi_FinderInfo[0], 4);
1013 /* finder_creator */
1014 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1015 &ai.afpi_FinderInfo[4], 4);
1018 /* finder_flags */
1019 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1020 &ai.afpi_FinderInfo[8], 2);
1022 /* finder_ext_flags */
1023 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1024 &ai.afpi_FinderInfo[24], 2);
1026 /* creation date */
1027 date_added = convert_time_t_to_uint32_t(
1028 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1030 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1032 return true;
1035 static uint64_t readdir_attr_rfork_size_adouble(
1036 struct vfs_handle_struct *handle,
1037 const struct smb_filename *smb_fname)
1039 struct adouble *ad = NULL;
1040 uint64_t rfork_size;
1042 ad = ad_get(talloc_tos(), handle, smb_fname,
1043 ADOUBLE_RSRC);
1044 if (ad == NULL) {
1045 return 0;
1048 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1049 TALLOC_FREE(ad);
1051 return rfork_size;
1054 static uint64_t readdir_attr_rfork_size_stream(
1055 struct vfs_handle_struct *handle,
1056 const struct smb_filename *smb_fname)
1058 struct smb_filename *stream_name = NULL;
1059 int ret;
1060 uint64_t rfork_size;
1062 stream_name = synthetic_smb_fname(talloc_tos(),
1063 smb_fname->base_name,
1064 AFPRESOURCE_STREAM_NAME,
1065 NULL, 0);
1066 if (stream_name == NULL) {
1067 return 0;
1070 ret = SMB_VFS_STAT(handle->conn, stream_name);
1071 if (ret != 0) {
1072 TALLOC_FREE(stream_name);
1073 return 0;
1076 rfork_size = stream_name->st.st_ex_size;
1077 TALLOC_FREE(stream_name);
1079 return rfork_size;
1082 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1083 const struct smb_filename *smb_fname)
1085 struct fruit_config_data *config = NULL;
1086 uint64_t rfork_size;
1088 SMB_VFS_HANDLE_GET_DATA(handle, config,
1089 struct fruit_config_data,
1090 return 0);
1092 switch (config->rsrc) {
1093 case FRUIT_RSRC_ADFILE:
1094 rfork_size = readdir_attr_rfork_size_adouble(handle,
1095 smb_fname);
1096 break;
1098 case FRUIT_RSRC_XATTR:
1099 case FRUIT_RSRC_STREAM:
1100 rfork_size = readdir_attr_rfork_size_stream(handle,
1101 smb_fname);
1102 break;
1104 default:
1105 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1106 rfork_size = 0;
1107 break;
1110 return rfork_size;
1113 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1114 const struct smb_filename *smb_fname,
1115 struct readdir_attr_data *attr_data)
1117 NTSTATUS status = NT_STATUS_OK;
1118 struct fruit_config_data *config = NULL;
1119 bool ok;
1121 SMB_VFS_HANDLE_GET_DATA(handle, config,
1122 struct fruit_config_data,
1123 return NT_STATUS_UNSUCCESSFUL);
1126 /* Ensure we return a default value in the creation_date field */
1127 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1130 * Resource fork length
1133 if (config->readdir_attr_rsize) {
1134 uint64_t rfork_size;
1136 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1137 attr_data->attr_data.aapl.rfork_size = rfork_size;
1141 * FinderInfo
1144 if (config->readdir_attr_finder_info) {
1145 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1146 if (!ok) {
1147 status = NT_STATUS_INTERNAL_ERROR;
1151 return status;
1154 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1156 NTSTATUS status;
1157 uint32_t i;
1159 if (psd->dacl == NULL) {
1160 return NT_STATUS_OK;
1163 for (i = 0; i < psd->dacl->num_aces; i++) {
1164 /* MS NFS style mode/uid/gid */
1165 int cmp = dom_sid_compare_domain(
1166 &global_sid_Unix_NFS,
1167 &psd->dacl->aces[i].trustee);
1168 if (cmp != 0) {
1169 /* Normal ACE entry. */
1170 continue;
1174 * security_descriptor_dacl_del()
1175 * *must* return NT_STATUS_OK as we know
1176 * we have something to remove.
1179 status = security_descriptor_dacl_del(psd,
1180 &psd->dacl->aces[i].trustee);
1181 if (!NT_STATUS_IS_OK(status)) {
1182 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1183 nt_errstr(status));
1184 return status;
1188 * security_descriptor_dacl_del() may delete more
1189 * then one entry subsequent to this one if the
1190 * SID matches, but we only need to ensure that
1191 * we stay looking at the same element in the array.
1193 i--;
1195 return NT_STATUS_OK;
1198 /* Search MS NFS style ACE with UNIX mode */
1199 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1200 files_struct *fsp,
1201 struct security_descriptor *psd,
1202 mode_t *pmode,
1203 bool *pdo_chmod)
1205 uint32_t i;
1206 struct fruit_config_data *config = NULL;
1208 *pdo_chmod = false;
1210 SMB_VFS_HANDLE_GET_DATA(handle, config,
1211 struct fruit_config_data,
1212 return NT_STATUS_UNSUCCESSFUL);
1214 if (!global_fruit_config.nego_aapl) {
1215 return NT_STATUS_OK;
1217 if (psd->dacl == NULL || !config->unix_info_enabled) {
1218 return NT_STATUS_OK;
1221 for (i = 0; i < psd->dacl->num_aces; i++) {
1222 if (dom_sid_compare_domain(
1223 &global_sid_Unix_NFS_Mode,
1224 &psd->dacl->aces[i].trustee) == 0) {
1225 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1226 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1227 *pdo_chmod = true;
1229 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1230 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1231 break;
1236 * Remove any incoming virtual ACE entries generated by
1237 * fruit_fget_nt_acl().
1240 return remove_virtual_nfs_aces(psd);
1243 /****************************************************************************
1244 * VFS ops
1245 ****************************************************************************/
1247 static int fruit_connect(vfs_handle_struct *handle,
1248 const char *service,
1249 const char *user)
1251 int rc;
1252 char *list = NULL, *newlist = NULL;
1253 struct fruit_config_data *config;
1254 const struct loadparm_substitution *lp_sub =
1255 loadparm_s3_global_substitution();
1257 DEBUG(10, ("fruit_connect\n"));
1259 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1260 if (rc < 0) {
1261 return rc;
1264 rc = init_fruit_config(handle);
1265 if (rc != 0) {
1266 return rc;
1269 SMB_VFS_HANDLE_GET_DATA(handle, config,
1270 struct fruit_config_data, return -1);
1272 if (config->veto_appledouble) {
1273 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1275 if (list) {
1276 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1277 newlist = talloc_asprintf(
1278 list,
1279 "%s/" ADOUBLE_NAME_PREFIX "*/",
1280 list);
1281 lp_do_parameter(SNUM(handle->conn),
1282 "veto files",
1283 newlist);
1285 } else {
1286 lp_do_parameter(SNUM(handle->conn),
1287 "veto files",
1288 "/" ADOUBLE_NAME_PREFIX "*/");
1291 TALLOC_FREE(list);
1294 if (config->encoding == FRUIT_ENC_NATIVE) {
1295 lp_do_parameter(SNUM(handle->conn),
1296 "catia:mappings",
1297 macos_string_replace_map);
1300 if (config->time_machine) {
1301 DBG_NOTICE("Enabling durable handles for Time Machine "
1302 "support on [%s]\n", service);
1303 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1304 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1305 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1306 if (!lp_strict_sync(SNUM(handle->conn))) {
1307 DBG_WARNING("Time Machine without strict sync is not "
1308 "recommended!\n");
1310 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1313 return rc;
1316 static int fruit_fake_fd(void)
1318 int pipe_fds[2];
1319 int fd;
1320 int ret;
1323 * Return a valid fd, but ensure any attempt to use it returns
1324 * an error (EPIPE). Once we get a write on the handle, we open
1325 * the real fd.
1327 ret = pipe(pipe_fds);
1328 if (ret != 0) {
1329 return -1;
1331 fd = pipe_fds[0];
1332 close(pipe_fds[1]);
1334 return fd;
1337 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1338 struct smb_filename *smb_fname,
1339 files_struct *fsp,
1340 int flags,
1341 mode_t mode)
1343 struct fruit_config_data *config = NULL;
1344 struct fio *fio = NULL;
1345 int open_flags = flags & ~O_CREAT;
1346 int fd;
1348 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1350 SMB_VFS_HANDLE_GET_DATA(handle, config,
1351 struct fruit_config_data, return -1);
1353 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1354 fio->type = ADOUBLE_META;
1355 fio->config = config;
1357 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, open_flags, mode);
1358 if (fd != -1) {
1359 return fd;
1362 if (!(flags & O_CREAT)) {
1363 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1364 return -1;
1367 fd = fruit_fake_fd();
1368 if (fd == -1) {
1369 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1370 return -1;
1373 fio->fake_fd = true;
1374 fio->flags = flags;
1375 fio->mode = mode;
1377 return fd;
1380 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1381 struct smb_filename *smb_fname,
1382 files_struct *fsp,
1383 int flags,
1384 mode_t mode)
1386 struct fruit_config_data *config = NULL;
1387 struct fio *fio = NULL;
1388 struct adouble *ad = NULL;
1389 bool meta_exists = false;
1390 int fd;
1392 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1394 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1395 if (ad != NULL) {
1396 meta_exists = true;
1399 TALLOC_FREE(ad);
1401 if (!meta_exists && !(flags & O_CREAT)) {
1402 errno = ENOENT;
1403 return -1;
1406 fd = fruit_fake_fd();
1407 if (fd == -1) {
1408 return -1;
1411 SMB_VFS_HANDLE_GET_DATA(handle, config,
1412 struct fruit_config_data, return -1);
1414 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1415 fio->type = ADOUBLE_META;
1416 fio->config = config;
1417 fio->fake_fd = true;
1418 fio->flags = flags;
1419 fio->mode = mode;
1421 return fd;
1424 static int fruit_open_meta(vfs_handle_struct *handle,
1425 struct smb_filename *smb_fname,
1426 files_struct *fsp, int flags, mode_t mode)
1428 int fd;
1429 struct fruit_config_data *config = NULL;
1431 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1433 SMB_VFS_HANDLE_GET_DATA(handle, config,
1434 struct fruit_config_data, return -1);
1436 switch (config->meta) {
1437 case FRUIT_META_STREAM:
1438 fd = fruit_open_meta_stream(handle, smb_fname,
1439 fsp, flags, mode);
1440 break;
1442 case FRUIT_META_NETATALK:
1443 fd = fruit_open_meta_netatalk(handle, smb_fname,
1444 fsp, flags, mode);
1445 break;
1447 default:
1448 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1449 return -1;
1452 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1454 return fd;
1457 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1458 struct smb_filename *smb_fname,
1459 files_struct *fsp,
1460 int flags,
1461 mode_t mode)
1463 int rc = 0;
1464 struct adouble *ad = NULL;
1465 struct smb_filename *smb_fname_base = NULL;
1466 struct fruit_config_data *config = NULL;
1467 int hostfd = -1;
1469 SMB_VFS_HANDLE_GET_DATA(handle, config,
1470 struct fruit_config_data, return -1);
1472 if ((!(flags & O_CREAT)) &&
1473 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1475 /* sorry, but directories don't habe a resource fork */
1476 rc = -1;
1477 goto exit;
1480 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
1481 if (rc != 0) {
1482 goto exit;
1485 /* We always need read/write access for the metadata header too */
1486 flags &= ~(O_RDONLY | O_WRONLY);
1487 flags |= O_RDWR;
1489 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
1490 flags, mode);
1491 if (hostfd == -1) {
1492 rc = -1;
1493 goto exit;
1496 if (flags & (O_CREAT | O_TRUNC)) {
1497 ad = ad_init(fsp, ADOUBLE_RSRC);
1498 if (ad == NULL) {
1499 rc = -1;
1500 goto exit;
1503 fsp->fh->fd = hostfd;
1505 rc = ad_fset(handle, ad, fsp);
1506 fsp->fh->fd = -1;
1507 if (rc != 0) {
1508 rc = -1;
1509 goto exit;
1511 TALLOC_FREE(ad);
1514 exit:
1516 TALLOC_FREE(smb_fname_base);
1518 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
1519 if (rc != 0) {
1520 int saved_errno = errno;
1521 if (hostfd >= 0) {
1523 * BUGBUGBUG -- we would need to call
1524 * fd_close_posix here, but we don't have a
1525 * full fsp yet
1527 fsp->fh->fd = hostfd;
1528 SMB_VFS_CLOSE(fsp);
1530 hostfd = -1;
1531 errno = saved_errno;
1533 return hostfd;
1536 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1537 struct smb_filename *smb_fname,
1538 files_struct *fsp,
1539 int flags,
1540 mode_t mode)
1542 #ifdef HAVE_ATTROPEN
1543 int fd = -1;
1545 fd = attropen(smb_fname->base_name,
1546 AFPRESOURCE_EA_NETATALK,
1547 flags,
1548 mode);
1549 if (fd == -1) {
1550 return -1;
1553 return fd;
1555 #else
1556 errno = ENOSYS;
1557 return -1;
1558 #endif
1561 static int fruit_open_rsrc(vfs_handle_struct *handle,
1562 struct smb_filename *smb_fname,
1563 files_struct *fsp, int flags, mode_t mode)
1565 int fd;
1566 struct fruit_config_data *config = NULL;
1567 struct fio *fio = NULL;
1569 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1571 SMB_VFS_HANDLE_GET_DATA(handle, config,
1572 struct fruit_config_data, return -1);
1574 switch (config->rsrc) {
1575 case FRUIT_RSRC_STREAM:
1576 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1577 break;
1579 case FRUIT_RSRC_ADFILE:
1580 fd = fruit_open_rsrc_adouble(handle, smb_fname,
1581 fsp, flags, mode);
1582 break;
1584 case FRUIT_RSRC_XATTR:
1585 fd = fruit_open_rsrc_xattr(handle, smb_fname,
1586 fsp, flags, mode);
1587 break;
1589 default:
1590 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1591 return -1;
1594 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1596 if (fd == -1) {
1597 return -1;
1600 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1601 fio->type = ADOUBLE_RSRC;
1602 fio->config = config;
1604 return fd;
1607 static int fruit_open(vfs_handle_struct *handle,
1608 struct smb_filename *smb_fname,
1609 files_struct *fsp, int flags, mode_t mode)
1611 int fd;
1613 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1615 if (!is_named_stream(smb_fname)) {
1616 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1619 if (is_afpinfo_stream(smb_fname->stream_name)) {
1620 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
1621 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1622 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
1623 } else {
1624 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1627 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1629 return fd;
1632 static int fruit_close_meta(vfs_handle_struct *handle,
1633 files_struct *fsp)
1635 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1636 int ret;
1637 struct fruit_config_data *config = NULL;
1639 SMB_VFS_HANDLE_GET_DATA(handle, config,
1640 struct fruit_config_data, return -1);
1642 switch (config->meta) {
1643 case FRUIT_META_STREAM:
1644 if (fio->fake_fd) {
1645 ret = vfs_fake_fd_close(fsp->fh->fd);
1646 fsp->fh->fd = -1;
1647 } else {
1648 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1650 break;
1652 case FRUIT_META_NETATALK:
1653 ret = vfs_fake_fd_close(fsp->fh->fd);
1654 fsp->fh->fd = -1;
1655 break;
1657 default:
1658 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1659 return -1;
1662 return ret;
1666 static int fruit_close_rsrc(vfs_handle_struct *handle,
1667 files_struct *fsp)
1669 int ret;
1670 struct fruit_config_data *config = NULL;
1672 SMB_VFS_HANDLE_GET_DATA(handle, config,
1673 struct fruit_config_data, return -1);
1675 switch (config->rsrc) {
1676 case FRUIT_RSRC_STREAM:
1677 case FRUIT_RSRC_ADFILE:
1678 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1679 break;
1681 case FRUIT_RSRC_XATTR:
1682 ret = vfs_fake_fd_close(fsp->fh->fd);
1683 fsp->fh->fd = -1;
1684 break;
1686 default:
1687 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1688 return -1;
1691 return ret;
1694 static int fruit_close(vfs_handle_struct *handle,
1695 files_struct *fsp)
1697 int ret;
1698 int fd;
1700 fd = fsp->fh->fd;
1702 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1704 if (!is_named_stream(fsp->fsp_name)) {
1705 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1708 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1709 ret = fruit_close_meta(handle, fsp);
1710 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1711 ret = fruit_close_rsrc(handle, fsp);
1712 } else {
1713 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1716 return ret;
1719 static int fruit_renameat(struct vfs_handle_struct *handle,
1720 files_struct *srcfsp,
1721 const struct smb_filename *smb_fname_src,
1722 files_struct *dstfsp,
1723 const struct smb_filename *smb_fname_dst)
1725 int rc = -1;
1726 struct fruit_config_data *config = NULL;
1727 struct smb_filename *src_adp_smb_fname = NULL;
1728 struct smb_filename *dst_adp_smb_fname = NULL;
1730 SMB_VFS_HANDLE_GET_DATA(handle, config,
1731 struct fruit_config_data, return -1);
1733 if (!VALID_STAT(smb_fname_src->st)) {
1734 DBG_ERR("Need valid stat for [%s]\n",
1735 smb_fname_str_dbg(smb_fname_src));
1736 return -1;
1739 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1740 srcfsp,
1741 smb_fname_src,
1742 dstfsp,
1743 smb_fname_dst);
1744 if (rc != 0) {
1745 return -1;
1748 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1749 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1751 return 0;
1754 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1755 if (rc != 0) {
1756 goto done;
1759 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1760 if (rc != 0) {
1761 goto done;
1764 DBG_DEBUG("%s -> %s\n",
1765 smb_fname_str_dbg(src_adp_smb_fname),
1766 smb_fname_str_dbg(dst_adp_smb_fname));
1768 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1769 srcfsp,
1770 src_adp_smb_fname,
1771 dstfsp,
1772 dst_adp_smb_fname);
1773 if (errno == ENOENT) {
1774 rc = 0;
1777 done:
1778 TALLOC_FREE(src_adp_smb_fname);
1779 TALLOC_FREE(dst_adp_smb_fname);
1780 return rc;
1783 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1784 struct files_struct *dirfsp,
1785 const struct smb_filename *smb_fname)
1787 return SMB_VFS_NEXT_UNLINKAT(handle,
1788 dirfsp,
1789 smb_fname,
1793 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1794 const struct smb_filename *smb_fname)
1796 return SMB_VFS_REMOVEXATTR(handle->conn,
1797 smb_fname,
1798 AFPINFO_EA_NETATALK);
1801 static int fruit_unlink_meta(vfs_handle_struct *handle,
1802 struct files_struct *dirfsp,
1803 const struct smb_filename *smb_fname)
1805 struct fruit_config_data *config = NULL;
1806 int rc;
1808 SMB_VFS_HANDLE_GET_DATA(handle, config,
1809 struct fruit_config_data, return -1);
1811 switch (config->meta) {
1812 case FRUIT_META_STREAM:
1813 rc = fruit_unlink_meta_stream(handle,
1814 dirfsp,
1815 smb_fname);
1816 break;
1818 case FRUIT_META_NETATALK:
1819 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1820 break;
1822 default:
1823 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
1824 return -1;
1827 return rc;
1830 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
1831 struct files_struct *dirfsp,
1832 const struct smb_filename *smb_fname,
1833 bool force_unlink)
1835 int ret;
1837 if (!force_unlink) {
1838 struct smb_filename *smb_fname_cp = NULL;
1839 off_t size;
1841 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
1842 if (smb_fname_cp == NULL) {
1843 return -1;
1847 * 0 byte resource fork streams are not listed by
1848 * vfs_streaminfo, as a result stream cleanup/deletion of file
1849 * deletion doesn't remove the resourcefork stream.
1852 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
1853 if (ret != 0) {
1854 TALLOC_FREE(smb_fname_cp);
1855 DBG_ERR("stat [%s] failed [%s]\n",
1856 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
1857 return -1;
1860 size = smb_fname_cp->st.st_ex_size;
1861 TALLOC_FREE(smb_fname_cp);
1863 if (size > 0) {
1864 /* OS X ignores resource fork stream delete requests */
1865 return 0;
1869 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1870 dirfsp,
1871 smb_fname,
1873 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
1874 ret = 0;
1877 return ret;
1880 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
1881 struct files_struct *dirfsp,
1882 const struct smb_filename *smb_fname,
1883 bool force_unlink)
1885 int rc;
1886 struct adouble *ad = NULL;
1887 struct smb_filename *adp_smb_fname = NULL;
1889 if (!force_unlink) {
1890 ad = ad_get(talloc_tos(), handle, smb_fname,
1891 ADOUBLE_RSRC);
1892 if (ad == NULL) {
1893 errno = ENOENT;
1894 return -1;
1899 * 0 byte resource fork streams are not listed by
1900 * vfs_streaminfo, as a result stream cleanup/deletion of file
1901 * deletion doesn't remove the resourcefork stream.
1904 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1905 /* OS X ignores resource fork stream delete requests */
1906 TALLOC_FREE(ad);
1907 return 0;
1910 TALLOC_FREE(ad);
1913 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1914 if (rc != 0) {
1915 return -1;
1918 rc = SMB_VFS_NEXT_UNLINKAT(handle,
1919 dirfsp,
1920 adp_smb_fname,
1922 TALLOC_FREE(adp_smb_fname);
1923 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
1924 rc = 0;
1927 return rc;
1930 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
1931 const struct smb_filename *smb_fname,
1932 bool force_unlink)
1935 * OS X ignores resource fork stream delete requests, so nothing to do
1936 * here. Removing the file will remove the xattr anyway, so we don't
1937 * have to take care of removing 0 byte resource forks that could be
1938 * left behind.
1940 return 0;
1943 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
1944 struct files_struct *dirfsp,
1945 const struct smb_filename *smb_fname,
1946 bool force_unlink)
1948 struct fruit_config_data *config = NULL;
1949 int rc;
1951 SMB_VFS_HANDLE_GET_DATA(handle, config,
1952 struct fruit_config_data, return -1);
1954 switch (config->rsrc) {
1955 case FRUIT_RSRC_STREAM:
1956 rc = fruit_unlink_rsrc_stream(handle,
1957 dirfsp,
1958 smb_fname,
1959 force_unlink);
1960 break;
1962 case FRUIT_RSRC_ADFILE:
1963 rc = fruit_unlink_rsrc_adouble(handle,
1964 dirfsp,
1965 smb_fname,
1966 force_unlink);
1967 break;
1969 case FRUIT_RSRC_XATTR:
1970 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
1971 break;
1973 default:
1974 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
1975 return -1;
1978 return rc;
1981 static int fruit_unlink_internal(vfs_handle_struct *handle,
1982 struct files_struct *dirfsp,
1983 const struct smb_filename *smb_fname)
1985 int rc;
1986 struct fruit_config_data *config = NULL;
1987 struct smb_filename *rsrc_smb_fname = NULL;
1989 SMB_VFS_HANDLE_GET_DATA(handle, config,
1990 struct fruit_config_data, return -1);
1992 if (is_afpinfo_stream(smb_fname->stream_name)) {
1993 return fruit_unlink_meta(handle,
1994 dirfsp,
1995 smb_fname);
1996 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1997 return fruit_unlink_rsrc(handle,
1998 dirfsp,
1999 smb_fname,
2000 false);
2001 } else if (is_named_stream(smb_fname)) {
2002 return SMB_VFS_NEXT_UNLINKAT(handle,
2003 dirfsp,
2004 smb_fname,
2006 } else if (is_adouble_file(smb_fname->base_name)) {
2007 return SMB_VFS_NEXT_UNLINKAT(handle,
2008 dirfsp,
2009 smb_fname,
2014 * A request to delete the base file. Because 0 byte resource
2015 * fork streams are not listed by fruit_streaminfo,
2016 * delete_all_streams() can't remove 0 byte resource fork
2017 * streams, so we have to cleanup this here.
2019 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2020 smb_fname->base_name,
2021 AFPRESOURCE_STREAM_NAME,
2022 NULL,
2023 smb_fname->flags);
2024 if (rsrc_smb_fname == NULL) {
2025 return -1;
2028 rc = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2029 if ((rc != 0) && (errno != ENOENT)) {
2030 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2031 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2032 TALLOC_FREE(rsrc_smb_fname);
2033 return -1;
2035 TALLOC_FREE(rsrc_smb_fname);
2037 return SMB_VFS_NEXT_UNLINKAT(handle,
2038 dirfsp,
2039 smb_fname,
2043 static int fruit_chmod(vfs_handle_struct *handle,
2044 const struct smb_filename *smb_fname,
2045 mode_t mode)
2047 int rc = -1;
2048 struct fruit_config_data *config = NULL;
2049 struct smb_filename *smb_fname_adp = NULL;
2051 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
2052 if (rc != 0) {
2053 return rc;
2056 SMB_VFS_HANDLE_GET_DATA(handle, config,
2057 struct fruit_config_data, return -1);
2059 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2060 return 0;
2063 if (!VALID_STAT(smb_fname->st)) {
2064 return 0;
2067 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2068 return 0;
2071 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2072 if (rc != 0) {
2073 return -1;
2076 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
2078 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
2079 if (errno == ENOENT) {
2080 rc = 0;
2083 TALLOC_FREE(smb_fname_adp);
2084 return rc;
2087 static int fruit_rmdir_internal(struct vfs_handle_struct *handle,
2088 struct files_struct *dirfsp,
2089 const struct smb_filename *smb_fname)
2091 DIR *dh = NULL;
2092 struct dirent *de;
2093 struct fruit_config_data *config;
2095 SMB_VFS_HANDLE_GET_DATA(handle, config,
2096 struct fruit_config_data, return -1);
2098 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2099 goto exit_rmdir;
2103 * Due to there is no way to change bDeleteVetoFiles variable
2104 * from this module, need to clean up ourselves
2107 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
2108 if (dh == NULL) {
2109 goto exit_rmdir;
2112 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
2113 struct adouble *ad = NULL;
2114 char *p = NULL;
2115 struct smb_filename *ad_smb_fname = NULL;
2116 int ret;
2118 if (!is_adouble_file(de->d_name)) {
2119 continue;
2122 p = talloc_asprintf(talloc_tos(), "%s/%s",
2123 smb_fname->base_name, de->d_name);
2124 if (p == NULL) {
2125 DBG_ERR("talloc_asprintf failed\n");
2126 return -1;
2129 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
2130 NULL, NULL,
2131 smb_fname->flags);
2132 TALLOC_FREE(p);
2133 if (ad_smb_fname == NULL) {
2134 DBG_ERR("synthetic_smb_fname failed\n");
2135 return -1;
2139 * Check whether it's a valid AppleDouble file, if
2140 * yes, delete it, ignore it otherwise.
2142 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
2143 if (ad == NULL) {
2144 TALLOC_FREE(ad_smb_fname);
2145 TALLOC_FREE(p);
2146 continue;
2148 TALLOC_FREE(ad);
2150 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2151 dirfsp,
2152 ad_smb_fname,
2154 if (ret != 0) {
2155 DBG_ERR("Deleting [%s] failed\n",
2156 smb_fname_str_dbg(ad_smb_fname));
2158 TALLOC_FREE(ad_smb_fname);
2161 exit_rmdir:
2162 if (dh) {
2163 SMB_VFS_CLOSEDIR(handle->conn, dh);
2165 return SMB_VFS_NEXT_UNLINKAT(handle,
2166 dirfsp,
2167 smb_fname,
2168 AT_REMOVEDIR);
2171 static int fruit_unlinkat(vfs_handle_struct *handle,
2172 struct files_struct *dirfsp,
2173 const struct smb_filename *smb_fname,
2174 int flags)
2176 int ret;
2178 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
2179 if (flags & AT_REMOVEDIR) {
2180 ret = fruit_rmdir_internal(handle,
2181 dirfsp,
2182 smb_fname);
2183 } else {
2184 ret = fruit_unlink_internal(handle,
2185 dirfsp,
2186 smb_fname);
2188 return ret;
2191 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2192 files_struct *fsp, void *data,
2193 size_t n, off_t offset)
2195 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2196 ssize_t nread;
2197 int ret;
2199 if (fio->fake_fd) {
2200 return -1;
2203 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2204 if (nread == -1 || nread == n) {
2205 return nread;
2208 DBG_ERR("Removing [%s] after short read [%zd]\n",
2209 fsp_str_dbg(fsp), nread);
2211 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2212 fsp->conn->cwd_fsp,
2213 fsp->fsp_name,
2215 if (ret != 0) {
2216 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2217 return -1;
2220 errno = EINVAL;
2221 return -1;
2224 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2225 files_struct *fsp, void *data,
2226 size_t n, off_t offset)
2228 AfpInfo *ai = NULL;
2229 struct adouble *ad = NULL;
2230 char afpinfo_buf[AFP_INFO_SIZE];
2231 char *p = NULL;
2232 ssize_t nread;
2234 ai = afpinfo_new(talloc_tos());
2235 if (ai == NULL) {
2236 return -1;
2239 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2240 if (ad == NULL) {
2241 nread = -1;
2242 goto fail;
2245 p = ad_get_entry(ad, ADEID_FINDERI);
2246 if (p == NULL) {
2247 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2248 nread = -1;
2249 goto fail;
2252 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2254 nread = afpinfo_pack(ai, afpinfo_buf);
2255 if (nread != AFP_INFO_SIZE) {
2256 nread = -1;
2257 goto fail;
2260 memcpy(data, afpinfo_buf, n);
2261 nread = n;
2263 fail:
2264 TALLOC_FREE(ai);
2265 return nread;
2268 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2269 files_struct *fsp, void *data,
2270 size_t n, off_t offset)
2272 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2273 ssize_t nread;
2274 ssize_t to_return;
2277 * OS X has a off-by-1 error in the offset calculation, so we're
2278 * bug compatible here. It won't hurt, as any relevant real
2279 * world read requests from the AFP_AfpInfo stream will be
2280 * offset=0 n=60. offset is ignored anyway, see below.
2282 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2283 return 0;
2286 if (fio == NULL) {
2287 DBG_ERR("Failed to fetch fsp extension");
2288 return -1;
2291 /* Yes, macOS always reads from offset 0 */
2292 offset = 0;
2293 to_return = MIN(n, AFP_INFO_SIZE);
2295 switch (fio->config->meta) {
2296 case FRUIT_META_STREAM:
2297 nread = fruit_pread_meta_stream(handle, fsp, data,
2298 to_return, offset);
2299 break;
2301 case FRUIT_META_NETATALK:
2302 nread = fruit_pread_meta_adouble(handle, fsp, data,
2303 to_return, offset);
2304 break;
2306 default:
2307 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2308 return -1;
2311 if (nread == -1 && fio->fake_fd) {
2312 AfpInfo *ai = NULL;
2313 char afpinfo_buf[AFP_INFO_SIZE];
2315 ai = afpinfo_new(talloc_tos());
2316 if (ai == NULL) {
2317 return -1;
2320 nread = afpinfo_pack(ai, afpinfo_buf);
2321 TALLOC_FREE(ai);
2322 if (nread != AFP_INFO_SIZE) {
2323 return -1;
2326 memcpy(data, afpinfo_buf, to_return);
2327 return to_return;
2330 return nread;
2333 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2334 files_struct *fsp, void *data,
2335 size_t n, off_t offset)
2337 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2340 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2341 files_struct *fsp, void *data,
2342 size_t n, off_t offset)
2344 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2347 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2348 files_struct *fsp, void *data,
2349 size_t n, off_t offset)
2351 struct adouble *ad = NULL;
2352 ssize_t nread;
2354 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2355 if (ad == NULL) {
2356 return -1;
2359 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
2360 offset + ad_getentryoff(ad, ADEID_RFORK));
2362 TALLOC_FREE(ad);
2363 return nread;
2366 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2367 files_struct *fsp, void *data,
2368 size_t n, off_t offset)
2370 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2371 ssize_t nread;
2373 if (fio == NULL) {
2374 errno = EINVAL;
2375 return -1;
2378 switch (fio->config->rsrc) {
2379 case FRUIT_RSRC_STREAM:
2380 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2381 break;
2383 case FRUIT_RSRC_ADFILE:
2384 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2385 break;
2387 case FRUIT_RSRC_XATTR:
2388 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2389 break;
2391 default:
2392 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2393 return -1;
2396 return nread;
2399 static ssize_t fruit_pread(vfs_handle_struct *handle,
2400 files_struct *fsp, void *data,
2401 size_t n, off_t offset)
2403 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2404 ssize_t nread;
2406 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2407 fsp_str_dbg(fsp), (intmax_t)offset, n);
2409 if (fio == NULL) {
2410 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2413 if (fio->type == ADOUBLE_META) {
2414 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2415 } else {
2416 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2419 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2420 return nread;
2423 static bool fruit_must_handle_aio_stream(struct fio *fio)
2425 if (fio == NULL) {
2426 return false;
2429 if (fio->type == ADOUBLE_META) {
2430 return true;
2433 if ((fio->type == ADOUBLE_RSRC) &&
2434 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2436 return true;
2439 return false;
2442 struct fruit_pread_state {
2443 ssize_t nread;
2444 struct vfs_aio_state vfs_aio_state;
2447 static void fruit_pread_done(struct tevent_req *subreq);
2449 static struct tevent_req *fruit_pread_send(
2450 struct vfs_handle_struct *handle,
2451 TALLOC_CTX *mem_ctx,
2452 struct tevent_context *ev,
2453 struct files_struct *fsp,
2454 void *data,
2455 size_t n, off_t offset)
2457 struct tevent_req *req = NULL;
2458 struct tevent_req *subreq = NULL;
2459 struct fruit_pread_state *state = NULL;
2460 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2462 req = tevent_req_create(mem_ctx, &state,
2463 struct fruit_pread_state);
2464 if (req == NULL) {
2465 return NULL;
2468 if (fruit_must_handle_aio_stream(fio)) {
2469 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2470 if (state->nread != n) {
2471 if (state->nread != -1) {
2472 errno = EIO;
2474 tevent_req_error(req, errno);
2475 return tevent_req_post(req, ev);
2477 tevent_req_done(req);
2478 return tevent_req_post(req, ev);
2481 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2482 data, n, offset);
2483 if (tevent_req_nomem(req, subreq)) {
2484 return tevent_req_post(req, ev);
2486 tevent_req_set_callback(subreq, fruit_pread_done, req);
2487 return req;
2490 static void fruit_pread_done(struct tevent_req *subreq)
2492 struct tevent_req *req = tevent_req_callback_data(
2493 subreq, struct tevent_req);
2494 struct fruit_pread_state *state = tevent_req_data(
2495 req, struct fruit_pread_state);
2497 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2498 TALLOC_FREE(subreq);
2500 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2501 return;
2503 tevent_req_done(req);
2506 static ssize_t fruit_pread_recv(struct tevent_req *req,
2507 struct vfs_aio_state *vfs_aio_state)
2509 struct fruit_pread_state *state = tevent_req_data(
2510 req, struct fruit_pread_state);
2512 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2513 return -1;
2516 *vfs_aio_state = state->vfs_aio_state;
2517 return state->nread;
2520 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2521 files_struct *fsp, const void *data,
2522 size_t n, off_t offset)
2524 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2525 AfpInfo *ai = NULL;
2526 size_t nwritten;
2527 int ret;
2528 bool ok;
2530 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2531 fsp_str_dbg(fsp), (intmax_t)offset, n);
2533 if (fio == NULL) {
2534 return -1;
2537 if (fio->fake_fd) {
2538 int fd = fsp->fh->fd;
2540 ret = vfs_fake_fd_close(fd);
2541 fsp->fh->fd = -1;
2542 if (ret != 0) {
2543 DBG_ERR("Close [%s] failed: %s\n",
2544 fsp_str_dbg(fsp), strerror(errno));
2545 return -1;
2548 fd = SMB_VFS_NEXT_OPEN(handle,
2549 fsp->fsp_name,
2550 fsp,
2551 fio->flags,
2552 fio->mode);
2553 if (fd == -1) {
2554 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2555 fsp_str_dbg(fsp), strerror(errno));
2556 return -1;
2558 fsp->fh->fd = fd;
2559 fio->fake_fd = false;
2562 ai = afpinfo_unpack(talloc_tos(), data);
2563 if (ai == NULL) {
2564 return -1;
2567 if (ai_empty_finderinfo(ai)) {
2569 * Writing an all 0 blob to the metadata stream results in the
2570 * stream being removed on a macOS server. This ensures we
2571 * behave the same and it verified by the "delete AFP_AfpInfo by
2572 * writing all 0" test.
2574 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2575 if (ret != 0) {
2576 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2577 fsp_str_dbg(fsp));
2578 return -1;
2581 ok = set_delete_on_close(
2582 fsp,
2583 true,
2584 handle->conn->session_info->security_token,
2585 handle->conn->session_info->unix_token);
2586 if (!ok) {
2587 DBG_ERR("set_delete_on_close on [%s] failed\n",
2588 fsp_str_dbg(fsp));
2589 return -1;
2591 return n;
2594 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2595 if (nwritten != n) {
2596 return -1;
2599 return n;
2602 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2603 files_struct *fsp, const void *data,
2604 size_t n, off_t offset)
2606 struct adouble *ad = NULL;
2607 AfpInfo *ai = NULL;
2608 char *p = NULL;
2609 int ret;
2610 bool ok;
2612 ai = afpinfo_unpack(talloc_tos(), data);
2613 if (ai == NULL) {
2614 return -1;
2617 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2618 if (ad == NULL) {
2619 ad = ad_init(talloc_tos(), ADOUBLE_META);
2620 if (ad == NULL) {
2621 return -1;
2624 p = ad_get_entry(ad, ADEID_FINDERI);
2625 if (p == NULL) {
2626 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2627 TALLOC_FREE(ad);
2628 return -1;
2631 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2633 ret = ad_fset(handle, ad, fsp);
2634 if (ret != 0) {
2635 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2636 TALLOC_FREE(ad);
2637 return -1;
2640 TALLOC_FREE(ad);
2642 if (!ai_empty_finderinfo(ai)) {
2643 return n;
2647 * Writing an all 0 blob to the metadata stream results in the stream
2648 * being removed on a macOS server. This ensures we behave the same and
2649 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2652 ok = set_delete_on_close(
2653 fsp,
2654 true,
2655 handle->conn->session_info->security_token,
2656 handle->conn->session_info->unix_token);
2657 if (!ok) {
2658 DBG_ERR("set_delete_on_close on [%s] failed\n",
2659 fsp_str_dbg(fsp));
2660 return -1;
2663 return n;
2666 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2667 files_struct *fsp, const void *data,
2668 size_t n, off_t offset)
2670 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2671 ssize_t nwritten;
2672 uint8_t buf[AFP_INFO_SIZE];
2673 size_t to_write;
2674 size_t to_copy;
2675 int cmp;
2677 if (fio == NULL) {
2678 DBG_ERR("Failed to fetch fsp extension");
2679 return -1;
2682 if (n < 3) {
2683 errno = EINVAL;
2684 return -1;
2687 if (offset != 0 && n < 60) {
2688 errno = EINVAL;
2689 return -1;
2692 cmp = memcmp(data, "AFP", 3);
2693 if (cmp != 0) {
2694 errno = EINVAL;
2695 return -1;
2698 if (n <= AFP_OFF_FinderInfo) {
2700 * Nothing to do here really, just return
2702 return n;
2705 offset = 0;
2707 to_copy = n;
2708 if (to_copy > AFP_INFO_SIZE) {
2709 to_copy = AFP_INFO_SIZE;
2711 memcpy(buf, data, to_copy);
2713 to_write = n;
2714 if (to_write != AFP_INFO_SIZE) {
2715 to_write = AFP_INFO_SIZE;
2718 switch (fio->config->meta) {
2719 case FRUIT_META_STREAM:
2720 nwritten = fruit_pwrite_meta_stream(handle,
2721 fsp,
2722 buf,
2723 to_write,
2724 offset);
2725 break;
2727 case FRUIT_META_NETATALK:
2728 nwritten = fruit_pwrite_meta_netatalk(handle,
2729 fsp,
2730 buf,
2731 to_write,
2732 offset);
2733 break;
2735 default:
2736 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2737 return -1;
2740 if (nwritten != to_write) {
2741 return -1;
2745 * Return the requested amount, verified against macOS SMB server
2747 return n;
2750 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2751 files_struct *fsp, const void *data,
2752 size_t n, off_t offset)
2754 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2757 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2758 files_struct *fsp, const void *data,
2759 size_t n, off_t offset)
2761 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2764 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2765 files_struct *fsp, const void *data,
2766 size_t n, off_t offset)
2768 struct adouble *ad = NULL;
2769 ssize_t nwritten;
2770 int ret;
2772 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2773 if (ad == NULL) {
2774 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
2775 return -1;
2778 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2779 offset + ad_getentryoff(ad, ADEID_RFORK));
2780 if (nwritten != n) {
2781 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2782 fsp_str_dbg(fsp), nwritten, n);
2783 TALLOC_FREE(ad);
2784 return -1;
2787 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2788 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2789 ret = ad_fset(handle, ad, fsp);
2790 if (ret != 0) {
2791 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2792 TALLOC_FREE(ad);
2793 return -1;
2797 TALLOC_FREE(ad);
2798 return n;
2801 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2802 files_struct *fsp, const void *data,
2803 size_t n, off_t offset)
2805 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2806 ssize_t nwritten;
2808 if (fio == NULL) {
2809 DBG_ERR("Failed to fetch fsp extension");
2810 return -1;
2813 switch (fio->config->rsrc) {
2814 case FRUIT_RSRC_STREAM:
2815 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2816 break;
2818 case FRUIT_RSRC_ADFILE:
2819 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2820 break;
2822 case FRUIT_RSRC_XATTR:
2823 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2824 break;
2826 default:
2827 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2828 return -1;
2831 return nwritten;
2834 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2835 files_struct *fsp, const void *data,
2836 size_t n, off_t offset)
2838 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2839 ssize_t nwritten;
2841 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2842 fsp_str_dbg(fsp), (intmax_t)offset, n);
2844 if (fio == NULL) {
2845 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2848 if (fio->type == ADOUBLE_META) {
2849 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2850 } else {
2851 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2854 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2855 return nwritten;
2858 struct fruit_pwrite_state {
2859 ssize_t nwritten;
2860 struct vfs_aio_state vfs_aio_state;
2863 static void fruit_pwrite_done(struct tevent_req *subreq);
2865 static struct tevent_req *fruit_pwrite_send(
2866 struct vfs_handle_struct *handle,
2867 TALLOC_CTX *mem_ctx,
2868 struct tevent_context *ev,
2869 struct files_struct *fsp,
2870 const void *data,
2871 size_t n, off_t offset)
2873 struct tevent_req *req = NULL;
2874 struct tevent_req *subreq = NULL;
2875 struct fruit_pwrite_state *state = NULL;
2876 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2878 req = tevent_req_create(mem_ctx, &state,
2879 struct fruit_pwrite_state);
2880 if (req == NULL) {
2881 return NULL;
2884 if (fruit_must_handle_aio_stream(fio)) {
2885 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
2886 if (state->nwritten != n) {
2887 if (state->nwritten != -1) {
2888 errno = EIO;
2890 tevent_req_error(req, errno);
2891 return tevent_req_post(req, ev);
2893 tevent_req_done(req);
2894 return tevent_req_post(req, ev);
2897 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
2898 data, n, offset);
2899 if (tevent_req_nomem(req, subreq)) {
2900 return tevent_req_post(req, ev);
2902 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
2903 return req;
2906 static void fruit_pwrite_done(struct tevent_req *subreq)
2908 struct tevent_req *req = tevent_req_callback_data(
2909 subreq, struct tevent_req);
2910 struct fruit_pwrite_state *state = tevent_req_data(
2911 req, struct fruit_pwrite_state);
2913 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2914 TALLOC_FREE(subreq);
2916 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2917 return;
2919 tevent_req_done(req);
2922 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
2923 struct vfs_aio_state *vfs_aio_state)
2925 struct fruit_pwrite_state *state = tevent_req_data(
2926 req, struct fruit_pwrite_state);
2928 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2929 return -1;
2932 *vfs_aio_state = state->vfs_aio_state;
2933 return state->nwritten;
2937 * Helper to stat/lstat the base file of an smb_fname.
2939 static int fruit_stat_base(vfs_handle_struct *handle,
2940 struct smb_filename *smb_fname,
2941 bool follow_links)
2943 char *tmp_stream_name;
2944 int rc;
2946 tmp_stream_name = smb_fname->stream_name;
2947 smb_fname->stream_name = NULL;
2948 if (follow_links) {
2949 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2950 } else {
2951 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2953 smb_fname->stream_name = tmp_stream_name;
2955 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
2956 smb_fname->base_name,
2957 (uintmax_t)smb_fname->st.st_ex_dev,
2958 (uintmax_t)smb_fname->st.st_ex_ino);
2959 return rc;
2962 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
2963 struct smb_filename *smb_fname,
2964 bool follow_links)
2966 int ret;
2967 ino_t ino;
2969 ret = fruit_stat_base(handle, smb_fname, false);
2970 if (ret != 0) {
2971 return -1;
2974 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
2976 if (follow_links) {
2977 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2978 } else {
2979 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2982 smb_fname->st.st_ex_ino = ino;
2984 return ret;
2987 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
2988 struct smb_filename *smb_fname,
2989 bool follow_links)
2991 struct adouble *ad = NULL;
2993 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2994 if (ad == NULL) {
2995 DBG_INFO("fruit_stat_meta %s: %s\n",
2996 smb_fname_str_dbg(smb_fname), strerror(errno));
2997 errno = ENOENT;
2998 return -1;
3000 TALLOC_FREE(ad);
3002 /* Populate the stat struct with info from the base file. */
3003 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3004 return -1;
3006 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3007 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3008 smb_fname->stream_name);
3009 return 0;
3012 static int fruit_stat_meta(vfs_handle_struct *handle,
3013 struct smb_filename *smb_fname,
3014 bool follow_links)
3016 struct fruit_config_data *config = NULL;
3017 int ret;
3019 SMB_VFS_HANDLE_GET_DATA(handle, config,
3020 struct fruit_config_data, return -1);
3022 switch (config->meta) {
3023 case FRUIT_META_STREAM:
3024 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3025 break;
3027 case FRUIT_META_NETATALK:
3028 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3029 break;
3031 default:
3032 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3033 return -1;
3036 return ret;
3039 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3040 struct smb_filename *smb_fname,
3041 bool follow_links)
3043 struct adouble *ad = NULL;
3044 int ret;
3046 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3047 if (ad == NULL) {
3048 errno = ENOENT;
3049 return -1;
3052 /* Populate the stat struct with info from the base file. */
3053 ret = fruit_stat_base(handle, smb_fname, follow_links);
3054 if (ret != 0) {
3055 TALLOC_FREE(ad);
3056 return -1;
3059 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3060 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3061 smb_fname->stream_name);
3062 TALLOC_FREE(ad);
3063 return 0;
3066 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3067 struct smb_filename *smb_fname,
3068 bool follow_links)
3070 int ret;
3072 if (follow_links) {
3073 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3074 } else {
3075 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3078 return ret;
3081 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3082 struct smb_filename *smb_fname,
3083 bool follow_links)
3085 #ifdef HAVE_ATTROPEN
3086 int ret;
3087 int fd = -1;
3089 /* Populate the stat struct with info from the base file. */
3090 ret = fruit_stat_base(handle, smb_fname, follow_links);
3091 if (ret != 0) {
3092 return -1;
3095 fd = attropen(smb_fname->base_name,
3096 AFPRESOURCE_EA_NETATALK,
3097 O_RDONLY);
3098 if (fd == -1) {
3099 return 0;
3102 ret = sys_fstat(fd, &smb_fname->st, false);
3103 if (ret != 0) {
3104 close(fd);
3105 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3106 AFPRESOURCE_EA_NETATALK);
3107 return -1;
3109 close(fd);
3110 fd = -1;
3112 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3113 smb_fname->stream_name);
3115 return ret;
3117 #else
3118 errno = ENOSYS;
3119 return -1;
3120 #endif
3123 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3124 struct smb_filename *smb_fname,
3125 bool follow_links)
3127 struct fruit_config_data *config = NULL;
3128 int ret;
3130 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3132 SMB_VFS_HANDLE_GET_DATA(handle, config,
3133 struct fruit_config_data, return -1);
3135 switch (config->rsrc) {
3136 case FRUIT_RSRC_STREAM:
3137 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3138 break;
3140 case FRUIT_RSRC_XATTR:
3141 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3142 break;
3144 case FRUIT_RSRC_ADFILE:
3145 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3146 break;
3148 default:
3149 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3150 return -1;
3153 return ret;
3156 static int fruit_stat(vfs_handle_struct *handle,
3157 struct smb_filename *smb_fname)
3159 int rc = -1;
3161 DEBUG(10, ("fruit_stat called for %s\n",
3162 smb_fname_str_dbg(smb_fname)));
3164 if (!is_named_stream(smb_fname)) {
3165 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3166 if (rc == 0) {
3167 update_btime(handle, smb_fname);
3169 return rc;
3173 * Note if lp_posix_paths() is true, we can never
3174 * get here as is_ntfs_stream_smb_fname() is
3175 * always false. So we never need worry about
3176 * not following links here.
3179 if (is_afpinfo_stream(smb_fname->stream_name)) {
3180 rc = fruit_stat_meta(handle, smb_fname, true);
3181 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3182 rc = fruit_stat_rsrc(handle, smb_fname, true);
3183 } else {
3184 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3187 if (rc == 0) {
3188 update_btime(handle, smb_fname);
3189 smb_fname->st.st_ex_mode &= ~S_IFMT;
3190 smb_fname->st.st_ex_mode |= S_IFREG;
3191 smb_fname->st.st_ex_blocks =
3192 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3194 return rc;
3197 static int fruit_lstat(vfs_handle_struct *handle,
3198 struct smb_filename *smb_fname)
3200 int rc = -1;
3202 DEBUG(10, ("fruit_lstat called for %s\n",
3203 smb_fname_str_dbg(smb_fname)));
3205 if (!is_named_stream(smb_fname)) {
3206 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3207 if (rc == 0) {
3208 update_btime(handle, smb_fname);
3210 return rc;
3213 if (is_afpinfo_stream(smb_fname->stream_name)) {
3214 rc = fruit_stat_meta(handle, smb_fname, false);
3215 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3216 rc = fruit_stat_rsrc(handle, smb_fname, false);
3217 } else {
3218 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3221 if (rc == 0) {
3222 update_btime(handle, smb_fname);
3223 smb_fname->st.st_ex_mode &= ~S_IFMT;
3224 smb_fname->st.st_ex_mode |= S_IFREG;
3225 smb_fname->st.st_ex_blocks =
3226 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3228 return rc;
3231 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3232 files_struct *fsp,
3233 SMB_STRUCT_STAT *sbuf)
3235 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3236 struct smb_filename smb_fname;
3237 ino_t ino;
3238 int ret;
3240 if (fio == NULL) {
3241 return -1;
3244 if (fio->fake_fd) {
3245 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3246 if (ret != 0) {
3247 return -1;
3250 *sbuf = fsp->base_fsp->fsp_name->st;
3251 sbuf->st_ex_size = AFP_INFO_SIZE;
3252 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3253 return 0;
3256 smb_fname = (struct smb_filename) {
3257 .base_name = fsp->fsp_name->base_name,
3260 ret = fruit_stat_base(handle, &smb_fname, false);
3261 if (ret != 0) {
3262 return -1;
3264 *sbuf = smb_fname.st;
3266 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3268 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3269 if (ret != 0) {
3270 return -1;
3273 sbuf->st_ex_ino = ino;
3274 return 0;
3277 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3278 files_struct *fsp,
3279 SMB_STRUCT_STAT *sbuf)
3281 int ret;
3283 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3284 if (ret != 0) {
3285 return -1;
3288 *sbuf = fsp->base_fsp->fsp_name->st;
3289 sbuf->st_ex_size = AFP_INFO_SIZE;
3290 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3292 return 0;
3295 static int fruit_fstat_meta(vfs_handle_struct *handle,
3296 files_struct *fsp,
3297 SMB_STRUCT_STAT *sbuf,
3298 struct fio *fio)
3300 int ret;
3302 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3304 switch (fio->config->meta) {
3305 case FRUIT_META_STREAM:
3306 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3307 break;
3309 case FRUIT_META_NETATALK:
3310 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3311 break;
3313 default:
3314 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3315 return -1;
3318 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3319 return ret;
3322 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3323 files_struct *fsp,
3324 SMB_STRUCT_STAT *sbuf)
3326 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3329 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3330 files_struct *fsp,
3331 SMB_STRUCT_STAT *sbuf)
3333 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3336 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3337 files_struct *fsp,
3338 SMB_STRUCT_STAT *sbuf)
3340 struct adouble *ad = NULL;
3341 int ret;
3343 /* Populate the stat struct with info from the base file. */
3344 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3345 if (ret == -1) {
3346 return -1;
3349 ad = ad_get(talloc_tos(), handle,
3350 fsp->base_fsp->fsp_name,
3351 ADOUBLE_RSRC);
3352 if (ad == NULL) {
3353 DBG_ERR("ad_get [%s] failed [%s]\n",
3354 fsp_str_dbg(fsp), strerror(errno));
3355 return -1;
3358 *sbuf = fsp->base_fsp->fsp_name->st;
3359 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3360 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3362 TALLOC_FREE(ad);
3363 return 0;
3366 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3367 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3369 int ret;
3371 switch (fio->config->rsrc) {
3372 case FRUIT_RSRC_STREAM:
3373 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3374 break;
3376 case FRUIT_RSRC_ADFILE:
3377 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3378 break;
3380 case FRUIT_RSRC_XATTR:
3381 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3382 break;
3384 default:
3385 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3386 return -1;
3389 return ret;
3392 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3393 SMB_STRUCT_STAT *sbuf)
3395 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3396 int rc;
3398 if (fio == NULL) {
3399 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3402 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3404 if (fio->type == ADOUBLE_META) {
3405 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3406 } else {
3407 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3410 if (rc == 0) {
3411 sbuf->st_ex_mode &= ~S_IFMT;
3412 sbuf->st_ex_mode |= S_IFREG;
3413 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3416 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3417 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3418 return rc;
3421 static NTSTATUS delete_invalid_meta_stream(
3422 vfs_handle_struct *handle,
3423 const struct smb_filename *smb_fname,
3424 TALLOC_CTX *mem_ctx,
3425 unsigned int *pnum_streams,
3426 struct stream_struct **pstreams,
3427 off_t size)
3429 struct smb_filename *sname = NULL;
3430 int ret;
3431 bool ok;
3433 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3434 if (!ok) {
3435 return NT_STATUS_INTERNAL_ERROR;
3438 if (size == 0) {
3439 return NT_STATUS_OK;
3442 sname = synthetic_smb_fname(talloc_tos(),
3443 smb_fname->base_name,
3444 AFPINFO_STREAM_NAME,
3445 NULL, 0);
3446 if (sname == NULL) {
3447 return NT_STATUS_NO_MEMORY;
3450 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3451 handle->conn->cwd_fsp,
3452 sname,
3454 TALLOC_FREE(sname);
3455 if (ret != 0) {
3456 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3457 return map_nt_error_from_unix(errno);
3460 return NT_STATUS_OK;
3463 static NTSTATUS fruit_streaminfo_meta_stream(
3464 vfs_handle_struct *handle,
3465 struct files_struct *fsp,
3466 const struct smb_filename *smb_fname,
3467 TALLOC_CTX *mem_ctx,
3468 unsigned int *pnum_streams,
3469 struct stream_struct **pstreams)
3471 struct stream_struct *stream = *pstreams;
3472 unsigned int num_streams = *pnum_streams;
3473 int i;
3475 for (i = 0; i < num_streams; i++) {
3476 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3477 break;
3481 if (i == num_streams) {
3482 return NT_STATUS_OK;
3485 if (stream[i].size != AFP_INFO_SIZE) {
3486 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3487 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3489 return delete_invalid_meta_stream(handle,
3490 smb_fname,
3491 mem_ctx,
3492 pnum_streams,
3493 pstreams,
3494 stream[i].size);
3498 return NT_STATUS_OK;
3501 static NTSTATUS fruit_streaminfo_meta_netatalk(
3502 vfs_handle_struct *handle,
3503 struct files_struct *fsp,
3504 const struct smb_filename *smb_fname,
3505 TALLOC_CTX *mem_ctx,
3506 unsigned int *pnum_streams,
3507 struct stream_struct **pstreams)
3509 struct stream_struct *stream = *pstreams;
3510 unsigned int num_streams = *pnum_streams;
3511 struct adouble *ad = NULL;
3512 bool is_fi_empty;
3513 int i;
3514 bool ok;
3516 /* Remove the Netatalk xattr from the list */
3517 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3518 ":" NETATALK_META_XATTR ":$DATA");
3519 if (!ok) {
3520 return NT_STATUS_NO_MEMORY;
3524 * Check if there's a AFPINFO_STREAM from the VFS streams
3525 * backend and if yes, remove it from the list
3527 for (i = 0; i < num_streams; i++) {
3528 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3529 break;
3533 if (i < num_streams) {
3534 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3535 smb_fname_str_dbg(smb_fname));
3537 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3538 AFPINFO_STREAM);
3539 if (!ok) {
3540 return NT_STATUS_INTERNAL_ERROR;
3544 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3545 if (ad == NULL) {
3546 return NT_STATUS_OK;
3549 is_fi_empty = ad_empty_finderinfo(ad);
3550 TALLOC_FREE(ad);
3552 if (is_fi_empty) {
3553 return NT_STATUS_OK;
3556 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3557 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3558 smb_roundup(handle->conn, AFP_INFO_SIZE));
3559 if (!ok) {
3560 return NT_STATUS_NO_MEMORY;
3563 return NT_STATUS_OK;
3566 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3567 struct files_struct *fsp,
3568 const struct smb_filename *smb_fname,
3569 TALLOC_CTX *mem_ctx,
3570 unsigned int *pnum_streams,
3571 struct stream_struct **pstreams)
3573 struct fruit_config_data *config = NULL;
3574 NTSTATUS status;
3576 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3577 return NT_STATUS_INTERNAL_ERROR);
3579 switch (config->meta) {
3580 case FRUIT_META_NETATALK:
3581 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3582 mem_ctx, pnum_streams,
3583 pstreams);
3584 break;
3586 case FRUIT_META_STREAM:
3587 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3588 mem_ctx, pnum_streams,
3589 pstreams);
3590 break;
3592 default:
3593 return NT_STATUS_INTERNAL_ERROR;
3596 return status;
3599 static NTSTATUS fruit_streaminfo_rsrc_stream(
3600 vfs_handle_struct *handle,
3601 struct files_struct *fsp,
3602 const struct smb_filename *smb_fname,
3603 TALLOC_CTX *mem_ctx,
3604 unsigned int *pnum_streams,
3605 struct stream_struct **pstreams)
3607 bool ok;
3609 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3610 if (!ok) {
3611 DBG_ERR("Filtering resource stream failed\n");
3612 return NT_STATUS_INTERNAL_ERROR;
3614 return NT_STATUS_OK;
3617 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3618 vfs_handle_struct *handle,
3619 struct files_struct *fsp,
3620 const struct smb_filename *smb_fname,
3621 TALLOC_CTX *mem_ctx,
3622 unsigned int *pnum_streams,
3623 struct stream_struct **pstreams)
3625 bool ok;
3627 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3628 if (!ok) {
3629 DBG_ERR("Filtering resource stream failed\n");
3630 return NT_STATUS_INTERNAL_ERROR;
3632 return NT_STATUS_OK;
3635 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3636 vfs_handle_struct *handle,
3637 struct files_struct *fsp,
3638 const struct smb_filename *smb_fname,
3639 TALLOC_CTX *mem_ctx,
3640 unsigned int *pnum_streams,
3641 struct stream_struct **pstreams)
3643 struct stream_struct *stream = *pstreams;
3644 unsigned int num_streams = *pnum_streams;
3645 struct adouble *ad = NULL;
3646 bool ok;
3647 size_t rlen;
3648 int i;
3651 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3652 * and if yes, remove it from the list
3654 for (i = 0; i < num_streams; i++) {
3655 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3656 break;
3660 if (i < num_streams) {
3661 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3662 smb_fname_str_dbg(smb_fname));
3664 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3665 AFPRESOURCE_STREAM);
3666 if (!ok) {
3667 return NT_STATUS_INTERNAL_ERROR;
3671 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3672 if (ad == NULL) {
3673 return NT_STATUS_OK;
3676 rlen = ad_getentrylen(ad, ADEID_RFORK);
3677 TALLOC_FREE(ad);
3679 if (rlen == 0) {
3680 return NT_STATUS_OK;
3683 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3684 AFPRESOURCE_STREAM_NAME, rlen,
3685 smb_roundup(handle->conn, rlen));
3686 if (!ok) {
3687 return NT_STATUS_NO_MEMORY;
3690 return NT_STATUS_OK;
3693 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3694 struct files_struct *fsp,
3695 const struct smb_filename *smb_fname,
3696 TALLOC_CTX *mem_ctx,
3697 unsigned int *pnum_streams,
3698 struct stream_struct **pstreams)
3700 struct fruit_config_data *config = NULL;
3701 NTSTATUS status;
3703 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3704 return NT_STATUS_INTERNAL_ERROR);
3706 switch (config->rsrc) {
3707 case FRUIT_RSRC_STREAM:
3708 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3709 mem_ctx, pnum_streams,
3710 pstreams);
3711 break;
3713 case FRUIT_RSRC_XATTR:
3714 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3715 mem_ctx, pnum_streams,
3716 pstreams);
3717 break;
3719 case FRUIT_RSRC_ADFILE:
3720 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3721 mem_ctx, pnum_streams,
3722 pstreams);
3723 break;
3725 default:
3726 return NT_STATUS_INTERNAL_ERROR;
3729 return status;
3732 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3733 struct stream_struct **pstreams)
3735 unsigned num_streams = *pnum_streams;
3736 struct stream_struct *streams = *pstreams;
3737 unsigned i = 0;
3739 if (!global_fruit_config.nego_aapl) {
3740 return;
3743 while (i < num_streams) {
3744 struct smb_filename smb_fname = (struct smb_filename) {
3745 .stream_name = streams[i].name,
3748 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3749 || streams[i].size > 0)
3751 i++;
3752 continue;
3755 streams[i] = streams[num_streams - 1];
3756 num_streams--;
3759 *pnum_streams = num_streams;
3762 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3763 struct files_struct *fsp,
3764 const struct smb_filename *smb_fname,
3765 TALLOC_CTX *mem_ctx,
3766 unsigned int *pnum_streams,
3767 struct stream_struct **pstreams)
3769 struct fruit_config_data *config = NULL;
3770 NTSTATUS status;
3772 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3773 return NT_STATUS_UNSUCCESSFUL);
3775 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3777 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3778 pnum_streams, pstreams);
3779 if (!NT_STATUS_IS_OK(status)) {
3780 return status;
3783 fruit_filter_empty_streams(pnum_streams, pstreams);
3785 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3786 mem_ctx, pnum_streams, pstreams);
3787 if (!NT_STATUS_IS_OK(status)) {
3788 return status;
3791 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3792 mem_ctx, pnum_streams, pstreams);
3793 if (!NT_STATUS_IS_OK(status)) {
3794 return status;
3797 return NT_STATUS_OK;
3800 static int fruit_ntimes(vfs_handle_struct *handle,
3801 const struct smb_filename *smb_fname,
3802 struct smb_file_time *ft)
3804 int rc = 0;
3805 struct adouble *ad = NULL;
3806 struct fruit_config_data *config = NULL;
3808 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3809 return -1);
3811 if ((config->meta != FRUIT_META_NETATALK) ||
3812 is_omit_timespec(&ft->create_time))
3814 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3817 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3818 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3820 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3821 if (ad == NULL) {
3822 goto exit;
3825 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3826 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3828 rc = ad_set(handle, ad, smb_fname);
3830 exit:
3832 TALLOC_FREE(ad);
3833 if (rc != 0) {
3834 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3835 return -1;
3837 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3840 static int fruit_fallocate(struct vfs_handle_struct *handle,
3841 struct files_struct *fsp,
3842 uint32_t mode,
3843 off_t offset,
3844 off_t len)
3846 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3848 if (fio == NULL) {
3849 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3852 /* Let the pwrite code path handle it. */
3853 errno = ENOSYS;
3854 return -1;
3857 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
3858 struct files_struct *fsp,
3859 off_t offset)
3861 #ifdef HAVE_ATTROPEN
3862 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3863 #endif
3864 return 0;
3867 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
3868 struct files_struct *fsp,
3869 off_t offset)
3871 int rc;
3872 struct adouble *ad = NULL;
3873 off_t ad_off;
3875 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3876 if (ad == NULL) {
3877 DBG_DEBUG("ad_get [%s] failed [%s]\n",
3878 fsp_str_dbg(fsp), strerror(errno));
3879 return -1;
3882 ad_off = ad_getentryoff(ad, ADEID_RFORK);
3884 rc = ftruncate(fsp->fh->fd, offset + ad_off);
3885 if (rc != 0) {
3886 TALLOC_FREE(ad);
3887 return -1;
3890 ad_setentrylen(ad, ADEID_RFORK, offset);
3892 rc = ad_fset(handle, ad, fsp);
3893 if (rc != 0) {
3894 DBG_ERR("ad_fset [%s] failed [%s]\n",
3895 fsp_str_dbg(fsp), strerror(errno));
3896 TALLOC_FREE(ad);
3897 return -1;
3900 TALLOC_FREE(ad);
3901 return 0;
3904 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
3905 struct files_struct *fsp,
3906 off_t offset)
3908 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3911 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3912 struct files_struct *fsp,
3913 off_t offset)
3915 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3916 int ret;
3918 if (fio == NULL) {
3919 DBG_ERR("Failed to fetch fsp extension");
3920 return -1;
3923 switch (fio->config->rsrc) {
3924 case FRUIT_RSRC_XATTR:
3925 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
3926 break;
3928 case FRUIT_RSRC_ADFILE:
3929 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
3930 break;
3932 case FRUIT_RSRC_STREAM:
3933 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
3934 break;
3936 default:
3937 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3938 return -1;
3942 return ret;
3945 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3946 struct files_struct *fsp,
3947 off_t offset)
3949 if (offset > 60) {
3950 DBG_WARNING("ftruncate %s to %jd",
3951 fsp_str_dbg(fsp), (intmax_t)offset);
3952 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
3953 errno = EOVERFLOW;
3954 return -1;
3957 /* OS X returns success but does nothing */
3958 DBG_INFO("ignoring ftruncate %s to %jd\n",
3959 fsp_str_dbg(fsp), (intmax_t)offset);
3960 return 0;
3963 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3964 struct files_struct *fsp,
3965 off_t offset)
3967 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3968 int ret;
3970 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
3971 (intmax_t)offset);
3973 if (fio == NULL) {
3974 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3977 if (fio->type == ADOUBLE_META) {
3978 ret = fruit_ftruncate_meta(handle, fsp, offset);
3979 } else {
3980 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
3983 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
3984 return ret;
3987 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3988 struct smb_request *req,
3989 uint16_t root_dir_fid,
3990 struct smb_filename *smb_fname,
3991 uint32_t access_mask,
3992 uint32_t share_access,
3993 uint32_t create_disposition,
3994 uint32_t create_options,
3995 uint32_t file_attributes,
3996 uint32_t oplock_request,
3997 const struct smb2_lease *lease,
3998 uint64_t allocation_size,
3999 uint32_t private_flags,
4000 struct security_descriptor *sd,
4001 struct ea_list *ea_list,
4002 files_struct **result,
4003 int *pinfo,
4004 const struct smb2_create_blobs *in_context_blobs,
4005 struct smb2_create_blobs *out_context_blobs)
4007 NTSTATUS status;
4008 struct fruit_config_data *config = NULL;
4009 files_struct *fsp = NULL;
4010 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4011 int ret;
4013 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4014 if (!NT_STATUS_IS_OK(status)) {
4015 goto fail;
4018 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4019 return NT_STATUS_UNSUCCESSFUL);
4021 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4022 uint32_t conv_flags = 0;
4024 if (config->wipe_intentionally_left_blank_rfork) {
4025 conv_flags |= AD_CONV_WIPE_BLANK;
4027 if (config->delete_empty_adfiles) {
4028 conv_flags |= AD_CONV_DELETE;
4031 ret = ad_convert(handle,
4032 handle->conn->cwd_fsp,
4033 smb_fname,
4034 macos_string_replace_map,
4035 conv_flags);
4036 if (ret != 0) {
4037 DBG_ERR("ad_convert() failed\n");
4038 return NT_STATUS_UNSUCCESSFUL;
4042 status = SMB_VFS_NEXT_CREATE_FILE(
4043 handle, req, root_dir_fid, smb_fname,
4044 access_mask, share_access,
4045 create_disposition, create_options,
4046 file_attributes, oplock_request,
4047 lease,
4048 allocation_size, private_flags,
4049 sd, ea_list, result,
4050 pinfo, in_context_blobs, out_context_blobs);
4051 if (!NT_STATUS_IS_OK(status)) {
4052 return status;
4055 fsp = *result;
4057 if (global_fruit_config.nego_aapl) {
4058 if (config->posix_rename && fsp->is_directory) {
4060 * Enable POSIX directory rename behaviour
4062 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4067 * If this is a plain open for existing files, opening an 0
4068 * byte size resource fork MUST fail with
4069 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4071 * Cf the vfs_fruit torture tests in test_rfork_create().
4073 if (global_fruit_config.nego_aapl &&
4074 create_disposition == FILE_OPEN &&
4075 smb_fname->st.st_ex_size == 0 &&
4076 is_named_stream(smb_fname))
4078 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4079 goto fail;
4082 if (is_named_stream(smb_fname)
4083 || fsp->is_directory) {
4084 return status;
4087 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4088 (fsp->op != NULL))
4090 status = fruit_check_access(
4091 handle, *result,
4092 access_mask,
4093 share_access);
4094 if (!NT_STATUS_IS_OK(status)) {
4095 goto fail;
4099 return status;
4101 fail:
4102 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4104 if (fsp) {
4105 close_file(req, fsp, ERROR_CLOSE);
4106 *result = fsp = NULL;
4109 return status;
4112 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4113 const struct smb_filename *fname,
4114 TALLOC_CTX *mem_ctx,
4115 struct readdir_attr_data **pattr_data)
4117 struct fruit_config_data *config = NULL;
4118 struct readdir_attr_data *attr_data;
4119 uint32_t conv_flags = 0;
4120 NTSTATUS status;
4121 int ret;
4123 SMB_VFS_HANDLE_GET_DATA(handle, config,
4124 struct fruit_config_data,
4125 return NT_STATUS_UNSUCCESSFUL);
4127 if (!global_fruit_config.nego_aapl) {
4128 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4131 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4133 if (config->wipe_intentionally_left_blank_rfork) {
4134 conv_flags |= AD_CONV_WIPE_BLANK;
4136 if (config->delete_empty_adfiles) {
4137 conv_flags |= AD_CONV_DELETE;
4140 ret = ad_convert(handle,
4141 handle->conn->cwd_fsp,
4142 fname,
4143 macos_string_replace_map,
4144 conv_flags);
4145 if (ret != 0) {
4146 DBG_ERR("ad_convert() failed\n");
4147 return NT_STATUS_UNSUCCESSFUL;
4150 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4151 if (*pattr_data == NULL) {
4152 return NT_STATUS_UNSUCCESSFUL;
4154 attr_data = *pattr_data;
4155 attr_data->type = RDATTR_AAPL;
4158 * Mac metadata: compressed FinderInfo, resource fork length
4159 * and creation date
4161 status = readdir_attr_macmeta(handle, fname, attr_data);
4162 if (!NT_STATUS_IS_OK(status)) {
4164 * Error handling is tricky: if we return failure from
4165 * this function, the corresponding directory entry
4166 * will to be passed to the client, so we really just
4167 * want to error out on fatal errors.
4169 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4170 goto fail;
4175 * UNIX mode
4177 if (config->unix_info_enabled) {
4178 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4182 * max_access
4184 if (!config->readdir_attr_max_access) {
4185 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4186 } else {
4187 status = smbd_calculate_access_mask(
4188 handle->conn,
4189 fname,
4190 false,
4191 SEC_FLAG_MAXIMUM_ALLOWED,
4192 &attr_data->attr_data.aapl.max_access);
4193 if (!NT_STATUS_IS_OK(status)) {
4194 goto fail;
4198 return NT_STATUS_OK;
4200 fail:
4201 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4202 fname->base_name, nt_errstr(status)));
4203 TALLOC_FREE(*pattr_data);
4204 return status;
4207 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4208 files_struct *fsp,
4209 uint32_t security_info,
4210 TALLOC_CTX *mem_ctx,
4211 struct security_descriptor **ppdesc)
4213 NTSTATUS status;
4214 struct security_ace ace;
4215 struct dom_sid sid;
4216 struct fruit_config_data *config;
4218 SMB_VFS_HANDLE_GET_DATA(handle, config,
4219 struct fruit_config_data,
4220 return NT_STATUS_UNSUCCESSFUL);
4222 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4223 mem_ctx, ppdesc);
4224 if (!NT_STATUS_IS_OK(status)) {
4225 return status;
4229 * Add MS NFS style ACEs with uid, gid and mode
4231 if (!global_fruit_config.nego_aapl) {
4232 return NT_STATUS_OK;
4234 if (!config->unix_info_enabled) {
4235 return NT_STATUS_OK;
4238 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4239 status = remove_virtual_nfs_aces(*ppdesc);
4240 if (!NT_STATUS_IS_OK(status)) {
4241 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4242 return status;
4245 /* MS NFS style mode */
4246 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4247 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4248 status = security_descriptor_dacl_add(*ppdesc, &ace);
4249 if (!NT_STATUS_IS_OK(status)) {
4250 DEBUG(1,("failed to add MS NFS style ACE\n"));
4251 return status;
4254 /* MS NFS style uid */
4255 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4256 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4257 status = security_descriptor_dacl_add(*ppdesc, &ace);
4258 if (!NT_STATUS_IS_OK(status)) {
4259 DEBUG(1,("failed to add MS NFS style ACE\n"));
4260 return status;
4263 /* MS NFS style gid */
4264 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4265 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4266 status = security_descriptor_dacl_add(*ppdesc, &ace);
4267 if (!NT_STATUS_IS_OK(status)) {
4268 DEBUG(1,("failed to add MS NFS style ACE\n"));
4269 return status;
4272 return NT_STATUS_OK;
4275 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4276 files_struct *fsp,
4277 uint32_t security_info_sent,
4278 const struct security_descriptor *orig_psd)
4280 NTSTATUS status;
4281 bool do_chmod;
4282 mode_t ms_nfs_mode = 0;
4283 int result;
4284 struct security_descriptor *psd = NULL;
4285 uint32_t orig_num_aces = 0;
4287 if (orig_psd->dacl != NULL) {
4288 orig_num_aces = orig_psd->dacl->num_aces;
4291 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4292 if (psd == NULL) {
4293 return NT_STATUS_NO_MEMORY;
4296 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4298 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4299 if (!NT_STATUS_IS_OK(status)) {
4300 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4301 TALLOC_FREE(psd);
4302 return status;
4306 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4307 * sent/present flags correctly now we've removed them.
4310 if (orig_num_aces != 0) {
4312 * Are there any ACE's left ?
4314 if (psd->dacl->num_aces == 0) {
4315 /* No - clear the DACL sent/present flags. */
4316 security_info_sent &= ~SECINFO_DACL;
4317 psd->type &= ~SEC_DESC_DACL_PRESENT;
4321 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4322 if (!NT_STATUS_IS_OK(status)) {
4323 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4324 TALLOC_FREE(psd);
4325 return status;
4328 if (do_chmod) {
4329 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4330 if (result != 0) {
4331 DBG_WARNING("%s, result: %d, %04o error %s\n",
4332 fsp_str_dbg(fsp),
4333 result,
4334 (unsigned)ms_nfs_mode,
4335 strerror(errno));
4336 status = map_nt_error_from_unix(errno);
4337 TALLOC_FREE(psd);
4338 return status;
4342 TALLOC_FREE(psd);
4343 return NT_STATUS_OK;
4346 static struct vfs_offload_ctx *fruit_offload_ctx;
4348 struct fruit_offload_read_state {
4349 struct vfs_handle_struct *handle;
4350 struct tevent_context *ev;
4351 files_struct *fsp;
4352 uint32_t fsctl;
4353 DATA_BLOB token;
4356 static void fruit_offload_read_done(struct tevent_req *subreq);
4358 static struct tevent_req *fruit_offload_read_send(
4359 TALLOC_CTX *mem_ctx,
4360 struct tevent_context *ev,
4361 struct vfs_handle_struct *handle,
4362 files_struct *fsp,
4363 uint32_t fsctl,
4364 uint32_t ttl,
4365 off_t offset,
4366 size_t to_copy)
4368 struct tevent_req *req = NULL;
4369 struct tevent_req *subreq = NULL;
4370 struct fruit_offload_read_state *state = NULL;
4372 req = tevent_req_create(mem_ctx, &state,
4373 struct fruit_offload_read_state);
4374 if (req == NULL) {
4375 return NULL;
4377 *state = (struct fruit_offload_read_state) {
4378 .handle = handle,
4379 .ev = ev,
4380 .fsp = fsp,
4381 .fsctl = fsctl,
4384 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4385 fsctl, ttl, offset, to_copy);
4386 if (tevent_req_nomem(subreq, req)) {
4387 return tevent_req_post(req, ev);
4389 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4390 return req;
4393 static void fruit_offload_read_done(struct tevent_req *subreq)
4395 struct tevent_req *req = tevent_req_callback_data(
4396 subreq, struct tevent_req);
4397 struct fruit_offload_read_state *state = tevent_req_data(
4398 req, struct fruit_offload_read_state);
4399 NTSTATUS status;
4401 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4402 state->handle,
4403 state,
4404 &state->token);
4405 TALLOC_FREE(subreq);
4406 if (tevent_req_nterror(req, status)) {
4407 return;
4410 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4411 tevent_req_done(req);
4412 return;
4415 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4416 &fruit_offload_ctx);
4417 if (tevent_req_nterror(req, status)) {
4418 return;
4421 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4422 state->fsp,
4423 &state->token);
4424 if (tevent_req_nterror(req, status)) {
4425 return;
4428 tevent_req_done(req);
4429 return;
4432 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4433 struct vfs_handle_struct *handle,
4434 TALLOC_CTX *mem_ctx,
4435 DATA_BLOB *token)
4437 struct fruit_offload_read_state *state = tevent_req_data(
4438 req, struct fruit_offload_read_state);
4439 NTSTATUS status;
4441 if (tevent_req_is_nterror(req, &status)) {
4442 tevent_req_received(req);
4443 return status;
4446 token->length = state->token.length;
4447 token->data = talloc_move(mem_ctx, &state->token.data);
4449 tevent_req_received(req);
4450 return NT_STATUS_OK;
4453 struct fruit_offload_write_state {
4454 struct vfs_handle_struct *handle;
4455 off_t copied;
4456 struct files_struct *src_fsp;
4457 struct files_struct *dst_fsp;
4458 bool is_copyfile;
4461 static void fruit_offload_write_done(struct tevent_req *subreq);
4462 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4463 TALLOC_CTX *mem_ctx,
4464 struct tevent_context *ev,
4465 uint32_t fsctl,
4466 DATA_BLOB *token,
4467 off_t transfer_offset,
4468 struct files_struct *dest_fsp,
4469 off_t dest_off,
4470 off_t num)
4472 struct tevent_req *req, *subreq;
4473 struct fruit_offload_write_state *state;
4474 NTSTATUS status;
4475 struct fruit_config_data *config;
4476 off_t src_off = transfer_offset;
4477 files_struct *src_fsp = NULL;
4478 off_t to_copy = num;
4479 bool copyfile_enabled = false;
4481 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4482 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4484 SMB_VFS_HANDLE_GET_DATA(handle, config,
4485 struct fruit_config_data,
4486 return NULL);
4488 req = tevent_req_create(mem_ctx, &state,
4489 struct fruit_offload_write_state);
4490 if (req == NULL) {
4491 return NULL;
4493 state->handle = handle;
4494 state->dst_fsp = dest_fsp;
4496 switch (fsctl) {
4497 case FSCTL_SRV_COPYCHUNK:
4498 case FSCTL_SRV_COPYCHUNK_WRITE:
4499 copyfile_enabled = config->copyfile_enabled;
4500 break;
4501 default:
4502 break;
4506 * Check if this a OS X copyfile style copychunk request with
4507 * a requested chunk count of 0 that was translated to a
4508 * offload_write_send VFS call overloading the parameters src_off
4509 * = dest_off = num = 0.
4511 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4512 status = vfs_offload_token_db_fetch_fsp(
4513 fruit_offload_ctx, token, &src_fsp);
4514 if (tevent_req_nterror(req, status)) {
4515 return tevent_req_post(req, ev);
4517 state->src_fsp = src_fsp;
4519 status = vfs_stat_fsp(src_fsp);
4520 if (tevent_req_nterror(req, status)) {
4521 return tevent_req_post(req, ev);
4524 to_copy = src_fsp->fsp_name->st.st_ex_size;
4525 state->is_copyfile = true;
4528 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4529 mem_ctx,
4531 fsctl,
4532 token,
4533 transfer_offset,
4534 dest_fsp,
4535 dest_off,
4536 to_copy);
4537 if (tevent_req_nomem(subreq, req)) {
4538 return tevent_req_post(req, ev);
4541 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4542 return req;
4545 static void fruit_offload_write_done(struct tevent_req *subreq)
4547 struct tevent_req *req = tevent_req_callback_data(
4548 subreq, struct tevent_req);
4549 struct fruit_offload_write_state *state = tevent_req_data(
4550 req, struct fruit_offload_write_state);
4551 NTSTATUS status;
4552 unsigned int num_streams = 0;
4553 struct stream_struct *streams = NULL;
4554 unsigned int i;
4555 struct smb_filename *src_fname_tmp = NULL;
4556 struct smb_filename *dst_fname_tmp = NULL;
4558 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4559 subreq,
4560 &state->copied);
4561 TALLOC_FREE(subreq);
4562 if (tevent_req_nterror(req, status)) {
4563 return;
4566 if (!state->is_copyfile) {
4567 tevent_req_done(req);
4568 return;
4572 * Now copy all remaining streams. We know the share supports
4573 * streams, because we're in vfs_fruit. We don't do this async
4574 * because streams are few and small.
4576 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4577 state->src_fsp->fsp_name,
4578 req, &num_streams, &streams);
4579 if (tevent_req_nterror(req, status)) {
4580 return;
4583 if (num_streams == 1) {
4584 /* There is always one stream, ::$DATA. */
4585 tevent_req_done(req);
4586 return;
4589 for (i = 0; i < num_streams; i++) {
4590 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4591 __func__, streams[i].name, (size_t)streams[i].size));
4593 src_fname_tmp = synthetic_smb_fname(
4594 req,
4595 state->src_fsp->fsp_name->base_name,
4596 streams[i].name,
4597 NULL,
4598 state->src_fsp->fsp_name->flags);
4599 if (tevent_req_nomem(src_fname_tmp, req)) {
4600 return;
4603 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4604 TALLOC_FREE(src_fname_tmp);
4605 continue;
4608 dst_fname_tmp = synthetic_smb_fname(
4609 req,
4610 state->dst_fsp->fsp_name->base_name,
4611 streams[i].name,
4612 NULL,
4613 state->dst_fsp->fsp_name->flags);
4614 if (tevent_req_nomem(dst_fname_tmp, req)) {
4615 TALLOC_FREE(src_fname_tmp);
4616 return;
4619 status = copy_file(req,
4620 state->handle->conn,
4621 src_fname_tmp,
4622 dst_fname_tmp,
4623 OPENX_FILE_CREATE_IF_NOT_EXIST,
4624 0, false);
4625 if (!NT_STATUS_IS_OK(status)) {
4626 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4627 smb_fname_str_dbg(src_fname_tmp),
4628 smb_fname_str_dbg(dst_fname_tmp),
4629 nt_errstr(status)));
4630 TALLOC_FREE(src_fname_tmp);
4631 TALLOC_FREE(dst_fname_tmp);
4632 tevent_req_nterror(req, status);
4633 return;
4636 TALLOC_FREE(src_fname_tmp);
4637 TALLOC_FREE(dst_fname_tmp);
4640 TALLOC_FREE(streams);
4641 TALLOC_FREE(src_fname_tmp);
4642 TALLOC_FREE(dst_fname_tmp);
4643 tevent_req_done(req);
4646 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4647 struct tevent_req *req,
4648 off_t *copied)
4650 struct fruit_offload_write_state *state = tevent_req_data(
4651 req, struct fruit_offload_write_state);
4652 NTSTATUS status;
4654 if (tevent_req_is_nterror(req, &status)) {
4655 DEBUG(1, ("server side copy chunk failed: %s\n",
4656 nt_errstr(status)));
4657 *copied = 0;
4658 tevent_req_received(req);
4659 return status;
4662 *copied = state->copied;
4663 tevent_req_received(req);
4665 return NT_STATUS_OK;
4668 static char *fruit_get_bandsize_line(char **lines, int numlines)
4670 static regex_t re;
4671 static bool re_initialized = false;
4672 int i;
4673 int ret;
4675 if (!re_initialized) {
4676 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4677 if (ret != 0) {
4678 return NULL;
4680 re_initialized = true;
4683 for (i = 0; i < numlines; i++) {
4684 regmatch_t matches[1];
4686 ret = regexec(&re, lines[i], 1, matches, 0);
4687 if (ret == 0) {
4689 * Check if the match was on the last line, sa we want
4690 * the subsequent line.
4692 if (i + 1 == numlines) {
4693 return NULL;
4695 return lines[i + 1];
4697 if (ret != REG_NOMATCH) {
4698 return NULL;
4702 return NULL;
4705 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4707 static regex_t re;
4708 static bool re_initialized = false;
4709 regmatch_t matches[2];
4710 uint64_t band_size;
4711 int ret;
4712 bool ok;
4714 if (!re_initialized) {
4715 ret = regcomp(&re,
4716 "^[[:blank:]]*"
4717 "<integer>\\([[:digit:]]*\\)</integer>$",
4719 if (ret != 0) {
4720 return false;
4722 re_initialized = true;
4725 ret = regexec(&re, line, 2, matches, 0);
4726 if (ret != 0) {
4727 DBG_ERR("regex failed [%s]\n", line);
4728 return false;
4731 line[matches[1].rm_eo] = '\0';
4733 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4734 if (!ok) {
4735 return false;
4737 *_band_size = (size_t)band_size;
4738 return true;
4742 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4743 * "band-size" key and value.
4745 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4746 const char *dir,
4747 size_t *band_size)
4749 #define INFO_PLIST_MAX_SIZE 64*1024
4750 char *plist = NULL;
4751 struct smb_filename *smb_fname = NULL;
4752 files_struct *fsp = NULL;
4753 uint8_t *file_data = NULL;
4754 char **lines = NULL;
4755 char *band_size_line = NULL;
4756 size_t plist_file_size;
4757 ssize_t nread;
4758 int numlines;
4759 int ret;
4760 bool ok = false;
4761 NTSTATUS status;
4763 plist = talloc_asprintf(talloc_tos(),
4764 "%s/%s/Info.plist",
4765 handle->conn->connectpath,
4766 dir);
4767 if (plist == NULL) {
4768 ok = false;
4769 goto out;
4772 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
4773 if (smb_fname == NULL) {
4774 ok = false;
4775 goto out;
4778 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4779 if (ret != 0) {
4780 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4781 ok = true;
4782 goto out;
4785 plist_file_size = smb_fname->st.st_ex_size;
4787 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4788 DBG_INFO("%s is too large, ignoring\n", plist);
4789 ok = true;
4790 goto out;
4793 status = SMB_VFS_NEXT_CREATE_FILE(
4794 handle, /* conn */
4795 NULL, /* req */
4796 0, /* root_dir_fid */
4797 smb_fname, /* fname */
4798 FILE_GENERIC_READ, /* access_mask */
4799 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4800 FILE_OPEN, /* create_disposition */
4801 0, /* create_options */
4802 0, /* file_attributes */
4803 INTERNAL_OPEN_ONLY, /* oplock_request */
4804 NULL, /* lease */
4805 0, /* allocation_size */
4806 0, /* private_flags */
4807 NULL, /* sd */
4808 NULL, /* ea_list */
4809 &fsp, /* result */
4810 NULL, /* psbuf */
4811 NULL, NULL); /* create context */
4812 if (!NT_STATUS_IS_OK(status)) {
4813 DBG_INFO("Opening [%s] failed [%s]\n",
4814 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4815 ok = false;
4816 goto out;
4819 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
4820 if (file_data == NULL) {
4821 ok = false;
4822 goto out;
4825 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4826 if (nread != plist_file_size) {
4827 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4828 fsp_str_dbg(fsp), nread, plist_file_size);
4829 ok = false;
4830 goto out;
4834 status = close_file(NULL, fsp, NORMAL_CLOSE);
4835 fsp = NULL;
4836 if (!NT_STATUS_IS_OK(status)) {
4837 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4838 ok = false;
4839 goto out;
4842 lines = file_lines_parse((char *)file_data,
4843 plist_file_size,
4844 &numlines,
4845 talloc_tos());
4846 if (lines == NULL) {
4847 ok = false;
4848 goto out;
4851 band_size_line = fruit_get_bandsize_line(lines, numlines);
4852 if (band_size_line == NULL) {
4853 DBG_ERR("Didn't find band-size key in [%s]\n",
4854 smb_fname_str_dbg(smb_fname));
4855 ok = false;
4856 goto out;
4859 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
4860 if (!ok) {
4861 DBG_ERR("fruit_get_bandsize_from_line failed\n");
4862 goto out;
4865 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
4867 out:
4868 if (fsp != NULL) {
4869 status = close_file(NULL, fsp, NORMAL_CLOSE);
4870 if (!NT_STATUS_IS_OK(status)) {
4871 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4873 fsp = NULL;
4875 TALLOC_FREE(plist);
4876 TALLOC_FREE(smb_fname);
4877 TALLOC_FREE(file_data);
4878 TALLOC_FREE(lines);
4879 return ok;
4882 struct fruit_disk_free_state {
4883 off_t total_size;
4886 static bool fruit_get_num_bands(vfs_handle_struct *handle,
4887 char *bundle,
4888 size_t *_nbands)
4890 char *path = NULL;
4891 struct smb_filename *bands_dir = NULL;
4892 DIR *d = NULL;
4893 struct dirent *e = NULL;
4894 size_t nbands;
4895 int ret;
4897 path = talloc_asprintf(talloc_tos(),
4898 "%s/%s/bands",
4899 handle->conn->connectpath,
4900 bundle);
4901 if (path == NULL) {
4902 return false;
4905 bands_dir = synthetic_smb_fname(talloc_tos(),
4906 path,
4907 NULL,
4908 NULL,
4910 TALLOC_FREE(path);
4911 if (bands_dir == NULL) {
4912 return false;
4915 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
4916 if (d == NULL) {
4917 TALLOC_FREE(bands_dir);
4918 return false;
4921 nbands = 0;
4923 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
4924 e != NULL;
4925 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
4927 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
4928 continue;
4930 nbands++;
4933 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
4934 if (ret != 0) {
4935 TALLOC_FREE(bands_dir);
4936 return false;
4939 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
4941 TALLOC_FREE(bands_dir);
4943 *_nbands = nbands;
4944 return true;
4947 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
4948 struct fruit_disk_free_state *state,
4949 struct dirent *e)
4951 bool ok;
4952 char *p = NULL;
4953 size_t sparsebundle_strlen = strlen("sparsebundle");
4954 size_t bandsize = 0;
4955 size_t nbands;
4956 off_t tm_size;
4958 p = strstr(e->d_name, "sparsebundle");
4959 if (p == NULL) {
4960 return true;
4963 if (p[sparsebundle_strlen] != '\0') {
4964 return true;
4967 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
4969 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
4970 if (!ok) {
4972 * Beware of race conditions: this may be an uninitialized
4973 * Info.plist that a client is just creating. We don't want let
4974 * this to trigger complete failure.
4976 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
4977 return true;
4980 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
4981 if (!ok) {
4983 * Beware of race conditions: this may be a backup sparsebundle
4984 * in an early stage lacking a bands subdirectory. We don't want
4985 * let this to trigger complete failure.
4987 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
4988 return true;
4992 * Arithmetic on 32-bit systems may cause overflow, depending on
4993 * size_t precision. First we check its unlikely, then we
4994 * force the precision into target off_t, then we check that
4995 * the total did not overflow either.
4997 if (bandsize > SIZE_MAX/nbands) {
4998 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
4999 bandsize, nbands);
5000 return false;
5002 tm_size = (off_t)bandsize * (off_t)nbands;
5004 if (state->total_size + tm_size < state->total_size) {
5005 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5006 bandsize, nbands);
5007 return false;
5010 state->total_size += tm_size;
5012 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5013 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
5015 return true;
5019 * Calculate used size of a TimeMachine volume
5021 * This assumes that the volume is used only for TimeMachine.
5023 * - readdir(basedir of share), then
5024 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5025 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5026 * - count band files in "\1.sparsebundle/bands/"
5027 * - calculate used size of all bands: band_count * band_size
5029 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5030 const struct smb_filename *smb_fname,
5031 uint64_t *_bsize,
5032 uint64_t *_dfree,
5033 uint64_t *_dsize)
5035 struct fruit_config_data *config = NULL;
5036 struct fruit_disk_free_state state = {0};
5037 DIR *d = NULL;
5038 struct dirent *e = NULL;
5039 uint64_t dfree;
5040 uint64_t dsize;
5041 int ret;
5042 bool ok;
5044 SMB_VFS_HANDLE_GET_DATA(handle, config,
5045 struct fruit_config_data,
5046 return UINT64_MAX);
5048 if (!config->time_machine ||
5049 config->time_machine_max_size == 0)
5051 return SMB_VFS_NEXT_DISK_FREE(handle,
5052 smb_fname,
5053 _bsize,
5054 _dfree,
5055 _dsize);
5058 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
5059 if (d == NULL) {
5060 return UINT64_MAX;
5063 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
5064 e != NULL;
5065 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
5067 ok = fruit_tmsize_do_dirent(handle, &state, e);
5068 if (!ok) {
5069 SMB_VFS_NEXT_CLOSEDIR(handle, d);
5070 return UINT64_MAX;
5074 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
5075 if (ret != 0) {
5076 return UINT64_MAX;
5079 dsize = config->time_machine_max_size / 512;
5080 dfree = dsize - (state.total_size / 512);
5081 if (dfree > dsize) {
5082 dfree = 0;
5085 *_bsize = 512;
5086 *_dsize = dsize;
5087 *_dfree = dfree;
5088 return dfree / 2;
5091 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5092 const SMB_STRUCT_STAT *psbuf)
5094 struct fruit_config_data *config = NULL;
5096 SMB_VFS_HANDLE_GET_DATA(handle, config,
5097 struct fruit_config_data,
5098 return 0);
5100 if (global_fruit_config.nego_aapl &&
5101 config->aapl_zero_file_id)
5103 return 0;
5106 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5109 static struct vfs_fn_pointers vfs_fruit_fns = {
5110 .connect_fn = fruit_connect,
5111 .disk_free_fn = fruit_disk_free,
5113 /* File operations */
5114 .chmod_fn = fruit_chmod,
5115 .unlinkat_fn = fruit_unlinkat,
5116 .renameat_fn = fruit_renameat,
5117 .open_fn = fruit_open,
5118 .close_fn = fruit_close,
5119 .pread_fn = fruit_pread,
5120 .pwrite_fn = fruit_pwrite,
5121 .pread_send_fn = fruit_pread_send,
5122 .pread_recv_fn = fruit_pread_recv,
5123 .pwrite_send_fn = fruit_pwrite_send,
5124 .pwrite_recv_fn = fruit_pwrite_recv,
5125 .stat_fn = fruit_stat,
5126 .lstat_fn = fruit_lstat,
5127 .fstat_fn = fruit_fstat,
5128 .streaminfo_fn = fruit_streaminfo,
5129 .ntimes_fn = fruit_ntimes,
5130 .ftruncate_fn = fruit_ftruncate,
5131 .fallocate_fn = fruit_fallocate,
5132 .create_file_fn = fruit_create_file,
5133 .readdir_attr_fn = fruit_readdir_attr,
5134 .offload_read_send_fn = fruit_offload_read_send,
5135 .offload_read_recv_fn = fruit_offload_read_recv,
5136 .offload_write_send_fn = fruit_offload_write_send,
5137 .offload_write_recv_fn = fruit_offload_write_recv,
5138 .fs_file_id_fn = fruit_fs_file_id,
5140 /* NT ACL operations */
5141 .fget_nt_acl_fn = fruit_fget_nt_acl,
5142 .fset_nt_acl_fn = fruit_fset_nt_acl,
5145 static_decl_vfs;
5146 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5148 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5149 &vfs_fruit_fns);
5150 if (!NT_STATUS_IS_OK(ret)) {
5151 return ret;
5154 vfs_fruit_debug_level = debug_add_class("fruit");
5155 if (vfs_fruit_debug_level == -1) {
5156 vfs_fruit_debug_level = DBGC_VFS;
5157 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5158 "vfs_fruit_init"));
5159 } else {
5160 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5161 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5164 return ret;