vfs_fruit: in fruit_rmdir() check ._ files before deleting them
[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 "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34
35 /*
36  * Enhanced OS X and Netatalk compatibility
37  * ========================================
38  *
39  * This modules takes advantage of vfs_streams_xattr and
40  * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
41  * loaded in the correct order:
42  *
43  *   vfs modules = catia fruit streams_xattr
44  *
45  * The module intercepts the OS X special streams "AFP_AfpInfo" and
46  * "AFP_Resource" and handles them in a special way. All other named
47  * streams are deferred to vfs_streams_xattr.
48  *
49  * The OS X client maps all NTFS illegal characters to the Unicode
50  * private range. This module optionally stores the charcters using
51  * their native ASCII encoding using vfs_catia. If you're not enabling
52  * this feature, you can skip catia from vfs modules.
53  *
54  * Finally, open modes are optionally checked against Netatalk AFP
55  * share modes.
56  *
57  * The "AFP_AfpInfo" named stream is a binary blob containing OS X
58  * extended metadata for files and directories. This module optionally
59  * reads and stores this metadata in a way compatible with Netatalk 3
60  * which stores the metadata in an EA "org.netatalk.metadata". Cf
61  * source3/include/MacExtensions.h for a description of the binary
62  * blobs content.
63  *
64  * The "AFP_Resource" named stream may be arbitrarily large, thus it
65  * can't be stored in an xattr on most filesystem. ZFS on Solaris is
66  * the only available filesystem where xattrs can be of any size and
67  * the OS supports using the file APIs for xattrs.
68  *
69  * The AFP_Resource stream is stored in an AppleDouble file prepending
70  * "._" to the filename. On Solaris with ZFS the stream is optionally
71  * stored in an EA "org.netatalk.resource".
72  *
73  *
74  * Extended Attributes
75  * ===================
76  *
77  * The OS X SMB client sends xattrs as ADS too. For xattr interop with
78  * other protocols you may want to adjust the xattr names the VFS
79  * module vfs_streams_xattr uses for storing ADS's. This defaults to
80  * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
81  * these module parameters:
82  *
83  *   streams_xattr:prefix = user.
84  *   streams_xattr:store_stream_type = false
85  *
86  *
87  * TODO
88  * ====
89  *
90  * - log diagnostic if any needed VFS module is not loaded
91  *   (eg with lp_vfs_objects())
92  * - add tests
93  */
94
95 static int vfs_fruit_debug_level = DBGC_VFS;
96
97 #undef DBGC_CLASS
98 #define DBGC_CLASS vfs_fruit_debug_level
99
100 #define FRUIT_PARAM_TYPE_NAME "fruit"
101 #define ADOUBLE_NAME_PREFIX "._"
102
103 #define NETATALK_META_XATTR "org.netatalk.Metadata"
104 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
105
106 #if defined(HAVE_ATTROPEN)
107 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
108 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
109 #else
110 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
111 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
112 #endif
113
114 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
115
116 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
117 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
118 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
119 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
120
121 struct fruit_config_data {
122         enum fruit_rsrc rsrc;
123         enum fruit_meta meta;
124         enum fruit_locking locking;
125         enum fruit_encoding encoding;
126         bool use_aapl;          /* config from smb.conf */
127         bool nego_aapl;         /* client negotiated AAPL */
128         bool use_copyfile;
129         bool readdir_attr_enabled;
130         bool unix_info_enabled;
131         bool copyfile_enabled;
132         bool veto_appledouble;
133         bool posix_rename;
134
135         /*
136          * Additional options, all enabled by default,
137          * possibly useful for analyzing performance. The associated
138          * operations with each of them may be expensive, so having
139          * the chance to disable them individually gives a chance
140          * tweaking the setup for the particular usecase.
141          */
142         bool readdir_attr_rsize;
143         bool readdir_attr_finder_info;
144         bool readdir_attr_max_access;
145 };
146
147 static const struct enum_list fruit_rsrc[] = {
148         {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
149         {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
150         {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
151         { -1, NULL}
152 };
153
154 static const struct enum_list fruit_meta[] = {
155         {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
156         {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
157         { -1, NULL}
158 };
159
160 static const struct enum_list fruit_locking[] = {
161         {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
162         {FRUIT_LOCKING_NONE, "none"},
163         { -1, NULL}
164 };
165
166 static const struct enum_list fruit_encoding[] = {
167         {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
168         {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
169         { -1, NULL}
170 };
171
172 /*****************************************************************************
173  * Defines, functions and data structures that deal with AppleDouble
174  *****************************************************************************/
175
176 /*
177  * There are two AppleDouble blobs we deal with:
178  *
179  * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
180  *   metadata in an xattr
181  *
182  * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
183  *   ._ files
184  */
185 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
186
187 /* Version info */
188 #define AD_VERSION2     0x00020000
189 #define AD_VERSION      AD_VERSION2
190
191 /*
192  * AppleDouble entry IDs.
193  */
194 #define ADEID_DFORK         1
195 #define ADEID_RFORK         2
196 #define ADEID_NAME          3
197 #define ADEID_COMMENT       4
198 #define ADEID_ICONBW        5
199 #define ADEID_ICONCOL       6
200 #define ADEID_FILEI         7
201 #define ADEID_FILEDATESI    8
202 #define ADEID_FINDERI       9
203 #define ADEID_MACFILEI      10
204 #define ADEID_PRODOSFILEI   11
205 #define ADEID_MSDOSFILEI    12
206 #define ADEID_SHORTNAME     13
207 #define ADEID_AFPFILEI      14
208 #define ADEID_DID           15
209
210 /* Private Netatalk entries */
211 #define ADEID_PRIVDEV       16
212 #define ADEID_PRIVINO       17
213 #define ADEID_PRIVSYN       18
214 #define ADEID_PRIVID        19
215 #define ADEID_MAX           (ADEID_PRIVID + 1)
216
217 /*
218  * These are the real ids for the private entries,
219  * as stored in the adouble file
220  */
221 #define AD_DEV              0x80444556
222 #define AD_INO              0x80494E4F
223 #define AD_SYN              0x8053594E
224 #define AD_ID               0x8053567E
225
226 /* Number of actually used entries */
227 #define ADEID_NUM_XATTR      8
228 #define ADEID_NUM_DOT_UND    2
229 #define ADEID_NUM_RSRC_XATTR 1
230
231 /* AppleDouble magic */
232 #define AD_APPLESINGLE_MAGIC 0x00051600
233 #define AD_APPLEDOUBLE_MAGIC 0x00051607
234 #define AD_MAGIC             AD_APPLEDOUBLE_MAGIC
235
236 /* Sizes of relevant entry bits */
237 #define ADEDLEN_MAGIC       4
238 #define ADEDLEN_VERSION     4
239 #define ADEDLEN_FILLER      16
240 #define AD_FILLER_TAG       "Netatalk        " /* should be 16 bytes */
241 #define ADEDLEN_NENTRIES    2
242 #define AD_HEADER_LEN       (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
243                              ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
244 #define AD_ENTRY_LEN_EID    4
245 #define AD_ENTRY_LEN_OFF    4
246 #define AD_ENTRY_LEN_LEN    4
247 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
248
249 /* Field widths */
250 #define ADEDLEN_NAME            255
251 #define ADEDLEN_COMMENT         200
252 #define ADEDLEN_FILEI           16
253 #define ADEDLEN_FINDERI         32
254 #define ADEDLEN_FILEDATESI      16
255 #define ADEDLEN_SHORTNAME       12 /* length up to 8.3 */
256 #define ADEDLEN_AFPFILEI        4
257 #define ADEDLEN_MACFILEI        4
258 #define ADEDLEN_PRODOSFILEI     8
259 #define ADEDLEN_MSDOSFILEI      2
260 #define ADEDLEN_DID             4
261 #define ADEDLEN_PRIVDEV         8
262 #define ADEDLEN_PRIVINO         8
263 #define ADEDLEN_PRIVSYN         8
264 #define ADEDLEN_PRIVID          4
265
266 /* Offsets */
267 #define ADEDOFF_MAGIC         0
268 #define ADEDOFF_VERSION       (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
269 #define ADEDOFF_FILLER        (ADEDOFF_VERSION + ADEDLEN_VERSION)
270 #define ADEDOFF_NENTRIES      (ADEDOFF_FILLER + ADEDLEN_FILLER)
271
272 #define ADEDOFF_FINDERI_XATTR    (AD_HEADER_LEN + \
273                                   (ADEID_NUM_XATTR * AD_ENTRY_LEN))
274 #define ADEDOFF_COMMENT_XATTR    (ADEDOFF_FINDERI_XATTR    + ADEDLEN_FINDERI)
275 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR    + ADEDLEN_COMMENT)
276 #define ADEDOFF_AFPFILEI_XATTR   (ADEDOFF_FILEDATESI_XATTR + \
277                                   ADEDLEN_FILEDATESI)
278 #define ADEDOFF_PRIVDEV_XATTR    (ADEDOFF_AFPFILEI_XATTR   + ADEDLEN_AFPFILEI)
279 #define ADEDOFF_PRIVINO_XATTR    (ADEDOFF_PRIVDEV_XATTR    + ADEDLEN_PRIVDEV)
280 #define ADEDOFF_PRIVSYN_XATTR    (ADEDOFF_PRIVINO_XATTR    + ADEDLEN_PRIVINO)
281 #define ADEDOFF_PRIVID_XATTR     (ADEDOFF_PRIVSYN_XATTR    + ADEDLEN_PRIVSYN)
282
283 #define ADEDOFF_FINDERI_DOT_UND  (AD_HEADER_LEN + \
284                                   (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
285 #define ADEDOFF_RFORK_DOT_UND    (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
286
287 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
288                          (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
289                          ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
290                          ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
291                          ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
292                          ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
293
294 #if AD_DATASZ_XATTR != 402
295 #error bad size for AD_DATASZ_XATTR
296 #endif
297
298 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
299                            (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
300                            ADEDLEN_FINDERI)
301 #if AD_DATASZ_DOT_UND != 82
302 #error bad size for AD_DATASZ_DOT_UND
303 #endif
304
305 /*
306  * Sharemode locks fcntl() offsets
307  */
308 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
309 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
310 #else
311 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
312 #endif
313 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
314
315 #define AD_FILELOCK_OPEN_WR        (AD_FILELOCK_BASE + 0)
316 #define AD_FILELOCK_OPEN_RD        (AD_FILELOCK_BASE + 1)
317 #define AD_FILELOCK_RSRC_OPEN_WR   (AD_FILELOCK_BASE + 2)
318 #define AD_FILELOCK_RSRC_OPEN_RD   (AD_FILELOCK_BASE + 3)
319 #define AD_FILELOCK_DENY_WR        (AD_FILELOCK_BASE + 4)
320 #define AD_FILELOCK_DENY_RD        (AD_FILELOCK_BASE + 5)
321 #define AD_FILELOCK_RSRC_DENY_WR   (AD_FILELOCK_BASE + 6)
322 #define AD_FILELOCK_RSRC_DENY_RD   (AD_FILELOCK_BASE + 7)
323 #define AD_FILELOCK_OPEN_NONE      (AD_FILELOCK_BASE + 8)
324 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
325
326 /* Time stuff we overload the bits a little */
327 #define AD_DATE_CREATE         0
328 #define AD_DATE_MODIFY         4
329 #define AD_DATE_BACKUP         8
330 #define AD_DATE_ACCESS        12
331 #define AD_DATE_MASK          (AD_DATE_CREATE | AD_DATE_MODIFY | \
332                                AD_DATE_BACKUP | AD_DATE_ACCESS)
333 #define AD_DATE_UNIX          (1 << 10)
334 #define AD_DATE_START         0x80000000
335 #define AD_DATE_DELTA         946684800
336 #define AD_DATE_FROM_UNIX(x)  (htonl((x) - AD_DATE_DELTA))
337 #define AD_DATE_TO_UNIX(x)    (ntohl(x) + AD_DATE_DELTA)
338
339 /* Accessor macros */
340 #define ad_getentrylen(ad,eid)     ((ad)->ad_eid[(eid)].ade_len)
341 #define ad_getentryoff(ad,eid)     ((ad)->ad_eid[(eid)].ade_off)
342 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
343 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
344
345 struct ad_entry {
346         size_t ade_off;
347         size_t ade_len;
348 };
349
350 struct adouble {
351         vfs_handle_struct        *ad_handle;
352         files_struct             *ad_fsp;
353         adouble_type_t            ad_type;
354         uint32_t                  ad_magic;
355         uint32_t                  ad_version;
356         struct ad_entry           ad_eid[ADEID_MAX];
357         char                     *ad_data;
358 };
359
360 struct ad_entry_order {
361         uint32_t id, offset, len;
362 };
363
364 /* Netatalk AppleDouble metadata xattr */
365 static const
366 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
367         {ADEID_FINDERI,    ADEDOFF_FINDERI_XATTR,    ADEDLEN_FINDERI},
368         {ADEID_COMMENT,    ADEDOFF_COMMENT_XATTR,    0},
369         {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
370         {ADEID_AFPFILEI,   ADEDOFF_AFPFILEI_XATTR,   ADEDLEN_AFPFILEI},
371         {ADEID_PRIVDEV,    ADEDOFF_PRIVDEV_XATTR,    0},
372         {ADEID_PRIVINO,    ADEDOFF_PRIVINO_XATTR,    0},
373         {ADEID_PRIVSYN,    ADEDOFF_PRIVSYN_XATTR,    0},
374         {ADEID_PRIVID,     ADEDOFF_PRIVID_XATTR,     0},
375         {0, 0, 0}
376 };
377
378 /* AppleDouble resource fork file (the ones prefixed by "._") */
379 static const
380 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
381         {ADEID_FINDERI,    ADEDOFF_FINDERI_DOT_UND,  ADEDLEN_FINDERI},
382         {ADEID_RFORK,      ADEDOFF_RFORK_DOT_UND,    0},
383         {0, 0, 0}
384 };
385
386 /*
387  * Fake AppleDouble entry oder for resource fork xattr.  The xattr
388  * isn't an AppleDouble file, it simply contains the resource data,
389  * but in order to be able to use some API calls like ad_getentryoff()
390  * we build a fake/helper struct adouble with this entry order struct.
391  */
392 static const
393 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
394         {ADEID_RFORK, 0, 0},
395         {0, 0, 0}
396 };
397
398 /* Conversion from enumerated id to on-disk AppleDouble id */
399 #define AD_EID_DISK(a) (set_eid[a])
400 static const uint32_t set_eid[] = {
401         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
402         AD_DEV, AD_INO, AD_SYN, AD_ID
403 };
404
405 /*
406  * Forward declarations
407  */
408 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
409                                adouble_type_t type, files_struct *fsp);
410 static int ad_write(struct adouble *ad, const char *path);
411 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
412
413 /**
414  * Return a pointer to an AppleDouble entry
415  *
416  * Returns NULL if the entry is not present
417  **/
418 static char *ad_get_entry(const struct adouble *ad, int eid)
419 {
420         off_t off = ad_getentryoff(ad, eid);
421         size_t len = ad_getentrylen(ad, eid);
422
423         if (off == 0 || len == 0) {
424                 return NULL;
425         }
426
427         return ad->ad_data + off;
428 }
429
430 /**
431  * Get a date
432  **/
433 static int ad_getdate(const struct adouble *ad,
434                       unsigned int dateoff,
435                       uint32_t *date)
436 {
437         bool xlate = (dateoff & AD_DATE_UNIX);
438         char *p = NULL;
439
440         dateoff &= AD_DATE_MASK;
441         p = ad_get_entry(ad, ADEID_FILEDATESI);
442         if (p == NULL) {
443                 return -1;
444         }
445
446         if (dateoff > AD_DATE_ACCESS) {
447             return -1;
448         }
449
450         memcpy(date, p + dateoff, sizeof(uint32_t));
451
452         if (xlate) {
453                 *date = AD_DATE_TO_UNIX(*date);
454         }
455         return 0;
456 }
457
458 /**
459  * Set a date
460  **/
461 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
462 {
463         bool xlate = (dateoff & AD_DATE_UNIX);
464         char *p = NULL;
465
466         p = ad_get_entry(ad, ADEID_FILEDATESI);
467         if (p == NULL) {
468                 return -1;
469         }
470
471         dateoff &= AD_DATE_MASK;
472         if (xlate) {
473                 date = AD_DATE_FROM_UNIX(date);
474         }
475
476         if (dateoff > AD_DATE_ACCESS) {
477                 return -1;
478         }
479
480         memcpy(p + dateoff, &date, sizeof(date));
481
482         return 0;
483 }
484
485
486 /**
487  * Map on-disk AppleDouble id to enumerated id
488  **/
489 static uint32_t get_eid(uint32_t eid)
490 {
491         if (eid <= 15) {
492                 return eid;
493         }
494
495         switch (eid) {
496         case AD_DEV:
497                 return ADEID_PRIVDEV;
498         case AD_INO:
499                 return ADEID_PRIVINO;
500         case AD_SYN:
501                 return ADEID_PRIVSYN;
502         case AD_ID:
503                 return ADEID_PRIVID;
504         default:
505                 break;
506         }
507
508         return 0;
509 }
510
511 /**
512  * Pack AppleDouble structure into data buffer
513  **/
514 static bool ad_pack(struct adouble *ad)
515 {
516         uint32_t       eid;
517         uint16_t       nent;
518         uint32_t       bufsize;
519         uint32_t       offset = 0;
520
521         bufsize = talloc_get_size(ad->ad_data);
522
523         if (offset + ADEDLEN_MAGIC < offset ||
524                         offset + ADEDLEN_MAGIC >= bufsize) {
525                 return false;
526         }
527         RSIVAL(ad->ad_data, offset, ad->ad_magic);
528         offset += ADEDLEN_MAGIC;
529
530         if (offset + ADEDLEN_VERSION < offset ||
531                         offset + ADEDLEN_VERSION >= bufsize) {
532                 return false;
533         }
534         RSIVAL(ad->ad_data, offset, ad->ad_version);
535         offset += ADEDLEN_VERSION;
536
537         if (offset + ADEDLEN_FILLER < offset ||
538                         offset + ADEDLEN_FILLER >= bufsize) {
539                 return false;
540         }
541         if (ad->ad_type == ADOUBLE_RSRC) {
542                 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
543         }
544         offset += ADEDLEN_FILLER;
545
546         if (offset + ADEDLEN_NENTRIES < offset ||
547                         offset + ADEDLEN_NENTRIES >= bufsize) {
548                 return false;
549         }
550         offset += ADEDLEN_NENTRIES;
551
552         for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
553                 if (ad->ad_eid[eid].ade_off == 0) {
554                         /*
555                          * ade_off is also used as indicator whether a
556                          * specific entry is used or not
557                          */
558                         continue;
559                 }
560
561                 if (offset + AD_ENTRY_LEN_EID < offset ||
562                                 offset + AD_ENTRY_LEN_EID >= bufsize) {
563                         return false;
564                 }
565                 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
566                 offset += AD_ENTRY_LEN_EID;
567
568                 if (offset + AD_ENTRY_LEN_OFF < offset ||
569                                 offset + AD_ENTRY_LEN_OFF >= bufsize) {
570                         return false;
571                 }
572                 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
573                 offset += AD_ENTRY_LEN_OFF;
574
575                 if (offset + AD_ENTRY_LEN_LEN < offset ||
576                                 offset + AD_ENTRY_LEN_LEN >= bufsize) {
577                         return false;
578                 }
579                 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
580                 offset += AD_ENTRY_LEN_LEN;
581
582                 nent++;
583         }
584
585         if (ADEDOFF_NENTRIES + 2 >= bufsize) {
586                 return false;
587         }
588         RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
589
590         return true;
591 }
592
593 /**
594  * Unpack an AppleDouble blob into a struct adoble
595  **/
596 static bool ad_unpack(struct adouble *ad, const size_t nentries,
597                       size_t filesize)
598 {
599         size_t bufsize = talloc_get_size(ad->ad_data);
600         size_t adentries, i;
601         uint32_t eid, len, off;
602
603         /*
604          * The size of the buffer ad->ad_data is checked when read, so
605          * we wouldn't have to check our own offsets, a few extra
606          * checks won't hurt though. We have to check the offsets we
607          * read from the buffer anyway.
608          */
609
610         if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
611                 DEBUG(1, ("bad size\n"));
612                 return false;
613         }
614
615         ad->ad_magic = RIVAL(ad->ad_data, 0);
616         ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
617         if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
618                 DEBUG(1, ("wrong magic or version\n"));
619                 return false;
620         }
621
622         adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
623         if (adentries != nentries) {
624                 DEBUG(1, ("invalid number of entries: %zu\n",
625                           adentries));
626                 return false;
627         }
628
629         /* now, read in the entry bits */
630         for (i = 0; i < adentries; i++) {
631                 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
632                 eid = get_eid(eid);
633                 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
634                 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
635
636                 if (!eid || eid > ADEID_MAX) {
637                         DEBUG(1, ("bogus eid %d\n", eid));
638                         return false;
639                 }
640
641                 /*
642                  * All entries other than the resource fork are
643                  * expected to be read into the ad_data buffer, so
644                  * ensure the specified offset is within that bound
645                  */
646                 if ((off > bufsize) && (eid != ADEID_RFORK)) {
647                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
648                                   eid, off, len));
649                         return false;
650                 }
651
652                 /*
653                  * All entries besides FinderInfo and resource fork
654                  * must fit into the buffer. FinderInfo is special as
655                  * it may be larger then the default 32 bytes (if it
656                  * contains marshalled xattrs), but we will fixup that
657                  * in ad_convert(). And the resource fork is never
658                  * accessed directly by the ad_data buf (also see
659                  * comment above) anyway.
660                  */
661                 if ((eid != ADEID_RFORK) &&
662                     (eid != ADEID_FINDERI) &&
663                     ((off + len) > bufsize)) {
664                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
665                                   eid, off, len));
666                         return false;
667                 }
668
669                 /*
670                  * That would be obviously broken
671                  */
672                 if (off > filesize) {
673                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
674                                   eid, off, len));
675                         return false;
676                 }
677
678                 /*
679                  * Check for any entry that has its end beyond the
680                  * filesize.
681                  */
682                 if (off + len < off) {
683                         DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
684                                   ", len: %" PRIu32 "\n",
685                                   eid, off, len));
686                         return false;
687
688                 }
689                 if (off + len > filesize) {
690                         /*
691                          * If this is the resource fork entry, we fix
692                          * up the length, for any other entry we bail
693                          * out.
694                          */
695                         if (eid != ADEID_RFORK) {
696                                 DEBUG(1, ("bogus eid %d: off: %" PRIu32
697                                           ", len: %" PRIu32 "\n",
698                                           eid, off, len));
699                                 return false;
700                         }
701
702                         /*
703                          * Fixup the resource fork entry by limiting
704                          * the size to entryoffset - filesize.
705                          */
706                         len = filesize - off;
707                         DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
708                                   ", len: %" PRIu32 "\n", off, len));
709                 }
710
711                 ad->ad_eid[eid].ade_off = off;
712                 ad->ad_eid[eid].ade_len = len;
713         }
714
715         return true;
716 }
717
718 /**
719  * Convert from Apple's ._ file to Netatalk
720  *
721  * Apple's AppleDouble may contain a FinderInfo entry longer then 32
722  * bytes containing packed xattrs. Netatalk can't deal with that, so
723  * we simply discard the packed xattrs.
724  *
725  * @return -1 in case an error occurred, 0 if no conversion was done, 1
726  * otherwise
727  **/
728 static int ad_convert(struct adouble *ad, int fd)
729 {
730         int rc = 0;
731         char *map = MAP_FAILED;
732         size_t origlen;
733
734         origlen = ad_getentryoff(ad, ADEID_RFORK) +
735                 ad_getentrylen(ad, ADEID_RFORK);
736
737         /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
738         map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
739         if (map == MAP_FAILED) {
740                 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
741                 rc = -1;
742                 goto exit;
743         }
744
745         if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
746                 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
747                         map + ad_getentryoff(ad, ADEID_RFORK),
748                         ad_getentrylen(ad, ADEID_RFORK));
749         }
750
751         ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
752         ad_setentryoff(ad, ADEID_RFORK,
753                        ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
754
755         /*
756          * FIXME: direct ftruncate(), but we don't have a fsp for the
757          * VFS call
758          */
759         rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
760                        + ad_getentrylen(ad, ADEID_RFORK));
761
762 exit:
763         if (map != MAP_FAILED) {
764                 munmap(map, origlen);
765         }
766         return rc;
767 }
768
769 /**
770  * Read and parse Netatalk AppleDouble metadata xattr
771  **/
772 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
773 {
774         int      rc = 0;
775         ssize_t  ealen;
776         bool     ok;
777
778         DEBUG(10, ("reading meta xattr for %s\n", path));
779
780         ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
781                                  AFPINFO_EA_NETATALK, ad->ad_data,
782                                  AD_DATASZ_XATTR);
783         if (ealen == -1) {
784                 switch (errno) {
785                 case ENOATTR:
786                 case ENOENT:
787                         if (errno == ENOATTR) {
788                                 errno = ENOENT;
789                         }
790                         rc = -1;
791                         goto exit;
792                 default:
793                         DEBUG(2, ("error reading meta xattr: %s\n",
794                                   strerror(errno)));
795                         rc = -1;
796                         goto exit;
797                 }
798         }
799         if (ealen != AD_DATASZ_XATTR) {
800                 DEBUG(2, ("bad size %zd\n", ealen));
801                 errno = EINVAL;
802                 rc = -1;
803                 goto exit;
804         }
805
806         /* Now parse entries */
807         ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
808         if (!ok) {
809                 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
810                 errno = EINVAL;
811                 rc = -1;
812                 goto exit;
813         }
814
815         if (!ad_getentryoff(ad, ADEID_FINDERI)
816             || !ad_getentryoff(ad, ADEID_COMMENT)
817             || !ad_getentryoff(ad, ADEID_FILEDATESI)
818             || !ad_getentryoff(ad, ADEID_AFPFILEI)
819             || !ad_getentryoff(ad, ADEID_PRIVDEV)
820             || !ad_getentryoff(ad, ADEID_PRIVINO)
821             || !ad_getentryoff(ad, ADEID_PRIVSYN)
822             || !ad_getentryoff(ad, ADEID_PRIVID)) {
823                 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
824                 errno = EINVAL;
825                 rc = -1;
826                 goto exit;
827         }
828
829 exit:
830         DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
831
832         if (rc != 0) {
833                 ealen = -1;
834                 if (errno == EINVAL) {
835                         become_root();
836                         removexattr(path, AFPINFO_EA_NETATALK);
837                         unbecome_root();
838                         errno = ENOENT;
839                 }
840         }
841         return ealen;
842 }
843
844 /**
845  * Read and parse resource fork, either ._ AppleDouble file or xattr
846  **/
847 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
848 {
849         struct fruit_config_data *config = NULL;
850         int fd = -1;
851         int rc = 0;
852         ssize_t len;
853         char *adpath = NULL;
854         bool opened = false;
855         int mode;
856         struct adouble *meta_ad = NULL;
857         SMB_STRUCT_STAT sbuf;
858         bool ok;
859         int saved_errno = 0;
860
861         SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
862                                 struct fruit_config_data, return -1);
863
864         /* Try rw first so we can use the fd in ad_convert() */
865         mode = O_RDWR;
866
867         if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
868                 fd = ad->ad_fsp->fh->fd;
869         } else {
870                 if (config->rsrc == FRUIT_RSRC_XATTR) {
871                         adpath = talloc_strdup(talloc_tos(), path);
872                 } else {
873                         rc = adouble_path(talloc_tos(), path, &adpath);
874                         if (rc != 0) {
875                                 goto exit;
876                         }
877                 }
878
879         retry:
880                 if (config->rsrc == FRUIT_RSRC_XATTR) {
881 #ifndef HAVE_ATTROPEN
882                         errno = ENOSYS;
883                         rc = -1;
884                         goto exit;
885 #else
886                         /* FIXME: direct Solaris xattr syscall */
887                         fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
888                                       mode, 0);
889 #endif
890                 } else {
891                         /* FIXME: direct open(), don't have an fsp */
892                         fd = open(adpath, mode);
893                 }
894
895                 if (fd == -1) {
896                         switch (errno) {
897                         case EROFS:
898                         case EACCES:
899                                 if (mode == O_RDWR) {
900                                         mode = O_RDONLY;
901                                         goto retry;
902                                 }
903                                 /* fall through ... */
904                         default:
905                                 DEBUG(2, ("open AppleDouble: %s, %s\n",
906                                           adpath, strerror(errno)));
907                                 rc = -1;
908                                 goto exit;
909                         }
910                 }
911                 opened = true;
912         }
913
914         if (config->rsrc == FRUIT_RSRC_XATTR) {
915                 /* FIXME: direct sys_fstat(), don't have an fsp */
916                 rc = sys_fstat(
917                         fd, &sbuf,
918                         lp_fake_directory_create_times(
919                                 SNUM(ad->ad_handle->conn)));
920                 if (rc != 0) {
921                         goto exit;
922                 }
923                 len = sbuf.st_ex_size;
924                 ad_setentrylen(ad, ADEID_RFORK, len);
925         } else {
926                 /* FIXME: direct sys_pread(), don't have an fsp */
927                 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
928                 if (len != AD_DATASZ_DOT_UND) {
929                         DEBUG(2, ("%s: bad size: %zd\n",
930                                   strerror(errno), len));
931                         rc = -1;
932                         goto exit;
933                 }
934
935                 /* FIXME: direct sys_fstat(), we don't have an fsp */
936                 rc = sys_fstat(fd, &sbuf,
937                                lp_fake_directory_create_times(
938                                        SNUM(ad->ad_handle->conn)));
939                 if (rc != 0) {
940                         goto exit;
941                 }
942
943                 /* Now parse entries */
944                 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
945                 if (!ok) {
946                         DEBUG(1, ("invalid AppleDouble resource %s\n", path));
947                         errno = EINVAL;
948                         rc = -1;
949                         goto exit;
950                 }
951
952                 if ((ad_getentryoff(ad, ADEID_FINDERI)
953                      != ADEDOFF_FINDERI_DOT_UND)
954                     || (ad_getentrylen(ad, ADEID_FINDERI)
955                         < ADEDLEN_FINDERI)
956                     || (ad_getentryoff(ad, ADEID_RFORK)
957                         < ADEDOFF_RFORK_DOT_UND)) {
958                         DEBUG(2, ("invalid AppleDouble resource %s\n", path));
959                         errno = EINVAL;
960                         rc = -1;
961                         goto exit;
962                 }
963
964                 if ((mode == O_RDWR)
965                     && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
966                         char *p_ad = NULL;
967                         char *p_meta_ad = NULL;
968
969                         rc = ad_convert(ad, fd);
970                         if (rc != 0) {
971                                 rc = -1;
972                                 goto exit;
973                         }
974                         /*
975                          * Can't use ad_write() because we might not have a fsp
976                          */
977                         ok = ad_pack(ad);
978                         if (!ok) {
979                                 rc = -1;
980                                 goto exit;
981                         }
982                         /* FIXME: direct sys_pwrite(), don't have an fsp */
983                         len = sys_pwrite(fd, ad->ad_data,
984                                          AD_DATASZ_DOT_UND, 0);
985                         if (len != AD_DATASZ_DOT_UND) {
986                                 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
987                                 rc = -1;
988                                 goto exit;
989                         }
990
991                         meta_ad = ad_init(talloc_tos(), ad->ad_handle,
992                                           ADOUBLE_META, NULL);
993                         if (meta_ad == NULL) {
994                                 rc = -1;
995                                 goto exit;
996                         }
997
998                         p_ad = ad_get_entry(ad, ADEID_FINDERI);
999                         if (p_ad == NULL) {
1000                                 rc = -1;
1001                                 goto exit;
1002                         }
1003                         p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1004                         if (p_meta_ad == NULL) {
1005                                 rc = -1;
1006                                 goto exit;
1007                         }
1008
1009                         memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1010
1011                         rc = ad_write(meta_ad, path);
1012                         if (rc != 0) {
1013                                 rc = -1;
1014                                 goto exit;
1015                         }
1016                 }
1017         }
1018
1019         DEBUG(10, ("opened AppleDouble: %s\n", path));
1020
1021 exit:
1022         if (rc != 0) {
1023                 saved_errno = errno;
1024                 len = -1;
1025         }
1026         if (opened && fd != -1) {
1027                 close(fd);
1028         }
1029         TALLOC_FREE(adpath);
1030         TALLOC_FREE(meta_ad);
1031         if (rc != 0) {
1032                 errno = saved_errno;
1033         }
1034         return len;
1035 }
1036
1037 /**
1038  * Read and unpack an AppleDouble metadata xattr or resource
1039  **/
1040 static ssize_t ad_read(struct adouble *ad, const char *path)
1041 {
1042         switch (ad->ad_type) {
1043         case ADOUBLE_META:
1044                 return ad_header_read_meta(ad, path);
1045         case ADOUBLE_RSRC:
1046                 return ad_header_read_rsrc(ad, path);
1047         default:
1048                 return -1;
1049         }
1050 }
1051
1052 /**
1053  * Allocate a struct adouble without initialiing it
1054  *
1055  * The struct is either hang of the fsp extension context or if fsp is
1056  * NULL from ctx.
1057  *
1058  * @param[in] ctx        talloc context
1059  * @param[in] handle     vfs handle
1060  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1061
1062  * @param[in] fsp        if not NULL (for stream IO), the adouble handle is
1063  *                       added as an fsp extension
1064  *
1065  * @return               adouble handle
1066  **/
1067 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1068                                 adouble_type_t type, files_struct *fsp)
1069 {
1070         int rc = 0;
1071         size_t adsize = 0;
1072         struct adouble *ad;
1073         struct fruit_config_data *config;
1074
1075         SMB_VFS_HANDLE_GET_DATA(handle, config,
1076                                 struct fruit_config_data, return NULL);
1077
1078         switch (type) {
1079         case ADOUBLE_META:
1080                 adsize = AD_DATASZ_XATTR;
1081                 break;
1082         case ADOUBLE_RSRC:
1083                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1084                         adsize = AD_DATASZ_DOT_UND;
1085                 }
1086                 break;
1087         default:
1088                 return NULL;
1089         }
1090
1091         if (!fsp) {
1092                 ad = talloc_zero(ctx, struct adouble);
1093                 if (ad == NULL) {
1094                         rc = -1;
1095                         goto exit;
1096                 }
1097                 if (adsize) {
1098                         ad->ad_data = talloc_zero_array(ad, char, adsize);
1099                 }
1100         } else {
1101                 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1102                                                              struct adouble,
1103                                                              NULL);
1104                 if (ad == NULL) {
1105                         rc = -1;
1106                         goto exit;
1107                 }
1108                 if (adsize) {
1109                         ad->ad_data = talloc_zero_array(
1110                                 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1111                                 char, adsize);
1112                 }
1113                 ad->ad_fsp = fsp;
1114         }
1115
1116         if (adsize && ad->ad_data == NULL) {
1117                 rc = -1;
1118                 goto exit;
1119         }
1120         ad->ad_handle = handle;
1121         ad->ad_type = type;
1122         ad->ad_magic = AD_MAGIC;
1123         ad->ad_version = AD_VERSION;
1124
1125 exit:
1126         if (rc != 0) {
1127                 TALLOC_FREE(ad);
1128         }
1129         return ad;
1130 }
1131
1132 /**
1133  * Allocate and initialize a new struct adouble
1134  *
1135  * @param[in] ctx        talloc context
1136  * @param[in] handle     vfs handle
1137  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1138  * @param[in] fsp        file handle, may be NULL for a type of e_ad_meta
1139  *
1140  * @return               adouble handle, initialized
1141  **/
1142 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1143                                adouble_type_t type, files_struct *fsp)
1144 {
1145         int rc = 0;
1146         const struct ad_entry_order  *eid;
1147         struct adouble *ad = NULL;
1148         struct fruit_config_data *config;
1149         time_t t = time(NULL);
1150
1151         SMB_VFS_HANDLE_GET_DATA(handle, config,
1152                                 struct fruit_config_data, return NULL);
1153
1154         switch (type) {
1155         case ADOUBLE_META:
1156                 eid = entry_order_meta_xattr;
1157                 break;
1158         case ADOUBLE_RSRC:
1159                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1160                         eid = entry_order_dot_und;
1161                 } else {
1162                         eid = entry_order_rsrc_xattr;
1163                 }
1164                 break;
1165         default:
1166                 return NULL;
1167         }
1168
1169         ad = ad_alloc(ctx, handle, type, fsp);
1170         if (ad == NULL) {
1171                 return NULL;
1172         }
1173
1174         while (eid->id) {
1175                 ad->ad_eid[eid->id].ade_off = eid->offset;
1176                 ad->ad_eid[eid->id].ade_len = eid->len;
1177                 eid++;
1178         }
1179
1180         /* put something sane in the date fields */
1181         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1182         ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1183         ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1184         ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1185
1186         if (rc != 0) {
1187                 TALLOC_FREE(ad);
1188         }
1189         return ad;
1190 }
1191
1192 /**
1193  * Return AppleDouble data for a file
1194  *
1195  * @param[in] ctx      talloc context
1196  * @param[in] handle   vfs handle
1197  * @param[in] path     pathname to file or directory
1198  * @param[in] type     type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1199  *
1200  * @return             talloced struct adouble or NULL on error
1201  **/
1202 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1203                               const char *path, adouble_type_t type)
1204 {
1205         int rc = 0;
1206         ssize_t len;
1207         struct adouble *ad = NULL;
1208
1209         DEBUG(10, ("ad_get(%s) called for %s\n",
1210                    type == ADOUBLE_META ? "meta" : "rsrc", path));
1211
1212         ad = ad_alloc(ctx, handle, type, NULL);
1213         if (ad == NULL) {
1214                 rc = -1;
1215                 goto exit;
1216         }
1217
1218         len = ad_read(ad, path);
1219         if (len == -1) {
1220                 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1221                 rc = -1;
1222                 goto exit;
1223         }
1224
1225 exit:
1226         DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1227                   type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1228
1229         if (rc != 0) {
1230                 TALLOC_FREE(ad);
1231         }
1232         return ad;
1233 }
1234
1235 /**
1236  * Set AppleDouble metadata on a file or directory
1237  *
1238  * @param[in] ad      adouble handle
1239
1240  * @param[in] path    pathname to file or directory, may be NULL for a
1241  *                    resource fork
1242  *
1243  * @return            status code, 0 means success
1244  **/
1245 static int ad_write(struct adouble *ad, const char *path)
1246 {
1247         int rc = 0;
1248         ssize_t len;
1249         bool ok;
1250
1251         ok = ad_pack(ad);
1252         if (!ok) {
1253                 return -1;
1254         }
1255
1256         switch (ad->ad_type) {
1257         case ADOUBLE_META:
1258                 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1259                                       AFPINFO_EA_NETATALK, ad->ad_data,
1260                                       AD_DATASZ_XATTR, 0);
1261                 break;
1262         case ADOUBLE_RSRC:
1263                 if ((ad->ad_fsp == NULL)
1264                     || (ad->ad_fsp->fh == NULL)
1265                     || (ad->ad_fsp->fh->fd == -1)) {
1266                         rc = -1;
1267                         goto exit;
1268                 }
1269                 /* FIXME: direct sys_pwrite(), don't have an fsp */
1270                 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1271                                  talloc_get_size(ad->ad_data), 0);
1272                 if (len != talloc_get_size(ad->ad_data)) {
1273                         DEBUG(1, ("short write on %s: %zd",
1274                                   fsp_str_dbg(ad->ad_fsp), len));
1275                         rc = -1;
1276                         goto exit;
1277                 }
1278                 break;
1279         default:
1280                 return -1;
1281         }
1282 exit:
1283         return rc;
1284 }
1285
1286 /*****************************************************************************
1287  * Helper functions
1288  *****************************************************************************/
1289
1290 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1291 {
1292         if (strncasecmp_m(smb_fname->stream_name,
1293                           AFPINFO_STREAM_NAME,
1294                           strlen(AFPINFO_STREAM_NAME)) == 0) {
1295                 return true;
1296         }
1297         return false;
1298 }
1299
1300 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1301 {
1302         if (strncasecmp_m(smb_fname->stream_name,
1303                           AFPRESOURCE_STREAM_NAME,
1304                           strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1305                 return true;
1306         }
1307         return false;
1308 }
1309
1310 /**
1311  * Test whether stream is an Apple stream, not used atm
1312  **/
1313 #if 0
1314 static bool is_apple_stream(const struct smb_filename *smb_fname)
1315 {
1316         if (is_afpinfo_stream(smb_fname)) {
1317                 return true;
1318         }
1319         if (is_afpresource_stream(smb_fname)) {
1320                 return true;
1321         }
1322         return false;
1323 }
1324 #endif
1325
1326 /**
1327  * Initialize config struct from our smb.conf config parameters
1328  **/
1329 static int init_fruit_config(vfs_handle_struct *handle)
1330 {
1331         struct fruit_config_data *config;
1332         int enumval;
1333
1334         config = talloc_zero(handle->conn, struct fruit_config_data);
1335         if (!config) {
1336                 DEBUG(1, ("talloc_zero() failed\n"));
1337                 errno = ENOMEM;
1338                 return -1;
1339         }
1340
1341         /*
1342          * Versions up to Samba 4.5.x had a spelling bug in the
1343          * fruit:resource option calling lp_parm_enum with
1344          * "res*s*ource" (ie two s).
1345          *
1346          * In Samba 4.6 we accept both the wrong and the correct
1347          * spelling, in Samba 4.7 the bad spelling will be removed.
1348          */
1349         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1350                                "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1351         if (enumval == -1) {
1352                 DEBUG(1, ("value for %s: resource type unknown\n",
1353                           FRUIT_PARAM_TYPE_NAME));
1354                 return -1;
1355         }
1356         config->rsrc = (enum fruit_rsrc)enumval;
1357
1358         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1359                                "resource", fruit_rsrc, enumval);
1360         if (enumval == -1) {
1361                 DEBUG(1, ("value for %s: resource type unknown\n",
1362                           FRUIT_PARAM_TYPE_NAME));
1363                 return -1;
1364         }
1365         config->rsrc = (enum fruit_rsrc)enumval;
1366
1367         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1368                                "metadata", fruit_meta, FRUIT_META_NETATALK);
1369         if (enumval == -1) {
1370                 DEBUG(1, ("value for %s: metadata type unknown\n",
1371                           FRUIT_PARAM_TYPE_NAME));
1372                 return -1;
1373         }
1374         config->meta = (enum fruit_meta)enumval;
1375
1376         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1377                                "locking", fruit_locking, FRUIT_LOCKING_NONE);
1378         if (enumval == -1) {
1379                 DEBUG(1, ("value for %s: locking type unknown\n",
1380                           FRUIT_PARAM_TYPE_NAME));
1381                 return -1;
1382         }
1383         config->locking = (enum fruit_locking)enumval;
1384
1385         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1386                                "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1387         if (enumval == -1) {
1388                 DEBUG(1, ("value for %s: encoding type unknown\n",
1389                           FRUIT_PARAM_TYPE_NAME));
1390                 return -1;
1391         }
1392         config->encoding = (enum fruit_encoding)enumval;
1393
1394         config->veto_appledouble = lp_parm_bool(
1395                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1396                 "veto_appledouble", true);
1397
1398         config->use_aapl = lp_parm_bool(
1399                 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1400
1401         config->unix_info_enabled = lp_parm_bool(
1402                 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1403
1404         config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1405                                            "copyfile", false);
1406
1407         config->posix_rename = lp_parm_bool(
1408                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1409
1410         config->readdir_attr_rsize = lp_parm_bool(
1411                 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1412
1413         config->readdir_attr_finder_info = lp_parm_bool(
1414                 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1415
1416         config->readdir_attr_max_access = lp_parm_bool(
1417                 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1418
1419         SMB_VFS_HANDLE_SET_DATA(handle, config,
1420                                 NULL, struct fruit_config_data,
1421                                 return -1);
1422
1423         return 0;
1424 }
1425
1426 /**
1427  * Prepend "._" to a basename
1428  **/
1429 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1430 {
1431         char *parent;
1432         const char *base;
1433
1434         if (!parent_dirname(ctx, path_in, &parent, &base)) {
1435                 return -1;
1436         }
1437
1438         *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1439         if (*path_out == NULL) {
1440                 return -1;
1441         }
1442
1443         return 0;
1444 }
1445
1446 /**
1447  * Allocate and initialize an AfpInfo struct
1448  **/
1449 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1450 {
1451         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1452         if (ai == NULL) {
1453                 return NULL;
1454         }
1455         ai->afpi_Signature = AFP_Signature;
1456         ai->afpi_Version = AFP_Version;
1457         ai->afpi_BackupTime = AD_DATE_START;
1458         return ai;
1459 }
1460
1461 /**
1462  * Pack an AfpInfo struct into a buffer
1463  *
1464  * Buffer size must be at least AFP_INFO_SIZE
1465  * Returns size of packed buffer
1466  **/
1467 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1468 {
1469         memset(buf, 0, AFP_INFO_SIZE);
1470
1471         RSIVAL(buf, 0, ai->afpi_Signature);
1472         RSIVAL(buf, 4, ai->afpi_Version);
1473         RSIVAL(buf, 12, ai->afpi_BackupTime);
1474         memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1475
1476         return AFP_INFO_SIZE;
1477 }
1478
1479 /**
1480  * Unpack a buffer into a AfpInfo structure
1481  *
1482  * Buffer size must be at least AFP_INFO_SIZE
1483  * Returns allocated AfpInfo struct
1484  **/
1485 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1486 {
1487         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1488         if (ai == NULL) {
1489                 return NULL;
1490         }
1491
1492         ai->afpi_Signature = RIVAL(data, 0);
1493         ai->afpi_Version = RIVAL(data, 4);
1494         ai->afpi_BackupTime = RIVAL(data, 12);
1495         memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1496                sizeof(ai->afpi_FinderInfo));
1497
1498         if (ai->afpi_Signature != AFP_Signature
1499             || ai->afpi_Version != AFP_Version) {
1500                 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1501                 TALLOC_FREE(ai);
1502         }
1503
1504         return ai;
1505 }
1506
1507 /**
1508  * Fake an inode number from the md5 hash of the (xattr) name
1509  **/
1510 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1511 {
1512         MD5_CTX ctx;
1513         unsigned char hash[16];
1514         SMB_INO_T result;
1515         char *upper_sname;
1516
1517         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1518         SMB_ASSERT(upper_sname != NULL);
1519
1520         MD5Init(&ctx);
1521         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1522                   sizeof(sbuf->st_ex_dev));
1523         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1524                   sizeof(sbuf->st_ex_ino));
1525         MD5Update(&ctx, (unsigned char *)upper_sname,
1526                   talloc_get_size(upper_sname)-1);
1527         MD5Final(hash, &ctx);
1528
1529         TALLOC_FREE(upper_sname);
1530
1531         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1532         memcpy(&result, hash, sizeof(result));
1533
1534         DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1535                    sname, (unsigned long long)result));
1536
1537         return result;
1538 }
1539
1540 /**
1541  * Ensure ad_fsp is still valid
1542  **/
1543 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1544 {
1545         if (ad->ad_fsp == fsp) {
1546                 return true;
1547         }
1548         ad->ad_fsp = fsp;
1549
1550         return true;
1551 }
1552
1553 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1554                              struct stream_struct **streams,
1555                              const char *name, off_t size,
1556                              off_t alloc_size)
1557 {
1558         struct stream_struct *tmp;
1559
1560         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1561                              (*num_streams)+1);
1562         if (tmp == NULL) {
1563                 return false;
1564         }
1565
1566         tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1567         if (tmp[*num_streams].name == NULL) {
1568                 return false;
1569         }
1570
1571         tmp[*num_streams].size = size;
1572         tmp[*num_streams].alloc_size = alloc_size;
1573
1574         *streams = tmp;
1575         *num_streams += 1;
1576         return true;
1577 }
1578
1579 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1580                              struct stream_struct **streams,
1581                              const char *name)
1582 {
1583         struct stream_struct *tmp = *streams;
1584         unsigned int i;
1585
1586         if (*num_streams == 0) {
1587                 return true;
1588         }
1589
1590         for (i = 0; i < *num_streams; i++) {
1591                 if (strequal_m(tmp[i].name, name)) {
1592                         break;
1593                 }
1594         }
1595
1596         if (i == *num_streams) {
1597                 return true;
1598         }
1599
1600         TALLOC_FREE(tmp[i].name);
1601         if (*num_streams - 1 > i) {
1602                 memmove(&tmp[i], &tmp[i+1],
1603                         (*num_streams - i - 1) * sizeof(struct stream_struct));
1604         }
1605
1606         *num_streams -= 1;
1607         return true;
1608 }
1609
1610 static bool ad_empty_finderinfo(const struct adouble *ad)
1611 {
1612         int cmp;
1613         char emptybuf[ADEDLEN_FINDERI] = {0};
1614         char *fi = NULL;
1615
1616         fi = ad_get_entry(ad, ADEID_FINDERI);
1617         if (fi == NULL) {
1618                 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1619                 return false;
1620         }
1621
1622         cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1623         return (cmp == 0);
1624 }
1625
1626 static bool ai_empty_finderinfo(const AfpInfo *ai)
1627 {
1628         int cmp;
1629         char emptybuf[ADEDLEN_FINDERI] = {0};
1630
1631         cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1632         return (cmp == 0);
1633 }
1634
1635 /**
1636  * Update btime with btime from Netatalk
1637  **/
1638 static void update_btime(vfs_handle_struct *handle,
1639                          struct smb_filename *smb_fname)
1640 {
1641         uint32_t t;
1642         struct timespec creation_time = {0};
1643         struct adouble *ad;
1644         struct fruit_config_data *config = NULL;
1645
1646         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1647                                 return);
1648
1649         switch (config->meta) {
1650         case FRUIT_META_STREAM:
1651                 return;
1652         case FRUIT_META_NETATALK:
1653                 /* Handled below */
1654                 break;
1655         default:
1656                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1657                 return;
1658         }
1659
1660         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1661         if (ad == NULL) {
1662                 return;
1663         }
1664         if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1665                 TALLOC_FREE(ad);
1666                 return;
1667         }
1668         TALLOC_FREE(ad);
1669
1670         creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1671         update_stat_ex_create_time(&smb_fname->st, creation_time);
1672
1673         return;
1674 }
1675
1676 /**
1677  * Map an access mask to a Netatalk single byte byte range lock
1678  **/
1679 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1680                                     uint32_t access_mask)
1681 {
1682         off_t offset;
1683
1684         switch (access_mask) {
1685         case FILE_READ_DATA:
1686                 offset = AD_FILELOCK_OPEN_RD;
1687                 break;
1688
1689         case FILE_WRITE_DATA:
1690         case FILE_APPEND_DATA:
1691                 offset = AD_FILELOCK_OPEN_WR;
1692                 break;
1693
1694         default:
1695                 offset = AD_FILELOCK_OPEN_NONE;
1696                 break;
1697         }
1698
1699         if (fork_type == APPLE_FORK_RSRC) {
1700                 if (offset == AD_FILELOCK_OPEN_NONE) {
1701                         offset = AD_FILELOCK_RSRC_OPEN_NONE;
1702                 } else {
1703                         offset += 2;
1704                 }
1705         }
1706
1707         return offset;
1708 }
1709
1710 /**
1711  * Map a deny mode to a Netatalk brl
1712  **/
1713 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1714                                       uint32_t deny_mode)
1715 {
1716         off_t offset;
1717
1718         switch (deny_mode) {
1719         case DENY_READ:
1720                 offset = AD_FILELOCK_DENY_RD;
1721                 break;
1722
1723         case DENY_WRITE:
1724                 offset = AD_FILELOCK_DENY_WR;
1725                 break;
1726
1727         default:
1728                 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1729         }
1730
1731         if (fork_type == APPLE_FORK_RSRC) {
1732                 offset += 2;
1733         }
1734
1735         return offset;
1736 }
1737
1738 /**
1739  * Call fcntl() with an exclusive F_GETLK request in order to
1740  * determine if there's an exisiting shared lock
1741  *
1742  * @return true if the requested lock was found or any error occurred
1743  *         false if the lock was not found
1744  **/
1745 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1746 {
1747         bool result;
1748         off_t offset = in_offset;
1749         off_t len = 1;
1750         int type = F_WRLCK;
1751         pid_t pid;
1752
1753         result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1754         if (result == false) {
1755                 return true;
1756         }
1757
1758         if (type != F_UNLCK) {
1759                 return true;
1760         }
1761
1762         return false;
1763 }
1764
1765 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1766                                    files_struct *fsp,
1767                                    uint32_t access_mask,
1768                                    uint32_t deny_mode)
1769 {
1770         NTSTATUS status = NT_STATUS_OK;
1771         struct byte_range_lock *br_lck = NULL;
1772         bool open_for_reading, open_for_writing, deny_read, deny_write;
1773         off_t off;
1774
1775         /* FIXME: hardcoded data fork, add resource fork */
1776         enum apple_fork fork_type = APPLE_FORK_DATA;
1777
1778         DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1779                   fsp_str_dbg(fsp),
1780                   access_mask & FILE_READ_DATA ? "READ" :"-",
1781                   access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1782                   deny_mode & DENY_READ ? "DENY_READ" : "-",
1783                   deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1784
1785         /*
1786          * Check read access and deny read mode
1787          */
1788         if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1789                 /* Check access */
1790                 open_for_reading = test_netatalk_lock(
1791                         fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1792
1793                 deny_read = test_netatalk_lock(
1794                         fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1795
1796                 DEBUG(10, ("read: %s, deny_write: %s\n",
1797                           open_for_reading == true ? "yes" : "no",
1798                           deny_read == true ? "yes" : "no"));
1799
1800                 if (((access_mask & FILE_READ_DATA) && deny_read)
1801                     || ((deny_mode & DENY_READ) && open_for_reading)) {
1802                         return NT_STATUS_SHARING_VIOLATION;
1803                 }
1804
1805                 /* Set locks */
1806                 if (access_mask & FILE_READ_DATA) {
1807                         off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1808                         br_lck = do_lock(
1809                                 handle->conn->sconn->msg_ctx, fsp,
1810                                 fsp->op->global->open_persistent_id, 1, off,
1811                                 READ_LOCK, POSIX_LOCK, false,
1812                                 &status, NULL);
1813
1814                         if (!NT_STATUS_IS_OK(status))  {
1815                                 return status;
1816                         }
1817                         TALLOC_FREE(br_lck);
1818                 }
1819
1820                 if (deny_mode & DENY_READ) {
1821                         off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1822                         br_lck = do_lock(
1823                                 handle->conn->sconn->msg_ctx, fsp,
1824                                 fsp->op->global->open_persistent_id, 1, off,
1825                                 READ_LOCK, POSIX_LOCK, false,
1826                                 &status, NULL);
1827
1828                         if (!NT_STATUS_IS_OK(status)) {
1829                                 return status;
1830                         }
1831                         TALLOC_FREE(br_lck);
1832                 }
1833         }
1834
1835         /*
1836          * Check write access and deny write mode
1837          */
1838         if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1839                 /* Check access */
1840                 open_for_writing = test_netatalk_lock(
1841                         fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1842
1843                 deny_write = test_netatalk_lock(
1844                         fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1845
1846                 DEBUG(10, ("write: %s, deny_write: %s\n",
1847                           open_for_writing == true ? "yes" : "no",
1848                           deny_write == true ? "yes" : "no"));
1849
1850                 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1851                     || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1852                         return NT_STATUS_SHARING_VIOLATION;
1853                 }
1854
1855                 /* Set locks */
1856                 if (access_mask & FILE_WRITE_DATA) {
1857                         off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1858                         br_lck = do_lock(
1859                                 handle->conn->sconn->msg_ctx, fsp,
1860                                 fsp->op->global->open_persistent_id, 1, off,
1861                                 READ_LOCK, POSIX_LOCK, false,
1862                                 &status, NULL);
1863
1864                         if (!NT_STATUS_IS_OK(status)) {
1865                                 return status;
1866                         }
1867                         TALLOC_FREE(br_lck);
1868
1869                 }
1870                 if (deny_mode & DENY_WRITE) {
1871                         off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1872                         br_lck = do_lock(
1873                                 handle->conn->sconn->msg_ctx, fsp,
1874                                 fsp->op->global->open_persistent_id, 1, off,
1875                                 READ_LOCK, POSIX_LOCK, false,
1876                                 &status, NULL);
1877
1878                         if (!NT_STATUS_IS_OK(status)) {
1879                                 return status;
1880                         }
1881                         TALLOC_FREE(br_lck);
1882                 }
1883         }
1884
1885         TALLOC_FREE(br_lck);
1886
1887         return status;
1888 }
1889
1890 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1891                            struct smb_request *req,
1892                            const struct smb2_create_blobs *in_context_blobs,
1893                            struct smb2_create_blobs *out_context_blobs)
1894 {
1895         struct fruit_config_data *config;
1896         NTSTATUS status;
1897         struct smb2_create_blob *aapl = NULL;
1898         uint32_t cmd;
1899         bool ok;
1900         uint8_t p[16];
1901         DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1902         uint64_t req_bitmap, client_caps;
1903         uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1904         smb_ucs2_t *model;
1905         size_t modellen;
1906
1907         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1908                                 return NT_STATUS_UNSUCCESSFUL);
1909
1910         if (!config->use_aapl
1911             || in_context_blobs == NULL
1912             || out_context_blobs == NULL) {
1913                 return NT_STATUS_OK;
1914         }
1915
1916         aapl = smb2_create_blob_find(in_context_blobs,
1917                                      SMB2_CREATE_TAG_AAPL);
1918         if (aapl == NULL) {
1919                 return NT_STATUS_OK;
1920         }
1921
1922         if (aapl->data.length != 24) {
1923                 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
1924                           (uintmax_t)aapl->data.length));
1925                 return NT_STATUS_INVALID_PARAMETER;
1926         }
1927
1928         cmd = IVAL(aapl->data.data, 0);
1929         if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1930                 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1931                 return NT_STATUS_INVALID_PARAMETER;
1932         }
1933
1934         req_bitmap = BVAL(aapl->data.data, 8);
1935         client_caps = BVAL(aapl->data.data, 16);
1936
1937         SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1938         SIVAL(p, 4, 0);
1939         SBVAL(p, 8, req_bitmap);
1940         ok = data_blob_append(req, &blob, p, 16);
1941         if (!ok) {
1942                 return NT_STATUS_UNSUCCESSFUL;
1943         }
1944
1945         if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1946                 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1947                     (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1948                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1949                         config->readdir_attr_enabled = true;
1950                 }
1951
1952                 if (config->use_copyfile) {
1953                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
1954                         config->copyfile_enabled = true;
1955                 }
1956
1957                 /*
1958                  * The client doesn't set the flag, so we can't check
1959                  * for it and just set it unconditionally
1960                  */
1961                 if (config->unix_info_enabled) {
1962                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1963                 }
1964
1965                 SBVAL(p, 0, server_caps);
1966                 ok = data_blob_append(req, &blob, p, 8);
1967                 if (!ok) {
1968                         return NT_STATUS_UNSUCCESSFUL;
1969                 }
1970         }
1971
1972         if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1973                 SBVAL(p, 0,
1974                       lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1975                       SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1976                 ok = data_blob_append(req, &blob, p, 8);
1977                 if (!ok) {
1978                         return NT_STATUS_UNSUCCESSFUL;
1979                 }
1980         }
1981
1982         if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1983                 ok = convert_string_talloc(req,
1984                                            CH_UNIX, CH_UTF16LE,
1985                                            "Samba", strlen("Samba"),
1986                                            &model, &modellen);
1987                 if (!ok) {
1988                         return NT_STATUS_UNSUCCESSFUL;
1989                 }
1990
1991                 SIVAL(p, 0, 0);
1992                 SIVAL(p + 4, 0, modellen);
1993                 ok = data_blob_append(req, &blob, p, 8);
1994                 if (!ok) {
1995                         talloc_free(model);
1996                         return NT_STATUS_UNSUCCESSFUL;
1997                 }
1998
1999                 ok = data_blob_append(req, &blob, model, modellen);
2000                 talloc_free(model);
2001                 if (!ok) {
2002                         return NT_STATUS_UNSUCCESSFUL;
2003                 }
2004         }
2005
2006         status = smb2_create_blob_add(out_context_blobs,
2007                                       out_context_blobs,
2008                                       SMB2_CREATE_TAG_AAPL,
2009                                       blob);
2010         if (NT_STATUS_IS_OK(status)) {
2011                 config->nego_aapl = true;
2012         }
2013
2014         return status;
2015 }
2016
2017 static bool readdir_attr_meta_finderi_stream(
2018         struct vfs_handle_struct *handle,
2019         const struct smb_filename *smb_fname,
2020         AfpInfo *ai)
2021 {
2022         struct smb_filename *stream_name = NULL;
2023         files_struct *fsp = NULL;
2024         ssize_t nread;
2025         NTSTATUS status;
2026         int ret;
2027         bool ok;
2028         uint8_t buf[AFP_INFO_SIZE];
2029
2030         stream_name = synthetic_smb_fname(talloc_tos(),
2031                                           smb_fname->base_name,
2032                                           AFPINFO_STREAM_NAME,
2033                                           NULL, smb_fname->flags);
2034         if (stream_name == NULL) {
2035                 return false;
2036         }
2037
2038         ret = SMB_VFS_STAT(handle->conn, stream_name);
2039         if (ret != 0) {
2040                 return false;
2041         }
2042
2043         status = SMB_VFS_CREATE_FILE(
2044                 handle->conn,                           /* conn */
2045                 NULL,                                   /* req */
2046                 0,                                      /* root_dir_fid */
2047                 stream_name,                            /* fname */
2048                 FILE_READ_DATA,                         /* access_mask */
2049                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
2050                         FILE_SHARE_DELETE),
2051                 FILE_OPEN,                              /* create_disposition*/
2052                 0,                                      /* create_options */
2053                 0,                                      /* file_attributes */
2054                 INTERNAL_OPEN_ONLY,                     /* oplock_request */
2055                 NULL,                                   /* lease */
2056                 0,                                      /* allocation_size */
2057                 0,                                      /* private_flags */
2058                 NULL,                                   /* sd */
2059                 NULL,                                   /* ea_list */
2060                 &fsp,                                   /* result */
2061                 NULL,                                   /* pinfo */
2062                 NULL, NULL);                            /* create context */
2063
2064         TALLOC_FREE(stream_name);
2065
2066         if (!NT_STATUS_IS_OK(status)) {
2067                 return false;
2068         }
2069
2070         nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2071         if (nread != AFP_INFO_SIZE) {
2072                 DBG_ERR("short read [%s] [%zd/%d]\n",
2073                         smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2074                 ok = false;
2075                 goto fail;
2076         }
2077
2078         memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2079                AFP_FinderSize);
2080
2081         ok = true;
2082
2083 fail:
2084         if (fsp != NULL) {
2085                 close_file(NULL, fsp, NORMAL_CLOSE);
2086         }
2087
2088         return ok;
2089 }
2090
2091 static bool readdir_attr_meta_finderi_netatalk(
2092         struct vfs_handle_struct *handle,
2093         const struct smb_filename *smb_fname,
2094         AfpInfo *ai)
2095 {
2096         struct adouble *ad = NULL;
2097         char *p = NULL;
2098
2099         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
2100         if (ad == NULL) {
2101                 return false;
2102         }
2103
2104         p = ad_get_entry(ad, ADEID_FINDERI);
2105         if (p == NULL) {
2106                 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2107                 TALLOC_FREE(ad);
2108                 return false;
2109         }
2110
2111         memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2112         TALLOC_FREE(ad);
2113         return true;
2114 }
2115
2116 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2117                                       const struct smb_filename *smb_fname,
2118                                       struct readdir_attr_data *attr_data)
2119 {
2120         struct fruit_config_data *config = NULL;
2121         uint32_t date_added;
2122         AfpInfo ai = {0};
2123         bool ok;
2124
2125         SMB_VFS_HANDLE_GET_DATA(handle, config,
2126                                 struct fruit_config_data,
2127                                 return false);
2128
2129         switch (config->meta) {
2130         case FRUIT_META_NETATALK:
2131                 ok = readdir_attr_meta_finderi_netatalk(
2132                         handle, smb_fname, &ai);
2133                 break;
2134
2135         case FRUIT_META_STREAM:
2136                 ok = readdir_attr_meta_finderi_stream(
2137                         handle, smb_fname, &ai);
2138                 break;
2139
2140         default:
2141                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2142                 return false;
2143         }
2144
2145         if (!ok) {
2146                 /* Don't bother with errors, it's likely ENOENT */
2147                 return true;
2148         }
2149
2150         if (S_ISREG(smb_fname->st.st_ex_mode)) {
2151                 /* finder_type */
2152                 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2153                        &ai.afpi_FinderInfo[0], 4);
2154
2155                 /* finder_creator */
2156                 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2157                        &ai.afpi_FinderInfo[4], 4);
2158         }
2159
2160         /* finder_flags */
2161         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2162                &ai.afpi_FinderInfo[8], 2);
2163
2164         /* finder_ext_flags */
2165         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2166                &ai.afpi_FinderInfo[24], 2);
2167
2168         /* creation date */
2169         date_added = convert_time_t_to_uint32_t(
2170                 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2171
2172         RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2173
2174         return true;
2175 }
2176
2177 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2178                                      const struct smb_filename *smb_fname,
2179                                      struct readdir_attr_data *attr_data)
2180 {
2181         NTSTATUS status = NT_STATUS_OK;
2182         struct adouble *ad = NULL;
2183         struct fruit_config_data *config = NULL;
2184         bool ok;
2185
2186         SMB_VFS_HANDLE_GET_DATA(handle, config,
2187                                 struct fruit_config_data,
2188                                 return NT_STATUS_UNSUCCESSFUL);
2189
2190
2191         /* Ensure we return a default value in the creation_date field */
2192         RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2193
2194         /*
2195          * Resource fork length
2196          */
2197
2198         if (config->readdir_attr_rsize) {
2199                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
2200                             ADOUBLE_RSRC);
2201                 if (ad) {
2202                         attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
2203                                 ad, ADEID_RFORK);
2204                         TALLOC_FREE(ad);
2205                 }
2206         }
2207
2208         /*
2209          * FinderInfo
2210          */
2211
2212         if (config->readdir_attr_finder_info) {
2213                 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2214                 if (!ok) {
2215                         status = NT_STATUS_INTERNAL_ERROR;
2216                 }
2217         }
2218
2219         return status;
2220 }
2221
2222 /* Search MS NFS style ACE with UNIX mode */
2223 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2224                              files_struct *fsp,
2225                              const struct security_descriptor *psd,
2226                              mode_t *pmode,
2227                              bool *pdo_chmod)
2228 {
2229         uint32_t i;
2230         struct fruit_config_data *config = NULL;
2231
2232         *pdo_chmod = false;
2233
2234         SMB_VFS_HANDLE_GET_DATA(handle, config,
2235                                 struct fruit_config_data,
2236                                 return NT_STATUS_UNSUCCESSFUL);
2237
2238         if (psd->dacl == NULL || !config->unix_info_enabled) {
2239                 return NT_STATUS_OK;
2240         }
2241
2242         for (i = 0; i < psd->dacl->num_aces; i++) {
2243                 if (dom_sid_compare_domain(
2244                             &global_sid_Unix_NFS_Mode,
2245                             &psd->dacl->aces[i].trustee) == 0) {
2246                         *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2247                         *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2248                         *pdo_chmod = true;
2249
2250                         DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2251                                    fsp_str_dbg(fsp), (unsigned)(*pmode)));
2252                         break;
2253                 }
2254         }
2255
2256         return NT_STATUS_OK;
2257 }
2258
2259 /****************************************************************************
2260  * VFS ops
2261  ****************************************************************************/
2262
2263 static int fruit_connect(vfs_handle_struct *handle,
2264                          const char *service,
2265                          const char *user)
2266 {
2267         int rc;
2268         char *list = NULL, *newlist = NULL;
2269         struct fruit_config_data *config;
2270
2271         DEBUG(10, ("fruit_connect\n"));
2272
2273         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2274         if (rc < 0) {
2275                 return rc;
2276         }
2277
2278         rc = init_fruit_config(handle);
2279         if (rc != 0) {
2280                 return rc;
2281         }
2282
2283         SMB_VFS_HANDLE_GET_DATA(handle, config,
2284                                 struct fruit_config_data, return -1);
2285
2286         if (config->veto_appledouble) {
2287                 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2288
2289                 if (list) {
2290                         if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2291                                 newlist = talloc_asprintf(
2292                                         list,
2293                                         "%s/" ADOUBLE_NAME_PREFIX "*/",
2294                                         list);
2295                                 lp_do_parameter(SNUM(handle->conn),
2296                                                 "veto files",
2297                                                 newlist);
2298                         }
2299                 } else {
2300                         lp_do_parameter(SNUM(handle->conn),
2301                                         "veto files",
2302                                         "/" ADOUBLE_NAME_PREFIX "*/");
2303                 }
2304
2305                 TALLOC_FREE(list);
2306         }
2307
2308         if (config->encoding == FRUIT_ENC_NATIVE) {
2309                 lp_do_parameter(
2310                         SNUM(handle->conn),
2311                         "catia:mappings",
2312                         "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2313                         "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2314                         "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2315                         "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2316                         "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2317                         "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2318                         "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2319                         "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2320                         "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2321                         "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2322                         "0x0d:0xf00d");
2323         }
2324
2325         return rc;
2326 }
2327
2328 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2329                                   struct smb_filename *smb_fname,
2330                                   files_struct *fsp,
2331                                   int flags,
2332                                   mode_t mode)
2333 {
2334         AfpInfo *ai = NULL;
2335         char afpinfo_buf[AFP_INFO_SIZE];
2336         ssize_t len, written;
2337         int hostfd = -1;
2338         int rc = -1;
2339
2340         hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2341         if (hostfd == -1) {
2342                 return -1;
2343         }
2344
2345         if (!(flags & (O_CREAT | O_TRUNC))) {
2346                 return hostfd;
2347         }
2348
2349         ai = afpinfo_new(talloc_tos());
2350         if (ai == NULL) {
2351                 rc = -1;
2352                 goto fail;
2353         }
2354
2355         len = afpinfo_pack(ai, afpinfo_buf);
2356         if (len != AFP_INFO_SIZE) {
2357                 rc = -1;
2358                 goto fail;
2359         }
2360
2361         /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2362         fsp->fh->fd = hostfd;
2363
2364         written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2365                                       AFP_INFO_SIZE, 0);
2366         fsp->fh->fd = -1;
2367         if (written != AFP_INFO_SIZE) {
2368                 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2369                 rc = -1;
2370                 goto fail;
2371         }
2372
2373         rc = 0;
2374
2375 fail:
2376         DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2377
2378         if (rc != 0) {
2379                 int saved_errno = errno;
2380                 if (hostfd >= 0) {
2381                         fsp->fh->fd = hostfd;
2382                         SMB_VFS_NEXT_CLOSE(handle, fsp);
2383                 }
2384                 hostfd = -1;
2385                 errno = saved_errno;
2386         }
2387         return hostfd;
2388 }
2389
2390 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2391                                     struct smb_filename *smb_fname,
2392                                     files_struct *fsp,
2393                                     int flags,
2394                                     mode_t mode)
2395 {
2396         int rc = 0;
2397         struct smb_filename *smb_fname_base = NULL;
2398         int baseflags;
2399         int hostfd = -1;
2400         struct adouble *ad = NULL;
2401
2402         /* Create an smb_filename with stream_name == NULL. */
2403         smb_fname_base = synthetic_smb_fname(talloc_tos(),
2404                                         smb_fname->base_name,
2405                                         NULL,
2406                                         NULL,
2407                                         smb_fname->flags);
2408
2409         if (smb_fname_base == NULL) {
2410                 errno = ENOMEM;
2411                 rc = -1;
2412                 goto exit;
2413         }
2414
2415         /*
2416          * We use baseflags to turn off nasty side-effects when opening the
2417          * underlying file.
2418          */
2419         baseflags = flags;
2420         baseflags &= ~O_TRUNC;
2421         baseflags &= ~O_EXCL;
2422         baseflags &= ~O_CREAT;
2423
2424         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2425                               baseflags, mode);
2426
2427         /*
2428          * It is legit to open a stream on a directory, but the base
2429          * fd has to be read-only.
2430          */
2431         if ((hostfd == -1) && (errno == EISDIR)) {
2432                 baseflags &= ~O_ACCMODE;
2433                 baseflags |= O_RDONLY;
2434                 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2435                                       baseflags, mode);
2436         }
2437
2438         TALLOC_FREE(smb_fname_base);
2439
2440         if (hostfd == -1) {
2441                 rc = -1;
2442                 goto exit;
2443         }
2444
2445         if (flags & (O_CREAT | O_TRUNC)) {
2446                 /*
2447                  * The attribute does not exist or needs to be truncated,
2448                  * create an AppleDouble EA
2449                  */
2450                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2451                              handle, ADOUBLE_META, fsp);
2452                 if (ad == NULL) {
2453                         rc = -1;
2454                         goto exit;
2455                 }
2456
2457                 rc = ad_write(ad, smb_fname->base_name);
2458                 if (rc != 0) {
2459                         rc = -1;
2460                         goto exit;
2461                 }
2462         } else {
2463                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2464                               handle, ADOUBLE_META, fsp);
2465                 if (ad == NULL) {
2466                         rc = -1;
2467                         goto exit;
2468                 }
2469                 if (ad_read(ad, smb_fname->base_name) == -1) {
2470                         rc = -1;
2471                         goto exit;
2472                 }
2473         }
2474
2475 exit:
2476         DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2477         if (rc != 0) {
2478                 int saved_errno = errno;
2479                 if (hostfd >= 0) {
2480                         /*
2481                          * BUGBUGBUG -- we would need to call
2482                          * fd_close_posix here, but we don't have a
2483                          * full fsp yet
2484                          */
2485                         fsp->fh->fd = hostfd;
2486                         SMB_VFS_CLOSE(fsp);
2487                 }
2488                 hostfd = -1;
2489                 errno = saved_errno;
2490         }
2491         return hostfd;
2492 }
2493
2494 static int fruit_open_meta(vfs_handle_struct *handle,
2495                            struct smb_filename *smb_fname,
2496                            files_struct *fsp, int flags, mode_t mode)
2497 {
2498         int rc;
2499         struct fruit_config_data *config = NULL;
2500
2501         DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2502
2503         SMB_VFS_HANDLE_GET_DATA(handle, config,
2504                                 struct fruit_config_data, return -1);
2505
2506         switch (config->meta) {
2507         case FRUIT_META_STREAM:
2508                 rc = fruit_open_meta_stream(handle, smb_fname,
2509                                             fsp, flags, mode);
2510                 break;
2511
2512         case FRUIT_META_NETATALK:
2513                 rc = fruit_open_meta_netatalk(handle, smb_fname,
2514                                               fsp, flags, mode);
2515                 break;
2516
2517         default:
2518                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2519                 return -1;
2520         }
2521
2522         DBG_DEBUG("path [%s] rc [%d]\n", smb_fname_str_dbg(smb_fname), rc);
2523         return rc;
2524 }
2525
2526 static int fruit_open_rsrc(vfs_handle_struct *handle,
2527                            struct smb_filename *smb_fname,
2528                            files_struct *fsp, int flags, mode_t mode)
2529 {
2530         int rc = 0;
2531         struct fruit_config_data *config = NULL;
2532         struct adouble *ad = NULL;
2533         struct smb_filename *smb_fname_base = NULL;
2534         char *adpath = NULL;
2535         int hostfd = -1;
2536
2537         DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2538
2539         SMB_VFS_HANDLE_GET_DATA(handle, config,
2540                                 struct fruit_config_data, return -1);
2541
2542         switch (config->rsrc) {
2543         case FRUIT_RSRC_STREAM:
2544                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2545         case FRUIT_RSRC_XATTR:
2546 #ifdef HAVE_ATTROPEN
2547                 hostfd = attropen(smb_fname->base_name,
2548                                   AFPRESOURCE_EA_NETATALK, flags, mode);
2549                 if (hostfd == -1) {
2550                         return -1;
2551                 }
2552                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2553                              handle, ADOUBLE_RSRC, fsp);
2554                 if (ad == NULL) {
2555                         rc = -1;
2556                         goto exit;
2557                 }
2558                 goto exit;
2559 #else
2560                 errno = ENOTSUP;
2561                 return -1;
2562 #endif
2563         default:
2564                 break;
2565         }
2566
2567         if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2568                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2569                 if (rc != 0) {
2570                         rc = -1;
2571                         goto exit;
2572                 }
2573         }
2574
2575         if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2576                 /* sorry, but directories don't habe a resource fork */
2577                 rc = -1;
2578                 goto exit;
2579         }
2580
2581         rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2582         if (rc != 0) {
2583                 goto exit;
2584         }
2585
2586         /* Create an smb_filename with stream_name == NULL. */
2587         smb_fname_base = synthetic_smb_fname(talloc_tos(),
2588                                         adpath,
2589                                         NULL,
2590                                         NULL,
2591                                         smb_fname->flags);
2592         if (smb_fname_base == NULL) {
2593                 errno = ENOMEM;
2594                 rc = -1;
2595                 goto exit;
2596         }
2597
2598         /* Sanitize flags */
2599         if (flags & O_WRONLY) {
2600                 /* We always need read access for the metadata header too */
2601                 flags &= ~O_WRONLY;
2602                 flags |= O_RDWR;
2603         }
2604
2605         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2606                               flags, mode);
2607         if (hostfd == -1) {
2608                 rc = -1;
2609                 goto exit;
2610         }
2611
2612         /* REVIEW: we need this in ad_write() */
2613         fsp->fh->fd = hostfd;
2614
2615         if (flags & (O_CREAT | O_TRUNC)) {
2616                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2617                              handle, ADOUBLE_RSRC, fsp);
2618                 if (ad == NULL) {
2619                         rc = -1;
2620                         goto exit;
2621                 }
2622                 rc = ad_write(ad, smb_fname->base_name);
2623                 if (rc != 0) {
2624                         rc = -1;
2625                         goto exit;
2626                 }
2627         } else {
2628                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2629                               handle, ADOUBLE_RSRC, fsp);
2630                 if (ad == NULL) {
2631                         rc = -1;
2632                         goto exit;
2633                 }
2634                 if (ad_read(ad, smb_fname->base_name) == -1) {
2635                         rc = -1;
2636                         goto exit;
2637                 }
2638         }
2639
2640 exit:
2641
2642         TALLOC_FREE(adpath);
2643         TALLOC_FREE(smb_fname_base);
2644
2645         DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2646         if (rc != 0) {
2647                 int saved_errno = errno;
2648                 if (hostfd >= 0) {
2649                         /*
2650                          * BUGBUGBUG -- we would need to call
2651                          * fd_close_posix here, but we don't have a
2652                          * full fsp yet
2653                          */
2654                         fsp->fh->fd = hostfd;
2655                         SMB_VFS_CLOSE(fsp);
2656                 }
2657                 hostfd = -1;
2658                 errno = saved_errno;
2659         }
2660         return hostfd;
2661 }
2662
2663 static int fruit_open(vfs_handle_struct *handle,
2664                       struct smb_filename *smb_fname,
2665                       files_struct *fsp, int flags, mode_t mode)
2666 {
2667         DEBUG(10, ("fruit_open called for %s\n",
2668                    smb_fname_str_dbg(smb_fname)));
2669
2670         if (!is_ntfs_stream_smb_fname(smb_fname)) {
2671                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2672         }
2673
2674         if (is_afpinfo_stream(smb_fname)) {
2675                 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2676         } else if (is_afpresource_stream(smb_fname)) {
2677                 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2678         }
2679
2680         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2681 }
2682
2683 static int fruit_rename(struct vfs_handle_struct *handle,
2684                         const struct smb_filename *smb_fname_src,
2685                         const struct smb_filename *smb_fname_dst)
2686 {
2687         int rc = -1;
2688         char *src_adouble_path = NULL;
2689         char *dst_adouble_path = NULL;
2690         struct fruit_config_data *config = NULL;
2691         struct smb_filename *src_adp_smb_fname = NULL;
2692         struct smb_filename *dst_adp_smb_fname = NULL;
2693
2694         SMB_VFS_HANDLE_GET_DATA(handle, config,
2695                                 struct fruit_config_data, return -1);
2696
2697         if (!VALID_STAT(smb_fname_src->st)) {
2698                 DBG_ERR("Need valid stat for [%s]\n",
2699                         smb_fname_str_dbg(smb_fname_src));
2700                 return -1;
2701         }
2702
2703         rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2704         if (rc != 0) {
2705                 return -1;
2706         }
2707
2708         if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
2709             (!S_ISREG(smb_fname_src->st.st_ex_mode)))
2710         {
2711                 return 0;
2712         }
2713
2714         rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2715                           &src_adouble_path);
2716         if (rc != 0) {
2717                 goto done;
2718         }
2719         src_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
2720                                                 src_adouble_path,
2721                                                 NULL, NULL,
2722                                                 smb_fname_src->flags);
2723         TALLOC_FREE(src_adouble_path);
2724         if (src_adp_smb_fname == NULL) {
2725                 rc = -1;
2726                 goto done;
2727         }
2728
2729         rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2730                           &dst_adouble_path);
2731         if (rc != 0) {
2732                 goto done;
2733         }
2734         dst_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
2735                                                 dst_adouble_path,
2736                                                 NULL, NULL,
2737                                                 smb_fname_dst->flags);
2738         TALLOC_FREE(dst_adouble_path);
2739         if (dst_adp_smb_fname == NULL) {
2740                 rc = -1;
2741                 goto done;
2742         }
2743
2744         DBG_DEBUG("%s -> %s\n",
2745                   smb_fname_str_dbg(src_adp_smb_fname),
2746                   smb_fname_str_dbg(dst_adp_smb_fname));
2747
2748         rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
2749         if (errno == ENOENT) {
2750                 rc = 0;
2751         }
2752
2753 done:
2754         TALLOC_FREE(src_adp_smb_fname);
2755         TALLOC_FREE(dst_adp_smb_fname);
2756         return rc;
2757 }
2758
2759 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
2760                                     const struct smb_filename *smb_fname)
2761 {
2762         return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2763 }
2764
2765 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
2766                                       const struct smb_filename *smb_fname)
2767 {
2768         return SMB_VFS_REMOVEXATTR(handle->conn,
2769                                    smb_fname->base_name,
2770                                    AFPINFO_EA_NETATALK);
2771 }
2772
2773 static int fruit_unlink_meta(vfs_handle_struct *handle,
2774                              const struct smb_filename *smb_fname)
2775 {
2776         struct fruit_config_data *config = NULL;
2777         int rc;
2778
2779         SMB_VFS_HANDLE_GET_DATA(handle, config,
2780                                 struct fruit_config_data, return -1);
2781
2782         switch (config->meta) {
2783         case FRUIT_META_STREAM:
2784                 rc = fruit_unlink_meta_stream(handle, smb_fname);
2785                 break;
2786
2787         case FRUIT_META_NETATALK:
2788                 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
2789                 break;
2790
2791         default:
2792                 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2793                 return -1;
2794         }
2795
2796         return rc;
2797 }
2798
2799 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2800                                     const struct smb_filename *smb_fname,
2801                                     bool force_unlink)
2802 {
2803         int ret;
2804
2805         if (!force_unlink) {
2806                 struct smb_filename *smb_fname_cp = NULL;
2807                 off_t size;
2808
2809                 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
2810                 if (smb_fname_cp == NULL) {
2811                         return -1;
2812                 }
2813
2814                 /*
2815                  * 0 byte resource fork streams are not listed by
2816                  * vfs_streaminfo, as a result stream cleanup/deletion of file
2817                  * deletion doesn't remove the resourcefork stream.
2818                  */
2819
2820                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
2821                 if (ret != 0) {
2822                         TALLOC_FREE(smb_fname_cp);
2823                         DBG_ERR("stat [%s] failed [%s]\n",
2824                                 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
2825                         return -1;
2826                 }
2827
2828                 size = smb_fname_cp->st.st_ex_size;
2829                 TALLOC_FREE(smb_fname_cp);
2830
2831                 if (size > 0) {
2832                         /* OS X ignores resource fork stream delete requests */
2833                         return 0;
2834                 }
2835         }
2836
2837         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2838         if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2839                 ret = 0;
2840         }
2841
2842         return ret;
2843 }
2844
2845 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2846                                      const struct smb_filename *smb_fname,
2847                                      bool force_unlink)
2848 {
2849         int rc;
2850         char *adp = NULL;
2851         struct adouble *ad = NULL;
2852         struct smb_filename *adp_smb_fname = NULL;
2853
2854         if (!force_unlink) {
2855                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
2856                             ADOUBLE_RSRC);
2857                 if (ad == NULL) {
2858                         errno = ENOENT;
2859                         return -1;
2860                 }
2861
2862
2863                 /*
2864                  * 0 byte resource fork streams are not listed by
2865                  * vfs_streaminfo, as a result stream cleanup/deletion of file
2866                  * deletion doesn't remove the resourcefork stream.
2867                  */
2868
2869                 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
2870                         /* OS X ignores resource fork stream delete requests */
2871                         TALLOC_FREE(ad);
2872                         return 0;
2873                 }
2874
2875                 TALLOC_FREE(ad);
2876         }
2877
2878         rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
2879         if (rc != 0) {
2880                 return -1;
2881         }
2882
2883         adp_smb_fname = synthetic_smb_fname(talloc_tos(), adp,
2884                                             NULL, NULL,
2885                                             smb_fname->flags);
2886         TALLOC_FREE(adp);
2887         if (adp_smb_fname == NULL) {
2888                 return -1;
2889         }
2890
2891         rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
2892         TALLOC_FREE(adp_smb_fname);
2893         if ((rc != 0) && (errno == ENOENT) && force_unlink) {
2894                 rc = 0;
2895         }
2896
2897         return rc;
2898 }
2899
2900 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
2901                                    const struct smb_filename *smb_fname,
2902                                    bool force_unlink)
2903 {
2904         /*
2905          * OS X ignores resource fork stream delete requests, so nothing to do
2906          * here. Removing the file will remove the xattr anyway, so we don't
2907          * have to take care of removing 0 byte resource forks that could be
2908          * left behind.
2909          */
2910         return 0;
2911 }
2912
2913 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
2914                              const struct smb_filename *smb_fname,
2915                              bool force_unlink)
2916 {
2917         struct fruit_config_data *config = NULL;
2918         int rc;
2919
2920         SMB_VFS_HANDLE_GET_DATA(handle, config,
2921                                 struct fruit_config_data, return -1);
2922
2923         switch (config->rsrc) {
2924         case FRUIT_RSRC_STREAM:
2925                 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
2926                 break;
2927
2928         case FRUIT_RSRC_ADFILE:
2929                 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
2930                 break;
2931
2932         case FRUIT_RSRC_XATTR:
2933                 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
2934                 break;
2935
2936         default:
2937                 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
2938                 return -1;
2939         }
2940
2941         return rc;
2942 }
2943
2944 static int fruit_unlink(vfs_handle_struct *handle,
2945                         const struct smb_filename *smb_fname)
2946 {
2947         int rc;
2948         struct fruit_config_data *config = NULL;
2949         struct smb_filename *rsrc_smb_fname = NULL;
2950
2951         SMB_VFS_HANDLE_GET_DATA(handle, config,
2952                                 struct fruit_config_data, return -1);
2953
2954         if (is_afpinfo_stream(smb_fname)) {
2955                 return fruit_unlink_meta(handle, smb_fname);
2956         } else if (is_afpresource_stream(smb_fname)) {
2957                 return fruit_unlink_rsrc(handle, smb_fname, false);
2958         } if (is_ntfs_stream_smb_fname(smb_fname)) {
2959                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2960         }
2961
2962         /*
2963          * A request to delete the base file. Because 0 byte resource
2964          * fork streams are not listed by fruit_streaminfo,
2965          * delete_all_streams() can't remove 0 byte resource fork
2966          * streams, so we have to cleanup this here.
2967          */
2968         rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
2969                                              smb_fname->base_name,
2970                                              AFPRESOURCE_STREAM_NAME,
2971                                              NULL,
2972                                              smb_fname->flags);
2973         if (rsrc_smb_fname == NULL) {
2974                 return -1;
2975         }
2976
2977         rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
2978         if ((rc != 0) && (errno != ENOENT)) {
2979                 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2980                         smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
2981                 TALLOC_FREE(rsrc_smb_fname);
2982                 return -1;
2983         }
2984         TALLOC_FREE(rsrc_smb_fname);
2985
2986         return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2987 }
2988
2989 static int fruit_chmod(vfs_handle_struct *handle,
2990                        const struct smb_filename *smb_fname,
2991                        mode_t mode)
2992 {
2993         int rc = -1;
2994         char *adp = NULL;
2995         struct fruit_config_data *config = NULL;
2996         const char *path = smb_fname->base_name;
2997         struct smb_filename *smb_fname_adp = NULL;
2998
2999         rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3000         if (rc != 0) {
3001                 return rc;
3002         }
3003
3004         SMB_VFS_HANDLE_GET_DATA(handle, config,
3005                                 struct fruit_config_data, return -1);
3006
3007         if (config->rsrc != FRUIT_RSRC_ADFILE) {
3008                 return 0;
3009         }
3010
3011         if (!VALID_STAT(smb_fname->st)) {
3012                 return 0;
3013         }
3014
3015         if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3016                 return 0;
3017         }
3018
3019         rc = adouble_path(talloc_tos(), path, &adp);
3020         if (rc != 0) {
3021                 return -1;
3022         }
3023
3024         DEBUG(10, ("fruit_chmod: %s\n", adp));
3025
3026         smb_fname_adp = synthetic_smb_fname(talloc_tos(),
3027                                         adp,
3028                                         NULL,
3029                                         NULL,
3030                                         smb_fname->flags);
3031         if (smb_fname_adp == NULL) {
3032                 TALLOC_FREE(adp);
3033                 errno = ENOMEM;
3034                 return -1;
3035         }
3036
3037         rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3038         if (errno == ENOENT) {
3039                 rc = 0;
3040         }
3041
3042         TALLOC_FREE(smb_fname_adp);
3043         TALLOC_FREE(adp);
3044         return rc;
3045 }
3046
3047 static int fruit_chown(vfs_handle_struct *handle,
3048                        const struct smb_filename *smb_fname,
3049                        uid_t uid,
3050                        gid_t gid)
3051 {
3052         int rc = -1;
3053         char *adp = NULL;
3054         struct fruit_config_data *config = NULL;
3055         struct smb_filename *adp_smb_fname = NULL;
3056
3057         rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3058         if (rc != 0) {
3059                 return rc;
3060         }
3061
3062         SMB_VFS_HANDLE_GET_DATA(handle, config,
3063                                 struct fruit_config_data, return -1);
3064
3065         if (config->rsrc != FRUIT_RSRC_ADFILE) {
3066                 return 0;
3067         }
3068
3069         if (!VALID_STAT(smb_fname->st)) {
3070                 return 0;
3071         }
3072
3073         if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3074                 return 0;
3075         }
3076
3077         rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
3078         if (rc != 0) {
3079                 goto done;
3080         }
3081
3082         DEBUG(10, ("fruit_chown: %s\n", adp));
3083
3084         adp_smb_fname = synthetic_smb_fname(talloc_tos(),
3085                                         adp,
3086                                         NULL,
3087                                         NULL,
3088                                         smb_fname->flags);
3089         if (adp_smb_fname == NULL) {
3090                 errno = ENOMEM;
3091                 rc = -1;
3092                 goto done;
3093         }
3094
3095         rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3096         if (errno == ENOENT) {
3097                 rc = 0;
3098         }
3099
3100  done:
3101         TALLOC_FREE(adp);
3102         TALLOC_FREE(adp_smb_fname);
3103         return rc;
3104 }
3105
3106 static int fruit_rmdir(struct vfs_handle_struct *handle,
3107                         const struct smb_filename *smb_fname)
3108 {
3109         DIR *dh = NULL;
3110         struct dirent *de;
3111         struct fruit_config_data *config;
3112
3113         SMB_VFS_HANDLE_GET_DATA(handle, config,
3114                                 struct fruit_config_data, return -1);
3115
3116         if (config->rsrc != FRUIT_RSRC_ADFILE) {
3117                 goto exit_rmdir;
3118         }
3119
3120         /*
3121          * Due to there is no way to change bDeleteVetoFiles variable
3122          * from this module, need to clean up ourselves
3123          */
3124
3125         dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3126         if (dh == NULL) {
3127                 goto exit_rmdir;
3128         }
3129
3130         while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3131                 int match;
3132                 struct adouble *ad = NULL;
3133                 char *p = NULL;
3134                 struct smb_filename *ad_smb_fname = NULL;
3135                 int ret;
3136
3137                 match = strncmp(de->d_name,
3138                                 ADOUBLE_NAME_PREFIX,
3139                                 strlen(ADOUBLE_NAME_PREFIX));
3140                 if (match != 0) {
3141                         continue;
3142                 }
3143
3144                 p = talloc_asprintf(talloc_tos(), "%s/%s",
3145                                     smb_fname->base_name, de->d_name);
3146                 if (p == NULL) {
3147                         DBG_ERR("talloc_asprintf failed\n");
3148                         return -1;
3149                 }
3150
3151                 /*
3152                  * Check whether it's a valid AppleDouble file, if
3153                  * yes, delete it, ignore it otherwise.
3154                  */
3155                 ad = ad_get(talloc_tos(), handle, p, ADOUBLE_RSRC);
3156                 if (ad == NULL) {
3157                         TALLOC_FREE(p);
3158                         continue;
3159                 }
3160                 TALLOC_FREE(ad);
3161
3162                 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3163                                                     NULL, NULL,
3164                                                     smb_fname->flags);
3165                 TALLOC_FREE(p);
3166                 if (ad_smb_fname == NULL) {
3167                         DBG_ERR("synthetic_smb_fname failed\n");
3168                         return -1;
3169                 }
3170
3171                 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3172                 TALLOC_FREE(ad_smb_fname);
3173                 if (ret != 0) {
3174                         DBG_ERR("Deleting [%s] failed\n",
3175                                 smb_fname_str_dbg(ad_smb_fname));
3176                 }
3177         }
3178
3179 exit_rmdir:
3180         if (dh) {
3181                 closedir(dh);
3182         }
3183         return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3184 }
3185
3186 static ssize_t fruit_pread(vfs_handle_struct *handle,
3187                            files_struct *fsp, void *data,
3188                            size_t n, off_t offset)
3189 {
3190         int rc = 0;
3191         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3192                 handle, fsp);
3193         struct fruit_config_data *config = NULL;
3194         AfpInfo *ai = NULL;
3195         ssize_t len = -1;
3196         char *name = NULL;
3197         char *tmp_base_name = NULL;
3198         NTSTATUS status;
3199         size_t to_return = n;
3200
3201         DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
3202
3203         if (!fsp->base_fsp) {
3204                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3205         }
3206
3207         SMB_VFS_HANDLE_GET_DATA(handle, config,
3208                                 struct fruit_config_data, return -1);
3209
3210         /* fsp_name is not converted with vfs_catia */
3211         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3212         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
3213                                         fsp->base_fsp->fsp_name->base_name,
3214                                         vfs_translate_to_unix,
3215                                         talloc_tos(), &name);
3216         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3217                 name = talloc_strdup(talloc_tos(), tmp_base_name);
3218                 if (name == NULL) {
3219                         rc = -1;
3220                         goto exit;
3221                 }
3222         } else if (!NT_STATUS_IS_OK(status)) {
3223                 errno = map_errno_from_nt_status(status);
3224                 rc = -1;
3225                 goto exit;
3226         }
3227         fsp->base_fsp->fsp_name->base_name = name;
3228
3229         if (is_afpinfo_stream(fsp->fsp_name)) {
3230                 /*
3231                  * OS X has a off-by-1 error in the offset calculation, so we're
3232                  * bug compatible here. It won't hurt, as any relevant real
3233                  * world read requests from the AFP_AfpInfo stream will be
3234                  * offset=0 n=60. offset is ignored anyway, see below.
3235                  */
3236                 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3237                         len = 0;
3238                         rc = 0;
3239                         goto exit;
3240                 }
3241
3242                 to_return = MIN(n, AFP_INFO_SIZE);
3243
3244                 /* Yes, macOS always reads from offset 0 */
3245                 offset = 0;
3246         }
3247
3248         if (ad == NULL) {
3249                 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, to_return, offset);
3250                 if (len == -1) {
3251                         rc = -1;
3252                         goto exit;
3253                 }
3254                 goto exit;
3255         }
3256
3257         if (!fruit_fsp_recheck(ad, fsp)) {
3258                 rc = -1;
3259                 goto exit;
3260         }
3261
3262         if (ad->ad_type == ADOUBLE_META) {
3263                 char afpinfo_buf[AFP_INFO_SIZE];
3264                 char *p = NULL;
3265
3266                 ai = afpinfo_new(talloc_tos());
3267                 if (ai == NULL) {
3268                         rc = -1;
3269                         goto exit;
3270                 }
3271
3272                 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
3273                 if (len == -1) {
3274                         rc = -1;
3275                         goto exit;
3276                 }
3277
3278                 p = ad_get_entry(ad, ADEID_FINDERI);
3279                 if (p == NULL) {
3280                         DBG_ERR("No ADEID_FINDERI for [%s]\n",
3281                                 fsp->fsp_name->base_name);
3282                         rc = -1;
3283                         goto exit;
3284                 }
3285
3286                 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3287
3288                 len = afpinfo_pack(ai, afpinfo_buf);
3289                 if (len != AFP_INFO_SIZE) {
3290                         rc = -1;
3291                         goto exit;
3292                 }
3293
3294                 /*
3295                  * OS X ignores offset when reading from AFP_AfpInfo stream!
3296                  */
3297                 memcpy(data, afpinfo_buf, to_return);
3298                 len = to_return;
3299         } else {
3300                 len = SMB_VFS_NEXT_PREAD(
3301                         handle, fsp, data, n,
3302                         offset + ad_getentryoff(ad, ADEID_RFORK));
3303                 if (len == -1) {
3304                         rc = -1;
3305                         goto exit;
3306                 }
3307         }
3308 exit:
3309         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3310         TALLOC_FREE(name);
3311         TALLOC_FREE(ai);
3312         if (rc != 0) {
3313                 len = -1;
3314         }
3315         DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
3316         return len;
3317 }
3318
3319 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
3320                             files_struct *fsp, const void *data,
3321                             size_t n, off_t offset)
3322 {
3323         int rc = 0;
3324         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3325                 handle, fsp);
3326         struct fruit_config_data *config = NULL;
3327         AfpInfo *ai = NULL;
3328         ssize_t len;
3329         char *name = NULL;
3330         char *tmp_base_name = NULL;
3331         NTSTATUS status;
3332
3333         DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
3334
3335         if (!fsp->base_fsp) {
3336                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3337         }
3338
3339         SMB_VFS_HANDLE_GET_DATA(handle, config,
3340                                 struct fruit_config_data, return -1);
3341
3342         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3343         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
3344                                         fsp->base_fsp->fsp_name->base_name,
3345                                         vfs_translate_to_unix,
3346                                         talloc_tos(), &name);
3347         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3348                 name = talloc_strdup(talloc_tos(), tmp_base_name);
3349                 if (name == NULL) {
3350                         rc = -1;
3351                         goto exit;
3352                 }
3353         } else if (!NT_STATUS_IS_OK(status)) {
3354                 errno = map_errno_from_nt_status(status);
3355                 rc = -1;
3356                 goto exit;
3357         }
3358         fsp->base_fsp->fsp_name->base_name = name;
3359
3360         if (is_afpinfo_stream(fsp->fsp_name)) {
3361                 /*
3362                  * Writing an all 0 blob to the metadata stream
3363                  * results in the stream being removed on a macOS
3364                  * server. This ensures we behave the same and it
3365                  * verified by the "delete AFP_AfpInfo by writing all
3366                  * 0" test.
3367                  */
3368                 if (n != AFP_INFO_SIZE || offset != 0) {
3369                         DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
3370                                   (intmax_t)offset, (intmax_t)n));
3371                         rc = -1;
3372                         goto exit;
3373                 }
3374                 ai = afpinfo_unpack(talloc_tos(), data);
3375                 if (ai == NULL) {
3376                         rc = -1;
3377                         goto exit;
3378                 }
3379
3380                 if (ai_empty_finderinfo(ai)) {
3381                         switch (config->meta) {
3382                         case FRUIT_META_STREAM:
3383                                 rc = SMB_VFS_UNLINK(handle->conn, fsp->fsp_name);
3384                                 break;
3385
3386                         case FRUIT_META_NETATALK:
3387                                 rc = SMB_VFS_REMOVEXATTR(
3388                                         handle->conn,
3389                                         fsp->fsp_name->base_name,
3390                                         AFPINFO_EA_NETATALK);
3391                                 break;
3392
3393                         default:
3394                                 DBG_ERR("Unexpected meta config [%d]\n",
3395                                         config->meta);
3396                                 rc = -1;
3397                                 goto exit;
3398                         }
3399
3400                         if (rc != 0 && errno != ENOENT && errno != ENOATTR) {
3401                                 DBG_WARNING("Can't delete metadata for %s: %s\n",
3402                                             fsp->fsp_name->base_name, strerror(errno));
3403                                 goto exit;
3404                         }
3405
3406                         rc = 0;
3407                         goto exit;
3408                 }
3409         }
3410
3411         if (ad == NULL) {
3412                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3413                 if (len != n) {
3414                         rc = -1;
3415                         goto exit;
3416                 }
3417                 goto exit;
3418         }
3419
3420         if (!fruit_fsp_recheck(ad, fsp)) {
3421                 rc = -1;
3422                 goto exit;
3423         }
3424
3425         if (ad->ad_type == ADOUBLE_META) {
3426                 char *p = NULL;
3427
3428                 p = ad_get_entry(ad, ADEID_FINDERI);
3429                 if (p == NULL) {
3430                         DBG_ERR("No ADEID_FINDERI for [%s]\n",
3431                                 fsp->fsp_name->base_name);
3432                         rc = -1;
3433                         goto exit;
3434                 }
3435
3436                 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3437                 rc = ad_write(ad, name);
3438         } else {
3439                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3440                                    offset + ad_getentryoff(ad, ADEID_RFORK));
3441                 if (len != n) {
3442                         rc = -1;
3443                         goto exit;
3444                 }
3445
3446                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
3447                         rc = ad_read(ad, name);
3448                         if (rc == -1) {
3449                                 goto exit;
3450                         }
3451                         rc = 0;
3452
3453                         if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3454                                 ad_setentrylen(ad, ADEID_RFORK, len + offset);
3455                                 rc = ad_write(ad, name);
3456                         }
3457                 }
3458         }
3459
3460 exit:
3461         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3462         TALLOC_FREE(name);
3463         TALLOC_FREE(ai);
3464         if (rc != 0) {
3465                 return -1;
3466         }
3467         return n;
3468 }
3469
3470 /**
3471  * Helper to stat/lstat the base file of an smb_fname.
3472  */
3473 static int fruit_stat_base(vfs_handle_struct *handle,
3474                            struct smb_filename *smb_fname,
3475                            bool follow_links)
3476 {
3477         char *tmp_stream_name;
3478         int rc;
3479
3480         tmp_stream_name = smb_fname->stream_name;
3481         smb_fname->stream_name = NULL;
3482         if (follow_links) {
3483                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3484         } else {
3485                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3486         }
3487         smb_fname->stream_name = tmp_stream_name;
3488         return rc;
3489 }
3490
3491 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3492                                   struct smb_filename *smb_fname,
3493                                   bool follow_links)
3494 {
3495         int ret;
3496
3497         if (follow_links) {
3498                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3499         } else {
3500                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3501         }
3502
3503         return ret;
3504 }
3505
3506 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3507                                     struct smb_filename *smb_fname,
3508                                     bool follow_links)
3509 {
3510         struct adouble *ad = NULL;
3511
3512         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3513         if (ad == NULL) {
3514                 DBG_INFO("fruit_stat_meta %s: %s\n",
3515                          smb_fname_str_dbg(smb_fname), strerror(errno));
3516                 errno = ENOENT;
3517                 return -1;
3518         }
3519         TALLOC_FREE(ad);
3520
3521         /* Populate the stat struct with info from the base file. */
3522         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3523                 return -1;
3524         }
3525         smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3526         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
3527                                               smb_fname->stream_name);
3528         return 0;
3529 }
3530
3531 static int fruit_stat_meta(vfs_handle_struct *handle,
3532                            struct smb_filename *smb_fname,
3533                            bool follow_links)
3534 {
3535         struct fruit_config_data *config = NULL;
3536         int ret;
3537
3538         SMB_VFS_HANDLE_GET_DATA(handle, config,
3539                                 struct fruit_config_data, return -1);
3540
3541         switch (config->meta) {
3542         case FRUIT_META_STREAM:
3543                 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3544                 break;
3545
3546         case FRUIT_META_NETATALK:
3547                 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3548                 break;
3549
3550         default:
3551                 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3552                 return -1;
3553         }
3554
3555         return ret;
3556 }
3557
3558 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3559                            struct smb_filename *smb_fname,
3560                            bool follow_links)
3561
3562 {
3563         struct adouble *ad = NULL;
3564
3565         DEBUG(10, ("fruit_stat_rsrc called for %s\n",
3566                    smb_fname_str_dbg(smb_fname)));
3567
3568         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
3569         if (ad == NULL) {
3570                 errno = ENOENT;
3571                 return -1;
3572         }
3573
3574         /* Populate the stat struct with info from the base file. */
3575         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3576                 TALLOC_FREE(ad);
3577                 return -1;
3578         }
3579
3580         smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3581         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
3582                                               smb_fname->stream_name);
3583         TALLOC_FREE(ad);
3584         return 0;
3585 }
3586
3587 static int fruit_stat(vfs_handle_struct *handle,
3588                       struct smb_filename *smb_fname)
3589 {
3590         int rc = -1;
3591
3592         DEBUG(10, ("fruit_stat called for %s\n",
3593                    smb_fname_str_dbg(smb_fname)));
3594
3595         if (!is_ntfs_stream_smb_fname(smb_fname)
3596             || is_ntfs_default_stream_smb_fname(smb_fname)) {
3597                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3598                 if (rc == 0) {
3599                         update_btime(handle, smb_fname);
3600                 }
3601                 return rc;
3602         }
3603
3604         /*
3605          * Note if lp_posix_paths() is true, we can never
3606          * get here as is_ntfs_stream_smb_fname() is
3607          * always false. So we never need worry about
3608          * not following links here.
3609          */
3610
3611         if (is_afpinfo_stream(smb_fname)) {
3612                 rc = fruit_stat_meta(handle, smb_fname, true);
3613         } else if (is_afpresource_stream(smb_fname)) {
3614                 rc = fruit_stat_rsrc(handle, smb_fname, true);
3615         } else {
3616                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3617         }
3618
3619         if (rc == 0) {
3620                 update_btime(handle, smb_fname);
3621                 smb_fname->st.st_ex_mode &= ~S_IFMT;
3622                 smb_fname->st.st_ex_mode |= S_IFREG;
3623                 smb_fname->st.st_ex_blocks =
3624                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3625         }
3626         return rc;
3627 }
3628
3629 static int fruit_lstat(vfs_handle_struct *handle,
3630                        struct smb_filename *smb_fname)
3631 {
3632         int rc = -1;
3633
3634         DEBUG(10, ("fruit_lstat called for %s\n",
3635                    smb_fname_str_dbg(smb_fname)));
3636
3637         if (!is_ntfs_stream_smb_fname(smb_fname)
3638             || is_ntfs_default_stream_smb_fname(smb_fname)) {
3639                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3640                 if (rc == 0) {
3641                         update_btime(handle, smb_fname);
3642                 }
3643                 return rc;
3644         }
3645
3646         if (is_afpinfo_stream(smb_fname)) {
3647                 rc = fruit_stat_meta(handle, smb_fname, false);
3648         } else if (is_afpresource_stream(smb_fname)) {
3649                 rc = fruit_stat_rsrc(handle, smb_fname, false);
3650         } else {
3651                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3652         }
3653
3654         if (rc == 0) {
3655                 update_btime(handle, smb_fname);
3656                 smb_fname->st.st_ex_mode &= ~S_IFMT;
3657                 smb_fname->st.st_ex_mode |= S_IFREG;
3658                 smb_fname->st.st_ex_blocks =
3659                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3660         }
3661         return rc;
3662 }
3663
3664 static int fruit_fstat_meta(vfs_handle_struct *handle,
3665                             files_struct *fsp,
3666                             SMB_STRUCT_STAT *sbuf)
3667 {
3668         DEBUG(10, ("fruit_fstat_meta called for %s\n",
3669                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3670
3671         /* Populate the stat struct with info from the base file. */
3672         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3673                 return -1;
3674         }
3675         *sbuf = fsp->base_fsp->fsp_name->st;
3676         sbuf->st_ex_size = AFP_INFO_SIZE;
3677         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3678
3679         return 0;
3680 }
3681
3682 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3683                             SMB_STRUCT_STAT *sbuf)
3684 {
3685         struct fruit_config_data *config;
3686         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3687                 handle, fsp);
3688
3689         DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
3690                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3691
3692         SMB_VFS_HANDLE_GET_DATA(handle, config,
3693                                 struct fruit_config_data, return -1);
3694
3695         if (config->rsrc == FRUIT_RSRC_STREAM) {
3696                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3697         }
3698
3699         /* Populate the stat struct with info from the base file. */
3700         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3701                 return -1;
3702         }
3703         *sbuf = fsp->base_fsp->fsp_name->st;
3704         sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3705         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3706
3707         DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3708                    smb_fname_str_dbg(fsp->fsp_name),
3709                    (ssize_t)sbuf->st_ex_size));
3710
3711         return 0;
3712 }
3713
3714 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3715                        SMB_STRUCT_STAT *sbuf)
3716 {
3717         int rc;
3718         char *name = NULL;
3719         char *tmp_base_name = NULL;
3720         NTSTATUS status;
3721         struct adouble *ad = (struct adouble *)
3722                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
3723
3724         DEBUG(10, ("fruit_fstat called for %s\n",
3725                    smb_fname_str_dbg(fsp->fsp_name)));
3726
3727         if (fsp->base_fsp) {
3728                 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3729                 /* fsp_name is not converted with vfs_catia */
3730                 status = SMB_VFS_TRANSLATE_NAME(
3731                         handle->conn,
3732                         fsp->base_fsp->fsp_name->base_name,
3733                         vfs_translate_to_unix,
3734                         talloc_tos(), &name);
3735
3736                 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3737                         name = talloc_strdup(talloc_tos(), tmp_base_name);
3738                         if (name == NULL) {
3739                                 rc = -1;
3740                                 goto exit;
3741                         }
3742                 } else if (!NT_STATUS_IS_OK(status)) {
3743                         errno = map_errno_from_nt_status(status);
3744                         rc = -1;
3745                         goto exit;
3746                 }
3747                 fsp->base_fsp->fsp_name->base_name = name;
3748         }
3749
3750         if (ad == NULL || fsp->base_fsp == NULL) {
3751                 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3752                 goto exit;
3753         }
3754
3755         if (!fruit_fsp_recheck(ad, fsp)) {
3756                 rc = -1;
3757                 goto exit;
3758         }
3759
3760         switch (ad->ad_type) {
3761         case ADOUBLE_META:
3762                 rc = fruit_fstat_meta(handle, fsp, sbuf);
3763                 break;
3764         case ADOUBLE_RSRC:
3765                 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
3766                 break;
3767         default:
3768                 DEBUG(10, ("fruit_fstat %s: bad type\n",
3769                            smb_fname_str_dbg(fsp->fsp_name)));
3770                 rc = -1;
3771                 goto exit;
3772         }
3773
3774         if (rc == 0) {
3775                 sbuf->st_ex_mode &= ~S_IFMT;
3776                 sbuf->st_ex_mode |= S_IFREG;
3777                 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3778         }
3779
3780 exit:
3781         DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3782                    smb_fname_str_dbg(fsp->fsp_name),
3783                    (ssize_t)sbuf->st_ex_size));
3784         if (tmp_base_name) {
3785                 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3786         }
3787         TALLOC_FREE(name);
3788         return rc;
3789 }
3790
3791 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3792                                  struct files_struct *fsp,
3793                                  const struct smb_filename *smb_fname,
3794                                  TALLOC_CTX *mem_ctx,
3795                                  unsigned int *pnum_streams,
3796                                  struct stream_struct **pstreams)
3797 {
3798         struct fruit_config_data *config = NULL;
3799         struct adouble *ad = NULL;
3800         NTSTATUS status;
3801
3802         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3803                                 return NT_STATUS_UNSUCCESSFUL);
3804         DEBUG(10, ("fruit_streaminfo called for %s\n", smb_fname->base_name));
3805
3806         if (config->meta == FRUIT_META_NETATALK) {
3807                 bool ok;
3808
3809                 ad = ad_get(talloc_tos(), handle,
3810                             smb_fname->base_name, ADOUBLE_META);
3811                 if ((ad != NULL) && !ad_empty_finderinfo(ad)) {
3812                         ok = add_fruit_stream(
3813                                 mem_ctx, pnum_streams, pstreams,
3814                                 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3815                                 smb_roundup(handle->conn, AFP_INFO_SIZE));
3816                         if (!ok) {
3817                                 TALLOC_FREE(ad);
3818                                 return NT_STATUS_NO_MEMORY;
3819                         }
3820                 }
3821                 TALLOC_FREE(ad);
3822         }
3823
3824         if (config->rsrc != FRUIT_RSRC_STREAM) {
3825                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3826                             ADOUBLE_RSRC);
3827                 if (ad && (ad_getentrylen(ad, ADEID_RFORK) > 0)) {
3828                         if (!add_fruit_stream(
3829                                     mem_ctx, pnum_streams, pstreams,
3830                                     AFPRESOURCE_STREAM_NAME,
3831                                     ad_getentrylen(ad, ADEID_RFORK),
3832                                     smb_roundup(handle->conn,
3833                                                 ad_getentrylen(
3834                                                         ad, ADEID_RFORK)))) {
3835                                 TALLOC_FREE(ad);
3836                                 return NT_STATUS_NO_MEMORY;
3837                         }
3838                 }
3839                 TALLOC_FREE(ad);
3840         }
3841
3842         status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3843                                          pnum_streams, pstreams);
3844         if (!NT_STATUS_IS_OK(status)) {
3845                 return status;
3846         }
3847
3848         if (config->meta == FRUIT_META_NETATALK) {
3849                 /* Remove the Netatalk xattr from the list */
3850                 if (!del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3851                                       ":" NETATALK_META_XATTR ":$DATA")) {
3852                                 TALLOC_FREE(ad);
3853                                 return NT_STATUS_NO_MEMORY;
3854                 }
3855         }
3856
3857         return NT_STATUS_OK;
3858 }
3859
3860 static int fruit_ntimes(vfs_handle_struct *handle,
3861                         const struct smb_filename *smb_fname,
3862                         struct smb_file_time *ft)
3863 {
3864         int rc = 0;
3865         struct adouble *ad = NULL;
3866
3867         if (null_timespec(ft->create_time)) {
3868                 goto exit;
3869         }
3870
3871         DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3872                  time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3873
3874         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3875         if (ad == NULL) {
3876                 goto exit;
3877         }
3878
3879         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3880                    convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3881
3882         rc = ad_write(ad, smb_fname->base_name);
3883
3884 exit:
3885
3886         TALLOC_FREE(ad);
3887         if (rc != 0) {
3888                 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3889                 return -1;
3890         }
3891         return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3892 }
3893
3894 static int fruit_fallocate(struct vfs_handle_struct *handle,
3895                            struct files_struct *fsp,
3896                            uint32_t mode,
3897                            off_t offset,
3898                            off_t len)
3899 {
3900         struct adouble *ad =
3901                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3902
3903         if (ad == NULL) {
3904                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3905         }
3906
3907         if (!fruit_fsp_recheck(ad, fsp)) {
3908                 return -1;
3909         }
3910
3911         /* Let the pwrite code path handle it. */
3912         errno = ENOSYS;
3913         return -1;
3914 }
3915
3916 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3917                                 struct files_struct *fsp,
3918                                 off_t offset,
3919                                 struct adouble *ad)
3920 {
3921         int rc;
3922         struct fruit_config_data *config;
3923
3924         SMB_VFS_HANDLE_GET_DATA(handle, config,
3925                                 struct fruit_config_data, return -1);
3926
3927         if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3928                 return SMB_VFS_FREMOVEXATTR(fsp,
3929                                             AFPRESOURCE_EA_NETATALK);
3930         }
3931
3932         rc = SMB_VFS_NEXT_FTRUNCATE(
3933                 handle, fsp,
3934                 offset + ad_getentryoff(ad, ADEID_RFORK));
3935         if (rc != 0) {
3936                 return -1;
3937         }
3938
3939         if (config->rsrc == FRUIT_RSRC_ADFILE) {
3940                 ad_setentrylen(ad, ADEID_RFORK, offset);
3941                 rc = ad_write(ad, NULL);
3942                 if (rc != 0) {
3943                         return -1;
3944                 }
3945                 DEBUG(10, ("fruit_ftruncate_rsrc file %s offset %jd\n",
3946                            fsp_str_dbg(fsp), (intmax_t)offset));
3947         }
3948
3949         return 0;
3950 }
3951
3952 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3953                            struct files_struct *fsp,
3954                            off_t offset)
3955 {
3956         int rc = 0;
3957         struct adouble *ad =
3958                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3959
3960         DBG_DEBUG("fruit_ftruncate called for file %s offset %.0f\n",
3961                    fsp_str_dbg(fsp), (double)offset);
3962
3963         if (is_afpinfo_stream(fsp->fsp_name)) {
3964                 if (offset > 60) {
3965                         DBG_WARNING("ftruncate %s to %jd",
3966                                     fsp_str_dbg(fsp), (intmax_t)offset);
3967                         /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED  */
3968                         errno = EOVERFLOW;
3969                         return -1;
3970                 }
3971
3972                 DBG_WARNING("ignoring ftruncate %s to %jd",
3973                             fsp_str_dbg(fsp), (intmax_t)offset);
3974                 /* OS X returns success but does nothing  */
3975                 return 0;
3976         }
3977
3978         if (ad == NULL) {
3979                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3980         }
3981
3982         if (!fruit_fsp_recheck(ad, fsp)) {
3983                 return -1;
3984         }
3985
3986         switch (ad->ad_type) {
3987         case ADOUBLE_RSRC:
3988                 rc = fruit_ftruncate_rsrc(handle, fsp, offset, ad);
3989                 break;
3990
3991         default:
3992                 DBG_ERR("unexpected ad_type [%d]\n", ad->ad_type);
3993                 return -1;
3994         }
3995
3996         return rc;
3997 }
3998
3999 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4000                                   struct smb_request *req,
4001                                   uint16_t root_dir_fid,
4002                                   struct smb_filename *smb_fname,
4003                                   uint32_t access_mask,
4004                                   uint32_t share_access,
4005                                   uint32_t create_disposition,
4006                                   uint32_t create_options,
4007                                   uint32_t file_attributes,
4008                                   uint32_t oplock_request,
4009                                   struct smb2_lease *lease,
4010                                   uint64_t allocation_size,
4011                                   uint32_t private_flags,
4012                                   struct security_descriptor *sd,
4013                                   struct ea_list *ea_list,
4014                                   files_struct **result,
4015                                   int *pinfo,
4016                                   const struct smb2_create_blobs *in_context_blobs,
4017                                   struct smb2_create_blobs *out_context_blobs)
4018 {
4019         NTSTATUS status;
4020         struct fruit_config_data *config = NULL;
4021         files_struct *fsp = NULL;
4022
4023         status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4024         if (!NT_STATUS_IS_OK(status)) {
4025                 goto fail;
4026         }
4027
4028         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4029                                 return NT_STATUS_UNSUCCESSFUL);
4030
4031         status = SMB_VFS_NEXT_CREATE_FILE(
4032                 handle, req, root_dir_fid, smb_fname,
4033                 access_mask, share_access,
4034                 create_disposition, create_options,
4035                 file_attributes, oplock_request,
4036                 lease,
4037                 allocation_size, private_flags,
4038                 sd, ea_list, result,
4039                 pinfo, in_context_blobs, out_context_blobs);
4040         if (!NT_STATUS_IS_OK(status)) {
4041                 return status;
4042         }
4043
4044         fsp = *result;
4045
4046         if (config->nego_aapl) {
4047                 if (config->copyfile_enabled) {
4048                         /*
4049                          * Set a flag in the fsp. Gets used in
4050                          * copychunk to check whether the special
4051                          * Apple copyfile semantics for copychunk
4052                          * should be allowed in a copychunk request
4053                          * with a count of 0.
4054                          */
4055                         fsp->aapl_copyfile_supported = true;
4056                 }
4057
4058                 if (config->posix_rename && fsp->is_directory) {
4059                         /*
4060                          * Enable POSIX directory rename behaviour
4061                          */
4062                         fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4063                 }
4064         }
4065
4066         /*
4067          * If this is a plain open for existing files, opening an 0
4068          * byte size resource fork MUST fail with
4069          * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4070          *
4071          * Cf the vfs_fruit torture tests in test_rfork_create().
4072          */
4073         if (is_afpresource_stream(fsp->fsp_name) &&
4074             create_disposition == FILE_OPEN)
4075         {
4076                 if (fsp->fsp_name->st.st_ex_size == 0) {
4077                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4078                         goto fail;
4079                 }
4080         }
4081
4082         if (is_ntfs_stream_smb_fname(smb_fname)
4083             || fsp->is_directory) {
4084                 return status;
4085         }
4086
4087         if (config->locking == FRUIT_LOCKING_NETATALK) {
4088                 status = fruit_check_access(
4089                         handle, *result,
4090                         access_mask,
4091                         map_share_mode_to_deny_mode(share_access, 0));
4092                 if (!NT_STATUS_IS_OK(status)) {
4093                         goto fail;
4094                 }
4095         }
4096
4097         return status;
4098
4099 fail:
4100         DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4101
4102         if (fsp) {
4103                 close_file(req, fsp, ERROR_CLOSE);
4104                 *result = fsp = NULL;
4105         }
4106
4107         return status;
4108 }
4109
4110 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4111                                    const struct smb_filename *fname,
4112                                    TALLOC_CTX *mem_ctx,
4113                                    struct readdir_attr_data **pattr_data)
4114 {
4115         struct fruit_config_data *config = NULL;
4116         struct readdir_attr_data *attr_data;
4117         NTSTATUS status;
4118
4119         SMB_VFS_HANDLE_GET_DATA(handle, config,
4120                                 struct fruit_config_data,
4121                                 return NT_STATUS_UNSUCCESSFUL);
4122
4123         if (!config->nego_aapl) {
4124                 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4125         }
4126
4127         DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4128
4129         *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4130         if (*pattr_data == NULL) {
4131                 return NT_STATUS_UNSUCCESSFUL;
4132         }
4133         attr_data = *pattr_data;
4134         attr_data->type = RDATTR_AAPL;
4135
4136         /*
4137          * Mac metadata: compressed FinderInfo, resource fork length
4138          * and creation date
4139          */
4140         status = readdir_attr_macmeta(handle, fname, attr_data);
4141         if (!NT_STATUS_IS_OK(status)) {
4142                 /*
4143                  * Error handling is tricky: if we return failure from
4144                  * this function, the corresponding directory entry
4145                  * will to be passed to the client, so we really just
4146                  * want to error out on fatal errors.
4147                  */
4148                 if  (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4149                         goto fail;
4150                 }
4151         }
4152
4153         /*
4154          * UNIX mode
4155          */
4156         if (config->unix_info_enabled) {
4157                 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4158         }
4159
4160         /*
4161          * max_access
4162          */
4163         if (!config->readdir_attr_max_access) {
4164                 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4165         } else {
4166                 status = smbd_calculate_access_mask(
4167                         handle->conn,
4168                         fname,
4169                         false,
4170                         SEC_FLAG_MAXIMUM_ALLOWED,
4171                         &attr_data->attr_data.aapl.max_access);
4172                 if (!NT_STATUS_IS_OK(status)) {
4173                         goto fail;
4174                 }
4175         }
4176
4177         return NT_STATUS_OK;
4178
4179 fail:
4180         DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4181                   fname->base_name, nt_errstr(status)));
4182         TALLOC_FREE(*pattr_data);
4183         return status;
4184 }
4185
4186 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4187                                   files_struct *fsp,
4188                                   uint32_t security_info,
4189                                   TALLOC_CTX *mem_ctx,
4190                                   struct security_descriptor **ppdesc)
4191 {
4192         NTSTATUS status;
4193         struct security_ace ace;
4194         struct dom_sid sid;
4195         struct fruit_config_data *config;
4196
4197         SMB_VFS_HANDLE_GET_DATA(handle, config,
4198                                 struct fruit_config_data,
4199                                 return NT_STATUS_UNSUCCESSFUL);
4200
4201         status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4202                                           mem_ctx, ppdesc);
4203         if (!NT_STATUS_IS_OK(status)) {
4204                 return status;
4205         }
4206
4207         /*
4208          * Add MS NFS style ACEs with uid, gid and mode
4209          */
4210         if (!config->unix_info_enabled) {
4211                 return NT_STATUS_OK;
4212         }
4213
4214         /* MS NFS style mode */
4215         sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4216         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4217         status = security_descriptor_dacl_add(*ppdesc, &ace);
4218         if (!NT_STATUS_IS_OK(status)) {
4219                 DEBUG(1,("failed to add MS NFS style ACE\n"));
4220                 return status;
4221         }
4222
4223         /* MS NFS style uid */
4224         sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4225         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4226         status = security_descriptor_dacl_add(*ppdesc, &ace);
4227         if (!NT_STATUS_IS_OK(status)) {
4228                 DEBUG(1,("failed to add MS NFS style ACE\n"));
4229                 return status;
4230         }
4231
4232         /* MS NFS style gid */
4233         sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4234         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4235         status = security_descriptor_dacl_add(*ppdesc, &ace);
4236         if (!NT_STATUS_IS_OK(status)) {
4237                 DEBUG(1,("failed to add MS NFS style ACE\n"));
4238                 return status;
4239         }
4240
4241         return NT_STATUS_OK;
4242 }
4243
4244 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4245                                   files_struct *fsp,
4246                                   uint32_t security_info_sent,
4247                                   const struct security_descriptor *psd)
4248 {
4249         NTSTATUS status;
4250         bool do_chmod;
4251         mode_t ms_nfs_mode = 0;
4252         int result;
4253
4254         DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4255
4256         status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4257         if (!NT_STATUS_IS_OK(status)) {
4258                 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4259                 return status;
4260         }
4261
4262         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4263         if (!NT_STATUS_IS_OK(status)) {
4264                 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4265                 return status;
4266         }
4267
4268         if (do_chmod) {
4269                 if (fsp->fh->fd != -1) {
4270                         result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4271                 } else {
4272                         result = SMB_VFS_CHMOD(fsp->conn,
4273                                                fsp->fsp_name,
4274                                                ms_nfs_mode);
4275                 }
4276
4277                 if (result != 0) {
4278                         DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
4279                                   result, (unsigned)ms_nfs_mode,
4280                                   strerror(errno)));
4281                         status = map_nt_error_from_unix(errno);
4282                         return status;
4283                 }
4284         }
4285
4286         return NT_STATUS_OK;
4287 }
4288
4289 struct fruit_copy_chunk_state {
4290         struct vfs_handle_struct *handle;
4291         off_t copied;
4292         struct files_struct *src_fsp;
4293         struct files_struct *dst_fsp;
4294         bool is_copyfile;
4295 };
4296
4297 static void fruit_copy_chunk_done(struct tevent_req *subreq);
4298 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
4299                                                 TALLOC_CTX *mem_ctx,
4300                                                 struct tevent_context *ev,
4301                                                 struct files_struct *src_fsp,
4302                                                 off_t src_off,
4303                                                 struct files_struct *dest_fsp,
4304                                                 off_t dest_off,
4305                                                 off_t num)
4306 {
4307         struct tevent_req *req, *subreq;
4308         struct fruit_copy_chunk_state *fruit_copy_chunk_state;
4309         NTSTATUS status;
4310         struct fruit_config_data *config;
4311         off_t to_copy = num;
4312
4313         DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4314                   (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4315
4316         SMB_VFS_HANDLE_GET_DATA(handle, config,
4317                                 struct fruit_config_data,
4318                                 return NULL);
4319
4320         req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
4321                                 struct fruit_copy_chunk_state);
4322         if (req == NULL) {
4323                 return NULL;
4324         }
4325         fruit_copy_chunk_state->handle = handle;
4326         fruit_copy_chunk_state->src_fsp = src_fsp;
4327         fruit_copy_chunk_state->dst_fsp = dest_fsp;
4328
4329         /*
4330          * Check if this a OS X copyfile style copychunk request with
4331          * a requested chunk count of 0 that was translated to a
4332          * copy_chunk_send VFS call overloading the parameters src_off
4333          * = dest_off = num = 0.
4334          */
4335         if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
4336             src_fsp->aapl_copyfile_supported &&
4337             dest_fsp->aapl_copyfile_supported)
4338         {
4339                 status = vfs_stat_fsp(src_fsp);
4340                 if (tevent_req_nterror(req, status)) {
4341                         return tevent_req_post(req, ev);
4342                 }
4343
4344                 to_copy = src_fsp->fsp_name->st.st_ex_size;
4345                 fruit_copy_chunk_state->is_copyfile = true;
4346         }
4347
4348         subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
4349                                               mem_ctx,
4350                                               ev,
4351                                               src_fsp,
4352                                               src_off,
4353                                               dest_fsp,
4354                                               dest_off,
4355                                               to_copy);
4356         if (tevent_req_nomem(subreq, req)) {
4357                 return tevent_req_post(req, ev);
4358         }
4359
4360         tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
4361         return req;
4362 }
4363
4364 static void fruit_copy_chunk_done(struct tevent_req *subreq)
4365 {
4366         struct tevent_req *req = tevent_req_callback_data(
4367                 subreq, struct tevent_req);
4368         struct fruit_copy_chunk_state *state = tevent_req_data(
4369                 req, struct fruit_copy_chunk_state);
4370         NTSTATUS status;
4371         unsigned int num_streams = 0;
4372         struct stream_struct *streams = NULL;
4373         unsigned int i;
4374         struct smb_filename *src_fname_tmp = NULL;
4375         struct smb_filename *dst_fname_tmp = NULL;
4376
4377         status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
4378                                               subreq,
4379                                               &state->copied);
4380         TALLOC_FREE(subreq);
4381         if (tevent_req_nterror(req, status)) {
4382                 return;
4383         }
4384
4385         if (!state->is_copyfile) {
4386                 tevent_req_done(req);
4387                 return;
4388         }
4389
4390         /*
4391          * Now copy all remaining streams. We know the share supports
4392          * streams, because we're in vfs_fruit. We don't do this async
4393          * because streams are few and small.
4394          */
4395         status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4396                                 state->src_fsp->fsp_name,
4397                                 req, &num_streams, &streams);
4398         if (tevent_req_nterror(req, status)) {
4399                 return;
4400         }
4401
4402         if (num_streams == 1) {
4403                 /* There is always one stream, ::$DATA. */
4404                 tevent_req_done(req);
4405                 return;
4406         }
4407
4408         for (i = 0; i < num_streams; i++) {
4409                 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4410                           __func__, streams[i].name, (size_t)streams[i].size));
4411
4412                 src_fname_tmp = synthetic_smb_fname(
4413                         req,
4414                         state->src_fsp->fsp_name->base_name,
4415                         streams[i].name,
4416                         NULL,
4417                         state->src_fsp->fsp_name->flags);
4418                 if (tevent_req_nomem(src_fname_tmp, req)) {
4419                         return;
4420                 }
4421
4422                 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4423                         TALLOC_FREE(src_fname_tmp);
4424                         continue;
4425                 }
4426
4427                 dst_fname_tmp = synthetic_smb_fname(
4428                         req,
4429                         state->dst_fsp->fsp_name->base_name,
4430                         streams[i].name,
4431                         NULL,
4432                         state->dst_fsp->fsp_name->flags);
4433                 if (tevent_req_nomem(dst_fname_tmp, req)) {
4434                         TALLOC_FREE(src_fname_tmp);
4435                         return;
4436                 }
4437
4438                 status = copy_file(req,
4439                                    state->handle->conn,
4440                                    src_fname_tmp,
4441                                    dst_fname_tmp,
4442                                    OPENX_FILE_CREATE_IF_NOT_EXIST,
4443                                    0, false);
4444                 if (!NT_STATUS_IS_OK(status)) {
4445                         DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4446                                   smb_fname_str_dbg(src_fname_tmp),
4447                                   smb_fname_str_dbg(dst_fname_tmp),
4448                                   nt_errstr(status)));
4449                         TALLOC_FREE(src_fname_tmp);
4450                         TALLOC_FREE(dst_fname_tmp);
4451                         tevent_req_nterror(req, status);
4452                         return;
4453                 }
4454
4455                 TALLOC_FREE(src_fname_tmp);
4456                 TALLOC_FREE(dst_fname_tmp);
4457         }
4458
4459         TALLOC_FREE(streams);
4460         TALLOC_FREE(src_fname_tmp);
4461         TALLOC_FREE(dst_fname_tmp);
4462         tevent_req_done(req);
4463 }
4464
4465 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
4466                                       struct tevent_req *req,
4467                                       off_t *copied)
4468 {
4469         struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
4470                 req, struct fruit_copy_chunk_state);
4471         NTSTATUS status;
4472
4473         if (tevent_req_is_nterror(req, &status)) {
4474                 DEBUG(1, ("server side copy chunk failed: %s\n",
4475                           nt_errstr(status)));
4476                 *copied = 0;
4477                 tevent_req_received(req);
4478                 return status;
4479         }
4480
4481         *copied = fruit_copy_chunk_state->copied;
4482         tevent_req_received(req);
4483
4484         return NT_STATUS_OK;
4485 }
4486
4487 static struct vfs_fn_pointers vfs_fruit_fns = {
4488         .connect_fn = fruit_connect,
4489
4490         /* File operations */
4491         .chmod_fn = fruit_chmod,
4492         .chown_fn = fruit_chown,
4493         .unlink_fn = fruit_unlink,
4494         .rename_fn = fruit_rename,
4495         .rmdir_fn = fruit_rmdir,
4496         .open_fn = fruit_open,
4497         .pread_fn = fruit_pread,
4498         .pwrite_fn = fruit_pwrite,
4499         .stat_fn = fruit_stat,
4500         .lstat_fn = fruit_lstat,
4501         .fstat_fn = fruit_fstat,
4502         .streaminfo_fn = fruit_streaminfo,
4503         .ntimes_fn = fruit_ntimes,
4504         .ftruncate_fn = fruit_ftruncate,
4505         .fallocate_fn = fruit_fallocate,
4506         .create_file_fn = fruit_create_file,
4507         .readdir_attr_fn = fruit_readdir_attr,
4508         .copy_chunk_send_fn = fruit_copy_chunk_send,
4509         .copy_chunk_recv_fn = fruit_copy_chunk_recv,
4510
4511         /* NT ACL operations */
4512         .fget_nt_acl_fn = fruit_fget_nt_acl,
4513         .fset_nt_acl_fn = fruit_fset_nt_acl,
4514 };
4515
4516 NTSTATUS vfs_fruit_init(void);
4517 NTSTATUS vfs_fruit_init(void)
4518 {
4519         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
4520                                         &vfs_fruit_fns);
4521         if (!NT_STATUS_IS_OK(ret)) {
4522                 return ret;
4523         }
4524
4525         vfs_fruit_debug_level = debug_add_class("fruit");
4526         if (vfs_fruit_debug_level == -1) {
4527                 vfs_fruit_debug_level = DBGC_VFS;
4528                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
4529                           "vfs_fruit_init"));
4530         } else {
4531                 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
4532                            "vfs_fruit_init","fruit",vfs_fruit_debug_level));
4533         }
4534
4535         return ret;
4536 }