2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
98 static int vfs_fruit_debug_level = DBGC_VFS;
100 static struct global_fruit_config {
101 bool nego_aapl; /* client negotiated AAPL */
103 } global_fruit_config;
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
122 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
124 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
125 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
126 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
127 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
129 struct fruit_config_data {
130 enum fruit_rsrc rsrc;
131 enum fruit_meta meta;
132 enum fruit_locking locking;
133 enum fruit_encoding encoding;
134 bool use_aapl; /* config from smb.conf */
136 bool readdir_attr_enabled;
137 bool unix_info_enabled;
138 bool copyfile_enabled;
139 bool veto_appledouble;
141 bool aapl_zero_file_id;
144 off_t time_machine_max_size;
147 * Additional options, all enabled by default,
148 * possibly useful for analyzing performance. The associated
149 * operations with each of them may be expensive, so having
150 * the chance to disable them individually gives a chance
151 * tweaking the setup for the particular usecase.
153 bool readdir_attr_rsize;
154 bool readdir_attr_finder_info;
155 bool readdir_attr_max_access;
158 static const struct enum_list fruit_rsrc[] = {
159 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
160 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
161 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
165 static const struct enum_list fruit_meta[] = {
166 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
167 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
171 static const struct enum_list fruit_locking[] = {
172 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
173 {FRUIT_LOCKING_NONE, "none"},
177 static const struct enum_list fruit_encoding[] = {
178 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
179 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
183 static const char *fruit_catia_maps =
184 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
185 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
186 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
187 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
188 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
189 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
190 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
191 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
192 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
193 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
196 /*****************************************************************************
197 * Defines, functions and data structures that deal with AppleDouble
198 *****************************************************************************/
201 * There are two AppleDouble blobs we deal with:
203 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
204 * metadata in an xattr
206 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
209 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
212 #define AD_VERSION2 0x00020000
213 #define AD_VERSION AD_VERSION2
216 * AppleDouble entry IDs.
218 #define ADEID_DFORK 1
219 #define ADEID_RFORK 2
221 #define ADEID_COMMENT 4
222 #define ADEID_ICONBW 5
223 #define ADEID_ICONCOL 6
224 #define ADEID_FILEI 7
225 #define ADEID_FILEDATESI 8
226 #define ADEID_FINDERI 9
227 #define ADEID_MACFILEI 10
228 #define ADEID_PRODOSFILEI 11
229 #define ADEID_MSDOSFILEI 12
230 #define ADEID_SHORTNAME 13
231 #define ADEID_AFPFILEI 14
234 /* Private Netatalk entries */
235 #define ADEID_PRIVDEV 16
236 #define ADEID_PRIVINO 17
237 #define ADEID_PRIVSYN 18
238 #define ADEID_PRIVID 19
239 #define ADEID_MAX (ADEID_PRIVID + 1)
242 * These are the real ids for the private entries,
243 * as stored in the adouble file
245 #define AD_DEV 0x80444556
246 #define AD_INO 0x80494E4F
247 #define AD_SYN 0x8053594E
248 #define AD_ID 0x8053567E
250 /* Number of actually used entries */
251 #define ADEID_NUM_XATTR 8
252 #define ADEID_NUM_DOT_UND 2
253 #define ADEID_NUM_RSRC_XATTR 1
255 /* AppleDouble magic */
256 #define AD_APPLESINGLE_MAGIC 0x00051600
257 #define AD_APPLEDOUBLE_MAGIC 0x00051607
258 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
260 /* Sizes of relevant entry bits */
261 #define ADEDLEN_MAGIC 4
262 #define ADEDLEN_VERSION 4
263 #define ADEDLEN_FILLER 16
264 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
265 #define AD_FILLER_TAG_OSX "Mac OS X " /* should be 16 bytes */
266 #define ADEDLEN_NENTRIES 2
267 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
268 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
269 #define AD_ENTRY_LEN_EID 4
270 #define AD_ENTRY_LEN_OFF 4
271 #define AD_ENTRY_LEN_LEN 4
272 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
275 #define ADEDLEN_NAME 255
276 #define ADEDLEN_COMMENT 200
277 #define ADEDLEN_FILEI 16
278 #define ADEDLEN_FINDERI 32
279 #define ADEDLEN_FILEDATESI 16
280 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
281 #define ADEDLEN_AFPFILEI 4
282 #define ADEDLEN_MACFILEI 4
283 #define ADEDLEN_PRODOSFILEI 8
284 #define ADEDLEN_MSDOSFILEI 2
285 #define ADEDLEN_DID 4
286 #define ADEDLEN_PRIVDEV 8
287 #define ADEDLEN_PRIVINO 8
288 #define ADEDLEN_PRIVSYN 8
289 #define ADEDLEN_PRIVID 4
292 #define ADEDOFF_MAGIC 0
293 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
294 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
295 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
297 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
298 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
299 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
300 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
301 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
303 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
304 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
305 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
306 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
308 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
309 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
310 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
312 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
313 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
314 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
315 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
316 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
317 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
319 #if AD_DATASZ_XATTR != 402
320 #error bad size for AD_DATASZ_XATTR
323 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
324 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
326 #if AD_DATASZ_DOT_UND != 82
327 #error bad size for AD_DATASZ_DOT_UND
331 * Sharemode locks fcntl() offsets
333 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
334 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
336 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
338 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
340 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
341 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
342 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
343 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
344 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
345 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
346 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
347 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
348 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
349 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
351 /* Time stuff we overload the bits a little */
352 #define AD_DATE_CREATE 0
353 #define AD_DATE_MODIFY 4
354 #define AD_DATE_BACKUP 8
355 #define AD_DATE_ACCESS 12
356 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
357 AD_DATE_BACKUP | AD_DATE_ACCESS)
358 #define AD_DATE_UNIX (1 << 10)
359 #define AD_DATE_START 0x80000000
360 #define AD_DATE_DELTA 946684800
361 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
362 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
364 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
365 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
366 #define AD_XATTR_HDR_SIZE 36
367 #define AD_XATTR_MAX_HDR_SIZE 65536
369 /* Accessor macros */
370 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
371 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
372 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
373 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
376 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
377 * representation as well as the on-disk format.
379 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
380 * the length of the FinderInfo entry is larger then 32 bytes. It is then
381 * preceeded with 2 bytes padding.
383 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
386 struct ad_xattr_header {
387 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
388 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
389 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
390 uint32_t adx_data_start; /* file offset to attribute data area */
391 uint32_t adx_data_length; /* length of attribute data area */
392 uint32_t adx_reserved[3];
394 uint16_t adx_num_attrs;
397 /* On-disk entries are aligned on 4 byte boundaries */
398 struct ad_xattr_entry {
399 uint32_t adx_offset; /* file offset to data */
400 uint32_t adx_length; /* size of attribute data */
402 uint8_t adx_namelen; /* included the NULL terminator */
403 char *adx_name; /* NULL-terminated UTF-8 name */
412 vfs_handle_struct *ad_handle;
415 adouble_type_t ad_type;
418 uint8_t ad_filler[ADEDLEN_FILLER];
419 struct ad_entry ad_eid[ADEID_MAX];
421 struct ad_xattr_header adx_header;
422 struct ad_xattr_entry *adx_entries;
425 struct ad_entry_order {
426 uint32_t id, offset, len;
429 /* Netatalk AppleDouble metadata xattr */
431 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
432 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
433 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
434 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
435 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
436 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
437 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
438 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
439 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
443 /* AppleDouble resource fork file (the ones prefixed by "._") */
445 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
446 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
447 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
452 * Fake AppleDouble entry oder for resource fork xattr. The xattr
453 * isn't an AppleDouble file, it simply contains the resource data,
454 * but in order to be able to use some API calls like ad_getentryoff()
455 * we build a fake/helper struct adouble with this entry order struct.
458 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
463 /* Conversion from enumerated id to on-disk AppleDouble id */
464 #define AD_EID_DISK(a) (set_eid[a])
465 static const uint32_t set_eid[] = {
466 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
467 AD_DEV, AD_INO, AD_SYN, AD_ID
471 /* tcon config handle */
472 struct fruit_config_data *config;
474 /* Denote stream type, meta or rsrc */
479 * Forward declarations
481 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
482 adouble_type_t type);
483 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
484 static int ad_fset(struct adouble *ad, files_struct *fsp);
485 static int adouble_path(TALLOC_CTX *ctx,
486 const struct smb_filename *smb_fname__in,
487 struct smb_filename **ppsmb_fname_out);
488 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
489 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
490 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
494 * Return a pointer to an AppleDouble entry
496 * Returns NULL if the entry is not present
498 static char *ad_get_entry(const struct adouble *ad, int eid)
500 off_t off = ad_getentryoff(ad, eid);
501 size_t len = ad_getentrylen(ad, eid);
503 if (off == 0 || len == 0) {
507 return ad->ad_data + off;
513 static int ad_getdate(const struct adouble *ad,
514 unsigned int dateoff,
517 bool xlate = (dateoff & AD_DATE_UNIX);
520 dateoff &= AD_DATE_MASK;
521 p = ad_get_entry(ad, ADEID_FILEDATESI);
526 if (dateoff > AD_DATE_ACCESS) {
530 memcpy(date, p + dateoff, sizeof(uint32_t));
533 *date = AD_DATE_TO_UNIX(*date);
541 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
543 bool xlate = (dateoff & AD_DATE_UNIX);
546 p = ad_get_entry(ad, ADEID_FILEDATESI);
551 dateoff &= AD_DATE_MASK;
553 date = AD_DATE_FROM_UNIX(date);
556 if (dateoff > AD_DATE_ACCESS) {
560 memcpy(p + dateoff, &date, sizeof(date));
567 * Map on-disk AppleDouble id to enumerated id
569 static uint32_t get_eid(uint32_t eid)
577 return ADEID_PRIVDEV;
579 return ADEID_PRIVINO;
581 return ADEID_PRIVSYN;
592 * Pack AppleDouble structure into data buffer
594 static bool ad_pack(struct adouble *ad)
601 bufsize = talloc_get_size(ad->ad_data);
602 if (bufsize < AD_DATASZ_DOT_UND) {
603 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
607 if (offset + ADEDLEN_MAGIC < offset ||
608 offset + ADEDLEN_MAGIC >= bufsize) {
611 RSIVAL(ad->ad_data, offset, ad->ad_magic);
612 offset += ADEDLEN_MAGIC;
614 if (offset + ADEDLEN_VERSION < offset ||
615 offset + ADEDLEN_VERSION >= bufsize) {
618 RSIVAL(ad->ad_data, offset, ad->ad_version);
619 offset += ADEDLEN_VERSION;
621 if (offset + ADEDLEN_FILLER < offset ||
622 offset + ADEDLEN_FILLER >= bufsize) {
625 if (ad->ad_type == ADOUBLE_RSRC) {
626 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
628 offset += ADEDLEN_FILLER;
630 if (offset + ADEDLEN_NENTRIES < offset ||
631 offset + ADEDLEN_NENTRIES >= bufsize) {
634 offset += ADEDLEN_NENTRIES;
636 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
637 if (ad->ad_eid[eid].ade_off == 0) {
639 * ade_off is also used as indicator whether a
640 * specific entry is used or not
645 if (offset + AD_ENTRY_LEN_EID < offset ||
646 offset + AD_ENTRY_LEN_EID >= bufsize) {
649 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
650 offset += AD_ENTRY_LEN_EID;
652 if (offset + AD_ENTRY_LEN_OFF < offset ||
653 offset + AD_ENTRY_LEN_OFF >= bufsize) {
656 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
657 offset += AD_ENTRY_LEN_OFF;
659 if (offset + AD_ENTRY_LEN_LEN < offset ||
660 offset + AD_ENTRY_LEN_LEN >= bufsize) {
663 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
664 offset += AD_ENTRY_LEN_LEN;
669 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
672 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
677 static bool ad_unpack_xattrs(struct adouble *ad)
679 struct ad_xattr_header *h = &ad->adx_header;
680 const char *p = ad->ad_data;
684 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
688 /* 2 bytes padding */
689 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
691 h->adx_magic = RIVAL(p, hoff + 0);
692 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
693 h->adx_total_size = RIVAL(p, hoff + 8);
694 h->adx_data_start = RIVAL(p, hoff + 12);
695 h->adx_data_length = RIVAL(p, hoff + 16);
696 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
697 h->adx_num_attrs = RSVAL(p, hoff + 34);
699 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
700 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
704 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
705 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
708 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
709 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
713 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
714 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
718 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
719 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
722 if ((h->adx_data_start + h->adx_data_length) >
723 ad->adx_header.adx_total_size)
725 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
729 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
730 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
734 if (h->adx_num_attrs == 0) {
738 ad->adx_entries = talloc_zero_array(
739 ad, struct ad_xattr_entry, h->adx_num_attrs);
740 if (ad->adx_entries == NULL) {
744 hoff += AD_XATTR_HDR_SIZE;
746 for (i = 0; i < h->adx_num_attrs; i++) {
747 struct ad_xattr_entry *e = &ad->adx_entries[i];
749 hoff = (hoff + 3) & ~3;
751 e->adx_offset = RIVAL(p, hoff + 0);
752 e->adx_length = RIVAL(p, hoff + 4);
753 e->adx_flags = RSVAL(p, hoff + 8);
754 e->adx_namelen = *(p + hoff + 10);
756 if (e->adx_offset >= ad->adx_header.adx_total_size) {
757 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
762 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
763 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
768 if ((e->adx_offset + e->adx_length) >
769 ad->adx_header.adx_total_size)
771 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
776 if (e->adx_namelen == 0) {
777 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
781 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
782 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
786 if ((hoff + 11 + e->adx_namelen) >
787 ad->adx_header.adx_data_start)
789 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
794 e->adx_name = talloc_strndup(ad->adx_entries,
797 if (e->adx_name == NULL) {
801 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
802 e->adx_name, e->adx_offset, e->adx_length);
803 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
806 hoff += 11 + e->adx_namelen;
813 * Unpack an AppleDouble blob into a struct adoble
815 static bool ad_unpack(struct adouble *ad, const size_t nentries,
818 size_t bufsize = talloc_get_size(ad->ad_data);
820 uint32_t eid, len, off;
824 * The size of the buffer ad->ad_data is checked when read, so
825 * we wouldn't have to check our own offsets, a few extra
826 * checks won't hurt though. We have to check the offsets we
827 * read from the buffer anyway.
830 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
831 DEBUG(1, ("bad size\n"));
835 ad->ad_magic = RIVAL(ad->ad_data, 0);
836 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
837 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
838 DEBUG(1, ("wrong magic or version\n"));
842 memcpy(ad->ad_filler, ad->ad_data + ADEDOFF_FILLER, ADEDLEN_FILLER);
844 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
845 if (adentries != nentries) {
846 DEBUG(1, ("invalid number of entries: %zu\n",
851 /* now, read in the entry bits */
852 for (i = 0; i < adentries; i++) {
853 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
855 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
856 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
858 if (!eid || eid >= ADEID_MAX) {
859 DEBUG(1, ("bogus eid %d\n", eid));
864 * All entries other than the resource fork are
865 * expected to be read into the ad_data buffer, so
866 * ensure the specified offset is within that bound
868 if ((off > bufsize) && (eid != ADEID_RFORK)) {
869 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
875 * All entries besides FinderInfo and resource fork
876 * must fit into the buffer. FinderInfo is special as
877 * it may be larger then the default 32 bytes (if it
878 * contains marshalled xattrs), but we will fixup that
879 * in ad_convert(). And the resource fork is never
880 * accessed directly by the ad_data buf (also see
881 * comment above) anyway.
883 if ((eid != ADEID_RFORK) &&
884 (eid != ADEID_FINDERI) &&
885 ((off + len) > bufsize)) {
886 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
892 * That would be obviously broken
894 if (off > filesize) {
895 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
901 * Check for any entry that has its end beyond the
904 if (off + len < off) {
905 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
906 ", len: %" PRIu32 "\n",
911 if (off + len > filesize) {
913 * If this is the resource fork entry, we fix
914 * up the length, for any other entry we bail
917 if (eid != ADEID_RFORK) {
918 DEBUG(1, ("bogus eid %d: off: %" PRIu32
919 ", len: %" PRIu32 "\n",
925 * Fixup the resource fork entry by limiting
926 * the size to entryoffset - filesize.
928 len = filesize - off;
929 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
930 ", len: %" PRIu32 "\n", off, len));
933 ad->ad_eid[eid].ade_off = off;
934 ad->ad_eid[eid].ade_len = len;
937 ok = ad_unpack_xattrs(ad);
945 static bool ad_convert_xattr(struct adouble *ad,
946 const struct smb_filename *smb_fname,
949 static struct char_mappings **string_replace_cmaps = NULL;
954 if (ad->adx_header.adx_num_attrs == 0) {
958 if (string_replace_cmaps == NULL) {
959 const char **mappings = NULL;
961 mappings = str_list_make_v3_const(
962 talloc_tos(), fruit_catia_maps, NULL);
963 if (mappings == NULL) {
966 string_replace_cmaps = string_replace_init_map(mappings);
967 TALLOC_FREE(mappings);
970 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
971 struct ad_xattr_entry *e = &ad->adx_entries[i];
972 char *mapped_name = NULL;
974 struct smb_filename *stream_name = NULL;
975 files_struct *fsp = NULL;
978 status = string_replace_allocate(ad->ad_handle->conn,
980 string_replace_cmaps,
983 vfs_translate_to_windows);
984 if (!NT_STATUS_IS_OK(status) &&
985 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
987 DBG_ERR("string_replace_allocate failed\n");
992 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
994 if (mapped_name == NULL) {
998 stream_name = synthetic_smb_fname(talloc_tos(),
999 smb_fname->base_name,
1003 TALLOC_FREE(mapped_name);
1004 if (stream_name == NULL) {
1005 DBG_ERR("synthetic_smb_fname failed\n");
1009 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1011 status = SMB_VFS_CREATE_FILE(
1012 ad->ad_handle->conn, /* conn */
1014 0, /* root_dir_fid */
1015 stream_name, /* fname */
1016 FILE_GENERIC_WRITE, /* access_mask */
1017 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1018 FILE_OPEN_IF, /* create_disposition */
1019 0, /* create_options */
1020 0, /* file_attributes */
1021 INTERNAL_OPEN_ONLY, /* oplock_request */
1023 0, /* allocation_size */
1024 0, /* private_flags */
1029 NULL, NULL); /* create context */
1030 TALLOC_FREE(stream_name);
1031 if (!NT_STATUS_IS_OK(status)) {
1032 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1036 nwritten = SMB_VFS_PWRITE(fsp,
1037 map + e->adx_offset,
1040 if (nwritten == -1) {
1041 DBG_ERR("SMB_VFS_PWRITE failed\n");
1042 saved_errno = errno;
1043 close_file(NULL, fsp, ERROR_CLOSE);
1044 errno = saved_errno;
1048 status = close_file(NULL, fsp, NORMAL_CLOSE);
1049 if (!NT_STATUS_IS_OK(status)) {
1055 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1059 static bool ad_convert_finderinfo(struct adouble *ad,
1060 const struct smb_filename *smb_fname)
1065 struct smb_filename *stream_name = NULL;
1066 files_struct *fsp = NULL;
1070 int saved_errno = 0;
1072 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1077 ai = afpinfo_new(talloc_tos());
1082 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1084 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1085 if (aiblob.data == NULL) {
1090 size = afpinfo_pack(ai, (char *)aiblob.data);
1092 if (size != AFP_INFO_SIZE) {
1096 stream_name = synthetic_smb_fname(talloc_tos(),
1097 smb_fname->base_name,
1101 if (stream_name == NULL) {
1102 data_blob_free(&aiblob);
1103 DBG_ERR("synthetic_smb_fname failed\n");
1107 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1109 status = SMB_VFS_CREATE_FILE(
1110 ad->ad_handle->conn, /* conn */
1112 0, /* root_dir_fid */
1113 stream_name, /* fname */
1114 FILE_GENERIC_WRITE, /* access_mask */
1115 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1116 FILE_OPEN_IF, /* create_disposition */
1117 0, /* create_options */
1118 0, /* file_attributes */
1119 INTERNAL_OPEN_ONLY, /* oplock_request */
1121 0, /* allocation_size */
1122 0, /* private_flags */
1127 NULL, NULL); /* create context */
1128 TALLOC_FREE(stream_name);
1129 if (!NT_STATUS_IS_OK(status)) {
1130 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1134 nwritten = SMB_VFS_PWRITE(fsp,
1138 if (nwritten == -1) {
1139 DBG_ERR("SMB_VFS_PWRITE failed\n");
1140 saved_errno = errno;
1141 close_file(NULL, fsp, ERROR_CLOSE);
1142 errno = saved_errno;
1146 status = close_file(NULL, fsp, NORMAL_CLOSE);
1147 if (!NT_STATUS_IS_OK(status)) {
1155 static bool ad_convert_truncate(struct adouble *ad,
1156 const struct smb_filename *smb_fname)
1161 * FIXME: direct ftruncate(), but we don't have a fsp for the
1164 rc = ftruncate(ad->ad_fd, ad_getentryoff(ad, ADEID_RFORK)
1165 + ad_getentrylen(ad, ADEID_RFORK));
1174 * Convert from Apple's ._ file to Netatalk
1176 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1177 * bytes containing packed xattrs.
1179 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1182 static int ad_convert(struct adouble *ad,
1183 const struct smb_filename *smb_fname)
1186 char *map = MAP_FAILED;
1191 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1195 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1196 ad_getentrylen(ad, ADEID_RFORK);
1198 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1199 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED,
1201 if (map == MAP_FAILED) {
1202 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1206 ok = ad_convert_xattr(ad, smb_fname, map);
1208 munmap(map, origlen);
1212 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1213 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1214 map + ad_getentryoff(ad, ADEID_RFORK),
1215 ad_getentrylen(ad, ADEID_RFORK));
1218 ad_setentryoff(ad, ADEID_RFORK,
1219 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1221 ok = ad_convert_truncate(ad, smb_fname);
1223 munmap(map, origlen);
1227 rc = munmap(map, origlen);
1229 DBG_ERR("munmap failed: %s\n", strerror(errno));
1235 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1239 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1240 if (len != AD_DATASZ_DOT_UND) {
1241 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1245 ok = ad_convert_finderinfo(ad, smb_fname);
1247 DBG_ERR("Failed to convert [%s]\n",
1248 smb_fname_str_dbg(smb_fname));
1256 * Read and parse Netatalk AppleDouble metadata xattr
1258 static ssize_t ad_read_meta(struct adouble *ad,
1259 const struct smb_filename *smb_fname)
1265 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1267 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1268 AFPINFO_EA_NETATALK, ad->ad_data,
1274 if (errno == ENOATTR) {
1280 DEBUG(2, ("error reading meta xattr: %s\n",
1286 if (ealen != AD_DATASZ_XATTR) {
1287 DEBUG(2, ("bad size %zd\n", ealen));
1293 /* Now parse entries */
1294 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1296 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1302 if (!ad_getentryoff(ad, ADEID_FINDERI)
1303 || !ad_getentryoff(ad, ADEID_COMMENT)
1304 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1305 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1306 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1307 || !ad_getentryoff(ad, ADEID_PRIVINO)
1308 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1309 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1310 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1317 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1318 smb_fname->base_name, rc));
1322 if (errno == EINVAL) {
1324 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1332 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1336 #ifdef HAVE_ATTROPEN
1337 /* FIXME: direct Solaris xattr syscall */
1338 return attropen(smb_fname->base_name,
1339 AFPRESOURCE_EA_NETATALK, flags, mode);
1346 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1352 struct smb_filename *adp_smb_fname = NULL;
1354 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1359 fd = open(adp_smb_fname->base_name, flags, mode);
1360 TALLOC_FREE(adp_smb_fname);
1365 static int ad_open_rsrc(vfs_handle_struct *handle,
1366 const struct smb_filename *smb_fname,
1370 struct fruit_config_data *config = NULL;
1373 SMB_VFS_HANDLE_GET_DATA(handle, config,
1374 struct fruit_config_data, return -1);
1376 if (config->rsrc == FRUIT_RSRC_XATTR) {
1377 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1379 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1386 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1387 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1388 * for file IO on the ._ file.
1390 static int ad_open(vfs_handle_struct *handle,
1393 const struct smb_filename *smb_fname,
1399 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1400 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1402 if (ad->ad_type == ADOUBLE_META) {
1406 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1407 ad->ad_fd = fsp->fh->fd;
1408 ad->ad_opened = false;
1412 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1416 ad->ad_opened = true;
1419 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1420 smb_fname->base_name,
1421 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1426 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1431 /* FIXME: direct sys_fstat(), don't have an fsp */
1432 ret = sys_fstat(ad->ad_fd, &st,
1433 lp_fake_directory_create_times(
1434 SNUM(ad->ad_handle->conn)));
1439 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1440 return st.st_ex_size;
1443 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1444 const struct smb_filename *smb_fname)
1446 SMB_STRUCT_STAT sbuf;
1453 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1454 SNUM(ad->ad_handle->conn)));
1460 * AppleDouble file header content and size, two cases:
1462 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1463 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1465 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1467 size = sbuf.st_ex_size;
1468 if (size > talloc_array_length(ad->ad_data)) {
1469 if (size > AD_XATTR_MAX_HDR_SIZE) {
1470 size = AD_XATTR_MAX_HDR_SIZE;
1472 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1479 len = sys_pread(ad->ad_fd, ad->ad_data,
1480 talloc_array_length(ad->ad_data), 0);
1481 if (len != talloc_array_length(ad->ad_data)) {
1482 DBG_NOTICE("%s %s: bad size: %zd\n",
1483 smb_fname->base_name, strerror(errno), len);
1487 /* Now parse entries */
1488 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1490 DBG_ERR("invalid AppleDouble resource %s\n",
1491 smb_fname->base_name);
1496 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1497 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1498 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1499 DBG_ERR("invalid AppleDouble resource %s\n",
1500 smb_fname->base_name);
1506 * Try to fixup AppleDouble files created by OS X with xattrs
1507 * appended to the ADEID_FINDERI entry.
1510 ret = ad_convert(ad, smb_fname);
1512 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1520 * Read and parse resource fork, either ._ AppleDouble file or xattr
1522 static ssize_t ad_read_rsrc(struct adouble *ad,
1523 const struct smb_filename *smb_fname)
1525 struct fruit_config_data *config = NULL;
1528 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1529 struct fruit_config_data, return -1);
1531 if (config->rsrc == FRUIT_RSRC_XATTR) {
1532 len = ad_read_rsrc_xattr(ad);
1534 len = ad_read_rsrc_adouble(ad, smb_fname);
1541 * Read and unpack an AppleDouble metadata xattr or resource
1543 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1545 switch (ad->ad_type) {
1547 return ad_read_meta(ad, smb_fname);
1549 return ad_read_rsrc(ad, smb_fname);
1555 static int adouble_destructor(struct adouble *ad)
1557 if ((ad->ad_fd != -1) && ad->ad_opened) {
1565 * Allocate a struct adouble without initialiing it
1567 * The struct is either hang of the fsp extension context or if fsp is
1570 * @param[in] ctx talloc context
1571 * @param[in] handle vfs handle
1572 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1574 * @return adouble handle
1576 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1577 adouble_type_t type)
1582 struct fruit_config_data *config;
1584 SMB_VFS_HANDLE_GET_DATA(handle, config,
1585 struct fruit_config_data, return NULL);
1589 adsize = AD_DATASZ_XATTR;
1592 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1593 adsize = AD_DATASZ_DOT_UND;
1600 ad = talloc_zero(ctx, struct adouble);
1607 ad->ad_data = talloc_zero_array(ad, char, adsize);
1608 if (ad->ad_data == NULL) {
1614 ad->ad_handle = handle;
1616 ad->ad_magic = AD_MAGIC;
1617 ad->ad_version = AD_VERSION;
1620 talloc_set_destructor(ad, adouble_destructor);
1630 * Allocate and initialize a new struct adouble
1632 * @param[in] ctx talloc context
1633 * @param[in] handle vfs handle
1634 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1636 * @return adouble handle, initialized
1638 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1639 adouble_type_t type)
1642 const struct ad_entry_order *eid;
1643 struct adouble *ad = NULL;
1644 struct fruit_config_data *config;
1645 time_t t = time(NULL);
1647 SMB_VFS_HANDLE_GET_DATA(handle, config,
1648 struct fruit_config_data, return NULL);
1652 eid = entry_order_meta_xattr;
1655 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1656 eid = entry_order_dot_und;
1658 eid = entry_order_rsrc_xattr;
1665 ad = ad_alloc(ctx, handle, type);
1671 ad->ad_eid[eid->id].ade_off = eid->offset;
1672 ad->ad_eid[eid->id].ade_len = eid->len;
1676 /* put something sane in the date fields */
1677 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1678 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1679 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1680 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1688 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1689 vfs_handle_struct *handle,
1691 const struct smb_filename *smb_fname,
1692 adouble_type_t type)
1696 struct adouble *ad = NULL;
1700 smb_fname = fsp->base_fsp->fsp_name;
1703 DEBUG(10, ("ad_get(%s) called for %s\n",
1704 type == ADOUBLE_META ? "meta" : "rsrc",
1705 smb_fname->base_name));
1707 ad = ad_alloc(ctx, handle, type);
1713 /* Try rw first so we can use the fd in ad_convert() */
1716 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1717 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1719 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1722 DBG_DEBUG("ad_open [%s] error [%s]\n",
1723 smb_fname->base_name, strerror(errno));
1728 len = ad_read(ad, smb_fname);
1730 DEBUG(10, ("error reading AppleDouble for %s\n",
1731 smb_fname->base_name));
1737 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1738 type == ADOUBLE_META ? "meta" : "rsrc",
1739 smb_fname->base_name, rc));
1748 * Return AppleDouble data for a file
1750 * @param[in] ctx talloc context
1751 * @param[in] handle vfs handle
1752 * @param[in] smb_fname pathname to file or directory
1753 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1755 * @return talloced struct adouble or NULL on error
1757 static struct adouble *ad_get(TALLOC_CTX *ctx,
1758 vfs_handle_struct *handle,
1759 const struct smb_filename *smb_fname,
1760 adouble_type_t type)
1762 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1766 * Return AppleDouble data for a file
1768 * @param[in] ctx talloc context
1769 * @param[in] handle vfs handle
1770 * @param[in] fsp fsp to use for IO
1771 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1773 * @return talloced struct adouble or NULL on error
1775 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1776 files_struct *fsp, adouble_type_t type)
1778 return ad_get_internal(ctx, handle, fsp, NULL, type);
1782 * Set AppleDouble metadata on a file or directory
1784 * @param[in] ad adouble handle
1786 * @param[in] smb_fname pathname to file or directory
1788 * @return status code, 0 means success
1790 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1795 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1797 if (ad->ad_type != ADOUBLE_META) {
1798 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1799 smb_fname->base_name);
1808 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1810 AFPINFO_EA_NETATALK,
1812 AD_DATASZ_XATTR, 0);
1814 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1820 * Set AppleDouble metadata on a file or directory
1822 * @param[in] ad adouble handle
1823 * @param[in] fsp file handle
1825 * @return status code, 0 means success
1827 static int ad_fset(struct adouble *ad, files_struct *fsp)
1833 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1836 || (fsp->fh == NULL)
1837 || (fsp->fh->fd == -1))
1839 smb_panic("bad fsp");
1847 switch (ad->ad_type) {
1849 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1851 AFPINFO_EA_NETATALK,
1853 AD_DATASZ_XATTR, 0);
1857 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1862 if (len != AD_DATASZ_DOT_UND) {
1863 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1873 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1878 /*****************************************************************************
1880 *****************************************************************************/
1882 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1884 if (strncasecmp_m(smb_fname->stream_name,
1885 AFPINFO_STREAM_NAME,
1886 strlen(AFPINFO_STREAM_NAME)) == 0) {
1892 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1894 if (strncasecmp_m(smb_fname->stream_name,
1895 AFPRESOURCE_STREAM_NAME,
1896 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1903 * Test whether stream is an Apple stream, not used atm
1906 static bool is_apple_stream(const struct smb_filename *smb_fname)
1908 if (is_afpinfo_stream(smb_fname)) {
1911 if (is_afpresource_stream(smb_fname)) {
1919 * Initialize config struct from our smb.conf config parameters
1921 static int init_fruit_config(vfs_handle_struct *handle)
1923 struct fruit_config_data *config;
1925 const char *tm_size_str = NULL;
1927 config = talloc_zero(handle->conn, struct fruit_config_data);
1929 DEBUG(1, ("talloc_zero() failed\n"));
1935 * Versions up to Samba 4.5.x had a spelling bug in the
1936 * fruit:resource option calling lp_parm_enum with
1937 * "res*s*ource" (ie two s).
1939 * In Samba 4.6 we accept both the wrong and the correct
1940 * spelling, in Samba 4.7 the bad spelling will be removed.
1942 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1943 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1944 if (enumval == -1) {
1945 DEBUG(1, ("value for %s: resource type unknown\n",
1946 FRUIT_PARAM_TYPE_NAME));
1949 config->rsrc = (enum fruit_rsrc)enumval;
1951 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1952 "resource", fruit_rsrc, enumval);
1953 if (enumval == -1) {
1954 DEBUG(1, ("value for %s: resource type unknown\n",
1955 FRUIT_PARAM_TYPE_NAME));
1958 config->rsrc = (enum fruit_rsrc)enumval;
1960 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1961 "metadata", fruit_meta, FRUIT_META_NETATALK);
1962 if (enumval == -1) {
1963 DEBUG(1, ("value for %s: metadata type unknown\n",
1964 FRUIT_PARAM_TYPE_NAME));
1967 config->meta = (enum fruit_meta)enumval;
1969 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1970 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1971 if (enumval == -1) {
1972 DEBUG(1, ("value for %s: locking type unknown\n",
1973 FRUIT_PARAM_TYPE_NAME));
1976 config->locking = (enum fruit_locking)enumval;
1978 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1979 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1980 if (enumval == -1) {
1981 DEBUG(1, ("value for %s: encoding type unknown\n",
1982 FRUIT_PARAM_TYPE_NAME));
1985 config->encoding = (enum fruit_encoding)enumval;
1987 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1988 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1989 FRUIT_PARAM_TYPE_NAME,
1994 config->use_aapl = lp_parm_bool(
1995 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1997 config->time_machine = lp_parm_bool(
1998 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
2000 config->unix_info_enabled = lp_parm_bool(
2001 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
2003 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
2006 config->posix_rename = lp_parm_bool(
2007 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
2009 config->aapl_zero_file_id =
2010 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
2012 config->readdir_attr_rsize = lp_parm_bool(
2013 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
2015 config->readdir_attr_finder_info = lp_parm_bool(
2016 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
2018 config->readdir_attr_max_access = lp_parm_bool(
2019 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
2021 config->model = lp_parm_const_string(
2022 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
2024 tm_size_str = lp_parm_const_string(
2025 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2026 "time machine max size", NULL);
2027 if (tm_size_str != NULL) {
2028 config->time_machine_max_size = conv_str_size(tm_size_str);
2031 SMB_VFS_HANDLE_SET_DATA(handle, config,
2032 NULL, struct fruit_config_data,
2039 * Prepend "._" to a basename
2040 * Return a new struct smb_filename with stream_name == NULL.
2042 static int adouble_path(TALLOC_CTX *ctx,
2043 const struct smb_filename *smb_fname_in,
2044 struct smb_filename **pp_smb_fname_out)
2048 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2051 if (smb_fname == NULL) {
2055 /* We need streamname to be NULL */
2056 TALLOC_FREE(smb_fname->stream_name);
2058 /* And we're replacing base_name. */
2059 TALLOC_FREE(smb_fname->base_name);
2061 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2063 TALLOC_FREE(smb_fname);
2067 smb_fname->base_name = talloc_asprintf(smb_fname,
2068 "%s/._%s", parent, base);
2069 if (smb_fname->base_name == NULL) {
2070 TALLOC_FREE(smb_fname);
2074 *pp_smb_fname_out = smb_fname;
2080 * Allocate and initialize an AfpInfo struct
2082 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2084 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2088 ai->afpi_Signature = AFP_Signature;
2089 ai->afpi_Version = AFP_Version;
2090 ai->afpi_BackupTime = AD_DATE_START;
2095 * Pack an AfpInfo struct into a buffer
2097 * Buffer size must be at least AFP_INFO_SIZE
2098 * Returns size of packed buffer
2100 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2102 memset(buf, 0, AFP_INFO_SIZE);
2104 RSIVAL(buf, 0, ai->afpi_Signature);
2105 RSIVAL(buf, 4, ai->afpi_Version);
2106 RSIVAL(buf, 12, ai->afpi_BackupTime);
2107 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2109 return AFP_INFO_SIZE;
2113 * Unpack a buffer into a AfpInfo structure
2115 * Buffer size must be at least AFP_INFO_SIZE
2116 * Returns allocated AfpInfo struct
2118 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2120 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2125 ai->afpi_Signature = RIVAL(data, 0);
2126 ai->afpi_Version = RIVAL(data, 4);
2127 ai->afpi_BackupTime = RIVAL(data, 12);
2128 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2129 sizeof(ai->afpi_FinderInfo));
2131 if (ai->afpi_Signature != AFP_Signature
2132 || ai->afpi_Version != AFP_Version) {
2133 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2141 * Fake an inode number from the md5 hash of the (xattr) name
2143 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2146 unsigned char hash[16];
2150 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2151 SMB_ASSERT(upper_sname != NULL);
2154 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2155 sizeof(sbuf->st_ex_dev));
2156 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2157 sizeof(sbuf->st_ex_ino));
2158 MD5Update(&ctx, (unsigned char *)upper_sname,
2159 talloc_get_size(upper_sname)-1);
2160 MD5Final(hash, &ctx);
2162 TALLOC_FREE(upper_sname);
2164 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2165 memcpy(&result, hash, sizeof(result));
2167 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2168 sname, (unsigned long long)result));
2173 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2174 struct stream_struct **streams,
2175 const char *name, off_t size,
2178 struct stream_struct *tmp;
2180 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2186 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2187 if (tmp[*num_streams].name == NULL) {
2191 tmp[*num_streams].size = size;
2192 tmp[*num_streams].alloc_size = alloc_size;
2199 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2200 struct stream_struct **streams)
2202 struct stream_struct *tmp = *streams;
2205 if (*num_streams == 0) {
2209 for (i = 0; i < *num_streams; i++) {
2210 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2215 if (i == *num_streams) {
2219 if (tmp[i].size > 0) {
2223 TALLOC_FREE(tmp[i].name);
2224 if (*num_streams - 1 > i) {
2225 memmove(&tmp[i], &tmp[i+1],
2226 (*num_streams - i - 1) * sizeof(struct stream_struct));
2233 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2234 struct stream_struct **streams,
2237 struct stream_struct *tmp = *streams;
2240 if (*num_streams == 0) {
2244 for (i = 0; i < *num_streams; i++) {
2245 if (strequal_m(tmp[i].name, name)) {
2250 if (i == *num_streams) {
2254 TALLOC_FREE(tmp[i].name);
2255 if (*num_streams - 1 > i) {
2256 memmove(&tmp[i], &tmp[i+1],
2257 (*num_streams - i - 1) * sizeof(struct stream_struct));
2264 static bool ad_empty_finderinfo(const struct adouble *ad)
2267 char emptybuf[ADEDLEN_FINDERI] = {0};
2270 fi = ad_get_entry(ad, ADEID_FINDERI);
2272 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2276 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2280 static bool ai_empty_finderinfo(const AfpInfo *ai)
2283 char emptybuf[ADEDLEN_FINDERI] = {0};
2285 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2290 * Update btime with btime from Netatalk
2292 static void update_btime(vfs_handle_struct *handle,
2293 struct smb_filename *smb_fname)
2296 struct timespec creation_time = {0};
2298 struct fruit_config_data *config = NULL;
2300 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2303 switch (config->meta) {
2304 case FRUIT_META_STREAM:
2306 case FRUIT_META_NETATALK:
2310 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2314 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2318 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2324 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2325 update_stat_ex_create_time(&smb_fname->st, creation_time);
2331 * Map an access mask to a Netatalk single byte byte range lock
2333 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2334 uint32_t access_mask)
2338 switch (access_mask) {
2339 case FILE_READ_DATA:
2340 offset = AD_FILELOCK_OPEN_RD;
2343 case FILE_WRITE_DATA:
2344 case FILE_APPEND_DATA:
2345 offset = AD_FILELOCK_OPEN_WR;
2349 offset = AD_FILELOCK_OPEN_NONE;
2353 if (fork_type == APPLE_FORK_RSRC) {
2354 if (offset == AD_FILELOCK_OPEN_NONE) {
2355 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2365 * Map a deny mode to a Netatalk brl
2367 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2372 switch (deny_mode) {
2374 offset = AD_FILELOCK_DENY_RD;
2378 offset = AD_FILELOCK_DENY_WR;
2382 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2385 if (fork_type == APPLE_FORK_RSRC) {
2393 * Call fcntl() with an exclusive F_GETLK request in order to
2394 * determine if there's an exisiting shared lock
2396 * @return true if the requested lock was found or any error occurred
2397 * false if the lock was not found
2399 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2402 off_t offset = in_offset;
2407 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2408 if (result == false) {
2412 if (type != F_UNLCK) {
2419 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2421 uint32_t access_mask,
2424 NTSTATUS status = NT_STATUS_OK;
2425 bool open_for_reading, open_for_writing, deny_read, deny_write;
2427 bool have_read = false;
2430 /* FIXME: hardcoded data fork, add resource fork */
2431 enum apple_fork fork_type = APPLE_FORK_DATA;
2433 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2435 access_mask & FILE_READ_DATA ? "READ" :"-",
2436 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2437 deny_mode & DENY_READ ? "DENY_READ" : "-",
2438 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2440 if (fsp->fh->fd == -1) {
2441 return NT_STATUS_OK;
2444 flags = fcntl(fsp->fh->fd, F_GETFL);
2446 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2447 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2448 return map_nt_error_from_unix(errno);
2451 if (flags & (O_RDONLY|O_RDWR)) {
2453 * Applying fcntl read locks requires an fd opened for
2454 * reading. This means we won't be applying locks for
2455 * files openend write-only, but what can we do...
2461 * Check read access and deny read mode
2463 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2465 open_for_reading = test_netatalk_lock(
2466 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2468 deny_read = test_netatalk_lock(
2469 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2471 DEBUG(10, ("read: %s, deny_write: %s\n",
2472 open_for_reading == true ? "yes" : "no",
2473 deny_read == true ? "yes" : "no"));
2475 if (((access_mask & FILE_READ_DATA) && deny_read)
2476 || ((deny_mode & DENY_READ) && open_for_reading)) {
2477 return NT_STATUS_SHARING_VIOLATION;
2481 if ((access_mask & FILE_READ_DATA) && have_read) {
2482 struct byte_range_lock *br_lck = NULL;
2484 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2486 handle->conn->sconn->msg_ctx, fsp,
2487 fsp->op->global->open_persistent_id, 1, off,
2488 READ_LOCK, POSIX_LOCK, false,
2491 TALLOC_FREE(br_lck);
2493 if (!NT_STATUS_IS_OK(status)) {
2498 if ((deny_mode & DENY_READ) && have_read) {
2499 struct byte_range_lock *br_lck = NULL;
2501 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2503 handle->conn->sconn->msg_ctx, fsp,
2504 fsp->op->global->open_persistent_id, 1, off,
2505 READ_LOCK, POSIX_LOCK, false,
2508 TALLOC_FREE(br_lck);
2510 if (!NT_STATUS_IS_OK(status)) {
2517 * Check write access and deny write mode
2519 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2521 open_for_writing = test_netatalk_lock(
2522 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2524 deny_write = test_netatalk_lock(
2525 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2527 DEBUG(10, ("write: %s, deny_write: %s\n",
2528 open_for_writing == true ? "yes" : "no",
2529 deny_write == true ? "yes" : "no"));
2531 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2532 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2533 return NT_STATUS_SHARING_VIOLATION;
2537 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2538 struct byte_range_lock *br_lck = NULL;
2540 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2542 handle->conn->sconn->msg_ctx, fsp,
2543 fsp->op->global->open_persistent_id, 1, off,
2544 READ_LOCK, POSIX_LOCK, false,
2547 TALLOC_FREE(br_lck);
2549 if (!NT_STATUS_IS_OK(status)) {
2553 if ((deny_mode & DENY_WRITE) && have_read) {
2554 struct byte_range_lock *br_lck = NULL;
2556 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2558 handle->conn->sconn->msg_ctx, fsp,
2559 fsp->op->global->open_persistent_id, 1, off,
2560 READ_LOCK, POSIX_LOCK, false,
2563 TALLOC_FREE(br_lck);
2565 if (!NT_STATUS_IS_OK(status)) {
2574 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2575 struct smb_request *req,
2576 const struct smb2_create_blobs *in_context_blobs,
2577 struct smb2_create_blobs *out_context_blobs)
2579 struct fruit_config_data *config;
2581 struct smb2_create_blob *aapl = NULL;
2585 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2586 uint64_t req_bitmap, client_caps;
2587 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2591 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2592 return NT_STATUS_UNSUCCESSFUL);
2594 if (!config->use_aapl
2595 || in_context_blobs == NULL
2596 || out_context_blobs == NULL) {
2597 return NT_STATUS_OK;
2600 aapl = smb2_create_blob_find(in_context_blobs,
2601 SMB2_CREATE_TAG_AAPL);
2603 return NT_STATUS_OK;
2606 if (aapl->data.length != 24) {
2607 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2608 (uintmax_t)aapl->data.length));
2609 return NT_STATUS_INVALID_PARAMETER;
2612 cmd = IVAL(aapl->data.data, 0);
2613 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2614 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2615 return NT_STATUS_INVALID_PARAMETER;
2618 req_bitmap = BVAL(aapl->data.data, 8);
2619 client_caps = BVAL(aapl->data.data, 16);
2621 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2623 SBVAL(p, 8, req_bitmap);
2624 ok = data_blob_append(req, &blob, p, 16);
2626 return NT_STATUS_UNSUCCESSFUL;
2629 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2630 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2631 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2632 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2633 config->readdir_attr_enabled = true;
2636 if (config->use_copyfile) {
2637 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2638 config->copyfile_enabled = true;
2642 * The client doesn't set the flag, so we can't check
2643 * for it and just set it unconditionally
2645 if (config->unix_info_enabled) {
2646 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2649 SBVAL(p, 0, server_caps);
2650 ok = data_blob_append(req, &blob, p, 8);
2652 return NT_STATUS_UNSUCCESSFUL;
2656 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2657 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2665 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2672 if (config->time_machine) {
2673 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2678 ok = data_blob_append(req, &blob, p, 8);
2680 return NT_STATUS_UNSUCCESSFUL;
2684 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2685 ok = convert_string_talloc(req,
2686 CH_UNIX, CH_UTF16LE,
2687 config->model, strlen(config->model),
2690 return NT_STATUS_UNSUCCESSFUL;
2694 SIVAL(p + 4, 0, modellen);
2695 ok = data_blob_append(req, &blob, p, 8);
2698 return NT_STATUS_UNSUCCESSFUL;
2701 ok = data_blob_append(req, &blob, model, modellen);
2704 return NT_STATUS_UNSUCCESSFUL;
2708 status = smb2_create_blob_add(out_context_blobs,
2710 SMB2_CREATE_TAG_AAPL,
2712 if (NT_STATUS_IS_OK(status)) {
2713 global_fruit_config.nego_aapl = true;
2714 if (config->aapl_zero_file_id) {
2715 aapl_force_zero_file_id(handle->conn->sconn);
2722 static bool readdir_attr_meta_finderi_stream(
2723 struct vfs_handle_struct *handle,
2724 const struct smb_filename *smb_fname,
2727 struct smb_filename *stream_name = NULL;
2728 files_struct *fsp = NULL;
2733 uint8_t buf[AFP_INFO_SIZE];
2735 stream_name = synthetic_smb_fname(talloc_tos(),
2736 smb_fname->base_name,
2737 AFPINFO_STREAM_NAME,
2738 NULL, smb_fname->flags);
2739 if (stream_name == NULL) {
2743 ret = SMB_VFS_STAT(handle->conn, stream_name);
2748 status = SMB_VFS_CREATE_FILE(
2749 handle->conn, /* conn */
2751 0, /* root_dir_fid */
2752 stream_name, /* fname */
2753 FILE_READ_DATA, /* access_mask */
2754 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2756 FILE_OPEN, /* create_disposition*/
2757 0, /* create_options */
2758 0, /* file_attributes */
2759 INTERNAL_OPEN_ONLY, /* oplock_request */
2761 0, /* allocation_size */
2762 0, /* private_flags */
2767 NULL, NULL); /* create context */
2769 TALLOC_FREE(stream_name);
2771 if (!NT_STATUS_IS_OK(status)) {
2775 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2776 if (nread != AFP_INFO_SIZE) {
2777 DBG_ERR("short read [%s] [%zd/%d]\n",
2778 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2783 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2790 close_file(NULL, fsp, NORMAL_CLOSE);
2796 static bool readdir_attr_meta_finderi_netatalk(
2797 struct vfs_handle_struct *handle,
2798 const struct smb_filename *smb_fname,
2801 struct adouble *ad = NULL;
2804 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2809 p = ad_get_entry(ad, ADEID_FINDERI);
2811 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2816 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2821 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2822 const struct smb_filename *smb_fname,
2823 struct readdir_attr_data *attr_data)
2825 struct fruit_config_data *config = NULL;
2826 uint32_t date_added;
2830 SMB_VFS_HANDLE_GET_DATA(handle, config,
2831 struct fruit_config_data,
2834 switch (config->meta) {
2835 case FRUIT_META_NETATALK:
2836 ok = readdir_attr_meta_finderi_netatalk(
2837 handle, smb_fname, &ai);
2840 case FRUIT_META_STREAM:
2841 ok = readdir_attr_meta_finderi_stream(
2842 handle, smb_fname, &ai);
2846 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2851 /* Don't bother with errors, it's likely ENOENT */
2855 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2857 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2858 &ai.afpi_FinderInfo[0], 4);
2860 /* finder_creator */
2861 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2862 &ai.afpi_FinderInfo[4], 4);
2866 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2867 &ai.afpi_FinderInfo[8], 2);
2869 /* finder_ext_flags */
2870 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2871 &ai.afpi_FinderInfo[24], 2);
2874 date_added = convert_time_t_to_uint32_t(
2875 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2877 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2882 static uint64_t readdir_attr_rfork_size_adouble(
2883 struct vfs_handle_struct *handle,
2884 const struct smb_filename *smb_fname)
2886 struct adouble *ad = NULL;
2887 uint64_t rfork_size;
2889 ad = ad_get(talloc_tos(), handle, smb_fname,
2895 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2901 static uint64_t readdir_attr_rfork_size_stream(
2902 struct vfs_handle_struct *handle,
2903 const struct smb_filename *smb_fname)
2905 struct smb_filename *stream_name = NULL;
2907 uint64_t rfork_size;
2909 stream_name = synthetic_smb_fname(talloc_tos(),
2910 smb_fname->base_name,
2911 AFPRESOURCE_STREAM_NAME,
2913 if (stream_name == NULL) {
2917 ret = SMB_VFS_STAT(handle->conn, stream_name);
2919 TALLOC_FREE(stream_name);
2923 rfork_size = stream_name->st.st_ex_size;
2924 TALLOC_FREE(stream_name);
2929 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2930 const struct smb_filename *smb_fname)
2932 struct fruit_config_data *config = NULL;
2933 uint64_t rfork_size;
2935 SMB_VFS_HANDLE_GET_DATA(handle, config,
2936 struct fruit_config_data,
2939 switch (config->rsrc) {
2940 case FRUIT_RSRC_ADFILE:
2941 case FRUIT_RSRC_XATTR:
2942 rfork_size = readdir_attr_rfork_size_adouble(handle,
2946 case FRUIT_META_STREAM:
2947 rfork_size = readdir_attr_rfork_size_stream(handle,
2952 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2960 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2961 const struct smb_filename *smb_fname,
2962 struct readdir_attr_data *attr_data)
2964 NTSTATUS status = NT_STATUS_OK;
2965 struct fruit_config_data *config = NULL;
2968 SMB_VFS_HANDLE_GET_DATA(handle, config,
2969 struct fruit_config_data,
2970 return NT_STATUS_UNSUCCESSFUL);
2973 /* Ensure we return a default value in the creation_date field */
2974 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2977 * Resource fork length
2980 if (config->readdir_attr_rsize) {
2981 uint64_t rfork_size;
2983 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2984 attr_data->attr_data.aapl.rfork_size = rfork_size;
2991 if (config->readdir_attr_finder_info) {
2992 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2994 status = NT_STATUS_INTERNAL_ERROR;
3001 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
3006 if (psd->dacl == NULL) {
3007 return NT_STATUS_OK;
3010 for (i = 0; i < psd->dacl->num_aces; i++) {
3011 /* MS NFS style mode/uid/gid */
3012 int cmp = dom_sid_compare_domain(
3013 &global_sid_Unix_NFS,
3014 &psd->dacl->aces[i].trustee);
3016 /* Normal ACE entry. */
3021 * security_descriptor_dacl_del()
3022 * *must* return NT_STATUS_OK as we know
3023 * we have something to remove.
3026 status = security_descriptor_dacl_del(psd,
3027 &psd->dacl->aces[i].trustee);
3028 if (!NT_STATUS_IS_OK(status)) {
3029 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3035 * security_descriptor_dacl_del() may delete more
3036 * then one entry subsequent to this one if the
3037 * SID matches, but we only need to ensure that
3038 * we stay looking at the same element in the array.
3042 return NT_STATUS_OK;
3045 /* Search MS NFS style ACE with UNIX mode */
3046 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3048 struct security_descriptor *psd,
3053 struct fruit_config_data *config = NULL;
3057 SMB_VFS_HANDLE_GET_DATA(handle, config,
3058 struct fruit_config_data,
3059 return NT_STATUS_UNSUCCESSFUL);
3061 if (!global_fruit_config.nego_aapl) {
3062 return NT_STATUS_OK;
3064 if (psd->dacl == NULL || !config->unix_info_enabled) {
3065 return NT_STATUS_OK;
3068 for (i = 0; i < psd->dacl->num_aces; i++) {
3069 if (dom_sid_compare_domain(
3070 &global_sid_Unix_NFS_Mode,
3071 &psd->dacl->aces[i].trustee) == 0) {
3072 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3073 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3076 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3077 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3083 * Remove any incoming virtual ACE entries generated by
3084 * fruit_fget_nt_acl().
3087 return remove_virtual_nfs_aces(psd);
3090 /****************************************************************************
3092 ****************************************************************************/
3094 static int fruit_connect(vfs_handle_struct *handle,
3095 const char *service,
3099 char *list = NULL, *newlist = NULL;
3100 struct fruit_config_data *config;
3102 DEBUG(10, ("fruit_connect\n"));
3104 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3109 rc = init_fruit_config(handle);
3114 SMB_VFS_HANDLE_GET_DATA(handle, config,
3115 struct fruit_config_data, return -1);
3117 if (config->veto_appledouble) {
3118 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3121 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3122 newlist = talloc_asprintf(
3124 "%s/" ADOUBLE_NAME_PREFIX "*/",
3126 lp_do_parameter(SNUM(handle->conn),
3131 lp_do_parameter(SNUM(handle->conn),
3133 "/" ADOUBLE_NAME_PREFIX "*/");
3139 if (config->encoding == FRUIT_ENC_NATIVE) {
3140 lp_do_parameter(SNUM(handle->conn),
3145 if (config->time_machine) {
3146 DBG_NOTICE("Enabling durable handles for Time Machine "
3147 "support on [%s]\n", service);
3148 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3149 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3150 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3151 if (!lp_strict_sync(SNUM(handle->conn))) {
3152 DBG_WARNING("Time Machine without strict sync is not "
3155 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3161 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3162 struct smb_filename *smb_fname,
3168 char afpinfo_buf[AFP_INFO_SIZE];
3169 ssize_t len, written;
3173 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3178 if (!(flags & (O_CREAT | O_TRUNC))) {
3182 ai = afpinfo_new(talloc_tos());
3188 len = afpinfo_pack(ai, afpinfo_buf);
3189 if (len != AFP_INFO_SIZE) {
3194 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3195 fsp->fh->fd = hostfd;
3197 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3200 if (written != AFP_INFO_SIZE) {
3201 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3209 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3212 int saved_errno = errno;
3214 fsp->fh->fd = hostfd;
3215 SMB_VFS_NEXT_CLOSE(handle, fsp);
3218 errno = saved_errno;
3223 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3224 struct smb_filename *smb_fname,
3231 struct adouble *ad = NULL;
3234 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3237 * Return a valid fd, but ensure any attempt to use it returns an error
3238 * (EPIPE). All operations on the smb_fname or the fsp will use path
3248 if (flags & (O_CREAT | O_TRUNC)) {
3250 * The attribute does not exist or needs to be truncated,
3251 * create an AppleDouble EA
3253 ad = ad_init(fsp, handle, ADOUBLE_META);
3259 rc = ad_set(ad, fsp->fsp_name);
3269 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3271 int saved_errno = errno;
3276 errno = saved_errno;
3281 static int fruit_open_meta(vfs_handle_struct *handle,
3282 struct smb_filename *smb_fname,
3283 files_struct *fsp, int flags, mode_t mode)
3286 struct fruit_config_data *config = NULL;
3287 struct fio *fio = NULL;
3289 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3291 SMB_VFS_HANDLE_GET_DATA(handle, config,
3292 struct fruit_config_data, return -1);
3294 switch (config->meta) {
3295 case FRUIT_META_STREAM:
3296 fd = fruit_open_meta_stream(handle, smb_fname,
3300 case FRUIT_META_NETATALK:
3301 fd = fruit_open_meta_netatalk(handle, smb_fname,
3306 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3310 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3316 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3317 fio->type = ADOUBLE_META;
3318 fio->config = config;
3323 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3324 struct smb_filename *smb_fname,
3330 struct adouble *ad = NULL;
3331 struct smb_filename *smb_fname_base = NULL;
3332 struct fruit_config_data *config = NULL;
3335 SMB_VFS_HANDLE_GET_DATA(handle, config,
3336 struct fruit_config_data, return -1);
3338 if ((!(flags & O_CREAT)) &&
3339 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3341 /* sorry, but directories don't habe a resource fork */
3346 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3351 /* Sanitize flags */
3352 if (flags & O_WRONLY) {
3353 /* We always need read access for the metadata header too */
3358 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3365 if (flags & (O_CREAT | O_TRUNC)) {
3366 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3372 fsp->fh->fd = hostfd;
3374 rc = ad_fset(ad, fsp);
3385 TALLOC_FREE(smb_fname_base);
3387 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3389 int saved_errno = errno;
3392 * BUGBUGBUG -- we would need to call
3393 * fd_close_posix here, but we don't have a
3396 fsp->fh->fd = hostfd;
3400 errno = saved_errno;
3405 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3406 struct smb_filename *smb_fname,
3411 #ifdef HAVE_ATTROPEN
3414 fd = attropen(smb_fname->base_name,
3415 AFPRESOURCE_EA_NETATALK,
3430 static int fruit_open_rsrc(vfs_handle_struct *handle,
3431 struct smb_filename *smb_fname,
3432 files_struct *fsp, int flags, mode_t mode)
3435 struct fruit_config_data *config = NULL;
3436 struct fio *fio = NULL;
3438 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3440 SMB_VFS_HANDLE_GET_DATA(handle, config,
3441 struct fruit_config_data, return -1);
3443 if (((flags & O_ACCMODE) == O_RDONLY)
3444 && (flags & O_CREAT)
3445 && !VALID_STAT(fsp->fsp_name->st))
3448 * This means the stream doesn't exist. macOS SMB server fails
3449 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3450 * 12565 and the test for this combination in
3451 * test_rfork_create().
3457 switch (config->rsrc) {
3458 case FRUIT_RSRC_STREAM:
3459 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3462 case FRUIT_RSRC_ADFILE:
3463 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3467 case FRUIT_RSRC_XATTR:
3468 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3473 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3477 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3483 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3484 fio->type = ADOUBLE_RSRC;
3485 fio->config = config;
3490 static int fruit_open(vfs_handle_struct *handle,
3491 struct smb_filename *smb_fname,
3492 files_struct *fsp, int flags, mode_t mode)
3496 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3498 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3499 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3502 if (is_afpinfo_stream(smb_fname)) {
3503 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3504 } else if (is_afpresource_stream(smb_fname)) {
3505 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3507 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3510 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3515 static int fruit_rename(struct vfs_handle_struct *handle,
3516 const struct smb_filename *smb_fname_src,
3517 const struct smb_filename *smb_fname_dst)
3520 struct fruit_config_data *config = NULL;
3521 struct smb_filename *src_adp_smb_fname = NULL;
3522 struct smb_filename *dst_adp_smb_fname = NULL;
3524 SMB_VFS_HANDLE_GET_DATA(handle, config,
3525 struct fruit_config_data, return -1);
3527 if (!VALID_STAT(smb_fname_src->st)) {
3528 DBG_ERR("Need valid stat for [%s]\n",
3529 smb_fname_str_dbg(smb_fname_src));
3533 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3538 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3539 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3544 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3549 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3554 DBG_DEBUG("%s -> %s\n",
3555 smb_fname_str_dbg(src_adp_smb_fname),
3556 smb_fname_str_dbg(dst_adp_smb_fname));
3558 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3559 if (errno == ENOENT) {
3564 TALLOC_FREE(src_adp_smb_fname);
3565 TALLOC_FREE(dst_adp_smb_fname);
3569 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3570 const struct smb_filename *smb_fname)
3572 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3575 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3576 const struct smb_filename *smb_fname)
3578 return SMB_VFS_REMOVEXATTR(handle->conn,
3580 AFPINFO_EA_NETATALK);
3583 static int fruit_unlink_meta(vfs_handle_struct *handle,
3584 const struct smb_filename *smb_fname)
3586 struct fruit_config_data *config = NULL;
3589 SMB_VFS_HANDLE_GET_DATA(handle, config,
3590 struct fruit_config_data, return -1);
3592 switch (config->meta) {
3593 case FRUIT_META_STREAM:
3594 rc = fruit_unlink_meta_stream(handle, smb_fname);
3597 case FRUIT_META_NETATALK:
3598 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3602 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3609 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3610 const struct smb_filename *smb_fname,
3615 if (!force_unlink) {
3616 struct smb_filename *smb_fname_cp = NULL;
3619 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3620 if (smb_fname_cp == NULL) {
3625 * 0 byte resource fork streams are not listed by
3626 * vfs_streaminfo, as a result stream cleanup/deletion of file
3627 * deletion doesn't remove the resourcefork stream.
3630 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3632 TALLOC_FREE(smb_fname_cp);
3633 DBG_ERR("stat [%s] failed [%s]\n",
3634 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3638 size = smb_fname_cp->st.st_ex_size;
3639 TALLOC_FREE(smb_fname_cp);
3642 /* OS X ignores resource fork stream delete requests */
3647 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3648 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3655 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3656 const struct smb_filename *smb_fname,
3660 struct adouble *ad = NULL;
3661 struct smb_filename *adp_smb_fname = NULL;
3663 if (!force_unlink) {
3664 ad = ad_get(talloc_tos(), handle, smb_fname,
3673 * 0 byte resource fork streams are not listed by
3674 * vfs_streaminfo, as a result stream cleanup/deletion of file
3675 * deletion doesn't remove the resourcefork stream.
3678 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3679 /* OS X ignores resource fork stream delete requests */
3687 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3692 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3693 TALLOC_FREE(adp_smb_fname);
3694 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3701 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3702 const struct smb_filename *smb_fname,
3706 * OS X ignores resource fork stream delete requests, so nothing to do
3707 * here. Removing the file will remove the xattr anyway, so we don't
3708 * have to take care of removing 0 byte resource forks that could be
3714 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3715 const struct smb_filename *smb_fname,
3718 struct fruit_config_data *config = NULL;
3721 SMB_VFS_HANDLE_GET_DATA(handle, config,
3722 struct fruit_config_data, return -1);
3724 switch (config->rsrc) {
3725 case FRUIT_RSRC_STREAM:
3726 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3729 case FRUIT_RSRC_ADFILE:
3730 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3733 case FRUIT_RSRC_XATTR:
3734 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3738 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3745 static int fruit_unlink(vfs_handle_struct *handle,
3746 const struct smb_filename *smb_fname)
3749 struct fruit_config_data *config = NULL;
3750 struct smb_filename *rsrc_smb_fname = NULL;
3752 SMB_VFS_HANDLE_GET_DATA(handle, config,
3753 struct fruit_config_data, return -1);
3755 if (is_afpinfo_stream(smb_fname)) {
3756 return fruit_unlink_meta(handle, smb_fname);
3757 } else if (is_afpresource_stream(smb_fname)) {
3758 return fruit_unlink_rsrc(handle, smb_fname, false);
3759 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3760 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3764 * A request to delete the base file. Because 0 byte resource
3765 * fork streams are not listed by fruit_streaminfo,
3766 * delete_all_streams() can't remove 0 byte resource fork
3767 * streams, so we have to cleanup this here.
3769 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3770 smb_fname->base_name,
3771 AFPRESOURCE_STREAM_NAME,
3774 if (rsrc_smb_fname == NULL) {
3778 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3779 if ((rc != 0) && (errno != ENOENT)) {
3780 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3781 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3782 TALLOC_FREE(rsrc_smb_fname);
3785 TALLOC_FREE(rsrc_smb_fname);
3787 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3790 static int fruit_chmod(vfs_handle_struct *handle,
3791 const struct smb_filename *smb_fname,
3795 struct fruit_config_data *config = NULL;
3796 struct smb_filename *smb_fname_adp = NULL;
3798 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3803 SMB_VFS_HANDLE_GET_DATA(handle, config,
3804 struct fruit_config_data, return -1);
3806 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3810 if (!VALID_STAT(smb_fname->st)) {
3814 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3818 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3823 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3825 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3826 if (errno == ENOENT) {
3830 TALLOC_FREE(smb_fname_adp);
3834 static int fruit_chown(vfs_handle_struct *handle,
3835 const struct smb_filename *smb_fname,
3840 struct fruit_config_data *config = NULL;
3841 struct smb_filename *adp_smb_fname = NULL;
3843 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3848 SMB_VFS_HANDLE_GET_DATA(handle, config,
3849 struct fruit_config_data, return -1);
3851 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3855 if (!VALID_STAT(smb_fname->st)) {
3859 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3863 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3868 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3870 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3871 if (errno == ENOENT) {
3876 TALLOC_FREE(adp_smb_fname);
3880 static int fruit_rmdir(struct vfs_handle_struct *handle,
3881 const struct smb_filename *smb_fname)
3885 struct fruit_config_data *config;
3887 SMB_VFS_HANDLE_GET_DATA(handle, config,
3888 struct fruit_config_data, return -1);
3890 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3895 * Due to there is no way to change bDeleteVetoFiles variable
3896 * from this module, need to clean up ourselves
3899 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3904 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3906 struct adouble *ad = NULL;
3908 struct smb_filename *ad_smb_fname = NULL;
3911 match = strncmp(de->d_name,
3912 ADOUBLE_NAME_PREFIX,
3913 strlen(ADOUBLE_NAME_PREFIX));
3918 p = talloc_asprintf(talloc_tos(), "%s/%s",
3919 smb_fname->base_name, de->d_name);
3921 DBG_ERR("talloc_asprintf failed\n");
3925 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3929 if (ad_smb_fname == NULL) {
3930 DBG_ERR("synthetic_smb_fname failed\n");
3935 * Check whether it's a valid AppleDouble file, if
3936 * yes, delete it, ignore it otherwise.
3938 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3940 TALLOC_FREE(ad_smb_fname);
3946 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3948 DBG_ERR("Deleting [%s] failed\n",
3949 smb_fname_str_dbg(ad_smb_fname));
3951 TALLOC_FREE(ad_smb_fname);
3956 SMB_VFS_CLOSEDIR(handle->conn, dh);
3958 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3961 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3962 files_struct *fsp, void *data,
3963 size_t n, off_t offset)
3968 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3974 DBG_ERR("Removing [%s] after short read [%zd]\n",
3975 fsp_str_dbg(fsp), nread);
3977 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3979 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3987 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3988 files_struct *fsp, void *data,
3989 size_t n, off_t offset)
3992 struct adouble *ad = NULL;
3993 char afpinfo_buf[AFP_INFO_SIZE];
3997 ai = afpinfo_new(talloc_tos());
4002 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4008 p = ad_get_entry(ad, ADEID_FINDERI);
4010 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4015 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
4017 nread = afpinfo_pack(ai, afpinfo_buf);
4018 if (nread != AFP_INFO_SIZE) {
4023 memcpy(data, afpinfo_buf, n);
4031 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
4032 files_struct *fsp, void *data,
4033 size_t n, off_t offset)
4035 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4040 * OS X has a off-by-1 error in the offset calculation, so we're
4041 * bug compatible here. It won't hurt, as any relevant real
4042 * world read requests from the AFP_AfpInfo stream will be
4043 * offset=0 n=60. offset is ignored anyway, see below.
4045 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4050 DBG_ERR("Failed to fetch fsp extension");
4054 /* Yes, macOS always reads from offset 0 */
4056 to_return = MIN(n, AFP_INFO_SIZE);
4058 switch (fio->config->meta) {
4059 case FRUIT_META_STREAM:
4060 nread = fruit_pread_meta_stream(handle, fsp, data,
4064 case FRUIT_META_NETATALK:
4065 nread = fruit_pread_meta_adouble(handle, fsp, data,
4070 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4077 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4078 files_struct *fsp, void *data,
4079 size_t n, off_t offset)
4081 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4084 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4085 files_struct *fsp, void *data,
4086 size_t n, off_t offset)
4088 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4091 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4092 files_struct *fsp, void *data,
4093 size_t n, off_t offset)
4095 struct adouble *ad = NULL;
4098 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4103 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4104 offset + ad_getentryoff(ad, ADEID_RFORK));
4110 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4111 files_struct *fsp, void *data,
4112 size_t n, off_t offset)
4114 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4122 switch (fio->config->rsrc) {
4123 case FRUIT_RSRC_STREAM:
4124 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4127 case FRUIT_RSRC_ADFILE:
4128 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4131 case FRUIT_RSRC_XATTR:
4132 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4136 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4143 static ssize_t fruit_pread(vfs_handle_struct *handle,
4144 files_struct *fsp, void *data,
4145 size_t n, off_t offset)
4147 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4150 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4151 fsp_str_dbg(fsp), (intmax_t)offset, n);
4154 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4157 if (fio->type == ADOUBLE_META) {
4158 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4160 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4163 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4167 static bool fruit_must_handle_aio_stream(struct fio *fio)
4173 if ((fio->type == ADOUBLE_META) &&
4174 (fio->config->meta == FRUIT_META_NETATALK))
4179 if ((fio->type == ADOUBLE_RSRC) &&
4180 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4188 struct fruit_pread_state {
4190 struct vfs_aio_state vfs_aio_state;
4193 static void fruit_pread_done(struct tevent_req *subreq);
4195 static struct tevent_req *fruit_pread_send(
4196 struct vfs_handle_struct *handle,
4197 TALLOC_CTX *mem_ctx,
4198 struct tevent_context *ev,
4199 struct files_struct *fsp,
4201 size_t n, off_t offset)
4203 struct tevent_req *req = NULL;
4204 struct tevent_req *subreq = NULL;
4205 struct fruit_pread_state *state = NULL;
4206 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4208 req = tevent_req_create(mem_ctx, &state,
4209 struct fruit_pread_state);
4214 if (fruit_must_handle_aio_stream(fio)) {
4215 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4216 if (state->nread != n) {
4217 if (state->nread != -1) {
4220 tevent_req_error(req, errno);
4221 return tevent_req_post(req, ev);
4223 tevent_req_done(req);
4224 return tevent_req_post(req, ev);
4227 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4229 if (tevent_req_nomem(req, subreq)) {
4230 return tevent_req_post(req, ev);
4232 tevent_req_set_callback(subreq, fruit_pread_done, req);
4236 static void fruit_pread_done(struct tevent_req *subreq)
4238 struct tevent_req *req = tevent_req_callback_data(
4239 subreq, struct tevent_req);
4240 struct fruit_pread_state *state = tevent_req_data(
4241 req, struct fruit_pread_state);
4243 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4244 TALLOC_FREE(subreq);
4246 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4249 tevent_req_done(req);
4252 static ssize_t fruit_pread_recv(struct tevent_req *req,
4253 struct vfs_aio_state *vfs_aio_state)
4255 struct fruit_pread_state *state = tevent_req_data(
4256 req, struct fruit_pread_state);
4258 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4262 *vfs_aio_state = state->vfs_aio_state;
4263 return state->nread;
4266 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4267 files_struct *fsp, const void *data,
4268 size_t n, off_t offset)
4274 ai = afpinfo_unpack(talloc_tos(), data);
4279 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4280 if (nwritten != n) {
4284 if (!ai_empty_finderinfo(ai)) {
4288 ok = set_delete_on_close(
4291 handle->conn->session_info->security_token,
4292 handle->conn->session_info->unix_token);
4294 DBG_ERR("set_delete_on_close on [%s] failed\n",
4302 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4303 files_struct *fsp, const void *data,
4304 size_t n, off_t offset)
4306 struct adouble *ad = NULL;
4312 ai = afpinfo_unpack(talloc_tos(), data);
4317 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4319 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4324 p = ad_get_entry(ad, ADEID_FINDERI);
4326 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4331 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4333 ret = ad_fset(ad, fsp);
4335 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4342 if (!ai_empty_finderinfo(ai)) {
4346 ok = set_delete_on_close(
4349 handle->conn->session_info->security_token,
4350 handle->conn->session_info->unix_token);
4352 DBG_ERR("set_delete_on_close on [%s] failed\n",
4360 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4361 files_struct *fsp, const void *data,
4362 size_t n, off_t offset)
4364 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4368 * Writing an all 0 blob to the metadata stream
4369 * results in the stream being removed on a macOS
4370 * server. This ensures we behave the same and it
4371 * verified by the "delete AFP_AfpInfo by writing all
4374 if (n != AFP_INFO_SIZE || offset != 0) {
4375 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4376 (intmax_t)offset, (intmax_t)n);
4381 DBG_ERR("Failed to fetch fsp extension");
4385 switch (fio->config->meta) {
4386 case FRUIT_META_STREAM:
4387 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4391 case FRUIT_META_NETATALK:
4392 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4397 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4404 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4405 files_struct *fsp, const void *data,
4406 size_t n, off_t offset)
4408 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4411 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4412 files_struct *fsp, const void *data,
4413 size_t n, off_t offset)
4415 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4418 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4419 files_struct *fsp, const void *data,
4420 size_t n, off_t offset)
4422 struct adouble *ad = NULL;
4426 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4428 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4432 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4433 offset + ad_getentryoff(ad, ADEID_RFORK));
4434 if (nwritten != n) {
4435 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4436 fsp_str_dbg(fsp), nwritten, n);
4441 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4442 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4443 ret = ad_fset(ad, fsp);
4445 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4455 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4456 files_struct *fsp, const void *data,
4457 size_t n, off_t offset)
4459 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4463 DBG_ERR("Failed to fetch fsp extension");
4467 switch (fio->config->rsrc) {
4468 case FRUIT_RSRC_STREAM:
4469 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4472 case FRUIT_RSRC_ADFILE:
4473 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4476 case FRUIT_RSRC_XATTR:
4477 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4481 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4488 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4489 files_struct *fsp, const void *data,
4490 size_t n, off_t offset)
4492 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4495 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4496 fsp_str_dbg(fsp), (intmax_t)offset, n);
4499 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4502 if (fio->type == ADOUBLE_META) {
4503 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4505 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4508 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4512 struct fruit_pwrite_state {
4514 struct vfs_aio_state vfs_aio_state;
4517 static void fruit_pwrite_done(struct tevent_req *subreq);
4519 static struct tevent_req *fruit_pwrite_send(
4520 struct vfs_handle_struct *handle,
4521 TALLOC_CTX *mem_ctx,
4522 struct tevent_context *ev,
4523 struct files_struct *fsp,
4525 size_t n, off_t offset)
4527 struct tevent_req *req = NULL;
4528 struct tevent_req *subreq = NULL;
4529 struct fruit_pwrite_state *state = NULL;
4530 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4532 req = tevent_req_create(mem_ctx, &state,
4533 struct fruit_pwrite_state);
4538 if (fruit_must_handle_aio_stream(fio)) {
4539 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4540 if (state->nwritten != n) {
4541 if (state->nwritten != -1) {
4544 tevent_req_error(req, errno);
4545 return tevent_req_post(req, ev);
4547 tevent_req_done(req);
4548 return tevent_req_post(req, ev);
4551 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4553 if (tevent_req_nomem(req, subreq)) {
4554 return tevent_req_post(req, ev);
4556 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4560 static void fruit_pwrite_done(struct tevent_req *subreq)
4562 struct tevent_req *req = tevent_req_callback_data(
4563 subreq, struct tevent_req);
4564 struct fruit_pwrite_state *state = tevent_req_data(
4565 req, struct fruit_pwrite_state);
4567 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4568 TALLOC_FREE(subreq);
4570 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4573 tevent_req_done(req);
4576 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4577 struct vfs_aio_state *vfs_aio_state)
4579 struct fruit_pwrite_state *state = tevent_req_data(
4580 req, struct fruit_pwrite_state);
4582 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4586 *vfs_aio_state = state->vfs_aio_state;
4587 return state->nwritten;
4591 * Helper to stat/lstat the base file of an smb_fname.
4593 static int fruit_stat_base(vfs_handle_struct *handle,
4594 struct smb_filename *smb_fname,
4597 char *tmp_stream_name;
4600 tmp_stream_name = smb_fname->stream_name;
4601 smb_fname->stream_name = NULL;
4603 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4605 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4607 smb_fname->stream_name = tmp_stream_name;
4611 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4612 struct smb_filename *smb_fname,
4618 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4620 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4626 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4627 struct smb_filename *smb_fname,
4630 struct adouble *ad = NULL;
4632 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4634 DBG_INFO("fruit_stat_meta %s: %s\n",
4635 smb_fname_str_dbg(smb_fname), strerror(errno));
4641 /* Populate the stat struct with info from the base file. */
4642 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4645 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4646 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4647 smb_fname->stream_name);
4651 static int fruit_stat_meta(vfs_handle_struct *handle,
4652 struct smb_filename *smb_fname,
4655 struct fruit_config_data *config = NULL;
4658 SMB_VFS_HANDLE_GET_DATA(handle, config,
4659 struct fruit_config_data, return -1);
4661 switch (config->meta) {
4662 case FRUIT_META_STREAM:
4663 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4666 case FRUIT_META_NETATALK:
4667 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4671 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4678 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4679 struct smb_filename *smb_fname,
4682 struct adouble *ad = NULL;
4685 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4691 /* Populate the stat struct with info from the base file. */
4692 ret = fruit_stat_base(handle, smb_fname, follow_links);
4698 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4699 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4700 smb_fname->stream_name);
4705 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4706 struct smb_filename *smb_fname,
4712 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4714 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4720 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4721 struct smb_filename *smb_fname,
4724 #ifdef HAVE_ATTROPEN
4728 /* Populate the stat struct with info from the base file. */
4729 ret = fruit_stat_base(handle, smb_fname, follow_links);
4734 fd = attropen(smb_fname->base_name,
4735 AFPRESOURCE_EA_NETATALK,
4741 ret = sys_fstat(fd, &smb_fname->st, false);
4744 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4745 AFPRESOURCE_EA_NETATALK);
4751 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4752 smb_fname->stream_name);
4762 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4763 struct smb_filename *smb_fname,
4766 struct fruit_config_data *config = NULL;
4769 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4771 SMB_VFS_HANDLE_GET_DATA(handle, config,
4772 struct fruit_config_data, return -1);
4774 switch (config->rsrc) {
4775 case FRUIT_RSRC_STREAM:
4776 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4779 case FRUIT_RSRC_XATTR:
4780 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4783 case FRUIT_RSRC_ADFILE:
4784 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4788 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4795 static int fruit_stat(vfs_handle_struct *handle,
4796 struct smb_filename *smb_fname)
4800 DEBUG(10, ("fruit_stat called for %s\n",
4801 smb_fname_str_dbg(smb_fname)));
4803 if (!is_ntfs_stream_smb_fname(smb_fname)
4804 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4805 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4807 update_btime(handle, smb_fname);
4813 * Note if lp_posix_paths() is true, we can never
4814 * get here as is_ntfs_stream_smb_fname() is
4815 * always false. So we never need worry about
4816 * not following links here.
4819 if (is_afpinfo_stream(smb_fname)) {
4820 rc = fruit_stat_meta(handle, smb_fname, true);
4821 } else if (is_afpresource_stream(smb_fname)) {
4822 rc = fruit_stat_rsrc(handle, smb_fname, true);
4824 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4828 update_btime(handle, smb_fname);
4829 smb_fname->st.st_ex_mode &= ~S_IFMT;
4830 smb_fname->st.st_ex_mode |= S_IFREG;
4831 smb_fname->st.st_ex_blocks =
4832 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4837 static int fruit_lstat(vfs_handle_struct *handle,
4838 struct smb_filename *smb_fname)
4842 DEBUG(10, ("fruit_lstat called for %s\n",
4843 smb_fname_str_dbg(smb_fname)));
4845 if (!is_ntfs_stream_smb_fname(smb_fname)
4846 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4847 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4849 update_btime(handle, smb_fname);
4854 if (is_afpinfo_stream(smb_fname)) {
4855 rc = fruit_stat_meta(handle, smb_fname, false);
4856 } else if (is_afpresource_stream(smb_fname)) {
4857 rc = fruit_stat_rsrc(handle, smb_fname, false);
4859 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4863 update_btime(handle, smb_fname);
4864 smb_fname->st.st_ex_mode &= ~S_IFMT;
4865 smb_fname->st.st_ex_mode |= S_IFREG;
4866 smb_fname->st.st_ex_blocks =
4867 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4872 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4874 SMB_STRUCT_STAT *sbuf)
4876 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4879 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4881 SMB_STRUCT_STAT *sbuf)
4885 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4890 *sbuf = fsp->base_fsp->fsp_name->st;
4891 sbuf->st_ex_size = AFP_INFO_SIZE;
4892 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4897 static int fruit_fstat_meta(vfs_handle_struct *handle,
4899 SMB_STRUCT_STAT *sbuf,
4904 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4906 switch (fio->config->meta) {
4907 case FRUIT_META_STREAM:
4908 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4911 case FRUIT_META_NETATALK:
4912 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4916 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4920 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4924 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4926 SMB_STRUCT_STAT *sbuf)
4928 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4931 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4933 SMB_STRUCT_STAT *sbuf)
4935 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4938 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4940 SMB_STRUCT_STAT *sbuf)
4942 struct adouble *ad = NULL;
4945 /* Populate the stat struct with info from the base file. */
4946 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4951 ad = ad_get(talloc_tos(), handle,
4952 fsp->base_fsp->fsp_name,
4955 DBG_ERR("ad_get [%s] failed [%s]\n",
4956 fsp_str_dbg(fsp), strerror(errno));
4960 *sbuf = fsp->base_fsp->fsp_name->st;
4961 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4962 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4968 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4969 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4973 switch (fio->config->rsrc) {
4974 case FRUIT_RSRC_STREAM:
4975 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4978 case FRUIT_RSRC_ADFILE:
4979 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4982 case FRUIT_RSRC_XATTR:
4983 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4987 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4994 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4995 SMB_STRUCT_STAT *sbuf)
4997 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5001 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5004 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
5006 if (fio->type == ADOUBLE_META) {
5007 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
5009 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
5013 sbuf->st_ex_mode &= ~S_IFMT;
5014 sbuf->st_ex_mode |= S_IFREG;
5015 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
5018 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
5019 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
5023 static NTSTATUS delete_invalid_meta_stream(
5024 vfs_handle_struct *handle,
5025 const struct smb_filename *smb_fname,
5026 TALLOC_CTX *mem_ctx,
5027 unsigned int *pnum_streams,
5028 struct stream_struct **pstreams)
5030 struct smb_filename *sname = NULL;
5034 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
5036 return NT_STATUS_INTERNAL_ERROR;
5039 sname = synthetic_smb_fname(talloc_tos(),
5040 smb_fname->base_name,
5041 AFPINFO_STREAM_NAME,
5043 if (sname == NULL) {
5044 return NT_STATUS_NO_MEMORY;
5047 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5050 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5051 return map_nt_error_from_unix(errno);
5054 return NT_STATUS_OK;
5057 static NTSTATUS fruit_streaminfo_meta_stream(
5058 vfs_handle_struct *handle,
5059 struct files_struct *fsp,
5060 const struct smb_filename *smb_fname,
5061 TALLOC_CTX *mem_ctx,
5062 unsigned int *pnum_streams,
5063 struct stream_struct **pstreams)
5065 struct stream_struct *stream = *pstreams;
5066 unsigned int num_streams = *pnum_streams;
5067 struct smb_filename *sname = NULL;
5068 char *full_name = NULL;
5070 struct share_mode_lock *lck = NULL;
5071 struct file_id id = {0};
5072 bool delete_on_close_set;
5078 for (i = 0; i < num_streams; i++) {
5079 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5084 if (i == num_streams) {
5085 return NT_STATUS_OK;
5088 if (stream[i].size != AFP_INFO_SIZE) {
5089 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5090 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5092 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5093 pnum_streams, pstreams);
5097 * Now check if there's a delete-on-close pending on the stream. If so,
5098 * hide the stream. This behaviour was verified against a macOS 10.12
5102 sname = synthetic_smb_fname(talloc_tos(),
5103 smb_fname->base_name,
5104 AFPINFO_STREAM_NAME,
5106 if (sname == NULL) {
5107 status = NT_STATUS_NO_MEMORY;
5111 ret = SMB_VFS_NEXT_STAT(handle, sname);
5113 status = map_nt_error_from_unix(errno);
5117 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5119 lck = get_existing_share_mode_lock(talloc_tos(), id);
5121 status = NT_STATUS_OK;
5125 full_name = talloc_asprintf(talloc_tos(),
5129 if (full_name == NULL) {
5130 status = NT_STATUS_NO_MEMORY;
5134 status = file_name_hash(handle->conn, full_name, &name_hash);
5135 if (!NT_STATUS_IS_OK(status)) {
5139 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5140 if (delete_on_close_set) {
5141 ok = del_fruit_stream(mem_ctx,
5146 status = NT_STATUS_INTERNAL_ERROR;
5151 status = NT_STATUS_OK;
5156 TALLOC_FREE(full_name);
5160 static NTSTATUS fruit_streaminfo_meta_netatalk(
5161 vfs_handle_struct *handle,
5162 struct files_struct *fsp,
5163 const struct smb_filename *smb_fname,
5164 TALLOC_CTX *mem_ctx,
5165 unsigned int *pnum_streams,
5166 struct stream_struct **pstreams)
5168 struct stream_struct *stream = *pstreams;
5169 unsigned int num_streams = *pnum_streams;
5170 struct adouble *ad = NULL;
5175 /* Remove the Netatalk xattr from the list */
5176 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5177 ":" NETATALK_META_XATTR ":$DATA");
5179 return NT_STATUS_NO_MEMORY;
5183 * Check if there's a AFPINFO_STREAM from the VFS streams
5184 * backend and if yes, remove it from the list
5186 for (i = 0; i < num_streams; i++) {
5187 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5192 if (i < num_streams) {
5193 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5194 smb_fname_str_dbg(smb_fname));
5196 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5199 return NT_STATUS_INTERNAL_ERROR;
5203 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5205 return NT_STATUS_OK;
5208 is_fi_empty = ad_empty_finderinfo(ad);
5212 return NT_STATUS_OK;
5215 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5216 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5217 smb_roundup(handle->conn, AFP_INFO_SIZE));
5219 return NT_STATUS_NO_MEMORY;
5222 return NT_STATUS_OK;
5225 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5226 struct files_struct *fsp,
5227 const struct smb_filename *smb_fname,
5228 TALLOC_CTX *mem_ctx,
5229 unsigned int *pnum_streams,
5230 struct stream_struct **pstreams)
5232 struct fruit_config_data *config = NULL;
5235 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5236 return NT_STATUS_INTERNAL_ERROR);
5238 switch (config->meta) {
5239 case FRUIT_META_NETATALK:
5240 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5241 mem_ctx, pnum_streams,
5245 case FRUIT_META_STREAM:
5246 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5247 mem_ctx, pnum_streams,
5252 return NT_STATUS_INTERNAL_ERROR;
5258 static NTSTATUS fruit_streaminfo_rsrc_stream(
5259 vfs_handle_struct *handle,
5260 struct files_struct *fsp,
5261 const struct smb_filename *smb_fname,
5262 TALLOC_CTX *mem_ctx,
5263 unsigned int *pnum_streams,
5264 struct stream_struct **pstreams)
5268 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5270 DBG_ERR("Filtering resource stream failed\n");
5271 return NT_STATUS_INTERNAL_ERROR;
5273 return NT_STATUS_OK;
5276 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5277 vfs_handle_struct *handle,
5278 struct files_struct *fsp,
5279 const struct smb_filename *smb_fname,
5280 TALLOC_CTX *mem_ctx,
5281 unsigned int *pnum_streams,
5282 struct stream_struct **pstreams)
5286 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5288 DBG_ERR("Filtering resource stream failed\n");
5289 return NT_STATUS_INTERNAL_ERROR;
5291 return NT_STATUS_OK;
5294 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5295 vfs_handle_struct *handle,
5296 struct files_struct *fsp,
5297 const struct smb_filename *smb_fname,
5298 TALLOC_CTX *mem_ctx,
5299 unsigned int *pnum_streams,
5300 struct stream_struct **pstreams)
5302 struct stream_struct *stream = *pstreams;
5303 unsigned int num_streams = *pnum_streams;
5304 struct adouble *ad = NULL;
5310 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5311 * and if yes, remove it from the list
5313 for (i = 0; i < num_streams; i++) {
5314 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5319 if (i < num_streams) {
5320 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5321 smb_fname_str_dbg(smb_fname));
5323 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5324 AFPRESOURCE_STREAM);
5326 return NT_STATUS_INTERNAL_ERROR;
5330 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5332 return NT_STATUS_OK;
5335 rlen = ad_getentrylen(ad, ADEID_RFORK);
5339 return NT_STATUS_OK;
5342 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5343 AFPRESOURCE_STREAM_NAME, rlen,
5344 smb_roundup(handle->conn, rlen));
5346 return NT_STATUS_NO_MEMORY;
5349 return NT_STATUS_OK;
5352 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5353 struct files_struct *fsp,
5354 const struct smb_filename *smb_fname,
5355 TALLOC_CTX *mem_ctx,
5356 unsigned int *pnum_streams,
5357 struct stream_struct **pstreams)
5359 struct fruit_config_data *config = NULL;
5362 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5363 return NT_STATUS_INTERNAL_ERROR);
5365 switch (config->rsrc) {
5366 case FRUIT_RSRC_STREAM:
5367 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5368 mem_ctx, pnum_streams,
5372 case FRUIT_RSRC_XATTR:
5373 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5374 mem_ctx, pnum_streams,
5378 case FRUIT_RSRC_ADFILE:
5379 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5380 mem_ctx, pnum_streams,
5385 return NT_STATUS_INTERNAL_ERROR;
5391 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5392 struct files_struct *fsp,
5393 const struct smb_filename *smb_fname,
5394 TALLOC_CTX *mem_ctx,
5395 unsigned int *pnum_streams,
5396 struct stream_struct **pstreams)
5398 struct fruit_config_data *config = NULL;
5401 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5402 return NT_STATUS_UNSUCCESSFUL);
5404 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5406 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5407 pnum_streams, pstreams);
5408 if (!NT_STATUS_IS_OK(status)) {
5412 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5413 mem_ctx, pnum_streams, pstreams);
5414 if (!NT_STATUS_IS_OK(status)) {
5418 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5419 mem_ctx, pnum_streams, pstreams);
5420 if (!NT_STATUS_IS_OK(status)) {
5424 return NT_STATUS_OK;
5427 static int fruit_ntimes(vfs_handle_struct *handle,
5428 const struct smb_filename *smb_fname,
5429 struct smb_file_time *ft)
5432 struct adouble *ad = NULL;
5433 struct fruit_config_data *config = NULL;
5435 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5438 if ((config->meta != FRUIT_META_NETATALK) ||
5439 null_timespec(ft->create_time))
5441 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5444 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5445 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5447 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5452 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5453 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5455 rc = ad_set(ad, smb_fname);
5461 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5464 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5467 static int fruit_fallocate(struct vfs_handle_struct *handle,
5468 struct files_struct *fsp,
5473 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5476 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5479 /* Let the pwrite code path handle it. */
5484 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5485 struct files_struct *fsp,
5489 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5492 #ifdef HAVE_ATTROPEN
5493 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5498 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5499 struct files_struct *fsp,
5503 struct adouble *ad = NULL;
5506 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5508 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5509 fsp_str_dbg(fsp), strerror(errno));
5513 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5515 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5521 ad_setentrylen(ad, ADEID_RFORK, offset);
5523 rc = ad_fset(ad, fsp);
5525 DBG_ERR("ad_fset [%s] failed [%s]\n",
5526 fsp_str_dbg(fsp), strerror(errno));
5535 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5536 struct files_struct *fsp,
5540 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5543 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5546 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5547 struct files_struct *fsp,
5550 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5554 DBG_ERR("Failed to fetch fsp extension");
5558 switch (fio->config->rsrc) {
5559 case FRUIT_RSRC_XATTR:
5560 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5563 case FRUIT_RSRC_ADFILE:
5564 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5567 case FRUIT_RSRC_STREAM:
5568 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5572 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5580 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5581 struct files_struct *fsp,
5585 DBG_WARNING("ftruncate %s to %jd",
5586 fsp_str_dbg(fsp), (intmax_t)offset);
5587 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5592 /* OS X returns success but does nothing */
5593 DBG_INFO("ignoring ftruncate %s to %jd\n",
5594 fsp_str_dbg(fsp), (intmax_t)offset);
5598 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5599 struct files_struct *fsp,
5602 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5605 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5610 global_fruit_config.nego_aapl &&
5611 is_ntfs_stream_smb_fname(fsp->fsp_name) &&
5612 !is_ntfs_default_stream_smb_fname(fsp->fsp_name))
5614 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5616 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5619 if (fio->type == ADOUBLE_META) {
5620 ret = fruit_ftruncate_meta(handle, fsp, offset);
5622 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5625 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5629 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5630 struct smb_request *req,
5631 uint16_t root_dir_fid,
5632 struct smb_filename *smb_fname,
5633 uint32_t access_mask,
5634 uint32_t share_access,
5635 uint32_t create_disposition,
5636 uint32_t create_options,
5637 uint32_t file_attributes,
5638 uint32_t oplock_request,
5639 struct smb2_lease *lease,
5640 uint64_t allocation_size,
5641 uint32_t private_flags,
5642 struct security_descriptor *sd,
5643 struct ea_list *ea_list,
5644 files_struct **result,
5646 const struct smb2_create_blobs *in_context_blobs,
5647 struct smb2_create_blobs *out_context_blobs)
5650 struct fruit_config_data *config = NULL;
5651 files_struct *fsp = NULL;
5653 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5654 if (!NT_STATUS_IS_OK(status)) {
5658 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5659 return NT_STATUS_UNSUCCESSFUL);
5661 status = SMB_VFS_NEXT_CREATE_FILE(
5662 handle, req, root_dir_fid, smb_fname,
5663 access_mask, share_access,
5664 create_disposition, create_options,
5665 file_attributes, oplock_request,
5667 allocation_size, private_flags,
5668 sd, ea_list, result,
5669 pinfo, in_context_blobs, out_context_blobs);
5670 if (!NT_STATUS_IS_OK(status)) {
5676 if (global_fruit_config.nego_aapl) {
5677 if (config->posix_rename && fsp->is_directory) {
5679 * Enable POSIX directory rename behaviour
5681 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5686 * If this is a plain open for existing files, opening an 0
5687 * byte size resource fork MUST fail with
5688 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5690 * Cf the vfs_fruit torture tests in test_rfork_create().
5692 if (is_afpresource_stream(fsp->fsp_name) &&
5693 create_disposition == FILE_OPEN)
5695 if (fsp->fsp_name->st.st_ex_size == 0) {
5696 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5701 if (is_ntfs_stream_smb_fname(smb_fname)
5702 || fsp->is_directory) {
5706 if (config->locking == FRUIT_LOCKING_NETATALK) {
5707 status = fruit_check_access(
5710 map_share_mode_to_deny_mode(share_access, 0));
5711 if (!NT_STATUS_IS_OK(status)) {
5719 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5722 close_file(req, fsp, ERROR_CLOSE);
5723 *result = fsp = NULL;
5729 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5730 const struct smb_filename *fname,
5731 TALLOC_CTX *mem_ctx,
5732 struct readdir_attr_data **pattr_data)
5734 struct fruit_config_data *config = NULL;
5735 struct readdir_attr_data *attr_data;
5738 SMB_VFS_HANDLE_GET_DATA(handle, config,
5739 struct fruit_config_data,
5740 return NT_STATUS_UNSUCCESSFUL);
5742 if (!global_fruit_config.nego_aapl) {
5743 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5746 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5748 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5749 if (*pattr_data == NULL) {
5750 return NT_STATUS_UNSUCCESSFUL;
5752 attr_data = *pattr_data;
5753 attr_data->type = RDATTR_AAPL;
5756 * Mac metadata: compressed FinderInfo, resource fork length
5759 status = readdir_attr_macmeta(handle, fname, attr_data);
5760 if (!NT_STATUS_IS_OK(status)) {
5762 * Error handling is tricky: if we return failure from
5763 * this function, the corresponding directory entry
5764 * will to be passed to the client, so we really just
5765 * want to error out on fatal errors.
5767 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5775 if (config->unix_info_enabled) {
5776 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5782 if (!config->readdir_attr_max_access) {
5783 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5785 status = smbd_calculate_access_mask(
5789 SEC_FLAG_MAXIMUM_ALLOWED,
5790 &attr_data->attr_data.aapl.max_access);
5791 if (!NT_STATUS_IS_OK(status)) {
5796 return NT_STATUS_OK;
5799 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5800 fname->base_name, nt_errstr(status)));
5801 TALLOC_FREE(*pattr_data);
5805 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5807 uint32_t security_info,
5808 TALLOC_CTX *mem_ctx,
5809 struct security_descriptor **ppdesc)
5812 struct security_ace ace;
5814 struct fruit_config_data *config;
5816 SMB_VFS_HANDLE_GET_DATA(handle, config,
5817 struct fruit_config_data,
5818 return NT_STATUS_UNSUCCESSFUL);
5820 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5822 if (!NT_STATUS_IS_OK(status)) {
5827 * Add MS NFS style ACEs with uid, gid and mode
5829 if (!global_fruit_config.nego_aapl) {
5830 return NT_STATUS_OK;
5832 if (!config->unix_info_enabled) {
5833 return NT_STATUS_OK;
5836 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5837 status = remove_virtual_nfs_aces(*ppdesc);
5838 if (!NT_STATUS_IS_OK(status)) {
5839 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5843 /* MS NFS style mode */
5844 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5845 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5846 status = security_descriptor_dacl_add(*ppdesc, &ace);
5847 if (!NT_STATUS_IS_OK(status)) {
5848 DEBUG(1,("failed to add MS NFS style ACE\n"));
5852 /* MS NFS style uid */
5853 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5854 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5855 status = security_descriptor_dacl_add(*ppdesc, &ace);
5856 if (!NT_STATUS_IS_OK(status)) {
5857 DEBUG(1,("failed to add MS NFS style ACE\n"));
5861 /* MS NFS style gid */
5862 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5863 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5864 status = security_descriptor_dacl_add(*ppdesc, &ace);
5865 if (!NT_STATUS_IS_OK(status)) {
5866 DEBUG(1,("failed to add MS NFS style ACE\n"));
5870 return NT_STATUS_OK;
5873 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5875 uint32_t security_info_sent,
5876 const struct security_descriptor *orig_psd)
5880 mode_t ms_nfs_mode = 0;
5882 struct security_descriptor *psd = NULL;
5883 uint32_t orig_num_aces = 0;
5885 if (orig_psd->dacl != NULL) {
5886 orig_num_aces = orig_psd->dacl->num_aces;
5889 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5891 return NT_STATUS_NO_MEMORY;
5894 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5896 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5897 if (!NT_STATUS_IS_OK(status)) {
5898 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5904 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5905 * sent/present flags correctly now we've removed them.
5908 if (orig_num_aces != 0) {
5910 * Are there any ACE's left ?
5912 if (psd->dacl->num_aces == 0) {
5913 /* No - clear the DACL sent/present flags. */
5914 security_info_sent &= ~SECINFO_DACL;
5915 psd->type &= ~SEC_DESC_DACL_PRESENT;
5919 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5920 if (!NT_STATUS_IS_OK(status)) {
5921 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5927 if (fsp->fh->fd != -1) {
5928 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5930 result = SMB_VFS_CHMOD(fsp->conn,
5936 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5937 result, (unsigned)ms_nfs_mode,
5939 status = map_nt_error_from_unix(errno);
5946 return NT_STATUS_OK;
5949 static struct vfs_offload_ctx *fruit_offload_ctx;
5951 struct fruit_offload_read_state {
5952 struct vfs_handle_struct *handle;
5953 struct tevent_context *ev;
5959 static void fruit_offload_read_done(struct tevent_req *subreq);
5961 static struct tevent_req *fruit_offload_read_send(
5962 TALLOC_CTX *mem_ctx,
5963 struct tevent_context *ev,
5964 struct vfs_handle_struct *handle,
5971 struct tevent_req *req = NULL;
5972 struct tevent_req *subreq = NULL;
5973 struct fruit_offload_read_state *state = NULL;
5975 req = tevent_req_create(mem_ctx, &state,
5976 struct fruit_offload_read_state);
5980 *state = (struct fruit_offload_read_state) {
5987 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5988 fsctl, ttl, offset, to_copy);
5989 if (tevent_req_nomem(subreq, req)) {
5990 return tevent_req_post(req, ev);
5992 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5996 static void fruit_offload_read_done(struct tevent_req *subreq)
5998 struct tevent_req *req = tevent_req_callback_data(
5999 subreq, struct tevent_req);
6000 struct fruit_offload_read_state *state = tevent_req_data(
6001 req, struct fruit_offload_read_state);
6004 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
6008 TALLOC_FREE(subreq);
6009 if (tevent_req_nterror(req, status)) {
6013 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
6014 tevent_req_done(req);
6018 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
6019 &fruit_offload_ctx);
6020 if (tevent_req_nterror(req, status)) {
6024 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
6027 if (tevent_req_nterror(req, status)) {
6031 tevent_req_done(req);
6035 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6036 struct vfs_handle_struct *handle,
6037 TALLOC_CTX *mem_ctx,
6040 struct fruit_offload_read_state *state = tevent_req_data(
6041 req, struct fruit_offload_read_state);
6044 if (tevent_req_is_nterror(req, &status)) {
6045 tevent_req_received(req);
6049 token->length = state->token.length;
6050 token->data = talloc_move(mem_ctx, &state->token.data);
6052 tevent_req_received(req);
6053 return NT_STATUS_OK;
6056 struct fruit_offload_write_state {
6057 struct vfs_handle_struct *handle;
6059 struct files_struct *src_fsp;
6060 struct files_struct *dst_fsp;
6064 static void fruit_offload_write_done(struct tevent_req *subreq);
6065 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6066 TALLOC_CTX *mem_ctx,
6067 struct tevent_context *ev,
6070 off_t transfer_offset,
6071 struct files_struct *dest_fsp,
6075 struct tevent_req *req, *subreq;
6076 struct fruit_offload_write_state *state;
6078 struct fruit_config_data *config;
6079 off_t src_off = transfer_offset;
6080 files_struct *src_fsp = NULL;
6081 off_t to_copy = num;
6082 bool copyfile_enabled = false;
6084 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6085 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6087 SMB_VFS_HANDLE_GET_DATA(handle, config,
6088 struct fruit_config_data,
6091 req = tevent_req_create(mem_ctx, &state,
6092 struct fruit_offload_write_state);
6096 state->handle = handle;
6097 state->dst_fsp = dest_fsp;
6100 case FSCTL_SRV_COPYCHUNK:
6101 case FSCTL_SRV_COPYCHUNK_WRITE:
6102 copyfile_enabled = config->copyfile_enabled;
6109 * Check if this a OS X copyfile style copychunk request with
6110 * a requested chunk count of 0 that was translated to a
6111 * offload_write_send VFS call overloading the parameters src_off
6112 * = dest_off = num = 0.
6114 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6115 status = vfs_offload_token_db_fetch_fsp(
6116 fruit_offload_ctx, token, &src_fsp);
6117 if (tevent_req_nterror(req, status)) {
6118 return tevent_req_post(req, ev);
6120 state->src_fsp = src_fsp;
6122 status = vfs_stat_fsp(src_fsp);
6123 if (tevent_req_nterror(req, status)) {
6124 return tevent_req_post(req, ev);
6127 to_copy = src_fsp->fsp_name->st.st_ex_size;
6128 state->is_copyfile = true;
6131 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6140 if (tevent_req_nomem(subreq, req)) {
6141 return tevent_req_post(req, ev);
6144 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6148 static void fruit_offload_write_done(struct tevent_req *subreq)
6150 struct tevent_req *req = tevent_req_callback_data(
6151 subreq, struct tevent_req);
6152 struct fruit_offload_write_state *state = tevent_req_data(
6153 req, struct fruit_offload_write_state);
6155 unsigned int num_streams = 0;
6156 struct stream_struct *streams = NULL;
6158 struct smb_filename *src_fname_tmp = NULL;
6159 struct smb_filename *dst_fname_tmp = NULL;
6161 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6164 TALLOC_FREE(subreq);
6165 if (tevent_req_nterror(req, status)) {
6169 if (!state->is_copyfile) {
6170 tevent_req_done(req);
6175 * Now copy all remaining streams. We know the share supports
6176 * streams, because we're in vfs_fruit. We don't do this async
6177 * because streams are few and small.
6179 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6180 state->src_fsp->fsp_name,
6181 req, &num_streams, &streams);
6182 if (tevent_req_nterror(req, status)) {
6186 if (num_streams == 1) {
6187 /* There is always one stream, ::$DATA. */
6188 tevent_req_done(req);
6192 for (i = 0; i < num_streams; i++) {
6193 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6194 __func__, streams[i].name, (size_t)streams[i].size));
6196 src_fname_tmp = synthetic_smb_fname(
6198 state->src_fsp->fsp_name->base_name,
6201 state->src_fsp->fsp_name->flags);
6202 if (tevent_req_nomem(src_fname_tmp, req)) {
6206 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6207 TALLOC_FREE(src_fname_tmp);
6211 dst_fname_tmp = synthetic_smb_fname(
6213 state->dst_fsp->fsp_name->base_name,
6216 state->dst_fsp->fsp_name->flags);
6217 if (tevent_req_nomem(dst_fname_tmp, req)) {
6218 TALLOC_FREE(src_fname_tmp);
6222 status = copy_file(req,
6223 state->handle->conn,
6226 OPENX_FILE_CREATE_IF_NOT_EXIST,
6228 if (!NT_STATUS_IS_OK(status)) {
6229 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6230 smb_fname_str_dbg(src_fname_tmp),
6231 smb_fname_str_dbg(dst_fname_tmp),
6232 nt_errstr(status)));
6233 TALLOC_FREE(src_fname_tmp);
6234 TALLOC_FREE(dst_fname_tmp);
6235 tevent_req_nterror(req, status);
6239 TALLOC_FREE(src_fname_tmp);
6240 TALLOC_FREE(dst_fname_tmp);
6243 TALLOC_FREE(streams);
6244 TALLOC_FREE(src_fname_tmp);
6245 TALLOC_FREE(dst_fname_tmp);
6246 tevent_req_done(req);
6249 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6250 struct tevent_req *req,
6253 struct fruit_offload_write_state *state = tevent_req_data(
6254 req, struct fruit_offload_write_state);
6257 if (tevent_req_is_nterror(req, &status)) {
6258 DEBUG(1, ("server side copy chunk failed: %s\n",
6259 nt_errstr(status)));
6261 tevent_req_received(req);
6265 *copied = state->copied;
6266 tevent_req_received(req);
6268 return NT_STATUS_OK;
6271 static char *fruit_get_bandsize_line(char **lines, int numlines)
6274 static bool re_initialized = false;
6278 if (!re_initialized) {
6279 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6283 re_initialized = true;
6286 for (i = 0; i < numlines; i++) {
6287 regmatch_t matches[1];
6289 ret = regexec(&re, lines[i], 1, matches, 0);
6292 * Check if the match was on the last line, sa we want
6293 * the subsequent line.
6295 if (i + 1 == numlines) {
6298 return lines[i + 1];
6300 if (ret != REG_NOMATCH) {
6308 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6311 static bool re_initialized = false;
6312 regmatch_t matches[2];
6317 if (!re_initialized) {
6320 "<integer>\\([[:digit:]]*\\)</integer>$",
6325 re_initialized = true;
6328 ret = regexec(&re, line, 2, matches, 0);
6330 DBG_ERR("regex failed [%s]\n", line);
6334 line[matches[1].rm_eo] = '\0';
6336 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6340 *_band_size = (size_t)band_size;
6345 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6346 * "band-size" key and value.
6348 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6352 #define INFO_PLIST_MAX_SIZE 64*1024
6354 struct smb_filename *smb_fname = NULL;
6355 files_struct *fsp = NULL;
6356 uint8_t *file_data = NULL;
6357 char **lines = NULL;
6358 char *band_size_line = NULL;
6359 size_t plist_file_size;
6366 plist = talloc_asprintf(talloc_tos(),
6368 handle->conn->connectpath,
6370 if (plist == NULL) {
6375 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6376 if (smb_fname == NULL) {
6381 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6383 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6388 plist_file_size = smb_fname->st.st_ex_size;
6390 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6391 DBG_INFO("%s is too large, ignoring\n", plist);
6396 status = SMB_VFS_NEXT_CREATE_FILE(
6399 0, /* root_dir_fid */
6400 smb_fname, /* fname */
6401 FILE_GENERIC_READ, /* access_mask */
6402 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6403 FILE_OPEN, /* create_disposition */
6404 0, /* create_options */
6405 0, /* file_attributes */
6406 INTERNAL_OPEN_ONLY, /* oplock_request */
6408 0, /* allocation_size */
6409 0, /* private_flags */
6414 NULL, NULL); /* create context */
6415 if (!NT_STATUS_IS_OK(status)) {
6416 DBG_INFO("Opening [%s] failed [%s]\n",
6417 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6422 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6423 if (file_data == NULL) {
6428 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6429 if (nread != plist_file_size) {
6430 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6431 fsp_str_dbg(fsp), nread, plist_file_size);
6437 status = close_file(NULL, fsp, NORMAL_CLOSE);
6439 if (!NT_STATUS_IS_OK(status)) {
6440 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6445 lines = file_lines_parse((char *)file_data,
6449 if (lines == NULL) {
6454 band_size_line = fruit_get_bandsize_line(lines, numlines);
6455 if (band_size_line == NULL) {
6456 DBG_ERR("Didn't find band-size key in [%s]\n",
6457 smb_fname_str_dbg(smb_fname));
6462 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6464 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6468 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6472 status = close_file(NULL, fsp, NORMAL_CLOSE);
6473 if (!NT_STATUS_IS_OK(status)) {
6474 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6479 TALLOC_FREE(smb_fname);
6480 TALLOC_FREE(file_data);
6485 struct fruit_disk_free_state {
6489 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6494 struct smb_filename *bands_dir = NULL;
6496 struct dirent *e = NULL;
6500 path = talloc_asprintf(talloc_tos(),
6502 handle->conn->connectpath,
6508 bands_dir = synthetic_smb_fname(talloc_tos(),
6514 if (bands_dir == NULL) {
6518 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6520 TALLOC_FREE(bands_dir);
6526 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6528 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6530 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6536 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6538 TALLOC_FREE(bands_dir);
6542 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6544 TALLOC_FREE(bands_dir);
6550 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6551 struct fruit_disk_free_state *state,
6556 size_t sparsebundle_strlen = strlen("sparsebundle");
6557 size_t bandsize = 0;
6561 p = strstr(e->d_name, "sparsebundle");
6566 if (p[sparsebundle_strlen] != '\0') {
6570 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6572 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6575 * Beware of race conditions: this may be an uninitialized
6576 * Info.plist that a client is just creating. We don't want let
6577 * this to trigger complete failure.
6579 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6583 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6586 * Beware of race conditions: this may be a backup sparsebundle
6587 * in an early stage lacking a bands subdirectory. We don't want
6588 * let this to trigger complete failure.
6590 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6594 if (bandsize > SIZE_MAX/nbands) {
6595 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6599 tm_size = bandsize * nbands;
6601 if (state->total_size + tm_size < state->total_size) {
6602 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6607 state->total_size += tm_size;
6609 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6610 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6616 * Calculate used size of a TimeMachine volume
6618 * This assumes that the volume is used only for TimeMachine.
6620 * - readdir(basedir of share), then
6621 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6622 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6623 * - count band files in "\1.sparsebundle/bands/"
6624 * - calculate used size of all bands: band_count * band_size
6626 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6627 const struct smb_filename *smb_fname,
6632 struct fruit_config_data *config = NULL;
6633 struct fruit_disk_free_state state = {0};
6635 struct dirent *e = NULL;
6641 SMB_VFS_HANDLE_GET_DATA(handle, config,
6642 struct fruit_config_data,
6645 if (!config->time_machine ||
6646 config->time_machine_max_size == 0)
6648 return SMB_VFS_NEXT_DISK_FREE(handle,
6655 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6660 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6662 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6664 ok = fruit_tmsize_do_dirent(handle, &state, e);
6666 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6671 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6676 dsize = config->time_machine_max_size / 512;
6677 dfree = dsize - (state.total_size / 512);
6678 if (dfree > dsize) {
6688 static struct vfs_fn_pointers vfs_fruit_fns = {
6689 .connect_fn = fruit_connect,
6690 .disk_free_fn = fruit_disk_free,
6692 /* File operations */
6693 .chmod_fn = fruit_chmod,
6694 .chown_fn = fruit_chown,
6695 .unlink_fn = fruit_unlink,
6696 .rename_fn = fruit_rename,
6697 .rmdir_fn = fruit_rmdir,
6698 .open_fn = fruit_open,
6699 .pread_fn = fruit_pread,
6700 .pwrite_fn = fruit_pwrite,
6701 .pread_send_fn = fruit_pread_send,
6702 .pread_recv_fn = fruit_pread_recv,
6703 .pwrite_send_fn = fruit_pwrite_send,
6704 .pwrite_recv_fn = fruit_pwrite_recv,
6705 .stat_fn = fruit_stat,
6706 .lstat_fn = fruit_lstat,
6707 .fstat_fn = fruit_fstat,
6708 .streaminfo_fn = fruit_streaminfo,
6709 .ntimes_fn = fruit_ntimes,
6710 .ftruncate_fn = fruit_ftruncate,
6711 .fallocate_fn = fruit_fallocate,
6712 .create_file_fn = fruit_create_file,
6713 .readdir_attr_fn = fruit_readdir_attr,
6714 .offload_read_send_fn = fruit_offload_read_send,
6715 .offload_read_recv_fn = fruit_offload_read_recv,
6716 .offload_write_send_fn = fruit_offload_write_send,
6717 .offload_write_recv_fn = fruit_offload_write_recv,
6719 /* NT ACL operations */
6720 .fget_nt_acl_fn = fruit_fget_nt_acl,
6721 .fset_nt_acl_fn = fruit_fset_nt_acl,
6725 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6727 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6729 if (!NT_STATUS_IS_OK(ret)) {
6733 vfs_fruit_debug_level = debug_add_class("fruit");
6734 if (vfs_fruit_debug_level == -1) {
6735 vfs_fruit_debug_level = DBGC_VFS;
6736 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6739 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6740 "vfs_fruit_init","fruit",vfs_fruit_debug_level));