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;
144 off_t time_machine_max_size;
145 bool wipe_intentionally_left_blank_rfork;
148 * Additional options, all enabled by default,
149 * possibly useful for analyzing performance. The associated
150 * operations with each of them may be expensive, so having
151 * the chance to disable them individually gives a chance
152 * tweaking the setup for the particular usecase.
154 bool readdir_attr_rsize;
155 bool readdir_attr_finder_info;
156 bool readdir_attr_max_access;
159 static const struct enum_list fruit_rsrc[] = {
160 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
161 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
162 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
166 static const struct enum_list fruit_meta[] = {
167 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
168 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
172 static const struct enum_list fruit_locking[] = {
173 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
174 {FRUIT_LOCKING_NONE, "none"},
178 static const struct enum_list fruit_encoding[] = {
179 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
180 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
184 static const char *fruit_catia_maps =
185 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
186 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
187 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
188 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
189 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
190 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
191 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
192 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
193 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
194 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
197 /*****************************************************************************
198 * Defines, functions and data structures that deal with AppleDouble
199 *****************************************************************************/
202 * There are two AppleDouble blobs we deal with:
204 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
205 * metadata in an xattr
207 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
210 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
213 #define AD_VERSION2 0x00020000
214 #define AD_VERSION AD_VERSION2
217 * AppleDouble entry IDs.
219 #define ADEID_DFORK 1
220 #define ADEID_RFORK 2
222 #define ADEID_COMMENT 4
223 #define ADEID_ICONBW 5
224 #define ADEID_ICONCOL 6
225 #define ADEID_FILEI 7
226 #define ADEID_FILEDATESI 8
227 #define ADEID_FINDERI 9
228 #define ADEID_MACFILEI 10
229 #define ADEID_PRODOSFILEI 11
230 #define ADEID_MSDOSFILEI 12
231 #define ADEID_SHORTNAME 13
232 #define ADEID_AFPFILEI 14
235 /* Private Netatalk entries */
236 #define ADEID_PRIVDEV 16
237 #define ADEID_PRIVINO 17
238 #define ADEID_PRIVSYN 18
239 #define ADEID_PRIVID 19
240 #define ADEID_MAX (ADEID_PRIVID + 1)
243 * These are the real ids for the private entries,
244 * as stored in the adouble file
246 #define AD_DEV 0x80444556
247 #define AD_INO 0x80494E4F
248 #define AD_SYN 0x8053594E
249 #define AD_ID 0x8053567E
251 /* Number of actually used entries */
252 #define ADEID_NUM_XATTR 8
253 #define ADEID_NUM_DOT_UND 2
254 #define ADEID_NUM_RSRC_XATTR 1
256 /* AppleDouble magic */
257 #define AD_APPLESINGLE_MAGIC 0x00051600
258 #define AD_APPLEDOUBLE_MAGIC 0x00051607
259 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
261 /* Sizes of relevant entry bits */
262 #define ADEDLEN_MAGIC 4
263 #define ADEDLEN_VERSION 4
264 #define ADEDLEN_FILLER 16
265 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
266 #define AD_FILLER_TAG_OSX "Mac OS X " /* should be 16 bytes */
267 #define ADEDLEN_NENTRIES 2
268 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
269 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
270 #define AD_ENTRY_LEN_EID 4
271 #define AD_ENTRY_LEN_OFF 4
272 #define AD_ENTRY_LEN_LEN 4
273 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
276 #define ADEDLEN_NAME 255
277 #define ADEDLEN_COMMENT 200
278 #define ADEDLEN_FILEI 16
279 #define ADEDLEN_FINDERI 32
280 #define ADEDLEN_FILEDATESI 16
281 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
282 #define ADEDLEN_AFPFILEI 4
283 #define ADEDLEN_MACFILEI 4
284 #define ADEDLEN_PRODOSFILEI 8
285 #define ADEDLEN_MSDOSFILEI 2
286 #define ADEDLEN_DID 4
287 #define ADEDLEN_PRIVDEV 8
288 #define ADEDLEN_PRIVINO 8
289 #define ADEDLEN_PRIVSYN 8
290 #define ADEDLEN_PRIVID 4
293 #define ADEDOFF_MAGIC 0
294 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
295 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
296 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
298 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
299 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
300 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
301 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
302 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
304 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
305 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
306 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
307 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
309 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
310 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
311 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
313 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
314 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
315 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
316 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
317 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
318 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
320 #if AD_DATASZ_XATTR != 402
321 #error bad size for AD_DATASZ_XATTR
324 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
325 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
327 #if AD_DATASZ_DOT_UND != 82
328 #error bad size for AD_DATASZ_DOT_UND
332 * Sharemode locks fcntl() offsets
334 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
335 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
337 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
339 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
341 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
342 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
343 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
344 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
345 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
346 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
347 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
348 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
349 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
350 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
352 /* Time stuff we overload the bits a little */
353 #define AD_DATE_CREATE 0
354 #define AD_DATE_MODIFY 4
355 #define AD_DATE_BACKUP 8
356 #define AD_DATE_ACCESS 12
357 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
358 AD_DATE_BACKUP | AD_DATE_ACCESS)
359 #define AD_DATE_UNIX (1 << 10)
360 #define AD_DATE_START 0x80000000
361 #define AD_DATE_DELTA 946684800
362 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
363 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
365 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
366 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
367 #define AD_XATTR_HDR_SIZE 36
368 #define AD_XATTR_MAX_HDR_SIZE 65536
370 /* Accessor macros */
371 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
372 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
373 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
374 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
377 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
378 * representation as well as the on-disk format.
380 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
381 * the length of the FinderInfo entry is larger then 32 bytes. It is then
382 * preceeded with 2 bytes padding.
384 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
387 struct ad_xattr_header {
388 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
389 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
390 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
391 uint32_t adx_data_start; /* file offset to attribute data area */
392 uint32_t adx_data_length; /* length of attribute data area */
393 uint32_t adx_reserved[3];
395 uint16_t adx_num_attrs;
398 /* On-disk entries are aligned on 4 byte boundaries */
399 struct ad_xattr_entry {
400 uint32_t adx_offset; /* file offset to data */
401 uint32_t adx_length; /* size of attribute data */
403 uint8_t adx_namelen; /* included the NULL terminator */
404 char *adx_name; /* NULL-terminated UTF-8 name */
413 vfs_handle_struct *ad_handle;
416 adouble_type_t ad_type;
419 uint8_t ad_filler[ADEDLEN_FILLER];
420 struct ad_entry ad_eid[ADEID_MAX];
422 struct ad_xattr_header adx_header;
423 struct ad_xattr_entry *adx_entries;
426 struct ad_entry_order {
427 uint32_t id, offset, len;
430 /* Netatalk AppleDouble metadata xattr */
432 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
433 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
434 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
435 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
436 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
437 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
438 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
439 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
440 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
444 /* AppleDouble resource fork file (the ones prefixed by "._") */
446 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
447 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
448 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
453 * Fake AppleDouble entry oder for resource fork xattr. The xattr
454 * isn't an AppleDouble file, it simply contains the resource data,
455 * but in order to be able to use some API calls like ad_getentryoff()
456 * we build a fake/helper struct adouble with this entry order struct.
459 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
464 /* Conversion from enumerated id to on-disk AppleDouble id */
465 #define AD_EID_DISK(a) (set_eid[a])
466 static const uint32_t set_eid[] = {
467 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
468 AD_DEV, AD_INO, AD_SYN, AD_ID
472 /* tcon config handle */
473 struct fruit_config_data *config;
475 /* Denote stream type, meta or rsrc */
480 * Forward declarations
482 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
483 adouble_type_t type);
484 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
485 static int ad_fset(struct adouble *ad, files_struct *fsp);
486 static int adouble_path(TALLOC_CTX *ctx,
487 const struct smb_filename *smb_fname__in,
488 struct smb_filename **ppsmb_fname_out);
489 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
490 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
491 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
495 * Return a pointer to an AppleDouble entry
497 * Returns NULL if the entry is not present
499 static char *ad_get_entry(const struct adouble *ad, int eid)
501 off_t off = ad_getentryoff(ad, eid);
502 size_t len = ad_getentrylen(ad, eid);
504 if (off == 0 || len == 0) {
508 return ad->ad_data + off;
514 static int ad_getdate(const struct adouble *ad,
515 unsigned int dateoff,
518 bool xlate = (dateoff & AD_DATE_UNIX);
521 dateoff &= AD_DATE_MASK;
522 p = ad_get_entry(ad, ADEID_FILEDATESI);
527 if (dateoff > AD_DATE_ACCESS) {
531 memcpy(date, p + dateoff, sizeof(uint32_t));
534 *date = AD_DATE_TO_UNIX(*date);
542 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
544 bool xlate = (dateoff & AD_DATE_UNIX);
547 p = ad_get_entry(ad, ADEID_FILEDATESI);
552 dateoff &= AD_DATE_MASK;
554 date = AD_DATE_FROM_UNIX(date);
557 if (dateoff > AD_DATE_ACCESS) {
561 memcpy(p + dateoff, &date, sizeof(date));
568 * Map on-disk AppleDouble id to enumerated id
570 static uint32_t get_eid(uint32_t eid)
578 return ADEID_PRIVDEV;
580 return ADEID_PRIVINO;
582 return ADEID_PRIVSYN;
593 * Pack AppleDouble structure into data buffer
595 static bool ad_pack(struct adouble *ad)
602 bufsize = talloc_get_size(ad->ad_data);
603 if (bufsize < AD_DATASZ_DOT_UND) {
604 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
608 if (offset + ADEDLEN_MAGIC < offset ||
609 offset + ADEDLEN_MAGIC >= bufsize) {
612 RSIVAL(ad->ad_data, offset, ad->ad_magic);
613 offset += ADEDLEN_MAGIC;
615 if (offset + ADEDLEN_VERSION < offset ||
616 offset + ADEDLEN_VERSION >= bufsize) {
619 RSIVAL(ad->ad_data, offset, ad->ad_version);
620 offset += ADEDLEN_VERSION;
622 if (offset + ADEDLEN_FILLER < offset ||
623 offset + ADEDLEN_FILLER >= bufsize) {
626 if (ad->ad_type == ADOUBLE_RSRC) {
627 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
629 offset += ADEDLEN_FILLER;
631 if (offset + ADEDLEN_NENTRIES < offset ||
632 offset + ADEDLEN_NENTRIES >= bufsize) {
635 offset += ADEDLEN_NENTRIES;
637 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
638 if (ad->ad_eid[eid].ade_off == 0) {
640 * ade_off is also used as indicator whether a
641 * specific entry is used or not
646 if (offset + AD_ENTRY_LEN_EID < offset ||
647 offset + AD_ENTRY_LEN_EID >= bufsize) {
650 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
651 offset += AD_ENTRY_LEN_EID;
653 if (offset + AD_ENTRY_LEN_OFF < offset ||
654 offset + AD_ENTRY_LEN_OFF >= bufsize) {
657 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
658 offset += AD_ENTRY_LEN_OFF;
660 if (offset + AD_ENTRY_LEN_LEN < offset ||
661 offset + AD_ENTRY_LEN_LEN >= bufsize) {
664 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
665 offset += AD_ENTRY_LEN_LEN;
670 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
673 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
678 static bool ad_unpack_xattrs(struct adouble *ad)
680 struct ad_xattr_header *h = &ad->adx_header;
681 const char *p = ad->ad_data;
685 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
689 /* 2 bytes padding */
690 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
692 h->adx_magic = RIVAL(p, hoff + 0);
693 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
694 h->adx_total_size = RIVAL(p, hoff + 8);
695 h->adx_data_start = RIVAL(p, hoff + 12);
696 h->adx_data_length = RIVAL(p, hoff + 16);
697 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
698 h->adx_num_attrs = RSVAL(p, hoff + 34);
700 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
701 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
705 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
706 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
709 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
710 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
714 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
715 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
719 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
720 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
723 if ((h->adx_data_start + h->adx_data_length) >
724 ad->adx_header.adx_total_size)
726 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
730 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
731 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
735 if (h->adx_num_attrs == 0) {
739 ad->adx_entries = talloc_zero_array(
740 ad, struct ad_xattr_entry, h->adx_num_attrs);
741 if (ad->adx_entries == NULL) {
745 hoff += AD_XATTR_HDR_SIZE;
747 for (i = 0; i < h->adx_num_attrs; i++) {
748 struct ad_xattr_entry *e = &ad->adx_entries[i];
750 hoff = (hoff + 3) & ~3;
752 e->adx_offset = RIVAL(p, hoff + 0);
753 e->adx_length = RIVAL(p, hoff + 4);
754 e->adx_flags = RSVAL(p, hoff + 8);
755 e->adx_namelen = *(p + hoff + 10);
757 if (e->adx_offset >= ad->adx_header.adx_total_size) {
758 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
763 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
764 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
769 if ((e->adx_offset + e->adx_length) >
770 ad->adx_header.adx_total_size)
772 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
777 if (e->adx_namelen == 0) {
778 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
782 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
783 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
787 if ((hoff + 11 + e->adx_namelen) >
788 ad->adx_header.adx_data_start)
790 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
795 e->adx_name = talloc_strndup(ad->adx_entries,
798 if (e->adx_name == NULL) {
802 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
803 e->adx_name, e->adx_offset, e->adx_length);
804 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
807 hoff += 11 + e->adx_namelen;
814 * Unpack an AppleDouble blob into a struct adoble
816 static bool ad_unpack(struct adouble *ad, const size_t nentries,
819 size_t bufsize = talloc_get_size(ad->ad_data);
821 uint32_t eid, len, off;
825 * The size of the buffer ad->ad_data is checked when read, so
826 * we wouldn't have to check our own offsets, a few extra
827 * checks won't hurt though. We have to check the offsets we
828 * read from the buffer anyway.
831 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
832 DEBUG(1, ("bad size\n"));
836 ad->ad_magic = RIVAL(ad->ad_data, 0);
837 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
838 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
839 DEBUG(1, ("wrong magic or version\n"));
843 memcpy(ad->ad_filler, ad->ad_data + ADEDOFF_FILLER, ADEDLEN_FILLER);
845 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
846 if (adentries != nentries) {
847 DEBUG(1, ("invalid number of entries: %zu\n",
852 /* now, read in the entry bits */
853 for (i = 0; i < adentries; i++) {
854 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
856 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
857 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
859 if (!eid || eid >= ADEID_MAX) {
860 DEBUG(1, ("bogus eid %d\n", eid));
865 * All entries other than the resource fork are
866 * expected to be read into the ad_data buffer, so
867 * ensure the specified offset is within that bound
869 if ((off > bufsize) && (eid != ADEID_RFORK)) {
870 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
876 * All entries besides FinderInfo and resource fork
877 * must fit into the buffer. FinderInfo is special as
878 * it may be larger then the default 32 bytes (if it
879 * contains marshalled xattrs), but we will fixup that
880 * in ad_convert(). And the resource fork is never
881 * accessed directly by the ad_data buf (also see
882 * comment above) anyway.
884 if ((eid != ADEID_RFORK) &&
885 (eid != ADEID_FINDERI) &&
886 ((off + len) > bufsize)) {
887 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
893 * That would be obviously broken
895 if (off > filesize) {
896 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
902 * Check for any entry that has its end beyond the
905 if (off + len < off) {
906 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
907 ", len: %" PRIu32 "\n",
912 if (off + len > filesize) {
914 * If this is the resource fork entry, we fix
915 * up the length, for any other entry we bail
918 if (eid != ADEID_RFORK) {
919 DEBUG(1, ("bogus eid %d: off: %" PRIu32
920 ", len: %" PRIu32 "\n",
926 * Fixup the resource fork entry by limiting
927 * the size to entryoffset - filesize.
929 len = filesize - off;
930 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
931 ", len: %" PRIu32 "\n", off, len));
934 ad->ad_eid[eid].ade_off = off;
935 ad->ad_eid[eid].ade_len = len;
938 ok = ad_unpack_xattrs(ad);
946 static bool ad_convert_move_reso(struct adouble *ad,
947 const struct smb_filename *smb_fname)
949 char *map = MAP_FAILED;
955 if (ad_getentrylen(ad, ADEID_RFORK) == 0) {
959 maplen = ad_getentryoff(ad, ADEID_RFORK) +
960 ad_getentrylen(ad, ADEID_RFORK);
962 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
963 map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
965 if (map == MAP_FAILED) {
966 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
971 memmove(map + ADEDOFF_RFORK_DOT_UND,
972 map + ad_getentryoff(ad, ADEID_RFORK),
973 ad_getentrylen(ad, ADEID_RFORK));
975 rc = munmap(map, maplen);
977 DBG_ERR("munmap failed: %s\n", strerror(errno));
981 ad_setentryoff(ad, ADEID_RFORK, ADEDOFF_RFORK_DOT_UND);
985 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
989 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
990 if (len != AD_DATASZ_DOT_UND) {
991 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
998 static bool ad_convert_xattr(struct adouble *ad,
999 const struct smb_filename *smb_fname,
1000 bool *converted_xattr)
1002 static struct char_mappings **string_replace_cmaps = NULL;
1003 char *map = MAP_FAILED;
1007 int saved_errno = 0;
1012 *converted_xattr = false;
1014 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1018 if (string_replace_cmaps == NULL) {
1019 const char **mappings = NULL;
1021 mappings = str_list_make_v3_const(
1022 talloc_tos(), fruit_catia_maps, NULL);
1023 if (mappings == NULL) {
1026 string_replace_cmaps = string_replace_init_map(mappings);
1027 TALLOC_FREE(mappings);
1030 maplen = ad_getentryoff(ad, ADEID_RFORK) +
1031 ad_getentrylen(ad, ADEID_RFORK);
1033 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1034 map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
1036 if (map == MAP_FAILED) {
1037 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
1041 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
1042 struct ad_xattr_entry *e = &ad->adx_entries[i];
1043 char *mapped_name = NULL;
1045 struct smb_filename *stream_name = NULL;
1046 files_struct *fsp = NULL;
1049 status = string_replace_allocate(ad->ad_handle->conn,
1051 string_replace_cmaps,
1054 vfs_translate_to_windows);
1055 if (!NT_STATUS_IS_OK(status) &&
1056 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
1058 DBG_ERR("string_replace_allocate failed\n");
1064 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
1066 if (mapped_name == NULL) {
1071 stream_name = synthetic_smb_fname(talloc_tos(),
1072 smb_fname->base_name,
1076 TALLOC_FREE(mapped_name);
1077 if (stream_name == NULL) {
1078 DBG_ERR("synthetic_smb_fname failed\n");
1083 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1085 status = SMB_VFS_CREATE_FILE(
1086 ad->ad_handle->conn, /* conn */
1088 0, /* root_dir_fid */
1089 stream_name, /* fname */
1090 FILE_GENERIC_WRITE, /* access_mask */
1091 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1092 FILE_OPEN_IF, /* create_disposition */
1093 0, /* create_options */
1094 0, /* file_attributes */
1095 INTERNAL_OPEN_ONLY, /* oplock_request */
1097 0, /* allocation_size */
1098 0, /* private_flags */
1103 NULL, NULL); /* create context */
1104 TALLOC_FREE(stream_name);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1111 nwritten = SMB_VFS_PWRITE(fsp,
1112 map + e->adx_offset,
1115 if (nwritten == -1) {
1116 DBG_ERR("SMB_VFS_PWRITE failed\n");
1117 saved_errno = errno;
1118 close_file(NULL, fsp, ERROR_CLOSE);
1119 errno = saved_errno;
1124 status = close_file(NULL, fsp, NORMAL_CLOSE);
1125 if (!NT_STATUS_IS_OK(status)) {
1132 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1136 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1140 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1141 if (len != AD_DATASZ_DOT_UND) {
1142 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1147 ok = ad_convert_move_reso(ad, smb_fname);
1152 *converted_xattr = true;
1156 rc = munmap(map, maplen);
1158 DBG_ERR("munmap failed: %s\n", strerror(errno));
1165 static bool ad_convert_finderinfo(struct adouble *ad,
1166 const struct smb_filename *smb_fname)
1171 struct smb_filename *stream_name = NULL;
1172 files_struct *fsp = NULL;
1176 int saved_errno = 0;
1179 cmp = memcmp(ad->ad_filler, AD_FILLER_TAG_OSX, ADEDLEN_FILLER);
1184 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1189 ai = afpinfo_new(talloc_tos());
1194 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1196 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1197 if (aiblob.data == NULL) {
1202 size = afpinfo_pack(ai, (char *)aiblob.data);
1204 if (size != AFP_INFO_SIZE) {
1208 stream_name = synthetic_smb_fname(talloc_tos(),
1209 smb_fname->base_name,
1213 if (stream_name == NULL) {
1214 data_blob_free(&aiblob);
1215 DBG_ERR("synthetic_smb_fname failed\n");
1219 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1221 status = SMB_VFS_CREATE_FILE(
1222 ad->ad_handle->conn, /* conn */
1224 0, /* root_dir_fid */
1225 stream_name, /* fname */
1226 FILE_GENERIC_WRITE, /* access_mask */
1227 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1228 FILE_OPEN_IF, /* create_disposition */
1229 0, /* create_options */
1230 0, /* file_attributes */
1231 INTERNAL_OPEN_ONLY, /* oplock_request */
1233 0, /* allocation_size */
1234 0, /* private_flags */
1239 NULL, NULL); /* create context */
1240 TALLOC_FREE(stream_name);
1241 if (!NT_STATUS_IS_OK(status)) {
1242 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1246 nwritten = SMB_VFS_PWRITE(fsp,
1250 if (nwritten == -1) {
1251 DBG_ERR("SMB_VFS_PWRITE failed\n");
1252 saved_errno = errno;
1253 close_file(NULL, fsp, ERROR_CLOSE);
1254 errno = saved_errno;
1258 status = close_file(NULL, fsp, NORMAL_CLOSE);
1259 if (!NT_STATUS_IS_OK(status)) {
1267 static bool ad_convert_truncate(struct adouble *ad,
1268 const struct smb_filename *smb_fname)
1273 * FIXME: direct ftruncate(), but we don't have a fsp for the
1276 rc = ftruncate(ad->ad_fd, ADEDOFF_RFORK_DOT_UND +
1277 ad_getentrylen(ad, ADEID_RFORK));
1286 * Convert from Apple's ._ file to Netatalk
1288 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1289 * bytes containing packed xattrs.
1291 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1294 static int ad_convert(struct adouble *ad,
1295 const struct smb_filename *smb_fname)
1298 bool converted_xattr = false;
1300 ok = ad_convert_xattr(ad, smb_fname, &converted_xattr);
1305 if (converted_xattr) {
1306 ok = ad_convert_truncate(ad, smb_fname);
1312 ok = ad_convert_finderinfo(ad, smb_fname);
1314 DBG_ERR("Failed to convert [%s]\n",
1315 smb_fname_str_dbg(smb_fname));
1323 * Read and parse Netatalk AppleDouble metadata xattr
1325 static ssize_t ad_read_meta(struct adouble *ad,
1326 const struct smb_filename *smb_fname)
1332 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1334 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1335 AFPINFO_EA_NETATALK, ad->ad_data,
1341 if (errno == ENOATTR) {
1347 DEBUG(2, ("error reading meta xattr: %s\n",
1353 if (ealen != AD_DATASZ_XATTR) {
1354 DEBUG(2, ("bad size %zd\n", ealen));
1360 /* Now parse entries */
1361 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1363 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1369 if (!ad_getentryoff(ad, ADEID_FINDERI)
1370 || !ad_getentryoff(ad, ADEID_COMMENT)
1371 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1372 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1373 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1374 || !ad_getentryoff(ad, ADEID_PRIVINO)
1375 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1376 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1377 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1384 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1385 smb_fname->base_name, rc));
1389 if (errno == EINVAL) {
1391 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1399 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1403 #ifdef HAVE_ATTROPEN
1404 /* FIXME: direct Solaris xattr syscall */
1405 return attropen(smb_fname->base_name,
1406 AFPRESOURCE_EA_NETATALK, flags, mode);
1413 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1419 struct smb_filename *adp_smb_fname = NULL;
1421 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1426 fd = open(adp_smb_fname->base_name, flags, mode);
1427 TALLOC_FREE(adp_smb_fname);
1432 static int ad_open_rsrc(vfs_handle_struct *handle,
1433 const struct smb_filename *smb_fname,
1437 struct fruit_config_data *config = NULL;
1440 SMB_VFS_HANDLE_GET_DATA(handle, config,
1441 struct fruit_config_data, return -1);
1443 if (config->rsrc == FRUIT_RSRC_XATTR) {
1444 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1446 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1453 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1454 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1455 * for file IO on the ._ file.
1457 static int ad_open(vfs_handle_struct *handle,
1460 const struct smb_filename *smb_fname,
1466 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1467 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1469 if (ad->ad_type == ADOUBLE_META) {
1473 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1474 ad->ad_fd = fsp->fh->fd;
1475 ad->ad_opened = false;
1479 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1483 ad->ad_opened = true;
1486 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1487 smb_fname->base_name,
1488 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1493 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1498 /* FIXME: direct sys_fstat(), don't have an fsp */
1499 ret = sys_fstat(ad->ad_fd, &st,
1500 lp_fake_directory_create_times(
1501 SNUM(ad->ad_handle->conn)));
1506 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1507 return st.st_ex_size;
1510 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1511 const struct smb_filename *smb_fname)
1513 SMB_STRUCT_STAT sbuf;
1520 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1521 SNUM(ad->ad_handle->conn)));
1527 * AppleDouble file header content and size, two cases:
1529 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1530 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1532 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1534 size = sbuf.st_ex_size;
1535 if (size > talloc_array_length(ad->ad_data)) {
1536 if (size > AD_XATTR_MAX_HDR_SIZE) {
1537 size = AD_XATTR_MAX_HDR_SIZE;
1539 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1546 len = sys_pread(ad->ad_fd, ad->ad_data,
1547 talloc_array_length(ad->ad_data), 0);
1548 if (len != talloc_array_length(ad->ad_data)) {
1549 DBG_NOTICE("%s %s: bad size: %zd\n",
1550 smb_fname->base_name, strerror(errno), len);
1554 /* Now parse entries */
1555 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1557 DBG_ERR("invalid AppleDouble resource %s\n",
1558 smb_fname->base_name);
1563 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1564 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1565 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1566 DBG_ERR("invalid AppleDouble resource %s\n",
1567 smb_fname->base_name);
1573 * Try to fixup AppleDouble files created by OS X with xattrs
1574 * appended to the ADEID_FINDERI entry.
1577 ret = ad_convert(ad, smb_fname);
1579 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1587 * Read and parse resource fork, either ._ AppleDouble file or xattr
1589 static ssize_t ad_read_rsrc(struct adouble *ad,
1590 const struct smb_filename *smb_fname)
1592 struct fruit_config_data *config = NULL;
1595 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1596 struct fruit_config_data, return -1);
1598 if (config->rsrc == FRUIT_RSRC_XATTR) {
1599 len = ad_read_rsrc_xattr(ad);
1601 len = ad_read_rsrc_adouble(ad, smb_fname);
1608 * Read and unpack an AppleDouble metadata xattr or resource
1610 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1612 switch (ad->ad_type) {
1614 return ad_read_meta(ad, smb_fname);
1616 return ad_read_rsrc(ad, smb_fname);
1622 static int adouble_destructor(struct adouble *ad)
1624 if ((ad->ad_fd != -1) && ad->ad_opened) {
1632 * Allocate a struct adouble without initialiing it
1634 * The struct is either hang of the fsp extension context or if fsp is
1637 * @param[in] ctx talloc context
1638 * @param[in] handle vfs handle
1639 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1641 * @return adouble handle
1643 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1644 adouble_type_t type)
1649 struct fruit_config_data *config;
1651 SMB_VFS_HANDLE_GET_DATA(handle, config,
1652 struct fruit_config_data, return NULL);
1656 adsize = AD_DATASZ_XATTR;
1659 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1660 adsize = AD_DATASZ_DOT_UND;
1667 ad = talloc_zero(ctx, struct adouble);
1674 ad->ad_data = talloc_zero_array(ad, char, adsize);
1675 if (ad->ad_data == NULL) {
1681 ad->ad_handle = handle;
1683 ad->ad_magic = AD_MAGIC;
1684 ad->ad_version = AD_VERSION;
1687 talloc_set_destructor(ad, adouble_destructor);
1697 * Allocate and initialize a new struct adouble
1699 * @param[in] ctx talloc context
1700 * @param[in] handle vfs handle
1701 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1703 * @return adouble handle, initialized
1705 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1706 adouble_type_t type)
1709 const struct ad_entry_order *eid;
1710 struct adouble *ad = NULL;
1711 struct fruit_config_data *config;
1712 time_t t = time(NULL);
1714 SMB_VFS_HANDLE_GET_DATA(handle, config,
1715 struct fruit_config_data, return NULL);
1719 eid = entry_order_meta_xattr;
1722 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1723 eid = entry_order_dot_und;
1725 eid = entry_order_rsrc_xattr;
1732 ad = ad_alloc(ctx, handle, type);
1738 ad->ad_eid[eid->id].ade_off = eid->offset;
1739 ad->ad_eid[eid->id].ade_len = eid->len;
1743 /* put something sane in the date fields */
1744 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1745 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1746 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1747 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1755 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1756 vfs_handle_struct *handle,
1758 const struct smb_filename *smb_fname,
1759 adouble_type_t type)
1763 struct adouble *ad = NULL;
1767 smb_fname = fsp->base_fsp->fsp_name;
1770 DEBUG(10, ("ad_get(%s) called for %s\n",
1771 type == ADOUBLE_META ? "meta" : "rsrc",
1772 smb_fname->base_name));
1774 ad = ad_alloc(ctx, handle, type);
1780 /* Try rw first so we can use the fd in ad_convert() */
1783 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1784 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1786 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1789 DBG_DEBUG("ad_open [%s] error [%s]\n",
1790 smb_fname->base_name, strerror(errno));
1795 len = ad_read(ad, smb_fname);
1797 DEBUG(10, ("error reading AppleDouble for %s\n",
1798 smb_fname->base_name));
1804 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1805 type == ADOUBLE_META ? "meta" : "rsrc",
1806 smb_fname->base_name, rc));
1815 * Return AppleDouble data for a file
1817 * @param[in] ctx talloc context
1818 * @param[in] handle vfs handle
1819 * @param[in] smb_fname pathname to file or directory
1820 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1822 * @return talloced struct adouble or NULL on error
1824 static struct adouble *ad_get(TALLOC_CTX *ctx,
1825 vfs_handle_struct *handle,
1826 const struct smb_filename *smb_fname,
1827 adouble_type_t type)
1829 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1833 * Return AppleDouble data for a file
1835 * @param[in] ctx talloc context
1836 * @param[in] handle vfs handle
1837 * @param[in] fsp fsp to use for IO
1838 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1840 * @return talloced struct adouble or NULL on error
1842 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1843 files_struct *fsp, adouble_type_t type)
1845 return ad_get_internal(ctx, handle, fsp, NULL, type);
1849 * Set AppleDouble metadata on a file or directory
1851 * @param[in] ad adouble handle
1853 * @param[in] smb_fname pathname to file or directory
1855 * @return status code, 0 means success
1857 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1862 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1864 if (ad->ad_type != ADOUBLE_META) {
1865 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1866 smb_fname->base_name);
1875 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1877 AFPINFO_EA_NETATALK,
1879 AD_DATASZ_XATTR, 0);
1881 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1887 * Set AppleDouble metadata on a file or directory
1889 * @param[in] ad adouble handle
1890 * @param[in] fsp file handle
1892 * @return status code, 0 means success
1894 static int ad_fset(struct adouble *ad, files_struct *fsp)
1900 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1903 || (fsp->fh == NULL)
1904 || (fsp->fh->fd == -1))
1906 smb_panic("bad fsp");
1914 switch (ad->ad_type) {
1916 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1918 AFPINFO_EA_NETATALK,
1920 AD_DATASZ_XATTR, 0);
1924 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1929 if (len != AD_DATASZ_DOT_UND) {
1930 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1940 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1945 /*****************************************************************************
1947 *****************************************************************************/
1949 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1951 if (strncasecmp_m(smb_fname->stream_name,
1952 AFPINFO_STREAM_NAME,
1953 strlen(AFPINFO_STREAM_NAME)) == 0) {
1959 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1961 if (strncasecmp_m(smb_fname->stream_name,
1962 AFPRESOURCE_STREAM_NAME,
1963 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1970 * Test whether stream is an Apple stream, not used atm
1973 static bool is_apple_stream(const struct smb_filename *smb_fname)
1975 if (is_afpinfo_stream(smb_fname)) {
1978 if (is_afpresource_stream(smb_fname)) {
1986 * Initialize config struct from our smb.conf config parameters
1988 static int init_fruit_config(vfs_handle_struct *handle)
1990 struct fruit_config_data *config;
1992 const char *tm_size_str = NULL;
1994 config = talloc_zero(handle->conn, struct fruit_config_data);
1996 DEBUG(1, ("talloc_zero() failed\n"));
2002 * Versions up to Samba 4.5.x had a spelling bug in the
2003 * fruit:resource option calling lp_parm_enum with
2004 * "res*s*ource" (ie two s).
2006 * In Samba 4.6 we accept both the wrong and the correct
2007 * spelling, in Samba 4.7 the bad spelling will be removed.
2009 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2010 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
2011 if (enumval == -1) {
2012 DEBUG(1, ("value for %s: resource type unknown\n",
2013 FRUIT_PARAM_TYPE_NAME));
2016 config->rsrc = (enum fruit_rsrc)enumval;
2018 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2019 "resource", fruit_rsrc, enumval);
2020 if (enumval == -1) {
2021 DEBUG(1, ("value for %s: resource type unknown\n",
2022 FRUIT_PARAM_TYPE_NAME));
2025 config->rsrc = (enum fruit_rsrc)enumval;
2027 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2028 "metadata", fruit_meta, FRUIT_META_NETATALK);
2029 if (enumval == -1) {
2030 DEBUG(1, ("value for %s: metadata type unknown\n",
2031 FRUIT_PARAM_TYPE_NAME));
2034 config->meta = (enum fruit_meta)enumval;
2036 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2037 "locking", fruit_locking, FRUIT_LOCKING_NONE);
2038 if (enumval == -1) {
2039 DEBUG(1, ("value for %s: locking type unknown\n",
2040 FRUIT_PARAM_TYPE_NAME));
2043 config->locking = (enum fruit_locking)enumval;
2045 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2046 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
2047 if (enumval == -1) {
2048 DEBUG(1, ("value for %s: encoding type unknown\n",
2049 FRUIT_PARAM_TYPE_NAME));
2052 config->encoding = (enum fruit_encoding)enumval;
2054 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2055 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
2056 FRUIT_PARAM_TYPE_NAME,
2061 config->use_aapl = lp_parm_bool(
2062 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
2064 config->time_machine = lp_parm_bool(
2065 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
2067 config->unix_info_enabled = lp_parm_bool(
2068 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
2070 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
2073 config->posix_rename = lp_parm_bool(
2074 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
2076 config->aapl_zero_file_id =
2077 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
2079 config->readdir_attr_rsize = lp_parm_bool(
2080 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
2082 config->readdir_attr_finder_info = lp_parm_bool(
2083 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
2085 config->readdir_attr_max_access = lp_parm_bool(
2086 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
2088 config->model = lp_parm_const_string(
2089 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
2091 tm_size_str = lp_parm_const_string(
2092 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2093 "time machine max size", NULL);
2094 if (tm_size_str != NULL) {
2095 config->time_machine_max_size = conv_str_size(tm_size_str);
2098 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
2099 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2100 "wipe_intentionally_left_blank_rfork", false);
2102 SMB_VFS_HANDLE_SET_DATA(handle, config,
2103 NULL, struct fruit_config_data,
2110 * Prepend "._" to a basename
2111 * Return a new struct smb_filename with stream_name == NULL.
2113 static int adouble_path(TALLOC_CTX *ctx,
2114 const struct smb_filename *smb_fname_in,
2115 struct smb_filename **pp_smb_fname_out)
2119 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2122 if (smb_fname == NULL) {
2126 /* We need streamname to be NULL */
2127 TALLOC_FREE(smb_fname->stream_name);
2129 /* And we're replacing base_name. */
2130 TALLOC_FREE(smb_fname->base_name);
2132 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2134 TALLOC_FREE(smb_fname);
2138 smb_fname->base_name = talloc_asprintf(smb_fname,
2139 "%s/._%s", parent, base);
2140 if (smb_fname->base_name == NULL) {
2141 TALLOC_FREE(smb_fname);
2145 *pp_smb_fname_out = smb_fname;
2151 * Allocate and initialize an AfpInfo struct
2153 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2155 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2159 ai->afpi_Signature = AFP_Signature;
2160 ai->afpi_Version = AFP_Version;
2161 ai->afpi_BackupTime = AD_DATE_START;
2166 * Pack an AfpInfo struct into a buffer
2168 * Buffer size must be at least AFP_INFO_SIZE
2169 * Returns size of packed buffer
2171 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2173 memset(buf, 0, AFP_INFO_SIZE);
2175 RSIVAL(buf, 0, ai->afpi_Signature);
2176 RSIVAL(buf, 4, ai->afpi_Version);
2177 RSIVAL(buf, 12, ai->afpi_BackupTime);
2178 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2180 return AFP_INFO_SIZE;
2184 * Unpack a buffer into a AfpInfo structure
2186 * Buffer size must be at least AFP_INFO_SIZE
2187 * Returns allocated AfpInfo struct
2189 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2191 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2196 ai->afpi_Signature = RIVAL(data, 0);
2197 ai->afpi_Version = RIVAL(data, 4);
2198 ai->afpi_BackupTime = RIVAL(data, 12);
2199 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2200 sizeof(ai->afpi_FinderInfo));
2202 if (ai->afpi_Signature != AFP_Signature
2203 || ai->afpi_Version != AFP_Version) {
2204 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2212 * Fake an inode number from the md5 hash of the (xattr) name
2214 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2217 unsigned char hash[16];
2221 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2222 SMB_ASSERT(upper_sname != NULL);
2225 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2226 sizeof(sbuf->st_ex_dev));
2227 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2228 sizeof(sbuf->st_ex_ino));
2229 MD5Update(&ctx, (unsigned char *)upper_sname,
2230 talloc_get_size(upper_sname)-1);
2231 MD5Final(hash, &ctx);
2233 TALLOC_FREE(upper_sname);
2235 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2236 memcpy(&result, hash, sizeof(result));
2238 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2239 sname, (unsigned long long)result));
2244 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2245 struct stream_struct **streams,
2246 const char *name, off_t size,
2249 struct stream_struct *tmp;
2251 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2257 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2258 if (tmp[*num_streams].name == NULL) {
2262 tmp[*num_streams].size = size;
2263 tmp[*num_streams].alloc_size = alloc_size;
2270 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2271 struct stream_struct **streams)
2273 struct stream_struct *tmp = *streams;
2276 if (*num_streams == 0) {
2280 for (i = 0; i < *num_streams; i++) {
2281 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2286 if (i == *num_streams) {
2290 if (tmp[i].size > 0) {
2294 TALLOC_FREE(tmp[i].name);
2295 if (*num_streams - 1 > i) {
2296 memmove(&tmp[i], &tmp[i+1],
2297 (*num_streams - i - 1) * sizeof(struct stream_struct));
2304 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2305 struct stream_struct **streams,
2308 struct stream_struct *tmp = *streams;
2311 if (*num_streams == 0) {
2315 for (i = 0; i < *num_streams; i++) {
2316 if (strequal_m(tmp[i].name, name)) {
2321 if (i == *num_streams) {
2325 TALLOC_FREE(tmp[i].name);
2326 if (*num_streams - 1 > i) {
2327 memmove(&tmp[i], &tmp[i+1],
2328 (*num_streams - i - 1) * sizeof(struct stream_struct));
2335 static bool ad_empty_finderinfo(const struct adouble *ad)
2338 char emptybuf[ADEDLEN_FINDERI] = {0};
2341 fi = ad_get_entry(ad, ADEID_FINDERI);
2343 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2347 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2351 static bool ai_empty_finderinfo(const AfpInfo *ai)
2354 char emptybuf[ADEDLEN_FINDERI] = {0};
2356 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2361 * Update btime with btime from Netatalk
2363 static void update_btime(vfs_handle_struct *handle,
2364 struct smb_filename *smb_fname)
2367 struct timespec creation_time = {0};
2369 struct fruit_config_data *config = NULL;
2371 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2374 switch (config->meta) {
2375 case FRUIT_META_STREAM:
2377 case FRUIT_META_NETATALK:
2381 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2385 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2389 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2395 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2396 update_stat_ex_create_time(&smb_fname->st, creation_time);
2402 * Map an access mask to a Netatalk single byte byte range lock
2404 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2405 uint32_t access_mask)
2409 switch (access_mask) {
2410 case FILE_READ_DATA:
2411 offset = AD_FILELOCK_OPEN_RD;
2414 case FILE_WRITE_DATA:
2415 case FILE_APPEND_DATA:
2416 offset = AD_FILELOCK_OPEN_WR;
2420 offset = AD_FILELOCK_OPEN_NONE;
2424 if (fork_type == APPLE_FORK_RSRC) {
2425 if (offset == AD_FILELOCK_OPEN_NONE) {
2426 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2436 * Map a deny mode to a Netatalk brl
2438 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2443 switch (deny_mode) {
2445 offset = AD_FILELOCK_DENY_RD;
2449 offset = AD_FILELOCK_DENY_WR;
2453 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2456 if (fork_type == APPLE_FORK_RSRC) {
2464 * Call fcntl() with an exclusive F_GETLK request in order to
2465 * determine if there's an exisiting shared lock
2467 * @return true if the requested lock was found or any error occurred
2468 * false if the lock was not found
2470 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2473 off_t offset = in_offset;
2478 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2479 if (result == false) {
2483 if (type != F_UNLCK) {
2490 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2492 uint32_t access_mask,
2495 NTSTATUS status = NT_STATUS_OK;
2496 bool open_for_reading, open_for_writing, deny_read, deny_write;
2498 bool have_read = false;
2501 /* FIXME: hardcoded data fork, add resource fork */
2502 enum apple_fork fork_type = APPLE_FORK_DATA;
2504 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2506 access_mask & FILE_READ_DATA ? "READ" :"-",
2507 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2508 deny_mode & DENY_READ ? "DENY_READ" : "-",
2509 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2511 if (fsp->fh->fd == -1) {
2512 return NT_STATUS_OK;
2515 flags = fcntl(fsp->fh->fd, F_GETFL);
2517 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2518 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2519 return map_nt_error_from_unix(errno);
2522 if (flags & (O_RDONLY|O_RDWR)) {
2524 * Applying fcntl read locks requires an fd opened for
2525 * reading. This means we won't be applying locks for
2526 * files openend write-only, but what can we do...
2532 * Check read access and deny read mode
2534 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2536 open_for_reading = test_netatalk_lock(
2537 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2539 deny_read = test_netatalk_lock(
2540 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2542 DEBUG(10, ("read: %s, deny_write: %s\n",
2543 open_for_reading == true ? "yes" : "no",
2544 deny_read == true ? "yes" : "no"));
2546 if (((access_mask & FILE_READ_DATA) && deny_read)
2547 || ((deny_mode & DENY_READ) && open_for_reading)) {
2548 return NT_STATUS_SHARING_VIOLATION;
2552 if ((access_mask & FILE_READ_DATA) && have_read) {
2553 struct byte_range_lock *br_lck = NULL;
2555 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2557 handle->conn->sconn->msg_ctx, fsp,
2558 fsp->op->global->open_persistent_id, 1, off,
2559 READ_LOCK, POSIX_LOCK, false,
2562 TALLOC_FREE(br_lck);
2564 if (!NT_STATUS_IS_OK(status)) {
2569 if ((deny_mode & DENY_READ) && have_read) {
2570 struct byte_range_lock *br_lck = NULL;
2572 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2574 handle->conn->sconn->msg_ctx, fsp,
2575 fsp->op->global->open_persistent_id, 1, off,
2576 READ_LOCK, POSIX_LOCK, false,
2579 TALLOC_FREE(br_lck);
2581 if (!NT_STATUS_IS_OK(status)) {
2588 * Check write access and deny write mode
2590 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2592 open_for_writing = test_netatalk_lock(
2593 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2595 deny_write = test_netatalk_lock(
2596 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2598 DEBUG(10, ("write: %s, deny_write: %s\n",
2599 open_for_writing == true ? "yes" : "no",
2600 deny_write == true ? "yes" : "no"));
2602 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2603 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2604 return NT_STATUS_SHARING_VIOLATION;
2608 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2609 struct byte_range_lock *br_lck = NULL;
2611 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2613 handle->conn->sconn->msg_ctx, fsp,
2614 fsp->op->global->open_persistent_id, 1, off,
2615 READ_LOCK, POSIX_LOCK, false,
2618 TALLOC_FREE(br_lck);
2620 if (!NT_STATUS_IS_OK(status)) {
2624 if ((deny_mode & DENY_WRITE) && have_read) {
2625 struct byte_range_lock *br_lck = NULL;
2627 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2629 handle->conn->sconn->msg_ctx, fsp,
2630 fsp->op->global->open_persistent_id, 1, off,
2631 READ_LOCK, POSIX_LOCK, false,
2634 TALLOC_FREE(br_lck);
2636 if (!NT_STATUS_IS_OK(status)) {
2645 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2646 struct smb_request *req,
2647 const struct smb2_create_blobs *in_context_blobs,
2648 struct smb2_create_blobs *out_context_blobs)
2650 struct fruit_config_data *config;
2652 struct smb2_create_blob *aapl = NULL;
2656 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2657 uint64_t req_bitmap, client_caps;
2658 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2662 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2663 return NT_STATUS_UNSUCCESSFUL);
2665 if (!config->use_aapl
2666 || in_context_blobs == NULL
2667 || out_context_blobs == NULL) {
2668 return NT_STATUS_OK;
2671 aapl = smb2_create_blob_find(in_context_blobs,
2672 SMB2_CREATE_TAG_AAPL);
2674 return NT_STATUS_OK;
2677 if (aapl->data.length != 24) {
2678 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2679 (uintmax_t)aapl->data.length));
2680 return NT_STATUS_INVALID_PARAMETER;
2683 cmd = IVAL(aapl->data.data, 0);
2684 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2685 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2686 return NT_STATUS_INVALID_PARAMETER;
2689 req_bitmap = BVAL(aapl->data.data, 8);
2690 client_caps = BVAL(aapl->data.data, 16);
2692 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2694 SBVAL(p, 8, req_bitmap);
2695 ok = data_blob_append(req, &blob, p, 16);
2697 return NT_STATUS_UNSUCCESSFUL;
2700 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2701 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2702 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2703 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2704 config->readdir_attr_enabled = true;
2707 if (config->use_copyfile) {
2708 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2709 config->copyfile_enabled = true;
2713 * The client doesn't set the flag, so we can't check
2714 * for it and just set it unconditionally
2716 if (config->unix_info_enabled) {
2717 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2720 SBVAL(p, 0, server_caps);
2721 ok = data_blob_append(req, &blob, p, 8);
2723 return NT_STATUS_UNSUCCESSFUL;
2727 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2728 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2736 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2743 if (config->time_machine) {
2744 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2749 ok = data_blob_append(req, &blob, p, 8);
2751 return NT_STATUS_UNSUCCESSFUL;
2755 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2756 ok = convert_string_talloc(req,
2757 CH_UNIX, CH_UTF16LE,
2758 config->model, strlen(config->model),
2761 return NT_STATUS_UNSUCCESSFUL;
2765 SIVAL(p + 4, 0, modellen);
2766 ok = data_blob_append(req, &blob, p, 8);
2769 return NT_STATUS_UNSUCCESSFUL;
2772 ok = data_blob_append(req, &blob, model, modellen);
2775 return NT_STATUS_UNSUCCESSFUL;
2779 status = smb2_create_blob_add(out_context_blobs,
2781 SMB2_CREATE_TAG_AAPL,
2783 if (NT_STATUS_IS_OK(status)) {
2784 global_fruit_config.nego_aapl = true;
2785 if (config->aapl_zero_file_id) {
2786 aapl_force_zero_file_id(handle->conn->sconn);
2793 static bool readdir_attr_meta_finderi_stream(
2794 struct vfs_handle_struct *handle,
2795 const struct smb_filename *smb_fname,
2798 struct smb_filename *stream_name = NULL;
2799 files_struct *fsp = NULL;
2804 uint8_t buf[AFP_INFO_SIZE];
2806 stream_name = synthetic_smb_fname(talloc_tos(),
2807 smb_fname->base_name,
2808 AFPINFO_STREAM_NAME,
2809 NULL, smb_fname->flags);
2810 if (stream_name == NULL) {
2814 ret = SMB_VFS_STAT(handle->conn, stream_name);
2819 status = SMB_VFS_CREATE_FILE(
2820 handle->conn, /* conn */
2822 0, /* root_dir_fid */
2823 stream_name, /* fname */
2824 FILE_READ_DATA, /* access_mask */
2825 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2827 FILE_OPEN, /* create_disposition*/
2828 0, /* create_options */
2829 0, /* file_attributes */
2830 INTERNAL_OPEN_ONLY, /* oplock_request */
2832 0, /* allocation_size */
2833 0, /* private_flags */
2838 NULL, NULL); /* create context */
2840 TALLOC_FREE(stream_name);
2842 if (!NT_STATUS_IS_OK(status)) {
2846 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2847 if (nread != AFP_INFO_SIZE) {
2848 DBG_ERR("short read [%s] [%zd/%d]\n",
2849 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2854 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2861 close_file(NULL, fsp, NORMAL_CLOSE);
2867 static bool readdir_attr_meta_finderi_netatalk(
2868 struct vfs_handle_struct *handle,
2869 const struct smb_filename *smb_fname,
2872 struct adouble *ad = NULL;
2875 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2880 p = ad_get_entry(ad, ADEID_FINDERI);
2882 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2887 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2892 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2893 const struct smb_filename *smb_fname,
2894 struct readdir_attr_data *attr_data)
2896 struct fruit_config_data *config = NULL;
2897 uint32_t date_added;
2901 SMB_VFS_HANDLE_GET_DATA(handle, config,
2902 struct fruit_config_data,
2905 switch (config->meta) {
2906 case FRUIT_META_NETATALK:
2907 ok = readdir_attr_meta_finderi_netatalk(
2908 handle, smb_fname, &ai);
2911 case FRUIT_META_STREAM:
2912 ok = readdir_attr_meta_finderi_stream(
2913 handle, smb_fname, &ai);
2917 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2922 /* Don't bother with errors, it's likely ENOENT */
2926 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2928 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2929 &ai.afpi_FinderInfo[0], 4);
2931 /* finder_creator */
2932 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2933 &ai.afpi_FinderInfo[4], 4);
2937 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2938 &ai.afpi_FinderInfo[8], 2);
2940 /* finder_ext_flags */
2941 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2942 &ai.afpi_FinderInfo[24], 2);
2945 date_added = convert_time_t_to_uint32_t(
2946 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2948 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2953 static uint64_t readdir_attr_rfork_size_adouble(
2954 struct vfs_handle_struct *handle,
2955 const struct smb_filename *smb_fname)
2957 struct adouble *ad = NULL;
2958 uint64_t rfork_size;
2960 ad = ad_get(talloc_tos(), handle, smb_fname,
2966 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2972 static uint64_t readdir_attr_rfork_size_stream(
2973 struct vfs_handle_struct *handle,
2974 const struct smb_filename *smb_fname)
2976 struct smb_filename *stream_name = NULL;
2978 uint64_t rfork_size;
2980 stream_name = synthetic_smb_fname(talloc_tos(),
2981 smb_fname->base_name,
2982 AFPRESOURCE_STREAM_NAME,
2984 if (stream_name == NULL) {
2988 ret = SMB_VFS_STAT(handle->conn, stream_name);
2990 TALLOC_FREE(stream_name);
2994 rfork_size = stream_name->st.st_ex_size;
2995 TALLOC_FREE(stream_name);
3000 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
3001 const struct smb_filename *smb_fname)
3003 struct fruit_config_data *config = NULL;
3004 uint64_t rfork_size;
3006 SMB_VFS_HANDLE_GET_DATA(handle, config,
3007 struct fruit_config_data,
3010 switch (config->rsrc) {
3011 case FRUIT_RSRC_ADFILE:
3012 case FRUIT_RSRC_XATTR:
3013 rfork_size = readdir_attr_rfork_size_adouble(handle,
3017 case FRUIT_META_STREAM:
3018 rfork_size = readdir_attr_rfork_size_stream(handle,
3023 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3031 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
3032 const struct smb_filename *smb_fname,
3033 struct readdir_attr_data *attr_data)
3035 NTSTATUS status = NT_STATUS_OK;
3036 struct fruit_config_data *config = NULL;
3039 SMB_VFS_HANDLE_GET_DATA(handle, config,
3040 struct fruit_config_data,
3041 return NT_STATUS_UNSUCCESSFUL);
3044 /* Ensure we return a default value in the creation_date field */
3045 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
3048 * Resource fork length
3051 if (config->readdir_attr_rsize) {
3052 uint64_t rfork_size;
3054 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
3055 attr_data->attr_data.aapl.rfork_size = rfork_size;
3062 if (config->readdir_attr_finder_info) {
3063 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
3065 status = NT_STATUS_INTERNAL_ERROR;
3072 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
3077 if (psd->dacl == NULL) {
3078 return NT_STATUS_OK;
3081 for (i = 0; i < psd->dacl->num_aces; i++) {
3082 /* MS NFS style mode/uid/gid */
3083 int cmp = dom_sid_compare_domain(
3084 &global_sid_Unix_NFS,
3085 &psd->dacl->aces[i].trustee);
3087 /* Normal ACE entry. */
3092 * security_descriptor_dacl_del()
3093 * *must* return NT_STATUS_OK as we know
3094 * we have something to remove.
3097 status = security_descriptor_dacl_del(psd,
3098 &psd->dacl->aces[i].trustee);
3099 if (!NT_STATUS_IS_OK(status)) {
3100 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3106 * security_descriptor_dacl_del() may delete more
3107 * then one entry subsequent to this one if the
3108 * SID matches, but we only need to ensure that
3109 * we stay looking at the same element in the array.
3113 return NT_STATUS_OK;
3116 /* Search MS NFS style ACE with UNIX mode */
3117 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3119 struct security_descriptor *psd,
3124 struct fruit_config_data *config = NULL;
3128 SMB_VFS_HANDLE_GET_DATA(handle, config,
3129 struct fruit_config_data,
3130 return NT_STATUS_UNSUCCESSFUL);
3132 if (!global_fruit_config.nego_aapl) {
3133 return NT_STATUS_OK;
3135 if (psd->dacl == NULL || !config->unix_info_enabled) {
3136 return NT_STATUS_OK;
3139 for (i = 0; i < psd->dacl->num_aces; i++) {
3140 if (dom_sid_compare_domain(
3141 &global_sid_Unix_NFS_Mode,
3142 &psd->dacl->aces[i].trustee) == 0) {
3143 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3144 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3147 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3148 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3154 * Remove any incoming virtual ACE entries generated by
3155 * fruit_fget_nt_acl().
3158 return remove_virtual_nfs_aces(psd);
3161 /****************************************************************************
3163 ****************************************************************************/
3165 static int fruit_connect(vfs_handle_struct *handle,
3166 const char *service,
3170 char *list = NULL, *newlist = NULL;
3171 struct fruit_config_data *config;
3173 DEBUG(10, ("fruit_connect\n"));
3175 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3180 rc = init_fruit_config(handle);
3185 SMB_VFS_HANDLE_GET_DATA(handle, config,
3186 struct fruit_config_data, return -1);
3188 if (config->veto_appledouble) {
3189 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3192 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3193 newlist = talloc_asprintf(
3195 "%s/" ADOUBLE_NAME_PREFIX "*/",
3197 lp_do_parameter(SNUM(handle->conn),
3202 lp_do_parameter(SNUM(handle->conn),
3204 "/" ADOUBLE_NAME_PREFIX "*/");
3210 if (config->encoding == FRUIT_ENC_NATIVE) {
3211 lp_do_parameter(SNUM(handle->conn),
3216 if (config->time_machine) {
3217 DBG_NOTICE("Enabling durable handles for Time Machine "
3218 "support on [%s]\n", service);
3219 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3220 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3221 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3222 if (!lp_strict_sync(SNUM(handle->conn))) {
3223 DBG_WARNING("Time Machine without strict sync is not "
3226 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3232 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3233 struct smb_filename *smb_fname,
3239 char afpinfo_buf[AFP_INFO_SIZE];
3240 ssize_t len, written;
3244 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3249 if (!(flags & (O_CREAT | O_TRUNC))) {
3253 ai = afpinfo_new(talloc_tos());
3259 len = afpinfo_pack(ai, afpinfo_buf);
3260 if (len != AFP_INFO_SIZE) {
3265 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3266 fsp->fh->fd = hostfd;
3268 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3271 if (written != AFP_INFO_SIZE) {
3272 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3280 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3283 int saved_errno = errno;
3285 fsp->fh->fd = hostfd;
3286 SMB_VFS_NEXT_CLOSE(handle, fsp);
3289 errno = saved_errno;
3294 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3295 struct smb_filename *smb_fname,
3302 struct adouble *ad = NULL;
3305 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3308 * Return a valid fd, but ensure any attempt to use it returns an error
3309 * (EPIPE). All operations on the smb_fname or the fsp will use path
3319 if (flags & (O_CREAT | O_TRUNC)) {
3321 * The attribute does not exist or needs to be truncated,
3322 * create an AppleDouble EA
3324 ad = ad_init(fsp, handle, ADOUBLE_META);
3330 rc = ad_set(ad, fsp->fsp_name);
3340 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3342 int saved_errno = errno;
3347 errno = saved_errno;
3352 static int fruit_open_meta(vfs_handle_struct *handle,
3353 struct smb_filename *smb_fname,
3354 files_struct *fsp, int flags, mode_t mode)
3357 struct fruit_config_data *config = NULL;
3358 struct fio *fio = NULL;
3360 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3362 SMB_VFS_HANDLE_GET_DATA(handle, config,
3363 struct fruit_config_data, return -1);
3365 switch (config->meta) {
3366 case FRUIT_META_STREAM:
3367 fd = fruit_open_meta_stream(handle, smb_fname,
3371 case FRUIT_META_NETATALK:
3372 fd = fruit_open_meta_netatalk(handle, smb_fname,
3377 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3381 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3387 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3388 fio->type = ADOUBLE_META;
3389 fio->config = config;
3394 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3395 struct smb_filename *smb_fname,
3401 struct adouble *ad = NULL;
3402 struct smb_filename *smb_fname_base = NULL;
3403 struct fruit_config_data *config = NULL;
3406 SMB_VFS_HANDLE_GET_DATA(handle, config,
3407 struct fruit_config_data, return -1);
3409 if ((!(flags & O_CREAT)) &&
3410 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3412 /* sorry, but directories don't habe a resource fork */
3417 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3422 /* Sanitize flags */
3423 if (flags & O_WRONLY) {
3424 /* We always need read access for the metadata header too */
3429 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3436 if (flags & (O_CREAT | O_TRUNC)) {
3437 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3443 fsp->fh->fd = hostfd;
3445 rc = ad_fset(ad, fsp);
3456 TALLOC_FREE(smb_fname_base);
3458 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3460 int saved_errno = errno;
3463 * BUGBUGBUG -- we would need to call
3464 * fd_close_posix here, but we don't have a
3467 fsp->fh->fd = hostfd;
3471 errno = saved_errno;
3476 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3477 struct smb_filename *smb_fname,
3482 #ifdef HAVE_ATTROPEN
3485 fd = attropen(smb_fname->base_name,
3486 AFPRESOURCE_EA_NETATALK,
3501 static int fruit_open_rsrc(vfs_handle_struct *handle,
3502 struct smb_filename *smb_fname,
3503 files_struct *fsp, int flags, mode_t mode)
3506 struct fruit_config_data *config = NULL;
3507 struct fio *fio = NULL;
3509 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3511 SMB_VFS_HANDLE_GET_DATA(handle, config,
3512 struct fruit_config_data, return -1);
3514 if (((flags & O_ACCMODE) == O_RDONLY)
3515 && (flags & O_CREAT)
3516 && !VALID_STAT(fsp->fsp_name->st))
3519 * This means the stream doesn't exist. macOS SMB server fails
3520 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3521 * 12565 and the test for this combination in
3522 * test_rfork_create().
3528 switch (config->rsrc) {
3529 case FRUIT_RSRC_STREAM:
3530 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3533 case FRUIT_RSRC_ADFILE:
3534 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3538 case FRUIT_RSRC_XATTR:
3539 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3544 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3548 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3554 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3555 fio->type = ADOUBLE_RSRC;
3556 fio->config = config;
3561 static int fruit_open(vfs_handle_struct *handle,
3562 struct smb_filename *smb_fname,
3563 files_struct *fsp, int flags, mode_t mode)
3567 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3569 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3570 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3573 if (is_afpinfo_stream(smb_fname)) {
3574 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3575 } else if (is_afpresource_stream(smb_fname)) {
3576 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3578 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3581 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3586 static int fruit_rename(struct vfs_handle_struct *handle,
3587 const struct smb_filename *smb_fname_src,
3588 const struct smb_filename *smb_fname_dst)
3591 struct fruit_config_data *config = NULL;
3592 struct smb_filename *src_adp_smb_fname = NULL;
3593 struct smb_filename *dst_adp_smb_fname = NULL;
3595 SMB_VFS_HANDLE_GET_DATA(handle, config,
3596 struct fruit_config_data, return -1);
3598 if (!VALID_STAT(smb_fname_src->st)) {
3599 DBG_ERR("Need valid stat for [%s]\n",
3600 smb_fname_str_dbg(smb_fname_src));
3604 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3609 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3610 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3615 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3620 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3625 DBG_DEBUG("%s -> %s\n",
3626 smb_fname_str_dbg(src_adp_smb_fname),
3627 smb_fname_str_dbg(dst_adp_smb_fname));
3629 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3630 if (errno == ENOENT) {
3635 TALLOC_FREE(src_adp_smb_fname);
3636 TALLOC_FREE(dst_adp_smb_fname);
3640 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3641 const struct smb_filename *smb_fname)
3643 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3646 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3647 const struct smb_filename *smb_fname)
3649 return SMB_VFS_REMOVEXATTR(handle->conn,
3651 AFPINFO_EA_NETATALK);
3654 static int fruit_unlink_meta(vfs_handle_struct *handle,
3655 const struct smb_filename *smb_fname)
3657 struct fruit_config_data *config = NULL;
3660 SMB_VFS_HANDLE_GET_DATA(handle, config,
3661 struct fruit_config_data, return -1);
3663 switch (config->meta) {
3664 case FRUIT_META_STREAM:
3665 rc = fruit_unlink_meta_stream(handle, smb_fname);
3668 case FRUIT_META_NETATALK:
3669 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3673 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3680 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3681 const struct smb_filename *smb_fname,
3686 if (!force_unlink) {
3687 struct smb_filename *smb_fname_cp = NULL;
3690 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3691 if (smb_fname_cp == NULL) {
3696 * 0 byte resource fork streams are not listed by
3697 * vfs_streaminfo, as a result stream cleanup/deletion of file
3698 * deletion doesn't remove the resourcefork stream.
3701 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3703 TALLOC_FREE(smb_fname_cp);
3704 DBG_ERR("stat [%s] failed [%s]\n",
3705 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3709 size = smb_fname_cp->st.st_ex_size;
3710 TALLOC_FREE(smb_fname_cp);
3713 /* OS X ignores resource fork stream delete requests */
3718 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3719 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3726 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3727 const struct smb_filename *smb_fname,
3731 struct adouble *ad = NULL;
3732 struct smb_filename *adp_smb_fname = NULL;
3734 if (!force_unlink) {
3735 ad = ad_get(talloc_tos(), handle, smb_fname,
3744 * 0 byte resource fork streams are not listed by
3745 * vfs_streaminfo, as a result stream cleanup/deletion of file
3746 * deletion doesn't remove the resourcefork stream.
3749 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3750 /* OS X ignores resource fork stream delete requests */
3758 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3763 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3764 TALLOC_FREE(adp_smb_fname);
3765 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3772 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3773 const struct smb_filename *smb_fname,
3777 * OS X ignores resource fork stream delete requests, so nothing to do
3778 * here. Removing the file will remove the xattr anyway, so we don't
3779 * have to take care of removing 0 byte resource forks that could be
3785 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3786 const struct smb_filename *smb_fname,
3789 struct fruit_config_data *config = NULL;
3792 SMB_VFS_HANDLE_GET_DATA(handle, config,
3793 struct fruit_config_data, return -1);
3795 switch (config->rsrc) {
3796 case FRUIT_RSRC_STREAM:
3797 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3800 case FRUIT_RSRC_ADFILE:
3801 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3804 case FRUIT_RSRC_XATTR:
3805 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3809 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3816 static int fruit_unlink(vfs_handle_struct *handle,
3817 const struct smb_filename *smb_fname)
3820 struct fruit_config_data *config = NULL;
3821 struct smb_filename *rsrc_smb_fname = NULL;
3823 SMB_VFS_HANDLE_GET_DATA(handle, config,
3824 struct fruit_config_data, return -1);
3826 if (is_afpinfo_stream(smb_fname)) {
3827 return fruit_unlink_meta(handle, smb_fname);
3828 } else if (is_afpresource_stream(smb_fname)) {
3829 return fruit_unlink_rsrc(handle, smb_fname, false);
3830 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3831 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3835 * A request to delete the base file. Because 0 byte resource
3836 * fork streams are not listed by fruit_streaminfo,
3837 * delete_all_streams() can't remove 0 byte resource fork
3838 * streams, so we have to cleanup this here.
3840 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3841 smb_fname->base_name,
3842 AFPRESOURCE_STREAM_NAME,
3845 if (rsrc_smb_fname == NULL) {
3849 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3850 if ((rc != 0) && (errno != ENOENT)) {
3851 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3852 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3853 TALLOC_FREE(rsrc_smb_fname);
3856 TALLOC_FREE(rsrc_smb_fname);
3858 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3861 static int fruit_chmod(vfs_handle_struct *handle,
3862 const struct smb_filename *smb_fname,
3866 struct fruit_config_data *config = NULL;
3867 struct smb_filename *smb_fname_adp = NULL;
3869 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3874 SMB_VFS_HANDLE_GET_DATA(handle, config,
3875 struct fruit_config_data, return -1);
3877 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3881 if (!VALID_STAT(smb_fname->st)) {
3885 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3889 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3894 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3896 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3897 if (errno == ENOENT) {
3901 TALLOC_FREE(smb_fname_adp);
3905 static int fruit_chown(vfs_handle_struct *handle,
3906 const struct smb_filename *smb_fname,
3911 struct fruit_config_data *config = NULL;
3912 struct smb_filename *adp_smb_fname = NULL;
3914 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3919 SMB_VFS_HANDLE_GET_DATA(handle, config,
3920 struct fruit_config_data, return -1);
3922 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3926 if (!VALID_STAT(smb_fname->st)) {
3930 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3934 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3939 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3941 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3942 if (errno == ENOENT) {
3947 TALLOC_FREE(adp_smb_fname);
3951 static int fruit_rmdir(struct vfs_handle_struct *handle,
3952 const struct smb_filename *smb_fname)
3956 struct fruit_config_data *config;
3958 SMB_VFS_HANDLE_GET_DATA(handle, config,
3959 struct fruit_config_data, return -1);
3961 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3966 * Due to there is no way to change bDeleteVetoFiles variable
3967 * from this module, need to clean up ourselves
3970 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3975 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3977 struct adouble *ad = NULL;
3979 struct smb_filename *ad_smb_fname = NULL;
3982 match = strncmp(de->d_name,
3983 ADOUBLE_NAME_PREFIX,
3984 strlen(ADOUBLE_NAME_PREFIX));
3989 p = talloc_asprintf(talloc_tos(), "%s/%s",
3990 smb_fname->base_name, de->d_name);
3992 DBG_ERR("talloc_asprintf failed\n");
3996 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
4000 if (ad_smb_fname == NULL) {
4001 DBG_ERR("synthetic_smb_fname failed\n");
4006 * Check whether it's a valid AppleDouble file, if
4007 * yes, delete it, ignore it otherwise.
4009 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
4011 TALLOC_FREE(ad_smb_fname);
4017 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
4019 DBG_ERR("Deleting [%s] failed\n",
4020 smb_fname_str_dbg(ad_smb_fname));
4022 TALLOC_FREE(ad_smb_fname);
4027 SMB_VFS_CLOSEDIR(handle->conn, dh);
4029 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
4032 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
4033 files_struct *fsp, void *data,
4034 size_t n, off_t offset)
4039 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4045 DBG_ERR("Removing [%s] after short read [%zd]\n",
4046 fsp_str_dbg(fsp), nread);
4048 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4050 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
4058 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
4059 files_struct *fsp, void *data,
4060 size_t n, off_t offset)
4063 struct adouble *ad = NULL;
4064 char afpinfo_buf[AFP_INFO_SIZE];
4068 ai = afpinfo_new(talloc_tos());
4073 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4079 p = ad_get_entry(ad, ADEID_FINDERI);
4081 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4086 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
4088 nread = afpinfo_pack(ai, afpinfo_buf);
4089 if (nread != AFP_INFO_SIZE) {
4094 memcpy(data, afpinfo_buf, n);
4102 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
4103 files_struct *fsp, void *data,
4104 size_t n, off_t offset)
4106 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4111 * OS X has a off-by-1 error in the offset calculation, so we're
4112 * bug compatible here. It won't hurt, as any relevant real
4113 * world read requests from the AFP_AfpInfo stream will be
4114 * offset=0 n=60. offset is ignored anyway, see below.
4116 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4121 DBG_ERR("Failed to fetch fsp extension");
4125 /* Yes, macOS always reads from offset 0 */
4127 to_return = MIN(n, AFP_INFO_SIZE);
4129 switch (fio->config->meta) {
4130 case FRUIT_META_STREAM:
4131 nread = fruit_pread_meta_stream(handle, fsp, data,
4135 case FRUIT_META_NETATALK:
4136 nread = fruit_pread_meta_adouble(handle, fsp, data,
4141 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4148 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4149 files_struct *fsp, void *data,
4150 size_t n, off_t offset)
4152 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4155 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4156 files_struct *fsp, void *data,
4157 size_t n, off_t offset)
4159 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4162 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4163 files_struct *fsp, void *data,
4164 size_t n, off_t offset)
4166 struct adouble *ad = NULL;
4169 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4174 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4175 offset + ad_getentryoff(ad, ADEID_RFORK));
4181 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4182 files_struct *fsp, void *data,
4183 size_t n, off_t offset)
4185 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4193 switch (fio->config->rsrc) {
4194 case FRUIT_RSRC_STREAM:
4195 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4198 case FRUIT_RSRC_ADFILE:
4199 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4202 case FRUIT_RSRC_XATTR:
4203 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4207 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4214 static ssize_t fruit_pread(vfs_handle_struct *handle,
4215 files_struct *fsp, void *data,
4216 size_t n, off_t offset)
4218 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4221 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4222 fsp_str_dbg(fsp), (intmax_t)offset, n);
4225 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4228 if (fio->type == ADOUBLE_META) {
4229 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4231 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4234 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4238 static bool fruit_must_handle_aio_stream(struct fio *fio)
4244 if ((fio->type == ADOUBLE_META) &&
4245 (fio->config->meta == FRUIT_META_NETATALK))
4250 if ((fio->type == ADOUBLE_RSRC) &&
4251 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4259 struct fruit_pread_state {
4261 struct vfs_aio_state vfs_aio_state;
4264 static void fruit_pread_done(struct tevent_req *subreq);
4266 static struct tevent_req *fruit_pread_send(
4267 struct vfs_handle_struct *handle,
4268 TALLOC_CTX *mem_ctx,
4269 struct tevent_context *ev,
4270 struct files_struct *fsp,
4272 size_t n, off_t offset)
4274 struct tevent_req *req = NULL;
4275 struct tevent_req *subreq = NULL;
4276 struct fruit_pread_state *state = NULL;
4277 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4279 req = tevent_req_create(mem_ctx, &state,
4280 struct fruit_pread_state);
4285 if (fruit_must_handle_aio_stream(fio)) {
4286 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4287 if (state->nread != n) {
4288 if (state->nread != -1) {
4291 tevent_req_error(req, errno);
4292 return tevent_req_post(req, ev);
4294 tevent_req_done(req);
4295 return tevent_req_post(req, ev);
4298 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4300 if (tevent_req_nomem(req, subreq)) {
4301 return tevent_req_post(req, ev);
4303 tevent_req_set_callback(subreq, fruit_pread_done, req);
4307 static void fruit_pread_done(struct tevent_req *subreq)
4309 struct tevent_req *req = tevent_req_callback_data(
4310 subreq, struct tevent_req);
4311 struct fruit_pread_state *state = tevent_req_data(
4312 req, struct fruit_pread_state);
4314 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4315 TALLOC_FREE(subreq);
4317 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4320 tevent_req_done(req);
4323 static ssize_t fruit_pread_recv(struct tevent_req *req,
4324 struct vfs_aio_state *vfs_aio_state)
4326 struct fruit_pread_state *state = tevent_req_data(
4327 req, struct fruit_pread_state);
4329 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4333 *vfs_aio_state = state->vfs_aio_state;
4334 return state->nread;
4337 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4338 files_struct *fsp, const void *data,
4339 size_t n, off_t offset)
4345 ai = afpinfo_unpack(talloc_tos(), data);
4350 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4351 if (nwritten != n) {
4355 if (!ai_empty_finderinfo(ai)) {
4359 ok = set_delete_on_close(
4362 handle->conn->session_info->security_token,
4363 handle->conn->session_info->unix_token);
4365 DBG_ERR("set_delete_on_close on [%s] failed\n",
4373 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4374 files_struct *fsp, const void *data,
4375 size_t n, off_t offset)
4377 struct adouble *ad = NULL;
4383 ai = afpinfo_unpack(talloc_tos(), data);
4388 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4390 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4395 p = ad_get_entry(ad, ADEID_FINDERI);
4397 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4402 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4404 ret = ad_fset(ad, fsp);
4406 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4413 if (!ai_empty_finderinfo(ai)) {
4417 ok = set_delete_on_close(
4420 handle->conn->session_info->security_token,
4421 handle->conn->session_info->unix_token);
4423 DBG_ERR("set_delete_on_close on [%s] failed\n",
4431 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4432 files_struct *fsp, const void *data,
4433 size_t n, off_t offset)
4435 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4439 * Writing an all 0 blob to the metadata stream
4440 * results in the stream being removed on a macOS
4441 * server. This ensures we behave the same and it
4442 * verified by the "delete AFP_AfpInfo by writing all
4445 if (n != AFP_INFO_SIZE || offset != 0) {
4446 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4447 (intmax_t)offset, (intmax_t)n);
4452 DBG_ERR("Failed to fetch fsp extension");
4456 switch (fio->config->meta) {
4457 case FRUIT_META_STREAM:
4458 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4462 case FRUIT_META_NETATALK:
4463 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4468 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4475 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4476 files_struct *fsp, const void *data,
4477 size_t n, off_t offset)
4479 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4482 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4483 files_struct *fsp, const void *data,
4484 size_t n, off_t offset)
4486 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4489 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4490 files_struct *fsp, const void *data,
4491 size_t n, off_t offset)
4493 struct adouble *ad = NULL;
4497 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4499 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4503 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4504 offset + ad_getentryoff(ad, ADEID_RFORK));
4505 if (nwritten != n) {
4506 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4507 fsp_str_dbg(fsp), nwritten, n);
4512 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4513 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4514 ret = ad_fset(ad, fsp);
4516 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4526 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4527 files_struct *fsp, const void *data,
4528 size_t n, off_t offset)
4530 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4534 DBG_ERR("Failed to fetch fsp extension");
4538 switch (fio->config->rsrc) {
4539 case FRUIT_RSRC_STREAM:
4540 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4543 case FRUIT_RSRC_ADFILE:
4544 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4547 case FRUIT_RSRC_XATTR:
4548 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4552 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4559 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4560 files_struct *fsp, const void *data,
4561 size_t n, off_t offset)
4563 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4566 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4567 fsp_str_dbg(fsp), (intmax_t)offset, n);
4570 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4573 if (fio->type == ADOUBLE_META) {
4574 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4576 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4579 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4583 struct fruit_pwrite_state {
4585 struct vfs_aio_state vfs_aio_state;
4588 static void fruit_pwrite_done(struct tevent_req *subreq);
4590 static struct tevent_req *fruit_pwrite_send(
4591 struct vfs_handle_struct *handle,
4592 TALLOC_CTX *mem_ctx,
4593 struct tevent_context *ev,
4594 struct files_struct *fsp,
4596 size_t n, off_t offset)
4598 struct tevent_req *req = NULL;
4599 struct tevent_req *subreq = NULL;
4600 struct fruit_pwrite_state *state = NULL;
4601 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4603 req = tevent_req_create(mem_ctx, &state,
4604 struct fruit_pwrite_state);
4609 if (fruit_must_handle_aio_stream(fio)) {
4610 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4611 if (state->nwritten != n) {
4612 if (state->nwritten != -1) {
4615 tevent_req_error(req, errno);
4616 return tevent_req_post(req, ev);
4618 tevent_req_done(req);
4619 return tevent_req_post(req, ev);
4622 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4624 if (tevent_req_nomem(req, subreq)) {
4625 return tevent_req_post(req, ev);
4627 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4631 static void fruit_pwrite_done(struct tevent_req *subreq)
4633 struct tevent_req *req = tevent_req_callback_data(
4634 subreq, struct tevent_req);
4635 struct fruit_pwrite_state *state = tevent_req_data(
4636 req, struct fruit_pwrite_state);
4638 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4639 TALLOC_FREE(subreq);
4641 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4644 tevent_req_done(req);
4647 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4648 struct vfs_aio_state *vfs_aio_state)
4650 struct fruit_pwrite_state *state = tevent_req_data(
4651 req, struct fruit_pwrite_state);
4653 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4657 *vfs_aio_state = state->vfs_aio_state;
4658 return state->nwritten;
4662 * Helper to stat/lstat the base file of an smb_fname.
4664 static int fruit_stat_base(vfs_handle_struct *handle,
4665 struct smb_filename *smb_fname,
4668 char *tmp_stream_name;
4671 tmp_stream_name = smb_fname->stream_name;
4672 smb_fname->stream_name = NULL;
4674 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4676 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4678 smb_fname->stream_name = tmp_stream_name;
4682 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4683 struct smb_filename *smb_fname,
4689 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4691 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4697 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4698 struct smb_filename *smb_fname,
4701 struct adouble *ad = NULL;
4703 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4705 DBG_INFO("fruit_stat_meta %s: %s\n",
4706 smb_fname_str_dbg(smb_fname), strerror(errno));
4712 /* Populate the stat struct with info from the base file. */
4713 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4716 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4717 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4718 smb_fname->stream_name);
4722 static int fruit_stat_meta(vfs_handle_struct *handle,
4723 struct smb_filename *smb_fname,
4726 struct fruit_config_data *config = NULL;
4729 SMB_VFS_HANDLE_GET_DATA(handle, config,
4730 struct fruit_config_data, return -1);
4732 switch (config->meta) {
4733 case FRUIT_META_STREAM:
4734 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4737 case FRUIT_META_NETATALK:
4738 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4742 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4749 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4750 struct smb_filename *smb_fname,
4753 struct adouble *ad = NULL;
4756 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4762 /* Populate the stat struct with info from the base file. */
4763 ret = fruit_stat_base(handle, smb_fname, follow_links);
4769 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4770 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4771 smb_fname->stream_name);
4776 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4777 struct smb_filename *smb_fname,
4783 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4785 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4791 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4792 struct smb_filename *smb_fname,
4795 #ifdef HAVE_ATTROPEN
4799 /* Populate the stat struct with info from the base file. */
4800 ret = fruit_stat_base(handle, smb_fname, follow_links);
4805 fd = attropen(smb_fname->base_name,
4806 AFPRESOURCE_EA_NETATALK,
4812 ret = sys_fstat(fd, &smb_fname->st, false);
4815 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4816 AFPRESOURCE_EA_NETATALK);
4822 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4823 smb_fname->stream_name);
4833 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4834 struct smb_filename *smb_fname,
4837 struct fruit_config_data *config = NULL;
4840 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4842 SMB_VFS_HANDLE_GET_DATA(handle, config,
4843 struct fruit_config_data, return -1);
4845 switch (config->rsrc) {
4846 case FRUIT_RSRC_STREAM:
4847 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4850 case FRUIT_RSRC_XATTR:
4851 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4854 case FRUIT_RSRC_ADFILE:
4855 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4859 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4866 static int fruit_stat(vfs_handle_struct *handle,
4867 struct smb_filename *smb_fname)
4871 DEBUG(10, ("fruit_stat called for %s\n",
4872 smb_fname_str_dbg(smb_fname)));
4874 if (!is_ntfs_stream_smb_fname(smb_fname)
4875 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4876 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4878 update_btime(handle, smb_fname);
4884 * Note if lp_posix_paths() is true, we can never
4885 * get here as is_ntfs_stream_smb_fname() is
4886 * always false. So we never need worry about
4887 * not following links here.
4890 if (is_afpinfo_stream(smb_fname)) {
4891 rc = fruit_stat_meta(handle, smb_fname, true);
4892 } else if (is_afpresource_stream(smb_fname)) {
4893 rc = fruit_stat_rsrc(handle, smb_fname, true);
4895 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4899 update_btime(handle, smb_fname);
4900 smb_fname->st.st_ex_mode &= ~S_IFMT;
4901 smb_fname->st.st_ex_mode |= S_IFREG;
4902 smb_fname->st.st_ex_blocks =
4903 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4908 static int fruit_lstat(vfs_handle_struct *handle,
4909 struct smb_filename *smb_fname)
4913 DEBUG(10, ("fruit_lstat called for %s\n",
4914 smb_fname_str_dbg(smb_fname)));
4916 if (!is_ntfs_stream_smb_fname(smb_fname)
4917 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4918 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4920 update_btime(handle, smb_fname);
4925 if (is_afpinfo_stream(smb_fname)) {
4926 rc = fruit_stat_meta(handle, smb_fname, false);
4927 } else if (is_afpresource_stream(smb_fname)) {
4928 rc = fruit_stat_rsrc(handle, smb_fname, false);
4930 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4934 update_btime(handle, smb_fname);
4935 smb_fname->st.st_ex_mode &= ~S_IFMT;
4936 smb_fname->st.st_ex_mode |= S_IFREG;
4937 smb_fname->st.st_ex_blocks =
4938 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4943 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4945 SMB_STRUCT_STAT *sbuf)
4947 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4950 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4952 SMB_STRUCT_STAT *sbuf)
4956 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4961 *sbuf = fsp->base_fsp->fsp_name->st;
4962 sbuf->st_ex_size = AFP_INFO_SIZE;
4963 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4968 static int fruit_fstat_meta(vfs_handle_struct *handle,
4970 SMB_STRUCT_STAT *sbuf,
4975 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4977 switch (fio->config->meta) {
4978 case FRUIT_META_STREAM:
4979 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4982 case FRUIT_META_NETATALK:
4983 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4987 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4991 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4995 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4997 SMB_STRUCT_STAT *sbuf)
4999 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5002 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
5004 SMB_STRUCT_STAT *sbuf)
5006 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5009 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
5011 SMB_STRUCT_STAT *sbuf)
5013 struct adouble *ad = NULL;
5016 /* Populate the stat struct with info from the base file. */
5017 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
5022 ad = ad_get(talloc_tos(), handle,
5023 fsp->base_fsp->fsp_name,
5026 DBG_ERR("ad_get [%s] failed [%s]\n",
5027 fsp_str_dbg(fsp), strerror(errno));
5031 *sbuf = fsp->base_fsp->fsp_name->st;
5032 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
5033 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
5039 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
5040 SMB_STRUCT_STAT *sbuf, struct fio *fio)
5044 switch (fio->config->rsrc) {
5045 case FRUIT_RSRC_STREAM:
5046 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
5049 case FRUIT_RSRC_ADFILE:
5050 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
5053 case FRUIT_RSRC_XATTR:
5054 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
5058 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5065 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
5066 SMB_STRUCT_STAT *sbuf)
5068 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5072 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5075 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
5077 if (fio->type == ADOUBLE_META) {
5078 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
5080 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
5084 sbuf->st_ex_mode &= ~S_IFMT;
5085 sbuf->st_ex_mode |= S_IFREG;
5086 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
5089 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
5090 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
5094 static NTSTATUS delete_invalid_meta_stream(
5095 vfs_handle_struct *handle,
5096 const struct smb_filename *smb_fname,
5097 TALLOC_CTX *mem_ctx,
5098 unsigned int *pnum_streams,
5099 struct stream_struct **pstreams)
5101 struct smb_filename *sname = NULL;
5105 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
5107 return NT_STATUS_INTERNAL_ERROR;
5110 sname = synthetic_smb_fname(talloc_tos(),
5111 smb_fname->base_name,
5112 AFPINFO_STREAM_NAME,
5114 if (sname == NULL) {
5115 return NT_STATUS_NO_MEMORY;
5118 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5121 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5122 return map_nt_error_from_unix(errno);
5125 return NT_STATUS_OK;
5128 static NTSTATUS fruit_streaminfo_meta_stream(
5129 vfs_handle_struct *handle,
5130 struct files_struct *fsp,
5131 const struct smb_filename *smb_fname,
5132 TALLOC_CTX *mem_ctx,
5133 unsigned int *pnum_streams,
5134 struct stream_struct **pstreams)
5136 struct stream_struct *stream = *pstreams;
5137 unsigned int num_streams = *pnum_streams;
5138 struct smb_filename *sname = NULL;
5139 char *full_name = NULL;
5141 struct share_mode_lock *lck = NULL;
5142 struct file_id id = {0};
5143 bool delete_on_close_set;
5149 for (i = 0; i < num_streams; i++) {
5150 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5155 if (i == num_streams) {
5156 return NT_STATUS_OK;
5159 if (stream[i].size != AFP_INFO_SIZE) {
5160 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5161 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5163 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5164 pnum_streams, pstreams);
5168 * Now check if there's a delete-on-close pending on the stream. If so,
5169 * hide the stream. This behaviour was verified against a macOS 10.12
5173 sname = synthetic_smb_fname(talloc_tos(),
5174 smb_fname->base_name,
5175 AFPINFO_STREAM_NAME,
5177 if (sname == NULL) {
5178 status = NT_STATUS_NO_MEMORY;
5182 ret = SMB_VFS_NEXT_STAT(handle, sname);
5184 status = map_nt_error_from_unix(errno);
5188 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5190 lck = get_existing_share_mode_lock(talloc_tos(), id);
5192 status = NT_STATUS_OK;
5196 full_name = talloc_asprintf(talloc_tos(),
5200 if (full_name == NULL) {
5201 status = NT_STATUS_NO_MEMORY;
5205 status = file_name_hash(handle->conn, full_name, &name_hash);
5206 if (!NT_STATUS_IS_OK(status)) {
5210 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5211 if (delete_on_close_set) {
5212 ok = del_fruit_stream(mem_ctx,
5217 status = NT_STATUS_INTERNAL_ERROR;
5222 status = NT_STATUS_OK;
5227 TALLOC_FREE(full_name);
5231 static NTSTATUS fruit_streaminfo_meta_netatalk(
5232 vfs_handle_struct *handle,
5233 struct files_struct *fsp,
5234 const struct smb_filename *smb_fname,
5235 TALLOC_CTX *mem_ctx,
5236 unsigned int *pnum_streams,
5237 struct stream_struct **pstreams)
5239 struct stream_struct *stream = *pstreams;
5240 unsigned int num_streams = *pnum_streams;
5241 struct adouble *ad = NULL;
5246 /* Remove the Netatalk xattr from the list */
5247 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5248 ":" NETATALK_META_XATTR ":$DATA");
5250 return NT_STATUS_NO_MEMORY;
5254 * Check if there's a AFPINFO_STREAM from the VFS streams
5255 * backend and if yes, remove it from the list
5257 for (i = 0; i < num_streams; i++) {
5258 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5263 if (i < num_streams) {
5264 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5265 smb_fname_str_dbg(smb_fname));
5267 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5270 return NT_STATUS_INTERNAL_ERROR;
5274 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5276 return NT_STATUS_OK;
5279 is_fi_empty = ad_empty_finderinfo(ad);
5283 return NT_STATUS_OK;
5286 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5287 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5288 smb_roundup(handle->conn, AFP_INFO_SIZE));
5290 return NT_STATUS_NO_MEMORY;
5293 return NT_STATUS_OK;
5296 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5297 struct files_struct *fsp,
5298 const struct smb_filename *smb_fname,
5299 TALLOC_CTX *mem_ctx,
5300 unsigned int *pnum_streams,
5301 struct stream_struct **pstreams)
5303 struct fruit_config_data *config = NULL;
5306 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5307 return NT_STATUS_INTERNAL_ERROR);
5309 switch (config->meta) {
5310 case FRUIT_META_NETATALK:
5311 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5312 mem_ctx, pnum_streams,
5316 case FRUIT_META_STREAM:
5317 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5318 mem_ctx, pnum_streams,
5323 return NT_STATUS_INTERNAL_ERROR;
5329 static NTSTATUS fruit_streaminfo_rsrc_stream(
5330 vfs_handle_struct *handle,
5331 struct files_struct *fsp,
5332 const struct smb_filename *smb_fname,
5333 TALLOC_CTX *mem_ctx,
5334 unsigned int *pnum_streams,
5335 struct stream_struct **pstreams)
5339 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5341 DBG_ERR("Filtering resource stream failed\n");
5342 return NT_STATUS_INTERNAL_ERROR;
5344 return NT_STATUS_OK;
5347 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5348 vfs_handle_struct *handle,
5349 struct files_struct *fsp,
5350 const struct smb_filename *smb_fname,
5351 TALLOC_CTX *mem_ctx,
5352 unsigned int *pnum_streams,
5353 struct stream_struct **pstreams)
5357 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5359 DBG_ERR("Filtering resource stream failed\n");
5360 return NT_STATUS_INTERNAL_ERROR;
5362 return NT_STATUS_OK;
5365 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5366 vfs_handle_struct *handle,
5367 struct files_struct *fsp,
5368 const struct smb_filename *smb_fname,
5369 TALLOC_CTX *mem_ctx,
5370 unsigned int *pnum_streams,
5371 struct stream_struct **pstreams)
5373 struct stream_struct *stream = *pstreams;
5374 unsigned int num_streams = *pnum_streams;
5375 struct adouble *ad = NULL;
5381 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5382 * and if yes, remove it from the list
5384 for (i = 0; i < num_streams; i++) {
5385 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5390 if (i < num_streams) {
5391 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5392 smb_fname_str_dbg(smb_fname));
5394 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5395 AFPRESOURCE_STREAM);
5397 return NT_STATUS_INTERNAL_ERROR;
5401 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5403 return NT_STATUS_OK;
5406 rlen = ad_getentrylen(ad, ADEID_RFORK);
5410 return NT_STATUS_OK;
5413 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5414 AFPRESOURCE_STREAM_NAME, rlen,
5415 smb_roundup(handle->conn, rlen));
5417 return NT_STATUS_NO_MEMORY;
5420 return NT_STATUS_OK;
5423 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5424 struct files_struct *fsp,
5425 const struct smb_filename *smb_fname,
5426 TALLOC_CTX *mem_ctx,
5427 unsigned int *pnum_streams,
5428 struct stream_struct **pstreams)
5430 struct fruit_config_data *config = NULL;
5433 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5434 return NT_STATUS_INTERNAL_ERROR);
5436 switch (config->rsrc) {
5437 case FRUIT_RSRC_STREAM:
5438 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5439 mem_ctx, pnum_streams,
5443 case FRUIT_RSRC_XATTR:
5444 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5445 mem_ctx, pnum_streams,
5449 case FRUIT_RSRC_ADFILE:
5450 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5451 mem_ctx, pnum_streams,
5456 return NT_STATUS_INTERNAL_ERROR;
5462 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5463 struct files_struct *fsp,
5464 const struct smb_filename *smb_fname,
5465 TALLOC_CTX *mem_ctx,
5466 unsigned int *pnum_streams,
5467 struct stream_struct **pstreams)
5469 struct fruit_config_data *config = NULL;
5472 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5473 return NT_STATUS_UNSUCCESSFUL);
5475 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5477 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5478 pnum_streams, pstreams);
5479 if (!NT_STATUS_IS_OK(status)) {
5483 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5484 mem_ctx, pnum_streams, pstreams);
5485 if (!NT_STATUS_IS_OK(status)) {
5489 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5490 mem_ctx, pnum_streams, pstreams);
5491 if (!NT_STATUS_IS_OK(status)) {
5495 return NT_STATUS_OK;
5498 static int fruit_ntimes(vfs_handle_struct *handle,
5499 const struct smb_filename *smb_fname,
5500 struct smb_file_time *ft)
5503 struct adouble *ad = NULL;
5504 struct fruit_config_data *config = NULL;
5506 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5509 if ((config->meta != FRUIT_META_NETATALK) ||
5510 null_timespec(ft->create_time))
5512 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5515 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5516 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5518 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5523 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5524 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5526 rc = ad_set(ad, smb_fname);
5532 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5535 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5538 static int fruit_fallocate(struct vfs_handle_struct *handle,
5539 struct files_struct *fsp,
5544 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5547 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5550 /* Let the pwrite code path handle it. */
5555 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5556 struct files_struct *fsp,
5560 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5563 #ifdef HAVE_ATTROPEN
5564 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5569 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5570 struct files_struct *fsp,
5574 struct adouble *ad = NULL;
5577 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5579 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5580 fsp_str_dbg(fsp), strerror(errno));
5584 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5586 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5592 ad_setentrylen(ad, ADEID_RFORK, offset);
5594 rc = ad_fset(ad, fsp);
5596 DBG_ERR("ad_fset [%s] failed [%s]\n",
5597 fsp_str_dbg(fsp), strerror(errno));
5606 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5607 struct files_struct *fsp,
5611 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5614 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5617 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5618 struct files_struct *fsp,
5621 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5625 DBG_ERR("Failed to fetch fsp extension");
5629 switch (fio->config->rsrc) {
5630 case FRUIT_RSRC_XATTR:
5631 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5634 case FRUIT_RSRC_ADFILE:
5635 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5638 case FRUIT_RSRC_STREAM:
5639 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5643 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5651 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5652 struct files_struct *fsp,
5656 DBG_WARNING("ftruncate %s to %jd",
5657 fsp_str_dbg(fsp), (intmax_t)offset);
5658 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5663 /* OS X returns success but does nothing */
5664 DBG_INFO("ignoring ftruncate %s to %jd\n",
5665 fsp_str_dbg(fsp), (intmax_t)offset);
5669 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5670 struct files_struct *fsp,
5673 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5676 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5681 global_fruit_config.nego_aapl &&
5682 is_ntfs_stream_smb_fname(fsp->fsp_name) &&
5683 !is_ntfs_default_stream_smb_fname(fsp->fsp_name))
5685 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5687 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5690 if (fio->type == ADOUBLE_META) {
5691 ret = fruit_ftruncate_meta(handle, fsp, offset);
5693 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5696 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5700 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5701 struct smb_request *req,
5702 uint16_t root_dir_fid,
5703 struct smb_filename *smb_fname,
5704 uint32_t access_mask,
5705 uint32_t share_access,
5706 uint32_t create_disposition,
5707 uint32_t create_options,
5708 uint32_t file_attributes,
5709 uint32_t oplock_request,
5710 struct smb2_lease *lease,
5711 uint64_t allocation_size,
5712 uint32_t private_flags,
5713 struct security_descriptor *sd,
5714 struct ea_list *ea_list,
5715 files_struct **result,
5717 const struct smb2_create_blobs *in_context_blobs,
5718 struct smb2_create_blobs *out_context_blobs)
5721 struct fruit_config_data *config = NULL;
5722 files_struct *fsp = NULL;
5724 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5725 if (!NT_STATUS_IS_OK(status)) {
5729 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5730 return NT_STATUS_UNSUCCESSFUL);
5732 status = SMB_VFS_NEXT_CREATE_FILE(
5733 handle, req, root_dir_fid, smb_fname,
5734 access_mask, share_access,
5735 create_disposition, create_options,
5736 file_attributes, oplock_request,
5738 allocation_size, private_flags,
5739 sd, ea_list, result,
5740 pinfo, in_context_blobs, out_context_blobs);
5741 if (!NT_STATUS_IS_OK(status)) {
5747 if (global_fruit_config.nego_aapl) {
5748 if (config->posix_rename && fsp->is_directory) {
5750 * Enable POSIX directory rename behaviour
5752 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5757 * If this is a plain open for existing files, opening an 0
5758 * byte size resource fork MUST fail with
5759 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5761 * Cf the vfs_fruit torture tests in test_rfork_create().
5763 if (is_afpresource_stream(fsp->fsp_name) &&
5764 create_disposition == FILE_OPEN)
5766 if (fsp->fsp_name->st.st_ex_size == 0) {
5767 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5772 if (is_ntfs_stream_smb_fname(smb_fname)
5773 || fsp->is_directory) {
5777 if (config->locking == FRUIT_LOCKING_NETATALK) {
5778 status = fruit_check_access(
5781 map_share_mode_to_deny_mode(share_access, 0));
5782 if (!NT_STATUS_IS_OK(status)) {
5790 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5793 close_file(req, fsp, ERROR_CLOSE);
5794 *result = fsp = NULL;
5800 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5801 const struct smb_filename *fname,
5802 TALLOC_CTX *mem_ctx,
5803 struct readdir_attr_data **pattr_data)
5805 struct fruit_config_data *config = NULL;
5806 struct readdir_attr_data *attr_data;
5809 SMB_VFS_HANDLE_GET_DATA(handle, config,
5810 struct fruit_config_data,
5811 return NT_STATUS_UNSUCCESSFUL);
5813 if (!global_fruit_config.nego_aapl) {
5814 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5817 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5819 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5820 if (*pattr_data == NULL) {
5821 return NT_STATUS_UNSUCCESSFUL;
5823 attr_data = *pattr_data;
5824 attr_data->type = RDATTR_AAPL;
5827 * Mac metadata: compressed FinderInfo, resource fork length
5830 status = readdir_attr_macmeta(handle, fname, attr_data);
5831 if (!NT_STATUS_IS_OK(status)) {
5833 * Error handling is tricky: if we return failure from
5834 * this function, the corresponding directory entry
5835 * will to be passed to the client, so we really just
5836 * want to error out on fatal errors.
5838 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5846 if (config->unix_info_enabled) {
5847 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5853 if (!config->readdir_attr_max_access) {
5854 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5856 status = smbd_calculate_access_mask(
5860 SEC_FLAG_MAXIMUM_ALLOWED,
5861 &attr_data->attr_data.aapl.max_access);
5862 if (!NT_STATUS_IS_OK(status)) {
5867 return NT_STATUS_OK;
5870 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5871 fname->base_name, nt_errstr(status)));
5872 TALLOC_FREE(*pattr_data);
5876 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5878 uint32_t security_info,
5879 TALLOC_CTX *mem_ctx,
5880 struct security_descriptor **ppdesc)
5883 struct security_ace ace;
5885 struct fruit_config_data *config;
5887 SMB_VFS_HANDLE_GET_DATA(handle, config,
5888 struct fruit_config_data,
5889 return NT_STATUS_UNSUCCESSFUL);
5891 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5893 if (!NT_STATUS_IS_OK(status)) {
5898 * Add MS NFS style ACEs with uid, gid and mode
5900 if (!global_fruit_config.nego_aapl) {
5901 return NT_STATUS_OK;
5903 if (!config->unix_info_enabled) {
5904 return NT_STATUS_OK;
5907 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5908 status = remove_virtual_nfs_aces(*ppdesc);
5909 if (!NT_STATUS_IS_OK(status)) {
5910 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5914 /* MS NFS style mode */
5915 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5916 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5917 status = security_descriptor_dacl_add(*ppdesc, &ace);
5918 if (!NT_STATUS_IS_OK(status)) {
5919 DEBUG(1,("failed to add MS NFS style ACE\n"));
5923 /* MS NFS style uid */
5924 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5925 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5926 status = security_descriptor_dacl_add(*ppdesc, &ace);
5927 if (!NT_STATUS_IS_OK(status)) {
5928 DEBUG(1,("failed to add MS NFS style ACE\n"));
5932 /* MS NFS style gid */
5933 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5934 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5935 status = security_descriptor_dacl_add(*ppdesc, &ace);
5936 if (!NT_STATUS_IS_OK(status)) {
5937 DEBUG(1,("failed to add MS NFS style ACE\n"));
5941 return NT_STATUS_OK;
5944 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5946 uint32_t security_info_sent,
5947 const struct security_descriptor *orig_psd)
5951 mode_t ms_nfs_mode = 0;
5953 struct security_descriptor *psd = NULL;
5954 uint32_t orig_num_aces = 0;
5956 if (orig_psd->dacl != NULL) {
5957 orig_num_aces = orig_psd->dacl->num_aces;
5960 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5962 return NT_STATUS_NO_MEMORY;
5965 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5967 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5968 if (!NT_STATUS_IS_OK(status)) {
5969 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5975 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5976 * sent/present flags correctly now we've removed them.
5979 if (orig_num_aces != 0) {
5981 * Are there any ACE's left ?
5983 if (psd->dacl->num_aces == 0) {
5984 /* No - clear the DACL sent/present flags. */
5985 security_info_sent &= ~SECINFO_DACL;
5986 psd->type &= ~SEC_DESC_DACL_PRESENT;
5990 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5991 if (!NT_STATUS_IS_OK(status)) {
5992 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5998 if (fsp->fh->fd != -1) {
5999 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
6001 result = SMB_VFS_CHMOD(fsp->conn,
6007 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
6008 result, (unsigned)ms_nfs_mode,
6010 status = map_nt_error_from_unix(errno);
6017 return NT_STATUS_OK;
6020 static struct vfs_offload_ctx *fruit_offload_ctx;
6022 struct fruit_offload_read_state {
6023 struct vfs_handle_struct *handle;
6024 struct tevent_context *ev;
6030 static void fruit_offload_read_done(struct tevent_req *subreq);
6032 static struct tevent_req *fruit_offload_read_send(
6033 TALLOC_CTX *mem_ctx,
6034 struct tevent_context *ev,
6035 struct vfs_handle_struct *handle,
6042 struct tevent_req *req = NULL;
6043 struct tevent_req *subreq = NULL;
6044 struct fruit_offload_read_state *state = NULL;
6046 req = tevent_req_create(mem_ctx, &state,
6047 struct fruit_offload_read_state);
6051 *state = (struct fruit_offload_read_state) {
6058 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
6059 fsctl, ttl, offset, to_copy);
6060 if (tevent_req_nomem(subreq, req)) {
6061 return tevent_req_post(req, ev);
6063 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
6067 static void fruit_offload_read_done(struct tevent_req *subreq)
6069 struct tevent_req *req = tevent_req_callback_data(
6070 subreq, struct tevent_req);
6071 struct fruit_offload_read_state *state = tevent_req_data(
6072 req, struct fruit_offload_read_state);
6075 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
6079 TALLOC_FREE(subreq);
6080 if (tevent_req_nterror(req, status)) {
6084 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
6085 tevent_req_done(req);
6089 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
6090 &fruit_offload_ctx);
6091 if (tevent_req_nterror(req, status)) {
6095 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
6098 if (tevent_req_nterror(req, status)) {
6102 tevent_req_done(req);
6106 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6107 struct vfs_handle_struct *handle,
6108 TALLOC_CTX *mem_ctx,
6111 struct fruit_offload_read_state *state = tevent_req_data(
6112 req, struct fruit_offload_read_state);
6115 if (tevent_req_is_nterror(req, &status)) {
6116 tevent_req_received(req);
6120 token->length = state->token.length;
6121 token->data = talloc_move(mem_ctx, &state->token.data);
6123 tevent_req_received(req);
6124 return NT_STATUS_OK;
6127 struct fruit_offload_write_state {
6128 struct vfs_handle_struct *handle;
6130 struct files_struct *src_fsp;
6131 struct files_struct *dst_fsp;
6135 static void fruit_offload_write_done(struct tevent_req *subreq);
6136 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6137 TALLOC_CTX *mem_ctx,
6138 struct tevent_context *ev,
6141 off_t transfer_offset,
6142 struct files_struct *dest_fsp,
6146 struct tevent_req *req, *subreq;
6147 struct fruit_offload_write_state *state;
6149 struct fruit_config_data *config;
6150 off_t src_off = transfer_offset;
6151 files_struct *src_fsp = NULL;
6152 off_t to_copy = num;
6153 bool copyfile_enabled = false;
6155 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6156 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6158 SMB_VFS_HANDLE_GET_DATA(handle, config,
6159 struct fruit_config_data,
6162 req = tevent_req_create(mem_ctx, &state,
6163 struct fruit_offload_write_state);
6167 state->handle = handle;
6168 state->dst_fsp = dest_fsp;
6171 case FSCTL_SRV_COPYCHUNK:
6172 case FSCTL_SRV_COPYCHUNK_WRITE:
6173 copyfile_enabled = config->copyfile_enabled;
6180 * Check if this a OS X copyfile style copychunk request with
6181 * a requested chunk count of 0 that was translated to a
6182 * offload_write_send VFS call overloading the parameters src_off
6183 * = dest_off = num = 0.
6185 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6186 status = vfs_offload_token_db_fetch_fsp(
6187 fruit_offload_ctx, token, &src_fsp);
6188 if (tevent_req_nterror(req, status)) {
6189 return tevent_req_post(req, ev);
6191 state->src_fsp = src_fsp;
6193 status = vfs_stat_fsp(src_fsp);
6194 if (tevent_req_nterror(req, status)) {
6195 return tevent_req_post(req, ev);
6198 to_copy = src_fsp->fsp_name->st.st_ex_size;
6199 state->is_copyfile = true;
6202 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6211 if (tevent_req_nomem(subreq, req)) {
6212 return tevent_req_post(req, ev);
6215 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6219 static void fruit_offload_write_done(struct tevent_req *subreq)
6221 struct tevent_req *req = tevent_req_callback_data(
6222 subreq, struct tevent_req);
6223 struct fruit_offload_write_state *state = tevent_req_data(
6224 req, struct fruit_offload_write_state);
6226 unsigned int num_streams = 0;
6227 struct stream_struct *streams = NULL;
6229 struct smb_filename *src_fname_tmp = NULL;
6230 struct smb_filename *dst_fname_tmp = NULL;
6232 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6235 TALLOC_FREE(subreq);
6236 if (tevent_req_nterror(req, status)) {
6240 if (!state->is_copyfile) {
6241 tevent_req_done(req);
6246 * Now copy all remaining streams. We know the share supports
6247 * streams, because we're in vfs_fruit. We don't do this async
6248 * because streams are few and small.
6250 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6251 state->src_fsp->fsp_name,
6252 req, &num_streams, &streams);
6253 if (tevent_req_nterror(req, status)) {
6257 if (num_streams == 1) {
6258 /* There is always one stream, ::$DATA. */
6259 tevent_req_done(req);
6263 for (i = 0; i < num_streams; i++) {
6264 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6265 __func__, streams[i].name, (size_t)streams[i].size));
6267 src_fname_tmp = synthetic_smb_fname(
6269 state->src_fsp->fsp_name->base_name,
6272 state->src_fsp->fsp_name->flags);
6273 if (tevent_req_nomem(src_fname_tmp, req)) {
6277 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6278 TALLOC_FREE(src_fname_tmp);
6282 dst_fname_tmp = synthetic_smb_fname(
6284 state->dst_fsp->fsp_name->base_name,
6287 state->dst_fsp->fsp_name->flags);
6288 if (tevent_req_nomem(dst_fname_tmp, req)) {
6289 TALLOC_FREE(src_fname_tmp);
6293 status = copy_file(req,
6294 state->handle->conn,
6297 OPENX_FILE_CREATE_IF_NOT_EXIST,
6299 if (!NT_STATUS_IS_OK(status)) {
6300 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6301 smb_fname_str_dbg(src_fname_tmp),
6302 smb_fname_str_dbg(dst_fname_tmp),
6303 nt_errstr(status)));
6304 TALLOC_FREE(src_fname_tmp);
6305 TALLOC_FREE(dst_fname_tmp);
6306 tevent_req_nterror(req, status);
6310 TALLOC_FREE(src_fname_tmp);
6311 TALLOC_FREE(dst_fname_tmp);
6314 TALLOC_FREE(streams);
6315 TALLOC_FREE(src_fname_tmp);
6316 TALLOC_FREE(dst_fname_tmp);
6317 tevent_req_done(req);
6320 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6321 struct tevent_req *req,
6324 struct fruit_offload_write_state *state = tevent_req_data(
6325 req, struct fruit_offload_write_state);
6328 if (tevent_req_is_nterror(req, &status)) {
6329 DEBUG(1, ("server side copy chunk failed: %s\n",
6330 nt_errstr(status)));
6332 tevent_req_received(req);
6336 *copied = state->copied;
6337 tevent_req_received(req);
6339 return NT_STATUS_OK;
6342 static char *fruit_get_bandsize_line(char **lines, int numlines)
6345 static bool re_initialized = false;
6349 if (!re_initialized) {
6350 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6354 re_initialized = true;
6357 for (i = 0; i < numlines; i++) {
6358 regmatch_t matches[1];
6360 ret = regexec(&re, lines[i], 1, matches, 0);
6363 * Check if the match was on the last line, sa we want
6364 * the subsequent line.
6366 if (i + 1 == numlines) {
6369 return lines[i + 1];
6371 if (ret != REG_NOMATCH) {
6379 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6382 static bool re_initialized = false;
6383 regmatch_t matches[2];
6388 if (!re_initialized) {
6391 "<integer>\\([[:digit:]]*\\)</integer>$",
6396 re_initialized = true;
6399 ret = regexec(&re, line, 2, matches, 0);
6401 DBG_ERR("regex failed [%s]\n", line);
6405 line[matches[1].rm_eo] = '\0';
6407 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6411 *_band_size = (size_t)band_size;
6416 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6417 * "band-size" key and value.
6419 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6423 #define INFO_PLIST_MAX_SIZE 64*1024
6425 struct smb_filename *smb_fname = NULL;
6426 files_struct *fsp = NULL;
6427 uint8_t *file_data = NULL;
6428 char **lines = NULL;
6429 char *band_size_line = NULL;
6430 size_t plist_file_size;
6437 plist = talloc_asprintf(talloc_tos(),
6439 handle->conn->connectpath,
6441 if (plist == NULL) {
6446 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6447 if (smb_fname == NULL) {
6452 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6454 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6459 plist_file_size = smb_fname->st.st_ex_size;
6461 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6462 DBG_INFO("%s is too large, ignoring\n", plist);
6467 status = SMB_VFS_NEXT_CREATE_FILE(
6470 0, /* root_dir_fid */
6471 smb_fname, /* fname */
6472 FILE_GENERIC_READ, /* access_mask */
6473 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6474 FILE_OPEN, /* create_disposition */
6475 0, /* create_options */
6476 0, /* file_attributes */
6477 INTERNAL_OPEN_ONLY, /* oplock_request */
6479 0, /* allocation_size */
6480 0, /* private_flags */
6485 NULL, NULL); /* create context */
6486 if (!NT_STATUS_IS_OK(status)) {
6487 DBG_INFO("Opening [%s] failed [%s]\n",
6488 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6493 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6494 if (file_data == NULL) {
6499 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6500 if (nread != plist_file_size) {
6501 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6502 fsp_str_dbg(fsp), nread, plist_file_size);
6508 status = close_file(NULL, fsp, NORMAL_CLOSE);
6510 if (!NT_STATUS_IS_OK(status)) {
6511 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6516 lines = file_lines_parse((char *)file_data,
6520 if (lines == NULL) {
6525 band_size_line = fruit_get_bandsize_line(lines, numlines);
6526 if (band_size_line == NULL) {
6527 DBG_ERR("Didn't find band-size key in [%s]\n",
6528 smb_fname_str_dbg(smb_fname));
6533 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6535 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6539 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6543 status = close_file(NULL, fsp, NORMAL_CLOSE);
6544 if (!NT_STATUS_IS_OK(status)) {
6545 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6550 TALLOC_FREE(smb_fname);
6551 TALLOC_FREE(file_data);
6556 struct fruit_disk_free_state {
6560 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6565 struct smb_filename *bands_dir = NULL;
6567 struct dirent *e = NULL;
6571 path = talloc_asprintf(talloc_tos(),
6573 handle->conn->connectpath,
6579 bands_dir = synthetic_smb_fname(talloc_tos(),
6585 if (bands_dir == NULL) {
6589 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6591 TALLOC_FREE(bands_dir);
6597 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6599 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6601 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6607 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6609 TALLOC_FREE(bands_dir);
6613 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6615 TALLOC_FREE(bands_dir);
6621 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6622 struct fruit_disk_free_state *state,
6627 size_t sparsebundle_strlen = strlen("sparsebundle");
6628 size_t bandsize = 0;
6632 p = strstr(e->d_name, "sparsebundle");
6637 if (p[sparsebundle_strlen] != '\0') {
6641 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6643 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6646 * Beware of race conditions: this may be an uninitialized
6647 * Info.plist that a client is just creating. We don't want let
6648 * this to trigger complete failure.
6650 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6654 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6657 * Beware of race conditions: this may be a backup sparsebundle
6658 * in an early stage lacking a bands subdirectory. We don't want
6659 * let this to trigger complete failure.
6661 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6665 if (bandsize > SIZE_MAX/nbands) {
6666 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6670 tm_size = bandsize * nbands;
6672 if (state->total_size + tm_size < state->total_size) {
6673 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6678 state->total_size += tm_size;
6680 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6681 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6687 * Calculate used size of a TimeMachine volume
6689 * This assumes that the volume is used only for TimeMachine.
6691 * - readdir(basedir of share), then
6692 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6693 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6694 * - count band files in "\1.sparsebundle/bands/"
6695 * - calculate used size of all bands: band_count * band_size
6697 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6698 const struct smb_filename *smb_fname,
6703 struct fruit_config_data *config = NULL;
6704 struct fruit_disk_free_state state = {0};
6706 struct dirent *e = NULL;
6712 SMB_VFS_HANDLE_GET_DATA(handle, config,
6713 struct fruit_config_data,
6716 if (!config->time_machine ||
6717 config->time_machine_max_size == 0)
6719 return SMB_VFS_NEXT_DISK_FREE(handle,
6726 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6731 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6733 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6735 ok = fruit_tmsize_do_dirent(handle, &state, e);
6737 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6742 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6747 dsize = config->time_machine_max_size / 512;
6748 dfree = dsize - (state.total_size / 512);
6749 if (dfree > dsize) {
6759 static struct vfs_fn_pointers vfs_fruit_fns = {
6760 .connect_fn = fruit_connect,
6761 .disk_free_fn = fruit_disk_free,
6763 /* File operations */
6764 .chmod_fn = fruit_chmod,
6765 .chown_fn = fruit_chown,
6766 .unlink_fn = fruit_unlink,
6767 .rename_fn = fruit_rename,
6768 .rmdir_fn = fruit_rmdir,
6769 .open_fn = fruit_open,
6770 .pread_fn = fruit_pread,
6771 .pwrite_fn = fruit_pwrite,
6772 .pread_send_fn = fruit_pread_send,
6773 .pread_recv_fn = fruit_pread_recv,
6774 .pwrite_send_fn = fruit_pwrite_send,
6775 .pwrite_recv_fn = fruit_pwrite_recv,
6776 .stat_fn = fruit_stat,
6777 .lstat_fn = fruit_lstat,
6778 .fstat_fn = fruit_fstat,
6779 .streaminfo_fn = fruit_streaminfo,
6780 .ntimes_fn = fruit_ntimes,
6781 .ftruncate_fn = fruit_ftruncate,
6782 .fallocate_fn = fruit_fallocate,
6783 .create_file_fn = fruit_create_file,
6784 .readdir_attr_fn = fruit_readdir_attr,
6785 .offload_read_send_fn = fruit_offload_read_send,
6786 .offload_read_recv_fn = fruit_offload_read_recv,
6787 .offload_write_send_fn = fruit_offload_write_send,
6788 .offload_write_recv_fn = fruit_offload_write_recv,
6790 /* NT ACL operations */
6791 .fget_nt_acl_fn = fruit_fget_nt_acl,
6792 .fset_nt_acl_fn = fruit_fset_nt_acl,
6796 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6798 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6800 if (!NT_STATUS_IS_OK(ret)) {
6804 vfs_fruit_debug_level = debug_add_class("fruit");
6805 if (vfs_fruit_debug_level == -1) {
6806 vfs_fruit_debug_level = DBGC_VFS;
6807 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6810 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6811 "vfs_fruit_init","fruit",vfs_fruit_debug_level));