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);
1060 * Convert from Apple's ._ file to Netatalk
1062 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1063 * bytes containing packed xattrs.
1065 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1068 static int ad_convert(struct adouble *ad,
1069 const struct smb_filename *smb_fname)
1072 char *map = MAP_FAILED;
1076 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1077 ad_getentrylen(ad, ADEID_RFORK);
1079 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1080 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED,
1082 if (map == MAP_FAILED) {
1083 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1087 ok = ad_convert_xattr(ad, smb_fname, map);
1089 munmap(map, origlen);
1093 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1094 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1095 map + ad_getentryoff(ad, ADEID_RFORK),
1096 ad_getentrylen(ad, ADEID_RFORK));
1099 ad_setentryoff(ad, ADEID_RFORK,
1100 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1103 * FIXME: direct ftruncate(), but we don't have a fsp for the
1106 rc = ftruncate(ad->ad_fd, ad_getentryoff(ad, ADEID_RFORK)
1107 + ad_getentrylen(ad, ADEID_RFORK));
1109 munmap(map, origlen);
1113 rc = munmap(map, origlen);
1115 DBG_ERR("munmap failed: %s\n", strerror(errno));
1123 * Read and parse Netatalk AppleDouble metadata xattr
1125 static ssize_t ad_read_meta(struct adouble *ad,
1126 const struct smb_filename *smb_fname)
1132 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1134 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1135 AFPINFO_EA_NETATALK, ad->ad_data,
1141 if (errno == ENOATTR) {
1147 DEBUG(2, ("error reading meta xattr: %s\n",
1153 if (ealen != AD_DATASZ_XATTR) {
1154 DEBUG(2, ("bad size %zd\n", ealen));
1160 /* Now parse entries */
1161 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1163 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1169 if (!ad_getentryoff(ad, ADEID_FINDERI)
1170 || !ad_getentryoff(ad, ADEID_COMMENT)
1171 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1172 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1173 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1174 || !ad_getentryoff(ad, ADEID_PRIVINO)
1175 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1176 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1177 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1184 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1185 smb_fname->base_name, rc));
1189 if (errno == EINVAL) {
1191 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1199 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1203 #ifdef HAVE_ATTROPEN
1204 /* FIXME: direct Solaris xattr syscall */
1205 return attropen(smb_fname->base_name,
1206 AFPRESOURCE_EA_NETATALK, flags, mode);
1213 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1219 struct smb_filename *adp_smb_fname = NULL;
1221 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1226 fd = open(adp_smb_fname->base_name, flags, mode);
1227 TALLOC_FREE(adp_smb_fname);
1232 static int ad_open_rsrc(vfs_handle_struct *handle,
1233 const struct smb_filename *smb_fname,
1237 struct fruit_config_data *config = NULL;
1240 SMB_VFS_HANDLE_GET_DATA(handle, config,
1241 struct fruit_config_data, return -1);
1243 if (config->rsrc == FRUIT_RSRC_XATTR) {
1244 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1246 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1253 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1254 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1255 * for file IO on the ._ file.
1257 static int ad_open(vfs_handle_struct *handle,
1260 const struct smb_filename *smb_fname,
1266 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1267 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1269 if (ad->ad_type == ADOUBLE_META) {
1273 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1274 ad->ad_fd = fsp->fh->fd;
1275 ad->ad_opened = false;
1279 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1283 ad->ad_opened = true;
1286 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1287 smb_fname->base_name,
1288 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1293 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1298 /* FIXME: direct sys_fstat(), don't have an fsp */
1299 ret = sys_fstat(ad->ad_fd, &st,
1300 lp_fake_directory_create_times(
1301 SNUM(ad->ad_handle->conn)));
1306 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1307 return st.st_ex_size;
1310 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1311 const struct smb_filename *smb_fname)
1313 SMB_STRUCT_STAT sbuf;
1317 struct smb_filename *stream_name = NULL;
1318 files_struct *fsp = NULL;
1323 int saved_errno = 0;
1327 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1328 SNUM(ad->ad_handle->conn)));
1334 * AppleDouble file header content and size, two cases:
1336 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1337 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1339 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1341 size = sbuf.st_ex_size;
1342 if (size > talloc_array_length(ad->ad_data)) {
1343 if (size > AD_XATTR_MAX_HDR_SIZE) {
1344 size = AD_XATTR_MAX_HDR_SIZE;
1346 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1353 len = sys_pread(ad->ad_fd, ad->ad_data,
1354 talloc_array_length(ad->ad_data), 0);
1355 if (len != talloc_array_length(ad->ad_data)) {
1356 DBG_NOTICE("%s %s: bad size: %zd\n",
1357 smb_fname->base_name, strerror(errno), len);
1361 /* Now parse entries */
1362 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1364 DBG_ERR("invalid AppleDouble resource %s\n",
1365 smb_fname->base_name);
1370 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1371 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1372 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1373 DBG_ERR("invalid AppleDouble resource %s\n",
1374 smb_fname->base_name);
1379 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1384 * Try to fixup AppleDouble files created by OS X with xattrs
1385 * appended to the ADEID_FINDERI entry.
1388 ret = ad_convert(ad, smb_fname);
1390 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1396 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1400 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1401 if (len != AD_DATASZ_DOT_UND) {
1402 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1406 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1411 ai = afpinfo_new(talloc_tos());
1416 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1418 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1419 if (aiblob.data == NULL) {
1424 size = afpinfo_pack(ai, (char *)aiblob.data);
1426 if (size != AFP_INFO_SIZE) {
1430 stream_name = synthetic_smb_fname(talloc_tos(),
1431 smb_fname->base_name,
1435 if (stream_name == NULL) {
1436 data_blob_free(&aiblob);
1437 DBG_ERR("synthetic_smb_fname failed\n");
1441 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1443 status = SMB_VFS_CREATE_FILE(
1444 ad->ad_handle->conn, /* conn */
1446 0, /* root_dir_fid */
1447 stream_name, /* fname */
1448 FILE_GENERIC_WRITE, /* access_mask */
1449 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1450 FILE_OPEN_IF, /* create_disposition */
1451 0, /* create_options */
1452 0, /* file_attributes */
1453 INTERNAL_OPEN_ONLY, /* oplock_request */
1455 0, /* allocation_size */
1456 0, /* private_flags */
1461 NULL, NULL); /* create context */
1462 TALLOC_FREE(stream_name);
1463 if (!NT_STATUS_IS_OK(status)) {
1464 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1468 nwritten = SMB_VFS_PWRITE(fsp,
1472 if (nwritten == -1) {
1473 DBG_ERR("SMB_VFS_PWRITE failed\n");
1474 saved_errno = errno;
1475 close_file(NULL, fsp, ERROR_CLOSE);
1476 errno = saved_errno;
1480 status = close_file(NULL, fsp, NORMAL_CLOSE);
1481 if (!NT_STATUS_IS_OK(status)) {
1490 * Read and parse resource fork, either ._ AppleDouble file or xattr
1492 static ssize_t ad_read_rsrc(struct adouble *ad,
1493 const struct smb_filename *smb_fname)
1495 struct fruit_config_data *config = NULL;
1498 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1499 struct fruit_config_data, return -1);
1501 if (config->rsrc == FRUIT_RSRC_XATTR) {
1502 len = ad_read_rsrc_xattr(ad);
1504 len = ad_read_rsrc_adouble(ad, smb_fname);
1511 * Read and unpack an AppleDouble metadata xattr or resource
1513 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1515 switch (ad->ad_type) {
1517 return ad_read_meta(ad, smb_fname);
1519 return ad_read_rsrc(ad, smb_fname);
1525 static int adouble_destructor(struct adouble *ad)
1527 if ((ad->ad_fd != -1) && ad->ad_opened) {
1535 * Allocate a struct adouble without initialiing it
1537 * The struct is either hang of the fsp extension context or if fsp is
1540 * @param[in] ctx talloc context
1541 * @param[in] handle vfs handle
1542 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1544 * @return adouble handle
1546 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1547 adouble_type_t type)
1552 struct fruit_config_data *config;
1554 SMB_VFS_HANDLE_GET_DATA(handle, config,
1555 struct fruit_config_data, return NULL);
1559 adsize = AD_DATASZ_XATTR;
1562 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1563 adsize = AD_DATASZ_DOT_UND;
1570 ad = talloc_zero(ctx, struct adouble);
1577 ad->ad_data = talloc_zero_array(ad, char, adsize);
1578 if (ad->ad_data == NULL) {
1584 ad->ad_handle = handle;
1586 ad->ad_magic = AD_MAGIC;
1587 ad->ad_version = AD_VERSION;
1590 talloc_set_destructor(ad, adouble_destructor);
1600 * Allocate and initialize a new struct adouble
1602 * @param[in] ctx talloc context
1603 * @param[in] handle vfs handle
1604 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1606 * @return adouble handle, initialized
1608 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1609 adouble_type_t type)
1612 const struct ad_entry_order *eid;
1613 struct adouble *ad = NULL;
1614 struct fruit_config_data *config;
1615 time_t t = time(NULL);
1617 SMB_VFS_HANDLE_GET_DATA(handle, config,
1618 struct fruit_config_data, return NULL);
1622 eid = entry_order_meta_xattr;
1625 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1626 eid = entry_order_dot_und;
1628 eid = entry_order_rsrc_xattr;
1635 ad = ad_alloc(ctx, handle, type);
1641 ad->ad_eid[eid->id].ade_off = eid->offset;
1642 ad->ad_eid[eid->id].ade_len = eid->len;
1646 /* put something sane in the date fields */
1647 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1648 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1649 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1650 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1658 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1659 vfs_handle_struct *handle,
1661 const struct smb_filename *smb_fname,
1662 adouble_type_t type)
1666 struct adouble *ad = NULL;
1670 smb_fname = fsp->base_fsp->fsp_name;
1673 DEBUG(10, ("ad_get(%s) called for %s\n",
1674 type == ADOUBLE_META ? "meta" : "rsrc",
1675 smb_fname->base_name));
1677 ad = ad_alloc(ctx, handle, type);
1683 /* Try rw first so we can use the fd in ad_convert() */
1686 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1687 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1689 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1692 DBG_DEBUG("ad_open [%s] error [%s]\n",
1693 smb_fname->base_name, strerror(errno));
1698 len = ad_read(ad, smb_fname);
1700 DEBUG(10, ("error reading AppleDouble for %s\n",
1701 smb_fname->base_name));
1707 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1708 type == ADOUBLE_META ? "meta" : "rsrc",
1709 smb_fname->base_name, rc));
1718 * Return AppleDouble data for a file
1720 * @param[in] ctx talloc context
1721 * @param[in] handle vfs handle
1722 * @param[in] smb_fname pathname to file or directory
1723 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1725 * @return talloced struct adouble or NULL on error
1727 static struct adouble *ad_get(TALLOC_CTX *ctx,
1728 vfs_handle_struct *handle,
1729 const struct smb_filename *smb_fname,
1730 adouble_type_t type)
1732 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1736 * Return AppleDouble data for a file
1738 * @param[in] ctx talloc context
1739 * @param[in] handle vfs handle
1740 * @param[in] fsp fsp to use for IO
1741 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1743 * @return talloced struct adouble or NULL on error
1745 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1746 files_struct *fsp, adouble_type_t type)
1748 return ad_get_internal(ctx, handle, fsp, NULL, type);
1752 * Set AppleDouble metadata on a file or directory
1754 * @param[in] ad adouble handle
1756 * @param[in] smb_fname pathname to file or directory
1758 * @return status code, 0 means success
1760 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1765 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1767 if (ad->ad_type != ADOUBLE_META) {
1768 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1769 smb_fname->base_name);
1778 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1780 AFPINFO_EA_NETATALK,
1782 AD_DATASZ_XATTR, 0);
1784 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1790 * Set AppleDouble metadata on a file or directory
1792 * @param[in] ad adouble handle
1793 * @param[in] fsp file handle
1795 * @return status code, 0 means success
1797 static int ad_fset(struct adouble *ad, files_struct *fsp)
1803 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1806 || (fsp->fh == NULL)
1807 || (fsp->fh->fd == -1))
1809 smb_panic("bad fsp");
1817 switch (ad->ad_type) {
1819 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1821 AFPINFO_EA_NETATALK,
1823 AD_DATASZ_XATTR, 0);
1827 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1832 if (len != AD_DATASZ_DOT_UND) {
1833 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1843 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1848 /*****************************************************************************
1850 *****************************************************************************/
1852 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1854 if (strncasecmp_m(smb_fname->stream_name,
1855 AFPINFO_STREAM_NAME,
1856 strlen(AFPINFO_STREAM_NAME)) == 0) {
1862 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1864 if (strncasecmp_m(smb_fname->stream_name,
1865 AFPRESOURCE_STREAM_NAME,
1866 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1873 * Test whether stream is an Apple stream, not used atm
1876 static bool is_apple_stream(const struct smb_filename *smb_fname)
1878 if (is_afpinfo_stream(smb_fname)) {
1881 if (is_afpresource_stream(smb_fname)) {
1889 * Initialize config struct from our smb.conf config parameters
1891 static int init_fruit_config(vfs_handle_struct *handle)
1893 struct fruit_config_data *config;
1895 const char *tm_size_str = NULL;
1897 config = talloc_zero(handle->conn, struct fruit_config_data);
1899 DEBUG(1, ("talloc_zero() failed\n"));
1905 * Versions up to Samba 4.5.x had a spelling bug in the
1906 * fruit:resource option calling lp_parm_enum with
1907 * "res*s*ource" (ie two s).
1909 * In Samba 4.6 we accept both the wrong and the correct
1910 * spelling, in Samba 4.7 the bad spelling will be removed.
1912 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1913 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1914 if (enumval == -1) {
1915 DEBUG(1, ("value for %s: resource type unknown\n",
1916 FRUIT_PARAM_TYPE_NAME));
1919 config->rsrc = (enum fruit_rsrc)enumval;
1921 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1922 "resource", fruit_rsrc, enumval);
1923 if (enumval == -1) {
1924 DEBUG(1, ("value for %s: resource type unknown\n",
1925 FRUIT_PARAM_TYPE_NAME));
1928 config->rsrc = (enum fruit_rsrc)enumval;
1930 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1931 "metadata", fruit_meta, FRUIT_META_NETATALK);
1932 if (enumval == -1) {
1933 DEBUG(1, ("value for %s: metadata type unknown\n",
1934 FRUIT_PARAM_TYPE_NAME));
1937 config->meta = (enum fruit_meta)enumval;
1939 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1940 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1941 if (enumval == -1) {
1942 DEBUG(1, ("value for %s: locking type unknown\n",
1943 FRUIT_PARAM_TYPE_NAME));
1946 config->locking = (enum fruit_locking)enumval;
1948 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1949 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1950 if (enumval == -1) {
1951 DEBUG(1, ("value for %s: encoding type unknown\n",
1952 FRUIT_PARAM_TYPE_NAME));
1955 config->encoding = (enum fruit_encoding)enumval;
1957 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1958 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1959 FRUIT_PARAM_TYPE_NAME,
1964 config->use_aapl = lp_parm_bool(
1965 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1967 config->time_machine = lp_parm_bool(
1968 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
1970 config->unix_info_enabled = lp_parm_bool(
1971 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1973 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1976 config->posix_rename = lp_parm_bool(
1977 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1979 config->aapl_zero_file_id =
1980 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1982 config->readdir_attr_rsize = lp_parm_bool(
1983 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1985 config->readdir_attr_finder_info = lp_parm_bool(
1986 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1988 config->readdir_attr_max_access = lp_parm_bool(
1989 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1991 config->model = lp_parm_const_string(
1992 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1994 tm_size_str = lp_parm_const_string(
1995 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1996 "time machine max size", NULL);
1997 if (tm_size_str != NULL) {
1998 config->time_machine_max_size = conv_str_size(tm_size_str);
2001 SMB_VFS_HANDLE_SET_DATA(handle, config,
2002 NULL, struct fruit_config_data,
2009 * Prepend "._" to a basename
2010 * Return a new struct smb_filename with stream_name == NULL.
2012 static int adouble_path(TALLOC_CTX *ctx,
2013 const struct smb_filename *smb_fname_in,
2014 struct smb_filename **pp_smb_fname_out)
2018 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2021 if (smb_fname == NULL) {
2025 /* We need streamname to be NULL */
2026 TALLOC_FREE(smb_fname->stream_name);
2028 /* And we're replacing base_name. */
2029 TALLOC_FREE(smb_fname->base_name);
2031 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2033 TALLOC_FREE(smb_fname);
2037 smb_fname->base_name = talloc_asprintf(smb_fname,
2038 "%s/._%s", parent, base);
2039 if (smb_fname->base_name == NULL) {
2040 TALLOC_FREE(smb_fname);
2044 *pp_smb_fname_out = smb_fname;
2050 * Allocate and initialize an AfpInfo struct
2052 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2054 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2058 ai->afpi_Signature = AFP_Signature;
2059 ai->afpi_Version = AFP_Version;
2060 ai->afpi_BackupTime = AD_DATE_START;
2065 * Pack an AfpInfo struct into a buffer
2067 * Buffer size must be at least AFP_INFO_SIZE
2068 * Returns size of packed buffer
2070 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2072 memset(buf, 0, AFP_INFO_SIZE);
2074 RSIVAL(buf, 0, ai->afpi_Signature);
2075 RSIVAL(buf, 4, ai->afpi_Version);
2076 RSIVAL(buf, 12, ai->afpi_BackupTime);
2077 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2079 return AFP_INFO_SIZE;
2083 * Unpack a buffer into a AfpInfo structure
2085 * Buffer size must be at least AFP_INFO_SIZE
2086 * Returns allocated AfpInfo struct
2088 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2090 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2095 ai->afpi_Signature = RIVAL(data, 0);
2096 ai->afpi_Version = RIVAL(data, 4);
2097 ai->afpi_BackupTime = RIVAL(data, 12);
2098 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2099 sizeof(ai->afpi_FinderInfo));
2101 if (ai->afpi_Signature != AFP_Signature
2102 || ai->afpi_Version != AFP_Version) {
2103 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2111 * Fake an inode number from the md5 hash of the (xattr) name
2113 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2116 unsigned char hash[16];
2120 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2121 SMB_ASSERT(upper_sname != NULL);
2124 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2125 sizeof(sbuf->st_ex_dev));
2126 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2127 sizeof(sbuf->st_ex_ino));
2128 MD5Update(&ctx, (unsigned char *)upper_sname,
2129 talloc_get_size(upper_sname)-1);
2130 MD5Final(hash, &ctx);
2132 TALLOC_FREE(upper_sname);
2134 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2135 memcpy(&result, hash, sizeof(result));
2137 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2138 sname, (unsigned long long)result));
2143 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2144 struct stream_struct **streams,
2145 const char *name, off_t size,
2148 struct stream_struct *tmp;
2150 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2156 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2157 if (tmp[*num_streams].name == NULL) {
2161 tmp[*num_streams].size = size;
2162 tmp[*num_streams].alloc_size = alloc_size;
2169 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2170 struct stream_struct **streams)
2172 struct stream_struct *tmp = *streams;
2175 if (*num_streams == 0) {
2179 for (i = 0; i < *num_streams; i++) {
2180 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2185 if (i == *num_streams) {
2189 if (tmp[i].size > 0) {
2193 TALLOC_FREE(tmp[i].name);
2194 if (*num_streams - 1 > i) {
2195 memmove(&tmp[i], &tmp[i+1],
2196 (*num_streams - i - 1) * sizeof(struct stream_struct));
2203 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2204 struct stream_struct **streams,
2207 struct stream_struct *tmp = *streams;
2210 if (*num_streams == 0) {
2214 for (i = 0; i < *num_streams; i++) {
2215 if (strequal_m(tmp[i].name, name)) {
2220 if (i == *num_streams) {
2224 TALLOC_FREE(tmp[i].name);
2225 if (*num_streams - 1 > i) {
2226 memmove(&tmp[i], &tmp[i+1],
2227 (*num_streams - i - 1) * sizeof(struct stream_struct));
2234 static bool ad_empty_finderinfo(const struct adouble *ad)
2237 char emptybuf[ADEDLEN_FINDERI] = {0};
2240 fi = ad_get_entry(ad, ADEID_FINDERI);
2242 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2246 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2250 static bool ai_empty_finderinfo(const AfpInfo *ai)
2253 char emptybuf[ADEDLEN_FINDERI] = {0};
2255 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2260 * Update btime with btime from Netatalk
2262 static void update_btime(vfs_handle_struct *handle,
2263 struct smb_filename *smb_fname)
2266 struct timespec creation_time = {0};
2268 struct fruit_config_data *config = NULL;
2270 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2273 switch (config->meta) {
2274 case FRUIT_META_STREAM:
2276 case FRUIT_META_NETATALK:
2280 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2284 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2288 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2294 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2295 update_stat_ex_create_time(&smb_fname->st, creation_time);
2301 * Map an access mask to a Netatalk single byte byte range lock
2303 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2304 uint32_t access_mask)
2308 switch (access_mask) {
2309 case FILE_READ_DATA:
2310 offset = AD_FILELOCK_OPEN_RD;
2313 case FILE_WRITE_DATA:
2314 case FILE_APPEND_DATA:
2315 offset = AD_FILELOCK_OPEN_WR;
2319 offset = AD_FILELOCK_OPEN_NONE;
2323 if (fork_type == APPLE_FORK_RSRC) {
2324 if (offset == AD_FILELOCK_OPEN_NONE) {
2325 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2335 * Map a deny mode to a Netatalk brl
2337 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2342 switch (deny_mode) {
2344 offset = AD_FILELOCK_DENY_RD;
2348 offset = AD_FILELOCK_DENY_WR;
2352 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2355 if (fork_type == APPLE_FORK_RSRC) {
2363 * Call fcntl() with an exclusive F_GETLK request in order to
2364 * determine if there's an exisiting shared lock
2366 * @return true if the requested lock was found or any error occurred
2367 * false if the lock was not found
2369 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2372 off_t offset = in_offset;
2377 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2378 if (result == false) {
2382 if (type != F_UNLCK) {
2389 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2391 uint32_t access_mask,
2394 NTSTATUS status = NT_STATUS_OK;
2395 bool open_for_reading, open_for_writing, deny_read, deny_write;
2397 bool have_read = false;
2400 /* FIXME: hardcoded data fork, add resource fork */
2401 enum apple_fork fork_type = APPLE_FORK_DATA;
2403 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2405 access_mask & FILE_READ_DATA ? "READ" :"-",
2406 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2407 deny_mode & DENY_READ ? "DENY_READ" : "-",
2408 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2410 if (fsp->fh->fd == -1) {
2411 return NT_STATUS_OK;
2414 flags = fcntl(fsp->fh->fd, F_GETFL);
2416 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2417 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2418 return map_nt_error_from_unix(errno);
2421 if (flags & (O_RDONLY|O_RDWR)) {
2423 * Applying fcntl read locks requires an fd opened for
2424 * reading. This means we won't be applying locks for
2425 * files openend write-only, but what can we do...
2431 * Check read access and deny read mode
2433 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2435 open_for_reading = test_netatalk_lock(
2436 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2438 deny_read = test_netatalk_lock(
2439 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2441 DEBUG(10, ("read: %s, deny_write: %s\n",
2442 open_for_reading == true ? "yes" : "no",
2443 deny_read == true ? "yes" : "no"));
2445 if (((access_mask & FILE_READ_DATA) && deny_read)
2446 || ((deny_mode & DENY_READ) && open_for_reading)) {
2447 return NT_STATUS_SHARING_VIOLATION;
2451 if ((access_mask & FILE_READ_DATA) && have_read) {
2452 struct byte_range_lock *br_lck = NULL;
2454 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2456 handle->conn->sconn->msg_ctx, fsp,
2457 fsp->op->global->open_persistent_id, 1, off,
2458 READ_LOCK, POSIX_LOCK, false,
2461 TALLOC_FREE(br_lck);
2463 if (!NT_STATUS_IS_OK(status)) {
2468 if ((deny_mode & DENY_READ) && have_read) {
2469 struct byte_range_lock *br_lck = NULL;
2471 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2473 handle->conn->sconn->msg_ctx, fsp,
2474 fsp->op->global->open_persistent_id, 1, off,
2475 READ_LOCK, POSIX_LOCK, false,
2478 TALLOC_FREE(br_lck);
2480 if (!NT_STATUS_IS_OK(status)) {
2487 * Check write access and deny write mode
2489 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2491 open_for_writing = test_netatalk_lock(
2492 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2494 deny_write = test_netatalk_lock(
2495 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2497 DEBUG(10, ("write: %s, deny_write: %s\n",
2498 open_for_writing == true ? "yes" : "no",
2499 deny_write == true ? "yes" : "no"));
2501 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2502 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2503 return NT_STATUS_SHARING_VIOLATION;
2507 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2508 struct byte_range_lock *br_lck = NULL;
2510 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2512 handle->conn->sconn->msg_ctx, fsp,
2513 fsp->op->global->open_persistent_id, 1, off,
2514 READ_LOCK, POSIX_LOCK, false,
2517 TALLOC_FREE(br_lck);
2519 if (!NT_STATUS_IS_OK(status)) {
2523 if ((deny_mode & DENY_WRITE) && have_read) {
2524 struct byte_range_lock *br_lck = NULL;
2526 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2528 handle->conn->sconn->msg_ctx, fsp,
2529 fsp->op->global->open_persistent_id, 1, off,
2530 READ_LOCK, POSIX_LOCK, false,
2533 TALLOC_FREE(br_lck);
2535 if (!NT_STATUS_IS_OK(status)) {
2544 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2545 struct smb_request *req,
2546 const struct smb2_create_blobs *in_context_blobs,
2547 struct smb2_create_blobs *out_context_blobs)
2549 struct fruit_config_data *config;
2551 struct smb2_create_blob *aapl = NULL;
2555 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2556 uint64_t req_bitmap, client_caps;
2557 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2561 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2562 return NT_STATUS_UNSUCCESSFUL);
2564 if (!config->use_aapl
2565 || in_context_blobs == NULL
2566 || out_context_blobs == NULL) {
2567 return NT_STATUS_OK;
2570 aapl = smb2_create_blob_find(in_context_blobs,
2571 SMB2_CREATE_TAG_AAPL);
2573 return NT_STATUS_OK;
2576 if (aapl->data.length != 24) {
2577 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2578 (uintmax_t)aapl->data.length));
2579 return NT_STATUS_INVALID_PARAMETER;
2582 cmd = IVAL(aapl->data.data, 0);
2583 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2584 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2585 return NT_STATUS_INVALID_PARAMETER;
2588 req_bitmap = BVAL(aapl->data.data, 8);
2589 client_caps = BVAL(aapl->data.data, 16);
2591 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2593 SBVAL(p, 8, req_bitmap);
2594 ok = data_blob_append(req, &blob, p, 16);
2596 return NT_STATUS_UNSUCCESSFUL;
2599 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2600 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2601 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2602 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2603 config->readdir_attr_enabled = true;
2606 if (config->use_copyfile) {
2607 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2608 config->copyfile_enabled = true;
2612 * The client doesn't set the flag, so we can't check
2613 * for it and just set it unconditionally
2615 if (config->unix_info_enabled) {
2616 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2619 SBVAL(p, 0, server_caps);
2620 ok = data_blob_append(req, &blob, p, 8);
2622 return NT_STATUS_UNSUCCESSFUL;
2626 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2627 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2635 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2642 if (config->time_machine) {
2643 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2648 ok = data_blob_append(req, &blob, p, 8);
2650 return NT_STATUS_UNSUCCESSFUL;
2654 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2655 ok = convert_string_talloc(req,
2656 CH_UNIX, CH_UTF16LE,
2657 config->model, strlen(config->model),
2660 return NT_STATUS_UNSUCCESSFUL;
2664 SIVAL(p + 4, 0, modellen);
2665 ok = data_blob_append(req, &blob, p, 8);
2668 return NT_STATUS_UNSUCCESSFUL;
2671 ok = data_blob_append(req, &blob, model, modellen);
2674 return NT_STATUS_UNSUCCESSFUL;
2678 status = smb2_create_blob_add(out_context_blobs,
2680 SMB2_CREATE_TAG_AAPL,
2682 if (NT_STATUS_IS_OK(status)) {
2683 global_fruit_config.nego_aapl = true;
2684 if (config->aapl_zero_file_id) {
2685 aapl_force_zero_file_id(handle->conn->sconn);
2692 static bool readdir_attr_meta_finderi_stream(
2693 struct vfs_handle_struct *handle,
2694 const struct smb_filename *smb_fname,
2697 struct smb_filename *stream_name = NULL;
2698 files_struct *fsp = NULL;
2703 uint8_t buf[AFP_INFO_SIZE];
2705 stream_name = synthetic_smb_fname(talloc_tos(),
2706 smb_fname->base_name,
2707 AFPINFO_STREAM_NAME,
2708 NULL, smb_fname->flags);
2709 if (stream_name == NULL) {
2713 ret = SMB_VFS_STAT(handle->conn, stream_name);
2718 status = SMB_VFS_CREATE_FILE(
2719 handle->conn, /* conn */
2721 0, /* root_dir_fid */
2722 stream_name, /* fname */
2723 FILE_READ_DATA, /* access_mask */
2724 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2726 FILE_OPEN, /* create_disposition*/
2727 0, /* create_options */
2728 0, /* file_attributes */
2729 INTERNAL_OPEN_ONLY, /* oplock_request */
2731 0, /* allocation_size */
2732 0, /* private_flags */
2737 NULL, NULL); /* create context */
2739 TALLOC_FREE(stream_name);
2741 if (!NT_STATUS_IS_OK(status)) {
2745 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2746 if (nread != AFP_INFO_SIZE) {
2747 DBG_ERR("short read [%s] [%zd/%d]\n",
2748 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2753 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2760 close_file(NULL, fsp, NORMAL_CLOSE);
2766 static bool readdir_attr_meta_finderi_netatalk(
2767 struct vfs_handle_struct *handle,
2768 const struct smb_filename *smb_fname,
2771 struct adouble *ad = NULL;
2774 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2779 p = ad_get_entry(ad, ADEID_FINDERI);
2781 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2786 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2791 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2792 const struct smb_filename *smb_fname,
2793 struct readdir_attr_data *attr_data)
2795 struct fruit_config_data *config = NULL;
2796 uint32_t date_added;
2800 SMB_VFS_HANDLE_GET_DATA(handle, config,
2801 struct fruit_config_data,
2804 switch (config->meta) {
2805 case FRUIT_META_NETATALK:
2806 ok = readdir_attr_meta_finderi_netatalk(
2807 handle, smb_fname, &ai);
2810 case FRUIT_META_STREAM:
2811 ok = readdir_attr_meta_finderi_stream(
2812 handle, smb_fname, &ai);
2816 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2821 /* Don't bother with errors, it's likely ENOENT */
2825 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2827 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2828 &ai.afpi_FinderInfo[0], 4);
2830 /* finder_creator */
2831 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2832 &ai.afpi_FinderInfo[4], 4);
2836 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2837 &ai.afpi_FinderInfo[8], 2);
2839 /* finder_ext_flags */
2840 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2841 &ai.afpi_FinderInfo[24], 2);
2844 date_added = convert_time_t_to_uint32_t(
2845 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2847 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2852 static uint64_t readdir_attr_rfork_size_adouble(
2853 struct vfs_handle_struct *handle,
2854 const struct smb_filename *smb_fname)
2856 struct adouble *ad = NULL;
2857 uint64_t rfork_size;
2859 ad = ad_get(talloc_tos(), handle, smb_fname,
2865 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2871 static uint64_t readdir_attr_rfork_size_stream(
2872 struct vfs_handle_struct *handle,
2873 const struct smb_filename *smb_fname)
2875 struct smb_filename *stream_name = NULL;
2877 uint64_t rfork_size;
2879 stream_name = synthetic_smb_fname(talloc_tos(),
2880 smb_fname->base_name,
2881 AFPRESOURCE_STREAM_NAME,
2883 if (stream_name == NULL) {
2887 ret = SMB_VFS_STAT(handle->conn, stream_name);
2889 TALLOC_FREE(stream_name);
2893 rfork_size = stream_name->st.st_ex_size;
2894 TALLOC_FREE(stream_name);
2899 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2900 const struct smb_filename *smb_fname)
2902 struct fruit_config_data *config = NULL;
2903 uint64_t rfork_size;
2905 SMB_VFS_HANDLE_GET_DATA(handle, config,
2906 struct fruit_config_data,
2909 switch (config->rsrc) {
2910 case FRUIT_RSRC_ADFILE:
2911 case FRUIT_RSRC_XATTR:
2912 rfork_size = readdir_attr_rfork_size_adouble(handle,
2916 case FRUIT_META_STREAM:
2917 rfork_size = readdir_attr_rfork_size_stream(handle,
2922 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2930 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2931 const struct smb_filename *smb_fname,
2932 struct readdir_attr_data *attr_data)
2934 NTSTATUS status = NT_STATUS_OK;
2935 struct fruit_config_data *config = NULL;
2938 SMB_VFS_HANDLE_GET_DATA(handle, config,
2939 struct fruit_config_data,
2940 return NT_STATUS_UNSUCCESSFUL);
2943 /* Ensure we return a default value in the creation_date field */
2944 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2947 * Resource fork length
2950 if (config->readdir_attr_rsize) {
2951 uint64_t rfork_size;
2953 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2954 attr_data->attr_data.aapl.rfork_size = rfork_size;
2961 if (config->readdir_attr_finder_info) {
2962 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2964 status = NT_STATUS_INTERNAL_ERROR;
2971 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
2976 if (psd->dacl == NULL) {
2977 return NT_STATUS_OK;
2980 for (i = 0; i < psd->dacl->num_aces; i++) {
2981 /* MS NFS style mode/uid/gid */
2982 int cmp = dom_sid_compare_domain(
2983 &global_sid_Unix_NFS,
2984 &psd->dacl->aces[i].trustee);
2986 /* Normal ACE entry. */
2991 * security_descriptor_dacl_del()
2992 * *must* return NT_STATUS_OK as we know
2993 * we have something to remove.
2996 status = security_descriptor_dacl_del(psd,
2997 &psd->dacl->aces[i].trustee);
2998 if (!NT_STATUS_IS_OK(status)) {
2999 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3005 * security_descriptor_dacl_del() may delete more
3006 * then one entry subsequent to this one if the
3007 * SID matches, but we only need to ensure that
3008 * we stay looking at the same element in the array.
3012 return NT_STATUS_OK;
3015 /* Search MS NFS style ACE with UNIX mode */
3016 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3018 struct security_descriptor *psd,
3023 struct fruit_config_data *config = NULL;
3027 SMB_VFS_HANDLE_GET_DATA(handle, config,
3028 struct fruit_config_data,
3029 return NT_STATUS_UNSUCCESSFUL);
3031 if (!global_fruit_config.nego_aapl) {
3032 return NT_STATUS_OK;
3034 if (psd->dacl == NULL || !config->unix_info_enabled) {
3035 return NT_STATUS_OK;
3038 for (i = 0; i < psd->dacl->num_aces; i++) {
3039 if (dom_sid_compare_domain(
3040 &global_sid_Unix_NFS_Mode,
3041 &psd->dacl->aces[i].trustee) == 0) {
3042 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3043 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3046 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3047 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3053 * Remove any incoming virtual ACE entries generated by
3054 * fruit_fget_nt_acl().
3057 return remove_virtual_nfs_aces(psd);
3060 /****************************************************************************
3062 ****************************************************************************/
3064 static int fruit_connect(vfs_handle_struct *handle,
3065 const char *service,
3069 char *list = NULL, *newlist = NULL;
3070 struct fruit_config_data *config;
3072 DEBUG(10, ("fruit_connect\n"));
3074 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3079 rc = init_fruit_config(handle);
3084 SMB_VFS_HANDLE_GET_DATA(handle, config,
3085 struct fruit_config_data, return -1);
3087 if (config->veto_appledouble) {
3088 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3091 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3092 newlist = talloc_asprintf(
3094 "%s/" ADOUBLE_NAME_PREFIX "*/",
3096 lp_do_parameter(SNUM(handle->conn),
3101 lp_do_parameter(SNUM(handle->conn),
3103 "/" ADOUBLE_NAME_PREFIX "*/");
3109 if (config->encoding == FRUIT_ENC_NATIVE) {
3110 lp_do_parameter(SNUM(handle->conn),
3115 if (config->time_machine) {
3116 DBG_NOTICE("Enabling durable handles for Time Machine "
3117 "support on [%s]\n", service);
3118 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3119 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3120 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3121 if (!lp_strict_sync(SNUM(handle->conn))) {
3122 DBG_WARNING("Time Machine without strict sync is not "
3125 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3131 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3132 struct smb_filename *smb_fname,
3138 char afpinfo_buf[AFP_INFO_SIZE];
3139 ssize_t len, written;
3143 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3148 if (!(flags & (O_CREAT | O_TRUNC))) {
3152 ai = afpinfo_new(talloc_tos());
3158 len = afpinfo_pack(ai, afpinfo_buf);
3159 if (len != AFP_INFO_SIZE) {
3164 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3165 fsp->fh->fd = hostfd;
3167 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3170 if (written != AFP_INFO_SIZE) {
3171 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3179 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3182 int saved_errno = errno;
3184 fsp->fh->fd = hostfd;
3185 SMB_VFS_NEXT_CLOSE(handle, fsp);
3188 errno = saved_errno;
3193 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3194 struct smb_filename *smb_fname,
3201 struct adouble *ad = NULL;
3204 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3207 * Return a valid fd, but ensure any attempt to use it returns an error
3208 * (EPIPE). All operations on the smb_fname or the fsp will use path
3218 if (flags & (O_CREAT | O_TRUNC)) {
3220 * The attribute does not exist or needs to be truncated,
3221 * create an AppleDouble EA
3223 ad = ad_init(fsp, handle, ADOUBLE_META);
3229 rc = ad_set(ad, fsp->fsp_name);
3239 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3241 int saved_errno = errno;
3246 errno = saved_errno;
3251 static int fruit_open_meta(vfs_handle_struct *handle,
3252 struct smb_filename *smb_fname,
3253 files_struct *fsp, int flags, mode_t mode)
3256 struct fruit_config_data *config = NULL;
3257 struct fio *fio = NULL;
3259 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3261 SMB_VFS_HANDLE_GET_DATA(handle, config,
3262 struct fruit_config_data, return -1);
3264 switch (config->meta) {
3265 case FRUIT_META_STREAM:
3266 fd = fruit_open_meta_stream(handle, smb_fname,
3270 case FRUIT_META_NETATALK:
3271 fd = fruit_open_meta_netatalk(handle, smb_fname,
3276 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3280 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3286 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3287 fio->type = ADOUBLE_META;
3288 fio->config = config;
3293 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3294 struct smb_filename *smb_fname,
3300 struct adouble *ad = NULL;
3301 struct smb_filename *smb_fname_base = NULL;
3302 struct fruit_config_data *config = NULL;
3305 SMB_VFS_HANDLE_GET_DATA(handle, config,
3306 struct fruit_config_data, return -1);
3308 if ((!(flags & O_CREAT)) &&
3309 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3311 /* sorry, but directories don't habe a resource fork */
3316 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3321 /* Sanitize flags */
3322 if (flags & O_WRONLY) {
3323 /* We always need read access for the metadata header too */
3328 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3335 if (flags & (O_CREAT | O_TRUNC)) {
3336 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3342 fsp->fh->fd = hostfd;
3344 rc = ad_fset(ad, fsp);
3355 TALLOC_FREE(smb_fname_base);
3357 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3359 int saved_errno = errno;
3362 * BUGBUGBUG -- we would need to call
3363 * fd_close_posix here, but we don't have a
3366 fsp->fh->fd = hostfd;
3370 errno = saved_errno;
3375 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3376 struct smb_filename *smb_fname,
3381 #ifdef HAVE_ATTROPEN
3384 fd = attropen(smb_fname->base_name,
3385 AFPRESOURCE_EA_NETATALK,
3400 static int fruit_open_rsrc(vfs_handle_struct *handle,
3401 struct smb_filename *smb_fname,
3402 files_struct *fsp, int flags, mode_t mode)
3405 struct fruit_config_data *config = NULL;
3406 struct fio *fio = NULL;
3408 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3410 SMB_VFS_HANDLE_GET_DATA(handle, config,
3411 struct fruit_config_data, return -1);
3413 if (((flags & O_ACCMODE) == O_RDONLY)
3414 && (flags & O_CREAT)
3415 && !VALID_STAT(fsp->fsp_name->st))
3418 * This means the stream doesn't exist. macOS SMB server fails
3419 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3420 * 12565 and the test for this combination in
3421 * test_rfork_create().
3427 switch (config->rsrc) {
3428 case FRUIT_RSRC_STREAM:
3429 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3432 case FRUIT_RSRC_ADFILE:
3433 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3437 case FRUIT_RSRC_XATTR:
3438 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3443 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3447 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3453 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3454 fio->type = ADOUBLE_RSRC;
3455 fio->config = config;
3460 static int fruit_open(vfs_handle_struct *handle,
3461 struct smb_filename *smb_fname,
3462 files_struct *fsp, int flags, mode_t mode)
3466 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3468 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3469 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3472 if (is_afpinfo_stream(smb_fname)) {
3473 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3474 } else if (is_afpresource_stream(smb_fname)) {
3475 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3477 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3480 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3485 static int fruit_rename(struct vfs_handle_struct *handle,
3486 const struct smb_filename *smb_fname_src,
3487 const struct smb_filename *smb_fname_dst)
3490 struct fruit_config_data *config = NULL;
3491 struct smb_filename *src_adp_smb_fname = NULL;
3492 struct smb_filename *dst_adp_smb_fname = NULL;
3494 SMB_VFS_HANDLE_GET_DATA(handle, config,
3495 struct fruit_config_data, return -1);
3497 if (!VALID_STAT(smb_fname_src->st)) {
3498 DBG_ERR("Need valid stat for [%s]\n",
3499 smb_fname_str_dbg(smb_fname_src));
3503 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3508 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3509 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3514 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3519 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3524 DBG_DEBUG("%s -> %s\n",
3525 smb_fname_str_dbg(src_adp_smb_fname),
3526 smb_fname_str_dbg(dst_adp_smb_fname));
3528 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3529 if (errno == ENOENT) {
3534 TALLOC_FREE(src_adp_smb_fname);
3535 TALLOC_FREE(dst_adp_smb_fname);
3539 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3540 const struct smb_filename *smb_fname)
3542 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3545 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3546 const struct smb_filename *smb_fname)
3548 return SMB_VFS_REMOVEXATTR(handle->conn,
3550 AFPINFO_EA_NETATALK);
3553 static int fruit_unlink_meta(vfs_handle_struct *handle,
3554 const struct smb_filename *smb_fname)
3556 struct fruit_config_data *config = NULL;
3559 SMB_VFS_HANDLE_GET_DATA(handle, config,
3560 struct fruit_config_data, return -1);
3562 switch (config->meta) {
3563 case FRUIT_META_STREAM:
3564 rc = fruit_unlink_meta_stream(handle, smb_fname);
3567 case FRUIT_META_NETATALK:
3568 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3572 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3579 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3580 const struct smb_filename *smb_fname,
3585 if (!force_unlink) {
3586 struct smb_filename *smb_fname_cp = NULL;
3589 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3590 if (smb_fname_cp == NULL) {
3595 * 0 byte resource fork streams are not listed by
3596 * vfs_streaminfo, as a result stream cleanup/deletion of file
3597 * deletion doesn't remove the resourcefork stream.
3600 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3602 TALLOC_FREE(smb_fname_cp);
3603 DBG_ERR("stat [%s] failed [%s]\n",
3604 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3608 size = smb_fname_cp->st.st_ex_size;
3609 TALLOC_FREE(smb_fname_cp);
3612 /* OS X ignores resource fork stream delete requests */
3617 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3618 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3625 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3626 const struct smb_filename *smb_fname,
3630 struct adouble *ad = NULL;
3631 struct smb_filename *adp_smb_fname = NULL;
3633 if (!force_unlink) {
3634 ad = ad_get(talloc_tos(), handle, smb_fname,
3643 * 0 byte resource fork streams are not listed by
3644 * vfs_streaminfo, as a result stream cleanup/deletion of file
3645 * deletion doesn't remove the resourcefork stream.
3648 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3649 /* OS X ignores resource fork stream delete requests */
3657 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3662 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3663 TALLOC_FREE(adp_smb_fname);
3664 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3671 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3672 const struct smb_filename *smb_fname,
3676 * OS X ignores resource fork stream delete requests, so nothing to do
3677 * here. Removing the file will remove the xattr anyway, so we don't
3678 * have to take care of removing 0 byte resource forks that could be
3684 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3685 const struct smb_filename *smb_fname,
3688 struct fruit_config_data *config = NULL;
3691 SMB_VFS_HANDLE_GET_DATA(handle, config,
3692 struct fruit_config_data, return -1);
3694 switch (config->rsrc) {
3695 case FRUIT_RSRC_STREAM:
3696 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3699 case FRUIT_RSRC_ADFILE:
3700 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3703 case FRUIT_RSRC_XATTR:
3704 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3708 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3715 static int fruit_unlink(vfs_handle_struct *handle,
3716 const struct smb_filename *smb_fname)
3719 struct fruit_config_data *config = NULL;
3720 struct smb_filename *rsrc_smb_fname = NULL;
3722 SMB_VFS_HANDLE_GET_DATA(handle, config,
3723 struct fruit_config_data, return -1);
3725 if (is_afpinfo_stream(smb_fname)) {
3726 return fruit_unlink_meta(handle, smb_fname);
3727 } else if (is_afpresource_stream(smb_fname)) {
3728 return fruit_unlink_rsrc(handle, smb_fname, false);
3729 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3730 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3734 * A request to delete the base file. Because 0 byte resource
3735 * fork streams are not listed by fruit_streaminfo,
3736 * delete_all_streams() can't remove 0 byte resource fork
3737 * streams, so we have to cleanup this here.
3739 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3740 smb_fname->base_name,
3741 AFPRESOURCE_STREAM_NAME,
3744 if (rsrc_smb_fname == NULL) {
3748 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3749 if ((rc != 0) && (errno != ENOENT)) {
3750 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3751 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3752 TALLOC_FREE(rsrc_smb_fname);
3755 TALLOC_FREE(rsrc_smb_fname);
3757 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3760 static int fruit_chmod(vfs_handle_struct *handle,
3761 const struct smb_filename *smb_fname,
3765 struct fruit_config_data *config = NULL;
3766 struct smb_filename *smb_fname_adp = NULL;
3768 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3773 SMB_VFS_HANDLE_GET_DATA(handle, config,
3774 struct fruit_config_data, return -1);
3776 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3780 if (!VALID_STAT(smb_fname->st)) {
3784 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3788 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3793 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3795 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3796 if (errno == ENOENT) {
3800 TALLOC_FREE(smb_fname_adp);
3804 static int fruit_chown(vfs_handle_struct *handle,
3805 const struct smb_filename *smb_fname,
3810 struct fruit_config_data *config = NULL;
3811 struct smb_filename *adp_smb_fname = NULL;
3813 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3818 SMB_VFS_HANDLE_GET_DATA(handle, config,
3819 struct fruit_config_data, return -1);
3821 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3825 if (!VALID_STAT(smb_fname->st)) {
3829 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3833 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3838 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3840 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3841 if (errno == ENOENT) {
3846 TALLOC_FREE(adp_smb_fname);
3850 static int fruit_rmdir(struct vfs_handle_struct *handle,
3851 const struct smb_filename *smb_fname)
3855 struct fruit_config_data *config;
3857 SMB_VFS_HANDLE_GET_DATA(handle, config,
3858 struct fruit_config_data, return -1);
3860 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3865 * Due to there is no way to change bDeleteVetoFiles variable
3866 * from this module, need to clean up ourselves
3869 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3874 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3876 struct adouble *ad = NULL;
3878 struct smb_filename *ad_smb_fname = NULL;
3881 match = strncmp(de->d_name,
3882 ADOUBLE_NAME_PREFIX,
3883 strlen(ADOUBLE_NAME_PREFIX));
3888 p = talloc_asprintf(talloc_tos(), "%s/%s",
3889 smb_fname->base_name, de->d_name);
3891 DBG_ERR("talloc_asprintf failed\n");
3895 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3899 if (ad_smb_fname == NULL) {
3900 DBG_ERR("synthetic_smb_fname failed\n");
3905 * Check whether it's a valid AppleDouble file, if
3906 * yes, delete it, ignore it otherwise.
3908 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3910 TALLOC_FREE(ad_smb_fname);
3916 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3918 DBG_ERR("Deleting [%s] failed\n",
3919 smb_fname_str_dbg(ad_smb_fname));
3921 TALLOC_FREE(ad_smb_fname);
3926 SMB_VFS_CLOSEDIR(handle->conn, dh);
3928 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3931 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3932 files_struct *fsp, void *data,
3933 size_t n, off_t offset)
3938 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3944 DBG_ERR("Removing [%s] after short read [%zd]\n",
3945 fsp_str_dbg(fsp), nread);
3947 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3949 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3957 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3958 files_struct *fsp, void *data,
3959 size_t n, off_t offset)
3962 struct adouble *ad = NULL;
3963 char afpinfo_buf[AFP_INFO_SIZE];
3967 ai = afpinfo_new(talloc_tos());
3972 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3978 p = ad_get_entry(ad, ADEID_FINDERI);
3980 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3985 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3987 nread = afpinfo_pack(ai, afpinfo_buf);
3988 if (nread != AFP_INFO_SIZE) {
3993 memcpy(data, afpinfo_buf, n);
4001 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
4002 files_struct *fsp, void *data,
4003 size_t n, off_t offset)
4005 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4010 * OS X has a off-by-1 error in the offset calculation, so we're
4011 * bug compatible here. It won't hurt, as any relevant real
4012 * world read requests from the AFP_AfpInfo stream will be
4013 * offset=0 n=60. offset is ignored anyway, see below.
4015 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4020 DBG_ERR("Failed to fetch fsp extension");
4024 /* Yes, macOS always reads from offset 0 */
4026 to_return = MIN(n, AFP_INFO_SIZE);
4028 switch (fio->config->meta) {
4029 case FRUIT_META_STREAM:
4030 nread = fruit_pread_meta_stream(handle, fsp, data,
4034 case FRUIT_META_NETATALK:
4035 nread = fruit_pread_meta_adouble(handle, fsp, data,
4040 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4047 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4048 files_struct *fsp, void *data,
4049 size_t n, off_t offset)
4051 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4054 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4055 files_struct *fsp, void *data,
4056 size_t n, off_t offset)
4058 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4061 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4062 files_struct *fsp, void *data,
4063 size_t n, off_t offset)
4065 struct adouble *ad = NULL;
4068 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4073 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4074 offset + ad_getentryoff(ad, ADEID_RFORK));
4080 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4081 files_struct *fsp, void *data,
4082 size_t n, off_t offset)
4084 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4092 switch (fio->config->rsrc) {
4093 case FRUIT_RSRC_STREAM:
4094 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4097 case FRUIT_RSRC_ADFILE:
4098 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4101 case FRUIT_RSRC_XATTR:
4102 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4106 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4113 static ssize_t fruit_pread(vfs_handle_struct *handle,
4114 files_struct *fsp, void *data,
4115 size_t n, off_t offset)
4117 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4120 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4121 fsp_str_dbg(fsp), (intmax_t)offset, n);
4124 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4127 if (fio->type == ADOUBLE_META) {
4128 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4130 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4133 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4137 static bool fruit_must_handle_aio_stream(struct fio *fio)
4143 if ((fio->type == ADOUBLE_META) &&
4144 (fio->config->meta == FRUIT_META_NETATALK))
4149 if ((fio->type == ADOUBLE_RSRC) &&
4150 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4158 struct fruit_pread_state {
4160 struct vfs_aio_state vfs_aio_state;
4163 static void fruit_pread_done(struct tevent_req *subreq);
4165 static struct tevent_req *fruit_pread_send(
4166 struct vfs_handle_struct *handle,
4167 TALLOC_CTX *mem_ctx,
4168 struct tevent_context *ev,
4169 struct files_struct *fsp,
4171 size_t n, off_t offset)
4173 struct tevent_req *req = NULL;
4174 struct tevent_req *subreq = NULL;
4175 struct fruit_pread_state *state = NULL;
4176 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4178 req = tevent_req_create(mem_ctx, &state,
4179 struct fruit_pread_state);
4184 if (fruit_must_handle_aio_stream(fio)) {
4185 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4186 if (state->nread != n) {
4187 if (state->nread != -1) {
4190 tevent_req_error(req, errno);
4191 return tevent_req_post(req, ev);
4193 tevent_req_done(req);
4194 return tevent_req_post(req, ev);
4197 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4199 if (tevent_req_nomem(req, subreq)) {
4200 return tevent_req_post(req, ev);
4202 tevent_req_set_callback(subreq, fruit_pread_done, req);
4206 static void fruit_pread_done(struct tevent_req *subreq)
4208 struct tevent_req *req = tevent_req_callback_data(
4209 subreq, struct tevent_req);
4210 struct fruit_pread_state *state = tevent_req_data(
4211 req, struct fruit_pread_state);
4213 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4214 TALLOC_FREE(subreq);
4216 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4219 tevent_req_done(req);
4222 static ssize_t fruit_pread_recv(struct tevent_req *req,
4223 struct vfs_aio_state *vfs_aio_state)
4225 struct fruit_pread_state *state = tevent_req_data(
4226 req, struct fruit_pread_state);
4228 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4232 *vfs_aio_state = state->vfs_aio_state;
4233 return state->nread;
4236 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4237 files_struct *fsp, const void *data,
4238 size_t n, off_t offset)
4244 ai = afpinfo_unpack(talloc_tos(), data);
4249 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4250 if (nwritten != n) {
4254 if (!ai_empty_finderinfo(ai)) {
4258 ok = set_delete_on_close(
4261 handle->conn->session_info->security_token,
4262 handle->conn->session_info->unix_token);
4264 DBG_ERR("set_delete_on_close on [%s] failed\n",
4272 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4273 files_struct *fsp, const void *data,
4274 size_t n, off_t offset)
4276 struct adouble *ad = NULL;
4282 ai = afpinfo_unpack(talloc_tos(), data);
4287 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4289 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4294 p = ad_get_entry(ad, ADEID_FINDERI);
4296 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4301 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4303 ret = ad_fset(ad, fsp);
4305 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4312 if (!ai_empty_finderinfo(ai)) {
4316 ok = set_delete_on_close(
4319 handle->conn->session_info->security_token,
4320 handle->conn->session_info->unix_token);
4322 DBG_ERR("set_delete_on_close on [%s] failed\n",
4330 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4331 files_struct *fsp, const void *data,
4332 size_t n, off_t offset)
4334 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4338 * Writing an all 0 blob to the metadata stream
4339 * results in the stream being removed on a macOS
4340 * server. This ensures we behave the same and it
4341 * verified by the "delete AFP_AfpInfo by writing all
4344 if (n != AFP_INFO_SIZE || offset != 0) {
4345 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4346 (intmax_t)offset, (intmax_t)n);
4351 DBG_ERR("Failed to fetch fsp extension");
4355 switch (fio->config->meta) {
4356 case FRUIT_META_STREAM:
4357 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4361 case FRUIT_META_NETATALK:
4362 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4367 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4374 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4375 files_struct *fsp, const void *data,
4376 size_t n, off_t offset)
4378 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4381 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4382 files_struct *fsp, const void *data,
4383 size_t n, off_t offset)
4385 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4388 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4389 files_struct *fsp, const void *data,
4390 size_t n, off_t offset)
4392 struct adouble *ad = NULL;
4396 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4398 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4402 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4403 offset + ad_getentryoff(ad, ADEID_RFORK));
4404 if (nwritten != n) {
4405 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4406 fsp_str_dbg(fsp), nwritten, n);
4411 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4412 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4413 ret = ad_fset(ad, fsp);
4415 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4425 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4426 files_struct *fsp, const void *data,
4427 size_t n, off_t offset)
4429 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4433 DBG_ERR("Failed to fetch fsp extension");
4437 switch (fio->config->rsrc) {
4438 case FRUIT_RSRC_STREAM:
4439 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4442 case FRUIT_RSRC_ADFILE:
4443 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4446 case FRUIT_RSRC_XATTR:
4447 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4451 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4458 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4459 files_struct *fsp, const void *data,
4460 size_t n, off_t offset)
4462 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4465 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4466 fsp_str_dbg(fsp), (intmax_t)offset, n);
4469 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4472 if (fio->type == ADOUBLE_META) {
4473 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4475 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4478 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4482 struct fruit_pwrite_state {
4484 struct vfs_aio_state vfs_aio_state;
4487 static void fruit_pwrite_done(struct tevent_req *subreq);
4489 static struct tevent_req *fruit_pwrite_send(
4490 struct vfs_handle_struct *handle,
4491 TALLOC_CTX *mem_ctx,
4492 struct tevent_context *ev,
4493 struct files_struct *fsp,
4495 size_t n, off_t offset)
4497 struct tevent_req *req = NULL;
4498 struct tevent_req *subreq = NULL;
4499 struct fruit_pwrite_state *state = NULL;
4500 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4502 req = tevent_req_create(mem_ctx, &state,
4503 struct fruit_pwrite_state);
4508 if (fruit_must_handle_aio_stream(fio)) {
4509 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4510 if (state->nwritten != n) {
4511 if (state->nwritten != -1) {
4514 tevent_req_error(req, errno);
4515 return tevent_req_post(req, ev);
4517 tevent_req_done(req);
4518 return tevent_req_post(req, ev);
4521 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4523 if (tevent_req_nomem(req, subreq)) {
4524 return tevent_req_post(req, ev);
4526 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4530 static void fruit_pwrite_done(struct tevent_req *subreq)
4532 struct tevent_req *req = tevent_req_callback_data(
4533 subreq, struct tevent_req);
4534 struct fruit_pwrite_state *state = tevent_req_data(
4535 req, struct fruit_pwrite_state);
4537 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4538 TALLOC_FREE(subreq);
4540 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4543 tevent_req_done(req);
4546 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4547 struct vfs_aio_state *vfs_aio_state)
4549 struct fruit_pwrite_state *state = tevent_req_data(
4550 req, struct fruit_pwrite_state);
4552 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4556 *vfs_aio_state = state->vfs_aio_state;
4557 return state->nwritten;
4561 * Helper to stat/lstat the base file of an smb_fname.
4563 static int fruit_stat_base(vfs_handle_struct *handle,
4564 struct smb_filename *smb_fname,
4567 char *tmp_stream_name;
4570 tmp_stream_name = smb_fname->stream_name;
4571 smb_fname->stream_name = NULL;
4573 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4575 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4577 smb_fname->stream_name = tmp_stream_name;
4581 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4582 struct smb_filename *smb_fname,
4588 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4590 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4596 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4597 struct smb_filename *smb_fname,
4600 struct adouble *ad = NULL;
4602 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4604 DBG_INFO("fruit_stat_meta %s: %s\n",
4605 smb_fname_str_dbg(smb_fname), strerror(errno));
4611 /* Populate the stat struct with info from the base file. */
4612 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4615 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4616 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4617 smb_fname->stream_name);
4621 static int fruit_stat_meta(vfs_handle_struct *handle,
4622 struct smb_filename *smb_fname,
4625 struct fruit_config_data *config = NULL;
4628 SMB_VFS_HANDLE_GET_DATA(handle, config,
4629 struct fruit_config_data, return -1);
4631 switch (config->meta) {
4632 case FRUIT_META_STREAM:
4633 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4636 case FRUIT_META_NETATALK:
4637 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4641 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4648 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4649 struct smb_filename *smb_fname,
4652 struct adouble *ad = NULL;
4655 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4661 /* Populate the stat struct with info from the base file. */
4662 ret = fruit_stat_base(handle, smb_fname, follow_links);
4668 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4669 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4670 smb_fname->stream_name);
4675 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4676 struct smb_filename *smb_fname,
4682 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4684 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4690 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4691 struct smb_filename *smb_fname,
4694 #ifdef HAVE_ATTROPEN
4698 /* Populate the stat struct with info from the base file. */
4699 ret = fruit_stat_base(handle, smb_fname, follow_links);
4704 fd = attropen(smb_fname->base_name,
4705 AFPRESOURCE_EA_NETATALK,
4711 ret = sys_fstat(fd, &smb_fname->st, false);
4714 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4715 AFPRESOURCE_EA_NETATALK);
4721 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4722 smb_fname->stream_name);
4732 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4733 struct smb_filename *smb_fname,
4736 struct fruit_config_data *config = NULL;
4739 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4741 SMB_VFS_HANDLE_GET_DATA(handle, config,
4742 struct fruit_config_data, return -1);
4744 switch (config->rsrc) {
4745 case FRUIT_RSRC_STREAM:
4746 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4749 case FRUIT_RSRC_XATTR:
4750 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4753 case FRUIT_RSRC_ADFILE:
4754 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4758 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4765 static int fruit_stat(vfs_handle_struct *handle,
4766 struct smb_filename *smb_fname)
4770 DEBUG(10, ("fruit_stat called for %s\n",
4771 smb_fname_str_dbg(smb_fname)));
4773 if (!is_ntfs_stream_smb_fname(smb_fname)
4774 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4775 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4777 update_btime(handle, smb_fname);
4783 * Note if lp_posix_paths() is true, we can never
4784 * get here as is_ntfs_stream_smb_fname() is
4785 * always false. So we never need worry about
4786 * not following links here.
4789 if (is_afpinfo_stream(smb_fname)) {
4790 rc = fruit_stat_meta(handle, smb_fname, true);
4791 } else if (is_afpresource_stream(smb_fname)) {
4792 rc = fruit_stat_rsrc(handle, smb_fname, true);
4794 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4798 update_btime(handle, smb_fname);
4799 smb_fname->st.st_ex_mode &= ~S_IFMT;
4800 smb_fname->st.st_ex_mode |= S_IFREG;
4801 smb_fname->st.st_ex_blocks =
4802 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4807 static int fruit_lstat(vfs_handle_struct *handle,
4808 struct smb_filename *smb_fname)
4812 DEBUG(10, ("fruit_lstat called for %s\n",
4813 smb_fname_str_dbg(smb_fname)));
4815 if (!is_ntfs_stream_smb_fname(smb_fname)
4816 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4817 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4819 update_btime(handle, smb_fname);
4824 if (is_afpinfo_stream(smb_fname)) {
4825 rc = fruit_stat_meta(handle, smb_fname, false);
4826 } else if (is_afpresource_stream(smb_fname)) {
4827 rc = fruit_stat_rsrc(handle, smb_fname, false);
4829 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4833 update_btime(handle, smb_fname);
4834 smb_fname->st.st_ex_mode &= ~S_IFMT;
4835 smb_fname->st.st_ex_mode |= S_IFREG;
4836 smb_fname->st.st_ex_blocks =
4837 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4842 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4844 SMB_STRUCT_STAT *sbuf)
4846 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4849 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4851 SMB_STRUCT_STAT *sbuf)
4855 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4860 *sbuf = fsp->base_fsp->fsp_name->st;
4861 sbuf->st_ex_size = AFP_INFO_SIZE;
4862 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4867 static int fruit_fstat_meta(vfs_handle_struct *handle,
4869 SMB_STRUCT_STAT *sbuf,
4874 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4876 switch (fio->config->meta) {
4877 case FRUIT_META_STREAM:
4878 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4881 case FRUIT_META_NETATALK:
4882 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4886 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4890 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4894 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4896 SMB_STRUCT_STAT *sbuf)
4898 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4901 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4903 SMB_STRUCT_STAT *sbuf)
4905 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4908 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4910 SMB_STRUCT_STAT *sbuf)
4912 struct adouble *ad = NULL;
4915 /* Populate the stat struct with info from the base file. */
4916 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4921 ad = ad_get(talloc_tos(), handle,
4922 fsp->base_fsp->fsp_name,
4925 DBG_ERR("ad_get [%s] failed [%s]\n",
4926 fsp_str_dbg(fsp), strerror(errno));
4930 *sbuf = fsp->base_fsp->fsp_name->st;
4931 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4932 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4938 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4939 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4943 switch (fio->config->rsrc) {
4944 case FRUIT_RSRC_STREAM:
4945 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4948 case FRUIT_RSRC_ADFILE:
4949 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4952 case FRUIT_RSRC_XATTR:
4953 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4957 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4964 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4965 SMB_STRUCT_STAT *sbuf)
4967 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4971 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4974 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4976 if (fio->type == ADOUBLE_META) {
4977 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4979 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4983 sbuf->st_ex_mode &= ~S_IFMT;
4984 sbuf->st_ex_mode |= S_IFREG;
4985 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4988 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4989 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4993 static NTSTATUS delete_invalid_meta_stream(
4994 vfs_handle_struct *handle,
4995 const struct smb_filename *smb_fname,
4996 TALLOC_CTX *mem_ctx,
4997 unsigned int *pnum_streams,
4998 struct stream_struct **pstreams)
5000 struct smb_filename *sname = NULL;
5004 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
5006 return NT_STATUS_INTERNAL_ERROR;
5009 sname = synthetic_smb_fname(talloc_tos(),
5010 smb_fname->base_name,
5011 AFPINFO_STREAM_NAME,
5013 if (sname == NULL) {
5014 return NT_STATUS_NO_MEMORY;
5017 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5020 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5021 return map_nt_error_from_unix(errno);
5024 return NT_STATUS_OK;
5027 static NTSTATUS fruit_streaminfo_meta_stream(
5028 vfs_handle_struct *handle,
5029 struct files_struct *fsp,
5030 const struct smb_filename *smb_fname,
5031 TALLOC_CTX *mem_ctx,
5032 unsigned int *pnum_streams,
5033 struct stream_struct **pstreams)
5035 struct stream_struct *stream = *pstreams;
5036 unsigned int num_streams = *pnum_streams;
5037 struct smb_filename *sname = NULL;
5038 char *full_name = NULL;
5040 struct share_mode_lock *lck = NULL;
5041 struct file_id id = {0};
5042 bool delete_on_close_set;
5048 for (i = 0; i < num_streams; i++) {
5049 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5054 if (i == num_streams) {
5055 return NT_STATUS_OK;
5058 if (stream[i].size != AFP_INFO_SIZE) {
5059 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5060 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5062 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5063 pnum_streams, pstreams);
5067 * Now check if there's a delete-on-close pending on the stream. If so,
5068 * hide the stream. This behaviour was verified against a macOS 10.12
5072 sname = synthetic_smb_fname(talloc_tos(),
5073 smb_fname->base_name,
5074 AFPINFO_STREAM_NAME,
5076 if (sname == NULL) {
5077 status = NT_STATUS_NO_MEMORY;
5081 ret = SMB_VFS_NEXT_STAT(handle, sname);
5083 status = map_nt_error_from_unix(errno);
5087 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5089 lck = get_existing_share_mode_lock(talloc_tos(), id);
5091 status = NT_STATUS_OK;
5095 full_name = talloc_asprintf(talloc_tos(),
5099 if (full_name == NULL) {
5100 status = NT_STATUS_NO_MEMORY;
5104 status = file_name_hash(handle->conn, full_name, &name_hash);
5105 if (!NT_STATUS_IS_OK(status)) {
5109 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5110 if (delete_on_close_set) {
5111 ok = del_fruit_stream(mem_ctx,
5116 status = NT_STATUS_INTERNAL_ERROR;
5121 status = NT_STATUS_OK;
5126 TALLOC_FREE(full_name);
5130 static NTSTATUS fruit_streaminfo_meta_netatalk(
5131 vfs_handle_struct *handle,
5132 struct files_struct *fsp,
5133 const struct smb_filename *smb_fname,
5134 TALLOC_CTX *mem_ctx,
5135 unsigned int *pnum_streams,
5136 struct stream_struct **pstreams)
5138 struct stream_struct *stream = *pstreams;
5139 unsigned int num_streams = *pnum_streams;
5140 struct adouble *ad = NULL;
5145 /* Remove the Netatalk xattr from the list */
5146 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5147 ":" NETATALK_META_XATTR ":$DATA");
5149 return NT_STATUS_NO_MEMORY;
5153 * Check if there's a AFPINFO_STREAM from the VFS streams
5154 * backend and if yes, remove it from the list
5156 for (i = 0; i < num_streams; i++) {
5157 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5162 if (i < num_streams) {
5163 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5164 smb_fname_str_dbg(smb_fname));
5166 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5169 return NT_STATUS_INTERNAL_ERROR;
5173 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5175 return NT_STATUS_OK;
5178 is_fi_empty = ad_empty_finderinfo(ad);
5182 return NT_STATUS_OK;
5185 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5186 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5187 smb_roundup(handle->conn, AFP_INFO_SIZE));
5189 return NT_STATUS_NO_MEMORY;
5192 return NT_STATUS_OK;
5195 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5196 struct files_struct *fsp,
5197 const struct smb_filename *smb_fname,
5198 TALLOC_CTX *mem_ctx,
5199 unsigned int *pnum_streams,
5200 struct stream_struct **pstreams)
5202 struct fruit_config_data *config = NULL;
5205 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5206 return NT_STATUS_INTERNAL_ERROR);
5208 switch (config->meta) {
5209 case FRUIT_META_NETATALK:
5210 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5211 mem_ctx, pnum_streams,
5215 case FRUIT_META_STREAM:
5216 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5217 mem_ctx, pnum_streams,
5222 return NT_STATUS_INTERNAL_ERROR;
5228 static NTSTATUS fruit_streaminfo_rsrc_stream(
5229 vfs_handle_struct *handle,
5230 struct files_struct *fsp,
5231 const struct smb_filename *smb_fname,
5232 TALLOC_CTX *mem_ctx,
5233 unsigned int *pnum_streams,
5234 struct stream_struct **pstreams)
5238 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5240 DBG_ERR("Filtering resource stream failed\n");
5241 return NT_STATUS_INTERNAL_ERROR;
5243 return NT_STATUS_OK;
5246 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5247 vfs_handle_struct *handle,
5248 struct files_struct *fsp,
5249 const struct smb_filename *smb_fname,
5250 TALLOC_CTX *mem_ctx,
5251 unsigned int *pnum_streams,
5252 struct stream_struct **pstreams)
5256 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5258 DBG_ERR("Filtering resource stream failed\n");
5259 return NT_STATUS_INTERNAL_ERROR;
5261 return NT_STATUS_OK;
5264 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5265 vfs_handle_struct *handle,
5266 struct files_struct *fsp,
5267 const struct smb_filename *smb_fname,
5268 TALLOC_CTX *mem_ctx,
5269 unsigned int *pnum_streams,
5270 struct stream_struct **pstreams)
5272 struct stream_struct *stream = *pstreams;
5273 unsigned int num_streams = *pnum_streams;
5274 struct adouble *ad = NULL;
5280 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5281 * and if yes, remove it from the list
5283 for (i = 0; i < num_streams; i++) {
5284 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5289 if (i < num_streams) {
5290 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5291 smb_fname_str_dbg(smb_fname));
5293 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5294 AFPRESOURCE_STREAM);
5296 return NT_STATUS_INTERNAL_ERROR;
5300 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5302 return NT_STATUS_OK;
5305 rlen = ad_getentrylen(ad, ADEID_RFORK);
5309 return NT_STATUS_OK;
5312 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5313 AFPRESOURCE_STREAM_NAME, rlen,
5314 smb_roundup(handle->conn, rlen));
5316 return NT_STATUS_NO_MEMORY;
5319 return NT_STATUS_OK;
5322 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5323 struct files_struct *fsp,
5324 const struct smb_filename *smb_fname,
5325 TALLOC_CTX *mem_ctx,
5326 unsigned int *pnum_streams,
5327 struct stream_struct **pstreams)
5329 struct fruit_config_data *config = NULL;
5332 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5333 return NT_STATUS_INTERNAL_ERROR);
5335 switch (config->rsrc) {
5336 case FRUIT_RSRC_STREAM:
5337 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5338 mem_ctx, pnum_streams,
5342 case FRUIT_RSRC_XATTR:
5343 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5344 mem_ctx, pnum_streams,
5348 case FRUIT_RSRC_ADFILE:
5349 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5350 mem_ctx, pnum_streams,
5355 return NT_STATUS_INTERNAL_ERROR;
5361 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5362 struct files_struct *fsp,
5363 const struct smb_filename *smb_fname,
5364 TALLOC_CTX *mem_ctx,
5365 unsigned int *pnum_streams,
5366 struct stream_struct **pstreams)
5368 struct fruit_config_data *config = NULL;
5371 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5372 return NT_STATUS_UNSUCCESSFUL);
5374 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5376 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5377 pnum_streams, pstreams);
5378 if (!NT_STATUS_IS_OK(status)) {
5382 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5383 mem_ctx, pnum_streams, pstreams);
5384 if (!NT_STATUS_IS_OK(status)) {
5388 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5389 mem_ctx, pnum_streams, pstreams);
5390 if (!NT_STATUS_IS_OK(status)) {
5394 return NT_STATUS_OK;
5397 static int fruit_ntimes(vfs_handle_struct *handle,
5398 const struct smb_filename *smb_fname,
5399 struct smb_file_time *ft)
5402 struct adouble *ad = NULL;
5403 struct fruit_config_data *config = NULL;
5405 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5408 if ((config->meta != FRUIT_META_NETATALK) ||
5409 null_timespec(ft->create_time))
5411 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5414 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5415 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5417 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5422 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5423 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5425 rc = ad_set(ad, smb_fname);
5431 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5434 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5437 static int fruit_fallocate(struct vfs_handle_struct *handle,
5438 struct files_struct *fsp,
5443 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5446 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5449 /* Let the pwrite code path handle it. */
5454 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5455 struct files_struct *fsp,
5459 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5462 #ifdef HAVE_ATTROPEN
5463 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5468 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5469 struct files_struct *fsp,
5473 struct adouble *ad = NULL;
5476 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5478 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5479 fsp_str_dbg(fsp), strerror(errno));
5483 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5485 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5491 ad_setentrylen(ad, ADEID_RFORK, offset);
5493 rc = ad_fset(ad, fsp);
5495 DBG_ERR("ad_fset [%s] failed [%s]\n",
5496 fsp_str_dbg(fsp), strerror(errno));
5505 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5506 struct files_struct *fsp,
5510 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5513 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5516 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5517 struct files_struct *fsp,
5520 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5524 DBG_ERR("Failed to fetch fsp extension");
5528 switch (fio->config->rsrc) {
5529 case FRUIT_RSRC_XATTR:
5530 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5533 case FRUIT_RSRC_ADFILE:
5534 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5537 case FRUIT_RSRC_STREAM:
5538 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5542 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5550 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5551 struct files_struct *fsp,
5555 DBG_WARNING("ftruncate %s to %jd",
5556 fsp_str_dbg(fsp), (intmax_t)offset);
5557 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5562 /* OS X returns success but does nothing */
5563 DBG_INFO("ignoring ftruncate %s to %jd\n",
5564 fsp_str_dbg(fsp), (intmax_t)offset);
5568 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5569 struct files_struct *fsp,
5572 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5575 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5580 global_fruit_config.nego_aapl &&
5581 is_ntfs_stream_smb_fname(fsp->fsp_name) &&
5582 !is_ntfs_default_stream_smb_fname(fsp->fsp_name))
5584 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5586 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5589 if (fio->type == ADOUBLE_META) {
5590 ret = fruit_ftruncate_meta(handle, fsp, offset);
5592 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5595 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5599 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5600 struct smb_request *req,
5601 uint16_t root_dir_fid,
5602 struct smb_filename *smb_fname,
5603 uint32_t access_mask,
5604 uint32_t share_access,
5605 uint32_t create_disposition,
5606 uint32_t create_options,
5607 uint32_t file_attributes,
5608 uint32_t oplock_request,
5609 struct smb2_lease *lease,
5610 uint64_t allocation_size,
5611 uint32_t private_flags,
5612 struct security_descriptor *sd,
5613 struct ea_list *ea_list,
5614 files_struct **result,
5616 const struct smb2_create_blobs *in_context_blobs,
5617 struct smb2_create_blobs *out_context_blobs)
5620 struct fruit_config_data *config = NULL;
5621 files_struct *fsp = NULL;
5623 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5624 if (!NT_STATUS_IS_OK(status)) {
5628 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5629 return NT_STATUS_UNSUCCESSFUL);
5631 status = SMB_VFS_NEXT_CREATE_FILE(
5632 handle, req, root_dir_fid, smb_fname,
5633 access_mask, share_access,
5634 create_disposition, create_options,
5635 file_attributes, oplock_request,
5637 allocation_size, private_flags,
5638 sd, ea_list, result,
5639 pinfo, in_context_blobs, out_context_blobs);
5640 if (!NT_STATUS_IS_OK(status)) {
5646 if (global_fruit_config.nego_aapl) {
5647 if (config->posix_rename && fsp->is_directory) {
5649 * Enable POSIX directory rename behaviour
5651 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5656 * If this is a plain open for existing files, opening an 0
5657 * byte size resource fork MUST fail with
5658 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5660 * Cf the vfs_fruit torture tests in test_rfork_create().
5662 if (is_afpresource_stream(fsp->fsp_name) &&
5663 create_disposition == FILE_OPEN)
5665 if (fsp->fsp_name->st.st_ex_size == 0) {
5666 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5671 if (is_ntfs_stream_smb_fname(smb_fname)
5672 || fsp->is_directory) {
5676 if (config->locking == FRUIT_LOCKING_NETATALK) {
5677 status = fruit_check_access(
5680 map_share_mode_to_deny_mode(share_access, 0));
5681 if (!NT_STATUS_IS_OK(status)) {
5689 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5692 close_file(req, fsp, ERROR_CLOSE);
5693 *result = fsp = NULL;
5699 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5700 const struct smb_filename *fname,
5701 TALLOC_CTX *mem_ctx,
5702 struct readdir_attr_data **pattr_data)
5704 struct fruit_config_data *config = NULL;
5705 struct readdir_attr_data *attr_data;
5708 SMB_VFS_HANDLE_GET_DATA(handle, config,
5709 struct fruit_config_data,
5710 return NT_STATUS_UNSUCCESSFUL);
5712 if (!global_fruit_config.nego_aapl) {
5713 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5716 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5718 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5719 if (*pattr_data == NULL) {
5720 return NT_STATUS_UNSUCCESSFUL;
5722 attr_data = *pattr_data;
5723 attr_data->type = RDATTR_AAPL;
5726 * Mac metadata: compressed FinderInfo, resource fork length
5729 status = readdir_attr_macmeta(handle, fname, attr_data);
5730 if (!NT_STATUS_IS_OK(status)) {
5732 * Error handling is tricky: if we return failure from
5733 * this function, the corresponding directory entry
5734 * will to be passed to the client, so we really just
5735 * want to error out on fatal errors.
5737 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5745 if (config->unix_info_enabled) {
5746 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5752 if (!config->readdir_attr_max_access) {
5753 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5755 status = smbd_calculate_access_mask(
5759 SEC_FLAG_MAXIMUM_ALLOWED,
5760 &attr_data->attr_data.aapl.max_access);
5761 if (!NT_STATUS_IS_OK(status)) {
5766 return NT_STATUS_OK;
5769 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5770 fname->base_name, nt_errstr(status)));
5771 TALLOC_FREE(*pattr_data);
5775 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5777 uint32_t security_info,
5778 TALLOC_CTX *mem_ctx,
5779 struct security_descriptor **ppdesc)
5782 struct security_ace ace;
5784 struct fruit_config_data *config;
5786 SMB_VFS_HANDLE_GET_DATA(handle, config,
5787 struct fruit_config_data,
5788 return NT_STATUS_UNSUCCESSFUL);
5790 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5792 if (!NT_STATUS_IS_OK(status)) {
5797 * Add MS NFS style ACEs with uid, gid and mode
5799 if (!global_fruit_config.nego_aapl) {
5800 return NT_STATUS_OK;
5802 if (!config->unix_info_enabled) {
5803 return NT_STATUS_OK;
5806 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5807 status = remove_virtual_nfs_aces(*ppdesc);
5808 if (!NT_STATUS_IS_OK(status)) {
5809 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5813 /* MS NFS style mode */
5814 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5815 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5816 status = security_descriptor_dacl_add(*ppdesc, &ace);
5817 if (!NT_STATUS_IS_OK(status)) {
5818 DEBUG(1,("failed to add MS NFS style ACE\n"));
5822 /* MS NFS style uid */
5823 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5824 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5825 status = security_descriptor_dacl_add(*ppdesc, &ace);
5826 if (!NT_STATUS_IS_OK(status)) {
5827 DEBUG(1,("failed to add MS NFS style ACE\n"));
5831 /* MS NFS style gid */
5832 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5833 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5834 status = security_descriptor_dacl_add(*ppdesc, &ace);
5835 if (!NT_STATUS_IS_OK(status)) {
5836 DEBUG(1,("failed to add MS NFS style ACE\n"));
5840 return NT_STATUS_OK;
5843 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5845 uint32_t security_info_sent,
5846 const struct security_descriptor *orig_psd)
5850 mode_t ms_nfs_mode = 0;
5852 struct security_descriptor *psd = NULL;
5853 uint32_t orig_num_aces = 0;
5855 if (orig_psd->dacl != NULL) {
5856 orig_num_aces = orig_psd->dacl->num_aces;
5859 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5861 return NT_STATUS_NO_MEMORY;
5864 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5866 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5867 if (!NT_STATUS_IS_OK(status)) {
5868 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5874 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5875 * sent/present flags correctly now we've removed them.
5878 if (orig_num_aces != 0) {
5880 * Are there any ACE's left ?
5882 if (psd->dacl->num_aces == 0) {
5883 /* No - clear the DACL sent/present flags. */
5884 security_info_sent &= ~SECINFO_DACL;
5885 psd->type &= ~SEC_DESC_DACL_PRESENT;
5889 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5890 if (!NT_STATUS_IS_OK(status)) {
5891 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5897 if (fsp->fh->fd != -1) {
5898 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5900 result = SMB_VFS_CHMOD(fsp->conn,
5906 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5907 result, (unsigned)ms_nfs_mode,
5909 status = map_nt_error_from_unix(errno);
5916 return NT_STATUS_OK;
5919 static struct vfs_offload_ctx *fruit_offload_ctx;
5921 struct fruit_offload_read_state {
5922 struct vfs_handle_struct *handle;
5923 struct tevent_context *ev;
5929 static void fruit_offload_read_done(struct tevent_req *subreq);
5931 static struct tevent_req *fruit_offload_read_send(
5932 TALLOC_CTX *mem_ctx,
5933 struct tevent_context *ev,
5934 struct vfs_handle_struct *handle,
5941 struct tevent_req *req = NULL;
5942 struct tevent_req *subreq = NULL;
5943 struct fruit_offload_read_state *state = NULL;
5945 req = tevent_req_create(mem_ctx, &state,
5946 struct fruit_offload_read_state);
5950 *state = (struct fruit_offload_read_state) {
5957 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5958 fsctl, ttl, offset, to_copy);
5959 if (tevent_req_nomem(subreq, req)) {
5960 return tevent_req_post(req, ev);
5962 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5966 static void fruit_offload_read_done(struct tevent_req *subreq)
5968 struct tevent_req *req = tevent_req_callback_data(
5969 subreq, struct tevent_req);
5970 struct fruit_offload_read_state *state = tevent_req_data(
5971 req, struct fruit_offload_read_state);
5974 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5978 TALLOC_FREE(subreq);
5979 if (tevent_req_nterror(req, status)) {
5983 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5984 tevent_req_done(req);
5988 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5989 &fruit_offload_ctx);
5990 if (tevent_req_nterror(req, status)) {
5994 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5997 if (tevent_req_nterror(req, status)) {
6001 tevent_req_done(req);
6005 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6006 struct vfs_handle_struct *handle,
6007 TALLOC_CTX *mem_ctx,
6010 struct fruit_offload_read_state *state = tevent_req_data(
6011 req, struct fruit_offload_read_state);
6014 if (tevent_req_is_nterror(req, &status)) {
6015 tevent_req_received(req);
6019 token->length = state->token.length;
6020 token->data = talloc_move(mem_ctx, &state->token.data);
6022 tevent_req_received(req);
6023 return NT_STATUS_OK;
6026 struct fruit_offload_write_state {
6027 struct vfs_handle_struct *handle;
6029 struct files_struct *src_fsp;
6030 struct files_struct *dst_fsp;
6034 static void fruit_offload_write_done(struct tevent_req *subreq);
6035 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6036 TALLOC_CTX *mem_ctx,
6037 struct tevent_context *ev,
6040 off_t transfer_offset,
6041 struct files_struct *dest_fsp,
6045 struct tevent_req *req, *subreq;
6046 struct fruit_offload_write_state *state;
6048 struct fruit_config_data *config;
6049 off_t src_off = transfer_offset;
6050 files_struct *src_fsp = NULL;
6051 off_t to_copy = num;
6052 bool copyfile_enabled = false;
6054 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6055 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6057 SMB_VFS_HANDLE_GET_DATA(handle, config,
6058 struct fruit_config_data,
6061 req = tevent_req_create(mem_ctx, &state,
6062 struct fruit_offload_write_state);
6066 state->handle = handle;
6067 state->dst_fsp = dest_fsp;
6070 case FSCTL_SRV_COPYCHUNK:
6071 case FSCTL_SRV_COPYCHUNK_WRITE:
6072 copyfile_enabled = config->copyfile_enabled;
6079 * Check if this a OS X copyfile style copychunk request with
6080 * a requested chunk count of 0 that was translated to a
6081 * offload_write_send VFS call overloading the parameters src_off
6082 * = dest_off = num = 0.
6084 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6085 status = vfs_offload_token_db_fetch_fsp(
6086 fruit_offload_ctx, token, &src_fsp);
6087 if (tevent_req_nterror(req, status)) {
6088 return tevent_req_post(req, ev);
6090 state->src_fsp = src_fsp;
6092 status = vfs_stat_fsp(src_fsp);
6093 if (tevent_req_nterror(req, status)) {
6094 return tevent_req_post(req, ev);
6097 to_copy = src_fsp->fsp_name->st.st_ex_size;
6098 state->is_copyfile = true;
6101 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6110 if (tevent_req_nomem(subreq, req)) {
6111 return tevent_req_post(req, ev);
6114 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6118 static void fruit_offload_write_done(struct tevent_req *subreq)
6120 struct tevent_req *req = tevent_req_callback_data(
6121 subreq, struct tevent_req);
6122 struct fruit_offload_write_state *state = tevent_req_data(
6123 req, struct fruit_offload_write_state);
6125 unsigned int num_streams = 0;
6126 struct stream_struct *streams = NULL;
6128 struct smb_filename *src_fname_tmp = NULL;
6129 struct smb_filename *dst_fname_tmp = NULL;
6131 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6134 TALLOC_FREE(subreq);
6135 if (tevent_req_nterror(req, status)) {
6139 if (!state->is_copyfile) {
6140 tevent_req_done(req);
6145 * Now copy all remaining streams. We know the share supports
6146 * streams, because we're in vfs_fruit. We don't do this async
6147 * because streams are few and small.
6149 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6150 state->src_fsp->fsp_name,
6151 req, &num_streams, &streams);
6152 if (tevent_req_nterror(req, status)) {
6156 if (num_streams == 1) {
6157 /* There is always one stream, ::$DATA. */
6158 tevent_req_done(req);
6162 for (i = 0; i < num_streams; i++) {
6163 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6164 __func__, streams[i].name, (size_t)streams[i].size));
6166 src_fname_tmp = synthetic_smb_fname(
6168 state->src_fsp->fsp_name->base_name,
6171 state->src_fsp->fsp_name->flags);
6172 if (tevent_req_nomem(src_fname_tmp, req)) {
6176 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6177 TALLOC_FREE(src_fname_tmp);
6181 dst_fname_tmp = synthetic_smb_fname(
6183 state->dst_fsp->fsp_name->base_name,
6186 state->dst_fsp->fsp_name->flags);
6187 if (tevent_req_nomem(dst_fname_tmp, req)) {
6188 TALLOC_FREE(src_fname_tmp);
6192 status = copy_file(req,
6193 state->handle->conn,
6196 OPENX_FILE_CREATE_IF_NOT_EXIST,
6198 if (!NT_STATUS_IS_OK(status)) {
6199 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6200 smb_fname_str_dbg(src_fname_tmp),
6201 smb_fname_str_dbg(dst_fname_tmp),
6202 nt_errstr(status)));
6203 TALLOC_FREE(src_fname_tmp);
6204 TALLOC_FREE(dst_fname_tmp);
6205 tevent_req_nterror(req, status);
6209 TALLOC_FREE(src_fname_tmp);
6210 TALLOC_FREE(dst_fname_tmp);
6213 TALLOC_FREE(streams);
6214 TALLOC_FREE(src_fname_tmp);
6215 TALLOC_FREE(dst_fname_tmp);
6216 tevent_req_done(req);
6219 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6220 struct tevent_req *req,
6223 struct fruit_offload_write_state *state = tevent_req_data(
6224 req, struct fruit_offload_write_state);
6227 if (tevent_req_is_nterror(req, &status)) {
6228 DEBUG(1, ("server side copy chunk failed: %s\n",
6229 nt_errstr(status)));
6231 tevent_req_received(req);
6235 *copied = state->copied;
6236 tevent_req_received(req);
6238 return NT_STATUS_OK;
6241 static char *fruit_get_bandsize_line(char **lines, int numlines)
6244 static bool re_initialized = false;
6248 if (!re_initialized) {
6249 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6253 re_initialized = true;
6256 for (i = 0; i < numlines; i++) {
6257 regmatch_t matches[1];
6259 ret = regexec(&re, lines[i], 1, matches, 0);
6262 * Check if the match was on the last line, sa we want
6263 * the subsequent line.
6265 if (i + 1 == numlines) {
6268 return lines[i + 1];
6270 if (ret != REG_NOMATCH) {
6278 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6281 static bool re_initialized = false;
6282 regmatch_t matches[2];
6287 if (!re_initialized) {
6290 "<integer>\\([[:digit:]]*\\)</integer>$",
6295 re_initialized = true;
6298 ret = regexec(&re, line, 2, matches, 0);
6300 DBG_ERR("regex failed [%s]\n", line);
6304 line[matches[1].rm_eo] = '\0';
6306 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6310 *_band_size = (size_t)band_size;
6315 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6316 * "band-size" key and value.
6318 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6322 #define INFO_PLIST_MAX_SIZE 64*1024
6324 struct smb_filename *smb_fname = NULL;
6325 files_struct *fsp = NULL;
6326 uint8_t *file_data = NULL;
6327 char **lines = NULL;
6328 char *band_size_line = NULL;
6329 size_t plist_file_size;
6336 plist = talloc_asprintf(talloc_tos(),
6338 handle->conn->connectpath,
6340 if (plist == NULL) {
6345 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6346 if (smb_fname == NULL) {
6351 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6353 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6358 plist_file_size = smb_fname->st.st_ex_size;
6360 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6361 DBG_INFO("%s is too large, ignoring\n", plist);
6366 status = SMB_VFS_NEXT_CREATE_FILE(
6369 0, /* root_dir_fid */
6370 smb_fname, /* fname */
6371 FILE_GENERIC_READ, /* access_mask */
6372 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6373 FILE_OPEN, /* create_disposition */
6374 0, /* create_options */
6375 0, /* file_attributes */
6376 INTERNAL_OPEN_ONLY, /* oplock_request */
6378 0, /* allocation_size */
6379 0, /* private_flags */
6384 NULL, NULL); /* create context */
6385 if (!NT_STATUS_IS_OK(status)) {
6386 DBG_INFO("Opening [%s] failed [%s]\n",
6387 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6392 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6393 if (file_data == NULL) {
6398 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6399 if (nread != plist_file_size) {
6400 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6401 fsp_str_dbg(fsp), nread, plist_file_size);
6407 status = close_file(NULL, fsp, NORMAL_CLOSE);
6409 if (!NT_STATUS_IS_OK(status)) {
6410 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6415 lines = file_lines_parse((char *)file_data,
6419 if (lines == NULL) {
6424 band_size_line = fruit_get_bandsize_line(lines, numlines);
6425 if (band_size_line == NULL) {
6426 DBG_ERR("Didn't find band-size key in [%s]\n",
6427 smb_fname_str_dbg(smb_fname));
6432 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6434 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6438 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6442 status = close_file(NULL, fsp, NORMAL_CLOSE);
6443 if (!NT_STATUS_IS_OK(status)) {
6444 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6449 TALLOC_FREE(smb_fname);
6450 TALLOC_FREE(file_data);
6455 struct fruit_disk_free_state {
6459 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6464 struct smb_filename *bands_dir = NULL;
6466 struct dirent *e = NULL;
6470 path = talloc_asprintf(talloc_tos(),
6472 handle->conn->connectpath,
6478 bands_dir = synthetic_smb_fname(talloc_tos(),
6484 if (bands_dir == NULL) {
6488 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6490 TALLOC_FREE(bands_dir);
6496 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6498 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6500 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6506 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6508 TALLOC_FREE(bands_dir);
6512 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6514 TALLOC_FREE(bands_dir);
6520 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6521 struct fruit_disk_free_state *state,
6526 size_t sparsebundle_strlen = strlen("sparsebundle");
6527 size_t bandsize = 0;
6531 p = strstr(e->d_name, "sparsebundle");
6536 if (p[sparsebundle_strlen] != '\0') {
6540 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6542 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6545 * Beware of race conditions: this may be an uninitialized
6546 * Info.plist that a client is just creating. We don't want let
6547 * this to trigger complete failure.
6549 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6553 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6556 * Beware of race conditions: this may be a backup sparsebundle
6557 * in an early stage lacking a bands subdirectory. We don't want
6558 * let this to trigger complete failure.
6560 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6564 if (bandsize > SIZE_MAX/nbands) {
6565 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6569 tm_size = bandsize * nbands;
6571 if (state->total_size + tm_size < state->total_size) {
6572 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6577 state->total_size += tm_size;
6579 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6580 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6586 * Calculate used size of a TimeMachine volume
6588 * This assumes that the volume is used only for TimeMachine.
6590 * - readdir(basedir of share), then
6591 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6592 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6593 * - count band files in "\1.sparsebundle/bands/"
6594 * - calculate used size of all bands: band_count * band_size
6596 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6597 const struct smb_filename *smb_fname,
6602 struct fruit_config_data *config = NULL;
6603 struct fruit_disk_free_state state = {0};
6605 struct dirent *e = NULL;
6611 SMB_VFS_HANDLE_GET_DATA(handle, config,
6612 struct fruit_config_data,
6615 if (!config->time_machine ||
6616 config->time_machine_max_size == 0)
6618 return SMB_VFS_NEXT_DISK_FREE(handle,
6625 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6630 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6632 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6634 ok = fruit_tmsize_do_dirent(handle, &state, e);
6636 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6641 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6646 dsize = config->time_machine_max_size / 512;
6647 dfree = dsize - (state.total_size / 512);
6648 if (dfree > dsize) {
6658 static struct vfs_fn_pointers vfs_fruit_fns = {
6659 .connect_fn = fruit_connect,
6660 .disk_free_fn = fruit_disk_free,
6662 /* File operations */
6663 .chmod_fn = fruit_chmod,
6664 .chown_fn = fruit_chown,
6665 .unlink_fn = fruit_unlink,
6666 .rename_fn = fruit_rename,
6667 .rmdir_fn = fruit_rmdir,
6668 .open_fn = fruit_open,
6669 .pread_fn = fruit_pread,
6670 .pwrite_fn = fruit_pwrite,
6671 .pread_send_fn = fruit_pread_send,
6672 .pread_recv_fn = fruit_pread_recv,
6673 .pwrite_send_fn = fruit_pwrite_send,
6674 .pwrite_recv_fn = fruit_pwrite_recv,
6675 .stat_fn = fruit_stat,
6676 .lstat_fn = fruit_lstat,
6677 .fstat_fn = fruit_fstat,
6678 .streaminfo_fn = fruit_streaminfo,
6679 .ntimes_fn = fruit_ntimes,
6680 .ftruncate_fn = fruit_ftruncate,
6681 .fallocate_fn = fruit_fallocate,
6682 .create_file_fn = fruit_create_file,
6683 .readdir_attr_fn = fruit_readdir_attr,
6684 .offload_read_send_fn = fruit_offload_read_send,
6685 .offload_read_recv_fn = fruit_offload_read_recv,
6686 .offload_write_send_fn = fruit_offload_write_send,
6687 .offload_write_recv_fn = fruit_offload_write_recv,
6689 /* NT ACL operations */
6690 .fget_nt_acl_fn = fruit_fget_nt_acl,
6691 .fset_nt_acl_fn = fruit_fset_nt_acl,
6695 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6697 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6699 if (!NT_STATUS_IS_OK(ret)) {
6703 vfs_fruit_debug_level = debug_add_class("fruit");
6704 if (vfs_fruit_debug_level == -1) {
6705 vfs_fruit_debug_level = DBGC_VFS;
6706 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6709 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6710 "vfs_fruit_init","fruit",vfs_fruit_debug_level));