2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
98 static int vfs_fruit_debug_level = DBGC_VFS;
100 static struct global_fruit_config {
101 bool nego_aapl; /* client negotiated AAPL */
103 } global_fruit_config;
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
122 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
124 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
125 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
126 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
127 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
129 struct fruit_config_data {
130 enum fruit_rsrc rsrc;
131 enum fruit_meta meta;
132 enum fruit_locking locking;
133 enum fruit_encoding encoding;
134 bool use_aapl; /* config from smb.conf */
136 bool readdir_attr_enabled;
137 bool unix_info_enabled;
138 bool copyfile_enabled;
139 bool veto_appledouble;
141 bool aapl_zero_file_id;
144 off_t time_machine_max_size;
145 bool wipe_intentionally_left_blank_rfork;
146 bool delete_empty_adfiles;
149 * Additional options, all enabled by default,
150 * possibly useful for analyzing performance. The associated
151 * operations with each of them may be expensive, so having
152 * the chance to disable them individually gives a chance
153 * tweaking the setup for the particular usecase.
155 bool readdir_attr_rsize;
156 bool readdir_attr_finder_info;
157 bool readdir_attr_max_access;
160 static const struct enum_list fruit_rsrc[] = {
161 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
162 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
163 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
167 static const struct enum_list fruit_meta[] = {
168 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
169 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
173 static const struct enum_list fruit_locking[] = {
174 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
175 {FRUIT_LOCKING_NONE, "none"},
179 static const struct enum_list fruit_encoding[] = {
180 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
181 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
185 static const char *fruit_catia_maps =
186 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
187 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
188 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
189 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
190 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
191 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
192 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
193 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
194 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
195 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
198 /*****************************************************************************
199 * Defines, functions and data structures that deal with AppleDouble
200 *****************************************************************************/
203 * There are two AppleDouble blobs we deal with:
205 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
206 * metadata in an xattr
208 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
211 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
214 #define AD_VERSION2 0x00020000
215 #define AD_VERSION AD_VERSION2
218 * AppleDouble entry IDs.
220 #define ADEID_DFORK 1
221 #define ADEID_RFORK 2
223 #define ADEID_COMMENT 4
224 #define ADEID_ICONBW 5
225 #define ADEID_ICONCOL 6
226 #define ADEID_FILEI 7
227 #define ADEID_FILEDATESI 8
228 #define ADEID_FINDERI 9
229 #define ADEID_MACFILEI 10
230 #define ADEID_PRODOSFILEI 11
231 #define ADEID_MSDOSFILEI 12
232 #define ADEID_SHORTNAME 13
233 #define ADEID_AFPFILEI 14
236 /* Private Netatalk entries */
237 #define ADEID_PRIVDEV 16
238 #define ADEID_PRIVINO 17
239 #define ADEID_PRIVSYN 18
240 #define ADEID_PRIVID 19
241 #define ADEID_MAX (ADEID_PRIVID + 1)
244 * These are the real ids for the private entries,
245 * as stored in the adouble file
247 #define AD_DEV 0x80444556
248 #define AD_INO 0x80494E4F
249 #define AD_SYN 0x8053594E
250 #define AD_ID 0x8053567E
252 /* Number of actually used entries */
253 #define ADEID_NUM_XATTR 8
254 #define ADEID_NUM_DOT_UND 2
255 #define ADEID_NUM_RSRC_XATTR 1
257 /* AppleDouble magic */
258 #define AD_APPLESINGLE_MAGIC 0x00051600
259 #define AD_APPLEDOUBLE_MAGIC 0x00051607
260 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
262 /* Sizes of relevant entry bits */
263 #define ADEDLEN_MAGIC 4
264 #define ADEDLEN_VERSION 4
265 #define ADEDLEN_FILLER 16
266 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
267 #define AD_FILLER_TAG_OSX "Mac OS X " /* should be 16 bytes */
268 #define ADEDLEN_NENTRIES 2
269 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
270 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
271 #define AD_ENTRY_LEN_EID 4
272 #define AD_ENTRY_LEN_OFF 4
273 #define AD_ENTRY_LEN_LEN 4
274 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
277 #define ADEDLEN_NAME 255
278 #define ADEDLEN_COMMENT 200
279 #define ADEDLEN_FILEI 16
280 #define ADEDLEN_FINDERI 32
281 #define ADEDLEN_FILEDATESI 16
282 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
283 #define ADEDLEN_AFPFILEI 4
284 #define ADEDLEN_MACFILEI 4
285 #define ADEDLEN_PRODOSFILEI 8
286 #define ADEDLEN_MSDOSFILEI 2
287 #define ADEDLEN_DID 4
288 #define ADEDLEN_PRIVDEV 8
289 #define ADEDLEN_PRIVINO 8
290 #define ADEDLEN_PRIVSYN 8
291 #define ADEDLEN_PRIVID 4
294 #define ADEDOFF_MAGIC 0
295 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
296 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
297 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
299 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
300 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
301 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
302 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
303 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
305 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
306 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
307 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
308 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
310 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
311 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
312 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
314 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
315 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
316 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
317 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
318 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
319 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
321 #if AD_DATASZ_XATTR != 402
322 #error bad size for AD_DATASZ_XATTR
325 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
326 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
328 #if AD_DATASZ_DOT_UND != 82
329 #error bad size for AD_DATASZ_DOT_UND
333 * Sharemode locks fcntl() offsets
335 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
336 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
338 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
340 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
342 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
343 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
344 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
345 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
346 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
347 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
348 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
349 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
350 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
351 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
353 /* Time stuff we overload the bits a little */
354 #define AD_DATE_CREATE 0
355 #define AD_DATE_MODIFY 4
356 #define AD_DATE_BACKUP 8
357 #define AD_DATE_ACCESS 12
358 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
359 AD_DATE_BACKUP | AD_DATE_ACCESS)
360 #define AD_DATE_UNIX (1 << 10)
361 #define AD_DATE_START 0x80000000
362 #define AD_DATE_DELTA 946684800
363 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
364 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
366 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
367 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
368 #define AD_XATTR_HDR_SIZE 36
369 #define AD_XATTR_MAX_HDR_SIZE 65536
371 /* Accessor macros */
372 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
373 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
374 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
375 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
378 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
379 * representation as well as the on-disk format.
381 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
382 * the length of the FinderInfo entry is larger then 32 bytes. It is then
383 * preceeded with 2 bytes padding.
385 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
388 struct ad_xattr_header {
389 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
390 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
391 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
392 uint32_t adx_data_start; /* file offset to attribute data area */
393 uint32_t adx_data_length; /* length of attribute data area */
394 uint32_t adx_reserved[3];
396 uint16_t adx_num_attrs;
399 /* On-disk entries are aligned on 4 byte boundaries */
400 struct ad_xattr_entry {
401 uint32_t adx_offset; /* file offset to data */
402 uint32_t adx_length; /* size of attribute data */
404 uint8_t adx_namelen; /* included the NULL terminator */
405 char *adx_name; /* NULL-terminated UTF-8 name */
414 vfs_handle_struct *ad_handle;
417 adouble_type_t ad_type;
420 uint8_t ad_filler[ADEDLEN_FILLER];
421 struct ad_entry ad_eid[ADEID_MAX];
423 struct ad_xattr_header adx_header;
424 struct ad_xattr_entry *adx_entries;
427 struct ad_entry_order {
428 uint32_t id, offset, len;
431 /* Netatalk AppleDouble metadata xattr */
433 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
434 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
435 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
436 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
437 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
438 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
439 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
440 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
441 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
445 /* AppleDouble resource fork file (the ones prefixed by "._") */
447 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
448 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
449 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
454 * Fake AppleDouble entry oder for resource fork xattr. The xattr
455 * isn't an AppleDouble file, it simply contains the resource data,
456 * but in order to be able to use some API calls like ad_getentryoff()
457 * we build a fake/helper struct adouble with this entry order struct.
460 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
465 /* Conversion from enumerated id to on-disk AppleDouble id */
466 #define AD_EID_DISK(a) (set_eid[a])
467 static const uint32_t set_eid[] = {
468 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
469 AD_DEV, AD_INO, AD_SYN, AD_ID
472 static char empty_resourcefork[] = {
473 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
474 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E,
475 0x54, 0x68, 0x69, 0x73, 0x20, 0x72, 0x65, 0x73,
476 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x66, 0x6F,
477 0x72, 0x6B, 0x20, 0x69, 0x6E, 0x74, 0x65, 0x6E,
478 0x74, 0x69, 0x6F, 0x6E, 0x61, 0x6C, 0x6C, 0x79,
479 0x20, 0x6C, 0x65, 0x66, 0x74, 0x20, 0x62, 0x6C,
480 0x61, 0x6E, 0x6B, 0x20, 0x20, 0x20, 0x00, 0x00,
481 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
482 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
484 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
487 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
488 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
489 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
490 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
491 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
492 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
493 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
494 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
495 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
497 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
498 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
499 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
500 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
501 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
502 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
503 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
504 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
505 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
506 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E,
507 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
508 0x00, 0x1C, 0x00, 0x1E, 0xFF, 0xFF
512 /* tcon config handle */
513 struct fruit_config_data *config;
515 /* Denote stream type, meta or rsrc */
518 /* Whether the create created the stream */
522 * AFP_AfpInfo stream created, but not written yet, thus still a fake
523 * pipe fd. This is set to true in fruit_open_meta if there was no
524 * exisiting stream but the caller requested O_CREAT. It is later set to
525 * false when we get a write on the stream that then does open and
534 * Forward declarations
536 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
537 adouble_type_t type);
538 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
539 static int ad_fset(struct adouble *ad, files_struct *fsp);
540 static int adouble_path(TALLOC_CTX *ctx,
541 const struct smb_filename *smb_fname__in,
542 struct smb_filename **ppsmb_fname_out);
543 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
544 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
545 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
549 * Return a pointer to an AppleDouble entry
551 * Returns NULL if the entry is not present
553 static char *ad_get_entry(const struct adouble *ad, int eid)
555 off_t off = ad_getentryoff(ad, eid);
556 size_t len = ad_getentrylen(ad, eid);
558 if (off == 0 || len == 0) {
562 return ad->ad_data + off;
568 static int ad_getdate(const struct adouble *ad,
569 unsigned int dateoff,
572 bool xlate = (dateoff & AD_DATE_UNIX);
575 dateoff &= AD_DATE_MASK;
576 p = ad_get_entry(ad, ADEID_FILEDATESI);
581 if (dateoff > AD_DATE_ACCESS) {
585 memcpy(date, p + dateoff, sizeof(uint32_t));
588 *date = AD_DATE_TO_UNIX(*date);
596 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
598 bool xlate = (dateoff & AD_DATE_UNIX);
601 p = ad_get_entry(ad, ADEID_FILEDATESI);
606 dateoff &= AD_DATE_MASK;
608 date = AD_DATE_FROM_UNIX(date);
611 if (dateoff > AD_DATE_ACCESS) {
615 memcpy(p + dateoff, &date, sizeof(date));
622 * Map on-disk AppleDouble id to enumerated id
624 static uint32_t get_eid(uint32_t eid)
632 return ADEID_PRIVDEV;
634 return ADEID_PRIVINO;
636 return ADEID_PRIVSYN;
647 * Pack AppleDouble structure into data buffer
649 static bool ad_pack(struct adouble *ad)
656 bufsize = talloc_get_size(ad->ad_data);
657 if (bufsize < AD_DATASZ_DOT_UND) {
658 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
662 if (offset + ADEDLEN_MAGIC < offset ||
663 offset + ADEDLEN_MAGIC >= bufsize) {
666 RSIVAL(ad->ad_data, offset, ad->ad_magic);
667 offset += ADEDLEN_MAGIC;
669 if (offset + ADEDLEN_VERSION < offset ||
670 offset + ADEDLEN_VERSION >= bufsize) {
673 RSIVAL(ad->ad_data, offset, ad->ad_version);
674 offset += ADEDLEN_VERSION;
676 if (offset + ADEDLEN_FILLER < offset ||
677 offset + ADEDLEN_FILLER >= bufsize) {
680 if (ad->ad_type == ADOUBLE_RSRC) {
681 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
683 offset += ADEDLEN_FILLER;
685 if (offset + ADEDLEN_NENTRIES < offset ||
686 offset + ADEDLEN_NENTRIES >= bufsize) {
689 offset += ADEDLEN_NENTRIES;
691 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
692 if (ad->ad_eid[eid].ade_off == 0) {
694 * ade_off is also used as indicator whether a
695 * specific entry is used or not
700 if (offset + AD_ENTRY_LEN_EID < offset ||
701 offset + AD_ENTRY_LEN_EID >= bufsize) {
704 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
705 offset += AD_ENTRY_LEN_EID;
707 if (offset + AD_ENTRY_LEN_OFF < offset ||
708 offset + AD_ENTRY_LEN_OFF >= bufsize) {
711 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
712 offset += AD_ENTRY_LEN_OFF;
714 if (offset + AD_ENTRY_LEN_LEN < offset ||
715 offset + AD_ENTRY_LEN_LEN >= bufsize) {
718 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
719 offset += AD_ENTRY_LEN_LEN;
724 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
727 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
732 static bool ad_unpack_xattrs(struct adouble *ad)
734 struct ad_xattr_header *h = &ad->adx_header;
735 const char *p = ad->ad_data;
739 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
743 /* 2 bytes padding */
744 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
746 h->adx_magic = RIVAL(p, hoff + 0);
747 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
748 h->adx_total_size = RIVAL(p, hoff + 8);
749 h->adx_data_start = RIVAL(p, hoff + 12);
750 h->adx_data_length = RIVAL(p, hoff + 16);
751 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
752 h->adx_num_attrs = RSVAL(p, hoff + 34);
754 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
755 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
759 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
760 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
763 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
764 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
768 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
769 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
773 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
774 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
777 if ((h->adx_data_start + h->adx_data_length) >
778 ad->adx_header.adx_total_size)
780 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
784 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
785 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
789 if (h->adx_num_attrs == 0) {
793 ad->adx_entries = talloc_zero_array(
794 ad, struct ad_xattr_entry, h->adx_num_attrs);
795 if (ad->adx_entries == NULL) {
799 hoff += AD_XATTR_HDR_SIZE;
801 for (i = 0; i < h->adx_num_attrs; i++) {
802 struct ad_xattr_entry *e = &ad->adx_entries[i];
804 hoff = (hoff + 3) & ~3;
806 e->adx_offset = RIVAL(p, hoff + 0);
807 e->adx_length = RIVAL(p, hoff + 4);
808 e->adx_flags = RSVAL(p, hoff + 8);
809 e->adx_namelen = *(p + hoff + 10);
811 if (e->adx_offset >= ad->adx_header.adx_total_size) {
812 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
817 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
818 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
823 if ((e->adx_offset + e->adx_length) >
824 ad->adx_header.adx_total_size)
826 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
831 if (e->adx_namelen == 0) {
832 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
836 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
837 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
841 if ((hoff + 11 + e->adx_namelen) >
842 ad->adx_header.adx_data_start)
844 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
849 e->adx_name = talloc_strndup(ad->adx_entries,
852 if (e->adx_name == NULL) {
856 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
857 e->adx_name, e->adx_offset, e->adx_length);
858 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
861 hoff += 11 + e->adx_namelen;
868 * Unpack an AppleDouble blob into a struct adoble
870 static bool ad_unpack(struct adouble *ad, const size_t nentries,
873 size_t bufsize = talloc_get_size(ad->ad_data);
875 uint32_t eid, len, off;
879 * The size of the buffer ad->ad_data is checked when read, so
880 * we wouldn't have to check our own offsets, a few extra
881 * checks won't hurt though. We have to check the offsets we
882 * read from the buffer anyway.
885 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
886 DEBUG(1, ("bad size\n"));
890 ad->ad_magic = RIVAL(ad->ad_data, 0);
891 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
892 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
893 DEBUG(1, ("wrong magic or version\n"));
897 memcpy(ad->ad_filler, ad->ad_data + ADEDOFF_FILLER, ADEDLEN_FILLER);
899 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
900 if (adentries != nentries) {
901 DEBUG(1, ("invalid number of entries: %zu\n",
906 /* now, read in the entry bits */
907 for (i = 0; i < adentries; i++) {
908 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
910 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
911 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
913 if (!eid || eid >= ADEID_MAX) {
914 DEBUG(1, ("bogus eid %d\n", eid));
919 * All entries other than the resource fork are
920 * expected to be read into the ad_data buffer, so
921 * ensure the specified offset is within that bound
923 if ((off > bufsize) && (eid != ADEID_RFORK)) {
924 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
930 * All entries besides FinderInfo and resource fork
931 * must fit into the buffer. FinderInfo is special as
932 * it may be larger then the default 32 bytes (if it
933 * contains marshalled xattrs), but we will fixup that
934 * in ad_convert(). And the resource fork is never
935 * accessed directly by the ad_data buf (also see
936 * comment above) anyway.
938 if ((eid != ADEID_RFORK) &&
939 (eid != ADEID_FINDERI) &&
940 ((off + len) > bufsize)) {
941 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
947 * That would be obviously broken
949 if (off > filesize) {
950 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
956 * Check for any entry that has its end beyond the
959 if (off + len < off) {
960 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
961 ", len: %" PRIu32 "\n",
966 if (off + len > filesize) {
968 * If this is the resource fork entry, we fix
969 * up the length, for any other entry we bail
972 if (eid != ADEID_RFORK) {
973 DEBUG(1, ("bogus eid %d: off: %" PRIu32
974 ", len: %" PRIu32 "\n",
980 * Fixup the resource fork entry by limiting
981 * the size to entryoffset - filesize.
983 len = filesize - off;
984 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
985 ", len: %" PRIu32 "\n", off, len));
988 ad->ad_eid[eid].ade_off = off;
989 ad->ad_eid[eid].ade_len = len;
992 ok = ad_unpack_xattrs(ad);
1000 static bool ad_convert_move_reso(struct adouble *ad,
1001 const struct smb_filename *smb_fname)
1003 char *map = MAP_FAILED;
1009 if (ad_getentrylen(ad, ADEID_RFORK) == 0) {
1013 maplen = ad_getentryoff(ad, ADEID_RFORK) +
1014 ad_getentrylen(ad, ADEID_RFORK);
1016 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1017 map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
1019 if (map == MAP_FAILED) {
1020 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
1025 memmove(map + ADEDOFF_RFORK_DOT_UND,
1026 map + ad_getentryoff(ad, ADEID_RFORK),
1027 ad_getentrylen(ad, ADEID_RFORK));
1029 rc = munmap(map, maplen);
1031 DBG_ERR("munmap failed: %s\n", strerror(errno));
1035 ad_setentryoff(ad, ADEID_RFORK, ADEDOFF_RFORK_DOT_UND);
1039 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1043 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1044 if (len != AD_DATASZ_DOT_UND) {
1045 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1052 static bool ad_convert_xattr(struct adouble *ad,
1053 const struct smb_filename *smb_fname,
1054 bool *converted_xattr)
1056 static struct char_mappings **string_replace_cmaps = NULL;
1057 char *map = MAP_FAILED;
1061 int saved_errno = 0;
1066 *converted_xattr = false;
1068 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1072 if (string_replace_cmaps == NULL) {
1073 const char **mappings = NULL;
1075 mappings = str_list_make_v3_const(
1076 talloc_tos(), fruit_catia_maps, NULL);
1077 if (mappings == NULL) {
1080 string_replace_cmaps = string_replace_init_map(mappings);
1081 TALLOC_FREE(mappings);
1084 maplen = ad_getentryoff(ad, ADEID_RFORK) +
1085 ad_getentrylen(ad, ADEID_RFORK);
1087 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1088 map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
1090 if (map == MAP_FAILED) {
1091 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
1095 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
1096 struct ad_xattr_entry *e = &ad->adx_entries[i];
1097 char *mapped_name = NULL;
1099 struct smb_filename *stream_name = NULL;
1100 files_struct *fsp = NULL;
1103 status = string_replace_allocate(ad->ad_handle->conn,
1105 string_replace_cmaps,
1108 vfs_translate_to_windows);
1109 if (!NT_STATUS_IS_OK(status) &&
1110 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
1112 DBG_ERR("string_replace_allocate failed\n");
1118 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
1120 if (mapped_name == NULL) {
1125 stream_name = synthetic_smb_fname(talloc_tos(),
1126 smb_fname->base_name,
1130 TALLOC_FREE(mapped_name);
1131 if (stream_name == NULL) {
1132 DBG_ERR("synthetic_smb_fname failed\n");
1137 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1139 status = SMB_VFS_CREATE_FILE(
1140 ad->ad_handle->conn, /* conn */
1142 0, /* root_dir_fid */
1143 stream_name, /* fname */
1144 FILE_GENERIC_WRITE, /* access_mask */
1145 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1146 FILE_OPEN_IF, /* create_disposition */
1147 0, /* create_options */
1148 0, /* file_attributes */
1149 INTERNAL_OPEN_ONLY, /* oplock_request */
1151 0, /* allocation_size */
1152 0, /* private_flags */
1157 NULL, NULL); /* create context */
1158 TALLOC_FREE(stream_name);
1159 if (!NT_STATUS_IS_OK(status)) {
1160 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1165 nwritten = SMB_VFS_PWRITE(fsp,
1166 map + e->adx_offset,
1169 if (nwritten == -1) {
1170 DBG_ERR("SMB_VFS_PWRITE failed\n");
1171 saved_errno = errno;
1172 close_file(NULL, fsp, ERROR_CLOSE);
1173 errno = saved_errno;
1178 status = close_file(NULL, fsp, NORMAL_CLOSE);
1179 if (!NT_STATUS_IS_OK(status)) {
1186 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1190 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1194 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1195 if (len != AD_DATASZ_DOT_UND) {
1196 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1201 ok = ad_convert_move_reso(ad, smb_fname);
1206 *converted_xattr = true;
1210 rc = munmap(map, maplen);
1212 DBG_ERR("munmap failed: %s\n", strerror(errno));
1219 static bool ad_convert_finderinfo(struct adouble *ad,
1220 const struct smb_filename *smb_fname)
1225 struct smb_filename *stream_name = NULL;
1226 files_struct *fsp = NULL;
1230 int saved_errno = 0;
1233 cmp = memcmp(ad->ad_filler, AD_FILLER_TAG_OSX, ADEDLEN_FILLER);
1238 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1243 ai = afpinfo_new(talloc_tos());
1248 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1250 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1251 if (aiblob.data == NULL) {
1256 size = afpinfo_pack(ai, (char *)aiblob.data);
1258 if (size != AFP_INFO_SIZE) {
1262 stream_name = synthetic_smb_fname(talloc_tos(),
1263 smb_fname->base_name,
1267 if (stream_name == NULL) {
1268 data_blob_free(&aiblob);
1269 DBG_ERR("synthetic_smb_fname failed\n");
1273 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1275 status = SMB_VFS_CREATE_FILE(
1276 ad->ad_handle->conn, /* conn */
1278 0, /* root_dir_fid */
1279 stream_name, /* fname */
1280 FILE_GENERIC_WRITE, /* access_mask */
1281 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1282 FILE_OPEN_IF, /* create_disposition */
1283 0, /* create_options */
1284 0, /* file_attributes */
1285 INTERNAL_OPEN_ONLY, /* oplock_request */
1287 0, /* allocation_size */
1288 0, /* private_flags */
1293 NULL, NULL); /* create context */
1294 TALLOC_FREE(stream_name);
1295 if (!NT_STATUS_IS_OK(status)) {
1296 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1300 nwritten = SMB_VFS_PWRITE(fsp,
1304 if (nwritten == -1) {
1305 DBG_ERR("SMB_VFS_PWRITE failed\n");
1306 saved_errno = errno;
1307 close_file(NULL, fsp, ERROR_CLOSE);
1308 errno = saved_errno;
1312 status = close_file(NULL, fsp, NORMAL_CLOSE);
1313 if (!NT_STATUS_IS_OK(status)) {
1321 static bool ad_convert_truncate(struct adouble *ad,
1322 const struct smb_filename *smb_fname)
1327 * FIXME: direct ftruncate(), but we don't have a fsp for the
1330 rc = ftruncate(ad->ad_fd, ADEDOFF_RFORK_DOT_UND +
1331 ad_getentrylen(ad, ADEID_RFORK));
1339 static bool ad_convert_blank_rfork(struct adouble *ad,
1342 struct fruit_config_data *config = NULL;
1343 uint8_t *map = MAP_FAILED;
1352 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1353 struct fruit_config_data, return false);
1355 if (!config->wipe_intentionally_left_blank_rfork) {
1359 if (ad_getentrylen(ad, ADEID_RFORK) != sizeof(empty_resourcefork)) {
1363 maplen = ad_getentryoff(ad, ADEID_RFORK) +
1364 ad_getentrylen(ad, ADEID_RFORK);
1366 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1367 map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
1369 if (map == MAP_FAILED) {
1370 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
1374 cmp = memcmp(map + ADEDOFF_RFORK_DOT_UND,
1376 sizeof(empty_resourcefork));
1377 rc = munmap(map, maplen);
1379 DBG_ERR("munmap failed: %s\n", strerror(errno));
1387 ad_setentrylen(ad, ADEID_RFORK, 0);
1394 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1395 if (len != AD_DATASZ_DOT_UND) {
1403 static bool ad_convert_delete_adfile(struct adouble *ad,
1404 const struct smb_filename *smb_fname)
1406 struct fruit_config_data *config = NULL;
1407 struct smb_filename *ad_name = NULL;
1410 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1414 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1415 struct fruit_config_data, return false);
1417 if (!config->delete_empty_adfiles) {
1421 rc = adouble_path(talloc_tos(), smb_fname, &ad_name);
1426 rc = SMB_VFS_NEXT_UNLINK(ad->ad_handle, ad_name);
1428 DBG_ERR("Unlinking [%s] failed: %s\n",
1429 smb_fname_str_dbg(ad_name), strerror(errno));
1430 TALLOC_FREE(ad_name);
1434 DBG_WARNING("Unlinked [%s] after conversion\n", smb_fname_str_dbg(ad_name));
1435 TALLOC_FREE(ad_name);
1441 * Convert from Apple's ._ file to Netatalk
1443 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1444 * bytes containing packed xattrs.
1446 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1449 static int ad_convert(struct adouble *ad,
1450 const struct smb_filename *smb_fname)
1453 bool converted_xattr = false;
1456 ok = ad_convert_xattr(ad, smb_fname, &converted_xattr);
1461 ok = ad_convert_blank_rfork(ad, &blank);
1466 if (converted_xattr || blank) {
1467 ok = ad_convert_truncate(ad, smb_fname);
1473 ok = ad_convert_finderinfo(ad, smb_fname);
1475 DBG_ERR("Failed to convert [%s]\n",
1476 smb_fname_str_dbg(smb_fname));
1480 ok = ad_convert_delete_adfile(ad, smb_fname);
1489 * Read and parse Netatalk AppleDouble metadata xattr
1491 static ssize_t ad_read_meta(struct adouble *ad,
1492 const struct smb_filename *smb_fname)
1498 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1500 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1501 AFPINFO_EA_NETATALK, ad->ad_data,
1507 if (errno == ENOATTR) {
1513 DEBUG(2, ("error reading meta xattr: %s\n",
1519 if (ealen != AD_DATASZ_XATTR) {
1520 DEBUG(2, ("bad size %zd\n", ealen));
1526 /* Now parse entries */
1527 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1529 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1535 if (!ad_getentryoff(ad, ADEID_FINDERI)
1536 || !ad_getentryoff(ad, ADEID_COMMENT)
1537 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1538 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1539 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1540 || !ad_getentryoff(ad, ADEID_PRIVINO)
1541 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1542 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1543 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1550 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1551 smb_fname->base_name, rc));
1555 if (errno == EINVAL) {
1557 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1565 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1569 #ifdef HAVE_ATTROPEN
1570 /* FIXME: direct Solaris xattr syscall */
1571 return attropen(smb_fname->base_name,
1572 AFPRESOURCE_EA_NETATALK, flags, mode);
1579 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1585 struct smb_filename *adp_smb_fname = NULL;
1587 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1592 fd = open(adp_smb_fname->base_name, flags, mode);
1593 TALLOC_FREE(adp_smb_fname);
1598 static int ad_open_rsrc(vfs_handle_struct *handle,
1599 const struct smb_filename *smb_fname,
1603 struct fruit_config_data *config = NULL;
1606 SMB_VFS_HANDLE_GET_DATA(handle, config,
1607 struct fruit_config_data, return -1);
1609 if (config->rsrc == FRUIT_RSRC_XATTR) {
1610 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1612 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1619 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1620 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1621 * for file IO on the ._ file.
1623 static int ad_open(vfs_handle_struct *handle,
1626 const struct smb_filename *smb_fname,
1632 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1633 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1635 if (ad->ad_type == ADOUBLE_META) {
1639 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1640 ad->ad_fd = fsp->fh->fd;
1641 ad->ad_opened = false;
1645 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1649 ad->ad_opened = true;
1652 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1653 smb_fname->base_name,
1654 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1659 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1664 /* FIXME: direct sys_fstat(), don't have an fsp */
1665 ret = sys_fstat(ad->ad_fd, &st,
1666 lp_fake_directory_create_times(
1667 SNUM(ad->ad_handle->conn)));
1672 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1673 return st.st_ex_size;
1676 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1677 const struct smb_filename *smb_fname)
1679 SMB_STRUCT_STAT sbuf;
1686 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1687 SNUM(ad->ad_handle->conn)));
1693 * AppleDouble file header content and size, two cases:
1695 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1696 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1698 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1700 size = sbuf.st_ex_size;
1701 if (size > talloc_array_length(ad->ad_data)) {
1702 if (size > AD_XATTR_MAX_HDR_SIZE) {
1703 size = AD_XATTR_MAX_HDR_SIZE;
1705 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1712 len = sys_pread(ad->ad_fd, ad->ad_data,
1713 talloc_array_length(ad->ad_data), 0);
1714 if (len != talloc_array_length(ad->ad_data)) {
1715 DBG_NOTICE("%s %s: bad size: %zd\n",
1716 smb_fname->base_name, strerror(errno), len);
1720 /* Now parse entries */
1721 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1723 DBG_ERR("invalid AppleDouble resource %s\n",
1724 smb_fname->base_name);
1729 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1730 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1731 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1732 DBG_ERR("invalid AppleDouble resource %s\n",
1733 smb_fname->base_name);
1739 * Try to fixup AppleDouble files created by OS X with xattrs
1740 * appended to the ADEID_FINDERI entry.
1743 ret = ad_convert(ad, smb_fname);
1745 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1753 * Read and parse resource fork, either ._ AppleDouble file or xattr
1755 static ssize_t ad_read_rsrc(struct adouble *ad,
1756 const struct smb_filename *smb_fname)
1758 struct fruit_config_data *config = NULL;
1761 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1762 struct fruit_config_data, return -1);
1764 if (config->rsrc == FRUIT_RSRC_XATTR) {
1765 len = ad_read_rsrc_xattr(ad);
1767 len = ad_read_rsrc_adouble(ad, smb_fname);
1774 * Read and unpack an AppleDouble metadata xattr or resource
1776 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1778 switch (ad->ad_type) {
1780 return ad_read_meta(ad, smb_fname);
1782 return ad_read_rsrc(ad, smb_fname);
1788 static int adouble_destructor(struct adouble *ad)
1790 if ((ad->ad_fd != -1) && ad->ad_opened) {
1798 * Allocate a struct adouble without initialiing it
1800 * The struct is either hang of the fsp extension context or if fsp is
1803 * @param[in] ctx talloc context
1804 * @param[in] handle vfs handle
1805 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1807 * @return adouble handle
1809 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1810 adouble_type_t type)
1815 struct fruit_config_data *config;
1817 SMB_VFS_HANDLE_GET_DATA(handle, config,
1818 struct fruit_config_data, return NULL);
1822 adsize = AD_DATASZ_XATTR;
1825 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1826 adsize = AD_DATASZ_DOT_UND;
1833 ad = talloc_zero(ctx, struct adouble);
1840 ad->ad_data = talloc_zero_array(ad, char, adsize);
1841 if (ad->ad_data == NULL) {
1847 ad->ad_handle = handle;
1849 ad->ad_magic = AD_MAGIC;
1850 ad->ad_version = AD_VERSION;
1853 talloc_set_destructor(ad, adouble_destructor);
1863 * Allocate and initialize a new struct adouble
1865 * @param[in] ctx talloc context
1866 * @param[in] handle vfs handle
1867 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1869 * @return adouble handle, initialized
1871 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1872 adouble_type_t type)
1875 const struct ad_entry_order *eid;
1876 struct adouble *ad = NULL;
1877 struct fruit_config_data *config;
1878 time_t t = time(NULL);
1880 SMB_VFS_HANDLE_GET_DATA(handle, config,
1881 struct fruit_config_data, return NULL);
1885 eid = entry_order_meta_xattr;
1888 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1889 eid = entry_order_dot_und;
1891 eid = entry_order_rsrc_xattr;
1898 ad = ad_alloc(ctx, handle, type);
1904 ad->ad_eid[eid->id].ade_off = eid->offset;
1905 ad->ad_eid[eid->id].ade_len = eid->len;
1909 /* put something sane in the date fields */
1910 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1911 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1912 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1913 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1921 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1922 vfs_handle_struct *handle,
1924 const struct smb_filename *smb_fname,
1925 adouble_type_t type)
1929 struct adouble *ad = NULL;
1933 smb_fname = fsp->base_fsp->fsp_name;
1936 DEBUG(10, ("ad_get(%s) called for %s\n",
1937 type == ADOUBLE_META ? "meta" : "rsrc",
1938 smb_fname->base_name));
1940 ad = ad_alloc(ctx, handle, type);
1946 /* Try rw first so we can use the fd in ad_convert() */
1949 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1950 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1952 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1955 DBG_DEBUG("ad_open [%s] error [%s]\n",
1956 smb_fname->base_name, strerror(errno));
1961 len = ad_read(ad, smb_fname);
1963 DEBUG(10, ("error reading AppleDouble for %s\n",
1964 smb_fname->base_name));
1970 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1971 type == ADOUBLE_META ? "meta" : "rsrc",
1972 smb_fname->base_name, rc));
1981 * Return AppleDouble data for a file
1983 * @param[in] ctx talloc context
1984 * @param[in] handle vfs handle
1985 * @param[in] smb_fname pathname to file or directory
1986 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1988 * @return talloced struct adouble or NULL on error
1990 static struct adouble *ad_get(TALLOC_CTX *ctx,
1991 vfs_handle_struct *handle,
1992 const struct smb_filename *smb_fname,
1993 adouble_type_t type)
1995 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1999 * Return AppleDouble data for a file
2001 * @param[in] ctx talloc context
2002 * @param[in] handle vfs handle
2003 * @param[in] fsp fsp to use for IO
2004 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
2006 * @return talloced struct adouble or NULL on error
2008 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
2009 files_struct *fsp, adouble_type_t type)
2011 return ad_get_internal(ctx, handle, fsp, NULL, type);
2015 * Set AppleDouble metadata on a file or directory
2017 * @param[in] ad adouble handle
2019 * @param[in] smb_fname pathname to file or directory
2021 * @return status code, 0 means success
2023 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
2028 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
2030 if (ad->ad_type != ADOUBLE_META) {
2031 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
2032 smb_fname->base_name);
2041 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
2043 AFPINFO_EA_NETATALK,
2045 AD_DATASZ_XATTR, 0);
2047 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
2053 * Set AppleDouble metadata on a file or directory
2055 * @param[in] ad adouble handle
2056 * @param[in] fsp file handle
2058 * @return status code, 0 means success
2060 static int ad_fset(struct adouble *ad, files_struct *fsp)
2066 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
2069 || (fsp->fh == NULL)
2070 || (fsp->fh->fd == -1))
2072 smb_panic("bad fsp");
2080 switch (ad->ad_type) {
2082 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
2084 AFPINFO_EA_NETATALK,
2086 AD_DATASZ_XATTR, 0);
2090 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
2095 if (len != AD_DATASZ_DOT_UND) {
2096 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
2106 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
2111 /*****************************************************************************
2113 *****************************************************************************/
2115 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
2117 if (strncasecmp_m(smb_fname->stream_name,
2118 AFPINFO_STREAM_NAME,
2119 strlen(AFPINFO_STREAM_NAME)) == 0) {
2125 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
2127 if (strncasecmp_m(smb_fname->stream_name,
2128 AFPRESOURCE_STREAM_NAME,
2129 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
2136 * Test whether stream is an Apple stream, not used atm
2139 static bool is_apple_stream(const struct smb_filename *smb_fname)
2141 if (is_afpinfo_stream(smb_fname)) {
2144 if (is_afpresource_stream(smb_fname)) {
2152 * Initialize config struct from our smb.conf config parameters
2154 static int init_fruit_config(vfs_handle_struct *handle)
2156 struct fruit_config_data *config;
2158 const char *tm_size_str = NULL;
2160 config = talloc_zero(handle->conn, struct fruit_config_data);
2162 DEBUG(1, ("talloc_zero() failed\n"));
2168 * Versions up to Samba 4.5.x had a spelling bug in the
2169 * fruit:resource option calling lp_parm_enum with
2170 * "res*s*ource" (ie two s).
2172 * In Samba 4.6 we accept both the wrong and the correct
2173 * spelling, in Samba 4.7 the bad spelling will be removed.
2175 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2176 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
2177 if (enumval == -1) {
2178 DEBUG(1, ("value for %s: resource type unknown\n",
2179 FRUIT_PARAM_TYPE_NAME));
2182 config->rsrc = (enum fruit_rsrc)enumval;
2184 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2185 "resource", fruit_rsrc, enumval);
2186 if (enumval == -1) {
2187 DEBUG(1, ("value for %s: resource type unknown\n",
2188 FRUIT_PARAM_TYPE_NAME));
2191 config->rsrc = (enum fruit_rsrc)enumval;
2193 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2194 "metadata", fruit_meta, FRUIT_META_NETATALK);
2195 if (enumval == -1) {
2196 DEBUG(1, ("value for %s: metadata type unknown\n",
2197 FRUIT_PARAM_TYPE_NAME));
2200 config->meta = (enum fruit_meta)enumval;
2202 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2203 "locking", fruit_locking, FRUIT_LOCKING_NONE);
2204 if (enumval == -1) {
2205 DEBUG(1, ("value for %s: locking type unknown\n",
2206 FRUIT_PARAM_TYPE_NAME));
2209 config->locking = (enum fruit_locking)enumval;
2211 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2212 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
2213 if (enumval == -1) {
2214 DEBUG(1, ("value for %s: encoding type unknown\n",
2215 FRUIT_PARAM_TYPE_NAME));
2218 config->encoding = (enum fruit_encoding)enumval;
2220 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2221 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
2222 FRUIT_PARAM_TYPE_NAME,
2227 config->use_aapl = lp_parm_bool(
2228 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
2230 config->time_machine = lp_parm_bool(
2231 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
2233 config->unix_info_enabled = lp_parm_bool(
2234 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
2236 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
2239 config->posix_rename = lp_parm_bool(
2240 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
2242 config->aapl_zero_file_id =
2243 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
2245 config->readdir_attr_rsize = lp_parm_bool(
2246 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
2248 config->readdir_attr_finder_info = lp_parm_bool(
2249 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
2251 config->readdir_attr_max_access = lp_parm_bool(
2252 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
2254 config->model = lp_parm_const_string(
2255 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
2257 tm_size_str = lp_parm_const_string(
2258 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2259 "time machine max size", NULL);
2260 if (tm_size_str != NULL) {
2261 config->time_machine_max_size = conv_str_size(tm_size_str);
2264 config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
2265 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2266 "wipe_intentionally_left_blank_rfork", false);
2268 config->delete_empty_adfiles = lp_parm_bool(
2269 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2270 "delete_empty_adfiles", false);
2272 SMB_VFS_HANDLE_SET_DATA(handle, config,
2273 NULL, struct fruit_config_data,
2280 * Prepend "._" to a basename
2281 * Return a new struct smb_filename with stream_name == NULL.
2283 static int adouble_path(TALLOC_CTX *ctx,
2284 const struct smb_filename *smb_fname_in,
2285 struct smb_filename **pp_smb_fname_out)
2289 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2292 if (smb_fname == NULL) {
2296 /* We need streamname to be NULL */
2297 TALLOC_FREE(smb_fname->stream_name);
2299 /* And we're replacing base_name. */
2300 TALLOC_FREE(smb_fname->base_name);
2302 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2304 TALLOC_FREE(smb_fname);
2308 smb_fname->base_name = talloc_asprintf(smb_fname,
2309 "%s/._%s", parent, base);
2310 if (smb_fname->base_name == NULL) {
2311 TALLOC_FREE(smb_fname);
2315 *pp_smb_fname_out = smb_fname;
2321 * Allocate and initialize an AfpInfo struct
2323 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2325 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2329 ai->afpi_Signature = AFP_Signature;
2330 ai->afpi_Version = AFP_Version;
2331 ai->afpi_BackupTime = AD_DATE_START;
2336 * Pack an AfpInfo struct into a buffer
2338 * Buffer size must be at least AFP_INFO_SIZE
2339 * Returns size of packed buffer
2341 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2343 memset(buf, 0, AFP_INFO_SIZE);
2345 RSIVAL(buf, 0, ai->afpi_Signature);
2346 RSIVAL(buf, 4, ai->afpi_Version);
2347 RSIVAL(buf, 12, ai->afpi_BackupTime);
2348 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2350 return AFP_INFO_SIZE;
2354 * Unpack a buffer into a AfpInfo structure
2356 * Buffer size must be at least AFP_INFO_SIZE
2357 * Returns allocated AfpInfo struct
2359 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2361 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2366 ai->afpi_Signature = RIVAL(data, 0);
2367 ai->afpi_Version = RIVAL(data, 4);
2368 ai->afpi_BackupTime = RIVAL(data, 12);
2369 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2370 sizeof(ai->afpi_FinderInfo));
2372 if (ai->afpi_Signature != AFP_Signature
2373 || ai->afpi_Version != AFP_Version) {
2374 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2382 * Fake an inode number from the md5 hash of the (xattr) name
2384 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2387 unsigned char hash[16];
2391 DBG_DEBUG("fruit_inode called for %ju/%ju [%s]\n",
2392 (uintmax_t)sbuf->st_ex_dev,
2393 (uintmax_t)sbuf->st_ex_ino, sname);
2395 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2396 SMB_ASSERT(upper_sname != NULL);
2399 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2400 sizeof(sbuf->st_ex_dev));
2401 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2402 sizeof(sbuf->st_ex_ino));
2403 MD5Update(&ctx, (unsigned char *)upper_sname,
2404 talloc_get_size(upper_sname)-1);
2405 MD5Final(hash, &ctx);
2407 TALLOC_FREE(upper_sname);
2409 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2410 memcpy(&result, hash, sizeof(result));
2412 DBG_DEBUG("fruit_inode \"%s\": ino=%ju\n",
2413 sname, (uintmax_t)result);
2418 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2419 struct stream_struct **streams,
2420 const char *name, off_t size,
2423 struct stream_struct *tmp;
2425 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2431 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2432 if (tmp[*num_streams].name == NULL) {
2436 tmp[*num_streams].size = size;
2437 tmp[*num_streams].alloc_size = alloc_size;
2444 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2445 struct stream_struct **streams)
2447 struct stream_struct *tmp = *streams;
2450 if (*num_streams == 0) {
2454 for (i = 0; i < *num_streams; i++) {
2455 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2460 if (i == *num_streams) {
2464 if (tmp[i].size > 0) {
2468 TALLOC_FREE(tmp[i].name);
2469 if (*num_streams - 1 > i) {
2470 memmove(&tmp[i], &tmp[i+1],
2471 (*num_streams - i - 1) * sizeof(struct stream_struct));
2478 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2479 struct stream_struct **streams,
2482 struct stream_struct *tmp = *streams;
2485 if (*num_streams == 0) {
2489 for (i = 0; i < *num_streams; i++) {
2490 if (strequal_m(tmp[i].name, name)) {
2495 if (i == *num_streams) {
2499 TALLOC_FREE(tmp[i].name);
2500 if (*num_streams - 1 > i) {
2501 memmove(&tmp[i], &tmp[i+1],
2502 (*num_streams - i - 1) * sizeof(struct stream_struct));
2509 static bool ad_empty_finderinfo(const struct adouble *ad)
2512 char emptybuf[ADEDLEN_FINDERI] = {0};
2515 fi = ad_get_entry(ad, ADEID_FINDERI);
2517 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2521 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2525 static bool ai_empty_finderinfo(const AfpInfo *ai)
2528 char emptybuf[ADEDLEN_FINDERI] = {0};
2530 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2535 * Update btime with btime from Netatalk
2537 static void update_btime(vfs_handle_struct *handle,
2538 struct smb_filename *smb_fname)
2541 struct timespec creation_time = {0};
2543 struct fruit_config_data *config = NULL;
2545 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2548 switch (config->meta) {
2549 case FRUIT_META_STREAM:
2551 case FRUIT_META_NETATALK:
2555 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2559 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2563 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2569 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2570 update_stat_ex_create_time(&smb_fname->st, creation_time);
2576 * Map an access mask to a Netatalk single byte byte range lock
2578 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2579 uint32_t access_mask)
2583 switch (access_mask) {
2584 case FILE_READ_DATA:
2585 offset = AD_FILELOCK_OPEN_RD;
2588 case FILE_WRITE_DATA:
2589 case FILE_APPEND_DATA:
2590 offset = AD_FILELOCK_OPEN_WR;
2594 offset = AD_FILELOCK_OPEN_NONE;
2598 if (fork_type == APPLE_FORK_RSRC) {
2599 if (offset == AD_FILELOCK_OPEN_NONE) {
2600 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2610 * Map a deny mode to a Netatalk brl
2612 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2617 switch (deny_mode) {
2619 offset = AD_FILELOCK_DENY_RD;
2623 offset = AD_FILELOCK_DENY_WR;
2627 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2630 if (fork_type == APPLE_FORK_RSRC) {
2638 * Call fcntl() with an exclusive F_GETLK request in order to
2639 * determine if there's an exisiting shared lock
2641 * @return true if the requested lock was found or any error occurred
2642 * false if the lock was not found
2644 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2647 off_t offset = in_offset;
2652 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2653 if (result == false) {
2657 if (type != F_UNLCK) {
2664 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2666 uint32_t access_mask,
2669 NTSTATUS status = NT_STATUS_OK;
2670 bool open_for_reading, open_for_writing, deny_read, deny_write;
2672 bool have_read = false;
2675 /* FIXME: hardcoded data fork, add resource fork */
2676 enum apple_fork fork_type = APPLE_FORK_DATA;
2678 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2680 access_mask & FILE_READ_DATA ? "READ" :"-",
2681 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2682 deny_mode & DENY_READ ? "DENY_READ" : "-",
2683 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2685 if (fsp->fh->fd == -1) {
2686 return NT_STATUS_OK;
2689 flags = fcntl(fsp->fh->fd, F_GETFL);
2691 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2692 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2693 return map_nt_error_from_unix(errno);
2696 if (flags & (O_RDONLY|O_RDWR)) {
2698 * Applying fcntl read locks requires an fd opened for
2699 * reading. This means we won't be applying locks for
2700 * files openend write-only, but what can we do...
2706 * Check read access and deny read mode
2708 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2710 open_for_reading = test_netatalk_lock(
2711 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2713 deny_read = test_netatalk_lock(
2714 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2716 DEBUG(10, ("read: %s, deny_write: %s\n",
2717 open_for_reading == true ? "yes" : "no",
2718 deny_read == true ? "yes" : "no"));
2720 if (((access_mask & FILE_READ_DATA) && deny_read)
2721 || ((deny_mode & DENY_READ) && open_for_reading)) {
2722 return NT_STATUS_SHARING_VIOLATION;
2726 if ((access_mask & FILE_READ_DATA) && have_read) {
2727 struct byte_range_lock *br_lck = NULL;
2729 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2731 handle->conn->sconn->msg_ctx, fsp,
2732 fsp->op->global->open_persistent_id, 1, off,
2733 READ_LOCK, POSIX_LOCK, false,
2736 TALLOC_FREE(br_lck);
2738 if (!NT_STATUS_IS_OK(status)) {
2743 if ((deny_mode & DENY_READ) && have_read) {
2744 struct byte_range_lock *br_lck = NULL;
2746 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2748 handle->conn->sconn->msg_ctx, fsp,
2749 fsp->op->global->open_persistent_id, 1, off,
2750 READ_LOCK, POSIX_LOCK, false,
2753 TALLOC_FREE(br_lck);
2755 if (!NT_STATUS_IS_OK(status)) {
2762 * Check write access and deny write mode
2764 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2766 open_for_writing = test_netatalk_lock(
2767 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2769 deny_write = test_netatalk_lock(
2770 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2772 DEBUG(10, ("write: %s, deny_write: %s\n",
2773 open_for_writing == true ? "yes" : "no",
2774 deny_write == true ? "yes" : "no"));
2776 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2777 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2778 return NT_STATUS_SHARING_VIOLATION;
2782 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2783 struct byte_range_lock *br_lck = NULL;
2785 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2787 handle->conn->sconn->msg_ctx, fsp,
2788 fsp->op->global->open_persistent_id, 1, off,
2789 READ_LOCK, POSIX_LOCK, false,
2792 TALLOC_FREE(br_lck);
2794 if (!NT_STATUS_IS_OK(status)) {
2798 if ((deny_mode & DENY_WRITE) && have_read) {
2799 struct byte_range_lock *br_lck = NULL;
2801 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2803 handle->conn->sconn->msg_ctx, fsp,
2804 fsp->op->global->open_persistent_id, 1, off,
2805 READ_LOCK, POSIX_LOCK, false,
2808 TALLOC_FREE(br_lck);
2810 if (!NT_STATUS_IS_OK(status)) {
2819 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2820 struct smb_request *req,
2821 const struct smb2_create_blobs *in_context_blobs,
2822 struct smb2_create_blobs *out_context_blobs)
2824 struct fruit_config_data *config;
2826 struct smb2_create_blob *aapl = NULL;
2830 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2831 uint64_t req_bitmap, client_caps;
2832 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2836 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2837 return NT_STATUS_UNSUCCESSFUL);
2839 if (!config->use_aapl
2840 || in_context_blobs == NULL
2841 || out_context_blobs == NULL) {
2842 return NT_STATUS_OK;
2845 aapl = smb2_create_blob_find(in_context_blobs,
2846 SMB2_CREATE_TAG_AAPL);
2848 return NT_STATUS_OK;
2851 if (aapl->data.length != 24) {
2852 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2853 (uintmax_t)aapl->data.length));
2854 return NT_STATUS_INVALID_PARAMETER;
2857 cmd = IVAL(aapl->data.data, 0);
2858 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2859 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2860 return NT_STATUS_INVALID_PARAMETER;
2863 req_bitmap = BVAL(aapl->data.data, 8);
2864 client_caps = BVAL(aapl->data.data, 16);
2866 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2868 SBVAL(p, 8, req_bitmap);
2869 ok = data_blob_append(req, &blob, p, 16);
2871 return NT_STATUS_UNSUCCESSFUL;
2874 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2875 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2876 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2877 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2878 config->readdir_attr_enabled = true;
2881 if (config->use_copyfile) {
2882 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2883 config->copyfile_enabled = true;
2887 * The client doesn't set the flag, so we can't check
2888 * for it and just set it unconditionally
2890 if (config->unix_info_enabled) {
2891 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2894 SBVAL(p, 0, server_caps);
2895 ok = data_blob_append(req, &blob, p, 8);
2897 return NT_STATUS_UNSUCCESSFUL;
2901 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2902 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2910 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2917 if (config->time_machine) {
2918 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2923 ok = data_blob_append(req, &blob, p, 8);
2925 return NT_STATUS_UNSUCCESSFUL;
2929 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2930 ok = convert_string_talloc(req,
2931 CH_UNIX, CH_UTF16LE,
2932 config->model, strlen(config->model),
2935 return NT_STATUS_UNSUCCESSFUL;
2939 SIVAL(p + 4, 0, modellen);
2940 ok = data_blob_append(req, &blob, p, 8);
2943 return NT_STATUS_UNSUCCESSFUL;
2946 ok = data_blob_append(req, &blob, model, modellen);
2949 return NT_STATUS_UNSUCCESSFUL;
2953 status = smb2_create_blob_add(out_context_blobs,
2955 SMB2_CREATE_TAG_AAPL,
2957 if (NT_STATUS_IS_OK(status)) {
2958 global_fruit_config.nego_aapl = true;
2959 if (config->aapl_zero_file_id) {
2960 aapl_force_zero_file_id(handle->conn->sconn);
2967 static bool readdir_attr_meta_finderi_stream(
2968 struct vfs_handle_struct *handle,
2969 const struct smb_filename *smb_fname,
2972 struct smb_filename *stream_name = NULL;
2973 files_struct *fsp = NULL;
2978 uint8_t buf[AFP_INFO_SIZE];
2980 stream_name = synthetic_smb_fname(talloc_tos(),
2981 smb_fname->base_name,
2982 AFPINFO_STREAM_NAME,
2983 NULL, smb_fname->flags);
2984 if (stream_name == NULL) {
2988 ret = SMB_VFS_STAT(handle->conn, stream_name);
2993 status = SMB_VFS_CREATE_FILE(
2994 handle->conn, /* conn */
2996 0, /* root_dir_fid */
2997 stream_name, /* fname */
2998 FILE_READ_DATA, /* access_mask */
2999 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
3001 FILE_OPEN, /* create_disposition*/
3002 0, /* create_options */
3003 0, /* file_attributes */
3004 INTERNAL_OPEN_ONLY, /* oplock_request */
3006 0, /* allocation_size */
3007 0, /* private_flags */
3012 NULL, NULL); /* create context */
3014 TALLOC_FREE(stream_name);
3016 if (!NT_STATUS_IS_OK(status)) {
3020 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
3021 if (nread != AFP_INFO_SIZE) {
3022 DBG_ERR("short read [%s] [%zd/%d]\n",
3023 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
3028 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
3035 close_file(NULL, fsp, NORMAL_CLOSE);
3041 static bool readdir_attr_meta_finderi_netatalk(
3042 struct vfs_handle_struct *handle,
3043 const struct smb_filename *smb_fname,
3046 struct adouble *ad = NULL;
3049 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
3054 p = ad_get_entry(ad, ADEID_FINDERI);
3056 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
3061 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
3066 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
3067 const struct smb_filename *smb_fname,
3068 struct readdir_attr_data *attr_data)
3070 struct fruit_config_data *config = NULL;
3071 uint32_t date_added;
3075 SMB_VFS_HANDLE_GET_DATA(handle, config,
3076 struct fruit_config_data,
3079 switch (config->meta) {
3080 case FRUIT_META_NETATALK:
3081 ok = readdir_attr_meta_finderi_netatalk(
3082 handle, smb_fname, &ai);
3085 case FRUIT_META_STREAM:
3086 ok = readdir_attr_meta_finderi_stream(
3087 handle, smb_fname, &ai);
3091 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3096 /* Don't bother with errors, it's likely ENOENT */
3100 if (S_ISREG(smb_fname->st.st_ex_mode)) {
3102 memcpy(&attr_data->attr_data.aapl.finder_info[0],
3103 &ai.afpi_FinderInfo[0], 4);
3105 /* finder_creator */
3106 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
3107 &ai.afpi_FinderInfo[4], 4);
3111 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
3112 &ai.afpi_FinderInfo[8], 2);
3114 /* finder_ext_flags */
3115 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
3116 &ai.afpi_FinderInfo[24], 2);
3119 date_added = convert_time_t_to_uint32_t(
3120 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
3122 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
3127 static uint64_t readdir_attr_rfork_size_adouble(
3128 struct vfs_handle_struct *handle,
3129 const struct smb_filename *smb_fname)
3131 struct adouble *ad = NULL;
3132 uint64_t rfork_size;
3134 ad = ad_get(talloc_tos(), handle, smb_fname,
3140 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
3146 static uint64_t readdir_attr_rfork_size_stream(
3147 struct vfs_handle_struct *handle,
3148 const struct smb_filename *smb_fname)
3150 struct smb_filename *stream_name = NULL;
3152 uint64_t rfork_size;
3154 stream_name = synthetic_smb_fname(talloc_tos(),
3155 smb_fname->base_name,
3156 AFPRESOURCE_STREAM_NAME,
3158 if (stream_name == NULL) {
3162 ret = SMB_VFS_STAT(handle->conn, stream_name);
3164 TALLOC_FREE(stream_name);
3168 rfork_size = stream_name->st.st_ex_size;
3169 TALLOC_FREE(stream_name);
3174 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
3175 const struct smb_filename *smb_fname)
3177 struct fruit_config_data *config = NULL;
3178 uint64_t rfork_size;
3180 SMB_VFS_HANDLE_GET_DATA(handle, config,
3181 struct fruit_config_data,
3184 switch (config->rsrc) {
3185 case FRUIT_RSRC_ADFILE:
3186 case FRUIT_RSRC_XATTR:
3187 rfork_size = readdir_attr_rfork_size_adouble(handle,
3191 case FRUIT_META_STREAM:
3192 rfork_size = readdir_attr_rfork_size_stream(handle,
3197 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3205 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
3206 const struct smb_filename *smb_fname,
3207 struct readdir_attr_data *attr_data)
3209 NTSTATUS status = NT_STATUS_OK;
3210 struct fruit_config_data *config = NULL;
3213 SMB_VFS_HANDLE_GET_DATA(handle, config,
3214 struct fruit_config_data,
3215 return NT_STATUS_UNSUCCESSFUL);
3218 /* Ensure we return a default value in the creation_date field */
3219 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
3222 * Resource fork length
3225 if (config->readdir_attr_rsize) {
3226 uint64_t rfork_size;
3228 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
3229 attr_data->attr_data.aapl.rfork_size = rfork_size;
3236 if (config->readdir_attr_finder_info) {
3237 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
3239 status = NT_STATUS_INTERNAL_ERROR;
3246 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
3251 if (psd->dacl == NULL) {
3252 return NT_STATUS_OK;
3255 for (i = 0; i < psd->dacl->num_aces; i++) {
3256 /* MS NFS style mode/uid/gid */
3257 int cmp = dom_sid_compare_domain(
3258 &global_sid_Unix_NFS,
3259 &psd->dacl->aces[i].trustee);
3261 /* Normal ACE entry. */
3266 * security_descriptor_dacl_del()
3267 * *must* return NT_STATUS_OK as we know
3268 * we have something to remove.
3271 status = security_descriptor_dacl_del(psd,
3272 &psd->dacl->aces[i].trustee);
3273 if (!NT_STATUS_IS_OK(status)) {
3274 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3280 * security_descriptor_dacl_del() may delete more
3281 * then one entry subsequent to this one if the
3282 * SID matches, but we only need to ensure that
3283 * we stay looking at the same element in the array.
3287 return NT_STATUS_OK;
3290 /* Search MS NFS style ACE with UNIX mode */
3291 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3293 struct security_descriptor *psd,
3298 struct fruit_config_data *config = NULL;
3302 SMB_VFS_HANDLE_GET_DATA(handle, config,
3303 struct fruit_config_data,
3304 return NT_STATUS_UNSUCCESSFUL);
3306 if (!global_fruit_config.nego_aapl) {
3307 return NT_STATUS_OK;
3309 if (psd->dacl == NULL || !config->unix_info_enabled) {
3310 return NT_STATUS_OK;
3313 for (i = 0; i < psd->dacl->num_aces; i++) {
3314 if (dom_sid_compare_domain(
3315 &global_sid_Unix_NFS_Mode,
3316 &psd->dacl->aces[i].trustee) == 0) {
3317 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3318 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3321 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3322 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3328 * Remove any incoming virtual ACE entries generated by
3329 * fruit_fget_nt_acl().
3332 return remove_virtual_nfs_aces(psd);
3335 /****************************************************************************
3337 ****************************************************************************/
3339 static int fruit_connect(vfs_handle_struct *handle,
3340 const char *service,
3344 char *list = NULL, *newlist = NULL;
3345 struct fruit_config_data *config;
3347 DEBUG(10, ("fruit_connect\n"));
3349 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3354 rc = init_fruit_config(handle);
3359 SMB_VFS_HANDLE_GET_DATA(handle, config,
3360 struct fruit_config_data, return -1);
3362 if (config->veto_appledouble) {
3363 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3366 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3367 newlist = talloc_asprintf(
3369 "%s/" ADOUBLE_NAME_PREFIX "*/",
3371 lp_do_parameter(SNUM(handle->conn),
3376 lp_do_parameter(SNUM(handle->conn),
3378 "/" ADOUBLE_NAME_PREFIX "*/");
3384 if (config->encoding == FRUIT_ENC_NATIVE) {
3385 lp_do_parameter(SNUM(handle->conn),
3390 if (config->time_machine) {
3391 DBG_NOTICE("Enabling durable handles for Time Machine "
3392 "support on [%s]\n", service);
3393 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3394 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3395 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3396 if (!lp_strict_sync(SNUM(handle->conn))) {
3397 DBG_WARNING("Time Machine without strict sync is not "
3400 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3406 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3407 struct smb_filename *smb_fname,
3413 char afpinfo_buf[AFP_INFO_SIZE];
3414 ssize_t len, written;
3418 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3423 if (!(flags & (O_CREAT | O_TRUNC))) {
3427 ai = afpinfo_new(talloc_tos());
3433 len = afpinfo_pack(ai, afpinfo_buf);
3434 if (len != AFP_INFO_SIZE) {
3439 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3440 fsp->fh->fd = hostfd;
3442 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3445 if (written != AFP_INFO_SIZE) {
3446 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3454 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3457 int saved_errno = errno;
3459 fsp->fh->fd = hostfd;
3460 SMB_VFS_NEXT_CLOSE(handle, fsp);
3463 errno = saved_errno;
3468 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3469 struct smb_filename *smb_fname,
3476 struct adouble *ad = NULL;
3479 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3482 * Return a valid fd, but ensure any attempt to use it returns an error
3483 * (EPIPE). All operations on the smb_fname or the fsp will use path
3493 if (flags & (O_CREAT | O_TRUNC)) {
3495 * The attribute does not exist or needs to be truncated,
3496 * create an AppleDouble EA
3498 ad = ad_init(fsp, handle, ADOUBLE_META);
3504 rc = ad_set(ad, fsp->fsp_name);
3514 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3516 int saved_errno = errno;
3521 errno = saved_errno;
3526 static int fruit_open_meta(vfs_handle_struct *handle,
3527 struct smb_filename *smb_fname,
3528 files_struct *fsp, int flags, mode_t mode)
3531 struct fruit_config_data *config = NULL;
3532 struct fio *fio = NULL;
3534 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3536 SMB_VFS_HANDLE_GET_DATA(handle, config,
3537 struct fruit_config_data, return -1);
3539 switch (config->meta) {
3540 case FRUIT_META_STREAM:
3541 fd = fruit_open_meta_stream(handle, smb_fname,
3545 case FRUIT_META_NETATALK:
3546 fd = fruit_open_meta_netatalk(handle, smb_fname,
3551 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3555 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3561 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3562 fio->type = ADOUBLE_META;
3563 fio->config = config;
3568 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3569 struct smb_filename *smb_fname,
3575 struct adouble *ad = NULL;
3576 struct smb_filename *smb_fname_base = NULL;
3577 struct fruit_config_data *config = NULL;
3580 SMB_VFS_HANDLE_GET_DATA(handle, config,
3581 struct fruit_config_data, return -1);
3583 if ((!(flags & O_CREAT)) &&
3584 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3586 /* sorry, but directories don't habe a resource fork */
3591 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3596 /* We always need read/write access for the metadata header too */
3597 flags &= ~(O_RDONLY | O_WRONLY);
3600 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3607 if (flags & (O_CREAT | O_TRUNC)) {
3608 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3614 fsp->fh->fd = hostfd;
3616 rc = ad_fset(ad, fsp);
3627 TALLOC_FREE(smb_fname_base);
3629 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3631 int saved_errno = errno;
3634 * BUGBUGBUG -- we would need to call
3635 * fd_close_posix here, but we don't have a
3638 fsp->fh->fd = hostfd;
3642 errno = saved_errno;
3647 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3648 struct smb_filename *smb_fname,
3653 #ifdef HAVE_ATTROPEN
3656 fd = attropen(smb_fname->base_name,
3657 AFPRESOURCE_EA_NETATALK,
3672 static int fruit_open_rsrc(vfs_handle_struct *handle,
3673 struct smb_filename *smb_fname,
3674 files_struct *fsp, int flags, mode_t mode)
3677 struct fruit_config_data *config = NULL;
3678 struct fio *fio = NULL;
3680 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3682 SMB_VFS_HANDLE_GET_DATA(handle, config,
3683 struct fruit_config_data, return -1);
3685 switch (config->rsrc) {
3686 case FRUIT_RSRC_STREAM:
3687 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3690 case FRUIT_RSRC_ADFILE:
3691 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3695 case FRUIT_RSRC_XATTR:
3696 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3701 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3705 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3711 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3712 fio->type = ADOUBLE_RSRC;
3713 fio->config = config;
3718 static int fruit_open(vfs_handle_struct *handle,
3719 struct smb_filename *smb_fname,
3720 files_struct *fsp, int flags, mode_t mode)
3724 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3726 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3727 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3730 if (is_afpinfo_stream(smb_fname)) {
3731 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3732 } else if (is_afpresource_stream(smb_fname)) {
3733 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3735 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3738 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3743 static int fruit_rename(struct vfs_handle_struct *handle,
3744 const struct smb_filename *smb_fname_src,
3745 const struct smb_filename *smb_fname_dst)
3748 struct fruit_config_data *config = NULL;
3749 struct smb_filename *src_adp_smb_fname = NULL;
3750 struct smb_filename *dst_adp_smb_fname = NULL;
3752 SMB_VFS_HANDLE_GET_DATA(handle, config,
3753 struct fruit_config_data, return -1);
3755 if (!VALID_STAT(smb_fname_src->st)) {
3756 DBG_ERR("Need valid stat for [%s]\n",
3757 smb_fname_str_dbg(smb_fname_src));
3761 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3766 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3767 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3772 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3777 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3782 DBG_DEBUG("%s -> %s\n",
3783 smb_fname_str_dbg(src_adp_smb_fname),
3784 smb_fname_str_dbg(dst_adp_smb_fname));
3786 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3787 if (errno == ENOENT) {
3792 TALLOC_FREE(src_adp_smb_fname);
3793 TALLOC_FREE(dst_adp_smb_fname);
3797 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3798 const struct smb_filename *smb_fname)
3800 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3803 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3804 const struct smb_filename *smb_fname)
3806 return SMB_VFS_REMOVEXATTR(handle->conn,
3808 AFPINFO_EA_NETATALK);
3811 static int fruit_unlink_meta(vfs_handle_struct *handle,
3812 const struct smb_filename *smb_fname)
3814 struct fruit_config_data *config = NULL;
3817 SMB_VFS_HANDLE_GET_DATA(handle, config,
3818 struct fruit_config_data, return -1);
3820 switch (config->meta) {
3821 case FRUIT_META_STREAM:
3822 rc = fruit_unlink_meta_stream(handle, smb_fname);
3825 case FRUIT_META_NETATALK:
3826 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3830 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3837 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3838 const struct smb_filename *smb_fname,
3843 if (!force_unlink) {
3844 struct smb_filename *smb_fname_cp = NULL;
3847 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3848 if (smb_fname_cp == NULL) {
3853 * 0 byte resource fork streams are not listed by
3854 * vfs_streaminfo, as a result stream cleanup/deletion of file
3855 * deletion doesn't remove the resourcefork stream.
3858 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3860 TALLOC_FREE(smb_fname_cp);
3861 DBG_ERR("stat [%s] failed [%s]\n",
3862 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3866 size = smb_fname_cp->st.st_ex_size;
3867 TALLOC_FREE(smb_fname_cp);
3870 /* OS X ignores resource fork stream delete requests */
3875 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3876 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3883 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3884 const struct smb_filename *smb_fname,
3888 struct adouble *ad = NULL;
3889 struct smb_filename *adp_smb_fname = NULL;
3891 if (!force_unlink) {
3892 ad = ad_get(talloc_tos(), handle, smb_fname,
3901 * 0 byte resource fork streams are not listed by
3902 * vfs_streaminfo, as a result stream cleanup/deletion of file
3903 * deletion doesn't remove the resourcefork stream.
3906 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3907 /* OS X ignores resource fork stream delete requests */
3915 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3920 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3921 TALLOC_FREE(adp_smb_fname);
3922 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3929 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3930 const struct smb_filename *smb_fname,
3934 * OS X ignores resource fork stream delete requests, so nothing to do
3935 * here. Removing the file will remove the xattr anyway, so we don't
3936 * have to take care of removing 0 byte resource forks that could be
3942 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3943 const struct smb_filename *smb_fname,
3946 struct fruit_config_data *config = NULL;
3949 SMB_VFS_HANDLE_GET_DATA(handle, config,
3950 struct fruit_config_data, return -1);
3952 switch (config->rsrc) {
3953 case FRUIT_RSRC_STREAM:
3954 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3957 case FRUIT_RSRC_ADFILE:
3958 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3961 case FRUIT_RSRC_XATTR:
3962 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3966 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3973 static int fruit_unlink(vfs_handle_struct *handle,
3974 const struct smb_filename *smb_fname)
3977 struct fruit_config_data *config = NULL;
3978 struct smb_filename *rsrc_smb_fname = NULL;
3980 SMB_VFS_HANDLE_GET_DATA(handle, config,
3981 struct fruit_config_data, return -1);
3983 if (is_afpinfo_stream(smb_fname)) {
3984 return fruit_unlink_meta(handle, smb_fname);
3985 } else if (is_afpresource_stream(smb_fname)) {
3986 return fruit_unlink_rsrc(handle, smb_fname, false);
3987 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3988 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3992 * A request to delete the base file. Because 0 byte resource
3993 * fork streams are not listed by fruit_streaminfo,
3994 * delete_all_streams() can't remove 0 byte resource fork
3995 * streams, so we have to cleanup this here.
3997 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3998 smb_fname->base_name,
3999 AFPRESOURCE_STREAM_NAME,
4002 if (rsrc_smb_fname == NULL) {
4006 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
4007 if ((rc != 0) && (errno != ENOENT)) {
4008 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
4009 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
4010 TALLOC_FREE(rsrc_smb_fname);
4013 TALLOC_FREE(rsrc_smb_fname);
4015 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
4018 static int fruit_chmod(vfs_handle_struct *handle,
4019 const struct smb_filename *smb_fname,
4023 struct fruit_config_data *config = NULL;
4024 struct smb_filename *smb_fname_adp = NULL;
4026 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
4031 SMB_VFS_HANDLE_GET_DATA(handle, config,
4032 struct fruit_config_data, return -1);
4034 if (config->rsrc != FRUIT_RSRC_ADFILE) {
4038 if (!VALID_STAT(smb_fname->st)) {
4042 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
4046 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
4051 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
4053 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
4054 if (errno == ENOENT) {
4058 TALLOC_FREE(smb_fname_adp);
4062 static int fruit_chown(vfs_handle_struct *handle,
4063 const struct smb_filename *smb_fname,
4068 struct fruit_config_data *config = NULL;
4069 struct smb_filename *adp_smb_fname = NULL;
4071 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
4076 SMB_VFS_HANDLE_GET_DATA(handle, config,
4077 struct fruit_config_data, return -1);
4079 if (config->rsrc != FRUIT_RSRC_ADFILE) {
4083 if (!VALID_STAT(smb_fname->st)) {
4087 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
4091 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
4096 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
4098 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
4099 if (errno == ENOENT) {
4104 TALLOC_FREE(adp_smb_fname);
4108 static int fruit_rmdir(struct vfs_handle_struct *handle,
4109 const struct smb_filename *smb_fname)
4113 struct fruit_config_data *config;
4115 SMB_VFS_HANDLE_GET_DATA(handle, config,
4116 struct fruit_config_data, return -1);
4118 if (config->rsrc != FRUIT_RSRC_ADFILE) {
4123 * Due to there is no way to change bDeleteVetoFiles variable
4124 * from this module, need to clean up ourselves
4127 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
4132 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
4134 struct adouble *ad = NULL;
4136 struct smb_filename *ad_smb_fname = NULL;
4139 match = strncmp(de->d_name,
4140 ADOUBLE_NAME_PREFIX,
4141 strlen(ADOUBLE_NAME_PREFIX));
4146 p = talloc_asprintf(talloc_tos(), "%s/%s",
4147 smb_fname->base_name, de->d_name);
4149 DBG_ERR("talloc_asprintf failed\n");
4153 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
4157 if (ad_smb_fname == NULL) {
4158 DBG_ERR("synthetic_smb_fname failed\n");
4163 * Check whether it's a valid AppleDouble file, if
4164 * yes, delete it, ignore it otherwise.
4166 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
4168 TALLOC_FREE(ad_smb_fname);
4174 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
4176 DBG_ERR("Deleting [%s] failed\n",
4177 smb_fname_str_dbg(ad_smb_fname));
4179 TALLOC_FREE(ad_smb_fname);
4184 SMB_VFS_CLOSEDIR(handle->conn, dh);
4186 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
4189 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
4190 files_struct *fsp, void *data,
4191 size_t n, off_t offset)
4196 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4197 if (nread == -1 || nread == n) {
4201 DBG_ERR("Removing [%s] after short read [%zd]\n",
4202 fsp_str_dbg(fsp), nread);
4204 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4206 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
4214 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
4215 files_struct *fsp, void *data,
4216 size_t n, off_t offset)
4219 struct adouble *ad = NULL;
4220 char afpinfo_buf[AFP_INFO_SIZE];
4224 ai = afpinfo_new(talloc_tos());
4229 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4235 p = ad_get_entry(ad, ADEID_FINDERI);
4237 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4242 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
4244 nread = afpinfo_pack(ai, afpinfo_buf);
4245 if (nread != AFP_INFO_SIZE) {
4250 memcpy(data, afpinfo_buf, n);
4258 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
4259 files_struct *fsp, void *data,
4260 size_t n, off_t offset)
4262 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4267 * OS X has a off-by-1 error in the offset calculation, so we're
4268 * bug compatible here. It won't hurt, as any relevant real
4269 * world read requests from the AFP_AfpInfo stream will be
4270 * offset=0 n=60. offset is ignored anyway, see below.
4272 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4277 DBG_ERR("Failed to fetch fsp extension");
4281 /* Yes, macOS always reads from offset 0 */
4283 to_return = MIN(n, AFP_INFO_SIZE);
4285 switch (fio->config->meta) {
4286 case FRUIT_META_STREAM:
4287 nread = fruit_pread_meta_stream(handle, fsp, data,
4291 case FRUIT_META_NETATALK:
4292 nread = fruit_pread_meta_adouble(handle, fsp, data,
4297 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4301 if (nread == -1 && fio->created) {
4303 char afpinfo_buf[AFP_INFO_SIZE];
4305 ai = afpinfo_new(talloc_tos());
4310 nread = afpinfo_pack(ai, afpinfo_buf);
4312 if (nread != AFP_INFO_SIZE) {
4316 memcpy(data, afpinfo_buf, to_return);
4323 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4324 files_struct *fsp, void *data,
4325 size_t n, off_t offset)
4327 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4330 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4331 files_struct *fsp, void *data,
4332 size_t n, off_t offset)
4334 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4337 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4338 files_struct *fsp, void *data,
4339 size_t n, off_t offset)
4341 struct adouble *ad = NULL;
4344 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4349 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4350 offset + ad_getentryoff(ad, ADEID_RFORK));
4356 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4357 files_struct *fsp, void *data,
4358 size_t n, off_t offset)
4360 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4368 switch (fio->config->rsrc) {
4369 case FRUIT_RSRC_STREAM:
4370 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4373 case FRUIT_RSRC_ADFILE:
4374 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4377 case FRUIT_RSRC_XATTR:
4378 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4382 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4389 static ssize_t fruit_pread(vfs_handle_struct *handle,
4390 files_struct *fsp, void *data,
4391 size_t n, off_t offset)
4393 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4396 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4397 fsp_str_dbg(fsp), (intmax_t)offset, n);
4400 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4403 if (fio->type == ADOUBLE_META) {
4404 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4406 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4409 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4413 static bool fruit_must_handle_aio_stream(struct fio *fio)
4419 if (fio->type == ADOUBLE_META) {
4423 if ((fio->type == ADOUBLE_RSRC) &&
4424 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4432 struct fruit_pread_state {
4434 struct vfs_aio_state vfs_aio_state;
4437 static void fruit_pread_done(struct tevent_req *subreq);
4439 static struct tevent_req *fruit_pread_send(
4440 struct vfs_handle_struct *handle,
4441 TALLOC_CTX *mem_ctx,
4442 struct tevent_context *ev,
4443 struct files_struct *fsp,
4445 size_t n, off_t offset)
4447 struct tevent_req *req = NULL;
4448 struct tevent_req *subreq = NULL;
4449 struct fruit_pread_state *state = NULL;
4450 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4452 req = tevent_req_create(mem_ctx, &state,
4453 struct fruit_pread_state);
4458 if (fruit_must_handle_aio_stream(fio)) {
4459 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4460 if (state->nread != n) {
4461 if (state->nread != -1) {
4464 tevent_req_error(req, errno);
4465 return tevent_req_post(req, ev);
4467 tevent_req_done(req);
4468 return tevent_req_post(req, ev);
4471 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4473 if (tevent_req_nomem(req, subreq)) {
4474 return tevent_req_post(req, ev);
4476 tevent_req_set_callback(subreq, fruit_pread_done, req);
4480 static void fruit_pread_done(struct tevent_req *subreq)
4482 struct tevent_req *req = tevent_req_callback_data(
4483 subreq, struct tevent_req);
4484 struct fruit_pread_state *state = tevent_req_data(
4485 req, struct fruit_pread_state);
4487 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4488 TALLOC_FREE(subreq);
4490 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4493 tevent_req_done(req);
4496 static ssize_t fruit_pread_recv(struct tevent_req *req,
4497 struct vfs_aio_state *vfs_aio_state)
4499 struct fruit_pread_state *state = tevent_req_data(
4500 req, struct fruit_pread_state);
4502 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4506 *vfs_aio_state = state->vfs_aio_state;
4507 return state->nread;
4510 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4511 files_struct *fsp, const void *data,
4512 size_t n, off_t offset)
4514 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4520 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4521 fsp_str_dbg(fsp), (intmax_t)offset, n);
4530 ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
4532 DBG_ERR("Close [%s] failed: %s\n",
4533 fsp_str_dbg(fsp), strerror(errno));
4538 fd = SMB_VFS_NEXT_OPEN(handle,
4544 DBG_ERR("On-demand create [%s] in write failed: %s\n",
4545 fsp_str_dbg(fsp), strerror(errno));
4549 fio->fake_fd = false;
4552 ai = afpinfo_unpack(talloc_tos(), data);
4557 if (ai_empty_finderinfo(ai)) {
4558 ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
4560 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
4565 ok = set_delete_on_close(
4568 handle->conn->session_info->security_token,
4569 handle->conn->session_info->unix_token);
4571 DBG_ERR("set_delete_on_close on [%s] failed\n",
4578 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4579 if (nwritten != n) {
4586 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4587 files_struct *fsp, const void *data,
4588 size_t n, off_t offset)
4590 struct adouble *ad = NULL;
4596 ai = afpinfo_unpack(talloc_tos(), data);
4601 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4603 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4608 p = ad_get_entry(ad, ADEID_FINDERI);
4610 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4615 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4617 ret = ad_fset(ad, fsp);
4619 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4626 if (!ai_empty_finderinfo(ai)) {
4630 ok = set_delete_on_close(
4633 handle->conn->session_info->security_token,
4634 handle->conn->session_info->unix_token);
4636 DBG_ERR("set_delete_on_close on [%s] failed\n",
4644 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4645 files_struct *fsp, const void *data,
4646 size_t n, off_t offset)
4648 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4652 * Writing an all 0 blob to the metadata stream
4653 * results in the stream being removed on a macOS
4654 * server. This ensures we behave the same and it
4655 * verified by the "delete AFP_AfpInfo by writing all
4658 if (n != AFP_INFO_SIZE || offset != 0) {
4659 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4660 (intmax_t)offset, (intmax_t)n);
4665 DBG_ERR("Failed to fetch fsp extension");
4669 switch (fio->config->meta) {
4670 case FRUIT_META_STREAM:
4671 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4675 case FRUIT_META_NETATALK:
4676 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4681 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4688 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4689 files_struct *fsp, const void *data,
4690 size_t n, off_t offset)
4692 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4695 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4696 files_struct *fsp, const void *data,
4697 size_t n, off_t offset)
4699 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4702 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4703 files_struct *fsp, const void *data,
4704 size_t n, off_t offset)
4706 struct adouble *ad = NULL;
4710 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4712 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4716 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4717 offset + ad_getentryoff(ad, ADEID_RFORK));
4718 if (nwritten != n) {
4719 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4720 fsp_str_dbg(fsp), nwritten, n);
4725 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4726 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4727 ret = ad_fset(ad, fsp);
4729 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4739 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4740 files_struct *fsp, const void *data,
4741 size_t n, off_t offset)
4743 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4747 DBG_ERR("Failed to fetch fsp extension");
4751 switch (fio->config->rsrc) {
4752 case FRUIT_RSRC_STREAM:
4753 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4756 case FRUIT_RSRC_ADFILE:
4757 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4760 case FRUIT_RSRC_XATTR:
4761 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4765 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4772 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4773 files_struct *fsp, const void *data,
4774 size_t n, off_t offset)
4776 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4779 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4780 fsp_str_dbg(fsp), (intmax_t)offset, n);
4783 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4786 if (fio->type == ADOUBLE_META) {
4787 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4789 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4792 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4796 struct fruit_pwrite_state {
4798 struct vfs_aio_state vfs_aio_state;
4801 static void fruit_pwrite_done(struct tevent_req *subreq);
4803 static struct tevent_req *fruit_pwrite_send(
4804 struct vfs_handle_struct *handle,
4805 TALLOC_CTX *mem_ctx,
4806 struct tevent_context *ev,
4807 struct files_struct *fsp,
4809 size_t n, off_t offset)
4811 struct tevent_req *req = NULL;
4812 struct tevent_req *subreq = NULL;
4813 struct fruit_pwrite_state *state = NULL;
4814 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4816 req = tevent_req_create(mem_ctx, &state,
4817 struct fruit_pwrite_state);
4822 if (fruit_must_handle_aio_stream(fio)) {
4823 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4824 if (state->nwritten != n) {
4825 if (state->nwritten != -1) {
4828 tevent_req_error(req, errno);
4829 return tevent_req_post(req, ev);
4831 tevent_req_done(req);
4832 return tevent_req_post(req, ev);
4835 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4837 if (tevent_req_nomem(req, subreq)) {
4838 return tevent_req_post(req, ev);
4840 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4844 static void fruit_pwrite_done(struct tevent_req *subreq)
4846 struct tevent_req *req = tevent_req_callback_data(
4847 subreq, struct tevent_req);
4848 struct fruit_pwrite_state *state = tevent_req_data(
4849 req, struct fruit_pwrite_state);
4851 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4852 TALLOC_FREE(subreq);
4854 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4857 tevent_req_done(req);
4860 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4861 struct vfs_aio_state *vfs_aio_state)
4863 struct fruit_pwrite_state *state = tevent_req_data(
4864 req, struct fruit_pwrite_state);
4866 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4870 *vfs_aio_state = state->vfs_aio_state;
4871 return state->nwritten;
4875 * Helper to stat/lstat the base file of an smb_fname.
4877 static int fruit_stat_base(vfs_handle_struct *handle,
4878 struct smb_filename *smb_fname,
4881 char *tmp_stream_name;
4884 tmp_stream_name = smb_fname->stream_name;
4885 smb_fname->stream_name = NULL;
4887 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4889 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4891 smb_fname->stream_name = tmp_stream_name;
4893 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
4894 smb_fname->base_name,
4895 (uintmax_t)smb_fname->st.st_ex_dev,
4896 (uintmax_t)smb_fname->st.st_ex_ino);
4900 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4901 struct smb_filename *smb_fname,
4907 ret = fruit_stat_base(handle, smb_fname, false);
4912 ino = fruit_inode(&smb_fname->st, smb_fname->stream_name);
4915 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4917 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4920 smb_fname->st.st_ex_ino = ino;
4925 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4926 struct smb_filename *smb_fname,
4929 struct adouble *ad = NULL;
4931 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4933 DBG_INFO("fruit_stat_meta %s: %s\n",
4934 smb_fname_str_dbg(smb_fname), strerror(errno));
4940 /* Populate the stat struct with info from the base file. */
4941 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4944 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4945 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4946 smb_fname->stream_name);
4950 static int fruit_stat_meta(vfs_handle_struct *handle,
4951 struct smb_filename *smb_fname,
4954 struct fruit_config_data *config = NULL;
4957 SMB_VFS_HANDLE_GET_DATA(handle, config,
4958 struct fruit_config_data, return -1);
4960 switch (config->meta) {
4961 case FRUIT_META_STREAM:
4962 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4965 case FRUIT_META_NETATALK:
4966 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4970 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4977 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4978 struct smb_filename *smb_fname,
4981 struct adouble *ad = NULL;
4984 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4990 /* Populate the stat struct with info from the base file. */
4991 ret = fruit_stat_base(handle, smb_fname, follow_links);
4997 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4998 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4999 smb_fname->stream_name);
5004 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
5005 struct smb_filename *smb_fname,
5011 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
5013 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5019 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
5020 struct smb_filename *smb_fname,
5023 #ifdef HAVE_ATTROPEN
5027 /* Populate the stat struct with info from the base file. */
5028 ret = fruit_stat_base(handle, smb_fname, follow_links);
5033 fd = attropen(smb_fname->base_name,
5034 AFPRESOURCE_EA_NETATALK,
5040 ret = sys_fstat(fd, &smb_fname->st, false);
5043 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
5044 AFPRESOURCE_EA_NETATALK);
5050 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
5051 smb_fname->stream_name);
5061 static int fruit_stat_rsrc(vfs_handle_struct *handle,
5062 struct smb_filename *smb_fname,
5065 struct fruit_config_data *config = NULL;
5068 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5070 SMB_VFS_HANDLE_GET_DATA(handle, config,
5071 struct fruit_config_data, return -1);
5073 switch (config->rsrc) {
5074 case FRUIT_RSRC_STREAM:
5075 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
5078 case FRUIT_RSRC_XATTR:
5079 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
5082 case FRUIT_RSRC_ADFILE:
5083 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
5087 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
5094 static int fruit_stat(vfs_handle_struct *handle,
5095 struct smb_filename *smb_fname)
5099 DEBUG(10, ("fruit_stat called for %s\n",
5100 smb_fname_str_dbg(smb_fname)));
5102 if (!is_ntfs_stream_smb_fname(smb_fname)
5103 || is_ntfs_default_stream_smb_fname(smb_fname)) {
5104 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
5106 update_btime(handle, smb_fname);
5112 * Note if lp_posix_paths() is true, we can never
5113 * get here as is_ntfs_stream_smb_fname() is
5114 * always false. So we never need worry about
5115 * not following links here.
5118 if (is_afpinfo_stream(smb_fname)) {
5119 rc = fruit_stat_meta(handle, smb_fname, true);
5120 } else if (is_afpresource_stream(smb_fname)) {
5121 rc = fruit_stat_rsrc(handle, smb_fname, true);
5123 return SMB_VFS_NEXT_STAT(handle, smb_fname);
5127 update_btime(handle, smb_fname);
5128 smb_fname->st.st_ex_mode &= ~S_IFMT;
5129 smb_fname->st.st_ex_mode |= S_IFREG;
5130 smb_fname->st.st_ex_blocks =
5131 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
5136 static int fruit_lstat(vfs_handle_struct *handle,
5137 struct smb_filename *smb_fname)
5141 DEBUG(10, ("fruit_lstat called for %s\n",
5142 smb_fname_str_dbg(smb_fname)));
5144 if (!is_ntfs_stream_smb_fname(smb_fname)
5145 || is_ntfs_default_stream_smb_fname(smb_fname)) {
5146 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5148 update_btime(handle, smb_fname);
5153 if (is_afpinfo_stream(smb_fname)) {
5154 rc = fruit_stat_meta(handle, smb_fname, false);
5155 } else if (is_afpresource_stream(smb_fname)) {
5156 rc = fruit_stat_rsrc(handle, smb_fname, false);
5158 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
5162 update_btime(handle, smb_fname);
5163 smb_fname->st.st_ex_mode &= ~S_IFMT;
5164 smb_fname->st.st_ex_mode |= S_IFREG;
5165 smb_fname->st.st_ex_blocks =
5166 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
5171 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
5173 SMB_STRUCT_STAT *sbuf)
5175 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5184 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
5189 *sbuf = fsp->base_fsp->fsp_name->st;
5190 sbuf->st_ex_size = AFP_INFO_SIZE;
5191 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
5195 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
5199 *sbuf = fsp->base_fsp->fsp_name->st;
5201 ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
5203 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5208 sbuf->st_ex_ino = ino;
5212 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
5214 SMB_STRUCT_STAT *sbuf)
5218 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
5223 *sbuf = fsp->base_fsp->fsp_name->st;
5224 sbuf->st_ex_size = AFP_INFO_SIZE;
5225 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
5230 static int fruit_fstat_meta(vfs_handle_struct *handle,
5232 SMB_STRUCT_STAT *sbuf,
5237 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
5239 switch (fio->config->meta) {
5240 case FRUIT_META_STREAM:
5241 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
5244 case FRUIT_META_NETATALK:
5245 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
5249 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
5253 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
5257 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
5259 SMB_STRUCT_STAT *sbuf)
5261 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5264 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
5266 SMB_STRUCT_STAT *sbuf)
5268 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5271 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
5273 SMB_STRUCT_STAT *sbuf)
5275 struct adouble *ad = NULL;
5278 /* Populate the stat struct with info from the base file. */
5279 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
5284 ad = ad_get(talloc_tos(), handle,
5285 fsp->base_fsp->fsp_name,
5288 DBG_ERR("ad_get [%s] failed [%s]\n",
5289 fsp_str_dbg(fsp), strerror(errno));
5293 *sbuf = fsp->base_fsp->fsp_name->st;
5294 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
5295 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
5301 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
5302 SMB_STRUCT_STAT *sbuf, struct fio *fio)
5306 switch (fio->config->rsrc) {
5307 case FRUIT_RSRC_STREAM:
5308 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
5311 case FRUIT_RSRC_ADFILE:
5312 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
5315 case FRUIT_RSRC_XATTR:
5316 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
5320 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5327 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
5328 SMB_STRUCT_STAT *sbuf)
5330 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5334 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5337 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
5339 if (fio->type == ADOUBLE_META) {
5340 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
5342 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
5346 sbuf->st_ex_mode &= ~S_IFMT;
5347 sbuf->st_ex_mode |= S_IFREG;
5348 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
5351 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
5352 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
5356 static NTSTATUS delete_invalid_meta_stream(
5357 vfs_handle_struct *handle,
5358 const struct smb_filename *smb_fname,
5359 TALLOC_CTX *mem_ctx,
5360 unsigned int *pnum_streams,
5361 struct stream_struct **pstreams,
5364 struct smb_filename *sname = NULL;
5368 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
5370 return NT_STATUS_INTERNAL_ERROR;
5374 return NT_STATUS_OK;
5377 sname = synthetic_smb_fname(talloc_tos(),
5378 smb_fname->base_name,
5379 AFPINFO_STREAM_NAME,
5381 if (sname == NULL) {
5382 return NT_STATUS_NO_MEMORY;
5385 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5388 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5389 return map_nt_error_from_unix(errno);
5392 return NT_STATUS_OK;
5395 static NTSTATUS fruit_streaminfo_meta_stream(
5396 vfs_handle_struct *handle,
5397 struct files_struct *fsp,
5398 const struct smb_filename *smb_fname,
5399 TALLOC_CTX *mem_ctx,
5400 unsigned int *pnum_streams,
5401 struct stream_struct **pstreams)
5403 struct stream_struct *stream = *pstreams;
5404 unsigned int num_streams = *pnum_streams;
5405 struct smb_filename *sname = NULL;
5406 char *full_name = NULL;
5408 struct share_mode_lock *lck = NULL;
5409 struct file_id id = {0};
5410 bool delete_on_close_set;
5416 for (i = 0; i < num_streams; i++) {
5417 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5422 if (i == num_streams) {
5423 return NT_STATUS_OK;
5426 if (stream[i].size != AFP_INFO_SIZE) {
5427 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5428 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5430 return delete_invalid_meta_stream(handle,
5439 * Now check if there's a delete-on-close pending on the stream. If so,
5440 * hide the stream. This behaviour was verified against a macOS 10.12
5444 sname = synthetic_smb_fname(talloc_tos(),
5445 smb_fname->base_name,
5446 AFPINFO_STREAM_NAME,
5448 if (sname == NULL) {
5449 status = NT_STATUS_NO_MEMORY;
5453 ret = fruit_stat_base(handle, sname, false);
5455 status = map_nt_error_from_unix(errno);
5459 sname->st.st_ex_ino = fruit_inode(&sname->st, AFPINFO_STREAM);
5461 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5463 lck = get_existing_share_mode_lock(talloc_tos(), id);
5465 status = NT_STATUS_OK;
5469 full_name = talloc_asprintf(talloc_tos(),
5473 if (full_name == NULL) {
5474 status = NT_STATUS_NO_MEMORY;
5478 status = file_name_hash(handle->conn, full_name, &name_hash);
5479 if (!NT_STATUS_IS_OK(status)) {
5483 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5484 if (delete_on_close_set) {
5485 ok = del_fruit_stream(mem_ctx,
5490 status = NT_STATUS_INTERNAL_ERROR;
5495 status = NT_STATUS_OK;
5500 TALLOC_FREE(full_name);
5504 static NTSTATUS fruit_streaminfo_meta_netatalk(
5505 vfs_handle_struct *handle,
5506 struct files_struct *fsp,
5507 const struct smb_filename *smb_fname,
5508 TALLOC_CTX *mem_ctx,
5509 unsigned int *pnum_streams,
5510 struct stream_struct **pstreams)
5512 struct stream_struct *stream = *pstreams;
5513 unsigned int num_streams = *pnum_streams;
5514 struct adouble *ad = NULL;
5519 /* Remove the Netatalk xattr from the list */
5520 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5521 ":" NETATALK_META_XATTR ":$DATA");
5523 return NT_STATUS_NO_MEMORY;
5527 * Check if there's a AFPINFO_STREAM from the VFS streams
5528 * backend and if yes, remove it from the list
5530 for (i = 0; i < num_streams; i++) {
5531 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5536 if (i < num_streams) {
5537 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5538 smb_fname_str_dbg(smb_fname));
5540 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5543 return NT_STATUS_INTERNAL_ERROR;
5547 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5549 return NT_STATUS_OK;
5552 is_fi_empty = ad_empty_finderinfo(ad);
5556 return NT_STATUS_OK;
5559 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5560 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5561 smb_roundup(handle->conn, AFP_INFO_SIZE));
5563 return NT_STATUS_NO_MEMORY;
5566 return NT_STATUS_OK;
5569 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5570 struct files_struct *fsp,
5571 const struct smb_filename *smb_fname,
5572 TALLOC_CTX *mem_ctx,
5573 unsigned int *pnum_streams,
5574 struct stream_struct **pstreams)
5576 struct fruit_config_data *config = NULL;
5579 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5580 return NT_STATUS_INTERNAL_ERROR);
5582 switch (config->meta) {
5583 case FRUIT_META_NETATALK:
5584 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5585 mem_ctx, pnum_streams,
5589 case FRUIT_META_STREAM:
5590 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5591 mem_ctx, pnum_streams,
5596 return NT_STATUS_INTERNAL_ERROR;
5602 static NTSTATUS fruit_streaminfo_rsrc_stream(
5603 vfs_handle_struct *handle,
5604 struct files_struct *fsp,
5605 const struct smb_filename *smb_fname,
5606 TALLOC_CTX *mem_ctx,
5607 unsigned int *pnum_streams,
5608 struct stream_struct **pstreams)
5612 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5614 DBG_ERR("Filtering resource stream failed\n");
5615 return NT_STATUS_INTERNAL_ERROR;
5617 return NT_STATUS_OK;
5620 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5621 vfs_handle_struct *handle,
5622 struct files_struct *fsp,
5623 const struct smb_filename *smb_fname,
5624 TALLOC_CTX *mem_ctx,
5625 unsigned int *pnum_streams,
5626 struct stream_struct **pstreams)
5630 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5632 DBG_ERR("Filtering resource stream failed\n");
5633 return NT_STATUS_INTERNAL_ERROR;
5635 return NT_STATUS_OK;
5638 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5639 vfs_handle_struct *handle,
5640 struct files_struct *fsp,
5641 const struct smb_filename *smb_fname,
5642 TALLOC_CTX *mem_ctx,
5643 unsigned int *pnum_streams,
5644 struct stream_struct **pstreams)
5646 struct stream_struct *stream = *pstreams;
5647 unsigned int num_streams = *pnum_streams;
5648 struct adouble *ad = NULL;
5654 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5655 * and if yes, remove it from the list
5657 for (i = 0; i < num_streams; i++) {
5658 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5663 if (i < num_streams) {
5664 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5665 smb_fname_str_dbg(smb_fname));
5667 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5668 AFPRESOURCE_STREAM);
5670 return NT_STATUS_INTERNAL_ERROR;
5674 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5676 return NT_STATUS_OK;
5679 rlen = ad_getentrylen(ad, ADEID_RFORK);
5683 return NT_STATUS_OK;
5686 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5687 AFPRESOURCE_STREAM_NAME, rlen,
5688 smb_roundup(handle->conn, rlen));
5690 return NT_STATUS_NO_MEMORY;
5693 return NT_STATUS_OK;
5696 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5697 struct files_struct *fsp,
5698 const struct smb_filename *smb_fname,
5699 TALLOC_CTX *mem_ctx,
5700 unsigned int *pnum_streams,
5701 struct stream_struct **pstreams)
5703 struct fruit_config_data *config = NULL;
5706 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5707 return NT_STATUS_INTERNAL_ERROR);
5709 switch (config->rsrc) {
5710 case FRUIT_RSRC_STREAM:
5711 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5712 mem_ctx, pnum_streams,
5716 case FRUIT_RSRC_XATTR:
5717 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5718 mem_ctx, pnum_streams,
5722 case FRUIT_RSRC_ADFILE:
5723 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5724 mem_ctx, pnum_streams,
5729 return NT_STATUS_INTERNAL_ERROR;
5735 static void fruit_filter_empty_streams(unsigned int *pnum_streams,
5736 struct stream_struct **pstreams)
5738 unsigned num_streams = *pnum_streams;
5739 struct stream_struct *streams = *pstreams;
5742 if (!global_fruit_config.nego_aapl) {
5746 while (i < num_streams) {
5747 struct smb_filename smb_fname = (struct smb_filename) {
5748 .stream_name = streams[i].name,
5751 if (is_ntfs_default_stream_smb_fname(&smb_fname)
5752 || streams[i].size > 0)
5758 streams[i] = streams[num_streams - 1];
5762 *pnum_streams = num_streams;
5765 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5766 struct files_struct *fsp,
5767 const struct smb_filename *smb_fname,
5768 TALLOC_CTX *mem_ctx,
5769 unsigned int *pnum_streams,
5770 struct stream_struct **pstreams)
5772 struct fruit_config_data *config = NULL;
5775 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5776 return NT_STATUS_UNSUCCESSFUL);
5778 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5780 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5781 pnum_streams, pstreams);
5782 if (!NT_STATUS_IS_OK(status)) {
5786 fruit_filter_empty_streams(pnum_streams, pstreams);
5788 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5789 mem_ctx, pnum_streams, pstreams);
5790 if (!NT_STATUS_IS_OK(status)) {
5794 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5795 mem_ctx, pnum_streams, pstreams);
5796 if (!NT_STATUS_IS_OK(status)) {
5800 return NT_STATUS_OK;
5803 static int fruit_ntimes(vfs_handle_struct *handle,
5804 const struct smb_filename *smb_fname,
5805 struct smb_file_time *ft)
5808 struct adouble *ad = NULL;
5809 struct fruit_config_data *config = NULL;
5811 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5814 if ((config->meta != FRUIT_META_NETATALK) ||
5815 null_timespec(ft->create_time))
5817 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5820 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5821 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5823 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5828 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5829 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5831 rc = ad_set(ad, smb_fname);
5837 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5840 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5843 static int fruit_fallocate(struct vfs_handle_struct *handle,
5844 struct files_struct *fsp,
5849 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5852 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5855 /* Let the pwrite code path handle it. */
5860 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5861 struct files_struct *fsp,
5864 #ifdef HAVE_ATTROPEN
5865 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5870 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5871 struct files_struct *fsp,
5875 struct adouble *ad = NULL;
5878 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5880 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5881 fsp_str_dbg(fsp), strerror(errno));
5885 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5887 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5893 ad_setentrylen(ad, ADEID_RFORK, offset);
5895 rc = ad_fset(ad, fsp);
5897 DBG_ERR("ad_fset [%s] failed [%s]\n",
5898 fsp_str_dbg(fsp), strerror(errno));
5907 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5908 struct files_struct *fsp,
5911 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5914 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5915 struct files_struct *fsp,
5918 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5922 DBG_ERR("Failed to fetch fsp extension");
5926 switch (fio->config->rsrc) {
5927 case FRUIT_RSRC_XATTR:
5928 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5931 case FRUIT_RSRC_ADFILE:
5932 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5935 case FRUIT_RSRC_STREAM:
5936 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5940 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5948 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5949 struct files_struct *fsp,
5953 DBG_WARNING("ftruncate %s to %jd",
5954 fsp_str_dbg(fsp), (intmax_t)offset);
5955 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5960 /* OS X returns success but does nothing */
5961 DBG_INFO("ignoring ftruncate %s to %jd\n",
5962 fsp_str_dbg(fsp), (intmax_t)offset);
5966 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5967 struct files_struct *fsp,
5970 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5973 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5977 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5980 if (fio->type == ADOUBLE_META) {
5981 ret = fruit_ftruncate_meta(handle, fsp, offset);
5983 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5986 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5990 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5991 struct smb_request *req,
5992 uint16_t root_dir_fid,
5993 struct smb_filename *smb_fname,
5994 uint32_t access_mask,
5995 uint32_t share_access,
5996 uint32_t create_disposition,
5997 uint32_t create_options,
5998 uint32_t file_attributes,
5999 uint32_t oplock_request,
6000 struct smb2_lease *lease,
6001 uint64_t allocation_size,
6002 uint32_t private_flags,
6003 struct security_descriptor *sd,
6004 struct ea_list *ea_list,
6005 files_struct **result,
6007 const struct smb2_create_blobs *in_context_blobs,
6008 struct smb2_create_blobs *out_context_blobs)
6011 struct fruit_config_data *config = NULL;
6012 files_struct *fsp = NULL;
6013 struct fio *fio = NULL;
6015 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
6016 if (!NT_STATUS_IS_OK(status)) {
6020 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
6021 return NT_STATUS_UNSUCCESSFUL);
6023 status = SMB_VFS_NEXT_CREATE_FILE(
6024 handle, req, root_dir_fid, smb_fname,
6025 access_mask, share_access,
6026 create_disposition, create_options,
6027 file_attributes, oplock_request,
6029 allocation_size, private_flags,
6030 sd, ea_list, result,
6031 pinfo, in_context_blobs, out_context_blobs);
6032 if (!NT_STATUS_IS_OK(status)) {
6038 if (global_fruit_config.nego_aapl) {
6039 if (config->posix_rename && fsp->is_directory) {
6041 * Enable POSIX directory rename behaviour
6043 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
6048 * If this is a plain open for existing files, opening an 0
6049 * byte size resource fork MUST fail with
6050 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
6052 * Cf the vfs_fruit torture tests in test_rfork_create().
6054 if (global_fruit_config.nego_aapl &&
6055 create_disposition == FILE_OPEN &&
6056 smb_fname->st.st_ex_size == 0 &&
6057 is_ntfs_stream_smb_fname(smb_fname) &&
6058 !(is_ntfs_default_stream_smb_fname(smb_fname)))
6060 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
6064 fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
6065 if (fio != NULL && pinfo != NULL && *pinfo == FILE_WAS_CREATED) {
6066 fio->created = true;
6069 if (is_ntfs_stream_smb_fname(smb_fname)
6070 || fsp->is_directory) {
6074 if (config->locking == FRUIT_LOCKING_NETATALK) {
6075 status = fruit_check_access(
6078 map_share_mode_to_deny_mode(share_access, 0));
6079 if (!NT_STATUS_IS_OK(status)) {
6087 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
6090 close_file(req, fsp, ERROR_CLOSE);
6091 *result = fsp = NULL;
6097 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
6098 const struct smb_filename *fname,
6099 TALLOC_CTX *mem_ctx,
6100 struct readdir_attr_data **pattr_data)
6102 struct fruit_config_data *config = NULL;
6103 struct readdir_attr_data *attr_data;
6106 SMB_VFS_HANDLE_GET_DATA(handle, config,
6107 struct fruit_config_data,
6108 return NT_STATUS_UNSUCCESSFUL);
6110 if (!global_fruit_config.nego_aapl) {
6111 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
6114 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
6116 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
6117 if (*pattr_data == NULL) {
6118 return NT_STATUS_UNSUCCESSFUL;
6120 attr_data = *pattr_data;
6121 attr_data->type = RDATTR_AAPL;
6124 * Mac metadata: compressed FinderInfo, resource fork length
6127 status = readdir_attr_macmeta(handle, fname, attr_data);
6128 if (!NT_STATUS_IS_OK(status)) {
6130 * Error handling is tricky: if we return failure from
6131 * this function, the corresponding directory entry
6132 * will to be passed to the client, so we really just
6133 * want to error out on fatal errors.
6135 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
6143 if (config->unix_info_enabled) {
6144 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
6150 if (!config->readdir_attr_max_access) {
6151 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
6153 status = smbd_calculate_access_mask(
6157 SEC_FLAG_MAXIMUM_ALLOWED,
6158 &attr_data->attr_data.aapl.max_access);
6159 if (!NT_STATUS_IS_OK(status)) {
6164 return NT_STATUS_OK;
6167 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
6168 fname->base_name, nt_errstr(status)));
6169 TALLOC_FREE(*pattr_data);
6173 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
6175 uint32_t security_info,
6176 TALLOC_CTX *mem_ctx,
6177 struct security_descriptor **ppdesc)
6180 struct security_ace ace;
6182 struct fruit_config_data *config;
6184 SMB_VFS_HANDLE_GET_DATA(handle, config,
6185 struct fruit_config_data,
6186 return NT_STATUS_UNSUCCESSFUL);
6188 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
6190 if (!NT_STATUS_IS_OK(status)) {
6195 * Add MS NFS style ACEs with uid, gid and mode
6197 if (!global_fruit_config.nego_aapl) {
6198 return NT_STATUS_OK;
6200 if (!config->unix_info_enabled) {
6201 return NT_STATUS_OK;
6204 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
6205 status = remove_virtual_nfs_aces(*ppdesc);
6206 if (!NT_STATUS_IS_OK(status)) {
6207 DBG_WARNING("failed to remove MS NFS style ACEs\n");
6211 /* MS NFS style mode */
6212 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
6213 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
6214 status = security_descriptor_dacl_add(*ppdesc, &ace);
6215 if (!NT_STATUS_IS_OK(status)) {
6216 DEBUG(1,("failed to add MS NFS style ACE\n"));
6220 /* MS NFS style uid */
6221 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
6222 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
6223 status = security_descriptor_dacl_add(*ppdesc, &ace);
6224 if (!NT_STATUS_IS_OK(status)) {
6225 DEBUG(1,("failed to add MS NFS style ACE\n"));
6229 /* MS NFS style gid */
6230 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
6231 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
6232 status = security_descriptor_dacl_add(*ppdesc, &ace);
6233 if (!NT_STATUS_IS_OK(status)) {
6234 DEBUG(1,("failed to add MS NFS style ACE\n"));
6238 return NT_STATUS_OK;
6241 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
6243 uint32_t security_info_sent,
6244 const struct security_descriptor *orig_psd)
6248 mode_t ms_nfs_mode = 0;
6250 struct security_descriptor *psd = NULL;
6251 uint32_t orig_num_aces = 0;
6253 if (orig_psd->dacl != NULL) {
6254 orig_num_aces = orig_psd->dacl->num_aces;
6257 psd = security_descriptor_copy(talloc_tos(), orig_psd);
6259 return NT_STATUS_NO_MEMORY;
6262 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
6264 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
6265 if (!NT_STATUS_IS_OK(status)) {
6266 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
6272 * If only ms_nfs ACE entries were sent, ensure we set the DACL
6273 * sent/present flags correctly now we've removed them.
6276 if (orig_num_aces != 0) {
6278 * Are there any ACE's left ?
6280 if (psd->dacl->num_aces == 0) {
6281 /* No - clear the DACL sent/present flags. */
6282 security_info_sent &= ~SECINFO_DACL;
6283 psd->type &= ~SEC_DESC_DACL_PRESENT;
6287 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
6288 if (!NT_STATUS_IS_OK(status)) {
6289 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
6295 if (fsp->fh->fd != -1) {
6296 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
6298 result = SMB_VFS_CHMOD(fsp->conn,
6304 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
6305 result, (unsigned)ms_nfs_mode,
6307 status = map_nt_error_from_unix(errno);
6314 return NT_STATUS_OK;
6317 static struct vfs_offload_ctx *fruit_offload_ctx;
6319 struct fruit_offload_read_state {
6320 struct vfs_handle_struct *handle;
6321 struct tevent_context *ev;
6327 static void fruit_offload_read_done(struct tevent_req *subreq);
6329 static struct tevent_req *fruit_offload_read_send(
6330 TALLOC_CTX *mem_ctx,
6331 struct tevent_context *ev,
6332 struct vfs_handle_struct *handle,
6339 struct tevent_req *req = NULL;
6340 struct tevent_req *subreq = NULL;
6341 struct fruit_offload_read_state *state = NULL;
6343 req = tevent_req_create(mem_ctx, &state,
6344 struct fruit_offload_read_state);
6348 *state = (struct fruit_offload_read_state) {
6355 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
6356 fsctl, ttl, offset, to_copy);
6357 if (tevent_req_nomem(subreq, req)) {
6358 return tevent_req_post(req, ev);
6360 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
6364 static void fruit_offload_read_done(struct tevent_req *subreq)
6366 struct tevent_req *req = tevent_req_callback_data(
6367 subreq, struct tevent_req);
6368 struct fruit_offload_read_state *state = tevent_req_data(
6369 req, struct fruit_offload_read_state);
6372 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
6376 TALLOC_FREE(subreq);
6377 if (tevent_req_nterror(req, status)) {
6381 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
6382 tevent_req_done(req);
6386 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
6387 &fruit_offload_ctx);
6388 if (tevent_req_nterror(req, status)) {
6392 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
6395 if (tevent_req_nterror(req, status)) {
6399 tevent_req_done(req);
6403 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6404 struct vfs_handle_struct *handle,
6405 TALLOC_CTX *mem_ctx,
6408 struct fruit_offload_read_state *state = tevent_req_data(
6409 req, struct fruit_offload_read_state);
6412 if (tevent_req_is_nterror(req, &status)) {
6413 tevent_req_received(req);
6417 token->length = state->token.length;
6418 token->data = talloc_move(mem_ctx, &state->token.data);
6420 tevent_req_received(req);
6421 return NT_STATUS_OK;
6424 struct fruit_offload_write_state {
6425 struct vfs_handle_struct *handle;
6427 struct files_struct *src_fsp;
6428 struct files_struct *dst_fsp;
6432 static void fruit_offload_write_done(struct tevent_req *subreq);
6433 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6434 TALLOC_CTX *mem_ctx,
6435 struct tevent_context *ev,
6438 off_t transfer_offset,
6439 struct files_struct *dest_fsp,
6443 struct tevent_req *req, *subreq;
6444 struct fruit_offload_write_state *state;
6446 struct fruit_config_data *config;
6447 off_t src_off = transfer_offset;
6448 files_struct *src_fsp = NULL;
6449 off_t to_copy = num;
6450 bool copyfile_enabled = false;
6452 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6453 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6455 SMB_VFS_HANDLE_GET_DATA(handle, config,
6456 struct fruit_config_data,
6459 req = tevent_req_create(mem_ctx, &state,
6460 struct fruit_offload_write_state);
6464 state->handle = handle;
6465 state->dst_fsp = dest_fsp;
6468 case FSCTL_SRV_COPYCHUNK:
6469 case FSCTL_SRV_COPYCHUNK_WRITE:
6470 copyfile_enabled = config->copyfile_enabled;
6477 * Check if this a OS X copyfile style copychunk request with
6478 * a requested chunk count of 0 that was translated to a
6479 * offload_write_send VFS call overloading the parameters src_off
6480 * = dest_off = num = 0.
6482 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6483 status = vfs_offload_token_db_fetch_fsp(
6484 fruit_offload_ctx, token, &src_fsp);
6485 if (tevent_req_nterror(req, status)) {
6486 return tevent_req_post(req, ev);
6488 state->src_fsp = src_fsp;
6490 status = vfs_stat_fsp(src_fsp);
6491 if (tevent_req_nterror(req, status)) {
6492 return tevent_req_post(req, ev);
6495 to_copy = src_fsp->fsp_name->st.st_ex_size;
6496 state->is_copyfile = true;
6499 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6508 if (tevent_req_nomem(subreq, req)) {
6509 return tevent_req_post(req, ev);
6512 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6516 static void fruit_offload_write_done(struct tevent_req *subreq)
6518 struct tevent_req *req = tevent_req_callback_data(
6519 subreq, struct tevent_req);
6520 struct fruit_offload_write_state *state = tevent_req_data(
6521 req, struct fruit_offload_write_state);
6523 unsigned int num_streams = 0;
6524 struct stream_struct *streams = NULL;
6526 struct smb_filename *src_fname_tmp = NULL;
6527 struct smb_filename *dst_fname_tmp = NULL;
6529 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6532 TALLOC_FREE(subreq);
6533 if (tevent_req_nterror(req, status)) {
6537 if (!state->is_copyfile) {
6538 tevent_req_done(req);
6543 * Now copy all remaining streams. We know the share supports
6544 * streams, because we're in vfs_fruit. We don't do this async
6545 * because streams are few and small.
6547 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6548 state->src_fsp->fsp_name,
6549 req, &num_streams, &streams);
6550 if (tevent_req_nterror(req, status)) {
6554 if (num_streams == 1) {
6555 /* There is always one stream, ::$DATA. */
6556 tevent_req_done(req);
6560 for (i = 0; i < num_streams; i++) {
6561 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6562 __func__, streams[i].name, (size_t)streams[i].size));
6564 src_fname_tmp = synthetic_smb_fname(
6566 state->src_fsp->fsp_name->base_name,
6569 state->src_fsp->fsp_name->flags);
6570 if (tevent_req_nomem(src_fname_tmp, req)) {
6574 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6575 TALLOC_FREE(src_fname_tmp);
6579 dst_fname_tmp = synthetic_smb_fname(
6581 state->dst_fsp->fsp_name->base_name,
6584 state->dst_fsp->fsp_name->flags);
6585 if (tevent_req_nomem(dst_fname_tmp, req)) {
6586 TALLOC_FREE(src_fname_tmp);
6590 status = copy_file(req,
6591 state->handle->conn,
6594 OPENX_FILE_CREATE_IF_NOT_EXIST,
6596 if (!NT_STATUS_IS_OK(status)) {
6597 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6598 smb_fname_str_dbg(src_fname_tmp),
6599 smb_fname_str_dbg(dst_fname_tmp),
6600 nt_errstr(status)));
6601 TALLOC_FREE(src_fname_tmp);
6602 TALLOC_FREE(dst_fname_tmp);
6603 tevent_req_nterror(req, status);
6607 TALLOC_FREE(src_fname_tmp);
6608 TALLOC_FREE(dst_fname_tmp);
6611 TALLOC_FREE(streams);
6612 TALLOC_FREE(src_fname_tmp);
6613 TALLOC_FREE(dst_fname_tmp);
6614 tevent_req_done(req);
6617 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6618 struct tevent_req *req,
6621 struct fruit_offload_write_state *state = tevent_req_data(
6622 req, struct fruit_offload_write_state);
6625 if (tevent_req_is_nterror(req, &status)) {
6626 DEBUG(1, ("server side copy chunk failed: %s\n",
6627 nt_errstr(status)));
6629 tevent_req_received(req);
6633 *copied = state->copied;
6634 tevent_req_received(req);
6636 return NT_STATUS_OK;
6639 static char *fruit_get_bandsize_line(char **lines, int numlines)
6642 static bool re_initialized = false;
6646 if (!re_initialized) {
6647 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6651 re_initialized = true;
6654 for (i = 0; i < numlines; i++) {
6655 regmatch_t matches[1];
6657 ret = regexec(&re, lines[i], 1, matches, 0);
6660 * Check if the match was on the last line, sa we want
6661 * the subsequent line.
6663 if (i + 1 == numlines) {
6666 return lines[i + 1];
6668 if (ret != REG_NOMATCH) {
6676 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6679 static bool re_initialized = false;
6680 regmatch_t matches[2];
6685 if (!re_initialized) {
6688 "<integer>\\([[:digit:]]*\\)</integer>$",
6693 re_initialized = true;
6696 ret = regexec(&re, line, 2, matches, 0);
6698 DBG_ERR("regex failed [%s]\n", line);
6702 line[matches[1].rm_eo] = '\0';
6704 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6708 *_band_size = (size_t)band_size;
6713 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6714 * "band-size" key and value.
6716 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6720 #define INFO_PLIST_MAX_SIZE 64*1024
6722 struct smb_filename *smb_fname = NULL;
6723 files_struct *fsp = NULL;
6724 uint8_t *file_data = NULL;
6725 char **lines = NULL;
6726 char *band_size_line = NULL;
6727 size_t plist_file_size;
6734 plist = talloc_asprintf(talloc_tos(),
6736 handle->conn->connectpath,
6738 if (plist == NULL) {
6743 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6744 if (smb_fname == NULL) {
6749 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6751 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6756 plist_file_size = smb_fname->st.st_ex_size;
6758 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6759 DBG_INFO("%s is too large, ignoring\n", plist);
6764 status = SMB_VFS_NEXT_CREATE_FILE(
6767 0, /* root_dir_fid */
6768 smb_fname, /* fname */
6769 FILE_GENERIC_READ, /* access_mask */
6770 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6771 FILE_OPEN, /* create_disposition */
6772 0, /* create_options */
6773 0, /* file_attributes */
6774 INTERNAL_OPEN_ONLY, /* oplock_request */
6776 0, /* allocation_size */
6777 0, /* private_flags */
6782 NULL, NULL); /* create context */
6783 if (!NT_STATUS_IS_OK(status)) {
6784 DBG_INFO("Opening [%s] failed [%s]\n",
6785 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6790 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6791 if (file_data == NULL) {
6796 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6797 if (nread != plist_file_size) {
6798 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6799 fsp_str_dbg(fsp), nread, plist_file_size);
6805 status = close_file(NULL, fsp, NORMAL_CLOSE);
6807 if (!NT_STATUS_IS_OK(status)) {
6808 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6813 lines = file_lines_parse((char *)file_data,
6817 if (lines == NULL) {
6822 band_size_line = fruit_get_bandsize_line(lines, numlines);
6823 if (band_size_line == NULL) {
6824 DBG_ERR("Didn't find band-size key in [%s]\n",
6825 smb_fname_str_dbg(smb_fname));
6830 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6832 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6836 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6840 status = close_file(NULL, fsp, NORMAL_CLOSE);
6841 if (!NT_STATUS_IS_OK(status)) {
6842 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6847 TALLOC_FREE(smb_fname);
6848 TALLOC_FREE(file_data);
6853 struct fruit_disk_free_state {
6857 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6862 struct smb_filename *bands_dir = NULL;
6864 struct dirent *e = NULL;
6868 path = talloc_asprintf(talloc_tos(),
6870 handle->conn->connectpath,
6876 bands_dir = synthetic_smb_fname(talloc_tos(),
6882 if (bands_dir == NULL) {
6886 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6888 TALLOC_FREE(bands_dir);
6894 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6896 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6898 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6904 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6906 TALLOC_FREE(bands_dir);
6910 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6912 TALLOC_FREE(bands_dir);
6918 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6919 struct fruit_disk_free_state *state,
6924 size_t sparsebundle_strlen = strlen("sparsebundle");
6925 size_t bandsize = 0;
6929 p = strstr(e->d_name, "sparsebundle");
6934 if (p[sparsebundle_strlen] != '\0') {
6938 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6940 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6943 * Beware of race conditions: this may be an uninitialized
6944 * Info.plist that a client is just creating. We don't want let
6945 * this to trigger complete failure.
6947 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6951 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6954 * Beware of race conditions: this may be a backup sparsebundle
6955 * in an early stage lacking a bands subdirectory. We don't want
6956 * let this to trigger complete failure.
6958 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6962 if (bandsize > SIZE_MAX/nbands) {
6963 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6967 tm_size = bandsize * nbands;
6969 if (state->total_size + tm_size < state->total_size) {
6970 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6975 state->total_size += tm_size;
6977 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6978 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6984 * Calculate used size of a TimeMachine volume
6986 * This assumes that the volume is used only for TimeMachine.
6988 * - readdir(basedir of share), then
6989 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6990 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6991 * - count band files in "\1.sparsebundle/bands/"
6992 * - calculate used size of all bands: band_count * band_size
6994 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6995 const struct smb_filename *smb_fname,
7000 struct fruit_config_data *config = NULL;
7001 struct fruit_disk_free_state state = {0};
7003 struct dirent *e = NULL;
7009 SMB_VFS_HANDLE_GET_DATA(handle, config,
7010 struct fruit_config_data,
7013 if (!config->time_machine ||
7014 config->time_machine_max_size == 0)
7016 return SMB_VFS_NEXT_DISK_FREE(handle,
7023 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
7028 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
7030 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
7032 ok = fruit_tmsize_do_dirent(handle, &state, e);
7034 SMB_VFS_NEXT_CLOSEDIR(handle, d);
7039 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
7044 dsize = config->time_machine_max_size / 512;
7045 dfree = dsize - (state.total_size / 512);
7046 if (dfree > dsize) {
7056 static struct vfs_fn_pointers vfs_fruit_fns = {
7057 .connect_fn = fruit_connect,
7058 .disk_free_fn = fruit_disk_free,
7060 /* File operations */
7061 .chmod_fn = fruit_chmod,
7062 .chown_fn = fruit_chown,
7063 .unlink_fn = fruit_unlink,
7064 .rename_fn = fruit_rename,
7065 .rmdir_fn = fruit_rmdir,
7066 .open_fn = fruit_open,
7067 .pread_fn = fruit_pread,
7068 .pwrite_fn = fruit_pwrite,
7069 .pread_send_fn = fruit_pread_send,
7070 .pread_recv_fn = fruit_pread_recv,
7071 .pwrite_send_fn = fruit_pwrite_send,
7072 .pwrite_recv_fn = fruit_pwrite_recv,
7073 .stat_fn = fruit_stat,
7074 .lstat_fn = fruit_lstat,
7075 .fstat_fn = fruit_fstat,
7076 .streaminfo_fn = fruit_streaminfo,
7077 .ntimes_fn = fruit_ntimes,
7078 .ftruncate_fn = fruit_ftruncate,
7079 .fallocate_fn = fruit_fallocate,
7080 .create_file_fn = fruit_create_file,
7081 .readdir_attr_fn = fruit_readdir_attr,
7082 .offload_read_send_fn = fruit_offload_read_send,
7083 .offload_read_recv_fn = fruit_offload_read_recv,
7084 .offload_write_send_fn = fruit_offload_write_send,
7085 .offload_write_recv_fn = fruit_offload_write_recv,
7087 /* NT ACL operations */
7088 .fget_nt_acl_fn = fruit_fget_nt_acl,
7089 .fset_nt_acl_fn = fruit_fset_nt_acl,
7093 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
7095 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
7097 if (!NT_STATUS_IS_OK(ret)) {
7101 vfs_fruit_debug_level = debug_add_class("fruit");
7102 if (vfs_fruit_debug_level == -1) {
7103 vfs_fruit_debug_level = DBGC_VFS;
7104 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
7107 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
7108 "vfs_fruit_init","fruit",vfs_fruit_debug_level));