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)) {
1156 * Convert from Apple's ._ file to Netatalk
1158 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1159 * bytes containing packed xattrs.
1161 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1164 static int ad_convert(struct adouble *ad,
1165 const struct smb_filename *smb_fname)
1168 char *map = MAP_FAILED;
1173 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1174 ad_getentrylen(ad, ADEID_RFORK);
1176 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1177 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED,
1179 if (map == MAP_FAILED) {
1180 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1184 ok = ad_convert_xattr(ad, smb_fname, map);
1186 munmap(map, origlen);
1190 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1191 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1192 map + ad_getentryoff(ad, ADEID_RFORK),
1193 ad_getentrylen(ad, ADEID_RFORK));
1196 ad_setentryoff(ad, ADEID_RFORK,
1197 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1200 * FIXME: direct ftruncate(), but we don't have a fsp for the
1203 rc = ftruncate(ad->ad_fd, ad_getentryoff(ad, ADEID_RFORK)
1204 + ad_getentrylen(ad, ADEID_RFORK));
1206 munmap(map, origlen);
1210 rc = munmap(map, origlen);
1212 DBG_ERR("munmap failed: %s\n", strerror(errno));
1218 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1222 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1223 if (len != AD_DATASZ_DOT_UND) {
1224 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1228 ok = ad_convert_finderinfo(ad, smb_fname);
1230 DBG_ERR("Failed to convert [%s]\n",
1231 smb_fname_str_dbg(smb_fname));
1239 * Read and parse Netatalk AppleDouble metadata xattr
1241 static ssize_t ad_read_meta(struct adouble *ad,
1242 const struct smb_filename *smb_fname)
1248 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1250 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1251 AFPINFO_EA_NETATALK, ad->ad_data,
1257 if (errno == ENOATTR) {
1263 DEBUG(2, ("error reading meta xattr: %s\n",
1269 if (ealen != AD_DATASZ_XATTR) {
1270 DEBUG(2, ("bad size %zd\n", ealen));
1276 /* Now parse entries */
1277 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1279 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1285 if (!ad_getentryoff(ad, ADEID_FINDERI)
1286 || !ad_getentryoff(ad, ADEID_COMMENT)
1287 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1288 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1289 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1290 || !ad_getentryoff(ad, ADEID_PRIVINO)
1291 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1292 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1293 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1300 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1301 smb_fname->base_name, rc));
1305 if (errno == EINVAL) {
1307 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1315 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1319 #ifdef HAVE_ATTROPEN
1320 /* FIXME: direct Solaris xattr syscall */
1321 return attropen(smb_fname->base_name,
1322 AFPRESOURCE_EA_NETATALK, flags, mode);
1329 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1335 struct smb_filename *adp_smb_fname = NULL;
1337 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1342 fd = open(adp_smb_fname->base_name, flags, mode);
1343 TALLOC_FREE(adp_smb_fname);
1348 static int ad_open_rsrc(vfs_handle_struct *handle,
1349 const struct smb_filename *smb_fname,
1353 struct fruit_config_data *config = NULL;
1356 SMB_VFS_HANDLE_GET_DATA(handle, config,
1357 struct fruit_config_data, return -1);
1359 if (config->rsrc == FRUIT_RSRC_XATTR) {
1360 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1362 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1369 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1370 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1371 * for file IO on the ._ file.
1373 static int ad_open(vfs_handle_struct *handle,
1376 const struct smb_filename *smb_fname,
1382 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1383 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1385 if (ad->ad_type == ADOUBLE_META) {
1389 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1390 ad->ad_fd = fsp->fh->fd;
1391 ad->ad_opened = false;
1395 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1399 ad->ad_opened = true;
1402 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1403 smb_fname->base_name,
1404 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1409 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1414 /* FIXME: direct sys_fstat(), don't have an fsp */
1415 ret = sys_fstat(ad->ad_fd, &st,
1416 lp_fake_directory_create_times(
1417 SNUM(ad->ad_handle->conn)));
1422 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1423 return st.st_ex_size;
1426 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1427 const struct smb_filename *smb_fname)
1429 SMB_STRUCT_STAT sbuf;
1436 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1437 SNUM(ad->ad_handle->conn)));
1443 * AppleDouble file header content and size, two cases:
1445 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1446 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1448 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1450 size = sbuf.st_ex_size;
1451 if (size > talloc_array_length(ad->ad_data)) {
1452 if (size > AD_XATTR_MAX_HDR_SIZE) {
1453 size = AD_XATTR_MAX_HDR_SIZE;
1455 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1462 len = sys_pread(ad->ad_fd, ad->ad_data,
1463 talloc_array_length(ad->ad_data), 0);
1464 if (len != talloc_array_length(ad->ad_data)) {
1465 DBG_NOTICE("%s %s: bad size: %zd\n",
1466 smb_fname->base_name, strerror(errno), len);
1470 /* Now parse entries */
1471 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1473 DBG_ERR("invalid AppleDouble resource %s\n",
1474 smb_fname->base_name);
1479 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1480 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1481 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1482 DBG_ERR("invalid AppleDouble resource %s\n",
1483 smb_fname->base_name);
1488 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1493 * Try to fixup AppleDouble files created by OS X with xattrs
1494 * appended to the ADEID_FINDERI entry.
1497 ret = ad_convert(ad, smb_fname);
1499 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1507 * Read and parse resource fork, either ._ AppleDouble file or xattr
1509 static ssize_t ad_read_rsrc(struct adouble *ad,
1510 const struct smb_filename *smb_fname)
1512 struct fruit_config_data *config = NULL;
1515 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1516 struct fruit_config_data, return -1);
1518 if (config->rsrc == FRUIT_RSRC_XATTR) {
1519 len = ad_read_rsrc_xattr(ad);
1521 len = ad_read_rsrc_adouble(ad, smb_fname);
1528 * Read and unpack an AppleDouble metadata xattr or resource
1530 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1532 switch (ad->ad_type) {
1534 return ad_read_meta(ad, smb_fname);
1536 return ad_read_rsrc(ad, smb_fname);
1542 static int adouble_destructor(struct adouble *ad)
1544 if ((ad->ad_fd != -1) && ad->ad_opened) {
1552 * Allocate a struct adouble without initialiing it
1554 * The struct is either hang of the fsp extension context or if fsp is
1557 * @param[in] ctx talloc context
1558 * @param[in] handle vfs handle
1559 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1561 * @return adouble handle
1563 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1564 adouble_type_t type)
1569 struct fruit_config_data *config;
1571 SMB_VFS_HANDLE_GET_DATA(handle, config,
1572 struct fruit_config_data, return NULL);
1576 adsize = AD_DATASZ_XATTR;
1579 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1580 adsize = AD_DATASZ_DOT_UND;
1587 ad = talloc_zero(ctx, struct adouble);
1594 ad->ad_data = talloc_zero_array(ad, char, adsize);
1595 if (ad->ad_data == NULL) {
1601 ad->ad_handle = handle;
1603 ad->ad_magic = AD_MAGIC;
1604 ad->ad_version = AD_VERSION;
1607 talloc_set_destructor(ad, adouble_destructor);
1617 * Allocate and initialize a new struct adouble
1619 * @param[in] ctx talloc context
1620 * @param[in] handle vfs handle
1621 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1623 * @return adouble handle, initialized
1625 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1626 adouble_type_t type)
1629 const struct ad_entry_order *eid;
1630 struct adouble *ad = NULL;
1631 struct fruit_config_data *config;
1632 time_t t = time(NULL);
1634 SMB_VFS_HANDLE_GET_DATA(handle, config,
1635 struct fruit_config_data, return NULL);
1639 eid = entry_order_meta_xattr;
1642 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1643 eid = entry_order_dot_und;
1645 eid = entry_order_rsrc_xattr;
1652 ad = ad_alloc(ctx, handle, type);
1658 ad->ad_eid[eid->id].ade_off = eid->offset;
1659 ad->ad_eid[eid->id].ade_len = eid->len;
1663 /* put something sane in the date fields */
1664 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1665 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1666 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1667 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1675 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1676 vfs_handle_struct *handle,
1678 const struct smb_filename *smb_fname,
1679 adouble_type_t type)
1683 struct adouble *ad = NULL;
1687 smb_fname = fsp->base_fsp->fsp_name;
1690 DEBUG(10, ("ad_get(%s) called for %s\n",
1691 type == ADOUBLE_META ? "meta" : "rsrc",
1692 smb_fname->base_name));
1694 ad = ad_alloc(ctx, handle, type);
1700 /* Try rw first so we can use the fd in ad_convert() */
1703 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1704 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1706 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1709 DBG_DEBUG("ad_open [%s] error [%s]\n",
1710 smb_fname->base_name, strerror(errno));
1715 len = ad_read(ad, smb_fname);
1717 DEBUG(10, ("error reading AppleDouble for %s\n",
1718 smb_fname->base_name));
1724 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1725 type == ADOUBLE_META ? "meta" : "rsrc",
1726 smb_fname->base_name, rc));
1735 * Return AppleDouble data for a file
1737 * @param[in] ctx talloc context
1738 * @param[in] handle vfs handle
1739 * @param[in] smb_fname pathname to file or directory
1740 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1742 * @return talloced struct adouble or NULL on error
1744 static struct adouble *ad_get(TALLOC_CTX *ctx,
1745 vfs_handle_struct *handle,
1746 const struct smb_filename *smb_fname,
1747 adouble_type_t type)
1749 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1753 * Return AppleDouble data for a file
1755 * @param[in] ctx talloc context
1756 * @param[in] handle vfs handle
1757 * @param[in] fsp fsp to use for IO
1758 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1760 * @return talloced struct adouble or NULL on error
1762 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1763 files_struct *fsp, adouble_type_t type)
1765 return ad_get_internal(ctx, handle, fsp, NULL, type);
1769 * Set AppleDouble metadata on a file or directory
1771 * @param[in] ad adouble handle
1773 * @param[in] smb_fname pathname to file or directory
1775 * @return status code, 0 means success
1777 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1782 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1784 if (ad->ad_type != ADOUBLE_META) {
1785 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1786 smb_fname->base_name);
1795 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1797 AFPINFO_EA_NETATALK,
1799 AD_DATASZ_XATTR, 0);
1801 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1807 * Set AppleDouble metadata on a file or directory
1809 * @param[in] ad adouble handle
1810 * @param[in] fsp file handle
1812 * @return status code, 0 means success
1814 static int ad_fset(struct adouble *ad, files_struct *fsp)
1820 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1823 || (fsp->fh == NULL)
1824 || (fsp->fh->fd == -1))
1826 smb_panic("bad fsp");
1834 switch (ad->ad_type) {
1836 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1838 AFPINFO_EA_NETATALK,
1840 AD_DATASZ_XATTR, 0);
1844 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1849 if (len != AD_DATASZ_DOT_UND) {
1850 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1860 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1865 /*****************************************************************************
1867 *****************************************************************************/
1869 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1871 if (strncasecmp_m(smb_fname->stream_name,
1872 AFPINFO_STREAM_NAME,
1873 strlen(AFPINFO_STREAM_NAME)) == 0) {
1879 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1881 if (strncasecmp_m(smb_fname->stream_name,
1882 AFPRESOURCE_STREAM_NAME,
1883 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1890 * Test whether stream is an Apple stream, not used atm
1893 static bool is_apple_stream(const struct smb_filename *smb_fname)
1895 if (is_afpinfo_stream(smb_fname)) {
1898 if (is_afpresource_stream(smb_fname)) {
1906 * Initialize config struct from our smb.conf config parameters
1908 static int init_fruit_config(vfs_handle_struct *handle)
1910 struct fruit_config_data *config;
1912 const char *tm_size_str = NULL;
1914 config = talloc_zero(handle->conn, struct fruit_config_data);
1916 DEBUG(1, ("talloc_zero() failed\n"));
1922 * Versions up to Samba 4.5.x had a spelling bug in the
1923 * fruit:resource option calling lp_parm_enum with
1924 * "res*s*ource" (ie two s).
1926 * In Samba 4.6 we accept both the wrong and the correct
1927 * spelling, in Samba 4.7 the bad spelling will be removed.
1929 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1930 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1931 if (enumval == -1) {
1932 DEBUG(1, ("value for %s: resource type unknown\n",
1933 FRUIT_PARAM_TYPE_NAME));
1936 config->rsrc = (enum fruit_rsrc)enumval;
1938 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1939 "resource", fruit_rsrc, enumval);
1940 if (enumval == -1) {
1941 DEBUG(1, ("value for %s: resource type unknown\n",
1942 FRUIT_PARAM_TYPE_NAME));
1945 config->rsrc = (enum fruit_rsrc)enumval;
1947 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1948 "metadata", fruit_meta, FRUIT_META_NETATALK);
1949 if (enumval == -1) {
1950 DEBUG(1, ("value for %s: metadata type unknown\n",
1951 FRUIT_PARAM_TYPE_NAME));
1954 config->meta = (enum fruit_meta)enumval;
1956 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1957 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1958 if (enumval == -1) {
1959 DEBUG(1, ("value for %s: locking type unknown\n",
1960 FRUIT_PARAM_TYPE_NAME));
1963 config->locking = (enum fruit_locking)enumval;
1965 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1966 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1967 if (enumval == -1) {
1968 DEBUG(1, ("value for %s: encoding type unknown\n",
1969 FRUIT_PARAM_TYPE_NAME));
1972 config->encoding = (enum fruit_encoding)enumval;
1974 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1975 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1976 FRUIT_PARAM_TYPE_NAME,
1981 config->use_aapl = lp_parm_bool(
1982 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1984 config->time_machine = lp_parm_bool(
1985 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
1987 config->unix_info_enabled = lp_parm_bool(
1988 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1990 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1993 config->posix_rename = lp_parm_bool(
1994 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1996 config->aapl_zero_file_id =
1997 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1999 config->readdir_attr_rsize = lp_parm_bool(
2000 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
2002 config->readdir_attr_finder_info = lp_parm_bool(
2003 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
2005 config->readdir_attr_max_access = lp_parm_bool(
2006 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
2008 config->model = lp_parm_const_string(
2009 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
2011 tm_size_str = lp_parm_const_string(
2012 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2013 "time machine max size", NULL);
2014 if (tm_size_str != NULL) {
2015 config->time_machine_max_size = conv_str_size(tm_size_str);
2018 SMB_VFS_HANDLE_SET_DATA(handle, config,
2019 NULL, struct fruit_config_data,
2026 * Prepend "._" to a basename
2027 * Return a new struct smb_filename with stream_name == NULL.
2029 static int adouble_path(TALLOC_CTX *ctx,
2030 const struct smb_filename *smb_fname_in,
2031 struct smb_filename **pp_smb_fname_out)
2035 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2038 if (smb_fname == NULL) {
2042 /* We need streamname to be NULL */
2043 TALLOC_FREE(smb_fname->stream_name);
2045 /* And we're replacing base_name. */
2046 TALLOC_FREE(smb_fname->base_name);
2048 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2050 TALLOC_FREE(smb_fname);
2054 smb_fname->base_name = talloc_asprintf(smb_fname,
2055 "%s/._%s", parent, base);
2056 if (smb_fname->base_name == NULL) {
2057 TALLOC_FREE(smb_fname);
2061 *pp_smb_fname_out = smb_fname;
2067 * Allocate and initialize an AfpInfo struct
2069 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2071 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2075 ai->afpi_Signature = AFP_Signature;
2076 ai->afpi_Version = AFP_Version;
2077 ai->afpi_BackupTime = AD_DATE_START;
2082 * Pack an AfpInfo struct into a buffer
2084 * Buffer size must be at least AFP_INFO_SIZE
2085 * Returns size of packed buffer
2087 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2089 memset(buf, 0, AFP_INFO_SIZE);
2091 RSIVAL(buf, 0, ai->afpi_Signature);
2092 RSIVAL(buf, 4, ai->afpi_Version);
2093 RSIVAL(buf, 12, ai->afpi_BackupTime);
2094 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2096 return AFP_INFO_SIZE;
2100 * Unpack a buffer into a AfpInfo structure
2102 * Buffer size must be at least AFP_INFO_SIZE
2103 * Returns allocated AfpInfo struct
2105 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2107 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2112 ai->afpi_Signature = RIVAL(data, 0);
2113 ai->afpi_Version = RIVAL(data, 4);
2114 ai->afpi_BackupTime = RIVAL(data, 12);
2115 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2116 sizeof(ai->afpi_FinderInfo));
2118 if (ai->afpi_Signature != AFP_Signature
2119 || ai->afpi_Version != AFP_Version) {
2120 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2128 * Fake an inode number from the md5 hash of the (xattr) name
2130 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2133 unsigned char hash[16];
2137 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2138 SMB_ASSERT(upper_sname != NULL);
2141 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2142 sizeof(sbuf->st_ex_dev));
2143 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2144 sizeof(sbuf->st_ex_ino));
2145 MD5Update(&ctx, (unsigned char *)upper_sname,
2146 talloc_get_size(upper_sname)-1);
2147 MD5Final(hash, &ctx);
2149 TALLOC_FREE(upper_sname);
2151 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2152 memcpy(&result, hash, sizeof(result));
2154 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2155 sname, (unsigned long long)result));
2160 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2161 struct stream_struct **streams,
2162 const char *name, off_t size,
2165 struct stream_struct *tmp;
2167 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2173 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2174 if (tmp[*num_streams].name == NULL) {
2178 tmp[*num_streams].size = size;
2179 tmp[*num_streams].alloc_size = alloc_size;
2186 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2187 struct stream_struct **streams)
2189 struct stream_struct *tmp = *streams;
2192 if (*num_streams == 0) {
2196 for (i = 0; i < *num_streams; i++) {
2197 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2202 if (i == *num_streams) {
2206 if (tmp[i].size > 0) {
2210 TALLOC_FREE(tmp[i].name);
2211 if (*num_streams - 1 > i) {
2212 memmove(&tmp[i], &tmp[i+1],
2213 (*num_streams - i - 1) * sizeof(struct stream_struct));
2220 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2221 struct stream_struct **streams,
2224 struct stream_struct *tmp = *streams;
2227 if (*num_streams == 0) {
2231 for (i = 0; i < *num_streams; i++) {
2232 if (strequal_m(tmp[i].name, name)) {
2237 if (i == *num_streams) {
2241 TALLOC_FREE(tmp[i].name);
2242 if (*num_streams - 1 > i) {
2243 memmove(&tmp[i], &tmp[i+1],
2244 (*num_streams - i - 1) * sizeof(struct stream_struct));
2251 static bool ad_empty_finderinfo(const struct adouble *ad)
2254 char emptybuf[ADEDLEN_FINDERI] = {0};
2257 fi = ad_get_entry(ad, ADEID_FINDERI);
2259 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2263 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2267 static bool ai_empty_finderinfo(const AfpInfo *ai)
2270 char emptybuf[ADEDLEN_FINDERI] = {0};
2272 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2277 * Update btime with btime from Netatalk
2279 static void update_btime(vfs_handle_struct *handle,
2280 struct smb_filename *smb_fname)
2283 struct timespec creation_time = {0};
2285 struct fruit_config_data *config = NULL;
2287 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2290 switch (config->meta) {
2291 case FRUIT_META_STREAM:
2293 case FRUIT_META_NETATALK:
2297 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2301 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2305 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2311 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2312 update_stat_ex_create_time(&smb_fname->st, creation_time);
2318 * Map an access mask to a Netatalk single byte byte range lock
2320 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2321 uint32_t access_mask)
2325 switch (access_mask) {
2326 case FILE_READ_DATA:
2327 offset = AD_FILELOCK_OPEN_RD;
2330 case FILE_WRITE_DATA:
2331 case FILE_APPEND_DATA:
2332 offset = AD_FILELOCK_OPEN_WR;
2336 offset = AD_FILELOCK_OPEN_NONE;
2340 if (fork_type == APPLE_FORK_RSRC) {
2341 if (offset == AD_FILELOCK_OPEN_NONE) {
2342 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2352 * Map a deny mode to a Netatalk brl
2354 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2359 switch (deny_mode) {
2361 offset = AD_FILELOCK_DENY_RD;
2365 offset = AD_FILELOCK_DENY_WR;
2369 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2372 if (fork_type == APPLE_FORK_RSRC) {
2380 * Call fcntl() with an exclusive F_GETLK request in order to
2381 * determine if there's an exisiting shared lock
2383 * @return true if the requested lock was found or any error occurred
2384 * false if the lock was not found
2386 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2389 off_t offset = in_offset;
2394 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2395 if (result == false) {
2399 if (type != F_UNLCK) {
2406 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2408 uint32_t access_mask,
2411 NTSTATUS status = NT_STATUS_OK;
2412 bool open_for_reading, open_for_writing, deny_read, deny_write;
2414 bool have_read = false;
2417 /* FIXME: hardcoded data fork, add resource fork */
2418 enum apple_fork fork_type = APPLE_FORK_DATA;
2420 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2422 access_mask & FILE_READ_DATA ? "READ" :"-",
2423 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2424 deny_mode & DENY_READ ? "DENY_READ" : "-",
2425 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2427 if (fsp->fh->fd == -1) {
2428 return NT_STATUS_OK;
2431 flags = fcntl(fsp->fh->fd, F_GETFL);
2433 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2434 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2435 return map_nt_error_from_unix(errno);
2438 if (flags & (O_RDONLY|O_RDWR)) {
2440 * Applying fcntl read locks requires an fd opened for
2441 * reading. This means we won't be applying locks for
2442 * files openend write-only, but what can we do...
2448 * Check read access and deny read mode
2450 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2452 open_for_reading = test_netatalk_lock(
2453 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2455 deny_read = test_netatalk_lock(
2456 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2458 DEBUG(10, ("read: %s, deny_write: %s\n",
2459 open_for_reading == true ? "yes" : "no",
2460 deny_read == true ? "yes" : "no"));
2462 if (((access_mask & FILE_READ_DATA) && deny_read)
2463 || ((deny_mode & DENY_READ) && open_for_reading)) {
2464 return NT_STATUS_SHARING_VIOLATION;
2468 if ((access_mask & FILE_READ_DATA) && have_read) {
2469 struct byte_range_lock *br_lck = NULL;
2471 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2473 handle->conn->sconn->msg_ctx, fsp,
2474 fsp->op->global->open_persistent_id, 1, off,
2475 READ_LOCK, POSIX_LOCK, false,
2478 TALLOC_FREE(br_lck);
2480 if (!NT_STATUS_IS_OK(status)) {
2485 if ((deny_mode & DENY_READ) && have_read) {
2486 struct byte_range_lock *br_lck = NULL;
2488 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2490 handle->conn->sconn->msg_ctx, fsp,
2491 fsp->op->global->open_persistent_id, 1, off,
2492 READ_LOCK, POSIX_LOCK, false,
2495 TALLOC_FREE(br_lck);
2497 if (!NT_STATUS_IS_OK(status)) {
2504 * Check write access and deny write mode
2506 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2508 open_for_writing = test_netatalk_lock(
2509 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2511 deny_write = test_netatalk_lock(
2512 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2514 DEBUG(10, ("write: %s, deny_write: %s\n",
2515 open_for_writing == true ? "yes" : "no",
2516 deny_write == true ? "yes" : "no"));
2518 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2519 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2520 return NT_STATUS_SHARING_VIOLATION;
2524 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2525 struct byte_range_lock *br_lck = NULL;
2527 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2529 handle->conn->sconn->msg_ctx, fsp,
2530 fsp->op->global->open_persistent_id, 1, off,
2531 READ_LOCK, POSIX_LOCK, false,
2534 TALLOC_FREE(br_lck);
2536 if (!NT_STATUS_IS_OK(status)) {
2540 if ((deny_mode & DENY_WRITE) && have_read) {
2541 struct byte_range_lock *br_lck = NULL;
2543 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2545 handle->conn->sconn->msg_ctx, fsp,
2546 fsp->op->global->open_persistent_id, 1, off,
2547 READ_LOCK, POSIX_LOCK, false,
2550 TALLOC_FREE(br_lck);
2552 if (!NT_STATUS_IS_OK(status)) {
2561 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2562 struct smb_request *req,
2563 const struct smb2_create_blobs *in_context_blobs,
2564 struct smb2_create_blobs *out_context_blobs)
2566 struct fruit_config_data *config;
2568 struct smb2_create_blob *aapl = NULL;
2572 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2573 uint64_t req_bitmap, client_caps;
2574 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2578 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2579 return NT_STATUS_UNSUCCESSFUL);
2581 if (!config->use_aapl
2582 || in_context_blobs == NULL
2583 || out_context_blobs == NULL) {
2584 return NT_STATUS_OK;
2587 aapl = smb2_create_blob_find(in_context_blobs,
2588 SMB2_CREATE_TAG_AAPL);
2590 return NT_STATUS_OK;
2593 if (aapl->data.length != 24) {
2594 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2595 (uintmax_t)aapl->data.length));
2596 return NT_STATUS_INVALID_PARAMETER;
2599 cmd = IVAL(aapl->data.data, 0);
2600 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2601 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2602 return NT_STATUS_INVALID_PARAMETER;
2605 req_bitmap = BVAL(aapl->data.data, 8);
2606 client_caps = BVAL(aapl->data.data, 16);
2608 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2610 SBVAL(p, 8, req_bitmap);
2611 ok = data_blob_append(req, &blob, p, 16);
2613 return NT_STATUS_UNSUCCESSFUL;
2616 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2617 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2618 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2619 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2620 config->readdir_attr_enabled = true;
2623 if (config->use_copyfile) {
2624 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2625 config->copyfile_enabled = true;
2629 * The client doesn't set the flag, so we can't check
2630 * for it and just set it unconditionally
2632 if (config->unix_info_enabled) {
2633 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2636 SBVAL(p, 0, server_caps);
2637 ok = data_blob_append(req, &blob, p, 8);
2639 return NT_STATUS_UNSUCCESSFUL;
2643 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2644 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2652 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2659 if (config->time_machine) {
2660 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2665 ok = data_blob_append(req, &blob, p, 8);
2667 return NT_STATUS_UNSUCCESSFUL;
2671 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2672 ok = convert_string_talloc(req,
2673 CH_UNIX, CH_UTF16LE,
2674 config->model, strlen(config->model),
2677 return NT_STATUS_UNSUCCESSFUL;
2681 SIVAL(p + 4, 0, modellen);
2682 ok = data_blob_append(req, &blob, p, 8);
2685 return NT_STATUS_UNSUCCESSFUL;
2688 ok = data_blob_append(req, &blob, model, modellen);
2691 return NT_STATUS_UNSUCCESSFUL;
2695 status = smb2_create_blob_add(out_context_blobs,
2697 SMB2_CREATE_TAG_AAPL,
2699 if (NT_STATUS_IS_OK(status)) {
2700 global_fruit_config.nego_aapl = true;
2701 if (config->aapl_zero_file_id) {
2702 aapl_force_zero_file_id(handle->conn->sconn);
2709 static bool readdir_attr_meta_finderi_stream(
2710 struct vfs_handle_struct *handle,
2711 const struct smb_filename *smb_fname,
2714 struct smb_filename *stream_name = NULL;
2715 files_struct *fsp = NULL;
2720 uint8_t buf[AFP_INFO_SIZE];
2722 stream_name = synthetic_smb_fname(talloc_tos(),
2723 smb_fname->base_name,
2724 AFPINFO_STREAM_NAME,
2725 NULL, smb_fname->flags);
2726 if (stream_name == NULL) {
2730 ret = SMB_VFS_STAT(handle->conn, stream_name);
2735 status = SMB_VFS_CREATE_FILE(
2736 handle->conn, /* conn */
2738 0, /* root_dir_fid */
2739 stream_name, /* fname */
2740 FILE_READ_DATA, /* access_mask */
2741 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2743 FILE_OPEN, /* create_disposition*/
2744 0, /* create_options */
2745 0, /* file_attributes */
2746 INTERNAL_OPEN_ONLY, /* oplock_request */
2748 0, /* allocation_size */
2749 0, /* private_flags */
2754 NULL, NULL); /* create context */
2756 TALLOC_FREE(stream_name);
2758 if (!NT_STATUS_IS_OK(status)) {
2762 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2763 if (nread != AFP_INFO_SIZE) {
2764 DBG_ERR("short read [%s] [%zd/%d]\n",
2765 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2770 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2777 close_file(NULL, fsp, NORMAL_CLOSE);
2783 static bool readdir_attr_meta_finderi_netatalk(
2784 struct vfs_handle_struct *handle,
2785 const struct smb_filename *smb_fname,
2788 struct adouble *ad = NULL;
2791 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2796 p = ad_get_entry(ad, ADEID_FINDERI);
2798 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2803 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2808 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2809 const struct smb_filename *smb_fname,
2810 struct readdir_attr_data *attr_data)
2812 struct fruit_config_data *config = NULL;
2813 uint32_t date_added;
2817 SMB_VFS_HANDLE_GET_DATA(handle, config,
2818 struct fruit_config_data,
2821 switch (config->meta) {
2822 case FRUIT_META_NETATALK:
2823 ok = readdir_attr_meta_finderi_netatalk(
2824 handle, smb_fname, &ai);
2827 case FRUIT_META_STREAM:
2828 ok = readdir_attr_meta_finderi_stream(
2829 handle, smb_fname, &ai);
2833 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2838 /* Don't bother with errors, it's likely ENOENT */
2842 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2844 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2845 &ai.afpi_FinderInfo[0], 4);
2847 /* finder_creator */
2848 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2849 &ai.afpi_FinderInfo[4], 4);
2853 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2854 &ai.afpi_FinderInfo[8], 2);
2856 /* finder_ext_flags */
2857 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2858 &ai.afpi_FinderInfo[24], 2);
2861 date_added = convert_time_t_to_uint32_t(
2862 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2864 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2869 static uint64_t readdir_attr_rfork_size_adouble(
2870 struct vfs_handle_struct *handle,
2871 const struct smb_filename *smb_fname)
2873 struct adouble *ad = NULL;
2874 uint64_t rfork_size;
2876 ad = ad_get(talloc_tos(), handle, smb_fname,
2882 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2888 static uint64_t readdir_attr_rfork_size_stream(
2889 struct vfs_handle_struct *handle,
2890 const struct smb_filename *smb_fname)
2892 struct smb_filename *stream_name = NULL;
2894 uint64_t rfork_size;
2896 stream_name = synthetic_smb_fname(talloc_tos(),
2897 smb_fname->base_name,
2898 AFPRESOURCE_STREAM_NAME,
2900 if (stream_name == NULL) {
2904 ret = SMB_VFS_STAT(handle->conn, stream_name);
2906 TALLOC_FREE(stream_name);
2910 rfork_size = stream_name->st.st_ex_size;
2911 TALLOC_FREE(stream_name);
2916 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2917 const struct smb_filename *smb_fname)
2919 struct fruit_config_data *config = NULL;
2920 uint64_t rfork_size;
2922 SMB_VFS_HANDLE_GET_DATA(handle, config,
2923 struct fruit_config_data,
2926 switch (config->rsrc) {
2927 case FRUIT_RSRC_ADFILE:
2928 case FRUIT_RSRC_XATTR:
2929 rfork_size = readdir_attr_rfork_size_adouble(handle,
2933 case FRUIT_META_STREAM:
2934 rfork_size = readdir_attr_rfork_size_stream(handle,
2939 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2947 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2948 const struct smb_filename *smb_fname,
2949 struct readdir_attr_data *attr_data)
2951 NTSTATUS status = NT_STATUS_OK;
2952 struct fruit_config_data *config = NULL;
2955 SMB_VFS_HANDLE_GET_DATA(handle, config,
2956 struct fruit_config_data,
2957 return NT_STATUS_UNSUCCESSFUL);
2960 /* Ensure we return a default value in the creation_date field */
2961 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2964 * Resource fork length
2967 if (config->readdir_attr_rsize) {
2968 uint64_t rfork_size;
2970 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2971 attr_data->attr_data.aapl.rfork_size = rfork_size;
2978 if (config->readdir_attr_finder_info) {
2979 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2981 status = NT_STATUS_INTERNAL_ERROR;
2988 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
2993 if (psd->dacl == NULL) {
2994 return NT_STATUS_OK;
2997 for (i = 0; i < psd->dacl->num_aces; i++) {
2998 /* MS NFS style mode/uid/gid */
2999 int cmp = dom_sid_compare_domain(
3000 &global_sid_Unix_NFS,
3001 &psd->dacl->aces[i].trustee);
3003 /* Normal ACE entry. */
3008 * security_descriptor_dacl_del()
3009 * *must* return NT_STATUS_OK as we know
3010 * we have something to remove.
3013 status = security_descriptor_dacl_del(psd,
3014 &psd->dacl->aces[i].trustee);
3015 if (!NT_STATUS_IS_OK(status)) {
3016 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3022 * security_descriptor_dacl_del() may delete more
3023 * then one entry subsequent to this one if the
3024 * SID matches, but we only need to ensure that
3025 * we stay looking at the same element in the array.
3029 return NT_STATUS_OK;
3032 /* Search MS NFS style ACE with UNIX mode */
3033 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3035 struct security_descriptor *psd,
3040 struct fruit_config_data *config = NULL;
3044 SMB_VFS_HANDLE_GET_DATA(handle, config,
3045 struct fruit_config_data,
3046 return NT_STATUS_UNSUCCESSFUL);
3048 if (!global_fruit_config.nego_aapl) {
3049 return NT_STATUS_OK;
3051 if (psd->dacl == NULL || !config->unix_info_enabled) {
3052 return NT_STATUS_OK;
3055 for (i = 0; i < psd->dacl->num_aces; i++) {
3056 if (dom_sid_compare_domain(
3057 &global_sid_Unix_NFS_Mode,
3058 &psd->dacl->aces[i].trustee) == 0) {
3059 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3060 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3063 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3064 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3070 * Remove any incoming virtual ACE entries generated by
3071 * fruit_fget_nt_acl().
3074 return remove_virtual_nfs_aces(psd);
3077 /****************************************************************************
3079 ****************************************************************************/
3081 static int fruit_connect(vfs_handle_struct *handle,
3082 const char *service,
3086 char *list = NULL, *newlist = NULL;
3087 struct fruit_config_data *config;
3089 DEBUG(10, ("fruit_connect\n"));
3091 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3096 rc = init_fruit_config(handle);
3101 SMB_VFS_HANDLE_GET_DATA(handle, config,
3102 struct fruit_config_data, return -1);
3104 if (config->veto_appledouble) {
3105 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3108 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3109 newlist = talloc_asprintf(
3111 "%s/" ADOUBLE_NAME_PREFIX "*/",
3113 lp_do_parameter(SNUM(handle->conn),
3118 lp_do_parameter(SNUM(handle->conn),
3120 "/" ADOUBLE_NAME_PREFIX "*/");
3126 if (config->encoding == FRUIT_ENC_NATIVE) {
3127 lp_do_parameter(SNUM(handle->conn),
3132 if (config->time_machine) {
3133 DBG_NOTICE("Enabling durable handles for Time Machine "
3134 "support on [%s]\n", service);
3135 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3136 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3137 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3138 if (!lp_strict_sync(SNUM(handle->conn))) {
3139 DBG_WARNING("Time Machine without strict sync is not "
3142 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3148 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3149 struct smb_filename *smb_fname,
3155 char afpinfo_buf[AFP_INFO_SIZE];
3156 ssize_t len, written;
3160 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3165 if (!(flags & (O_CREAT | O_TRUNC))) {
3169 ai = afpinfo_new(talloc_tos());
3175 len = afpinfo_pack(ai, afpinfo_buf);
3176 if (len != AFP_INFO_SIZE) {
3181 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3182 fsp->fh->fd = hostfd;
3184 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3187 if (written != AFP_INFO_SIZE) {
3188 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3196 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3199 int saved_errno = errno;
3201 fsp->fh->fd = hostfd;
3202 SMB_VFS_NEXT_CLOSE(handle, fsp);
3205 errno = saved_errno;
3210 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3211 struct smb_filename *smb_fname,
3218 struct adouble *ad = NULL;
3221 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3224 * Return a valid fd, but ensure any attempt to use it returns an error
3225 * (EPIPE). All operations on the smb_fname or the fsp will use path
3235 if (flags & (O_CREAT | O_TRUNC)) {
3237 * The attribute does not exist or needs to be truncated,
3238 * create an AppleDouble EA
3240 ad = ad_init(fsp, handle, ADOUBLE_META);
3246 rc = ad_set(ad, fsp->fsp_name);
3256 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3258 int saved_errno = errno;
3263 errno = saved_errno;
3268 static int fruit_open_meta(vfs_handle_struct *handle,
3269 struct smb_filename *smb_fname,
3270 files_struct *fsp, int flags, mode_t mode)
3273 struct fruit_config_data *config = NULL;
3274 struct fio *fio = NULL;
3276 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3278 SMB_VFS_HANDLE_GET_DATA(handle, config,
3279 struct fruit_config_data, return -1);
3281 switch (config->meta) {
3282 case FRUIT_META_STREAM:
3283 fd = fruit_open_meta_stream(handle, smb_fname,
3287 case FRUIT_META_NETATALK:
3288 fd = fruit_open_meta_netatalk(handle, smb_fname,
3293 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3297 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3303 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3304 fio->type = ADOUBLE_META;
3305 fio->config = config;
3310 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3311 struct smb_filename *smb_fname,
3317 struct adouble *ad = NULL;
3318 struct smb_filename *smb_fname_base = NULL;
3319 struct fruit_config_data *config = NULL;
3322 SMB_VFS_HANDLE_GET_DATA(handle, config,
3323 struct fruit_config_data, return -1);
3325 if ((!(flags & O_CREAT)) &&
3326 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3328 /* sorry, but directories don't habe a resource fork */
3333 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3338 /* Sanitize flags */
3339 if (flags & O_WRONLY) {
3340 /* We always need read access for the metadata header too */
3345 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3352 if (flags & (O_CREAT | O_TRUNC)) {
3353 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3359 fsp->fh->fd = hostfd;
3361 rc = ad_fset(ad, fsp);
3372 TALLOC_FREE(smb_fname_base);
3374 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3376 int saved_errno = errno;
3379 * BUGBUGBUG -- we would need to call
3380 * fd_close_posix here, but we don't have a
3383 fsp->fh->fd = hostfd;
3387 errno = saved_errno;
3392 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3393 struct smb_filename *smb_fname,
3398 #ifdef HAVE_ATTROPEN
3401 fd = attropen(smb_fname->base_name,
3402 AFPRESOURCE_EA_NETATALK,
3417 static int fruit_open_rsrc(vfs_handle_struct *handle,
3418 struct smb_filename *smb_fname,
3419 files_struct *fsp, int flags, mode_t mode)
3422 struct fruit_config_data *config = NULL;
3423 struct fio *fio = NULL;
3425 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3427 SMB_VFS_HANDLE_GET_DATA(handle, config,
3428 struct fruit_config_data, return -1);
3430 if (((flags & O_ACCMODE) == O_RDONLY)
3431 && (flags & O_CREAT)
3432 && !VALID_STAT(fsp->fsp_name->st))
3435 * This means the stream doesn't exist. macOS SMB server fails
3436 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3437 * 12565 and the test for this combination in
3438 * test_rfork_create().
3444 switch (config->rsrc) {
3445 case FRUIT_RSRC_STREAM:
3446 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3449 case FRUIT_RSRC_ADFILE:
3450 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3454 case FRUIT_RSRC_XATTR:
3455 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3460 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3464 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3470 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3471 fio->type = ADOUBLE_RSRC;
3472 fio->config = config;
3477 static int fruit_open(vfs_handle_struct *handle,
3478 struct smb_filename *smb_fname,
3479 files_struct *fsp, int flags, mode_t mode)
3483 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3485 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3486 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3489 if (is_afpinfo_stream(smb_fname)) {
3490 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3491 } else if (is_afpresource_stream(smb_fname)) {
3492 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3494 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3497 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3502 static int fruit_rename(struct vfs_handle_struct *handle,
3503 const struct smb_filename *smb_fname_src,
3504 const struct smb_filename *smb_fname_dst)
3507 struct fruit_config_data *config = NULL;
3508 struct smb_filename *src_adp_smb_fname = NULL;
3509 struct smb_filename *dst_adp_smb_fname = NULL;
3511 SMB_VFS_HANDLE_GET_DATA(handle, config,
3512 struct fruit_config_data, return -1);
3514 if (!VALID_STAT(smb_fname_src->st)) {
3515 DBG_ERR("Need valid stat for [%s]\n",
3516 smb_fname_str_dbg(smb_fname_src));
3520 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3525 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3526 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3531 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3536 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3541 DBG_DEBUG("%s -> %s\n",
3542 smb_fname_str_dbg(src_adp_smb_fname),
3543 smb_fname_str_dbg(dst_adp_smb_fname));
3545 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3546 if (errno == ENOENT) {
3551 TALLOC_FREE(src_adp_smb_fname);
3552 TALLOC_FREE(dst_adp_smb_fname);
3556 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3557 const struct smb_filename *smb_fname)
3559 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3562 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3563 const struct smb_filename *smb_fname)
3565 return SMB_VFS_REMOVEXATTR(handle->conn,
3567 AFPINFO_EA_NETATALK);
3570 static int fruit_unlink_meta(vfs_handle_struct *handle,
3571 const struct smb_filename *smb_fname)
3573 struct fruit_config_data *config = NULL;
3576 SMB_VFS_HANDLE_GET_DATA(handle, config,
3577 struct fruit_config_data, return -1);
3579 switch (config->meta) {
3580 case FRUIT_META_STREAM:
3581 rc = fruit_unlink_meta_stream(handle, smb_fname);
3584 case FRUIT_META_NETATALK:
3585 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3589 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3596 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3597 const struct smb_filename *smb_fname,
3602 if (!force_unlink) {
3603 struct smb_filename *smb_fname_cp = NULL;
3606 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3607 if (smb_fname_cp == NULL) {
3612 * 0 byte resource fork streams are not listed by
3613 * vfs_streaminfo, as a result stream cleanup/deletion of file
3614 * deletion doesn't remove the resourcefork stream.
3617 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3619 TALLOC_FREE(smb_fname_cp);
3620 DBG_ERR("stat [%s] failed [%s]\n",
3621 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3625 size = smb_fname_cp->st.st_ex_size;
3626 TALLOC_FREE(smb_fname_cp);
3629 /* OS X ignores resource fork stream delete requests */
3634 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3635 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3642 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3643 const struct smb_filename *smb_fname,
3647 struct adouble *ad = NULL;
3648 struct smb_filename *adp_smb_fname = NULL;
3650 if (!force_unlink) {
3651 ad = ad_get(talloc_tos(), handle, smb_fname,
3660 * 0 byte resource fork streams are not listed by
3661 * vfs_streaminfo, as a result stream cleanup/deletion of file
3662 * deletion doesn't remove the resourcefork stream.
3665 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3666 /* OS X ignores resource fork stream delete requests */
3674 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3679 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3680 TALLOC_FREE(adp_smb_fname);
3681 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3688 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3689 const struct smb_filename *smb_fname,
3693 * OS X ignores resource fork stream delete requests, so nothing to do
3694 * here. Removing the file will remove the xattr anyway, so we don't
3695 * have to take care of removing 0 byte resource forks that could be
3701 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3702 const struct smb_filename *smb_fname,
3705 struct fruit_config_data *config = NULL;
3708 SMB_VFS_HANDLE_GET_DATA(handle, config,
3709 struct fruit_config_data, return -1);
3711 switch (config->rsrc) {
3712 case FRUIT_RSRC_STREAM:
3713 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3716 case FRUIT_RSRC_ADFILE:
3717 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3720 case FRUIT_RSRC_XATTR:
3721 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3725 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3732 static int fruit_unlink(vfs_handle_struct *handle,
3733 const struct smb_filename *smb_fname)
3736 struct fruit_config_data *config = NULL;
3737 struct smb_filename *rsrc_smb_fname = NULL;
3739 SMB_VFS_HANDLE_GET_DATA(handle, config,
3740 struct fruit_config_data, return -1);
3742 if (is_afpinfo_stream(smb_fname)) {
3743 return fruit_unlink_meta(handle, smb_fname);
3744 } else if (is_afpresource_stream(smb_fname)) {
3745 return fruit_unlink_rsrc(handle, smb_fname, false);
3746 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3747 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3751 * A request to delete the base file. Because 0 byte resource
3752 * fork streams are not listed by fruit_streaminfo,
3753 * delete_all_streams() can't remove 0 byte resource fork
3754 * streams, so we have to cleanup this here.
3756 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3757 smb_fname->base_name,
3758 AFPRESOURCE_STREAM_NAME,
3761 if (rsrc_smb_fname == NULL) {
3765 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3766 if ((rc != 0) && (errno != ENOENT)) {
3767 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3768 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3769 TALLOC_FREE(rsrc_smb_fname);
3772 TALLOC_FREE(rsrc_smb_fname);
3774 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3777 static int fruit_chmod(vfs_handle_struct *handle,
3778 const struct smb_filename *smb_fname,
3782 struct fruit_config_data *config = NULL;
3783 struct smb_filename *smb_fname_adp = NULL;
3785 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3790 SMB_VFS_HANDLE_GET_DATA(handle, config,
3791 struct fruit_config_data, return -1);
3793 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3797 if (!VALID_STAT(smb_fname->st)) {
3801 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3805 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3810 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3812 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3813 if (errno == ENOENT) {
3817 TALLOC_FREE(smb_fname_adp);
3821 static int fruit_chown(vfs_handle_struct *handle,
3822 const struct smb_filename *smb_fname,
3827 struct fruit_config_data *config = NULL;
3828 struct smb_filename *adp_smb_fname = NULL;
3830 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3835 SMB_VFS_HANDLE_GET_DATA(handle, config,
3836 struct fruit_config_data, return -1);
3838 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3842 if (!VALID_STAT(smb_fname->st)) {
3846 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3850 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3855 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3857 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3858 if (errno == ENOENT) {
3863 TALLOC_FREE(adp_smb_fname);
3867 static int fruit_rmdir(struct vfs_handle_struct *handle,
3868 const struct smb_filename *smb_fname)
3872 struct fruit_config_data *config;
3874 SMB_VFS_HANDLE_GET_DATA(handle, config,
3875 struct fruit_config_data, return -1);
3877 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3882 * Due to there is no way to change bDeleteVetoFiles variable
3883 * from this module, need to clean up ourselves
3886 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3891 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3893 struct adouble *ad = NULL;
3895 struct smb_filename *ad_smb_fname = NULL;
3898 match = strncmp(de->d_name,
3899 ADOUBLE_NAME_PREFIX,
3900 strlen(ADOUBLE_NAME_PREFIX));
3905 p = talloc_asprintf(talloc_tos(), "%s/%s",
3906 smb_fname->base_name, de->d_name);
3908 DBG_ERR("talloc_asprintf failed\n");
3912 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3916 if (ad_smb_fname == NULL) {
3917 DBG_ERR("synthetic_smb_fname failed\n");
3922 * Check whether it's a valid AppleDouble file, if
3923 * yes, delete it, ignore it otherwise.
3925 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3927 TALLOC_FREE(ad_smb_fname);
3933 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3935 DBG_ERR("Deleting [%s] failed\n",
3936 smb_fname_str_dbg(ad_smb_fname));
3938 TALLOC_FREE(ad_smb_fname);
3943 SMB_VFS_CLOSEDIR(handle->conn, dh);
3945 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3948 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3949 files_struct *fsp, void *data,
3950 size_t n, off_t offset)
3955 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3961 DBG_ERR("Removing [%s] after short read [%zd]\n",
3962 fsp_str_dbg(fsp), nread);
3964 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3966 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3974 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3975 files_struct *fsp, void *data,
3976 size_t n, off_t offset)
3979 struct adouble *ad = NULL;
3980 char afpinfo_buf[AFP_INFO_SIZE];
3984 ai = afpinfo_new(talloc_tos());
3989 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3995 p = ad_get_entry(ad, ADEID_FINDERI);
3997 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4002 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
4004 nread = afpinfo_pack(ai, afpinfo_buf);
4005 if (nread != AFP_INFO_SIZE) {
4010 memcpy(data, afpinfo_buf, n);
4018 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
4019 files_struct *fsp, void *data,
4020 size_t n, off_t offset)
4022 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4027 * OS X has a off-by-1 error in the offset calculation, so we're
4028 * bug compatible here. It won't hurt, as any relevant real
4029 * world read requests from the AFP_AfpInfo stream will be
4030 * offset=0 n=60. offset is ignored anyway, see below.
4032 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4037 DBG_ERR("Failed to fetch fsp extension");
4041 /* Yes, macOS always reads from offset 0 */
4043 to_return = MIN(n, AFP_INFO_SIZE);
4045 switch (fio->config->meta) {
4046 case FRUIT_META_STREAM:
4047 nread = fruit_pread_meta_stream(handle, fsp, data,
4051 case FRUIT_META_NETATALK:
4052 nread = fruit_pread_meta_adouble(handle, fsp, data,
4057 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4064 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4065 files_struct *fsp, void *data,
4066 size_t n, off_t offset)
4068 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4071 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4072 files_struct *fsp, void *data,
4073 size_t n, off_t offset)
4075 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4078 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4079 files_struct *fsp, void *data,
4080 size_t n, off_t offset)
4082 struct adouble *ad = NULL;
4085 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4090 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4091 offset + ad_getentryoff(ad, ADEID_RFORK));
4097 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4098 files_struct *fsp, void *data,
4099 size_t n, off_t offset)
4101 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4109 switch (fio->config->rsrc) {
4110 case FRUIT_RSRC_STREAM:
4111 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4114 case FRUIT_RSRC_ADFILE:
4115 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4118 case FRUIT_RSRC_XATTR:
4119 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4123 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4130 static ssize_t fruit_pread(vfs_handle_struct *handle,
4131 files_struct *fsp, void *data,
4132 size_t n, off_t offset)
4134 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4137 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4138 fsp_str_dbg(fsp), (intmax_t)offset, n);
4141 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4144 if (fio->type == ADOUBLE_META) {
4145 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4147 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4150 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4154 static bool fruit_must_handle_aio_stream(struct fio *fio)
4160 if ((fio->type == ADOUBLE_META) &&
4161 (fio->config->meta == FRUIT_META_NETATALK))
4166 if ((fio->type == ADOUBLE_RSRC) &&
4167 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4175 struct fruit_pread_state {
4177 struct vfs_aio_state vfs_aio_state;
4180 static void fruit_pread_done(struct tevent_req *subreq);
4182 static struct tevent_req *fruit_pread_send(
4183 struct vfs_handle_struct *handle,
4184 TALLOC_CTX *mem_ctx,
4185 struct tevent_context *ev,
4186 struct files_struct *fsp,
4188 size_t n, off_t offset)
4190 struct tevent_req *req = NULL;
4191 struct tevent_req *subreq = NULL;
4192 struct fruit_pread_state *state = NULL;
4193 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4195 req = tevent_req_create(mem_ctx, &state,
4196 struct fruit_pread_state);
4201 if (fruit_must_handle_aio_stream(fio)) {
4202 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4203 if (state->nread != n) {
4204 if (state->nread != -1) {
4207 tevent_req_error(req, errno);
4208 return tevent_req_post(req, ev);
4210 tevent_req_done(req);
4211 return tevent_req_post(req, ev);
4214 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4216 if (tevent_req_nomem(req, subreq)) {
4217 return tevent_req_post(req, ev);
4219 tevent_req_set_callback(subreq, fruit_pread_done, req);
4223 static void fruit_pread_done(struct tevent_req *subreq)
4225 struct tevent_req *req = tevent_req_callback_data(
4226 subreq, struct tevent_req);
4227 struct fruit_pread_state *state = tevent_req_data(
4228 req, struct fruit_pread_state);
4230 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4231 TALLOC_FREE(subreq);
4233 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4236 tevent_req_done(req);
4239 static ssize_t fruit_pread_recv(struct tevent_req *req,
4240 struct vfs_aio_state *vfs_aio_state)
4242 struct fruit_pread_state *state = tevent_req_data(
4243 req, struct fruit_pread_state);
4245 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4249 *vfs_aio_state = state->vfs_aio_state;
4250 return state->nread;
4253 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4254 files_struct *fsp, const void *data,
4255 size_t n, off_t offset)
4261 ai = afpinfo_unpack(talloc_tos(), data);
4266 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4267 if (nwritten != n) {
4271 if (!ai_empty_finderinfo(ai)) {
4275 ok = set_delete_on_close(
4278 handle->conn->session_info->security_token,
4279 handle->conn->session_info->unix_token);
4281 DBG_ERR("set_delete_on_close on [%s] failed\n",
4289 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4290 files_struct *fsp, const void *data,
4291 size_t n, off_t offset)
4293 struct adouble *ad = NULL;
4299 ai = afpinfo_unpack(talloc_tos(), data);
4304 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4306 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4311 p = ad_get_entry(ad, ADEID_FINDERI);
4313 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4318 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4320 ret = ad_fset(ad, fsp);
4322 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4329 if (!ai_empty_finderinfo(ai)) {
4333 ok = set_delete_on_close(
4336 handle->conn->session_info->security_token,
4337 handle->conn->session_info->unix_token);
4339 DBG_ERR("set_delete_on_close on [%s] failed\n",
4347 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4348 files_struct *fsp, const void *data,
4349 size_t n, off_t offset)
4351 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4355 * Writing an all 0 blob to the metadata stream
4356 * results in the stream being removed on a macOS
4357 * server. This ensures we behave the same and it
4358 * verified by the "delete AFP_AfpInfo by writing all
4361 if (n != AFP_INFO_SIZE || offset != 0) {
4362 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4363 (intmax_t)offset, (intmax_t)n);
4368 DBG_ERR("Failed to fetch fsp extension");
4372 switch (fio->config->meta) {
4373 case FRUIT_META_STREAM:
4374 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4378 case FRUIT_META_NETATALK:
4379 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4384 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4391 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4392 files_struct *fsp, const void *data,
4393 size_t n, off_t offset)
4395 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4398 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4399 files_struct *fsp, const void *data,
4400 size_t n, off_t offset)
4402 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4405 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4406 files_struct *fsp, const void *data,
4407 size_t n, off_t offset)
4409 struct adouble *ad = NULL;
4413 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4415 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4419 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4420 offset + ad_getentryoff(ad, ADEID_RFORK));
4421 if (nwritten != n) {
4422 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4423 fsp_str_dbg(fsp), nwritten, n);
4428 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4429 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4430 ret = ad_fset(ad, fsp);
4432 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4442 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4443 files_struct *fsp, const void *data,
4444 size_t n, off_t offset)
4446 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4450 DBG_ERR("Failed to fetch fsp extension");
4454 switch (fio->config->rsrc) {
4455 case FRUIT_RSRC_STREAM:
4456 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4459 case FRUIT_RSRC_ADFILE:
4460 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4463 case FRUIT_RSRC_XATTR:
4464 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4468 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4475 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4476 files_struct *fsp, const void *data,
4477 size_t n, off_t offset)
4479 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4482 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4483 fsp_str_dbg(fsp), (intmax_t)offset, n);
4486 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4489 if (fio->type == ADOUBLE_META) {
4490 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4492 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4495 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4499 struct fruit_pwrite_state {
4501 struct vfs_aio_state vfs_aio_state;
4504 static void fruit_pwrite_done(struct tevent_req *subreq);
4506 static struct tevent_req *fruit_pwrite_send(
4507 struct vfs_handle_struct *handle,
4508 TALLOC_CTX *mem_ctx,
4509 struct tevent_context *ev,
4510 struct files_struct *fsp,
4512 size_t n, off_t offset)
4514 struct tevent_req *req = NULL;
4515 struct tevent_req *subreq = NULL;
4516 struct fruit_pwrite_state *state = NULL;
4517 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4519 req = tevent_req_create(mem_ctx, &state,
4520 struct fruit_pwrite_state);
4525 if (fruit_must_handle_aio_stream(fio)) {
4526 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4527 if (state->nwritten != n) {
4528 if (state->nwritten != -1) {
4531 tevent_req_error(req, errno);
4532 return tevent_req_post(req, ev);
4534 tevent_req_done(req);
4535 return tevent_req_post(req, ev);
4538 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4540 if (tevent_req_nomem(req, subreq)) {
4541 return tevent_req_post(req, ev);
4543 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4547 static void fruit_pwrite_done(struct tevent_req *subreq)
4549 struct tevent_req *req = tevent_req_callback_data(
4550 subreq, struct tevent_req);
4551 struct fruit_pwrite_state *state = tevent_req_data(
4552 req, struct fruit_pwrite_state);
4554 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4555 TALLOC_FREE(subreq);
4557 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4560 tevent_req_done(req);
4563 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4564 struct vfs_aio_state *vfs_aio_state)
4566 struct fruit_pwrite_state *state = tevent_req_data(
4567 req, struct fruit_pwrite_state);
4569 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4573 *vfs_aio_state = state->vfs_aio_state;
4574 return state->nwritten;
4578 * Helper to stat/lstat the base file of an smb_fname.
4580 static int fruit_stat_base(vfs_handle_struct *handle,
4581 struct smb_filename *smb_fname,
4584 char *tmp_stream_name;
4587 tmp_stream_name = smb_fname->stream_name;
4588 smb_fname->stream_name = NULL;
4590 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4592 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4594 smb_fname->stream_name = tmp_stream_name;
4598 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4599 struct smb_filename *smb_fname,
4605 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4607 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4613 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4614 struct smb_filename *smb_fname,
4617 struct adouble *ad = NULL;
4619 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4621 DBG_INFO("fruit_stat_meta %s: %s\n",
4622 smb_fname_str_dbg(smb_fname), strerror(errno));
4628 /* Populate the stat struct with info from the base file. */
4629 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4632 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4633 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4634 smb_fname->stream_name);
4638 static int fruit_stat_meta(vfs_handle_struct *handle,
4639 struct smb_filename *smb_fname,
4642 struct fruit_config_data *config = NULL;
4645 SMB_VFS_HANDLE_GET_DATA(handle, config,
4646 struct fruit_config_data, return -1);
4648 switch (config->meta) {
4649 case FRUIT_META_STREAM:
4650 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4653 case FRUIT_META_NETATALK:
4654 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4658 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4665 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4666 struct smb_filename *smb_fname,
4669 struct adouble *ad = NULL;
4672 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4678 /* Populate the stat struct with info from the base file. */
4679 ret = fruit_stat_base(handle, smb_fname, follow_links);
4685 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4686 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4687 smb_fname->stream_name);
4692 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4693 struct smb_filename *smb_fname,
4699 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4701 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4707 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4708 struct smb_filename *smb_fname,
4711 #ifdef HAVE_ATTROPEN
4715 /* Populate the stat struct with info from the base file. */
4716 ret = fruit_stat_base(handle, smb_fname, follow_links);
4721 fd = attropen(smb_fname->base_name,
4722 AFPRESOURCE_EA_NETATALK,
4728 ret = sys_fstat(fd, &smb_fname->st, false);
4731 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4732 AFPRESOURCE_EA_NETATALK);
4738 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4739 smb_fname->stream_name);
4749 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4750 struct smb_filename *smb_fname,
4753 struct fruit_config_data *config = NULL;
4756 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4758 SMB_VFS_HANDLE_GET_DATA(handle, config,
4759 struct fruit_config_data, return -1);
4761 switch (config->rsrc) {
4762 case FRUIT_RSRC_STREAM:
4763 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4766 case FRUIT_RSRC_XATTR:
4767 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4770 case FRUIT_RSRC_ADFILE:
4771 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4775 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4782 static int fruit_stat(vfs_handle_struct *handle,
4783 struct smb_filename *smb_fname)
4787 DEBUG(10, ("fruit_stat called for %s\n",
4788 smb_fname_str_dbg(smb_fname)));
4790 if (!is_ntfs_stream_smb_fname(smb_fname)
4791 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4792 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4794 update_btime(handle, smb_fname);
4800 * Note if lp_posix_paths() is true, we can never
4801 * get here as is_ntfs_stream_smb_fname() is
4802 * always false. So we never need worry about
4803 * not following links here.
4806 if (is_afpinfo_stream(smb_fname)) {
4807 rc = fruit_stat_meta(handle, smb_fname, true);
4808 } else if (is_afpresource_stream(smb_fname)) {
4809 rc = fruit_stat_rsrc(handle, smb_fname, true);
4811 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4815 update_btime(handle, smb_fname);
4816 smb_fname->st.st_ex_mode &= ~S_IFMT;
4817 smb_fname->st.st_ex_mode |= S_IFREG;
4818 smb_fname->st.st_ex_blocks =
4819 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4824 static int fruit_lstat(vfs_handle_struct *handle,
4825 struct smb_filename *smb_fname)
4829 DEBUG(10, ("fruit_lstat called for %s\n",
4830 smb_fname_str_dbg(smb_fname)));
4832 if (!is_ntfs_stream_smb_fname(smb_fname)
4833 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4834 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4836 update_btime(handle, smb_fname);
4841 if (is_afpinfo_stream(smb_fname)) {
4842 rc = fruit_stat_meta(handle, smb_fname, false);
4843 } else if (is_afpresource_stream(smb_fname)) {
4844 rc = fruit_stat_rsrc(handle, smb_fname, false);
4846 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4850 update_btime(handle, smb_fname);
4851 smb_fname->st.st_ex_mode &= ~S_IFMT;
4852 smb_fname->st.st_ex_mode |= S_IFREG;
4853 smb_fname->st.st_ex_blocks =
4854 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4859 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4861 SMB_STRUCT_STAT *sbuf)
4863 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4866 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4868 SMB_STRUCT_STAT *sbuf)
4872 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4877 *sbuf = fsp->base_fsp->fsp_name->st;
4878 sbuf->st_ex_size = AFP_INFO_SIZE;
4879 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4884 static int fruit_fstat_meta(vfs_handle_struct *handle,
4886 SMB_STRUCT_STAT *sbuf,
4891 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4893 switch (fio->config->meta) {
4894 case FRUIT_META_STREAM:
4895 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4898 case FRUIT_META_NETATALK:
4899 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4903 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4907 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4911 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4913 SMB_STRUCT_STAT *sbuf)
4915 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4918 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4920 SMB_STRUCT_STAT *sbuf)
4922 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4925 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4927 SMB_STRUCT_STAT *sbuf)
4929 struct adouble *ad = NULL;
4932 /* Populate the stat struct with info from the base file. */
4933 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4938 ad = ad_get(talloc_tos(), handle,
4939 fsp->base_fsp->fsp_name,
4942 DBG_ERR("ad_get [%s] failed [%s]\n",
4943 fsp_str_dbg(fsp), strerror(errno));
4947 *sbuf = fsp->base_fsp->fsp_name->st;
4948 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4949 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4955 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4956 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4960 switch (fio->config->rsrc) {
4961 case FRUIT_RSRC_STREAM:
4962 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4965 case FRUIT_RSRC_ADFILE:
4966 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4969 case FRUIT_RSRC_XATTR:
4970 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4974 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4981 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4982 SMB_STRUCT_STAT *sbuf)
4984 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4988 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4991 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4993 if (fio->type == ADOUBLE_META) {
4994 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4996 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
5000 sbuf->st_ex_mode &= ~S_IFMT;
5001 sbuf->st_ex_mode |= S_IFREG;
5002 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
5005 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
5006 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
5010 static NTSTATUS delete_invalid_meta_stream(
5011 vfs_handle_struct *handle,
5012 const struct smb_filename *smb_fname,
5013 TALLOC_CTX *mem_ctx,
5014 unsigned int *pnum_streams,
5015 struct stream_struct **pstreams)
5017 struct smb_filename *sname = NULL;
5021 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
5023 return NT_STATUS_INTERNAL_ERROR;
5026 sname = synthetic_smb_fname(talloc_tos(),
5027 smb_fname->base_name,
5028 AFPINFO_STREAM_NAME,
5030 if (sname == NULL) {
5031 return NT_STATUS_NO_MEMORY;
5034 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5037 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5038 return map_nt_error_from_unix(errno);
5041 return NT_STATUS_OK;
5044 static NTSTATUS fruit_streaminfo_meta_stream(
5045 vfs_handle_struct *handle,
5046 struct files_struct *fsp,
5047 const struct smb_filename *smb_fname,
5048 TALLOC_CTX *mem_ctx,
5049 unsigned int *pnum_streams,
5050 struct stream_struct **pstreams)
5052 struct stream_struct *stream = *pstreams;
5053 unsigned int num_streams = *pnum_streams;
5054 struct smb_filename *sname = NULL;
5055 char *full_name = NULL;
5057 struct share_mode_lock *lck = NULL;
5058 struct file_id id = {0};
5059 bool delete_on_close_set;
5065 for (i = 0; i < num_streams; i++) {
5066 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5071 if (i == num_streams) {
5072 return NT_STATUS_OK;
5075 if (stream[i].size != AFP_INFO_SIZE) {
5076 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5077 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5079 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5080 pnum_streams, pstreams);
5084 * Now check if there's a delete-on-close pending on the stream. If so,
5085 * hide the stream. This behaviour was verified against a macOS 10.12
5089 sname = synthetic_smb_fname(talloc_tos(),
5090 smb_fname->base_name,
5091 AFPINFO_STREAM_NAME,
5093 if (sname == NULL) {
5094 status = NT_STATUS_NO_MEMORY;
5098 ret = SMB_VFS_NEXT_STAT(handle, sname);
5100 status = map_nt_error_from_unix(errno);
5104 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5106 lck = get_existing_share_mode_lock(talloc_tos(), id);
5108 status = NT_STATUS_OK;
5112 full_name = talloc_asprintf(talloc_tos(),
5116 if (full_name == NULL) {
5117 status = NT_STATUS_NO_MEMORY;
5121 status = file_name_hash(handle->conn, full_name, &name_hash);
5122 if (!NT_STATUS_IS_OK(status)) {
5126 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5127 if (delete_on_close_set) {
5128 ok = del_fruit_stream(mem_ctx,
5133 status = NT_STATUS_INTERNAL_ERROR;
5138 status = NT_STATUS_OK;
5143 TALLOC_FREE(full_name);
5147 static NTSTATUS fruit_streaminfo_meta_netatalk(
5148 vfs_handle_struct *handle,
5149 struct files_struct *fsp,
5150 const struct smb_filename *smb_fname,
5151 TALLOC_CTX *mem_ctx,
5152 unsigned int *pnum_streams,
5153 struct stream_struct **pstreams)
5155 struct stream_struct *stream = *pstreams;
5156 unsigned int num_streams = *pnum_streams;
5157 struct adouble *ad = NULL;
5162 /* Remove the Netatalk xattr from the list */
5163 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5164 ":" NETATALK_META_XATTR ":$DATA");
5166 return NT_STATUS_NO_MEMORY;
5170 * Check if there's a AFPINFO_STREAM from the VFS streams
5171 * backend and if yes, remove it from the list
5173 for (i = 0; i < num_streams; i++) {
5174 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5179 if (i < num_streams) {
5180 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5181 smb_fname_str_dbg(smb_fname));
5183 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5186 return NT_STATUS_INTERNAL_ERROR;
5190 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5192 return NT_STATUS_OK;
5195 is_fi_empty = ad_empty_finderinfo(ad);
5199 return NT_STATUS_OK;
5202 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5203 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5204 smb_roundup(handle->conn, AFP_INFO_SIZE));
5206 return NT_STATUS_NO_MEMORY;
5209 return NT_STATUS_OK;
5212 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5213 struct files_struct *fsp,
5214 const struct smb_filename *smb_fname,
5215 TALLOC_CTX *mem_ctx,
5216 unsigned int *pnum_streams,
5217 struct stream_struct **pstreams)
5219 struct fruit_config_data *config = NULL;
5222 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5223 return NT_STATUS_INTERNAL_ERROR);
5225 switch (config->meta) {
5226 case FRUIT_META_NETATALK:
5227 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5228 mem_ctx, pnum_streams,
5232 case FRUIT_META_STREAM:
5233 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5234 mem_ctx, pnum_streams,
5239 return NT_STATUS_INTERNAL_ERROR;
5245 static NTSTATUS fruit_streaminfo_rsrc_stream(
5246 vfs_handle_struct *handle,
5247 struct files_struct *fsp,
5248 const struct smb_filename *smb_fname,
5249 TALLOC_CTX *mem_ctx,
5250 unsigned int *pnum_streams,
5251 struct stream_struct **pstreams)
5255 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5257 DBG_ERR("Filtering resource stream failed\n");
5258 return NT_STATUS_INTERNAL_ERROR;
5260 return NT_STATUS_OK;
5263 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5264 vfs_handle_struct *handle,
5265 struct files_struct *fsp,
5266 const struct smb_filename *smb_fname,
5267 TALLOC_CTX *mem_ctx,
5268 unsigned int *pnum_streams,
5269 struct stream_struct **pstreams)
5273 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5275 DBG_ERR("Filtering resource stream failed\n");
5276 return NT_STATUS_INTERNAL_ERROR;
5278 return NT_STATUS_OK;
5281 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5282 vfs_handle_struct *handle,
5283 struct files_struct *fsp,
5284 const struct smb_filename *smb_fname,
5285 TALLOC_CTX *mem_ctx,
5286 unsigned int *pnum_streams,
5287 struct stream_struct **pstreams)
5289 struct stream_struct *stream = *pstreams;
5290 unsigned int num_streams = *pnum_streams;
5291 struct adouble *ad = NULL;
5297 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5298 * and if yes, remove it from the list
5300 for (i = 0; i < num_streams; i++) {
5301 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5306 if (i < num_streams) {
5307 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5308 smb_fname_str_dbg(smb_fname));
5310 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5311 AFPRESOURCE_STREAM);
5313 return NT_STATUS_INTERNAL_ERROR;
5317 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5319 return NT_STATUS_OK;
5322 rlen = ad_getentrylen(ad, ADEID_RFORK);
5326 return NT_STATUS_OK;
5329 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5330 AFPRESOURCE_STREAM_NAME, rlen,
5331 smb_roundup(handle->conn, rlen));
5333 return NT_STATUS_NO_MEMORY;
5336 return NT_STATUS_OK;
5339 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5340 struct files_struct *fsp,
5341 const struct smb_filename *smb_fname,
5342 TALLOC_CTX *mem_ctx,
5343 unsigned int *pnum_streams,
5344 struct stream_struct **pstreams)
5346 struct fruit_config_data *config = NULL;
5349 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5350 return NT_STATUS_INTERNAL_ERROR);
5352 switch (config->rsrc) {
5353 case FRUIT_RSRC_STREAM:
5354 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5355 mem_ctx, pnum_streams,
5359 case FRUIT_RSRC_XATTR:
5360 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5361 mem_ctx, pnum_streams,
5365 case FRUIT_RSRC_ADFILE:
5366 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5367 mem_ctx, pnum_streams,
5372 return NT_STATUS_INTERNAL_ERROR;
5378 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5379 struct files_struct *fsp,
5380 const struct smb_filename *smb_fname,
5381 TALLOC_CTX *mem_ctx,
5382 unsigned int *pnum_streams,
5383 struct stream_struct **pstreams)
5385 struct fruit_config_data *config = NULL;
5388 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5389 return NT_STATUS_UNSUCCESSFUL);
5391 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5393 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5394 pnum_streams, pstreams);
5395 if (!NT_STATUS_IS_OK(status)) {
5399 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5400 mem_ctx, pnum_streams, pstreams);
5401 if (!NT_STATUS_IS_OK(status)) {
5405 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5406 mem_ctx, pnum_streams, pstreams);
5407 if (!NT_STATUS_IS_OK(status)) {
5411 return NT_STATUS_OK;
5414 static int fruit_ntimes(vfs_handle_struct *handle,
5415 const struct smb_filename *smb_fname,
5416 struct smb_file_time *ft)
5419 struct adouble *ad = NULL;
5420 struct fruit_config_data *config = NULL;
5422 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5425 if ((config->meta != FRUIT_META_NETATALK) ||
5426 null_timespec(ft->create_time))
5428 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5431 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5432 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5434 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5439 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5440 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5442 rc = ad_set(ad, smb_fname);
5448 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5451 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5454 static int fruit_fallocate(struct vfs_handle_struct *handle,
5455 struct files_struct *fsp,
5460 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5463 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5466 /* Let the pwrite code path handle it. */
5471 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5472 struct files_struct *fsp,
5476 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5479 #ifdef HAVE_ATTROPEN
5480 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5485 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5486 struct files_struct *fsp,
5490 struct adouble *ad = NULL;
5493 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5495 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5496 fsp_str_dbg(fsp), strerror(errno));
5500 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5502 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5508 ad_setentrylen(ad, ADEID_RFORK, offset);
5510 rc = ad_fset(ad, fsp);
5512 DBG_ERR("ad_fset [%s] failed [%s]\n",
5513 fsp_str_dbg(fsp), strerror(errno));
5522 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5523 struct files_struct *fsp,
5527 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5530 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5533 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5534 struct files_struct *fsp,
5537 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5541 DBG_ERR("Failed to fetch fsp extension");
5545 switch (fio->config->rsrc) {
5546 case FRUIT_RSRC_XATTR:
5547 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5550 case FRUIT_RSRC_ADFILE:
5551 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5554 case FRUIT_RSRC_STREAM:
5555 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5559 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5567 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5568 struct files_struct *fsp,
5572 DBG_WARNING("ftruncate %s to %jd",
5573 fsp_str_dbg(fsp), (intmax_t)offset);
5574 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5579 /* OS X returns success but does nothing */
5580 DBG_INFO("ignoring ftruncate %s to %jd\n",
5581 fsp_str_dbg(fsp), (intmax_t)offset);
5585 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5586 struct files_struct *fsp,
5589 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5592 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5597 global_fruit_config.nego_aapl &&
5598 is_ntfs_stream_smb_fname(fsp->fsp_name) &&
5599 !is_ntfs_default_stream_smb_fname(fsp->fsp_name))
5601 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5603 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5606 if (fio->type == ADOUBLE_META) {
5607 ret = fruit_ftruncate_meta(handle, fsp, offset);
5609 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5612 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5616 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5617 struct smb_request *req,
5618 uint16_t root_dir_fid,
5619 struct smb_filename *smb_fname,
5620 uint32_t access_mask,
5621 uint32_t share_access,
5622 uint32_t create_disposition,
5623 uint32_t create_options,
5624 uint32_t file_attributes,
5625 uint32_t oplock_request,
5626 struct smb2_lease *lease,
5627 uint64_t allocation_size,
5628 uint32_t private_flags,
5629 struct security_descriptor *sd,
5630 struct ea_list *ea_list,
5631 files_struct **result,
5633 const struct smb2_create_blobs *in_context_blobs,
5634 struct smb2_create_blobs *out_context_blobs)
5637 struct fruit_config_data *config = NULL;
5638 files_struct *fsp = NULL;
5640 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5641 if (!NT_STATUS_IS_OK(status)) {
5645 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5646 return NT_STATUS_UNSUCCESSFUL);
5648 status = SMB_VFS_NEXT_CREATE_FILE(
5649 handle, req, root_dir_fid, smb_fname,
5650 access_mask, share_access,
5651 create_disposition, create_options,
5652 file_attributes, oplock_request,
5654 allocation_size, private_flags,
5655 sd, ea_list, result,
5656 pinfo, in_context_blobs, out_context_blobs);
5657 if (!NT_STATUS_IS_OK(status)) {
5663 if (global_fruit_config.nego_aapl) {
5664 if (config->posix_rename && fsp->is_directory) {
5666 * Enable POSIX directory rename behaviour
5668 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5673 * If this is a plain open for existing files, opening an 0
5674 * byte size resource fork MUST fail with
5675 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5677 * Cf the vfs_fruit torture tests in test_rfork_create().
5679 if (is_afpresource_stream(fsp->fsp_name) &&
5680 create_disposition == FILE_OPEN)
5682 if (fsp->fsp_name->st.st_ex_size == 0) {
5683 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5688 if (is_ntfs_stream_smb_fname(smb_fname)
5689 || fsp->is_directory) {
5693 if (config->locking == FRUIT_LOCKING_NETATALK) {
5694 status = fruit_check_access(
5697 map_share_mode_to_deny_mode(share_access, 0));
5698 if (!NT_STATUS_IS_OK(status)) {
5706 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5709 close_file(req, fsp, ERROR_CLOSE);
5710 *result = fsp = NULL;
5716 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5717 const struct smb_filename *fname,
5718 TALLOC_CTX *mem_ctx,
5719 struct readdir_attr_data **pattr_data)
5721 struct fruit_config_data *config = NULL;
5722 struct readdir_attr_data *attr_data;
5725 SMB_VFS_HANDLE_GET_DATA(handle, config,
5726 struct fruit_config_data,
5727 return NT_STATUS_UNSUCCESSFUL);
5729 if (!global_fruit_config.nego_aapl) {
5730 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5733 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5735 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5736 if (*pattr_data == NULL) {
5737 return NT_STATUS_UNSUCCESSFUL;
5739 attr_data = *pattr_data;
5740 attr_data->type = RDATTR_AAPL;
5743 * Mac metadata: compressed FinderInfo, resource fork length
5746 status = readdir_attr_macmeta(handle, fname, attr_data);
5747 if (!NT_STATUS_IS_OK(status)) {
5749 * Error handling is tricky: if we return failure from
5750 * this function, the corresponding directory entry
5751 * will to be passed to the client, so we really just
5752 * want to error out on fatal errors.
5754 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5762 if (config->unix_info_enabled) {
5763 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5769 if (!config->readdir_attr_max_access) {
5770 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5772 status = smbd_calculate_access_mask(
5776 SEC_FLAG_MAXIMUM_ALLOWED,
5777 &attr_data->attr_data.aapl.max_access);
5778 if (!NT_STATUS_IS_OK(status)) {
5783 return NT_STATUS_OK;
5786 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5787 fname->base_name, nt_errstr(status)));
5788 TALLOC_FREE(*pattr_data);
5792 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5794 uint32_t security_info,
5795 TALLOC_CTX *mem_ctx,
5796 struct security_descriptor **ppdesc)
5799 struct security_ace ace;
5801 struct fruit_config_data *config;
5803 SMB_VFS_HANDLE_GET_DATA(handle, config,
5804 struct fruit_config_data,
5805 return NT_STATUS_UNSUCCESSFUL);
5807 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5809 if (!NT_STATUS_IS_OK(status)) {
5814 * Add MS NFS style ACEs with uid, gid and mode
5816 if (!global_fruit_config.nego_aapl) {
5817 return NT_STATUS_OK;
5819 if (!config->unix_info_enabled) {
5820 return NT_STATUS_OK;
5823 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5824 status = remove_virtual_nfs_aces(*ppdesc);
5825 if (!NT_STATUS_IS_OK(status)) {
5826 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5830 /* MS NFS style mode */
5831 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5832 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5833 status = security_descriptor_dacl_add(*ppdesc, &ace);
5834 if (!NT_STATUS_IS_OK(status)) {
5835 DEBUG(1,("failed to add MS NFS style ACE\n"));
5839 /* MS NFS style uid */
5840 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5841 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5842 status = security_descriptor_dacl_add(*ppdesc, &ace);
5843 if (!NT_STATUS_IS_OK(status)) {
5844 DEBUG(1,("failed to add MS NFS style ACE\n"));
5848 /* MS NFS style gid */
5849 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5850 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5851 status = security_descriptor_dacl_add(*ppdesc, &ace);
5852 if (!NT_STATUS_IS_OK(status)) {
5853 DEBUG(1,("failed to add MS NFS style ACE\n"));
5857 return NT_STATUS_OK;
5860 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5862 uint32_t security_info_sent,
5863 const struct security_descriptor *orig_psd)
5867 mode_t ms_nfs_mode = 0;
5869 struct security_descriptor *psd = NULL;
5870 uint32_t orig_num_aces = 0;
5872 if (orig_psd->dacl != NULL) {
5873 orig_num_aces = orig_psd->dacl->num_aces;
5876 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5878 return NT_STATUS_NO_MEMORY;
5881 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5883 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5884 if (!NT_STATUS_IS_OK(status)) {
5885 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5891 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5892 * sent/present flags correctly now we've removed them.
5895 if (orig_num_aces != 0) {
5897 * Are there any ACE's left ?
5899 if (psd->dacl->num_aces == 0) {
5900 /* No - clear the DACL sent/present flags. */
5901 security_info_sent &= ~SECINFO_DACL;
5902 psd->type &= ~SEC_DESC_DACL_PRESENT;
5906 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5907 if (!NT_STATUS_IS_OK(status)) {
5908 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5914 if (fsp->fh->fd != -1) {
5915 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5917 result = SMB_VFS_CHMOD(fsp->conn,
5923 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5924 result, (unsigned)ms_nfs_mode,
5926 status = map_nt_error_from_unix(errno);
5933 return NT_STATUS_OK;
5936 static struct vfs_offload_ctx *fruit_offload_ctx;
5938 struct fruit_offload_read_state {
5939 struct vfs_handle_struct *handle;
5940 struct tevent_context *ev;
5946 static void fruit_offload_read_done(struct tevent_req *subreq);
5948 static struct tevent_req *fruit_offload_read_send(
5949 TALLOC_CTX *mem_ctx,
5950 struct tevent_context *ev,
5951 struct vfs_handle_struct *handle,
5958 struct tevent_req *req = NULL;
5959 struct tevent_req *subreq = NULL;
5960 struct fruit_offload_read_state *state = NULL;
5962 req = tevent_req_create(mem_ctx, &state,
5963 struct fruit_offload_read_state);
5967 *state = (struct fruit_offload_read_state) {
5974 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5975 fsctl, ttl, offset, to_copy);
5976 if (tevent_req_nomem(subreq, req)) {
5977 return tevent_req_post(req, ev);
5979 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5983 static void fruit_offload_read_done(struct tevent_req *subreq)
5985 struct tevent_req *req = tevent_req_callback_data(
5986 subreq, struct tevent_req);
5987 struct fruit_offload_read_state *state = tevent_req_data(
5988 req, struct fruit_offload_read_state);
5991 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5995 TALLOC_FREE(subreq);
5996 if (tevent_req_nterror(req, status)) {
6000 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
6001 tevent_req_done(req);
6005 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
6006 &fruit_offload_ctx);
6007 if (tevent_req_nterror(req, status)) {
6011 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
6014 if (tevent_req_nterror(req, status)) {
6018 tevent_req_done(req);
6022 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6023 struct vfs_handle_struct *handle,
6024 TALLOC_CTX *mem_ctx,
6027 struct fruit_offload_read_state *state = tevent_req_data(
6028 req, struct fruit_offload_read_state);
6031 if (tevent_req_is_nterror(req, &status)) {
6032 tevent_req_received(req);
6036 token->length = state->token.length;
6037 token->data = talloc_move(mem_ctx, &state->token.data);
6039 tevent_req_received(req);
6040 return NT_STATUS_OK;
6043 struct fruit_offload_write_state {
6044 struct vfs_handle_struct *handle;
6046 struct files_struct *src_fsp;
6047 struct files_struct *dst_fsp;
6051 static void fruit_offload_write_done(struct tevent_req *subreq);
6052 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6053 TALLOC_CTX *mem_ctx,
6054 struct tevent_context *ev,
6057 off_t transfer_offset,
6058 struct files_struct *dest_fsp,
6062 struct tevent_req *req, *subreq;
6063 struct fruit_offload_write_state *state;
6065 struct fruit_config_data *config;
6066 off_t src_off = transfer_offset;
6067 files_struct *src_fsp = NULL;
6068 off_t to_copy = num;
6069 bool copyfile_enabled = false;
6071 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6072 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6074 SMB_VFS_HANDLE_GET_DATA(handle, config,
6075 struct fruit_config_data,
6078 req = tevent_req_create(mem_ctx, &state,
6079 struct fruit_offload_write_state);
6083 state->handle = handle;
6084 state->dst_fsp = dest_fsp;
6087 case FSCTL_SRV_COPYCHUNK:
6088 case FSCTL_SRV_COPYCHUNK_WRITE:
6089 copyfile_enabled = config->copyfile_enabled;
6096 * Check if this a OS X copyfile style copychunk request with
6097 * a requested chunk count of 0 that was translated to a
6098 * offload_write_send VFS call overloading the parameters src_off
6099 * = dest_off = num = 0.
6101 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6102 status = vfs_offload_token_db_fetch_fsp(
6103 fruit_offload_ctx, token, &src_fsp);
6104 if (tevent_req_nterror(req, status)) {
6105 return tevent_req_post(req, ev);
6107 state->src_fsp = src_fsp;
6109 status = vfs_stat_fsp(src_fsp);
6110 if (tevent_req_nterror(req, status)) {
6111 return tevent_req_post(req, ev);
6114 to_copy = src_fsp->fsp_name->st.st_ex_size;
6115 state->is_copyfile = true;
6118 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6127 if (tevent_req_nomem(subreq, req)) {
6128 return tevent_req_post(req, ev);
6131 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6135 static void fruit_offload_write_done(struct tevent_req *subreq)
6137 struct tevent_req *req = tevent_req_callback_data(
6138 subreq, struct tevent_req);
6139 struct fruit_offload_write_state *state = tevent_req_data(
6140 req, struct fruit_offload_write_state);
6142 unsigned int num_streams = 0;
6143 struct stream_struct *streams = NULL;
6145 struct smb_filename *src_fname_tmp = NULL;
6146 struct smb_filename *dst_fname_tmp = NULL;
6148 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6151 TALLOC_FREE(subreq);
6152 if (tevent_req_nterror(req, status)) {
6156 if (!state->is_copyfile) {
6157 tevent_req_done(req);
6162 * Now copy all remaining streams. We know the share supports
6163 * streams, because we're in vfs_fruit. We don't do this async
6164 * because streams are few and small.
6166 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6167 state->src_fsp->fsp_name,
6168 req, &num_streams, &streams);
6169 if (tevent_req_nterror(req, status)) {
6173 if (num_streams == 1) {
6174 /* There is always one stream, ::$DATA. */
6175 tevent_req_done(req);
6179 for (i = 0; i < num_streams; i++) {
6180 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6181 __func__, streams[i].name, (size_t)streams[i].size));
6183 src_fname_tmp = synthetic_smb_fname(
6185 state->src_fsp->fsp_name->base_name,
6188 state->src_fsp->fsp_name->flags);
6189 if (tevent_req_nomem(src_fname_tmp, req)) {
6193 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6194 TALLOC_FREE(src_fname_tmp);
6198 dst_fname_tmp = synthetic_smb_fname(
6200 state->dst_fsp->fsp_name->base_name,
6203 state->dst_fsp->fsp_name->flags);
6204 if (tevent_req_nomem(dst_fname_tmp, req)) {
6205 TALLOC_FREE(src_fname_tmp);
6209 status = copy_file(req,
6210 state->handle->conn,
6213 OPENX_FILE_CREATE_IF_NOT_EXIST,
6215 if (!NT_STATUS_IS_OK(status)) {
6216 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6217 smb_fname_str_dbg(src_fname_tmp),
6218 smb_fname_str_dbg(dst_fname_tmp),
6219 nt_errstr(status)));
6220 TALLOC_FREE(src_fname_tmp);
6221 TALLOC_FREE(dst_fname_tmp);
6222 tevent_req_nterror(req, status);
6226 TALLOC_FREE(src_fname_tmp);
6227 TALLOC_FREE(dst_fname_tmp);
6230 TALLOC_FREE(streams);
6231 TALLOC_FREE(src_fname_tmp);
6232 TALLOC_FREE(dst_fname_tmp);
6233 tevent_req_done(req);
6236 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6237 struct tevent_req *req,
6240 struct fruit_offload_write_state *state = tevent_req_data(
6241 req, struct fruit_offload_write_state);
6244 if (tevent_req_is_nterror(req, &status)) {
6245 DEBUG(1, ("server side copy chunk failed: %s\n",
6246 nt_errstr(status)));
6248 tevent_req_received(req);
6252 *copied = state->copied;
6253 tevent_req_received(req);
6255 return NT_STATUS_OK;
6258 static char *fruit_get_bandsize_line(char **lines, int numlines)
6261 static bool re_initialized = false;
6265 if (!re_initialized) {
6266 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6270 re_initialized = true;
6273 for (i = 0; i < numlines; i++) {
6274 regmatch_t matches[1];
6276 ret = regexec(&re, lines[i], 1, matches, 0);
6279 * Check if the match was on the last line, sa we want
6280 * the subsequent line.
6282 if (i + 1 == numlines) {
6285 return lines[i + 1];
6287 if (ret != REG_NOMATCH) {
6295 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6298 static bool re_initialized = false;
6299 regmatch_t matches[2];
6304 if (!re_initialized) {
6307 "<integer>\\([[:digit:]]*\\)</integer>$",
6312 re_initialized = true;
6315 ret = regexec(&re, line, 2, matches, 0);
6317 DBG_ERR("regex failed [%s]\n", line);
6321 line[matches[1].rm_eo] = '\0';
6323 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6327 *_band_size = (size_t)band_size;
6332 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6333 * "band-size" key and value.
6335 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6339 #define INFO_PLIST_MAX_SIZE 64*1024
6341 struct smb_filename *smb_fname = NULL;
6342 files_struct *fsp = NULL;
6343 uint8_t *file_data = NULL;
6344 char **lines = NULL;
6345 char *band_size_line = NULL;
6346 size_t plist_file_size;
6353 plist = talloc_asprintf(talloc_tos(),
6355 handle->conn->connectpath,
6357 if (plist == NULL) {
6362 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6363 if (smb_fname == NULL) {
6368 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6370 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6375 plist_file_size = smb_fname->st.st_ex_size;
6377 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6378 DBG_INFO("%s is too large, ignoring\n", plist);
6383 status = SMB_VFS_NEXT_CREATE_FILE(
6386 0, /* root_dir_fid */
6387 smb_fname, /* fname */
6388 FILE_GENERIC_READ, /* access_mask */
6389 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6390 FILE_OPEN, /* create_disposition */
6391 0, /* create_options */
6392 0, /* file_attributes */
6393 INTERNAL_OPEN_ONLY, /* oplock_request */
6395 0, /* allocation_size */
6396 0, /* private_flags */
6401 NULL, NULL); /* create context */
6402 if (!NT_STATUS_IS_OK(status)) {
6403 DBG_INFO("Opening [%s] failed [%s]\n",
6404 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6409 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6410 if (file_data == NULL) {
6415 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6416 if (nread != plist_file_size) {
6417 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6418 fsp_str_dbg(fsp), nread, plist_file_size);
6424 status = close_file(NULL, fsp, NORMAL_CLOSE);
6426 if (!NT_STATUS_IS_OK(status)) {
6427 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6432 lines = file_lines_parse((char *)file_data,
6436 if (lines == NULL) {
6441 band_size_line = fruit_get_bandsize_line(lines, numlines);
6442 if (band_size_line == NULL) {
6443 DBG_ERR("Didn't find band-size key in [%s]\n",
6444 smb_fname_str_dbg(smb_fname));
6449 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6451 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6455 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6459 status = close_file(NULL, fsp, NORMAL_CLOSE);
6460 if (!NT_STATUS_IS_OK(status)) {
6461 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6466 TALLOC_FREE(smb_fname);
6467 TALLOC_FREE(file_data);
6472 struct fruit_disk_free_state {
6476 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6481 struct smb_filename *bands_dir = NULL;
6483 struct dirent *e = NULL;
6487 path = talloc_asprintf(talloc_tos(),
6489 handle->conn->connectpath,
6495 bands_dir = synthetic_smb_fname(talloc_tos(),
6501 if (bands_dir == NULL) {
6505 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6507 TALLOC_FREE(bands_dir);
6513 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6515 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6517 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6523 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6525 TALLOC_FREE(bands_dir);
6529 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6531 TALLOC_FREE(bands_dir);
6537 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6538 struct fruit_disk_free_state *state,
6543 size_t sparsebundle_strlen = strlen("sparsebundle");
6544 size_t bandsize = 0;
6548 p = strstr(e->d_name, "sparsebundle");
6553 if (p[sparsebundle_strlen] != '\0') {
6557 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6559 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6562 * Beware of race conditions: this may be an uninitialized
6563 * Info.plist that a client is just creating. We don't want let
6564 * this to trigger complete failure.
6566 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6570 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6573 * Beware of race conditions: this may be a backup sparsebundle
6574 * in an early stage lacking a bands subdirectory. We don't want
6575 * let this to trigger complete failure.
6577 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6581 if (bandsize > SIZE_MAX/nbands) {
6582 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6586 tm_size = bandsize * nbands;
6588 if (state->total_size + tm_size < state->total_size) {
6589 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6594 state->total_size += tm_size;
6596 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6597 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6603 * Calculate used size of a TimeMachine volume
6605 * This assumes that the volume is used only for TimeMachine.
6607 * - readdir(basedir of share), then
6608 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6609 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6610 * - count band files in "\1.sparsebundle/bands/"
6611 * - calculate used size of all bands: band_count * band_size
6613 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6614 const struct smb_filename *smb_fname,
6619 struct fruit_config_data *config = NULL;
6620 struct fruit_disk_free_state state = {0};
6622 struct dirent *e = NULL;
6628 SMB_VFS_HANDLE_GET_DATA(handle, config,
6629 struct fruit_config_data,
6632 if (!config->time_machine ||
6633 config->time_machine_max_size == 0)
6635 return SMB_VFS_NEXT_DISK_FREE(handle,
6642 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6647 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6649 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6651 ok = fruit_tmsize_do_dirent(handle, &state, e);
6653 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6658 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6663 dsize = config->time_machine_max_size / 512;
6664 dfree = dsize - (state.total_size / 512);
6665 if (dfree > dsize) {
6675 static struct vfs_fn_pointers vfs_fruit_fns = {
6676 .connect_fn = fruit_connect,
6677 .disk_free_fn = fruit_disk_free,
6679 /* File operations */
6680 .chmod_fn = fruit_chmod,
6681 .chown_fn = fruit_chown,
6682 .unlink_fn = fruit_unlink,
6683 .rename_fn = fruit_rename,
6684 .rmdir_fn = fruit_rmdir,
6685 .open_fn = fruit_open,
6686 .pread_fn = fruit_pread,
6687 .pwrite_fn = fruit_pwrite,
6688 .pread_send_fn = fruit_pread_send,
6689 .pread_recv_fn = fruit_pread_recv,
6690 .pwrite_send_fn = fruit_pwrite_send,
6691 .pwrite_recv_fn = fruit_pwrite_recv,
6692 .stat_fn = fruit_stat,
6693 .lstat_fn = fruit_lstat,
6694 .fstat_fn = fruit_fstat,
6695 .streaminfo_fn = fruit_streaminfo,
6696 .ntimes_fn = fruit_ntimes,
6697 .ftruncate_fn = fruit_ftruncate,
6698 .fallocate_fn = fruit_fallocate,
6699 .create_file_fn = fruit_create_file,
6700 .readdir_attr_fn = fruit_readdir_attr,
6701 .offload_read_send_fn = fruit_offload_read_send,
6702 .offload_read_recv_fn = fruit_offload_read_recv,
6703 .offload_write_send_fn = fruit_offload_write_send,
6704 .offload_write_recv_fn = fruit_offload_write_recv,
6706 /* NT ACL operations */
6707 .fget_nt_acl_fn = fruit_fget_nt_acl,
6708 .fset_nt_acl_fn = fruit_fset_nt_acl,
6712 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6714 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6716 if (!NT_STATUS_IS_OK(ret)) {
6720 vfs_fruit_debug_level = debug_add_class("fruit");
6721 if (vfs_fruit_debug_level == -1) {
6722 vfs_fruit_debug_level = DBGC_VFS;
6723 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6726 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6727 "vfs_fruit_init","fruit",vfs_fruit_debug_level));