CVE-2023-0614 ldb: Centralise checking for inaccessible matches
[Samba.git] / source3 / modules / vfs_fruit.c
blobba210ac6aceed668efd08ab91b9b0d3c3057f3e4
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;
147 /* Recursion guard. Will go away when we have STATX. */
148 bool in_openat_pathref_fsp;
151 static const struct enum_list fruit_rsrc[] = {
152 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
153 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
154 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
155 { -1, NULL}
158 static const struct enum_list fruit_meta[] = {
159 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
160 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
161 { -1, NULL}
164 static const struct enum_list fruit_locking[] = {
165 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
166 {FRUIT_LOCKING_NONE, "none"},
167 { -1, NULL}
170 static const struct enum_list fruit_encoding[] = {
171 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
172 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
173 { -1, NULL}
176 struct fio {
177 vfs_handle_struct *handle;
178 files_struct *fsp; /* backlink to itself */
180 /* tcon config handle */
181 struct fruit_config_data *config;
183 /* Backend fsp for AppleDouble file, can be NULL */
184 files_struct *ad_fsp;
185 /* link from adouble_open_from_base_fsp() to fio */
186 struct fio *real_fio;
188 /* Denote stream type, meta or rsrc */
189 adouble_type_t type;
192 * AFP_AfpInfo stream created, but not written yet, thus still a fake
193 * pipe fd. This is set to true in fruit_open_meta if there was no
194 * existing stream but the caller requested O_CREAT. It is later set to
195 * false when we get a write on the stream that then does open and
196 * create the stream.
198 bool fake_fd;
199 int flags;
200 int mode;
203 /*****************************************************************************
204 * Helper functions
205 *****************************************************************************/
207 static struct adouble *ad_get_meta_fsp(TALLOC_CTX *ctx,
208 vfs_handle_struct *handle,
209 const struct smb_filename *smb_fname)
211 NTSTATUS status;
212 struct adouble *ad = NULL;
213 struct smb_filename *smb_fname_cp = NULL;
214 struct fruit_config_data *config = NULL;
216 if (smb_fname->fsp != NULL) {
217 return ad_get(ctx, handle, smb_fname, ADOUBLE_META);
220 SMB_VFS_HANDLE_GET_DATA(handle,
221 config,
222 struct fruit_config_data,
223 return NULL);
225 if (config->in_openat_pathref_fsp) {
226 return NULL;
229 smb_fname_cp = cp_smb_filename(ctx,
230 smb_fname);
231 if (smb_fname_cp == NULL) {
232 return NULL;
234 TALLOC_FREE(smb_fname_cp->stream_name);
235 config->in_openat_pathref_fsp = true;
236 status = openat_pathref_fsp(handle->conn->cwd_fsp,
237 smb_fname_cp);
238 config->in_openat_pathref_fsp = false;
239 if (!NT_STATUS_IS_OK(status)) {
240 TALLOC_FREE(smb_fname_cp);
241 return NULL;
244 ad = ad_get(ctx, handle, smb_fname_cp, ADOUBLE_META);
245 TALLOC_FREE(smb_fname_cp);
246 return ad;
249 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
250 files_struct *fsp)
252 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
254 if (fio == NULL) {
255 return NULL;
258 if (fio->real_fio != NULL) {
260 * This is an fsp from adouble_open_from_base_fsp()
261 * we should just pass this to the next
262 * module.
264 return NULL;
267 return fio;
271 * Initialize config struct from our smb.conf config parameters
273 static int init_fruit_config(vfs_handle_struct *handle)
275 struct fruit_config_data *config;
276 int enumval;
277 const char *tm_size_str = NULL;
279 config = talloc_zero(handle->conn, struct fruit_config_data);
280 if (!config) {
281 DEBUG(1, ("talloc_zero() failed\n"));
282 errno = ENOMEM;
283 return -1;
287 * Versions up to Samba 4.5.x had a spelling bug in the
288 * fruit:resource option calling lp_parm_enum with
289 * "res*s*ource" (ie two s).
291 * In Samba 4.6 we accept both the wrong and the correct
292 * spelling, in Samba 4.7 the bad spelling will be removed.
294 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
295 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
296 if (enumval == -1) {
297 DEBUG(1, ("value for %s: resource type unknown\n",
298 FRUIT_PARAM_TYPE_NAME));
299 return -1;
301 config->rsrc = (enum fruit_rsrc)enumval;
303 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
304 "resource", fruit_rsrc, enumval);
305 if (enumval == -1) {
306 DEBUG(1, ("value for %s: resource type unknown\n",
307 FRUIT_PARAM_TYPE_NAME));
308 return -1;
310 config->rsrc = (enum fruit_rsrc)enumval;
312 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
313 "metadata", fruit_meta, FRUIT_META_NETATALK);
314 if (enumval == -1) {
315 DEBUG(1, ("value for %s: metadata type unknown\n",
316 FRUIT_PARAM_TYPE_NAME));
317 return -1;
319 config->meta = (enum fruit_meta)enumval;
321 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
322 "locking", fruit_locking, FRUIT_LOCKING_NONE);
323 if (enumval == -1) {
324 DEBUG(1, ("value for %s: locking type unknown\n",
325 FRUIT_PARAM_TYPE_NAME));
326 return -1;
328 config->locking = (enum fruit_locking)enumval;
330 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
331 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
332 if (enumval == -1) {
333 DEBUG(1, ("value for %s: encoding type unknown\n",
334 FRUIT_PARAM_TYPE_NAME));
335 return -1;
337 config->encoding = (enum fruit_encoding)enumval;
339 if (config->rsrc == FRUIT_RSRC_ADFILE) {
340 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
341 FRUIT_PARAM_TYPE_NAME,
342 "veto_appledouble",
343 true);
346 config->use_aapl = lp_parm_bool(
347 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
349 config->time_machine = lp_parm_bool(
350 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
352 config->unix_info_enabled = lp_parm_bool(
353 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
355 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
356 "copyfile", false);
358 config->posix_rename = lp_parm_bool(
359 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
361 config->aapl_zero_file_id =
362 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
363 "zero_file_id", false);
365 config->readdir_attr_rsize = lp_parm_bool(
366 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
368 config->readdir_attr_finder_info = lp_parm_bool(
369 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
371 config->readdir_attr_max_access = lp_parm_bool(
372 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
374 config->model = lp_parm_const_string(
375 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
377 tm_size_str = lp_parm_const_string(
378 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
379 "time machine max size", NULL);
380 if (tm_size_str != NULL) {
381 config->time_machine_max_size = conv_str_size(tm_size_str);
384 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
385 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
386 "wipe_intentionally_left_blank_rfork", false);
388 config->delete_empty_adfiles = lp_parm_bool(
389 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
390 "delete_empty_adfiles", false);
392 SMB_VFS_HANDLE_SET_DATA(handle, config,
393 NULL, struct fruit_config_data,
394 return -1);
396 return 0;
399 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
400 struct stream_struct **streams,
401 const char *name, off_t size,
402 off_t alloc_size)
404 struct stream_struct *tmp;
406 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
407 (*num_streams)+1);
408 if (tmp == NULL) {
409 return false;
412 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
413 if (tmp[*num_streams].name == NULL) {
414 return false;
417 tmp[*num_streams].size = size;
418 tmp[*num_streams].alloc_size = alloc_size;
420 *streams = tmp;
421 *num_streams += 1;
422 return true;
425 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
426 struct stream_struct **streams)
428 struct stream_struct *tmp = *streams;
429 unsigned int i;
431 if (*num_streams == 0) {
432 return true;
435 for (i = 0; i < *num_streams; i++) {
436 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
437 break;
441 if (i == *num_streams) {
442 return true;
445 if (tmp[i].size > 0) {
446 return true;
449 TALLOC_FREE(tmp[i].name);
450 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
451 *num_streams -= 1;
452 return true;
455 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
456 struct stream_struct **streams,
457 const char *name)
459 struct stream_struct *tmp = *streams;
460 unsigned int i;
462 if (*num_streams == 0) {
463 return true;
466 for (i = 0; i < *num_streams; i++) {
467 if (strequal_m(tmp[i].name, name)) {
468 break;
472 if (i == *num_streams) {
473 return true;
476 TALLOC_FREE(tmp[i].name);
477 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
478 *num_streams -= 1;
479 return true;
482 static bool ad_empty_finderinfo(const struct adouble *ad)
484 int cmp;
485 char emptybuf[ADEDLEN_FINDERI] = {0};
486 char *fi = NULL;
488 fi = ad_get_entry(ad, ADEID_FINDERI);
489 if (fi == NULL) {
490 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
491 return false;
494 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
495 return (cmp == 0);
498 static bool ai_empty_finderinfo(const AfpInfo *ai)
500 int cmp;
501 char emptybuf[ADEDLEN_FINDERI] = {0};
503 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
504 return (cmp == 0);
508 * Update btime with btime from Netatalk
510 static void update_btime(vfs_handle_struct *handle,
511 struct smb_filename *smb_fname)
513 uint32_t t;
514 struct timespec creation_time = {0};
515 struct adouble *ad;
516 struct fruit_config_data *config = NULL;
518 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
519 return);
521 switch (config->meta) {
522 case FRUIT_META_STREAM:
523 return;
524 case FRUIT_META_NETATALK:
525 /* Handled below */
526 break;
527 default:
528 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
529 return;
532 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
533 if (ad == NULL) {
534 return;
536 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
537 TALLOC_FREE(ad);
538 return;
540 TALLOC_FREE(ad);
542 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
543 update_stat_ex_create_time(&smb_fname->st, creation_time);
545 return;
549 * Map an access mask to a Netatalk single byte byte range lock
551 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
552 uint32_t access_mask)
554 off_t offset;
556 switch (access_mask) {
557 case FILE_READ_DATA:
558 offset = AD_FILELOCK_OPEN_RD;
559 break;
561 case FILE_WRITE_DATA:
562 case FILE_APPEND_DATA:
563 offset = AD_FILELOCK_OPEN_WR;
564 break;
566 default:
567 offset = AD_FILELOCK_OPEN_NONE;
568 break;
571 if (fork_type == APPLE_FORK_RSRC) {
572 if (offset == AD_FILELOCK_OPEN_NONE) {
573 offset = AD_FILELOCK_RSRC_OPEN_NONE;
574 } else {
575 offset += 2;
579 return offset;
583 * Map a deny mode to a Netatalk brl
585 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
586 uint32_t deny_mode)
588 off_t offset = 0;
590 switch (deny_mode) {
591 case DENY_READ:
592 offset = AD_FILELOCK_DENY_RD;
593 break;
595 case DENY_WRITE:
596 offset = AD_FILELOCK_DENY_WR;
597 break;
599 default:
600 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
603 if (fork_type == APPLE_FORK_RSRC) {
604 offset += 2;
607 return offset;
611 * Call fcntl() with an exclusive F_GETLK request in order to
612 * determine if there's an existing shared lock
614 * @return true if the requested lock was found or any error occurred
615 * false if the lock was not found
617 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
619 bool result;
620 off_t offset = in_offset;
621 off_t len = 1;
622 int type = F_WRLCK;
623 pid_t pid = 0;
625 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
626 if (result == false) {
627 return true;
630 if (type != F_UNLCK) {
631 return true;
634 return false;
637 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
638 files_struct *fsp,
639 uint32_t access_mask,
640 uint32_t share_mode)
642 NTSTATUS status = NT_STATUS_OK;
643 off_t off;
644 bool share_for_read = (share_mode & FILE_SHARE_READ);
645 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
646 bool netatalk_already_open_for_reading = false;
647 bool netatalk_already_open_for_writing = false;
648 bool netatalk_already_open_with_deny_read = false;
649 bool netatalk_already_open_with_deny_write = false;
650 struct GUID req_guid = GUID_random();
652 /* FIXME: hardcoded data fork, add resource fork */
653 enum apple_fork fork_type = APPLE_FORK_DATA;
655 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
656 fsp_str_dbg(fsp),
657 access_mask & FILE_READ_DATA ? "READ" :"-",
658 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
659 share_mode);
661 if (fsp_get_io_fd(fsp) == -1) {
662 return NT_STATUS_OK;
665 /* Read NetATalk opens and deny modes on the file. */
666 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
667 access_to_netatalk_brl(fork_type,
668 FILE_READ_DATA));
670 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
671 denymode_to_netatalk_brl(fork_type,
672 DENY_READ));
674 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
675 access_to_netatalk_brl(fork_type,
676 FILE_WRITE_DATA));
678 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
679 denymode_to_netatalk_brl(fork_type,
680 DENY_WRITE));
682 /* If there are any conflicts - sharing violation. */
683 if ((access_mask & FILE_READ_DATA) &&
684 netatalk_already_open_with_deny_read) {
685 return NT_STATUS_SHARING_VIOLATION;
688 if (!share_for_read &&
689 netatalk_already_open_for_reading) {
690 return NT_STATUS_SHARING_VIOLATION;
693 if ((access_mask & FILE_WRITE_DATA) &&
694 netatalk_already_open_with_deny_write) {
695 return NT_STATUS_SHARING_VIOLATION;
698 if (!share_for_write &&
699 netatalk_already_open_for_writing) {
700 return NT_STATUS_SHARING_VIOLATION;
703 if (!(access_mask & FILE_READ_DATA)) {
705 * Nothing we can do here, we need read access
706 * to set locks.
708 return NT_STATUS_OK;
711 /* Set NetAtalk locks matching our access */
712 if (access_mask & FILE_READ_DATA) {
713 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
714 req_guid.time_hi_and_version = __LINE__;
715 status = do_lock(
716 fsp,
717 talloc_tos(),
718 &req_guid,
719 fsp->op->global->open_persistent_id,
721 off,
722 READ_LOCK,
723 POSIX_LOCK,
724 NULL,
725 NULL);
727 if (!NT_STATUS_IS_OK(status)) {
728 return status;
732 if (!share_for_read) {
733 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
734 req_guid.time_hi_and_version = __LINE__;
735 status = do_lock(
736 fsp,
737 talloc_tos(),
738 &req_guid,
739 fsp->op->global->open_persistent_id,
741 off,
742 READ_LOCK,
743 POSIX_LOCK,
744 NULL,
745 NULL);
747 if (!NT_STATUS_IS_OK(status)) {
748 return status;
752 if (access_mask & FILE_WRITE_DATA) {
753 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
754 req_guid.time_hi_and_version = __LINE__;
755 status = do_lock(
756 fsp,
757 talloc_tos(),
758 &req_guid,
759 fsp->op->global->open_persistent_id,
761 off,
762 READ_LOCK,
763 POSIX_LOCK,
764 NULL,
765 NULL);
767 if (!NT_STATUS_IS_OK(status)) {
768 return status;
772 if (!share_for_write) {
773 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
774 req_guid.time_hi_and_version = __LINE__;
775 status = do_lock(
776 fsp,
777 talloc_tos(),
778 &req_guid,
779 fsp->op->global->open_persistent_id,
781 off,
782 READ_LOCK,
783 POSIX_LOCK,
784 NULL,
785 NULL);
787 if (!NT_STATUS_IS_OK(status)) {
788 return status;
792 return NT_STATUS_OK;
795 static NTSTATUS check_aapl(vfs_handle_struct *handle,
796 struct smb_request *req,
797 const struct smb2_create_blobs *in_context_blobs,
798 struct smb2_create_blobs *out_context_blobs)
800 struct fruit_config_data *config;
801 NTSTATUS status;
802 struct smb2_create_blob *aapl = NULL;
803 uint32_t cmd;
804 bool ok;
805 uint8_t p[16];
806 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
807 uint64_t req_bitmap, client_caps;
808 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
809 smb_ucs2_t *model;
810 size_t modellen;
812 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
813 return NT_STATUS_UNSUCCESSFUL);
815 if (!config->use_aapl
816 || in_context_blobs == NULL
817 || out_context_blobs == NULL) {
818 return NT_STATUS_OK;
821 aapl = smb2_create_blob_find(in_context_blobs,
822 SMB2_CREATE_TAG_AAPL);
823 if (aapl == NULL) {
824 return NT_STATUS_OK;
827 if (aapl->data.length != 24) {
828 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
829 (uintmax_t)aapl->data.length));
830 return NT_STATUS_INVALID_PARAMETER;
833 cmd = IVAL(aapl->data.data, 0);
834 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
835 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
836 return NT_STATUS_INVALID_PARAMETER;
839 req_bitmap = BVAL(aapl->data.data, 8);
840 client_caps = BVAL(aapl->data.data, 16);
842 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
843 SIVAL(p, 4, 0);
844 SBVAL(p, 8, req_bitmap);
845 ok = data_blob_append(req, &blob, p, 16);
846 if (!ok) {
847 return NT_STATUS_UNSUCCESSFUL;
850 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
851 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
852 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
853 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
854 config->readdir_attr_enabled = true;
857 if (config->use_copyfile) {
858 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
859 config->copyfile_enabled = true;
863 * The client doesn't set the flag, so we can't check
864 * for it and just set it unconditionally
866 if (config->unix_info_enabled) {
867 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
870 SBVAL(p, 0, server_caps);
871 ok = data_blob_append(req, &blob, p, 8);
872 if (!ok) {
873 return NT_STATUS_UNSUCCESSFUL;
877 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
878 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
879 uint64_t caps = 0;
881 switch (val) {
882 case Auto:
883 break;
885 case True:
886 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
887 break;
889 default:
890 break;
893 if (config->time_machine) {
894 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
897 SBVAL(p, 0, caps);
899 ok = data_blob_append(req, &blob, p, 8);
900 if (!ok) {
901 return NT_STATUS_UNSUCCESSFUL;
905 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
906 ok = convert_string_talloc(req,
907 CH_UNIX, CH_UTF16LE,
908 config->model, strlen(config->model),
909 &model, &modellen);
910 if (!ok) {
911 return NT_STATUS_UNSUCCESSFUL;
914 SIVAL(p, 0, 0);
915 SIVAL(p + 4, 0, modellen);
916 ok = data_blob_append(req, &blob, p, 8);
917 if (!ok) {
918 talloc_free(model);
919 return NT_STATUS_UNSUCCESSFUL;
922 ok = data_blob_append(req, &blob, model, modellen);
923 talloc_free(model);
924 if (!ok) {
925 return NT_STATUS_UNSUCCESSFUL;
929 status = smb2_create_blob_add(out_context_blobs,
930 out_context_blobs,
931 SMB2_CREATE_TAG_AAPL,
932 blob);
933 if (NT_STATUS_IS_OK(status)) {
934 global_fruit_config.nego_aapl = true;
937 return status;
940 static bool readdir_attr_meta_finderi_stream(
941 struct vfs_handle_struct *handle,
942 const struct smb_filename *smb_fname,
943 AfpInfo *ai)
945 struct smb_filename *stream_name = NULL;
946 files_struct *fsp = NULL;
947 ssize_t nread;
948 NTSTATUS status;
949 bool ok;
950 uint8_t buf[AFP_INFO_SIZE];
952 status = synthetic_pathref(talloc_tos(),
953 handle->conn->cwd_fsp,
954 smb_fname->base_name,
955 AFPINFO_STREAM_NAME,
956 NULL,
957 smb_fname->twrp,
958 smb_fname->flags,
959 &stream_name);
960 if (!NT_STATUS_IS_OK(status)) {
961 return false;
964 status = SMB_VFS_CREATE_FILE(
965 handle->conn, /* conn */
966 NULL, /* req */
967 stream_name, /* fname */
968 FILE_READ_DATA, /* access_mask */
969 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
970 FILE_SHARE_DELETE),
971 FILE_OPEN, /* create_disposition*/
972 0, /* create_options */
973 0, /* file_attributes */
974 INTERNAL_OPEN_ONLY, /* oplock_request */
975 NULL, /* lease */
976 0, /* allocation_size */
977 0, /* private_flags */
978 NULL, /* sd */
979 NULL, /* ea_list */
980 &fsp, /* result */
981 NULL, /* pinfo */
982 NULL, NULL); /* create context */
984 TALLOC_FREE(stream_name);
986 if (!NT_STATUS_IS_OK(status)) {
987 return false;
990 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
991 if (nread != AFP_INFO_SIZE) {
992 DBG_ERR("short read [%s] [%zd/%d]\n",
993 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
994 ok = false;
995 goto fail;
998 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
999 AFP_FinderSize);
1001 ok = true;
1003 fail:
1004 if (fsp != NULL) {
1005 close_file_free(NULL, &fsp, NORMAL_CLOSE);
1008 return ok;
1011 static bool readdir_attr_meta_finderi_netatalk(
1012 struct vfs_handle_struct *handle,
1013 const struct smb_filename *smb_fname,
1014 AfpInfo *ai)
1016 struct adouble *ad = NULL;
1017 char *p = NULL;
1019 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
1020 if (ad == NULL) {
1021 return false;
1024 p = ad_get_entry(ad, ADEID_FINDERI);
1025 if (p == NULL) {
1026 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
1027 TALLOC_FREE(ad);
1028 return false;
1031 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1032 TALLOC_FREE(ad);
1033 return true;
1036 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1037 const struct smb_filename *smb_fname,
1038 struct readdir_attr_data *attr_data)
1040 struct fruit_config_data *config = NULL;
1041 uint32_t date_added;
1042 AfpInfo ai = {0};
1043 bool ok;
1045 SMB_VFS_HANDLE_GET_DATA(handle, config,
1046 struct fruit_config_data,
1047 return false);
1049 switch (config->meta) {
1050 case FRUIT_META_NETATALK:
1051 ok = readdir_attr_meta_finderi_netatalk(
1052 handle, smb_fname, &ai);
1053 break;
1055 case FRUIT_META_STREAM:
1056 ok = readdir_attr_meta_finderi_stream(
1057 handle, smb_fname, &ai);
1058 break;
1060 default:
1061 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1062 return false;
1065 if (!ok) {
1066 /* Don't bother with errors, it's likely ENOENT */
1067 return true;
1070 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1071 /* finder_type */
1072 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1073 &ai.afpi_FinderInfo[0], 4);
1075 /* finder_creator */
1076 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1077 &ai.afpi_FinderInfo[4], 4);
1080 /* finder_flags */
1081 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1082 &ai.afpi_FinderInfo[8], 2);
1084 /* finder_ext_flags */
1085 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1086 &ai.afpi_FinderInfo[24], 2);
1088 /* creation date */
1089 date_added = convert_time_t_to_uint32_t(
1090 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1092 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1094 return true;
1097 static uint64_t readdir_attr_rfork_size_adouble(
1098 struct vfs_handle_struct *handle,
1099 const struct smb_filename *smb_fname)
1101 struct adouble *ad = NULL;
1102 uint64_t rfork_size;
1104 ad = ad_get(talloc_tos(), handle, smb_fname,
1105 ADOUBLE_RSRC);
1106 if (ad == NULL) {
1107 return 0;
1110 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1111 TALLOC_FREE(ad);
1113 return rfork_size;
1116 static uint64_t readdir_attr_rfork_size_stream(
1117 struct vfs_handle_struct *handle,
1118 const struct smb_filename *smb_fname)
1120 struct smb_filename *stream_name = NULL;
1121 int ret;
1122 uint64_t rfork_size;
1124 stream_name = synthetic_smb_fname(talloc_tos(),
1125 smb_fname->base_name,
1126 AFPRESOURCE_STREAM_NAME,
1127 NULL,
1128 smb_fname->twrp,
1130 if (stream_name == NULL) {
1131 return 0;
1134 ret = SMB_VFS_STAT(handle->conn, stream_name);
1135 if (ret != 0) {
1136 TALLOC_FREE(stream_name);
1137 return 0;
1140 rfork_size = stream_name->st.st_ex_size;
1141 TALLOC_FREE(stream_name);
1143 return rfork_size;
1146 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1147 const struct smb_filename *smb_fname)
1149 struct fruit_config_data *config = NULL;
1150 uint64_t rfork_size;
1152 SMB_VFS_HANDLE_GET_DATA(handle, config,
1153 struct fruit_config_data,
1154 return 0);
1156 switch (config->rsrc) {
1157 case FRUIT_RSRC_ADFILE:
1158 rfork_size = readdir_attr_rfork_size_adouble(handle,
1159 smb_fname);
1160 break;
1162 case FRUIT_RSRC_XATTR:
1163 case FRUIT_RSRC_STREAM:
1164 rfork_size = readdir_attr_rfork_size_stream(handle,
1165 smb_fname);
1166 break;
1168 default:
1169 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1170 rfork_size = 0;
1171 break;
1174 return rfork_size;
1177 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1178 const struct smb_filename *smb_fname,
1179 struct readdir_attr_data *attr_data)
1181 NTSTATUS status = NT_STATUS_OK;
1182 struct fruit_config_data *config = NULL;
1183 bool ok;
1185 SMB_VFS_HANDLE_GET_DATA(handle, config,
1186 struct fruit_config_data,
1187 return NT_STATUS_UNSUCCESSFUL);
1190 /* Ensure we return a default value in the creation_date field */
1191 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1194 * Resource fork length
1197 if (config->readdir_attr_rsize) {
1198 uint64_t rfork_size;
1200 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1201 attr_data->attr_data.aapl.rfork_size = rfork_size;
1205 * FinderInfo
1208 if (config->readdir_attr_finder_info) {
1209 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1210 if (!ok) {
1211 status = NT_STATUS_INTERNAL_ERROR;
1215 return status;
1218 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1220 NTSTATUS status;
1221 uint32_t i;
1223 if (psd->dacl == NULL) {
1224 return NT_STATUS_OK;
1227 for (i = 0; i < psd->dacl->num_aces; i++) {
1228 /* MS NFS style mode/uid/gid */
1229 int cmp = dom_sid_compare_domain(
1230 &global_sid_Unix_NFS,
1231 &psd->dacl->aces[i].trustee);
1232 if (cmp != 0) {
1233 /* Normal ACE entry. */
1234 continue;
1238 * security_descriptor_dacl_del()
1239 * *must* return NT_STATUS_OK as we know
1240 * we have something to remove.
1243 status = security_descriptor_dacl_del(psd,
1244 &psd->dacl->aces[i].trustee);
1245 if (!NT_STATUS_IS_OK(status)) {
1246 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1247 nt_errstr(status));
1248 return status;
1252 * security_descriptor_dacl_del() may delete more
1253 * then one entry subsequent to this one if the
1254 * SID matches, but we only need to ensure that
1255 * we stay looking at the same element in the array.
1257 i--;
1259 return NT_STATUS_OK;
1262 /* Search MS NFS style ACE with UNIX mode */
1263 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1264 files_struct *fsp,
1265 struct security_descriptor *psd,
1266 mode_t *pmode,
1267 bool *pdo_chmod)
1269 uint32_t i;
1270 struct fruit_config_data *config = NULL;
1272 *pdo_chmod = false;
1274 SMB_VFS_HANDLE_GET_DATA(handle, config,
1275 struct fruit_config_data,
1276 return NT_STATUS_UNSUCCESSFUL);
1278 if (!global_fruit_config.nego_aapl) {
1279 return NT_STATUS_OK;
1281 if (psd->dacl == NULL || !config->unix_info_enabled) {
1282 return NT_STATUS_OK;
1285 for (i = 0; i < psd->dacl->num_aces; i++) {
1286 if (dom_sid_compare_domain(
1287 &global_sid_Unix_NFS_Mode,
1288 &psd->dacl->aces[i].trustee) == 0) {
1289 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1290 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1291 *pdo_chmod = true;
1293 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1294 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1295 break;
1300 * Remove any incoming virtual ACE entries generated by
1301 * fruit_fget_nt_acl().
1304 return remove_virtual_nfs_aces(psd);
1307 /****************************************************************************
1308 * VFS ops
1309 ****************************************************************************/
1311 static int fruit_connect(vfs_handle_struct *handle,
1312 const char *service,
1313 const char *user)
1315 int rc;
1316 char *list = NULL, *newlist = NULL;
1317 struct fruit_config_data *config;
1318 const struct loadparm_substitution *lp_sub =
1319 loadparm_s3_global_substitution();
1321 DEBUG(10, ("fruit_connect\n"));
1323 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1324 if (rc < 0) {
1325 return rc;
1328 rc = init_fruit_config(handle);
1329 if (rc != 0) {
1330 return rc;
1333 SMB_VFS_HANDLE_GET_DATA(handle, config,
1334 struct fruit_config_data, return -1);
1336 if (config->veto_appledouble) {
1337 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1339 if (list) {
1340 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1341 newlist = talloc_asprintf(
1342 list,
1343 "%s/" ADOUBLE_NAME_PREFIX "*/",
1344 list);
1345 lp_do_parameter(SNUM(handle->conn),
1346 "veto files",
1347 newlist);
1349 } else {
1350 lp_do_parameter(SNUM(handle->conn),
1351 "veto files",
1352 "/" ADOUBLE_NAME_PREFIX "*/");
1355 TALLOC_FREE(list);
1358 if (config->encoding == FRUIT_ENC_NATIVE) {
1359 lp_do_parameter(SNUM(handle->conn),
1360 "catia:mappings",
1361 macos_string_replace_map);
1364 if (config->time_machine) {
1365 DBG_NOTICE("Enabling durable handles for Time Machine "
1366 "support on [%s]\n", service);
1367 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1368 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1369 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1370 if (!lp_strict_sync(SNUM(handle->conn))) {
1371 DBG_WARNING("Time Machine without strict sync is not "
1372 "recommended!\n");
1374 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1377 return rc;
1380 static void fio_ref_destroy_fn(void *p_data)
1382 struct fio *ref_fio = (struct fio *)p_data;
1383 if (ref_fio->real_fio != NULL) {
1384 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1385 ref_fio->real_fio->ad_fsp = NULL;
1386 ref_fio->real_fio = NULL;
1390 static void fio_close_ad_fsp(struct fio *fio)
1392 if (fio->ad_fsp != NULL) {
1393 fd_close(fio->ad_fsp);
1394 file_free(NULL, fio->ad_fsp);
1395 /* fio_ref_destroy_fn() should have cleared this */
1396 SMB_ASSERT(fio->ad_fsp == NULL);
1400 static void fio_destroy_fn(void *p_data)
1402 struct fio *fio = (struct fio *)p_data;
1403 fio_close_ad_fsp(fio);
1406 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1407 const struct files_struct *dirfsp,
1408 const struct smb_filename *smb_fname,
1409 files_struct *fsp,
1410 int flags,
1411 mode_t mode)
1413 struct fruit_config_data *config = NULL;
1414 struct fio *fio = NULL;
1415 int open_flags = flags & ~O_CREAT;
1416 int fd;
1418 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1420 SMB_VFS_HANDLE_GET_DATA(handle, config,
1421 struct fruit_config_data, return -1);
1423 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1424 fio->handle = handle;
1425 fio->fsp = fsp;
1426 fio->type = ADOUBLE_META;
1427 fio->config = config;
1429 fd = SMB_VFS_NEXT_OPENAT(handle,
1430 dirfsp,
1431 smb_fname,
1432 fsp,
1433 open_flags,
1434 mode);
1435 if (fd != -1) {
1436 return fd;
1439 if (!(flags & O_CREAT)) {
1440 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1441 return -1;
1444 fd = vfs_fake_fd();
1445 if (fd == -1) {
1446 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1447 return -1;
1450 fio->fake_fd = true;
1451 fio->flags = flags;
1452 fio->mode = mode;
1454 return fd;
1457 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1458 const struct files_struct *dirfsp,
1459 const struct smb_filename *smb_fname,
1460 files_struct *fsp,
1461 int flags,
1462 mode_t mode)
1464 struct fruit_config_data *config = NULL;
1465 struct fio *fio = NULL;
1466 struct adouble *ad = NULL;
1467 bool meta_exists = false;
1468 int fd;
1470 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1473 * We know this is a stream open, so fsp->base_fsp must
1474 * already be open.
1476 SMB_ASSERT(fsp->base_fsp != NULL);
1477 SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1479 ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1480 if (ad != NULL) {
1481 meta_exists = true;
1484 TALLOC_FREE(ad);
1486 if (!meta_exists && !(flags & O_CREAT)) {
1487 errno = ENOENT;
1488 return -1;
1491 fd = vfs_fake_fd();
1492 if (fd == -1) {
1493 return -1;
1496 SMB_VFS_HANDLE_GET_DATA(handle, config,
1497 struct fruit_config_data, return -1);
1499 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1500 fio->handle = handle;
1501 fio->fsp = fsp;
1502 fio->type = ADOUBLE_META;
1503 fio->config = config;
1504 fio->fake_fd = true;
1505 fio->flags = flags;
1506 fio->mode = mode;
1508 return fd;
1511 static int fruit_open_meta(vfs_handle_struct *handle,
1512 const struct files_struct *dirfsp,
1513 const struct smb_filename *smb_fname,
1514 files_struct *fsp, int flags, mode_t mode)
1516 int fd;
1517 struct fruit_config_data *config = NULL;
1519 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1521 SMB_VFS_HANDLE_GET_DATA(handle, config,
1522 struct fruit_config_data, return -1);
1524 switch (config->meta) {
1525 case FRUIT_META_STREAM:
1526 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1527 fsp, flags, mode);
1528 break;
1530 case FRUIT_META_NETATALK:
1531 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1532 fsp, flags, mode);
1533 break;
1535 default:
1536 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1537 return -1;
1540 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1542 return fd;
1545 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1546 const struct files_struct *dirfsp,
1547 const struct smb_filename *smb_fname,
1548 files_struct *fsp,
1549 int flags,
1550 mode_t mode)
1552 int rc = 0;
1553 struct fruit_config_data *config = NULL;
1554 struct files_struct *ad_fsp = NULL;
1555 struct fio *fio = NULL;
1556 struct fio *ref_fio = NULL;
1557 NTSTATUS status;
1558 int fd = -1;
1560 SMB_VFS_HANDLE_GET_DATA(handle, config,
1561 struct fruit_config_data, return -1);
1563 if ((!(flags & O_CREAT)) &&
1564 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1566 /* sorry, but directories don't habe a resource fork */
1567 errno = EISDIR;
1568 rc = -1;
1569 goto exit;
1573 * We return a fake_fd to the vfs modules above,
1574 * while we open an internal backend fsp for the
1575 * '._' file for the next vfs modules.
1577 * Note that adouble_open_from_base_fsp() recurses
1578 * into fruit_openat(), but it'll just pass to
1579 * the next module as just opens a flat file on
1580 * disk.
1583 fd = vfs_fake_fd();
1584 if (fd == -1) {
1585 rc = fd;
1586 goto exit;
1589 status = adouble_open_from_base_fsp(dirfsp,
1590 fsp->base_fsp,
1591 ADOUBLE_RSRC,
1592 flags,
1593 mode,
1594 &ad_fsp);
1595 if (!NT_STATUS_IS_OK(status)) {
1596 errno = map_errno_from_nt_status(status);
1597 rc = -1;
1598 goto exit;
1602 * Now we need to glue both handles together,
1603 * so that they automatically detach each other
1604 * on close.
1606 fio = fruit_get_complete_fio(handle, fsp);
1607 if (fio == NULL) {
1608 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1609 errno = EBADF;
1610 rc = -1;
1611 goto exit;
1614 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1615 struct fio,
1616 fio_ref_destroy_fn);
1617 if (ref_fio == NULL) {
1618 int saved_errno = errno;
1619 fd_close(ad_fsp);
1620 file_free(NULL, ad_fsp);
1621 ad_fsp = NULL;
1622 errno = saved_errno;
1623 rc = -1;
1624 goto exit;
1627 SMB_ASSERT(ref_fio->fsp == NULL);
1628 ref_fio->handle = handle;
1629 ref_fio->fsp = ad_fsp;
1630 ref_fio->type = ADOUBLE_RSRC;
1631 ref_fio->config = config;
1632 ref_fio->real_fio = fio;
1633 SMB_ASSERT(fio->ad_fsp == NULL);
1634 fio->ad_fsp = ad_fsp;
1635 fio->fake_fd = true;
1637 exit:
1639 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1640 if (rc != 0) {
1641 int saved_errno = errno;
1642 if (fd != -1) {
1643 vfs_fake_fd_close(fd);
1645 errno = saved_errno;
1646 return rc;
1648 return fd;
1651 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1652 const struct files_struct *dirfsp,
1653 const struct smb_filename *smb_fname,
1654 files_struct *fsp,
1655 int flags,
1656 mode_t mode)
1658 #ifdef HAVE_ATTROPEN
1659 int fd = -1;
1662 * As there's no attropenat() this is only going to work with AT_FDCWD.
1664 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1666 fd = attropen(smb_fname->base_name,
1667 AFPRESOURCE_EA_NETATALK,
1668 flags,
1669 mode);
1670 if (fd == -1) {
1671 return -1;
1674 return fd;
1676 #else
1677 errno = ENOSYS;
1678 return -1;
1679 #endif
1682 static int fruit_open_rsrc(vfs_handle_struct *handle,
1683 const struct files_struct *dirfsp,
1684 const struct smb_filename *smb_fname,
1685 files_struct *fsp, int flags, mode_t mode)
1687 int fd;
1688 struct fruit_config_data *config = NULL;
1689 struct fio *fio = NULL;
1691 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1693 SMB_VFS_HANDLE_GET_DATA(handle, config,
1694 struct fruit_config_data, return -1);
1696 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1697 fio->handle = handle;
1698 fio->fsp = fsp;
1699 fio->type = ADOUBLE_RSRC;
1700 fio->config = config;
1702 switch (config->rsrc) {
1703 case FRUIT_RSRC_STREAM:
1704 fd = SMB_VFS_NEXT_OPENAT(handle,
1705 dirfsp,
1706 smb_fname,
1707 fsp,
1708 flags,
1709 mode);
1710 break;
1712 case FRUIT_RSRC_ADFILE:
1713 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1714 fsp, flags, mode);
1715 break;
1717 case FRUIT_RSRC_XATTR:
1718 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1719 fsp, flags, mode);
1720 break;
1722 default:
1723 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1724 return -1;
1727 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1729 if (fd == -1) {
1730 return -1;
1733 return fd;
1736 static int fruit_openat(vfs_handle_struct *handle,
1737 const struct files_struct *dirfsp,
1738 const struct smb_filename *smb_fname,
1739 files_struct *fsp,
1740 int flags,
1741 mode_t mode)
1743 int fd;
1745 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1747 if (!is_named_stream(smb_fname)) {
1748 return SMB_VFS_NEXT_OPENAT(handle,
1749 dirfsp,
1750 smb_fname,
1751 fsp,
1752 flags,
1753 mode);
1756 if (is_afpinfo_stream(smb_fname->stream_name)) {
1757 fd = fruit_open_meta(handle,
1758 dirfsp,
1759 smb_fname,
1760 fsp,
1761 flags,
1762 mode);
1763 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1764 fd = fruit_open_rsrc(handle,
1765 dirfsp,
1766 smb_fname,
1767 fsp,
1768 flags,
1769 mode);
1770 } else {
1771 fd = SMB_VFS_NEXT_OPENAT(handle,
1772 dirfsp,
1773 smb_fname,
1774 fsp,
1775 flags,
1776 mode);
1779 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1781 /* Prevent reopen optimisation */
1782 fsp->fsp_flags.have_proc_fds = false;
1783 return fd;
1786 static int fruit_close_meta(vfs_handle_struct *handle,
1787 files_struct *fsp)
1789 int ret;
1790 struct fruit_config_data *config = NULL;
1792 SMB_VFS_HANDLE_GET_DATA(handle, config,
1793 struct fruit_config_data, return -1);
1795 switch (config->meta) {
1796 case FRUIT_META_STREAM:
1798 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1799 if (fio == NULL) {
1800 return -1;
1802 if (fio->fake_fd) {
1803 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1804 fsp_set_fd(fsp, -1);
1805 } else {
1806 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1808 break;
1810 case FRUIT_META_NETATALK:
1811 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1812 fsp_set_fd(fsp, -1);
1813 break;
1815 default:
1816 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1817 return -1;
1820 return ret;
1824 static int fruit_close_rsrc(vfs_handle_struct *handle,
1825 files_struct *fsp)
1827 int ret;
1828 struct fruit_config_data *config = NULL;
1830 SMB_VFS_HANDLE_GET_DATA(handle, config,
1831 struct fruit_config_data, return -1);
1833 switch (config->rsrc) {
1834 case FRUIT_RSRC_STREAM:
1835 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1836 break;
1838 case FRUIT_RSRC_ADFILE:
1840 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1841 if (fio == NULL) {
1842 return -1;
1844 fio_close_ad_fsp(fio);
1845 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1846 fsp_set_fd(fsp, -1);
1847 break;
1850 case FRUIT_RSRC_XATTR:
1851 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1852 fsp_set_fd(fsp, -1);
1853 break;
1855 default:
1856 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1857 return -1;
1860 return ret;
1863 static int fruit_close(vfs_handle_struct *handle,
1864 files_struct *fsp)
1866 int ret;
1867 int fd;
1869 fd = fsp_get_pathref_fd(fsp);
1871 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1873 if (!is_named_stream(fsp->fsp_name)) {
1874 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1877 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1878 ret = fruit_close_meta(handle, fsp);
1879 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1880 ret = fruit_close_rsrc(handle, fsp);
1881 } else {
1882 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1885 return ret;
1888 static int fruit_renameat(struct vfs_handle_struct *handle,
1889 files_struct *srcfsp,
1890 const struct smb_filename *smb_fname_src,
1891 files_struct *dstfsp,
1892 const struct smb_filename *smb_fname_dst)
1894 int rc = -1;
1895 struct fruit_config_data *config = NULL;
1896 struct smb_filename *src_adp_smb_fname = NULL;
1897 struct smb_filename *dst_adp_smb_fname = NULL;
1899 SMB_VFS_HANDLE_GET_DATA(handle, config,
1900 struct fruit_config_data, return -1);
1902 if (!VALID_STAT(smb_fname_src->st)) {
1903 DBG_ERR("Need valid stat for [%s]\n",
1904 smb_fname_str_dbg(smb_fname_src));
1905 return -1;
1908 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1909 srcfsp,
1910 smb_fname_src,
1911 dstfsp,
1912 smb_fname_dst);
1913 if (rc != 0) {
1914 return -1;
1917 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1918 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1920 return 0;
1923 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1924 if (rc != 0) {
1925 goto done;
1928 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1929 if (rc != 0) {
1930 goto done;
1933 DBG_DEBUG("%s -> %s\n",
1934 smb_fname_str_dbg(src_adp_smb_fname),
1935 smb_fname_str_dbg(dst_adp_smb_fname));
1937 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1938 srcfsp,
1939 src_adp_smb_fname,
1940 dstfsp,
1941 dst_adp_smb_fname);
1942 if (errno == ENOENT) {
1943 rc = 0;
1946 done:
1947 TALLOC_FREE(src_adp_smb_fname);
1948 TALLOC_FREE(dst_adp_smb_fname);
1949 return rc;
1952 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1953 struct files_struct *dirfsp,
1954 const struct smb_filename *smb_fname)
1956 return SMB_VFS_NEXT_UNLINKAT(handle,
1957 dirfsp,
1958 smb_fname,
1962 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1963 const struct smb_filename *smb_fname)
1965 SMB_ASSERT(smb_fname->fsp != NULL);
1966 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
1967 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1968 AFPINFO_EA_NETATALK);
1971 static int fruit_unlink_meta(vfs_handle_struct *handle,
1972 struct files_struct *dirfsp,
1973 const struct smb_filename *smb_fname)
1975 struct fruit_config_data *config = NULL;
1976 int rc;
1978 SMB_VFS_HANDLE_GET_DATA(handle, config,
1979 struct fruit_config_data, return -1);
1981 switch (config->meta) {
1982 case FRUIT_META_STREAM:
1983 rc = fruit_unlink_meta_stream(handle,
1984 dirfsp,
1985 smb_fname);
1986 break;
1988 case FRUIT_META_NETATALK:
1989 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1990 break;
1992 default:
1993 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
1994 return -1;
1997 return rc;
2000 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2001 struct files_struct *dirfsp,
2002 const struct smb_filename *smb_fname,
2003 bool force_unlink)
2005 int ret;
2007 if (!force_unlink) {
2008 struct smb_filename *full_fname = NULL;
2009 off_t size;
2012 * TODO: use SMB_VFS_STATX() once we have it.
2015 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2016 dirfsp,
2017 smb_fname);
2018 if (full_fname == NULL) {
2019 return -1;
2023 * 0 byte resource fork streams are not listed by
2024 * vfs_streaminfo, as a result stream cleanup/deletion of file
2025 * deletion doesn't remove the resourcefork stream.
2028 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2029 if (ret != 0) {
2030 TALLOC_FREE(full_fname);
2031 DBG_ERR("stat [%s] failed [%s]\n",
2032 smb_fname_str_dbg(full_fname), strerror(errno));
2033 return -1;
2036 size = full_fname->st.st_ex_size;
2037 TALLOC_FREE(full_fname);
2039 if (size > 0) {
2040 /* OS X ignores resource fork stream delete requests */
2041 return 0;
2045 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2046 dirfsp,
2047 smb_fname,
2049 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2050 ret = 0;
2053 return ret;
2056 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2057 struct files_struct *dirfsp,
2058 const struct smb_filename *smb_fname,
2059 bool force_unlink)
2061 int rc;
2062 struct adouble *ad = NULL;
2063 struct smb_filename *adp_smb_fname = NULL;
2065 if (!force_unlink) {
2066 struct smb_filename *full_fname = NULL;
2068 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2069 dirfsp,
2070 smb_fname);
2071 if (full_fname == NULL) {
2072 return -1;
2075 ad = ad_get(talloc_tos(), handle, full_fname,
2076 ADOUBLE_RSRC);
2077 TALLOC_FREE(full_fname);
2078 if (ad == NULL) {
2079 errno = ENOENT;
2080 return -1;
2085 * 0 byte resource fork streams are not listed by
2086 * vfs_streaminfo, as a result stream cleanup/deletion of file
2087 * deletion doesn't remove the resourcefork stream.
2090 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2091 /* OS X ignores resource fork stream delete requests */
2092 TALLOC_FREE(ad);
2093 return 0;
2096 TALLOC_FREE(ad);
2099 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2100 if (rc != 0) {
2101 return -1;
2104 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2105 dirfsp,
2106 adp_smb_fname,
2108 TALLOC_FREE(adp_smb_fname);
2109 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2110 rc = 0;
2113 return rc;
2116 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2117 const struct smb_filename *smb_fname,
2118 bool force_unlink)
2121 * OS X ignores resource fork stream delete requests, so nothing to do
2122 * here. Removing the file will remove the xattr anyway, so we don't
2123 * have to take care of removing 0 byte resource forks that could be
2124 * left behind.
2126 return 0;
2129 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2130 struct files_struct *dirfsp,
2131 const struct smb_filename *smb_fname,
2132 bool force_unlink)
2134 struct fruit_config_data *config = NULL;
2135 int rc;
2137 SMB_VFS_HANDLE_GET_DATA(handle, config,
2138 struct fruit_config_data, return -1);
2140 switch (config->rsrc) {
2141 case FRUIT_RSRC_STREAM:
2142 rc = fruit_unlink_rsrc_stream(handle,
2143 dirfsp,
2144 smb_fname,
2145 force_unlink);
2146 break;
2148 case FRUIT_RSRC_ADFILE:
2149 rc = fruit_unlink_rsrc_adouble(handle,
2150 dirfsp,
2151 smb_fname,
2152 force_unlink);
2153 break;
2155 case FRUIT_RSRC_XATTR:
2156 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2157 break;
2159 default:
2160 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2161 return -1;
2164 return rc;
2167 static int fruit_fchmod(vfs_handle_struct *handle,
2168 struct files_struct *fsp,
2169 mode_t mode)
2171 int rc = -1;
2172 struct fruit_config_data *config = NULL;
2173 struct smb_filename *smb_fname_adp = NULL;
2174 const struct smb_filename *smb_fname = NULL;
2175 NTSTATUS status;
2177 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2178 if (rc != 0) {
2179 return rc;
2182 smb_fname = fsp->fsp_name;
2183 SMB_VFS_HANDLE_GET_DATA(handle, config,
2184 struct fruit_config_data, return -1);
2186 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2187 return 0;
2190 if (!VALID_STAT(smb_fname->st)) {
2191 return 0;
2194 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2195 return 0;
2198 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2199 if (rc != 0) {
2200 return -1;
2203 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2204 smb_fname_adp);
2205 if (!NT_STATUS_IS_OK(status)) {
2206 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2207 if (NT_STATUS_EQUAL(status,
2208 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2209 rc = 0;
2210 goto out;
2212 rc = -1;
2213 goto out;
2216 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2218 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2219 if (errno == ENOENT) {
2220 rc = 0;
2222 out:
2223 TALLOC_FREE(smb_fname_adp);
2224 return rc;
2227 static int fruit_unlinkat(vfs_handle_struct *handle,
2228 struct files_struct *dirfsp,
2229 const struct smb_filename *smb_fname,
2230 int flags)
2232 struct fruit_config_data *config = NULL;
2233 struct smb_filename *rsrc_smb_fname = NULL;
2234 int ret;
2236 if (flags & AT_REMOVEDIR) {
2237 return SMB_VFS_NEXT_UNLINKAT(handle,
2238 dirfsp,
2239 smb_fname,
2240 AT_REMOVEDIR);
2243 SMB_VFS_HANDLE_GET_DATA(handle, config,
2244 struct fruit_config_data, return -1);
2246 if (is_afpinfo_stream(smb_fname->stream_name)) {
2247 return fruit_unlink_meta(handle,
2248 dirfsp,
2249 smb_fname);
2250 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2251 return fruit_unlink_rsrc(handle,
2252 dirfsp,
2253 smb_fname,
2254 false);
2255 } else if (is_named_stream(smb_fname)) {
2256 return SMB_VFS_NEXT_UNLINKAT(handle,
2257 dirfsp,
2258 smb_fname,
2260 } else if (is_adouble_file(smb_fname->base_name)) {
2261 return SMB_VFS_NEXT_UNLINKAT(handle,
2262 dirfsp,
2263 smb_fname,
2268 * A request to delete the base file. Because 0 byte resource
2269 * fork streams are not listed by fruit_streaminfo,
2270 * delete_all_streams() can't remove 0 byte resource fork
2271 * streams, so we have to cleanup this here.
2273 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2274 smb_fname->base_name,
2275 AFPRESOURCE_STREAM_NAME,
2276 NULL,
2277 smb_fname->twrp,
2278 smb_fname->flags);
2279 if (rsrc_smb_fname == NULL) {
2280 return -1;
2283 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2284 if ((ret != 0) && (errno != ENOENT)) {
2285 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2286 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2287 TALLOC_FREE(rsrc_smb_fname);
2288 return -1;
2290 TALLOC_FREE(rsrc_smb_fname);
2292 return SMB_VFS_NEXT_UNLINKAT(handle,
2293 dirfsp,
2294 smb_fname,
2298 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2299 files_struct *fsp, void *data,
2300 size_t n, off_t offset)
2302 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2303 ssize_t nread;
2304 int ret;
2306 if ((fio == NULL) || fio->fake_fd) {
2307 return -1;
2310 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2311 if (nread == -1 || nread == n) {
2312 return nread;
2315 DBG_ERR("Removing [%s] after short read [%zd]\n",
2316 fsp_str_dbg(fsp), nread);
2318 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2319 fsp->conn->cwd_fsp,
2320 fsp->fsp_name,
2322 if (ret != 0) {
2323 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2324 return -1;
2327 errno = EINVAL;
2328 return -1;
2331 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2332 files_struct *fsp, void *data,
2333 size_t n, off_t offset)
2335 AfpInfo *ai = NULL;
2336 struct adouble *ad = NULL;
2337 char afpinfo_buf[AFP_INFO_SIZE];
2338 char *p = NULL;
2339 ssize_t nread;
2341 ai = afpinfo_new(talloc_tos());
2342 if (ai == NULL) {
2343 return -1;
2346 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2347 if (ad == NULL) {
2348 nread = -1;
2349 goto fail;
2352 p = ad_get_entry(ad, ADEID_FINDERI);
2353 if (p == NULL) {
2354 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2355 nread = -1;
2356 goto fail;
2359 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2361 nread = afpinfo_pack(ai, afpinfo_buf);
2362 if (nread != AFP_INFO_SIZE) {
2363 nread = -1;
2364 goto fail;
2367 memcpy(data, afpinfo_buf, n);
2368 nread = n;
2370 fail:
2371 TALLOC_FREE(ai);
2372 return nread;
2375 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2376 files_struct *fsp, void *data,
2377 size_t n, off_t offset)
2379 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2380 ssize_t nread;
2381 ssize_t to_return;
2384 * OS X has a off-by-1 error in the offset calculation, so we're
2385 * bug compatible here. It won't hurt, as any relevant real
2386 * world read requests from the AFP_AfpInfo stream will be
2387 * offset=0 n=60. offset is ignored anyway, see below.
2389 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2390 return 0;
2393 if (fio == NULL) {
2394 DBG_ERR("Failed to fetch fsp extension");
2395 return -1;
2398 /* Yes, macOS always reads from offset 0 */
2399 offset = 0;
2400 to_return = MIN(n, AFP_INFO_SIZE);
2402 switch (fio->config->meta) {
2403 case FRUIT_META_STREAM:
2404 nread = fruit_pread_meta_stream(handle, fsp, data,
2405 to_return, offset);
2406 break;
2408 case FRUIT_META_NETATALK:
2409 nread = fruit_pread_meta_adouble(handle, fsp, data,
2410 to_return, offset);
2411 break;
2413 default:
2414 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2415 return -1;
2418 if (nread == -1 && fio->fake_fd) {
2419 AfpInfo *ai = NULL;
2420 char afpinfo_buf[AFP_INFO_SIZE];
2422 ai = afpinfo_new(talloc_tos());
2423 if (ai == NULL) {
2424 return -1;
2427 nread = afpinfo_pack(ai, afpinfo_buf);
2428 TALLOC_FREE(ai);
2429 if (nread != AFP_INFO_SIZE) {
2430 return -1;
2433 memcpy(data, afpinfo_buf, to_return);
2434 return to_return;
2437 return nread;
2440 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2441 files_struct *fsp, void *data,
2442 size_t n, off_t offset)
2444 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2447 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2448 files_struct *fsp, void *data,
2449 size_t n, off_t offset)
2451 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2454 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2455 files_struct *fsp, void *data,
2456 size_t n, off_t offset)
2458 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2459 struct adouble *ad = NULL;
2460 ssize_t nread;
2462 if (fio == NULL || fio->ad_fsp == NULL) {
2463 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2464 errno = EBADF;
2465 return -1;
2468 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2469 if (ad == NULL) {
2470 DBG_ERR("ad_fget [%s] failed [%s]\n",
2471 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2472 return -1;
2475 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2476 offset + ad_getentryoff(ad, ADEID_RFORK));
2478 TALLOC_FREE(ad);
2479 return nread;
2482 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2483 files_struct *fsp, void *data,
2484 size_t n, off_t offset)
2486 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2487 ssize_t nread;
2489 if (fio == NULL) {
2490 errno = EINVAL;
2491 return -1;
2494 switch (fio->config->rsrc) {
2495 case FRUIT_RSRC_STREAM:
2496 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2497 break;
2499 case FRUIT_RSRC_ADFILE:
2500 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2501 break;
2503 case FRUIT_RSRC_XATTR:
2504 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2505 break;
2507 default:
2508 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2509 return -1;
2512 return nread;
2515 static ssize_t fruit_pread(vfs_handle_struct *handle,
2516 files_struct *fsp, void *data,
2517 size_t n, off_t offset)
2519 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2520 ssize_t nread;
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 SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2529 if (fio->type == ADOUBLE_META) {
2530 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2531 } else {
2532 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2535 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2536 return nread;
2539 static bool fruit_must_handle_aio_stream(struct fio *fio)
2541 if (fio == NULL) {
2542 return false;
2545 if (fio->type == ADOUBLE_META) {
2546 return true;
2549 if ((fio->type == ADOUBLE_RSRC) &&
2550 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2552 return true;
2555 return false;
2558 struct fruit_pread_state {
2559 ssize_t nread;
2560 struct vfs_aio_state vfs_aio_state;
2563 static void fruit_pread_done(struct tevent_req *subreq);
2565 static struct tevent_req *fruit_pread_send(
2566 struct vfs_handle_struct *handle,
2567 TALLOC_CTX *mem_ctx,
2568 struct tevent_context *ev,
2569 struct files_struct *fsp,
2570 void *data,
2571 size_t n, off_t offset)
2573 struct tevent_req *req = NULL;
2574 struct tevent_req *subreq = NULL;
2575 struct fruit_pread_state *state = NULL;
2576 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2578 req = tevent_req_create(mem_ctx, &state,
2579 struct fruit_pread_state);
2580 if (req == NULL) {
2581 return NULL;
2584 if (fruit_must_handle_aio_stream(fio)) {
2585 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2586 if (state->nread != n) {
2587 if (state->nread != -1) {
2588 errno = EIO;
2590 tevent_req_error(req, errno);
2591 return tevent_req_post(req, ev);
2593 tevent_req_done(req);
2594 return tevent_req_post(req, ev);
2597 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2598 data, n, offset);
2599 if (tevent_req_nomem(req, subreq)) {
2600 return tevent_req_post(req, ev);
2602 tevent_req_set_callback(subreq, fruit_pread_done, req);
2603 return req;
2606 static void fruit_pread_done(struct tevent_req *subreq)
2608 struct tevent_req *req = tevent_req_callback_data(
2609 subreq, struct tevent_req);
2610 struct fruit_pread_state *state = tevent_req_data(
2611 req, struct fruit_pread_state);
2613 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2614 TALLOC_FREE(subreq);
2616 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2617 return;
2619 tevent_req_done(req);
2622 static ssize_t fruit_pread_recv(struct tevent_req *req,
2623 struct vfs_aio_state *vfs_aio_state)
2625 struct fruit_pread_state *state = tevent_req_data(
2626 req, struct fruit_pread_state);
2627 ssize_t retval = -1;
2629 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2630 tevent_req_received(req);
2631 return -1;
2634 *vfs_aio_state = state->vfs_aio_state;
2635 retval = state->nread;
2636 tevent_req_received(req);
2637 return retval;
2640 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2641 files_struct *fsp, const void *data,
2642 size_t n, off_t offset)
2644 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2645 AfpInfo *ai = NULL;
2646 size_t nwritten;
2647 int ret;
2648 bool ok;
2650 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2651 fsp_str_dbg(fsp), (intmax_t)offset, n);
2653 if (fio == NULL) {
2654 return -1;
2657 if (fio->fake_fd) {
2658 int fd = fsp_get_pathref_fd(fsp);
2660 ret = vfs_fake_fd_close(fd);
2661 fsp_set_fd(fsp, -1);
2662 if (ret != 0) {
2663 DBG_ERR("Close [%s] failed: %s\n",
2664 fsp_str_dbg(fsp), strerror(errno));
2665 return -1;
2668 fd = SMB_VFS_NEXT_OPENAT(handle,
2669 fsp->conn->cwd_fsp,
2670 fsp->fsp_name,
2671 fsp,
2672 fio->flags,
2673 fio->mode);
2674 if (fd == -1) {
2675 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2676 fsp_str_dbg(fsp), strerror(errno));
2677 return -1;
2679 fsp_set_fd(fsp, fd);
2680 fio->fake_fd = false;
2683 ai = afpinfo_unpack(talloc_tos(), data);
2684 if (ai == NULL) {
2685 return -1;
2688 if (ai_empty_finderinfo(ai)) {
2690 * Writing an all 0 blob to the metadata stream results in the
2691 * stream being removed on a macOS server. This ensures we
2692 * behave the same and it verified by the "delete AFP_AfpInfo by
2693 * writing all 0" test.
2695 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2696 if (ret != 0) {
2697 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2698 fsp_str_dbg(fsp));
2699 return -1;
2702 ok = set_delete_on_close(
2703 fsp,
2704 true,
2705 handle->conn->session_info->security_token,
2706 handle->conn->session_info->unix_token);
2707 if (!ok) {
2708 DBG_ERR("set_delete_on_close on [%s] failed\n",
2709 fsp_str_dbg(fsp));
2710 return -1;
2712 return n;
2715 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2716 if (nwritten != n) {
2717 return -1;
2720 return n;
2723 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2724 files_struct *fsp, const void *data,
2725 size_t n, off_t offset)
2727 struct adouble *ad = NULL;
2728 AfpInfo *ai = NULL;
2729 char *p = NULL;
2730 int ret;
2731 bool ok;
2733 ai = afpinfo_unpack(talloc_tos(), data);
2734 if (ai == NULL) {
2735 return -1;
2738 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2739 if (ad == NULL) {
2740 ad = ad_init(talloc_tos(), ADOUBLE_META);
2741 if (ad == NULL) {
2742 return -1;
2745 p = ad_get_entry(ad, ADEID_FINDERI);
2746 if (p == NULL) {
2747 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2748 TALLOC_FREE(ad);
2749 return -1;
2752 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2754 ret = ad_fset(handle, ad, fsp);
2755 if (ret != 0) {
2756 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2757 TALLOC_FREE(ad);
2758 return -1;
2761 TALLOC_FREE(ad);
2763 if (!ai_empty_finderinfo(ai)) {
2764 return n;
2768 * Writing an all 0 blob to the metadata stream results in the stream
2769 * being removed on a macOS server. This ensures we behave the same and
2770 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2773 ok = set_delete_on_close(
2774 fsp,
2775 true,
2776 handle->conn->session_info->security_token,
2777 handle->conn->session_info->unix_token);
2778 if (!ok) {
2779 DBG_ERR("set_delete_on_close on [%s] failed\n",
2780 fsp_str_dbg(fsp));
2781 return -1;
2784 return n;
2787 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2788 files_struct *fsp, const void *data,
2789 size_t n, off_t offset)
2791 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2792 ssize_t nwritten;
2793 uint8_t buf[AFP_INFO_SIZE];
2794 size_t to_write;
2795 size_t to_copy;
2796 int cmp;
2798 if (fio == NULL) {
2799 DBG_ERR("Failed to fetch fsp extension");
2800 return -1;
2803 if (n < 3) {
2804 errno = EINVAL;
2805 return -1;
2808 if (offset != 0 && n < 60) {
2809 errno = EINVAL;
2810 return -1;
2813 cmp = memcmp(data, "AFP", 3);
2814 if (cmp != 0) {
2815 errno = EINVAL;
2816 return -1;
2819 if (n <= AFP_OFF_FinderInfo) {
2821 * Nothing to do here really, just return
2823 return n;
2826 offset = 0;
2828 to_copy = n;
2829 if (to_copy > AFP_INFO_SIZE) {
2830 to_copy = AFP_INFO_SIZE;
2832 memcpy(buf, data, to_copy);
2834 to_write = n;
2835 if (to_write != AFP_INFO_SIZE) {
2836 to_write = AFP_INFO_SIZE;
2839 switch (fio->config->meta) {
2840 case FRUIT_META_STREAM:
2841 nwritten = fruit_pwrite_meta_stream(handle,
2842 fsp,
2843 buf,
2844 to_write,
2845 offset);
2846 break;
2848 case FRUIT_META_NETATALK:
2849 nwritten = fruit_pwrite_meta_netatalk(handle,
2850 fsp,
2851 buf,
2852 to_write,
2853 offset);
2854 break;
2856 default:
2857 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2858 return -1;
2861 if (nwritten != to_write) {
2862 return -1;
2866 * Return the requested amount, verified against macOS SMB server
2868 return n;
2871 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2872 files_struct *fsp, const void *data,
2873 size_t n, off_t offset)
2875 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2878 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2879 files_struct *fsp, const void *data,
2880 size_t n, off_t offset)
2882 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2885 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2886 files_struct *fsp, const void *data,
2887 size_t n, off_t offset)
2889 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2890 struct adouble *ad = NULL;
2891 ssize_t nwritten;
2892 int ret;
2894 if (fio == NULL || fio->ad_fsp == NULL) {
2895 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2896 errno = EBADF;
2897 return -1;
2900 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2901 if (ad == NULL) {
2902 DBG_ERR("ad_fget [%s] failed [%s]\n",
2903 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2904 return -1;
2907 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2908 offset + ad_getentryoff(ad, ADEID_RFORK));
2909 if (nwritten != n) {
2910 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2911 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2912 TALLOC_FREE(ad);
2913 return -1;
2916 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2917 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2918 ret = ad_fset(handle, ad, fio->ad_fsp);
2919 if (ret != 0) {
2920 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2921 TALLOC_FREE(ad);
2922 return -1;
2926 TALLOC_FREE(ad);
2927 return n;
2930 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2931 files_struct *fsp, const void *data,
2932 size_t n, off_t offset)
2934 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2935 ssize_t nwritten;
2937 if (fio == NULL) {
2938 DBG_ERR("Failed to fetch fsp extension");
2939 return -1;
2942 switch (fio->config->rsrc) {
2943 case FRUIT_RSRC_STREAM:
2944 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2945 break;
2947 case FRUIT_RSRC_ADFILE:
2948 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2949 break;
2951 case FRUIT_RSRC_XATTR:
2952 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2953 break;
2955 default:
2956 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2957 return -1;
2960 return nwritten;
2963 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2964 files_struct *fsp, const void *data,
2965 size_t n, off_t offset)
2967 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2968 ssize_t nwritten;
2970 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2971 fsp_str_dbg(fsp), (intmax_t)offset, n);
2973 if (fio == NULL) {
2974 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2977 if (fio->type == ADOUBLE_META) {
2978 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2979 } else {
2980 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2983 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2984 return nwritten;
2987 struct fruit_pwrite_state {
2988 ssize_t nwritten;
2989 struct vfs_aio_state vfs_aio_state;
2992 static void fruit_pwrite_done(struct tevent_req *subreq);
2994 static struct tevent_req *fruit_pwrite_send(
2995 struct vfs_handle_struct *handle,
2996 TALLOC_CTX *mem_ctx,
2997 struct tevent_context *ev,
2998 struct files_struct *fsp,
2999 const void *data,
3000 size_t n, off_t offset)
3002 struct tevent_req *req = NULL;
3003 struct tevent_req *subreq = NULL;
3004 struct fruit_pwrite_state *state = NULL;
3005 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3007 req = tevent_req_create(mem_ctx, &state,
3008 struct fruit_pwrite_state);
3009 if (req == NULL) {
3010 return NULL;
3013 if (fruit_must_handle_aio_stream(fio)) {
3014 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3015 if (state->nwritten != n) {
3016 if (state->nwritten != -1) {
3017 errno = EIO;
3019 tevent_req_error(req, errno);
3020 return tevent_req_post(req, ev);
3022 tevent_req_done(req);
3023 return tevent_req_post(req, ev);
3026 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3027 data, n, offset);
3028 if (tevent_req_nomem(req, subreq)) {
3029 return tevent_req_post(req, ev);
3031 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3032 return req;
3035 static void fruit_pwrite_done(struct tevent_req *subreq)
3037 struct tevent_req *req = tevent_req_callback_data(
3038 subreq, struct tevent_req);
3039 struct fruit_pwrite_state *state = tevent_req_data(
3040 req, struct fruit_pwrite_state);
3042 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3043 TALLOC_FREE(subreq);
3045 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3046 return;
3048 tevent_req_done(req);
3051 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3052 struct vfs_aio_state *vfs_aio_state)
3054 struct fruit_pwrite_state *state = tevent_req_data(
3055 req, struct fruit_pwrite_state);
3056 ssize_t retval = -1;
3058 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3059 tevent_req_received(req);
3060 return -1;
3063 *vfs_aio_state = state->vfs_aio_state;
3064 retval = state->nwritten;
3065 tevent_req_received(req);
3066 return retval;
3069 struct fruit_fsync_state {
3070 int ret;
3071 struct vfs_aio_state vfs_aio_state;
3074 static void fruit_fsync_done(struct tevent_req *subreq);
3076 static struct tevent_req *fruit_fsync_send(
3077 struct vfs_handle_struct *handle,
3078 TALLOC_CTX *mem_ctx,
3079 struct tevent_context *ev,
3080 struct files_struct *fsp)
3082 struct tevent_req *req = NULL;
3083 struct tevent_req *subreq = NULL;
3084 struct fruit_fsync_state *state = NULL;
3085 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3087 req = tevent_req_create(mem_ctx, &state,
3088 struct fruit_fsync_state);
3089 if (req == NULL) {
3090 return NULL;
3093 if (fruit_must_handle_aio_stream(fio)) {
3094 struct adouble *ad = NULL;
3096 if (fio->type == ADOUBLE_META) {
3098 * We must never pass a fake_fd
3099 * to lower level fsync calls.
3100 * Everything is already done
3101 * synchronously, so just return
3102 * true.
3104 SMB_ASSERT(fio->fake_fd);
3105 tevent_req_done(req);
3106 return tevent_req_post(req, ev);
3110 * We know the following must be true,
3111 * as it's the condition for fruit_must_handle_aio_stream()
3112 * to return true if fio->type == ADOUBLE_RSRC.
3114 SMB_ASSERT(fio->config->rsrc == FRUIT_RSRC_ADFILE);
3115 if (fio->ad_fsp == NULL) {
3116 tevent_req_error(req, EBADF);
3117 return tevent_req_post(req, ev);
3119 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3120 if (ad == NULL) {
3121 tevent_req_error(req, ENOMEM);
3122 return tevent_req_post(req, ev);
3124 fsp = fio->ad_fsp;
3127 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
3128 if (tevent_req_nomem(req, subreq)) {
3129 return tevent_req_post(req, ev);
3131 tevent_req_set_callback(subreq, fruit_fsync_done, req);
3132 return req;
3135 static void fruit_fsync_done(struct tevent_req *subreq)
3137 struct tevent_req *req = tevent_req_callback_data(
3138 subreq, struct tevent_req);
3139 struct fruit_fsync_state *state = tevent_req_data(
3140 req, struct fruit_fsync_state);
3142 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
3143 TALLOC_FREE(subreq);
3144 if (state->ret != 0) {
3145 tevent_req_error(req, errno);
3146 return;
3148 tevent_req_done(req);
3151 static int fruit_fsync_recv(struct tevent_req *req,
3152 struct vfs_aio_state *vfs_aio_state)
3154 struct fruit_fsync_state *state = tevent_req_data(
3155 req, struct fruit_fsync_state);
3156 int retval = -1;
3158 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3159 tevent_req_received(req);
3160 return -1;
3163 *vfs_aio_state = state->vfs_aio_state;
3164 retval = state->ret;
3165 tevent_req_received(req);
3166 return retval;
3170 * Helper to stat/lstat the base file of an smb_fname.
3172 static int fruit_stat_base(vfs_handle_struct *handle,
3173 struct smb_filename *smb_fname,
3174 bool follow_links)
3176 char *tmp_stream_name;
3177 int rc;
3179 tmp_stream_name = smb_fname->stream_name;
3180 smb_fname->stream_name = NULL;
3181 if (follow_links) {
3182 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3183 } else {
3184 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3186 smb_fname->stream_name = tmp_stream_name;
3188 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3189 smb_fname->base_name,
3190 (uintmax_t)smb_fname->st.st_ex_dev,
3191 (uintmax_t)smb_fname->st.st_ex_ino);
3192 return rc;
3195 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3196 struct smb_filename *smb_fname,
3197 bool follow_links)
3199 int ret;
3200 ino_t ino;
3202 ret = fruit_stat_base(handle, smb_fname, false);
3203 if (ret != 0) {
3204 return -1;
3207 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3209 if (follow_links) {
3210 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3211 } else {
3212 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3215 smb_fname->st.st_ex_ino = ino;
3217 return ret;
3220 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3221 struct smb_filename *smb_fname,
3222 bool follow_links)
3224 struct adouble *ad = NULL;
3226 /* Populate the stat struct with info from the base file. */
3227 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3228 return -1;
3231 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3232 if (ad == NULL) {
3233 DBG_INFO("fruit_stat_meta %s: %s\n",
3234 smb_fname_str_dbg(smb_fname), strerror(errno));
3235 errno = ENOENT;
3236 return -1;
3238 TALLOC_FREE(ad);
3240 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3241 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3242 smb_fname->stream_name);
3243 return 0;
3246 static int fruit_stat_meta(vfs_handle_struct *handle,
3247 struct smb_filename *smb_fname,
3248 bool follow_links)
3250 struct fruit_config_data *config = NULL;
3251 int ret;
3253 SMB_VFS_HANDLE_GET_DATA(handle, config,
3254 struct fruit_config_data, return -1);
3256 switch (config->meta) {
3257 case FRUIT_META_STREAM:
3258 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3259 break;
3261 case FRUIT_META_NETATALK:
3262 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3263 break;
3265 default:
3266 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3267 return -1;
3270 return ret;
3273 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3274 struct smb_filename *smb_fname,
3275 bool follow_links)
3277 struct adouble *ad = NULL;
3278 int ret;
3280 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3281 if (ad == NULL) {
3282 errno = ENOENT;
3283 return -1;
3286 /* Populate the stat struct with info from the base file. */
3287 ret = fruit_stat_base(handle, smb_fname, follow_links);
3288 if (ret != 0) {
3289 TALLOC_FREE(ad);
3290 return -1;
3293 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3294 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3295 smb_fname->stream_name);
3296 TALLOC_FREE(ad);
3297 return 0;
3300 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3301 struct smb_filename *smb_fname,
3302 bool follow_links)
3304 int ret;
3306 if (follow_links) {
3307 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3308 } else {
3309 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3312 return ret;
3315 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3316 struct smb_filename *smb_fname,
3317 bool follow_links)
3319 #ifdef HAVE_ATTROPEN
3320 int ret;
3321 int fd = -1;
3323 /* Populate the stat struct with info from the base file. */
3324 ret = fruit_stat_base(handle, smb_fname, follow_links);
3325 if (ret != 0) {
3326 return -1;
3329 fd = attropen(smb_fname->base_name,
3330 AFPRESOURCE_EA_NETATALK,
3331 O_RDONLY);
3332 if (fd == -1) {
3333 return 0;
3336 ret = sys_fstat(fd, &smb_fname->st, false);
3337 if (ret != 0) {
3338 close(fd);
3339 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3340 AFPRESOURCE_EA_NETATALK);
3341 return -1;
3343 close(fd);
3344 fd = -1;
3346 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3347 smb_fname->stream_name);
3349 return ret;
3351 #else
3352 errno = ENOSYS;
3353 return -1;
3354 #endif
3357 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3358 struct smb_filename *smb_fname,
3359 bool follow_links)
3361 struct fruit_config_data *config = NULL;
3362 int ret;
3364 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3366 SMB_VFS_HANDLE_GET_DATA(handle, config,
3367 struct fruit_config_data, return -1);
3369 switch (config->rsrc) {
3370 case FRUIT_RSRC_STREAM:
3371 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3372 break;
3374 case FRUIT_RSRC_XATTR:
3375 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3376 break;
3378 case FRUIT_RSRC_ADFILE:
3379 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3380 break;
3382 default:
3383 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3384 return -1;
3387 return ret;
3390 static int fruit_stat(vfs_handle_struct *handle,
3391 struct smb_filename *smb_fname)
3393 int rc = -1;
3395 DEBUG(10, ("fruit_stat called for %s\n",
3396 smb_fname_str_dbg(smb_fname)));
3398 if (!is_named_stream(smb_fname)) {
3399 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3400 if (rc == 0) {
3401 update_btime(handle, smb_fname);
3403 return rc;
3407 * Note if lp_posix_paths() is true, we can never
3408 * get here as is_ntfs_stream_smb_fname() is
3409 * always false. So we never need worry about
3410 * not following links here.
3413 if (is_afpinfo_stream(smb_fname->stream_name)) {
3414 rc = fruit_stat_meta(handle, smb_fname, true);
3415 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3416 rc = fruit_stat_rsrc(handle, smb_fname, true);
3417 } else {
3418 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3421 if (rc == 0) {
3422 update_btime(handle, smb_fname);
3423 smb_fname->st.st_ex_mode &= ~S_IFMT;
3424 smb_fname->st.st_ex_mode |= S_IFREG;
3425 smb_fname->st.st_ex_blocks =
3426 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3428 return rc;
3431 static int fruit_lstat(vfs_handle_struct *handle,
3432 struct smb_filename *smb_fname)
3434 int rc = -1;
3436 DEBUG(10, ("fruit_lstat called for %s\n",
3437 smb_fname_str_dbg(smb_fname)));
3439 if (!is_named_stream(smb_fname)) {
3440 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3441 if (rc == 0) {
3442 update_btime(handle, smb_fname);
3444 return rc;
3447 if (is_afpinfo_stream(smb_fname->stream_name)) {
3448 rc = fruit_stat_meta(handle, smb_fname, false);
3449 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3450 rc = fruit_stat_rsrc(handle, smb_fname, false);
3451 } else {
3452 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3455 if (rc == 0) {
3456 update_btime(handle, smb_fname);
3457 smb_fname->st.st_ex_mode &= ~S_IFMT;
3458 smb_fname->st.st_ex_mode |= S_IFREG;
3459 smb_fname->st.st_ex_blocks =
3460 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3462 return rc;
3465 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3466 files_struct *fsp,
3467 SMB_STRUCT_STAT *sbuf)
3469 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3470 struct smb_filename smb_fname;
3471 ino_t ino;
3472 int ret;
3474 if (fio == NULL) {
3475 return -1;
3478 if (fio->fake_fd) {
3479 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3480 if (ret != 0) {
3481 return -1;
3484 *sbuf = fsp->base_fsp->fsp_name->st;
3485 sbuf->st_ex_size = AFP_INFO_SIZE;
3486 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3487 return 0;
3490 smb_fname = (struct smb_filename) {
3491 .base_name = fsp->fsp_name->base_name,
3492 .twrp = fsp->fsp_name->twrp,
3495 ret = fruit_stat_base(handle, &smb_fname, false);
3496 if (ret != 0) {
3497 return -1;
3499 *sbuf = smb_fname.st;
3501 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3503 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3504 if (ret != 0) {
3505 return -1;
3508 sbuf->st_ex_ino = ino;
3509 return 0;
3512 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3513 files_struct *fsp,
3514 SMB_STRUCT_STAT *sbuf)
3516 int ret;
3518 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3519 if (ret != 0) {
3520 return -1;
3523 *sbuf = fsp->base_fsp->fsp_name->st;
3524 sbuf->st_ex_size = AFP_INFO_SIZE;
3525 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3527 return 0;
3530 static int fruit_fstat_meta(vfs_handle_struct *handle,
3531 files_struct *fsp,
3532 SMB_STRUCT_STAT *sbuf,
3533 struct fio *fio)
3535 int ret;
3537 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3539 switch (fio->config->meta) {
3540 case FRUIT_META_STREAM:
3541 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3542 break;
3544 case FRUIT_META_NETATALK:
3545 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3546 break;
3548 default:
3549 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3550 return -1;
3553 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3554 return ret;
3557 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3558 files_struct *fsp,
3559 SMB_STRUCT_STAT *sbuf)
3561 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3564 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3565 files_struct *fsp,
3566 SMB_STRUCT_STAT *sbuf)
3568 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3571 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3572 files_struct *fsp,
3573 SMB_STRUCT_STAT *sbuf)
3575 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3576 struct adouble *ad = NULL;
3577 int ret;
3579 if (fio == NULL || fio->ad_fsp == NULL) {
3580 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3581 errno = EBADF;
3582 return -1;
3585 /* Populate the stat struct with info from the base file. */
3586 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3587 if (ret == -1) {
3588 return -1;
3591 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3592 if (ad == NULL) {
3593 DBG_ERR("ad_fget [%s] failed [%s]\n",
3594 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3595 return -1;
3598 *sbuf = fsp->base_fsp->fsp_name->st;
3599 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3600 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3602 TALLOC_FREE(ad);
3603 return 0;
3606 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3607 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3609 int ret;
3611 switch (fio->config->rsrc) {
3612 case FRUIT_RSRC_STREAM:
3613 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3614 break;
3616 case FRUIT_RSRC_ADFILE:
3617 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3618 break;
3620 case FRUIT_RSRC_XATTR:
3621 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3622 break;
3624 default:
3625 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3626 return -1;
3629 return ret;
3632 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3633 SMB_STRUCT_STAT *sbuf)
3635 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3636 int rc;
3638 if (fio == NULL) {
3639 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3642 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3644 if (fio->type == ADOUBLE_META) {
3645 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3646 } else {
3647 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3650 if (rc == 0) {
3651 sbuf->st_ex_mode &= ~S_IFMT;
3652 sbuf->st_ex_mode |= S_IFREG;
3653 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3656 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3657 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3658 return rc;
3661 static NTSTATUS delete_invalid_meta_stream(
3662 vfs_handle_struct *handle,
3663 const struct smb_filename *smb_fname,
3664 TALLOC_CTX *mem_ctx,
3665 unsigned int *pnum_streams,
3666 struct stream_struct **pstreams,
3667 off_t size)
3669 struct smb_filename *sname = NULL;
3670 NTSTATUS status;
3671 int ret;
3672 bool ok;
3674 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3675 if (!ok) {
3676 return NT_STATUS_INTERNAL_ERROR;
3679 if (size == 0) {
3680 return NT_STATUS_OK;
3683 status = synthetic_pathref(talloc_tos(),
3684 handle->conn->cwd_fsp,
3685 smb_fname->base_name,
3686 AFPINFO_STREAM_NAME,
3687 NULL,
3688 smb_fname->twrp,
3690 &sname);
3691 if (!NT_STATUS_IS_OK(status)) {
3692 return NT_STATUS_NO_MEMORY;
3695 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3696 handle->conn->cwd_fsp,
3697 sname,
3699 if (ret != 0) {
3700 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3701 TALLOC_FREE(sname);
3702 return map_nt_error_from_unix(errno);
3705 TALLOC_FREE(sname);
3706 return NT_STATUS_OK;
3709 static NTSTATUS fruit_streaminfo_meta_stream(
3710 vfs_handle_struct *handle,
3711 struct files_struct *fsp,
3712 const struct smb_filename *smb_fname,
3713 TALLOC_CTX *mem_ctx,
3714 unsigned int *pnum_streams,
3715 struct stream_struct **pstreams)
3717 struct stream_struct *stream = *pstreams;
3718 unsigned int num_streams = *pnum_streams;
3719 int i;
3721 for (i = 0; i < num_streams; i++) {
3722 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3723 break;
3727 if (i == num_streams) {
3728 return NT_STATUS_OK;
3731 if (stream[i].size != AFP_INFO_SIZE) {
3732 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3733 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3735 return delete_invalid_meta_stream(handle,
3736 smb_fname,
3737 mem_ctx,
3738 pnum_streams,
3739 pstreams,
3740 stream[i].size);
3744 return NT_STATUS_OK;
3747 static NTSTATUS fruit_streaminfo_meta_netatalk(
3748 vfs_handle_struct *handle,
3749 struct files_struct *fsp,
3750 const struct smb_filename *smb_fname,
3751 TALLOC_CTX *mem_ctx,
3752 unsigned int *pnum_streams,
3753 struct stream_struct **pstreams)
3755 struct stream_struct *stream = *pstreams;
3756 unsigned int num_streams = *pnum_streams;
3757 struct adouble *ad = NULL;
3758 bool is_fi_empty;
3759 int i;
3760 bool ok;
3762 /* Remove the Netatalk xattr from the list */
3763 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3764 ":" NETATALK_META_XATTR ":$DATA");
3765 if (!ok) {
3766 return NT_STATUS_NO_MEMORY;
3770 * Check if there's a AFPINFO_STREAM from the VFS streams
3771 * backend and if yes, remove it from the list
3773 for (i = 0; i < num_streams; i++) {
3774 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3775 break;
3779 if (i < num_streams) {
3780 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3781 smb_fname_str_dbg(smb_fname));
3783 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3784 AFPINFO_STREAM);
3785 if (!ok) {
3786 return NT_STATUS_INTERNAL_ERROR;
3790 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3791 if (ad == NULL) {
3792 return NT_STATUS_OK;
3795 is_fi_empty = ad_empty_finderinfo(ad);
3796 TALLOC_FREE(ad);
3798 if (is_fi_empty) {
3799 return NT_STATUS_OK;
3802 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3803 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3804 smb_roundup(handle->conn, AFP_INFO_SIZE));
3805 if (!ok) {
3806 return NT_STATUS_NO_MEMORY;
3809 return NT_STATUS_OK;
3812 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3813 struct files_struct *fsp,
3814 const struct smb_filename *smb_fname,
3815 TALLOC_CTX *mem_ctx,
3816 unsigned int *pnum_streams,
3817 struct stream_struct **pstreams)
3819 struct fruit_config_data *config = NULL;
3820 NTSTATUS status;
3822 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3823 return NT_STATUS_INTERNAL_ERROR);
3825 switch (config->meta) {
3826 case FRUIT_META_NETATALK:
3827 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3828 mem_ctx, pnum_streams,
3829 pstreams);
3830 break;
3832 case FRUIT_META_STREAM:
3833 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3834 mem_ctx, pnum_streams,
3835 pstreams);
3836 break;
3838 default:
3839 return NT_STATUS_INTERNAL_ERROR;
3842 return status;
3845 static NTSTATUS fruit_streaminfo_rsrc_stream(
3846 vfs_handle_struct *handle,
3847 struct files_struct *fsp,
3848 const struct smb_filename *smb_fname,
3849 TALLOC_CTX *mem_ctx,
3850 unsigned int *pnum_streams,
3851 struct stream_struct **pstreams)
3853 bool ok;
3855 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3856 if (!ok) {
3857 DBG_ERR("Filtering resource stream failed\n");
3858 return NT_STATUS_INTERNAL_ERROR;
3860 return NT_STATUS_OK;
3863 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3864 vfs_handle_struct *handle,
3865 struct files_struct *fsp,
3866 const struct smb_filename *smb_fname,
3867 TALLOC_CTX *mem_ctx,
3868 unsigned int *pnum_streams,
3869 struct stream_struct **pstreams)
3871 bool ok;
3873 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3874 if (!ok) {
3875 DBG_ERR("Filtering resource stream failed\n");
3876 return NT_STATUS_INTERNAL_ERROR;
3878 return NT_STATUS_OK;
3881 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3882 vfs_handle_struct *handle,
3883 struct files_struct *fsp,
3884 const struct smb_filename *smb_fname,
3885 TALLOC_CTX *mem_ctx,
3886 unsigned int *pnum_streams,
3887 struct stream_struct **pstreams)
3889 struct stream_struct *stream = *pstreams;
3890 unsigned int num_streams = *pnum_streams;
3891 struct adouble *ad = NULL;
3892 bool ok;
3893 size_t rlen;
3894 int i;
3897 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3898 * and if yes, remove it from the list
3900 for (i = 0; i < num_streams; i++) {
3901 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3902 break;
3906 if (i < num_streams) {
3907 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3908 smb_fname_str_dbg(smb_fname));
3910 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3911 AFPRESOURCE_STREAM);
3912 if (!ok) {
3913 return NT_STATUS_INTERNAL_ERROR;
3917 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3918 if (ad == NULL) {
3919 return NT_STATUS_OK;
3922 rlen = ad_getentrylen(ad, ADEID_RFORK);
3923 TALLOC_FREE(ad);
3925 if (rlen == 0) {
3926 return NT_STATUS_OK;
3929 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3930 AFPRESOURCE_STREAM_NAME, rlen,
3931 smb_roundup(handle->conn, rlen));
3932 if (!ok) {
3933 return NT_STATUS_NO_MEMORY;
3936 return NT_STATUS_OK;
3939 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3940 struct files_struct *fsp,
3941 const struct smb_filename *smb_fname,
3942 TALLOC_CTX *mem_ctx,
3943 unsigned int *pnum_streams,
3944 struct stream_struct **pstreams)
3946 struct fruit_config_data *config = NULL;
3947 NTSTATUS status;
3949 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3950 return NT_STATUS_INTERNAL_ERROR);
3952 switch (config->rsrc) {
3953 case FRUIT_RSRC_STREAM:
3954 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3955 mem_ctx, pnum_streams,
3956 pstreams);
3957 break;
3959 case FRUIT_RSRC_XATTR:
3960 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3961 mem_ctx, pnum_streams,
3962 pstreams);
3963 break;
3965 case FRUIT_RSRC_ADFILE:
3966 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3967 mem_ctx, pnum_streams,
3968 pstreams);
3969 break;
3971 default:
3972 return NT_STATUS_INTERNAL_ERROR;
3975 return status;
3978 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3979 struct stream_struct **pstreams)
3981 unsigned num_streams = *pnum_streams;
3982 struct stream_struct *streams = *pstreams;
3983 unsigned i = 0;
3985 if (!global_fruit_config.nego_aapl) {
3986 return;
3989 while (i < num_streams) {
3990 struct smb_filename smb_fname = (struct smb_filename) {
3991 .stream_name = streams[i].name,
3994 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3995 || streams[i].size > 0)
3997 i++;
3998 continue;
4001 streams[i] = streams[num_streams - 1];
4002 num_streams--;
4005 *pnum_streams = num_streams;
4008 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
4009 struct files_struct *fsp,
4010 TALLOC_CTX *mem_ctx,
4011 unsigned int *pnum_streams,
4012 struct stream_struct **pstreams)
4014 struct fruit_config_data *config = NULL;
4015 const struct smb_filename *smb_fname = NULL;
4016 NTSTATUS status;
4018 smb_fname = fsp->fsp_name;
4020 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4021 return NT_STATUS_UNSUCCESSFUL);
4023 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4025 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
4026 pnum_streams, pstreams);
4027 if (!NT_STATUS_IS_OK(status)) {
4028 return status;
4031 fruit_filter_empty_streams(pnum_streams, pstreams);
4033 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4034 mem_ctx, pnum_streams, pstreams);
4035 if (!NT_STATUS_IS_OK(status)) {
4036 return status;
4039 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4040 mem_ctx, pnum_streams, pstreams);
4041 if (!NT_STATUS_IS_OK(status)) {
4042 return status;
4045 return NT_STATUS_OK;
4048 static int fruit_fntimes(vfs_handle_struct *handle,
4049 files_struct *fsp,
4050 struct smb_file_time *ft)
4052 int rc = 0;
4053 struct adouble *ad = NULL;
4054 struct fruit_config_data *config = NULL;
4056 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4057 return -1);
4059 if ((config->meta != FRUIT_META_NETATALK) ||
4060 is_omit_timespec(&ft->create_time))
4062 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4065 DBG_DEBUG("set btime for %s to %s\n", fsp_str_dbg(fsp),
4066 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
4068 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4069 if (ad == NULL) {
4070 goto exit;
4073 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4074 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4076 rc = ad_fset(handle, ad, fsp);
4078 exit:
4080 TALLOC_FREE(ad);
4081 if (rc != 0) {
4082 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
4083 return -1;
4085 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4088 static int fruit_fallocate(struct vfs_handle_struct *handle,
4089 struct files_struct *fsp,
4090 uint32_t mode,
4091 off_t offset,
4092 off_t len)
4094 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4096 if (fio == NULL) {
4097 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4100 /* Let the pwrite code path handle it. */
4101 errno = ENOSYS;
4102 return -1;
4105 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4106 struct files_struct *fsp,
4107 off_t offset)
4109 #ifdef HAVE_ATTROPEN
4110 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4111 #endif
4112 return 0;
4115 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4116 struct files_struct *fsp,
4117 off_t offset)
4119 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4120 int rc;
4121 struct adouble *ad = NULL;
4122 off_t ad_off;
4124 if (fio == NULL || fio->ad_fsp == NULL) {
4125 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4126 errno = EBADF;
4127 return -1;
4130 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4131 if (ad == NULL) {
4132 DBG_ERR("ad_fget [%s] failed [%s]\n",
4133 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4134 return -1;
4137 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4139 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4140 if (rc != 0) {
4141 TALLOC_FREE(ad);
4142 return -1;
4145 ad_setentrylen(ad, ADEID_RFORK, offset);
4147 rc = ad_fset(handle, ad, fio->ad_fsp);
4148 if (rc != 0) {
4149 DBG_ERR("ad_fset [%s] failed [%s]\n",
4150 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4151 TALLOC_FREE(ad);
4152 return -1;
4155 TALLOC_FREE(ad);
4156 return 0;
4159 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4160 struct files_struct *fsp,
4161 off_t offset)
4163 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4166 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4167 struct files_struct *fsp,
4168 off_t offset)
4170 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4171 int ret;
4173 if (fio == NULL) {
4174 DBG_ERR("Failed to fetch fsp extension");
4175 return -1;
4178 switch (fio->config->rsrc) {
4179 case FRUIT_RSRC_XATTR:
4180 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4181 break;
4183 case FRUIT_RSRC_ADFILE:
4184 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4185 break;
4187 case FRUIT_RSRC_STREAM:
4188 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4189 break;
4191 default:
4192 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4193 return -1;
4197 return ret;
4200 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4201 struct files_struct *fsp,
4202 off_t offset)
4204 if (offset > 60) {
4205 DBG_WARNING("ftruncate %s to %jd",
4206 fsp_str_dbg(fsp), (intmax_t)offset);
4207 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4208 errno = EOVERFLOW;
4209 return -1;
4212 /* OS X returns success but does nothing */
4213 DBG_INFO("ignoring ftruncate %s to %jd\n",
4214 fsp_str_dbg(fsp), (intmax_t)offset);
4215 return 0;
4218 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4219 struct files_struct *fsp,
4220 off_t offset)
4222 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4223 int ret;
4225 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4226 (intmax_t)offset);
4228 if (fio == NULL) {
4229 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4232 if (fio->type == ADOUBLE_META) {
4233 ret = fruit_ftruncate_meta(handle, fsp, offset);
4234 } else {
4235 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4238 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4239 return ret;
4242 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4243 struct smb_request *req,
4244 struct smb_filename *smb_fname,
4245 uint32_t access_mask,
4246 uint32_t share_access,
4247 uint32_t create_disposition,
4248 uint32_t create_options,
4249 uint32_t file_attributes,
4250 uint32_t oplock_request,
4251 const struct smb2_lease *lease,
4252 uint64_t allocation_size,
4253 uint32_t private_flags,
4254 struct security_descriptor *sd,
4255 struct ea_list *ea_list,
4256 files_struct **result,
4257 int *pinfo,
4258 const struct smb2_create_blobs *in_context_blobs,
4259 struct smb2_create_blobs *out_context_blobs)
4261 NTSTATUS status;
4262 struct fruit_config_data *config = NULL;
4263 files_struct *fsp = NULL;
4264 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4265 int ret;
4267 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4268 if (!NT_STATUS_IS_OK(status)) {
4269 goto fail;
4272 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4273 return NT_STATUS_UNSUCCESSFUL);
4275 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4276 uint32_t conv_flags = 0;
4278 if (config->wipe_intentionally_left_blank_rfork) {
4279 conv_flags |= AD_CONV_WIPE_BLANK;
4281 if (config->delete_empty_adfiles) {
4282 conv_flags |= AD_CONV_DELETE;
4285 ret = ad_convert(handle,
4286 smb_fname,
4287 macos_string_replace_map,
4288 conv_flags);
4289 if (ret != 0) {
4290 DBG_ERR("ad_convert() failed\n");
4291 return NT_STATUS_UNSUCCESSFUL;
4295 status = SMB_VFS_NEXT_CREATE_FILE(
4296 handle, req, smb_fname,
4297 access_mask, share_access,
4298 create_disposition, create_options,
4299 file_attributes, oplock_request,
4300 lease,
4301 allocation_size, private_flags,
4302 sd, ea_list, result,
4303 pinfo, in_context_blobs, out_context_blobs);
4304 if (!NT_STATUS_IS_OK(status)) {
4305 return status;
4308 fsp = *result;
4310 if (global_fruit_config.nego_aapl) {
4311 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4313 * Enable POSIX directory rename behaviour
4315 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4320 * If this is a plain open for existing files, opening an 0
4321 * byte size resource fork MUST fail with
4322 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4324 * Cf the vfs_fruit torture tests in test_rfork_create().
4326 if (global_fruit_config.nego_aapl &&
4327 create_disposition == FILE_OPEN &&
4328 smb_fname->st.st_ex_size == 0 &&
4329 is_named_stream(smb_fname))
4331 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4332 goto fail;
4335 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4336 return status;
4339 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4340 (fsp->op != NULL) &&
4341 !fsp->fsp_flags.is_pathref)
4343 status = fruit_check_access(
4344 handle, *result,
4345 access_mask,
4346 share_access);
4347 if (!NT_STATUS_IS_OK(status)) {
4348 goto fail;
4352 return status;
4354 fail:
4355 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4357 if (fsp) {
4358 close_file_free(req, &fsp, ERROR_CLOSE);
4359 *result = NULL;
4362 return status;
4365 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4366 struct files_struct *fsp,
4367 TALLOC_CTX *mem_ctx,
4368 struct readdir_attr_data **pattr_data)
4370 struct fruit_config_data *config = NULL;
4371 struct readdir_attr_data *attr_data;
4372 uint32_t conv_flags = 0;
4373 NTSTATUS status;
4374 int ret;
4376 SMB_VFS_HANDLE_GET_DATA(handle, config,
4377 struct fruit_config_data,
4378 return NT_STATUS_UNSUCCESSFUL);
4380 if (!global_fruit_config.nego_aapl) {
4381 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4382 fsp,
4383 mem_ctx,
4384 pattr_data);
4387 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4389 if (config->wipe_intentionally_left_blank_rfork) {
4390 conv_flags |= AD_CONV_WIPE_BLANK;
4392 if (config->delete_empty_adfiles) {
4393 conv_flags |= AD_CONV_DELETE;
4396 ret = ad_convert(handle,
4397 fsp->fsp_name,
4398 macos_string_replace_map,
4399 conv_flags);
4400 if (ret != 0) {
4401 DBG_ERR("ad_convert() failed\n");
4402 return NT_STATUS_UNSUCCESSFUL;
4405 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4406 if (*pattr_data == NULL) {
4407 return NT_STATUS_NO_MEMORY;
4409 attr_data = *pattr_data;
4410 attr_data->type = RDATTR_AAPL;
4413 * Mac metadata: compressed FinderInfo, resource fork length
4414 * and creation date
4416 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4417 if (!NT_STATUS_IS_OK(status)) {
4419 * Error handling is tricky: if we return failure from
4420 * this function, the corresponding directory entry
4421 * will to be passed to the client, so we really just
4422 * want to error out on fatal errors.
4424 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4425 goto fail;
4430 * UNIX mode
4432 if (config->unix_info_enabled) {
4433 attr_data->attr_data.aapl.unix_mode =
4434 fsp->fsp_name->st.st_ex_mode;
4438 * max_access
4440 if (!config->readdir_attr_max_access) {
4441 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4442 } else {
4443 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4444 fsp,
4445 false,
4446 SEC_FLAG_MAXIMUM_ALLOWED,
4447 &attr_data->attr_data.aapl.max_access);
4448 if (!NT_STATUS_IS_OK(status)) {
4449 goto fail;
4453 return NT_STATUS_OK;
4455 fail:
4456 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4457 nt_errstr(status));
4458 TALLOC_FREE(*pattr_data);
4459 return status;
4462 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4463 files_struct *fsp,
4464 uint32_t security_info,
4465 TALLOC_CTX *mem_ctx,
4466 struct security_descriptor **ppdesc)
4468 NTSTATUS status;
4469 struct security_ace ace;
4470 struct dom_sid sid;
4471 struct fruit_config_data *config;
4473 SMB_VFS_HANDLE_GET_DATA(handle, config,
4474 struct fruit_config_data,
4475 return NT_STATUS_UNSUCCESSFUL);
4477 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4478 mem_ctx, ppdesc);
4479 if (!NT_STATUS_IS_OK(status)) {
4480 return status;
4484 * Add MS NFS style ACEs with uid, gid and mode
4486 if (!global_fruit_config.nego_aapl) {
4487 return NT_STATUS_OK;
4489 if (!config->unix_info_enabled) {
4490 return NT_STATUS_OK;
4493 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4494 status = remove_virtual_nfs_aces(*ppdesc);
4495 if (!NT_STATUS_IS_OK(status)) {
4496 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4497 return status;
4500 /* MS NFS style mode */
4501 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4502 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4503 status = security_descriptor_dacl_add(*ppdesc, &ace);
4504 if (!NT_STATUS_IS_OK(status)) {
4505 DEBUG(1,("failed to add MS NFS style ACE\n"));
4506 return status;
4509 /* MS NFS style uid */
4510 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4511 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4512 status = security_descriptor_dacl_add(*ppdesc, &ace);
4513 if (!NT_STATUS_IS_OK(status)) {
4514 DEBUG(1,("failed to add MS NFS style ACE\n"));
4515 return status;
4518 /* MS NFS style gid */
4519 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4520 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4521 status = security_descriptor_dacl_add(*ppdesc, &ace);
4522 if (!NT_STATUS_IS_OK(status)) {
4523 DEBUG(1,("failed to add MS NFS style ACE\n"));
4524 return status;
4527 return NT_STATUS_OK;
4530 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4531 files_struct *fsp,
4532 uint32_t security_info_sent,
4533 const struct security_descriptor *orig_psd)
4535 NTSTATUS status;
4536 bool do_chmod;
4537 mode_t ms_nfs_mode = 0;
4538 int result;
4539 struct security_descriptor *psd = NULL;
4540 uint32_t orig_num_aces = 0;
4542 if (orig_psd->dacl != NULL) {
4543 orig_num_aces = orig_psd->dacl->num_aces;
4546 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4547 if (psd == NULL) {
4548 return NT_STATUS_NO_MEMORY;
4551 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4553 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4554 if (!NT_STATUS_IS_OK(status)) {
4555 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4556 TALLOC_FREE(psd);
4557 return status;
4561 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4562 * sent/present flags correctly now we've removed them.
4565 if (orig_num_aces != 0) {
4567 * Are there any ACE's left ?
4569 if (psd->dacl->num_aces == 0) {
4570 /* No - clear the DACL sent/present flags. */
4571 security_info_sent &= ~SECINFO_DACL;
4572 psd->type &= ~SEC_DESC_DACL_PRESENT;
4576 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4577 if (!NT_STATUS_IS_OK(status)) {
4578 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4579 TALLOC_FREE(psd);
4580 return status;
4583 if (do_chmod) {
4584 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4585 if (result != 0) {
4586 DBG_WARNING("%s, result: %d, %04o error %s\n",
4587 fsp_str_dbg(fsp),
4588 result,
4589 (unsigned)ms_nfs_mode,
4590 strerror(errno));
4591 status = map_nt_error_from_unix(errno);
4592 TALLOC_FREE(psd);
4593 return status;
4597 TALLOC_FREE(psd);
4598 return NT_STATUS_OK;
4601 static struct vfs_offload_ctx *fruit_offload_ctx;
4603 struct fruit_offload_read_state {
4604 struct vfs_handle_struct *handle;
4605 struct tevent_context *ev;
4606 files_struct *fsp;
4607 uint32_t fsctl;
4608 uint32_t flags;
4609 uint64_t xferlen;
4610 DATA_BLOB token;
4613 static void fruit_offload_read_done(struct tevent_req *subreq);
4615 static struct tevent_req *fruit_offload_read_send(
4616 TALLOC_CTX *mem_ctx,
4617 struct tevent_context *ev,
4618 struct vfs_handle_struct *handle,
4619 files_struct *fsp,
4620 uint32_t fsctl,
4621 uint32_t ttl,
4622 off_t offset,
4623 size_t to_copy)
4625 struct tevent_req *req = NULL;
4626 struct tevent_req *subreq = NULL;
4627 struct fruit_offload_read_state *state = NULL;
4629 req = tevent_req_create(mem_ctx, &state,
4630 struct fruit_offload_read_state);
4631 if (req == NULL) {
4632 return NULL;
4634 *state = (struct fruit_offload_read_state) {
4635 .handle = handle,
4636 .ev = ev,
4637 .fsp = fsp,
4638 .fsctl = fsctl,
4641 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4642 fsctl, ttl, offset, to_copy);
4643 if (tevent_req_nomem(subreq, req)) {
4644 return tevent_req_post(req, ev);
4646 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4647 return req;
4650 static void fruit_offload_read_done(struct tevent_req *subreq)
4652 struct tevent_req *req = tevent_req_callback_data(
4653 subreq, struct tevent_req);
4654 struct fruit_offload_read_state *state = tevent_req_data(
4655 req, struct fruit_offload_read_state);
4656 NTSTATUS status;
4658 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4659 state->handle,
4660 state,
4661 &state->flags,
4662 &state->xferlen,
4663 &state->token);
4664 TALLOC_FREE(subreq);
4665 if (tevent_req_nterror(req, status)) {
4666 return;
4669 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4670 tevent_req_done(req);
4671 return;
4674 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4675 &fruit_offload_ctx);
4676 if (tevent_req_nterror(req, status)) {
4677 return;
4680 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4681 state->fsp,
4682 &state->token);
4683 if (tevent_req_nterror(req, status)) {
4684 return;
4687 tevent_req_done(req);
4688 return;
4691 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4692 struct vfs_handle_struct *handle,
4693 TALLOC_CTX *mem_ctx,
4694 uint32_t *flags,
4695 uint64_t *xferlen,
4696 DATA_BLOB *token)
4698 struct fruit_offload_read_state *state = tevent_req_data(
4699 req, struct fruit_offload_read_state);
4700 NTSTATUS status;
4702 if (tevent_req_is_nterror(req, &status)) {
4703 tevent_req_received(req);
4704 return status;
4707 *flags = state->flags;
4708 *xferlen = state->xferlen;
4709 token->length = state->token.length;
4710 token->data = talloc_move(mem_ctx, &state->token.data);
4712 tevent_req_received(req);
4713 return NT_STATUS_OK;
4716 struct fruit_offload_write_state {
4717 struct vfs_handle_struct *handle;
4718 off_t copied;
4719 struct files_struct *src_fsp;
4720 struct files_struct *dst_fsp;
4721 bool is_copyfile;
4724 static void fruit_offload_write_done(struct tevent_req *subreq);
4725 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4726 TALLOC_CTX *mem_ctx,
4727 struct tevent_context *ev,
4728 uint32_t fsctl,
4729 DATA_BLOB *token,
4730 off_t transfer_offset,
4731 struct files_struct *dest_fsp,
4732 off_t dest_off,
4733 off_t num)
4735 struct tevent_req *req, *subreq;
4736 struct fruit_offload_write_state *state;
4737 NTSTATUS status;
4738 struct fruit_config_data *config;
4739 off_t src_off = transfer_offset;
4740 files_struct *src_fsp = NULL;
4741 off_t to_copy = num;
4742 bool copyfile_enabled = false;
4744 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4745 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4747 SMB_VFS_HANDLE_GET_DATA(handle, config,
4748 struct fruit_config_data,
4749 return NULL);
4751 req = tevent_req_create(mem_ctx, &state,
4752 struct fruit_offload_write_state);
4753 if (req == NULL) {
4754 return NULL;
4756 state->handle = handle;
4757 state->dst_fsp = dest_fsp;
4759 switch (fsctl) {
4760 case FSCTL_SRV_COPYCHUNK:
4761 case FSCTL_SRV_COPYCHUNK_WRITE:
4762 copyfile_enabled = config->copyfile_enabled;
4763 break;
4764 default:
4765 break;
4769 * Check if this a OS X copyfile style copychunk request with
4770 * a requested chunk count of 0 that was translated to a
4771 * offload_write_send VFS call overloading the parameters src_off
4772 * = dest_off = num = 0.
4774 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4775 status = vfs_offload_token_db_fetch_fsp(
4776 fruit_offload_ctx, token, &src_fsp);
4777 if (tevent_req_nterror(req, status)) {
4778 return tevent_req_post(req, ev);
4780 state->src_fsp = src_fsp;
4782 status = vfs_stat_fsp(src_fsp);
4783 if (tevent_req_nterror(req, status)) {
4784 return tevent_req_post(req, ev);
4787 to_copy = src_fsp->fsp_name->st.st_ex_size;
4788 state->is_copyfile = true;
4791 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4792 mem_ctx,
4794 fsctl,
4795 token,
4796 transfer_offset,
4797 dest_fsp,
4798 dest_off,
4799 to_copy);
4800 if (tevent_req_nomem(subreq, req)) {
4801 return tevent_req_post(req, ev);
4804 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4805 return req;
4808 static void fruit_offload_write_done(struct tevent_req *subreq)
4810 struct tevent_req *req = tevent_req_callback_data(
4811 subreq, struct tevent_req);
4812 struct fruit_offload_write_state *state = tevent_req_data(
4813 req, struct fruit_offload_write_state);
4814 NTSTATUS status;
4815 unsigned int num_streams = 0;
4816 struct stream_struct *streams = NULL;
4817 unsigned int i;
4818 struct smb_filename *src_fname_tmp = NULL;
4819 struct smb_filename *dst_fname_tmp = NULL;
4821 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4822 subreq,
4823 &state->copied);
4824 TALLOC_FREE(subreq);
4825 if (tevent_req_nterror(req, status)) {
4826 return;
4829 if (!state->is_copyfile) {
4830 tevent_req_done(req);
4831 return;
4835 * Now copy all remaining streams. We know the share supports
4836 * streams, because we're in vfs_fruit. We don't do this async
4837 * because streams are few and small.
4839 status = vfs_fstreaminfo(state->src_fsp,
4840 req, &num_streams, &streams);
4841 if (tevent_req_nterror(req, status)) {
4842 return;
4845 if (num_streams == 1) {
4846 /* There is always one stream, ::$DATA. */
4847 tevent_req_done(req);
4848 return;
4851 for (i = 0; i < num_streams; i++) {
4852 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4853 __func__, streams[i].name, (size_t)streams[i].size));
4855 src_fname_tmp = synthetic_smb_fname(
4856 req,
4857 state->src_fsp->fsp_name->base_name,
4858 streams[i].name,
4859 NULL,
4860 state->src_fsp->fsp_name->twrp,
4861 state->src_fsp->fsp_name->flags);
4862 if (tevent_req_nomem(src_fname_tmp, req)) {
4863 return;
4866 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4867 TALLOC_FREE(src_fname_tmp);
4868 continue;
4871 dst_fname_tmp = synthetic_smb_fname(
4872 req,
4873 state->dst_fsp->fsp_name->base_name,
4874 streams[i].name,
4875 NULL,
4876 state->dst_fsp->fsp_name->twrp,
4877 state->dst_fsp->fsp_name->flags);
4878 if (tevent_req_nomem(dst_fname_tmp, req)) {
4879 TALLOC_FREE(src_fname_tmp);
4880 return;
4883 status = copy_file(req,
4884 state->handle->conn,
4885 src_fname_tmp,
4886 dst_fname_tmp,
4887 OPENX_FILE_CREATE_IF_NOT_EXIST,
4888 0, false);
4889 if (!NT_STATUS_IS_OK(status)) {
4890 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4891 smb_fname_str_dbg(src_fname_tmp),
4892 smb_fname_str_dbg(dst_fname_tmp),
4893 nt_errstr(status)));
4894 TALLOC_FREE(src_fname_tmp);
4895 TALLOC_FREE(dst_fname_tmp);
4896 tevent_req_nterror(req, status);
4897 return;
4900 TALLOC_FREE(src_fname_tmp);
4901 TALLOC_FREE(dst_fname_tmp);
4904 TALLOC_FREE(streams);
4905 TALLOC_FREE(src_fname_tmp);
4906 TALLOC_FREE(dst_fname_tmp);
4907 tevent_req_done(req);
4910 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4911 struct tevent_req *req,
4912 off_t *copied)
4914 struct fruit_offload_write_state *state = tevent_req_data(
4915 req, struct fruit_offload_write_state);
4916 NTSTATUS status;
4918 if (tevent_req_is_nterror(req, &status)) {
4919 DEBUG(1, ("server side copy chunk failed: %s\n",
4920 nt_errstr(status)));
4921 *copied = 0;
4922 tevent_req_received(req);
4923 return status;
4926 *copied = state->copied;
4927 tevent_req_received(req);
4929 return NT_STATUS_OK;
4932 static char *fruit_get_bandsize_line(char **lines, int numlines)
4934 static regex_t re;
4935 static bool re_initialized = false;
4936 int i;
4937 int ret;
4939 if (!re_initialized) {
4940 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4941 if (ret != 0) {
4942 return NULL;
4944 re_initialized = true;
4947 for (i = 0; i < numlines; i++) {
4948 regmatch_t matches[1];
4950 ret = regexec(&re, lines[i], 1, matches, 0);
4951 if (ret == 0) {
4953 * Check if the match was on the last line, sa we want
4954 * the subsequent line.
4956 if (i + 1 == numlines) {
4957 return NULL;
4959 return lines[i + 1];
4961 if (ret != REG_NOMATCH) {
4962 return NULL;
4966 return NULL;
4969 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4971 static regex_t re;
4972 static bool re_initialized = false;
4973 regmatch_t matches[2];
4974 uint64_t band_size;
4975 int ret;
4976 bool ok;
4978 if (!re_initialized) {
4979 ret = regcomp(&re,
4980 "^[[:blank:]]*"
4981 "<integer>\\([[:digit:]]*\\)</integer>$",
4983 if (ret != 0) {
4984 return false;
4986 re_initialized = true;
4989 ret = regexec(&re, line, 2, matches, 0);
4990 if (ret != 0) {
4991 DBG_ERR("regex failed [%s]\n", line);
4992 return false;
4995 line[matches[1].rm_eo] = '\0';
4997 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4998 if (!ok) {
4999 return false;
5001 *_band_size = (size_t)band_size;
5002 return true;
5006 * This reads and parses an Info.plist from a TM sparsebundle looking for the
5007 * "band-size" key and value.
5009 static bool fruit_get_bandsize(vfs_handle_struct *handle,
5010 const char *dir,
5011 size_t *band_size)
5013 #define INFO_PLIST_MAX_SIZE 64*1024
5014 char *plist = NULL;
5015 struct smb_filename *smb_fname = NULL;
5016 files_struct *fsp = NULL;
5017 uint8_t *file_data = NULL;
5018 char **lines = NULL;
5019 char *band_size_line = NULL;
5020 size_t plist_file_size;
5021 ssize_t nread;
5022 int numlines;
5023 int ret;
5024 bool ok = false;
5025 NTSTATUS status;
5027 plist = talloc_asprintf(talloc_tos(),
5028 "%s/%s/Info.plist",
5029 handle->conn->connectpath,
5030 dir);
5031 if (plist == NULL) {
5032 ok = false;
5033 goto out;
5036 smb_fname = synthetic_smb_fname(talloc_tos(),
5037 plist,
5038 NULL,
5039 NULL,
5042 if (smb_fname == NULL) {
5043 ok = false;
5044 goto out;
5047 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5048 if (ret != 0) {
5049 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
5050 ok = true;
5051 goto out;
5054 plist_file_size = smb_fname->st.st_ex_size;
5056 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
5057 DBG_INFO("%s is too large, ignoring\n", plist);
5058 ok = true;
5059 goto out;
5062 status = SMB_VFS_NEXT_CREATE_FILE(
5063 handle, /* conn */
5064 NULL, /* req */
5065 smb_fname, /* fname */
5066 FILE_GENERIC_READ, /* access_mask */
5067 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
5068 FILE_OPEN, /* create_disposition */
5069 0, /* create_options */
5070 0, /* file_attributes */
5071 INTERNAL_OPEN_ONLY, /* oplock_request */
5072 NULL, /* lease */
5073 0, /* allocation_size */
5074 0, /* private_flags */
5075 NULL, /* sd */
5076 NULL, /* ea_list */
5077 &fsp, /* result */
5078 NULL, /* psbuf */
5079 NULL, NULL); /* create context */
5080 if (!NT_STATUS_IS_OK(status)) {
5081 DBG_INFO("Opening [%s] failed [%s]\n",
5082 smb_fname_str_dbg(smb_fname), nt_errstr(status));
5083 ok = false;
5084 goto out;
5087 file_data = talloc_zero_array(talloc_tos(),
5088 uint8_t,
5089 plist_file_size + 1);
5090 if (file_data == NULL) {
5091 ok = false;
5092 goto out;
5095 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
5096 if (nread != plist_file_size) {
5097 DBG_ERR("Short read on [%s]: %zu/%zd\n",
5098 fsp_str_dbg(fsp), nread, plist_file_size);
5099 ok = false;
5100 goto out;
5104 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5105 if (!NT_STATUS_IS_OK(status)) {
5106 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5107 ok = false;
5108 goto out;
5111 lines = file_lines_parse((char *)file_data,
5112 plist_file_size,
5113 &numlines,
5114 talloc_tos());
5115 if (lines == NULL) {
5116 ok = false;
5117 goto out;
5120 band_size_line = fruit_get_bandsize_line(lines, numlines);
5121 if (band_size_line == NULL) {
5122 DBG_ERR("Didn't find band-size key in [%s]\n",
5123 smb_fname_str_dbg(smb_fname));
5124 ok = false;
5125 goto out;
5128 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5129 if (!ok) {
5130 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5131 goto out;
5134 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5136 out:
5137 if (fsp != NULL) {
5138 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5139 if (!NT_STATUS_IS_OK(status)) {
5140 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5143 TALLOC_FREE(plist);
5144 TALLOC_FREE(smb_fname);
5145 TALLOC_FREE(file_data);
5146 TALLOC_FREE(lines);
5147 return ok;
5150 struct fruit_disk_free_state {
5151 off_t total_size;
5154 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5155 const char *bundle,
5156 size_t *_nbands)
5158 char *path = NULL;
5159 struct smb_filename *bands_dir = NULL;
5160 struct smb_Dir *dir_hnd = NULL;
5161 const char *dname = NULL;
5162 char *talloced = NULL;
5163 long offset = 0;
5164 size_t nbands;
5166 path = talloc_asprintf(talloc_tos(),
5167 "%s/%s/bands",
5168 handle->conn->connectpath,
5169 bundle);
5170 if (path == NULL) {
5171 return false;
5174 bands_dir = synthetic_smb_fname(talloc_tos(),
5175 path,
5176 NULL,
5177 NULL,
5180 TALLOC_FREE(path);
5181 if (bands_dir == NULL) {
5182 return false;
5185 dir_hnd = OpenDir(talloc_tos(), handle->conn, bands_dir, NULL, 0);
5186 if (dir_hnd == NULL) {
5187 TALLOC_FREE(bands_dir);
5188 return false;
5191 nbands = 0;
5193 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5194 != NULL)
5196 if (ISDOT(dname) || ISDOTDOT(dname)) {
5197 continue;
5199 nbands++;
5201 TALLOC_FREE(dir_hnd);
5203 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5205 TALLOC_FREE(bands_dir);
5207 *_nbands = nbands;
5208 return true;
5211 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5212 struct fruit_disk_free_state *state,
5213 const char *name)
5215 bool ok;
5216 char *p = NULL;
5217 size_t sparsebundle_strlen = strlen("sparsebundle");
5218 size_t bandsize = 0;
5219 size_t nbands;
5220 off_t tm_size;
5222 p = strstr(name, "sparsebundle");
5223 if (p == NULL) {
5224 return true;
5227 if (p[sparsebundle_strlen] != '\0') {
5228 return true;
5231 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5233 ok = fruit_get_bandsize(handle, name, &bandsize);
5234 if (!ok) {
5236 * Beware of race conditions: this may be an uninitialized
5237 * Info.plist that a client is just creating. We don't want let
5238 * this to trigger complete failure.
5240 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5241 return true;
5244 ok = fruit_get_num_bands(handle, name, &nbands);
5245 if (!ok) {
5247 * Beware of race conditions: this may be a backup sparsebundle
5248 * in an early stage lacking a bands subdirectory. We don't want
5249 * let this to trigger complete failure.
5251 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5252 return true;
5256 * Arithmetic on 32-bit systems may cause overflow, depending on
5257 * size_t precision. First we check its unlikely, then we
5258 * force the precision into target off_t, then we check that
5259 * the total did not overflow either.
5261 if (bandsize > SIZE_MAX/nbands) {
5262 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5263 bandsize, nbands);
5264 return false;
5266 tm_size = (off_t)bandsize * (off_t)nbands;
5268 if (state->total_size + tm_size < state->total_size) {
5269 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5270 bandsize, nbands);
5271 return false;
5274 state->total_size += tm_size;
5276 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5277 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5279 return true;
5283 * Calculate used size of a TimeMachine volume
5285 * This assumes that the volume is used only for TimeMachine.
5287 * - readdir(basedir of share), then
5288 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5289 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5290 * - count band files in "\1.sparsebundle/bands/"
5291 * - calculate used size of all bands: band_count * band_size
5293 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5294 const struct smb_filename *smb_fname,
5295 uint64_t *_bsize,
5296 uint64_t *_dfree,
5297 uint64_t *_dsize)
5299 struct fruit_config_data *config = NULL;
5300 struct fruit_disk_free_state state = {0};
5301 struct smb_Dir *dir_hnd = NULL;
5302 const char *dname = NULL;
5303 char *talloced = NULL;
5304 long offset = 0;
5305 uint64_t dfree;
5306 uint64_t dsize;
5307 bool ok;
5309 SMB_VFS_HANDLE_GET_DATA(handle, config,
5310 struct fruit_config_data,
5311 return UINT64_MAX);
5313 if (!config->time_machine ||
5314 config->time_machine_max_size == 0)
5316 return SMB_VFS_NEXT_DISK_FREE(handle,
5317 smb_fname,
5318 _bsize,
5319 _dfree,
5320 _dsize);
5323 dir_hnd = OpenDir(talloc_tos(), handle->conn, smb_fname, NULL, 0);
5324 if (dir_hnd == NULL) {
5325 return UINT64_MAX;
5328 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5329 != NULL)
5331 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5332 if (!ok) {
5333 TALLOC_FREE(talloced);
5334 TALLOC_FREE(dir_hnd);
5335 return UINT64_MAX;
5337 TALLOC_FREE(talloced);
5340 TALLOC_FREE(dir_hnd);
5342 dsize = config->time_machine_max_size / 512;
5343 dfree = dsize - (state.total_size / 512);
5344 if (dfree > dsize) {
5345 dfree = 0;
5348 *_bsize = 512;
5349 *_dsize = dsize;
5350 *_dfree = dfree;
5351 return dfree / 2;
5354 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5355 const SMB_STRUCT_STAT *psbuf)
5357 struct fruit_config_data *config = NULL;
5359 SMB_VFS_HANDLE_GET_DATA(handle, config,
5360 struct fruit_config_data,
5361 return 0);
5363 if (global_fruit_config.nego_aapl &&
5364 config->aapl_zero_file_id)
5366 return 0;
5369 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5372 static struct vfs_fn_pointers vfs_fruit_fns = {
5373 .connect_fn = fruit_connect,
5374 .disk_free_fn = fruit_disk_free,
5376 /* File operations */
5377 .fchmod_fn = fruit_fchmod,
5378 .unlinkat_fn = fruit_unlinkat,
5379 .renameat_fn = fruit_renameat,
5380 .openat_fn = fruit_openat,
5381 .close_fn = fruit_close,
5382 .pread_fn = fruit_pread,
5383 .pwrite_fn = fruit_pwrite,
5384 .pread_send_fn = fruit_pread_send,
5385 .pread_recv_fn = fruit_pread_recv,
5386 .pwrite_send_fn = fruit_pwrite_send,
5387 .pwrite_recv_fn = fruit_pwrite_recv,
5388 .fsync_send_fn = fruit_fsync_send,
5389 .fsync_recv_fn = fruit_fsync_recv,
5390 .stat_fn = fruit_stat,
5391 .lstat_fn = fruit_lstat,
5392 .fstat_fn = fruit_fstat,
5393 .fstreaminfo_fn = fruit_fstreaminfo,
5394 .fntimes_fn = fruit_fntimes,
5395 .ftruncate_fn = fruit_ftruncate,
5396 .fallocate_fn = fruit_fallocate,
5397 .create_file_fn = fruit_create_file,
5398 .freaddir_attr_fn = fruit_freaddir_attr,
5399 .offload_read_send_fn = fruit_offload_read_send,
5400 .offload_read_recv_fn = fruit_offload_read_recv,
5401 .offload_write_send_fn = fruit_offload_write_send,
5402 .offload_write_recv_fn = fruit_offload_write_recv,
5403 .fs_file_id_fn = fruit_fs_file_id,
5405 /* NT ACL operations */
5406 .fget_nt_acl_fn = fruit_fget_nt_acl,
5407 .fset_nt_acl_fn = fruit_fset_nt_acl,
5410 static_decl_vfs;
5411 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5413 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5414 &vfs_fruit_fns);
5415 if (!NT_STATUS_IS_OK(ret)) {
5416 return ret;
5419 vfs_fruit_debug_level = debug_add_class("fruit");
5420 if (vfs_fruit_debug_level == -1) {
5421 vfs_fruit_debug_level = DBGC_VFS;
5422 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5423 "vfs_fruit_init"));
5424 } else {
5425 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5426 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5429 return ret;