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;
145 * Additional options, all enabled by default,
146 * possibly useful for analyzing performance. The associated
147 * operations with each of them may be expensive, so having
148 * the chance to disable them individually gives a chance
149 * tweaking the setup for the particular usecase.
151 bool readdir_attr_rsize;
152 bool readdir_attr_finder_info;
153 bool readdir_attr_max_access;
156 static const struct enum_list fruit_rsrc[] = {
157 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
159 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
163 static const struct enum_list fruit_meta[] = {
164 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
165 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
169 static const struct enum_list fruit_locking[] = {
170 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
171 {FRUIT_LOCKING_NONE, "none"},
175 static const struct enum_list fruit_encoding[] = {
176 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
177 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
181 /*****************************************************************************
182 * Defines, functions and data structures that deal with AppleDouble
183 *****************************************************************************/
186 * There are two AppleDouble blobs we deal with:
188 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
189 * metadata in an xattr
191 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
194 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
197 #define AD_VERSION2 0x00020000
198 #define AD_VERSION AD_VERSION2
201 * AppleDouble entry IDs.
203 #define ADEID_DFORK 1
204 #define ADEID_RFORK 2
206 #define ADEID_COMMENT 4
207 #define ADEID_ICONBW 5
208 #define ADEID_ICONCOL 6
209 #define ADEID_FILEI 7
210 #define ADEID_FILEDATESI 8
211 #define ADEID_FINDERI 9
212 #define ADEID_MACFILEI 10
213 #define ADEID_PRODOSFILEI 11
214 #define ADEID_MSDOSFILEI 12
215 #define ADEID_SHORTNAME 13
216 #define ADEID_AFPFILEI 14
219 /* Private Netatalk entries */
220 #define ADEID_PRIVDEV 16
221 #define ADEID_PRIVINO 17
222 #define ADEID_PRIVSYN 18
223 #define ADEID_PRIVID 19
224 #define ADEID_MAX (ADEID_PRIVID + 1)
227 * These are the real ids for the private entries,
228 * as stored in the adouble file
230 #define AD_DEV 0x80444556
231 #define AD_INO 0x80494E4F
232 #define AD_SYN 0x8053594E
233 #define AD_ID 0x8053567E
235 /* Number of actually used entries */
236 #define ADEID_NUM_XATTR 8
237 #define ADEID_NUM_DOT_UND 2
238 #define ADEID_NUM_RSRC_XATTR 1
240 /* AppleDouble magic */
241 #define AD_APPLESINGLE_MAGIC 0x00051600
242 #define AD_APPLEDOUBLE_MAGIC 0x00051607
243 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
245 /* Sizes of relevant entry bits */
246 #define ADEDLEN_MAGIC 4
247 #define ADEDLEN_VERSION 4
248 #define ADEDLEN_FILLER 16
249 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
250 #define ADEDLEN_NENTRIES 2
251 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
252 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
253 #define AD_ENTRY_LEN_EID 4
254 #define AD_ENTRY_LEN_OFF 4
255 #define AD_ENTRY_LEN_LEN 4
256 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
259 #define ADEDLEN_NAME 255
260 #define ADEDLEN_COMMENT 200
261 #define ADEDLEN_FILEI 16
262 #define ADEDLEN_FINDERI 32
263 #define ADEDLEN_FILEDATESI 16
264 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
265 #define ADEDLEN_AFPFILEI 4
266 #define ADEDLEN_MACFILEI 4
267 #define ADEDLEN_PRODOSFILEI 8
268 #define ADEDLEN_MSDOSFILEI 2
269 #define ADEDLEN_DID 4
270 #define ADEDLEN_PRIVDEV 8
271 #define ADEDLEN_PRIVINO 8
272 #define ADEDLEN_PRIVSYN 8
273 #define ADEDLEN_PRIVID 4
276 #define ADEDOFF_MAGIC 0
277 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
278 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
279 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
281 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
282 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
283 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
284 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
285 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
287 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
288 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
289 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
290 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
292 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
293 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
294 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
296 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
297 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
298 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
299 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
300 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
301 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
303 #if AD_DATASZ_XATTR != 402
304 #error bad size for AD_DATASZ_XATTR
307 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
308 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
310 #if AD_DATASZ_DOT_UND != 82
311 #error bad size for AD_DATASZ_DOT_UND
315 * Sharemode locks fcntl() offsets
317 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
318 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
320 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
322 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
324 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
325 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
326 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
327 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
328 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
329 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
330 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
331 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
332 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
333 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
335 /* Time stuff we overload the bits a little */
336 #define AD_DATE_CREATE 0
337 #define AD_DATE_MODIFY 4
338 #define AD_DATE_BACKUP 8
339 #define AD_DATE_ACCESS 12
340 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
341 AD_DATE_BACKUP | AD_DATE_ACCESS)
342 #define AD_DATE_UNIX (1 << 10)
343 #define AD_DATE_START 0x80000000
344 #define AD_DATE_DELTA 946684800
345 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
346 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
348 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
349 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
350 #define AD_XATTR_HDR_SIZE 36
351 #define AD_XATTR_MAX_HDR_SIZE 65536
353 /* Accessor macros */
354 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
355 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
356 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
357 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
360 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
361 * representation as well as the on-disk format.
363 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
364 * the length of the FinderInfo entry is larger then 32 bytes. It is then
365 * preceeded with 2 bytes padding.
367 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
370 struct ad_xattr_header {
371 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
372 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
373 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
374 uint32_t adx_data_start; /* file offset to attribute data area */
375 uint32_t adx_data_length; /* length of attribute data area */
376 uint32_t adx_reserved[3];
378 uint16_t adx_num_attrs;
381 /* On-disk entries are aligned on 4 byte boundaries */
382 struct ad_xattr_entry {
383 uint32_t adx_offset; /* file offset to data */
384 uint32_t adx_length; /* size of attribute data */
386 uint8_t adx_namelen; /* included the NULL terminator */
387 char *adx_name; /* NULL-terminated UTF-8 name */
396 vfs_handle_struct *ad_handle;
399 adouble_type_t ad_type;
402 struct ad_entry ad_eid[ADEID_MAX];
404 struct ad_xattr_header adx_header;
405 struct ad_xattr_entry *adx_entries;
408 struct ad_entry_order {
409 uint32_t id, offset, len;
412 /* Netatalk AppleDouble metadata xattr */
414 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
415 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
416 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
417 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
418 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
419 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
420 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
421 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
422 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
426 /* AppleDouble resource fork file (the ones prefixed by "._") */
428 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
429 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
430 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
435 * Fake AppleDouble entry oder for resource fork xattr. The xattr
436 * isn't an AppleDouble file, it simply contains the resource data,
437 * but in order to be able to use some API calls like ad_getentryoff()
438 * we build a fake/helper struct adouble with this entry order struct.
441 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
446 /* Conversion from enumerated id to on-disk AppleDouble id */
447 #define AD_EID_DISK(a) (set_eid[a])
448 static const uint32_t set_eid[] = {
449 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
450 AD_DEV, AD_INO, AD_SYN, AD_ID
454 /* tcon config handle */
455 struct fruit_config_data *config;
457 /* Denote stream type, meta or rsrc */
462 * Forward declarations
464 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
465 adouble_type_t type);
466 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
467 static int ad_fset(struct adouble *ad, files_struct *fsp);
468 static int adouble_path(TALLOC_CTX *ctx,
469 const struct smb_filename *smb_fname__in,
470 struct smb_filename **ppsmb_fname_out);
473 * Return a pointer to an AppleDouble entry
475 * Returns NULL if the entry is not present
477 static char *ad_get_entry(const struct adouble *ad, int eid)
479 off_t off = ad_getentryoff(ad, eid);
480 size_t len = ad_getentrylen(ad, eid);
482 if (off == 0 || len == 0) {
486 return ad->ad_data + off;
492 static int ad_getdate(const struct adouble *ad,
493 unsigned int dateoff,
496 bool xlate = (dateoff & AD_DATE_UNIX);
499 dateoff &= AD_DATE_MASK;
500 p = ad_get_entry(ad, ADEID_FILEDATESI);
505 if (dateoff > AD_DATE_ACCESS) {
509 memcpy(date, p + dateoff, sizeof(uint32_t));
512 *date = AD_DATE_TO_UNIX(*date);
520 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
522 bool xlate = (dateoff & AD_DATE_UNIX);
525 p = ad_get_entry(ad, ADEID_FILEDATESI);
530 dateoff &= AD_DATE_MASK;
532 date = AD_DATE_FROM_UNIX(date);
535 if (dateoff > AD_DATE_ACCESS) {
539 memcpy(p + dateoff, &date, sizeof(date));
546 * Map on-disk AppleDouble id to enumerated id
548 static uint32_t get_eid(uint32_t eid)
556 return ADEID_PRIVDEV;
558 return ADEID_PRIVINO;
560 return ADEID_PRIVSYN;
571 * Pack AppleDouble structure into data buffer
573 static bool ad_pack(struct adouble *ad)
580 bufsize = talloc_get_size(ad->ad_data);
581 if (bufsize < AD_DATASZ_DOT_UND) {
582 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
586 if (offset + ADEDLEN_MAGIC < offset ||
587 offset + ADEDLEN_MAGIC >= bufsize) {
590 RSIVAL(ad->ad_data, offset, ad->ad_magic);
591 offset += ADEDLEN_MAGIC;
593 if (offset + ADEDLEN_VERSION < offset ||
594 offset + ADEDLEN_VERSION >= bufsize) {
597 RSIVAL(ad->ad_data, offset, ad->ad_version);
598 offset += ADEDLEN_VERSION;
600 if (offset + ADEDLEN_FILLER < offset ||
601 offset + ADEDLEN_FILLER >= bufsize) {
604 if (ad->ad_type == ADOUBLE_RSRC) {
605 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
607 offset += ADEDLEN_FILLER;
609 if (offset + ADEDLEN_NENTRIES < offset ||
610 offset + ADEDLEN_NENTRIES >= bufsize) {
613 offset += ADEDLEN_NENTRIES;
615 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
616 if (ad->ad_eid[eid].ade_off == 0) {
618 * ade_off is also used as indicator whether a
619 * specific entry is used or not
624 if (offset + AD_ENTRY_LEN_EID < offset ||
625 offset + AD_ENTRY_LEN_EID >= bufsize) {
628 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
629 offset += AD_ENTRY_LEN_EID;
631 if (offset + AD_ENTRY_LEN_OFF < offset ||
632 offset + AD_ENTRY_LEN_OFF >= bufsize) {
635 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
636 offset += AD_ENTRY_LEN_OFF;
638 if (offset + AD_ENTRY_LEN_LEN < offset ||
639 offset + AD_ENTRY_LEN_LEN >= bufsize) {
642 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
643 offset += AD_ENTRY_LEN_LEN;
648 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
651 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
657 * Unpack an AppleDouble blob into a struct adoble
659 static bool ad_unpack(struct adouble *ad, const size_t nentries,
662 size_t bufsize = talloc_get_size(ad->ad_data);
664 uint32_t eid, len, off;
667 * The size of the buffer ad->ad_data is checked when read, so
668 * we wouldn't have to check our own offsets, a few extra
669 * checks won't hurt though. We have to check the offsets we
670 * read from the buffer anyway.
673 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
674 DEBUG(1, ("bad size\n"));
678 ad->ad_magic = RIVAL(ad->ad_data, 0);
679 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
680 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
681 DEBUG(1, ("wrong magic or version\n"));
685 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
686 if (adentries != nentries) {
687 DEBUG(1, ("invalid number of entries: %zu\n",
692 /* now, read in the entry bits */
693 for (i = 0; i < adentries; i++) {
694 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
696 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
697 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
699 if (!eid || eid >= ADEID_MAX) {
700 DEBUG(1, ("bogus eid %d\n", eid));
705 * All entries other than the resource fork are
706 * expected to be read into the ad_data buffer, so
707 * ensure the specified offset is within that bound
709 if ((off > bufsize) && (eid != ADEID_RFORK)) {
710 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
716 * All entries besides FinderInfo and resource fork
717 * must fit into the buffer. FinderInfo is special as
718 * it may be larger then the default 32 bytes (if it
719 * contains marshalled xattrs), but we will fixup that
720 * in ad_convert(). And the resource fork is never
721 * accessed directly by the ad_data buf (also see
722 * comment above) anyway.
724 if ((eid != ADEID_RFORK) &&
725 (eid != ADEID_FINDERI) &&
726 ((off + len) > bufsize)) {
727 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
733 * That would be obviously broken
735 if (off > filesize) {
736 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
742 * Check for any entry that has its end beyond the
745 if (off + len < off) {
746 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
747 ", len: %" PRIu32 "\n",
752 if (off + len > filesize) {
754 * If this is the resource fork entry, we fix
755 * up the length, for any other entry we bail
758 if (eid != ADEID_RFORK) {
759 DEBUG(1, ("bogus eid %d: off: %" PRIu32
760 ", len: %" PRIu32 "\n",
766 * Fixup the resource fork entry by limiting
767 * the size to entryoffset - filesize.
769 len = filesize - off;
770 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
771 ", len: %" PRIu32 "\n", off, len));
774 ad->ad_eid[eid].ade_off = off;
775 ad->ad_eid[eid].ade_len = len;
782 * Convert from Apple's ._ file to Netatalk
784 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
785 * bytes containing packed xattrs. Netatalk can't deal with that, so
786 * we simply discard the packed xattrs.
788 * @return -1 in case an error occurred, 0 if no conversion was done, 1
791 static int ad_convert(struct adouble *ad, int fd)
794 char *map = MAP_FAILED;
797 origlen = ad_getentryoff(ad, ADEID_RFORK) +
798 ad_getentrylen(ad, ADEID_RFORK);
800 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
801 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
802 if (map == MAP_FAILED) {
803 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
808 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
809 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
810 map + ad_getentryoff(ad, ADEID_RFORK),
811 ad_getentrylen(ad, ADEID_RFORK));
814 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
815 ad_setentryoff(ad, ADEID_RFORK,
816 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
819 * FIXME: direct ftruncate(), but we don't have a fsp for the
822 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
823 + ad_getentrylen(ad, ADEID_RFORK));
826 if (map != MAP_FAILED) {
827 munmap(map, origlen);
833 * Read and parse Netatalk AppleDouble metadata xattr
835 static ssize_t ad_read_meta(struct adouble *ad,
836 const struct smb_filename *smb_fname)
842 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
844 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
845 AFPINFO_EA_NETATALK, ad->ad_data,
851 if (errno == ENOATTR) {
857 DEBUG(2, ("error reading meta xattr: %s\n",
863 if (ealen != AD_DATASZ_XATTR) {
864 DEBUG(2, ("bad size %zd\n", ealen));
870 /* Now parse entries */
871 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
873 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
879 if (!ad_getentryoff(ad, ADEID_FINDERI)
880 || !ad_getentryoff(ad, ADEID_COMMENT)
881 || !ad_getentryoff(ad, ADEID_FILEDATESI)
882 || !ad_getentryoff(ad, ADEID_AFPFILEI)
883 || !ad_getentryoff(ad, ADEID_PRIVDEV)
884 || !ad_getentryoff(ad, ADEID_PRIVINO)
885 || !ad_getentryoff(ad, ADEID_PRIVSYN)
886 || !ad_getentryoff(ad, ADEID_PRIVID)) {
887 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
894 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
895 smb_fname->base_name, rc));
899 if (errno == EINVAL) {
901 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
909 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
914 /* FIXME: direct Solaris xattr syscall */
915 return attropen(smb_fname->base_name,
916 AFPRESOURCE_EA_NETATALK, flags, mode);
923 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
929 struct smb_filename *adp_smb_fname = NULL;
931 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
936 fd = open(adp_smb_fname->base_name, flags, mode);
937 TALLOC_FREE(adp_smb_fname);
942 static int ad_open_rsrc(vfs_handle_struct *handle,
943 const struct smb_filename *smb_fname,
947 struct fruit_config_data *config = NULL;
950 SMB_VFS_HANDLE_GET_DATA(handle, config,
951 struct fruit_config_data, return -1);
953 if (config->rsrc == FRUIT_RSRC_XATTR) {
954 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
956 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
963 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
964 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
965 * for file IO on the ._ file.
967 static int ad_open(vfs_handle_struct *handle,
970 const struct smb_filename *smb_fname,
976 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
977 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
979 if (ad->ad_type == ADOUBLE_META) {
983 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
984 ad->ad_fd = fsp->fh->fd;
985 ad->ad_opened = false;
989 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
993 ad->ad_opened = true;
996 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
997 smb_fname->base_name,
998 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1003 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1008 /* FIXME: direct sys_fstat(), don't have an fsp */
1009 ret = sys_fstat(ad->ad_fd, &st,
1010 lp_fake_directory_create_times(
1011 SNUM(ad->ad_handle->conn)));
1016 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1017 return st.st_ex_size;
1020 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1021 const struct smb_filename *smb_fname)
1023 struct adouble *meta_ad = NULL;
1024 SMB_STRUCT_STAT sbuf;
1026 char *p_meta_ad = NULL;
1032 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1033 SNUM(ad->ad_handle->conn)));
1039 * AppleDouble file header content and size, two cases:
1041 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1042 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1044 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1046 size = sbuf.st_ex_size;
1047 if (size > talloc_array_length(ad->ad_data)) {
1048 if (size > AD_XATTR_MAX_HDR_SIZE) {
1049 size = AD_XATTR_MAX_HDR_SIZE;
1051 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1058 len = sys_pread(ad->ad_fd, ad->ad_data,
1059 talloc_array_length(ad->ad_data), 0);
1060 if (len != talloc_array_length(ad->ad_data)) {
1061 DBG_NOTICE("%s %s: bad size: %zd\n",
1062 smb_fname->base_name, strerror(errno), len);
1066 /* Now parse entries */
1067 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1069 DBG_ERR("invalid AppleDouble resource %s\n",
1070 smb_fname->base_name);
1075 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1076 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1077 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1078 DBG_ERR("invalid AppleDouble resource %s\n",
1079 smb_fname->base_name);
1084 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1089 * Try to fixup AppleDouble files created by OS X with xattrs
1090 * appended to the ADEID_FINDERI entry. We simply remove the
1091 * xattrs blob, this means any fancy xattr that was stored
1095 ret = ad_convert(ad, ad->ad_fd);
1097 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1103 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1107 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1108 if (len != AD_DATASZ_DOT_UND) {
1109 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1113 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1114 if (meta_ad == NULL) {
1118 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1120 TALLOC_FREE(meta_ad);
1123 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1124 if (p_meta_ad == NULL) {
1125 TALLOC_FREE(meta_ad);
1129 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1131 ret = ad_set(meta_ad, smb_fname);
1132 TALLOC_FREE(meta_ad);
1141 * Read and parse resource fork, either ._ AppleDouble file or xattr
1143 static ssize_t ad_read_rsrc(struct adouble *ad,
1144 const struct smb_filename *smb_fname)
1146 struct fruit_config_data *config = NULL;
1149 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1150 struct fruit_config_data, return -1);
1152 if (config->rsrc == FRUIT_RSRC_XATTR) {
1153 len = ad_read_rsrc_xattr(ad);
1155 len = ad_read_rsrc_adouble(ad, smb_fname);
1162 * Read and unpack an AppleDouble metadata xattr or resource
1164 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1166 switch (ad->ad_type) {
1168 return ad_read_meta(ad, smb_fname);
1170 return ad_read_rsrc(ad, smb_fname);
1176 static int adouble_destructor(struct adouble *ad)
1178 if ((ad->ad_fd != -1) && ad->ad_opened) {
1186 * Allocate a struct adouble without initialiing it
1188 * The struct is either hang of the fsp extension context or if fsp is
1191 * @param[in] ctx talloc context
1192 * @param[in] handle vfs handle
1193 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1195 * @return adouble handle
1197 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1198 adouble_type_t type)
1203 struct fruit_config_data *config;
1205 SMB_VFS_HANDLE_GET_DATA(handle, config,
1206 struct fruit_config_data, return NULL);
1210 adsize = AD_DATASZ_XATTR;
1213 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1214 adsize = AD_DATASZ_DOT_UND;
1221 ad = talloc_zero(ctx, struct adouble);
1228 ad->ad_data = talloc_zero_array(ad, char, adsize);
1229 if (ad->ad_data == NULL) {
1235 ad->ad_handle = handle;
1237 ad->ad_magic = AD_MAGIC;
1238 ad->ad_version = AD_VERSION;
1241 talloc_set_destructor(ad, adouble_destructor);
1251 * Allocate and initialize a new struct adouble
1253 * @param[in] ctx talloc context
1254 * @param[in] handle vfs handle
1255 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1257 * @return adouble handle, initialized
1259 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1260 adouble_type_t type)
1263 const struct ad_entry_order *eid;
1264 struct adouble *ad = NULL;
1265 struct fruit_config_data *config;
1266 time_t t = time(NULL);
1268 SMB_VFS_HANDLE_GET_DATA(handle, config,
1269 struct fruit_config_data, return NULL);
1273 eid = entry_order_meta_xattr;
1276 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1277 eid = entry_order_dot_und;
1279 eid = entry_order_rsrc_xattr;
1286 ad = ad_alloc(ctx, handle, type);
1292 ad->ad_eid[eid->id].ade_off = eid->offset;
1293 ad->ad_eid[eid->id].ade_len = eid->len;
1297 /* put something sane in the date fields */
1298 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1299 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1300 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1301 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1309 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1310 vfs_handle_struct *handle,
1312 const struct smb_filename *smb_fname,
1313 adouble_type_t type)
1317 struct adouble *ad = NULL;
1321 smb_fname = fsp->base_fsp->fsp_name;
1324 DEBUG(10, ("ad_get(%s) called for %s\n",
1325 type == ADOUBLE_META ? "meta" : "rsrc",
1326 smb_fname->base_name));
1328 ad = ad_alloc(ctx, handle, type);
1334 /* Try rw first so we can use the fd in ad_convert() */
1337 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1338 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1340 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1343 DBG_DEBUG("ad_open [%s] error [%s]\n",
1344 smb_fname->base_name, strerror(errno));
1349 len = ad_read(ad, smb_fname);
1351 DEBUG(10, ("error reading AppleDouble for %s\n",
1352 smb_fname->base_name));
1358 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1359 type == ADOUBLE_META ? "meta" : "rsrc",
1360 smb_fname->base_name, rc));
1369 * Return AppleDouble data for a file
1371 * @param[in] ctx talloc context
1372 * @param[in] handle vfs handle
1373 * @param[in] smb_fname pathname to file or directory
1374 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1376 * @return talloced struct adouble or NULL on error
1378 static struct adouble *ad_get(TALLOC_CTX *ctx,
1379 vfs_handle_struct *handle,
1380 const struct smb_filename *smb_fname,
1381 adouble_type_t type)
1383 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1387 * Return AppleDouble data for a file
1389 * @param[in] ctx talloc context
1390 * @param[in] handle vfs handle
1391 * @param[in] fsp fsp to use for IO
1392 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1394 * @return talloced struct adouble or NULL on error
1396 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1397 files_struct *fsp, adouble_type_t type)
1399 return ad_get_internal(ctx, handle, fsp, NULL, type);
1403 * Set AppleDouble metadata on a file or directory
1405 * @param[in] ad adouble handle
1407 * @param[in] smb_fname pathname to file or directory
1409 * @return status code, 0 means success
1411 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1416 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1418 if (ad->ad_type != ADOUBLE_META) {
1419 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1420 smb_fname->base_name);
1429 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1431 AFPINFO_EA_NETATALK,
1433 AD_DATASZ_XATTR, 0);
1435 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1441 * Set AppleDouble metadata on a file or directory
1443 * @param[in] ad adouble handle
1444 * @param[in] fsp file handle
1446 * @return status code, 0 means success
1448 static int ad_fset(struct adouble *ad, files_struct *fsp)
1454 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1457 || (fsp->fh == NULL)
1458 || (fsp->fh->fd == -1))
1460 smb_panic("bad fsp");
1468 switch (ad->ad_type) {
1470 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1472 AFPINFO_EA_NETATALK,
1474 AD_DATASZ_XATTR, 0);
1478 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1483 if (len != AD_DATASZ_DOT_UND) {
1484 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1494 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1499 /*****************************************************************************
1501 *****************************************************************************/
1503 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1505 if (strncasecmp_m(smb_fname->stream_name,
1506 AFPINFO_STREAM_NAME,
1507 strlen(AFPINFO_STREAM_NAME)) == 0) {
1513 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1515 if (strncasecmp_m(smb_fname->stream_name,
1516 AFPRESOURCE_STREAM_NAME,
1517 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1524 * Test whether stream is an Apple stream, not used atm
1527 static bool is_apple_stream(const struct smb_filename *smb_fname)
1529 if (is_afpinfo_stream(smb_fname)) {
1532 if (is_afpresource_stream(smb_fname)) {
1540 * Initialize config struct from our smb.conf config parameters
1542 static int init_fruit_config(vfs_handle_struct *handle)
1544 struct fruit_config_data *config;
1547 config = talloc_zero(handle->conn, struct fruit_config_data);
1549 DEBUG(1, ("talloc_zero() failed\n"));
1555 * Versions up to Samba 4.5.x had a spelling bug in the
1556 * fruit:resource option calling lp_parm_enum with
1557 * "res*s*ource" (ie two s).
1559 * In Samba 4.6 we accept both the wrong and the correct
1560 * spelling, in Samba 4.7 the bad spelling will be removed.
1562 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1563 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1564 if (enumval == -1) {
1565 DEBUG(1, ("value for %s: resource type unknown\n",
1566 FRUIT_PARAM_TYPE_NAME));
1569 config->rsrc = (enum fruit_rsrc)enumval;
1571 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1572 "resource", fruit_rsrc, enumval);
1573 if (enumval == -1) {
1574 DEBUG(1, ("value for %s: resource type unknown\n",
1575 FRUIT_PARAM_TYPE_NAME));
1578 config->rsrc = (enum fruit_rsrc)enumval;
1580 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1581 "metadata", fruit_meta, FRUIT_META_NETATALK);
1582 if (enumval == -1) {
1583 DEBUG(1, ("value for %s: metadata type unknown\n",
1584 FRUIT_PARAM_TYPE_NAME));
1587 config->meta = (enum fruit_meta)enumval;
1589 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1590 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1591 if (enumval == -1) {
1592 DEBUG(1, ("value for %s: locking type unknown\n",
1593 FRUIT_PARAM_TYPE_NAME));
1596 config->locking = (enum fruit_locking)enumval;
1598 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1599 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1600 if (enumval == -1) {
1601 DEBUG(1, ("value for %s: encoding type unknown\n",
1602 FRUIT_PARAM_TYPE_NAME));
1605 config->encoding = (enum fruit_encoding)enumval;
1607 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1608 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1609 FRUIT_PARAM_TYPE_NAME,
1614 config->use_aapl = lp_parm_bool(
1615 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1617 config->time_machine = lp_parm_bool(
1618 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
1620 config->unix_info_enabled = lp_parm_bool(
1621 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1623 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1626 config->posix_rename = lp_parm_bool(
1627 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1629 config->aapl_zero_file_id =
1630 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1632 config->readdir_attr_rsize = lp_parm_bool(
1633 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1635 config->readdir_attr_finder_info = lp_parm_bool(
1636 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1638 config->readdir_attr_max_access = lp_parm_bool(
1639 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1641 config->model = lp_parm_const_string(
1642 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1644 SMB_VFS_HANDLE_SET_DATA(handle, config,
1645 NULL, struct fruit_config_data,
1652 * Prepend "._" to a basename
1653 * Return a new struct smb_filename with stream_name == NULL.
1655 static int adouble_path(TALLOC_CTX *ctx,
1656 const struct smb_filename *smb_fname_in,
1657 struct smb_filename **pp_smb_fname_out)
1661 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1664 if (smb_fname == NULL) {
1668 /* We need streamname to be NULL */
1669 TALLOC_FREE(smb_fname->stream_name);
1671 /* And we're replacing base_name. */
1672 TALLOC_FREE(smb_fname->base_name);
1674 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
1676 TALLOC_FREE(smb_fname);
1680 smb_fname->base_name = talloc_asprintf(smb_fname,
1681 "%s/._%s", parent, base);
1682 if (smb_fname->base_name == NULL) {
1683 TALLOC_FREE(smb_fname);
1687 *pp_smb_fname_out = smb_fname;
1693 * Allocate and initialize an AfpInfo struct
1695 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1697 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1701 ai->afpi_Signature = AFP_Signature;
1702 ai->afpi_Version = AFP_Version;
1703 ai->afpi_BackupTime = AD_DATE_START;
1708 * Pack an AfpInfo struct into a buffer
1710 * Buffer size must be at least AFP_INFO_SIZE
1711 * Returns size of packed buffer
1713 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1715 memset(buf, 0, AFP_INFO_SIZE);
1717 RSIVAL(buf, 0, ai->afpi_Signature);
1718 RSIVAL(buf, 4, ai->afpi_Version);
1719 RSIVAL(buf, 12, ai->afpi_BackupTime);
1720 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1722 return AFP_INFO_SIZE;
1726 * Unpack a buffer into a AfpInfo structure
1728 * Buffer size must be at least AFP_INFO_SIZE
1729 * Returns allocated AfpInfo struct
1731 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1733 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1738 ai->afpi_Signature = RIVAL(data, 0);
1739 ai->afpi_Version = RIVAL(data, 4);
1740 ai->afpi_BackupTime = RIVAL(data, 12);
1741 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1742 sizeof(ai->afpi_FinderInfo));
1744 if (ai->afpi_Signature != AFP_Signature
1745 || ai->afpi_Version != AFP_Version) {
1746 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1754 * Fake an inode number from the md5 hash of the (xattr) name
1756 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1759 unsigned char hash[16];
1763 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1764 SMB_ASSERT(upper_sname != NULL);
1767 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1768 sizeof(sbuf->st_ex_dev));
1769 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1770 sizeof(sbuf->st_ex_ino));
1771 MD5Update(&ctx, (unsigned char *)upper_sname,
1772 talloc_get_size(upper_sname)-1);
1773 MD5Final(hash, &ctx);
1775 TALLOC_FREE(upper_sname);
1777 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1778 memcpy(&result, hash, sizeof(result));
1780 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1781 sname, (unsigned long long)result));
1786 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1787 struct stream_struct **streams,
1788 const char *name, off_t size,
1791 struct stream_struct *tmp;
1793 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1799 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1800 if (tmp[*num_streams].name == NULL) {
1804 tmp[*num_streams].size = size;
1805 tmp[*num_streams].alloc_size = alloc_size;
1812 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1813 struct stream_struct **streams)
1815 struct stream_struct *tmp = *streams;
1818 if (*num_streams == 0) {
1822 for (i = 0; i < *num_streams; i++) {
1823 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1828 if (i == *num_streams) {
1832 if (tmp[i].size > 0) {
1836 TALLOC_FREE(tmp[i].name);
1837 if (*num_streams - 1 > i) {
1838 memmove(&tmp[i], &tmp[i+1],
1839 (*num_streams - i - 1) * sizeof(struct stream_struct));
1846 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1847 struct stream_struct **streams,
1850 struct stream_struct *tmp = *streams;
1853 if (*num_streams == 0) {
1857 for (i = 0; i < *num_streams; i++) {
1858 if (strequal_m(tmp[i].name, name)) {
1863 if (i == *num_streams) {
1867 TALLOC_FREE(tmp[i].name);
1868 if (*num_streams - 1 > i) {
1869 memmove(&tmp[i], &tmp[i+1],
1870 (*num_streams - i - 1) * sizeof(struct stream_struct));
1877 static bool ad_empty_finderinfo(const struct adouble *ad)
1880 char emptybuf[ADEDLEN_FINDERI] = {0};
1883 fi = ad_get_entry(ad, ADEID_FINDERI);
1885 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1889 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1893 static bool ai_empty_finderinfo(const AfpInfo *ai)
1896 char emptybuf[ADEDLEN_FINDERI] = {0};
1898 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1903 * Update btime with btime from Netatalk
1905 static void update_btime(vfs_handle_struct *handle,
1906 struct smb_filename *smb_fname)
1909 struct timespec creation_time = {0};
1911 struct fruit_config_data *config = NULL;
1913 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1916 switch (config->meta) {
1917 case FRUIT_META_STREAM:
1919 case FRUIT_META_NETATALK:
1923 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1927 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1931 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1937 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1938 update_stat_ex_create_time(&smb_fname->st, creation_time);
1944 * Map an access mask to a Netatalk single byte byte range lock
1946 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1947 uint32_t access_mask)
1951 switch (access_mask) {
1952 case FILE_READ_DATA:
1953 offset = AD_FILELOCK_OPEN_RD;
1956 case FILE_WRITE_DATA:
1957 case FILE_APPEND_DATA:
1958 offset = AD_FILELOCK_OPEN_WR;
1962 offset = AD_FILELOCK_OPEN_NONE;
1966 if (fork_type == APPLE_FORK_RSRC) {
1967 if (offset == AD_FILELOCK_OPEN_NONE) {
1968 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1978 * Map a deny mode to a Netatalk brl
1980 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1985 switch (deny_mode) {
1987 offset = AD_FILELOCK_DENY_RD;
1991 offset = AD_FILELOCK_DENY_WR;
1995 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1998 if (fork_type == APPLE_FORK_RSRC) {
2006 * Call fcntl() with an exclusive F_GETLK request in order to
2007 * determine if there's an exisiting shared lock
2009 * @return true if the requested lock was found or any error occurred
2010 * false if the lock was not found
2012 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2015 off_t offset = in_offset;
2020 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2021 if (result == false) {
2025 if (type != F_UNLCK) {
2032 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2034 uint32_t access_mask,
2037 NTSTATUS status = NT_STATUS_OK;
2038 struct byte_range_lock *br_lck = NULL;
2039 bool open_for_reading, open_for_writing, deny_read, deny_write;
2041 bool have_read = false;
2044 /* FIXME: hardcoded data fork, add resource fork */
2045 enum apple_fork fork_type = APPLE_FORK_DATA;
2047 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2049 access_mask & FILE_READ_DATA ? "READ" :"-",
2050 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2051 deny_mode & DENY_READ ? "DENY_READ" : "-",
2052 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2054 if (fsp->fh->fd == -1) {
2055 return NT_STATUS_OK;
2058 flags = fcntl(fsp->fh->fd, F_GETFL);
2060 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2061 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2062 return map_nt_error_from_unix(errno);
2065 if (flags & (O_RDONLY|O_RDWR)) {
2067 * Applying fcntl read locks requires an fd opened for
2068 * reading. This means we won't be applying locks for
2069 * files openend write-only, but what can we do...
2075 * Check read access and deny read mode
2077 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2079 open_for_reading = test_netatalk_lock(
2080 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2082 deny_read = test_netatalk_lock(
2083 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2085 DEBUG(10, ("read: %s, deny_write: %s\n",
2086 open_for_reading == true ? "yes" : "no",
2087 deny_read == true ? "yes" : "no"));
2089 if (((access_mask & FILE_READ_DATA) && deny_read)
2090 || ((deny_mode & DENY_READ) && open_for_reading)) {
2091 return NT_STATUS_SHARING_VIOLATION;
2095 if ((access_mask & FILE_READ_DATA) && have_read) {
2096 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2098 handle->conn->sconn->msg_ctx, fsp,
2099 fsp->op->global->open_persistent_id, 1, off,
2100 READ_LOCK, POSIX_LOCK, false,
2103 if (!NT_STATUS_IS_OK(status)) {
2106 TALLOC_FREE(br_lck);
2109 if ((deny_mode & DENY_READ) && have_read) {
2110 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2112 handle->conn->sconn->msg_ctx, fsp,
2113 fsp->op->global->open_persistent_id, 1, off,
2114 READ_LOCK, POSIX_LOCK, false,
2117 if (!NT_STATUS_IS_OK(status)) {
2120 TALLOC_FREE(br_lck);
2125 * Check write access and deny write mode
2127 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2129 open_for_writing = test_netatalk_lock(
2130 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2132 deny_write = test_netatalk_lock(
2133 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2135 DEBUG(10, ("write: %s, deny_write: %s\n",
2136 open_for_writing == true ? "yes" : "no",
2137 deny_write == true ? "yes" : "no"));
2139 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2140 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2141 return NT_STATUS_SHARING_VIOLATION;
2145 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2146 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2148 handle->conn->sconn->msg_ctx, fsp,
2149 fsp->op->global->open_persistent_id, 1, off,
2150 READ_LOCK, POSIX_LOCK, false,
2153 if (!NT_STATUS_IS_OK(status)) {
2156 TALLOC_FREE(br_lck);
2159 if ((deny_mode & DENY_WRITE) && have_read) {
2160 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2162 handle->conn->sconn->msg_ctx, fsp,
2163 fsp->op->global->open_persistent_id, 1, off,
2164 READ_LOCK, POSIX_LOCK, false,
2167 if (!NT_STATUS_IS_OK(status)) {
2170 TALLOC_FREE(br_lck);
2174 TALLOC_FREE(br_lck);
2179 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2180 struct smb_request *req,
2181 const struct smb2_create_blobs *in_context_blobs,
2182 struct smb2_create_blobs *out_context_blobs)
2184 struct fruit_config_data *config;
2186 struct smb2_create_blob *aapl = NULL;
2190 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2191 uint64_t req_bitmap, client_caps;
2192 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2196 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2197 return NT_STATUS_UNSUCCESSFUL);
2199 if (!config->use_aapl
2200 || in_context_blobs == NULL
2201 || out_context_blobs == NULL) {
2202 return NT_STATUS_OK;
2205 aapl = smb2_create_blob_find(in_context_blobs,
2206 SMB2_CREATE_TAG_AAPL);
2208 return NT_STATUS_OK;
2211 if (aapl->data.length != 24) {
2212 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2213 (uintmax_t)aapl->data.length));
2214 return NT_STATUS_INVALID_PARAMETER;
2217 cmd = IVAL(aapl->data.data, 0);
2218 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2219 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2220 return NT_STATUS_INVALID_PARAMETER;
2223 req_bitmap = BVAL(aapl->data.data, 8);
2224 client_caps = BVAL(aapl->data.data, 16);
2226 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2228 SBVAL(p, 8, req_bitmap);
2229 ok = data_blob_append(req, &blob, p, 16);
2231 return NT_STATUS_UNSUCCESSFUL;
2234 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2235 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2236 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2237 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2238 config->readdir_attr_enabled = true;
2241 if (config->use_copyfile) {
2242 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2243 config->copyfile_enabled = true;
2247 * The client doesn't set the flag, so we can't check
2248 * for it and just set it unconditionally
2250 if (config->unix_info_enabled) {
2251 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2254 SBVAL(p, 0, server_caps);
2255 ok = data_blob_append(req, &blob, p, 8);
2257 return NT_STATUS_UNSUCCESSFUL;
2261 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2262 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2270 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2277 if (config->time_machine) {
2278 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2283 ok = data_blob_append(req, &blob, p, 8);
2285 return NT_STATUS_UNSUCCESSFUL;
2289 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2290 ok = convert_string_talloc(req,
2291 CH_UNIX, CH_UTF16LE,
2292 config->model, strlen(config->model),
2295 return NT_STATUS_UNSUCCESSFUL;
2299 SIVAL(p + 4, 0, modellen);
2300 ok = data_blob_append(req, &blob, p, 8);
2303 return NT_STATUS_UNSUCCESSFUL;
2306 ok = data_blob_append(req, &blob, model, modellen);
2309 return NT_STATUS_UNSUCCESSFUL;
2313 status = smb2_create_blob_add(out_context_blobs,
2315 SMB2_CREATE_TAG_AAPL,
2317 if (NT_STATUS_IS_OK(status)) {
2318 global_fruit_config.nego_aapl = true;
2319 if (config->aapl_zero_file_id) {
2320 aapl_force_zero_file_id(handle->conn->sconn);
2327 static bool readdir_attr_meta_finderi_stream(
2328 struct vfs_handle_struct *handle,
2329 const struct smb_filename *smb_fname,
2332 struct smb_filename *stream_name = NULL;
2333 files_struct *fsp = NULL;
2338 uint8_t buf[AFP_INFO_SIZE];
2340 stream_name = synthetic_smb_fname(talloc_tos(),
2341 smb_fname->base_name,
2342 AFPINFO_STREAM_NAME,
2343 NULL, smb_fname->flags);
2344 if (stream_name == NULL) {
2348 ret = SMB_VFS_STAT(handle->conn, stream_name);
2353 status = SMB_VFS_CREATE_FILE(
2354 handle->conn, /* conn */
2356 0, /* root_dir_fid */
2357 stream_name, /* fname */
2358 FILE_READ_DATA, /* access_mask */
2359 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2361 FILE_OPEN, /* create_disposition*/
2362 0, /* create_options */
2363 0, /* file_attributes */
2364 INTERNAL_OPEN_ONLY, /* oplock_request */
2366 0, /* allocation_size */
2367 0, /* private_flags */
2372 NULL, NULL); /* create context */
2374 TALLOC_FREE(stream_name);
2376 if (!NT_STATUS_IS_OK(status)) {
2380 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2381 if (nread != AFP_INFO_SIZE) {
2382 DBG_ERR("short read [%s] [%zd/%d]\n",
2383 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2388 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2395 close_file(NULL, fsp, NORMAL_CLOSE);
2401 static bool readdir_attr_meta_finderi_netatalk(
2402 struct vfs_handle_struct *handle,
2403 const struct smb_filename *smb_fname,
2406 struct adouble *ad = NULL;
2409 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2414 p = ad_get_entry(ad, ADEID_FINDERI);
2416 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2421 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2426 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2427 const struct smb_filename *smb_fname,
2428 struct readdir_attr_data *attr_data)
2430 struct fruit_config_data *config = NULL;
2431 uint32_t date_added;
2435 SMB_VFS_HANDLE_GET_DATA(handle, config,
2436 struct fruit_config_data,
2439 switch (config->meta) {
2440 case FRUIT_META_NETATALK:
2441 ok = readdir_attr_meta_finderi_netatalk(
2442 handle, smb_fname, &ai);
2445 case FRUIT_META_STREAM:
2446 ok = readdir_attr_meta_finderi_stream(
2447 handle, smb_fname, &ai);
2451 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2456 /* Don't bother with errors, it's likely ENOENT */
2460 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2462 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2463 &ai.afpi_FinderInfo[0], 4);
2465 /* finder_creator */
2466 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2467 &ai.afpi_FinderInfo[4], 4);
2471 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2472 &ai.afpi_FinderInfo[8], 2);
2474 /* finder_ext_flags */
2475 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2476 &ai.afpi_FinderInfo[24], 2);
2479 date_added = convert_time_t_to_uint32_t(
2480 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2482 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2487 static uint64_t readdir_attr_rfork_size_adouble(
2488 struct vfs_handle_struct *handle,
2489 const struct smb_filename *smb_fname)
2491 struct adouble *ad = NULL;
2492 uint64_t rfork_size;
2494 ad = ad_get(talloc_tos(), handle, smb_fname,
2500 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2506 static uint64_t readdir_attr_rfork_size_stream(
2507 struct vfs_handle_struct *handle,
2508 const struct smb_filename *smb_fname)
2510 struct smb_filename *stream_name = NULL;
2512 uint64_t rfork_size;
2514 stream_name = synthetic_smb_fname(talloc_tos(),
2515 smb_fname->base_name,
2516 AFPRESOURCE_STREAM_NAME,
2518 if (stream_name == NULL) {
2522 ret = SMB_VFS_STAT(handle->conn, stream_name);
2524 TALLOC_FREE(stream_name);
2528 rfork_size = stream_name->st.st_ex_size;
2529 TALLOC_FREE(stream_name);
2534 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2535 const struct smb_filename *smb_fname)
2537 struct fruit_config_data *config = NULL;
2538 uint64_t rfork_size;
2540 SMB_VFS_HANDLE_GET_DATA(handle, config,
2541 struct fruit_config_data,
2544 switch (config->rsrc) {
2545 case FRUIT_RSRC_ADFILE:
2546 case FRUIT_RSRC_XATTR:
2547 rfork_size = readdir_attr_rfork_size_adouble(handle,
2551 case FRUIT_META_STREAM:
2552 rfork_size = readdir_attr_rfork_size_stream(handle,
2557 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2565 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2566 const struct smb_filename *smb_fname,
2567 struct readdir_attr_data *attr_data)
2569 NTSTATUS status = NT_STATUS_OK;
2570 struct fruit_config_data *config = NULL;
2573 SMB_VFS_HANDLE_GET_DATA(handle, config,
2574 struct fruit_config_data,
2575 return NT_STATUS_UNSUCCESSFUL);
2578 /* Ensure we return a default value in the creation_date field */
2579 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2582 * Resource fork length
2585 if (config->readdir_attr_rsize) {
2586 uint64_t rfork_size;
2588 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2589 attr_data->attr_data.aapl.rfork_size = rfork_size;
2596 if (config->readdir_attr_finder_info) {
2597 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2599 status = NT_STATUS_INTERNAL_ERROR;
2606 /* Search MS NFS style ACE with UNIX mode */
2607 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2609 const struct security_descriptor *psd,
2614 struct fruit_config_data *config = NULL;
2618 SMB_VFS_HANDLE_GET_DATA(handle, config,
2619 struct fruit_config_data,
2620 return NT_STATUS_UNSUCCESSFUL);
2622 if (!global_fruit_config.nego_aapl) {
2623 return NT_STATUS_OK;
2625 if (psd->dacl == NULL || !config->unix_info_enabled) {
2626 return NT_STATUS_OK;
2629 for (i = 0; i < psd->dacl->num_aces; i++) {
2630 if (dom_sid_compare_domain(
2631 &global_sid_Unix_NFS_Mode,
2632 &psd->dacl->aces[i].trustee) == 0) {
2633 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2634 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2637 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2638 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2643 return NT_STATUS_OK;
2646 /****************************************************************************
2648 ****************************************************************************/
2650 static int fruit_connect(vfs_handle_struct *handle,
2651 const char *service,
2655 char *list = NULL, *newlist = NULL;
2656 struct fruit_config_data *config;
2658 DEBUG(10, ("fruit_connect\n"));
2660 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2665 rc = init_fruit_config(handle);
2670 SMB_VFS_HANDLE_GET_DATA(handle, config,
2671 struct fruit_config_data, return -1);
2673 if (config->veto_appledouble) {
2674 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2677 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2678 newlist = talloc_asprintf(
2680 "%s/" ADOUBLE_NAME_PREFIX "*/",
2682 lp_do_parameter(SNUM(handle->conn),
2687 lp_do_parameter(SNUM(handle->conn),
2689 "/" ADOUBLE_NAME_PREFIX "*/");
2695 if (config->encoding == FRUIT_ENC_NATIVE) {
2699 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2700 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2701 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2702 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2703 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2704 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2705 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2706 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2707 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2708 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2712 if (config->time_machine) {
2713 DBG_NOTICE("Enabling durable handles for Time Machine "
2714 "support on [%s]\n", service);
2715 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
2716 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
2717 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
2718 if (!lp_strict_sync(SNUM(handle->conn))) {
2719 DBG_WARNING("Time Machine without strict sync is not "
2722 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
2728 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2729 struct smb_filename *smb_fname,
2735 char afpinfo_buf[AFP_INFO_SIZE];
2736 ssize_t len, written;
2740 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2745 if (!(flags & (O_CREAT | O_TRUNC))) {
2749 ai = afpinfo_new(talloc_tos());
2755 len = afpinfo_pack(ai, afpinfo_buf);
2756 if (len != AFP_INFO_SIZE) {
2761 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2762 fsp->fh->fd = hostfd;
2764 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2767 if (written != AFP_INFO_SIZE) {
2768 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2776 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2779 int saved_errno = errno;
2781 fsp->fh->fd = hostfd;
2782 SMB_VFS_NEXT_CLOSE(handle, fsp);
2785 errno = saved_errno;
2790 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2791 struct smb_filename *smb_fname,
2798 struct adouble *ad = NULL;
2801 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2804 * Return a valid fd, but ensure any attempt to use it returns an error
2805 * (EPIPE). All operations on the smb_fname or the fsp will use path
2815 if (flags & (O_CREAT | O_TRUNC)) {
2817 * The attribute does not exist or needs to be truncated,
2818 * create an AppleDouble EA
2820 ad = ad_init(fsp, handle, ADOUBLE_META);
2826 rc = ad_set(ad, fsp->fsp_name);
2836 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
2838 int saved_errno = errno;
2843 errno = saved_errno;
2848 static int fruit_open_meta(vfs_handle_struct *handle,
2849 struct smb_filename *smb_fname,
2850 files_struct *fsp, int flags, mode_t mode)
2853 struct fruit_config_data *config = NULL;
2854 struct fio *fio = NULL;
2856 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2858 SMB_VFS_HANDLE_GET_DATA(handle, config,
2859 struct fruit_config_data, return -1);
2861 switch (config->meta) {
2862 case FRUIT_META_STREAM:
2863 fd = fruit_open_meta_stream(handle, smb_fname,
2867 case FRUIT_META_NETATALK:
2868 fd = fruit_open_meta_netatalk(handle, smb_fname,
2873 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2877 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2883 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
2884 fio->type = ADOUBLE_META;
2885 fio->config = config;
2890 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2891 struct smb_filename *smb_fname,
2897 struct adouble *ad = NULL;
2898 struct smb_filename *smb_fname_base = NULL;
2899 struct fruit_config_data *config = NULL;
2902 SMB_VFS_HANDLE_GET_DATA(handle, config,
2903 struct fruit_config_data, return -1);
2905 if ((!(flags & O_CREAT)) &&
2906 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
2908 /* sorry, but directories don't habe a resource fork */
2913 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
2918 /* Sanitize flags */
2919 if (flags & O_WRONLY) {
2920 /* We always need read access for the metadata header too */
2925 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2932 if (flags & (O_CREAT | O_TRUNC)) {
2933 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
2939 fsp->fh->fd = hostfd;
2941 rc = ad_fset(ad, fsp);
2952 TALLOC_FREE(smb_fname_base);
2954 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2956 int saved_errno = errno;
2959 * BUGBUGBUG -- we would need to call
2960 * fd_close_posix here, but we don't have a
2963 fsp->fh->fd = hostfd;
2967 errno = saved_errno;
2972 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2973 struct smb_filename *smb_fname,
2978 #ifdef HAVE_ATTROPEN
2981 fd = attropen(smb_fname->base_name,
2982 AFPRESOURCE_EA_NETATALK,
2997 static int fruit_open_rsrc(vfs_handle_struct *handle,
2998 struct smb_filename *smb_fname,
2999 files_struct *fsp, int flags, mode_t mode)
3002 struct fruit_config_data *config = NULL;
3003 struct fio *fio = NULL;
3005 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3007 SMB_VFS_HANDLE_GET_DATA(handle, config,
3008 struct fruit_config_data, return -1);
3010 if (((flags & O_ACCMODE) == O_RDONLY)
3011 && (flags & O_CREAT)
3012 && !VALID_STAT(fsp->fsp_name->st))
3015 * This means the stream doesn't exist. macOS SMB server fails
3016 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3017 * 12565 and the test for this combination in
3018 * test_rfork_create().
3024 switch (config->rsrc) {
3025 case FRUIT_RSRC_STREAM:
3026 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3029 case FRUIT_RSRC_ADFILE:
3030 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3034 case FRUIT_RSRC_XATTR:
3035 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3040 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3044 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3050 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3051 fio->type = ADOUBLE_RSRC;
3052 fio->config = config;
3057 static int fruit_open(vfs_handle_struct *handle,
3058 struct smb_filename *smb_fname,
3059 files_struct *fsp, int flags, mode_t mode)
3063 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3065 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3066 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3069 if (is_afpinfo_stream(smb_fname)) {
3070 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3071 } else if (is_afpresource_stream(smb_fname)) {
3072 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3074 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3077 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3082 static int fruit_rename(struct vfs_handle_struct *handle,
3083 const struct smb_filename *smb_fname_src,
3084 const struct smb_filename *smb_fname_dst)
3087 struct fruit_config_data *config = NULL;
3088 struct smb_filename *src_adp_smb_fname = NULL;
3089 struct smb_filename *dst_adp_smb_fname = NULL;
3091 SMB_VFS_HANDLE_GET_DATA(handle, config,
3092 struct fruit_config_data, return -1);
3094 if (!VALID_STAT(smb_fname_src->st)) {
3095 DBG_ERR("Need valid stat for [%s]\n",
3096 smb_fname_str_dbg(smb_fname_src));
3100 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3105 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3106 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3111 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3116 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3121 DBG_DEBUG("%s -> %s\n",
3122 smb_fname_str_dbg(src_adp_smb_fname),
3123 smb_fname_str_dbg(dst_adp_smb_fname));
3125 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3126 if (errno == ENOENT) {
3131 TALLOC_FREE(src_adp_smb_fname);
3132 TALLOC_FREE(dst_adp_smb_fname);
3136 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3137 const struct smb_filename *smb_fname)
3139 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3142 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3143 const struct smb_filename *smb_fname)
3145 return SMB_VFS_REMOVEXATTR(handle->conn,
3147 AFPINFO_EA_NETATALK);
3150 static int fruit_unlink_meta(vfs_handle_struct *handle,
3151 const struct smb_filename *smb_fname)
3153 struct fruit_config_data *config = NULL;
3156 SMB_VFS_HANDLE_GET_DATA(handle, config,
3157 struct fruit_config_data, return -1);
3159 switch (config->meta) {
3160 case FRUIT_META_STREAM:
3161 rc = fruit_unlink_meta_stream(handle, smb_fname);
3164 case FRUIT_META_NETATALK:
3165 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3169 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3176 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3177 const struct smb_filename *smb_fname,
3182 if (!force_unlink) {
3183 struct smb_filename *smb_fname_cp = NULL;
3186 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3187 if (smb_fname_cp == NULL) {
3192 * 0 byte resource fork streams are not listed by
3193 * vfs_streaminfo, as a result stream cleanup/deletion of file
3194 * deletion doesn't remove the resourcefork stream.
3197 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3199 TALLOC_FREE(smb_fname_cp);
3200 DBG_ERR("stat [%s] failed [%s]\n",
3201 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3205 size = smb_fname_cp->st.st_ex_size;
3206 TALLOC_FREE(smb_fname_cp);
3209 /* OS X ignores resource fork stream delete requests */
3214 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3215 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3222 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3223 const struct smb_filename *smb_fname,
3227 struct adouble *ad = NULL;
3228 struct smb_filename *adp_smb_fname = NULL;
3230 if (!force_unlink) {
3231 ad = ad_get(talloc_tos(), handle, smb_fname,
3240 * 0 byte resource fork streams are not listed by
3241 * vfs_streaminfo, as a result stream cleanup/deletion of file
3242 * deletion doesn't remove the resourcefork stream.
3245 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3246 /* OS X ignores resource fork stream delete requests */
3254 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3259 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3260 TALLOC_FREE(adp_smb_fname);
3261 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3268 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3269 const struct smb_filename *smb_fname,
3273 * OS X ignores resource fork stream delete requests, so nothing to do
3274 * here. Removing the file will remove the xattr anyway, so we don't
3275 * have to take care of removing 0 byte resource forks that could be
3281 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3282 const struct smb_filename *smb_fname,
3285 struct fruit_config_data *config = NULL;
3288 SMB_VFS_HANDLE_GET_DATA(handle, config,
3289 struct fruit_config_data, return -1);
3291 switch (config->rsrc) {
3292 case FRUIT_RSRC_STREAM:
3293 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3296 case FRUIT_RSRC_ADFILE:
3297 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3300 case FRUIT_RSRC_XATTR:
3301 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3305 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3312 static int fruit_unlink(vfs_handle_struct *handle,
3313 const struct smb_filename *smb_fname)
3316 struct fruit_config_data *config = NULL;
3317 struct smb_filename *rsrc_smb_fname = NULL;
3319 SMB_VFS_HANDLE_GET_DATA(handle, config,
3320 struct fruit_config_data, return -1);
3322 if (is_afpinfo_stream(smb_fname)) {
3323 return fruit_unlink_meta(handle, smb_fname);
3324 } else if (is_afpresource_stream(smb_fname)) {
3325 return fruit_unlink_rsrc(handle, smb_fname, false);
3326 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3327 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3331 * A request to delete the base file. Because 0 byte resource
3332 * fork streams are not listed by fruit_streaminfo,
3333 * delete_all_streams() can't remove 0 byte resource fork
3334 * streams, so we have to cleanup this here.
3336 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3337 smb_fname->base_name,
3338 AFPRESOURCE_STREAM_NAME,
3341 if (rsrc_smb_fname == NULL) {
3345 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3346 if ((rc != 0) && (errno != ENOENT)) {
3347 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3348 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3349 TALLOC_FREE(rsrc_smb_fname);
3352 TALLOC_FREE(rsrc_smb_fname);
3354 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3357 static int fruit_chmod(vfs_handle_struct *handle,
3358 const struct smb_filename *smb_fname,
3362 struct fruit_config_data *config = NULL;
3363 struct smb_filename *smb_fname_adp = NULL;
3365 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3370 SMB_VFS_HANDLE_GET_DATA(handle, config,
3371 struct fruit_config_data, return -1);
3373 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3377 if (!VALID_STAT(smb_fname->st)) {
3381 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3385 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3390 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3392 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3393 if (errno == ENOENT) {
3397 TALLOC_FREE(smb_fname_adp);
3401 static int fruit_chown(vfs_handle_struct *handle,
3402 const struct smb_filename *smb_fname,
3407 struct fruit_config_data *config = NULL;
3408 struct smb_filename *adp_smb_fname = NULL;
3410 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3415 SMB_VFS_HANDLE_GET_DATA(handle, config,
3416 struct fruit_config_data, return -1);
3418 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3422 if (!VALID_STAT(smb_fname->st)) {
3426 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3430 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3435 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3437 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3438 if (errno == ENOENT) {
3443 TALLOC_FREE(adp_smb_fname);
3447 static int fruit_rmdir(struct vfs_handle_struct *handle,
3448 const struct smb_filename *smb_fname)
3452 struct fruit_config_data *config;
3454 SMB_VFS_HANDLE_GET_DATA(handle, config,
3455 struct fruit_config_data, return -1);
3457 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3462 * Due to there is no way to change bDeleteVetoFiles variable
3463 * from this module, need to clean up ourselves
3466 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3471 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3473 struct adouble *ad = NULL;
3475 struct smb_filename *ad_smb_fname = NULL;
3478 match = strncmp(de->d_name,
3479 ADOUBLE_NAME_PREFIX,
3480 strlen(ADOUBLE_NAME_PREFIX));
3485 p = talloc_asprintf(talloc_tos(), "%s/%s",
3486 smb_fname->base_name, de->d_name);
3488 DBG_ERR("talloc_asprintf failed\n");
3492 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3496 if (ad_smb_fname == NULL) {
3497 DBG_ERR("synthetic_smb_fname failed\n");
3502 * Check whether it's a valid AppleDouble file, if
3503 * yes, delete it, ignore it otherwise.
3505 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3507 TALLOC_FREE(ad_smb_fname);
3513 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3514 TALLOC_FREE(ad_smb_fname);
3516 DBG_ERR("Deleting [%s] failed\n",
3517 smb_fname_str_dbg(ad_smb_fname));
3525 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3528 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3529 files_struct *fsp, void *data,
3530 size_t n, off_t offset)
3535 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3541 DBG_ERR("Removing [%s] after short read [%zd]\n",
3542 fsp_str_dbg(fsp), nread);
3544 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3546 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3554 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3555 files_struct *fsp, void *data,
3556 size_t n, off_t offset)
3559 struct adouble *ad = NULL;
3560 char afpinfo_buf[AFP_INFO_SIZE];
3564 ai = afpinfo_new(talloc_tos());
3569 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3575 p = ad_get_entry(ad, ADEID_FINDERI);
3577 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3582 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3584 nread = afpinfo_pack(ai, afpinfo_buf);
3585 if (nread != AFP_INFO_SIZE) {
3590 memcpy(data, afpinfo_buf, n);
3598 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3599 files_struct *fsp, void *data,
3600 size_t n, off_t offset)
3602 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3607 * OS X has a off-by-1 error in the offset calculation, so we're
3608 * bug compatible here. It won't hurt, as any relevant real
3609 * world read requests from the AFP_AfpInfo stream will be
3610 * offset=0 n=60. offset is ignored anyway, see below.
3612 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3616 /* Yes, macOS always reads from offset 0 */
3618 to_return = MIN(n, AFP_INFO_SIZE);
3620 switch (fio->config->meta) {
3621 case FRUIT_META_STREAM:
3622 nread = fruit_pread_meta_stream(handle, fsp, data,
3626 case FRUIT_META_NETATALK:
3627 nread = fruit_pread_meta_adouble(handle, fsp, data,
3632 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3639 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3640 files_struct *fsp, void *data,
3641 size_t n, off_t offset)
3643 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3646 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3647 files_struct *fsp, void *data,
3648 size_t n, off_t offset)
3650 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3653 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3654 files_struct *fsp, void *data,
3655 size_t n, off_t offset)
3657 struct adouble *ad = NULL;
3660 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3665 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3666 offset + ad_getentryoff(ad, ADEID_RFORK));
3672 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3673 files_struct *fsp, void *data,
3674 size_t n, off_t offset)
3676 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3679 switch (fio->config->rsrc) {
3680 case FRUIT_RSRC_STREAM:
3681 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3684 case FRUIT_RSRC_ADFILE:
3685 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3688 case FRUIT_RSRC_XATTR:
3689 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3693 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3700 static ssize_t fruit_pread(vfs_handle_struct *handle,
3701 files_struct *fsp, void *data,
3702 size_t n, off_t offset)
3704 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3707 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3708 fsp_str_dbg(fsp), (intmax_t)offset, n);
3711 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3714 if (fio->type == ADOUBLE_META) {
3715 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3717 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3720 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3724 static bool fruit_must_handle_aio_stream(struct fio *fio)
3730 if ((fio->type == ADOUBLE_META) &&
3731 (fio->config->meta == FRUIT_META_NETATALK))
3736 if ((fio->type == ADOUBLE_RSRC) &&
3737 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
3745 struct fruit_pread_state {
3747 struct vfs_aio_state vfs_aio_state;
3750 static void fruit_pread_done(struct tevent_req *subreq);
3752 static struct tevent_req *fruit_pread_send(
3753 struct vfs_handle_struct *handle,
3754 TALLOC_CTX *mem_ctx,
3755 struct tevent_context *ev,
3756 struct files_struct *fsp,
3758 size_t n, off_t offset)
3760 struct tevent_req *req = NULL;
3761 struct tevent_req *subreq = NULL;
3762 struct fruit_pread_state *state = NULL;
3763 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3765 req = tevent_req_create(mem_ctx, &state,
3766 struct fruit_pread_state);
3771 if (fruit_must_handle_aio_stream(fio)) {
3772 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
3773 if (state->nread != n) {
3774 if (state->nread != -1) {
3777 tevent_req_error(req, errno);
3778 return tevent_req_post(req, ev);
3780 tevent_req_done(req);
3781 return tevent_req_post(req, ev);
3784 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
3786 if (tevent_req_nomem(req, subreq)) {
3787 return tevent_req_post(req, ev);
3789 tevent_req_set_callback(subreq, fruit_pread_done, req);
3793 static void fruit_pread_done(struct tevent_req *subreq)
3795 struct tevent_req *req = tevent_req_callback_data(
3796 subreq, struct tevent_req);
3797 struct fruit_pread_state *state = tevent_req_data(
3798 req, struct fruit_pread_state);
3800 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
3801 TALLOC_FREE(subreq);
3803 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3806 tevent_req_done(req);
3809 static ssize_t fruit_pread_recv(struct tevent_req *req,
3810 struct vfs_aio_state *vfs_aio_state)
3812 struct fruit_pread_state *state = tevent_req_data(
3813 req, struct fruit_pread_state);
3815 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3819 *vfs_aio_state = state->vfs_aio_state;
3820 return state->nread;
3823 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
3824 files_struct *fsp, const void *data,
3825 size_t n, off_t offset)
3830 ai = afpinfo_unpack(talloc_tos(), data);
3835 if (ai_empty_finderinfo(ai)) {
3836 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3837 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3838 DBG_ERR("Can't delete metadata for %s: %s\n",
3839 fsp_str_dbg(fsp), strerror(errno));
3847 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3850 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
3851 files_struct *fsp, const void *data,
3852 size_t n, off_t offset)
3854 struct adouble *ad = NULL;
3859 ai = afpinfo_unpack(talloc_tos(), data);
3864 if (ai_empty_finderinfo(ai)) {
3865 ret = SMB_VFS_REMOVEXATTR(handle->conn,
3867 AFPINFO_EA_NETATALK);
3869 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3870 DBG_ERR("Can't delete metadata for %s: %s\n",
3871 fsp_str_dbg(fsp), strerror(errno));
3878 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3880 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
3885 p = ad_get_entry(ad, ADEID_FINDERI);
3887 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3892 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3894 ret = ad_fset(ad, fsp);
3896 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3905 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
3906 files_struct *fsp, const void *data,
3907 size_t n, off_t offset)
3909 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3913 * Writing an all 0 blob to the metadata stream
3914 * results in the stream being removed on a macOS
3915 * server. This ensures we behave the same and it
3916 * verified by the "delete AFP_AfpInfo by writing all
3919 if (n != AFP_INFO_SIZE || offset != 0) {
3920 DBG_ERR("unexpected offset=%jd or size=%jd\n",
3921 (intmax_t)offset, (intmax_t)n);
3925 switch (fio->config->meta) {
3926 case FRUIT_META_STREAM:
3927 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
3931 case FRUIT_META_NETATALK:
3932 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
3937 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3944 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
3945 files_struct *fsp, const void *data,
3946 size_t n, off_t offset)
3948 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3951 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
3952 files_struct *fsp, const void *data,
3953 size_t n, off_t offset)
3955 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3958 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
3959 files_struct *fsp, const void *data,
3960 size_t n, off_t offset)
3962 struct adouble *ad = NULL;
3966 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3968 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
3972 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3973 offset + ad_getentryoff(ad, ADEID_RFORK));
3974 if (nwritten != n) {
3975 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
3976 fsp_str_dbg(fsp), nwritten, n);
3981 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3982 ad_setentrylen(ad, ADEID_RFORK, n + offset);
3983 ret = ad_fset(ad, fsp);
3985 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3995 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
3996 files_struct *fsp, const void *data,
3997 size_t n, off_t offset)
3999 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4002 switch (fio->config->rsrc) {
4003 case FRUIT_RSRC_STREAM:
4004 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4007 case FRUIT_RSRC_ADFILE:
4008 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4011 case FRUIT_RSRC_XATTR:
4012 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4016 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4023 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4024 files_struct *fsp, const void *data,
4025 size_t n, off_t offset)
4027 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4030 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4031 fsp_str_dbg(fsp), (intmax_t)offset, n);
4034 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4037 if (fio->type == ADOUBLE_META) {
4038 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4040 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4043 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4047 struct fruit_pwrite_state {
4049 struct vfs_aio_state vfs_aio_state;
4052 static void fruit_pwrite_done(struct tevent_req *subreq);
4054 static struct tevent_req *fruit_pwrite_send(
4055 struct vfs_handle_struct *handle,
4056 TALLOC_CTX *mem_ctx,
4057 struct tevent_context *ev,
4058 struct files_struct *fsp,
4060 size_t n, off_t offset)
4062 struct tevent_req *req = NULL;
4063 struct tevent_req *subreq = NULL;
4064 struct fruit_pwrite_state *state = NULL;
4065 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4067 req = tevent_req_create(mem_ctx, &state,
4068 struct fruit_pwrite_state);
4073 if (fruit_must_handle_aio_stream(fio)) {
4074 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4075 if (state->nwritten != n) {
4076 if (state->nwritten != -1) {
4079 tevent_req_error(req, errno);
4080 return tevent_req_post(req, ev);
4082 tevent_req_done(req);
4083 return tevent_req_post(req, ev);
4086 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4088 if (tevent_req_nomem(req, subreq)) {
4089 return tevent_req_post(req, ev);
4091 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4095 static void fruit_pwrite_done(struct tevent_req *subreq)
4097 struct tevent_req *req = tevent_req_callback_data(
4098 subreq, struct tevent_req);
4099 struct fruit_pwrite_state *state = tevent_req_data(
4100 req, struct fruit_pwrite_state);
4102 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4103 TALLOC_FREE(subreq);
4105 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4108 tevent_req_done(req);
4111 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4112 struct vfs_aio_state *vfs_aio_state)
4114 struct fruit_pwrite_state *state = tevent_req_data(
4115 req, struct fruit_pwrite_state);
4117 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4121 *vfs_aio_state = state->vfs_aio_state;
4122 return state->nwritten;
4126 * Helper to stat/lstat the base file of an smb_fname.
4128 static int fruit_stat_base(vfs_handle_struct *handle,
4129 struct smb_filename *smb_fname,
4132 char *tmp_stream_name;
4135 tmp_stream_name = smb_fname->stream_name;
4136 smb_fname->stream_name = NULL;
4138 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4140 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4142 smb_fname->stream_name = tmp_stream_name;
4146 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4147 struct smb_filename *smb_fname,
4153 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4155 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4161 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4162 struct smb_filename *smb_fname,
4165 struct adouble *ad = NULL;
4167 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4169 DBG_INFO("fruit_stat_meta %s: %s\n",
4170 smb_fname_str_dbg(smb_fname), strerror(errno));
4176 /* Populate the stat struct with info from the base file. */
4177 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4180 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4181 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4182 smb_fname->stream_name);
4186 static int fruit_stat_meta(vfs_handle_struct *handle,
4187 struct smb_filename *smb_fname,
4190 struct fruit_config_data *config = NULL;
4193 SMB_VFS_HANDLE_GET_DATA(handle, config,
4194 struct fruit_config_data, return -1);
4196 switch (config->meta) {
4197 case FRUIT_META_STREAM:
4198 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4201 case FRUIT_META_NETATALK:
4202 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4206 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4213 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4214 struct smb_filename *smb_fname,
4217 struct adouble *ad = NULL;
4220 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4226 /* Populate the stat struct with info from the base file. */
4227 ret = fruit_stat_base(handle, smb_fname, follow_links);
4233 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4234 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4235 smb_fname->stream_name);
4240 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4241 struct smb_filename *smb_fname,
4247 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4249 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4255 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4256 struct smb_filename *smb_fname,
4259 #ifdef HAVE_ATTROPEN
4263 /* Populate the stat struct with info from the base file. */
4264 ret = fruit_stat_base(handle, smb_fname, follow_links);
4269 fd = attropen(smb_fname->base_name,
4270 AFPRESOURCE_EA_NETATALK,
4276 ret = sys_fstat(fd, &smb_fname->st, false);
4279 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4280 AFPRESOURCE_EA_NETATALK);
4286 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4287 smb_fname->stream_name);
4297 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4298 struct smb_filename *smb_fname,
4301 struct fruit_config_data *config = NULL;
4304 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4306 SMB_VFS_HANDLE_GET_DATA(handle, config,
4307 struct fruit_config_data, return -1);
4309 switch (config->rsrc) {
4310 case FRUIT_RSRC_STREAM:
4311 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4314 case FRUIT_RSRC_XATTR:
4315 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4318 case FRUIT_RSRC_ADFILE:
4319 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4323 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4330 static int fruit_stat(vfs_handle_struct *handle,
4331 struct smb_filename *smb_fname)
4335 DEBUG(10, ("fruit_stat called for %s\n",
4336 smb_fname_str_dbg(smb_fname)));
4338 if (!is_ntfs_stream_smb_fname(smb_fname)
4339 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4340 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4342 update_btime(handle, smb_fname);
4348 * Note if lp_posix_paths() is true, we can never
4349 * get here as is_ntfs_stream_smb_fname() is
4350 * always false. So we never need worry about
4351 * not following links here.
4354 if (is_afpinfo_stream(smb_fname)) {
4355 rc = fruit_stat_meta(handle, smb_fname, true);
4356 } else if (is_afpresource_stream(smb_fname)) {
4357 rc = fruit_stat_rsrc(handle, smb_fname, true);
4359 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4363 update_btime(handle, smb_fname);
4364 smb_fname->st.st_ex_mode &= ~S_IFMT;
4365 smb_fname->st.st_ex_mode |= S_IFREG;
4366 smb_fname->st.st_ex_blocks =
4367 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4372 static int fruit_lstat(vfs_handle_struct *handle,
4373 struct smb_filename *smb_fname)
4377 DEBUG(10, ("fruit_lstat called for %s\n",
4378 smb_fname_str_dbg(smb_fname)));
4380 if (!is_ntfs_stream_smb_fname(smb_fname)
4381 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4382 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4384 update_btime(handle, smb_fname);
4389 if (is_afpinfo_stream(smb_fname)) {
4390 rc = fruit_stat_meta(handle, smb_fname, false);
4391 } else if (is_afpresource_stream(smb_fname)) {
4392 rc = fruit_stat_rsrc(handle, smb_fname, false);
4394 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4398 update_btime(handle, smb_fname);
4399 smb_fname->st.st_ex_mode &= ~S_IFMT;
4400 smb_fname->st.st_ex_mode |= S_IFREG;
4401 smb_fname->st.st_ex_blocks =
4402 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4407 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4409 SMB_STRUCT_STAT *sbuf)
4411 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4414 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4416 SMB_STRUCT_STAT *sbuf)
4420 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4425 *sbuf = fsp->base_fsp->fsp_name->st;
4426 sbuf->st_ex_size = AFP_INFO_SIZE;
4427 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4432 static int fruit_fstat_meta(vfs_handle_struct *handle,
4434 SMB_STRUCT_STAT *sbuf,
4439 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4441 switch (fio->config->meta) {
4442 case FRUIT_META_STREAM:
4443 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4446 case FRUIT_META_NETATALK:
4447 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4451 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4455 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4459 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4461 SMB_STRUCT_STAT *sbuf)
4463 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4466 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4468 SMB_STRUCT_STAT *sbuf)
4470 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4473 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4475 SMB_STRUCT_STAT *sbuf)
4477 struct adouble *ad = NULL;
4480 /* Populate the stat struct with info from the base file. */
4481 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4486 ad = ad_get(talloc_tos(), handle,
4487 fsp->base_fsp->fsp_name,
4490 DBG_ERR("ad_get [%s] failed [%s]\n",
4491 fsp_str_dbg(fsp), strerror(errno));
4495 *sbuf = fsp->base_fsp->fsp_name->st;
4496 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4497 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4503 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4504 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4508 switch (fio->config->rsrc) {
4509 case FRUIT_RSRC_STREAM:
4510 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4513 case FRUIT_RSRC_ADFILE:
4514 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4517 case FRUIT_RSRC_XATTR:
4518 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4522 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4529 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4530 SMB_STRUCT_STAT *sbuf)
4532 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4536 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4539 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4541 if (fio->type == ADOUBLE_META) {
4542 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4544 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4548 sbuf->st_ex_mode &= ~S_IFMT;
4549 sbuf->st_ex_mode |= S_IFREG;
4550 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4553 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4554 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4558 static NTSTATUS fruit_streaminfo_meta_stream(
4559 vfs_handle_struct *handle,
4560 struct files_struct *fsp,
4561 const struct smb_filename *smb_fname,
4562 TALLOC_CTX *mem_ctx,
4563 unsigned int *pnum_streams,
4564 struct stream_struct **pstreams)
4566 struct stream_struct *stream = *pstreams;
4567 unsigned int num_streams = *pnum_streams;
4568 struct smb_filename *sname = NULL;
4573 for (i = 0; i < num_streams; i++) {
4574 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4579 if (i == num_streams) {
4580 return NT_STATUS_OK;
4583 if (stream[i].size == AFP_INFO_SIZE) {
4584 return NT_STATUS_OK;
4587 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4588 "from [%s]\n", (intmax_t)stream[i].size,
4589 smb_fname_str_dbg(smb_fname));
4591 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4593 return NT_STATUS_INTERNAL_ERROR;
4596 sname = synthetic_smb_fname(talloc_tos(),
4597 smb_fname->base_name,
4598 AFPINFO_STREAM_NAME,
4600 if (sname == NULL) {
4601 return NT_STATUS_NO_MEMORY;
4604 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4607 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4608 return map_nt_error_from_unix(errno);
4611 return NT_STATUS_OK;
4614 static NTSTATUS fruit_streaminfo_meta_netatalk(
4615 vfs_handle_struct *handle,
4616 struct files_struct *fsp,
4617 const struct smb_filename *smb_fname,
4618 TALLOC_CTX *mem_ctx,
4619 unsigned int *pnum_streams,
4620 struct stream_struct **pstreams)
4622 struct stream_struct *stream = *pstreams;
4623 unsigned int num_streams = *pnum_streams;
4624 struct adouble *ad = NULL;
4629 /* Remove the Netatalk xattr from the list */
4630 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4631 ":" NETATALK_META_XATTR ":$DATA");
4633 return NT_STATUS_NO_MEMORY;
4637 * Check if there's a AFPINFO_STREAM from the VFS streams
4638 * backend and if yes, remove it from the list
4640 for (i = 0; i < num_streams; i++) {
4641 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4646 if (i < num_streams) {
4647 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4648 smb_fname_str_dbg(smb_fname));
4650 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4653 return NT_STATUS_INTERNAL_ERROR;
4657 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4659 return NT_STATUS_OK;
4662 is_fi_empty = ad_empty_finderinfo(ad);
4666 return NT_STATUS_OK;
4669 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4670 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4671 smb_roundup(handle->conn, AFP_INFO_SIZE));
4673 return NT_STATUS_NO_MEMORY;
4676 return NT_STATUS_OK;
4679 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4680 struct files_struct *fsp,
4681 const struct smb_filename *smb_fname,
4682 TALLOC_CTX *mem_ctx,
4683 unsigned int *pnum_streams,
4684 struct stream_struct **pstreams)
4686 struct fruit_config_data *config = NULL;
4689 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4690 return NT_STATUS_INTERNAL_ERROR);
4692 switch (config->meta) {
4693 case FRUIT_META_NETATALK:
4694 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4695 mem_ctx, pnum_streams,
4699 case FRUIT_META_STREAM:
4700 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4701 mem_ctx, pnum_streams,
4706 return NT_STATUS_INTERNAL_ERROR;
4712 static NTSTATUS fruit_streaminfo_rsrc_stream(
4713 vfs_handle_struct *handle,
4714 struct files_struct *fsp,
4715 const struct smb_filename *smb_fname,
4716 TALLOC_CTX *mem_ctx,
4717 unsigned int *pnum_streams,
4718 struct stream_struct **pstreams)
4722 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4724 DBG_ERR("Filtering resource stream failed\n");
4725 return NT_STATUS_INTERNAL_ERROR;
4727 return NT_STATUS_OK;
4730 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4731 vfs_handle_struct *handle,
4732 struct files_struct *fsp,
4733 const struct smb_filename *smb_fname,
4734 TALLOC_CTX *mem_ctx,
4735 unsigned int *pnum_streams,
4736 struct stream_struct **pstreams)
4740 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4742 DBG_ERR("Filtering resource stream failed\n");
4743 return NT_STATUS_INTERNAL_ERROR;
4745 return NT_STATUS_OK;
4748 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4749 vfs_handle_struct *handle,
4750 struct files_struct *fsp,
4751 const struct smb_filename *smb_fname,
4752 TALLOC_CTX *mem_ctx,
4753 unsigned int *pnum_streams,
4754 struct stream_struct **pstreams)
4756 struct stream_struct *stream = *pstreams;
4757 unsigned int num_streams = *pnum_streams;
4758 struct adouble *ad = NULL;
4764 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4765 * and if yes, remove it from the list
4767 for (i = 0; i < num_streams; i++) {
4768 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4773 if (i < num_streams) {
4774 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4775 smb_fname_str_dbg(smb_fname));
4777 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4778 AFPRESOURCE_STREAM);
4780 return NT_STATUS_INTERNAL_ERROR;
4784 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4786 return NT_STATUS_OK;
4789 rlen = ad_getentrylen(ad, ADEID_RFORK);
4793 return NT_STATUS_OK;
4796 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4797 AFPRESOURCE_STREAM_NAME, rlen,
4798 smb_roundup(handle->conn, rlen));
4800 return NT_STATUS_NO_MEMORY;
4803 return NT_STATUS_OK;
4806 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4807 struct files_struct *fsp,
4808 const struct smb_filename *smb_fname,
4809 TALLOC_CTX *mem_ctx,
4810 unsigned int *pnum_streams,
4811 struct stream_struct **pstreams)
4813 struct fruit_config_data *config = NULL;
4816 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4817 return NT_STATUS_INTERNAL_ERROR);
4819 switch (config->rsrc) {
4820 case FRUIT_RSRC_STREAM:
4821 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4822 mem_ctx, pnum_streams,
4826 case FRUIT_RSRC_XATTR:
4827 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4828 mem_ctx, pnum_streams,
4832 case FRUIT_RSRC_ADFILE:
4833 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4834 mem_ctx, pnum_streams,
4839 return NT_STATUS_INTERNAL_ERROR;
4845 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4846 struct files_struct *fsp,
4847 const struct smb_filename *smb_fname,
4848 TALLOC_CTX *mem_ctx,
4849 unsigned int *pnum_streams,
4850 struct stream_struct **pstreams)
4852 struct fruit_config_data *config = NULL;
4855 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4856 return NT_STATUS_UNSUCCESSFUL);
4858 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4860 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4861 pnum_streams, pstreams);
4862 if (!NT_STATUS_IS_OK(status)) {
4866 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4867 mem_ctx, pnum_streams, pstreams);
4868 if (!NT_STATUS_IS_OK(status)) {
4872 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4873 mem_ctx, pnum_streams, pstreams);
4874 if (!NT_STATUS_IS_OK(status)) {
4878 return NT_STATUS_OK;
4881 static int fruit_ntimes(vfs_handle_struct *handle,
4882 const struct smb_filename *smb_fname,
4883 struct smb_file_time *ft)
4886 struct adouble *ad = NULL;
4887 struct fruit_config_data *config = NULL;
4889 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4892 if ((config->meta != FRUIT_META_NETATALK) ||
4893 null_timespec(ft->create_time))
4895 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4898 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4899 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4901 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4906 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4907 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4909 rc = ad_set(ad, smb_fname);
4915 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4918 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4921 static int fruit_fallocate(struct vfs_handle_struct *handle,
4922 struct files_struct *fsp,
4927 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4930 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4933 /* Let the pwrite code path handle it. */
4938 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4939 struct files_struct *fsp,
4943 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4946 #ifdef HAVE_ATTROPEN
4947 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4952 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4953 struct files_struct *fsp,
4957 struct adouble *ad = NULL;
4960 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4962 DBG_DEBUG("ad_get [%s] failed [%s]\n",
4963 fsp_str_dbg(fsp), strerror(errno));
4967 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4969 rc = ftruncate(fsp->fh->fd, offset + ad_off);
4975 ad_setentrylen(ad, ADEID_RFORK, offset);
4977 rc = ad_fset(ad, fsp);
4979 DBG_ERR("ad_fset [%s] failed [%s]\n",
4980 fsp_str_dbg(fsp), strerror(errno));
4989 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4990 struct files_struct *fsp,
4994 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4997 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5000 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5001 struct files_struct *fsp,
5004 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5007 switch (fio->config->rsrc) {
5008 case FRUIT_RSRC_XATTR:
5009 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5012 case FRUIT_RSRC_ADFILE:
5013 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5016 case FRUIT_RSRC_STREAM:
5017 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5021 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5029 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5030 struct files_struct *fsp,
5034 DBG_WARNING("ftruncate %s to %jd",
5035 fsp_str_dbg(fsp), (intmax_t)offset);
5036 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5041 /* OS X returns success but does nothing */
5042 DBG_INFO("ignoring ftruncate %s to %jd\n",
5043 fsp_str_dbg(fsp), (intmax_t)offset);
5047 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5048 struct files_struct *fsp,
5051 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5054 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5058 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5061 if (fio->type == ADOUBLE_META) {
5062 ret = fruit_ftruncate_meta(handle, fsp, offset);
5064 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5067 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5071 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5072 struct smb_request *req,
5073 uint16_t root_dir_fid,
5074 struct smb_filename *smb_fname,
5075 uint32_t access_mask,
5076 uint32_t share_access,
5077 uint32_t create_disposition,
5078 uint32_t create_options,
5079 uint32_t file_attributes,
5080 uint32_t oplock_request,
5081 struct smb2_lease *lease,
5082 uint64_t allocation_size,
5083 uint32_t private_flags,
5084 struct security_descriptor *sd,
5085 struct ea_list *ea_list,
5086 files_struct **result,
5088 const struct smb2_create_blobs *in_context_blobs,
5089 struct smb2_create_blobs *out_context_blobs)
5092 struct fruit_config_data *config = NULL;
5093 files_struct *fsp = NULL;
5095 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5096 if (!NT_STATUS_IS_OK(status)) {
5100 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5101 return NT_STATUS_UNSUCCESSFUL);
5103 status = SMB_VFS_NEXT_CREATE_FILE(
5104 handle, req, root_dir_fid, smb_fname,
5105 access_mask, share_access,
5106 create_disposition, create_options,
5107 file_attributes, oplock_request,
5109 allocation_size, private_flags,
5110 sd, ea_list, result,
5111 pinfo, in_context_blobs, out_context_blobs);
5112 if (!NT_STATUS_IS_OK(status)) {
5118 if (global_fruit_config.nego_aapl) {
5119 if (config->posix_rename && fsp->is_directory) {
5121 * Enable POSIX directory rename behaviour
5123 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5128 * If this is a plain open for existing files, opening an 0
5129 * byte size resource fork MUST fail with
5130 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5132 * Cf the vfs_fruit torture tests in test_rfork_create().
5134 if (is_afpresource_stream(fsp->fsp_name) &&
5135 create_disposition == FILE_OPEN)
5137 if (fsp->fsp_name->st.st_ex_size == 0) {
5138 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5143 if (is_ntfs_stream_smb_fname(smb_fname)
5144 || fsp->is_directory) {
5148 if (config->locking == FRUIT_LOCKING_NETATALK) {
5149 status = fruit_check_access(
5152 map_share_mode_to_deny_mode(share_access, 0));
5153 if (!NT_STATUS_IS_OK(status)) {
5161 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5164 close_file(req, fsp, ERROR_CLOSE);
5165 *result = fsp = NULL;
5171 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5172 const struct smb_filename *fname,
5173 TALLOC_CTX *mem_ctx,
5174 struct readdir_attr_data **pattr_data)
5176 struct fruit_config_data *config = NULL;
5177 struct readdir_attr_data *attr_data;
5180 SMB_VFS_HANDLE_GET_DATA(handle, config,
5181 struct fruit_config_data,
5182 return NT_STATUS_UNSUCCESSFUL);
5184 if (!global_fruit_config.nego_aapl) {
5185 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5188 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5190 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5191 if (*pattr_data == NULL) {
5192 return NT_STATUS_UNSUCCESSFUL;
5194 attr_data = *pattr_data;
5195 attr_data->type = RDATTR_AAPL;
5198 * Mac metadata: compressed FinderInfo, resource fork length
5201 status = readdir_attr_macmeta(handle, fname, attr_data);
5202 if (!NT_STATUS_IS_OK(status)) {
5204 * Error handling is tricky: if we return failure from
5205 * this function, the corresponding directory entry
5206 * will to be passed to the client, so we really just
5207 * want to error out on fatal errors.
5209 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5217 if (config->unix_info_enabled) {
5218 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5224 if (!config->readdir_attr_max_access) {
5225 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5227 status = smbd_calculate_access_mask(
5231 SEC_FLAG_MAXIMUM_ALLOWED,
5232 &attr_data->attr_data.aapl.max_access);
5233 if (!NT_STATUS_IS_OK(status)) {
5238 return NT_STATUS_OK;
5241 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5242 fname->base_name, nt_errstr(status)));
5243 TALLOC_FREE(*pattr_data);
5247 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5249 uint32_t security_info,
5250 TALLOC_CTX *mem_ctx,
5251 struct security_descriptor **ppdesc)
5254 struct security_ace ace;
5256 struct fruit_config_data *config;
5258 SMB_VFS_HANDLE_GET_DATA(handle, config,
5259 struct fruit_config_data,
5260 return NT_STATUS_UNSUCCESSFUL);
5262 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5264 if (!NT_STATUS_IS_OK(status)) {
5269 * Add MS NFS style ACEs with uid, gid and mode
5271 if (!global_fruit_config.nego_aapl) {
5272 return NT_STATUS_OK;
5274 if (!config->unix_info_enabled) {
5275 return NT_STATUS_OK;
5278 /* MS NFS style mode */
5279 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5280 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5281 status = security_descriptor_dacl_add(*ppdesc, &ace);
5282 if (!NT_STATUS_IS_OK(status)) {
5283 DEBUG(1,("failed to add MS NFS style ACE\n"));
5287 /* MS NFS style uid */
5288 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5289 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5290 status = security_descriptor_dacl_add(*ppdesc, &ace);
5291 if (!NT_STATUS_IS_OK(status)) {
5292 DEBUG(1,("failed to add MS NFS style ACE\n"));
5296 /* MS NFS style gid */
5297 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5298 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5299 status = security_descriptor_dacl_add(*ppdesc, &ace);
5300 if (!NT_STATUS_IS_OK(status)) {
5301 DEBUG(1,("failed to add MS NFS style ACE\n"));
5305 return NT_STATUS_OK;
5308 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5310 uint32_t security_info_sent,
5311 const struct security_descriptor *psd)
5315 mode_t ms_nfs_mode = 0;
5318 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5320 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5321 if (!NT_STATUS_IS_OK(status)) {
5322 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5326 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5327 if (!NT_STATUS_IS_OK(status)) {
5328 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5333 if (fsp->fh->fd != -1) {
5334 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5336 result = SMB_VFS_CHMOD(fsp->conn,
5342 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5343 result, (unsigned)ms_nfs_mode,
5345 status = map_nt_error_from_unix(errno);
5350 return NT_STATUS_OK;
5353 static struct vfs_offload_ctx *fruit_offload_ctx;
5355 struct fruit_offload_read_state {
5356 struct vfs_handle_struct *handle;
5357 struct tevent_context *ev;
5363 static void fruit_offload_read_done(struct tevent_req *subreq);
5365 static struct tevent_req *fruit_offload_read_send(
5366 TALLOC_CTX *mem_ctx,
5367 struct tevent_context *ev,
5368 struct vfs_handle_struct *handle,
5375 struct tevent_req *req = NULL;
5376 struct tevent_req *subreq = NULL;
5377 struct fruit_offload_read_state *state = NULL;
5379 req = tevent_req_create(mem_ctx, &state,
5380 struct fruit_offload_read_state);
5384 *state = (struct fruit_offload_read_state) {
5391 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5392 fsctl, ttl, offset, to_copy);
5393 if (tevent_req_nomem(subreq, req)) {
5394 return tevent_req_post(req, ev);
5396 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5400 static void fruit_offload_read_done(struct tevent_req *subreq)
5402 struct tevent_req *req = tevent_req_callback_data(
5403 subreq, struct tevent_req);
5404 struct fruit_offload_read_state *state = tevent_req_data(
5405 req, struct fruit_offload_read_state);
5408 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5412 TALLOC_FREE(subreq);
5413 if (tevent_req_nterror(req, status)) {
5417 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5418 tevent_req_done(req);
5422 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5423 &fruit_offload_ctx);
5424 if (tevent_req_nterror(req, status)) {
5428 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5431 if (tevent_req_nterror(req, status)) {
5435 tevent_req_done(req);
5439 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5440 struct vfs_handle_struct *handle,
5441 TALLOC_CTX *mem_ctx,
5444 struct fruit_offload_read_state *state = tevent_req_data(
5445 req, struct fruit_offload_read_state);
5448 if (tevent_req_is_nterror(req, &status)) {
5449 tevent_req_received(req);
5453 token->length = state->token.length;
5454 token->data = talloc_move(mem_ctx, &state->token.data);
5456 tevent_req_received(req);
5457 return NT_STATUS_OK;
5460 struct fruit_offload_write_state {
5461 struct vfs_handle_struct *handle;
5463 struct files_struct *src_fsp;
5464 struct files_struct *dst_fsp;
5468 static void fruit_offload_write_done(struct tevent_req *subreq);
5469 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
5470 TALLOC_CTX *mem_ctx,
5471 struct tevent_context *ev,
5474 off_t transfer_offset,
5475 struct files_struct *dest_fsp,
5479 struct tevent_req *req, *subreq;
5480 struct fruit_offload_write_state *state;
5482 struct fruit_config_data *config;
5483 off_t src_off = transfer_offset;
5484 files_struct *src_fsp = NULL;
5485 off_t to_copy = num;
5486 bool copyfile_enabled = false;
5488 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5489 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5491 SMB_VFS_HANDLE_GET_DATA(handle, config,
5492 struct fruit_config_data,
5495 req = tevent_req_create(mem_ctx, &state,
5496 struct fruit_offload_write_state);
5500 state->handle = handle;
5501 state->dst_fsp = dest_fsp;
5504 case FSCTL_SRV_COPYCHUNK:
5505 case FSCTL_SRV_COPYCHUNK_WRITE:
5506 copyfile_enabled = config->copyfile_enabled;
5513 * Check if this a OS X copyfile style copychunk request with
5514 * a requested chunk count of 0 that was translated to a
5515 * offload_write_send VFS call overloading the parameters src_off
5516 * = dest_off = num = 0.
5518 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
5519 status = vfs_offload_token_db_fetch_fsp(
5520 fruit_offload_ctx, token, &src_fsp);
5521 if (tevent_req_nterror(req, status)) {
5522 return tevent_req_post(req, ev);
5524 state->src_fsp = src_fsp;
5526 status = vfs_stat_fsp(src_fsp);
5527 if (tevent_req_nterror(req, status)) {
5528 return tevent_req_post(req, ev);
5531 to_copy = src_fsp->fsp_name->st.st_ex_size;
5532 state->is_copyfile = true;
5535 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
5544 if (tevent_req_nomem(subreq, req)) {
5545 return tevent_req_post(req, ev);
5548 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
5552 static void fruit_offload_write_done(struct tevent_req *subreq)
5554 struct tevent_req *req = tevent_req_callback_data(
5555 subreq, struct tevent_req);
5556 struct fruit_offload_write_state *state = tevent_req_data(
5557 req, struct fruit_offload_write_state);
5559 unsigned int num_streams = 0;
5560 struct stream_struct *streams = NULL;
5562 struct smb_filename *src_fname_tmp = NULL;
5563 struct smb_filename *dst_fname_tmp = NULL;
5565 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
5568 TALLOC_FREE(subreq);
5569 if (tevent_req_nterror(req, status)) {
5573 if (!state->is_copyfile) {
5574 tevent_req_done(req);
5579 * Now copy all remaining streams. We know the share supports
5580 * streams, because we're in vfs_fruit. We don't do this async
5581 * because streams are few and small.
5583 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5584 state->src_fsp->fsp_name,
5585 req, &num_streams, &streams);
5586 if (tevent_req_nterror(req, status)) {
5590 if (num_streams == 1) {
5591 /* There is always one stream, ::$DATA. */
5592 tevent_req_done(req);
5596 for (i = 0; i < num_streams; i++) {
5597 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5598 __func__, streams[i].name, (size_t)streams[i].size));
5600 src_fname_tmp = synthetic_smb_fname(
5602 state->src_fsp->fsp_name->base_name,
5605 state->src_fsp->fsp_name->flags);
5606 if (tevent_req_nomem(src_fname_tmp, req)) {
5610 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5611 TALLOC_FREE(src_fname_tmp);
5615 dst_fname_tmp = synthetic_smb_fname(
5617 state->dst_fsp->fsp_name->base_name,
5620 state->dst_fsp->fsp_name->flags);
5621 if (tevent_req_nomem(dst_fname_tmp, req)) {
5622 TALLOC_FREE(src_fname_tmp);
5626 status = copy_file(req,
5627 state->handle->conn,
5630 OPENX_FILE_CREATE_IF_NOT_EXIST,
5632 if (!NT_STATUS_IS_OK(status)) {
5633 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5634 smb_fname_str_dbg(src_fname_tmp),
5635 smb_fname_str_dbg(dst_fname_tmp),
5636 nt_errstr(status)));
5637 TALLOC_FREE(src_fname_tmp);
5638 TALLOC_FREE(dst_fname_tmp);
5639 tevent_req_nterror(req, status);
5643 TALLOC_FREE(src_fname_tmp);
5644 TALLOC_FREE(dst_fname_tmp);
5647 TALLOC_FREE(streams);
5648 TALLOC_FREE(src_fname_tmp);
5649 TALLOC_FREE(dst_fname_tmp);
5650 tevent_req_done(req);
5653 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
5654 struct tevent_req *req,
5657 struct fruit_offload_write_state *state = tevent_req_data(
5658 req, struct fruit_offload_write_state);
5661 if (tevent_req_is_nterror(req, &status)) {
5662 DEBUG(1, ("server side copy chunk failed: %s\n",
5663 nt_errstr(status)));
5665 tevent_req_received(req);
5669 *copied = state->copied;
5670 tevent_req_received(req);
5672 return NT_STATUS_OK;
5675 static struct vfs_fn_pointers vfs_fruit_fns = {
5676 .connect_fn = fruit_connect,
5678 /* File operations */
5679 .chmod_fn = fruit_chmod,
5680 .chown_fn = fruit_chown,
5681 .unlink_fn = fruit_unlink,
5682 .rename_fn = fruit_rename,
5683 .rmdir_fn = fruit_rmdir,
5684 .open_fn = fruit_open,
5685 .pread_fn = fruit_pread,
5686 .pwrite_fn = fruit_pwrite,
5687 .pread_send_fn = fruit_pread_send,
5688 .pread_recv_fn = fruit_pread_recv,
5689 .pwrite_send_fn = fruit_pwrite_send,
5690 .pwrite_recv_fn = fruit_pwrite_recv,
5691 .stat_fn = fruit_stat,
5692 .lstat_fn = fruit_lstat,
5693 .fstat_fn = fruit_fstat,
5694 .streaminfo_fn = fruit_streaminfo,
5695 .ntimes_fn = fruit_ntimes,
5696 .ftruncate_fn = fruit_ftruncate,
5697 .fallocate_fn = fruit_fallocate,
5698 .create_file_fn = fruit_create_file,
5699 .readdir_attr_fn = fruit_readdir_attr,
5700 .offload_read_send_fn = fruit_offload_read_send,
5701 .offload_read_recv_fn = fruit_offload_read_recv,
5702 .offload_write_send_fn = fruit_offload_write_send,
5703 .offload_write_recv_fn = fruit_offload_write_recv,
5705 /* NT ACL operations */
5706 .fget_nt_acl_fn = fruit_fget_nt_acl,
5707 .fset_nt_acl_fn = fruit_fset_nt_acl,
5710 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
5711 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5713 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5715 if (!NT_STATUS_IS_OK(ret)) {
5719 vfs_fruit_debug_level = debug_add_class("fruit");
5720 if (vfs_fruit_debug_level == -1) {
5721 vfs_fruit_debug_level = DBGC_VFS;
5722 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5725 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5726 "vfs_fruit_init","fruit",vfs_fruit_debug_level));