vfs_fruit: hide the Netatalk metadata xattr in streaminfo
[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.ressource".
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 /*
104  * REVIEW:
105  * This is hokey, but what else can we do?
106  */
107 #define NETATALK_META_XATTR "org.netatalk.Metadata"
108 #if defined(HAVE_ATTROPEN) || defined(FREEBSD)
109 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
110 #define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
111 #else
112 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
113 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
114 #endif
115
116 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
117
118 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
119 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
120 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
121 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
122
123 struct fruit_config_data {
124         enum fruit_rsrc rsrc;
125         enum fruit_meta meta;
126         enum fruit_locking locking;
127         enum fruit_encoding encoding;
128         bool use_aapl;
129         bool use_copyfile;
130         bool readdir_attr_enabled;
131         bool unix_info_enabled;
132         bool copyfile_enabled;
133         bool veto_appledouble;
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 #define ad_entry(ad,eid)           ((ad)->ad_data + ad_getentryoff((ad),(eid)))
345
346 struct ad_entry {
347         size_t ade_off;
348         size_t ade_len;
349 };
350
351 struct adouble {
352         vfs_handle_struct        *ad_handle;
353         files_struct             *ad_fsp;
354         adouble_type_t            ad_type;
355         uint32_t                  ad_magic;
356         uint32_t                  ad_version;
357         struct ad_entry           ad_eid[ADEID_MAX];
358         char                     *ad_data;
359 };
360
361 struct ad_entry_order {
362         uint32_t id, offset, len;
363 };
364
365 /* Netatalk AppleDouble metadata xattr */
366 static const
367 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
368         {ADEID_FINDERI,    ADEDOFF_FINDERI_XATTR,    ADEDLEN_FINDERI},
369         {ADEID_COMMENT,    ADEDOFF_COMMENT_XATTR,    0},
370         {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
371         {ADEID_AFPFILEI,   ADEDOFF_AFPFILEI_XATTR,   ADEDLEN_AFPFILEI},
372         {ADEID_PRIVDEV,    ADEDOFF_PRIVDEV_XATTR,    0},
373         {ADEID_PRIVINO,    ADEDOFF_PRIVINO_XATTR,    0},
374         {ADEID_PRIVSYN,    ADEDOFF_PRIVSYN_XATTR,    0},
375         {ADEID_PRIVID,     ADEDOFF_PRIVID_XATTR,     0},
376         {0, 0, 0}
377 };
378
379 /* AppleDouble ressource fork file (the ones prefixed by "._") */
380 static const
381 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
382         {ADEID_FINDERI,    ADEDOFF_FINDERI_DOT_UND,  ADEDLEN_FINDERI},
383         {ADEID_RFORK,      ADEDOFF_RFORK_DOT_UND,    0},
384         {0, 0, 0}
385 };
386
387 /*
388  * Fake AppleDouble entry oder for ressource fork xattr.  The xattr
389  * isn't an AppleDouble file, it simply contains the ressource data,
390  * but in order to be able to use some API calls like ad_getentryoff()
391  * we build a fake/helper struct adouble with this entry order struct.
392  */
393 static const
394 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
395         {ADEID_RFORK, 0, 0},
396         {0, 0, 0}
397 };
398
399 /* Conversion from enumerated id to on-disk AppleDouble id */
400 #define AD_EID_DISK(a) (set_eid[a])
401 static const uint32_t set_eid[] = {
402         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
403         AD_DEV, AD_INO, AD_SYN, AD_ID
404 };
405
406 /*
407  * Forward declarations
408  */
409 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
410                                adouble_type_t type, files_struct *fsp);
411 static int ad_write(struct adouble *ad, const char *path);
412 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
413
414 /**
415  * Get a date
416  **/
417 static int ad_getdate(const struct adouble *ad,
418                       unsigned int dateoff,
419                       uint32_t *date)
420 {
421         bool xlate = (dateoff & AD_DATE_UNIX);
422
423         dateoff &= AD_DATE_MASK;
424         if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
425                 return -1;
426         }
427
428         if (dateoff > AD_DATE_ACCESS) {
429             return -1;
430         }
431         memcpy(date,
432                ad_entry(ad, ADEID_FILEDATESI) + dateoff,
433                sizeof(uint32_t));
434
435         if (xlate) {
436                 *date = AD_DATE_TO_UNIX(*date);
437         }
438         return 0;
439 }
440
441 /**
442  * Set a date
443  **/
444 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
445 {
446         bool xlate = (dateoff & AD_DATE_UNIX);
447
448         if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
449                 return 0;
450         }
451
452         dateoff &= AD_DATE_MASK;
453         if (xlate) {
454                 date = AD_DATE_FROM_UNIX(date);
455         }
456
457         if (dateoff > AD_DATE_ACCESS) {
458                 return -1;
459         }
460
461         memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
462
463         return 0;
464 }
465
466
467 /**
468  * Map on-disk AppleDouble id to enumerated id
469  **/
470 static uint32_t get_eid(uint32_t eid)
471 {
472         if (eid <= 15) {
473                 return eid;
474         }
475
476         switch (eid) {
477         case AD_DEV:
478                 return ADEID_PRIVDEV;
479         case AD_INO:
480                 return ADEID_PRIVINO;
481         case AD_SYN:
482                 return ADEID_PRIVSYN;
483         case AD_ID:
484                 return ADEID_PRIVID;
485         default:
486                 break;
487         }
488
489         return 0;
490 }
491
492 /**
493  * Pack AppleDouble structure into data buffer
494  **/
495 static bool ad_pack(struct adouble *ad)
496 {
497         uint32_t       eid;
498         uint16_t       nent;
499         uint32_t       bufsize;
500         uint32_t       offset = 0;
501
502         bufsize = talloc_get_size(ad->ad_data);
503
504         if (offset + ADEDLEN_MAGIC < offset ||
505                         offset + ADEDLEN_MAGIC >= bufsize) {
506                 return false;
507         }
508         RSIVAL(ad->ad_data, offset, ad->ad_magic);
509         offset += ADEDLEN_MAGIC;
510
511         if (offset + ADEDLEN_VERSION < offset ||
512                         offset + ADEDLEN_VERSION >= bufsize) {
513                 return false;
514         }
515         RSIVAL(ad->ad_data, offset, ad->ad_version);
516         offset += ADEDLEN_VERSION;
517
518         if (offset + ADEDLEN_FILLER < offset ||
519                         offset + ADEDLEN_FILLER >= bufsize) {
520                 return false;
521         }
522         if (ad->ad_type == ADOUBLE_RSRC) {
523                 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
524         }
525         offset += ADEDLEN_FILLER;
526
527         if (offset + ADEDLEN_NENTRIES < offset ||
528                         offset + ADEDLEN_NENTRIES >= bufsize) {
529                 return false;
530         }
531         offset += ADEDLEN_NENTRIES;
532
533         for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
534                 if (ad->ad_eid[eid].ade_off == 0) {
535                         /*
536                          * ade_off is also used as indicator whether a
537                          * specific entry is used or not
538                          */
539                         continue;
540                 }
541
542                 if (offset + AD_ENTRY_LEN_EID < offset ||
543                                 offset + AD_ENTRY_LEN_EID >= bufsize) {
544                         return false;
545                 }
546                 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
547                 offset += AD_ENTRY_LEN_EID;
548
549                 if (offset + AD_ENTRY_LEN_OFF < offset ||
550                                 offset + AD_ENTRY_LEN_OFF >= bufsize) {
551                         return false;
552                 }
553                 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
554                 offset += AD_ENTRY_LEN_OFF;
555
556                 if (offset + AD_ENTRY_LEN_LEN < offset ||
557                                 offset + AD_ENTRY_LEN_LEN >= bufsize) {
558                         return false;
559                 }
560                 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
561                 offset += AD_ENTRY_LEN_LEN;
562
563                 nent++;
564         }
565
566         if (ADEDOFF_NENTRIES + 2 >= bufsize) {
567                 return false;
568         }
569         RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
570
571         return true;
572 }
573
574 /**
575  * Unpack an AppleDouble blob into a struct adoble
576  **/
577 static bool ad_unpack(struct adouble *ad, const int nentries, size_t filesize)
578 {
579         size_t bufsize = talloc_get_size(ad->ad_data);
580         int adentries, i;
581         uint32_t eid, len, off;
582
583         /*
584          * The size of the buffer ad->ad_data is checked when read, so
585          * we wouldn't have to check our own offsets, a few extra
586          * checks won't hurt though. We have to check the offsets we
587          * read from the buffer anyway.
588          */
589
590         if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
591                 DEBUG(1, ("bad size\n"));
592                 return false;
593         }
594
595         ad->ad_magic = RIVAL(ad->ad_data, 0);
596         ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
597         if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
598                 DEBUG(1, ("wrong magic or version\n"));
599                 return false;
600         }
601
602         adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
603         if (adentries != nentries) {
604                 DEBUG(1, ("invalid number of entries: %d\n", adentries));
605                 return false;
606         }
607
608         /* now, read in the entry bits */
609         for (i = 0; i < adentries; i++) {
610                 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
611                 eid = get_eid(eid);
612                 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
613                 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
614
615                 if (!eid || eid > ADEID_MAX) {
616                         DEBUG(1, ("bogus eid %d\n", eid));
617                         return false;
618                 }
619
620                 /*
621                  * All entries other than the resource fork are
622                  * expected to be read into the ad_data buffer, so
623                  * ensure the specified offset is within that bound
624                  */
625                 if ((off > bufsize) && (eid != ADEID_RFORK)) {
626                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
627                                   eid, off, len));
628                         return false;
629                 }
630
631                 /*
632                  * All entries besides FinderInfo and resource fork
633                  * must fit into the buffer. FinderInfo is special as
634                  * it may be larger then the default 32 bytes (if it
635                  * contains marshalled xattrs), but we will fixup that
636                  * in ad_convert(). And the resource fork is never
637                  * accessed directly by the ad_data buf (also see
638                  * comment above) anyway.
639                  */
640                 if ((eid != ADEID_RFORK) &&
641                     (eid != ADEID_FINDERI) &&
642                     ((off + len) > bufsize)) {
643                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
644                                   eid, off, len));
645                         return false;
646                 }
647
648                 /*
649                  * That would be obviously broken
650                  */
651                 if (off > filesize) {
652                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
653                                   eid, off, len));
654                         return false;
655                 }
656
657                 /*
658                  * Check for any entry that has its end beyond the
659                  * filesize.
660                  */
661                 if (off + len < off) {
662                         DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
663                                   ", len: %" PRIu32 "\n",
664                                   eid, off, len));
665                         return false;
666
667                 }
668                 if (off + len > filesize) {
669                         /*
670                          * If this is the resource fork entry, we fix
671                          * up the length, for any other entry we bail
672                          * out.
673                          */
674                         if (eid != ADEID_RFORK) {
675                                 DEBUG(1, ("bogus eid %d: off: %" PRIu32
676                                           ", len: %" PRIu32 "\n",
677                                           eid, off, len));
678                                 return false;
679                         }
680
681                         /*
682                          * Fixup the resource fork entry by limiting
683                          * the size to entryoffset - filesize.
684                          */
685                         len = filesize - off;
686                         DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
687                                   ", len: %" PRIu32 "\n", off, len));
688                 }
689
690                 ad->ad_eid[eid].ade_off = off;
691                 ad->ad_eid[eid].ade_len = len;
692         }
693
694         return true;
695 }
696
697 /**
698  * Convert from Apple's ._ file to Netatalk
699  *
700  * Apple's AppleDouble may contain a FinderInfo entry longer then 32
701  * bytes containing packed xattrs. Netatalk can't deal with that, so
702  * we simply discard the packed xattrs.
703  *
704  * @return -1 in case an error occured, 0 if no conversion was done, 1
705  * otherwise
706  **/
707 static int ad_convert(struct adouble *ad, int fd)
708 {
709         int rc = 0;
710         char *map = MAP_FAILED;
711         size_t origlen;
712
713         origlen = ad_getentryoff(ad, ADEID_RFORK) +
714                 ad_getentrylen(ad, ADEID_RFORK);
715
716         /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
717         map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
718         if (map == MAP_FAILED) {
719                 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
720                 rc = -1;
721                 goto exit;
722         }
723
724         if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
725                 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
726                         map + ad_getentryoff(ad, ADEID_RFORK),
727                         ad_getentrylen(ad, ADEID_RFORK));
728         }
729
730         ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
731         ad_setentryoff(ad, ADEID_RFORK,
732                        ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
733
734         /*
735          * FIXME: direct ftruncate(), but we don't have a fsp for the
736          * VFS call
737          */
738         rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
739                        + ad_getentrylen(ad, ADEID_RFORK));
740
741 exit:
742         if (map != MAP_FAILED) {
743                 munmap(map, origlen);
744         }
745         return rc;
746 }
747
748 /**
749  * Read and parse Netatalk AppleDouble metadata xattr
750  **/
751 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
752 {
753         int      rc = 0;
754         ssize_t  ealen;
755         bool     ok;
756
757         DEBUG(10, ("reading meta xattr for %s\n", path));
758
759         ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
760                                  AFPINFO_EA_NETATALK, ad->ad_data,
761                                  AD_DATASZ_XATTR);
762         if (ealen == -1) {
763                 switch (errno) {
764                 case ENOATTR:
765                 case ENOENT:
766                         if (errno == ENOATTR) {
767                                 errno = ENOENT;
768                         }
769                         rc = -1;
770                         goto exit;
771                 default:
772                         DEBUG(2, ("error reading meta xattr: %s\n",
773                                   strerror(errno)));
774                         rc = -1;
775                         goto exit;
776                 }
777         }
778         if (ealen != AD_DATASZ_XATTR) {
779                 DEBUG(2, ("bad size %zd\n", ealen));
780                 errno = EINVAL;
781                 rc = -1;
782                 goto exit;
783         }
784
785         /* Now parse entries */
786         ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
787         if (!ok) {
788                 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
789                 errno = EINVAL;
790                 rc = -1;
791                 goto exit;
792         }
793
794         if (!ad_getentryoff(ad, ADEID_FINDERI)
795             || !ad_getentryoff(ad, ADEID_COMMENT)
796             || !ad_getentryoff(ad, ADEID_FILEDATESI)
797             || !ad_getentryoff(ad, ADEID_AFPFILEI)
798             || !ad_getentryoff(ad, ADEID_PRIVDEV)
799             || !ad_getentryoff(ad, ADEID_PRIVINO)
800             || !ad_getentryoff(ad, ADEID_PRIVSYN)
801             || !ad_getentryoff(ad, ADEID_PRIVID)) {
802                 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
803                 errno = EINVAL;
804                 rc = -1;
805                 goto exit;
806         }
807
808 exit:
809         DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
810
811         if (rc != 0) {
812                 ealen = -1;
813                 if (errno == EINVAL) {
814                         become_root();
815                         removexattr(path, AFPINFO_EA_NETATALK);
816                         unbecome_root();
817                         errno = ENOENT;
818                 }
819         }
820         return ealen;
821 }
822
823 /**
824  * Read and parse resource fork, either ._ AppleDouble file or xattr
825  **/
826 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
827 {
828         struct fruit_config_data *config = NULL;
829         int fd = -1;
830         int rc = 0;
831         ssize_t len;
832         char *adpath = NULL;
833         bool opened = false;
834         int mode;
835         struct adouble *meta_ad = NULL;
836         SMB_STRUCT_STAT sbuf;
837         bool ok;
838         int saved_errno = 0;
839
840         SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
841                                 struct fruit_config_data, return -1);
842
843         /* Try rw first so we can use the fd in ad_convert() */
844         mode = O_RDWR;
845
846         if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
847                 fd = ad->ad_fsp->fh->fd;
848         } else {
849                 if (config->rsrc == FRUIT_RSRC_XATTR) {
850                         adpath = talloc_strdup(talloc_tos(), path);
851                 } else {
852                         rc = adouble_path(talloc_tos(), path, &adpath);
853                         if (rc != 0) {
854                                 goto exit;
855                         }
856                 }
857
858         retry:
859                 if (config->rsrc == FRUIT_RSRC_XATTR) {
860 #ifndef HAVE_ATTROPEN
861                         errno = ENOSYS;
862                         rc = -1;
863                         goto exit;
864 #else
865                         /* FIXME: direct Solaris xattr syscall */
866                         fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
867                                       mode, 0);
868 #endif
869                 } else {
870                         /* FIXME: direct open(), don't have an fsp */
871                         fd = open(adpath, mode);
872                 }
873
874                 if (fd == -1) {
875                         switch (errno) {
876                         case EROFS:
877                         case EACCES:
878                                 if (mode == O_RDWR) {
879                                         mode = O_RDONLY;
880                                         goto retry;
881                                 }
882                                 /* fall through ... */
883                         default:
884                                 DEBUG(2, ("open AppleDouble: %s, %s\n",
885                                           adpath, strerror(errno)));
886                                 rc = -1;
887                                 goto exit;
888                         }
889                 }
890                 opened = true;
891         }
892
893         if (config->rsrc == FRUIT_RSRC_XATTR) {
894                 /* FIXME: direct sys_fstat(), don't have an fsp */
895                 rc = sys_fstat(
896                         fd, &sbuf,
897                         lp_fake_directory_create_times(
898                                 SNUM(ad->ad_handle->conn)));
899                 if (rc != 0) {
900                         goto exit;
901                 }
902                 len = sbuf.st_ex_size;
903                 ad_setentrylen(ad, ADEID_RFORK, len);
904         } else {
905                 /* FIXME: direct sys_pread(), don't have an fsp */
906                 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
907                 if (len != AD_DATASZ_DOT_UND) {
908                         DEBUG(2, ("%s: bad size: %zd\n",
909                                   strerror(errno), len));
910                         rc = -1;
911                         goto exit;
912                 }
913
914                 /* FIXME: direct sys_fstat(), we don't have an fsp */
915                 rc = sys_fstat(fd, &sbuf,
916                                lp_fake_directory_create_times(
917                                        SNUM(ad->ad_handle->conn)));
918                 if (rc != 0) {
919                         goto exit;
920                 }
921
922                 /* Now parse entries */
923                 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
924                 if (!ok) {
925                         DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
926                         errno = EINVAL;
927                         rc = -1;
928                         goto exit;
929                 }
930
931                 if ((ad_getentryoff(ad, ADEID_FINDERI)
932                      != ADEDOFF_FINDERI_DOT_UND)
933                     || (ad_getentrylen(ad, ADEID_FINDERI)
934                         < ADEDLEN_FINDERI)
935                     || (ad_getentryoff(ad, ADEID_RFORK)
936                         < ADEDOFF_RFORK_DOT_UND)) {
937                         DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
938                         errno = EINVAL;
939                         rc = -1;
940                         goto exit;
941                 }
942
943                 if ((mode == O_RDWR)
944                     && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
945                         rc = ad_convert(ad, fd);
946                         if (rc != 0) {
947                                 rc = -1;
948                                 goto exit;
949                         }
950                         /*
951                          * Can't use ad_write() because we might not have a fsp
952                          */
953                         ok = ad_pack(ad);
954                         if (!ok) {
955                                 rc = -1;
956                                 goto exit;
957                         }
958                         /* FIXME: direct sys_pwrite(), don't have an fsp */
959                         len = sys_pwrite(fd, ad->ad_data,
960                                          AD_DATASZ_DOT_UND, 0);
961                         if (len != AD_DATASZ_DOT_UND) {
962                                 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
963                                 rc = -1;
964                                 goto exit;
965                         }
966
967                         meta_ad = ad_init(talloc_tos(), ad->ad_handle,
968                                           ADOUBLE_META, NULL);
969                         if (meta_ad == NULL) {
970                                 rc = -1;
971                                 goto exit;
972                         }
973
974                         memcpy(ad_entry(meta_ad, ADEID_FINDERI),
975                                ad_entry(ad, ADEID_FINDERI),
976                                ADEDLEN_FINDERI);
977
978                         rc = ad_write(meta_ad, path);
979                         if (rc != 0) {
980                                 rc = -1;
981                                 goto exit;
982                         }
983                 }
984         }
985
986         DEBUG(10, ("opened AppleDouble: %s\n", path));
987
988 exit:
989         if (rc != 0) {
990                 saved_errno = errno;
991                 len = -1;
992         }
993         if (opened && fd != -1) {
994                 close(fd);
995         }
996         TALLOC_FREE(adpath);
997         TALLOC_FREE(meta_ad);
998         if (rc != 0) {
999                 errno = saved_errno;
1000         }
1001         return len;
1002 }
1003
1004 /**
1005  * Read and unpack an AppleDouble metadata xattr or resource
1006  **/
1007 static ssize_t ad_read(struct adouble *ad, const char *path)
1008 {
1009         switch (ad->ad_type) {
1010         case ADOUBLE_META:
1011                 return ad_header_read_meta(ad, path);
1012         case ADOUBLE_RSRC:
1013                 return ad_header_read_rsrc(ad, path);
1014         default:
1015                 return -1;
1016         }
1017 }
1018
1019 /**
1020  * Allocate a struct adouble without initialiing it
1021  *
1022  * The struct is either hang of the fsp extension context or if fsp is
1023  * NULL from ctx.
1024  *
1025  * @param[in] ctx        talloc context
1026  * @param[in] handle     vfs handle
1027  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1028
1029  * @param[in] fsp        if not NULL (for stream IO), the adouble handle is
1030  *                       added as an fsp extension
1031  *
1032  * @return               adouble handle
1033  **/
1034 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1035                                 adouble_type_t type, files_struct *fsp)
1036 {
1037         int rc = 0;
1038         size_t adsize = 0;
1039         struct adouble *ad;
1040         struct fruit_config_data *config;
1041
1042         SMB_VFS_HANDLE_GET_DATA(handle, config,
1043                                 struct fruit_config_data, return NULL);
1044
1045         switch (type) {
1046         case ADOUBLE_META:
1047                 adsize = AD_DATASZ_XATTR;
1048                 break;
1049         case ADOUBLE_RSRC:
1050                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1051                         adsize = AD_DATASZ_DOT_UND;
1052                 }
1053                 break;
1054         default:
1055                 return NULL;
1056         }
1057
1058         if (!fsp) {
1059                 ad = talloc_zero(ctx, struct adouble);
1060                 if (ad == NULL) {
1061                         rc = -1;
1062                         goto exit;
1063                 }
1064                 if (adsize) {
1065                         ad->ad_data = talloc_zero_array(ad, char, adsize);
1066                 }
1067         } else {
1068                 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1069                                                              struct adouble,
1070                                                              NULL);
1071                 if (ad == NULL) {
1072                         rc = -1;
1073                         goto exit;
1074                 }
1075                 if (adsize) {
1076                         ad->ad_data = talloc_zero_array(
1077                                 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1078                                 char, adsize);
1079                 }
1080                 ad->ad_fsp = fsp;
1081         }
1082
1083         if (adsize && ad->ad_data == NULL) {
1084                 rc = -1;
1085                 goto exit;
1086         }
1087         ad->ad_handle = handle;
1088         ad->ad_type = type;
1089         ad->ad_magic = AD_MAGIC;
1090         ad->ad_version = AD_VERSION;
1091
1092 exit:
1093         if (rc != 0) {
1094                 TALLOC_FREE(ad);
1095         }
1096         return ad;
1097 }
1098
1099 /**
1100  * Allocate and initialize a new struct adouble
1101  *
1102  * @param[in] ctx        talloc context
1103  * @param[in] handle     vfs handle
1104  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1105  * @param[in] fsp        file handle, may be NULL for a type of e_ad_meta
1106  *
1107  * @return               adouble handle, initialized
1108  **/
1109 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1110                                adouble_type_t type, files_struct *fsp)
1111 {
1112         int rc = 0;
1113         const struct ad_entry_order  *eid;
1114         struct adouble *ad = NULL;
1115         struct fruit_config_data *config;
1116         time_t t = time(NULL);
1117
1118         SMB_VFS_HANDLE_GET_DATA(handle, config,
1119                                 struct fruit_config_data, return NULL);
1120
1121         switch (type) {
1122         case ADOUBLE_META:
1123                 eid = entry_order_meta_xattr;
1124                 break;
1125         case ADOUBLE_RSRC:
1126                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1127                         eid = entry_order_dot_und;
1128                 } else {
1129                         eid = entry_order_rsrc_xattr;
1130                 }
1131                 break;
1132         default:
1133                 return NULL;
1134         }
1135
1136         ad = ad_alloc(ctx, handle, type, fsp);
1137         if (ad == NULL) {
1138                 return NULL;
1139         }
1140
1141         while (eid->id) {
1142                 ad->ad_eid[eid->id].ade_off = eid->offset;
1143                 ad->ad_eid[eid->id].ade_len = eid->len;
1144                 eid++;
1145         }
1146
1147         /* put something sane in the date fields */
1148         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1149         ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1150         ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1151         ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1152
1153         if (rc != 0) {
1154                 TALLOC_FREE(ad);
1155         }
1156         return ad;
1157 }
1158
1159 /**
1160  * Return AppleDouble data for a file
1161  *
1162  * @param[in] ctx      talloc context
1163  * @param[in] handle   vfs handle
1164  * @param[in] path     pathname to file or directory
1165  * @param[in] type     type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1166  *
1167  * @return             talloced struct adouble or NULL on error
1168  **/
1169 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1170                               const char *path, adouble_type_t type)
1171 {
1172         int rc = 0;
1173         ssize_t len;
1174         struct adouble *ad = NULL;
1175
1176         DEBUG(10, ("ad_get(%s) called for %s\n",
1177                    type == ADOUBLE_META ? "meta" : "rsrc", path));
1178
1179         ad = ad_alloc(ctx, handle, type, NULL);
1180         if (ad == NULL) {
1181                 rc = -1;
1182                 goto exit;
1183         }
1184
1185         len = ad_read(ad, path);
1186         if (len == -1) {
1187                 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1188                 rc = -1;
1189                 goto exit;
1190         }
1191
1192 exit:
1193         DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1194                   type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1195
1196         if (rc != 0) {
1197                 TALLOC_FREE(ad);
1198         }
1199         return ad;
1200 }
1201
1202 /**
1203  * Set AppleDouble metadata on a file or directory
1204  *
1205  * @param[in] ad      adouble handle
1206
1207  * @param[in] path    pathname to file or directory, may be NULL for a
1208  *                    resource fork
1209  *
1210  * @return            status code, 0 means success
1211  **/
1212 static int ad_write(struct adouble *ad, const char *path)
1213 {
1214         int rc = 0;
1215         ssize_t len;
1216         bool ok;
1217
1218         ok = ad_pack(ad);
1219         if (!ok) {
1220                 return -1;
1221         }
1222
1223         switch (ad->ad_type) {
1224         case ADOUBLE_META:
1225                 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1226                                       AFPINFO_EA_NETATALK, ad->ad_data,
1227                                       AD_DATASZ_XATTR, 0);
1228                 break;
1229         case ADOUBLE_RSRC:
1230                 if ((ad->ad_fsp == NULL)
1231                     || (ad->ad_fsp->fh == NULL)
1232                     || (ad->ad_fsp->fh->fd == -1)) {
1233                         rc = -1;
1234                         goto exit;
1235                 }
1236                 /* FIXME: direct sys_pwrite(), don't have an fsp */
1237                 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1238                                  talloc_get_size(ad->ad_data), 0);
1239                 if (len != talloc_get_size(ad->ad_data)) {
1240                         DEBUG(1, ("short write on %s: %zd",
1241                                   fsp_str_dbg(ad->ad_fsp), len));
1242                         rc = -1;
1243                         goto exit;
1244                 }
1245                 break;
1246         default:
1247                 return -1;
1248         }
1249 exit:
1250         return rc;
1251 }
1252
1253 /*****************************************************************************
1254  * Helper functions
1255  *****************************************************************************/
1256
1257 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1258 {
1259         if (strncasecmp_m(smb_fname->stream_name,
1260                           AFPINFO_STREAM_NAME,
1261                           strlen(AFPINFO_STREAM_NAME)) == 0) {
1262                 return true;
1263         }
1264         return false;
1265 }
1266
1267 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1268 {
1269         if (strncasecmp_m(smb_fname->stream_name,
1270                           AFPRESOURCE_STREAM_NAME,
1271                           strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1272                 return true;
1273         }
1274         return false;
1275 }
1276
1277 /**
1278  * Test whether stream is an Apple stream, not used atm
1279  **/
1280 #if 0
1281 static bool is_apple_stream(const struct smb_filename *smb_fname)
1282 {
1283         if (is_afpinfo_stream(smb_fname)) {
1284                 return true;
1285         }
1286         if (is_afpresource_stream(smb_fname)) {
1287                 return true;
1288         }
1289         return false;
1290 }
1291 #endif
1292
1293 /**
1294  * Initialize config struct from our smb.conf config parameters
1295  **/
1296 static int init_fruit_config(vfs_handle_struct *handle)
1297 {
1298         struct fruit_config_data *config;
1299         int enumval;
1300
1301         config = talloc_zero(handle->conn, struct fruit_config_data);
1302         if (!config) {
1303                 DEBUG(1, ("talloc_zero() failed\n"));
1304                 errno = ENOMEM;
1305                 return -1;
1306         }
1307
1308         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1309                                "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1310         if (enumval == -1) {
1311                 DEBUG(1, ("value for %s: ressource type unknown\n",
1312                           FRUIT_PARAM_TYPE_NAME));
1313                 return -1;
1314         }
1315         config->rsrc = (enum fruit_rsrc)enumval;
1316
1317         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1318                                "metadata", fruit_meta, FRUIT_META_NETATALK);
1319         if (enumval == -1) {
1320                 DEBUG(1, ("value for %s: metadata type unknown\n",
1321                           FRUIT_PARAM_TYPE_NAME));
1322                 return -1;
1323         }
1324         config->meta = (enum fruit_meta)enumval;
1325
1326         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1327                                "locking", fruit_locking, FRUIT_LOCKING_NONE);
1328         if (enumval == -1) {
1329                 DEBUG(1, ("value for %s: locking type unknown\n",
1330                           FRUIT_PARAM_TYPE_NAME));
1331                 return -1;
1332         }
1333         config->locking = (enum fruit_locking)enumval;
1334
1335         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1336                                "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1337         if (enumval == -1) {
1338                 DEBUG(1, ("value for %s: encoding type unknown\n",
1339                           FRUIT_PARAM_TYPE_NAME));
1340                 return -1;
1341         }
1342         config->encoding = (enum fruit_encoding)enumval;
1343
1344         config->veto_appledouble = lp_parm_bool(
1345                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1346                 "veto_appledouble", true);
1347
1348         config->use_aapl = lp_parm_bool(
1349                 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1350
1351         config->unix_info_enabled = lp_parm_bool(
1352                 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1353
1354         config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1355                                            "copyfile", false);
1356
1357         config->readdir_attr_rsize = lp_parm_bool(
1358                 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1359
1360         config->readdir_attr_finder_info = lp_parm_bool(
1361                 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1362
1363         config->readdir_attr_max_access = lp_parm_bool(
1364                 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1365
1366         SMB_VFS_HANDLE_SET_DATA(handle, config,
1367                                 NULL, struct fruit_config_data,
1368                                 return -1);
1369
1370         return 0;
1371 }
1372
1373 /**
1374  * Prepend "._" to a basename
1375  **/
1376 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1377 {
1378         char *parent;
1379         const char *base;
1380
1381         if (!parent_dirname(ctx, path_in, &parent, &base)) {
1382                 return -1;
1383         }
1384
1385         *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1386         if (*path_out == NULL) {
1387                 return -1;
1388         }
1389
1390         return 0;
1391 }
1392
1393 /**
1394  * Allocate and initialize an AfpInfo struct
1395  **/
1396 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1397 {
1398         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1399         if (ai == NULL) {
1400                 return NULL;
1401         }
1402         ai->afpi_Signature = AFP_Signature;
1403         ai->afpi_Version = AFP_Version;
1404         ai->afpi_BackupTime = AD_DATE_START;
1405         return ai;
1406 }
1407
1408 /**
1409  * Pack an AfpInfo struct into a buffer
1410  *
1411  * Buffer size must be at least AFP_INFO_SIZE
1412  * Returns size of packed buffer
1413  **/
1414 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1415 {
1416         memset(buf, 0, AFP_INFO_SIZE);
1417
1418         RSIVAL(buf, 0, ai->afpi_Signature);
1419         RSIVAL(buf, 4, ai->afpi_Version);
1420         RSIVAL(buf, 12, ai->afpi_BackupTime);
1421         memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1422
1423         return AFP_INFO_SIZE;
1424 }
1425
1426 /**
1427  * Unpack a buffer into a AfpInfo structure
1428  *
1429  * Buffer size must be at least AFP_INFO_SIZE
1430  * Returns allocated AfpInfo struct
1431  **/
1432 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1433 {
1434         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1435         if (ai == NULL) {
1436                 return NULL;
1437         }
1438
1439         ai->afpi_Signature = RIVAL(data, 0);
1440         ai->afpi_Version = RIVAL(data, 4);
1441         ai->afpi_BackupTime = RIVAL(data, 12);
1442         memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1443                sizeof(ai->afpi_FinderInfo));
1444
1445         if (ai->afpi_Signature != AFP_Signature
1446             || ai->afpi_Version != AFP_Version) {
1447                 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1448                 TALLOC_FREE(ai);
1449         }
1450
1451         return ai;
1452 }
1453
1454 /**
1455  * Fake an inode number from the md5 hash of the (xattr) name
1456  **/
1457 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1458 {
1459         MD5_CTX ctx;
1460         unsigned char hash[16];
1461         SMB_INO_T result;
1462         char *upper_sname;
1463
1464         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1465         SMB_ASSERT(upper_sname != NULL);
1466
1467         MD5Init(&ctx);
1468         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1469                   sizeof(sbuf->st_ex_dev));
1470         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1471                   sizeof(sbuf->st_ex_ino));
1472         MD5Update(&ctx, (unsigned char *)upper_sname,
1473                   talloc_get_size(upper_sname)-1);
1474         MD5Final(hash, &ctx);
1475
1476         TALLOC_FREE(upper_sname);
1477
1478         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1479         memcpy(&result, hash, sizeof(result));
1480
1481         DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1482                    sname, (unsigned long long)result));
1483
1484         return result;
1485 }
1486
1487 /**
1488  * Ensure ad_fsp is still valid
1489  **/
1490 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1491 {
1492         if (ad->ad_fsp == fsp) {
1493                 return true;
1494         }
1495         ad->ad_fsp = fsp;
1496
1497         return true;
1498 }
1499
1500 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1501                              struct stream_struct **streams,
1502                              const char *name, off_t size,
1503                              off_t alloc_size)
1504 {
1505         struct stream_struct *tmp;
1506
1507         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1508                              (*num_streams)+1);
1509         if (tmp == NULL) {
1510                 return false;
1511         }
1512
1513         tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1514         if (tmp[*num_streams].name == NULL) {
1515                 return false;
1516         }
1517
1518         tmp[*num_streams].size = size;
1519         tmp[*num_streams].alloc_size = alloc_size;
1520
1521         *streams = tmp;
1522         *num_streams += 1;
1523         return true;
1524 }
1525
1526 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1527                              struct stream_struct **streams,
1528                              const char *name)
1529 {
1530         struct stream_struct *tmp = *streams;
1531         int i;
1532
1533         if (*num_streams == 0) {
1534                 return true;
1535         }
1536
1537         for (i = 0; i < *num_streams; i++) {
1538                 if (strequal_m(tmp[i].name, name)) {
1539                         break;
1540                 }
1541         }
1542
1543         if (i == *num_streams) {
1544                 return true;
1545         }
1546
1547         TALLOC_FREE(tmp[i].name);
1548         if (*num_streams - 1 > i) {
1549                 memmove(&tmp[i], &tmp[i+1],
1550                         (*num_streams - i - 1) * sizeof(struct stream_struct));
1551         }
1552
1553         *num_streams -= 1;
1554         return true;
1555 }
1556
1557 static bool empty_finderinfo(const struct adouble *ad)
1558 {
1559
1560         char emptybuf[ADEDLEN_FINDERI] = {0};
1561         if (memcmp(emptybuf,
1562                    ad_entry(ad, ADEID_FINDERI),
1563                    ADEDLEN_FINDERI) == 0) {
1564                 return true;
1565         }
1566         return false;
1567 }
1568
1569 /**
1570  * Update btime with btime from Netatalk
1571  **/
1572 static void update_btime(vfs_handle_struct *handle,
1573                          struct smb_filename *smb_fname)
1574 {
1575         uint32_t t;
1576         struct timespec creation_time = {0};
1577         struct adouble *ad;
1578
1579         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1580         if (ad == NULL) {
1581                 return;
1582         }
1583         if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1584                 TALLOC_FREE(ad);
1585                 return;
1586         }
1587         TALLOC_FREE(ad);
1588
1589         creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1590         update_stat_ex_create_time(&smb_fname->st, creation_time);
1591
1592         return;
1593 }
1594
1595 /**
1596  * Map an access mask to a Netatalk single byte byte range lock
1597  **/
1598 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1599                                     uint32_t access_mask)
1600 {
1601         off_t offset;
1602
1603         switch (access_mask) {
1604         case FILE_READ_DATA:
1605                 offset = AD_FILELOCK_OPEN_RD;
1606                 break;
1607
1608         case FILE_WRITE_DATA:
1609         case FILE_APPEND_DATA:
1610                 offset = AD_FILELOCK_OPEN_WR;
1611                 break;
1612
1613         default:
1614                 offset = AD_FILELOCK_OPEN_NONE;
1615                 break;
1616         }
1617
1618         if (fork_type == APPLE_FORK_RSRC) {
1619                 if (offset == AD_FILELOCK_OPEN_NONE) {
1620                         offset = AD_FILELOCK_RSRC_OPEN_NONE;
1621                 } else {
1622                         offset += 2;
1623                 }
1624         }
1625
1626         return offset;
1627 }
1628
1629 /**
1630  * Map a deny mode to a Netatalk brl
1631  **/
1632 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1633                                       uint32_t deny_mode)
1634 {
1635         off_t offset;
1636
1637         switch (deny_mode) {
1638         case DENY_READ:
1639                 offset = AD_FILELOCK_DENY_RD;
1640                 break;
1641
1642         case DENY_WRITE:
1643                 offset = AD_FILELOCK_DENY_WR;
1644                 break;
1645
1646         default:
1647                 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1648         }
1649
1650         if (fork_type == APPLE_FORK_RSRC) {
1651                 offset += 2;
1652         }
1653
1654         return offset;
1655 }
1656
1657 /**
1658  * Call fcntl() with an exclusive F_GETLK request in order to
1659  * determine if there's an exisiting shared lock
1660  *
1661  * @return true if the requested lock was found or any error occured
1662  *         false if the lock was not found
1663  **/
1664 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1665 {
1666         bool result;
1667         off_t offset = in_offset;
1668         off_t len = 1;
1669         int type = F_WRLCK;
1670         pid_t pid;
1671
1672         result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1673         if (result == false) {
1674                 return true;
1675         }
1676
1677         if (type != F_UNLCK) {
1678                 return true;
1679         }
1680
1681         return false;
1682 }
1683
1684 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1685                                    files_struct *fsp,
1686                                    uint32_t access_mask,
1687                                    uint32_t deny_mode)
1688 {
1689         NTSTATUS status = NT_STATUS_OK;
1690         struct byte_range_lock *br_lck = NULL;
1691         bool open_for_reading, open_for_writing, deny_read, deny_write;
1692         off_t off;
1693
1694         /* FIXME: hardcoded data fork, add resource fork */
1695         enum apple_fork fork_type = APPLE_FORK_DATA;
1696
1697         DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1698                   fsp_str_dbg(fsp),
1699                   access_mask & FILE_READ_DATA ? "READ" :"-",
1700                   access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1701                   deny_mode & DENY_READ ? "DENY_READ" : "-",
1702                   deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1703
1704         /*
1705          * Check read access and deny read mode
1706          */
1707         if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1708                 /* Check access */
1709                 open_for_reading = test_netatalk_lock(
1710                         fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1711
1712                 deny_read = test_netatalk_lock(
1713                         fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1714
1715                 DEBUG(10, ("read: %s, deny_write: %s\n",
1716                           open_for_reading == true ? "yes" : "no",
1717                           deny_read == true ? "yes" : "no"));
1718
1719                 if (((access_mask & FILE_READ_DATA) && deny_read)
1720                     || ((deny_mode & DENY_READ) && open_for_reading)) {
1721                         return NT_STATUS_SHARING_VIOLATION;
1722                 }
1723
1724                 /* Set locks */
1725                 if (access_mask & FILE_READ_DATA) {
1726                         off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1727                         br_lck = do_lock(
1728                                 handle->conn->sconn->msg_ctx, fsp,
1729                                 fsp->op->global->open_persistent_id, 1, off,
1730                                 READ_LOCK, POSIX_LOCK, false,
1731                                 &status, NULL);
1732
1733                         if (!NT_STATUS_IS_OK(status))  {
1734                                 return status;
1735                         }
1736                         TALLOC_FREE(br_lck);
1737                 }
1738
1739                 if (deny_mode & DENY_READ) {
1740                         off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1741                         br_lck = do_lock(
1742                                 handle->conn->sconn->msg_ctx, fsp,
1743                                 fsp->op->global->open_persistent_id, 1, off,
1744                                 READ_LOCK, POSIX_LOCK, false,
1745                                 &status, NULL);
1746
1747                         if (!NT_STATUS_IS_OK(status)) {
1748                                 return status;
1749                         }
1750                         TALLOC_FREE(br_lck);
1751                 }
1752         }
1753
1754         /*
1755          * Check write access and deny write mode
1756          */
1757         if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1758                 /* Check access */
1759                 open_for_writing = test_netatalk_lock(
1760                         fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1761
1762                 deny_write = test_netatalk_lock(
1763                         fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1764
1765                 DEBUG(10, ("write: %s, deny_write: %s\n",
1766                           open_for_writing == true ? "yes" : "no",
1767                           deny_write == true ? "yes" : "no"));
1768
1769                 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1770                     || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1771                         return NT_STATUS_SHARING_VIOLATION;
1772                 }
1773
1774                 /* Set locks */
1775                 if (access_mask & FILE_WRITE_DATA) {
1776                         off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1777                         br_lck = do_lock(
1778                                 handle->conn->sconn->msg_ctx, fsp,
1779                                 fsp->op->global->open_persistent_id, 1, off,
1780                                 READ_LOCK, POSIX_LOCK, false,
1781                                 &status, NULL);
1782
1783                         if (!NT_STATUS_IS_OK(status)) {
1784                                 return status;
1785                         }
1786                         TALLOC_FREE(br_lck);
1787
1788                 }
1789                 if (deny_mode & DENY_WRITE) {
1790                         off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1791                         br_lck = do_lock(
1792                                 handle->conn->sconn->msg_ctx, fsp,
1793                                 fsp->op->global->open_persistent_id, 1, off,
1794                                 READ_LOCK, POSIX_LOCK, false,
1795                                 &status, NULL);
1796
1797                         if (!NT_STATUS_IS_OK(status)) {
1798                                 return status;
1799                         }
1800                         TALLOC_FREE(br_lck);
1801                 }
1802         }
1803
1804         TALLOC_FREE(br_lck);
1805
1806         return status;
1807 }
1808
1809 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1810                            struct smb_request *req,
1811                            const struct smb2_create_blobs *in_context_blobs,
1812                            struct smb2_create_blobs *out_context_blobs)
1813 {
1814         struct fruit_config_data *config;
1815         NTSTATUS status;
1816         struct smb2_create_blob *aapl = NULL;
1817         uint32_t cmd;
1818         bool ok;
1819         uint8_t p[16];
1820         DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1821         uint64_t req_bitmap, client_caps;
1822         uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1823         smb_ucs2_t *model;
1824         size_t modellen;
1825
1826         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1827                                 return NT_STATUS_UNSUCCESSFUL);
1828
1829         if (!config->use_aapl
1830             || in_context_blobs == NULL
1831             || out_context_blobs == NULL) {
1832                 return NT_STATUS_OK;
1833         }
1834
1835         aapl = smb2_create_blob_find(in_context_blobs,
1836                                      SMB2_CREATE_TAG_AAPL);
1837         if (aapl == NULL) {
1838                 return NT_STATUS_OK;
1839         }
1840
1841         if (aapl->data.length != 24) {
1842                 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1843                           (uintmax_t)aapl->data.length));
1844                 return NT_STATUS_INVALID_PARAMETER;
1845         }
1846
1847         cmd = IVAL(aapl->data.data, 0);
1848         if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1849                 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1850                 return NT_STATUS_INVALID_PARAMETER;
1851         }
1852
1853         req_bitmap = BVAL(aapl->data.data, 8);
1854         client_caps = BVAL(aapl->data.data, 16);
1855
1856         SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1857         SIVAL(p, 4, 0);
1858         SBVAL(p, 8, req_bitmap);
1859         ok = data_blob_append(req, &blob, p, 16);
1860         if (!ok) {
1861                 return NT_STATUS_UNSUCCESSFUL;
1862         }
1863
1864         if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1865                 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1866                     (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1867                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1868                         config->readdir_attr_enabled = true;
1869                 }
1870
1871                 if (config->use_copyfile) {
1872                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
1873                         config->copyfile_enabled = true;
1874                 }
1875
1876                 /*
1877                  * The client doesn't set the flag, so we can't check
1878                  * for it and just set it unconditionally
1879                  */
1880                 if (config->unix_info_enabled) {
1881                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1882                 }
1883
1884                 SBVAL(p, 0, server_caps);
1885                 ok = data_blob_append(req, &blob, p, 8);
1886                 if (!ok) {
1887                         return NT_STATUS_UNSUCCESSFUL;
1888                 }
1889         }
1890
1891         if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1892                 SBVAL(p, 0,
1893                       lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1894                       SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1895                 ok = data_blob_append(req, &blob, p, 8);
1896                 if (!ok) {
1897                         return NT_STATUS_UNSUCCESSFUL;
1898                 }
1899         }
1900
1901         if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1902                 ok = convert_string_talloc(req,
1903                                            CH_UNIX, CH_UTF16LE,
1904                                            "Samba", strlen("Samba"),
1905                                            &model, &modellen);
1906                 if (!ok) {
1907                         return NT_STATUS_UNSUCCESSFUL;
1908                 }
1909
1910                 SIVAL(p, 0, 0);
1911                 SIVAL(p + 4, 0, modellen);
1912                 ok = data_blob_append(req, &blob, p, 8);
1913                 if (!ok) {
1914                         talloc_free(model);
1915                         return NT_STATUS_UNSUCCESSFUL;
1916                 }
1917
1918                 ok = data_blob_append(req, &blob, model, modellen);
1919                 talloc_free(model);
1920                 if (!ok) {
1921                         return NT_STATUS_UNSUCCESSFUL;
1922                 }
1923         }
1924
1925         status = smb2_create_blob_add(out_context_blobs,
1926                                       out_context_blobs,
1927                                       SMB2_CREATE_TAG_AAPL,
1928                                       blob);
1929
1930         return status;
1931 }
1932
1933 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1934                                      const struct smb_filename *smb_fname,
1935                                      struct readdir_attr_data *attr_data)
1936 {
1937         NTSTATUS status = NT_STATUS_OK;
1938         uint32_t date_added;
1939         struct adouble *ad = NULL;
1940         struct fruit_config_data *config = NULL;
1941
1942         SMB_VFS_HANDLE_GET_DATA(handle, config,
1943                                 struct fruit_config_data,
1944                                 return NT_STATUS_UNSUCCESSFUL);
1945
1946
1947         /* Ensure we return a default value in the creation_date field */
1948         RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1949
1950         /*
1951          * Resource fork length
1952          */
1953
1954         if (config->readdir_attr_rsize) {
1955                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1956                             ADOUBLE_RSRC);
1957                 if (ad) {
1958                         attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1959                                 ad, ADEID_RFORK);
1960                         TALLOC_FREE(ad);
1961                 }
1962         }
1963
1964         /*
1965          * FinderInfo
1966          */
1967
1968         if (config->readdir_attr_finder_info) {
1969                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1970                             ADOUBLE_META);
1971                 if (ad) {
1972                         if (S_ISREG(smb_fname->st.st_ex_mode)) {
1973                                 /* finder_type */
1974                                 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1975                                        ad_entry(ad, ADEID_FINDERI), 4);
1976
1977                                 /* finder_creator */
1978                                 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1979                                        ad_entry(ad, ADEID_FINDERI) + 4, 4);
1980                         }
1981
1982                         /* finder_flags */
1983                         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1984                                ad_entry(ad, ADEID_FINDERI) + 8, 2);
1985
1986                         /* finder_ext_flags */
1987                         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1988                                ad_entry(ad, ADEID_FINDERI) + 24, 2);
1989
1990                         /* creation date */
1991                         date_added = convert_time_t_to_uint32_t(
1992                                 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1993                         RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1994
1995                         TALLOC_FREE(ad);
1996                 }
1997         }
1998
1999         TALLOC_FREE(ad);
2000         return status;
2001 }
2002
2003 /* Search MS NFS style ACE with UNIX mode */
2004 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2005                              files_struct *fsp,
2006                              const struct security_descriptor *psd,
2007                              mode_t *pmode,
2008                              bool *pdo_chmod)
2009 {
2010         int i;
2011         struct fruit_config_data *config = NULL;
2012
2013         *pdo_chmod = false;
2014
2015         SMB_VFS_HANDLE_GET_DATA(handle, config,
2016                                 struct fruit_config_data,
2017                                 return NT_STATUS_UNSUCCESSFUL);
2018
2019         if (psd->dacl == NULL || !config->unix_info_enabled) {
2020                 return NT_STATUS_OK;
2021         }
2022
2023         for (i = 0; i < psd->dacl->num_aces; i++) {
2024                 if (dom_sid_compare_domain(
2025                             &global_sid_Unix_NFS_Mode,
2026                             &psd->dacl->aces[i].trustee) == 0) {
2027                         *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2028                         *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2029                         *pdo_chmod = true;
2030
2031                         DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2032                                    fsp_str_dbg(fsp), (unsigned)(*pmode)));
2033                         break;
2034                 }
2035         }
2036
2037         return NT_STATUS_OK;
2038 }
2039
2040 /****************************************************************************
2041  * VFS ops
2042  ****************************************************************************/
2043
2044 static int fruit_connect(vfs_handle_struct *handle,
2045                          const char *service,
2046                          const char *user)
2047 {
2048         int rc;
2049         char *list = NULL, *newlist = NULL;
2050         struct fruit_config_data *config;
2051
2052         DEBUG(10, ("fruit_connect\n"));
2053
2054         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2055         if (rc < 0) {
2056                 return rc;
2057         }
2058
2059         rc = init_fruit_config(handle);
2060         if (rc != 0) {
2061                 return rc;
2062         }
2063
2064         SMB_VFS_HANDLE_GET_DATA(handle, config,
2065                                 struct fruit_config_data, return -1);
2066
2067         if (config->veto_appledouble) {
2068                 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2069
2070                 if (list) {
2071                         if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2072                                 newlist = talloc_asprintf(
2073                                         list,
2074                                         "%s/" ADOUBLE_NAME_PREFIX "*/",
2075                                         list);
2076                                 lp_do_parameter(SNUM(handle->conn),
2077                                                 "veto files",
2078                                                 newlist);
2079                         }
2080                 } else {
2081                         lp_do_parameter(SNUM(handle->conn),
2082                                         "veto files",
2083                                         "/" ADOUBLE_NAME_PREFIX "*/");
2084                 }
2085
2086                 TALLOC_FREE(list);
2087         }
2088
2089         if (config->encoding == FRUIT_ENC_NATIVE) {
2090                 lp_do_parameter(
2091                         SNUM(handle->conn),
2092                         "catia:mappings",
2093                         "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2094                         "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2095                         "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2096                         "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2097                         "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2098                         "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2099                         "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2100                         "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2101                         "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2102                         "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2103                         "0x0d:0xf00d");
2104         }
2105
2106         return rc;
2107 }
2108
2109 static int fruit_open_meta(vfs_handle_struct *handle,
2110                            struct smb_filename *smb_fname,
2111                            files_struct *fsp, int flags, mode_t mode)
2112 {
2113         int rc = 0;
2114         struct fruit_config_data *config = NULL;
2115         struct smb_filename *smb_fname_base = NULL;
2116         int baseflags;
2117         int hostfd = -1;
2118         struct adouble *ad = NULL;
2119
2120         DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
2121
2122         SMB_VFS_HANDLE_GET_DATA(handle, config,
2123                                 struct fruit_config_data, return -1);
2124
2125         if (config->meta == FRUIT_META_STREAM) {
2126                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2127         }
2128
2129         /* Create an smb_filename with stream_name == NULL. */
2130         smb_fname_base = synthetic_smb_fname(talloc_tos(),
2131                                              smb_fname->base_name, NULL, NULL);
2132
2133         if (smb_fname_base == NULL) {
2134                 errno = ENOMEM;
2135                 rc = -1;
2136                 goto exit;
2137         }
2138
2139         /*
2140          * We use baseflags to turn off nasty side-effects when opening the
2141          * underlying file.
2142          */
2143         baseflags = flags;
2144         baseflags &= ~O_TRUNC;
2145         baseflags &= ~O_EXCL;
2146         baseflags &= ~O_CREAT;
2147
2148         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2149                               baseflags, mode);
2150
2151         /*
2152          * It is legit to open a stream on a directory, but the base
2153          * fd has to be read-only.
2154          */
2155         if ((hostfd == -1) && (errno == EISDIR)) {
2156                 baseflags &= ~O_ACCMODE;
2157                 baseflags |= O_RDONLY;
2158                 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2159                                       baseflags, mode);
2160         }
2161
2162         TALLOC_FREE(smb_fname_base);
2163
2164         if (hostfd == -1) {
2165                 rc = -1;
2166                 goto exit;
2167         }
2168
2169         if (flags & (O_CREAT | O_TRUNC)) {
2170                 /*
2171                  * The attribute does not exist or needs to be truncated,
2172                  * create an AppleDouble EA
2173                  */
2174                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2175                              handle, ADOUBLE_META, fsp);
2176                 if (ad == NULL) {
2177                         rc = -1;
2178                         goto exit;
2179                 }
2180
2181                 rc = ad_write(ad, smb_fname->base_name);
2182                 if (rc != 0) {
2183                         rc = -1;
2184                         goto exit;
2185                 }
2186         } else {
2187                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2188                               handle, ADOUBLE_META, fsp);
2189                 if (ad == NULL) {
2190                         rc = -1;
2191                         goto exit;
2192                 }
2193                 if (ad_read(ad, smb_fname->base_name) == -1) {
2194                         rc = -1;
2195                         goto exit;
2196                 }
2197         }
2198
2199 exit:
2200         DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2201         if (rc != 0) {
2202                 int saved_errno = errno;
2203                 if (hostfd >= 0) {
2204                         /*
2205                          * BUGBUGBUG -- we would need to call
2206                          * fd_close_posix here, but we don't have a
2207                          * full fsp yet
2208                          */
2209                         fsp->fh->fd = hostfd;
2210                         SMB_VFS_CLOSE(fsp);
2211                 }
2212                 hostfd = -1;
2213                 errno = saved_errno;
2214         }
2215         return hostfd;
2216 }
2217
2218 static int fruit_open_rsrc(vfs_handle_struct *handle,
2219                            struct smb_filename *smb_fname,
2220                            files_struct *fsp, int flags, mode_t mode)
2221 {
2222         int rc = 0;
2223         struct fruit_config_data *config = NULL;
2224         struct adouble *ad = NULL;
2225         struct smb_filename *smb_fname_base = NULL;
2226         char *adpath = NULL;
2227         int hostfd = -1;
2228
2229         DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2230
2231         SMB_VFS_HANDLE_GET_DATA(handle, config,
2232                                 struct fruit_config_data, return -1);
2233
2234         switch (config->rsrc) {
2235         case FRUIT_RSRC_STREAM:
2236                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2237         case FRUIT_RSRC_XATTR:
2238 #ifdef HAVE_ATTROPEN
2239                 hostfd = attropen(smb_fname->base_name,
2240                                   AFPRESOURCE_EA_NETATALK, flags, mode);
2241                 if (hostfd == -1) {
2242                         return -1;
2243                 }
2244                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2245                              handle, ADOUBLE_RSRC, fsp);
2246                 if (ad == NULL) {
2247                         rc = -1;
2248                         goto exit;
2249                 }
2250                 goto exit;
2251 #else
2252                 errno = ENOTSUP;
2253                 return -1;
2254 #endif
2255         default:
2256                 break;
2257         }
2258
2259         if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2260                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2261                 if (rc != 0) {
2262                         rc = -1;
2263                         goto exit;
2264                 }
2265         }
2266
2267         if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2268                 /* sorry, but directories don't habe a resource fork */
2269                 rc = -1;
2270                 goto exit;
2271         }
2272
2273         rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2274         if (rc != 0) {
2275                 goto exit;
2276         }
2277
2278         /* Create an smb_filename with stream_name == NULL. */
2279         smb_fname_base = synthetic_smb_fname(talloc_tos(),
2280                                              adpath, NULL, NULL);
2281         if (smb_fname_base == NULL) {
2282                 errno = ENOMEM;
2283                 rc = -1;
2284                 goto exit;
2285         }
2286
2287         /* Sanitize flags */
2288         if (flags & O_WRONLY) {
2289                 /* We always need read access for the metadata header too */
2290                 flags &= ~O_WRONLY;
2291                 flags |= O_RDWR;
2292         }
2293
2294         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2295                               flags, mode);
2296         if (hostfd == -1) {
2297                 rc = -1;
2298                 goto exit;
2299         }
2300
2301         /* REVIEW: we need this in ad_write() */
2302         fsp->fh->fd = hostfd;
2303
2304         if (flags & (O_CREAT | O_TRUNC)) {
2305                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2306                              handle, ADOUBLE_RSRC, fsp);
2307                 if (ad == NULL) {
2308                         rc = -1;
2309                         goto exit;
2310                 }
2311                 rc = ad_write(ad, smb_fname->base_name);
2312                 if (rc != 0) {
2313                         rc = -1;
2314                         goto exit;
2315                 }
2316         } else {
2317                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2318                               handle, ADOUBLE_RSRC, fsp);
2319                 if (ad == NULL) {
2320                         rc = -1;
2321                         goto exit;
2322                 }
2323                 if (ad_read(ad, smb_fname->base_name) == -1) {
2324                         rc = -1;
2325                         goto exit;
2326                 }
2327         }
2328
2329 exit:
2330
2331         TALLOC_FREE(adpath);
2332         TALLOC_FREE(smb_fname_base);
2333
2334         DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2335         if (rc != 0) {
2336                 int saved_errno = errno;
2337                 if (hostfd >= 0) {
2338                         /*
2339                          * BUGBUGBUG -- we would need to call
2340                          * fd_close_posix here, but we don't have a
2341                          * full fsp yet
2342                          */
2343                         fsp->fh->fd = hostfd;
2344                         SMB_VFS_CLOSE(fsp);
2345                 }
2346                 hostfd = -1;
2347                 errno = saved_errno;
2348         }
2349         return hostfd;
2350 }
2351
2352 static int fruit_open(vfs_handle_struct *handle,
2353                       struct smb_filename *smb_fname,
2354                       files_struct *fsp, int flags, mode_t mode)
2355 {
2356         DEBUG(10, ("fruit_open called for %s\n",
2357                    smb_fname_str_dbg(smb_fname)));
2358
2359         if (!is_ntfs_stream_smb_fname(smb_fname)) {
2360                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2361         }
2362
2363         if (is_afpinfo_stream(smb_fname)) {
2364                 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2365         } else if (is_afpresource_stream(smb_fname)) {
2366                 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2367         }
2368
2369         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2370 }
2371
2372 static int fruit_rename(struct vfs_handle_struct *handle,
2373                         const struct smb_filename *smb_fname_src,
2374                         const struct smb_filename *smb_fname_dst)
2375 {
2376         int rc = -1;
2377         char *src_adouble_path = NULL;
2378         char *dst_adouble_path = NULL;
2379         struct fruit_config_data *config = NULL;
2380
2381         rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2382
2383         if (!VALID_STAT(smb_fname_src->st)
2384             || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2385                 return rc;
2386         }
2387
2388         SMB_VFS_HANDLE_GET_DATA(handle, config,
2389                                 struct fruit_config_data, return -1);
2390
2391         if (config->rsrc == FRUIT_RSRC_XATTR) {
2392                 return rc;
2393         }
2394
2395         rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2396                           &src_adouble_path);
2397         if (rc != 0) {
2398                 goto done;
2399         }
2400         rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2401                           &dst_adouble_path);
2402         if (rc != 0) {
2403                 goto done;
2404         }
2405
2406         DEBUG(10, ("fruit_rename: %s -> %s\n",
2407                    src_adouble_path, dst_adouble_path));
2408
2409         rc = rename(src_adouble_path, dst_adouble_path);
2410         if (errno == ENOENT) {
2411                 rc = 0;
2412         }
2413
2414         TALLOC_FREE(src_adouble_path);
2415         TALLOC_FREE(dst_adouble_path);
2416
2417 done:
2418         return rc;
2419 }
2420
2421 static int fruit_unlink(vfs_handle_struct *handle,
2422                         const struct smb_filename *smb_fname)
2423 {
2424         int rc = -1;
2425         struct fruit_config_data *config = NULL;
2426
2427         SMB_VFS_HANDLE_GET_DATA(handle, config,
2428                                 struct fruit_config_data, return -1);
2429
2430         if (!is_ntfs_stream_smb_fname(smb_fname)) {
2431                 char *adp = NULL;
2432
2433                 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2434                 if (rc != 0) {
2435                         return -1;
2436                 }
2437
2438                 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2439                         return 0;
2440                 }
2441
2442                 /*
2443                  * 0 byte resource fork streams are not listed by
2444                  * vfs_streaminfo, as a result stream cleanup/deletion of file
2445                  * deletion doesn't remove the resourcefork stream.
2446                  */
2447                 rc = adouble_path(talloc_tos(),
2448                                   smb_fname->base_name, &adp);
2449                 if (rc != 0) {
2450                         return -1;
2451                 }
2452
2453                 /* FIXME: direct unlink(), missing smb_fname */
2454                 DEBUG(1,("fruit_unlink: %s\n", adp));
2455                 rc = unlink(adp);
2456                 if ((rc == -1) && (errno == ENOENT)) {
2457                         rc = 0;
2458                 }
2459
2460                 TALLOC_FREE(adp);
2461                 return 0;
2462         }
2463
2464         if (is_afpinfo_stream(smb_fname)) {
2465                 if (config->meta == FRUIT_META_STREAM) {
2466                         rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2467                 } else {
2468                         rc = SMB_VFS_REMOVEXATTR(handle->conn,
2469                                                  smb_fname->base_name,
2470                                                  AFPINFO_EA_NETATALK);
2471                 }
2472
2473                 return rc;
2474         }
2475
2476         if (is_afpresource_stream(smb_fname)) {
2477                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2478                         char *adp = NULL;
2479
2480                         rc = adouble_path(talloc_tos(),
2481                                           smb_fname->base_name, &adp);
2482                         if (rc != 0) {
2483                                 return -1;
2484                         }
2485                         /* FIXME: direct unlink(), missing smb_fname */
2486                         rc = unlink(adp);
2487                         if ((rc == -1) && (errno == ENOENT)) {
2488                                 rc = 0;
2489                         }
2490                         TALLOC_FREE(adp);
2491                 } else {
2492                         rc = SMB_VFS_REMOVEXATTR(handle->conn,
2493                                                  smb_fname->base_name,
2494                                                  AFPRESOURCE_EA_NETATALK);
2495                 }
2496
2497                 return rc;
2498         }
2499
2500         return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2501
2502
2503         return 0;
2504 }
2505
2506 static int fruit_chmod(vfs_handle_struct *handle,
2507                        const char *path,
2508                        mode_t mode)
2509 {
2510         int rc = -1;
2511         char *adp = NULL;
2512         struct fruit_config_data *config = NULL;
2513         SMB_STRUCT_STAT sb;
2514
2515         rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2516         if (rc != 0) {
2517                 return rc;
2518         }
2519
2520         SMB_VFS_HANDLE_GET_DATA(handle, config,
2521                                 struct fruit_config_data, return -1);
2522
2523         if (config->rsrc == FRUIT_RSRC_XATTR) {
2524                 return 0;
2525         }
2526
2527         /* FIXME: direct sys_lstat(), missing smb_fname */
2528         rc = sys_lstat(path, &sb, false);
2529         if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2530                 return rc;
2531         }
2532
2533         rc = adouble_path(talloc_tos(), path, &adp);
2534         if (rc != 0) {
2535                 return -1;
2536         }
2537
2538         DEBUG(10, ("fruit_chmod: %s\n", adp));
2539
2540         rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2541         if (errno == ENOENT) {
2542                 rc = 0;
2543         }
2544
2545         TALLOC_FREE(adp);
2546         return rc;
2547 }
2548
2549 static int fruit_chown(vfs_handle_struct *handle,
2550                        const char *path,
2551                        uid_t uid,
2552                        gid_t gid)
2553 {
2554         int rc = -1;
2555         char *adp = NULL;
2556         struct fruit_config_data *config = NULL;
2557         SMB_STRUCT_STAT sb;
2558
2559         rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2560         if (rc != 0) {
2561                 return rc;
2562         }
2563
2564         SMB_VFS_HANDLE_GET_DATA(handle, config,
2565                                 struct fruit_config_data, return -1);
2566
2567         if (config->rsrc == FRUIT_RSRC_XATTR) {
2568                 return rc;
2569         }
2570
2571         /* FIXME: direct sys_lstat(), missing smb_fname */
2572         rc = sys_lstat(path, &sb, false);
2573         if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2574                 return rc;
2575         }
2576
2577         rc = adouble_path(talloc_tos(), path, &adp);
2578         if (rc != 0) {
2579                 goto done;
2580         }
2581
2582         DEBUG(10, ("fruit_chown: %s\n", adp));
2583
2584         rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2585         if (errno == ENOENT) {
2586                 rc = 0;
2587         }
2588
2589  done:
2590         TALLOC_FREE(adp);
2591         return rc;
2592 }
2593
2594 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2595 {
2596         DIR *dh = NULL;
2597         struct dirent *de;
2598         struct fruit_config_data *config;
2599
2600         SMB_VFS_HANDLE_GET_DATA(handle, config,
2601                                 struct fruit_config_data, return -1);
2602
2603         if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2604                 goto exit_rmdir;
2605         }
2606
2607         /*
2608          * Due to there is no way to change bDeleteVetoFiles variable
2609          * from this module, need to clean up ourselves
2610          */
2611         dh = opendir(path);
2612         if (dh == NULL) {
2613                 goto exit_rmdir;
2614         }
2615
2616         while ((de = readdir(dh)) != NULL) {
2617                 if ((strncmp(de->d_name,
2618                              ADOUBLE_NAME_PREFIX,
2619                              strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2620                         char *p = talloc_asprintf(talloc_tos(),
2621                                                   "%s/%s",
2622                                                   path, de->d_name);
2623                         if (p == NULL) {
2624                                 goto exit_rmdir;
2625                         }
2626                         DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2627                         (void)unlink(p);
2628                         TALLOC_FREE(p);
2629                 }
2630         }
2631
2632 exit_rmdir:
2633         if (dh) {
2634                 closedir(dh);
2635         }
2636         return SMB_VFS_NEXT_RMDIR(handle, path);
2637 }
2638
2639 static ssize_t fruit_pread(vfs_handle_struct *handle,
2640                            files_struct *fsp, void *data,
2641                            size_t n, off_t offset)
2642 {
2643         int rc = 0;
2644         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2645                 handle, fsp);
2646         struct fruit_config_data *config = NULL;
2647         AfpInfo *ai = NULL;
2648         ssize_t len;
2649         char *name = NULL;
2650         char *tmp_base_name = NULL;
2651         NTSTATUS status;
2652
2653         DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2654
2655         if (!fsp->base_fsp) {
2656                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2657         }
2658
2659         SMB_VFS_HANDLE_GET_DATA(handle, config,
2660                                 struct fruit_config_data, return -1);
2661
2662         /* fsp_name is not converted with vfs_catia */
2663         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2664         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2665                                         fsp->base_fsp->fsp_name->base_name,
2666                                         vfs_translate_to_unix,
2667                                         talloc_tos(), &name);
2668         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2669                 name = talloc_strdup(talloc_tos(), tmp_base_name);
2670                 if (name == NULL) {
2671                         rc = -1;
2672                         goto exit;
2673                 }
2674         } else if (!NT_STATUS_IS_OK(status)) {
2675                 errno = map_errno_from_nt_status(status);
2676                 rc = -1;
2677                 goto exit;
2678         }
2679         fsp->base_fsp->fsp_name->base_name = name;
2680
2681         if (ad == NULL) {
2682                 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2683                 if (len == -1) {
2684                         rc = -1;
2685                         goto exit;
2686                 }
2687                 goto exit;
2688         }
2689
2690         if (!fruit_fsp_recheck(ad, fsp)) {
2691                 rc = -1;
2692                 goto exit;
2693         }
2694
2695         if (ad->ad_type == ADOUBLE_META) {
2696                 char afpinfo_buf[AFP_INFO_SIZE];
2697                 size_t to_return;
2698
2699                 if ((offset < 0) || (offset >= AFP_INFO_SIZE)) {
2700                         len = 0;
2701                         rc = 0;
2702                         goto exit;
2703                 }
2704
2705                 to_return = AFP_INFO_SIZE - offset;
2706
2707                 ai = afpinfo_new(talloc_tos());
2708                 if (ai == NULL) {
2709                         rc = -1;
2710                         goto exit;
2711                 }
2712
2713                 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2714                 if (len == -1) {
2715                         rc = -1;
2716                         goto exit;
2717                 }
2718
2719                 memcpy(&ai->afpi_FinderInfo[0],
2720                        ad_entry(ad, ADEID_FINDERI),
2721                        ADEDLEN_FINDERI);
2722                 len = afpinfo_pack(ai, afpinfo_buf);
2723                 if (len != AFP_INFO_SIZE) {
2724                         rc = -1;
2725                         goto exit;
2726                 }
2727
2728                 memcpy(data, afpinfo_buf + offset, to_return);
2729                 len = to_return;
2730         } else {
2731                 len = SMB_VFS_NEXT_PREAD(
2732                         handle, fsp, data, n,
2733                         offset + ad_getentryoff(ad, ADEID_RFORK));
2734                 if (len == -1) {
2735                         rc = -1;
2736                         goto exit;
2737                 }
2738         }
2739 exit:
2740         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2741         TALLOC_FREE(name);
2742         TALLOC_FREE(ai);
2743         if (rc != 0) {
2744                 len = -1;
2745         }
2746         DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2747         return len;
2748 }
2749
2750 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2751                             files_struct *fsp, const void *data,
2752                             size_t n, off_t offset)
2753 {
2754         int rc = 0;
2755         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2756                 handle, fsp);
2757         struct fruit_config_data *config = NULL;
2758         AfpInfo *ai = NULL;
2759         ssize_t len;
2760         char *name = NULL;
2761         char *tmp_base_name = NULL;
2762         NTSTATUS status;
2763
2764         DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2765
2766         if (!fsp->base_fsp) {
2767                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2768         }
2769
2770         SMB_VFS_HANDLE_GET_DATA(handle, config,
2771                                 struct fruit_config_data, return -1);
2772
2773         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2774         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2775                                         fsp->base_fsp->fsp_name->base_name,
2776                                         vfs_translate_to_unix,
2777                                         talloc_tos(), &name);
2778         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2779                 name = talloc_strdup(talloc_tos(), tmp_base_name);
2780                 if (name == NULL) {
2781                         rc = -1;
2782                         goto exit;
2783                 }
2784         } else if (!NT_STATUS_IS_OK(status)) {
2785                 errno = map_errno_from_nt_status(status);
2786                 rc = -1;
2787                 goto exit;
2788         }
2789         fsp->base_fsp->fsp_name->base_name = name;
2790
2791         if (ad == NULL) {
2792                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2793                 if (len != n) {
2794                         rc = -1;
2795                         goto exit;
2796                 }
2797                 goto exit;
2798         }
2799
2800         if (!fruit_fsp_recheck(ad, fsp)) {
2801                 rc = -1;
2802                 goto exit;
2803         }
2804
2805         if (ad->ad_type == ADOUBLE_META) {
2806                 if (n != AFP_INFO_SIZE || offset != 0) {
2807                         DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2808                                   (intmax_t)offset, (intmax_t)n));
2809                         rc = -1;
2810                         goto exit;
2811                 }
2812                 ai = afpinfo_unpack(talloc_tos(), data);
2813                 if (ai == NULL) {
2814                         rc = -1;
2815                         goto exit;
2816                 }
2817                 memcpy(ad_entry(ad, ADEID_FINDERI),
2818                        &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2819                 rc = ad_write(ad, name);
2820         } else {
2821                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2822                                    offset + ad_getentryoff(ad, ADEID_RFORK));
2823                 if (len != n) {
2824                         rc = -1;
2825                         goto exit;
2826                 }
2827
2828                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2829                         rc = ad_read(ad, name);
2830                         if (rc == -1) {
2831                                 goto exit;
2832                         }
2833                         rc = 0;
2834
2835                         if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2836                                 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2837                                 rc = ad_write(ad, name);
2838                         }
2839                 }
2840         }
2841
2842 exit:
2843         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2844         TALLOC_FREE(name);
2845         TALLOC_FREE(ai);
2846         if (rc != 0) {
2847                 return -1;
2848         }
2849         return n;
2850 }
2851
2852 /**
2853  * Helper to stat/lstat the base file of an smb_fname.
2854  */
2855 static int fruit_stat_base(vfs_handle_struct *handle,
2856                            struct smb_filename *smb_fname,
2857                            bool follow_links)
2858 {
2859         char *tmp_stream_name;
2860         int rc;
2861
2862         tmp_stream_name = smb_fname->stream_name;
2863         smb_fname->stream_name = NULL;
2864         if (follow_links) {
2865                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2866         } else {
2867                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2868         }
2869         smb_fname->stream_name = tmp_stream_name;
2870         return rc;
2871 }
2872
2873 static int fruit_stat_meta(vfs_handle_struct *handle,
2874                            struct smb_filename *smb_fname,
2875                            bool follow_links)
2876 {
2877         /* Populate the stat struct with info from the base file. */
2878         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2879                 return -1;
2880         }
2881         smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2882         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2883                                               smb_fname->stream_name);
2884         return 0;
2885 }
2886
2887 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2888                            struct smb_filename *smb_fname,
2889                            bool follow_links)
2890
2891 {
2892         struct adouble *ad = NULL;
2893
2894         DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2895                    smb_fname_str_dbg(smb_fname)));
2896
2897         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2898         if (ad == NULL) {
2899                 errno = ENOENT;
2900                 return -1;
2901         }
2902
2903         /* Populate the stat struct with info from the base file. */
2904         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2905                 TALLOC_FREE(ad);
2906                 return -1;
2907         }
2908
2909         smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2910         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2911                                               smb_fname->stream_name);
2912         TALLOC_FREE(ad);
2913         return 0;
2914 }
2915
2916 static int fruit_stat(vfs_handle_struct *handle,
2917                       struct smb_filename *smb_fname)
2918 {
2919         int rc = -1;
2920
2921         DEBUG(10, ("fruit_stat called for %s\n",
2922                    smb_fname_str_dbg(smb_fname)));
2923
2924         if (!is_ntfs_stream_smb_fname(smb_fname)
2925             || is_ntfs_default_stream_smb_fname(smb_fname)) {
2926                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2927                 if (rc == 0) {
2928                         update_btime(handle, smb_fname);
2929                 }
2930                 return rc;
2931         }
2932
2933         /*
2934          * Note if lp_posix_paths() is true, we can never
2935          * get here as is_ntfs_stream_smb_fname() is
2936          * always false. So we never need worry about
2937          * not following links here.
2938          */
2939
2940         if (is_afpinfo_stream(smb_fname)) {
2941                 rc = fruit_stat_meta(handle, smb_fname, true);
2942         } else if (is_afpresource_stream(smb_fname)) {
2943                 rc = fruit_stat_rsrc(handle, smb_fname, true);
2944         } else {
2945                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2946         }
2947
2948         if (rc == 0) {
2949                 update_btime(handle, smb_fname);
2950                 smb_fname->st.st_ex_mode &= ~S_IFMT;
2951                 smb_fname->st.st_ex_mode |= S_IFREG;
2952                 smb_fname->st.st_ex_blocks =
2953                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2954         }
2955         return rc;
2956 }
2957
2958 static int fruit_lstat(vfs_handle_struct *handle,
2959                        struct smb_filename *smb_fname)
2960 {
2961         int rc = -1;
2962
2963         DEBUG(10, ("fruit_lstat called for %s\n",
2964                    smb_fname_str_dbg(smb_fname)));
2965
2966         if (!is_ntfs_stream_smb_fname(smb_fname)
2967             || is_ntfs_default_stream_smb_fname(smb_fname)) {
2968                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2969                 if (rc == 0) {
2970                         update_btime(handle, smb_fname);
2971                 }
2972                 return rc;
2973         }
2974
2975         if (is_afpinfo_stream(smb_fname)) {
2976                 rc = fruit_stat_meta(handle, smb_fname, false);
2977         } else if (is_afpresource_stream(smb_fname)) {
2978                 rc = fruit_stat_rsrc(handle, smb_fname, false);
2979         } else {
2980                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2981         }
2982
2983         if (rc == 0) {
2984                 update_btime(handle, smb_fname);
2985                 smb_fname->st.st_ex_mode &= ~S_IFMT;
2986                 smb_fname->st.st_ex_mode |= S_IFREG;
2987                 smb_fname->st.st_ex_blocks =
2988                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2989         }
2990         return rc;
2991 }
2992
2993 static int fruit_fstat_meta(vfs_handle_struct *handle,
2994                             files_struct *fsp,
2995                             SMB_STRUCT_STAT *sbuf)
2996 {
2997         DEBUG(10, ("fruit_fstat_meta called for %s\n",
2998                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2999
3000         /* Populate the stat struct with info from the base file. */
3001         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3002                 return -1;
3003         }
3004         *sbuf = fsp->base_fsp->fsp_name->st;
3005         sbuf->st_ex_size = AFP_INFO_SIZE;
3006         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3007
3008         return 0;
3009 }
3010
3011 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3012                             SMB_STRUCT_STAT *sbuf)
3013 {
3014         struct fruit_config_data *config;
3015         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3016                 handle, fsp);
3017
3018         DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
3019                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3020
3021         SMB_VFS_HANDLE_GET_DATA(handle, config,
3022                                 struct fruit_config_data, return -1);
3023
3024         if (config->rsrc == FRUIT_RSRC_STREAM) {
3025                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3026         }
3027
3028         /* Populate the stat struct with info from the base file. */
3029         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3030                 return -1;
3031         }
3032         *sbuf = fsp->base_fsp->fsp_name->st;
3033         sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3034         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3035
3036         DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3037                    smb_fname_str_dbg(fsp->fsp_name),
3038                    (ssize_t)sbuf->st_ex_size));
3039
3040         return 0;
3041 }
3042
3043 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3044                        SMB_STRUCT_STAT *sbuf)
3045 {
3046         int rc;
3047         char *name = NULL;
3048         char *tmp_base_name = NULL;
3049         NTSTATUS status;
3050         struct adouble *ad = (struct adouble *)
3051                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
3052
3053         DEBUG(10, ("fruit_fstat called for %s\n",
3054                    smb_fname_str_dbg(fsp->fsp_name)));
3055
3056         if (fsp->base_fsp) {
3057                 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3058                 /* fsp_name is not converted with vfs_catia */
3059                 status = SMB_VFS_TRANSLATE_NAME(
3060                         handle->conn,
3061                         fsp->base_fsp->fsp_name->base_name,
3062                         vfs_translate_to_unix,
3063                         talloc_tos(), &name);
3064
3065                 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3066                         name = talloc_strdup(talloc_tos(), tmp_base_name);
3067                         if (name == NULL) {
3068                                 rc = -1;
3069                                 goto exit;
3070                         }
3071                 } else if (!NT_STATUS_IS_OK(status)) {
3072                         errno = map_errno_from_nt_status(status);
3073                         rc = -1;
3074                         goto exit;
3075                 }
3076                 fsp->base_fsp->fsp_name->base_name = name;
3077         }
3078
3079         if (ad == NULL || fsp->base_fsp == NULL) {
3080                 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3081                 goto exit;
3082         }
3083
3084         if (!fruit_fsp_recheck(ad, fsp)) {
3085                 rc = -1;
3086                 goto exit;
3087         }
3088
3089         switch (ad->ad_type) {
3090         case ADOUBLE_META:
3091                 rc = fruit_fstat_meta(handle, fsp, sbuf);
3092                 break;
3093         case ADOUBLE_RSRC:
3094                 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
3095                 break;
3096         default:
3097                 DEBUG(10, ("fruit_fstat %s: bad type\n",
3098                            smb_fname_str_dbg(fsp->fsp_name)));
3099                 rc = -1;
3100                 goto exit;
3101         }
3102
3103         if (rc == 0) {
3104                 sbuf->st_ex_mode &= ~S_IFMT;
3105                 sbuf->st_ex_mode |= S_IFREG;
3106                 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3107         }
3108
3109 exit:
3110         DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3111                    smb_fname_str_dbg(fsp->fsp_name),
3112                    (ssize_t)sbuf->st_ex_size));
3113         if (tmp_base_name) {
3114                 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3115         }
3116         TALLOC_FREE(name);
3117         return rc;
3118 }
3119
3120 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3121                                  struct files_struct *fsp,
3122                                  const char *fname,
3123                                  TALLOC_CTX *mem_ctx,
3124                                  unsigned int *pnum_streams,
3125                                  struct stream_struct **pstreams)
3126 {
3127         struct fruit_config_data *config = NULL;
3128         struct smb_filename *smb_fname = NULL;
3129         struct adouble *ad = NULL;
3130         NTSTATUS status;
3131
3132         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3133                                 return NT_STATUS_UNSUCCESSFUL);
3134         DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
3135
3136         smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
3137         if (smb_fname == NULL) {
3138                 return NT_STATUS_NO_MEMORY;
3139         }
3140
3141         if (config->meta == FRUIT_META_NETATALK) {
3142                 ad = ad_get(talloc_tos(), handle,
3143                             smb_fname->base_name, ADOUBLE_META);
3144                 if (ad && !empty_finderinfo(ad)) {
3145                         if (!add_fruit_stream(
3146                                     mem_ctx, pnum_streams, pstreams,
3147                                     AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3148                                     smb_roundup(handle->conn,
3149                                                 AFP_INFO_SIZE))) {
3150                                 TALLOC_FREE(ad);
3151                                 TALLOC_FREE(smb_fname);
3152                                 return NT_STATUS_NO_MEMORY;
3153                         }
3154                 }
3155                 TALLOC_FREE(ad);
3156         }
3157
3158         if (config->rsrc != FRUIT_RSRC_STREAM) {
3159                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3160                             ADOUBLE_RSRC);
3161                 if (ad && (ad_getentrylen(ad, ADEID_RFORK) > 0)) {
3162                         if (!add_fruit_stream(
3163                                     mem_ctx, pnum_streams, pstreams,
3164                                     AFPRESOURCE_STREAM_NAME,
3165                                     ad_getentrylen(ad, ADEID_RFORK),
3166                                     smb_roundup(handle->conn,
3167                                                 ad_getentrylen(
3168                                                         ad, ADEID_RFORK)))) {
3169                                 TALLOC_FREE(ad);
3170                                 TALLOC_FREE(smb_fname);
3171                                 return NT_STATUS_NO_MEMORY;
3172                         }
3173                 }
3174                 TALLOC_FREE(ad);
3175         }
3176
3177         TALLOC_FREE(smb_fname);
3178
3179         status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3180                                          pnum_streams, pstreams);
3181         if (!NT_STATUS_IS_OK(status)) {
3182                 return status;
3183         }
3184
3185         if (config->meta == FRUIT_META_NETATALK) {
3186                 /* Remove the Netatalk xattr from the list */
3187                 if (!del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3188                                       ":" NETATALK_META_XATTR ":$DATA")) {
3189                                 TALLOC_FREE(ad);
3190                                 TALLOC_FREE(smb_fname);
3191                                 return NT_STATUS_NO_MEMORY;
3192                 }
3193         }
3194
3195         return NT_STATUS_OK;
3196 }
3197
3198 static int fruit_ntimes(vfs_handle_struct *handle,
3199                         const struct smb_filename *smb_fname,
3200                         struct smb_file_time *ft)
3201 {
3202         int rc = 0;
3203         struct adouble *ad = NULL;
3204
3205         if (null_timespec(ft->create_time)) {
3206                 goto exit;
3207         }
3208
3209         DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3210                  time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3211
3212         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3213         if (ad == NULL) {
3214                 goto exit;
3215         }
3216
3217         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3218                    convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3219
3220         rc = ad_write(ad, smb_fname->base_name);
3221
3222 exit:
3223
3224         TALLOC_FREE(ad);
3225         if (rc != 0) {
3226                 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3227                 return -1;
3228         }
3229         return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3230 }
3231
3232 static int fruit_fallocate(struct vfs_handle_struct *handle,
3233                            struct files_struct *fsp,
3234                            uint32_t mode,
3235                            off_t offset,
3236                            off_t len)
3237 {
3238         struct adouble *ad =
3239                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3240
3241         if (ad == NULL) {
3242                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3243         }
3244
3245         if (!fruit_fsp_recheck(ad, fsp)) {
3246                 return -1;
3247         }
3248
3249         /* Let the pwrite code path handle it. */
3250         errno = ENOSYS;
3251         return -1;
3252 }
3253
3254 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3255                                 struct files_struct *fsp,
3256                                 off_t offset,
3257                                 struct adouble *ad)
3258 {
3259         /*
3260          * As this request hasn't been seen in the wild,
3261          * the only sensible use I can imagine is the client
3262          * truncating the stream to 0 bytes size.
3263          * We simply remove the metadata on such a request.
3264          */
3265         if (offset != 0) {
3266                 DBG_WARNING("ftruncate %s to %jd",
3267                             fsp_str_dbg(fsp), (intmax_t)offset);
3268                 return -1;
3269         }
3270
3271         return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
3272 }
3273
3274 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3275                                 struct files_struct *fsp,
3276                                 off_t offset,
3277                                 struct adouble *ad)
3278 {
3279         int rc;
3280         struct fruit_config_data *config;
3281
3282         SMB_VFS_HANDLE_GET_DATA(handle, config,
3283                                 struct fruit_config_data, return -1);
3284
3285         if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3286                 return SMB_VFS_FREMOVEXATTR(fsp,
3287                                             AFPRESOURCE_EA_NETATALK);
3288         }
3289
3290         rc = SMB_VFS_NEXT_FTRUNCATE(
3291                 handle, fsp,
3292                 offset + ad_getentryoff(ad, ADEID_RFORK));
3293         if (rc != 0) {
3294                 return -1;
3295         }
3296
3297         if (config->rsrc == FRUIT_RSRC_ADFILE) {
3298                 ad_setentrylen(ad, ADEID_RFORK, offset);
3299                 rc = ad_write(ad, NULL);
3300                 if (rc != 0) {
3301                         return -1;
3302                 }
3303                 DEBUG(10, ("fruit_ftruncate_rsrc file %s offset %jd\n",
3304                            fsp_str_dbg(fsp), (intmax_t)offset));
3305         }
3306
3307         return 0;
3308 }
3309
3310 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3311                            struct files_struct *fsp,
3312                            off_t offset)
3313 {
3314         int rc = 0;
3315         struct adouble *ad =
3316                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3317
3318         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3319                    fsp_str_dbg(fsp), (double)offset));
3320
3321         if (ad == NULL) {
3322                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3323         }
3324
3325         if (!fruit_fsp_recheck(ad, fsp)) {
3326                 return -1;
3327         }
3328
3329         switch (ad->ad_type) {
3330         case ADOUBLE_META:
3331                 rc = fruit_ftruncate_meta(handle, fsp, offset, ad);
3332                 break;
3333
3334         case ADOUBLE_RSRC:
3335                 rc = fruit_ftruncate_rsrc(handle, fsp, offset, ad);
3336                 break;
3337
3338         default:
3339                 return -1;
3340         }
3341
3342         return rc;
3343 }
3344
3345 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3346                                   struct smb_request *req,
3347                                   uint16_t root_dir_fid,
3348                                   struct smb_filename *smb_fname,
3349                                   uint32_t access_mask,
3350                                   uint32_t share_access,
3351                                   uint32_t create_disposition,
3352                                   uint32_t create_options,
3353                                   uint32_t file_attributes,
3354                                   uint32_t oplock_request,
3355                                   struct smb2_lease *lease,
3356                                   uint64_t allocation_size,
3357                                   uint32_t private_flags,
3358                                   struct security_descriptor *sd,
3359                                   struct ea_list *ea_list,
3360                                   files_struct **result,
3361                                   int *pinfo,
3362                                   const struct smb2_create_blobs *in_context_blobs,
3363                                   struct smb2_create_blobs *out_context_blobs)
3364 {
3365         NTSTATUS status;
3366         struct fruit_config_data *config = NULL;
3367         files_struct *fsp = NULL;
3368
3369         status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3370         if (!NT_STATUS_IS_OK(status)) {
3371                 goto fail;
3372         }
3373
3374         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3375                                 return NT_STATUS_UNSUCCESSFUL);
3376
3377         status = SMB_VFS_NEXT_CREATE_FILE(
3378                 handle, req, root_dir_fid, smb_fname,
3379                 access_mask, share_access,
3380                 create_disposition, create_options,
3381                 file_attributes, oplock_request,
3382                 lease,
3383                 allocation_size, private_flags,
3384                 sd, ea_list, result,
3385                 pinfo, in_context_blobs, out_context_blobs);
3386         if (!NT_STATUS_IS_OK(status)) {
3387                 return status;
3388         }
3389         fsp = *result;
3390
3391         if (config->copyfile_enabled) {
3392                 /*
3393                  * Set a flag in the fsp. Gets used in copychunk to
3394                  * check whether the special Apple copyfile semantics
3395                  * for copychunk should be allowed in a copychunk
3396                  * request with a count of 0.
3397                  */
3398                 fsp->aapl_copyfile_supported = true;
3399         }
3400
3401         /*
3402          * If this is a plain open for existing files, opening an 0
3403          * byte size resource fork MUST fail with
3404          * NT_STATUS_OBJECT_NAME_NOT_FOUND.
3405          *
3406          * Cf the vfs_fruit torture tests in test_rfork_create().
3407          */
3408         if (is_afpresource_stream(fsp->fsp_name) &&
3409             create_disposition == FILE_OPEN)
3410         {
3411                 if (fsp->fsp_name->st.st_ex_size == 0) {
3412                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3413                         goto fail;
3414                 }
3415         }
3416
3417         if (is_ntfs_stream_smb_fname(smb_fname)
3418             || fsp->is_directory) {
3419                 return status;
3420         }
3421
3422         if (config->locking == FRUIT_LOCKING_NETATALK) {
3423                 status = fruit_check_access(
3424                         handle, *result,
3425                         access_mask,
3426                         map_share_mode_to_deny_mode(share_access, 0));
3427                 if (!NT_STATUS_IS_OK(status)) {
3428                         goto fail;
3429                 }
3430         }
3431
3432         return status;
3433
3434 fail:
3435         DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
3436
3437         if (fsp) {
3438                 close_file(req, fsp, ERROR_CLOSE);
3439                 *result = fsp = NULL;
3440         }
3441
3442         return status;
3443 }
3444
3445 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3446                                    const struct smb_filename *fname,
3447                                    TALLOC_CTX *mem_ctx,
3448                                    struct readdir_attr_data **pattr_data)
3449 {
3450         struct fruit_config_data *config = NULL;
3451         struct readdir_attr_data *attr_data;
3452         NTSTATUS status;
3453
3454         SMB_VFS_HANDLE_GET_DATA(handle, config,
3455                                 struct fruit_config_data,
3456                                 return NT_STATUS_UNSUCCESSFUL);
3457
3458         if (!config->use_aapl) {
3459                 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3460         }
3461
3462         DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3463
3464         *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3465         if (*pattr_data == NULL) {
3466                 return NT_STATUS_UNSUCCESSFUL;
3467         }
3468         attr_data = *pattr_data;
3469         attr_data->type = RDATTR_AAPL;
3470
3471         /*
3472          * Mac metadata: compressed FinderInfo, resource fork length
3473          * and creation date
3474          */
3475         status = readdir_attr_macmeta(handle, fname, attr_data);
3476         if (!NT_STATUS_IS_OK(status)) {
3477                 /*
3478                  * Error handling is tricky: if we return failure from
3479                  * this function, the corresponding directory entry
3480                  * will to be passed to the client, so we really just
3481                  * want to error out on fatal errors.
3482                  */
3483                 if  (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3484                         goto fail;
3485                 }
3486         }
3487
3488         /*
3489          * UNIX mode
3490          */
3491         if (config->unix_info_enabled) {
3492                 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3493         }
3494
3495         /*
3496          * max_access
3497          */
3498         if (!config->readdir_attr_max_access) {
3499                 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3500         } else {
3501                 status = smbd_calculate_access_mask(
3502                         handle->conn,
3503                         fname,
3504                         false,
3505                         SEC_FLAG_MAXIMUM_ALLOWED,
3506                         &attr_data->attr_data.aapl.max_access);
3507                 if (!NT_STATUS_IS_OK(status)) {
3508                         goto fail;
3509                 }
3510         }
3511
3512         return NT_STATUS_OK;
3513
3514 fail:
3515         DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3516                   fname->base_name, nt_errstr(status)));
3517         TALLOC_FREE(*pattr_data);
3518         return status;
3519 }
3520
3521 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3522                                   files_struct *fsp,
3523                                   uint32_t security_info,
3524                                   TALLOC_CTX *mem_ctx,
3525                                   struct security_descriptor **ppdesc)
3526 {
3527         NTSTATUS status;
3528         struct security_ace ace;
3529         struct dom_sid sid;
3530         struct fruit_config_data *config;
3531
3532         SMB_VFS_HANDLE_GET_DATA(handle, config,
3533                                 struct fruit_config_data,
3534                                 return NT_STATUS_UNSUCCESSFUL);
3535
3536         status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3537                                           mem_ctx, ppdesc);
3538         if (!NT_STATUS_IS_OK(status)) {
3539                 return status;
3540         }
3541
3542         /*
3543          * Add MS NFS style ACEs with uid, gid and mode
3544          */
3545         if (!config->unix_info_enabled) {
3546                 return NT_STATUS_OK;
3547         }
3548
3549         /* MS NFS style mode */
3550         sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3551         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3552         status = security_descriptor_dacl_add(*ppdesc, &ace);
3553         if (!NT_STATUS_IS_OK(status)) {
3554                 DEBUG(1,("failed to add MS NFS style ACE\n"));
3555                 return status;
3556         }
3557
3558         /* MS NFS style uid */
3559         sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3560         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3561         status = security_descriptor_dacl_add(*ppdesc, &ace);
3562         if (!NT_STATUS_IS_OK(status)) {
3563                 DEBUG(1,("failed to add MS NFS style ACE\n"));
3564                 return status;
3565         }
3566
3567         /* MS NFS style gid */
3568         sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3569         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3570         status = security_descriptor_dacl_add(*ppdesc, &ace);
3571         if (!NT_STATUS_IS_OK(status)) {
3572                 DEBUG(1,("failed to add MS NFS style ACE\n"));
3573                 return status;
3574         }
3575
3576         return NT_STATUS_OK;
3577 }
3578
3579 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3580                                   files_struct *fsp,
3581                                   uint32_t security_info_sent,
3582                                   const struct security_descriptor *psd)
3583 {
3584         NTSTATUS status;
3585         bool do_chmod;
3586         mode_t ms_nfs_mode;
3587         int result;
3588
3589         DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp)));
3590
3591         status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3592         if (!NT_STATUS_IS_OK(status)) {
3593                 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3594                 return status;
3595         }
3596
3597         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3598         if (!NT_STATUS_IS_OK(status)) {
3599                 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3600                 return status;
3601         }
3602
3603         if (do_chmod) {
3604                 if (fsp->fh->fd != -1) {
3605                         DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp)));
3606                         result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3607                 } else {
3608                         DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp)));
3609                         result = SMB_VFS_CHMOD(fsp->conn,
3610                                                fsp->fsp_name->base_name,
3611                                                ms_nfs_mode);
3612                 }
3613
3614                 if (result != 0) {
3615                         DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3616                                   result, (unsigned)ms_nfs_mode,
3617                                   strerror(errno)));
3618                         status = map_nt_error_from_unix(errno);
3619                         return status;
3620                 }
3621         }
3622
3623         return NT_STATUS_OK;
3624 }
3625
3626 struct fruit_copy_chunk_state {
3627         struct vfs_handle_struct *handle;
3628         off_t copied;
3629         struct files_struct *src_fsp;
3630         struct files_struct *dst_fsp;
3631         bool is_copyfile;
3632 };
3633
3634 static void fruit_copy_chunk_done(struct tevent_req *subreq);
3635 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
3636                                                 TALLOC_CTX *mem_ctx,
3637                                                 struct tevent_context *ev,
3638                                                 struct files_struct *src_fsp,
3639                                                 off_t src_off,
3640                                                 struct files_struct *dest_fsp,
3641                                                 off_t dest_off,
3642                                                 off_t num)
3643 {
3644         struct tevent_req *req, *subreq;
3645         struct fruit_copy_chunk_state *fruit_copy_chunk_state;
3646         NTSTATUS status;
3647         struct fruit_config_data *config;
3648         off_t to_copy = num;
3649
3650         DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
3651                   (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
3652
3653         SMB_VFS_HANDLE_GET_DATA(handle, config,
3654                                 struct fruit_config_data,
3655                                 return NULL);
3656
3657         req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
3658                                 struct fruit_copy_chunk_state);
3659         if (req == NULL) {
3660                 return NULL;
3661         }
3662         fruit_copy_chunk_state->handle = handle;
3663         fruit_copy_chunk_state->src_fsp = src_fsp;
3664         fruit_copy_chunk_state->dst_fsp = dest_fsp;
3665
3666         /*
3667          * Check if this a OS X copyfile style copychunk request with
3668          * a requested chunk count of 0 that was translated to a
3669          * copy_chunk_send VFS call overloading the parameters src_off
3670          * = dest_off = num = 0.
3671          */
3672         if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
3673             src_fsp->aapl_copyfile_supported &&
3674             dest_fsp->aapl_copyfile_supported)
3675         {
3676                 status = vfs_stat_fsp(src_fsp);
3677                 if (tevent_req_nterror(req, status)) {
3678                         return tevent_req_post(req, ev);
3679                 }
3680
3681                 to_copy = src_fsp->fsp_name->st.st_ex_size;
3682                 fruit_copy_chunk_state->is_copyfile = true;
3683         }
3684
3685         subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
3686                                               mem_ctx,
3687                                               ev,
3688                                               src_fsp,
3689                                               src_off,
3690                                               dest_fsp,
3691                                               dest_off,
3692                                               to_copy);
3693         if (tevent_req_nomem(subreq, req)) {
3694                 return tevent_req_post(req, ev);
3695         }
3696
3697         tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
3698         return req;
3699 }
3700
3701 static void fruit_copy_chunk_done(struct tevent_req *subreq)
3702 {
3703         struct tevent_req *req = tevent_req_callback_data(
3704                 subreq, struct tevent_req);
3705         struct fruit_copy_chunk_state *state = tevent_req_data(
3706                 req, struct fruit_copy_chunk_state);
3707         NTSTATUS status;
3708         unsigned int num_streams = 0;
3709         struct stream_struct *streams = NULL;
3710         int i;
3711         struct smb_filename *src_fname_tmp = NULL;
3712         struct smb_filename *dst_fname_tmp = NULL;
3713
3714         status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
3715                                               subreq,
3716                                               &state->copied);
3717         TALLOC_FREE(subreq);
3718         if (tevent_req_nterror(req, status)) {
3719                 return;
3720         }
3721
3722         if (!state->is_copyfile) {
3723                 tevent_req_done(req);
3724                 return;
3725         }
3726
3727         /*
3728          * Now copy all reamining streams. We know the share supports
3729          * streams, because we're in vfs_fruit. We don't do this async
3730          * because streams are few and small.
3731          */
3732         status = vfs_streaminfo(state->handle->conn, NULL,
3733                                 state->src_fsp->fsp_name->base_name,
3734                                 req, &num_streams, &streams);
3735         if (tevent_req_nterror(req, status)) {
3736                 return;
3737         }
3738
3739         if (num_streams == 1) {
3740                 /* There is always one stream, ::$DATA. */
3741                 tevent_req_done(req);
3742                 return;
3743         }
3744
3745         for (i = 0; i < num_streams; i++) {
3746                 DEBUG(10, ("%s: stream: '%s'/%ju\n",
3747                            __func__, streams[i].name,
3748                            (uintmax_t)streams[i].size));
3749
3750                 src_fname_tmp = synthetic_smb_fname(
3751                         req,
3752                         state->src_fsp->fsp_name->base_name,
3753                         streams[i].name,
3754                         NULL);
3755                 if (tevent_req_nomem(src_fname_tmp, req)) {
3756                         return;
3757                 }
3758
3759                 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
3760                         TALLOC_FREE(src_fname_tmp);
3761                         continue;
3762                 }
3763
3764                 dst_fname_tmp = synthetic_smb_fname(
3765                         req,
3766                         state->dst_fsp->fsp_name->base_name,
3767                         streams[i].name,
3768                         NULL);
3769                 if (tevent_req_nomem(dst_fname_tmp, req)) {
3770                         TALLOC_FREE(src_fname_tmp);
3771                         return;
3772                 }
3773
3774                 status = copy_file(req,
3775                                    state->handle->conn,
3776                                    src_fname_tmp,
3777                                    dst_fname_tmp,
3778                                    OPENX_FILE_CREATE_IF_NOT_EXIST,
3779                                    0, false);
3780                 if (!NT_STATUS_IS_OK(status)) {
3781                         DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
3782                                   smb_fname_str_dbg(src_fname_tmp),
3783                                   smb_fname_str_dbg(dst_fname_tmp),
3784                                   nt_errstr(status)));
3785                         TALLOC_FREE(src_fname_tmp);
3786                         TALLOC_FREE(dst_fname_tmp);
3787                         tevent_req_nterror(req, status);
3788                         return;
3789                 }
3790
3791                 TALLOC_FREE(src_fname_tmp);
3792                 TALLOC_FREE(dst_fname_tmp);
3793         }
3794
3795         TALLOC_FREE(streams);
3796         TALLOC_FREE(src_fname_tmp);
3797         TALLOC_FREE(dst_fname_tmp);
3798         tevent_req_done(req);
3799 }
3800
3801 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
3802                                       struct tevent_req *req,
3803                                       off_t *copied)
3804 {
3805         struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
3806                 req, struct fruit_copy_chunk_state);
3807         NTSTATUS status;
3808
3809         if (tevent_req_is_nterror(req, &status)) {
3810                 DEBUG(1, ("server side copy chunk failed: %s\n",
3811                           nt_errstr(status)));
3812                 *copied = 0;
3813                 tevent_req_received(req);
3814                 return status;
3815         }
3816
3817         *copied = fruit_copy_chunk_state->copied;
3818         tevent_req_received(req);
3819
3820         return NT_STATUS_OK;
3821 }
3822
3823 static struct vfs_fn_pointers vfs_fruit_fns = {
3824         .connect_fn = fruit_connect,
3825
3826         /* File operations */
3827         .chmod_fn = fruit_chmod,
3828         .chown_fn = fruit_chown,
3829         .unlink_fn = fruit_unlink,
3830         .rename_fn = fruit_rename,
3831         .rmdir_fn = fruit_rmdir,
3832         .open_fn = fruit_open,
3833         .pread_fn = fruit_pread,
3834         .pwrite_fn = fruit_pwrite,
3835         .stat_fn = fruit_stat,
3836         .lstat_fn = fruit_lstat,
3837         .fstat_fn = fruit_fstat,
3838         .streaminfo_fn = fruit_streaminfo,
3839         .ntimes_fn = fruit_ntimes,
3840         .ftruncate_fn = fruit_ftruncate,
3841         .fallocate_fn = fruit_fallocate,
3842         .create_file_fn = fruit_create_file,
3843         .readdir_attr_fn = fruit_readdir_attr,
3844         .copy_chunk_send_fn = fruit_copy_chunk_send,
3845         .copy_chunk_recv_fn = fruit_copy_chunk_recv,
3846
3847         /* NT ACL operations */
3848         .fget_nt_acl_fn = fruit_fget_nt_acl,
3849         .fset_nt_acl_fn = fruit_fset_nt_acl,
3850 };
3851
3852 NTSTATUS vfs_fruit_init(void);
3853 NTSTATUS vfs_fruit_init(void)
3854 {
3855         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3856                                         &vfs_fruit_fns);
3857         if (!NT_STATUS_IS_OK(ret)) {
3858                 return ret;
3859         }
3860
3861         vfs_fruit_debug_level = debug_add_class("fruit");
3862         if (vfs_fruit_debug_level == -1) {
3863                 vfs_fruit_debug_level = DBGC_VFS;
3864                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3865                           "vfs_fruit_init"));
3866         } else {
3867                 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3868                            "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3869         }
3870
3871         return ret;
3872 }