2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
98 static int vfs_fruit_debug_level = DBGC_VFS;
100 static struct global_fruit_config {
101 bool nego_aapl; /* client negotiated AAPL */
103 } global_fruit_config;
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
122 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
124 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
125 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
126 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
127 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
129 struct fruit_config_data {
130 enum fruit_rsrc rsrc;
131 enum fruit_meta meta;
132 enum fruit_locking locking;
133 enum fruit_encoding encoding;
134 bool use_aapl; /* config from smb.conf */
136 bool readdir_attr_enabled;
137 bool unix_info_enabled;
138 bool copyfile_enabled;
139 bool veto_appledouble;
141 bool aapl_zero_file_id;
144 off_t time_machine_max_size;
147 * Additional options, all enabled by default,
148 * possibly useful for analyzing performance. The associated
149 * operations with each of them may be expensive, so having
150 * the chance to disable them individually gives a chance
151 * tweaking the setup for the particular usecase.
153 bool readdir_attr_rsize;
154 bool readdir_attr_finder_info;
155 bool readdir_attr_max_access;
158 static const struct enum_list fruit_rsrc[] = {
159 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
160 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
161 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
165 static const struct enum_list fruit_meta[] = {
166 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
167 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
171 static const struct enum_list fruit_locking[] = {
172 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
173 {FRUIT_LOCKING_NONE, "none"},
177 static const struct enum_list fruit_encoding[] = {
178 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
179 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
183 static const char *fruit_catia_maps =
184 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
185 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
186 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
187 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
188 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
189 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
190 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
191 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
192 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
193 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
196 /*****************************************************************************
197 * Defines, functions and data structures that deal with AppleDouble
198 *****************************************************************************/
201 * There are two AppleDouble blobs we deal with:
203 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
204 * metadata in an xattr
206 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
209 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
212 #define AD_VERSION2 0x00020000
213 #define AD_VERSION AD_VERSION2
216 * AppleDouble entry IDs.
218 #define ADEID_DFORK 1
219 #define ADEID_RFORK 2
221 #define ADEID_COMMENT 4
222 #define ADEID_ICONBW 5
223 #define ADEID_ICONCOL 6
224 #define ADEID_FILEI 7
225 #define ADEID_FILEDATESI 8
226 #define ADEID_FINDERI 9
227 #define ADEID_MACFILEI 10
228 #define ADEID_PRODOSFILEI 11
229 #define ADEID_MSDOSFILEI 12
230 #define ADEID_SHORTNAME 13
231 #define ADEID_AFPFILEI 14
234 /* Private Netatalk entries */
235 #define ADEID_PRIVDEV 16
236 #define ADEID_PRIVINO 17
237 #define ADEID_PRIVSYN 18
238 #define ADEID_PRIVID 19
239 #define ADEID_MAX (ADEID_PRIVID + 1)
242 * These are the real ids for the private entries,
243 * as stored in the adouble file
245 #define AD_DEV 0x80444556
246 #define AD_INO 0x80494E4F
247 #define AD_SYN 0x8053594E
248 #define AD_ID 0x8053567E
250 /* Number of actually used entries */
251 #define ADEID_NUM_XATTR 8
252 #define ADEID_NUM_DOT_UND 2
253 #define ADEID_NUM_RSRC_XATTR 1
255 /* AppleDouble magic */
256 #define AD_APPLESINGLE_MAGIC 0x00051600
257 #define AD_APPLEDOUBLE_MAGIC 0x00051607
258 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
260 /* Sizes of relevant entry bits */
261 #define ADEDLEN_MAGIC 4
262 #define ADEDLEN_VERSION 4
263 #define ADEDLEN_FILLER 16
264 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
265 #define ADEDLEN_NENTRIES 2
266 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
267 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
268 #define AD_ENTRY_LEN_EID 4
269 #define AD_ENTRY_LEN_OFF 4
270 #define AD_ENTRY_LEN_LEN 4
271 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
274 #define ADEDLEN_NAME 255
275 #define ADEDLEN_COMMENT 200
276 #define ADEDLEN_FILEI 16
277 #define ADEDLEN_FINDERI 32
278 #define ADEDLEN_FILEDATESI 16
279 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
280 #define ADEDLEN_AFPFILEI 4
281 #define ADEDLEN_MACFILEI 4
282 #define ADEDLEN_PRODOSFILEI 8
283 #define ADEDLEN_MSDOSFILEI 2
284 #define ADEDLEN_DID 4
285 #define ADEDLEN_PRIVDEV 8
286 #define ADEDLEN_PRIVINO 8
287 #define ADEDLEN_PRIVSYN 8
288 #define ADEDLEN_PRIVID 4
291 #define ADEDOFF_MAGIC 0
292 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
293 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
294 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
296 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
297 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
298 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
299 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
300 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
302 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
303 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
304 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
305 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
307 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
308 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
309 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
311 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
312 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
313 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
314 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
315 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
316 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
318 #if AD_DATASZ_XATTR != 402
319 #error bad size for AD_DATASZ_XATTR
322 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
323 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
325 #if AD_DATASZ_DOT_UND != 82
326 #error bad size for AD_DATASZ_DOT_UND
330 * Sharemode locks fcntl() offsets
332 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
333 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
335 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
337 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
339 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
340 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
341 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
342 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
343 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
344 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
345 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
346 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
347 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
348 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
350 /* Time stuff we overload the bits a little */
351 #define AD_DATE_CREATE 0
352 #define AD_DATE_MODIFY 4
353 #define AD_DATE_BACKUP 8
354 #define AD_DATE_ACCESS 12
355 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
356 AD_DATE_BACKUP | AD_DATE_ACCESS)
357 #define AD_DATE_UNIX (1 << 10)
358 #define AD_DATE_START 0x80000000
359 #define AD_DATE_DELTA 946684800
360 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
361 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
363 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
364 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
365 #define AD_XATTR_HDR_SIZE 36
366 #define AD_XATTR_MAX_HDR_SIZE 65536
368 /* Accessor macros */
369 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
370 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
371 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
372 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
375 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
376 * representation as well as the on-disk format.
378 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
379 * the length of the FinderInfo entry is larger then 32 bytes. It is then
380 * preceeded with 2 bytes padding.
382 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
385 struct ad_xattr_header {
386 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
387 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
388 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
389 uint32_t adx_data_start; /* file offset to attribute data area */
390 uint32_t adx_data_length; /* length of attribute data area */
391 uint32_t adx_reserved[3];
393 uint16_t adx_num_attrs;
396 /* On-disk entries are aligned on 4 byte boundaries */
397 struct ad_xattr_entry {
398 uint32_t adx_offset; /* file offset to data */
399 uint32_t adx_length; /* size of attribute data */
401 uint8_t adx_namelen; /* included the NULL terminator */
402 char *adx_name; /* NULL-terminated UTF-8 name */
411 vfs_handle_struct *ad_handle;
414 adouble_type_t ad_type;
417 struct ad_entry ad_eid[ADEID_MAX];
419 struct ad_xattr_header adx_header;
420 struct ad_xattr_entry *adx_entries;
423 struct ad_entry_order {
424 uint32_t id, offset, len;
427 /* Netatalk AppleDouble metadata xattr */
429 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
430 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
431 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
432 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
433 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
434 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
435 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
436 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
437 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
441 /* AppleDouble resource fork file (the ones prefixed by "._") */
443 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
444 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
445 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
450 * Fake AppleDouble entry oder for resource fork xattr. The xattr
451 * isn't an AppleDouble file, it simply contains the resource data,
452 * but in order to be able to use some API calls like ad_getentryoff()
453 * we build a fake/helper struct adouble with this entry order struct.
456 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
461 /* Conversion from enumerated id to on-disk AppleDouble id */
462 #define AD_EID_DISK(a) (set_eid[a])
463 static const uint32_t set_eid[] = {
464 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
465 AD_DEV, AD_INO, AD_SYN, AD_ID
469 /* tcon config handle */
470 struct fruit_config_data *config;
472 /* Denote stream type, meta or rsrc */
477 * Forward declarations
479 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
480 adouble_type_t type);
481 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
482 static int ad_fset(struct adouble *ad, files_struct *fsp);
483 static int adouble_path(TALLOC_CTX *ctx,
484 const struct smb_filename *smb_fname__in,
485 struct smb_filename **ppsmb_fname_out);
486 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
487 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
488 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
492 * Return a pointer to an AppleDouble entry
494 * Returns NULL if the entry is not present
496 static char *ad_get_entry(const struct adouble *ad, int eid)
498 off_t off = ad_getentryoff(ad, eid);
499 size_t len = ad_getentrylen(ad, eid);
501 if (off == 0 || len == 0) {
505 return ad->ad_data + off;
511 static int ad_getdate(const struct adouble *ad,
512 unsigned int dateoff,
515 bool xlate = (dateoff & AD_DATE_UNIX);
518 dateoff &= AD_DATE_MASK;
519 p = ad_get_entry(ad, ADEID_FILEDATESI);
524 if (dateoff > AD_DATE_ACCESS) {
528 memcpy(date, p + dateoff, sizeof(uint32_t));
531 *date = AD_DATE_TO_UNIX(*date);
539 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
541 bool xlate = (dateoff & AD_DATE_UNIX);
544 p = ad_get_entry(ad, ADEID_FILEDATESI);
549 dateoff &= AD_DATE_MASK;
551 date = AD_DATE_FROM_UNIX(date);
554 if (dateoff > AD_DATE_ACCESS) {
558 memcpy(p + dateoff, &date, sizeof(date));
565 * Map on-disk AppleDouble id to enumerated id
567 static uint32_t get_eid(uint32_t eid)
575 return ADEID_PRIVDEV;
577 return ADEID_PRIVINO;
579 return ADEID_PRIVSYN;
590 * Pack AppleDouble structure into data buffer
592 static bool ad_pack(struct adouble *ad)
599 bufsize = talloc_get_size(ad->ad_data);
600 if (bufsize < AD_DATASZ_DOT_UND) {
601 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
605 if (offset + ADEDLEN_MAGIC < offset ||
606 offset + ADEDLEN_MAGIC >= bufsize) {
609 RSIVAL(ad->ad_data, offset, ad->ad_magic);
610 offset += ADEDLEN_MAGIC;
612 if (offset + ADEDLEN_VERSION < offset ||
613 offset + ADEDLEN_VERSION >= bufsize) {
616 RSIVAL(ad->ad_data, offset, ad->ad_version);
617 offset += ADEDLEN_VERSION;
619 if (offset + ADEDLEN_FILLER < offset ||
620 offset + ADEDLEN_FILLER >= bufsize) {
623 if (ad->ad_type == ADOUBLE_RSRC) {
624 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
626 offset += ADEDLEN_FILLER;
628 if (offset + ADEDLEN_NENTRIES < offset ||
629 offset + ADEDLEN_NENTRIES >= bufsize) {
632 offset += ADEDLEN_NENTRIES;
634 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
635 if (ad->ad_eid[eid].ade_off == 0) {
637 * ade_off is also used as indicator whether a
638 * specific entry is used or not
643 if (offset + AD_ENTRY_LEN_EID < offset ||
644 offset + AD_ENTRY_LEN_EID >= bufsize) {
647 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
648 offset += AD_ENTRY_LEN_EID;
650 if (offset + AD_ENTRY_LEN_OFF < offset ||
651 offset + AD_ENTRY_LEN_OFF >= bufsize) {
654 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
655 offset += AD_ENTRY_LEN_OFF;
657 if (offset + AD_ENTRY_LEN_LEN < offset ||
658 offset + AD_ENTRY_LEN_LEN >= bufsize) {
661 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
662 offset += AD_ENTRY_LEN_LEN;
667 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
670 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
675 static bool ad_unpack_xattrs(struct adouble *ad)
677 struct ad_xattr_header *h = &ad->adx_header;
678 const char *p = ad->ad_data;
682 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
686 /* 2 bytes padding */
687 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
689 h->adx_magic = RIVAL(p, hoff + 0);
690 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
691 h->adx_total_size = RIVAL(p, hoff + 8);
692 h->adx_data_start = RIVAL(p, hoff + 12);
693 h->adx_data_length = RIVAL(p, hoff + 16);
694 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
695 h->adx_num_attrs = RSVAL(p, hoff + 34);
697 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
698 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
702 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
703 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
706 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
707 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
711 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
712 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
716 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
717 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
720 if ((h->adx_data_start + h->adx_data_length) >
721 ad->adx_header.adx_total_size)
723 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
727 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
728 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
732 if (h->adx_num_attrs == 0) {
736 ad->adx_entries = talloc_zero_array(
737 ad, struct ad_xattr_entry, h->adx_num_attrs);
738 if (ad->adx_entries == NULL) {
742 hoff += AD_XATTR_HDR_SIZE;
744 for (i = 0; i < h->adx_num_attrs; i++) {
745 struct ad_xattr_entry *e = &ad->adx_entries[i];
747 hoff = (hoff + 3) & ~3;
749 e->adx_offset = RIVAL(p, hoff + 0);
750 e->adx_length = RIVAL(p, hoff + 4);
751 e->adx_flags = RSVAL(p, hoff + 8);
752 e->adx_namelen = *(p + hoff + 10);
754 if (e->adx_offset >= ad->adx_header.adx_total_size) {
755 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
760 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
761 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
766 if ((e->adx_offset + e->adx_length) >
767 ad->adx_header.adx_total_size)
769 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
774 if (e->adx_namelen == 0) {
775 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
779 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
780 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
784 if ((hoff + 11 + e->adx_namelen) >
785 ad->adx_header.adx_data_start)
787 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
792 e->adx_name = talloc_strndup(ad->adx_entries,
795 if (e->adx_name == NULL) {
799 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
800 e->adx_name, e->adx_offset, e->adx_length);
801 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
804 hoff += 11 + e->adx_namelen;
811 * Unpack an AppleDouble blob into a struct adoble
813 static bool ad_unpack(struct adouble *ad, const size_t nentries,
816 size_t bufsize = talloc_get_size(ad->ad_data);
818 uint32_t eid, len, off;
822 * The size of the buffer ad->ad_data is checked when read, so
823 * we wouldn't have to check our own offsets, a few extra
824 * checks won't hurt though. We have to check the offsets we
825 * read from the buffer anyway.
828 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
829 DEBUG(1, ("bad size\n"));
833 ad->ad_magic = RIVAL(ad->ad_data, 0);
834 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
835 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
836 DEBUG(1, ("wrong magic or version\n"));
840 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
841 if (adentries != nentries) {
842 DEBUG(1, ("invalid number of entries: %zu\n",
847 /* now, read in the entry bits */
848 for (i = 0; i < adentries; i++) {
849 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
851 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
852 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
854 if (!eid || eid >= ADEID_MAX) {
855 DEBUG(1, ("bogus eid %d\n", eid));
860 * All entries other than the resource fork are
861 * expected to be read into the ad_data buffer, so
862 * ensure the specified offset is within that bound
864 if ((off > bufsize) && (eid != ADEID_RFORK)) {
865 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
871 * All entries besides FinderInfo and resource fork
872 * must fit into the buffer. FinderInfo is special as
873 * it may be larger then the default 32 bytes (if it
874 * contains marshalled xattrs), but we will fixup that
875 * in ad_convert(). And the resource fork is never
876 * accessed directly by the ad_data buf (also see
877 * comment above) anyway.
879 if ((eid != ADEID_RFORK) &&
880 (eid != ADEID_FINDERI) &&
881 ((off + len) > bufsize)) {
882 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
888 * That would be obviously broken
890 if (off > filesize) {
891 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
897 * Check for any entry that has its end beyond the
900 if (off + len < off) {
901 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
902 ", len: %" PRIu32 "\n",
907 if (off + len > filesize) {
909 * If this is the resource fork entry, we fix
910 * up the length, for any other entry we bail
913 if (eid != ADEID_RFORK) {
914 DEBUG(1, ("bogus eid %d: off: %" PRIu32
915 ", len: %" PRIu32 "\n",
921 * Fixup the resource fork entry by limiting
922 * the size to entryoffset - filesize.
924 len = filesize - off;
925 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
926 ", len: %" PRIu32 "\n", off, len));
929 ad->ad_eid[eid].ade_off = off;
930 ad->ad_eid[eid].ade_len = len;
933 ok = ad_unpack_xattrs(ad);
941 static bool ad_convert_xattr(struct adouble *ad,
942 const struct smb_filename *smb_fname,
945 static struct char_mappings **string_replace_cmaps = NULL;
950 if (ad->adx_header.adx_num_attrs == 0) {
954 if (string_replace_cmaps == NULL) {
955 const char **mappings = NULL;
957 mappings = str_list_make_v3_const(
958 talloc_tos(), fruit_catia_maps, NULL);
959 if (mappings == NULL) {
962 string_replace_cmaps = string_replace_init_map(mappings);
963 TALLOC_FREE(mappings);
966 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
967 struct ad_xattr_entry *e = &ad->adx_entries[i];
968 char *mapped_name = NULL;
970 struct smb_filename *stream_name = NULL;
971 files_struct *fsp = NULL;
974 status = string_replace_allocate(ad->ad_handle->conn,
976 string_replace_cmaps,
979 vfs_translate_to_windows);
980 if (!NT_STATUS_IS_OK(status) &&
981 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
983 DBG_ERR("string_replace_allocate failed\n");
988 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
990 if (mapped_name == NULL) {
994 stream_name = synthetic_smb_fname(talloc_tos(),
995 smb_fname->base_name,
999 TALLOC_FREE(mapped_name);
1000 if (stream_name == NULL) {
1001 DBG_ERR("synthetic_smb_fname failed\n");
1005 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1007 status = SMB_VFS_CREATE_FILE(
1008 ad->ad_handle->conn, /* conn */
1010 0, /* root_dir_fid */
1011 stream_name, /* fname */
1012 FILE_GENERIC_WRITE, /* access_mask */
1013 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1014 FILE_OPEN_IF, /* create_disposition */
1015 0, /* create_options */
1016 0, /* file_attributes */
1017 INTERNAL_OPEN_ONLY, /* oplock_request */
1019 0, /* allocation_size */
1020 0, /* private_flags */
1025 NULL, NULL); /* create context */
1026 TALLOC_FREE(stream_name);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1032 nwritten = SMB_VFS_PWRITE(fsp,
1033 map + e->adx_offset,
1036 if (nwritten == -1) {
1037 DBG_ERR("SMB_VFS_PWRITE failed\n");
1038 saved_errno = errno;
1039 close_file(NULL, fsp, ERROR_CLOSE);
1040 errno = saved_errno;
1044 status = close_file(NULL, fsp, NORMAL_CLOSE);
1045 if (!NT_STATUS_IS_OK(status)) {
1055 * Convert from Apple's ._ file to Netatalk
1057 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1058 * bytes containing packed xattrs. Netatalk can't deal with that, so
1059 * we simply discard the packed xattrs.
1061 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1064 static int ad_convert(struct adouble *ad,
1065 const struct smb_filename *smb_fname,
1069 char *map = MAP_FAILED;
1073 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1074 ad_getentrylen(ad, ADEID_RFORK);
1076 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1077 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1078 if (map == MAP_FAILED) {
1079 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1084 ok = ad_convert_xattr(ad, smb_fname, map);
1086 munmap(map, origlen);
1090 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1091 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1092 map + ad_getentryoff(ad, ADEID_RFORK),
1093 ad_getentrylen(ad, ADEID_RFORK));
1096 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1097 ad_setentryoff(ad, ADEID_RFORK,
1098 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1101 * FIXME: direct ftruncate(), but we don't have a fsp for the
1104 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
1105 + ad_getentrylen(ad, ADEID_RFORK));
1108 if (map != MAP_FAILED) {
1109 munmap(map, origlen);
1115 * Read and parse Netatalk AppleDouble metadata xattr
1117 static ssize_t ad_read_meta(struct adouble *ad,
1118 const struct smb_filename *smb_fname)
1124 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1126 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1127 AFPINFO_EA_NETATALK, ad->ad_data,
1133 if (errno == ENOATTR) {
1139 DEBUG(2, ("error reading meta xattr: %s\n",
1145 if (ealen != AD_DATASZ_XATTR) {
1146 DEBUG(2, ("bad size %zd\n", ealen));
1152 /* Now parse entries */
1153 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1155 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1161 if (!ad_getentryoff(ad, ADEID_FINDERI)
1162 || !ad_getentryoff(ad, ADEID_COMMENT)
1163 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1164 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1165 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1166 || !ad_getentryoff(ad, ADEID_PRIVINO)
1167 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1168 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1169 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1176 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1177 smb_fname->base_name, rc));
1181 if (errno == EINVAL) {
1183 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1191 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1195 #ifdef HAVE_ATTROPEN
1196 /* FIXME: direct Solaris xattr syscall */
1197 return attropen(smb_fname->base_name,
1198 AFPRESOURCE_EA_NETATALK, flags, mode);
1205 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1211 struct smb_filename *adp_smb_fname = NULL;
1213 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1218 fd = open(adp_smb_fname->base_name, flags, mode);
1219 TALLOC_FREE(adp_smb_fname);
1224 static int ad_open_rsrc(vfs_handle_struct *handle,
1225 const struct smb_filename *smb_fname,
1229 struct fruit_config_data *config = NULL;
1232 SMB_VFS_HANDLE_GET_DATA(handle, config,
1233 struct fruit_config_data, return -1);
1235 if (config->rsrc == FRUIT_RSRC_XATTR) {
1236 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1238 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1245 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1246 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1247 * for file IO on the ._ file.
1249 static int ad_open(vfs_handle_struct *handle,
1252 const struct smb_filename *smb_fname,
1258 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1259 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1261 if (ad->ad_type == ADOUBLE_META) {
1265 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1266 ad->ad_fd = fsp->fh->fd;
1267 ad->ad_opened = false;
1271 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1275 ad->ad_opened = true;
1278 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1279 smb_fname->base_name,
1280 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1285 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1290 /* FIXME: direct sys_fstat(), don't have an fsp */
1291 ret = sys_fstat(ad->ad_fd, &st,
1292 lp_fake_directory_create_times(
1293 SNUM(ad->ad_handle->conn)));
1298 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1299 return st.st_ex_size;
1302 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1303 const struct smb_filename *smb_fname)
1305 SMB_STRUCT_STAT sbuf;
1309 struct smb_filename *stream_name = NULL;
1310 files_struct *fsp = NULL;
1315 int saved_errno = 0;
1319 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1320 SNUM(ad->ad_handle->conn)));
1326 * AppleDouble file header content and size, two cases:
1328 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1329 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1331 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1333 size = sbuf.st_ex_size;
1334 if (size > talloc_array_length(ad->ad_data)) {
1335 if (size > AD_XATTR_MAX_HDR_SIZE) {
1336 size = AD_XATTR_MAX_HDR_SIZE;
1338 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1345 len = sys_pread(ad->ad_fd, ad->ad_data,
1346 talloc_array_length(ad->ad_data), 0);
1347 if (len != talloc_array_length(ad->ad_data)) {
1348 DBG_NOTICE("%s %s: bad size: %zd\n",
1349 smb_fname->base_name, strerror(errno), len);
1353 /* Now parse entries */
1354 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1356 DBG_ERR("invalid AppleDouble resource %s\n",
1357 smb_fname->base_name);
1362 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1363 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1364 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1365 DBG_ERR("invalid AppleDouble resource %s\n",
1366 smb_fname->base_name);
1371 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1376 * Try to fixup AppleDouble files created by OS X with xattrs
1377 * appended to the ADEID_FINDERI entry. We simply remove the
1378 * xattrs blob, this means any fancy xattr that was stored
1382 ret = ad_convert(ad, smb_fname, ad->ad_fd);
1384 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1390 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1394 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1395 if (len != AD_DATASZ_DOT_UND) {
1396 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1400 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1405 ai = afpinfo_new(talloc_tos());
1410 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1412 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1413 if (aiblob.data == NULL) {
1418 size = afpinfo_pack(ai, (char *)aiblob.data);
1420 if (size != AFP_INFO_SIZE) {
1424 stream_name = synthetic_smb_fname(talloc_tos(),
1425 smb_fname->base_name,
1429 if (stream_name == NULL) {
1430 data_blob_free(&aiblob);
1431 DBG_ERR("synthetic_smb_fname failed\n");
1435 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1437 status = SMB_VFS_CREATE_FILE(
1438 ad->ad_handle->conn, /* conn */
1440 0, /* root_dir_fid */
1441 stream_name, /* fname */
1442 FILE_GENERIC_WRITE, /* access_mask */
1443 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1444 FILE_OPEN_IF, /* create_disposition */
1445 0, /* create_options */
1446 0, /* file_attributes */
1447 INTERNAL_OPEN_ONLY, /* oplock_request */
1449 0, /* allocation_size */
1450 0, /* private_flags */
1455 NULL, NULL); /* create context */
1456 TALLOC_FREE(stream_name);
1457 if (!NT_STATUS_IS_OK(status)) {
1458 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1462 nwritten = SMB_VFS_PWRITE(fsp,
1466 if (nwritten == -1) {
1467 DBG_ERR("SMB_VFS_PWRITE failed\n");
1468 saved_errno = errno;
1469 close_file(NULL, fsp, ERROR_CLOSE);
1470 errno = saved_errno;
1474 status = close_file(NULL, fsp, NORMAL_CLOSE);
1475 if (!NT_STATUS_IS_OK(status)) {
1484 * Read and parse resource fork, either ._ AppleDouble file or xattr
1486 static ssize_t ad_read_rsrc(struct adouble *ad,
1487 const struct smb_filename *smb_fname)
1489 struct fruit_config_data *config = NULL;
1492 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1493 struct fruit_config_data, return -1);
1495 if (config->rsrc == FRUIT_RSRC_XATTR) {
1496 len = ad_read_rsrc_xattr(ad);
1498 len = ad_read_rsrc_adouble(ad, smb_fname);
1505 * Read and unpack an AppleDouble metadata xattr or resource
1507 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1509 switch (ad->ad_type) {
1511 return ad_read_meta(ad, smb_fname);
1513 return ad_read_rsrc(ad, smb_fname);
1519 static int adouble_destructor(struct adouble *ad)
1521 if ((ad->ad_fd != -1) && ad->ad_opened) {
1529 * Allocate a struct adouble without initialiing it
1531 * The struct is either hang of the fsp extension context or if fsp is
1534 * @param[in] ctx talloc context
1535 * @param[in] handle vfs handle
1536 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1538 * @return adouble handle
1540 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1541 adouble_type_t type)
1546 struct fruit_config_data *config;
1548 SMB_VFS_HANDLE_GET_DATA(handle, config,
1549 struct fruit_config_data, return NULL);
1553 adsize = AD_DATASZ_XATTR;
1556 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1557 adsize = AD_DATASZ_DOT_UND;
1564 ad = talloc_zero(ctx, struct adouble);
1571 ad->ad_data = talloc_zero_array(ad, char, adsize);
1572 if (ad->ad_data == NULL) {
1578 ad->ad_handle = handle;
1580 ad->ad_magic = AD_MAGIC;
1581 ad->ad_version = AD_VERSION;
1584 talloc_set_destructor(ad, adouble_destructor);
1594 * Allocate and initialize a new struct adouble
1596 * @param[in] ctx talloc context
1597 * @param[in] handle vfs handle
1598 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1600 * @return adouble handle, initialized
1602 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1603 adouble_type_t type)
1606 const struct ad_entry_order *eid;
1607 struct adouble *ad = NULL;
1608 struct fruit_config_data *config;
1609 time_t t = time(NULL);
1611 SMB_VFS_HANDLE_GET_DATA(handle, config,
1612 struct fruit_config_data, return NULL);
1616 eid = entry_order_meta_xattr;
1619 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1620 eid = entry_order_dot_und;
1622 eid = entry_order_rsrc_xattr;
1629 ad = ad_alloc(ctx, handle, type);
1635 ad->ad_eid[eid->id].ade_off = eid->offset;
1636 ad->ad_eid[eid->id].ade_len = eid->len;
1640 /* put something sane in the date fields */
1641 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1642 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1643 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1644 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1652 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1653 vfs_handle_struct *handle,
1655 const struct smb_filename *smb_fname,
1656 adouble_type_t type)
1660 struct adouble *ad = NULL;
1664 smb_fname = fsp->base_fsp->fsp_name;
1667 DEBUG(10, ("ad_get(%s) called for %s\n",
1668 type == ADOUBLE_META ? "meta" : "rsrc",
1669 smb_fname->base_name));
1671 ad = ad_alloc(ctx, handle, type);
1677 /* Try rw first so we can use the fd in ad_convert() */
1680 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1681 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1683 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1686 DBG_DEBUG("ad_open [%s] error [%s]\n",
1687 smb_fname->base_name, strerror(errno));
1692 len = ad_read(ad, smb_fname);
1694 DEBUG(10, ("error reading AppleDouble for %s\n",
1695 smb_fname->base_name));
1701 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1702 type == ADOUBLE_META ? "meta" : "rsrc",
1703 smb_fname->base_name, rc));
1712 * Return AppleDouble data for a file
1714 * @param[in] ctx talloc context
1715 * @param[in] handle vfs handle
1716 * @param[in] smb_fname pathname to file or directory
1717 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1719 * @return talloced struct adouble or NULL on error
1721 static struct adouble *ad_get(TALLOC_CTX *ctx,
1722 vfs_handle_struct *handle,
1723 const struct smb_filename *smb_fname,
1724 adouble_type_t type)
1726 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1730 * Return AppleDouble data for a file
1732 * @param[in] ctx talloc context
1733 * @param[in] handle vfs handle
1734 * @param[in] fsp fsp to use for IO
1735 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1737 * @return talloced struct adouble or NULL on error
1739 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1740 files_struct *fsp, adouble_type_t type)
1742 return ad_get_internal(ctx, handle, fsp, NULL, type);
1746 * Set AppleDouble metadata on a file or directory
1748 * @param[in] ad adouble handle
1750 * @param[in] smb_fname pathname to file or directory
1752 * @return status code, 0 means success
1754 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1759 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1761 if (ad->ad_type != ADOUBLE_META) {
1762 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1763 smb_fname->base_name);
1772 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1774 AFPINFO_EA_NETATALK,
1776 AD_DATASZ_XATTR, 0);
1778 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1784 * Set AppleDouble metadata on a file or directory
1786 * @param[in] ad adouble handle
1787 * @param[in] fsp file handle
1789 * @return status code, 0 means success
1791 static int ad_fset(struct adouble *ad, files_struct *fsp)
1797 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1800 || (fsp->fh == NULL)
1801 || (fsp->fh->fd == -1))
1803 smb_panic("bad fsp");
1811 switch (ad->ad_type) {
1813 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1815 AFPINFO_EA_NETATALK,
1817 AD_DATASZ_XATTR, 0);
1821 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1826 if (len != AD_DATASZ_DOT_UND) {
1827 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1837 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1842 /*****************************************************************************
1844 *****************************************************************************/
1846 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1848 if (strncasecmp_m(smb_fname->stream_name,
1849 AFPINFO_STREAM_NAME,
1850 strlen(AFPINFO_STREAM_NAME)) == 0) {
1856 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1858 if (strncasecmp_m(smb_fname->stream_name,
1859 AFPRESOURCE_STREAM_NAME,
1860 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1867 * Test whether stream is an Apple stream, not used atm
1870 static bool is_apple_stream(const struct smb_filename *smb_fname)
1872 if (is_afpinfo_stream(smb_fname)) {
1875 if (is_afpresource_stream(smb_fname)) {
1883 * Initialize config struct from our smb.conf config parameters
1885 static int init_fruit_config(vfs_handle_struct *handle)
1887 struct fruit_config_data *config;
1889 const char *tm_size_str = NULL;
1891 config = talloc_zero(handle->conn, struct fruit_config_data);
1893 DEBUG(1, ("talloc_zero() failed\n"));
1899 * Versions up to Samba 4.5.x had a spelling bug in the
1900 * fruit:resource option calling lp_parm_enum with
1901 * "res*s*ource" (ie two s).
1903 * In Samba 4.6 we accept both the wrong and the correct
1904 * spelling, in Samba 4.7 the bad spelling will be removed.
1906 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1907 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1908 if (enumval == -1) {
1909 DEBUG(1, ("value for %s: resource type unknown\n",
1910 FRUIT_PARAM_TYPE_NAME));
1913 config->rsrc = (enum fruit_rsrc)enumval;
1915 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1916 "resource", fruit_rsrc, enumval);
1917 if (enumval == -1) {
1918 DEBUG(1, ("value for %s: resource type unknown\n",
1919 FRUIT_PARAM_TYPE_NAME));
1922 config->rsrc = (enum fruit_rsrc)enumval;
1924 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1925 "metadata", fruit_meta, FRUIT_META_NETATALK);
1926 if (enumval == -1) {
1927 DEBUG(1, ("value for %s: metadata type unknown\n",
1928 FRUIT_PARAM_TYPE_NAME));
1931 config->meta = (enum fruit_meta)enumval;
1933 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1934 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1935 if (enumval == -1) {
1936 DEBUG(1, ("value for %s: locking type unknown\n",
1937 FRUIT_PARAM_TYPE_NAME));
1940 config->locking = (enum fruit_locking)enumval;
1942 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1943 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1944 if (enumval == -1) {
1945 DEBUG(1, ("value for %s: encoding type unknown\n",
1946 FRUIT_PARAM_TYPE_NAME));
1949 config->encoding = (enum fruit_encoding)enumval;
1951 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1952 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1953 FRUIT_PARAM_TYPE_NAME,
1958 config->use_aapl = lp_parm_bool(
1959 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1961 config->time_machine = lp_parm_bool(
1962 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
1964 config->unix_info_enabled = lp_parm_bool(
1965 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1967 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1970 config->posix_rename = lp_parm_bool(
1971 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1973 config->aapl_zero_file_id =
1974 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1976 config->readdir_attr_rsize = lp_parm_bool(
1977 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1979 config->readdir_attr_finder_info = lp_parm_bool(
1980 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1982 config->readdir_attr_max_access = lp_parm_bool(
1983 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1985 config->model = lp_parm_const_string(
1986 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1988 tm_size_str = lp_parm_const_string(
1989 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1990 "time machine max size", NULL);
1991 if (tm_size_str != NULL) {
1992 config->time_machine_max_size = conv_str_size(tm_size_str);
1995 SMB_VFS_HANDLE_SET_DATA(handle, config,
1996 NULL, struct fruit_config_data,
2003 * Prepend "._" to a basename
2004 * Return a new struct smb_filename with stream_name == NULL.
2006 static int adouble_path(TALLOC_CTX *ctx,
2007 const struct smb_filename *smb_fname_in,
2008 struct smb_filename **pp_smb_fname_out)
2012 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2015 if (smb_fname == NULL) {
2019 /* We need streamname to be NULL */
2020 TALLOC_FREE(smb_fname->stream_name);
2022 /* And we're replacing base_name. */
2023 TALLOC_FREE(smb_fname->base_name);
2025 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2027 TALLOC_FREE(smb_fname);
2031 smb_fname->base_name = talloc_asprintf(smb_fname,
2032 "%s/._%s", parent, base);
2033 if (smb_fname->base_name == NULL) {
2034 TALLOC_FREE(smb_fname);
2038 *pp_smb_fname_out = smb_fname;
2044 * Allocate and initialize an AfpInfo struct
2046 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2048 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2052 ai->afpi_Signature = AFP_Signature;
2053 ai->afpi_Version = AFP_Version;
2054 ai->afpi_BackupTime = AD_DATE_START;
2059 * Pack an AfpInfo struct into a buffer
2061 * Buffer size must be at least AFP_INFO_SIZE
2062 * Returns size of packed buffer
2064 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2066 memset(buf, 0, AFP_INFO_SIZE);
2068 RSIVAL(buf, 0, ai->afpi_Signature);
2069 RSIVAL(buf, 4, ai->afpi_Version);
2070 RSIVAL(buf, 12, ai->afpi_BackupTime);
2071 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2073 return AFP_INFO_SIZE;
2077 * Unpack a buffer into a AfpInfo structure
2079 * Buffer size must be at least AFP_INFO_SIZE
2080 * Returns allocated AfpInfo struct
2082 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2084 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2089 ai->afpi_Signature = RIVAL(data, 0);
2090 ai->afpi_Version = RIVAL(data, 4);
2091 ai->afpi_BackupTime = RIVAL(data, 12);
2092 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2093 sizeof(ai->afpi_FinderInfo));
2095 if (ai->afpi_Signature != AFP_Signature
2096 || ai->afpi_Version != AFP_Version) {
2097 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2105 * Fake an inode number from the md5 hash of the (xattr) name
2107 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2110 unsigned char hash[16];
2114 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2115 SMB_ASSERT(upper_sname != NULL);
2118 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2119 sizeof(sbuf->st_ex_dev));
2120 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2121 sizeof(sbuf->st_ex_ino));
2122 MD5Update(&ctx, (unsigned char *)upper_sname,
2123 talloc_get_size(upper_sname)-1);
2124 MD5Final(hash, &ctx);
2126 TALLOC_FREE(upper_sname);
2128 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2129 memcpy(&result, hash, sizeof(result));
2131 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2132 sname, (unsigned long long)result));
2137 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2138 struct stream_struct **streams,
2139 const char *name, off_t size,
2142 struct stream_struct *tmp;
2144 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2150 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2151 if (tmp[*num_streams].name == NULL) {
2155 tmp[*num_streams].size = size;
2156 tmp[*num_streams].alloc_size = alloc_size;
2163 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2164 struct stream_struct **streams)
2166 struct stream_struct *tmp = *streams;
2169 if (*num_streams == 0) {
2173 for (i = 0; i < *num_streams; i++) {
2174 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2179 if (i == *num_streams) {
2183 if (tmp[i].size > 0) {
2187 TALLOC_FREE(tmp[i].name);
2188 if (*num_streams - 1 > i) {
2189 memmove(&tmp[i], &tmp[i+1],
2190 (*num_streams - i - 1) * sizeof(struct stream_struct));
2197 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2198 struct stream_struct **streams,
2201 struct stream_struct *tmp = *streams;
2204 if (*num_streams == 0) {
2208 for (i = 0; i < *num_streams; i++) {
2209 if (strequal_m(tmp[i].name, name)) {
2214 if (i == *num_streams) {
2218 TALLOC_FREE(tmp[i].name);
2219 if (*num_streams - 1 > i) {
2220 memmove(&tmp[i], &tmp[i+1],
2221 (*num_streams - i - 1) * sizeof(struct stream_struct));
2228 static bool ad_empty_finderinfo(const struct adouble *ad)
2231 char emptybuf[ADEDLEN_FINDERI] = {0};
2234 fi = ad_get_entry(ad, ADEID_FINDERI);
2236 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2240 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2244 static bool ai_empty_finderinfo(const AfpInfo *ai)
2247 char emptybuf[ADEDLEN_FINDERI] = {0};
2249 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2254 * Update btime with btime from Netatalk
2256 static void update_btime(vfs_handle_struct *handle,
2257 struct smb_filename *smb_fname)
2260 struct timespec creation_time = {0};
2262 struct fruit_config_data *config = NULL;
2264 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2267 switch (config->meta) {
2268 case FRUIT_META_STREAM:
2270 case FRUIT_META_NETATALK:
2274 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2278 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2282 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2288 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2289 update_stat_ex_create_time(&smb_fname->st, creation_time);
2295 * Map an access mask to a Netatalk single byte byte range lock
2297 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2298 uint32_t access_mask)
2302 switch (access_mask) {
2303 case FILE_READ_DATA:
2304 offset = AD_FILELOCK_OPEN_RD;
2307 case FILE_WRITE_DATA:
2308 case FILE_APPEND_DATA:
2309 offset = AD_FILELOCK_OPEN_WR;
2313 offset = AD_FILELOCK_OPEN_NONE;
2317 if (fork_type == APPLE_FORK_RSRC) {
2318 if (offset == AD_FILELOCK_OPEN_NONE) {
2319 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2329 * Map a deny mode to a Netatalk brl
2331 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2336 switch (deny_mode) {
2338 offset = AD_FILELOCK_DENY_RD;
2342 offset = AD_FILELOCK_DENY_WR;
2346 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2349 if (fork_type == APPLE_FORK_RSRC) {
2357 * Call fcntl() with an exclusive F_GETLK request in order to
2358 * determine if there's an exisiting shared lock
2360 * @return true if the requested lock was found or any error occurred
2361 * false if the lock was not found
2363 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2366 off_t offset = in_offset;
2371 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2372 if (result == false) {
2376 if (type != F_UNLCK) {
2383 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2385 uint32_t access_mask,
2388 NTSTATUS status = NT_STATUS_OK;
2389 struct byte_range_lock *br_lck = NULL;
2390 bool open_for_reading, open_for_writing, deny_read, deny_write;
2392 bool have_read = false;
2395 /* FIXME: hardcoded data fork, add resource fork */
2396 enum apple_fork fork_type = APPLE_FORK_DATA;
2398 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2400 access_mask & FILE_READ_DATA ? "READ" :"-",
2401 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2402 deny_mode & DENY_READ ? "DENY_READ" : "-",
2403 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2405 if (fsp->fh->fd == -1) {
2406 return NT_STATUS_OK;
2409 flags = fcntl(fsp->fh->fd, F_GETFL);
2411 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2412 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2413 return map_nt_error_from_unix(errno);
2416 if (flags & (O_RDONLY|O_RDWR)) {
2418 * Applying fcntl read locks requires an fd opened for
2419 * reading. This means we won't be applying locks for
2420 * files openend write-only, but what can we do...
2426 * Check read access and deny read mode
2428 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2430 open_for_reading = test_netatalk_lock(
2431 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2433 deny_read = test_netatalk_lock(
2434 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2436 DEBUG(10, ("read: %s, deny_write: %s\n",
2437 open_for_reading == true ? "yes" : "no",
2438 deny_read == true ? "yes" : "no"));
2440 if (((access_mask & FILE_READ_DATA) && deny_read)
2441 || ((deny_mode & DENY_READ) && open_for_reading)) {
2442 return NT_STATUS_SHARING_VIOLATION;
2446 if ((access_mask & FILE_READ_DATA) && have_read) {
2447 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
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);
2460 if ((deny_mode & DENY_READ) && have_read) {
2461 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2463 handle->conn->sconn->msg_ctx, fsp,
2464 fsp->op->global->open_persistent_id, 1, off,
2465 READ_LOCK, POSIX_LOCK, false,
2468 if (!NT_STATUS_IS_OK(status)) {
2471 TALLOC_FREE(br_lck);
2476 * Check write access and deny write mode
2478 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2480 open_for_writing = test_netatalk_lock(
2481 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2483 deny_write = test_netatalk_lock(
2484 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2486 DEBUG(10, ("write: %s, deny_write: %s\n",
2487 open_for_writing == true ? "yes" : "no",
2488 deny_write == true ? "yes" : "no"));
2490 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2491 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2492 return NT_STATUS_SHARING_VIOLATION;
2496 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2497 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
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);
2510 if ((deny_mode & DENY_WRITE) && have_read) {
2511 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2513 handle->conn->sconn->msg_ctx, fsp,
2514 fsp->op->global->open_persistent_id, 1, off,
2515 READ_LOCK, POSIX_LOCK, false,
2518 if (!NT_STATUS_IS_OK(status)) {
2521 TALLOC_FREE(br_lck);
2525 TALLOC_FREE(br_lck);
2530 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2531 struct smb_request *req,
2532 const struct smb2_create_blobs *in_context_blobs,
2533 struct smb2_create_blobs *out_context_blobs)
2535 struct fruit_config_data *config;
2537 struct smb2_create_blob *aapl = NULL;
2541 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2542 uint64_t req_bitmap, client_caps;
2543 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2547 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2548 return NT_STATUS_UNSUCCESSFUL);
2550 if (!config->use_aapl
2551 || in_context_blobs == NULL
2552 || out_context_blobs == NULL) {
2553 return NT_STATUS_OK;
2556 aapl = smb2_create_blob_find(in_context_blobs,
2557 SMB2_CREATE_TAG_AAPL);
2559 return NT_STATUS_OK;
2562 if (aapl->data.length != 24) {
2563 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2564 (uintmax_t)aapl->data.length));
2565 return NT_STATUS_INVALID_PARAMETER;
2568 cmd = IVAL(aapl->data.data, 0);
2569 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2570 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2571 return NT_STATUS_INVALID_PARAMETER;
2574 req_bitmap = BVAL(aapl->data.data, 8);
2575 client_caps = BVAL(aapl->data.data, 16);
2577 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2579 SBVAL(p, 8, req_bitmap);
2580 ok = data_blob_append(req, &blob, p, 16);
2582 return NT_STATUS_UNSUCCESSFUL;
2585 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2586 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2587 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2588 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2589 config->readdir_attr_enabled = true;
2592 if (config->use_copyfile) {
2593 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2594 config->copyfile_enabled = true;
2598 * The client doesn't set the flag, so we can't check
2599 * for it and just set it unconditionally
2601 if (config->unix_info_enabled) {
2602 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2605 SBVAL(p, 0, server_caps);
2606 ok = data_blob_append(req, &blob, p, 8);
2608 return NT_STATUS_UNSUCCESSFUL;
2612 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2613 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2621 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2628 if (config->time_machine) {
2629 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2634 ok = data_blob_append(req, &blob, p, 8);
2636 return NT_STATUS_UNSUCCESSFUL;
2640 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2641 ok = convert_string_talloc(req,
2642 CH_UNIX, CH_UTF16LE,
2643 config->model, strlen(config->model),
2646 return NT_STATUS_UNSUCCESSFUL;
2650 SIVAL(p + 4, 0, modellen);
2651 ok = data_blob_append(req, &blob, p, 8);
2654 return NT_STATUS_UNSUCCESSFUL;
2657 ok = data_blob_append(req, &blob, model, modellen);
2660 return NT_STATUS_UNSUCCESSFUL;
2664 status = smb2_create_blob_add(out_context_blobs,
2666 SMB2_CREATE_TAG_AAPL,
2668 if (NT_STATUS_IS_OK(status)) {
2669 global_fruit_config.nego_aapl = true;
2670 if (config->aapl_zero_file_id) {
2671 aapl_force_zero_file_id(handle->conn->sconn);
2678 static bool readdir_attr_meta_finderi_stream(
2679 struct vfs_handle_struct *handle,
2680 const struct smb_filename *smb_fname,
2683 struct smb_filename *stream_name = NULL;
2684 files_struct *fsp = NULL;
2689 uint8_t buf[AFP_INFO_SIZE];
2691 stream_name = synthetic_smb_fname(talloc_tos(),
2692 smb_fname->base_name,
2693 AFPINFO_STREAM_NAME,
2694 NULL, smb_fname->flags);
2695 if (stream_name == NULL) {
2699 ret = SMB_VFS_STAT(handle->conn, stream_name);
2704 status = SMB_VFS_CREATE_FILE(
2705 handle->conn, /* conn */
2707 0, /* root_dir_fid */
2708 stream_name, /* fname */
2709 FILE_READ_DATA, /* access_mask */
2710 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2712 FILE_OPEN, /* create_disposition*/
2713 0, /* create_options */
2714 0, /* file_attributes */
2715 INTERNAL_OPEN_ONLY, /* oplock_request */
2717 0, /* allocation_size */
2718 0, /* private_flags */
2723 NULL, NULL); /* create context */
2725 TALLOC_FREE(stream_name);
2727 if (!NT_STATUS_IS_OK(status)) {
2731 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2732 if (nread != AFP_INFO_SIZE) {
2733 DBG_ERR("short read [%s] [%zd/%d]\n",
2734 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2739 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2746 close_file(NULL, fsp, NORMAL_CLOSE);
2752 static bool readdir_attr_meta_finderi_netatalk(
2753 struct vfs_handle_struct *handle,
2754 const struct smb_filename *smb_fname,
2757 struct adouble *ad = NULL;
2760 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2765 p = ad_get_entry(ad, ADEID_FINDERI);
2767 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2772 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2777 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2778 const struct smb_filename *smb_fname,
2779 struct readdir_attr_data *attr_data)
2781 struct fruit_config_data *config = NULL;
2782 uint32_t date_added;
2786 SMB_VFS_HANDLE_GET_DATA(handle, config,
2787 struct fruit_config_data,
2790 switch (config->meta) {
2791 case FRUIT_META_NETATALK:
2792 ok = readdir_attr_meta_finderi_netatalk(
2793 handle, smb_fname, &ai);
2796 case FRUIT_META_STREAM:
2797 ok = readdir_attr_meta_finderi_stream(
2798 handle, smb_fname, &ai);
2802 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2807 /* Don't bother with errors, it's likely ENOENT */
2811 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2813 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2814 &ai.afpi_FinderInfo[0], 4);
2816 /* finder_creator */
2817 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2818 &ai.afpi_FinderInfo[4], 4);
2822 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2823 &ai.afpi_FinderInfo[8], 2);
2825 /* finder_ext_flags */
2826 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2827 &ai.afpi_FinderInfo[24], 2);
2830 date_added = convert_time_t_to_uint32_t(
2831 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2833 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2838 static uint64_t readdir_attr_rfork_size_adouble(
2839 struct vfs_handle_struct *handle,
2840 const struct smb_filename *smb_fname)
2842 struct adouble *ad = NULL;
2843 uint64_t rfork_size;
2845 ad = ad_get(talloc_tos(), handle, smb_fname,
2851 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2857 static uint64_t readdir_attr_rfork_size_stream(
2858 struct vfs_handle_struct *handle,
2859 const struct smb_filename *smb_fname)
2861 struct smb_filename *stream_name = NULL;
2863 uint64_t rfork_size;
2865 stream_name = synthetic_smb_fname(talloc_tos(),
2866 smb_fname->base_name,
2867 AFPRESOURCE_STREAM_NAME,
2869 if (stream_name == NULL) {
2873 ret = SMB_VFS_STAT(handle->conn, stream_name);
2875 TALLOC_FREE(stream_name);
2879 rfork_size = stream_name->st.st_ex_size;
2880 TALLOC_FREE(stream_name);
2885 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2886 const struct smb_filename *smb_fname)
2888 struct fruit_config_data *config = NULL;
2889 uint64_t rfork_size;
2891 SMB_VFS_HANDLE_GET_DATA(handle, config,
2892 struct fruit_config_data,
2895 switch (config->rsrc) {
2896 case FRUIT_RSRC_ADFILE:
2897 case FRUIT_RSRC_XATTR:
2898 rfork_size = readdir_attr_rfork_size_adouble(handle,
2902 case FRUIT_META_STREAM:
2903 rfork_size = readdir_attr_rfork_size_stream(handle,
2908 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2916 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2917 const struct smb_filename *smb_fname,
2918 struct readdir_attr_data *attr_data)
2920 NTSTATUS status = NT_STATUS_OK;
2921 struct fruit_config_data *config = NULL;
2924 SMB_VFS_HANDLE_GET_DATA(handle, config,
2925 struct fruit_config_data,
2926 return NT_STATUS_UNSUCCESSFUL);
2929 /* Ensure we return a default value in the creation_date field */
2930 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2933 * Resource fork length
2936 if (config->readdir_attr_rsize) {
2937 uint64_t rfork_size;
2939 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2940 attr_data->attr_data.aapl.rfork_size = rfork_size;
2947 if (config->readdir_attr_finder_info) {
2948 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2950 status = NT_STATUS_INTERNAL_ERROR;
2957 /* Search MS NFS style ACE with UNIX mode */
2958 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2960 struct security_descriptor *psd,
2965 struct fruit_config_data *config = NULL;
2967 NTSTATUS status = NT_STATUS_OK;
2968 bool remove_ok = false;
2972 SMB_VFS_HANDLE_GET_DATA(handle, config,
2973 struct fruit_config_data,
2974 return NT_STATUS_UNSUCCESSFUL);
2976 if (!global_fruit_config.nego_aapl) {
2977 return NT_STATUS_OK;
2979 if (psd->dacl == NULL || !config->unix_info_enabled) {
2980 return NT_STATUS_OK;
2983 for (i = 0; i < psd->dacl->num_aces; i++) {
2984 if (dom_sid_compare_domain(
2985 &global_sid_Unix_NFS_Mode,
2986 &psd->dacl->aces[i].trustee) == 0) {
2987 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2988 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2991 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2992 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2998 * Remove any incoming virtual ACE entries generated by
2999 * fruit_fget_nt_acl().
3002 /* MS NFS style mode */
3003 sid_compose(&sid, &global_sid_Unix_NFS_Mode,
3004 fsp->fsp_name->st.st_ex_mode);
3005 status = security_descriptor_dacl_del(psd, &sid);
3006 remove_ok = (NT_STATUS_IS_OK(status) ||
3007 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
3009 DBG_WARNING("failed to remove MS NFS_mode style ACE\n");
3013 /* MS NFS style uid */
3014 sid_compose(&sid, &global_sid_Unix_NFS_Users,
3015 fsp->fsp_name->st.st_ex_uid);
3016 status = security_descriptor_dacl_del(psd, &sid);
3017 remove_ok = (NT_STATUS_IS_OK(status) ||
3018 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
3020 DBG_WARNING("failed to remove MS NFS_users style ACE\n");
3024 /* MS NFS style gid */
3025 sid_compose(&sid, &global_sid_Unix_NFS_Groups,
3026 fsp->fsp_name->st.st_ex_gid);
3027 status = security_descriptor_dacl_del(psd, &sid);
3028 remove_ok = (NT_STATUS_IS_OK(status) ||
3029 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
3031 DBG_WARNING("failed to remove MS NFS_groups style ACE\n");
3035 return NT_STATUS_OK;
3038 /****************************************************************************
3040 ****************************************************************************/
3042 static int fruit_connect(vfs_handle_struct *handle,
3043 const char *service,
3047 char *list = NULL, *newlist = NULL;
3048 struct fruit_config_data *config;
3050 DEBUG(10, ("fruit_connect\n"));
3052 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3057 rc = init_fruit_config(handle);
3062 SMB_VFS_HANDLE_GET_DATA(handle, config,
3063 struct fruit_config_data, return -1);
3065 if (config->veto_appledouble) {
3066 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3069 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3070 newlist = talloc_asprintf(
3072 "%s/" ADOUBLE_NAME_PREFIX "*/",
3074 lp_do_parameter(SNUM(handle->conn),
3079 lp_do_parameter(SNUM(handle->conn),
3081 "/" ADOUBLE_NAME_PREFIX "*/");
3087 if (config->encoding == FRUIT_ENC_NATIVE) {
3088 lp_do_parameter(SNUM(handle->conn),
3093 if (config->time_machine) {
3094 DBG_NOTICE("Enabling durable handles for Time Machine "
3095 "support on [%s]\n", service);
3096 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3097 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3098 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3099 if (!lp_strict_sync(SNUM(handle->conn))) {
3100 DBG_WARNING("Time Machine without strict sync is not "
3103 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3109 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3110 struct smb_filename *smb_fname,
3116 char afpinfo_buf[AFP_INFO_SIZE];
3117 ssize_t len, written;
3121 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3126 if (!(flags & (O_CREAT | O_TRUNC))) {
3130 ai = afpinfo_new(talloc_tos());
3136 len = afpinfo_pack(ai, afpinfo_buf);
3137 if (len != AFP_INFO_SIZE) {
3142 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3143 fsp->fh->fd = hostfd;
3145 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3148 if (written != AFP_INFO_SIZE) {
3149 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3157 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3160 int saved_errno = errno;
3162 fsp->fh->fd = hostfd;
3163 SMB_VFS_NEXT_CLOSE(handle, fsp);
3166 errno = saved_errno;
3171 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3172 struct smb_filename *smb_fname,
3179 struct adouble *ad = NULL;
3182 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3185 * Return a valid fd, but ensure any attempt to use it returns an error
3186 * (EPIPE). All operations on the smb_fname or the fsp will use path
3196 if (flags & (O_CREAT | O_TRUNC)) {
3198 * The attribute does not exist or needs to be truncated,
3199 * create an AppleDouble EA
3201 ad = ad_init(fsp, handle, ADOUBLE_META);
3207 rc = ad_set(ad, fsp->fsp_name);
3217 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3219 int saved_errno = errno;
3224 errno = saved_errno;
3229 static int fruit_open_meta(vfs_handle_struct *handle,
3230 struct smb_filename *smb_fname,
3231 files_struct *fsp, int flags, mode_t mode)
3234 struct fruit_config_data *config = NULL;
3235 struct fio *fio = NULL;
3237 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3239 SMB_VFS_HANDLE_GET_DATA(handle, config,
3240 struct fruit_config_data, return -1);
3242 switch (config->meta) {
3243 case FRUIT_META_STREAM:
3244 fd = fruit_open_meta_stream(handle, smb_fname,
3248 case FRUIT_META_NETATALK:
3249 fd = fruit_open_meta_netatalk(handle, smb_fname,
3254 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3258 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3264 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3265 fio->type = ADOUBLE_META;
3266 fio->config = config;
3271 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3272 struct smb_filename *smb_fname,
3278 struct adouble *ad = NULL;
3279 struct smb_filename *smb_fname_base = NULL;
3280 struct fruit_config_data *config = NULL;
3283 SMB_VFS_HANDLE_GET_DATA(handle, config,
3284 struct fruit_config_data, return -1);
3286 if ((!(flags & O_CREAT)) &&
3287 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3289 /* sorry, but directories don't habe a resource fork */
3294 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3299 /* Sanitize flags */
3300 if (flags & O_WRONLY) {
3301 /* We always need read access for the metadata header too */
3306 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3313 if (flags & (O_CREAT | O_TRUNC)) {
3314 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3320 fsp->fh->fd = hostfd;
3322 rc = ad_fset(ad, fsp);
3333 TALLOC_FREE(smb_fname_base);
3335 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3337 int saved_errno = errno;
3340 * BUGBUGBUG -- we would need to call
3341 * fd_close_posix here, but we don't have a
3344 fsp->fh->fd = hostfd;
3348 errno = saved_errno;
3353 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3354 struct smb_filename *smb_fname,
3359 #ifdef HAVE_ATTROPEN
3362 fd = attropen(smb_fname->base_name,
3363 AFPRESOURCE_EA_NETATALK,
3378 static int fruit_open_rsrc(vfs_handle_struct *handle,
3379 struct smb_filename *smb_fname,
3380 files_struct *fsp, int flags, mode_t mode)
3383 struct fruit_config_data *config = NULL;
3384 struct fio *fio = NULL;
3386 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3388 SMB_VFS_HANDLE_GET_DATA(handle, config,
3389 struct fruit_config_data, return -1);
3391 if (((flags & O_ACCMODE) == O_RDONLY)
3392 && (flags & O_CREAT)
3393 && !VALID_STAT(fsp->fsp_name->st))
3396 * This means the stream doesn't exist. macOS SMB server fails
3397 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3398 * 12565 and the test for this combination in
3399 * test_rfork_create().
3405 switch (config->rsrc) {
3406 case FRUIT_RSRC_STREAM:
3407 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3410 case FRUIT_RSRC_ADFILE:
3411 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3415 case FRUIT_RSRC_XATTR:
3416 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3421 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3425 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3431 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3432 fio->type = ADOUBLE_RSRC;
3433 fio->config = config;
3438 static int fruit_open(vfs_handle_struct *handle,
3439 struct smb_filename *smb_fname,
3440 files_struct *fsp, int flags, mode_t mode)
3444 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3446 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3447 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3450 if (is_afpinfo_stream(smb_fname)) {
3451 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3452 } else if (is_afpresource_stream(smb_fname)) {
3453 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3455 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3458 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3463 static int fruit_rename(struct vfs_handle_struct *handle,
3464 const struct smb_filename *smb_fname_src,
3465 const struct smb_filename *smb_fname_dst)
3468 struct fruit_config_data *config = NULL;
3469 struct smb_filename *src_adp_smb_fname = NULL;
3470 struct smb_filename *dst_adp_smb_fname = NULL;
3472 SMB_VFS_HANDLE_GET_DATA(handle, config,
3473 struct fruit_config_data, return -1);
3475 if (!VALID_STAT(smb_fname_src->st)) {
3476 DBG_ERR("Need valid stat for [%s]\n",
3477 smb_fname_str_dbg(smb_fname_src));
3481 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3486 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3487 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3492 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3497 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3502 DBG_DEBUG("%s -> %s\n",
3503 smb_fname_str_dbg(src_adp_smb_fname),
3504 smb_fname_str_dbg(dst_adp_smb_fname));
3506 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3507 if (errno == ENOENT) {
3512 TALLOC_FREE(src_adp_smb_fname);
3513 TALLOC_FREE(dst_adp_smb_fname);
3517 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3518 const struct smb_filename *smb_fname)
3520 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3523 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3524 const struct smb_filename *smb_fname)
3526 return SMB_VFS_REMOVEXATTR(handle->conn,
3528 AFPINFO_EA_NETATALK);
3531 static int fruit_unlink_meta(vfs_handle_struct *handle,
3532 const struct smb_filename *smb_fname)
3534 struct fruit_config_data *config = NULL;
3537 SMB_VFS_HANDLE_GET_DATA(handle, config,
3538 struct fruit_config_data, return -1);
3540 switch (config->meta) {
3541 case FRUIT_META_STREAM:
3542 rc = fruit_unlink_meta_stream(handle, smb_fname);
3545 case FRUIT_META_NETATALK:
3546 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3550 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3557 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3558 const struct smb_filename *smb_fname,
3563 if (!force_unlink) {
3564 struct smb_filename *smb_fname_cp = NULL;
3567 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3568 if (smb_fname_cp == NULL) {
3573 * 0 byte resource fork streams are not listed by
3574 * vfs_streaminfo, as a result stream cleanup/deletion of file
3575 * deletion doesn't remove the resourcefork stream.
3578 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3580 TALLOC_FREE(smb_fname_cp);
3581 DBG_ERR("stat [%s] failed [%s]\n",
3582 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3586 size = smb_fname_cp->st.st_ex_size;
3587 TALLOC_FREE(smb_fname_cp);
3590 /* OS X ignores resource fork stream delete requests */
3595 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3596 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3603 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3604 const struct smb_filename *smb_fname,
3608 struct adouble *ad = NULL;
3609 struct smb_filename *adp_smb_fname = NULL;
3611 if (!force_unlink) {
3612 ad = ad_get(talloc_tos(), handle, smb_fname,
3621 * 0 byte resource fork streams are not listed by
3622 * vfs_streaminfo, as a result stream cleanup/deletion of file
3623 * deletion doesn't remove the resourcefork stream.
3626 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3627 /* OS X ignores resource fork stream delete requests */
3635 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3640 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3641 TALLOC_FREE(adp_smb_fname);
3642 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3649 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3650 const struct smb_filename *smb_fname,
3654 * OS X ignores resource fork stream delete requests, so nothing to do
3655 * here. Removing the file will remove the xattr anyway, so we don't
3656 * have to take care of removing 0 byte resource forks that could be
3662 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3663 const struct smb_filename *smb_fname,
3666 struct fruit_config_data *config = NULL;
3669 SMB_VFS_HANDLE_GET_DATA(handle, config,
3670 struct fruit_config_data, return -1);
3672 switch (config->rsrc) {
3673 case FRUIT_RSRC_STREAM:
3674 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3677 case FRUIT_RSRC_ADFILE:
3678 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3681 case FRUIT_RSRC_XATTR:
3682 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3686 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3693 static int fruit_unlink(vfs_handle_struct *handle,
3694 const struct smb_filename *smb_fname)
3697 struct fruit_config_data *config = NULL;
3698 struct smb_filename *rsrc_smb_fname = NULL;
3700 SMB_VFS_HANDLE_GET_DATA(handle, config,
3701 struct fruit_config_data, return -1);
3703 if (is_afpinfo_stream(smb_fname)) {
3704 return fruit_unlink_meta(handle, smb_fname);
3705 } else if (is_afpresource_stream(smb_fname)) {
3706 return fruit_unlink_rsrc(handle, smb_fname, false);
3707 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3708 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3712 * A request to delete the base file. Because 0 byte resource
3713 * fork streams are not listed by fruit_streaminfo,
3714 * delete_all_streams() can't remove 0 byte resource fork
3715 * streams, so we have to cleanup this here.
3717 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3718 smb_fname->base_name,
3719 AFPRESOURCE_STREAM_NAME,
3722 if (rsrc_smb_fname == NULL) {
3726 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3727 if ((rc != 0) && (errno != ENOENT)) {
3728 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3729 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3730 TALLOC_FREE(rsrc_smb_fname);
3733 TALLOC_FREE(rsrc_smb_fname);
3735 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3738 static int fruit_chmod(vfs_handle_struct *handle,
3739 const struct smb_filename *smb_fname,
3743 struct fruit_config_data *config = NULL;
3744 struct smb_filename *smb_fname_adp = NULL;
3746 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3751 SMB_VFS_HANDLE_GET_DATA(handle, config,
3752 struct fruit_config_data, return -1);
3754 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3758 if (!VALID_STAT(smb_fname->st)) {
3762 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3766 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3771 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3773 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3774 if (errno == ENOENT) {
3778 TALLOC_FREE(smb_fname_adp);
3782 static int fruit_chown(vfs_handle_struct *handle,
3783 const struct smb_filename *smb_fname,
3788 struct fruit_config_data *config = NULL;
3789 struct smb_filename *adp_smb_fname = NULL;
3791 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3796 SMB_VFS_HANDLE_GET_DATA(handle, config,
3797 struct fruit_config_data, return -1);
3799 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3803 if (!VALID_STAT(smb_fname->st)) {
3807 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3811 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3816 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3818 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3819 if (errno == ENOENT) {
3824 TALLOC_FREE(adp_smb_fname);
3828 static int fruit_rmdir(struct vfs_handle_struct *handle,
3829 const struct smb_filename *smb_fname)
3833 struct fruit_config_data *config;
3835 SMB_VFS_HANDLE_GET_DATA(handle, config,
3836 struct fruit_config_data, return -1);
3838 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3843 * Due to there is no way to change bDeleteVetoFiles variable
3844 * from this module, need to clean up ourselves
3847 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3852 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3854 struct adouble *ad = NULL;
3856 struct smb_filename *ad_smb_fname = NULL;
3859 match = strncmp(de->d_name,
3860 ADOUBLE_NAME_PREFIX,
3861 strlen(ADOUBLE_NAME_PREFIX));
3866 p = talloc_asprintf(talloc_tos(), "%s/%s",
3867 smb_fname->base_name, de->d_name);
3869 DBG_ERR("talloc_asprintf failed\n");
3873 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3877 if (ad_smb_fname == NULL) {
3878 DBG_ERR("synthetic_smb_fname failed\n");
3883 * Check whether it's a valid AppleDouble file, if
3884 * yes, delete it, ignore it otherwise.
3886 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3888 TALLOC_FREE(ad_smb_fname);
3894 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3896 DBG_ERR("Deleting [%s] failed\n",
3897 smb_fname_str_dbg(ad_smb_fname));
3899 TALLOC_FREE(ad_smb_fname);
3904 SMB_VFS_CLOSEDIR(handle->conn, dh);
3906 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3909 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3910 files_struct *fsp, void *data,
3911 size_t n, off_t offset)
3916 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3922 DBG_ERR("Removing [%s] after short read [%zd]\n",
3923 fsp_str_dbg(fsp), nread);
3925 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3927 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3935 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3936 files_struct *fsp, void *data,
3937 size_t n, off_t offset)
3940 struct adouble *ad = NULL;
3941 char afpinfo_buf[AFP_INFO_SIZE];
3945 ai = afpinfo_new(talloc_tos());
3950 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3956 p = ad_get_entry(ad, ADEID_FINDERI);
3958 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3963 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3965 nread = afpinfo_pack(ai, afpinfo_buf);
3966 if (nread != AFP_INFO_SIZE) {
3971 memcpy(data, afpinfo_buf, n);
3979 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3980 files_struct *fsp, void *data,
3981 size_t n, off_t offset)
3983 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3988 * OS X has a off-by-1 error in the offset calculation, so we're
3989 * bug compatible here. It won't hurt, as any relevant real
3990 * world read requests from the AFP_AfpInfo stream will be
3991 * offset=0 n=60. offset is ignored anyway, see below.
3993 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3997 /* Yes, macOS always reads from offset 0 */
3999 to_return = MIN(n, AFP_INFO_SIZE);
4001 switch (fio->config->meta) {
4002 case FRUIT_META_STREAM:
4003 nread = fruit_pread_meta_stream(handle, fsp, data,
4007 case FRUIT_META_NETATALK:
4008 nread = fruit_pread_meta_adouble(handle, fsp, data,
4013 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4020 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4021 files_struct *fsp, void *data,
4022 size_t n, off_t offset)
4024 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4027 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4028 files_struct *fsp, void *data,
4029 size_t n, off_t offset)
4031 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4034 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4035 files_struct *fsp, void *data,
4036 size_t n, off_t offset)
4038 struct adouble *ad = NULL;
4041 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4046 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4047 offset + ad_getentryoff(ad, ADEID_RFORK));
4053 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4054 files_struct *fsp, void *data,
4055 size_t n, off_t offset)
4057 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4060 switch (fio->config->rsrc) {
4061 case FRUIT_RSRC_STREAM:
4062 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4065 case FRUIT_RSRC_ADFILE:
4066 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4069 case FRUIT_RSRC_XATTR:
4070 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4074 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4081 static ssize_t fruit_pread(vfs_handle_struct *handle,
4082 files_struct *fsp, void *data,
4083 size_t n, off_t offset)
4085 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4088 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4089 fsp_str_dbg(fsp), (intmax_t)offset, n);
4092 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4095 if (fio->type == ADOUBLE_META) {
4096 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4098 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4101 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4105 static bool fruit_must_handle_aio_stream(struct fio *fio)
4111 if ((fio->type == ADOUBLE_META) &&
4112 (fio->config->meta == FRUIT_META_NETATALK))
4117 if ((fio->type == ADOUBLE_RSRC) &&
4118 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4126 struct fruit_pread_state {
4128 struct vfs_aio_state vfs_aio_state;
4131 static void fruit_pread_done(struct tevent_req *subreq);
4133 static struct tevent_req *fruit_pread_send(
4134 struct vfs_handle_struct *handle,
4135 TALLOC_CTX *mem_ctx,
4136 struct tevent_context *ev,
4137 struct files_struct *fsp,
4139 size_t n, off_t offset)
4141 struct tevent_req *req = NULL;
4142 struct tevent_req *subreq = NULL;
4143 struct fruit_pread_state *state = NULL;
4144 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4146 req = tevent_req_create(mem_ctx, &state,
4147 struct fruit_pread_state);
4152 if (fruit_must_handle_aio_stream(fio)) {
4153 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4154 if (state->nread != n) {
4155 if (state->nread != -1) {
4158 tevent_req_error(req, errno);
4159 return tevent_req_post(req, ev);
4161 tevent_req_done(req);
4162 return tevent_req_post(req, ev);
4165 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4167 if (tevent_req_nomem(req, subreq)) {
4168 return tevent_req_post(req, ev);
4170 tevent_req_set_callback(subreq, fruit_pread_done, req);
4174 static void fruit_pread_done(struct tevent_req *subreq)
4176 struct tevent_req *req = tevent_req_callback_data(
4177 subreq, struct tevent_req);
4178 struct fruit_pread_state *state = tevent_req_data(
4179 req, struct fruit_pread_state);
4181 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4182 TALLOC_FREE(subreq);
4184 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4187 tevent_req_done(req);
4190 static ssize_t fruit_pread_recv(struct tevent_req *req,
4191 struct vfs_aio_state *vfs_aio_state)
4193 struct fruit_pread_state *state = tevent_req_data(
4194 req, struct fruit_pread_state);
4196 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4200 *vfs_aio_state = state->vfs_aio_state;
4201 return state->nread;
4204 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4205 files_struct *fsp, const void *data,
4206 size_t n, off_t offset)
4212 ai = afpinfo_unpack(talloc_tos(), data);
4217 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4218 if (nwritten != n) {
4222 if (!ai_empty_finderinfo(ai)) {
4226 ok = set_delete_on_close(
4229 handle->conn->session_info->security_token,
4230 handle->conn->session_info->unix_token);
4232 DBG_ERR("set_delete_on_close on [%s] failed\n",
4240 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4241 files_struct *fsp, const void *data,
4242 size_t n, off_t offset)
4244 struct adouble *ad = NULL;
4250 ai = afpinfo_unpack(talloc_tos(), data);
4255 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4257 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4262 p = ad_get_entry(ad, ADEID_FINDERI);
4264 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4269 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4271 ret = ad_fset(ad, fsp);
4273 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4280 if (!ai_empty_finderinfo(ai)) {
4284 ok = set_delete_on_close(
4287 handle->conn->session_info->security_token,
4288 handle->conn->session_info->unix_token);
4290 DBG_ERR("set_delete_on_close on [%s] failed\n",
4298 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4299 files_struct *fsp, const void *data,
4300 size_t n, off_t offset)
4302 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4306 * Writing an all 0 blob to the metadata stream
4307 * results in the stream being removed on a macOS
4308 * server. This ensures we behave the same and it
4309 * verified by the "delete AFP_AfpInfo by writing all
4312 if (n != AFP_INFO_SIZE || offset != 0) {
4313 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4314 (intmax_t)offset, (intmax_t)n);
4318 switch (fio->config->meta) {
4319 case FRUIT_META_STREAM:
4320 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4324 case FRUIT_META_NETATALK:
4325 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4330 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4337 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4338 files_struct *fsp, const void *data,
4339 size_t n, off_t offset)
4341 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4344 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4345 files_struct *fsp, const void *data,
4346 size_t n, off_t offset)
4348 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4351 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4352 files_struct *fsp, const void *data,
4353 size_t n, off_t offset)
4355 struct adouble *ad = NULL;
4359 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4361 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4365 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4366 offset + ad_getentryoff(ad, ADEID_RFORK));
4367 if (nwritten != n) {
4368 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4369 fsp_str_dbg(fsp), nwritten, n);
4374 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4375 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4376 ret = ad_fset(ad, fsp);
4378 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4388 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4389 files_struct *fsp, const void *data,
4390 size_t n, off_t offset)
4392 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4395 switch (fio->config->rsrc) {
4396 case FRUIT_RSRC_STREAM:
4397 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4400 case FRUIT_RSRC_ADFILE:
4401 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4404 case FRUIT_RSRC_XATTR:
4405 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4409 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4416 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4417 files_struct *fsp, const void *data,
4418 size_t n, off_t offset)
4420 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4423 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4424 fsp_str_dbg(fsp), (intmax_t)offset, n);
4427 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4430 if (fio->type == ADOUBLE_META) {
4431 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4433 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4436 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4440 struct fruit_pwrite_state {
4442 struct vfs_aio_state vfs_aio_state;
4445 static void fruit_pwrite_done(struct tevent_req *subreq);
4447 static struct tevent_req *fruit_pwrite_send(
4448 struct vfs_handle_struct *handle,
4449 TALLOC_CTX *mem_ctx,
4450 struct tevent_context *ev,
4451 struct files_struct *fsp,
4453 size_t n, off_t offset)
4455 struct tevent_req *req = NULL;
4456 struct tevent_req *subreq = NULL;
4457 struct fruit_pwrite_state *state = NULL;
4458 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4460 req = tevent_req_create(mem_ctx, &state,
4461 struct fruit_pwrite_state);
4466 if (fruit_must_handle_aio_stream(fio)) {
4467 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4468 if (state->nwritten != n) {
4469 if (state->nwritten != -1) {
4472 tevent_req_error(req, errno);
4473 return tevent_req_post(req, ev);
4475 tevent_req_done(req);
4476 return tevent_req_post(req, ev);
4479 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4481 if (tevent_req_nomem(req, subreq)) {
4482 return tevent_req_post(req, ev);
4484 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4488 static void fruit_pwrite_done(struct tevent_req *subreq)
4490 struct tevent_req *req = tevent_req_callback_data(
4491 subreq, struct tevent_req);
4492 struct fruit_pwrite_state *state = tevent_req_data(
4493 req, struct fruit_pwrite_state);
4495 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4496 TALLOC_FREE(subreq);
4498 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4501 tevent_req_done(req);
4504 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4505 struct vfs_aio_state *vfs_aio_state)
4507 struct fruit_pwrite_state *state = tevent_req_data(
4508 req, struct fruit_pwrite_state);
4510 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4514 *vfs_aio_state = state->vfs_aio_state;
4515 return state->nwritten;
4519 * Helper to stat/lstat the base file of an smb_fname.
4521 static int fruit_stat_base(vfs_handle_struct *handle,
4522 struct smb_filename *smb_fname,
4525 char *tmp_stream_name;
4528 tmp_stream_name = smb_fname->stream_name;
4529 smb_fname->stream_name = NULL;
4531 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4533 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4535 smb_fname->stream_name = tmp_stream_name;
4539 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4540 struct smb_filename *smb_fname,
4546 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4548 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4554 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4555 struct smb_filename *smb_fname,
4558 struct adouble *ad = NULL;
4560 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4562 DBG_INFO("fruit_stat_meta %s: %s\n",
4563 smb_fname_str_dbg(smb_fname), strerror(errno));
4569 /* Populate the stat struct with info from the base file. */
4570 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4573 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4574 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4575 smb_fname->stream_name);
4579 static int fruit_stat_meta(vfs_handle_struct *handle,
4580 struct smb_filename *smb_fname,
4583 struct fruit_config_data *config = NULL;
4586 SMB_VFS_HANDLE_GET_DATA(handle, config,
4587 struct fruit_config_data, return -1);
4589 switch (config->meta) {
4590 case FRUIT_META_STREAM:
4591 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4594 case FRUIT_META_NETATALK:
4595 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4599 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4606 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4607 struct smb_filename *smb_fname,
4610 struct adouble *ad = NULL;
4613 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4619 /* Populate the stat struct with info from the base file. */
4620 ret = fruit_stat_base(handle, smb_fname, follow_links);
4626 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4627 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4628 smb_fname->stream_name);
4633 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4634 struct smb_filename *smb_fname,
4640 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4642 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4648 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4649 struct smb_filename *smb_fname,
4652 #ifdef HAVE_ATTROPEN
4656 /* Populate the stat struct with info from the base file. */
4657 ret = fruit_stat_base(handle, smb_fname, follow_links);
4662 fd = attropen(smb_fname->base_name,
4663 AFPRESOURCE_EA_NETATALK,
4669 ret = sys_fstat(fd, &smb_fname->st, false);
4672 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4673 AFPRESOURCE_EA_NETATALK);
4679 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4680 smb_fname->stream_name);
4690 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4691 struct smb_filename *smb_fname,
4694 struct fruit_config_data *config = NULL;
4697 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4699 SMB_VFS_HANDLE_GET_DATA(handle, config,
4700 struct fruit_config_data, return -1);
4702 switch (config->rsrc) {
4703 case FRUIT_RSRC_STREAM:
4704 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4707 case FRUIT_RSRC_XATTR:
4708 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4711 case FRUIT_RSRC_ADFILE:
4712 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4716 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4723 static int fruit_stat(vfs_handle_struct *handle,
4724 struct smb_filename *smb_fname)
4728 DEBUG(10, ("fruit_stat called for %s\n",
4729 smb_fname_str_dbg(smb_fname)));
4731 if (!is_ntfs_stream_smb_fname(smb_fname)
4732 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4733 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4735 update_btime(handle, smb_fname);
4741 * Note if lp_posix_paths() is true, we can never
4742 * get here as is_ntfs_stream_smb_fname() is
4743 * always false. So we never need worry about
4744 * not following links here.
4747 if (is_afpinfo_stream(smb_fname)) {
4748 rc = fruit_stat_meta(handle, smb_fname, true);
4749 } else if (is_afpresource_stream(smb_fname)) {
4750 rc = fruit_stat_rsrc(handle, smb_fname, true);
4752 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4756 update_btime(handle, smb_fname);
4757 smb_fname->st.st_ex_mode &= ~S_IFMT;
4758 smb_fname->st.st_ex_mode |= S_IFREG;
4759 smb_fname->st.st_ex_blocks =
4760 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4765 static int fruit_lstat(vfs_handle_struct *handle,
4766 struct smb_filename *smb_fname)
4770 DEBUG(10, ("fruit_lstat called for %s\n",
4771 smb_fname_str_dbg(smb_fname)));
4773 if (!is_ntfs_stream_smb_fname(smb_fname)
4774 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4775 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4777 update_btime(handle, smb_fname);
4782 if (is_afpinfo_stream(smb_fname)) {
4783 rc = fruit_stat_meta(handle, smb_fname, false);
4784 } else if (is_afpresource_stream(smb_fname)) {
4785 rc = fruit_stat_rsrc(handle, smb_fname, false);
4787 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4791 update_btime(handle, smb_fname);
4792 smb_fname->st.st_ex_mode &= ~S_IFMT;
4793 smb_fname->st.st_ex_mode |= S_IFREG;
4794 smb_fname->st.st_ex_blocks =
4795 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4800 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4802 SMB_STRUCT_STAT *sbuf)
4804 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4807 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4809 SMB_STRUCT_STAT *sbuf)
4813 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4818 *sbuf = fsp->base_fsp->fsp_name->st;
4819 sbuf->st_ex_size = AFP_INFO_SIZE;
4820 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4825 static int fruit_fstat_meta(vfs_handle_struct *handle,
4827 SMB_STRUCT_STAT *sbuf,
4832 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4834 switch (fio->config->meta) {
4835 case FRUIT_META_STREAM:
4836 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4839 case FRUIT_META_NETATALK:
4840 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4844 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4848 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4852 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4854 SMB_STRUCT_STAT *sbuf)
4856 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4859 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4861 SMB_STRUCT_STAT *sbuf)
4863 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4866 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4868 SMB_STRUCT_STAT *sbuf)
4870 struct adouble *ad = NULL;
4873 /* Populate the stat struct with info from the base file. */
4874 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4879 ad = ad_get(talloc_tos(), handle,
4880 fsp->base_fsp->fsp_name,
4883 DBG_ERR("ad_get [%s] failed [%s]\n",
4884 fsp_str_dbg(fsp), strerror(errno));
4888 *sbuf = fsp->base_fsp->fsp_name->st;
4889 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4890 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4896 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4897 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4901 switch (fio->config->rsrc) {
4902 case FRUIT_RSRC_STREAM:
4903 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4906 case FRUIT_RSRC_ADFILE:
4907 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4910 case FRUIT_RSRC_XATTR:
4911 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4915 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4922 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4923 SMB_STRUCT_STAT *sbuf)
4925 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4929 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4932 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4934 if (fio->type == ADOUBLE_META) {
4935 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4937 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4941 sbuf->st_ex_mode &= ~S_IFMT;
4942 sbuf->st_ex_mode |= S_IFREG;
4943 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4946 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4947 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4951 static NTSTATUS delete_invalid_meta_stream(
4952 vfs_handle_struct *handle,
4953 const struct smb_filename *smb_fname,
4954 TALLOC_CTX *mem_ctx,
4955 unsigned int *pnum_streams,
4956 struct stream_struct **pstreams)
4958 struct smb_filename *sname = NULL;
4962 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4964 return NT_STATUS_INTERNAL_ERROR;
4967 sname = synthetic_smb_fname(talloc_tos(),
4968 smb_fname->base_name,
4969 AFPINFO_STREAM_NAME,
4971 if (sname == NULL) {
4972 return NT_STATUS_NO_MEMORY;
4975 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4978 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4979 return map_nt_error_from_unix(errno);
4982 return NT_STATUS_OK;
4985 static NTSTATUS fruit_streaminfo_meta_stream(
4986 vfs_handle_struct *handle,
4987 struct files_struct *fsp,
4988 const struct smb_filename *smb_fname,
4989 TALLOC_CTX *mem_ctx,
4990 unsigned int *pnum_streams,
4991 struct stream_struct **pstreams)
4993 struct stream_struct *stream = *pstreams;
4994 unsigned int num_streams = *pnum_streams;
4995 struct smb_filename *sname = NULL;
4996 char *full_name = NULL;
4998 struct share_mode_lock *lck = NULL;
4999 struct file_id id = {0};
5000 bool delete_on_close_set;
5006 for (i = 0; i < num_streams; i++) {
5007 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5012 if (i == num_streams) {
5013 return NT_STATUS_OK;
5016 if (stream[i].size != AFP_INFO_SIZE) {
5017 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5018 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5020 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5021 pnum_streams, pstreams);
5025 * Now check if there's a delete-on-close pending on the stream. If so,
5026 * hide the stream. This behaviour was verified against a macOS 10.12
5030 sname = synthetic_smb_fname(talloc_tos(),
5031 smb_fname->base_name,
5032 AFPINFO_STREAM_NAME,
5034 if (sname == NULL) {
5035 status = NT_STATUS_NO_MEMORY;
5039 ret = SMB_VFS_NEXT_STAT(handle, sname);
5041 status = map_nt_error_from_unix(errno);
5045 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5047 lck = get_existing_share_mode_lock(talloc_tos(), id);
5049 status = NT_STATUS_OK;
5053 full_name = talloc_asprintf(talloc_tos(),
5057 if (full_name == NULL) {
5058 status = NT_STATUS_NO_MEMORY;
5062 status = file_name_hash(handle->conn, full_name, &name_hash);
5063 if (!NT_STATUS_IS_OK(status)) {
5067 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5068 if (delete_on_close_set) {
5069 ok = del_fruit_stream(mem_ctx,
5074 status = NT_STATUS_INTERNAL_ERROR;
5079 status = NT_STATUS_OK;
5084 TALLOC_FREE(full_name);
5088 static NTSTATUS fruit_streaminfo_meta_netatalk(
5089 vfs_handle_struct *handle,
5090 struct files_struct *fsp,
5091 const struct smb_filename *smb_fname,
5092 TALLOC_CTX *mem_ctx,
5093 unsigned int *pnum_streams,
5094 struct stream_struct **pstreams)
5096 struct stream_struct *stream = *pstreams;
5097 unsigned int num_streams = *pnum_streams;
5098 struct adouble *ad = NULL;
5103 /* Remove the Netatalk xattr from the list */
5104 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5105 ":" NETATALK_META_XATTR ":$DATA");
5107 return NT_STATUS_NO_MEMORY;
5111 * Check if there's a AFPINFO_STREAM from the VFS streams
5112 * backend and if yes, remove it from the list
5114 for (i = 0; i < num_streams; i++) {
5115 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5120 if (i < num_streams) {
5121 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5122 smb_fname_str_dbg(smb_fname));
5124 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5127 return NT_STATUS_INTERNAL_ERROR;
5131 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5133 return NT_STATUS_OK;
5136 is_fi_empty = ad_empty_finderinfo(ad);
5140 return NT_STATUS_OK;
5143 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5144 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5145 smb_roundup(handle->conn, AFP_INFO_SIZE));
5147 return NT_STATUS_NO_MEMORY;
5150 return NT_STATUS_OK;
5153 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5154 struct files_struct *fsp,
5155 const struct smb_filename *smb_fname,
5156 TALLOC_CTX *mem_ctx,
5157 unsigned int *pnum_streams,
5158 struct stream_struct **pstreams)
5160 struct fruit_config_data *config = NULL;
5163 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5164 return NT_STATUS_INTERNAL_ERROR);
5166 switch (config->meta) {
5167 case FRUIT_META_NETATALK:
5168 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5169 mem_ctx, pnum_streams,
5173 case FRUIT_META_STREAM:
5174 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5175 mem_ctx, pnum_streams,
5180 return NT_STATUS_INTERNAL_ERROR;
5186 static NTSTATUS fruit_streaminfo_rsrc_stream(
5187 vfs_handle_struct *handle,
5188 struct files_struct *fsp,
5189 const struct smb_filename *smb_fname,
5190 TALLOC_CTX *mem_ctx,
5191 unsigned int *pnum_streams,
5192 struct stream_struct **pstreams)
5196 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5198 DBG_ERR("Filtering resource stream failed\n");
5199 return NT_STATUS_INTERNAL_ERROR;
5201 return NT_STATUS_OK;
5204 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5205 vfs_handle_struct *handle,
5206 struct files_struct *fsp,
5207 const struct smb_filename *smb_fname,
5208 TALLOC_CTX *mem_ctx,
5209 unsigned int *pnum_streams,
5210 struct stream_struct **pstreams)
5214 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5216 DBG_ERR("Filtering resource stream failed\n");
5217 return NT_STATUS_INTERNAL_ERROR;
5219 return NT_STATUS_OK;
5222 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5223 vfs_handle_struct *handle,
5224 struct files_struct *fsp,
5225 const struct smb_filename *smb_fname,
5226 TALLOC_CTX *mem_ctx,
5227 unsigned int *pnum_streams,
5228 struct stream_struct **pstreams)
5230 struct stream_struct *stream = *pstreams;
5231 unsigned int num_streams = *pnum_streams;
5232 struct adouble *ad = NULL;
5238 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5239 * and if yes, remove it from the list
5241 for (i = 0; i < num_streams; i++) {
5242 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5247 if (i < num_streams) {
5248 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5249 smb_fname_str_dbg(smb_fname));
5251 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5252 AFPRESOURCE_STREAM);
5254 return NT_STATUS_INTERNAL_ERROR;
5258 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5260 return NT_STATUS_OK;
5263 rlen = ad_getentrylen(ad, ADEID_RFORK);
5267 return NT_STATUS_OK;
5270 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5271 AFPRESOURCE_STREAM_NAME, rlen,
5272 smb_roundup(handle->conn, rlen));
5274 return NT_STATUS_NO_MEMORY;
5277 return NT_STATUS_OK;
5280 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5281 struct files_struct *fsp,
5282 const struct smb_filename *smb_fname,
5283 TALLOC_CTX *mem_ctx,
5284 unsigned int *pnum_streams,
5285 struct stream_struct **pstreams)
5287 struct fruit_config_data *config = NULL;
5290 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5291 return NT_STATUS_INTERNAL_ERROR);
5293 switch (config->rsrc) {
5294 case FRUIT_RSRC_STREAM:
5295 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5296 mem_ctx, pnum_streams,
5300 case FRUIT_RSRC_XATTR:
5301 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5302 mem_ctx, pnum_streams,
5306 case FRUIT_RSRC_ADFILE:
5307 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5308 mem_ctx, pnum_streams,
5313 return NT_STATUS_INTERNAL_ERROR;
5319 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5320 struct files_struct *fsp,
5321 const struct smb_filename *smb_fname,
5322 TALLOC_CTX *mem_ctx,
5323 unsigned int *pnum_streams,
5324 struct stream_struct **pstreams)
5326 struct fruit_config_data *config = NULL;
5329 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5330 return NT_STATUS_UNSUCCESSFUL);
5332 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5334 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5335 pnum_streams, pstreams);
5336 if (!NT_STATUS_IS_OK(status)) {
5340 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5341 mem_ctx, pnum_streams, pstreams);
5342 if (!NT_STATUS_IS_OK(status)) {
5346 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5347 mem_ctx, pnum_streams, pstreams);
5348 if (!NT_STATUS_IS_OK(status)) {
5352 return NT_STATUS_OK;
5355 static int fruit_ntimes(vfs_handle_struct *handle,
5356 const struct smb_filename *smb_fname,
5357 struct smb_file_time *ft)
5360 struct adouble *ad = NULL;
5361 struct fruit_config_data *config = NULL;
5363 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5366 if ((config->meta != FRUIT_META_NETATALK) ||
5367 null_timespec(ft->create_time))
5369 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5372 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5373 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5375 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5380 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5381 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5383 rc = ad_set(ad, smb_fname);
5389 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5392 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5395 static int fruit_fallocate(struct vfs_handle_struct *handle,
5396 struct files_struct *fsp,
5401 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5404 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5407 /* Let the pwrite code path handle it. */
5412 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5413 struct files_struct *fsp,
5417 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5420 #ifdef HAVE_ATTROPEN
5421 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5426 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5427 struct files_struct *fsp,
5431 struct adouble *ad = NULL;
5434 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5436 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5437 fsp_str_dbg(fsp), strerror(errno));
5441 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5443 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5449 ad_setentrylen(ad, ADEID_RFORK, offset);
5451 rc = ad_fset(ad, fsp);
5453 DBG_ERR("ad_fset [%s] failed [%s]\n",
5454 fsp_str_dbg(fsp), strerror(errno));
5463 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5464 struct files_struct *fsp,
5468 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5471 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5474 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5475 struct files_struct *fsp,
5478 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5481 switch (fio->config->rsrc) {
5482 case FRUIT_RSRC_XATTR:
5483 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5486 case FRUIT_RSRC_ADFILE:
5487 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5490 case FRUIT_RSRC_STREAM:
5491 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5495 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5503 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5504 struct files_struct *fsp,
5508 DBG_WARNING("ftruncate %s to %jd",
5509 fsp_str_dbg(fsp), (intmax_t)offset);
5510 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5515 /* OS X returns success but does nothing */
5516 DBG_INFO("ignoring ftruncate %s to %jd\n",
5517 fsp_str_dbg(fsp), (intmax_t)offset);
5521 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5522 struct files_struct *fsp,
5525 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5528 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5532 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5535 if (fio->type == ADOUBLE_META) {
5536 ret = fruit_ftruncate_meta(handle, fsp, offset);
5538 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5541 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5545 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5546 struct smb_request *req,
5547 uint16_t root_dir_fid,
5548 struct smb_filename *smb_fname,
5549 uint32_t access_mask,
5550 uint32_t share_access,
5551 uint32_t create_disposition,
5552 uint32_t create_options,
5553 uint32_t file_attributes,
5554 uint32_t oplock_request,
5555 struct smb2_lease *lease,
5556 uint64_t allocation_size,
5557 uint32_t private_flags,
5558 struct security_descriptor *sd,
5559 struct ea_list *ea_list,
5560 files_struct **result,
5562 const struct smb2_create_blobs *in_context_blobs,
5563 struct smb2_create_blobs *out_context_blobs)
5566 struct fruit_config_data *config = NULL;
5567 files_struct *fsp = NULL;
5569 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5570 if (!NT_STATUS_IS_OK(status)) {
5574 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5575 return NT_STATUS_UNSUCCESSFUL);
5577 status = SMB_VFS_NEXT_CREATE_FILE(
5578 handle, req, root_dir_fid, smb_fname,
5579 access_mask, share_access,
5580 create_disposition, create_options,
5581 file_attributes, oplock_request,
5583 allocation_size, private_flags,
5584 sd, ea_list, result,
5585 pinfo, in_context_blobs, out_context_blobs);
5586 if (!NT_STATUS_IS_OK(status)) {
5592 if (global_fruit_config.nego_aapl) {
5593 if (config->posix_rename && fsp->is_directory) {
5595 * Enable POSIX directory rename behaviour
5597 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5602 * If this is a plain open for existing files, opening an 0
5603 * byte size resource fork MUST fail with
5604 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5606 * Cf the vfs_fruit torture tests in test_rfork_create().
5608 if (is_afpresource_stream(fsp->fsp_name) &&
5609 create_disposition == FILE_OPEN)
5611 if (fsp->fsp_name->st.st_ex_size == 0) {
5612 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5617 if (is_ntfs_stream_smb_fname(smb_fname)
5618 || fsp->is_directory) {
5622 if (config->locking == FRUIT_LOCKING_NETATALK) {
5623 status = fruit_check_access(
5626 map_share_mode_to_deny_mode(share_access, 0));
5627 if (!NT_STATUS_IS_OK(status)) {
5635 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5638 close_file(req, fsp, ERROR_CLOSE);
5639 *result = fsp = NULL;
5645 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5646 const struct smb_filename *fname,
5647 TALLOC_CTX *mem_ctx,
5648 struct readdir_attr_data **pattr_data)
5650 struct fruit_config_data *config = NULL;
5651 struct readdir_attr_data *attr_data;
5654 SMB_VFS_HANDLE_GET_DATA(handle, config,
5655 struct fruit_config_data,
5656 return NT_STATUS_UNSUCCESSFUL);
5658 if (!global_fruit_config.nego_aapl) {
5659 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5662 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5664 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5665 if (*pattr_data == NULL) {
5666 return NT_STATUS_UNSUCCESSFUL;
5668 attr_data = *pattr_data;
5669 attr_data->type = RDATTR_AAPL;
5672 * Mac metadata: compressed FinderInfo, resource fork length
5675 status = readdir_attr_macmeta(handle, fname, attr_data);
5676 if (!NT_STATUS_IS_OK(status)) {
5678 * Error handling is tricky: if we return failure from
5679 * this function, the corresponding directory entry
5680 * will to be passed to the client, so we really just
5681 * want to error out on fatal errors.
5683 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5691 if (config->unix_info_enabled) {
5692 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5698 if (!config->readdir_attr_max_access) {
5699 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5701 status = smbd_calculate_access_mask(
5705 SEC_FLAG_MAXIMUM_ALLOWED,
5706 &attr_data->attr_data.aapl.max_access);
5707 if (!NT_STATUS_IS_OK(status)) {
5712 return NT_STATUS_OK;
5715 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5716 fname->base_name, nt_errstr(status)));
5717 TALLOC_FREE(*pattr_data);
5721 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5723 uint32_t security_info,
5724 TALLOC_CTX *mem_ctx,
5725 struct security_descriptor **ppdesc)
5728 struct security_ace ace;
5730 struct fruit_config_data *config;
5731 bool remove_ok = false;
5733 SMB_VFS_HANDLE_GET_DATA(handle, config,
5734 struct fruit_config_data,
5735 return NT_STATUS_UNSUCCESSFUL);
5737 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5739 if (!NT_STATUS_IS_OK(status)) {
5744 * Add MS NFS style ACEs with uid, gid and mode
5746 if (!global_fruit_config.nego_aapl) {
5747 return NT_STATUS_OK;
5749 if (!config->unix_info_enabled) {
5750 return NT_STATUS_OK;
5753 /* MS NFS style mode */
5754 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5755 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5757 /* First remove any existing ACE's with this SID. */
5758 status = security_descriptor_dacl_del(*ppdesc, &sid);
5759 remove_ok = (NT_STATUS_IS_OK(status) ||
5760 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
5762 DBG_WARNING("failed to remove MS NFS_mode style ACE\n");
5765 status = security_descriptor_dacl_add(*ppdesc, &ace);
5766 if (!NT_STATUS_IS_OK(status)) {
5767 DEBUG(1,("failed to add MS NFS style ACE\n"));
5771 /* MS NFS style uid */
5772 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5773 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5775 /* First remove any existing ACE's with this SID. */
5776 status = security_descriptor_dacl_del(*ppdesc, &sid);
5777 remove_ok = (NT_STATUS_IS_OK(status) ||
5778 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
5780 DBG_WARNING("failed to remove MS NFS_users style ACE\n");
5783 status = security_descriptor_dacl_add(*ppdesc, &ace);
5784 if (!NT_STATUS_IS_OK(status)) {
5785 DEBUG(1,("failed to add MS NFS style ACE\n"));
5789 /* MS NFS style gid */
5790 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5791 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5793 /* First remove any existing ACE's with this SID. */
5794 status = security_descriptor_dacl_del(*ppdesc, &sid);
5795 remove_ok = (NT_STATUS_IS_OK(status) ||
5796 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
5798 DBG_WARNING("failed to remove MS NFS_groups style ACE\n");
5801 status = security_descriptor_dacl_add(*ppdesc, &ace);
5802 if (!NT_STATUS_IS_OK(status)) {
5803 DEBUG(1,("failed to add MS NFS style ACE\n"));
5807 return NT_STATUS_OK;
5810 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5812 uint32_t security_info_sent,
5813 const struct security_descriptor *orig_psd)
5817 mode_t ms_nfs_mode = 0;
5819 struct security_descriptor *psd = NULL;
5820 uint32_t orig_num_aces = 0;
5822 if (orig_psd->dacl != NULL) {
5823 orig_num_aces = orig_psd->dacl->num_aces;
5826 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5828 return NT_STATUS_NO_MEMORY;
5831 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5833 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5834 if (!NT_STATUS_IS_OK(status)) {
5835 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5841 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5842 * sent/present flags correctly now we've removed them.
5845 if (orig_num_aces != 0) {
5847 * Are there any ACE's left ?
5849 if (psd->dacl->num_aces == 0) {
5850 /* No - clear the DACL sent/present flags. */
5851 security_info_sent &= ~SECINFO_DACL;
5852 psd->type &= ~SEC_DESC_DACL_PRESENT;
5856 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5857 if (!NT_STATUS_IS_OK(status)) {
5858 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5864 if (fsp->fh->fd != -1) {
5865 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5867 result = SMB_VFS_CHMOD(fsp->conn,
5873 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5874 result, (unsigned)ms_nfs_mode,
5876 status = map_nt_error_from_unix(errno);
5883 return NT_STATUS_OK;
5886 static struct vfs_offload_ctx *fruit_offload_ctx;
5888 struct fruit_offload_read_state {
5889 struct vfs_handle_struct *handle;
5890 struct tevent_context *ev;
5896 static void fruit_offload_read_done(struct tevent_req *subreq);
5898 static struct tevent_req *fruit_offload_read_send(
5899 TALLOC_CTX *mem_ctx,
5900 struct tevent_context *ev,
5901 struct vfs_handle_struct *handle,
5908 struct tevent_req *req = NULL;
5909 struct tevent_req *subreq = NULL;
5910 struct fruit_offload_read_state *state = NULL;
5912 req = tevent_req_create(mem_ctx, &state,
5913 struct fruit_offload_read_state);
5917 *state = (struct fruit_offload_read_state) {
5924 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5925 fsctl, ttl, offset, to_copy);
5926 if (tevent_req_nomem(subreq, req)) {
5927 return tevent_req_post(req, ev);
5929 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5933 static void fruit_offload_read_done(struct tevent_req *subreq)
5935 struct tevent_req *req = tevent_req_callback_data(
5936 subreq, struct tevent_req);
5937 struct fruit_offload_read_state *state = tevent_req_data(
5938 req, struct fruit_offload_read_state);
5941 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5945 TALLOC_FREE(subreq);
5946 if (tevent_req_nterror(req, status)) {
5950 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5951 tevent_req_done(req);
5955 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5956 &fruit_offload_ctx);
5957 if (tevent_req_nterror(req, status)) {
5961 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5964 if (tevent_req_nterror(req, status)) {
5968 tevent_req_done(req);
5972 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5973 struct vfs_handle_struct *handle,
5974 TALLOC_CTX *mem_ctx,
5977 struct fruit_offload_read_state *state = tevent_req_data(
5978 req, struct fruit_offload_read_state);
5981 if (tevent_req_is_nterror(req, &status)) {
5982 tevent_req_received(req);
5986 token->length = state->token.length;
5987 token->data = talloc_move(mem_ctx, &state->token.data);
5989 tevent_req_received(req);
5990 return NT_STATUS_OK;
5993 struct fruit_offload_write_state {
5994 struct vfs_handle_struct *handle;
5996 struct files_struct *src_fsp;
5997 struct files_struct *dst_fsp;
6001 static void fruit_offload_write_done(struct tevent_req *subreq);
6002 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6003 TALLOC_CTX *mem_ctx,
6004 struct tevent_context *ev,
6007 off_t transfer_offset,
6008 struct files_struct *dest_fsp,
6012 struct tevent_req *req, *subreq;
6013 struct fruit_offload_write_state *state;
6015 struct fruit_config_data *config;
6016 off_t src_off = transfer_offset;
6017 files_struct *src_fsp = NULL;
6018 off_t to_copy = num;
6019 bool copyfile_enabled = false;
6021 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6022 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6024 SMB_VFS_HANDLE_GET_DATA(handle, config,
6025 struct fruit_config_data,
6028 req = tevent_req_create(mem_ctx, &state,
6029 struct fruit_offload_write_state);
6033 state->handle = handle;
6034 state->dst_fsp = dest_fsp;
6037 case FSCTL_SRV_COPYCHUNK:
6038 case FSCTL_SRV_COPYCHUNK_WRITE:
6039 copyfile_enabled = config->copyfile_enabled;
6046 * Check if this a OS X copyfile style copychunk request with
6047 * a requested chunk count of 0 that was translated to a
6048 * offload_write_send VFS call overloading the parameters src_off
6049 * = dest_off = num = 0.
6051 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6052 status = vfs_offload_token_db_fetch_fsp(
6053 fruit_offload_ctx, token, &src_fsp);
6054 if (tevent_req_nterror(req, status)) {
6055 return tevent_req_post(req, ev);
6057 state->src_fsp = src_fsp;
6059 status = vfs_stat_fsp(src_fsp);
6060 if (tevent_req_nterror(req, status)) {
6061 return tevent_req_post(req, ev);
6064 to_copy = src_fsp->fsp_name->st.st_ex_size;
6065 state->is_copyfile = true;
6068 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6077 if (tevent_req_nomem(subreq, req)) {
6078 return tevent_req_post(req, ev);
6081 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6085 static void fruit_offload_write_done(struct tevent_req *subreq)
6087 struct tevent_req *req = tevent_req_callback_data(
6088 subreq, struct tevent_req);
6089 struct fruit_offload_write_state *state = tevent_req_data(
6090 req, struct fruit_offload_write_state);
6092 unsigned int num_streams = 0;
6093 struct stream_struct *streams = NULL;
6095 struct smb_filename *src_fname_tmp = NULL;
6096 struct smb_filename *dst_fname_tmp = NULL;
6098 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6101 TALLOC_FREE(subreq);
6102 if (tevent_req_nterror(req, status)) {
6106 if (!state->is_copyfile) {
6107 tevent_req_done(req);
6112 * Now copy all remaining streams. We know the share supports
6113 * streams, because we're in vfs_fruit. We don't do this async
6114 * because streams are few and small.
6116 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6117 state->src_fsp->fsp_name,
6118 req, &num_streams, &streams);
6119 if (tevent_req_nterror(req, status)) {
6123 if (num_streams == 1) {
6124 /* There is always one stream, ::$DATA. */
6125 tevent_req_done(req);
6129 for (i = 0; i < num_streams; i++) {
6130 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6131 __func__, streams[i].name, (size_t)streams[i].size));
6133 src_fname_tmp = synthetic_smb_fname(
6135 state->src_fsp->fsp_name->base_name,
6138 state->src_fsp->fsp_name->flags);
6139 if (tevent_req_nomem(src_fname_tmp, req)) {
6143 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6144 TALLOC_FREE(src_fname_tmp);
6148 dst_fname_tmp = synthetic_smb_fname(
6150 state->dst_fsp->fsp_name->base_name,
6153 state->dst_fsp->fsp_name->flags);
6154 if (tevent_req_nomem(dst_fname_tmp, req)) {
6155 TALLOC_FREE(src_fname_tmp);
6159 status = copy_file(req,
6160 state->handle->conn,
6163 OPENX_FILE_CREATE_IF_NOT_EXIST,
6165 if (!NT_STATUS_IS_OK(status)) {
6166 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6167 smb_fname_str_dbg(src_fname_tmp),
6168 smb_fname_str_dbg(dst_fname_tmp),
6169 nt_errstr(status)));
6170 TALLOC_FREE(src_fname_tmp);
6171 TALLOC_FREE(dst_fname_tmp);
6172 tevent_req_nterror(req, status);
6176 TALLOC_FREE(src_fname_tmp);
6177 TALLOC_FREE(dst_fname_tmp);
6180 TALLOC_FREE(streams);
6181 TALLOC_FREE(src_fname_tmp);
6182 TALLOC_FREE(dst_fname_tmp);
6183 tevent_req_done(req);
6186 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6187 struct tevent_req *req,
6190 struct fruit_offload_write_state *state = tevent_req_data(
6191 req, struct fruit_offload_write_state);
6194 if (tevent_req_is_nterror(req, &status)) {
6195 DEBUG(1, ("server side copy chunk failed: %s\n",
6196 nt_errstr(status)));
6198 tevent_req_received(req);
6202 *copied = state->copied;
6203 tevent_req_received(req);
6205 return NT_STATUS_OK;
6208 static char *fruit_get_bandsize_line(char **lines, int numlines)
6211 static bool re_initialized = false;
6215 if (!re_initialized) {
6216 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6220 re_initialized = true;
6223 for (i = 0; i < numlines; i++) {
6224 regmatch_t matches[1];
6226 ret = regexec(&re, lines[i], 1, matches, 0);
6229 * Check if the match was on the last line, sa we want
6230 * the subsequent line.
6232 if (i + 1 == numlines) {
6235 return lines[i + 1];
6237 if (ret != REG_NOMATCH) {
6245 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6248 static bool re_initialized = false;
6249 regmatch_t matches[2];
6254 if (!re_initialized) {
6257 "<integer>\\([[:digit:]]*\\)</integer>$",
6262 re_initialized = true;
6265 ret = regexec(&re, line, 2, matches, 0);
6267 DBG_ERR("regex failed [%s]\n", line);
6271 line[matches[1].rm_eo] = '\0';
6273 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6277 *_band_size = (size_t)band_size;
6282 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6283 * "band-size" key and value.
6285 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6289 #define INFO_PLIST_MAX_SIZE 64*1024
6291 struct smb_filename *smb_fname = NULL;
6292 files_struct *fsp = NULL;
6293 uint8_t *file_data = NULL;
6294 char **lines = NULL;
6295 char *band_size_line = NULL;
6296 size_t plist_file_size;
6303 plist = talloc_asprintf(talloc_tos(),
6305 handle->conn->connectpath,
6307 if (plist == NULL) {
6312 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6313 if (smb_fname == NULL) {
6318 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6320 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6325 plist_file_size = smb_fname->st.st_ex_size;
6327 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6328 DBG_INFO("%s is too large, ignoring\n", plist);
6333 status = SMB_VFS_NEXT_CREATE_FILE(
6336 0, /* root_dir_fid */
6337 smb_fname, /* fname */
6338 FILE_GENERIC_READ, /* access_mask */
6339 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6340 FILE_OPEN, /* create_disposition */
6341 0, /* create_options */
6342 0, /* file_attributes */
6343 INTERNAL_OPEN_ONLY, /* oplock_request */
6345 0, /* allocation_size */
6346 0, /* private_flags */
6351 NULL, NULL); /* create context */
6352 if (!NT_STATUS_IS_OK(status)) {
6353 DBG_INFO("Opening [%s] failed [%s]\n",
6354 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6359 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6360 if (file_data == NULL) {
6365 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6366 if (nread != plist_file_size) {
6367 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6368 fsp_str_dbg(fsp), nread, plist_file_size);
6374 status = close_file(NULL, fsp, NORMAL_CLOSE);
6376 if (!NT_STATUS_IS_OK(status)) {
6377 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6382 lines = file_lines_parse((char *)file_data,
6386 if (lines == NULL) {
6391 band_size_line = fruit_get_bandsize_line(lines, numlines);
6392 if (band_size_line == NULL) {
6393 DBG_ERR("Didn't find band-size key in [%s]\n",
6394 smb_fname_str_dbg(smb_fname));
6399 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6401 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6405 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6409 status = close_file(NULL, fsp, NORMAL_CLOSE);
6410 if (!NT_STATUS_IS_OK(status)) {
6411 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6416 TALLOC_FREE(smb_fname);
6417 TALLOC_FREE(file_data);
6422 struct fruit_disk_free_state {
6426 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6431 struct smb_filename *bands_dir = NULL;
6433 struct dirent *e = NULL;
6437 path = talloc_asprintf(talloc_tos(),
6439 handle->conn->connectpath,
6445 bands_dir = synthetic_smb_fname(talloc_tos(),
6451 if (bands_dir == NULL) {
6455 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6457 TALLOC_FREE(bands_dir);
6463 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6465 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6467 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6473 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6475 TALLOC_FREE(bands_dir);
6479 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6481 TALLOC_FREE(bands_dir);
6487 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6488 struct fruit_disk_free_state *state,
6493 size_t sparsebundle_strlen = strlen("sparsebundle");
6494 size_t bandsize = 0;
6498 p = strstr(e->d_name, "sparsebundle");
6503 if (p[sparsebundle_strlen] != '\0') {
6507 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6509 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6512 * Beware of race conditions: this may be an uninitialized
6513 * Info.plist that a client is just creating. We don't want let
6514 * this to trigger complete failure.
6516 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6520 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6523 * Beware of race conditions: this may be a backup sparsebundle
6524 * in an early stage lacking a bands subdirectory. We don't want
6525 * let this to trigger complete failure.
6527 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6531 tm_size = bandsize * nbands;
6532 if (tm_size > UINT64_MAX) {
6533 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6538 if (state->total_size + tm_size < state->total_size) {
6539 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6544 state->total_size += tm_size;
6546 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6547 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6553 * Calculate used size of a TimeMachine volume
6555 * This assumes that the volume is used only for TimeMachine.
6557 * - readdir(basedir of share), then
6558 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6559 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6560 * - count band files in "\1.sparsebundle/bands/"
6561 * - calculate used size of all bands: band_count * band_size
6563 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6564 const struct smb_filename *smb_fname,
6569 struct fruit_config_data *config = NULL;
6570 struct fruit_disk_free_state state = {0};
6572 struct dirent *e = NULL;
6578 SMB_VFS_HANDLE_GET_DATA(handle, config,
6579 struct fruit_config_data,
6582 if (!config->time_machine ||
6583 config->time_machine_max_size == 0)
6585 return SMB_VFS_NEXT_DISK_FREE(handle,
6592 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6597 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6599 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6601 ok = fruit_tmsize_do_dirent(handle, &state, e);
6603 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6608 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6613 dsize = config->time_machine_max_size / 512;
6614 dfree = dsize - (state.total_size / 512);
6615 if (dfree > dsize) {
6625 static struct vfs_fn_pointers vfs_fruit_fns = {
6626 .connect_fn = fruit_connect,
6627 .disk_free_fn = fruit_disk_free,
6629 /* File operations */
6630 .chmod_fn = fruit_chmod,
6631 .chown_fn = fruit_chown,
6632 .unlink_fn = fruit_unlink,
6633 .rename_fn = fruit_rename,
6634 .rmdir_fn = fruit_rmdir,
6635 .open_fn = fruit_open,
6636 .pread_fn = fruit_pread,
6637 .pwrite_fn = fruit_pwrite,
6638 .pread_send_fn = fruit_pread_send,
6639 .pread_recv_fn = fruit_pread_recv,
6640 .pwrite_send_fn = fruit_pwrite_send,
6641 .pwrite_recv_fn = fruit_pwrite_recv,
6642 .stat_fn = fruit_stat,
6643 .lstat_fn = fruit_lstat,
6644 .fstat_fn = fruit_fstat,
6645 .streaminfo_fn = fruit_streaminfo,
6646 .ntimes_fn = fruit_ntimes,
6647 .ftruncate_fn = fruit_ftruncate,
6648 .fallocate_fn = fruit_fallocate,
6649 .create_file_fn = fruit_create_file,
6650 .readdir_attr_fn = fruit_readdir_attr,
6651 .offload_read_send_fn = fruit_offload_read_send,
6652 .offload_read_recv_fn = fruit_offload_read_recv,
6653 .offload_write_send_fn = fruit_offload_write_send,
6654 .offload_write_recv_fn = fruit_offload_write_recv,
6656 /* NT ACL operations */
6657 .fget_nt_acl_fn = fruit_fget_nt_acl,
6658 .fset_nt_acl_fn = fruit_fset_nt_acl,
6662 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6664 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6666 if (!NT_STATUS_IS_OK(ret)) {
6670 vfs_fruit_debug_level = debug_add_class("fruit");
6671 if (vfs_fruit_debug_level == -1) {
6672 vfs_fruit_debug_level = DBGC_VFS;
6673 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6676 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6677 "vfs_fruit_init","fruit",vfs_fruit_debug_level));