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 int cmp
= dom_sid_compare_domain(
2951 &global_sid_Unix_NFS
,
2952 &psd
->dacl
->aces
[i
].trustee
);
2954 /* Normal ACE entry. */
2959 * security_descriptor_dacl_del()
2960 * *must* return NT_STATUS_OK as we know
2961 * we have something to remove.
2964 status
= security_descriptor_dacl_del(psd
,
2965 &psd
->dacl
->aces
[i
].trustee
);
2966 if (!NT_STATUS_IS_OK(status
)) {
2967 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
2973 * security_descriptor_dacl_del() may delete more
2974 * then one entry subsequent to this one if the
2975 * SID matches, but we only need to ensure that
2976 * we stay looking at the same element in the array.
2980 return NT_STATUS_OK
;
2983 /* Search MS NFS style ACE with UNIX mode */
2984 static NTSTATUS
check_ms_nfs(vfs_handle_struct
*handle
,
2986 struct security_descriptor
*psd
,
2991 struct fruit_config_data
*config
= NULL
;
2995 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2996 struct fruit_config_data
,
2997 return NT_STATUS_UNSUCCESSFUL
);
2999 if (!global_fruit_config
.nego_aapl
) {
3000 return NT_STATUS_OK
;
3002 if (psd
->dacl
== NULL
|| !config
->unix_info_enabled
) {
3003 return NT_STATUS_OK
;
3006 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
3007 if (dom_sid_compare_domain(
3008 &global_sid_Unix_NFS_Mode
,
3009 &psd
->dacl
->aces
[i
].trustee
) == 0) {
3010 *pmode
= (mode_t
)psd
->dacl
->aces
[i
].trustee
.sub_auths
[2];
3011 *pmode
&= (S_IRWXU
| S_IRWXG
| S_IRWXO
);
3014 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3015 fsp_str_dbg(fsp
), (unsigned)(*pmode
)));
3021 * Remove any incoming virtual ACE entries generated by
3022 * fruit_fget_nt_acl().
3025 return remove_virtual_nfs_aces(psd
);
3028 /****************************************************************************
3030 ****************************************************************************/
3032 static int fruit_connect(vfs_handle_struct
*handle
,
3033 const char *service
,
3037 char *list
= NULL
, *newlist
= NULL
;
3038 struct fruit_config_data
*config
;
3040 DEBUG(10, ("fruit_connect\n"));
3042 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
3047 rc
= init_fruit_config(handle
);
3052 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3053 struct fruit_config_data
, return -1);
3055 if (config
->veto_appledouble
) {
3056 list
= lp_veto_files(talloc_tos(), SNUM(handle
->conn
));
3059 if (strstr(list
, "/" ADOUBLE_NAME_PREFIX
"*/") == NULL
) {
3060 newlist
= talloc_asprintf(
3062 "%s/" ADOUBLE_NAME_PREFIX
"*/",
3064 lp_do_parameter(SNUM(handle
->conn
),
3069 lp_do_parameter(SNUM(handle
->conn
),
3071 "/" ADOUBLE_NAME_PREFIX
"*/");
3077 if (config
->encoding
== FRUIT_ENC_NATIVE
) {
3078 lp_do_parameter(SNUM(handle
->conn
),
3086 static int fruit_open_meta_stream(vfs_handle_struct
*handle
,
3087 struct smb_filename
*smb_fname
,
3093 char afpinfo_buf
[AFP_INFO_SIZE
];
3094 ssize_t len
, written
;
3098 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3103 if (!(flags
& (O_CREAT
| O_TRUNC
))) {
3107 ai
= afpinfo_new(talloc_tos());
3113 len
= afpinfo_pack(ai
, afpinfo_buf
);
3114 if (len
!= AFP_INFO_SIZE
) {
3119 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3120 fsp
->fh
->fd
= hostfd
;
3122 written
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, afpinfo_buf
,
3125 if (written
!= AFP_INFO_SIZE
) {
3126 DBG_ERR("bad write [%zd/%d]\n", written
, AFP_INFO_SIZE
);
3134 DBG_DEBUG("rc=%d, fd=%d\n", rc
, hostfd
);
3137 int saved_errno
= errno
;
3139 fsp
->fh
->fd
= hostfd
;
3140 SMB_VFS_NEXT_CLOSE(handle
, fsp
);
3143 errno
= saved_errno
;
3148 static int fruit_open_meta_netatalk(vfs_handle_struct
*handle
,
3149 struct smb_filename
*smb_fname
,
3156 struct adouble
*ad
= NULL
;
3159 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3162 * Return a valid fd, but ensure any attempt to use it returns an error
3163 * (EPIPE). All operations on the smb_fname or the fsp will use path
3173 if (flags
& (O_CREAT
| O_TRUNC
)) {
3175 * The attribute does not exist or needs to be truncated,
3176 * create an AppleDouble EA
3178 ad
= ad_init(fsp
, handle
, ADOUBLE_META
);
3184 rc
= ad_set(ad
, fsp
->fsp_name
);
3194 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc
, fakefd
));
3196 int saved_errno
= errno
;
3201 errno
= saved_errno
;
3206 static int fruit_open_meta(vfs_handle_struct
*handle
,
3207 struct smb_filename
*smb_fname
,
3208 files_struct
*fsp
, int flags
, mode_t mode
)
3211 struct fruit_config_data
*config
= NULL
;
3212 struct fio
*fio
= NULL
;
3214 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname
));
3216 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3217 struct fruit_config_data
, return -1);
3219 switch (config
->meta
) {
3220 case FRUIT_META_STREAM
:
3221 fd
= fruit_open_meta_stream(handle
, smb_fname
,
3225 case FRUIT_META_NETATALK
:
3226 fd
= fruit_open_meta_netatalk(handle
, smb_fname
,
3231 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
3235 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3241 fio
= (struct fio
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
3242 fio
->type
= ADOUBLE_META
;
3243 fio
->config
= config
;
3248 static int fruit_open_rsrc_adouble(vfs_handle_struct
*handle
,
3249 struct smb_filename
*smb_fname
,
3255 struct adouble
*ad
= NULL
;
3256 struct smb_filename
*smb_fname_base
= NULL
;
3257 struct fruit_config_data
*config
= NULL
;
3260 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3261 struct fruit_config_data
, return -1);
3263 if ((!(flags
& O_CREAT
)) &&
3264 S_ISDIR(fsp
->base_fsp
->fsp_name
->st
.st_ex_mode
))
3266 /* sorry, but directories don't habe a resource fork */
3271 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_base
);
3276 /* Sanitize flags */
3277 if (flags
& O_WRONLY
) {
3278 /* We always need read access for the metadata header too */
3283 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname_base
, fsp
,
3290 if (flags
& (O_CREAT
| O_TRUNC
)) {
3291 ad
= ad_init(fsp
, handle
, ADOUBLE_RSRC
);
3297 fsp
->fh
->fd
= hostfd
;
3299 rc
= ad_fset(ad
, fsp
);
3310 TALLOC_FREE(smb_fname_base
);
3312 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc
, hostfd
));
3314 int saved_errno
= errno
;
3317 * BUGBUGBUG -- we would need to call
3318 * fd_close_posix here, but we don't have a
3321 fsp
->fh
->fd
= hostfd
;
3325 errno
= saved_errno
;
3330 static int fruit_open_rsrc_xattr(vfs_handle_struct
*handle
,
3331 struct smb_filename
*smb_fname
,
3336 #ifdef HAVE_ATTROPEN
3339 fd
= attropen(smb_fname
->base_name
,
3340 AFPRESOURCE_EA_NETATALK
,
3355 static int fruit_open_rsrc(vfs_handle_struct
*handle
,
3356 struct smb_filename
*smb_fname
,
3357 files_struct
*fsp
, int flags
, mode_t mode
)
3360 struct fruit_config_data
*config
= NULL
;
3361 struct fio
*fio
= NULL
;
3363 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3365 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3366 struct fruit_config_data
, return -1);
3368 if (((flags
& O_ACCMODE
) == O_RDONLY
)
3369 && (flags
& O_CREAT
)
3370 && !VALID_STAT(fsp
->fsp_name
->st
))
3373 * This means the stream doesn't exist. macOS SMB server fails
3374 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3375 * 12565 and the test for this combination in
3376 * test_rfork_create().
3382 switch (config
->rsrc
) {
3383 case FRUIT_RSRC_STREAM
:
3384 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3387 case FRUIT_RSRC_ADFILE
:
3388 fd
= fruit_open_rsrc_adouble(handle
, smb_fname
,
3392 case FRUIT_RSRC_XATTR
:
3393 fd
= fruit_open_rsrc_xattr(handle
, smb_fname
,
3398 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
3402 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3408 fio
= (struct fio
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
3409 fio
->type
= ADOUBLE_RSRC
;
3410 fio
->config
= config
;
3415 static int fruit_open(vfs_handle_struct
*handle
,
3416 struct smb_filename
*smb_fname
,
3417 files_struct
*fsp
, int flags
, mode_t mode
)
3421 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3423 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
3424 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3427 if (is_afpinfo_stream(smb_fname
)) {
3428 fd
= fruit_open_meta(handle
, smb_fname
, fsp
, flags
, mode
);
3429 } else if (is_afpresource_stream(smb_fname
)) {
3430 fd
= fruit_open_rsrc(handle
, smb_fname
, fsp
, flags
, mode
);
3432 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
3435 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
3440 static int fruit_rename(struct vfs_handle_struct
*handle
,
3441 const struct smb_filename
*smb_fname_src
,
3442 const struct smb_filename
*smb_fname_dst
)
3445 struct fruit_config_data
*config
= NULL
;
3446 struct smb_filename
*src_adp_smb_fname
= NULL
;
3447 struct smb_filename
*dst_adp_smb_fname
= NULL
;
3449 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3450 struct fruit_config_data
, return -1);
3452 if (!VALID_STAT(smb_fname_src
->st
)) {
3453 DBG_ERR("Need valid stat for [%s]\n",
3454 smb_fname_str_dbg(smb_fname_src
));
3458 rc
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
3463 if ((config
->rsrc
!= FRUIT_RSRC_ADFILE
) ||
3464 (!S_ISREG(smb_fname_src
->st
.st_ex_mode
)))
3469 rc
= adouble_path(talloc_tos(), smb_fname_src
, &src_adp_smb_fname
);
3474 rc
= adouble_path(talloc_tos(), smb_fname_dst
, &dst_adp_smb_fname
);
3479 DBG_DEBUG("%s -> %s\n",
3480 smb_fname_str_dbg(src_adp_smb_fname
),
3481 smb_fname_str_dbg(dst_adp_smb_fname
));
3483 rc
= SMB_VFS_NEXT_RENAME(handle
, src_adp_smb_fname
, dst_adp_smb_fname
);
3484 if (errno
== ENOENT
) {
3489 TALLOC_FREE(src_adp_smb_fname
);
3490 TALLOC_FREE(dst_adp_smb_fname
);
3494 static int fruit_unlink_meta_stream(vfs_handle_struct
*handle
,
3495 const struct smb_filename
*smb_fname
)
3497 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3500 static int fruit_unlink_meta_netatalk(vfs_handle_struct
*handle
,
3501 const struct smb_filename
*smb_fname
)
3503 return SMB_VFS_REMOVEXATTR(handle
->conn
,
3505 AFPINFO_EA_NETATALK
);
3508 static int fruit_unlink_meta(vfs_handle_struct
*handle
,
3509 const struct smb_filename
*smb_fname
)
3511 struct fruit_config_data
*config
= NULL
;
3514 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3515 struct fruit_config_data
, return -1);
3517 switch (config
->meta
) {
3518 case FRUIT_META_STREAM
:
3519 rc
= fruit_unlink_meta_stream(handle
, smb_fname
);
3522 case FRUIT_META_NETATALK
:
3523 rc
= fruit_unlink_meta_netatalk(handle
, smb_fname
);
3527 DBG_ERR("Unsupported meta config [%d]\n", config
->meta
);
3534 static int fruit_unlink_rsrc_stream(vfs_handle_struct
*handle
,
3535 const struct smb_filename
*smb_fname
,
3540 if (!force_unlink
) {
3541 struct smb_filename
*smb_fname_cp
= NULL
;
3544 smb_fname_cp
= cp_smb_filename(talloc_tos(), smb_fname
);
3545 if (smb_fname_cp
== NULL
) {
3550 * 0 byte resource fork streams are not listed by
3551 * vfs_streaminfo, as a result stream cleanup/deletion of file
3552 * deletion doesn't remove the resourcefork stream.
3555 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_cp
);
3557 TALLOC_FREE(smb_fname_cp
);
3558 DBG_ERR("stat [%s] failed [%s]\n",
3559 smb_fname_str_dbg(smb_fname_cp
), strerror(errno
));
3563 size
= smb_fname_cp
->st
.st_ex_size
;
3564 TALLOC_FREE(smb_fname_cp
);
3567 /* OS X ignores resource fork stream delete requests */
3572 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3573 if ((ret
!= 0) && (errno
== ENOENT
) && force_unlink
) {
3580 static int fruit_unlink_rsrc_adouble(vfs_handle_struct
*handle
,
3581 const struct smb_filename
*smb_fname
,
3585 struct adouble
*ad
= NULL
;
3586 struct smb_filename
*adp_smb_fname
= NULL
;
3588 if (!force_unlink
) {
3589 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
3598 * 0 byte resource fork streams are not listed by
3599 * vfs_streaminfo, as a result stream cleanup/deletion of file
3600 * deletion doesn't remove the resourcefork stream.
3603 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
3604 /* OS X ignores resource fork stream delete requests */
3612 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
3617 rc
= SMB_VFS_NEXT_UNLINK(handle
, adp_smb_fname
);
3618 TALLOC_FREE(adp_smb_fname
);
3619 if ((rc
!= 0) && (errno
== ENOENT
) && force_unlink
) {
3626 static int fruit_unlink_rsrc_xattr(vfs_handle_struct
*handle
,
3627 const struct smb_filename
*smb_fname
,
3631 * OS X ignores resource fork stream delete requests, so nothing to do
3632 * here. Removing the file will remove the xattr anyway, so we don't
3633 * have to take care of removing 0 byte resource forks that could be
3639 static int fruit_unlink_rsrc(vfs_handle_struct
*handle
,
3640 const struct smb_filename
*smb_fname
,
3643 struct fruit_config_data
*config
= NULL
;
3646 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3647 struct fruit_config_data
, return -1);
3649 switch (config
->rsrc
) {
3650 case FRUIT_RSRC_STREAM
:
3651 rc
= fruit_unlink_rsrc_stream(handle
, smb_fname
, force_unlink
);
3654 case FRUIT_RSRC_ADFILE
:
3655 rc
= fruit_unlink_rsrc_adouble(handle
, smb_fname
, force_unlink
);
3658 case FRUIT_RSRC_XATTR
:
3659 rc
= fruit_unlink_rsrc_xattr(handle
, smb_fname
, force_unlink
);
3663 DBG_ERR("Unsupported rsrc config [%d]\n", config
->rsrc
);
3670 static int fruit_unlink(vfs_handle_struct
*handle
,
3671 const struct smb_filename
*smb_fname
)
3674 struct fruit_config_data
*config
= NULL
;
3675 struct smb_filename
*rsrc_smb_fname
= NULL
;
3677 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3678 struct fruit_config_data
, return -1);
3680 if (is_afpinfo_stream(smb_fname
)) {
3681 return fruit_unlink_meta(handle
, smb_fname
);
3682 } else if (is_afpresource_stream(smb_fname
)) {
3683 return fruit_unlink_rsrc(handle
, smb_fname
, false);
3684 } if (is_ntfs_stream_smb_fname(smb_fname
)) {
3685 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3689 * A request to delete the base file. Because 0 byte resource
3690 * fork streams are not listed by fruit_streaminfo,
3691 * delete_all_streams() can't remove 0 byte resource fork
3692 * streams, so we have to cleanup this here.
3694 rsrc_smb_fname
= synthetic_smb_fname(talloc_tos(),
3695 smb_fname
->base_name
,
3696 AFPRESOURCE_STREAM_NAME
,
3699 if (rsrc_smb_fname
== NULL
) {
3703 rc
= fruit_unlink_rsrc(handle
, rsrc_smb_fname
, true);
3704 if ((rc
!= 0) && (errno
!= ENOENT
)) {
3705 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3706 smb_fname_str_dbg(rsrc_smb_fname
), strerror(errno
));
3707 TALLOC_FREE(rsrc_smb_fname
);
3710 TALLOC_FREE(rsrc_smb_fname
);
3712 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
3715 static int fruit_chmod(vfs_handle_struct
*handle
,
3716 const struct smb_filename
*smb_fname
,
3720 struct fruit_config_data
*config
= NULL
;
3721 struct smb_filename
*smb_fname_adp
= NULL
;
3723 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname
, mode
);
3728 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3729 struct fruit_config_data
, return -1);
3731 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3735 if (!VALID_STAT(smb_fname
->st
)) {
3739 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
3743 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_adp
);
3748 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp
->base_name
));
3750 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname_adp
, mode
);
3751 if (errno
== ENOENT
) {
3755 TALLOC_FREE(smb_fname_adp
);
3759 static int fruit_chown(vfs_handle_struct
*handle
,
3760 const struct smb_filename
*smb_fname
,
3765 struct fruit_config_data
*config
= NULL
;
3766 struct smb_filename
*adp_smb_fname
= NULL
;
3768 rc
= SMB_VFS_NEXT_CHOWN(handle
, smb_fname
, uid
, gid
);
3773 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3774 struct fruit_config_data
, return -1);
3776 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3780 if (!VALID_STAT(smb_fname
->st
)) {
3784 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
3788 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
3793 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname
->base_name
));
3795 rc
= SMB_VFS_NEXT_CHOWN(handle
, adp_smb_fname
, uid
, gid
);
3796 if (errno
== ENOENT
) {
3801 TALLOC_FREE(adp_smb_fname
);
3805 static int fruit_rmdir(struct vfs_handle_struct
*handle
,
3806 const struct smb_filename
*smb_fname
)
3810 struct fruit_config_data
*config
;
3812 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3813 struct fruit_config_data
, return -1);
3815 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
3820 * Due to there is no way to change bDeleteVetoFiles variable
3821 * from this module, need to clean up ourselves
3824 dh
= SMB_VFS_OPENDIR(handle
->conn
, smb_fname
, NULL
, 0);
3829 while ((de
= SMB_VFS_READDIR(handle
->conn
, dh
, NULL
)) != NULL
) {
3831 struct adouble
*ad
= NULL
;
3833 struct smb_filename
*ad_smb_fname
= NULL
;
3836 match
= strncmp(de
->d_name
,
3837 ADOUBLE_NAME_PREFIX
,
3838 strlen(ADOUBLE_NAME_PREFIX
));
3843 p
= talloc_asprintf(talloc_tos(), "%s/%s",
3844 smb_fname
->base_name
, de
->d_name
);
3846 DBG_ERR("talloc_asprintf failed\n");
3850 ad_smb_fname
= synthetic_smb_fname(talloc_tos(), p
,
3854 if (ad_smb_fname
== NULL
) {
3855 DBG_ERR("synthetic_smb_fname failed\n");
3860 * Check whether it's a valid AppleDouble file, if
3861 * yes, delete it, ignore it otherwise.
3863 ad
= ad_get(talloc_tos(), handle
, ad_smb_fname
, ADOUBLE_RSRC
);
3865 TALLOC_FREE(ad_smb_fname
);
3871 ret
= SMB_VFS_NEXT_UNLINK(handle
, ad_smb_fname
);
3872 TALLOC_FREE(ad_smb_fname
);
3874 DBG_ERR("Deleting [%s] failed\n",
3875 smb_fname_str_dbg(ad_smb_fname
));
3881 SMB_VFS_CLOSEDIR(handle
->conn
, dh
);
3883 return SMB_VFS_NEXT_RMDIR(handle
, smb_fname
);
3886 static ssize_t
fruit_pread_meta_stream(vfs_handle_struct
*handle
,
3887 files_struct
*fsp
, void *data
,
3888 size_t n
, off_t offset
)
3893 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
3899 DBG_ERR("Removing [%s] after short read [%zd]\n",
3900 fsp_str_dbg(fsp
), nread
);
3902 ret
= SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
3904 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp
));
3912 static ssize_t
fruit_pread_meta_adouble(vfs_handle_struct
*handle
,
3913 files_struct
*fsp
, void *data
,
3914 size_t n
, off_t offset
)
3917 struct adouble
*ad
= NULL
;
3918 char afpinfo_buf
[AFP_INFO_SIZE
];
3922 ai
= afpinfo_new(talloc_tos());
3927 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
3933 p
= ad_get_entry(ad
, ADEID_FINDERI
);
3935 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
3940 memcpy(&ai
->afpi_FinderInfo
[0], p
, ADEDLEN_FINDERI
);
3942 nread
= afpinfo_pack(ai
, afpinfo_buf
);
3943 if (nread
!= AFP_INFO_SIZE
) {
3948 memcpy(data
, afpinfo_buf
, n
);
3956 static ssize_t
fruit_pread_meta(vfs_handle_struct
*handle
,
3957 files_struct
*fsp
, void *data
,
3958 size_t n
, off_t offset
)
3960 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3965 * OS X has a off-by-1 error in the offset calculation, so we're
3966 * bug compatible here. It won't hurt, as any relevant real
3967 * world read requests from the AFP_AfpInfo stream will be
3968 * offset=0 n=60. offset is ignored anyway, see below.
3970 if ((offset
< 0) || (offset
>= AFP_INFO_SIZE
+ 1)) {
3974 /* Yes, macOS always reads from offset 0 */
3976 to_return
= MIN(n
, AFP_INFO_SIZE
);
3978 switch (fio
->config
->meta
) {
3979 case FRUIT_META_STREAM
:
3980 nread
= fruit_pread_meta_stream(handle
, fsp
, data
,
3984 case FRUIT_META_NETATALK
:
3985 nread
= fruit_pread_meta_adouble(handle
, fsp
, data
,
3990 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
3997 static ssize_t
fruit_pread_rsrc_stream(vfs_handle_struct
*handle
,
3998 files_struct
*fsp
, void *data
,
3999 size_t n
, off_t offset
)
4001 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4004 static ssize_t
fruit_pread_rsrc_xattr(vfs_handle_struct
*handle
,
4005 files_struct
*fsp
, void *data
,
4006 size_t n
, off_t offset
)
4008 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4011 static ssize_t
fruit_pread_rsrc_adouble(vfs_handle_struct
*handle
,
4012 files_struct
*fsp
, void *data
,
4013 size_t n
, off_t offset
)
4015 struct adouble
*ad
= NULL
;
4018 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
4023 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
,
4024 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
4030 static ssize_t
fruit_pread_rsrc(vfs_handle_struct
*handle
,
4031 files_struct
*fsp
, void *data
,
4032 size_t n
, off_t offset
)
4034 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4037 switch (fio
->config
->rsrc
) {
4038 case FRUIT_RSRC_STREAM
:
4039 nread
= fruit_pread_rsrc_stream(handle
, fsp
, data
, n
, offset
);
4042 case FRUIT_RSRC_ADFILE
:
4043 nread
= fruit_pread_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
4046 case FRUIT_RSRC_XATTR
:
4047 nread
= fruit_pread_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
4051 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4058 static ssize_t
fruit_pread(vfs_handle_struct
*handle
,
4059 files_struct
*fsp
, void *data
,
4060 size_t n
, off_t offset
)
4062 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4065 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
4066 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
4069 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
4072 if (fio
->type
== ADOUBLE_META
) {
4073 nread
= fruit_pread_meta(handle
, fsp
, data
, n
, offset
);
4075 nread
= fruit_pread_rsrc(handle
, fsp
, data
, n
, offset
);
4078 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp
), nread
);
4082 static bool fruit_must_handle_aio_stream(struct fio
*fio
)
4088 if ((fio
->type
== ADOUBLE_META
) &&
4089 (fio
->config
->meta
== FRUIT_META_NETATALK
))
4094 if ((fio
->type
== ADOUBLE_RSRC
) &&
4095 (fio
->config
->rsrc
== FRUIT_RSRC_ADFILE
))
4103 struct fruit_pread_state
{
4105 struct vfs_aio_state vfs_aio_state
;
4108 static void fruit_pread_done(struct tevent_req
*subreq
);
4110 static struct tevent_req
*fruit_pread_send(
4111 struct vfs_handle_struct
*handle
,
4112 TALLOC_CTX
*mem_ctx
,
4113 struct tevent_context
*ev
,
4114 struct files_struct
*fsp
,
4116 size_t n
, off_t offset
)
4118 struct tevent_req
*req
= NULL
;
4119 struct tevent_req
*subreq
= NULL
;
4120 struct fruit_pread_state
*state
= NULL
;
4121 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4123 req
= tevent_req_create(mem_ctx
, &state
,
4124 struct fruit_pread_state
);
4129 if (fruit_must_handle_aio_stream(fio
)) {
4130 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
4131 if (state
->nread
!= n
) {
4132 if (state
->nread
!= -1) {
4135 tevent_req_error(req
, errno
);
4136 return tevent_req_post(req
, ev
);
4138 tevent_req_done(req
);
4139 return tevent_req_post(req
, ev
);
4142 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
4144 if (tevent_req_nomem(req
, subreq
)) {
4145 return tevent_req_post(req
, ev
);
4147 tevent_req_set_callback(subreq
, fruit_pread_done
, req
);
4151 static void fruit_pread_done(struct tevent_req
*subreq
)
4153 struct tevent_req
*req
= tevent_req_callback_data(
4154 subreq
, struct tevent_req
);
4155 struct fruit_pread_state
*state
= tevent_req_data(
4156 req
, struct fruit_pread_state
);
4158 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
4159 TALLOC_FREE(subreq
);
4161 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
4164 tevent_req_done(req
);
4167 static ssize_t
fruit_pread_recv(struct tevent_req
*req
,
4168 struct vfs_aio_state
*vfs_aio_state
)
4170 struct fruit_pread_state
*state
= tevent_req_data(
4171 req
, struct fruit_pread_state
);
4173 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
4177 *vfs_aio_state
= state
->vfs_aio_state
;
4178 return state
->nread
;
4181 static ssize_t
fruit_pwrite_meta_stream(vfs_handle_struct
*handle
,
4182 files_struct
*fsp
, const void *data
,
4183 size_t n
, off_t offset
)
4189 ai
= afpinfo_unpack(talloc_tos(), data
);
4194 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4195 if (nwritten
!= n
) {
4199 if (!ai_empty_finderinfo(ai
)) {
4203 ok
= set_delete_on_close(
4206 handle
->conn
->session_info
->security_token
,
4207 handle
->conn
->session_info
->unix_token
);
4209 DBG_ERR("set_delete_on_close on [%s] failed\n",
4217 static ssize_t
fruit_pwrite_meta_netatalk(vfs_handle_struct
*handle
,
4218 files_struct
*fsp
, const void *data
,
4219 size_t n
, off_t offset
)
4221 struct adouble
*ad
= NULL
;
4227 ai
= afpinfo_unpack(talloc_tos(), data
);
4232 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
4234 ad
= ad_init(talloc_tos(), handle
, ADOUBLE_META
);
4239 p
= ad_get_entry(ad
, ADEID_FINDERI
);
4241 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
4246 memcpy(p
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
4248 ret
= ad_fset(ad
, fsp
);
4250 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
4257 if (!ai_empty_finderinfo(ai
)) {
4261 ok
= set_delete_on_close(
4264 handle
->conn
->session_info
->security_token
,
4265 handle
->conn
->session_info
->unix_token
);
4267 DBG_ERR("set_delete_on_close on [%s] failed\n",
4275 static ssize_t
fruit_pwrite_meta(vfs_handle_struct
*handle
,
4276 files_struct
*fsp
, const void *data
,
4277 size_t n
, off_t offset
)
4279 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4283 * Writing an all 0 blob to the metadata stream
4284 * results in the stream being removed on a macOS
4285 * server. This ensures we behave the same and it
4286 * verified by the "delete AFP_AfpInfo by writing all
4289 if (n
!= AFP_INFO_SIZE
|| offset
!= 0) {
4290 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4291 (intmax_t)offset
, (intmax_t)n
);
4295 switch (fio
->config
->meta
) {
4296 case FRUIT_META_STREAM
:
4297 nwritten
= fruit_pwrite_meta_stream(handle
, fsp
, data
,
4301 case FRUIT_META_NETATALK
:
4302 nwritten
= fruit_pwrite_meta_netatalk(handle
, fsp
, data
,
4307 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4314 static ssize_t
fruit_pwrite_rsrc_stream(vfs_handle_struct
*handle
,
4315 files_struct
*fsp
, const void *data
,
4316 size_t n
, off_t offset
)
4318 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4321 static ssize_t
fruit_pwrite_rsrc_xattr(vfs_handle_struct
*handle
,
4322 files_struct
*fsp
, const void *data
,
4323 size_t n
, off_t offset
)
4325 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4328 static ssize_t
fruit_pwrite_rsrc_adouble(vfs_handle_struct
*handle
,
4329 files_struct
*fsp
, const void *data
,
4330 size_t n
, off_t offset
)
4332 struct adouble
*ad
= NULL
;
4336 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
4338 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp
));
4342 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
,
4343 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
4344 if (nwritten
!= n
) {
4345 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4346 fsp_str_dbg(fsp
), nwritten
, n
);
4351 if ((n
+ offset
) > ad_getentrylen(ad
, ADEID_RFORK
)) {
4352 ad_setentrylen(ad
, ADEID_RFORK
, n
+ offset
);
4353 ret
= ad_fset(ad
, fsp
);
4355 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
4365 static ssize_t
fruit_pwrite_rsrc(vfs_handle_struct
*handle
,
4366 files_struct
*fsp
, const void *data
,
4367 size_t n
, off_t offset
)
4369 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4372 switch (fio
->config
->rsrc
) {
4373 case FRUIT_RSRC_STREAM
:
4374 nwritten
= fruit_pwrite_rsrc_stream(handle
, fsp
, data
, n
, offset
);
4377 case FRUIT_RSRC_ADFILE
:
4378 nwritten
= fruit_pwrite_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
4381 case FRUIT_RSRC_XATTR
:
4382 nwritten
= fruit_pwrite_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
4386 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4393 static ssize_t
fruit_pwrite(vfs_handle_struct
*handle
,
4394 files_struct
*fsp
, const void *data
,
4395 size_t n
, off_t offset
)
4397 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4400 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
4401 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
4404 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
4407 if (fio
->type
== ADOUBLE_META
) {
4408 nwritten
= fruit_pwrite_meta(handle
, fsp
, data
, n
, offset
);
4410 nwritten
= fruit_pwrite_rsrc(handle
, fsp
, data
, n
, offset
);
4413 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp
), nwritten
);
4417 struct fruit_pwrite_state
{
4419 struct vfs_aio_state vfs_aio_state
;
4422 static void fruit_pwrite_done(struct tevent_req
*subreq
);
4424 static struct tevent_req
*fruit_pwrite_send(
4425 struct vfs_handle_struct
*handle
,
4426 TALLOC_CTX
*mem_ctx
,
4427 struct tevent_context
*ev
,
4428 struct files_struct
*fsp
,
4430 size_t n
, off_t offset
)
4432 struct tevent_req
*req
= NULL
;
4433 struct tevent_req
*subreq
= NULL
;
4434 struct fruit_pwrite_state
*state
= NULL
;
4435 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4437 req
= tevent_req_create(mem_ctx
, &state
,
4438 struct fruit_pwrite_state
);
4443 if (fruit_must_handle_aio_stream(fio
)) {
4444 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
4445 if (state
->nwritten
!= n
) {
4446 if (state
->nwritten
!= -1) {
4449 tevent_req_error(req
, errno
);
4450 return tevent_req_post(req
, ev
);
4452 tevent_req_done(req
);
4453 return tevent_req_post(req
, ev
);
4456 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
4458 if (tevent_req_nomem(req
, subreq
)) {
4459 return tevent_req_post(req
, ev
);
4461 tevent_req_set_callback(subreq
, fruit_pwrite_done
, req
);
4465 static void fruit_pwrite_done(struct tevent_req
*subreq
)
4467 struct tevent_req
*req
= tevent_req_callback_data(
4468 subreq
, struct tevent_req
);
4469 struct fruit_pwrite_state
*state
= tevent_req_data(
4470 req
, struct fruit_pwrite_state
);
4472 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
4473 TALLOC_FREE(subreq
);
4475 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
4478 tevent_req_done(req
);
4481 static ssize_t
fruit_pwrite_recv(struct tevent_req
*req
,
4482 struct vfs_aio_state
*vfs_aio_state
)
4484 struct fruit_pwrite_state
*state
= tevent_req_data(
4485 req
, struct fruit_pwrite_state
);
4487 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
4491 *vfs_aio_state
= state
->vfs_aio_state
;
4492 return state
->nwritten
;
4496 * Helper to stat/lstat the base file of an smb_fname.
4498 static int fruit_stat_base(vfs_handle_struct
*handle
,
4499 struct smb_filename
*smb_fname
,
4502 char *tmp_stream_name
;
4505 tmp_stream_name
= smb_fname
->stream_name
;
4506 smb_fname
->stream_name
= NULL
;
4508 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4510 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4512 smb_fname
->stream_name
= tmp_stream_name
;
4516 static int fruit_stat_meta_stream(vfs_handle_struct
*handle
,
4517 struct smb_filename
*smb_fname
,
4523 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4525 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4531 static int fruit_stat_meta_netatalk(vfs_handle_struct
*handle
,
4532 struct smb_filename
*smb_fname
,
4535 struct adouble
*ad
= NULL
;
4537 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
4539 DBG_INFO("fruit_stat_meta %s: %s\n",
4540 smb_fname_str_dbg(smb_fname
), strerror(errno
));
4546 /* Populate the stat struct with info from the base file. */
4547 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
4550 smb_fname
->st
.st_ex_size
= AFP_INFO_SIZE
;
4551 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4552 smb_fname
->stream_name
);
4556 static int fruit_stat_meta(vfs_handle_struct
*handle
,
4557 struct smb_filename
*smb_fname
,
4560 struct fruit_config_data
*config
= NULL
;
4563 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4564 struct fruit_config_data
, return -1);
4566 switch (config
->meta
) {
4567 case FRUIT_META_STREAM
:
4568 ret
= fruit_stat_meta_stream(handle
, smb_fname
, follow_links
);
4571 case FRUIT_META_NETATALK
:
4572 ret
= fruit_stat_meta_netatalk(handle
, smb_fname
, follow_links
);
4576 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
4583 static int fruit_stat_rsrc_netatalk(vfs_handle_struct
*handle
,
4584 struct smb_filename
*smb_fname
,
4587 struct adouble
*ad
= NULL
;
4590 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
4596 /* Populate the stat struct with info from the base file. */
4597 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
4603 smb_fname
->st
.st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
4604 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4605 smb_fname
->stream_name
);
4610 static int fruit_stat_rsrc_stream(vfs_handle_struct
*handle
,
4611 struct smb_filename
*smb_fname
,
4617 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4619 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4625 static int fruit_stat_rsrc_xattr(vfs_handle_struct
*handle
,
4626 struct smb_filename
*smb_fname
,
4629 #ifdef HAVE_ATTROPEN
4633 /* Populate the stat struct with info from the base file. */
4634 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
4639 fd
= attropen(smb_fname
->base_name
,
4640 AFPRESOURCE_EA_NETATALK
,
4646 ret
= sys_fstat(fd
, &smb_fname
->st
, false);
4649 DBG_ERR("fstat [%s:%s] failed\n", smb_fname
->base_name
,
4650 AFPRESOURCE_EA_NETATALK
);
4656 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
4657 smb_fname
->stream_name
);
4667 static int fruit_stat_rsrc(vfs_handle_struct
*handle
,
4668 struct smb_filename
*smb_fname
,
4671 struct fruit_config_data
*config
= NULL
;
4674 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
4676 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4677 struct fruit_config_data
, return -1);
4679 switch (config
->rsrc
) {
4680 case FRUIT_RSRC_STREAM
:
4681 ret
= fruit_stat_rsrc_stream(handle
, smb_fname
, follow_links
);
4684 case FRUIT_RSRC_XATTR
:
4685 ret
= fruit_stat_rsrc_xattr(handle
, smb_fname
, follow_links
);
4688 case FRUIT_RSRC_ADFILE
:
4689 ret
= fruit_stat_rsrc_netatalk(handle
, smb_fname
, follow_links
);
4693 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
4700 static int fruit_stat(vfs_handle_struct
*handle
,
4701 struct smb_filename
*smb_fname
)
4705 DEBUG(10, ("fruit_stat called for %s\n",
4706 smb_fname_str_dbg(smb_fname
)));
4708 if (!is_ntfs_stream_smb_fname(smb_fname
)
4709 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
4710 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4712 update_btime(handle
, smb_fname
);
4718 * Note if lp_posix_paths() is true, we can never
4719 * get here as is_ntfs_stream_smb_fname() is
4720 * always false. So we never need worry about
4721 * not following links here.
4724 if (is_afpinfo_stream(smb_fname
)) {
4725 rc
= fruit_stat_meta(handle
, smb_fname
, true);
4726 } else if (is_afpresource_stream(smb_fname
)) {
4727 rc
= fruit_stat_rsrc(handle
, smb_fname
, true);
4729 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
4733 update_btime(handle
, smb_fname
);
4734 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
4735 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
4736 smb_fname
->st
.st_ex_blocks
=
4737 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
4742 static int fruit_lstat(vfs_handle_struct
*handle
,
4743 struct smb_filename
*smb_fname
)
4747 DEBUG(10, ("fruit_lstat called for %s\n",
4748 smb_fname_str_dbg(smb_fname
)));
4750 if (!is_ntfs_stream_smb_fname(smb_fname
)
4751 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
4752 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4754 update_btime(handle
, smb_fname
);
4759 if (is_afpinfo_stream(smb_fname
)) {
4760 rc
= fruit_stat_meta(handle
, smb_fname
, false);
4761 } else if (is_afpresource_stream(smb_fname
)) {
4762 rc
= fruit_stat_rsrc(handle
, smb_fname
, false);
4764 return SMB_VFS_NEXT_LSTAT(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_fstat_meta_stream(vfs_handle_struct
*handle
,
4779 SMB_STRUCT_STAT
*sbuf
)
4781 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4784 static int fruit_fstat_meta_netatalk(vfs_handle_struct
*handle
,
4786 SMB_STRUCT_STAT
*sbuf
)
4790 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
4795 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
4796 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
4797 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
4802 static int fruit_fstat_meta(vfs_handle_struct
*handle
,
4804 SMB_STRUCT_STAT
*sbuf
,
4809 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
4811 switch (fio
->config
->meta
) {
4812 case FRUIT_META_STREAM
:
4813 ret
= fruit_fstat_meta_stream(handle
, fsp
, sbuf
);
4816 case FRUIT_META_NETATALK
:
4817 ret
= fruit_fstat_meta_netatalk(handle
, fsp
, sbuf
);
4821 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
4825 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp
), ret
);
4829 static int fruit_fstat_rsrc_xattr(vfs_handle_struct
*handle
,
4831 SMB_STRUCT_STAT
*sbuf
)
4833 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4836 static int fruit_fstat_rsrc_stream(vfs_handle_struct
*handle
,
4838 SMB_STRUCT_STAT
*sbuf
)
4840 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4843 static int fruit_fstat_rsrc_adouble(vfs_handle_struct
*handle
,
4845 SMB_STRUCT_STAT
*sbuf
)
4847 struct adouble
*ad
= NULL
;
4850 /* Populate the stat struct with info from the base file. */
4851 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
4856 ad
= ad_get(talloc_tos(), handle
,
4857 fsp
->base_fsp
->fsp_name
,
4860 DBG_ERR("ad_get [%s] failed [%s]\n",
4861 fsp_str_dbg(fsp
), strerror(errno
));
4865 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
4866 sbuf
->st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
4867 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
4873 static int fruit_fstat_rsrc(vfs_handle_struct
*handle
, files_struct
*fsp
,
4874 SMB_STRUCT_STAT
*sbuf
, struct fio
*fio
)
4878 switch (fio
->config
->rsrc
) {
4879 case FRUIT_RSRC_STREAM
:
4880 ret
= fruit_fstat_rsrc_stream(handle
, fsp
, sbuf
);
4883 case FRUIT_RSRC_ADFILE
:
4884 ret
= fruit_fstat_rsrc_adouble(handle
, fsp
, sbuf
);
4887 case FRUIT_RSRC_XATTR
:
4888 ret
= fruit_fstat_rsrc_xattr(handle
, fsp
, sbuf
);
4892 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4899 static int fruit_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
4900 SMB_STRUCT_STAT
*sbuf
)
4902 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
4906 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
4909 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
4911 if (fio
->type
== ADOUBLE_META
) {
4912 rc
= fruit_fstat_meta(handle
, fsp
, sbuf
, fio
);
4914 rc
= fruit_fstat_rsrc(handle
, fsp
, sbuf
, fio
);
4918 sbuf
->st_ex_mode
&= ~S_IFMT
;
4919 sbuf
->st_ex_mode
|= S_IFREG
;
4920 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
4923 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX
"]\n",
4924 fsp_str_dbg(fsp
), rc
, (intmax_t)sbuf
->st_ex_size
);
4928 static NTSTATUS
delete_invalid_meta_stream(
4929 vfs_handle_struct
*handle
,
4930 const struct smb_filename
*smb_fname
,
4931 TALLOC_CTX
*mem_ctx
,
4932 unsigned int *pnum_streams
,
4933 struct stream_struct
**pstreams
)
4935 struct smb_filename
*sname
= NULL
;
4939 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
, AFPINFO_STREAM
);
4941 return NT_STATUS_INTERNAL_ERROR
;
4944 sname
= synthetic_smb_fname(talloc_tos(),
4945 smb_fname
->base_name
,
4946 AFPINFO_STREAM_NAME
,
4948 if (sname
== NULL
) {
4949 return NT_STATUS_NO_MEMORY
;
4952 ret
= SMB_VFS_NEXT_UNLINK(handle
, sname
);
4955 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname
));
4956 return map_nt_error_from_unix(errno
);
4959 return NT_STATUS_OK
;
4962 static NTSTATUS
fruit_streaminfo_meta_stream(
4963 vfs_handle_struct
*handle
,
4964 struct files_struct
*fsp
,
4965 const struct smb_filename
*smb_fname
,
4966 TALLOC_CTX
*mem_ctx
,
4967 unsigned int *pnum_streams
,
4968 struct stream_struct
**pstreams
)
4970 struct stream_struct
*stream
= *pstreams
;
4971 unsigned int num_streams
= *pnum_streams
;
4972 struct smb_filename
*sname
= NULL
;
4973 char *full_name
= NULL
;
4975 struct share_mode_lock
*lck
= NULL
;
4976 struct file_id id
= {0};
4977 bool delete_on_close_set
;
4983 for (i
= 0; i
< num_streams
; i
++) {
4984 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
4989 if (i
== num_streams
) {
4990 return NT_STATUS_OK
;
4993 if (stream
[i
].size
!= AFP_INFO_SIZE
) {
4994 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
4995 (intmax_t)stream
[i
].size
, smb_fname_str_dbg(smb_fname
));
4997 return delete_invalid_meta_stream(handle
, smb_fname
, mem_ctx
,
4998 pnum_streams
, pstreams
);
5002 * Now check if there's a delete-on-close pending on the stream. If so,
5003 * hide the stream. This behaviour was verified against a macOS 10.12
5007 sname
= synthetic_smb_fname(talloc_tos(),
5008 smb_fname
->base_name
,
5009 AFPINFO_STREAM_NAME
,
5011 if (sname
== NULL
) {
5012 status
= NT_STATUS_NO_MEMORY
;
5016 ret
= SMB_VFS_NEXT_STAT(handle
, sname
);
5018 status
= map_nt_error_from_unix(errno
);
5022 id
= SMB_VFS_NEXT_FILE_ID_CREATE(handle
, &sname
->st
);
5024 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
5026 status
= NT_STATUS_OK
;
5030 full_name
= talloc_asprintf(talloc_tos(),
5034 if (full_name
== NULL
) {
5035 status
= NT_STATUS_NO_MEMORY
;
5039 status
= file_name_hash(handle
->conn
, full_name
, &name_hash
);
5040 if (!NT_STATUS_IS_OK(status
)) {
5044 delete_on_close_set
= is_delete_on_close_set(lck
, name_hash
);
5045 if (delete_on_close_set
) {
5046 ok
= del_fruit_stream(mem_ctx
,
5051 status
= NT_STATUS_INTERNAL_ERROR
;
5056 status
= NT_STATUS_OK
;
5061 TALLOC_FREE(full_name
);
5065 static NTSTATUS
fruit_streaminfo_meta_netatalk(
5066 vfs_handle_struct
*handle
,
5067 struct files_struct
*fsp
,
5068 const struct smb_filename
*smb_fname
,
5069 TALLOC_CTX
*mem_ctx
,
5070 unsigned int *pnum_streams
,
5071 struct stream_struct
**pstreams
)
5073 struct stream_struct
*stream
= *pstreams
;
5074 unsigned int num_streams
= *pnum_streams
;
5075 struct adouble
*ad
= NULL
;
5080 /* Remove the Netatalk xattr from the list */
5081 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5082 ":" NETATALK_META_XATTR
":$DATA");
5084 return NT_STATUS_NO_MEMORY
;
5088 * Check if there's a AFPINFO_STREAM from the VFS streams
5089 * backend and if yes, remove it from the list
5091 for (i
= 0; i
< num_streams
; i
++) {
5092 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
5097 if (i
< num_streams
) {
5098 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5099 smb_fname_str_dbg(smb_fname
));
5101 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5104 return NT_STATUS_INTERNAL_ERROR
;
5108 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
5110 return NT_STATUS_OK
;
5113 is_fi_empty
= ad_empty_finderinfo(ad
);
5117 return NT_STATUS_OK
;
5120 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5121 AFPINFO_STREAM_NAME
, AFP_INFO_SIZE
,
5122 smb_roundup(handle
->conn
, AFP_INFO_SIZE
));
5124 return NT_STATUS_NO_MEMORY
;
5127 return NT_STATUS_OK
;
5130 static NTSTATUS
fruit_streaminfo_meta(vfs_handle_struct
*handle
,
5131 struct files_struct
*fsp
,
5132 const struct smb_filename
*smb_fname
,
5133 TALLOC_CTX
*mem_ctx
,
5134 unsigned int *pnum_streams
,
5135 struct stream_struct
**pstreams
)
5137 struct fruit_config_data
*config
= NULL
;
5140 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5141 return NT_STATUS_INTERNAL_ERROR
);
5143 switch (config
->meta
) {
5144 case FRUIT_META_NETATALK
:
5145 status
= fruit_streaminfo_meta_netatalk(handle
, fsp
, smb_fname
,
5146 mem_ctx
, pnum_streams
,
5150 case FRUIT_META_STREAM
:
5151 status
= fruit_streaminfo_meta_stream(handle
, fsp
, smb_fname
,
5152 mem_ctx
, pnum_streams
,
5157 return NT_STATUS_INTERNAL_ERROR
;
5163 static NTSTATUS
fruit_streaminfo_rsrc_stream(
5164 vfs_handle_struct
*handle
,
5165 struct files_struct
*fsp
,
5166 const struct smb_filename
*smb_fname
,
5167 TALLOC_CTX
*mem_ctx
,
5168 unsigned int *pnum_streams
,
5169 struct stream_struct
**pstreams
)
5173 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
5175 DBG_ERR("Filtering resource stream failed\n");
5176 return NT_STATUS_INTERNAL_ERROR
;
5178 return NT_STATUS_OK
;
5181 static NTSTATUS
fruit_streaminfo_rsrc_xattr(
5182 vfs_handle_struct
*handle
,
5183 struct files_struct
*fsp
,
5184 const struct smb_filename
*smb_fname
,
5185 TALLOC_CTX
*mem_ctx
,
5186 unsigned int *pnum_streams
,
5187 struct stream_struct
**pstreams
)
5191 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
5193 DBG_ERR("Filtering resource stream failed\n");
5194 return NT_STATUS_INTERNAL_ERROR
;
5196 return NT_STATUS_OK
;
5199 static NTSTATUS
fruit_streaminfo_rsrc_adouble(
5200 vfs_handle_struct
*handle
,
5201 struct files_struct
*fsp
,
5202 const struct smb_filename
*smb_fname
,
5203 TALLOC_CTX
*mem_ctx
,
5204 unsigned int *pnum_streams
,
5205 struct stream_struct
**pstreams
)
5207 struct stream_struct
*stream
= *pstreams
;
5208 unsigned int num_streams
= *pnum_streams
;
5209 struct adouble
*ad
= NULL
;
5215 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5216 * and if yes, remove it from the list
5218 for (i
= 0; i
< num_streams
; i
++) {
5219 if (strequal_m(stream
[i
].name
, AFPRESOURCE_STREAM
)) {
5224 if (i
< num_streams
) {
5225 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5226 smb_fname_str_dbg(smb_fname
));
5228 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5229 AFPRESOURCE_STREAM
);
5231 return NT_STATUS_INTERNAL_ERROR
;
5235 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
5237 return NT_STATUS_OK
;
5240 rlen
= ad_getentrylen(ad
, ADEID_RFORK
);
5244 return NT_STATUS_OK
;
5247 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
5248 AFPRESOURCE_STREAM_NAME
, rlen
,
5249 smb_roundup(handle
->conn
, rlen
));
5251 return NT_STATUS_NO_MEMORY
;
5254 return NT_STATUS_OK
;
5257 static NTSTATUS
fruit_streaminfo_rsrc(vfs_handle_struct
*handle
,
5258 struct files_struct
*fsp
,
5259 const struct smb_filename
*smb_fname
,
5260 TALLOC_CTX
*mem_ctx
,
5261 unsigned int *pnum_streams
,
5262 struct stream_struct
**pstreams
)
5264 struct fruit_config_data
*config
= NULL
;
5267 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5268 return NT_STATUS_INTERNAL_ERROR
);
5270 switch (config
->rsrc
) {
5271 case FRUIT_RSRC_STREAM
:
5272 status
= fruit_streaminfo_rsrc_stream(handle
, fsp
, smb_fname
,
5273 mem_ctx
, pnum_streams
,
5277 case FRUIT_RSRC_XATTR
:
5278 status
= fruit_streaminfo_rsrc_xattr(handle
, fsp
, smb_fname
,
5279 mem_ctx
, pnum_streams
,
5283 case FRUIT_RSRC_ADFILE
:
5284 status
= fruit_streaminfo_rsrc_adouble(handle
, fsp
, smb_fname
,
5285 mem_ctx
, pnum_streams
,
5290 return NT_STATUS_INTERNAL_ERROR
;
5296 static NTSTATUS
fruit_streaminfo(vfs_handle_struct
*handle
,
5297 struct files_struct
*fsp
,
5298 const struct smb_filename
*smb_fname
,
5299 TALLOC_CTX
*mem_ctx
,
5300 unsigned int *pnum_streams
,
5301 struct stream_struct
**pstreams
)
5303 struct fruit_config_data
*config
= NULL
;
5306 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5307 return NT_STATUS_UNSUCCESSFUL
);
5309 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
5311 status
= SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, smb_fname
, mem_ctx
,
5312 pnum_streams
, pstreams
);
5313 if (!NT_STATUS_IS_OK(status
)) {
5317 status
= fruit_streaminfo_meta(handle
, fsp
, smb_fname
,
5318 mem_ctx
, pnum_streams
, pstreams
);
5319 if (!NT_STATUS_IS_OK(status
)) {
5323 status
= fruit_streaminfo_rsrc(handle
, fsp
, smb_fname
,
5324 mem_ctx
, pnum_streams
, pstreams
);
5325 if (!NT_STATUS_IS_OK(status
)) {
5329 return NT_STATUS_OK
;
5332 static int fruit_ntimes(vfs_handle_struct
*handle
,
5333 const struct smb_filename
*smb_fname
,
5334 struct smb_file_time
*ft
)
5337 struct adouble
*ad
= NULL
;
5338 struct fruit_config_data
*config
= NULL
;
5340 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5343 if ((config
->meta
!= FRUIT_META_NETATALK
) ||
5344 null_timespec(ft
->create_time
))
5346 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
5349 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname
),
5350 time_to_asc(convert_timespec_to_time_t(ft
->create_time
))));
5352 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
5357 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
,
5358 convert_time_t_to_uint32_t(ft
->create_time
.tv_sec
));
5360 rc
= ad_set(ad
, smb_fname
);
5366 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname
)));
5369 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
5372 static int fruit_fallocate(struct vfs_handle_struct
*handle
,
5373 struct files_struct
*fsp
,
5378 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5381 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
5384 /* Let the pwrite code path handle it. */
5389 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct
*handle
,
5390 struct files_struct
*fsp
,
5394 return SMB_VFS_FREMOVEXATTR(fsp
, AFPRESOURCE_EA_NETATALK
);
5397 #ifdef HAVE_ATTROPEN
5398 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5403 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct
*handle
,
5404 struct files_struct
*fsp
,
5408 struct adouble
*ad
= NULL
;
5411 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
5413 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5414 fsp_str_dbg(fsp
), strerror(errno
));
5418 ad_off
= ad_getentryoff(ad
, ADEID_RFORK
);
5420 rc
= ftruncate(fsp
->fh
->fd
, offset
+ ad_off
);
5426 ad_setentrylen(ad
, ADEID_RFORK
, offset
);
5428 rc
= ad_fset(ad
, fsp
);
5430 DBG_ERR("ad_fset [%s] failed [%s]\n",
5431 fsp_str_dbg(fsp
), strerror(errno
));
5440 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct
*handle
,
5441 struct files_struct
*fsp
,
5445 return SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
5448 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5451 static int fruit_ftruncate_rsrc(struct vfs_handle_struct
*handle
,
5452 struct files_struct
*fsp
,
5455 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5458 switch (fio
->config
->rsrc
) {
5459 case FRUIT_RSRC_XATTR
:
5460 ret
= fruit_ftruncate_rsrc_xattr(handle
, fsp
, offset
);
5463 case FRUIT_RSRC_ADFILE
:
5464 ret
= fruit_ftruncate_rsrc_adouble(handle
, fsp
, offset
);
5467 case FRUIT_RSRC_STREAM
:
5468 ret
= fruit_ftruncate_rsrc_stream(handle
, fsp
, offset
);
5472 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
5480 static int fruit_ftruncate_meta(struct vfs_handle_struct
*handle
,
5481 struct files_struct
*fsp
,
5485 DBG_WARNING("ftruncate %s to %jd",
5486 fsp_str_dbg(fsp
), (intmax_t)offset
);
5487 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5492 /* OS X returns success but does nothing */
5493 DBG_INFO("ignoring ftruncate %s to %jd\n",
5494 fsp_str_dbg(fsp
), (intmax_t)offset
);
5498 static int fruit_ftruncate(struct vfs_handle_struct
*handle
,
5499 struct files_struct
*fsp
,
5502 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
5505 DBG_DEBUG("Path [%s] offset [%"PRIdMAX
"]\n", fsp_str_dbg(fsp
),
5509 if (offset
== 0 && global_fruit_config
.nego_aapl
) {
5510 return SMB_VFS_NEXT_UNLINK(handle
, fsp
->fsp_name
);
5512 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
5515 if (fio
->type
== ADOUBLE_META
) {
5516 ret
= fruit_ftruncate_meta(handle
, fsp
, offset
);
5518 ret
= fruit_ftruncate_rsrc(handle
, fsp
, offset
);
5521 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp
), ret
);
5525 static NTSTATUS
fruit_create_file(vfs_handle_struct
*handle
,
5526 struct smb_request
*req
,
5527 uint16_t root_dir_fid
,
5528 struct smb_filename
*smb_fname
,
5529 uint32_t access_mask
,
5530 uint32_t share_access
,
5531 uint32_t create_disposition
,
5532 uint32_t create_options
,
5533 uint32_t file_attributes
,
5534 uint32_t oplock_request
,
5535 struct smb2_lease
*lease
,
5536 uint64_t allocation_size
,
5537 uint32_t private_flags
,
5538 struct security_descriptor
*sd
,
5539 struct ea_list
*ea_list
,
5540 files_struct
**result
,
5542 const struct smb2_create_blobs
*in_context_blobs
,
5543 struct smb2_create_blobs
*out_context_blobs
)
5546 struct fruit_config_data
*config
= NULL
;
5547 files_struct
*fsp
= NULL
;
5549 status
= check_aapl(handle
, req
, in_context_blobs
, out_context_blobs
);
5550 if (!NT_STATUS_IS_OK(status
)) {
5554 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
5555 return NT_STATUS_UNSUCCESSFUL
);
5557 status
= SMB_VFS_NEXT_CREATE_FILE(
5558 handle
, req
, root_dir_fid
, smb_fname
,
5559 access_mask
, share_access
,
5560 create_disposition
, create_options
,
5561 file_attributes
, oplock_request
,
5563 allocation_size
, private_flags
,
5564 sd
, ea_list
, result
,
5565 pinfo
, in_context_blobs
, out_context_blobs
);
5566 if (!NT_STATUS_IS_OK(status
)) {
5572 if (global_fruit_config
.nego_aapl
) {
5573 if (config
->posix_rename
&& fsp
->is_directory
) {
5575 * Enable POSIX directory rename behaviour
5577 fsp
->posix_flags
|= FSP_POSIX_FLAGS_RENAME
;
5582 * If this is a plain open for existing files, opening an 0
5583 * byte size resource fork MUST fail with
5584 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5586 * Cf the vfs_fruit torture tests in test_rfork_create().
5588 if (is_afpresource_stream(fsp
->fsp_name
) &&
5589 create_disposition
== FILE_OPEN
)
5591 if (fsp
->fsp_name
->st
.st_ex_size
== 0) {
5592 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5597 if (is_ntfs_stream_smb_fname(smb_fname
)
5598 || fsp
->is_directory
) {
5602 if (config
->locking
== FRUIT_LOCKING_NETATALK
) {
5603 status
= fruit_check_access(
5606 map_share_mode_to_deny_mode(share_access
, 0));
5607 if (!NT_STATUS_IS_OK(status
)) {
5615 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status
)));
5618 close_file(req
, fsp
, ERROR_CLOSE
);
5619 *result
= fsp
= NULL
;
5625 static NTSTATUS
fruit_readdir_attr(struct vfs_handle_struct
*handle
,
5626 const struct smb_filename
*fname
,
5627 TALLOC_CTX
*mem_ctx
,
5628 struct readdir_attr_data
**pattr_data
)
5630 struct fruit_config_data
*config
= NULL
;
5631 struct readdir_attr_data
*attr_data
;
5634 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5635 struct fruit_config_data
,
5636 return NT_STATUS_UNSUCCESSFUL
);
5638 if (!global_fruit_config
.nego_aapl
) {
5639 return SMB_VFS_NEXT_READDIR_ATTR(handle
, fname
, mem_ctx
, pattr_data
);
5642 DEBUG(10, ("fruit_readdir_attr %s\n", fname
->base_name
));
5644 *pattr_data
= talloc_zero(mem_ctx
, struct readdir_attr_data
);
5645 if (*pattr_data
== NULL
) {
5646 return NT_STATUS_UNSUCCESSFUL
;
5648 attr_data
= *pattr_data
;
5649 attr_data
->type
= RDATTR_AAPL
;
5652 * Mac metadata: compressed FinderInfo, resource fork length
5655 status
= readdir_attr_macmeta(handle
, fname
, attr_data
);
5656 if (!NT_STATUS_IS_OK(status
)) {
5658 * Error handling is tricky: if we return failure from
5659 * this function, the corresponding directory entry
5660 * will to be passed to the client, so we really just
5661 * want to error out on fatal errors.
5663 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
5671 if (config
->unix_info_enabled
) {
5672 attr_data
->attr_data
.aapl
.unix_mode
= fname
->st
.st_ex_mode
;
5678 if (!config
->readdir_attr_max_access
) {
5679 attr_data
->attr_data
.aapl
.max_access
= FILE_GENERIC_ALL
;
5681 status
= smbd_calculate_access_mask(
5685 SEC_FLAG_MAXIMUM_ALLOWED
,
5686 &attr_data
->attr_data
.aapl
.max_access
);
5687 if (!NT_STATUS_IS_OK(status
)) {
5692 return NT_STATUS_OK
;
5695 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5696 fname
->base_name
, nt_errstr(status
)));
5697 TALLOC_FREE(*pattr_data
);
5701 static NTSTATUS
fruit_fget_nt_acl(vfs_handle_struct
*handle
,
5703 uint32_t security_info
,
5704 TALLOC_CTX
*mem_ctx
,
5705 struct security_descriptor
**ppdesc
)
5708 struct security_ace ace
;
5710 struct fruit_config_data
*config
;
5712 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5713 struct fruit_config_data
,
5714 return NT_STATUS_UNSUCCESSFUL
);
5716 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
5718 if (!NT_STATUS_IS_OK(status
)) {
5723 * Add MS NFS style ACEs with uid, gid and mode
5725 if (!global_fruit_config
.nego_aapl
) {
5726 return NT_STATUS_OK
;
5728 if (!config
->unix_info_enabled
) {
5729 return NT_STATUS_OK
;
5732 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5733 status
= remove_virtual_nfs_aces(*ppdesc
);
5734 if (!NT_STATUS_IS_OK(status
)) {
5735 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5739 /* MS NFS style mode */
5740 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
, fsp
->fsp_name
->st
.st_ex_mode
);
5741 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5742 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5743 if (!NT_STATUS_IS_OK(status
)) {
5744 DEBUG(1,("failed to add MS NFS style ACE\n"));
5748 /* MS NFS style uid */
5749 sid_compose(&sid
, &global_sid_Unix_NFS_Users
, fsp
->fsp_name
->st
.st_ex_uid
);
5750 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5751 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5752 if (!NT_STATUS_IS_OK(status
)) {
5753 DEBUG(1,("failed to add MS NFS style ACE\n"));
5757 /* MS NFS style gid */
5758 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
, fsp
->fsp_name
->st
.st_ex_gid
);
5759 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
5760 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
5761 if (!NT_STATUS_IS_OK(status
)) {
5762 DEBUG(1,("failed to add MS NFS style ACE\n"));
5766 return NT_STATUS_OK
;
5769 static NTSTATUS
fruit_fset_nt_acl(vfs_handle_struct
*handle
,
5771 uint32_t security_info_sent
,
5772 const struct security_descriptor
*orig_psd
)
5776 mode_t ms_nfs_mode
= 0;
5778 struct security_descriptor
*psd
= NULL
;
5779 uint32_t orig_num_aces
= 0;
5781 if (orig_psd
->dacl
!= NULL
) {
5782 orig_num_aces
= orig_psd
->dacl
->num_aces
;
5785 psd
= security_descriptor_copy(talloc_tos(), orig_psd
);
5787 return NT_STATUS_NO_MEMORY
;
5790 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp
));
5792 status
= check_ms_nfs(handle
, fsp
, psd
, &ms_nfs_mode
, &do_chmod
);
5793 if (!NT_STATUS_IS_OK(status
)) {
5794 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp
)));
5800 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5801 * sent/present flags correctly now we've removed them.
5804 if (orig_num_aces
!= 0) {
5806 * Are there any ACE's left ?
5808 if (psd
->dacl
->num_aces
== 0) {
5809 /* No - clear the DACL sent/present flags. */
5810 security_info_sent
&= ~SECINFO_DACL
;
5811 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
5815 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
5816 if (!NT_STATUS_IS_OK(status
)) {
5817 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp
)));
5823 if (fsp
->fh
->fd
!= -1) {
5824 result
= SMB_VFS_FCHMOD(fsp
, ms_nfs_mode
);
5826 result
= SMB_VFS_CHMOD(fsp
->conn
,
5832 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp
),
5833 result
, (unsigned)ms_nfs_mode
,
5835 status
= map_nt_error_from_unix(errno
);
5842 return NT_STATUS_OK
;
5845 static struct vfs_offload_ctx
*fruit_offload_ctx
;
5847 struct fruit_offload_read_state
{
5848 struct vfs_handle_struct
*handle
;
5849 struct tevent_context
*ev
;
5855 static void fruit_offload_read_done(struct tevent_req
*subreq
);
5857 static struct tevent_req
*fruit_offload_read_send(
5858 TALLOC_CTX
*mem_ctx
,
5859 struct tevent_context
*ev
,
5860 struct vfs_handle_struct
*handle
,
5867 struct tevent_req
*req
= NULL
;
5868 struct tevent_req
*subreq
= NULL
;
5869 struct fruit_offload_read_state
*state
= NULL
;
5871 req
= tevent_req_create(mem_ctx
, &state
,
5872 struct fruit_offload_read_state
);
5876 *state
= (struct fruit_offload_read_state
) {
5883 subreq
= SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx
, ev
, handle
, fsp
,
5884 fsctl
, ttl
, offset
, to_copy
);
5885 if (tevent_req_nomem(subreq
, req
)) {
5886 return tevent_req_post(req
, ev
);
5888 tevent_req_set_callback(subreq
, fruit_offload_read_done
, req
);
5892 static void fruit_offload_read_done(struct tevent_req
*subreq
)
5894 struct tevent_req
*req
= tevent_req_callback_data(
5895 subreq
, struct tevent_req
);
5896 struct fruit_offload_read_state
*state
= tevent_req_data(
5897 req
, struct fruit_offload_read_state
);
5900 status
= SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq
,
5904 TALLOC_FREE(subreq
);
5905 if (tevent_req_nterror(req
, status
)) {
5909 if (state
->fsctl
!= FSCTL_SRV_REQUEST_RESUME_KEY
) {
5910 tevent_req_done(req
);
5914 status
= vfs_offload_token_ctx_init(state
->fsp
->conn
->sconn
->client
,
5915 &fruit_offload_ctx
);
5916 if (tevent_req_nterror(req
, status
)) {
5920 status
= vfs_offload_token_db_store_fsp(fruit_offload_ctx
,
5923 if (tevent_req_nterror(req
, status
)) {
5927 tevent_req_done(req
);
5931 static NTSTATUS
fruit_offload_read_recv(struct tevent_req
*req
,
5932 struct vfs_handle_struct
*handle
,
5933 TALLOC_CTX
*mem_ctx
,
5936 struct fruit_offload_read_state
*state
= tevent_req_data(
5937 req
, struct fruit_offload_read_state
);
5940 if (tevent_req_is_nterror(req
, &status
)) {
5941 tevent_req_received(req
);
5945 token
->length
= state
->token
.length
;
5946 token
->data
= talloc_move(mem_ctx
, &state
->token
.data
);
5948 tevent_req_received(req
);
5949 return NT_STATUS_OK
;
5952 struct fruit_offload_write_state
{
5953 struct vfs_handle_struct
*handle
;
5955 struct files_struct
*src_fsp
;
5956 struct files_struct
*dst_fsp
;
5960 static void fruit_offload_write_done(struct tevent_req
*subreq
);
5961 static struct tevent_req
*fruit_offload_write_send(struct vfs_handle_struct
*handle
,
5962 TALLOC_CTX
*mem_ctx
,
5963 struct tevent_context
*ev
,
5966 off_t transfer_offset
,
5967 struct files_struct
*dest_fsp
,
5971 struct tevent_req
*req
, *subreq
;
5972 struct fruit_offload_write_state
*state
;
5974 struct fruit_config_data
*config
;
5975 off_t src_off
= transfer_offset
;
5976 files_struct
*src_fsp
= NULL
;
5977 off_t to_copy
= num
;
5978 bool copyfile_enabled
= false;
5980 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5981 (uintmax_t)src_off
, (uintmax_t)dest_off
, (uintmax_t)num
));
5983 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5984 struct fruit_config_data
,
5987 req
= tevent_req_create(mem_ctx
, &state
,
5988 struct fruit_offload_write_state
);
5992 state
->handle
= handle
;
5993 state
->dst_fsp
= dest_fsp
;
5996 case FSCTL_SRV_COPYCHUNK
:
5997 case FSCTL_SRV_COPYCHUNK_WRITE
:
5998 copyfile_enabled
= config
->copyfile_enabled
;
6005 * Check if this a OS X copyfile style copychunk request with
6006 * a requested chunk count of 0 that was translated to a
6007 * offload_write_send VFS call overloading the parameters src_off
6008 * = dest_off = num = 0.
6010 if (copyfile_enabled
&& num
== 0 && src_off
== 0 && dest_off
== 0) {
6011 status
= vfs_offload_token_db_fetch_fsp(
6012 fruit_offload_ctx
, token
, &src_fsp
);
6013 if (tevent_req_nterror(req
, status
)) {
6014 return tevent_req_post(req
, ev
);
6016 state
->src_fsp
= src_fsp
;
6018 status
= vfs_stat_fsp(src_fsp
);
6019 if (tevent_req_nterror(req
, status
)) {
6020 return tevent_req_post(req
, ev
);
6023 to_copy
= src_fsp
->fsp_name
->st
.st_ex_size
;
6024 state
->is_copyfile
= true;
6027 subreq
= SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle
,
6036 if (tevent_req_nomem(subreq
, req
)) {
6037 return tevent_req_post(req
, ev
);
6040 tevent_req_set_callback(subreq
, fruit_offload_write_done
, req
);
6044 static void fruit_offload_write_done(struct tevent_req
*subreq
)
6046 struct tevent_req
*req
= tevent_req_callback_data(
6047 subreq
, struct tevent_req
);
6048 struct fruit_offload_write_state
*state
= tevent_req_data(
6049 req
, struct fruit_offload_write_state
);
6051 unsigned int num_streams
= 0;
6052 struct stream_struct
*streams
= NULL
;
6054 struct smb_filename
*src_fname_tmp
= NULL
;
6055 struct smb_filename
*dst_fname_tmp
= NULL
;
6057 status
= SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state
->handle
,
6060 TALLOC_FREE(subreq
);
6061 if (tevent_req_nterror(req
, status
)) {
6065 if (!state
->is_copyfile
) {
6066 tevent_req_done(req
);
6071 * Now copy all remaining streams. We know the share supports
6072 * streams, because we're in vfs_fruit. We don't do this async
6073 * because streams are few and small.
6075 status
= vfs_streaminfo(state
->handle
->conn
, state
->src_fsp
,
6076 state
->src_fsp
->fsp_name
,
6077 req
, &num_streams
, &streams
);
6078 if (tevent_req_nterror(req
, status
)) {
6082 if (num_streams
== 1) {
6083 /* There is always one stream, ::$DATA. */
6084 tevent_req_done(req
);
6088 for (i
= 0; i
< num_streams
; i
++) {
6089 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6090 __func__
, streams
[i
].name
, (size_t)streams
[i
].size
));
6092 src_fname_tmp
= synthetic_smb_fname(
6094 state
->src_fsp
->fsp_name
->base_name
,
6097 state
->src_fsp
->fsp_name
->flags
);
6098 if (tevent_req_nomem(src_fname_tmp
, req
)) {
6102 if (is_ntfs_default_stream_smb_fname(src_fname_tmp
)) {
6103 TALLOC_FREE(src_fname_tmp
);
6107 dst_fname_tmp
= synthetic_smb_fname(
6109 state
->dst_fsp
->fsp_name
->base_name
,
6112 state
->dst_fsp
->fsp_name
->flags
);
6113 if (tevent_req_nomem(dst_fname_tmp
, req
)) {
6114 TALLOC_FREE(src_fname_tmp
);
6118 status
= copy_file(req
,
6119 state
->handle
->conn
,
6122 OPENX_FILE_CREATE_IF_NOT_EXIST
,
6124 if (!NT_STATUS_IS_OK(status
)) {
6125 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__
,
6126 smb_fname_str_dbg(src_fname_tmp
),
6127 smb_fname_str_dbg(dst_fname_tmp
),
6128 nt_errstr(status
)));
6129 TALLOC_FREE(src_fname_tmp
);
6130 TALLOC_FREE(dst_fname_tmp
);
6131 tevent_req_nterror(req
, status
);
6135 TALLOC_FREE(src_fname_tmp
);
6136 TALLOC_FREE(dst_fname_tmp
);
6139 TALLOC_FREE(streams
);
6140 TALLOC_FREE(src_fname_tmp
);
6141 TALLOC_FREE(dst_fname_tmp
);
6142 tevent_req_done(req
);
6145 static NTSTATUS
fruit_offload_write_recv(struct vfs_handle_struct
*handle
,
6146 struct tevent_req
*req
,
6149 struct fruit_offload_write_state
*state
= tevent_req_data(
6150 req
, struct fruit_offload_write_state
);
6153 if (tevent_req_is_nterror(req
, &status
)) {
6154 DEBUG(1, ("server side copy chunk failed: %s\n",
6155 nt_errstr(status
)));
6157 tevent_req_received(req
);
6161 *copied
= state
->copied
;
6162 tevent_req_received(req
);
6164 return NT_STATUS_OK
;
6167 static struct vfs_fn_pointers vfs_fruit_fns
= {
6168 .connect_fn
= fruit_connect
,
6170 /* File operations */
6171 .chmod_fn
= fruit_chmod
,
6172 .chown_fn
= fruit_chown
,
6173 .unlink_fn
= fruit_unlink
,
6174 .rename_fn
= fruit_rename
,
6175 .rmdir_fn
= fruit_rmdir
,
6176 .open_fn
= fruit_open
,
6177 .pread_fn
= fruit_pread
,
6178 .pwrite_fn
= fruit_pwrite
,
6179 .pread_send_fn
= fruit_pread_send
,
6180 .pread_recv_fn
= fruit_pread_recv
,
6181 .pwrite_send_fn
= fruit_pwrite_send
,
6182 .pwrite_recv_fn
= fruit_pwrite_recv
,
6183 .stat_fn
= fruit_stat
,
6184 .lstat_fn
= fruit_lstat
,
6185 .fstat_fn
= fruit_fstat
,
6186 .streaminfo_fn
= fruit_streaminfo
,
6187 .ntimes_fn
= fruit_ntimes
,
6188 .ftruncate_fn
= fruit_ftruncate
,
6189 .fallocate_fn
= fruit_fallocate
,
6190 .create_file_fn
= fruit_create_file
,
6191 .readdir_attr_fn
= fruit_readdir_attr
,
6192 .offload_read_send_fn
= fruit_offload_read_send
,
6193 .offload_read_recv_fn
= fruit_offload_read_recv
,
6194 .offload_write_send_fn
= fruit_offload_write_send
,
6195 .offload_write_recv_fn
= fruit_offload_write_recv
,
6197 /* NT ACL operations */
6198 .fget_nt_acl_fn
= fruit_fget_nt_acl
,
6199 .fset_nt_acl_fn
= fruit_fset_nt_acl
,
6202 NTSTATUS
vfs_fruit_init(TALLOC_CTX
*);
6203 NTSTATUS
vfs_fruit_init(TALLOC_CTX
*ctx
)
6205 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fruit",
6207 if (!NT_STATUS_IS_OK(ret
)) {
6211 vfs_fruit_debug_level
= debug_add_class("fruit");
6212 if (vfs_fruit_debug_level
== -1) {
6213 vfs_fruit_debug_level
= DBGC_VFS
;
6214 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6217 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6218 "vfs_fruit_init","fruit",vfs_fruit_debug_level
));