lib: Move sys_rw* to lib/util
[nivanova/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/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34
35 /*
36  * Enhanced OS X and Netatalk compatibility
37  * ========================================
38  *
39  * This modules takes advantage of vfs_streams_xattr and
40  * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
41  * loaded in the correct order:
42  *
43  *   vfs modules = catia fruit streams_xattr
44  *
45  * The module intercepts the OS X special streams "AFP_AfpInfo" and
46  * "AFP_Resource" and handles them in a special way. All other named
47  * streams are deferred to vfs_streams_xattr.
48  *
49  * The OS X client maps all NTFS illegal characters to the Unicode
50  * private range. This module optionally stores the charcters using
51  * their native ASCII encoding using vfs_catia. If you're not enabling
52  * this feature, you can skip catia from vfs modules.
53  *
54  * Finally, open modes are optionally checked against Netatalk AFP
55  * share modes.
56  *
57  * The "AFP_AfpInfo" named stream is a binary blob containing OS X
58  * extended metadata for files and directories. This module optionally
59  * reads and stores this metadata in a way compatible with Netatalk 3
60  * which stores the metadata in an EA "org.netatalk.metadata". Cf
61  * source3/include/MacExtensions.h for a description of the binary
62  * blobs content.
63  *
64  * The "AFP_Resource" named stream may be arbitrarily large, thus it
65  * can't be stored in an xattr on most filesystem. ZFS on Solaris is
66  * the only available filesystem where xattrs can be of any size and
67  * the OS supports using the file APIs for xattrs.
68  *
69  * The AFP_Resource stream is stored in an AppleDouble file prepending
70  * "._" to the filename. On Solaris with ZFS the stream is optionally
71  * stored in an EA "org.netatalk.ressource".
72  *
73  *
74  * Extended Attributes
75  * ===================
76  *
77  * The OS X SMB client sends xattrs as ADS too. For xattr interop with
78  * other protocols you may want to adjust the xattr names the VFS
79  * module vfs_streams_xattr uses for storing ADS's. This defaults to
80  * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
81  * these module parameters:
82  *
83  *   streams_xattr:prefix = user.
84  *   streams_xattr:store_stream_type = false
85  *
86  *
87  * TODO
88  * ====
89  *
90  * - log diagnostic if any needed VFS module is not loaded
91  *   (eg with lp_vfs_objects())
92  * - add tests
93  */
94
95 static int vfs_fruit_debug_level = DBGC_VFS;
96
97 #undef DBGC_CLASS
98 #define DBGC_CLASS vfs_fruit_debug_level
99
100 #define FRUIT_PARAM_TYPE_NAME "fruit"
101 #define ADOUBLE_NAME_PREFIX "._"
102
103 /*
104  * REVIEW:
105  * This is hokey, but what else can we do?
106  */
107 #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 true;
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                         ok = ad_pack(ad);
953                         if (!ok) {
954                                 rc = -1;
955                                 goto exit;
956                         }
957                         /* FIXME: direct sys_pwrite(), don't have an fsp */
958                         len = sys_pwrite(fd, ad->ad_data,
959                                          AD_DATASZ_DOT_UND, 0);
960                         if (len != AD_DATASZ_DOT_UND) {
961                                 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
962                                 rc = -1;
963                                 goto exit;
964                         }
965
966                         meta_ad = ad_init(talloc_tos(), ad->ad_handle,
967                                           ADOUBLE_META, NULL);
968                         if (meta_ad == NULL) {
969                                 rc = -1;
970                                 goto exit;
971                         }
972
973                         memcpy(ad_entry(meta_ad, ADEID_FINDERI),
974                                ad_entry(ad, ADEID_FINDERI),
975                                ADEDLEN_FINDERI);
976
977                         rc = ad_write(meta_ad, path);
978                         if (rc != 0) {
979                                 rc = -1;
980                                 goto exit;
981                         }
982                 }
983         }
984
985         DEBUG(10, ("opened AppleDouble: %s\n", path));
986
987 exit:
988         if (rc != 0) {
989                 saved_errno = errno;
990                 len = -1;
991         }
992         if (opened && fd != -1) {
993                 close(fd);
994         }
995         TALLOC_FREE(adpath);
996         TALLOC_FREE(meta_ad);
997         if (rc != 0) {
998                 errno = saved_errno;
999         }
1000         return len;
1001 }
1002
1003 /**
1004  * Read and unpack an AppleDouble metadata xattr or resource
1005  **/
1006 static ssize_t ad_read(struct adouble *ad, const char *path)
1007 {
1008         switch (ad->ad_type) {
1009         case ADOUBLE_META:
1010                 return ad_header_read_meta(ad, path);
1011         case ADOUBLE_RSRC:
1012                 return ad_header_read_rsrc(ad, path);
1013         default:
1014                 return -1;
1015         }
1016 }
1017
1018 /**
1019  * Allocate a struct adouble without initialiing it
1020  *
1021  * The struct is either hang of the fsp extension context or if fsp is
1022  * NULL from ctx.
1023  *
1024  * @param[in] ctx        talloc context
1025  * @param[in] handle     vfs handle
1026  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1027
1028  * @param[in] fsp        if not NULL (for stream IO), the adouble handle is
1029  *                       added as an fsp extension
1030  *
1031  * @return               adouble handle
1032  **/
1033 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1034                                 adouble_type_t type, files_struct *fsp)
1035 {
1036         int rc = 0;
1037         size_t adsize = 0;
1038         struct adouble *ad;
1039         struct fruit_config_data *config;
1040
1041         SMB_VFS_HANDLE_GET_DATA(handle, config,
1042                                 struct fruit_config_data, return NULL);
1043
1044         switch (type) {
1045         case ADOUBLE_META:
1046                 adsize = AD_DATASZ_XATTR;
1047                 break;
1048         case ADOUBLE_RSRC:
1049                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1050                         adsize = AD_DATASZ_DOT_UND;
1051                 }
1052                 break;
1053         default:
1054                 return NULL;
1055         }
1056
1057         if (!fsp) {
1058                 ad = talloc_zero(ctx, struct adouble);
1059                 if (ad == NULL) {
1060                         rc = -1;
1061                         goto exit;
1062                 }
1063                 if (adsize) {
1064                         ad->ad_data = talloc_zero_array(ad, char, adsize);
1065                 }
1066         } else {
1067                 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1068                                                              struct adouble,
1069                                                              NULL);
1070                 if (ad == NULL) {
1071                         rc = -1;
1072                         goto exit;
1073                 }
1074                 if (adsize) {
1075                         ad->ad_data = talloc_zero_array(
1076                                 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1077                                 char, adsize);
1078                 }
1079                 ad->ad_fsp = fsp;
1080         }
1081
1082         if (adsize && ad->ad_data == NULL) {
1083                 rc = -1;
1084                 goto exit;
1085         }
1086         ad->ad_handle = handle;
1087         ad->ad_type = type;
1088         ad->ad_magic = AD_MAGIC;
1089         ad->ad_version = AD_VERSION;
1090
1091 exit:
1092         if (rc != 0) {
1093                 TALLOC_FREE(ad);
1094         }
1095         return ad;
1096 }
1097
1098 /**
1099  * Allocate and initialize a new struct adouble
1100  *
1101  * @param[in] ctx        talloc context
1102  * @param[in] handle     vfs handle
1103  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1104  * @param[in] fsp        file handle, may be NULL for a type of e_ad_meta
1105  *
1106  * @return               adouble handle, initialized
1107  **/
1108 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1109                                adouble_type_t type, files_struct *fsp)
1110 {
1111         int rc = 0;
1112         const struct ad_entry_order  *eid;
1113         struct adouble *ad = NULL;
1114         struct fruit_config_data *config;
1115         time_t t = time(NULL);
1116
1117         SMB_VFS_HANDLE_GET_DATA(handle, config,
1118                                 struct fruit_config_data, return NULL);
1119
1120         switch (type) {
1121         case ADOUBLE_META:
1122                 eid = entry_order_meta_xattr;
1123                 break;
1124         case ADOUBLE_RSRC:
1125                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1126                         eid = entry_order_dot_und;
1127                 } else {
1128                         eid = entry_order_rsrc_xattr;
1129                 }
1130                 break;
1131         default:
1132                 return NULL;
1133         }
1134
1135         ad = ad_alloc(ctx, handle, type, fsp);
1136         if (ad == NULL) {
1137                 return NULL;
1138         }
1139
1140         while (eid->id) {
1141                 ad->ad_eid[eid->id].ade_off = eid->offset;
1142                 ad->ad_eid[eid->id].ade_len = eid->len;
1143                 eid++;
1144         }
1145
1146         /* put something sane in the date fields */
1147         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1148         ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1149         ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1150         ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1151
1152         if (rc != 0) {
1153                 TALLOC_FREE(ad);
1154         }
1155         return ad;
1156 }
1157
1158 /**
1159  * Return AppleDouble data for a file
1160  *
1161  * @param[in] ctx      talloc context
1162  * @param[in] handle   vfs handle
1163  * @param[in] path     pathname to file or directory
1164  * @param[in] type     type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1165  *
1166  * @return             talloced struct adouble or NULL on error
1167  **/
1168 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1169                               const char *path, adouble_type_t type)
1170 {
1171         int rc = 0;
1172         ssize_t len;
1173         struct adouble *ad = NULL;
1174
1175         DEBUG(10, ("ad_get(%s) called for %s\n",
1176                    type == ADOUBLE_META ? "meta" : "rsrc", path));
1177
1178         ad = ad_alloc(ctx, handle, type, NULL);
1179         if (ad == NULL) {
1180                 rc = -1;
1181                 goto exit;
1182         }
1183
1184         len = ad_read(ad, path);
1185         if (len == -1) {
1186                 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1187                 rc = -1;
1188                 goto exit;
1189         }
1190
1191 exit:
1192         DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1193                   type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1194
1195         if (rc != 0) {
1196                 TALLOC_FREE(ad);
1197         }
1198         return ad;
1199 }
1200
1201 /**
1202  * Set AppleDouble metadata on a file or directory
1203  *
1204  * @param[in] ad      adouble handle
1205
1206  * @param[in] path    pathname to file or directory, may be NULL for a
1207  *                    resource fork
1208  *
1209  * @return            status code, 0 means success
1210  **/
1211 static int ad_write(struct adouble *ad, const char *path)
1212 {
1213         int rc = 0;
1214         ssize_t len;
1215         bool ok;
1216
1217         ok = ad_pack(ad);
1218         if (!ok) {
1219                 return -1;
1220         }
1221
1222         switch (ad->ad_type) {
1223         case ADOUBLE_META:
1224                 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1225                                       AFPINFO_EA_NETATALK, ad->ad_data,
1226                                       AD_DATASZ_XATTR, 0);
1227                 break;
1228         case ADOUBLE_RSRC:
1229                 if ((ad->ad_fsp == NULL)
1230                     || (ad->ad_fsp->fh == NULL)
1231                     || (ad->ad_fsp->fh->fd == -1)) {
1232                         rc = -1;
1233                         goto exit;
1234                 }
1235                 /* FIXME: direct sys_pwrite(), don't have an fsp */
1236                 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1237                                  talloc_get_size(ad->ad_data), 0);
1238                 if (len != talloc_get_size(ad->ad_data)) {
1239                         DEBUG(1, ("short write on %s: %zd",
1240                                   fsp_str_dbg(ad->ad_fsp), len));
1241                         rc = -1;
1242                         goto exit;
1243                 }
1244                 break;
1245         default:
1246                 return -1;
1247         }
1248 exit:
1249         return rc;
1250 }
1251
1252 /*****************************************************************************
1253  * Helper functions
1254  *****************************************************************************/
1255
1256 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1257 {
1258         if (strncasecmp_m(smb_fname->stream_name,
1259                           AFPINFO_STREAM_NAME,
1260                           strlen(AFPINFO_STREAM_NAME)) == 0) {
1261                 return true;
1262         }
1263         return false;
1264 }
1265
1266 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1267 {
1268         if (strncasecmp_m(smb_fname->stream_name,
1269                           AFPRESOURCE_STREAM_NAME,
1270                           strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1271                 return true;
1272         }
1273         return false;
1274 }
1275
1276 /**
1277  * Test whether stream is an Apple stream, not used atm
1278  **/
1279 #if 0
1280 static bool is_apple_stream(const struct smb_filename *smb_fname)
1281 {
1282         if (is_afpinfo_stream(smb_fname)) {
1283                 return true;
1284         }
1285         if (is_afpresource_stream(smb_fname)) {
1286                 return true;
1287         }
1288         return false;
1289 }
1290 #endif
1291
1292 /**
1293  * Initialize config struct from our smb.conf config parameters
1294  **/
1295 static int init_fruit_config(vfs_handle_struct *handle)
1296 {
1297         struct fruit_config_data *config;
1298         int enumval;
1299
1300         config = talloc_zero(handle->conn, struct fruit_config_data);
1301         if (!config) {
1302                 DEBUG(1, ("talloc_zero() failed\n"));
1303                 errno = ENOMEM;
1304                 return -1;
1305         }
1306
1307         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1308                                "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1309         if (enumval == -1) {
1310                 DEBUG(1, ("value for %s: ressource type unknown\n",
1311                           FRUIT_PARAM_TYPE_NAME));
1312                 return -1;
1313         }
1314         config->rsrc = (enum fruit_rsrc)enumval;
1315
1316         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1317                                "metadata", fruit_meta, FRUIT_META_NETATALK);
1318         if (enumval == -1) {
1319                 DEBUG(1, ("value for %s: metadata type unknown\n",
1320                           FRUIT_PARAM_TYPE_NAME));
1321                 return -1;
1322         }
1323         config->meta = (enum fruit_meta)enumval;
1324
1325         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1326                                "locking", fruit_locking, FRUIT_LOCKING_NONE);
1327         if (enumval == -1) {
1328                 DEBUG(1, ("value for %s: locking type unknown\n",
1329                           FRUIT_PARAM_TYPE_NAME));
1330                 return -1;
1331         }
1332         config->locking = (enum fruit_locking)enumval;
1333
1334         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1335                                "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1336         if (enumval == -1) {
1337                 DEBUG(1, ("value for %s: encoding type unknown\n",
1338                           FRUIT_PARAM_TYPE_NAME));
1339                 return -1;
1340         }
1341         config->encoding = (enum fruit_encoding)enumval;
1342
1343         config->veto_appledouble = lp_parm_bool(
1344                 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1345                 "veto_appledouble", true);
1346
1347         config->use_aapl = lp_parm_bool(
1348                 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1349
1350         config->unix_info_enabled = lp_parm_bool(
1351                 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1352
1353         config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1354                                            "copyfile", false);
1355
1356         config->readdir_attr_rsize = lp_parm_bool(
1357                 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1358
1359         config->readdir_attr_finder_info = lp_parm_bool(
1360                 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1361
1362         config->readdir_attr_max_access = lp_parm_bool(
1363                 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1364
1365         SMB_VFS_HANDLE_SET_DATA(handle, config,
1366                                 NULL, struct fruit_config_data,
1367                                 return -1);
1368
1369         return 0;
1370 }
1371
1372 /**
1373  * Prepend "._" to a basename
1374  **/
1375 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1376 {
1377         char *parent;
1378         const char *base;
1379
1380         if (!parent_dirname(ctx, path_in, &parent, &base)) {
1381                 return -1;
1382         }
1383
1384         *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1385         if (*path_out == NULL) {
1386                 return -1;
1387         }
1388
1389         return 0;
1390 }
1391
1392 /**
1393  * Allocate and initialize an AfpInfo struct
1394  **/
1395 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1396 {
1397         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1398         if (ai == NULL) {
1399                 return NULL;
1400         }
1401         ai->afpi_Signature = AFP_Signature;
1402         ai->afpi_Version = AFP_Version;
1403         ai->afpi_BackupTime = AD_DATE_START;
1404         return ai;
1405 }
1406
1407 /**
1408  * Pack an AfpInfo struct into a buffer
1409  *
1410  * Buffer size must be at least AFP_INFO_SIZE
1411  * Returns size of packed buffer
1412  **/
1413 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1414 {
1415         memset(buf, 0, AFP_INFO_SIZE);
1416
1417         RSIVAL(buf, 0, ai->afpi_Signature);
1418         RSIVAL(buf, 4, ai->afpi_Version);
1419         RSIVAL(buf, 12, ai->afpi_BackupTime);
1420         memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1421
1422         return AFP_INFO_SIZE;
1423 }
1424
1425 /**
1426  * Unpack a buffer into a AfpInfo structure
1427  *
1428  * Buffer size must be at least AFP_INFO_SIZE
1429  * Returns allocated AfpInfo struct
1430  **/
1431 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1432 {
1433         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1434         if (ai == NULL) {
1435                 return NULL;
1436         }
1437
1438         ai->afpi_Signature = RIVAL(data, 0);
1439         ai->afpi_Version = RIVAL(data, 4);
1440         ai->afpi_BackupTime = RIVAL(data, 12);
1441         memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1442                sizeof(ai->afpi_FinderInfo));
1443
1444         if (ai->afpi_Signature != AFP_Signature
1445             || ai->afpi_Version != AFP_Version) {
1446                 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1447                 TALLOC_FREE(ai);
1448         }
1449
1450         return ai;
1451 }
1452
1453 /**
1454  * Fake an inode number from the md5 hash of the (xattr) name
1455  **/
1456 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1457 {
1458         MD5_CTX ctx;
1459         unsigned char hash[16];
1460         SMB_INO_T result;
1461         char *upper_sname;
1462
1463         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1464         SMB_ASSERT(upper_sname != NULL);
1465
1466         MD5Init(&ctx);
1467         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1468                   sizeof(sbuf->st_ex_dev));
1469         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1470                   sizeof(sbuf->st_ex_ino));
1471         MD5Update(&ctx, (unsigned char *)upper_sname,
1472                   talloc_get_size(upper_sname)-1);
1473         MD5Final(hash, &ctx);
1474
1475         TALLOC_FREE(upper_sname);
1476
1477         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1478         memcpy(&result, hash, sizeof(result));
1479
1480         DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1481                    sname, (unsigned long long)result));
1482
1483         return result;
1484 }
1485
1486 /**
1487  * Ensure ad_fsp is still valid
1488  **/
1489 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1490 {
1491         if (ad->ad_fsp == fsp) {
1492                 return true;
1493         }
1494         ad->ad_fsp = fsp;
1495
1496         return true;
1497 }
1498
1499 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1500                              struct stream_struct **streams,
1501                              const char *name, off_t size,
1502                              off_t alloc_size)
1503 {
1504         struct stream_struct *tmp;
1505
1506         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1507                              (*num_streams)+1);
1508         if (tmp == NULL) {
1509                 return false;
1510         }
1511
1512         tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1513         if (tmp[*num_streams].name == NULL) {
1514                 return false;
1515         }
1516
1517         tmp[*num_streams].size = size;
1518         tmp[*num_streams].alloc_size = alloc_size;
1519
1520         *streams = tmp;
1521         *num_streams += 1;
1522         return true;
1523 }
1524
1525 static bool empty_finderinfo(const struct adouble *ad)
1526 {
1527
1528         char emptybuf[ADEDLEN_FINDERI] = {0};
1529         if (memcmp(emptybuf,
1530                    ad_entry(ad, ADEID_FINDERI),
1531                    ADEDLEN_FINDERI) == 0) {
1532                 return true;
1533         }
1534         return false;
1535 }
1536
1537 /**
1538  * Update btime with btime from Netatalk
1539  **/
1540 static void update_btime(vfs_handle_struct *handle,
1541                          struct smb_filename *smb_fname)
1542 {
1543         uint32_t t;
1544         struct timespec creation_time = {0};
1545         struct adouble *ad;
1546
1547         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1548         if (ad == NULL) {
1549                 return;
1550         }
1551         if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1552                 TALLOC_FREE(ad);
1553                 return;
1554         }
1555         TALLOC_FREE(ad);
1556
1557         creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1558         update_stat_ex_create_time(&smb_fname->st, creation_time);
1559
1560         return;
1561 }
1562
1563 /**
1564  * Map an access mask to a Netatalk single byte byte range lock
1565  **/
1566 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1567                                     uint32_t access_mask)
1568 {
1569         off_t offset;
1570
1571         switch (access_mask) {
1572         case FILE_READ_DATA:
1573                 offset = AD_FILELOCK_OPEN_RD;
1574                 break;
1575
1576         case FILE_WRITE_DATA:
1577         case FILE_APPEND_DATA:
1578                 offset = AD_FILELOCK_OPEN_WR;
1579                 break;
1580
1581         default:
1582                 offset = AD_FILELOCK_OPEN_NONE;
1583                 break;
1584         }
1585
1586         if (fork_type == APPLE_FORK_RSRC) {
1587                 if (offset == AD_FILELOCK_OPEN_NONE) {
1588                         offset = AD_FILELOCK_RSRC_OPEN_NONE;
1589                 } else {
1590                         offset += 2;
1591                 }
1592         }
1593
1594         return offset;
1595 }
1596
1597 /**
1598  * Map a deny mode to a Netatalk brl
1599  **/
1600 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1601                                       uint32_t deny_mode)
1602 {
1603         off_t offset;
1604
1605         switch (deny_mode) {
1606         case DENY_READ:
1607                 offset = AD_FILELOCK_DENY_RD;
1608                 break;
1609
1610         case DENY_WRITE:
1611                 offset = AD_FILELOCK_DENY_WR;
1612                 break;
1613
1614         default:
1615                 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1616         }
1617
1618         if (fork_type == APPLE_FORK_RSRC) {
1619                 offset += 2;
1620         }
1621
1622         return offset;
1623 }
1624
1625 /**
1626  * Call fcntl() with an exclusive F_GETLK request in order to
1627  * determine if there's an exisiting shared lock
1628  *
1629  * @return true if the requested lock was found or any error occured
1630  *         false if the lock was not found
1631  **/
1632 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1633 {
1634         bool result;
1635         off_t offset = in_offset;
1636         off_t len = 1;
1637         int type = F_WRLCK;
1638         pid_t pid;
1639
1640         result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1641         if (result == false) {
1642                 return true;
1643         }
1644
1645         if (type != F_UNLCK) {
1646                 return true;
1647         }
1648
1649         return false;
1650 }
1651
1652 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1653                                    files_struct *fsp,
1654                                    uint32_t access_mask,
1655                                    uint32_t deny_mode)
1656 {
1657         NTSTATUS status = NT_STATUS_OK;
1658         struct byte_range_lock *br_lck = NULL;
1659         bool open_for_reading, open_for_writing, deny_read, deny_write;
1660         off_t off;
1661
1662         /* FIXME: hardcoded data fork, add resource fork */
1663         enum apple_fork fork_type = APPLE_FORK_DATA;
1664
1665         DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1666                   fsp_str_dbg(fsp),
1667                   access_mask & FILE_READ_DATA ? "READ" :"-",
1668                   access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1669                   deny_mode & DENY_READ ? "DENY_READ" : "-",
1670                   deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1671
1672         /*
1673          * Check read access and deny read mode
1674          */
1675         if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1676                 /* Check access */
1677                 open_for_reading = test_netatalk_lock(
1678                         fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1679
1680                 deny_read = test_netatalk_lock(
1681                         fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1682
1683                 DEBUG(10, ("read: %s, deny_write: %s\n",
1684                           open_for_reading == true ? "yes" : "no",
1685                           deny_read == true ? "yes" : "no"));
1686
1687                 if (((access_mask & FILE_READ_DATA) && deny_read)
1688                     || ((deny_mode & DENY_READ) && open_for_reading)) {
1689                         return NT_STATUS_SHARING_VIOLATION;
1690                 }
1691
1692                 /* Set locks */
1693                 if (access_mask & FILE_READ_DATA) {
1694                         off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1695                         br_lck = do_lock(
1696                                 handle->conn->sconn->msg_ctx, fsp,
1697                                 fsp->op->global->open_persistent_id, 1, off,
1698                                 READ_LOCK, POSIX_LOCK, false,
1699                                 &status, NULL);
1700
1701                         if (!NT_STATUS_IS_OK(status))  {
1702                                 return status;
1703                         }
1704                         TALLOC_FREE(br_lck);
1705                 }
1706
1707                 if (deny_mode & DENY_READ) {
1708                         off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1709                         br_lck = do_lock(
1710                                 handle->conn->sconn->msg_ctx, fsp,
1711                                 fsp->op->global->open_persistent_id, 1, off,
1712                                 READ_LOCK, POSIX_LOCK, false,
1713                                 &status, NULL);
1714
1715                         if (!NT_STATUS_IS_OK(status)) {
1716                                 return status;
1717                         }
1718                         TALLOC_FREE(br_lck);
1719                 }
1720         }
1721
1722         /*
1723          * Check write access and deny write mode
1724          */
1725         if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1726                 /* Check access */
1727                 open_for_writing = test_netatalk_lock(
1728                         fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1729
1730                 deny_write = test_netatalk_lock(
1731                         fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1732
1733                 DEBUG(10, ("write: %s, deny_write: %s\n",
1734                           open_for_writing == true ? "yes" : "no",
1735                           deny_write == true ? "yes" : "no"));
1736
1737                 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1738                     || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1739                         return NT_STATUS_SHARING_VIOLATION;
1740                 }
1741
1742                 /* Set locks */
1743                 if (access_mask & FILE_WRITE_DATA) {
1744                         off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1745                         br_lck = do_lock(
1746                                 handle->conn->sconn->msg_ctx, fsp,
1747                                 fsp->op->global->open_persistent_id, 1, off,
1748                                 READ_LOCK, POSIX_LOCK, false,
1749                                 &status, NULL);
1750
1751                         if (!NT_STATUS_IS_OK(status)) {
1752                                 return status;
1753                         }
1754                         TALLOC_FREE(br_lck);
1755
1756                 }
1757                 if (deny_mode & DENY_WRITE) {
1758                         off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1759                         br_lck = do_lock(
1760                                 handle->conn->sconn->msg_ctx, fsp,
1761                                 fsp->op->global->open_persistent_id, 1, off,
1762                                 READ_LOCK, POSIX_LOCK, false,
1763                                 &status, NULL);
1764
1765                         if (!NT_STATUS_IS_OK(status)) {
1766                                 return status;
1767                         }
1768                         TALLOC_FREE(br_lck);
1769                 }
1770         }
1771
1772         TALLOC_FREE(br_lck);
1773
1774         return status;
1775 }
1776
1777 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1778                            struct smb_request *req,
1779                            const struct smb2_create_blobs *in_context_blobs,
1780                            struct smb2_create_blobs *out_context_blobs)
1781 {
1782         struct fruit_config_data *config;
1783         NTSTATUS status;
1784         struct smb2_create_blob *aapl = NULL;
1785         uint32_t cmd;
1786         bool ok;
1787         uint8_t p[16];
1788         DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1789         uint64_t req_bitmap, client_caps;
1790         uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1791         smb_ucs2_t *model;
1792         size_t modellen;
1793
1794         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1795                                 return NT_STATUS_UNSUCCESSFUL);
1796
1797         if (!config->use_aapl
1798             || in_context_blobs == NULL
1799             || out_context_blobs == NULL) {
1800                 return NT_STATUS_OK;
1801         }
1802
1803         aapl = smb2_create_blob_find(in_context_blobs,
1804                                      SMB2_CREATE_TAG_AAPL);
1805         if (aapl == NULL) {
1806                 return NT_STATUS_OK;
1807         }
1808
1809         if (aapl->data.length != 24) {
1810                 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1811                           (uintmax_t)aapl->data.length));
1812                 return NT_STATUS_INVALID_PARAMETER;
1813         }
1814
1815         cmd = IVAL(aapl->data.data, 0);
1816         if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1817                 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1818                 return NT_STATUS_INVALID_PARAMETER;
1819         }
1820
1821         req_bitmap = BVAL(aapl->data.data, 8);
1822         client_caps = BVAL(aapl->data.data, 16);
1823
1824         SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1825         SIVAL(p, 4, 0);
1826         SBVAL(p, 8, req_bitmap);
1827         ok = data_blob_append(req, &blob, p, 16);
1828         if (!ok) {
1829                 return NT_STATUS_UNSUCCESSFUL;
1830         }
1831
1832         if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1833                 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1834                     (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1835                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1836                         config->readdir_attr_enabled = true;
1837                 }
1838
1839                 if (config->use_copyfile) {
1840                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
1841                         config->copyfile_enabled = true;
1842                 }
1843
1844                 /*
1845                  * The client doesn't set the flag, so we can't check
1846                  * for it and just set it unconditionally
1847                  */
1848                 if (config->unix_info_enabled) {
1849                         server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1850                 }
1851
1852                 SBVAL(p, 0, server_caps);
1853                 ok = data_blob_append(req, &blob, p, 8);
1854                 if (!ok) {
1855                         return NT_STATUS_UNSUCCESSFUL;
1856                 }
1857         }
1858
1859         if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1860                 SBVAL(p, 0,
1861                       lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1862                       SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1863                 ok = data_blob_append(req, &blob, p, 8);
1864                 if (!ok) {
1865                         return NT_STATUS_UNSUCCESSFUL;
1866                 }
1867         }
1868
1869         if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1870                 ok = convert_string_talloc(req,
1871                                            CH_UNIX, CH_UTF16LE,
1872                                            "Samba", strlen("Samba"),
1873                                            &model, &modellen);
1874                 if (!ok) {
1875                         return NT_STATUS_UNSUCCESSFUL;
1876                 }
1877
1878                 SIVAL(p, 0, 0);
1879                 SIVAL(p + 4, 0, modellen);
1880                 ok = data_blob_append(req, &blob, p, 8);
1881                 if (!ok) {
1882                         talloc_free(model);
1883                         return NT_STATUS_UNSUCCESSFUL;
1884                 }
1885
1886                 ok = data_blob_append(req, &blob, model, modellen);
1887                 talloc_free(model);
1888                 if (!ok) {
1889                         return NT_STATUS_UNSUCCESSFUL;
1890                 }
1891         }
1892
1893         status = smb2_create_blob_add(out_context_blobs,
1894                                       out_context_blobs,
1895                                       SMB2_CREATE_TAG_AAPL,
1896                                       blob);
1897
1898         return status;
1899 }
1900
1901 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1902                                      const struct smb_filename *smb_fname,
1903                                      struct readdir_attr_data *attr_data)
1904 {
1905         NTSTATUS status = NT_STATUS_OK;
1906         uint32_t date_added;
1907         struct adouble *ad = NULL;
1908         struct fruit_config_data *config = NULL;
1909
1910         SMB_VFS_HANDLE_GET_DATA(handle, config,
1911                                 struct fruit_config_data,
1912                                 return NT_STATUS_UNSUCCESSFUL);
1913
1914
1915         /* Ensure we return a default value in the creation_date field */
1916         RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1917
1918         /*
1919          * Resource fork length
1920          */
1921
1922         if (config->readdir_attr_rsize) {
1923                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1924                             ADOUBLE_RSRC);
1925                 if (ad) {
1926                         attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1927                                 ad, ADEID_RFORK);
1928                         TALLOC_FREE(ad);
1929                 }
1930         }
1931
1932         /*
1933          * FinderInfo
1934          */
1935
1936         if (config->readdir_attr_finder_info) {
1937                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1938                             ADOUBLE_META);
1939                 if (ad) {
1940                         if (S_ISREG(smb_fname->st.st_ex_mode)) {
1941                                 /* finder_type */
1942                                 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1943                                        ad_entry(ad, ADEID_FINDERI), 4);
1944
1945                                 /* finder_creator */
1946                                 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1947                                        ad_entry(ad, ADEID_FINDERI) + 4, 4);
1948                         }
1949
1950                         /* finder_flags */
1951                         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1952                                ad_entry(ad, ADEID_FINDERI) + 8, 2);
1953
1954                         /* finder_ext_flags */
1955                         memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1956                                ad_entry(ad, ADEID_FINDERI) + 24, 2);
1957
1958                         /* creation date */
1959                         date_added = convert_time_t_to_uint32_t(
1960                                 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1961                         RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1962
1963                         TALLOC_FREE(ad);
1964                 }
1965         }
1966
1967         TALLOC_FREE(ad);
1968         return status;
1969 }
1970
1971 /* Search MS NFS style ACE with UNIX mode */
1972 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1973                              files_struct *fsp,
1974                              const struct security_descriptor *psd,
1975                              mode_t *pmode,
1976                              bool *pdo_chmod)
1977 {
1978         int i;
1979         struct fruit_config_data *config = NULL;
1980
1981         *pdo_chmod = false;
1982
1983         SMB_VFS_HANDLE_GET_DATA(handle, config,
1984                                 struct fruit_config_data,
1985                                 return NT_STATUS_UNSUCCESSFUL);
1986
1987         if (psd->dacl == NULL || !config->unix_info_enabled) {
1988                 return NT_STATUS_OK;
1989         }
1990
1991         for (i = 0; i < psd->dacl->num_aces; i++) {
1992                 if (dom_sid_compare_domain(
1993                             &global_sid_Unix_NFS_Mode,
1994                             &psd->dacl->aces[i].trustee) == 0) {
1995                         *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1996                         *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1997                         *pdo_chmod = true;
1998
1999                         DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2000                                    fsp_str_dbg(fsp), (unsigned)(*pmode)));
2001                         break;
2002                 }
2003         }
2004
2005         return NT_STATUS_OK;
2006 }
2007
2008 /****************************************************************************
2009  * VFS ops
2010  ****************************************************************************/
2011
2012 static int fruit_connect(vfs_handle_struct *handle,
2013                          const char *service,
2014                          const char *user)
2015 {
2016         int rc;
2017         char *list = NULL, *newlist = NULL;
2018         struct fruit_config_data *config;
2019
2020         DEBUG(10, ("fruit_connect\n"));
2021
2022         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2023         if (rc < 0) {
2024                 return rc;
2025         }
2026
2027         rc = init_fruit_config(handle);
2028         if (rc != 0) {
2029                 return rc;
2030         }
2031
2032         SMB_VFS_HANDLE_GET_DATA(handle, config,
2033                                 struct fruit_config_data, return -1);
2034
2035         if (config->veto_appledouble) {
2036                 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2037
2038                 if (list) {
2039                         if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2040                                 newlist = talloc_asprintf(
2041                                         list,
2042                                         "%s/" ADOUBLE_NAME_PREFIX "*/",
2043                                         list);
2044                                 lp_do_parameter(SNUM(handle->conn),
2045                                                 "veto files",
2046                                                 newlist);
2047                         }
2048                 } else {
2049                         lp_do_parameter(SNUM(handle->conn),
2050                                         "veto files",
2051                                         "/" ADOUBLE_NAME_PREFIX "*/");
2052                 }
2053
2054                 TALLOC_FREE(list);
2055         }
2056
2057         if (config->encoding == FRUIT_ENC_NATIVE) {
2058                 lp_do_parameter(
2059                         SNUM(handle->conn),
2060                         "catia:mappings",
2061                         "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2062                         "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2063                         "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2064                         "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2065                         "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2066                         "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2067                         "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2068                         "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2069                         "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2070                         "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2071                         "0x0d:0xf00d");
2072         }
2073
2074         return rc;
2075 }
2076
2077 static int fruit_open_meta(vfs_handle_struct *handle,
2078                            struct smb_filename *smb_fname,
2079                            files_struct *fsp, int flags, mode_t mode)
2080 {
2081         int rc = 0;
2082         struct fruit_config_data *config = NULL;
2083         struct smb_filename *smb_fname_base = NULL;
2084         int baseflags;
2085         int hostfd = -1;
2086         struct adouble *ad = NULL;
2087
2088         DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
2089
2090         SMB_VFS_HANDLE_GET_DATA(handle, config,
2091                                 struct fruit_config_data, return -1);
2092
2093         if (config->meta == FRUIT_META_STREAM) {
2094                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2095         }
2096
2097         /* Create an smb_filename with stream_name == NULL. */
2098         smb_fname_base = synthetic_smb_fname(talloc_tos(),
2099                                              smb_fname->base_name, NULL, NULL);
2100
2101         if (smb_fname_base == NULL) {
2102                 errno = ENOMEM;
2103                 rc = -1;
2104                 goto exit;
2105         }
2106
2107         /*
2108          * We use baseflags to turn off nasty side-effects when opening the
2109          * underlying file.
2110          */
2111         baseflags = flags;
2112         baseflags &= ~O_TRUNC;
2113         baseflags &= ~O_EXCL;
2114         baseflags &= ~O_CREAT;
2115
2116         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2117                               baseflags, mode);
2118
2119         /*
2120          * It is legit to open a stream on a directory, but the base
2121          * fd has to be read-only.
2122          */
2123         if ((hostfd == -1) && (errno == EISDIR)) {
2124                 baseflags &= ~O_ACCMODE;
2125                 baseflags |= O_RDONLY;
2126                 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2127                                       baseflags, mode);
2128         }
2129
2130         TALLOC_FREE(smb_fname_base);
2131
2132         if (hostfd == -1) {
2133                 rc = -1;
2134                 goto exit;
2135         }
2136
2137         if (flags & (O_CREAT | O_TRUNC)) {
2138                 /*
2139                  * The attribute does not exist or needs to be truncated,
2140                  * create an AppleDouble EA
2141                  */
2142                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2143                              handle, ADOUBLE_META, fsp);
2144                 if (ad == NULL) {
2145                         rc = -1;
2146                         goto exit;
2147                 }
2148
2149                 rc = ad_write(ad, smb_fname->base_name);
2150                 if (rc != 0) {
2151                         rc = -1;
2152                         goto exit;
2153                 }
2154         } else {
2155                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2156                               handle, ADOUBLE_META, fsp);
2157                 if (ad == NULL) {
2158                         rc = -1;
2159                         goto exit;
2160                 }
2161                 if (ad_read(ad, smb_fname->base_name) == -1) {
2162                         rc = -1;
2163                         goto exit;
2164                 }
2165         }
2166
2167 exit:
2168         DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2169         if (rc != 0) {
2170                 int saved_errno = errno;
2171                 if (hostfd >= 0) {
2172                         /*
2173                          * BUGBUGBUG -- we would need to call
2174                          * fd_close_posix here, but we don't have a
2175                          * full fsp yet
2176                          */
2177                         fsp->fh->fd = hostfd;
2178                         SMB_VFS_CLOSE(fsp);
2179                 }
2180                 hostfd = -1;
2181                 errno = saved_errno;
2182         }
2183         return hostfd;
2184 }
2185
2186 static int fruit_open_rsrc(vfs_handle_struct *handle,
2187                            struct smb_filename *smb_fname,
2188                            files_struct *fsp, int flags, mode_t mode)
2189 {
2190         int rc = 0;
2191         struct fruit_config_data *config = NULL;
2192         struct adouble *ad = NULL;
2193         struct smb_filename *smb_fname_base = NULL;
2194         char *adpath = NULL;
2195         int hostfd = -1;
2196
2197         DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2198
2199         SMB_VFS_HANDLE_GET_DATA(handle, config,
2200                                 struct fruit_config_data, return -1);
2201
2202         switch (config->rsrc) {
2203         case FRUIT_RSRC_STREAM:
2204                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2205         case FRUIT_RSRC_XATTR:
2206 #ifdef HAVE_ATTROPEN
2207                 hostfd = attropen(smb_fname->base_name,
2208                                   AFPRESOURCE_EA_NETATALK, flags, mode);
2209                 if (hostfd == -1) {
2210                         return -1;
2211                 }
2212                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2213                              handle, ADOUBLE_RSRC, fsp);
2214                 if (ad == NULL) {
2215                         rc = -1;
2216                         goto exit;
2217                 }
2218                 goto exit;
2219 #else
2220                 errno = ENOTSUP;
2221                 return -1;
2222 #endif
2223         default:
2224                 break;
2225         }
2226
2227         if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2228                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2229                 if (rc != 0) {
2230                         rc = -1;
2231                         goto exit;
2232                 }
2233         }
2234
2235         if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2236                 /* sorry, but directories don't habe a resource fork */
2237                 rc = -1;
2238                 goto exit;
2239         }
2240
2241         rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2242         if (rc != 0) {
2243                 goto exit;
2244         }
2245
2246         /* Create an smb_filename with stream_name == NULL. */
2247         smb_fname_base = synthetic_smb_fname(talloc_tos(),
2248                                              adpath, NULL, NULL);
2249         if (smb_fname_base == NULL) {
2250                 errno = ENOMEM;
2251                 rc = -1;
2252                 goto exit;
2253         }
2254
2255         /* Sanitize flags */
2256         if (flags & O_WRONLY) {
2257                 /* We always need read access for the metadata header too */
2258                 flags &= ~O_WRONLY;
2259                 flags |= O_RDWR;
2260         }
2261
2262         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2263                               flags, mode);
2264         if (hostfd == -1) {
2265                 rc = -1;
2266                 goto exit;
2267         }
2268
2269         /* REVIEW: we need this in ad_write() */
2270         fsp->fh->fd = hostfd;
2271
2272         if (flags & (O_CREAT | O_TRUNC)) {
2273                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2274                              handle, ADOUBLE_RSRC, fsp);
2275                 if (ad == NULL) {
2276                         rc = -1;
2277                         goto exit;
2278                 }
2279                 rc = ad_write(ad, smb_fname->base_name);
2280                 if (rc != 0) {
2281                         rc = -1;
2282                         goto exit;
2283                 }
2284         } else {
2285                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2286                               handle, ADOUBLE_RSRC, fsp);
2287                 if (ad == NULL) {
2288                         rc = -1;
2289                         goto exit;
2290                 }
2291                 if (ad_read(ad, smb_fname->base_name) == -1) {
2292                         rc = -1;
2293                         goto exit;
2294                 }
2295         }
2296
2297 exit:
2298
2299         TALLOC_FREE(adpath);
2300         TALLOC_FREE(smb_fname_base);
2301
2302         DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2303         if (rc != 0) {
2304                 int saved_errno = errno;
2305                 if (hostfd >= 0) {
2306                         /*
2307                          * BUGBUGBUG -- we would need to call
2308                          * fd_close_posix here, but we don't have a
2309                          * full fsp yet
2310                          */
2311                         fsp->fh->fd = hostfd;
2312                         SMB_VFS_CLOSE(fsp);
2313                 }
2314                 hostfd = -1;
2315                 errno = saved_errno;
2316         }
2317         return hostfd;
2318 }
2319
2320 static int fruit_open(vfs_handle_struct *handle,
2321                       struct smb_filename *smb_fname,
2322                       files_struct *fsp, int flags, mode_t mode)
2323 {
2324         DEBUG(10, ("fruit_open called for %s\n",
2325                    smb_fname_str_dbg(smb_fname)));
2326
2327         if (!is_ntfs_stream_smb_fname(smb_fname)) {
2328                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2329         }
2330
2331         if (is_afpinfo_stream(smb_fname)) {
2332                 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2333         } else if (is_afpresource_stream(smb_fname)) {
2334                 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2335         }
2336
2337         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2338 }
2339
2340 static int fruit_rename(struct vfs_handle_struct *handle,
2341                         const struct smb_filename *smb_fname_src,
2342                         const struct smb_filename *smb_fname_dst)
2343 {
2344         int rc = -1;
2345         char *src_adouble_path = NULL;
2346         char *dst_adouble_path = NULL;
2347         struct fruit_config_data *config = NULL;
2348
2349         rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2350
2351         if (!VALID_STAT(smb_fname_src->st)
2352             || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2353                 return rc;
2354         }
2355
2356         SMB_VFS_HANDLE_GET_DATA(handle, config,
2357                                 struct fruit_config_data, return -1);
2358
2359         if (config->rsrc == FRUIT_RSRC_XATTR) {
2360                 return rc;
2361         }
2362
2363         rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2364                           &src_adouble_path);
2365         if (rc != 0) {
2366                 goto done;
2367         }
2368         rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2369                           &dst_adouble_path);
2370         if (rc != 0) {
2371                 goto done;
2372         }
2373
2374         DEBUG(10, ("fruit_rename: %s -> %s\n",
2375                    src_adouble_path, dst_adouble_path));
2376
2377         rc = rename(src_adouble_path, dst_adouble_path);
2378         if (errno == ENOENT) {
2379                 rc = 0;
2380         }
2381
2382         TALLOC_FREE(src_adouble_path);
2383         TALLOC_FREE(dst_adouble_path);
2384
2385 done:
2386         return rc;
2387 }
2388
2389 static int fruit_unlink(vfs_handle_struct *handle,
2390                         const struct smb_filename *smb_fname)
2391 {
2392         int rc = -1;
2393         struct fruit_config_data *config = NULL;
2394
2395         SMB_VFS_HANDLE_GET_DATA(handle, config,
2396                                 struct fruit_config_data, return -1);
2397
2398         if (!is_ntfs_stream_smb_fname(smb_fname)) {
2399                 char *adp = NULL;
2400
2401                 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2402                 if (rc != 0) {
2403                         return -1;
2404                 }
2405
2406                 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2407                         return 0;
2408                 }
2409
2410                 /*
2411                  * 0 byte resource fork streams are not listed by
2412                  * vfs_streaminfo, as a result stream cleanup/deletion of file
2413                  * deletion doesn't remove the resourcefork stream.
2414                  */
2415                 rc = adouble_path(talloc_tos(),
2416                                   smb_fname->base_name, &adp);
2417                 if (rc != 0) {
2418                         return -1;
2419                 }
2420
2421                 /* FIXME: direct unlink(), missing smb_fname */
2422                 DEBUG(1,("fruit_unlink: %s\n", adp));
2423                 rc = unlink(adp);
2424                 if ((rc == -1) && (errno == ENOENT)) {
2425                         rc = 0;
2426                 }
2427
2428                 TALLOC_FREE(adp);
2429                 return 0;
2430         }
2431
2432         if (is_afpinfo_stream(smb_fname)) {
2433                 if (config->meta == FRUIT_META_STREAM) {
2434                         rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2435                 } else {
2436                         rc = SMB_VFS_REMOVEXATTR(handle->conn,
2437                                                  smb_fname->base_name,
2438                                                  AFPINFO_EA_NETATALK);
2439                 }
2440
2441                 return rc;
2442         }
2443
2444         if (is_afpresource_stream(smb_fname)) {
2445                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2446                         char *adp = NULL;
2447
2448                         rc = adouble_path(talloc_tos(),
2449                                           smb_fname->base_name, &adp);
2450                         if (rc != 0) {
2451                                 return -1;
2452                         }
2453                         /* FIXME: direct unlink(), missing smb_fname */
2454                         rc = unlink(adp);
2455                         if ((rc == -1) && (errno == ENOENT)) {
2456                                 rc = 0;
2457                         }
2458                         TALLOC_FREE(adp);
2459                 } else {
2460                         rc = SMB_VFS_REMOVEXATTR(handle->conn,
2461                                                  smb_fname->base_name,
2462                                                  AFPRESOURCE_EA_NETATALK);
2463                 }
2464
2465                 return rc;
2466         }
2467
2468         return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2469
2470
2471         return 0;
2472 }
2473
2474 static int fruit_chmod(vfs_handle_struct *handle,
2475                        const char *path,
2476                        mode_t mode)
2477 {
2478         int rc = -1;
2479         char *adp = NULL;
2480         struct fruit_config_data *config = NULL;
2481         SMB_STRUCT_STAT sb;
2482
2483         rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2484         if (rc != 0) {
2485                 return rc;
2486         }
2487
2488         SMB_VFS_HANDLE_GET_DATA(handle, config,
2489                                 struct fruit_config_data, return -1);
2490
2491         if (config->rsrc == FRUIT_RSRC_XATTR) {
2492                 return 0;
2493         }
2494
2495         /* FIXME: direct sys_lstat(), missing smb_fname */
2496         rc = sys_lstat(path, &sb, false);
2497         if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2498                 return rc;
2499         }
2500
2501         rc = adouble_path(talloc_tos(), path, &adp);
2502         if (rc != 0) {
2503                 return -1;
2504         }
2505
2506         DEBUG(10, ("fruit_chmod: %s\n", adp));
2507
2508         rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2509         if (errno == ENOENT) {
2510                 rc = 0;
2511         }
2512
2513         TALLOC_FREE(adp);
2514         return rc;
2515 }
2516
2517 static int fruit_chown(vfs_handle_struct *handle,
2518                        const char *path,
2519                        uid_t uid,
2520                        gid_t gid)
2521 {
2522         int rc = -1;
2523         char *adp = NULL;
2524         struct fruit_config_data *config = NULL;
2525         SMB_STRUCT_STAT sb;
2526
2527         rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2528         if (rc != 0) {
2529                 return rc;
2530         }
2531
2532         SMB_VFS_HANDLE_GET_DATA(handle, config,
2533                                 struct fruit_config_data, return -1);
2534
2535         if (config->rsrc == FRUIT_RSRC_XATTR) {
2536                 return rc;
2537         }
2538
2539         /* FIXME: direct sys_lstat(), missing smb_fname */
2540         rc = sys_lstat(path, &sb, false);
2541         if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2542                 return rc;
2543         }
2544
2545         rc = adouble_path(talloc_tos(), path, &adp);
2546         if (rc != 0) {
2547                 goto done;
2548         }
2549
2550         DEBUG(10, ("fruit_chown: %s\n", adp));
2551
2552         rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2553         if (errno == ENOENT) {
2554                 rc = 0;
2555         }
2556
2557  done:
2558         TALLOC_FREE(adp);
2559         return rc;
2560 }
2561
2562 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2563 {
2564         DIR *dh = NULL;
2565         struct dirent *de;
2566         struct fruit_config_data *config;
2567
2568         SMB_VFS_HANDLE_GET_DATA(handle, config,
2569                                 struct fruit_config_data, return -1);
2570
2571         if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2572                 goto exit_rmdir;
2573         }
2574
2575         /*
2576          * Due to there is no way to change bDeleteVetoFiles variable
2577          * from this module, need to clean up ourselves
2578          */
2579         dh = opendir(path);
2580         if (dh == NULL) {
2581                 goto exit_rmdir;
2582         }
2583
2584         while ((de = readdir(dh)) != NULL) {
2585                 if ((strncmp(de->d_name,
2586                              ADOUBLE_NAME_PREFIX,
2587                              strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2588                         char *p = talloc_asprintf(talloc_tos(),
2589                                                   "%s/%s",
2590                                                   path, de->d_name);
2591                         if (p == NULL) {
2592                                 goto exit_rmdir;
2593                         }
2594                         DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2595                         (void)unlink(p);
2596                         TALLOC_FREE(p);
2597                 }
2598         }
2599
2600 exit_rmdir:
2601         if (dh) {
2602                 closedir(dh);
2603         }
2604         return SMB_VFS_NEXT_RMDIR(handle, path);
2605 }
2606
2607 static ssize_t fruit_pread(vfs_handle_struct *handle,
2608                            files_struct *fsp, void *data,
2609                            size_t n, off_t offset)
2610 {
2611         int rc = 0;
2612         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2613                 handle, fsp);
2614         struct fruit_config_data *config = NULL;
2615         AfpInfo *ai = NULL;
2616         ssize_t len;
2617         char *name = NULL;
2618         char *tmp_base_name = NULL;
2619         NTSTATUS status;
2620
2621         DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2622
2623         if (!fsp->base_fsp) {
2624                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2625         }
2626
2627         SMB_VFS_HANDLE_GET_DATA(handle, config,
2628                                 struct fruit_config_data, return -1);
2629
2630         /* fsp_name is not converted with vfs_catia */
2631         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2632         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2633                                         fsp->base_fsp->fsp_name->base_name,
2634                                         vfs_translate_to_unix,
2635                                         talloc_tos(), &name);
2636         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2637                 name = talloc_strdup(talloc_tos(), tmp_base_name);
2638                 if (name == NULL) {
2639                         rc = -1;
2640                         goto exit;
2641                 }
2642         } else if (!NT_STATUS_IS_OK(status)) {
2643                 errno = map_errno_from_nt_status(status);
2644                 rc = -1;
2645                 goto exit;
2646         }
2647         fsp->base_fsp->fsp_name->base_name = name;
2648
2649         if (ad == NULL) {
2650                 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2651                 if (len == -1) {
2652                         rc = -1;
2653                         goto exit;
2654                 }
2655                 goto exit;
2656         }
2657
2658         if (!fruit_fsp_recheck(ad, fsp)) {
2659                 rc = -1;
2660                 goto exit;
2661         }
2662
2663         if (ad->ad_type == ADOUBLE_META) {
2664                 char afpinfo_buf[AFP_INFO_SIZE];
2665                 size_t to_return;
2666
2667                 if ((offset < 0) || (offset >= AFP_INFO_SIZE)) {
2668                         len = 0;
2669                         rc = 0;
2670                         goto exit;
2671                 }
2672
2673                 to_return = AFP_INFO_SIZE - offset;
2674
2675                 ai = afpinfo_new(talloc_tos());
2676                 if (ai == NULL) {
2677                         rc = -1;
2678                         goto exit;
2679                 }
2680
2681                 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2682                 if (len == -1) {
2683                         rc = -1;
2684                         goto exit;
2685                 }
2686
2687                 memcpy(&ai->afpi_FinderInfo[0],
2688                        ad_entry(ad, ADEID_FINDERI),
2689                        ADEDLEN_FINDERI);
2690                 len = afpinfo_pack(ai, afpinfo_buf);
2691                 if (len != AFP_INFO_SIZE) {
2692                         rc = -1;
2693                         goto exit;
2694                 }
2695
2696                 memcpy(data, afpinfo_buf + offset, to_return);
2697                 len = to_return;
2698         } else {
2699                 len = SMB_VFS_NEXT_PREAD(
2700                         handle, fsp, data, n,
2701                         offset + ad_getentryoff(ad, ADEID_RFORK));
2702                 if (len == -1) {
2703                         rc = -1;
2704                         goto exit;
2705                 }
2706         }
2707 exit:
2708         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2709         TALLOC_FREE(name);
2710         TALLOC_FREE(ai);
2711         if (rc != 0) {
2712                 len = -1;
2713         }
2714         DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2715         return len;
2716 }
2717
2718 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2719                             files_struct *fsp, const void *data,
2720                             size_t n, off_t offset)
2721 {
2722         int rc = 0;
2723         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2724                 handle, fsp);
2725         struct fruit_config_data *config = NULL;
2726         AfpInfo *ai = NULL;
2727         ssize_t len;
2728         char *name = NULL;
2729         char *tmp_base_name = NULL;
2730         NTSTATUS status;
2731
2732         DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2733
2734         if (!fsp->base_fsp) {
2735                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2736         }
2737
2738         SMB_VFS_HANDLE_GET_DATA(handle, config,
2739                                 struct fruit_config_data, return -1);
2740
2741         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2742         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2743                                         fsp->base_fsp->fsp_name->base_name,
2744                                         vfs_translate_to_unix,
2745                                         talloc_tos(), &name);
2746         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2747                 name = talloc_strdup(talloc_tos(), tmp_base_name);
2748                 if (name == NULL) {
2749                         rc = -1;
2750                         goto exit;
2751                 }
2752         } else if (!NT_STATUS_IS_OK(status)) {
2753                 errno = map_errno_from_nt_status(status);
2754                 rc = -1;
2755                 goto exit;
2756         }
2757         fsp->base_fsp->fsp_name->base_name = name;
2758
2759         if (ad == NULL) {
2760                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2761                 if (len != n) {
2762                         rc = -1;
2763                         goto exit;
2764                 }
2765                 goto exit;
2766         }
2767
2768         if (!fruit_fsp_recheck(ad, fsp)) {
2769                 rc = -1;
2770                 goto exit;
2771         }
2772
2773         if (ad->ad_type == ADOUBLE_META) {
2774                 if (n != AFP_INFO_SIZE || offset != 0) {
2775                         DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2776                                   (intmax_t)offset, (intmax_t)n));
2777                         rc = -1;
2778                         goto exit;
2779                 }
2780                 ai = afpinfo_unpack(talloc_tos(), data);
2781                 if (ai == NULL) {
2782                         rc = -1;
2783                         goto exit;
2784                 }
2785                 memcpy(ad_entry(ad, ADEID_FINDERI),
2786                        &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2787                 rc = ad_write(ad, name);
2788         } else {
2789                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2790                                    offset + ad_getentryoff(ad, ADEID_RFORK));
2791                 if (len != n) {
2792                         rc = -1;
2793                         goto exit;
2794                 }
2795
2796                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2797                         rc = ad_read(ad, name);
2798                         if (rc == -1) {
2799                                 goto exit;
2800                         }
2801                         rc = 0;
2802
2803                         if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2804                                 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2805                                 rc = ad_write(ad, name);
2806                         }
2807                 }
2808         }
2809
2810 exit:
2811         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2812         TALLOC_FREE(name);
2813         TALLOC_FREE(ai);
2814         if (rc != 0) {
2815                 return -1;
2816         }
2817         return n;
2818 }
2819
2820 /**
2821  * Helper to stat/lstat the base file of an smb_fname.
2822  */
2823 static int fruit_stat_base(vfs_handle_struct *handle,
2824                            struct smb_filename *smb_fname,
2825                            bool follow_links)
2826 {
2827         char *tmp_stream_name;
2828         int rc;
2829
2830         tmp_stream_name = smb_fname->stream_name;
2831         smb_fname->stream_name = NULL;
2832         if (follow_links) {
2833                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2834         } else {
2835                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2836         }
2837         smb_fname->stream_name = tmp_stream_name;
2838         return rc;
2839 }
2840
2841 static int fruit_stat_meta(vfs_handle_struct *handle,
2842                            struct smb_filename *smb_fname,
2843                            bool follow_links)
2844 {
2845         /* Populate the stat struct with info from the base file. */
2846         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2847                 return -1;
2848         }
2849         smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2850         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2851                                               smb_fname->stream_name);
2852         return 0;
2853 }
2854
2855 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2856                            struct smb_filename *smb_fname,
2857                            bool follow_links)
2858
2859 {
2860         struct adouble *ad = NULL;
2861
2862         DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2863                    smb_fname_str_dbg(smb_fname)));
2864
2865         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2866         if (ad == NULL) {
2867                 errno = ENOENT;
2868                 return -1;
2869         }
2870
2871         /* Populate the stat struct with info from the base file. */
2872         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2873                 TALLOC_FREE(ad);
2874                 return -1;
2875         }
2876
2877         smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2878         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2879                                               smb_fname->stream_name);
2880         TALLOC_FREE(ad);
2881         return 0;
2882 }
2883
2884 static int fruit_stat(vfs_handle_struct *handle,
2885                       struct smb_filename *smb_fname)
2886 {
2887         int rc = -1;
2888
2889         DEBUG(10, ("fruit_stat called for %s\n",
2890                    smb_fname_str_dbg(smb_fname)));
2891
2892         if (!is_ntfs_stream_smb_fname(smb_fname)
2893             || is_ntfs_default_stream_smb_fname(smb_fname)) {
2894                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2895                 if (rc == 0) {
2896                         update_btime(handle, smb_fname);
2897                 }
2898                 return rc;
2899         }
2900
2901         /*
2902          * Note if lp_posix_paths() is true, we can never
2903          * get here as is_ntfs_stream_smb_fname() is
2904          * always false. So we never need worry about
2905          * not following links here.
2906          */
2907
2908         if (is_afpinfo_stream(smb_fname)) {
2909                 rc = fruit_stat_meta(handle, smb_fname, true);
2910         } else if (is_afpresource_stream(smb_fname)) {
2911                 rc = fruit_stat_rsrc(handle, smb_fname, true);
2912         } else {
2913                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2914         }
2915
2916         if (rc == 0) {
2917                 update_btime(handle, smb_fname);
2918                 smb_fname->st.st_ex_mode &= ~S_IFMT;
2919                 smb_fname->st.st_ex_mode |= S_IFREG;
2920                 smb_fname->st.st_ex_blocks =
2921                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2922         }
2923         return rc;
2924 }
2925
2926 static int fruit_lstat(vfs_handle_struct *handle,
2927                        struct smb_filename *smb_fname)
2928 {
2929         int rc = -1;
2930
2931         DEBUG(10, ("fruit_lstat called for %s\n",
2932                    smb_fname_str_dbg(smb_fname)));
2933
2934         if (!is_ntfs_stream_smb_fname(smb_fname)
2935             || is_ntfs_default_stream_smb_fname(smb_fname)) {
2936                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2937                 if (rc == 0) {
2938                         update_btime(handle, smb_fname);
2939                 }
2940                 return rc;
2941         }
2942
2943         if (is_afpinfo_stream(smb_fname)) {
2944                 rc = fruit_stat_meta(handle, smb_fname, false);
2945         } else if (is_afpresource_stream(smb_fname)) {
2946                 rc = fruit_stat_rsrc(handle, smb_fname, false);
2947         } else {
2948                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2949         }
2950
2951         if (rc == 0) {
2952                 update_btime(handle, smb_fname);
2953                 smb_fname->st.st_ex_mode &= ~S_IFMT;
2954                 smb_fname->st.st_ex_mode |= S_IFREG;
2955                 smb_fname->st.st_ex_blocks =
2956                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2957         }
2958         return rc;
2959 }
2960
2961 static int fruit_fstat_meta(vfs_handle_struct *handle,
2962                             files_struct *fsp,
2963                             SMB_STRUCT_STAT *sbuf)
2964 {
2965         DEBUG(10, ("fruit_fstat_meta called for %s\n",
2966                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2967
2968         /* Populate the stat struct with info from the base file. */
2969         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2970                 return -1;
2971         }
2972         *sbuf = fsp->base_fsp->fsp_name->st;
2973         sbuf->st_ex_size = AFP_INFO_SIZE;
2974         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2975
2976         return 0;
2977 }
2978
2979 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
2980                             SMB_STRUCT_STAT *sbuf)
2981 {
2982         struct fruit_config_data *config;
2983         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2984                 handle, fsp);
2985
2986         DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
2987                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2988
2989         SMB_VFS_HANDLE_GET_DATA(handle, config,
2990                                 struct fruit_config_data, return -1);
2991
2992         if (config->rsrc == FRUIT_RSRC_STREAM) {
2993                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2994         }
2995
2996         /* Populate the stat struct with info from the base file. */
2997         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2998                 return -1;
2999         }
3000         *sbuf = fsp->base_fsp->fsp_name->st;
3001         sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3002         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3003
3004         DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3005                    smb_fname_str_dbg(fsp->fsp_name),
3006                    (ssize_t)sbuf->st_ex_size));
3007
3008         return 0;
3009 }
3010
3011 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3012                        SMB_STRUCT_STAT *sbuf)
3013 {
3014         int rc;
3015         char *name = NULL;
3016         char *tmp_base_name = NULL;
3017         NTSTATUS status;
3018         struct adouble *ad = (struct adouble *)
3019                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
3020
3021         DEBUG(10, ("fruit_fstat called for %s\n",
3022                    smb_fname_str_dbg(fsp->fsp_name)));
3023
3024         if (fsp->base_fsp) {
3025                 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3026                 /* fsp_name is not converted with vfs_catia */
3027                 status = SMB_VFS_TRANSLATE_NAME(
3028                         handle->conn,
3029                         fsp->base_fsp->fsp_name->base_name,
3030                         vfs_translate_to_unix,
3031                         talloc_tos(), &name);
3032
3033                 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3034                         name = talloc_strdup(talloc_tos(), tmp_base_name);
3035                         if (name == NULL) {
3036                                 rc = -1;
3037                                 goto exit;
3038                         }
3039                 } else if (!NT_STATUS_IS_OK(status)) {
3040                         errno = map_errno_from_nt_status(status);
3041                         rc = -1;
3042                         goto exit;
3043                 }
3044                 fsp->base_fsp->fsp_name->base_name = name;
3045         }
3046
3047         if (ad == NULL || fsp->base_fsp == NULL) {
3048                 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3049                 goto exit;
3050         }
3051
3052         if (!fruit_fsp_recheck(ad, fsp)) {
3053                 rc = -1;
3054                 goto exit;
3055         }
3056
3057         switch (ad->ad_type) {
3058         case ADOUBLE_META:
3059                 rc = fruit_fstat_meta(handle, fsp, sbuf);
3060                 break;
3061         case ADOUBLE_RSRC:
3062                 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
3063                 break;
3064         default:
3065                 DEBUG(10, ("fruit_fstat %s: bad type\n",
3066                            smb_fname_str_dbg(fsp->fsp_name)));
3067                 rc = -1;
3068                 goto exit;
3069         }
3070
3071         if (rc == 0) {
3072                 sbuf->st_ex_mode &= ~S_IFMT;
3073                 sbuf->st_ex_mode |= S_IFREG;
3074                 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3075         }
3076
3077 exit:
3078         DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3079                    smb_fname_str_dbg(fsp->fsp_name),
3080                    (ssize_t)sbuf->st_ex_size));
3081         if (tmp_base_name) {
3082                 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3083         }
3084         TALLOC_FREE(name);
3085         return rc;
3086 }
3087
3088 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3089                                  struct files_struct *fsp,
3090                                  const char *fname,
3091                                  TALLOC_CTX *mem_ctx,
3092                                  unsigned int *pnum_streams,
3093                                  struct stream_struct **pstreams)
3094 {
3095         struct fruit_config_data *config = NULL;
3096         struct smb_filename *smb_fname = NULL;
3097         struct adouble *ad = NULL;
3098
3099         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3100                                 return NT_STATUS_UNSUCCESSFUL);
3101         DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
3102
3103         smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
3104         if (smb_fname == NULL) {
3105                 return NT_STATUS_NO_MEMORY;
3106         }
3107
3108         if (config->meta == FRUIT_META_NETATALK) {
3109                 ad = ad_get(talloc_tos(), handle,
3110                             smb_fname->base_name, ADOUBLE_META);
3111                 if (ad && !empty_finderinfo(ad)) {
3112                         if (!add_fruit_stream(
3113                                     mem_ctx, pnum_streams, pstreams,
3114                                     AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3115                                     smb_roundup(handle->conn,
3116                                                 AFP_INFO_SIZE))) {
3117                                 TALLOC_FREE(ad);
3118                                 TALLOC_FREE(smb_fname);
3119                                 return NT_STATUS_NO_MEMORY;
3120                         }
3121                 }
3122                 TALLOC_FREE(ad);
3123         }
3124
3125         if (config->rsrc != FRUIT_RSRC_STREAM) {
3126                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3127                             ADOUBLE_RSRC);
3128                 if (ad && (ad_getentrylen(ad, ADEID_RFORK) > 0)) {
3129                         if (!add_fruit_stream(
3130                                     mem_ctx, pnum_streams, pstreams,
3131                                     AFPRESOURCE_STREAM_NAME,
3132                                     ad_getentrylen(ad, ADEID_RFORK),
3133                                     smb_roundup(handle->conn,
3134                                                 ad_getentrylen(
3135                                                         ad, ADEID_RFORK)))) {
3136                                 TALLOC_FREE(ad);
3137                                 TALLOC_FREE(smb_fname);
3138                                 return NT_STATUS_NO_MEMORY;
3139                         }
3140                 }
3141                 TALLOC_FREE(ad);
3142         }
3143
3144         TALLOC_FREE(smb_fname);
3145
3146         return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3147                                        pnum_streams, pstreams);
3148 }
3149
3150 static int fruit_ntimes(vfs_handle_struct *handle,
3151                         const struct smb_filename *smb_fname,
3152                         struct smb_file_time *ft)
3153 {
3154         int rc = 0;
3155         struct adouble *ad = NULL;
3156
3157         if (null_timespec(ft->create_time)) {
3158                 goto exit;
3159         }
3160
3161         DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3162                  time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3163
3164         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3165         if (ad == NULL) {
3166                 goto exit;
3167         }
3168
3169         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3170                    convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3171
3172         rc = ad_write(ad, smb_fname->base_name);
3173
3174 exit:
3175
3176         TALLOC_FREE(ad);
3177         if (rc != 0) {
3178                 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3179                 return -1;
3180         }
3181         return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3182 }
3183
3184 static int fruit_fallocate(struct vfs_handle_struct *handle,
3185                            struct files_struct *fsp,
3186                            uint32_t mode,
3187                            off_t offset,
3188                            off_t len)
3189 {
3190         struct adouble *ad =
3191                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3192
3193         if (ad == NULL) {
3194                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3195         }
3196
3197         if (!fruit_fsp_recheck(ad, fsp)) {
3198                 return -1;
3199         }
3200
3201         /* Let the pwrite code path handle it. */
3202         errno = ENOSYS;
3203         return -1;
3204 }
3205
3206 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3207                                 struct files_struct *fsp,
3208                                 off_t offset,
3209                                 struct adouble *ad)
3210 {
3211         /*
3212          * As this request hasn't been seen in the wild,
3213          * the only sensible use I can imagine is the client
3214          * truncating the stream to 0 bytes size.
3215          * We simply remove the metadata on such a request.
3216          */
3217         if (offset != 0) {
3218                 DBG_WARNING("ftruncate %s to %jd",
3219                             fsp_str_dbg(fsp), (intmax_t)offset);
3220                 return -1;
3221         }
3222
3223         return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
3224 }
3225
3226 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3227                                 struct files_struct *fsp,
3228                                 off_t offset,
3229                                 struct adouble *ad)
3230 {
3231         int rc;
3232         struct fruit_config_data *config;
3233
3234         SMB_VFS_HANDLE_GET_DATA(handle, config,
3235                                 struct fruit_config_data, return -1);
3236
3237         if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3238                 return SMB_VFS_FREMOVEXATTR(fsp,
3239                                             AFPRESOURCE_EA_NETATALK);
3240         }
3241
3242         rc = SMB_VFS_NEXT_FTRUNCATE(
3243                 handle, fsp,
3244                 offset + ad_getentryoff(ad, ADEID_RFORK));
3245         if (rc != 0) {
3246                 return -1;
3247         }
3248
3249         if (config->rsrc == FRUIT_RSRC_ADFILE) {
3250                 ad_setentrylen(ad, ADEID_RFORK, offset);
3251                 rc = ad_write(ad, NULL);
3252                 if (rc != 0) {
3253                         return -1;
3254                 }
3255                 DEBUG(10, ("fruit_ftruncate_rsrc file %s offset %jd\n",
3256                            fsp_str_dbg(fsp), (intmax_t)offset));
3257         }
3258
3259         return 0;
3260 }
3261
3262 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3263                            struct files_struct *fsp,
3264                            off_t offset)
3265 {
3266         int rc = 0;
3267         struct adouble *ad =
3268                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3269
3270         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3271                    fsp_str_dbg(fsp), (double)offset));
3272
3273         if (ad == NULL) {
3274                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3275         }
3276
3277         if (!fruit_fsp_recheck(ad, fsp)) {
3278                 return -1;
3279         }
3280
3281         switch (ad->ad_type) {
3282         case ADOUBLE_META:
3283                 rc = fruit_ftruncate_meta(handle, fsp, offset, ad);
3284                 break;
3285
3286         case ADOUBLE_RSRC:
3287                 rc = fruit_ftruncate_rsrc(handle, fsp, offset, ad);
3288                 break;
3289
3290         default:
3291                 return -1;
3292         }
3293
3294         return rc;
3295 }
3296
3297 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3298                                   struct smb_request *req,
3299                                   uint16_t root_dir_fid,
3300                                   struct smb_filename *smb_fname,
3301                                   uint32_t access_mask,
3302                                   uint32_t share_access,
3303                                   uint32_t create_disposition,
3304                                   uint32_t create_options,
3305                                   uint32_t file_attributes,
3306                                   uint32_t oplock_request,
3307                                   struct smb2_lease *lease,
3308                                   uint64_t allocation_size,
3309                                   uint32_t private_flags,
3310                                   struct security_descriptor *sd,
3311                                   struct ea_list *ea_list,
3312                                   files_struct **result,
3313                                   int *pinfo,
3314                                   const struct smb2_create_blobs *in_context_blobs,
3315                                   struct smb2_create_blobs *out_context_blobs)
3316 {
3317         NTSTATUS status;
3318         struct fruit_config_data *config = NULL;
3319         files_struct *fsp = NULL;
3320
3321         status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3322         if (!NT_STATUS_IS_OK(status)) {
3323                 goto fail;
3324         }
3325
3326         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3327                                 return NT_STATUS_UNSUCCESSFUL);
3328
3329         status = SMB_VFS_NEXT_CREATE_FILE(
3330                 handle, req, root_dir_fid, smb_fname,
3331                 access_mask, share_access,
3332                 create_disposition, create_options,
3333                 file_attributes, oplock_request,
3334                 lease,
3335                 allocation_size, private_flags,
3336                 sd, ea_list, result,
3337                 pinfo, in_context_blobs, out_context_blobs);
3338         if (!NT_STATUS_IS_OK(status)) {
3339                 return status;
3340         }
3341         fsp = *result;
3342
3343         if (config->copyfile_enabled) {
3344                 /*
3345                  * Set a flag in the fsp. Gets used in copychunk to
3346                  * check whether the special Apple copyfile semantics
3347                  * for copychunk should be allowed in a copychunk
3348                  * request with a count of 0.
3349                  */
3350                 fsp->aapl_copyfile_supported = true;
3351         }
3352
3353         /*
3354          * If this is a plain open for existing files, opening an 0
3355          * byte size resource fork MUST fail with
3356          * NT_STATUS_OBJECT_NAME_NOT_FOUND.
3357          *
3358          * Cf the vfs_fruit torture tests in test_rfork_create().
3359          */
3360         if (is_afpresource_stream(fsp->fsp_name) &&
3361             create_disposition == FILE_OPEN)
3362         {
3363                 if (fsp->fsp_name->st.st_ex_size == 0) {
3364                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3365                         goto fail;
3366                 }
3367         }
3368
3369         if (is_ntfs_stream_smb_fname(smb_fname)
3370             || fsp->is_directory) {
3371                 return status;
3372         }
3373
3374         if (config->locking == FRUIT_LOCKING_NETATALK) {
3375                 status = fruit_check_access(
3376                         handle, *result,
3377                         access_mask,
3378                         map_share_mode_to_deny_mode(share_access, 0));
3379                 if (!NT_STATUS_IS_OK(status)) {
3380                         goto fail;
3381                 }
3382         }
3383
3384         return status;
3385
3386 fail:
3387         DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
3388
3389         if (fsp) {
3390                 close_file(req, fsp, ERROR_CLOSE);
3391                 *result = fsp = NULL;
3392         }
3393
3394         return status;
3395 }
3396
3397 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3398                                    const struct smb_filename *fname,
3399                                    TALLOC_CTX *mem_ctx,
3400                                    struct readdir_attr_data **pattr_data)
3401 {
3402         struct fruit_config_data *config = NULL;
3403         struct readdir_attr_data *attr_data;
3404         NTSTATUS status;
3405
3406         SMB_VFS_HANDLE_GET_DATA(handle, config,
3407                                 struct fruit_config_data,
3408                                 return NT_STATUS_UNSUCCESSFUL);
3409
3410         if (!config->use_aapl) {
3411                 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3412         }
3413
3414         DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3415
3416         *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3417         if (*pattr_data == NULL) {
3418                 return NT_STATUS_UNSUCCESSFUL;
3419         }
3420         attr_data = *pattr_data;
3421         attr_data->type = RDATTR_AAPL;
3422
3423         /*
3424          * Mac metadata: compressed FinderInfo, resource fork length
3425          * and creation date
3426          */
3427         status = readdir_attr_macmeta(handle, fname, attr_data);
3428         if (!NT_STATUS_IS_OK(status)) {
3429                 /*
3430                  * Error handling is tricky: if we return failure from
3431                  * this function, the corresponding directory entry
3432                  * will to be passed to the client, so we really just
3433                  * want to error out on fatal errors.
3434                  */
3435                 if  (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3436                         goto fail;
3437                 }
3438         }
3439
3440         /*
3441          * UNIX mode
3442          */
3443         if (config->unix_info_enabled) {
3444                 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3445         }
3446
3447         /*
3448          * max_access
3449          */
3450         if (!config->readdir_attr_max_access) {
3451                 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3452         } else {
3453                 status = smbd_calculate_access_mask(
3454                         handle->conn,
3455                         fname,
3456                         false,
3457                         SEC_FLAG_MAXIMUM_ALLOWED,
3458                         &attr_data->attr_data.aapl.max_access);
3459                 if (!NT_STATUS_IS_OK(status)) {
3460                         goto fail;
3461                 }
3462         }
3463
3464         return NT_STATUS_OK;
3465
3466 fail:
3467         DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3468                   fname->base_name, nt_errstr(status)));
3469         TALLOC_FREE(*pattr_data);
3470         return status;
3471 }
3472
3473 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3474                                   files_struct *fsp,
3475                                   uint32_t security_info,
3476                                   TALLOC_CTX *mem_ctx,
3477                                   struct security_descriptor **ppdesc)
3478 {
3479         NTSTATUS status;
3480         struct security_ace ace;
3481         struct dom_sid sid;
3482         struct fruit_config_data *config;
3483
3484         SMB_VFS_HANDLE_GET_DATA(handle, config,
3485                                 struct fruit_config_data,
3486                                 return NT_STATUS_UNSUCCESSFUL);
3487
3488         status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3489                                           mem_ctx, ppdesc);
3490         if (!NT_STATUS_IS_OK(status)) {
3491                 return status;
3492         }
3493
3494         /*
3495          * Add MS NFS style ACEs with uid, gid and mode
3496          */
3497         if (!config->unix_info_enabled) {
3498                 return NT_STATUS_OK;
3499         }
3500
3501         /* MS NFS style mode */
3502         sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3503         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3504         status = security_descriptor_dacl_add(*ppdesc, &ace);
3505         if (!NT_STATUS_IS_OK(status)) {
3506                 DEBUG(1,("failed to add MS NFS style ACE\n"));
3507                 return status;
3508         }
3509
3510         /* MS NFS style uid */
3511         sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3512         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3513         status = security_descriptor_dacl_add(*ppdesc, &ace);
3514         if (!NT_STATUS_IS_OK(status)) {
3515                 DEBUG(1,("failed to add MS NFS style ACE\n"));
3516                 return status;
3517         }
3518
3519         /* MS NFS style gid */
3520         sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3521         init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3522         status = security_descriptor_dacl_add(*ppdesc, &ace);
3523         if (!NT_STATUS_IS_OK(status)) {
3524                 DEBUG(1,("failed to add MS NFS style ACE\n"));
3525                 return status;
3526         }
3527
3528         return NT_STATUS_OK;
3529 }
3530
3531 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3532                                   files_struct *fsp,
3533                                   uint32_t security_info_sent,
3534                                   const struct security_descriptor *psd)
3535 {
3536         NTSTATUS status;
3537         bool do_chmod;
3538         mode_t ms_nfs_mode;
3539         int result;
3540
3541         DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp)));
3542
3543         status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3544         if (!NT_STATUS_IS_OK(status)) {
3545                 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3546                 return status;
3547         }
3548
3549         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3550         if (!NT_STATUS_IS_OK(status)) {
3551                 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3552                 return status;
3553         }
3554
3555         if (do_chmod) {
3556                 if (fsp->fh->fd != -1) {
3557                         DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp)));
3558                         result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3559                 } else {
3560                         DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp)));
3561                         result = SMB_VFS_CHMOD(fsp->conn,
3562                                                fsp->fsp_name->base_name,
3563                                                ms_nfs_mode);
3564                 }
3565
3566                 if (result != 0) {
3567                         DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3568                                   result, (unsigned)ms_nfs_mode,
3569                                   strerror(errno)));
3570                         status = map_nt_error_from_unix(errno);
3571                         return status;
3572                 }
3573         }
3574
3575         return NT_STATUS_OK;
3576 }
3577
3578 struct fruit_copy_chunk_state {
3579         struct vfs_handle_struct *handle;
3580         off_t copied;
3581         struct files_struct *src_fsp;
3582         struct files_struct *dst_fsp;
3583         bool is_copyfile;
3584 };
3585
3586 static void fruit_copy_chunk_done(struct tevent_req *subreq);
3587 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
3588                                                 TALLOC_CTX *mem_ctx,
3589                                                 struct tevent_context *ev,
3590                                                 struct files_struct *src_fsp,
3591                                                 off_t src_off,
3592                                                 struct files_struct *dest_fsp,
3593                                                 off_t dest_off,
3594                                                 off_t num)
3595 {
3596         struct tevent_req *req, *subreq;
3597         struct fruit_copy_chunk_state *fruit_copy_chunk_state;
3598         NTSTATUS status;
3599         struct fruit_config_data *config;
3600         off_t to_copy = num;
3601
3602         DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
3603                   (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
3604
3605         SMB_VFS_HANDLE_GET_DATA(handle, config,
3606                                 struct fruit_config_data,
3607                                 return NULL);
3608
3609         req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
3610                                 struct fruit_copy_chunk_state);
3611         if (req == NULL) {
3612                 return NULL;
3613         }
3614         fruit_copy_chunk_state->handle = handle;
3615         fruit_copy_chunk_state->src_fsp = src_fsp;
3616         fruit_copy_chunk_state->dst_fsp = dest_fsp;
3617
3618         /*
3619          * Check if this a OS X copyfile style copychunk request with
3620          * a requested chunk count of 0 that was translated to a
3621          * copy_chunk_send VFS call overloading the parameters src_off
3622          * = dest_off = num = 0.
3623          */
3624         if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
3625             src_fsp->aapl_copyfile_supported &&
3626             dest_fsp->aapl_copyfile_supported)
3627         {
3628                 status = vfs_stat_fsp(src_fsp);
3629                 if (tevent_req_nterror(req, status)) {
3630                         return tevent_req_post(req, ev);
3631                 }
3632
3633                 to_copy = src_fsp->fsp_name->st.st_ex_size;
3634                 fruit_copy_chunk_state->is_copyfile = true;
3635         }
3636
3637         subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
3638                                               mem_ctx,
3639                                               ev,
3640                                               src_fsp,
3641                                               src_off,
3642                                               dest_fsp,
3643                                               dest_off,
3644                                               to_copy);
3645         if (tevent_req_nomem(subreq, req)) {
3646                 return tevent_req_post(req, ev);
3647         }
3648
3649         tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
3650         return req;
3651 }
3652
3653 static void fruit_copy_chunk_done(struct tevent_req *subreq)
3654 {
3655         struct tevent_req *req = tevent_req_callback_data(
3656                 subreq, struct tevent_req);
3657         struct fruit_copy_chunk_state *state = tevent_req_data(
3658                 req, struct fruit_copy_chunk_state);
3659         NTSTATUS status;
3660         unsigned int num_streams = 0;
3661         struct stream_struct *streams = NULL;
3662         int i;
3663         struct smb_filename *src_fname_tmp = NULL;
3664         struct smb_filename *dst_fname_tmp = NULL;
3665
3666         status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
3667                                               subreq,
3668                                               &state->copied);
3669         TALLOC_FREE(subreq);
3670         if (tevent_req_nterror(req, status)) {
3671                 return;
3672         }
3673
3674         if (!state->is_copyfile) {
3675                 tevent_req_done(req);
3676                 return;
3677         }
3678
3679         /*
3680          * Now copy all reamining streams. We know the share supports
3681          * streams, because we're in vfs_fruit. We don't do this async
3682          * because streams are few and small.
3683          */
3684         status = vfs_streaminfo(state->handle->conn, NULL,
3685                                 state->src_fsp->fsp_name->base_name,
3686                                 req, &num_streams, &streams);
3687         if (tevent_req_nterror(req, status)) {
3688                 return;
3689         }
3690
3691         if (num_streams == 1) {
3692                 /* There is always one stream, ::$DATA. */
3693                 tevent_req_done(req);
3694                 return;
3695         }
3696
3697         for (i = 0; i < num_streams; i++) {
3698                 DEBUG(10, ("%s: stream: '%s'/%ju\n",
3699                            __func__, streams[i].name,
3700                            (uintmax_t)streams[i].size));
3701
3702                 src_fname_tmp = synthetic_smb_fname(
3703                         req,
3704                         state->src_fsp->fsp_name->base_name,
3705                         streams[i].name,
3706                         NULL);
3707                 if (tevent_req_nomem(src_fname_tmp, req)) {
3708                         return;
3709                 }
3710
3711                 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
3712                         TALLOC_FREE(src_fname_tmp);
3713                         continue;
3714                 }
3715
3716                 dst_fname_tmp = synthetic_smb_fname(
3717                         req,
3718                         state->dst_fsp->fsp_name->base_name,
3719                         streams[i].name,
3720                         NULL);
3721                 if (tevent_req_nomem(dst_fname_tmp, req)) {
3722                         TALLOC_FREE(src_fname_tmp);
3723                         return;
3724                 }
3725
3726                 status = copy_file(req,
3727                                    state->handle->conn,
3728                                    src_fname_tmp,
3729                                    dst_fname_tmp,
3730                                    OPENX_FILE_CREATE_IF_NOT_EXIST,
3731                                    0, false);
3732                 if (!NT_STATUS_IS_OK(status)) {
3733                         DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
3734                                   smb_fname_str_dbg(src_fname_tmp),
3735                                   smb_fname_str_dbg(dst_fname_tmp),
3736                                   nt_errstr(status)));
3737                         TALLOC_FREE(src_fname_tmp);
3738                         TALLOC_FREE(dst_fname_tmp);
3739                         tevent_req_nterror(req, status);
3740                         return;
3741                 }
3742
3743                 TALLOC_FREE(src_fname_tmp);
3744                 TALLOC_FREE(dst_fname_tmp);
3745         }
3746
3747         TALLOC_FREE(streams);
3748         TALLOC_FREE(src_fname_tmp);
3749         TALLOC_FREE(dst_fname_tmp);
3750         tevent_req_done(req);
3751 }
3752
3753 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
3754                                       struct tevent_req *req,
3755                                       off_t *copied)
3756 {
3757         struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
3758                 req, struct fruit_copy_chunk_state);
3759         NTSTATUS status;
3760
3761         if (tevent_req_is_nterror(req, &status)) {
3762                 DEBUG(1, ("server side copy chunk failed: %s\n",
3763                           nt_errstr(status)));
3764                 *copied = 0;
3765                 tevent_req_received(req);
3766                 return status;
3767         }
3768
3769         *copied = fruit_copy_chunk_state->copied;
3770         tevent_req_received(req);
3771
3772         return NT_STATUS_OK;
3773 }
3774
3775 static struct vfs_fn_pointers vfs_fruit_fns = {
3776         .connect_fn = fruit_connect,
3777
3778         /* File operations */
3779         .chmod_fn = fruit_chmod,
3780         .chown_fn = fruit_chown,
3781         .unlink_fn = fruit_unlink,
3782         .rename_fn = fruit_rename,
3783         .rmdir_fn = fruit_rmdir,
3784         .open_fn = fruit_open,
3785         .pread_fn = fruit_pread,
3786         .pwrite_fn = fruit_pwrite,
3787         .stat_fn = fruit_stat,
3788         .lstat_fn = fruit_lstat,
3789         .fstat_fn = fruit_fstat,
3790         .streaminfo_fn = fruit_streaminfo,
3791         .ntimes_fn = fruit_ntimes,
3792         .ftruncate_fn = fruit_ftruncate,
3793         .fallocate_fn = fruit_fallocate,
3794         .create_file_fn = fruit_create_file,
3795         .readdir_attr_fn = fruit_readdir_attr,
3796         .copy_chunk_send_fn = fruit_copy_chunk_send,
3797         .copy_chunk_recv_fn = fruit_copy_chunk_recv,
3798
3799         /* NT ACL operations */
3800         .fget_nt_acl_fn = fruit_fget_nt_acl,
3801         .fset_nt_acl_fn = fruit_fset_nt_acl,
3802 };
3803
3804 NTSTATUS vfs_fruit_init(void);
3805 NTSTATUS vfs_fruit_init(void)
3806 {
3807         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3808                                         &vfs_fruit_fns);
3809         if (!NT_STATUS_IS_OK(ret)) {
3810                 return ret;
3811         }
3812
3813         vfs_fruit_debug_level = debug_add_class("fruit");
3814         if (vfs_fruit_debug_level == -1) {
3815                 vfs_fruit_debug_level = DBGC_VFS;
3816                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3817                           "vfs_fruit_init"));
3818         } else {
3819                 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3820                            "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3821         }
3822
3823         return ret;
3824 }