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/>.
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"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
38 * Enhanced OS X and Netatalk compatibility
39 * ========================================
41 * This modules takes advantage of vfs_streams_xattr and
42 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
43 * loaded in the correct order:
45 * vfs modules = catia fruit streams_xattr
47 * The module intercepts the OS X special streams "AFP_AfpInfo" and
48 * "AFP_Resource" and handles them in a special way. All other named
49 * streams are deferred to vfs_streams_xattr.
51 * The OS X client maps all NTFS illegal characters to the Unicode
52 * private range. This module optionally stores the charcters using
53 * their native ASCII encoding using vfs_catia. If you're not enabling
54 * this feature, you can skip catia from vfs modules.
56 * Finally, open modes are optionally checked against Netatalk AFP
59 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
60 * extended metadata for files and directories. This module optionally
61 * reads and stores this metadata in a way compatible with Netatalk 3
62 * which stores the metadata in an EA "org.netatalk.metadata". Cf
63 * source3/include/MacExtensions.h for a description of the binary
66 * The "AFP_Resource" named stream may be arbitrarily large, thus it
67 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
68 * the only available filesystem where xattrs can be of any size and
69 * the OS supports using the file APIs for xattrs.
71 * The AFP_Resource stream is stored in an AppleDouble file prepending
72 * "._" to the filename. On Solaris with ZFS the stream is optionally
73 * stored in an EA "org.netatalk.resource".
79 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
80 * other protocols you may want to adjust the xattr names the VFS
81 * module vfs_streams_xattr uses for storing ADS's. This defaults to
82 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
83 * these module parameters:
85 * streams_xattr:prefix = user.
86 * streams_xattr:store_stream_type = false
92 * - log diagnostic if any needed VFS module is not loaded
93 * (eg with lp_vfs_objects())
97 static int vfs_fruit_debug_level = DBGC_VFS;
99 static struct global_fruit_config {
100 bool nego_aapl; /* client negotiated AAPL */
102 } global_fruit_config;
105 #define DBGC_CLASS vfs_fruit_debug_level
107 #define FRUIT_PARAM_TYPE_NAME "fruit"
108 #define ADOUBLE_NAME_PREFIX "._"
110 #define NETATALK_META_XATTR "org.netatalk.Metadata"
111 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
113 #if defined(HAVE_ATTROPEN)
114 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
115 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
117 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
118 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
121 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
123 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
124 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
125 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
126 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
128 struct fruit_config_data {
129 enum fruit_rsrc rsrc;
130 enum fruit_meta meta;
131 enum fruit_locking locking;
132 enum fruit_encoding encoding;
133 bool use_aapl; /* config from smb.conf */
135 bool readdir_attr_enabled;
136 bool unix_info_enabled;
137 bool copyfile_enabled;
138 bool veto_appledouble;
140 bool aapl_zero_file_id;
143 * Additional options, all enabled by default,
144 * possibly useful for analyzing performance. The associated
145 * operations with each of them may be expensive, so having
146 * the chance to disable them individually gives a chance
147 * tweaking the setup for the particular usecase.
149 bool readdir_attr_rsize;
150 bool readdir_attr_finder_info;
151 bool readdir_attr_max_access;
154 static const struct enum_list fruit_rsrc[] = {
155 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
156 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
157 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
161 static const struct enum_list fruit_meta[] = {
162 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
163 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
167 static const struct enum_list fruit_locking[] = {
168 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
169 {FRUIT_LOCKING_NONE, "none"},
173 static const struct enum_list fruit_encoding[] = {
174 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
175 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
179 /*****************************************************************************
180 * Defines, functions and data structures that deal with AppleDouble
181 *****************************************************************************/
184 * There are two AppleDouble blobs we deal with:
186 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
187 * metadata in an xattr
189 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
192 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
195 #define AD_VERSION2 0x00020000
196 #define AD_VERSION AD_VERSION2
199 * AppleDouble entry IDs.
201 #define ADEID_DFORK 1
202 #define ADEID_RFORK 2
204 #define ADEID_COMMENT 4
205 #define ADEID_ICONBW 5
206 #define ADEID_ICONCOL 6
207 #define ADEID_FILEI 7
208 #define ADEID_FILEDATESI 8
209 #define ADEID_FINDERI 9
210 #define ADEID_MACFILEI 10
211 #define ADEID_PRODOSFILEI 11
212 #define ADEID_MSDOSFILEI 12
213 #define ADEID_SHORTNAME 13
214 #define ADEID_AFPFILEI 14
217 /* Private Netatalk entries */
218 #define ADEID_PRIVDEV 16
219 #define ADEID_PRIVINO 17
220 #define ADEID_PRIVSYN 18
221 #define ADEID_PRIVID 19
222 #define ADEID_MAX (ADEID_PRIVID + 1)
225 * These are the real ids for the private entries,
226 * as stored in the adouble file
228 #define AD_DEV 0x80444556
229 #define AD_INO 0x80494E4F
230 #define AD_SYN 0x8053594E
231 #define AD_ID 0x8053567E
233 /* Number of actually used entries */
234 #define ADEID_NUM_XATTR 8
235 #define ADEID_NUM_DOT_UND 2
236 #define ADEID_NUM_RSRC_XATTR 1
238 /* AppleDouble magic */
239 #define AD_APPLESINGLE_MAGIC 0x00051600
240 #define AD_APPLEDOUBLE_MAGIC 0x00051607
241 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
243 /* Sizes of relevant entry bits */
244 #define ADEDLEN_MAGIC 4
245 #define ADEDLEN_VERSION 4
246 #define ADEDLEN_FILLER 16
247 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
248 #define ADEDLEN_NENTRIES 2
249 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
250 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
251 #define AD_ENTRY_LEN_EID 4
252 #define AD_ENTRY_LEN_OFF 4
253 #define AD_ENTRY_LEN_LEN 4
254 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
257 #define ADEDLEN_NAME 255
258 #define ADEDLEN_COMMENT 200
259 #define ADEDLEN_FILEI 16
260 #define ADEDLEN_FINDERI 32
261 #define ADEDLEN_FILEDATESI 16
262 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
263 #define ADEDLEN_AFPFILEI 4
264 #define ADEDLEN_MACFILEI 4
265 #define ADEDLEN_PRODOSFILEI 8
266 #define ADEDLEN_MSDOSFILEI 2
267 #define ADEDLEN_DID 4
268 #define ADEDLEN_PRIVDEV 8
269 #define ADEDLEN_PRIVINO 8
270 #define ADEDLEN_PRIVSYN 8
271 #define ADEDLEN_PRIVID 4
274 #define ADEDOFF_MAGIC 0
275 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
276 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
277 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
279 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
280 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
281 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
282 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
283 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
285 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
286 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
287 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
288 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
290 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
291 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
292 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
294 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
295 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
296 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
297 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
298 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
299 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
301 #if AD_DATASZ_XATTR != 402
302 #error bad size for AD_DATASZ_XATTR
305 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
306 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
308 #if AD_DATASZ_DOT_UND != 82
309 #error bad size for AD_DATASZ_DOT_UND
313 * Sharemode locks fcntl() offsets
315 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
316 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
318 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
320 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
322 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
323 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
324 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
325 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
326 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
327 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
328 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
329 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
330 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
331 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
333 /* Time stuff we overload the bits a little */
334 #define AD_DATE_CREATE 0
335 #define AD_DATE_MODIFY 4
336 #define AD_DATE_BACKUP 8
337 #define AD_DATE_ACCESS 12
338 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
339 AD_DATE_BACKUP | AD_DATE_ACCESS)
340 #define AD_DATE_UNIX (1 << 10)
341 #define AD_DATE_START 0x80000000
342 #define AD_DATE_DELTA 946684800
343 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
344 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
346 /* Accessor macros */
347 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
348 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
349 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
350 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
358 vfs_handle_struct *ad_handle;
361 adouble_type_t ad_type;
364 struct ad_entry ad_eid[ADEID_MAX];
368 struct ad_entry_order {
369 uint32_t id, offset, len;
372 /* Netatalk AppleDouble metadata xattr */
374 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
375 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
376 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
377 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
378 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
379 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
380 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
381 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
382 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
386 /* AppleDouble resource fork file (the ones prefixed by "._") */
388 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
389 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
390 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
395 * Fake AppleDouble entry oder for resource fork xattr. The xattr
396 * isn't an AppleDouble file, it simply contains the resource data,
397 * but in order to be able to use some API calls like ad_getentryoff()
398 * we build a fake/helper struct adouble with this entry order struct.
401 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
406 /* Conversion from enumerated id to on-disk AppleDouble id */
407 #define AD_EID_DISK(a) (set_eid[a])
408 static const uint32_t set_eid[] = {
409 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
410 AD_DEV, AD_INO, AD_SYN, AD_ID
414 /* tcon config handle */
415 struct fruit_config_data *config;
417 /* Denote stream type, meta or rsrc */
422 * Forward declarations
424 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
425 adouble_type_t type);
426 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
427 static int ad_fset(struct adouble *ad, files_struct *fsp);
428 static int adouble_path(TALLOC_CTX *ctx,
429 const struct smb_filename *smb_fname__in,
430 struct smb_filename **ppsmb_fname_out);
433 * Return a pointer to an AppleDouble entry
435 * Returns NULL if the entry is not present
437 static char *ad_get_entry(const struct adouble *ad, int eid)
439 off_t off = ad_getentryoff(ad, eid);
440 size_t len = ad_getentrylen(ad, eid);
442 if (off == 0 || len == 0) {
446 return ad->ad_data + off;
452 static int ad_getdate(const struct adouble *ad,
453 unsigned int dateoff,
456 bool xlate = (dateoff & AD_DATE_UNIX);
459 dateoff &= AD_DATE_MASK;
460 p = ad_get_entry(ad, ADEID_FILEDATESI);
465 if (dateoff > AD_DATE_ACCESS) {
469 memcpy(date, p + dateoff, sizeof(uint32_t));
472 *date = AD_DATE_TO_UNIX(*date);
480 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
482 bool xlate = (dateoff & AD_DATE_UNIX);
485 p = ad_get_entry(ad, ADEID_FILEDATESI);
490 dateoff &= AD_DATE_MASK;
492 date = AD_DATE_FROM_UNIX(date);
495 if (dateoff > AD_DATE_ACCESS) {
499 memcpy(p + dateoff, &date, sizeof(date));
506 * Map on-disk AppleDouble id to enumerated id
508 static uint32_t get_eid(uint32_t eid)
516 return ADEID_PRIVDEV;
518 return ADEID_PRIVINO;
520 return ADEID_PRIVSYN;
531 * Pack AppleDouble structure into data buffer
533 static bool ad_pack(struct adouble *ad)
540 bufsize = talloc_get_size(ad->ad_data);
542 if (offset + ADEDLEN_MAGIC < offset ||
543 offset + ADEDLEN_MAGIC >= bufsize) {
546 RSIVAL(ad->ad_data, offset, ad->ad_magic);
547 offset += ADEDLEN_MAGIC;
549 if (offset + ADEDLEN_VERSION < offset ||
550 offset + ADEDLEN_VERSION >= bufsize) {
553 RSIVAL(ad->ad_data, offset, ad->ad_version);
554 offset += ADEDLEN_VERSION;
556 if (offset + ADEDLEN_FILLER < offset ||
557 offset + ADEDLEN_FILLER >= bufsize) {
560 if (ad->ad_type == ADOUBLE_RSRC) {
561 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
563 offset += ADEDLEN_FILLER;
565 if (offset + ADEDLEN_NENTRIES < offset ||
566 offset + ADEDLEN_NENTRIES >= bufsize) {
569 offset += ADEDLEN_NENTRIES;
571 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
572 if (ad->ad_eid[eid].ade_off == 0) {
574 * ade_off is also used as indicator whether a
575 * specific entry is used or not
580 if (offset + AD_ENTRY_LEN_EID < offset ||
581 offset + AD_ENTRY_LEN_EID >= bufsize) {
584 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
585 offset += AD_ENTRY_LEN_EID;
587 if (offset + AD_ENTRY_LEN_OFF < offset ||
588 offset + AD_ENTRY_LEN_OFF >= bufsize) {
591 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
592 offset += AD_ENTRY_LEN_OFF;
594 if (offset + AD_ENTRY_LEN_LEN < offset ||
595 offset + AD_ENTRY_LEN_LEN >= bufsize) {
598 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
599 offset += AD_ENTRY_LEN_LEN;
604 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
607 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
613 * Unpack an AppleDouble blob into a struct adoble
615 static bool ad_unpack(struct adouble *ad, const size_t nentries,
618 size_t bufsize = talloc_get_size(ad->ad_data);
620 uint32_t eid, len, off;
623 * The size of the buffer ad->ad_data is checked when read, so
624 * we wouldn't have to check our own offsets, a few extra
625 * checks won't hurt though. We have to check the offsets we
626 * read from the buffer anyway.
629 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
630 DEBUG(1, ("bad size\n"));
634 ad->ad_magic = RIVAL(ad->ad_data, 0);
635 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
636 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
637 DEBUG(1, ("wrong magic or version\n"));
641 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
642 if (adentries != nentries) {
643 DEBUG(1, ("invalid number of entries: %zu\n",
648 /* now, read in the entry bits */
649 for (i = 0; i < adentries; i++) {
650 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
652 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
653 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
655 if (!eid || eid > ADEID_MAX) {
656 DEBUG(1, ("bogus eid %d\n", eid));
661 * All entries other than the resource fork are
662 * expected to be read into the ad_data buffer, so
663 * ensure the specified offset is within that bound
665 if ((off > bufsize) && (eid != ADEID_RFORK)) {
666 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
672 * All entries besides FinderInfo and resource fork
673 * must fit into the buffer. FinderInfo is special as
674 * it may be larger then the default 32 bytes (if it
675 * contains marshalled xattrs), but we will fixup that
676 * in ad_convert(). And the resource fork is never
677 * accessed directly by the ad_data buf (also see
678 * comment above) anyway.
680 if ((eid != ADEID_RFORK) &&
681 (eid != ADEID_FINDERI) &&
682 ((off + len) > bufsize)) {
683 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
689 * That would be obviously broken
691 if (off > filesize) {
692 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
698 * Check for any entry that has its end beyond the
701 if (off + len < off) {
702 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
703 ", len: %" PRIu32 "\n",
708 if (off + len > filesize) {
710 * If this is the resource fork entry, we fix
711 * up the length, for any other entry we bail
714 if (eid != ADEID_RFORK) {
715 DEBUG(1, ("bogus eid %d: off: %" PRIu32
716 ", len: %" PRIu32 "\n",
722 * Fixup the resource fork entry by limiting
723 * the size to entryoffset - filesize.
725 len = filesize - off;
726 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
727 ", len: %" PRIu32 "\n", off, len));
730 ad->ad_eid[eid].ade_off = off;
731 ad->ad_eid[eid].ade_len = len;
738 * Convert from Apple's ._ file to Netatalk
740 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
741 * bytes containing packed xattrs. Netatalk can't deal with that, so
742 * we simply discard the packed xattrs.
744 * @return -1 in case an error occurred, 0 if no conversion was done, 1
747 static int ad_convert(struct adouble *ad, int fd)
750 char *map = MAP_FAILED;
753 origlen = ad_getentryoff(ad, ADEID_RFORK) +
754 ad_getentrylen(ad, ADEID_RFORK);
756 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
757 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
758 if (map == MAP_FAILED) {
759 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
764 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
765 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
766 map + ad_getentryoff(ad, ADEID_RFORK),
767 ad_getentrylen(ad, ADEID_RFORK));
770 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
771 ad_setentryoff(ad, ADEID_RFORK,
772 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
775 * FIXME: direct ftruncate(), but we don't have a fsp for the
778 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
779 + ad_getentrylen(ad, ADEID_RFORK));
782 if (map != MAP_FAILED) {
783 munmap(map, origlen);
789 * Read and parse Netatalk AppleDouble metadata xattr
791 static ssize_t ad_read_meta(struct adouble *ad,
792 const struct smb_filename *smb_fname)
798 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
800 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
801 AFPINFO_EA_NETATALK, ad->ad_data,
807 if (errno == ENOATTR) {
813 DEBUG(2, ("error reading meta xattr: %s\n",
819 if (ealen != AD_DATASZ_XATTR) {
820 DEBUG(2, ("bad size %zd\n", ealen));
826 /* Now parse entries */
827 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
829 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
835 if (!ad_getentryoff(ad, ADEID_FINDERI)
836 || !ad_getentryoff(ad, ADEID_COMMENT)
837 || !ad_getentryoff(ad, ADEID_FILEDATESI)
838 || !ad_getentryoff(ad, ADEID_AFPFILEI)
839 || !ad_getentryoff(ad, ADEID_PRIVDEV)
840 || !ad_getentryoff(ad, ADEID_PRIVINO)
841 || !ad_getentryoff(ad, ADEID_PRIVSYN)
842 || !ad_getentryoff(ad, ADEID_PRIVID)) {
843 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
850 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
851 smb_fname->base_name, rc));
855 if (errno == EINVAL) {
857 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
865 static int ad_open_meta(const struct smb_filename *smb_fname,
869 return open(smb_fname->base_name, flags, mode);
872 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
877 /* FIXME: direct Solaris xattr syscall */
878 return attropen(smb_fname->base_name,
879 AFPRESOURCE_EA_NETATALK, flags, mode);
886 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
892 struct smb_filename *adp_smb_fname = NULL;
894 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
899 fd = open(adp_smb_fname->base_name, flags, mode);
900 TALLOC_FREE(adp_smb_fname);
905 static int ad_open_rsrc(vfs_handle_struct *handle,
906 const struct smb_filename *smb_fname,
910 struct fruit_config_data *config = NULL;
913 SMB_VFS_HANDLE_GET_DATA(handle, config,
914 struct fruit_config_data, return -1);
916 if (config->rsrc == FRUIT_RSRC_XATTR) {
917 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
919 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
925 static int ad_open(vfs_handle_struct *handle,
927 const struct smb_filename *smb_fname,
934 DBG_DEBUG("Path [%s] type [%s]\n",
935 smb_fname->base_name, t == ADOUBLE_META ? "meta" : "rsrc");
937 if (t == ADOUBLE_META) {
938 fd = ad_open_meta(smb_fname, flags, mode);
940 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
944 ad->ad_opened = true;
948 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
949 smb_fname->base_name,
950 t == ADOUBLE_META ? "meta" : "rsrc", fd);
955 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
960 /* FIXME: direct sys_fstat(), don't have an fsp */
961 ret = sys_fstat(ad->ad_fd, &st,
962 lp_fake_directory_create_times(
963 SNUM(ad->ad_handle->conn)));
968 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
969 return st.st_ex_size;
972 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
973 const struct smb_filename *smb_fname)
975 struct adouble *meta_ad = NULL;
976 SMB_STRUCT_STAT sbuf;
978 char *p_meta_ad = NULL;
983 len = sys_pread(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
984 if (len != AD_DATASZ_DOT_UND) {
985 DBG_NOTICE("%s %s: bad size: %zd\n",
986 smb_fname->base_name, strerror(errno), len);
990 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
991 SNUM(ad->ad_handle->conn)));
996 /* Now parse entries */
997 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
999 DBG_ERR("invalid AppleDouble resource %s\n",
1000 smb_fname->base_name);
1005 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1006 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1007 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1008 DBG_ERR("invalid AppleDouble resource %s\n",
1009 smb_fname->base_name);
1014 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1019 * Try to fixup AppleDouble files created by OS X with xattrs
1020 * appended to the ADEID_FINDERI entry. We simply remove the
1021 * xattrs blob, this means any fancy xattr that was stored
1025 ret = ad_convert(ad, ad->ad_fd);
1027 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1033 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1037 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1038 if (len != AD_DATASZ_DOT_UND) {
1039 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1043 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1044 if (meta_ad == NULL) {
1048 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1050 TALLOC_FREE(meta_ad);
1053 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1054 if (p_meta_ad == NULL) {
1055 TALLOC_FREE(meta_ad);
1059 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1061 ret = ad_set(meta_ad, smb_fname);
1062 TALLOC_FREE(meta_ad);
1071 * Read and parse resource fork, either ._ AppleDouble file or xattr
1073 static ssize_t ad_read_rsrc(struct adouble *ad,
1074 const struct smb_filename *smb_fname)
1076 struct fruit_config_data *config = NULL;
1079 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1080 struct fruit_config_data, return -1);
1082 if (config->rsrc == FRUIT_RSRC_XATTR) {
1083 len = ad_read_rsrc_xattr(ad);
1085 len = ad_read_rsrc_adouble(ad, smb_fname);
1092 * Read and unpack an AppleDouble metadata xattr or resource
1094 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1096 switch (ad->ad_type) {
1098 return ad_read_meta(ad, smb_fname);
1100 return ad_read_rsrc(ad, smb_fname);
1106 static int adouble_destructor(struct adouble *ad)
1108 if ((ad->ad_fd != -1) && ad->ad_opened) {
1116 * Allocate a struct adouble without initialiing it
1118 * The struct is either hang of the fsp extension context or if fsp is
1121 * @param[in] ctx talloc context
1122 * @param[in] handle vfs handle
1123 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1125 * @return adouble handle
1127 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1128 adouble_type_t type)
1133 struct fruit_config_data *config;
1135 SMB_VFS_HANDLE_GET_DATA(handle, config,
1136 struct fruit_config_data, return NULL);
1140 adsize = AD_DATASZ_XATTR;
1143 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1144 adsize = AD_DATASZ_DOT_UND;
1151 ad = talloc_zero(ctx, struct adouble);
1158 ad->ad_data = talloc_zero_array(ad, char, adsize);
1159 if (ad->ad_data == NULL) {
1165 ad->ad_handle = handle;
1167 ad->ad_magic = AD_MAGIC;
1168 ad->ad_version = AD_VERSION;
1171 talloc_set_destructor(ad, adouble_destructor);
1181 * Allocate and initialize a new struct adouble
1183 * @param[in] ctx talloc context
1184 * @param[in] handle vfs handle
1185 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1187 * @return adouble handle, initialized
1189 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1190 adouble_type_t type)
1193 const struct ad_entry_order *eid;
1194 struct adouble *ad = NULL;
1195 struct fruit_config_data *config;
1196 time_t t = time(NULL);
1198 SMB_VFS_HANDLE_GET_DATA(handle, config,
1199 struct fruit_config_data, return NULL);
1203 eid = entry_order_meta_xattr;
1206 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1207 eid = entry_order_dot_und;
1209 eid = entry_order_rsrc_xattr;
1216 ad = ad_alloc(ctx, handle, type);
1222 ad->ad_eid[eid->id].ade_off = eid->offset;
1223 ad->ad_eid[eid->id].ade_len = eid->len;
1227 /* put something sane in the date fields */
1228 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1229 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1230 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1231 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1240 * Return AppleDouble data for a file
1242 * @param[in] ctx talloc context
1243 * @param[in] handle vfs handle
1244 * @param[in] smb_fname pathname to file or directory
1245 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1247 * @return talloced struct adouble or NULL on error
1249 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1250 const struct smb_filename *smb_fname,
1251 adouble_type_t type)
1255 struct adouble *ad = NULL;
1259 DEBUG(10, ("ad_get(%s) called for %s\n",
1260 type == ADOUBLE_META ? "meta" : "rsrc",
1261 smb_fname->base_name));
1263 ad = ad_alloc(ctx, handle, type);
1270 * Here's the deal: for ADOUBLE_META we can do without an fd
1271 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1272 * however we need a full-fledged fd for file IO on the ._
1275 if (type == ADOUBLE_RSRC) {
1276 /* Try rw first so we can use the fd in ad_convert() */
1279 fd = ad_open(handle, ad, smb_fname, ADOUBLE_RSRC, mode, 0);
1280 if (fd == -1 && ((errno == EROFS) || (errno == EACCES))) {
1282 fd = ad_open(handle, ad, smb_fname,
1283 ADOUBLE_RSRC, mode, 0);
1287 DBG_DEBUG("ad_open [%s] error [%s]\n",
1288 smb_fname->base_name, strerror(errno));
1294 len = ad_read(ad, smb_fname);
1296 DEBUG(10, ("error reading AppleDouble for %s\n",
1297 smb_fname->base_name));
1303 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1304 type == ADOUBLE_META ? "meta" : "rsrc",
1305 smb_fname->base_name, rc));
1314 * Return AppleDouble data for a file
1316 * @param[in] ctx talloc context
1317 * @param[in] handle vfs handle
1318 * @param[in] fsp fsp to use for IO
1319 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1321 * @return talloced struct adouble or NULL on error
1323 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1324 files_struct *fsp, adouble_type_t type)
1328 struct adouble *ad = NULL;
1330 DBG_DEBUG("ad_get(%s) path [%s]\n",
1331 type == ADOUBLE_META ? "meta" : "rsrc",
1334 ad = ad_alloc(ctx, handle, type);
1340 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1341 ad->ad_fd = fsp->fh->fd;
1344 * Here's the deal: for ADOUBLE_META we can do without an fd
1345 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1346 * however we need a full-fledged fd for file IO on the ._
1352 if (type == ADOUBLE_RSRC) {
1353 /* Try rw first so we can use the fd in ad_convert() */
1356 fd = ad_open(handle, ad, fsp->base_fsp->fsp_name,
1357 ADOUBLE_RSRC, mode, 0);
1359 ((errno == EROFS) || (errno == EACCES)))
1362 fd = ad_open(handle, ad,
1363 fsp->base_fsp->fsp_name, ADOUBLE_RSRC,
1368 DBG_DEBUG("error opening AppleDouble for %s\n",
1376 len = ad_read(ad, fsp->base_fsp->fsp_name);
1378 DBG_DEBUG("error reading AppleDouble for %s\n",
1385 DBG_DEBUG("ad_get(%s) path [%s] rc [%d]\n",
1386 type == ADOUBLE_META ? "meta" : "rsrc",
1387 fsp_str_dbg(fsp), rc);
1396 * Set AppleDouble metadata on a file or directory
1398 * @param[in] ad adouble handle
1400 * @param[in] smb_fname pathname to file or directory
1402 * @return status code, 0 means success
1404 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1409 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1411 if (ad->ad_type != ADOUBLE_META) {
1412 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1413 smb_fname->base_name);
1422 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1424 AFPINFO_EA_NETATALK,
1426 AD_DATASZ_XATTR, 0);
1428 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1434 * Set AppleDouble metadata on a file or directory
1436 * @param[in] ad adouble handle
1437 * @param[in] fsp file handle
1439 * @return status code, 0 means success
1441 static int ad_fset(struct adouble *ad, files_struct *fsp)
1447 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1450 || (fsp->fh == NULL)
1451 || (fsp->fh->fd == -1))
1453 smb_panic("bad fsp");
1461 switch (ad->ad_type) {
1463 rc = SMB_VFS_NEXT_FSETXATTR(ad->ad_handle,
1465 AFPINFO_EA_NETATALK,
1467 AD_DATASZ_XATTR, 0);
1471 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1474 talloc_get_size(ad->ad_data),
1476 if (len != (ssize_t)talloc_get_size(ad->ad_data)) {
1477 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1487 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1492 /*****************************************************************************
1494 *****************************************************************************/
1496 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1498 if (strncasecmp_m(smb_fname->stream_name,
1499 AFPINFO_STREAM_NAME,
1500 strlen(AFPINFO_STREAM_NAME)) == 0) {
1506 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1508 if (strncasecmp_m(smb_fname->stream_name,
1509 AFPRESOURCE_STREAM_NAME,
1510 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1517 * Test whether stream is an Apple stream, not used atm
1520 static bool is_apple_stream(const struct smb_filename *smb_fname)
1522 if (is_afpinfo_stream(smb_fname)) {
1525 if (is_afpresource_stream(smb_fname)) {
1533 * Initialize config struct from our smb.conf config parameters
1535 static int init_fruit_config(vfs_handle_struct *handle)
1537 struct fruit_config_data *config;
1540 config = talloc_zero(handle->conn, struct fruit_config_data);
1542 DEBUG(1, ("talloc_zero() failed\n"));
1548 * Versions up to Samba 4.5.x had a spelling bug in the
1549 * fruit:resource option calling lp_parm_enum with
1550 * "res*s*ource" (ie two s).
1552 * In Samba 4.6 we accept both the wrong and the correct
1553 * spelling, in Samba 4.7 the bad spelling will be removed.
1555 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1556 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1557 if (enumval == -1) {
1558 DEBUG(1, ("value for %s: resource type unknown\n",
1559 FRUIT_PARAM_TYPE_NAME));
1562 config->rsrc = (enum fruit_rsrc)enumval;
1564 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1565 "resource", fruit_rsrc, enumval);
1566 if (enumval == -1) {
1567 DEBUG(1, ("value for %s: resource type unknown\n",
1568 FRUIT_PARAM_TYPE_NAME));
1571 config->rsrc = (enum fruit_rsrc)enumval;
1573 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1574 "metadata", fruit_meta, FRUIT_META_NETATALK);
1575 if (enumval == -1) {
1576 DEBUG(1, ("value for %s: metadata type unknown\n",
1577 FRUIT_PARAM_TYPE_NAME));
1580 config->meta = (enum fruit_meta)enumval;
1582 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1583 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1584 if (enumval == -1) {
1585 DEBUG(1, ("value for %s: locking type unknown\n",
1586 FRUIT_PARAM_TYPE_NAME));
1589 config->locking = (enum fruit_locking)enumval;
1591 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1592 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1593 if (enumval == -1) {
1594 DEBUG(1, ("value for %s: encoding type unknown\n",
1595 FRUIT_PARAM_TYPE_NAME));
1598 config->encoding = (enum fruit_encoding)enumval;
1600 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1601 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1602 FRUIT_PARAM_TYPE_NAME,
1607 config->use_aapl = lp_parm_bool(
1608 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1610 config->unix_info_enabled = lp_parm_bool(
1611 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1613 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1616 config->posix_rename = lp_parm_bool(
1617 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1619 config->aapl_zero_file_id =
1620 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1622 config->readdir_attr_rsize = lp_parm_bool(
1623 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1625 config->readdir_attr_finder_info = lp_parm_bool(
1626 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1628 config->readdir_attr_max_access = lp_parm_bool(
1629 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1631 SMB_VFS_HANDLE_SET_DATA(handle, config,
1632 NULL, struct fruit_config_data,
1639 * Prepend "._" to a basename
1640 * Return a new struct smb_filename with stream_name == NULL.
1642 static int adouble_path(TALLOC_CTX *ctx,
1643 const struct smb_filename *smb_fname_in,
1644 struct smb_filename **pp_smb_fname_out)
1648 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1651 if (smb_fname == NULL) {
1655 /* We need streamname to be NULL */
1656 TALLOC_FREE(smb_fname->stream_name);
1658 /* And we're replacing base_name. */
1659 TALLOC_FREE(smb_fname->base_name);
1661 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
1663 TALLOC_FREE(smb_fname);
1667 smb_fname->base_name = talloc_asprintf(smb_fname,
1668 "%s/._%s", parent, base);
1669 if (smb_fname->base_name == NULL) {
1670 TALLOC_FREE(smb_fname);
1674 *pp_smb_fname_out = smb_fname;
1680 * Allocate and initialize an AfpInfo struct
1682 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1684 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1688 ai->afpi_Signature = AFP_Signature;
1689 ai->afpi_Version = AFP_Version;
1690 ai->afpi_BackupTime = AD_DATE_START;
1695 * Pack an AfpInfo struct into a buffer
1697 * Buffer size must be at least AFP_INFO_SIZE
1698 * Returns size of packed buffer
1700 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1702 memset(buf, 0, AFP_INFO_SIZE);
1704 RSIVAL(buf, 0, ai->afpi_Signature);
1705 RSIVAL(buf, 4, ai->afpi_Version);
1706 RSIVAL(buf, 12, ai->afpi_BackupTime);
1707 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1709 return AFP_INFO_SIZE;
1713 * Unpack a buffer into a AfpInfo structure
1715 * Buffer size must be at least AFP_INFO_SIZE
1716 * Returns allocated AfpInfo struct
1718 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1720 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1725 ai->afpi_Signature = RIVAL(data, 0);
1726 ai->afpi_Version = RIVAL(data, 4);
1727 ai->afpi_BackupTime = RIVAL(data, 12);
1728 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1729 sizeof(ai->afpi_FinderInfo));
1731 if (ai->afpi_Signature != AFP_Signature
1732 || ai->afpi_Version != AFP_Version) {
1733 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1741 * Fake an inode number from the md5 hash of the (xattr) name
1743 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1746 unsigned char hash[16];
1750 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1751 SMB_ASSERT(upper_sname != NULL);
1754 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1755 sizeof(sbuf->st_ex_dev));
1756 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1757 sizeof(sbuf->st_ex_ino));
1758 MD5Update(&ctx, (unsigned char *)upper_sname,
1759 talloc_get_size(upper_sname)-1);
1760 MD5Final(hash, &ctx);
1762 TALLOC_FREE(upper_sname);
1764 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1765 memcpy(&result, hash, sizeof(result));
1767 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1768 sname, (unsigned long long)result));
1773 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1774 struct stream_struct **streams,
1775 const char *name, off_t size,
1778 struct stream_struct *tmp;
1780 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1786 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1787 if (tmp[*num_streams].name == NULL) {
1791 tmp[*num_streams].size = size;
1792 tmp[*num_streams].alloc_size = alloc_size;
1799 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1800 struct stream_struct **streams)
1802 struct stream_struct *tmp = *streams;
1805 if (*num_streams == 0) {
1809 for (i = 0; i < *num_streams; i++) {
1810 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1815 if (i == *num_streams) {
1819 if (tmp[i].size > 0) {
1823 TALLOC_FREE(tmp[i].name);
1824 if (*num_streams - 1 > i) {
1825 memmove(&tmp[i], &tmp[i+1],
1826 (*num_streams - i - 1) * sizeof(struct stream_struct));
1833 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1834 struct stream_struct **streams,
1837 struct stream_struct *tmp = *streams;
1840 if (*num_streams == 0) {
1844 for (i = 0; i < *num_streams; i++) {
1845 if (strequal_m(tmp[i].name, name)) {
1850 if (i == *num_streams) {
1854 TALLOC_FREE(tmp[i].name);
1855 if (*num_streams - 1 > i) {
1856 memmove(&tmp[i], &tmp[i+1],
1857 (*num_streams - i - 1) * sizeof(struct stream_struct));
1864 static bool ad_empty_finderinfo(const struct adouble *ad)
1867 char emptybuf[ADEDLEN_FINDERI] = {0};
1870 fi = ad_get_entry(ad, ADEID_FINDERI);
1872 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1876 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1880 static bool ai_empty_finderinfo(const AfpInfo *ai)
1883 char emptybuf[ADEDLEN_FINDERI] = {0};
1885 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1890 * Update btime with btime from Netatalk
1892 static void update_btime(vfs_handle_struct *handle,
1893 struct smb_filename *smb_fname)
1896 struct timespec creation_time = {0};
1898 struct fruit_config_data *config = NULL;
1900 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1903 switch (config->meta) {
1904 case FRUIT_META_STREAM:
1906 case FRUIT_META_NETATALK:
1910 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1914 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1918 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1924 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1925 update_stat_ex_create_time(&smb_fname->st, creation_time);
1931 * Map an access mask to a Netatalk single byte byte range lock
1933 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1934 uint32_t access_mask)
1938 switch (access_mask) {
1939 case FILE_READ_DATA:
1940 offset = AD_FILELOCK_OPEN_RD;
1943 case FILE_WRITE_DATA:
1944 case FILE_APPEND_DATA:
1945 offset = AD_FILELOCK_OPEN_WR;
1949 offset = AD_FILELOCK_OPEN_NONE;
1953 if (fork_type == APPLE_FORK_RSRC) {
1954 if (offset == AD_FILELOCK_OPEN_NONE) {
1955 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1965 * Map a deny mode to a Netatalk brl
1967 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1972 switch (deny_mode) {
1974 offset = AD_FILELOCK_DENY_RD;
1978 offset = AD_FILELOCK_DENY_WR;
1982 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1985 if (fork_type == APPLE_FORK_RSRC) {
1993 * Call fcntl() with an exclusive F_GETLK request in order to
1994 * determine if there's an exisiting shared lock
1996 * @return true if the requested lock was found or any error occurred
1997 * false if the lock was not found
1999 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2002 off_t offset = in_offset;
2007 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2008 if (result == false) {
2012 if (type != F_UNLCK) {
2019 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2021 uint32_t access_mask,
2024 NTSTATUS status = NT_STATUS_OK;
2025 struct byte_range_lock *br_lck = NULL;
2026 bool open_for_reading, open_for_writing, deny_read, deny_write;
2028 bool have_read = false;
2031 /* FIXME: hardcoded data fork, add resource fork */
2032 enum apple_fork fork_type = APPLE_FORK_DATA;
2034 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2036 access_mask & FILE_READ_DATA ? "READ" :"-",
2037 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2038 deny_mode & DENY_READ ? "DENY_READ" : "-",
2039 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2041 if (fsp->fh->fd == -1) {
2042 return NT_STATUS_OK;
2045 flags = fcntl(fsp->fh->fd, F_GETFL);
2047 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2048 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2049 return map_nt_error_from_unix(errno);
2052 if (flags & (O_RDONLY|O_RDWR)) {
2054 * Applying fcntl read locks requires an fd opened for
2055 * reading. This means we won't be applying locks for
2056 * files openend write-only, but what can we do...
2062 * Check read access and deny read mode
2064 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2066 open_for_reading = test_netatalk_lock(
2067 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2069 deny_read = test_netatalk_lock(
2070 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2072 DEBUG(10, ("read: %s, deny_write: %s\n",
2073 open_for_reading == true ? "yes" : "no",
2074 deny_read == true ? "yes" : "no"));
2076 if (((access_mask & FILE_READ_DATA) && deny_read)
2077 || ((deny_mode & DENY_READ) && open_for_reading)) {
2078 return NT_STATUS_SHARING_VIOLATION;
2082 if ((access_mask & FILE_READ_DATA) && have_read) {
2083 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2085 handle->conn->sconn->msg_ctx, fsp,
2086 fsp->op->global->open_persistent_id, 1, off,
2087 READ_LOCK, POSIX_LOCK, false,
2090 if (!NT_STATUS_IS_OK(status)) {
2093 TALLOC_FREE(br_lck);
2096 if ((deny_mode & DENY_READ) && have_read) {
2097 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2099 handle->conn->sconn->msg_ctx, fsp,
2100 fsp->op->global->open_persistent_id, 1, off,
2101 READ_LOCK, POSIX_LOCK, false,
2104 if (!NT_STATUS_IS_OK(status)) {
2107 TALLOC_FREE(br_lck);
2112 * Check write access and deny write mode
2114 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2116 open_for_writing = test_netatalk_lock(
2117 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2119 deny_write = test_netatalk_lock(
2120 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2122 DEBUG(10, ("write: %s, deny_write: %s\n",
2123 open_for_writing == true ? "yes" : "no",
2124 deny_write == true ? "yes" : "no"));
2126 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2127 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2128 return NT_STATUS_SHARING_VIOLATION;
2132 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2133 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2135 handle->conn->sconn->msg_ctx, fsp,
2136 fsp->op->global->open_persistent_id, 1, off,
2137 READ_LOCK, POSIX_LOCK, false,
2140 if (!NT_STATUS_IS_OK(status)) {
2143 TALLOC_FREE(br_lck);
2146 if ((deny_mode & DENY_WRITE) && have_read) {
2147 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2149 handle->conn->sconn->msg_ctx, fsp,
2150 fsp->op->global->open_persistent_id, 1, off,
2151 READ_LOCK, POSIX_LOCK, false,
2154 if (!NT_STATUS_IS_OK(status)) {
2157 TALLOC_FREE(br_lck);
2161 TALLOC_FREE(br_lck);
2166 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2167 struct smb_request *req,
2168 const struct smb2_create_blobs *in_context_blobs,
2169 struct smb2_create_blobs *out_context_blobs)
2171 struct fruit_config_data *config;
2173 struct smb2_create_blob *aapl = NULL;
2177 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2178 uint64_t req_bitmap, client_caps;
2179 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2183 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2184 return NT_STATUS_UNSUCCESSFUL);
2186 if (!config->use_aapl
2187 || in_context_blobs == NULL
2188 || out_context_blobs == NULL) {
2189 return NT_STATUS_OK;
2192 aapl = smb2_create_blob_find(in_context_blobs,
2193 SMB2_CREATE_TAG_AAPL);
2195 return NT_STATUS_OK;
2198 if (aapl->data.length != 24) {
2199 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2200 (uintmax_t)aapl->data.length));
2201 return NT_STATUS_INVALID_PARAMETER;
2204 cmd = IVAL(aapl->data.data, 0);
2205 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2206 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2207 return NT_STATUS_INVALID_PARAMETER;
2210 req_bitmap = BVAL(aapl->data.data, 8);
2211 client_caps = BVAL(aapl->data.data, 16);
2213 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2215 SBVAL(p, 8, req_bitmap);
2216 ok = data_blob_append(req, &blob, p, 16);
2218 return NT_STATUS_UNSUCCESSFUL;
2221 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2222 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2223 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2224 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2225 config->readdir_attr_enabled = true;
2228 if (config->use_copyfile) {
2229 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2230 config->copyfile_enabled = true;
2234 * The client doesn't set the flag, so we can't check
2235 * for it and just set it unconditionally
2237 if (config->unix_info_enabled) {
2238 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2241 SBVAL(p, 0, server_caps);
2242 ok = data_blob_append(req, &blob, p, 8);
2244 return NT_STATUS_UNSUCCESSFUL;
2248 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2249 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2257 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2266 ok = data_blob_append(req, &blob, p, 8);
2268 return NT_STATUS_UNSUCCESSFUL;
2272 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2273 ok = convert_string_talloc(req,
2274 CH_UNIX, CH_UTF16LE,
2275 "Samba", strlen("Samba"),
2278 return NT_STATUS_UNSUCCESSFUL;
2282 SIVAL(p + 4, 0, modellen);
2283 ok = data_blob_append(req, &blob, p, 8);
2286 return NT_STATUS_UNSUCCESSFUL;
2289 ok = data_blob_append(req, &blob, model, modellen);
2292 return NT_STATUS_UNSUCCESSFUL;
2296 status = smb2_create_blob_add(out_context_blobs,
2298 SMB2_CREATE_TAG_AAPL,
2300 if (NT_STATUS_IS_OK(status)) {
2301 global_fruit_config.nego_aapl = true;
2302 if (config->aapl_zero_file_id) {
2303 aapl_force_zero_file_id(handle->conn->sconn);
2310 static bool readdir_attr_meta_finderi_stream(
2311 struct vfs_handle_struct *handle,
2312 const struct smb_filename *smb_fname,
2315 struct smb_filename *stream_name = NULL;
2316 files_struct *fsp = NULL;
2321 uint8_t buf[AFP_INFO_SIZE];
2323 stream_name = synthetic_smb_fname(talloc_tos(),
2324 smb_fname->base_name,
2325 AFPINFO_STREAM_NAME,
2326 NULL, smb_fname->flags);
2327 if (stream_name == NULL) {
2331 ret = SMB_VFS_STAT(handle->conn, stream_name);
2336 status = SMB_VFS_CREATE_FILE(
2337 handle->conn, /* conn */
2339 0, /* root_dir_fid */
2340 stream_name, /* fname */
2341 FILE_READ_DATA, /* access_mask */
2342 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2344 FILE_OPEN, /* create_disposition*/
2345 0, /* create_options */
2346 0, /* file_attributes */
2347 INTERNAL_OPEN_ONLY, /* oplock_request */
2349 0, /* allocation_size */
2350 0, /* private_flags */
2355 NULL, NULL); /* create context */
2357 TALLOC_FREE(stream_name);
2359 if (!NT_STATUS_IS_OK(status)) {
2363 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2364 if (nread != AFP_INFO_SIZE) {
2365 DBG_ERR("short read [%s] [%zd/%d]\n",
2366 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2371 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2378 close_file(NULL, fsp, NORMAL_CLOSE);
2384 static bool readdir_attr_meta_finderi_netatalk(
2385 struct vfs_handle_struct *handle,
2386 const struct smb_filename *smb_fname,
2389 struct adouble *ad = NULL;
2392 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2397 p = ad_get_entry(ad, ADEID_FINDERI);
2399 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2404 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2409 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2410 const struct smb_filename *smb_fname,
2411 struct readdir_attr_data *attr_data)
2413 struct fruit_config_data *config = NULL;
2414 uint32_t date_added;
2418 SMB_VFS_HANDLE_GET_DATA(handle, config,
2419 struct fruit_config_data,
2422 switch (config->meta) {
2423 case FRUIT_META_NETATALK:
2424 ok = readdir_attr_meta_finderi_netatalk(
2425 handle, smb_fname, &ai);
2428 case FRUIT_META_STREAM:
2429 ok = readdir_attr_meta_finderi_stream(
2430 handle, smb_fname, &ai);
2434 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2439 /* Don't bother with errors, it's likely ENOENT */
2443 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2445 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2446 &ai.afpi_FinderInfo[0], 4);
2448 /* finder_creator */
2449 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2450 &ai.afpi_FinderInfo[4], 4);
2454 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2455 &ai.afpi_FinderInfo[8], 2);
2457 /* finder_ext_flags */
2458 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2459 &ai.afpi_FinderInfo[24], 2);
2462 date_added = convert_time_t_to_uint32_t(
2463 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2465 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2470 static uint64_t readdir_attr_rfork_size_adouble(
2471 struct vfs_handle_struct *handle,
2472 const struct smb_filename *smb_fname)
2474 struct adouble *ad = NULL;
2475 uint64_t rfork_size;
2477 ad = ad_get(talloc_tos(), handle, smb_fname,
2483 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2489 static uint64_t readdir_attr_rfork_size_stream(
2490 struct vfs_handle_struct *handle,
2491 const struct smb_filename *smb_fname)
2493 struct smb_filename *stream_name = NULL;
2495 uint64_t rfork_size;
2497 stream_name = synthetic_smb_fname(talloc_tos(),
2498 smb_fname->base_name,
2499 AFPRESOURCE_STREAM_NAME,
2501 if (stream_name == NULL) {
2505 ret = SMB_VFS_STAT(handle->conn, stream_name);
2507 TALLOC_FREE(stream_name);
2511 rfork_size = stream_name->st.st_ex_size;
2512 TALLOC_FREE(stream_name);
2517 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2518 const struct smb_filename *smb_fname)
2520 struct fruit_config_data *config = NULL;
2521 uint64_t rfork_size;
2523 SMB_VFS_HANDLE_GET_DATA(handle, config,
2524 struct fruit_config_data,
2527 switch (config->rsrc) {
2528 case FRUIT_RSRC_ADFILE:
2529 case FRUIT_RSRC_XATTR:
2530 rfork_size = readdir_attr_rfork_size_adouble(handle,
2534 case FRUIT_META_STREAM:
2535 rfork_size = readdir_attr_rfork_size_stream(handle,
2540 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2548 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2549 const struct smb_filename *smb_fname,
2550 struct readdir_attr_data *attr_data)
2552 NTSTATUS status = NT_STATUS_OK;
2553 struct fruit_config_data *config = NULL;
2556 SMB_VFS_HANDLE_GET_DATA(handle, config,
2557 struct fruit_config_data,
2558 return NT_STATUS_UNSUCCESSFUL);
2561 /* Ensure we return a default value in the creation_date field */
2562 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2565 * Resource fork length
2568 if (config->readdir_attr_rsize) {
2569 uint64_t rfork_size;
2571 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2572 attr_data->attr_data.aapl.rfork_size = rfork_size;
2579 if (config->readdir_attr_finder_info) {
2580 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2582 status = NT_STATUS_INTERNAL_ERROR;
2589 /* Search MS NFS style ACE with UNIX mode */
2590 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2592 const struct security_descriptor *psd,
2597 struct fruit_config_data *config = NULL;
2601 SMB_VFS_HANDLE_GET_DATA(handle, config,
2602 struct fruit_config_data,
2603 return NT_STATUS_UNSUCCESSFUL);
2605 if (psd->dacl == NULL || !config->unix_info_enabled) {
2606 return NT_STATUS_OK;
2609 for (i = 0; i < psd->dacl->num_aces; i++) {
2610 if (dom_sid_compare_domain(
2611 &global_sid_Unix_NFS_Mode,
2612 &psd->dacl->aces[i].trustee) == 0) {
2613 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2614 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2617 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2618 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2623 return NT_STATUS_OK;
2626 /****************************************************************************
2628 ****************************************************************************/
2630 static int fruit_connect(vfs_handle_struct *handle,
2631 const char *service,
2635 char *list = NULL, *newlist = NULL;
2636 struct fruit_config_data *config;
2638 DEBUG(10, ("fruit_connect\n"));
2640 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2645 rc = init_fruit_config(handle);
2650 SMB_VFS_HANDLE_GET_DATA(handle, config,
2651 struct fruit_config_data, return -1);
2653 if (config->veto_appledouble) {
2654 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2657 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2658 newlist = talloc_asprintf(
2660 "%s/" ADOUBLE_NAME_PREFIX "*/",
2662 lp_do_parameter(SNUM(handle->conn),
2667 lp_do_parameter(SNUM(handle->conn),
2669 "/" ADOUBLE_NAME_PREFIX "*/");
2675 if (config->encoding == FRUIT_ENC_NATIVE) {
2679 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2680 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2681 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2682 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2683 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2684 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2685 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2686 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2687 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2688 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2695 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2696 struct smb_filename *smb_fname,
2702 char afpinfo_buf[AFP_INFO_SIZE];
2703 ssize_t len, written;
2707 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2712 if (!(flags & (O_CREAT | O_TRUNC))) {
2716 ai = afpinfo_new(talloc_tos());
2722 len = afpinfo_pack(ai, afpinfo_buf);
2723 if (len != AFP_INFO_SIZE) {
2728 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2729 fsp->fh->fd = hostfd;
2731 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2734 if (written != AFP_INFO_SIZE) {
2735 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2743 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2746 int saved_errno = errno;
2748 fsp->fh->fd = hostfd;
2749 SMB_VFS_NEXT_CLOSE(handle, fsp);
2752 errno = saved_errno;
2757 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2758 struct smb_filename *smb_fname,
2764 struct smb_filename *smb_fname_base = NULL;
2767 struct adouble *ad = NULL;
2769 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2771 /* Create an smb_filename with stream_name == NULL. */
2772 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2773 smb_fname->base_name,
2778 if (smb_fname_base == NULL) {
2785 * We use baseflags to turn off nasty side-effects when opening the
2789 baseflags &= ~O_TRUNC;
2790 baseflags &= ~O_EXCL;
2791 baseflags &= ~O_CREAT;
2793 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2797 * It is legit to open a stream on a directory, but the base
2798 * fd has to be read-only.
2800 if ((hostfd == -1) && (errno == EISDIR)) {
2801 baseflags &= ~O_ACCMODE;
2802 baseflags |= O_RDONLY;
2803 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2807 TALLOC_FREE(smb_fname_base);
2814 if (flags & (O_CREAT | O_TRUNC)) {
2816 * The attribute does not exist or needs to be truncated,
2817 * create an AppleDouble EA
2819 ad = ad_init(fsp, handle, ADOUBLE_META);
2825 fsp->fh->fd = hostfd;
2827 rc = ad_fset(ad, fsp);
2838 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2840 int saved_errno = errno;
2843 * BUGBUGBUG -- we would need to call
2844 * fd_close_posix here, but we don't have a
2847 fsp->fh->fd = hostfd;
2848 SMB_VFS_NEXT_CLOSE(handle, fsp);
2851 errno = saved_errno;
2856 static int fruit_open_meta(vfs_handle_struct *handle,
2857 struct smb_filename *smb_fname,
2858 files_struct *fsp, int flags, mode_t mode)
2861 struct fruit_config_data *config = NULL;
2862 struct fio *fio = NULL;
2864 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2866 SMB_VFS_HANDLE_GET_DATA(handle, config,
2867 struct fruit_config_data, return -1);
2869 switch (config->meta) {
2870 case FRUIT_META_STREAM:
2871 fd = fruit_open_meta_stream(handle, smb_fname,
2875 case FRUIT_META_NETATALK:
2876 fd = fruit_open_meta_netatalk(handle, smb_fname,
2881 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2885 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2891 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
2892 fio->type = ADOUBLE_META;
2893 fio->config = config;
2898 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2899 struct smb_filename *smb_fname,
2905 struct adouble *ad = NULL;
2906 struct smb_filename *smb_fname_base = NULL;
2907 struct fruit_config_data *config = NULL;
2910 SMB_VFS_HANDLE_GET_DATA(handle, config,
2911 struct fruit_config_data, return -1);
2913 if ((!(flags & O_CREAT)) &&
2914 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
2916 /* sorry, but directories don't habe a resource fork */
2921 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
2926 /* Sanitize flags */
2927 if (flags & O_WRONLY) {
2928 /* We always need read access for the metadata header too */
2933 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2940 if (flags & (O_CREAT | O_TRUNC)) {
2941 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
2947 fsp->fh->fd = hostfd;
2949 rc = ad_fset(ad, fsp);
2960 TALLOC_FREE(smb_fname_base);
2962 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2964 int saved_errno = errno;
2967 * BUGBUGBUG -- we would need to call
2968 * fd_close_posix here, but we don't have a
2971 fsp->fh->fd = hostfd;
2975 errno = saved_errno;
2980 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2981 struct smb_filename *smb_fname,
2986 #ifdef HAVE_ATTROPEN
2989 fd = attropen(smb_fname->base_name,
2990 AFPRESOURCE_EA_NETATALK,
3005 static int fruit_open_rsrc(vfs_handle_struct *handle,
3006 struct smb_filename *smb_fname,
3007 files_struct *fsp, int flags, mode_t mode)
3010 struct fruit_config_data *config = NULL;
3011 struct fio *fio = NULL;
3013 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3015 SMB_VFS_HANDLE_GET_DATA(handle, config,
3016 struct fruit_config_data, return -1);
3018 if (((flags & O_ACCMODE) == O_RDONLY)
3019 && (flags & O_CREAT)
3020 && !VALID_STAT(fsp->fsp_name->st))
3023 * This means the stream doesn't exist. macOS SMB server fails
3024 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3025 * 12565 and the test for this combination in
3026 * test_rfork_create().
3032 switch (config->rsrc) {
3033 case FRUIT_RSRC_STREAM:
3034 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3037 case FRUIT_RSRC_ADFILE:
3038 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3042 case FRUIT_RSRC_XATTR:
3043 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3048 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3052 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3058 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3059 fio->type = ADOUBLE_RSRC;
3060 fio->config = config;
3065 static int fruit_open(vfs_handle_struct *handle,
3066 struct smb_filename *smb_fname,
3067 files_struct *fsp, int flags, mode_t mode)
3071 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3073 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3074 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3077 if (is_afpinfo_stream(smb_fname)) {
3078 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3079 } else if (is_afpresource_stream(smb_fname)) {
3080 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3082 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3085 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3090 static int fruit_rename(struct vfs_handle_struct *handle,
3091 const struct smb_filename *smb_fname_src,
3092 const struct smb_filename *smb_fname_dst)
3095 struct fruit_config_data *config = NULL;
3096 struct smb_filename *src_adp_smb_fname = NULL;
3097 struct smb_filename *dst_adp_smb_fname = NULL;
3099 SMB_VFS_HANDLE_GET_DATA(handle, config,
3100 struct fruit_config_data, return -1);
3102 if (!VALID_STAT(smb_fname_src->st)) {
3103 DBG_ERR("Need valid stat for [%s]\n",
3104 smb_fname_str_dbg(smb_fname_src));
3108 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3113 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3114 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3119 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3124 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3129 DBG_DEBUG("%s -> %s\n",
3130 smb_fname_str_dbg(src_adp_smb_fname),
3131 smb_fname_str_dbg(dst_adp_smb_fname));
3133 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3134 if (errno == ENOENT) {
3139 TALLOC_FREE(src_adp_smb_fname);
3140 TALLOC_FREE(dst_adp_smb_fname);
3144 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3145 const struct smb_filename *smb_fname)
3147 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3150 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3151 const struct smb_filename *smb_fname)
3153 return SMB_VFS_REMOVEXATTR(handle->conn,
3155 AFPINFO_EA_NETATALK);
3158 static int fruit_unlink_meta(vfs_handle_struct *handle,
3159 const struct smb_filename *smb_fname)
3161 struct fruit_config_data *config = NULL;
3164 SMB_VFS_HANDLE_GET_DATA(handle, config,
3165 struct fruit_config_data, return -1);
3167 switch (config->meta) {
3168 case FRUIT_META_STREAM:
3169 rc = fruit_unlink_meta_stream(handle, smb_fname);
3172 case FRUIT_META_NETATALK:
3173 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3177 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3184 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3185 const struct smb_filename *smb_fname,
3190 if (!force_unlink) {
3191 struct smb_filename *smb_fname_cp = NULL;
3194 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3195 if (smb_fname_cp == NULL) {
3200 * 0 byte resource fork streams are not listed by
3201 * vfs_streaminfo, as a result stream cleanup/deletion of file
3202 * deletion doesn't remove the resourcefork stream.
3205 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3207 TALLOC_FREE(smb_fname_cp);
3208 DBG_ERR("stat [%s] failed [%s]\n",
3209 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3213 size = smb_fname_cp->st.st_ex_size;
3214 TALLOC_FREE(smb_fname_cp);
3217 /* OS X ignores resource fork stream delete requests */
3222 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3223 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3230 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3231 const struct smb_filename *smb_fname,
3235 struct adouble *ad = NULL;
3236 struct smb_filename *adp_smb_fname = NULL;
3238 if (!force_unlink) {
3239 ad = ad_get(talloc_tos(), handle, smb_fname,
3248 * 0 byte resource fork streams are not listed by
3249 * vfs_streaminfo, as a result stream cleanup/deletion of file
3250 * deletion doesn't remove the resourcefork stream.
3253 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3254 /* OS X ignores resource fork stream delete requests */
3262 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3267 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3268 TALLOC_FREE(adp_smb_fname);
3269 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3276 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3277 const struct smb_filename *smb_fname,
3281 * OS X ignores resource fork stream delete requests, so nothing to do
3282 * here. Removing the file will remove the xattr anyway, so we don't
3283 * have to take care of removing 0 byte resource forks that could be
3289 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3290 const struct smb_filename *smb_fname,
3293 struct fruit_config_data *config = NULL;
3296 SMB_VFS_HANDLE_GET_DATA(handle, config,
3297 struct fruit_config_data, return -1);
3299 switch (config->rsrc) {
3300 case FRUIT_RSRC_STREAM:
3301 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3304 case FRUIT_RSRC_ADFILE:
3305 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3308 case FRUIT_RSRC_XATTR:
3309 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3313 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3320 static int fruit_unlink(vfs_handle_struct *handle,
3321 const struct smb_filename *smb_fname)
3324 struct fruit_config_data *config = NULL;
3325 struct smb_filename *rsrc_smb_fname = NULL;
3327 SMB_VFS_HANDLE_GET_DATA(handle, config,
3328 struct fruit_config_data, return -1);
3330 if (is_afpinfo_stream(smb_fname)) {
3331 return fruit_unlink_meta(handle, smb_fname);
3332 } else if (is_afpresource_stream(smb_fname)) {
3333 return fruit_unlink_rsrc(handle, smb_fname, false);
3334 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3335 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3339 * A request to delete the base file. Because 0 byte resource
3340 * fork streams are not listed by fruit_streaminfo,
3341 * delete_all_streams() can't remove 0 byte resource fork
3342 * streams, so we have to cleanup this here.
3344 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3345 smb_fname->base_name,
3346 AFPRESOURCE_STREAM_NAME,
3349 if (rsrc_smb_fname == NULL) {
3353 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3354 if ((rc != 0) && (errno != ENOENT)) {
3355 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3356 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3357 TALLOC_FREE(rsrc_smb_fname);
3360 TALLOC_FREE(rsrc_smb_fname);
3362 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3365 static int fruit_chmod(vfs_handle_struct *handle,
3366 const struct smb_filename *smb_fname,
3370 struct fruit_config_data *config = NULL;
3371 struct smb_filename *smb_fname_adp = NULL;
3373 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3378 SMB_VFS_HANDLE_GET_DATA(handle, config,
3379 struct fruit_config_data, return -1);
3381 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3385 if (!VALID_STAT(smb_fname->st)) {
3389 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3393 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3398 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3400 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3401 if (errno == ENOENT) {
3405 TALLOC_FREE(smb_fname_adp);
3409 static int fruit_chown(vfs_handle_struct *handle,
3410 const struct smb_filename *smb_fname,
3415 struct fruit_config_data *config = NULL;
3416 struct smb_filename *adp_smb_fname = NULL;
3418 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3423 SMB_VFS_HANDLE_GET_DATA(handle, config,
3424 struct fruit_config_data, return -1);
3426 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3430 if (!VALID_STAT(smb_fname->st)) {
3434 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3438 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3443 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3445 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3446 if (errno == ENOENT) {
3451 TALLOC_FREE(adp_smb_fname);
3455 static int fruit_rmdir(struct vfs_handle_struct *handle,
3456 const struct smb_filename *smb_fname)
3460 struct fruit_config_data *config;
3462 SMB_VFS_HANDLE_GET_DATA(handle, config,
3463 struct fruit_config_data, return -1);
3465 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3470 * Due to there is no way to change bDeleteVetoFiles variable
3471 * from this module, need to clean up ourselves
3474 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3479 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3481 struct adouble *ad = NULL;
3483 struct smb_filename *ad_smb_fname = NULL;
3486 match = strncmp(de->d_name,
3487 ADOUBLE_NAME_PREFIX,
3488 strlen(ADOUBLE_NAME_PREFIX));
3493 p = talloc_asprintf(talloc_tos(), "%s/%s",
3494 smb_fname->base_name, de->d_name);
3496 DBG_ERR("talloc_asprintf failed\n");
3500 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3504 if (ad_smb_fname == NULL) {
3505 DBG_ERR("synthetic_smb_fname failed\n");
3510 * Check whether it's a valid AppleDouble file, if
3511 * yes, delete it, ignore it otherwise.
3513 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3515 TALLOC_FREE(ad_smb_fname);
3521 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3522 TALLOC_FREE(ad_smb_fname);
3524 DBG_ERR("Deleting [%s] failed\n",
3525 smb_fname_str_dbg(ad_smb_fname));
3533 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3536 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3537 files_struct *fsp, void *data,
3538 size_t n, off_t offset)
3543 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3549 DBG_ERR("Removing [%s] after short read [%zd]\n",
3550 fsp_str_dbg(fsp), nread);
3552 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3554 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3562 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3563 files_struct *fsp, void *data,
3564 size_t n, off_t offset)
3567 struct adouble *ad = NULL;
3568 char afpinfo_buf[AFP_INFO_SIZE];
3572 ai = afpinfo_new(talloc_tos());
3577 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3583 p = ad_get_entry(ad, ADEID_FINDERI);
3585 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3590 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3592 nread = afpinfo_pack(ai, afpinfo_buf);
3593 if (nread != AFP_INFO_SIZE) {
3598 memcpy(data, afpinfo_buf, n);
3606 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3607 files_struct *fsp, void *data,
3608 size_t n, off_t offset)
3610 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3615 * OS X has a off-by-1 error in the offset calculation, so we're
3616 * bug compatible here. It won't hurt, as any relevant real
3617 * world read requests from the AFP_AfpInfo stream will be
3618 * offset=0 n=60. offset is ignored anyway, see below.
3620 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3624 /* Yes, macOS always reads from offset 0 */
3626 to_return = MIN(n, AFP_INFO_SIZE);
3628 switch (fio->config->meta) {
3629 case FRUIT_META_STREAM:
3630 nread = fruit_pread_meta_stream(handle, fsp, data,
3634 case FRUIT_META_NETATALK:
3635 nread = fruit_pread_meta_adouble(handle, fsp, data,
3640 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3647 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3648 files_struct *fsp, void *data,
3649 size_t n, off_t offset)
3651 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3654 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3655 files_struct *fsp, void *data,
3656 size_t n, off_t offset)
3658 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3661 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3662 files_struct *fsp, void *data,
3663 size_t n, off_t offset)
3665 struct adouble *ad = NULL;
3668 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3673 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3674 offset + ad_getentryoff(ad, ADEID_RFORK));
3680 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3681 files_struct *fsp, void *data,
3682 size_t n, off_t offset)
3684 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3687 switch (fio->config->rsrc) {
3688 case FRUIT_RSRC_STREAM:
3689 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3692 case FRUIT_RSRC_ADFILE:
3693 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3696 case FRUIT_RSRC_XATTR:
3697 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3701 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3708 static ssize_t fruit_pread(vfs_handle_struct *handle,
3709 files_struct *fsp, void *data,
3710 size_t n, off_t offset)
3712 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3715 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3716 fsp_str_dbg(fsp), (intmax_t)offset, n);
3719 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3722 if (fio->type == ADOUBLE_META) {
3723 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3725 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3728 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3732 static bool fruit_must_handle_aio_stream(struct fio *fio)
3738 if ((fio->type == ADOUBLE_META) &&
3739 (fio->config->meta == FRUIT_META_NETATALK))
3744 if ((fio->type == ADOUBLE_RSRC) &&
3745 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
3753 struct fruit_pread_state {
3755 struct vfs_aio_state vfs_aio_state;
3758 static void fruit_pread_done(struct tevent_req *subreq);
3760 static struct tevent_req *fruit_pread_send(
3761 struct vfs_handle_struct *handle,
3762 TALLOC_CTX *mem_ctx,
3763 struct tevent_context *ev,
3764 struct files_struct *fsp,
3766 size_t n, off_t offset)
3768 struct tevent_req *req = NULL;
3769 struct tevent_req *subreq = NULL;
3770 struct fruit_pread_state *state = NULL;
3771 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3773 req = tevent_req_create(mem_ctx, &state,
3774 struct fruit_pread_state);
3779 if (fruit_must_handle_aio_stream(fio)) {
3780 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
3781 if (state->nread != n) {
3782 if (state->nread != -1) {
3785 tevent_req_error(req, errno);
3786 return tevent_req_post(req, ev);
3788 tevent_req_done(req);
3789 return tevent_req_post(req, ev);
3792 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
3794 if (tevent_req_nomem(req, subreq)) {
3795 return tevent_req_post(req, ev);
3797 tevent_req_set_callback(subreq, fruit_pread_done, req);
3801 static void fruit_pread_done(struct tevent_req *subreq)
3803 struct tevent_req *req = tevent_req_callback_data(
3804 subreq, struct tevent_req);
3805 struct fruit_pread_state *state = tevent_req_data(
3806 req, struct fruit_pread_state);
3808 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
3809 TALLOC_FREE(subreq);
3811 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3814 tevent_req_done(req);
3817 static ssize_t fruit_pread_recv(struct tevent_req *req,
3818 struct vfs_aio_state *vfs_aio_state)
3820 struct fruit_pread_state *state = tevent_req_data(
3821 req, struct fruit_pread_state);
3823 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3827 *vfs_aio_state = state->vfs_aio_state;
3828 return state->nread;
3831 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
3832 files_struct *fsp, const void *data,
3833 size_t n, off_t offset)
3838 ai = afpinfo_unpack(talloc_tos(), data);
3843 if (ai_empty_finderinfo(ai)) {
3844 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3845 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3846 DBG_ERR("Can't delete metadata for %s: %s\n",
3847 fsp_str_dbg(fsp), strerror(errno));
3855 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3858 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
3859 files_struct *fsp, const void *data,
3860 size_t n, off_t offset)
3862 struct adouble *ad = NULL;
3867 ai = afpinfo_unpack(talloc_tos(), data);
3872 if (ai_empty_finderinfo(ai)) {
3873 ret = SMB_VFS_REMOVEXATTR(handle->conn,
3875 AFPINFO_EA_NETATALK);
3877 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3878 DBG_ERR("Can't delete metadata for %s: %s\n",
3879 fsp_str_dbg(fsp), strerror(errno));
3886 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3888 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
3893 p = ad_get_entry(ad, ADEID_FINDERI);
3895 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3900 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3902 ret = ad_fset(ad, fsp);
3904 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3913 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
3914 files_struct *fsp, const void *data,
3915 size_t n, off_t offset)
3917 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3921 * Writing an all 0 blob to the metadata stream
3922 * results in the stream being removed on a macOS
3923 * server. This ensures we behave the same and it
3924 * verified by the "delete AFP_AfpInfo by writing all
3927 if (n != AFP_INFO_SIZE || offset != 0) {
3928 DBG_ERR("unexpected offset=%jd or size=%jd\n",
3929 (intmax_t)offset, (intmax_t)n);
3933 switch (fio->config->meta) {
3934 case FRUIT_META_STREAM:
3935 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
3939 case FRUIT_META_NETATALK:
3940 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
3945 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3952 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
3953 files_struct *fsp, const void *data,
3954 size_t n, off_t offset)
3956 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3959 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
3960 files_struct *fsp, const void *data,
3961 size_t n, off_t offset)
3963 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3966 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
3967 files_struct *fsp, const void *data,
3968 size_t n, off_t offset)
3970 struct adouble *ad = NULL;
3974 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3976 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
3980 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3981 offset + ad_getentryoff(ad, ADEID_RFORK));
3982 if (nwritten != n) {
3983 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
3984 fsp_str_dbg(fsp), nwritten, n);
3989 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3990 ad_setentrylen(ad, ADEID_RFORK, n + offset);
3991 ret = ad_fset(ad, fsp);
3993 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4003 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4004 files_struct *fsp, const void *data,
4005 size_t n, off_t offset)
4007 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4010 switch (fio->config->rsrc) {
4011 case FRUIT_RSRC_STREAM:
4012 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4015 case FRUIT_RSRC_ADFILE:
4016 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4019 case FRUIT_RSRC_XATTR:
4020 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4024 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4031 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4032 files_struct *fsp, const void *data,
4033 size_t n, off_t offset)
4035 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4038 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4039 fsp_str_dbg(fsp), (intmax_t)offset, n);
4042 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4045 if (fio->type == ADOUBLE_META) {
4046 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4048 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4051 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4055 struct fruit_pwrite_state {
4057 struct vfs_aio_state vfs_aio_state;
4060 static void fruit_pwrite_done(struct tevent_req *subreq);
4062 static struct tevent_req *fruit_pwrite_send(
4063 struct vfs_handle_struct *handle,
4064 TALLOC_CTX *mem_ctx,
4065 struct tevent_context *ev,
4066 struct files_struct *fsp,
4068 size_t n, off_t offset)
4070 struct tevent_req *req = NULL;
4071 struct tevent_req *subreq = NULL;
4072 struct fruit_pwrite_state *state = NULL;
4073 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4075 req = tevent_req_create(mem_ctx, &state,
4076 struct fruit_pwrite_state);
4081 if (fruit_must_handle_aio_stream(fio)) {
4082 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4083 if (state->nwritten != n) {
4084 if (state->nwritten != -1) {
4087 tevent_req_error(req, errno);
4088 return tevent_req_post(req, ev);
4090 tevent_req_done(req);
4091 return tevent_req_post(req, ev);
4094 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4096 if (tevent_req_nomem(req, subreq)) {
4097 return tevent_req_post(req, ev);
4099 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4103 static void fruit_pwrite_done(struct tevent_req *subreq)
4105 struct tevent_req *req = tevent_req_callback_data(
4106 subreq, struct tevent_req);
4107 struct fruit_pwrite_state *state = tevent_req_data(
4108 req, struct fruit_pwrite_state);
4110 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4111 TALLOC_FREE(subreq);
4113 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4116 tevent_req_done(req);
4119 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4120 struct vfs_aio_state *vfs_aio_state)
4122 struct fruit_pwrite_state *state = tevent_req_data(
4123 req, struct fruit_pwrite_state);
4125 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4129 *vfs_aio_state = state->vfs_aio_state;
4130 return state->nwritten;
4134 * Helper to stat/lstat the base file of an smb_fname.
4136 static int fruit_stat_base(vfs_handle_struct *handle,
4137 struct smb_filename *smb_fname,
4140 char *tmp_stream_name;
4143 tmp_stream_name = smb_fname->stream_name;
4144 smb_fname->stream_name = NULL;
4146 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4148 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4150 smb_fname->stream_name = tmp_stream_name;
4154 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4155 struct smb_filename *smb_fname,
4161 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4163 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4169 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4170 struct smb_filename *smb_fname,
4173 struct adouble *ad = NULL;
4175 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4177 DBG_INFO("fruit_stat_meta %s: %s\n",
4178 smb_fname_str_dbg(smb_fname), strerror(errno));
4184 /* Populate the stat struct with info from the base file. */
4185 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4188 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4189 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4190 smb_fname->stream_name);
4194 static int fruit_stat_meta(vfs_handle_struct *handle,
4195 struct smb_filename *smb_fname,
4198 struct fruit_config_data *config = NULL;
4201 SMB_VFS_HANDLE_GET_DATA(handle, config,
4202 struct fruit_config_data, return -1);
4204 switch (config->meta) {
4205 case FRUIT_META_STREAM:
4206 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4209 case FRUIT_META_NETATALK:
4210 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4214 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4221 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4222 struct smb_filename *smb_fname,
4225 struct adouble *ad = NULL;
4228 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4234 /* Populate the stat struct with info from the base file. */
4235 ret = fruit_stat_base(handle, smb_fname, follow_links);
4241 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4242 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4243 smb_fname->stream_name);
4248 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4249 struct smb_filename *smb_fname,
4255 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4257 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4263 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4264 struct smb_filename *smb_fname,
4267 #ifdef HAVE_ATTROPEN
4271 /* Populate the stat struct with info from the base file. */
4272 ret = fruit_stat_base(handle, smb_fname, follow_links);
4277 fd = attropen(smb_fname->base_name,
4278 AFPRESOURCE_EA_NETATALK,
4284 ret = sys_fstat(fd, &smb_fname->st, false);
4287 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4288 AFPRESOURCE_EA_NETATALK);
4294 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4295 smb_fname->stream_name);
4305 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4306 struct smb_filename *smb_fname,
4309 struct fruit_config_data *config = NULL;
4312 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4314 SMB_VFS_HANDLE_GET_DATA(handle, config,
4315 struct fruit_config_data, return -1);
4317 switch (config->rsrc) {
4318 case FRUIT_RSRC_STREAM:
4319 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4322 case FRUIT_RSRC_XATTR:
4323 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4326 case FRUIT_RSRC_ADFILE:
4327 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4331 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4338 static int fruit_stat(vfs_handle_struct *handle,
4339 struct smb_filename *smb_fname)
4343 DEBUG(10, ("fruit_stat called for %s\n",
4344 smb_fname_str_dbg(smb_fname)));
4346 if (!is_ntfs_stream_smb_fname(smb_fname)
4347 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4348 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4350 update_btime(handle, smb_fname);
4356 * Note if lp_posix_paths() is true, we can never
4357 * get here as is_ntfs_stream_smb_fname() is
4358 * always false. So we never need worry about
4359 * not following links here.
4362 if (is_afpinfo_stream(smb_fname)) {
4363 rc = fruit_stat_meta(handle, smb_fname, true);
4364 } else if (is_afpresource_stream(smb_fname)) {
4365 rc = fruit_stat_rsrc(handle, smb_fname, true);
4367 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4371 update_btime(handle, smb_fname);
4372 smb_fname->st.st_ex_mode &= ~S_IFMT;
4373 smb_fname->st.st_ex_mode |= S_IFREG;
4374 smb_fname->st.st_ex_blocks =
4375 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4380 static int fruit_lstat(vfs_handle_struct *handle,
4381 struct smb_filename *smb_fname)
4385 DEBUG(10, ("fruit_lstat called for %s\n",
4386 smb_fname_str_dbg(smb_fname)));
4388 if (!is_ntfs_stream_smb_fname(smb_fname)
4389 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4390 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4392 update_btime(handle, smb_fname);
4397 if (is_afpinfo_stream(smb_fname)) {
4398 rc = fruit_stat_meta(handle, smb_fname, false);
4399 } else if (is_afpresource_stream(smb_fname)) {
4400 rc = fruit_stat_rsrc(handle, smb_fname, false);
4402 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4406 update_btime(handle, smb_fname);
4407 smb_fname->st.st_ex_mode &= ~S_IFMT;
4408 smb_fname->st.st_ex_mode |= S_IFREG;
4409 smb_fname->st.st_ex_blocks =
4410 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4415 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4417 SMB_STRUCT_STAT *sbuf)
4419 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4422 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4424 SMB_STRUCT_STAT *sbuf)
4428 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4433 *sbuf = fsp->base_fsp->fsp_name->st;
4434 sbuf->st_ex_size = AFP_INFO_SIZE;
4435 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4440 static int fruit_fstat_meta(vfs_handle_struct *handle,
4442 SMB_STRUCT_STAT *sbuf,
4447 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4449 switch (fio->config->meta) {
4450 case FRUIT_META_STREAM:
4451 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4454 case FRUIT_META_NETATALK:
4455 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4459 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4463 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4467 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4469 SMB_STRUCT_STAT *sbuf)
4471 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4474 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4476 SMB_STRUCT_STAT *sbuf)
4478 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4481 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4483 SMB_STRUCT_STAT *sbuf)
4485 struct adouble *ad = NULL;
4488 /* Populate the stat struct with info from the base file. */
4489 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4494 ad = ad_get(talloc_tos(), handle,
4495 fsp->base_fsp->fsp_name,
4498 DBG_ERR("ad_get [%s] failed [%s]\n",
4499 fsp_str_dbg(fsp), strerror(errno));
4503 *sbuf = fsp->base_fsp->fsp_name->st;
4504 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4505 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4511 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4512 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4516 switch (fio->config->rsrc) {
4517 case FRUIT_RSRC_STREAM:
4518 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4521 case FRUIT_RSRC_ADFILE:
4522 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4525 case FRUIT_RSRC_XATTR:
4526 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4530 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4537 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4538 SMB_STRUCT_STAT *sbuf)
4540 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4544 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4547 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4549 if (fio->type == ADOUBLE_META) {
4550 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4552 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4556 sbuf->st_ex_mode &= ~S_IFMT;
4557 sbuf->st_ex_mode |= S_IFREG;
4558 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4561 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4562 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4566 static NTSTATUS fruit_streaminfo_meta_stream(
4567 vfs_handle_struct *handle,
4568 struct files_struct *fsp,
4569 const struct smb_filename *smb_fname,
4570 TALLOC_CTX *mem_ctx,
4571 unsigned int *pnum_streams,
4572 struct stream_struct **pstreams)
4574 struct stream_struct *stream = *pstreams;
4575 unsigned int num_streams = *pnum_streams;
4576 struct smb_filename *sname = NULL;
4581 for (i = 0; i < num_streams; i++) {
4582 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4587 if (i == num_streams) {
4588 return NT_STATUS_OK;
4591 if (stream[i].size == AFP_INFO_SIZE) {
4592 return NT_STATUS_OK;
4595 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4596 "from [%s]\n", (intmax_t)stream[i].size,
4597 smb_fname_str_dbg(smb_fname));
4599 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4601 return NT_STATUS_INTERNAL_ERROR;
4604 sname = synthetic_smb_fname(talloc_tos(),
4605 smb_fname->base_name,
4606 AFPINFO_STREAM_NAME,
4608 if (sname == NULL) {
4609 return NT_STATUS_NO_MEMORY;
4612 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4615 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4616 return map_nt_error_from_unix(errno);
4619 return NT_STATUS_OK;
4622 static NTSTATUS fruit_streaminfo_meta_netatalk(
4623 vfs_handle_struct *handle,
4624 struct files_struct *fsp,
4625 const struct smb_filename *smb_fname,
4626 TALLOC_CTX *mem_ctx,
4627 unsigned int *pnum_streams,
4628 struct stream_struct **pstreams)
4630 struct stream_struct *stream = *pstreams;
4631 unsigned int num_streams = *pnum_streams;
4632 struct adouble *ad = NULL;
4637 /* Remove the Netatalk xattr from the list */
4638 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4639 ":" NETATALK_META_XATTR ":$DATA");
4641 return NT_STATUS_NO_MEMORY;
4645 * Check if there's a AFPINFO_STREAM from the VFS streams
4646 * backend and if yes, remove it from the list
4648 for (i = 0; i < num_streams; i++) {
4649 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4654 if (i < num_streams) {
4655 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4656 smb_fname_str_dbg(smb_fname));
4658 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4661 return NT_STATUS_INTERNAL_ERROR;
4665 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4667 return NT_STATUS_OK;
4670 is_fi_empty = ad_empty_finderinfo(ad);
4674 return NT_STATUS_OK;
4677 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4678 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4679 smb_roundup(handle->conn, AFP_INFO_SIZE));
4681 return NT_STATUS_NO_MEMORY;
4684 return NT_STATUS_OK;
4687 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4688 struct files_struct *fsp,
4689 const struct smb_filename *smb_fname,
4690 TALLOC_CTX *mem_ctx,
4691 unsigned int *pnum_streams,
4692 struct stream_struct **pstreams)
4694 struct fruit_config_data *config = NULL;
4697 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4698 return NT_STATUS_INTERNAL_ERROR);
4700 switch (config->meta) {
4701 case FRUIT_META_NETATALK:
4702 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4703 mem_ctx, pnum_streams,
4707 case FRUIT_META_STREAM:
4708 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4709 mem_ctx, pnum_streams,
4714 return NT_STATUS_INTERNAL_ERROR;
4720 static NTSTATUS fruit_streaminfo_rsrc_stream(
4721 vfs_handle_struct *handle,
4722 struct files_struct *fsp,
4723 const struct smb_filename *smb_fname,
4724 TALLOC_CTX *mem_ctx,
4725 unsigned int *pnum_streams,
4726 struct stream_struct **pstreams)
4730 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4732 DBG_ERR("Filtering resource stream failed\n");
4733 return NT_STATUS_INTERNAL_ERROR;
4735 return NT_STATUS_OK;
4738 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4739 vfs_handle_struct *handle,
4740 struct files_struct *fsp,
4741 const struct smb_filename *smb_fname,
4742 TALLOC_CTX *mem_ctx,
4743 unsigned int *pnum_streams,
4744 struct stream_struct **pstreams)
4748 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4750 DBG_ERR("Filtering resource stream failed\n");
4751 return NT_STATUS_INTERNAL_ERROR;
4753 return NT_STATUS_OK;
4756 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4757 vfs_handle_struct *handle,
4758 struct files_struct *fsp,
4759 const struct smb_filename *smb_fname,
4760 TALLOC_CTX *mem_ctx,
4761 unsigned int *pnum_streams,
4762 struct stream_struct **pstreams)
4764 struct stream_struct *stream = *pstreams;
4765 unsigned int num_streams = *pnum_streams;
4766 struct adouble *ad = NULL;
4772 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4773 * and if yes, remove it from the list
4775 for (i = 0; i < num_streams; i++) {
4776 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4781 if (i < num_streams) {
4782 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4783 smb_fname_str_dbg(smb_fname));
4785 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4786 AFPRESOURCE_STREAM);
4788 return NT_STATUS_INTERNAL_ERROR;
4792 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4794 return NT_STATUS_OK;
4797 rlen = ad_getentrylen(ad, ADEID_RFORK);
4801 return NT_STATUS_OK;
4804 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4805 AFPRESOURCE_STREAM_NAME, rlen,
4806 smb_roundup(handle->conn, rlen));
4808 return NT_STATUS_NO_MEMORY;
4811 return NT_STATUS_OK;
4814 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4815 struct files_struct *fsp,
4816 const struct smb_filename *smb_fname,
4817 TALLOC_CTX *mem_ctx,
4818 unsigned int *pnum_streams,
4819 struct stream_struct **pstreams)
4821 struct fruit_config_data *config = NULL;
4824 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4825 return NT_STATUS_INTERNAL_ERROR);
4827 switch (config->rsrc) {
4828 case FRUIT_RSRC_STREAM:
4829 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4830 mem_ctx, pnum_streams,
4834 case FRUIT_RSRC_XATTR:
4835 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4836 mem_ctx, pnum_streams,
4840 case FRUIT_RSRC_ADFILE:
4841 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4842 mem_ctx, pnum_streams,
4847 return NT_STATUS_INTERNAL_ERROR;
4853 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4854 struct files_struct *fsp,
4855 const struct smb_filename *smb_fname,
4856 TALLOC_CTX *mem_ctx,
4857 unsigned int *pnum_streams,
4858 struct stream_struct **pstreams)
4860 struct fruit_config_data *config = NULL;
4863 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4864 return NT_STATUS_UNSUCCESSFUL);
4866 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4868 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4869 pnum_streams, pstreams);
4870 if (!NT_STATUS_IS_OK(status)) {
4874 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4875 mem_ctx, pnum_streams, pstreams);
4876 if (!NT_STATUS_IS_OK(status)) {
4880 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4881 mem_ctx, pnum_streams, pstreams);
4882 if (!NT_STATUS_IS_OK(status)) {
4886 return NT_STATUS_OK;
4889 static int fruit_ntimes(vfs_handle_struct *handle,
4890 const struct smb_filename *smb_fname,
4891 struct smb_file_time *ft)
4894 struct adouble *ad = NULL;
4895 struct fruit_config_data *config = NULL;
4897 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4900 if ((config->meta != FRUIT_META_NETATALK) ||
4901 null_timespec(ft->create_time))
4903 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4906 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4907 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4909 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4914 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4915 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4917 rc = ad_set(ad, smb_fname);
4923 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4926 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4929 static int fruit_fallocate(struct vfs_handle_struct *handle,
4930 struct files_struct *fsp,
4935 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4938 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4941 /* Let the pwrite code path handle it. */
4946 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4947 struct files_struct *fsp,
4951 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4954 #ifdef HAVE_ATTROPEN
4955 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4960 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4961 struct files_struct *fsp,
4965 struct adouble *ad = NULL;
4968 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4970 DBG_DEBUG("ad_get [%s] failed [%s]\n",
4971 fsp_str_dbg(fsp), strerror(errno));
4975 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4977 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset + ad_off);
4983 ad_setentrylen(ad, ADEID_RFORK, offset);
4985 rc = ad_fset(ad, fsp);
4987 DBG_ERR("ad_fset [%s] failed [%s]\n",
4988 fsp_str_dbg(fsp), strerror(errno));
4997 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4998 struct files_struct *fsp,
5002 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5005 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5008 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5009 struct files_struct *fsp,
5012 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5015 switch (fio->config->rsrc) {
5016 case FRUIT_RSRC_XATTR:
5017 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5020 case FRUIT_RSRC_ADFILE:
5021 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5024 case FRUIT_RSRC_STREAM:
5025 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5029 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5037 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5038 struct files_struct *fsp,
5042 DBG_WARNING("ftruncate %s to %jd",
5043 fsp_str_dbg(fsp), (intmax_t)offset);
5044 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5049 /* OS X returns success but does nothing */
5050 DBG_INFO("ignoring ftruncate %s to %jd\n",
5051 fsp_str_dbg(fsp), (intmax_t)offset);
5055 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5056 struct files_struct *fsp,
5059 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5062 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5066 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5069 if (fio->type == ADOUBLE_META) {
5070 ret = fruit_ftruncate_meta(handle, fsp, offset);
5072 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5075 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5079 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5080 struct smb_request *req,
5081 uint16_t root_dir_fid,
5082 struct smb_filename *smb_fname,
5083 uint32_t access_mask,
5084 uint32_t share_access,
5085 uint32_t create_disposition,
5086 uint32_t create_options,
5087 uint32_t file_attributes,
5088 uint32_t oplock_request,
5089 struct smb2_lease *lease,
5090 uint64_t allocation_size,
5091 uint32_t private_flags,
5092 struct security_descriptor *sd,
5093 struct ea_list *ea_list,
5094 files_struct **result,
5096 const struct smb2_create_blobs *in_context_blobs,
5097 struct smb2_create_blobs *out_context_blobs)
5100 struct fruit_config_data *config = NULL;
5101 files_struct *fsp = NULL;
5103 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5104 if (!NT_STATUS_IS_OK(status)) {
5108 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5109 return NT_STATUS_UNSUCCESSFUL);
5111 status = SMB_VFS_NEXT_CREATE_FILE(
5112 handle, req, root_dir_fid, smb_fname,
5113 access_mask, share_access,
5114 create_disposition, create_options,
5115 file_attributes, oplock_request,
5117 allocation_size, private_flags,
5118 sd, ea_list, result,
5119 pinfo, in_context_blobs, out_context_blobs);
5120 if (!NT_STATUS_IS_OK(status)) {
5126 if (global_fruit_config.nego_aapl) {
5127 if (config->copyfile_enabled) {
5129 * Set a flag in the fsp. Gets used in
5130 * copychunk to check whether the special
5131 * Apple copyfile semantics for copychunk
5132 * should be allowed in a copychunk request
5133 * with a count of 0.
5135 fsp->aapl_copyfile_supported = true;
5138 if (config->posix_rename && fsp->is_directory) {
5140 * Enable POSIX directory rename behaviour
5142 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5147 * If this is a plain open for existing files, opening an 0
5148 * byte size resource fork MUST fail with
5149 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5151 * Cf the vfs_fruit torture tests in test_rfork_create().
5153 if (is_afpresource_stream(fsp->fsp_name) &&
5154 create_disposition == FILE_OPEN)
5156 if (fsp->fsp_name->st.st_ex_size == 0) {
5157 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5162 if (is_ntfs_stream_smb_fname(smb_fname)
5163 || fsp->is_directory) {
5167 if (config->locking == FRUIT_LOCKING_NETATALK) {
5168 status = fruit_check_access(
5171 map_share_mode_to_deny_mode(share_access, 0));
5172 if (!NT_STATUS_IS_OK(status)) {
5180 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5183 close_file(req, fsp, ERROR_CLOSE);
5184 *result = fsp = NULL;
5190 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5191 const struct smb_filename *fname,
5192 TALLOC_CTX *mem_ctx,
5193 struct readdir_attr_data **pattr_data)
5195 struct fruit_config_data *config = NULL;
5196 struct readdir_attr_data *attr_data;
5199 SMB_VFS_HANDLE_GET_DATA(handle, config,
5200 struct fruit_config_data,
5201 return NT_STATUS_UNSUCCESSFUL);
5203 if (!global_fruit_config.nego_aapl) {
5204 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5207 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5209 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5210 if (*pattr_data == NULL) {
5211 return NT_STATUS_UNSUCCESSFUL;
5213 attr_data = *pattr_data;
5214 attr_data->type = RDATTR_AAPL;
5217 * Mac metadata: compressed FinderInfo, resource fork length
5220 status = readdir_attr_macmeta(handle, fname, attr_data);
5221 if (!NT_STATUS_IS_OK(status)) {
5223 * Error handling is tricky: if we return failure from
5224 * this function, the corresponding directory entry
5225 * will to be passed to the client, so we really just
5226 * want to error out on fatal errors.
5228 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5236 if (config->unix_info_enabled) {
5237 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5243 if (!config->readdir_attr_max_access) {
5244 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5246 status = smbd_calculate_access_mask(
5250 SEC_FLAG_MAXIMUM_ALLOWED,
5251 &attr_data->attr_data.aapl.max_access);
5252 if (!NT_STATUS_IS_OK(status)) {
5257 return NT_STATUS_OK;
5260 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5261 fname->base_name, nt_errstr(status)));
5262 TALLOC_FREE(*pattr_data);
5266 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5268 uint32_t security_info,
5269 TALLOC_CTX *mem_ctx,
5270 struct security_descriptor **ppdesc)
5273 struct security_ace ace;
5275 struct fruit_config_data *config;
5277 SMB_VFS_HANDLE_GET_DATA(handle, config,
5278 struct fruit_config_data,
5279 return NT_STATUS_UNSUCCESSFUL);
5281 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5283 if (!NT_STATUS_IS_OK(status)) {
5288 * Add MS NFS style ACEs with uid, gid and mode
5290 if (!config->unix_info_enabled) {
5291 return NT_STATUS_OK;
5294 /* MS NFS style mode */
5295 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5296 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5297 status = security_descriptor_dacl_add(*ppdesc, &ace);
5298 if (!NT_STATUS_IS_OK(status)) {
5299 DEBUG(1,("failed to add MS NFS style ACE\n"));
5303 /* MS NFS style uid */
5304 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5305 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5306 status = security_descriptor_dacl_add(*ppdesc, &ace);
5307 if (!NT_STATUS_IS_OK(status)) {
5308 DEBUG(1,("failed to add MS NFS style ACE\n"));
5312 /* MS NFS style gid */
5313 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5314 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5315 status = security_descriptor_dacl_add(*ppdesc, &ace);
5316 if (!NT_STATUS_IS_OK(status)) {
5317 DEBUG(1,("failed to add MS NFS style ACE\n"));
5321 return NT_STATUS_OK;
5324 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5326 uint32_t security_info_sent,
5327 const struct security_descriptor *psd)
5331 mode_t ms_nfs_mode = 0;
5334 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5336 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5337 if (!NT_STATUS_IS_OK(status)) {
5338 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5342 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5343 if (!NT_STATUS_IS_OK(status)) {
5344 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5349 if (fsp->fh->fd != -1) {
5350 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5352 result = SMB_VFS_CHMOD(fsp->conn,
5358 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5359 result, (unsigned)ms_nfs_mode,
5361 status = map_nt_error_from_unix(errno);
5366 return NT_STATUS_OK;
5369 static struct vfs_offload_ctx *fruit_offload_ctx;
5371 struct fruit_offload_read_state {
5372 struct vfs_handle_struct *handle;
5373 struct tevent_context *ev;
5379 static void fruit_offload_read_done(struct tevent_req *subreq);
5381 static struct tevent_req *fruit_offload_read_send(
5382 TALLOC_CTX *mem_ctx,
5383 struct tevent_context *ev,
5384 struct vfs_handle_struct *handle,
5391 struct tevent_req *req = NULL;
5392 struct tevent_req *subreq = NULL;
5393 struct fruit_offload_read_state *state = NULL;
5395 req = tevent_req_create(mem_ctx, &state,
5396 struct fruit_offload_read_state);
5400 *state = (struct fruit_offload_read_state) {
5407 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5408 fsctl, ttl, offset, to_copy);
5409 if (tevent_req_nomem(subreq, req)) {
5410 return tevent_req_post(req, ev);
5412 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5416 static void fruit_offload_read_done(struct tevent_req *subreq)
5418 struct tevent_req *req = tevent_req_callback_data(
5419 subreq, struct tevent_req);
5420 struct fruit_offload_read_state *state = tevent_req_data(
5421 req, struct fruit_offload_read_state);
5424 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5428 TALLOC_FREE(subreq);
5429 if (tevent_req_nterror(req, status)) {
5433 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5434 tevent_req_done(req);
5438 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5439 &fruit_offload_ctx);
5440 if (tevent_req_nterror(req, status)) {
5444 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5447 if (tevent_req_nterror(req, status)) {
5451 tevent_req_done(req);
5455 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5456 struct vfs_handle_struct *handle,
5457 TALLOC_CTX *mem_ctx,
5460 struct fruit_offload_read_state *state = tevent_req_data(
5461 req, struct fruit_offload_read_state);
5464 if (tevent_req_is_nterror(req, &status)) {
5465 tevent_req_received(req);
5469 token->length = state->token.length;
5470 token->data = talloc_move(mem_ctx, &state->token.data);
5472 tevent_req_received(req);
5473 return NT_STATUS_OK;
5476 struct fruit_offload_write_state {
5477 struct vfs_handle_struct *handle;
5479 struct files_struct *src_fsp;
5480 struct files_struct *dst_fsp;
5484 static void fruit_offload_write_done(struct tevent_req *subreq);
5485 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
5486 TALLOC_CTX *mem_ctx,
5487 struct tevent_context *ev,
5488 struct files_struct *src_fsp,
5490 struct files_struct *dest_fsp,
5495 struct tevent_req *req, *subreq;
5496 struct fruit_offload_write_state *state;
5498 struct fruit_config_data *config;
5499 off_t to_copy = num;
5501 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5502 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5504 SMB_VFS_HANDLE_GET_DATA(handle, config,
5505 struct fruit_config_data,
5508 req = tevent_req_create(mem_ctx, &state,
5509 struct fruit_offload_write_state);
5513 state->handle = handle;
5514 state->src_fsp = src_fsp;
5515 state->dst_fsp = dest_fsp;
5518 * Check if this a OS X copyfile style copychunk request with
5519 * a requested chunk count of 0 that was translated to a
5520 * offload_write_send VFS call overloading the parameters src_off
5521 * = dest_off = num = 0.
5523 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
5524 src_fsp->aapl_copyfile_supported &&
5525 dest_fsp->aapl_copyfile_supported)
5527 status = vfs_stat_fsp(src_fsp);
5528 if (tevent_req_nterror(req, status)) {
5529 return tevent_req_post(req, ev);
5532 to_copy = src_fsp->fsp_name->st.st_ex_size;
5533 state->is_copyfile = true;
5536 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
5545 if (tevent_req_nomem(subreq, req)) {
5546 return tevent_req_post(req, ev);
5549 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
5553 static void fruit_offload_write_done(struct tevent_req *subreq)
5555 struct tevent_req *req = tevent_req_callback_data(
5556 subreq, struct tevent_req);
5557 struct fruit_offload_write_state *state = tevent_req_data(
5558 req, struct fruit_offload_write_state);
5560 unsigned int num_streams = 0;
5561 struct stream_struct *streams = NULL;
5563 struct smb_filename *src_fname_tmp = NULL;
5564 struct smb_filename *dst_fname_tmp = NULL;
5566 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
5569 TALLOC_FREE(subreq);
5570 if (tevent_req_nterror(req, status)) {
5574 if (!state->is_copyfile) {
5575 tevent_req_done(req);
5580 * Now copy all remaining streams. We know the share supports
5581 * streams, because we're in vfs_fruit. We don't do this async
5582 * because streams are few and small.
5584 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5585 state->src_fsp->fsp_name,
5586 req, &num_streams, &streams);
5587 if (tevent_req_nterror(req, status)) {
5591 if (num_streams == 1) {
5592 /* There is always one stream, ::$DATA. */
5593 tevent_req_done(req);
5597 for (i = 0; i < num_streams; i++) {
5598 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5599 __func__, streams[i].name, (size_t)streams[i].size));
5601 src_fname_tmp = synthetic_smb_fname(
5603 state->src_fsp->fsp_name->base_name,
5606 state->src_fsp->fsp_name->flags);
5607 if (tevent_req_nomem(src_fname_tmp, req)) {
5611 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5612 TALLOC_FREE(src_fname_tmp);
5616 dst_fname_tmp = synthetic_smb_fname(
5618 state->dst_fsp->fsp_name->base_name,
5621 state->dst_fsp->fsp_name->flags);
5622 if (tevent_req_nomem(dst_fname_tmp, req)) {
5623 TALLOC_FREE(src_fname_tmp);
5627 status = copy_file(req,
5628 state->handle->conn,
5631 OPENX_FILE_CREATE_IF_NOT_EXIST,
5633 if (!NT_STATUS_IS_OK(status)) {
5634 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5635 smb_fname_str_dbg(src_fname_tmp),
5636 smb_fname_str_dbg(dst_fname_tmp),
5637 nt_errstr(status)));
5638 TALLOC_FREE(src_fname_tmp);
5639 TALLOC_FREE(dst_fname_tmp);
5640 tevent_req_nterror(req, status);
5644 TALLOC_FREE(src_fname_tmp);
5645 TALLOC_FREE(dst_fname_tmp);
5648 TALLOC_FREE(streams);
5649 TALLOC_FREE(src_fname_tmp);
5650 TALLOC_FREE(dst_fname_tmp);
5651 tevent_req_done(req);
5654 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
5655 struct tevent_req *req,
5658 struct fruit_offload_write_state *state = tevent_req_data(
5659 req, struct fruit_offload_write_state);
5662 if (tevent_req_is_nterror(req, &status)) {
5663 DEBUG(1, ("server side copy chunk failed: %s\n",
5664 nt_errstr(status)));
5666 tevent_req_received(req);
5670 *copied = state->copied;
5671 tevent_req_received(req);
5673 return NT_STATUS_OK;
5676 static struct vfs_fn_pointers vfs_fruit_fns = {
5677 .connect_fn = fruit_connect,
5679 /* File operations */
5680 .chmod_fn = fruit_chmod,
5681 .chown_fn = fruit_chown,
5682 .unlink_fn = fruit_unlink,
5683 .rename_fn = fruit_rename,
5684 .rmdir_fn = fruit_rmdir,
5685 .open_fn = fruit_open,
5686 .pread_fn = fruit_pread,
5687 .pwrite_fn = fruit_pwrite,
5688 .pread_send_fn = fruit_pread_send,
5689 .pread_recv_fn = fruit_pread_recv,
5690 .pwrite_send_fn = fruit_pwrite_send,
5691 .pwrite_recv_fn = fruit_pwrite_recv,
5692 .stat_fn = fruit_stat,
5693 .lstat_fn = fruit_lstat,
5694 .fstat_fn = fruit_fstat,
5695 .streaminfo_fn = fruit_streaminfo,
5696 .ntimes_fn = fruit_ntimes,
5697 .ftruncate_fn = fruit_ftruncate,
5698 .fallocate_fn = fruit_fallocate,
5699 .create_file_fn = fruit_create_file,
5700 .readdir_attr_fn = fruit_readdir_attr,
5701 .offload_read_send_fn = fruit_offload_read_send,
5702 .offload_read_recv_fn = fruit_offload_read_recv,
5703 .offload_write_send_fn = fruit_offload_write_send,
5704 .offload_write_recv_fn = fruit_offload_write_recv,
5706 /* NT ACL operations */
5707 .fget_nt_acl_fn = fruit_fget_nt_acl,
5708 .fset_nt_acl_fn = fruit_fset_nt_acl,
5711 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
5712 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5714 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5716 if (!NT_STATUS_IS_OK(ret)) {
5720 vfs_fruit_debug_level = debug_add_class("fruit");
5721 if (vfs_fruit_debug_level == -1) {
5722 vfs_fruit_debug_level = DBGC_VFS;
5723 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5726 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5727 "vfs_fruit_init","fruit",vfs_fruit_debug_level));