2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
98 static int vfs_fruit_debug_level
= DBGC_VFS
;
100 static struct global_fruit_config
{
101 bool nego_aapl
; /* client negotiated AAPL */
103 } global_fruit_config
;
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
122 enum apple_fork
{APPLE_FORK_DATA
, APPLE_FORK_RSRC
};
124 enum fruit_rsrc
{FRUIT_RSRC_STREAM
, FRUIT_RSRC_ADFILE
, FRUIT_RSRC_XATTR
};
125 enum fruit_meta
{FRUIT_META_STREAM
, FRUIT_META_NETATALK
};
126 enum fruit_locking
{FRUIT_LOCKING_NETATALK
, FRUIT_LOCKING_NONE
};
127 enum fruit_encoding
{FRUIT_ENC_NATIVE
, FRUIT_ENC_PRIVATE
};
129 struct fruit_config_data
{
130 enum fruit_rsrc rsrc
;
131 enum fruit_meta meta
;
132 enum fruit_locking locking
;
133 enum fruit_encoding encoding
;
134 bool use_aapl
; /* config from smb.conf */
136 bool readdir_attr_enabled
;
137 bool unix_info_enabled
;
138 bool copyfile_enabled
;
139 bool veto_appledouble
;
141 bool aapl_zero_file_id
;
144 off_t time_machine_max_size
;
147 * Additional options, all enabled by default,
148 * possibly useful for analyzing performance. The associated
149 * operations with each of them may be expensive, so having
150 * the chance to disable them individually gives a chance
151 * tweaking the setup for the particular usecase.
153 bool readdir_attr_rsize
;
154 bool readdir_attr_finder_info
;
155 bool readdir_attr_max_access
;
158 static const struct enum_list fruit_rsrc
[] = {
159 {FRUIT_RSRC_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
160 {FRUIT_RSRC_ADFILE
, "file"}, /* ._ AppleDouble file */
161 {FRUIT_RSRC_XATTR
, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
165 static const struct enum_list fruit_meta
[] = {
166 {FRUIT_META_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
167 {FRUIT_META_NETATALK
, "netatalk"}, /* Netatalk compatible xattr */
171 static const struct enum_list fruit_locking
[] = {
172 {FRUIT_LOCKING_NETATALK
, "netatalk"}, /* synchronize locks with Netatalk */
173 {FRUIT_LOCKING_NONE
, "none"},
177 static const struct enum_list fruit_encoding
[] = {
178 {FRUIT_ENC_NATIVE
, "native"}, /* map unicode private chars to ASCII */
179 {FRUIT_ENC_PRIVATE
, "private"}, /* keep unicode private chars */
183 static const char *fruit_catia_maps
=
184 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
185 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
186 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
187 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
188 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
189 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
190 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
191 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
192 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
193 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
196 /*****************************************************************************
197 * Defines, functions and data structures that deal with AppleDouble
198 *****************************************************************************/
201 * There are two AppleDouble blobs we deal with:
203 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
204 * metadata in an xattr
206 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
209 typedef enum {ADOUBLE_META
, ADOUBLE_RSRC
} adouble_type_t
;
212 #define AD_VERSION2 0x00020000
213 #define AD_VERSION AD_VERSION2
216 * AppleDouble entry IDs.
218 #define ADEID_DFORK 1
219 #define ADEID_RFORK 2
221 #define ADEID_COMMENT 4
222 #define ADEID_ICONBW 5
223 #define ADEID_ICONCOL 6
224 #define ADEID_FILEI 7
225 #define ADEID_FILEDATESI 8
226 #define ADEID_FINDERI 9
227 #define ADEID_MACFILEI 10
228 #define ADEID_PRODOSFILEI 11
229 #define ADEID_MSDOSFILEI 12
230 #define ADEID_SHORTNAME 13
231 #define ADEID_AFPFILEI 14
234 /* Private Netatalk entries */
235 #define ADEID_PRIVDEV 16
236 #define ADEID_PRIVINO 17
237 #define ADEID_PRIVSYN 18
238 #define ADEID_PRIVID 19
239 #define ADEID_MAX (ADEID_PRIVID + 1)
242 * These are the real ids for the private entries,
243 * as stored in the adouble file
245 #define AD_DEV 0x80444556
246 #define AD_INO 0x80494E4F
247 #define AD_SYN 0x8053594E
248 #define AD_ID 0x8053567E
250 /* Number of actually used entries */
251 #define ADEID_NUM_XATTR 8
252 #define ADEID_NUM_DOT_UND 2
253 #define ADEID_NUM_RSRC_XATTR 1
255 /* AppleDouble magic */
256 #define AD_APPLESINGLE_MAGIC 0x00051600
257 #define AD_APPLEDOUBLE_MAGIC 0x00051607
258 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
260 /* Sizes of relevant entry bits */
261 #define ADEDLEN_MAGIC 4
262 #define ADEDLEN_VERSION 4
263 #define ADEDLEN_FILLER 16
264 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
265 #define AD_FILLER_TAG_OSX "Mac OS X " /* should be 16 bytes */
266 #define ADEDLEN_NENTRIES 2
267 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
268 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
269 #define AD_ENTRY_LEN_EID 4
270 #define AD_ENTRY_LEN_OFF 4
271 #define AD_ENTRY_LEN_LEN 4
272 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
275 #define ADEDLEN_NAME 255
276 #define ADEDLEN_COMMENT 200
277 #define ADEDLEN_FILEI 16
278 #define ADEDLEN_FINDERI 32
279 #define ADEDLEN_FILEDATESI 16
280 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
281 #define ADEDLEN_AFPFILEI 4
282 #define ADEDLEN_MACFILEI 4
283 #define ADEDLEN_PRODOSFILEI 8
284 #define ADEDLEN_MSDOSFILEI 2
285 #define ADEDLEN_DID 4
286 #define ADEDLEN_PRIVDEV 8
287 #define ADEDLEN_PRIVINO 8
288 #define ADEDLEN_PRIVSYN 8
289 #define ADEDLEN_PRIVID 4
292 #define ADEDOFF_MAGIC 0
293 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
294 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
295 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
297 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
298 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
299 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
300 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
301 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
303 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
304 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
305 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
306 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
308 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
309 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
310 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
312 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
313 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
314 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
315 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
316 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
317 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
319 #if AD_DATASZ_XATTR != 402
320 #error bad size for AD_DATASZ_XATTR
323 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
324 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
326 #if AD_DATASZ_DOT_UND != 82
327 #error bad size for AD_DATASZ_DOT_UND
331 * Sharemode locks fcntl() offsets
333 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
334 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
336 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
338 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
340 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
341 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
342 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
343 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
344 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
345 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
346 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
347 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
348 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
349 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
351 /* Time stuff we overload the bits a little */
352 #define AD_DATE_CREATE 0
353 #define AD_DATE_MODIFY 4
354 #define AD_DATE_BACKUP 8
355 #define AD_DATE_ACCESS 12
356 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
357 AD_DATE_BACKUP | AD_DATE_ACCESS)
358 #define AD_DATE_UNIX (1 << 10)
359 #define AD_DATE_START 0x80000000
360 #define AD_DATE_DELTA 946684800
361 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
362 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
364 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
365 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
366 #define AD_XATTR_HDR_SIZE 36
367 #define AD_XATTR_MAX_HDR_SIZE 65536
369 /* Accessor macros */
370 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
371 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
372 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
373 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
376 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
377 * representation as well as the on-disk format.
379 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
380 * the length of the FinderInfo entry is larger then 32 bytes. It is then
381 * preceeded with 2 bytes padding.
383 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
386 struct ad_xattr_header
{
387 uint32_t adx_magic
; /* ATTR_HDR_MAGIC */
388 uint32_t adx_debug_tag
; /* for debugging == file id of owning file */
389 uint32_t adx_total_size
; /* file offset of end of attribute header + entries + data */
390 uint32_t adx_data_start
; /* file offset to attribute data area */
391 uint32_t adx_data_length
; /* length of attribute data area */
392 uint32_t adx_reserved
[3];
394 uint16_t adx_num_attrs
;
397 /* On-disk entries are aligned on 4 byte boundaries */
398 struct ad_xattr_entry
{
399 uint32_t adx_offset
; /* file offset to data */
400 uint32_t adx_length
; /* size of attribute data */
402 uint8_t adx_namelen
; /* included the NULL terminator */
403 char *adx_name
; /* NULL-terminated UTF-8 name */
412 vfs_handle_struct
*ad_handle
;
415 adouble_type_t ad_type
;
418 uint8_t ad_filler
[ADEDLEN_FILLER
];
419 struct ad_entry ad_eid
[ADEID_MAX
];
421 struct ad_xattr_header adx_header
;
422 struct ad_xattr_entry
*adx_entries
;
425 struct ad_entry_order
{
426 uint32_t id
, offset
, len
;
429 /* Netatalk AppleDouble metadata xattr */
431 struct ad_entry_order entry_order_meta_xattr
[ADEID_NUM_XATTR
+ 1] = {
432 {ADEID_FINDERI
, ADEDOFF_FINDERI_XATTR
, ADEDLEN_FINDERI
},
433 {ADEID_COMMENT
, ADEDOFF_COMMENT_XATTR
, 0},
434 {ADEID_FILEDATESI
, ADEDOFF_FILEDATESI_XATTR
, ADEDLEN_FILEDATESI
},
435 {ADEID_AFPFILEI
, ADEDOFF_AFPFILEI_XATTR
, ADEDLEN_AFPFILEI
},
436 {ADEID_PRIVDEV
, ADEDOFF_PRIVDEV_XATTR
, 0},
437 {ADEID_PRIVINO
, ADEDOFF_PRIVINO_XATTR
, 0},
438 {ADEID_PRIVSYN
, ADEDOFF_PRIVSYN_XATTR
, 0},
439 {ADEID_PRIVID
, ADEDOFF_PRIVID_XATTR
, 0},
443 /* AppleDouble resource fork file (the ones prefixed by "._") */
445 struct ad_entry_order entry_order_dot_und
[ADEID_NUM_DOT_UND
+ 1] = {
446 {ADEID_FINDERI
, ADEDOFF_FINDERI_DOT_UND
, ADEDLEN_FINDERI
},
447 {ADEID_RFORK
, ADEDOFF_RFORK_DOT_UND
, 0},
452 * Fake AppleDouble entry oder for resource fork xattr. The xattr
453 * isn't an AppleDouble file, it simply contains the resource data,
454 * but in order to be able to use some API calls like ad_getentryoff()
455 * we build a fake/helper struct adouble with this entry order struct.
458 struct ad_entry_order entry_order_rsrc_xattr
[ADEID_NUM_RSRC_XATTR
+ 1] = {
463 /* Conversion from enumerated id to on-disk AppleDouble id */
464 #define AD_EID_DISK(a) (set_eid[a])
465 static const uint32_t set_eid
[] = {
466 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
467 AD_DEV
, AD_INO
, AD_SYN
, AD_ID
471 /* tcon config handle */
472 struct fruit_config_data
*config
;
474 /* Denote stream type, meta or rsrc */
479 * Forward declarations
481 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
482 adouble_type_t type
);
483 static int ad_set(struct adouble
*ad
, const struct smb_filename
*smb_fname
);
484 static int ad_fset(struct adouble
*ad
, files_struct
*fsp
);
485 static int adouble_path(TALLOC_CTX
*ctx
,
486 const struct smb_filename
*smb_fname__in
,
487 struct smb_filename
**ppsmb_fname_out
);
488 static AfpInfo
*afpinfo_new(TALLOC_CTX
*ctx
);
489 static ssize_t
afpinfo_pack(const AfpInfo
*ai
, char *buf
);
490 static AfpInfo
*afpinfo_unpack(TALLOC_CTX
*ctx
, const void *data
);
494 * Return a pointer to an AppleDouble entry
496 * Returns NULL if the entry is not present
498 static char *ad_get_entry(const struct adouble
*ad
, int eid
)
500 off_t off
= ad_getentryoff(ad
, eid
);
501 size_t len
= ad_getentrylen(ad
, eid
);
503 if (off
== 0 || len
== 0) {
507 return ad
->ad_data
+ off
;
513 static int ad_getdate(const struct adouble
*ad
,
514 unsigned int dateoff
,
517 bool xlate
= (dateoff
& AD_DATE_UNIX
);
520 dateoff
&= AD_DATE_MASK
;
521 p
= ad_get_entry(ad
, ADEID_FILEDATESI
);
526 if (dateoff
> AD_DATE_ACCESS
) {
530 memcpy(date
, p
+ dateoff
, sizeof(uint32_t));
533 *date
= AD_DATE_TO_UNIX(*date
);
541 static int ad_setdate(struct adouble
*ad
, unsigned int dateoff
, uint32_t date
)
543 bool xlate
= (dateoff
& AD_DATE_UNIX
);
546 p
= ad_get_entry(ad
, ADEID_FILEDATESI
);
551 dateoff
&= AD_DATE_MASK
;
553 date
= AD_DATE_FROM_UNIX(date
);
556 if (dateoff
> AD_DATE_ACCESS
) {
560 memcpy(p
+ dateoff
, &date
, sizeof(date
));
567 * Map on-disk AppleDouble id to enumerated id
569 static uint32_t get_eid(uint32_t eid
)
577 return ADEID_PRIVDEV
;
579 return ADEID_PRIVINO
;
581 return ADEID_PRIVSYN
;
592 * Pack AppleDouble structure into data buffer
594 static bool ad_pack(struct adouble
*ad
)
601 bufsize
= talloc_get_size(ad
->ad_data
);
602 if (bufsize
< AD_DATASZ_DOT_UND
) {
603 DBG_ERR("bad buffer size [0x%" PRIx32
"]\n", bufsize
);
607 if (offset
+ ADEDLEN_MAGIC
< offset
||
608 offset
+ ADEDLEN_MAGIC
>= bufsize
) {
611 RSIVAL(ad
->ad_data
, offset
, ad
->ad_magic
);
612 offset
+= ADEDLEN_MAGIC
;
614 if (offset
+ ADEDLEN_VERSION
< offset
||
615 offset
+ ADEDLEN_VERSION
>= bufsize
) {
618 RSIVAL(ad
->ad_data
, offset
, ad
->ad_version
);
619 offset
+= ADEDLEN_VERSION
;
621 if (offset
+ ADEDLEN_FILLER
< offset
||
622 offset
+ ADEDLEN_FILLER
>= bufsize
) {
625 if (ad
->ad_type
== ADOUBLE_RSRC
) {
626 memcpy(ad
->ad_data
+ offset
, AD_FILLER_TAG
, ADEDLEN_FILLER
);
628 offset
+= ADEDLEN_FILLER
;
630 if (offset
+ ADEDLEN_NENTRIES
< offset
||
631 offset
+ ADEDLEN_NENTRIES
>= bufsize
) {
634 offset
+= ADEDLEN_NENTRIES
;
636 for (eid
= 0, nent
= 0; eid
< ADEID_MAX
; eid
++) {
637 if (ad
->ad_eid
[eid
].ade_off
== 0) {
639 * ade_off is also used as indicator whether a
640 * specific entry is used or not
645 if (offset
+ AD_ENTRY_LEN_EID
< offset
||
646 offset
+ AD_ENTRY_LEN_EID
>= bufsize
) {
649 RSIVAL(ad
->ad_data
, offset
, AD_EID_DISK(eid
));
650 offset
+= AD_ENTRY_LEN_EID
;
652 if (offset
+ AD_ENTRY_LEN_OFF
< offset
||
653 offset
+ AD_ENTRY_LEN_OFF
>= bufsize
) {
656 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_off
);
657 offset
+= AD_ENTRY_LEN_OFF
;
659 if (offset
+ AD_ENTRY_LEN_LEN
< offset
||
660 offset
+ AD_ENTRY_LEN_LEN
>= bufsize
) {
663 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_len
);
664 offset
+= AD_ENTRY_LEN_LEN
;
669 if (ADEDOFF_NENTRIES
+ 2 >= bufsize
) {
672 RSSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
, nent
);
677 static bool ad_unpack_xattrs(struct adouble
*ad
)
679 struct ad_xattr_header
*h
= &ad
->adx_header
;
680 const char *p
= ad
->ad_data
;
684 if (ad_getentrylen(ad
, ADEID_FINDERI
) <= ADEDLEN_FINDERI
) {
688 /* 2 bytes padding */
689 hoff
= ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
+ 2;
691 h
->adx_magic
= RIVAL(p
, hoff
+ 0);
692 h
->adx_debug_tag
= RIVAL(p
, hoff
+ 4); /* Not used -> not checked */
693 h
->adx_total_size
= RIVAL(p
, hoff
+ 8);
694 h
->adx_data_start
= RIVAL(p
, hoff
+ 12);
695 h
->adx_data_length
= RIVAL(p
, hoff
+ 16);
696 h
->adx_flags
= RSVAL(p
, hoff
+ 32); /* Not used -> not checked */
697 h
->adx_num_attrs
= RSVAL(p
, hoff
+ 34);
699 if (h
->adx_magic
!= AD_XATTR_HDR_MAGIC
) {
700 DBG_ERR("Bad magic: 0x%" PRIx32
"\n", h
->adx_magic
);
704 if (h
->adx_total_size
> ad_getentryoff(ad
, ADEID_RFORK
)) {
705 DBG_ERR("Bad total size: 0x%" PRIx32
"\n", h
->adx_total_size
);
708 if (h
->adx_total_size
> AD_XATTR_MAX_HDR_SIZE
) {
709 DBG_ERR("Bad total size: 0x%" PRIx32
"\n", h
->adx_total_size
);
713 if (h
->adx_data_start
< (hoff
+ AD_XATTR_HDR_SIZE
)) {
714 DBG_ERR("Bad start: 0x%" PRIx32
"\n", h
->adx_data_start
);
718 if ((h
->adx_data_start
+ h
->adx_data_length
) < h
->adx_data_start
) {
719 DBG_ERR("Bad length: %" PRIu32
"\n", h
->adx_data_length
);
722 if ((h
->adx_data_start
+ h
->adx_data_length
) >
723 ad
->adx_header
.adx_total_size
)
725 DBG_ERR("Bad length: %" PRIu32
"\n", h
->adx_data_length
);
729 if (h
->adx_num_attrs
> AD_XATTR_MAX_ENTRIES
) {
730 DBG_ERR("Bad num xattrs: %" PRIu16
"\n", h
->adx_num_attrs
);
734 if (h
->adx_num_attrs
== 0) {
738 ad
->adx_entries
= talloc_zero_array(
739 ad
, struct ad_xattr_entry
, h
->adx_num_attrs
);
740 if (ad
->adx_entries
== NULL
) {
744 hoff
+= AD_XATTR_HDR_SIZE
;
746 for (i
= 0; i
< h
->adx_num_attrs
; i
++) {
747 struct ad_xattr_entry
*e
= &ad
->adx_entries
[i
];
749 hoff
= (hoff
+ 3) & ~3;
751 e
->adx_offset
= RIVAL(p
, hoff
+ 0);
752 e
->adx_length
= RIVAL(p
, hoff
+ 4);
753 e
->adx_flags
= RSVAL(p
, hoff
+ 8);
754 e
->adx_namelen
= *(p
+ hoff
+ 10);
756 if (e
->adx_offset
>= ad
->adx_header
.adx_total_size
) {
757 DBG_ERR("Bad adx_offset: %" PRIx32
"\n",
762 if ((e
->adx_offset
+ e
->adx_length
) < e
->adx_offset
) {
763 DBG_ERR("Bad adx_length: %" PRIx32
"\n",
768 if ((e
->adx_offset
+ e
->adx_length
) >
769 ad
->adx_header
.adx_total_size
)
771 DBG_ERR("Bad adx_length: %" PRIx32
"\n",
776 if (e
->adx_namelen
== 0) {
777 DBG_ERR("Bad adx_namelen: %" PRIx32
"\n",
781 if ((hoff
+ 11 + e
->adx_namelen
) < hoff
+ 11) {
782 DBG_ERR("Bad adx_namelen: %" PRIx32
"\n",
786 if ((hoff
+ 11 + e
->adx_namelen
) >
787 ad
->adx_header
.adx_data_start
)
789 DBG_ERR("Bad adx_namelen: %" PRIx32
"\n",
794 e
->adx_name
= talloc_strndup(ad
->adx_entries
,
797 if (e
->adx_name
== NULL
) {
801 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
802 e
->adx_name
, e
->adx_offset
, e
->adx_length
);
803 dump_data(10, (uint8_t *)(ad
->ad_data
+ e
->adx_offset
),
806 hoff
+= 11 + e
->adx_namelen
;
813 * Unpack an AppleDouble blob into a struct adoble
815 static bool ad_unpack(struct adouble
*ad
, const size_t nentries
,
818 size_t bufsize
= talloc_get_size(ad
->ad_data
);
820 uint32_t eid
, len
, off
;
824 * The size of the buffer ad->ad_data is checked when read, so
825 * we wouldn't have to check our own offsets, a few extra
826 * checks won't hurt though. We have to check the offsets we
827 * read from the buffer anyway.
830 if (bufsize
< (AD_HEADER_LEN
+ (AD_ENTRY_LEN
* nentries
))) {
831 DEBUG(1, ("bad size\n"));
835 ad
->ad_magic
= RIVAL(ad
->ad_data
, 0);
836 ad
->ad_version
= RIVAL(ad
->ad_data
, ADEDOFF_VERSION
);
837 if ((ad
->ad_magic
!= AD_MAGIC
) || (ad
->ad_version
!= AD_VERSION
)) {
838 DEBUG(1, ("wrong magic or version\n"));
842 memcpy(ad
->ad_filler
, ad
->ad_data
+ ADEDOFF_FILLER
, ADEDLEN_FILLER
);
844 adentries
= RSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
);
845 if (adentries
!= nentries
) {
846 DEBUG(1, ("invalid number of entries: %zu\n",
851 /* now, read in the entry bits */
852 for (i
= 0; i
< adentries
; i
++) {
853 eid
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
));
855 off
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 4);
856 len
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 8);
858 if (!eid
|| eid
>= ADEID_MAX
) {
859 DEBUG(1, ("bogus eid %d\n", eid
));
864 * All entries other than the resource fork are
865 * expected to be read into the ad_data buffer, so
866 * ensure the specified offset is within that bound
868 if ((off
> bufsize
) && (eid
!= ADEID_RFORK
)) {
869 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
875 * All entries besides FinderInfo and resource fork
876 * must fit into the buffer. FinderInfo is special as
877 * it may be larger then the default 32 bytes (if it
878 * contains marshalled xattrs), but we will fixup that
879 * in ad_convert(). And the resource fork is never
880 * accessed directly by the ad_data buf (also see
881 * comment above) anyway.
883 if ((eid
!= ADEID_RFORK
) &&
884 (eid
!= ADEID_FINDERI
) &&
885 ((off
+ len
) > bufsize
)) {
886 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
892 * That would be obviously broken
894 if (off
> filesize
) {
895 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
901 * Check for any entry that has its end beyond the
904 if (off
+ len
< off
) {
905 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
906 ", len: %" PRIu32
"\n",
911 if (off
+ len
> filesize
) {
913 * If this is the resource fork entry, we fix
914 * up the length, for any other entry we bail
917 if (eid
!= ADEID_RFORK
) {
918 DEBUG(1, ("bogus eid %d: off: %" PRIu32
919 ", len: %" PRIu32
"\n",
925 * Fixup the resource fork entry by limiting
926 * the size to entryoffset - filesize.
928 len
= filesize
- off
;
929 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
930 ", len: %" PRIu32
"\n", off
, len
));
933 ad
->ad_eid
[eid
].ade_off
= off
;
934 ad
->ad_eid
[eid
].ade_len
= len
;
937 ok
= ad_unpack_xattrs(ad
);
945 static bool ad_convert_move_reso(struct adouble
*ad
,
946 const struct smb_filename
*smb_fname
)
948 char *map
= MAP_FAILED
;
954 if (ad_getentrylen(ad
, ADEID_RFORK
) == 0) {
958 maplen
= ad_getentryoff(ad
, ADEID_RFORK
) +
959 ad_getentrylen(ad
, ADEID_RFORK
);
961 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
962 map
= mmap(NULL
, maplen
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
964 if (map
== MAP_FAILED
) {
965 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno
));
970 memmove(map
+ ADEDOFF_RFORK_DOT_UND
,
971 map
+ ad_getentryoff(ad
, ADEID_RFORK
),
972 ad_getentrylen(ad
, ADEID_RFORK
));
974 rc
= munmap(map
, maplen
);
976 DBG_ERR("munmap failed: %s\n", strerror(errno
));
980 ad_setentryoff(ad
, ADEID_RFORK
, ADEDOFF_RFORK_DOT_UND
);
984 DBG_WARNING("ad_pack [%s] failed\n", smb_fname
->base_name
);
988 len
= sys_pwrite(ad
->ad_fd
, ad
->ad_data
, AD_DATASZ_DOT_UND
, 0);
989 if (len
!= AD_DATASZ_DOT_UND
) {
990 DBG_ERR("%s: bad size: %zd\n", smb_fname
->base_name
, len
);
997 static bool ad_convert_xattr(struct adouble
*ad
,
998 const struct smb_filename
*smb_fname
)
1000 static struct char_mappings
**string_replace_cmaps
= NULL
;
1001 char *map
= MAP_FAILED
;
1005 int saved_errno
= 0;
1010 if (ad
->adx_header
.adx_num_attrs
== 0) {
1014 if (string_replace_cmaps
== NULL
) {
1015 const char **mappings
= NULL
;
1017 mappings
= str_list_make_v3_const(
1018 talloc_tos(), fruit_catia_maps
, NULL
);
1019 if (mappings
== NULL
) {
1022 string_replace_cmaps
= string_replace_init_map(mappings
);
1023 TALLOC_FREE(mappings
);
1026 maplen
= ad_getentryoff(ad
, ADEID_RFORK
) +
1027 ad_getentrylen(ad
, ADEID_RFORK
);
1029 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1030 map
= mmap(NULL
, maplen
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
1032 if (map
== MAP_FAILED
) {
1033 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno
));
1037 for (i
= 0; i
< ad
->adx_header
.adx_num_attrs
; i
++) {
1038 struct ad_xattr_entry
*e
= &ad
->adx_entries
[i
];
1039 char *mapped_name
= NULL
;
1041 struct smb_filename
*stream_name
= NULL
;
1042 files_struct
*fsp
= NULL
;
1045 status
= string_replace_allocate(ad
->ad_handle
->conn
,
1047 string_replace_cmaps
,
1050 vfs_translate_to_windows
);
1051 if (!NT_STATUS_IS_OK(status
) &&
1052 !NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
))
1054 DBG_ERR("string_replace_allocate failed\n");
1060 mapped_name
= talloc_asprintf(talloc_tos(), ":%s", tmp
);
1062 if (mapped_name
== NULL
) {
1067 stream_name
= synthetic_smb_fname(talloc_tos(),
1068 smb_fname
->base_name
,
1072 TALLOC_FREE(mapped_name
);
1073 if (stream_name
== NULL
) {
1074 DBG_ERR("synthetic_smb_fname failed\n");
1079 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name
));
1081 status
= SMB_VFS_CREATE_FILE(
1082 ad
->ad_handle
->conn
, /* conn */
1084 0, /* root_dir_fid */
1085 stream_name
, /* fname */
1086 FILE_GENERIC_WRITE
, /* access_mask */
1087 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
1088 FILE_OPEN_IF
, /* create_disposition */
1089 0, /* create_options */
1090 0, /* file_attributes */
1091 INTERNAL_OPEN_ONLY
, /* oplock_request */
1093 0, /* allocation_size */
1094 0, /* private_flags */
1099 NULL
, NULL
); /* create context */
1100 TALLOC_FREE(stream_name
);
1101 if (!NT_STATUS_IS_OK(status
)) {
1102 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1107 nwritten
= SMB_VFS_PWRITE(fsp
,
1108 map
+ e
->adx_offset
,
1111 if (nwritten
== -1) {
1112 DBG_ERR("SMB_VFS_PWRITE failed\n");
1113 saved_errno
= errno
;
1114 close_file(NULL
, fsp
, ERROR_CLOSE
);
1115 errno
= saved_errno
;
1120 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
1121 if (!NT_STATUS_IS_OK(status
)) {
1128 ad_setentrylen(ad
, ADEID_FINDERI
, ADEDLEN_FINDERI
);
1132 DBG_WARNING("ad_pack [%s] failed\n", smb_fname
->base_name
);
1136 len
= sys_pwrite(ad
->ad_fd
, ad
->ad_data
, AD_DATASZ_DOT_UND
, 0);
1137 if (len
!= AD_DATASZ_DOT_UND
) {
1138 DBG_ERR("%s: bad size: %zd\n", smb_fname
->base_name
, len
);
1143 ok
= ad_convert_move_reso(ad
, smb_fname
);
1151 rc
= munmap(map
, maplen
);
1153 DBG_ERR("munmap failed: %s\n", strerror(errno
));
1160 static bool ad_convert_finderinfo(struct adouble
*ad
,
1161 const struct smb_filename
*smb_fname
)
1166 struct smb_filename
*stream_name
= NULL
;
1167 files_struct
*fsp
= NULL
;
1171 int saved_errno
= 0;
1173 p_ad
= ad_get_entry(ad
, ADEID_FINDERI
);
1178 ai
= afpinfo_new(talloc_tos());
1183 memcpy(ai
->afpi_FinderInfo
, p_ad
, ADEDLEN_FINDERI
);
1185 aiblob
= data_blob_talloc(talloc_tos(), NULL
, AFP_INFO_SIZE
);
1186 if (aiblob
.data
== NULL
) {
1191 size
= afpinfo_pack(ai
, (char *)aiblob
.data
);
1193 if (size
!= AFP_INFO_SIZE
) {
1197 stream_name
= synthetic_smb_fname(talloc_tos(),
1198 smb_fname
->base_name
,
1202 if (stream_name
== NULL
) {
1203 data_blob_free(&aiblob
);
1204 DBG_ERR("synthetic_smb_fname failed\n");
1208 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name
));
1210 status
= SMB_VFS_CREATE_FILE(
1211 ad
->ad_handle
->conn
, /* conn */
1213 0, /* root_dir_fid */
1214 stream_name
, /* fname */
1215 FILE_GENERIC_WRITE
, /* access_mask */
1216 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
1217 FILE_OPEN_IF
, /* create_disposition */
1218 0, /* create_options */
1219 0, /* file_attributes */
1220 INTERNAL_OPEN_ONLY
, /* oplock_request */
1222 0, /* allocation_size */
1223 0, /* private_flags */
1228 NULL
, NULL
); /* create context */
1229 TALLOC_FREE(stream_name
);
1230 if (!NT_STATUS_IS_OK(status
)) {
1231 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1235 nwritten
= SMB_VFS_PWRITE(fsp
,
1239 if (nwritten
== -1) {
1240 DBG_ERR("SMB_VFS_PWRITE failed\n");
1241 saved_errno
= errno
;
1242 close_file(NULL
, fsp
, ERROR_CLOSE
);
1243 errno
= saved_errno
;
1247 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
1248 if (!NT_STATUS_IS_OK(status
)) {
1256 static bool ad_convert_truncate(struct adouble
*ad
,
1257 const struct smb_filename
*smb_fname
)
1262 * FIXME: direct ftruncate(), but we don't have a fsp for the
1265 rc
= ftruncate(ad
->ad_fd
, ADEDOFF_RFORK_DOT_UND
+
1266 ad_getentrylen(ad
, ADEID_RFORK
));
1275 * Convert from Apple's ._ file to Netatalk
1277 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1278 * bytes containing packed xattrs.
1280 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1283 static int ad_convert(struct adouble
*ad
,
1284 const struct smb_filename
*smb_fname
)
1288 if (ad_getentrylen(ad
, ADEID_FINDERI
) == ADEDLEN_FINDERI
) {
1292 ok
= ad_convert_xattr(ad
, smb_fname
);
1297 ok
= ad_convert_truncate(ad
, smb_fname
);
1302 ok
= ad_convert_finderinfo(ad
, smb_fname
);
1304 DBG_ERR("Failed to convert [%s]\n",
1305 smb_fname_str_dbg(smb_fname
));
1313 * Read and parse Netatalk AppleDouble metadata xattr
1315 static ssize_t
ad_read_meta(struct adouble
*ad
,
1316 const struct smb_filename
*smb_fname
)
1322 DEBUG(10, ("reading meta xattr for %s\n", smb_fname
->base_name
));
1324 ealen
= SMB_VFS_GETXATTR(ad
->ad_handle
->conn
, smb_fname
,
1325 AFPINFO_EA_NETATALK
, ad
->ad_data
,
1331 if (errno
== ENOATTR
) {
1337 DEBUG(2, ("error reading meta xattr: %s\n",
1343 if (ealen
!= AD_DATASZ_XATTR
) {
1344 DEBUG(2, ("bad size %zd\n", ealen
));
1350 /* Now parse entries */
1351 ok
= ad_unpack(ad
, ADEID_NUM_XATTR
, AD_DATASZ_XATTR
);
1353 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1359 if (!ad_getentryoff(ad
, ADEID_FINDERI
)
1360 || !ad_getentryoff(ad
, ADEID_COMMENT
)
1361 || !ad_getentryoff(ad
, ADEID_FILEDATESI
)
1362 || !ad_getentryoff(ad
, ADEID_AFPFILEI
)
1363 || !ad_getentryoff(ad
, ADEID_PRIVDEV
)
1364 || !ad_getentryoff(ad
, ADEID_PRIVINO
)
1365 || !ad_getentryoff(ad
, ADEID_PRIVSYN
)
1366 || !ad_getentryoff(ad
, ADEID_PRIVID
)) {
1367 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1374 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1375 smb_fname
->base_name
, rc
));
1379 if (errno
== EINVAL
) {
1381 removexattr(smb_fname
->base_name
, AFPINFO_EA_NETATALK
);
1389 static int ad_open_rsrc_xattr(const struct smb_filename
*smb_fname
,
1393 #ifdef HAVE_ATTROPEN
1394 /* FIXME: direct Solaris xattr syscall */
1395 return attropen(smb_fname
->base_name
,
1396 AFPRESOURCE_EA_NETATALK
, flags
, mode
);
1403 static int ad_open_rsrc_adouble(const struct smb_filename
*smb_fname
,
1409 struct smb_filename
*adp_smb_fname
= NULL
;
1411 ret
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
1416 fd
= open(adp_smb_fname
->base_name
, flags
, mode
);
1417 TALLOC_FREE(adp_smb_fname
);
1422 static int ad_open_rsrc(vfs_handle_struct
*handle
,
1423 const struct smb_filename
*smb_fname
,
1427 struct fruit_config_data
*config
= NULL
;
1430 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1431 struct fruit_config_data
, return -1);
1433 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
1434 fd
= ad_open_rsrc_xattr(smb_fname
, flags
, mode
);
1436 fd
= ad_open_rsrc_adouble(smb_fname
, flags
, mode
);
1443 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1444 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1445 * for file IO on the ._ file.
1447 static int ad_open(vfs_handle_struct
*handle
,
1450 const struct smb_filename
*smb_fname
,
1456 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname
->base_name
,
1457 ad
->ad_type
== ADOUBLE_META
? "meta" : "rsrc");
1459 if (ad
->ad_type
== ADOUBLE_META
) {
1463 if ((fsp
!= NULL
) && (fsp
->fh
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
1464 ad
->ad_fd
= fsp
->fh
->fd
;
1465 ad
->ad_opened
= false;
1469 fd
= ad_open_rsrc(handle
, smb_fname
, flags
, mode
);
1473 ad
->ad_opened
= true;
1476 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1477 smb_fname
->base_name
,
1478 ad
->ad_type
== ADOUBLE_META
? "meta" : "rsrc", fd
);
1483 static ssize_t
ad_read_rsrc_xattr(struct adouble
*ad
)
1488 /* FIXME: direct sys_fstat(), don't have an fsp */
1489 ret
= sys_fstat(ad
->ad_fd
, &st
,
1490 lp_fake_directory_create_times(
1491 SNUM(ad
->ad_handle
->conn
)));
1496 ad_setentrylen(ad
, ADEID_RFORK
, st
.st_ex_size
);
1497 return st
.st_ex_size
;
1500 static ssize_t
ad_read_rsrc_adouble(struct adouble
*ad
,
1501 const struct smb_filename
*smb_fname
)
1503 SMB_STRUCT_STAT sbuf
;
1510 ret
= sys_fstat(ad
->ad_fd
, &sbuf
, lp_fake_directory_create_times(
1511 SNUM(ad
->ad_handle
->conn
)));
1517 * AppleDouble file header content and size, two cases:
1519 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1520 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1522 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1524 size
= sbuf
.st_ex_size
;
1525 if (size
> talloc_array_length(ad
->ad_data
)) {
1526 if (size
> AD_XATTR_MAX_HDR_SIZE
) {
1527 size
= AD_XATTR_MAX_HDR_SIZE
;
1529 p_ad
= talloc_realloc(ad
, ad
->ad_data
, char, size
);
1536 len
= sys_pread(ad
->ad_fd
, ad
->ad_data
,
1537 talloc_array_length(ad
->ad_data
), 0);
1538 if (len
!= talloc_array_length(ad
->ad_data
)) {
1539 DBG_NOTICE("%s %s: bad size: %zd\n",
1540 smb_fname
->base_name
, strerror(errno
), len
);
1544 /* Now parse entries */
1545 ok
= ad_unpack(ad
, ADEID_NUM_DOT_UND
, sbuf
.st_ex_size
);
1547 DBG_ERR("invalid AppleDouble resource %s\n",
1548 smb_fname
->base_name
);
1553 if ((ad_getentryoff(ad
, ADEID_FINDERI
) != ADEDOFF_FINDERI_DOT_UND
)
1554 || (ad_getentrylen(ad
, ADEID_FINDERI
) < ADEDLEN_FINDERI
)
1555 || (ad_getentryoff(ad
, ADEID_RFORK
) < ADEDOFF_RFORK_DOT_UND
)) {
1556 DBG_ERR("invalid AppleDouble resource %s\n",
1557 smb_fname
->base_name
);
1563 * Try to fixup AppleDouble files created by OS X with xattrs
1564 * appended to the ADEID_FINDERI entry.
1567 ret
= ad_convert(ad
, smb_fname
);
1569 DBG_WARNING("Failed to convert [%s]\n", smb_fname
->base_name
);
1577 * Read and parse resource fork, either ._ AppleDouble file or xattr
1579 static ssize_t
ad_read_rsrc(struct adouble
*ad
,
1580 const struct smb_filename
*smb_fname
)
1582 struct fruit_config_data
*config
= NULL
;
1585 SMB_VFS_HANDLE_GET_DATA(ad
->ad_handle
, config
,
1586 struct fruit_config_data
, return -1);
1588 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
1589 len
= ad_read_rsrc_xattr(ad
);
1591 len
= ad_read_rsrc_adouble(ad
, smb_fname
);
1598 * Read and unpack an AppleDouble metadata xattr or resource
1600 static ssize_t
ad_read(struct adouble
*ad
, const struct smb_filename
*smb_fname
)
1602 switch (ad
->ad_type
) {
1604 return ad_read_meta(ad
, smb_fname
);
1606 return ad_read_rsrc(ad
, smb_fname
);
1612 static int adouble_destructor(struct adouble
*ad
)
1614 if ((ad
->ad_fd
!= -1) && ad
->ad_opened
) {
1622 * Allocate a struct adouble without initialiing it
1624 * The struct is either hang of the fsp extension context or if fsp is
1627 * @param[in] ctx talloc context
1628 * @param[in] handle vfs handle
1629 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1631 * @return adouble handle
1633 static struct adouble
*ad_alloc(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1634 adouble_type_t type
)
1639 struct fruit_config_data
*config
;
1641 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1642 struct fruit_config_data
, return NULL
);
1646 adsize
= AD_DATASZ_XATTR
;
1649 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1650 adsize
= AD_DATASZ_DOT_UND
;
1657 ad
= talloc_zero(ctx
, struct adouble
);
1664 ad
->ad_data
= talloc_zero_array(ad
, char, adsize
);
1665 if (ad
->ad_data
== NULL
) {
1671 ad
->ad_handle
= handle
;
1673 ad
->ad_magic
= AD_MAGIC
;
1674 ad
->ad_version
= AD_VERSION
;
1677 talloc_set_destructor(ad
, adouble_destructor
);
1687 * Allocate and initialize a new struct adouble
1689 * @param[in] ctx talloc context
1690 * @param[in] handle vfs handle
1691 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1693 * @return adouble handle, initialized
1695 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1696 adouble_type_t type
)
1699 const struct ad_entry_order
*eid
;
1700 struct adouble
*ad
= NULL
;
1701 struct fruit_config_data
*config
;
1702 time_t t
= time(NULL
);
1704 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1705 struct fruit_config_data
, return NULL
);
1709 eid
= entry_order_meta_xattr
;
1712 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1713 eid
= entry_order_dot_und
;
1715 eid
= entry_order_rsrc_xattr
;
1722 ad
= ad_alloc(ctx
, handle
, type
);
1728 ad
->ad_eid
[eid
->id
].ade_off
= eid
->offset
;
1729 ad
->ad_eid
[eid
->id
].ade_len
= eid
->len
;
1733 /* put something sane in the date fields */
1734 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
, t
);
1735 ad_setdate(ad
, AD_DATE_MODIFY
| AD_DATE_UNIX
, t
);
1736 ad_setdate(ad
, AD_DATE_ACCESS
| AD_DATE_UNIX
, t
);
1737 ad_setdate(ad
, AD_DATE_BACKUP
, htonl(AD_DATE_START
));
1745 static struct adouble
*ad_get_internal(TALLOC_CTX
*ctx
,
1746 vfs_handle_struct
*handle
,
1748 const struct smb_filename
*smb_fname
,
1749 adouble_type_t type
)
1753 struct adouble
*ad
= NULL
;
1757 smb_fname
= fsp
->base_fsp
->fsp_name
;
1760 DEBUG(10, ("ad_get(%s) called for %s\n",
1761 type
== ADOUBLE_META
? "meta" : "rsrc",
1762 smb_fname
->base_name
));
1764 ad
= ad_alloc(ctx
, handle
, type
);
1770 /* Try rw first so we can use the fd in ad_convert() */
1773 rc
= ad_open(handle
, ad
, fsp
, smb_fname
, mode
, 0);
1774 if (rc
== -1 && ((errno
== EROFS
) || (errno
== EACCES
))) {
1776 rc
= ad_open(handle
, ad
, fsp
, smb_fname
, mode
, 0);
1779 DBG_DEBUG("ad_open [%s] error [%s]\n",
1780 smb_fname
->base_name
, strerror(errno
));
1785 len
= ad_read(ad
, smb_fname
);
1787 DEBUG(10, ("error reading AppleDouble for %s\n",
1788 smb_fname
->base_name
));
1794 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1795 type
== ADOUBLE_META
? "meta" : "rsrc",
1796 smb_fname
->base_name
, rc
));
1805 * Return AppleDouble data for a file
1807 * @param[in] ctx talloc context
1808 * @param[in] handle vfs handle
1809 * @param[in] smb_fname pathname to file or directory
1810 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1812 * @return talloced struct adouble or NULL on error
1814 static struct adouble
*ad_get(TALLOC_CTX
*ctx
,
1815 vfs_handle_struct
*handle
,
1816 const struct smb_filename
*smb_fname
,
1817 adouble_type_t type
)
1819 return ad_get_internal(ctx
, handle
, NULL
, smb_fname
, type
);
1823 * Return AppleDouble data for a file
1825 * @param[in] ctx talloc context
1826 * @param[in] handle vfs handle
1827 * @param[in] fsp fsp to use for IO
1828 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1830 * @return talloced struct adouble or NULL on error
1832 static struct adouble
*ad_fget(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1833 files_struct
*fsp
, adouble_type_t type
)
1835 return ad_get_internal(ctx
, handle
, fsp
, NULL
, type
);
1839 * Set AppleDouble metadata on a file or directory
1841 * @param[in] ad adouble handle
1843 * @param[in] smb_fname pathname to file or directory
1845 * @return status code, 0 means success
1847 static int ad_set(struct adouble
*ad
, const struct smb_filename
*smb_fname
)
1852 DBG_DEBUG("Path [%s]\n", smb_fname
->base_name
);
1854 if (ad
->ad_type
!= ADOUBLE_META
) {
1855 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1856 smb_fname
->base_name
);
1865 ret
= SMB_VFS_SETXATTR(ad
->ad_handle
->conn
,
1867 AFPINFO_EA_NETATALK
,
1869 AD_DATASZ_XATTR
, 0);
1871 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname
->base_name
, ret
);
1877 * Set AppleDouble metadata on a file or directory
1879 * @param[in] ad adouble handle
1880 * @param[in] fsp file handle
1882 * @return status code, 0 means success
1884 static int ad_fset(struct adouble
*ad
, files_struct
*fsp
)
1890 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
1893 || (fsp
->fh
== NULL
)
1894 || (fsp
->fh
->fd
== -1))
1896 smb_panic("bad fsp");
1904 switch (ad
->ad_type
) {
1906 rc
= SMB_VFS_NEXT_SETXATTR(ad
->ad_handle
,
1908 AFPINFO_EA_NETATALK
,
1910 AD_DATASZ_XATTR
, 0);
1914 len
= SMB_VFS_NEXT_PWRITE(ad
->ad_handle
,
1919 if (len
!= AD_DATASZ_DOT_UND
) {
1920 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp
), len
);
1930 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp
), rc
);
1935 /*****************************************************************************
1937 *****************************************************************************/
1939 static bool is_afpinfo_stream(const struct smb_filename
*smb_fname
)
1941 if (strncasecmp_m(smb_fname
->stream_name
,
1942 AFPINFO_STREAM_NAME
,
1943 strlen(AFPINFO_STREAM_NAME
)) == 0) {
1949 static bool is_afpresource_stream(const struct smb_filename
*smb_fname
)
1951 if (strncasecmp_m(smb_fname
->stream_name
,
1952 AFPRESOURCE_STREAM_NAME
,
1953 strlen(AFPRESOURCE_STREAM_NAME
)) == 0) {
1960 * Test whether stream is an Apple stream, not used atm
1963 static bool is_apple_stream(const struct smb_filename
*smb_fname
)
1965 if (is_afpinfo_stream(smb_fname
)) {
1968 if (is_afpresource_stream(smb_fname
)) {
1976 * Initialize config struct from our smb.conf config parameters
1978 static int init_fruit_config(vfs_handle_struct
*handle
)
1980 struct fruit_config_data
*config
;
1982 const char *tm_size_str
= NULL
;
1984 config
= talloc_zero(handle
->conn
, struct fruit_config_data
);
1986 DEBUG(1, ("talloc_zero() failed\n"));
1992 * Versions up to Samba 4.5.x had a spelling bug in the
1993 * fruit:resource option calling lp_parm_enum with
1994 * "res*s*ource" (ie two s).
1996 * In Samba 4.6 we accept both the wrong and the correct
1997 * spelling, in Samba 4.7 the bad spelling will be removed.
1999 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
2000 "ressource", fruit_rsrc
, FRUIT_RSRC_ADFILE
);
2001 if (enumval
== -1) {
2002 DEBUG(1, ("value for %s: resource type unknown\n",
2003 FRUIT_PARAM_TYPE_NAME
));
2006 config
->rsrc
= (enum fruit_rsrc
)enumval
;
2008 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
2009 "resource", fruit_rsrc
, enumval
);
2010 if (enumval
== -1) {
2011 DEBUG(1, ("value for %s: resource type unknown\n",
2012 FRUIT_PARAM_TYPE_NAME
));
2015 config
->rsrc
= (enum fruit_rsrc
)enumval
;
2017 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
2018 "metadata", fruit_meta
, FRUIT_META_NETATALK
);
2019 if (enumval
== -1) {
2020 DEBUG(1, ("value for %s: metadata type unknown\n",
2021 FRUIT_PARAM_TYPE_NAME
));
2024 config
->meta
= (enum fruit_meta
)enumval
;
2026 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
2027 "locking", fruit_locking
, FRUIT_LOCKING_NONE
);
2028 if (enumval
== -1) {
2029 DEBUG(1, ("value for %s: locking type unknown\n",
2030 FRUIT_PARAM_TYPE_NAME
));
2033 config
->locking
= (enum fruit_locking
)enumval
;
2035 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
2036 "encoding", fruit_encoding
, FRUIT_ENC_PRIVATE
);
2037 if (enumval
== -1) {
2038 DEBUG(1, ("value for %s: encoding type unknown\n",
2039 FRUIT_PARAM_TYPE_NAME
));
2042 config
->encoding
= (enum fruit_encoding
)enumval
;
2044 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
2045 config
->veto_appledouble
= lp_parm_bool(SNUM(handle
->conn
),
2046 FRUIT_PARAM_TYPE_NAME
,
2051 config
->use_aapl
= lp_parm_bool(
2052 -1, FRUIT_PARAM_TYPE_NAME
, "aapl", true);
2054 config
->time_machine
= lp_parm_bool(
2055 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
, "time machine", false);
2057 config
->unix_info_enabled
= lp_parm_bool(
2058 -1, FRUIT_PARAM_TYPE_NAME
, "nfs_aces", true);
2060 config
->use_copyfile
= lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
,
2063 config
->posix_rename
= lp_parm_bool(
2064 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
, "posix_rename", true);
2066 config
->aapl_zero_file_id
=
2067 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
, "zero_file_id", true);
2069 config
->readdir_attr_rsize
= lp_parm_bool(
2070 SNUM(handle
->conn
), "readdir_attr", "aapl_rsize", true);
2072 config
->readdir_attr_finder_info
= lp_parm_bool(
2073 SNUM(handle
->conn
), "readdir_attr", "aapl_finder_info", true);
2075 config
->readdir_attr_max_access
= lp_parm_bool(
2076 SNUM(handle
->conn
), "readdir_attr", "aapl_max_access", true);
2078 config
->model
= lp_parm_const_string(
2079 -1, FRUIT_PARAM_TYPE_NAME
, "model", "MacSamba");
2081 tm_size_str
= lp_parm_const_string(
2082 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
2083 "time machine max size", NULL
);
2084 if (tm_size_str
!= NULL
) {
2085 config
->time_machine_max_size
= conv_str_size(tm_size_str
);
2088 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
2089 NULL
, struct fruit_config_data
,
2096 * Prepend "._" to a basename
2097 * Return a new struct smb_filename with stream_name == NULL.
2099 static int adouble_path(TALLOC_CTX
*ctx
,
2100 const struct smb_filename
*smb_fname_in
,
2101 struct smb_filename
**pp_smb_fname_out
)
2105 struct smb_filename
*smb_fname
= cp_smb_filename(ctx
,
2108 if (smb_fname
== NULL
) {
2112 /* We need streamname to be NULL */
2113 TALLOC_FREE(smb_fname
->stream_name
);
2115 /* And we're replacing base_name. */
2116 TALLOC_FREE(smb_fname
->base_name
);
2118 if (!parent_dirname(smb_fname
, smb_fname_in
->base_name
,
2120 TALLOC_FREE(smb_fname
);
2124 smb_fname
->base_name
= talloc_asprintf(smb_fname
,
2125 "%s/._%s", parent
, base
);
2126 if (smb_fname
->base_name
== NULL
) {
2127 TALLOC_FREE(smb_fname
);
2131 *pp_smb_fname_out
= smb_fname
;
2137 * Allocate and initialize an AfpInfo struct
2139 static AfpInfo
*afpinfo_new(TALLOC_CTX
*ctx
)
2141 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
2145 ai
->afpi_Signature
= AFP_Signature
;
2146 ai
->afpi_Version
= AFP_Version
;
2147 ai
->afpi_BackupTime
= AD_DATE_START
;
2152 * Pack an AfpInfo struct into a buffer
2154 * Buffer size must be at least AFP_INFO_SIZE
2155 * Returns size of packed buffer
2157 static ssize_t
afpinfo_pack(const AfpInfo
*ai
, char *buf
)
2159 memset(buf
, 0, AFP_INFO_SIZE
);
2161 RSIVAL(buf
, 0, ai
->afpi_Signature
);
2162 RSIVAL(buf
, 4, ai
->afpi_Version
);
2163 RSIVAL(buf
, 12, ai
->afpi_BackupTime
);
2164 memcpy(buf
+ 16, ai
->afpi_FinderInfo
, sizeof(ai
->afpi_FinderInfo
));
2166 return AFP_INFO_SIZE
;
2170 * Unpack a buffer into a AfpInfo structure
2172 * Buffer size must be at least AFP_INFO_SIZE
2173 * Returns allocated AfpInfo struct
2175 static AfpInfo
*afpinfo_unpack(TALLOC_CTX
*ctx
, const void *data
)
2177 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
2182 ai
->afpi_Signature
= RIVAL(data
, 0);
2183 ai
->afpi_Version
= RIVAL(data
, 4);
2184 ai
->afpi_BackupTime
= RIVAL(data
, 12);
2185 memcpy(ai
->afpi_FinderInfo
, (const char *)data
+ 16,
2186 sizeof(ai
->afpi_FinderInfo
));
2188 if (ai
->afpi_Signature
!= AFP_Signature
2189 || ai
->afpi_Version
!= AFP_Version
) {
2190 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2198 * Fake an inode number from the md5 hash of the (xattr) name
2200 static SMB_INO_T
fruit_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
2203 unsigned char hash
[16];
2207 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
2208 SMB_ASSERT(upper_sname
!= NULL
);
2211 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_dev
),
2212 sizeof(sbuf
->st_ex_dev
));
2213 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_ino
),
2214 sizeof(sbuf
->st_ex_ino
));
2215 MD5Update(&ctx
, (unsigned char *)upper_sname
,
2216 talloc_get_size(upper_sname
)-1);
2217 MD5Final(hash
, &ctx
);
2219 TALLOC_FREE(upper_sname
);
2221 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2222 memcpy(&result
, hash
, sizeof(result
));
2224 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2225 sname
, (unsigned long long)result
));
2230 static bool add_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
2231 struct stream_struct
**streams
,
2232 const char *name
, off_t size
,
2235 struct stream_struct
*tmp
;
2237 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
2243 tmp
[*num_streams
].name
= talloc_asprintf(tmp
, "%s:$DATA", name
);
2244 if (tmp
[*num_streams
].name
== NULL
) {
2248 tmp
[*num_streams
].size
= size
;
2249 tmp
[*num_streams
].alloc_size
= alloc_size
;
2256 static bool filter_empty_rsrc_stream(unsigned int *num_streams
,
2257 struct stream_struct
**streams
)
2259 struct stream_struct
*tmp
= *streams
;
2262 if (*num_streams
== 0) {
2266 for (i
= 0; i
< *num_streams
; i
++) {
2267 if (strequal_m(tmp
[i
].name
, AFPRESOURCE_STREAM
)) {
2272 if (i
== *num_streams
) {
2276 if (tmp
[i
].size
> 0) {
2280 TALLOC_FREE(tmp
[i
].name
);
2281 if (*num_streams
- 1 > i
) {
2282 memmove(&tmp
[i
], &tmp
[i
+1],
2283 (*num_streams
- i
- 1) * sizeof(struct stream_struct
));
2290 static bool del_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
2291 struct stream_struct
**streams
,
2294 struct stream_struct
*tmp
= *streams
;
2297 if (*num_streams
== 0) {
2301 for (i
= 0; i
< *num_streams
; i
++) {
2302 if (strequal_m(tmp
[i
].name
, name
)) {
2307 if (i
== *num_streams
) {
2311 TALLOC_FREE(tmp
[i
].name
);
2312 if (*num_streams
- 1 > i
) {
2313 memmove(&tmp
[i
], &tmp
[i
+1],
2314 (*num_streams
- i
- 1) * sizeof(struct stream_struct
));
2321 static bool ad_empty_finderinfo(const struct adouble
*ad
)
2324 char emptybuf
[ADEDLEN_FINDERI
] = {0};
2327 fi
= ad_get_entry(ad
, ADEID_FINDERI
);
2329 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad
);
2333 cmp
= memcmp(emptybuf
, fi
, ADEDLEN_FINDERI
);
2337 static bool ai_empty_finderinfo(const AfpInfo
*ai
)
2340 char emptybuf
[ADEDLEN_FINDERI
] = {0};
2342 cmp
= memcmp(emptybuf
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
2347 * Update btime with btime from Netatalk
2349 static void update_btime(vfs_handle_struct
*handle
,
2350 struct smb_filename
*smb_fname
)
2353 struct timespec creation_time
= {0};
2355 struct fruit_config_data
*config
= NULL
;
2357 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
2360 switch (config
->meta
) {
2361 case FRUIT_META_STREAM
:
2363 case FRUIT_META_NETATALK
:
2367 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
2371 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
2375 if (ad_getdate(ad
, AD_DATE_UNIX
| AD_DATE_CREATE
, &t
) != 0) {
2381 creation_time
.tv_sec
= convert_uint32_t_to_time_t(t
);
2382 update_stat_ex_create_time(&smb_fname
->st
, creation_time
);
2388 * Map an access mask to a Netatalk single byte byte range lock
2390 static off_t
access_to_netatalk_brl(enum apple_fork fork_type
,
2391 uint32_t access_mask
)
2395 switch (access_mask
) {
2396 case FILE_READ_DATA
:
2397 offset
= AD_FILELOCK_OPEN_RD
;
2400 case FILE_WRITE_DATA
:
2401 case FILE_APPEND_DATA
:
2402 offset
= AD_FILELOCK_OPEN_WR
;
2406 offset
= AD_FILELOCK_OPEN_NONE
;
2410 if (fork_type
== APPLE_FORK_RSRC
) {
2411 if (offset
== AD_FILELOCK_OPEN_NONE
) {
2412 offset
= AD_FILELOCK_RSRC_OPEN_NONE
;
2422 * Map a deny mode to a Netatalk brl
2424 static off_t
denymode_to_netatalk_brl(enum apple_fork fork_type
,
2429 switch (deny_mode
) {
2431 offset
= AD_FILELOCK_DENY_RD
;
2435 offset
= AD_FILELOCK_DENY_WR
;
2439 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2442 if (fork_type
== APPLE_FORK_RSRC
) {
2450 * Call fcntl() with an exclusive F_GETLK request in order to
2451 * determine if there's an exisiting shared lock
2453 * @return true if the requested lock was found or any error occurred
2454 * false if the lock was not found
2456 static bool test_netatalk_lock(files_struct
*fsp
, off_t in_offset
)
2459 off_t offset
= in_offset
;
2464 result
= SMB_VFS_GETLOCK(fsp
, &offset
, &len
, &type
, &pid
);
2465 if (result
== false) {
2469 if (type
!= F_UNLCK
) {
2476 static NTSTATUS
fruit_check_access(vfs_handle_struct
*handle
,
2478 uint32_t access_mask
,
2481 NTSTATUS status
= NT_STATUS_OK
;
2482 bool open_for_reading
, open_for_writing
, deny_read
, deny_write
;
2484 bool have_read
= false;
2487 /* FIXME: hardcoded data fork, add resource fork */
2488 enum apple_fork fork_type
= APPLE_FORK_DATA
;
2490 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2492 access_mask
& FILE_READ_DATA
? "READ" :"-",
2493 access_mask
& FILE_WRITE_DATA
? "WRITE" : "-",
2494 deny_mode
& DENY_READ
? "DENY_READ" : "-",
2495 deny_mode
& DENY_WRITE
? "DENY_WRITE" : "-"));
2497 if (fsp
->fh
->fd
== -1) {
2498 return NT_STATUS_OK
;
2501 flags
= fcntl(fsp
->fh
->fd
, F_GETFL
);
2503 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2504 fsp_str_dbg(fsp
), fsp
->fh
->fd
, strerror(errno
));
2505 return map_nt_error_from_unix(errno
);
2508 if (flags
& (O_RDONLY
|O_RDWR
)) {
2510 * Applying fcntl read locks requires an fd opened for
2511 * reading. This means we won't be applying locks for
2512 * files openend write-only, but what can we do...
2518 * Check read access and deny read mode
2520 if ((access_mask
& FILE_READ_DATA
) || (deny_mode
& DENY_READ
)) {
2522 open_for_reading
= test_netatalk_lock(
2523 fsp
, access_to_netatalk_brl(fork_type
, FILE_READ_DATA
));
2525 deny_read
= test_netatalk_lock(
2526 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_READ
));
2528 DEBUG(10, ("read: %s, deny_write: %s\n",
2529 open_for_reading
== true ? "yes" : "no",
2530 deny_read
== true ? "yes" : "no"));
2532 if (((access_mask
& FILE_READ_DATA
) && deny_read
)
2533 || ((deny_mode
& DENY_READ
) && open_for_reading
)) {
2534 return NT_STATUS_SHARING_VIOLATION
;
2538 if ((access_mask
& FILE_READ_DATA
) && have_read
) {
2539 struct byte_range_lock
*br_lck
= NULL
;
2541 off
= access_to_netatalk_brl(fork_type
, FILE_READ_DATA
);
2543 handle
->conn
->sconn
->msg_ctx
, fsp
,
2544 fsp
->op
->global
->open_persistent_id
, 1, off
,
2545 READ_LOCK
, POSIX_LOCK
, false,
2548 TALLOC_FREE(br_lck
);
2550 if (!NT_STATUS_IS_OK(status
)) {
2555 if ((deny_mode
& DENY_READ
) && have_read
) {
2556 struct byte_range_lock
*br_lck
= NULL
;
2558 off
= denymode_to_netatalk_brl(fork_type
, DENY_READ
);
2560 handle
->conn
->sconn
->msg_ctx
, fsp
,
2561 fsp
->op
->global
->open_persistent_id
, 1, off
,
2562 READ_LOCK
, POSIX_LOCK
, false,
2565 TALLOC_FREE(br_lck
);
2567 if (!NT_STATUS_IS_OK(status
)) {
2574 * Check write access and deny write mode
2576 if ((access_mask
& FILE_WRITE_DATA
) || (deny_mode
& DENY_WRITE
)) {
2578 open_for_writing
= test_netatalk_lock(
2579 fsp
, access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
));
2581 deny_write
= test_netatalk_lock(
2582 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_WRITE
));
2584 DEBUG(10, ("write: %s, deny_write: %s\n",
2585 open_for_writing
== true ? "yes" : "no",
2586 deny_write
== true ? "yes" : "no"));
2588 if (((access_mask
& FILE_WRITE_DATA
) && deny_write
)
2589 || ((deny_mode
& DENY_WRITE
) && open_for_writing
)) {
2590 return NT_STATUS_SHARING_VIOLATION
;
2594 if ((access_mask
& FILE_WRITE_DATA
) && have_read
) {
2595 struct byte_range_lock
*br_lck
= NULL
;
2597 off
= access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
);
2599 handle
->conn
->sconn
->msg_ctx
, fsp
,
2600 fsp
->op
->global
->open_persistent_id
, 1, off
,
2601 READ_LOCK
, POSIX_LOCK
, false,
2604 TALLOC_FREE(br_lck
);
2606 if (!NT_STATUS_IS_OK(status
)) {
2610 if ((deny_mode
& DENY_WRITE
) && have_read
) {
2611 struct byte_range_lock
*br_lck
= NULL
;
2613 off
= denymode_to_netatalk_brl(fork_type
, DENY_WRITE
);
2615 handle
->conn
->sconn
->msg_ctx
, fsp
,
2616 fsp
->op
->global
->open_persistent_id
, 1, off
,
2617 READ_LOCK
, POSIX_LOCK
, false,
2620 TALLOC_FREE(br_lck
);
2622 if (!NT_STATUS_IS_OK(status
)) {
2631 static NTSTATUS
check_aapl(vfs_handle_struct
*handle
,
2632 struct smb_request
*req
,
2633 const struct smb2_create_blobs
*in_context_blobs
,
2634 struct smb2_create_blobs
*out_context_blobs
)
2636 struct fruit_config_data
*config
;
2638 struct smb2_create_blob
*aapl
= NULL
;
2642 DATA_BLOB blob
= data_blob_talloc(req
, NULL
, 0);
2643 uint64_t req_bitmap
, client_caps
;
2644 uint64_t server_caps
= SMB2_CRTCTX_AAPL_UNIX_BASED
;
2648 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
2649 return NT_STATUS_UNSUCCESSFUL
);
2651 if (!config
->use_aapl
2652 || in_context_blobs
== NULL
2653 || out_context_blobs
== NULL
) {
2654 return NT_STATUS_OK
;
2657 aapl
= smb2_create_blob_find(in_context_blobs
,
2658 SMB2_CREATE_TAG_AAPL
);
2660 return NT_STATUS_OK
;
2663 if (aapl
->data
.length
!= 24) {
2664 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2665 (uintmax_t)aapl
->data
.length
));
2666 return NT_STATUS_INVALID_PARAMETER
;
2669 cmd
= IVAL(aapl
->data
.data
, 0);
2670 if (cmd
!= SMB2_CRTCTX_AAPL_SERVER_QUERY
) {
2671 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd
));
2672 return NT_STATUS_INVALID_PARAMETER
;
2675 req_bitmap
= BVAL(aapl
->data
.data
, 8);
2676 client_caps
= BVAL(aapl
->data
.data
, 16);
2678 SIVAL(p
, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY
);
2680 SBVAL(p
, 8, req_bitmap
);
2681 ok
= data_blob_append(req
, &blob
, p
, 16);
2683 return NT_STATUS_UNSUCCESSFUL
;
2686 if (req_bitmap
& SMB2_CRTCTX_AAPL_SERVER_CAPS
) {
2687 if ((client_caps
& SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
) &&
2688 (handle
->conn
->tcon
->compat
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
2689 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
;
2690 config
->readdir_attr_enabled
= true;
2693 if (config
->use_copyfile
) {
2694 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE
;
2695 config
->copyfile_enabled
= true;
2699 * The client doesn't set the flag, so we can't check
2700 * for it and just set it unconditionally
2702 if (config
->unix_info_enabled
) {
2703 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE
;
2706 SBVAL(p
, 0, server_caps
);
2707 ok
= data_blob_append(req
, &blob
, p
, 8);
2709 return NT_STATUS_UNSUCCESSFUL
;
2713 if (req_bitmap
& SMB2_CRTCTX_AAPL_VOLUME_CAPS
) {
2714 int val
= lp_case_sensitive(SNUM(handle
->conn
->tcon
->compat
));
2722 caps
|= SMB2_CRTCTX_AAPL_CASE_SENSITIVE
;
2729 if (config
->time_machine
) {
2730 caps
|= SMB2_CRTCTX_AAPL_FULL_SYNC
;
2735 ok
= data_blob_append(req
, &blob
, p
, 8);
2737 return NT_STATUS_UNSUCCESSFUL
;
2741 if (req_bitmap
& SMB2_CRTCTX_AAPL_MODEL_INFO
) {
2742 ok
= convert_string_talloc(req
,
2743 CH_UNIX
, CH_UTF16LE
,
2744 config
->model
, strlen(config
->model
),
2747 return NT_STATUS_UNSUCCESSFUL
;
2751 SIVAL(p
+ 4, 0, modellen
);
2752 ok
= data_blob_append(req
, &blob
, p
, 8);
2755 return NT_STATUS_UNSUCCESSFUL
;
2758 ok
= data_blob_append(req
, &blob
, model
, modellen
);
2761 return NT_STATUS_UNSUCCESSFUL
;
2765 status
= smb2_create_blob_add(out_context_blobs
,
2767 SMB2_CREATE_TAG_AAPL
,
2769 if (NT_STATUS_IS_OK(status
)) {
2770 global_fruit_config
.nego_aapl
= true;
2771 if (config
->aapl_zero_file_id
) {
2772 aapl_force_zero_file_id(handle
->conn
->sconn
);
2779 static bool readdir_attr_meta_finderi_stream(
2780 struct vfs_handle_struct
*handle
,
2781 const struct smb_filename
*smb_fname
,
2784 struct smb_filename
*stream_name
= NULL
;
2785 files_struct
*fsp
= NULL
;
2790 uint8_t buf
[AFP_INFO_SIZE
];
2792 stream_name
= synthetic_smb_fname(talloc_tos(),
2793 smb_fname
->base_name
,
2794 AFPINFO_STREAM_NAME
,
2795 NULL
, smb_fname
->flags
);
2796 if (stream_name
== NULL
) {
2800 ret
= SMB_VFS_STAT(handle
->conn
, stream_name
);
2805 status
= SMB_VFS_CREATE_FILE(
2806 handle
->conn
, /* conn */
2808 0, /* root_dir_fid */
2809 stream_name
, /* fname */
2810 FILE_READ_DATA
, /* access_mask */
2811 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2813 FILE_OPEN
, /* create_disposition*/
2814 0, /* create_options */
2815 0, /* file_attributes */
2816 INTERNAL_OPEN_ONLY
, /* oplock_request */
2818 0, /* allocation_size */
2819 0, /* private_flags */
2824 NULL
, NULL
); /* create context */
2826 TALLOC_FREE(stream_name
);
2828 if (!NT_STATUS_IS_OK(status
)) {
2832 nread
= SMB_VFS_PREAD(fsp
, &buf
[0], AFP_INFO_SIZE
, 0);
2833 if (nread
!= AFP_INFO_SIZE
) {
2834 DBG_ERR("short read [%s] [%zd/%d]\n",
2835 smb_fname_str_dbg(stream_name
), nread
, AFP_INFO_SIZE
);
2840 memcpy(&ai
->afpi_FinderInfo
[0], &buf
[AFP_OFF_FinderInfo
],
2847 close_file(NULL
, fsp
, NORMAL_CLOSE
);
2853 static bool readdir_attr_meta_finderi_netatalk(
2854 struct vfs_handle_struct
*handle
,
2855 const struct smb_filename
*smb_fname
,
2858 struct adouble
*ad
= NULL
;
2861 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
2866 p
= ad_get_entry(ad
, ADEID_FINDERI
);
2868 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname
->base_name
);
2873 memcpy(&ai
->afpi_FinderInfo
[0], p
, AFP_FinderSize
);
2878 static bool readdir_attr_meta_finderi(struct vfs_handle_struct
*handle
,
2879 const struct smb_filename
*smb_fname
,
2880 struct readdir_attr_data
*attr_data
)
2882 struct fruit_config_data
*config
= NULL
;
2883 uint32_t date_added
;
2887 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2888 struct fruit_config_data
,
2891 switch (config
->meta
) {
2892 case FRUIT_META_NETATALK
:
2893 ok
= readdir_attr_meta_finderi_netatalk(
2894 handle
, smb_fname
, &ai
);
2897 case FRUIT_META_STREAM
:
2898 ok
= readdir_attr_meta_finderi_stream(
2899 handle
, smb_fname
, &ai
);
2903 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
2908 /* Don't bother with errors, it's likely ENOENT */
2912 if (S_ISREG(smb_fname
->st
.st_ex_mode
)) {
2914 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0],
2915 &ai
.afpi_FinderInfo
[0], 4);
2917 /* finder_creator */
2918 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 4,
2919 &ai
.afpi_FinderInfo
[4], 4);
2923 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 8,
2924 &ai
.afpi_FinderInfo
[8], 2);
2926 /* finder_ext_flags */
2927 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 10,
2928 &ai
.afpi_FinderInfo
[24], 2);
2931 date_added
= convert_time_t_to_uint32_t(
2932 smb_fname
->st
.st_ex_btime
.tv_sec
- AD_DATE_DELTA
);
2934 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
[0], 12, date_added
);
2939 static uint64_t readdir_attr_rfork_size_adouble(
2940 struct vfs_handle_struct
*handle
,
2941 const struct smb_filename
*smb_fname
)
2943 struct adouble
*ad
= NULL
;
2944 uint64_t rfork_size
;
2946 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
2952 rfork_size
= ad_getentrylen(ad
, ADEID_RFORK
);
2958 static uint64_t readdir_attr_rfork_size_stream(
2959 struct vfs_handle_struct
*handle
,
2960 const struct smb_filename
*smb_fname
)
2962 struct smb_filename
*stream_name
= NULL
;
2964 uint64_t rfork_size
;
2966 stream_name
= synthetic_smb_fname(talloc_tos(),
2967 smb_fname
->base_name
,
2968 AFPRESOURCE_STREAM_NAME
,
2970 if (stream_name
== NULL
) {
2974 ret
= SMB_VFS_STAT(handle
->conn
, stream_name
);
2976 TALLOC_FREE(stream_name
);
2980 rfork_size
= stream_name
->st
.st_ex_size
;
2981 TALLOC_FREE(stream_name
);
2986 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct
*handle
,
2987 const struct smb_filename
*smb_fname
)
2989 struct fruit_config_data
*config
= NULL
;
2990 uint64_t rfork_size
;
2992 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2993 struct fruit_config_data
,
2996 switch (config
->rsrc
) {
2997 case FRUIT_RSRC_ADFILE
:
2998 case FRUIT_RSRC_XATTR
:
2999 rfork_size
= readdir_attr_rfork_size_adouble(handle
,
3003 case FRUIT_META_STREAM
:
3004 rfork_size
= readdir_attr_rfork_size_stream(handle
,
3009 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
3017 static NTSTATUS
readdir_attr_macmeta(struct vfs_handle_struct
*handle
,
3018 const struct smb_filename
*smb_fname
,
3019 struct readdir_attr_data
*attr_data
)
3021 NTSTATUS status
= NT_STATUS_OK
;
3022 struct fruit_config_data
*config
= NULL
;
3025 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3026 struct fruit_config_data
,
3027 return NT_STATUS_UNSUCCESSFUL
);
3030 /* Ensure we return a default value in the creation_date field */
3031 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
, 12, AD_DATE_START
);
3034 * Resource fork length
3037 if (config
->readdir_attr_rsize
) {
3038 uint64_t rfork_size
;
3040 rfork_size
= readdir_attr_rfork_size(handle
, smb_fname
);
3041 attr_data
->attr_data
.aapl
.rfork_size
= rfork_size
;
3048 if (config
->readdir_attr_finder_info
) {
3049 ok
= readdir_attr_meta_finderi(handle
, smb_fname
, attr_data
);
3051 status
= NT_STATUS_INTERNAL_ERROR
;
3058 static NTSTATUS
remove_virtual_nfs_aces(struct security_descriptor
*psd
)
3063 if (psd
->dacl
== NULL
) {
3064 return NT_STATUS_OK
;
3067 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
3068 /* MS NFS style mode/uid/gid */
3069 int cmp
= dom_sid_compare_domain(
3070 &global_sid_Unix_NFS
,
3071 &psd
->dacl
->aces
[i
].trustee
);
3073 /* Normal ACE entry. */
3078 * security_descriptor_dacl_del()
3079 * *must* return NT_STATUS_OK as we know
3080 * we have something to remove.
3083 status
= security_descriptor_dacl_del(psd
,
3084 &psd
->dacl
->aces
[i
].trustee
);
3085 if (!NT_STATUS_IS_OK(status
)) {
3086 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3092 * security_descriptor_dacl_del() may delete more
3093 * then one entry subsequent to this one if the
3094 * SID matches, but we only need to ensure that
3095 * we stay looking at the same element in the array.
3099 return NT_STATUS_OK
;
3102 /* Search MS NFS style ACE with UNIX mode */
3103 static NTSTATUS
check_ms_nfs(vfs_handle_struct
*handle
,
3105 struct security_descriptor
*psd
,
3110 struct fruit_config_data
*config
= NULL
;
3114 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3115 struct fruit_config_data
,
3116 return NT_STATUS_UNSUCCESSFUL
);
3118 if (!global_fruit_config
.nego_aapl
) {
3119 return NT_STATUS_OK
;
3121 if (psd
->dacl
== NULL
|| !config
->unix_info_enabled
) {
3122 return NT_STATUS_OK
;
3125 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
3126 if (dom_sid_compare_domain(
3127 &global_sid_Unix_NFS_Mode
,
3128 &psd
->dacl
->aces
[i
].trustee
) == 0) {
3129 *pmode
= (mode_t
)psd
->dacl
->aces
[i
].trustee
.sub_auths
[2];
3130 *pmode
&= (S_IRWXU
| S_IRWXG
| S_IRWXO
);
3133 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3134 fsp_str_dbg(fsp
), (unsigned)(*pmode
)));
3140 * Remove any incoming virtual ACE entries generated by
3141 * fruit_fget_nt_acl().
3144 return remove_virtual_nfs_aces(psd
);
3147 /****************************************************************************
3149 ****************************************************************************/
3151 static int fruit_connect(vfs_handle_struct
*handle
,
3152 const char *service
,
3156 char *list
= NULL
, *newlist
= NULL
;
3157 struct fruit_config_data
*config
;
3159 DEBUG(10, ("fruit_connect\n"));
3161 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
3166 rc
= init_fruit_config(handle
);
3171 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3172 struct fruit_config_data
, return -1);
3174 if (config
->veto_appledouble
) {
3175 list
= lp_veto_files(talloc_tos(), SNUM(handle
->conn
));
3178 if (strstr(list
, "/" ADOUBLE_NAME_PREFIX
"*/") == NULL
) {
3179 newlist
= talloc_asprintf(
3181 "%s/" ADOUBLE_NAME_PREFIX
"*/",
3183 lp_do_parameter(SNUM(handle
->conn
),
3188 lp_do_parameter(SNUM(handle
->conn
),
3190 "/" ADOUBLE_NAME_PREFIX
"*/");
3196 if (config
->encoding
== FRUIT_ENC_NATIVE
) {
3197 lp_do_parameter(SNUM(handle
->conn
),
3202 if (config
->time_machine
) {
3203 DBG_NOTICE("Enabling durable handles for Time Machine "
3204 "support on [%s]\n", service
);
3205 lp_do_parameter(SNUM(handle
->conn
), "durable handles", "yes");
3206 lp_do_parameter(SNUM(handle
->conn
), "kernel oplocks", "no");
3207 lp_do_parameter(SNUM(handle
->conn
), "kernel share modes", "no");
3208 if (!lp_strict_sync(SNUM(handle
->conn
))) {
3209 DBG_WARNING("Time Machine without strict sync is not "
3212 lp_do_parameter(SNUM(handle
->conn
), "posix locking", "no");
3218 static int fruit_open_meta_stream(vfs_handle_struct
*handle
,
3219 struct smb_filename
*smb_fname
,
3225 char afpinfo_buf
[AFP_INFO_SIZE
];
3226 ssize_t len
, written
;
3230 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3235 if (!(flags
& (O_CREAT
| O_TRUNC
))) {
3239 ai
= afpinfo_new(talloc_tos());
3245 len
= afpinfo_pack(ai
, afpinfo_buf
);
3246 if (len
!= AFP_INFO_SIZE
) {
3251 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3252 fsp
->fh
->fd
= hostfd
;
3254 written
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, afpinfo_buf
,
3257 if (written
!= AFP_INFO_SIZE
) {
3258 DBG_ERR("bad write [%zd/%d]\n", written
, AFP_INFO_SIZE
);
3266 DBG_DEBUG("rc=%d, fd=%d\n", rc
, hostfd
);
3269 int saved_errno
= errno
;
3271 fsp
->fh
->fd
= hostfd
;
3272 SMB_VFS_NEXT_CLOSE(handle
, fsp
);
3275 errno
= saved_errno
;
3280 static int fruit_open_meta_netatalk(vfs_handle_struct
*handle
,
3281 struct smb_filename
*smb_fname
,
3288 struct adouble
*ad
= NULL
;
3291 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3294 * Return a valid fd, but ensure any attempt to use it returns an error
3295 * (EPIPE). All operations on the smb_fname or the fsp will use path
3305 if (flags
& (O_CREAT
| O_TRUNC
)) {
3307 * The attribute does not exist or needs to be truncated,
3308 * create an AppleDouble EA
3310 ad
= ad_init(fsp
, handle
, ADOUBLE_META
);
3316 rc
= ad_set(ad
, fsp
->fsp_name
);
3326 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc
, fakefd
));
3328 int saved_errno
= errno
;
3333 errno
= saved_errno
;
3338 static int fruit_open_meta(vfs_handle_struct
*handle
,
3339 struct smb_filename
*smb_fname
,
3340 files_struct
*fsp
, int flags
, mode_t mode
)
3343 struct fruit_config_data
*config
= NULL
;
3344 struct fio
*fio
= NULL
;
3346 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname
));
3348 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3349 struct fruit_config_data
, return -1);
3351 switch (config
->meta
) {
3352 case FRUIT_META_STREAM
:
3353 fd
= fruit_open_meta_stream(handle
, smb_fname
,
3357 case FRUIT_META_NETATALK
:
3358 fd
= fruit_open_meta_netatalk(handle
, smb_fname
,
3363 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
3367 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3373 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
3374 fio
->type
= ADOUBLE_META
;
3375 fio
->config
= config
;
3380 static int fruit_open_rsrc_adouble(vfs_handle_struct
*handle
,
3381 struct smb_filename
*smb_fname
,
3387 struct adouble
*ad
= NULL
;
3388 struct smb_filename
*smb_fname_base
= NULL
;
3389 struct fruit_config_data
*config
= NULL
;
3392 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3393 struct fruit_config_data
, return -1);
3395 if ((!(flags
& O_CREAT
)) &&
3396 S_ISDIR(fsp
->base_fsp
->fsp_name
->st
.st_ex_mode
))
3398 /* sorry, but directories don't habe a resource fork */
3403 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_base
);
3408 /* Sanitize flags */
3409 if (flags
& O_WRONLY
) {
3410 /* We always need read access for the metadata header too */
3415 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname_base
, fsp
,
3422 if (flags
& (O_CREAT
| O_TRUNC
)) {
3423 ad
= ad_init(fsp
, handle
, ADOUBLE_RSRC
);
3429 fsp
->fh
->fd
= hostfd
;
3431 rc
= ad_fset(ad
, fsp
);
3442 TALLOC_FREE(smb_fname_base
);
3444 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc
, hostfd
));
3446 int saved_errno
= errno
;
3449 * BUGBUGBUG -- we would need to call
3450 * fd_close_posix here, but we don't have a
3453 fsp
->fh
->fd
= hostfd
;
3457 errno
= saved_errno
;
3462 static int fruit_open_rsrc_xattr(vfs_handle_struct
*handle
,
3463 struct smb_filename
*smb_fname
,
3468 #ifdef HAVE_ATTROPEN
3471 fd
= attropen(smb_fname
->base_name
,
3472 AFPRESOURCE_EA_NETATALK
,
3487 static int fruit_open_rsrc(vfs_handle_struct
*handle
,
3488 struct smb_filename
*smb_fname
,
3489 files_struct
*fsp
, int flags
, mode_t mode
)
3492 struct fruit_config_data
*config
= NULL
;
3493 struct fio
*fio
= NULL
;
3495 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3497 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3498 struct fruit_config_data
, return -1);
3500 if (((flags
& O_ACCMODE
) == O_RDONLY
)
3501 && (flags
& O_CREAT
)
3502 && !VALID_STAT(fsp
->fsp_name
->st
))
3505 * This means the stream doesn't exist. macOS SMB server fails
3506 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3507 * 12565 and the test for this combination in
3508 * test_rfork_create().
3514 switch (config
->rsrc
) {
3515 case FRUIT_RSRC_STREAM
:
3516 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3519 case FRUIT_RSRC_ADFILE
:
3520 fd
= fruit_open_rsrc_adouble(handle
, smb_fname
,
3524 case FRUIT_RSRC_XATTR
:
3525 fd
= fruit_open_rsrc_xattr(handle
, smb_fname
,
3530 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
3534 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3540 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
3541 fio
->type
= ADOUBLE_RSRC
;
3542 fio
->config
= config
;
3547 static int fruit_open(vfs_handle_struct
*handle
,
3548 struct smb_filename
*smb_fname
,
3549 files_struct
*fsp
, int flags
, mode_t mode
)
3553 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3555 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
3556 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3559 if (is_afpinfo_stream(smb_fname
)) {
3560 fd
= fruit_open_meta(handle
, smb_fname
, fsp
, flags
, mode
);
3561 } else if (is_afpresource_stream(smb_fname
)) {
3562 fd
= fruit_open_rsrc(handle
, smb_fname
, fsp
, flags
, mode
);
3564 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3567 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3572 static int fruit_rename(struct vfs_handle_struct
*handle
,
3573 const struct smb_filename
*smb_fname_src
,
3574 const struct smb_filename
*smb_fname_dst
)
3577 struct fruit_config_data
*config
= NULL
;
3578 struct smb_filename
*src_adp_smb_fname
= NULL
;
3579 struct smb_filename
*dst_adp_smb_fname
= NULL
;
3581 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3582 struct fruit_config_data
, return -1);
3584 if (!VALID_STAT(smb_fname_src
->st
)) {
3585 DBG_ERR("Need valid stat for [%s]\n",
3586 smb_fname_str_dbg(smb_fname_src
));
3590 rc
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
3595 if ((config
->rsrc
!= FRUIT_RSRC_ADFILE
) ||
3596 (!S_ISREG(smb_fname_src
->st
.st_ex_mode
)))
3601 rc
= adouble_path(talloc_tos(), smb_fname_src
, &src_adp_smb_fname
);
3606 rc
= adouble_path(talloc_tos(), smb_fname_dst
, &dst_adp_smb_fname
);
3611 DBG_DEBUG("%s -> %s\n",
3612 smb_fname_str_dbg(src_adp_smb_fname
),
3613 smb_fname_str_dbg(dst_adp_smb_fname
));
3615 rc
= SMB_VFS_NEXT_RENAME(handle
, src_adp_smb_fname
, dst_adp_smb_fname
);
3616 if (errno
== ENOENT
) {
3621 TALLOC_FREE(src_adp_smb_fname
);
3622 TALLOC_FREE(dst_adp_smb_fname
);
3626 static int fruit_unlink_meta_stream(vfs_handle_struct
*handle
,
3627 const struct smb_filename
*smb_fname
)
3629 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3632 static int fruit_unlink_meta_netatalk(vfs_handle_struct
*handle
,
3633 const struct smb_filename
*smb_fname
)
3635 return SMB_VFS_REMOVEXATTR(handle
->conn
,
3637 AFPINFO_EA_NETATALK
);
3640 static int fruit_unlink_meta(vfs_handle_struct
*handle
,
3641 const struct smb_filename
*smb_fname
)
3643 struct fruit_config_data
*config
= NULL
;
3646 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3647 struct fruit_config_data
, return -1);
3649 switch (config
->meta
) {
3650 case FRUIT_META_STREAM
:
3651 rc
= fruit_unlink_meta_stream(handle
, smb_fname
);
3654 case FRUIT_META_NETATALK
:
3655 rc
= fruit_unlink_meta_netatalk(handle
, smb_fname
);
3659 DBG_ERR("Unsupported meta config [%d]\n", config
->meta
);
3666 static int fruit_unlink_rsrc_stream(vfs_handle_struct
*handle
,
3667 const struct smb_filename
*smb_fname
,
3672 if (!force_unlink
) {
3673 struct smb_filename
*smb_fname_cp
= NULL
;
3676 smb_fname_cp
= cp_smb_filename(talloc_tos(), smb_fname
);
3677 if (smb_fname_cp
== NULL
) {
3682 * 0 byte resource fork streams are not listed by
3683 * vfs_streaminfo, as a result stream cleanup/deletion of file
3684 * deletion doesn't remove the resourcefork stream.
3687 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_cp
);
3689 TALLOC_FREE(smb_fname_cp
);
3690 DBG_ERR("stat [%s] failed [%s]\n",
3691 smb_fname_str_dbg(smb_fname_cp
), strerror(errno
));
3695 size
= smb_fname_cp
->st
.st_ex_size
;
3696 TALLOC_FREE(smb_fname_cp
);
3699 /* OS X ignores resource fork stream delete requests */
3704 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3705 if ((ret
!= 0) && (errno
== ENOENT
) && force_unlink
) {
3712 static int fruit_unlink_rsrc_adouble(vfs_handle_struct
*handle
,
3713 const struct smb_filename
*smb_fname
,
3717 struct adouble
*ad
= NULL
;
3718 struct smb_filename
*adp_smb_fname
= NULL
;
3720 if (!force_unlink
) {
3721 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
3730 * 0 byte resource fork streams are not listed by
3731 * vfs_streaminfo, as a result stream cleanup/deletion of file
3732 * deletion doesn't remove the resourcefork stream.
3735 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
3736 /* OS X ignores resource fork stream delete requests */
3744 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
3749 rc
= SMB_VFS_NEXT_UNLINK(handle
, adp_smb_fname
);
3750 TALLOC_FREE(adp_smb_fname
);
3751 if ((rc
!= 0) && (errno
== ENOENT
) && force_unlink
) {
3758 static int fruit_unlink_rsrc_xattr(vfs_handle_struct
*handle
,
3759 const struct smb_filename
*smb_fname
,
3763 * OS X ignores resource fork stream delete requests, so nothing to do
3764 * here. Removing the file will remove the xattr anyway, so we don't
3765 * have to take care of removing 0 byte resource forks that could be
3771 static int fruit_unlink_rsrc(vfs_handle_struct
*handle
,
3772 const struct smb_filename
*smb_fname
,
3775 struct fruit_config_data
*config
= NULL
;
3778 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3779 struct fruit_config_data
, return -1);
3781 switch (config
->rsrc
) {
3782 case FRUIT_RSRC_STREAM
:
3783 rc
= fruit_unlink_rsrc_stream(handle
, smb_fname
, force_unlink
);
3786 case FRUIT_RSRC_ADFILE
:
3787 rc
= fruit_unlink_rsrc_adouble(handle
, smb_fname
, force_unlink
);
3790 case FRUIT_RSRC_XATTR
:
3791 rc
= fruit_unlink_rsrc_xattr(handle
, smb_fname
, force_unlink
);
3795 DBG_ERR("Unsupported rsrc config [%d]\n", config
->rsrc
);
3802 static int fruit_unlink(vfs_handle_struct
*handle
,
3803 const struct smb_filename
*smb_fname
)
3806 struct fruit_config_data
*config
= NULL
;
3807 struct smb_filename
*rsrc_smb_fname
= NULL
;
3809 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3810 struct fruit_config_data
, return -1);
3812 if (is_afpinfo_stream(smb_fname
)) {
3813 return fruit_unlink_meta(handle
, smb_fname
);
3814 } else if (is_afpresource_stream(smb_fname
)) {
3815 return fruit_unlink_rsrc(handle
, smb_fname
, false);
3816 } if (is_ntfs_stream_smb_fname(smb_fname
)) {
3817 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3821 * A request to delete the base file. Because 0 byte resource
3822 * fork streams are not listed by fruit_streaminfo,
3823 * delete_all_streams() can't remove 0 byte resource fork
3824 * streams, so we have to cleanup this here.
3826 rsrc_smb_fname
= synthetic_smb_fname(talloc_tos(),
3827 smb_fname
->base_name
,
3828 AFPRESOURCE_STREAM_NAME
,
3831 if (rsrc_smb_fname
== NULL
) {
3835 rc
= fruit_unlink_rsrc(handle
, rsrc_smb_fname
, true);
3836 if ((rc
!= 0) && (errno
!= ENOENT
)) {
3837 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3838 smb_fname_str_dbg(rsrc_smb_fname
), strerror(errno
));
3839 TALLOC_FREE(rsrc_smb_fname
);
3842 TALLOC_FREE(rsrc_smb_fname
);
3844 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3847 static int fruit_chmod(vfs_handle_struct
*handle
,
3848 const struct smb_filename
*smb_fname
,
3852 struct fruit_config_data
*config
= NULL
;
3853 struct smb_filename
*smb_fname_adp
= NULL
;
3855 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname
, mode
);
3860 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3861 struct fruit_config_data
, return -1);
3863 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3867 if (!VALID_STAT(smb_fname
->st
)) {
3871 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
3875 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_adp
);
3880 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp
->base_name
));
3882 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname_adp
, mode
);
3883 if (errno
== ENOENT
) {
3887 TALLOC_FREE(smb_fname_adp
);
3891 static int fruit_chown(vfs_handle_struct
*handle
,
3892 const struct smb_filename
*smb_fname
,
3897 struct fruit_config_data
*config
= NULL
;
3898 struct smb_filename
*adp_smb_fname
= NULL
;
3900 rc
= SMB_VFS_NEXT_CHOWN(handle
, smb_fname
, uid
, gid
);
3905 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3906 struct fruit_config_data
, return -1);
3908 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3912 if (!VALID_STAT(smb_fname
->st
)) {
3916 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
3920 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
3925 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname
->base_name
));
3927 rc
= SMB_VFS_NEXT_CHOWN(handle
, adp_smb_fname
, uid
, gid
);
3928 if (errno
== ENOENT
) {
3933 TALLOC_FREE(adp_smb_fname
);
3937 static int fruit_rmdir(struct vfs_handle_struct
*handle
,
3938 const struct smb_filename
*smb_fname
)
3942 struct fruit_config_data
*config
;
3944 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3945 struct fruit_config_data
, return -1);
3947 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3952 * Due to there is no way to change bDeleteVetoFiles variable
3953 * from this module, need to clean up ourselves
3956 dh
= SMB_VFS_OPENDIR(handle
->conn
, smb_fname
, NULL
, 0);
3961 while ((de
= SMB_VFS_READDIR(handle
->conn
, dh
, NULL
)) != NULL
) {
3963 struct adouble
*ad
= NULL
;
3965 struct smb_filename
*ad_smb_fname
= NULL
;
3968 match
= strncmp(de
->d_name
,
3969 ADOUBLE_NAME_PREFIX
,
3970 strlen(ADOUBLE_NAME_PREFIX
));
3975 p
= talloc_asprintf(talloc_tos(), "%s/%s",
3976 smb_fname
->base_name
, de
->d_name
);
3978 DBG_ERR("talloc_asprintf failed\n");
3982 ad_smb_fname
= synthetic_smb_fname(talloc_tos(), p
,
3986 if (ad_smb_fname
== NULL
) {
3987 DBG_ERR("synthetic_smb_fname failed\n");
3992 * Check whether it's a valid AppleDouble file, if
3993 * yes, delete it, ignore it otherwise.
3995 ad
= ad_get(talloc_tos(), handle
, ad_smb_fname
, ADOUBLE_RSRC
);
3997 TALLOC_FREE(ad_smb_fname
);
4003 ret
= SMB_VFS_NEXT_UNLINK(handle
, ad_smb_fname
);
4005 DBG_ERR("Deleting [%s] failed\n",
4006 smb_fname_str_dbg(ad_smb_fname
));
4008 TALLOC_FREE(ad_smb_fname
);
4013 SMB_VFS_CLOSEDIR(handle
->conn
, dh
);
4015 return SMB_VFS_NEXT_RMDIR(handle
, smb_fname
);
4018 static ssize_t
fruit_pread_meta_stream(vfs_handle_struct
*handle
,
4019 files_struct
*fsp
, void *data
,
4020 size_t n
, off_t offset
)
4025 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4031 DBG_ERR("Removing [%s] after short read [%zd]\n",
4032 fsp_str_dbg(fsp
), nread
);
4034 ret
= SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
4036 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp
));
4044 static ssize_t
fruit_pread_meta_adouble(vfs_handle_struct
*handle
,
4045 files_struct
*fsp
, void *data
,
4046 size_t n
, off_t offset
)
4049 struct adouble
*ad
= NULL
;
4050 char afpinfo_buf
[AFP_INFO_SIZE
];
4054 ai
= afpinfo_new(talloc_tos());
4059 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
4065 p
= ad_get_entry(ad
, ADEID_FINDERI
);
4067 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
4072 memcpy(&ai
->afpi_FinderInfo
[0], p
, ADEDLEN_FINDERI
);
4074 nread
= afpinfo_pack(ai
, afpinfo_buf
);
4075 if (nread
!= AFP_INFO_SIZE
) {
4080 memcpy(data
, afpinfo_buf
, n
);
4088 static ssize_t
fruit_pread_meta(vfs_handle_struct
*handle
,
4089 files_struct
*fsp
, void *data
,
4090 size_t n
, off_t offset
)
4092 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4097 * OS X has a off-by-1 error in the offset calculation, so we're
4098 * bug compatible here. It won't hurt, as any relevant real
4099 * world read requests from the AFP_AfpInfo stream will be
4100 * offset=0 n=60. offset is ignored anyway, see below.
4102 if ((offset
< 0) || (offset
>= AFP_INFO_SIZE
+ 1)) {
4107 DBG_ERR("Failed to fetch fsp extension");
4111 /* Yes, macOS always reads from offset 0 */
4113 to_return
= MIN(n
, AFP_INFO_SIZE
);
4115 switch (fio
->config
->meta
) {
4116 case FRUIT_META_STREAM
:
4117 nread
= fruit_pread_meta_stream(handle
, fsp
, data
,
4121 case FRUIT_META_NETATALK
:
4122 nread
= fruit_pread_meta_adouble(handle
, fsp
, data
,
4127 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4134 static ssize_t
fruit_pread_rsrc_stream(vfs_handle_struct
*handle
,
4135 files_struct
*fsp
, void *data
,
4136 size_t n
, off_t offset
)
4138 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4141 static ssize_t
fruit_pread_rsrc_xattr(vfs_handle_struct
*handle
,
4142 files_struct
*fsp
, void *data
,
4143 size_t n
, off_t offset
)
4145 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4148 static ssize_t
fruit_pread_rsrc_adouble(vfs_handle_struct
*handle
,
4149 files_struct
*fsp
, void *data
,
4150 size_t n
, off_t offset
)
4152 struct adouble
*ad
= NULL
;
4155 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
4160 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
,
4161 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
4167 static ssize_t
fruit_pread_rsrc(vfs_handle_struct
*handle
,
4168 files_struct
*fsp
, void *data
,
4169 size_t n
, off_t offset
)
4171 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4179 switch (fio
->config
->rsrc
) {
4180 case FRUIT_RSRC_STREAM
:
4181 nread
= fruit_pread_rsrc_stream(handle
, fsp
, data
, n
, offset
);
4184 case FRUIT_RSRC_ADFILE
:
4185 nread
= fruit_pread_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
4188 case FRUIT_RSRC_XATTR
:
4189 nread
= fruit_pread_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
4193 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4200 static ssize_t
fruit_pread(vfs_handle_struct
*handle
,
4201 files_struct
*fsp
, void *data
,
4202 size_t n
, off_t offset
)
4204 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4207 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
4208 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
4211 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4214 if (fio
->type
== ADOUBLE_META
) {
4215 nread
= fruit_pread_meta(handle
, fsp
, data
, n
, offset
);
4217 nread
= fruit_pread_rsrc(handle
, fsp
, data
, n
, offset
);
4220 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp
), nread
);
4224 static bool fruit_must_handle_aio_stream(struct fio
*fio
)
4230 if ((fio
->type
== ADOUBLE_META
) &&
4231 (fio
->config
->meta
== FRUIT_META_NETATALK
))
4236 if ((fio
->type
== ADOUBLE_RSRC
) &&
4237 (fio
->config
->rsrc
== FRUIT_RSRC_ADFILE
))
4245 struct fruit_pread_state
{
4247 struct vfs_aio_state vfs_aio_state
;
4250 static void fruit_pread_done(struct tevent_req
*subreq
);
4252 static struct tevent_req
*fruit_pread_send(
4253 struct vfs_handle_struct
*handle
,
4254 TALLOC_CTX
*mem_ctx
,
4255 struct tevent_context
*ev
,
4256 struct files_struct
*fsp
,
4258 size_t n
, off_t offset
)
4260 struct tevent_req
*req
= NULL
;
4261 struct tevent_req
*subreq
= NULL
;
4262 struct fruit_pread_state
*state
= NULL
;
4263 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4265 req
= tevent_req_create(mem_ctx
, &state
,
4266 struct fruit_pread_state
);
4271 if (fruit_must_handle_aio_stream(fio
)) {
4272 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
4273 if (state
->nread
!= n
) {
4274 if (state
->nread
!= -1) {
4277 tevent_req_error(req
, errno
);
4278 return tevent_req_post(req
, ev
);
4280 tevent_req_done(req
);
4281 return tevent_req_post(req
, ev
);
4284 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
4286 if (tevent_req_nomem(req
, subreq
)) {
4287 return tevent_req_post(req
, ev
);
4289 tevent_req_set_callback(subreq
, fruit_pread_done
, req
);
4293 static void fruit_pread_done(struct tevent_req
*subreq
)
4295 struct tevent_req
*req
= tevent_req_callback_data(
4296 subreq
, struct tevent_req
);
4297 struct fruit_pread_state
*state
= tevent_req_data(
4298 req
, struct fruit_pread_state
);
4300 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
4301 TALLOC_FREE(subreq
);
4303 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
4306 tevent_req_done(req
);
4309 static ssize_t
fruit_pread_recv(struct tevent_req
*req
,
4310 struct vfs_aio_state
*vfs_aio_state
)
4312 struct fruit_pread_state
*state
= tevent_req_data(
4313 req
, struct fruit_pread_state
);
4315 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
4319 *vfs_aio_state
= state
->vfs_aio_state
;
4320 return state
->nread
;
4323 static ssize_t
fruit_pwrite_meta_stream(vfs_handle_struct
*handle
,
4324 files_struct
*fsp
, const void *data
,
4325 size_t n
, off_t offset
)
4331 ai
= afpinfo_unpack(talloc_tos(), data
);
4336 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4337 if (nwritten
!= n
) {
4341 if (!ai_empty_finderinfo(ai
)) {
4345 ok
= set_delete_on_close(
4348 handle
->conn
->session_info
->security_token
,
4349 handle
->conn
->session_info
->unix_token
);
4351 DBG_ERR("set_delete_on_close on [%s] failed\n",
4359 static ssize_t
fruit_pwrite_meta_netatalk(vfs_handle_struct
*handle
,
4360 files_struct
*fsp
, const void *data
,
4361 size_t n
, off_t offset
)
4363 struct adouble
*ad
= NULL
;
4369 ai
= afpinfo_unpack(talloc_tos(), data
);
4374 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
4376 ad
= ad_init(talloc_tos(), handle
, ADOUBLE_META
);
4381 p
= ad_get_entry(ad
, ADEID_FINDERI
);
4383 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
4388 memcpy(p
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
4390 ret
= ad_fset(ad
, fsp
);
4392 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
4399 if (!ai_empty_finderinfo(ai
)) {
4403 ok
= set_delete_on_close(
4406 handle
->conn
->session_info
->security_token
,
4407 handle
->conn
->session_info
->unix_token
);
4409 DBG_ERR("set_delete_on_close on [%s] failed\n",
4417 static ssize_t
fruit_pwrite_meta(vfs_handle_struct
*handle
,
4418 files_struct
*fsp
, const void *data
,
4419 size_t n
, off_t offset
)
4421 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4425 * Writing an all 0 blob to the metadata stream
4426 * results in the stream being removed on a macOS
4427 * server. This ensures we behave the same and it
4428 * verified by the "delete AFP_AfpInfo by writing all
4431 if (n
!= AFP_INFO_SIZE
|| offset
!= 0) {
4432 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4433 (intmax_t)offset
, (intmax_t)n
);
4438 DBG_ERR("Failed to fetch fsp extension");
4442 switch (fio
->config
->meta
) {
4443 case FRUIT_META_STREAM
:
4444 nwritten
= fruit_pwrite_meta_stream(handle
, fsp
, data
,
4448 case FRUIT_META_NETATALK
:
4449 nwritten
= fruit_pwrite_meta_netatalk(handle
, fsp
, data
,
4454 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4461 static ssize_t
fruit_pwrite_rsrc_stream(vfs_handle_struct
*handle
,
4462 files_struct
*fsp
, const void *data
,
4463 size_t n
, off_t offset
)
4465 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4468 static ssize_t
fruit_pwrite_rsrc_xattr(vfs_handle_struct
*handle
,
4469 files_struct
*fsp
, const void *data
,
4470 size_t n
, off_t offset
)
4472 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4475 static ssize_t
fruit_pwrite_rsrc_adouble(vfs_handle_struct
*handle
,
4476 files_struct
*fsp
, const void *data
,
4477 size_t n
, off_t offset
)
4479 struct adouble
*ad
= NULL
;
4483 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
4485 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp
));
4489 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
,
4490 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
4491 if (nwritten
!= n
) {
4492 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4493 fsp_str_dbg(fsp
), nwritten
, n
);
4498 if ((n
+ offset
) > ad_getentrylen(ad
, ADEID_RFORK
)) {
4499 ad_setentrylen(ad
, ADEID_RFORK
, n
+ offset
);
4500 ret
= ad_fset(ad
, fsp
);
4502 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
4512 static ssize_t
fruit_pwrite_rsrc(vfs_handle_struct
*handle
,
4513 files_struct
*fsp
, const void *data
,
4514 size_t n
, off_t offset
)
4516 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4520 DBG_ERR("Failed to fetch fsp extension");
4524 switch (fio
->config
->rsrc
) {
4525 case FRUIT_RSRC_STREAM
:
4526 nwritten
= fruit_pwrite_rsrc_stream(handle
, fsp
, data
, n
, offset
);
4529 case FRUIT_RSRC_ADFILE
:
4530 nwritten
= fruit_pwrite_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
4533 case FRUIT_RSRC_XATTR
:
4534 nwritten
= fruit_pwrite_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
4538 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4545 static ssize_t
fruit_pwrite(vfs_handle_struct
*handle
,
4546 files_struct
*fsp
, const void *data
,
4547 size_t n
, off_t offset
)
4549 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4552 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
4553 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
4556 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4559 if (fio
->type
== ADOUBLE_META
) {
4560 nwritten
= fruit_pwrite_meta(handle
, fsp
, data
, n
, offset
);
4562 nwritten
= fruit_pwrite_rsrc(handle
, fsp
, data
, n
, offset
);
4565 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp
), nwritten
);
4569 struct fruit_pwrite_state
{
4571 struct vfs_aio_state vfs_aio_state
;
4574 static void fruit_pwrite_done(struct tevent_req
*subreq
);
4576 static struct tevent_req
*fruit_pwrite_send(
4577 struct vfs_handle_struct
*handle
,
4578 TALLOC_CTX
*mem_ctx
,
4579 struct tevent_context
*ev
,
4580 struct files_struct
*fsp
,
4582 size_t n
, off_t offset
)
4584 struct tevent_req
*req
= NULL
;
4585 struct tevent_req
*subreq
= NULL
;
4586 struct fruit_pwrite_state
*state
= NULL
;
4587 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4589 req
= tevent_req_create(mem_ctx
, &state
,
4590 struct fruit_pwrite_state
);
4595 if (fruit_must_handle_aio_stream(fio
)) {
4596 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
4597 if (state
->nwritten
!= n
) {
4598 if (state
->nwritten
!= -1) {
4601 tevent_req_error(req
, errno
);
4602 return tevent_req_post(req
, ev
);
4604 tevent_req_done(req
);
4605 return tevent_req_post(req
, ev
);
4608 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
4610 if (tevent_req_nomem(req
, subreq
)) {
4611 return tevent_req_post(req
, ev
);
4613 tevent_req_set_callback(subreq
, fruit_pwrite_done
, req
);
4617 static void fruit_pwrite_done(struct tevent_req
*subreq
)
4619 struct tevent_req
*req
= tevent_req_callback_data(
4620 subreq
, struct tevent_req
);
4621 struct fruit_pwrite_state
*state
= tevent_req_data(
4622 req
, struct fruit_pwrite_state
);
4624 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
4625 TALLOC_FREE(subreq
);
4627 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
4630 tevent_req_done(req
);
4633 static ssize_t
fruit_pwrite_recv(struct tevent_req
*req
,
4634 struct vfs_aio_state
*vfs_aio_state
)
4636 struct fruit_pwrite_state
*state
= tevent_req_data(
4637 req
, struct fruit_pwrite_state
);
4639 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
4643 *vfs_aio_state
= state
->vfs_aio_state
;
4644 return state
->nwritten
;
4648 * Helper to stat/lstat the base file of an smb_fname.
4650 static int fruit_stat_base(vfs_handle_struct
*handle
,
4651 struct smb_filename
*smb_fname
,
4654 char *tmp_stream_name
;
4657 tmp_stream_name
= smb_fname
->stream_name
;
4658 smb_fname
->stream_name
= NULL
;
4660 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4662 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4664 smb_fname
->stream_name
= tmp_stream_name
;
4668 static int fruit_stat_meta_stream(vfs_handle_struct
*handle
,
4669 struct smb_filename
*smb_fname
,
4675 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4677 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4683 static int fruit_stat_meta_netatalk(vfs_handle_struct
*handle
,
4684 struct smb_filename
*smb_fname
,
4687 struct adouble
*ad
= NULL
;
4689 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
4691 DBG_INFO("fruit_stat_meta %s: %s\n",
4692 smb_fname_str_dbg(smb_fname
), strerror(errno
));
4698 /* Populate the stat struct with info from the base file. */
4699 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
4702 smb_fname
->st
.st_ex_size
= AFP_INFO_SIZE
;
4703 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4704 smb_fname
->stream_name
);
4708 static int fruit_stat_meta(vfs_handle_struct
*handle
,
4709 struct smb_filename
*smb_fname
,
4712 struct fruit_config_data
*config
= NULL
;
4715 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4716 struct fruit_config_data
, return -1);
4718 switch (config
->meta
) {
4719 case FRUIT_META_STREAM
:
4720 ret
= fruit_stat_meta_stream(handle
, smb_fname
, follow_links
);
4723 case FRUIT_META_NETATALK
:
4724 ret
= fruit_stat_meta_netatalk(handle
, smb_fname
, follow_links
);
4728 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
4735 static int fruit_stat_rsrc_netatalk(vfs_handle_struct
*handle
,
4736 struct smb_filename
*smb_fname
,
4739 struct adouble
*ad
= NULL
;
4742 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
4748 /* Populate the stat struct with info from the base file. */
4749 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
4755 smb_fname
->st
.st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
4756 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4757 smb_fname
->stream_name
);
4762 static int fruit_stat_rsrc_stream(vfs_handle_struct
*handle
,
4763 struct smb_filename
*smb_fname
,
4769 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4771 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4777 static int fruit_stat_rsrc_xattr(vfs_handle_struct
*handle
,
4778 struct smb_filename
*smb_fname
,
4781 #ifdef HAVE_ATTROPEN
4785 /* Populate the stat struct with info from the base file. */
4786 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
4791 fd
= attropen(smb_fname
->base_name
,
4792 AFPRESOURCE_EA_NETATALK
,
4798 ret
= sys_fstat(fd
, &smb_fname
->st
, false);
4801 DBG_ERR("fstat [%s:%s] failed\n", smb_fname
->base_name
,
4802 AFPRESOURCE_EA_NETATALK
);
4808 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4809 smb_fname
->stream_name
);
4819 static int fruit_stat_rsrc(vfs_handle_struct
*handle
,
4820 struct smb_filename
*smb_fname
,
4823 struct fruit_config_data
*config
= NULL
;
4826 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
4828 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4829 struct fruit_config_data
, return -1);
4831 switch (config
->rsrc
) {
4832 case FRUIT_RSRC_STREAM
:
4833 ret
= fruit_stat_rsrc_stream(handle
, smb_fname
, follow_links
);
4836 case FRUIT_RSRC_XATTR
:
4837 ret
= fruit_stat_rsrc_xattr(handle
, smb_fname
, follow_links
);
4840 case FRUIT_RSRC_ADFILE
:
4841 ret
= fruit_stat_rsrc_netatalk(handle
, smb_fname
, follow_links
);
4845 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
4852 static int fruit_stat(vfs_handle_struct
*handle
,
4853 struct smb_filename
*smb_fname
)
4857 DEBUG(10, ("fruit_stat called for %s\n",
4858 smb_fname_str_dbg(smb_fname
)));
4860 if (!is_ntfs_stream_smb_fname(smb_fname
)
4861 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
4862 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4864 update_btime(handle
, smb_fname
);
4870 * Note if lp_posix_paths() is true, we can never
4871 * get here as is_ntfs_stream_smb_fname() is
4872 * always false. So we never need worry about
4873 * not following links here.
4876 if (is_afpinfo_stream(smb_fname
)) {
4877 rc
= fruit_stat_meta(handle
, smb_fname
, true);
4878 } else if (is_afpresource_stream(smb_fname
)) {
4879 rc
= fruit_stat_rsrc(handle
, smb_fname
, true);
4881 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4885 update_btime(handle
, smb_fname
);
4886 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
4887 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
4888 smb_fname
->st
.st_ex_blocks
=
4889 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
4894 static int fruit_lstat(vfs_handle_struct
*handle
,
4895 struct smb_filename
*smb_fname
)
4899 DEBUG(10, ("fruit_lstat called for %s\n",
4900 smb_fname_str_dbg(smb_fname
)));
4902 if (!is_ntfs_stream_smb_fname(smb_fname
)
4903 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
4904 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4906 update_btime(handle
, smb_fname
);
4911 if (is_afpinfo_stream(smb_fname
)) {
4912 rc
= fruit_stat_meta(handle
, smb_fname
, false);
4913 } else if (is_afpresource_stream(smb_fname
)) {
4914 rc
= fruit_stat_rsrc(handle
, smb_fname
, false);
4916 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4920 update_btime(handle
, smb_fname
);
4921 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
4922 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
4923 smb_fname
->st
.st_ex_blocks
=
4924 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
4929 static int fruit_fstat_meta_stream(vfs_handle_struct
*handle
,
4931 SMB_STRUCT_STAT
*sbuf
)
4933 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4936 static int fruit_fstat_meta_netatalk(vfs_handle_struct
*handle
,
4938 SMB_STRUCT_STAT
*sbuf
)
4942 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
4947 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
4948 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
4949 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
4954 static int fruit_fstat_meta(vfs_handle_struct
*handle
,
4956 SMB_STRUCT_STAT
*sbuf
,
4961 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
4963 switch (fio
->config
->meta
) {
4964 case FRUIT_META_STREAM
:
4965 ret
= fruit_fstat_meta_stream(handle
, fsp
, sbuf
);
4968 case FRUIT_META_NETATALK
:
4969 ret
= fruit_fstat_meta_netatalk(handle
, fsp
, sbuf
);
4973 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4977 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp
), ret
);
4981 static int fruit_fstat_rsrc_xattr(vfs_handle_struct
*handle
,
4983 SMB_STRUCT_STAT
*sbuf
)
4985 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4988 static int fruit_fstat_rsrc_stream(vfs_handle_struct
*handle
,
4990 SMB_STRUCT_STAT
*sbuf
)
4992 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4995 static int fruit_fstat_rsrc_adouble(vfs_handle_struct
*handle
,
4997 SMB_STRUCT_STAT
*sbuf
)
4999 struct adouble
*ad
= NULL
;
5002 /* Populate the stat struct with info from the base file. */
5003 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
5008 ad
= ad_get(talloc_tos(), handle
,
5009 fsp
->base_fsp
->fsp_name
,
5012 DBG_ERR("ad_get [%s] failed [%s]\n",
5013 fsp_str_dbg(fsp
), strerror(errno
));
5017 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
5018 sbuf
->st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
5019 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
5025 static int fruit_fstat_rsrc(vfs_handle_struct
*handle
, files_struct
*fsp
,
5026 SMB_STRUCT_STAT
*sbuf
, struct fio
*fio
)
5030 switch (fio
->config
->rsrc
) {
5031 case FRUIT_RSRC_STREAM
:
5032 ret
= fruit_fstat_rsrc_stream(handle
, fsp
, sbuf
);
5035 case FRUIT_RSRC_ADFILE
:
5036 ret
= fruit_fstat_rsrc_adouble(handle
, fsp
, sbuf
);
5039 case FRUIT_RSRC_XATTR
:
5040 ret
= fruit_fstat_rsrc_xattr(handle
, fsp
, sbuf
);
5044 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
5051 static int fruit_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
5052 SMB_STRUCT_STAT
*sbuf
)
5054 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5058 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
5061 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
5063 if (fio
->type
== ADOUBLE_META
) {
5064 rc
= fruit_fstat_meta(handle
, fsp
, sbuf
, fio
);
5066 rc
= fruit_fstat_rsrc(handle
, fsp
, sbuf
, fio
);
5070 sbuf
->st_ex_mode
&= ~S_IFMT
;
5071 sbuf
->st_ex_mode
|= S_IFREG
;
5072 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
5075 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX
"]\n",
5076 fsp_str_dbg(fsp
), rc
, (intmax_t)sbuf
->st_ex_size
);
5080 static NTSTATUS
delete_invalid_meta_stream(
5081 vfs_handle_struct
*handle
,
5082 const struct smb_filename
*smb_fname
,
5083 TALLOC_CTX
*mem_ctx
,
5084 unsigned int *pnum_streams
,
5085 struct stream_struct
**pstreams
)
5087 struct smb_filename
*sname
= NULL
;
5091 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
, AFPINFO_STREAM
);
5093 return NT_STATUS_INTERNAL_ERROR
;
5096 sname
= synthetic_smb_fname(talloc_tos(),
5097 smb_fname
->base_name
,
5098 AFPINFO_STREAM_NAME
,
5100 if (sname
== NULL
) {
5101 return NT_STATUS_NO_MEMORY
;
5104 ret
= SMB_VFS_NEXT_UNLINK(handle
, sname
);
5107 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname
));
5108 return map_nt_error_from_unix(errno
);
5111 return NT_STATUS_OK
;
5114 static NTSTATUS
fruit_streaminfo_meta_stream(
5115 vfs_handle_struct
*handle
,
5116 struct files_struct
*fsp
,
5117 const struct smb_filename
*smb_fname
,
5118 TALLOC_CTX
*mem_ctx
,
5119 unsigned int *pnum_streams
,
5120 struct stream_struct
**pstreams
)
5122 struct stream_struct
*stream
= *pstreams
;
5123 unsigned int num_streams
= *pnum_streams
;
5124 struct smb_filename
*sname
= NULL
;
5125 char *full_name
= NULL
;
5127 struct share_mode_lock
*lck
= NULL
;
5128 struct file_id id
= {0};
5129 bool delete_on_close_set
;
5135 for (i
= 0; i
< num_streams
; i
++) {
5136 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
5141 if (i
== num_streams
) {
5142 return NT_STATUS_OK
;
5145 if (stream
[i
].size
!= AFP_INFO_SIZE
) {
5146 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5147 (intmax_t)stream
[i
].size
, smb_fname_str_dbg(smb_fname
));
5149 return delete_invalid_meta_stream(handle
, smb_fname
, mem_ctx
,
5150 pnum_streams
, pstreams
);
5154 * Now check if there's a delete-on-close pending on the stream. If so,
5155 * hide the stream. This behaviour was verified against a macOS 10.12
5159 sname
= synthetic_smb_fname(talloc_tos(),
5160 smb_fname
->base_name
,
5161 AFPINFO_STREAM_NAME
,
5163 if (sname
== NULL
) {
5164 status
= NT_STATUS_NO_MEMORY
;
5168 ret
= SMB_VFS_NEXT_STAT(handle
, sname
);
5170 status
= map_nt_error_from_unix(errno
);
5174 id
= SMB_VFS_NEXT_FILE_ID_CREATE(handle
, &sname
->st
);
5176 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
5178 status
= NT_STATUS_OK
;
5182 full_name
= talloc_asprintf(talloc_tos(),
5186 if (full_name
== NULL
) {
5187 status
= NT_STATUS_NO_MEMORY
;
5191 status
= file_name_hash(handle
->conn
, full_name
, &name_hash
);
5192 if (!NT_STATUS_IS_OK(status
)) {
5196 delete_on_close_set
= is_delete_on_close_set(lck
, name_hash
);
5197 if (delete_on_close_set
) {
5198 ok
= del_fruit_stream(mem_ctx
,
5203 status
= NT_STATUS_INTERNAL_ERROR
;
5208 status
= NT_STATUS_OK
;
5213 TALLOC_FREE(full_name
);
5217 static NTSTATUS
fruit_streaminfo_meta_netatalk(
5218 vfs_handle_struct
*handle
,
5219 struct files_struct
*fsp
,
5220 const struct smb_filename
*smb_fname
,
5221 TALLOC_CTX
*mem_ctx
,
5222 unsigned int *pnum_streams
,
5223 struct stream_struct
**pstreams
)
5225 struct stream_struct
*stream
= *pstreams
;
5226 unsigned int num_streams
= *pnum_streams
;
5227 struct adouble
*ad
= NULL
;
5232 /* Remove the Netatalk xattr from the list */
5233 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5234 ":" NETATALK_META_XATTR
":$DATA");
5236 return NT_STATUS_NO_MEMORY
;
5240 * Check if there's a AFPINFO_STREAM from the VFS streams
5241 * backend and if yes, remove it from the list
5243 for (i
= 0; i
< num_streams
; i
++) {
5244 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
5249 if (i
< num_streams
) {
5250 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5251 smb_fname_str_dbg(smb_fname
));
5253 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5256 return NT_STATUS_INTERNAL_ERROR
;
5260 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
5262 return NT_STATUS_OK
;
5265 is_fi_empty
= ad_empty_finderinfo(ad
);
5269 return NT_STATUS_OK
;
5272 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5273 AFPINFO_STREAM_NAME
, AFP_INFO_SIZE
,
5274 smb_roundup(handle
->conn
, AFP_INFO_SIZE
));
5276 return NT_STATUS_NO_MEMORY
;
5279 return NT_STATUS_OK
;
5282 static NTSTATUS
fruit_streaminfo_meta(vfs_handle_struct
*handle
,
5283 struct files_struct
*fsp
,
5284 const struct smb_filename
*smb_fname
,
5285 TALLOC_CTX
*mem_ctx
,
5286 unsigned int *pnum_streams
,
5287 struct stream_struct
**pstreams
)
5289 struct fruit_config_data
*config
= NULL
;
5292 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5293 return NT_STATUS_INTERNAL_ERROR
);
5295 switch (config
->meta
) {
5296 case FRUIT_META_NETATALK
:
5297 status
= fruit_streaminfo_meta_netatalk(handle
, fsp
, smb_fname
,
5298 mem_ctx
, pnum_streams
,
5302 case FRUIT_META_STREAM
:
5303 status
= fruit_streaminfo_meta_stream(handle
, fsp
, smb_fname
,
5304 mem_ctx
, pnum_streams
,
5309 return NT_STATUS_INTERNAL_ERROR
;
5315 static NTSTATUS
fruit_streaminfo_rsrc_stream(
5316 vfs_handle_struct
*handle
,
5317 struct files_struct
*fsp
,
5318 const struct smb_filename
*smb_fname
,
5319 TALLOC_CTX
*mem_ctx
,
5320 unsigned int *pnum_streams
,
5321 struct stream_struct
**pstreams
)
5325 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
5327 DBG_ERR("Filtering resource stream failed\n");
5328 return NT_STATUS_INTERNAL_ERROR
;
5330 return NT_STATUS_OK
;
5333 static NTSTATUS
fruit_streaminfo_rsrc_xattr(
5334 vfs_handle_struct
*handle
,
5335 struct files_struct
*fsp
,
5336 const struct smb_filename
*smb_fname
,
5337 TALLOC_CTX
*mem_ctx
,
5338 unsigned int *pnum_streams
,
5339 struct stream_struct
**pstreams
)
5343 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
5345 DBG_ERR("Filtering resource stream failed\n");
5346 return NT_STATUS_INTERNAL_ERROR
;
5348 return NT_STATUS_OK
;
5351 static NTSTATUS
fruit_streaminfo_rsrc_adouble(
5352 vfs_handle_struct
*handle
,
5353 struct files_struct
*fsp
,
5354 const struct smb_filename
*smb_fname
,
5355 TALLOC_CTX
*mem_ctx
,
5356 unsigned int *pnum_streams
,
5357 struct stream_struct
**pstreams
)
5359 struct stream_struct
*stream
= *pstreams
;
5360 unsigned int num_streams
= *pnum_streams
;
5361 struct adouble
*ad
= NULL
;
5367 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5368 * and if yes, remove it from the list
5370 for (i
= 0; i
< num_streams
; i
++) {
5371 if (strequal_m(stream
[i
].name
, AFPRESOURCE_STREAM
)) {
5376 if (i
< num_streams
) {
5377 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5378 smb_fname_str_dbg(smb_fname
));
5380 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5381 AFPRESOURCE_STREAM
);
5383 return NT_STATUS_INTERNAL_ERROR
;
5387 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
5389 return NT_STATUS_OK
;
5392 rlen
= ad_getentrylen(ad
, ADEID_RFORK
);
5396 return NT_STATUS_OK
;
5399 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5400 AFPRESOURCE_STREAM_NAME
, rlen
,
5401 smb_roundup(handle
->conn
, rlen
));
5403 return NT_STATUS_NO_MEMORY
;
5406 return NT_STATUS_OK
;
5409 static NTSTATUS
fruit_streaminfo_rsrc(vfs_handle_struct
*handle
,
5410 struct files_struct
*fsp
,
5411 const struct smb_filename
*smb_fname
,
5412 TALLOC_CTX
*mem_ctx
,
5413 unsigned int *pnum_streams
,
5414 struct stream_struct
**pstreams
)
5416 struct fruit_config_data
*config
= NULL
;
5419 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5420 return NT_STATUS_INTERNAL_ERROR
);
5422 switch (config
->rsrc
) {
5423 case FRUIT_RSRC_STREAM
:
5424 status
= fruit_streaminfo_rsrc_stream(handle
, fsp
, smb_fname
,
5425 mem_ctx
, pnum_streams
,
5429 case FRUIT_RSRC_XATTR
:
5430 status
= fruit_streaminfo_rsrc_xattr(handle
, fsp
, smb_fname
,
5431 mem_ctx
, pnum_streams
,
5435 case FRUIT_RSRC_ADFILE
:
5436 status
= fruit_streaminfo_rsrc_adouble(handle
, fsp
, smb_fname
,
5437 mem_ctx
, pnum_streams
,
5442 return NT_STATUS_INTERNAL_ERROR
;
5448 static NTSTATUS
fruit_streaminfo(vfs_handle_struct
*handle
,
5449 struct files_struct
*fsp
,
5450 const struct smb_filename
*smb_fname
,
5451 TALLOC_CTX
*mem_ctx
,
5452 unsigned int *pnum_streams
,
5453 struct stream_struct
**pstreams
)
5455 struct fruit_config_data
*config
= NULL
;
5458 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5459 return NT_STATUS_UNSUCCESSFUL
);
5461 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
5463 status
= SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, smb_fname
, mem_ctx
,
5464 pnum_streams
, pstreams
);
5465 if (!NT_STATUS_IS_OK(status
)) {
5469 status
= fruit_streaminfo_meta(handle
, fsp
, smb_fname
,
5470 mem_ctx
, pnum_streams
, pstreams
);
5471 if (!NT_STATUS_IS_OK(status
)) {
5475 status
= fruit_streaminfo_rsrc(handle
, fsp
, smb_fname
,
5476 mem_ctx
, pnum_streams
, pstreams
);
5477 if (!NT_STATUS_IS_OK(status
)) {
5481 return NT_STATUS_OK
;
5484 static int fruit_ntimes(vfs_handle_struct
*handle
,
5485 const struct smb_filename
*smb_fname
,
5486 struct smb_file_time
*ft
)
5489 struct adouble
*ad
= NULL
;
5490 struct fruit_config_data
*config
= NULL
;
5492 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5495 if ((config
->meta
!= FRUIT_META_NETATALK
) ||
5496 null_timespec(ft
->create_time
))
5498 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
5501 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname
),
5502 time_to_asc(convert_timespec_to_time_t(ft
->create_time
))));
5504 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
5509 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
,
5510 convert_time_t_to_uint32_t(ft
->create_time
.tv_sec
));
5512 rc
= ad_set(ad
, smb_fname
);
5518 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname
)));
5521 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
5524 static int fruit_fallocate(struct vfs_handle_struct
*handle
,
5525 struct files_struct
*fsp
,
5530 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5533 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
5536 /* Let the pwrite code path handle it. */
5541 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct
*handle
,
5542 struct files_struct
*fsp
,
5546 return SMB_VFS_FREMOVEXATTR(fsp
, AFPRESOURCE_EA_NETATALK
);
5549 #ifdef HAVE_ATTROPEN
5550 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5555 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct
*handle
,
5556 struct files_struct
*fsp
,
5560 struct adouble
*ad
= NULL
;
5563 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
5565 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5566 fsp_str_dbg(fsp
), strerror(errno
));
5570 ad_off
= ad_getentryoff(ad
, ADEID_RFORK
);
5572 rc
= ftruncate(fsp
->fh
->fd
, offset
+ ad_off
);
5578 ad_setentrylen(ad
, ADEID_RFORK
, offset
);
5580 rc
= ad_fset(ad
, fsp
);
5582 DBG_ERR("ad_fset [%s] failed [%s]\n",
5583 fsp_str_dbg(fsp
), strerror(errno
));
5592 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct
*handle
,
5593 struct files_struct
*fsp
,
5597 return SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
5600 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5603 static int fruit_ftruncate_rsrc(struct vfs_handle_struct
*handle
,
5604 struct files_struct
*fsp
,
5607 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5611 DBG_ERR("Failed to fetch fsp extension");
5615 switch (fio
->config
->rsrc
) {
5616 case FRUIT_RSRC_XATTR
:
5617 ret
= fruit_ftruncate_rsrc_xattr(handle
, fsp
, offset
);
5620 case FRUIT_RSRC_ADFILE
:
5621 ret
= fruit_ftruncate_rsrc_adouble(handle
, fsp
, offset
);
5624 case FRUIT_RSRC_STREAM
:
5625 ret
= fruit_ftruncate_rsrc_stream(handle
, fsp
, offset
);
5629 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
5637 static int fruit_ftruncate_meta(struct vfs_handle_struct
*handle
,
5638 struct files_struct
*fsp
,
5642 DBG_WARNING("ftruncate %s to %jd",
5643 fsp_str_dbg(fsp
), (intmax_t)offset
);
5644 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5649 /* OS X returns success but does nothing */
5650 DBG_INFO("ignoring ftruncate %s to %jd\n",
5651 fsp_str_dbg(fsp
), (intmax_t)offset
);
5655 static int fruit_ftruncate(struct vfs_handle_struct
*handle
,
5656 struct files_struct
*fsp
,
5659 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5662 DBG_DEBUG("Path [%s] offset [%"PRIdMAX
"]\n", fsp_str_dbg(fsp
),
5667 global_fruit_config
.nego_aapl
&&
5668 is_ntfs_stream_smb_fname(fsp
->fsp_name
) &&
5669 !is_ntfs_default_stream_smb_fname(fsp
->fsp_name
))
5671 return SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
5673 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5676 if (fio
->type
== ADOUBLE_META
) {
5677 ret
= fruit_ftruncate_meta(handle
, fsp
, offset
);
5679 ret
= fruit_ftruncate_rsrc(handle
, fsp
, offset
);
5682 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp
), ret
);
5686 static NTSTATUS
fruit_create_file(vfs_handle_struct
*handle
,
5687 struct smb_request
*req
,
5688 uint16_t root_dir_fid
,
5689 struct smb_filename
*smb_fname
,
5690 uint32_t access_mask
,
5691 uint32_t share_access
,
5692 uint32_t create_disposition
,
5693 uint32_t create_options
,
5694 uint32_t file_attributes
,
5695 uint32_t oplock_request
,
5696 struct smb2_lease
*lease
,
5697 uint64_t allocation_size
,
5698 uint32_t private_flags
,
5699 struct security_descriptor
*sd
,
5700 struct ea_list
*ea_list
,
5701 files_struct
**result
,
5703 const struct smb2_create_blobs
*in_context_blobs
,
5704 struct smb2_create_blobs
*out_context_blobs
)
5707 struct fruit_config_data
*config
= NULL
;
5708 files_struct
*fsp
= NULL
;
5710 status
= check_aapl(handle
, req
, in_context_blobs
, out_context_blobs
);
5711 if (!NT_STATUS_IS_OK(status
)) {
5715 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5716 return NT_STATUS_UNSUCCESSFUL
);
5718 status
= SMB_VFS_NEXT_CREATE_FILE(
5719 handle
, req
, root_dir_fid
, smb_fname
,
5720 access_mask
, share_access
,
5721 create_disposition
, create_options
,
5722 file_attributes
, oplock_request
,
5724 allocation_size
, private_flags
,
5725 sd
, ea_list
, result
,
5726 pinfo
, in_context_blobs
, out_context_blobs
);
5727 if (!NT_STATUS_IS_OK(status
)) {
5733 if (global_fruit_config
.nego_aapl
) {
5734 if (config
->posix_rename
&& fsp
->is_directory
) {
5736 * Enable POSIX directory rename behaviour
5738 fsp
->posix_flags
|= FSP_POSIX_FLAGS_RENAME
;
5743 * If this is a plain open for existing files, opening an 0
5744 * byte size resource fork MUST fail with
5745 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5747 * Cf the vfs_fruit torture tests in test_rfork_create().
5749 if (is_afpresource_stream(fsp
->fsp_name
) &&
5750 create_disposition
== FILE_OPEN
)
5752 if (fsp
->fsp_name
->st
.st_ex_size
== 0) {
5753 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5758 if (is_ntfs_stream_smb_fname(smb_fname
)
5759 || fsp
->is_directory
) {
5763 if (config
->locking
== FRUIT_LOCKING_NETATALK
) {
5764 status
= fruit_check_access(
5767 map_share_mode_to_deny_mode(share_access
, 0));
5768 if (!NT_STATUS_IS_OK(status
)) {
5776 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status
)));
5779 close_file(req
, fsp
, ERROR_CLOSE
);
5780 *result
= fsp
= NULL
;
5786 static NTSTATUS
fruit_readdir_attr(struct vfs_handle_struct
*handle
,
5787 const struct smb_filename
*fname
,
5788 TALLOC_CTX
*mem_ctx
,
5789 struct readdir_attr_data
**pattr_data
)
5791 struct fruit_config_data
*config
= NULL
;
5792 struct readdir_attr_data
*attr_data
;
5795 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5796 struct fruit_config_data
,
5797 return NT_STATUS_UNSUCCESSFUL
);
5799 if (!global_fruit_config
.nego_aapl
) {
5800 return SMB_VFS_NEXT_READDIR_ATTR(handle
, fname
, mem_ctx
, pattr_data
);
5803 DEBUG(10, ("fruit_readdir_attr %s\n", fname
->base_name
));
5805 *pattr_data
= talloc_zero(mem_ctx
, struct readdir_attr_data
);
5806 if (*pattr_data
== NULL
) {
5807 return NT_STATUS_UNSUCCESSFUL
;
5809 attr_data
= *pattr_data
;
5810 attr_data
->type
= RDATTR_AAPL
;
5813 * Mac metadata: compressed FinderInfo, resource fork length
5816 status
= readdir_attr_macmeta(handle
, fname
, attr_data
);
5817 if (!NT_STATUS_IS_OK(status
)) {
5819 * Error handling is tricky: if we return failure from
5820 * this function, the corresponding directory entry
5821 * will to be passed to the client, so we really just
5822 * want to error out on fatal errors.
5824 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
5832 if (config
->unix_info_enabled
) {
5833 attr_data
->attr_data
.aapl
.unix_mode
= fname
->st
.st_ex_mode
;
5839 if (!config
->readdir_attr_max_access
) {
5840 attr_data
->attr_data
.aapl
.max_access
= FILE_GENERIC_ALL
;
5842 status
= smbd_calculate_access_mask(
5846 SEC_FLAG_MAXIMUM_ALLOWED
,
5847 &attr_data
->attr_data
.aapl
.max_access
);
5848 if (!NT_STATUS_IS_OK(status
)) {
5853 return NT_STATUS_OK
;
5856 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5857 fname
->base_name
, nt_errstr(status
)));
5858 TALLOC_FREE(*pattr_data
);
5862 static NTSTATUS
fruit_fget_nt_acl(vfs_handle_struct
*handle
,
5864 uint32_t security_info
,
5865 TALLOC_CTX
*mem_ctx
,
5866 struct security_descriptor
**ppdesc
)
5869 struct security_ace ace
;
5871 struct fruit_config_data
*config
;
5873 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5874 struct fruit_config_data
,
5875 return NT_STATUS_UNSUCCESSFUL
);
5877 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
5879 if (!NT_STATUS_IS_OK(status
)) {
5884 * Add MS NFS style ACEs with uid, gid and mode
5886 if (!global_fruit_config
.nego_aapl
) {
5887 return NT_STATUS_OK
;
5889 if (!config
->unix_info_enabled
) {
5890 return NT_STATUS_OK
;
5893 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5894 status
= remove_virtual_nfs_aces(*ppdesc
);
5895 if (!NT_STATUS_IS_OK(status
)) {
5896 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5900 /* MS NFS style mode */
5901 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
, fsp
->fsp_name
->st
.st_ex_mode
);
5902 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5903 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5904 if (!NT_STATUS_IS_OK(status
)) {
5905 DEBUG(1,("failed to add MS NFS style ACE\n"));
5909 /* MS NFS style uid */
5910 sid_compose(&sid
, &global_sid_Unix_NFS_Users
, fsp
->fsp_name
->st
.st_ex_uid
);
5911 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5912 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5913 if (!NT_STATUS_IS_OK(status
)) {
5914 DEBUG(1,("failed to add MS NFS style ACE\n"));
5918 /* MS NFS style gid */
5919 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
, fsp
->fsp_name
->st
.st_ex_gid
);
5920 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5921 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5922 if (!NT_STATUS_IS_OK(status
)) {
5923 DEBUG(1,("failed to add MS NFS style ACE\n"));
5927 return NT_STATUS_OK
;
5930 static NTSTATUS
fruit_fset_nt_acl(vfs_handle_struct
*handle
,
5932 uint32_t security_info_sent
,
5933 const struct security_descriptor
*orig_psd
)
5937 mode_t ms_nfs_mode
= 0;
5939 struct security_descriptor
*psd
= NULL
;
5940 uint32_t orig_num_aces
= 0;
5942 if (orig_psd
->dacl
!= NULL
) {
5943 orig_num_aces
= orig_psd
->dacl
->num_aces
;
5946 psd
= security_descriptor_copy(talloc_tos(), orig_psd
);
5948 return NT_STATUS_NO_MEMORY
;
5951 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp
));
5953 status
= check_ms_nfs(handle
, fsp
, psd
, &ms_nfs_mode
, &do_chmod
);
5954 if (!NT_STATUS_IS_OK(status
)) {
5955 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp
)));
5961 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5962 * sent/present flags correctly now we've removed them.
5965 if (orig_num_aces
!= 0) {
5967 * Are there any ACE's left ?
5969 if (psd
->dacl
->num_aces
== 0) {
5970 /* No - clear the DACL sent/present flags. */
5971 security_info_sent
&= ~SECINFO_DACL
;
5972 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
5976 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
5977 if (!NT_STATUS_IS_OK(status
)) {
5978 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp
)));
5984 if (fsp
->fh
->fd
!= -1) {
5985 result
= SMB_VFS_FCHMOD(fsp
, ms_nfs_mode
);
5987 result
= SMB_VFS_CHMOD(fsp
->conn
,
5993 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp
),
5994 result
, (unsigned)ms_nfs_mode
,
5996 status
= map_nt_error_from_unix(errno
);
6003 return NT_STATUS_OK
;
6006 static struct vfs_offload_ctx
*fruit_offload_ctx
;
6008 struct fruit_offload_read_state
{
6009 struct vfs_handle_struct
*handle
;
6010 struct tevent_context
*ev
;
6016 static void fruit_offload_read_done(struct tevent_req
*subreq
);
6018 static struct tevent_req
*fruit_offload_read_send(
6019 TALLOC_CTX
*mem_ctx
,
6020 struct tevent_context
*ev
,
6021 struct vfs_handle_struct
*handle
,
6028 struct tevent_req
*req
= NULL
;
6029 struct tevent_req
*subreq
= NULL
;
6030 struct fruit_offload_read_state
*state
= NULL
;
6032 req
= tevent_req_create(mem_ctx
, &state
,
6033 struct fruit_offload_read_state
);
6037 *state
= (struct fruit_offload_read_state
) {
6044 subreq
= SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx
, ev
, handle
, fsp
,
6045 fsctl
, ttl
, offset
, to_copy
);
6046 if (tevent_req_nomem(subreq
, req
)) {
6047 return tevent_req_post(req
, ev
);
6049 tevent_req_set_callback(subreq
, fruit_offload_read_done
, req
);
6053 static void fruit_offload_read_done(struct tevent_req
*subreq
)
6055 struct tevent_req
*req
= tevent_req_callback_data(
6056 subreq
, struct tevent_req
);
6057 struct fruit_offload_read_state
*state
= tevent_req_data(
6058 req
, struct fruit_offload_read_state
);
6061 status
= SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq
,
6065 TALLOC_FREE(subreq
);
6066 if (tevent_req_nterror(req
, status
)) {
6070 if (state
->fsctl
!= FSCTL_SRV_REQUEST_RESUME_KEY
) {
6071 tevent_req_done(req
);
6075 status
= vfs_offload_token_ctx_init(state
->fsp
->conn
->sconn
->client
,
6076 &fruit_offload_ctx
);
6077 if (tevent_req_nterror(req
, status
)) {
6081 status
= vfs_offload_token_db_store_fsp(fruit_offload_ctx
,
6084 if (tevent_req_nterror(req
, status
)) {
6088 tevent_req_done(req
);
6092 static NTSTATUS
fruit_offload_read_recv(struct tevent_req
*req
,
6093 struct vfs_handle_struct
*handle
,
6094 TALLOC_CTX
*mem_ctx
,
6097 struct fruit_offload_read_state
*state
= tevent_req_data(
6098 req
, struct fruit_offload_read_state
);
6101 if (tevent_req_is_nterror(req
, &status
)) {
6102 tevent_req_received(req
);
6106 token
->length
= state
->token
.length
;
6107 token
->data
= talloc_move(mem_ctx
, &state
->token
.data
);
6109 tevent_req_received(req
);
6110 return NT_STATUS_OK
;
6113 struct fruit_offload_write_state
{
6114 struct vfs_handle_struct
*handle
;
6116 struct files_struct
*src_fsp
;
6117 struct files_struct
*dst_fsp
;
6121 static void fruit_offload_write_done(struct tevent_req
*subreq
);
6122 static struct tevent_req
*fruit_offload_write_send(struct vfs_handle_struct
*handle
,
6123 TALLOC_CTX
*mem_ctx
,
6124 struct tevent_context
*ev
,
6127 off_t transfer_offset
,
6128 struct files_struct
*dest_fsp
,
6132 struct tevent_req
*req
, *subreq
;
6133 struct fruit_offload_write_state
*state
;
6135 struct fruit_config_data
*config
;
6136 off_t src_off
= transfer_offset
;
6137 files_struct
*src_fsp
= NULL
;
6138 off_t to_copy
= num
;
6139 bool copyfile_enabled
= false;
6141 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6142 (uintmax_t)src_off
, (uintmax_t)dest_off
, (uintmax_t)num
));
6144 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
6145 struct fruit_config_data
,
6148 req
= tevent_req_create(mem_ctx
, &state
,
6149 struct fruit_offload_write_state
);
6153 state
->handle
= handle
;
6154 state
->dst_fsp
= dest_fsp
;
6157 case FSCTL_SRV_COPYCHUNK
:
6158 case FSCTL_SRV_COPYCHUNK_WRITE
:
6159 copyfile_enabled
= config
->copyfile_enabled
;
6166 * Check if this a OS X copyfile style copychunk request with
6167 * a requested chunk count of 0 that was translated to a
6168 * offload_write_send VFS call overloading the parameters src_off
6169 * = dest_off = num = 0.
6171 if (copyfile_enabled
&& num
== 0 && src_off
== 0 && dest_off
== 0) {
6172 status
= vfs_offload_token_db_fetch_fsp(
6173 fruit_offload_ctx
, token
, &src_fsp
);
6174 if (tevent_req_nterror(req
, status
)) {
6175 return tevent_req_post(req
, ev
);
6177 state
->src_fsp
= src_fsp
;
6179 status
= vfs_stat_fsp(src_fsp
);
6180 if (tevent_req_nterror(req
, status
)) {
6181 return tevent_req_post(req
, ev
);
6184 to_copy
= src_fsp
->fsp_name
->st
.st_ex_size
;
6185 state
->is_copyfile
= true;
6188 subreq
= SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle
,
6197 if (tevent_req_nomem(subreq
, req
)) {
6198 return tevent_req_post(req
, ev
);
6201 tevent_req_set_callback(subreq
, fruit_offload_write_done
, req
);
6205 static void fruit_offload_write_done(struct tevent_req
*subreq
)
6207 struct tevent_req
*req
= tevent_req_callback_data(
6208 subreq
, struct tevent_req
);
6209 struct fruit_offload_write_state
*state
= tevent_req_data(
6210 req
, struct fruit_offload_write_state
);
6212 unsigned int num_streams
= 0;
6213 struct stream_struct
*streams
= NULL
;
6215 struct smb_filename
*src_fname_tmp
= NULL
;
6216 struct smb_filename
*dst_fname_tmp
= NULL
;
6218 status
= SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state
->handle
,
6221 TALLOC_FREE(subreq
);
6222 if (tevent_req_nterror(req
, status
)) {
6226 if (!state
->is_copyfile
) {
6227 tevent_req_done(req
);
6232 * Now copy all remaining streams. We know the share supports
6233 * streams, because we're in vfs_fruit. We don't do this async
6234 * because streams are few and small.
6236 status
= vfs_streaminfo(state
->handle
->conn
, state
->src_fsp
,
6237 state
->src_fsp
->fsp_name
,
6238 req
, &num_streams
, &streams
);
6239 if (tevent_req_nterror(req
, status
)) {
6243 if (num_streams
== 1) {
6244 /* There is always one stream, ::$DATA. */
6245 tevent_req_done(req
);
6249 for (i
= 0; i
< num_streams
; i
++) {
6250 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6251 __func__
, streams
[i
].name
, (size_t)streams
[i
].size
));
6253 src_fname_tmp
= synthetic_smb_fname(
6255 state
->src_fsp
->fsp_name
->base_name
,
6258 state
->src_fsp
->fsp_name
->flags
);
6259 if (tevent_req_nomem(src_fname_tmp
, req
)) {
6263 if (is_ntfs_default_stream_smb_fname(src_fname_tmp
)) {
6264 TALLOC_FREE(src_fname_tmp
);
6268 dst_fname_tmp
= synthetic_smb_fname(
6270 state
->dst_fsp
->fsp_name
->base_name
,
6273 state
->dst_fsp
->fsp_name
->flags
);
6274 if (tevent_req_nomem(dst_fname_tmp
, req
)) {
6275 TALLOC_FREE(src_fname_tmp
);
6279 status
= copy_file(req
,
6280 state
->handle
->conn
,
6283 OPENX_FILE_CREATE_IF_NOT_EXIST
,
6285 if (!NT_STATUS_IS_OK(status
)) {
6286 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__
,
6287 smb_fname_str_dbg(src_fname_tmp
),
6288 smb_fname_str_dbg(dst_fname_tmp
),
6289 nt_errstr(status
)));
6290 TALLOC_FREE(src_fname_tmp
);
6291 TALLOC_FREE(dst_fname_tmp
);
6292 tevent_req_nterror(req
, status
);
6296 TALLOC_FREE(src_fname_tmp
);
6297 TALLOC_FREE(dst_fname_tmp
);
6300 TALLOC_FREE(streams
);
6301 TALLOC_FREE(src_fname_tmp
);
6302 TALLOC_FREE(dst_fname_tmp
);
6303 tevent_req_done(req
);
6306 static NTSTATUS
fruit_offload_write_recv(struct vfs_handle_struct
*handle
,
6307 struct tevent_req
*req
,
6310 struct fruit_offload_write_state
*state
= tevent_req_data(
6311 req
, struct fruit_offload_write_state
);
6314 if (tevent_req_is_nterror(req
, &status
)) {
6315 DEBUG(1, ("server side copy chunk failed: %s\n",
6316 nt_errstr(status
)));
6318 tevent_req_received(req
);
6322 *copied
= state
->copied
;
6323 tevent_req_received(req
);
6325 return NT_STATUS_OK
;
6328 static char *fruit_get_bandsize_line(char **lines
, int numlines
)
6331 static bool re_initialized
= false;
6335 if (!re_initialized
) {
6336 ret
= regcomp(&re
, "^[[:blank:]]*<key>band-size</key>$", 0);
6340 re_initialized
= true;
6343 for (i
= 0; i
< numlines
; i
++) {
6344 regmatch_t matches
[1];
6346 ret
= regexec(&re
, lines
[i
], 1, matches
, 0);
6349 * Check if the match was on the last line, sa we want
6350 * the subsequent line.
6352 if (i
+ 1 == numlines
) {
6355 return lines
[i
+ 1];
6357 if (ret
!= REG_NOMATCH
) {
6365 static bool fruit_get_bandsize_from_line(char *line
, size_t *_band_size
)
6368 static bool re_initialized
= false;
6369 regmatch_t matches
[2];
6374 if (!re_initialized
) {
6377 "<integer>\\([[:digit:]]*\\)</integer>$",
6382 re_initialized
= true;
6385 ret
= regexec(&re
, line
, 2, matches
, 0);
6387 DBG_ERR("regex failed [%s]\n", line
);
6391 line
[matches
[1].rm_eo
] = '\0';
6393 ok
= conv_str_u64(&line
[matches
[1].rm_so
], &band_size
);
6397 *_band_size
= (size_t)band_size
;
6402 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6403 * "band-size" key and value.
6405 static bool fruit_get_bandsize(vfs_handle_struct
*handle
,
6409 #define INFO_PLIST_MAX_SIZE 64*1024
6411 struct smb_filename
*smb_fname
= NULL
;
6412 files_struct
*fsp
= NULL
;
6413 uint8_t *file_data
= NULL
;
6414 char **lines
= NULL
;
6415 char *band_size_line
= NULL
;
6416 size_t plist_file_size
;
6423 plist
= talloc_asprintf(talloc_tos(),
6425 handle
->conn
->connectpath
,
6427 if (plist
== NULL
) {
6432 smb_fname
= synthetic_smb_fname(talloc_tos(), plist
, NULL
, NULL
, 0);
6433 if (smb_fname
== NULL
) {
6438 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
6440 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir
);
6445 plist_file_size
= smb_fname
->st
.st_ex_size
;
6447 if (plist_file_size
> INFO_PLIST_MAX_SIZE
) {
6448 DBG_INFO("%s is too large, ignoring\n", plist
);
6453 status
= SMB_VFS_NEXT_CREATE_FILE(
6456 0, /* root_dir_fid */
6457 smb_fname
, /* fname */
6458 FILE_GENERIC_READ
, /* access_mask */
6459 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
6460 FILE_OPEN
, /* create_disposition */
6461 0, /* create_options */
6462 0, /* file_attributes */
6463 INTERNAL_OPEN_ONLY
, /* oplock_request */
6465 0, /* allocation_size */
6466 0, /* private_flags */
6471 NULL
, NULL
); /* create context */
6472 if (!NT_STATUS_IS_OK(status
)) {
6473 DBG_INFO("Opening [%s] failed [%s]\n",
6474 smb_fname_str_dbg(smb_fname
), nt_errstr(status
));
6479 file_data
= talloc_array(talloc_tos(), uint8_t, plist_file_size
);
6480 if (file_data
== NULL
) {
6485 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, file_data
, plist_file_size
, 0);
6486 if (nread
!= plist_file_size
) {
6487 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6488 fsp_str_dbg(fsp
), nread
, plist_file_size
);
6494 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
6496 if (!NT_STATUS_IS_OK(status
)) {
6497 DBG_ERR("close_file failed: %s\n", nt_errstr(status
));
6502 lines
= file_lines_parse((char *)file_data
,
6506 if (lines
== NULL
) {
6511 band_size_line
= fruit_get_bandsize_line(lines
, numlines
);
6512 if (band_size_line
== NULL
) {
6513 DBG_ERR("Didn't find band-size key in [%s]\n",
6514 smb_fname_str_dbg(smb_fname
));
6519 ok
= fruit_get_bandsize_from_line(band_size_line
, band_size
);
6521 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6525 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size
, plist
);
6529 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
6530 if (!NT_STATUS_IS_OK(status
)) {
6531 DBG_ERR("close_file failed: %s\n", nt_errstr(status
));
6536 TALLOC_FREE(smb_fname
);
6537 TALLOC_FREE(file_data
);
6542 struct fruit_disk_free_state
{
6546 static bool fruit_get_num_bands(vfs_handle_struct
*handle
,
6551 struct smb_filename
*bands_dir
= NULL
;
6553 struct dirent
*e
= NULL
;
6557 path
= talloc_asprintf(talloc_tos(),
6559 handle
->conn
->connectpath
,
6565 bands_dir
= synthetic_smb_fname(talloc_tos(),
6571 if (bands_dir
== NULL
) {
6575 d
= SMB_VFS_NEXT_OPENDIR(handle
, bands_dir
, NULL
, 0);
6577 TALLOC_FREE(bands_dir
);
6583 for (e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
);
6585 e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
))
6587 if (ISDOT(e
->d_name
) || ISDOTDOT(e
->d_name
)) {
6593 ret
= SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
6595 TALLOC_FREE(bands_dir
);
6599 DBG_DEBUG("%zu bands in [%s]\n", nbands
, smb_fname_str_dbg(bands_dir
));
6601 TALLOC_FREE(bands_dir
);
6607 static bool fruit_tmsize_do_dirent(vfs_handle_struct
*handle
,
6608 struct fruit_disk_free_state
*state
,
6613 size_t sparsebundle_strlen
= strlen("sparsebundle");
6614 size_t bandsize
= 0;
6618 p
= strstr(e
->d_name
, "sparsebundle");
6623 if (p
[sparsebundle_strlen
] != '\0') {
6627 DBG_DEBUG("Processing sparsebundle [%s]\n", e
->d_name
);
6629 ok
= fruit_get_bandsize(handle
, e
->d_name
, &bandsize
);
6632 * Beware of race conditions: this may be an uninitialized
6633 * Info.plist that a client is just creating. We don't want let
6634 * this to trigger complete failure.
6636 DBG_ERR("Processing sparsebundle [%s] failed\n", e
->d_name
);
6640 ok
= fruit_get_num_bands(handle
, e
->d_name
, &nbands
);
6643 * Beware of race conditions: this may be a backup sparsebundle
6644 * in an early stage lacking a bands subdirectory. We don't want
6645 * let this to trigger complete failure.
6647 DBG_ERR("Processing sparsebundle [%s] failed\n", e
->d_name
);
6651 if (bandsize
> SIZE_MAX
/nbands
) {
6652 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6656 tm_size
= bandsize
* nbands
;
6658 if (state
->total_size
+ tm_size
< state
->total_size
) {
6659 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6664 state
->total_size
+= tm_size
;
6666 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6667 e
->d_name
, (intmax_t)tm_size
, (intmax_t)state
->total_size
);
6673 * Calculate used size of a TimeMachine volume
6675 * This assumes that the volume is used only for TimeMachine.
6677 * - readdir(basedir of share), then
6678 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6679 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6680 * - count band files in "\1.sparsebundle/bands/"
6681 * - calculate used size of all bands: band_count * band_size
6683 static uint64_t fruit_disk_free(vfs_handle_struct
*handle
,
6684 const struct smb_filename
*smb_fname
,
6689 struct fruit_config_data
*config
= NULL
;
6690 struct fruit_disk_free_state state
= {0};
6692 struct dirent
*e
= NULL
;
6698 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
6699 struct fruit_config_data
,
6702 if (!config
->time_machine
||
6703 config
->time_machine_max_size
== 0)
6705 return SMB_VFS_NEXT_DISK_FREE(handle
,
6712 d
= SMB_VFS_NEXT_OPENDIR(handle
, smb_fname
, NULL
, 0);
6717 for (e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
);
6719 e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
))
6721 ok
= fruit_tmsize_do_dirent(handle
, &state
, e
);
6723 SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
6728 ret
= SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
6733 dsize
= config
->time_machine_max_size
/ 512;
6734 dfree
= dsize
- (state
.total_size
/ 512);
6735 if (dfree
> dsize
) {
6745 static struct vfs_fn_pointers vfs_fruit_fns
= {
6746 .connect_fn
= fruit_connect
,
6747 .disk_free_fn
= fruit_disk_free
,
6749 /* File operations */
6750 .chmod_fn
= fruit_chmod
,
6751 .chown_fn
= fruit_chown
,
6752 .unlink_fn
= fruit_unlink
,
6753 .rename_fn
= fruit_rename
,
6754 .rmdir_fn
= fruit_rmdir
,
6755 .open_fn
= fruit_open
,
6756 .pread_fn
= fruit_pread
,
6757 .pwrite_fn
= fruit_pwrite
,
6758 .pread_send_fn
= fruit_pread_send
,
6759 .pread_recv_fn
= fruit_pread_recv
,
6760 .pwrite_send_fn
= fruit_pwrite_send
,
6761 .pwrite_recv_fn
= fruit_pwrite_recv
,
6762 .stat_fn
= fruit_stat
,
6763 .lstat_fn
= fruit_lstat
,
6764 .fstat_fn
= fruit_fstat
,
6765 .streaminfo_fn
= fruit_streaminfo
,
6766 .ntimes_fn
= fruit_ntimes
,
6767 .ftruncate_fn
= fruit_ftruncate
,
6768 .fallocate_fn
= fruit_fallocate
,
6769 .create_file_fn
= fruit_create_file
,
6770 .readdir_attr_fn
= fruit_readdir_attr
,
6771 .offload_read_send_fn
= fruit_offload_read_send
,
6772 .offload_read_recv_fn
= fruit_offload_read_recv
,
6773 .offload_write_send_fn
= fruit_offload_write_send
,
6774 .offload_write_recv_fn
= fruit_offload_write_recv
,
6776 /* NT ACL operations */
6777 .fget_nt_acl_fn
= fruit_fget_nt_acl
,
6778 .fset_nt_acl_fn
= fruit_fset_nt_acl
,
6782 NTSTATUS
vfs_fruit_init(TALLOC_CTX
*ctx
)
6784 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fruit",
6786 if (!NT_STATUS_IS_OK(ret
)) {
6790 vfs_fruit_debug_level
= debug_add_class("fruit");
6791 if (vfs_fruit_debug_level
== -1) {
6792 vfs_fruit_debug_level
= DBGC_VFS
;
6793 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6796 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6797 "vfs_fruit_init","fruit",vfs_fruit_debug_level
));