utils: Fix up 14a533680245
[samba4-gss.git] / source3 / modules / vfs_fruit.c
blobf8b12c09594531263d2c4a62a2f2b10033683c6f
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 "lib/util/util_file.h"
34 #include "offload_token.h"
35 #include "string_replace.h"
36 #include "hash_inode.h"
37 #include "lib/adouble.h"
38 #include "lib/util_macstreams.h"
39 #include "source3/smbd/dir.h"
42 * Enhanced OS X and Netatalk compatibility
43 * ========================================
45 * This modules takes advantage of vfs_streams_xattr and
46 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
47 * loaded in the correct order:
49 * vfs modules = catia fruit streams_xattr
51 * The module intercepts the OS X special streams "AFP_AfpInfo" and
52 * "AFP_Resource" and handles them in a special way. All other named
53 * streams are deferred to vfs_streams_xattr.
55 * The OS X client maps all NTFS illegal characters to the Unicode
56 * private range. This module optionally stores the characters using
57 * their native ASCII encoding using vfs_catia. If you're not enabling
58 * this feature, you can skip catia from vfs modules.
60 * Finally, open modes are optionally checked against Netatalk AFP
61 * share modes.
63 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
64 * extended metadata for files and directories. This module optionally
65 * reads and stores this metadata in a way compatible with Netatalk 3
66 * which stores the metadata in an EA "org.netatalk.metadata". Cf
67 * source3/include/MacExtensions.h for a description of the binary
68 * blobs content.
70 * The "AFP_Resource" named stream may be arbitrarily large, thus it
71 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
72 * the only available filesystem where xattrs can be of any size and
73 * the OS supports using the file APIs for xattrs.
75 * The AFP_Resource stream is stored in an AppleDouble file prepending
76 * "._" to the filename. On Solaris with ZFS the stream is optionally
77 * stored in an EA "org.netatalk.resource".
80 * Extended Attributes
81 * ===================
83 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
84 * other protocols you may want to adjust the xattr names the VFS
85 * module vfs_streams_xattr uses for storing ADS's. This defaults to
86 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
87 * these module parameters:
89 * streams_xattr:prefix = user.
90 * streams_xattr:store_stream_type = false
93 * TODO
94 * ====
96 * - log diagnostic if any needed VFS module is not loaded
97 * (eg with lp_vfs_objects())
98 * - add tests
101 static int vfs_fruit_debug_level = DBGC_VFS;
103 static struct global_fruit_config {
104 bool nego_aapl; /* client negotiated AAPL */
106 } global_fruit_config;
108 #undef DBGC_CLASS
109 #define DBGC_CLASS vfs_fruit_debug_level
111 #define FRUIT_PARAM_TYPE_NAME "fruit"
113 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
115 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
116 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
117 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
118 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
120 struct fruit_config_data {
121 enum fruit_rsrc rsrc;
122 enum fruit_meta meta;
123 enum fruit_locking locking;
124 enum fruit_encoding encoding;
125 bool use_aapl; /* config from smb.conf */
126 bool use_copyfile;
127 bool readdir_attr_enabled;
128 bool unix_info_enabled;
129 bool copyfile_enabled;
130 bool veto_appledouble;
131 bool posix_rename;
132 bool aapl_zero_file_id;
133 const char *model;
134 bool time_machine;
135 off_t time_machine_max_size;
136 bool convert_adouble;
137 bool wipe_intentionally_left_blank_rfork;
138 bool delete_empty_adfiles;
139 bool validate_afpinfo;
142 * Additional options, all enabled by default,
143 * possibly useful for analyzing performance. The associated
144 * operations with each of them may be expensive, so having
145 * the chance to disable them individually gives a chance
146 * tweaking the setup for the particular usecase.
148 bool readdir_attr_rsize;
149 bool readdir_attr_finder_info;
150 bool readdir_attr_max_access;
151 /* Recursion guard. Will go away when we have STATX. */
152 bool in_openat_pathref_fsp;
155 static const struct enum_list fruit_rsrc[] = {
156 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
157 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
158 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
159 { -1, NULL}
162 static const struct enum_list fruit_meta[] = {
163 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
164 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
165 { -1, NULL}
168 static const struct enum_list fruit_locking[] = {
169 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
170 {FRUIT_LOCKING_NONE, "none"},
171 { -1, NULL}
174 static const struct enum_list fruit_encoding[] = {
175 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
176 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
177 { -1, NULL}
180 struct fio {
181 vfs_handle_struct *handle;
182 files_struct *fsp; /* backlink to itself */
184 /* tcon config handle */
185 struct fruit_config_data *config;
187 /* Backend fsp for AppleDouble file, can be NULL */
188 files_struct *ad_fsp;
189 /* link from adouble_open_from_base_fsp() to fio */
190 struct fio *real_fio;
192 /* Denote stream type, meta or rsrc */
193 adouble_type_t type;
196 * AFP_AfpInfo stream created, but not written yet, thus still a fake
197 * pipe fd. This is set to true in fruit_open_meta if there was no
198 * existing stream but the caller requested O_CREAT. It is later set to
199 * false when we get a write on the stream that then does open and
200 * create the stream.
202 bool fake_fd;
203 int flags;
204 int mode;
207 /*****************************************************************************
208 * Helper functions
209 *****************************************************************************/
211 static struct adouble *ad_get_meta_fsp(TALLOC_CTX *ctx,
212 vfs_handle_struct *handle,
213 const struct smb_filename *smb_fname)
215 NTSTATUS status;
216 struct adouble *ad = NULL;
217 struct smb_filename *smb_fname_cp = NULL;
218 struct fruit_config_data *config = NULL;
220 if (smb_fname->fsp != NULL) {
221 return ad_get(ctx, handle, smb_fname, ADOUBLE_META);
224 SMB_VFS_HANDLE_GET_DATA(handle,
225 config,
226 struct fruit_config_data,
227 return NULL);
229 if (config->in_openat_pathref_fsp) {
230 return NULL;
233 smb_fname_cp = cp_smb_filename(ctx,
234 smb_fname);
235 if (smb_fname_cp == NULL) {
236 return NULL;
238 TALLOC_FREE(smb_fname_cp->stream_name);
239 config->in_openat_pathref_fsp = true;
240 status = openat_pathref_fsp(handle->conn->cwd_fsp,
241 smb_fname_cp);
242 config->in_openat_pathref_fsp = false;
243 if (!NT_STATUS_IS_OK(status)) {
244 TALLOC_FREE(smb_fname_cp);
245 return NULL;
248 ad = ad_get(ctx, handle, smb_fname_cp, ADOUBLE_META);
249 TALLOC_FREE(smb_fname_cp);
250 return ad;
253 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
254 files_struct *fsp)
256 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
258 if (fio == NULL) {
259 return NULL;
262 if (fio->real_fio != NULL) {
264 * This is an fsp from adouble_open_from_base_fsp()
265 * we should just pass this to the next
266 * module.
268 return NULL;
271 return fio;
275 * Initialize config struct from our smb.conf config parameters
277 static int init_fruit_config(vfs_handle_struct *handle)
279 struct fruit_config_data *config;
280 int enumval = -1;
281 const char *tm_size_str = NULL;
283 config = talloc_zero(handle->conn, struct fruit_config_data);
284 if (!config) {
285 DEBUG(1, ("talloc_zero() failed\n"));
286 errno = ENOMEM;
287 return -1;
290 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
291 "resource", fruit_rsrc, FRUIT_RSRC_ADFILE);
292 if (enumval == -1) {
293 DEBUG(1, ("value for %s: resource type unknown\n",
294 FRUIT_PARAM_TYPE_NAME));
295 return -1;
297 config->rsrc = (enum fruit_rsrc)enumval;
299 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
300 "metadata", fruit_meta, FRUIT_META_NETATALK);
301 if (enumval == -1) {
302 DEBUG(1, ("value for %s: metadata type unknown\n",
303 FRUIT_PARAM_TYPE_NAME));
304 return -1;
306 config->meta = (enum fruit_meta)enumval;
308 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
309 "locking", fruit_locking, FRUIT_LOCKING_NONE);
310 if (enumval == -1) {
311 DEBUG(1, ("value for %s: locking type unknown\n",
312 FRUIT_PARAM_TYPE_NAME));
313 return -1;
315 config->locking = (enum fruit_locking)enumval;
317 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
318 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
319 if (enumval == -1) {
320 DEBUG(1, ("value for %s: encoding type unknown\n",
321 FRUIT_PARAM_TYPE_NAME));
322 return -1;
324 config->encoding = (enum fruit_encoding)enumval;
326 if (config->rsrc == FRUIT_RSRC_ADFILE) {
327 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
328 FRUIT_PARAM_TYPE_NAME,
329 "veto_appledouble",
330 true);
333 config->use_aapl = lp_parm_bool(
334 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
336 config->time_machine = lp_parm_bool(
337 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
339 config->unix_info_enabled = lp_parm_bool(
340 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
342 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
343 "copyfile", false);
345 config->posix_rename = lp_parm_bool(
346 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
348 config->aapl_zero_file_id =
349 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
350 "zero_file_id", true);
352 config->readdir_attr_rsize = lp_parm_bool(
353 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
355 config->readdir_attr_finder_info = lp_parm_bool(
356 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
358 config->readdir_attr_max_access = lp_parm_bool(
359 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
361 config->model = lp_parm_const_string(
362 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
364 tm_size_str = lp_parm_const_string(
365 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
366 "time machine max size", NULL);
367 if (tm_size_str != NULL) {
368 config->time_machine_max_size = conv_str_size(tm_size_str);
371 config->convert_adouble = lp_parm_bool(
372 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
373 "convert_adouble", true);
375 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
376 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
377 "wipe_intentionally_left_blank_rfork", false);
379 config->delete_empty_adfiles = lp_parm_bool(
380 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
381 "delete_empty_adfiles", false);
383 config->validate_afpinfo = lp_parm_bool(
384 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
385 "validate_afpinfo", true);
387 SMB_VFS_HANDLE_SET_DATA(handle, config,
388 NULL, struct fruit_config_data,
389 return -1);
391 return 0;
394 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
395 struct stream_struct **streams,
396 const char *name, off_t size,
397 off_t alloc_size)
399 struct stream_struct *tmp;
401 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
402 (*num_streams)+1);
403 if (tmp == NULL) {
404 return false;
407 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
408 if (tmp[*num_streams].name == NULL) {
409 return false;
412 tmp[*num_streams].size = size;
413 tmp[*num_streams].alloc_size = alloc_size;
415 *streams = tmp;
416 *num_streams += 1;
417 return true;
420 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
421 struct stream_struct **streams)
423 struct stream_struct *tmp = *streams;
424 unsigned int i;
426 if (*num_streams == 0) {
427 return true;
430 for (i = 0; i < *num_streams; i++) {
431 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
432 break;
436 if (i == *num_streams) {
437 return true;
440 if (tmp[i].size > 0) {
441 return true;
444 TALLOC_FREE(tmp[i].name);
445 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
446 *num_streams -= 1;
447 return true;
450 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
451 struct stream_struct **streams,
452 const char *name)
454 struct stream_struct *tmp = *streams;
455 unsigned int i;
457 if (*num_streams == 0) {
458 return true;
461 for (i = 0; i < *num_streams; i++) {
462 if (strequal_m(tmp[i].name, name)) {
463 break;
467 if (i == *num_streams) {
468 return true;
471 TALLOC_FREE(tmp[i].name);
472 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
473 *num_streams -= 1;
474 return true;
477 static bool ad_empty_finderinfo(const struct adouble *ad)
479 int cmp;
480 char emptybuf[ADEDLEN_FINDERI] = {0};
481 char *fi = NULL;
483 fi = ad_get_entry(ad, ADEID_FINDERI);
484 if (fi == NULL) {
485 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
486 return false;
489 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
490 return (cmp == 0);
493 static bool ai_empty_finderinfo(const AfpInfo *ai)
495 int cmp;
496 char emptybuf[ADEDLEN_FINDERI] = {0};
498 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
499 return (cmp == 0);
503 * Update btime with btime from Netatalk
505 static void update_btime(vfs_handle_struct *handle,
506 struct smb_filename *smb_fname)
508 uint32_t t;
509 struct timespec creation_time = {0};
510 struct adouble *ad;
511 struct fruit_config_data *config = NULL;
513 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
514 return);
516 switch (config->meta) {
517 case FRUIT_META_STREAM:
518 return;
519 case FRUIT_META_NETATALK:
520 /* Handled below */
521 break;
522 default:
523 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
524 return;
527 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
528 if (ad == NULL) {
529 return;
531 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
532 TALLOC_FREE(ad);
533 return;
535 TALLOC_FREE(ad);
537 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
538 update_stat_ex_create_time(&smb_fname->st, creation_time);
540 return;
544 * Map an access mask to a Netatalk single byte byte range lock
546 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
547 uint32_t access_mask)
549 off_t offset;
551 switch (access_mask) {
552 case FILE_READ_DATA:
553 offset = AD_FILELOCK_OPEN_RD;
554 break;
556 case FILE_WRITE_DATA:
557 case FILE_APPEND_DATA:
558 offset = AD_FILELOCK_OPEN_WR;
559 break;
561 default:
562 offset = AD_FILELOCK_OPEN_NONE;
563 break;
566 if (fork_type == APPLE_FORK_RSRC) {
567 if (offset == AD_FILELOCK_OPEN_NONE) {
568 offset = AD_FILELOCK_RSRC_OPEN_NONE;
569 } else {
570 offset += 2;
574 return offset;
578 * Map a deny mode to a Netatalk brl
580 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
581 uint32_t deny_mode)
583 off_t offset = 0;
585 switch (deny_mode) {
586 case DENY_READ:
587 offset = AD_FILELOCK_DENY_RD;
588 break;
590 case DENY_WRITE:
591 offset = AD_FILELOCK_DENY_WR;
592 break;
594 default:
595 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
598 if (fork_type == APPLE_FORK_RSRC) {
599 offset += 2;
602 return offset;
606 * Call fcntl() with an exclusive F_GETLK request in order to
607 * determine if there's an existing shared lock
609 * @return true if the requested lock was found or any error occurred
610 * false if the lock was not found
612 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
614 bool result;
615 off_t offset = in_offset;
616 off_t len = 1;
617 int type = F_WRLCK;
618 pid_t pid = 0;
620 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
621 if (result == false) {
622 return true;
625 if (type != F_UNLCK) {
626 return true;
629 return false;
632 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
633 files_struct *fsp,
634 uint32_t access_mask,
635 uint32_t share_mode)
637 NTSTATUS status = NT_STATUS_OK;
638 off_t off;
639 bool share_for_read = (share_mode & FILE_SHARE_READ);
640 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
641 bool netatalk_already_open_for_reading = false;
642 bool netatalk_already_open_for_writing = false;
643 bool netatalk_already_open_with_deny_read = false;
644 bool netatalk_already_open_with_deny_write = false;
645 struct GUID req_guid = GUID_random();
647 /* FIXME: hardcoded data fork, add resource fork */
648 enum apple_fork fork_type = APPLE_FORK_DATA;
650 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
651 fsp_str_dbg(fsp),
652 access_mask & FILE_READ_DATA ? "READ" :"-",
653 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
654 share_mode);
656 if (fsp_get_io_fd(fsp) == -1) {
657 return NT_STATUS_OK;
660 /* Read NetATalk opens and deny modes on the file. */
661 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
662 access_to_netatalk_brl(fork_type,
663 FILE_READ_DATA));
665 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
666 denymode_to_netatalk_brl(fork_type,
667 DENY_READ));
669 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
670 access_to_netatalk_brl(fork_type,
671 FILE_WRITE_DATA));
673 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
674 denymode_to_netatalk_brl(fork_type,
675 DENY_WRITE));
677 /* If there are any conflicts - sharing violation. */
678 if ((access_mask & FILE_READ_DATA) &&
679 netatalk_already_open_with_deny_read) {
680 return NT_STATUS_SHARING_VIOLATION;
683 if (!share_for_read &&
684 netatalk_already_open_for_reading) {
685 return NT_STATUS_SHARING_VIOLATION;
688 if ((access_mask & FILE_WRITE_DATA) &&
689 netatalk_already_open_with_deny_write) {
690 return NT_STATUS_SHARING_VIOLATION;
693 if (!share_for_write &&
694 netatalk_already_open_for_writing) {
695 return NT_STATUS_SHARING_VIOLATION;
698 if (!(access_mask & FILE_READ_DATA)) {
700 * Nothing we can do here, we need read access
701 * to set locks.
703 return NT_STATUS_OK;
706 /* Set NetAtalk locks matching our access */
707 if (access_mask & FILE_READ_DATA) {
708 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
709 req_guid.time_hi_and_version = __LINE__;
710 status = do_lock(
711 fsp,
712 talloc_tos(),
713 &req_guid,
714 fsp->op->global->open_persistent_id,
716 off,
717 READ_LOCK,
718 POSIX_LOCK,
719 NULL,
720 NULL);
722 if (!NT_STATUS_IS_OK(status)) {
723 return status;
727 if (!share_for_read) {
728 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
729 req_guid.time_hi_and_version = __LINE__;
730 status = do_lock(
731 fsp,
732 talloc_tos(),
733 &req_guid,
734 fsp->op->global->open_persistent_id,
736 off,
737 READ_LOCK,
738 POSIX_LOCK,
739 NULL,
740 NULL);
742 if (!NT_STATUS_IS_OK(status)) {
743 return status;
747 if (access_mask & FILE_WRITE_DATA) {
748 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
749 req_guid.time_hi_and_version = __LINE__;
750 status = do_lock(
751 fsp,
752 talloc_tos(),
753 &req_guid,
754 fsp->op->global->open_persistent_id,
756 off,
757 READ_LOCK,
758 POSIX_LOCK,
759 NULL,
760 NULL);
762 if (!NT_STATUS_IS_OK(status)) {
763 return status;
767 if (!share_for_write) {
768 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
769 req_guid.time_hi_and_version = __LINE__;
770 status = do_lock(
771 fsp,
772 talloc_tos(),
773 &req_guid,
774 fsp->op->global->open_persistent_id,
776 off,
777 READ_LOCK,
778 POSIX_LOCK,
779 NULL,
780 NULL);
782 if (!NT_STATUS_IS_OK(status)) {
783 return status;
787 return NT_STATUS_OK;
790 static NTSTATUS check_aapl(vfs_handle_struct *handle,
791 struct smb_request *req,
792 const struct smb2_create_blobs *in_context_blobs,
793 struct smb2_create_blobs *out_context_blobs)
795 struct fruit_config_data *config;
796 NTSTATUS status;
797 struct smb2_create_blob *aapl = NULL;
798 uint32_t cmd;
799 bool ok;
800 uint8_t p[16];
801 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
802 uint64_t req_bitmap, client_caps;
803 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
804 smb_ucs2_t *model;
805 size_t modellen;
807 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
808 return NT_STATUS_UNSUCCESSFUL);
810 if (!config->use_aapl
811 || in_context_blobs == NULL
812 || out_context_blobs == NULL) {
813 return NT_STATUS_OK;
816 aapl = smb2_create_blob_find(in_context_blobs,
817 SMB2_CREATE_TAG_AAPL);
818 if (aapl == NULL) {
819 return NT_STATUS_OK;
822 if (aapl->data.length != 24) {
823 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
824 (uintmax_t)aapl->data.length));
825 return NT_STATUS_INVALID_PARAMETER;
828 cmd = IVAL(aapl->data.data, 0);
829 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
830 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
831 return NT_STATUS_INVALID_PARAMETER;
834 req_bitmap = BVAL(aapl->data.data, 8);
835 client_caps = BVAL(aapl->data.data, 16);
837 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
838 SIVAL(p, 4, 0);
839 SBVAL(p, 8, req_bitmap);
840 ok = data_blob_append(req, &blob, p, 16);
841 if (!ok) {
842 return NT_STATUS_UNSUCCESSFUL;
845 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
846 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
847 (handle->conn->fs_capabilities & FILE_NAMED_STREAMS)) {
848 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
849 config->readdir_attr_enabled = true;
852 if (config->use_copyfile) {
853 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
854 config->copyfile_enabled = true;
858 * The client doesn't set the flag, so we can't check
859 * for it and just set it unconditionally
861 if (config->unix_info_enabled) {
862 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
865 SBVAL(p, 0, server_caps);
866 ok = data_blob_append(req, &blob, p, 8);
867 if (!ok) {
868 return NT_STATUS_UNSUCCESSFUL;
872 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
873 int val = lp_case_sensitive(SNUM(handle->conn));
874 uint64_t caps = 0;
876 switch (val) {
877 case Auto:
878 break;
880 case True:
881 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
882 break;
884 default:
885 break;
888 if (config->time_machine) {
889 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
892 SBVAL(p, 0, caps);
894 ok = data_blob_append(req, &blob, p, 8);
895 if (!ok) {
896 return NT_STATUS_UNSUCCESSFUL;
900 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
901 ok = convert_string_talloc(req,
902 CH_UNIX, CH_UTF16LE,
903 config->model, strlen(config->model),
904 &model, &modellen);
905 if (!ok) {
906 return NT_STATUS_UNSUCCESSFUL;
909 SIVAL(p, 0, 0);
910 SIVAL(p + 4, 0, modellen);
911 ok = data_blob_append(req, &blob, p, 8);
912 if (!ok) {
913 talloc_free(model);
914 return NT_STATUS_UNSUCCESSFUL;
917 ok = data_blob_append(req, &blob, model, modellen);
918 talloc_free(model);
919 if (!ok) {
920 return NT_STATUS_UNSUCCESSFUL;
924 status = smb2_create_blob_add(out_context_blobs,
925 out_context_blobs,
926 SMB2_CREATE_TAG_AAPL,
927 blob);
928 if (NT_STATUS_IS_OK(status)) {
929 global_fruit_config.nego_aapl = true;
932 return status;
935 static bool readdir_attr_meta_finderi_stream(
936 struct vfs_handle_struct *handle,
937 const struct smb_filename *smb_fname,
938 AfpInfo *ai)
940 struct smb_filename *stream_name = NULL;
941 files_struct *fsp = NULL;
942 ssize_t nread;
943 NTSTATUS status;
944 bool ok;
945 uint8_t buf[AFP_INFO_SIZE];
947 status = synthetic_pathref(talloc_tos(),
948 handle->conn->cwd_fsp,
949 smb_fname->base_name,
950 AFPINFO_STREAM_NAME,
951 NULL,
952 smb_fname->twrp,
953 smb_fname->flags,
954 &stream_name);
955 if (!NT_STATUS_IS_OK(status)) {
956 return false;
959 status = SMB_VFS_CREATE_FILE(
960 handle->conn, /* conn */
961 NULL, /* req */
962 NULL, /* dirfsp */
963 stream_name, /* fname */
964 FILE_READ_DATA, /* access_mask */
965 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
966 FILE_SHARE_DELETE),
967 FILE_OPEN, /* create_disposition*/
968 0, /* create_options */
969 0, /* file_attributes */
970 INTERNAL_OPEN_ONLY, /* oplock_request */
971 NULL, /* lease */
972 0, /* allocation_size */
973 0, /* private_flags */
974 NULL, /* sd */
975 NULL, /* ea_list */
976 &fsp, /* result */
977 NULL, /* pinfo */
978 NULL, NULL); /* create context */
980 TALLOC_FREE(stream_name);
982 if (!NT_STATUS_IS_OK(status)) {
983 return false;
986 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
987 if (nread != AFP_INFO_SIZE) {
988 DBG_ERR("short read [%s] [%zd/%d]\n",
989 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
990 ok = false;
991 goto fail;
994 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
995 AFP_FinderSize);
997 ok = true;
999 fail:
1000 if (fsp != NULL) {
1001 close_file_free(NULL, &fsp, NORMAL_CLOSE);
1004 return ok;
1007 static bool readdir_attr_meta_finderi_netatalk(
1008 struct vfs_handle_struct *handle,
1009 const struct smb_filename *smb_fname,
1010 AfpInfo *ai)
1012 struct adouble *ad = NULL;
1013 char *p = NULL;
1015 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
1016 if (ad == NULL) {
1017 return false;
1020 p = ad_get_entry(ad, ADEID_FINDERI);
1021 if (p == NULL) {
1022 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
1023 TALLOC_FREE(ad);
1024 return false;
1027 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1028 TALLOC_FREE(ad);
1029 return true;
1032 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1033 const struct smb_filename *smb_fname,
1034 struct readdir_attr_data *attr_data)
1036 struct fruit_config_data *config = NULL;
1037 uint32_t date_added;
1038 AfpInfo ai = {0};
1039 bool ok;
1041 SMB_VFS_HANDLE_GET_DATA(handle, config,
1042 struct fruit_config_data,
1043 return false);
1045 switch (config->meta) {
1046 case FRUIT_META_NETATALK:
1047 ok = readdir_attr_meta_finderi_netatalk(
1048 handle, smb_fname, &ai);
1049 break;
1051 case FRUIT_META_STREAM:
1052 ok = readdir_attr_meta_finderi_stream(
1053 handle, smb_fname, &ai);
1054 break;
1056 default:
1057 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1058 return false;
1061 if (!ok) {
1062 /* Don't bother with errors, it's likely ENOENT */
1063 return true;
1066 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1067 /* finder_type */
1068 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1069 &ai.afpi_FinderInfo[0], 4);
1071 /* finder_creator */
1072 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1073 &ai.afpi_FinderInfo[4], 4);
1076 /* finder_flags */
1077 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1078 &ai.afpi_FinderInfo[8], 2);
1080 /* finder_ext_flags */
1081 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1082 &ai.afpi_FinderInfo[24], 2);
1084 /* creation date */
1085 date_added = convert_time_t_to_uint32_t(
1086 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1088 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1090 return true;
1093 static uint64_t readdir_attr_rfork_size_adouble(
1094 struct vfs_handle_struct *handle,
1095 const struct smb_filename *smb_fname)
1097 struct adouble *ad = NULL;
1098 uint64_t rfork_size;
1100 ad = ad_get(talloc_tos(), handle, smb_fname,
1101 ADOUBLE_RSRC);
1102 if (ad == NULL) {
1103 return 0;
1106 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1107 TALLOC_FREE(ad);
1109 return rfork_size;
1112 static uint64_t readdir_attr_rfork_size_stream(
1113 struct vfs_handle_struct *handle,
1114 const struct smb_filename *smb_fname)
1116 struct smb_filename *stream_name = NULL;
1117 int ret;
1118 uint64_t rfork_size;
1120 stream_name = synthetic_smb_fname(talloc_tos(),
1121 smb_fname->base_name,
1122 AFPRESOURCE_STREAM_NAME,
1123 NULL,
1124 smb_fname->twrp,
1126 if (stream_name == NULL) {
1127 return 0;
1130 ret = SMB_VFS_STAT(handle->conn, stream_name);
1131 if (ret != 0) {
1132 TALLOC_FREE(stream_name);
1133 return 0;
1136 rfork_size = stream_name->st.st_ex_size;
1137 TALLOC_FREE(stream_name);
1139 return rfork_size;
1142 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1143 const struct smb_filename *smb_fname)
1145 struct fruit_config_data *config = NULL;
1146 uint64_t rfork_size;
1148 SMB_VFS_HANDLE_GET_DATA(handle, config,
1149 struct fruit_config_data,
1150 return 0);
1152 switch (config->rsrc) {
1153 case FRUIT_RSRC_ADFILE:
1154 rfork_size = readdir_attr_rfork_size_adouble(handle,
1155 smb_fname);
1156 break;
1158 case FRUIT_RSRC_XATTR:
1159 case FRUIT_RSRC_STREAM:
1160 rfork_size = readdir_attr_rfork_size_stream(handle,
1161 smb_fname);
1162 break;
1164 default:
1165 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1166 rfork_size = 0;
1167 break;
1170 return rfork_size;
1173 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1174 const struct smb_filename *smb_fname,
1175 struct readdir_attr_data *attr_data)
1177 NTSTATUS status = NT_STATUS_OK;
1178 struct fruit_config_data *config = NULL;
1179 bool ok;
1181 SMB_VFS_HANDLE_GET_DATA(handle, config,
1182 struct fruit_config_data,
1183 return NT_STATUS_UNSUCCESSFUL);
1186 /* Ensure we return a default value in the creation_date field */
1187 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1190 * Resource fork length
1193 if (config->readdir_attr_rsize) {
1194 uint64_t rfork_size;
1196 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1197 attr_data->attr_data.aapl.rfork_size = rfork_size;
1201 * FinderInfo
1204 if (config->readdir_attr_finder_info) {
1205 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1206 if (!ok) {
1207 status = NT_STATUS_INTERNAL_ERROR;
1211 return status;
1214 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1216 NTSTATUS status;
1217 uint32_t i;
1219 if (psd->dacl == NULL) {
1220 return NT_STATUS_OK;
1223 for (i = 0; i < psd->dacl->num_aces; i++) {
1224 /* MS NFS style mode/uid/gid */
1225 int cmp = dom_sid_compare_domain(
1226 &global_sid_Unix_NFS,
1227 &psd->dacl->aces[i].trustee);
1228 if (cmp != 0) {
1229 /* Normal ACE entry. */
1230 continue;
1234 * security_descriptor_dacl_del()
1235 * *must* return NT_STATUS_OK as we know
1236 * we have something to remove.
1239 status = security_descriptor_dacl_del(psd,
1240 &psd->dacl->aces[i].trustee);
1241 if (!NT_STATUS_IS_OK(status)) {
1242 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1243 nt_errstr(status));
1244 return status;
1248 * security_descriptor_dacl_del() may delete more
1249 * then one entry subsequent to this one if the
1250 * SID matches, but we only need to ensure that
1251 * we stay looking at the same element in the array.
1253 i--;
1255 return NT_STATUS_OK;
1258 /* Search MS NFS style ACE with UNIX mode */
1259 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1260 files_struct *fsp,
1261 struct security_descriptor *psd,
1262 mode_t *pmode,
1263 bool *pdo_chmod)
1265 uint32_t i;
1266 struct fruit_config_data *config = NULL;
1268 *pdo_chmod = false;
1270 SMB_VFS_HANDLE_GET_DATA(handle, config,
1271 struct fruit_config_data,
1272 return NT_STATUS_UNSUCCESSFUL);
1274 if (!global_fruit_config.nego_aapl) {
1275 return NT_STATUS_OK;
1277 if (psd->dacl == NULL || !config->unix_info_enabled) {
1278 return NT_STATUS_OK;
1281 for (i = 0; i < psd->dacl->num_aces; i++) {
1282 if (dom_sid_compare_domain(
1283 &global_sid_Unix_NFS_Mode,
1284 &psd->dacl->aces[i].trustee) == 0) {
1285 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1286 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1287 *pdo_chmod = true;
1289 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1290 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1291 break;
1296 * Remove any incoming virtual ACE entries generated by
1297 * fruit_fget_nt_acl().
1300 return remove_virtual_nfs_aces(psd);
1303 /****************************************************************************
1304 * VFS ops
1305 ****************************************************************************/
1307 static int fruit_connect(vfs_handle_struct *handle,
1308 const char *service,
1309 const char *user)
1311 int rc;
1312 char *list = NULL, *newlist = NULL;
1313 struct fruit_config_data *config;
1314 const struct loadparm_substitution *lp_sub =
1315 loadparm_s3_global_substitution();
1317 DEBUG(10, ("fruit_connect\n"));
1319 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1320 if (rc < 0) {
1321 return rc;
1324 rc = init_fruit_config(handle);
1325 if (rc != 0) {
1326 return rc;
1329 SMB_VFS_HANDLE_GET_DATA(handle, config,
1330 struct fruit_config_data, return -1);
1332 if (config->veto_appledouble) {
1333 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1335 if (list) {
1336 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1337 newlist = talloc_asprintf(
1338 list,
1339 "%s/" ADOUBLE_NAME_PREFIX "*/",
1340 list);
1341 lp_do_parameter(SNUM(handle->conn),
1342 "veto files",
1343 newlist);
1345 } else {
1346 lp_do_parameter(SNUM(handle->conn),
1347 "veto files",
1348 "/" ADOUBLE_NAME_PREFIX "*/");
1351 TALLOC_FREE(list);
1354 if (config->encoding == FRUIT_ENC_NATIVE) {
1355 lp_do_parameter(SNUM(handle->conn),
1356 "catia:mappings",
1357 macos_string_replace_map);
1360 if (config->time_machine) {
1361 DBG_NOTICE("Enabling durable handles for Time Machine "
1362 "support on [%s]\n", service);
1363 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1364 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1365 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1366 if (!lp_strict_sync(SNUM(handle->conn))) {
1367 DBG_WARNING("Time Machine without strict sync is not "
1368 "recommended!\n");
1370 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1373 return rc;
1376 static void fio_ref_destroy_fn(void *p_data)
1378 struct fio *ref_fio = (struct fio *)p_data;
1379 if (ref_fio->real_fio != NULL) {
1380 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1381 ref_fio->real_fio->ad_fsp = NULL;
1382 ref_fio->real_fio = NULL;
1386 static void fio_close_ad_fsp(struct fio *fio)
1388 if (fio->ad_fsp != NULL) {
1389 fd_close(fio->ad_fsp);
1390 file_free(NULL, fio->ad_fsp);
1391 /* fio_ref_destroy_fn() should have cleared this */
1392 SMB_ASSERT(fio->ad_fsp == NULL);
1396 static void fio_destroy_fn(void *p_data)
1398 struct fio *fio = (struct fio *)p_data;
1399 fio_close_ad_fsp(fio);
1402 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1403 const struct files_struct *dirfsp,
1404 const struct smb_filename *smb_fname,
1405 files_struct *fsp,
1406 int flags,
1407 mode_t mode)
1409 struct fruit_config_data *config = NULL;
1410 struct fio *fio = NULL;
1411 struct vfs_open_how how = {
1412 .flags = flags & ~O_CREAT,
1413 .mode = mode,
1415 int fd;
1417 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1419 SMB_VFS_HANDLE_GET_DATA(handle, config,
1420 struct fruit_config_data, return -1);
1422 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1423 fio->handle = handle;
1424 fio->fsp = fsp;
1425 fio->type = ADOUBLE_META;
1426 fio->config = config;
1428 fd = SMB_VFS_NEXT_OPENAT(handle,
1429 dirfsp,
1430 smb_fname,
1431 fsp,
1432 &how);
1433 if (fd != -1) {
1434 return fd;
1437 if (!(flags & O_CREAT)) {
1438 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1439 return -1;
1442 fd = vfs_fake_fd();
1443 if (fd == -1) {
1444 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1445 return -1;
1448 fio->fake_fd = true;
1449 fio->flags = flags;
1450 fio->mode = mode;
1452 return fd;
1455 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1456 const struct files_struct *dirfsp,
1457 const struct smb_filename *smb_fname,
1458 files_struct *fsp,
1459 int flags,
1460 mode_t mode)
1462 struct fruit_config_data *config = NULL;
1463 struct fio *fio = NULL;
1464 struct adouble *ad = NULL;
1465 bool meta_exists = false;
1466 int fd;
1468 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1471 * We know this is a stream open, so fsp->base_fsp must
1472 * already be open.
1474 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1475 SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1477 ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1478 if (ad != NULL) {
1479 meta_exists = true;
1482 TALLOC_FREE(ad);
1484 if (!meta_exists && !(flags & O_CREAT)) {
1485 errno = ENOENT;
1486 return -1;
1489 fd = vfs_fake_fd();
1490 if (fd == -1) {
1491 return -1;
1494 SMB_VFS_HANDLE_GET_DATA(handle, config,
1495 struct fruit_config_data, return -1);
1497 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1498 fio->handle = handle;
1499 fio->fsp = fsp;
1500 fio->type = ADOUBLE_META;
1501 fio->config = config;
1502 fio->fake_fd = true;
1503 fio->flags = flags;
1504 fio->mode = mode;
1506 return fd;
1509 static int fruit_open_meta(vfs_handle_struct *handle,
1510 const struct files_struct *dirfsp,
1511 const struct smb_filename *smb_fname,
1512 files_struct *fsp, int flags, mode_t mode)
1514 int fd;
1515 struct fruit_config_data *config = NULL;
1517 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1519 SMB_VFS_HANDLE_GET_DATA(handle, config,
1520 struct fruit_config_data, return -1);
1522 switch (config->meta) {
1523 case FRUIT_META_STREAM:
1524 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1525 fsp, flags, mode);
1526 break;
1528 case FRUIT_META_NETATALK:
1529 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1530 fsp, flags, mode);
1531 break;
1533 default:
1534 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1535 return -1;
1538 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1540 return fd;
1543 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1544 const struct files_struct *dirfsp,
1545 const struct smb_filename *smb_fname,
1546 files_struct *fsp,
1547 int flags,
1548 mode_t mode)
1550 int rc = 0;
1551 struct fruit_config_data *config = NULL;
1552 struct files_struct *ad_fsp = NULL;
1553 struct fio *fio = NULL;
1554 struct fio *ref_fio = NULL;
1555 NTSTATUS status;
1556 int fd = -1;
1558 SMB_VFS_HANDLE_GET_DATA(handle, config,
1559 struct fruit_config_data, return -1);
1561 if ((!(flags & O_CREAT)) &&
1562 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1564 /* sorry, but directories don't have a resource fork */
1565 errno = ENOENT;
1566 rc = -1;
1567 goto exit;
1571 * We return a fake_fd to the vfs modules above,
1572 * while we open an internal backend fsp for the
1573 * '._' file for the next vfs modules.
1575 * Note that adouble_open_from_base_fsp() recurses
1576 * into fruit_openat(), but it'll just pass to
1577 * the next module as just opens a flat file on
1578 * disk.
1581 fd = vfs_fake_fd();
1582 if (fd == -1) {
1583 rc = fd;
1584 goto exit;
1587 status = adouble_open_from_base_fsp(fsp->conn->cwd_fsp,
1588 fsp->base_fsp,
1589 ADOUBLE_RSRC,
1590 flags,
1591 mode,
1592 &ad_fsp);
1593 if (!NT_STATUS_IS_OK(status)) {
1594 errno = map_errno_from_nt_status(status);
1595 rc = -1;
1596 goto exit;
1600 * Now we need to glue both handles together,
1601 * so that they automatically detach each other
1602 * on close.
1604 fio = fruit_get_complete_fio(handle, fsp);
1605 if (fio == NULL) {
1606 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1607 errno = EBADF;
1608 rc = -1;
1609 goto exit;
1612 ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1613 struct fio,
1614 fio_ref_destroy_fn);
1615 if (ref_fio == NULL) {
1616 int saved_errno = errno;
1617 fd_close(ad_fsp);
1618 file_free(NULL, ad_fsp);
1619 ad_fsp = NULL;
1620 errno = saved_errno;
1621 rc = -1;
1622 goto exit;
1625 SMB_ASSERT(ref_fio->fsp == NULL);
1626 ref_fio->handle = handle;
1627 ref_fio->fsp = ad_fsp;
1628 ref_fio->type = ADOUBLE_RSRC;
1629 ref_fio->config = config;
1630 ref_fio->real_fio = fio;
1631 SMB_ASSERT(fio->ad_fsp == NULL);
1632 fio->ad_fsp = ad_fsp;
1633 fio->fake_fd = true;
1635 exit:
1637 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1638 if (rc != 0) {
1639 int saved_errno = errno;
1640 if (fd != -1) {
1641 vfs_fake_fd_close(fd);
1643 errno = saved_errno;
1644 return rc;
1646 return fd;
1649 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1650 const struct files_struct *dirfsp,
1651 const struct smb_filename *smb_fname,
1652 files_struct *fsp,
1653 int flags,
1654 mode_t mode)
1656 #ifdef HAVE_ATTROPEN
1657 int fd = -1;
1660 * As there's no attropenat() this is only going to work with AT_FDCWD.
1662 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1664 fd = attropen(smb_fname->base_name,
1665 AFPRESOURCE_EA_NETATALK,
1666 flags,
1667 mode);
1668 if (fd == -1) {
1669 return -1;
1672 return fd;
1674 #else
1675 errno = ENOSYS;
1676 return -1;
1677 #endif
1680 static int fruit_open_rsrc(vfs_handle_struct *handle,
1681 const struct files_struct *dirfsp,
1682 const struct smb_filename *smb_fname,
1683 files_struct *fsp, int flags, mode_t mode)
1685 int fd;
1686 struct fruit_config_data *config = NULL;
1687 struct fio *fio = NULL;
1689 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1691 SMB_VFS_HANDLE_GET_DATA(handle, config,
1692 struct fruit_config_data, return -1);
1694 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1695 fio->handle = handle;
1696 fio->fsp = fsp;
1697 fio->type = ADOUBLE_RSRC;
1698 fio->config = config;
1700 switch (config->rsrc) {
1701 case FRUIT_RSRC_STREAM: {
1702 struct vfs_open_how how = {
1703 .flags = flags, .mode = mode,
1705 fd = SMB_VFS_NEXT_OPENAT(handle,
1706 dirfsp,
1707 smb_fname,
1708 fsp,
1709 &how);
1710 break;
1713 case FRUIT_RSRC_ADFILE:
1714 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1715 fsp, flags, mode);
1716 break;
1718 case FRUIT_RSRC_XATTR:
1719 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1720 fsp, flags, mode);
1721 break;
1723 default:
1724 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1725 errno = EINVAL;
1726 return -1;
1729 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1731 if (fd == -1) {
1732 return -1;
1735 return fd;
1738 static int fruit_openat(vfs_handle_struct *handle,
1739 const struct files_struct *dirfsp,
1740 const struct smb_filename *smb_fname,
1741 files_struct *fsp,
1742 const struct vfs_open_how *how)
1744 int fd;
1746 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1748 if (!is_named_stream(smb_fname)) {
1749 return SMB_VFS_NEXT_OPENAT(handle,
1750 dirfsp,
1751 smb_fname,
1752 fsp,
1753 how);
1756 if (how->resolve != 0) {
1757 errno = ENOSYS;
1758 return -1;
1761 SMB_ASSERT(fsp_is_alternate_stream(fsp));
1763 if (is_afpinfo_stream(smb_fname->stream_name)) {
1764 fd = fruit_open_meta(handle,
1765 dirfsp,
1766 smb_fname,
1767 fsp,
1768 how->flags,
1769 how->mode);
1770 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1771 fd = fruit_open_rsrc(handle,
1772 dirfsp,
1773 smb_fname,
1774 fsp,
1775 how->flags,
1776 how->mode);
1777 } else {
1778 fd = SMB_VFS_NEXT_OPENAT(handle,
1779 dirfsp,
1780 smb_fname,
1781 fsp,
1782 how);
1785 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1787 /* Prevent reopen optimisation */
1788 fsp->fsp_flags.have_proc_fds = false;
1789 return fd;
1792 static int fruit_close_meta(vfs_handle_struct *handle,
1793 files_struct *fsp)
1795 int ret;
1796 struct fruit_config_data *config = NULL;
1798 SMB_VFS_HANDLE_GET_DATA(handle, config,
1799 struct fruit_config_data, return -1);
1801 switch (config->meta) {
1802 case FRUIT_META_STREAM:
1804 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1805 if (fio == NULL) {
1806 return -1;
1808 if (fio->fake_fd) {
1809 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1810 fsp_set_fd(fsp, -1);
1811 } else {
1812 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1814 break;
1816 case FRUIT_META_NETATALK:
1817 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1818 fsp_set_fd(fsp, -1);
1819 break;
1821 default:
1822 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1823 return -1;
1826 return ret;
1830 static int fruit_close_rsrc(vfs_handle_struct *handle,
1831 files_struct *fsp)
1833 int ret;
1834 struct fruit_config_data *config = NULL;
1836 SMB_VFS_HANDLE_GET_DATA(handle, config,
1837 struct fruit_config_data, return -1);
1839 switch (config->rsrc) {
1840 case FRUIT_RSRC_STREAM:
1841 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1842 break;
1844 case FRUIT_RSRC_ADFILE:
1846 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1847 if (fio == NULL) {
1848 return -1;
1850 fio_close_ad_fsp(fio);
1851 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1852 fsp_set_fd(fsp, -1);
1853 break;
1856 case FRUIT_RSRC_XATTR:
1857 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1858 fsp_set_fd(fsp, -1);
1859 break;
1861 default:
1862 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1863 return -1;
1866 return ret;
1869 static int fruit_close(vfs_handle_struct *handle,
1870 files_struct *fsp)
1872 int ret;
1873 int fd;
1875 fd = fsp_get_pathref_fd(fsp);
1877 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1879 if (!fsp_is_alternate_stream(fsp)) {
1880 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1883 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1884 ret = fruit_close_meta(handle, fsp);
1885 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1886 ret = fruit_close_rsrc(handle, fsp);
1887 } else {
1888 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1891 return ret;
1894 static int fruit_renameat(struct vfs_handle_struct *handle,
1895 files_struct *srcfsp,
1896 const struct smb_filename *smb_fname_src,
1897 files_struct *dstfsp,
1898 const struct smb_filename *smb_fname_dst,
1899 const struct vfs_rename_how *how)
1901 int rc = -1;
1902 struct fruit_config_data *config = NULL;
1903 struct smb_filename *src_adp_smb_fname = NULL;
1904 struct smb_filename *dst_adp_smb_fname = NULL;
1906 SMB_VFS_HANDLE_GET_DATA(handle, config,
1907 struct fruit_config_data, return -1);
1909 if (!VALID_STAT(smb_fname_src->st)) {
1910 DBG_ERR("Need valid stat for [%s]\n",
1911 smb_fname_str_dbg(smb_fname_src));
1912 return -1;
1915 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1916 srcfsp,
1917 smb_fname_src,
1918 dstfsp,
1919 smb_fname_dst,
1920 how);
1921 if (rc != 0) {
1922 return -1;
1925 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1926 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1928 return 0;
1931 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1932 if (rc != 0) {
1933 goto done;
1936 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1937 if (rc != 0) {
1938 goto done;
1941 DBG_DEBUG("%s -> %s\n",
1942 smb_fname_str_dbg(src_adp_smb_fname),
1943 smb_fname_str_dbg(dst_adp_smb_fname));
1945 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1946 srcfsp,
1947 src_adp_smb_fname,
1948 dstfsp,
1949 dst_adp_smb_fname,
1950 how);
1951 if (errno == ENOENT) {
1952 rc = 0;
1955 done:
1956 TALLOC_FREE(src_adp_smb_fname);
1957 TALLOC_FREE(dst_adp_smb_fname);
1958 return rc;
1961 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1962 struct files_struct *dirfsp,
1963 const struct smb_filename *smb_fname)
1965 return SMB_VFS_NEXT_UNLINKAT(handle,
1966 dirfsp,
1967 smb_fname,
1971 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1972 const struct smb_filename *smb_fname)
1974 SMB_ASSERT(smb_fname->fsp != NULL);
1975 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1976 return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1977 AFPINFO_EA_NETATALK);
1980 static int fruit_unlink_meta(vfs_handle_struct *handle,
1981 struct files_struct *dirfsp,
1982 const struct smb_filename *smb_fname)
1984 struct fruit_config_data *config = NULL;
1985 int rc;
1987 SMB_VFS_HANDLE_GET_DATA(handle, config,
1988 struct fruit_config_data, return -1);
1990 switch (config->meta) {
1991 case FRUIT_META_STREAM:
1992 rc = fruit_unlink_meta_stream(handle,
1993 dirfsp,
1994 smb_fname);
1995 break;
1997 case FRUIT_META_NETATALK:
1998 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1999 break;
2001 default:
2002 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2003 return -1;
2006 return rc;
2009 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2010 struct files_struct *dirfsp,
2011 const struct smb_filename *smb_fname,
2012 bool force_unlink)
2014 int ret;
2016 if (!force_unlink) {
2017 struct smb_filename *full_fname = NULL;
2018 off_t size;
2021 * TODO: use SMB_VFS_STATX() once we have it.
2024 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2025 dirfsp,
2026 smb_fname);
2027 if (full_fname == NULL) {
2028 return -1;
2032 * 0 byte resource fork streams are not listed by
2033 * vfs_streaminfo, as a result stream cleanup/deletion of file
2034 * deletion doesn't remove the resourcefork stream.
2037 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2038 if (ret != 0) {
2039 TALLOC_FREE(full_fname);
2040 DBG_ERR("stat [%s] failed [%s]\n",
2041 smb_fname_str_dbg(full_fname), strerror(errno));
2042 return -1;
2045 size = full_fname->st.st_ex_size;
2046 TALLOC_FREE(full_fname);
2048 if (size > 0) {
2049 /* OS X ignores resource fork stream delete requests */
2050 return 0;
2054 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2055 dirfsp,
2056 smb_fname,
2058 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2059 ret = 0;
2062 return ret;
2065 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2066 struct files_struct *dirfsp,
2067 const struct smb_filename *smb_fname,
2068 bool force_unlink)
2070 int rc;
2071 struct adouble *ad = NULL;
2072 struct smb_filename *adp_smb_fname = NULL;
2074 if (!force_unlink) {
2075 struct smb_filename *full_fname = NULL;
2077 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2078 dirfsp,
2079 smb_fname);
2080 if (full_fname == NULL) {
2081 return -1;
2084 ad = ad_get(talloc_tos(), handle, full_fname,
2085 ADOUBLE_RSRC);
2086 TALLOC_FREE(full_fname);
2087 if (ad == NULL) {
2088 errno = ENOENT;
2089 return -1;
2094 * 0 byte resource fork streams are not listed by
2095 * vfs_streaminfo, as a result stream cleanup/deletion of file
2096 * deletion doesn't remove the resourcefork stream.
2099 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2100 /* OS X ignores resource fork stream delete requests */
2101 TALLOC_FREE(ad);
2102 return 0;
2105 TALLOC_FREE(ad);
2108 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2109 if (rc != 0) {
2110 return -1;
2113 rc = SMB_VFS_NEXT_UNLINKAT(handle,
2114 dirfsp,
2115 adp_smb_fname,
2117 TALLOC_FREE(adp_smb_fname);
2118 if ((rc != 0) && (errno == ENOENT || errno == ENAMETOOLONG) && force_unlink) {
2119 rc = 0;
2122 return rc;
2125 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2126 const struct smb_filename *smb_fname,
2127 bool force_unlink)
2130 * OS X ignores resource fork stream delete requests, so nothing to do
2131 * here. Removing the file will remove the xattr anyway, so we don't
2132 * have to take care of removing 0 byte resource forks that could be
2133 * left behind.
2135 return 0;
2138 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2139 struct files_struct *dirfsp,
2140 const struct smb_filename *smb_fname,
2141 bool force_unlink)
2143 struct fruit_config_data *config = NULL;
2144 int rc;
2146 SMB_VFS_HANDLE_GET_DATA(handle, config,
2147 struct fruit_config_data, return -1);
2149 switch (config->rsrc) {
2150 case FRUIT_RSRC_STREAM:
2151 rc = fruit_unlink_rsrc_stream(handle,
2152 dirfsp,
2153 smb_fname,
2154 force_unlink);
2155 break;
2157 case FRUIT_RSRC_ADFILE:
2158 rc = fruit_unlink_rsrc_adouble(handle,
2159 dirfsp,
2160 smb_fname,
2161 force_unlink);
2162 break;
2164 case FRUIT_RSRC_XATTR:
2165 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2166 break;
2168 default:
2169 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2170 return -1;
2173 return rc;
2176 static int fruit_fchmod(vfs_handle_struct *handle,
2177 struct files_struct *fsp,
2178 mode_t mode)
2180 int rc = -1;
2181 struct fruit_config_data *config = NULL;
2182 struct smb_filename *smb_fname_adp = NULL;
2183 const struct smb_filename *smb_fname = NULL;
2184 NTSTATUS status;
2186 rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2187 if (rc != 0) {
2188 return rc;
2191 smb_fname = fsp->fsp_name;
2192 SMB_VFS_HANDLE_GET_DATA(handle, config,
2193 struct fruit_config_data, return -1);
2195 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2196 return 0;
2199 if (!VALID_STAT(smb_fname->st)) {
2200 return 0;
2203 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2204 return 0;
2207 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2208 if (rc != 0) {
2209 return -1;
2212 status = openat_pathref_fsp(handle->conn->cwd_fsp,
2213 smb_fname_adp);
2214 if (!NT_STATUS_IS_OK(status)) {
2215 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2216 if (NT_STATUS_EQUAL(status,
2217 NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2218 rc = 0;
2219 goto out;
2221 rc = -1;
2222 goto out;
2225 DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2227 rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2228 if (errno == ENOENT) {
2229 rc = 0;
2231 out:
2232 TALLOC_FREE(smb_fname_adp);
2233 return rc;
2236 static int fruit_unlinkat(vfs_handle_struct *handle,
2237 struct files_struct *dirfsp,
2238 const struct smb_filename *smb_fname,
2239 int flags)
2241 struct fruit_config_data *config = NULL;
2242 struct smb_filename *rsrc_smb_fname = NULL;
2243 int ret;
2245 if (flags & AT_REMOVEDIR) {
2246 return SMB_VFS_NEXT_UNLINKAT(handle,
2247 dirfsp,
2248 smb_fname,
2249 AT_REMOVEDIR);
2252 SMB_VFS_HANDLE_GET_DATA(handle, config,
2253 struct fruit_config_data, return -1);
2255 if (is_afpinfo_stream(smb_fname->stream_name)) {
2256 return fruit_unlink_meta(handle,
2257 dirfsp,
2258 smb_fname);
2259 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2260 return fruit_unlink_rsrc(handle,
2261 dirfsp,
2262 smb_fname,
2263 false);
2264 } else if (is_named_stream(smb_fname)) {
2265 return SMB_VFS_NEXT_UNLINKAT(handle,
2266 dirfsp,
2267 smb_fname,
2269 } else if (is_adouble_file(smb_fname->base_name)) {
2270 return SMB_VFS_NEXT_UNLINKAT(handle,
2271 dirfsp,
2272 smb_fname,
2277 * A request to delete the base file. Because 0 byte resource
2278 * fork streams are not listed by fruit_streaminfo,
2279 * delete_all_streams() can't remove 0 byte resource fork
2280 * streams, so we have to cleanup this here.
2282 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2283 smb_fname->base_name,
2284 AFPRESOURCE_STREAM_NAME,
2285 NULL,
2286 smb_fname->twrp,
2287 smb_fname->flags);
2288 if (rsrc_smb_fname == NULL) {
2289 return -1;
2292 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2293 if ((ret != 0) && (errno != ENOENT)) {
2294 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2295 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2296 TALLOC_FREE(rsrc_smb_fname);
2297 return -1;
2299 TALLOC_FREE(rsrc_smb_fname);
2301 return SMB_VFS_NEXT_UNLINKAT(handle,
2302 dirfsp,
2303 smb_fname,
2307 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2308 files_struct *fsp, void *data,
2309 size_t n, off_t offset)
2311 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2312 ssize_t nread;
2313 int ret;
2315 if ((fio == NULL) || fio->fake_fd) {
2316 return -1;
2319 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2320 if (nread == -1 || nread == n) {
2321 return nread;
2324 DBG_ERR("Removing [%s] after short read [%zd]\n",
2325 fsp_str_dbg(fsp), nread);
2327 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2328 fsp->conn->cwd_fsp,
2329 fsp->fsp_name,
2331 if (ret != 0) {
2332 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2333 return -1;
2336 errno = EINVAL;
2337 return -1;
2340 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2341 files_struct *fsp, void *data,
2342 size_t n, off_t offset)
2344 AfpInfo *ai = NULL;
2345 struct adouble *ad = NULL;
2346 char afpinfo_buf[AFP_INFO_SIZE];
2347 char *p = NULL;
2348 ssize_t nread;
2350 ai = afpinfo_new(talloc_tos());
2351 if (ai == NULL) {
2352 return -1;
2355 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2356 if (ad == NULL) {
2357 nread = -1;
2358 goto fail;
2361 p = ad_get_entry(ad, ADEID_FINDERI);
2362 if (p == NULL) {
2363 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2364 nread = -1;
2365 goto fail;
2368 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2370 nread = afpinfo_pack(ai, afpinfo_buf);
2371 if (nread != AFP_INFO_SIZE) {
2372 nread = -1;
2373 goto fail;
2376 memcpy(data, afpinfo_buf, n);
2377 nread = n;
2379 fail:
2380 TALLOC_FREE(ai);
2381 return nread;
2384 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2385 files_struct *fsp, void *data,
2386 size_t n, off_t offset)
2388 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2389 ssize_t nread;
2390 ssize_t to_return;
2393 * OS X has a off-by-1 error in the offset calculation, so we're
2394 * bug compatible here. It won't hurt, as any relevant real
2395 * world read requests from the AFP_AfpInfo stream will be
2396 * offset=0 n=60. offset is ignored anyway, see below.
2398 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2399 return 0;
2402 if (fio == NULL) {
2403 DBG_ERR("Failed to fetch fsp extension\n");
2404 return -1;
2407 /* Yes, macOS always reads from offset 0 */
2408 offset = 0;
2409 to_return = MIN(n, AFP_INFO_SIZE);
2411 switch (fio->config->meta) {
2412 case FRUIT_META_STREAM:
2413 nread = fruit_pread_meta_stream(handle, fsp, data,
2414 to_return, offset);
2415 break;
2417 case FRUIT_META_NETATALK:
2418 nread = fruit_pread_meta_adouble(handle, fsp, data,
2419 to_return, offset);
2420 break;
2422 default:
2423 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2424 return -1;
2427 if (nread == -1 && fio->fake_fd) {
2428 AfpInfo *ai = NULL;
2429 char afpinfo_buf[AFP_INFO_SIZE];
2431 ai = afpinfo_new(talloc_tos());
2432 if (ai == NULL) {
2433 return -1;
2436 nread = afpinfo_pack(ai, afpinfo_buf);
2437 TALLOC_FREE(ai);
2438 if (nread != AFP_INFO_SIZE) {
2439 return -1;
2442 memcpy(data, afpinfo_buf, to_return);
2443 return to_return;
2446 return nread;
2449 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2450 files_struct *fsp, void *data,
2451 size_t n, off_t offset)
2453 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2456 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2457 files_struct *fsp, void *data,
2458 size_t n, off_t offset)
2460 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2463 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2464 files_struct *fsp, void *data,
2465 size_t n, off_t offset)
2467 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2468 struct adouble *ad = NULL;
2469 ssize_t nread;
2471 if (fio == NULL || fio->ad_fsp == NULL) {
2472 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2473 errno = EBADF;
2474 return -1;
2477 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2478 if (ad == NULL) {
2479 DBG_ERR("ad_fget [%s] failed [%s]\n",
2480 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2481 return -1;
2484 nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2485 offset + ad_getentryoff(ad, ADEID_RFORK));
2487 TALLOC_FREE(ad);
2488 return nread;
2491 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2492 files_struct *fsp, void *data,
2493 size_t n, off_t offset)
2495 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2496 ssize_t nread;
2498 if (fio == NULL) {
2499 errno = EINVAL;
2500 return -1;
2503 switch (fio->config->rsrc) {
2504 case FRUIT_RSRC_STREAM:
2505 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2506 break;
2508 case FRUIT_RSRC_ADFILE:
2509 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2510 break;
2512 case FRUIT_RSRC_XATTR:
2513 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2514 break;
2516 default:
2517 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2518 return -1;
2521 return nread;
2524 static ssize_t fruit_pread(vfs_handle_struct *handle,
2525 files_struct *fsp, void *data,
2526 size_t n, off_t offset)
2528 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2529 ssize_t nread;
2531 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2532 fsp_str_dbg(fsp), (intmax_t)offset, n);
2534 if (fio == NULL) {
2535 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2538 if (fio->type == ADOUBLE_META) {
2539 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2540 } else {
2541 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2544 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2545 return nread;
2548 static bool fruit_must_handle_aio_stream(struct fio *fio)
2550 if (fio == NULL) {
2551 return false;
2554 if (fio->type == ADOUBLE_META) {
2555 return true;
2558 if ((fio->type == ADOUBLE_RSRC) &&
2559 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2561 return true;
2564 return false;
2567 struct fruit_pread_state {
2568 ssize_t nread;
2569 struct vfs_aio_state vfs_aio_state;
2572 static void fruit_pread_done(struct tevent_req *subreq);
2574 static struct tevent_req *fruit_pread_send(
2575 struct vfs_handle_struct *handle,
2576 TALLOC_CTX *mem_ctx,
2577 struct tevent_context *ev,
2578 struct files_struct *fsp,
2579 void *data,
2580 size_t n, off_t offset)
2582 struct tevent_req *req = NULL;
2583 struct tevent_req *subreq = NULL;
2584 struct fruit_pread_state *state = NULL;
2585 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2587 req = tevent_req_create(mem_ctx, &state,
2588 struct fruit_pread_state);
2589 if (req == NULL) {
2590 return NULL;
2593 if (fruit_must_handle_aio_stream(fio)) {
2594 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2595 if (state->nread != n) {
2596 if (state->nread != -1) {
2597 errno = EIO;
2599 tevent_req_error(req, errno);
2600 return tevent_req_post(req, ev);
2602 tevent_req_done(req);
2603 return tevent_req_post(req, ev);
2606 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2607 data, n, offset);
2608 if (tevent_req_nomem(req, subreq)) {
2609 return tevent_req_post(req, ev);
2611 tevent_req_set_callback(subreq, fruit_pread_done, req);
2612 return req;
2615 static void fruit_pread_done(struct tevent_req *subreq)
2617 struct tevent_req *req = tevent_req_callback_data(
2618 subreq, struct tevent_req);
2619 struct fruit_pread_state *state = tevent_req_data(
2620 req, struct fruit_pread_state);
2622 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2623 TALLOC_FREE(subreq);
2625 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2626 return;
2628 tevent_req_done(req);
2631 static ssize_t fruit_pread_recv(struct tevent_req *req,
2632 struct vfs_aio_state *vfs_aio_state)
2634 struct fruit_pread_state *state = tevent_req_data(
2635 req, struct fruit_pread_state);
2636 ssize_t retval = -1;
2638 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2639 tevent_req_received(req);
2640 return -1;
2643 *vfs_aio_state = state->vfs_aio_state;
2644 retval = state->nread;
2645 tevent_req_received(req);
2646 return retval;
2649 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2650 files_struct *fsp, const void *indata,
2651 size_t n, off_t offset)
2653 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2654 const void *data = indata;
2655 char afpinfo_buf[AFP_INFO_SIZE];
2656 AfpInfo *ai = NULL;
2657 size_t nwritten;
2658 int ret;
2659 bool ok;
2661 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2662 fsp_str_dbg(fsp), (intmax_t)offset, n);
2664 if (fio == NULL) {
2665 return -1;
2668 if (fio->fake_fd) {
2669 struct vfs_open_how how = {
2670 .flags = fio->flags, .mode = fio->mode,
2672 int fd = fsp_get_pathref_fd(fsp);
2674 ret = vfs_fake_fd_close(fd);
2675 fsp_set_fd(fsp, -1);
2676 if (ret != 0) {
2677 DBG_ERR("Close [%s] failed: %s\n",
2678 fsp_str_dbg(fsp), strerror(errno));
2679 return -1;
2682 fd = SMB_VFS_NEXT_OPENAT(handle,
2683 NULL, /* opening a stream */
2684 fsp->fsp_name,
2685 fsp,
2686 &how);
2687 if (fd == -1) {
2688 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2689 fsp_str_dbg(fsp), strerror(errno));
2690 return -1;
2692 fsp_set_fd(fsp, fd);
2693 fio->fake_fd = false;
2696 ai = afpinfo_unpack(talloc_tos(), data, fio->config->validate_afpinfo);
2697 if (ai == NULL) {
2698 return -1;
2701 if (ai_empty_finderinfo(ai)) {
2703 * Writing an all 0 blob to the metadata stream results in the
2704 * stream being removed on a macOS server. This ensures we
2705 * behave the same and it verified by the "delete AFP_AfpInfo by
2706 * writing all 0" test.
2708 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2709 if (ret != 0) {
2710 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2711 fsp_str_dbg(fsp));
2712 return -1;
2715 ok = set_delete_on_close(
2716 fsp,
2717 true,
2718 handle->conn->session_info->security_token,
2719 handle->conn->session_info->unix_token);
2720 if (!ok) {
2721 DBG_ERR("set_delete_on_close on [%s] failed\n",
2722 fsp_str_dbg(fsp));
2723 return -1;
2725 return n;
2728 if (!fio->config->validate_afpinfo) {
2730 * Ensure the buffer contains a valid header, so marshall
2731 * the data from the afpinfo struck back into a buffer
2732 * and write that instead of the possibly malformed data
2733 * we got from the client.
2735 nwritten = afpinfo_pack(ai, afpinfo_buf);
2736 if (nwritten != AFP_INFO_SIZE) {
2737 errno = EINVAL;
2738 return -1;
2740 data = afpinfo_buf;
2743 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2744 if (nwritten != n) {
2745 return -1;
2748 return n;
2751 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2752 files_struct *fsp, const void *data,
2753 size_t n, off_t offset)
2755 struct fruit_config_data *config = NULL;
2756 struct adouble *ad = NULL;
2757 AfpInfo *ai = NULL;
2758 char *p = NULL;
2759 int ret;
2760 bool ok;
2762 SMB_VFS_HANDLE_GET_DATA(handle, config,
2763 struct fruit_config_data, return -1);
2765 ai = afpinfo_unpack(talloc_tos(), data, config->validate_afpinfo);
2766 if (ai == NULL) {
2767 return -1;
2770 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2771 if (ad == NULL) {
2772 ad = ad_init(talloc_tos(), ADOUBLE_META);
2773 if (ad == NULL) {
2774 return -1;
2777 p = ad_get_entry(ad, ADEID_FINDERI);
2778 if (p == NULL) {
2779 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2780 TALLOC_FREE(ad);
2781 return -1;
2784 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2786 ret = ad_fset(handle, ad, fsp);
2787 if (ret != 0) {
2788 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2789 TALLOC_FREE(ad);
2790 return -1;
2793 TALLOC_FREE(ad);
2795 if (!ai_empty_finderinfo(ai)) {
2796 return n;
2800 * Writing an all 0 blob to the metadata stream results in the stream
2801 * being removed on a macOS server. This ensures we behave the same and
2802 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2805 ok = set_delete_on_close(
2806 fsp,
2807 true,
2808 handle->conn->session_info->security_token,
2809 handle->conn->session_info->unix_token);
2810 if (!ok) {
2811 DBG_ERR("set_delete_on_close on [%s] failed\n",
2812 fsp_str_dbg(fsp));
2813 return -1;
2816 return n;
2819 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2820 files_struct *fsp, const void *data,
2821 size_t n, off_t offset)
2823 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2824 ssize_t nwritten;
2825 uint8_t buf[AFP_INFO_SIZE];
2826 size_t to_write;
2827 size_t to_copy;
2828 int cmp;
2830 if (fio == NULL) {
2831 DBG_ERR("Failed to fetch fsp extension\n");
2832 return -1;
2835 if (n < 3) {
2836 errno = EINVAL;
2837 return -1;
2840 if (offset != 0 && n < 60) {
2841 errno = EINVAL;
2842 return -1;
2845 if (fio->config->validate_afpinfo) {
2846 cmp = memcmp(data, "AFP", 3);
2847 if (cmp != 0) {
2848 errno = EINVAL;
2849 return -1;
2853 if (n <= AFP_OFF_FinderInfo) {
2855 * Nothing to do here really, just return
2857 return n;
2860 offset = 0;
2862 to_copy = n;
2863 if (to_copy > AFP_INFO_SIZE) {
2864 to_copy = AFP_INFO_SIZE;
2866 memcpy(buf, data, to_copy);
2868 to_write = n;
2869 if (to_write != AFP_INFO_SIZE) {
2870 to_write = AFP_INFO_SIZE;
2873 switch (fio->config->meta) {
2874 case FRUIT_META_STREAM:
2875 nwritten = fruit_pwrite_meta_stream(handle,
2876 fsp,
2877 buf,
2878 to_write,
2879 offset);
2880 break;
2882 case FRUIT_META_NETATALK:
2883 nwritten = fruit_pwrite_meta_netatalk(handle,
2884 fsp,
2885 buf,
2886 to_write,
2887 offset);
2888 break;
2890 default:
2891 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2892 return -1;
2895 if (nwritten != to_write) {
2896 return -1;
2900 * Return the requested amount, verified against macOS SMB server
2902 return n;
2905 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2906 files_struct *fsp, const void *data,
2907 size_t n, off_t offset)
2909 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2912 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2913 files_struct *fsp, const void *data,
2914 size_t n, off_t offset)
2916 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2919 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2920 files_struct *fsp, const void *data,
2921 size_t n, off_t offset)
2923 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2924 struct adouble *ad = NULL;
2925 ssize_t nwritten;
2926 int ret;
2928 if (fio == NULL || fio->ad_fsp == NULL) {
2929 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2930 errno = EBADF;
2931 return -1;
2934 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2935 if (ad == NULL) {
2936 DBG_ERR("ad_fget [%s] failed [%s]\n",
2937 fsp_str_dbg(fio->ad_fsp), strerror(errno));
2938 return -1;
2941 nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2942 offset + ad_getentryoff(ad, ADEID_RFORK));
2943 if (nwritten != n) {
2944 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2945 fsp_str_dbg(fio->ad_fsp), nwritten, n);
2946 TALLOC_FREE(ad);
2947 return -1;
2950 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2951 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2952 ret = ad_fset(handle, ad, fio->ad_fsp);
2953 if (ret != 0) {
2954 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2955 TALLOC_FREE(ad);
2956 return -1;
2960 TALLOC_FREE(ad);
2961 return n;
2964 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2965 files_struct *fsp, const void *data,
2966 size_t n, off_t offset)
2968 struct fio *fio = fruit_get_complete_fio(handle, fsp);
2969 ssize_t nwritten;
2971 if (fio == NULL) {
2972 DBG_ERR("Failed to fetch fsp extension\n");
2973 return -1;
2976 switch (fio->config->rsrc) {
2977 case FRUIT_RSRC_STREAM:
2978 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2979 break;
2981 case FRUIT_RSRC_ADFILE:
2982 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2983 break;
2985 case FRUIT_RSRC_XATTR:
2986 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2987 break;
2989 default:
2990 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2991 return -1;
2994 return nwritten;
2997 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2998 files_struct *fsp, const void *data,
2999 size_t n, off_t offset)
3001 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3002 ssize_t nwritten;
3004 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3005 fsp_str_dbg(fsp), (intmax_t)offset, n);
3007 if (fio == NULL) {
3008 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3011 if (fio->type == ADOUBLE_META) {
3012 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
3013 } else {
3014 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
3017 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
3018 return nwritten;
3021 struct fruit_pwrite_state {
3022 ssize_t nwritten;
3023 struct vfs_aio_state vfs_aio_state;
3026 static void fruit_pwrite_done(struct tevent_req *subreq);
3028 static struct tevent_req *fruit_pwrite_send(
3029 struct vfs_handle_struct *handle,
3030 TALLOC_CTX *mem_ctx,
3031 struct tevent_context *ev,
3032 struct files_struct *fsp,
3033 const void *data,
3034 size_t n, off_t offset)
3036 struct tevent_req *req = NULL;
3037 struct tevent_req *subreq = NULL;
3038 struct fruit_pwrite_state *state = NULL;
3039 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3041 req = tevent_req_create(mem_ctx, &state,
3042 struct fruit_pwrite_state);
3043 if (req == NULL) {
3044 return NULL;
3047 if (fruit_must_handle_aio_stream(fio)) {
3048 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3049 if (state->nwritten != n) {
3050 if (state->nwritten != -1) {
3051 errno = EIO;
3053 tevent_req_error(req, errno);
3054 return tevent_req_post(req, ev);
3056 tevent_req_done(req);
3057 return tevent_req_post(req, ev);
3060 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3061 data, n, offset);
3062 if (tevent_req_nomem(req, subreq)) {
3063 return tevent_req_post(req, ev);
3065 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3066 return req;
3069 static void fruit_pwrite_done(struct tevent_req *subreq)
3071 struct tevent_req *req = tevent_req_callback_data(
3072 subreq, struct tevent_req);
3073 struct fruit_pwrite_state *state = tevent_req_data(
3074 req, struct fruit_pwrite_state);
3076 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3077 TALLOC_FREE(subreq);
3079 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3080 return;
3082 tevent_req_done(req);
3085 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3086 struct vfs_aio_state *vfs_aio_state)
3088 struct fruit_pwrite_state *state = tevent_req_data(
3089 req, struct fruit_pwrite_state);
3090 ssize_t retval = -1;
3092 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3093 tevent_req_received(req);
3094 return -1;
3097 *vfs_aio_state = state->vfs_aio_state;
3098 retval = state->nwritten;
3099 tevent_req_received(req);
3100 return retval;
3103 struct fruit_fsync_state {
3104 int ret;
3105 struct vfs_aio_state vfs_aio_state;
3108 static void fruit_fsync_done(struct tevent_req *subreq);
3110 static struct tevent_req *fruit_fsync_send(
3111 struct vfs_handle_struct *handle,
3112 TALLOC_CTX *mem_ctx,
3113 struct tevent_context *ev,
3114 struct files_struct *fsp)
3116 struct tevent_req *req = NULL;
3117 struct tevent_req *subreq = NULL;
3118 struct fruit_fsync_state *state = NULL;
3119 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3121 req = tevent_req_create(mem_ctx, &state,
3122 struct fruit_fsync_state);
3123 if (req == NULL) {
3124 return NULL;
3127 if (fruit_must_handle_aio_stream(fio)) {
3128 struct adouble *ad = NULL;
3130 if (fio->type == ADOUBLE_META) {
3132 * We must never pass a fake_fd
3133 * to lower level fsync calls.
3134 * Everything is already done
3135 * synchronously, so just return
3136 * true.
3138 SMB_ASSERT(fio->fake_fd);
3139 tevent_req_done(req);
3140 return tevent_req_post(req, ev);
3144 * We know the following must be true,
3145 * as it's the condition for fruit_must_handle_aio_stream()
3146 * to return true if fio->type == ADOUBLE_RSRC.
3148 SMB_ASSERT(fio->config->rsrc == FRUIT_RSRC_ADFILE);
3149 if (fio->ad_fsp == NULL) {
3150 tevent_req_error(req, EBADF);
3151 return tevent_req_post(req, ev);
3153 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3154 if (ad == NULL) {
3155 tevent_req_error(req, ENOMEM);
3156 return tevent_req_post(req, ev);
3158 fsp = fio->ad_fsp;
3161 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
3162 if (tevent_req_nomem(req, subreq)) {
3163 return tevent_req_post(req, ev);
3165 tevent_req_set_callback(subreq, fruit_fsync_done, req);
3166 return req;
3169 static void fruit_fsync_done(struct tevent_req *subreq)
3171 struct tevent_req *req = tevent_req_callback_data(
3172 subreq, struct tevent_req);
3173 struct fruit_fsync_state *state = tevent_req_data(
3174 req, struct fruit_fsync_state);
3176 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
3177 TALLOC_FREE(subreq);
3178 if (state->ret != 0) {
3179 tevent_req_error(req, errno);
3180 return;
3182 tevent_req_done(req);
3185 static int fruit_fsync_recv(struct tevent_req *req,
3186 struct vfs_aio_state *vfs_aio_state)
3188 struct fruit_fsync_state *state = tevent_req_data(
3189 req, struct fruit_fsync_state);
3190 int retval = -1;
3192 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3193 tevent_req_received(req);
3194 return -1;
3197 *vfs_aio_state = state->vfs_aio_state;
3198 retval = state->ret;
3199 tevent_req_received(req);
3200 return retval;
3204 * Helper to stat/lstat the base file of an smb_fname.
3206 static int fruit_stat_base(vfs_handle_struct *handle,
3207 struct smb_filename *smb_fname,
3208 bool follow_links)
3210 char *tmp_stream_name;
3211 int rc;
3213 tmp_stream_name = smb_fname->stream_name;
3214 smb_fname->stream_name = NULL;
3215 if (follow_links) {
3216 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3217 } else {
3218 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3220 smb_fname->stream_name = tmp_stream_name;
3222 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3223 smb_fname->base_name,
3224 (uintmax_t)smb_fname->st.st_ex_dev,
3225 (uintmax_t)smb_fname->st.st_ex_ino);
3226 return rc;
3229 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3230 struct smb_filename *smb_fname,
3231 bool follow_links)
3233 int ret;
3234 ino_t ino;
3236 ret = fruit_stat_base(handle, smb_fname, false);
3237 if (ret != 0) {
3238 return -1;
3241 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3243 if (follow_links) {
3244 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3245 } else {
3246 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3249 smb_fname->st.st_ex_ino = ino;
3251 return ret;
3254 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3255 struct smb_filename *smb_fname,
3256 bool follow_links)
3258 struct adouble *ad = NULL;
3260 /* Populate the stat struct with info from the base file. */
3261 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3262 return -1;
3265 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3266 if (ad == NULL) {
3267 DBG_INFO("fruit_stat_meta %s: %s\n",
3268 smb_fname_str_dbg(smb_fname), strerror(errno));
3269 errno = ENOENT;
3270 return -1;
3272 TALLOC_FREE(ad);
3274 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3275 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3276 smb_fname->stream_name);
3277 return 0;
3280 static int fruit_stat_meta(vfs_handle_struct *handle,
3281 struct smb_filename *smb_fname,
3282 bool follow_links)
3284 struct fruit_config_data *config = NULL;
3285 int ret;
3287 SMB_VFS_HANDLE_GET_DATA(handle, config,
3288 struct fruit_config_data, return -1);
3290 switch (config->meta) {
3291 case FRUIT_META_STREAM:
3292 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3293 break;
3295 case FRUIT_META_NETATALK:
3296 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3297 break;
3299 default:
3300 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3301 return -1;
3304 return ret;
3307 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3308 struct smb_filename *smb_fname,
3309 bool follow_links)
3311 struct adouble *ad = NULL;
3312 int ret;
3314 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3315 if (ad == NULL) {
3316 errno = ENOENT;
3317 return -1;
3320 /* Populate the stat struct with info from the base file. */
3321 ret = fruit_stat_base(handle, smb_fname, follow_links);
3322 if (ret != 0) {
3323 TALLOC_FREE(ad);
3324 return -1;
3327 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3328 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3329 smb_fname->stream_name);
3330 TALLOC_FREE(ad);
3331 return 0;
3334 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3335 struct smb_filename *smb_fname,
3336 bool follow_links)
3338 int ret;
3340 if (follow_links) {
3341 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3342 } else {
3343 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3346 return ret;
3349 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3350 struct smb_filename *smb_fname,
3351 bool follow_links)
3353 #ifdef HAVE_ATTROPEN
3354 int ret;
3355 int fd = -1;
3357 /* Populate the stat struct with info from the base file. */
3358 ret = fruit_stat_base(handle, smb_fname, follow_links);
3359 if (ret != 0) {
3360 return -1;
3363 fd = attropen(smb_fname->base_name,
3364 AFPRESOURCE_EA_NETATALK,
3365 O_RDONLY);
3366 if (fd == -1) {
3367 return 0;
3370 ret = sys_fstat(fd, &smb_fname->st, false);
3371 if (ret != 0) {
3372 close(fd);
3373 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3374 AFPRESOURCE_EA_NETATALK);
3375 return -1;
3377 close(fd);
3378 fd = -1;
3380 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3381 smb_fname->stream_name);
3383 return ret;
3385 #else
3386 errno = ENOSYS;
3387 return -1;
3388 #endif
3391 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3392 struct smb_filename *smb_fname,
3393 bool follow_links)
3395 struct fruit_config_data *config = NULL;
3396 int ret;
3398 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3400 SMB_VFS_HANDLE_GET_DATA(handle, config,
3401 struct fruit_config_data, return -1);
3403 switch (config->rsrc) {
3404 case FRUIT_RSRC_STREAM:
3405 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3406 break;
3408 case FRUIT_RSRC_XATTR:
3409 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3410 break;
3412 case FRUIT_RSRC_ADFILE:
3413 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3414 break;
3416 default:
3417 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3418 return -1;
3421 return ret;
3424 static int fruit_stat(vfs_handle_struct *handle,
3425 struct smb_filename *smb_fname)
3427 int rc = -1;
3429 DEBUG(10, ("fruit_stat called for %s\n",
3430 smb_fname_str_dbg(smb_fname)));
3432 if (!is_named_stream(smb_fname)) {
3433 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3434 if (rc == 0) {
3435 update_btime(handle, smb_fname);
3437 return rc;
3441 * Note if lp_posix_paths() is true, we can never
3442 * get here as is_ntfs_stream_smb_fname() is
3443 * always false. So we never need worry about
3444 * not following links here.
3447 if (is_afpinfo_stream(smb_fname->stream_name)) {
3448 rc = fruit_stat_meta(handle, smb_fname, true);
3449 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3450 rc = fruit_stat_rsrc(handle, smb_fname, true);
3451 } else {
3452 return SMB_VFS_NEXT_STAT(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_lstat(vfs_handle_struct *handle,
3466 struct smb_filename *smb_fname)
3468 int rc = -1;
3470 DEBUG(10, ("fruit_lstat called for %s\n",
3471 smb_fname_str_dbg(smb_fname)));
3473 if (!is_named_stream(smb_fname)) {
3474 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3475 if (rc == 0) {
3476 update_btime(handle, smb_fname);
3478 return rc;
3481 if (is_afpinfo_stream(smb_fname->stream_name)) {
3482 rc = fruit_stat_meta(handle, smb_fname, false);
3483 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3484 rc = fruit_stat_rsrc(handle, smb_fname, false);
3485 } else {
3486 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3489 if (rc == 0) {
3490 update_btime(handle, smb_fname);
3491 smb_fname->st.st_ex_mode &= ~S_IFMT;
3492 smb_fname->st.st_ex_mode |= S_IFREG;
3493 smb_fname->st.st_ex_blocks =
3494 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3496 return rc;
3499 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3500 files_struct *fsp,
3501 SMB_STRUCT_STAT *sbuf)
3503 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3504 struct smb_filename smb_fname;
3505 ino_t ino;
3506 int ret;
3508 if (fio == NULL) {
3509 return -1;
3512 if (fio->fake_fd) {
3513 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3514 if (ret != 0) {
3515 return -1;
3518 *sbuf = fsp->base_fsp->fsp_name->st;
3519 sbuf->st_ex_size = AFP_INFO_SIZE;
3520 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3521 return 0;
3524 smb_fname = (struct smb_filename) {
3525 .base_name = fsp->fsp_name->base_name,
3526 .twrp = fsp->fsp_name->twrp,
3529 ret = fruit_stat_base(handle, &smb_fname, false);
3530 if (ret != 0) {
3531 return -1;
3533 *sbuf = smb_fname.st;
3535 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3537 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3538 if (ret != 0) {
3539 return -1;
3542 sbuf->st_ex_ino = ino;
3543 return 0;
3546 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3547 files_struct *fsp,
3548 SMB_STRUCT_STAT *sbuf)
3550 int ret;
3552 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3553 if (ret != 0) {
3554 return -1;
3557 *sbuf = fsp->base_fsp->fsp_name->st;
3558 sbuf->st_ex_size = AFP_INFO_SIZE;
3559 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3561 return 0;
3564 static int fruit_fstat_meta(vfs_handle_struct *handle,
3565 files_struct *fsp,
3566 SMB_STRUCT_STAT *sbuf,
3567 struct fio *fio)
3569 int ret;
3571 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3573 switch (fio->config->meta) {
3574 case FRUIT_META_STREAM:
3575 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3576 break;
3578 case FRUIT_META_NETATALK:
3579 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3580 break;
3582 default:
3583 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3584 return -1;
3587 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3588 return ret;
3591 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3592 files_struct *fsp,
3593 SMB_STRUCT_STAT *sbuf)
3595 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3598 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3599 files_struct *fsp,
3600 SMB_STRUCT_STAT *sbuf)
3602 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3605 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3606 files_struct *fsp,
3607 SMB_STRUCT_STAT *sbuf)
3609 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3610 struct adouble *ad = NULL;
3611 int ret;
3613 if (fio == NULL || fio->ad_fsp == NULL) {
3614 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3615 errno = EBADF;
3616 return -1;
3619 /* Populate the stat struct with info from the base file. */
3620 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3621 if (ret == -1) {
3622 return -1;
3625 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3626 if (ad == NULL) {
3627 DBG_ERR("ad_fget [%s] failed [%s]\n",
3628 fsp_str_dbg(fio->ad_fsp), strerror(errno));
3629 return -1;
3632 *sbuf = fsp->base_fsp->fsp_name->st;
3633 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3634 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3636 TALLOC_FREE(ad);
3637 return 0;
3640 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3641 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3643 int ret;
3645 switch (fio->config->rsrc) {
3646 case FRUIT_RSRC_STREAM:
3647 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3648 break;
3650 case FRUIT_RSRC_ADFILE:
3651 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3652 break;
3654 case FRUIT_RSRC_XATTR:
3655 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3656 break;
3658 default:
3659 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3660 return -1;
3663 return ret;
3666 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3667 SMB_STRUCT_STAT *sbuf)
3669 struct fio *fio = fruit_get_complete_fio(handle, fsp);
3670 int rc;
3672 if (fio == NULL) {
3673 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3676 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3678 if (fio->type == ADOUBLE_META) {
3679 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3680 } else {
3681 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3684 if (rc == 0) {
3685 sbuf->st_ex_mode &= ~S_IFMT;
3686 sbuf->st_ex_mode |= S_IFREG;
3687 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3690 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3691 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3692 return rc;
3695 static NTSTATUS delete_invalid_meta_stream(
3696 vfs_handle_struct *handle,
3697 const struct smb_filename *smb_fname,
3698 TALLOC_CTX *mem_ctx,
3699 unsigned int *pnum_streams,
3700 struct stream_struct **pstreams,
3701 off_t size)
3703 struct smb_filename *sname = NULL;
3704 NTSTATUS status;
3705 int ret;
3706 bool ok;
3708 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3709 if (!ok) {
3710 return NT_STATUS_INTERNAL_ERROR;
3713 if (size == 0) {
3714 return NT_STATUS_OK;
3717 status = synthetic_pathref(talloc_tos(),
3718 handle->conn->cwd_fsp,
3719 smb_fname->base_name,
3720 AFPINFO_STREAM_NAME,
3721 NULL,
3722 smb_fname->twrp,
3724 &sname);
3725 if (!NT_STATUS_IS_OK(status)) {
3726 return NT_STATUS_NO_MEMORY;
3729 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3730 handle->conn->cwd_fsp,
3731 sname,
3733 if (ret != 0) {
3734 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3735 TALLOC_FREE(sname);
3736 return map_nt_error_from_unix(errno);
3739 TALLOC_FREE(sname);
3740 return NT_STATUS_OK;
3743 static NTSTATUS fruit_streaminfo_meta_stream(
3744 vfs_handle_struct *handle,
3745 struct files_struct *fsp,
3746 const struct smb_filename *smb_fname,
3747 TALLOC_CTX *mem_ctx,
3748 unsigned int *pnum_streams,
3749 struct stream_struct **pstreams)
3751 struct stream_struct *stream = *pstreams;
3752 unsigned int num_streams = *pnum_streams;
3753 int i;
3755 for (i = 0; i < num_streams; i++) {
3756 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3757 break;
3761 if (i == num_streams) {
3762 return NT_STATUS_OK;
3765 if (stream[i].size != AFP_INFO_SIZE) {
3766 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3767 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3769 return delete_invalid_meta_stream(handle,
3770 smb_fname,
3771 mem_ctx,
3772 pnum_streams,
3773 pstreams,
3774 stream[i].size);
3778 return NT_STATUS_OK;
3781 static NTSTATUS fruit_streaminfo_meta_netatalk(
3782 vfs_handle_struct *handle,
3783 struct files_struct *fsp,
3784 const struct smb_filename *smb_fname,
3785 TALLOC_CTX *mem_ctx,
3786 unsigned int *pnum_streams,
3787 struct stream_struct **pstreams)
3789 struct stream_struct *stream = *pstreams;
3790 unsigned int num_streams = *pnum_streams;
3791 struct adouble *ad = NULL;
3792 bool is_fi_empty;
3793 int i;
3794 bool ok;
3796 /* Remove the Netatalk xattr from the list */
3797 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3798 ":" NETATALK_META_XATTR ":$DATA");
3799 if (!ok) {
3800 return NT_STATUS_NO_MEMORY;
3804 * Check if there's a AFPINFO_STREAM from the VFS streams
3805 * backend and if yes, remove it from the list
3807 for (i = 0; i < num_streams; i++) {
3808 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3809 break;
3813 if (i < num_streams) {
3814 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3815 smb_fname_str_dbg(smb_fname));
3817 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3818 AFPINFO_STREAM);
3819 if (!ok) {
3820 return NT_STATUS_INTERNAL_ERROR;
3824 ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3825 if (ad == NULL) {
3826 return NT_STATUS_OK;
3829 is_fi_empty = ad_empty_finderinfo(ad);
3830 TALLOC_FREE(ad);
3832 if (is_fi_empty) {
3833 return NT_STATUS_OK;
3836 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3837 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3838 smb_roundup(handle->conn, AFP_INFO_SIZE));
3839 if (!ok) {
3840 return NT_STATUS_NO_MEMORY;
3843 return NT_STATUS_OK;
3846 static NTSTATUS fruit_streaminfo_meta(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 struct fruit_config_data *config = NULL;
3854 NTSTATUS status;
3856 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3857 return NT_STATUS_INTERNAL_ERROR);
3859 switch (config->meta) {
3860 case FRUIT_META_NETATALK:
3861 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3862 mem_ctx, pnum_streams,
3863 pstreams);
3864 break;
3866 case FRUIT_META_STREAM:
3867 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3868 mem_ctx, pnum_streams,
3869 pstreams);
3870 break;
3872 default:
3873 return NT_STATUS_INTERNAL_ERROR;
3876 return status;
3879 static NTSTATUS fruit_streaminfo_rsrc_stream(
3880 vfs_handle_struct *handle,
3881 struct files_struct *fsp,
3882 const struct smb_filename *smb_fname,
3883 TALLOC_CTX *mem_ctx,
3884 unsigned int *pnum_streams,
3885 struct stream_struct **pstreams)
3887 bool ok;
3889 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3890 if (!ok) {
3891 DBG_ERR("Filtering resource stream failed\n");
3892 return NT_STATUS_INTERNAL_ERROR;
3894 return NT_STATUS_OK;
3897 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3898 vfs_handle_struct *handle,
3899 struct files_struct *fsp,
3900 const struct smb_filename *smb_fname,
3901 TALLOC_CTX *mem_ctx,
3902 unsigned int *pnum_streams,
3903 struct stream_struct **pstreams)
3905 bool ok;
3907 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3908 if (!ok) {
3909 DBG_ERR("Filtering resource stream failed\n");
3910 return NT_STATUS_INTERNAL_ERROR;
3912 return NT_STATUS_OK;
3915 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3916 vfs_handle_struct *handle,
3917 struct files_struct *fsp,
3918 const struct smb_filename *smb_fname,
3919 TALLOC_CTX *mem_ctx,
3920 unsigned int *pnum_streams,
3921 struct stream_struct **pstreams)
3923 struct stream_struct *stream = *pstreams;
3924 unsigned int num_streams = *pnum_streams;
3925 struct adouble *ad = NULL;
3926 bool ok;
3927 size_t rlen;
3928 int i;
3931 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3932 * and if yes, remove it from the list
3934 for (i = 0; i < num_streams; i++) {
3935 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3936 break;
3940 if (i < num_streams) {
3941 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3942 smb_fname_str_dbg(smb_fname));
3944 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3945 AFPRESOURCE_STREAM);
3946 if (!ok) {
3947 return NT_STATUS_INTERNAL_ERROR;
3951 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3952 if (ad == NULL) {
3953 return NT_STATUS_OK;
3956 rlen = ad_getentrylen(ad, ADEID_RFORK);
3957 TALLOC_FREE(ad);
3959 if (rlen == 0) {
3960 return NT_STATUS_OK;
3963 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3964 AFPRESOURCE_STREAM_NAME, rlen,
3965 smb_roundup(handle->conn, rlen));
3966 if (!ok) {
3967 return NT_STATUS_NO_MEMORY;
3970 return NT_STATUS_OK;
3973 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3974 struct files_struct *fsp,
3975 const struct smb_filename *smb_fname,
3976 TALLOC_CTX *mem_ctx,
3977 unsigned int *pnum_streams,
3978 struct stream_struct **pstreams)
3980 struct fruit_config_data *config = NULL;
3981 NTSTATUS status;
3983 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
3984 return NT_STATUS_OK;
3987 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3988 return NT_STATUS_INTERNAL_ERROR);
3990 switch (config->rsrc) {
3991 case FRUIT_RSRC_STREAM:
3992 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3993 mem_ctx, pnum_streams,
3994 pstreams);
3995 break;
3997 case FRUIT_RSRC_XATTR:
3998 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3999 mem_ctx, pnum_streams,
4000 pstreams);
4001 break;
4003 case FRUIT_RSRC_ADFILE:
4004 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4005 mem_ctx, pnum_streams,
4006 pstreams);
4007 break;
4009 default:
4010 return NT_STATUS_INTERNAL_ERROR;
4013 return status;
4016 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
4017 struct stream_struct **pstreams)
4019 unsigned num_streams = *pnum_streams;
4020 struct stream_struct *streams = *pstreams;
4021 unsigned i = 0;
4023 if (!global_fruit_config.nego_aapl) {
4024 return;
4027 while (i < num_streams) {
4028 struct smb_filename smb_fname = (struct smb_filename) {
4029 .stream_name = streams[i].name,
4032 if (is_ntfs_default_stream_smb_fname(&smb_fname)
4033 || streams[i].size > 0)
4035 i++;
4036 continue;
4039 streams[i] = streams[num_streams - 1];
4040 num_streams--;
4043 *pnum_streams = num_streams;
4046 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
4047 struct files_struct *fsp,
4048 TALLOC_CTX *mem_ctx,
4049 unsigned int *pnum_streams,
4050 struct stream_struct **pstreams)
4052 struct fruit_config_data *config = NULL;
4053 const struct smb_filename *smb_fname = NULL;
4054 NTSTATUS status;
4056 smb_fname = fsp->fsp_name;
4058 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4059 return NT_STATUS_UNSUCCESSFUL);
4061 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4063 status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
4064 pnum_streams, pstreams);
4065 if (!NT_STATUS_IS_OK(status)) {
4066 return status;
4069 fruit_filter_empty_streams(pnum_streams, pstreams);
4071 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4072 mem_ctx, pnum_streams, pstreams);
4073 if (!NT_STATUS_IS_OK(status)) {
4074 return status;
4077 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4078 mem_ctx, pnum_streams, pstreams);
4079 if (!NT_STATUS_IS_OK(status)) {
4080 return status;
4083 return NT_STATUS_OK;
4086 static int fruit_fntimes(vfs_handle_struct *handle,
4087 files_struct *fsp,
4088 struct smb_file_time *ft)
4090 int rc = 0;
4091 struct adouble *ad = NULL;
4092 struct fruit_config_data *config = NULL;
4094 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4095 return -1);
4097 if ((config->meta != FRUIT_META_NETATALK) ||
4098 is_omit_timespec(&ft->create_time))
4100 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4103 DBG_DEBUG("set btime for %s to %s", fsp_str_dbg(fsp),
4104 time_to_asc(convert_timespec_to_time_t(ft->create_time)));
4106 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4107 if (ad == NULL) {
4108 goto exit;
4111 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4112 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4114 rc = ad_fset(handle, ad, fsp);
4116 exit:
4118 TALLOC_FREE(ad);
4119 if (rc != 0) {
4120 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
4121 return -1;
4123 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
4126 static int fruit_fallocate(struct vfs_handle_struct *handle,
4127 struct files_struct *fsp,
4128 uint32_t mode,
4129 off_t offset,
4130 off_t len)
4132 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4134 if (fio == NULL) {
4135 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4138 /* Let the pwrite code path handle it. */
4139 errno = ENOSYS;
4140 return -1;
4143 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4144 struct files_struct *fsp,
4145 off_t offset)
4147 #ifdef HAVE_ATTROPEN
4148 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4149 #endif
4150 return 0;
4153 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4154 struct files_struct *fsp,
4155 off_t offset)
4157 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4158 int rc;
4159 struct adouble *ad = NULL;
4160 off_t ad_off;
4162 if (fio == NULL || fio->ad_fsp == NULL) {
4163 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4164 errno = EBADF;
4165 return -1;
4168 ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4169 if (ad == NULL) {
4170 DBG_ERR("ad_fget [%s] failed [%s]\n",
4171 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4172 return -1;
4175 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4177 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4178 if (rc != 0) {
4179 TALLOC_FREE(ad);
4180 return -1;
4183 ad_setentrylen(ad, ADEID_RFORK, offset);
4185 rc = ad_fset(handle, ad, fio->ad_fsp);
4186 if (rc != 0) {
4187 DBG_ERR("ad_fset [%s] failed [%s]\n",
4188 fsp_str_dbg(fio->ad_fsp), strerror(errno));
4189 TALLOC_FREE(ad);
4190 return -1;
4193 TALLOC_FREE(ad);
4194 return 0;
4197 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4198 struct files_struct *fsp,
4199 off_t offset)
4201 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4204 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4205 struct files_struct *fsp,
4206 off_t offset)
4208 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4209 int ret;
4211 if (fio == NULL) {
4212 DBG_ERR("Failed to fetch fsp extension\n");
4213 return -1;
4216 switch (fio->config->rsrc) {
4217 case FRUIT_RSRC_XATTR:
4218 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4219 break;
4221 case FRUIT_RSRC_ADFILE:
4222 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4223 break;
4225 case FRUIT_RSRC_STREAM:
4226 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4227 break;
4229 default:
4230 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4231 return -1;
4235 return ret;
4238 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4239 struct files_struct *fsp,
4240 off_t offset)
4242 if (offset > 60) {
4243 DBG_WARNING("ftruncate %s to %jd\n",
4244 fsp_str_dbg(fsp), (intmax_t)offset);
4245 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4246 errno = EOVERFLOW;
4247 return -1;
4250 /* OS X returns success but does nothing */
4251 DBG_INFO("ignoring ftruncate %s to %jd\n",
4252 fsp_str_dbg(fsp), (intmax_t)offset);
4253 return 0;
4256 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4257 struct files_struct *fsp,
4258 off_t offset)
4260 struct fio *fio = fruit_get_complete_fio(handle, fsp);
4261 int ret;
4263 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4264 (intmax_t)offset);
4266 if (fio == NULL) {
4267 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4270 if (fio->type == ADOUBLE_META) {
4271 ret = fruit_ftruncate_meta(handle, fsp, offset);
4272 } else {
4273 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4276 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4277 return ret;
4280 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4281 struct smb_request *req,
4282 struct files_struct *dirfsp,
4283 struct smb_filename *smb_fname,
4284 uint32_t access_mask,
4285 uint32_t share_access,
4286 uint32_t create_disposition,
4287 uint32_t create_options,
4288 uint32_t file_attributes,
4289 uint32_t oplock_request,
4290 const struct smb2_lease *lease,
4291 uint64_t allocation_size,
4292 uint32_t private_flags,
4293 struct security_descriptor *sd,
4294 struct ea_list *ea_list,
4295 files_struct **result,
4296 int *pinfo,
4297 const struct smb2_create_blobs *in_context_blobs,
4298 struct smb2_create_blobs *out_context_blobs)
4300 NTSTATUS status;
4301 struct fruit_config_data *config = NULL;
4302 files_struct *fsp = NULL;
4303 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4304 int ret;
4306 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4307 if (!NT_STATUS_IS_OK(status)) {
4308 goto fail;
4311 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4312 return NT_STATUS_UNSUCCESSFUL);
4314 if (is_apple_stream(smb_fname->stream_name) &&
4315 !internal_open &&
4316 config->convert_adouble)
4318 uint32_t conv_flags = 0;
4320 if (config->wipe_intentionally_left_blank_rfork) {
4321 conv_flags |= AD_CONV_WIPE_BLANK;
4323 if (config->delete_empty_adfiles) {
4324 conv_flags |= AD_CONV_DELETE;
4327 ret = ad_convert(handle,
4328 smb_fname,
4329 macos_string_replace_map,
4330 conv_flags);
4331 if (ret != 0) {
4332 DBG_ERR("ad_convert(\"%s\") failed\n",
4333 smb_fname_str_dbg(smb_fname));
4337 status = SMB_VFS_NEXT_CREATE_FILE(
4338 handle, req, dirfsp, smb_fname,
4339 access_mask, share_access,
4340 create_disposition, create_options,
4341 file_attributes, oplock_request,
4342 lease,
4343 allocation_size, private_flags,
4344 sd, ea_list, result,
4345 pinfo, in_context_blobs, out_context_blobs);
4346 if (!NT_STATUS_IS_OK(status)) {
4347 return status;
4350 fsp = *result;
4352 if (global_fruit_config.nego_aapl) {
4353 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4355 * Enable POSIX directory rename behaviour
4357 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4362 * If this is a plain open for existing files, opening an 0
4363 * byte size resource fork MUST fail with
4364 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4366 * Cf the vfs_fruit torture tests in test_rfork_create().
4368 if (global_fruit_config.nego_aapl &&
4369 create_disposition == FILE_OPEN &&
4370 smb_fname->st.st_ex_size == 0 &&
4371 is_named_stream(smb_fname))
4373 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4374 goto fail;
4377 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4378 return status;
4381 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4382 (fsp->op != NULL) &&
4383 !fsp->fsp_flags.is_pathref)
4385 status = fruit_check_access(
4386 handle, *result,
4387 access_mask,
4388 share_access);
4389 if (!NT_STATUS_IS_OK(status)) {
4390 goto fail;
4394 return status;
4396 fail:
4397 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4399 if (fsp) {
4400 close_file_free(req, &fsp, ERROR_CLOSE);
4401 *result = NULL;
4404 return status;
4407 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4408 struct files_struct *fsp,
4409 TALLOC_CTX *mem_ctx,
4410 struct readdir_attr_data **pattr_data)
4412 struct fruit_config_data *config = NULL;
4413 struct readdir_attr_data *attr_data;
4414 uint32_t conv_flags = 0;
4415 NTSTATUS status;
4416 int ret;
4418 SMB_VFS_HANDLE_GET_DATA(handle, config,
4419 struct fruit_config_data,
4420 return NT_STATUS_UNSUCCESSFUL);
4422 if (!global_fruit_config.nego_aapl) {
4423 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4424 fsp,
4425 mem_ctx,
4426 pattr_data);
4429 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4431 if (config->convert_adouble) {
4432 if (config->wipe_intentionally_left_blank_rfork) {
4433 conv_flags |= AD_CONV_WIPE_BLANK;
4435 if (config->delete_empty_adfiles) {
4436 conv_flags |= AD_CONV_DELETE;
4439 ret = ad_convert(handle,
4440 fsp->fsp_name,
4441 macos_string_replace_map,
4442 conv_flags);
4443 if (ret != 0) {
4444 DBG_ERR("ad_convert(\"%s\") failed\n",
4445 fsp_str_dbg(fsp));
4449 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4450 if (*pattr_data == NULL) {
4451 return NT_STATUS_NO_MEMORY;
4453 attr_data = *pattr_data;
4454 attr_data->type = RDATTR_AAPL;
4457 * Mac metadata: compressed FinderInfo, resource fork length
4458 * and creation date
4460 status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4461 if (!NT_STATUS_IS_OK(status)) {
4463 * Error handling is tricky: if we return failure from
4464 * this function, the corresponding directory entry
4465 * will to be passed to the client, so we really just
4466 * want to error out on fatal errors.
4468 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4469 goto fail;
4474 * UNIX mode
4476 if (config->unix_info_enabled) {
4477 attr_data->attr_data.aapl.unix_mode =
4478 fsp->fsp_name->st.st_ex_mode;
4482 * max_access
4484 if (!config->readdir_attr_max_access) {
4485 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4486 } else {
4487 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4488 fsp,
4489 false,
4490 SEC_FLAG_MAXIMUM_ALLOWED,
4491 &attr_data->attr_data.aapl.max_access);
4492 if (!NT_STATUS_IS_OK(status)) {
4493 goto fail;
4497 return NT_STATUS_OK;
4499 fail:
4500 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4501 nt_errstr(status));
4502 TALLOC_FREE(*pattr_data);
4503 return status;
4506 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4507 files_struct *fsp,
4508 uint32_t security_info,
4509 TALLOC_CTX *mem_ctx,
4510 struct security_descriptor **ppdesc)
4512 NTSTATUS status;
4513 struct security_ace ace;
4514 struct dom_sid sid;
4515 struct fruit_config_data *config;
4517 SMB_VFS_HANDLE_GET_DATA(handle, config,
4518 struct fruit_config_data,
4519 return NT_STATUS_UNSUCCESSFUL);
4521 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4522 mem_ctx, ppdesc);
4523 if (!NT_STATUS_IS_OK(status)) {
4524 return status;
4528 * Add MS NFS style ACEs with uid, gid and mode
4530 if (!global_fruit_config.nego_aapl) {
4531 return NT_STATUS_OK;
4533 if (!config->unix_info_enabled) {
4534 return NT_STATUS_OK;
4537 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4538 status = remove_virtual_nfs_aces(*ppdesc);
4539 if (!NT_STATUS_IS_OK(status)) {
4540 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4541 return status;
4544 /* MS NFS style mode */
4545 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4546 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4547 status = security_descriptor_dacl_add(*ppdesc, &ace);
4548 if (!NT_STATUS_IS_OK(status)) {
4549 DEBUG(1,("failed to add MS NFS style ACE\n"));
4550 return status;
4553 /* MS NFS style uid */
4554 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4555 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4556 status = security_descriptor_dacl_add(*ppdesc, &ace);
4557 if (!NT_STATUS_IS_OK(status)) {
4558 DEBUG(1,("failed to add MS NFS style ACE\n"));
4559 return status;
4562 /* MS NFS style gid */
4563 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4564 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4565 status = security_descriptor_dacl_add(*ppdesc, &ace);
4566 if (!NT_STATUS_IS_OK(status)) {
4567 DEBUG(1,("failed to add MS NFS style ACE\n"));
4568 return status;
4571 return NT_STATUS_OK;
4574 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4575 files_struct *fsp,
4576 uint32_t security_info_sent,
4577 const struct security_descriptor *orig_psd)
4579 NTSTATUS status;
4580 bool do_chmod;
4581 mode_t ms_nfs_mode = 0;
4582 int result;
4583 struct security_descriptor *psd = NULL;
4584 uint32_t orig_num_aces = 0;
4586 if (orig_psd->dacl != NULL) {
4587 orig_num_aces = orig_psd->dacl->num_aces;
4590 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4591 if (psd == NULL) {
4592 return NT_STATUS_NO_MEMORY;
4595 DBG_DEBUG("%s\n", fsp_str_dbg(fsp));
4597 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4598 if (!NT_STATUS_IS_OK(status)) {
4599 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4600 TALLOC_FREE(psd);
4601 return status;
4605 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4606 * sent/present flags correctly now we've removed them.
4609 if (orig_num_aces != 0) {
4611 * Are there any ACE's left ?
4613 if (psd->dacl->num_aces == 0) {
4614 /* No - clear the DACL sent/present flags. */
4615 security_info_sent &= ~SECINFO_DACL;
4616 psd->type &= ~SEC_DESC_DACL_PRESENT;
4620 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4621 if (!NT_STATUS_IS_OK(status)) {
4622 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4623 TALLOC_FREE(psd);
4624 return status;
4627 if (do_chmod) {
4628 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4629 if (result != 0) {
4630 DBG_WARNING("%s, result: %d, %04o error %s\n",
4631 fsp_str_dbg(fsp),
4632 result,
4633 (unsigned)ms_nfs_mode,
4634 strerror(errno));
4635 status = map_nt_error_from_unix(errno);
4636 TALLOC_FREE(psd);
4637 return status;
4641 TALLOC_FREE(psd);
4642 return NT_STATUS_OK;
4645 static struct vfs_offload_ctx *fruit_offload_ctx;
4647 struct fruit_offload_read_state {
4648 struct vfs_handle_struct *handle;
4649 struct tevent_context *ev;
4650 files_struct *fsp;
4651 uint32_t fsctl;
4652 uint32_t flags;
4653 uint64_t xferlen;
4654 DATA_BLOB token;
4657 static void fruit_offload_read_done(struct tevent_req *subreq);
4659 static struct tevent_req *fruit_offload_read_send(
4660 TALLOC_CTX *mem_ctx,
4661 struct tevent_context *ev,
4662 struct vfs_handle_struct *handle,
4663 files_struct *fsp,
4664 uint32_t fsctl,
4665 uint32_t ttl,
4666 off_t offset,
4667 size_t to_copy)
4669 struct tevent_req *req = NULL;
4670 struct tevent_req *subreq = NULL;
4671 struct fruit_offload_read_state *state = NULL;
4673 req = tevent_req_create(mem_ctx, &state,
4674 struct fruit_offload_read_state);
4675 if (req == NULL) {
4676 return NULL;
4678 *state = (struct fruit_offload_read_state) {
4679 .handle = handle,
4680 .ev = ev,
4681 .fsp = fsp,
4682 .fsctl = fsctl,
4685 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4686 fsctl, ttl, offset, to_copy);
4687 if (tevent_req_nomem(subreq, req)) {
4688 return tevent_req_post(req, ev);
4690 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4691 return req;
4694 static void fruit_offload_read_done(struct tevent_req *subreq)
4696 struct tevent_req *req = tevent_req_callback_data(
4697 subreq, struct tevent_req);
4698 struct fruit_offload_read_state *state = tevent_req_data(
4699 req, struct fruit_offload_read_state);
4700 NTSTATUS status;
4702 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4703 state->handle,
4704 state,
4705 &state->flags,
4706 &state->xferlen,
4707 &state->token);
4708 TALLOC_FREE(subreq);
4709 if (tevent_req_nterror(req, status)) {
4710 return;
4713 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4714 tevent_req_done(req);
4715 return;
4718 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4719 &fruit_offload_ctx);
4720 if (tevent_req_nterror(req, status)) {
4721 return;
4724 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4725 state->fsp,
4726 &state->token);
4727 if (tevent_req_nterror(req, status)) {
4728 return;
4731 tevent_req_done(req);
4732 return;
4735 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4736 struct vfs_handle_struct *handle,
4737 TALLOC_CTX *mem_ctx,
4738 uint32_t *flags,
4739 uint64_t *xferlen,
4740 DATA_BLOB *token)
4742 struct fruit_offload_read_state *state = tevent_req_data(
4743 req, struct fruit_offload_read_state);
4744 NTSTATUS status;
4746 if (tevent_req_is_nterror(req, &status)) {
4747 tevent_req_received(req);
4748 return status;
4751 *flags = state->flags;
4752 *xferlen = state->xferlen;
4753 token->length = state->token.length;
4754 token->data = talloc_move(mem_ctx, &state->token.data);
4756 tevent_req_received(req);
4757 return NT_STATUS_OK;
4760 struct fruit_offload_write_state {
4761 struct vfs_handle_struct *handle;
4762 off_t copied;
4763 struct files_struct *src_fsp;
4764 struct files_struct *dst_fsp;
4765 bool is_copyfile;
4768 static void fruit_offload_write_done(struct tevent_req *subreq);
4769 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4770 TALLOC_CTX *mem_ctx,
4771 struct tevent_context *ev,
4772 uint32_t fsctl,
4773 DATA_BLOB *token,
4774 off_t transfer_offset,
4775 struct files_struct *dest_fsp,
4776 off_t dest_off,
4777 off_t num)
4779 struct tevent_req *req, *subreq;
4780 struct fruit_offload_write_state *state;
4781 NTSTATUS status;
4782 struct fruit_config_data *config;
4783 off_t src_off = transfer_offset;
4784 files_struct *src_fsp = NULL;
4785 off_t to_copy = num;
4786 bool copyfile_enabled = false;
4788 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4789 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4791 SMB_VFS_HANDLE_GET_DATA(handle, config,
4792 struct fruit_config_data,
4793 return NULL);
4795 req = tevent_req_create(mem_ctx, &state,
4796 struct fruit_offload_write_state);
4797 if (req == NULL) {
4798 return NULL;
4800 state->handle = handle;
4801 state->dst_fsp = dest_fsp;
4803 switch (fsctl) {
4804 case FSCTL_SRV_COPYCHUNK:
4805 case FSCTL_SRV_COPYCHUNK_WRITE:
4806 copyfile_enabled = config->copyfile_enabled;
4807 break;
4808 default:
4809 break;
4813 * Check if this a OS X copyfile style copychunk request with
4814 * a requested chunk count of 0 that was translated to a
4815 * offload_write_send VFS call overloading the parameters src_off
4816 * = dest_off = num = 0.
4818 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4819 status = vfs_offload_token_db_fetch_fsp(
4820 fruit_offload_ctx, token, &src_fsp);
4821 if (tevent_req_nterror(req, status)) {
4822 return tevent_req_post(req, ev);
4824 state->src_fsp = src_fsp;
4826 status = vfs_stat_fsp(src_fsp);
4827 if (tevent_req_nterror(req, status)) {
4828 return tevent_req_post(req, ev);
4831 to_copy = src_fsp->fsp_name->st.st_ex_size;
4832 state->is_copyfile = true;
4835 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4836 mem_ctx,
4838 fsctl,
4839 token,
4840 transfer_offset,
4841 dest_fsp,
4842 dest_off,
4843 to_copy);
4844 if (tevent_req_nomem(subreq, req)) {
4845 return tevent_req_post(req, ev);
4848 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4849 return req;
4852 static void fruit_offload_write_done(struct tevent_req *subreq)
4854 struct tevent_req *req = tevent_req_callback_data(
4855 subreq, struct tevent_req);
4856 struct fruit_offload_write_state *state = tevent_req_data(
4857 req, struct fruit_offload_write_state);
4858 NTSTATUS status;
4859 unsigned int num_streams = 0;
4860 struct stream_struct *streams = NULL;
4861 unsigned int i;
4862 struct smb_filename *src_fname_tmp = NULL;
4863 struct smb_filename *dst_fname_tmp = NULL;
4865 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4866 subreq,
4867 &state->copied);
4868 TALLOC_FREE(subreq);
4869 if (tevent_req_nterror(req, status)) {
4870 return;
4873 if (!state->is_copyfile) {
4874 tevent_req_done(req);
4875 return;
4879 * Now copy all remaining streams. We know the share supports
4880 * streams, because we're in vfs_fruit. We don't do this async
4881 * because streams are few and small.
4883 status = vfs_fstreaminfo(state->src_fsp,
4884 req, &num_streams, &streams);
4885 if (tevent_req_nterror(req, status)) {
4886 return;
4889 if (num_streams == 1) {
4890 /* There is always one stream, ::$DATA. */
4891 tevent_req_done(req);
4892 return;
4895 for (i = 0; i < num_streams; i++) {
4896 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4897 __func__, streams[i].name, (size_t)streams[i].size));
4899 src_fname_tmp = synthetic_smb_fname(
4900 req,
4901 state->src_fsp->fsp_name->base_name,
4902 streams[i].name,
4903 NULL,
4904 state->src_fsp->fsp_name->twrp,
4905 state->src_fsp->fsp_name->flags);
4906 if (tevent_req_nomem(src_fname_tmp, req)) {
4907 return;
4910 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4911 TALLOC_FREE(src_fname_tmp);
4912 continue;
4915 dst_fname_tmp = synthetic_smb_fname(
4916 req,
4917 state->dst_fsp->fsp_name->base_name,
4918 streams[i].name,
4919 NULL,
4920 state->dst_fsp->fsp_name->twrp,
4921 state->dst_fsp->fsp_name->flags);
4922 if (tevent_req_nomem(dst_fname_tmp, req)) {
4923 TALLOC_FREE(src_fname_tmp);
4924 return;
4927 status = copy_file(req,
4928 state->handle->conn,
4929 src_fname_tmp,
4930 dst_fname_tmp,
4931 FILE_CREATE);
4932 if (!NT_STATUS_IS_OK(status)) {
4933 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4934 smb_fname_str_dbg(src_fname_tmp),
4935 smb_fname_str_dbg(dst_fname_tmp),
4936 nt_errstr(status)));
4937 TALLOC_FREE(src_fname_tmp);
4938 TALLOC_FREE(dst_fname_tmp);
4939 tevent_req_nterror(req, status);
4940 return;
4943 TALLOC_FREE(src_fname_tmp);
4944 TALLOC_FREE(dst_fname_tmp);
4947 TALLOC_FREE(streams);
4948 TALLOC_FREE(src_fname_tmp);
4949 TALLOC_FREE(dst_fname_tmp);
4950 tevent_req_done(req);
4953 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4954 struct tevent_req *req,
4955 off_t *copied)
4957 struct fruit_offload_write_state *state = tevent_req_data(
4958 req, struct fruit_offload_write_state);
4959 NTSTATUS status;
4961 if (tevent_req_is_nterror(req, &status)) {
4962 DEBUG(1, ("server side copy chunk failed: %s\n",
4963 nt_errstr(status)));
4964 *copied = 0;
4965 tevent_req_received(req);
4966 return status;
4969 *copied = state->copied;
4970 tevent_req_received(req);
4972 return NT_STATUS_OK;
4975 static char *fruit_get_bandsize_line(char **lines, int numlines)
4977 static regex_t re;
4978 static bool re_initialized = false;
4979 int i;
4980 int ret;
4982 if (!re_initialized) {
4983 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4984 if (ret != 0) {
4985 return NULL;
4987 re_initialized = true;
4990 for (i = 0; i < numlines; i++) {
4991 regmatch_t matches[1];
4993 ret = regexec(&re, lines[i], 1, matches, 0);
4994 if (ret == 0) {
4996 * Check if the match was on the last line, sa we want
4997 * the subsequent line.
4999 if (i + 1 == numlines) {
5000 return NULL;
5002 return lines[i + 1];
5004 if (ret != REG_NOMATCH) {
5005 return NULL;
5009 return NULL;
5012 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
5014 static regex_t re;
5015 static bool re_initialized = false;
5016 regmatch_t matches[2];
5017 uint64_t band_size;
5018 int ret;
5019 bool ok;
5021 if (!re_initialized) {
5022 ret = regcomp(&re,
5023 "^[[:blank:]]*"
5024 "<integer>\\([[:digit:]]*\\)</integer>$",
5026 if (ret != 0) {
5027 return false;
5029 re_initialized = true;
5032 ret = regexec(&re, line, 2, matches, 0);
5033 if (ret != 0) {
5034 DBG_ERR("regex failed [%s]\n", line);
5035 return false;
5038 line[matches[1].rm_eo] = '\0';
5040 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
5041 if (!ok) {
5042 return false;
5044 *_band_size = (size_t)band_size;
5045 return true;
5049 * This reads and parses an Info.plist from a TM sparsebundle looking for the
5050 * "band-size" key and value.
5052 static bool fruit_get_bandsize(vfs_handle_struct *handle,
5053 const char *dir,
5054 size_t *band_size)
5056 #define INFO_PLIST_MAX_SIZE 64*1024
5057 char *plist = NULL;
5058 struct smb_filename *smb_fname = NULL;
5059 files_struct *fsp = NULL;
5060 uint8_t *file_data = NULL;
5061 char **lines = NULL;
5062 char *band_size_line = NULL;
5063 size_t plist_file_size;
5064 ssize_t nread;
5065 int numlines;
5066 int ret;
5067 bool ok = false;
5068 NTSTATUS status;
5070 plist = talloc_asprintf(talloc_tos(),
5071 "%s/%s/Info.plist",
5072 handle->conn->connectpath,
5073 dir);
5074 if (plist == NULL) {
5075 ok = false;
5076 goto out;
5079 smb_fname = synthetic_smb_fname(talloc_tos(),
5080 plist,
5081 NULL,
5082 NULL,
5085 if (smb_fname == NULL) {
5086 ok = false;
5087 goto out;
5090 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5091 if (ret != 0) {
5092 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
5093 ok = true;
5094 goto out;
5097 plist_file_size = smb_fname->st.st_ex_size;
5099 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
5100 DBG_INFO("%s is too large, ignoring\n", plist);
5101 ok = true;
5102 goto out;
5105 status = SMB_VFS_NEXT_CREATE_FILE(
5106 handle, /* conn */
5107 NULL, /* req */
5108 NULL, /* dirfsp */
5109 smb_fname, /* fname */
5110 FILE_GENERIC_READ, /* access_mask */
5111 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
5112 FILE_OPEN, /* create_disposition */
5113 0, /* create_options */
5114 0, /* file_attributes */
5115 INTERNAL_OPEN_ONLY, /* oplock_request */
5116 NULL, /* lease */
5117 0, /* allocation_size */
5118 0, /* private_flags */
5119 NULL, /* sd */
5120 NULL, /* ea_list */
5121 &fsp, /* result */
5122 NULL, /* psbuf */
5123 NULL, NULL); /* create context */
5124 if (!NT_STATUS_IS_OK(status)) {
5125 DBG_INFO("Opening [%s] failed [%s]\n",
5126 smb_fname_str_dbg(smb_fname), nt_errstr(status));
5127 ok = false;
5128 goto out;
5131 file_data = talloc_zero_array(talloc_tos(),
5132 uint8_t,
5133 plist_file_size + 1);
5134 if (file_data == NULL) {
5135 ok = false;
5136 goto out;
5139 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
5140 if (nread != plist_file_size) {
5141 DBG_ERR("Short read on [%s]: %zu/%zd\n",
5142 fsp_str_dbg(fsp), nread, plist_file_size);
5143 ok = false;
5144 goto out;
5148 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5149 if (!NT_STATUS_IS_OK(status)) {
5150 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5151 ok = false;
5152 goto out;
5155 lines = file_lines_parse((char *)file_data,
5156 plist_file_size,
5157 &numlines,
5158 talloc_tos());
5159 if (lines == NULL) {
5160 ok = false;
5161 goto out;
5164 band_size_line = fruit_get_bandsize_line(lines, numlines);
5165 if (band_size_line == NULL) {
5166 DBG_ERR("Didn't find band-size key in [%s]\n",
5167 smb_fname_str_dbg(smb_fname));
5168 ok = false;
5169 goto out;
5172 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5173 if (!ok) {
5174 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5175 goto out;
5178 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5180 out:
5181 if (fsp != NULL) {
5182 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5183 if (!NT_STATUS_IS_OK(status)) {
5184 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5187 TALLOC_FREE(plist);
5188 TALLOC_FREE(smb_fname);
5189 TALLOC_FREE(file_data);
5190 TALLOC_FREE(lines);
5191 return ok;
5194 struct fruit_disk_free_state {
5195 off_t total_size;
5198 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5199 const char *bundle,
5200 size_t *_nbands)
5202 char *path = NULL;
5203 struct smb_filename *bands_dir = NULL;
5204 struct smb_Dir *dir_hnd = NULL;
5205 const char *dname = NULL;
5206 char *talloced = NULL;
5207 size_t nbands;
5208 NTSTATUS status;
5210 path = talloc_asprintf(talloc_tos(),
5211 "%s/%s/bands",
5212 handle->conn->connectpath,
5213 bundle);
5214 if (path == NULL) {
5215 return false;
5218 bands_dir = synthetic_smb_fname(talloc_tos(),
5219 path,
5220 NULL,
5221 NULL,
5224 TALLOC_FREE(path);
5225 if (bands_dir == NULL) {
5226 return false;
5229 status = OpenDir(talloc_tos(),
5230 handle->conn,
5231 bands_dir,
5232 NULL,
5234 &dir_hnd);
5235 if (!NT_STATUS_IS_OK(status)) {
5236 TALLOC_FREE(bands_dir);
5237 errno = map_errno_from_nt_status(status);
5238 return false;
5241 nbands = 0;
5243 while ((dname = ReadDirName(dir_hnd, &talloced)) != NULL) {
5244 if (ISDOT(dname) || ISDOTDOT(dname)) {
5245 continue;
5247 nbands++;
5249 TALLOC_FREE(dir_hnd);
5251 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5253 TALLOC_FREE(bands_dir);
5255 *_nbands = nbands;
5256 return true;
5259 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5260 struct fruit_disk_free_state *state,
5261 const char *name)
5263 bool ok;
5264 char *p = NULL;
5265 size_t sparsebundle_strlen = strlen("sparsebundle");
5266 size_t bandsize = 0;
5267 size_t nbands;
5268 off_t tm_size;
5270 p = strstr(name, "sparsebundle");
5271 if (p == NULL) {
5272 return true;
5275 if (p[sparsebundle_strlen] != '\0') {
5276 return true;
5279 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5281 ok = fruit_get_bandsize(handle, name, &bandsize);
5282 if (!ok) {
5284 * Beware of race conditions: this may be an uninitialized
5285 * Info.plist that a client is just creating. We don't want let
5286 * this to trigger complete failure.
5288 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5289 return true;
5292 ok = fruit_get_num_bands(handle, name, &nbands);
5293 if (!ok) {
5295 * Beware of race conditions: this may be a backup sparsebundle
5296 * in an early stage lacking a bands subdirectory. We don't want
5297 * let this to trigger complete failure.
5299 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5300 return true;
5304 * Arithmetic on 32-bit systems may cause overflow, depending on
5305 * size_t precision. First we check its unlikely, then we
5306 * force the precision into target off_t, then we check that
5307 * the total did not overflow either.
5309 if (bandsize > SIZE_MAX/nbands) {
5310 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5311 bandsize, nbands);
5312 return false;
5314 tm_size = (off_t)bandsize * (off_t)nbands;
5316 if (state->total_size + tm_size < state->total_size) {
5317 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5318 bandsize, nbands);
5319 return false;
5322 state->total_size += tm_size;
5324 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5325 name, (intmax_t)tm_size, (intmax_t)state->total_size);
5327 return true;
5331 * Calculate used size of a TimeMachine volume
5333 * This assumes that the volume is used only for TimeMachine.
5335 * - readdir(basedir of share), then
5336 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5337 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5338 * - count band files in "\1.sparsebundle/bands/"
5339 * - calculate used size of all bands: band_count * band_size
5341 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5342 const struct smb_filename *smb_fname,
5343 uint64_t *_bsize,
5344 uint64_t *_dfree,
5345 uint64_t *_dsize)
5347 struct fruit_config_data *config = NULL;
5348 struct fruit_disk_free_state state = {0};
5349 struct smb_Dir *dir_hnd = NULL;
5350 const char *dname = NULL;
5351 char *talloced = NULL;
5352 uint64_t dfree;
5353 uint64_t dsize;
5354 bool ok;
5355 NTSTATUS status;
5357 SMB_VFS_HANDLE_GET_DATA(handle, config,
5358 struct fruit_config_data,
5359 return UINT64_MAX);
5361 if (!config->time_machine ||
5362 config->time_machine_max_size == 0)
5364 return SMB_VFS_NEXT_DISK_FREE(handle,
5365 smb_fname,
5366 _bsize,
5367 _dfree,
5368 _dsize);
5371 status = OpenDir(talloc_tos(),
5372 handle->conn,
5373 smb_fname,
5374 NULL,
5376 &dir_hnd);
5377 if (!NT_STATUS_IS_OK(status)) {
5378 errno = map_errno_from_nt_status(status);
5379 return UINT64_MAX;
5382 while ((dname = ReadDirName(dir_hnd, &talloced)) != NULL) {
5383 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5384 if (!ok) {
5385 TALLOC_FREE(talloced);
5386 TALLOC_FREE(dir_hnd);
5387 return UINT64_MAX;
5389 TALLOC_FREE(talloced);
5392 TALLOC_FREE(dir_hnd);
5394 dsize = config->time_machine_max_size / 512;
5395 dfree = dsize - (state.total_size / 512);
5396 if (dfree > dsize) {
5397 dfree = 0;
5400 *_bsize = 512;
5401 *_dsize = dsize;
5402 *_dfree = dfree;
5403 return dfree / 2;
5406 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5407 const SMB_STRUCT_STAT *psbuf)
5409 struct fruit_config_data *config = NULL;
5411 SMB_VFS_HANDLE_GET_DATA(handle, config,
5412 struct fruit_config_data,
5413 return 0);
5415 if (global_fruit_config.nego_aapl &&
5416 config->aapl_zero_file_id)
5418 return 0;
5421 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5424 static struct vfs_fn_pointers vfs_fruit_fns = {
5425 .connect_fn = fruit_connect,
5426 .disk_free_fn = fruit_disk_free,
5428 /* File operations */
5429 .fchmod_fn = fruit_fchmod,
5430 .unlinkat_fn = fruit_unlinkat,
5431 .renameat_fn = fruit_renameat,
5432 .openat_fn = fruit_openat,
5433 .close_fn = fruit_close,
5434 .pread_fn = fruit_pread,
5435 .pwrite_fn = fruit_pwrite,
5436 .pread_send_fn = fruit_pread_send,
5437 .pread_recv_fn = fruit_pread_recv,
5438 .pwrite_send_fn = fruit_pwrite_send,
5439 .pwrite_recv_fn = fruit_pwrite_recv,
5440 .fsync_send_fn = fruit_fsync_send,
5441 .fsync_recv_fn = fruit_fsync_recv,
5442 .stat_fn = fruit_stat,
5443 .lstat_fn = fruit_lstat,
5444 .fstat_fn = fruit_fstat,
5445 .fstreaminfo_fn = fruit_fstreaminfo,
5446 .fntimes_fn = fruit_fntimes,
5447 .ftruncate_fn = fruit_ftruncate,
5448 .fallocate_fn = fruit_fallocate,
5449 .create_file_fn = fruit_create_file,
5450 .freaddir_attr_fn = fruit_freaddir_attr,
5451 .offload_read_send_fn = fruit_offload_read_send,
5452 .offload_read_recv_fn = fruit_offload_read_recv,
5453 .offload_write_send_fn = fruit_offload_write_send,
5454 .offload_write_recv_fn = fruit_offload_write_recv,
5455 .fs_file_id_fn = fruit_fs_file_id,
5457 /* NT ACL operations */
5458 .fget_nt_acl_fn = fruit_fget_nt_acl,
5459 .fset_nt_acl_fn = fruit_fset_nt_acl,
5462 static_decl_vfs;
5463 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5465 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5466 &vfs_fruit_fns);
5467 if (!NT_STATUS_IS_OK(ret)) {
5468 return ret;
5471 vfs_fruit_debug_level = debug_add_class("fruit");
5472 if (vfs_fruit_debug_level == -1) {
5473 vfs_fruit_debug_level = DBGC_VFS;
5474 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5475 "vfs_fruit_init"));
5476 } else {
5477 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5478 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5481 return ret;