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;
147 * Additional options, all enabled by default,
148 * possibly useful for analyzing performance. The associated
149 * operations with each of them may be expensive, so having
150 * the chance to disable them individually gives a chance
151 * tweaking the setup for the particular usecase.
153 bool readdir_attr_rsize;
154 bool readdir_attr_finder_info;
155 bool readdir_attr_max_access;
158 static const struct enum_list fruit_rsrc[] = {
159 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
160 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
161 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
165 static const struct enum_list fruit_meta[] = {
166 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
167 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
171 static const struct enum_list fruit_locking[] = {
172 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
173 {FRUIT_LOCKING_NONE, "none"},
177 static const struct enum_list fruit_encoding[] = {
178 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
179 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
183 static const char *fruit_catia_maps =
184 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
185 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
186 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
187 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
188 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
189 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
190 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
191 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
192 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
193 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
196 /*****************************************************************************
197 * Defines, functions and data structures that deal with AppleDouble
198 *****************************************************************************/
201 * There are two AppleDouble blobs we deal with:
203 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
204 * metadata in an xattr
206 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
209 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
212 #define AD_VERSION2 0x00020000
213 #define AD_VERSION AD_VERSION2
216 * AppleDouble entry IDs.
218 #define ADEID_DFORK 1
219 #define ADEID_RFORK 2
221 #define ADEID_COMMENT 4
222 #define ADEID_ICONBW 5
223 #define ADEID_ICONCOL 6
224 #define ADEID_FILEI 7
225 #define ADEID_FILEDATESI 8
226 #define ADEID_FINDERI 9
227 #define ADEID_MACFILEI 10
228 #define ADEID_PRODOSFILEI 11
229 #define ADEID_MSDOSFILEI 12
230 #define ADEID_SHORTNAME 13
231 #define ADEID_AFPFILEI 14
234 /* Private Netatalk entries */
235 #define ADEID_PRIVDEV 16
236 #define ADEID_PRIVINO 17
237 #define ADEID_PRIVSYN 18
238 #define ADEID_PRIVID 19
239 #define ADEID_MAX (ADEID_PRIVID + 1)
242 * These are the real ids for the private entries,
243 * as stored in the adouble file
245 #define AD_DEV 0x80444556
246 #define AD_INO 0x80494E4F
247 #define AD_SYN 0x8053594E
248 #define AD_ID 0x8053567E
250 /* Number of actually used entries */
251 #define ADEID_NUM_XATTR 8
252 #define ADEID_NUM_DOT_UND 2
253 #define ADEID_NUM_RSRC_XATTR 1
255 /* AppleDouble magic */
256 #define AD_APPLESINGLE_MAGIC 0x00051600
257 #define AD_APPLEDOUBLE_MAGIC 0x00051607
258 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
260 /* Sizes of relevant entry bits */
261 #define ADEDLEN_MAGIC 4
262 #define ADEDLEN_VERSION 4
263 #define ADEDLEN_FILLER 16
264 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
265 #define AD_FILLER_TAG_OSX "Mac OS X " /* should be 16 bytes */
266 #define ADEDLEN_NENTRIES 2
267 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
268 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
269 #define AD_ENTRY_LEN_EID 4
270 #define AD_ENTRY_LEN_OFF 4
271 #define AD_ENTRY_LEN_LEN 4
272 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
275 #define ADEDLEN_NAME 255
276 #define ADEDLEN_COMMENT 200
277 #define ADEDLEN_FILEI 16
278 #define ADEDLEN_FINDERI 32
279 #define ADEDLEN_FILEDATESI 16
280 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
281 #define ADEDLEN_AFPFILEI 4
282 #define ADEDLEN_MACFILEI 4
283 #define ADEDLEN_PRODOSFILEI 8
284 #define ADEDLEN_MSDOSFILEI 2
285 #define ADEDLEN_DID 4
286 #define ADEDLEN_PRIVDEV 8
287 #define ADEDLEN_PRIVINO 8
288 #define ADEDLEN_PRIVSYN 8
289 #define ADEDLEN_PRIVID 4
292 #define ADEDOFF_MAGIC 0
293 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
294 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
295 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
297 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
298 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
299 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
300 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
301 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
303 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
304 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
305 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
306 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
308 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
309 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
310 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
312 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
313 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
314 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
315 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
316 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
317 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
319 #if AD_DATASZ_XATTR != 402
320 #error bad size for AD_DATASZ_XATTR
323 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
324 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
326 #if AD_DATASZ_DOT_UND != 82
327 #error bad size for AD_DATASZ_DOT_UND
331 * Sharemode locks fcntl() offsets
333 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
334 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
336 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
338 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
340 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
341 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
342 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
343 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
344 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
345 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
346 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
347 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
348 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
349 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
351 /* Time stuff we overload the bits a little */
352 #define AD_DATE_CREATE 0
353 #define AD_DATE_MODIFY 4
354 #define AD_DATE_BACKUP 8
355 #define AD_DATE_ACCESS 12
356 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
357 AD_DATE_BACKUP | AD_DATE_ACCESS)
358 #define AD_DATE_UNIX (1 << 10)
359 #define AD_DATE_START 0x80000000
360 #define AD_DATE_DELTA 946684800
361 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
362 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
364 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
365 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
366 #define AD_XATTR_HDR_SIZE 36
367 #define AD_XATTR_MAX_HDR_SIZE 65536
369 /* Accessor macros */
370 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
371 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
372 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
373 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
376 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
377 * representation as well as the on-disk format.
379 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
380 * the length of the FinderInfo entry is larger then 32 bytes. It is then
381 * preceeded with 2 bytes padding.
383 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
386 struct ad_xattr_header {
387 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
388 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
389 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
390 uint32_t adx_data_start; /* file offset to attribute data area */
391 uint32_t adx_data_length; /* length of attribute data area */
392 uint32_t adx_reserved[3];
394 uint16_t adx_num_attrs;
397 /* On-disk entries are aligned on 4 byte boundaries */
398 struct ad_xattr_entry {
399 uint32_t adx_offset; /* file offset to data */
400 uint32_t adx_length; /* size of attribute data */
402 uint8_t adx_namelen; /* included the NULL terminator */
403 char *adx_name; /* NULL-terminated UTF-8 name */
412 vfs_handle_struct *ad_handle;
415 adouble_type_t ad_type;
418 uint8_t ad_filler[ADEDLEN_FILLER];
419 struct ad_entry ad_eid[ADEID_MAX];
421 struct ad_xattr_header adx_header;
422 struct ad_xattr_entry *adx_entries;
425 struct ad_entry_order {
426 uint32_t id, offset, len;
429 /* Netatalk AppleDouble metadata xattr */
431 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
432 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
433 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
434 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
435 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
436 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
437 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
438 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
439 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
443 /* AppleDouble resource fork file (the ones prefixed by "._") */
445 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
446 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
447 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
452 * Fake AppleDouble entry oder for resource fork xattr. The xattr
453 * isn't an AppleDouble file, it simply contains the resource data,
454 * but in order to be able to use some API calls like ad_getentryoff()
455 * we build a fake/helper struct adouble with this entry order struct.
458 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
463 /* Conversion from enumerated id to on-disk AppleDouble id */
464 #define AD_EID_DISK(a) (set_eid[a])
465 static const uint32_t set_eid[] = {
466 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
467 AD_DEV, AD_INO, AD_SYN, AD_ID
471 /* tcon config handle */
472 struct fruit_config_data *config;
474 /* Denote stream type, meta or rsrc */
479 * Forward declarations
481 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
482 adouble_type_t type);
483 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
484 static int ad_fset(struct adouble *ad, files_struct *fsp);
485 static int adouble_path(TALLOC_CTX *ctx,
486 const struct smb_filename *smb_fname__in,
487 struct smb_filename **ppsmb_fname_out);
488 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
489 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
490 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
494 * Return a pointer to an AppleDouble entry
496 * Returns NULL if the entry is not present
498 static char *ad_get_entry(const struct adouble *ad, int eid)
500 off_t off = ad_getentryoff(ad, eid);
501 size_t len = ad_getentrylen(ad, eid);
503 if (off == 0 || len == 0) {
507 return ad->ad_data + off;
513 static int ad_getdate(const struct adouble *ad,
514 unsigned int dateoff,
517 bool xlate = (dateoff & AD_DATE_UNIX);
520 dateoff &= AD_DATE_MASK;
521 p = ad_get_entry(ad, ADEID_FILEDATESI);
526 if (dateoff > AD_DATE_ACCESS) {
530 memcpy(date, p + dateoff, sizeof(uint32_t));
533 *date = AD_DATE_TO_UNIX(*date);
541 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
543 bool xlate = (dateoff & AD_DATE_UNIX);
546 p = ad_get_entry(ad, ADEID_FILEDATESI);
551 dateoff &= AD_DATE_MASK;
553 date = AD_DATE_FROM_UNIX(date);
556 if (dateoff > AD_DATE_ACCESS) {
560 memcpy(p + dateoff, &date, sizeof(date));
567 * Map on-disk AppleDouble id to enumerated id
569 static uint32_t get_eid(uint32_t eid)
577 return ADEID_PRIVDEV;
579 return ADEID_PRIVINO;
581 return ADEID_PRIVSYN;
592 * Pack AppleDouble structure into data buffer
594 static bool ad_pack(struct adouble *ad)
601 bufsize = talloc_get_size(ad->ad_data);
602 if (bufsize < AD_DATASZ_DOT_UND) {
603 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
607 if (offset + ADEDLEN_MAGIC < offset ||
608 offset + ADEDLEN_MAGIC >= bufsize) {
611 RSIVAL(ad->ad_data, offset, ad->ad_magic);
612 offset += ADEDLEN_MAGIC;
614 if (offset + ADEDLEN_VERSION < offset ||
615 offset + ADEDLEN_VERSION >= bufsize) {
618 RSIVAL(ad->ad_data, offset, ad->ad_version);
619 offset += ADEDLEN_VERSION;
621 if (offset + ADEDLEN_FILLER < offset ||
622 offset + ADEDLEN_FILLER >= bufsize) {
625 if (ad->ad_type == ADOUBLE_RSRC) {
626 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
628 offset += ADEDLEN_FILLER;
630 if (offset + ADEDLEN_NENTRIES < offset ||
631 offset + ADEDLEN_NENTRIES >= bufsize) {
634 offset += ADEDLEN_NENTRIES;
636 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
637 if (ad->ad_eid[eid].ade_off == 0) {
639 * ade_off is also used as indicator whether a
640 * specific entry is used or not
645 if (offset + AD_ENTRY_LEN_EID < offset ||
646 offset + AD_ENTRY_LEN_EID >= bufsize) {
649 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
650 offset += AD_ENTRY_LEN_EID;
652 if (offset + AD_ENTRY_LEN_OFF < offset ||
653 offset + AD_ENTRY_LEN_OFF >= bufsize) {
656 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
657 offset += AD_ENTRY_LEN_OFF;
659 if (offset + AD_ENTRY_LEN_LEN < offset ||
660 offset + AD_ENTRY_LEN_LEN >= bufsize) {
663 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
664 offset += AD_ENTRY_LEN_LEN;
669 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
672 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
677 static bool ad_unpack_xattrs(struct adouble *ad)
679 struct ad_xattr_header *h = &ad->adx_header;
680 const char *p = ad->ad_data;
684 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
688 /* 2 bytes padding */
689 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
691 h->adx_magic = RIVAL(p, hoff + 0);
692 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
693 h->adx_total_size = RIVAL(p, hoff + 8);
694 h->adx_data_start = RIVAL(p, hoff + 12);
695 h->adx_data_length = RIVAL(p, hoff + 16);
696 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
697 h->adx_num_attrs = RSVAL(p, hoff + 34);
699 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
700 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
704 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
705 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
708 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
709 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
713 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
714 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
718 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
719 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
722 if ((h->adx_data_start + h->adx_data_length) >
723 ad->adx_header.adx_total_size)
725 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
729 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
730 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
734 if (h->adx_num_attrs == 0) {
738 ad->adx_entries = talloc_zero_array(
739 ad, struct ad_xattr_entry, h->adx_num_attrs);
740 if (ad->adx_entries == NULL) {
744 hoff += AD_XATTR_HDR_SIZE;
746 for (i = 0; i < h->adx_num_attrs; i++) {
747 struct ad_xattr_entry *e = &ad->adx_entries[i];
749 hoff = (hoff + 3) & ~3;
751 e->adx_offset = RIVAL(p, hoff + 0);
752 e->adx_length = RIVAL(p, hoff + 4);
753 e->adx_flags = RSVAL(p, hoff + 8);
754 e->adx_namelen = *(p + hoff + 10);
756 if (e->adx_offset >= ad->adx_header.adx_total_size) {
757 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
762 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
763 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
768 if ((e->adx_offset + e->adx_length) >
769 ad->adx_header.adx_total_size)
771 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
776 if (e->adx_namelen == 0) {
777 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
781 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
782 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
786 if ((hoff + 11 + e->adx_namelen) >
787 ad->adx_header.adx_data_start)
789 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
794 e->adx_name = talloc_strndup(ad->adx_entries,
797 if (e->adx_name == NULL) {
801 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
802 e->adx_name, e->adx_offset, e->adx_length);
803 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
806 hoff += 11 + e->adx_namelen;
813 * Unpack an AppleDouble blob into a struct adoble
815 static bool ad_unpack(struct adouble *ad, const size_t nentries,
818 size_t bufsize = talloc_get_size(ad->ad_data);
820 uint32_t eid, len, off;
824 * The size of the buffer ad->ad_data is checked when read, so
825 * we wouldn't have to check our own offsets, a few extra
826 * checks won't hurt though. We have to check the offsets we
827 * read from the buffer anyway.
830 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
831 DEBUG(1, ("bad size\n"));
835 ad->ad_magic = RIVAL(ad->ad_data, 0);
836 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
837 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
838 DEBUG(1, ("wrong magic or version\n"));
842 memcpy(ad->ad_filler, ad->ad_data + ADEDOFF_FILLER, ADEDLEN_FILLER);
844 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
845 if (adentries != nentries) {
846 DEBUG(1, ("invalid number of entries: %zu\n",
851 /* now, read in the entry bits */
852 for (i = 0; i < adentries; i++) {
853 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
855 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
856 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
858 if (!eid || eid >= ADEID_MAX) {
859 DEBUG(1, ("bogus eid %d\n", eid));
864 * All entries other than the resource fork are
865 * expected to be read into the ad_data buffer, so
866 * ensure the specified offset is within that bound
868 if ((off > bufsize) && (eid != ADEID_RFORK)) {
869 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
875 * All entries besides FinderInfo and resource fork
876 * must fit into the buffer. FinderInfo is special as
877 * it may be larger then the default 32 bytes (if it
878 * contains marshalled xattrs), but we will fixup that
879 * in ad_convert(). And the resource fork is never
880 * accessed directly by the ad_data buf (also see
881 * comment above) anyway.
883 if ((eid != ADEID_RFORK) &&
884 (eid != ADEID_FINDERI) &&
885 ((off + len) > bufsize)) {
886 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
892 * That would be obviously broken
894 if (off > filesize) {
895 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
901 * Check for any entry that has its end beyond the
904 if (off + len < off) {
905 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
906 ", len: %" PRIu32 "\n",
911 if (off + len > filesize) {
913 * If this is the resource fork entry, we fix
914 * up the length, for any other entry we bail
917 if (eid != ADEID_RFORK) {
918 DEBUG(1, ("bogus eid %d: off: %" PRIu32
919 ", len: %" PRIu32 "\n",
925 * Fixup the resource fork entry by limiting
926 * the size to entryoffset - filesize.
928 len = filesize - off;
929 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
930 ", len: %" PRIu32 "\n", off, len));
933 ad->ad_eid[eid].ade_off = off;
934 ad->ad_eid[eid].ade_len = len;
937 ok = ad_unpack_xattrs(ad);
945 static bool ad_convert_xattr(struct adouble *ad,
946 const struct smb_filename *smb_fname,
949 static struct char_mappings **string_replace_cmaps = NULL;
954 if (ad->adx_header.adx_num_attrs == 0) {
958 if (string_replace_cmaps == NULL) {
959 const char **mappings = NULL;
961 mappings = str_list_make_v3_const(
962 talloc_tos(), fruit_catia_maps, NULL);
963 if (mappings == NULL) {
966 string_replace_cmaps = string_replace_init_map(mappings);
967 TALLOC_FREE(mappings);
970 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
971 struct ad_xattr_entry *e = &ad->adx_entries[i];
972 char *mapped_name = NULL;
974 struct smb_filename *stream_name = NULL;
975 files_struct *fsp = NULL;
978 status = string_replace_allocate(ad->ad_handle->conn,
980 string_replace_cmaps,
983 vfs_translate_to_windows);
984 if (!NT_STATUS_IS_OK(status) &&
985 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
987 DBG_ERR("string_replace_allocate failed\n");
992 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
994 if (mapped_name == NULL) {
998 stream_name = synthetic_smb_fname(talloc_tos(),
999 smb_fname->base_name,
1003 TALLOC_FREE(mapped_name);
1004 if (stream_name == NULL) {
1005 DBG_ERR("synthetic_smb_fname failed\n");
1009 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1011 status = SMB_VFS_CREATE_FILE(
1012 ad->ad_handle->conn, /* conn */
1014 0, /* root_dir_fid */
1015 stream_name, /* fname */
1016 FILE_GENERIC_WRITE, /* access_mask */
1017 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1018 FILE_OPEN_IF, /* create_disposition */
1019 0, /* create_options */
1020 0, /* file_attributes */
1021 INTERNAL_OPEN_ONLY, /* oplock_request */
1023 0, /* allocation_size */
1024 0, /* private_flags */
1029 NULL, NULL); /* create context */
1030 TALLOC_FREE(stream_name);
1031 if (!NT_STATUS_IS_OK(status)) {
1032 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1036 nwritten = SMB_VFS_PWRITE(fsp,
1037 map + e->adx_offset,
1040 if (nwritten == -1) {
1041 DBG_ERR("SMB_VFS_PWRITE failed\n");
1042 saved_errno = errno;
1043 close_file(NULL, fsp, ERROR_CLOSE);
1044 errno = saved_errno;
1048 status = close_file(NULL, fsp, NORMAL_CLOSE);
1049 if (!NT_STATUS_IS_OK(status)) {
1055 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1059 static bool ad_convert_finderinfo(struct adouble *ad,
1060 const struct smb_filename *smb_fname)
1065 struct smb_filename *stream_name = NULL;
1066 files_struct *fsp = NULL;
1070 int saved_errno = 0;
1072 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1077 ai = afpinfo_new(talloc_tos());
1082 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1084 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1085 if (aiblob.data == NULL) {
1090 size = afpinfo_pack(ai, (char *)aiblob.data);
1092 if (size != AFP_INFO_SIZE) {
1096 stream_name = synthetic_smb_fname(talloc_tos(),
1097 smb_fname->base_name,
1101 if (stream_name == NULL) {
1102 data_blob_free(&aiblob);
1103 DBG_ERR("synthetic_smb_fname failed\n");
1107 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1109 status = SMB_VFS_CREATE_FILE(
1110 ad->ad_handle->conn, /* conn */
1112 0, /* root_dir_fid */
1113 stream_name, /* fname */
1114 FILE_GENERIC_WRITE, /* access_mask */
1115 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1116 FILE_OPEN_IF, /* create_disposition */
1117 0, /* create_options */
1118 0, /* file_attributes */
1119 INTERNAL_OPEN_ONLY, /* oplock_request */
1121 0, /* allocation_size */
1122 0, /* private_flags */
1127 NULL, NULL); /* create context */
1128 TALLOC_FREE(stream_name);
1129 if (!NT_STATUS_IS_OK(status)) {
1130 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1134 nwritten = SMB_VFS_PWRITE(fsp,
1138 if (nwritten == -1) {
1139 DBG_ERR("SMB_VFS_PWRITE failed\n");
1140 saved_errno = errno;
1141 close_file(NULL, fsp, ERROR_CLOSE);
1142 errno = saved_errno;
1146 status = close_file(NULL, fsp, NORMAL_CLOSE);
1147 if (!NT_STATUS_IS_OK(status)) {
1155 static bool ad_convert_truncate(struct adouble *ad,
1156 const struct smb_filename *smb_fname)
1161 * FIXME: direct ftruncate(), but we don't have a fsp for the
1164 rc = ftruncate(ad->ad_fd, ADEDOFF_RFORK_DOT_UND +
1165 ad_getentrylen(ad, ADEID_RFORK));
1173 static bool ad_convert_move_reso(struct adouble *ad,
1174 const struct smb_filename *smb_fname,
1177 if (ad_getentrylen(ad, ADEID_RFORK) == 0) {
1181 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1182 map + ad_getentryoff(ad, ADEID_RFORK),
1183 ad_getentrylen(ad, ADEID_RFORK));
1185 ad_setentryoff(ad, ADEID_RFORK,
1186 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1192 * Convert from Apple's ._ file to Netatalk
1194 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1195 * bytes containing packed xattrs.
1197 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1200 static int ad_convert(struct adouble *ad,
1201 const struct smb_filename *smb_fname)
1204 char *map = MAP_FAILED;
1209 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1213 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1214 ad_getentrylen(ad, ADEID_RFORK);
1216 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1217 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED,
1219 if (map == MAP_FAILED) {
1220 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1224 ok = ad_convert_xattr(ad, smb_fname, map);
1226 munmap(map, origlen);
1230 ok = ad_convert_move_reso(ad, smb_fname, map);
1232 munmap(map, origlen);
1236 ok = ad_convert_truncate(ad, smb_fname);
1238 munmap(map, origlen);
1242 rc = munmap(map, origlen);
1244 DBG_ERR("munmap failed: %s\n", strerror(errno));
1250 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1254 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1255 if (len != AD_DATASZ_DOT_UND) {
1256 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1260 ok = ad_convert_finderinfo(ad, smb_fname);
1262 DBG_ERR("Failed to convert [%s]\n",
1263 smb_fname_str_dbg(smb_fname));
1271 * Read and parse Netatalk AppleDouble metadata xattr
1273 static ssize_t ad_read_meta(struct adouble *ad,
1274 const struct smb_filename *smb_fname)
1280 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1282 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1283 AFPINFO_EA_NETATALK, ad->ad_data,
1289 if (errno == ENOATTR) {
1295 DEBUG(2, ("error reading meta xattr: %s\n",
1301 if (ealen != AD_DATASZ_XATTR) {
1302 DEBUG(2, ("bad size %zd\n", ealen));
1308 /* Now parse entries */
1309 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1311 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1317 if (!ad_getentryoff(ad, ADEID_FINDERI)
1318 || !ad_getentryoff(ad, ADEID_COMMENT)
1319 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1320 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1321 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1322 || !ad_getentryoff(ad, ADEID_PRIVINO)
1323 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1324 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1325 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1332 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1333 smb_fname->base_name, rc));
1337 if (errno == EINVAL) {
1339 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1347 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1351 #ifdef HAVE_ATTROPEN
1352 /* FIXME: direct Solaris xattr syscall */
1353 return attropen(smb_fname->base_name,
1354 AFPRESOURCE_EA_NETATALK, flags, mode);
1361 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1367 struct smb_filename *adp_smb_fname = NULL;
1369 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1374 fd = open(adp_smb_fname->base_name, flags, mode);
1375 TALLOC_FREE(adp_smb_fname);
1380 static int ad_open_rsrc(vfs_handle_struct *handle,
1381 const struct smb_filename *smb_fname,
1385 struct fruit_config_data *config = NULL;
1388 SMB_VFS_HANDLE_GET_DATA(handle, config,
1389 struct fruit_config_data, return -1);
1391 if (config->rsrc == FRUIT_RSRC_XATTR) {
1392 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1394 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1401 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1402 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1403 * for file IO on the ._ file.
1405 static int ad_open(vfs_handle_struct *handle,
1408 const struct smb_filename *smb_fname,
1414 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1415 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1417 if (ad->ad_type == ADOUBLE_META) {
1421 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1422 ad->ad_fd = fsp->fh->fd;
1423 ad->ad_opened = false;
1427 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1431 ad->ad_opened = true;
1434 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1435 smb_fname->base_name,
1436 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1441 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1446 /* FIXME: direct sys_fstat(), don't have an fsp */
1447 ret = sys_fstat(ad->ad_fd, &st,
1448 lp_fake_directory_create_times(
1449 SNUM(ad->ad_handle->conn)));
1454 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1455 return st.st_ex_size;
1458 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1459 const struct smb_filename *smb_fname)
1461 SMB_STRUCT_STAT sbuf;
1468 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1469 SNUM(ad->ad_handle->conn)));
1475 * AppleDouble file header content and size, two cases:
1477 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1478 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1480 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1482 size = sbuf.st_ex_size;
1483 if (size > talloc_array_length(ad->ad_data)) {
1484 if (size > AD_XATTR_MAX_HDR_SIZE) {
1485 size = AD_XATTR_MAX_HDR_SIZE;
1487 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1494 len = sys_pread(ad->ad_fd, ad->ad_data,
1495 talloc_array_length(ad->ad_data), 0);
1496 if (len != talloc_array_length(ad->ad_data)) {
1497 DBG_NOTICE("%s %s: bad size: %zd\n",
1498 smb_fname->base_name, strerror(errno), len);
1502 /* Now parse entries */
1503 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1505 DBG_ERR("invalid AppleDouble resource %s\n",
1506 smb_fname->base_name);
1511 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1512 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1513 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1514 DBG_ERR("invalid AppleDouble resource %s\n",
1515 smb_fname->base_name);
1521 * Try to fixup AppleDouble files created by OS X with xattrs
1522 * appended to the ADEID_FINDERI entry.
1525 ret = ad_convert(ad, smb_fname);
1527 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1535 * Read and parse resource fork, either ._ AppleDouble file or xattr
1537 static ssize_t ad_read_rsrc(struct adouble *ad,
1538 const struct smb_filename *smb_fname)
1540 struct fruit_config_data *config = NULL;
1543 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1544 struct fruit_config_data, return -1);
1546 if (config->rsrc == FRUIT_RSRC_XATTR) {
1547 len = ad_read_rsrc_xattr(ad);
1549 len = ad_read_rsrc_adouble(ad, smb_fname);
1556 * Read and unpack an AppleDouble metadata xattr or resource
1558 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1560 switch (ad->ad_type) {
1562 return ad_read_meta(ad, smb_fname);
1564 return ad_read_rsrc(ad, smb_fname);
1570 static int adouble_destructor(struct adouble *ad)
1572 if ((ad->ad_fd != -1) && ad->ad_opened) {
1580 * Allocate a struct adouble without initialiing it
1582 * The struct is either hang of the fsp extension context or if fsp is
1585 * @param[in] ctx talloc context
1586 * @param[in] handle vfs handle
1587 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1589 * @return adouble handle
1591 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1592 adouble_type_t type)
1597 struct fruit_config_data *config;
1599 SMB_VFS_HANDLE_GET_DATA(handle, config,
1600 struct fruit_config_data, return NULL);
1604 adsize = AD_DATASZ_XATTR;
1607 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1608 adsize = AD_DATASZ_DOT_UND;
1615 ad = talloc_zero(ctx, struct adouble);
1622 ad->ad_data = talloc_zero_array(ad, char, adsize);
1623 if (ad->ad_data == NULL) {
1629 ad->ad_handle = handle;
1631 ad->ad_magic = AD_MAGIC;
1632 ad->ad_version = AD_VERSION;
1635 talloc_set_destructor(ad, adouble_destructor);
1645 * Allocate and initialize a new struct adouble
1647 * @param[in] ctx talloc context
1648 * @param[in] handle vfs handle
1649 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1651 * @return adouble handle, initialized
1653 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1654 adouble_type_t type)
1657 const struct ad_entry_order *eid;
1658 struct adouble *ad = NULL;
1659 struct fruit_config_data *config;
1660 time_t t = time(NULL);
1662 SMB_VFS_HANDLE_GET_DATA(handle, config,
1663 struct fruit_config_data, return NULL);
1667 eid = entry_order_meta_xattr;
1670 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1671 eid = entry_order_dot_und;
1673 eid = entry_order_rsrc_xattr;
1680 ad = ad_alloc(ctx, handle, type);
1686 ad->ad_eid[eid->id].ade_off = eid->offset;
1687 ad->ad_eid[eid->id].ade_len = eid->len;
1691 /* put something sane in the date fields */
1692 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1693 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1694 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1695 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1703 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1704 vfs_handle_struct *handle,
1706 const struct smb_filename *smb_fname,
1707 adouble_type_t type)
1711 struct adouble *ad = NULL;
1715 smb_fname = fsp->base_fsp->fsp_name;
1718 DEBUG(10, ("ad_get(%s) called for %s\n",
1719 type == ADOUBLE_META ? "meta" : "rsrc",
1720 smb_fname->base_name));
1722 ad = ad_alloc(ctx, handle, type);
1728 /* Try rw first so we can use the fd in ad_convert() */
1731 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1732 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1734 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1737 DBG_DEBUG("ad_open [%s] error [%s]\n",
1738 smb_fname->base_name, strerror(errno));
1743 len = ad_read(ad, smb_fname);
1745 DEBUG(10, ("error reading AppleDouble for %s\n",
1746 smb_fname->base_name));
1752 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1753 type == ADOUBLE_META ? "meta" : "rsrc",
1754 smb_fname->base_name, rc));
1763 * Return AppleDouble data for a file
1765 * @param[in] ctx talloc context
1766 * @param[in] handle vfs handle
1767 * @param[in] smb_fname pathname to file or directory
1768 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1770 * @return talloced struct adouble or NULL on error
1772 static struct adouble *ad_get(TALLOC_CTX *ctx,
1773 vfs_handle_struct *handle,
1774 const struct smb_filename *smb_fname,
1775 adouble_type_t type)
1777 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1781 * Return AppleDouble data for a file
1783 * @param[in] ctx talloc context
1784 * @param[in] handle vfs handle
1785 * @param[in] fsp fsp to use for IO
1786 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1788 * @return talloced struct adouble or NULL on error
1790 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1791 files_struct *fsp, adouble_type_t type)
1793 return ad_get_internal(ctx, handle, fsp, NULL, type);
1797 * Set AppleDouble metadata on a file or directory
1799 * @param[in] ad adouble handle
1801 * @param[in] smb_fname pathname to file or directory
1803 * @return status code, 0 means success
1805 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1810 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1812 if (ad->ad_type != ADOUBLE_META) {
1813 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1814 smb_fname->base_name);
1823 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1825 AFPINFO_EA_NETATALK,
1827 AD_DATASZ_XATTR, 0);
1829 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1835 * Set AppleDouble metadata on a file or directory
1837 * @param[in] ad adouble handle
1838 * @param[in] fsp file handle
1840 * @return status code, 0 means success
1842 static int ad_fset(struct adouble *ad, files_struct *fsp)
1848 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1851 || (fsp->fh == NULL)
1852 || (fsp->fh->fd == -1))
1854 smb_panic("bad fsp");
1862 switch (ad->ad_type) {
1864 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1866 AFPINFO_EA_NETATALK,
1868 AD_DATASZ_XATTR, 0);
1872 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1877 if (len != AD_DATASZ_DOT_UND) {
1878 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1888 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1893 /*****************************************************************************
1895 *****************************************************************************/
1897 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1899 if (strncasecmp_m(smb_fname->stream_name,
1900 AFPINFO_STREAM_NAME,
1901 strlen(AFPINFO_STREAM_NAME)) == 0) {
1907 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1909 if (strncasecmp_m(smb_fname->stream_name,
1910 AFPRESOURCE_STREAM_NAME,
1911 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1918 * Test whether stream is an Apple stream, not used atm
1921 static bool is_apple_stream(const struct smb_filename *smb_fname)
1923 if (is_afpinfo_stream(smb_fname)) {
1926 if (is_afpresource_stream(smb_fname)) {
1934 * Initialize config struct from our smb.conf config parameters
1936 static int init_fruit_config(vfs_handle_struct *handle)
1938 struct fruit_config_data *config;
1940 const char *tm_size_str = NULL;
1942 config = talloc_zero(handle->conn, struct fruit_config_data);
1944 DEBUG(1, ("talloc_zero() failed\n"));
1950 * Versions up to Samba 4.5.x had a spelling bug in the
1951 * fruit:resource option calling lp_parm_enum with
1952 * "res*s*ource" (ie two s).
1954 * In Samba 4.6 we accept both the wrong and the correct
1955 * spelling, in Samba 4.7 the bad spelling will be removed.
1957 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1958 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1959 if (enumval == -1) {
1960 DEBUG(1, ("value for %s: resource type unknown\n",
1961 FRUIT_PARAM_TYPE_NAME));
1964 config->rsrc = (enum fruit_rsrc)enumval;
1966 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1967 "resource", fruit_rsrc, enumval);
1968 if (enumval == -1) {
1969 DEBUG(1, ("value for %s: resource type unknown\n",
1970 FRUIT_PARAM_TYPE_NAME));
1973 config->rsrc = (enum fruit_rsrc)enumval;
1975 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1976 "metadata", fruit_meta, FRUIT_META_NETATALK);
1977 if (enumval == -1) {
1978 DEBUG(1, ("value for %s: metadata type unknown\n",
1979 FRUIT_PARAM_TYPE_NAME));
1982 config->meta = (enum fruit_meta)enumval;
1984 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1985 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1986 if (enumval == -1) {
1987 DEBUG(1, ("value for %s: locking type unknown\n",
1988 FRUIT_PARAM_TYPE_NAME));
1991 config->locking = (enum fruit_locking)enumval;
1993 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1994 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1995 if (enumval == -1) {
1996 DEBUG(1, ("value for %s: encoding type unknown\n",
1997 FRUIT_PARAM_TYPE_NAME));
2000 config->encoding = (enum fruit_encoding)enumval;
2002 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2003 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
2004 FRUIT_PARAM_TYPE_NAME,
2009 config->use_aapl = lp_parm_bool(
2010 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
2012 config->time_machine = lp_parm_bool(
2013 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
2015 config->unix_info_enabled = lp_parm_bool(
2016 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
2018 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
2021 config->posix_rename = lp_parm_bool(
2022 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
2024 config->aapl_zero_file_id =
2025 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
2027 config->readdir_attr_rsize = lp_parm_bool(
2028 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
2030 config->readdir_attr_finder_info = lp_parm_bool(
2031 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
2033 config->readdir_attr_max_access = lp_parm_bool(
2034 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
2036 config->model = lp_parm_const_string(
2037 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
2039 tm_size_str = lp_parm_const_string(
2040 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2041 "time machine max size", NULL);
2042 if (tm_size_str != NULL) {
2043 config->time_machine_max_size = conv_str_size(tm_size_str);
2046 SMB_VFS_HANDLE_SET_DATA(handle, config,
2047 NULL, struct fruit_config_data,
2054 * Prepend "._" to a basename
2055 * Return a new struct smb_filename with stream_name == NULL.
2057 static int adouble_path(TALLOC_CTX *ctx,
2058 const struct smb_filename *smb_fname_in,
2059 struct smb_filename **pp_smb_fname_out)
2063 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2066 if (smb_fname == NULL) {
2070 /* We need streamname to be NULL */
2071 TALLOC_FREE(smb_fname->stream_name);
2073 /* And we're replacing base_name. */
2074 TALLOC_FREE(smb_fname->base_name);
2076 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2078 TALLOC_FREE(smb_fname);
2082 smb_fname->base_name = talloc_asprintf(smb_fname,
2083 "%s/._%s", parent, base);
2084 if (smb_fname->base_name == NULL) {
2085 TALLOC_FREE(smb_fname);
2089 *pp_smb_fname_out = smb_fname;
2095 * Allocate and initialize an AfpInfo struct
2097 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2099 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2103 ai->afpi_Signature = AFP_Signature;
2104 ai->afpi_Version = AFP_Version;
2105 ai->afpi_BackupTime = AD_DATE_START;
2110 * Pack an AfpInfo struct into a buffer
2112 * Buffer size must be at least AFP_INFO_SIZE
2113 * Returns size of packed buffer
2115 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2117 memset(buf, 0, AFP_INFO_SIZE);
2119 RSIVAL(buf, 0, ai->afpi_Signature);
2120 RSIVAL(buf, 4, ai->afpi_Version);
2121 RSIVAL(buf, 12, ai->afpi_BackupTime);
2122 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2124 return AFP_INFO_SIZE;
2128 * Unpack a buffer into a AfpInfo structure
2130 * Buffer size must be at least AFP_INFO_SIZE
2131 * Returns allocated AfpInfo struct
2133 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2135 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2140 ai->afpi_Signature = RIVAL(data, 0);
2141 ai->afpi_Version = RIVAL(data, 4);
2142 ai->afpi_BackupTime = RIVAL(data, 12);
2143 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2144 sizeof(ai->afpi_FinderInfo));
2146 if (ai->afpi_Signature != AFP_Signature
2147 || ai->afpi_Version != AFP_Version) {
2148 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2156 * Fake an inode number from the md5 hash of the (xattr) name
2158 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2161 unsigned char hash[16];
2165 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2166 SMB_ASSERT(upper_sname != NULL);
2169 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2170 sizeof(sbuf->st_ex_dev));
2171 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2172 sizeof(sbuf->st_ex_ino));
2173 MD5Update(&ctx, (unsigned char *)upper_sname,
2174 talloc_get_size(upper_sname)-1);
2175 MD5Final(hash, &ctx);
2177 TALLOC_FREE(upper_sname);
2179 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2180 memcpy(&result, hash, sizeof(result));
2182 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2183 sname, (unsigned long long)result));
2188 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2189 struct stream_struct **streams,
2190 const char *name, off_t size,
2193 struct stream_struct *tmp;
2195 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2201 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2202 if (tmp[*num_streams].name == NULL) {
2206 tmp[*num_streams].size = size;
2207 tmp[*num_streams].alloc_size = alloc_size;
2214 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2215 struct stream_struct **streams)
2217 struct stream_struct *tmp = *streams;
2220 if (*num_streams == 0) {
2224 for (i = 0; i < *num_streams; i++) {
2225 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2230 if (i == *num_streams) {
2234 if (tmp[i].size > 0) {
2238 TALLOC_FREE(tmp[i].name);
2239 if (*num_streams - 1 > i) {
2240 memmove(&tmp[i], &tmp[i+1],
2241 (*num_streams - i - 1) * sizeof(struct stream_struct));
2248 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2249 struct stream_struct **streams,
2252 struct stream_struct *tmp = *streams;
2255 if (*num_streams == 0) {
2259 for (i = 0; i < *num_streams; i++) {
2260 if (strequal_m(tmp[i].name, name)) {
2265 if (i == *num_streams) {
2269 TALLOC_FREE(tmp[i].name);
2270 if (*num_streams - 1 > i) {
2271 memmove(&tmp[i], &tmp[i+1],
2272 (*num_streams - i - 1) * sizeof(struct stream_struct));
2279 static bool ad_empty_finderinfo(const struct adouble *ad)
2282 char emptybuf[ADEDLEN_FINDERI] = {0};
2285 fi = ad_get_entry(ad, ADEID_FINDERI);
2287 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2291 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2295 static bool ai_empty_finderinfo(const AfpInfo *ai)
2298 char emptybuf[ADEDLEN_FINDERI] = {0};
2300 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2305 * Update btime with btime from Netatalk
2307 static void update_btime(vfs_handle_struct *handle,
2308 struct smb_filename *smb_fname)
2311 struct timespec creation_time = {0};
2313 struct fruit_config_data *config = NULL;
2315 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2318 switch (config->meta) {
2319 case FRUIT_META_STREAM:
2321 case FRUIT_META_NETATALK:
2325 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2329 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2333 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2339 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2340 update_stat_ex_create_time(&smb_fname->st, creation_time);
2346 * Map an access mask to a Netatalk single byte byte range lock
2348 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2349 uint32_t access_mask)
2353 switch (access_mask) {
2354 case FILE_READ_DATA:
2355 offset = AD_FILELOCK_OPEN_RD;
2358 case FILE_WRITE_DATA:
2359 case FILE_APPEND_DATA:
2360 offset = AD_FILELOCK_OPEN_WR;
2364 offset = AD_FILELOCK_OPEN_NONE;
2368 if (fork_type == APPLE_FORK_RSRC) {
2369 if (offset == AD_FILELOCK_OPEN_NONE) {
2370 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2380 * Map a deny mode to a Netatalk brl
2382 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2387 switch (deny_mode) {
2389 offset = AD_FILELOCK_DENY_RD;
2393 offset = AD_FILELOCK_DENY_WR;
2397 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2400 if (fork_type == APPLE_FORK_RSRC) {
2408 * Call fcntl() with an exclusive F_GETLK request in order to
2409 * determine if there's an exisiting shared lock
2411 * @return true if the requested lock was found or any error occurred
2412 * false if the lock was not found
2414 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2417 off_t offset = in_offset;
2422 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2423 if (result == false) {
2427 if (type != F_UNLCK) {
2434 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2436 uint32_t access_mask,
2439 NTSTATUS status = NT_STATUS_OK;
2440 bool open_for_reading, open_for_writing, deny_read, deny_write;
2442 bool have_read = false;
2445 /* FIXME: hardcoded data fork, add resource fork */
2446 enum apple_fork fork_type = APPLE_FORK_DATA;
2448 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2450 access_mask & FILE_READ_DATA ? "READ" :"-",
2451 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2452 deny_mode & DENY_READ ? "DENY_READ" : "-",
2453 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2455 if (fsp->fh->fd == -1) {
2456 return NT_STATUS_OK;
2459 flags = fcntl(fsp->fh->fd, F_GETFL);
2461 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2462 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2463 return map_nt_error_from_unix(errno);
2466 if (flags & (O_RDONLY|O_RDWR)) {
2468 * Applying fcntl read locks requires an fd opened for
2469 * reading. This means we won't be applying locks for
2470 * files openend write-only, but what can we do...
2476 * Check read access and deny read mode
2478 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2480 open_for_reading = test_netatalk_lock(
2481 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2483 deny_read = test_netatalk_lock(
2484 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2486 DEBUG(10, ("read: %s, deny_write: %s\n",
2487 open_for_reading == true ? "yes" : "no",
2488 deny_read == true ? "yes" : "no"));
2490 if (((access_mask & FILE_READ_DATA) && deny_read)
2491 || ((deny_mode & DENY_READ) && open_for_reading)) {
2492 return NT_STATUS_SHARING_VIOLATION;
2496 if ((access_mask & FILE_READ_DATA) && have_read) {
2497 struct byte_range_lock *br_lck = NULL;
2499 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2501 handle->conn->sconn->msg_ctx, fsp,
2502 fsp->op->global->open_persistent_id, 1, off,
2503 READ_LOCK, POSIX_LOCK, false,
2506 TALLOC_FREE(br_lck);
2508 if (!NT_STATUS_IS_OK(status)) {
2513 if ((deny_mode & DENY_READ) && have_read) {
2514 struct byte_range_lock *br_lck = NULL;
2516 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2518 handle->conn->sconn->msg_ctx, fsp,
2519 fsp->op->global->open_persistent_id, 1, off,
2520 READ_LOCK, POSIX_LOCK, false,
2523 TALLOC_FREE(br_lck);
2525 if (!NT_STATUS_IS_OK(status)) {
2532 * Check write access and deny write mode
2534 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2536 open_for_writing = test_netatalk_lock(
2537 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2539 deny_write = test_netatalk_lock(
2540 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2542 DEBUG(10, ("write: %s, deny_write: %s\n",
2543 open_for_writing == true ? "yes" : "no",
2544 deny_write == true ? "yes" : "no"));
2546 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2547 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2548 return NT_STATUS_SHARING_VIOLATION;
2552 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2553 struct byte_range_lock *br_lck = NULL;
2555 off = access_to_netatalk_brl(fork_type, FILE_WRITE_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)) {
2568 if ((deny_mode & DENY_WRITE) && have_read) {
2569 struct byte_range_lock *br_lck = NULL;
2571 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2573 handle->conn->sconn->msg_ctx, fsp,
2574 fsp->op->global->open_persistent_id, 1, off,
2575 READ_LOCK, POSIX_LOCK, false,
2578 TALLOC_FREE(br_lck);
2580 if (!NT_STATUS_IS_OK(status)) {
2589 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2590 struct smb_request *req,
2591 const struct smb2_create_blobs *in_context_blobs,
2592 struct smb2_create_blobs *out_context_blobs)
2594 struct fruit_config_data *config;
2596 struct smb2_create_blob *aapl = NULL;
2600 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2601 uint64_t req_bitmap, client_caps;
2602 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2606 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2607 return NT_STATUS_UNSUCCESSFUL);
2609 if (!config->use_aapl
2610 || in_context_blobs == NULL
2611 || out_context_blobs == NULL) {
2612 return NT_STATUS_OK;
2615 aapl = smb2_create_blob_find(in_context_blobs,
2616 SMB2_CREATE_TAG_AAPL);
2618 return NT_STATUS_OK;
2621 if (aapl->data.length != 24) {
2622 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2623 (uintmax_t)aapl->data.length));
2624 return NT_STATUS_INVALID_PARAMETER;
2627 cmd = IVAL(aapl->data.data, 0);
2628 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2629 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2630 return NT_STATUS_INVALID_PARAMETER;
2633 req_bitmap = BVAL(aapl->data.data, 8);
2634 client_caps = BVAL(aapl->data.data, 16);
2636 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2638 SBVAL(p, 8, req_bitmap);
2639 ok = data_blob_append(req, &blob, p, 16);
2641 return NT_STATUS_UNSUCCESSFUL;
2644 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2645 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2646 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2647 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2648 config->readdir_attr_enabled = true;
2651 if (config->use_copyfile) {
2652 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2653 config->copyfile_enabled = true;
2657 * The client doesn't set the flag, so we can't check
2658 * for it and just set it unconditionally
2660 if (config->unix_info_enabled) {
2661 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2664 SBVAL(p, 0, server_caps);
2665 ok = data_blob_append(req, &blob, p, 8);
2667 return NT_STATUS_UNSUCCESSFUL;
2671 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2672 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2680 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2687 if (config->time_machine) {
2688 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2693 ok = data_blob_append(req, &blob, p, 8);
2695 return NT_STATUS_UNSUCCESSFUL;
2699 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2700 ok = convert_string_talloc(req,
2701 CH_UNIX, CH_UTF16LE,
2702 config->model, strlen(config->model),
2705 return NT_STATUS_UNSUCCESSFUL;
2709 SIVAL(p + 4, 0, modellen);
2710 ok = data_blob_append(req, &blob, p, 8);
2713 return NT_STATUS_UNSUCCESSFUL;
2716 ok = data_blob_append(req, &blob, model, modellen);
2719 return NT_STATUS_UNSUCCESSFUL;
2723 status = smb2_create_blob_add(out_context_blobs,
2725 SMB2_CREATE_TAG_AAPL,
2727 if (NT_STATUS_IS_OK(status)) {
2728 global_fruit_config.nego_aapl = true;
2729 if (config->aapl_zero_file_id) {
2730 aapl_force_zero_file_id(handle->conn->sconn);
2737 static bool readdir_attr_meta_finderi_stream(
2738 struct vfs_handle_struct *handle,
2739 const struct smb_filename *smb_fname,
2742 struct smb_filename *stream_name = NULL;
2743 files_struct *fsp = NULL;
2748 uint8_t buf[AFP_INFO_SIZE];
2750 stream_name = synthetic_smb_fname(talloc_tos(),
2751 smb_fname->base_name,
2752 AFPINFO_STREAM_NAME,
2753 NULL, smb_fname->flags);
2754 if (stream_name == NULL) {
2758 ret = SMB_VFS_STAT(handle->conn, stream_name);
2763 status = SMB_VFS_CREATE_FILE(
2764 handle->conn, /* conn */
2766 0, /* root_dir_fid */
2767 stream_name, /* fname */
2768 FILE_READ_DATA, /* access_mask */
2769 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2771 FILE_OPEN, /* create_disposition*/
2772 0, /* create_options */
2773 0, /* file_attributes */
2774 INTERNAL_OPEN_ONLY, /* oplock_request */
2776 0, /* allocation_size */
2777 0, /* private_flags */
2782 NULL, NULL); /* create context */
2784 TALLOC_FREE(stream_name);
2786 if (!NT_STATUS_IS_OK(status)) {
2790 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2791 if (nread != AFP_INFO_SIZE) {
2792 DBG_ERR("short read [%s] [%zd/%d]\n",
2793 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2798 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2805 close_file(NULL, fsp, NORMAL_CLOSE);
2811 static bool readdir_attr_meta_finderi_netatalk(
2812 struct vfs_handle_struct *handle,
2813 const struct smb_filename *smb_fname,
2816 struct adouble *ad = NULL;
2819 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2824 p = ad_get_entry(ad, ADEID_FINDERI);
2826 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2831 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2836 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2837 const struct smb_filename *smb_fname,
2838 struct readdir_attr_data *attr_data)
2840 struct fruit_config_data *config = NULL;
2841 uint32_t date_added;
2845 SMB_VFS_HANDLE_GET_DATA(handle, config,
2846 struct fruit_config_data,
2849 switch (config->meta) {
2850 case FRUIT_META_NETATALK:
2851 ok = readdir_attr_meta_finderi_netatalk(
2852 handle, smb_fname, &ai);
2855 case FRUIT_META_STREAM:
2856 ok = readdir_attr_meta_finderi_stream(
2857 handle, smb_fname, &ai);
2861 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2866 /* Don't bother with errors, it's likely ENOENT */
2870 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2872 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2873 &ai.afpi_FinderInfo[0], 4);
2875 /* finder_creator */
2876 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2877 &ai.afpi_FinderInfo[4], 4);
2881 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2882 &ai.afpi_FinderInfo[8], 2);
2884 /* finder_ext_flags */
2885 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2886 &ai.afpi_FinderInfo[24], 2);
2889 date_added = convert_time_t_to_uint32_t(
2890 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2892 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2897 static uint64_t readdir_attr_rfork_size_adouble(
2898 struct vfs_handle_struct *handle,
2899 const struct smb_filename *smb_fname)
2901 struct adouble *ad = NULL;
2902 uint64_t rfork_size;
2904 ad = ad_get(talloc_tos(), handle, smb_fname,
2910 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2916 static uint64_t readdir_attr_rfork_size_stream(
2917 struct vfs_handle_struct *handle,
2918 const struct smb_filename *smb_fname)
2920 struct smb_filename *stream_name = NULL;
2922 uint64_t rfork_size;
2924 stream_name = synthetic_smb_fname(talloc_tos(),
2925 smb_fname->base_name,
2926 AFPRESOURCE_STREAM_NAME,
2928 if (stream_name == NULL) {
2932 ret = SMB_VFS_STAT(handle->conn, stream_name);
2934 TALLOC_FREE(stream_name);
2938 rfork_size = stream_name->st.st_ex_size;
2939 TALLOC_FREE(stream_name);
2944 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2945 const struct smb_filename *smb_fname)
2947 struct fruit_config_data *config = NULL;
2948 uint64_t rfork_size;
2950 SMB_VFS_HANDLE_GET_DATA(handle, config,
2951 struct fruit_config_data,
2954 switch (config->rsrc) {
2955 case FRUIT_RSRC_ADFILE:
2956 case FRUIT_RSRC_XATTR:
2957 rfork_size = readdir_attr_rfork_size_adouble(handle,
2961 case FRUIT_META_STREAM:
2962 rfork_size = readdir_attr_rfork_size_stream(handle,
2967 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2975 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2976 const struct smb_filename *smb_fname,
2977 struct readdir_attr_data *attr_data)
2979 NTSTATUS status = NT_STATUS_OK;
2980 struct fruit_config_data *config = NULL;
2983 SMB_VFS_HANDLE_GET_DATA(handle, config,
2984 struct fruit_config_data,
2985 return NT_STATUS_UNSUCCESSFUL);
2988 /* Ensure we return a default value in the creation_date field */
2989 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2992 * Resource fork length
2995 if (config->readdir_attr_rsize) {
2996 uint64_t rfork_size;
2998 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2999 attr_data->attr_data.aapl.rfork_size = rfork_size;
3006 if (config->readdir_attr_finder_info) {
3007 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
3009 status = NT_STATUS_INTERNAL_ERROR;
3016 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
3021 if (psd->dacl == NULL) {
3022 return NT_STATUS_OK;
3025 for (i = 0; i < psd->dacl->num_aces; i++) {
3026 /* MS NFS style mode/uid/gid */
3027 int cmp = dom_sid_compare_domain(
3028 &global_sid_Unix_NFS,
3029 &psd->dacl->aces[i].trustee);
3031 /* Normal ACE entry. */
3036 * security_descriptor_dacl_del()
3037 * *must* return NT_STATUS_OK as we know
3038 * we have something to remove.
3041 status = security_descriptor_dacl_del(psd,
3042 &psd->dacl->aces[i].trustee);
3043 if (!NT_STATUS_IS_OK(status)) {
3044 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3050 * security_descriptor_dacl_del() may delete more
3051 * then one entry subsequent to this one if the
3052 * SID matches, but we only need to ensure that
3053 * we stay looking at the same element in the array.
3057 return NT_STATUS_OK;
3060 /* Search MS NFS style ACE with UNIX mode */
3061 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3063 struct security_descriptor *psd,
3068 struct fruit_config_data *config = NULL;
3072 SMB_VFS_HANDLE_GET_DATA(handle, config,
3073 struct fruit_config_data,
3074 return NT_STATUS_UNSUCCESSFUL);
3076 if (!global_fruit_config.nego_aapl) {
3077 return NT_STATUS_OK;
3079 if (psd->dacl == NULL || !config->unix_info_enabled) {
3080 return NT_STATUS_OK;
3083 for (i = 0; i < psd->dacl->num_aces; i++) {
3084 if (dom_sid_compare_domain(
3085 &global_sid_Unix_NFS_Mode,
3086 &psd->dacl->aces[i].trustee) == 0) {
3087 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3088 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3091 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3092 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3098 * Remove any incoming virtual ACE entries generated by
3099 * fruit_fget_nt_acl().
3102 return remove_virtual_nfs_aces(psd);
3105 /****************************************************************************
3107 ****************************************************************************/
3109 static int fruit_connect(vfs_handle_struct *handle,
3110 const char *service,
3114 char *list = NULL, *newlist = NULL;
3115 struct fruit_config_data *config;
3117 DEBUG(10, ("fruit_connect\n"));
3119 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3124 rc = init_fruit_config(handle);
3129 SMB_VFS_HANDLE_GET_DATA(handle, config,
3130 struct fruit_config_data, return -1);
3132 if (config->veto_appledouble) {
3133 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3136 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3137 newlist = talloc_asprintf(
3139 "%s/" ADOUBLE_NAME_PREFIX "*/",
3141 lp_do_parameter(SNUM(handle->conn),
3146 lp_do_parameter(SNUM(handle->conn),
3148 "/" ADOUBLE_NAME_PREFIX "*/");
3154 if (config->encoding == FRUIT_ENC_NATIVE) {
3155 lp_do_parameter(SNUM(handle->conn),
3160 if (config->time_machine) {
3161 DBG_NOTICE("Enabling durable handles for Time Machine "
3162 "support on [%s]\n", service);
3163 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3164 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3165 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3166 if (!lp_strict_sync(SNUM(handle->conn))) {
3167 DBG_WARNING("Time Machine without strict sync is not "
3170 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3176 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3177 struct smb_filename *smb_fname,
3183 char afpinfo_buf[AFP_INFO_SIZE];
3184 ssize_t len, written;
3188 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3193 if (!(flags & (O_CREAT | O_TRUNC))) {
3197 ai = afpinfo_new(talloc_tos());
3203 len = afpinfo_pack(ai, afpinfo_buf);
3204 if (len != AFP_INFO_SIZE) {
3209 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3210 fsp->fh->fd = hostfd;
3212 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3215 if (written != AFP_INFO_SIZE) {
3216 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3224 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3227 int saved_errno = errno;
3229 fsp->fh->fd = hostfd;
3230 SMB_VFS_NEXT_CLOSE(handle, fsp);
3233 errno = saved_errno;
3238 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3239 struct smb_filename *smb_fname,
3246 struct adouble *ad = NULL;
3249 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3252 * Return a valid fd, but ensure any attempt to use it returns an error
3253 * (EPIPE). All operations on the smb_fname or the fsp will use path
3263 if (flags & (O_CREAT | O_TRUNC)) {
3265 * The attribute does not exist or needs to be truncated,
3266 * create an AppleDouble EA
3268 ad = ad_init(fsp, handle, ADOUBLE_META);
3274 rc = ad_set(ad, fsp->fsp_name);
3284 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3286 int saved_errno = errno;
3291 errno = saved_errno;
3296 static int fruit_open_meta(vfs_handle_struct *handle,
3297 struct smb_filename *smb_fname,
3298 files_struct *fsp, int flags, mode_t mode)
3301 struct fruit_config_data *config = NULL;
3302 struct fio *fio = NULL;
3304 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3306 SMB_VFS_HANDLE_GET_DATA(handle, config,
3307 struct fruit_config_data, return -1);
3309 switch (config->meta) {
3310 case FRUIT_META_STREAM:
3311 fd = fruit_open_meta_stream(handle, smb_fname,
3315 case FRUIT_META_NETATALK:
3316 fd = fruit_open_meta_netatalk(handle, smb_fname,
3321 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3325 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3331 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3332 fio->type = ADOUBLE_META;
3333 fio->config = config;
3338 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3339 struct smb_filename *smb_fname,
3345 struct adouble *ad = NULL;
3346 struct smb_filename *smb_fname_base = NULL;
3347 struct fruit_config_data *config = NULL;
3350 SMB_VFS_HANDLE_GET_DATA(handle, config,
3351 struct fruit_config_data, return -1);
3353 if ((!(flags & O_CREAT)) &&
3354 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3356 /* sorry, but directories don't habe a resource fork */
3361 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3366 /* Sanitize flags */
3367 if (flags & O_WRONLY) {
3368 /* We always need read access for the metadata header too */
3373 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3380 if (flags & (O_CREAT | O_TRUNC)) {
3381 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3387 fsp->fh->fd = hostfd;
3389 rc = ad_fset(ad, fsp);
3400 TALLOC_FREE(smb_fname_base);
3402 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3404 int saved_errno = errno;
3407 * BUGBUGBUG -- we would need to call
3408 * fd_close_posix here, but we don't have a
3411 fsp->fh->fd = hostfd;
3415 errno = saved_errno;
3420 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3421 struct smb_filename *smb_fname,
3426 #ifdef HAVE_ATTROPEN
3429 fd = attropen(smb_fname->base_name,
3430 AFPRESOURCE_EA_NETATALK,
3445 static int fruit_open_rsrc(vfs_handle_struct *handle,
3446 struct smb_filename *smb_fname,
3447 files_struct *fsp, int flags, mode_t mode)
3450 struct fruit_config_data *config = NULL;
3451 struct fio *fio = NULL;
3453 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3455 SMB_VFS_HANDLE_GET_DATA(handle, config,
3456 struct fruit_config_data, return -1);
3458 if (((flags & O_ACCMODE) == O_RDONLY)
3459 && (flags & O_CREAT)
3460 && !VALID_STAT(fsp->fsp_name->st))
3463 * This means the stream doesn't exist. macOS SMB server fails
3464 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3465 * 12565 and the test for this combination in
3466 * test_rfork_create().
3472 switch (config->rsrc) {
3473 case FRUIT_RSRC_STREAM:
3474 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3477 case FRUIT_RSRC_ADFILE:
3478 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3482 case FRUIT_RSRC_XATTR:
3483 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3488 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3492 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3498 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3499 fio->type = ADOUBLE_RSRC;
3500 fio->config = config;
3505 static int fruit_open(vfs_handle_struct *handle,
3506 struct smb_filename *smb_fname,
3507 files_struct *fsp, int flags, mode_t mode)
3511 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3513 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3514 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3517 if (is_afpinfo_stream(smb_fname)) {
3518 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3519 } else if (is_afpresource_stream(smb_fname)) {
3520 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3522 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3525 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3530 static int fruit_rename(struct vfs_handle_struct *handle,
3531 const struct smb_filename *smb_fname_src,
3532 const struct smb_filename *smb_fname_dst)
3535 struct fruit_config_data *config = NULL;
3536 struct smb_filename *src_adp_smb_fname = NULL;
3537 struct smb_filename *dst_adp_smb_fname = NULL;
3539 SMB_VFS_HANDLE_GET_DATA(handle, config,
3540 struct fruit_config_data, return -1);
3542 if (!VALID_STAT(smb_fname_src->st)) {
3543 DBG_ERR("Need valid stat for [%s]\n",
3544 smb_fname_str_dbg(smb_fname_src));
3548 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3553 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3554 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3559 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3564 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3569 DBG_DEBUG("%s -> %s\n",
3570 smb_fname_str_dbg(src_adp_smb_fname),
3571 smb_fname_str_dbg(dst_adp_smb_fname));
3573 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3574 if (errno == ENOENT) {
3579 TALLOC_FREE(src_adp_smb_fname);
3580 TALLOC_FREE(dst_adp_smb_fname);
3584 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3585 const struct smb_filename *smb_fname)
3587 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3590 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3591 const struct smb_filename *smb_fname)
3593 return SMB_VFS_REMOVEXATTR(handle->conn,
3595 AFPINFO_EA_NETATALK);
3598 static int fruit_unlink_meta(vfs_handle_struct *handle,
3599 const struct smb_filename *smb_fname)
3601 struct fruit_config_data *config = NULL;
3604 SMB_VFS_HANDLE_GET_DATA(handle, config,
3605 struct fruit_config_data, return -1);
3607 switch (config->meta) {
3608 case FRUIT_META_STREAM:
3609 rc = fruit_unlink_meta_stream(handle, smb_fname);
3612 case FRUIT_META_NETATALK:
3613 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3617 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3624 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3625 const struct smb_filename *smb_fname,
3630 if (!force_unlink) {
3631 struct smb_filename *smb_fname_cp = NULL;
3634 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3635 if (smb_fname_cp == NULL) {
3640 * 0 byte resource fork streams are not listed by
3641 * vfs_streaminfo, as a result stream cleanup/deletion of file
3642 * deletion doesn't remove the resourcefork stream.
3645 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3647 TALLOC_FREE(smb_fname_cp);
3648 DBG_ERR("stat [%s] failed [%s]\n",
3649 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3653 size = smb_fname_cp->st.st_ex_size;
3654 TALLOC_FREE(smb_fname_cp);
3657 /* OS X ignores resource fork stream delete requests */
3662 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3663 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3670 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3671 const struct smb_filename *smb_fname,
3675 struct adouble *ad = NULL;
3676 struct smb_filename *adp_smb_fname = NULL;
3678 if (!force_unlink) {
3679 ad = ad_get(talloc_tos(), handle, smb_fname,
3688 * 0 byte resource fork streams are not listed by
3689 * vfs_streaminfo, as a result stream cleanup/deletion of file
3690 * deletion doesn't remove the resourcefork stream.
3693 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3694 /* OS X ignores resource fork stream delete requests */
3702 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3707 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3708 TALLOC_FREE(adp_smb_fname);
3709 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3716 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3717 const struct smb_filename *smb_fname,
3721 * OS X ignores resource fork stream delete requests, so nothing to do
3722 * here. Removing the file will remove the xattr anyway, so we don't
3723 * have to take care of removing 0 byte resource forks that could be
3729 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3730 const struct smb_filename *smb_fname,
3733 struct fruit_config_data *config = NULL;
3736 SMB_VFS_HANDLE_GET_DATA(handle, config,
3737 struct fruit_config_data, return -1);
3739 switch (config->rsrc) {
3740 case FRUIT_RSRC_STREAM:
3741 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3744 case FRUIT_RSRC_ADFILE:
3745 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3748 case FRUIT_RSRC_XATTR:
3749 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3753 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3760 static int fruit_unlink(vfs_handle_struct *handle,
3761 const struct smb_filename *smb_fname)
3764 struct fruit_config_data *config = NULL;
3765 struct smb_filename *rsrc_smb_fname = NULL;
3767 SMB_VFS_HANDLE_GET_DATA(handle, config,
3768 struct fruit_config_data, return -1);
3770 if (is_afpinfo_stream(smb_fname)) {
3771 return fruit_unlink_meta(handle, smb_fname);
3772 } else if (is_afpresource_stream(smb_fname)) {
3773 return fruit_unlink_rsrc(handle, smb_fname, false);
3774 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3775 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3779 * A request to delete the base file. Because 0 byte resource
3780 * fork streams are not listed by fruit_streaminfo,
3781 * delete_all_streams() can't remove 0 byte resource fork
3782 * streams, so we have to cleanup this here.
3784 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3785 smb_fname->base_name,
3786 AFPRESOURCE_STREAM_NAME,
3789 if (rsrc_smb_fname == NULL) {
3793 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3794 if ((rc != 0) && (errno != ENOENT)) {
3795 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3796 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3797 TALLOC_FREE(rsrc_smb_fname);
3800 TALLOC_FREE(rsrc_smb_fname);
3802 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3805 static int fruit_chmod(vfs_handle_struct *handle,
3806 const struct smb_filename *smb_fname,
3810 struct fruit_config_data *config = NULL;
3811 struct smb_filename *smb_fname_adp = NULL;
3813 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3818 SMB_VFS_HANDLE_GET_DATA(handle, config,
3819 struct fruit_config_data, return -1);
3821 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3825 if (!VALID_STAT(smb_fname->st)) {
3829 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3833 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3838 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3840 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3841 if (errno == ENOENT) {
3845 TALLOC_FREE(smb_fname_adp);
3849 static int fruit_chown(vfs_handle_struct *handle,
3850 const struct smb_filename *smb_fname,
3855 struct fruit_config_data *config = NULL;
3856 struct smb_filename *adp_smb_fname = NULL;
3858 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3863 SMB_VFS_HANDLE_GET_DATA(handle, config,
3864 struct fruit_config_data, return -1);
3866 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3870 if (!VALID_STAT(smb_fname->st)) {
3874 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3878 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3883 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3885 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3886 if (errno == ENOENT) {
3891 TALLOC_FREE(adp_smb_fname);
3895 static int fruit_rmdir(struct vfs_handle_struct *handle,
3896 const struct smb_filename *smb_fname)
3900 struct fruit_config_data *config;
3902 SMB_VFS_HANDLE_GET_DATA(handle, config,
3903 struct fruit_config_data, return -1);
3905 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3910 * Due to there is no way to change bDeleteVetoFiles variable
3911 * from this module, need to clean up ourselves
3914 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3919 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3921 struct adouble *ad = NULL;
3923 struct smb_filename *ad_smb_fname = NULL;
3926 match = strncmp(de->d_name,
3927 ADOUBLE_NAME_PREFIX,
3928 strlen(ADOUBLE_NAME_PREFIX));
3933 p = talloc_asprintf(talloc_tos(), "%s/%s",
3934 smb_fname->base_name, de->d_name);
3936 DBG_ERR("talloc_asprintf failed\n");
3940 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3944 if (ad_smb_fname == NULL) {
3945 DBG_ERR("synthetic_smb_fname failed\n");
3950 * Check whether it's a valid AppleDouble file, if
3951 * yes, delete it, ignore it otherwise.
3953 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3955 TALLOC_FREE(ad_smb_fname);
3961 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3963 DBG_ERR("Deleting [%s] failed\n",
3964 smb_fname_str_dbg(ad_smb_fname));
3966 TALLOC_FREE(ad_smb_fname);
3971 SMB_VFS_CLOSEDIR(handle->conn, dh);
3973 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3976 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3977 files_struct *fsp, void *data,
3978 size_t n, off_t offset)
3983 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3989 DBG_ERR("Removing [%s] after short read [%zd]\n",
3990 fsp_str_dbg(fsp), nread);
3992 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3994 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
4002 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
4003 files_struct *fsp, void *data,
4004 size_t n, off_t offset)
4007 struct adouble *ad = NULL;
4008 char afpinfo_buf[AFP_INFO_SIZE];
4012 ai = afpinfo_new(talloc_tos());
4017 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4023 p = ad_get_entry(ad, ADEID_FINDERI);
4025 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4030 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
4032 nread = afpinfo_pack(ai, afpinfo_buf);
4033 if (nread != AFP_INFO_SIZE) {
4038 memcpy(data, afpinfo_buf, n);
4046 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
4047 files_struct *fsp, void *data,
4048 size_t n, off_t offset)
4050 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4055 * OS X has a off-by-1 error in the offset calculation, so we're
4056 * bug compatible here. It won't hurt, as any relevant real
4057 * world read requests from the AFP_AfpInfo stream will be
4058 * offset=0 n=60. offset is ignored anyway, see below.
4060 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4065 DBG_ERR("Failed to fetch fsp extension");
4069 /* Yes, macOS always reads from offset 0 */
4071 to_return = MIN(n, AFP_INFO_SIZE);
4073 switch (fio->config->meta) {
4074 case FRUIT_META_STREAM:
4075 nread = fruit_pread_meta_stream(handle, fsp, data,
4079 case FRUIT_META_NETATALK:
4080 nread = fruit_pread_meta_adouble(handle, fsp, data,
4085 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4092 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4093 files_struct *fsp, void *data,
4094 size_t n, off_t offset)
4096 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4099 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4100 files_struct *fsp, void *data,
4101 size_t n, off_t offset)
4103 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4106 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4107 files_struct *fsp, void *data,
4108 size_t n, off_t offset)
4110 struct adouble *ad = NULL;
4113 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4118 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4119 offset + ad_getentryoff(ad, ADEID_RFORK));
4125 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4126 files_struct *fsp, void *data,
4127 size_t n, off_t offset)
4129 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4137 switch (fio->config->rsrc) {
4138 case FRUIT_RSRC_STREAM:
4139 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4142 case FRUIT_RSRC_ADFILE:
4143 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4146 case FRUIT_RSRC_XATTR:
4147 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4151 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4158 static ssize_t fruit_pread(vfs_handle_struct *handle,
4159 files_struct *fsp, void *data,
4160 size_t n, off_t offset)
4162 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4165 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4166 fsp_str_dbg(fsp), (intmax_t)offset, n);
4169 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4172 if (fio->type == ADOUBLE_META) {
4173 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4175 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4178 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4182 static bool fruit_must_handle_aio_stream(struct fio *fio)
4188 if ((fio->type == ADOUBLE_META) &&
4189 (fio->config->meta == FRUIT_META_NETATALK))
4194 if ((fio->type == ADOUBLE_RSRC) &&
4195 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4203 struct fruit_pread_state {
4205 struct vfs_aio_state vfs_aio_state;
4208 static void fruit_pread_done(struct tevent_req *subreq);
4210 static struct tevent_req *fruit_pread_send(
4211 struct vfs_handle_struct *handle,
4212 TALLOC_CTX *mem_ctx,
4213 struct tevent_context *ev,
4214 struct files_struct *fsp,
4216 size_t n, off_t offset)
4218 struct tevent_req *req = NULL;
4219 struct tevent_req *subreq = NULL;
4220 struct fruit_pread_state *state = NULL;
4221 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4223 req = tevent_req_create(mem_ctx, &state,
4224 struct fruit_pread_state);
4229 if (fruit_must_handle_aio_stream(fio)) {
4230 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4231 if (state->nread != n) {
4232 if (state->nread != -1) {
4235 tevent_req_error(req, errno);
4236 return tevent_req_post(req, ev);
4238 tevent_req_done(req);
4239 return tevent_req_post(req, ev);
4242 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4244 if (tevent_req_nomem(req, subreq)) {
4245 return tevent_req_post(req, ev);
4247 tevent_req_set_callback(subreq, fruit_pread_done, req);
4251 static void fruit_pread_done(struct tevent_req *subreq)
4253 struct tevent_req *req = tevent_req_callback_data(
4254 subreq, struct tevent_req);
4255 struct fruit_pread_state *state = tevent_req_data(
4256 req, struct fruit_pread_state);
4258 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4259 TALLOC_FREE(subreq);
4261 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4264 tevent_req_done(req);
4267 static ssize_t fruit_pread_recv(struct tevent_req *req,
4268 struct vfs_aio_state *vfs_aio_state)
4270 struct fruit_pread_state *state = tevent_req_data(
4271 req, struct fruit_pread_state);
4273 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4277 *vfs_aio_state = state->vfs_aio_state;
4278 return state->nread;
4281 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4282 files_struct *fsp, const void *data,
4283 size_t n, off_t offset)
4289 ai = afpinfo_unpack(talloc_tos(), data);
4294 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4295 if (nwritten != n) {
4299 if (!ai_empty_finderinfo(ai)) {
4303 ok = set_delete_on_close(
4306 handle->conn->session_info->security_token,
4307 handle->conn->session_info->unix_token);
4309 DBG_ERR("set_delete_on_close on [%s] failed\n",
4317 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4318 files_struct *fsp, const void *data,
4319 size_t n, off_t offset)
4321 struct adouble *ad = NULL;
4327 ai = afpinfo_unpack(talloc_tos(), data);
4332 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4334 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4339 p = ad_get_entry(ad, ADEID_FINDERI);
4341 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4346 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4348 ret = ad_fset(ad, fsp);
4350 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4357 if (!ai_empty_finderinfo(ai)) {
4361 ok = set_delete_on_close(
4364 handle->conn->session_info->security_token,
4365 handle->conn->session_info->unix_token);
4367 DBG_ERR("set_delete_on_close on [%s] failed\n",
4375 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4376 files_struct *fsp, const void *data,
4377 size_t n, off_t offset)
4379 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4383 * Writing an all 0 blob to the metadata stream
4384 * results in the stream being removed on a macOS
4385 * server. This ensures we behave the same and it
4386 * verified by the "delete AFP_AfpInfo by writing all
4389 if (n != AFP_INFO_SIZE || offset != 0) {
4390 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4391 (intmax_t)offset, (intmax_t)n);
4396 DBG_ERR("Failed to fetch fsp extension");
4400 switch (fio->config->meta) {
4401 case FRUIT_META_STREAM:
4402 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4406 case FRUIT_META_NETATALK:
4407 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4412 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4419 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4420 files_struct *fsp, const void *data,
4421 size_t n, off_t offset)
4423 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4426 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4427 files_struct *fsp, const void *data,
4428 size_t n, off_t offset)
4430 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4433 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4434 files_struct *fsp, const void *data,
4435 size_t n, off_t offset)
4437 struct adouble *ad = NULL;
4441 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4443 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4447 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4448 offset + ad_getentryoff(ad, ADEID_RFORK));
4449 if (nwritten != n) {
4450 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4451 fsp_str_dbg(fsp), nwritten, n);
4456 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4457 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4458 ret = ad_fset(ad, fsp);
4460 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4470 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4471 files_struct *fsp, const void *data,
4472 size_t n, off_t offset)
4474 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4478 DBG_ERR("Failed to fetch fsp extension");
4482 switch (fio->config->rsrc) {
4483 case FRUIT_RSRC_STREAM:
4484 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4487 case FRUIT_RSRC_ADFILE:
4488 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4491 case FRUIT_RSRC_XATTR:
4492 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4496 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4503 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4504 files_struct *fsp, const void *data,
4505 size_t n, off_t offset)
4507 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4510 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4511 fsp_str_dbg(fsp), (intmax_t)offset, n);
4514 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4517 if (fio->type == ADOUBLE_META) {
4518 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4520 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4523 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4527 struct fruit_pwrite_state {
4529 struct vfs_aio_state vfs_aio_state;
4532 static void fruit_pwrite_done(struct tevent_req *subreq);
4534 static struct tevent_req *fruit_pwrite_send(
4535 struct vfs_handle_struct *handle,
4536 TALLOC_CTX *mem_ctx,
4537 struct tevent_context *ev,
4538 struct files_struct *fsp,
4540 size_t n, off_t offset)
4542 struct tevent_req *req = NULL;
4543 struct tevent_req *subreq = NULL;
4544 struct fruit_pwrite_state *state = NULL;
4545 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4547 req = tevent_req_create(mem_ctx, &state,
4548 struct fruit_pwrite_state);
4553 if (fruit_must_handle_aio_stream(fio)) {
4554 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4555 if (state->nwritten != n) {
4556 if (state->nwritten != -1) {
4559 tevent_req_error(req, errno);
4560 return tevent_req_post(req, ev);
4562 tevent_req_done(req);
4563 return tevent_req_post(req, ev);
4566 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4568 if (tevent_req_nomem(req, subreq)) {
4569 return tevent_req_post(req, ev);
4571 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4575 static void fruit_pwrite_done(struct tevent_req *subreq)
4577 struct tevent_req *req = tevent_req_callback_data(
4578 subreq, struct tevent_req);
4579 struct fruit_pwrite_state *state = tevent_req_data(
4580 req, struct fruit_pwrite_state);
4582 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4583 TALLOC_FREE(subreq);
4585 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4588 tevent_req_done(req);
4591 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4592 struct vfs_aio_state *vfs_aio_state)
4594 struct fruit_pwrite_state *state = tevent_req_data(
4595 req, struct fruit_pwrite_state);
4597 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4601 *vfs_aio_state = state->vfs_aio_state;
4602 return state->nwritten;
4606 * Helper to stat/lstat the base file of an smb_fname.
4608 static int fruit_stat_base(vfs_handle_struct *handle,
4609 struct smb_filename *smb_fname,
4612 char *tmp_stream_name;
4615 tmp_stream_name = smb_fname->stream_name;
4616 smb_fname->stream_name = NULL;
4618 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4620 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4622 smb_fname->stream_name = tmp_stream_name;
4626 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4627 struct smb_filename *smb_fname,
4633 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4635 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4641 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4642 struct smb_filename *smb_fname,
4645 struct adouble *ad = NULL;
4647 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4649 DBG_INFO("fruit_stat_meta %s: %s\n",
4650 smb_fname_str_dbg(smb_fname), strerror(errno));
4656 /* Populate the stat struct with info from the base file. */
4657 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4660 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4661 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4662 smb_fname->stream_name);
4666 static int fruit_stat_meta(vfs_handle_struct *handle,
4667 struct smb_filename *smb_fname,
4670 struct fruit_config_data *config = NULL;
4673 SMB_VFS_HANDLE_GET_DATA(handle, config,
4674 struct fruit_config_data, return -1);
4676 switch (config->meta) {
4677 case FRUIT_META_STREAM:
4678 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4681 case FRUIT_META_NETATALK:
4682 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4686 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4693 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4694 struct smb_filename *smb_fname,
4697 struct adouble *ad = NULL;
4700 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4706 /* Populate the stat struct with info from the base file. */
4707 ret = fruit_stat_base(handle, smb_fname, follow_links);
4713 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4714 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4715 smb_fname->stream_name);
4720 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4721 struct smb_filename *smb_fname,
4727 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4729 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4735 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4736 struct smb_filename *smb_fname,
4739 #ifdef HAVE_ATTROPEN
4743 /* Populate the stat struct with info from the base file. */
4744 ret = fruit_stat_base(handle, smb_fname, follow_links);
4749 fd = attropen(smb_fname->base_name,
4750 AFPRESOURCE_EA_NETATALK,
4756 ret = sys_fstat(fd, &smb_fname->st, false);
4759 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4760 AFPRESOURCE_EA_NETATALK);
4766 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4767 smb_fname->stream_name);
4777 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4778 struct smb_filename *smb_fname,
4781 struct fruit_config_data *config = NULL;
4784 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4786 SMB_VFS_HANDLE_GET_DATA(handle, config,
4787 struct fruit_config_data, return -1);
4789 switch (config->rsrc) {
4790 case FRUIT_RSRC_STREAM:
4791 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4794 case FRUIT_RSRC_XATTR:
4795 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4798 case FRUIT_RSRC_ADFILE:
4799 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4803 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4810 static int fruit_stat(vfs_handle_struct *handle,
4811 struct smb_filename *smb_fname)
4815 DEBUG(10, ("fruit_stat called for %s\n",
4816 smb_fname_str_dbg(smb_fname)));
4818 if (!is_ntfs_stream_smb_fname(smb_fname)
4819 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4820 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4822 update_btime(handle, smb_fname);
4828 * Note if lp_posix_paths() is true, we can never
4829 * get here as is_ntfs_stream_smb_fname() is
4830 * always false. So we never need worry about
4831 * not following links here.
4834 if (is_afpinfo_stream(smb_fname)) {
4835 rc = fruit_stat_meta(handle, smb_fname, true);
4836 } else if (is_afpresource_stream(smb_fname)) {
4837 rc = fruit_stat_rsrc(handle, smb_fname, true);
4839 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4843 update_btime(handle, smb_fname);
4844 smb_fname->st.st_ex_mode &= ~S_IFMT;
4845 smb_fname->st.st_ex_mode |= S_IFREG;
4846 smb_fname->st.st_ex_blocks =
4847 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4852 static int fruit_lstat(vfs_handle_struct *handle,
4853 struct smb_filename *smb_fname)
4857 DEBUG(10, ("fruit_lstat called for %s\n",
4858 smb_fname_str_dbg(smb_fname)));
4860 if (!is_ntfs_stream_smb_fname(smb_fname)
4861 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4862 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4864 update_btime(handle, smb_fname);
4869 if (is_afpinfo_stream(smb_fname)) {
4870 rc = fruit_stat_meta(handle, smb_fname, false);
4871 } else if (is_afpresource_stream(smb_fname)) {
4872 rc = fruit_stat_rsrc(handle, smb_fname, false);
4874 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4878 update_btime(handle, smb_fname);
4879 smb_fname->st.st_ex_mode &= ~S_IFMT;
4880 smb_fname->st.st_ex_mode |= S_IFREG;
4881 smb_fname->st.st_ex_blocks =
4882 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4887 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4889 SMB_STRUCT_STAT *sbuf)
4891 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4894 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4896 SMB_STRUCT_STAT *sbuf)
4900 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4905 *sbuf = fsp->base_fsp->fsp_name->st;
4906 sbuf->st_ex_size = AFP_INFO_SIZE;
4907 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4912 static int fruit_fstat_meta(vfs_handle_struct *handle,
4914 SMB_STRUCT_STAT *sbuf,
4919 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4921 switch (fio->config->meta) {
4922 case FRUIT_META_STREAM:
4923 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4926 case FRUIT_META_NETATALK:
4927 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4931 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4935 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4939 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4941 SMB_STRUCT_STAT *sbuf)
4943 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4946 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4948 SMB_STRUCT_STAT *sbuf)
4950 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4953 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4955 SMB_STRUCT_STAT *sbuf)
4957 struct adouble *ad = NULL;
4960 /* Populate the stat struct with info from the base file. */
4961 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4966 ad = ad_get(talloc_tos(), handle,
4967 fsp->base_fsp->fsp_name,
4970 DBG_ERR("ad_get [%s] failed [%s]\n",
4971 fsp_str_dbg(fsp), strerror(errno));
4975 *sbuf = fsp->base_fsp->fsp_name->st;
4976 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4977 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4983 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4984 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4988 switch (fio->config->rsrc) {
4989 case FRUIT_RSRC_STREAM:
4990 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4993 case FRUIT_RSRC_ADFILE:
4994 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4997 case FRUIT_RSRC_XATTR:
4998 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
5002 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5009 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
5010 SMB_STRUCT_STAT *sbuf)
5012 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5016 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5019 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
5021 if (fio->type == ADOUBLE_META) {
5022 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
5024 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
5028 sbuf->st_ex_mode &= ~S_IFMT;
5029 sbuf->st_ex_mode |= S_IFREG;
5030 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
5033 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
5034 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
5038 static NTSTATUS delete_invalid_meta_stream(
5039 vfs_handle_struct *handle,
5040 const struct smb_filename *smb_fname,
5041 TALLOC_CTX *mem_ctx,
5042 unsigned int *pnum_streams,
5043 struct stream_struct **pstreams)
5045 struct smb_filename *sname = NULL;
5049 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
5051 return NT_STATUS_INTERNAL_ERROR;
5054 sname = synthetic_smb_fname(talloc_tos(),
5055 smb_fname->base_name,
5056 AFPINFO_STREAM_NAME,
5058 if (sname == NULL) {
5059 return NT_STATUS_NO_MEMORY;
5062 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5065 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5066 return map_nt_error_from_unix(errno);
5069 return NT_STATUS_OK;
5072 static NTSTATUS fruit_streaminfo_meta_stream(
5073 vfs_handle_struct *handle,
5074 struct files_struct *fsp,
5075 const struct smb_filename *smb_fname,
5076 TALLOC_CTX *mem_ctx,
5077 unsigned int *pnum_streams,
5078 struct stream_struct **pstreams)
5080 struct stream_struct *stream = *pstreams;
5081 unsigned int num_streams = *pnum_streams;
5082 struct smb_filename *sname = NULL;
5083 char *full_name = NULL;
5085 struct share_mode_lock *lck = NULL;
5086 struct file_id id = {0};
5087 bool delete_on_close_set;
5093 for (i = 0; i < num_streams; i++) {
5094 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5099 if (i == num_streams) {
5100 return NT_STATUS_OK;
5103 if (stream[i].size != AFP_INFO_SIZE) {
5104 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5105 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5107 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5108 pnum_streams, pstreams);
5112 * Now check if there's a delete-on-close pending on the stream. If so,
5113 * hide the stream. This behaviour was verified against a macOS 10.12
5117 sname = synthetic_smb_fname(talloc_tos(),
5118 smb_fname->base_name,
5119 AFPINFO_STREAM_NAME,
5121 if (sname == NULL) {
5122 status = NT_STATUS_NO_MEMORY;
5126 ret = SMB_VFS_NEXT_STAT(handle, sname);
5128 status = map_nt_error_from_unix(errno);
5132 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5134 lck = get_existing_share_mode_lock(talloc_tos(), id);
5136 status = NT_STATUS_OK;
5140 full_name = talloc_asprintf(talloc_tos(),
5144 if (full_name == NULL) {
5145 status = NT_STATUS_NO_MEMORY;
5149 status = file_name_hash(handle->conn, full_name, &name_hash);
5150 if (!NT_STATUS_IS_OK(status)) {
5154 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5155 if (delete_on_close_set) {
5156 ok = del_fruit_stream(mem_ctx,
5161 status = NT_STATUS_INTERNAL_ERROR;
5166 status = NT_STATUS_OK;
5171 TALLOC_FREE(full_name);
5175 static NTSTATUS fruit_streaminfo_meta_netatalk(
5176 vfs_handle_struct *handle,
5177 struct files_struct *fsp,
5178 const struct smb_filename *smb_fname,
5179 TALLOC_CTX *mem_ctx,
5180 unsigned int *pnum_streams,
5181 struct stream_struct **pstreams)
5183 struct stream_struct *stream = *pstreams;
5184 unsigned int num_streams = *pnum_streams;
5185 struct adouble *ad = NULL;
5190 /* Remove the Netatalk xattr from the list */
5191 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5192 ":" NETATALK_META_XATTR ":$DATA");
5194 return NT_STATUS_NO_MEMORY;
5198 * Check if there's a AFPINFO_STREAM from the VFS streams
5199 * backend and if yes, remove it from the list
5201 for (i = 0; i < num_streams; i++) {
5202 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5207 if (i < num_streams) {
5208 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5209 smb_fname_str_dbg(smb_fname));
5211 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5214 return NT_STATUS_INTERNAL_ERROR;
5218 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5220 return NT_STATUS_OK;
5223 is_fi_empty = ad_empty_finderinfo(ad);
5227 return NT_STATUS_OK;
5230 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5231 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5232 smb_roundup(handle->conn, AFP_INFO_SIZE));
5234 return NT_STATUS_NO_MEMORY;
5237 return NT_STATUS_OK;
5240 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5241 struct files_struct *fsp,
5242 const struct smb_filename *smb_fname,
5243 TALLOC_CTX *mem_ctx,
5244 unsigned int *pnum_streams,
5245 struct stream_struct **pstreams)
5247 struct fruit_config_data *config = NULL;
5250 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5251 return NT_STATUS_INTERNAL_ERROR);
5253 switch (config->meta) {
5254 case FRUIT_META_NETATALK:
5255 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5256 mem_ctx, pnum_streams,
5260 case FRUIT_META_STREAM:
5261 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5262 mem_ctx, pnum_streams,
5267 return NT_STATUS_INTERNAL_ERROR;
5273 static NTSTATUS fruit_streaminfo_rsrc_stream(
5274 vfs_handle_struct *handle,
5275 struct files_struct *fsp,
5276 const struct smb_filename *smb_fname,
5277 TALLOC_CTX *mem_ctx,
5278 unsigned int *pnum_streams,
5279 struct stream_struct **pstreams)
5283 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5285 DBG_ERR("Filtering resource stream failed\n");
5286 return NT_STATUS_INTERNAL_ERROR;
5288 return NT_STATUS_OK;
5291 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5292 vfs_handle_struct *handle,
5293 struct files_struct *fsp,
5294 const struct smb_filename *smb_fname,
5295 TALLOC_CTX *mem_ctx,
5296 unsigned int *pnum_streams,
5297 struct stream_struct **pstreams)
5301 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5303 DBG_ERR("Filtering resource stream failed\n");
5304 return NT_STATUS_INTERNAL_ERROR;
5306 return NT_STATUS_OK;
5309 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5310 vfs_handle_struct *handle,
5311 struct files_struct *fsp,
5312 const struct smb_filename *smb_fname,
5313 TALLOC_CTX *mem_ctx,
5314 unsigned int *pnum_streams,
5315 struct stream_struct **pstreams)
5317 struct stream_struct *stream = *pstreams;
5318 unsigned int num_streams = *pnum_streams;
5319 struct adouble *ad = NULL;
5325 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5326 * and if yes, remove it from the list
5328 for (i = 0; i < num_streams; i++) {
5329 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5334 if (i < num_streams) {
5335 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5336 smb_fname_str_dbg(smb_fname));
5338 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5339 AFPRESOURCE_STREAM);
5341 return NT_STATUS_INTERNAL_ERROR;
5345 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5347 return NT_STATUS_OK;
5350 rlen = ad_getentrylen(ad, ADEID_RFORK);
5354 return NT_STATUS_OK;
5357 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5358 AFPRESOURCE_STREAM_NAME, rlen,
5359 smb_roundup(handle->conn, rlen));
5361 return NT_STATUS_NO_MEMORY;
5364 return NT_STATUS_OK;
5367 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5368 struct files_struct *fsp,
5369 const struct smb_filename *smb_fname,
5370 TALLOC_CTX *mem_ctx,
5371 unsigned int *pnum_streams,
5372 struct stream_struct **pstreams)
5374 struct fruit_config_data *config = NULL;
5377 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5378 return NT_STATUS_INTERNAL_ERROR);
5380 switch (config->rsrc) {
5381 case FRUIT_RSRC_STREAM:
5382 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5383 mem_ctx, pnum_streams,
5387 case FRUIT_RSRC_XATTR:
5388 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5389 mem_ctx, pnum_streams,
5393 case FRUIT_RSRC_ADFILE:
5394 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5395 mem_ctx, pnum_streams,
5400 return NT_STATUS_INTERNAL_ERROR;
5406 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5407 struct files_struct *fsp,
5408 const struct smb_filename *smb_fname,
5409 TALLOC_CTX *mem_ctx,
5410 unsigned int *pnum_streams,
5411 struct stream_struct **pstreams)
5413 struct fruit_config_data *config = NULL;
5416 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5417 return NT_STATUS_UNSUCCESSFUL);
5419 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5421 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5422 pnum_streams, pstreams);
5423 if (!NT_STATUS_IS_OK(status)) {
5427 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5428 mem_ctx, pnum_streams, pstreams);
5429 if (!NT_STATUS_IS_OK(status)) {
5433 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5434 mem_ctx, pnum_streams, pstreams);
5435 if (!NT_STATUS_IS_OK(status)) {
5439 return NT_STATUS_OK;
5442 static int fruit_ntimes(vfs_handle_struct *handle,
5443 const struct smb_filename *smb_fname,
5444 struct smb_file_time *ft)
5447 struct adouble *ad = NULL;
5448 struct fruit_config_data *config = NULL;
5450 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5453 if ((config->meta != FRUIT_META_NETATALK) ||
5454 null_timespec(ft->create_time))
5456 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5459 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5460 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5462 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5467 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5468 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5470 rc = ad_set(ad, smb_fname);
5476 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5479 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5482 static int fruit_fallocate(struct vfs_handle_struct *handle,
5483 struct files_struct *fsp,
5488 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5491 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5494 /* Let the pwrite code path handle it. */
5499 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5500 struct files_struct *fsp,
5504 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5507 #ifdef HAVE_ATTROPEN
5508 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5513 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5514 struct files_struct *fsp,
5518 struct adouble *ad = NULL;
5521 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5523 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5524 fsp_str_dbg(fsp), strerror(errno));
5528 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5530 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5536 ad_setentrylen(ad, ADEID_RFORK, offset);
5538 rc = ad_fset(ad, fsp);
5540 DBG_ERR("ad_fset [%s] failed [%s]\n",
5541 fsp_str_dbg(fsp), strerror(errno));
5550 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5551 struct files_struct *fsp,
5555 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5558 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5561 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5562 struct files_struct *fsp,
5565 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5569 DBG_ERR("Failed to fetch fsp extension");
5573 switch (fio->config->rsrc) {
5574 case FRUIT_RSRC_XATTR:
5575 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5578 case FRUIT_RSRC_ADFILE:
5579 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5582 case FRUIT_RSRC_STREAM:
5583 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5587 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5595 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5596 struct files_struct *fsp,
5600 DBG_WARNING("ftruncate %s to %jd",
5601 fsp_str_dbg(fsp), (intmax_t)offset);
5602 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5607 /* OS X returns success but does nothing */
5608 DBG_INFO("ignoring ftruncate %s to %jd\n",
5609 fsp_str_dbg(fsp), (intmax_t)offset);
5613 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5614 struct files_struct *fsp,
5617 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5620 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5625 global_fruit_config.nego_aapl &&
5626 is_ntfs_stream_smb_fname(fsp->fsp_name) &&
5627 !is_ntfs_default_stream_smb_fname(fsp->fsp_name))
5629 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5631 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5634 if (fio->type == ADOUBLE_META) {
5635 ret = fruit_ftruncate_meta(handle, fsp, offset);
5637 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5640 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5644 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5645 struct smb_request *req,
5646 uint16_t root_dir_fid,
5647 struct smb_filename *smb_fname,
5648 uint32_t access_mask,
5649 uint32_t share_access,
5650 uint32_t create_disposition,
5651 uint32_t create_options,
5652 uint32_t file_attributes,
5653 uint32_t oplock_request,
5654 struct smb2_lease *lease,
5655 uint64_t allocation_size,
5656 uint32_t private_flags,
5657 struct security_descriptor *sd,
5658 struct ea_list *ea_list,
5659 files_struct **result,
5661 const struct smb2_create_blobs *in_context_blobs,
5662 struct smb2_create_blobs *out_context_blobs)
5665 struct fruit_config_data *config = NULL;
5666 files_struct *fsp = NULL;
5668 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5669 if (!NT_STATUS_IS_OK(status)) {
5673 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5674 return NT_STATUS_UNSUCCESSFUL);
5676 status = SMB_VFS_NEXT_CREATE_FILE(
5677 handle, req, root_dir_fid, smb_fname,
5678 access_mask, share_access,
5679 create_disposition, create_options,
5680 file_attributes, oplock_request,
5682 allocation_size, private_flags,
5683 sd, ea_list, result,
5684 pinfo, in_context_blobs, out_context_blobs);
5685 if (!NT_STATUS_IS_OK(status)) {
5691 if (global_fruit_config.nego_aapl) {
5692 if (config->posix_rename && fsp->is_directory) {
5694 * Enable POSIX directory rename behaviour
5696 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5701 * If this is a plain open for existing files, opening an 0
5702 * byte size resource fork MUST fail with
5703 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5705 * Cf the vfs_fruit torture tests in test_rfork_create().
5707 if (is_afpresource_stream(fsp->fsp_name) &&
5708 create_disposition == FILE_OPEN)
5710 if (fsp->fsp_name->st.st_ex_size == 0) {
5711 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5716 if (is_ntfs_stream_smb_fname(smb_fname)
5717 || fsp->is_directory) {
5721 if (config->locking == FRUIT_LOCKING_NETATALK) {
5722 status = fruit_check_access(
5725 map_share_mode_to_deny_mode(share_access, 0));
5726 if (!NT_STATUS_IS_OK(status)) {
5734 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5737 close_file(req, fsp, ERROR_CLOSE);
5738 *result = fsp = NULL;
5744 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5745 const struct smb_filename *fname,
5746 TALLOC_CTX *mem_ctx,
5747 struct readdir_attr_data **pattr_data)
5749 struct fruit_config_data *config = NULL;
5750 struct readdir_attr_data *attr_data;
5753 SMB_VFS_HANDLE_GET_DATA(handle, config,
5754 struct fruit_config_data,
5755 return NT_STATUS_UNSUCCESSFUL);
5757 if (!global_fruit_config.nego_aapl) {
5758 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5761 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5763 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5764 if (*pattr_data == NULL) {
5765 return NT_STATUS_UNSUCCESSFUL;
5767 attr_data = *pattr_data;
5768 attr_data->type = RDATTR_AAPL;
5771 * Mac metadata: compressed FinderInfo, resource fork length
5774 status = readdir_attr_macmeta(handle, fname, attr_data);
5775 if (!NT_STATUS_IS_OK(status)) {
5777 * Error handling is tricky: if we return failure from
5778 * this function, the corresponding directory entry
5779 * will to be passed to the client, so we really just
5780 * want to error out on fatal errors.
5782 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5790 if (config->unix_info_enabled) {
5791 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5797 if (!config->readdir_attr_max_access) {
5798 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5800 status = smbd_calculate_access_mask(
5804 SEC_FLAG_MAXIMUM_ALLOWED,
5805 &attr_data->attr_data.aapl.max_access);
5806 if (!NT_STATUS_IS_OK(status)) {
5811 return NT_STATUS_OK;
5814 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5815 fname->base_name, nt_errstr(status)));
5816 TALLOC_FREE(*pattr_data);
5820 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5822 uint32_t security_info,
5823 TALLOC_CTX *mem_ctx,
5824 struct security_descriptor **ppdesc)
5827 struct security_ace ace;
5829 struct fruit_config_data *config;
5831 SMB_VFS_HANDLE_GET_DATA(handle, config,
5832 struct fruit_config_data,
5833 return NT_STATUS_UNSUCCESSFUL);
5835 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5837 if (!NT_STATUS_IS_OK(status)) {
5842 * Add MS NFS style ACEs with uid, gid and mode
5844 if (!global_fruit_config.nego_aapl) {
5845 return NT_STATUS_OK;
5847 if (!config->unix_info_enabled) {
5848 return NT_STATUS_OK;
5851 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5852 status = remove_virtual_nfs_aces(*ppdesc);
5853 if (!NT_STATUS_IS_OK(status)) {
5854 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5858 /* MS NFS style mode */
5859 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5860 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5861 status = security_descriptor_dacl_add(*ppdesc, &ace);
5862 if (!NT_STATUS_IS_OK(status)) {
5863 DEBUG(1,("failed to add MS NFS style ACE\n"));
5867 /* MS NFS style uid */
5868 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5869 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5870 status = security_descriptor_dacl_add(*ppdesc, &ace);
5871 if (!NT_STATUS_IS_OK(status)) {
5872 DEBUG(1,("failed to add MS NFS style ACE\n"));
5876 /* MS NFS style gid */
5877 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5878 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5879 status = security_descriptor_dacl_add(*ppdesc, &ace);
5880 if (!NT_STATUS_IS_OK(status)) {
5881 DEBUG(1,("failed to add MS NFS style ACE\n"));
5885 return NT_STATUS_OK;
5888 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5890 uint32_t security_info_sent,
5891 const struct security_descriptor *orig_psd)
5895 mode_t ms_nfs_mode = 0;
5897 struct security_descriptor *psd = NULL;
5898 uint32_t orig_num_aces = 0;
5900 if (orig_psd->dacl != NULL) {
5901 orig_num_aces = orig_psd->dacl->num_aces;
5904 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5906 return NT_STATUS_NO_MEMORY;
5909 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5911 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5912 if (!NT_STATUS_IS_OK(status)) {
5913 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5919 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5920 * sent/present flags correctly now we've removed them.
5923 if (orig_num_aces != 0) {
5925 * Are there any ACE's left ?
5927 if (psd->dacl->num_aces == 0) {
5928 /* No - clear the DACL sent/present flags. */
5929 security_info_sent &= ~SECINFO_DACL;
5930 psd->type &= ~SEC_DESC_DACL_PRESENT;
5934 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5935 if (!NT_STATUS_IS_OK(status)) {
5936 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5942 if (fsp->fh->fd != -1) {
5943 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5945 result = SMB_VFS_CHMOD(fsp->conn,
5951 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5952 result, (unsigned)ms_nfs_mode,
5954 status = map_nt_error_from_unix(errno);
5961 return NT_STATUS_OK;
5964 static struct vfs_offload_ctx *fruit_offload_ctx;
5966 struct fruit_offload_read_state {
5967 struct vfs_handle_struct *handle;
5968 struct tevent_context *ev;
5974 static void fruit_offload_read_done(struct tevent_req *subreq);
5976 static struct tevent_req *fruit_offload_read_send(
5977 TALLOC_CTX *mem_ctx,
5978 struct tevent_context *ev,
5979 struct vfs_handle_struct *handle,
5986 struct tevent_req *req = NULL;
5987 struct tevent_req *subreq = NULL;
5988 struct fruit_offload_read_state *state = NULL;
5990 req = tevent_req_create(mem_ctx, &state,
5991 struct fruit_offload_read_state);
5995 *state = (struct fruit_offload_read_state) {
6002 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
6003 fsctl, ttl, offset, to_copy);
6004 if (tevent_req_nomem(subreq, req)) {
6005 return tevent_req_post(req, ev);
6007 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
6011 static void fruit_offload_read_done(struct tevent_req *subreq)
6013 struct tevent_req *req = tevent_req_callback_data(
6014 subreq, struct tevent_req);
6015 struct fruit_offload_read_state *state = tevent_req_data(
6016 req, struct fruit_offload_read_state);
6019 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
6023 TALLOC_FREE(subreq);
6024 if (tevent_req_nterror(req, status)) {
6028 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
6029 tevent_req_done(req);
6033 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
6034 &fruit_offload_ctx);
6035 if (tevent_req_nterror(req, status)) {
6039 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
6042 if (tevent_req_nterror(req, status)) {
6046 tevent_req_done(req);
6050 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6051 struct vfs_handle_struct *handle,
6052 TALLOC_CTX *mem_ctx,
6055 struct fruit_offload_read_state *state = tevent_req_data(
6056 req, struct fruit_offload_read_state);
6059 if (tevent_req_is_nterror(req, &status)) {
6060 tevent_req_received(req);
6064 token->length = state->token.length;
6065 token->data = talloc_move(mem_ctx, &state->token.data);
6067 tevent_req_received(req);
6068 return NT_STATUS_OK;
6071 struct fruit_offload_write_state {
6072 struct vfs_handle_struct *handle;
6074 struct files_struct *src_fsp;
6075 struct files_struct *dst_fsp;
6079 static void fruit_offload_write_done(struct tevent_req *subreq);
6080 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6081 TALLOC_CTX *mem_ctx,
6082 struct tevent_context *ev,
6085 off_t transfer_offset,
6086 struct files_struct *dest_fsp,
6090 struct tevent_req *req, *subreq;
6091 struct fruit_offload_write_state *state;
6093 struct fruit_config_data *config;
6094 off_t src_off = transfer_offset;
6095 files_struct *src_fsp = NULL;
6096 off_t to_copy = num;
6097 bool copyfile_enabled = false;
6099 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6100 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6102 SMB_VFS_HANDLE_GET_DATA(handle, config,
6103 struct fruit_config_data,
6106 req = tevent_req_create(mem_ctx, &state,
6107 struct fruit_offload_write_state);
6111 state->handle = handle;
6112 state->dst_fsp = dest_fsp;
6115 case FSCTL_SRV_COPYCHUNK:
6116 case FSCTL_SRV_COPYCHUNK_WRITE:
6117 copyfile_enabled = config->copyfile_enabled;
6124 * Check if this a OS X copyfile style copychunk request with
6125 * a requested chunk count of 0 that was translated to a
6126 * offload_write_send VFS call overloading the parameters src_off
6127 * = dest_off = num = 0.
6129 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6130 status = vfs_offload_token_db_fetch_fsp(
6131 fruit_offload_ctx, token, &src_fsp);
6132 if (tevent_req_nterror(req, status)) {
6133 return tevent_req_post(req, ev);
6135 state->src_fsp = src_fsp;
6137 status = vfs_stat_fsp(src_fsp);
6138 if (tevent_req_nterror(req, status)) {
6139 return tevent_req_post(req, ev);
6142 to_copy = src_fsp->fsp_name->st.st_ex_size;
6143 state->is_copyfile = true;
6146 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6155 if (tevent_req_nomem(subreq, req)) {
6156 return tevent_req_post(req, ev);
6159 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6163 static void fruit_offload_write_done(struct tevent_req *subreq)
6165 struct tevent_req *req = tevent_req_callback_data(
6166 subreq, struct tevent_req);
6167 struct fruit_offload_write_state *state = tevent_req_data(
6168 req, struct fruit_offload_write_state);
6170 unsigned int num_streams = 0;
6171 struct stream_struct *streams = NULL;
6173 struct smb_filename *src_fname_tmp = NULL;
6174 struct smb_filename *dst_fname_tmp = NULL;
6176 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6179 TALLOC_FREE(subreq);
6180 if (tevent_req_nterror(req, status)) {
6184 if (!state->is_copyfile) {
6185 tevent_req_done(req);
6190 * Now copy all remaining streams. We know the share supports
6191 * streams, because we're in vfs_fruit. We don't do this async
6192 * because streams are few and small.
6194 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6195 state->src_fsp->fsp_name,
6196 req, &num_streams, &streams);
6197 if (tevent_req_nterror(req, status)) {
6201 if (num_streams == 1) {
6202 /* There is always one stream, ::$DATA. */
6203 tevent_req_done(req);
6207 for (i = 0; i < num_streams; i++) {
6208 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6209 __func__, streams[i].name, (size_t)streams[i].size));
6211 src_fname_tmp = synthetic_smb_fname(
6213 state->src_fsp->fsp_name->base_name,
6216 state->src_fsp->fsp_name->flags);
6217 if (tevent_req_nomem(src_fname_tmp, req)) {
6221 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6222 TALLOC_FREE(src_fname_tmp);
6226 dst_fname_tmp = synthetic_smb_fname(
6228 state->dst_fsp->fsp_name->base_name,
6231 state->dst_fsp->fsp_name->flags);
6232 if (tevent_req_nomem(dst_fname_tmp, req)) {
6233 TALLOC_FREE(src_fname_tmp);
6237 status = copy_file(req,
6238 state->handle->conn,
6241 OPENX_FILE_CREATE_IF_NOT_EXIST,
6243 if (!NT_STATUS_IS_OK(status)) {
6244 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6245 smb_fname_str_dbg(src_fname_tmp),
6246 smb_fname_str_dbg(dst_fname_tmp),
6247 nt_errstr(status)));
6248 TALLOC_FREE(src_fname_tmp);
6249 TALLOC_FREE(dst_fname_tmp);
6250 tevent_req_nterror(req, status);
6254 TALLOC_FREE(src_fname_tmp);
6255 TALLOC_FREE(dst_fname_tmp);
6258 TALLOC_FREE(streams);
6259 TALLOC_FREE(src_fname_tmp);
6260 TALLOC_FREE(dst_fname_tmp);
6261 tevent_req_done(req);
6264 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6265 struct tevent_req *req,
6268 struct fruit_offload_write_state *state = tevent_req_data(
6269 req, struct fruit_offload_write_state);
6272 if (tevent_req_is_nterror(req, &status)) {
6273 DEBUG(1, ("server side copy chunk failed: %s\n",
6274 nt_errstr(status)));
6276 tevent_req_received(req);
6280 *copied = state->copied;
6281 tevent_req_received(req);
6283 return NT_STATUS_OK;
6286 static char *fruit_get_bandsize_line(char **lines, int numlines)
6289 static bool re_initialized = false;
6293 if (!re_initialized) {
6294 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6298 re_initialized = true;
6301 for (i = 0; i < numlines; i++) {
6302 regmatch_t matches[1];
6304 ret = regexec(&re, lines[i], 1, matches, 0);
6307 * Check if the match was on the last line, sa we want
6308 * the subsequent line.
6310 if (i + 1 == numlines) {
6313 return lines[i + 1];
6315 if (ret != REG_NOMATCH) {
6323 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6326 static bool re_initialized = false;
6327 regmatch_t matches[2];
6332 if (!re_initialized) {
6335 "<integer>\\([[:digit:]]*\\)</integer>$",
6340 re_initialized = true;
6343 ret = regexec(&re, line, 2, matches, 0);
6345 DBG_ERR("regex failed [%s]\n", line);
6349 line[matches[1].rm_eo] = '\0';
6351 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6355 *_band_size = (size_t)band_size;
6360 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6361 * "band-size" key and value.
6363 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6367 #define INFO_PLIST_MAX_SIZE 64*1024
6369 struct smb_filename *smb_fname = NULL;
6370 files_struct *fsp = NULL;
6371 uint8_t *file_data = NULL;
6372 char **lines = NULL;
6373 char *band_size_line = NULL;
6374 size_t plist_file_size;
6381 plist = talloc_asprintf(talloc_tos(),
6383 handle->conn->connectpath,
6385 if (plist == NULL) {
6390 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6391 if (smb_fname == NULL) {
6396 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6398 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6403 plist_file_size = smb_fname->st.st_ex_size;
6405 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6406 DBG_INFO("%s is too large, ignoring\n", plist);
6411 status = SMB_VFS_NEXT_CREATE_FILE(
6414 0, /* root_dir_fid */
6415 smb_fname, /* fname */
6416 FILE_GENERIC_READ, /* access_mask */
6417 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6418 FILE_OPEN, /* create_disposition */
6419 0, /* create_options */
6420 0, /* file_attributes */
6421 INTERNAL_OPEN_ONLY, /* oplock_request */
6423 0, /* allocation_size */
6424 0, /* private_flags */
6429 NULL, NULL); /* create context */
6430 if (!NT_STATUS_IS_OK(status)) {
6431 DBG_INFO("Opening [%s] failed [%s]\n",
6432 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6437 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6438 if (file_data == NULL) {
6443 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6444 if (nread != plist_file_size) {
6445 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6446 fsp_str_dbg(fsp), nread, plist_file_size);
6452 status = close_file(NULL, fsp, NORMAL_CLOSE);
6454 if (!NT_STATUS_IS_OK(status)) {
6455 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6460 lines = file_lines_parse((char *)file_data,
6464 if (lines == NULL) {
6469 band_size_line = fruit_get_bandsize_line(lines, numlines);
6470 if (band_size_line == NULL) {
6471 DBG_ERR("Didn't find band-size key in [%s]\n",
6472 smb_fname_str_dbg(smb_fname));
6477 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6479 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6483 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6487 status = close_file(NULL, fsp, NORMAL_CLOSE);
6488 if (!NT_STATUS_IS_OK(status)) {
6489 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6494 TALLOC_FREE(smb_fname);
6495 TALLOC_FREE(file_data);
6500 struct fruit_disk_free_state {
6504 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6509 struct smb_filename *bands_dir = NULL;
6511 struct dirent *e = NULL;
6515 path = talloc_asprintf(talloc_tos(),
6517 handle->conn->connectpath,
6523 bands_dir = synthetic_smb_fname(talloc_tos(),
6529 if (bands_dir == NULL) {
6533 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6535 TALLOC_FREE(bands_dir);
6541 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6543 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6545 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6551 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6553 TALLOC_FREE(bands_dir);
6557 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6559 TALLOC_FREE(bands_dir);
6565 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6566 struct fruit_disk_free_state *state,
6571 size_t sparsebundle_strlen = strlen("sparsebundle");
6572 size_t bandsize = 0;
6576 p = strstr(e->d_name, "sparsebundle");
6581 if (p[sparsebundle_strlen] != '\0') {
6585 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6587 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6590 * Beware of race conditions: this may be an uninitialized
6591 * Info.plist that a client is just creating. We don't want let
6592 * this to trigger complete failure.
6594 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6598 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6601 * Beware of race conditions: this may be a backup sparsebundle
6602 * in an early stage lacking a bands subdirectory. We don't want
6603 * let this to trigger complete failure.
6605 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6609 if (bandsize > SIZE_MAX/nbands) {
6610 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6614 tm_size = bandsize * nbands;
6616 if (state->total_size + tm_size < state->total_size) {
6617 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6622 state->total_size += tm_size;
6624 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6625 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6631 * Calculate used size of a TimeMachine volume
6633 * This assumes that the volume is used only for TimeMachine.
6635 * - readdir(basedir of share), then
6636 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6637 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6638 * - count band files in "\1.sparsebundle/bands/"
6639 * - calculate used size of all bands: band_count * band_size
6641 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6642 const struct smb_filename *smb_fname,
6647 struct fruit_config_data *config = NULL;
6648 struct fruit_disk_free_state state = {0};
6650 struct dirent *e = NULL;
6656 SMB_VFS_HANDLE_GET_DATA(handle, config,
6657 struct fruit_config_data,
6660 if (!config->time_machine ||
6661 config->time_machine_max_size == 0)
6663 return SMB_VFS_NEXT_DISK_FREE(handle,
6670 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6675 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6677 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6679 ok = fruit_tmsize_do_dirent(handle, &state, e);
6681 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6686 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6691 dsize = config->time_machine_max_size / 512;
6692 dfree = dsize - (state.total_size / 512);
6693 if (dfree > dsize) {
6703 static struct vfs_fn_pointers vfs_fruit_fns = {
6704 .connect_fn = fruit_connect,
6705 .disk_free_fn = fruit_disk_free,
6707 /* File operations */
6708 .chmod_fn = fruit_chmod,
6709 .chown_fn = fruit_chown,
6710 .unlink_fn = fruit_unlink,
6711 .rename_fn = fruit_rename,
6712 .rmdir_fn = fruit_rmdir,
6713 .open_fn = fruit_open,
6714 .pread_fn = fruit_pread,
6715 .pwrite_fn = fruit_pwrite,
6716 .pread_send_fn = fruit_pread_send,
6717 .pread_recv_fn = fruit_pread_recv,
6718 .pwrite_send_fn = fruit_pwrite_send,
6719 .pwrite_recv_fn = fruit_pwrite_recv,
6720 .stat_fn = fruit_stat,
6721 .lstat_fn = fruit_lstat,
6722 .fstat_fn = fruit_fstat,
6723 .streaminfo_fn = fruit_streaminfo,
6724 .ntimes_fn = fruit_ntimes,
6725 .ftruncate_fn = fruit_ftruncate,
6726 .fallocate_fn = fruit_fallocate,
6727 .create_file_fn = fruit_create_file,
6728 .readdir_attr_fn = fruit_readdir_attr,
6729 .offload_read_send_fn = fruit_offload_read_send,
6730 .offload_read_recv_fn = fruit_offload_read_recv,
6731 .offload_write_send_fn = fruit_offload_write_send,
6732 .offload_write_recv_fn = fruit_offload_write_recv,
6734 /* NT ACL operations */
6735 .fget_nt_acl_fn = fruit_fget_nt_acl,
6736 .fset_nt_acl_fn = fruit_fset_nt_acl,
6740 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6742 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6744 if (!NT_STATUS_IS_OK(ret)) {
6748 vfs_fruit_debug_level = debug_add_class("fruit");
6749 if (vfs_fruit_debug_level == -1) {
6750 vfs_fruit_debug_level = DBGC_VFS;
6751 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6754 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6755 "vfs_fruit_init","fruit",vfs_fruit_debug_level));