vfs_io_uring: move error handling out of vfs_io_uring_pread_recv()
[Samba.git] / source3 / modules / vfs_fruit.c
blobb2d0901a80076f20ad363425f8821c392c69d06d
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;
181 /* Whether the create created the stream */
182 bool created;
185 * AFP_AfpInfo stream created, but not written yet, thus still a fake
186 * pipe fd. This is set to true in fruit_open_meta if there was no
187 * existing stream but the caller requested O_CREAT. It is later set to
188 * false when we get a write on the stream that then does open and
189 * create the stream.
191 bool fake_fd;
192 int flags;
193 int mode;
196 /*****************************************************************************
197 * Helper functions
198 *****************************************************************************/
201 * Initialize config struct from our smb.conf config parameters
203 static int init_fruit_config(vfs_handle_struct *handle)
205 struct fruit_config_data *config;
206 int enumval;
207 const char *tm_size_str = NULL;
209 config = talloc_zero(handle->conn, struct fruit_config_data);
210 if (!config) {
211 DEBUG(1, ("talloc_zero() failed\n"));
212 errno = ENOMEM;
213 return -1;
217 * Versions up to Samba 4.5.x had a spelling bug in the
218 * fruit:resource option calling lp_parm_enum with
219 * "res*s*ource" (ie two s).
221 * In Samba 4.6 we accept both the wrong and the correct
222 * spelling, in Samba 4.7 the bad spelling will be removed.
224 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
225 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
226 if (enumval == -1) {
227 DEBUG(1, ("value for %s: resource type unknown\n",
228 FRUIT_PARAM_TYPE_NAME));
229 return -1;
231 config->rsrc = (enum fruit_rsrc)enumval;
233 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
234 "resource", fruit_rsrc, enumval);
235 if (enumval == -1) {
236 DEBUG(1, ("value for %s: resource type unknown\n",
237 FRUIT_PARAM_TYPE_NAME));
238 return -1;
240 config->rsrc = (enum fruit_rsrc)enumval;
242 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
243 "metadata", fruit_meta, FRUIT_META_NETATALK);
244 if (enumval == -1) {
245 DEBUG(1, ("value for %s: metadata type unknown\n",
246 FRUIT_PARAM_TYPE_NAME));
247 return -1;
249 config->meta = (enum fruit_meta)enumval;
251 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
252 "locking", fruit_locking, FRUIT_LOCKING_NONE);
253 if (enumval == -1) {
254 DEBUG(1, ("value for %s: locking type unknown\n",
255 FRUIT_PARAM_TYPE_NAME));
256 return -1;
258 config->locking = (enum fruit_locking)enumval;
260 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
261 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
262 if (enumval == -1) {
263 DEBUG(1, ("value for %s: encoding type unknown\n",
264 FRUIT_PARAM_TYPE_NAME));
265 return -1;
267 config->encoding = (enum fruit_encoding)enumval;
269 if (config->rsrc == FRUIT_RSRC_ADFILE) {
270 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
271 FRUIT_PARAM_TYPE_NAME,
272 "veto_appledouble",
273 true);
276 config->use_aapl = lp_parm_bool(
277 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
279 config->time_machine = lp_parm_bool(
280 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
282 config->unix_info_enabled = lp_parm_bool(
283 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
285 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
286 "copyfile", false);
288 config->posix_rename = lp_parm_bool(
289 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
291 config->aapl_zero_file_id =
292 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
293 "zero_file_id", false);
295 config->readdir_attr_rsize = lp_parm_bool(
296 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
298 config->readdir_attr_finder_info = lp_parm_bool(
299 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
301 config->readdir_attr_max_access = lp_parm_bool(
302 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
304 config->model = lp_parm_const_string(
305 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
307 tm_size_str = lp_parm_const_string(
308 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
309 "time machine max size", NULL);
310 if (tm_size_str != NULL) {
311 config->time_machine_max_size = conv_str_size(tm_size_str);
314 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
315 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
316 "wipe_intentionally_left_blank_rfork", false);
318 config->delete_empty_adfiles = lp_parm_bool(
319 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
320 "delete_empty_adfiles", false);
322 SMB_VFS_HANDLE_SET_DATA(handle, config,
323 NULL, struct fruit_config_data,
324 return -1);
326 return 0;
329 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
330 struct stream_struct **streams,
331 const char *name, off_t size,
332 off_t alloc_size)
334 struct stream_struct *tmp;
336 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
337 (*num_streams)+1);
338 if (tmp == NULL) {
339 return false;
342 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
343 if (tmp[*num_streams].name == NULL) {
344 return false;
347 tmp[*num_streams].size = size;
348 tmp[*num_streams].alloc_size = alloc_size;
350 *streams = tmp;
351 *num_streams += 1;
352 return true;
355 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
356 struct stream_struct **streams)
358 struct stream_struct *tmp = *streams;
359 unsigned int i;
361 if (*num_streams == 0) {
362 return true;
365 for (i = 0; i < *num_streams; i++) {
366 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
367 break;
371 if (i == *num_streams) {
372 return true;
375 if (tmp[i].size > 0) {
376 return true;
379 TALLOC_FREE(tmp[i].name);
380 if (*num_streams - 1 > i) {
381 memmove(&tmp[i], &tmp[i+1],
382 (*num_streams - i - 1) * sizeof(struct stream_struct));
385 *num_streams -= 1;
386 return true;
389 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
390 struct stream_struct **streams,
391 const char *name)
393 struct stream_struct *tmp = *streams;
394 unsigned int i;
396 if (*num_streams == 0) {
397 return true;
400 for (i = 0; i < *num_streams; i++) {
401 if (strequal_m(tmp[i].name, name)) {
402 break;
406 if (i == *num_streams) {
407 return true;
410 TALLOC_FREE(tmp[i].name);
411 if (*num_streams - 1 > i) {
412 memmove(&tmp[i], &tmp[i+1],
413 (*num_streams - i - 1) * sizeof(struct stream_struct));
416 *num_streams -= 1;
417 return true;
420 static bool ad_empty_finderinfo(const struct adouble *ad)
422 int cmp;
423 char emptybuf[ADEDLEN_FINDERI] = {0};
424 char *fi = NULL;
426 fi = ad_get_entry(ad, ADEID_FINDERI);
427 if (fi == NULL) {
428 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
429 return false;
432 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
433 return (cmp == 0);
436 static bool ai_empty_finderinfo(const AfpInfo *ai)
438 int cmp;
439 char emptybuf[ADEDLEN_FINDERI] = {0};
441 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
442 return (cmp == 0);
446 * Update btime with btime from Netatalk
448 static void update_btime(vfs_handle_struct *handle,
449 struct smb_filename *smb_fname)
451 uint32_t t;
452 struct timespec creation_time = {0};
453 struct adouble *ad;
454 struct fruit_config_data *config = NULL;
456 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
457 return);
459 switch (config->meta) {
460 case FRUIT_META_STREAM:
461 return;
462 case FRUIT_META_NETATALK:
463 /* Handled below */
464 break;
465 default:
466 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
467 return;
470 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
471 if (ad == NULL) {
472 return;
474 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
475 TALLOC_FREE(ad);
476 return;
478 TALLOC_FREE(ad);
480 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
481 update_stat_ex_create_time(&smb_fname->st, creation_time);
483 return;
487 * Map an access mask to a Netatalk single byte byte range lock
489 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
490 uint32_t access_mask)
492 off_t offset;
494 switch (access_mask) {
495 case FILE_READ_DATA:
496 offset = AD_FILELOCK_OPEN_RD;
497 break;
499 case FILE_WRITE_DATA:
500 case FILE_APPEND_DATA:
501 offset = AD_FILELOCK_OPEN_WR;
502 break;
504 default:
505 offset = AD_FILELOCK_OPEN_NONE;
506 break;
509 if (fork_type == APPLE_FORK_RSRC) {
510 if (offset == AD_FILELOCK_OPEN_NONE) {
511 offset = AD_FILELOCK_RSRC_OPEN_NONE;
512 } else {
513 offset += 2;
517 return offset;
521 * Map a deny mode to a Netatalk brl
523 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
524 uint32_t deny_mode)
526 off_t offset = 0;
528 switch (deny_mode) {
529 case DENY_READ:
530 offset = AD_FILELOCK_DENY_RD;
531 break;
533 case DENY_WRITE:
534 offset = AD_FILELOCK_DENY_WR;
535 break;
537 default:
538 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
541 if (fork_type == APPLE_FORK_RSRC) {
542 offset += 2;
545 return offset;
549 * Call fcntl() with an exclusive F_GETLK request in order to
550 * determine if there's an existing shared lock
552 * @return true if the requested lock was found or any error occurred
553 * false if the lock was not found
555 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
557 bool result;
558 off_t offset = in_offset;
559 off_t len = 1;
560 int type = F_WRLCK;
561 pid_t pid = 0;
563 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
564 if (result == false) {
565 return true;
568 if (type != F_UNLCK) {
569 return true;
572 return false;
575 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
576 files_struct *fsp,
577 uint32_t access_mask,
578 uint32_t share_mode)
580 NTSTATUS status = NT_STATUS_OK;
581 off_t off;
582 bool share_for_read = (share_mode & FILE_SHARE_READ);
583 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
584 bool netatalk_already_open_for_reading = false;
585 bool netatalk_already_open_for_writing = false;
586 bool netatalk_already_open_with_deny_read = false;
587 bool netatalk_already_open_with_deny_write = false;
588 struct GUID req_guid = GUID_random();
590 /* FIXME: hardcoded data fork, add resource fork */
591 enum apple_fork fork_type = APPLE_FORK_DATA;
593 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
594 fsp_str_dbg(fsp),
595 access_mask & FILE_READ_DATA ? "READ" :"-",
596 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
597 share_mode);
599 if (fsp->fh->fd == -1) {
600 return NT_STATUS_OK;
603 /* Read NetATalk opens and deny modes on the file. */
604 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
605 access_to_netatalk_brl(fork_type,
606 FILE_READ_DATA));
608 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
609 denymode_to_netatalk_brl(fork_type,
610 DENY_READ));
612 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
613 access_to_netatalk_brl(fork_type,
614 FILE_WRITE_DATA));
616 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
617 denymode_to_netatalk_brl(fork_type,
618 DENY_WRITE));
620 /* If there are any conflicts - sharing violation. */
621 if ((access_mask & FILE_READ_DATA) &&
622 netatalk_already_open_with_deny_read) {
623 return NT_STATUS_SHARING_VIOLATION;
626 if (!share_for_read &&
627 netatalk_already_open_for_reading) {
628 return NT_STATUS_SHARING_VIOLATION;
631 if ((access_mask & FILE_WRITE_DATA) &&
632 netatalk_already_open_with_deny_write) {
633 return NT_STATUS_SHARING_VIOLATION;
636 if (!share_for_write &&
637 netatalk_already_open_for_writing) {
638 return NT_STATUS_SHARING_VIOLATION;
641 if (!(access_mask & FILE_READ_DATA)) {
643 * Nothing we can do here, we need read access
644 * to set locks.
646 return NT_STATUS_OK;
649 /* Set NetAtalk locks matching our access */
650 if (access_mask & FILE_READ_DATA) {
651 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
652 req_guid.time_hi_and_version = __LINE__;
653 status = do_lock(
654 fsp,
655 talloc_tos(),
656 &req_guid,
657 fsp->op->global->open_persistent_id,
659 off,
660 READ_LOCK,
661 POSIX_LOCK,
662 NULL,
663 NULL);
665 if (!NT_STATUS_IS_OK(status)) {
666 return status;
670 if (!share_for_read) {
671 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
672 req_guid.time_hi_and_version = __LINE__;
673 status = do_lock(
674 fsp,
675 talloc_tos(),
676 &req_guid,
677 fsp->op->global->open_persistent_id,
679 off,
680 READ_LOCK,
681 POSIX_LOCK,
682 NULL,
683 NULL);
685 if (!NT_STATUS_IS_OK(status)) {
686 return status;
690 if (access_mask & FILE_WRITE_DATA) {
691 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
692 req_guid.time_hi_and_version = __LINE__;
693 status = do_lock(
694 fsp,
695 talloc_tos(),
696 &req_guid,
697 fsp->op->global->open_persistent_id,
699 off,
700 READ_LOCK,
701 POSIX_LOCK,
702 NULL,
703 NULL);
705 if (!NT_STATUS_IS_OK(status)) {
706 return status;
710 if (!share_for_write) {
711 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
712 req_guid.time_hi_and_version = __LINE__;
713 status = do_lock(
714 fsp,
715 talloc_tos(),
716 &req_guid,
717 fsp->op->global->open_persistent_id,
719 off,
720 READ_LOCK,
721 POSIX_LOCK,
722 NULL,
723 NULL);
725 if (!NT_STATUS_IS_OK(status)) {
726 return status;
730 return NT_STATUS_OK;
733 static NTSTATUS check_aapl(vfs_handle_struct *handle,
734 struct smb_request *req,
735 const struct smb2_create_blobs *in_context_blobs,
736 struct smb2_create_blobs *out_context_blobs)
738 struct fruit_config_data *config;
739 NTSTATUS status;
740 struct smb2_create_blob *aapl = NULL;
741 uint32_t cmd;
742 bool ok;
743 uint8_t p[16];
744 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
745 uint64_t req_bitmap, client_caps;
746 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
747 smb_ucs2_t *model;
748 size_t modellen;
750 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
751 return NT_STATUS_UNSUCCESSFUL);
753 if (!config->use_aapl
754 || in_context_blobs == NULL
755 || out_context_blobs == NULL) {
756 return NT_STATUS_OK;
759 aapl = smb2_create_blob_find(in_context_blobs,
760 SMB2_CREATE_TAG_AAPL);
761 if (aapl == NULL) {
762 return NT_STATUS_OK;
765 if (aapl->data.length != 24) {
766 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
767 (uintmax_t)aapl->data.length));
768 return NT_STATUS_INVALID_PARAMETER;
771 cmd = IVAL(aapl->data.data, 0);
772 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
773 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
774 return NT_STATUS_INVALID_PARAMETER;
777 req_bitmap = BVAL(aapl->data.data, 8);
778 client_caps = BVAL(aapl->data.data, 16);
780 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
781 SIVAL(p, 4, 0);
782 SBVAL(p, 8, req_bitmap);
783 ok = data_blob_append(req, &blob, p, 16);
784 if (!ok) {
785 return NT_STATUS_UNSUCCESSFUL;
788 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
789 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
790 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
791 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
792 config->readdir_attr_enabled = true;
795 if (config->use_copyfile) {
796 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
797 config->copyfile_enabled = true;
801 * The client doesn't set the flag, so we can't check
802 * for it and just set it unconditionally
804 if (config->unix_info_enabled) {
805 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
808 SBVAL(p, 0, server_caps);
809 ok = data_blob_append(req, &blob, p, 8);
810 if (!ok) {
811 return NT_STATUS_UNSUCCESSFUL;
815 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
816 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
817 uint64_t caps = 0;
819 switch (val) {
820 case Auto:
821 break;
823 case True:
824 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
825 break;
827 default:
828 break;
831 if (config->time_machine) {
832 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
835 SBVAL(p, 0, caps);
837 ok = data_blob_append(req, &blob, p, 8);
838 if (!ok) {
839 return NT_STATUS_UNSUCCESSFUL;
843 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
844 ok = convert_string_talloc(req,
845 CH_UNIX, CH_UTF16LE,
846 config->model, strlen(config->model),
847 &model, &modellen);
848 if (!ok) {
849 return NT_STATUS_UNSUCCESSFUL;
852 SIVAL(p, 0, 0);
853 SIVAL(p + 4, 0, modellen);
854 ok = data_blob_append(req, &blob, p, 8);
855 if (!ok) {
856 talloc_free(model);
857 return NT_STATUS_UNSUCCESSFUL;
860 ok = data_blob_append(req, &blob, model, modellen);
861 talloc_free(model);
862 if (!ok) {
863 return NT_STATUS_UNSUCCESSFUL;
867 status = smb2_create_blob_add(out_context_blobs,
868 out_context_blobs,
869 SMB2_CREATE_TAG_AAPL,
870 blob);
871 if (NT_STATUS_IS_OK(status)) {
872 global_fruit_config.nego_aapl = true;
875 return status;
878 static bool readdir_attr_meta_finderi_stream(
879 struct vfs_handle_struct *handle,
880 const struct smb_filename *smb_fname,
881 AfpInfo *ai)
883 struct smb_filename *stream_name = NULL;
884 files_struct *fsp = NULL;
885 ssize_t nread;
886 NTSTATUS status;
887 int ret;
888 bool ok;
889 uint8_t buf[AFP_INFO_SIZE];
891 stream_name = synthetic_smb_fname(talloc_tos(),
892 smb_fname->base_name,
893 AFPINFO_STREAM_NAME,
894 NULL, smb_fname->flags);
895 if (stream_name == NULL) {
896 return false;
899 ret = SMB_VFS_STAT(handle->conn, stream_name);
900 if (ret != 0) {
901 return false;
904 status = SMB_VFS_CREATE_FILE(
905 handle->conn, /* conn */
906 NULL, /* req */
907 0, /* root_dir_fid */
908 stream_name, /* fname */
909 FILE_READ_DATA, /* access_mask */
910 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
911 FILE_SHARE_DELETE),
912 FILE_OPEN, /* create_disposition*/
913 0, /* create_options */
914 0, /* file_attributes */
915 INTERNAL_OPEN_ONLY, /* oplock_request */
916 NULL, /* lease */
917 0, /* allocation_size */
918 0, /* private_flags */
919 NULL, /* sd */
920 NULL, /* ea_list */
921 &fsp, /* result */
922 NULL, /* pinfo */
923 NULL, NULL); /* create context */
925 TALLOC_FREE(stream_name);
927 if (!NT_STATUS_IS_OK(status)) {
928 return false;
931 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
932 if (nread != AFP_INFO_SIZE) {
933 DBG_ERR("short read [%s] [%zd/%d]\n",
934 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
935 ok = false;
936 goto fail;
939 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
940 AFP_FinderSize);
942 ok = true;
944 fail:
945 if (fsp != NULL) {
946 close_file(NULL, fsp, NORMAL_CLOSE);
949 return ok;
952 static bool readdir_attr_meta_finderi_netatalk(
953 struct vfs_handle_struct *handle,
954 const struct smb_filename *smb_fname,
955 AfpInfo *ai)
957 struct adouble *ad = NULL;
958 char *p = NULL;
960 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
961 if (ad == NULL) {
962 return false;
965 p = ad_get_entry(ad, ADEID_FINDERI);
966 if (p == NULL) {
967 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
968 TALLOC_FREE(ad);
969 return false;
972 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
973 TALLOC_FREE(ad);
974 return true;
977 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
978 const struct smb_filename *smb_fname,
979 struct readdir_attr_data *attr_data)
981 struct fruit_config_data *config = NULL;
982 uint32_t date_added;
983 AfpInfo ai = {0};
984 bool ok;
986 SMB_VFS_HANDLE_GET_DATA(handle, config,
987 struct fruit_config_data,
988 return false);
990 switch (config->meta) {
991 case FRUIT_META_NETATALK:
992 ok = readdir_attr_meta_finderi_netatalk(
993 handle, smb_fname, &ai);
994 break;
996 case FRUIT_META_STREAM:
997 ok = readdir_attr_meta_finderi_stream(
998 handle, smb_fname, &ai);
999 break;
1001 default:
1002 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1003 return false;
1006 if (!ok) {
1007 /* Don't bother with errors, it's likely ENOENT */
1008 return true;
1011 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1012 /* finder_type */
1013 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1014 &ai.afpi_FinderInfo[0], 4);
1016 /* finder_creator */
1017 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1018 &ai.afpi_FinderInfo[4], 4);
1021 /* finder_flags */
1022 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1023 &ai.afpi_FinderInfo[8], 2);
1025 /* finder_ext_flags */
1026 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1027 &ai.afpi_FinderInfo[24], 2);
1029 /* creation date */
1030 date_added = convert_time_t_to_uint32_t(
1031 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1033 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1035 return true;
1038 static uint64_t readdir_attr_rfork_size_adouble(
1039 struct vfs_handle_struct *handle,
1040 const struct smb_filename *smb_fname)
1042 struct adouble *ad = NULL;
1043 uint64_t rfork_size;
1045 ad = ad_get(talloc_tos(), handle, smb_fname,
1046 ADOUBLE_RSRC);
1047 if (ad == NULL) {
1048 return 0;
1051 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1052 TALLOC_FREE(ad);
1054 return rfork_size;
1057 static uint64_t readdir_attr_rfork_size_stream(
1058 struct vfs_handle_struct *handle,
1059 const struct smb_filename *smb_fname)
1061 struct smb_filename *stream_name = NULL;
1062 int ret;
1063 uint64_t rfork_size;
1065 stream_name = synthetic_smb_fname(talloc_tos(),
1066 smb_fname->base_name,
1067 AFPRESOURCE_STREAM_NAME,
1068 NULL, 0);
1069 if (stream_name == NULL) {
1070 return 0;
1073 ret = SMB_VFS_STAT(handle->conn, stream_name);
1074 if (ret != 0) {
1075 TALLOC_FREE(stream_name);
1076 return 0;
1079 rfork_size = stream_name->st.st_ex_size;
1080 TALLOC_FREE(stream_name);
1082 return rfork_size;
1085 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1086 const struct smb_filename *smb_fname)
1088 struct fruit_config_data *config = NULL;
1089 uint64_t rfork_size;
1091 SMB_VFS_HANDLE_GET_DATA(handle, config,
1092 struct fruit_config_data,
1093 return 0);
1095 switch (config->rsrc) {
1096 case FRUIT_RSRC_ADFILE:
1097 rfork_size = readdir_attr_rfork_size_adouble(handle,
1098 smb_fname);
1099 break;
1101 case FRUIT_RSRC_XATTR:
1102 case FRUIT_RSRC_STREAM:
1103 rfork_size = readdir_attr_rfork_size_stream(handle,
1104 smb_fname);
1105 break;
1107 default:
1108 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1109 rfork_size = 0;
1110 break;
1113 return rfork_size;
1116 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1117 const struct smb_filename *smb_fname,
1118 struct readdir_attr_data *attr_data)
1120 NTSTATUS status = NT_STATUS_OK;
1121 struct fruit_config_data *config = NULL;
1122 bool ok;
1124 SMB_VFS_HANDLE_GET_DATA(handle, config,
1125 struct fruit_config_data,
1126 return NT_STATUS_UNSUCCESSFUL);
1129 /* Ensure we return a default value in the creation_date field */
1130 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1133 * Resource fork length
1136 if (config->readdir_attr_rsize) {
1137 uint64_t rfork_size;
1139 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1140 attr_data->attr_data.aapl.rfork_size = rfork_size;
1144 * FinderInfo
1147 if (config->readdir_attr_finder_info) {
1148 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1149 if (!ok) {
1150 status = NT_STATUS_INTERNAL_ERROR;
1154 return status;
1157 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1159 NTSTATUS status;
1160 uint32_t i;
1162 if (psd->dacl == NULL) {
1163 return NT_STATUS_OK;
1166 for (i = 0; i < psd->dacl->num_aces; i++) {
1167 /* MS NFS style mode/uid/gid */
1168 int cmp = dom_sid_compare_domain(
1169 &global_sid_Unix_NFS,
1170 &psd->dacl->aces[i].trustee);
1171 if (cmp != 0) {
1172 /* Normal ACE entry. */
1173 continue;
1177 * security_descriptor_dacl_del()
1178 * *must* return NT_STATUS_OK as we know
1179 * we have something to remove.
1182 status = security_descriptor_dacl_del(psd,
1183 &psd->dacl->aces[i].trustee);
1184 if (!NT_STATUS_IS_OK(status)) {
1185 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1186 nt_errstr(status));
1187 return status;
1191 * security_descriptor_dacl_del() may delete more
1192 * then one entry subsequent to this one if the
1193 * SID matches, but we only need to ensure that
1194 * we stay looking at the same element in the array.
1196 i--;
1198 return NT_STATUS_OK;
1201 /* Search MS NFS style ACE with UNIX mode */
1202 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1203 files_struct *fsp,
1204 struct security_descriptor *psd,
1205 mode_t *pmode,
1206 bool *pdo_chmod)
1208 uint32_t i;
1209 struct fruit_config_data *config = NULL;
1211 *pdo_chmod = false;
1213 SMB_VFS_HANDLE_GET_DATA(handle, config,
1214 struct fruit_config_data,
1215 return NT_STATUS_UNSUCCESSFUL);
1217 if (!global_fruit_config.nego_aapl) {
1218 return NT_STATUS_OK;
1220 if (psd->dacl == NULL || !config->unix_info_enabled) {
1221 return NT_STATUS_OK;
1224 for (i = 0; i < psd->dacl->num_aces; i++) {
1225 if (dom_sid_compare_domain(
1226 &global_sid_Unix_NFS_Mode,
1227 &psd->dacl->aces[i].trustee) == 0) {
1228 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1229 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1230 *pdo_chmod = true;
1232 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1233 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1234 break;
1239 * Remove any incoming virtual ACE entries generated by
1240 * fruit_fget_nt_acl().
1243 return remove_virtual_nfs_aces(psd);
1246 /****************************************************************************
1247 * VFS ops
1248 ****************************************************************************/
1250 static int fruit_connect(vfs_handle_struct *handle,
1251 const char *service,
1252 const char *user)
1254 int rc;
1255 char *list = NULL, *newlist = NULL;
1256 struct fruit_config_data *config;
1257 const struct loadparm_substitution *lp_sub =
1258 loadparm_s3_global_substitution();
1260 DEBUG(10, ("fruit_connect\n"));
1262 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1263 if (rc < 0) {
1264 return rc;
1267 rc = init_fruit_config(handle);
1268 if (rc != 0) {
1269 return rc;
1272 SMB_VFS_HANDLE_GET_DATA(handle, config,
1273 struct fruit_config_data, return -1);
1275 if (config->veto_appledouble) {
1276 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1278 if (list) {
1279 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1280 newlist = talloc_asprintf(
1281 list,
1282 "%s/" ADOUBLE_NAME_PREFIX "*/",
1283 list);
1284 lp_do_parameter(SNUM(handle->conn),
1285 "veto files",
1286 newlist);
1288 } else {
1289 lp_do_parameter(SNUM(handle->conn),
1290 "veto files",
1291 "/" ADOUBLE_NAME_PREFIX "*/");
1294 TALLOC_FREE(list);
1297 if (config->encoding == FRUIT_ENC_NATIVE) {
1298 lp_do_parameter(SNUM(handle->conn),
1299 "catia:mappings",
1300 macos_string_replace_map);
1303 if (config->time_machine) {
1304 DBG_NOTICE("Enabling durable handles for Time Machine "
1305 "support on [%s]\n", service);
1306 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1307 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1308 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1309 if (!lp_strict_sync(SNUM(handle->conn))) {
1310 DBG_WARNING("Time Machine without strict sync is not "
1311 "recommended!\n");
1313 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1316 return rc;
1319 static int fruit_fake_fd(void)
1321 int pipe_fds[2];
1322 int fd;
1323 int ret;
1326 * Return a valid fd, but ensure any attempt to use it returns
1327 * an error (EPIPE). Once we get a write on the handle, we open
1328 * the real fd.
1330 ret = pipe(pipe_fds);
1331 if (ret != 0) {
1332 return -1;
1334 fd = pipe_fds[0];
1335 close(pipe_fds[1]);
1337 return fd;
1340 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1341 struct smb_filename *smb_fname,
1342 files_struct *fsp,
1343 int flags,
1344 mode_t mode)
1346 struct fruit_config_data *config = NULL;
1347 struct fio *fio = NULL;
1348 int open_flags = flags & ~O_CREAT;
1349 int fd;
1351 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1353 SMB_VFS_HANDLE_GET_DATA(handle, config,
1354 struct fruit_config_data, return -1);
1356 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1357 fio->type = ADOUBLE_META;
1358 fio->config = config;
1360 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, open_flags, mode);
1361 if (fd != -1) {
1362 return fd;
1365 if (!(flags & O_CREAT)) {
1366 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1367 return -1;
1370 fd = fruit_fake_fd();
1371 if (fd == -1) {
1372 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1373 return -1;
1376 fio->fake_fd = true;
1377 fio->flags = flags;
1378 fio->mode = mode;
1380 return fd;
1383 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1384 struct smb_filename *smb_fname,
1385 files_struct *fsp,
1386 int flags,
1387 mode_t mode)
1389 struct fruit_config_data *config = NULL;
1390 struct fio *fio = NULL;
1391 struct adouble *ad = NULL;
1392 bool meta_exists = false;
1393 int fd;
1395 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1397 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1398 if (ad != NULL) {
1399 meta_exists = true;
1402 TALLOC_FREE(ad);
1404 if (!meta_exists && !(flags & O_CREAT)) {
1405 errno = ENOENT;
1406 return -1;
1409 fd = fruit_fake_fd();
1410 if (fd == -1) {
1411 return -1;
1414 SMB_VFS_HANDLE_GET_DATA(handle, config,
1415 struct fruit_config_data, return -1);
1417 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1418 fio->type = ADOUBLE_META;
1419 fio->config = config;
1420 fio->fake_fd = true;
1421 fio->flags = flags;
1422 fio->mode = mode;
1424 return fd;
1427 static int fruit_open_meta(vfs_handle_struct *handle,
1428 struct smb_filename *smb_fname,
1429 files_struct *fsp, int flags, mode_t mode)
1431 int fd;
1432 struct fruit_config_data *config = NULL;
1434 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1436 SMB_VFS_HANDLE_GET_DATA(handle, config,
1437 struct fruit_config_data, return -1);
1439 switch (config->meta) {
1440 case FRUIT_META_STREAM:
1441 fd = fruit_open_meta_stream(handle, smb_fname,
1442 fsp, flags, mode);
1443 break;
1445 case FRUIT_META_NETATALK:
1446 fd = fruit_open_meta_netatalk(handle, smb_fname,
1447 fsp, flags, mode);
1448 break;
1450 default:
1451 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1452 return -1;
1455 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1457 return fd;
1460 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1461 struct smb_filename *smb_fname,
1462 files_struct *fsp,
1463 int flags,
1464 mode_t mode)
1466 int rc = 0;
1467 struct adouble *ad = NULL;
1468 struct smb_filename *smb_fname_base = NULL;
1469 struct fruit_config_data *config = NULL;
1470 int hostfd = -1;
1472 SMB_VFS_HANDLE_GET_DATA(handle, config,
1473 struct fruit_config_data, return -1);
1475 if ((!(flags & O_CREAT)) &&
1476 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1478 /* sorry, but directories don't habe a resource fork */
1479 rc = -1;
1480 goto exit;
1483 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
1484 if (rc != 0) {
1485 goto exit;
1488 /* We always need read/write access for the metadata header too */
1489 flags &= ~(O_RDONLY | O_WRONLY);
1490 flags |= O_RDWR;
1492 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
1493 flags, mode);
1494 if (hostfd == -1) {
1495 rc = -1;
1496 goto exit;
1499 if (flags & (O_CREAT | O_TRUNC)) {
1500 ad = ad_init(fsp, ADOUBLE_RSRC);
1501 if (ad == NULL) {
1502 rc = -1;
1503 goto exit;
1506 fsp->fh->fd = hostfd;
1508 rc = ad_fset(handle, ad, fsp);
1509 fsp->fh->fd = -1;
1510 if (rc != 0) {
1511 rc = -1;
1512 goto exit;
1514 TALLOC_FREE(ad);
1517 exit:
1519 TALLOC_FREE(smb_fname_base);
1521 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
1522 if (rc != 0) {
1523 int saved_errno = errno;
1524 if (hostfd >= 0) {
1526 * BUGBUGBUG -- we would need to call
1527 * fd_close_posix here, but we don't have a
1528 * full fsp yet
1530 fsp->fh->fd = hostfd;
1531 SMB_VFS_CLOSE(fsp);
1533 hostfd = -1;
1534 errno = saved_errno;
1536 return hostfd;
1539 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1540 struct smb_filename *smb_fname,
1541 files_struct *fsp,
1542 int flags,
1543 mode_t mode)
1545 #ifdef HAVE_ATTROPEN
1546 int fd = -1;
1548 fd = attropen(smb_fname->base_name,
1549 AFPRESOURCE_EA_NETATALK,
1550 flags,
1551 mode);
1552 if (fd == -1) {
1553 return -1;
1556 return fd;
1558 #else
1559 errno = ENOSYS;
1560 return -1;
1561 #endif
1564 static int fruit_open_rsrc(vfs_handle_struct *handle,
1565 struct smb_filename *smb_fname,
1566 files_struct *fsp, int flags, mode_t mode)
1568 int fd;
1569 struct fruit_config_data *config = NULL;
1570 struct fio *fio = NULL;
1572 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1574 SMB_VFS_HANDLE_GET_DATA(handle, config,
1575 struct fruit_config_data, return -1);
1577 switch (config->rsrc) {
1578 case FRUIT_RSRC_STREAM:
1579 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1580 break;
1582 case FRUIT_RSRC_ADFILE:
1583 fd = fruit_open_rsrc_adouble(handle, smb_fname,
1584 fsp, flags, mode);
1585 break;
1587 case FRUIT_RSRC_XATTR:
1588 fd = fruit_open_rsrc_xattr(handle, smb_fname,
1589 fsp, flags, mode);
1590 break;
1592 default:
1593 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1594 return -1;
1597 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1599 if (fd == -1) {
1600 return -1;
1603 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1604 fio->type = ADOUBLE_RSRC;
1605 fio->config = config;
1607 return fd;
1610 static int fruit_open(vfs_handle_struct *handle,
1611 struct smb_filename *smb_fname,
1612 files_struct *fsp, int flags, mode_t mode)
1614 int fd;
1616 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1618 if (!is_named_stream(smb_fname)) {
1619 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1622 if (is_afpinfo_stream(smb_fname->stream_name)) {
1623 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
1624 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1625 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
1626 } else {
1627 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1630 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1632 return fd;
1635 static int fruit_close_meta(vfs_handle_struct *handle,
1636 files_struct *fsp)
1638 int ret;
1639 struct fruit_config_data *config = NULL;
1641 SMB_VFS_HANDLE_GET_DATA(handle, config,
1642 struct fruit_config_data, return -1);
1644 switch (config->meta) {
1645 case FRUIT_META_STREAM:
1646 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1647 break;
1649 case FRUIT_META_NETATALK:
1650 ret = close(fsp->fh->fd);
1651 fsp->fh->fd = -1;
1652 break;
1654 default:
1655 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1656 return -1;
1659 return ret;
1663 static int fruit_close_rsrc(vfs_handle_struct *handle,
1664 files_struct *fsp)
1666 int ret;
1667 struct fruit_config_data *config = NULL;
1669 SMB_VFS_HANDLE_GET_DATA(handle, config,
1670 struct fruit_config_data, return -1);
1672 switch (config->rsrc) {
1673 case FRUIT_RSRC_STREAM:
1674 case FRUIT_RSRC_ADFILE:
1675 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1676 break;
1678 case FRUIT_RSRC_XATTR:
1679 ret = close(fsp->fh->fd);
1680 fsp->fh->fd = -1;
1681 break;
1683 default:
1684 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1685 return -1;
1688 return ret;
1691 static int fruit_close(vfs_handle_struct *handle,
1692 files_struct *fsp)
1694 int ret;
1695 int fd;
1697 fd = fsp->fh->fd;
1699 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1701 if (!is_named_stream(fsp->fsp_name)) {
1702 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1705 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1706 ret = fruit_close_meta(handle, fsp);
1707 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1708 ret = fruit_close_rsrc(handle, fsp);
1709 } else {
1710 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1713 return ret;
1716 static int fruit_renameat(struct vfs_handle_struct *handle,
1717 files_struct *srcfsp,
1718 const struct smb_filename *smb_fname_src,
1719 files_struct *dstfsp,
1720 const struct smb_filename *smb_fname_dst)
1722 int rc = -1;
1723 struct fruit_config_data *config = NULL;
1724 struct smb_filename *src_adp_smb_fname = NULL;
1725 struct smb_filename *dst_adp_smb_fname = NULL;
1727 SMB_VFS_HANDLE_GET_DATA(handle, config,
1728 struct fruit_config_data, return -1);
1730 if (!VALID_STAT(smb_fname_src->st)) {
1731 DBG_ERR("Need valid stat for [%s]\n",
1732 smb_fname_str_dbg(smb_fname_src));
1733 return -1;
1736 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1737 srcfsp,
1738 smb_fname_src,
1739 dstfsp,
1740 smb_fname_dst);
1741 if (rc != 0) {
1742 return -1;
1745 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1746 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1748 return 0;
1751 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1752 if (rc != 0) {
1753 goto done;
1756 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1757 if (rc != 0) {
1758 goto done;
1761 DBG_DEBUG("%s -> %s\n",
1762 smb_fname_str_dbg(src_adp_smb_fname),
1763 smb_fname_str_dbg(dst_adp_smb_fname));
1765 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1766 srcfsp,
1767 src_adp_smb_fname,
1768 dstfsp,
1769 dst_adp_smb_fname);
1770 if (errno == ENOENT) {
1771 rc = 0;
1774 done:
1775 TALLOC_FREE(src_adp_smb_fname);
1776 TALLOC_FREE(dst_adp_smb_fname);
1777 return rc;
1780 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1781 struct files_struct *dirfsp,
1782 const struct smb_filename *smb_fname)
1784 return SMB_VFS_NEXT_UNLINKAT(handle,
1785 dirfsp,
1786 smb_fname,
1790 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1791 const struct smb_filename *smb_fname)
1793 return SMB_VFS_REMOVEXATTR(handle->conn,
1794 smb_fname,
1795 AFPINFO_EA_NETATALK);
1798 static int fruit_unlink_meta(vfs_handle_struct *handle,
1799 struct files_struct *dirfsp,
1800 const struct smb_filename *smb_fname)
1802 struct fruit_config_data *config = NULL;
1803 int rc;
1805 SMB_VFS_HANDLE_GET_DATA(handle, config,
1806 struct fruit_config_data, return -1);
1808 switch (config->meta) {
1809 case FRUIT_META_STREAM:
1810 rc = fruit_unlink_meta_stream(handle,
1811 dirfsp,
1812 smb_fname);
1813 break;
1815 case FRUIT_META_NETATALK:
1816 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1817 break;
1819 default:
1820 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
1821 return -1;
1824 return rc;
1827 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
1828 struct files_struct *dirfsp,
1829 const struct smb_filename *smb_fname,
1830 bool force_unlink)
1832 int ret;
1834 if (!force_unlink) {
1835 struct smb_filename *smb_fname_cp = NULL;
1836 off_t size;
1838 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
1839 if (smb_fname_cp == NULL) {
1840 return -1;
1844 * 0 byte resource fork streams are not listed by
1845 * vfs_streaminfo, as a result stream cleanup/deletion of file
1846 * deletion doesn't remove the resourcefork stream.
1849 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
1850 if (ret != 0) {
1851 TALLOC_FREE(smb_fname_cp);
1852 DBG_ERR("stat [%s] failed [%s]\n",
1853 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
1854 return -1;
1857 size = smb_fname_cp->st.st_ex_size;
1858 TALLOC_FREE(smb_fname_cp);
1860 if (size > 0) {
1861 /* OS X ignores resource fork stream delete requests */
1862 return 0;
1866 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1867 dirfsp,
1868 smb_fname,
1870 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
1871 ret = 0;
1874 return ret;
1877 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
1878 struct files_struct *dirfsp,
1879 const struct smb_filename *smb_fname,
1880 bool force_unlink)
1882 int rc;
1883 struct adouble *ad = NULL;
1884 struct smb_filename *adp_smb_fname = NULL;
1886 if (!force_unlink) {
1887 ad = ad_get(talloc_tos(), handle, smb_fname,
1888 ADOUBLE_RSRC);
1889 if (ad == NULL) {
1890 errno = ENOENT;
1891 return -1;
1896 * 0 byte resource fork streams are not listed by
1897 * vfs_streaminfo, as a result stream cleanup/deletion of file
1898 * deletion doesn't remove the resourcefork stream.
1901 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1902 /* OS X ignores resource fork stream delete requests */
1903 TALLOC_FREE(ad);
1904 return 0;
1907 TALLOC_FREE(ad);
1910 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1911 if (rc != 0) {
1912 return -1;
1915 rc = SMB_VFS_NEXT_UNLINKAT(handle,
1916 dirfsp,
1917 adp_smb_fname,
1919 TALLOC_FREE(adp_smb_fname);
1920 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
1921 rc = 0;
1924 return rc;
1927 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
1928 const struct smb_filename *smb_fname,
1929 bool force_unlink)
1932 * OS X ignores resource fork stream delete requests, so nothing to do
1933 * here. Removing the file will remove the xattr anyway, so we don't
1934 * have to take care of removing 0 byte resource forks that could be
1935 * left behind.
1937 return 0;
1940 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
1941 struct files_struct *dirfsp,
1942 const struct smb_filename *smb_fname,
1943 bool force_unlink)
1945 struct fruit_config_data *config = NULL;
1946 int rc;
1948 SMB_VFS_HANDLE_GET_DATA(handle, config,
1949 struct fruit_config_data, return -1);
1951 switch (config->rsrc) {
1952 case FRUIT_RSRC_STREAM:
1953 rc = fruit_unlink_rsrc_stream(handle,
1954 dirfsp,
1955 smb_fname,
1956 force_unlink);
1957 break;
1959 case FRUIT_RSRC_ADFILE:
1960 rc = fruit_unlink_rsrc_adouble(handle,
1961 dirfsp,
1962 smb_fname,
1963 force_unlink);
1964 break;
1966 case FRUIT_RSRC_XATTR:
1967 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
1968 break;
1970 default:
1971 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
1972 return -1;
1975 return rc;
1978 static int fruit_unlink_internal(vfs_handle_struct *handle,
1979 struct files_struct *dirfsp,
1980 const struct smb_filename *smb_fname)
1982 int rc;
1983 struct fruit_config_data *config = NULL;
1984 struct smb_filename *rsrc_smb_fname = NULL;
1986 SMB_VFS_HANDLE_GET_DATA(handle, config,
1987 struct fruit_config_data, return -1);
1989 if (is_afpinfo_stream(smb_fname->stream_name)) {
1990 return fruit_unlink_meta(handle,
1991 dirfsp,
1992 smb_fname);
1993 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1994 return fruit_unlink_rsrc(handle,
1995 dirfsp,
1996 smb_fname,
1997 false);
1998 } else if (is_named_stream(smb_fname)) {
1999 return SMB_VFS_NEXT_UNLINKAT(handle,
2000 dirfsp,
2001 smb_fname,
2003 } else if (is_adouble_file(smb_fname->base_name)) {
2004 return SMB_VFS_NEXT_UNLINKAT(handle,
2005 dirfsp,
2006 smb_fname,
2011 * A request to delete the base file. Because 0 byte resource
2012 * fork streams are not listed by fruit_streaminfo,
2013 * delete_all_streams() can't remove 0 byte resource fork
2014 * streams, so we have to cleanup this here.
2016 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2017 smb_fname->base_name,
2018 AFPRESOURCE_STREAM_NAME,
2019 NULL,
2020 smb_fname->flags);
2021 if (rsrc_smb_fname == NULL) {
2022 return -1;
2025 rc = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2026 if ((rc != 0) && (errno != ENOENT)) {
2027 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2028 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2029 TALLOC_FREE(rsrc_smb_fname);
2030 return -1;
2032 TALLOC_FREE(rsrc_smb_fname);
2034 return SMB_VFS_NEXT_UNLINKAT(handle,
2035 dirfsp,
2036 smb_fname,
2040 static int fruit_chmod(vfs_handle_struct *handle,
2041 const struct smb_filename *smb_fname,
2042 mode_t mode)
2044 int rc = -1;
2045 struct fruit_config_data *config = NULL;
2046 struct smb_filename *smb_fname_adp = NULL;
2048 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
2049 if (rc != 0) {
2050 return rc;
2053 SMB_VFS_HANDLE_GET_DATA(handle, config,
2054 struct fruit_config_data, return -1);
2056 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2057 return 0;
2060 if (!VALID_STAT(smb_fname->st)) {
2061 return 0;
2064 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2065 return 0;
2068 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2069 if (rc != 0) {
2070 return -1;
2073 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
2075 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
2076 if (errno == ENOENT) {
2077 rc = 0;
2080 TALLOC_FREE(smb_fname_adp);
2081 return rc;
2084 static int fruit_rmdir_internal(struct vfs_handle_struct *handle,
2085 struct files_struct *dirfsp,
2086 const struct smb_filename *smb_fname)
2088 DIR *dh = NULL;
2089 struct dirent *de;
2090 struct fruit_config_data *config;
2092 SMB_VFS_HANDLE_GET_DATA(handle, config,
2093 struct fruit_config_data, return -1);
2095 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2096 goto exit_rmdir;
2100 * Due to there is no way to change bDeleteVetoFiles variable
2101 * from this module, need to clean up ourselves
2104 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
2105 if (dh == NULL) {
2106 goto exit_rmdir;
2109 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
2110 struct adouble *ad = NULL;
2111 char *p = NULL;
2112 struct smb_filename *ad_smb_fname = NULL;
2113 int ret;
2115 if (!is_adouble_file(de->d_name)) {
2116 continue;
2119 p = talloc_asprintf(talloc_tos(), "%s/%s",
2120 smb_fname->base_name, de->d_name);
2121 if (p == NULL) {
2122 DBG_ERR("talloc_asprintf failed\n");
2123 return -1;
2126 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
2127 NULL, NULL,
2128 smb_fname->flags);
2129 TALLOC_FREE(p);
2130 if (ad_smb_fname == NULL) {
2131 DBG_ERR("synthetic_smb_fname failed\n");
2132 return -1;
2136 * Check whether it's a valid AppleDouble file, if
2137 * yes, delete it, ignore it otherwise.
2139 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
2140 if (ad == NULL) {
2141 TALLOC_FREE(ad_smb_fname);
2142 TALLOC_FREE(p);
2143 continue;
2145 TALLOC_FREE(ad);
2147 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2148 dirfsp,
2149 ad_smb_fname,
2151 if (ret != 0) {
2152 DBG_ERR("Deleting [%s] failed\n",
2153 smb_fname_str_dbg(ad_smb_fname));
2155 TALLOC_FREE(ad_smb_fname);
2158 exit_rmdir:
2159 if (dh) {
2160 SMB_VFS_CLOSEDIR(handle->conn, dh);
2162 return SMB_VFS_NEXT_UNLINKAT(handle,
2163 dirfsp,
2164 smb_fname,
2165 AT_REMOVEDIR);
2168 static int fruit_unlinkat(vfs_handle_struct *handle,
2169 struct files_struct *dirfsp,
2170 const struct smb_filename *smb_fname,
2171 int flags)
2173 int ret;
2175 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
2176 if (flags & AT_REMOVEDIR) {
2177 ret = fruit_rmdir_internal(handle,
2178 dirfsp,
2179 smb_fname);
2180 } else {
2181 ret = fruit_unlink_internal(handle,
2182 dirfsp,
2183 smb_fname);
2185 return ret;
2188 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2189 files_struct *fsp, void *data,
2190 size_t n, off_t offset)
2192 ssize_t nread;
2193 int ret;
2195 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2196 if (nread == -1 || nread == n) {
2197 return nread;
2200 DBG_ERR("Removing [%s] after short read [%zd]\n",
2201 fsp_str_dbg(fsp), nread);
2203 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2204 fsp->conn->cwd_fsp,
2205 fsp->fsp_name,
2207 if (ret != 0) {
2208 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2209 return -1;
2212 errno = EINVAL;
2213 return -1;
2216 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2217 files_struct *fsp, void *data,
2218 size_t n, off_t offset)
2220 AfpInfo *ai = NULL;
2221 struct adouble *ad = NULL;
2222 char afpinfo_buf[AFP_INFO_SIZE];
2223 char *p = NULL;
2224 ssize_t nread;
2226 ai = afpinfo_new(talloc_tos());
2227 if (ai == NULL) {
2228 return -1;
2231 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2232 if (ad == NULL) {
2233 nread = -1;
2234 goto fail;
2237 p = ad_get_entry(ad, ADEID_FINDERI);
2238 if (p == NULL) {
2239 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2240 nread = -1;
2241 goto fail;
2244 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2246 nread = afpinfo_pack(ai, afpinfo_buf);
2247 if (nread != AFP_INFO_SIZE) {
2248 nread = -1;
2249 goto fail;
2252 memcpy(data, afpinfo_buf, n);
2253 nread = n;
2255 fail:
2256 TALLOC_FREE(ai);
2257 return nread;
2260 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2261 files_struct *fsp, void *data,
2262 size_t n, off_t offset)
2264 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2265 ssize_t nread;
2266 ssize_t to_return;
2269 * OS X has a off-by-1 error in the offset calculation, so we're
2270 * bug compatible here. It won't hurt, as any relevant real
2271 * world read requests from the AFP_AfpInfo stream will be
2272 * offset=0 n=60. offset is ignored anyway, see below.
2274 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2275 return 0;
2278 if (fio == NULL) {
2279 DBG_ERR("Failed to fetch fsp extension");
2280 return -1;
2283 /* Yes, macOS always reads from offset 0 */
2284 offset = 0;
2285 to_return = MIN(n, AFP_INFO_SIZE);
2287 switch (fio->config->meta) {
2288 case FRUIT_META_STREAM:
2289 nread = fruit_pread_meta_stream(handle, fsp, data,
2290 to_return, offset);
2291 break;
2293 case FRUIT_META_NETATALK:
2294 nread = fruit_pread_meta_adouble(handle, fsp, data,
2295 to_return, offset);
2296 break;
2298 default:
2299 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2300 return -1;
2303 if (nread == -1 && fio->created) {
2304 AfpInfo *ai = NULL;
2305 char afpinfo_buf[AFP_INFO_SIZE];
2307 ai = afpinfo_new(talloc_tos());
2308 if (ai == NULL) {
2309 return -1;
2312 nread = afpinfo_pack(ai, afpinfo_buf);
2313 TALLOC_FREE(ai);
2314 if (nread != AFP_INFO_SIZE) {
2315 return -1;
2318 memcpy(data, afpinfo_buf, to_return);
2319 return to_return;
2322 return nread;
2325 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2326 files_struct *fsp, void *data,
2327 size_t n, off_t offset)
2329 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2332 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2333 files_struct *fsp, void *data,
2334 size_t n, off_t offset)
2336 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2339 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2340 files_struct *fsp, void *data,
2341 size_t n, off_t offset)
2343 struct adouble *ad = NULL;
2344 ssize_t nread;
2346 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2347 if (ad == NULL) {
2348 return -1;
2351 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
2352 offset + ad_getentryoff(ad, ADEID_RFORK));
2354 TALLOC_FREE(ad);
2355 return nread;
2358 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2359 files_struct *fsp, void *data,
2360 size_t n, off_t offset)
2362 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2363 ssize_t nread;
2365 if (fio == NULL) {
2366 errno = EINVAL;
2367 return -1;
2370 switch (fio->config->rsrc) {
2371 case FRUIT_RSRC_STREAM:
2372 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2373 break;
2375 case FRUIT_RSRC_ADFILE:
2376 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2377 break;
2379 case FRUIT_RSRC_XATTR:
2380 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2381 break;
2383 default:
2384 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2385 return -1;
2388 return nread;
2391 static ssize_t fruit_pread(vfs_handle_struct *handle,
2392 files_struct *fsp, void *data,
2393 size_t n, off_t offset)
2395 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2396 ssize_t nread;
2398 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2399 fsp_str_dbg(fsp), (intmax_t)offset, n);
2401 if (fio == NULL) {
2402 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2405 if (fio->type == ADOUBLE_META) {
2406 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2407 } else {
2408 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2411 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2412 return nread;
2415 static bool fruit_must_handle_aio_stream(struct fio *fio)
2417 if (fio == NULL) {
2418 return false;
2421 if (fio->type == ADOUBLE_META) {
2422 return true;
2425 if ((fio->type == ADOUBLE_RSRC) &&
2426 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2428 return true;
2431 return false;
2434 struct fruit_pread_state {
2435 ssize_t nread;
2436 struct vfs_aio_state vfs_aio_state;
2439 static void fruit_pread_done(struct tevent_req *subreq);
2441 static struct tevent_req *fruit_pread_send(
2442 struct vfs_handle_struct *handle,
2443 TALLOC_CTX *mem_ctx,
2444 struct tevent_context *ev,
2445 struct files_struct *fsp,
2446 void *data,
2447 size_t n, off_t offset)
2449 struct tevent_req *req = NULL;
2450 struct tevent_req *subreq = NULL;
2451 struct fruit_pread_state *state = NULL;
2452 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2454 req = tevent_req_create(mem_ctx, &state,
2455 struct fruit_pread_state);
2456 if (req == NULL) {
2457 return NULL;
2460 if (fruit_must_handle_aio_stream(fio)) {
2461 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2462 if (state->nread != n) {
2463 if (state->nread != -1) {
2464 errno = EIO;
2466 tevent_req_error(req, errno);
2467 return tevent_req_post(req, ev);
2469 tevent_req_done(req);
2470 return tevent_req_post(req, ev);
2473 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2474 data, n, offset);
2475 if (tevent_req_nomem(req, subreq)) {
2476 return tevent_req_post(req, ev);
2478 tevent_req_set_callback(subreq, fruit_pread_done, req);
2479 return req;
2482 static void fruit_pread_done(struct tevent_req *subreq)
2484 struct tevent_req *req = tevent_req_callback_data(
2485 subreq, struct tevent_req);
2486 struct fruit_pread_state *state = tevent_req_data(
2487 req, struct fruit_pread_state);
2489 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2490 TALLOC_FREE(subreq);
2492 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2493 return;
2495 tevent_req_done(req);
2498 static ssize_t fruit_pread_recv(struct tevent_req *req,
2499 struct vfs_aio_state *vfs_aio_state)
2501 struct fruit_pread_state *state = tevent_req_data(
2502 req, struct fruit_pread_state);
2504 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2505 return -1;
2508 *vfs_aio_state = state->vfs_aio_state;
2509 return state->nread;
2512 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2513 files_struct *fsp, const void *data,
2514 size_t n, off_t offset)
2516 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2517 AfpInfo *ai = NULL;
2518 size_t nwritten;
2519 int ret;
2520 bool ok;
2522 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2523 fsp_str_dbg(fsp), (intmax_t)offset, n);
2525 if (fio == NULL) {
2526 return -1;
2529 if (fio->fake_fd) {
2530 int fd;
2532 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
2533 if (ret != 0) {
2534 DBG_ERR("Close [%s] failed: %s\n",
2535 fsp_str_dbg(fsp), strerror(errno));
2536 fsp->fh->fd = -1;
2537 return -1;
2540 fd = SMB_VFS_NEXT_OPEN(handle,
2541 fsp->fsp_name,
2542 fsp,
2543 fio->flags,
2544 fio->mode);
2545 if (fd == -1) {
2546 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2547 fsp_str_dbg(fsp), strerror(errno));
2548 return -1;
2550 fsp->fh->fd = fd;
2551 fio->fake_fd = false;
2554 ai = afpinfo_unpack(talloc_tos(), data);
2555 if (ai == NULL) {
2556 return -1;
2559 if (ai_empty_finderinfo(ai)) {
2561 * Writing an all 0 blob to the metadata stream results in the
2562 * stream being removed on a macOS server. This ensures we
2563 * behave the same and it verified by the "delete AFP_AfpInfo by
2564 * writing all 0" test.
2566 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2567 if (ret != 0) {
2568 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2569 fsp_str_dbg(fsp));
2570 return -1;
2573 ok = set_delete_on_close(
2574 fsp,
2575 true,
2576 handle->conn->session_info->security_token,
2577 handle->conn->session_info->unix_token);
2578 if (!ok) {
2579 DBG_ERR("set_delete_on_close on [%s] failed\n",
2580 fsp_str_dbg(fsp));
2581 return -1;
2583 return n;
2586 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2587 if (nwritten != n) {
2588 return -1;
2591 return n;
2594 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2595 files_struct *fsp, const void *data,
2596 size_t n, off_t offset)
2598 struct adouble *ad = NULL;
2599 AfpInfo *ai = NULL;
2600 char *p = NULL;
2601 int ret;
2602 bool ok;
2604 ai = afpinfo_unpack(talloc_tos(), data);
2605 if (ai == NULL) {
2606 return -1;
2609 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2610 if (ad == NULL) {
2611 ad = ad_init(talloc_tos(), ADOUBLE_META);
2612 if (ad == NULL) {
2613 return -1;
2616 p = ad_get_entry(ad, ADEID_FINDERI);
2617 if (p == NULL) {
2618 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2619 TALLOC_FREE(ad);
2620 return -1;
2623 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2625 ret = ad_fset(handle, ad, fsp);
2626 if (ret != 0) {
2627 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2628 TALLOC_FREE(ad);
2629 return -1;
2632 TALLOC_FREE(ad);
2634 if (!ai_empty_finderinfo(ai)) {
2635 return n;
2639 * Writing an all 0 blob to the metadata stream results in the stream
2640 * being removed on a macOS server. This ensures we behave the same and
2641 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2644 ok = set_delete_on_close(
2645 fsp,
2646 true,
2647 handle->conn->session_info->security_token,
2648 handle->conn->session_info->unix_token);
2649 if (!ok) {
2650 DBG_ERR("set_delete_on_close on [%s] failed\n",
2651 fsp_str_dbg(fsp));
2652 return -1;
2655 return n;
2658 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2659 files_struct *fsp, const void *data,
2660 size_t n, off_t offset)
2662 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2663 ssize_t nwritten;
2664 uint8_t buf[AFP_INFO_SIZE];
2665 size_t to_write;
2666 size_t to_copy;
2667 int cmp;
2669 if (fio == NULL) {
2670 DBG_ERR("Failed to fetch fsp extension");
2671 return -1;
2674 if (n < 3) {
2675 errno = EINVAL;
2676 return -1;
2679 if (offset != 0 && n < 60) {
2680 errno = EINVAL;
2681 return -1;
2684 cmp = memcmp(data, "AFP", 3);
2685 if (cmp != 0) {
2686 errno = EINVAL;
2687 return -1;
2690 if (n <= AFP_OFF_FinderInfo) {
2692 * Nothing to do here really, just return
2694 return n;
2697 offset = 0;
2699 to_copy = n;
2700 if (to_copy > AFP_INFO_SIZE) {
2701 to_copy = AFP_INFO_SIZE;
2703 memcpy(buf, data, to_copy);
2705 to_write = n;
2706 if (to_write != AFP_INFO_SIZE) {
2707 to_write = AFP_INFO_SIZE;
2710 switch (fio->config->meta) {
2711 case FRUIT_META_STREAM:
2712 nwritten = fruit_pwrite_meta_stream(handle,
2713 fsp,
2714 buf,
2715 to_write,
2716 offset);
2717 break;
2719 case FRUIT_META_NETATALK:
2720 nwritten = fruit_pwrite_meta_netatalk(handle,
2721 fsp,
2722 buf,
2723 to_write,
2724 offset);
2725 break;
2727 default:
2728 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2729 return -1;
2732 if (nwritten != to_write) {
2733 return -1;
2737 * Return the requested amount, verified against macOS SMB server
2739 return n;
2742 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2743 files_struct *fsp, const void *data,
2744 size_t n, off_t offset)
2746 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2749 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2750 files_struct *fsp, const void *data,
2751 size_t n, off_t offset)
2753 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2756 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2757 files_struct *fsp, const void *data,
2758 size_t n, off_t offset)
2760 struct adouble *ad = NULL;
2761 ssize_t nwritten;
2762 int ret;
2764 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2765 if (ad == NULL) {
2766 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
2767 return -1;
2770 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2771 offset + ad_getentryoff(ad, ADEID_RFORK));
2772 if (nwritten != n) {
2773 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2774 fsp_str_dbg(fsp), nwritten, n);
2775 TALLOC_FREE(ad);
2776 return -1;
2779 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2780 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2781 ret = ad_fset(handle, ad, fsp);
2782 if (ret != 0) {
2783 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2784 TALLOC_FREE(ad);
2785 return -1;
2789 TALLOC_FREE(ad);
2790 return n;
2793 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2794 files_struct *fsp, const void *data,
2795 size_t n, off_t offset)
2797 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2798 ssize_t nwritten;
2800 if (fio == NULL) {
2801 DBG_ERR("Failed to fetch fsp extension");
2802 return -1;
2805 switch (fio->config->rsrc) {
2806 case FRUIT_RSRC_STREAM:
2807 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2808 break;
2810 case FRUIT_RSRC_ADFILE:
2811 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2812 break;
2814 case FRUIT_RSRC_XATTR:
2815 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2816 break;
2818 default:
2819 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2820 return -1;
2823 return nwritten;
2826 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2827 files_struct *fsp, const void *data,
2828 size_t n, off_t offset)
2830 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2831 ssize_t nwritten;
2833 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2834 fsp_str_dbg(fsp), (intmax_t)offset, n);
2836 if (fio == NULL) {
2837 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2840 if (fio->type == ADOUBLE_META) {
2841 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2842 } else {
2843 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2846 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2847 return nwritten;
2850 struct fruit_pwrite_state {
2851 ssize_t nwritten;
2852 struct vfs_aio_state vfs_aio_state;
2855 static void fruit_pwrite_done(struct tevent_req *subreq);
2857 static struct tevent_req *fruit_pwrite_send(
2858 struct vfs_handle_struct *handle,
2859 TALLOC_CTX *mem_ctx,
2860 struct tevent_context *ev,
2861 struct files_struct *fsp,
2862 const void *data,
2863 size_t n, off_t offset)
2865 struct tevent_req *req = NULL;
2866 struct tevent_req *subreq = NULL;
2867 struct fruit_pwrite_state *state = NULL;
2868 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2870 req = tevent_req_create(mem_ctx, &state,
2871 struct fruit_pwrite_state);
2872 if (req == NULL) {
2873 return NULL;
2876 if (fruit_must_handle_aio_stream(fio)) {
2877 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
2878 if (state->nwritten != n) {
2879 if (state->nwritten != -1) {
2880 errno = EIO;
2882 tevent_req_error(req, errno);
2883 return tevent_req_post(req, ev);
2885 tevent_req_done(req);
2886 return tevent_req_post(req, ev);
2889 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
2890 data, n, offset);
2891 if (tevent_req_nomem(req, subreq)) {
2892 return tevent_req_post(req, ev);
2894 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
2895 return req;
2898 static void fruit_pwrite_done(struct tevent_req *subreq)
2900 struct tevent_req *req = tevent_req_callback_data(
2901 subreq, struct tevent_req);
2902 struct fruit_pwrite_state *state = tevent_req_data(
2903 req, struct fruit_pwrite_state);
2905 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2906 TALLOC_FREE(subreq);
2908 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2909 return;
2911 tevent_req_done(req);
2914 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
2915 struct vfs_aio_state *vfs_aio_state)
2917 struct fruit_pwrite_state *state = tevent_req_data(
2918 req, struct fruit_pwrite_state);
2920 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2921 return -1;
2924 *vfs_aio_state = state->vfs_aio_state;
2925 return state->nwritten;
2929 * Helper to stat/lstat the base file of an smb_fname.
2931 static int fruit_stat_base(vfs_handle_struct *handle,
2932 struct smb_filename *smb_fname,
2933 bool follow_links)
2935 char *tmp_stream_name;
2936 int rc;
2938 tmp_stream_name = smb_fname->stream_name;
2939 smb_fname->stream_name = NULL;
2940 if (follow_links) {
2941 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2942 } else {
2943 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2945 smb_fname->stream_name = tmp_stream_name;
2947 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
2948 smb_fname->base_name,
2949 (uintmax_t)smb_fname->st.st_ex_dev,
2950 (uintmax_t)smb_fname->st.st_ex_ino);
2951 return rc;
2954 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
2955 struct smb_filename *smb_fname,
2956 bool follow_links)
2958 int ret;
2959 ino_t ino;
2961 ret = fruit_stat_base(handle, smb_fname, false);
2962 if (ret != 0) {
2963 return -1;
2966 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
2968 if (follow_links) {
2969 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2970 } else {
2971 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2974 smb_fname->st.st_ex_ino = ino;
2976 return ret;
2979 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
2980 struct smb_filename *smb_fname,
2981 bool follow_links)
2983 struct adouble *ad = NULL;
2985 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2986 if (ad == NULL) {
2987 DBG_INFO("fruit_stat_meta %s: %s\n",
2988 smb_fname_str_dbg(smb_fname), strerror(errno));
2989 errno = ENOENT;
2990 return -1;
2992 TALLOC_FREE(ad);
2994 /* Populate the stat struct with info from the base file. */
2995 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2996 return -1;
2998 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2999 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3000 smb_fname->stream_name);
3001 return 0;
3004 static int fruit_stat_meta(vfs_handle_struct *handle,
3005 struct smb_filename *smb_fname,
3006 bool follow_links)
3008 struct fruit_config_data *config = NULL;
3009 int ret;
3011 SMB_VFS_HANDLE_GET_DATA(handle, config,
3012 struct fruit_config_data, return -1);
3014 switch (config->meta) {
3015 case FRUIT_META_STREAM:
3016 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3017 break;
3019 case FRUIT_META_NETATALK:
3020 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3021 break;
3023 default:
3024 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3025 return -1;
3028 return ret;
3031 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3032 struct smb_filename *smb_fname,
3033 bool follow_links)
3035 struct adouble *ad = NULL;
3036 int ret;
3038 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3039 if (ad == NULL) {
3040 errno = ENOENT;
3041 return -1;
3044 /* Populate the stat struct with info from the base file. */
3045 ret = fruit_stat_base(handle, smb_fname, follow_links);
3046 if (ret != 0) {
3047 TALLOC_FREE(ad);
3048 return -1;
3051 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3052 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3053 smb_fname->stream_name);
3054 TALLOC_FREE(ad);
3055 return 0;
3058 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3059 struct smb_filename *smb_fname,
3060 bool follow_links)
3062 int ret;
3064 if (follow_links) {
3065 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3066 } else {
3067 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3070 return ret;
3073 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3074 struct smb_filename *smb_fname,
3075 bool follow_links)
3077 #ifdef HAVE_ATTROPEN
3078 int ret;
3079 int fd = -1;
3081 /* Populate the stat struct with info from the base file. */
3082 ret = fruit_stat_base(handle, smb_fname, follow_links);
3083 if (ret != 0) {
3084 return -1;
3087 fd = attropen(smb_fname->base_name,
3088 AFPRESOURCE_EA_NETATALK,
3089 O_RDONLY);
3090 if (fd == -1) {
3091 return 0;
3094 ret = sys_fstat(fd, &smb_fname->st, false);
3095 if (ret != 0) {
3096 close(fd);
3097 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3098 AFPRESOURCE_EA_NETATALK);
3099 return -1;
3101 close(fd);
3102 fd = -1;
3104 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3105 smb_fname->stream_name);
3107 return ret;
3109 #else
3110 errno = ENOSYS;
3111 return -1;
3112 #endif
3115 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3116 struct smb_filename *smb_fname,
3117 bool follow_links)
3119 struct fruit_config_data *config = NULL;
3120 int ret;
3122 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3124 SMB_VFS_HANDLE_GET_DATA(handle, config,
3125 struct fruit_config_data, return -1);
3127 switch (config->rsrc) {
3128 case FRUIT_RSRC_STREAM:
3129 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3130 break;
3132 case FRUIT_RSRC_XATTR:
3133 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3134 break;
3136 case FRUIT_RSRC_ADFILE:
3137 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3138 break;
3140 default:
3141 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3142 return -1;
3145 return ret;
3148 static int fruit_stat(vfs_handle_struct *handle,
3149 struct smb_filename *smb_fname)
3151 int rc = -1;
3153 DEBUG(10, ("fruit_stat called for %s\n",
3154 smb_fname_str_dbg(smb_fname)));
3156 if (!is_named_stream(smb_fname)) {
3157 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3158 if (rc == 0) {
3159 update_btime(handle, smb_fname);
3161 return rc;
3165 * Note if lp_posix_paths() is true, we can never
3166 * get here as is_ntfs_stream_smb_fname() is
3167 * always false. So we never need worry about
3168 * not following links here.
3171 if (is_afpinfo_stream(smb_fname->stream_name)) {
3172 rc = fruit_stat_meta(handle, smb_fname, true);
3173 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3174 rc = fruit_stat_rsrc(handle, smb_fname, true);
3175 } else {
3176 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3179 if (rc == 0) {
3180 update_btime(handle, smb_fname);
3181 smb_fname->st.st_ex_mode &= ~S_IFMT;
3182 smb_fname->st.st_ex_mode |= S_IFREG;
3183 smb_fname->st.st_ex_blocks =
3184 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3186 return rc;
3189 static int fruit_lstat(vfs_handle_struct *handle,
3190 struct smb_filename *smb_fname)
3192 int rc = -1;
3194 DEBUG(10, ("fruit_lstat called for %s\n",
3195 smb_fname_str_dbg(smb_fname)));
3197 if (!is_named_stream(smb_fname)) {
3198 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3199 if (rc == 0) {
3200 update_btime(handle, smb_fname);
3202 return rc;
3205 if (is_afpinfo_stream(smb_fname->stream_name)) {
3206 rc = fruit_stat_meta(handle, smb_fname, false);
3207 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3208 rc = fruit_stat_rsrc(handle, smb_fname, false);
3209 } else {
3210 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3213 if (rc == 0) {
3214 update_btime(handle, smb_fname);
3215 smb_fname->st.st_ex_mode &= ~S_IFMT;
3216 smb_fname->st.st_ex_mode |= S_IFREG;
3217 smb_fname->st.st_ex_blocks =
3218 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3220 return rc;
3223 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3224 files_struct *fsp,
3225 SMB_STRUCT_STAT *sbuf)
3227 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3228 struct smb_filename smb_fname;
3229 ino_t ino;
3230 int ret;
3232 if (fio == NULL) {
3233 return -1;
3236 if (fio->fake_fd) {
3237 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3238 if (ret != 0) {
3239 return -1;
3242 *sbuf = fsp->base_fsp->fsp_name->st;
3243 sbuf->st_ex_size = AFP_INFO_SIZE;
3244 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3245 return 0;
3248 smb_fname = (struct smb_filename) {
3249 .base_name = fsp->fsp_name->base_name,
3252 ret = fruit_stat_base(handle, &smb_fname, false);
3253 if (ret != 0) {
3254 return -1;
3256 *sbuf = smb_fname.st;
3258 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3260 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3261 if (ret != 0) {
3262 return -1;
3265 sbuf->st_ex_ino = ino;
3266 return 0;
3269 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3270 files_struct *fsp,
3271 SMB_STRUCT_STAT *sbuf)
3273 int ret;
3275 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3276 if (ret != 0) {
3277 return -1;
3280 *sbuf = fsp->base_fsp->fsp_name->st;
3281 sbuf->st_ex_size = AFP_INFO_SIZE;
3282 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3284 return 0;
3287 static int fruit_fstat_meta(vfs_handle_struct *handle,
3288 files_struct *fsp,
3289 SMB_STRUCT_STAT *sbuf,
3290 struct fio *fio)
3292 int ret;
3294 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3296 switch (fio->config->meta) {
3297 case FRUIT_META_STREAM:
3298 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3299 break;
3301 case FRUIT_META_NETATALK:
3302 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3303 break;
3305 default:
3306 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3307 return -1;
3310 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3311 return ret;
3314 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3315 files_struct *fsp,
3316 SMB_STRUCT_STAT *sbuf)
3318 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3321 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3322 files_struct *fsp,
3323 SMB_STRUCT_STAT *sbuf)
3325 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3328 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3329 files_struct *fsp,
3330 SMB_STRUCT_STAT *sbuf)
3332 struct adouble *ad = NULL;
3333 int ret;
3335 /* Populate the stat struct with info from the base file. */
3336 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3337 if (ret == -1) {
3338 return -1;
3341 ad = ad_get(talloc_tos(), handle,
3342 fsp->base_fsp->fsp_name,
3343 ADOUBLE_RSRC);
3344 if (ad == NULL) {
3345 DBG_ERR("ad_get [%s] failed [%s]\n",
3346 fsp_str_dbg(fsp), strerror(errno));
3347 return -1;
3350 *sbuf = fsp->base_fsp->fsp_name->st;
3351 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3352 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3354 TALLOC_FREE(ad);
3355 return 0;
3358 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3359 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3361 int ret;
3363 switch (fio->config->rsrc) {
3364 case FRUIT_RSRC_STREAM:
3365 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3366 break;
3368 case FRUIT_RSRC_ADFILE:
3369 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3370 break;
3372 case FRUIT_RSRC_XATTR:
3373 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3374 break;
3376 default:
3377 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3378 return -1;
3381 return ret;
3384 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3385 SMB_STRUCT_STAT *sbuf)
3387 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3388 int rc;
3390 if (fio == NULL) {
3391 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3394 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3396 if (fio->type == ADOUBLE_META) {
3397 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3398 } else {
3399 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3402 if (rc == 0) {
3403 sbuf->st_ex_mode &= ~S_IFMT;
3404 sbuf->st_ex_mode |= S_IFREG;
3405 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3408 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3409 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3410 return rc;
3413 static NTSTATUS delete_invalid_meta_stream(
3414 vfs_handle_struct *handle,
3415 const struct smb_filename *smb_fname,
3416 TALLOC_CTX *mem_ctx,
3417 unsigned int *pnum_streams,
3418 struct stream_struct **pstreams,
3419 off_t size)
3421 struct smb_filename *sname = NULL;
3422 int ret;
3423 bool ok;
3425 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3426 if (!ok) {
3427 return NT_STATUS_INTERNAL_ERROR;
3430 if (size == 0) {
3431 return NT_STATUS_OK;
3434 sname = synthetic_smb_fname(talloc_tos(),
3435 smb_fname->base_name,
3436 AFPINFO_STREAM_NAME,
3437 NULL, 0);
3438 if (sname == NULL) {
3439 return NT_STATUS_NO_MEMORY;
3442 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3443 handle->conn->cwd_fsp,
3444 sname,
3446 TALLOC_FREE(sname);
3447 if (ret != 0) {
3448 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3449 return map_nt_error_from_unix(errno);
3452 return NT_STATUS_OK;
3455 static NTSTATUS fruit_streaminfo_meta_stream(
3456 vfs_handle_struct *handle,
3457 struct files_struct *fsp,
3458 const struct smb_filename *smb_fname,
3459 TALLOC_CTX *mem_ctx,
3460 unsigned int *pnum_streams,
3461 struct stream_struct **pstreams)
3463 struct stream_struct *stream = *pstreams;
3464 unsigned int num_streams = *pnum_streams;
3465 int i;
3467 for (i = 0; i < num_streams; i++) {
3468 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3469 break;
3473 if (i == num_streams) {
3474 return NT_STATUS_OK;
3477 if (stream[i].size != AFP_INFO_SIZE) {
3478 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3479 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3481 return delete_invalid_meta_stream(handle,
3482 smb_fname,
3483 mem_ctx,
3484 pnum_streams,
3485 pstreams,
3486 stream[i].size);
3490 return NT_STATUS_OK;
3493 static NTSTATUS fruit_streaminfo_meta_netatalk(
3494 vfs_handle_struct *handle,
3495 struct files_struct *fsp,
3496 const struct smb_filename *smb_fname,
3497 TALLOC_CTX *mem_ctx,
3498 unsigned int *pnum_streams,
3499 struct stream_struct **pstreams)
3501 struct stream_struct *stream = *pstreams;
3502 unsigned int num_streams = *pnum_streams;
3503 struct adouble *ad = NULL;
3504 bool is_fi_empty;
3505 int i;
3506 bool ok;
3508 /* Remove the Netatalk xattr from the list */
3509 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3510 ":" NETATALK_META_XATTR ":$DATA");
3511 if (!ok) {
3512 return NT_STATUS_NO_MEMORY;
3516 * Check if there's a AFPINFO_STREAM from the VFS streams
3517 * backend and if yes, remove it from the list
3519 for (i = 0; i < num_streams; i++) {
3520 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3521 break;
3525 if (i < num_streams) {
3526 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3527 smb_fname_str_dbg(smb_fname));
3529 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3530 AFPINFO_STREAM);
3531 if (!ok) {
3532 return NT_STATUS_INTERNAL_ERROR;
3536 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3537 if (ad == NULL) {
3538 return NT_STATUS_OK;
3541 is_fi_empty = ad_empty_finderinfo(ad);
3542 TALLOC_FREE(ad);
3544 if (is_fi_empty) {
3545 return NT_STATUS_OK;
3548 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3549 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3550 smb_roundup(handle->conn, AFP_INFO_SIZE));
3551 if (!ok) {
3552 return NT_STATUS_NO_MEMORY;
3555 return NT_STATUS_OK;
3558 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3559 struct files_struct *fsp,
3560 const struct smb_filename *smb_fname,
3561 TALLOC_CTX *mem_ctx,
3562 unsigned int *pnum_streams,
3563 struct stream_struct **pstreams)
3565 struct fruit_config_data *config = NULL;
3566 NTSTATUS status;
3568 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3569 return NT_STATUS_INTERNAL_ERROR);
3571 switch (config->meta) {
3572 case FRUIT_META_NETATALK:
3573 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3574 mem_ctx, pnum_streams,
3575 pstreams);
3576 break;
3578 case FRUIT_META_STREAM:
3579 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3580 mem_ctx, pnum_streams,
3581 pstreams);
3582 break;
3584 default:
3585 return NT_STATUS_INTERNAL_ERROR;
3588 return status;
3591 static NTSTATUS fruit_streaminfo_rsrc_stream(
3592 vfs_handle_struct *handle,
3593 struct files_struct *fsp,
3594 const struct smb_filename *smb_fname,
3595 TALLOC_CTX *mem_ctx,
3596 unsigned int *pnum_streams,
3597 struct stream_struct **pstreams)
3599 bool ok;
3601 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3602 if (!ok) {
3603 DBG_ERR("Filtering resource stream failed\n");
3604 return NT_STATUS_INTERNAL_ERROR;
3606 return NT_STATUS_OK;
3609 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3610 vfs_handle_struct *handle,
3611 struct files_struct *fsp,
3612 const struct smb_filename *smb_fname,
3613 TALLOC_CTX *mem_ctx,
3614 unsigned int *pnum_streams,
3615 struct stream_struct **pstreams)
3617 bool ok;
3619 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3620 if (!ok) {
3621 DBG_ERR("Filtering resource stream failed\n");
3622 return NT_STATUS_INTERNAL_ERROR;
3624 return NT_STATUS_OK;
3627 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3628 vfs_handle_struct *handle,
3629 struct files_struct *fsp,
3630 const struct smb_filename *smb_fname,
3631 TALLOC_CTX *mem_ctx,
3632 unsigned int *pnum_streams,
3633 struct stream_struct **pstreams)
3635 struct stream_struct *stream = *pstreams;
3636 unsigned int num_streams = *pnum_streams;
3637 struct adouble *ad = NULL;
3638 bool ok;
3639 size_t rlen;
3640 int i;
3643 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3644 * and if yes, remove it from the list
3646 for (i = 0; i < num_streams; i++) {
3647 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3648 break;
3652 if (i < num_streams) {
3653 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3654 smb_fname_str_dbg(smb_fname));
3656 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3657 AFPRESOURCE_STREAM);
3658 if (!ok) {
3659 return NT_STATUS_INTERNAL_ERROR;
3663 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3664 if (ad == NULL) {
3665 return NT_STATUS_OK;
3668 rlen = ad_getentrylen(ad, ADEID_RFORK);
3669 TALLOC_FREE(ad);
3671 if (rlen == 0) {
3672 return NT_STATUS_OK;
3675 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3676 AFPRESOURCE_STREAM_NAME, rlen,
3677 smb_roundup(handle->conn, rlen));
3678 if (!ok) {
3679 return NT_STATUS_NO_MEMORY;
3682 return NT_STATUS_OK;
3685 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3686 struct files_struct *fsp,
3687 const struct smb_filename *smb_fname,
3688 TALLOC_CTX *mem_ctx,
3689 unsigned int *pnum_streams,
3690 struct stream_struct **pstreams)
3692 struct fruit_config_data *config = NULL;
3693 NTSTATUS status;
3695 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3696 return NT_STATUS_INTERNAL_ERROR);
3698 switch (config->rsrc) {
3699 case FRUIT_RSRC_STREAM:
3700 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3701 mem_ctx, pnum_streams,
3702 pstreams);
3703 break;
3705 case FRUIT_RSRC_XATTR:
3706 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3707 mem_ctx, pnum_streams,
3708 pstreams);
3709 break;
3711 case FRUIT_RSRC_ADFILE:
3712 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3713 mem_ctx, pnum_streams,
3714 pstreams);
3715 break;
3717 default:
3718 return NT_STATUS_INTERNAL_ERROR;
3721 return status;
3724 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3725 struct stream_struct **pstreams)
3727 unsigned num_streams = *pnum_streams;
3728 struct stream_struct *streams = *pstreams;
3729 unsigned i = 0;
3731 if (!global_fruit_config.nego_aapl) {
3732 return;
3735 while (i < num_streams) {
3736 struct smb_filename smb_fname = (struct smb_filename) {
3737 .stream_name = streams[i].name,
3740 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3741 || streams[i].size > 0)
3743 i++;
3744 continue;
3747 streams[i] = streams[num_streams - 1];
3748 num_streams--;
3751 *pnum_streams = num_streams;
3754 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3755 struct files_struct *fsp,
3756 const struct smb_filename *smb_fname,
3757 TALLOC_CTX *mem_ctx,
3758 unsigned int *pnum_streams,
3759 struct stream_struct **pstreams)
3761 struct fruit_config_data *config = NULL;
3762 NTSTATUS status;
3764 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3765 return NT_STATUS_UNSUCCESSFUL);
3767 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3769 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3770 pnum_streams, pstreams);
3771 if (!NT_STATUS_IS_OK(status)) {
3772 return status;
3775 fruit_filter_empty_streams(pnum_streams, pstreams);
3777 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3778 mem_ctx, pnum_streams, pstreams);
3779 if (!NT_STATUS_IS_OK(status)) {
3780 return status;
3783 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3784 mem_ctx, pnum_streams, pstreams);
3785 if (!NT_STATUS_IS_OK(status)) {
3786 return status;
3789 return NT_STATUS_OK;
3792 static int fruit_ntimes(vfs_handle_struct *handle,
3793 const struct smb_filename *smb_fname,
3794 struct smb_file_time *ft)
3796 int rc = 0;
3797 struct adouble *ad = NULL;
3798 struct fruit_config_data *config = NULL;
3800 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3801 return -1);
3803 if ((config->meta != FRUIT_META_NETATALK) ||
3804 is_omit_timespec(&ft->create_time))
3806 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3809 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3810 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3812 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3813 if (ad == NULL) {
3814 goto exit;
3817 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3818 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3820 rc = ad_set(handle, ad, smb_fname);
3822 exit:
3824 TALLOC_FREE(ad);
3825 if (rc != 0) {
3826 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3827 return -1;
3829 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3832 static int fruit_fallocate(struct vfs_handle_struct *handle,
3833 struct files_struct *fsp,
3834 uint32_t mode,
3835 off_t offset,
3836 off_t len)
3838 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3840 if (fio == NULL) {
3841 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3844 /* Let the pwrite code path handle it. */
3845 errno = ENOSYS;
3846 return -1;
3849 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
3850 struct files_struct *fsp,
3851 off_t offset)
3853 #ifdef HAVE_ATTROPEN
3854 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3855 #endif
3856 return 0;
3859 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
3860 struct files_struct *fsp,
3861 off_t offset)
3863 int rc;
3864 struct adouble *ad = NULL;
3865 off_t ad_off;
3867 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3868 if (ad == NULL) {
3869 DBG_DEBUG("ad_get [%s] failed [%s]\n",
3870 fsp_str_dbg(fsp), strerror(errno));
3871 return -1;
3874 ad_off = ad_getentryoff(ad, ADEID_RFORK);
3876 rc = ftruncate(fsp->fh->fd, offset + ad_off);
3877 if (rc != 0) {
3878 TALLOC_FREE(ad);
3879 return -1;
3882 ad_setentrylen(ad, ADEID_RFORK, offset);
3884 rc = ad_fset(handle, ad, fsp);
3885 if (rc != 0) {
3886 DBG_ERR("ad_fset [%s] failed [%s]\n",
3887 fsp_str_dbg(fsp), strerror(errno));
3888 TALLOC_FREE(ad);
3889 return -1;
3892 TALLOC_FREE(ad);
3893 return 0;
3896 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
3897 struct files_struct *fsp,
3898 off_t offset)
3900 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3903 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3904 struct files_struct *fsp,
3905 off_t offset)
3907 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3908 int ret;
3910 if (fio == NULL) {
3911 DBG_ERR("Failed to fetch fsp extension");
3912 return -1;
3915 switch (fio->config->rsrc) {
3916 case FRUIT_RSRC_XATTR:
3917 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
3918 break;
3920 case FRUIT_RSRC_ADFILE:
3921 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
3922 break;
3924 case FRUIT_RSRC_STREAM:
3925 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
3926 break;
3928 default:
3929 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3930 return -1;
3934 return ret;
3937 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3938 struct files_struct *fsp,
3939 off_t offset)
3941 if (offset > 60) {
3942 DBG_WARNING("ftruncate %s to %jd",
3943 fsp_str_dbg(fsp), (intmax_t)offset);
3944 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
3945 errno = EOVERFLOW;
3946 return -1;
3949 /* OS X returns success but does nothing */
3950 DBG_INFO("ignoring ftruncate %s to %jd\n",
3951 fsp_str_dbg(fsp), (intmax_t)offset);
3952 return 0;
3955 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3956 struct files_struct *fsp,
3957 off_t offset)
3959 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3960 int ret;
3962 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
3963 (intmax_t)offset);
3965 if (fio == NULL) {
3966 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3969 if (fio->type == ADOUBLE_META) {
3970 ret = fruit_ftruncate_meta(handle, fsp, offset);
3971 } else {
3972 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
3975 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
3976 return ret;
3979 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3980 struct smb_request *req,
3981 uint16_t root_dir_fid,
3982 struct smb_filename *smb_fname,
3983 uint32_t access_mask,
3984 uint32_t share_access,
3985 uint32_t create_disposition,
3986 uint32_t create_options,
3987 uint32_t file_attributes,
3988 uint32_t oplock_request,
3989 const struct smb2_lease *lease,
3990 uint64_t allocation_size,
3991 uint32_t private_flags,
3992 struct security_descriptor *sd,
3993 struct ea_list *ea_list,
3994 files_struct **result,
3995 int *pinfo,
3996 const struct smb2_create_blobs *in_context_blobs,
3997 struct smb2_create_blobs *out_context_blobs)
3999 NTSTATUS status;
4000 struct fruit_config_data *config = NULL;
4001 files_struct *fsp = NULL;
4002 struct fio *fio = NULL;
4003 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4004 int ret;
4006 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4007 if (!NT_STATUS_IS_OK(status)) {
4008 goto fail;
4011 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4012 return NT_STATUS_UNSUCCESSFUL);
4014 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4015 uint32_t conv_flags = 0;
4017 if (config->wipe_intentionally_left_blank_rfork) {
4018 conv_flags |= AD_CONV_WIPE_BLANK;
4020 if (config->delete_empty_adfiles) {
4021 conv_flags |= AD_CONV_DELETE;
4024 ret = ad_convert(handle,
4025 handle->conn->cwd_fsp,
4026 smb_fname,
4027 macos_string_replace_map,
4028 conv_flags);
4029 if (ret != 0) {
4030 DBG_ERR("ad_convert() failed\n");
4031 return NT_STATUS_UNSUCCESSFUL;
4035 status = SMB_VFS_NEXT_CREATE_FILE(
4036 handle, req, root_dir_fid, smb_fname,
4037 access_mask, share_access,
4038 create_disposition, create_options,
4039 file_attributes, oplock_request,
4040 lease,
4041 allocation_size, private_flags,
4042 sd, ea_list, result,
4043 pinfo, in_context_blobs, out_context_blobs);
4044 if (!NT_STATUS_IS_OK(status)) {
4045 return status;
4048 fsp = *result;
4050 if (global_fruit_config.nego_aapl) {
4051 if (config->posix_rename && fsp->is_directory) {
4053 * Enable POSIX directory rename behaviour
4055 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4060 * If this is a plain open for existing files, opening an 0
4061 * byte size resource fork MUST fail with
4062 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4064 * Cf the vfs_fruit torture tests in test_rfork_create().
4066 if (global_fruit_config.nego_aapl &&
4067 create_disposition == FILE_OPEN &&
4068 smb_fname->st.st_ex_size == 0 &&
4069 is_named_stream(smb_fname))
4071 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4072 goto fail;
4075 fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4076 if (fio != NULL && pinfo != NULL && *pinfo == FILE_WAS_CREATED) {
4077 fio->created = true;
4080 if (is_named_stream(smb_fname)
4081 || fsp->is_directory) {
4082 return status;
4085 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4086 (fsp->op != NULL))
4088 status = fruit_check_access(
4089 handle, *result,
4090 access_mask,
4091 share_access);
4092 if (!NT_STATUS_IS_OK(status)) {
4093 goto fail;
4097 return status;
4099 fail:
4100 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4102 if (fsp) {
4103 close_file(req, fsp, ERROR_CLOSE);
4104 *result = fsp = NULL;
4107 return status;
4110 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4111 const struct smb_filename *fname,
4112 TALLOC_CTX *mem_ctx,
4113 struct readdir_attr_data **pattr_data)
4115 struct fruit_config_data *config = NULL;
4116 struct readdir_attr_data *attr_data;
4117 uint32_t conv_flags = 0;
4118 NTSTATUS status;
4119 int ret;
4121 SMB_VFS_HANDLE_GET_DATA(handle, config,
4122 struct fruit_config_data,
4123 return NT_STATUS_UNSUCCESSFUL);
4125 if (!global_fruit_config.nego_aapl) {
4126 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4129 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4131 if (config->wipe_intentionally_left_blank_rfork) {
4132 conv_flags |= AD_CONV_WIPE_BLANK;
4134 if (config->delete_empty_adfiles) {
4135 conv_flags |= AD_CONV_DELETE;
4138 ret = ad_convert(handle,
4139 handle->conn->cwd_fsp,
4140 fname,
4141 macos_string_replace_map,
4142 conv_flags);
4143 if (ret != 0) {
4144 DBG_ERR("ad_convert() failed\n");
4145 return NT_STATUS_UNSUCCESSFUL;
4148 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4149 if (*pattr_data == NULL) {
4150 return NT_STATUS_UNSUCCESSFUL;
4152 attr_data = *pattr_data;
4153 attr_data->type = RDATTR_AAPL;
4156 * Mac metadata: compressed FinderInfo, resource fork length
4157 * and creation date
4159 status = readdir_attr_macmeta(handle, fname, attr_data);
4160 if (!NT_STATUS_IS_OK(status)) {
4162 * Error handling is tricky: if we return failure from
4163 * this function, the corresponding directory entry
4164 * will to be passed to the client, so we really just
4165 * want to error out on fatal errors.
4167 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4168 goto fail;
4173 * UNIX mode
4175 if (config->unix_info_enabled) {
4176 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4180 * max_access
4182 if (!config->readdir_attr_max_access) {
4183 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4184 } else {
4185 status = smbd_calculate_access_mask(
4186 handle->conn,
4187 fname,
4188 false,
4189 SEC_FLAG_MAXIMUM_ALLOWED,
4190 &attr_data->attr_data.aapl.max_access);
4191 if (!NT_STATUS_IS_OK(status)) {
4192 goto fail;
4196 return NT_STATUS_OK;
4198 fail:
4199 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4200 fname->base_name, nt_errstr(status)));
4201 TALLOC_FREE(*pattr_data);
4202 return status;
4205 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4206 files_struct *fsp,
4207 uint32_t security_info,
4208 TALLOC_CTX *mem_ctx,
4209 struct security_descriptor **ppdesc)
4211 NTSTATUS status;
4212 struct security_ace ace;
4213 struct dom_sid sid;
4214 struct fruit_config_data *config;
4216 SMB_VFS_HANDLE_GET_DATA(handle, config,
4217 struct fruit_config_data,
4218 return NT_STATUS_UNSUCCESSFUL);
4220 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4221 mem_ctx, ppdesc);
4222 if (!NT_STATUS_IS_OK(status)) {
4223 return status;
4227 * Add MS NFS style ACEs with uid, gid and mode
4229 if (!global_fruit_config.nego_aapl) {
4230 return NT_STATUS_OK;
4232 if (!config->unix_info_enabled) {
4233 return NT_STATUS_OK;
4236 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4237 status = remove_virtual_nfs_aces(*ppdesc);
4238 if (!NT_STATUS_IS_OK(status)) {
4239 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4240 return status;
4243 /* MS NFS style mode */
4244 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4245 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4246 status = security_descriptor_dacl_add(*ppdesc, &ace);
4247 if (!NT_STATUS_IS_OK(status)) {
4248 DEBUG(1,("failed to add MS NFS style ACE\n"));
4249 return status;
4252 /* MS NFS style uid */
4253 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4254 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4255 status = security_descriptor_dacl_add(*ppdesc, &ace);
4256 if (!NT_STATUS_IS_OK(status)) {
4257 DEBUG(1,("failed to add MS NFS style ACE\n"));
4258 return status;
4261 /* MS NFS style gid */
4262 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4263 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4264 status = security_descriptor_dacl_add(*ppdesc, &ace);
4265 if (!NT_STATUS_IS_OK(status)) {
4266 DEBUG(1,("failed to add MS NFS style ACE\n"));
4267 return status;
4270 return NT_STATUS_OK;
4273 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4274 files_struct *fsp,
4275 uint32_t security_info_sent,
4276 const struct security_descriptor *orig_psd)
4278 NTSTATUS status;
4279 bool do_chmod;
4280 mode_t ms_nfs_mode = 0;
4281 int result;
4282 struct security_descriptor *psd = NULL;
4283 uint32_t orig_num_aces = 0;
4285 if (orig_psd->dacl != NULL) {
4286 orig_num_aces = orig_psd->dacl->num_aces;
4289 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4290 if (psd == NULL) {
4291 return NT_STATUS_NO_MEMORY;
4294 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4296 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4297 if (!NT_STATUS_IS_OK(status)) {
4298 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4299 TALLOC_FREE(psd);
4300 return status;
4304 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4305 * sent/present flags correctly now we've removed them.
4308 if (orig_num_aces != 0) {
4310 * Are there any ACE's left ?
4312 if (psd->dacl->num_aces == 0) {
4313 /* No - clear the DACL sent/present flags. */
4314 security_info_sent &= ~SECINFO_DACL;
4315 psd->type &= ~SEC_DESC_DACL_PRESENT;
4319 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4320 if (!NT_STATUS_IS_OK(status)) {
4321 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4322 TALLOC_FREE(psd);
4323 return status;
4326 if (do_chmod) {
4327 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4328 if (result != 0) {
4329 DBG_WARNING("%s, result: %d, %04o error %s\n",
4330 fsp_str_dbg(fsp),
4331 result,
4332 (unsigned)ms_nfs_mode,
4333 strerror(errno));
4334 status = map_nt_error_from_unix(errno);
4335 TALLOC_FREE(psd);
4336 return status;
4340 TALLOC_FREE(psd);
4341 return NT_STATUS_OK;
4344 static struct vfs_offload_ctx *fruit_offload_ctx;
4346 struct fruit_offload_read_state {
4347 struct vfs_handle_struct *handle;
4348 struct tevent_context *ev;
4349 files_struct *fsp;
4350 uint32_t fsctl;
4351 DATA_BLOB token;
4354 static void fruit_offload_read_done(struct tevent_req *subreq);
4356 static struct tevent_req *fruit_offload_read_send(
4357 TALLOC_CTX *mem_ctx,
4358 struct tevent_context *ev,
4359 struct vfs_handle_struct *handle,
4360 files_struct *fsp,
4361 uint32_t fsctl,
4362 uint32_t ttl,
4363 off_t offset,
4364 size_t to_copy)
4366 struct tevent_req *req = NULL;
4367 struct tevent_req *subreq = NULL;
4368 struct fruit_offload_read_state *state = NULL;
4370 req = tevent_req_create(mem_ctx, &state,
4371 struct fruit_offload_read_state);
4372 if (req == NULL) {
4373 return NULL;
4375 *state = (struct fruit_offload_read_state) {
4376 .handle = handle,
4377 .ev = ev,
4378 .fsp = fsp,
4379 .fsctl = fsctl,
4382 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4383 fsctl, ttl, offset, to_copy);
4384 if (tevent_req_nomem(subreq, req)) {
4385 return tevent_req_post(req, ev);
4387 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4388 return req;
4391 static void fruit_offload_read_done(struct tevent_req *subreq)
4393 struct tevent_req *req = tevent_req_callback_data(
4394 subreq, struct tevent_req);
4395 struct fruit_offload_read_state *state = tevent_req_data(
4396 req, struct fruit_offload_read_state);
4397 NTSTATUS status;
4399 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4400 state->handle,
4401 state,
4402 &state->token);
4403 TALLOC_FREE(subreq);
4404 if (tevent_req_nterror(req, status)) {
4405 return;
4408 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4409 tevent_req_done(req);
4410 return;
4413 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4414 &fruit_offload_ctx);
4415 if (tevent_req_nterror(req, status)) {
4416 return;
4419 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4420 state->fsp,
4421 &state->token);
4422 if (tevent_req_nterror(req, status)) {
4423 return;
4426 tevent_req_done(req);
4427 return;
4430 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4431 struct vfs_handle_struct *handle,
4432 TALLOC_CTX *mem_ctx,
4433 DATA_BLOB *token)
4435 struct fruit_offload_read_state *state = tevent_req_data(
4436 req, struct fruit_offload_read_state);
4437 NTSTATUS status;
4439 if (tevent_req_is_nterror(req, &status)) {
4440 tevent_req_received(req);
4441 return status;
4444 token->length = state->token.length;
4445 token->data = talloc_move(mem_ctx, &state->token.data);
4447 tevent_req_received(req);
4448 return NT_STATUS_OK;
4451 struct fruit_offload_write_state {
4452 struct vfs_handle_struct *handle;
4453 off_t copied;
4454 struct files_struct *src_fsp;
4455 struct files_struct *dst_fsp;
4456 bool is_copyfile;
4459 static void fruit_offload_write_done(struct tevent_req *subreq);
4460 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4461 TALLOC_CTX *mem_ctx,
4462 struct tevent_context *ev,
4463 uint32_t fsctl,
4464 DATA_BLOB *token,
4465 off_t transfer_offset,
4466 struct files_struct *dest_fsp,
4467 off_t dest_off,
4468 off_t num)
4470 struct tevent_req *req, *subreq;
4471 struct fruit_offload_write_state *state;
4472 NTSTATUS status;
4473 struct fruit_config_data *config;
4474 off_t src_off = transfer_offset;
4475 files_struct *src_fsp = NULL;
4476 off_t to_copy = num;
4477 bool copyfile_enabled = false;
4479 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4480 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4482 SMB_VFS_HANDLE_GET_DATA(handle, config,
4483 struct fruit_config_data,
4484 return NULL);
4486 req = tevent_req_create(mem_ctx, &state,
4487 struct fruit_offload_write_state);
4488 if (req == NULL) {
4489 return NULL;
4491 state->handle = handle;
4492 state->dst_fsp = dest_fsp;
4494 switch (fsctl) {
4495 case FSCTL_SRV_COPYCHUNK:
4496 case FSCTL_SRV_COPYCHUNK_WRITE:
4497 copyfile_enabled = config->copyfile_enabled;
4498 break;
4499 default:
4500 break;
4504 * Check if this a OS X copyfile style copychunk request with
4505 * a requested chunk count of 0 that was translated to a
4506 * offload_write_send VFS call overloading the parameters src_off
4507 * = dest_off = num = 0.
4509 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4510 status = vfs_offload_token_db_fetch_fsp(
4511 fruit_offload_ctx, token, &src_fsp);
4512 if (tevent_req_nterror(req, status)) {
4513 return tevent_req_post(req, ev);
4515 state->src_fsp = src_fsp;
4517 status = vfs_stat_fsp(src_fsp);
4518 if (tevent_req_nterror(req, status)) {
4519 return tevent_req_post(req, ev);
4522 to_copy = src_fsp->fsp_name->st.st_ex_size;
4523 state->is_copyfile = true;
4526 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4527 mem_ctx,
4529 fsctl,
4530 token,
4531 transfer_offset,
4532 dest_fsp,
4533 dest_off,
4534 to_copy);
4535 if (tevent_req_nomem(subreq, req)) {
4536 return tevent_req_post(req, ev);
4539 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4540 return req;
4543 static void fruit_offload_write_done(struct tevent_req *subreq)
4545 struct tevent_req *req = tevent_req_callback_data(
4546 subreq, struct tevent_req);
4547 struct fruit_offload_write_state *state = tevent_req_data(
4548 req, struct fruit_offload_write_state);
4549 NTSTATUS status;
4550 unsigned int num_streams = 0;
4551 struct stream_struct *streams = NULL;
4552 unsigned int i;
4553 struct smb_filename *src_fname_tmp = NULL;
4554 struct smb_filename *dst_fname_tmp = NULL;
4556 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4557 subreq,
4558 &state->copied);
4559 TALLOC_FREE(subreq);
4560 if (tevent_req_nterror(req, status)) {
4561 return;
4564 if (!state->is_copyfile) {
4565 tevent_req_done(req);
4566 return;
4570 * Now copy all remaining streams. We know the share supports
4571 * streams, because we're in vfs_fruit. We don't do this async
4572 * because streams are few and small.
4574 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4575 state->src_fsp->fsp_name,
4576 req, &num_streams, &streams);
4577 if (tevent_req_nterror(req, status)) {
4578 return;
4581 if (num_streams == 1) {
4582 /* There is always one stream, ::$DATA. */
4583 tevent_req_done(req);
4584 return;
4587 for (i = 0; i < num_streams; i++) {
4588 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4589 __func__, streams[i].name, (size_t)streams[i].size));
4591 src_fname_tmp = synthetic_smb_fname(
4592 req,
4593 state->src_fsp->fsp_name->base_name,
4594 streams[i].name,
4595 NULL,
4596 state->src_fsp->fsp_name->flags);
4597 if (tevent_req_nomem(src_fname_tmp, req)) {
4598 return;
4601 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4602 TALLOC_FREE(src_fname_tmp);
4603 continue;
4606 dst_fname_tmp = synthetic_smb_fname(
4607 req,
4608 state->dst_fsp->fsp_name->base_name,
4609 streams[i].name,
4610 NULL,
4611 state->dst_fsp->fsp_name->flags);
4612 if (tevent_req_nomem(dst_fname_tmp, req)) {
4613 TALLOC_FREE(src_fname_tmp);
4614 return;
4617 status = copy_file(req,
4618 state->handle->conn,
4619 src_fname_tmp,
4620 dst_fname_tmp,
4621 OPENX_FILE_CREATE_IF_NOT_EXIST,
4622 0, false);
4623 if (!NT_STATUS_IS_OK(status)) {
4624 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4625 smb_fname_str_dbg(src_fname_tmp),
4626 smb_fname_str_dbg(dst_fname_tmp),
4627 nt_errstr(status)));
4628 TALLOC_FREE(src_fname_tmp);
4629 TALLOC_FREE(dst_fname_tmp);
4630 tevent_req_nterror(req, status);
4631 return;
4634 TALLOC_FREE(src_fname_tmp);
4635 TALLOC_FREE(dst_fname_tmp);
4638 TALLOC_FREE(streams);
4639 TALLOC_FREE(src_fname_tmp);
4640 TALLOC_FREE(dst_fname_tmp);
4641 tevent_req_done(req);
4644 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4645 struct tevent_req *req,
4646 off_t *copied)
4648 struct fruit_offload_write_state *state = tevent_req_data(
4649 req, struct fruit_offload_write_state);
4650 NTSTATUS status;
4652 if (tevent_req_is_nterror(req, &status)) {
4653 DEBUG(1, ("server side copy chunk failed: %s\n",
4654 nt_errstr(status)));
4655 *copied = 0;
4656 tevent_req_received(req);
4657 return status;
4660 *copied = state->copied;
4661 tevent_req_received(req);
4663 return NT_STATUS_OK;
4666 static char *fruit_get_bandsize_line(char **lines, int numlines)
4668 static regex_t re;
4669 static bool re_initialized = false;
4670 int i;
4671 int ret;
4673 if (!re_initialized) {
4674 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4675 if (ret != 0) {
4676 return NULL;
4678 re_initialized = true;
4681 for (i = 0; i < numlines; i++) {
4682 regmatch_t matches[1];
4684 ret = regexec(&re, lines[i], 1, matches, 0);
4685 if (ret == 0) {
4687 * Check if the match was on the last line, sa we want
4688 * the subsequent line.
4690 if (i + 1 == numlines) {
4691 return NULL;
4693 return lines[i + 1];
4695 if (ret != REG_NOMATCH) {
4696 return NULL;
4700 return NULL;
4703 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4705 static regex_t re;
4706 static bool re_initialized = false;
4707 regmatch_t matches[2];
4708 uint64_t band_size;
4709 int ret;
4710 bool ok;
4712 if (!re_initialized) {
4713 ret = regcomp(&re,
4714 "^[[:blank:]]*"
4715 "<integer>\\([[:digit:]]*\\)</integer>$",
4717 if (ret != 0) {
4718 return false;
4720 re_initialized = true;
4723 ret = regexec(&re, line, 2, matches, 0);
4724 if (ret != 0) {
4725 DBG_ERR("regex failed [%s]\n", line);
4726 return false;
4729 line[matches[1].rm_eo] = '\0';
4731 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4732 if (!ok) {
4733 return false;
4735 *_band_size = (size_t)band_size;
4736 return true;
4740 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4741 * "band-size" key and value.
4743 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4744 const char *dir,
4745 size_t *band_size)
4747 #define INFO_PLIST_MAX_SIZE 64*1024
4748 char *plist = NULL;
4749 struct smb_filename *smb_fname = NULL;
4750 files_struct *fsp = NULL;
4751 uint8_t *file_data = NULL;
4752 char **lines = NULL;
4753 char *band_size_line = NULL;
4754 size_t plist_file_size;
4755 ssize_t nread;
4756 int numlines;
4757 int ret;
4758 bool ok = false;
4759 NTSTATUS status;
4761 plist = talloc_asprintf(talloc_tos(),
4762 "%s/%s/Info.plist",
4763 handle->conn->connectpath,
4764 dir);
4765 if (plist == NULL) {
4766 ok = false;
4767 goto out;
4770 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
4771 if (smb_fname == NULL) {
4772 ok = false;
4773 goto out;
4776 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4777 if (ret != 0) {
4778 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4779 ok = true;
4780 goto out;
4783 plist_file_size = smb_fname->st.st_ex_size;
4785 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4786 DBG_INFO("%s is too large, ignoring\n", plist);
4787 ok = true;
4788 goto out;
4791 status = SMB_VFS_NEXT_CREATE_FILE(
4792 handle, /* conn */
4793 NULL, /* req */
4794 0, /* root_dir_fid */
4795 smb_fname, /* fname */
4796 FILE_GENERIC_READ, /* access_mask */
4797 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4798 FILE_OPEN, /* create_disposition */
4799 0, /* create_options */
4800 0, /* file_attributes */
4801 INTERNAL_OPEN_ONLY, /* oplock_request */
4802 NULL, /* lease */
4803 0, /* allocation_size */
4804 0, /* private_flags */
4805 NULL, /* sd */
4806 NULL, /* ea_list */
4807 &fsp, /* result */
4808 NULL, /* psbuf */
4809 NULL, NULL); /* create context */
4810 if (!NT_STATUS_IS_OK(status)) {
4811 DBG_INFO("Opening [%s] failed [%s]\n",
4812 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4813 ok = false;
4814 goto out;
4817 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
4818 if (file_data == NULL) {
4819 ok = false;
4820 goto out;
4823 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4824 if (nread != plist_file_size) {
4825 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4826 fsp_str_dbg(fsp), nread, plist_file_size);
4827 ok = false;
4828 goto out;
4832 status = close_file(NULL, fsp, NORMAL_CLOSE);
4833 fsp = NULL;
4834 if (!NT_STATUS_IS_OK(status)) {
4835 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4836 ok = false;
4837 goto out;
4840 lines = file_lines_parse((char *)file_data,
4841 plist_file_size,
4842 &numlines,
4843 talloc_tos());
4844 if (lines == NULL) {
4845 ok = false;
4846 goto out;
4849 band_size_line = fruit_get_bandsize_line(lines, numlines);
4850 if (band_size_line == NULL) {
4851 DBG_ERR("Didn't find band-size key in [%s]\n",
4852 smb_fname_str_dbg(smb_fname));
4853 ok = false;
4854 goto out;
4857 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
4858 if (!ok) {
4859 DBG_ERR("fruit_get_bandsize_from_line failed\n");
4860 goto out;
4863 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
4865 out:
4866 if (fsp != NULL) {
4867 status = close_file(NULL, fsp, NORMAL_CLOSE);
4868 if (!NT_STATUS_IS_OK(status)) {
4869 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4871 fsp = NULL;
4873 TALLOC_FREE(plist);
4874 TALLOC_FREE(smb_fname);
4875 TALLOC_FREE(file_data);
4876 TALLOC_FREE(lines);
4877 return ok;
4880 struct fruit_disk_free_state {
4881 off_t total_size;
4884 static bool fruit_get_num_bands(vfs_handle_struct *handle,
4885 char *bundle,
4886 size_t *_nbands)
4888 char *path = NULL;
4889 struct smb_filename *bands_dir = NULL;
4890 DIR *d = NULL;
4891 struct dirent *e = NULL;
4892 size_t nbands;
4893 int ret;
4895 path = talloc_asprintf(talloc_tos(),
4896 "%s/%s/bands",
4897 handle->conn->connectpath,
4898 bundle);
4899 if (path == NULL) {
4900 return false;
4903 bands_dir = synthetic_smb_fname(talloc_tos(),
4904 path,
4905 NULL,
4906 NULL,
4908 TALLOC_FREE(path);
4909 if (bands_dir == NULL) {
4910 return false;
4913 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
4914 if (d == NULL) {
4915 TALLOC_FREE(bands_dir);
4916 return false;
4919 nbands = 0;
4921 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
4922 e != NULL;
4923 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
4925 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
4926 continue;
4928 nbands++;
4931 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
4932 if (ret != 0) {
4933 TALLOC_FREE(bands_dir);
4934 return false;
4937 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
4939 TALLOC_FREE(bands_dir);
4941 *_nbands = nbands;
4942 return true;
4945 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
4946 struct fruit_disk_free_state *state,
4947 struct dirent *e)
4949 bool ok;
4950 char *p = NULL;
4951 size_t sparsebundle_strlen = strlen("sparsebundle");
4952 size_t bandsize = 0;
4953 size_t nbands;
4954 off_t tm_size;
4956 p = strstr(e->d_name, "sparsebundle");
4957 if (p == NULL) {
4958 return true;
4961 if (p[sparsebundle_strlen] != '\0') {
4962 return true;
4965 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
4967 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
4968 if (!ok) {
4970 * Beware of race conditions: this may be an uninitialized
4971 * Info.plist that a client is just creating. We don't want let
4972 * this to trigger complete failure.
4974 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
4975 return true;
4978 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
4979 if (!ok) {
4981 * Beware of race conditions: this may be a backup sparsebundle
4982 * in an early stage lacking a bands subdirectory. We don't want
4983 * let this to trigger complete failure.
4985 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
4986 return true;
4990 * Arithmetic on 32-bit systems may cause overflow, depending on
4991 * size_t precision. First we check its unlikely, then we
4992 * force the precision into target off_t, then we check that
4993 * the total did not overflow either.
4995 if (bandsize > SIZE_MAX/nbands) {
4996 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
4997 bandsize, nbands);
4998 return false;
5000 tm_size = (off_t)bandsize * (off_t)nbands;
5002 if (state->total_size + tm_size < state->total_size) {
5003 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5004 bandsize, nbands);
5005 return false;
5008 state->total_size += tm_size;
5010 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5011 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
5013 return true;
5017 * Calculate used size of a TimeMachine volume
5019 * This assumes that the volume is used only for TimeMachine.
5021 * - readdir(basedir of share), then
5022 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5023 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5024 * - count band files in "\1.sparsebundle/bands/"
5025 * - calculate used size of all bands: band_count * band_size
5027 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5028 const struct smb_filename *smb_fname,
5029 uint64_t *_bsize,
5030 uint64_t *_dfree,
5031 uint64_t *_dsize)
5033 struct fruit_config_data *config = NULL;
5034 struct fruit_disk_free_state state = {0};
5035 DIR *d = NULL;
5036 struct dirent *e = NULL;
5037 uint64_t dfree;
5038 uint64_t dsize;
5039 int ret;
5040 bool ok;
5042 SMB_VFS_HANDLE_GET_DATA(handle, config,
5043 struct fruit_config_data,
5044 return UINT64_MAX);
5046 if (!config->time_machine ||
5047 config->time_machine_max_size == 0)
5049 return SMB_VFS_NEXT_DISK_FREE(handle,
5050 smb_fname,
5051 _bsize,
5052 _dfree,
5053 _dsize);
5056 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
5057 if (d == NULL) {
5058 return UINT64_MAX;
5061 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
5062 e != NULL;
5063 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
5065 ok = fruit_tmsize_do_dirent(handle, &state, e);
5066 if (!ok) {
5067 SMB_VFS_NEXT_CLOSEDIR(handle, d);
5068 return UINT64_MAX;
5072 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
5073 if (ret != 0) {
5074 return UINT64_MAX;
5077 dsize = config->time_machine_max_size / 512;
5078 dfree = dsize - (state.total_size / 512);
5079 if (dfree > dsize) {
5080 dfree = 0;
5083 *_bsize = 512;
5084 *_dsize = dsize;
5085 *_dfree = dfree;
5086 return dfree / 2;
5089 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5090 const SMB_STRUCT_STAT *psbuf)
5092 struct fruit_config_data *config = NULL;
5094 SMB_VFS_HANDLE_GET_DATA(handle, config,
5095 struct fruit_config_data,
5096 return 0);
5098 if (global_fruit_config.nego_aapl &&
5099 config->aapl_zero_file_id)
5101 return 0;
5104 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5107 static struct vfs_fn_pointers vfs_fruit_fns = {
5108 .connect_fn = fruit_connect,
5109 .disk_free_fn = fruit_disk_free,
5111 /* File operations */
5112 .chmod_fn = fruit_chmod,
5113 .unlinkat_fn = fruit_unlinkat,
5114 .renameat_fn = fruit_renameat,
5115 .open_fn = fruit_open,
5116 .close_fn = fruit_close,
5117 .pread_fn = fruit_pread,
5118 .pwrite_fn = fruit_pwrite,
5119 .pread_send_fn = fruit_pread_send,
5120 .pread_recv_fn = fruit_pread_recv,
5121 .pwrite_send_fn = fruit_pwrite_send,
5122 .pwrite_recv_fn = fruit_pwrite_recv,
5123 .stat_fn = fruit_stat,
5124 .lstat_fn = fruit_lstat,
5125 .fstat_fn = fruit_fstat,
5126 .streaminfo_fn = fruit_streaminfo,
5127 .ntimes_fn = fruit_ntimes,
5128 .ftruncate_fn = fruit_ftruncate,
5129 .fallocate_fn = fruit_fallocate,
5130 .create_file_fn = fruit_create_file,
5131 .readdir_attr_fn = fruit_readdir_attr,
5132 .offload_read_send_fn = fruit_offload_read_send,
5133 .offload_read_recv_fn = fruit_offload_read_recv,
5134 .offload_write_send_fn = fruit_offload_write_send,
5135 .offload_write_recv_fn = fruit_offload_write_recv,
5136 .fs_file_id_fn = fruit_fs_file_id,
5138 /* NT ACL operations */
5139 .fget_nt_acl_fn = fruit_fget_nt_acl,
5140 .fset_nt_acl_fn = fruit_fset_nt_acl,
5143 static_decl_vfs;
5144 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5146 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5147 &vfs_fruit_fns);
5148 if (!NT_STATUS_IS_OK(ret)) {
5149 return ret;
5152 vfs_fruit_debug_level = debug_add_class("fruit");
5153 if (vfs_fruit_debug_level == -1) {
5154 vfs_fruit_debug_level = DBGC_VFS;
5155 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5156 "vfs_fruit_init"));
5157 } else {
5158 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5159 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5162 return ret;