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