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
;
145 * Additional options, all enabled by default,
146 * possibly useful for analyzing performance. The associated
147 * operations with each of them may be expensive, so having
148 * the chance to disable them individually gives a chance
149 * tweaking the setup for the particular usecase.
151 bool readdir_attr_rsize
;
152 bool readdir_attr_finder_info
;
153 bool readdir_attr_max_access
;
156 static const struct enum_list fruit_rsrc
[] = {
157 {FRUIT_RSRC_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_RSRC_ADFILE
, "file"}, /* ._ AppleDouble file */
159 {FRUIT_RSRC_XATTR
, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
163 static const struct enum_list fruit_meta
[] = {
164 {FRUIT_META_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
165 {FRUIT_META_NETATALK
, "netatalk"}, /* Netatalk compatible xattr */
169 static const struct enum_list fruit_locking
[] = {
170 {FRUIT_LOCKING_NETATALK
, "netatalk"}, /* synchronize locks with Netatalk */
171 {FRUIT_LOCKING_NONE
, "none"},
175 static const struct enum_list fruit_encoding
[] = {
176 {FRUIT_ENC_NATIVE
, "native"}, /* map unicode private chars to ASCII */
177 {FRUIT_ENC_PRIVATE
, "private"}, /* keep unicode private chars */
181 static const char *fruit_catia_maps
=
182 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
183 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
184 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
185 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
186 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
187 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
188 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
189 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
190 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
191 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
194 /*****************************************************************************
195 * Defines, functions and data structures that deal with AppleDouble
196 *****************************************************************************/
199 * There are two AppleDouble blobs we deal with:
201 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
202 * metadata in an xattr
204 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
207 typedef enum {ADOUBLE_META
, ADOUBLE_RSRC
} adouble_type_t
;
210 #define AD_VERSION2 0x00020000
211 #define AD_VERSION AD_VERSION2
214 * AppleDouble entry IDs.
216 #define ADEID_DFORK 1
217 #define ADEID_RFORK 2
219 #define ADEID_COMMENT 4
220 #define ADEID_ICONBW 5
221 #define ADEID_ICONCOL 6
222 #define ADEID_FILEI 7
223 #define ADEID_FILEDATESI 8
224 #define ADEID_FINDERI 9
225 #define ADEID_MACFILEI 10
226 #define ADEID_PRODOSFILEI 11
227 #define ADEID_MSDOSFILEI 12
228 #define ADEID_SHORTNAME 13
229 #define ADEID_AFPFILEI 14
232 /* Private Netatalk entries */
233 #define ADEID_PRIVDEV 16
234 #define ADEID_PRIVINO 17
235 #define ADEID_PRIVSYN 18
236 #define ADEID_PRIVID 19
237 #define ADEID_MAX (ADEID_PRIVID + 1)
240 * These are the real ids for the private entries,
241 * as stored in the adouble file
243 #define AD_DEV 0x80444556
244 #define AD_INO 0x80494E4F
245 #define AD_SYN 0x8053594E
246 #define AD_ID 0x8053567E
248 /* Number of actually used entries */
249 #define ADEID_NUM_XATTR 8
250 #define ADEID_NUM_DOT_UND 2
251 #define ADEID_NUM_RSRC_XATTR 1
253 /* AppleDouble magic */
254 #define AD_APPLESINGLE_MAGIC 0x00051600
255 #define AD_APPLEDOUBLE_MAGIC 0x00051607
256 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
258 /* Sizes of relevant entry bits */
259 #define ADEDLEN_MAGIC 4
260 #define ADEDLEN_VERSION 4
261 #define ADEDLEN_FILLER 16
262 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
263 #define ADEDLEN_NENTRIES 2
264 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
265 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
266 #define AD_ENTRY_LEN_EID 4
267 #define AD_ENTRY_LEN_OFF 4
268 #define AD_ENTRY_LEN_LEN 4
269 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
272 #define ADEDLEN_NAME 255
273 #define ADEDLEN_COMMENT 200
274 #define ADEDLEN_FILEI 16
275 #define ADEDLEN_FINDERI 32
276 #define ADEDLEN_FILEDATESI 16
277 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
278 #define ADEDLEN_AFPFILEI 4
279 #define ADEDLEN_MACFILEI 4
280 #define ADEDLEN_PRODOSFILEI 8
281 #define ADEDLEN_MSDOSFILEI 2
282 #define ADEDLEN_DID 4
283 #define ADEDLEN_PRIVDEV 8
284 #define ADEDLEN_PRIVINO 8
285 #define ADEDLEN_PRIVSYN 8
286 #define ADEDLEN_PRIVID 4
289 #define ADEDOFF_MAGIC 0
290 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
291 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
292 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
294 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
295 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
296 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
297 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
298 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
300 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
301 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
302 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
303 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
305 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
306 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
307 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
309 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
310 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
311 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
312 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
313 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
314 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
316 #if AD_DATASZ_XATTR != 402
317 #error bad size for AD_DATASZ_XATTR
320 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
321 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
323 #if AD_DATASZ_DOT_UND != 82
324 #error bad size for AD_DATASZ_DOT_UND
328 * Sharemode locks fcntl() offsets
330 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
331 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
333 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
335 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
337 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
338 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
339 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
340 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
341 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
342 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
343 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
344 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
345 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
346 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
348 /* Time stuff we overload the bits a little */
349 #define AD_DATE_CREATE 0
350 #define AD_DATE_MODIFY 4
351 #define AD_DATE_BACKUP 8
352 #define AD_DATE_ACCESS 12
353 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
354 AD_DATE_BACKUP | AD_DATE_ACCESS)
355 #define AD_DATE_UNIX (1 << 10)
356 #define AD_DATE_START 0x80000000
357 #define AD_DATE_DELTA 946684800
358 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
359 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
361 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
362 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
363 #define AD_XATTR_HDR_SIZE 36
364 #define AD_XATTR_MAX_HDR_SIZE 65536
366 /* Accessor macros */
367 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
368 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
369 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
370 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
373 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
374 * representation as well as the on-disk format.
376 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
377 * the length of the FinderInfo entry is larger then 32 bytes. It is then
378 * preceeded with 2 bytes padding.
380 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
383 struct ad_xattr_header
{
384 uint32_t adx_magic
; /* ATTR_HDR_MAGIC */
385 uint32_t adx_debug_tag
; /* for debugging == file id of owning file */
386 uint32_t adx_total_size
; /* file offset of end of attribute header + entries + data */
387 uint32_t adx_data_start
; /* file offset to attribute data area */
388 uint32_t adx_data_length
; /* length of attribute data area */
389 uint32_t adx_reserved
[3];
391 uint16_t adx_num_attrs
;
394 /* On-disk entries are aligned on 4 byte boundaries */
395 struct ad_xattr_entry
{
396 uint32_t adx_offset
; /* file offset to data */
397 uint32_t adx_length
; /* size of attribute data */
399 uint8_t adx_namelen
; /* included the NULL terminator */
400 char *adx_name
; /* NULL-terminated UTF-8 name */
409 vfs_handle_struct
*ad_handle
;
412 adouble_type_t ad_type
;
415 struct ad_entry ad_eid
[ADEID_MAX
];
417 struct ad_xattr_header adx_header
;
418 struct ad_xattr_entry
*adx_entries
;
421 struct ad_entry_order
{
422 uint32_t id
, offset
, len
;
425 /* Netatalk AppleDouble metadata xattr */
427 struct ad_entry_order entry_order_meta_xattr
[ADEID_NUM_XATTR
+ 1] = {
428 {ADEID_FINDERI
, ADEDOFF_FINDERI_XATTR
, ADEDLEN_FINDERI
},
429 {ADEID_COMMENT
, ADEDOFF_COMMENT_XATTR
, 0},
430 {ADEID_FILEDATESI
, ADEDOFF_FILEDATESI_XATTR
, ADEDLEN_FILEDATESI
},
431 {ADEID_AFPFILEI
, ADEDOFF_AFPFILEI_XATTR
, ADEDLEN_AFPFILEI
},
432 {ADEID_PRIVDEV
, ADEDOFF_PRIVDEV_XATTR
, 0},
433 {ADEID_PRIVINO
, ADEDOFF_PRIVINO_XATTR
, 0},
434 {ADEID_PRIVSYN
, ADEDOFF_PRIVSYN_XATTR
, 0},
435 {ADEID_PRIVID
, ADEDOFF_PRIVID_XATTR
, 0},
439 /* AppleDouble resource fork file (the ones prefixed by "._") */
441 struct ad_entry_order entry_order_dot_und
[ADEID_NUM_DOT_UND
+ 1] = {
442 {ADEID_FINDERI
, ADEDOFF_FINDERI_DOT_UND
, ADEDLEN_FINDERI
},
443 {ADEID_RFORK
, ADEDOFF_RFORK_DOT_UND
, 0},
448 * Fake AppleDouble entry oder for resource fork xattr. The xattr
449 * isn't an AppleDouble file, it simply contains the resource data,
450 * but in order to be able to use some API calls like ad_getentryoff()
451 * we build a fake/helper struct adouble with this entry order struct.
454 struct ad_entry_order entry_order_rsrc_xattr
[ADEID_NUM_RSRC_XATTR
+ 1] = {
459 /* Conversion from enumerated id to on-disk AppleDouble id */
460 #define AD_EID_DISK(a) (set_eid[a])
461 static const uint32_t set_eid
[] = {
462 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
463 AD_DEV
, AD_INO
, AD_SYN
, AD_ID
467 /* tcon config handle */
468 struct fruit_config_data
*config
;
470 /* Denote stream type, meta or rsrc */
475 * Forward declarations
477 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
478 adouble_type_t type
);
479 static int ad_set(struct adouble
*ad
, const struct smb_filename
*smb_fname
);
480 static int ad_fset(struct adouble
*ad
, files_struct
*fsp
);
481 static int adouble_path(TALLOC_CTX
*ctx
,
482 const struct smb_filename
*smb_fname__in
,
483 struct smb_filename
**ppsmb_fname_out
);
484 static AfpInfo
*afpinfo_new(TALLOC_CTX
*ctx
);
485 static ssize_t
afpinfo_pack(const AfpInfo
*ai
, char *buf
);
486 static AfpInfo
*afpinfo_unpack(TALLOC_CTX
*ctx
, const void *data
);
490 * Return a pointer to an AppleDouble entry
492 * Returns NULL if the entry is not present
494 static char *ad_get_entry(const struct adouble
*ad
, int eid
)
496 off_t off
= ad_getentryoff(ad
, eid
);
497 size_t len
= ad_getentrylen(ad
, eid
);
499 if (off
== 0 || len
== 0) {
503 return ad
->ad_data
+ off
;
509 static int ad_getdate(const struct adouble
*ad
,
510 unsigned int dateoff
,
513 bool xlate
= (dateoff
& AD_DATE_UNIX
);
516 dateoff
&= AD_DATE_MASK
;
517 p
= ad_get_entry(ad
, ADEID_FILEDATESI
);
522 if (dateoff
> AD_DATE_ACCESS
) {
526 memcpy(date
, p
+ dateoff
, sizeof(uint32_t));
529 *date
= AD_DATE_TO_UNIX(*date
);
537 static int ad_setdate(struct adouble
*ad
, unsigned int dateoff
, uint32_t date
)
539 bool xlate
= (dateoff
& AD_DATE_UNIX
);
542 p
= ad_get_entry(ad
, ADEID_FILEDATESI
);
547 dateoff
&= AD_DATE_MASK
;
549 date
= AD_DATE_FROM_UNIX(date
);
552 if (dateoff
> AD_DATE_ACCESS
) {
556 memcpy(p
+ dateoff
, &date
, sizeof(date
));
563 * Map on-disk AppleDouble id to enumerated id
565 static uint32_t get_eid(uint32_t eid
)
573 return ADEID_PRIVDEV
;
575 return ADEID_PRIVINO
;
577 return ADEID_PRIVSYN
;
588 * Pack AppleDouble structure into data buffer
590 static bool ad_pack(struct adouble
*ad
)
597 bufsize
= talloc_get_size(ad
->ad_data
);
598 if (bufsize
< AD_DATASZ_DOT_UND
) {
599 DBG_ERR("bad buffer size [0x%" PRIx32
"]\n", bufsize
);
603 if (offset
+ ADEDLEN_MAGIC
< offset
||
604 offset
+ ADEDLEN_MAGIC
>= bufsize
) {
607 RSIVAL(ad
->ad_data
, offset
, ad
->ad_magic
);
608 offset
+= ADEDLEN_MAGIC
;
610 if (offset
+ ADEDLEN_VERSION
< offset
||
611 offset
+ ADEDLEN_VERSION
>= bufsize
) {
614 RSIVAL(ad
->ad_data
, offset
, ad
->ad_version
);
615 offset
+= ADEDLEN_VERSION
;
617 if (offset
+ ADEDLEN_FILLER
< offset
||
618 offset
+ ADEDLEN_FILLER
>= bufsize
) {
621 if (ad
->ad_type
== ADOUBLE_RSRC
) {
622 memcpy(ad
->ad_data
+ offset
, AD_FILLER_TAG
, ADEDLEN_FILLER
);
624 offset
+= ADEDLEN_FILLER
;
626 if (offset
+ ADEDLEN_NENTRIES
< offset
||
627 offset
+ ADEDLEN_NENTRIES
>= bufsize
) {
630 offset
+= ADEDLEN_NENTRIES
;
632 for (eid
= 0, nent
= 0; eid
< ADEID_MAX
; eid
++) {
633 if (ad
->ad_eid
[eid
].ade_off
== 0) {
635 * ade_off is also used as indicator whether a
636 * specific entry is used or not
641 if (offset
+ AD_ENTRY_LEN_EID
< offset
||
642 offset
+ AD_ENTRY_LEN_EID
>= bufsize
) {
645 RSIVAL(ad
->ad_data
, offset
, AD_EID_DISK(eid
));
646 offset
+= AD_ENTRY_LEN_EID
;
648 if (offset
+ AD_ENTRY_LEN_OFF
< offset
||
649 offset
+ AD_ENTRY_LEN_OFF
>= bufsize
) {
652 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_off
);
653 offset
+= AD_ENTRY_LEN_OFF
;
655 if (offset
+ AD_ENTRY_LEN_LEN
< offset
||
656 offset
+ AD_ENTRY_LEN_LEN
>= bufsize
) {
659 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_len
);
660 offset
+= AD_ENTRY_LEN_LEN
;
665 if (ADEDOFF_NENTRIES
+ 2 >= bufsize
) {
668 RSSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
, nent
);
673 static bool ad_unpack_xattrs(struct adouble
*ad
)
675 struct ad_xattr_header
*h
= &ad
->adx_header
;
676 const char *p
= ad
->ad_data
;
680 if (ad_getentrylen(ad
, ADEID_FINDERI
) <= ADEDLEN_FINDERI
) {
684 /* 2 bytes padding */
685 hoff
= ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
+ 2;
687 h
->adx_magic
= RIVAL(p
, hoff
+ 0);
688 h
->adx_debug_tag
= RIVAL(p
, hoff
+ 4); /* Not used -> not checked */
689 h
->adx_total_size
= RIVAL(p
, hoff
+ 8);
690 h
->adx_data_start
= RIVAL(p
, hoff
+ 12);
691 h
->adx_data_length
= RIVAL(p
, hoff
+ 16);
692 h
->adx_flags
= RSVAL(p
, hoff
+ 32); /* Not used -> not checked */
693 h
->adx_num_attrs
= RSVAL(p
, hoff
+ 34);
695 if (h
->adx_magic
!= AD_XATTR_HDR_MAGIC
) {
696 DBG_ERR("Bad magic: 0x%" PRIx32
"\n", h
->adx_magic
);
700 if (h
->adx_total_size
> ad_getentryoff(ad
, ADEID_RFORK
)) {
701 DBG_ERR("Bad total size: 0x%" PRIx32
"\n", h
->adx_total_size
);
704 if (h
->adx_total_size
> AD_XATTR_MAX_HDR_SIZE
) {
705 DBG_ERR("Bad total size: 0x%" PRIx32
"\n", h
->adx_total_size
);
709 if (h
->adx_data_start
< (hoff
+ AD_XATTR_HDR_SIZE
)) {
710 DBG_ERR("Bad start: 0x%" PRIx32
"\n", h
->adx_data_start
);
714 if ((h
->adx_data_start
+ h
->adx_data_length
) < h
->adx_data_start
) {
715 DBG_ERR("Bad length: %" PRIu32
"\n", h
->adx_data_length
);
718 if ((h
->adx_data_start
+ h
->adx_data_length
) >
719 ad
->adx_header
.adx_total_size
)
721 DBG_ERR("Bad length: %" PRIu32
"\n", h
->adx_data_length
);
725 if (h
->adx_num_attrs
> AD_XATTR_MAX_ENTRIES
) {
726 DBG_ERR("Bad num xattrs: %" PRIu16
"\n", h
->adx_num_attrs
);
730 if (h
->adx_num_attrs
== 0) {
734 ad
->adx_entries
= talloc_zero_array(
735 ad
, struct ad_xattr_entry
, h
->adx_num_attrs
);
736 if (ad
->adx_entries
== NULL
) {
740 hoff
+= AD_XATTR_HDR_SIZE
;
742 for (i
= 0; i
< h
->adx_num_attrs
; i
++) {
743 struct ad_xattr_entry
*e
= &ad
->adx_entries
[i
];
745 hoff
= (hoff
+ 3) & ~3;
747 e
->adx_offset
= RIVAL(p
, hoff
+ 0);
748 e
->adx_length
= RIVAL(p
, hoff
+ 4);
749 e
->adx_flags
= RSVAL(p
, hoff
+ 8);
750 e
->adx_namelen
= *(p
+ hoff
+ 10);
752 if (e
->adx_offset
>= ad
->adx_header
.adx_total_size
) {
753 DBG_ERR("Bad adx_offset: %" PRIx32
"\n",
758 if ((e
->adx_offset
+ e
->adx_length
) < e
->adx_offset
) {
759 DBG_ERR("Bad adx_length: %" PRIx32
"\n",
764 if ((e
->adx_offset
+ e
->adx_length
) >
765 ad
->adx_header
.adx_total_size
)
767 DBG_ERR("Bad adx_length: %" PRIx32
"\n",
772 if (e
->adx_namelen
== 0) {
773 DBG_ERR("Bad adx_namelen: %" PRIx32
"\n",
777 if ((hoff
+ 11 + e
->adx_namelen
) < hoff
+ 11) {
778 DBG_ERR("Bad adx_namelen: %" PRIx32
"\n",
782 if ((hoff
+ 11 + e
->adx_namelen
) >
783 ad
->adx_header
.adx_data_start
)
785 DBG_ERR("Bad adx_namelen: %" PRIx32
"\n",
790 e
->adx_name
= talloc_strndup(ad
->adx_entries
,
793 if (e
->adx_name
== NULL
) {
797 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
798 e
->adx_name
, e
->adx_offset
, e
->adx_length
);
799 dump_data(10, (uint8_t *)(ad
->ad_data
+ e
->adx_offset
),
802 hoff
+= 11 + e
->adx_namelen
;
809 * Unpack an AppleDouble blob into a struct adoble
811 static bool ad_unpack(struct adouble
*ad
, const size_t nentries
,
814 size_t bufsize
= talloc_get_size(ad
->ad_data
);
816 uint32_t eid
, len
, off
;
820 * The size of the buffer ad->ad_data is checked when read, so
821 * we wouldn't have to check our own offsets, a few extra
822 * checks won't hurt though. We have to check the offsets we
823 * read from the buffer anyway.
826 if (bufsize
< (AD_HEADER_LEN
+ (AD_ENTRY_LEN
* nentries
))) {
827 DEBUG(1, ("bad size\n"));
831 ad
->ad_magic
= RIVAL(ad
->ad_data
, 0);
832 ad
->ad_version
= RIVAL(ad
->ad_data
, ADEDOFF_VERSION
);
833 if ((ad
->ad_magic
!= AD_MAGIC
) || (ad
->ad_version
!= AD_VERSION
)) {
834 DEBUG(1, ("wrong magic or version\n"));
838 adentries
= RSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
);
839 if (adentries
!= nentries
) {
840 DEBUG(1, ("invalid number of entries: %zu\n",
845 /* now, read in the entry bits */
846 for (i
= 0; i
< adentries
; i
++) {
847 eid
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
));
849 off
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 4);
850 len
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 8);
852 if (!eid
|| eid
> ADEID_MAX
) {
853 DEBUG(1, ("bogus eid %d\n", eid
));
858 * All entries other than the resource fork are
859 * expected to be read into the ad_data buffer, so
860 * ensure the specified offset is within that bound
862 if ((off
> bufsize
) && (eid
!= ADEID_RFORK
)) {
863 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
869 * All entries besides FinderInfo and resource fork
870 * must fit into the buffer. FinderInfo is special as
871 * it may be larger then the default 32 bytes (if it
872 * contains marshalled xattrs), but we will fixup that
873 * in ad_convert(). And the resource fork is never
874 * accessed directly by the ad_data buf (also see
875 * comment above) anyway.
877 if ((eid
!= ADEID_RFORK
) &&
878 (eid
!= ADEID_FINDERI
) &&
879 ((off
+ len
) > bufsize
)) {
880 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
886 * That would be obviously broken
888 if (off
> filesize
) {
889 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
895 * Check for any entry that has its end beyond the
898 if (off
+ len
< off
) {
899 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
900 ", len: %" PRIu32
"\n",
905 if (off
+ len
> filesize
) {
907 * If this is the resource fork entry, we fix
908 * up the length, for any other entry we bail
911 if (eid
!= ADEID_RFORK
) {
912 DEBUG(1, ("bogus eid %d: off: %" PRIu32
913 ", len: %" PRIu32
"\n",
919 * Fixup the resource fork entry by limiting
920 * the size to entryoffset - filesize.
922 len
= filesize
- off
;
923 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
924 ", len: %" PRIu32
"\n", off
, len
));
927 ad
->ad_eid
[eid
].ade_off
= off
;
928 ad
->ad_eid
[eid
].ade_len
= len
;
931 ok
= ad_unpack_xattrs(ad
);
939 static bool ad_convert_xattr(struct adouble
*ad
,
940 const struct smb_filename
*smb_fname
,
943 static struct char_mappings
**string_replace_cmaps
= NULL
;
948 if (ad
->adx_header
.adx_num_attrs
== 0) {
952 if (string_replace_cmaps
== NULL
) {
953 const char **mappings
= NULL
;
955 mappings
= str_list_make_v3_const(
956 talloc_tos(), fruit_catia_maps
, NULL
);
957 if (mappings
== NULL
) {
960 string_replace_cmaps
= string_replace_init_map(mappings
);
961 TALLOC_FREE(mappings
);
964 for (i
= 0; i
< ad
->adx_header
.adx_num_attrs
; i
++) {
965 struct ad_xattr_entry
*e
= &ad
->adx_entries
[i
];
966 char *mapped_name
= NULL
;
968 struct smb_filename
*stream_name
= NULL
;
969 files_struct
*fsp
= NULL
;
972 status
= string_replace_allocate(ad
->ad_handle
->conn
,
974 string_replace_cmaps
,
977 vfs_translate_to_windows
);
978 if (!NT_STATUS_IS_OK(status
) &&
979 !NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
))
981 DBG_ERR("string_replace_allocate failed\n");
986 mapped_name
= talloc_asprintf(talloc_tos(), ":%s", tmp
);
988 if (mapped_name
== NULL
) {
992 stream_name
= synthetic_smb_fname(talloc_tos(),
993 smb_fname
->base_name
,
997 TALLOC_FREE(mapped_name
);
998 if (stream_name
== NULL
) {
999 DBG_ERR("synthetic_smb_fname failed\n");
1003 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name
));
1005 status
= SMB_VFS_CREATE_FILE(
1006 ad
->ad_handle
->conn
, /* conn */
1008 0, /* root_dir_fid */
1009 stream_name
, /* fname */
1010 FILE_GENERIC_WRITE
, /* access_mask */
1011 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
1012 FILE_OPEN_IF
, /* create_disposition */
1013 0, /* create_options */
1014 0, /* file_attributes */
1015 INTERNAL_OPEN_ONLY
, /* oplock_request */
1017 0, /* allocation_size */
1018 0, /* private_flags */
1023 NULL
, NULL
); /* create context */
1024 TALLOC_FREE(stream_name
);
1025 if (!NT_STATUS_IS_OK(status
)) {
1026 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1030 nwritten
= SMB_VFS_PWRITE(fsp
,
1031 map
+ e
->adx_offset
,
1034 if (nwritten
== -1) {
1035 DBG_ERR("SMB_VFS_PWRITE failed\n");
1036 saved_errno
= errno
;
1037 close_file(NULL
, fsp
, ERROR_CLOSE
);
1038 errno
= saved_errno
;
1042 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
1043 if (!NT_STATUS_IS_OK(status
)) {
1053 * Convert from Apple's ._ file to Netatalk
1055 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1056 * bytes containing packed xattrs. Netatalk can't deal with that, so
1057 * we simply discard the packed xattrs.
1059 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1062 static int ad_convert(struct adouble
*ad
,
1063 const struct smb_filename
*smb_fname
,
1067 char *map
= MAP_FAILED
;
1071 origlen
= ad_getentryoff(ad
, ADEID_RFORK
) +
1072 ad_getentrylen(ad
, ADEID_RFORK
);
1074 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1075 map
= mmap(NULL
, origlen
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
1076 if (map
== MAP_FAILED
) {
1077 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno
)));
1082 ok
= ad_convert_xattr(ad
, smb_fname
, map
);
1087 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
1088 memmove(map
+ ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
,
1089 map
+ ad_getentryoff(ad
, ADEID_RFORK
),
1090 ad_getentrylen(ad
, ADEID_RFORK
));
1093 ad_setentrylen(ad
, ADEID_FINDERI
, ADEDLEN_FINDERI
);
1094 ad_setentryoff(ad
, ADEID_RFORK
,
1095 ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
);
1098 * FIXME: direct ftruncate(), but we don't have a fsp for the
1101 rc
= ftruncate(fd
, ad_getentryoff(ad
, ADEID_RFORK
)
1102 + ad_getentrylen(ad
, ADEID_RFORK
));
1105 if (map
!= MAP_FAILED
) {
1106 munmap(map
, origlen
);
1112 * Read and parse Netatalk AppleDouble metadata xattr
1114 static ssize_t
ad_read_meta(struct adouble
*ad
,
1115 const struct smb_filename
*smb_fname
)
1121 DEBUG(10, ("reading meta xattr for %s\n", smb_fname
->base_name
));
1123 ealen
= SMB_VFS_GETXATTR(ad
->ad_handle
->conn
, smb_fname
,
1124 AFPINFO_EA_NETATALK
, ad
->ad_data
,
1130 if (errno
== ENOATTR
) {
1136 DEBUG(2, ("error reading meta xattr: %s\n",
1142 if (ealen
!= AD_DATASZ_XATTR
) {
1143 DEBUG(2, ("bad size %zd\n", ealen
));
1149 /* Now parse entries */
1150 ok
= ad_unpack(ad
, ADEID_NUM_XATTR
, AD_DATASZ_XATTR
);
1152 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1158 if (!ad_getentryoff(ad
, ADEID_FINDERI
)
1159 || !ad_getentryoff(ad
, ADEID_COMMENT
)
1160 || !ad_getentryoff(ad
, ADEID_FILEDATESI
)
1161 || !ad_getentryoff(ad
, ADEID_AFPFILEI
)
1162 || !ad_getentryoff(ad
, ADEID_PRIVDEV
)
1163 || !ad_getentryoff(ad
, ADEID_PRIVINO
)
1164 || !ad_getentryoff(ad
, ADEID_PRIVSYN
)
1165 || !ad_getentryoff(ad
, ADEID_PRIVID
)) {
1166 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1173 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1174 smb_fname
->base_name
, rc
));
1178 if (errno
== EINVAL
) {
1180 removexattr(smb_fname
->base_name
, AFPINFO_EA_NETATALK
);
1188 static int ad_open_rsrc_xattr(const struct smb_filename
*smb_fname
,
1192 #ifdef HAVE_ATTROPEN
1193 /* FIXME: direct Solaris xattr syscall */
1194 return attropen(smb_fname
->base_name
,
1195 AFPRESOURCE_EA_NETATALK
, flags
, mode
);
1202 static int ad_open_rsrc_adouble(const struct smb_filename
*smb_fname
,
1208 struct smb_filename
*adp_smb_fname
= NULL
;
1210 ret
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
1215 fd
= open(adp_smb_fname
->base_name
, flags
, mode
);
1216 TALLOC_FREE(adp_smb_fname
);
1221 static int ad_open_rsrc(vfs_handle_struct
*handle
,
1222 const struct smb_filename
*smb_fname
,
1226 struct fruit_config_data
*config
= NULL
;
1229 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1230 struct fruit_config_data
, return -1);
1232 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
1233 fd
= ad_open_rsrc_xattr(smb_fname
, flags
, mode
);
1235 fd
= ad_open_rsrc_adouble(smb_fname
, flags
, mode
);
1242 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1243 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1244 * for file IO on the ._ file.
1246 static int ad_open(vfs_handle_struct
*handle
,
1249 const struct smb_filename
*smb_fname
,
1255 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname
->base_name
,
1256 ad
->ad_type
== ADOUBLE_META
? "meta" : "rsrc");
1258 if (ad
->ad_type
== ADOUBLE_META
) {
1262 if ((fsp
!= NULL
) && (fsp
->fh
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
1263 ad
->ad_fd
= fsp
->fh
->fd
;
1264 ad
->ad_opened
= false;
1268 fd
= ad_open_rsrc(handle
, smb_fname
, flags
, mode
);
1272 ad
->ad_opened
= true;
1275 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1276 smb_fname
->base_name
,
1277 ad
->ad_type
== ADOUBLE_META
? "meta" : "rsrc", fd
);
1282 static ssize_t
ad_read_rsrc_xattr(struct adouble
*ad
)
1287 /* FIXME: direct sys_fstat(), don't have an fsp */
1288 ret
= sys_fstat(ad
->ad_fd
, &st
,
1289 lp_fake_directory_create_times(
1290 SNUM(ad
->ad_handle
->conn
)));
1295 ad_setentrylen(ad
, ADEID_RFORK
, st
.st_ex_size
);
1296 return st
.st_ex_size
;
1299 static ssize_t
ad_read_rsrc_adouble(struct adouble
*ad
,
1300 const struct smb_filename
*smb_fname
)
1302 SMB_STRUCT_STAT sbuf
;
1306 struct smb_filename
*stream_name
= NULL
;
1307 files_struct
*fsp
= NULL
;
1312 int saved_errno
= 0;
1316 ret
= sys_fstat(ad
->ad_fd
, &sbuf
, lp_fake_directory_create_times(
1317 SNUM(ad
->ad_handle
->conn
)));
1323 * AppleDouble file header content and size, two cases:
1325 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1326 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1328 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1330 size
= sbuf
.st_ex_size
;
1331 if (size
> talloc_array_length(ad
->ad_data
)) {
1332 if (size
> AD_XATTR_MAX_HDR_SIZE
) {
1333 size
= AD_XATTR_MAX_HDR_SIZE
;
1335 p_ad
= talloc_realloc(ad
, ad
->ad_data
, char, size
);
1342 len
= sys_pread(ad
->ad_fd
, ad
->ad_data
,
1343 talloc_array_length(ad
->ad_data
), 0);
1344 if (len
!= talloc_array_length(ad
->ad_data
)) {
1345 DBG_NOTICE("%s %s: bad size: %zd\n",
1346 smb_fname
->base_name
, strerror(errno
), len
);
1350 /* Now parse entries */
1351 ok
= ad_unpack(ad
, ADEID_NUM_DOT_UND
, sbuf
.st_ex_size
);
1353 DBG_ERR("invalid AppleDouble resource %s\n",
1354 smb_fname
->base_name
);
1359 if ((ad_getentryoff(ad
, ADEID_FINDERI
) != ADEDOFF_FINDERI_DOT_UND
)
1360 || (ad_getentrylen(ad
, ADEID_FINDERI
) < ADEDLEN_FINDERI
)
1361 || (ad_getentryoff(ad
, ADEID_RFORK
) < ADEDOFF_RFORK_DOT_UND
)) {
1362 DBG_ERR("invalid AppleDouble resource %s\n",
1363 smb_fname
->base_name
);
1368 if (ad_getentrylen(ad
, ADEID_FINDERI
) == ADEDLEN_FINDERI
) {
1373 * Try to fixup AppleDouble files created by OS X with xattrs
1374 * appended to the ADEID_FINDERI entry. We simply remove the
1375 * xattrs blob, this means any fancy xattr that was stored
1379 ret
= ad_convert(ad
, smb_fname
, ad
->ad_fd
);
1381 DBG_WARNING("Failed to convert [%s]\n", smb_fname
->base_name
);
1387 DBG_WARNING("ad_pack [%s] failed\n", smb_fname
->base_name
);
1391 len
= sys_pwrite(ad
->ad_fd
, ad
->ad_data
, AD_DATASZ_DOT_UND
, 0);
1392 if (len
!= AD_DATASZ_DOT_UND
) {
1393 DBG_ERR("%s: bad size: %zd\n", smb_fname
->base_name
, len
);
1397 p_ad
= ad_get_entry(ad
, ADEID_FINDERI
);
1402 ai
= afpinfo_new(talloc_tos());
1407 memcpy(ai
->afpi_FinderInfo
, p_ad
, ADEDLEN_FINDERI
);
1409 aiblob
= data_blob_talloc(talloc_tos(), NULL
, AFP_INFO_SIZE
);
1410 if (aiblob
.data
== NULL
) {
1415 size
= afpinfo_pack(ai
, (char *)aiblob
.data
);
1417 if (size
!= AFP_INFO_SIZE
) {
1421 stream_name
= synthetic_smb_fname(talloc_tos(),
1422 smb_fname
->base_name
,
1426 if (stream_name
== NULL
) {
1427 data_blob_free(&aiblob
);
1428 DBG_ERR("synthetic_smb_fname failed\n");
1432 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name
));
1434 status
= SMB_VFS_CREATE_FILE(
1435 ad
->ad_handle
->conn
, /* conn */
1437 0, /* root_dir_fid */
1438 stream_name
, /* fname */
1439 FILE_GENERIC_WRITE
, /* access_mask */
1440 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
1441 FILE_OPEN_IF
, /* create_disposition */
1442 0, /* create_options */
1443 0, /* file_attributes */
1444 INTERNAL_OPEN_ONLY
, /* oplock_request */
1446 0, /* allocation_size */
1447 0, /* private_flags */
1452 NULL
, NULL
); /* create context */
1453 TALLOC_FREE(stream_name
);
1454 if (!NT_STATUS_IS_OK(status
)) {
1455 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1459 nwritten
= SMB_VFS_PWRITE(fsp
,
1463 if (nwritten
== -1) {
1464 DBG_ERR("SMB_VFS_PWRITE failed\n");
1465 saved_errno
= errno
;
1466 close_file(NULL
, fsp
, ERROR_CLOSE
);
1467 errno
= saved_errno
;
1471 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
1472 if (!NT_STATUS_IS_OK(status
)) {
1481 * Read and parse resource fork, either ._ AppleDouble file or xattr
1483 static ssize_t
ad_read_rsrc(struct adouble
*ad
,
1484 const struct smb_filename
*smb_fname
)
1486 struct fruit_config_data
*config
= NULL
;
1489 SMB_VFS_HANDLE_GET_DATA(ad
->ad_handle
, config
,
1490 struct fruit_config_data
, return -1);
1492 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
1493 len
= ad_read_rsrc_xattr(ad
);
1495 len
= ad_read_rsrc_adouble(ad
, smb_fname
);
1502 * Read and unpack an AppleDouble metadata xattr or resource
1504 static ssize_t
ad_read(struct adouble
*ad
, const struct smb_filename
*smb_fname
)
1506 switch (ad
->ad_type
) {
1508 return ad_read_meta(ad
, smb_fname
);
1510 return ad_read_rsrc(ad
, smb_fname
);
1516 static int adouble_destructor(struct adouble
*ad
)
1518 if ((ad
->ad_fd
!= -1) && ad
->ad_opened
) {
1526 * Allocate a struct adouble without initialiing it
1528 * The struct is either hang of the fsp extension context or if fsp is
1531 * @param[in] ctx talloc context
1532 * @param[in] handle vfs handle
1533 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1535 * @return adouble handle
1537 static struct adouble
*ad_alloc(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1538 adouble_type_t type
)
1543 struct fruit_config_data
*config
;
1545 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1546 struct fruit_config_data
, return NULL
);
1550 adsize
= AD_DATASZ_XATTR
;
1553 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1554 adsize
= AD_DATASZ_DOT_UND
;
1561 ad
= talloc_zero(ctx
, struct adouble
);
1568 ad
->ad_data
= talloc_zero_array(ad
, char, adsize
);
1569 if (ad
->ad_data
== NULL
) {
1575 ad
->ad_handle
= handle
;
1577 ad
->ad_magic
= AD_MAGIC
;
1578 ad
->ad_version
= AD_VERSION
;
1581 talloc_set_destructor(ad
, adouble_destructor
);
1591 * Allocate and initialize a new struct adouble
1593 * @param[in] ctx talloc context
1594 * @param[in] handle vfs handle
1595 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1597 * @return adouble handle, initialized
1599 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1600 adouble_type_t type
)
1603 const struct ad_entry_order
*eid
;
1604 struct adouble
*ad
= NULL
;
1605 struct fruit_config_data
*config
;
1606 time_t t
= time(NULL
);
1608 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1609 struct fruit_config_data
, return NULL
);
1613 eid
= entry_order_meta_xattr
;
1616 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1617 eid
= entry_order_dot_und
;
1619 eid
= entry_order_rsrc_xattr
;
1626 ad
= ad_alloc(ctx
, handle
, type
);
1632 ad
->ad_eid
[eid
->id
].ade_off
= eid
->offset
;
1633 ad
->ad_eid
[eid
->id
].ade_len
= eid
->len
;
1637 /* put something sane in the date fields */
1638 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
, t
);
1639 ad_setdate(ad
, AD_DATE_MODIFY
| AD_DATE_UNIX
, t
);
1640 ad_setdate(ad
, AD_DATE_ACCESS
| AD_DATE_UNIX
, t
);
1641 ad_setdate(ad
, AD_DATE_BACKUP
, htonl(AD_DATE_START
));
1649 static struct adouble
*ad_get_internal(TALLOC_CTX
*ctx
,
1650 vfs_handle_struct
*handle
,
1652 const struct smb_filename
*smb_fname
,
1653 adouble_type_t type
)
1657 struct adouble
*ad
= NULL
;
1661 smb_fname
= fsp
->base_fsp
->fsp_name
;
1664 DEBUG(10, ("ad_get(%s) called for %s\n",
1665 type
== ADOUBLE_META
? "meta" : "rsrc",
1666 smb_fname
->base_name
));
1668 ad
= ad_alloc(ctx
, handle
, type
);
1674 /* Try rw first so we can use the fd in ad_convert() */
1677 rc
= ad_open(handle
, ad
, fsp
, smb_fname
, mode
, 0);
1678 if (rc
== -1 && ((errno
== EROFS
) || (errno
== EACCES
))) {
1680 rc
= ad_open(handle
, ad
, fsp
, smb_fname
, mode
, 0);
1683 DBG_DEBUG("ad_open [%s] error [%s]\n",
1684 smb_fname
->base_name
, strerror(errno
));
1689 len
= ad_read(ad
, smb_fname
);
1691 DEBUG(10, ("error reading AppleDouble for %s\n",
1692 smb_fname
->base_name
));
1698 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1699 type
== ADOUBLE_META
? "meta" : "rsrc",
1700 smb_fname
->base_name
, rc
));
1709 * Return AppleDouble data for a file
1711 * @param[in] ctx talloc context
1712 * @param[in] handle vfs handle
1713 * @param[in] smb_fname pathname to file or directory
1714 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1716 * @return talloced struct adouble or NULL on error
1718 static struct adouble
*ad_get(TALLOC_CTX
*ctx
,
1719 vfs_handle_struct
*handle
,
1720 const struct smb_filename
*smb_fname
,
1721 adouble_type_t type
)
1723 return ad_get_internal(ctx
, handle
, NULL
, smb_fname
, type
);
1727 * Return AppleDouble data for a file
1729 * @param[in] ctx talloc context
1730 * @param[in] handle vfs handle
1731 * @param[in] fsp fsp to use for IO
1732 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1734 * @return talloced struct adouble or NULL on error
1736 static struct adouble
*ad_fget(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1737 files_struct
*fsp
, adouble_type_t type
)
1739 return ad_get_internal(ctx
, handle
, fsp
, NULL
, type
);
1743 * Set AppleDouble metadata on a file or directory
1745 * @param[in] ad adouble handle
1747 * @param[in] smb_fname pathname to file or directory
1749 * @return status code, 0 means success
1751 static int ad_set(struct adouble
*ad
, const struct smb_filename
*smb_fname
)
1756 DBG_DEBUG("Path [%s]\n", smb_fname
->base_name
);
1758 if (ad
->ad_type
!= ADOUBLE_META
) {
1759 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1760 smb_fname
->base_name
);
1769 ret
= SMB_VFS_SETXATTR(ad
->ad_handle
->conn
,
1771 AFPINFO_EA_NETATALK
,
1773 AD_DATASZ_XATTR
, 0);
1775 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname
->base_name
, ret
);
1781 * Set AppleDouble metadata on a file or directory
1783 * @param[in] ad adouble handle
1784 * @param[in] fsp file handle
1786 * @return status code, 0 means success
1788 static int ad_fset(struct adouble
*ad
, files_struct
*fsp
)
1794 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
1797 || (fsp
->fh
== NULL
)
1798 || (fsp
->fh
->fd
== -1))
1800 smb_panic("bad fsp");
1808 switch (ad
->ad_type
) {
1810 rc
= SMB_VFS_NEXT_SETXATTR(ad
->ad_handle
,
1812 AFPINFO_EA_NETATALK
,
1814 AD_DATASZ_XATTR
, 0);
1818 len
= SMB_VFS_NEXT_PWRITE(ad
->ad_handle
,
1823 if (len
!= AD_DATASZ_DOT_UND
) {
1824 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp
), len
);
1834 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp
), rc
);
1839 /*****************************************************************************
1841 *****************************************************************************/
1843 static bool is_afpinfo_stream(const struct smb_filename
*smb_fname
)
1845 if (strncasecmp_m(smb_fname
->stream_name
,
1846 AFPINFO_STREAM_NAME
,
1847 strlen(AFPINFO_STREAM_NAME
)) == 0) {
1853 static bool is_afpresource_stream(const struct smb_filename
*smb_fname
)
1855 if (strncasecmp_m(smb_fname
->stream_name
,
1856 AFPRESOURCE_STREAM_NAME
,
1857 strlen(AFPRESOURCE_STREAM_NAME
)) == 0) {
1864 * Test whether stream is an Apple stream, not used atm
1867 static bool is_apple_stream(const struct smb_filename
*smb_fname
)
1869 if (is_afpinfo_stream(smb_fname
)) {
1872 if (is_afpresource_stream(smb_fname
)) {
1880 * Initialize config struct from our smb.conf config parameters
1882 static int init_fruit_config(vfs_handle_struct
*handle
)
1884 struct fruit_config_data
*config
;
1887 config
= talloc_zero(handle
->conn
, struct fruit_config_data
);
1889 DEBUG(1, ("talloc_zero() failed\n"));
1895 * Versions up to Samba 4.5.x had a spelling bug in the
1896 * fruit:resource option calling lp_parm_enum with
1897 * "res*s*ource" (ie two s).
1899 * In Samba 4.6 we accept both the wrong and the correct
1900 * spelling, in Samba 4.7 the bad spelling will be removed.
1902 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1903 "ressource", fruit_rsrc
, FRUIT_RSRC_ADFILE
);
1904 if (enumval
== -1) {
1905 DEBUG(1, ("value for %s: resource type unknown\n",
1906 FRUIT_PARAM_TYPE_NAME
));
1909 config
->rsrc
= (enum fruit_rsrc
)enumval
;
1911 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1912 "resource", fruit_rsrc
, enumval
);
1913 if (enumval
== -1) {
1914 DEBUG(1, ("value for %s: resource type unknown\n",
1915 FRUIT_PARAM_TYPE_NAME
));
1918 config
->rsrc
= (enum fruit_rsrc
)enumval
;
1920 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1921 "metadata", fruit_meta
, FRUIT_META_NETATALK
);
1922 if (enumval
== -1) {
1923 DEBUG(1, ("value for %s: metadata type unknown\n",
1924 FRUIT_PARAM_TYPE_NAME
));
1927 config
->meta
= (enum fruit_meta
)enumval
;
1929 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1930 "locking", fruit_locking
, FRUIT_LOCKING_NONE
);
1931 if (enumval
== -1) {
1932 DEBUG(1, ("value for %s: locking type unknown\n",
1933 FRUIT_PARAM_TYPE_NAME
));
1936 config
->locking
= (enum fruit_locking
)enumval
;
1938 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1939 "encoding", fruit_encoding
, FRUIT_ENC_PRIVATE
);
1940 if (enumval
== -1) {
1941 DEBUG(1, ("value for %s: encoding type unknown\n",
1942 FRUIT_PARAM_TYPE_NAME
));
1945 config
->encoding
= (enum fruit_encoding
)enumval
;
1947 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1948 config
->veto_appledouble
= lp_parm_bool(SNUM(handle
->conn
),
1949 FRUIT_PARAM_TYPE_NAME
,
1954 config
->use_aapl
= lp_parm_bool(
1955 -1, FRUIT_PARAM_TYPE_NAME
, "aapl", true);
1957 config
->unix_info_enabled
= lp_parm_bool(
1958 -1, FRUIT_PARAM_TYPE_NAME
, "nfs_aces", true);
1960 config
->use_copyfile
= lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
,
1963 config
->posix_rename
= lp_parm_bool(
1964 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
, "posix_rename", true);
1966 config
->aapl_zero_file_id
=
1967 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
, "zero_file_id", true);
1969 config
->readdir_attr_rsize
= lp_parm_bool(
1970 SNUM(handle
->conn
), "readdir_attr", "aapl_rsize", true);
1972 config
->readdir_attr_finder_info
= lp_parm_bool(
1973 SNUM(handle
->conn
), "readdir_attr", "aapl_finder_info", true);
1975 config
->readdir_attr_max_access
= lp_parm_bool(
1976 SNUM(handle
->conn
), "readdir_attr", "aapl_max_access", true);
1978 config
->model
= lp_parm_const_string(
1979 -1, FRUIT_PARAM_TYPE_NAME
, "model", "MacSamba");
1981 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
1982 NULL
, struct fruit_config_data
,
1989 * Prepend "._" to a basename
1990 * Return a new struct smb_filename with stream_name == NULL.
1992 static int adouble_path(TALLOC_CTX
*ctx
,
1993 const struct smb_filename
*smb_fname_in
,
1994 struct smb_filename
**pp_smb_fname_out
)
1998 struct smb_filename
*smb_fname
= cp_smb_filename(ctx
,
2001 if (smb_fname
== NULL
) {
2005 /* We need streamname to be NULL */
2006 TALLOC_FREE(smb_fname
->stream_name
);
2008 /* And we're replacing base_name. */
2009 TALLOC_FREE(smb_fname
->base_name
);
2011 if (!parent_dirname(smb_fname
, smb_fname_in
->base_name
,
2013 TALLOC_FREE(smb_fname
);
2017 smb_fname
->base_name
= talloc_asprintf(smb_fname
,
2018 "%s/._%s", parent
, base
);
2019 if (smb_fname
->base_name
== NULL
) {
2020 TALLOC_FREE(smb_fname
);
2024 *pp_smb_fname_out
= smb_fname
;
2030 * Allocate and initialize an AfpInfo struct
2032 static AfpInfo
*afpinfo_new(TALLOC_CTX
*ctx
)
2034 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
2038 ai
->afpi_Signature
= AFP_Signature
;
2039 ai
->afpi_Version
= AFP_Version
;
2040 ai
->afpi_BackupTime
= AD_DATE_START
;
2045 * Pack an AfpInfo struct into a buffer
2047 * Buffer size must be at least AFP_INFO_SIZE
2048 * Returns size of packed buffer
2050 static ssize_t
afpinfo_pack(const AfpInfo
*ai
, char *buf
)
2052 memset(buf
, 0, AFP_INFO_SIZE
);
2054 RSIVAL(buf
, 0, ai
->afpi_Signature
);
2055 RSIVAL(buf
, 4, ai
->afpi_Version
);
2056 RSIVAL(buf
, 12, ai
->afpi_BackupTime
);
2057 memcpy(buf
+ 16, ai
->afpi_FinderInfo
, sizeof(ai
->afpi_FinderInfo
));
2059 return AFP_INFO_SIZE
;
2063 * Unpack a buffer into a AfpInfo structure
2065 * Buffer size must be at least AFP_INFO_SIZE
2066 * Returns allocated AfpInfo struct
2068 static AfpInfo
*afpinfo_unpack(TALLOC_CTX
*ctx
, const void *data
)
2070 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
2075 ai
->afpi_Signature
= RIVAL(data
, 0);
2076 ai
->afpi_Version
= RIVAL(data
, 4);
2077 ai
->afpi_BackupTime
= RIVAL(data
, 12);
2078 memcpy(ai
->afpi_FinderInfo
, (const char *)data
+ 16,
2079 sizeof(ai
->afpi_FinderInfo
));
2081 if (ai
->afpi_Signature
!= AFP_Signature
2082 || ai
->afpi_Version
!= AFP_Version
) {
2083 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2091 * Fake an inode number from the md5 hash of the (xattr) name
2093 static SMB_INO_T
fruit_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
2096 unsigned char hash
[16];
2100 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
2101 SMB_ASSERT(upper_sname
!= NULL
);
2104 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_dev
),
2105 sizeof(sbuf
->st_ex_dev
));
2106 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_ino
),
2107 sizeof(sbuf
->st_ex_ino
));
2108 MD5Update(&ctx
, (unsigned char *)upper_sname
,
2109 talloc_get_size(upper_sname
)-1);
2110 MD5Final(hash
, &ctx
);
2112 TALLOC_FREE(upper_sname
);
2114 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2115 memcpy(&result
, hash
, sizeof(result
));
2117 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2118 sname
, (unsigned long long)result
));
2123 static bool add_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
2124 struct stream_struct
**streams
,
2125 const char *name
, off_t size
,
2128 struct stream_struct
*tmp
;
2130 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
2136 tmp
[*num_streams
].name
= talloc_asprintf(tmp
, "%s:$DATA", name
);
2137 if (tmp
[*num_streams
].name
== NULL
) {
2141 tmp
[*num_streams
].size
= size
;
2142 tmp
[*num_streams
].alloc_size
= alloc_size
;
2149 static bool filter_empty_rsrc_stream(unsigned int *num_streams
,
2150 struct stream_struct
**streams
)
2152 struct stream_struct
*tmp
= *streams
;
2155 if (*num_streams
== 0) {
2159 for (i
= 0; i
< *num_streams
; i
++) {
2160 if (strequal_m(tmp
[i
].name
, AFPRESOURCE_STREAM
)) {
2165 if (i
== *num_streams
) {
2169 if (tmp
[i
].size
> 0) {
2173 TALLOC_FREE(tmp
[i
].name
);
2174 if (*num_streams
- 1 > i
) {
2175 memmove(&tmp
[i
], &tmp
[i
+1],
2176 (*num_streams
- i
- 1) * sizeof(struct stream_struct
));
2183 static bool del_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
2184 struct stream_struct
**streams
,
2187 struct stream_struct
*tmp
= *streams
;
2190 if (*num_streams
== 0) {
2194 for (i
= 0; i
< *num_streams
; i
++) {
2195 if (strequal_m(tmp
[i
].name
, name
)) {
2200 if (i
== *num_streams
) {
2204 TALLOC_FREE(tmp
[i
].name
);
2205 if (*num_streams
- 1 > i
) {
2206 memmove(&tmp
[i
], &tmp
[i
+1],
2207 (*num_streams
- i
- 1) * sizeof(struct stream_struct
));
2214 static bool ad_empty_finderinfo(const struct adouble
*ad
)
2217 char emptybuf
[ADEDLEN_FINDERI
] = {0};
2220 fi
= ad_get_entry(ad
, ADEID_FINDERI
);
2222 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad
);
2226 cmp
= memcmp(emptybuf
, fi
, ADEDLEN_FINDERI
);
2230 static bool ai_empty_finderinfo(const AfpInfo
*ai
)
2233 char emptybuf
[ADEDLEN_FINDERI
] = {0};
2235 cmp
= memcmp(emptybuf
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
2240 * Update btime with btime from Netatalk
2242 static void update_btime(vfs_handle_struct
*handle
,
2243 struct smb_filename
*smb_fname
)
2246 struct timespec creation_time
= {0};
2248 struct fruit_config_data
*config
= NULL
;
2250 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
2253 switch (config
->meta
) {
2254 case FRUIT_META_STREAM
:
2256 case FRUIT_META_NETATALK
:
2260 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
2264 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
2268 if (ad_getdate(ad
, AD_DATE_UNIX
| AD_DATE_CREATE
, &t
) != 0) {
2274 creation_time
.tv_sec
= convert_uint32_t_to_time_t(t
);
2275 update_stat_ex_create_time(&smb_fname
->st
, creation_time
);
2281 * Map an access mask to a Netatalk single byte byte range lock
2283 static off_t
access_to_netatalk_brl(enum apple_fork fork_type
,
2284 uint32_t access_mask
)
2288 switch (access_mask
) {
2289 case FILE_READ_DATA
:
2290 offset
= AD_FILELOCK_OPEN_RD
;
2293 case FILE_WRITE_DATA
:
2294 case FILE_APPEND_DATA
:
2295 offset
= AD_FILELOCK_OPEN_WR
;
2299 offset
= AD_FILELOCK_OPEN_NONE
;
2303 if (fork_type
== APPLE_FORK_RSRC
) {
2304 if (offset
== AD_FILELOCK_OPEN_NONE
) {
2305 offset
= AD_FILELOCK_RSRC_OPEN_NONE
;
2315 * Map a deny mode to a Netatalk brl
2317 static off_t
denymode_to_netatalk_brl(enum apple_fork fork_type
,
2322 switch (deny_mode
) {
2324 offset
= AD_FILELOCK_DENY_RD
;
2328 offset
= AD_FILELOCK_DENY_WR
;
2332 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2335 if (fork_type
== APPLE_FORK_RSRC
) {
2343 * Call fcntl() with an exclusive F_GETLK request in order to
2344 * determine if there's an exisiting shared lock
2346 * @return true if the requested lock was found or any error occurred
2347 * false if the lock was not found
2349 static bool test_netatalk_lock(files_struct
*fsp
, off_t in_offset
)
2352 off_t offset
= in_offset
;
2357 result
= SMB_VFS_GETLOCK(fsp
, &offset
, &len
, &type
, &pid
);
2358 if (result
== false) {
2362 if (type
!= F_UNLCK
) {
2369 static NTSTATUS
fruit_check_access(vfs_handle_struct
*handle
,
2371 uint32_t access_mask
,
2374 NTSTATUS status
= NT_STATUS_OK
;
2375 struct byte_range_lock
*br_lck
= NULL
;
2376 bool open_for_reading
, open_for_writing
, deny_read
, deny_write
;
2378 bool have_read
= false;
2381 /* FIXME: hardcoded data fork, add resource fork */
2382 enum apple_fork fork_type
= APPLE_FORK_DATA
;
2384 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2386 access_mask
& FILE_READ_DATA
? "READ" :"-",
2387 access_mask
& FILE_WRITE_DATA
? "WRITE" : "-",
2388 deny_mode
& DENY_READ
? "DENY_READ" : "-",
2389 deny_mode
& DENY_WRITE
? "DENY_WRITE" : "-"));
2391 if (fsp
->fh
->fd
== -1) {
2392 return NT_STATUS_OK
;
2395 flags
= fcntl(fsp
->fh
->fd
, F_GETFL
);
2397 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2398 fsp_str_dbg(fsp
), fsp
->fh
->fd
, strerror(errno
));
2399 return map_nt_error_from_unix(errno
);
2402 if (flags
& (O_RDONLY
|O_RDWR
)) {
2404 * Applying fcntl read locks requires an fd opened for
2405 * reading. This means we won't be applying locks for
2406 * files openend write-only, but what can we do...
2412 * Check read access and deny read mode
2414 if ((access_mask
& FILE_READ_DATA
) || (deny_mode
& DENY_READ
)) {
2416 open_for_reading
= test_netatalk_lock(
2417 fsp
, access_to_netatalk_brl(fork_type
, FILE_READ_DATA
));
2419 deny_read
= test_netatalk_lock(
2420 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_READ
));
2422 DEBUG(10, ("read: %s, deny_write: %s\n",
2423 open_for_reading
== true ? "yes" : "no",
2424 deny_read
== true ? "yes" : "no"));
2426 if (((access_mask
& FILE_READ_DATA
) && deny_read
)
2427 || ((deny_mode
& DENY_READ
) && open_for_reading
)) {
2428 return NT_STATUS_SHARING_VIOLATION
;
2432 if ((access_mask
& FILE_READ_DATA
) && have_read
) {
2433 off
= access_to_netatalk_brl(fork_type
, FILE_READ_DATA
);
2435 handle
->conn
->sconn
->msg_ctx
, fsp
,
2436 fsp
->op
->global
->open_persistent_id
, 1, off
,
2437 READ_LOCK
, POSIX_LOCK
, false,
2440 if (!NT_STATUS_IS_OK(status
)) {
2443 TALLOC_FREE(br_lck
);
2446 if ((deny_mode
& DENY_READ
) && have_read
) {
2447 off
= denymode_to_netatalk_brl(fork_type
, DENY_READ
);
2449 handle
->conn
->sconn
->msg_ctx
, fsp
,
2450 fsp
->op
->global
->open_persistent_id
, 1, off
,
2451 READ_LOCK
, POSIX_LOCK
, false,
2454 if (!NT_STATUS_IS_OK(status
)) {
2457 TALLOC_FREE(br_lck
);
2462 * Check write access and deny write mode
2464 if ((access_mask
& FILE_WRITE_DATA
) || (deny_mode
& DENY_WRITE
)) {
2466 open_for_writing
= test_netatalk_lock(
2467 fsp
, access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
));
2469 deny_write
= test_netatalk_lock(
2470 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_WRITE
));
2472 DEBUG(10, ("write: %s, deny_write: %s\n",
2473 open_for_writing
== true ? "yes" : "no",
2474 deny_write
== true ? "yes" : "no"));
2476 if (((access_mask
& FILE_WRITE_DATA
) && deny_write
)
2477 || ((deny_mode
& DENY_WRITE
) && open_for_writing
)) {
2478 return NT_STATUS_SHARING_VIOLATION
;
2482 if ((access_mask
& FILE_WRITE_DATA
) && have_read
) {
2483 off
= access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
);
2485 handle
->conn
->sconn
->msg_ctx
, fsp
,
2486 fsp
->op
->global
->open_persistent_id
, 1, off
,
2487 READ_LOCK
, POSIX_LOCK
, false,
2490 if (!NT_STATUS_IS_OK(status
)) {
2493 TALLOC_FREE(br_lck
);
2496 if ((deny_mode
& DENY_WRITE
) && have_read
) {
2497 off
= denymode_to_netatalk_brl(fork_type
, DENY_WRITE
);
2499 handle
->conn
->sconn
->msg_ctx
, fsp
,
2500 fsp
->op
->global
->open_persistent_id
, 1, off
,
2501 READ_LOCK
, POSIX_LOCK
, false,
2504 if (!NT_STATUS_IS_OK(status
)) {
2507 TALLOC_FREE(br_lck
);
2511 TALLOC_FREE(br_lck
);
2516 static NTSTATUS
check_aapl(vfs_handle_struct
*handle
,
2517 struct smb_request
*req
,
2518 const struct smb2_create_blobs
*in_context_blobs
,
2519 struct smb2_create_blobs
*out_context_blobs
)
2521 struct fruit_config_data
*config
;
2523 struct smb2_create_blob
*aapl
= NULL
;
2527 DATA_BLOB blob
= data_blob_talloc(req
, NULL
, 0);
2528 uint64_t req_bitmap
, client_caps
;
2529 uint64_t server_caps
= SMB2_CRTCTX_AAPL_UNIX_BASED
;
2533 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
2534 return NT_STATUS_UNSUCCESSFUL
);
2536 if (!config
->use_aapl
2537 || in_context_blobs
== NULL
2538 || out_context_blobs
== NULL
) {
2539 return NT_STATUS_OK
;
2542 aapl
= smb2_create_blob_find(in_context_blobs
,
2543 SMB2_CREATE_TAG_AAPL
);
2545 return NT_STATUS_OK
;
2548 if (aapl
->data
.length
!= 24) {
2549 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2550 (uintmax_t)aapl
->data
.length
));
2551 return NT_STATUS_INVALID_PARAMETER
;
2554 cmd
= IVAL(aapl
->data
.data
, 0);
2555 if (cmd
!= SMB2_CRTCTX_AAPL_SERVER_QUERY
) {
2556 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd
));
2557 return NT_STATUS_INVALID_PARAMETER
;
2560 req_bitmap
= BVAL(aapl
->data
.data
, 8);
2561 client_caps
= BVAL(aapl
->data
.data
, 16);
2563 SIVAL(p
, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY
);
2565 SBVAL(p
, 8, req_bitmap
);
2566 ok
= data_blob_append(req
, &blob
, p
, 16);
2568 return NT_STATUS_UNSUCCESSFUL
;
2571 if (req_bitmap
& SMB2_CRTCTX_AAPL_SERVER_CAPS
) {
2572 if ((client_caps
& SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
) &&
2573 (handle
->conn
->tcon
->compat
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
2574 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
;
2575 config
->readdir_attr_enabled
= true;
2578 if (config
->use_copyfile
) {
2579 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE
;
2580 config
->copyfile_enabled
= true;
2584 * The client doesn't set the flag, so we can't check
2585 * for it and just set it unconditionally
2587 if (config
->unix_info_enabled
) {
2588 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE
;
2591 SBVAL(p
, 0, server_caps
);
2592 ok
= data_blob_append(req
, &blob
, p
, 8);
2594 return NT_STATUS_UNSUCCESSFUL
;
2598 if (req_bitmap
& SMB2_CRTCTX_AAPL_VOLUME_CAPS
) {
2599 int val
= lp_case_sensitive(SNUM(handle
->conn
->tcon
->compat
));
2607 caps
|= SMB2_CRTCTX_AAPL_CASE_SENSITIVE
;
2616 ok
= data_blob_append(req
, &blob
, p
, 8);
2618 return NT_STATUS_UNSUCCESSFUL
;
2622 if (req_bitmap
& SMB2_CRTCTX_AAPL_MODEL_INFO
) {
2623 ok
= convert_string_talloc(req
,
2624 CH_UNIX
, CH_UTF16LE
,
2625 config
->model
, strlen(config
->model
),
2628 return NT_STATUS_UNSUCCESSFUL
;
2632 SIVAL(p
+ 4, 0, modellen
);
2633 ok
= data_blob_append(req
, &blob
, p
, 8);
2636 return NT_STATUS_UNSUCCESSFUL
;
2639 ok
= data_blob_append(req
, &blob
, model
, modellen
);
2642 return NT_STATUS_UNSUCCESSFUL
;
2646 status
= smb2_create_blob_add(out_context_blobs
,
2648 SMB2_CREATE_TAG_AAPL
,
2650 if (NT_STATUS_IS_OK(status
)) {
2651 global_fruit_config
.nego_aapl
= true;
2652 if (config
->aapl_zero_file_id
) {
2653 aapl_force_zero_file_id(handle
->conn
->sconn
);
2660 static bool readdir_attr_meta_finderi_stream(
2661 struct vfs_handle_struct
*handle
,
2662 const struct smb_filename
*smb_fname
,
2665 struct smb_filename
*stream_name
= NULL
;
2666 files_struct
*fsp
= NULL
;
2671 uint8_t buf
[AFP_INFO_SIZE
];
2673 stream_name
= synthetic_smb_fname(talloc_tos(),
2674 smb_fname
->base_name
,
2675 AFPINFO_STREAM_NAME
,
2676 NULL
, smb_fname
->flags
);
2677 if (stream_name
== NULL
) {
2681 ret
= SMB_VFS_STAT(handle
->conn
, stream_name
);
2686 status
= SMB_VFS_CREATE_FILE(
2687 handle
->conn
, /* conn */
2689 0, /* root_dir_fid */
2690 stream_name
, /* fname */
2691 FILE_READ_DATA
, /* access_mask */
2692 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2694 FILE_OPEN
, /* create_disposition*/
2695 0, /* create_options */
2696 0, /* file_attributes */
2697 INTERNAL_OPEN_ONLY
, /* oplock_request */
2699 0, /* allocation_size */
2700 0, /* private_flags */
2705 NULL
, NULL
); /* create context */
2707 TALLOC_FREE(stream_name
);
2709 if (!NT_STATUS_IS_OK(status
)) {
2713 nread
= SMB_VFS_PREAD(fsp
, &buf
[0], AFP_INFO_SIZE
, 0);
2714 if (nread
!= AFP_INFO_SIZE
) {
2715 DBG_ERR("short read [%s] [%zd/%d]\n",
2716 smb_fname_str_dbg(stream_name
), nread
, AFP_INFO_SIZE
);
2721 memcpy(&ai
->afpi_FinderInfo
[0], &buf
[AFP_OFF_FinderInfo
],
2728 close_file(NULL
, fsp
, NORMAL_CLOSE
);
2734 static bool readdir_attr_meta_finderi_netatalk(
2735 struct vfs_handle_struct
*handle
,
2736 const struct smb_filename
*smb_fname
,
2739 struct adouble
*ad
= NULL
;
2742 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
2747 p
= ad_get_entry(ad
, ADEID_FINDERI
);
2749 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname
->base_name
);
2754 memcpy(&ai
->afpi_FinderInfo
[0], p
, AFP_FinderSize
);
2759 static bool readdir_attr_meta_finderi(struct vfs_handle_struct
*handle
,
2760 const struct smb_filename
*smb_fname
,
2761 struct readdir_attr_data
*attr_data
)
2763 struct fruit_config_data
*config
= NULL
;
2764 uint32_t date_added
;
2768 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2769 struct fruit_config_data
,
2772 switch (config
->meta
) {
2773 case FRUIT_META_NETATALK
:
2774 ok
= readdir_attr_meta_finderi_netatalk(
2775 handle
, smb_fname
, &ai
);
2778 case FRUIT_META_STREAM
:
2779 ok
= readdir_attr_meta_finderi_stream(
2780 handle
, smb_fname
, &ai
);
2784 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
2789 /* Don't bother with errors, it's likely ENOENT */
2793 if (S_ISREG(smb_fname
->st
.st_ex_mode
)) {
2795 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0],
2796 &ai
.afpi_FinderInfo
[0], 4);
2798 /* finder_creator */
2799 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 4,
2800 &ai
.afpi_FinderInfo
[4], 4);
2804 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 8,
2805 &ai
.afpi_FinderInfo
[8], 2);
2807 /* finder_ext_flags */
2808 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 10,
2809 &ai
.afpi_FinderInfo
[24], 2);
2812 date_added
= convert_time_t_to_uint32_t(
2813 smb_fname
->st
.st_ex_btime
.tv_sec
- AD_DATE_DELTA
);
2815 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
[0], 12, date_added
);
2820 static uint64_t readdir_attr_rfork_size_adouble(
2821 struct vfs_handle_struct
*handle
,
2822 const struct smb_filename
*smb_fname
)
2824 struct adouble
*ad
= NULL
;
2825 uint64_t rfork_size
;
2827 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
2833 rfork_size
= ad_getentrylen(ad
, ADEID_RFORK
);
2839 static uint64_t readdir_attr_rfork_size_stream(
2840 struct vfs_handle_struct
*handle
,
2841 const struct smb_filename
*smb_fname
)
2843 struct smb_filename
*stream_name
= NULL
;
2845 uint64_t rfork_size
;
2847 stream_name
= synthetic_smb_fname(talloc_tos(),
2848 smb_fname
->base_name
,
2849 AFPRESOURCE_STREAM_NAME
,
2851 if (stream_name
== NULL
) {
2855 ret
= SMB_VFS_STAT(handle
->conn
, stream_name
);
2857 TALLOC_FREE(stream_name
);
2861 rfork_size
= stream_name
->st
.st_ex_size
;
2862 TALLOC_FREE(stream_name
);
2867 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct
*handle
,
2868 const struct smb_filename
*smb_fname
)
2870 struct fruit_config_data
*config
= NULL
;
2871 uint64_t rfork_size
;
2873 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2874 struct fruit_config_data
,
2877 switch (config
->rsrc
) {
2878 case FRUIT_RSRC_ADFILE
:
2879 case FRUIT_RSRC_XATTR
:
2880 rfork_size
= readdir_attr_rfork_size_adouble(handle
,
2884 case FRUIT_META_STREAM
:
2885 rfork_size
= readdir_attr_rfork_size_stream(handle
,
2890 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
2898 static NTSTATUS
readdir_attr_macmeta(struct vfs_handle_struct
*handle
,
2899 const struct smb_filename
*smb_fname
,
2900 struct readdir_attr_data
*attr_data
)
2902 NTSTATUS status
= NT_STATUS_OK
;
2903 struct fruit_config_data
*config
= NULL
;
2906 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2907 struct fruit_config_data
,
2908 return NT_STATUS_UNSUCCESSFUL
);
2911 /* Ensure we return a default value in the creation_date field */
2912 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
, 12, AD_DATE_START
);
2915 * Resource fork length
2918 if (config
->readdir_attr_rsize
) {
2919 uint64_t rfork_size
;
2921 rfork_size
= readdir_attr_rfork_size(handle
, smb_fname
);
2922 attr_data
->attr_data
.aapl
.rfork_size
= rfork_size
;
2929 if (config
->readdir_attr_finder_info
) {
2930 ok
= readdir_attr_meta_finderi(handle
, smb_fname
, attr_data
);
2932 status
= NT_STATUS_INTERNAL_ERROR
;
2939 static NTSTATUS
remove_virtual_nfs_aces(struct security_descriptor
*psd
)
2944 if (psd
->dacl
== NULL
) {
2945 return NT_STATUS_OK
;
2948 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
2949 /* MS NFS style mode/uid/gid */
2950 if (!dom_sid_compare_domain(
2951 &global_sid_Unix_NFS
,
2952 &psd
->dacl
->aces
[i
].trustee
) == 0) {
2953 /* Normal ACE entry. */
2958 * security_descriptor_dacl_del()
2959 * *must* return NT_STATUS_OK as we know
2960 * we have something to remove.
2963 status
= security_descriptor_dacl_del(psd
,
2964 &psd
->dacl
->aces
[i
].trustee
);
2965 if (!NT_STATUS_IS_OK(status
)) {
2966 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
2972 * security_descriptor_dacl_del() may delete more
2973 * then one entry subsequent to this one if the
2974 * SID matches, but we only need to ensure that
2975 * we stay looking at the same element in the array.
2979 return NT_STATUS_OK
;
2982 /* Search MS NFS style ACE with UNIX mode */
2983 static NTSTATUS
check_ms_nfs(vfs_handle_struct
*handle
,
2985 struct security_descriptor
*psd
,
2990 struct fruit_config_data
*config
= NULL
;
2992 NTSTATUS status
= NT_STATUS_OK
;
2993 bool remove_ok
= false;
2997 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2998 struct fruit_config_data
,
2999 return NT_STATUS_UNSUCCESSFUL
);
3001 if (!global_fruit_config
.nego_aapl
) {
3002 return NT_STATUS_OK
;
3004 if (psd
->dacl
== NULL
|| !config
->unix_info_enabled
) {
3005 return NT_STATUS_OK
;
3008 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
3009 if (dom_sid_compare_domain(
3010 &global_sid_Unix_NFS_Mode
,
3011 &psd
->dacl
->aces
[i
].trustee
) == 0) {
3012 *pmode
= (mode_t
)psd
->dacl
->aces
[i
].trustee
.sub_auths
[2];
3013 *pmode
&= (S_IRWXU
| S_IRWXG
| S_IRWXO
);
3016 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3017 fsp_str_dbg(fsp
), (unsigned)(*pmode
)));
3023 * Remove any incoming virtual ACE entries generated by
3024 * fruit_fget_nt_acl().
3027 /* MS NFS style mode */
3028 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
,
3029 fsp
->fsp_name
->st
.st_ex_mode
);
3030 status
= security_descriptor_dacl_del(psd
, &sid
);
3031 remove_ok
= (NT_STATUS_IS_OK(status
) ||
3032 NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
));
3034 DBG_WARNING("failed to remove MS NFS_mode style ACE\n");
3038 /* MS NFS style uid */
3039 sid_compose(&sid
, &global_sid_Unix_NFS_Users
,
3040 fsp
->fsp_name
->st
.st_ex_uid
);
3041 status
= security_descriptor_dacl_del(psd
, &sid
);
3042 remove_ok
= (NT_STATUS_IS_OK(status
) ||
3043 NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
));
3045 DBG_WARNING("failed to remove MS NFS_users style ACE\n");
3049 /* MS NFS style gid */
3050 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
,
3051 fsp
->fsp_name
->st
.st_ex_gid
);
3052 status
= security_descriptor_dacl_del(psd
, &sid
);
3053 remove_ok
= (NT_STATUS_IS_OK(status
) ||
3054 NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
));
3056 DBG_WARNING("failed to remove MS NFS_groups style ACE\n");
3060 return NT_STATUS_OK
;
3063 /****************************************************************************
3065 ****************************************************************************/
3067 static int fruit_connect(vfs_handle_struct
*handle
,
3068 const char *service
,
3072 char *list
= NULL
, *newlist
= NULL
;
3073 struct fruit_config_data
*config
;
3075 DEBUG(10, ("fruit_connect\n"));
3077 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
3082 rc
= init_fruit_config(handle
);
3087 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3088 struct fruit_config_data
, return -1);
3090 if (config
->veto_appledouble
) {
3091 list
= lp_veto_files(talloc_tos(), SNUM(handle
->conn
));
3094 if (strstr(list
, "/" ADOUBLE_NAME_PREFIX
"*/") == NULL
) {
3095 newlist
= talloc_asprintf(
3097 "%s/" ADOUBLE_NAME_PREFIX
"*/",
3099 lp_do_parameter(SNUM(handle
->conn
),
3104 lp_do_parameter(SNUM(handle
->conn
),
3106 "/" ADOUBLE_NAME_PREFIX
"*/");
3112 if (config
->encoding
== FRUIT_ENC_NATIVE
) {
3113 lp_do_parameter(SNUM(handle
->conn
),
3121 static int fruit_open_meta_stream(vfs_handle_struct
*handle
,
3122 struct smb_filename
*smb_fname
,
3128 char afpinfo_buf
[AFP_INFO_SIZE
];
3129 ssize_t len
, written
;
3133 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3138 if (!(flags
& (O_CREAT
| O_TRUNC
))) {
3142 ai
= afpinfo_new(talloc_tos());
3148 len
= afpinfo_pack(ai
, afpinfo_buf
);
3149 if (len
!= AFP_INFO_SIZE
) {
3154 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3155 fsp
->fh
->fd
= hostfd
;
3157 written
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, afpinfo_buf
,
3160 if (written
!= AFP_INFO_SIZE
) {
3161 DBG_ERR("bad write [%zd/%d]\n", written
, AFP_INFO_SIZE
);
3169 DBG_DEBUG("rc=%d, fd=%d\n", rc
, hostfd
);
3172 int saved_errno
= errno
;
3174 fsp
->fh
->fd
= hostfd
;
3175 SMB_VFS_NEXT_CLOSE(handle
, fsp
);
3178 errno
= saved_errno
;
3183 static int fruit_open_meta_netatalk(vfs_handle_struct
*handle
,
3184 struct smb_filename
*smb_fname
,
3191 struct adouble
*ad
= NULL
;
3194 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3197 * Return a valid fd, but ensure any attempt to use it returns an error
3198 * (EPIPE). All operations on the smb_fname or the fsp will use path
3208 if (flags
& (O_CREAT
| O_TRUNC
)) {
3210 * The attribute does not exist or needs to be truncated,
3211 * create an AppleDouble EA
3213 ad
= ad_init(fsp
, handle
, ADOUBLE_META
);
3219 rc
= ad_set(ad
, fsp
->fsp_name
);
3229 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc
, fakefd
));
3231 int saved_errno
= errno
;
3236 errno
= saved_errno
;
3241 static int fruit_open_meta(vfs_handle_struct
*handle
,
3242 struct smb_filename
*smb_fname
,
3243 files_struct
*fsp
, int flags
, mode_t mode
)
3246 struct fruit_config_data
*config
= NULL
;
3247 struct fio
*fio
= NULL
;
3249 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname
));
3251 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3252 struct fruit_config_data
, return -1);
3254 switch (config
->meta
) {
3255 case FRUIT_META_STREAM
:
3256 fd
= fruit_open_meta_stream(handle
, smb_fname
,
3260 case FRUIT_META_NETATALK
:
3261 fd
= fruit_open_meta_netatalk(handle
, smb_fname
,
3266 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
3270 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3276 fio
= (struct fio
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
3277 fio
->type
= ADOUBLE_META
;
3278 fio
->config
= config
;
3283 static int fruit_open_rsrc_adouble(vfs_handle_struct
*handle
,
3284 struct smb_filename
*smb_fname
,
3290 struct adouble
*ad
= NULL
;
3291 struct smb_filename
*smb_fname_base
= NULL
;
3292 struct fruit_config_data
*config
= NULL
;
3295 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3296 struct fruit_config_data
, return -1);
3298 if ((!(flags
& O_CREAT
)) &&
3299 S_ISDIR(fsp
->base_fsp
->fsp_name
->st
.st_ex_mode
))
3301 /* sorry, but directories don't habe a resource fork */
3306 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_base
);
3311 /* Sanitize flags */
3312 if (flags
& O_WRONLY
) {
3313 /* We always need read access for the metadata header too */
3318 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname_base
, fsp
,
3325 if (flags
& (O_CREAT
| O_TRUNC
)) {
3326 ad
= ad_init(fsp
, handle
, ADOUBLE_RSRC
);
3332 fsp
->fh
->fd
= hostfd
;
3334 rc
= ad_fset(ad
, fsp
);
3345 TALLOC_FREE(smb_fname_base
);
3347 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc
, hostfd
));
3349 int saved_errno
= errno
;
3352 * BUGBUGBUG -- we would need to call
3353 * fd_close_posix here, but we don't have a
3356 fsp
->fh
->fd
= hostfd
;
3360 errno
= saved_errno
;
3365 static int fruit_open_rsrc_xattr(vfs_handle_struct
*handle
,
3366 struct smb_filename
*smb_fname
,
3371 #ifdef HAVE_ATTROPEN
3374 fd
= attropen(smb_fname
->base_name
,
3375 AFPRESOURCE_EA_NETATALK
,
3390 static int fruit_open_rsrc(vfs_handle_struct
*handle
,
3391 struct smb_filename
*smb_fname
,
3392 files_struct
*fsp
, int flags
, mode_t mode
)
3395 struct fruit_config_data
*config
= NULL
;
3396 struct fio
*fio
= NULL
;
3398 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3400 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3401 struct fruit_config_data
, return -1);
3403 if (((flags
& O_ACCMODE
) == O_RDONLY
)
3404 && (flags
& O_CREAT
)
3405 && !VALID_STAT(fsp
->fsp_name
->st
))
3408 * This means the stream doesn't exist. macOS SMB server fails
3409 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3410 * 12565 and the test for this combination in
3411 * test_rfork_create().
3417 switch (config
->rsrc
) {
3418 case FRUIT_RSRC_STREAM
:
3419 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3422 case FRUIT_RSRC_ADFILE
:
3423 fd
= fruit_open_rsrc_adouble(handle
, smb_fname
,
3427 case FRUIT_RSRC_XATTR
:
3428 fd
= fruit_open_rsrc_xattr(handle
, smb_fname
,
3433 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
3437 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3443 fio
= (struct fio
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
3444 fio
->type
= ADOUBLE_RSRC
;
3445 fio
->config
= config
;
3450 static int fruit_open(vfs_handle_struct
*handle
,
3451 struct smb_filename
*smb_fname
,
3452 files_struct
*fsp
, int flags
, mode_t mode
)
3456 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3458 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
3459 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3462 if (is_afpinfo_stream(smb_fname
)) {
3463 fd
= fruit_open_meta(handle
, smb_fname
, fsp
, flags
, mode
);
3464 } else if (is_afpresource_stream(smb_fname
)) {
3465 fd
= fruit_open_rsrc(handle
, smb_fname
, fsp
, flags
, mode
);
3467 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3470 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3475 static int fruit_rename(struct vfs_handle_struct
*handle
,
3476 const struct smb_filename
*smb_fname_src
,
3477 const struct smb_filename
*smb_fname_dst
)
3480 struct fruit_config_data
*config
= NULL
;
3481 struct smb_filename
*src_adp_smb_fname
= NULL
;
3482 struct smb_filename
*dst_adp_smb_fname
= NULL
;
3484 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3485 struct fruit_config_data
, return -1);
3487 if (!VALID_STAT(smb_fname_src
->st
)) {
3488 DBG_ERR("Need valid stat for [%s]\n",
3489 smb_fname_str_dbg(smb_fname_src
));
3493 rc
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
3498 if ((config
->rsrc
!= FRUIT_RSRC_ADFILE
) ||
3499 (!S_ISREG(smb_fname_src
->st
.st_ex_mode
)))
3504 rc
= adouble_path(talloc_tos(), smb_fname_src
, &src_adp_smb_fname
);
3509 rc
= adouble_path(talloc_tos(), smb_fname_dst
, &dst_adp_smb_fname
);
3514 DBG_DEBUG("%s -> %s\n",
3515 smb_fname_str_dbg(src_adp_smb_fname
),
3516 smb_fname_str_dbg(dst_adp_smb_fname
));
3518 rc
= SMB_VFS_NEXT_RENAME(handle
, src_adp_smb_fname
, dst_adp_smb_fname
);
3519 if (errno
== ENOENT
) {
3524 TALLOC_FREE(src_adp_smb_fname
);
3525 TALLOC_FREE(dst_adp_smb_fname
);
3529 static int fruit_unlink_meta_stream(vfs_handle_struct
*handle
,
3530 const struct smb_filename
*smb_fname
)
3532 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3535 static int fruit_unlink_meta_netatalk(vfs_handle_struct
*handle
,
3536 const struct smb_filename
*smb_fname
)
3538 return SMB_VFS_REMOVEXATTR(handle
->conn
,
3540 AFPINFO_EA_NETATALK
);
3543 static int fruit_unlink_meta(vfs_handle_struct
*handle
,
3544 const struct smb_filename
*smb_fname
)
3546 struct fruit_config_data
*config
= NULL
;
3549 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3550 struct fruit_config_data
, return -1);
3552 switch (config
->meta
) {
3553 case FRUIT_META_STREAM
:
3554 rc
= fruit_unlink_meta_stream(handle
, smb_fname
);
3557 case FRUIT_META_NETATALK
:
3558 rc
= fruit_unlink_meta_netatalk(handle
, smb_fname
);
3562 DBG_ERR("Unsupported meta config [%d]\n", config
->meta
);
3569 static int fruit_unlink_rsrc_stream(vfs_handle_struct
*handle
,
3570 const struct smb_filename
*smb_fname
,
3575 if (!force_unlink
) {
3576 struct smb_filename
*smb_fname_cp
= NULL
;
3579 smb_fname_cp
= cp_smb_filename(talloc_tos(), smb_fname
);
3580 if (smb_fname_cp
== NULL
) {
3585 * 0 byte resource fork streams are not listed by
3586 * vfs_streaminfo, as a result stream cleanup/deletion of file
3587 * deletion doesn't remove the resourcefork stream.
3590 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_cp
);
3592 TALLOC_FREE(smb_fname_cp
);
3593 DBG_ERR("stat [%s] failed [%s]\n",
3594 smb_fname_str_dbg(smb_fname_cp
), strerror(errno
));
3598 size
= smb_fname_cp
->st
.st_ex_size
;
3599 TALLOC_FREE(smb_fname_cp
);
3602 /* OS X ignores resource fork stream delete requests */
3607 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3608 if ((ret
!= 0) && (errno
== ENOENT
) && force_unlink
) {
3615 static int fruit_unlink_rsrc_adouble(vfs_handle_struct
*handle
,
3616 const struct smb_filename
*smb_fname
,
3620 struct adouble
*ad
= NULL
;
3621 struct smb_filename
*adp_smb_fname
= NULL
;
3623 if (!force_unlink
) {
3624 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
3633 * 0 byte resource fork streams are not listed by
3634 * vfs_streaminfo, as a result stream cleanup/deletion of file
3635 * deletion doesn't remove the resourcefork stream.
3638 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
3639 /* OS X ignores resource fork stream delete requests */
3647 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
3652 rc
= SMB_VFS_NEXT_UNLINK(handle
, adp_smb_fname
);
3653 TALLOC_FREE(adp_smb_fname
);
3654 if ((rc
!= 0) && (errno
== ENOENT
) && force_unlink
) {
3661 static int fruit_unlink_rsrc_xattr(vfs_handle_struct
*handle
,
3662 const struct smb_filename
*smb_fname
,
3666 * OS X ignores resource fork stream delete requests, so nothing to do
3667 * here. Removing the file will remove the xattr anyway, so we don't
3668 * have to take care of removing 0 byte resource forks that could be
3674 static int fruit_unlink_rsrc(vfs_handle_struct
*handle
,
3675 const struct smb_filename
*smb_fname
,
3678 struct fruit_config_data
*config
= NULL
;
3681 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3682 struct fruit_config_data
, return -1);
3684 switch (config
->rsrc
) {
3685 case FRUIT_RSRC_STREAM
:
3686 rc
= fruit_unlink_rsrc_stream(handle
, smb_fname
, force_unlink
);
3689 case FRUIT_RSRC_ADFILE
:
3690 rc
= fruit_unlink_rsrc_adouble(handle
, smb_fname
, force_unlink
);
3693 case FRUIT_RSRC_XATTR
:
3694 rc
= fruit_unlink_rsrc_xattr(handle
, smb_fname
, force_unlink
);
3698 DBG_ERR("Unsupported rsrc config [%d]\n", config
->rsrc
);
3705 static int fruit_unlink(vfs_handle_struct
*handle
,
3706 const struct smb_filename
*smb_fname
)
3709 struct fruit_config_data
*config
= NULL
;
3710 struct smb_filename
*rsrc_smb_fname
= NULL
;
3712 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3713 struct fruit_config_data
, return -1);
3715 if (is_afpinfo_stream(smb_fname
)) {
3716 return fruit_unlink_meta(handle
, smb_fname
);
3717 } else if (is_afpresource_stream(smb_fname
)) {
3718 return fruit_unlink_rsrc(handle
, smb_fname
, false);
3719 } if (is_ntfs_stream_smb_fname(smb_fname
)) {
3720 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3724 * A request to delete the base file. Because 0 byte resource
3725 * fork streams are not listed by fruit_streaminfo,
3726 * delete_all_streams() can't remove 0 byte resource fork
3727 * streams, so we have to cleanup this here.
3729 rsrc_smb_fname
= synthetic_smb_fname(talloc_tos(),
3730 smb_fname
->base_name
,
3731 AFPRESOURCE_STREAM_NAME
,
3734 if (rsrc_smb_fname
== NULL
) {
3738 rc
= fruit_unlink_rsrc(handle
, rsrc_smb_fname
, true);
3739 if ((rc
!= 0) && (errno
!= ENOENT
)) {
3740 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3741 smb_fname_str_dbg(rsrc_smb_fname
), strerror(errno
));
3742 TALLOC_FREE(rsrc_smb_fname
);
3745 TALLOC_FREE(rsrc_smb_fname
);
3747 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3750 static int fruit_chmod(vfs_handle_struct
*handle
,
3751 const struct smb_filename
*smb_fname
,
3755 struct fruit_config_data
*config
= NULL
;
3756 struct smb_filename
*smb_fname_adp
= NULL
;
3758 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname
, mode
);
3763 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3764 struct fruit_config_data
, return -1);
3766 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3770 if (!VALID_STAT(smb_fname
->st
)) {
3774 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
3778 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_adp
);
3783 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp
->base_name
));
3785 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname_adp
, mode
);
3786 if (errno
== ENOENT
) {
3790 TALLOC_FREE(smb_fname_adp
);
3794 static int fruit_chown(vfs_handle_struct
*handle
,
3795 const struct smb_filename
*smb_fname
,
3800 struct fruit_config_data
*config
= NULL
;
3801 struct smb_filename
*adp_smb_fname
= NULL
;
3803 rc
= SMB_VFS_NEXT_CHOWN(handle
, smb_fname
, uid
, gid
);
3808 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3809 struct fruit_config_data
, return -1);
3811 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3815 if (!VALID_STAT(smb_fname
->st
)) {
3819 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
3823 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
3828 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname
->base_name
));
3830 rc
= SMB_VFS_NEXT_CHOWN(handle
, adp_smb_fname
, uid
, gid
);
3831 if (errno
== ENOENT
) {
3836 TALLOC_FREE(adp_smb_fname
);
3840 static int fruit_rmdir(struct vfs_handle_struct
*handle
,
3841 const struct smb_filename
*smb_fname
)
3845 struct fruit_config_data
*config
;
3847 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3848 struct fruit_config_data
, return -1);
3850 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3855 * Due to there is no way to change bDeleteVetoFiles variable
3856 * from this module, need to clean up ourselves
3859 dh
= SMB_VFS_OPENDIR(handle
->conn
, smb_fname
, NULL
, 0);
3864 while ((de
= SMB_VFS_READDIR(handle
->conn
, dh
, NULL
)) != NULL
) {
3866 struct adouble
*ad
= NULL
;
3868 struct smb_filename
*ad_smb_fname
= NULL
;
3871 match
= strncmp(de
->d_name
,
3872 ADOUBLE_NAME_PREFIX
,
3873 strlen(ADOUBLE_NAME_PREFIX
));
3878 p
= talloc_asprintf(talloc_tos(), "%s/%s",
3879 smb_fname
->base_name
, de
->d_name
);
3881 DBG_ERR("talloc_asprintf failed\n");
3885 ad_smb_fname
= synthetic_smb_fname(talloc_tos(), p
,
3889 if (ad_smb_fname
== NULL
) {
3890 DBG_ERR("synthetic_smb_fname failed\n");
3895 * Check whether it's a valid AppleDouble file, if
3896 * yes, delete it, ignore it otherwise.
3898 ad
= ad_get(talloc_tos(), handle
, ad_smb_fname
, ADOUBLE_RSRC
);
3900 TALLOC_FREE(ad_smb_fname
);
3906 ret
= SMB_VFS_NEXT_UNLINK(handle
, ad_smb_fname
);
3907 TALLOC_FREE(ad_smb_fname
);
3909 DBG_ERR("Deleting [%s] failed\n",
3910 smb_fname_str_dbg(ad_smb_fname
));
3916 SMB_VFS_CLOSEDIR(handle
->conn
, dh
);
3918 return SMB_VFS_NEXT_RMDIR(handle
, smb_fname
);
3921 static ssize_t
fruit_pread_meta_stream(vfs_handle_struct
*handle
,
3922 files_struct
*fsp
, void *data
,
3923 size_t n
, off_t offset
)
3928 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
3934 DBG_ERR("Removing [%s] after short read [%zd]\n",
3935 fsp_str_dbg(fsp
), nread
);
3937 ret
= SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
3939 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp
));
3947 static ssize_t
fruit_pread_meta_adouble(vfs_handle_struct
*handle
,
3948 files_struct
*fsp
, void *data
,
3949 size_t n
, off_t offset
)
3952 struct adouble
*ad
= NULL
;
3953 char afpinfo_buf
[AFP_INFO_SIZE
];
3957 ai
= afpinfo_new(talloc_tos());
3962 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
3968 p
= ad_get_entry(ad
, ADEID_FINDERI
);
3970 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
3975 memcpy(&ai
->afpi_FinderInfo
[0], p
, ADEDLEN_FINDERI
);
3977 nread
= afpinfo_pack(ai
, afpinfo_buf
);
3978 if (nread
!= AFP_INFO_SIZE
) {
3983 memcpy(data
, afpinfo_buf
, n
);
3991 static ssize_t
fruit_pread_meta(vfs_handle_struct
*handle
,
3992 files_struct
*fsp
, void *data
,
3993 size_t n
, off_t offset
)
3995 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4000 * OS X has a off-by-1 error in the offset calculation, so we're
4001 * bug compatible here. It won't hurt, as any relevant real
4002 * world read requests from the AFP_AfpInfo stream will be
4003 * offset=0 n=60. offset is ignored anyway, see below.
4005 if ((offset
< 0) || (offset
>= AFP_INFO_SIZE
+ 1)) {
4009 /* Yes, macOS always reads from offset 0 */
4011 to_return
= MIN(n
, AFP_INFO_SIZE
);
4013 switch (fio
->config
->meta
) {
4014 case FRUIT_META_STREAM
:
4015 nread
= fruit_pread_meta_stream(handle
, fsp
, data
,
4019 case FRUIT_META_NETATALK
:
4020 nread
= fruit_pread_meta_adouble(handle
, fsp
, data
,
4025 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4032 static ssize_t
fruit_pread_rsrc_stream(vfs_handle_struct
*handle
,
4033 files_struct
*fsp
, void *data
,
4034 size_t n
, off_t offset
)
4036 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4039 static ssize_t
fruit_pread_rsrc_xattr(vfs_handle_struct
*handle
,
4040 files_struct
*fsp
, void *data
,
4041 size_t n
, off_t offset
)
4043 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4046 static ssize_t
fruit_pread_rsrc_adouble(vfs_handle_struct
*handle
,
4047 files_struct
*fsp
, void *data
,
4048 size_t n
, off_t offset
)
4050 struct adouble
*ad
= NULL
;
4053 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
4058 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
,
4059 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
4065 static ssize_t
fruit_pread_rsrc(vfs_handle_struct
*handle
,
4066 files_struct
*fsp
, void *data
,
4067 size_t n
, off_t offset
)
4069 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4072 switch (fio
->config
->rsrc
) {
4073 case FRUIT_RSRC_STREAM
:
4074 nread
= fruit_pread_rsrc_stream(handle
, fsp
, data
, n
, offset
);
4077 case FRUIT_RSRC_ADFILE
:
4078 nread
= fruit_pread_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
4081 case FRUIT_RSRC_XATTR
:
4082 nread
= fruit_pread_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
4086 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4093 static ssize_t
fruit_pread(vfs_handle_struct
*handle
,
4094 files_struct
*fsp
, void *data
,
4095 size_t n
, off_t offset
)
4097 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4100 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
4101 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
4104 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4107 if (fio
->type
== ADOUBLE_META
) {
4108 nread
= fruit_pread_meta(handle
, fsp
, data
, n
, offset
);
4110 nread
= fruit_pread_rsrc(handle
, fsp
, data
, n
, offset
);
4113 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp
), nread
);
4117 static bool fruit_must_handle_aio_stream(struct fio
*fio
)
4123 if ((fio
->type
== ADOUBLE_META
) &&
4124 (fio
->config
->meta
== FRUIT_META_NETATALK
))
4129 if ((fio
->type
== ADOUBLE_RSRC
) &&
4130 (fio
->config
->rsrc
== FRUIT_RSRC_ADFILE
))
4138 struct fruit_pread_state
{
4140 struct vfs_aio_state vfs_aio_state
;
4143 static void fruit_pread_done(struct tevent_req
*subreq
);
4145 static struct tevent_req
*fruit_pread_send(
4146 struct vfs_handle_struct
*handle
,
4147 TALLOC_CTX
*mem_ctx
,
4148 struct tevent_context
*ev
,
4149 struct files_struct
*fsp
,
4151 size_t n
, off_t offset
)
4153 struct tevent_req
*req
= NULL
;
4154 struct tevent_req
*subreq
= NULL
;
4155 struct fruit_pread_state
*state
= NULL
;
4156 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4158 req
= tevent_req_create(mem_ctx
, &state
,
4159 struct fruit_pread_state
);
4164 if (fruit_must_handle_aio_stream(fio
)) {
4165 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
4166 if (state
->nread
!= n
) {
4167 if (state
->nread
!= -1) {
4170 tevent_req_error(req
, errno
);
4171 return tevent_req_post(req
, ev
);
4173 tevent_req_done(req
);
4174 return tevent_req_post(req
, ev
);
4177 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
4179 if (tevent_req_nomem(req
, subreq
)) {
4180 return tevent_req_post(req
, ev
);
4182 tevent_req_set_callback(subreq
, fruit_pread_done
, req
);
4186 static void fruit_pread_done(struct tevent_req
*subreq
)
4188 struct tevent_req
*req
= tevent_req_callback_data(
4189 subreq
, struct tevent_req
);
4190 struct fruit_pread_state
*state
= tevent_req_data(
4191 req
, struct fruit_pread_state
);
4193 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
4194 TALLOC_FREE(subreq
);
4196 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
4199 tevent_req_done(req
);
4202 static ssize_t
fruit_pread_recv(struct tevent_req
*req
,
4203 struct vfs_aio_state
*vfs_aio_state
)
4205 struct fruit_pread_state
*state
= tevent_req_data(
4206 req
, struct fruit_pread_state
);
4208 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
4212 *vfs_aio_state
= state
->vfs_aio_state
;
4213 return state
->nread
;
4216 static ssize_t
fruit_pwrite_meta_stream(vfs_handle_struct
*handle
,
4217 files_struct
*fsp
, const void *data
,
4218 size_t n
, off_t offset
)
4224 ai
= afpinfo_unpack(talloc_tos(), data
);
4229 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4230 if (nwritten
!= n
) {
4234 if (!ai_empty_finderinfo(ai
)) {
4238 ok
= set_delete_on_close(
4241 handle
->conn
->session_info
->security_token
,
4242 handle
->conn
->session_info
->unix_token
);
4244 DBG_ERR("set_delete_on_close on [%s] failed\n",
4252 static ssize_t
fruit_pwrite_meta_netatalk(vfs_handle_struct
*handle
,
4253 files_struct
*fsp
, const void *data
,
4254 size_t n
, off_t offset
)
4256 struct adouble
*ad
= NULL
;
4262 ai
= afpinfo_unpack(talloc_tos(), data
);
4267 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
4269 ad
= ad_init(talloc_tos(), handle
, ADOUBLE_META
);
4274 p
= ad_get_entry(ad
, ADEID_FINDERI
);
4276 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
4281 memcpy(p
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
4283 ret
= ad_fset(ad
, fsp
);
4285 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
4292 if (!ai_empty_finderinfo(ai
)) {
4296 ok
= set_delete_on_close(
4299 handle
->conn
->session_info
->security_token
,
4300 handle
->conn
->session_info
->unix_token
);
4302 DBG_ERR("set_delete_on_close on [%s] failed\n",
4310 static ssize_t
fruit_pwrite_meta(vfs_handle_struct
*handle
,
4311 files_struct
*fsp
, const void *data
,
4312 size_t n
, off_t offset
)
4314 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4318 * Writing an all 0 blob to the metadata stream
4319 * results in the stream being removed on a macOS
4320 * server. This ensures we behave the same and it
4321 * verified by the "delete AFP_AfpInfo by writing all
4324 if (n
!= AFP_INFO_SIZE
|| offset
!= 0) {
4325 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4326 (intmax_t)offset
, (intmax_t)n
);
4330 switch (fio
->config
->meta
) {
4331 case FRUIT_META_STREAM
:
4332 nwritten
= fruit_pwrite_meta_stream(handle
, fsp
, data
,
4336 case FRUIT_META_NETATALK
:
4337 nwritten
= fruit_pwrite_meta_netatalk(handle
, fsp
, data
,
4342 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4349 static ssize_t
fruit_pwrite_rsrc_stream(vfs_handle_struct
*handle
,
4350 files_struct
*fsp
, const void *data
,
4351 size_t n
, off_t offset
)
4353 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4356 static ssize_t
fruit_pwrite_rsrc_xattr(vfs_handle_struct
*handle
,
4357 files_struct
*fsp
, const void *data
,
4358 size_t n
, off_t offset
)
4360 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4363 static ssize_t
fruit_pwrite_rsrc_adouble(vfs_handle_struct
*handle
,
4364 files_struct
*fsp
, const void *data
,
4365 size_t n
, off_t offset
)
4367 struct adouble
*ad
= NULL
;
4371 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
4373 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp
));
4377 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
,
4378 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
4379 if (nwritten
!= n
) {
4380 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4381 fsp_str_dbg(fsp
), nwritten
, n
);
4386 if ((n
+ offset
) > ad_getentrylen(ad
, ADEID_RFORK
)) {
4387 ad_setentrylen(ad
, ADEID_RFORK
, n
+ offset
);
4388 ret
= ad_fset(ad
, fsp
);
4390 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
4400 static ssize_t
fruit_pwrite_rsrc(vfs_handle_struct
*handle
,
4401 files_struct
*fsp
, const void *data
,
4402 size_t n
, off_t offset
)
4404 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4407 switch (fio
->config
->rsrc
) {
4408 case FRUIT_RSRC_STREAM
:
4409 nwritten
= fruit_pwrite_rsrc_stream(handle
, fsp
, data
, n
, offset
);
4412 case FRUIT_RSRC_ADFILE
:
4413 nwritten
= fruit_pwrite_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
4416 case FRUIT_RSRC_XATTR
:
4417 nwritten
= fruit_pwrite_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
4421 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4428 static ssize_t
fruit_pwrite(vfs_handle_struct
*handle
,
4429 files_struct
*fsp
, const void *data
,
4430 size_t n
, off_t offset
)
4432 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4435 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
4436 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
4439 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4442 if (fio
->type
== ADOUBLE_META
) {
4443 nwritten
= fruit_pwrite_meta(handle
, fsp
, data
, n
, offset
);
4445 nwritten
= fruit_pwrite_rsrc(handle
, fsp
, data
, n
, offset
);
4448 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp
), nwritten
);
4452 struct fruit_pwrite_state
{
4454 struct vfs_aio_state vfs_aio_state
;
4457 static void fruit_pwrite_done(struct tevent_req
*subreq
);
4459 static struct tevent_req
*fruit_pwrite_send(
4460 struct vfs_handle_struct
*handle
,
4461 TALLOC_CTX
*mem_ctx
,
4462 struct tevent_context
*ev
,
4463 struct files_struct
*fsp
,
4465 size_t n
, off_t offset
)
4467 struct tevent_req
*req
= NULL
;
4468 struct tevent_req
*subreq
= NULL
;
4469 struct fruit_pwrite_state
*state
= NULL
;
4470 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4472 req
= tevent_req_create(mem_ctx
, &state
,
4473 struct fruit_pwrite_state
);
4478 if (fruit_must_handle_aio_stream(fio
)) {
4479 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
4480 if (state
->nwritten
!= n
) {
4481 if (state
->nwritten
!= -1) {
4484 tevent_req_error(req
, errno
);
4485 return tevent_req_post(req
, ev
);
4487 tevent_req_done(req
);
4488 return tevent_req_post(req
, ev
);
4491 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
4493 if (tevent_req_nomem(req
, subreq
)) {
4494 return tevent_req_post(req
, ev
);
4496 tevent_req_set_callback(subreq
, fruit_pwrite_done
, req
);
4500 static void fruit_pwrite_done(struct tevent_req
*subreq
)
4502 struct tevent_req
*req
= tevent_req_callback_data(
4503 subreq
, struct tevent_req
);
4504 struct fruit_pwrite_state
*state
= tevent_req_data(
4505 req
, struct fruit_pwrite_state
);
4507 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
4508 TALLOC_FREE(subreq
);
4510 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
4513 tevent_req_done(req
);
4516 static ssize_t
fruit_pwrite_recv(struct tevent_req
*req
,
4517 struct vfs_aio_state
*vfs_aio_state
)
4519 struct fruit_pwrite_state
*state
= tevent_req_data(
4520 req
, struct fruit_pwrite_state
);
4522 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
4526 *vfs_aio_state
= state
->vfs_aio_state
;
4527 return state
->nwritten
;
4531 * Helper to stat/lstat the base file of an smb_fname.
4533 static int fruit_stat_base(vfs_handle_struct
*handle
,
4534 struct smb_filename
*smb_fname
,
4537 char *tmp_stream_name
;
4540 tmp_stream_name
= smb_fname
->stream_name
;
4541 smb_fname
->stream_name
= NULL
;
4543 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4545 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4547 smb_fname
->stream_name
= tmp_stream_name
;
4551 static int fruit_stat_meta_stream(vfs_handle_struct
*handle
,
4552 struct smb_filename
*smb_fname
,
4558 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4560 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4566 static int fruit_stat_meta_netatalk(vfs_handle_struct
*handle
,
4567 struct smb_filename
*smb_fname
,
4570 struct adouble
*ad
= NULL
;
4572 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
4574 DBG_INFO("fruit_stat_meta %s: %s\n",
4575 smb_fname_str_dbg(smb_fname
), strerror(errno
));
4581 /* Populate the stat struct with info from the base file. */
4582 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
4585 smb_fname
->st
.st_ex_size
= AFP_INFO_SIZE
;
4586 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4587 smb_fname
->stream_name
);
4591 static int fruit_stat_meta(vfs_handle_struct
*handle
,
4592 struct smb_filename
*smb_fname
,
4595 struct fruit_config_data
*config
= NULL
;
4598 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4599 struct fruit_config_data
, return -1);
4601 switch (config
->meta
) {
4602 case FRUIT_META_STREAM
:
4603 ret
= fruit_stat_meta_stream(handle
, smb_fname
, follow_links
);
4606 case FRUIT_META_NETATALK
:
4607 ret
= fruit_stat_meta_netatalk(handle
, smb_fname
, follow_links
);
4611 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
4618 static int fruit_stat_rsrc_netatalk(vfs_handle_struct
*handle
,
4619 struct smb_filename
*smb_fname
,
4622 struct adouble
*ad
= NULL
;
4625 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
4631 /* Populate the stat struct with info from the base file. */
4632 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
4638 smb_fname
->st
.st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
4639 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4640 smb_fname
->stream_name
);
4645 static int fruit_stat_rsrc_stream(vfs_handle_struct
*handle
,
4646 struct smb_filename
*smb_fname
,
4652 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4654 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4660 static int fruit_stat_rsrc_xattr(vfs_handle_struct
*handle
,
4661 struct smb_filename
*smb_fname
,
4664 #ifdef HAVE_ATTROPEN
4668 /* Populate the stat struct with info from the base file. */
4669 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
4674 fd
= attropen(smb_fname
->base_name
,
4675 AFPRESOURCE_EA_NETATALK
,
4681 ret
= sys_fstat(fd
, &smb_fname
->st
, false);
4684 DBG_ERR("fstat [%s:%s] failed\n", smb_fname
->base_name
,
4685 AFPRESOURCE_EA_NETATALK
);
4691 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4692 smb_fname
->stream_name
);
4702 static int fruit_stat_rsrc(vfs_handle_struct
*handle
,
4703 struct smb_filename
*smb_fname
,
4706 struct fruit_config_data
*config
= NULL
;
4709 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
4711 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4712 struct fruit_config_data
, return -1);
4714 switch (config
->rsrc
) {
4715 case FRUIT_RSRC_STREAM
:
4716 ret
= fruit_stat_rsrc_stream(handle
, smb_fname
, follow_links
);
4719 case FRUIT_RSRC_XATTR
:
4720 ret
= fruit_stat_rsrc_xattr(handle
, smb_fname
, follow_links
);
4723 case FRUIT_RSRC_ADFILE
:
4724 ret
= fruit_stat_rsrc_netatalk(handle
, smb_fname
, follow_links
);
4728 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
4735 static int fruit_stat(vfs_handle_struct
*handle
,
4736 struct smb_filename
*smb_fname
)
4740 DEBUG(10, ("fruit_stat called for %s\n",
4741 smb_fname_str_dbg(smb_fname
)));
4743 if (!is_ntfs_stream_smb_fname(smb_fname
)
4744 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
4745 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4747 update_btime(handle
, smb_fname
);
4753 * Note if lp_posix_paths() is true, we can never
4754 * get here as is_ntfs_stream_smb_fname() is
4755 * always false. So we never need worry about
4756 * not following links here.
4759 if (is_afpinfo_stream(smb_fname
)) {
4760 rc
= fruit_stat_meta(handle
, smb_fname
, true);
4761 } else if (is_afpresource_stream(smb_fname
)) {
4762 rc
= fruit_stat_rsrc(handle
, smb_fname
, true);
4764 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4768 update_btime(handle
, smb_fname
);
4769 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
4770 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
4771 smb_fname
->st
.st_ex_blocks
=
4772 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
4777 static int fruit_lstat(vfs_handle_struct
*handle
,
4778 struct smb_filename
*smb_fname
)
4782 DEBUG(10, ("fruit_lstat called for %s\n",
4783 smb_fname_str_dbg(smb_fname
)));
4785 if (!is_ntfs_stream_smb_fname(smb_fname
)
4786 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
4787 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4789 update_btime(handle
, smb_fname
);
4794 if (is_afpinfo_stream(smb_fname
)) {
4795 rc
= fruit_stat_meta(handle
, smb_fname
, false);
4796 } else if (is_afpresource_stream(smb_fname
)) {
4797 rc
= fruit_stat_rsrc(handle
, smb_fname
, false);
4799 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4803 update_btime(handle
, smb_fname
);
4804 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
4805 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
4806 smb_fname
->st
.st_ex_blocks
=
4807 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
4812 static int fruit_fstat_meta_stream(vfs_handle_struct
*handle
,
4814 SMB_STRUCT_STAT
*sbuf
)
4816 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4819 static int fruit_fstat_meta_netatalk(vfs_handle_struct
*handle
,
4821 SMB_STRUCT_STAT
*sbuf
)
4825 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
4830 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
4831 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
4832 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
4837 static int fruit_fstat_meta(vfs_handle_struct
*handle
,
4839 SMB_STRUCT_STAT
*sbuf
,
4844 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
4846 switch (fio
->config
->meta
) {
4847 case FRUIT_META_STREAM
:
4848 ret
= fruit_fstat_meta_stream(handle
, fsp
, sbuf
);
4851 case FRUIT_META_NETATALK
:
4852 ret
= fruit_fstat_meta_netatalk(handle
, fsp
, sbuf
);
4856 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4860 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp
), ret
);
4864 static int fruit_fstat_rsrc_xattr(vfs_handle_struct
*handle
,
4866 SMB_STRUCT_STAT
*sbuf
)
4868 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4871 static int fruit_fstat_rsrc_stream(vfs_handle_struct
*handle
,
4873 SMB_STRUCT_STAT
*sbuf
)
4875 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4878 static int fruit_fstat_rsrc_adouble(vfs_handle_struct
*handle
,
4880 SMB_STRUCT_STAT
*sbuf
)
4882 struct adouble
*ad
= NULL
;
4885 /* Populate the stat struct with info from the base file. */
4886 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
4891 ad
= ad_get(talloc_tos(), handle
,
4892 fsp
->base_fsp
->fsp_name
,
4895 DBG_ERR("ad_get [%s] failed [%s]\n",
4896 fsp_str_dbg(fsp
), strerror(errno
));
4900 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
4901 sbuf
->st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
4902 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
4908 static int fruit_fstat_rsrc(vfs_handle_struct
*handle
, files_struct
*fsp
,
4909 SMB_STRUCT_STAT
*sbuf
, struct fio
*fio
)
4913 switch (fio
->config
->rsrc
) {
4914 case FRUIT_RSRC_STREAM
:
4915 ret
= fruit_fstat_rsrc_stream(handle
, fsp
, sbuf
);
4918 case FRUIT_RSRC_ADFILE
:
4919 ret
= fruit_fstat_rsrc_adouble(handle
, fsp
, sbuf
);
4922 case FRUIT_RSRC_XATTR
:
4923 ret
= fruit_fstat_rsrc_xattr(handle
, fsp
, sbuf
);
4927 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4934 static int fruit_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
4935 SMB_STRUCT_STAT
*sbuf
)
4937 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4941 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4944 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
4946 if (fio
->type
== ADOUBLE_META
) {
4947 rc
= fruit_fstat_meta(handle
, fsp
, sbuf
, fio
);
4949 rc
= fruit_fstat_rsrc(handle
, fsp
, sbuf
, fio
);
4953 sbuf
->st_ex_mode
&= ~S_IFMT
;
4954 sbuf
->st_ex_mode
|= S_IFREG
;
4955 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
4958 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX
"]\n",
4959 fsp_str_dbg(fsp
), rc
, (intmax_t)sbuf
->st_ex_size
);
4963 static NTSTATUS
delete_invalid_meta_stream(
4964 vfs_handle_struct
*handle
,
4965 const struct smb_filename
*smb_fname
,
4966 TALLOC_CTX
*mem_ctx
,
4967 unsigned int *pnum_streams
,
4968 struct stream_struct
**pstreams
)
4970 struct smb_filename
*sname
= NULL
;
4974 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
, AFPINFO_STREAM
);
4976 return NT_STATUS_INTERNAL_ERROR
;
4979 sname
= synthetic_smb_fname(talloc_tos(),
4980 smb_fname
->base_name
,
4981 AFPINFO_STREAM_NAME
,
4983 if (sname
== NULL
) {
4984 return NT_STATUS_NO_MEMORY
;
4987 ret
= SMB_VFS_NEXT_UNLINK(handle
, sname
);
4990 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname
));
4991 return map_nt_error_from_unix(errno
);
4994 return NT_STATUS_OK
;
4997 static NTSTATUS
fruit_streaminfo_meta_stream(
4998 vfs_handle_struct
*handle
,
4999 struct files_struct
*fsp
,
5000 const struct smb_filename
*smb_fname
,
5001 TALLOC_CTX
*mem_ctx
,
5002 unsigned int *pnum_streams
,
5003 struct stream_struct
**pstreams
)
5005 struct stream_struct
*stream
= *pstreams
;
5006 unsigned int num_streams
= *pnum_streams
;
5007 struct smb_filename
*sname
= NULL
;
5008 char *full_name
= NULL
;
5010 struct share_mode_lock
*lck
= NULL
;
5011 struct file_id id
= {0};
5012 bool delete_on_close_set
;
5018 for (i
= 0; i
< num_streams
; i
++) {
5019 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
5024 if (i
== num_streams
) {
5025 return NT_STATUS_OK
;
5028 if (stream
[i
].size
!= AFP_INFO_SIZE
) {
5029 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5030 (intmax_t)stream
[i
].size
, smb_fname_str_dbg(smb_fname
));
5032 return delete_invalid_meta_stream(handle
, smb_fname
, mem_ctx
,
5033 pnum_streams
, pstreams
);
5037 * Now check if there's a delete-on-close pending on the stream. If so,
5038 * hide the stream. This behaviour was verified against a macOS 10.12
5042 sname
= synthetic_smb_fname(talloc_tos(),
5043 smb_fname
->base_name
,
5044 AFPINFO_STREAM_NAME
,
5046 if (sname
== NULL
) {
5047 status
= NT_STATUS_NO_MEMORY
;
5051 ret
= SMB_VFS_NEXT_STAT(handle
, sname
);
5053 status
= map_nt_error_from_unix(errno
);
5057 id
= SMB_VFS_NEXT_FILE_ID_CREATE(handle
, &sname
->st
);
5059 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
5061 status
= NT_STATUS_OK
;
5065 full_name
= talloc_asprintf(talloc_tos(),
5069 if (full_name
== NULL
) {
5070 status
= NT_STATUS_NO_MEMORY
;
5074 status
= file_name_hash(handle
->conn
, full_name
, &name_hash
);
5075 if (!NT_STATUS_IS_OK(status
)) {
5079 delete_on_close_set
= is_delete_on_close_set(lck
, name_hash
);
5080 if (delete_on_close_set
) {
5081 ok
= del_fruit_stream(mem_ctx
,
5086 status
= NT_STATUS_INTERNAL_ERROR
;
5091 status
= NT_STATUS_OK
;
5096 TALLOC_FREE(full_name
);
5100 static NTSTATUS
fruit_streaminfo_meta_netatalk(
5101 vfs_handle_struct
*handle
,
5102 struct files_struct
*fsp
,
5103 const struct smb_filename
*smb_fname
,
5104 TALLOC_CTX
*mem_ctx
,
5105 unsigned int *pnum_streams
,
5106 struct stream_struct
**pstreams
)
5108 struct stream_struct
*stream
= *pstreams
;
5109 unsigned int num_streams
= *pnum_streams
;
5110 struct adouble
*ad
= NULL
;
5115 /* Remove the Netatalk xattr from the list */
5116 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5117 ":" NETATALK_META_XATTR
":$DATA");
5119 return NT_STATUS_NO_MEMORY
;
5123 * Check if there's a AFPINFO_STREAM from the VFS streams
5124 * backend and if yes, remove it from the list
5126 for (i
= 0; i
< num_streams
; i
++) {
5127 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
5132 if (i
< num_streams
) {
5133 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5134 smb_fname_str_dbg(smb_fname
));
5136 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5139 return NT_STATUS_INTERNAL_ERROR
;
5143 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
5145 return NT_STATUS_OK
;
5148 is_fi_empty
= ad_empty_finderinfo(ad
);
5152 return NT_STATUS_OK
;
5155 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5156 AFPINFO_STREAM_NAME
, AFP_INFO_SIZE
,
5157 smb_roundup(handle
->conn
, AFP_INFO_SIZE
));
5159 return NT_STATUS_NO_MEMORY
;
5162 return NT_STATUS_OK
;
5165 static NTSTATUS
fruit_streaminfo_meta(vfs_handle_struct
*handle
,
5166 struct files_struct
*fsp
,
5167 const struct smb_filename
*smb_fname
,
5168 TALLOC_CTX
*mem_ctx
,
5169 unsigned int *pnum_streams
,
5170 struct stream_struct
**pstreams
)
5172 struct fruit_config_data
*config
= NULL
;
5175 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5176 return NT_STATUS_INTERNAL_ERROR
);
5178 switch (config
->meta
) {
5179 case FRUIT_META_NETATALK
:
5180 status
= fruit_streaminfo_meta_netatalk(handle
, fsp
, smb_fname
,
5181 mem_ctx
, pnum_streams
,
5185 case FRUIT_META_STREAM
:
5186 status
= fruit_streaminfo_meta_stream(handle
, fsp
, smb_fname
,
5187 mem_ctx
, pnum_streams
,
5192 return NT_STATUS_INTERNAL_ERROR
;
5198 static NTSTATUS
fruit_streaminfo_rsrc_stream(
5199 vfs_handle_struct
*handle
,
5200 struct files_struct
*fsp
,
5201 const struct smb_filename
*smb_fname
,
5202 TALLOC_CTX
*mem_ctx
,
5203 unsigned int *pnum_streams
,
5204 struct stream_struct
**pstreams
)
5208 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
5210 DBG_ERR("Filtering resource stream failed\n");
5211 return NT_STATUS_INTERNAL_ERROR
;
5213 return NT_STATUS_OK
;
5216 static NTSTATUS
fruit_streaminfo_rsrc_xattr(
5217 vfs_handle_struct
*handle
,
5218 struct files_struct
*fsp
,
5219 const struct smb_filename
*smb_fname
,
5220 TALLOC_CTX
*mem_ctx
,
5221 unsigned int *pnum_streams
,
5222 struct stream_struct
**pstreams
)
5226 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
5228 DBG_ERR("Filtering resource stream failed\n");
5229 return NT_STATUS_INTERNAL_ERROR
;
5231 return NT_STATUS_OK
;
5234 static NTSTATUS
fruit_streaminfo_rsrc_adouble(
5235 vfs_handle_struct
*handle
,
5236 struct files_struct
*fsp
,
5237 const struct smb_filename
*smb_fname
,
5238 TALLOC_CTX
*mem_ctx
,
5239 unsigned int *pnum_streams
,
5240 struct stream_struct
**pstreams
)
5242 struct stream_struct
*stream
= *pstreams
;
5243 unsigned int num_streams
= *pnum_streams
;
5244 struct adouble
*ad
= NULL
;
5250 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5251 * and if yes, remove it from the list
5253 for (i
= 0; i
< num_streams
; i
++) {
5254 if (strequal_m(stream
[i
].name
, AFPRESOURCE_STREAM
)) {
5259 if (i
< num_streams
) {
5260 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5261 smb_fname_str_dbg(smb_fname
));
5263 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5264 AFPRESOURCE_STREAM
);
5266 return NT_STATUS_INTERNAL_ERROR
;
5270 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
5272 return NT_STATUS_OK
;
5275 rlen
= ad_getentrylen(ad
, ADEID_RFORK
);
5279 return NT_STATUS_OK
;
5282 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5283 AFPRESOURCE_STREAM_NAME
, rlen
,
5284 smb_roundup(handle
->conn
, rlen
));
5286 return NT_STATUS_NO_MEMORY
;
5289 return NT_STATUS_OK
;
5292 static NTSTATUS
fruit_streaminfo_rsrc(vfs_handle_struct
*handle
,
5293 struct files_struct
*fsp
,
5294 const struct smb_filename
*smb_fname
,
5295 TALLOC_CTX
*mem_ctx
,
5296 unsigned int *pnum_streams
,
5297 struct stream_struct
**pstreams
)
5299 struct fruit_config_data
*config
= NULL
;
5302 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5303 return NT_STATUS_INTERNAL_ERROR
);
5305 switch (config
->rsrc
) {
5306 case FRUIT_RSRC_STREAM
:
5307 status
= fruit_streaminfo_rsrc_stream(handle
, fsp
, smb_fname
,
5308 mem_ctx
, pnum_streams
,
5312 case FRUIT_RSRC_XATTR
:
5313 status
= fruit_streaminfo_rsrc_xattr(handle
, fsp
, smb_fname
,
5314 mem_ctx
, pnum_streams
,
5318 case FRUIT_RSRC_ADFILE
:
5319 status
= fruit_streaminfo_rsrc_adouble(handle
, fsp
, smb_fname
,
5320 mem_ctx
, pnum_streams
,
5325 return NT_STATUS_INTERNAL_ERROR
;
5331 static NTSTATUS
fruit_streaminfo(vfs_handle_struct
*handle
,
5332 struct files_struct
*fsp
,
5333 const struct smb_filename
*smb_fname
,
5334 TALLOC_CTX
*mem_ctx
,
5335 unsigned int *pnum_streams
,
5336 struct stream_struct
**pstreams
)
5338 struct fruit_config_data
*config
= NULL
;
5341 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5342 return NT_STATUS_UNSUCCESSFUL
);
5344 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
5346 status
= SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, smb_fname
, mem_ctx
,
5347 pnum_streams
, pstreams
);
5348 if (!NT_STATUS_IS_OK(status
)) {
5352 status
= fruit_streaminfo_meta(handle
, fsp
, smb_fname
,
5353 mem_ctx
, pnum_streams
, pstreams
);
5354 if (!NT_STATUS_IS_OK(status
)) {
5358 status
= fruit_streaminfo_rsrc(handle
, fsp
, smb_fname
,
5359 mem_ctx
, pnum_streams
, pstreams
);
5360 if (!NT_STATUS_IS_OK(status
)) {
5364 return NT_STATUS_OK
;
5367 static int fruit_ntimes(vfs_handle_struct
*handle
,
5368 const struct smb_filename
*smb_fname
,
5369 struct smb_file_time
*ft
)
5372 struct adouble
*ad
= NULL
;
5373 struct fruit_config_data
*config
= NULL
;
5375 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5378 if ((config
->meta
!= FRUIT_META_NETATALK
) ||
5379 null_timespec(ft
->create_time
))
5381 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
5384 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname
),
5385 time_to_asc(convert_timespec_to_time_t(ft
->create_time
))));
5387 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
5392 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
,
5393 convert_time_t_to_uint32_t(ft
->create_time
.tv_sec
));
5395 rc
= ad_set(ad
, smb_fname
);
5401 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname
)));
5404 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
5407 static int fruit_fallocate(struct vfs_handle_struct
*handle
,
5408 struct files_struct
*fsp
,
5413 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5416 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
5419 /* Let the pwrite code path handle it. */
5424 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct
*handle
,
5425 struct files_struct
*fsp
,
5429 return SMB_VFS_FREMOVEXATTR(fsp
, AFPRESOURCE_EA_NETATALK
);
5432 #ifdef HAVE_ATTROPEN
5433 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5438 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct
*handle
,
5439 struct files_struct
*fsp
,
5443 struct adouble
*ad
= NULL
;
5446 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
5448 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5449 fsp_str_dbg(fsp
), strerror(errno
));
5453 ad_off
= ad_getentryoff(ad
, ADEID_RFORK
);
5455 rc
= ftruncate(fsp
->fh
->fd
, offset
+ ad_off
);
5461 ad_setentrylen(ad
, ADEID_RFORK
, offset
);
5463 rc
= ad_fset(ad
, fsp
);
5465 DBG_ERR("ad_fset [%s] failed [%s]\n",
5466 fsp_str_dbg(fsp
), strerror(errno
));
5475 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct
*handle
,
5476 struct files_struct
*fsp
,
5480 return SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
5483 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5486 static int fruit_ftruncate_rsrc(struct vfs_handle_struct
*handle
,
5487 struct files_struct
*fsp
,
5490 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5493 switch (fio
->config
->rsrc
) {
5494 case FRUIT_RSRC_XATTR
:
5495 ret
= fruit_ftruncate_rsrc_xattr(handle
, fsp
, offset
);
5498 case FRUIT_RSRC_ADFILE
:
5499 ret
= fruit_ftruncate_rsrc_adouble(handle
, fsp
, offset
);
5502 case FRUIT_RSRC_STREAM
:
5503 ret
= fruit_ftruncate_rsrc_stream(handle
, fsp
, offset
);
5507 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
5515 static int fruit_ftruncate_meta(struct vfs_handle_struct
*handle
,
5516 struct files_struct
*fsp
,
5520 DBG_WARNING("ftruncate %s to %jd",
5521 fsp_str_dbg(fsp
), (intmax_t)offset
);
5522 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5527 /* OS X returns success but does nothing */
5528 DBG_INFO("ignoring ftruncate %s to %jd\n",
5529 fsp_str_dbg(fsp
), (intmax_t)offset
);
5533 static int fruit_ftruncate(struct vfs_handle_struct
*handle
,
5534 struct files_struct
*fsp
,
5537 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5540 DBG_DEBUG("Path [%s] offset [%"PRIdMAX
"]\n", fsp_str_dbg(fsp
),
5544 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5547 if (fio
->type
== ADOUBLE_META
) {
5548 ret
= fruit_ftruncate_meta(handle
, fsp
, offset
);
5550 ret
= fruit_ftruncate_rsrc(handle
, fsp
, offset
);
5553 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp
), ret
);
5557 static NTSTATUS
fruit_create_file(vfs_handle_struct
*handle
,
5558 struct smb_request
*req
,
5559 uint16_t root_dir_fid
,
5560 struct smb_filename
*smb_fname
,
5561 uint32_t access_mask
,
5562 uint32_t share_access
,
5563 uint32_t create_disposition
,
5564 uint32_t create_options
,
5565 uint32_t file_attributes
,
5566 uint32_t oplock_request
,
5567 struct smb2_lease
*lease
,
5568 uint64_t allocation_size
,
5569 uint32_t private_flags
,
5570 struct security_descriptor
*sd
,
5571 struct ea_list
*ea_list
,
5572 files_struct
**result
,
5574 const struct smb2_create_blobs
*in_context_blobs
,
5575 struct smb2_create_blobs
*out_context_blobs
)
5578 struct fruit_config_data
*config
= NULL
;
5579 files_struct
*fsp
= NULL
;
5581 status
= check_aapl(handle
, req
, in_context_blobs
, out_context_blobs
);
5582 if (!NT_STATUS_IS_OK(status
)) {
5586 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5587 return NT_STATUS_UNSUCCESSFUL
);
5589 status
= SMB_VFS_NEXT_CREATE_FILE(
5590 handle
, req
, root_dir_fid
, smb_fname
,
5591 access_mask
, share_access
,
5592 create_disposition
, create_options
,
5593 file_attributes
, oplock_request
,
5595 allocation_size
, private_flags
,
5596 sd
, ea_list
, result
,
5597 pinfo
, in_context_blobs
, out_context_blobs
);
5598 if (!NT_STATUS_IS_OK(status
)) {
5604 if (global_fruit_config
.nego_aapl
) {
5605 if (config
->posix_rename
&& fsp
->is_directory
) {
5607 * Enable POSIX directory rename behaviour
5609 fsp
->posix_flags
|= FSP_POSIX_FLAGS_RENAME
;
5614 * If this is a plain open for existing files, opening an 0
5615 * byte size resource fork MUST fail with
5616 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5618 * Cf the vfs_fruit torture tests in test_rfork_create().
5620 if (is_afpresource_stream(fsp
->fsp_name
) &&
5621 create_disposition
== FILE_OPEN
)
5623 if (fsp
->fsp_name
->st
.st_ex_size
== 0) {
5624 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5629 if (is_ntfs_stream_smb_fname(smb_fname
)
5630 || fsp
->is_directory
) {
5634 if (config
->locking
== FRUIT_LOCKING_NETATALK
) {
5635 status
= fruit_check_access(
5638 map_share_mode_to_deny_mode(share_access
, 0));
5639 if (!NT_STATUS_IS_OK(status
)) {
5647 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status
)));
5650 close_file(req
, fsp
, ERROR_CLOSE
);
5651 *result
= fsp
= NULL
;
5657 static NTSTATUS
fruit_readdir_attr(struct vfs_handle_struct
*handle
,
5658 const struct smb_filename
*fname
,
5659 TALLOC_CTX
*mem_ctx
,
5660 struct readdir_attr_data
**pattr_data
)
5662 struct fruit_config_data
*config
= NULL
;
5663 struct readdir_attr_data
*attr_data
;
5666 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5667 struct fruit_config_data
,
5668 return NT_STATUS_UNSUCCESSFUL
);
5670 if (!global_fruit_config
.nego_aapl
) {
5671 return SMB_VFS_NEXT_READDIR_ATTR(handle
, fname
, mem_ctx
, pattr_data
);
5674 DEBUG(10, ("fruit_readdir_attr %s\n", fname
->base_name
));
5676 *pattr_data
= talloc_zero(mem_ctx
, struct readdir_attr_data
);
5677 if (*pattr_data
== NULL
) {
5678 return NT_STATUS_UNSUCCESSFUL
;
5680 attr_data
= *pattr_data
;
5681 attr_data
->type
= RDATTR_AAPL
;
5684 * Mac metadata: compressed FinderInfo, resource fork length
5687 status
= readdir_attr_macmeta(handle
, fname
, attr_data
);
5688 if (!NT_STATUS_IS_OK(status
)) {
5690 * Error handling is tricky: if we return failure from
5691 * this function, the corresponding directory entry
5692 * will to be passed to the client, so we really just
5693 * want to error out on fatal errors.
5695 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
5703 if (config
->unix_info_enabled
) {
5704 attr_data
->attr_data
.aapl
.unix_mode
= fname
->st
.st_ex_mode
;
5710 if (!config
->readdir_attr_max_access
) {
5711 attr_data
->attr_data
.aapl
.max_access
= FILE_GENERIC_ALL
;
5713 status
= smbd_calculate_access_mask(
5717 SEC_FLAG_MAXIMUM_ALLOWED
,
5718 &attr_data
->attr_data
.aapl
.max_access
);
5719 if (!NT_STATUS_IS_OK(status
)) {
5724 return NT_STATUS_OK
;
5727 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5728 fname
->base_name
, nt_errstr(status
)));
5729 TALLOC_FREE(*pattr_data
);
5733 static NTSTATUS
fruit_fget_nt_acl(vfs_handle_struct
*handle
,
5735 uint32_t security_info
,
5736 TALLOC_CTX
*mem_ctx
,
5737 struct security_descriptor
**ppdesc
)
5740 struct security_ace ace
;
5742 struct fruit_config_data
*config
;
5743 bool remove_ok
= false;
5745 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5746 struct fruit_config_data
,
5747 return NT_STATUS_UNSUCCESSFUL
);
5749 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
5751 if (!NT_STATUS_IS_OK(status
)) {
5756 * Add MS NFS style ACEs with uid, gid and mode
5758 if (!global_fruit_config
.nego_aapl
) {
5759 return NT_STATUS_OK
;
5761 if (!config
->unix_info_enabled
) {
5762 return NT_STATUS_OK
;
5765 /* MS NFS style mode */
5766 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
, fsp
->fsp_name
->st
.st_ex_mode
);
5767 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5769 /* First remove any existing ACE's with this SID. */
5770 status
= security_descriptor_dacl_del(*ppdesc
, &sid
);
5771 remove_ok
= (NT_STATUS_IS_OK(status
) ||
5772 NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
));
5774 DBG_WARNING("failed to remove MS NFS_mode style ACE\n");
5777 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5778 if (!NT_STATUS_IS_OK(status
)) {
5779 DEBUG(1,("failed to add MS NFS style ACE\n"));
5783 /* MS NFS style uid */
5784 sid_compose(&sid
, &global_sid_Unix_NFS_Users
, fsp
->fsp_name
->st
.st_ex_uid
);
5785 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5787 /* First remove any existing ACE's with this SID. */
5788 status
= security_descriptor_dacl_del(*ppdesc
, &sid
);
5789 remove_ok
= (NT_STATUS_IS_OK(status
) ||
5790 NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
));
5792 DBG_WARNING("failed to remove MS NFS_users style ACE\n");
5795 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5796 if (!NT_STATUS_IS_OK(status
)) {
5797 DEBUG(1,("failed to add MS NFS style ACE\n"));
5801 /* MS NFS style gid */
5802 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
, fsp
->fsp_name
->st
.st_ex_gid
);
5803 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5805 /* First remove any existing ACE's with this SID. */
5806 status
= security_descriptor_dacl_del(*ppdesc
, &sid
);
5807 remove_ok
= (NT_STATUS_IS_OK(status
) ||
5808 NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
));
5810 DBG_WARNING("failed to remove MS NFS_groups style ACE\n");
5813 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5814 if (!NT_STATUS_IS_OK(status
)) {
5815 DEBUG(1,("failed to add MS NFS style ACE\n"));
5819 return NT_STATUS_OK
;
5822 static NTSTATUS
fruit_fset_nt_acl(vfs_handle_struct
*handle
,
5824 uint32_t security_info_sent
,
5825 const struct security_descriptor
*orig_psd
)
5829 mode_t ms_nfs_mode
= 0;
5831 struct security_descriptor
*psd
= NULL
;
5832 uint32_t orig_num_aces
= 0;
5834 if (orig_psd
->dacl
!= NULL
) {
5835 orig_num_aces
= orig_psd
->dacl
->num_aces
;
5838 psd
= security_descriptor_copy(talloc_tos(), orig_psd
);
5840 return NT_STATUS_NO_MEMORY
;
5843 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp
));
5845 status
= check_ms_nfs(handle
, fsp
, psd
, &ms_nfs_mode
, &do_chmod
);
5846 if (!NT_STATUS_IS_OK(status
)) {
5847 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp
)));
5853 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5854 * sent/present flags correctly now we've removed them.
5857 if (orig_num_aces
!= 0) {
5859 * Are there any ACE's left ?
5861 if (psd
->dacl
->num_aces
== 0) {
5862 /* No - clear the DACL sent/present flags. */
5863 security_info_sent
&= ~SECINFO_DACL
;
5864 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
5868 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
5869 if (!NT_STATUS_IS_OK(status
)) {
5870 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp
)));
5876 if (fsp
->fh
->fd
!= -1) {
5877 result
= SMB_VFS_FCHMOD(fsp
, ms_nfs_mode
);
5879 result
= SMB_VFS_CHMOD(fsp
->conn
,
5885 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp
),
5886 result
, (unsigned)ms_nfs_mode
,
5888 status
= map_nt_error_from_unix(errno
);
5895 return NT_STATUS_OK
;
5898 static struct vfs_offload_ctx
*fruit_offload_ctx
;
5900 struct fruit_offload_read_state
{
5901 struct vfs_handle_struct
*handle
;
5902 struct tevent_context
*ev
;
5908 static void fruit_offload_read_done(struct tevent_req
*subreq
);
5910 static struct tevent_req
*fruit_offload_read_send(
5911 TALLOC_CTX
*mem_ctx
,
5912 struct tevent_context
*ev
,
5913 struct vfs_handle_struct
*handle
,
5920 struct tevent_req
*req
= NULL
;
5921 struct tevent_req
*subreq
= NULL
;
5922 struct fruit_offload_read_state
*state
= NULL
;
5924 req
= tevent_req_create(mem_ctx
, &state
,
5925 struct fruit_offload_read_state
);
5929 *state
= (struct fruit_offload_read_state
) {
5936 subreq
= SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx
, ev
, handle
, fsp
,
5937 fsctl
, ttl
, offset
, to_copy
);
5938 if (tevent_req_nomem(subreq
, req
)) {
5939 return tevent_req_post(req
, ev
);
5941 tevent_req_set_callback(subreq
, fruit_offload_read_done
, req
);
5945 static void fruit_offload_read_done(struct tevent_req
*subreq
)
5947 struct tevent_req
*req
= tevent_req_callback_data(
5948 subreq
, struct tevent_req
);
5949 struct fruit_offload_read_state
*state
= tevent_req_data(
5950 req
, struct fruit_offload_read_state
);
5953 status
= SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq
,
5957 TALLOC_FREE(subreq
);
5958 if (tevent_req_nterror(req
, status
)) {
5962 if (state
->fsctl
!= FSCTL_SRV_REQUEST_RESUME_KEY
) {
5963 tevent_req_done(req
);
5967 status
= vfs_offload_token_ctx_init(state
->fsp
->conn
->sconn
->client
,
5968 &fruit_offload_ctx
);
5969 if (tevent_req_nterror(req
, status
)) {
5973 status
= vfs_offload_token_db_store_fsp(fruit_offload_ctx
,
5976 if (tevent_req_nterror(req
, status
)) {
5980 tevent_req_done(req
);
5984 static NTSTATUS
fruit_offload_read_recv(struct tevent_req
*req
,
5985 struct vfs_handle_struct
*handle
,
5986 TALLOC_CTX
*mem_ctx
,
5989 struct fruit_offload_read_state
*state
= tevent_req_data(
5990 req
, struct fruit_offload_read_state
);
5993 if (tevent_req_is_nterror(req
, &status
)) {
5994 tevent_req_received(req
);
5998 token
->length
= state
->token
.length
;
5999 token
->data
= talloc_move(mem_ctx
, &state
->token
.data
);
6001 tevent_req_received(req
);
6002 return NT_STATUS_OK
;
6005 struct fruit_offload_write_state
{
6006 struct vfs_handle_struct
*handle
;
6008 struct files_struct
*src_fsp
;
6009 struct files_struct
*dst_fsp
;
6013 static void fruit_offload_write_done(struct tevent_req
*subreq
);
6014 static struct tevent_req
*fruit_offload_write_send(struct vfs_handle_struct
*handle
,
6015 TALLOC_CTX
*mem_ctx
,
6016 struct tevent_context
*ev
,
6019 off_t transfer_offset
,
6020 struct files_struct
*dest_fsp
,
6024 struct tevent_req
*req
, *subreq
;
6025 struct fruit_offload_write_state
*state
;
6027 struct fruit_config_data
*config
;
6028 off_t src_off
= transfer_offset
;
6029 files_struct
*src_fsp
= NULL
;
6030 off_t to_copy
= num
;
6031 bool copyfile_enabled
= false;
6033 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6034 (uintmax_t)src_off
, (uintmax_t)dest_off
, (uintmax_t)num
));
6036 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
6037 struct fruit_config_data
,
6040 req
= tevent_req_create(mem_ctx
, &state
,
6041 struct fruit_offload_write_state
);
6045 state
->handle
= handle
;
6046 state
->dst_fsp
= dest_fsp
;
6049 case FSCTL_SRV_COPYCHUNK
:
6050 case FSCTL_SRV_COPYCHUNK_WRITE
:
6051 copyfile_enabled
= config
->copyfile_enabled
;
6058 * Check if this a OS X copyfile style copychunk request with
6059 * a requested chunk count of 0 that was translated to a
6060 * offload_write_send VFS call overloading the parameters src_off
6061 * = dest_off = num = 0.
6063 if (copyfile_enabled
&& num
== 0 && src_off
== 0 && dest_off
== 0) {
6064 status
= vfs_offload_token_db_fetch_fsp(
6065 fruit_offload_ctx
, token
, &src_fsp
);
6066 if (tevent_req_nterror(req
, status
)) {
6067 return tevent_req_post(req
, ev
);
6069 state
->src_fsp
= src_fsp
;
6071 status
= vfs_stat_fsp(src_fsp
);
6072 if (tevent_req_nterror(req
, status
)) {
6073 return tevent_req_post(req
, ev
);
6076 to_copy
= src_fsp
->fsp_name
->st
.st_ex_size
;
6077 state
->is_copyfile
= true;
6080 subreq
= SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle
,
6089 if (tevent_req_nomem(subreq
, req
)) {
6090 return tevent_req_post(req
, ev
);
6093 tevent_req_set_callback(subreq
, fruit_offload_write_done
, req
);
6097 static void fruit_offload_write_done(struct tevent_req
*subreq
)
6099 struct tevent_req
*req
= tevent_req_callback_data(
6100 subreq
, struct tevent_req
);
6101 struct fruit_offload_write_state
*state
= tevent_req_data(
6102 req
, struct fruit_offload_write_state
);
6104 unsigned int num_streams
= 0;
6105 struct stream_struct
*streams
= NULL
;
6107 struct smb_filename
*src_fname_tmp
= NULL
;
6108 struct smb_filename
*dst_fname_tmp
= NULL
;
6110 status
= SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state
->handle
,
6113 TALLOC_FREE(subreq
);
6114 if (tevent_req_nterror(req
, status
)) {
6118 if (!state
->is_copyfile
) {
6119 tevent_req_done(req
);
6124 * Now copy all remaining streams. We know the share supports
6125 * streams, because we're in vfs_fruit. We don't do this async
6126 * because streams are few and small.
6128 status
= vfs_streaminfo(state
->handle
->conn
, state
->src_fsp
,
6129 state
->src_fsp
->fsp_name
,
6130 req
, &num_streams
, &streams
);
6131 if (tevent_req_nterror(req
, status
)) {
6135 if (num_streams
== 1) {
6136 /* There is always one stream, ::$DATA. */
6137 tevent_req_done(req
);
6141 for (i
= 0; i
< num_streams
; i
++) {
6142 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6143 __func__
, streams
[i
].name
, (size_t)streams
[i
].size
));
6145 src_fname_tmp
= synthetic_smb_fname(
6147 state
->src_fsp
->fsp_name
->base_name
,
6150 state
->src_fsp
->fsp_name
->flags
);
6151 if (tevent_req_nomem(src_fname_tmp
, req
)) {
6155 if (is_ntfs_default_stream_smb_fname(src_fname_tmp
)) {
6156 TALLOC_FREE(src_fname_tmp
);
6160 dst_fname_tmp
= synthetic_smb_fname(
6162 state
->dst_fsp
->fsp_name
->base_name
,
6165 state
->dst_fsp
->fsp_name
->flags
);
6166 if (tevent_req_nomem(dst_fname_tmp
, req
)) {
6167 TALLOC_FREE(src_fname_tmp
);
6171 status
= copy_file(req
,
6172 state
->handle
->conn
,
6175 OPENX_FILE_CREATE_IF_NOT_EXIST
,
6177 if (!NT_STATUS_IS_OK(status
)) {
6178 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__
,
6179 smb_fname_str_dbg(src_fname_tmp
),
6180 smb_fname_str_dbg(dst_fname_tmp
),
6181 nt_errstr(status
)));
6182 TALLOC_FREE(src_fname_tmp
);
6183 TALLOC_FREE(dst_fname_tmp
);
6184 tevent_req_nterror(req
, status
);
6188 TALLOC_FREE(src_fname_tmp
);
6189 TALLOC_FREE(dst_fname_tmp
);
6192 TALLOC_FREE(streams
);
6193 TALLOC_FREE(src_fname_tmp
);
6194 TALLOC_FREE(dst_fname_tmp
);
6195 tevent_req_done(req
);
6198 static NTSTATUS
fruit_offload_write_recv(struct vfs_handle_struct
*handle
,
6199 struct tevent_req
*req
,
6202 struct fruit_offload_write_state
*state
= tevent_req_data(
6203 req
, struct fruit_offload_write_state
);
6206 if (tevent_req_is_nterror(req
, &status
)) {
6207 DEBUG(1, ("server side copy chunk failed: %s\n",
6208 nt_errstr(status
)));
6210 tevent_req_received(req
);
6214 *copied
= state
->copied
;
6215 tevent_req_received(req
);
6217 return NT_STATUS_OK
;
6220 static struct vfs_fn_pointers vfs_fruit_fns
= {
6221 .connect_fn
= fruit_connect
,
6223 /* File operations */
6224 .chmod_fn
= fruit_chmod
,
6225 .chown_fn
= fruit_chown
,
6226 .unlink_fn
= fruit_unlink
,
6227 .rename_fn
= fruit_rename
,
6228 .rmdir_fn
= fruit_rmdir
,
6229 .open_fn
= fruit_open
,
6230 .pread_fn
= fruit_pread
,
6231 .pwrite_fn
= fruit_pwrite
,
6232 .pread_send_fn
= fruit_pread_send
,
6233 .pread_recv_fn
= fruit_pread_recv
,
6234 .pwrite_send_fn
= fruit_pwrite_send
,
6235 .pwrite_recv_fn
= fruit_pwrite_recv
,
6236 .stat_fn
= fruit_stat
,
6237 .lstat_fn
= fruit_lstat
,
6238 .fstat_fn
= fruit_fstat
,
6239 .streaminfo_fn
= fruit_streaminfo
,
6240 .ntimes_fn
= fruit_ntimes
,
6241 .ftruncate_fn
= fruit_ftruncate
,
6242 .fallocate_fn
= fruit_fallocate
,
6243 .create_file_fn
= fruit_create_file
,
6244 .readdir_attr_fn
= fruit_readdir_attr
,
6245 .offload_read_send_fn
= fruit_offload_read_send
,
6246 .offload_read_recv_fn
= fruit_offload_read_recv
,
6247 .offload_write_send_fn
= fruit_offload_write_send
,
6248 .offload_write_recv_fn
= fruit_offload_write_recv
,
6250 /* NT ACL operations */
6251 .fget_nt_acl_fn
= fruit_fget_nt_acl
,
6252 .fset_nt_acl_fn
= fruit_fset_nt_acl
,
6255 NTSTATUS
vfs_fruit_init(TALLOC_CTX
*);
6256 NTSTATUS
vfs_fruit_init(TALLOC_CTX
*ctx
)
6258 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fruit",
6260 if (!NT_STATUS_IS_OK(ret
)) {
6264 vfs_fruit_debug_level
= debug_add_class("fruit");
6265 if (vfs_fruit_debug_level
== -1) {
6266 vfs_fruit_debug_level
= DBGC_VFS
;
6267 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6270 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6271 "vfs_fruit_init","fruit",vfs_fruit_debug_level
));