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;
144 * Additional options, all enabled by default,
145 * possibly useful for analyzing performance. The associated
146 * operations with each of them may be expensive, so having
147 * the chance to disable them individually gives a chance
148 * tweaking the setup for the particular usecase.
150 bool readdir_attr_rsize;
151 bool readdir_attr_finder_info;
152 bool readdir_attr_max_access;
155 static const struct enum_list fruit_rsrc[] = {
156 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
157 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
158 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
162 static const struct enum_list fruit_meta[] = {
163 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
164 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
168 static const struct enum_list fruit_locking[] = {
169 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
170 {FRUIT_LOCKING_NONE, "none"},
174 static const struct enum_list fruit_encoding[] = {
175 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
176 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
180 /*****************************************************************************
181 * Defines, functions and data structures that deal with AppleDouble
182 *****************************************************************************/
185 * There are two AppleDouble blobs we deal with:
187 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
188 * metadata in an xattr
190 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
193 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
196 #define AD_VERSION2 0x00020000
197 #define AD_VERSION AD_VERSION2
200 * AppleDouble entry IDs.
202 #define ADEID_DFORK 1
203 #define ADEID_RFORK 2
205 #define ADEID_COMMENT 4
206 #define ADEID_ICONBW 5
207 #define ADEID_ICONCOL 6
208 #define ADEID_FILEI 7
209 #define ADEID_FILEDATESI 8
210 #define ADEID_FINDERI 9
211 #define ADEID_MACFILEI 10
212 #define ADEID_PRODOSFILEI 11
213 #define ADEID_MSDOSFILEI 12
214 #define ADEID_SHORTNAME 13
215 #define ADEID_AFPFILEI 14
218 /* Private Netatalk entries */
219 #define ADEID_PRIVDEV 16
220 #define ADEID_PRIVINO 17
221 #define ADEID_PRIVSYN 18
222 #define ADEID_PRIVID 19
223 #define ADEID_MAX (ADEID_PRIVID + 1)
226 * These are the real ids for the private entries,
227 * as stored in the adouble file
229 #define AD_DEV 0x80444556
230 #define AD_INO 0x80494E4F
231 #define AD_SYN 0x8053594E
232 #define AD_ID 0x8053567E
234 /* Number of actually used entries */
235 #define ADEID_NUM_XATTR 8
236 #define ADEID_NUM_DOT_UND 2
237 #define ADEID_NUM_RSRC_XATTR 1
239 /* AppleDouble magic */
240 #define AD_APPLESINGLE_MAGIC 0x00051600
241 #define AD_APPLEDOUBLE_MAGIC 0x00051607
242 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
244 /* Sizes of relevant entry bits */
245 #define ADEDLEN_MAGIC 4
246 #define ADEDLEN_VERSION 4
247 #define ADEDLEN_FILLER 16
248 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
249 #define ADEDLEN_NENTRIES 2
250 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
251 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
252 #define AD_ENTRY_LEN_EID 4
253 #define AD_ENTRY_LEN_OFF 4
254 #define AD_ENTRY_LEN_LEN 4
255 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
258 #define ADEDLEN_NAME 255
259 #define ADEDLEN_COMMENT 200
260 #define ADEDLEN_FILEI 16
261 #define ADEDLEN_FINDERI 32
262 #define ADEDLEN_FILEDATESI 16
263 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
264 #define ADEDLEN_AFPFILEI 4
265 #define ADEDLEN_MACFILEI 4
266 #define ADEDLEN_PRODOSFILEI 8
267 #define ADEDLEN_MSDOSFILEI 2
268 #define ADEDLEN_DID 4
269 #define ADEDLEN_PRIVDEV 8
270 #define ADEDLEN_PRIVINO 8
271 #define ADEDLEN_PRIVSYN 8
272 #define ADEDLEN_PRIVID 4
275 #define ADEDOFF_MAGIC 0
276 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
277 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
278 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
280 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
281 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
282 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
283 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
284 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
286 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
287 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
288 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
289 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
291 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
292 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
293 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
295 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
296 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
297 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
298 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
299 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
300 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
302 #if AD_DATASZ_XATTR != 402
303 #error bad size for AD_DATASZ_XATTR
306 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
307 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
309 #if AD_DATASZ_DOT_UND != 82
310 #error bad size for AD_DATASZ_DOT_UND
314 * Sharemode locks fcntl() offsets
316 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
317 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
319 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
321 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
323 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
324 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
325 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
326 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
327 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
328 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
329 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
330 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
331 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
332 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
334 /* Time stuff we overload the bits a little */
335 #define AD_DATE_CREATE 0
336 #define AD_DATE_MODIFY 4
337 #define AD_DATE_BACKUP 8
338 #define AD_DATE_ACCESS 12
339 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
340 AD_DATE_BACKUP | AD_DATE_ACCESS)
341 #define AD_DATE_UNIX (1 << 10)
342 #define AD_DATE_START 0x80000000
343 #define AD_DATE_DELTA 946684800
344 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
345 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
347 /* Accessor macros */
348 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
349 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
350 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
351 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
359 vfs_handle_struct *ad_handle;
362 adouble_type_t ad_type;
365 struct ad_entry ad_eid[ADEID_MAX];
369 struct ad_entry_order {
370 uint32_t id, offset, len;
373 /* Netatalk AppleDouble metadata xattr */
375 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
376 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
377 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
378 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
379 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
380 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
381 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
382 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
383 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
387 /* AppleDouble resource fork file (the ones prefixed by "._") */
389 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
390 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
391 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
396 * Fake AppleDouble entry oder for resource fork xattr. The xattr
397 * isn't an AppleDouble file, it simply contains the resource data,
398 * but in order to be able to use some API calls like ad_getentryoff()
399 * we build a fake/helper struct adouble with this entry order struct.
402 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
407 /* Conversion from enumerated id to on-disk AppleDouble id */
408 #define AD_EID_DISK(a) (set_eid[a])
409 static const uint32_t set_eid[] = {
410 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
411 AD_DEV, AD_INO, AD_SYN, AD_ID
415 /* tcon config handle */
416 struct fruit_config_data *config;
418 /* Denote stream type, meta or rsrc */
423 * Forward declarations
425 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
426 adouble_type_t type);
427 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
428 static int ad_fset(struct adouble *ad, files_struct *fsp);
429 static int adouble_path(TALLOC_CTX *ctx,
430 const struct smb_filename *smb_fname__in,
431 struct smb_filename **ppsmb_fname_out);
434 * Return a pointer to an AppleDouble entry
436 * Returns NULL if the entry is not present
438 static char *ad_get_entry(const struct adouble *ad, int eid)
440 off_t off = ad_getentryoff(ad, eid);
441 size_t len = ad_getentrylen(ad, eid);
443 if (off == 0 || len == 0) {
447 return ad->ad_data + off;
453 static int ad_getdate(const struct adouble *ad,
454 unsigned int dateoff,
457 bool xlate = (dateoff & AD_DATE_UNIX);
460 dateoff &= AD_DATE_MASK;
461 p = ad_get_entry(ad, ADEID_FILEDATESI);
466 if (dateoff > AD_DATE_ACCESS) {
470 memcpy(date, p + dateoff, sizeof(uint32_t));
473 *date = AD_DATE_TO_UNIX(*date);
481 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
483 bool xlate = (dateoff & AD_DATE_UNIX);
486 p = ad_get_entry(ad, ADEID_FILEDATESI);
491 dateoff &= AD_DATE_MASK;
493 date = AD_DATE_FROM_UNIX(date);
496 if (dateoff > AD_DATE_ACCESS) {
500 memcpy(p + dateoff, &date, sizeof(date));
507 * Map on-disk AppleDouble id to enumerated id
509 static uint32_t get_eid(uint32_t eid)
517 return ADEID_PRIVDEV;
519 return ADEID_PRIVINO;
521 return ADEID_PRIVSYN;
532 * Pack AppleDouble structure into data buffer
534 static bool ad_pack(struct adouble *ad)
541 bufsize = talloc_get_size(ad->ad_data);
543 if (offset + ADEDLEN_MAGIC < offset ||
544 offset + ADEDLEN_MAGIC >= bufsize) {
547 RSIVAL(ad->ad_data, offset, ad->ad_magic);
548 offset += ADEDLEN_MAGIC;
550 if (offset + ADEDLEN_VERSION < offset ||
551 offset + ADEDLEN_VERSION >= bufsize) {
554 RSIVAL(ad->ad_data, offset, ad->ad_version);
555 offset += ADEDLEN_VERSION;
557 if (offset + ADEDLEN_FILLER < offset ||
558 offset + ADEDLEN_FILLER >= bufsize) {
561 if (ad->ad_type == ADOUBLE_RSRC) {
562 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
564 offset += ADEDLEN_FILLER;
566 if (offset + ADEDLEN_NENTRIES < offset ||
567 offset + ADEDLEN_NENTRIES >= bufsize) {
570 offset += ADEDLEN_NENTRIES;
572 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
573 if (ad->ad_eid[eid].ade_off == 0) {
575 * ade_off is also used as indicator whether a
576 * specific entry is used or not
581 if (offset + AD_ENTRY_LEN_EID < offset ||
582 offset + AD_ENTRY_LEN_EID >= bufsize) {
585 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
586 offset += AD_ENTRY_LEN_EID;
588 if (offset + AD_ENTRY_LEN_OFF < offset ||
589 offset + AD_ENTRY_LEN_OFF >= bufsize) {
592 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
593 offset += AD_ENTRY_LEN_OFF;
595 if (offset + AD_ENTRY_LEN_LEN < offset ||
596 offset + AD_ENTRY_LEN_LEN >= bufsize) {
599 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
600 offset += AD_ENTRY_LEN_LEN;
605 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
608 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
614 * Unpack an AppleDouble blob into a struct adoble
616 static bool ad_unpack(struct adouble *ad, const size_t nentries,
619 size_t bufsize = talloc_get_size(ad->ad_data);
621 uint32_t eid, len, off;
624 * The size of the buffer ad->ad_data is checked when read, so
625 * we wouldn't have to check our own offsets, a few extra
626 * checks won't hurt though. We have to check the offsets we
627 * read from the buffer anyway.
630 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
631 DEBUG(1, ("bad size\n"));
635 ad->ad_magic = RIVAL(ad->ad_data, 0);
636 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
637 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
638 DEBUG(1, ("wrong magic or version\n"));
642 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
643 if (adentries != nentries) {
644 DEBUG(1, ("invalid number of entries: %zu\n",
649 /* now, read in the entry bits */
650 for (i = 0; i < adentries; i++) {
651 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
653 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
654 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
656 if (!eid || eid > ADEID_MAX) {
657 DEBUG(1, ("bogus eid %d\n", eid));
662 * All entries other than the resource fork are
663 * expected to be read into the ad_data buffer, so
664 * ensure the specified offset is within that bound
666 if ((off > bufsize) && (eid != ADEID_RFORK)) {
667 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
673 * All entries besides FinderInfo and resource fork
674 * must fit into the buffer. FinderInfo is special as
675 * it may be larger then the default 32 bytes (if it
676 * contains marshalled xattrs), but we will fixup that
677 * in ad_convert(). And the resource fork is never
678 * accessed directly by the ad_data buf (also see
679 * comment above) anyway.
681 if ((eid != ADEID_RFORK) &&
682 (eid != ADEID_FINDERI) &&
683 ((off + len) > bufsize)) {
684 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
690 * That would be obviously broken
692 if (off > filesize) {
693 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
699 * Check for any entry that has its end beyond the
702 if (off + len < off) {
703 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
704 ", len: %" PRIu32 "\n",
709 if (off + len > filesize) {
711 * If this is the resource fork entry, we fix
712 * up the length, for any other entry we bail
715 if (eid != ADEID_RFORK) {
716 DEBUG(1, ("bogus eid %d: off: %" PRIu32
717 ", len: %" PRIu32 "\n",
723 * Fixup the resource fork entry by limiting
724 * the size to entryoffset - filesize.
726 len = filesize - off;
727 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
728 ", len: %" PRIu32 "\n", off, len));
731 ad->ad_eid[eid].ade_off = off;
732 ad->ad_eid[eid].ade_len = len;
739 * Convert from Apple's ._ file to Netatalk
741 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
742 * bytes containing packed xattrs. Netatalk can't deal with that, so
743 * we simply discard the packed xattrs.
745 * @return -1 in case an error occurred, 0 if no conversion was done, 1
748 static int ad_convert(struct adouble *ad, int fd)
751 char *map = MAP_FAILED;
754 origlen = ad_getentryoff(ad, ADEID_RFORK) +
755 ad_getentrylen(ad, ADEID_RFORK);
757 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
758 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
759 if (map == MAP_FAILED) {
760 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
765 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
766 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
767 map + ad_getentryoff(ad, ADEID_RFORK),
768 ad_getentrylen(ad, ADEID_RFORK));
771 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
772 ad_setentryoff(ad, ADEID_RFORK,
773 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
776 * FIXME: direct ftruncate(), but we don't have a fsp for the
779 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
780 + ad_getentrylen(ad, ADEID_RFORK));
783 if (map != MAP_FAILED) {
784 munmap(map, origlen);
790 * Read and parse Netatalk AppleDouble metadata xattr
792 static ssize_t ad_read_meta(struct adouble *ad,
793 const struct smb_filename *smb_fname)
799 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
801 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
802 AFPINFO_EA_NETATALK, ad->ad_data,
808 if (errno == ENOATTR) {
814 DEBUG(2, ("error reading meta xattr: %s\n",
820 if (ealen != AD_DATASZ_XATTR) {
821 DEBUG(2, ("bad size %zd\n", ealen));
827 /* Now parse entries */
828 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
830 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
836 if (!ad_getentryoff(ad, ADEID_FINDERI)
837 || !ad_getentryoff(ad, ADEID_COMMENT)
838 || !ad_getentryoff(ad, ADEID_FILEDATESI)
839 || !ad_getentryoff(ad, ADEID_AFPFILEI)
840 || !ad_getentryoff(ad, ADEID_PRIVDEV)
841 || !ad_getentryoff(ad, ADEID_PRIVINO)
842 || !ad_getentryoff(ad, ADEID_PRIVSYN)
843 || !ad_getentryoff(ad, ADEID_PRIVID)) {
844 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
851 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
852 smb_fname->base_name, rc));
856 if (errno == EINVAL) {
858 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
866 static int ad_open_meta(const struct smb_filename *smb_fname,
870 return open(smb_fname->base_name, flags, mode);
873 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
878 /* FIXME: direct Solaris xattr syscall */
879 return attropen(smb_fname->base_name,
880 AFPRESOURCE_EA_NETATALK, flags, mode);
887 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
893 struct smb_filename *adp_smb_fname = NULL;
895 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
900 fd = open(adp_smb_fname->base_name, flags, mode);
901 TALLOC_FREE(adp_smb_fname);
906 static int ad_open_rsrc(vfs_handle_struct *handle,
907 const struct smb_filename *smb_fname,
911 struct fruit_config_data *config = NULL;
914 SMB_VFS_HANDLE_GET_DATA(handle, config,
915 struct fruit_config_data, return -1);
917 if (config->rsrc == FRUIT_RSRC_XATTR) {
918 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
920 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
926 static int ad_open(vfs_handle_struct *handle,
928 const struct smb_filename *smb_fname,
935 DBG_DEBUG("Path [%s] type [%s]\n",
936 smb_fname->base_name, t == ADOUBLE_META ? "meta" : "rsrc");
938 if (t == ADOUBLE_META) {
939 fd = ad_open_meta(smb_fname, flags, mode);
941 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
945 ad->ad_opened = true;
949 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
950 smb_fname->base_name,
951 t == ADOUBLE_META ? "meta" : "rsrc", fd);
956 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
961 /* FIXME: direct sys_fstat(), don't have an fsp */
962 ret = sys_fstat(ad->ad_fd, &st,
963 lp_fake_directory_create_times(
964 SNUM(ad->ad_handle->conn)));
969 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
970 return st.st_ex_size;
973 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
974 const struct smb_filename *smb_fname)
976 struct adouble *meta_ad = NULL;
977 SMB_STRUCT_STAT sbuf;
979 char *p_meta_ad = NULL;
984 len = sys_pread(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
985 if (len != AD_DATASZ_DOT_UND) {
986 DBG_NOTICE("%s %s: bad size: %zd\n",
987 smb_fname->base_name, strerror(errno), len);
991 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
992 SNUM(ad->ad_handle->conn)));
997 /* Now parse entries */
998 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1000 DBG_ERR("invalid AppleDouble resource %s\n",
1001 smb_fname->base_name);
1006 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1007 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1008 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1009 DBG_ERR("invalid AppleDouble resource %s\n",
1010 smb_fname->base_name);
1015 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1020 * Try to fixup AppleDouble files created by OS X with xattrs
1021 * appended to the ADEID_FINDERI entry. We simply remove the
1022 * xattrs blob, this means any fancy xattr that was stored
1026 ret = ad_convert(ad, ad->ad_fd);
1028 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1034 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1038 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1039 if (len != AD_DATASZ_DOT_UND) {
1040 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1044 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1045 if (meta_ad == NULL) {
1049 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1051 TALLOC_FREE(meta_ad);
1054 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1055 if (p_meta_ad == NULL) {
1056 TALLOC_FREE(meta_ad);
1060 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1062 ret = ad_set(meta_ad, smb_fname);
1063 TALLOC_FREE(meta_ad);
1072 * Read and parse resource fork, either ._ AppleDouble file or xattr
1074 static ssize_t ad_read_rsrc(struct adouble *ad,
1075 const struct smb_filename *smb_fname)
1077 struct fruit_config_data *config = NULL;
1080 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1081 struct fruit_config_data, return -1);
1083 if (config->rsrc == FRUIT_RSRC_XATTR) {
1084 len = ad_read_rsrc_xattr(ad);
1086 len = ad_read_rsrc_adouble(ad, smb_fname);
1093 * Read and unpack an AppleDouble metadata xattr or resource
1095 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1097 switch (ad->ad_type) {
1099 return ad_read_meta(ad, smb_fname);
1101 return ad_read_rsrc(ad, smb_fname);
1107 static int adouble_destructor(struct adouble *ad)
1109 if ((ad->ad_fd != -1) && ad->ad_opened) {
1117 * Allocate a struct adouble without initialiing it
1119 * The struct is either hang of the fsp extension context or if fsp is
1122 * @param[in] ctx talloc context
1123 * @param[in] handle vfs handle
1124 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1126 * @return adouble handle
1128 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1129 adouble_type_t type)
1134 struct fruit_config_data *config;
1136 SMB_VFS_HANDLE_GET_DATA(handle, config,
1137 struct fruit_config_data, return NULL);
1141 adsize = AD_DATASZ_XATTR;
1144 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1145 adsize = AD_DATASZ_DOT_UND;
1152 ad = talloc_zero(ctx, struct adouble);
1159 ad->ad_data = talloc_zero_array(ad, char, adsize);
1160 if (ad->ad_data == NULL) {
1166 ad->ad_handle = handle;
1168 ad->ad_magic = AD_MAGIC;
1169 ad->ad_version = AD_VERSION;
1172 talloc_set_destructor(ad, adouble_destructor);
1182 * Allocate and initialize a new struct adouble
1184 * @param[in] ctx talloc context
1185 * @param[in] handle vfs handle
1186 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1188 * @return adouble handle, initialized
1190 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1191 adouble_type_t type)
1194 const struct ad_entry_order *eid;
1195 struct adouble *ad = NULL;
1196 struct fruit_config_data *config;
1197 time_t t = time(NULL);
1199 SMB_VFS_HANDLE_GET_DATA(handle, config,
1200 struct fruit_config_data, return NULL);
1204 eid = entry_order_meta_xattr;
1207 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1208 eid = entry_order_dot_und;
1210 eid = entry_order_rsrc_xattr;
1217 ad = ad_alloc(ctx, handle, type);
1223 ad->ad_eid[eid->id].ade_off = eid->offset;
1224 ad->ad_eid[eid->id].ade_len = eid->len;
1228 /* put something sane in the date fields */
1229 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1230 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1231 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1232 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1241 * Return AppleDouble data for a file
1243 * @param[in] ctx talloc context
1244 * @param[in] handle vfs handle
1245 * @param[in] smb_fname pathname to file or directory
1246 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1248 * @return talloced struct adouble or NULL on error
1250 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1251 const struct smb_filename *smb_fname,
1252 adouble_type_t type)
1256 struct adouble *ad = NULL;
1260 DEBUG(10, ("ad_get(%s) called for %s\n",
1261 type == ADOUBLE_META ? "meta" : "rsrc",
1262 smb_fname->base_name));
1264 ad = ad_alloc(ctx, handle, type);
1271 * Here's the deal: for ADOUBLE_META we can do without an fd
1272 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1273 * however we need a full-fledged fd for file IO on the ._
1276 if (type == ADOUBLE_RSRC) {
1277 /* Try rw first so we can use the fd in ad_convert() */
1280 fd = ad_open(handle, ad, smb_fname, ADOUBLE_RSRC, mode, 0);
1281 if (fd == -1 && ((errno == EROFS) || (errno == EACCES))) {
1283 fd = ad_open(handle, ad, smb_fname,
1284 ADOUBLE_RSRC, mode, 0);
1288 DBG_DEBUG("ad_open [%s] error [%s]\n",
1289 smb_fname->base_name, strerror(errno));
1295 len = ad_read(ad, smb_fname);
1297 DEBUG(10, ("error reading AppleDouble for %s\n",
1298 smb_fname->base_name));
1304 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1305 type == ADOUBLE_META ? "meta" : "rsrc",
1306 smb_fname->base_name, rc));
1315 * Return AppleDouble data for a file
1317 * @param[in] ctx talloc context
1318 * @param[in] handle vfs handle
1319 * @param[in] fsp fsp to use for IO
1320 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1322 * @return talloced struct adouble or NULL on error
1324 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1325 files_struct *fsp, adouble_type_t type)
1329 struct adouble *ad = NULL;
1331 DBG_DEBUG("ad_get(%s) path [%s]\n",
1332 type == ADOUBLE_META ? "meta" : "rsrc",
1335 ad = ad_alloc(ctx, handle, type);
1341 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1342 ad->ad_fd = fsp->fh->fd;
1345 * Here's the deal: for ADOUBLE_META we can do without an fd
1346 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1347 * however we need a full-fledged fd for file IO on the ._
1353 if (type == ADOUBLE_RSRC) {
1354 /* Try rw first so we can use the fd in ad_convert() */
1357 fd = ad_open(handle, ad, fsp->base_fsp->fsp_name,
1358 ADOUBLE_RSRC, mode, 0);
1360 ((errno == EROFS) || (errno == EACCES)))
1363 fd = ad_open(handle, ad,
1364 fsp->base_fsp->fsp_name, ADOUBLE_RSRC,
1369 DBG_DEBUG("error opening AppleDouble for %s\n",
1377 len = ad_read(ad, fsp->base_fsp->fsp_name);
1379 DBG_DEBUG("error reading AppleDouble for %s\n",
1386 DBG_DEBUG("ad_get(%s) path [%s] rc [%d]\n",
1387 type == ADOUBLE_META ? "meta" : "rsrc",
1388 fsp_str_dbg(fsp), rc);
1397 * Set AppleDouble metadata on a file or directory
1399 * @param[in] ad adouble handle
1401 * @param[in] smb_fname pathname to file or directory
1403 * @return status code, 0 means success
1405 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1410 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1412 if (ad->ad_type != ADOUBLE_META) {
1413 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1414 smb_fname->base_name);
1423 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1425 AFPINFO_EA_NETATALK,
1427 AD_DATASZ_XATTR, 0);
1429 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1435 * Set AppleDouble metadata on a file or directory
1437 * @param[in] ad adouble handle
1438 * @param[in] fsp file handle
1440 * @return status code, 0 means success
1442 static int ad_fset(struct adouble *ad, files_struct *fsp)
1448 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1451 || (fsp->fh == NULL)
1452 || (fsp->fh->fd == -1))
1454 smb_panic("bad fsp");
1462 switch (ad->ad_type) {
1464 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1466 AFPINFO_EA_NETATALK,
1468 AD_DATASZ_XATTR, 0);
1472 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1475 talloc_get_size(ad->ad_data),
1477 if (len != (ssize_t)talloc_get_size(ad->ad_data)) {
1478 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1488 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1493 /*****************************************************************************
1495 *****************************************************************************/
1497 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1499 if (strncasecmp_m(smb_fname->stream_name,
1500 AFPINFO_STREAM_NAME,
1501 strlen(AFPINFO_STREAM_NAME)) == 0) {
1507 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1509 if (strncasecmp_m(smb_fname->stream_name,
1510 AFPRESOURCE_STREAM_NAME,
1511 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1518 * Test whether stream is an Apple stream, not used atm
1521 static bool is_apple_stream(const struct smb_filename *smb_fname)
1523 if (is_afpinfo_stream(smb_fname)) {
1526 if (is_afpresource_stream(smb_fname)) {
1534 * Initialize config struct from our smb.conf config parameters
1536 static int init_fruit_config(vfs_handle_struct *handle)
1538 struct fruit_config_data *config;
1541 config = talloc_zero(handle->conn, struct fruit_config_data);
1543 DEBUG(1, ("talloc_zero() failed\n"));
1549 * Versions up to Samba 4.5.x had a spelling bug in the
1550 * fruit:resource option calling lp_parm_enum with
1551 * "res*s*ource" (ie two s).
1553 * In Samba 4.6 we accept both the wrong and the correct
1554 * spelling, in Samba 4.7 the bad spelling will be removed.
1556 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1557 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1558 if (enumval == -1) {
1559 DEBUG(1, ("value for %s: resource type unknown\n",
1560 FRUIT_PARAM_TYPE_NAME));
1563 config->rsrc = (enum fruit_rsrc)enumval;
1565 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1566 "resource", fruit_rsrc, enumval);
1567 if (enumval == -1) {
1568 DEBUG(1, ("value for %s: resource type unknown\n",
1569 FRUIT_PARAM_TYPE_NAME));
1572 config->rsrc = (enum fruit_rsrc)enumval;
1574 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1575 "metadata", fruit_meta, FRUIT_META_NETATALK);
1576 if (enumval == -1) {
1577 DEBUG(1, ("value for %s: metadata type unknown\n",
1578 FRUIT_PARAM_TYPE_NAME));
1581 config->meta = (enum fruit_meta)enumval;
1583 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1584 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1585 if (enumval == -1) {
1586 DEBUG(1, ("value for %s: locking type unknown\n",
1587 FRUIT_PARAM_TYPE_NAME));
1590 config->locking = (enum fruit_locking)enumval;
1592 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1593 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1594 if (enumval == -1) {
1595 DEBUG(1, ("value for %s: encoding type unknown\n",
1596 FRUIT_PARAM_TYPE_NAME));
1599 config->encoding = (enum fruit_encoding)enumval;
1601 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1602 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1603 FRUIT_PARAM_TYPE_NAME,
1608 config->use_aapl = lp_parm_bool(
1609 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1611 config->unix_info_enabled = lp_parm_bool(
1612 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1614 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1617 config->posix_rename = lp_parm_bool(
1618 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1620 config->aapl_zero_file_id =
1621 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1623 config->readdir_attr_rsize = lp_parm_bool(
1624 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1626 config->readdir_attr_finder_info = lp_parm_bool(
1627 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1629 config->readdir_attr_max_access = lp_parm_bool(
1630 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1632 config->model = lp_parm_const_string(
1633 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1635 SMB_VFS_HANDLE_SET_DATA(handle, config,
1636 NULL, struct fruit_config_data,
1643 * Prepend "._" to a basename
1644 * Return a new struct smb_filename with stream_name == NULL.
1646 static int adouble_path(TALLOC_CTX *ctx,
1647 const struct smb_filename *smb_fname_in,
1648 struct smb_filename **pp_smb_fname_out)
1652 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1655 if (smb_fname == NULL) {
1659 /* We need streamname to be NULL */
1660 TALLOC_FREE(smb_fname->stream_name);
1662 /* And we're replacing base_name. */
1663 TALLOC_FREE(smb_fname->base_name);
1665 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
1667 TALLOC_FREE(smb_fname);
1671 smb_fname->base_name = talloc_asprintf(smb_fname,
1672 "%s/._%s", parent, base);
1673 if (smb_fname->base_name == NULL) {
1674 TALLOC_FREE(smb_fname);
1678 *pp_smb_fname_out = smb_fname;
1684 * Allocate and initialize an AfpInfo struct
1686 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1688 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1692 ai->afpi_Signature = AFP_Signature;
1693 ai->afpi_Version = AFP_Version;
1694 ai->afpi_BackupTime = AD_DATE_START;
1699 * Pack an AfpInfo struct into a buffer
1701 * Buffer size must be at least AFP_INFO_SIZE
1702 * Returns size of packed buffer
1704 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1706 memset(buf, 0, AFP_INFO_SIZE);
1708 RSIVAL(buf, 0, ai->afpi_Signature);
1709 RSIVAL(buf, 4, ai->afpi_Version);
1710 RSIVAL(buf, 12, ai->afpi_BackupTime);
1711 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1713 return AFP_INFO_SIZE;
1717 * Unpack a buffer into a AfpInfo structure
1719 * Buffer size must be at least AFP_INFO_SIZE
1720 * Returns allocated AfpInfo struct
1722 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1724 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1729 ai->afpi_Signature = RIVAL(data, 0);
1730 ai->afpi_Version = RIVAL(data, 4);
1731 ai->afpi_BackupTime = RIVAL(data, 12);
1732 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1733 sizeof(ai->afpi_FinderInfo));
1735 if (ai->afpi_Signature != AFP_Signature
1736 || ai->afpi_Version != AFP_Version) {
1737 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1745 * Fake an inode number from the md5 hash of the (xattr) name
1747 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1750 unsigned char hash[16];
1754 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1755 SMB_ASSERT(upper_sname != NULL);
1758 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1759 sizeof(sbuf->st_ex_dev));
1760 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1761 sizeof(sbuf->st_ex_ino));
1762 MD5Update(&ctx, (unsigned char *)upper_sname,
1763 talloc_get_size(upper_sname)-1);
1764 MD5Final(hash, &ctx);
1766 TALLOC_FREE(upper_sname);
1768 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1769 memcpy(&result, hash, sizeof(result));
1771 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1772 sname, (unsigned long long)result));
1777 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1778 struct stream_struct **streams,
1779 const char *name, off_t size,
1782 struct stream_struct *tmp;
1784 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1790 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1791 if (tmp[*num_streams].name == NULL) {
1795 tmp[*num_streams].size = size;
1796 tmp[*num_streams].alloc_size = alloc_size;
1803 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1804 struct stream_struct **streams)
1806 struct stream_struct *tmp = *streams;
1809 if (*num_streams == 0) {
1813 for (i = 0; i < *num_streams; i++) {
1814 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1819 if (i == *num_streams) {
1823 if (tmp[i].size > 0) {
1827 TALLOC_FREE(tmp[i].name);
1828 if (*num_streams - 1 > i) {
1829 memmove(&tmp[i], &tmp[i+1],
1830 (*num_streams - i - 1) * sizeof(struct stream_struct));
1837 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1838 struct stream_struct **streams,
1841 struct stream_struct *tmp = *streams;
1844 if (*num_streams == 0) {
1848 for (i = 0; i < *num_streams; i++) {
1849 if (strequal_m(tmp[i].name, name)) {
1854 if (i == *num_streams) {
1858 TALLOC_FREE(tmp[i].name);
1859 if (*num_streams - 1 > i) {
1860 memmove(&tmp[i], &tmp[i+1],
1861 (*num_streams - i - 1) * sizeof(struct stream_struct));
1868 static bool ad_empty_finderinfo(const struct adouble *ad)
1871 char emptybuf[ADEDLEN_FINDERI] = {0};
1874 fi = ad_get_entry(ad, ADEID_FINDERI);
1876 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1880 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1884 static bool ai_empty_finderinfo(const AfpInfo *ai)
1887 char emptybuf[ADEDLEN_FINDERI] = {0};
1889 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1894 * Update btime with btime from Netatalk
1896 static void update_btime(vfs_handle_struct *handle,
1897 struct smb_filename *smb_fname)
1900 struct timespec creation_time = {0};
1902 struct fruit_config_data *config = NULL;
1904 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1907 switch (config->meta) {
1908 case FRUIT_META_STREAM:
1910 case FRUIT_META_NETATALK:
1914 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1918 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1922 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1928 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1929 update_stat_ex_create_time(&smb_fname->st, creation_time);
1935 * Map an access mask to a Netatalk single byte byte range lock
1937 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1938 uint32_t access_mask)
1942 switch (access_mask) {
1943 case FILE_READ_DATA:
1944 offset = AD_FILELOCK_OPEN_RD;
1947 case FILE_WRITE_DATA:
1948 case FILE_APPEND_DATA:
1949 offset = AD_FILELOCK_OPEN_WR;
1953 offset = AD_FILELOCK_OPEN_NONE;
1957 if (fork_type == APPLE_FORK_RSRC) {
1958 if (offset == AD_FILELOCK_OPEN_NONE) {
1959 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1969 * Map a deny mode to a Netatalk brl
1971 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1976 switch (deny_mode) {
1978 offset = AD_FILELOCK_DENY_RD;
1982 offset = AD_FILELOCK_DENY_WR;
1986 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1989 if (fork_type == APPLE_FORK_RSRC) {
1997 * Call fcntl() with an exclusive F_GETLK request in order to
1998 * determine if there's an exisiting shared lock
2000 * @return true if the requested lock was found or any error occurred
2001 * false if the lock was not found
2003 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2006 off_t offset = in_offset;
2011 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2012 if (result == false) {
2016 if (type != F_UNLCK) {
2023 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2025 uint32_t access_mask,
2028 NTSTATUS status = NT_STATUS_OK;
2029 struct byte_range_lock *br_lck = NULL;
2030 bool open_for_reading, open_for_writing, deny_read, deny_write;
2032 bool have_read = false;
2035 /* FIXME: hardcoded data fork, add resource fork */
2036 enum apple_fork fork_type = APPLE_FORK_DATA;
2038 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2040 access_mask & FILE_READ_DATA ? "READ" :"-",
2041 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2042 deny_mode & DENY_READ ? "DENY_READ" : "-",
2043 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2045 if (fsp->fh->fd == -1) {
2046 return NT_STATUS_OK;
2049 flags = fcntl(fsp->fh->fd, F_GETFL);
2051 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2052 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2053 return map_nt_error_from_unix(errno);
2056 if (flags & (O_RDONLY|O_RDWR)) {
2058 * Applying fcntl read locks requires an fd opened for
2059 * reading. This means we won't be applying locks for
2060 * files openend write-only, but what can we do...
2066 * Check read access and deny read mode
2068 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2070 open_for_reading = test_netatalk_lock(
2071 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2073 deny_read = test_netatalk_lock(
2074 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2076 DEBUG(10, ("read: %s, deny_write: %s\n",
2077 open_for_reading == true ? "yes" : "no",
2078 deny_read == true ? "yes" : "no"));
2080 if (((access_mask & FILE_READ_DATA) && deny_read)
2081 || ((deny_mode & DENY_READ) && open_for_reading)) {
2082 return NT_STATUS_SHARING_VIOLATION;
2086 if ((access_mask & FILE_READ_DATA) && have_read) {
2087 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2089 handle->conn->sconn->msg_ctx, fsp,
2090 fsp->op->global->open_persistent_id, 1, off,
2091 READ_LOCK, POSIX_LOCK, false,
2094 if (!NT_STATUS_IS_OK(status)) {
2097 TALLOC_FREE(br_lck);
2100 if ((deny_mode & DENY_READ) && have_read) {
2101 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2103 handle->conn->sconn->msg_ctx, fsp,
2104 fsp->op->global->open_persistent_id, 1, off,
2105 READ_LOCK, POSIX_LOCK, false,
2108 if (!NT_STATUS_IS_OK(status)) {
2111 TALLOC_FREE(br_lck);
2116 * Check write access and deny write mode
2118 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2120 open_for_writing = test_netatalk_lock(
2121 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2123 deny_write = test_netatalk_lock(
2124 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2126 DEBUG(10, ("write: %s, deny_write: %s\n",
2127 open_for_writing == true ? "yes" : "no",
2128 deny_write == true ? "yes" : "no"));
2130 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2131 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2132 return NT_STATUS_SHARING_VIOLATION;
2136 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2137 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2139 handle->conn->sconn->msg_ctx, fsp,
2140 fsp->op->global->open_persistent_id, 1, off,
2141 READ_LOCK, POSIX_LOCK, false,
2144 if (!NT_STATUS_IS_OK(status)) {
2147 TALLOC_FREE(br_lck);
2150 if ((deny_mode & DENY_WRITE) && have_read) {
2151 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2153 handle->conn->sconn->msg_ctx, fsp,
2154 fsp->op->global->open_persistent_id, 1, off,
2155 READ_LOCK, POSIX_LOCK, false,
2158 if (!NT_STATUS_IS_OK(status)) {
2161 TALLOC_FREE(br_lck);
2165 TALLOC_FREE(br_lck);
2170 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2171 struct smb_request *req,
2172 const struct smb2_create_blobs *in_context_blobs,
2173 struct smb2_create_blobs *out_context_blobs)
2175 struct fruit_config_data *config;
2177 struct smb2_create_blob *aapl = NULL;
2181 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2182 uint64_t req_bitmap, client_caps;
2183 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2187 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2188 return NT_STATUS_UNSUCCESSFUL);
2190 if (!config->use_aapl
2191 || in_context_blobs == NULL
2192 || out_context_blobs == NULL) {
2193 return NT_STATUS_OK;
2196 aapl = smb2_create_blob_find(in_context_blobs,
2197 SMB2_CREATE_TAG_AAPL);
2199 return NT_STATUS_OK;
2202 if (aapl->data.length != 24) {
2203 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2204 (uintmax_t)aapl->data.length));
2205 return NT_STATUS_INVALID_PARAMETER;
2208 cmd = IVAL(aapl->data.data, 0);
2209 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2210 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2211 return NT_STATUS_INVALID_PARAMETER;
2214 req_bitmap = BVAL(aapl->data.data, 8);
2215 client_caps = BVAL(aapl->data.data, 16);
2217 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2219 SBVAL(p, 8, req_bitmap);
2220 ok = data_blob_append(req, &blob, p, 16);
2222 return NT_STATUS_UNSUCCESSFUL;
2225 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2226 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2227 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2228 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2229 config->readdir_attr_enabled = true;
2232 if (config->use_copyfile) {
2233 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2234 config->copyfile_enabled = true;
2238 * The client doesn't set the flag, so we can't check
2239 * for it and just set it unconditionally
2241 if (config->unix_info_enabled) {
2242 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2245 SBVAL(p, 0, server_caps);
2246 ok = data_blob_append(req, &blob, p, 8);
2248 return NT_STATUS_UNSUCCESSFUL;
2252 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2253 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2261 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2270 ok = data_blob_append(req, &blob, p, 8);
2272 return NT_STATUS_UNSUCCESSFUL;
2276 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2277 ok = convert_string_talloc(req,
2278 CH_UNIX, CH_UTF16LE,
2279 config->model, strlen(config->model),
2282 return NT_STATUS_UNSUCCESSFUL;
2286 SIVAL(p + 4, 0, modellen);
2287 ok = data_blob_append(req, &blob, p, 8);
2290 return NT_STATUS_UNSUCCESSFUL;
2293 ok = data_blob_append(req, &blob, model, modellen);
2296 return NT_STATUS_UNSUCCESSFUL;
2300 status = smb2_create_blob_add(out_context_blobs,
2302 SMB2_CREATE_TAG_AAPL,
2304 if (NT_STATUS_IS_OK(status)) {
2305 global_fruit_config.nego_aapl = true;
2306 if (config->aapl_zero_file_id) {
2307 aapl_force_zero_file_id(handle->conn->sconn);
2314 static bool readdir_attr_meta_finderi_stream(
2315 struct vfs_handle_struct *handle,
2316 const struct smb_filename *smb_fname,
2319 struct smb_filename *stream_name = NULL;
2320 files_struct *fsp = NULL;
2325 uint8_t buf[AFP_INFO_SIZE];
2327 stream_name = synthetic_smb_fname(talloc_tos(),
2328 smb_fname->base_name,
2329 AFPINFO_STREAM_NAME,
2330 NULL, smb_fname->flags);
2331 if (stream_name == NULL) {
2335 ret = SMB_VFS_STAT(handle->conn, stream_name);
2340 status = SMB_VFS_CREATE_FILE(
2341 handle->conn, /* conn */
2343 0, /* root_dir_fid */
2344 stream_name, /* fname */
2345 FILE_READ_DATA, /* access_mask */
2346 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2348 FILE_OPEN, /* create_disposition*/
2349 0, /* create_options */
2350 0, /* file_attributes */
2351 INTERNAL_OPEN_ONLY, /* oplock_request */
2353 0, /* allocation_size */
2354 0, /* private_flags */
2359 NULL, NULL); /* create context */
2361 TALLOC_FREE(stream_name);
2363 if (!NT_STATUS_IS_OK(status)) {
2367 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2368 if (nread != AFP_INFO_SIZE) {
2369 DBG_ERR("short read [%s] [%zd/%d]\n",
2370 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2375 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2382 close_file(NULL, fsp, NORMAL_CLOSE);
2388 static bool readdir_attr_meta_finderi_netatalk(
2389 struct vfs_handle_struct *handle,
2390 const struct smb_filename *smb_fname,
2393 struct adouble *ad = NULL;
2396 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2401 p = ad_get_entry(ad, ADEID_FINDERI);
2403 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2408 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2413 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2414 const struct smb_filename *smb_fname,
2415 struct readdir_attr_data *attr_data)
2417 struct fruit_config_data *config = NULL;
2418 uint32_t date_added;
2422 SMB_VFS_HANDLE_GET_DATA(handle, config,
2423 struct fruit_config_data,
2426 switch (config->meta) {
2427 case FRUIT_META_NETATALK:
2428 ok = readdir_attr_meta_finderi_netatalk(
2429 handle, smb_fname, &ai);
2432 case FRUIT_META_STREAM:
2433 ok = readdir_attr_meta_finderi_stream(
2434 handle, smb_fname, &ai);
2438 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2443 /* Don't bother with errors, it's likely ENOENT */
2447 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2449 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2450 &ai.afpi_FinderInfo[0], 4);
2452 /* finder_creator */
2453 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2454 &ai.afpi_FinderInfo[4], 4);
2458 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2459 &ai.afpi_FinderInfo[8], 2);
2461 /* finder_ext_flags */
2462 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2463 &ai.afpi_FinderInfo[24], 2);
2466 date_added = convert_time_t_to_uint32_t(
2467 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2469 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2474 static uint64_t readdir_attr_rfork_size_adouble(
2475 struct vfs_handle_struct *handle,
2476 const struct smb_filename *smb_fname)
2478 struct adouble *ad = NULL;
2479 uint64_t rfork_size;
2481 ad = ad_get(talloc_tos(), handle, smb_fname,
2487 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2493 static uint64_t readdir_attr_rfork_size_stream(
2494 struct vfs_handle_struct *handle,
2495 const struct smb_filename *smb_fname)
2497 struct smb_filename *stream_name = NULL;
2499 uint64_t rfork_size;
2501 stream_name = synthetic_smb_fname(talloc_tos(),
2502 smb_fname->base_name,
2503 AFPRESOURCE_STREAM_NAME,
2505 if (stream_name == NULL) {
2509 ret = SMB_VFS_STAT(handle->conn, stream_name);
2511 TALLOC_FREE(stream_name);
2515 rfork_size = stream_name->st.st_ex_size;
2516 TALLOC_FREE(stream_name);
2521 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2522 const struct smb_filename *smb_fname)
2524 struct fruit_config_data *config = NULL;
2525 uint64_t rfork_size;
2527 SMB_VFS_HANDLE_GET_DATA(handle, config,
2528 struct fruit_config_data,
2531 switch (config->rsrc) {
2532 case FRUIT_RSRC_ADFILE:
2533 case FRUIT_RSRC_XATTR:
2534 rfork_size = readdir_attr_rfork_size_adouble(handle,
2538 case FRUIT_META_STREAM:
2539 rfork_size = readdir_attr_rfork_size_stream(handle,
2544 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2552 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2553 const struct smb_filename *smb_fname,
2554 struct readdir_attr_data *attr_data)
2556 NTSTATUS status = NT_STATUS_OK;
2557 struct fruit_config_data *config = NULL;
2560 SMB_VFS_HANDLE_GET_DATA(handle, config,
2561 struct fruit_config_data,
2562 return NT_STATUS_UNSUCCESSFUL);
2565 /* Ensure we return a default value in the creation_date field */
2566 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2569 * Resource fork length
2572 if (config->readdir_attr_rsize) {
2573 uint64_t rfork_size;
2575 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2576 attr_data->attr_data.aapl.rfork_size = rfork_size;
2583 if (config->readdir_attr_finder_info) {
2584 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2586 status = NT_STATUS_INTERNAL_ERROR;
2593 /* Search MS NFS style ACE with UNIX mode */
2594 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2596 const struct security_descriptor *psd,
2601 struct fruit_config_data *config = NULL;
2605 SMB_VFS_HANDLE_GET_DATA(handle, config,
2606 struct fruit_config_data,
2607 return NT_STATUS_UNSUCCESSFUL);
2609 if (!global_fruit_config.nego_aapl) {
2610 return NT_STATUS_OK;
2612 if (psd->dacl == NULL || !config->unix_info_enabled) {
2613 return NT_STATUS_OK;
2616 for (i = 0; i < psd->dacl->num_aces; i++) {
2617 if (dom_sid_compare_domain(
2618 &global_sid_Unix_NFS_Mode,
2619 &psd->dacl->aces[i].trustee) == 0) {
2620 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2621 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2624 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2625 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2630 return NT_STATUS_OK;
2633 /****************************************************************************
2635 ****************************************************************************/
2637 static int fruit_connect(vfs_handle_struct *handle,
2638 const char *service,
2642 char *list = NULL, *newlist = NULL;
2643 struct fruit_config_data *config;
2645 DEBUG(10, ("fruit_connect\n"));
2647 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2652 rc = init_fruit_config(handle);
2657 SMB_VFS_HANDLE_GET_DATA(handle, config,
2658 struct fruit_config_data, return -1);
2660 if (config->veto_appledouble) {
2661 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2664 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2665 newlist = talloc_asprintf(
2667 "%s/" ADOUBLE_NAME_PREFIX "*/",
2669 lp_do_parameter(SNUM(handle->conn),
2674 lp_do_parameter(SNUM(handle->conn),
2676 "/" ADOUBLE_NAME_PREFIX "*/");
2682 if (config->encoding == FRUIT_ENC_NATIVE) {
2686 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2687 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2688 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2689 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2690 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2691 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2692 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2693 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2694 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2695 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2702 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2703 struct smb_filename *smb_fname,
2709 char afpinfo_buf[AFP_INFO_SIZE];
2710 ssize_t len, written;
2714 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2719 if (!(flags & (O_CREAT | O_TRUNC))) {
2723 ai = afpinfo_new(talloc_tos());
2729 len = afpinfo_pack(ai, afpinfo_buf);
2730 if (len != AFP_INFO_SIZE) {
2735 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2736 fsp->fh->fd = hostfd;
2738 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2741 if (written != AFP_INFO_SIZE) {
2742 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2750 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2753 int saved_errno = errno;
2755 fsp->fh->fd = hostfd;
2756 SMB_VFS_NEXT_CLOSE(handle, fsp);
2759 errno = saved_errno;
2764 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2765 struct smb_filename *smb_fname,
2771 struct smb_filename *smb_fname_base = NULL;
2774 struct adouble *ad = NULL;
2776 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2778 /* Create an smb_filename with stream_name == NULL. */
2779 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2780 smb_fname->base_name,
2785 if (smb_fname_base == NULL) {
2792 * We use baseflags to turn off nasty side-effects when opening the
2796 baseflags &= ~O_TRUNC;
2797 baseflags &= ~O_EXCL;
2798 baseflags &= ~O_CREAT;
2800 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2804 * It is legit to open a stream on a directory, but the base
2805 * fd has to be read-only.
2807 if ((hostfd == -1) && (errno == EISDIR)) {
2808 baseflags &= ~O_ACCMODE;
2809 baseflags |= O_RDONLY;
2810 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2814 TALLOC_FREE(smb_fname_base);
2821 if (flags & (O_CREAT | O_TRUNC)) {
2823 * The attribute does not exist or needs to be truncated,
2824 * create an AppleDouble EA
2826 ad = ad_init(fsp, handle, ADOUBLE_META);
2832 fsp->fh->fd = hostfd;
2834 rc = ad_fset(ad, fsp);
2845 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2847 int saved_errno = errno;
2850 * BUGBUGBUG -- we would need to call
2851 * fd_close_posix here, but we don't have a
2854 fsp->fh->fd = hostfd;
2855 SMB_VFS_NEXT_CLOSE(handle, fsp);
2858 errno = saved_errno;
2863 static int fruit_open_meta(vfs_handle_struct *handle,
2864 struct smb_filename *smb_fname,
2865 files_struct *fsp, int flags, mode_t mode)
2868 struct fruit_config_data *config = NULL;
2869 struct fio *fio = NULL;
2871 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2873 SMB_VFS_HANDLE_GET_DATA(handle, config,
2874 struct fruit_config_data, return -1);
2876 switch (config->meta) {
2877 case FRUIT_META_STREAM:
2878 fd = fruit_open_meta_stream(handle, smb_fname,
2882 case FRUIT_META_NETATALK:
2883 fd = fruit_open_meta_netatalk(handle, smb_fname,
2888 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2892 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2898 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
2899 fio->type = ADOUBLE_META;
2900 fio->config = config;
2905 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2906 struct smb_filename *smb_fname,
2912 struct adouble *ad = NULL;
2913 struct smb_filename *smb_fname_base = NULL;
2914 struct fruit_config_data *config = NULL;
2917 SMB_VFS_HANDLE_GET_DATA(handle, config,
2918 struct fruit_config_data, return -1);
2920 if ((!(flags & O_CREAT)) &&
2921 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
2923 /* sorry, but directories don't habe a resource fork */
2928 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
2933 /* Sanitize flags */
2934 if (flags & O_WRONLY) {
2935 /* We always need read access for the metadata header too */
2940 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2947 if (flags & (O_CREAT | O_TRUNC)) {
2948 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
2954 fsp->fh->fd = hostfd;
2956 rc = ad_fset(ad, fsp);
2967 TALLOC_FREE(smb_fname_base);
2969 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2971 int saved_errno = errno;
2974 * BUGBUGBUG -- we would need to call
2975 * fd_close_posix here, but we don't have a
2978 fsp->fh->fd = hostfd;
2982 errno = saved_errno;
2987 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2988 struct smb_filename *smb_fname,
2993 #ifdef HAVE_ATTROPEN
2996 fd = attropen(smb_fname->base_name,
2997 AFPRESOURCE_EA_NETATALK,
3012 static int fruit_open_rsrc(vfs_handle_struct *handle,
3013 struct smb_filename *smb_fname,
3014 files_struct *fsp, int flags, mode_t mode)
3017 struct fruit_config_data *config = NULL;
3018 struct fio *fio = NULL;
3020 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3022 SMB_VFS_HANDLE_GET_DATA(handle, config,
3023 struct fruit_config_data, return -1);
3025 if (((flags & O_ACCMODE) == O_RDONLY)
3026 && (flags & O_CREAT)
3027 && !VALID_STAT(fsp->fsp_name->st))
3030 * This means the stream doesn't exist. macOS SMB server fails
3031 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3032 * 12565 and the test for this combination in
3033 * test_rfork_create().
3039 switch (config->rsrc) {
3040 case FRUIT_RSRC_STREAM:
3041 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3044 case FRUIT_RSRC_ADFILE:
3045 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3049 case FRUIT_RSRC_XATTR:
3050 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3055 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3059 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3065 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3066 fio->type = ADOUBLE_RSRC;
3067 fio->config = config;
3072 static int fruit_open(vfs_handle_struct *handle,
3073 struct smb_filename *smb_fname,
3074 files_struct *fsp, int flags, mode_t mode)
3078 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3080 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3081 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3084 if (is_afpinfo_stream(smb_fname)) {
3085 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3086 } else if (is_afpresource_stream(smb_fname)) {
3087 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3089 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3092 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3097 static int fruit_rename(struct vfs_handle_struct *handle,
3098 const struct smb_filename *smb_fname_src,
3099 const struct smb_filename *smb_fname_dst)
3102 struct fruit_config_data *config = NULL;
3103 struct smb_filename *src_adp_smb_fname = NULL;
3104 struct smb_filename *dst_adp_smb_fname = NULL;
3106 SMB_VFS_HANDLE_GET_DATA(handle, config,
3107 struct fruit_config_data, return -1);
3109 if (!VALID_STAT(smb_fname_src->st)) {
3110 DBG_ERR("Need valid stat for [%s]\n",
3111 smb_fname_str_dbg(smb_fname_src));
3115 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3120 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3121 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3126 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3131 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3136 DBG_DEBUG("%s -> %s\n",
3137 smb_fname_str_dbg(src_adp_smb_fname),
3138 smb_fname_str_dbg(dst_adp_smb_fname));
3140 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3141 if (errno == ENOENT) {
3146 TALLOC_FREE(src_adp_smb_fname);
3147 TALLOC_FREE(dst_adp_smb_fname);
3151 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3152 const struct smb_filename *smb_fname)
3154 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3157 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3158 const struct smb_filename *smb_fname)
3160 return SMB_VFS_REMOVEXATTR(handle->conn,
3162 AFPINFO_EA_NETATALK);
3165 static int fruit_unlink_meta(vfs_handle_struct *handle,
3166 const struct smb_filename *smb_fname)
3168 struct fruit_config_data *config = NULL;
3171 SMB_VFS_HANDLE_GET_DATA(handle, config,
3172 struct fruit_config_data, return -1);
3174 switch (config->meta) {
3175 case FRUIT_META_STREAM:
3176 rc = fruit_unlink_meta_stream(handle, smb_fname);
3179 case FRUIT_META_NETATALK:
3180 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3184 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3191 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3192 const struct smb_filename *smb_fname,
3197 if (!force_unlink) {
3198 struct smb_filename *smb_fname_cp = NULL;
3201 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3202 if (smb_fname_cp == NULL) {
3207 * 0 byte resource fork streams are not listed by
3208 * vfs_streaminfo, as a result stream cleanup/deletion of file
3209 * deletion doesn't remove the resourcefork stream.
3212 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3214 TALLOC_FREE(smb_fname_cp);
3215 DBG_ERR("stat [%s] failed [%s]\n",
3216 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3220 size = smb_fname_cp->st.st_ex_size;
3221 TALLOC_FREE(smb_fname_cp);
3224 /* OS X ignores resource fork stream delete requests */
3229 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3230 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3237 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3238 const struct smb_filename *smb_fname,
3242 struct adouble *ad = NULL;
3243 struct smb_filename *adp_smb_fname = NULL;
3245 if (!force_unlink) {
3246 ad = ad_get(talloc_tos(), handle, smb_fname,
3255 * 0 byte resource fork streams are not listed by
3256 * vfs_streaminfo, as a result stream cleanup/deletion of file
3257 * deletion doesn't remove the resourcefork stream.
3260 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3261 /* OS X ignores resource fork stream delete requests */
3269 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3274 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3275 TALLOC_FREE(adp_smb_fname);
3276 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3283 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3284 const struct smb_filename *smb_fname,
3288 * OS X ignores resource fork stream delete requests, so nothing to do
3289 * here. Removing the file will remove the xattr anyway, so we don't
3290 * have to take care of removing 0 byte resource forks that could be
3296 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3297 const struct smb_filename *smb_fname,
3300 struct fruit_config_data *config = NULL;
3303 SMB_VFS_HANDLE_GET_DATA(handle, config,
3304 struct fruit_config_data, return -1);
3306 switch (config->rsrc) {
3307 case FRUIT_RSRC_STREAM:
3308 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3311 case FRUIT_RSRC_ADFILE:
3312 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3315 case FRUIT_RSRC_XATTR:
3316 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3320 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3327 static int fruit_unlink(vfs_handle_struct *handle,
3328 const struct smb_filename *smb_fname)
3331 struct fruit_config_data *config = NULL;
3332 struct smb_filename *rsrc_smb_fname = NULL;
3334 SMB_VFS_HANDLE_GET_DATA(handle, config,
3335 struct fruit_config_data, return -1);
3337 if (is_afpinfo_stream(smb_fname)) {
3338 return fruit_unlink_meta(handle, smb_fname);
3339 } else if (is_afpresource_stream(smb_fname)) {
3340 return fruit_unlink_rsrc(handle, smb_fname, false);
3341 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3342 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3346 * A request to delete the base file. Because 0 byte resource
3347 * fork streams are not listed by fruit_streaminfo,
3348 * delete_all_streams() can't remove 0 byte resource fork
3349 * streams, so we have to cleanup this here.
3351 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3352 smb_fname->base_name,
3353 AFPRESOURCE_STREAM_NAME,
3356 if (rsrc_smb_fname == NULL) {
3360 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3361 if ((rc != 0) && (errno != ENOENT)) {
3362 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3363 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3364 TALLOC_FREE(rsrc_smb_fname);
3367 TALLOC_FREE(rsrc_smb_fname);
3369 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3372 static int fruit_chmod(vfs_handle_struct *handle,
3373 const struct smb_filename *smb_fname,
3377 struct fruit_config_data *config = NULL;
3378 struct smb_filename *smb_fname_adp = NULL;
3380 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3385 SMB_VFS_HANDLE_GET_DATA(handle, config,
3386 struct fruit_config_data, return -1);
3388 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3392 if (!VALID_STAT(smb_fname->st)) {
3396 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3400 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3405 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3407 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3408 if (errno == ENOENT) {
3412 TALLOC_FREE(smb_fname_adp);
3416 static int fruit_chown(vfs_handle_struct *handle,
3417 const struct smb_filename *smb_fname,
3422 struct fruit_config_data *config = NULL;
3423 struct smb_filename *adp_smb_fname = NULL;
3425 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3430 SMB_VFS_HANDLE_GET_DATA(handle, config,
3431 struct fruit_config_data, return -1);
3433 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3437 if (!VALID_STAT(smb_fname->st)) {
3441 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3445 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3450 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3452 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3453 if (errno == ENOENT) {
3458 TALLOC_FREE(adp_smb_fname);
3462 static int fruit_rmdir(struct vfs_handle_struct *handle,
3463 const struct smb_filename *smb_fname)
3467 struct fruit_config_data *config;
3469 SMB_VFS_HANDLE_GET_DATA(handle, config,
3470 struct fruit_config_data, return -1);
3472 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3477 * Due to there is no way to change bDeleteVetoFiles variable
3478 * from this module, need to clean up ourselves
3481 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3486 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3488 struct adouble *ad = NULL;
3490 struct smb_filename *ad_smb_fname = NULL;
3493 match = strncmp(de->d_name,
3494 ADOUBLE_NAME_PREFIX,
3495 strlen(ADOUBLE_NAME_PREFIX));
3500 p = talloc_asprintf(talloc_tos(), "%s/%s",
3501 smb_fname->base_name, de->d_name);
3503 DBG_ERR("talloc_asprintf failed\n");
3507 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3511 if (ad_smb_fname == NULL) {
3512 DBG_ERR("synthetic_smb_fname failed\n");
3517 * Check whether it's a valid AppleDouble file, if
3518 * yes, delete it, ignore it otherwise.
3520 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3522 TALLOC_FREE(ad_smb_fname);
3528 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3529 TALLOC_FREE(ad_smb_fname);
3531 DBG_ERR("Deleting [%s] failed\n",
3532 smb_fname_str_dbg(ad_smb_fname));
3540 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3543 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3544 files_struct *fsp, void *data,
3545 size_t n, off_t offset)
3550 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3556 DBG_ERR("Removing [%s] after short read [%zd]\n",
3557 fsp_str_dbg(fsp), nread);
3559 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3561 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3569 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3570 files_struct *fsp, void *data,
3571 size_t n, off_t offset)
3574 struct adouble *ad = NULL;
3575 char afpinfo_buf[AFP_INFO_SIZE];
3579 ai = afpinfo_new(talloc_tos());
3584 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3590 p = ad_get_entry(ad, ADEID_FINDERI);
3592 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3597 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3599 nread = afpinfo_pack(ai, afpinfo_buf);
3600 if (nread != AFP_INFO_SIZE) {
3605 memcpy(data, afpinfo_buf, n);
3613 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3614 files_struct *fsp, void *data,
3615 size_t n, off_t offset)
3617 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3622 * OS X has a off-by-1 error in the offset calculation, so we're
3623 * bug compatible here. It won't hurt, as any relevant real
3624 * world read requests from the AFP_AfpInfo stream will be
3625 * offset=0 n=60. offset is ignored anyway, see below.
3627 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3631 /* Yes, macOS always reads from offset 0 */
3633 to_return = MIN(n, AFP_INFO_SIZE);
3635 switch (fio->config->meta) {
3636 case FRUIT_META_STREAM:
3637 nread = fruit_pread_meta_stream(handle, fsp, data,
3641 case FRUIT_META_NETATALK:
3642 nread = fruit_pread_meta_adouble(handle, fsp, data,
3647 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3654 static ssize_t fruit_pread_rsrc_stream(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_xattr(vfs_handle_struct *handle,
3662 files_struct *fsp, void *data,
3663 size_t n, off_t offset)
3665 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3668 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3669 files_struct *fsp, void *data,
3670 size_t n, off_t offset)
3672 struct adouble *ad = NULL;
3675 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3680 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3681 offset + ad_getentryoff(ad, ADEID_RFORK));
3687 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3688 files_struct *fsp, void *data,
3689 size_t n, off_t offset)
3691 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3694 switch (fio->config->rsrc) {
3695 case FRUIT_RSRC_STREAM:
3696 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3699 case FRUIT_RSRC_ADFILE:
3700 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3703 case FRUIT_RSRC_XATTR:
3704 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3708 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3715 static ssize_t fruit_pread(vfs_handle_struct *handle,
3716 files_struct *fsp, void *data,
3717 size_t n, off_t offset)
3719 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3722 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3723 fsp_str_dbg(fsp), (intmax_t)offset, n);
3726 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3729 if (fio->type == ADOUBLE_META) {
3730 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3732 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3735 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3739 static bool fruit_must_handle_aio_stream(struct fio *fio)
3745 if ((fio->type == ADOUBLE_META) &&
3746 (fio->config->meta == FRUIT_META_NETATALK))
3751 if ((fio->type == ADOUBLE_RSRC) &&
3752 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
3760 struct fruit_pread_state {
3762 struct vfs_aio_state vfs_aio_state;
3765 static void fruit_pread_done(struct tevent_req *subreq);
3767 static struct tevent_req *fruit_pread_send(
3768 struct vfs_handle_struct *handle,
3769 TALLOC_CTX *mem_ctx,
3770 struct tevent_context *ev,
3771 struct files_struct *fsp,
3773 size_t n, off_t offset)
3775 struct tevent_req *req = NULL;
3776 struct tevent_req *subreq = NULL;
3777 struct fruit_pread_state *state = NULL;
3778 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3780 req = tevent_req_create(mem_ctx, &state,
3781 struct fruit_pread_state);
3786 if (fruit_must_handle_aio_stream(fio)) {
3787 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
3788 if (state->nread != n) {
3789 if (state->nread != -1) {
3792 tevent_req_error(req, errno);
3793 return tevent_req_post(req, ev);
3795 tevent_req_done(req);
3796 return tevent_req_post(req, ev);
3799 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
3801 if (tevent_req_nomem(req, subreq)) {
3802 return tevent_req_post(req, ev);
3804 tevent_req_set_callback(subreq, fruit_pread_done, req);
3808 static void fruit_pread_done(struct tevent_req *subreq)
3810 struct tevent_req *req = tevent_req_callback_data(
3811 subreq, struct tevent_req);
3812 struct fruit_pread_state *state = tevent_req_data(
3813 req, struct fruit_pread_state);
3815 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
3816 TALLOC_FREE(subreq);
3818 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3821 tevent_req_done(req);
3824 static ssize_t fruit_pread_recv(struct tevent_req *req,
3825 struct vfs_aio_state *vfs_aio_state)
3827 struct fruit_pread_state *state = tevent_req_data(
3828 req, struct fruit_pread_state);
3830 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3834 *vfs_aio_state = state->vfs_aio_state;
3835 return state->nread;
3838 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
3839 files_struct *fsp, const void *data,
3840 size_t n, off_t offset)
3845 ai = afpinfo_unpack(talloc_tos(), data);
3850 if (ai_empty_finderinfo(ai)) {
3851 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3852 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3853 DBG_ERR("Can't delete metadata for %s: %s\n",
3854 fsp_str_dbg(fsp), strerror(errno));
3862 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3865 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
3866 files_struct *fsp, const void *data,
3867 size_t n, off_t offset)
3869 struct adouble *ad = NULL;
3874 ai = afpinfo_unpack(talloc_tos(), data);
3879 if (ai_empty_finderinfo(ai)) {
3880 ret = SMB_VFS_REMOVEXATTR(handle->conn,
3882 AFPINFO_EA_NETATALK);
3884 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3885 DBG_ERR("Can't delete metadata for %s: %s\n",
3886 fsp_str_dbg(fsp), strerror(errno));
3893 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3895 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
3900 p = ad_get_entry(ad, ADEID_FINDERI);
3902 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3907 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3909 ret = ad_fset(ad, fsp);
3911 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3920 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
3921 files_struct *fsp, const void *data,
3922 size_t n, off_t offset)
3924 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3928 * Writing an all 0 blob to the metadata stream
3929 * results in the stream being removed on a macOS
3930 * server. This ensures we behave the same and it
3931 * verified by the "delete AFP_AfpInfo by writing all
3934 if (n != AFP_INFO_SIZE || offset != 0) {
3935 DBG_ERR("unexpected offset=%jd or size=%jd\n",
3936 (intmax_t)offset, (intmax_t)n);
3940 switch (fio->config->meta) {
3941 case FRUIT_META_STREAM:
3942 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
3946 case FRUIT_META_NETATALK:
3947 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
3952 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3959 static ssize_t fruit_pwrite_rsrc_stream(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_xattr(vfs_handle_struct *handle,
3967 files_struct *fsp, const void *data,
3968 size_t n, off_t offset)
3970 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3973 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
3974 files_struct *fsp, const void *data,
3975 size_t n, off_t offset)
3977 struct adouble *ad = NULL;
3981 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3983 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
3987 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3988 offset + ad_getentryoff(ad, ADEID_RFORK));
3989 if (nwritten != n) {
3990 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
3991 fsp_str_dbg(fsp), nwritten, n);
3996 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3997 ad_setentrylen(ad, ADEID_RFORK, n + offset);
3998 ret = ad_fset(ad, fsp);
4000 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4010 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4011 files_struct *fsp, const void *data,
4012 size_t n, off_t offset)
4014 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4017 switch (fio->config->rsrc) {
4018 case FRUIT_RSRC_STREAM:
4019 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4022 case FRUIT_RSRC_ADFILE:
4023 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4026 case FRUIT_RSRC_XATTR:
4027 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4031 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4038 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4039 files_struct *fsp, const void *data,
4040 size_t n, off_t offset)
4042 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4045 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4046 fsp_str_dbg(fsp), (intmax_t)offset, n);
4049 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4052 if (fio->type == ADOUBLE_META) {
4053 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4055 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4058 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4062 struct fruit_pwrite_state {
4064 struct vfs_aio_state vfs_aio_state;
4067 static void fruit_pwrite_done(struct tevent_req *subreq);
4069 static struct tevent_req *fruit_pwrite_send(
4070 struct vfs_handle_struct *handle,
4071 TALLOC_CTX *mem_ctx,
4072 struct tevent_context *ev,
4073 struct files_struct *fsp,
4075 size_t n, off_t offset)
4077 struct tevent_req *req = NULL;
4078 struct tevent_req *subreq = NULL;
4079 struct fruit_pwrite_state *state = NULL;
4080 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4082 req = tevent_req_create(mem_ctx, &state,
4083 struct fruit_pwrite_state);
4088 if (fruit_must_handle_aio_stream(fio)) {
4089 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4090 if (state->nwritten != n) {
4091 if (state->nwritten != -1) {
4094 tevent_req_error(req, errno);
4095 return tevent_req_post(req, ev);
4097 tevent_req_done(req);
4098 return tevent_req_post(req, ev);
4101 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4103 if (tevent_req_nomem(req, subreq)) {
4104 return tevent_req_post(req, ev);
4106 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4110 static void fruit_pwrite_done(struct tevent_req *subreq)
4112 struct tevent_req *req = tevent_req_callback_data(
4113 subreq, struct tevent_req);
4114 struct fruit_pwrite_state *state = tevent_req_data(
4115 req, struct fruit_pwrite_state);
4117 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4118 TALLOC_FREE(subreq);
4120 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4123 tevent_req_done(req);
4126 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4127 struct vfs_aio_state *vfs_aio_state)
4129 struct fruit_pwrite_state *state = tevent_req_data(
4130 req, struct fruit_pwrite_state);
4132 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4136 *vfs_aio_state = state->vfs_aio_state;
4137 return state->nwritten;
4141 * Helper to stat/lstat the base file of an smb_fname.
4143 static int fruit_stat_base(vfs_handle_struct *handle,
4144 struct smb_filename *smb_fname,
4147 char *tmp_stream_name;
4150 tmp_stream_name = smb_fname->stream_name;
4151 smb_fname->stream_name = NULL;
4153 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4155 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4157 smb_fname->stream_name = tmp_stream_name;
4161 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4162 struct smb_filename *smb_fname,
4168 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4170 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4176 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4177 struct smb_filename *smb_fname,
4180 struct adouble *ad = NULL;
4182 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4184 DBG_INFO("fruit_stat_meta %s: %s\n",
4185 smb_fname_str_dbg(smb_fname), strerror(errno));
4191 /* Populate the stat struct with info from the base file. */
4192 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4195 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4196 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4197 smb_fname->stream_name);
4201 static int fruit_stat_meta(vfs_handle_struct *handle,
4202 struct smb_filename *smb_fname,
4205 struct fruit_config_data *config = NULL;
4208 SMB_VFS_HANDLE_GET_DATA(handle, config,
4209 struct fruit_config_data, return -1);
4211 switch (config->meta) {
4212 case FRUIT_META_STREAM:
4213 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4216 case FRUIT_META_NETATALK:
4217 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4221 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4228 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4229 struct smb_filename *smb_fname,
4232 struct adouble *ad = NULL;
4235 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4241 /* Populate the stat struct with info from the base file. */
4242 ret = fruit_stat_base(handle, smb_fname, follow_links);
4248 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4249 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4250 smb_fname->stream_name);
4255 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4256 struct smb_filename *smb_fname,
4262 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4264 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4270 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4271 struct smb_filename *smb_fname,
4274 #ifdef HAVE_ATTROPEN
4278 /* Populate the stat struct with info from the base file. */
4279 ret = fruit_stat_base(handle, smb_fname, follow_links);
4284 fd = attropen(smb_fname->base_name,
4285 AFPRESOURCE_EA_NETATALK,
4291 ret = sys_fstat(fd, &smb_fname->st, false);
4294 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4295 AFPRESOURCE_EA_NETATALK);
4301 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4302 smb_fname->stream_name);
4312 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4313 struct smb_filename *smb_fname,
4316 struct fruit_config_data *config = NULL;
4319 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4321 SMB_VFS_HANDLE_GET_DATA(handle, config,
4322 struct fruit_config_data, return -1);
4324 switch (config->rsrc) {
4325 case FRUIT_RSRC_STREAM:
4326 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4329 case FRUIT_RSRC_XATTR:
4330 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4333 case FRUIT_RSRC_ADFILE:
4334 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4338 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4345 static int fruit_stat(vfs_handle_struct *handle,
4346 struct smb_filename *smb_fname)
4350 DEBUG(10, ("fruit_stat called for %s\n",
4351 smb_fname_str_dbg(smb_fname)));
4353 if (!is_ntfs_stream_smb_fname(smb_fname)
4354 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4355 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4357 update_btime(handle, smb_fname);
4363 * Note if lp_posix_paths() is true, we can never
4364 * get here as is_ntfs_stream_smb_fname() is
4365 * always false. So we never need worry about
4366 * not following links here.
4369 if (is_afpinfo_stream(smb_fname)) {
4370 rc = fruit_stat_meta(handle, smb_fname, true);
4371 } else if (is_afpresource_stream(smb_fname)) {
4372 rc = fruit_stat_rsrc(handle, smb_fname, true);
4374 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4378 update_btime(handle, smb_fname);
4379 smb_fname->st.st_ex_mode &= ~S_IFMT;
4380 smb_fname->st.st_ex_mode |= S_IFREG;
4381 smb_fname->st.st_ex_blocks =
4382 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4387 static int fruit_lstat(vfs_handle_struct *handle,
4388 struct smb_filename *smb_fname)
4392 DEBUG(10, ("fruit_lstat called for %s\n",
4393 smb_fname_str_dbg(smb_fname)));
4395 if (!is_ntfs_stream_smb_fname(smb_fname)
4396 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4397 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4399 update_btime(handle, smb_fname);
4404 if (is_afpinfo_stream(smb_fname)) {
4405 rc = fruit_stat_meta(handle, smb_fname, false);
4406 } else if (is_afpresource_stream(smb_fname)) {
4407 rc = fruit_stat_rsrc(handle, smb_fname, false);
4409 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4413 update_btime(handle, smb_fname);
4414 smb_fname->st.st_ex_mode &= ~S_IFMT;
4415 smb_fname->st.st_ex_mode |= S_IFREG;
4416 smb_fname->st.st_ex_blocks =
4417 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4422 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4424 SMB_STRUCT_STAT *sbuf)
4426 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4429 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4431 SMB_STRUCT_STAT *sbuf)
4435 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4440 *sbuf = fsp->base_fsp->fsp_name->st;
4441 sbuf->st_ex_size = AFP_INFO_SIZE;
4442 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4447 static int fruit_fstat_meta(vfs_handle_struct *handle,
4449 SMB_STRUCT_STAT *sbuf,
4454 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4456 switch (fio->config->meta) {
4457 case FRUIT_META_STREAM:
4458 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4461 case FRUIT_META_NETATALK:
4462 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4466 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4470 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4474 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4476 SMB_STRUCT_STAT *sbuf)
4478 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4481 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4483 SMB_STRUCT_STAT *sbuf)
4485 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4488 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4490 SMB_STRUCT_STAT *sbuf)
4492 struct adouble *ad = NULL;
4495 /* Populate the stat struct with info from the base file. */
4496 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4501 ad = ad_get(talloc_tos(), handle,
4502 fsp->base_fsp->fsp_name,
4505 DBG_ERR("ad_get [%s] failed [%s]\n",
4506 fsp_str_dbg(fsp), strerror(errno));
4510 *sbuf = fsp->base_fsp->fsp_name->st;
4511 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4512 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4518 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4519 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4523 switch (fio->config->rsrc) {
4524 case FRUIT_RSRC_STREAM:
4525 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4528 case FRUIT_RSRC_ADFILE:
4529 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4532 case FRUIT_RSRC_XATTR:
4533 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4537 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4544 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4545 SMB_STRUCT_STAT *sbuf)
4547 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4551 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4554 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4556 if (fio->type == ADOUBLE_META) {
4557 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4559 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4563 sbuf->st_ex_mode &= ~S_IFMT;
4564 sbuf->st_ex_mode |= S_IFREG;
4565 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4568 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4569 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4573 static NTSTATUS fruit_streaminfo_meta_stream(
4574 vfs_handle_struct *handle,
4575 struct files_struct *fsp,
4576 const struct smb_filename *smb_fname,
4577 TALLOC_CTX *mem_ctx,
4578 unsigned int *pnum_streams,
4579 struct stream_struct **pstreams)
4581 struct stream_struct *stream = *pstreams;
4582 unsigned int num_streams = *pnum_streams;
4583 struct smb_filename *sname = NULL;
4588 for (i = 0; i < num_streams; i++) {
4589 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4594 if (i == num_streams) {
4595 return NT_STATUS_OK;
4598 if (stream[i].size == AFP_INFO_SIZE) {
4599 return NT_STATUS_OK;
4602 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4603 "from [%s]\n", (intmax_t)stream[i].size,
4604 smb_fname_str_dbg(smb_fname));
4606 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4608 return NT_STATUS_INTERNAL_ERROR;
4611 sname = synthetic_smb_fname(talloc_tos(),
4612 smb_fname->base_name,
4613 AFPINFO_STREAM_NAME,
4615 if (sname == NULL) {
4616 return NT_STATUS_NO_MEMORY;
4619 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4622 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4623 return map_nt_error_from_unix(errno);
4626 return NT_STATUS_OK;
4629 static NTSTATUS fruit_streaminfo_meta_netatalk(
4630 vfs_handle_struct *handle,
4631 struct files_struct *fsp,
4632 const struct smb_filename *smb_fname,
4633 TALLOC_CTX *mem_ctx,
4634 unsigned int *pnum_streams,
4635 struct stream_struct **pstreams)
4637 struct stream_struct *stream = *pstreams;
4638 unsigned int num_streams = *pnum_streams;
4639 struct adouble *ad = NULL;
4644 /* Remove the Netatalk xattr from the list */
4645 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4646 ":" NETATALK_META_XATTR ":$DATA");
4648 return NT_STATUS_NO_MEMORY;
4652 * Check if there's a AFPINFO_STREAM from the VFS streams
4653 * backend and if yes, remove it from the list
4655 for (i = 0; i < num_streams; i++) {
4656 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4661 if (i < num_streams) {
4662 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4663 smb_fname_str_dbg(smb_fname));
4665 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4668 return NT_STATUS_INTERNAL_ERROR;
4672 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4674 return NT_STATUS_OK;
4677 is_fi_empty = ad_empty_finderinfo(ad);
4681 return NT_STATUS_OK;
4684 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4685 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4686 smb_roundup(handle->conn, AFP_INFO_SIZE));
4688 return NT_STATUS_NO_MEMORY;
4691 return NT_STATUS_OK;
4694 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4695 struct files_struct *fsp,
4696 const struct smb_filename *smb_fname,
4697 TALLOC_CTX *mem_ctx,
4698 unsigned int *pnum_streams,
4699 struct stream_struct **pstreams)
4701 struct fruit_config_data *config = NULL;
4704 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4705 return NT_STATUS_INTERNAL_ERROR);
4707 switch (config->meta) {
4708 case FRUIT_META_NETATALK:
4709 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4710 mem_ctx, pnum_streams,
4714 case FRUIT_META_STREAM:
4715 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4716 mem_ctx, pnum_streams,
4721 return NT_STATUS_INTERNAL_ERROR;
4727 static NTSTATUS fruit_streaminfo_rsrc_stream(
4728 vfs_handle_struct *handle,
4729 struct files_struct *fsp,
4730 const struct smb_filename *smb_fname,
4731 TALLOC_CTX *mem_ctx,
4732 unsigned int *pnum_streams,
4733 struct stream_struct **pstreams)
4737 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4739 DBG_ERR("Filtering resource stream failed\n");
4740 return NT_STATUS_INTERNAL_ERROR;
4742 return NT_STATUS_OK;
4745 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4746 vfs_handle_struct *handle,
4747 struct files_struct *fsp,
4748 const struct smb_filename *smb_fname,
4749 TALLOC_CTX *mem_ctx,
4750 unsigned int *pnum_streams,
4751 struct stream_struct **pstreams)
4755 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4757 DBG_ERR("Filtering resource stream failed\n");
4758 return NT_STATUS_INTERNAL_ERROR;
4760 return NT_STATUS_OK;
4763 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4764 vfs_handle_struct *handle,
4765 struct files_struct *fsp,
4766 const struct smb_filename *smb_fname,
4767 TALLOC_CTX *mem_ctx,
4768 unsigned int *pnum_streams,
4769 struct stream_struct **pstreams)
4771 struct stream_struct *stream = *pstreams;
4772 unsigned int num_streams = *pnum_streams;
4773 struct adouble *ad = NULL;
4779 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4780 * and if yes, remove it from the list
4782 for (i = 0; i < num_streams; i++) {
4783 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4788 if (i < num_streams) {
4789 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4790 smb_fname_str_dbg(smb_fname));
4792 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4793 AFPRESOURCE_STREAM);
4795 return NT_STATUS_INTERNAL_ERROR;
4799 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4801 return NT_STATUS_OK;
4804 rlen = ad_getentrylen(ad, ADEID_RFORK);
4808 return NT_STATUS_OK;
4811 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4812 AFPRESOURCE_STREAM_NAME, rlen,
4813 smb_roundup(handle->conn, rlen));
4815 return NT_STATUS_NO_MEMORY;
4818 return NT_STATUS_OK;
4821 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4822 struct files_struct *fsp,
4823 const struct smb_filename *smb_fname,
4824 TALLOC_CTX *mem_ctx,
4825 unsigned int *pnum_streams,
4826 struct stream_struct **pstreams)
4828 struct fruit_config_data *config = NULL;
4831 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4832 return NT_STATUS_INTERNAL_ERROR);
4834 switch (config->rsrc) {
4835 case FRUIT_RSRC_STREAM:
4836 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4837 mem_ctx, pnum_streams,
4841 case FRUIT_RSRC_XATTR:
4842 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4843 mem_ctx, pnum_streams,
4847 case FRUIT_RSRC_ADFILE:
4848 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4849 mem_ctx, pnum_streams,
4854 return NT_STATUS_INTERNAL_ERROR;
4860 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4861 struct files_struct *fsp,
4862 const struct smb_filename *smb_fname,
4863 TALLOC_CTX *mem_ctx,
4864 unsigned int *pnum_streams,
4865 struct stream_struct **pstreams)
4867 struct fruit_config_data *config = NULL;
4870 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4871 return NT_STATUS_UNSUCCESSFUL);
4873 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4875 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4876 pnum_streams, pstreams);
4877 if (!NT_STATUS_IS_OK(status)) {
4881 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4882 mem_ctx, pnum_streams, pstreams);
4883 if (!NT_STATUS_IS_OK(status)) {
4887 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4888 mem_ctx, pnum_streams, pstreams);
4889 if (!NT_STATUS_IS_OK(status)) {
4893 return NT_STATUS_OK;
4896 static int fruit_ntimes(vfs_handle_struct *handle,
4897 const struct smb_filename *smb_fname,
4898 struct smb_file_time *ft)
4901 struct adouble *ad = NULL;
4902 struct fruit_config_data *config = NULL;
4904 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4907 if ((config->meta != FRUIT_META_NETATALK) ||
4908 null_timespec(ft->create_time))
4910 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4913 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4914 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4916 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4921 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4922 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4924 rc = ad_set(ad, smb_fname);
4930 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4933 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4936 static int fruit_fallocate(struct vfs_handle_struct *handle,
4937 struct files_struct *fsp,
4942 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4945 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4948 /* Let the pwrite code path handle it. */
4953 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4954 struct files_struct *fsp,
4958 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4961 #ifdef HAVE_ATTROPEN
4962 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4967 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4968 struct files_struct *fsp,
4972 struct adouble *ad = NULL;
4975 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4977 DBG_DEBUG("ad_get [%s] failed [%s]\n",
4978 fsp_str_dbg(fsp), strerror(errno));
4982 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4984 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset + ad_off);
4990 ad_setentrylen(ad, ADEID_RFORK, offset);
4992 rc = ad_fset(ad, fsp);
4994 DBG_ERR("ad_fset [%s] failed [%s]\n",
4995 fsp_str_dbg(fsp), strerror(errno));
5004 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5005 struct files_struct *fsp,
5009 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5012 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5015 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5016 struct files_struct *fsp,
5019 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5022 switch (fio->config->rsrc) {
5023 case FRUIT_RSRC_XATTR:
5024 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5027 case FRUIT_RSRC_ADFILE:
5028 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5031 case FRUIT_RSRC_STREAM:
5032 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5036 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5044 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5045 struct files_struct *fsp,
5049 DBG_WARNING("ftruncate %s to %jd",
5050 fsp_str_dbg(fsp), (intmax_t)offset);
5051 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5056 /* OS X returns success but does nothing */
5057 DBG_INFO("ignoring ftruncate %s to %jd\n",
5058 fsp_str_dbg(fsp), (intmax_t)offset);
5062 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5063 struct files_struct *fsp,
5066 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5069 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5073 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5076 if (fio->type == ADOUBLE_META) {
5077 ret = fruit_ftruncate_meta(handle, fsp, offset);
5079 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5082 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5086 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5087 struct smb_request *req,
5088 uint16_t root_dir_fid,
5089 struct smb_filename *smb_fname,
5090 uint32_t access_mask,
5091 uint32_t share_access,
5092 uint32_t create_disposition,
5093 uint32_t create_options,
5094 uint32_t file_attributes,
5095 uint32_t oplock_request,
5096 struct smb2_lease *lease,
5097 uint64_t allocation_size,
5098 uint32_t private_flags,
5099 struct security_descriptor *sd,
5100 struct ea_list *ea_list,
5101 files_struct **result,
5103 const struct smb2_create_blobs *in_context_blobs,
5104 struct smb2_create_blobs *out_context_blobs)
5107 struct fruit_config_data *config = NULL;
5108 files_struct *fsp = NULL;
5110 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5111 if (!NT_STATUS_IS_OK(status)) {
5115 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5116 return NT_STATUS_UNSUCCESSFUL);
5118 status = SMB_VFS_NEXT_CREATE_FILE(
5119 handle, req, root_dir_fid, smb_fname,
5120 access_mask, share_access,
5121 create_disposition, create_options,
5122 file_attributes, oplock_request,
5124 allocation_size, private_flags,
5125 sd, ea_list, result,
5126 pinfo, in_context_blobs, out_context_blobs);
5127 if (!NT_STATUS_IS_OK(status)) {
5133 if (global_fruit_config.nego_aapl) {
5134 if (config->posix_rename && fsp->is_directory) {
5136 * Enable POSIX directory rename behaviour
5138 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5143 * If this is a plain open for existing files, opening an 0
5144 * byte size resource fork MUST fail with
5145 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5147 * Cf the vfs_fruit torture tests in test_rfork_create().
5149 if (is_afpresource_stream(fsp->fsp_name) &&
5150 create_disposition == FILE_OPEN)
5152 if (fsp->fsp_name->st.st_ex_size == 0) {
5153 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5158 if (is_ntfs_stream_smb_fname(smb_fname)
5159 || fsp->is_directory) {
5163 if (config->locking == FRUIT_LOCKING_NETATALK) {
5164 status = fruit_check_access(
5167 map_share_mode_to_deny_mode(share_access, 0));
5168 if (!NT_STATUS_IS_OK(status)) {
5176 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5179 close_file(req, fsp, ERROR_CLOSE);
5180 *result = fsp = NULL;
5186 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5187 const struct smb_filename *fname,
5188 TALLOC_CTX *mem_ctx,
5189 struct readdir_attr_data **pattr_data)
5191 struct fruit_config_data *config = NULL;
5192 struct readdir_attr_data *attr_data;
5195 SMB_VFS_HANDLE_GET_DATA(handle, config,
5196 struct fruit_config_data,
5197 return NT_STATUS_UNSUCCESSFUL);
5199 if (!global_fruit_config.nego_aapl) {
5200 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5203 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5205 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5206 if (*pattr_data == NULL) {
5207 return NT_STATUS_UNSUCCESSFUL;
5209 attr_data = *pattr_data;
5210 attr_data->type = RDATTR_AAPL;
5213 * Mac metadata: compressed FinderInfo, resource fork length
5216 status = readdir_attr_macmeta(handle, fname, attr_data);
5217 if (!NT_STATUS_IS_OK(status)) {
5219 * Error handling is tricky: if we return failure from
5220 * this function, the corresponding directory entry
5221 * will to be passed to the client, so we really just
5222 * want to error out on fatal errors.
5224 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5232 if (config->unix_info_enabled) {
5233 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5239 if (!config->readdir_attr_max_access) {
5240 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5242 status = smbd_calculate_access_mask(
5246 SEC_FLAG_MAXIMUM_ALLOWED,
5247 &attr_data->attr_data.aapl.max_access);
5248 if (!NT_STATUS_IS_OK(status)) {
5253 return NT_STATUS_OK;
5256 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5257 fname->base_name, nt_errstr(status)));
5258 TALLOC_FREE(*pattr_data);
5262 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5264 uint32_t security_info,
5265 TALLOC_CTX *mem_ctx,
5266 struct security_descriptor **ppdesc)
5269 struct security_ace ace;
5271 struct fruit_config_data *config;
5273 SMB_VFS_HANDLE_GET_DATA(handle, config,
5274 struct fruit_config_data,
5275 return NT_STATUS_UNSUCCESSFUL);
5277 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5279 if (!NT_STATUS_IS_OK(status)) {
5284 * Add MS NFS style ACEs with uid, gid and mode
5286 if (!global_fruit_config.nego_aapl) {
5287 return NT_STATUS_OK;
5289 if (!config->unix_info_enabled) {
5290 return NT_STATUS_OK;
5293 /* MS NFS style mode */
5294 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5295 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5296 status = security_descriptor_dacl_add(*ppdesc, &ace);
5297 if (!NT_STATUS_IS_OK(status)) {
5298 DEBUG(1,("failed to add MS NFS style ACE\n"));
5302 /* MS NFS style uid */
5303 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5304 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5305 status = security_descriptor_dacl_add(*ppdesc, &ace);
5306 if (!NT_STATUS_IS_OK(status)) {
5307 DEBUG(1,("failed to add MS NFS style ACE\n"));
5311 /* MS NFS style gid */
5312 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5313 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5314 status = security_descriptor_dacl_add(*ppdesc, &ace);
5315 if (!NT_STATUS_IS_OK(status)) {
5316 DEBUG(1,("failed to add MS NFS style ACE\n"));
5320 return NT_STATUS_OK;
5323 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5325 uint32_t security_info_sent,
5326 const struct security_descriptor *psd)
5330 mode_t ms_nfs_mode = 0;
5333 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5335 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5336 if (!NT_STATUS_IS_OK(status)) {
5337 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5341 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5342 if (!NT_STATUS_IS_OK(status)) {
5343 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5348 if (fsp->fh->fd != -1) {
5349 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5351 result = SMB_VFS_CHMOD(fsp->conn,
5357 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5358 result, (unsigned)ms_nfs_mode,
5360 status = map_nt_error_from_unix(errno);
5365 return NT_STATUS_OK;
5368 static struct vfs_offload_ctx *fruit_offload_ctx;
5370 struct fruit_offload_read_state {
5371 struct vfs_handle_struct *handle;
5372 struct tevent_context *ev;
5378 static void fruit_offload_read_done(struct tevent_req *subreq);
5380 static struct tevent_req *fruit_offload_read_send(
5381 TALLOC_CTX *mem_ctx,
5382 struct tevent_context *ev,
5383 struct vfs_handle_struct *handle,
5390 struct tevent_req *req = NULL;
5391 struct tevent_req *subreq = NULL;
5392 struct fruit_offload_read_state *state = NULL;
5394 req = tevent_req_create(mem_ctx, &state,
5395 struct fruit_offload_read_state);
5399 *state = (struct fruit_offload_read_state) {
5406 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5407 fsctl, ttl, offset, to_copy);
5408 if (tevent_req_nomem(subreq, req)) {
5409 return tevent_req_post(req, ev);
5411 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5415 static void fruit_offload_read_done(struct tevent_req *subreq)
5417 struct tevent_req *req = tevent_req_callback_data(
5418 subreq, struct tevent_req);
5419 struct fruit_offload_read_state *state = tevent_req_data(
5420 req, struct fruit_offload_read_state);
5423 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5427 TALLOC_FREE(subreq);
5428 if (tevent_req_nterror(req, status)) {
5432 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5433 tevent_req_done(req);
5437 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5438 &fruit_offload_ctx);
5439 if (tevent_req_nterror(req, status)) {
5443 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5446 if (tevent_req_nterror(req, status)) {
5450 tevent_req_done(req);
5454 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5455 struct vfs_handle_struct *handle,
5456 TALLOC_CTX *mem_ctx,
5459 struct fruit_offload_read_state *state = tevent_req_data(
5460 req, struct fruit_offload_read_state);
5463 if (tevent_req_is_nterror(req, &status)) {
5464 tevent_req_received(req);
5468 token->length = state->token.length;
5469 token->data = talloc_move(mem_ctx, &state->token.data);
5471 tevent_req_received(req);
5472 return NT_STATUS_OK;
5475 struct fruit_offload_write_state {
5476 struct vfs_handle_struct *handle;
5478 struct files_struct *src_fsp;
5479 struct files_struct *dst_fsp;
5483 static void fruit_offload_write_done(struct tevent_req *subreq);
5484 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
5485 TALLOC_CTX *mem_ctx,
5486 struct tevent_context *ev,
5489 off_t transfer_offset,
5490 struct files_struct *dest_fsp,
5494 struct tevent_req *req, *subreq;
5495 struct fruit_offload_write_state *state;
5497 struct fruit_config_data *config;
5498 off_t src_off = transfer_offset;
5499 files_struct *src_fsp = NULL;
5500 off_t to_copy = num;
5501 bool copyfile_enabled = false;
5503 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5504 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5506 SMB_VFS_HANDLE_GET_DATA(handle, config,
5507 struct fruit_config_data,
5510 req = tevent_req_create(mem_ctx, &state,
5511 struct fruit_offload_write_state);
5515 state->handle = handle;
5516 state->dst_fsp = dest_fsp;
5519 case FSCTL_SRV_COPYCHUNK:
5520 case FSCTL_SRV_COPYCHUNK_WRITE:
5521 copyfile_enabled = config->copyfile_enabled;
5528 * Check if this a OS X copyfile style copychunk request with
5529 * a requested chunk count of 0 that was translated to a
5530 * offload_write_send VFS call overloading the parameters src_off
5531 * = dest_off = num = 0.
5533 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
5534 status = vfs_offload_token_db_fetch_fsp(
5535 fruit_offload_ctx, token, &src_fsp);
5536 if (tevent_req_nterror(req, status)) {
5537 return tevent_req_post(req, ev);
5539 state->src_fsp = src_fsp;
5541 status = vfs_stat_fsp(src_fsp);
5542 if (tevent_req_nterror(req, status)) {
5543 return tevent_req_post(req, ev);
5546 to_copy = src_fsp->fsp_name->st.st_ex_size;
5547 state->is_copyfile = true;
5550 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
5559 if (tevent_req_nomem(subreq, req)) {
5560 return tevent_req_post(req, ev);
5563 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
5567 static void fruit_offload_write_done(struct tevent_req *subreq)
5569 struct tevent_req *req = tevent_req_callback_data(
5570 subreq, struct tevent_req);
5571 struct fruit_offload_write_state *state = tevent_req_data(
5572 req, struct fruit_offload_write_state);
5574 unsigned int num_streams = 0;
5575 struct stream_struct *streams = NULL;
5577 struct smb_filename *src_fname_tmp = NULL;
5578 struct smb_filename *dst_fname_tmp = NULL;
5580 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
5583 TALLOC_FREE(subreq);
5584 if (tevent_req_nterror(req, status)) {
5588 if (!state->is_copyfile) {
5589 tevent_req_done(req);
5594 * Now copy all remaining streams. We know the share supports
5595 * streams, because we're in vfs_fruit. We don't do this async
5596 * because streams are few and small.
5598 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5599 state->src_fsp->fsp_name,
5600 req, &num_streams, &streams);
5601 if (tevent_req_nterror(req, status)) {
5605 if (num_streams == 1) {
5606 /* There is always one stream, ::$DATA. */
5607 tevent_req_done(req);
5611 for (i = 0; i < num_streams; i++) {
5612 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5613 __func__, streams[i].name, (size_t)streams[i].size));
5615 src_fname_tmp = synthetic_smb_fname(
5617 state->src_fsp->fsp_name->base_name,
5620 state->src_fsp->fsp_name->flags);
5621 if (tevent_req_nomem(src_fname_tmp, req)) {
5625 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5626 TALLOC_FREE(src_fname_tmp);
5630 dst_fname_tmp = synthetic_smb_fname(
5632 state->dst_fsp->fsp_name->base_name,
5635 state->dst_fsp->fsp_name->flags);
5636 if (tevent_req_nomem(dst_fname_tmp, req)) {
5637 TALLOC_FREE(src_fname_tmp);
5641 status = copy_file(req,
5642 state->handle->conn,
5645 OPENX_FILE_CREATE_IF_NOT_EXIST,
5647 if (!NT_STATUS_IS_OK(status)) {
5648 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5649 smb_fname_str_dbg(src_fname_tmp),
5650 smb_fname_str_dbg(dst_fname_tmp),
5651 nt_errstr(status)));
5652 TALLOC_FREE(src_fname_tmp);
5653 TALLOC_FREE(dst_fname_tmp);
5654 tevent_req_nterror(req, status);
5658 TALLOC_FREE(src_fname_tmp);
5659 TALLOC_FREE(dst_fname_tmp);
5662 TALLOC_FREE(streams);
5663 TALLOC_FREE(src_fname_tmp);
5664 TALLOC_FREE(dst_fname_tmp);
5665 tevent_req_done(req);
5668 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
5669 struct tevent_req *req,
5672 struct fruit_offload_write_state *state = tevent_req_data(
5673 req, struct fruit_offload_write_state);
5676 if (tevent_req_is_nterror(req, &status)) {
5677 DEBUG(1, ("server side copy chunk failed: %s\n",
5678 nt_errstr(status)));
5680 tevent_req_received(req);
5684 *copied = state->copied;
5685 tevent_req_received(req);
5687 return NT_STATUS_OK;
5690 static struct vfs_fn_pointers vfs_fruit_fns = {
5691 .connect_fn = fruit_connect,
5693 /* File operations */
5694 .chmod_fn = fruit_chmod,
5695 .chown_fn = fruit_chown,
5696 .unlink_fn = fruit_unlink,
5697 .rename_fn = fruit_rename,
5698 .rmdir_fn = fruit_rmdir,
5699 .open_fn = fruit_open,
5700 .pread_fn = fruit_pread,
5701 .pwrite_fn = fruit_pwrite,
5702 .pread_send_fn = fruit_pread_send,
5703 .pread_recv_fn = fruit_pread_recv,
5704 .pwrite_send_fn = fruit_pwrite_send,
5705 .pwrite_recv_fn = fruit_pwrite_recv,
5706 .stat_fn = fruit_stat,
5707 .lstat_fn = fruit_lstat,
5708 .fstat_fn = fruit_fstat,
5709 .streaminfo_fn = fruit_streaminfo,
5710 .ntimes_fn = fruit_ntimes,
5711 .ftruncate_fn = fruit_ftruncate,
5712 .fallocate_fn = fruit_fallocate,
5713 .create_file_fn = fruit_create_file,
5714 .readdir_attr_fn = fruit_readdir_attr,
5715 .offload_read_send_fn = fruit_offload_read_send,
5716 .offload_read_recv_fn = fruit_offload_read_recv,
5717 .offload_write_send_fn = fruit_offload_write_send,
5718 .offload_write_recv_fn = fruit_offload_write_recv,
5720 /* NT ACL operations */
5721 .fget_nt_acl_fn = fruit_fget_nt_acl,
5722 .fset_nt_acl_fn = fruit_fset_nt_acl,
5725 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
5726 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5728 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5730 if (!NT_STATUS_IS_OK(ret)) {
5734 vfs_fruit_debug_level = debug_add_class("fruit");
5735 if (vfs_fruit_debug_level == -1) {
5736 vfs_fruit_debug_level = DBGC_VFS;
5737 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5740 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5741 "vfs_fruit_init","fruit",vfs_fruit_debug_level));