vfs_fruit: deal with vfs_catia not being loaded
[metze/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
32 /*
33  * Enhanced OS X and Netatalk compatibility
34  * ========================================
35  *
36  * This modules takes advantage of vfs_streams_xattr and
37  * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
38  * loaded in the correct order:
39  *
40  *   vfs modules = catia fruit streams_xattr
41  *
42  * The module intercepts the OS X special streams "AFP_AfpInfo" and
43  * "AFP_Resource" and handles them in a special way. All other named
44  * streams are deferred to vfs_streams_xattr.
45  *
46  * The OS X client maps all NTFS illegal characters to the Unicode
47  * private range. This module optionally stores the charcters using
48  * their native ASCII encoding using vfs_catia. If you're not enabling
49  * this feature, you can skip catia from vfs modules.
50  *
51  * Finally, open modes are optionally checked against Netatalk AFP
52  * share modes.
53  *
54  * The "AFP_AfpInfo" named stream is a binary blob containing OS X
55  * extended metadata for files and directories. This module optionally
56  * reads and stores this metadata in a way compatible with Netatalk 3
57  * which stores the metadata in an EA "org.netatalk.metadata". Cf
58  * source3/include/MacExtensions.h for a description of the binary
59  * blobs content.
60  *
61  * The "AFP_Resource" named stream may be arbitrarily large, thus it
62  * can't be stored in an xattr on most filesystem. ZFS on Solaris is
63  * the only available filesystem where xattrs can be of any size and
64  * the OS supports using the file APIs for xattrs.
65  *
66  * The AFP_Resource stream is stored in an AppleDouble file prepending
67  * "._" to the filename. On Solaris with ZFS the stream is optionally
68  * stored in an EA "org.netatalk.ressource".
69  *
70  *
71  * Extended Attributes
72  * ===================
73  *
74  * The OS X SMB client sends xattrs as ADS too. For xattr interop with
75  * other protocols you may want to adjust the xattr names the VFS
76  * module vfs_streams_xattr uses for storing ADS's. This defaults to
77  * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
78  * these module parameters:
79  *
80  *   streams_xattr:prefix = user.
81  *   streams_xattr:store_stream_type = false
82  *
83  *
84  * TODO
85  * ====
86  *
87  * - log diagnostic if any needed VFS module is not loaded
88  *   (eg with lp_vfs_objects())
89  * - add tests
90  */
91
92 static int vfs_fruit_debug_level = DBGC_VFS;
93
94 #undef DBGC_CLASS
95 #define DBGC_CLASS vfs_fruit_debug_level
96
97 #define FRUIT_PARAM_TYPE_NAME "fruit"
98 #define ADOUBLE_NAME_PREFIX "._"
99
100 /*
101  * REVIEW:
102  * This is hokey, but what else can we do?
103  */
104 #if defined(HAVE_ATTROPEN) || defined(FREEBSD)
105 #define AFPINFO_EA_NETATALK "org.netatalk.Metadata"
106 #define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
107 #else
108 #define AFPINFO_EA_NETATALK "user.org.netatalk.Metadata"
109 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
110 #endif
111
112 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
113
114 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
115 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
116 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
117 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
118
119 struct fruit_config_data {
120         enum fruit_rsrc rsrc;
121         enum fruit_meta meta;
122         enum fruit_locking locking;
123         enum fruit_encoding encoding;
124 };
125
126 static const struct enum_list fruit_rsrc[] = {
127         {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
128         {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
129         {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
130         { -1, NULL}
131 };
132
133 static const struct enum_list fruit_meta[] = {
134         {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
135         {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
136         { -1, NULL}
137 };
138
139 static const struct enum_list fruit_locking[] = {
140         {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
141         {FRUIT_LOCKING_NONE, "none"},
142         { -1, NULL}
143 };
144
145 static const struct enum_list fruit_encoding[] = {
146         {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
147         {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
148         { -1, NULL}
149 };
150
151 /*****************************************************************************
152  * Defines, functions and data structures that deal with AppleDouble
153  *****************************************************************************/
154
155 /*
156  * There are two AppleDouble blobs we deal with:
157  *
158  * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
159  *   metadata in an xattr
160  *
161  * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
162  *   ._ files
163  */
164 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
165
166 /* Version info */
167 #define AD_VERSION2     0x00020000
168 #define AD_VERSION      AD_VERSION2
169
170 /*
171  * AppleDouble entry IDs.
172  */
173 #define ADEID_DFORK         1
174 #define ADEID_RFORK         2
175 #define ADEID_NAME          3
176 #define ADEID_COMMENT       4
177 #define ADEID_ICONBW        5
178 #define ADEID_ICONCOL       6
179 #define ADEID_FILEI         7
180 #define ADEID_FILEDATESI    8
181 #define ADEID_FINDERI       9
182 #define ADEID_MACFILEI      10
183 #define ADEID_PRODOSFILEI   11
184 #define ADEID_MSDOSFILEI    12
185 #define ADEID_SHORTNAME     13
186 #define ADEID_AFPFILEI      14
187 #define ADEID_DID           15
188
189 /* Private Netatalk entries */
190 #define ADEID_PRIVDEV       16
191 #define ADEID_PRIVINO       17
192 #define ADEID_PRIVSYN       18
193 #define ADEID_PRIVID        19
194 #define ADEID_MAX           (ADEID_PRIVID + 1)
195
196 /*
197  * These are the real ids for the private entries,
198  * as stored in the adouble file
199  */
200 #define AD_DEV              0x80444556
201 #define AD_INO              0x80494E4F
202 #define AD_SYN              0x8053594E
203 #define AD_ID               0x8053567E
204
205 /* Number of actually used entries */
206 #define ADEID_NUM_XATTR      8
207 #define ADEID_NUM_DOT_UND    2
208 #define ADEID_NUM_RSRC_XATTR 1
209
210 /* AppleDouble magic */
211 #define AD_APPLESINGLE_MAGIC 0x00051600
212 #define AD_APPLEDOUBLE_MAGIC 0x00051607
213 #define AD_MAGIC             AD_APPLEDOUBLE_MAGIC
214
215 /* Sizes of relevant entry bits */
216 #define ADEDLEN_MAGIC       4
217 #define ADEDLEN_VERSION     4
218 #define ADEDLEN_FILLER      16
219 #define AD_FILLER_TAG       "Netatalk        " /* should be 16 bytes */
220 #define ADEDLEN_NENTRIES    2
221 #define AD_HEADER_LEN       (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
222                              ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
223 #define AD_ENTRY_LEN_EID    4
224 #define AD_ENTRY_LEN_OFF    4
225 #define AD_ENTRY_LEN_LEN    4
226 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
227
228 /* Field widths */
229 #define ADEDLEN_NAME            255
230 #define ADEDLEN_COMMENT         200
231 #define ADEDLEN_FILEI           16
232 #define ADEDLEN_FINDERI         32
233 #define ADEDLEN_FILEDATESI      16
234 #define ADEDLEN_SHORTNAME       12 /* length up to 8.3 */
235 #define ADEDLEN_AFPFILEI        4
236 #define ADEDLEN_MACFILEI        4
237 #define ADEDLEN_PRODOSFILEI     8
238 #define ADEDLEN_MSDOSFILEI      2
239 #define ADEDLEN_DID             4
240 #define ADEDLEN_PRIVDEV         8
241 #define ADEDLEN_PRIVINO         8
242 #define ADEDLEN_PRIVSYN         8
243 #define ADEDLEN_PRIVID          4
244
245 /* Offsets */
246 #define ADEDOFF_MAGIC         0
247 #define ADEDOFF_VERSION       (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
248 #define ADEDOFF_FILLER        (ADEDOFF_VERSION + ADEDLEN_VERSION)
249 #define ADEDOFF_NENTRIES      (ADEDOFF_FILLER + ADEDLEN_FILLER)
250
251 #define ADEDOFF_FINDERI_XATTR    (AD_HEADER_LEN + \
252                                   (ADEID_NUM_XATTR * AD_ENTRY_LEN))
253 #define ADEDOFF_COMMENT_XATTR    (ADEDOFF_FINDERI_XATTR    + ADEDLEN_FINDERI)
254 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR    + ADEDLEN_COMMENT)
255 #define ADEDOFF_AFPFILEI_XATTR   (ADEDOFF_FILEDATESI_XATTR + \
256                                   ADEDLEN_FILEDATESI)
257 #define ADEDOFF_PRIVDEV_XATTR    (ADEDOFF_AFPFILEI_XATTR   + ADEDLEN_AFPFILEI)
258 #define ADEDOFF_PRIVINO_XATTR    (ADEDOFF_PRIVDEV_XATTR    + ADEDLEN_PRIVDEV)
259 #define ADEDOFF_PRIVSYN_XATTR    (ADEDOFF_PRIVINO_XATTR    + ADEDLEN_PRIVINO)
260 #define ADEDOFF_PRIVID_XATTR     (ADEDOFF_PRIVSYN_XATTR    + ADEDLEN_PRIVSYN)
261
262 #define ADEDOFF_FINDERI_DOT_UND  (AD_HEADER_LEN + \
263                                   (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
264 #define ADEDOFF_RFORK_DOT_UND    (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
265
266 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
267                          (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
268                          ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
269                          ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
270                          ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
271                          ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
272
273 #if AD_DATASZ_XATTR != 402
274 #error bad size for AD_DATASZ_XATTR
275 #endif
276
277 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
278                            (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
279                            ADEDLEN_FINDERI)
280 #if AD_DATASZ_DOT_UND != 82
281 #error bad size for AD_DATASZ_DOT_UND
282 #endif
283
284 /*
285  * Sharemode locks fcntl() offsets
286  */
287 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
288 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
289 #else
290 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
291 #endif
292 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
293
294 #define AD_FILELOCK_OPEN_WR        (AD_FILELOCK_BASE + 0)
295 #define AD_FILELOCK_OPEN_RD        (AD_FILELOCK_BASE + 1)
296 #define AD_FILELOCK_RSRC_OPEN_WR   (AD_FILELOCK_BASE + 2)
297 #define AD_FILELOCK_RSRC_OPEN_RD   (AD_FILELOCK_BASE + 3)
298 #define AD_FILELOCK_DENY_WR        (AD_FILELOCK_BASE + 4)
299 #define AD_FILELOCK_DENY_RD        (AD_FILELOCK_BASE + 5)
300 #define AD_FILELOCK_RSRC_DENY_WR   (AD_FILELOCK_BASE + 6)
301 #define AD_FILELOCK_RSRC_DENY_RD   (AD_FILELOCK_BASE + 7)
302 #define AD_FILELOCK_OPEN_NONE      (AD_FILELOCK_BASE + 8)
303 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
304
305 /* Time stuff we overload the bits a little */
306 #define AD_DATE_CREATE         0
307 #define AD_DATE_MODIFY         4
308 #define AD_DATE_BACKUP         8
309 #define AD_DATE_ACCESS        12
310 #define AD_DATE_MASK          (AD_DATE_CREATE | AD_DATE_MODIFY | \
311                                AD_DATE_BACKUP | AD_DATE_ACCESS)
312 #define AD_DATE_UNIX          (1 << 10)
313 #define AD_DATE_START         0x80000000
314 #define AD_DATE_DELTA         946684800
315 #define AD_DATE_FROM_UNIX(x)  (htonl((x) - AD_DATE_DELTA))
316 #define AD_DATE_TO_UNIX(x)    (ntohl(x) + AD_DATE_DELTA)
317
318 /* Accessor macros */
319 #define ad_getentrylen(ad,eid)     ((ad)->ad_eid[(eid)].ade_len)
320 #define ad_getentryoff(ad,eid)     ((ad)->ad_eid[(eid)].ade_off)
321 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
322 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
323 #define ad_entry(ad,eid)           ((ad)->ad_data + ad_getentryoff((ad),(eid)))
324
325 struct ad_entry {
326         size_t ade_off;
327         size_t ade_len;
328 };
329
330 struct adouble {
331         vfs_handle_struct        *ad_handle;
332         files_struct             *ad_fsp;
333         adouble_type_t            ad_type;
334         uint32_t                  ad_magic;
335         uint32_t                  ad_version;
336         struct ad_entry           ad_eid[ADEID_MAX];
337         char                     *ad_data;
338 };
339
340 struct ad_entry_order {
341         uint32_t id, offset, len;
342 };
343
344 /* Netatalk AppleDouble metadata xattr */
345 static const
346 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
347         {ADEID_FINDERI,    ADEDOFF_FINDERI_XATTR,    ADEDLEN_FINDERI},
348         {ADEID_COMMENT,    ADEDOFF_COMMENT_XATTR,    0},
349         {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
350         {ADEID_AFPFILEI,   ADEDOFF_AFPFILEI_XATTR,   ADEDLEN_AFPFILEI},
351         {ADEID_PRIVDEV,    ADEDOFF_PRIVDEV_XATTR,    0},
352         {ADEID_PRIVINO,    ADEDOFF_PRIVINO_XATTR,    0},
353         {ADEID_PRIVSYN,    ADEDOFF_PRIVSYN_XATTR,    0},
354         {ADEID_PRIVID,     ADEDOFF_PRIVID_XATTR,     0},
355         {0, 0, 0}
356 };
357
358 /* AppleDouble ressource fork file (the ones prefixed by "._") */
359 static const
360 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
361         {ADEID_FINDERI,    ADEDOFF_FINDERI_DOT_UND,  ADEDLEN_FINDERI},
362         {ADEID_RFORK,      ADEDOFF_RFORK_DOT_UND,    0},
363         {0, 0, 0}
364 };
365
366 /*
367  * Fake AppleDouble entry oder for ressource fork xattr.  The xattr
368  * isn't an AppleDouble file, it simply contains the ressource data,
369  * but in order to be able to use some API calls like ad_getentryoff()
370  * we build a fake/helper struct adouble with this entry order struct.
371  */
372 static const
373 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
374         {ADEID_RFORK, 0, 0},
375         {0, 0, 0}
376 };
377
378 /* Conversion from enumerated id to on-disk AppleDouble id */
379 #define AD_EID_DISK(a) (set_eid[a])
380 static const uint32_t set_eid[] = {
381         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
382         AD_DEV, AD_INO, AD_SYN, AD_ID
383 };
384
385 /*
386  * Forward declarations
387  */
388 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
389                                adouble_type_t type, files_struct *fsp);
390 static int ad_write(struct adouble *ad, const char *path);
391 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
392
393 /**
394  * Get a date
395  **/
396 static int ad_getdate(const struct adouble *ad,
397                       unsigned int dateoff,
398                       uint32_t *date)
399 {
400         bool xlate = (dateoff & AD_DATE_UNIX);
401
402         dateoff &= AD_DATE_MASK;
403         if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
404                 return -1;
405         }
406
407         if (dateoff > AD_DATE_ACCESS) {
408             return -1;
409         }
410         memcpy(date,
411                ad_entry(ad, ADEID_FILEDATESI) + dateoff,
412                sizeof(uint32_t));
413
414         if (xlate) {
415                 *date = AD_DATE_TO_UNIX(*date);
416         }
417         return 0;
418 }
419
420 /**
421  * Set a date
422  **/
423 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
424 {
425         bool xlate = (dateoff & AD_DATE_UNIX);
426
427         if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
428                 return 0;
429         }
430
431         dateoff &= AD_DATE_MASK;
432         if (xlate) {
433                 date = AD_DATE_FROM_UNIX(date);
434         }
435
436         if (dateoff > AD_DATE_ACCESS) {
437                 return -1;
438         }
439
440         memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
441
442         return 0;
443 }
444
445
446 /**
447  * Map on-disk AppleDouble id to enumerated id
448  **/
449 static uint32_t get_eid(uint32_t eid)
450 {
451         if (eid <= 15) {
452                 return eid;
453         }
454
455         switch (eid) {
456         case AD_DEV:
457                 return ADEID_PRIVDEV;
458         case AD_INO:
459                 return ADEID_PRIVINO;
460         case AD_SYN:
461                 return ADEID_PRIVSYN;
462         case AD_ID:
463                 return ADEID_PRIVID;
464         default:
465                 break;
466         }
467
468         return 0;
469 }
470
471 /**
472  * Pack AppleDouble structure into data buffer
473  **/
474 static bool ad_pack(struct adouble *ad)
475 {
476         uint32_t       eid;
477         uint16_t       nent;
478         uint32_t       bufsize;
479         uint32_t       offset = 0;
480
481         bufsize = talloc_get_size(ad->ad_data);
482
483         if (offset + ADEDLEN_MAGIC < offset ||
484                         offset + ADEDLEN_MAGIC >= bufsize) {
485                 return false;
486         }
487         RSIVAL(ad->ad_data, offset, ad->ad_magic);
488         offset += ADEDLEN_MAGIC;
489
490         if (offset + ADEDLEN_VERSION < offset ||
491                         offset + ADEDLEN_VERSION >= bufsize) {
492                 return false;
493         }
494         RSIVAL(ad->ad_data, offset, ad->ad_version);
495         offset += ADEDLEN_VERSION;
496
497         if (offset + ADEDLEN_FILLER < offset ||
498                         offset + ADEDLEN_FILLER >= bufsize) {
499                 return false;
500         }
501         if (ad->ad_type == ADOUBLE_RSRC) {
502                 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
503         }
504         offset += ADEDLEN_FILLER;
505
506         if (offset + ADEDLEN_NENTRIES < offset ||
507                         offset + ADEDLEN_NENTRIES >= bufsize) {
508                 return false;
509         }
510         offset += ADEDLEN_NENTRIES;
511
512         for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
513                 if ((ad->ad_eid[eid].ade_off == 0)) {
514                         /*
515                          * ade_off is also used as indicator whether a
516                          * specific entry is used or not
517                          */
518                         continue;
519                 }
520
521                 if (offset + AD_ENTRY_LEN_EID < offset ||
522                                 offset + AD_ENTRY_LEN_EID >= bufsize) {
523                         return false;
524                 }
525                 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
526                 offset += AD_ENTRY_LEN_EID;
527
528                 if (offset + AD_ENTRY_LEN_OFF < offset ||
529                                 offset + AD_ENTRY_LEN_OFF >= bufsize) {
530                         return false;
531                 }
532                 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
533                 offset += AD_ENTRY_LEN_OFF;
534
535                 if (offset + AD_ENTRY_LEN_LEN < offset ||
536                                 offset + AD_ENTRY_LEN_LEN >= bufsize) {
537                         return false;
538                 }
539                 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
540                 offset += AD_ENTRY_LEN_LEN;
541
542                 nent++;
543         }
544
545         if (ADEDOFF_NENTRIES + 2 >= bufsize) {
546                 return false;
547         }
548         RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
549
550         return 0;
551 }
552
553 /**
554  * Unpack an AppleDouble blob into a struct adoble
555  **/
556 static bool ad_unpack(struct adouble *ad, const int nentries)
557 {
558         size_t bufsize = talloc_get_size(ad->ad_data);
559         int adentries, i;
560         uint32_t eid, len, off;
561
562         /*
563          * The size of the buffer ad->ad_data is checked when read, so
564          * we wouldn't have to check our own offsets, a few extra
565          * checks won't hurt though. We have to check the offsets we
566          * read from the buffer anyway.
567          */
568
569         if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
570                 DEBUG(1, ("bad size\n"));
571                 return false;
572         }
573
574         ad->ad_magic = RIVAL(ad->ad_data, 0);
575         ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
576         if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
577                 DEBUG(1, ("wrong magic or version\n"));
578                 return false;
579         }
580
581         adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
582         if (adentries != nentries) {
583                 DEBUG(1, ("invalid number of entries: %d\n", adentries));
584                 return false;
585         }
586
587         /* now, read in the entry bits */
588         for (i = 0; i < adentries; i++) {
589                 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
590                 eid = get_eid(eid);
591                 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
592                 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
593
594                 if (!eid || eid > ADEID_MAX) {
595                         DEBUG(1, ("bogus eid %d\n", eid));
596                         return false;
597                 }
598
599                 if ((off > bufsize) && (eid != ADEID_RFORK)) {
600                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
601                                   eid, off, len));
602                         return false;
603                 }
604                 if ((eid != ADEID_RFORK) &&
605                     (eid != ADEID_FINDERI) &&
606                     ((off + len) > bufsize)) {
607                         DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
608                                   eid, off, len));
609                         return false;
610                 }
611
612                 ad->ad_eid[eid].ade_off = off;
613                 ad->ad_eid[eid].ade_len = len;
614         }
615
616         return true;
617 }
618
619 /**
620  * Convert from Apple's ._ file to Netatalk
621  *
622  * Apple's AppleDouble may contain a FinderInfo entry longer then 32
623  * bytes containing packed xattrs. Netatalk can't deal with that, so
624  * we simply discard the packed xattrs.
625  *
626  * @return -1 in case an error occured, 0 if no conversion was done, 1
627  * otherwise
628  **/
629 static int ad_convert(struct adouble *ad, int fd)
630 {
631         int rc = 0;
632         char *map = MAP_FAILED;
633         size_t origlen;
634
635         origlen = ad_getentryoff(ad, ADEID_RFORK) +
636                 ad_getentrylen(ad, ADEID_RFORK);
637
638         /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
639         map = mmap(NULL, origlen, PROT_WRITE, MAP_SHARED, fd, 0);
640         if (map == MAP_FAILED) {
641                 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
642                 rc = -1;
643                 goto exit;
644         }
645
646         memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
647                 map + ad_getentryoff(ad, ADEID_RFORK),
648                 ad_getentrylen(ad, ADEID_RFORK));
649
650         ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
651         ad_setentryoff(ad, ADEID_RFORK,
652                        ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
653
654         /*
655          * FIXME: direct ftruncate(), but we don't have a fsp for the
656          * VFS call
657          */
658         rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
659                        + ad_getentrylen(ad, ADEID_RFORK));
660
661 exit:
662         if (map != MAP_FAILED) {
663                 munmap(map, origlen);
664         }
665         return rc;
666 }
667
668 /**
669  * Read and parse Netatalk AppleDouble metadata xattr
670  **/
671 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
672 {
673         int      rc = 0;
674         ssize_t  ealen;
675         bool     ok;
676
677         DEBUG(10, ("reading meta xattr for %s\n", path));
678
679         ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
680                                  AFPINFO_EA_NETATALK, ad->ad_data,
681                                  AD_DATASZ_XATTR);
682         if (ealen == -1) {
683                 switch (errno) {
684                 case ENOATTR:
685                 case ENOENT:
686                         if (errno == ENOATTR) {
687                                 errno = ENOENT;
688                         }
689                         rc = -1;
690                         goto exit;
691                 default:
692                         DEBUG(2, ("error reading meta xattr: %s\n",
693                                   strerror(errno)));
694                         rc = -1;
695                         goto exit;
696                 }
697         }
698         if (ealen != AD_DATASZ_XATTR) {
699                 DEBUG(2, ("bad size %zd\n", ealen));
700                 errno = EINVAL;
701                 rc = -1;
702                 goto exit;
703         }
704
705         /* Now parse entries */
706         ok = ad_unpack(ad, ADEID_NUM_XATTR);
707         if (!ok) {
708                 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
709                 errno = EINVAL;
710                 rc = -1;
711                 goto exit;
712         }
713
714         if (!ad_getentryoff(ad, ADEID_FINDERI)
715             || !ad_getentryoff(ad, ADEID_COMMENT)
716             || !ad_getentryoff(ad, ADEID_FILEDATESI)
717             || !ad_getentryoff(ad, ADEID_AFPFILEI)
718             || !ad_getentryoff(ad, ADEID_PRIVDEV)
719             || !ad_getentryoff(ad, ADEID_PRIVINO)
720             || !ad_getentryoff(ad, ADEID_PRIVSYN)
721             || !ad_getentryoff(ad, ADEID_PRIVID)) {
722                 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
723                 errno = EINVAL;
724                 rc = -1;
725                 goto exit;
726         }
727
728 exit:
729         DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
730
731         if (rc != 0) {
732                 ealen = -1;
733                 if (errno == EINVAL) {
734                         become_root();
735                         removexattr(path, AFPINFO_EA_NETATALK);
736                         unbecome_root();
737                         errno = ENOENT;
738                 }
739         }
740         return ealen;
741 }
742
743 /**
744  * Read and parse resource fork, either ._ AppleDouble file or xattr
745  **/
746 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
747 {
748         struct fruit_config_data *config = NULL;
749         int fd = -1;
750         int rc = 0;
751         ssize_t len;
752         char *adpath = NULL;
753         bool opened = false;
754         int mode;
755         struct adouble *meta_ad = NULL;
756         SMB_STRUCT_STAT sbuf;
757         bool ok;
758         int saved_errno = 0;
759
760         SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
761                                 struct fruit_config_data, return -1);
762
763         /* Try rw first so we can use the fd in ad_convert() */
764         mode = O_RDWR;
765
766         if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
767                 fd = ad->ad_fsp->fh->fd;
768         } else {
769                 if (config->rsrc == FRUIT_RSRC_XATTR) {
770                         adpath = talloc_strdup(talloc_tos(), path);
771                 } else {
772                         rc = adouble_path(talloc_tos(), path, &adpath);
773                         if (rc != 0) {
774                                 goto exit;
775                         }
776                 }
777
778         retry:
779                 if (config->rsrc == FRUIT_RSRC_XATTR) {
780 #ifndef HAVE_ATTROPEN
781                         errno = ENOSYS;
782                         rc = -1;
783                         goto exit;
784 #else
785                         /* FIXME: direct Solaris xattr syscall */
786                         fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
787                                       mode, 0);
788 #endif
789                 } else {
790                         /* FIXME: direct open(), don't have an fsp */
791                         fd = open(adpath, mode);
792                 }
793
794                 if (fd == -1) {
795                         switch (errno) {
796                         case EROFS:
797                         case EACCES:
798                                 if (mode == O_RDWR) {
799                                         mode = O_RDONLY;
800                                         goto retry;
801                                 }
802                                 /* fall through ... */
803                         default:
804                                 DEBUG(2, ("open AppleDouble: %s, %s\n",
805                                           adpath, strerror(errno)));
806                                 rc = -1;
807                                 goto exit;
808                         }
809                 }
810                 opened = true;
811         }
812
813         if (config->rsrc == FRUIT_RSRC_XATTR) {
814                 /* FIXME: direct sys_fstat(), don't have an fsp */
815                 rc = sys_fstat(
816                         fd, &sbuf,
817                         lp_fake_directory_create_times(
818                                 SNUM(ad->ad_handle->conn)));
819                 if (rc != 0) {
820                         goto exit;
821                 }
822                 ad_setentrylen(ad, ADEID_RFORK, sbuf.st_ex_size);
823         } else {
824                 /* FIXME: direct sys_pread(), don't have an fsp */
825                 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
826                 if (len != AD_DATASZ_DOT_UND) {
827                         DEBUG(2, ("%s: bad size: %zd\n",
828                                   strerror(errno), len));
829                         rc = -1;
830                         goto exit;
831                 }
832
833                 /* Now parse entries */
834                 ok = ad_unpack(ad, ADEID_NUM_DOT_UND);
835                 if (!ok) {
836                         DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
837                         errno = EINVAL;
838                         rc = -1;
839                         goto exit;
840                 }
841
842                 if ((ad_getentryoff(ad, ADEID_FINDERI)
843                      != ADEDOFF_FINDERI_DOT_UND)
844                     || (ad_getentrylen(ad, ADEID_FINDERI)
845                         < ADEDLEN_FINDERI)
846                     || (ad_getentryoff(ad, ADEID_RFORK)
847                         < ADEDOFF_RFORK_DOT_UND)) {
848                         DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
849                         errno = EINVAL;
850                         rc = -1;
851                         goto exit;
852                 }
853
854                 if ((mode == O_RDWR)
855                     && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
856                         rc = ad_convert(ad, fd);
857                         if (rc != 0) {
858                                 rc = -1;
859                                 goto exit;
860                         }
861                         /*
862                          * Can't use ad_write() because we might not have a fsp
863                          */
864                         rc = ad_pack(ad);
865                         if (rc != 0) {
866                                 goto exit;
867                         }
868                         /* FIXME: direct sys_pwrite(), don't have an fsp */
869                         len = sys_pwrite(fd, ad->ad_data,
870                                          AD_DATASZ_DOT_UND, 0);
871                         if (len != AD_DATASZ_DOT_UND) {
872                                 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
873                                 rc = -1;
874                                 goto exit;
875                         }
876
877                         meta_ad = ad_init(talloc_tos(), ad->ad_handle,
878                                           ADOUBLE_META, NULL);
879                         if (meta_ad == NULL) {
880                                 rc = -1;
881                                 goto exit;
882                         }
883
884                         memcpy(ad_entry(meta_ad, ADEID_FINDERI),
885                                ad_entry(ad, ADEID_FINDERI),
886                                ADEDLEN_FINDERI);
887
888                         rc = ad_write(meta_ad, path);
889                         if (rc != 0) {
890                                 rc = -1;
891                                 goto exit;
892                         }
893                 }
894         }
895
896         DEBUG(10, ("opened AppleDouble: %s\n", path));
897
898 exit:
899         if (rc != 0) {
900                 saved_errno = errno;
901                 len = -1;
902         }
903         if (opened && fd != -1) {
904                 close(fd);
905         }
906         TALLOC_FREE(adpath);
907         TALLOC_FREE(meta_ad);
908         if (rc != 0) {
909                 errno = saved_errno;
910         }
911         return len;
912 }
913
914 /**
915  * Read and unpack an AppleDouble metadata xattr or resource
916  **/
917 static ssize_t ad_read(struct adouble *ad, const char *path)
918 {
919         switch (ad->ad_type) {
920         case ADOUBLE_META:
921                 return ad_header_read_meta(ad, path);
922         case ADOUBLE_RSRC:
923                 return ad_header_read_rsrc(ad, path);
924         default:
925                 return -1;
926         }
927 }
928
929 /**
930  * Allocate a struct adouble without initialiing it
931  *
932  * The struct is either hang of the fsp extension context or if fsp is
933  * NULL from ctx.
934  *
935  * @param[in] ctx        talloc context
936  * @param[in] handle     vfs handle
937  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
938
939  * @param[in] fsp        if not NULL (for stream IO), the adouble handle is
940  *                       added as an fsp extension
941  *
942  * @return               adouble handle
943  **/
944 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
945                                 adouble_type_t type, files_struct *fsp)
946 {
947         int rc = 0;
948         size_t adsize = 0;
949         struct adouble *ad;
950         struct fruit_config_data *config;
951
952         SMB_VFS_HANDLE_GET_DATA(handle, config,
953                                 struct fruit_config_data, return NULL);
954
955         switch (type) {
956         case ADOUBLE_META:
957                 adsize = AD_DATASZ_XATTR;
958                 break;
959         case ADOUBLE_RSRC:
960                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
961                         adsize = AD_DATASZ_DOT_UND;
962                 }
963                 break;
964         default:
965                 return NULL;
966         }
967
968         if (!fsp) {
969                 ad = talloc_zero(ctx, struct adouble);
970                 if (ad == NULL) {
971                         rc = -1;
972                         goto exit;
973                 }
974                 if (adsize) {
975                         ad->ad_data = talloc_zero_array(ad, char, adsize);
976                 }
977         } else {
978                 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
979                                                              struct adouble,
980                                                              NULL);
981                 if (ad == NULL) {
982                         rc = -1;
983                         goto exit;
984                 }
985                 if (adsize) {
986                         ad->ad_data = talloc_zero_array(
987                                 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
988                                 char, adsize);
989                 }
990                 ad->ad_fsp = fsp;
991         }
992
993         if (adsize && ad->ad_data == NULL) {
994                 rc = -1;
995                 goto exit;
996         }
997         ad->ad_handle = handle;
998         ad->ad_type = type;
999         ad->ad_magic = AD_MAGIC;
1000         ad->ad_version = AD_VERSION;
1001
1002 exit:
1003         if (rc != 0) {
1004                 TALLOC_FREE(ad);
1005         }
1006         return ad;
1007 }
1008
1009 /**
1010  * Allocate and initialize a new struct adouble
1011  *
1012  * @param[in] ctx        talloc context
1013  * @param[in] handle     vfs handle
1014  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1015  * @param[in] fsp        file handle, may be NULL for a type of e_ad_meta
1016  *
1017  * @return               adouble handle, initialized
1018  **/
1019 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1020                                adouble_type_t type, files_struct *fsp)
1021 {
1022         int rc = 0;
1023         const struct ad_entry_order  *eid;
1024         struct adouble *ad = NULL;
1025         struct fruit_config_data *config;
1026         time_t t = time(NULL);
1027
1028         SMB_VFS_HANDLE_GET_DATA(handle, config,
1029                                 struct fruit_config_data, return NULL);
1030
1031         switch (type) {
1032         case ADOUBLE_META:
1033                 eid = entry_order_meta_xattr;
1034                 break;
1035         case ADOUBLE_RSRC:
1036                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1037                         eid = entry_order_dot_und;
1038                 } else {
1039                         eid = entry_order_rsrc_xattr;
1040                 }
1041                 break;
1042         default:
1043                 return NULL;
1044         }
1045
1046         ad = ad_alloc(ctx, handle, type, fsp);
1047         if (ad == NULL) {
1048                 return NULL;
1049         }
1050
1051         while (eid->id) {
1052                 ad->ad_eid[eid->id].ade_off = eid->offset;
1053                 ad->ad_eid[eid->id].ade_len = eid->len;
1054                 eid++;
1055         }
1056
1057         /* put something sane in the date fields */
1058         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1059         ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1060         ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1061         ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1062
1063         if (rc != 0) {
1064                 TALLOC_FREE(ad);
1065         }
1066         return ad;
1067 }
1068
1069 /**
1070  * Return AppleDouble data for a file
1071  *
1072  * @param[in] ctx      talloc context
1073  * @param[in] handle   vfs handle
1074  * @param[in] path     pathname to file or directory
1075  * @param[in] type     type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1076  *
1077  * @return             talloced struct adouble or NULL on error
1078  **/
1079 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1080                               const char *path, adouble_type_t type)
1081 {
1082         int rc = 0;
1083         ssize_t len;
1084         struct adouble *ad = NULL;
1085
1086         DEBUG(10, ("ad_get(%s) called for %s\n",
1087                    type == ADOUBLE_META ? "meta" : "rsrc", path));
1088
1089         ad = ad_alloc(ctx, handle, type, NULL);
1090         if (ad == NULL) {
1091                 rc = -1;
1092                 goto exit;
1093         }
1094
1095         len = ad_read(ad, path);
1096         if (len == -1) {
1097                 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1098                 rc = -1;
1099                 goto exit;
1100         }
1101
1102 exit:
1103         DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1104                   type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1105
1106         if (rc != 0) {
1107                 TALLOC_FREE(ad);
1108         }
1109         return ad;
1110 }
1111
1112 /**
1113  * Set AppleDouble metadata on a file or directory
1114  *
1115  * @param[in] ad      adouble handle
1116
1117  * @param[in] path    pathname to file or directory, may be NULL for a
1118  *                    resource fork
1119  *
1120  * @return            status code, 0 means success
1121  **/
1122 static int ad_write(struct adouble *ad, const char *path)
1123 {
1124         int rc = 0;
1125         ssize_t len;
1126
1127         rc = ad_pack(ad);
1128         if (rc != 0) {
1129                 goto exit;
1130         }
1131
1132         switch (ad->ad_type) {
1133         case ADOUBLE_META:
1134                 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1135                                       AFPINFO_EA_NETATALK, ad->ad_data,
1136                                       AD_DATASZ_XATTR, 0);
1137                 break;
1138         case ADOUBLE_RSRC:
1139                 if ((ad->ad_fsp == NULL)
1140                     || (ad->ad_fsp->fh == NULL)
1141                     || (ad->ad_fsp->fh->fd == -1)) {
1142                         rc = -1;
1143                         goto exit;
1144                 }
1145                 /* FIXME: direct sys_pwrite(), don't have an fsp */
1146                 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1147                                  talloc_get_size(ad->ad_data), 0);
1148                 if (len != talloc_get_size(ad->ad_data)) {
1149                         DEBUG(1, ("short write on %s: %zd",
1150                                   fsp_str_dbg(ad->ad_fsp), len));
1151                         rc = -1;
1152                         goto exit;
1153                 }
1154                 break;
1155         default:
1156                 return -1;
1157         }
1158 exit:
1159         return rc;
1160 }
1161
1162 /*****************************************************************************
1163  * Helper functions
1164  *****************************************************************************/
1165
1166 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1167 {
1168         if (strncasecmp_m(smb_fname->stream_name,
1169                           AFPINFO_STREAM_NAME,
1170                           strlen(AFPINFO_STREAM_NAME)) == 0) {
1171                 return true;
1172         }
1173         return false;
1174 }
1175
1176 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1177 {
1178         if (strncasecmp_m(smb_fname->stream_name,
1179                           AFPRESOURCE_STREAM_NAME,
1180                           strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1181                 return true;
1182         }
1183         return false;
1184 }
1185
1186 /**
1187  * Test whether stream is an Apple stream, not used atm
1188  **/
1189 #if 0
1190 static bool is_apple_stream(const struct smb_filename *smb_fname)
1191 {
1192         if (is_afpinfo_stream(smb_fname)) {
1193                 return true;
1194         }
1195         if (is_afpresource_stream(smb_fname)) {
1196                 return true;
1197         }
1198         return false;
1199 }
1200 #endif
1201
1202 /**
1203  * Initialize config struct from our smb.conf config parameters
1204  **/
1205 static int init_fruit_config(vfs_handle_struct *handle)
1206 {
1207         struct fruit_config_data *config;
1208         int enumval;
1209
1210         config = talloc_zero(handle->conn, struct fruit_config_data);
1211         if (!config) {
1212                 DEBUG(1, ("talloc_zero() failed\n"));
1213                 errno = ENOMEM;
1214                 return -1;
1215         }
1216
1217         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1218                                "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1219         if (enumval == -1) {
1220                 DEBUG(1, ("value for %s: ressource type unknown\n",
1221                           FRUIT_PARAM_TYPE_NAME));
1222                 return -1;
1223         }
1224         config->rsrc = (enum fruit_rsrc)enumval;
1225
1226         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1227                                "metadata", fruit_meta, FRUIT_META_NETATALK);
1228         if (enumval == -1) {
1229                 DEBUG(1, ("value for %s: metadata type unknown\n",
1230                           FRUIT_PARAM_TYPE_NAME));
1231                 return -1;
1232         }
1233         config->meta = (enum fruit_meta)enumval;
1234
1235         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1236                                "locking", fruit_locking, FRUIT_LOCKING_NONE);
1237         if (enumval == -1) {
1238                 DEBUG(1, ("value for %s: locking type unknown\n",
1239                           FRUIT_PARAM_TYPE_NAME));
1240                 return -1;
1241         }
1242         config->locking = (enum fruit_locking)enumval;
1243
1244         enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1245                                "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1246         if (enumval == -1) {
1247                 DEBUG(1, ("value for %s: encoding type unknown\n",
1248                           FRUIT_PARAM_TYPE_NAME));
1249                 return -1;
1250         }
1251         config->encoding = (enum fruit_encoding)enumval;
1252
1253         SMB_VFS_HANDLE_SET_DATA(handle, config,
1254                                 NULL, struct fruit_config_data,
1255                                 return -1);
1256
1257         return 0;
1258 }
1259
1260 /**
1261  * Prepend "._" to a basename
1262  **/
1263 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1264 {
1265         char *parent;
1266         const char *basename;
1267
1268         if (!parent_dirname(ctx, path_in, &parent, &basename)) {
1269                 return -1;
1270         }
1271
1272         *path_out = talloc_asprintf(ctx, "%s/._%s", parent, basename);
1273         if (*path_out == NULL) {
1274                 return -1;
1275         }
1276
1277         return 0;
1278 }
1279
1280 /**
1281  * Allocate and initialize an AfpInfo struct
1282  **/
1283 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1284 {
1285         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1286         if (ai == NULL) {
1287                 return NULL;
1288         }
1289         ai->afpi_Signature = AFP_Signature;
1290         ai->afpi_Version = AFP_Version;
1291         ai->afpi_BackupTime = AD_DATE_START;
1292         return ai;
1293 }
1294
1295 /**
1296  * Pack an AfpInfo struct into a buffer
1297  *
1298  * Buffer size must be at least AFP_INFO_SIZE
1299  * Returns size of packed buffer
1300  **/
1301 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1302 {
1303         memset(buf, 0, AFP_INFO_SIZE);
1304
1305         RSIVAL(buf, 0, ai->afpi_Signature);
1306         RSIVAL(buf, 4, ai->afpi_Version);
1307         RSIVAL(buf, 12, ai->afpi_BackupTime);
1308         memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1309
1310         return AFP_INFO_SIZE;
1311 }
1312
1313 /**
1314  * Unpack a buffer into a AfpInfo structure
1315  *
1316  * Buffer size must be at least AFP_INFO_SIZE
1317  * Returns allocated AfpInfo struct
1318  **/
1319 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1320 {
1321         AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1322         if (ai == NULL) {
1323                 return NULL;
1324         }
1325
1326         ai->afpi_Signature = RIVAL(data, 0);
1327         ai->afpi_Version = RIVAL(data, 4);
1328         ai->afpi_BackupTime = RIVAL(data, 12);
1329         memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1330                sizeof(ai->afpi_FinderInfo));
1331
1332         if (ai->afpi_Signature != AFP_Signature
1333             || ai->afpi_Version != AFP_Version) {
1334                 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1335                 TALLOC_FREE(ai);
1336         }
1337
1338         return ai;
1339 }
1340
1341 /**
1342  * Fake an inode number from the md5 hash of the (xattr) name
1343  **/
1344 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1345 {
1346         MD5_CTX ctx;
1347         unsigned char hash[16];
1348         SMB_INO_T result;
1349         char *upper_sname;
1350
1351         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1352         SMB_ASSERT(upper_sname != NULL);
1353
1354         MD5Init(&ctx);
1355         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1356                   sizeof(sbuf->st_ex_dev));
1357         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1358                   sizeof(sbuf->st_ex_ino));
1359         MD5Update(&ctx, (unsigned char *)upper_sname,
1360                   talloc_get_size(upper_sname)-1);
1361         MD5Final(hash, &ctx);
1362
1363         TALLOC_FREE(upper_sname);
1364
1365         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1366         memcpy(&result, hash, sizeof(result));
1367
1368         DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1369                    sname, (unsigned long long)result));
1370
1371         return result;
1372 }
1373
1374 /**
1375  * Ensure ad_fsp is still valid
1376  **/
1377 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1378 {
1379         if (ad->ad_fsp == fsp) {
1380                 return true;
1381         }
1382         ad->ad_fsp = fsp;
1383
1384         return true;
1385 }
1386
1387 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1388                              struct stream_struct **streams,
1389                              const char *name, off_t size,
1390                              off_t alloc_size)
1391 {
1392         struct stream_struct *tmp;
1393
1394         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1395                              (*num_streams)+1);
1396         if (tmp == NULL) {
1397                 return false;
1398         }
1399
1400         tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1401         if (tmp[*num_streams].name == NULL) {
1402                 return false;
1403         }
1404
1405         tmp[*num_streams].size = size;
1406         tmp[*num_streams].alloc_size = alloc_size;
1407
1408         *streams = tmp;
1409         *num_streams += 1;
1410         return true;
1411 }
1412
1413 static bool empty_finderinfo(const struct adouble *ad)
1414 {
1415
1416         char emptybuf[ADEDLEN_FINDERI] = {0};
1417         if (memcmp(emptybuf,
1418                    ad_entry(ad, ADEID_FINDERI),
1419                    ADEDLEN_FINDERI) == 0) {
1420                 return true;
1421         }
1422         return false;
1423 }
1424
1425 /**
1426  * Update btime with btime from Netatalk
1427  **/
1428 static void update_btime(vfs_handle_struct *handle,
1429                          struct smb_filename *smb_fname)
1430 {
1431         uint32_t t;
1432         struct timespec creation_time = {0};
1433         struct adouble *ad;
1434
1435         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1436         if (ad == NULL) {
1437                 return;
1438         }
1439         if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1440                 TALLOC_FREE(ad);
1441                 return;
1442         }
1443         TALLOC_FREE(ad);
1444
1445         creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1446         update_stat_ex_create_time(&smb_fname->st, creation_time);
1447
1448         return;
1449 }
1450
1451 /**
1452  * Map an access mask to a Netatalk single byte byte range lock
1453  **/
1454 static off_t access_to_netatalk_brl(enum apple_fork fork,
1455                                     uint32_t access_mask)
1456 {
1457         off_t offset;
1458
1459         switch (access_mask) {
1460         case FILE_READ_DATA:
1461                 offset = AD_FILELOCK_OPEN_RD;
1462                 break;
1463
1464         case FILE_WRITE_DATA:
1465         case FILE_APPEND_DATA:
1466                 offset = AD_FILELOCK_OPEN_WR;
1467                 break;
1468
1469         default:
1470                 offset = AD_FILELOCK_OPEN_NONE;
1471                 break;
1472         }
1473
1474         if (fork == APPLE_FORK_RSRC) {
1475                 if (offset == AD_FILELOCK_OPEN_NONE) {
1476                         offset = AD_FILELOCK_RSRC_OPEN_NONE;
1477                 } else {
1478                         offset += 2;
1479                 }
1480         }
1481
1482         return offset;
1483 }
1484
1485 /**
1486  * Map a deny mode to a Netatalk brl
1487  **/
1488 static off_t denymode_to_netatalk_brl(enum apple_fork fork,
1489                                       uint32_t deny_mode)
1490 {
1491         off_t offset;
1492
1493         switch (deny_mode) {
1494         case DENY_READ:
1495                 offset = AD_FILELOCK_DENY_RD;
1496                 break;
1497
1498         case DENY_WRITE:
1499                 offset = AD_FILELOCK_DENY_WR;
1500                 break;
1501
1502         default:
1503                 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1504         }
1505
1506         if (fork == APPLE_FORK_RSRC) {
1507                 offset += 2;
1508         }
1509
1510         return offset;
1511 }
1512
1513 /**
1514  * Call fcntl() with an exclusive F_GETLK request in order to
1515  * determine if there's an exisiting shared lock
1516  *
1517  * @return true if the requested lock was found or any error occured
1518  *         false if the lock was not found
1519  **/
1520 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1521 {
1522         bool result;
1523         off_t offset = in_offset;
1524         off_t len = 1;
1525         int type = F_WRLCK;
1526         pid_t pid;
1527
1528         result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1529         if (result == false) {
1530                 return true;
1531         }
1532
1533         if (type != F_UNLCK) {
1534                 return true;
1535         }
1536
1537         return false;
1538 }
1539
1540 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1541                                    files_struct *fsp,
1542                                    uint32_t access_mask,
1543                                    uint32_t deny_mode)
1544 {
1545         NTSTATUS status = NT_STATUS_OK;
1546         struct byte_range_lock *br_lck = NULL;
1547         bool open_for_reading, open_for_writing, deny_read, deny_write;
1548         off_t off;
1549
1550         /* FIXME: hardcoded data fork, add resource fork */
1551         enum apple_fork fork = APPLE_FORK_DATA;
1552
1553         DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1554                   fsp_str_dbg(fsp),
1555                   access_mask & FILE_READ_DATA ? "READ" :"-",
1556                   access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1557                   deny_mode & DENY_READ ? "DENY_READ" : "-",
1558                   deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1559
1560         /*
1561          * Check read access and deny read mode
1562          */
1563         if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1564                 /* Check access */
1565                 open_for_reading = test_netatalk_lock(
1566                         fsp, access_to_netatalk_brl(fork, FILE_READ_DATA));
1567
1568                 deny_read = test_netatalk_lock(
1569                         fsp, denymode_to_netatalk_brl(fork, DENY_READ));
1570
1571                 DEBUG(10, ("read: %s, deny_write: %s\n",
1572                           open_for_reading == true ? "yes" : "no",
1573                           deny_read == true ? "yes" : "no"));
1574
1575                 if (((access_mask & FILE_READ_DATA) && deny_read)
1576                     || ((deny_mode & DENY_READ) && open_for_reading)) {
1577                         return NT_STATUS_SHARING_VIOLATION;
1578                 }
1579
1580                 /* Set locks */
1581                 if (access_mask & FILE_READ_DATA) {
1582                         off = access_to_netatalk_brl(fork, FILE_READ_DATA);
1583                         br_lck = do_lock(
1584                                 handle->conn->sconn->msg_ctx, fsp,
1585                                 fsp->op->global->open_persistent_id, 1, off,
1586                                 READ_LOCK, POSIX_LOCK, false,
1587                                 &status, NULL);
1588
1589                         if (!NT_STATUS_IS_OK(status))  {
1590                                 return status;
1591                         }
1592                         TALLOC_FREE(br_lck);
1593                 }
1594
1595                 if (deny_mode & DENY_READ) {
1596                         off = denymode_to_netatalk_brl(fork, DENY_READ);
1597                         br_lck = do_lock(
1598                                 handle->conn->sconn->msg_ctx, fsp,
1599                                 fsp->op->global->open_persistent_id, 1, off,
1600                                 READ_LOCK, POSIX_LOCK, false,
1601                                 &status, NULL);
1602
1603                         if (!NT_STATUS_IS_OK(status)) {
1604                                 return status;
1605                         }
1606                         TALLOC_FREE(br_lck);
1607                 }
1608         }
1609
1610         /*
1611          * Check write access and deny write mode
1612          */
1613         if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1614                 /* Check access */
1615                 open_for_writing = test_netatalk_lock(
1616                         fsp, access_to_netatalk_brl(fork, FILE_WRITE_DATA));
1617
1618                 deny_write = test_netatalk_lock(
1619                         fsp, denymode_to_netatalk_brl(fork, DENY_WRITE));
1620
1621                 DEBUG(10, ("write: %s, deny_write: %s\n",
1622                           open_for_writing == true ? "yes" : "no",
1623                           deny_write == true ? "yes" : "no"));
1624
1625                 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1626                     || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1627                         return NT_STATUS_SHARING_VIOLATION;
1628                 }
1629
1630                 /* Set locks */
1631                 if (access_mask & FILE_WRITE_DATA) {
1632                         off = access_to_netatalk_brl(fork, FILE_WRITE_DATA);
1633                         br_lck = do_lock(
1634                                 handle->conn->sconn->msg_ctx, fsp,
1635                                 fsp->op->global->open_persistent_id, 1, off,
1636                                 READ_LOCK, POSIX_LOCK, false,
1637                                 &status, NULL);
1638
1639                         if (!NT_STATUS_IS_OK(status)) {
1640                                 return status;
1641                         }
1642                         TALLOC_FREE(br_lck);
1643
1644                 }
1645                 if (deny_mode & DENY_WRITE) {
1646                         off = denymode_to_netatalk_brl(fork, DENY_WRITE);
1647                         br_lck = do_lock(
1648                                 handle->conn->sconn->msg_ctx, fsp,
1649                                 fsp->op->global->open_persistent_id, 1, off,
1650                                 READ_LOCK, POSIX_LOCK, false,
1651                                 &status, NULL);
1652
1653                         if (!NT_STATUS_IS_OK(status)) {
1654                                 return status;
1655                         }
1656                         TALLOC_FREE(br_lck);
1657                 }
1658         }
1659
1660         TALLOC_FREE(br_lck);
1661
1662         return status;
1663 }
1664
1665 /****************************************************************************
1666  * VFS ops
1667  ****************************************************************************/
1668
1669 static int fruit_connect(vfs_handle_struct *handle,
1670                          const char *service,
1671                          const char *user)
1672 {
1673         int rc;
1674         char *list = NULL, *newlist = NULL;
1675         struct fruit_config_data *config;
1676
1677         DEBUG(10, ("fruit_connect\n"));
1678
1679         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1680         if (rc < 0) {
1681                 return rc;
1682         }
1683
1684         list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
1685
1686         if (list) {
1687                 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1688                         newlist = talloc_asprintf(
1689                                 list,
1690                                 "%s/" ADOUBLE_NAME_PREFIX "*/",
1691                                 list);
1692                         lp_do_parameter(SNUM(handle->conn),
1693                                         "veto files",
1694                                         newlist);
1695                 }
1696         } else {
1697                 lp_do_parameter(SNUM(handle->conn),
1698                                 "veto files",
1699                                 "/" ADOUBLE_NAME_PREFIX "*/");
1700         }
1701
1702         TALLOC_FREE(list);
1703
1704         rc = init_fruit_config(handle);
1705         if (rc != 0) {
1706                 return rc;
1707         }
1708
1709         SMB_VFS_HANDLE_GET_DATA(handle, config,
1710                                 struct fruit_config_data, return -1);
1711
1712         if (config->encoding == FRUIT_ENC_NATIVE) {
1713                 lp_do_parameter(
1714                         SNUM(handle->conn),
1715                         "catia:mappings",
1716                         "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
1717                         "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
1718                         "0x0d:0xf00d");
1719         }
1720
1721         return rc;
1722 }
1723
1724 static int fruit_open_meta(vfs_handle_struct *handle,
1725                            struct smb_filename *smb_fname,
1726                            files_struct *fsp, int flags, mode_t mode)
1727 {
1728         int rc = 0;
1729         struct fruit_config_data *config = NULL;
1730         struct smb_filename *smb_fname_base = NULL;
1731         int baseflags;
1732         int hostfd = -1;
1733         struct adouble *ad = NULL;
1734
1735         DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
1736
1737         SMB_VFS_HANDLE_GET_DATA(handle, config,
1738                                 struct fruit_config_data, return -1);
1739
1740         if (config->meta == FRUIT_META_STREAM) {
1741                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1742         }
1743
1744         /* Create an smb_filename with stream_name == NULL. */
1745         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1746                                              smb_fname->base_name, NULL, NULL);
1747
1748         if (smb_fname_base == NULL) {
1749                 errno = ENOMEM;
1750                 rc = -1;
1751                 goto exit;
1752         }
1753
1754         /*
1755          * We use baseflags to turn off nasty side-effects when opening the
1756          * underlying file.
1757          */
1758         baseflags = flags;
1759         baseflags &= ~O_TRUNC;
1760         baseflags &= ~O_EXCL;
1761         baseflags &= ~O_CREAT;
1762
1763         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
1764                               baseflags, mode);
1765
1766         /*
1767          * It is legit to open a stream on a directory, but the base
1768          * fd has to be read-only.
1769          */
1770         if ((hostfd == -1) && (errno == EISDIR)) {
1771                 baseflags &= ~O_ACCMODE;
1772                 baseflags |= O_RDONLY;
1773                 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
1774                                       baseflags, mode);
1775         }
1776
1777         TALLOC_FREE(smb_fname_base);
1778
1779         if (hostfd == -1) {
1780                 rc = -1;
1781                 goto exit;
1782         }
1783
1784         if (flags & (O_CREAT | O_TRUNC)) {
1785                 /*
1786                  * The attribute does not exist or needs to be truncated,
1787                  * create an AppleDouble EA
1788                  */
1789                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1790                              handle, ADOUBLE_META, fsp);
1791                 if (ad == NULL) {
1792                         rc = -1;
1793                         goto exit;
1794                 }
1795
1796                 rc = ad_write(ad, smb_fname->base_name);
1797                 if (rc != 0) {
1798                         rc = -1;
1799                         goto exit;
1800                 }
1801         } else {
1802                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1803                               handle, ADOUBLE_META, fsp);
1804                 if (ad == NULL) {
1805                         rc = -1;
1806                         goto exit;
1807                 }
1808                 if (ad_read(ad, smb_fname->base_name) == -1) {
1809                         rc = -1;
1810                         goto exit;
1811                 }
1812         }
1813
1814 exit:
1815         DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
1816         if (rc != 0) {
1817                 int saved_errno = errno;
1818                 if (hostfd >= 0) {
1819                         /*
1820                          * BUGBUGBUG -- we would need to call
1821                          * fd_close_posix here, but we don't have a
1822                          * full fsp yet
1823                          */
1824                         fsp->fh->fd = hostfd;
1825                         SMB_VFS_CLOSE(fsp);
1826                 }
1827                 hostfd = -1;
1828                 errno = saved_errno;
1829         }
1830         return hostfd;
1831 }
1832
1833 static int fruit_open_rsrc(vfs_handle_struct *handle,
1834                            struct smb_filename *smb_fname,
1835                            files_struct *fsp, int flags, mode_t mode)
1836 {
1837         int rc = 0;
1838         struct fruit_config_data *config = NULL;
1839         struct adouble *ad = NULL;
1840         struct smb_filename *smb_fname_base = NULL;
1841         char *adpath = NULL;
1842         int hostfd = -1;
1843
1844         DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
1845
1846         SMB_VFS_HANDLE_GET_DATA(handle, config,
1847                                 struct fruit_config_data, return -1);
1848
1849         switch (config->rsrc) {
1850         case FRUIT_RSRC_STREAM:
1851                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1852         case FRUIT_RSRC_XATTR:
1853 #ifdef HAVE_ATTROPEN
1854                 hostfd = attropen(smb_fname->base_name,
1855                                   AFPRESOURCE_EA_NETATALK, flags, mode);
1856                 if (hostfd == -1) {
1857                         return -1;
1858                 }
1859                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1860                              handle, ADOUBLE_RSRC, fsp);
1861                 if (ad == NULL) {
1862                         rc = -1;
1863                         goto exit;
1864                 }
1865                 goto exit;
1866 #else
1867                 errno = ENOTSUP;
1868                 return -1;
1869 #endif
1870         default:
1871                 break;
1872         }
1873
1874         if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
1875                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
1876                 if (rc != 0) {
1877                         rc = -1;
1878                         goto exit;
1879                 }
1880         }
1881
1882         if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
1883                 /* sorry, but directories don't habe a resource fork */
1884                 rc = -1;
1885                 goto exit;
1886         }
1887
1888         rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
1889         if (rc != 0) {
1890                 goto exit;
1891         }
1892
1893         /* Create an smb_filename with stream_name == NULL. */
1894         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1895                                              adpath, NULL, NULL);
1896         if (smb_fname_base == NULL) {
1897                 errno = ENOMEM;
1898                 rc = -1;
1899                 goto exit;
1900         }
1901
1902         /* Sanitize flags */
1903         if (flags & O_WRONLY) {
1904                 /* We always need read access for the metadata header too */
1905                 flags &= ~O_WRONLY;
1906                 flags |= O_RDWR;
1907         }
1908
1909         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
1910                               flags, mode);
1911         if (hostfd == -1) {
1912                 rc = -1;
1913                 goto exit;
1914         }
1915
1916         /* REVIEW: we need this in ad_write() */
1917         fsp->fh->fd = hostfd;
1918
1919         if (flags & (O_CREAT | O_TRUNC)) {
1920                 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1921                              handle, ADOUBLE_RSRC, fsp);
1922                 if (ad == NULL) {
1923                         rc = -1;
1924                         goto exit;
1925                 }
1926                 rc = ad_write(ad, smb_fname->base_name);
1927                 if (rc != 0) {
1928                         rc = -1;
1929                         goto exit;
1930                 }
1931         } else {
1932                 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1933                               handle, ADOUBLE_RSRC, fsp);
1934                 if (ad == NULL) {
1935                         rc = -1;
1936                         goto exit;
1937                 }
1938                 if (ad_read(ad, smb_fname->base_name) == -1) {
1939                         rc = -1;
1940                         goto exit;
1941                 }
1942         }
1943
1944 exit:
1945
1946         TALLOC_FREE(adpath);
1947         TALLOC_FREE(smb_fname_base);
1948
1949         DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
1950         if (rc != 0) {
1951                 int saved_errno = errno;
1952                 if (hostfd >= 0) {
1953                         /*
1954                          * BUGBUGBUG -- we would need to call
1955                          * fd_close_posix here, but we don't have a
1956                          * full fsp yet
1957                          */
1958                         fsp->fh->fd = hostfd;
1959                         SMB_VFS_CLOSE(fsp);
1960                 }
1961                 hostfd = -1;
1962                 errno = saved_errno;
1963         }
1964         return hostfd;
1965 }
1966
1967 static int fruit_open(vfs_handle_struct *handle,
1968                       struct smb_filename *smb_fname,
1969                       files_struct *fsp, int flags, mode_t mode)
1970 {
1971         DEBUG(10, ("fruit_open called for %s\n",
1972                    smb_fname_str_dbg(smb_fname)));
1973
1974         if (!is_ntfs_stream_smb_fname(smb_fname)) {
1975                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1976         }
1977
1978         if (is_afpinfo_stream(smb_fname)) {
1979                 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
1980         } else if (is_afpresource_stream(smb_fname)) {
1981                 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
1982         }
1983
1984         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1985 }
1986
1987 static int fruit_rename(struct vfs_handle_struct *handle,
1988                         const struct smb_filename *smb_fname_src,
1989                         const struct smb_filename *smb_fname_dst)
1990 {
1991         int rc = -1;
1992         char *src_adouble_path = NULL;
1993         char *dst_adouble_path = NULL;
1994         struct fruit_config_data *config = NULL;
1995
1996         rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
1997
1998         if (!VALID_STAT(smb_fname_src->st)
1999             || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2000                 return rc;
2001         }
2002
2003         SMB_VFS_HANDLE_GET_DATA(handle, config,
2004                                 struct fruit_config_data, return -1);
2005
2006         if (config->rsrc == FRUIT_RSRC_XATTR) {
2007                 return rc;
2008         }
2009
2010         rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2011                           &src_adouble_path);
2012         if (rc != 0) {
2013                 goto done;
2014         }
2015         rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2016                           &dst_adouble_path);
2017         if (rc != 0) {
2018                 goto done;
2019         }
2020
2021         DEBUG(10, ("fruit_rename: %s -> %s\n",
2022                    src_adouble_path, dst_adouble_path));
2023
2024         rc = rename(src_adouble_path, dst_adouble_path);
2025         if (errno == ENOENT) {
2026                 rc = 0;
2027         }
2028
2029         TALLOC_FREE(src_adouble_path);
2030         TALLOC_FREE(dst_adouble_path);
2031
2032 done:
2033         return rc;
2034 }
2035
2036 static int fruit_unlink(vfs_handle_struct *handle,
2037                         const struct smb_filename *smb_fname)
2038 {
2039         int rc = -1;
2040         struct fruit_config_data *config = NULL;
2041         char *adp = NULL;
2042
2043         if (!is_ntfs_stream_smb_fname(smb_fname)) {
2044                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2045         }
2046
2047         SMB_VFS_HANDLE_GET_DATA(handle, config,
2048                                 struct fruit_config_data, return -1);
2049
2050         if (is_afpinfo_stream(smb_fname)) {
2051                 if (config->meta == FRUIT_META_STREAM) {
2052                         rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2053                 } else {
2054                         rc = SMB_VFS_REMOVEXATTR(handle->conn,
2055                                                  smb_fname->base_name,
2056                                                  AFPINFO_EA_NETATALK);
2057                 }
2058         } else if (is_afpresource_stream(smb_fname)) {
2059                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2060                         rc = adouble_path(talloc_tos(),
2061                                           smb_fname->base_name, &adp);
2062                         if (rc != 0) {
2063                                 return -1;
2064                         }
2065                         /* FIXME: direct unlink(), missing smb_fname */
2066                         rc = unlink(adp);
2067                         if ((rc == -1) && (errno == ENOENT)) {
2068                                 rc = 0;
2069                         }
2070                 } else {
2071                         rc = SMB_VFS_REMOVEXATTR(handle->conn,
2072                                      smb_fname->base_name,
2073                                      AFPRESOURCE_EA_NETATALK);
2074                 }
2075         } else {
2076                 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2077         }
2078
2079         TALLOC_FREE(adp);
2080         return rc;
2081 }
2082
2083 static int fruit_chmod(vfs_handle_struct *handle,
2084                        const char *path,
2085                        mode_t mode)
2086 {
2087         int rc = -1;
2088         char *adp = NULL;
2089         struct fruit_config_data *config = NULL;
2090         SMB_STRUCT_STAT sb;
2091
2092         rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2093         if (rc != 0) {
2094                 return rc;
2095         }
2096
2097         SMB_VFS_HANDLE_GET_DATA(handle, config,
2098                                 struct fruit_config_data, return -1);
2099
2100         if (config->rsrc == FRUIT_RSRC_XATTR) {
2101                 return 0;
2102         }
2103
2104         /* FIXME: direct sys_lstat(), missing smb_fname */
2105         rc = sys_lstat(path, &sb, false);
2106         if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2107                 return rc;
2108         }
2109
2110         rc = adouble_path(talloc_tos(), path, &adp);
2111         if (rc != 0) {
2112                 return -1;
2113         }
2114
2115         DEBUG(10, ("fruit_chmod: %s\n", adp));
2116
2117         rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2118         if (errno == ENOENT) {
2119                 rc = 0;
2120         }
2121
2122         TALLOC_FREE(adp);
2123         return rc;
2124 }
2125
2126 static int fruit_chown(vfs_handle_struct *handle,
2127                        const char *path,
2128                        uid_t uid,
2129                        gid_t gid)
2130 {
2131         int rc = -1;
2132         char *adp = NULL;
2133         struct fruit_config_data *config = NULL;
2134         SMB_STRUCT_STAT sb;
2135
2136         rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2137         if (rc != 0) {
2138                 return rc;
2139         }
2140
2141         SMB_VFS_HANDLE_GET_DATA(handle, config,
2142                                 struct fruit_config_data, return -1);
2143
2144         if (config->rsrc == FRUIT_RSRC_XATTR) {
2145                 return rc;
2146         }
2147
2148         /* FIXME: direct sys_lstat(), missing smb_fname */
2149         rc = sys_lstat(path, &sb, false);
2150         if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2151                 return rc;
2152         }
2153
2154         rc = adouble_path(talloc_tos(), path, &adp);
2155         if (rc != 0) {
2156                 goto done;
2157         }
2158
2159         DEBUG(10, ("fruit_chown: %s\n", adp));
2160
2161         rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2162         if (errno == ENOENT) {
2163                 rc = 0;
2164         }
2165
2166  done:
2167         TALLOC_FREE(adp);
2168         return rc;
2169 }
2170
2171 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2172 {
2173         DIR *dh = NULL;
2174         struct dirent *de;
2175         struct fruit_config_data *config;
2176
2177         SMB_VFS_HANDLE_GET_DATA(handle, config,
2178                                 struct fruit_config_data, return -1);
2179
2180         if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2181                 goto exit_rmdir;
2182         }
2183
2184         /*
2185          * Due to there is no way to change bDeleteVetoFiles variable
2186          * from this module, need to clean up ourselves
2187          */
2188         dh = opendir(path);
2189         if (dh == NULL) {
2190                 goto exit_rmdir;
2191         }
2192
2193         while ((de = readdir(dh)) != NULL) {
2194                 if ((strncmp(de->d_name,
2195                              ADOUBLE_NAME_PREFIX,
2196                              strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2197                         char *p = talloc_asprintf(talloc_tos(),
2198                                                   "%s/%s",
2199                                                   path, de->d_name);
2200                         if (p == NULL) {
2201                                 goto exit_rmdir;
2202                         }
2203                         DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2204                         (void)unlink(p);
2205                         TALLOC_FREE(p);
2206                 }
2207         }
2208
2209 exit_rmdir:
2210         if (dh) {
2211                 closedir(dh);
2212         }
2213         return SMB_VFS_NEXT_RMDIR(handle, path);
2214 }
2215
2216 static ssize_t fruit_pread(vfs_handle_struct *handle,
2217                            files_struct *fsp, void *data,
2218                            size_t n, off_t offset)
2219 {
2220         int rc = 0;
2221         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2222                 handle, fsp);
2223         struct fruit_config_data *config = NULL;
2224         AfpInfo *ai = NULL;
2225         ssize_t len;
2226         char *name = NULL;
2227         char *tmp_base_name = NULL;
2228         NTSTATUS status;
2229
2230         DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2231
2232         if (!fsp->base_fsp) {
2233                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2234         }
2235
2236         SMB_VFS_HANDLE_GET_DATA(handle, config,
2237                                 struct fruit_config_data, return -1);
2238
2239         /* fsp_name is not converted with vfs_catia */
2240         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2241         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2242                                         fsp->base_fsp->fsp_name->base_name,
2243                                         vfs_translate_to_unix,
2244                                         talloc_tos(), &name);
2245         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2246                 name = talloc_strdup(talloc_tos(), tmp_base_name);
2247                 if (name == NULL) {
2248                         rc = -1;
2249                         goto exit;
2250                 }
2251         } else if (!NT_STATUS_IS_OK(status)) {
2252                 errno = map_errno_from_nt_status(status);
2253                 rc = -1;
2254                 goto exit;
2255         }
2256         fsp->base_fsp->fsp_name->base_name = name;
2257
2258         if (ad == NULL) {
2259                 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2260                 if (len == -1) {
2261                         rc = -1;
2262                         goto exit;
2263                 }
2264                 goto exit;
2265         }
2266
2267         if (!fruit_fsp_recheck(ad, fsp)) {
2268                 rc = -1;
2269                 goto exit;
2270         }
2271
2272         if (ad->ad_type == ADOUBLE_META) {
2273                 ai = afpinfo_new(talloc_tos());
2274                 if (ai == NULL) {
2275                         rc = -1;
2276                         goto exit;
2277                 }
2278
2279                 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2280                 if (len == -1) {
2281                         rc = -1;
2282                         goto exit;
2283                 }
2284
2285                 memcpy(&ai->afpi_FinderInfo[0],
2286                        ad_entry(ad, ADEID_FINDERI),
2287                        ADEDLEN_FINDERI);
2288                 len = afpinfo_pack(ai, data);
2289                 if (len != AFP_INFO_SIZE) {
2290                         rc = -1;
2291                         goto exit;
2292                 }
2293         } else {
2294                 len = SMB_VFS_NEXT_PREAD(
2295                         handle, fsp, data, n,
2296                         offset + ad_getentryoff(ad, ADEID_RFORK));
2297                 if (len == -1) {
2298                         rc = -1;
2299                         goto exit;
2300                 }
2301         }
2302 exit:
2303         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2304         TALLOC_FREE(name);
2305         TALLOC_FREE(ai);
2306         if (rc != 0) {
2307                 len = -1;
2308         }
2309         DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2310         return len;
2311 }
2312
2313 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2314                             files_struct *fsp, const void *data,
2315                             size_t n, off_t offset)
2316 {
2317         int rc = 0;
2318         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2319                 handle, fsp);
2320         struct fruit_config_data *config = NULL;
2321         AfpInfo *ai = NULL;
2322         ssize_t len;
2323         char *name = NULL;
2324         char *tmp_base_name = NULL;
2325         NTSTATUS status;
2326
2327         DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2328
2329         if (!fsp->base_fsp) {
2330                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2331         }
2332
2333         SMB_VFS_HANDLE_GET_DATA(handle, config,
2334                                 struct fruit_config_data, return -1);
2335
2336         tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2337         status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2338                                         fsp->base_fsp->fsp_name->base_name,
2339                                         vfs_translate_to_unix,
2340                                         talloc_tos(), &name);
2341         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2342                 name = talloc_strdup(talloc_tos(), tmp_base_name);
2343                 if (name == NULL) {
2344                         rc = -1;
2345                         goto exit;
2346                 }
2347         } else if (!NT_STATUS_IS_OK(status)) {
2348                 errno = map_errno_from_nt_status(status);
2349                 rc = -1;
2350                 goto exit;
2351         }
2352         fsp->base_fsp->fsp_name->base_name = name;
2353
2354         if (ad == NULL) {
2355                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2356                 if (len != n) {
2357                         rc = -1;
2358                         goto exit;
2359                 }
2360                 goto exit;
2361         }
2362
2363         if (!fruit_fsp_recheck(ad, fsp)) {
2364                 rc = -1;
2365                 goto exit;
2366         }
2367
2368         if (ad->ad_type == ADOUBLE_META) {
2369                 if (n != AFP_INFO_SIZE || offset != 0) {
2370                         DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2371                                   (intmax_t)offset, (intmax_t)n));
2372                         rc = -1;
2373                         goto exit;
2374                 }
2375                 ai = afpinfo_unpack(talloc_tos(), data);
2376                 if (ai == NULL) {
2377                         rc = -1;
2378                         goto exit;
2379                 }
2380                 memcpy(ad_entry(ad, ADEID_FINDERI),
2381                        &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2382                 rc = ad_write(ad, name);
2383         } else {
2384                 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2385                                    offset + ad_getentryoff(ad, ADEID_RFORK));
2386                 if (len != n) {
2387                         rc = -1;
2388                         goto exit;
2389                 }
2390
2391                 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2392                         rc = ad_read(ad, name);
2393                         if (rc == -1) {
2394                                 goto exit;
2395                         }
2396                         rc = 0;
2397
2398                         if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2399                                 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2400                                 rc = ad_write(ad, name);
2401                         }
2402                 }
2403         }
2404
2405 exit:
2406         fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2407         TALLOC_FREE(name);
2408         TALLOC_FREE(ai);
2409         if (rc != 0) {
2410                 return -1;
2411         }
2412         return n;
2413 }
2414
2415 /**
2416  * Helper to stat/lstat the base file of an smb_fname.
2417  */
2418 static int fruit_stat_base(vfs_handle_struct *handle,
2419                            struct smb_filename *smb_fname,
2420                            bool follow_links)
2421 {
2422         char *tmp_stream_name;
2423         int rc;
2424
2425         tmp_stream_name = smb_fname->stream_name;
2426         smb_fname->stream_name = NULL;
2427         if (follow_links) {
2428                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2429         } else {
2430                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2431         }
2432         smb_fname->stream_name = tmp_stream_name;
2433         return rc;
2434 }
2435
2436 static int fruit_stat_meta(vfs_handle_struct *handle,
2437                            struct smb_filename *smb_fname,
2438                            bool follow_links)
2439 {
2440         /* Populate the stat struct with info from the base file. */
2441         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2442                 return -1;
2443         }
2444         smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2445         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2446                                               smb_fname->stream_name);
2447         return 0;
2448 }
2449
2450 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2451                            struct smb_filename *smb_fname,
2452                            bool follow_links)
2453
2454 {
2455         struct adouble *ad = NULL;
2456
2457         DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2458                    smb_fname_str_dbg(smb_fname)));
2459
2460         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2461         if (ad == NULL) {
2462                 errno = ENOENT;
2463                 return -1;
2464         }
2465
2466         /* Populate the stat struct with info from the base file. */
2467         if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2468                 TALLOC_FREE(ad);
2469                 return -1;
2470         }
2471
2472         smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2473         smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2474                                               smb_fname->stream_name);
2475         TALLOC_FREE(ad);
2476         return 0;
2477 }
2478
2479 static int fruit_stat(vfs_handle_struct *handle,
2480                       struct smb_filename *smb_fname)
2481 {
2482         int rc = -1;
2483
2484         DEBUG(10, ("fruit_stat called for %s\n",
2485                    smb_fname_str_dbg(smb_fname)));
2486
2487         if (!is_ntfs_stream_smb_fname(smb_fname)
2488             || is_ntfs_default_stream_smb_fname(smb_fname)) {
2489                 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2490                 if (rc == 0) {
2491                         update_btime(handle, smb_fname);
2492                 }
2493                 return rc;
2494         }
2495
2496         /*
2497          * Note if lp_posix_paths() is true, we can never
2498          * get here as is_ntfs_stream_smb_fname() is
2499          * always false. So we never need worry about
2500          * not following links here.
2501          */
2502
2503         if (is_afpinfo_stream(smb_fname)) {
2504                 rc = fruit_stat_meta(handle, smb_fname, true);
2505         } else if (is_afpresource_stream(smb_fname)) {
2506                 rc = fruit_stat_rsrc(handle, smb_fname, true);
2507         } else {
2508                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2509         }
2510
2511         if (rc == 0) {
2512                 update_btime(handle, smb_fname);
2513                 smb_fname->st.st_ex_mode &= ~S_IFMT;
2514                 smb_fname->st.st_ex_mode |= S_IFREG;
2515                 smb_fname->st.st_ex_blocks =
2516                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2517         }
2518         return rc;
2519 }
2520
2521 static int fruit_lstat(vfs_handle_struct *handle,
2522                        struct smb_filename *smb_fname)
2523 {
2524         int rc = -1;
2525
2526         DEBUG(10, ("fruit_lstat called for %s\n",
2527                    smb_fname_str_dbg(smb_fname)));
2528
2529         if (!is_ntfs_stream_smb_fname(smb_fname)
2530             || is_ntfs_default_stream_smb_fname(smb_fname)) {
2531                 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2532                 if (rc == 0) {
2533                         update_btime(handle, smb_fname);
2534                 }
2535                 return rc;
2536         }
2537
2538         if (is_afpinfo_stream(smb_fname)) {
2539                 rc = fruit_stat_meta(handle, smb_fname, false);
2540         } else if (is_afpresource_stream(smb_fname)) {
2541                 rc = fruit_stat_rsrc(handle, smb_fname, false);
2542         } else {
2543                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2544         }
2545
2546         if (rc == 0) {
2547                 update_btime(handle, smb_fname);
2548                 smb_fname->st.st_ex_mode &= ~S_IFMT;
2549                 smb_fname->st.st_ex_mode |= S_IFREG;
2550                 smb_fname->st.st_ex_blocks =
2551                         smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2552         }
2553         return rc;
2554 }
2555
2556 static int fruit_fstat_meta(vfs_handle_struct *handle,
2557                             files_struct *fsp,
2558                             SMB_STRUCT_STAT *sbuf)
2559 {
2560         DEBUG(10, ("fruit_fstat_meta called for %s\n",
2561                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2562
2563         /* Populate the stat struct with info from the base file. */
2564         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2565                 return -1;
2566         }
2567         *sbuf = fsp->base_fsp->fsp_name->st;
2568         sbuf->st_ex_size = AFP_INFO_SIZE;
2569         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2570
2571         return 0;
2572 }
2573
2574 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
2575                             SMB_STRUCT_STAT *sbuf)
2576 {
2577         struct fruit_config_data *config;
2578         struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2579                 handle, fsp);
2580
2581         DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
2582                    smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2583
2584         SMB_VFS_HANDLE_GET_DATA(handle, config,
2585                                 struct fruit_config_data, return -1);
2586
2587         if (config->rsrc == FRUIT_RSRC_STREAM) {
2588                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2589         }
2590
2591         /* Populate the stat struct with info from the base file. */
2592         if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2593                 return -1;
2594         }
2595         *sbuf = fsp->base_fsp->fsp_name->st;
2596         sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2597         sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2598
2599         DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
2600                    smb_fname_str_dbg(fsp->fsp_name),
2601                    (ssize_t)sbuf->st_ex_size));
2602
2603         return 0;
2604 }
2605
2606 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
2607                        SMB_STRUCT_STAT *sbuf)
2608 {
2609         int rc;
2610         char *name = NULL;
2611         char *tmp_base_name = NULL;
2612         NTSTATUS status;
2613         struct adouble *ad = (struct adouble *)
2614                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
2615
2616         DEBUG(10, ("fruit_fstat called for %s\n",
2617                    smb_fname_str_dbg(fsp->fsp_name)));
2618
2619         if (fsp->base_fsp) {
2620                 tmp_base_name = fsp->fsp_name->base_name;
2621                 /* fsp_name is not converted with vfs_catia */
2622                 status = SMB_VFS_TRANSLATE_NAME(
2623                         handle->conn,
2624                         fsp->base_fsp->fsp_name->base_name,
2625                         vfs_translate_to_unix,
2626                         talloc_tos(), &name);
2627
2628                 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2629                         name = talloc_strdup(talloc_tos(), tmp_base_name);
2630                         if (name == NULL) {
2631                                 rc = -1;
2632                                 goto exit;
2633                         }
2634                 } else if (!NT_STATUS_IS_OK(status)) {
2635                         errno = map_errno_from_nt_status(status);
2636                         rc = -1;
2637                         goto exit;
2638                 }
2639                 fsp->base_fsp->fsp_name->base_name = name;
2640         }
2641
2642         if (ad == NULL || fsp->base_fsp == NULL) {
2643                 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2644                 goto exit;
2645         }
2646
2647         if (!fruit_fsp_recheck(ad, fsp)) {
2648                 rc = -1;
2649                 goto exit;
2650         }
2651
2652         switch (ad->ad_type) {
2653         case ADOUBLE_META:
2654                 rc = fruit_fstat_meta(handle, fsp, sbuf);
2655                 break;
2656         case ADOUBLE_RSRC:
2657                 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
2658                 break;
2659         default:
2660                 DEBUG(10, ("fruit_fstat %s: bad type\n",
2661                            smb_fname_str_dbg(fsp->fsp_name)));
2662                 rc = -1;
2663                 goto exit;
2664         }
2665
2666         if (rc == 0) {
2667                 sbuf->st_ex_mode &= ~S_IFMT;
2668                 sbuf->st_ex_mode |= S_IFREG;
2669                 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
2670         }
2671
2672 exit:
2673         DEBUG(10, ("fruit_fstat %s, size: %zd\n",
2674                    smb_fname_str_dbg(fsp->fsp_name),
2675                    (ssize_t)sbuf->st_ex_size));
2676         if (tmp_base_name) {
2677                 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2678         }
2679         TALLOC_FREE(name);
2680         return rc;
2681 }
2682
2683 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
2684                                  struct files_struct *fsp,
2685                                  const char *fname,
2686                                  TALLOC_CTX *mem_ctx,
2687                                  unsigned int *pnum_streams,
2688                                  struct stream_struct **pstreams)
2689 {
2690         struct fruit_config_data *config = NULL;
2691         struct smb_filename *smb_fname = NULL;
2692         struct adouble *ad = NULL;
2693
2694         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2695                                 return NT_STATUS_UNSUCCESSFUL);
2696         DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
2697
2698         smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
2699         if (smb_fname == NULL) {
2700                 return NT_STATUS_NO_MEMORY;
2701         }
2702
2703         if (config->meta == FRUIT_META_NETATALK) {
2704                 ad = ad_get(talloc_tos(), handle,
2705                             smb_fname->base_name, ADOUBLE_META);
2706                 if (ad && !empty_finderinfo(ad)) {
2707                         if (!add_fruit_stream(
2708                                     mem_ctx, pnum_streams, pstreams,
2709                                     AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
2710                                     smb_roundup(handle->conn,
2711                                                 AFP_INFO_SIZE))) {
2712                                 TALLOC_FREE(ad);
2713                                 TALLOC_FREE(smb_fname);
2714                                 return NT_STATUS_NO_MEMORY;
2715                         }
2716                 }
2717                 TALLOC_FREE(ad);
2718         }
2719
2720         if (config->rsrc != FRUIT_RSRC_STREAM) {
2721                 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
2722                             ADOUBLE_RSRC);
2723                 if (ad) {
2724                         if (!add_fruit_stream(
2725                                     mem_ctx, pnum_streams, pstreams,
2726                                     AFPRESOURCE_STREAM_NAME,
2727                                     ad_getentrylen(ad, ADEID_RFORK),
2728                                     smb_roundup(handle->conn,
2729                                                 ad_getentrylen(
2730                                                         ad, ADEID_RFORK)))) {
2731                                 TALLOC_FREE(ad);
2732                                 TALLOC_FREE(smb_fname);
2733                                 return NT_STATUS_NO_MEMORY;
2734                         }
2735                 }
2736                 TALLOC_FREE(ad);
2737         }
2738
2739         TALLOC_FREE(smb_fname);
2740
2741         return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
2742                                        pnum_streams, pstreams);
2743 }
2744
2745 static int fruit_ntimes(vfs_handle_struct *handle,
2746                         const struct smb_filename *smb_fname,
2747                         struct smb_file_time *ft)
2748 {
2749         int rc = 0;
2750         struct adouble *ad = NULL;
2751
2752         if (null_timespec(ft->create_time)) {
2753                 goto exit;
2754         }
2755
2756         DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
2757                  time_to_asc(convert_timespec_to_time_t(ft->create_time))));
2758
2759         ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
2760         if (ad == NULL) {
2761                 goto exit;
2762         }
2763
2764         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
2765                    convert_time_t_to_uint32_t(ft->create_time.tv_sec));
2766
2767         rc = ad_write(ad, smb_fname->base_name);
2768
2769 exit:
2770
2771         TALLOC_FREE(ad);
2772         if (rc != 0) {
2773                 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
2774                 return -1;
2775         }
2776         return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
2777 }
2778
2779 static int fruit_fallocate(struct vfs_handle_struct *handle,
2780                            struct files_struct *fsp,
2781                            enum vfs_fallocate_mode mode,
2782                            off_t offset,
2783                            off_t len)
2784 {
2785         struct adouble *ad =
2786                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2787
2788         if (ad == NULL) {
2789                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
2790         }
2791
2792         if (!fruit_fsp_recheck(ad, fsp)) {
2793                 return errno;
2794         }
2795
2796         /* Let the pwrite code path handle it. */
2797         return ENOSYS;
2798 }
2799
2800 static int fruit_ftruncate(struct vfs_handle_struct *handle,
2801                            struct files_struct *fsp,
2802                            off_t offset)
2803 {
2804         int rc = 0;
2805         struct adouble *ad =
2806                 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
2807         struct fruit_config_data *config;
2808
2809         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
2810                    fsp_str_dbg(fsp), (double)offset));
2811
2812         if (ad == NULL) {
2813                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
2814         }
2815
2816         if (!fruit_fsp_recheck(ad, fsp)) {
2817                 return -1;
2818         }
2819
2820         SMB_VFS_HANDLE_GET_DATA(handle, config,
2821                                 struct fruit_config_data, return -1);
2822
2823         switch (ad->ad_type) {
2824         case ADOUBLE_META:
2825                 /*
2826                  * As this request hasn't been seen in the wild,
2827                  * the only sensible use I can imagine is the client
2828                  * truncating the stream to 0 bytes size.
2829                  * We simply remove the metadata on such a request.
2830                  */
2831                 if (offset == 0) {
2832                         rc = SMB_VFS_FREMOVEXATTR(fsp,
2833                                                   AFPRESOURCE_EA_NETATALK);
2834                 }
2835                 break;
2836         case ADOUBLE_RSRC:
2837                 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
2838                         rc = SMB_VFS_FREMOVEXATTR(fsp,
2839                                                   AFPRESOURCE_EA_NETATALK);
2840                 } else {
2841                         rc = SMB_VFS_NEXT_FTRUNCATE(
2842                                 handle, fsp,
2843                                 offset + ad_getentryoff(ad, ADEID_RFORK));
2844                         if (rc != 0) {
2845                                 return -1;
2846                         }
2847                         ad_setentrylen(ad, ADEID_RFORK, offset);
2848                         rc = ad_write(ad, NULL);
2849                         if (rc != 0) {
2850                                 return -1;
2851                         }
2852                 }
2853                 break;
2854         default:
2855                 return -1;
2856         }
2857
2858         return rc;
2859 }
2860
2861 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
2862                                   struct smb_request *req,
2863                                   uint16_t root_dir_fid,
2864                                   struct smb_filename *smb_fname,
2865                                   uint32_t access_mask,
2866                                   uint32_t share_access,
2867                                   uint32_t create_disposition,
2868                                   uint32_t create_options,
2869                                   uint32_t file_attributes,
2870                                   uint32_t oplock_request,
2871                                   struct smb2_lease *lease,
2872                                   uint64_t allocation_size,
2873                                   uint32_t private_flags,
2874                                   struct security_descriptor *sd,
2875                                   struct ea_list *ea_list,
2876                                   files_struct **result,
2877                                   int *pinfo)
2878 {
2879         NTSTATUS status;
2880         struct fruit_config_data *config = NULL;
2881
2882         status = SMB_VFS_NEXT_CREATE_FILE(
2883                 handle, req, root_dir_fid, smb_fname,
2884                 access_mask, share_access,
2885                 create_disposition, create_options,
2886                 file_attributes, oplock_request,
2887                 lease,
2888                 allocation_size, private_flags,
2889                 sd, ea_list, result,
2890                 pinfo);
2891
2892         if (!NT_STATUS_IS_OK(status)) {
2893                 return status;
2894         }
2895
2896         if (is_ntfs_stream_smb_fname(smb_fname)
2897             || (*result == NULL)
2898             || ((*result)->is_directory)) {
2899                 return status;
2900         }
2901
2902         SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2903                                 return NT_STATUS_UNSUCCESSFUL);
2904
2905         if (config->locking == FRUIT_LOCKING_NETATALK) {
2906                 status = fruit_check_access(
2907                         handle, *result,
2908                         access_mask,
2909                         map_share_mode_to_deny_mode(share_access, 0));
2910                 if (!NT_STATUS_IS_OK(status)) {
2911                         goto fail;
2912                 }
2913         }
2914
2915         return status;
2916
2917 fail:
2918         DEBUG(1, ("fruit_create_file: %s\n", nt_errstr(status)));
2919
2920         if (*result) {
2921                 close_file(req, *result, ERROR_CLOSE);
2922                 *result = NULL;
2923         }
2924
2925         return status;
2926 }
2927
2928 static struct vfs_fn_pointers vfs_fruit_fns = {
2929         .connect_fn = fruit_connect,
2930
2931         /* File operations */
2932         .chmod_fn = fruit_chmod,
2933         .chown_fn = fruit_chown,
2934         .unlink_fn = fruit_unlink,
2935         .rename_fn = fruit_rename,
2936         .rmdir_fn = fruit_rmdir,
2937         .open_fn = fruit_open,
2938         .pread_fn = fruit_pread,
2939         .pwrite_fn = fruit_pwrite,
2940         .stat_fn = fruit_stat,
2941         .lstat_fn = fruit_lstat,
2942         .fstat_fn = fruit_fstat,
2943         .streaminfo_fn = fruit_streaminfo,
2944         .ntimes_fn = fruit_ntimes,
2945         .unlink_fn = fruit_unlink,
2946         .ftruncate_fn = fruit_ftruncate,
2947         .fallocate_fn = fruit_fallocate,
2948         .create_file_fn = fruit_create_file,
2949 };
2950
2951 NTSTATUS vfs_fruit_init(void);
2952 NTSTATUS vfs_fruit_init(void)
2953 {
2954         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
2955                                         &vfs_fruit_fns);
2956         if (!NT_STATUS_IS_OK(ret)) {
2957                 return ret;
2958         }
2959
2960         vfs_fruit_debug_level = debug_add_class("fruit");
2961         if (vfs_fruit_debug_level == -1) {
2962                 vfs_fruit_debug_level = DBGC_VFS;
2963                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
2964                           "vfs_fruit_init"));
2965         } else {
2966                 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
2967                            "vfs_fruit_init","fruit",vfs_fruit_debug_level));
2968         }
2969
2970         return ret;
2971 }