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"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
98 static int vfs_fruit_debug_level = DBGC_VFS;
100 static struct global_fruit_config {
101 bool nego_aapl; /* client negotiated AAPL */
103 } global_fruit_config;
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
122 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
124 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
125 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
126 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
127 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
129 struct fruit_config_data {
130 enum fruit_rsrc rsrc;
131 enum fruit_meta meta;
132 enum fruit_locking locking;
133 enum fruit_encoding encoding;
134 bool use_aapl; /* config from smb.conf */
136 bool readdir_attr_enabled;
137 bool unix_info_enabled;
138 bool copyfile_enabled;
139 bool veto_appledouble;
141 bool aapl_zero_file_id;
146 * Additional options, all enabled by default,
147 * possibly useful for analyzing performance. The associated
148 * operations with each of them may be expensive, so having
149 * the chance to disable them individually gives a chance
150 * tweaking the setup for the particular usecase.
152 bool readdir_attr_rsize;
153 bool readdir_attr_finder_info;
154 bool readdir_attr_max_access;
157 static const struct enum_list fruit_rsrc[] = {
158 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
159 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
160 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
164 static const struct enum_list fruit_meta[] = {
165 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
166 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
170 static const struct enum_list fruit_locking[] = {
171 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
172 {FRUIT_LOCKING_NONE, "none"},
176 static const struct enum_list fruit_encoding[] = {
177 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
178 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
182 static const char *fruit_catia_maps =
183 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
184 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
185 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
186 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
187 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
188 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
189 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
190 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
191 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
192 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
195 /*****************************************************************************
196 * Defines, functions and data structures that deal with AppleDouble
197 *****************************************************************************/
200 * There are two AppleDouble blobs we deal with:
202 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
203 * metadata in an xattr
205 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
208 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
211 #define AD_VERSION2 0x00020000
212 #define AD_VERSION AD_VERSION2
215 * AppleDouble entry IDs.
217 #define ADEID_DFORK 1
218 #define ADEID_RFORK 2
220 #define ADEID_COMMENT 4
221 #define ADEID_ICONBW 5
222 #define ADEID_ICONCOL 6
223 #define ADEID_FILEI 7
224 #define ADEID_FILEDATESI 8
225 #define ADEID_FINDERI 9
226 #define ADEID_MACFILEI 10
227 #define ADEID_PRODOSFILEI 11
228 #define ADEID_MSDOSFILEI 12
229 #define ADEID_SHORTNAME 13
230 #define ADEID_AFPFILEI 14
233 /* Private Netatalk entries */
234 #define ADEID_PRIVDEV 16
235 #define ADEID_PRIVINO 17
236 #define ADEID_PRIVSYN 18
237 #define ADEID_PRIVID 19
238 #define ADEID_MAX (ADEID_PRIVID + 1)
241 * These are the real ids for the private entries,
242 * as stored in the adouble file
244 #define AD_DEV 0x80444556
245 #define AD_INO 0x80494E4F
246 #define AD_SYN 0x8053594E
247 #define AD_ID 0x8053567E
249 /* Number of actually used entries */
250 #define ADEID_NUM_XATTR 8
251 #define ADEID_NUM_DOT_UND 2
252 #define ADEID_NUM_RSRC_XATTR 1
254 /* AppleDouble magic */
255 #define AD_APPLESINGLE_MAGIC 0x00051600
256 #define AD_APPLEDOUBLE_MAGIC 0x00051607
257 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
259 /* Sizes of relevant entry bits */
260 #define ADEDLEN_MAGIC 4
261 #define ADEDLEN_VERSION 4
262 #define ADEDLEN_FILLER 16
263 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
264 #define ADEDLEN_NENTRIES 2
265 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
266 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
267 #define AD_ENTRY_LEN_EID 4
268 #define AD_ENTRY_LEN_OFF 4
269 #define AD_ENTRY_LEN_LEN 4
270 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
273 #define ADEDLEN_NAME 255
274 #define ADEDLEN_COMMENT 200
275 #define ADEDLEN_FILEI 16
276 #define ADEDLEN_FINDERI 32
277 #define ADEDLEN_FILEDATESI 16
278 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
279 #define ADEDLEN_AFPFILEI 4
280 #define ADEDLEN_MACFILEI 4
281 #define ADEDLEN_PRODOSFILEI 8
282 #define ADEDLEN_MSDOSFILEI 2
283 #define ADEDLEN_DID 4
284 #define ADEDLEN_PRIVDEV 8
285 #define ADEDLEN_PRIVINO 8
286 #define ADEDLEN_PRIVSYN 8
287 #define ADEDLEN_PRIVID 4
290 #define ADEDOFF_MAGIC 0
291 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
292 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
293 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
295 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
296 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
297 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
298 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
299 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
301 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
302 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
303 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
304 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
306 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
307 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
308 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
310 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
311 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
312 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
313 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
314 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
315 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
317 #if AD_DATASZ_XATTR != 402
318 #error bad size for AD_DATASZ_XATTR
321 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
322 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
324 #if AD_DATASZ_DOT_UND != 82
325 #error bad size for AD_DATASZ_DOT_UND
329 * Sharemode locks fcntl() offsets
331 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
332 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
334 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
336 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
338 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
339 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
340 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
341 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
342 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
343 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
344 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
345 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
346 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
347 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
349 /* Time stuff we overload the bits a little */
350 #define AD_DATE_CREATE 0
351 #define AD_DATE_MODIFY 4
352 #define AD_DATE_BACKUP 8
353 #define AD_DATE_ACCESS 12
354 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
355 AD_DATE_BACKUP | AD_DATE_ACCESS)
356 #define AD_DATE_UNIX (1 << 10)
357 #define AD_DATE_START 0x80000000
358 #define AD_DATE_DELTA 946684800
359 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
360 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
362 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
363 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
364 #define AD_XATTR_HDR_SIZE 36
365 #define AD_XATTR_MAX_HDR_SIZE 65536
367 /* Accessor macros */
368 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
369 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
370 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
371 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
374 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
375 * representation as well as the on-disk format.
377 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
378 * the length of the FinderInfo entry is larger then 32 bytes. It is then
379 * preceeded with 2 bytes padding.
381 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
384 struct ad_xattr_header {
385 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
386 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
387 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
388 uint32_t adx_data_start; /* file offset to attribute data area */
389 uint32_t adx_data_length; /* length of attribute data area */
390 uint32_t adx_reserved[3];
392 uint16_t adx_num_attrs;
395 /* On-disk entries are aligned on 4 byte boundaries */
396 struct ad_xattr_entry {
397 uint32_t adx_offset; /* file offset to data */
398 uint32_t adx_length; /* size of attribute data */
400 uint8_t adx_namelen; /* included the NULL terminator */
401 char *adx_name; /* NULL-terminated UTF-8 name */
410 vfs_handle_struct *ad_handle;
413 adouble_type_t ad_type;
416 struct ad_entry ad_eid[ADEID_MAX];
418 struct ad_xattr_header adx_header;
419 struct ad_xattr_entry *adx_entries;
422 struct ad_entry_order {
423 uint32_t id, offset, len;
426 /* Netatalk AppleDouble metadata xattr */
428 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
429 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
430 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
431 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
432 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
433 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
434 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
435 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
436 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
440 /* AppleDouble resource fork file (the ones prefixed by "._") */
442 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
443 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
444 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
449 * Fake AppleDouble entry oder for resource fork xattr. The xattr
450 * isn't an AppleDouble file, it simply contains the resource data,
451 * but in order to be able to use some API calls like ad_getentryoff()
452 * we build a fake/helper struct adouble with this entry order struct.
455 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
460 /* Conversion from enumerated id to on-disk AppleDouble id */
461 #define AD_EID_DISK(a) (set_eid[a])
462 static const uint32_t set_eid[] = {
463 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
464 AD_DEV, AD_INO, AD_SYN, AD_ID
468 /* tcon config handle */
469 struct fruit_config_data *config;
471 /* Denote stream type, meta or rsrc */
476 * Forward declarations
478 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
479 adouble_type_t type);
480 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
481 static int ad_fset(struct adouble *ad, files_struct *fsp);
482 static int adouble_path(TALLOC_CTX *ctx,
483 const struct smb_filename *smb_fname__in,
484 struct smb_filename **ppsmb_fname_out);
485 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
486 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
487 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
491 * Return a pointer to an AppleDouble entry
493 * Returns NULL if the entry is not present
495 static char *ad_get_entry(const struct adouble *ad, int eid)
497 off_t off = ad_getentryoff(ad, eid);
498 size_t len = ad_getentrylen(ad, eid);
500 if (off == 0 || len == 0) {
504 return ad->ad_data + off;
510 static int ad_getdate(const struct adouble *ad,
511 unsigned int dateoff,
514 bool xlate = (dateoff & AD_DATE_UNIX);
517 dateoff &= AD_DATE_MASK;
518 p = ad_get_entry(ad, ADEID_FILEDATESI);
523 if (dateoff > AD_DATE_ACCESS) {
527 memcpy(date, p + dateoff, sizeof(uint32_t));
530 *date = AD_DATE_TO_UNIX(*date);
538 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
540 bool xlate = (dateoff & AD_DATE_UNIX);
543 p = ad_get_entry(ad, ADEID_FILEDATESI);
548 dateoff &= AD_DATE_MASK;
550 date = AD_DATE_FROM_UNIX(date);
553 if (dateoff > AD_DATE_ACCESS) {
557 memcpy(p + dateoff, &date, sizeof(date));
564 * Map on-disk AppleDouble id to enumerated id
566 static uint32_t get_eid(uint32_t eid)
574 return ADEID_PRIVDEV;
576 return ADEID_PRIVINO;
578 return ADEID_PRIVSYN;
589 * Pack AppleDouble structure into data buffer
591 static bool ad_pack(struct adouble *ad)
598 bufsize = talloc_get_size(ad->ad_data);
599 if (bufsize < AD_DATASZ_DOT_UND) {
600 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
604 if (offset + ADEDLEN_MAGIC < offset ||
605 offset + ADEDLEN_MAGIC >= bufsize) {
608 RSIVAL(ad->ad_data, offset, ad->ad_magic);
609 offset += ADEDLEN_MAGIC;
611 if (offset + ADEDLEN_VERSION < offset ||
612 offset + ADEDLEN_VERSION >= bufsize) {
615 RSIVAL(ad->ad_data, offset, ad->ad_version);
616 offset += ADEDLEN_VERSION;
618 if (offset + ADEDLEN_FILLER < offset ||
619 offset + ADEDLEN_FILLER >= bufsize) {
622 if (ad->ad_type == ADOUBLE_RSRC) {
623 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
625 offset += ADEDLEN_FILLER;
627 if (offset + ADEDLEN_NENTRIES < offset ||
628 offset + ADEDLEN_NENTRIES >= bufsize) {
631 offset += ADEDLEN_NENTRIES;
633 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
634 if (ad->ad_eid[eid].ade_off == 0) {
636 * ade_off is also used as indicator whether a
637 * specific entry is used or not
642 if (offset + AD_ENTRY_LEN_EID < offset ||
643 offset + AD_ENTRY_LEN_EID >= bufsize) {
646 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
647 offset += AD_ENTRY_LEN_EID;
649 if (offset + AD_ENTRY_LEN_OFF < offset ||
650 offset + AD_ENTRY_LEN_OFF >= bufsize) {
653 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
654 offset += AD_ENTRY_LEN_OFF;
656 if (offset + AD_ENTRY_LEN_LEN < offset ||
657 offset + AD_ENTRY_LEN_LEN >= bufsize) {
660 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
661 offset += AD_ENTRY_LEN_LEN;
666 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
669 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
674 static bool ad_unpack_xattrs(struct adouble *ad)
676 struct ad_xattr_header *h = &ad->adx_header;
677 const char *p = ad->ad_data;
681 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
685 /* 2 bytes padding */
686 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
688 h->adx_magic = RIVAL(p, hoff + 0);
689 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
690 h->adx_total_size = RIVAL(p, hoff + 8);
691 h->adx_data_start = RIVAL(p, hoff + 12);
692 h->adx_data_length = RIVAL(p, hoff + 16);
693 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
694 h->adx_num_attrs = RSVAL(p, hoff + 34);
696 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
697 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
701 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
702 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
705 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
706 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
710 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
711 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
715 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
716 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
719 if ((h->adx_data_start + h->adx_data_length) >
720 ad->adx_header.adx_total_size)
722 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
726 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
727 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
731 if (h->adx_num_attrs == 0) {
735 ad->adx_entries = talloc_zero_array(
736 ad, struct ad_xattr_entry, h->adx_num_attrs);
737 if (ad->adx_entries == NULL) {
741 hoff += AD_XATTR_HDR_SIZE;
743 for (i = 0; i < h->adx_num_attrs; i++) {
744 struct ad_xattr_entry *e = &ad->adx_entries[i];
746 hoff = (hoff + 3) & ~3;
748 e->adx_offset = RIVAL(p, hoff + 0);
749 e->adx_length = RIVAL(p, hoff + 4);
750 e->adx_flags = RSVAL(p, hoff + 8);
751 e->adx_namelen = *(p + hoff + 10);
753 if (e->adx_offset >= ad->adx_header.adx_total_size) {
754 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
759 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
760 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
765 if ((e->adx_offset + e->adx_length) >
766 ad->adx_header.adx_total_size)
768 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
773 if (e->adx_namelen == 0) {
774 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
778 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
779 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
783 if ((hoff + 11 + e->adx_namelen) >
784 ad->adx_header.adx_data_start)
786 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
791 e->adx_name = talloc_strndup(ad->adx_entries,
794 if (e->adx_name == NULL) {
798 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
799 e->adx_name, e->adx_offset, e->adx_length);
800 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
803 hoff += 11 + e->adx_namelen;
810 * Unpack an AppleDouble blob into a struct adoble
812 static bool ad_unpack(struct adouble *ad, const size_t nentries,
815 size_t bufsize = talloc_get_size(ad->ad_data);
817 uint32_t eid, len, off;
821 * The size of the buffer ad->ad_data is checked when read, so
822 * we wouldn't have to check our own offsets, a few extra
823 * checks won't hurt though. We have to check the offsets we
824 * read from the buffer anyway.
827 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
828 DEBUG(1, ("bad size\n"));
832 ad->ad_magic = RIVAL(ad->ad_data, 0);
833 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
834 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
835 DEBUG(1, ("wrong magic or version\n"));
839 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
840 if (adentries != nentries) {
841 DEBUG(1, ("invalid number of entries: %zu\n",
846 /* now, read in the entry bits */
847 for (i = 0; i < adentries; i++) {
848 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
850 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
851 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
853 if (!eid || eid >= ADEID_MAX) {
854 DEBUG(1, ("bogus eid %d\n", eid));
859 * All entries other than the resource fork are
860 * expected to be read into the ad_data buffer, so
861 * ensure the specified offset is within that bound
863 if ((off > bufsize) && (eid != ADEID_RFORK)) {
864 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
870 * All entries besides FinderInfo and resource fork
871 * must fit into the buffer. FinderInfo is special as
872 * it may be larger then the default 32 bytes (if it
873 * contains marshalled xattrs), but we will fixup that
874 * in ad_convert(). And the resource fork is never
875 * accessed directly by the ad_data buf (also see
876 * comment above) anyway.
878 if ((eid != ADEID_RFORK) &&
879 (eid != ADEID_FINDERI) &&
880 ((off + len) > bufsize)) {
881 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
887 * That would be obviously broken
889 if (off > filesize) {
890 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
896 * Check for any entry that has its end beyond the
899 if (off + len < off) {
900 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
901 ", len: %" PRIu32 "\n",
906 if (off + len > filesize) {
908 * If this is the resource fork entry, we fix
909 * up the length, for any other entry we bail
912 if (eid != ADEID_RFORK) {
913 DEBUG(1, ("bogus eid %d: off: %" PRIu32
914 ", len: %" PRIu32 "\n",
920 * Fixup the resource fork entry by limiting
921 * the size to entryoffset - filesize.
923 len = filesize - off;
924 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
925 ", len: %" PRIu32 "\n", off, len));
928 ad->ad_eid[eid].ade_off = off;
929 ad->ad_eid[eid].ade_len = len;
932 ok = ad_unpack_xattrs(ad);
940 static bool ad_convert_xattr(struct adouble *ad,
941 const struct smb_filename *smb_fname,
944 static struct char_mappings **string_replace_cmaps = NULL;
949 if (ad->adx_header.adx_num_attrs == 0) {
953 if (string_replace_cmaps == NULL) {
954 const char **mappings = NULL;
956 mappings = str_list_make_v3_const(
957 talloc_tos(), fruit_catia_maps, NULL);
958 if (mappings == NULL) {
961 string_replace_cmaps = string_replace_init_map(mappings);
962 TALLOC_FREE(mappings);
965 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
966 struct ad_xattr_entry *e = &ad->adx_entries[i];
967 char *mapped_name = NULL;
969 struct smb_filename *stream_name = NULL;
970 files_struct *fsp = NULL;
973 status = string_replace_allocate(ad->ad_handle->conn,
975 string_replace_cmaps,
978 vfs_translate_to_windows);
979 if (!NT_STATUS_IS_OK(status) &&
980 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
982 DBG_ERR("string_replace_allocate failed\n");
987 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
989 if (mapped_name == NULL) {
993 stream_name = synthetic_smb_fname(talloc_tos(),
994 smb_fname->base_name,
998 TALLOC_FREE(mapped_name);
999 if (stream_name == NULL) {
1000 DBG_ERR("synthetic_smb_fname failed\n");
1004 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1006 status = SMB_VFS_CREATE_FILE(
1007 ad->ad_handle->conn, /* conn */
1009 0, /* root_dir_fid */
1010 stream_name, /* fname */
1011 FILE_GENERIC_WRITE, /* access_mask */
1012 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1013 FILE_OPEN_IF, /* create_disposition */
1014 0, /* create_options */
1015 0, /* file_attributes */
1016 INTERNAL_OPEN_ONLY, /* oplock_request */
1018 0, /* allocation_size */
1019 0, /* private_flags */
1024 NULL, NULL); /* create context */
1025 TALLOC_FREE(stream_name);
1026 if (!NT_STATUS_IS_OK(status)) {
1027 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1031 nwritten = SMB_VFS_PWRITE(fsp,
1032 map + e->adx_offset,
1035 if (nwritten == -1) {
1036 DBG_ERR("SMB_VFS_PWRITE failed\n");
1037 saved_errno = errno;
1038 close_file(NULL, fsp, ERROR_CLOSE);
1039 errno = saved_errno;
1043 status = close_file(NULL, fsp, NORMAL_CLOSE);
1044 if (!NT_STATUS_IS_OK(status)) {
1054 * Convert from Apple's ._ file to Netatalk
1056 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1057 * bytes containing packed xattrs. Netatalk can't deal with that, so
1058 * we simply discard the packed xattrs.
1060 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1063 static int ad_convert(struct adouble *ad,
1064 const struct smb_filename *smb_fname,
1068 char *map = MAP_FAILED;
1072 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1073 ad_getentrylen(ad, ADEID_RFORK);
1075 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1076 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1077 if (map == MAP_FAILED) {
1078 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1083 ok = ad_convert_xattr(ad, smb_fname, map);
1085 munmap(map, origlen);
1089 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1090 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1091 map + ad_getentryoff(ad, ADEID_RFORK),
1092 ad_getentrylen(ad, ADEID_RFORK));
1095 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1096 ad_setentryoff(ad, ADEID_RFORK,
1097 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1100 * FIXME: direct ftruncate(), but we don't have a fsp for the
1103 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
1104 + ad_getentrylen(ad, ADEID_RFORK));
1107 if (map != MAP_FAILED) {
1108 munmap(map, origlen);
1114 * Read and parse Netatalk AppleDouble metadata xattr
1116 static ssize_t ad_read_meta(struct adouble *ad,
1117 const struct smb_filename *smb_fname)
1123 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1125 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1126 AFPINFO_EA_NETATALK, ad->ad_data,
1132 if (errno == ENOATTR) {
1138 DEBUG(2, ("error reading meta xattr: %s\n",
1144 if (ealen != AD_DATASZ_XATTR) {
1145 DEBUG(2, ("bad size %zd\n", ealen));
1151 /* Now parse entries */
1152 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1154 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1160 if (!ad_getentryoff(ad, ADEID_FINDERI)
1161 || !ad_getentryoff(ad, ADEID_COMMENT)
1162 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1163 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1164 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1165 || !ad_getentryoff(ad, ADEID_PRIVINO)
1166 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1167 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1168 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1175 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1176 smb_fname->base_name, rc));
1180 if (errno == EINVAL) {
1182 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1190 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1194 #ifdef HAVE_ATTROPEN
1195 /* FIXME: direct Solaris xattr syscall */
1196 return attropen(smb_fname->base_name,
1197 AFPRESOURCE_EA_NETATALK, flags, mode);
1204 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1210 struct smb_filename *adp_smb_fname = NULL;
1212 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1217 fd = open(adp_smb_fname->base_name, flags, mode);
1218 TALLOC_FREE(adp_smb_fname);
1223 static int ad_open_rsrc(vfs_handle_struct *handle,
1224 const struct smb_filename *smb_fname,
1228 struct fruit_config_data *config = NULL;
1231 SMB_VFS_HANDLE_GET_DATA(handle, config,
1232 struct fruit_config_data, return -1);
1234 if (config->rsrc == FRUIT_RSRC_XATTR) {
1235 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1237 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1244 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1245 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1246 * for file IO on the ._ file.
1248 static int ad_open(vfs_handle_struct *handle,
1251 const struct smb_filename *smb_fname,
1257 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1258 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1260 if (ad->ad_type == ADOUBLE_META) {
1264 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1265 ad->ad_fd = fsp->fh->fd;
1266 ad->ad_opened = false;
1270 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1274 ad->ad_opened = true;
1277 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1278 smb_fname->base_name,
1279 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1284 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1289 /* FIXME: direct sys_fstat(), don't have an fsp */
1290 ret = sys_fstat(ad->ad_fd, &st,
1291 lp_fake_directory_create_times(
1292 SNUM(ad->ad_handle->conn)));
1297 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1298 return st.st_ex_size;
1301 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1302 const struct smb_filename *smb_fname)
1304 SMB_STRUCT_STAT sbuf;
1308 struct smb_filename *stream_name = NULL;
1309 files_struct *fsp = NULL;
1314 int saved_errno = 0;
1318 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1319 SNUM(ad->ad_handle->conn)));
1325 * AppleDouble file header content and size, two cases:
1327 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1328 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1330 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1332 size = sbuf.st_ex_size;
1333 if (size > talloc_array_length(ad->ad_data)) {
1334 if (size > AD_XATTR_MAX_HDR_SIZE) {
1335 size = AD_XATTR_MAX_HDR_SIZE;
1337 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1344 len = sys_pread(ad->ad_fd, ad->ad_data,
1345 talloc_array_length(ad->ad_data), 0);
1346 if (len != talloc_array_length(ad->ad_data)) {
1347 DBG_NOTICE("%s %s: bad size: %zd\n",
1348 smb_fname->base_name, strerror(errno), len);
1352 /* Now parse entries */
1353 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1355 DBG_ERR("invalid AppleDouble resource %s\n",
1356 smb_fname->base_name);
1361 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1362 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1363 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1364 DBG_ERR("invalid AppleDouble resource %s\n",
1365 smb_fname->base_name);
1370 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1375 * Try to fixup AppleDouble files created by OS X with xattrs
1376 * appended to the ADEID_FINDERI entry. We simply remove the
1377 * xattrs blob, this means any fancy xattr that was stored
1381 ret = ad_convert(ad, smb_fname, ad->ad_fd);
1383 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1389 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1393 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1394 if (len != AD_DATASZ_DOT_UND) {
1395 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1399 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1404 ai = afpinfo_new(talloc_tos());
1409 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1411 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1412 if (aiblob.data == NULL) {
1417 size = afpinfo_pack(ai, (char *)aiblob.data);
1419 if (size != AFP_INFO_SIZE) {
1423 stream_name = synthetic_smb_fname(talloc_tos(),
1424 smb_fname->base_name,
1428 if (stream_name == NULL) {
1429 data_blob_free(&aiblob);
1430 DBG_ERR("synthetic_smb_fname failed\n");
1434 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1436 status = SMB_VFS_CREATE_FILE(
1437 ad->ad_handle->conn, /* conn */
1439 0, /* root_dir_fid */
1440 stream_name, /* fname */
1441 FILE_GENERIC_WRITE, /* access_mask */
1442 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1443 FILE_OPEN_IF, /* create_disposition */
1444 0, /* create_options */
1445 0, /* file_attributes */
1446 INTERNAL_OPEN_ONLY, /* oplock_request */
1448 0, /* allocation_size */
1449 0, /* private_flags */
1454 NULL, NULL); /* create context */
1455 TALLOC_FREE(stream_name);
1456 if (!NT_STATUS_IS_OK(status)) {
1457 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1461 nwritten = SMB_VFS_PWRITE(fsp,
1465 if (nwritten == -1) {
1466 DBG_ERR("SMB_VFS_PWRITE failed\n");
1467 saved_errno = errno;
1468 close_file(NULL, fsp, ERROR_CLOSE);
1469 errno = saved_errno;
1473 status = close_file(NULL, fsp, NORMAL_CLOSE);
1474 if (!NT_STATUS_IS_OK(status)) {
1483 * Read and parse resource fork, either ._ AppleDouble file or xattr
1485 static ssize_t ad_read_rsrc(struct adouble *ad,
1486 const struct smb_filename *smb_fname)
1488 struct fruit_config_data *config = NULL;
1491 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1492 struct fruit_config_data, return -1);
1494 if (config->rsrc == FRUIT_RSRC_XATTR) {
1495 len = ad_read_rsrc_xattr(ad);
1497 len = ad_read_rsrc_adouble(ad, smb_fname);
1504 * Read and unpack an AppleDouble metadata xattr or resource
1506 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1508 switch (ad->ad_type) {
1510 return ad_read_meta(ad, smb_fname);
1512 return ad_read_rsrc(ad, smb_fname);
1518 static int adouble_destructor(struct adouble *ad)
1520 if ((ad->ad_fd != -1) && ad->ad_opened) {
1528 * Allocate a struct adouble without initialiing it
1530 * The struct is either hang of the fsp extension context or if fsp is
1533 * @param[in] ctx talloc context
1534 * @param[in] handle vfs handle
1535 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1537 * @return adouble handle
1539 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1540 adouble_type_t type)
1545 struct fruit_config_data *config;
1547 SMB_VFS_HANDLE_GET_DATA(handle, config,
1548 struct fruit_config_data, return NULL);
1552 adsize = AD_DATASZ_XATTR;
1555 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1556 adsize = AD_DATASZ_DOT_UND;
1563 ad = talloc_zero(ctx, struct adouble);
1570 ad->ad_data = talloc_zero_array(ad, char, adsize);
1571 if (ad->ad_data == NULL) {
1577 ad->ad_handle = handle;
1579 ad->ad_magic = AD_MAGIC;
1580 ad->ad_version = AD_VERSION;
1583 talloc_set_destructor(ad, adouble_destructor);
1593 * Allocate and initialize a new struct adouble
1595 * @param[in] ctx talloc context
1596 * @param[in] handle vfs handle
1597 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1599 * @return adouble handle, initialized
1601 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1602 adouble_type_t type)
1605 const struct ad_entry_order *eid;
1606 struct adouble *ad = NULL;
1607 struct fruit_config_data *config;
1608 time_t t = time(NULL);
1610 SMB_VFS_HANDLE_GET_DATA(handle, config,
1611 struct fruit_config_data, return NULL);
1615 eid = entry_order_meta_xattr;
1618 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1619 eid = entry_order_dot_und;
1621 eid = entry_order_rsrc_xattr;
1628 ad = ad_alloc(ctx, handle, type);
1634 ad->ad_eid[eid->id].ade_off = eid->offset;
1635 ad->ad_eid[eid->id].ade_len = eid->len;
1639 /* put something sane in the date fields */
1640 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1641 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1642 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1643 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1651 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1652 vfs_handle_struct *handle,
1654 const struct smb_filename *smb_fname,
1655 adouble_type_t type)
1659 struct adouble *ad = NULL;
1663 smb_fname = fsp->base_fsp->fsp_name;
1666 DEBUG(10, ("ad_get(%s) called for %s\n",
1667 type == ADOUBLE_META ? "meta" : "rsrc",
1668 smb_fname->base_name));
1670 ad = ad_alloc(ctx, handle, type);
1676 /* Try rw first so we can use the fd in ad_convert() */
1679 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1680 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1682 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1685 DBG_DEBUG("ad_open [%s] error [%s]\n",
1686 smb_fname->base_name, strerror(errno));
1691 len = ad_read(ad, smb_fname);
1693 DEBUG(10, ("error reading AppleDouble for %s\n",
1694 smb_fname->base_name));
1700 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1701 type == ADOUBLE_META ? "meta" : "rsrc",
1702 smb_fname->base_name, rc));
1711 * Return AppleDouble data for a file
1713 * @param[in] ctx talloc context
1714 * @param[in] handle vfs handle
1715 * @param[in] smb_fname pathname to file or directory
1716 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1718 * @return talloced struct adouble or NULL on error
1720 static struct adouble *ad_get(TALLOC_CTX *ctx,
1721 vfs_handle_struct *handle,
1722 const struct smb_filename *smb_fname,
1723 adouble_type_t type)
1725 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1729 * Return AppleDouble data for a file
1731 * @param[in] ctx talloc context
1732 * @param[in] handle vfs handle
1733 * @param[in] fsp fsp to use for IO
1734 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1736 * @return talloced struct adouble or NULL on error
1738 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1739 files_struct *fsp, adouble_type_t type)
1741 return ad_get_internal(ctx, handle, fsp, NULL, type);
1745 * Set AppleDouble metadata on a file or directory
1747 * @param[in] ad adouble handle
1749 * @param[in] smb_fname pathname to file or directory
1751 * @return status code, 0 means success
1753 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1758 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1760 if (ad->ad_type != ADOUBLE_META) {
1761 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1762 smb_fname->base_name);
1771 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1773 AFPINFO_EA_NETATALK,
1775 AD_DATASZ_XATTR, 0);
1777 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1783 * Set AppleDouble metadata on a file or directory
1785 * @param[in] ad adouble handle
1786 * @param[in] fsp file handle
1788 * @return status code, 0 means success
1790 static int ad_fset(struct adouble *ad, files_struct *fsp)
1796 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1799 || (fsp->fh == NULL)
1800 || (fsp->fh->fd == -1))
1802 smb_panic("bad fsp");
1810 switch (ad->ad_type) {
1812 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1814 AFPINFO_EA_NETATALK,
1816 AD_DATASZ_XATTR, 0);
1820 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1825 if (len != AD_DATASZ_DOT_UND) {
1826 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1836 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1841 /*****************************************************************************
1843 *****************************************************************************/
1845 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1847 if (strncasecmp_m(smb_fname->stream_name,
1848 AFPINFO_STREAM_NAME,
1849 strlen(AFPINFO_STREAM_NAME)) == 0) {
1855 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1857 if (strncasecmp_m(smb_fname->stream_name,
1858 AFPRESOURCE_STREAM_NAME,
1859 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1866 * Test whether stream is an Apple stream, not used atm
1869 static bool is_apple_stream(const struct smb_filename *smb_fname)
1871 if (is_afpinfo_stream(smb_fname)) {
1874 if (is_afpresource_stream(smb_fname)) {
1882 * Initialize config struct from our smb.conf config parameters
1884 static int init_fruit_config(vfs_handle_struct *handle)
1886 struct fruit_config_data *config;
1889 config = talloc_zero(handle->conn, struct fruit_config_data);
1891 DEBUG(1, ("talloc_zero() failed\n"));
1897 * Versions up to Samba 4.5.x had a spelling bug in the
1898 * fruit:resource option calling lp_parm_enum with
1899 * "res*s*ource" (ie two s).
1901 * In Samba 4.6 we accept both the wrong and the correct
1902 * spelling, in Samba 4.7 the bad spelling will be removed.
1904 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1905 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1906 if (enumval == -1) {
1907 DEBUG(1, ("value for %s: resource type unknown\n",
1908 FRUIT_PARAM_TYPE_NAME));
1911 config->rsrc = (enum fruit_rsrc)enumval;
1913 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1914 "resource", fruit_rsrc, enumval);
1915 if (enumval == -1) {
1916 DEBUG(1, ("value for %s: resource type unknown\n",
1917 FRUIT_PARAM_TYPE_NAME));
1920 config->rsrc = (enum fruit_rsrc)enumval;
1922 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1923 "metadata", fruit_meta, FRUIT_META_NETATALK);
1924 if (enumval == -1) {
1925 DEBUG(1, ("value for %s: metadata type unknown\n",
1926 FRUIT_PARAM_TYPE_NAME));
1929 config->meta = (enum fruit_meta)enumval;
1931 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1932 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1933 if (enumval == -1) {
1934 DEBUG(1, ("value for %s: locking type unknown\n",
1935 FRUIT_PARAM_TYPE_NAME));
1938 config->locking = (enum fruit_locking)enumval;
1940 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1941 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1942 if (enumval == -1) {
1943 DEBUG(1, ("value for %s: encoding type unknown\n",
1944 FRUIT_PARAM_TYPE_NAME));
1947 config->encoding = (enum fruit_encoding)enumval;
1949 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1950 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1951 FRUIT_PARAM_TYPE_NAME,
1956 config->use_aapl = lp_parm_bool(
1957 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1959 config->time_machine = lp_parm_bool(
1960 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
1962 config->unix_info_enabled = lp_parm_bool(
1963 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1965 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1968 config->posix_rename = lp_parm_bool(
1969 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1971 config->aapl_zero_file_id =
1972 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1974 config->readdir_attr_rsize = lp_parm_bool(
1975 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1977 config->readdir_attr_finder_info = lp_parm_bool(
1978 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1980 config->readdir_attr_max_access = lp_parm_bool(
1981 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1983 config->model = lp_parm_const_string(
1984 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1986 SMB_VFS_HANDLE_SET_DATA(handle, config,
1987 NULL, struct fruit_config_data,
1994 * Prepend "._" to a basename
1995 * Return a new struct smb_filename with stream_name == NULL.
1997 static int adouble_path(TALLOC_CTX *ctx,
1998 const struct smb_filename *smb_fname_in,
1999 struct smb_filename **pp_smb_fname_out)
2003 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2006 if (smb_fname == NULL) {
2010 /* We need streamname to be NULL */
2011 TALLOC_FREE(smb_fname->stream_name);
2013 /* And we're replacing base_name. */
2014 TALLOC_FREE(smb_fname->base_name);
2016 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2018 TALLOC_FREE(smb_fname);
2022 smb_fname->base_name = talloc_asprintf(smb_fname,
2023 "%s/._%s", parent, base);
2024 if (smb_fname->base_name == NULL) {
2025 TALLOC_FREE(smb_fname);
2029 *pp_smb_fname_out = smb_fname;
2035 * Allocate and initialize an AfpInfo struct
2037 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2039 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2043 ai->afpi_Signature = AFP_Signature;
2044 ai->afpi_Version = AFP_Version;
2045 ai->afpi_BackupTime = AD_DATE_START;
2050 * Pack an AfpInfo struct into a buffer
2052 * Buffer size must be at least AFP_INFO_SIZE
2053 * Returns size of packed buffer
2055 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2057 memset(buf, 0, AFP_INFO_SIZE);
2059 RSIVAL(buf, 0, ai->afpi_Signature);
2060 RSIVAL(buf, 4, ai->afpi_Version);
2061 RSIVAL(buf, 12, ai->afpi_BackupTime);
2062 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2064 return AFP_INFO_SIZE;
2068 * Unpack a buffer into a AfpInfo structure
2070 * Buffer size must be at least AFP_INFO_SIZE
2071 * Returns allocated AfpInfo struct
2073 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2075 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2080 ai->afpi_Signature = RIVAL(data, 0);
2081 ai->afpi_Version = RIVAL(data, 4);
2082 ai->afpi_BackupTime = RIVAL(data, 12);
2083 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2084 sizeof(ai->afpi_FinderInfo));
2086 if (ai->afpi_Signature != AFP_Signature
2087 || ai->afpi_Version != AFP_Version) {
2088 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2096 * Fake an inode number from the md5 hash of the (xattr) name
2098 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2101 unsigned char hash[16];
2105 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2106 SMB_ASSERT(upper_sname != NULL);
2109 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2110 sizeof(sbuf->st_ex_dev));
2111 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2112 sizeof(sbuf->st_ex_ino));
2113 MD5Update(&ctx, (unsigned char *)upper_sname,
2114 talloc_get_size(upper_sname)-1);
2115 MD5Final(hash, &ctx);
2117 TALLOC_FREE(upper_sname);
2119 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2120 memcpy(&result, hash, sizeof(result));
2122 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2123 sname, (unsigned long long)result));
2128 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2129 struct stream_struct **streams,
2130 const char *name, off_t size,
2133 struct stream_struct *tmp;
2135 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2141 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2142 if (tmp[*num_streams].name == NULL) {
2146 tmp[*num_streams].size = size;
2147 tmp[*num_streams].alloc_size = alloc_size;
2154 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2155 struct stream_struct **streams)
2157 struct stream_struct *tmp = *streams;
2160 if (*num_streams == 0) {
2164 for (i = 0; i < *num_streams; i++) {
2165 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2170 if (i == *num_streams) {
2174 if (tmp[i].size > 0) {
2178 TALLOC_FREE(tmp[i].name);
2179 if (*num_streams - 1 > i) {
2180 memmove(&tmp[i], &tmp[i+1],
2181 (*num_streams - i - 1) * sizeof(struct stream_struct));
2188 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2189 struct stream_struct **streams,
2192 struct stream_struct *tmp = *streams;
2195 if (*num_streams == 0) {
2199 for (i = 0; i < *num_streams; i++) {
2200 if (strequal_m(tmp[i].name, name)) {
2205 if (i == *num_streams) {
2209 TALLOC_FREE(tmp[i].name);
2210 if (*num_streams - 1 > i) {
2211 memmove(&tmp[i], &tmp[i+1],
2212 (*num_streams - i - 1) * sizeof(struct stream_struct));
2219 static bool ad_empty_finderinfo(const struct adouble *ad)
2222 char emptybuf[ADEDLEN_FINDERI] = {0};
2225 fi = ad_get_entry(ad, ADEID_FINDERI);
2227 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2231 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2235 static bool ai_empty_finderinfo(const AfpInfo *ai)
2238 char emptybuf[ADEDLEN_FINDERI] = {0};
2240 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2245 * Update btime with btime from Netatalk
2247 static void update_btime(vfs_handle_struct *handle,
2248 struct smb_filename *smb_fname)
2251 struct timespec creation_time = {0};
2253 struct fruit_config_data *config = NULL;
2255 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2258 switch (config->meta) {
2259 case FRUIT_META_STREAM:
2261 case FRUIT_META_NETATALK:
2265 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2269 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2273 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2279 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2280 update_stat_ex_create_time(&smb_fname->st, creation_time);
2286 * Map an access mask to a Netatalk single byte byte range lock
2288 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2289 uint32_t access_mask)
2293 switch (access_mask) {
2294 case FILE_READ_DATA:
2295 offset = AD_FILELOCK_OPEN_RD;
2298 case FILE_WRITE_DATA:
2299 case FILE_APPEND_DATA:
2300 offset = AD_FILELOCK_OPEN_WR;
2304 offset = AD_FILELOCK_OPEN_NONE;
2308 if (fork_type == APPLE_FORK_RSRC) {
2309 if (offset == AD_FILELOCK_OPEN_NONE) {
2310 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2320 * Map a deny mode to a Netatalk brl
2322 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2327 switch (deny_mode) {
2329 offset = AD_FILELOCK_DENY_RD;
2333 offset = AD_FILELOCK_DENY_WR;
2337 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2340 if (fork_type == APPLE_FORK_RSRC) {
2348 * Call fcntl() with an exclusive F_GETLK request in order to
2349 * determine if there's an exisiting shared lock
2351 * @return true if the requested lock was found or any error occurred
2352 * false if the lock was not found
2354 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2357 off_t offset = in_offset;
2362 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2363 if (result == false) {
2367 if (type != F_UNLCK) {
2374 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2376 uint32_t access_mask,
2379 NTSTATUS status = NT_STATUS_OK;
2380 struct byte_range_lock *br_lck = NULL;
2381 bool open_for_reading, open_for_writing, deny_read, deny_write;
2383 bool have_read = false;
2386 /* FIXME: hardcoded data fork, add resource fork */
2387 enum apple_fork fork_type = APPLE_FORK_DATA;
2389 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2391 access_mask & FILE_READ_DATA ? "READ" :"-",
2392 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2393 deny_mode & DENY_READ ? "DENY_READ" : "-",
2394 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2396 if (fsp->fh->fd == -1) {
2397 return NT_STATUS_OK;
2400 flags = fcntl(fsp->fh->fd, F_GETFL);
2402 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2403 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2404 return map_nt_error_from_unix(errno);
2407 if (flags & (O_RDONLY|O_RDWR)) {
2409 * Applying fcntl read locks requires an fd opened for
2410 * reading. This means we won't be applying locks for
2411 * files openend write-only, but what can we do...
2417 * Check read access and deny read mode
2419 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2421 open_for_reading = test_netatalk_lock(
2422 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2424 deny_read = test_netatalk_lock(
2425 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2427 DEBUG(10, ("read: %s, deny_write: %s\n",
2428 open_for_reading == true ? "yes" : "no",
2429 deny_read == true ? "yes" : "no"));
2431 if (((access_mask & FILE_READ_DATA) && deny_read)
2432 || ((deny_mode & DENY_READ) && open_for_reading)) {
2433 return NT_STATUS_SHARING_VIOLATION;
2437 if ((access_mask & FILE_READ_DATA) && have_read) {
2438 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2440 handle->conn->sconn->msg_ctx, fsp,
2441 fsp->op->global->open_persistent_id, 1, off,
2442 READ_LOCK, POSIX_LOCK, false,
2445 if (!NT_STATUS_IS_OK(status)) {
2448 TALLOC_FREE(br_lck);
2451 if ((deny_mode & DENY_READ) && have_read) {
2452 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2454 handle->conn->sconn->msg_ctx, fsp,
2455 fsp->op->global->open_persistent_id, 1, off,
2456 READ_LOCK, POSIX_LOCK, false,
2459 if (!NT_STATUS_IS_OK(status)) {
2462 TALLOC_FREE(br_lck);
2467 * Check write access and deny write mode
2469 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2471 open_for_writing = test_netatalk_lock(
2472 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2474 deny_write = test_netatalk_lock(
2475 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2477 DEBUG(10, ("write: %s, deny_write: %s\n",
2478 open_for_writing == true ? "yes" : "no",
2479 deny_write == true ? "yes" : "no"));
2481 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2482 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2483 return NT_STATUS_SHARING_VIOLATION;
2487 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2488 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2490 handle->conn->sconn->msg_ctx, fsp,
2491 fsp->op->global->open_persistent_id, 1, off,
2492 READ_LOCK, POSIX_LOCK, false,
2495 if (!NT_STATUS_IS_OK(status)) {
2498 TALLOC_FREE(br_lck);
2501 if ((deny_mode & DENY_WRITE) && have_read) {
2502 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2504 handle->conn->sconn->msg_ctx, fsp,
2505 fsp->op->global->open_persistent_id, 1, off,
2506 READ_LOCK, POSIX_LOCK, false,
2509 if (!NT_STATUS_IS_OK(status)) {
2512 TALLOC_FREE(br_lck);
2516 TALLOC_FREE(br_lck);
2521 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2522 struct smb_request *req,
2523 const struct smb2_create_blobs *in_context_blobs,
2524 struct smb2_create_blobs *out_context_blobs)
2526 struct fruit_config_data *config;
2528 struct smb2_create_blob *aapl = NULL;
2532 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2533 uint64_t req_bitmap, client_caps;
2534 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2538 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2539 return NT_STATUS_UNSUCCESSFUL);
2541 if (!config->use_aapl
2542 || in_context_blobs == NULL
2543 || out_context_blobs == NULL) {
2544 return NT_STATUS_OK;
2547 aapl = smb2_create_blob_find(in_context_blobs,
2548 SMB2_CREATE_TAG_AAPL);
2550 return NT_STATUS_OK;
2553 if (aapl->data.length != 24) {
2554 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2555 (uintmax_t)aapl->data.length));
2556 return NT_STATUS_INVALID_PARAMETER;
2559 cmd = IVAL(aapl->data.data, 0);
2560 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2561 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2562 return NT_STATUS_INVALID_PARAMETER;
2565 req_bitmap = BVAL(aapl->data.data, 8);
2566 client_caps = BVAL(aapl->data.data, 16);
2568 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2570 SBVAL(p, 8, req_bitmap);
2571 ok = data_blob_append(req, &blob, p, 16);
2573 return NT_STATUS_UNSUCCESSFUL;
2576 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2577 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2578 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2579 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2580 config->readdir_attr_enabled = true;
2583 if (config->use_copyfile) {
2584 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2585 config->copyfile_enabled = true;
2589 * The client doesn't set the flag, so we can't check
2590 * for it and just set it unconditionally
2592 if (config->unix_info_enabled) {
2593 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2596 SBVAL(p, 0, server_caps);
2597 ok = data_blob_append(req, &blob, p, 8);
2599 return NT_STATUS_UNSUCCESSFUL;
2603 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2604 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2612 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2619 if (config->time_machine) {
2620 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2625 ok = data_blob_append(req, &blob, p, 8);
2627 return NT_STATUS_UNSUCCESSFUL;
2631 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2632 ok = convert_string_talloc(req,
2633 CH_UNIX, CH_UTF16LE,
2634 config->model, strlen(config->model),
2637 return NT_STATUS_UNSUCCESSFUL;
2641 SIVAL(p + 4, 0, modellen);
2642 ok = data_blob_append(req, &blob, p, 8);
2645 return NT_STATUS_UNSUCCESSFUL;
2648 ok = data_blob_append(req, &blob, model, modellen);
2651 return NT_STATUS_UNSUCCESSFUL;
2655 status = smb2_create_blob_add(out_context_blobs,
2657 SMB2_CREATE_TAG_AAPL,
2659 if (NT_STATUS_IS_OK(status)) {
2660 global_fruit_config.nego_aapl = true;
2661 if (config->aapl_zero_file_id) {
2662 aapl_force_zero_file_id(handle->conn->sconn);
2669 static bool readdir_attr_meta_finderi_stream(
2670 struct vfs_handle_struct *handle,
2671 const struct smb_filename *smb_fname,
2674 struct smb_filename *stream_name = NULL;
2675 files_struct *fsp = NULL;
2680 uint8_t buf[AFP_INFO_SIZE];
2682 stream_name = synthetic_smb_fname(talloc_tos(),
2683 smb_fname->base_name,
2684 AFPINFO_STREAM_NAME,
2685 NULL, smb_fname->flags);
2686 if (stream_name == NULL) {
2690 ret = SMB_VFS_STAT(handle->conn, stream_name);
2695 status = SMB_VFS_CREATE_FILE(
2696 handle->conn, /* conn */
2698 0, /* root_dir_fid */
2699 stream_name, /* fname */
2700 FILE_READ_DATA, /* access_mask */
2701 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2703 FILE_OPEN, /* create_disposition*/
2704 0, /* create_options */
2705 0, /* file_attributes */
2706 INTERNAL_OPEN_ONLY, /* oplock_request */
2708 0, /* allocation_size */
2709 0, /* private_flags */
2714 NULL, NULL); /* create context */
2716 TALLOC_FREE(stream_name);
2718 if (!NT_STATUS_IS_OK(status)) {
2722 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2723 if (nread != AFP_INFO_SIZE) {
2724 DBG_ERR("short read [%s] [%zd/%d]\n",
2725 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2730 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2737 close_file(NULL, fsp, NORMAL_CLOSE);
2743 static bool readdir_attr_meta_finderi_netatalk(
2744 struct vfs_handle_struct *handle,
2745 const struct smb_filename *smb_fname,
2748 struct adouble *ad = NULL;
2751 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2756 p = ad_get_entry(ad, ADEID_FINDERI);
2758 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2763 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2768 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2769 const struct smb_filename *smb_fname,
2770 struct readdir_attr_data *attr_data)
2772 struct fruit_config_data *config = NULL;
2773 uint32_t date_added;
2777 SMB_VFS_HANDLE_GET_DATA(handle, config,
2778 struct fruit_config_data,
2781 switch (config->meta) {
2782 case FRUIT_META_NETATALK:
2783 ok = readdir_attr_meta_finderi_netatalk(
2784 handle, smb_fname, &ai);
2787 case FRUIT_META_STREAM:
2788 ok = readdir_attr_meta_finderi_stream(
2789 handle, smb_fname, &ai);
2793 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2798 /* Don't bother with errors, it's likely ENOENT */
2802 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2804 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2805 &ai.afpi_FinderInfo[0], 4);
2807 /* finder_creator */
2808 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2809 &ai.afpi_FinderInfo[4], 4);
2813 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2814 &ai.afpi_FinderInfo[8], 2);
2816 /* finder_ext_flags */
2817 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2818 &ai.afpi_FinderInfo[24], 2);
2821 date_added = convert_time_t_to_uint32_t(
2822 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2824 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2829 static uint64_t readdir_attr_rfork_size_adouble(
2830 struct vfs_handle_struct *handle,
2831 const struct smb_filename *smb_fname)
2833 struct adouble *ad = NULL;
2834 uint64_t rfork_size;
2836 ad = ad_get(talloc_tos(), handle, smb_fname,
2842 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2848 static uint64_t readdir_attr_rfork_size_stream(
2849 struct vfs_handle_struct *handle,
2850 const struct smb_filename *smb_fname)
2852 struct smb_filename *stream_name = NULL;
2854 uint64_t rfork_size;
2856 stream_name = synthetic_smb_fname(talloc_tos(),
2857 smb_fname->base_name,
2858 AFPRESOURCE_STREAM_NAME,
2860 if (stream_name == NULL) {
2864 ret = SMB_VFS_STAT(handle->conn, stream_name);
2866 TALLOC_FREE(stream_name);
2870 rfork_size = stream_name->st.st_ex_size;
2871 TALLOC_FREE(stream_name);
2876 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2877 const struct smb_filename *smb_fname)
2879 struct fruit_config_data *config = NULL;
2880 uint64_t rfork_size;
2882 SMB_VFS_HANDLE_GET_DATA(handle, config,
2883 struct fruit_config_data,
2886 switch (config->rsrc) {
2887 case FRUIT_RSRC_ADFILE:
2888 case FRUIT_RSRC_XATTR:
2889 rfork_size = readdir_attr_rfork_size_adouble(handle,
2893 case FRUIT_META_STREAM:
2894 rfork_size = readdir_attr_rfork_size_stream(handle,
2899 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2907 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2908 const struct smb_filename *smb_fname,
2909 struct readdir_attr_data *attr_data)
2911 NTSTATUS status = NT_STATUS_OK;
2912 struct fruit_config_data *config = NULL;
2915 SMB_VFS_HANDLE_GET_DATA(handle, config,
2916 struct fruit_config_data,
2917 return NT_STATUS_UNSUCCESSFUL);
2920 /* Ensure we return a default value in the creation_date field */
2921 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2924 * Resource fork length
2927 if (config->readdir_attr_rsize) {
2928 uint64_t rfork_size;
2930 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2931 attr_data->attr_data.aapl.rfork_size = rfork_size;
2938 if (config->readdir_attr_finder_info) {
2939 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2941 status = NT_STATUS_INTERNAL_ERROR;
2948 /* Search MS NFS style ACE with UNIX mode */
2949 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2951 const struct security_descriptor *psd,
2956 struct fruit_config_data *config = NULL;
2960 SMB_VFS_HANDLE_GET_DATA(handle, config,
2961 struct fruit_config_data,
2962 return NT_STATUS_UNSUCCESSFUL);
2964 if (!global_fruit_config.nego_aapl) {
2965 return NT_STATUS_OK;
2967 if (psd->dacl == NULL || !config->unix_info_enabled) {
2968 return NT_STATUS_OK;
2971 for (i = 0; i < psd->dacl->num_aces; i++) {
2972 if (dom_sid_compare_domain(
2973 &global_sid_Unix_NFS_Mode,
2974 &psd->dacl->aces[i].trustee) == 0) {
2975 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2976 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2979 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2980 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2985 return NT_STATUS_OK;
2988 /****************************************************************************
2990 ****************************************************************************/
2992 static int fruit_connect(vfs_handle_struct *handle,
2993 const char *service,
2997 char *list = NULL, *newlist = NULL;
2998 struct fruit_config_data *config;
3000 DEBUG(10, ("fruit_connect\n"));
3002 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3007 rc = init_fruit_config(handle);
3012 SMB_VFS_HANDLE_GET_DATA(handle, config,
3013 struct fruit_config_data, return -1);
3015 if (config->veto_appledouble) {
3016 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3019 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3020 newlist = talloc_asprintf(
3022 "%s/" ADOUBLE_NAME_PREFIX "*/",
3024 lp_do_parameter(SNUM(handle->conn),
3029 lp_do_parameter(SNUM(handle->conn),
3031 "/" ADOUBLE_NAME_PREFIX "*/");
3037 if (config->encoding == FRUIT_ENC_NATIVE) {
3038 lp_do_parameter(SNUM(handle->conn),
3043 if (config->time_machine) {
3044 DBG_NOTICE("Enabling durable handles for Time Machine "
3045 "support on [%s]\n", service);
3046 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3047 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3048 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3049 if (!lp_strict_sync(SNUM(handle->conn))) {
3050 DBG_WARNING("Time Machine without strict sync is not "
3053 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3059 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3060 struct smb_filename *smb_fname,
3066 char afpinfo_buf[AFP_INFO_SIZE];
3067 ssize_t len, written;
3071 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3076 if (!(flags & (O_CREAT | O_TRUNC))) {
3080 ai = afpinfo_new(talloc_tos());
3086 len = afpinfo_pack(ai, afpinfo_buf);
3087 if (len != AFP_INFO_SIZE) {
3092 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3093 fsp->fh->fd = hostfd;
3095 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3098 if (written != AFP_INFO_SIZE) {
3099 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3107 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3110 int saved_errno = errno;
3112 fsp->fh->fd = hostfd;
3113 SMB_VFS_NEXT_CLOSE(handle, fsp);
3116 errno = saved_errno;
3121 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3122 struct smb_filename *smb_fname,
3129 struct adouble *ad = NULL;
3132 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3135 * Return a valid fd, but ensure any attempt to use it returns an error
3136 * (EPIPE). All operations on the smb_fname or the fsp will use path
3146 if (flags & (O_CREAT | O_TRUNC)) {
3148 * The attribute does not exist or needs to be truncated,
3149 * create an AppleDouble EA
3151 ad = ad_init(fsp, handle, ADOUBLE_META);
3157 rc = ad_set(ad, fsp->fsp_name);
3167 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3169 int saved_errno = errno;
3174 errno = saved_errno;
3179 static int fruit_open_meta(vfs_handle_struct *handle,
3180 struct smb_filename *smb_fname,
3181 files_struct *fsp, int flags, mode_t mode)
3184 struct fruit_config_data *config = NULL;
3185 struct fio *fio = NULL;
3187 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3189 SMB_VFS_HANDLE_GET_DATA(handle, config,
3190 struct fruit_config_data, return -1);
3192 switch (config->meta) {
3193 case FRUIT_META_STREAM:
3194 fd = fruit_open_meta_stream(handle, smb_fname,
3198 case FRUIT_META_NETATALK:
3199 fd = fruit_open_meta_netatalk(handle, smb_fname,
3204 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3208 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3214 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3215 fio->type = ADOUBLE_META;
3216 fio->config = config;
3221 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3222 struct smb_filename *smb_fname,
3228 struct adouble *ad = NULL;
3229 struct smb_filename *smb_fname_base = NULL;
3230 struct fruit_config_data *config = NULL;
3233 SMB_VFS_HANDLE_GET_DATA(handle, config,
3234 struct fruit_config_data, return -1);
3236 if ((!(flags & O_CREAT)) &&
3237 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3239 /* sorry, but directories don't habe a resource fork */
3244 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3249 /* Sanitize flags */
3250 if (flags & O_WRONLY) {
3251 /* We always need read access for the metadata header too */
3256 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3263 if (flags & (O_CREAT | O_TRUNC)) {
3264 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3270 fsp->fh->fd = hostfd;
3272 rc = ad_fset(ad, fsp);
3283 TALLOC_FREE(smb_fname_base);
3285 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3287 int saved_errno = errno;
3290 * BUGBUGBUG -- we would need to call
3291 * fd_close_posix here, but we don't have a
3294 fsp->fh->fd = hostfd;
3298 errno = saved_errno;
3303 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3304 struct smb_filename *smb_fname,
3309 #ifdef HAVE_ATTROPEN
3312 fd = attropen(smb_fname->base_name,
3313 AFPRESOURCE_EA_NETATALK,
3328 static int fruit_open_rsrc(vfs_handle_struct *handle,
3329 struct smb_filename *smb_fname,
3330 files_struct *fsp, int flags, mode_t mode)
3333 struct fruit_config_data *config = NULL;
3334 struct fio *fio = NULL;
3336 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3338 SMB_VFS_HANDLE_GET_DATA(handle, config,
3339 struct fruit_config_data, return -1);
3341 if (((flags & O_ACCMODE) == O_RDONLY)
3342 && (flags & O_CREAT)
3343 && !VALID_STAT(fsp->fsp_name->st))
3346 * This means the stream doesn't exist. macOS SMB server fails
3347 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3348 * 12565 and the test for this combination in
3349 * test_rfork_create().
3355 switch (config->rsrc) {
3356 case FRUIT_RSRC_STREAM:
3357 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3360 case FRUIT_RSRC_ADFILE:
3361 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3365 case FRUIT_RSRC_XATTR:
3366 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3371 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3375 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3381 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3382 fio->type = ADOUBLE_RSRC;
3383 fio->config = config;
3388 static int fruit_open(vfs_handle_struct *handle,
3389 struct smb_filename *smb_fname,
3390 files_struct *fsp, int flags, mode_t mode)
3394 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3396 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3397 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3400 if (is_afpinfo_stream(smb_fname)) {
3401 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3402 } else if (is_afpresource_stream(smb_fname)) {
3403 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3405 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3408 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3413 static int fruit_rename(struct vfs_handle_struct *handle,
3414 const struct smb_filename *smb_fname_src,
3415 const struct smb_filename *smb_fname_dst)
3418 struct fruit_config_data *config = NULL;
3419 struct smb_filename *src_adp_smb_fname = NULL;
3420 struct smb_filename *dst_adp_smb_fname = NULL;
3422 SMB_VFS_HANDLE_GET_DATA(handle, config,
3423 struct fruit_config_data, return -1);
3425 if (!VALID_STAT(smb_fname_src->st)) {
3426 DBG_ERR("Need valid stat for [%s]\n",
3427 smb_fname_str_dbg(smb_fname_src));
3431 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3436 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3437 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3442 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3447 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3452 DBG_DEBUG("%s -> %s\n",
3453 smb_fname_str_dbg(src_adp_smb_fname),
3454 smb_fname_str_dbg(dst_adp_smb_fname));
3456 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3457 if (errno == ENOENT) {
3462 TALLOC_FREE(src_adp_smb_fname);
3463 TALLOC_FREE(dst_adp_smb_fname);
3467 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3468 const struct smb_filename *smb_fname)
3470 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3473 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3474 const struct smb_filename *smb_fname)
3476 return SMB_VFS_REMOVEXATTR(handle->conn,
3478 AFPINFO_EA_NETATALK);
3481 static int fruit_unlink_meta(vfs_handle_struct *handle,
3482 const struct smb_filename *smb_fname)
3484 struct fruit_config_data *config = NULL;
3487 SMB_VFS_HANDLE_GET_DATA(handle, config,
3488 struct fruit_config_data, return -1);
3490 switch (config->meta) {
3491 case FRUIT_META_STREAM:
3492 rc = fruit_unlink_meta_stream(handle, smb_fname);
3495 case FRUIT_META_NETATALK:
3496 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3500 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3507 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3508 const struct smb_filename *smb_fname,
3513 if (!force_unlink) {
3514 struct smb_filename *smb_fname_cp = NULL;
3517 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3518 if (smb_fname_cp == NULL) {
3523 * 0 byte resource fork streams are not listed by
3524 * vfs_streaminfo, as a result stream cleanup/deletion of file
3525 * deletion doesn't remove the resourcefork stream.
3528 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3530 TALLOC_FREE(smb_fname_cp);
3531 DBG_ERR("stat [%s] failed [%s]\n",
3532 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3536 size = smb_fname_cp->st.st_ex_size;
3537 TALLOC_FREE(smb_fname_cp);
3540 /* OS X ignores resource fork stream delete requests */
3545 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3546 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3553 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3554 const struct smb_filename *smb_fname,
3558 struct adouble *ad = NULL;
3559 struct smb_filename *adp_smb_fname = NULL;
3561 if (!force_unlink) {
3562 ad = ad_get(talloc_tos(), handle, smb_fname,
3571 * 0 byte resource fork streams are not listed by
3572 * vfs_streaminfo, as a result stream cleanup/deletion of file
3573 * deletion doesn't remove the resourcefork stream.
3576 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3577 /* OS X ignores resource fork stream delete requests */
3585 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3590 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3591 TALLOC_FREE(adp_smb_fname);
3592 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3599 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3600 const struct smb_filename *smb_fname,
3604 * OS X ignores resource fork stream delete requests, so nothing to do
3605 * here. Removing the file will remove the xattr anyway, so we don't
3606 * have to take care of removing 0 byte resource forks that could be
3612 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3613 const struct smb_filename *smb_fname,
3616 struct fruit_config_data *config = NULL;
3619 SMB_VFS_HANDLE_GET_DATA(handle, config,
3620 struct fruit_config_data, return -1);
3622 switch (config->rsrc) {
3623 case FRUIT_RSRC_STREAM:
3624 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3627 case FRUIT_RSRC_ADFILE:
3628 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3631 case FRUIT_RSRC_XATTR:
3632 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3636 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3643 static int fruit_unlink(vfs_handle_struct *handle,
3644 const struct smb_filename *smb_fname)
3647 struct fruit_config_data *config = NULL;
3648 struct smb_filename *rsrc_smb_fname = NULL;
3650 SMB_VFS_HANDLE_GET_DATA(handle, config,
3651 struct fruit_config_data, return -1);
3653 if (is_afpinfo_stream(smb_fname)) {
3654 return fruit_unlink_meta(handle, smb_fname);
3655 } else if (is_afpresource_stream(smb_fname)) {
3656 return fruit_unlink_rsrc(handle, smb_fname, false);
3657 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3658 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3662 * A request to delete the base file. Because 0 byte resource
3663 * fork streams are not listed by fruit_streaminfo,
3664 * delete_all_streams() can't remove 0 byte resource fork
3665 * streams, so we have to cleanup this here.
3667 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3668 smb_fname->base_name,
3669 AFPRESOURCE_STREAM_NAME,
3672 if (rsrc_smb_fname == NULL) {
3676 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3677 if ((rc != 0) && (errno != ENOENT)) {
3678 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3679 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3680 TALLOC_FREE(rsrc_smb_fname);
3683 TALLOC_FREE(rsrc_smb_fname);
3685 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3688 static int fruit_chmod(vfs_handle_struct *handle,
3689 const struct smb_filename *smb_fname,
3693 struct fruit_config_data *config = NULL;
3694 struct smb_filename *smb_fname_adp = NULL;
3696 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3701 SMB_VFS_HANDLE_GET_DATA(handle, config,
3702 struct fruit_config_data, return -1);
3704 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3708 if (!VALID_STAT(smb_fname->st)) {
3712 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3716 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3721 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3723 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3724 if (errno == ENOENT) {
3728 TALLOC_FREE(smb_fname_adp);
3732 static int fruit_chown(vfs_handle_struct *handle,
3733 const struct smb_filename *smb_fname,
3738 struct fruit_config_data *config = NULL;
3739 struct smb_filename *adp_smb_fname = NULL;
3741 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3746 SMB_VFS_HANDLE_GET_DATA(handle, config,
3747 struct fruit_config_data, return -1);
3749 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3753 if (!VALID_STAT(smb_fname->st)) {
3757 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3761 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3766 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3768 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3769 if (errno == ENOENT) {
3774 TALLOC_FREE(adp_smb_fname);
3778 static int fruit_rmdir(struct vfs_handle_struct *handle,
3779 const struct smb_filename *smb_fname)
3783 struct fruit_config_data *config;
3785 SMB_VFS_HANDLE_GET_DATA(handle, config,
3786 struct fruit_config_data, return -1);
3788 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3793 * Due to there is no way to change bDeleteVetoFiles variable
3794 * from this module, need to clean up ourselves
3797 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3802 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3804 struct adouble *ad = NULL;
3806 struct smb_filename *ad_smb_fname = NULL;
3809 match = strncmp(de->d_name,
3810 ADOUBLE_NAME_PREFIX,
3811 strlen(ADOUBLE_NAME_PREFIX));
3816 p = talloc_asprintf(talloc_tos(), "%s/%s",
3817 smb_fname->base_name, de->d_name);
3819 DBG_ERR("talloc_asprintf failed\n");
3823 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3827 if (ad_smb_fname == NULL) {
3828 DBG_ERR("synthetic_smb_fname failed\n");
3833 * Check whether it's a valid AppleDouble file, if
3834 * yes, delete it, ignore it otherwise.
3836 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3838 TALLOC_FREE(ad_smb_fname);
3844 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3846 DBG_ERR("Deleting [%s] failed\n",
3847 smb_fname_str_dbg(ad_smb_fname));
3849 TALLOC_FREE(ad_smb_fname);
3854 SMB_VFS_CLOSEDIR(handle->conn, dh);
3856 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3859 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3860 files_struct *fsp, void *data,
3861 size_t n, off_t offset)
3866 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3872 DBG_ERR("Removing [%s] after short read [%zd]\n",
3873 fsp_str_dbg(fsp), nread);
3875 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3877 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3885 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3886 files_struct *fsp, void *data,
3887 size_t n, off_t offset)
3890 struct adouble *ad = NULL;
3891 char afpinfo_buf[AFP_INFO_SIZE];
3895 ai = afpinfo_new(talloc_tos());
3900 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3906 p = ad_get_entry(ad, ADEID_FINDERI);
3908 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3913 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3915 nread = afpinfo_pack(ai, afpinfo_buf);
3916 if (nread != AFP_INFO_SIZE) {
3921 memcpy(data, afpinfo_buf, n);
3929 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3930 files_struct *fsp, void *data,
3931 size_t n, off_t offset)
3933 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3938 * OS X has a off-by-1 error in the offset calculation, so we're
3939 * bug compatible here. It won't hurt, as any relevant real
3940 * world read requests from the AFP_AfpInfo stream will be
3941 * offset=0 n=60. offset is ignored anyway, see below.
3943 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3947 /* Yes, macOS always reads from offset 0 */
3949 to_return = MIN(n, AFP_INFO_SIZE);
3951 switch (fio->config->meta) {
3952 case FRUIT_META_STREAM:
3953 nread = fruit_pread_meta_stream(handle, fsp, data,
3957 case FRUIT_META_NETATALK:
3958 nread = fruit_pread_meta_adouble(handle, fsp, data,
3963 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3970 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3971 files_struct *fsp, void *data,
3972 size_t n, off_t offset)
3974 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3977 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3978 files_struct *fsp, void *data,
3979 size_t n, off_t offset)
3981 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3984 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3985 files_struct *fsp, void *data,
3986 size_t n, off_t offset)
3988 struct adouble *ad = NULL;
3991 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3996 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3997 offset + ad_getentryoff(ad, ADEID_RFORK));
4003 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4004 files_struct *fsp, void *data,
4005 size_t n, off_t offset)
4007 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4010 switch (fio->config->rsrc) {
4011 case FRUIT_RSRC_STREAM:
4012 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4015 case FRUIT_RSRC_ADFILE:
4016 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4019 case FRUIT_RSRC_XATTR:
4020 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4024 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4031 static ssize_t fruit_pread(vfs_handle_struct *handle,
4032 files_struct *fsp, void *data,
4033 size_t n, off_t offset)
4035 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4038 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4039 fsp_str_dbg(fsp), (intmax_t)offset, n);
4042 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4045 if (fio->type == ADOUBLE_META) {
4046 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4048 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4051 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4055 static bool fruit_must_handle_aio_stream(struct fio *fio)
4061 if ((fio->type == ADOUBLE_META) &&
4062 (fio->config->meta == FRUIT_META_NETATALK))
4067 if ((fio->type == ADOUBLE_RSRC) &&
4068 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4076 struct fruit_pread_state {
4078 struct vfs_aio_state vfs_aio_state;
4081 static void fruit_pread_done(struct tevent_req *subreq);
4083 static struct tevent_req *fruit_pread_send(
4084 struct vfs_handle_struct *handle,
4085 TALLOC_CTX *mem_ctx,
4086 struct tevent_context *ev,
4087 struct files_struct *fsp,
4089 size_t n, off_t offset)
4091 struct tevent_req *req = NULL;
4092 struct tevent_req *subreq = NULL;
4093 struct fruit_pread_state *state = NULL;
4094 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4096 req = tevent_req_create(mem_ctx, &state,
4097 struct fruit_pread_state);
4102 if (fruit_must_handle_aio_stream(fio)) {
4103 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4104 if (state->nread != n) {
4105 if (state->nread != -1) {
4108 tevent_req_error(req, errno);
4109 return tevent_req_post(req, ev);
4111 tevent_req_done(req);
4112 return tevent_req_post(req, ev);
4115 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4117 if (tevent_req_nomem(req, subreq)) {
4118 return tevent_req_post(req, ev);
4120 tevent_req_set_callback(subreq, fruit_pread_done, req);
4124 static void fruit_pread_done(struct tevent_req *subreq)
4126 struct tevent_req *req = tevent_req_callback_data(
4127 subreq, struct tevent_req);
4128 struct fruit_pread_state *state = tevent_req_data(
4129 req, struct fruit_pread_state);
4131 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4132 TALLOC_FREE(subreq);
4134 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4137 tevent_req_done(req);
4140 static ssize_t fruit_pread_recv(struct tevent_req *req,
4141 struct vfs_aio_state *vfs_aio_state)
4143 struct fruit_pread_state *state = tevent_req_data(
4144 req, struct fruit_pread_state);
4146 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4150 *vfs_aio_state = state->vfs_aio_state;
4151 return state->nread;
4154 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4155 files_struct *fsp, const void *data,
4156 size_t n, off_t offset)
4161 ai = afpinfo_unpack(talloc_tos(), data);
4166 if (ai_empty_finderinfo(ai)) {
4167 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4168 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
4169 DBG_ERR("Can't delete metadata for %s: %s\n",
4170 fsp_str_dbg(fsp), strerror(errno));
4178 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4181 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4182 files_struct *fsp, const void *data,
4183 size_t n, off_t offset)
4185 struct adouble *ad = NULL;
4190 ai = afpinfo_unpack(talloc_tos(), data);
4195 if (ai_empty_finderinfo(ai)) {
4196 ret = SMB_VFS_REMOVEXATTR(handle->conn,
4198 AFPINFO_EA_NETATALK);
4200 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
4201 DBG_ERR("Can't delete metadata for %s: %s\n",
4202 fsp_str_dbg(fsp), strerror(errno));
4209 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4211 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4216 p = ad_get_entry(ad, ADEID_FINDERI);
4218 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4223 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4225 ret = ad_fset(ad, fsp);
4227 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4236 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4237 files_struct *fsp, const void *data,
4238 size_t n, off_t offset)
4240 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4244 * Writing an all 0 blob to the metadata stream
4245 * results in the stream being removed on a macOS
4246 * server. This ensures we behave the same and it
4247 * verified by the "delete AFP_AfpInfo by writing all
4250 if (n != AFP_INFO_SIZE || offset != 0) {
4251 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4252 (intmax_t)offset, (intmax_t)n);
4256 switch (fio->config->meta) {
4257 case FRUIT_META_STREAM:
4258 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4262 case FRUIT_META_NETATALK:
4263 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4268 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4275 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4276 files_struct *fsp, const void *data,
4277 size_t n, off_t offset)
4279 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4282 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4283 files_struct *fsp, const void *data,
4284 size_t n, off_t offset)
4286 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4289 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4290 files_struct *fsp, const void *data,
4291 size_t n, off_t offset)
4293 struct adouble *ad = NULL;
4297 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4299 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4303 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4304 offset + ad_getentryoff(ad, ADEID_RFORK));
4305 if (nwritten != n) {
4306 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4307 fsp_str_dbg(fsp), nwritten, n);
4312 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4313 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4314 ret = ad_fset(ad, fsp);
4316 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4326 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4327 files_struct *fsp, const void *data,
4328 size_t n, off_t offset)
4330 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4333 switch (fio->config->rsrc) {
4334 case FRUIT_RSRC_STREAM:
4335 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4338 case FRUIT_RSRC_ADFILE:
4339 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4342 case FRUIT_RSRC_XATTR:
4343 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4347 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4354 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4355 files_struct *fsp, const void *data,
4356 size_t n, off_t offset)
4358 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4361 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4362 fsp_str_dbg(fsp), (intmax_t)offset, n);
4365 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4368 if (fio->type == ADOUBLE_META) {
4369 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4371 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4374 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4378 struct fruit_pwrite_state {
4380 struct vfs_aio_state vfs_aio_state;
4383 static void fruit_pwrite_done(struct tevent_req *subreq);
4385 static struct tevent_req *fruit_pwrite_send(
4386 struct vfs_handle_struct *handle,
4387 TALLOC_CTX *mem_ctx,
4388 struct tevent_context *ev,
4389 struct files_struct *fsp,
4391 size_t n, off_t offset)
4393 struct tevent_req *req = NULL;
4394 struct tevent_req *subreq = NULL;
4395 struct fruit_pwrite_state *state = NULL;
4396 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4398 req = tevent_req_create(mem_ctx, &state,
4399 struct fruit_pwrite_state);
4404 if (fruit_must_handle_aio_stream(fio)) {
4405 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4406 if (state->nwritten != n) {
4407 if (state->nwritten != -1) {
4410 tevent_req_error(req, errno);
4411 return tevent_req_post(req, ev);
4413 tevent_req_done(req);
4414 return tevent_req_post(req, ev);
4417 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4419 if (tevent_req_nomem(req, subreq)) {
4420 return tevent_req_post(req, ev);
4422 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4426 static void fruit_pwrite_done(struct tevent_req *subreq)
4428 struct tevent_req *req = tevent_req_callback_data(
4429 subreq, struct tevent_req);
4430 struct fruit_pwrite_state *state = tevent_req_data(
4431 req, struct fruit_pwrite_state);
4433 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4434 TALLOC_FREE(subreq);
4436 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4439 tevent_req_done(req);
4442 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4443 struct vfs_aio_state *vfs_aio_state)
4445 struct fruit_pwrite_state *state = tevent_req_data(
4446 req, struct fruit_pwrite_state);
4448 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4452 *vfs_aio_state = state->vfs_aio_state;
4453 return state->nwritten;
4457 * Helper to stat/lstat the base file of an smb_fname.
4459 static int fruit_stat_base(vfs_handle_struct *handle,
4460 struct smb_filename *smb_fname,
4463 char *tmp_stream_name;
4466 tmp_stream_name = smb_fname->stream_name;
4467 smb_fname->stream_name = NULL;
4469 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4471 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4473 smb_fname->stream_name = tmp_stream_name;
4477 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4478 struct smb_filename *smb_fname,
4484 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4486 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4492 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4493 struct smb_filename *smb_fname,
4496 struct adouble *ad = NULL;
4498 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4500 DBG_INFO("fruit_stat_meta %s: %s\n",
4501 smb_fname_str_dbg(smb_fname), strerror(errno));
4507 /* Populate the stat struct with info from the base file. */
4508 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4511 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4512 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4513 smb_fname->stream_name);
4517 static int fruit_stat_meta(vfs_handle_struct *handle,
4518 struct smb_filename *smb_fname,
4521 struct fruit_config_data *config = NULL;
4524 SMB_VFS_HANDLE_GET_DATA(handle, config,
4525 struct fruit_config_data, return -1);
4527 switch (config->meta) {
4528 case FRUIT_META_STREAM:
4529 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4532 case FRUIT_META_NETATALK:
4533 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4537 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4544 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4545 struct smb_filename *smb_fname,
4548 struct adouble *ad = NULL;
4551 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4557 /* Populate the stat struct with info from the base file. */
4558 ret = fruit_stat_base(handle, smb_fname, follow_links);
4564 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4565 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4566 smb_fname->stream_name);
4571 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4572 struct smb_filename *smb_fname,
4578 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4580 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4586 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4587 struct smb_filename *smb_fname,
4590 #ifdef HAVE_ATTROPEN
4594 /* Populate the stat struct with info from the base file. */
4595 ret = fruit_stat_base(handle, smb_fname, follow_links);
4600 fd = attropen(smb_fname->base_name,
4601 AFPRESOURCE_EA_NETATALK,
4607 ret = sys_fstat(fd, &smb_fname->st, false);
4610 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4611 AFPRESOURCE_EA_NETATALK);
4617 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4618 smb_fname->stream_name);
4628 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4629 struct smb_filename *smb_fname,
4632 struct fruit_config_data *config = NULL;
4635 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4637 SMB_VFS_HANDLE_GET_DATA(handle, config,
4638 struct fruit_config_data, return -1);
4640 switch (config->rsrc) {
4641 case FRUIT_RSRC_STREAM:
4642 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4645 case FRUIT_RSRC_XATTR:
4646 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4649 case FRUIT_RSRC_ADFILE:
4650 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4654 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4661 static int fruit_stat(vfs_handle_struct *handle,
4662 struct smb_filename *smb_fname)
4666 DEBUG(10, ("fruit_stat called for %s\n",
4667 smb_fname_str_dbg(smb_fname)));
4669 if (!is_ntfs_stream_smb_fname(smb_fname)
4670 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4671 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4673 update_btime(handle, smb_fname);
4679 * Note if lp_posix_paths() is true, we can never
4680 * get here as is_ntfs_stream_smb_fname() is
4681 * always false. So we never need worry about
4682 * not following links here.
4685 if (is_afpinfo_stream(smb_fname)) {
4686 rc = fruit_stat_meta(handle, smb_fname, true);
4687 } else if (is_afpresource_stream(smb_fname)) {
4688 rc = fruit_stat_rsrc(handle, smb_fname, true);
4690 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4694 update_btime(handle, smb_fname);
4695 smb_fname->st.st_ex_mode &= ~S_IFMT;
4696 smb_fname->st.st_ex_mode |= S_IFREG;
4697 smb_fname->st.st_ex_blocks =
4698 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4703 static int fruit_lstat(vfs_handle_struct *handle,
4704 struct smb_filename *smb_fname)
4708 DEBUG(10, ("fruit_lstat called for %s\n",
4709 smb_fname_str_dbg(smb_fname)));
4711 if (!is_ntfs_stream_smb_fname(smb_fname)
4712 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4713 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4715 update_btime(handle, smb_fname);
4720 if (is_afpinfo_stream(smb_fname)) {
4721 rc = fruit_stat_meta(handle, smb_fname, false);
4722 } else if (is_afpresource_stream(smb_fname)) {
4723 rc = fruit_stat_rsrc(handle, smb_fname, false);
4725 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4729 update_btime(handle, smb_fname);
4730 smb_fname->st.st_ex_mode &= ~S_IFMT;
4731 smb_fname->st.st_ex_mode |= S_IFREG;
4732 smb_fname->st.st_ex_blocks =
4733 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4738 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4740 SMB_STRUCT_STAT *sbuf)
4742 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4745 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4747 SMB_STRUCT_STAT *sbuf)
4751 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4756 *sbuf = fsp->base_fsp->fsp_name->st;
4757 sbuf->st_ex_size = AFP_INFO_SIZE;
4758 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4763 static int fruit_fstat_meta(vfs_handle_struct *handle,
4765 SMB_STRUCT_STAT *sbuf,
4770 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4772 switch (fio->config->meta) {
4773 case FRUIT_META_STREAM:
4774 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4777 case FRUIT_META_NETATALK:
4778 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4782 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4786 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4790 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4792 SMB_STRUCT_STAT *sbuf)
4794 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4797 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4799 SMB_STRUCT_STAT *sbuf)
4801 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4804 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4806 SMB_STRUCT_STAT *sbuf)
4808 struct adouble *ad = NULL;
4811 /* Populate the stat struct with info from the base file. */
4812 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4817 ad = ad_get(talloc_tos(), handle,
4818 fsp->base_fsp->fsp_name,
4821 DBG_ERR("ad_get [%s] failed [%s]\n",
4822 fsp_str_dbg(fsp), strerror(errno));
4826 *sbuf = fsp->base_fsp->fsp_name->st;
4827 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4828 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4834 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4835 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4839 switch (fio->config->rsrc) {
4840 case FRUIT_RSRC_STREAM:
4841 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4844 case FRUIT_RSRC_ADFILE:
4845 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4848 case FRUIT_RSRC_XATTR:
4849 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4853 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4860 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4861 SMB_STRUCT_STAT *sbuf)
4863 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4867 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4870 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4872 if (fio->type == ADOUBLE_META) {
4873 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4875 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4879 sbuf->st_ex_mode &= ~S_IFMT;
4880 sbuf->st_ex_mode |= S_IFREG;
4881 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4884 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4885 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4889 static NTSTATUS fruit_streaminfo_meta_stream(
4890 vfs_handle_struct *handle,
4891 struct files_struct *fsp,
4892 const struct smb_filename *smb_fname,
4893 TALLOC_CTX *mem_ctx,
4894 unsigned int *pnum_streams,
4895 struct stream_struct **pstreams)
4897 struct stream_struct *stream = *pstreams;
4898 unsigned int num_streams = *pnum_streams;
4899 struct smb_filename *sname = NULL;
4904 for (i = 0; i < num_streams; i++) {
4905 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4910 if (i == num_streams) {
4911 return NT_STATUS_OK;
4914 if (stream[i].size == AFP_INFO_SIZE) {
4915 return NT_STATUS_OK;
4918 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4919 "from [%s]\n", (intmax_t)stream[i].size,
4920 smb_fname_str_dbg(smb_fname));
4922 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4924 return NT_STATUS_INTERNAL_ERROR;
4927 sname = synthetic_smb_fname(talloc_tos(),
4928 smb_fname->base_name,
4929 AFPINFO_STREAM_NAME,
4931 if (sname == NULL) {
4932 return NT_STATUS_NO_MEMORY;
4935 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4938 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4939 return map_nt_error_from_unix(errno);
4942 return NT_STATUS_OK;
4945 static NTSTATUS fruit_streaminfo_meta_netatalk(
4946 vfs_handle_struct *handle,
4947 struct files_struct *fsp,
4948 const struct smb_filename *smb_fname,
4949 TALLOC_CTX *mem_ctx,
4950 unsigned int *pnum_streams,
4951 struct stream_struct **pstreams)
4953 struct stream_struct *stream = *pstreams;
4954 unsigned int num_streams = *pnum_streams;
4955 struct adouble *ad = NULL;
4960 /* Remove the Netatalk xattr from the list */
4961 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4962 ":" NETATALK_META_XATTR ":$DATA");
4964 return NT_STATUS_NO_MEMORY;
4968 * Check if there's a AFPINFO_STREAM from the VFS streams
4969 * backend and if yes, remove it from the list
4971 for (i = 0; i < num_streams; i++) {
4972 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4977 if (i < num_streams) {
4978 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4979 smb_fname_str_dbg(smb_fname));
4981 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4984 return NT_STATUS_INTERNAL_ERROR;
4988 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4990 return NT_STATUS_OK;
4993 is_fi_empty = ad_empty_finderinfo(ad);
4997 return NT_STATUS_OK;
5000 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5001 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5002 smb_roundup(handle->conn, AFP_INFO_SIZE));
5004 return NT_STATUS_NO_MEMORY;
5007 return NT_STATUS_OK;
5010 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5011 struct files_struct *fsp,
5012 const struct smb_filename *smb_fname,
5013 TALLOC_CTX *mem_ctx,
5014 unsigned int *pnum_streams,
5015 struct stream_struct **pstreams)
5017 struct fruit_config_data *config = NULL;
5020 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5021 return NT_STATUS_INTERNAL_ERROR);
5023 switch (config->meta) {
5024 case FRUIT_META_NETATALK:
5025 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5026 mem_ctx, pnum_streams,
5030 case FRUIT_META_STREAM:
5031 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5032 mem_ctx, pnum_streams,
5037 return NT_STATUS_INTERNAL_ERROR;
5043 static NTSTATUS fruit_streaminfo_rsrc_stream(
5044 vfs_handle_struct *handle,
5045 struct files_struct *fsp,
5046 const struct smb_filename *smb_fname,
5047 TALLOC_CTX *mem_ctx,
5048 unsigned int *pnum_streams,
5049 struct stream_struct **pstreams)
5053 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5055 DBG_ERR("Filtering resource stream failed\n");
5056 return NT_STATUS_INTERNAL_ERROR;
5058 return NT_STATUS_OK;
5061 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5062 vfs_handle_struct *handle,
5063 struct files_struct *fsp,
5064 const struct smb_filename *smb_fname,
5065 TALLOC_CTX *mem_ctx,
5066 unsigned int *pnum_streams,
5067 struct stream_struct **pstreams)
5071 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5073 DBG_ERR("Filtering resource stream failed\n");
5074 return NT_STATUS_INTERNAL_ERROR;
5076 return NT_STATUS_OK;
5079 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5080 vfs_handle_struct *handle,
5081 struct files_struct *fsp,
5082 const struct smb_filename *smb_fname,
5083 TALLOC_CTX *mem_ctx,
5084 unsigned int *pnum_streams,
5085 struct stream_struct **pstreams)
5087 struct stream_struct *stream = *pstreams;
5088 unsigned int num_streams = *pnum_streams;
5089 struct adouble *ad = NULL;
5095 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5096 * and if yes, remove it from the list
5098 for (i = 0; i < num_streams; i++) {
5099 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5104 if (i < num_streams) {
5105 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5106 smb_fname_str_dbg(smb_fname));
5108 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5109 AFPRESOURCE_STREAM);
5111 return NT_STATUS_INTERNAL_ERROR;
5115 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5117 return NT_STATUS_OK;
5120 rlen = ad_getentrylen(ad, ADEID_RFORK);
5124 return NT_STATUS_OK;
5127 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5128 AFPRESOURCE_STREAM_NAME, rlen,
5129 smb_roundup(handle->conn, rlen));
5131 return NT_STATUS_NO_MEMORY;
5134 return NT_STATUS_OK;
5137 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5138 struct files_struct *fsp,
5139 const struct smb_filename *smb_fname,
5140 TALLOC_CTX *mem_ctx,
5141 unsigned int *pnum_streams,
5142 struct stream_struct **pstreams)
5144 struct fruit_config_data *config = NULL;
5147 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5148 return NT_STATUS_INTERNAL_ERROR);
5150 switch (config->rsrc) {
5151 case FRUIT_RSRC_STREAM:
5152 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5153 mem_ctx, pnum_streams,
5157 case FRUIT_RSRC_XATTR:
5158 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5159 mem_ctx, pnum_streams,
5163 case FRUIT_RSRC_ADFILE:
5164 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5165 mem_ctx, pnum_streams,
5170 return NT_STATUS_INTERNAL_ERROR;
5176 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5177 struct files_struct *fsp,
5178 const struct smb_filename *smb_fname,
5179 TALLOC_CTX *mem_ctx,
5180 unsigned int *pnum_streams,
5181 struct stream_struct **pstreams)
5183 struct fruit_config_data *config = NULL;
5186 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5187 return NT_STATUS_UNSUCCESSFUL);
5189 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5191 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5192 pnum_streams, pstreams);
5193 if (!NT_STATUS_IS_OK(status)) {
5197 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5198 mem_ctx, pnum_streams, pstreams);
5199 if (!NT_STATUS_IS_OK(status)) {
5203 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5204 mem_ctx, pnum_streams, pstreams);
5205 if (!NT_STATUS_IS_OK(status)) {
5209 return NT_STATUS_OK;
5212 static int fruit_ntimes(vfs_handle_struct *handle,
5213 const struct smb_filename *smb_fname,
5214 struct smb_file_time *ft)
5217 struct adouble *ad = NULL;
5218 struct fruit_config_data *config = NULL;
5220 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5223 if ((config->meta != FRUIT_META_NETATALK) ||
5224 null_timespec(ft->create_time))
5226 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5229 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5230 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5232 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5237 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5238 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5240 rc = ad_set(ad, smb_fname);
5246 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5249 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5252 static int fruit_fallocate(struct vfs_handle_struct *handle,
5253 struct files_struct *fsp,
5258 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5261 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5264 /* Let the pwrite code path handle it. */
5269 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5270 struct files_struct *fsp,
5274 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5277 #ifdef HAVE_ATTROPEN
5278 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5283 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5284 struct files_struct *fsp,
5288 struct adouble *ad = NULL;
5291 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5293 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5294 fsp_str_dbg(fsp), strerror(errno));
5298 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5300 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5306 ad_setentrylen(ad, ADEID_RFORK, offset);
5308 rc = ad_fset(ad, fsp);
5310 DBG_ERR("ad_fset [%s] failed [%s]\n",
5311 fsp_str_dbg(fsp), strerror(errno));
5320 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5321 struct files_struct *fsp,
5325 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5328 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5331 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5332 struct files_struct *fsp,
5335 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5338 switch (fio->config->rsrc) {
5339 case FRUIT_RSRC_XATTR:
5340 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5343 case FRUIT_RSRC_ADFILE:
5344 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5347 case FRUIT_RSRC_STREAM:
5348 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5352 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5360 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5361 struct files_struct *fsp,
5365 DBG_WARNING("ftruncate %s to %jd",
5366 fsp_str_dbg(fsp), (intmax_t)offset);
5367 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5372 /* OS X returns success but does nothing */
5373 DBG_INFO("ignoring ftruncate %s to %jd\n",
5374 fsp_str_dbg(fsp), (intmax_t)offset);
5378 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5379 struct files_struct *fsp,
5382 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5385 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5389 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5392 if (fio->type == ADOUBLE_META) {
5393 ret = fruit_ftruncate_meta(handle, fsp, offset);
5395 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5398 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5402 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5403 struct smb_request *req,
5404 uint16_t root_dir_fid,
5405 struct smb_filename *smb_fname,
5406 uint32_t access_mask,
5407 uint32_t share_access,
5408 uint32_t create_disposition,
5409 uint32_t create_options,
5410 uint32_t file_attributes,
5411 uint32_t oplock_request,
5412 struct smb2_lease *lease,
5413 uint64_t allocation_size,
5414 uint32_t private_flags,
5415 struct security_descriptor *sd,
5416 struct ea_list *ea_list,
5417 files_struct **result,
5419 const struct smb2_create_blobs *in_context_blobs,
5420 struct smb2_create_blobs *out_context_blobs)
5423 struct fruit_config_data *config = NULL;
5424 files_struct *fsp = NULL;
5426 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5427 if (!NT_STATUS_IS_OK(status)) {
5431 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5432 return NT_STATUS_UNSUCCESSFUL);
5434 status = SMB_VFS_NEXT_CREATE_FILE(
5435 handle, req, root_dir_fid, smb_fname,
5436 access_mask, share_access,
5437 create_disposition, create_options,
5438 file_attributes, oplock_request,
5440 allocation_size, private_flags,
5441 sd, ea_list, result,
5442 pinfo, in_context_blobs, out_context_blobs);
5443 if (!NT_STATUS_IS_OK(status)) {
5449 if (global_fruit_config.nego_aapl) {
5450 if (config->posix_rename && fsp->is_directory) {
5452 * Enable POSIX directory rename behaviour
5454 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5459 * If this is a plain open for existing files, opening an 0
5460 * byte size resource fork MUST fail with
5461 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5463 * Cf the vfs_fruit torture tests in test_rfork_create().
5465 if (is_afpresource_stream(fsp->fsp_name) &&
5466 create_disposition == FILE_OPEN)
5468 if (fsp->fsp_name->st.st_ex_size == 0) {
5469 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5474 if (is_ntfs_stream_smb_fname(smb_fname)
5475 || fsp->is_directory) {
5479 if (config->locking == FRUIT_LOCKING_NETATALK) {
5480 status = fruit_check_access(
5483 map_share_mode_to_deny_mode(share_access, 0));
5484 if (!NT_STATUS_IS_OK(status)) {
5492 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5495 close_file(req, fsp, ERROR_CLOSE);
5496 *result = fsp = NULL;
5502 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5503 const struct smb_filename *fname,
5504 TALLOC_CTX *mem_ctx,
5505 struct readdir_attr_data **pattr_data)
5507 struct fruit_config_data *config = NULL;
5508 struct readdir_attr_data *attr_data;
5511 SMB_VFS_HANDLE_GET_DATA(handle, config,
5512 struct fruit_config_data,
5513 return NT_STATUS_UNSUCCESSFUL);
5515 if (!global_fruit_config.nego_aapl) {
5516 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5519 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5521 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5522 if (*pattr_data == NULL) {
5523 return NT_STATUS_UNSUCCESSFUL;
5525 attr_data = *pattr_data;
5526 attr_data->type = RDATTR_AAPL;
5529 * Mac metadata: compressed FinderInfo, resource fork length
5532 status = readdir_attr_macmeta(handle, fname, attr_data);
5533 if (!NT_STATUS_IS_OK(status)) {
5535 * Error handling is tricky: if we return failure from
5536 * this function, the corresponding directory entry
5537 * will to be passed to the client, so we really just
5538 * want to error out on fatal errors.
5540 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5548 if (config->unix_info_enabled) {
5549 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5555 if (!config->readdir_attr_max_access) {
5556 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5558 status = smbd_calculate_access_mask(
5562 SEC_FLAG_MAXIMUM_ALLOWED,
5563 &attr_data->attr_data.aapl.max_access);
5564 if (!NT_STATUS_IS_OK(status)) {
5569 return NT_STATUS_OK;
5572 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5573 fname->base_name, nt_errstr(status)));
5574 TALLOC_FREE(*pattr_data);
5578 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5580 uint32_t security_info,
5581 TALLOC_CTX *mem_ctx,
5582 struct security_descriptor **ppdesc)
5585 struct security_ace ace;
5587 struct fruit_config_data *config;
5589 SMB_VFS_HANDLE_GET_DATA(handle, config,
5590 struct fruit_config_data,
5591 return NT_STATUS_UNSUCCESSFUL);
5593 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5595 if (!NT_STATUS_IS_OK(status)) {
5600 * Add MS NFS style ACEs with uid, gid and mode
5602 if (!global_fruit_config.nego_aapl) {
5603 return NT_STATUS_OK;
5605 if (!config->unix_info_enabled) {
5606 return NT_STATUS_OK;
5609 /* MS NFS style mode */
5610 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5611 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5612 status = security_descriptor_dacl_add(*ppdesc, &ace);
5613 if (!NT_STATUS_IS_OK(status)) {
5614 DEBUG(1,("failed to add MS NFS style ACE\n"));
5618 /* MS NFS style uid */
5619 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5620 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5621 status = security_descriptor_dacl_add(*ppdesc, &ace);
5622 if (!NT_STATUS_IS_OK(status)) {
5623 DEBUG(1,("failed to add MS NFS style ACE\n"));
5627 /* MS NFS style gid */
5628 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5629 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5630 status = security_descriptor_dacl_add(*ppdesc, &ace);
5631 if (!NT_STATUS_IS_OK(status)) {
5632 DEBUG(1,("failed to add MS NFS style ACE\n"));
5636 return NT_STATUS_OK;
5639 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5641 uint32_t security_info_sent,
5642 const struct security_descriptor *psd)
5646 mode_t ms_nfs_mode = 0;
5649 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5651 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5652 if (!NT_STATUS_IS_OK(status)) {
5653 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5657 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5658 if (!NT_STATUS_IS_OK(status)) {
5659 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5664 if (fsp->fh->fd != -1) {
5665 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5667 result = SMB_VFS_CHMOD(fsp->conn,
5673 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5674 result, (unsigned)ms_nfs_mode,
5676 status = map_nt_error_from_unix(errno);
5681 return NT_STATUS_OK;
5684 static struct vfs_offload_ctx *fruit_offload_ctx;
5686 struct fruit_offload_read_state {
5687 struct vfs_handle_struct *handle;
5688 struct tevent_context *ev;
5694 static void fruit_offload_read_done(struct tevent_req *subreq);
5696 static struct tevent_req *fruit_offload_read_send(
5697 TALLOC_CTX *mem_ctx,
5698 struct tevent_context *ev,
5699 struct vfs_handle_struct *handle,
5706 struct tevent_req *req = NULL;
5707 struct tevent_req *subreq = NULL;
5708 struct fruit_offload_read_state *state = NULL;
5710 req = tevent_req_create(mem_ctx, &state,
5711 struct fruit_offload_read_state);
5715 *state = (struct fruit_offload_read_state) {
5722 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5723 fsctl, ttl, offset, to_copy);
5724 if (tevent_req_nomem(subreq, req)) {
5725 return tevent_req_post(req, ev);
5727 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5731 static void fruit_offload_read_done(struct tevent_req *subreq)
5733 struct tevent_req *req = tevent_req_callback_data(
5734 subreq, struct tevent_req);
5735 struct fruit_offload_read_state *state = tevent_req_data(
5736 req, struct fruit_offload_read_state);
5739 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5743 TALLOC_FREE(subreq);
5744 if (tevent_req_nterror(req, status)) {
5748 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5749 tevent_req_done(req);
5753 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5754 &fruit_offload_ctx);
5755 if (tevent_req_nterror(req, status)) {
5759 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5762 if (tevent_req_nterror(req, status)) {
5766 tevent_req_done(req);
5770 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5771 struct vfs_handle_struct *handle,
5772 TALLOC_CTX *mem_ctx,
5775 struct fruit_offload_read_state *state = tevent_req_data(
5776 req, struct fruit_offload_read_state);
5779 if (tevent_req_is_nterror(req, &status)) {
5780 tevent_req_received(req);
5784 token->length = state->token.length;
5785 token->data = talloc_move(mem_ctx, &state->token.data);
5787 tevent_req_received(req);
5788 return NT_STATUS_OK;
5791 struct fruit_offload_write_state {
5792 struct vfs_handle_struct *handle;
5794 struct files_struct *src_fsp;
5795 struct files_struct *dst_fsp;
5799 static void fruit_offload_write_done(struct tevent_req *subreq);
5800 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
5801 TALLOC_CTX *mem_ctx,
5802 struct tevent_context *ev,
5805 off_t transfer_offset,
5806 struct files_struct *dest_fsp,
5810 struct tevent_req *req, *subreq;
5811 struct fruit_offload_write_state *state;
5813 struct fruit_config_data *config;
5814 off_t src_off = transfer_offset;
5815 files_struct *src_fsp = NULL;
5816 off_t to_copy = num;
5817 bool copyfile_enabled = false;
5819 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5820 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5822 SMB_VFS_HANDLE_GET_DATA(handle, config,
5823 struct fruit_config_data,
5826 req = tevent_req_create(mem_ctx, &state,
5827 struct fruit_offload_write_state);
5831 state->handle = handle;
5832 state->dst_fsp = dest_fsp;
5835 case FSCTL_SRV_COPYCHUNK:
5836 case FSCTL_SRV_COPYCHUNK_WRITE:
5837 copyfile_enabled = config->copyfile_enabled;
5844 * Check if this a OS X copyfile style copychunk request with
5845 * a requested chunk count of 0 that was translated to a
5846 * offload_write_send VFS call overloading the parameters src_off
5847 * = dest_off = num = 0.
5849 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
5850 status = vfs_offload_token_db_fetch_fsp(
5851 fruit_offload_ctx, token, &src_fsp);
5852 if (tevent_req_nterror(req, status)) {
5853 return tevent_req_post(req, ev);
5855 state->src_fsp = src_fsp;
5857 status = vfs_stat_fsp(src_fsp);
5858 if (tevent_req_nterror(req, status)) {
5859 return tevent_req_post(req, ev);
5862 to_copy = src_fsp->fsp_name->st.st_ex_size;
5863 state->is_copyfile = true;
5866 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
5875 if (tevent_req_nomem(subreq, req)) {
5876 return tevent_req_post(req, ev);
5879 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
5883 static void fruit_offload_write_done(struct tevent_req *subreq)
5885 struct tevent_req *req = tevent_req_callback_data(
5886 subreq, struct tevent_req);
5887 struct fruit_offload_write_state *state = tevent_req_data(
5888 req, struct fruit_offload_write_state);
5890 unsigned int num_streams = 0;
5891 struct stream_struct *streams = NULL;
5893 struct smb_filename *src_fname_tmp = NULL;
5894 struct smb_filename *dst_fname_tmp = NULL;
5896 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
5899 TALLOC_FREE(subreq);
5900 if (tevent_req_nterror(req, status)) {
5904 if (!state->is_copyfile) {
5905 tevent_req_done(req);
5910 * Now copy all remaining streams. We know the share supports
5911 * streams, because we're in vfs_fruit. We don't do this async
5912 * because streams are few and small.
5914 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5915 state->src_fsp->fsp_name,
5916 req, &num_streams, &streams);
5917 if (tevent_req_nterror(req, status)) {
5921 if (num_streams == 1) {
5922 /* There is always one stream, ::$DATA. */
5923 tevent_req_done(req);
5927 for (i = 0; i < num_streams; i++) {
5928 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5929 __func__, streams[i].name, (size_t)streams[i].size));
5931 src_fname_tmp = synthetic_smb_fname(
5933 state->src_fsp->fsp_name->base_name,
5936 state->src_fsp->fsp_name->flags);
5937 if (tevent_req_nomem(src_fname_tmp, req)) {
5941 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5942 TALLOC_FREE(src_fname_tmp);
5946 dst_fname_tmp = synthetic_smb_fname(
5948 state->dst_fsp->fsp_name->base_name,
5951 state->dst_fsp->fsp_name->flags);
5952 if (tevent_req_nomem(dst_fname_tmp, req)) {
5953 TALLOC_FREE(src_fname_tmp);
5957 status = copy_file(req,
5958 state->handle->conn,
5961 OPENX_FILE_CREATE_IF_NOT_EXIST,
5963 if (!NT_STATUS_IS_OK(status)) {
5964 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5965 smb_fname_str_dbg(src_fname_tmp),
5966 smb_fname_str_dbg(dst_fname_tmp),
5967 nt_errstr(status)));
5968 TALLOC_FREE(src_fname_tmp);
5969 TALLOC_FREE(dst_fname_tmp);
5970 tevent_req_nterror(req, status);
5974 TALLOC_FREE(src_fname_tmp);
5975 TALLOC_FREE(dst_fname_tmp);
5978 TALLOC_FREE(streams);
5979 TALLOC_FREE(src_fname_tmp);
5980 TALLOC_FREE(dst_fname_tmp);
5981 tevent_req_done(req);
5984 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
5985 struct tevent_req *req,
5988 struct fruit_offload_write_state *state = tevent_req_data(
5989 req, struct fruit_offload_write_state);
5992 if (tevent_req_is_nterror(req, &status)) {
5993 DEBUG(1, ("server side copy chunk failed: %s\n",
5994 nt_errstr(status)));
5996 tevent_req_received(req);
6000 *copied = state->copied;
6001 tevent_req_received(req);
6003 return NT_STATUS_OK;
6006 static struct vfs_fn_pointers vfs_fruit_fns = {
6007 .connect_fn = fruit_connect,
6009 /* File operations */
6010 .chmod_fn = fruit_chmod,
6011 .chown_fn = fruit_chown,
6012 .unlink_fn = fruit_unlink,
6013 .rename_fn = fruit_rename,
6014 .rmdir_fn = fruit_rmdir,
6015 .open_fn = fruit_open,
6016 .pread_fn = fruit_pread,
6017 .pwrite_fn = fruit_pwrite,
6018 .pread_send_fn = fruit_pread_send,
6019 .pread_recv_fn = fruit_pread_recv,
6020 .pwrite_send_fn = fruit_pwrite_send,
6021 .pwrite_recv_fn = fruit_pwrite_recv,
6022 .stat_fn = fruit_stat,
6023 .lstat_fn = fruit_lstat,
6024 .fstat_fn = fruit_fstat,
6025 .streaminfo_fn = fruit_streaminfo,
6026 .ntimes_fn = fruit_ntimes,
6027 .ftruncate_fn = fruit_ftruncate,
6028 .fallocate_fn = fruit_fallocate,
6029 .create_file_fn = fruit_create_file,
6030 .readdir_attr_fn = fruit_readdir_attr,
6031 .offload_read_send_fn = fruit_offload_read_send,
6032 .offload_read_recv_fn = fruit_offload_read_recv,
6033 .offload_write_send_fn = fruit_offload_write_send,
6034 .offload_write_recv_fn = fruit_offload_write_recv,
6036 /* NT ACL operations */
6037 .fget_nt_acl_fn = fruit_fget_nt_acl,
6038 .fset_nt_acl_fn = fruit_fset_nt_acl,
6042 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6044 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6046 if (!NT_STATUS_IS_OK(ret)) {
6050 vfs_fruit_debug_level = debug_add_class("fruit");
6051 if (vfs_fruit_debug_level == -1) {
6052 vfs_fruit_debug_level = DBGC_VFS;
6053 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6056 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6057 "vfs_fruit_init","fruit",vfs_fruit_debug_level));