vfs: remove SMB_VFS_OPEN()
[Samba.git] / source3 / modules / vfs_fruit.c
blob51251b7b86beae25b87be6ddc4fc0d8113e2838d
1 /*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "system/shmem.h"
26 #include "locking/proto.h"
27 #include "smbd/globals.h"
28 #include "messages.h"
29 #include "libcli/security/security.h"
30 #include "../libcli/smb/smb2_create_ctx.h"
31 #include "lib/util/tevent_ntstatus.h"
32 #include "lib/util/tevent_unix.h"
33 #include "offload_token.h"
34 #include "string_replace.h"
35 #include "hash_inode.h"
36 #include "lib/adouble.h"
37 #include "lib/util_macstreams.h"
40 * Enhanced OS X and Netatalk compatibility
41 * ========================================
43 * This modules takes advantage of vfs_streams_xattr and
44 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
45 * loaded in the correct order:
47 * vfs modules = catia fruit streams_xattr
49 * The module intercepts the OS X special streams "AFP_AfpInfo" and
50 * "AFP_Resource" and handles them in a special way. All other named
51 * streams are deferred to vfs_streams_xattr.
53 * The OS X client maps all NTFS illegal characters to the Unicode
54 * private range. This module optionally stores the characters using
55 * their native ASCII encoding using vfs_catia. If you're not enabling
56 * this feature, you can skip catia from vfs modules.
58 * Finally, open modes are optionally checked against Netatalk AFP
59 * share modes.
61 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
62 * extended metadata for files and directories. This module optionally
63 * reads and stores this metadata in a way compatible with Netatalk 3
64 * which stores the metadata in an EA "org.netatalk.metadata". Cf
65 * source3/include/MacExtensions.h for a description of the binary
66 * blobs content.
68 * The "AFP_Resource" named stream may be arbitrarily large, thus it
69 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
70 * the only available filesystem where xattrs can be of any size and
71 * the OS supports using the file APIs for xattrs.
73 * The AFP_Resource stream is stored in an AppleDouble file prepending
74 * "._" to the filename. On Solaris with ZFS the stream is optionally
75 * stored in an EA "org.netatalk.resource".
78 * Extended Attributes
79 * ===================
81 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
82 * other protocols you may want to adjust the xattr names the VFS
83 * module vfs_streams_xattr uses for storing ADS's. This defaults to
84 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
85 * these module parameters:
87 * streams_xattr:prefix = user.
88 * streams_xattr:store_stream_type = false
91 * TODO
92 * ====
94 * - log diagnostic if any needed VFS module is not loaded
95 * (eg with lp_vfs_objects())
96 * - add tests
99 static int vfs_fruit_debug_level = DBGC_VFS;
101 static struct global_fruit_config {
102 bool nego_aapl; /* client negotiated AAPL */
104 } global_fruit_config;
106 #undef DBGC_CLASS
107 #define DBGC_CLASS vfs_fruit_debug_level
109 #define FRUIT_PARAM_TYPE_NAME "fruit"
111 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
113 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
114 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
115 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
116 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
118 struct fruit_config_data {
119 enum fruit_rsrc rsrc;
120 enum fruit_meta meta;
121 enum fruit_locking locking;
122 enum fruit_encoding encoding;
123 bool use_aapl; /* config from smb.conf */
124 bool use_copyfile;
125 bool readdir_attr_enabled;
126 bool unix_info_enabled;
127 bool copyfile_enabled;
128 bool veto_appledouble;
129 bool posix_rename;
130 bool aapl_zero_file_id;
131 const char *model;
132 bool time_machine;
133 off_t time_machine_max_size;
134 bool wipe_intentionally_left_blank_rfork;
135 bool delete_empty_adfiles;
138 * Additional options, all enabled by default,
139 * possibly useful for analyzing performance. The associated
140 * operations with each of them may be expensive, so having
141 * the chance to disable them individually gives a chance
142 * tweaking the setup for the particular usecase.
144 bool readdir_attr_rsize;
145 bool readdir_attr_finder_info;
146 bool readdir_attr_max_access;
149 static const struct enum_list fruit_rsrc[] = {
150 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
151 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
152 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
153 { -1, NULL}
156 static const struct enum_list fruit_meta[] = {
157 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
159 { -1, NULL}
162 static const struct enum_list fruit_locking[] = {
163 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
164 {FRUIT_LOCKING_NONE, "none"},
165 { -1, NULL}
168 static const struct enum_list fruit_encoding[] = {
169 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
170 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
171 { -1, NULL}
174 struct fio {
175 /* tcon config handle */
176 struct fruit_config_data *config;
178 /* Denote stream type, meta or rsrc */
179 adouble_type_t type;
181 /* Whether the create created the stream */
182 bool created;
185 * AFP_AfpInfo stream created, but not written yet, thus still a fake
186 * pipe fd. This is set to true in fruit_open_meta if there was no
187 * existing stream but the caller requested O_CREAT. It is later set to
188 * false when we get a write on the stream that then does open and
189 * create the stream.
191 bool fake_fd;
192 int flags;
193 int mode;
196 /*****************************************************************************
197 * Helper functions
198 *****************************************************************************/
201 * Initialize config struct from our smb.conf config parameters
203 static int init_fruit_config(vfs_handle_struct *handle)
205 struct fruit_config_data *config;
206 int enumval;
207 const char *tm_size_str = NULL;
209 config = talloc_zero(handle->conn, struct fruit_config_data);
210 if (!config) {
211 DEBUG(1, ("talloc_zero() failed\n"));
212 errno = ENOMEM;
213 return -1;
217 * Versions up to Samba 4.5.x had a spelling bug in the
218 * fruit:resource option calling lp_parm_enum with
219 * "res*s*ource" (ie two s).
221 * In Samba 4.6 we accept both the wrong and the correct
222 * spelling, in Samba 4.7 the bad spelling will be removed.
224 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
225 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
226 if (enumval == -1) {
227 DEBUG(1, ("value for %s: resource type unknown\n",
228 FRUIT_PARAM_TYPE_NAME));
229 return -1;
231 config->rsrc = (enum fruit_rsrc)enumval;
233 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
234 "resource", fruit_rsrc, enumval);
235 if (enumval == -1) {
236 DEBUG(1, ("value for %s: resource type unknown\n",
237 FRUIT_PARAM_TYPE_NAME));
238 return -1;
240 config->rsrc = (enum fruit_rsrc)enumval;
242 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
243 "metadata", fruit_meta, FRUIT_META_NETATALK);
244 if (enumval == -1) {
245 DEBUG(1, ("value for %s: metadata type unknown\n",
246 FRUIT_PARAM_TYPE_NAME));
247 return -1;
249 config->meta = (enum fruit_meta)enumval;
251 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
252 "locking", fruit_locking, FRUIT_LOCKING_NONE);
253 if (enumval == -1) {
254 DEBUG(1, ("value for %s: locking type unknown\n",
255 FRUIT_PARAM_TYPE_NAME));
256 return -1;
258 config->locking = (enum fruit_locking)enumval;
260 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
261 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
262 if (enumval == -1) {
263 DEBUG(1, ("value for %s: encoding type unknown\n",
264 FRUIT_PARAM_TYPE_NAME));
265 return -1;
267 config->encoding = (enum fruit_encoding)enumval;
269 if (config->rsrc == FRUIT_RSRC_ADFILE) {
270 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
271 FRUIT_PARAM_TYPE_NAME,
272 "veto_appledouble",
273 true);
276 config->use_aapl = lp_parm_bool(
277 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
279 config->time_machine = lp_parm_bool(
280 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
282 config->unix_info_enabled = lp_parm_bool(
283 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
285 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
286 "copyfile", false);
288 config->posix_rename = lp_parm_bool(
289 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
291 config->aapl_zero_file_id =
292 lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
293 "zero_file_id", false);
295 config->readdir_attr_rsize = lp_parm_bool(
296 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
298 config->readdir_attr_finder_info = lp_parm_bool(
299 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
301 config->readdir_attr_max_access = lp_parm_bool(
302 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
304 config->model = lp_parm_const_string(
305 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
307 tm_size_str = lp_parm_const_string(
308 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
309 "time machine max size", NULL);
310 if (tm_size_str != NULL) {
311 config->time_machine_max_size = conv_str_size(tm_size_str);
314 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
315 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
316 "wipe_intentionally_left_blank_rfork", false);
318 config->delete_empty_adfiles = lp_parm_bool(
319 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
320 "delete_empty_adfiles", false);
322 SMB_VFS_HANDLE_SET_DATA(handle, config,
323 NULL, struct fruit_config_data,
324 return -1);
326 return 0;
329 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
330 struct stream_struct **streams,
331 const char *name, off_t size,
332 off_t alloc_size)
334 struct stream_struct *tmp;
336 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
337 (*num_streams)+1);
338 if (tmp == NULL) {
339 return false;
342 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
343 if (tmp[*num_streams].name == NULL) {
344 return false;
347 tmp[*num_streams].size = size;
348 tmp[*num_streams].alloc_size = alloc_size;
350 *streams = tmp;
351 *num_streams += 1;
352 return true;
355 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
356 struct stream_struct **streams)
358 struct stream_struct *tmp = *streams;
359 unsigned int i;
361 if (*num_streams == 0) {
362 return true;
365 for (i = 0; i < *num_streams; i++) {
366 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
367 break;
371 if (i == *num_streams) {
372 return true;
375 if (tmp[i].size > 0) {
376 return true;
379 TALLOC_FREE(tmp[i].name);
380 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
381 *num_streams -= 1;
382 return true;
385 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
386 struct stream_struct **streams,
387 const char *name)
389 struct stream_struct *tmp = *streams;
390 unsigned int i;
392 if (*num_streams == 0) {
393 return true;
396 for (i = 0; i < *num_streams; i++) {
397 if (strequal_m(tmp[i].name, name)) {
398 break;
402 if (i == *num_streams) {
403 return true;
406 TALLOC_FREE(tmp[i].name);
407 ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
408 *num_streams -= 1;
409 return true;
412 static bool ad_empty_finderinfo(const struct adouble *ad)
414 int cmp;
415 char emptybuf[ADEDLEN_FINDERI] = {0};
416 char *fi = NULL;
418 fi = ad_get_entry(ad, ADEID_FINDERI);
419 if (fi == NULL) {
420 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
421 return false;
424 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
425 return (cmp == 0);
428 static bool ai_empty_finderinfo(const AfpInfo *ai)
430 int cmp;
431 char emptybuf[ADEDLEN_FINDERI] = {0};
433 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
434 return (cmp == 0);
438 * Update btime with btime from Netatalk
440 static void update_btime(vfs_handle_struct *handle,
441 struct smb_filename *smb_fname)
443 uint32_t t;
444 struct timespec creation_time = {0};
445 struct adouble *ad;
446 struct fruit_config_data *config = NULL;
448 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
449 return);
451 switch (config->meta) {
452 case FRUIT_META_STREAM:
453 return;
454 case FRUIT_META_NETATALK:
455 /* Handled below */
456 break;
457 default:
458 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
459 return;
462 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
463 if (ad == NULL) {
464 return;
466 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
467 TALLOC_FREE(ad);
468 return;
470 TALLOC_FREE(ad);
472 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
473 update_stat_ex_create_time(&smb_fname->st, creation_time);
475 return;
479 * Map an access mask to a Netatalk single byte byte range lock
481 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
482 uint32_t access_mask)
484 off_t offset;
486 switch (access_mask) {
487 case FILE_READ_DATA:
488 offset = AD_FILELOCK_OPEN_RD;
489 break;
491 case FILE_WRITE_DATA:
492 case FILE_APPEND_DATA:
493 offset = AD_FILELOCK_OPEN_WR;
494 break;
496 default:
497 offset = AD_FILELOCK_OPEN_NONE;
498 break;
501 if (fork_type == APPLE_FORK_RSRC) {
502 if (offset == AD_FILELOCK_OPEN_NONE) {
503 offset = AD_FILELOCK_RSRC_OPEN_NONE;
504 } else {
505 offset += 2;
509 return offset;
513 * Map a deny mode to a Netatalk brl
515 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
516 uint32_t deny_mode)
518 off_t offset = 0;
520 switch (deny_mode) {
521 case DENY_READ:
522 offset = AD_FILELOCK_DENY_RD;
523 break;
525 case DENY_WRITE:
526 offset = AD_FILELOCK_DENY_WR;
527 break;
529 default:
530 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
533 if (fork_type == APPLE_FORK_RSRC) {
534 offset += 2;
537 return offset;
541 * Call fcntl() with an exclusive F_GETLK request in order to
542 * determine if there's an existing shared lock
544 * @return true if the requested lock was found or any error occurred
545 * false if the lock was not found
547 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
549 bool result;
550 off_t offset = in_offset;
551 off_t len = 1;
552 int type = F_WRLCK;
553 pid_t pid = 0;
555 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
556 if (result == false) {
557 return true;
560 if (type != F_UNLCK) {
561 return true;
564 return false;
567 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
568 files_struct *fsp,
569 uint32_t access_mask,
570 uint32_t share_mode)
572 NTSTATUS status = NT_STATUS_OK;
573 off_t off;
574 bool share_for_read = (share_mode & FILE_SHARE_READ);
575 bool share_for_write = (share_mode & FILE_SHARE_WRITE);
576 bool netatalk_already_open_for_reading = false;
577 bool netatalk_already_open_for_writing = false;
578 bool netatalk_already_open_with_deny_read = false;
579 bool netatalk_already_open_with_deny_write = false;
580 struct GUID req_guid = GUID_random();
582 /* FIXME: hardcoded data fork, add resource fork */
583 enum apple_fork fork_type = APPLE_FORK_DATA;
585 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
586 fsp_str_dbg(fsp),
587 access_mask & FILE_READ_DATA ? "READ" :"-",
588 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
589 share_mode);
591 if (fsp->fh->fd == -1) {
592 return NT_STATUS_OK;
595 /* Read NetATalk opens and deny modes on the file. */
596 netatalk_already_open_for_reading = test_netatalk_lock(fsp,
597 access_to_netatalk_brl(fork_type,
598 FILE_READ_DATA));
600 netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
601 denymode_to_netatalk_brl(fork_type,
602 DENY_READ));
604 netatalk_already_open_for_writing = test_netatalk_lock(fsp,
605 access_to_netatalk_brl(fork_type,
606 FILE_WRITE_DATA));
608 netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
609 denymode_to_netatalk_brl(fork_type,
610 DENY_WRITE));
612 /* If there are any conflicts - sharing violation. */
613 if ((access_mask & FILE_READ_DATA) &&
614 netatalk_already_open_with_deny_read) {
615 return NT_STATUS_SHARING_VIOLATION;
618 if (!share_for_read &&
619 netatalk_already_open_for_reading) {
620 return NT_STATUS_SHARING_VIOLATION;
623 if ((access_mask & FILE_WRITE_DATA) &&
624 netatalk_already_open_with_deny_write) {
625 return NT_STATUS_SHARING_VIOLATION;
628 if (!share_for_write &&
629 netatalk_already_open_for_writing) {
630 return NT_STATUS_SHARING_VIOLATION;
633 if (!(access_mask & FILE_READ_DATA)) {
635 * Nothing we can do here, we need read access
636 * to set locks.
638 return NT_STATUS_OK;
641 /* Set NetAtalk locks matching our access */
642 if (access_mask & FILE_READ_DATA) {
643 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
644 req_guid.time_hi_and_version = __LINE__;
645 status = do_lock(
646 fsp,
647 talloc_tos(),
648 &req_guid,
649 fsp->op->global->open_persistent_id,
651 off,
652 READ_LOCK,
653 POSIX_LOCK,
654 NULL,
655 NULL);
657 if (!NT_STATUS_IS_OK(status)) {
658 return status;
662 if (!share_for_read) {
663 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
664 req_guid.time_hi_and_version = __LINE__;
665 status = do_lock(
666 fsp,
667 talloc_tos(),
668 &req_guid,
669 fsp->op->global->open_persistent_id,
671 off,
672 READ_LOCK,
673 POSIX_LOCK,
674 NULL,
675 NULL);
677 if (!NT_STATUS_IS_OK(status)) {
678 return status;
682 if (access_mask & FILE_WRITE_DATA) {
683 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
684 req_guid.time_hi_and_version = __LINE__;
685 status = do_lock(
686 fsp,
687 talloc_tos(),
688 &req_guid,
689 fsp->op->global->open_persistent_id,
691 off,
692 READ_LOCK,
693 POSIX_LOCK,
694 NULL,
695 NULL);
697 if (!NT_STATUS_IS_OK(status)) {
698 return status;
702 if (!share_for_write) {
703 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
704 req_guid.time_hi_and_version = __LINE__;
705 status = do_lock(
706 fsp,
707 talloc_tos(),
708 &req_guid,
709 fsp->op->global->open_persistent_id,
711 off,
712 READ_LOCK,
713 POSIX_LOCK,
714 NULL,
715 NULL);
717 if (!NT_STATUS_IS_OK(status)) {
718 return status;
722 return NT_STATUS_OK;
725 static NTSTATUS check_aapl(vfs_handle_struct *handle,
726 struct smb_request *req,
727 const struct smb2_create_blobs *in_context_blobs,
728 struct smb2_create_blobs *out_context_blobs)
730 struct fruit_config_data *config;
731 NTSTATUS status;
732 struct smb2_create_blob *aapl = NULL;
733 uint32_t cmd;
734 bool ok;
735 uint8_t p[16];
736 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
737 uint64_t req_bitmap, client_caps;
738 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
739 smb_ucs2_t *model;
740 size_t modellen;
742 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
743 return NT_STATUS_UNSUCCESSFUL);
745 if (!config->use_aapl
746 || in_context_blobs == NULL
747 || out_context_blobs == NULL) {
748 return NT_STATUS_OK;
751 aapl = smb2_create_blob_find(in_context_blobs,
752 SMB2_CREATE_TAG_AAPL);
753 if (aapl == NULL) {
754 return NT_STATUS_OK;
757 if (aapl->data.length != 24) {
758 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
759 (uintmax_t)aapl->data.length));
760 return NT_STATUS_INVALID_PARAMETER;
763 cmd = IVAL(aapl->data.data, 0);
764 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
765 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
766 return NT_STATUS_INVALID_PARAMETER;
769 req_bitmap = BVAL(aapl->data.data, 8);
770 client_caps = BVAL(aapl->data.data, 16);
772 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
773 SIVAL(p, 4, 0);
774 SBVAL(p, 8, req_bitmap);
775 ok = data_blob_append(req, &blob, p, 16);
776 if (!ok) {
777 return NT_STATUS_UNSUCCESSFUL;
780 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
781 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
782 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
783 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
784 config->readdir_attr_enabled = true;
787 if (config->use_copyfile) {
788 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
789 config->copyfile_enabled = true;
793 * The client doesn't set the flag, so we can't check
794 * for it and just set it unconditionally
796 if (config->unix_info_enabled) {
797 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
800 SBVAL(p, 0, server_caps);
801 ok = data_blob_append(req, &blob, p, 8);
802 if (!ok) {
803 return NT_STATUS_UNSUCCESSFUL;
807 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
808 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
809 uint64_t caps = 0;
811 switch (val) {
812 case Auto:
813 break;
815 case True:
816 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
817 break;
819 default:
820 break;
823 if (config->time_machine) {
824 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
827 SBVAL(p, 0, caps);
829 ok = data_blob_append(req, &blob, p, 8);
830 if (!ok) {
831 return NT_STATUS_UNSUCCESSFUL;
835 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
836 ok = convert_string_talloc(req,
837 CH_UNIX, CH_UTF16LE,
838 config->model, strlen(config->model),
839 &model, &modellen);
840 if (!ok) {
841 return NT_STATUS_UNSUCCESSFUL;
844 SIVAL(p, 0, 0);
845 SIVAL(p + 4, 0, modellen);
846 ok = data_blob_append(req, &blob, p, 8);
847 if (!ok) {
848 talloc_free(model);
849 return NT_STATUS_UNSUCCESSFUL;
852 ok = data_blob_append(req, &blob, model, modellen);
853 talloc_free(model);
854 if (!ok) {
855 return NT_STATUS_UNSUCCESSFUL;
859 status = smb2_create_blob_add(out_context_blobs,
860 out_context_blobs,
861 SMB2_CREATE_TAG_AAPL,
862 blob);
863 if (NT_STATUS_IS_OK(status)) {
864 global_fruit_config.nego_aapl = true;
867 return status;
870 static bool readdir_attr_meta_finderi_stream(
871 struct vfs_handle_struct *handle,
872 const struct smb_filename *smb_fname,
873 AfpInfo *ai)
875 struct smb_filename *stream_name = NULL;
876 files_struct *fsp = NULL;
877 ssize_t nread;
878 NTSTATUS status;
879 int ret;
880 bool ok;
881 uint8_t buf[AFP_INFO_SIZE];
883 stream_name = synthetic_smb_fname(talloc_tos(),
884 smb_fname->base_name,
885 AFPINFO_STREAM_NAME,
886 NULL,
887 smb_fname->twrp,
888 smb_fname->flags);
889 if (stream_name == NULL) {
890 return false;
893 ret = SMB_VFS_STAT(handle->conn, stream_name);
894 if (ret != 0) {
895 return false;
898 status = SMB_VFS_CREATE_FILE(
899 handle->conn, /* conn */
900 NULL, /* req */
901 &handle->conn->cwd_fsp, /* dirfsp */
902 stream_name, /* fname */
903 FILE_READ_DATA, /* access_mask */
904 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
905 FILE_SHARE_DELETE),
906 FILE_OPEN, /* create_disposition*/
907 0, /* create_options */
908 0, /* file_attributes */
909 INTERNAL_OPEN_ONLY, /* oplock_request */
910 NULL, /* lease */
911 0, /* allocation_size */
912 0, /* private_flags */
913 NULL, /* sd */
914 NULL, /* ea_list */
915 &fsp, /* result */
916 NULL, /* pinfo */
917 NULL, NULL); /* create context */
919 TALLOC_FREE(stream_name);
921 if (!NT_STATUS_IS_OK(status)) {
922 return false;
925 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
926 if (nread != AFP_INFO_SIZE) {
927 DBG_ERR("short read [%s] [%zd/%d]\n",
928 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
929 ok = false;
930 goto fail;
933 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
934 AFP_FinderSize);
936 ok = true;
938 fail:
939 if (fsp != NULL) {
940 close_file(NULL, fsp, NORMAL_CLOSE);
943 return ok;
946 static bool readdir_attr_meta_finderi_netatalk(
947 struct vfs_handle_struct *handle,
948 const struct smb_filename *smb_fname,
949 AfpInfo *ai)
951 struct adouble *ad = NULL;
952 char *p = NULL;
954 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
955 if (ad == NULL) {
956 return false;
959 p = ad_get_entry(ad, ADEID_FINDERI);
960 if (p == NULL) {
961 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
962 TALLOC_FREE(ad);
963 return false;
966 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
967 TALLOC_FREE(ad);
968 return true;
971 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
972 const struct smb_filename *smb_fname,
973 struct readdir_attr_data *attr_data)
975 struct fruit_config_data *config = NULL;
976 uint32_t date_added;
977 AfpInfo ai = {0};
978 bool ok;
980 SMB_VFS_HANDLE_GET_DATA(handle, config,
981 struct fruit_config_data,
982 return false);
984 switch (config->meta) {
985 case FRUIT_META_NETATALK:
986 ok = readdir_attr_meta_finderi_netatalk(
987 handle, smb_fname, &ai);
988 break;
990 case FRUIT_META_STREAM:
991 ok = readdir_attr_meta_finderi_stream(
992 handle, smb_fname, &ai);
993 break;
995 default:
996 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
997 return false;
1000 if (!ok) {
1001 /* Don't bother with errors, it's likely ENOENT */
1002 return true;
1005 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1006 /* finder_type */
1007 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1008 &ai.afpi_FinderInfo[0], 4);
1010 /* finder_creator */
1011 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1012 &ai.afpi_FinderInfo[4], 4);
1015 /* finder_flags */
1016 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1017 &ai.afpi_FinderInfo[8], 2);
1019 /* finder_ext_flags */
1020 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1021 &ai.afpi_FinderInfo[24], 2);
1023 /* creation date */
1024 date_added = convert_time_t_to_uint32_t(
1025 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1027 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1029 return true;
1032 static uint64_t readdir_attr_rfork_size_adouble(
1033 struct vfs_handle_struct *handle,
1034 const struct smb_filename *smb_fname)
1036 struct adouble *ad = NULL;
1037 uint64_t rfork_size;
1039 ad = ad_get(talloc_tos(), handle, smb_fname,
1040 ADOUBLE_RSRC);
1041 if (ad == NULL) {
1042 return 0;
1045 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1046 TALLOC_FREE(ad);
1048 return rfork_size;
1051 static uint64_t readdir_attr_rfork_size_stream(
1052 struct vfs_handle_struct *handle,
1053 const struct smb_filename *smb_fname)
1055 struct smb_filename *stream_name = NULL;
1056 int ret;
1057 uint64_t rfork_size;
1059 stream_name = synthetic_smb_fname(talloc_tos(),
1060 smb_fname->base_name,
1061 AFPRESOURCE_STREAM_NAME,
1062 NULL,
1063 smb_fname->twrp,
1065 if (stream_name == NULL) {
1066 return 0;
1069 ret = SMB_VFS_STAT(handle->conn, stream_name);
1070 if (ret != 0) {
1071 TALLOC_FREE(stream_name);
1072 return 0;
1075 rfork_size = stream_name->st.st_ex_size;
1076 TALLOC_FREE(stream_name);
1078 return rfork_size;
1081 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1082 const struct smb_filename *smb_fname)
1084 struct fruit_config_data *config = NULL;
1085 uint64_t rfork_size;
1087 SMB_VFS_HANDLE_GET_DATA(handle, config,
1088 struct fruit_config_data,
1089 return 0);
1091 switch (config->rsrc) {
1092 case FRUIT_RSRC_ADFILE:
1093 rfork_size = readdir_attr_rfork_size_adouble(handle,
1094 smb_fname);
1095 break;
1097 case FRUIT_RSRC_XATTR:
1098 case FRUIT_RSRC_STREAM:
1099 rfork_size = readdir_attr_rfork_size_stream(handle,
1100 smb_fname);
1101 break;
1103 default:
1104 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1105 rfork_size = 0;
1106 break;
1109 return rfork_size;
1112 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1113 const struct smb_filename *smb_fname,
1114 struct readdir_attr_data *attr_data)
1116 NTSTATUS status = NT_STATUS_OK;
1117 struct fruit_config_data *config = NULL;
1118 bool ok;
1120 SMB_VFS_HANDLE_GET_DATA(handle, config,
1121 struct fruit_config_data,
1122 return NT_STATUS_UNSUCCESSFUL);
1125 /* Ensure we return a default value in the creation_date field */
1126 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1129 * Resource fork length
1132 if (config->readdir_attr_rsize) {
1133 uint64_t rfork_size;
1135 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1136 attr_data->attr_data.aapl.rfork_size = rfork_size;
1140 * FinderInfo
1143 if (config->readdir_attr_finder_info) {
1144 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1145 if (!ok) {
1146 status = NT_STATUS_INTERNAL_ERROR;
1150 return status;
1153 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1155 NTSTATUS status;
1156 uint32_t i;
1158 if (psd->dacl == NULL) {
1159 return NT_STATUS_OK;
1162 for (i = 0; i < psd->dacl->num_aces; i++) {
1163 /* MS NFS style mode/uid/gid */
1164 int cmp = dom_sid_compare_domain(
1165 &global_sid_Unix_NFS,
1166 &psd->dacl->aces[i].trustee);
1167 if (cmp != 0) {
1168 /* Normal ACE entry. */
1169 continue;
1173 * security_descriptor_dacl_del()
1174 * *must* return NT_STATUS_OK as we know
1175 * we have something to remove.
1178 status = security_descriptor_dacl_del(psd,
1179 &psd->dacl->aces[i].trustee);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1182 nt_errstr(status));
1183 return status;
1187 * security_descriptor_dacl_del() may delete more
1188 * then one entry subsequent to this one if the
1189 * SID matches, but we only need to ensure that
1190 * we stay looking at the same element in the array.
1192 i--;
1194 return NT_STATUS_OK;
1197 /* Search MS NFS style ACE with UNIX mode */
1198 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1199 files_struct *fsp,
1200 struct security_descriptor *psd,
1201 mode_t *pmode,
1202 bool *pdo_chmod)
1204 uint32_t i;
1205 struct fruit_config_data *config = NULL;
1207 *pdo_chmod = false;
1209 SMB_VFS_HANDLE_GET_DATA(handle, config,
1210 struct fruit_config_data,
1211 return NT_STATUS_UNSUCCESSFUL);
1213 if (!global_fruit_config.nego_aapl) {
1214 return NT_STATUS_OK;
1216 if (psd->dacl == NULL || !config->unix_info_enabled) {
1217 return NT_STATUS_OK;
1220 for (i = 0; i < psd->dacl->num_aces; i++) {
1221 if (dom_sid_compare_domain(
1222 &global_sid_Unix_NFS_Mode,
1223 &psd->dacl->aces[i].trustee) == 0) {
1224 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1225 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1226 *pdo_chmod = true;
1228 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1229 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1230 break;
1235 * Remove any incoming virtual ACE entries generated by
1236 * fruit_fget_nt_acl().
1239 return remove_virtual_nfs_aces(psd);
1242 /****************************************************************************
1243 * VFS ops
1244 ****************************************************************************/
1246 static int fruit_connect(vfs_handle_struct *handle,
1247 const char *service,
1248 const char *user)
1250 int rc;
1251 char *list = NULL, *newlist = NULL;
1252 struct fruit_config_data *config;
1253 const struct loadparm_substitution *lp_sub =
1254 loadparm_s3_global_substitution();
1256 DEBUG(10, ("fruit_connect\n"));
1258 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1259 if (rc < 0) {
1260 return rc;
1263 rc = init_fruit_config(handle);
1264 if (rc != 0) {
1265 return rc;
1268 SMB_VFS_HANDLE_GET_DATA(handle, config,
1269 struct fruit_config_data, return -1);
1271 if (config->veto_appledouble) {
1272 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1274 if (list) {
1275 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1276 newlist = talloc_asprintf(
1277 list,
1278 "%s/" ADOUBLE_NAME_PREFIX "*/",
1279 list);
1280 lp_do_parameter(SNUM(handle->conn),
1281 "veto files",
1282 newlist);
1284 } else {
1285 lp_do_parameter(SNUM(handle->conn),
1286 "veto files",
1287 "/" ADOUBLE_NAME_PREFIX "*/");
1290 TALLOC_FREE(list);
1293 if (config->encoding == FRUIT_ENC_NATIVE) {
1294 lp_do_parameter(SNUM(handle->conn),
1295 "catia:mappings",
1296 macos_string_replace_map);
1299 if (config->time_machine) {
1300 DBG_NOTICE("Enabling durable handles for Time Machine "
1301 "support on [%s]\n", service);
1302 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1303 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1304 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1305 if (!lp_strict_sync(SNUM(handle->conn))) {
1306 DBG_WARNING("Time Machine without strict sync is not "
1307 "recommended!\n");
1309 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1312 return rc;
1315 static int fruit_fake_fd(void)
1317 int pipe_fds[2];
1318 int fd;
1319 int ret;
1322 * Return a valid fd, but ensure any attempt to use it returns
1323 * an error (EPIPE). Once we get a write on the handle, we open
1324 * the real fd.
1326 ret = pipe(pipe_fds);
1327 if (ret != 0) {
1328 return -1;
1330 fd = pipe_fds[0];
1331 close(pipe_fds[1]);
1333 return fd;
1336 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1337 const struct files_struct *dirfsp,
1338 const struct smb_filename *smb_fname,
1339 files_struct *fsp,
1340 int flags,
1341 mode_t mode)
1343 struct fruit_config_data *config = NULL;
1344 struct fio *fio = NULL;
1345 int open_flags = flags & ~O_CREAT;
1346 int fd;
1348 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1350 SMB_VFS_HANDLE_GET_DATA(handle, config,
1351 struct fruit_config_data, return -1);
1353 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1354 fio->type = ADOUBLE_META;
1355 fio->config = config;
1357 fd = SMB_VFS_NEXT_OPENAT(handle,
1358 dirfsp,
1359 smb_fname,
1360 fsp,
1361 open_flags,
1362 mode);
1363 if (fd != -1) {
1364 return fd;
1367 if (!(flags & O_CREAT)) {
1368 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1369 return -1;
1372 fd = fruit_fake_fd();
1373 if (fd == -1) {
1374 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1375 return -1;
1378 fio->fake_fd = true;
1379 fio->flags = flags;
1380 fio->mode = mode;
1382 return fd;
1385 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1386 const struct files_struct *dirfsp,
1387 const struct smb_filename *smb_fname,
1388 files_struct *fsp,
1389 int flags,
1390 mode_t mode)
1392 struct fruit_config_data *config = NULL;
1393 struct fio *fio = NULL;
1394 struct adouble *ad = NULL;
1395 bool meta_exists = false;
1396 int fd;
1398 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1400 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1401 if (ad != NULL) {
1402 meta_exists = true;
1405 TALLOC_FREE(ad);
1407 if (!meta_exists && !(flags & O_CREAT)) {
1408 errno = ENOENT;
1409 return -1;
1412 fd = fruit_fake_fd();
1413 if (fd == -1) {
1414 return -1;
1417 SMB_VFS_HANDLE_GET_DATA(handle, config,
1418 struct fruit_config_data, return -1);
1420 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1421 fio->type = ADOUBLE_META;
1422 fio->config = config;
1423 fio->fake_fd = true;
1424 fio->flags = flags;
1425 fio->mode = mode;
1427 return fd;
1430 static int fruit_open_meta(vfs_handle_struct *handle,
1431 const struct files_struct *dirfsp,
1432 const struct smb_filename *smb_fname,
1433 files_struct *fsp, int flags, mode_t mode)
1435 int fd;
1436 struct fruit_config_data *config = NULL;
1438 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1440 SMB_VFS_HANDLE_GET_DATA(handle, config,
1441 struct fruit_config_data, return -1);
1443 switch (config->meta) {
1444 case FRUIT_META_STREAM:
1445 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1446 fsp, flags, mode);
1447 break;
1449 case FRUIT_META_NETATALK:
1450 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1451 fsp, flags, mode);
1452 break;
1454 default:
1455 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1456 return -1;
1459 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1461 return fd;
1464 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
1465 const struct files_struct *dirfsp,
1466 const struct smb_filename *smb_fname,
1467 files_struct *fsp,
1468 int flags,
1469 mode_t mode)
1471 int rc = 0;
1472 struct adouble *ad = NULL;
1473 struct smb_filename *smb_fname_base = NULL;
1474 struct fruit_config_data *config = NULL;
1475 int hostfd = -1;
1477 SMB_VFS_HANDLE_GET_DATA(handle, config,
1478 struct fruit_config_data, return -1);
1480 if ((!(flags & O_CREAT)) &&
1481 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1483 /* sorry, but directories don't habe a resource fork */
1484 rc = -1;
1485 goto exit;
1488 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
1489 if (rc != 0) {
1490 goto exit;
1493 /* We always need read/write access for the metadata header too */
1494 flags &= ~(O_RDONLY | O_WRONLY);
1495 flags |= O_RDWR;
1497 hostfd = SMB_VFS_NEXT_OPENAT(handle,
1498 dirfsp,
1499 smb_fname_base,
1500 fsp,
1501 flags,
1502 mode);
1503 if (hostfd == -1) {
1504 rc = -1;
1505 goto exit;
1508 if (flags & (O_CREAT | O_TRUNC)) {
1509 ad = ad_init(fsp, ADOUBLE_RSRC);
1510 if (ad == NULL) {
1511 rc = -1;
1512 goto exit;
1515 fsp->fh->fd = hostfd;
1517 rc = ad_fset(handle, ad, fsp);
1518 fsp->fh->fd = -1;
1519 if (rc != 0) {
1520 rc = -1;
1521 goto exit;
1523 TALLOC_FREE(ad);
1526 exit:
1528 TALLOC_FREE(smb_fname_base);
1530 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
1531 if (rc != 0) {
1532 int saved_errno = errno;
1533 if (hostfd >= 0) {
1535 * BUGBUGBUG -- we would need to call
1536 * fd_close_posix here, but we don't have a
1537 * full fsp yet
1539 fsp->fh->fd = hostfd;
1540 SMB_VFS_CLOSE(fsp);
1542 hostfd = -1;
1543 errno = saved_errno;
1545 return hostfd;
1548 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1549 const struct files_struct *dirfsp,
1550 const struct smb_filename *smb_fname,
1551 files_struct *fsp,
1552 int flags,
1553 mode_t mode)
1555 #ifdef HAVE_ATTROPEN
1556 int fd = -1;
1559 * As there's no attropenat() this is only going to work with AT_FDCWD.
1561 SMB_ASSERT(dirfsp->fh->fd == AT_FDCWD);
1563 fd = attropen(smb_fname->base_name,
1564 AFPRESOURCE_EA_NETATALK,
1565 flags,
1566 mode);
1567 if (fd == -1) {
1568 return -1;
1571 return fd;
1573 #else
1574 errno = ENOSYS;
1575 return -1;
1576 #endif
1579 static int fruit_open_rsrc(vfs_handle_struct *handle,
1580 const struct files_struct *dirfsp,
1581 const struct smb_filename *smb_fname,
1582 files_struct *fsp, int flags, mode_t mode)
1584 int fd;
1585 struct fruit_config_data *config = NULL;
1586 struct fio *fio = NULL;
1588 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1590 SMB_VFS_HANDLE_GET_DATA(handle, config,
1591 struct fruit_config_data, return -1);
1593 switch (config->rsrc) {
1594 case FRUIT_RSRC_STREAM:
1595 fd = SMB_VFS_NEXT_OPENAT(handle,
1596 dirfsp,
1597 smb_fname,
1598 fsp,
1599 flags,
1600 mode);
1601 break;
1603 case FRUIT_RSRC_ADFILE:
1604 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1605 fsp, flags, mode);
1606 break;
1608 case FRUIT_RSRC_XATTR:
1609 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1610 fsp, flags, mode);
1611 break;
1613 default:
1614 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1615 return -1;
1618 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1620 if (fd == -1) {
1621 return -1;
1624 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
1625 fio->type = ADOUBLE_RSRC;
1626 fio->config = config;
1628 return fd;
1631 static int fruit_openat(vfs_handle_struct *handle,
1632 const struct files_struct *dirfsp,
1633 const struct smb_filename *smb_fname,
1634 files_struct *fsp,
1635 int flags,
1636 mode_t mode)
1638 int fd;
1640 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1642 if (!is_named_stream(smb_fname)) {
1643 return SMB_VFS_NEXT_OPENAT(handle,
1644 dirfsp,
1645 smb_fname,
1646 fsp,
1647 flags,
1648 mode);
1651 if (is_afpinfo_stream(smb_fname->stream_name)) {
1652 fd = fruit_open_meta(handle,
1653 dirfsp,
1654 smb_fname,
1655 fsp,
1656 flags,
1657 mode);
1658 } else if (is_afpresource_stream(smb_fname->stream_name)) {
1659 fd = fruit_open_rsrc(handle,
1660 dirfsp,
1661 smb_fname,
1662 fsp,
1663 flags,
1664 mode);
1665 } else {
1666 fd = SMB_VFS_NEXT_OPENAT(handle,
1667 dirfsp,
1668 smb_fname,
1669 fsp,
1670 flags,
1671 mode);
1674 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1676 return fd;
1679 static int fruit_close_meta(vfs_handle_struct *handle,
1680 files_struct *fsp)
1682 int ret;
1683 struct fruit_config_data *config = NULL;
1685 SMB_VFS_HANDLE_GET_DATA(handle, config,
1686 struct fruit_config_data, return -1);
1688 switch (config->meta) {
1689 case FRUIT_META_STREAM:
1690 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1691 break;
1693 case FRUIT_META_NETATALK:
1694 ret = close(fsp->fh->fd);
1695 fsp->fh->fd = -1;
1696 break;
1698 default:
1699 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1700 return -1;
1703 return ret;
1707 static int fruit_close_rsrc(vfs_handle_struct *handle,
1708 files_struct *fsp)
1710 int ret;
1711 struct fruit_config_data *config = NULL;
1713 SMB_VFS_HANDLE_GET_DATA(handle, config,
1714 struct fruit_config_data, return -1);
1716 switch (config->rsrc) {
1717 case FRUIT_RSRC_STREAM:
1718 case FRUIT_RSRC_ADFILE:
1719 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1720 break;
1722 case FRUIT_RSRC_XATTR:
1723 ret = close(fsp->fh->fd);
1724 fsp->fh->fd = -1;
1725 break;
1727 default:
1728 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1729 return -1;
1732 return ret;
1735 static int fruit_close(vfs_handle_struct *handle,
1736 files_struct *fsp)
1738 int ret;
1739 int fd;
1741 fd = fsp->fh->fd;
1743 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1745 if (!is_named_stream(fsp->fsp_name)) {
1746 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1749 if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1750 ret = fruit_close_meta(handle, fsp);
1751 } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1752 ret = fruit_close_rsrc(handle, fsp);
1753 } else {
1754 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1757 return ret;
1760 static int fruit_renameat(struct vfs_handle_struct *handle,
1761 files_struct *srcfsp,
1762 const struct smb_filename *smb_fname_src,
1763 files_struct *dstfsp,
1764 const struct smb_filename *smb_fname_dst)
1766 int rc = -1;
1767 struct fruit_config_data *config = NULL;
1768 struct smb_filename *src_adp_smb_fname = NULL;
1769 struct smb_filename *dst_adp_smb_fname = NULL;
1771 SMB_VFS_HANDLE_GET_DATA(handle, config,
1772 struct fruit_config_data, return -1);
1774 if (!VALID_STAT(smb_fname_src->st)) {
1775 DBG_ERR("Need valid stat for [%s]\n",
1776 smb_fname_str_dbg(smb_fname_src));
1777 return -1;
1780 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1781 srcfsp,
1782 smb_fname_src,
1783 dstfsp,
1784 smb_fname_dst);
1785 if (rc != 0) {
1786 return -1;
1789 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1790 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1792 return 0;
1795 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1796 if (rc != 0) {
1797 goto done;
1800 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1801 if (rc != 0) {
1802 goto done;
1805 DBG_DEBUG("%s -> %s\n",
1806 smb_fname_str_dbg(src_adp_smb_fname),
1807 smb_fname_str_dbg(dst_adp_smb_fname));
1809 rc = SMB_VFS_NEXT_RENAMEAT(handle,
1810 srcfsp,
1811 src_adp_smb_fname,
1812 dstfsp,
1813 dst_adp_smb_fname);
1814 if (errno == ENOENT) {
1815 rc = 0;
1818 done:
1819 TALLOC_FREE(src_adp_smb_fname);
1820 TALLOC_FREE(dst_adp_smb_fname);
1821 return rc;
1824 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1825 struct files_struct *dirfsp,
1826 const struct smb_filename *smb_fname)
1828 return SMB_VFS_NEXT_UNLINKAT(handle,
1829 dirfsp,
1830 smb_fname,
1834 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1835 const struct smb_filename *smb_fname)
1837 return SMB_VFS_REMOVEXATTR(handle->conn,
1838 smb_fname,
1839 AFPINFO_EA_NETATALK);
1842 static int fruit_unlink_meta(vfs_handle_struct *handle,
1843 struct files_struct *dirfsp,
1844 const struct smb_filename *smb_fname)
1846 struct fruit_config_data *config = NULL;
1847 int rc;
1849 SMB_VFS_HANDLE_GET_DATA(handle, config,
1850 struct fruit_config_data, return -1);
1852 switch (config->meta) {
1853 case FRUIT_META_STREAM:
1854 rc = fruit_unlink_meta_stream(handle,
1855 dirfsp,
1856 smb_fname);
1857 break;
1859 case FRUIT_META_NETATALK:
1860 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1861 break;
1863 default:
1864 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
1865 return -1;
1868 return rc;
1871 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
1872 struct files_struct *dirfsp,
1873 const struct smb_filename *smb_fname,
1874 bool force_unlink)
1876 int ret;
1878 if (!force_unlink) {
1879 struct smb_filename *smb_fname_cp = NULL;
1880 off_t size;
1882 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
1883 if (smb_fname_cp == NULL) {
1884 return -1;
1888 * 0 byte resource fork streams are not listed by
1889 * vfs_streaminfo, as a result stream cleanup/deletion of file
1890 * deletion doesn't remove the resourcefork stream.
1893 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
1894 if (ret != 0) {
1895 TALLOC_FREE(smb_fname_cp);
1896 DBG_ERR("stat [%s] failed [%s]\n",
1897 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
1898 return -1;
1901 size = smb_fname_cp->st.st_ex_size;
1902 TALLOC_FREE(smb_fname_cp);
1904 if (size > 0) {
1905 /* OS X ignores resource fork stream delete requests */
1906 return 0;
1910 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1911 dirfsp,
1912 smb_fname,
1914 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
1915 ret = 0;
1918 return ret;
1921 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
1922 struct files_struct *dirfsp,
1923 const struct smb_filename *smb_fname,
1924 bool force_unlink)
1926 int rc;
1927 struct adouble *ad = NULL;
1928 struct smb_filename *adp_smb_fname = NULL;
1930 if (!force_unlink) {
1931 ad = ad_get(talloc_tos(), handle, smb_fname,
1932 ADOUBLE_RSRC);
1933 if (ad == NULL) {
1934 errno = ENOENT;
1935 return -1;
1940 * 0 byte resource fork streams are not listed by
1941 * vfs_streaminfo, as a result stream cleanup/deletion of file
1942 * deletion doesn't remove the resourcefork stream.
1945 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1946 /* OS X ignores resource fork stream delete requests */
1947 TALLOC_FREE(ad);
1948 return 0;
1951 TALLOC_FREE(ad);
1954 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1955 if (rc != 0) {
1956 return -1;
1959 rc = SMB_VFS_NEXT_UNLINKAT(handle,
1960 dirfsp,
1961 adp_smb_fname,
1963 TALLOC_FREE(adp_smb_fname);
1964 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
1965 rc = 0;
1968 return rc;
1971 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
1972 const struct smb_filename *smb_fname,
1973 bool force_unlink)
1976 * OS X ignores resource fork stream delete requests, so nothing to do
1977 * here. Removing the file will remove the xattr anyway, so we don't
1978 * have to take care of removing 0 byte resource forks that could be
1979 * left behind.
1981 return 0;
1984 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
1985 struct files_struct *dirfsp,
1986 const struct smb_filename *smb_fname,
1987 bool force_unlink)
1989 struct fruit_config_data *config = NULL;
1990 int rc;
1992 SMB_VFS_HANDLE_GET_DATA(handle, config,
1993 struct fruit_config_data, return -1);
1995 switch (config->rsrc) {
1996 case FRUIT_RSRC_STREAM:
1997 rc = fruit_unlink_rsrc_stream(handle,
1998 dirfsp,
1999 smb_fname,
2000 force_unlink);
2001 break;
2003 case FRUIT_RSRC_ADFILE:
2004 rc = fruit_unlink_rsrc_adouble(handle,
2005 dirfsp,
2006 smb_fname,
2007 force_unlink);
2008 break;
2010 case FRUIT_RSRC_XATTR:
2011 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2012 break;
2014 default:
2015 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2016 return -1;
2019 return rc;
2022 static int fruit_chmod(vfs_handle_struct *handle,
2023 const struct smb_filename *smb_fname,
2024 mode_t mode)
2026 int rc = -1;
2027 struct fruit_config_data *config = NULL;
2028 struct smb_filename *smb_fname_adp = NULL;
2030 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
2031 if (rc != 0) {
2032 return rc;
2035 SMB_VFS_HANDLE_GET_DATA(handle, config,
2036 struct fruit_config_data, return -1);
2038 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2039 return 0;
2042 if (!VALID_STAT(smb_fname->st)) {
2043 return 0;
2046 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2047 return 0;
2050 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2051 if (rc != 0) {
2052 return -1;
2055 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
2057 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
2058 if (errno == ENOENT) {
2059 rc = 0;
2062 TALLOC_FREE(smb_fname_adp);
2063 return rc;
2066 static int fruit_unlinkat(vfs_handle_struct *handle,
2067 struct files_struct *dirfsp,
2068 const struct smb_filename *smb_fname,
2069 int flags)
2071 struct fruit_config_data *config = NULL;
2072 struct smb_filename *rsrc_smb_fname = NULL;
2073 int ret;
2075 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
2077 if (flags & AT_REMOVEDIR) {
2078 return SMB_VFS_NEXT_UNLINKAT(handle,
2079 dirfsp,
2080 smb_fname,
2081 AT_REMOVEDIR);
2084 SMB_VFS_HANDLE_GET_DATA(handle, config,
2085 struct fruit_config_data, return -1);
2087 if (is_afpinfo_stream(smb_fname->stream_name)) {
2088 return fruit_unlink_meta(handle,
2089 dirfsp,
2090 smb_fname);
2091 } else if (is_afpresource_stream(smb_fname->stream_name)) {
2092 return fruit_unlink_rsrc(handle,
2093 dirfsp,
2094 smb_fname,
2095 false);
2096 } else if (is_named_stream(smb_fname)) {
2097 return SMB_VFS_NEXT_UNLINKAT(handle,
2098 dirfsp,
2099 smb_fname,
2101 } else if (is_adouble_file(smb_fname->base_name)) {
2102 return SMB_VFS_NEXT_UNLINKAT(handle,
2103 dirfsp,
2104 smb_fname,
2109 * A request to delete the base file. Because 0 byte resource
2110 * fork streams are not listed by fruit_streaminfo,
2111 * delete_all_streams() can't remove 0 byte resource fork
2112 * streams, so we have to cleanup this here.
2114 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2115 smb_fname->base_name,
2116 AFPRESOURCE_STREAM_NAME,
2117 NULL,
2118 smb_fname->twrp,
2119 smb_fname->flags);
2120 if (rsrc_smb_fname == NULL) {
2121 return -1;
2124 ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2125 if ((ret != 0) && (errno != ENOENT)) {
2126 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2127 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2128 TALLOC_FREE(rsrc_smb_fname);
2129 return -1;
2131 TALLOC_FREE(rsrc_smb_fname);
2133 return SMB_VFS_NEXT_UNLINKAT(handle,
2134 dirfsp,
2135 smb_fname,
2139 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2140 files_struct *fsp, void *data,
2141 size_t n, off_t offset)
2143 ssize_t nread;
2144 int ret;
2146 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2147 if (nread == -1 || nread == n) {
2148 return nread;
2151 DBG_ERR("Removing [%s] after short read [%zd]\n",
2152 fsp_str_dbg(fsp), nread);
2154 ret = SMB_VFS_NEXT_UNLINKAT(handle,
2155 fsp->conn->cwd_fsp,
2156 fsp->fsp_name,
2158 if (ret != 0) {
2159 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2160 return -1;
2163 errno = EINVAL;
2164 return -1;
2167 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2168 files_struct *fsp, void *data,
2169 size_t n, off_t offset)
2171 AfpInfo *ai = NULL;
2172 struct adouble *ad = NULL;
2173 char afpinfo_buf[AFP_INFO_SIZE];
2174 char *p = NULL;
2175 ssize_t nread;
2177 ai = afpinfo_new(talloc_tos());
2178 if (ai == NULL) {
2179 return -1;
2182 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2183 if (ad == NULL) {
2184 nread = -1;
2185 goto fail;
2188 p = ad_get_entry(ad, ADEID_FINDERI);
2189 if (p == NULL) {
2190 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2191 nread = -1;
2192 goto fail;
2195 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2197 nread = afpinfo_pack(ai, afpinfo_buf);
2198 if (nread != AFP_INFO_SIZE) {
2199 nread = -1;
2200 goto fail;
2203 memcpy(data, afpinfo_buf, n);
2204 nread = n;
2206 fail:
2207 TALLOC_FREE(ai);
2208 return nread;
2211 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2212 files_struct *fsp, void *data,
2213 size_t n, off_t offset)
2215 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2216 ssize_t nread;
2217 ssize_t to_return;
2220 * OS X has a off-by-1 error in the offset calculation, so we're
2221 * bug compatible here. It won't hurt, as any relevant real
2222 * world read requests from the AFP_AfpInfo stream will be
2223 * offset=0 n=60. offset is ignored anyway, see below.
2225 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2226 return 0;
2229 if (fio == NULL) {
2230 DBG_ERR("Failed to fetch fsp extension");
2231 return -1;
2234 /* Yes, macOS always reads from offset 0 */
2235 offset = 0;
2236 to_return = MIN(n, AFP_INFO_SIZE);
2238 switch (fio->config->meta) {
2239 case FRUIT_META_STREAM:
2240 nread = fruit_pread_meta_stream(handle, fsp, data,
2241 to_return, offset);
2242 break;
2244 case FRUIT_META_NETATALK:
2245 nread = fruit_pread_meta_adouble(handle, fsp, data,
2246 to_return, offset);
2247 break;
2249 default:
2250 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2251 return -1;
2254 if (nread == -1 && fio->created) {
2255 AfpInfo *ai = NULL;
2256 char afpinfo_buf[AFP_INFO_SIZE];
2258 ai = afpinfo_new(talloc_tos());
2259 if (ai == NULL) {
2260 return -1;
2263 nread = afpinfo_pack(ai, afpinfo_buf);
2264 TALLOC_FREE(ai);
2265 if (nread != AFP_INFO_SIZE) {
2266 return -1;
2269 memcpy(data, afpinfo_buf, to_return);
2270 return to_return;
2273 return nread;
2276 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2277 files_struct *fsp, void *data,
2278 size_t n, off_t offset)
2280 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2283 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2284 files_struct *fsp, void *data,
2285 size_t n, off_t offset)
2287 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2290 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2291 files_struct *fsp, void *data,
2292 size_t n, off_t offset)
2294 struct adouble *ad = NULL;
2295 ssize_t nread;
2297 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2298 if (ad == NULL) {
2299 return -1;
2302 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
2303 offset + ad_getentryoff(ad, ADEID_RFORK));
2305 TALLOC_FREE(ad);
2306 return nread;
2309 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2310 files_struct *fsp, void *data,
2311 size_t n, off_t offset)
2313 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2314 ssize_t nread;
2316 if (fio == NULL) {
2317 errno = EINVAL;
2318 return -1;
2321 switch (fio->config->rsrc) {
2322 case FRUIT_RSRC_STREAM:
2323 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2324 break;
2326 case FRUIT_RSRC_ADFILE:
2327 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2328 break;
2330 case FRUIT_RSRC_XATTR:
2331 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2332 break;
2334 default:
2335 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2336 return -1;
2339 return nread;
2342 static ssize_t fruit_pread(vfs_handle_struct *handle,
2343 files_struct *fsp, void *data,
2344 size_t n, off_t offset)
2346 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2347 ssize_t nread;
2349 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2350 fsp_str_dbg(fsp), (intmax_t)offset, n);
2352 if (fio == NULL) {
2353 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2356 if (fio->type == ADOUBLE_META) {
2357 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2358 } else {
2359 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2362 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2363 return nread;
2366 static bool fruit_must_handle_aio_stream(struct fio *fio)
2368 if (fio == NULL) {
2369 return false;
2372 if (fio->type == ADOUBLE_META) {
2373 return true;
2376 if ((fio->type == ADOUBLE_RSRC) &&
2377 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2379 return true;
2382 return false;
2385 struct fruit_pread_state {
2386 ssize_t nread;
2387 struct vfs_aio_state vfs_aio_state;
2390 static void fruit_pread_done(struct tevent_req *subreq);
2392 static struct tevent_req *fruit_pread_send(
2393 struct vfs_handle_struct *handle,
2394 TALLOC_CTX *mem_ctx,
2395 struct tevent_context *ev,
2396 struct files_struct *fsp,
2397 void *data,
2398 size_t n, off_t offset)
2400 struct tevent_req *req = NULL;
2401 struct tevent_req *subreq = NULL;
2402 struct fruit_pread_state *state = NULL;
2403 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2405 req = tevent_req_create(mem_ctx, &state,
2406 struct fruit_pread_state);
2407 if (req == NULL) {
2408 return NULL;
2411 if (fruit_must_handle_aio_stream(fio)) {
2412 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2413 if (state->nread != n) {
2414 if (state->nread != -1) {
2415 errno = EIO;
2417 tevent_req_error(req, errno);
2418 return tevent_req_post(req, ev);
2420 tevent_req_done(req);
2421 return tevent_req_post(req, ev);
2424 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2425 data, n, offset);
2426 if (tevent_req_nomem(req, subreq)) {
2427 return tevent_req_post(req, ev);
2429 tevent_req_set_callback(subreq, fruit_pread_done, req);
2430 return req;
2433 static void fruit_pread_done(struct tevent_req *subreq)
2435 struct tevent_req *req = tevent_req_callback_data(
2436 subreq, struct tevent_req);
2437 struct fruit_pread_state *state = tevent_req_data(
2438 req, struct fruit_pread_state);
2440 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2441 TALLOC_FREE(subreq);
2443 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2444 return;
2446 tevent_req_done(req);
2449 static ssize_t fruit_pread_recv(struct tevent_req *req,
2450 struct vfs_aio_state *vfs_aio_state)
2452 struct fruit_pread_state *state = tevent_req_data(
2453 req, struct fruit_pread_state);
2455 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2456 return -1;
2459 *vfs_aio_state = state->vfs_aio_state;
2460 return state->nread;
2463 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2464 files_struct *fsp, const void *data,
2465 size_t n, off_t offset)
2467 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2468 AfpInfo *ai = NULL;
2469 size_t nwritten;
2470 int ret;
2471 bool ok;
2473 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2474 fsp_str_dbg(fsp), (intmax_t)offset, n);
2476 if (fio == NULL) {
2477 return -1;
2480 if (fio->fake_fd) {
2481 int fd;
2483 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
2484 if (ret != 0) {
2485 DBG_ERR("Close [%s] failed: %s\n",
2486 fsp_str_dbg(fsp), strerror(errno));
2487 fsp->fh->fd = -1;
2488 return -1;
2491 fd = SMB_VFS_NEXT_OPENAT(handle,
2492 fsp->dirfsp,
2493 fsp->fsp_name,
2494 fsp,
2495 fio->flags,
2496 fio->mode);
2497 if (fd == -1) {
2498 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2499 fsp_str_dbg(fsp), strerror(errno));
2500 return -1;
2502 fsp->fh->fd = fd;
2503 fio->fake_fd = false;
2506 ai = afpinfo_unpack(talloc_tos(), data);
2507 if (ai == NULL) {
2508 return -1;
2511 if (ai_empty_finderinfo(ai)) {
2513 * Writing an all 0 blob to the metadata stream results in the
2514 * stream being removed on a macOS server. This ensures we
2515 * behave the same and it verified by the "delete AFP_AfpInfo by
2516 * writing all 0" test.
2518 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2519 if (ret != 0) {
2520 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2521 fsp_str_dbg(fsp));
2522 return -1;
2525 ok = set_delete_on_close(
2526 fsp,
2527 true,
2528 handle->conn->session_info->security_token,
2529 handle->conn->session_info->unix_token);
2530 if (!ok) {
2531 DBG_ERR("set_delete_on_close on [%s] failed\n",
2532 fsp_str_dbg(fsp));
2533 return -1;
2535 return n;
2538 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2539 if (nwritten != n) {
2540 return -1;
2543 return n;
2546 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2547 files_struct *fsp, const void *data,
2548 size_t n, off_t offset)
2550 struct adouble *ad = NULL;
2551 AfpInfo *ai = NULL;
2552 char *p = NULL;
2553 int ret;
2554 bool ok;
2556 ai = afpinfo_unpack(talloc_tos(), data);
2557 if (ai == NULL) {
2558 return -1;
2561 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2562 if (ad == NULL) {
2563 ad = ad_init(talloc_tos(), ADOUBLE_META);
2564 if (ad == NULL) {
2565 return -1;
2568 p = ad_get_entry(ad, ADEID_FINDERI);
2569 if (p == NULL) {
2570 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2571 TALLOC_FREE(ad);
2572 return -1;
2575 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2577 ret = ad_fset(handle, ad, fsp);
2578 if (ret != 0) {
2579 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2580 TALLOC_FREE(ad);
2581 return -1;
2584 TALLOC_FREE(ad);
2586 if (!ai_empty_finderinfo(ai)) {
2587 return n;
2591 * Writing an all 0 blob to the metadata stream results in the stream
2592 * being removed on a macOS server. This ensures we behave the same and
2593 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2596 ok = set_delete_on_close(
2597 fsp,
2598 true,
2599 handle->conn->session_info->security_token,
2600 handle->conn->session_info->unix_token);
2601 if (!ok) {
2602 DBG_ERR("set_delete_on_close on [%s] failed\n",
2603 fsp_str_dbg(fsp));
2604 return -1;
2607 return n;
2610 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2611 files_struct *fsp, const void *data,
2612 size_t n, off_t offset)
2614 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2615 ssize_t nwritten;
2616 uint8_t buf[AFP_INFO_SIZE];
2617 size_t to_write;
2618 size_t to_copy;
2619 int cmp;
2621 if (fio == NULL) {
2622 DBG_ERR("Failed to fetch fsp extension");
2623 return -1;
2626 if (n < 3) {
2627 errno = EINVAL;
2628 return -1;
2631 if (offset != 0 && n < 60) {
2632 errno = EINVAL;
2633 return -1;
2636 cmp = memcmp(data, "AFP", 3);
2637 if (cmp != 0) {
2638 errno = EINVAL;
2639 return -1;
2642 if (n <= AFP_OFF_FinderInfo) {
2644 * Nothing to do here really, just return
2646 return n;
2649 offset = 0;
2651 to_copy = n;
2652 if (to_copy > AFP_INFO_SIZE) {
2653 to_copy = AFP_INFO_SIZE;
2655 memcpy(buf, data, to_copy);
2657 to_write = n;
2658 if (to_write != AFP_INFO_SIZE) {
2659 to_write = AFP_INFO_SIZE;
2662 switch (fio->config->meta) {
2663 case FRUIT_META_STREAM:
2664 nwritten = fruit_pwrite_meta_stream(handle,
2665 fsp,
2666 buf,
2667 to_write,
2668 offset);
2669 break;
2671 case FRUIT_META_NETATALK:
2672 nwritten = fruit_pwrite_meta_netatalk(handle,
2673 fsp,
2674 buf,
2675 to_write,
2676 offset);
2677 break;
2679 default:
2680 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2681 return -1;
2684 if (nwritten != to_write) {
2685 return -1;
2689 * Return the requested amount, verified against macOS SMB server
2691 return n;
2694 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2695 files_struct *fsp, const void *data,
2696 size_t n, off_t offset)
2698 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2701 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2702 files_struct *fsp, const void *data,
2703 size_t n, off_t offset)
2705 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2708 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2709 files_struct *fsp, const void *data,
2710 size_t n, off_t offset)
2712 struct adouble *ad = NULL;
2713 ssize_t nwritten;
2714 int ret;
2716 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
2717 if (ad == NULL) {
2718 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
2719 return -1;
2722 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2723 offset + ad_getentryoff(ad, ADEID_RFORK));
2724 if (nwritten != n) {
2725 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2726 fsp_str_dbg(fsp), nwritten, n);
2727 TALLOC_FREE(ad);
2728 return -1;
2731 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2732 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2733 ret = ad_fset(handle, ad, fsp);
2734 if (ret != 0) {
2735 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2736 TALLOC_FREE(ad);
2737 return -1;
2741 TALLOC_FREE(ad);
2742 return n;
2745 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2746 files_struct *fsp, const void *data,
2747 size_t n, off_t offset)
2749 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2750 ssize_t nwritten;
2752 if (fio == NULL) {
2753 DBG_ERR("Failed to fetch fsp extension");
2754 return -1;
2757 switch (fio->config->rsrc) {
2758 case FRUIT_RSRC_STREAM:
2759 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2760 break;
2762 case FRUIT_RSRC_ADFILE:
2763 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2764 break;
2766 case FRUIT_RSRC_XATTR:
2767 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2768 break;
2770 default:
2771 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2772 return -1;
2775 return nwritten;
2778 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2779 files_struct *fsp, const void *data,
2780 size_t n, off_t offset)
2782 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2783 ssize_t nwritten;
2785 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2786 fsp_str_dbg(fsp), (intmax_t)offset, n);
2788 if (fio == NULL) {
2789 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2792 if (fio->type == ADOUBLE_META) {
2793 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2794 } else {
2795 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2798 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2799 return nwritten;
2802 struct fruit_pwrite_state {
2803 ssize_t nwritten;
2804 struct vfs_aio_state vfs_aio_state;
2807 static void fruit_pwrite_done(struct tevent_req *subreq);
2809 static struct tevent_req *fruit_pwrite_send(
2810 struct vfs_handle_struct *handle,
2811 TALLOC_CTX *mem_ctx,
2812 struct tevent_context *ev,
2813 struct files_struct *fsp,
2814 const void *data,
2815 size_t n, off_t offset)
2817 struct tevent_req *req = NULL;
2818 struct tevent_req *subreq = NULL;
2819 struct fruit_pwrite_state *state = NULL;
2820 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2822 req = tevent_req_create(mem_ctx, &state,
2823 struct fruit_pwrite_state);
2824 if (req == NULL) {
2825 return NULL;
2828 if (fruit_must_handle_aio_stream(fio)) {
2829 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
2830 if (state->nwritten != n) {
2831 if (state->nwritten != -1) {
2832 errno = EIO;
2834 tevent_req_error(req, errno);
2835 return tevent_req_post(req, ev);
2837 tevent_req_done(req);
2838 return tevent_req_post(req, ev);
2841 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
2842 data, n, offset);
2843 if (tevent_req_nomem(req, subreq)) {
2844 return tevent_req_post(req, ev);
2846 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
2847 return req;
2850 static void fruit_pwrite_done(struct tevent_req *subreq)
2852 struct tevent_req *req = tevent_req_callback_data(
2853 subreq, struct tevent_req);
2854 struct fruit_pwrite_state *state = tevent_req_data(
2855 req, struct fruit_pwrite_state);
2857 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2858 TALLOC_FREE(subreq);
2860 if (tevent_req_error(req, state->vfs_aio_state.error)) {
2861 return;
2863 tevent_req_done(req);
2866 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
2867 struct vfs_aio_state *vfs_aio_state)
2869 struct fruit_pwrite_state *state = tevent_req_data(
2870 req, struct fruit_pwrite_state);
2872 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2873 return -1;
2876 *vfs_aio_state = state->vfs_aio_state;
2877 return state->nwritten;
2881 * Helper to stat/lstat the base file of an smb_fname.
2883 static int fruit_stat_base(vfs_handle_struct *handle,
2884 struct smb_filename *smb_fname,
2885 bool follow_links)
2887 char *tmp_stream_name;
2888 int rc;
2890 tmp_stream_name = smb_fname->stream_name;
2891 smb_fname->stream_name = NULL;
2892 if (follow_links) {
2893 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2894 } else {
2895 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2897 smb_fname->stream_name = tmp_stream_name;
2899 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
2900 smb_fname->base_name,
2901 (uintmax_t)smb_fname->st.st_ex_dev,
2902 (uintmax_t)smb_fname->st.st_ex_ino);
2903 return rc;
2906 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
2907 struct smb_filename *smb_fname,
2908 bool follow_links)
2910 int ret;
2911 ino_t ino;
2913 ret = fruit_stat_base(handle, smb_fname, false);
2914 if (ret != 0) {
2915 return -1;
2918 ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
2920 if (follow_links) {
2921 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
2922 } else {
2923 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2926 smb_fname->st.st_ex_ino = ino;
2928 return ret;
2931 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
2932 struct smb_filename *smb_fname,
2933 bool follow_links)
2935 struct adouble *ad = NULL;
2937 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2938 if (ad == NULL) {
2939 DBG_INFO("fruit_stat_meta %s: %s\n",
2940 smb_fname_str_dbg(smb_fname), strerror(errno));
2941 errno = ENOENT;
2942 return -1;
2944 TALLOC_FREE(ad);
2946 /* Populate the stat struct with info from the base file. */
2947 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2948 return -1;
2950 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2951 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
2952 smb_fname->stream_name);
2953 return 0;
2956 static int fruit_stat_meta(vfs_handle_struct *handle,
2957 struct smb_filename *smb_fname,
2958 bool follow_links)
2960 struct fruit_config_data *config = NULL;
2961 int ret;
2963 SMB_VFS_HANDLE_GET_DATA(handle, config,
2964 struct fruit_config_data, return -1);
2966 switch (config->meta) {
2967 case FRUIT_META_STREAM:
2968 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
2969 break;
2971 case FRUIT_META_NETATALK:
2972 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
2973 break;
2975 default:
2976 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2977 return -1;
2980 return ret;
2983 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
2984 struct smb_filename *smb_fname,
2985 bool follow_links)
2987 struct adouble *ad = NULL;
2988 int ret;
2990 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
2991 if (ad == NULL) {
2992 errno = ENOENT;
2993 return -1;
2996 /* Populate the stat struct with info from the base file. */
2997 ret = fruit_stat_base(handle, smb_fname, follow_links);
2998 if (ret != 0) {
2999 TALLOC_FREE(ad);
3000 return -1;
3003 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3004 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3005 smb_fname->stream_name);
3006 TALLOC_FREE(ad);
3007 return 0;
3010 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3011 struct smb_filename *smb_fname,
3012 bool follow_links)
3014 int ret;
3016 if (follow_links) {
3017 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3018 } else {
3019 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3022 return ret;
3025 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3026 struct smb_filename *smb_fname,
3027 bool follow_links)
3029 #ifdef HAVE_ATTROPEN
3030 int ret;
3031 int fd = -1;
3033 /* Populate the stat struct with info from the base file. */
3034 ret = fruit_stat_base(handle, smb_fname, follow_links);
3035 if (ret != 0) {
3036 return -1;
3039 fd = attropen(smb_fname->base_name,
3040 AFPRESOURCE_EA_NETATALK,
3041 O_RDONLY);
3042 if (fd == -1) {
3043 return 0;
3046 ret = sys_fstat(fd, &smb_fname->st, false);
3047 if (ret != 0) {
3048 close(fd);
3049 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3050 AFPRESOURCE_EA_NETATALK);
3051 return -1;
3053 close(fd);
3054 fd = -1;
3056 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3057 smb_fname->stream_name);
3059 return ret;
3061 #else
3062 errno = ENOSYS;
3063 return -1;
3064 #endif
3067 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3068 struct smb_filename *smb_fname,
3069 bool follow_links)
3071 struct fruit_config_data *config = NULL;
3072 int ret;
3074 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3076 SMB_VFS_HANDLE_GET_DATA(handle, config,
3077 struct fruit_config_data, return -1);
3079 switch (config->rsrc) {
3080 case FRUIT_RSRC_STREAM:
3081 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3082 break;
3084 case FRUIT_RSRC_XATTR:
3085 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3086 break;
3088 case FRUIT_RSRC_ADFILE:
3089 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3090 break;
3092 default:
3093 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3094 return -1;
3097 return ret;
3100 static int fruit_stat(vfs_handle_struct *handle,
3101 struct smb_filename *smb_fname)
3103 int rc = -1;
3105 DEBUG(10, ("fruit_stat called for %s\n",
3106 smb_fname_str_dbg(smb_fname)));
3108 if (!is_named_stream(smb_fname)) {
3109 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3110 if (rc == 0) {
3111 update_btime(handle, smb_fname);
3113 return rc;
3117 * Note if lp_posix_paths() is true, we can never
3118 * get here as is_ntfs_stream_smb_fname() is
3119 * always false. So we never need worry about
3120 * not following links here.
3123 if (is_afpinfo_stream(smb_fname->stream_name)) {
3124 rc = fruit_stat_meta(handle, smb_fname, true);
3125 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3126 rc = fruit_stat_rsrc(handle, smb_fname, true);
3127 } else {
3128 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3131 if (rc == 0) {
3132 update_btime(handle, smb_fname);
3133 smb_fname->st.st_ex_mode &= ~S_IFMT;
3134 smb_fname->st.st_ex_mode |= S_IFREG;
3135 smb_fname->st.st_ex_blocks =
3136 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3138 return rc;
3141 static int fruit_lstat(vfs_handle_struct *handle,
3142 struct smb_filename *smb_fname)
3144 int rc = -1;
3146 DEBUG(10, ("fruit_lstat called for %s\n",
3147 smb_fname_str_dbg(smb_fname)));
3149 if (!is_named_stream(smb_fname)) {
3150 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3151 if (rc == 0) {
3152 update_btime(handle, smb_fname);
3154 return rc;
3157 if (is_afpinfo_stream(smb_fname->stream_name)) {
3158 rc = fruit_stat_meta(handle, smb_fname, false);
3159 } else if (is_afpresource_stream(smb_fname->stream_name)) {
3160 rc = fruit_stat_rsrc(handle, smb_fname, false);
3161 } else {
3162 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3165 if (rc == 0) {
3166 update_btime(handle, smb_fname);
3167 smb_fname->st.st_ex_mode &= ~S_IFMT;
3168 smb_fname->st.st_ex_mode |= S_IFREG;
3169 smb_fname->st.st_ex_blocks =
3170 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3172 return rc;
3175 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3176 files_struct *fsp,
3177 SMB_STRUCT_STAT *sbuf)
3179 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3180 struct smb_filename smb_fname;
3181 ino_t ino;
3182 int ret;
3184 if (fio == NULL) {
3185 return -1;
3188 if (fio->fake_fd) {
3189 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3190 if (ret != 0) {
3191 return -1;
3194 *sbuf = fsp->base_fsp->fsp_name->st;
3195 sbuf->st_ex_size = AFP_INFO_SIZE;
3196 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3197 return 0;
3200 smb_fname = (struct smb_filename) {
3201 .base_name = fsp->fsp_name->base_name,
3202 .twrp = fsp->fsp_name->twrp,
3205 ret = fruit_stat_base(handle, &smb_fname, false);
3206 if (ret != 0) {
3207 return -1;
3209 *sbuf = smb_fname.st;
3211 ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3213 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3214 if (ret != 0) {
3215 return -1;
3218 sbuf->st_ex_ino = ino;
3219 return 0;
3222 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3223 files_struct *fsp,
3224 SMB_STRUCT_STAT *sbuf)
3226 int ret;
3228 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3229 if (ret != 0) {
3230 return -1;
3233 *sbuf = fsp->base_fsp->fsp_name->st;
3234 sbuf->st_ex_size = AFP_INFO_SIZE;
3235 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3237 return 0;
3240 static int fruit_fstat_meta(vfs_handle_struct *handle,
3241 files_struct *fsp,
3242 SMB_STRUCT_STAT *sbuf,
3243 struct fio *fio)
3245 int ret;
3247 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3249 switch (fio->config->meta) {
3250 case FRUIT_META_STREAM:
3251 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3252 break;
3254 case FRUIT_META_NETATALK:
3255 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3256 break;
3258 default:
3259 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3260 return -1;
3263 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3264 return ret;
3267 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3268 files_struct *fsp,
3269 SMB_STRUCT_STAT *sbuf)
3271 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3274 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3275 files_struct *fsp,
3276 SMB_STRUCT_STAT *sbuf)
3278 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3281 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3282 files_struct *fsp,
3283 SMB_STRUCT_STAT *sbuf)
3285 struct adouble *ad = NULL;
3286 int ret;
3288 /* Populate the stat struct with info from the base file. */
3289 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3290 if (ret == -1) {
3291 return -1;
3294 ad = ad_get(talloc_tos(), handle,
3295 fsp->base_fsp->fsp_name,
3296 ADOUBLE_RSRC);
3297 if (ad == NULL) {
3298 DBG_ERR("ad_get [%s] failed [%s]\n",
3299 fsp_str_dbg(fsp), strerror(errno));
3300 return -1;
3303 *sbuf = fsp->base_fsp->fsp_name->st;
3304 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3305 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3307 TALLOC_FREE(ad);
3308 return 0;
3311 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3312 SMB_STRUCT_STAT *sbuf, struct fio *fio)
3314 int ret;
3316 switch (fio->config->rsrc) {
3317 case FRUIT_RSRC_STREAM:
3318 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3319 break;
3321 case FRUIT_RSRC_ADFILE:
3322 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3323 break;
3325 case FRUIT_RSRC_XATTR:
3326 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3327 break;
3329 default:
3330 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3331 return -1;
3334 return ret;
3337 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3338 SMB_STRUCT_STAT *sbuf)
3340 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3341 int rc;
3343 if (fio == NULL) {
3344 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3347 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3349 if (fio->type == ADOUBLE_META) {
3350 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3351 } else {
3352 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3355 if (rc == 0) {
3356 sbuf->st_ex_mode &= ~S_IFMT;
3357 sbuf->st_ex_mode |= S_IFREG;
3358 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3361 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3362 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3363 return rc;
3366 static NTSTATUS delete_invalid_meta_stream(
3367 vfs_handle_struct *handle,
3368 const struct smb_filename *smb_fname,
3369 TALLOC_CTX *mem_ctx,
3370 unsigned int *pnum_streams,
3371 struct stream_struct **pstreams,
3372 off_t size)
3374 struct smb_filename *sname = NULL;
3375 int ret;
3376 bool ok;
3378 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3379 if (!ok) {
3380 return NT_STATUS_INTERNAL_ERROR;
3383 if (size == 0) {
3384 return NT_STATUS_OK;
3387 sname = synthetic_smb_fname(talloc_tos(),
3388 smb_fname->base_name,
3389 AFPINFO_STREAM_NAME,
3390 NULL,
3391 smb_fname->twrp,
3393 if (sname == NULL) {
3394 return NT_STATUS_NO_MEMORY;
3397 ret = SMB_VFS_NEXT_UNLINKAT(handle,
3398 handle->conn->cwd_fsp,
3399 sname,
3401 TALLOC_FREE(sname);
3402 if (ret != 0) {
3403 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3404 return map_nt_error_from_unix(errno);
3407 return NT_STATUS_OK;
3410 static NTSTATUS fruit_streaminfo_meta_stream(
3411 vfs_handle_struct *handle,
3412 struct files_struct *fsp,
3413 const struct smb_filename *smb_fname,
3414 TALLOC_CTX *mem_ctx,
3415 unsigned int *pnum_streams,
3416 struct stream_struct **pstreams)
3418 struct stream_struct *stream = *pstreams;
3419 unsigned int num_streams = *pnum_streams;
3420 int i;
3422 for (i = 0; i < num_streams; i++) {
3423 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3424 break;
3428 if (i == num_streams) {
3429 return NT_STATUS_OK;
3432 if (stream[i].size != AFP_INFO_SIZE) {
3433 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3434 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3436 return delete_invalid_meta_stream(handle,
3437 smb_fname,
3438 mem_ctx,
3439 pnum_streams,
3440 pstreams,
3441 stream[i].size);
3445 return NT_STATUS_OK;
3448 static NTSTATUS fruit_streaminfo_meta_netatalk(
3449 vfs_handle_struct *handle,
3450 struct files_struct *fsp,
3451 const struct smb_filename *smb_fname,
3452 TALLOC_CTX *mem_ctx,
3453 unsigned int *pnum_streams,
3454 struct stream_struct **pstreams)
3456 struct stream_struct *stream = *pstreams;
3457 unsigned int num_streams = *pnum_streams;
3458 struct adouble *ad = NULL;
3459 bool is_fi_empty;
3460 int i;
3461 bool ok;
3463 /* Remove the Netatalk xattr from the list */
3464 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3465 ":" NETATALK_META_XATTR ":$DATA");
3466 if (!ok) {
3467 return NT_STATUS_NO_MEMORY;
3471 * Check if there's a AFPINFO_STREAM from the VFS streams
3472 * backend and if yes, remove it from the list
3474 for (i = 0; i < num_streams; i++) {
3475 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3476 break;
3480 if (i < num_streams) {
3481 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3482 smb_fname_str_dbg(smb_fname));
3484 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3485 AFPINFO_STREAM);
3486 if (!ok) {
3487 return NT_STATUS_INTERNAL_ERROR;
3491 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3492 if (ad == NULL) {
3493 return NT_STATUS_OK;
3496 is_fi_empty = ad_empty_finderinfo(ad);
3497 TALLOC_FREE(ad);
3499 if (is_fi_empty) {
3500 return NT_STATUS_OK;
3503 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3504 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3505 smb_roundup(handle->conn, AFP_INFO_SIZE));
3506 if (!ok) {
3507 return NT_STATUS_NO_MEMORY;
3510 return NT_STATUS_OK;
3513 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3514 struct files_struct *fsp,
3515 const struct smb_filename *smb_fname,
3516 TALLOC_CTX *mem_ctx,
3517 unsigned int *pnum_streams,
3518 struct stream_struct **pstreams)
3520 struct fruit_config_data *config = NULL;
3521 NTSTATUS status;
3523 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3524 return NT_STATUS_INTERNAL_ERROR);
3526 switch (config->meta) {
3527 case FRUIT_META_NETATALK:
3528 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3529 mem_ctx, pnum_streams,
3530 pstreams);
3531 break;
3533 case FRUIT_META_STREAM:
3534 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3535 mem_ctx, pnum_streams,
3536 pstreams);
3537 break;
3539 default:
3540 return NT_STATUS_INTERNAL_ERROR;
3543 return status;
3546 static NTSTATUS fruit_streaminfo_rsrc_stream(
3547 vfs_handle_struct *handle,
3548 struct files_struct *fsp,
3549 const struct smb_filename *smb_fname,
3550 TALLOC_CTX *mem_ctx,
3551 unsigned int *pnum_streams,
3552 struct stream_struct **pstreams)
3554 bool ok;
3556 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3557 if (!ok) {
3558 DBG_ERR("Filtering resource stream failed\n");
3559 return NT_STATUS_INTERNAL_ERROR;
3561 return NT_STATUS_OK;
3564 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3565 vfs_handle_struct *handle,
3566 struct files_struct *fsp,
3567 const struct smb_filename *smb_fname,
3568 TALLOC_CTX *mem_ctx,
3569 unsigned int *pnum_streams,
3570 struct stream_struct **pstreams)
3572 bool ok;
3574 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3575 if (!ok) {
3576 DBG_ERR("Filtering resource stream failed\n");
3577 return NT_STATUS_INTERNAL_ERROR;
3579 return NT_STATUS_OK;
3582 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3583 vfs_handle_struct *handle,
3584 struct files_struct *fsp,
3585 const struct smb_filename *smb_fname,
3586 TALLOC_CTX *mem_ctx,
3587 unsigned int *pnum_streams,
3588 struct stream_struct **pstreams)
3590 struct stream_struct *stream = *pstreams;
3591 unsigned int num_streams = *pnum_streams;
3592 struct adouble *ad = NULL;
3593 bool ok;
3594 size_t rlen;
3595 int i;
3598 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3599 * and if yes, remove it from the list
3601 for (i = 0; i < num_streams; i++) {
3602 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3603 break;
3607 if (i < num_streams) {
3608 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3609 smb_fname_str_dbg(smb_fname));
3611 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3612 AFPRESOURCE_STREAM);
3613 if (!ok) {
3614 return NT_STATUS_INTERNAL_ERROR;
3618 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3619 if (ad == NULL) {
3620 return NT_STATUS_OK;
3623 rlen = ad_getentrylen(ad, ADEID_RFORK);
3624 TALLOC_FREE(ad);
3626 if (rlen == 0) {
3627 return NT_STATUS_OK;
3630 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3631 AFPRESOURCE_STREAM_NAME, rlen,
3632 smb_roundup(handle->conn, rlen));
3633 if (!ok) {
3634 return NT_STATUS_NO_MEMORY;
3637 return NT_STATUS_OK;
3640 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3641 struct files_struct *fsp,
3642 const struct smb_filename *smb_fname,
3643 TALLOC_CTX *mem_ctx,
3644 unsigned int *pnum_streams,
3645 struct stream_struct **pstreams)
3647 struct fruit_config_data *config = NULL;
3648 NTSTATUS status;
3650 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3651 return NT_STATUS_INTERNAL_ERROR);
3653 switch (config->rsrc) {
3654 case FRUIT_RSRC_STREAM:
3655 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3656 mem_ctx, pnum_streams,
3657 pstreams);
3658 break;
3660 case FRUIT_RSRC_XATTR:
3661 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3662 mem_ctx, pnum_streams,
3663 pstreams);
3664 break;
3666 case FRUIT_RSRC_ADFILE:
3667 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3668 mem_ctx, pnum_streams,
3669 pstreams);
3670 break;
3672 default:
3673 return NT_STATUS_INTERNAL_ERROR;
3676 return status;
3679 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3680 struct stream_struct **pstreams)
3682 unsigned num_streams = *pnum_streams;
3683 struct stream_struct *streams = *pstreams;
3684 unsigned i = 0;
3686 if (!global_fruit_config.nego_aapl) {
3687 return;
3690 while (i < num_streams) {
3691 struct smb_filename smb_fname = (struct smb_filename) {
3692 .stream_name = streams[i].name,
3695 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3696 || streams[i].size > 0)
3698 i++;
3699 continue;
3702 streams[i] = streams[num_streams - 1];
3703 num_streams--;
3706 *pnum_streams = num_streams;
3709 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3710 struct files_struct *fsp,
3711 const struct smb_filename *smb_fname,
3712 TALLOC_CTX *mem_ctx,
3713 unsigned int *pnum_streams,
3714 struct stream_struct **pstreams)
3716 struct fruit_config_data *config = NULL;
3717 NTSTATUS status;
3719 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3720 return NT_STATUS_UNSUCCESSFUL);
3722 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3724 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3725 pnum_streams, pstreams);
3726 if (!NT_STATUS_IS_OK(status)) {
3727 return status;
3730 fruit_filter_empty_streams(pnum_streams, pstreams);
3732 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3733 mem_ctx, pnum_streams, pstreams);
3734 if (!NT_STATUS_IS_OK(status)) {
3735 return status;
3738 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3739 mem_ctx, pnum_streams, pstreams);
3740 if (!NT_STATUS_IS_OK(status)) {
3741 return status;
3744 return NT_STATUS_OK;
3747 static int fruit_ntimes(vfs_handle_struct *handle,
3748 const struct smb_filename *smb_fname,
3749 struct smb_file_time *ft)
3751 int rc = 0;
3752 struct adouble *ad = NULL;
3753 struct fruit_config_data *config = NULL;
3755 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3756 return -1);
3758 if ((config->meta != FRUIT_META_NETATALK) ||
3759 is_omit_timespec(&ft->create_time))
3761 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3764 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3765 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3767 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3768 if (ad == NULL) {
3769 goto exit;
3772 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3773 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3775 rc = ad_set(handle, ad, smb_fname);
3777 exit:
3779 TALLOC_FREE(ad);
3780 if (rc != 0) {
3781 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3782 return -1;
3784 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3787 static int fruit_fallocate(struct vfs_handle_struct *handle,
3788 struct files_struct *fsp,
3789 uint32_t mode,
3790 off_t offset,
3791 off_t len)
3793 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3795 if (fio == NULL) {
3796 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3799 /* Let the pwrite code path handle it. */
3800 errno = ENOSYS;
3801 return -1;
3804 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
3805 struct files_struct *fsp,
3806 off_t offset)
3808 #ifdef HAVE_ATTROPEN
3809 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3810 #endif
3811 return 0;
3814 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
3815 struct files_struct *fsp,
3816 off_t offset)
3818 int rc;
3819 struct adouble *ad = NULL;
3820 off_t ad_off;
3822 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3823 if (ad == NULL) {
3824 DBG_DEBUG("ad_get [%s] failed [%s]\n",
3825 fsp_str_dbg(fsp), strerror(errno));
3826 return -1;
3829 ad_off = ad_getentryoff(ad, ADEID_RFORK);
3831 rc = ftruncate(fsp->fh->fd, offset + ad_off);
3832 if (rc != 0) {
3833 TALLOC_FREE(ad);
3834 return -1;
3837 ad_setentrylen(ad, ADEID_RFORK, offset);
3839 rc = ad_fset(handle, ad, fsp);
3840 if (rc != 0) {
3841 DBG_ERR("ad_fset [%s] failed [%s]\n",
3842 fsp_str_dbg(fsp), strerror(errno));
3843 TALLOC_FREE(ad);
3844 return -1;
3847 TALLOC_FREE(ad);
3848 return 0;
3851 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
3852 struct files_struct *fsp,
3853 off_t offset)
3855 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3858 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3859 struct files_struct *fsp,
3860 off_t offset)
3862 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3863 int ret;
3865 if (fio == NULL) {
3866 DBG_ERR("Failed to fetch fsp extension");
3867 return -1;
3870 switch (fio->config->rsrc) {
3871 case FRUIT_RSRC_XATTR:
3872 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
3873 break;
3875 case FRUIT_RSRC_ADFILE:
3876 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
3877 break;
3879 case FRUIT_RSRC_STREAM:
3880 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
3881 break;
3883 default:
3884 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3885 return -1;
3889 return ret;
3892 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3893 struct files_struct *fsp,
3894 off_t offset)
3896 if (offset > 60) {
3897 DBG_WARNING("ftruncate %s to %jd",
3898 fsp_str_dbg(fsp), (intmax_t)offset);
3899 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
3900 errno = EOVERFLOW;
3901 return -1;
3904 /* OS X returns success but does nothing */
3905 DBG_INFO("ignoring ftruncate %s to %jd\n",
3906 fsp_str_dbg(fsp), (intmax_t)offset);
3907 return 0;
3910 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3911 struct files_struct *fsp,
3912 off_t offset)
3914 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3915 int ret;
3917 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
3918 (intmax_t)offset);
3920 if (fio == NULL) {
3921 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3924 if (fio->type == ADOUBLE_META) {
3925 ret = fruit_ftruncate_meta(handle, fsp, offset);
3926 } else {
3927 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
3930 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
3931 return ret;
3934 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3935 struct smb_request *req,
3936 struct files_struct **dirfsp,
3937 struct smb_filename *smb_fname,
3938 uint32_t access_mask,
3939 uint32_t share_access,
3940 uint32_t create_disposition,
3941 uint32_t create_options,
3942 uint32_t file_attributes,
3943 uint32_t oplock_request,
3944 const struct smb2_lease *lease,
3945 uint64_t allocation_size,
3946 uint32_t private_flags,
3947 struct security_descriptor *sd,
3948 struct ea_list *ea_list,
3949 files_struct **result,
3950 int *pinfo,
3951 const struct smb2_create_blobs *in_context_blobs,
3952 struct smb2_create_blobs *out_context_blobs)
3954 NTSTATUS status;
3955 struct fruit_config_data *config = NULL;
3956 files_struct *fsp = NULL;
3957 struct fio *fio = NULL;
3958 bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
3959 int ret;
3961 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3962 if (!NT_STATUS_IS_OK(status)) {
3963 goto fail;
3966 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3967 return NT_STATUS_UNSUCCESSFUL);
3969 if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
3970 uint32_t conv_flags = 0;
3972 if (config->wipe_intentionally_left_blank_rfork) {
3973 conv_flags |= AD_CONV_WIPE_BLANK;
3975 if (config->delete_empty_adfiles) {
3976 conv_flags |= AD_CONV_DELETE;
3979 ret = ad_convert(handle,
3980 handle->conn->cwd_fsp,
3981 smb_fname,
3982 macos_string_replace_map,
3983 conv_flags);
3984 if (ret != 0) {
3985 DBG_ERR("ad_convert() failed\n");
3986 return NT_STATUS_UNSUCCESSFUL;
3990 status = SMB_VFS_NEXT_CREATE_FILE(
3991 handle, req, dirfsp, smb_fname,
3992 access_mask, share_access,
3993 create_disposition, create_options,
3994 file_attributes, oplock_request,
3995 lease,
3996 allocation_size, private_flags,
3997 sd, ea_list, result,
3998 pinfo, in_context_blobs, out_context_blobs);
3999 if (!NT_STATUS_IS_OK(status)) {
4000 return status;
4003 fsp = *result;
4005 if (global_fruit_config.nego_aapl) {
4006 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4008 * Enable POSIX directory rename behaviour
4010 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4015 * If this is a plain open for existing files, opening an 0
4016 * byte size resource fork MUST fail with
4017 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4019 * Cf the vfs_fruit torture tests in test_rfork_create().
4021 if (global_fruit_config.nego_aapl &&
4022 create_disposition == FILE_OPEN &&
4023 smb_fname->st.st_ex_size == 0 &&
4024 is_named_stream(smb_fname))
4026 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4027 goto fail;
4030 fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4031 if (fio != NULL && pinfo != NULL && *pinfo == FILE_WAS_CREATED) {
4032 fio->created = true;
4035 if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4036 return status;
4039 if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4040 (fsp->op != NULL))
4042 status = fruit_check_access(
4043 handle, *result,
4044 access_mask,
4045 share_access);
4046 if (!NT_STATUS_IS_OK(status)) {
4047 goto fail;
4051 return status;
4053 fail:
4054 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4056 if (fsp) {
4057 close_file(req, fsp, ERROR_CLOSE);
4058 *result = fsp = NULL;
4061 return status;
4064 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4065 const struct smb_filename *fname,
4066 TALLOC_CTX *mem_ctx,
4067 struct readdir_attr_data **pattr_data)
4069 struct fruit_config_data *config = NULL;
4070 struct readdir_attr_data *attr_data;
4071 uint32_t conv_flags = 0;
4072 NTSTATUS status;
4073 int ret;
4075 SMB_VFS_HANDLE_GET_DATA(handle, config,
4076 struct fruit_config_data,
4077 return NT_STATUS_UNSUCCESSFUL);
4079 if (!global_fruit_config.nego_aapl) {
4080 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4083 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4085 if (config->wipe_intentionally_left_blank_rfork) {
4086 conv_flags |= AD_CONV_WIPE_BLANK;
4088 if (config->delete_empty_adfiles) {
4089 conv_flags |= AD_CONV_DELETE;
4092 ret = ad_convert(handle,
4093 handle->conn->cwd_fsp,
4094 fname,
4095 macos_string_replace_map,
4096 conv_flags);
4097 if (ret != 0) {
4098 DBG_ERR("ad_convert() failed\n");
4099 return NT_STATUS_UNSUCCESSFUL;
4102 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4103 if (*pattr_data == NULL) {
4104 return NT_STATUS_UNSUCCESSFUL;
4106 attr_data = *pattr_data;
4107 attr_data->type = RDATTR_AAPL;
4110 * Mac metadata: compressed FinderInfo, resource fork length
4111 * and creation date
4113 status = readdir_attr_macmeta(handle, fname, attr_data);
4114 if (!NT_STATUS_IS_OK(status)) {
4116 * Error handling is tricky: if we return failure from
4117 * this function, the corresponding directory entry
4118 * will to be passed to the client, so we really just
4119 * want to error out on fatal errors.
4121 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4122 goto fail;
4127 * UNIX mode
4129 if (config->unix_info_enabled) {
4130 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4134 * max_access
4136 if (!config->readdir_attr_max_access) {
4137 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4138 } else {
4139 status = smbd_calculate_access_mask(
4140 handle->conn,
4141 handle->conn->cwd_fsp,
4142 fname,
4143 false,
4144 SEC_FLAG_MAXIMUM_ALLOWED,
4145 &attr_data->attr_data.aapl.max_access);
4146 if (!NT_STATUS_IS_OK(status)) {
4147 goto fail;
4151 return NT_STATUS_OK;
4153 fail:
4154 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4155 fname->base_name, nt_errstr(status)));
4156 TALLOC_FREE(*pattr_data);
4157 return status;
4160 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4161 files_struct *fsp,
4162 uint32_t security_info,
4163 TALLOC_CTX *mem_ctx,
4164 struct security_descriptor **ppdesc)
4166 NTSTATUS status;
4167 struct security_ace ace;
4168 struct dom_sid sid;
4169 struct fruit_config_data *config;
4171 SMB_VFS_HANDLE_GET_DATA(handle, config,
4172 struct fruit_config_data,
4173 return NT_STATUS_UNSUCCESSFUL);
4175 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4176 mem_ctx, ppdesc);
4177 if (!NT_STATUS_IS_OK(status)) {
4178 return status;
4182 * Add MS NFS style ACEs with uid, gid and mode
4184 if (!global_fruit_config.nego_aapl) {
4185 return NT_STATUS_OK;
4187 if (!config->unix_info_enabled) {
4188 return NT_STATUS_OK;
4191 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4192 status = remove_virtual_nfs_aces(*ppdesc);
4193 if (!NT_STATUS_IS_OK(status)) {
4194 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4195 return status;
4198 /* MS NFS style mode */
4199 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4200 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4201 status = security_descriptor_dacl_add(*ppdesc, &ace);
4202 if (!NT_STATUS_IS_OK(status)) {
4203 DEBUG(1,("failed to add MS NFS style ACE\n"));
4204 return status;
4207 /* MS NFS style uid */
4208 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4209 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4210 status = security_descriptor_dacl_add(*ppdesc, &ace);
4211 if (!NT_STATUS_IS_OK(status)) {
4212 DEBUG(1,("failed to add MS NFS style ACE\n"));
4213 return status;
4216 /* MS NFS style gid */
4217 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4218 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4219 status = security_descriptor_dacl_add(*ppdesc, &ace);
4220 if (!NT_STATUS_IS_OK(status)) {
4221 DEBUG(1,("failed to add MS NFS style ACE\n"));
4222 return status;
4225 return NT_STATUS_OK;
4228 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4229 files_struct *fsp,
4230 uint32_t security_info_sent,
4231 const struct security_descriptor *orig_psd)
4233 NTSTATUS status;
4234 bool do_chmod;
4235 mode_t ms_nfs_mode = 0;
4236 int result;
4237 struct security_descriptor *psd = NULL;
4238 uint32_t orig_num_aces = 0;
4240 if (orig_psd->dacl != NULL) {
4241 orig_num_aces = orig_psd->dacl->num_aces;
4244 psd = security_descriptor_copy(talloc_tos(), orig_psd);
4245 if (psd == NULL) {
4246 return NT_STATUS_NO_MEMORY;
4249 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4251 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4252 if (!NT_STATUS_IS_OK(status)) {
4253 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4254 TALLOC_FREE(psd);
4255 return status;
4259 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4260 * sent/present flags correctly now we've removed them.
4263 if (orig_num_aces != 0) {
4265 * Are there any ACE's left ?
4267 if (psd->dacl->num_aces == 0) {
4268 /* No - clear the DACL sent/present flags. */
4269 security_info_sent &= ~SECINFO_DACL;
4270 psd->type &= ~SEC_DESC_DACL_PRESENT;
4274 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4275 if (!NT_STATUS_IS_OK(status)) {
4276 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4277 TALLOC_FREE(psd);
4278 return status;
4281 if (do_chmod) {
4282 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4283 if (result != 0) {
4284 DBG_WARNING("%s, result: %d, %04o error %s\n",
4285 fsp_str_dbg(fsp),
4286 result,
4287 (unsigned)ms_nfs_mode,
4288 strerror(errno));
4289 status = map_nt_error_from_unix(errno);
4290 TALLOC_FREE(psd);
4291 return status;
4295 TALLOC_FREE(psd);
4296 return NT_STATUS_OK;
4299 static struct vfs_offload_ctx *fruit_offload_ctx;
4301 struct fruit_offload_read_state {
4302 struct vfs_handle_struct *handle;
4303 struct tevent_context *ev;
4304 files_struct *fsp;
4305 uint32_t fsctl;
4306 DATA_BLOB token;
4309 static void fruit_offload_read_done(struct tevent_req *subreq);
4311 static struct tevent_req *fruit_offload_read_send(
4312 TALLOC_CTX *mem_ctx,
4313 struct tevent_context *ev,
4314 struct vfs_handle_struct *handle,
4315 files_struct *fsp,
4316 uint32_t fsctl,
4317 uint32_t ttl,
4318 off_t offset,
4319 size_t to_copy)
4321 struct tevent_req *req = NULL;
4322 struct tevent_req *subreq = NULL;
4323 struct fruit_offload_read_state *state = NULL;
4325 req = tevent_req_create(mem_ctx, &state,
4326 struct fruit_offload_read_state);
4327 if (req == NULL) {
4328 return NULL;
4330 *state = (struct fruit_offload_read_state) {
4331 .handle = handle,
4332 .ev = ev,
4333 .fsp = fsp,
4334 .fsctl = fsctl,
4337 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4338 fsctl, ttl, offset, to_copy);
4339 if (tevent_req_nomem(subreq, req)) {
4340 return tevent_req_post(req, ev);
4342 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4343 return req;
4346 static void fruit_offload_read_done(struct tevent_req *subreq)
4348 struct tevent_req *req = tevent_req_callback_data(
4349 subreq, struct tevent_req);
4350 struct fruit_offload_read_state *state = tevent_req_data(
4351 req, struct fruit_offload_read_state);
4352 NTSTATUS status;
4354 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4355 state->handle,
4356 state,
4357 &state->token);
4358 TALLOC_FREE(subreq);
4359 if (tevent_req_nterror(req, status)) {
4360 return;
4363 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4364 tevent_req_done(req);
4365 return;
4368 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4369 &fruit_offload_ctx);
4370 if (tevent_req_nterror(req, status)) {
4371 return;
4374 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4375 state->fsp,
4376 &state->token);
4377 if (tevent_req_nterror(req, status)) {
4378 return;
4381 tevent_req_done(req);
4382 return;
4385 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4386 struct vfs_handle_struct *handle,
4387 TALLOC_CTX *mem_ctx,
4388 DATA_BLOB *token)
4390 struct fruit_offload_read_state *state = tevent_req_data(
4391 req, struct fruit_offload_read_state);
4392 NTSTATUS status;
4394 if (tevent_req_is_nterror(req, &status)) {
4395 tevent_req_received(req);
4396 return status;
4399 token->length = state->token.length;
4400 token->data = talloc_move(mem_ctx, &state->token.data);
4402 tevent_req_received(req);
4403 return NT_STATUS_OK;
4406 struct fruit_offload_write_state {
4407 struct vfs_handle_struct *handle;
4408 off_t copied;
4409 struct files_struct *src_fsp;
4410 struct files_struct *dst_fsp;
4411 bool is_copyfile;
4414 static void fruit_offload_write_done(struct tevent_req *subreq);
4415 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4416 TALLOC_CTX *mem_ctx,
4417 struct tevent_context *ev,
4418 uint32_t fsctl,
4419 DATA_BLOB *token,
4420 off_t transfer_offset,
4421 struct files_struct *dest_fsp,
4422 off_t dest_off,
4423 off_t num)
4425 struct tevent_req *req, *subreq;
4426 struct fruit_offload_write_state *state;
4427 NTSTATUS status;
4428 struct fruit_config_data *config;
4429 off_t src_off = transfer_offset;
4430 files_struct *src_fsp = NULL;
4431 off_t to_copy = num;
4432 bool copyfile_enabled = false;
4434 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4435 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4437 SMB_VFS_HANDLE_GET_DATA(handle, config,
4438 struct fruit_config_data,
4439 return NULL);
4441 req = tevent_req_create(mem_ctx, &state,
4442 struct fruit_offload_write_state);
4443 if (req == NULL) {
4444 return NULL;
4446 state->handle = handle;
4447 state->dst_fsp = dest_fsp;
4449 switch (fsctl) {
4450 case FSCTL_SRV_COPYCHUNK:
4451 case FSCTL_SRV_COPYCHUNK_WRITE:
4452 copyfile_enabled = config->copyfile_enabled;
4453 break;
4454 default:
4455 break;
4459 * Check if this a OS X copyfile style copychunk request with
4460 * a requested chunk count of 0 that was translated to a
4461 * offload_write_send VFS call overloading the parameters src_off
4462 * = dest_off = num = 0.
4464 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4465 status = vfs_offload_token_db_fetch_fsp(
4466 fruit_offload_ctx, token, &src_fsp);
4467 if (tevent_req_nterror(req, status)) {
4468 return tevent_req_post(req, ev);
4470 state->src_fsp = src_fsp;
4472 status = vfs_stat_fsp(src_fsp);
4473 if (tevent_req_nterror(req, status)) {
4474 return tevent_req_post(req, ev);
4477 to_copy = src_fsp->fsp_name->st.st_ex_size;
4478 state->is_copyfile = true;
4481 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4482 mem_ctx,
4484 fsctl,
4485 token,
4486 transfer_offset,
4487 dest_fsp,
4488 dest_off,
4489 to_copy);
4490 if (tevent_req_nomem(subreq, req)) {
4491 return tevent_req_post(req, ev);
4494 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4495 return req;
4498 static void fruit_offload_write_done(struct tevent_req *subreq)
4500 struct tevent_req *req = tevent_req_callback_data(
4501 subreq, struct tevent_req);
4502 struct fruit_offload_write_state *state = tevent_req_data(
4503 req, struct fruit_offload_write_state);
4504 NTSTATUS status;
4505 unsigned int num_streams = 0;
4506 struct stream_struct *streams = NULL;
4507 unsigned int i;
4508 struct smb_filename *src_fname_tmp = NULL;
4509 struct smb_filename *dst_fname_tmp = NULL;
4511 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4512 subreq,
4513 &state->copied);
4514 TALLOC_FREE(subreq);
4515 if (tevent_req_nterror(req, status)) {
4516 return;
4519 if (!state->is_copyfile) {
4520 tevent_req_done(req);
4521 return;
4525 * Now copy all remaining streams. We know the share supports
4526 * streams, because we're in vfs_fruit. We don't do this async
4527 * because streams are few and small.
4529 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4530 state->src_fsp->fsp_name,
4531 req, &num_streams, &streams);
4532 if (tevent_req_nterror(req, status)) {
4533 return;
4536 if (num_streams == 1) {
4537 /* There is always one stream, ::$DATA. */
4538 tevent_req_done(req);
4539 return;
4542 for (i = 0; i < num_streams; i++) {
4543 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4544 __func__, streams[i].name, (size_t)streams[i].size));
4546 src_fname_tmp = synthetic_smb_fname(
4547 req,
4548 state->src_fsp->fsp_name->base_name,
4549 streams[i].name,
4550 NULL,
4551 state->src_fsp->fsp_name->twrp,
4552 state->src_fsp->fsp_name->flags);
4553 if (tevent_req_nomem(src_fname_tmp, req)) {
4554 return;
4557 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4558 TALLOC_FREE(src_fname_tmp);
4559 continue;
4562 dst_fname_tmp = synthetic_smb_fname(
4563 req,
4564 state->dst_fsp->fsp_name->base_name,
4565 streams[i].name,
4566 NULL,
4567 state->dst_fsp->fsp_name->twrp,
4568 state->dst_fsp->fsp_name->flags);
4569 if (tevent_req_nomem(dst_fname_tmp, req)) {
4570 TALLOC_FREE(src_fname_tmp);
4571 return;
4574 status = copy_file(req,
4575 state->handle->conn,
4576 src_fname_tmp,
4577 dst_fname_tmp,
4578 OPENX_FILE_CREATE_IF_NOT_EXIST,
4579 0, false);
4580 if (!NT_STATUS_IS_OK(status)) {
4581 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4582 smb_fname_str_dbg(src_fname_tmp),
4583 smb_fname_str_dbg(dst_fname_tmp),
4584 nt_errstr(status)));
4585 TALLOC_FREE(src_fname_tmp);
4586 TALLOC_FREE(dst_fname_tmp);
4587 tevent_req_nterror(req, status);
4588 return;
4591 TALLOC_FREE(src_fname_tmp);
4592 TALLOC_FREE(dst_fname_tmp);
4595 TALLOC_FREE(streams);
4596 TALLOC_FREE(src_fname_tmp);
4597 TALLOC_FREE(dst_fname_tmp);
4598 tevent_req_done(req);
4601 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4602 struct tevent_req *req,
4603 off_t *copied)
4605 struct fruit_offload_write_state *state = tevent_req_data(
4606 req, struct fruit_offload_write_state);
4607 NTSTATUS status;
4609 if (tevent_req_is_nterror(req, &status)) {
4610 DEBUG(1, ("server side copy chunk failed: %s\n",
4611 nt_errstr(status)));
4612 *copied = 0;
4613 tevent_req_received(req);
4614 return status;
4617 *copied = state->copied;
4618 tevent_req_received(req);
4620 return NT_STATUS_OK;
4623 static char *fruit_get_bandsize_line(char **lines, int numlines)
4625 static regex_t re;
4626 static bool re_initialized = false;
4627 int i;
4628 int ret;
4630 if (!re_initialized) {
4631 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4632 if (ret != 0) {
4633 return NULL;
4635 re_initialized = true;
4638 for (i = 0; i < numlines; i++) {
4639 regmatch_t matches[1];
4641 ret = regexec(&re, lines[i], 1, matches, 0);
4642 if (ret == 0) {
4644 * Check if the match was on the last line, sa we want
4645 * the subsequent line.
4647 if (i + 1 == numlines) {
4648 return NULL;
4650 return lines[i + 1];
4652 if (ret != REG_NOMATCH) {
4653 return NULL;
4657 return NULL;
4660 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4662 static regex_t re;
4663 static bool re_initialized = false;
4664 regmatch_t matches[2];
4665 uint64_t band_size;
4666 int ret;
4667 bool ok;
4669 if (!re_initialized) {
4670 ret = regcomp(&re,
4671 "^[[:blank:]]*"
4672 "<integer>\\([[:digit:]]*\\)</integer>$",
4674 if (ret != 0) {
4675 return false;
4677 re_initialized = true;
4680 ret = regexec(&re, line, 2, matches, 0);
4681 if (ret != 0) {
4682 DBG_ERR("regex failed [%s]\n", line);
4683 return false;
4686 line[matches[1].rm_eo] = '\0';
4688 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4689 if (!ok) {
4690 return false;
4692 *_band_size = (size_t)band_size;
4693 return true;
4697 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4698 * "band-size" key and value.
4700 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4701 const char *dir,
4702 size_t *band_size)
4704 #define INFO_PLIST_MAX_SIZE 64*1024
4705 char *plist = NULL;
4706 struct smb_filename *smb_fname = NULL;
4707 files_struct *fsp = NULL;
4708 uint8_t *file_data = NULL;
4709 char **lines = NULL;
4710 char *band_size_line = NULL;
4711 size_t plist_file_size;
4712 ssize_t nread;
4713 int numlines;
4714 int ret;
4715 bool ok = false;
4716 NTSTATUS status;
4718 plist = talloc_asprintf(talloc_tos(),
4719 "%s/%s/Info.plist",
4720 handle->conn->connectpath,
4721 dir);
4722 if (plist == NULL) {
4723 ok = false;
4724 goto out;
4727 smb_fname = synthetic_smb_fname(talloc_tos(),
4728 plist,
4729 NULL,
4730 NULL,
4733 if (smb_fname == NULL) {
4734 ok = false;
4735 goto out;
4738 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4739 if (ret != 0) {
4740 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4741 ok = true;
4742 goto out;
4745 plist_file_size = smb_fname->st.st_ex_size;
4747 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4748 DBG_INFO("%s is too large, ignoring\n", plist);
4749 ok = true;
4750 goto out;
4753 status = SMB_VFS_NEXT_CREATE_FILE(
4754 handle, /* conn */
4755 NULL, /* req */
4756 &handle->conn->cwd_fsp, /* dirfsp */
4757 smb_fname, /* fname */
4758 FILE_GENERIC_READ, /* access_mask */
4759 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4760 FILE_OPEN, /* create_disposition */
4761 0, /* create_options */
4762 0, /* file_attributes */
4763 INTERNAL_OPEN_ONLY, /* oplock_request */
4764 NULL, /* lease */
4765 0, /* allocation_size */
4766 0, /* private_flags */
4767 NULL, /* sd */
4768 NULL, /* ea_list */
4769 &fsp, /* result */
4770 NULL, /* psbuf */
4771 NULL, NULL); /* create context */
4772 if (!NT_STATUS_IS_OK(status)) {
4773 DBG_INFO("Opening [%s] failed [%s]\n",
4774 smb_fname_str_dbg(smb_fname), nt_errstr(status));
4775 ok = false;
4776 goto out;
4779 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
4780 if (file_data == NULL) {
4781 ok = false;
4782 goto out;
4785 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4786 if (nread != plist_file_size) {
4787 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4788 fsp_str_dbg(fsp), nread, plist_file_size);
4789 ok = false;
4790 goto out;
4794 status = close_file(NULL, fsp, NORMAL_CLOSE);
4795 fsp = NULL;
4796 if (!NT_STATUS_IS_OK(status)) {
4797 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4798 ok = false;
4799 goto out;
4802 lines = file_lines_parse((char *)file_data,
4803 plist_file_size,
4804 &numlines,
4805 talloc_tos());
4806 if (lines == NULL) {
4807 ok = false;
4808 goto out;
4811 band_size_line = fruit_get_bandsize_line(lines, numlines);
4812 if (band_size_line == NULL) {
4813 DBG_ERR("Didn't find band-size key in [%s]\n",
4814 smb_fname_str_dbg(smb_fname));
4815 ok = false;
4816 goto out;
4819 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
4820 if (!ok) {
4821 DBG_ERR("fruit_get_bandsize_from_line failed\n");
4822 goto out;
4825 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
4827 out:
4828 if (fsp != NULL) {
4829 status = close_file(NULL, fsp, NORMAL_CLOSE);
4830 if (!NT_STATUS_IS_OK(status)) {
4831 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
4833 fsp = NULL;
4835 TALLOC_FREE(plist);
4836 TALLOC_FREE(smb_fname);
4837 TALLOC_FREE(file_data);
4838 TALLOC_FREE(lines);
4839 return ok;
4842 struct fruit_disk_free_state {
4843 off_t total_size;
4846 static bool fruit_get_num_bands(vfs_handle_struct *handle,
4847 const char *bundle,
4848 size_t *_nbands)
4850 char *path = NULL;
4851 struct smb_filename *bands_dir = NULL;
4852 struct smb_Dir *dir_hnd = NULL;
4853 const char *dname = NULL;
4854 char *talloced = NULL;
4855 long offset = 0;
4856 size_t nbands;
4858 path = talloc_asprintf(talloc_tos(),
4859 "%s/%s/bands",
4860 handle->conn->connectpath,
4861 bundle);
4862 if (path == NULL) {
4863 return false;
4866 bands_dir = synthetic_smb_fname(talloc_tos(),
4867 path,
4868 NULL,
4869 NULL,
4872 TALLOC_FREE(path);
4873 if (bands_dir == NULL) {
4874 return false;
4877 dir_hnd = OpenDir(talloc_tos(), handle->conn, bands_dir, NULL, 0);
4878 if (dir_hnd == NULL) {
4879 TALLOC_FREE(bands_dir);
4880 return false;
4883 nbands = 0;
4885 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
4886 != NULL)
4888 if (ISDOT(dname) || ISDOTDOT(dname)) {
4889 continue;
4891 nbands++;
4893 TALLOC_FREE(dir_hnd);
4895 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
4897 TALLOC_FREE(bands_dir);
4899 *_nbands = nbands;
4900 return true;
4903 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
4904 struct fruit_disk_free_state *state,
4905 const char *name)
4907 bool ok;
4908 char *p = NULL;
4909 size_t sparsebundle_strlen = strlen("sparsebundle");
4910 size_t bandsize = 0;
4911 size_t nbands;
4912 off_t tm_size;
4914 p = strstr(name, "sparsebundle");
4915 if (p == NULL) {
4916 return true;
4919 if (p[sparsebundle_strlen] != '\0') {
4920 return true;
4923 DBG_DEBUG("Processing sparsebundle [%s]\n", name);
4925 ok = fruit_get_bandsize(handle, name, &bandsize);
4926 if (!ok) {
4928 * Beware of race conditions: this may be an uninitialized
4929 * Info.plist that a client is just creating. We don't want let
4930 * this to trigger complete failure.
4932 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
4933 return true;
4936 ok = fruit_get_num_bands(handle, name, &nbands);
4937 if (!ok) {
4939 * Beware of race conditions: this may be a backup sparsebundle
4940 * in an early stage lacking a bands subdirectory. We don't want
4941 * let this to trigger complete failure.
4943 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
4944 return true;
4948 * Arithmetic on 32-bit systems may cause overflow, depending on
4949 * size_t precision. First we check its unlikely, then we
4950 * force the precision into target off_t, then we check that
4951 * the total did not overflow either.
4953 if (bandsize > SIZE_MAX/nbands) {
4954 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
4955 bandsize, nbands);
4956 return false;
4958 tm_size = (off_t)bandsize * (off_t)nbands;
4960 if (state->total_size + tm_size < state->total_size) {
4961 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
4962 bandsize, nbands);
4963 return false;
4966 state->total_size += tm_size;
4968 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
4969 name, (intmax_t)tm_size, (intmax_t)state->total_size);
4971 return true;
4975 * Calculate used size of a TimeMachine volume
4977 * This assumes that the volume is used only for TimeMachine.
4979 * - readdir(basedir of share), then
4980 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
4981 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
4982 * - count band files in "\1.sparsebundle/bands/"
4983 * - calculate used size of all bands: band_count * band_size
4985 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
4986 const struct smb_filename *smb_fname,
4987 uint64_t *_bsize,
4988 uint64_t *_dfree,
4989 uint64_t *_dsize)
4991 struct fruit_config_data *config = NULL;
4992 struct fruit_disk_free_state state = {0};
4993 struct smb_Dir *dir_hnd = NULL;
4994 const char *dname = NULL;
4995 char *talloced = NULL;
4996 long offset = 0;
4997 uint64_t dfree;
4998 uint64_t dsize;
4999 bool ok;
5001 SMB_VFS_HANDLE_GET_DATA(handle, config,
5002 struct fruit_config_data,
5003 return UINT64_MAX);
5005 if (!config->time_machine ||
5006 config->time_machine_max_size == 0)
5008 return SMB_VFS_NEXT_DISK_FREE(handle,
5009 smb_fname,
5010 _bsize,
5011 _dfree,
5012 _dsize);
5015 dir_hnd = OpenDir(talloc_tos(), handle->conn, smb_fname, NULL, 0);
5016 if (dir_hnd == NULL) {
5017 return UINT64_MAX;
5020 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5021 != NULL)
5023 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5024 if (!ok) {
5025 TALLOC_FREE(talloced);
5026 TALLOC_FREE(dir_hnd);
5027 return UINT64_MAX;
5029 TALLOC_FREE(talloced);
5032 TALLOC_FREE(dir_hnd);
5034 dsize = config->time_machine_max_size / 512;
5035 dfree = dsize - (state.total_size / 512);
5036 if (dfree > dsize) {
5037 dfree = 0;
5040 *_bsize = 512;
5041 *_dsize = dsize;
5042 *_dfree = dfree;
5043 return dfree / 2;
5046 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5047 const SMB_STRUCT_STAT *psbuf)
5049 struct fruit_config_data *config = NULL;
5051 SMB_VFS_HANDLE_GET_DATA(handle, config,
5052 struct fruit_config_data,
5053 return 0);
5055 if (global_fruit_config.nego_aapl &&
5056 config->aapl_zero_file_id)
5058 return 0;
5061 return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5064 static struct vfs_fn_pointers vfs_fruit_fns = {
5065 .connect_fn = fruit_connect,
5066 .disk_free_fn = fruit_disk_free,
5068 /* File operations */
5069 .chmod_fn = fruit_chmod,
5070 .unlinkat_fn = fruit_unlinkat,
5071 .renameat_fn = fruit_renameat,
5072 .openat_fn = fruit_openat,
5073 .close_fn = fruit_close,
5074 .pread_fn = fruit_pread,
5075 .pwrite_fn = fruit_pwrite,
5076 .pread_send_fn = fruit_pread_send,
5077 .pread_recv_fn = fruit_pread_recv,
5078 .pwrite_send_fn = fruit_pwrite_send,
5079 .pwrite_recv_fn = fruit_pwrite_recv,
5080 .stat_fn = fruit_stat,
5081 .lstat_fn = fruit_lstat,
5082 .fstat_fn = fruit_fstat,
5083 .streaminfo_fn = fruit_streaminfo,
5084 .ntimes_fn = fruit_ntimes,
5085 .ftruncate_fn = fruit_ftruncate,
5086 .fallocate_fn = fruit_fallocate,
5087 .create_file_fn = fruit_create_file,
5088 .readdir_attr_fn = fruit_readdir_attr,
5089 .offload_read_send_fn = fruit_offload_read_send,
5090 .offload_read_recv_fn = fruit_offload_read_recv,
5091 .offload_write_send_fn = fruit_offload_write_send,
5092 .offload_write_recv_fn = fruit_offload_write_recv,
5093 .fs_file_id_fn = fruit_fs_file_id,
5095 /* NT ACL operations */
5096 .fget_nt_acl_fn = fruit_fget_nt_acl,
5097 .fset_nt_acl_fn = fruit_fset_nt_acl,
5100 static_decl_vfs;
5101 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5103 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5104 &vfs_fruit_fns);
5105 if (!NT_STATUS_IS_OK(ret)) {
5106 return ret;
5109 vfs_fruit_debug_level = debug_add_class("fruit");
5110 if (vfs_fruit_debug_level == -1) {
5111 vfs_fruit_debug_level = DBGC_VFS;
5112 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5113 "vfs_fruit_init"));
5114 } else {
5115 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5116 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5119 return ret;