vfs: change openat propotype to match linux openat2
[samba.git] / source3 / modules / vfs_fruit.c
1 /*
2  * OS X and Netatalk interoperability VFS module for Samba-3.x
3  *
4  * Copyright (C) Ralph Boehme, 2013, 2014
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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"
38
39 /*
40  * Enhanced OS X and Netatalk compatibility
41  * ========================================
42  *
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:
46  *
47  *   vfs modules = catia fruit streams_xattr
48  *
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.
52  *
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.
57  *
58  * Finally, open modes are optionally checked against Netatalk AFP
59  * share modes.
60  *
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.
67  *
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.
72  *
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".
76  *
77  *
78  * Extended Attributes
79  * ===================
80  *
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:
86  *
87  *   streams_xattr:prefix = user.
88  *   streams_xattr:store_stream_type = false
89  *
90  *
91  * TODO
92  * ====
93  *
94  * - log diagnostic if any needed VFS module is not loaded
95  *   (eg with lp_vfs_objects())
96  * - add tests
97  */
98
99 static int vfs_fruit_debug_level = DBGC_VFS;
100
101 static struct global_fruit_config {
102         bool nego_aapl; /* client negotiated AAPL */
103
104 } global_fruit_config;
105
106 #undef DBGC_CLASS
107 #define DBGC_CLASS vfs_fruit_debug_level
108
109 #define FRUIT_PARAM_TYPE_NAME "fruit"
110
111 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
112
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};
117
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;
136
137         /*
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.
143          */
144         bool readdir_attr_rsize;
145         bool readdir_attr_finder_info;
146         bool readdir_attr_max_access;
147         /* Recursion guard. Will go away when we have STATX. */
148         bool in_openat_pathref_fsp;
149 };
150
151 static const struct enum_list fruit_rsrc[] = {
152         {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
153         {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
154         {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
155         { -1, NULL}
156 };
157
158 static const struct enum_list fruit_meta[] = {
159         {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
160         {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
161         { -1, NULL}
162 };
163
164 static const struct enum_list fruit_locking[] = {
165         {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
166         {FRUIT_LOCKING_NONE, "none"},
167         { -1, NULL}
168 };
169
170 static const struct enum_list fruit_encoding[] = {
171         {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
172         {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
173         { -1, NULL}
174 };
175
176 struct fio {
177         vfs_handle_struct *handle;
178         files_struct *fsp; /* backlink to itself */
179
180         /* tcon config handle */
181         struct fruit_config_data *config;
182
183         /* Backend fsp for AppleDouble file, can be NULL */
184         files_struct *ad_fsp;
185         /* link from adouble_open_from_base_fsp() to fio */
186         struct fio *real_fio;
187
188         /* Denote stream type, meta or rsrc */
189         adouble_type_t type;
190
191         /*
192          * AFP_AfpInfo stream created, but not written yet, thus still a fake
193          * pipe fd. This is set to true in fruit_open_meta if there was no
194          * existing stream but the caller requested O_CREAT. It is later set to
195          * false when we get a write on the stream that then does open and
196          * create the stream.
197          */
198         bool fake_fd;
199         int flags;
200         int mode;
201 };
202
203 /*****************************************************************************
204  * Helper functions
205  *****************************************************************************/
206
207 static struct adouble *ad_get_meta_fsp(TALLOC_CTX *ctx,
208                                        vfs_handle_struct *handle,
209                                        const struct smb_filename *smb_fname)
210 {
211         NTSTATUS status;
212         struct adouble *ad = NULL;
213         struct smb_filename *smb_fname_cp = NULL;
214         struct fruit_config_data *config = NULL;
215
216         if (smb_fname->fsp != NULL) {
217                 return ad_get(ctx, handle, smb_fname, ADOUBLE_META);
218         }
219
220         SMB_VFS_HANDLE_GET_DATA(handle,
221                                 config,
222                                 struct fruit_config_data,
223                                 return NULL);
224
225         if (config->in_openat_pathref_fsp) {
226                 return NULL;
227         }
228
229         smb_fname_cp = cp_smb_filename(ctx,
230                                        smb_fname);
231         if (smb_fname_cp == NULL) {
232                 return NULL;
233         }
234         TALLOC_FREE(smb_fname_cp->stream_name);
235         config->in_openat_pathref_fsp = true;
236         status = openat_pathref_fsp(handle->conn->cwd_fsp,
237                                     smb_fname_cp);
238         config->in_openat_pathref_fsp = false;
239         if (!NT_STATUS_IS_OK(status)) {
240                 TALLOC_FREE(smb_fname_cp);
241                 return NULL;
242         }
243
244         ad = ad_get(ctx, handle, smb_fname_cp, ADOUBLE_META);
245         TALLOC_FREE(smb_fname_cp);
246         return ad;
247 }
248
249 static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
250                                           files_struct *fsp)
251 {
252         struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
253
254         if (fio == NULL) {
255                 return NULL;
256         }
257
258         if (fio->real_fio != NULL) {
259                 /*
260                  * This is an fsp from adouble_open_from_base_fsp()
261                  * we should just pass this to the next
262                  * module.
263                  */
264                 return NULL;
265         }
266
267         return fio;
268 }
269
270 /**
271  * Initialize config struct from our smb.conf config parameters
272  **/
273 static int init_fruit_config(vfs_handle_struct *handle)
274 {
275         struct fruit_config_data *config;
276         int enumval;
277         const char *tm_size_str = NULL;
278
279         config = talloc_zero(handle->conn, struct fruit_config_data);
280         if (!config) {
281                 DEBUG(1, ("talloc_zero() failed\n"));
282                 errno = ENOMEM;
283                 return -1;
284         }
285
286         /*
287          * Versions up to Samba 4.5.x had a spelling bug in the
288          * fruit:resource option calling lp_parm_enum with
289          * "res*s*ource" (ie two s).
290          *
291          * In Samba 4.6 we accept both the wrong and the correct
292          * spelling, in Samba 4.7 the bad spelling will be removed.
293          */
294         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
295                                "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
296         if (enumval == -1) {
297                 DEBUG(1, ("value for %s: resource type unknown\n",
298                           FRUIT_PARAM_TYPE_NAME));
299                 return -1;
300         }
301         config->rsrc = (enum fruit_rsrc)enumval;
302
303         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
304                                "resource", fruit_rsrc, enumval);
305         if (enumval == -1) {
306                 DEBUG(1, ("value for %s: resource type unknown\n",
307                           FRUIT_PARAM_TYPE_NAME));
308                 return -1;
309         }
310         config->rsrc = (enum fruit_rsrc)enumval;
311
312         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
313                                "metadata", fruit_meta, FRUIT_META_NETATALK);
314         if (enumval == -1) {
315                 DEBUG(1, ("value for %s: metadata type unknown\n",
316                           FRUIT_PARAM_TYPE_NAME));
317                 return -1;
318         }
319         config->meta = (enum fruit_meta)enumval;
320
321         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
322                                "locking", fruit_locking, FRUIT_LOCKING_NONE);
323         if (enumval == -1) {
324                 DEBUG(1, ("value for %s: locking type unknown\n",
325                           FRUIT_PARAM_TYPE_NAME));
326                 return -1;
327         }
328         config->locking = (enum fruit_locking)enumval;
329
330         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
331                                "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
332         if (enumval == -1) {
333                 DEBUG(1, ("value for %s: encoding type unknown\n",
334                           FRUIT_PARAM_TYPE_NAME));
335                 return -1;
336         }
337         config->encoding = (enum fruit_encoding)enumval;
338
339         if (config->rsrc == FRUIT_RSRC_ADFILE) {
340                 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
341                                                         FRUIT_PARAM_TYPE_NAME,
342                                                         "veto_appledouble",
343                                                         true);
344         }
345
346         config->use_aapl = lp_parm_bool(
347                 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
348
349         config->time_machine = lp_parm_bool(
350                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
351
352         config->unix_info_enabled = lp_parm_bool(
353                 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
354
355         config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
356                                            "copyfile", false);
357
358         config->posix_rename = lp_parm_bool(
359                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
360
361         config->aapl_zero_file_id =
362             lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
363                          "zero_file_id", true);
364
365         config->readdir_attr_rsize = lp_parm_bool(
366                 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
367
368         config->readdir_attr_finder_info = lp_parm_bool(
369                 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
370
371         config->readdir_attr_max_access = lp_parm_bool(
372                 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
373
374         config->model = lp_parm_const_string(
375                 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
376
377         tm_size_str = lp_parm_const_string(
378                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
379                 "time machine max size", NULL);
380         if (tm_size_str != NULL) {
381                 config->time_machine_max_size = conv_str_size(tm_size_str);
382         }
383
384         config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
385                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
386                 "wipe_intentionally_left_blank_rfork", false);
387
388         config->delete_empty_adfiles = lp_parm_bool(
389                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
390                 "delete_empty_adfiles", false);
391
392         SMB_VFS_HANDLE_SET_DATA(handle, config,
393                                 NULL, struct fruit_config_data,
394                                 return -1);
395
396         return 0;
397 }
398
399 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
400                              struct stream_struct **streams,
401                              const char *name, off_t size,
402                              off_t alloc_size)
403 {
404         struct stream_struct *tmp;
405
406         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
407                              (*num_streams)+1);
408         if (tmp == NULL) {
409                 return false;
410         }
411
412         tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
413         if (tmp[*num_streams].name == NULL) {
414                 return false;
415         }
416
417         tmp[*num_streams].size = size;
418         tmp[*num_streams].alloc_size = alloc_size;
419
420         *streams = tmp;
421         *num_streams += 1;
422         return true;
423 }
424
425 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
426                                      struct stream_struct **streams)
427 {
428         struct stream_struct *tmp = *streams;
429         unsigned int i;
430
431         if (*num_streams == 0) {
432                 return true;
433         }
434
435         for (i = 0; i < *num_streams; i++) {
436                 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
437                         break;
438                 }
439         }
440
441         if (i == *num_streams) {
442                 return true;
443         }
444
445         if (tmp[i].size > 0) {
446                 return true;
447         }
448
449         TALLOC_FREE(tmp[i].name);
450         ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
451         *num_streams -= 1;
452         return true;
453 }
454
455 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
456                              struct stream_struct **streams,
457                              const char *name)
458 {
459         struct stream_struct *tmp = *streams;
460         unsigned int i;
461
462         if (*num_streams == 0) {
463                 return true;
464         }
465
466         for (i = 0; i < *num_streams; i++) {
467                 if (strequal_m(tmp[i].name, name)) {
468                         break;
469                 }
470         }
471
472         if (i == *num_streams) {
473                 return true;
474         }
475
476         TALLOC_FREE(tmp[i].name);
477         ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
478         *num_streams -= 1;
479         return true;
480 }
481
482 static bool ad_empty_finderinfo(const struct adouble *ad)
483 {
484         int cmp;
485         char emptybuf[ADEDLEN_FINDERI] = {0};
486         char *fi = NULL;
487
488         fi = ad_get_entry(ad, ADEID_FINDERI);
489         if (fi == NULL) {
490                 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
491                 return false;
492         }
493
494         cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
495         return (cmp == 0);
496 }
497
498 static bool ai_empty_finderinfo(const AfpInfo *ai)
499 {
500         int cmp;
501         char emptybuf[ADEDLEN_FINDERI] = {0};
502
503         cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
504         return (cmp == 0);
505 }
506
507 /**
508  * Update btime with btime from Netatalk
509  **/
510 static void update_btime(vfs_handle_struct *handle,
511                          struct smb_filename *smb_fname)
512 {
513         uint32_t t;
514         struct timespec creation_time = {0};
515         struct adouble *ad;
516         struct fruit_config_data *config = NULL;
517
518         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
519                                 return);
520
521         switch (config->meta) {
522         case FRUIT_META_STREAM:
523                 return;
524         case FRUIT_META_NETATALK:
525                 /* Handled below */
526                 break;
527         default:
528                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
529                 return;
530         }
531
532         ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
533         if (ad == NULL) {
534                 return;
535         }
536         if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
537                 TALLOC_FREE(ad);
538                 return;
539         }
540         TALLOC_FREE(ad);
541
542         creation_time.tv_sec = convert_uint32_t_to_time_t(t);
543         update_stat_ex_create_time(&smb_fname->st, creation_time);
544
545         return;
546 }
547
548 /**
549  * Map an access mask to a Netatalk single byte byte range lock
550  **/
551 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
552                                     uint32_t access_mask)
553 {
554         off_t offset;
555
556         switch (access_mask) {
557         case FILE_READ_DATA:
558                 offset = AD_FILELOCK_OPEN_RD;
559                 break;
560
561         case FILE_WRITE_DATA:
562         case FILE_APPEND_DATA:
563                 offset = AD_FILELOCK_OPEN_WR;
564                 break;
565
566         default:
567                 offset = AD_FILELOCK_OPEN_NONE;
568                 break;
569         }
570
571         if (fork_type == APPLE_FORK_RSRC) {
572                 if (offset == AD_FILELOCK_OPEN_NONE) {
573                         offset = AD_FILELOCK_RSRC_OPEN_NONE;
574                 } else {
575                         offset += 2;
576                 }
577         }
578
579         return offset;
580 }
581
582 /**
583  * Map a deny mode to a Netatalk brl
584  **/
585 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
586                                       uint32_t deny_mode)
587 {
588         off_t offset = 0;
589
590         switch (deny_mode) {
591         case DENY_READ:
592                 offset = AD_FILELOCK_DENY_RD;
593                 break;
594
595         case DENY_WRITE:
596                 offset = AD_FILELOCK_DENY_WR;
597                 break;
598
599         default:
600                 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
601         }
602
603         if (fork_type == APPLE_FORK_RSRC) {
604                 offset += 2;
605         }
606
607         return offset;
608 }
609
610 /**
611  * Call fcntl() with an exclusive F_GETLK request in order to
612  * determine if there's an existing shared lock
613  *
614  * @return true if the requested lock was found or any error occurred
615  *         false if the lock was not found
616  **/
617 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
618 {
619         bool result;
620         off_t offset = in_offset;
621         off_t len = 1;
622         int type = F_WRLCK;
623         pid_t pid = 0;
624
625         result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
626         if (result == false) {
627                 return true;
628         }
629
630         if (type != F_UNLCK) {
631                 return true;
632         }
633
634         return false;
635 }
636
637 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
638                                    files_struct *fsp,
639                                    uint32_t access_mask,
640                                    uint32_t share_mode)
641 {
642         NTSTATUS status = NT_STATUS_OK;
643         off_t off;
644         bool share_for_read = (share_mode & FILE_SHARE_READ);
645         bool share_for_write = (share_mode & FILE_SHARE_WRITE);
646         bool netatalk_already_open_for_reading = false;
647         bool netatalk_already_open_for_writing = false;
648         bool netatalk_already_open_with_deny_read = false;
649         bool netatalk_already_open_with_deny_write = false;
650         struct GUID req_guid = GUID_random();
651
652         /* FIXME: hardcoded data fork, add resource fork */
653         enum apple_fork fork_type = APPLE_FORK_DATA;
654
655         DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
656                   fsp_str_dbg(fsp),
657                   access_mask & FILE_READ_DATA ? "READ" :"-",
658                   access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
659                   share_mode);
660
661         if (fsp_get_io_fd(fsp) == -1) {
662                 return NT_STATUS_OK;
663         }
664
665         /* Read NetATalk opens and deny modes on the file. */
666         netatalk_already_open_for_reading = test_netatalk_lock(fsp,
667                                 access_to_netatalk_brl(fork_type,
668                                         FILE_READ_DATA));
669
670         netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
671                                 denymode_to_netatalk_brl(fork_type,
672                                         DENY_READ));
673
674         netatalk_already_open_for_writing = test_netatalk_lock(fsp,
675                                 access_to_netatalk_brl(fork_type,
676                                         FILE_WRITE_DATA));
677
678         netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
679                                 denymode_to_netatalk_brl(fork_type,
680                                         DENY_WRITE));
681
682         /* If there are any conflicts - sharing violation. */
683         if ((access_mask & FILE_READ_DATA) &&
684                         netatalk_already_open_with_deny_read) {
685                 return NT_STATUS_SHARING_VIOLATION;
686         }
687
688         if (!share_for_read &&
689                         netatalk_already_open_for_reading) {
690                 return NT_STATUS_SHARING_VIOLATION;
691         }
692
693         if ((access_mask & FILE_WRITE_DATA) &&
694                         netatalk_already_open_with_deny_write) {
695                 return NT_STATUS_SHARING_VIOLATION;
696         }
697
698         if (!share_for_write &&
699                         netatalk_already_open_for_writing) {
700                 return NT_STATUS_SHARING_VIOLATION;
701         }
702
703         if (!(access_mask & FILE_READ_DATA)) {
704                 /*
705                  * Nothing we can do here, we need read access
706                  * to set locks.
707                  */
708                 return NT_STATUS_OK;
709         }
710
711         /* Set NetAtalk locks matching our access */
712         if (access_mask & FILE_READ_DATA) {
713                 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
714                 req_guid.time_hi_and_version = __LINE__;
715                 status = do_lock(
716                         fsp,
717                         talloc_tos(),
718                         &req_guid,
719                         fsp->op->global->open_persistent_id,
720                         1,
721                         off,
722                         READ_LOCK,
723                         POSIX_LOCK,
724                         NULL,
725                         NULL);
726
727                 if (!NT_STATUS_IS_OK(status))  {
728                         return status;
729                 }
730         }
731
732         if (!share_for_read) {
733                 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
734                 req_guid.time_hi_and_version = __LINE__;
735                 status = do_lock(
736                         fsp,
737                         talloc_tos(),
738                         &req_guid,
739                         fsp->op->global->open_persistent_id,
740                         1,
741                         off,
742                         READ_LOCK,
743                         POSIX_LOCK,
744                         NULL,
745                         NULL);
746
747                 if (!NT_STATUS_IS_OK(status)) {
748                         return status;
749                 }
750         }
751
752         if (access_mask & FILE_WRITE_DATA) {
753                 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
754                 req_guid.time_hi_and_version = __LINE__;
755                 status = do_lock(
756                         fsp,
757                         talloc_tos(),
758                         &req_guid,
759                         fsp->op->global->open_persistent_id,
760                         1,
761                         off,
762                         READ_LOCK,
763                         POSIX_LOCK,
764                         NULL,
765                         NULL);
766
767                 if (!NT_STATUS_IS_OK(status)) {
768                         return status;
769                 }
770         }
771
772         if (!share_for_write) {
773                 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
774                 req_guid.time_hi_and_version = __LINE__;
775                 status = do_lock(
776                         fsp,
777                         talloc_tos(),
778                         &req_guid,
779                         fsp->op->global->open_persistent_id,
780                         1,
781                         off,
782                         READ_LOCK,
783                         POSIX_LOCK,
784                         NULL,
785                         NULL);
786
787                 if (!NT_STATUS_IS_OK(status)) {
788                         return status;
789                 }
790         }
791
792         return NT_STATUS_OK;
793 }
794
795 static NTSTATUS check_aapl(vfs_handle_struct *handle,
796                            struct smb_request *req,
797                            const struct smb2_create_blobs *in_context_blobs,
798                            struct smb2_create_blobs *out_context_blobs)
799 {
800         struct fruit_config_data *config;
801         NTSTATUS status;
802         struct smb2_create_blob *aapl = NULL;
803         uint32_t cmd;
804         bool ok;
805         uint8_t p[16];
806         DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
807         uint64_t req_bitmap, client_caps;
808         uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
809         smb_ucs2_t *model;
810         size_t modellen;
811
812         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
813                                 return NT_STATUS_UNSUCCESSFUL);
814
815         if (!config->use_aapl
816             || in_context_blobs == NULL
817             || out_context_blobs == NULL) {
818                 return NT_STATUS_OK;
819         }
820
821         aapl = smb2_create_blob_find(in_context_blobs,
822                                      SMB2_CREATE_TAG_AAPL);
823         if (aapl == NULL) {
824                 return NT_STATUS_OK;
825         }
826
827         if (aapl->data.length != 24) {
828                 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
829                           (uintmax_t)aapl->data.length));
830                 return NT_STATUS_INVALID_PARAMETER;
831         }
832
833         cmd = IVAL(aapl->data.data, 0);
834         if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
835                 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
836                 return NT_STATUS_INVALID_PARAMETER;
837         }
838
839         req_bitmap = BVAL(aapl->data.data, 8);
840         client_caps = BVAL(aapl->data.data, 16);
841
842         SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
843         SIVAL(p, 4, 0);
844         SBVAL(p, 8, req_bitmap);
845         ok = data_blob_append(req, &blob, p, 16);
846         if (!ok) {
847                 return NT_STATUS_UNSUCCESSFUL;
848         }
849
850         if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
851                 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
852                     (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
853                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
854                         config->readdir_attr_enabled = true;
855                 }
856
857                 if (config->use_copyfile) {
858                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
859                         config->copyfile_enabled = true;
860                 }
861
862                 /*
863                  * The client doesn't set the flag, so we can't check
864                  * for it and just set it unconditionally
865                  */
866                 if (config->unix_info_enabled) {
867                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
868                 }
869
870                 SBVAL(p, 0, server_caps);
871                 ok = data_blob_append(req, &blob, p, 8);
872                 if (!ok) {
873                         return NT_STATUS_UNSUCCESSFUL;
874                 }
875         }
876
877         if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
878                 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
879                 uint64_t caps = 0;
880
881                 switch (val) {
882                 case Auto:
883                         break;
884
885                 case True:
886                         caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
887                         break;
888
889                 default:
890                         break;
891                 }
892
893                 if (config->time_machine) {
894                         caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
895                 }
896
897                 SBVAL(p, 0, caps);
898
899                 ok = data_blob_append(req, &blob, p, 8);
900                 if (!ok) {
901                         return NT_STATUS_UNSUCCESSFUL;
902                 }
903         }
904
905         if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
906                 ok = convert_string_talloc(req,
907                                            CH_UNIX, CH_UTF16LE,
908                                            config->model, strlen(config->model),
909                                            &model, &modellen);
910                 if (!ok) {
911                         return NT_STATUS_UNSUCCESSFUL;
912                 }
913
914                 SIVAL(p, 0, 0);
915                 SIVAL(p + 4, 0, modellen);
916                 ok = data_blob_append(req, &blob, p, 8);
917                 if (!ok) {
918                         talloc_free(model);
919                         return NT_STATUS_UNSUCCESSFUL;
920                 }
921
922                 ok = data_blob_append(req, &blob, model, modellen);
923                 talloc_free(model);
924                 if (!ok) {
925                         return NT_STATUS_UNSUCCESSFUL;
926                 }
927         }
928
929         status = smb2_create_blob_add(out_context_blobs,
930                                       out_context_blobs,
931                                       SMB2_CREATE_TAG_AAPL,
932                                       blob);
933         if (NT_STATUS_IS_OK(status)) {
934                 global_fruit_config.nego_aapl = true;
935         }
936
937         return status;
938 }
939
940 static bool readdir_attr_meta_finderi_stream(
941         struct vfs_handle_struct *handle,
942         const struct smb_filename *smb_fname,
943         AfpInfo *ai)
944 {
945         struct smb_filename *stream_name = NULL;
946         files_struct *fsp = NULL;
947         ssize_t nread;
948         NTSTATUS status;
949         bool ok;
950         uint8_t buf[AFP_INFO_SIZE];
951
952         status = synthetic_pathref(talloc_tos(),
953                                    handle->conn->cwd_fsp,
954                                    smb_fname->base_name,
955                                    AFPINFO_STREAM_NAME,
956                                    NULL,
957                                    smb_fname->twrp,
958                                    smb_fname->flags,
959                                    &stream_name);
960         if (!NT_STATUS_IS_OK(status)) {
961                 return false;
962         }
963
964         status = SMB_VFS_CREATE_FILE(
965                 handle->conn,                           /* conn */
966                 NULL,                                   /* req */
967                 NULL,                                   /* dirfsp */
968                 stream_name,                            /* fname */
969                 FILE_READ_DATA,                         /* access_mask */
970                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
971                         FILE_SHARE_DELETE),
972                 FILE_OPEN,                              /* create_disposition*/
973                 0,                                      /* create_options */
974                 0,                                      /* file_attributes */
975                 INTERNAL_OPEN_ONLY,                     /* oplock_request */
976                 NULL,                                   /* lease */
977                 0,                                      /* allocation_size */
978                 0,                                      /* private_flags */
979                 NULL,                                   /* sd */
980                 NULL,                                   /* ea_list */
981                 &fsp,                                   /* result */
982                 NULL,                                   /* pinfo */
983                 NULL, NULL);                            /* create context */
984
985         TALLOC_FREE(stream_name);
986
987         if (!NT_STATUS_IS_OK(status)) {
988                 return false;
989         }
990
991         nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
992         if (nread != AFP_INFO_SIZE) {
993                 DBG_ERR("short read [%s] [%zd/%d]\n",
994                         smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
995                 ok = false;
996                 goto fail;
997         }
998
999         memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
1000                AFP_FinderSize);
1001
1002         ok = true;
1003
1004 fail:
1005         if (fsp != NULL) {
1006                 close_file_free(NULL, &fsp, NORMAL_CLOSE);
1007         }
1008
1009         return ok;
1010 }
1011
1012 static bool readdir_attr_meta_finderi_netatalk(
1013         struct vfs_handle_struct *handle,
1014         const struct smb_filename *smb_fname,
1015         AfpInfo *ai)
1016 {
1017         struct adouble *ad = NULL;
1018         char *p = NULL;
1019
1020         ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
1021         if (ad == NULL) {
1022                 return false;
1023         }
1024
1025         p = ad_get_entry(ad, ADEID_FINDERI);
1026         if (p == NULL) {
1027                 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
1028                 TALLOC_FREE(ad);
1029                 return false;
1030         }
1031
1032         memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
1033         TALLOC_FREE(ad);
1034         return true;
1035 }
1036
1037 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
1038                                       const struct smb_filename *smb_fname,
1039                                       struct readdir_attr_data *attr_data)
1040 {
1041         struct fruit_config_data *config = NULL;
1042         uint32_t date_added;
1043         AfpInfo ai = {0};
1044         bool ok;
1045
1046         SMB_VFS_HANDLE_GET_DATA(handle, config,
1047                                 struct fruit_config_data,
1048                                 return false);
1049
1050         switch (config->meta) {
1051         case FRUIT_META_NETATALK:
1052                 ok = readdir_attr_meta_finderi_netatalk(
1053                         handle, smb_fname, &ai);
1054                 break;
1055
1056         case FRUIT_META_STREAM:
1057                 ok = readdir_attr_meta_finderi_stream(
1058                         handle, smb_fname, &ai);
1059                 break;
1060
1061         default:
1062                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1063                 return false;
1064         }
1065
1066         if (!ok) {
1067                 /* Don't bother with errors, it's likely ENOENT */
1068                 return true;
1069         }
1070
1071         if (S_ISREG(smb_fname->st.st_ex_mode)) {
1072                 /* finder_type */
1073                 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1074                        &ai.afpi_FinderInfo[0], 4);
1075
1076                 /* finder_creator */
1077                 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1078                        &ai.afpi_FinderInfo[4], 4);
1079         }
1080
1081         /* finder_flags */
1082         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1083                &ai.afpi_FinderInfo[8], 2);
1084
1085         /* finder_ext_flags */
1086         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1087                &ai.afpi_FinderInfo[24], 2);
1088
1089         /* creation date */
1090         date_added = convert_time_t_to_uint32_t(
1091                 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1092
1093         RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1094
1095         return true;
1096 }
1097
1098 static uint64_t readdir_attr_rfork_size_adouble(
1099         struct vfs_handle_struct *handle,
1100         const struct smb_filename *smb_fname)
1101 {
1102         struct adouble *ad = NULL;
1103         uint64_t rfork_size;
1104
1105         ad = ad_get(talloc_tos(), handle, smb_fname,
1106                     ADOUBLE_RSRC);
1107         if (ad == NULL) {
1108                 return 0;
1109         }
1110
1111         rfork_size = ad_getentrylen(ad, ADEID_RFORK);
1112         TALLOC_FREE(ad);
1113
1114         return rfork_size;
1115 }
1116
1117 static uint64_t readdir_attr_rfork_size_stream(
1118         struct vfs_handle_struct *handle,
1119         const struct smb_filename *smb_fname)
1120 {
1121         struct smb_filename *stream_name = NULL;
1122         int ret;
1123         uint64_t rfork_size;
1124
1125         stream_name = synthetic_smb_fname(talloc_tos(),
1126                                           smb_fname->base_name,
1127                                           AFPRESOURCE_STREAM_NAME,
1128                                           NULL,
1129                                           smb_fname->twrp,
1130                                           0);
1131         if (stream_name == NULL) {
1132                 return 0;
1133         }
1134
1135         ret = SMB_VFS_STAT(handle->conn, stream_name);
1136         if (ret != 0) {
1137                 TALLOC_FREE(stream_name);
1138                 return 0;
1139         }
1140
1141         rfork_size = stream_name->st.st_ex_size;
1142         TALLOC_FREE(stream_name);
1143
1144         return rfork_size;
1145 }
1146
1147 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
1148                                         const struct smb_filename *smb_fname)
1149 {
1150         struct fruit_config_data *config = NULL;
1151         uint64_t rfork_size;
1152
1153         SMB_VFS_HANDLE_GET_DATA(handle, config,
1154                                 struct fruit_config_data,
1155                                 return 0);
1156
1157         switch (config->rsrc) {
1158         case FRUIT_RSRC_ADFILE:
1159                 rfork_size = readdir_attr_rfork_size_adouble(handle,
1160                                                              smb_fname);
1161                 break;
1162
1163         case FRUIT_RSRC_XATTR:
1164         case FRUIT_RSRC_STREAM:
1165                 rfork_size = readdir_attr_rfork_size_stream(handle,
1166                                                             smb_fname);
1167                 break;
1168
1169         default:
1170                 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1171                 rfork_size = 0;
1172                 break;
1173         }
1174
1175         return rfork_size;
1176 }
1177
1178 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1179                                      const struct smb_filename *smb_fname,
1180                                      struct readdir_attr_data *attr_data)
1181 {
1182         NTSTATUS status = NT_STATUS_OK;
1183         struct fruit_config_data *config = NULL;
1184         bool ok;
1185
1186         SMB_VFS_HANDLE_GET_DATA(handle, config,
1187                                 struct fruit_config_data,
1188                                 return NT_STATUS_UNSUCCESSFUL);
1189
1190
1191         /* Ensure we return a default value in the creation_date field */
1192         RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1193
1194         /*
1195          * Resource fork length
1196          */
1197
1198         if (config->readdir_attr_rsize) {
1199                 uint64_t rfork_size;
1200
1201                 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
1202                 attr_data->attr_data.aapl.rfork_size = rfork_size;
1203         }
1204
1205         /*
1206          * FinderInfo
1207          */
1208
1209         if (config->readdir_attr_finder_info) {
1210                 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
1211                 if (!ok) {
1212                         status = NT_STATUS_INTERNAL_ERROR;
1213                 }
1214         }
1215
1216         return status;
1217 }
1218
1219 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
1220 {
1221         NTSTATUS status;
1222         uint32_t i;
1223
1224         if (psd->dacl == NULL) {
1225                 return NT_STATUS_OK;
1226         }
1227
1228         for (i = 0; i < psd->dacl->num_aces; i++) {
1229                 /* MS NFS style mode/uid/gid */
1230                 int cmp = dom_sid_compare_domain(
1231                                 &global_sid_Unix_NFS,
1232                                 &psd->dacl->aces[i].trustee);
1233                 if (cmp != 0) {
1234                         /* Normal ACE entry. */
1235                         continue;
1236                 }
1237
1238                 /*
1239                  * security_descriptor_dacl_del()
1240                  * *must* return NT_STATUS_OK as we know
1241                  * we have something to remove.
1242                  */
1243
1244                 status = security_descriptor_dacl_del(psd,
1245                                 &psd->dacl->aces[i].trustee);
1246                 if (!NT_STATUS_IS_OK(status)) {
1247                         DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1248                                 nt_errstr(status));
1249                         return status;
1250                 }
1251
1252                 /*
1253                  * security_descriptor_dacl_del() may delete more
1254                  * then one entry subsequent to this one if the
1255                  * SID matches, but we only need to ensure that
1256                  * we stay looking at the same element in the array.
1257                  */
1258                 i--;
1259         }
1260         return NT_STATUS_OK;
1261 }
1262
1263 /* Search MS NFS style ACE with UNIX mode */
1264 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1265                              files_struct *fsp,
1266                              struct security_descriptor *psd,
1267                              mode_t *pmode,
1268                              bool *pdo_chmod)
1269 {
1270         uint32_t i;
1271         struct fruit_config_data *config = NULL;
1272
1273         *pdo_chmod = false;
1274
1275         SMB_VFS_HANDLE_GET_DATA(handle, config,
1276                                 struct fruit_config_data,
1277                                 return NT_STATUS_UNSUCCESSFUL);
1278
1279         if (!global_fruit_config.nego_aapl) {
1280                 return NT_STATUS_OK;
1281         }
1282         if (psd->dacl == NULL || !config->unix_info_enabled) {
1283                 return NT_STATUS_OK;
1284         }
1285
1286         for (i = 0; i < psd->dacl->num_aces; i++) {
1287                 if (dom_sid_compare_domain(
1288                             &global_sid_Unix_NFS_Mode,
1289                             &psd->dacl->aces[i].trustee) == 0) {
1290                         *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1291                         *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1292                         *pdo_chmod = true;
1293
1294                         DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1295                                    fsp_str_dbg(fsp), (unsigned)(*pmode)));
1296                         break;
1297                 }
1298         }
1299
1300         /*
1301          * Remove any incoming virtual ACE entries generated by
1302          * fruit_fget_nt_acl().
1303          */
1304
1305         return remove_virtual_nfs_aces(psd);
1306 }
1307
1308 /****************************************************************************
1309  * VFS ops
1310  ****************************************************************************/
1311
1312 static int fruit_connect(vfs_handle_struct *handle,
1313                          const char *service,
1314                          const char *user)
1315 {
1316         int rc;
1317         char *list = NULL, *newlist = NULL;
1318         struct fruit_config_data *config;
1319         const struct loadparm_substitution *lp_sub =
1320                 loadparm_s3_global_substitution();
1321
1322         DEBUG(10, ("fruit_connect\n"));
1323
1324         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1325         if (rc < 0) {
1326                 return rc;
1327         }
1328
1329         rc = init_fruit_config(handle);
1330         if (rc != 0) {
1331                 return rc;
1332         }
1333
1334         SMB_VFS_HANDLE_GET_DATA(handle, config,
1335                                 struct fruit_config_data, return -1);
1336
1337         if (config->veto_appledouble) {
1338                 list = lp_veto_files(talloc_tos(), lp_sub, SNUM(handle->conn));
1339
1340                 if (list) {
1341                         if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1342                                 newlist = talloc_asprintf(
1343                                         list,
1344                                         "%s/" ADOUBLE_NAME_PREFIX "*/",
1345                                         list);
1346                                 lp_do_parameter(SNUM(handle->conn),
1347                                                 "veto files",
1348                                                 newlist);
1349                         }
1350                 } else {
1351                         lp_do_parameter(SNUM(handle->conn),
1352                                         "veto files",
1353                                         "/" ADOUBLE_NAME_PREFIX "*/");
1354                 }
1355
1356                 TALLOC_FREE(list);
1357         }
1358
1359         if (config->encoding == FRUIT_ENC_NATIVE) {
1360                 lp_do_parameter(SNUM(handle->conn),
1361                                 "catia:mappings",
1362                                 macos_string_replace_map);
1363         }
1364
1365         if (config->time_machine) {
1366                 DBG_NOTICE("Enabling durable handles for Time Machine "
1367                            "support on [%s]\n", service);
1368                 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
1369                 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
1370                 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
1371                 if (!lp_strict_sync(SNUM(handle->conn))) {
1372                         DBG_WARNING("Time Machine without strict sync is not "
1373                                     "recommended!\n");
1374                 }
1375                 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
1376         }
1377
1378         return rc;
1379 }
1380
1381 static void fio_ref_destroy_fn(void *p_data)
1382 {
1383         struct fio *ref_fio = (struct fio *)p_data;
1384         if (ref_fio->real_fio != NULL) {
1385                 SMB_ASSERT(ref_fio->real_fio->ad_fsp == ref_fio->fsp);
1386                 ref_fio->real_fio->ad_fsp = NULL;
1387                 ref_fio->real_fio = NULL;
1388         }
1389 }
1390
1391 static void fio_close_ad_fsp(struct fio *fio)
1392 {
1393         if (fio->ad_fsp != NULL) {
1394                 fd_close(fio->ad_fsp);
1395                 file_free(NULL, fio->ad_fsp);
1396                 /* fio_ref_destroy_fn() should have cleared this */
1397                 SMB_ASSERT(fio->ad_fsp == NULL);
1398         }
1399 }
1400
1401 static void fio_destroy_fn(void *p_data)
1402 {
1403         struct fio *fio = (struct fio *)p_data;
1404         fio_close_ad_fsp(fio);
1405 }
1406
1407 static int fruit_open_meta_stream(vfs_handle_struct *handle,
1408                                   const struct files_struct *dirfsp,
1409                                   const struct smb_filename *smb_fname,
1410                                   files_struct *fsp,
1411                                   int flags,
1412                                   mode_t mode)
1413 {
1414         struct fruit_config_data *config = NULL;
1415         struct fio *fio = NULL;
1416         struct vfs_open_how how = {
1417                 .flags = flags & ~O_CREAT,
1418                 .mode = mode,
1419         };
1420         int fd;
1421
1422         DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1423
1424         SMB_VFS_HANDLE_GET_DATA(handle, config,
1425                                 struct fruit_config_data, return -1);
1426
1427         fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1428         fio->handle = handle;
1429         fio->fsp = fsp;
1430         fio->type = ADOUBLE_META;
1431         fio->config = config;
1432
1433         fd = SMB_VFS_NEXT_OPENAT(handle,
1434                                  dirfsp,
1435                                  smb_fname,
1436                                  fsp,
1437                                  &how);
1438         if (fd != -1) {
1439                 return fd;
1440         }
1441
1442         if (!(flags & O_CREAT)) {
1443                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1444                 return -1;
1445         }
1446
1447         fd = vfs_fake_fd();
1448         if (fd == -1) {
1449                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
1450                 return -1;
1451         }
1452
1453         fio->fake_fd = true;
1454         fio->flags = flags;
1455         fio->mode = mode;
1456
1457         return fd;
1458 }
1459
1460 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
1461                                     const struct files_struct *dirfsp,
1462                                     const struct smb_filename *smb_fname,
1463                                     files_struct *fsp,
1464                                     int flags,
1465                                     mode_t mode)
1466 {
1467         struct fruit_config_data *config = NULL;
1468         struct fio *fio = NULL;
1469         struct adouble *ad = NULL;
1470         bool meta_exists = false;
1471         int fd;
1472
1473         DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1474
1475         /*
1476          * We know this is a stream open, so fsp->base_fsp must
1477          * already be open.
1478          */
1479         SMB_ASSERT(fsp_is_alternate_stream(fsp));
1480         SMB_ASSERT(fsp->base_fsp->fsp_name->fsp == fsp->base_fsp);
1481
1482         ad = ad_get(talloc_tos(), handle, fsp->base_fsp->fsp_name, ADOUBLE_META);
1483         if (ad != NULL) {
1484                 meta_exists = true;
1485         }
1486
1487         TALLOC_FREE(ad);
1488
1489         if (!meta_exists && !(flags & O_CREAT)) {
1490                 errno = ENOENT;
1491                 return -1;
1492         }
1493
1494         fd = vfs_fake_fd();
1495         if (fd == -1) {
1496                 return -1;
1497         }
1498
1499         SMB_VFS_HANDLE_GET_DATA(handle, config,
1500                                 struct fruit_config_data, return -1);
1501
1502         fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1503         fio->handle = handle;
1504         fio->fsp = fsp;
1505         fio->type = ADOUBLE_META;
1506         fio->config = config;
1507         fio->fake_fd = true;
1508         fio->flags = flags;
1509         fio->mode = mode;
1510
1511         return fd;
1512 }
1513
1514 static int fruit_open_meta(vfs_handle_struct *handle,
1515                            const struct files_struct *dirfsp,
1516                            const struct smb_filename *smb_fname,
1517                            files_struct *fsp, int flags, mode_t mode)
1518 {
1519         int fd;
1520         struct fruit_config_data *config = NULL;
1521
1522         DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
1523
1524         SMB_VFS_HANDLE_GET_DATA(handle, config,
1525                                 struct fruit_config_data, return -1);
1526
1527         switch (config->meta) {
1528         case FRUIT_META_STREAM:
1529                 fd = fruit_open_meta_stream(handle, dirfsp, smb_fname,
1530                                             fsp, flags, mode);
1531                 break;
1532
1533         case FRUIT_META_NETATALK:
1534                 fd = fruit_open_meta_netatalk(handle, dirfsp, smb_fname,
1535                                               fsp, flags, mode);
1536                 break;
1537
1538         default:
1539                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1540                 return -1;
1541         }
1542
1543         DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1544
1545         return fd;
1546 }
1547
1548 static int fruit_open_rsrc_adouble(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)
1554 {
1555         int rc = 0;
1556         struct fruit_config_data *config = NULL;
1557         struct files_struct *ad_fsp = NULL;
1558         struct fio *fio = NULL;
1559         struct fio *ref_fio = NULL;
1560         NTSTATUS status;
1561         int fd = -1;
1562
1563         SMB_VFS_HANDLE_GET_DATA(handle, config,
1564                                 struct fruit_config_data, return -1);
1565
1566         if ((!(flags & O_CREAT)) &&
1567             S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
1568         {
1569                 /* sorry, but directories don't have a resource fork */
1570                 errno = EISDIR;
1571                 rc = -1;
1572                 goto exit;
1573         }
1574
1575         /*
1576          * We return a fake_fd to the vfs modules above,
1577          * while we open an internal backend fsp for the
1578          * '._' file for the next vfs modules.
1579          *
1580          * Note that adouble_open_from_base_fsp() recurses
1581          * into fruit_openat(), but it'll just pass to
1582          * the next module as just opens a flat file on
1583          * disk.
1584          */
1585
1586         fd = vfs_fake_fd();
1587         if (fd == -1) {
1588                 rc = fd;
1589                 goto exit;
1590         }
1591
1592         status = adouble_open_from_base_fsp(fsp->conn->cwd_fsp,
1593                                             fsp->base_fsp,
1594                                             ADOUBLE_RSRC,
1595                                             flags,
1596                                             mode,
1597                                             &ad_fsp);
1598         if (!NT_STATUS_IS_OK(status)) {
1599                 errno = map_errno_from_nt_status(status);
1600                 rc = -1;
1601                 goto exit;
1602         }
1603
1604         /*
1605          * Now we need to glue both handles together,
1606          * so that they automatically detach each other
1607          * on close.
1608          */
1609         fio = fruit_get_complete_fio(handle, fsp);
1610         if (fio == NULL) {
1611                 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp));
1612                 errno = EBADF;
1613                 rc = -1;
1614                 goto exit;
1615         }
1616
1617         ref_fio = VFS_ADD_FSP_EXTENSION(handle, ad_fsp,
1618                                         struct fio,
1619                                         fio_ref_destroy_fn);
1620         if (ref_fio == NULL) {
1621                 int saved_errno = errno;
1622                 fd_close(ad_fsp);
1623                 file_free(NULL, ad_fsp);
1624                 ad_fsp = NULL;
1625                 errno = saved_errno;
1626                 rc = -1;
1627                 goto exit;
1628         }
1629
1630         SMB_ASSERT(ref_fio->fsp == NULL);
1631         ref_fio->handle = handle;
1632         ref_fio->fsp = ad_fsp;
1633         ref_fio->type = ADOUBLE_RSRC;
1634         ref_fio->config = config;
1635         ref_fio->real_fio = fio;
1636         SMB_ASSERT(fio->ad_fsp == NULL);
1637         fio->ad_fsp = ad_fsp;
1638         fio->fake_fd = true;
1639
1640 exit:
1641
1642         DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc));
1643         if (rc != 0) {
1644                 int saved_errno = errno;
1645                 if (fd != -1) {
1646                         vfs_fake_fd_close(fd);
1647                 }
1648                 errno = saved_errno;
1649                 return rc;
1650         }
1651         return fd;
1652 }
1653
1654 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
1655                                  const struct files_struct *dirfsp,
1656                                  const struct smb_filename *smb_fname,
1657                                  files_struct *fsp,
1658                                  int flags,
1659                                  mode_t mode)
1660 {
1661 #ifdef HAVE_ATTROPEN
1662         int fd = -1;
1663
1664         /*
1665          * As there's no attropenat() this is only going to work with AT_FDCWD.
1666          */
1667         SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
1668
1669         fd = attropen(smb_fname->base_name,
1670                       AFPRESOURCE_EA_NETATALK,
1671                       flags,
1672                       mode);
1673         if (fd == -1) {
1674                 return -1;
1675         }
1676
1677         return fd;
1678
1679 #else
1680         errno = ENOSYS;
1681         return -1;
1682 #endif
1683 }
1684
1685 static int fruit_open_rsrc(vfs_handle_struct *handle,
1686                            const struct files_struct *dirfsp,
1687                            const struct smb_filename *smb_fname,
1688                            files_struct *fsp, int flags, mode_t mode)
1689 {
1690         int fd;
1691         struct fruit_config_data *config = NULL;
1692         struct fio *fio = NULL;
1693
1694         DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1695
1696         SMB_VFS_HANDLE_GET_DATA(handle, config,
1697                                 struct fruit_config_data, return -1);
1698
1699         fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, fio_destroy_fn);
1700         fio->handle = handle;
1701         fio->fsp = fsp;
1702         fio->type = ADOUBLE_RSRC;
1703         fio->config = config;
1704
1705         switch (config->rsrc) {
1706         case FRUIT_RSRC_STREAM: {
1707                 struct vfs_open_how how = {
1708                         .flags = flags, .mode = mode,
1709                 };
1710                 fd = SMB_VFS_NEXT_OPENAT(handle,
1711                                          dirfsp,
1712                                          smb_fname,
1713                                          fsp,
1714                                          &how);
1715                 break;
1716         }
1717
1718         case FRUIT_RSRC_ADFILE:
1719                 fd = fruit_open_rsrc_adouble(handle, dirfsp, smb_fname,
1720                                              fsp, flags, mode);
1721                 break;
1722
1723         case FRUIT_RSRC_XATTR:
1724                 fd = fruit_open_rsrc_xattr(handle, dirfsp, smb_fname,
1725                                            fsp, flags, mode);
1726                 break;
1727
1728         default:
1729                 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1730                 errno = EINVAL;
1731                 return -1;
1732         }
1733
1734         DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1735
1736         if (fd == -1) {
1737                 return -1;
1738         }
1739
1740         return fd;
1741 }
1742
1743 static int fruit_openat(vfs_handle_struct *handle,
1744                         const struct files_struct *dirfsp,
1745                         const struct smb_filename *smb_fname,
1746                         files_struct *fsp,
1747                         const struct vfs_open_how *how)
1748 {
1749         int fd;
1750
1751         DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
1752
1753         if (!is_named_stream(smb_fname)) {
1754                 return SMB_VFS_NEXT_OPENAT(handle,
1755                                            dirfsp,
1756                                            smb_fname,
1757                                            fsp,
1758                                            how);
1759         }
1760
1761         SMB_ASSERT(fsp_is_alternate_stream(fsp));
1762
1763         if (is_afpinfo_stream(smb_fname->stream_name)) {
1764                 fd = fruit_open_meta(handle,
1765                                      dirfsp,
1766                                      smb_fname,
1767                                      fsp,
1768                                      how->flags,
1769                                      how->mode);
1770         } else if (is_afpresource_stream(smb_fname->stream_name)) {
1771                 fd = fruit_open_rsrc(handle,
1772                                      dirfsp,
1773                                      smb_fname,
1774                                      fsp,
1775                                      how->flags,
1776                                      how->mode);
1777         } else {
1778                 fd = SMB_VFS_NEXT_OPENAT(handle,
1779                                          dirfsp,
1780                                          smb_fname,
1781                                          fsp,
1782                                          how);
1783         }
1784
1785         DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
1786
1787         /* Prevent reopen optimisation */
1788         fsp->fsp_flags.have_proc_fds = false;
1789         return fd;
1790 }
1791
1792 static int fruit_close_meta(vfs_handle_struct *handle,
1793                             files_struct *fsp)
1794 {
1795         int ret;
1796         struct fruit_config_data *config = NULL;
1797
1798         SMB_VFS_HANDLE_GET_DATA(handle, config,
1799                                 struct fruit_config_data, return -1);
1800
1801         switch (config->meta) {
1802         case FRUIT_META_STREAM:
1803         {
1804                 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1805                 if (fio == NULL) {
1806                         return -1;
1807                 }
1808                 if (fio->fake_fd) {
1809                         ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1810                         fsp_set_fd(fsp, -1);
1811                 } else {
1812                         ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1813                 }
1814                 break;
1815         }
1816         case FRUIT_META_NETATALK:
1817                 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1818                 fsp_set_fd(fsp, -1);
1819                 break;
1820
1821         default:
1822                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1823                 return -1;
1824         }
1825
1826         return ret;
1827 }
1828
1829
1830 static int fruit_close_rsrc(vfs_handle_struct *handle,
1831                             files_struct *fsp)
1832 {
1833         int ret;
1834         struct fruit_config_data *config = NULL;
1835
1836         SMB_VFS_HANDLE_GET_DATA(handle, config,
1837                                 struct fruit_config_data, return -1);
1838
1839         switch (config->rsrc) {
1840         case FRUIT_RSRC_STREAM:
1841                 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1842                 break;
1843
1844         case FRUIT_RSRC_ADFILE:
1845         {
1846                 struct fio *fio = fruit_get_complete_fio(handle, fsp);
1847                 if (fio == NULL) {
1848                         return -1;
1849                 }
1850                 fio_close_ad_fsp(fio);
1851                 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1852                 fsp_set_fd(fsp, -1);
1853                 break;
1854         }
1855
1856         case FRUIT_RSRC_XATTR:
1857                 ret = vfs_fake_fd_close(fsp_get_pathref_fd(fsp));
1858                 fsp_set_fd(fsp, -1);
1859                 break;
1860
1861         default:
1862                 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
1863                 return -1;
1864         }
1865
1866         return ret;
1867 }
1868
1869 static int fruit_close(vfs_handle_struct *handle,
1870                        files_struct *fsp)
1871 {
1872         int ret;
1873         int fd;
1874
1875         fd = fsp_get_pathref_fd(fsp);
1876
1877         DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
1878
1879         if (!fsp_is_alternate_stream(fsp)) {
1880                 return SMB_VFS_NEXT_CLOSE(handle, fsp);
1881         }
1882
1883         if (is_afpinfo_stream(fsp->fsp_name->stream_name)) {
1884                 ret = fruit_close_meta(handle, fsp);
1885         } else if (is_afpresource_stream(fsp->fsp_name->stream_name)) {
1886                 ret = fruit_close_rsrc(handle, fsp);
1887         } else {
1888                 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
1889         }
1890
1891         return ret;
1892 }
1893
1894 static int fruit_renameat(struct vfs_handle_struct *handle,
1895                         files_struct *srcfsp,
1896                         const struct smb_filename *smb_fname_src,
1897                         files_struct *dstfsp,
1898                         const struct smb_filename *smb_fname_dst)
1899 {
1900         int rc = -1;
1901         struct fruit_config_data *config = NULL;
1902         struct smb_filename *src_adp_smb_fname = NULL;
1903         struct smb_filename *dst_adp_smb_fname = NULL;
1904
1905         SMB_VFS_HANDLE_GET_DATA(handle, config,
1906                                 struct fruit_config_data, return -1);
1907
1908         if (!VALID_STAT(smb_fname_src->st)) {
1909                 DBG_ERR("Need valid stat for [%s]\n",
1910                         smb_fname_str_dbg(smb_fname_src));
1911                 return -1;
1912         }
1913
1914         rc = SMB_VFS_NEXT_RENAMEAT(handle,
1915                                 srcfsp,
1916                                 smb_fname_src,
1917                                 dstfsp,
1918                                 smb_fname_dst);
1919         if (rc != 0) {
1920                 return -1;
1921         }
1922
1923         if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
1924             (!S_ISREG(smb_fname_src->st.st_ex_mode)))
1925         {
1926                 return 0;
1927         }
1928
1929         rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
1930         if (rc != 0) {
1931                 goto done;
1932         }
1933
1934         rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
1935         if (rc != 0) {
1936                 goto done;
1937         }
1938
1939         DBG_DEBUG("%s -> %s\n",
1940                   smb_fname_str_dbg(src_adp_smb_fname),
1941                   smb_fname_str_dbg(dst_adp_smb_fname));
1942
1943         rc = SMB_VFS_NEXT_RENAMEAT(handle,
1944                         srcfsp,
1945                         src_adp_smb_fname,
1946                         dstfsp,
1947                         dst_adp_smb_fname);
1948         if (errno == ENOENT) {
1949                 rc = 0;
1950         }
1951
1952 done:
1953         TALLOC_FREE(src_adp_smb_fname);
1954         TALLOC_FREE(dst_adp_smb_fname);
1955         return rc;
1956 }
1957
1958 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
1959                                 struct files_struct *dirfsp,
1960                                 const struct smb_filename *smb_fname)
1961 {
1962         return SMB_VFS_NEXT_UNLINKAT(handle,
1963                                 dirfsp,
1964                                 smb_fname,
1965                                 0);
1966 }
1967
1968 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
1969                                       const struct smb_filename *smb_fname)
1970 {
1971         SMB_ASSERT(smb_fname->fsp != NULL);
1972         SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
1973         return SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp,
1974                                    AFPINFO_EA_NETATALK);
1975 }
1976
1977 static int fruit_unlink_meta(vfs_handle_struct *handle,
1978                         struct files_struct *dirfsp,
1979                         const struct smb_filename *smb_fname)
1980 {
1981         struct fruit_config_data *config = NULL;
1982         int rc;
1983
1984         SMB_VFS_HANDLE_GET_DATA(handle, config,
1985                                 struct fruit_config_data, return -1);
1986
1987         switch (config->meta) {
1988         case FRUIT_META_STREAM:
1989                 rc = fruit_unlink_meta_stream(handle,
1990                                 dirfsp,
1991                                 smb_fname);
1992                 break;
1993
1994         case FRUIT_META_NETATALK:
1995                 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
1996                 break;
1997
1998         default:
1999                 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2000                 return -1;
2001         }
2002
2003         return rc;
2004 }
2005
2006 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2007                                 struct files_struct *dirfsp,
2008                                 const struct smb_filename *smb_fname,
2009                                 bool force_unlink)
2010 {
2011         int ret;
2012
2013         if (!force_unlink) {
2014                 struct smb_filename *full_fname = NULL;
2015                 off_t size;
2016
2017                 /*
2018                  * TODO: use SMB_VFS_STATX() once we have it.
2019                  */
2020
2021                 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2022                                                           dirfsp,
2023                                                           smb_fname);
2024                 if (full_fname == NULL) {
2025                         return -1;
2026                 }
2027
2028                 /*
2029                  * 0 byte resource fork streams are not listed by
2030                  * vfs_streaminfo, as a result stream cleanup/deletion of file
2031                  * deletion doesn't remove the resourcefork stream.
2032                  */
2033
2034                 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
2035                 if (ret != 0) {
2036                         TALLOC_FREE(full_fname);
2037                         DBG_ERR("stat [%s] failed [%s]\n",
2038                                 smb_fname_str_dbg(full_fname), strerror(errno));
2039                         return -1;
2040                 }
2041
2042                 size = full_fname->st.st_ex_size;
2043                 TALLOC_FREE(full_fname);
2044
2045                 if (size > 0) {
2046                         /* OS X ignores resource fork stream delete requests */
2047                         return 0;
2048                 }
2049         }
2050
2051         ret = SMB_VFS_NEXT_UNLINKAT(handle,
2052                         dirfsp,
2053                         smb_fname,
2054                         0);
2055         if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2056                 ret = 0;
2057         }
2058
2059         return ret;
2060 }
2061
2062 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2063                                 struct files_struct *dirfsp,
2064                                 const struct smb_filename *smb_fname,
2065                                 bool force_unlink)
2066 {
2067         int rc;
2068         struct adouble *ad = NULL;
2069         struct smb_filename *adp_smb_fname = NULL;
2070
2071         if (!force_unlink) {
2072                 struct smb_filename *full_fname = NULL;
2073
2074                 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2075                                                           dirfsp,
2076                                                           smb_fname);
2077                 if (full_fname == NULL) {
2078                         return -1;
2079                 }
2080
2081                 ad = ad_get(talloc_tos(), handle, full_fname,
2082                             ADOUBLE_RSRC);
2083                 TALLOC_FREE(full_fname);
2084                 if (ad == NULL) {
2085                         errno = ENOENT;
2086                         return -1;
2087                 }
2088
2089
2090                 /*
2091                  * 0 byte resource fork streams are not listed by
2092                  * vfs_streaminfo, as a result stream cleanup/deletion of file
2093                  * deletion doesn't remove the resourcefork stream.
2094                  */
2095
2096                 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2097                         /* OS X ignores resource fork stream delete requests */
2098                         TALLOC_FREE(ad);
2099                         return 0;
2100                 }
2101
2102                 TALLOC_FREE(ad);
2103         }
2104
2105         rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
2106         if (rc != 0) {
2107                 return -1;
2108         }
2109
2110         rc = SMB_VFS_NEXT_UNLINKAT(handle,
2111                         dirfsp,
2112                         adp_smb_fname,
2113                         0);
2114         TALLOC_FREE(adp_smb_fname);
2115         if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2116                 rc = 0;
2117         }
2118
2119         return rc;
2120 }
2121
2122 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2123                                    const struct smb_filename *smb_fname,
2124                                    bool force_unlink)
2125 {
2126         /*
2127          * OS X ignores resource fork stream delete requests, so nothing to do
2128          * here. Removing the file will remove the xattr anyway, so we don't
2129          * have to take care of removing 0 byte resource forks that could be
2130          * left behind.
2131          */
2132         return 0;
2133 }
2134
2135 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2136                         struct files_struct *dirfsp,
2137                         const struct smb_filename *smb_fname,
2138                         bool force_unlink)
2139 {
2140         struct fruit_config_data *config = NULL;
2141         int rc;
2142
2143         SMB_VFS_HANDLE_GET_DATA(handle, config,
2144                                 struct fruit_config_data, return -1);
2145
2146         switch (config->rsrc) {
2147         case FRUIT_RSRC_STREAM:
2148                 rc = fruit_unlink_rsrc_stream(handle,
2149                                 dirfsp,
2150                                 smb_fname,
2151                                 force_unlink);
2152                 break;
2153
2154         case FRUIT_RSRC_ADFILE:
2155                 rc = fruit_unlink_rsrc_adouble(handle,
2156                                 dirfsp,
2157                                 smb_fname,
2158                                 force_unlink);
2159                 break;
2160
2161         case FRUIT_RSRC_XATTR:
2162                 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2163                 break;
2164
2165         default:
2166                 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2167                 return -1;
2168         }
2169
2170         return rc;
2171 }
2172
2173 static int fruit_fchmod(vfs_handle_struct *handle,
2174                       struct files_struct *fsp,
2175                       mode_t mode)
2176 {
2177         int rc = -1;
2178         struct fruit_config_data *config = NULL;
2179         struct smb_filename *smb_fname_adp = NULL;
2180         const struct smb_filename *smb_fname = NULL;
2181         NTSTATUS status;
2182
2183         rc = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
2184         if (rc != 0) {
2185                 return rc;
2186         }
2187
2188         smb_fname = fsp->fsp_name;
2189         SMB_VFS_HANDLE_GET_DATA(handle, config,
2190                                 struct fruit_config_data, return -1);
2191
2192         if (config->rsrc != FRUIT_RSRC_ADFILE) {
2193                 return 0;
2194         }
2195
2196         if (!VALID_STAT(smb_fname->st)) {
2197                 return 0;
2198         }
2199
2200         if (!S_ISREG(smb_fname->st.st_ex_mode)) {
2201                 return 0;
2202         }
2203
2204         rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
2205         if (rc != 0) {
2206                 return -1;
2207         }
2208
2209         status = openat_pathref_fsp(handle->conn->cwd_fsp,
2210                                     smb_fname_adp);
2211         if (!NT_STATUS_IS_OK(status)) {
2212                 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2213                 if (NT_STATUS_EQUAL(status,
2214                                     NT_STATUS_OBJECT_NAME_NOT_FOUND)){
2215                         rc = 0;
2216                         goto out;
2217                 }
2218                 rc = -1;
2219                 goto out;
2220         }
2221
2222         DBG_DEBUG("%s\n", smb_fname_adp->base_name);
2223
2224         rc = SMB_VFS_NEXT_FCHMOD(handle, smb_fname_adp->fsp, mode);
2225         if (errno == ENOENT) {
2226                 rc = 0;
2227         }
2228 out:
2229         TALLOC_FREE(smb_fname_adp);
2230         return rc;
2231 }
2232
2233 static int fruit_unlinkat(vfs_handle_struct *handle,
2234                         struct files_struct *dirfsp,
2235                         const struct smb_filename *smb_fname,
2236                         int flags)
2237 {
2238         struct fruit_config_data *config = NULL;
2239         struct smb_filename *rsrc_smb_fname = NULL;
2240         int ret;
2241
2242         if (flags & AT_REMOVEDIR) {
2243                 return SMB_VFS_NEXT_UNLINKAT(handle,
2244                                              dirfsp,
2245                                              smb_fname,
2246                                              AT_REMOVEDIR);
2247         }
2248
2249         SMB_VFS_HANDLE_GET_DATA(handle, config,
2250                                 struct fruit_config_data, return -1);
2251
2252         if (is_afpinfo_stream(smb_fname->stream_name)) {
2253                 return fruit_unlink_meta(handle,
2254                                 dirfsp,
2255                                 smb_fname);
2256         } else if (is_afpresource_stream(smb_fname->stream_name)) {
2257                 return fruit_unlink_rsrc(handle,
2258                                 dirfsp,
2259                                 smb_fname,
2260                                 false);
2261         } else if (is_named_stream(smb_fname)) {
2262                 return SMB_VFS_NEXT_UNLINKAT(handle,
2263                                 dirfsp,
2264                                 smb_fname,
2265                                 0);
2266         } else if (is_adouble_file(smb_fname->base_name)) {
2267                 return SMB_VFS_NEXT_UNLINKAT(handle,
2268                                 dirfsp,
2269                                 smb_fname,
2270                                 0);
2271         }
2272
2273         /*
2274          * A request to delete the base file. Because 0 byte resource
2275          * fork streams are not listed by fruit_streaminfo,
2276          * delete_all_streams() can't remove 0 byte resource fork
2277          * streams, so we have to cleanup this here.
2278          */
2279         rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2280                                              smb_fname->base_name,
2281                                              AFPRESOURCE_STREAM_NAME,
2282                                              NULL,
2283                                              smb_fname->twrp,
2284                                              smb_fname->flags);
2285         if (rsrc_smb_fname == NULL) {
2286                 return -1;
2287         }
2288
2289         ret = fruit_unlink_rsrc(handle, dirfsp, rsrc_smb_fname, true);
2290         if ((ret != 0) && (errno != ENOENT)) {
2291                 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2292                         smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2293                 TALLOC_FREE(rsrc_smb_fname);
2294                 return -1;
2295         }
2296         TALLOC_FREE(rsrc_smb_fname);
2297
2298         return SMB_VFS_NEXT_UNLINKAT(handle,
2299                         dirfsp,
2300                         smb_fname,
2301                         0);
2302 }
2303
2304 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
2305                                        files_struct *fsp, void *data,
2306                                        size_t n, off_t offset)
2307 {
2308         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2309         ssize_t nread;
2310         int ret;
2311
2312         if ((fio == NULL) || fio->fake_fd) {
2313                 return -1;
2314         }
2315
2316         nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2317         if (nread == -1 || nread == n) {
2318                 return nread;
2319         }
2320
2321         DBG_ERR("Removing [%s] after short read [%zd]\n",
2322                 fsp_str_dbg(fsp), nread);
2323
2324         ret = SMB_VFS_NEXT_UNLINKAT(handle,
2325                         fsp->conn->cwd_fsp,
2326                         fsp->fsp_name,
2327                         0);
2328         if (ret != 0) {
2329                 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
2330                 return -1;
2331         }
2332
2333         errno = EINVAL;
2334         return -1;
2335 }
2336
2337 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
2338                                         files_struct *fsp, void *data,
2339                                         size_t n, off_t offset)
2340 {
2341         AfpInfo *ai = NULL;
2342         struct adouble *ad = NULL;
2343         char afpinfo_buf[AFP_INFO_SIZE];
2344         char *p = NULL;
2345         ssize_t nread;
2346
2347         ai = afpinfo_new(talloc_tos());
2348         if (ai == NULL) {
2349                 return -1;
2350         }
2351
2352         ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2353         if (ad == NULL) {
2354                 nread = -1;
2355                 goto fail;
2356         }
2357
2358         p = ad_get_entry(ad, ADEID_FINDERI);
2359         if (p == NULL) {
2360                 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2361                 nread = -1;
2362                 goto fail;
2363         }
2364
2365         memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
2366
2367         nread = afpinfo_pack(ai, afpinfo_buf);
2368         if (nread != AFP_INFO_SIZE) {
2369                 nread = -1;
2370                 goto fail;
2371         }
2372
2373         memcpy(data, afpinfo_buf, n);
2374         nread = n;
2375
2376 fail:
2377         TALLOC_FREE(ai);
2378         return nread;
2379 }
2380
2381 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
2382                                 files_struct *fsp, void *data,
2383                                 size_t n, off_t offset)
2384 {
2385         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2386         ssize_t nread;
2387         ssize_t to_return;
2388
2389         /*
2390          * OS X has a off-by-1 error in the offset calculation, so we're
2391          * bug compatible here. It won't hurt, as any relevant real
2392          * world read requests from the AFP_AfpInfo stream will be
2393          * offset=0 n=60. offset is ignored anyway, see below.
2394          */
2395         if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2396                 return 0;
2397         }
2398
2399         if (fio == NULL) {
2400                 DBG_ERR("Failed to fetch fsp extension");
2401                 return -1;
2402         }
2403
2404         /* Yes, macOS always reads from offset 0 */
2405         offset = 0;
2406         to_return = MIN(n, AFP_INFO_SIZE);
2407
2408         switch (fio->config->meta) {
2409         case FRUIT_META_STREAM:
2410                 nread = fruit_pread_meta_stream(handle, fsp, data,
2411                                                 to_return, offset);
2412                 break;
2413
2414         case FRUIT_META_NETATALK:
2415                 nread = fruit_pread_meta_adouble(handle, fsp, data,
2416                                                  to_return, offset);
2417                 break;
2418
2419         default:
2420                 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2421                 return -1;
2422         }
2423
2424         if (nread == -1 && fio->fake_fd) {
2425                 AfpInfo *ai = NULL;
2426                 char afpinfo_buf[AFP_INFO_SIZE];
2427
2428                 ai = afpinfo_new(talloc_tos());
2429                 if (ai == NULL) {
2430                         return -1;
2431                 }
2432
2433                 nread = afpinfo_pack(ai, afpinfo_buf);
2434                 TALLOC_FREE(ai);
2435                 if (nread != AFP_INFO_SIZE) {
2436                         return -1;
2437                 }
2438
2439                 memcpy(data, afpinfo_buf, to_return);
2440                 return to_return;
2441         }
2442
2443         return nread;
2444 }
2445
2446 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
2447                                        files_struct *fsp, void *data,
2448                                        size_t n, off_t offset)
2449 {
2450         return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2451 }
2452
2453 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
2454                                       files_struct *fsp, void *data,
2455                                       size_t n, off_t offset)
2456 {
2457         return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2458 }
2459
2460 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
2461                                         files_struct *fsp, void *data,
2462                                         size_t n, off_t offset)
2463 {
2464         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2465         struct adouble *ad = NULL;
2466         ssize_t nread;
2467
2468         if (fio == NULL || fio->ad_fsp == NULL) {
2469                 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2470                 errno = EBADF;
2471                 return -1;
2472         }
2473
2474         ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2475         if (ad == NULL) {
2476                 DBG_ERR("ad_fget [%s] failed [%s]\n",
2477                         fsp_str_dbg(fio->ad_fsp), strerror(errno));
2478                 return -1;
2479         }
2480
2481         nread = SMB_VFS_NEXT_PREAD(handle, fio->ad_fsp, data, n,
2482                                    offset + ad_getentryoff(ad, ADEID_RFORK));
2483
2484         TALLOC_FREE(ad);
2485         return nread;
2486 }
2487
2488 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
2489                                 files_struct *fsp, void *data,
2490                                 size_t n, off_t offset)
2491 {
2492         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2493         ssize_t nread;
2494
2495         if (fio == NULL) {
2496                 errno = EINVAL;
2497                 return -1;
2498         }
2499
2500         switch (fio->config->rsrc) {
2501         case FRUIT_RSRC_STREAM:
2502                 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
2503                 break;
2504
2505         case FRUIT_RSRC_ADFILE:
2506                 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
2507                 break;
2508
2509         case FRUIT_RSRC_XATTR:
2510                 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
2511                 break;
2512
2513         default:
2514                 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2515                 return -1;
2516         }
2517
2518         return nread;
2519 }
2520
2521 static ssize_t fruit_pread(vfs_handle_struct *handle,
2522                            files_struct *fsp, void *data,
2523                            size_t n, off_t offset)
2524 {
2525         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2526         ssize_t nread;
2527
2528         DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2529                   fsp_str_dbg(fsp), (intmax_t)offset, n);
2530
2531         if (fio == NULL) {
2532                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2533         }
2534
2535         if (fio->type == ADOUBLE_META) {
2536                 nread = fruit_pread_meta(handle, fsp, data, n, offset);
2537         } else {
2538                 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
2539         }
2540
2541         DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
2542         return nread;
2543 }
2544
2545 static bool fruit_must_handle_aio_stream(struct fio *fio)
2546 {
2547         if (fio == NULL) {
2548                 return false;
2549         };
2550
2551         if (fio->type == ADOUBLE_META) {
2552                 return true;
2553         }
2554
2555         if ((fio->type == ADOUBLE_RSRC) &&
2556             (fio->config->rsrc == FRUIT_RSRC_ADFILE))
2557         {
2558                 return true;
2559         }
2560
2561         return false;
2562 }
2563
2564 struct fruit_pread_state {
2565         ssize_t nread;
2566         struct vfs_aio_state vfs_aio_state;
2567 };
2568
2569 static void fruit_pread_done(struct tevent_req *subreq);
2570
2571 static struct tevent_req *fruit_pread_send(
2572         struct vfs_handle_struct *handle,
2573         TALLOC_CTX *mem_ctx,
2574         struct tevent_context *ev,
2575         struct files_struct *fsp,
2576         void *data,
2577         size_t n, off_t offset)
2578 {
2579         struct tevent_req *req = NULL;
2580         struct tevent_req *subreq = NULL;
2581         struct fruit_pread_state *state = NULL;
2582         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2583
2584         req = tevent_req_create(mem_ctx, &state,
2585                                 struct fruit_pread_state);
2586         if (req == NULL) {
2587                 return NULL;
2588         }
2589
2590         if (fruit_must_handle_aio_stream(fio)) {
2591                 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
2592                 if (state->nread != n) {
2593                         if (state->nread != -1) {
2594                                 errno = EIO;
2595                         }
2596                         tevent_req_error(req, errno);
2597                         return tevent_req_post(req, ev);
2598                 }
2599                 tevent_req_done(req);
2600                 return tevent_req_post(req, ev);
2601         }
2602
2603         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
2604                                          data, n, offset);
2605         if (tevent_req_nomem(req, subreq)) {
2606                 return tevent_req_post(req, ev);
2607         }
2608         tevent_req_set_callback(subreq, fruit_pread_done, req);
2609         return req;
2610 }
2611
2612 static void fruit_pread_done(struct tevent_req *subreq)
2613 {
2614         struct tevent_req *req = tevent_req_callback_data(
2615                 subreq, struct tevent_req);
2616         struct fruit_pread_state *state = tevent_req_data(
2617                 req, struct fruit_pread_state);
2618
2619         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2620         TALLOC_FREE(subreq);
2621
2622         if (tevent_req_error(req, state->vfs_aio_state.error)) {
2623                 return;
2624         }
2625         tevent_req_done(req);
2626 }
2627
2628 static ssize_t fruit_pread_recv(struct tevent_req *req,
2629                                         struct vfs_aio_state *vfs_aio_state)
2630 {
2631         struct fruit_pread_state *state = tevent_req_data(
2632                 req, struct fruit_pread_state);
2633
2634         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2635                 return -1;
2636         }
2637
2638         *vfs_aio_state = state->vfs_aio_state;
2639         return state->nread;
2640 }
2641
2642 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
2643                                         files_struct *fsp, const void *data,
2644                                         size_t n, off_t offset)
2645 {
2646         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2647         AfpInfo *ai = NULL;
2648         size_t nwritten;
2649         int ret;
2650         bool ok;
2651
2652         DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2653                   fsp_str_dbg(fsp), (intmax_t)offset, n);
2654
2655         if (fio == NULL) {
2656                 return -1;
2657         }
2658
2659         if (fio->fake_fd) {
2660                 struct vfs_open_how how = {
2661                         .flags = fio->flags, .mode = fio->mode,
2662                 };
2663                 int fd = fsp_get_pathref_fd(fsp);
2664
2665                 ret = vfs_fake_fd_close(fd);
2666                 fsp_set_fd(fsp, -1);
2667                 if (ret != 0) {
2668                         DBG_ERR("Close [%s] failed: %s\n",
2669                                 fsp_str_dbg(fsp), strerror(errno));
2670                         return -1;
2671                 }
2672
2673                 fd = SMB_VFS_NEXT_OPENAT(handle,
2674                                          NULL, /* opening a stream */
2675                                          fsp->fsp_name,
2676                                          fsp,
2677                                          &how);
2678                 if (fd == -1) {
2679                         DBG_ERR("On-demand create [%s] in write failed: %s\n",
2680                                 fsp_str_dbg(fsp), strerror(errno));
2681                         return -1;
2682                 }
2683                 fsp_set_fd(fsp, fd);
2684                 fio->fake_fd = false;
2685         }
2686
2687         ai = afpinfo_unpack(talloc_tos(), data);
2688         if (ai == NULL) {
2689                 return -1;
2690         }
2691
2692         if (ai_empty_finderinfo(ai)) {
2693                 /*
2694                  * Writing an all 0 blob to the metadata stream results in the
2695                  * stream being removed on a macOS server. This ensures we
2696                  * behave the same and it verified by the "delete AFP_AfpInfo by
2697                  * writing all 0" test.
2698                  */
2699                 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
2700                 if (ret != 0) {
2701                         DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2702                                 fsp_str_dbg(fsp));
2703                         return -1;
2704                 }
2705
2706                 ok = set_delete_on_close(
2707                         fsp,
2708                         true,
2709                         handle->conn->session_info->security_token,
2710                         handle->conn->session_info->unix_token);
2711                 if (!ok) {
2712                         DBG_ERR("set_delete_on_close on [%s] failed\n",
2713                                 fsp_str_dbg(fsp));
2714                         return -1;
2715                 }
2716                 return n;
2717         }
2718
2719         nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2720         if (nwritten != n) {
2721                 return -1;
2722         }
2723
2724         return n;
2725 }
2726
2727 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
2728                                           files_struct *fsp, const void *data,
2729                                           size_t n, off_t offset)
2730 {
2731         struct adouble *ad = NULL;
2732         AfpInfo *ai = NULL;
2733         char *p = NULL;
2734         int ret;
2735         bool ok;
2736
2737         ai = afpinfo_unpack(talloc_tos(), data);
2738         if (ai == NULL) {
2739                 return -1;
2740         }
2741
2742         ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
2743         if (ad == NULL) {
2744                 ad = ad_init(talloc_tos(), ADOUBLE_META);
2745                 if (ad == NULL) {
2746                         return -1;
2747                 }
2748         }
2749         p = ad_get_entry(ad, ADEID_FINDERI);
2750         if (p == NULL) {
2751                 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
2752                 TALLOC_FREE(ad);
2753                 return -1;
2754         }
2755
2756         memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2757
2758         ret = ad_fset(handle, ad, fsp);
2759         if (ret != 0) {
2760                 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
2761                 TALLOC_FREE(ad);
2762                 return -1;
2763         }
2764
2765         TALLOC_FREE(ad);
2766
2767         if (!ai_empty_finderinfo(ai)) {
2768                 return n;
2769         }
2770
2771         /*
2772          * Writing an all 0 blob to the metadata stream results in the stream
2773          * being removed on a macOS server. This ensures we behave the same and
2774          * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2775          */
2776
2777         ok = set_delete_on_close(
2778                 fsp,
2779                 true,
2780                 handle->conn->session_info->security_token,
2781                 handle->conn->session_info->unix_token);
2782         if (!ok) {
2783                 DBG_ERR("set_delete_on_close on [%s] failed\n",
2784                         fsp_str_dbg(fsp));
2785                 return -1;
2786         }
2787
2788         return n;
2789 }
2790
2791 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
2792                                  files_struct *fsp, const void *data,
2793                                  size_t n, off_t offset)
2794 {
2795         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2796         ssize_t nwritten;
2797         uint8_t buf[AFP_INFO_SIZE];
2798         size_t to_write;
2799         size_t to_copy;
2800         int cmp;
2801
2802         if (fio == NULL) {
2803                 DBG_ERR("Failed to fetch fsp extension");
2804                 return -1;
2805         }
2806
2807         if (n < 3) {
2808                 errno = EINVAL;
2809                 return -1;
2810         }
2811
2812         if (offset != 0 && n < 60) {
2813                 errno = EINVAL;
2814                 return -1;
2815         }
2816
2817         cmp = memcmp(data, "AFP", 3);
2818         if (cmp != 0) {
2819                 errno = EINVAL;
2820                 return -1;
2821         }
2822
2823         if (n <= AFP_OFF_FinderInfo) {
2824                 /*
2825                  * Nothing to do here really, just return
2826                  */
2827                 return n;
2828         }
2829
2830         offset = 0;
2831
2832         to_copy = n;
2833         if (to_copy > AFP_INFO_SIZE) {
2834                 to_copy = AFP_INFO_SIZE;
2835         }
2836         memcpy(buf, data, to_copy);
2837
2838         to_write = n;
2839         if (to_write != AFP_INFO_SIZE) {
2840                 to_write = AFP_INFO_SIZE;
2841         }
2842
2843         switch (fio->config->meta) {
2844         case FRUIT_META_STREAM:
2845                 nwritten = fruit_pwrite_meta_stream(handle,
2846                                                     fsp,
2847                                                     buf,
2848                                                     to_write,
2849                                                     offset);
2850                 break;
2851
2852         case FRUIT_META_NETATALK:
2853                 nwritten = fruit_pwrite_meta_netatalk(handle,
2854                                                       fsp,
2855                                                       buf,
2856                                                       to_write,
2857                                                       offset);
2858                 break;
2859
2860         default:
2861                 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
2862                 return -1;
2863         }
2864
2865         if (nwritten != to_write) {
2866                 return -1;
2867         }
2868
2869         /*
2870          * Return the requested amount, verified against macOS SMB server
2871          */
2872         return n;
2873 }
2874
2875 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
2876                                         files_struct *fsp, const void *data,
2877                                         size_t n, off_t offset)
2878 {
2879         return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2880 }
2881
2882 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
2883                                        files_struct *fsp, const void *data,
2884                                        size_t n, off_t offset)
2885 {
2886         return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2887 }
2888
2889 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
2890                                          files_struct *fsp, const void *data,
2891                                          size_t n, off_t offset)
2892 {
2893         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2894         struct adouble *ad = NULL;
2895         ssize_t nwritten;
2896         int ret;
2897
2898         if (fio == NULL || fio->ad_fsp == NULL) {
2899                 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
2900                 errno = EBADF;
2901                 return -1;
2902         }
2903
2904         ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
2905         if (ad == NULL) {
2906                 DBG_ERR("ad_fget [%s] failed [%s]\n",
2907                         fsp_str_dbg(fio->ad_fsp), strerror(errno));
2908                 return -1;
2909         }
2910
2911         nwritten = SMB_VFS_NEXT_PWRITE(handle, fio->ad_fsp, data, n,
2912                                        offset + ad_getentryoff(ad, ADEID_RFORK));
2913         if (nwritten != n) {
2914                 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2915                         fsp_str_dbg(fio->ad_fsp), nwritten, n);
2916                 TALLOC_FREE(ad);
2917                 return -1;
2918         }
2919
2920         if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2921                 ad_setentrylen(ad, ADEID_RFORK, n + offset);
2922                 ret = ad_fset(handle, ad, fio->ad_fsp);
2923                 if (ret != 0) {
2924                         DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio->ad_fsp));
2925                         TALLOC_FREE(ad);
2926                         return -1;
2927                 }
2928         }
2929
2930         TALLOC_FREE(ad);
2931         return n;
2932 }
2933
2934 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
2935                                  files_struct *fsp, const void *data,
2936                                  size_t n, off_t offset)
2937 {
2938         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2939         ssize_t nwritten;
2940
2941         if (fio == NULL) {
2942                 DBG_ERR("Failed to fetch fsp extension");
2943                 return -1;
2944         }
2945
2946         switch (fio->config->rsrc) {
2947         case FRUIT_RSRC_STREAM:
2948                 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
2949                 break;
2950
2951         case FRUIT_RSRC_ADFILE:
2952                 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
2953                 break;
2954
2955         case FRUIT_RSRC_XATTR:
2956                 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
2957                 break;
2958
2959         default:
2960                 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
2961                 return -1;
2962         }
2963
2964         return nwritten;
2965 }
2966
2967 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2968                             files_struct *fsp, const void *data,
2969                             size_t n, off_t offset)
2970 {
2971         struct fio *fio = fruit_get_complete_fio(handle, fsp);
2972         ssize_t nwritten;
2973
2974         DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
2975                   fsp_str_dbg(fsp), (intmax_t)offset, n);
2976
2977         if (fio == NULL) {
2978                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2979         }
2980
2981         if (fio->type == ADOUBLE_META) {
2982                 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
2983         } else {
2984                 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
2985         }
2986
2987         DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
2988         return nwritten;
2989 }
2990
2991 struct fruit_pwrite_state {
2992         ssize_t nwritten;
2993         struct vfs_aio_state vfs_aio_state;
2994 };
2995
2996 static void fruit_pwrite_done(struct tevent_req *subreq);
2997
2998 static struct tevent_req *fruit_pwrite_send(
2999         struct vfs_handle_struct *handle,
3000         TALLOC_CTX *mem_ctx,
3001         struct tevent_context *ev,
3002         struct files_struct *fsp,
3003         const void *data,
3004         size_t n, off_t offset)
3005 {
3006         struct tevent_req *req = NULL;
3007         struct tevent_req *subreq = NULL;
3008         struct fruit_pwrite_state *state = NULL;
3009         struct fio *fio = fruit_get_complete_fio(handle, fsp);
3010
3011         req = tevent_req_create(mem_ctx, &state,
3012                                 struct fruit_pwrite_state);
3013         if (req == NULL) {
3014                 return NULL;
3015         }
3016
3017         if (fruit_must_handle_aio_stream(fio)) {
3018                 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
3019                 if (state->nwritten != n) {
3020                         if (state->nwritten != -1) {
3021                                 errno = EIO;
3022                         }
3023                         tevent_req_error(req, errno);
3024                         return tevent_req_post(req, ev);
3025                 }
3026                 tevent_req_done(req);
3027                 return tevent_req_post(req, ev);
3028         }
3029
3030         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
3031                                           data, n, offset);
3032         if (tevent_req_nomem(req, subreq)) {
3033                 return tevent_req_post(req, ev);
3034         }
3035         tevent_req_set_callback(subreq, fruit_pwrite_done, req);
3036         return req;
3037 }
3038
3039 static void fruit_pwrite_done(struct tevent_req *subreq)
3040 {
3041         struct tevent_req *req = tevent_req_callback_data(
3042                 subreq, struct tevent_req);
3043         struct fruit_pwrite_state *state = tevent_req_data(
3044                 req, struct fruit_pwrite_state);
3045
3046         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3047         TALLOC_FREE(subreq);
3048
3049         if (tevent_req_error(req, state->vfs_aio_state.error)) {
3050                 return;
3051         }
3052         tevent_req_done(req);
3053 }
3054
3055 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
3056                                          struct vfs_aio_state *vfs_aio_state)
3057 {
3058         struct fruit_pwrite_state *state = tevent_req_data(
3059                 req, struct fruit_pwrite_state);
3060
3061         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3062                 return -1;
3063         }
3064
3065         *vfs_aio_state = state->vfs_aio_state;
3066         return state->nwritten;
3067 }
3068
3069 /**
3070  * Helper to stat/lstat the base file of an smb_fname.
3071  */
3072 static int fruit_stat_base(vfs_handle_struct *handle,
3073                            struct smb_filename *smb_fname,
3074                            bool follow_links)
3075 {
3076         char *tmp_stream_name;
3077         int rc;
3078
3079         tmp_stream_name = smb_fname->stream_name;
3080         smb_fname->stream_name = NULL;
3081         if (follow_links) {
3082                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3083         } else {
3084                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3085         }
3086         smb_fname->stream_name = tmp_stream_name;
3087
3088         DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3089                   smb_fname->base_name,
3090                   (uintmax_t)smb_fname->st.st_ex_dev,
3091                   (uintmax_t)smb_fname->st.st_ex_ino);
3092         return rc;
3093 }
3094
3095 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3096                                   struct smb_filename *smb_fname,
3097                                   bool follow_links)
3098 {
3099         int ret;
3100         ino_t ino;
3101
3102         ret = fruit_stat_base(handle, smb_fname, false);
3103         if (ret != 0) {
3104                 return -1;
3105         }
3106
3107         ino = hash_inode(&smb_fname->st, smb_fname->stream_name);
3108
3109         if (follow_links) {
3110                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3111         } else {
3112                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3113         }
3114
3115         smb_fname->st.st_ex_ino = ino;
3116
3117         return ret;
3118 }
3119
3120 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3121                                     struct smb_filename *smb_fname,
3122                                     bool follow_links)
3123 {
3124         struct adouble *ad = NULL;
3125
3126         /* Populate the stat struct with info from the base file. */
3127         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3128                 return -1;
3129         }
3130
3131         ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3132         if (ad == NULL) {
3133                 DBG_INFO("fruit_stat_meta %s: %s\n",
3134                          smb_fname_str_dbg(smb_fname), strerror(errno));
3135                 errno = ENOENT;
3136                 return -1;
3137         }
3138         TALLOC_FREE(ad);
3139
3140         smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3141         smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3142                                               smb_fname->stream_name);
3143         return 0;
3144 }
3145
3146 static int fruit_stat_meta(vfs_handle_struct *handle,
3147                            struct smb_filename *smb_fname,
3148                            bool follow_links)
3149 {
3150         struct fruit_config_data *config = NULL;
3151         int ret;
3152
3153         SMB_VFS_HANDLE_GET_DATA(handle, config,
3154                                 struct fruit_config_data, return -1);
3155
3156         switch (config->meta) {
3157         case FRUIT_META_STREAM:
3158                 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3159                 break;
3160
3161         case FRUIT_META_NETATALK:
3162                 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3163                 break;
3164
3165         default:
3166                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3167                 return -1;
3168         }
3169
3170         return ret;
3171 }
3172
3173 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3174                                     struct smb_filename *smb_fname,
3175                                     bool follow_links)
3176 {
3177         struct adouble *ad = NULL;
3178         int ret;
3179
3180         ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3181         if (ad == NULL) {
3182                 errno = ENOENT;
3183                 return -1;
3184         }
3185
3186         /* Populate the stat struct with info from the base file. */
3187         ret = fruit_stat_base(handle, smb_fname, follow_links);
3188         if (ret != 0) {
3189                 TALLOC_FREE(ad);
3190                 return -1;
3191         }
3192
3193         smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3194         smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3195                                               smb_fname->stream_name);
3196         TALLOC_FREE(ad);
3197         return 0;
3198 }
3199
3200 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3201                                   struct smb_filename *smb_fname,
3202                                   bool follow_links)
3203 {
3204         int ret;
3205
3206         if (follow_links) {
3207                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3208         } else {
3209                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3210         }
3211
3212         return ret;
3213 }
3214
3215 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3216                                  struct smb_filename *smb_fname,
3217                                  bool follow_links)
3218 {
3219 #ifdef HAVE_ATTROPEN
3220         int ret;
3221         int fd = -1;
3222
3223         /* Populate the stat struct with info from the base file. */
3224         ret = fruit_stat_base(handle, smb_fname, follow_links);
3225         if (ret != 0) {
3226                 return -1;
3227         }
3228
3229         fd = attropen(smb_fname->base_name,
3230                       AFPRESOURCE_EA_NETATALK,
3231                       O_RDONLY);
3232         if (fd == -1) {
3233                 return 0;
3234         }
3235
3236         ret = sys_fstat(fd, &smb_fname->st, false);
3237         if (ret != 0) {
3238                 close(fd);
3239                 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3240                         AFPRESOURCE_EA_NETATALK);
3241                 return -1;
3242         }
3243         close(fd);
3244         fd = -1;
3245
3246         smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st,
3247                                              smb_fname->stream_name);
3248
3249         return ret;
3250
3251 #else
3252         errno = ENOSYS;
3253         return -1;
3254 #endif
3255 }
3256
3257 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3258                            struct smb_filename *smb_fname,
3259                            bool follow_links)
3260 {
3261         struct fruit_config_data *config = NULL;
3262         int ret;
3263
3264         DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3265
3266         SMB_VFS_HANDLE_GET_DATA(handle, config,
3267                                 struct fruit_config_data, return -1);
3268
3269         switch (config->rsrc) {
3270         case FRUIT_RSRC_STREAM:
3271                 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3272                 break;
3273
3274         case FRUIT_RSRC_XATTR:
3275                 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3276                 break;
3277
3278         case FRUIT_RSRC_ADFILE:
3279                 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3280                 break;
3281
3282         default:
3283                 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3284                 return -1;
3285         }
3286
3287         return ret;
3288 }
3289
3290 static int fruit_stat(vfs_handle_struct *handle,
3291                       struct smb_filename *smb_fname)
3292 {
3293         int rc = -1;
3294
3295         DEBUG(10, ("fruit_stat called for %s\n",
3296                    smb_fname_str_dbg(smb_fname)));
3297
3298         if (!is_named_stream(smb_fname)) {
3299                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3300                 if (rc == 0) {
3301                         update_btime(handle, smb_fname);
3302                 }
3303                 return rc;
3304         }
3305
3306         /*
3307          * Note if lp_posix_paths() is true, we can never
3308          * get here as is_ntfs_stream_smb_fname() is
3309          * always false. So we never need worry about
3310          * not following links here.
3311          */
3312
3313         if (is_afpinfo_stream(smb_fname->stream_name)) {
3314                 rc = fruit_stat_meta(handle, smb_fname, true);
3315         } else if (is_afpresource_stream(smb_fname->stream_name)) {
3316                 rc = fruit_stat_rsrc(handle, smb_fname, true);
3317         } else {
3318                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3319         }
3320
3321         if (rc == 0) {
3322                 update_btime(handle, smb_fname);
3323                 smb_fname->st.st_ex_mode &= ~S_IFMT;
3324                 smb_fname->st.st_ex_mode |= S_IFREG;
3325                 smb_fname->st.st_ex_blocks =
3326                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3327         }
3328         return rc;
3329 }
3330
3331 static int fruit_lstat(vfs_handle_struct *handle,
3332                        struct smb_filename *smb_fname)
3333 {
3334         int rc = -1;
3335
3336         DEBUG(10, ("fruit_lstat called for %s\n",
3337                    smb_fname_str_dbg(smb_fname)));
3338
3339         if (!is_named_stream(smb_fname)) {
3340                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3341                 if (rc == 0) {
3342                         update_btime(handle, smb_fname);
3343                 }
3344                 return rc;
3345         }
3346
3347         if (is_afpinfo_stream(smb_fname->stream_name)) {
3348                 rc = fruit_stat_meta(handle, smb_fname, false);
3349         } else if (is_afpresource_stream(smb_fname->stream_name)) {
3350                 rc = fruit_stat_rsrc(handle, smb_fname, false);
3351         } else {
3352                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3353         }
3354
3355         if (rc == 0) {
3356                 update_btime(handle, smb_fname);
3357                 smb_fname->st.st_ex_mode &= ~S_IFMT;
3358                 smb_fname->st.st_ex_mode |= S_IFREG;
3359                 smb_fname->st.st_ex_blocks =
3360                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3361         }
3362         return rc;
3363 }
3364
3365 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
3366                                    files_struct *fsp,
3367                                    SMB_STRUCT_STAT *sbuf)
3368 {
3369         struct fio *fio = fruit_get_complete_fio(handle, fsp);
3370         struct smb_filename smb_fname;
3371         ino_t ino;
3372         int ret;
3373
3374         if (fio == NULL) {
3375                 return -1;
3376         }
3377
3378         if (fio->fake_fd) {
3379                 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3380                 if (ret != 0) {
3381                         return -1;
3382                 }
3383
3384                 *sbuf = fsp->base_fsp->fsp_name->st;
3385                 sbuf->st_ex_size = AFP_INFO_SIZE;
3386                 sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3387                 return 0;
3388         }
3389
3390         smb_fname = (struct smb_filename) {
3391                 .base_name = fsp->fsp_name->base_name,
3392                 .twrp = fsp->fsp_name->twrp,
3393         };
3394
3395         ret = fruit_stat_base(handle, &smb_fname, false);
3396         if (ret != 0) {
3397                 return -1;
3398         }
3399         *sbuf = smb_fname.st;
3400
3401         ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3402
3403         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3404         if (ret != 0) {
3405                 return -1;
3406         }
3407
3408         sbuf->st_ex_ino = ino;
3409         return 0;
3410 }
3411
3412 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
3413                                      files_struct *fsp,
3414                                      SMB_STRUCT_STAT *sbuf)
3415 {
3416         int ret;
3417
3418         ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3419         if (ret != 0) {
3420                 return -1;
3421         }
3422
3423         *sbuf = fsp->base_fsp->fsp_name->st;
3424         sbuf->st_ex_size = AFP_INFO_SIZE;
3425         sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3426
3427         return 0;
3428 }
3429
3430 static int fruit_fstat_meta(vfs_handle_struct *handle,
3431                             files_struct *fsp,
3432                             SMB_STRUCT_STAT *sbuf,
3433                             struct fio *fio)
3434 {
3435         int ret;
3436
3437         DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3438
3439         switch (fio->config->meta) {
3440         case FRUIT_META_STREAM:
3441                 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
3442                 break;
3443
3444         case FRUIT_META_NETATALK:
3445                 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
3446                 break;
3447
3448         default:
3449                 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3450                 return -1;
3451         }
3452
3453         DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
3454         return ret;
3455 }
3456
3457 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
3458                                   files_struct *fsp,
3459                                   SMB_STRUCT_STAT *sbuf)
3460 {
3461         return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3462 }
3463
3464 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
3465                                    files_struct *fsp,
3466                                    SMB_STRUCT_STAT *sbuf)
3467 {
3468         return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3469 }
3470
3471 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
3472                                     files_struct *fsp,
3473                                     SMB_STRUCT_STAT *sbuf)
3474 {
3475         struct fio *fio = fruit_get_complete_fio(handle, fsp);
3476         struct adouble *ad = NULL;
3477         int ret;
3478
3479         if (fio == NULL || fio->ad_fsp == NULL) {
3480                 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
3481                 errno = EBADF;
3482                 return -1;
3483         }
3484
3485         /* Populate the stat struct with info from the base file. */
3486         ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
3487         if (ret == -1) {
3488                 return -1;
3489         }
3490
3491         ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
3492         if (ad == NULL) {
3493                 DBG_ERR("ad_fget [%s] failed [%s]\n",
3494                         fsp_str_dbg(fio->ad_fsp), strerror(errno));
3495                 return -1;
3496         }
3497
3498         *sbuf = fsp->base_fsp->fsp_name->st;
3499         sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3500         sbuf->st_ex_ino = hash_inode(sbuf, fsp->fsp_name->stream_name);
3501
3502         TALLOC_FREE(ad);
3503         return 0;
3504 }
3505
3506 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3507                             SMB_STRUCT_STAT *sbuf, struct fio *fio)
3508 {
3509         int ret;
3510
3511         switch (fio->config->rsrc) {
3512         case FRUIT_RSRC_STREAM:
3513                 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
3514                 break;
3515
3516         case FRUIT_RSRC_ADFILE:
3517                 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
3518                 break;
3519
3520         case FRUIT_RSRC_XATTR:
3521                 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
3522                 break;
3523
3524         default:
3525                 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3526                 return -1;
3527         }
3528
3529         return ret;
3530 }
3531
3532 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3533                        SMB_STRUCT_STAT *sbuf)
3534 {
3535         struct fio *fio = fruit_get_complete_fio(handle, fsp);
3536         int rc;
3537
3538         if (fio == NULL) {
3539                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3540         }
3541
3542         DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
3543
3544         if (fio->type == ADOUBLE_META) {
3545                 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
3546         } else {
3547                 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
3548         }
3549
3550         if (rc == 0) {
3551                 sbuf->st_ex_mode &= ~S_IFMT;
3552                 sbuf->st_ex_mode |= S_IFREG;
3553                 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3554         }
3555
3556         DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
3557                   fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
3558         return rc;
3559 }
3560
3561 static NTSTATUS delete_invalid_meta_stream(
3562         vfs_handle_struct *handle,
3563         const struct smb_filename *smb_fname,
3564         TALLOC_CTX *mem_ctx,
3565         unsigned int *pnum_streams,
3566         struct stream_struct **pstreams,
3567         off_t size)
3568 {
3569         struct smb_filename *sname = NULL;
3570         NTSTATUS status;
3571         int ret;
3572         bool ok;
3573
3574         ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
3575         if (!ok) {
3576                 return NT_STATUS_INTERNAL_ERROR;
3577         }
3578
3579         if (size == 0) {
3580                 return NT_STATUS_OK;
3581         }
3582
3583         status = synthetic_pathref(talloc_tos(),
3584                                    handle->conn->cwd_fsp,
3585                                    smb_fname->base_name,
3586                                    AFPINFO_STREAM_NAME,
3587                                    NULL,
3588                                    smb_fname->twrp,
3589                                    0,
3590                                    &sname);
3591         if (!NT_STATUS_IS_OK(status)) {
3592                 return NT_STATUS_NO_MEMORY;
3593         }
3594
3595         ret = SMB_VFS_NEXT_UNLINKAT(handle,
3596                         handle->conn->cwd_fsp,
3597                         sname,
3598                         0);
3599         if (ret != 0) {
3600                 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
3601                 TALLOC_FREE(sname);
3602                 return map_nt_error_from_unix(errno);
3603         }
3604
3605         TALLOC_FREE(sname);
3606         return NT_STATUS_OK;
3607 }
3608
3609 static NTSTATUS fruit_streaminfo_meta_stream(
3610         vfs_handle_struct *handle,
3611         struct files_struct *fsp,
3612         const struct smb_filename *smb_fname,
3613         TALLOC_CTX *mem_ctx,
3614         unsigned int *pnum_streams,
3615         struct stream_struct **pstreams)
3616 {
3617         struct stream_struct *stream = *pstreams;
3618         unsigned int num_streams = *pnum_streams;
3619         int i;
3620
3621         for (i = 0; i < num_streams; i++) {
3622                 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3623                         break;
3624                 }
3625         }
3626
3627         if (i == num_streams) {
3628                 return NT_STATUS_OK;
3629         }
3630
3631         if (stream[i].size != AFP_INFO_SIZE) {
3632                 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3633                         (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
3634
3635                 return delete_invalid_meta_stream(handle,
3636                                                   smb_fname,
3637                                                   mem_ctx,
3638                                                   pnum_streams,
3639                                                   pstreams,
3640                                                   stream[i].size);
3641         }
3642
3643
3644         return NT_STATUS_OK;
3645 }
3646
3647 static NTSTATUS fruit_streaminfo_meta_netatalk(
3648         vfs_handle_struct *handle,
3649         struct files_struct *fsp,
3650         const struct smb_filename *smb_fname,
3651         TALLOC_CTX *mem_ctx,
3652         unsigned int *pnum_streams,
3653         struct stream_struct **pstreams)
3654 {
3655         struct stream_struct *stream = *pstreams;
3656         unsigned int num_streams = *pnum_streams;
3657         struct adouble *ad = NULL;
3658         bool is_fi_empty;
3659         int i;
3660         bool ok;
3661
3662         /* Remove the Netatalk xattr from the list */
3663         ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3664                               ":" NETATALK_META_XATTR ":$DATA");
3665         if (!ok) {
3666                 return NT_STATUS_NO_MEMORY;
3667         }
3668
3669         /*
3670          * Check if there's a AFPINFO_STREAM from the VFS streams
3671          * backend and if yes, remove it from the list
3672          */
3673         for (i = 0; i < num_streams; i++) {
3674                 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
3675                         break;
3676                 }
3677         }
3678
3679         if (i < num_streams) {
3680                 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3681                             smb_fname_str_dbg(smb_fname));
3682
3683                 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3684                                       AFPINFO_STREAM);
3685                 if (!ok) {
3686                         return NT_STATUS_INTERNAL_ERROR;
3687                 }
3688         }
3689
3690         ad = ad_get_meta_fsp(talloc_tos(), handle, smb_fname);
3691         if (ad == NULL) {
3692                 return NT_STATUS_OK;
3693         }
3694
3695         is_fi_empty = ad_empty_finderinfo(ad);
3696         TALLOC_FREE(ad);
3697
3698         if (is_fi_empty) {
3699                 return NT_STATUS_OK;
3700         }
3701
3702         ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3703                               AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3704                               smb_roundup(handle->conn, AFP_INFO_SIZE));
3705         if (!ok) {
3706                 return NT_STATUS_NO_MEMORY;
3707         }
3708
3709         return NT_STATUS_OK;
3710 }
3711
3712 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
3713                                       struct files_struct *fsp,
3714                                       const struct smb_filename *smb_fname,
3715                                       TALLOC_CTX *mem_ctx,
3716                                       unsigned int *pnum_streams,
3717                                       struct stream_struct **pstreams)
3718 {
3719         struct fruit_config_data *config = NULL;
3720         NTSTATUS status;
3721
3722         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3723                                 return NT_STATUS_INTERNAL_ERROR);
3724
3725         switch (config->meta) {
3726         case FRUIT_META_NETATALK:
3727                 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
3728                                                         mem_ctx, pnum_streams,
3729                                                         pstreams);
3730                 break;
3731
3732         case FRUIT_META_STREAM:
3733                 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
3734                                                       mem_ctx, pnum_streams,
3735                                                       pstreams);
3736                 break;
3737
3738         default:
3739                 return NT_STATUS_INTERNAL_ERROR;
3740         }
3741
3742         return status;
3743 }
3744
3745 static NTSTATUS fruit_streaminfo_rsrc_stream(
3746         vfs_handle_struct *handle,
3747         struct files_struct *fsp,
3748         const struct smb_filename *smb_fname,
3749         TALLOC_CTX *mem_ctx,
3750         unsigned int *pnum_streams,
3751         struct stream_struct **pstreams)
3752 {
3753         bool ok;
3754
3755         ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3756         if (!ok) {
3757                 DBG_ERR("Filtering resource stream failed\n");
3758                 return NT_STATUS_INTERNAL_ERROR;
3759         }
3760         return NT_STATUS_OK;
3761 }
3762
3763 static NTSTATUS fruit_streaminfo_rsrc_xattr(
3764         vfs_handle_struct *handle,
3765         struct files_struct *fsp,
3766         const struct smb_filename *smb_fname,
3767         TALLOC_CTX *mem_ctx,
3768         unsigned int *pnum_streams,
3769         struct stream_struct **pstreams)
3770 {
3771         bool ok;
3772
3773         ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
3774         if (!ok) {
3775                 DBG_ERR("Filtering resource stream failed\n");
3776                 return NT_STATUS_INTERNAL_ERROR;
3777         }
3778         return NT_STATUS_OK;
3779 }
3780
3781 static NTSTATUS fruit_streaminfo_rsrc_adouble(
3782         vfs_handle_struct *handle,
3783         struct files_struct *fsp,
3784         const struct smb_filename *smb_fname,
3785         TALLOC_CTX *mem_ctx,
3786         unsigned int *pnum_streams,
3787         struct stream_struct **pstreams)
3788 {
3789         struct stream_struct *stream = *pstreams;
3790         unsigned int num_streams = *pnum_streams;
3791         struct adouble *ad = NULL;
3792         bool ok;
3793         size_t rlen;
3794         int i;
3795
3796         /*
3797          * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3798          * and if yes, remove it from the list
3799          */
3800         for (i = 0; i < num_streams; i++) {
3801                 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
3802                         break;
3803                 }
3804         }
3805
3806         if (i < num_streams) {
3807                 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3808                             smb_fname_str_dbg(smb_fname));
3809
3810                 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3811                                       AFPRESOURCE_STREAM);
3812                 if (!ok) {
3813                         return NT_STATUS_INTERNAL_ERROR;
3814                 }
3815         }
3816
3817         ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
3818         if (ad == NULL) {
3819                 return NT_STATUS_OK;
3820         }
3821
3822         rlen = ad_getentrylen(ad, ADEID_RFORK);
3823         TALLOC_FREE(ad);
3824
3825         if (rlen == 0) {
3826                 return NT_STATUS_OK;
3827         }
3828
3829         ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
3830                               AFPRESOURCE_STREAM_NAME, rlen,
3831                               smb_roundup(handle->conn, rlen));
3832         if (!ok) {
3833                 return NT_STATUS_NO_MEMORY;
3834         }
3835
3836         return NT_STATUS_OK;
3837 }
3838
3839 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
3840                                       struct files_struct *fsp,
3841                                       const struct smb_filename *smb_fname,
3842                                       TALLOC_CTX *mem_ctx,
3843                                       unsigned int *pnum_streams,
3844                                       struct stream_struct **pstreams)
3845 {
3846         struct fruit_config_data *config = NULL;
3847         NTSTATUS status;
3848
3849         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3850                                 return NT_STATUS_INTERNAL_ERROR);
3851
3852         switch (config->rsrc) {
3853         case FRUIT_RSRC_STREAM:
3854                 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
3855                                                       mem_ctx, pnum_streams,
3856                                                       pstreams);
3857                 break;
3858
3859         case FRUIT_RSRC_XATTR:
3860                 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
3861                                                      mem_ctx, pnum_streams,
3862                                                      pstreams);
3863                 break;
3864
3865         case FRUIT_RSRC_ADFILE:
3866                 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
3867                                                        mem_ctx, pnum_streams,
3868                                                        pstreams);
3869                 break;
3870
3871         default:
3872                 return NT_STATUS_INTERNAL_ERROR;
3873         }
3874
3875         return status;
3876 }
3877
3878 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
3879                                        struct stream_struct **pstreams)
3880 {
3881         unsigned num_streams = *pnum_streams;
3882         struct stream_struct *streams = *pstreams;
3883         unsigned i = 0;
3884
3885         if (!global_fruit_config.nego_aapl) {
3886                 return;
3887         }
3888
3889         while (i < num_streams) {
3890                 struct smb_filename smb_fname = (struct smb_filename) {
3891                         .stream_name = streams[i].name,
3892                 };
3893
3894                 if (is_ntfs_default_stream_smb_fname(&smb_fname)
3895                     || streams[i].size > 0)
3896                 {
3897                         i++;
3898                         continue;
3899                 }
3900
3901                 streams[i] = streams[num_streams - 1];
3902                 num_streams--;
3903         }
3904
3905         *pnum_streams = num_streams;
3906 }
3907
3908 static NTSTATUS fruit_fstreaminfo(vfs_handle_struct *handle,
3909                                  struct files_struct *fsp,
3910                                  TALLOC_CTX *mem_ctx,
3911                                  unsigned int *pnum_streams,
3912                                  struct stream_struct **pstreams)
3913 {
3914         struct fruit_config_data *config = NULL;
3915         const struct smb_filename *smb_fname = NULL;
3916         NTSTATUS status;
3917
3918         smb_fname = fsp->fsp_name;
3919
3920         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3921                                 return NT_STATUS_UNSUCCESSFUL);
3922
3923         DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3924
3925         status = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
3926                                          pnum_streams, pstreams);
3927         if (!NT_STATUS_IS_OK(status)) {
3928                 return status;
3929         }
3930
3931         fruit_filter_empty_streams(pnum_streams, pstreams);
3932
3933         status = fruit_streaminfo_meta(handle, fsp, smb_fname,
3934                                        mem_ctx, pnum_streams, pstreams);
3935         if (!NT_STATUS_IS_OK(status)) {
3936                 return status;
3937         }
3938
3939         status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
3940                                        mem_ctx, pnum_streams, pstreams);
3941         if (!NT_STATUS_IS_OK(status)) {
3942                 return status;
3943         }
3944
3945         return NT_STATUS_OK;
3946 }
3947
3948 static int fruit_fntimes(vfs_handle_struct *handle,
3949                          files_struct *fsp,
3950                          struct smb_file_time *ft)
3951 {
3952         int rc = 0;
3953         struct adouble *ad = NULL;
3954         struct fruit_config_data *config = NULL;
3955
3956         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3957                                 return -1);
3958
3959         if ((config->meta != FRUIT_META_NETATALK) ||
3960             is_omit_timespec(&ft->create_time))
3961         {
3962                 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3963         }
3964
3965         DBG_DEBUG("set btime for %s to %s\n", fsp_str_dbg(fsp),
3966                   time_to_asc(convert_timespec_to_time_t(ft->create_time)));
3967
3968         ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3969         if (ad == NULL) {
3970                 goto exit;
3971         }
3972
3973         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3974                    convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3975
3976         rc = ad_fset(handle, ad, fsp);
3977
3978 exit:
3979
3980         TALLOC_FREE(ad);
3981         if (rc != 0) {
3982                 DBG_WARNING("%s\n", fsp_str_dbg(fsp));
3983                 return -1;
3984         }
3985         return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
3986 }
3987
3988 static int fruit_fallocate(struct vfs_handle_struct *handle,
3989                            struct files_struct *fsp,
3990                            uint32_t mode,
3991                            off_t offset,
3992                            off_t len)
3993 {
3994         struct fio *fio = fruit_get_complete_fio(handle, fsp);
3995
3996         if (fio == NULL) {
3997                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3998         }
3999
4000         /* Let the pwrite code path handle it. */
4001         errno = ENOSYS;
4002         return -1;
4003 }
4004
4005 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4006                                       struct files_struct *fsp,
4007                                       off_t offset)
4008 {
4009 #ifdef HAVE_ATTROPEN
4010         return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4011 #endif
4012         return 0;
4013 }
4014
4015 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4016                                         struct files_struct *fsp,
4017                                         off_t offset)
4018 {
4019         struct fio *fio = fruit_get_complete_fio(handle, fsp);
4020         int rc;
4021         struct adouble *ad = NULL;
4022         off_t ad_off;
4023
4024         if (fio == NULL || fio->ad_fsp == NULL) {
4025                 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp));
4026                 errno = EBADF;
4027                 return -1;
4028         }
4029
4030         ad = ad_fget(talloc_tos(), handle, fio->ad_fsp, ADOUBLE_RSRC);
4031         if (ad == NULL) {
4032                 DBG_ERR("ad_fget [%s] failed [%s]\n",
4033                         fsp_str_dbg(fio->ad_fsp), strerror(errno));
4034                 return -1;
4035         }
4036
4037         ad_off = ad_getentryoff(ad, ADEID_RFORK);
4038
4039         rc = SMB_VFS_NEXT_FTRUNCATE(handle, fio->ad_fsp, offset + ad_off);
4040         if (rc != 0) {
4041                 TALLOC_FREE(ad);
4042                 return -1;
4043         }
4044
4045         ad_setentrylen(ad, ADEID_RFORK, offset);
4046
4047         rc = ad_fset(handle, ad, fio->ad_fsp);
4048         if (rc != 0) {
4049                 DBG_ERR("ad_fset [%s] failed [%s]\n",
4050                         fsp_str_dbg(fio->ad_fsp), strerror(errno));
4051                 TALLOC_FREE(ad);
4052                 return -1;
4053         }
4054
4055         TALLOC_FREE(ad);
4056         return 0;
4057 }
4058
4059 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4060                                        struct files_struct *fsp,
4061                                        off_t offset)
4062 {
4063         return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4064 }
4065
4066 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4067                                 struct files_struct *fsp,
4068                                 off_t offset)
4069 {
4070         struct fio *fio = fruit_get_complete_fio(handle, fsp);
4071         int ret;
4072
4073         if (fio == NULL) {
4074                 DBG_ERR("Failed to fetch fsp extension");
4075                 return -1;
4076         }
4077
4078         switch (fio->config->rsrc) {
4079         case FRUIT_RSRC_XATTR:
4080                 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4081                 break;
4082
4083         case FRUIT_RSRC_ADFILE:
4084                 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4085                 break;
4086
4087         case FRUIT_RSRC_STREAM:
4088                 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4089                 break;
4090
4091         default:
4092                 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4093                 return -1;
4094         }
4095
4096
4097         return ret;
4098 }
4099
4100 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4101                                 struct files_struct *fsp,
4102                                 off_t offset)
4103 {
4104         if (offset > 60) {
4105                 DBG_WARNING("ftruncate %s to %jd",
4106                             fsp_str_dbg(fsp), (intmax_t)offset);
4107                 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED  */
4108                 errno = EOVERFLOW;
4109                 return -1;
4110         }
4111
4112         /* OS X returns success but does nothing  */
4113         DBG_INFO("ignoring ftruncate %s to %jd\n",
4114                  fsp_str_dbg(fsp), (intmax_t)offset);
4115         return 0;
4116 }
4117
4118 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4119                            struct files_struct *fsp,
4120                            off_t offset)
4121 {
4122         struct fio *fio = fruit_get_complete_fio(handle, fsp);
4123         int ret;
4124
4125         DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
4126                   (intmax_t)offset);
4127
4128         if (fio == NULL) {
4129                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4130         }
4131
4132         if (fio->type == ADOUBLE_META) {
4133                 ret = fruit_ftruncate_meta(handle, fsp, offset);
4134         } else {
4135                 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4136         }
4137
4138         DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4139         return ret;
4140 }
4141
4142 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4143                                   struct smb_request *req,
4144                                   struct files_struct *dirfsp,
4145                                   struct smb_filename *smb_fname,
4146                                   uint32_t access_mask,
4147                                   uint32_t share_access,
4148                                   uint32_t create_disposition,
4149                                   uint32_t create_options,
4150                                   uint32_t file_attributes,
4151                                   uint32_t oplock_request,
4152                                   const struct smb2_lease *lease,
4153                                   uint64_t allocation_size,
4154                                   uint32_t private_flags,
4155                                   struct security_descriptor *sd,
4156                                   struct ea_list *ea_list,
4157                                   files_struct **result,
4158                                   int *pinfo,
4159                                   const struct smb2_create_blobs *in_context_blobs,
4160                                   struct smb2_create_blobs *out_context_blobs)
4161 {
4162         NTSTATUS status;
4163         struct fruit_config_data *config = NULL;
4164         files_struct *fsp = NULL;
4165         bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
4166         int ret;
4167
4168         status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4169         if (!NT_STATUS_IS_OK(status)) {
4170                 goto fail;
4171         }
4172
4173         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4174                                 return NT_STATUS_UNSUCCESSFUL);
4175
4176         if (is_apple_stream(smb_fname->stream_name) && !internal_open) {
4177                 uint32_t conv_flags  = 0;
4178
4179                 if (config->wipe_intentionally_left_blank_rfork) {
4180                         conv_flags |= AD_CONV_WIPE_BLANK;
4181                 }
4182                 if (config->delete_empty_adfiles) {
4183                         conv_flags |= AD_CONV_DELETE;
4184                 }
4185
4186                 ret = ad_convert(handle,
4187                                  smb_fname,
4188                                  macos_string_replace_map,
4189                                  conv_flags);
4190                 if (ret != 0) {
4191                         DBG_ERR("ad_convert() failed\n");
4192                         return NT_STATUS_UNSUCCESSFUL;
4193                 }
4194         }
4195
4196         status = SMB_VFS_NEXT_CREATE_FILE(
4197                 handle, req, dirfsp, smb_fname,
4198                 access_mask, share_access,
4199                 create_disposition, create_options,
4200                 file_attributes, oplock_request,
4201                 lease,
4202                 allocation_size, private_flags,
4203                 sd, ea_list, result,
4204                 pinfo, in_context_blobs, out_context_blobs);
4205         if (!NT_STATUS_IS_OK(status)) {
4206                 return status;
4207         }
4208
4209         fsp = *result;
4210
4211         if (global_fruit_config.nego_aapl) {
4212                 if (config->posix_rename && fsp->fsp_flags.is_directory) {
4213                         /*
4214                          * Enable POSIX directory rename behaviour
4215                          */
4216                         fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4217                 }
4218         }
4219
4220         /*
4221          * If this is a plain open for existing files, opening an 0
4222          * byte size resource fork MUST fail with
4223          * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4224          *
4225          * Cf the vfs_fruit torture tests in test_rfork_create().
4226          */
4227         if (global_fruit_config.nego_aapl &&
4228             create_disposition == FILE_OPEN &&
4229             smb_fname->st.st_ex_size == 0 &&
4230             is_named_stream(smb_fname))
4231         {
4232                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4233                 goto fail;
4234         }
4235
4236         if (is_named_stream(smb_fname) || fsp->fsp_flags.is_directory) {
4237                 return status;
4238         }
4239
4240         if ((config->locking == FRUIT_LOCKING_NETATALK) &&
4241             (fsp->op != NULL) &&
4242             !fsp->fsp_flags.is_pathref)
4243         {
4244                 status = fruit_check_access(
4245                         handle, *result,
4246                         access_mask,
4247                         share_access);
4248                 if (!NT_STATUS_IS_OK(status)) {
4249                         goto fail;
4250                 }
4251         }
4252
4253         return status;
4254
4255 fail:
4256         DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4257
4258         if (fsp) {
4259                 close_file_free(req, &fsp, ERROR_CLOSE);
4260                 *result = NULL;
4261         }
4262
4263         return status;
4264 }
4265
4266 static NTSTATUS fruit_freaddir_attr(struct vfs_handle_struct *handle,
4267                                     struct files_struct *fsp,
4268                                     TALLOC_CTX *mem_ctx,
4269                                     struct readdir_attr_data **pattr_data)
4270 {
4271         struct fruit_config_data *config = NULL;
4272         struct readdir_attr_data *attr_data;
4273         uint32_t conv_flags  = 0;
4274         NTSTATUS status;
4275         int ret;
4276
4277         SMB_VFS_HANDLE_GET_DATA(handle, config,
4278                                 struct fruit_config_data,
4279                                 return NT_STATUS_UNSUCCESSFUL);
4280
4281         if (!global_fruit_config.nego_aapl) {
4282                 return SMB_VFS_NEXT_FREADDIR_ATTR(handle,
4283                                                   fsp,
4284                                                   mem_ctx,
4285                                                   pattr_data);
4286         }
4287
4288         DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4289
4290         if (config->wipe_intentionally_left_blank_rfork) {
4291                 conv_flags |= AD_CONV_WIPE_BLANK;
4292         }
4293         if (config->delete_empty_adfiles) {
4294                 conv_flags |= AD_CONV_DELETE;
4295         }
4296
4297         ret = ad_convert(handle,
4298                          fsp->fsp_name,
4299                          macos_string_replace_map,
4300                          conv_flags);
4301         if (ret != 0) {
4302                 DBG_ERR("ad_convert() failed\n");
4303                 return NT_STATUS_UNSUCCESSFUL;
4304         }
4305
4306         *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4307         if (*pattr_data == NULL) {
4308                 return NT_STATUS_NO_MEMORY;
4309         }
4310         attr_data = *pattr_data;
4311         attr_data->type = RDATTR_AAPL;
4312
4313         /*
4314          * Mac metadata: compressed FinderInfo, resource fork length
4315          * and creation date
4316          */
4317         status = readdir_attr_macmeta(handle, fsp->fsp_name, attr_data);
4318         if (!NT_STATUS_IS_OK(status)) {
4319                 /*
4320                  * Error handling is tricky: if we return failure from
4321                  * this function, the corresponding directory entry
4322                  * will to be passed to the client, so we really just
4323                  * want to error out on fatal errors.
4324                  */
4325                 if  (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4326                         goto fail;
4327                 }
4328         }
4329
4330         /*
4331          * UNIX mode
4332          */
4333         if (config->unix_info_enabled) {
4334                 attr_data->attr_data.aapl.unix_mode =
4335                         fsp->fsp_name->st.st_ex_mode;
4336         }
4337
4338         /*
4339          * max_access
4340          */
4341         if (!config->readdir_attr_max_access) {
4342                 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4343         } else {
4344                 status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
4345                         fsp,
4346                         false,
4347                         SEC_FLAG_MAXIMUM_ALLOWED,
4348                         &attr_data->attr_data.aapl.max_access);
4349                 if (!NT_STATUS_IS_OK(status)) {
4350                         goto fail;
4351                 }
4352         }
4353
4354         return NT_STATUS_OK;
4355
4356 fail:
4357         DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp),
4358                    nt_errstr(status));
4359         TALLOC_FREE(*pattr_data);
4360         return status;
4361 }
4362
4363 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4364                                   files_struct *fsp,
4365                                   uint32_t security_info,
4366                                   TALLOC_CTX *mem_ctx,
4367                                   struct security_descriptor **ppdesc)
4368 {
4369         NTSTATUS status;
4370         struct security_ace ace;
4371         struct dom_sid sid;
4372         struct fruit_config_data *config;
4373
4374         SMB_VFS_HANDLE_GET_DATA(handle, config,
4375                                 struct fruit_config_data,
4376                                 return NT_STATUS_UNSUCCESSFUL);
4377
4378         status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4379                                           mem_ctx, ppdesc);
4380         if (!NT_STATUS_IS_OK(status)) {
4381                 return status;
4382         }
4383
4384         /*
4385          * Add MS NFS style ACEs with uid, gid and mode
4386          */
4387         if (!global_fruit_config.nego_aapl) {
4388                 return NT_STATUS_OK;
4389         }
4390         if (!config->unix_info_enabled) {
4391                 return NT_STATUS_OK;
4392         }
4393
4394         /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4395         status = remove_virtual_nfs_aces(*ppdesc);
4396         if (!NT_STATUS_IS_OK(status)) {
4397                 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4398                 return status;
4399         }
4400
4401         /* MS NFS style mode */
4402         sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4403         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4404         status = security_descriptor_dacl_add(*ppdesc, &ace);
4405         if (!NT_STATUS_IS_OK(status)) {
4406                 DEBUG(1,("failed to add MS NFS style ACE\n"));
4407                 return status;
4408         }
4409
4410         /* MS NFS style uid */
4411         sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4412         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4413         status = security_descriptor_dacl_add(*ppdesc, &ace);
4414         if (!NT_STATUS_IS_OK(status)) {
4415                 DEBUG(1,("failed to add MS NFS style ACE\n"));
4416                 return status;
4417         }
4418
4419         /* MS NFS style gid */
4420         sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4421         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4422         status = security_descriptor_dacl_add(*ppdesc, &ace);
4423         if (!NT_STATUS_IS_OK(status)) {
4424                 DEBUG(1,("failed to add MS NFS style ACE\n"));
4425                 return status;
4426         }
4427
4428         return NT_STATUS_OK;
4429 }
4430
4431 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4432                                   files_struct *fsp,
4433                                   uint32_t security_info_sent,
4434                                   const struct security_descriptor *orig_psd)
4435 {
4436         NTSTATUS status;
4437         bool do_chmod;
4438         mode_t ms_nfs_mode = 0;
4439         int result;
4440         struct security_descriptor *psd = NULL;
4441         uint32_t orig_num_aces = 0;
4442
4443         if (orig_psd->dacl != NULL) {
4444                 orig_num_aces = orig_psd->dacl->num_aces;
4445         }
4446
4447         psd = security_descriptor_copy(talloc_tos(), orig_psd);
4448         if (psd == NULL) {
4449                 return NT_STATUS_NO_MEMORY;
4450         }
4451
4452         DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4453
4454         status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4455         if (!NT_STATUS_IS_OK(status)) {
4456                 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4457                 TALLOC_FREE(psd);
4458                 return status;
4459         }
4460
4461         /*
4462          * If only ms_nfs ACE entries were sent, ensure we set the DACL
4463          * sent/present flags correctly now we've removed them.
4464          */
4465
4466         if (orig_num_aces != 0) {
4467                 /*
4468                  * Are there any ACE's left ?
4469                  */
4470                 if (psd->dacl->num_aces == 0) {
4471                         /* No - clear the DACL sent/present flags. */
4472                         security_info_sent &= ~SECINFO_DACL;
4473                         psd->type &= ~SEC_DESC_DACL_PRESENT;
4474                 }
4475         }
4476
4477         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4478         if (!NT_STATUS_IS_OK(status)) {
4479                 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4480                 TALLOC_FREE(psd);
4481                 return status;
4482         }
4483
4484         if (do_chmod) {
4485                 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4486                 if (result != 0) {
4487                         DBG_WARNING("%s, result: %d, %04o error %s\n",
4488                                 fsp_str_dbg(fsp),
4489                                 result,
4490                                 (unsigned)ms_nfs_mode,
4491                                 strerror(errno));
4492                         status = map_nt_error_from_unix(errno);
4493                         TALLOC_FREE(psd);
4494                         return status;
4495                 }
4496         }
4497
4498         TALLOC_FREE(psd);
4499         return NT_STATUS_OK;
4500 }
4501
4502 static struct vfs_offload_ctx *fruit_offload_ctx;
4503
4504 struct fruit_offload_read_state {
4505         struct vfs_handle_struct *handle;
4506         struct tevent_context *ev;
4507         files_struct *fsp;
4508         uint32_t fsctl;
4509         uint32_t flags;
4510         uint64_t xferlen;
4511         DATA_BLOB token;
4512 };
4513
4514 static void fruit_offload_read_done(struct tevent_req *subreq);
4515
4516 static struct tevent_req *fruit_offload_read_send(
4517         TALLOC_CTX *mem_ctx,
4518         struct tevent_context *ev,
4519         struct vfs_handle_struct *handle,
4520         files_struct *fsp,
4521         uint32_t fsctl,
4522         uint32_t ttl,
4523         off_t offset,
4524         size_t to_copy)
4525 {
4526         struct tevent_req *req = NULL;
4527         struct tevent_req *subreq = NULL;
4528         struct fruit_offload_read_state *state = NULL;
4529
4530         req = tevent_req_create(mem_ctx, &state,
4531                                 struct fruit_offload_read_state);
4532         if (req == NULL) {
4533                 return NULL;
4534         }
4535         *state = (struct fruit_offload_read_state) {
4536                 .handle = handle,
4537                 .ev = ev,
4538                 .fsp = fsp,
4539                 .fsctl = fsctl,
4540         };
4541
4542         subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
4543                                                 fsctl, ttl, offset, to_copy);
4544         if (tevent_req_nomem(subreq, req)) {
4545                 return tevent_req_post(req, ev);
4546         }
4547         tevent_req_set_callback(subreq, fruit_offload_read_done, req);
4548         return req;
4549 }
4550
4551 static void fruit_offload_read_done(struct tevent_req *subreq)
4552 {
4553         struct tevent_req *req = tevent_req_callback_data(
4554                 subreq, struct tevent_req);
4555         struct fruit_offload_read_state *state = tevent_req_data(
4556                 req, struct fruit_offload_read_state);
4557         NTSTATUS status;
4558
4559         status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
4560                                                 state->handle,
4561                                                 state,
4562                                                 &state->flags,
4563                                                 &state->xferlen,
4564                                                 &state->token);
4565         TALLOC_FREE(subreq);
4566         if (tevent_req_nterror(req, status)) {
4567                 return;
4568         }
4569
4570         if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
4571                 tevent_req_done(req);
4572                 return;
4573         }
4574
4575         status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
4576                                             &fruit_offload_ctx);
4577         if (tevent_req_nterror(req, status)) {
4578                 return;
4579         }
4580
4581         status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
4582                                                 state->fsp,
4583                                                 &state->token);
4584         if (tevent_req_nterror(req, status)) {
4585                 return;
4586         }
4587
4588         tevent_req_done(req);
4589         return;
4590 }
4591
4592 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
4593                                         struct vfs_handle_struct *handle,
4594                                         TALLOC_CTX *mem_ctx,
4595                                         uint32_t *flags,
4596                                         uint64_t *xferlen,
4597                                         DATA_BLOB *token)
4598 {
4599         struct fruit_offload_read_state *state = tevent_req_data(
4600                 req, struct fruit_offload_read_state);
4601         NTSTATUS status;
4602
4603         if (tevent_req_is_nterror(req, &status)) {
4604                 tevent_req_received(req);
4605                 return status;
4606         }
4607
4608         *flags = state->flags;
4609         *xferlen = state->xferlen;
4610         token->length = state->token.length;
4611         token->data = talloc_move(mem_ctx, &state->token.data);
4612
4613         tevent_req_received(req);
4614         return NT_STATUS_OK;
4615 }
4616
4617 struct fruit_offload_write_state {
4618         struct vfs_handle_struct *handle;
4619         off_t copied;
4620         struct files_struct *src_fsp;
4621         struct files_struct *dst_fsp;
4622         bool is_copyfile;
4623 };
4624
4625 static void fruit_offload_write_done(struct tevent_req *subreq);
4626 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
4627                                                 TALLOC_CTX *mem_ctx,
4628                                                 struct tevent_context *ev,
4629                                                 uint32_t fsctl,
4630                                                 DATA_BLOB *token,
4631                                                 off_t transfer_offset,
4632                                                 struct files_struct *dest_fsp,
4633                                                 off_t dest_off,
4634                                                 off_t num)
4635 {
4636         struct tevent_req *req, *subreq;
4637         struct fruit_offload_write_state *state;
4638         NTSTATUS status;
4639         struct fruit_config_data *config;
4640         off_t src_off = transfer_offset;
4641         files_struct *src_fsp = NULL;
4642         off_t to_copy = num;
4643         bool copyfile_enabled = false;
4644
4645         DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4646                   (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4647
4648         SMB_VFS_HANDLE_GET_DATA(handle, config,
4649                                 struct fruit_config_data,
4650                                 return NULL);
4651
4652         req = tevent_req_create(mem_ctx, &state,
4653                                 struct fruit_offload_write_state);
4654         if (req == NULL) {
4655                 return NULL;
4656         }
4657         state->handle = handle;
4658         state->dst_fsp = dest_fsp;
4659
4660         switch (fsctl) {
4661         case FSCTL_SRV_COPYCHUNK:
4662         case FSCTL_SRV_COPYCHUNK_WRITE:
4663                 copyfile_enabled = config->copyfile_enabled;
4664                 break;
4665         default:
4666                 break;
4667         }
4668
4669         /*
4670          * Check if this a OS X copyfile style copychunk request with
4671          * a requested chunk count of 0 that was translated to a
4672          * offload_write_send VFS call overloading the parameters src_off
4673          * = dest_off = num = 0.
4674          */
4675         if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
4676                 status = vfs_offload_token_db_fetch_fsp(
4677                         fruit_offload_ctx, token, &src_fsp);
4678                 if (tevent_req_nterror(req, status)) {
4679                         return tevent_req_post(req, ev);
4680                 }
4681                 state->src_fsp = src_fsp;
4682
4683                 status = vfs_stat_fsp(src_fsp);
4684                 if (tevent_req_nterror(req, status)) {
4685                         return tevent_req_post(req, ev);
4686                 }
4687
4688                 to_copy = src_fsp->fsp_name->st.st_ex_size;
4689                 state->is_copyfile = true;
4690         }
4691
4692         subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
4693                                               mem_ctx,
4694                                               ev,
4695                                               fsctl,
4696                                               token,
4697                                               transfer_offset,
4698                                               dest_fsp,
4699                                               dest_off,
4700                                               to_copy);
4701         if (tevent_req_nomem(subreq, req)) {
4702                 return tevent_req_post(req, ev);
4703         }
4704
4705         tevent_req_set_callback(subreq, fruit_offload_write_done, req);
4706         return req;
4707 }
4708
4709 static void fruit_offload_write_done(struct tevent_req *subreq)
4710 {
4711         struct tevent_req *req = tevent_req_callback_data(
4712                 subreq, struct tevent_req);
4713         struct fruit_offload_write_state *state = tevent_req_data(
4714                 req, struct fruit_offload_write_state);
4715         NTSTATUS status;
4716         unsigned int num_streams = 0;
4717         struct stream_struct *streams = NULL;
4718         unsigned int i;
4719         struct smb_filename *src_fname_tmp = NULL;
4720         struct smb_filename *dst_fname_tmp = NULL;
4721
4722         status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
4723                                               subreq,
4724                                               &state->copied);
4725         TALLOC_FREE(subreq);
4726         if (tevent_req_nterror(req, status)) {
4727                 return;
4728         }
4729
4730         if (!state->is_copyfile) {
4731                 tevent_req_done(req);
4732                 return;
4733         }
4734
4735         /*
4736          * Now copy all remaining streams. We know the share supports
4737          * streams, because we're in vfs_fruit. We don't do this async
4738          * because streams are few and small.
4739          */
4740         status = vfs_fstreaminfo(state->src_fsp,
4741                                 req, &num_streams, &streams);
4742         if (tevent_req_nterror(req, status)) {
4743                 return;
4744         }
4745
4746         if (num_streams == 1) {
4747                 /* There is always one stream, ::$DATA. */
4748                 tevent_req_done(req);
4749                 return;
4750         }
4751
4752         for (i = 0; i < num_streams; i++) {
4753                 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4754                           __func__, streams[i].name, (size_t)streams[i].size));
4755
4756                 src_fname_tmp = synthetic_smb_fname(
4757                         req,
4758                         state->src_fsp->fsp_name->base_name,
4759                         streams[i].name,
4760                         NULL,
4761                         state->src_fsp->fsp_name->twrp,
4762                         state->src_fsp->fsp_name->flags);
4763                 if (tevent_req_nomem(src_fname_tmp, req)) {
4764                         return;
4765                 }
4766
4767                 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4768                         TALLOC_FREE(src_fname_tmp);
4769                         continue;
4770                 }
4771
4772                 dst_fname_tmp = synthetic_smb_fname(
4773                         req,
4774                         state->dst_fsp->fsp_name->base_name,
4775                         streams[i].name,
4776                         NULL,
4777                         state->dst_fsp->fsp_name->twrp,
4778                         state->dst_fsp->fsp_name->flags);
4779                 if (tevent_req_nomem(dst_fname_tmp, req)) {
4780                         TALLOC_FREE(src_fname_tmp);
4781                         return;
4782                 }
4783
4784                 status = copy_file(req,
4785                                    state->handle->conn,
4786                                    src_fname_tmp,
4787                                    dst_fname_tmp,
4788                                    FILE_CREATE);
4789                 if (!NT_STATUS_IS_OK(status)) {
4790                         DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4791                                   smb_fname_str_dbg(src_fname_tmp),
4792                                   smb_fname_str_dbg(dst_fname_tmp),
4793                                   nt_errstr(status)));
4794                         TALLOC_FREE(src_fname_tmp);
4795                         TALLOC_FREE(dst_fname_tmp);
4796                         tevent_req_nterror(req, status);
4797                         return;
4798                 }
4799
4800                 TALLOC_FREE(src_fname_tmp);
4801                 TALLOC_FREE(dst_fname_tmp);
4802         }
4803
4804         TALLOC_FREE(streams);
4805         TALLOC_FREE(src_fname_tmp);
4806         TALLOC_FREE(dst_fname_tmp);
4807         tevent_req_done(req);
4808 }
4809
4810 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
4811                                       struct tevent_req *req,
4812                                       off_t *copied)
4813 {
4814         struct fruit_offload_write_state *state = tevent_req_data(
4815                 req, struct fruit_offload_write_state);
4816         NTSTATUS status;
4817
4818         if (tevent_req_is_nterror(req, &status)) {
4819                 DEBUG(1, ("server side copy chunk failed: %s\n",
4820                           nt_errstr(status)));
4821                 *copied = 0;
4822                 tevent_req_received(req);
4823                 return status;
4824         }
4825
4826         *copied = state->copied;
4827         tevent_req_received(req);
4828
4829         return NT_STATUS_OK;
4830 }
4831
4832 static char *fruit_get_bandsize_line(char **lines, int numlines)
4833 {
4834         static regex_t re;
4835         static bool re_initialized = false;
4836         int i;
4837         int ret;
4838
4839         if (!re_initialized) {
4840                 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
4841                 if (ret != 0) {
4842                         return NULL;
4843                 }
4844                 re_initialized = true;
4845         }
4846
4847         for (i = 0; i < numlines; i++) {
4848                 regmatch_t matches[1];
4849
4850                 ret = regexec(&re, lines[i], 1, matches, 0);
4851                 if (ret == 0) {
4852                         /*
4853                          * Check if the match was on the last line, sa we want
4854                          * the subsequent line.
4855                          */
4856                         if (i + 1 == numlines) {
4857                                 return NULL;
4858                         }
4859                         return lines[i + 1];
4860                 }
4861                 if (ret != REG_NOMATCH) {
4862                         return NULL;
4863                 }
4864         }
4865
4866         return NULL;
4867 }
4868
4869 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
4870 {
4871         static regex_t re;
4872         static bool re_initialized = false;
4873         regmatch_t matches[2];
4874         uint64_t band_size;
4875         int ret;
4876         bool ok;
4877
4878         if (!re_initialized) {
4879                 ret = regcomp(&re,
4880                               "^[[:blank:]]*"
4881                               "<integer>\\([[:digit:]]*\\)</integer>$",
4882                               0);
4883                 if (ret != 0) {
4884                         return false;
4885                 }
4886                 re_initialized = true;
4887         }
4888
4889         ret = regexec(&re, line, 2, matches, 0);
4890         if (ret != 0) {
4891                 DBG_ERR("regex failed [%s]\n", line);
4892                 return false;
4893         }
4894
4895         line[matches[1].rm_eo] = '\0';
4896
4897         ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
4898         if (!ok) {
4899                 return false;
4900         }
4901         *_band_size = (size_t)band_size;
4902         return true;
4903 }
4904
4905 /*
4906  * This reads and parses an Info.plist from a TM sparsebundle looking for the
4907  * "band-size" key and value.
4908  */
4909 static bool fruit_get_bandsize(vfs_handle_struct *handle,
4910                                const char *dir,
4911                                size_t *band_size)
4912 {
4913 #define INFO_PLIST_MAX_SIZE 64*1024
4914         char *plist = NULL;
4915         struct smb_filename *smb_fname = NULL;
4916         files_struct *fsp = NULL;
4917         uint8_t *file_data = NULL;
4918         char **lines = NULL;
4919         char *band_size_line = NULL;
4920         size_t plist_file_size;
4921         ssize_t nread;
4922         int numlines;
4923         int ret;
4924         bool ok = false;
4925         NTSTATUS status;
4926
4927         plist = talloc_asprintf(talloc_tos(),
4928                                 "%s/%s/Info.plist",
4929                                 handle->conn->connectpath,
4930                                 dir);
4931         if (plist == NULL) {
4932                 ok = false;
4933                 goto out;
4934         }
4935
4936         smb_fname = synthetic_smb_fname(talloc_tos(),
4937                                         plist,
4938                                         NULL,
4939                                         NULL,
4940                                         0,
4941                                         0);
4942         if (smb_fname == NULL) {
4943                 ok = false;
4944                 goto out;
4945         }
4946
4947         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4948         if (ret != 0) {
4949                 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
4950                 ok = true;
4951                 goto out;
4952         }
4953
4954         plist_file_size = smb_fname->st.st_ex_size;
4955
4956         if (plist_file_size > INFO_PLIST_MAX_SIZE) {
4957                 DBG_INFO("%s is too large, ignoring\n", plist);
4958                 ok = true;
4959                 goto out;
4960         }
4961
4962         status = SMB_VFS_NEXT_CREATE_FILE(
4963                 handle,                         /* conn */
4964                 NULL,                           /* req */
4965                 NULL,                           /* dirfsp */
4966                 smb_fname,                      /* fname */
4967                 FILE_GENERIC_READ,              /* access_mask */
4968                 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
4969                 FILE_OPEN,                      /* create_disposition */
4970                 0,                              /* create_options */
4971                 0,                              /* file_attributes */
4972                 INTERNAL_OPEN_ONLY,             /* oplock_request */
4973                 NULL,                           /* lease */
4974                 0,                              /* allocation_size */
4975                 0,                              /* private_flags */
4976                 NULL,                           /* sd */
4977                 NULL,                           /* ea_list */
4978                 &fsp,                           /* result */
4979                 NULL,                           /* psbuf */
4980                 NULL, NULL);                    /* create context */
4981         if (!NT_STATUS_IS_OK(status)) {
4982                 DBG_INFO("Opening [%s] failed [%s]\n",
4983                          smb_fname_str_dbg(smb_fname), nt_errstr(status));
4984                 ok = false;
4985                 goto out;
4986         }
4987
4988         file_data = talloc_zero_array(talloc_tos(),
4989                                       uint8_t,
4990                                       plist_file_size + 1);
4991         if (file_data == NULL) {
4992                 ok = false;
4993                 goto out;
4994         }
4995
4996         nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
4997         if (nread != plist_file_size) {
4998                 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4999                         fsp_str_dbg(fsp), nread, plist_file_size);
5000                 ok = false;
5001                 goto out;
5002
5003         }
5004
5005         status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5006         if (!NT_STATUS_IS_OK(status)) {
5007                 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5008                 ok = false;
5009                 goto out;
5010         }
5011
5012         lines = file_lines_parse((char *)file_data,
5013                                  plist_file_size,
5014                                  &numlines,
5015                                  talloc_tos());
5016         if (lines == NULL) {
5017                 ok = false;
5018                 goto out;
5019         }
5020
5021         band_size_line = fruit_get_bandsize_line(lines, numlines);
5022         if (band_size_line == NULL) {
5023                 DBG_ERR("Didn't find band-size key in [%s]\n",
5024                         smb_fname_str_dbg(smb_fname));
5025                 ok = false;
5026                 goto out;
5027         }
5028
5029         ok = fruit_get_bandsize_from_line(band_size_line, band_size);
5030         if (!ok) {
5031                 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5032                 goto out;
5033         }
5034
5035         DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
5036
5037 out:
5038         if (fsp != NULL) {
5039                 status = close_file_free(NULL, &fsp, NORMAL_CLOSE);
5040                 if (!NT_STATUS_IS_OK(status)) {
5041                         DBG_ERR("close_file failed: %s\n", nt_errstr(status));
5042                 }
5043         }
5044         TALLOC_FREE(plist);
5045         TALLOC_FREE(smb_fname);
5046         TALLOC_FREE(file_data);
5047         TALLOC_FREE(lines);
5048         return ok;
5049 }
5050
5051 struct fruit_disk_free_state {
5052         off_t total_size;
5053 };
5054
5055 static bool fruit_get_num_bands(vfs_handle_struct *handle,
5056                                 const char *bundle,
5057                                 size_t *_nbands)
5058 {
5059         char *path = NULL;
5060         struct smb_filename *bands_dir = NULL;
5061         struct smb_Dir *dir_hnd = NULL;
5062         const char *dname = NULL;
5063         char *talloced = NULL;
5064         long offset = 0;
5065         size_t nbands;
5066         NTSTATUS status;
5067
5068         path = talloc_asprintf(talloc_tos(),
5069                                "%s/%s/bands",
5070                                handle->conn->connectpath,
5071                                bundle);
5072         if (path == NULL) {
5073                 return false;
5074         }
5075
5076         bands_dir = synthetic_smb_fname(talloc_tos(),
5077                                         path,
5078                                         NULL,
5079                                         NULL,
5080                                         0,
5081                                         0);
5082         TALLOC_FREE(path);
5083         if (bands_dir == NULL) {
5084                 return false;
5085         }
5086
5087         status = OpenDir(talloc_tos(),
5088                          handle->conn,
5089                          bands_dir,
5090                          NULL,
5091                          0,
5092                          &dir_hnd);
5093         if (!NT_STATUS_IS_OK(status)) {
5094                 TALLOC_FREE(bands_dir);
5095                 errno = map_errno_from_nt_status(status);
5096                 return false;
5097         }
5098
5099         nbands = 0;
5100
5101         while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5102                != NULL)
5103         {
5104                 if (ISDOT(dname) || ISDOTDOT(dname)) {
5105                         continue;
5106                 }
5107                 nbands++;
5108         }
5109         TALLOC_FREE(dir_hnd);
5110
5111         DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
5112
5113         TALLOC_FREE(bands_dir);
5114
5115         *_nbands = nbands;
5116         return true;
5117 }
5118
5119 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
5120                                    struct fruit_disk_free_state *state,
5121                                    const char *name)
5122 {
5123         bool ok;
5124         char *p = NULL;
5125         size_t sparsebundle_strlen = strlen("sparsebundle");
5126         size_t bandsize = 0;
5127         size_t nbands;
5128         off_t tm_size;
5129
5130         p = strstr(name, "sparsebundle");
5131         if (p == NULL) {
5132                 return true;
5133         }
5134
5135         if (p[sparsebundle_strlen] != '\0') {
5136                 return true;
5137         }
5138
5139         DBG_DEBUG("Processing sparsebundle [%s]\n", name);
5140
5141         ok = fruit_get_bandsize(handle, name, &bandsize);
5142         if (!ok) {
5143                 /*
5144                  * Beware of race conditions: this may be an uninitialized
5145                  * Info.plist that a client is just creating. We don't want let
5146                  * this to trigger complete failure.
5147                  */
5148                 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5149                 return true;
5150         }
5151
5152         ok = fruit_get_num_bands(handle, name, &nbands);
5153         if (!ok) {
5154                 /*
5155                  * Beware of race conditions: this may be a backup sparsebundle
5156                  * in an early stage lacking a bands subdirectory. We don't want
5157                  * let this to trigger complete failure.
5158                  */
5159                 DBG_ERR("Processing sparsebundle [%s] failed\n", name);
5160                 return true;
5161         }
5162
5163         /*
5164          * Arithmetic on 32-bit systems may cause overflow, depending on
5165          * size_t precision. First we check its unlikely, then we
5166          * force the precision into target off_t, then we check that
5167          * the total did not overflow either.
5168          */
5169         if (bandsize > SIZE_MAX/nbands) {
5170                 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5171                         bandsize, nbands);
5172                 return false;
5173         }
5174         tm_size = (off_t)bandsize * (off_t)nbands;
5175
5176         if (state->total_size + tm_size < state->total_size) {
5177                 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5178                         bandsize, nbands);
5179                 return false;
5180         }
5181
5182         state->total_size += tm_size;
5183
5184         DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5185                   name, (intmax_t)tm_size, (intmax_t)state->total_size);
5186
5187         return true;
5188 }
5189
5190 /**
5191  * Calculate used size of a TimeMachine volume
5192  *
5193  * This assumes that the volume is used only for TimeMachine.
5194  *
5195  * - readdir(basedir of share), then
5196  * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5197  * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5198  * - count band files in "\1.sparsebundle/bands/"
5199  * - calculate used size of all bands: band_count * band_size
5200  **/
5201 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
5202                                 const struct smb_filename *smb_fname,
5203                                 uint64_t *_bsize,
5204                                 uint64_t *_dfree,
5205                                 uint64_t *_dsize)
5206 {
5207         struct fruit_config_data *config = NULL;
5208         struct fruit_disk_free_state state = {0};
5209         struct smb_Dir *dir_hnd = NULL;
5210         const char *dname = NULL;
5211         char *talloced = NULL;
5212         long offset = 0;
5213         uint64_t dfree;
5214         uint64_t dsize;
5215         bool ok;
5216         NTSTATUS status;
5217
5218         SMB_VFS_HANDLE_GET_DATA(handle, config,
5219                                 struct fruit_config_data,
5220                                 return UINT64_MAX);
5221
5222         if (!config->time_machine ||
5223             config->time_machine_max_size == 0)
5224         {
5225                 return SMB_VFS_NEXT_DISK_FREE(handle,
5226                                               smb_fname,
5227                                               _bsize,
5228                                               _dfree,
5229                                               _dsize);
5230         }
5231
5232         status = OpenDir(talloc_tos(),
5233                          handle->conn,
5234                          smb_fname,
5235                          NULL,
5236                          0,
5237                          &dir_hnd);
5238         if (!NT_STATUS_IS_OK(status)) {
5239                 errno = map_errno_from_nt_status(status);
5240                 return UINT64_MAX;
5241         }
5242
5243         while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
5244                != NULL)
5245         {
5246                 ok = fruit_tmsize_do_dirent(handle, &state, dname);
5247                 if (!ok) {
5248                         TALLOC_FREE(talloced);
5249                         TALLOC_FREE(dir_hnd);
5250                         return UINT64_MAX;
5251                 }
5252                 TALLOC_FREE(talloced);
5253         }
5254
5255         TALLOC_FREE(dir_hnd);
5256
5257         dsize = config->time_machine_max_size / 512;
5258         dfree = dsize - (state.total_size / 512);
5259         if (dfree > dsize) {
5260                 dfree = 0;
5261         }
5262
5263         *_bsize = 512;
5264         *_dsize = dsize;
5265         *_dfree = dfree;
5266         return dfree / 2;
5267 }
5268
5269 static uint64_t fruit_fs_file_id(struct vfs_handle_struct *handle,
5270                                  const SMB_STRUCT_STAT *psbuf)
5271 {
5272         struct fruit_config_data *config = NULL;
5273
5274         SMB_VFS_HANDLE_GET_DATA(handle, config,
5275                                 struct fruit_config_data,
5276                                 return 0);
5277
5278         if (global_fruit_config.nego_aapl &&
5279             config->aapl_zero_file_id)
5280         {
5281                 return 0;
5282         }
5283
5284         return SMB_VFS_NEXT_FS_FILE_ID(handle, psbuf);
5285 }
5286
5287 static struct vfs_fn_pointers vfs_fruit_fns = {
5288         .connect_fn = fruit_connect,
5289         .disk_free_fn = fruit_disk_free,
5290
5291         /* File operations */
5292         .fchmod_fn = fruit_fchmod,
5293         .unlinkat_fn = fruit_unlinkat,
5294         .renameat_fn = fruit_renameat,
5295         .openat_fn = fruit_openat,
5296         .close_fn = fruit_close,
5297         .pread_fn = fruit_pread,
5298         .pwrite_fn = fruit_pwrite,
5299         .pread_send_fn = fruit_pread_send,
5300         .pread_recv_fn = fruit_pread_recv,
5301         .pwrite_send_fn = fruit_pwrite_send,
5302         .pwrite_recv_fn = fruit_pwrite_recv,
5303         .stat_fn = fruit_stat,
5304         .lstat_fn = fruit_lstat,
5305         .fstat_fn = fruit_fstat,
5306         .fstreaminfo_fn = fruit_fstreaminfo,
5307         .fntimes_fn = fruit_fntimes,
5308         .ftruncate_fn = fruit_ftruncate,
5309         .fallocate_fn = fruit_fallocate,
5310         .create_file_fn = fruit_create_file,
5311         .freaddir_attr_fn = fruit_freaddir_attr,
5312         .offload_read_send_fn = fruit_offload_read_send,
5313         .offload_read_recv_fn = fruit_offload_read_recv,
5314         .offload_write_send_fn = fruit_offload_write_send,
5315         .offload_write_recv_fn = fruit_offload_write_recv,
5316         .fs_file_id_fn = fruit_fs_file_id,
5317
5318         /* NT ACL operations */
5319         .fget_nt_acl_fn = fruit_fget_nt_acl,
5320         .fset_nt_acl_fn = fruit_fset_nt_acl,
5321 };
5322
5323 static_decl_vfs;
5324 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5325 {
5326         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5327                                         &vfs_fruit_fns);
5328         if (!NT_STATUS_IS_OK(ret)) {
5329                 return ret;
5330         }
5331
5332         vfs_fruit_debug_level = debug_add_class("fruit");
5333         if (vfs_fruit_debug_level == -1) {
5334                 vfs_fruit_debug_level = DBGC_VFS;
5335                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5336                           "vfs_fruit_init"));
5337         } else {
5338                 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5339                            "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5340         }
5341
5342         return ret;
5343 }