vfs_fruit: add option veto_appledouble
[Samba.git] / source3 / modules / vfs_fruit.c
blob2547838c6483a925e5243990b32f726b663879c1
1 /*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
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.
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.
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/>.
20 #include "includes.h"
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/sys_rw.h"
35 * Enhanced OS X and Netatalk compatibility
36 * ========================================
38 * This modules takes advantage of vfs_streams_xattr and
39 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
40 * loaded in the correct order:
42 * vfs modules = catia fruit streams_xattr
44 * The module intercepts the OS X special streams "AFP_AfpInfo" and
45 * "AFP_Resource" and handles them in a special way. All other named
46 * streams are deferred to vfs_streams_xattr.
48 * The OS X client maps all NTFS illegal characters to the Unicode
49 * private range. This module optionally stores the charcters using
50 * their native ASCII encoding using vfs_catia. If you're not enabling
51 * this feature, you can skip catia from vfs modules.
53 * Finally, open modes are optionally checked against Netatalk AFP
54 * share modes.
56 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
57 * extended metadata for files and directories. This module optionally
58 * reads and stores this metadata in a way compatible with Netatalk 3
59 * which stores the metadata in an EA "org.netatalk.metadata". Cf
60 * source3/include/MacExtensions.h for a description of the binary
61 * blobs content.
63 * The "AFP_Resource" named stream may be arbitrarily large, thus it
64 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
65 * the only available filesystem where xattrs can be of any size and
66 * the OS supports using the file APIs for xattrs.
68 * The AFP_Resource stream is stored in an AppleDouble file prepending
69 * "._" to the filename. On Solaris with ZFS the stream is optionally
70 * stored in an EA "org.netatalk.ressource".
73 * Extended Attributes
74 * ===================
76 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
77 * other protocols you may want to adjust the xattr names the VFS
78 * module vfs_streams_xattr uses for storing ADS's. This defaults to
79 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
80 * these module parameters:
82 * streams_xattr:prefix = user.
83 * streams_xattr:store_stream_type = false
86 * TODO
87 * ====
89 * - log diagnostic if any needed VFS module is not loaded
90 * (eg with lp_vfs_objects())
91 * - add tests
94 static int vfs_fruit_debug_level = DBGC_VFS;
96 #undef DBGC_CLASS
97 #define DBGC_CLASS vfs_fruit_debug_level
99 #define FRUIT_PARAM_TYPE_NAME "fruit"
100 #define ADOUBLE_NAME_PREFIX "._"
103 * REVIEW:
104 * This is hokey, but what else can we do?
106 #if defined(HAVE_ATTROPEN) || defined(FREEBSD)
107 #define AFPINFO_EA_NETATALK "org.netatalk.Metadata"
108 #define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
109 #else
110 #define AFPINFO_EA_NETATALK "user.org.netatalk.Metadata"
111 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
112 #endif
114 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
116 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
117 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
118 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
119 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
121 struct fruit_config_data {
122 enum fruit_rsrc rsrc;
123 enum fruit_meta meta;
124 enum fruit_locking locking;
125 enum fruit_encoding encoding;
126 bool use_aapl;
127 bool readdir_attr_enabled;
128 bool unix_info_enabled;
129 bool veto_appledouble;
132 * Additional options, all enabled by default,
133 * possibly useful for analyzing performance. The associated
134 * operations with each of them may be expensive, so having
135 * the chance to disable them individually gives a chance
136 * tweaking the setup for the particular usecase.
138 bool readdir_attr_rsize;
139 bool readdir_attr_finder_info;
140 bool readdir_attr_max_access;
143 static const struct enum_list fruit_rsrc[] = {
144 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
145 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
146 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
147 { -1, NULL}
150 static const struct enum_list fruit_meta[] = {
151 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
152 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
153 { -1, NULL}
156 static const struct enum_list fruit_locking[] = {
157 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
158 {FRUIT_LOCKING_NONE, "none"},
159 { -1, NULL}
162 static const struct enum_list fruit_encoding[] = {
163 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
164 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
165 { -1, NULL}
168 /*****************************************************************************
169 * Defines, functions and data structures that deal with AppleDouble
170 *****************************************************************************/
173 * There are two AppleDouble blobs we deal with:
175 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
176 * metadata in an xattr
178 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
179 * ._ files
181 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
183 /* Version info */
184 #define AD_VERSION2 0x00020000
185 #define AD_VERSION AD_VERSION2
188 * AppleDouble entry IDs.
190 #define ADEID_DFORK 1
191 #define ADEID_RFORK 2
192 #define ADEID_NAME 3
193 #define ADEID_COMMENT 4
194 #define ADEID_ICONBW 5
195 #define ADEID_ICONCOL 6
196 #define ADEID_FILEI 7
197 #define ADEID_FILEDATESI 8
198 #define ADEID_FINDERI 9
199 #define ADEID_MACFILEI 10
200 #define ADEID_PRODOSFILEI 11
201 #define ADEID_MSDOSFILEI 12
202 #define ADEID_SHORTNAME 13
203 #define ADEID_AFPFILEI 14
204 #define ADEID_DID 15
206 /* Private Netatalk entries */
207 #define ADEID_PRIVDEV 16
208 #define ADEID_PRIVINO 17
209 #define ADEID_PRIVSYN 18
210 #define ADEID_PRIVID 19
211 #define ADEID_MAX (ADEID_PRIVID + 1)
214 * These are the real ids for the private entries,
215 * as stored in the adouble file
217 #define AD_DEV 0x80444556
218 #define AD_INO 0x80494E4F
219 #define AD_SYN 0x8053594E
220 #define AD_ID 0x8053567E
222 /* Number of actually used entries */
223 #define ADEID_NUM_XATTR 8
224 #define ADEID_NUM_DOT_UND 2
225 #define ADEID_NUM_RSRC_XATTR 1
227 /* AppleDouble magic */
228 #define AD_APPLESINGLE_MAGIC 0x00051600
229 #define AD_APPLEDOUBLE_MAGIC 0x00051607
230 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
232 /* Sizes of relevant entry bits */
233 #define ADEDLEN_MAGIC 4
234 #define ADEDLEN_VERSION 4
235 #define ADEDLEN_FILLER 16
236 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
237 #define ADEDLEN_NENTRIES 2
238 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
239 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
240 #define AD_ENTRY_LEN_EID 4
241 #define AD_ENTRY_LEN_OFF 4
242 #define AD_ENTRY_LEN_LEN 4
243 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
245 /* Field widths */
246 #define ADEDLEN_NAME 255
247 #define ADEDLEN_COMMENT 200
248 #define ADEDLEN_FILEI 16
249 #define ADEDLEN_FINDERI 32
250 #define ADEDLEN_FILEDATESI 16
251 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
252 #define ADEDLEN_AFPFILEI 4
253 #define ADEDLEN_MACFILEI 4
254 #define ADEDLEN_PRODOSFILEI 8
255 #define ADEDLEN_MSDOSFILEI 2
256 #define ADEDLEN_DID 4
257 #define ADEDLEN_PRIVDEV 8
258 #define ADEDLEN_PRIVINO 8
259 #define ADEDLEN_PRIVSYN 8
260 #define ADEDLEN_PRIVID 4
262 /* Offsets */
263 #define ADEDOFF_MAGIC 0
264 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
265 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
266 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
268 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
269 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
270 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
271 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
272 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
273 ADEDLEN_FILEDATESI)
274 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
275 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
276 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
277 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
279 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
280 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
281 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
283 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
284 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
285 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
286 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
287 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
288 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
290 #if AD_DATASZ_XATTR != 402
291 #error bad size for AD_DATASZ_XATTR
292 #endif
294 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
295 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
296 ADEDLEN_FINDERI)
297 #if AD_DATASZ_DOT_UND != 82
298 #error bad size for AD_DATASZ_DOT_UND
299 #endif
302 * Sharemode locks fcntl() offsets
304 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
305 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
306 #else
307 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
308 #endif
309 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
311 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
312 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
313 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
314 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
315 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
316 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
317 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
318 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
319 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
320 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
322 /* Time stuff we overload the bits a little */
323 #define AD_DATE_CREATE 0
324 #define AD_DATE_MODIFY 4
325 #define AD_DATE_BACKUP 8
326 #define AD_DATE_ACCESS 12
327 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
328 AD_DATE_BACKUP | AD_DATE_ACCESS)
329 #define AD_DATE_UNIX (1 << 10)
330 #define AD_DATE_START 0x80000000
331 #define AD_DATE_DELTA 946684800
332 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
333 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
335 /* Accessor macros */
336 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
337 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
338 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
339 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
340 #define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
342 struct ad_entry {
343 size_t ade_off;
344 size_t ade_len;
347 struct adouble {
348 vfs_handle_struct *ad_handle;
349 files_struct *ad_fsp;
350 adouble_type_t ad_type;
351 uint32_t ad_magic;
352 uint32_t ad_version;
353 struct ad_entry ad_eid[ADEID_MAX];
354 char *ad_data;
357 struct ad_entry_order {
358 uint32_t id, offset, len;
361 /* Netatalk AppleDouble metadata xattr */
362 static const
363 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
364 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
365 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
366 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
367 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
368 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
369 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
370 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
371 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
372 {0, 0, 0}
375 /* AppleDouble ressource fork file (the ones prefixed by "._") */
376 static const
377 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
378 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
379 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
380 {0, 0, 0}
384 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
385 * isn't an AppleDouble file, it simply contains the ressource data,
386 * but in order to be able to use some API calls like ad_getentryoff()
387 * we build a fake/helper struct adouble with this entry order struct.
389 static const
390 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
391 {ADEID_RFORK, 0, 0},
392 {0, 0, 0}
395 /* Conversion from enumerated id to on-disk AppleDouble id */
396 #define AD_EID_DISK(a) (set_eid[a])
397 static const uint32_t set_eid[] = {
398 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
399 AD_DEV, AD_INO, AD_SYN, AD_ID
403 * Forward declarations
405 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
406 adouble_type_t type, files_struct *fsp);
407 static int ad_write(struct adouble *ad, const char *path);
408 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
411 * Get a date
413 static int ad_getdate(const struct adouble *ad,
414 unsigned int dateoff,
415 uint32_t *date)
417 bool xlate = (dateoff & AD_DATE_UNIX);
419 dateoff &= AD_DATE_MASK;
420 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
421 return -1;
424 if (dateoff > AD_DATE_ACCESS) {
425 return -1;
427 memcpy(date,
428 ad_entry(ad, ADEID_FILEDATESI) + dateoff,
429 sizeof(uint32_t));
431 if (xlate) {
432 *date = AD_DATE_TO_UNIX(*date);
434 return 0;
438 * Set a date
440 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
442 bool xlate = (dateoff & AD_DATE_UNIX);
444 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
445 return 0;
448 dateoff &= AD_DATE_MASK;
449 if (xlate) {
450 date = AD_DATE_FROM_UNIX(date);
453 if (dateoff > AD_DATE_ACCESS) {
454 return -1;
457 memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
459 return 0;
464 * Map on-disk AppleDouble id to enumerated id
466 static uint32_t get_eid(uint32_t eid)
468 if (eid <= 15) {
469 return eid;
472 switch (eid) {
473 case AD_DEV:
474 return ADEID_PRIVDEV;
475 case AD_INO:
476 return ADEID_PRIVINO;
477 case AD_SYN:
478 return ADEID_PRIVSYN;
479 case AD_ID:
480 return ADEID_PRIVID;
481 default:
482 break;
485 return 0;
489 * Pack AppleDouble structure into data buffer
491 static bool ad_pack(struct adouble *ad)
493 uint32_t eid;
494 uint16_t nent;
495 uint32_t bufsize;
496 uint32_t offset = 0;
498 bufsize = talloc_get_size(ad->ad_data);
500 if (offset + ADEDLEN_MAGIC < offset ||
501 offset + ADEDLEN_MAGIC >= bufsize) {
502 return false;
504 RSIVAL(ad->ad_data, offset, ad->ad_magic);
505 offset += ADEDLEN_MAGIC;
507 if (offset + ADEDLEN_VERSION < offset ||
508 offset + ADEDLEN_VERSION >= bufsize) {
509 return false;
511 RSIVAL(ad->ad_data, offset, ad->ad_version);
512 offset += ADEDLEN_VERSION;
514 if (offset + ADEDLEN_FILLER < offset ||
515 offset + ADEDLEN_FILLER >= bufsize) {
516 return false;
518 if (ad->ad_type == ADOUBLE_RSRC) {
519 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
521 offset += ADEDLEN_FILLER;
523 if (offset + ADEDLEN_NENTRIES < offset ||
524 offset + ADEDLEN_NENTRIES >= bufsize) {
525 return false;
527 offset += ADEDLEN_NENTRIES;
529 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
530 if (ad->ad_eid[eid].ade_off == 0) {
532 * ade_off is also used as indicator whether a
533 * specific entry is used or not
535 continue;
538 if (offset + AD_ENTRY_LEN_EID < offset ||
539 offset + AD_ENTRY_LEN_EID >= bufsize) {
540 return false;
542 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
543 offset += AD_ENTRY_LEN_EID;
545 if (offset + AD_ENTRY_LEN_OFF < offset ||
546 offset + AD_ENTRY_LEN_OFF >= bufsize) {
547 return false;
549 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
550 offset += AD_ENTRY_LEN_OFF;
552 if (offset + AD_ENTRY_LEN_LEN < offset ||
553 offset + AD_ENTRY_LEN_LEN >= bufsize) {
554 return false;
556 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
557 offset += AD_ENTRY_LEN_LEN;
559 nent++;
562 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
563 return false;
565 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
567 return 0;
571 * Unpack an AppleDouble blob into a struct adoble
573 static bool ad_unpack(struct adouble *ad, const int nentries, size_t filesize)
575 size_t bufsize = talloc_get_size(ad->ad_data);
576 int adentries, i;
577 uint32_t eid, len, off;
580 * The size of the buffer ad->ad_data is checked when read, so
581 * we wouldn't have to check our own offsets, a few extra
582 * checks won't hurt though. We have to check the offsets we
583 * read from the buffer anyway.
586 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
587 DEBUG(1, ("bad size\n"));
588 return false;
591 ad->ad_magic = RIVAL(ad->ad_data, 0);
592 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
593 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
594 DEBUG(1, ("wrong magic or version\n"));
595 return false;
598 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
599 if (adentries != nentries) {
600 DEBUG(1, ("invalid number of entries: %d\n", adentries));
601 return false;
604 /* now, read in the entry bits */
605 for (i = 0; i < adentries; i++) {
606 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
607 eid = get_eid(eid);
608 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
609 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
611 if (!eid || eid > ADEID_MAX) {
612 DEBUG(1, ("bogus eid %d\n", eid));
613 return false;
617 * All entries other than the resource fork are
618 * expected to be read into the ad_data buffer, so
619 * ensure the specified offset is within that bound
621 if ((off > bufsize) && (eid != ADEID_RFORK)) {
622 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
623 eid, off, len));
624 return false;
628 * All entries besides FinderInfo and resource fork
629 * must fit into the buffer. FinderInfo is special as
630 * it may be larger then the default 32 bytes (if it
631 * contains marshalled xattrs), but we will fixup that
632 * in ad_convert(). And the resource fork is never
633 * accessed directly by the ad_data buf (also see
634 * comment above) anyway.
636 if ((eid != ADEID_RFORK) &&
637 (eid != ADEID_FINDERI) &&
638 ((off + len) > bufsize)) {
639 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
640 eid, off, len));
641 return false;
645 * That would be obviously broken
647 if (off > filesize) {
648 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
649 eid, off, len));
650 return false;
654 * Check for any entry that has its end beyond the
655 * filesize.
657 if (off + len < off) {
658 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
659 ", len: %" PRIu32 "\n",
660 eid, off, len));
661 return false;
664 if (off + len > filesize) {
666 * If this is the resource fork entry, we fix
667 * up the length, for any other entry we bail
668 * out.
670 if (eid != ADEID_RFORK) {
671 DEBUG(1, ("bogus eid %d: off: %" PRIu32
672 ", len: %" PRIu32 "\n",
673 eid, off, len));
674 return false;
678 * Fixup the resource fork entry by limiting
679 * the size to entryoffset - filesize.
681 len = filesize - off;
682 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
683 ", len: %" PRIu32 "\n", off, len));
686 ad->ad_eid[eid].ade_off = off;
687 ad->ad_eid[eid].ade_len = len;
690 return true;
694 * Convert from Apple's ._ file to Netatalk
696 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
697 * bytes containing packed xattrs. Netatalk can't deal with that, so
698 * we simply discard the packed xattrs.
700 * @return -1 in case an error occured, 0 if no conversion was done, 1
701 * otherwise
703 static int ad_convert(struct adouble *ad, int fd)
705 int rc = 0;
706 char *map = MAP_FAILED;
707 size_t origlen;
709 origlen = ad_getentryoff(ad, ADEID_RFORK) +
710 ad_getentrylen(ad, ADEID_RFORK);
712 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
713 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
714 if (map == MAP_FAILED) {
715 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
716 rc = -1;
717 goto exit;
720 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
721 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
722 map + ad_getentryoff(ad, ADEID_RFORK),
723 ad_getentrylen(ad, ADEID_RFORK));
726 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
727 ad_setentryoff(ad, ADEID_RFORK,
728 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
731 * FIXME: direct ftruncate(), but we don't have a fsp for the
732 * VFS call
734 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
735 + ad_getentrylen(ad, ADEID_RFORK));
737 exit:
738 if (map != MAP_FAILED) {
739 munmap(map, origlen);
741 return rc;
745 * Read and parse Netatalk AppleDouble metadata xattr
747 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
749 int rc = 0;
750 ssize_t ealen;
751 bool ok;
753 DEBUG(10, ("reading meta xattr for %s\n", path));
755 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
756 AFPINFO_EA_NETATALK, ad->ad_data,
757 AD_DATASZ_XATTR);
758 if (ealen == -1) {
759 switch (errno) {
760 case ENOATTR:
761 case ENOENT:
762 if (errno == ENOATTR) {
763 errno = ENOENT;
765 rc = -1;
766 goto exit;
767 default:
768 DEBUG(2, ("error reading meta xattr: %s\n",
769 strerror(errno)));
770 rc = -1;
771 goto exit;
774 if (ealen != AD_DATASZ_XATTR) {
775 DEBUG(2, ("bad size %zd\n", ealen));
776 errno = EINVAL;
777 rc = -1;
778 goto exit;
781 /* Now parse entries */
782 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
783 if (!ok) {
784 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
785 errno = EINVAL;
786 rc = -1;
787 goto exit;
790 if (!ad_getentryoff(ad, ADEID_FINDERI)
791 || !ad_getentryoff(ad, ADEID_COMMENT)
792 || !ad_getentryoff(ad, ADEID_FILEDATESI)
793 || !ad_getentryoff(ad, ADEID_AFPFILEI)
794 || !ad_getentryoff(ad, ADEID_PRIVDEV)
795 || !ad_getentryoff(ad, ADEID_PRIVINO)
796 || !ad_getentryoff(ad, ADEID_PRIVSYN)
797 || !ad_getentryoff(ad, ADEID_PRIVID)) {
798 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
799 errno = EINVAL;
800 rc = -1;
801 goto exit;
804 exit:
805 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
807 if (rc != 0) {
808 ealen = -1;
809 if (errno == EINVAL) {
810 become_root();
811 removexattr(path, AFPINFO_EA_NETATALK);
812 unbecome_root();
813 errno = ENOENT;
816 return ealen;
820 * Read and parse resource fork, either ._ AppleDouble file or xattr
822 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
824 struct fruit_config_data *config = NULL;
825 int fd = -1;
826 int rc = 0;
827 ssize_t len;
828 char *adpath = NULL;
829 bool opened = false;
830 int mode;
831 struct adouble *meta_ad = NULL;
832 SMB_STRUCT_STAT sbuf;
833 bool ok;
834 int saved_errno = 0;
836 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
837 struct fruit_config_data, return -1);
839 /* Try rw first so we can use the fd in ad_convert() */
840 mode = O_RDWR;
842 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
843 fd = ad->ad_fsp->fh->fd;
844 } else {
845 if (config->rsrc == FRUIT_RSRC_XATTR) {
846 adpath = talloc_strdup(talloc_tos(), path);
847 } else {
848 rc = adouble_path(talloc_tos(), path, &adpath);
849 if (rc != 0) {
850 goto exit;
854 retry:
855 if (config->rsrc == FRUIT_RSRC_XATTR) {
856 #ifndef HAVE_ATTROPEN
857 errno = ENOSYS;
858 rc = -1;
859 goto exit;
860 #else
861 /* FIXME: direct Solaris xattr syscall */
862 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
863 mode, 0);
864 #endif
865 } else {
866 /* FIXME: direct open(), don't have an fsp */
867 fd = open(adpath, mode);
870 if (fd == -1) {
871 switch (errno) {
872 case EROFS:
873 case EACCES:
874 if (mode == O_RDWR) {
875 mode = O_RDONLY;
876 goto retry;
878 /* fall through ... */
879 default:
880 DEBUG(2, ("open AppleDouble: %s, %s\n",
881 adpath, strerror(errno)));
882 rc = -1;
883 goto exit;
886 opened = true;
889 if (config->rsrc == FRUIT_RSRC_XATTR) {
890 /* FIXME: direct sys_fstat(), don't have an fsp */
891 rc = sys_fstat(
892 fd, &sbuf,
893 lp_fake_directory_create_times(
894 SNUM(ad->ad_handle->conn)));
895 if (rc != 0) {
896 goto exit;
898 len = sbuf.st_ex_size;
899 ad_setentrylen(ad, ADEID_RFORK, len);
900 } else {
901 /* FIXME: direct sys_pread(), don't have an fsp */
902 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
903 if (len != AD_DATASZ_DOT_UND) {
904 DEBUG(2, ("%s: bad size: %zd\n",
905 strerror(errno), len));
906 rc = -1;
907 goto exit;
910 /* FIXME: direct sys_fstat(), we don't have an fsp */
911 rc = sys_fstat(fd, &sbuf,
912 lp_fake_directory_create_times(
913 SNUM(ad->ad_handle->conn)));
914 if (rc != 0) {
915 goto exit;
918 /* Now parse entries */
919 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
920 if (!ok) {
921 DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
922 errno = EINVAL;
923 rc = -1;
924 goto exit;
927 if ((ad_getentryoff(ad, ADEID_FINDERI)
928 != ADEDOFF_FINDERI_DOT_UND)
929 || (ad_getentrylen(ad, ADEID_FINDERI)
930 < ADEDLEN_FINDERI)
931 || (ad_getentryoff(ad, ADEID_RFORK)
932 < ADEDOFF_RFORK_DOT_UND)) {
933 DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
934 errno = EINVAL;
935 rc = -1;
936 goto exit;
939 if ((mode == O_RDWR)
940 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
941 rc = ad_convert(ad, fd);
942 if (rc != 0) {
943 rc = -1;
944 goto exit;
947 * Can't use ad_write() because we might not have a fsp
949 rc = ad_pack(ad);
950 if (rc != 0) {
951 goto exit;
953 /* FIXME: direct sys_pwrite(), don't have an fsp */
954 len = sys_pwrite(fd, ad->ad_data,
955 AD_DATASZ_DOT_UND, 0);
956 if (len != AD_DATASZ_DOT_UND) {
957 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
958 rc = -1;
959 goto exit;
962 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
963 ADOUBLE_META, NULL);
964 if (meta_ad == NULL) {
965 rc = -1;
966 goto exit;
969 memcpy(ad_entry(meta_ad, ADEID_FINDERI),
970 ad_entry(ad, ADEID_FINDERI),
971 ADEDLEN_FINDERI);
973 rc = ad_write(meta_ad, path);
974 if (rc != 0) {
975 rc = -1;
976 goto exit;
981 DEBUG(10, ("opened AppleDouble: %s\n", path));
983 exit:
984 if (rc != 0) {
985 saved_errno = errno;
986 len = -1;
988 if (opened && fd != -1) {
989 close(fd);
991 TALLOC_FREE(adpath);
992 TALLOC_FREE(meta_ad);
993 if (rc != 0) {
994 errno = saved_errno;
996 return len;
1000 * Read and unpack an AppleDouble metadata xattr or resource
1002 static ssize_t ad_read(struct adouble *ad, const char *path)
1004 switch (ad->ad_type) {
1005 case ADOUBLE_META:
1006 return ad_header_read_meta(ad, path);
1007 case ADOUBLE_RSRC:
1008 return ad_header_read_rsrc(ad, path);
1009 default:
1010 return -1;
1015 * Allocate a struct adouble without initialiing it
1017 * The struct is either hang of the fsp extension context or if fsp is
1018 * NULL from ctx.
1020 * @param[in] ctx talloc context
1021 * @param[in] handle vfs handle
1022 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1024 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1025 * added as an fsp extension
1027 * @return adouble handle
1029 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1030 adouble_type_t type, files_struct *fsp)
1032 int rc = 0;
1033 size_t adsize = 0;
1034 struct adouble *ad;
1035 struct fruit_config_data *config;
1037 SMB_VFS_HANDLE_GET_DATA(handle, config,
1038 struct fruit_config_data, return NULL);
1040 switch (type) {
1041 case ADOUBLE_META:
1042 adsize = AD_DATASZ_XATTR;
1043 break;
1044 case ADOUBLE_RSRC:
1045 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1046 adsize = AD_DATASZ_DOT_UND;
1048 break;
1049 default:
1050 return NULL;
1053 if (!fsp) {
1054 ad = talloc_zero(ctx, struct adouble);
1055 if (ad == NULL) {
1056 rc = -1;
1057 goto exit;
1059 if (adsize) {
1060 ad->ad_data = talloc_zero_array(ad, char, adsize);
1062 } else {
1063 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1064 struct adouble,
1065 NULL);
1066 if (ad == NULL) {
1067 rc = -1;
1068 goto exit;
1070 if (adsize) {
1071 ad->ad_data = talloc_zero_array(
1072 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1073 char, adsize);
1075 ad->ad_fsp = fsp;
1078 if (adsize && ad->ad_data == NULL) {
1079 rc = -1;
1080 goto exit;
1082 ad->ad_handle = handle;
1083 ad->ad_type = type;
1084 ad->ad_magic = AD_MAGIC;
1085 ad->ad_version = AD_VERSION;
1087 exit:
1088 if (rc != 0) {
1089 TALLOC_FREE(ad);
1091 return ad;
1095 * Allocate and initialize a new struct adouble
1097 * @param[in] ctx talloc context
1098 * @param[in] handle vfs handle
1099 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1100 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1102 * @return adouble handle, initialized
1104 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1105 adouble_type_t type, files_struct *fsp)
1107 int rc = 0;
1108 const struct ad_entry_order *eid;
1109 struct adouble *ad = NULL;
1110 struct fruit_config_data *config;
1111 time_t t = time(NULL);
1113 SMB_VFS_HANDLE_GET_DATA(handle, config,
1114 struct fruit_config_data, return NULL);
1116 switch (type) {
1117 case ADOUBLE_META:
1118 eid = entry_order_meta_xattr;
1119 break;
1120 case ADOUBLE_RSRC:
1121 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1122 eid = entry_order_dot_und;
1123 } else {
1124 eid = entry_order_rsrc_xattr;
1126 break;
1127 default:
1128 return NULL;
1131 ad = ad_alloc(ctx, handle, type, fsp);
1132 if (ad == NULL) {
1133 return NULL;
1136 while (eid->id) {
1137 ad->ad_eid[eid->id].ade_off = eid->offset;
1138 ad->ad_eid[eid->id].ade_len = eid->len;
1139 eid++;
1142 /* put something sane in the date fields */
1143 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1144 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1145 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1146 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1148 if (rc != 0) {
1149 TALLOC_FREE(ad);
1151 return ad;
1155 * Return AppleDouble data for a file
1157 * @param[in] ctx talloc context
1158 * @param[in] handle vfs handle
1159 * @param[in] path pathname to file or directory
1160 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1162 * @return talloced struct adouble or NULL on error
1164 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1165 const char *path, adouble_type_t type)
1167 int rc = 0;
1168 ssize_t len;
1169 struct adouble *ad = NULL;
1171 DEBUG(10, ("ad_get(%s) called for %s\n",
1172 type == ADOUBLE_META ? "meta" : "rsrc", path));
1174 ad = ad_alloc(ctx, handle, type, NULL);
1175 if (ad == NULL) {
1176 rc = -1;
1177 goto exit;
1180 len = ad_read(ad, path);
1181 if (len == -1) {
1182 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1183 rc = -1;
1184 goto exit;
1187 exit:
1188 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1189 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1191 if (rc != 0) {
1192 TALLOC_FREE(ad);
1194 return ad;
1198 * Set AppleDouble metadata on a file or directory
1200 * @param[in] ad adouble handle
1202 * @param[in] path pathname to file or directory, may be NULL for a
1203 * resource fork
1205 * @return status code, 0 means success
1207 static int ad_write(struct adouble *ad, const char *path)
1209 int rc = 0;
1210 ssize_t len;
1212 rc = ad_pack(ad);
1213 if (rc != 0) {
1214 goto exit;
1217 switch (ad->ad_type) {
1218 case ADOUBLE_META:
1219 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1220 AFPINFO_EA_NETATALK, ad->ad_data,
1221 AD_DATASZ_XATTR, 0);
1222 break;
1223 case ADOUBLE_RSRC:
1224 if ((ad->ad_fsp == NULL)
1225 || (ad->ad_fsp->fh == NULL)
1226 || (ad->ad_fsp->fh->fd == -1)) {
1227 rc = -1;
1228 goto exit;
1230 /* FIXME: direct sys_pwrite(), don't have an fsp */
1231 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1232 talloc_get_size(ad->ad_data), 0);
1233 if (len != talloc_get_size(ad->ad_data)) {
1234 DEBUG(1, ("short write on %s: %zd",
1235 fsp_str_dbg(ad->ad_fsp), len));
1236 rc = -1;
1237 goto exit;
1239 break;
1240 default:
1241 return -1;
1243 exit:
1244 return rc;
1247 /*****************************************************************************
1248 * Helper functions
1249 *****************************************************************************/
1251 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1253 if (strncasecmp_m(smb_fname->stream_name,
1254 AFPINFO_STREAM_NAME,
1255 strlen(AFPINFO_STREAM_NAME)) == 0) {
1256 return true;
1258 return false;
1261 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1263 if (strncasecmp_m(smb_fname->stream_name,
1264 AFPRESOURCE_STREAM_NAME,
1265 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1266 return true;
1268 return false;
1272 * Test whether stream is an Apple stream, not used atm
1274 #if 0
1275 static bool is_apple_stream(const struct smb_filename *smb_fname)
1277 if (is_afpinfo_stream(smb_fname)) {
1278 return true;
1280 if (is_afpresource_stream(smb_fname)) {
1281 return true;
1283 return false;
1285 #endif
1288 * Initialize config struct from our smb.conf config parameters
1290 static int init_fruit_config(vfs_handle_struct *handle)
1292 struct fruit_config_data *config;
1293 int enumval;
1295 config = talloc_zero(handle->conn, struct fruit_config_data);
1296 if (!config) {
1297 DEBUG(1, ("talloc_zero() failed\n"));
1298 errno = ENOMEM;
1299 return -1;
1302 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1303 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1304 if (enumval == -1) {
1305 DEBUG(1, ("value for %s: ressource type unknown\n",
1306 FRUIT_PARAM_TYPE_NAME));
1307 return -1;
1309 config->rsrc = (enum fruit_rsrc)enumval;
1311 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1312 "metadata", fruit_meta, FRUIT_META_NETATALK);
1313 if (enumval == -1) {
1314 DEBUG(1, ("value for %s: metadata type unknown\n",
1315 FRUIT_PARAM_TYPE_NAME));
1316 return -1;
1318 config->meta = (enum fruit_meta)enumval;
1320 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1321 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1322 if (enumval == -1) {
1323 DEBUG(1, ("value for %s: locking type unknown\n",
1324 FRUIT_PARAM_TYPE_NAME));
1325 return -1;
1327 config->locking = (enum fruit_locking)enumval;
1329 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1330 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1331 if (enumval == -1) {
1332 DEBUG(1, ("value for %s: encoding type unknown\n",
1333 FRUIT_PARAM_TYPE_NAME));
1334 return -1;
1336 config->encoding = (enum fruit_encoding)enumval;
1338 if (lp_parm_bool(SNUM(handle->conn),
1339 FRUIT_PARAM_TYPE_NAME, "veto_appledouble", true)) {
1340 config->veto_appledouble = true;
1343 if (lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "aapl", true)) {
1344 config->use_aapl = true;
1347 if (lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true)) {
1348 config->unix_info_enabled = true;
1351 if (lp_parm_bool(SNUM(handle->conn),
1352 "readdir_attr", "aapl_rsize", true)) {
1353 config->readdir_attr_rsize = true;
1356 if (lp_parm_bool(SNUM(handle->conn),
1357 "readdir_attr", "aapl_finder_info", true)) {
1358 config->readdir_attr_finder_info = true;
1361 if (lp_parm_bool(SNUM(handle->conn),
1362 "readdir_attr", "aapl_max_access", true)) {
1363 config->readdir_attr_max_access = true;
1366 SMB_VFS_HANDLE_SET_DATA(handle, config,
1367 NULL, struct fruit_config_data,
1368 return -1);
1370 return 0;
1374 * Prepend "._" to a basename
1376 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1378 char *parent;
1379 const char *base;
1381 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1382 return -1;
1385 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1386 if (*path_out == NULL) {
1387 return -1;
1390 return 0;
1394 * Allocate and initialize an AfpInfo struct
1396 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1398 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1399 if (ai == NULL) {
1400 return NULL;
1402 ai->afpi_Signature = AFP_Signature;
1403 ai->afpi_Version = AFP_Version;
1404 ai->afpi_BackupTime = AD_DATE_START;
1405 return ai;
1409 * Pack an AfpInfo struct into a buffer
1411 * Buffer size must be at least AFP_INFO_SIZE
1412 * Returns size of packed buffer
1414 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1416 memset(buf, 0, AFP_INFO_SIZE);
1418 RSIVAL(buf, 0, ai->afpi_Signature);
1419 RSIVAL(buf, 4, ai->afpi_Version);
1420 RSIVAL(buf, 12, ai->afpi_BackupTime);
1421 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1423 return AFP_INFO_SIZE;
1427 * Unpack a buffer into a AfpInfo structure
1429 * Buffer size must be at least AFP_INFO_SIZE
1430 * Returns allocated AfpInfo struct
1432 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1434 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1435 if (ai == NULL) {
1436 return NULL;
1439 ai->afpi_Signature = RIVAL(data, 0);
1440 ai->afpi_Version = RIVAL(data, 4);
1441 ai->afpi_BackupTime = RIVAL(data, 12);
1442 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1443 sizeof(ai->afpi_FinderInfo));
1445 if (ai->afpi_Signature != AFP_Signature
1446 || ai->afpi_Version != AFP_Version) {
1447 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1448 TALLOC_FREE(ai);
1451 return ai;
1455 * Fake an inode number from the md5 hash of the (xattr) name
1457 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1459 MD5_CTX ctx;
1460 unsigned char hash[16];
1461 SMB_INO_T result;
1462 char *upper_sname;
1464 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1465 SMB_ASSERT(upper_sname != NULL);
1467 MD5Init(&ctx);
1468 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1469 sizeof(sbuf->st_ex_dev));
1470 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1471 sizeof(sbuf->st_ex_ino));
1472 MD5Update(&ctx, (unsigned char *)upper_sname,
1473 talloc_get_size(upper_sname)-1);
1474 MD5Final(hash, &ctx);
1476 TALLOC_FREE(upper_sname);
1478 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1479 memcpy(&result, hash, sizeof(result));
1481 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1482 sname, (unsigned long long)result));
1484 return result;
1488 * Ensure ad_fsp is still valid
1490 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1492 if (ad->ad_fsp == fsp) {
1493 return true;
1495 ad->ad_fsp = fsp;
1497 return true;
1500 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1501 struct stream_struct **streams,
1502 const char *name, off_t size,
1503 off_t alloc_size)
1505 struct stream_struct *tmp;
1507 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1508 (*num_streams)+1);
1509 if (tmp == NULL) {
1510 return false;
1513 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1514 if (tmp[*num_streams].name == NULL) {
1515 return false;
1518 tmp[*num_streams].size = size;
1519 tmp[*num_streams].alloc_size = alloc_size;
1521 *streams = tmp;
1522 *num_streams += 1;
1523 return true;
1526 static bool empty_finderinfo(const struct adouble *ad)
1529 char emptybuf[ADEDLEN_FINDERI] = {0};
1530 if (memcmp(emptybuf,
1531 ad_entry(ad, ADEID_FINDERI),
1532 ADEDLEN_FINDERI) == 0) {
1533 return true;
1535 return false;
1539 * Update btime with btime from Netatalk
1541 static void update_btime(vfs_handle_struct *handle,
1542 struct smb_filename *smb_fname)
1544 uint32_t t;
1545 struct timespec creation_time = {0};
1546 struct adouble *ad;
1548 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1549 if (ad == NULL) {
1550 return;
1552 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1553 TALLOC_FREE(ad);
1554 return;
1556 TALLOC_FREE(ad);
1558 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1559 update_stat_ex_create_time(&smb_fname->st, creation_time);
1561 return;
1565 * Map an access mask to a Netatalk single byte byte range lock
1567 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1568 uint32_t access_mask)
1570 off_t offset;
1572 switch (access_mask) {
1573 case FILE_READ_DATA:
1574 offset = AD_FILELOCK_OPEN_RD;
1575 break;
1577 case FILE_WRITE_DATA:
1578 case FILE_APPEND_DATA:
1579 offset = AD_FILELOCK_OPEN_WR;
1580 break;
1582 default:
1583 offset = AD_FILELOCK_OPEN_NONE;
1584 break;
1587 if (fork_type == APPLE_FORK_RSRC) {
1588 if (offset == AD_FILELOCK_OPEN_NONE) {
1589 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1590 } else {
1591 offset += 2;
1595 return offset;
1599 * Map a deny mode to a Netatalk brl
1601 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1602 uint32_t deny_mode)
1604 off_t offset;
1606 switch (deny_mode) {
1607 case DENY_READ:
1608 offset = AD_FILELOCK_DENY_RD;
1609 break;
1611 case DENY_WRITE:
1612 offset = AD_FILELOCK_DENY_WR;
1613 break;
1615 default:
1616 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1619 if (fork_type == APPLE_FORK_RSRC) {
1620 offset += 2;
1623 return offset;
1627 * Call fcntl() with an exclusive F_GETLK request in order to
1628 * determine if there's an exisiting shared lock
1630 * @return true if the requested lock was found or any error occured
1631 * false if the lock was not found
1633 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1635 bool result;
1636 off_t offset = in_offset;
1637 off_t len = 1;
1638 int type = F_WRLCK;
1639 pid_t pid;
1641 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1642 if (result == false) {
1643 return true;
1646 if (type != F_UNLCK) {
1647 return true;
1650 return false;
1653 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1654 files_struct *fsp,
1655 uint32_t access_mask,
1656 uint32_t deny_mode)
1658 NTSTATUS status = NT_STATUS_OK;
1659 struct byte_range_lock *br_lck = NULL;
1660 bool open_for_reading, open_for_writing, deny_read, deny_write;
1661 off_t off;
1663 /* FIXME: hardcoded data fork, add resource fork */
1664 enum apple_fork fork_type = APPLE_FORK_DATA;
1666 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1667 fsp_str_dbg(fsp),
1668 access_mask & FILE_READ_DATA ? "READ" :"-",
1669 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1670 deny_mode & DENY_READ ? "DENY_READ" : "-",
1671 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1674 * Check read access and deny read mode
1676 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1677 /* Check access */
1678 open_for_reading = test_netatalk_lock(
1679 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1681 deny_read = test_netatalk_lock(
1682 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1684 DEBUG(10, ("read: %s, deny_write: %s\n",
1685 open_for_reading == true ? "yes" : "no",
1686 deny_read == true ? "yes" : "no"));
1688 if (((access_mask & FILE_READ_DATA) && deny_read)
1689 || ((deny_mode & DENY_READ) && open_for_reading)) {
1690 return NT_STATUS_SHARING_VIOLATION;
1693 /* Set locks */
1694 if (access_mask & FILE_READ_DATA) {
1695 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1696 br_lck = do_lock(
1697 handle->conn->sconn->msg_ctx, fsp,
1698 fsp->op->global->open_persistent_id, 1, off,
1699 READ_LOCK, POSIX_LOCK, false,
1700 &status, NULL);
1702 if (!NT_STATUS_IS_OK(status)) {
1703 return status;
1705 TALLOC_FREE(br_lck);
1708 if (deny_mode & DENY_READ) {
1709 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1710 br_lck = do_lock(
1711 handle->conn->sconn->msg_ctx, fsp,
1712 fsp->op->global->open_persistent_id, 1, off,
1713 READ_LOCK, POSIX_LOCK, false,
1714 &status, NULL);
1716 if (!NT_STATUS_IS_OK(status)) {
1717 return status;
1719 TALLOC_FREE(br_lck);
1724 * Check write access and deny write mode
1726 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1727 /* Check access */
1728 open_for_writing = test_netatalk_lock(
1729 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1731 deny_write = test_netatalk_lock(
1732 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1734 DEBUG(10, ("write: %s, deny_write: %s\n",
1735 open_for_writing == true ? "yes" : "no",
1736 deny_write == true ? "yes" : "no"));
1738 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1739 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1740 return NT_STATUS_SHARING_VIOLATION;
1743 /* Set locks */
1744 if (access_mask & FILE_WRITE_DATA) {
1745 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1746 br_lck = do_lock(
1747 handle->conn->sconn->msg_ctx, fsp,
1748 fsp->op->global->open_persistent_id, 1, off,
1749 READ_LOCK, POSIX_LOCK, false,
1750 &status, NULL);
1752 if (!NT_STATUS_IS_OK(status)) {
1753 return status;
1755 TALLOC_FREE(br_lck);
1758 if (deny_mode & DENY_WRITE) {
1759 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1760 br_lck = do_lock(
1761 handle->conn->sconn->msg_ctx, fsp,
1762 fsp->op->global->open_persistent_id, 1, off,
1763 READ_LOCK, POSIX_LOCK, false,
1764 &status, NULL);
1766 if (!NT_STATUS_IS_OK(status)) {
1767 return status;
1769 TALLOC_FREE(br_lck);
1773 TALLOC_FREE(br_lck);
1775 return status;
1778 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1779 struct smb_request *req,
1780 const struct smb2_create_blobs *in_context_blobs,
1781 struct smb2_create_blobs *out_context_blobs)
1783 struct fruit_config_data *config;
1784 NTSTATUS status;
1785 struct smb2_create_blob *aapl = NULL;
1786 uint32_t cmd;
1787 bool ok;
1788 uint8_t p[16];
1789 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1790 uint64_t req_bitmap, client_caps;
1791 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1792 smb_ucs2_t *model;
1793 size_t modellen;
1795 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1796 return NT_STATUS_UNSUCCESSFUL);
1798 if (!config->use_aapl
1799 || in_context_blobs == NULL
1800 || out_context_blobs == NULL) {
1801 return NT_STATUS_OK;
1804 aapl = smb2_create_blob_find(in_context_blobs,
1805 SMB2_CREATE_TAG_AAPL);
1806 if (aapl == NULL) {
1807 return NT_STATUS_OK;
1810 if (aapl->data.length != 24) {
1811 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1812 (uintmax_t)aapl->data.length));
1813 return NT_STATUS_INVALID_PARAMETER;
1816 cmd = IVAL(aapl->data.data, 0);
1817 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1818 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1819 return NT_STATUS_INVALID_PARAMETER;
1822 req_bitmap = BVAL(aapl->data.data, 8);
1823 client_caps = BVAL(aapl->data.data, 16);
1825 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1826 SIVAL(p, 4, 0);
1827 SBVAL(p, 8, req_bitmap);
1828 ok = data_blob_append(req, &blob, p, 16);
1829 if (!ok) {
1830 return NT_STATUS_UNSUCCESSFUL;
1833 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1834 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1835 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1836 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1837 config->readdir_attr_enabled = true;
1841 * The client doesn't set the flag, so we can't check
1842 * for it and just set it unconditionally
1844 if (config->unix_info_enabled) {
1845 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1848 SBVAL(p, 0, server_caps);
1849 ok = data_blob_append(req, &blob, p, 8);
1850 if (!ok) {
1851 return NT_STATUS_UNSUCCESSFUL;
1855 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1856 SBVAL(p, 0,
1857 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1858 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1859 ok = data_blob_append(req, &blob, p, 8);
1860 if (!ok) {
1861 return NT_STATUS_UNSUCCESSFUL;
1865 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1866 ok = convert_string_talloc(req,
1867 CH_UNIX, CH_UTF16LE,
1868 "Samba", strlen("Samba"),
1869 &model, &modellen);
1870 if (!ok) {
1871 return NT_STATUS_UNSUCCESSFUL;
1874 SIVAL(p, 0, 0);
1875 SIVAL(p + 4, 0, modellen);
1876 ok = data_blob_append(req, &blob, p, 8);
1877 if (!ok) {
1878 talloc_free(model);
1879 return NT_STATUS_UNSUCCESSFUL;
1882 ok = data_blob_append(req, &blob, model, modellen);
1883 talloc_free(model);
1884 if (!ok) {
1885 return NT_STATUS_UNSUCCESSFUL;
1889 status = smb2_create_blob_add(out_context_blobs,
1890 out_context_blobs,
1891 SMB2_CREATE_TAG_AAPL,
1892 blob);
1894 return status;
1897 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1898 const struct smb_filename *smb_fname,
1899 struct readdir_attr_data *attr_data)
1901 NTSTATUS status = NT_STATUS_OK;
1902 uint32_t date_added;
1903 struct adouble *ad = NULL;
1904 struct fruit_config_data *config = NULL;
1906 SMB_VFS_HANDLE_GET_DATA(handle, config,
1907 struct fruit_config_data,
1908 return NT_STATUS_UNSUCCESSFUL);
1911 /* Ensure we return a default value in the creation_date field */
1912 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1915 * Resource fork length
1918 if (config->readdir_attr_rsize) {
1919 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1920 ADOUBLE_RSRC);
1921 if (ad) {
1922 attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1923 ad, ADEID_RFORK);
1924 TALLOC_FREE(ad);
1929 * FinderInfo
1932 if (config->readdir_attr_finder_info) {
1933 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1934 ADOUBLE_META);
1935 if (ad) {
1936 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1937 /* finder_type */
1938 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1939 ad_entry(ad, ADEID_FINDERI), 4);
1941 /* finder_creator */
1942 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1943 ad_entry(ad, ADEID_FINDERI) + 4, 4);
1946 /* finder_flags */
1947 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1948 ad_entry(ad, ADEID_FINDERI) + 8, 2);
1950 /* finder_ext_flags */
1951 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1952 ad_entry(ad, ADEID_FINDERI) + 24, 2);
1954 /* creation date */
1955 date_added = convert_time_t_to_uint32_t(
1956 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1957 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1959 TALLOC_FREE(ad);
1963 TALLOC_FREE(ad);
1964 return status;
1967 /* Search MS NFS style ACE with UNIX mode */
1968 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1969 files_struct *fsp,
1970 const struct security_descriptor *psd,
1971 mode_t *pmode,
1972 bool *pdo_chmod)
1974 int i;
1975 struct fruit_config_data *config = NULL;
1977 *pdo_chmod = false;
1979 SMB_VFS_HANDLE_GET_DATA(handle, config,
1980 struct fruit_config_data,
1981 return NT_STATUS_UNSUCCESSFUL);
1983 if (psd->dacl == NULL || !config->unix_info_enabled) {
1984 return NT_STATUS_OK;
1987 for (i = 0; i < psd->dacl->num_aces; i++) {
1988 if (dom_sid_compare_domain(
1989 &global_sid_Unix_NFS_Mode,
1990 &psd->dacl->aces[i].trustee) == 0) {
1991 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1992 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1993 *pdo_chmod = true;
1995 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1996 fsp_str_dbg(fsp), (unsigned)(*pmode)));
1997 break;
2001 return NT_STATUS_OK;
2004 /****************************************************************************
2005 * VFS ops
2006 ****************************************************************************/
2008 static int fruit_connect(vfs_handle_struct *handle,
2009 const char *service,
2010 const char *user)
2012 int rc;
2013 char *list = NULL, *newlist = NULL;
2014 struct fruit_config_data *config;
2016 DEBUG(10, ("fruit_connect\n"));
2018 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2019 if (rc < 0) {
2020 return rc;
2023 rc = init_fruit_config(handle);
2024 if (rc != 0) {
2025 return rc;
2028 SMB_VFS_HANDLE_GET_DATA(handle, config,
2029 struct fruit_config_data, return -1);
2031 if (config->veto_appledouble) {
2032 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2034 if (list) {
2035 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2036 newlist = talloc_asprintf(
2037 list,
2038 "%s/" ADOUBLE_NAME_PREFIX "*/",
2039 list);
2040 lp_do_parameter(SNUM(handle->conn),
2041 "veto files",
2042 newlist);
2044 } else {
2045 lp_do_parameter(SNUM(handle->conn),
2046 "veto files",
2047 "/" ADOUBLE_NAME_PREFIX "*/");
2050 TALLOC_FREE(list);
2053 if (config->encoding == FRUIT_ENC_NATIVE) {
2054 lp_do_parameter(
2055 SNUM(handle->conn),
2056 "catia:mappings",
2057 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2058 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2059 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2060 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2061 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2062 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2063 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2064 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2065 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2066 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2067 "0x0d:0xf00d");
2070 return rc;
2073 static int fruit_open_meta(vfs_handle_struct *handle,
2074 struct smb_filename *smb_fname,
2075 files_struct *fsp, int flags, mode_t mode)
2077 int rc = 0;
2078 struct fruit_config_data *config = NULL;
2079 struct smb_filename *smb_fname_base = NULL;
2080 int baseflags;
2081 int hostfd = -1;
2082 struct adouble *ad = NULL;
2084 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
2086 SMB_VFS_HANDLE_GET_DATA(handle, config,
2087 struct fruit_config_data, return -1);
2089 if (config->meta == FRUIT_META_STREAM) {
2090 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2093 /* Create an smb_filename with stream_name == NULL. */
2094 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2095 smb_fname->base_name, NULL, NULL);
2097 if (smb_fname_base == NULL) {
2098 errno = ENOMEM;
2099 rc = -1;
2100 goto exit;
2104 * We use baseflags to turn off nasty side-effects when opening the
2105 * underlying file.
2107 baseflags = flags;
2108 baseflags &= ~O_TRUNC;
2109 baseflags &= ~O_EXCL;
2110 baseflags &= ~O_CREAT;
2112 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2113 baseflags, mode);
2116 * It is legit to open a stream on a directory, but the base
2117 * fd has to be read-only.
2119 if ((hostfd == -1) && (errno == EISDIR)) {
2120 baseflags &= ~O_ACCMODE;
2121 baseflags |= O_RDONLY;
2122 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2123 baseflags, mode);
2126 TALLOC_FREE(smb_fname_base);
2128 if (hostfd == -1) {
2129 rc = -1;
2130 goto exit;
2133 if (flags & (O_CREAT | O_TRUNC)) {
2135 * The attribute does not exist or needs to be truncated,
2136 * create an AppleDouble EA
2138 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2139 handle, ADOUBLE_META, fsp);
2140 if (ad == NULL) {
2141 rc = -1;
2142 goto exit;
2145 rc = ad_write(ad, smb_fname->base_name);
2146 if (rc != 0) {
2147 rc = -1;
2148 goto exit;
2150 } else {
2151 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2152 handle, ADOUBLE_META, fsp);
2153 if (ad == NULL) {
2154 rc = -1;
2155 goto exit;
2157 if (ad_read(ad, smb_fname->base_name) == -1) {
2158 rc = -1;
2159 goto exit;
2163 exit:
2164 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2165 if (rc != 0) {
2166 int saved_errno = errno;
2167 if (hostfd >= 0) {
2169 * BUGBUGBUG -- we would need to call
2170 * fd_close_posix here, but we don't have a
2171 * full fsp yet
2173 fsp->fh->fd = hostfd;
2174 SMB_VFS_CLOSE(fsp);
2176 hostfd = -1;
2177 errno = saved_errno;
2179 return hostfd;
2182 static int fruit_open_rsrc(vfs_handle_struct *handle,
2183 struct smb_filename *smb_fname,
2184 files_struct *fsp, int flags, mode_t mode)
2186 int rc = 0;
2187 struct fruit_config_data *config = NULL;
2188 struct adouble *ad = NULL;
2189 struct smb_filename *smb_fname_base = NULL;
2190 char *adpath = NULL;
2191 int hostfd = -1;
2193 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2195 SMB_VFS_HANDLE_GET_DATA(handle, config,
2196 struct fruit_config_data, return -1);
2198 switch (config->rsrc) {
2199 case FRUIT_RSRC_STREAM:
2200 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2201 case FRUIT_RSRC_XATTR:
2202 #ifdef HAVE_ATTROPEN
2203 hostfd = attropen(smb_fname->base_name,
2204 AFPRESOURCE_EA_NETATALK, flags, mode);
2205 if (hostfd == -1) {
2206 return -1;
2208 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2209 handle, ADOUBLE_RSRC, fsp);
2210 if (ad == NULL) {
2211 rc = -1;
2212 goto exit;
2214 goto exit;
2215 #else
2216 errno = ENOTSUP;
2217 return -1;
2218 #endif
2219 default:
2220 break;
2223 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2224 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2225 if (rc != 0) {
2226 rc = -1;
2227 goto exit;
2231 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2232 /* sorry, but directories don't habe a resource fork */
2233 rc = -1;
2234 goto exit;
2237 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2238 if (rc != 0) {
2239 goto exit;
2242 /* Create an smb_filename with stream_name == NULL. */
2243 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2244 adpath, NULL, NULL);
2245 if (smb_fname_base == NULL) {
2246 errno = ENOMEM;
2247 rc = -1;
2248 goto exit;
2251 /* Sanitize flags */
2252 if (flags & O_WRONLY) {
2253 /* We always need read access for the metadata header too */
2254 flags &= ~O_WRONLY;
2255 flags |= O_RDWR;
2258 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2259 flags, mode);
2260 if (hostfd == -1) {
2261 rc = -1;
2262 goto exit;
2265 /* REVIEW: we need this in ad_write() */
2266 fsp->fh->fd = hostfd;
2268 if (flags & (O_CREAT | O_TRUNC)) {
2269 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2270 handle, ADOUBLE_RSRC, fsp);
2271 if (ad == NULL) {
2272 rc = -1;
2273 goto exit;
2275 rc = ad_write(ad, smb_fname->base_name);
2276 if (rc != 0) {
2277 rc = -1;
2278 goto exit;
2280 } else {
2281 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2282 handle, ADOUBLE_RSRC, fsp);
2283 if (ad == NULL) {
2284 rc = -1;
2285 goto exit;
2287 if (ad_read(ad, smb_fname->base_name) == -1) {
2288 rc = -1;
2289 goto exit;
2293 exit:
2295 TALLOC_FREE(adpath);
2296 TALLOC_FREE(smb_fname_base);
2298 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2299 if (rc != 0) {
2300 int saved_errno = errno;
2301 if (hostfd >= 0) {
2303 * BUGBUGBUG -- we would need to call
2304 * fd_close_posix here, but we don't have a
2305 * full fsp yet
2307 fsp->fh->fd = hostfd;
2308 SMB_VFS_CLOSE(fsp);
2310 hostfd = -1;
2311 errno = saved_errno;
2313 return hostfd;
2316 static int fruit_open(vfs_handle_struct *handle,
2317 struct smb_filename *smb_fname,
2318 files_struct *fsp, int flags, mode_t mode)
2320 DEBUG(10, ("fruit_open called for %s\n",
2321 smb_fname_str_dbg(smb_fname)));
2323 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2324 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2327 if (is_afpinfo_stream(smb_fname)) {
2328 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2329 } else if (is_afpresource_stream(smb_fname)) {
2330 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2333 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2336 static int fruit_rename(struct vfs_handle_struct *handle,
2337 const struct smb_filename *smb_fname_src,
2338 const struct smb_filename *smb_fname_dst)
2340 int rc = -1;
2341 char *src_adouble_path = NULL;
2342 char *dst_adouble_path = NULL;
2343 struct fruit_config_data *config = NULL;
2345 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2347 if (!VALID_STAT(smb_fname_src->st)
2348 || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2349 return rc;
2352 SMB_VFS_HANDLE_GET_DATA(handle, config,
2353 struct fruit_config_data, return -1);
2355 if (config->rsrc == FRUIT_RSRC_XATTR) {
2356 return rc;
2359 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2360 &src_adouble_path);
2361 if (rc != 0) {
2362 goto done;
2364 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2365 &dst_adouble_path);
2366 if (rc != 0) {
2367 goto done;
2370 DEBUG(10, ("fruit_rename: %s -> %s\n",
2371 src_adouble_path, dst_adouble_path));
2373 rc = rename(src_adouble_path, dst_adouble_path);
2374 if (errno == ENOENT) {
2375 rc = 0;
2378 TALLOC_FREE(src_adouble_path);
2379 TALLOC_FREE(dst_adouble_path);
2381 done:
2382 return rc;
2385 static int fruit_unlink(vfs_handle_struct *handle,
2386 const struct smb_filename *smb_fname)
2388 int rc = -1;
2389 struct fruit_config_data *config = NULL;
2390 char *adp = NULL;
2392 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2393 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2396 SMB_VFS_HANDLE_GET_DATA(handle, config,
2397 struct fruit_config_data, return -1);
2399 if (is_afpinfo_stream(smb_fname)) {
2400 if (config->meta == FRUIT_META_STREAM) {
2401 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2402 } else {
2403 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2404 smb_fname->base_name,
2405 AFPINFO_EA_NETATALK);
2407 } else if (is_afpresource_stream(smb_fname)) {
2408 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2409 rc = adouble_path(talloc_tos(),
2410 smb_fname->base_name, &adp);
2411 if (rc != 0) {
2412 return -1;
2414 /* FIXME: direct unlink(), missing smb_fname */
2415 rc = unlink(adp);
2416 if ((rc == -1) && (errno == ENOENT)) {
2417 rc = 0;
2419 } else {
2420 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2421 smb_fname->base_name,
2422 AFPRESOURCE_EA_NETATALK);
2424 } else {
2425 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2428 TALLOC_FREE(adp);
2429 return rc;
2432 static int fruit_chmod(vfs_handle_struct *handle,
2433 const char *path,
2434 mode_t mode)
2436 int rc = -1;
2437 char *adp = NULL;
2438 struct fruit_config_data *config = NULL;
2439 SMB_STRUCT_STAT sb;
2441 rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2442 if (rc != 0) {
2443 return rc;
2446 SMB_VFS_HANDLE_GET_DATA(handle, config,
2447 struct fruit_config_data, return -1);
2449 if (config->rsrc == FRUIT_RSRC_XATTR) {
2450 return 0;
2453 /* FIXME: direct sys_lstat(), missing smb_fname */
2454 rc = sys_lstat(path, &sb, false);
2455 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2456 return rc;
2459 rc = adouble_path(talloc_tos(), path, &adp);
2460 if (rc != 0) {
2461 return -1;
2464 DEBUG(10, ("fruit_chmod: %s\n", adp));
2466 rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2467 if (errno == ENOENT) {
2468 rc = 0;
2471 TALLOC_FREE(adp);
2472 return rc;
2475 static int fruit_chown(vfs_handle_struct *handle,
2476 const char *path,
2477 uid_t uid,
2478 gid_t gid)
2480 int rc = -1;
2481 char *adp = NULL;
2482 struct fruit_config_data *config = NULL;
2483 SMB_STRUCT_STAT sb;
2485 rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2486 if (rc != 0) {
2487 return rc;
2490 SMB_VFS_HANDLE_GET_DATA(handle, config,
2491 struct fruit_config_data, return -1);
2493 if (config->rsrc == FRUIT_RSRC_XATTR) {
2494 return rc;
2497 /* FIXME: direct sys_lstat(), missing smb_fname */
2498 rc = sys_lstat(path, &sb, false);
2499 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2500 return rc;
2503 rc = adouble_path(talloc_tos(), path, &adp);
2504 if (rc != 0) {
2505 goto done;
2508 DEBUG(10, ("fruit_chown: %s\n", adp));
2510 rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2511 if (errno == ENOENT) {
2512 rc = 0;
2515 done:
2516 TALLOC_FREE(adp);
2517 return rc;
2520 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2522 DIR *dh = NULL;
2523 struct dirent *de;
2524 struct fruit_config_data *config;
2526 SMB_VFS_HANDLE_GET_DATA(handle, config,
2527 struct fruit_config_data, return -1);
2529 if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2530 goto exit_rmdir;
2534 * Due to there is no way to change bDeleteVetoFiles variable
2535 * from this module, need to clean up ourselves
2537 dh = opendir(path);
2538 if (dh == NULL) {
2539 goto exit_rmdir;
2542 while ((de = readdir(dh)) != NULL) {
2543 if ((strncmp(de->d_name,
2544 ADOUBLE_NAME_PREFIX,
2545 strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2546 char *p = talloc_asprintf(talloc_tos(),
2547 "%s/%s",
2548 path, de->d_name);
2549 if (p == NULL) {
2550 goto exit_rmdir;
2552 DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2553 (void)unlink(p);
2554 TALLOC_FREE(p);
2558 exit_rmdir:
2559 if (dh) {
2560 closedir(dh);
2562 return SMB_VFS_NEXT_RMDIR(handle, path);
2565 static ssize_t fruit_pread(vfs_handle_struct *handle,
2566 files_struct *fsp, void *data,
2567 size_t n, off_t offset)
2569 int rc = 0;
2570 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2571 handle, fsp);
2572 struct fruit_config_data *config = NULL;
2573 AfpInfo *ai = NULL;
2574 ssize_t len;
2575 char *name = NULL;
2576 char *tmp_base_name = NULL;
2577 NTSTATUS status;
2579 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2581 if (!fsp->base_fsp) {
2582 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2585 SMB_VFS_HANDLE_GET_DATA(handle, config,
2586 struct fruit_config_data, return -1);
2588 /* fsp_name is not converted with vfs_catia */
2589 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2590 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2591 fsp->base_fsp->fsp_name->base_name,
2592 vfs_translate_to_unix,
2593 talloc_tos(), &name);
2594 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2595 name = talloc_strdup(talloc_tos(), tmp_base_name);
2596 if (name == NULL) {
2597 rc = -1;
2598 goto exit;
2600 } else if (!NT_STATUS_IS_OK(status)) {
2601 errno = map_errno_from_nt_status(status);
2602 rc = -1;
2603 goto exit;
2605 fsp->base_fsp->fsp_name->base_name = name;
2607 if (ad == NULL) {
2608 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2609 if (len == -1) {
2610 rc = -1;
2611 goto exit;
2613 goto exit;
2616 if (!fruit_fsp_recheck(ad, fsp)) {
2617 rc = -1;
2618 goto exit;
2621 if (ad->ad_type == ADOUBLE_META) {
2622 ai = afpinfo_new(talloc_tos());
2623 if (ai == NULL) {
2624 rc = -1;
2625 goto exit;
2628 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2629 if (len == -1) {
2630 rc = -1;
2631 goto exit;
2634 memcpy(&ai->afpi_FinderInfo[0],
2635 ad_entry(ad, ADEID_FINDERI),
2636 ADEDLEN_FINDERI);
2637 len = afpinfo_pack(ai, data);
2638 if (len != AFP_INFO_SIZE) {
2639 rc = -1;
2640 goto exit;
2642 } else {
2643 len = SMB_VFS_NEXT_PREAD(
2644 handle, fsp, data, n,
2645 offset + ad_getentryoff(ad, ADEID_RFORK));
2646 if (len == -1) {
2647 rc = -1;
2648 goto exit;
2651 exit:
2652 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2653 TALLOC_FREE(name);
2654 TALLOC_FREE(ai);
2655 if (rc != 0) {
2656 len = -1;
2658 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2659 return len;
2662 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2663 files_struct *fsp, const void *data,
2664 size_t n, off_t offset)
2666 int rc = 0;
2667 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2668 handle, fsp);
2669 struct fruit_config_data *config = NULL;
2670 AfpInfo *ai = NULL;
2671 ssize_t len;
2672 char *name = NULL;
2673 char *tmp_base_name = NULL;
2674 NTSTATUS status;
2676 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2678 if (!fsp->base_fsp) {
2679 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2682 SMB_VFS_HANDLE_GET_DATA(handle, config,
2683 struct fruit_config_data, return -1);
2685 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2686 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2687 fsp->base_fsp->fsp_name->base_name,
2688 vfs_translate_to_unix,
2689 talloc_tos(), &name);
2690 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2691 name = talloc_strdup(talloc_tos(), tmp_base_name);
2692 if (name == NULL) {
2693 rc = -1;
2694 goto exit;
2696 } else if (!NT_STATUS_IS_OK(status)) {
2697 errno = map_errno_from_nt_status(status);
2698 rc = -1;
2699 goto exit;
2701 fsp->base_fsp->fsp_name->base_name = name;
2703 if (ad == NULL) {
2704 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2705 if (len != n) {
2706 rc = -1;
2707 goto exit;
2709 goto exit;
2712 if (!fruit_fsp_recheck(ad, fsp)) {
2713 rc = -1;
2714 goto exit;
2717 if (ad->ad_type == ADOUBLE_META) {
2718 if (n != AFP_INFO_SIZE || offset != 0) {
2719 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2720 (intmax_t)offset, (intmax_t)n));
2721 rc = -1;
2722 goto exit;
2724 ai = afpinfo_unpack(talloc_tos(), data);
2725 if (ai == NULL) {
2726 rc = -1;
2727 goto exit;
2729 memcpy(ad_entry(ad, ADEID_FINDERI),
2730 &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2731 rc = ad_write(ad, name);
2732 } else {
2733 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2734 offset + ad_getentryoff(ad, ADEID_RFORK));
2735 if (len != n) {
2736 rc = -1;
2737 goto exit;
2740 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2741 rc = ad_read(ad, name);
2742 if (rc == -1) {
2743 goto exit;
2745 rc = 0;
2747 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2748 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2749 rc = ad_write(ad, name);
2754 exit:
2755 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2756 TALLOC_FREE(name);
2757 TALLOC_FREE(ai);
2758 if (rc != 0) {
2759 return -1;
2761 return n;
2765 * Helper to stat/lstat the base file of an smb_fname.
2767 static int fruit_stat_base(vfs_handle_struct *handle,
2768 struct smb_filename *smb_fname,
2769 bool follow_links)
2771 char *tmp_stream_name;
2772 int rc;
2774 tmp_stream_name = smb_fname->stream_name;
2775 smb_fname->stream_name = NULL;
2776 if (follow_links) {
2777 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2778 } else {
2779 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2781 smb_fname->stream_name = tmp_stream_name;
2782 return rc;
2785 static int fruit_stat_meta(vfs_handle_struct *handle,
2786 struct smb_filename *smb_fname,
2787 bool follow_links)
2789 /* Populate the stat struct with info from the base file. */
2790 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2791 return -1;
2793 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2794 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2795 smb_fname->stream_name);
2796 return 0;
2799 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2800 struct smb_filename *smb_fname,
2801 bool follow_links)
2804 struct adouble *ad = NULL;
2806 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2807 smb_fname_str_dbg(smb_fname)));
2809 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2810 if (ad == NULL) {
2811 errno = ENOENT;
2812 return -1;
2815 /* Populate the stat struct with info from the base file. */
2816 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2817 TALLOC_FREE(ad);
2818 return -1;
2821 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2822 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2823 smb_fname->stream_name);
2824 TALLOC_FREE(ad);
2825 return 0;
2828 static int fruit_stat(vfs_handle_struct *handle,
2829 struct smb_filename *smb_fname)
2831 int rc = -1;
2833 DEBUG(10, ("fruit_stat called for %s\n",
2834 smb_fname_str_dbg(smb_fname)));
2836 if (!is_ntfs_stream_smb_fname(smb_fname)
2837 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2838 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2839 if (rc == 0) {
2840 update_btime(handle, smb_fname);
2842 return rc;
2846 * Note if lp_posix_paths() is true, we can never
2847 * get here as is_ntfs_stream_smb_fname() is
2848 * always false. So we never need worry about
2849 * not following links here.
2852 if (is_afpinfo_stream(smb_fname)) {
2853 rc = fruit_stat_meta(handle, smb_fname, true);
2854 } else if (is_afpresource_stream(smb_fname)) {
2855 rc = fruit_stat_rsrc(handle, smb_fname, true);
2856 } else {
2857 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2860 if (rc == 0) {
2861 update_btime(handle, smb_fname);
2862 smb_fname->st.st_ex_mode &= ~S_IFMT;
2863 smb_fname->st.st_ex_mode |= S_IFREG;
2864 smb_fname->st.st_ex_blocks =
2865 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2867 return rc;
2870 static int fruit_lstat(vfs_handle_struct *handle,
2871 struct smb_filename *smb_fname)
2873 int rc = -1;
2875 DEBUG(10, ("fruit_lstat called for %s\n",
2876 smb_fname_str_dbg(smb_fname)));
2878 if (!is_ntfs_stream_smb_fname(smb_fname)
2879 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2880 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2881 if (rc == 0) {
2882 update_btime(handle, smb_fname);
2884 return rc;
2887 if (is_afpinfo_stream(smb_fname)) {
2888 rc = fruit_stat_meta(handle, smb_fname, false);
2889 } else if (is_afpresource_stream(smb_fname)) {
2890 rc = fruit_stat_rsrc(handle, smb_fname, false);
2891 } else {
2892 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2895 if (rc == 0) {
2896 update_btime(handle, smb_fname);
2897 smb_fname->st.st_ex_mode &= ~S_IFMT;
2898 smb_fname->st.st_ex_mode |= S_IFREG;
2899 smb_fname->st.st_ex_blocks =
2900 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2902 return rc;
2905 static int fruit_fstat_meta(vfs_handle_struct *handle,
2906 files_struct *fsp,
2907 SMB_STRUCT_STAT *sbuf)
2909 DEBUG(10, ("fruit_fstat_meta called for %s\n",
2910 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2912 /* Populate the stat struct with info from the base file. */
2913 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2914 return -1;
2916 *sbuf = fsp->base_fsp->fsp_name->st;
2917 sbuf->st_ex_size = AFP_INFO_SIZE;
2918 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2920 return 0;
2923 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
2924 SMB_STRUCT_STAT *sbuf)
2926 struct fruit_config_data *config;
2927 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2928 handle, fsp);
2930 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
2931 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2933 SMB_VFS_HANDLE_GET_DATA(handle, config,
2934 struct fruit_config_data, return -1);
2936 if (config->rsrc == FRUIT_RSRC_STREAM) {
2937 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2940 /* Populate the stat struct with info from the base file. */
2941 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2942 return -1;
2944 *sbuf = fsp->base_fsp->fsp_name->st;
2945 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2946 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2948 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
2949 smb_fname_str_dbg(fsp->fsp_name),
2950 (ssize_t)sbuf->st_ex_size));
2952 return 0;
2955 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
2956 SMB_STRUCT_STAT *sbuf)
2958 int rc;
2959 char *name = NULL;
2960 char *tmp_base_name = NULL;
2961 NTSTATUS status;
2962 struct adouble *ad = (struct adouble *)
2963 VFS_FETCH_FSP_EXTENSION(handle, fsp);
2965 DEBUG(10, ("fruit_fstat called for %s\n",
2966 smb_fname_str_dbg(fsp->fsp_name)));
2968 if (fsp->base_fsp) {
2969 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2970 /* fsp_name is not converted with vfs_catia */
2971 status = SMB_VFS_TRANSLATE_NAME(
2972 handle->conn,
2973 fsp->base_fsp->fsp_name->base_name,
2974 vfs_translate_to_unix,
2975 talloc_tos(), &name);
2977 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2978 name = talloc_strdup(talloc_tos(), tmp_base_name);
2979 if (name == NULL) {
2980 rc = -1;
2981 goto exit;
2983 } else if (!NT_STATUS_IS_OK(status)) {
2984 errno = map_errno_from_nt_status(status);
2985 rc = -1;
2986 goto exit;
2988 fsp->base_fsp->fsp_name->base_name = name;
2991 if (ad == NULL || fsp->base_fsp == NULL) {
2992 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2993 goto exit;
2996 if (!fruit_fsp_recheck(ad, fsp)) {
2997 rc = -1;
2998 goto exit;
3001 switch (ad->ad_type) {
3002 case ADOUBLE_META:
3003 rc = fruit_fstat_meta(handle, fsp, sbuf);
3004 break;
3005 case ADOUBLE_RSRC:
3006 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
3007 break;
3008 default:
3009 DEBUG(10, ("fruit_fstat %s: bad type\n",
3010 smb_fname_str_dbg(fsp->fsp_name)));
3011 rc = -1;
3012 goto exit;
3015 if (rc == 0) {
3016 sbuf->st_ex_mode &= ~S_IFMT;
3017 sbuf->st_ex_mode |= S_IFREG;
3018 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3021 exit:
3022 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3023 smb_fname_str_dbg(fsp->fsp_name),
3024 (ssize_t)sbuf->st_ex_size));
3025 if (tmp_base_name) {
3026 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3028 TALLOC_FREE(name);
3029 return rc;
3032 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3033 struct files_struct *fsp,
3034 const char *fname,
3035 TALLOC_CTX *mem_ctx,
3036 unsigned int *pnum_streams,
3037 struct stream_struct **pstreams)
3039 struct fruit_config_data *config = NULL;
3040 struct smb_filename *smb_fname = NULL;
3041 struct adouble *ad = NULL;
3043 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3044 return NT_STATUS_UNSUCCESSFUL);
3045 DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
3047 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
3048 if (smb_fname == NULL) {
3049 return NT_STATUS_NO_MEMORY;
3052 if (config->meta == FRUIT_META_NETATALK) {
3053 ad = ad_get(talloc_tos(), handle,
3054 smb_fname->base_name, ADOUBLE_META);
3055 if (ad && !empty_finderinfo(ad)) {
3056 if (!add_fruit_stream(
3057 mem_ctx, pnum_streams, pstreams,
3058 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3059 smb_roundup(handle->conn,
3060 AFP_INFO_SIZE))) {
3061 TALLOC_FREE(ad);
3062 TALLOC_FREE(smb_fname);
3063 return NT_STATUS_NO_MEMORY;
3066 TALLOC_FREE(ad);
3069 if (config->rsrc != FRUIT_RSRC_STREAM) {
3070 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3071 ADOUBLE_RSRC);
3072 if (ad) {
3073 if (!add_fruit_stream(
3074 mem_ctx, pnum_streams, pstreams,
3075 AFPRESOURCE_STREAM_NAME,
3076 ad_getentrylen(ad, ADEID_RFORK),
3077 smb_roundup(handle->conn,
3078 ad_getentrylen(
3079 ad, ADEID_RFORK)))) {
3080 TALLOC_FREE(ad);
3081 TALLOC_FREE(smb_fname);
3082 return NT_STATUS_NO_MEMORY;
3085 TALLOC_FREE(ad);
3088 TALLOC_FREE(smb_fname);
3090 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3091 pnum_streams, pstreams);
3094 static int fruit_ntimes(vfs_handle_struct *handle,
3095 const struct smb_filename *smb_fname,
3096 struct smb_file_time *ft)
3098 int rc = 0;
3099 struct adouble *ad = NULL;
3101 if (null_timespec(ft->create_time)) {
3102 goto exit;
3105 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3106 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3108 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3109 if (ad == NULL) {
3110 goto exit;
3113 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3114 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3116 rc = ad_write(ad, smb_fname->base_name);
3118 exit:
3120 TALLOC_FREE(ad);
3121 if (rc != 0) {
3122 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3123 return -1;
3125 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3128 static int fruit_fallocate(struct vfs_handle_struct *handle,
3129 struct files_struct *fsp,
3130 uint32_t mode,
3131 off_t offset,
3132 off_t len)
3134 struct adouble *ad =
3135 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3137 if (ad == NULL) {
3138 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3141 if (!fruit_fsp_recheck(ad, fsp)) {
3142 return -1;
3145 /* Let the pwrite code path handle it. */
3146 errno = ENOSYS;
3147 return -1;
3150 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3151 struct files_struct *fsp,
3152 off_t offset)
3154 int rc = 0;
3155 struct adouble *ad =
3156 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3157 struct fruit_config_data *config;
3159 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3160 fsp_str_dbg(fsp), (double)offset));
3162 if (ad == NULL) {
3163 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3166 if (!fruit_fsp_recheck(ad, fsp)) {
3167 return -1;
3170 SMB_VFS_HANDLE_GET_DATA(handle, config,
3171 struct fruit_config_data, return -1);
3173 switch (ad->ad_type) {
3174 case ADOUBLE_META:
3176 * As this request hasn't been seen in the wild,
3177 * the only sensible use I can imagine is the client
3178 * truncating the stream to 0 bytes size.
3179 * We simply remove the metadata on such a request.
3181 if (offset == 0) {
3182 rc = SMB_VFS_FREMOVEXATTR(fsp,
3183 AFPRESOURCE_EA_NETATALK);
3185 break;
3186 case ADOUBLE_RSRC:
3187 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3188 rc = SMB_VFS_FREMOVEXATTR(fsp,
3189 AFPRESOURCE_EA_NETATALK);
3190 } else {
3191 rc = SMB_VFS_NEXT_FTRUNCATE(
3192 handle, fsp,
3193 offset + ad_getentryoff(ad, ADEID_RFORK));
3194 if (rc != 0) {
3195 return -1;
3197 ad_setentrylen(ad, ADEID_RFORK, offset);
3198 rc = ad_write(ad, NULL);
3199 if (rc != 0) {
3200 return -1;
3203 break;
3204 default:
3205 return -1;
3208 return rc;
3211 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3212 struct smb_request *req,
3213 uint16_t root_dir_fid,
3214 struct smb_filename *smb_fname,
3215 uint32_t access_mask,
3216 uint32_t share_access,
3217 uint32_t create_disposition,
3218 uint32_t create_options,
3219 uint32_t file_attributes,
3220 uint32_t oplock_request,
3221 struct smb2_lease *lease,
3222 uint64_t allocation_size,
3223 uint32_t private_flags,
3224 struct security_descriptor *sd,
3225 struct ea_list *ea_list,
3226 files_struct **result,
3227 int *pinfo,
3228 const struct smb2_create_blobs *in_context_blobs,
3229 struct smb2_create_blobs *out_context_blobs)
3231 NTSTATUS status;
3232 struct fruit_config_data *config = NULL;
3234 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3235 if (!NT_STATUS_IS_OK(status)) {
3236 return status;
3239 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3240 return NT_STATUS_UNSUCCESSFUL);
3242 status = SMB_VFS_NEXT_CREATE_FILE(
3243 handle, req, root_dir_fid, smb_fname,
3244 access_mask, share_access,
3245 create_disposition, create_options,
3246 file_attributes, oplock_request,
3247 lease,
3248 allocation_size, private_flags,
3249 sd, ea_list, result,
3250 pinfo, in_context_blobs, out_context_blobs);
3251 if (!NT_STATUS_IS_OK(status)) {
3252 return status;
3255 if (is_ntfs_stream_smb_fname(smb_fname)
3256 || (*result == NULL)
3257 || ((*result)->is_directory)) {
3258 return status;
3261 if (config->locking == FRUIT_LOCKING_NETATALK) {
3262 status = fruit_check_access(
3263 handle, *result,
3264 access_mask,
3265 map_share_mode_to_deny_mode(share_access, 0));
3266 if (!NT_STATUS_IS_OK(status)) {
3267 goto fail;
3271 return status;
3273 fail:
3274 DEBUG(1, ("fruit_create_file: %s\n", nt_errstr(status)));
3276 if (*result) {
3277 close_file(req, *result, ERROR_CLOSE);
3278 *result = NULL;
3281 return status;
3284 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3285 const struct smb_filename *fname,
3286 TALLOC_CTX *mem_ctx,
3287 struct readdir_attr_data **pattr_data)
3289 struct fruit_config_data *config = NULL;
3290 struct readdir_attr_data *attr_data;
3291 NTSTATUS status;
3293 SMB_VFS_HANDLE_GET_DATA(handle, config,
3294 struct fruit_config_data,
3295 return NT_STATUS_UNSUCCESSFUL);
3297 if (!config->use_aapl) {
3298 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3301 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3303 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3304 if (*pattr_data == NULL) {
3305 return NT_STATUS_UNSUCCESSFUL;
3307 attr_data = *pattr_data;
3308 attr_data->type = RDATTR_AAPL;
3311 * Mac metadata: compressed FinderInfo, resource fork length
3312 * and creation date
3314 status = readdir_attr_macmeta(handle, fname, attr_data);
3315 if (!NT_STATUS_IS_OK(status)) {
3317 * Error handling is tricky: if we return failure from
3318 * this function, the corresponding directory entry
3319 * will to be passed to the client, so we really just
3320 * want to error out on fatal errors.
3322 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3323 goto fail;
3328 * UNIX mode
3330 if (config->unix_info_enabled) {
3331 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3335 * max_access
3337 if (!config->readdir_attr_max_access) {
3338 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3339 } else {
3340 status = smbd_calculate_access_mask(
3341 handle->conn,
3342 fname,
3343 false,
3344 SEC_FLAG_MAXIMUM_ALLOWED,
3345 &attr_data->attr_data.aapl.max_access);
3346 if (!NT_STATUS_IS_OK(status)) {
3347 goto fail;
3351 return NT_STATUS_OK;
3353 fail:
3354 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3355 fname->base_name, nt_errstr(status)));
3356 TALLOC_FREE(*pattr_data);
3357 return status;
3360 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3361 files_struct *fsp,
3362 uint32_t security_info,
3363 TALLOC_CTX *mem_ctx,
3364 struct security_descriptor **ppdesc)
3366 NTSTATUS status;
3367 struct security_ace ace;
3368 struct dom_sid sid;
3369 struct fruit_config_data *config;
3371 SMB_VFS_HANDLE_GET_DATA(handle, config,
3372 struct fruit_config_data,
3373 return NT_STATUS_UNSUCCESSFUL);
3375 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3376 mem_ctx, ppdesc);
3377 if (!NT_STATUS_IS_OK(status)) {
3378 return status;
3382 * Add MS NFS style ACEs with uid, gid and mode
3384 if (!config->unix_info_enabled) {
3385 return NT_STATUS_OK;
3388 /* MS NFS style mode */
3389 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3390 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3391 status = security_descriptor_dacl_add(*ppdesc, &ace);
3392 if (!NT_STATUS_IS_OK(status)) {
3393 DEBUG(1,("failed to add MS NFS style ACE\n"));
3394 return status;
3397 /* MS NFS style uid */
3398 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3399 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3400 status = security_descriptor_dacl_add(*ppdesc, &ace);
3401 if (!NT_STATUS_IS_OK(status)) {
3402 DEBUG(1,("failed to add MS NFS style ACE\n"));
3403 return status;
3406 /* MS NFS style gid */
3407 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3408 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3409 status = security_descriptor_dacl_add(*ppdesc, &ace);
3410 if (!NT_STATUS_IS_OK(status)) {
3411 DEBUG(1,("failed to add MS NFS style ACE\n"));
3412 return status;
3415 return NT_STATUS_OK;
3418 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3419 files_struct *fsp,
3420 uint32_t security_info_sent,
3421 const struct security_descriptor *psd)
3423 NTSTATUS status;
3424 bool do_chmod;
3425 mode_t ms_nfs_mode;
3426 int result;
3428 DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp)));
3430 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3431 if (!NT_STATUS_IS_OK(status)) {
3432 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3433 return status;
3436 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3437 if (!NT_STATUS_IS_OK(status)) {
3438 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3439 return status;
3442 if (do_chmod) {
3443 if (fsp->fh->fd != -1) {
3444 DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp)));
3445 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3446 } else {
3447 DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp)));
3448 result = SMB_VFS_CHMOD(fsp->conn,
3449 fsp->fsp_name->base_name,
3450 ms_nfs_mode);
3453 if (result != 0) {
3454 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3455 result, (unsigned)ms_nfs_mode,
3456 strerror(errno)));
3457 status = map_nt_error_from_unix(errno);
3458 return status;
3462 return NT_STATUS_OK;
3465 static struct vfs_fn_pointers vfs_fruit_fns = {
3466 .connect_fn = fruit_connect,
3468 /* File operations */
3469 .chmod_fn = fruit_chmod,
3470 .chown_fn = fruit_chown,
3471 .unlink_fn = fruit_unlink,
3472 .rename_fn = fruit_rename,
3473 .rmdir_fn = fruit_rmdir,
3474 .open_fn = fruit_open,
3475 .pread_fn = fruit_pread,
3476 .pwrite_fn = fruit_pwrite,
3477 .stat_fn = fruit_stat,
3478 .lstat_fn = fruit_lstat,
3479 .fstat_fn = fruit_fstat,
3480 .streaminfo_fn = fruit_streaminfo,
3481 .ntimes_fn = fruit_ntimes,
3482 .ftruncate_fn = fruit_ftruncate,
3483 .fallocate_fn = fruit_fallocate,
3484 .create_file_fn = fruit_create_file,
3485 .readdir_attr_fn = fruit_readdir_attr,
3487 /* NT ACL operations */
3488 .fget_nt_acl_fn = fruit_fget_nt_acl,
3489 .fset_nt_acl_fn = fruit_fset_nt_acl,
3492 NTSTATUS vfs_fruit_init(void);
3493 NTSTATUS vfs_fruit_init(void)
3495 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3496 &vfs_fruit_fns);
3497 if (!NT_STATUS_IS_OK(ret)) {
3498 return ret;
3501 vfs_fruit_debug_level = debug_add_class("fruit");
3502 if (vfs_fruit_debug_level == -1) {
3503 vfs_fruit_debug_level = DBGC_VFS;
3504 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3505 "vfs_fruit_init"));
3506 } else {
3507 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3508 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3511 return ret;