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"
37 * Enhanced OS X and Netatalk compatibility
38 * ========================================
40 * This modules takes advantage of vfs_streams_xattr and
41 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
42 * loaded in the correct order:
44 * vfs modules = catia fruit streams_xattr
46 * The module intercepts the OS X special streams "AFP_AfpInfo" and
47 * "AFP_Resource" and handles them in a special way. All other named
48 * streams are deferred to vfs_streams_xattr.
50 * The OS X client maps all NTFS illegal characters to the Unicode
51 * private range. This module optionally stores the charcters using
52 * their native ASCII encoding using vfs_catia. If you're not enabling
53 * this feature, you can skip catia from vfs modules.
55 * Finally, open modes are optionally checked against Netatalk AFP
58 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
59 * extended metadata for files and directories. This module optionally
60 * reads and stores this metadata in a way compatible with Netatalk 3
61 * which stores the metadata in an EA "org.netatalk.metadata". Cf
62 * source3/include/MacExtensions.h for a description of the binary
65 * The "AFP_Resource" named stream may be arbitrarily large, thus it
66 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
67 * the only available filesystem where xattrs can be of any size and
68 * the OS supports using the file APIs for xattrs.
70 * The AFP_Resource stream is stored in an AppleDouble file prepending
71 * "._" to the filename. On Solaris with ZFS the stream is optionally
72 * stored in an EA "org.netatalk.resource".
78 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
79 * other protocols you may want to adjust the xattr names the VFS
80 * module vfs_streams_xattr uses for storing ADS's. This defaults to
81 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
82 * these module parameters:
84 * streams_xattr:prefix = user.
85 * streams_xattr:store_stream_type = false
91 * - log diagnostic if any needed VFS module is not loaded
92 * (eg with lp_vfs_objects())
96 static int vfs_fruit_debug_level = DBGC_VFS;
98 static struct global_fruit_config {
99 bool nego_aapl; /* client negotiated AAPL */
101 } global_fruit_config;
104 #define DBGC_CLASS vfs_fruit_debug_level
106 #define FRUIT_PARAM_TYPE_NAME "fruit"
107 #define ADOUBLE_NAME_PREFIX "._"
109 #define NETATALK_META_XATTR "org.netatalk.Metadata"
110 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
112 #if defined(HAVE_ATTROPEN)
113 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
114 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
116 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
117 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
120 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
122 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
123 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
124 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
125 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
127 struct fruit_config_data {
128 enum fruit_rsrc rsrc;
129 enum fruit_meta meta;
130 enum fruit_locking locking;
131 enum fruit_encoding encoding;
132 bool use_aapl; /* config from smb.conf */
134 bool readdir_attr_enabled;
135 bool unix_info_enabled;
136 bool copyfile_enabled;
137 bool veto_appledouble;
139 bool aapl_zero_file_id;
142 * Additional options, all enabled by default,
143 * possibly useful for analyzing performance. The associated
144 * operations with each of them may be expensive, so having
145 * the chance to disable them individually gives a chance
146 * tweaking the setup for the particular usecase.
148 bool readdir_attr_rsize;
149 bool readdir_attr_finder_info;
150 bool readdir_attr_max_access;
153 static const struct enum_list fruit_rsrc[] = {
154 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
155 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
156 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
160 static const struct enum_list fruit_meta[] = {
161 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
162 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
166 static const struct enum_list fruit_locking[] = {
167 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
168 {FRUIT_LOCKING_NONE, "none"},
172 static const struct enum_list fruit_encoding[] = {
173 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
174 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
178 /*****************************************************************************
179 * Defines, functions and data structures that deal with AppleDouble
180 *****************************************************************************/
183 * There are two AppleDouble blobs we deal with:
185 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
186 * metadata in an xattr
188 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
191 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
194 #define AD_VERSION2 0x00020000
195 #define AD_VERSION AD_VERSION2
198 * AppleDouble entry IDs.
200 #define ADEID_DFORK 1
201 #define ADEID_RFORK 2
203 #define ADEID_COMMENT 4
204 #define ADEID_ICONBW 5
205 #define ADEID_ICONCOL 6
206 #define ADEID_FILEI 7
207 #define ADEID_FILEDATESI 8
208 #define ADEID_FINDERI 9
209 #define ADEID_MACFILEI 10
210 #define ADEID_PRODOSFILEI 11
211 #define ADEID_MSDOSFILEI 12
212 #define ADEID_SHORTNAME 13
213 #define ADEID_AFPFILEI 14
216 /* Private Netatalk entries */
217 #define ADEID_PRIVDEV 16
218 #define ADEID_PRIVINO 17
219 #define ADEID_PRIVSYN 18
220 #define ADEID_PRIVID 19
221 #define ADEID_MAX (ADEID_PRIVID + 1)
224 * These are the real ids for the private entries,
225 * as stored in the adouble file
227 #define AD_DEV 0x80444556
228 #define AD_INO 0x80494E4F
229 #define AD_SYN 0x8053594E
230 #define AD_ID 0x8053567E
232 /* Number of actually used entries */
233 #define ADEID_NUM_XATTR 8
234 #define ADEID_NUM_DOT_UND 2
235 #define ADEID_NUM_RSRC_XATTR 1
237 /* AppleDouble magic */
238 #define AD_APPLESINGLE_MAGIC 0x00051600
239 #define AD_APPLEDOUBLE_MAGIC 0x00051607
240 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
242 /* Sizes of relevant entry bits */
243 #define ADEDLEN_MAGIC 4
244 #define ADEDLEN_VERSION 4
245 #define ADEDLEN_FILLER 16
246 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
247 #define ADEDLEN_NENTRIES 2
248 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
249 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
250 #define AD_ENTRY_LEN_EID 4
251 #define AD_ENTRY_LEN_OFF 4
252 #define AD_ENTRY_LEN_LEN 4
253 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
256 #define ADEDLEN_NAME 255
257 #define ADEDLEN_COMMENT 200
258 #define ADEDLEN_FILEI 16
259 #define ADEDLEN_FINDERI 32
260 #define ADEDLEN_FILEDATESI 16
261 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
262 #define ADEDLEN_AFPFILEI 4
263 #define ADEDLEN_MACFILEI 4
264 #define ADEDLEN_PRODOSFILEI 8
265 #define ADEDLEN_MSDOSFILEI 2
266 #define ADEDLEN_DID 4
267 #define ADEDLEN_PRIVDEV 8
268 #define ADEDLEN_PRIVINO 8
269 #define ADEDLEN_PRIVSYN 8
270 #define ADEDLEN_PRIVID 4
273 #define ADEDOFF_MAGIC 0
274 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
275 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
276 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
278 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
279 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
280 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
281 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
282 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
284 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
285 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
286 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
287 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
289 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
290 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
291 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
293 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
294 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
295 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
296 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
297 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
298 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
300 #if AD_DATASZ_XATTR != 402
301 #error bad size for AD_DATASZ_XATTR
304 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
305 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
307 #if AD_DATASZ_DOT_UND != 82
308 #error bad size for AD_DATASZ_DOT_UND
312 * Sharemode locks fcntl() offsets
314 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
315 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
317 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
319 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
321 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
322 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
323 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
324 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
325 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
326 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
327 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
328 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
329 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
330 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
332 /* Time stuff we overload the bits a little */
333 #define AD_DATE_CREATE 0
334 #define AD_DATE_MODIFY 4
335 #define AD_DATE_BACKUP 8
336 #define AD_DATE_ACCESS 12
337 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
338 AD_DATE_BACKUP | AD_DATE_ACCESS)
339 #define AD_DATE_UNIX (1 << 10)
340 #define AD_DATE_START 0x80000000
341 #define AD_DATE_DELTA 946684800
342 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
343 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
345 /* Accessor macros */
346 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
347 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
348 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
349 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
357 vfs_handle_struct *ad_handle;
360 adouble_type_t ad_type;
363 struct ad_entry ad_eid[ADEID_MAX];
367 struct ad_entry_order {
368 uint32_t id, offset, len;
371 /* Netatalk AppleDouble metadata xattr */
373 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
374 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
375 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
376 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
377 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
378 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
379 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
380 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
381 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
385 /* AppleDouble resource fork file (the ones prefixed by "._") */
387 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
388 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
389 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
394 * Fake AppleDouble entry oder for resource fork xattr. The xattr
395 * isn't an AppleDouble file, it simply contains the resource data,
396 * but in order to be able to use some API calls like ad_getentryoff()
397 * we build a fake/helper struct adouble with this entry order struct.
400 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
405 /* Conversion from enumerated id to on-disk AppleDouble id */
406 #define AD_EID_DISK(a) (set_eid[a])
407 static const uint32_t set_eid[] = {
408 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
409 AD_DEV, AD_INO, AD_SYN, AD_ID
413 /* tcon config handle */
414 struct fruit_config_data *config;
416 /* Denote stream type, meta or rsrc */
421 * Forward declarations
423 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
424 adouble_type_t type);
425 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
426 static int ad_fset(struct adouble *ad, files_struct *fsp);
427 static int adouble_path(TALLOC_CTX *ctx,
428 const struct smb_filename *smb_fname__in,
429 struct smb_filename **ppsmb_fname_out);
432 * Return a pointer to an AppleDouble entry
434 * Returns NULL if the entry is not present
436 static char *ad_get_entry(const struct adouble *ad, int eid)
438 off_t off = ad_getentryoff(ad, eid);
439 size_t len = ad_getentrylen(ad, eid);
441 if (off == 0 || len == 0) {
445 return ad->ad_data + off;
451 static int ad_getdate(const struct adouble *ad,
452 unsigned int dateoff,
455 bool xlate = (dateoff & AD_DATE_UNIX);
458 dateoff &= AD_DATE_MASK;
459 p = ad_get_entry(ad, ADEID_FILEDATESI);
464 if (dateoff > AD_DATE_ACCESS) {
468 memcpy(date, p + dateoff, sizeof(uint32_t));
471 *date = AD_DATE_TO_UNIX(*date);
479 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
481 bool xlate = (dateoff & AD_DATE_UNIX);
484 p = ad_get_entry(ad, ADEID_FILEDATESI);
489 dateoff &= AD_DATE_MASK;
491 date = AD_DATE_FROM_UNIX(date);
494 if (dateoff > AD_DATE_ACCESS) {
498 memcpy(p + dateoff, &date, sizeof(date));
505 * Map on-disk AppleDouble id to enumerated id
507 static uint32_t get_eid(uint32_t eid)
515 return ADEID_PRIVDEV;
517 return ADEID_PRIVINO;
519 return ADEID_PRIVSYN;
530 * Pack AppleDouble structure into data buffer
532 static bool ad_pack(struct adouble *ad)
539 bufsize = talloc_get_size(ad->ad_data);
541 if (offset + ADEDLEN_MAGIC < offset ||
542 offset + ADEDLEN_MAGIC >= bufsize) {
545 RSIVAL(ad->ad_data, offset, ad->ad_magic);
546 offset += ADEDLEN_MAGIC;
548 if (offset + ADEDLEN_VERSION < offset ||
549 offset + ADEDLEN_VERSION >= bufsize) {
552 RSIVAL(ad->ad_data, offset, ad->ad_version);
553 offset += ADEDLEN_VERSION;
555 if (offset + ADEDLEN_FILLER < offset ||
556 offset + ADEDLEN_FILLER >= bufsize) {
559 if (ad->ad_type == ADOUBLE_RSRC) {
560 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
562 offset += ADEDLEN_FILLER;
564 if (offset + ADEDLEN_NENTRIES < offset ||
565 offset + ADEDLEN_NENTRIES >= bufsize) {
568 offset += ADEDLEN_NENTRIES;
570 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
571 if (ad->ad_eid[eid].ade_off == 0) {
573 * ade_off is also used as indicator whether a
574 * specific entry is used or not
579 if (offset + AD_ENTRY_LEN_EID < offset ||
580 offset + AD_ENTRY_LEN_EID >= bufsize) {
583 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
584 offset += AD_ENTRY_LEN_EID;
586 if (offset + AD_ENTRY_LEN_OFF < offset ||
587 offset + AD_ENTRY_LEN_OFF >= bufsize) {
590 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
591 offset += AD_ENTRY_LEN_OFF;
593 if (offset + AD_ENTRY_LEN_LEN < offset ||
594 offset + AD_ENTRY_LEN_LEN >= bufsize) {
597 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
598 offset += AD_ENTRY_LEN_LEN;
603 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
606 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
612 * Unpack an AppleDouble blob into a struct adoble
614 static bool ad_unpack(struct adouble *ad, const size_t nentries,
617 size_t bufsize = talloc_get_size(ad->ad_data);
619 uint32_t eid, len, off;
622 * The size of the buffer ad->ad_data is checked when read, so
623 * we wouldn't have to check our own offsets, a few extra
624 * checks won't hurt though. We have to check the offsets we
625 * read from the buffer anyway.
628 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
629 DEBUG(1, ("bad size\n"));
633 ad->ad_magic = RIVAL(ad->ad_data, 0);
634 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
635 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
636 DEBUG(1, ("wrong magic or version\n"));
640 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
641 if (adentries != nentries) {
642 DEBUG(1, ("invalid number of entries: %zu\n",
647 /* now, read in the entry bits */
648 for (i = 0; i < adentries; i++) {
649 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
651 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
652 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
654 if (!eid || eid > ADEID_MAX) {
655 DEBUG(1, ("bogus eid %d\n", eid));
660 * All entries other than the resource fork are
661 * expected to be read into the ad_data buffer, so
662 * ensure the specified offset is within that bound
664 if ((off > bufsize) && (eid != ADEID_RFORK)) {
665 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
671 * All entries besides FinderInfo and resource fork
672 * must fit into the buffer. FinderInfo is special as
673 * it may be larger then the default 32 bytes (if it
674 * contains marshalled xattrs), but we will fixup that
675 * in ad_convert(). And the resource fork is never
676 * accessed directly by the ad_data buf (also see
677 * comment above) anyway.
679 if ((eid != ADEID_RFORK) &&
680 (eid != ADEID_FINDERI) &&
681 ((off + len) > bufsize)) {
682 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
688 * That would be obviously broken
690 if (off > filesize) {
691 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
697 * Check for any entry that has its end beyond the
700 if (off + len < off) {
701 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
702 ", len: %" PRIu32 "\n",
707 if (off + len > filesize) {
709 * If this is the resource fork entry, we fix
710 * up the length, for any other entry we bail
713 if (eid != ADEID_RFORK) {
714 DEBUG(1, ("bogus eid %d: off: %" PRIu32
715 ", len: %" PRIu32 "\n",
721 * Fixup the resource fork entry by limiting
722 * the size to entryoffset - filesize.
724 len = filesize - off;
725 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
726 ", len: %" PRIu32 "\n", off, len));
729 ad->ad_eid[eid].ade_off = off;
730 ad->ad_eid[eid].ade_len = len;
737 * Convert from Apple's ._ file to Netatalk
739 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
740 * bytes containing packed xattrs. Netatalk can't deal with that, so
741 * we simply discard the packed xattrs.
743 * @return -1 in case an error occurred, 0 if no conversion was done, 1
746 static int ad_convert(struct adouble *ad, int fd)
749 char *map = MAP_FAILED;
752 origlen = ad_getentryoff(ad, ADEID_RFORK) +
753 ad_getentrylen(ad, ADEID_RFORK);
755 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
756 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
757 if (map == MAP_FAILED) {
758 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
763 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
764 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
765 map + ad_getentryoff(ad, ADEID_RFORK),
766 ad_getentrylen(ad, ADEID_RFORK));
769 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
770 ad_setentryoff(ad, ADEID_RFORK,
771 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
774 * FIXME: direct ftruncate(), but we don't have a fsp for the
777 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
778 + ad_getentrylen(ad, ADEID_RFORK));
781 if (map != MAP_FAILED) {
782 munmap(map, origlen);
788 * Read and parse Netatalk AppleDouble metadata xattr
790 static ssize_t ad_read_meta(struct adouble *ad,
791 const struct smb_filename *smb_fname)
797 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
799 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname->base_name,
800 AFPINFO_EA_NETATALK, ad->ad_data,
806 if (errno == ENOATTR) {
812 DEBUG(2, ("error reading meta xattr: %s\n",
818 if (ealen != AD_DATASZ_XATTR) {
819 DEBUG(2, ("bad size %zd\n", ealen));
825 /* Now parse entries */
826 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
828 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
834 if (!ad_getentryoff(ad, ADEID_FINDERI)
835 || !ad_getentryoff(ad, ADEID_COMMENT)
836 || !ad_getentryoff(ad, ADEID_FILEDATESI)
837 || !ad_getentryoff(ad, ADEID_AFPFILEI)
838 || !ad_getentryoff(ad, ADEID_PRIVDEV)
839 || !ad_getentryoff(ad, ADEID_PRIVINO)
840 || !ad_getentryoff(ad, ADEID_PRIVSYN)
841 || !ad_getentryoff(ad, ADEID_PRIVID)) {
842 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
849 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
850 smb_fname->base_name, rc));
854 if (errno == EINVAL) {
856 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
864 static int ad_open_meta(const struct smb_filename *smb_fname,
868 return open(smb_fname->base_name, flags, mode);
871 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
876 /* FIXME: direct Solaris xattr syscall */
877 return attropen(smb_fname->base_name,
878 AFPRESOURCE_EA_NETATALK, flags, mode);
885 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
891 struct smb_filename *adp_smb_fname = NULL;
893 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
898 fd = open(adp_smb_fname->base_name, flags, mode);
899 TALLOC_FREE(adp_smb_fname);
904 static int ad_open_rsrc(vfs_handle_struct *handle,
905 const struct smb_filename *smb_fname,
909 struct fruit_config_data *config = NULL;
912 SMB_VFS_HANDLE_GET_DATA(handle, config,
913 struct fruit_config_data, return -1);
915 if (config->rsrc == FRUIT_RSRC_XATTR) {
916 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
918 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
924 static int ad_open(vfs_handle_struct *handle,
926 const struct smb_filename *smb_fname,
933 DBG_DEBUG("Path [%s] type [%s]\n",
934 smb_fname->base_name, t == ADOUBLE_META ? "meta" : "rsrc");
936 if (t == ADOUBLE_META) {
937 fd = ad_open_meta(smb_fname, flags, mode);
939 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
943 ad->ad_opened = true;
947 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
948 smb_fname->base_name,
949 t == ADOUBLE_META ? "meta" : "rsrc", fd);
954 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
959 /* FIXME: direct sys_fstat(), don't have an fsp */
960 ret = sys_fstat(ad->ad_fd, &st,
961 lp_fake_directory_create_times(
962 SNUM(ad->ad_handle->conn)));
967 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
968 return st.st_ex_size;
971 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
972 const struct smb_filename *smb_fname)
974 struct adouble *meta_ad = NULL;
975 SMB_STRUCT_STAT sbuf;
977 char *p_meta_ad = NULL;
982 len = sys_pread(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
983 if (len != AD_DATASZ_DOT_UND) {
984 DBG_NOTICE("%s %s: bad size: %zd\n",
985 smb_fname->base_name, strerror(errno), len);
989 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
990 SNUM(ad->ad_handle->conn)));
995 /* Now parse entries */
996 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
998 DBG_ERR("invalid AppleDouble resource %s\n",
999 smb_fname->base_name);
1004 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1005 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1006 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1007 DBG_ERR("invalid AppleDouble resource %s\n",
1008 smb_fname->base_name);
1013 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1018 * Try to fixup AppleDouble files created by OS X with xattrs
1019 * appended to the ADEID_FINDERI entry. We simply remove the
1020 * xattrs blob, this means any fancy xattr that was stored
1024 ret = ad_convert(ad, ad->ad_fd);
1026 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1032 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1036 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1037 if (len != AD_DATASZ_DOT_UND) {
1038 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1042 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1043 if (meta_ad == NULL) {
1047 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1049 TALLOC_FREE(meta_ad);
1052 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1053 if (p_meta_ad == NULL) {
1054 TALLOC_FREE(meta_ad);
1058 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1060 ret = ad_set(meta_ad, smb_fname);
1061 TALLOC_FREE(meta_ad);
1070 * Read and parse resource fork, either ._ AppleDouble file or xattr
1072 static ssize_t ad_read_rsrc(struct adouble *ad,
1073 const struct smb_filename *smb_fname)
1075 struct fruit_config_data *config = NULL;
1078 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1079 struct fruit_config_data, return -1);
1081 if (config->rsrc == FRUIT_RSRC_XATTR) {
1082 len = ad_read_rsrc_xattr(ad);
1084 len = ad_read_rsrc_adouble(ad, smb_fname);
1091 * Read and unpack an AppleDouble metadata xattr or resource
1093 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1095 switch (ad->ad_type) {
1097 return ad_read_meta(ad, smb_fname);
1099 return ad_read_rsrc(ad, smb_fname);
1105 static int adouble_destructor(struct adouble *ad)
1107 if ((ad->ad_fd != -1) && ad->ad_opened) {
1115 * Allocate a struct adouble without initialiing it
1117 * The struct is either hang of the fsp extension context or if fsp is
1120 * @param[in] ctx talloc context
1121 * @param[in] handle vfs handle
1122 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1124 * @return adouble handle
1126 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1127 adouble_type_t type)
1132 struct fruit_config_data *config;
1134 SMB_VFS_HANDLE_GET_DATA(handle, config,
1135 struct fruit_config_data, return NULL);
1139 adsize = AD_DATASZ_XATTR;
1142 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1143 adsize = AD_DATASZ_DOT_UND;
1150 ad = talloc_zero(ctx, struct adouble);
1157 ad->ad_data = talloc_zero_array(ad, char, adsize);
1158 if (ad->ad_data == NULL) {
1164 ad->ad_handle = handle;
1166 ad->ad_magic = AD_MAGIC;
1167 ad->ad_version = AD_VERSION;
1170 talloc_set_destructor(ad, adouble_destructor);
1180 * Allocate and initialize a new struct adouble
1182 * @param[in] ctx talloc context
1183 * @param[in] handle vfs handle
1184 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1186 * @return adouble handle, initialized
1188 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1189 adouble_type_t type)
1192 const struct ad_entry_order *eid;
1193 struct adouble *ad = NULL;
1194 struct fruit_config_data *config;
1195 time_t t = time(NULL);
1197 SMB_VFS_HANDLE_GET_DATA(handle, config,
1198 struct fruit_config_data, return NULL);
1202 eid = entry_order_meta_xattr;
1205 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1206 eid = entry_order_dot_und;
1208 eid = entry_order_rsrc_xattr;
1215 ad = ad_alloc(ctx, handle, type);
1221 ad->ad_eid[eid->id].ade_off = eid->offset;
1222 ad->ad_eid[eid->id].ade_len = eid->len;
1226 /* put something sane in the date fields */
1227 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1228 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1229 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1230 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1239 * Return AppleDouble data for a file
1241 * @param[in] ctx talloc context
1242 * @param[in] handle vfs handle
1243 * @param[in] smb_fname pathname to file or directory
1244 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1246 * @return talloced struct adouble or NULL on error
1248 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1249 const struct smb_filename *smb_fname,
1250 adouble_type_t type)
1254 struct adouble *ad = NULL;
1258 DEBUG(10, ("ad_get(%s) called for %s\n",
1259 type == ADOUBLE_META ? "meta" : "rsrc",
1260 smb_fname->base_name));
1262 ad = ad_alloc(ctx, handle, type);
1269 * Here's the deal: for ADOUBLE_META we can do without an fd
1270 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1271 * however we need a full-fledged fd for file IO on the ._
1274 if (type == ADOUBLE_RSRC) {
1275 /* Try rw first so we can use the fd in ad_convert() */
1278 fd = ad_open(handle, ad, smb_fname, ADOUBLE_RSRC, mode, 0);
1279 if (fd == -1 && ((errno == EROFS) || (errno == EACCES))) {
1281 fd = ad_open(handle, ad, smb_fname,
1282 ADOUBLE_RSRC, mode, 0);
1286 DBG_DEBUG("ad_open [%s] error [%s]\n",
1287 smb_fname->base_name, strerror(errno));
1293 len = ad_read(ad, smb_fname);
1295 DEBUG(10, ("error reading AppleDouble for %s\n",
1296 smb_fname->base_name));
1302 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1303 type == ADOUBLE_META ? "meta" : "rsrc",
1304 smb_fname->base_name, rc));
1313 * Return AppleDouble data for a file
1315 * @param[in] ctx talloc context
1316 * @param[in] handle vfs handle
1317 * @param[in] fsp fsp to use for IO
1318 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1320 * @return talloced struct adouble or NULL on error
1322 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1323 files_struct *fsp, adouble_type_t type)
1327 struct adouble *ad = NULL;
1329 DBG_DEBUG("ad_get(%s) path [%s]\n",
1330 type == ADOUBLE_META ? "meta" : "rsrc",
1333 ad = ad_alloc(ctx, handle, type);
1339 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1340 ad->ad_fd = fsp->fh->fd;
1343 * Here's the deal: for ADOUBLE_META we can do without an fd
1344 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1345 * however we need a full-fledged fd for file IO on the ._
1351 if (type == ADOUBLE_RSRC) {
1352 /* Try rw first so we can use the fd in ad_convert() */
1355 fd = ad_open(handle, ad, fsp->base_fsp->fsp_name,
1356 ADOUBLE_RSRC, mode, 0);
1358 ((errno == EROFS) || (errno == EACCES)))
1361 fd = ad_open(handle, ad,
1362 fsp->base_fsp->fsp_name, ADOUBLE_RSRC,
1367 DBG_DEBUG("error opening AppleDouble for %s\n",
1375 len = ad_read(ad, fsp->base_fsp->fsp_name);
1377 DBG_DEBUG("error reading AppleDouble for %s\n",
1384 DBG_DEBUG("ad_get(%s) path [%s] rc [%d]\n",
1385 type == ADOUBLE_META ? "meta" : "rsrc",
1386 fsp_str_dbg(fsp), rc);
1395 * Set AppleDouble metadata on a file or directory
1397 * @param[in] ad adouble handle
1399 * @param[in] smb_fname pathname to file or directory
1401 * @return status code, 0 means success
1403 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1408 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1410 if (ad->ad_type != ADOUBLE_META) {
1411 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1412 smb_fname->base_name);
1421 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1423 AFPINFO_EA_NETATALK,
1425 AD_DATASZ_XATTR, 0);
1427 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1433 * Set AppleDouble metadata on a file or directory
1435 * @param[in] ad adouble handle
1436 * @param[in] fsp file handle
1438 * @return status code, 0 means success
1440 static int ad_fset(struct adouble *ad, files_struct *fsp)
1446 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1449 || (fsp->fh == NULL)
1450 || (fsp->fh->fd == -1))
1452 smb_panic("bad fsp");
1460 switch (ad->ad_type) {
1462 rc = SMB_VFS_NEXT_FSETXATTR(ad->ad_handle,
1464 AFPINFO_EA_NETATALK,
1466 AD_DATASZ_XATTR, 0);
1470 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1473 talloc_get_size(ad->ad_data),
1475 if (len != (ssize_t)talloc_get_size(ad->ad_data)) {
1476 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1486 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1491 /*****************************************************************************
1493 *****************************************************************************/
1495 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1497 if (strncasecmp_m(smb_fname->stream_name,
1498 AFPINFO_STREAM_NAME,
1499 strlen(AFPINFO_STREAM_NAME)) == 0) {
1505 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1507 if (strncasecmp_m(smb_fname->stream_name,
1508 AFPRESOURCE_STREAM_NAME,
1509 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1516 * Test whether stream is an Apple stream, not used atm
1519 static bool is_apple_stream(const struct smb_filename *smb_fname)
1521 if (is_afpinfo_stream(smb_fname)) {
1524 if (is_afpresource_stream(smb_fname)) {
1532 * Initialize config struct from our smb.conf config parameters
1534 static int init_fruit_config(vfs_handle_struct *handle)
1536 struct fruit_config_data *config;
1539 config = talloc_zero(handle->conn, struct fruit_config_data);
1541 DEBUG(1, ("talloc_zero() failed\n"));
1547 * Versions up to Samba 4.5.x had a spelling bug in the
1548 * fruit:resource option calling lp_parm_enum with
1549 * "res*s*ource" (ie two s).
1551 * In Samba 4.6 we accept both the wrong and the correct
1552 * spelling, in Samba 4.7 the bad spelling will be removed.
1554 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1555 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1556 if (enumval == -1) {
1557 DEBUG(1, ("value for %s: resource type unknown\n",
1558 FRUIT_PARAM_TYPE_NAME));
1561 config->rsrc = (enum fruit_rsrc)enumval;
1563 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1564 "resource", fruit_rsrc, enumval);
1565 if (enumval == -1) {
1566 DEBUG(1, ("value for %s: resource type unknown\n",
1567 FRUIT_PARAM_TYPE_NAME));
1570 config->rsrc = (enum fruit_rsrc)enumval;
1572 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1573 "metadata", fruit_meta, FRUIT_META_NETATALK);
1574 if (enumval == -1) {
1575 DEBUG(1, ("value for %s: metadata type unknown\n",
1576 FRUIT_PARAM_TYPE_NAME));
1579 config->meta = (enum fruit_meta)enumval;
1581 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1582 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1583 if (enumval == -1) {
1584 DEBUG(1, ("value for %s: locking type unknown\n",
1585 FRUIT_PARAM_TYPE_NAME));
1588 config->locking = (enum fruit_locking)enumval;
1590 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1591 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1592 if (enumval == -1) {
1593 DEBUG(1, ("value for %s: encoding type unknown\n",
1594 FRUIT_PARAM_TYPE_NAME));
1597 config->encoding = (enum fruit_encoding)enumval;
1599 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1600 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1601 FRUIT_PARAM_TYPE_NAME,
1606 config->use_aapl = lp_parm_bool(
1607 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1609 config->unix_info_enabled = lp_parm_bool(
1610 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1612 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1615 config->posix_rename = lp_parm_bool(
1616 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1618 config->aapl_zero_file_id =
1619 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1621 config->readdir_attr_rsize = lp_parm_bool(
1622 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1624 config->readdir_attr_finder_info = lp_parm_bool(
1625 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1627 config->readdir_attr_max_access = lp_parm_bool(
1628 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1630 SMB_VFS_HANDLE_SET_DATA(handle, config,
1631 NULL, struct fruit_config_data,
1638 * Prepend "._" to a basename
1639 * Return a new struct smb_filename with stream_name == NULL.
1641 static int adouble_path(TALLOC_CTX *ctx,
1642 const struct smb_filename *smb_fname_in,
1643 struct smb_filename **pp_smb_fname_out)
1647 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1650 if (smb_fname == NULL) {
1654 /* We need streamname to be NULL */
1655 TALLOC_FREE(smb_fname->stream_name);
1657 /* And we're replacing base_name. */
1658 TALLOC_FREE(smb_fname->base_name);
1660 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
1662 TALLOC_FREE(smb_fname);
1666 smb_fname->base_name = talloc_asprintf(smb_fname,
1667 "%s/._%s", parent, base);
1668 if (smb_fname->base_name == NULL) {
1669 TALLOC_FREE(smb_fname);
1673 *pp_smb_fname_out = smb_fname;
1679 * Allocate and initialize an AfpInfo struct
1681 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1683 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1687 ai->afpi_Signature = AFP_Signature;
1688 ai->afpi_Version = AFP_Version;
1689 ai->afpi_BackupTime = AD_DATE_START;
1694 * Pack an AfpInfo struct into a buffer
1696 * Buffer size must be at least AFP_INFO_SIZE
1697 * Returns size of packed buffer
1699 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1701 memset(buf, 0, AFP_INFO_SIZE);
1703 RSIVAL(buf, 0, ai->afpi_Signature);
1704 RSIVAL(buf, 4, ai->afpi_Version);
1705 RSIVAL(buf, 12, ai->afpi_BackupTime);
1706 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1708 return AFP_INFO_SIZE;
1712 * Unpack a buffer into a AfpInfo structure
1714 * Buffer size must be at least AFP_INFO_SIZE
1715 * Returns allocated AfpInfo struct
1717 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1719 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1724 ai->afpi_Signature = RIVAL(data, 0);
1725 ai->afpi_Version = RIVAL(data, 4);
1726 ai->afpi_BackupTime = RIVAL(data, 12);
1727 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1728 sizeof(ai->afpi_FinderInfo));
1730 if (ai->afpi_Signature != AFP_Signature
1731 || ai->afpi_Version != AFP_Version) {
1732 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1740 * Fake an inode number from the md5 hash of the (xattr) name
1742 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1745 unsigned char hash[16];
1749 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1750 SMB_ASSERT(upper_sname != NULL);
1753 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1754 sizeof(sbuf->st_ex_dev));
1755 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1756 sizeof(sbuf->st_ex_ino));
1757 MD5Update(&ctx, (unsigned char *)upper_sname,
1758 talloc_get_size(upper_sname)-1);
1759 MD5Final(hash, &ctx);
1761 TALLOC_FREE(upper_sname);
1763 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1764 memcpy(&result, hash, sizeof(result));
1766 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1767 sname, (unsigned long long)result));
1772 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1773 struct stream_struct **streams,
1774 const char *name, off_t size,
1777 struct stream_struct *tmp;
1779 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1785 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1786 if (tmp[*num_streams].name == NULL) {
1790 tmp[*num_streams].size = size;
1791 tmp[*num_streams].alloc_size = alloc_size;
1798 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1799 struct stream_struct **streams)
1801 struct stream_struct *tmp = *streams;
1804 if (*num_streams == 0) {
1808 for (i = 0; i < *num_streams; i++) {
1809 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1814 if (i == *num_streams) {
1818 if (tmp[i].size > 0) {
1822 TALLOC_FREE(tmp[i].name);
1823 if (*num_streams - 1 > i) {
1824 memmove(&tmp[i], &tmp[i+1],
1825 (*num_streams - i - 1) * sizeof(struct stream_struct));
1832 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1833 struct stream_struct **streams,
1836 struct stream_struct *tmp = *streams;
1839 if (*num_streams == 0) {
1843 for (i = 0; i < *num_streams; i++) {
1844 if (strequal_m(tmp[i].name, name)) {
1849 if (i == *num_streams) {
1853 TALLOC_FREE(tmp[i].name);
1854 if (*num_streams - 1 > i) {
1855 memmove(&tmp[i], &tmp[i+1],
1856 (*num_streams - i - 1) * sizeof(struct stream_struct));
1863 static bool ad_empty_finderinfo(const struct adouble *ad)
1866 char emptybuf[ADEDLEN_FINDERI] = {0};
1869 fi = ad_get_entry(ad, ADEID_FINDERI);
1871 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1875 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1879 static bool ai_empty_finderinfo(const AfpInfo *ai)
1882 char emptybuf[ADEDLEN_FINDERI] = {0};
1884 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1889 * Update btime with btime from Netatalk
1891 static void update_btime(vfs_handle_struct *handle,
1892 struct smb_filename *smb_fname)
1895 struct timespec creation_time = {0};
1897 struct fruit_config_data *config = NULL;
1899 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1902 switch (config->meta) {
1903 case FRUIT_META_STREAM:
1905 case FRUIT_META_NETATALK:
1909 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1913 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1917 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1923 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1924 update_stat_ex_create_time(&smb_fname->st, creation_time);
1930 * Map an access mask to a Netatalk single byte byte range lock
1932 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1933 uint32_t access_mask)
1937 switch (access_mask) {
1938 case FILE_READ_DATA:
1939 offset = AD_FILELOCK_OPEN_RD;
1942 case FILE_WRITE_DATA:
1943 case FILE_APPEND_DATA:
1944 offset = AD_FILELOCK_OPEN_WR;
1948 offset = AD_FILELOCK_OPEN_NONE;
1952 if (fork_type == APPLE_FORK_RSRC) {
1953 if (offset == AD_FILELOCK_OPEN_NONE) {
1954 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1964 * Map a deny mode to a Netatalk brl
1966 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1971 switch (deny_mode) {
1973 offset = AD_FILELOCK_DENY_RD;
1977 offset = AD_FILELOCK_DENY_WR;
1981 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1984 if (fork_type == APPLE_FORK_RSRC) {
1992 * Call fcntl() with an exclusive F_GETLK request in order to
1993 * determine if there's an exisiting shared lock
1995 * @return true if the requested lock was found or any error occurred
1996 * false if the lock was not found
1998 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2001 off_t offset = in_offset;
2006 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2007 if (result == false) {
2011 if (type != F_UNLCK) {
2018 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2020 uint32_t access_mask,
2023 NTSTATUS status = NT_STATUS_OK;
2024 struct byte_range_lock *br_lck = NULL;
2025 bool open_for_reading, open_for_writing, deny_read, deny_write;
2027 bool have_read = false;
2030 /* FIXME: hardcoded data fork, add resource fork */
2031 enum apple_fork fork_type = APPLE_FORK_DATA;
2033 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2035 access_mask & FILE_READ_DATA ? "READ" :"-",
2036 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2037 deny_mode & DENY_READ ? "DENY_READ" : "-",
2038 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2040 if (fsp->fh->fd == -1) {
2041 return NT_STATUS_OK;
2044 flags = fcntl(fsp->fh->fd, F_GETFL);
2046 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2047 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2048 return map_nt_error_from_unix(errno);
2051 if (flags & (O_RDONLY|O_RDWR)) {
2053 * Applying fcntl read locks requires an fd opened for
2054 * reading. This means we won't be applying locks for
2055 * files openend write-only, but what can we do...
2061 * Check read access and deny read mode
2063 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2065 open_for_reading = test_netatalk_lock(
2066 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2068 deny_read = test_netatalk_lock(
2069 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2071 DEBUG(10, ("read: %s, deny_write: %s\n",
2072 open_for_reading == true ? "yes" : "no",
2073 deny_read == true ? "yes" : "no"));
2075 if (((access_mask & FILE_READ_DATA) && deny_read)
2076 || ((deny_mode & DENY_READ) && open_for_reading)) {
2077 return NT_STATUS_SHARING_VIOLATION;
2081 if ((access_mask & FILE_READ_DATA) && have_read) {
2082 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2084 handle->conn->sconn->msg_ctx, fsp,
2085 fsp->op->global->open_persistent_id, 1, off,
2086 READ_LOCK, POSIX_LOCK, false,
2089 if (!NT_STATUS_IS_OK(status)) {
2092 TALLOC_FREE(br_lck);
2095 if ((deny_mode & DENY_READ) && have_read) {
2096 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2098 handle->conn->sconn->msg_ctx, fsp,
2099 fsp->op->global->open_persistent_id, 1, off,
2100 READ_LOCK, POSIX_LOCK, false,
2103 if (!NT_STATUS_IS_OK(status)) {
2106 TALLOC_FREE(br_lck);
2111 * Check write access and deny write mode
2113 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2115 open_for_writing = test_netatalk_lock(
2116 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2118 deny_write = test_netatalk_lock(
2119 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2121 DEBUG(10, ("write: %s, deny_write: %s\n",
2122 open_for_writing == true ? "yes" : "no",
2123 deny_write == true ? "yes" : "no"));
2125 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2126 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2127 return NT_STATUS_SHARING_VIOLATION;
2131 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2132 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2134 handle->conn->sconn->msg_ctx, fsp,
2135 fsp->op->global->open_persistent_id, 1, off,
2136 READ_LOCK, POSIX_LOCK, false,
2139 if (!NT_STATUS_IS_OK(status)) {
2142 TALLOC_FREE(br_lck);
2145 if ((deny_mode & DENY_WRITE) && have_read) {
2146 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2148 handle->conn->sconn->msg_ctx, fsp,
2149 fsp->op->global->open_persistent_id, 1, off,
2150 READ_LOCK, POSIX_LOCK, false,
2153 if (!NT_STATUS_IS_OK(status)) {
2156 TALLOC_FREE(br_lck);
2160 TALLOC_FREE(br_lck);
2165 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2166 struct smb_request *req,
2167 const struct smb2_create_blobs *in_context_blobs,
2168 struct smb2_create_blobs *out_context_blobs)
2170 struct fruit_config_data *config;
2172 struct smb2_create_blob *aapl = NULL;
2176 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2177 uint64_t req_bitmap, client_caps;
2178 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2182 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2183 return NT_STATUS_UNSUCCESSFUL);
2185 if (!config->use_aapl
2186 || in_context_blobs == NULL
2187 || out_context_blobs == NULL) {
2188 return NT_STATUS_OK;
2191 aapl = smb2_create_blob_find(in_context_blobs,
2192 SMB2_CREATE_TAG_AAPL);
2194 return NT_STATUS_OK;
2197 if (aapl->data.length != 24) {
2198 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2199 (uintmax_t)aapl->data.length));
2200 return NT_STATUS_INVALID_PARAMETER;
2203 cmd = IVAL(aapl->data.data, 0);
2204 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2205 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2206 return NT_STATUS_INVALID_PARAMETER;
2209 req_bitmap = BVAL(aapl->data.data, 8);
2210 client_caps = BVAL(aapl->data.data, 16);
2212 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2214 SBVAL(p, 8, req_bitmap);
2215 ok = data_blob_append(req, &blob, p, 16);
2217 return NT_STATUS_UNSUCCESSFUL;
2220 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2221 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2222 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2223 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2224 config->readdir_attr_enabled = true;
2227 if (config->use_copyfile) {
2228 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2229 config->copyfile_enabled = true;
2233 * The client doesn't set the flag, so we can't check
2234 * for it and just set it unconditionally
2236 if (config->unix_info_enabled) {
2237 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2240 SBVAL(p, 0, server_caps);
2241 ok = data_blob_append(req, &blob, p, 8);
2243 return NT_STATUS_UNSUCCESSFUL;
2247 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2248 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2256 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2265 ok = data_blob_append(req, &blob, p, 8);
2267 return NT_STATUS_UNSUCCESSFUL;
2271 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2272 ok = convert_string_talloc(req,
2273 CH_UNIX, CH_UTF16LE,
2274 "Samba", strlen("Samba"),
2277 return NT_STATUS_UNSUCCESSFUL;
2281 SIVAL(p + 4, 0, modellen);
2282 ok = data_blob_append(req, &blob, p, 8);
2285 return NT_STATUS_UNSUCCESSFUL;
2288 ok = data_blob_append(req, &blob, model, modellen);
2291 return NT_STATUS_UNSUCCESSFUL;
2295 status = smb2_create_blob_add(out_context_blobs,
2297 SMB2_CREATE_TAG_AAPL,
2299 if (NT_STATUS_IS_OK(status)) {
2300 global_fruit_config.nego_aapl = true;
2301 if (config->aapl_zero_file_id) {
2302 aapl_force_zero_file_id(handle->conn->sconn);
2309 static bool readdir_attr_meta_finderi_stream(
2310 struct vfs_handle_struct *handle,
2311 const struct smb_filename *smb_fname,
2314 struct smb_filename *stream_name = NULL;
2315 files_struct *fsp = NULL;
2320 uint8_t buf[AFP_INFO_SIZE];
2322 stream_name = synthetic_smb_fname(talloc_tos(),
2323 smb_fname->base_name,
2324 AFPINFO_STREAM_NAME,
2325 NULL, smb_fname->flags);
2326 if (stream_name == NULL) {
2330 ret = SMB_VFS_STAT(handle->conn, stream_name);
2335 status = SMB_VFS_CREATE_FILE(
2336 handle->conn, /* conn */
2338 0, /* root_dir_fid */
2339 stream_name, /* fname */
2340 FILE_READ_DATA, /* access_mask */
2341 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2343 FILE_OPEN, /* create_disposition*/
2344 0, /* create_options */
2345 0, /* file_attributes */
2346 INTERNAL_OPEN_ONLY, /* oplock_request */
2348 0, /* allocation_size */
2349 0, /* private_flags */
2354 NULL, NULL); /* create context */
2356 TALLOC_FREE(stream_name);
2358 if (!NT_STATUS_IS_OK(status)) {
2362 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2363 if (nread != AFP_INFO_SIZE) {
2364 DBG_ERR("short read [%s] [%zd/%d]\n",
2365 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2370 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2377 close_file(NULL, fsp, NORMAL_CLOSE);
2383 static bool readdir_attr_meta_finderi_netatalk(
2384 struct vfs_handle_struct *handle,
2385 const struct smb_filename *smb_fname,
2388 struct adouble *ad = NULL;
2391 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2396 p = ad_get_entry(ad, ADEID_FINDERI);
2398 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2403 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2408 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2409 const struct smb_filename *smb_fname,
2410 struct readdir_attr_data *attr_data)
2412 struct fruit_config_data *config = NULL;
2413 uint32_t date_added;
2417 SMB_VFS_HANDLE_GET_DATA(handle, config,
2418 struct fruit_config_data,
2421 switch (config->meta) {
2422 case FRUIT_META_NETATALK:
2423 ok = readdir_attr_meta_finderi_netatalk(
2424 handle, smb_fname, &ai);
2427 case FRUIT_META_STREAM:
2428 ok = readdir_attr_meta_finderi_stream(
2429 handle, smb_fname, &ai);
2433 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2438 /* Don't bother with errors, it's likely ENOENT */
2442 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2444 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2445 &ai.afpi_FinderInfo[0], 4);
2447 /* finder_creator */
2448 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2449 &ai.afpi_FinderInfo[4], 4);
2453 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2454 &ai.afpi_FinderInfo[8], 2);
2456 /* finder_ext_flags */
2457 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2458 &ai.afpi_FinderInfo[24], 2);
2461 date_added = convert_time_t_to_uint32_t(
2462 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2464 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2469 static uint64_t readdir_attr_rfork_size_adouble(
2470 struct vfs_handle_struct *handle,
2471 const struct smb_filename *smb_fname)
2473 struct adouble *ad = NULL;
2474 uint64_t rfork_size;
2476 ad = ad_get(talloc_tos(), handle, smb_fname,
2482 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2488 static uint64_t readdir_attr_rfork_size_stream(
2489 struct vfs_handle_struct *handle,
2490 const struct smb_filename *smb_fname)
2492 struct smb_filename *stream_name = NULL;
2494 uint64_t rfork_size;
2496 stream_name = synthetic_smb_fname(talloc_tos(),
2497 smb_fname->base_name,
2498 AFPRESOURCE_STREAM_NAME,
2500 if (stream_name == NULL) {
2504 ret = SMB_VFS_STAT(handle->conn, stream_name);
2506 TALLOC_FREE(stream_name);
2510 rfork_size = stream_name->st.st_ex_size;
2511 TALLOC_FREE(stream_name);
2516 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2517 const struct smb_filename *smb_fname)
2519 struct fruit_config_data *config = NULL;
2520 uint64_t rfork_size;
2522 SMB_VFS_HANDLE_GET_DATA(handle, config,
2523 struct fruit_config_data,
2526 switch (config->rsrc) {
2527 case FRUIT_RSRC_ADFILE:
2528 case FRUIT_RSRC_XATTR:
2529 rfork_size = readdir_attr_rfork_size_adouble(handle,
2533 case FRUIT_META_STREAM:
2534 rfork_size = readdir_attr_rfork_size_stream(handle,
2539 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2547 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2548 const struct smb_filename *smb_fname,
2549 struct readdir_attr_data *attr_data)
2551 NTSTATUS status = NT_STATUS_OK;
2552 struct fruit_config_data *config = NULL;
2555 SMB_VFS_HANDLE_GET_DATA(handle, config,
2556 struct fruit_config_data,
2557 return NT_STATUS_UNSUCCESSFUL);
2560 /* Ensure we return a default value in the creation_date field */
2561 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2564 * Resource fork length
2567 if (config->readdir_attr_rsize) {
2568 uint64_t rfork_size;
2570 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2571 attr_data->attr_data.aapl.rfork_size = rfork_size;
2578 if (config->readdir_attr_finder_info) {
2579 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2581 status = NT_STATUS_INTERNAL_ERROR;
2588 /* Search MS NFS style ACE with UNIX mode */
2589 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2591 const struct security_descriptor *psd,
2596 struct fruit_config_data *config = NULL;
2600 SMB_VFS_HANDLE_GET_DATA(handle, config,
2601 struct fruit_config_data,
2602 return NT_STATUS_UNSUCCESSFUL);
2604 if (psd->dacl == NULL || !config->unix_info_enabled) {
2605 return NT_STATUS_OK;
2608 for (i = 0; i < psd->dacl->num_aces; i++) {
2609 if (dom_sid_compare_domain(
2610 &global_sid_Unix_NFS_Mode,
2611 &psd->dacl->aces[i].trustee) == 0) {
2612 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2613 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2616 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2617 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2622 return NT_STATUS_OK;
2625 /****************************************************************************
2627 ****************************************************************************/
2629 static int fruit_connect(vfs_handle_struct *handle,
2630 const char *service,
2634 char *list = NULL, *newlist = NULL;
2635 struct fruit_config_data *config;
2637 DEBUG(10, ("fruit_connect\n"));
2639 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2644 rc = init_fruit_config(handle);
2649 SMB_VFS_HANDLE_GET_DATA(handle, config,
2650 struct fruit_config_data, return -1);
2652 if (config->veto_appledouble) {
2653 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2656 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2657 newlist = talloc_asprintf(
2659 "%s/" ADOUBLE_NAME_PREFIX "*/",
2661 lp_do_parameter(SNUM(handle->conn),
2666 lp_do_parameter(SNUM(handle->conn),
2668 "/" ADOUBLE_NAME_PREFIX "*/");
2674 if (config->encoding == FRUIT_ENC_NATIVE) {
2678 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2679 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2680 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2681 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2682 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2683 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2684 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2685 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2686 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2687 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2694 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2695 struct smb_filename *smb_fname,
2701 char afpinfo_buf[AFP_INFO_SIZE];
2702 ssize_t len, written;
2706 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2711 if (!(flags & (O_CREAT | O_TRUNC))) {
2715 ai = afpinfo_new(talloc_tos());
2721 len = afpinfo_pack(ai, afpinfo_buf);
2722 if (len != AFP_INFO_SIZE) {
2727 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2728 fsp->fh->fd = hostfd;
2730 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2733 if (written != AFP_INFO_SIZE) {
2734 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2742 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2745 int saved_errno = errno;
2747 fsp->fh->fd = hostfd;
2748 SMB_VFS_NEXT_CLOSE(handle, fsp);
2751 errno = saved_errno;
2756 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2757 struct smb_filename *smb_fname,
2763 struct smb_filename *smb_fname_base = NULL;
2766 struct adouble *ad = NULL;
2768 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2770 /* Create an smb_filename with stream_name == NULL. */
2771 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2772 smb_fname->base_name,
2777 if (smb_fname_base == NULL) {
2784 * We use baseflags to turn off nasty side-effects when opening the
2788 baseflags &= ~O_TRUNC;
2789 baseflags &= ~O_EXCL;
2790 baseflags &= ~O_CREAT;
2792 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2796 * It is legit to open a stream on a directory, but the base
2797 * fd has to be read-only.
2799 if ((hostfd == -1) && (errno == EISDIR)) {
2800 baseflags &= ~O_ACCMODE;
2801 baseflags |= O_RDONLY;
2802 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2806 TALLOC_FREE(smb_fname_base);
2813 if (flags & (O_CREAT | O_TRUNC)) {
2815 * The attribute does not exist or needs to be truncated,
2816 * create an AppleDouble EA
2818 ad = ad_init(fsp, handle, ADOUBLE_META);
2824 fsp->fh->fd = hostfd;
2826 rc = ad_fset(ad, fsp);
2837 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2839 int saved_errno = errno;
2842 * BUGBUGBUG -- we would need to call
2843 * fd_close_posix here, but we don't have a
2846 fsp->fh->fd = hostfd;
2847 SMB_VFS_NEXT_CLOSE(handle, fsp);
2850 errno = saved_errno;
2855 static int fruit_open_meta(vfs_handle_struct *handle,
2856 struct smb_filename *smb_fname,
2857 files_struct *fsp, int flags, mode_t mode)
2860 struct fruit_config_data *config = NULL;
2861 struct fio *fio = NULL;
2863 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2865 SMB_VFS_HANDLE_GET_DATA(handle, config,
2866 struct fruit_config_data, return -1);
2868 switch (config->meta) {
2869 case FRUIT_META_STREAM:
2870 fd = fruit_open_meta_stream(handle, smb_fname,
2874 case FRUIT_META_NETATALK:
2875 fd = fruit_open_meta_netatalk(handle, smb_fname,
2880 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2884 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2890 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
2891 fio->type = ADOUBLE_META;
2892 fio->config = config;
2897 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2898 struct smb_filename *smb_fname,
2904 struct adouble *ad = NULL;
2905 struct smb_filename *smb_fname_base = NULL;
2906 struct fruit_config_data *config = NULL;
2909 SMB_VFS_HANDLE_GET_DATA(handle, config,
2910 struct fruit_config_data, return -1);
2912 if ((!(flags & O_CREAT)) &&
2913 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
2915 /* sorry, but directories don't habe a resource fork */
2920 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
2925 /* Sanitize flags */
2926 if (flags & O_WRONLY) {
2927 /* We always need read access for the metadata header too */
2932 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2939 if (flags & (O_CREAT | O_TRUNC)) {
2940 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
2946 fsp->fh->fd = hostfd;
2948 rc = ad_fset(ad, fsp);
2959 TALLOC_FREE(smb_fname_base);
2961 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2963 int saved_errno = errno;
2966 * BUGBUGBUG -- we would need to call
2967 * fd_close_posix here, but we don't have a
2970 fsp->fh->fd = hostfd;
2974 errno = saved_errno;
2979 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2980 struct smb_filename *smb_fname,
2985 #ifdef HAVE_ATTROPEN
2988 fd = attropen(smb_fname->base_name,
2989 AFPRESOURCE_EA_NETATALK,
3004 static int fruit_open_rsrc(vfs_handle_struct *handle,
3005 struct smb_filename *smb_fname,
3006 files_struct *fsp, int flags, mode_t mode)
3009 struct fruit_config_data *config = NULL;
3010 struct fio *fio = NULL;
3012 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3014 SMB_VFS_HANDLE_GET_DATA(handle, config,
3015 struct fruit_config_data, return -1);
3017 if (((flags & O_ACCMODE) == O_RDONLY)
3018 && (flags & O_CREAT)
3019 && !VALID_STAT(fsp->fsp_name->st))
3022 * This means the stream doesn't exist. macOS SMB server fails
3023 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3024 * 12565 and the test for this combination in
3025 * test_rfork_create().
3031 switch (config->rsrc) {
3032 case FRUIT_RSRC_STREAM:
3033 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3036 case FRUIT_RSRC_ADFILE:
3037 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3041 case FRUIT_RSRC_XATTR:
3042 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3047 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3051 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3057 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3058 fio->type = ADOUBLE_RSRC;
3059 fio->config = config;
3064 static int fruit_open(vfs_handle_struct *handle,
3065 struct smb_filename *smb_fname,
3066 files_struct *fsp, int flags, mode_t mode)
3070 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3072 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3073 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3076 if (is_afpinfo_stream(smb_fname)) {
3077 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3078 } else if (is_afpresource_stream(smb_fname)) {
3079 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3081 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3084 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3089 static int fruit_rename(struct vfs_handle_struct *handle,
3090 const struct smb_filename *smb_fname_src,
3091 const struct smb_filename *smb_fname_dst)
3094 struct fruit_config_data *config = NULL;
3095 struct smb_filename *src_adp_smb_fname = NULL;
3096 struct smb_filename *dst_adp_smb_fname = NULL;
3098 SMB_VFS_HANDLE_GET_DATA(handle, config,
3099 struct fruit_config_data, return -1);
3101 if (!VALID_STAT(smb_fname_src->st)) {
3102 DBG_ERR("Need valid stat for [%s]\n",
3103 smb_fname_str_dbg(smb_fname_src));
3107 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3112 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3113 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3118 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3123 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3128 DBG_DEBUG("%s -> %s\n",
3129 smb_fname_str_dbg(src_adp_smb_fname),
3130 smb_fname_str_dbg(dst_adp_smb_fname));
3132 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3133 if (errno == ENOENT) {
3138 TALLOC_FREE(src_adp_smb_fname);
3139 TALLOC_FREE(dst_adp_smb_fname);
3143 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3144 const struct smb_filename *smb_fname)
3146 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3149 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3150 const struct smb_filename *smb_fname)
3152 return SMB_VFS_REMOVEXATTR(handle->conn,
3154 AFPINFO_EA_NETATALK);
3157 static int fruit_unlink_meta(vfs_handle_struct *handle,
3158 const struct smb_filename *smb_fname)
3160 struct fruit_config_data *config = NULL;
3163 SMB_VFS_HANDLE_GET_DATA(handle, config,
3164 struct fruit_config_data, return -1);
3166 switch (config->meta) {
3167 case FRUIT_META_STREAM:
3168 rc = fruit_unlink_meta_stream(handle, smb_fname);
3171 case FRUIT_META_NETATALK:
3172 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3176 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3183 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3184 const struct smb_filename *smb_fname,
3189 if (!force_unlink) {
3190 struct smb_filename *smb_fname_cp = NULL;
3193 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3194 if (smb_fname_cp == NULL) {
3199 * 0 byte resource fork streams are not listed by
3200 * vfs_streaminfo, as a result stream cleanup/deletion of file
3201 * deletion doesn't remove the resourcefork stream.
3204 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3206 TALLOC_FREE(smb_fname_cp);
3207 DBG_ERR("stat [%s] failed [%s]\n",
3208 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3212 size = smb_fname_cp->st.st_ex_size;
3213 TALLOC_FREE(smb_fname_cp);
3216 /* OS X ignores resource fork stream delete requests */
3221 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3222 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3229 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3230 const struct smb_filename *smb_fname,
3234 struct adouble *ad = NULL;
3235 struct smb_filename *adp_smb_fname = NULL;
3237 if (!force_unlink) {
3238 ad = ad_get(talloc_tos(), handle, smb_fname,
3247 * 0 byte resource fork streams are not listed by
3248 * vfs_streaminfo, as a result stream cleanup/deletion of file
3249 * deletion doesn't remove the resourcefork stream.
3252 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3253 /* OS X ignores resource fork stream delete requests */
3261 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3266 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3267 TALLOC_FREE(adp_smb_fname);
3268 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3275 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3276 const struct smb_filename *smb_fname,
3280 * OS X ignores resource fork stream delete requests, so nothing to do
3281 * here. Removing the file will remove the xattr anyway, so we don't
3282 * have to take care of removing 0 byte resource forks that could be
3288 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3289 const struct smb_filename *smb_fname,
3292 struct fruit_config_data *config = NULL;
3295 SMB_VFS_HANDLE_GET_DATA(handle, config,
3296 struct fruit_config_data, return -1);
3298 switch (config->rsrc) {
3299 case FRUIT_RSRC_STREAM:
3300 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3303 case FRUIT_RSRC_ADFILE:
3304 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3307 case FRUIT_RSRC_XATTR:
3308 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3312 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3319 static int fruit_unlink(vfs_handle_struct *handle,
3320 const struct smb_filename *smb_fname)
3323 struct fruit_config_data *config = NULL;
3324 struct smb_filename *rsrc_smb_fname = NULL;
3326 SMB_VFS_HANDLE_GET_DATA(handle, config,
3327 struct fruit_config_data, return -1);
3329 if (is_afpinfo_stream(smb_fname)) {
3330 return fruit_unlink_meta(handle, smb_fname);
3331 } else if (is_afpresource_stream(smb_fname)) {
3332 return fruit_unlink_rsrc(handle, smb_fname, false);
3333 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3334 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3338 * A request to delete the base file. Because 0 byte resource
3339 * fork streams are not listed by fruit_streaminfo,
3340 * delete_all_streams() can't remove 0 byte resource fork
3341 * streams, so we have to cleanup this here.
3343 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3344 smb_fname->base_name,
3345 AFPRESOURCE_STREAM_NAME,
3348 if (rsrc_smb_fname == NULL) {
3352 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3353 if ((rc != 0) && (errno != ENOENT)) {
3354 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3355 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3356 TALLOC_FREE(rsrc_smb_fname);
3359 TALLOC_FREE(rsrc_smb_fname);
3361 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3364 static int fruit_chmod(vfs_handle_struct *handle,
3365 const struct smb_filename *smb_fname,
3369 struct fruit_config_data *config = NULL;
3370 struct smb_filename *smb_fname_adp = NULL;
3372 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3377 SMB_VFS_HANDLE_GET_DATA(handle, config,
3378 struct fruit_config_data, return -1);
3380 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3384 if (!VALID_STAT(smb_fname->st)) {
3388 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3392 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3397 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3399 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3400 if (errno == ENOENT) {
3404 TALLOC_FREE(smb_fname_adp);
3408 static int fruit_chown(vfs_handle_struct *handle,
3409 const struct smb_filename *smb_fname,
3414 struct fruit_config_data *config = NULL;
3415 struct smb_filename *adp_smb_fname = NULL;
3417 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3422 SMB_VFS_HANDLE_GET_DATA(handle, config,
3423 struct fruit_config_data, return -1);
3425 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3429 if (!VALID_STAT(smb_fname->st)) {
3433 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3437 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3442 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3444 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3445 if (errno == ENOENT) {
3450 TALLOC_FREE(adp_smb_fname);
3454 static int fruit_rmdir(struct vfs_handle_struct *handle,
3455 const struct smb_filename *smb_fname)
3459 struct fruit_config_data *config;
3461 SMB_VFS_HANDLE_GET_DATA(handle, config,
3462 struct fruit_config_data, return -1);
3464 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3469 * Due to there is no way to change bDeleteVetoFiles variable
3470 * from this module, need to clean up ourselves
3473 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3478 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3480 struct adouble *ad = NULL;
3482 struct smb_filename *ad_smb_fname = NULL;
3485 match = strncmp(de->d_name,
3486 ADOUBLE_NAME_PREFIX,
3487 strlen(ADOUBLE_NAME_PREFIX));
3492 p = talloc_asprintf(talloc_tos(), "%s/%s",
3493 smb_fname->base_name, de->d_name);
3495 DBG_ERR("talloc_asprintf failed\n");
3499 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3503 if (ad_smb_fname == NULL) {
3504 DBG_ERR("synthetic_smb_fname failed\n");
3509 * Check whether it's a valid AppleDouble file, if
3510 * yes, delete it, ignore it otherwise.
3512 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3514 TALLOC_FREE(ad_smb_fname);
3520 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3521 TALLOC_FREE(ad_smb_fname);
3523 DBG_ERR("Deleting [%s] failed\n",
3524 smb_fname_str_dbg(ad_smb_fname));
3532 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3535 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3536 files_struct *fsp, void *data,
3537 size_t n, off_t offset)
3542 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3548 DBG_ERR("Removing [%s] after short read [%zd]\n",
3549 fsp_str_dbg(fsp), nread);
3551 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3553 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3561 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3562 files_struct *fsp, void *data,
3563 size_t n, off_t offset)
3566 struct adouble *ad = NULL;
3567 char afpinfo_buf[AFP_INFO_SIZE];
3571 ai = afpinfo_new(talloc_tos());
3576 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3582 p = ad_get_entry(ad, ADEID_FINDERI);
3584 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3589 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3591 nread = afpinfo_pack(ai, afpinfo_buf);
3592 if (nread != AFP_INFO_SIZE) {
3597 memcpy(data, afpinfo_buf, n);
3605 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3606 files_struct *fsp, void *data,
3607 size_t n, off_t offset)
3609 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3614 * OS X has a off-by-1 error in the offset calculation, so we're
3615 * bug compatible here. It won't hurt, as any relevant real
3616 * world read requests from the AFP_AfpInfo stream will be
3617 * offset=0 n=60. offset is ignored anyway, see below.
3619 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3623 /* Yes, macOS always reads from offset 0 */
3625 to_return = MIN(n, AFP_INFO_SIZE);
3627 switch (fio->config->meta) {
3628 case FRUIT_META_STREAM:
3629 nread = fruit_pread_meta_stream(handle, fsp, data,
3633 case FRUIT_META_NETATALK:
3634 nread = fruit_pread_meta_adouble(handle, fsp, data,
3639 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3646 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3647 files_struct *fsp, void *data,
3648 size_t n, off_t offset)
3650 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3653 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3654 files_struct *fsp, void *data,
3655 size_t n, off_t offset)
3657 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3660 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3661 files_struct *fsp, void *data,
3662 size_t n, off_t offset)
3664 struct adouble *ad = NULL;
3667 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3672 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3673 offset + ad_getentryoff(ad, ADEID_RFORK));
3679 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3680 files_struct *fsp, void *data,
3681 size_t n, off_t offset)
3683 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3686 switch (fio->config->rsrc) {
3687 case FRUIT_RSRC_STREAM:
3688 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3691 case FRUIT_RSRC_ADFILE:
3692 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3695 case FRUIT_RSRC_XATTR:
3696 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3700 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3707 static ssize_t fruit_pread(vfs_handle_struct *handle,
3708 files_struct *fsp, void *data,
3709 size_t n, off_t offset)
3711 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3714 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3715 fsp_str_dbg(fsp), (intmax_t)offset, n);
3718 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3721 if (fio->type == ADOUBLE_META) {
3722 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3724 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3727 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3731 static bool fruit_must_handle_aio_stream(struct fio *fio)
3737 if ((fio->type == ADOUBLE_META) &&
3738 (fio->config->meta == FRUIT_META_NETATALK))
3743 if ((fio->type == ADOUBLE_RSRC) &&
3744 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
3752 struct fruit_pread_state {
3754 struct vfs_aio_state vfs_aio_state;
3757 static void fruit_pread_done(struct tevent_req *subreq);
3759 static struct tevent_req *fruit_pread_send(
3760 struct vfs_handle_struct *handle,
3761 TALLOC_CTX *mem_ctx,
3762 struct tevent_context *ev,
3763 struct files_struct *fsp,
3765 size_t n, off_t offset)
3767 struct tevent_req *req = NULL;
3768 struct tevent_req *subreq = NULL;
3769 struct fruit_pread_state *state = NULL;
3770 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3772 req = tevent_req_create(mem_ctx, &state,
3773 struct fruit_pread_state);
3778 if (fruit_must_handle_aio_stream(fio)) {
3779 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
3780 if (state->nread != n) {
3781 if (state->nread != -1) {
3784 tevent_req_error(req, errno);
3785 return tevent_req_post(req, ev);
3787 tevent_req_done(req);
3788 return tevent_req_post(req, ev);
3791 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
3793 if (tevent_req_nomem(req, subreq)) {
3794 return tevent_req_post(req, ev);
3796 tevent_req_set_callback(subreq, fruit_pread_done, req);
3800 static void fruit_pread_done(struct tevent_req *subreq)
3802 struct tevent_req *req = tevent_req_callback_data(
3803 subreq, struct tevent_req);
3804 struct fruit_pread_state *state = tevent_req_data(
3805 req, struct fruit_pread_state);
3807 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
3808 TALLOC_FREE(subreq);
3810 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3813 tevent_req_done(req);
3816 static ssize_t fruit_pread_recv(struct tevent_req *req,
3817 struct vfs_aio_state *vfs_aio_state)
3819 struct fruit_pread_state *state = tevent_req_data(
3820 req, struct fruit_pread_state);
3822 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3826 *vfs_aio_state = state->vfs_aio_state;
3827 return state->nread;
3830 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
3831 files_struct *fsp, const void *data,
3832 size_t n, off_t offset)
3837 ai = afpinfo_unpack(talloc_tos(), data);
3842 if (ai_empty_finderinfo(ai)) {
3843 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3844 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3845 DBG_ERR("Can't delete metadata for %s: %s\n",
3846 fsp_str_dbg(fsp), strerror(errno));
3854 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3857 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
3858 files_struct *fsp, const void *data,
3859 size_t n, off_t offset)
3861 struct adouble *ad = NULL;
3866 ai = afpinfo_unpack(talloc_tos(), data);
3871 if (ai_empty_finderinfo(ai)) {
3872 ret = SMB_VFS_REMOVEXATTR(handle->conn,
3874 AFPINFO_EA_NETATALK);
3876 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3877 DBG_ERR("Can't delete metadata for %s: %s\n",
3878 fsp_str_dbg(fsp), strerror(errno));
3885 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3887 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
3892 p = ad_get_entry(ad, ADEID_FINDERI);
3894 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3899 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3901 ret = ad_fset(ad, fsp);
3903 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3912 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
3913 files_struct *fsp, const void *data,
3914 size_t n, off_t offset)
3916 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3920 * Writing an all 0 blob to the metadata stream
3921 * results in the stream being removed on a macOS
3922 * server. This ensures we behave the same and it
3923 * verified by the "delete AFP_AfpInfo by writing all
3926 if (n != AFP_INFO_SIZE || offset != 0) {
3927 DBG_ERR("unexpected offset=%jd or size=%jd\n",
3928 (intmax_t)offset, (intmax_t)n);
3932 switch (fio->config->meta) {
3933 case FRUIT_META_STREAM:
3934 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
3938 case FRUIT_META_NETATALK:
3939 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
3944 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3951 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
3952 files_struct *fsp, const void *data,
3953 size_t n, off_t offset)
3955 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3958 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
3959 files_struct *fsp, const void *data,
3960 size_t n, off_t offset)
3962 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3965 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
3966 files_struct *fsp, const void *data,
3967 size_t n, off_t offset)
3969 struct adouble *ad = NULL;
3973 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3975 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
3979 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3980 offset + ad_getentryoff(ad, ADEID_RFORK));
3981 if (nwritten != n) {
3982 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
3983 fsp_str_dbg(fsp), nwritten, n);
3988 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3989 ad_setentrylen(ad, ADEID_RFORK, n + offset);
3990 ret = ad_fset(ad, fsp);
3992 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4002 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4003 files_struct *fsp, const void *data,
4004 size_t n, off_t offset)
4006 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4009 switch (fio->config->rsrc) {
4010 case FRUIT_RSRC_STREAM:
4011 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4014 case FRUIT_RSRC_ADFILE:
4015 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4018 case FRUIT_RSRC_XATTR:
4019 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4023 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4030 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4031 files_struct *fsp, const void *data,
4032 size_t n, off_t offset)
4034 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4037 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4038 fsp_str_dbg(fsp), (intmax_t)offset, n);
4041 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4044 if (fio->type == ADOUBLE_META) {
4045 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4047 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4050 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4054 struct fruit_pwrite_state {
4056 struct vfs_aio_state vfs_aio_state;
4059 static void fruit_pwrite_done(struct tevent_req *subreq);
4061 static struct tevent_req *fruit_pwrite_send(
4062 struct vfs_handle_struct *handle,
4063 TALLOC_CTX *mem_ctx,
4064 struct tevent_context *ev,
4065 struct files_struct *fsp,
4067 size_t n, off_t offset)
4069 struct tevent_req *req = NULL;
4070 struct tevent_req *subreq = NULL;
4071 struct fruit_pwrite_state *state = NULL;
4072 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4074 req = tevent_req_create(mem_ctx, &state,
4075 struct fruit_pwrite_state);
4080 if (fruit_must_handle_aio_stream(fio)) {
4081 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4082 if (state->nwritten != n) {
4083 if (state->nwritten != -1) {
4086 tevent_req_error(req, errno);
4087 return tevent_req_post(req, ev);
4089 tevent_req_done(req);
4090 return tevent_req_post(req, ev);
4093 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4095 if (tevent_req_nomem(req, subreq)) {
4096 return tevent_req_post(req, ev);
4098 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4102 static void fruit_pwrite_done(struct tevent_req *subreq)
4104 struct tevent_req *req = tevent_req_callback_data(
4105 subreq, struct tevent_req);
4106 struct fruit_pwrite_state *state = tevent_req_data(
4107 req, struct fruit_pwrite_state);
4109 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4110 TALLOC_FREE(subreq);
4112 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4115 tevent_req_done(req);
4118 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4119 struct vfs_aio_state *vfs_aio_state)
4121 struct fruit_pwrite_state *state = tevent_req_data(
4122 req, struct fruit_pwrite_state);
4124 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4128 *vfs_aio_state = state->vfs_aio_state;
4129 return state->nwritten;
4133 * Helper to stat/lstat the base file of an smb_fname.
4135 static int fruit_stat_base(vfs_handle_struct *handle,
4136 struct smb_filename *smb_fname,
4139 char *tmp_stream_name;
4142 tmp_stream_name = smb_fname->stream_name;
4143 smb_fname->stream_name = NULL;
4145 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4147 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4149 smb_fname->stream_name = tmp_stream_name;
4153 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4154 struct smb_filename *smb_fname,
4160 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4162 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4168 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4169 struct smb_filename *smb_fname,
4172 struct adouble *ad = NULL;
4174 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4176 DBG_INFO("fruit_stat_meta %s: %s\n",
4177 smb_fname_str_dbg(smb_fname), strerror(errno));
4183 /* Populate the stat struct with info from the base file. */
4184 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4187 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4188 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4189 smb_fname->stream_name);
4193 static int fruit_stat_meta(vfs_handle_struct *handle,
4194 struct smb_filename *smb_fname,
4197 struct fruit_config_data *config = NULL;
4200 SMB_VFS_HANDLE_GET_DATA(handle, config,
4201 struct fruit_config_data, return -1);
4203 switch (config->meta) {
4204 case FRUIT_META_STREAM:
4205 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4208 case FRUIT_META_NETATALK:
4209 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4213 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4220 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4221 struct smb_filename *smb_fname,
4224 struct adouble *ad = NULL;
4227 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4233 /* Populate the stat struct with info from the base file. */
4234 ret = fruit_stat_base(handle, smb_fname, follow_links);
4240 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4241 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4242 smb_fname->stream_name);
4247 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4248 struct smb_filename *smb_fname,
4254 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4256 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4262 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4263 struct smb_filename *smb_fname,
4266 #ifdef HAVE_ATTROPEN
4270 /* Populate the stat struct with info from the base file. */
4271 ret = fruit_stat_base(handle, smb_fname, follow_links);
4276 fd = attropen(smb_fname->base_name,
4277 AFPRESOURCE_EA_NETATALK,
4283 ret = sys_fstat(fd, &smb_fname->st, false);
4286 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4287 AFPRESOURCE_EA_NETATALK);
4293 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4294 smb_fname->stream_name);
4304 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4305 struct smb_filename *smb_fname,
4308 struct fruit_config_data *config = NULL;
4311 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4313 SMB_VFS_HANDLE_GET_DATA(handle, config,
4314 struct fruit_config_data, return -1);
4316 switch (config->rsrc) {
4317 case FRUIT_RSRC_STREAM:
4318 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4321 case FRUIT_RSRC_XATTR:
4322 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4325 case FRUIT_RSRC_ADFILE:
4326 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4330 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4337 static int fruit_stat(vfs_handle_struct *handle,
4338 struct smb_filename *smb_fname)
4342 DEBUG(10, ("fruit_stat called for %s\n",
4343 smb_fname_str_dbg(smb_fname)));
4345 if (!is_ntfs_stream_smb_fname(smb_fname)
4346 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4347 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4349 update_btime(handle, smb_fname);
4355 * Note if lp_posix_paths() is true, we can never
4356 * get here as is_ntfs_stream_smb_fname() is
4357 * always false. So we never need worry about
4358 * not following links here.
4361 if (is_afpinfo_stream(smb_fname)) {
4362 rc = fruit_stat_meta(handle, smb_fname, true);
4363 } else if (is_afpresource_stream(smb_fname)) {
4364 rc = fruit_stat_rsrc(handle, smb_fname, true);
4366 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4370 update_btime(handle, smb_fname);
4371 smb_fname->st.st_ex_mode &= ~S_IFMT;
4372 smb_fname->st.st_ex_mode |= S_IFREG;
4373 smb_fname->st.st_ex_blocks =
4374 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4379 static int fruit_lstat(vfs_handle_struct *handle,
4380 struct smb_filename *smb_fname)
4384 DEBUG(10, ("fruit_lstat called for %s\n",
4385 smb_fname_str_dbg(smb_fname)));
4387 if (!is_ntfs_stream_smb_fname(smb_fname)
4388 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4389 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4391 update_btime(handle, smb_fname);
4396 if (is_afpinfo_stream(smb_fname)) {
4397 rc = fruit_stat_meta(handle, smb_fname, false);
4398 } else if (is_afpresource_stream(smb_fname)) {
4399 rc = fruit_stat_rsrc(handle, smb_fname, false);
4401 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4405 update_btime(handle, smb_fname);
4406 smb_fname->st.st_ex_mode &= ~S_IFMT;
4407 smb_fname->st.st_ex_mode |= S_IFREG;
4408 smb_fname->st.st_ex_blocks =
4409 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4414 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4416 SMB_STRUCT_STAT *sbuf)
4418 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4421 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4423 SMB_STRUCT_STAT *sbuf)
4427 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4432 *sbuf = fsp->base_fsp->fsp_name->st;
4433 sbuf->st_ex_size = AFP_INFO_SIZE;
4434 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4439 static int fruit_fstat_meta(vfs_handle_struct *handle,
4441 SMB_STRUCT_STAT *sbuf,
4446 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4448 switch (fio->config->meta) {
4449 case FRUIT_META_STREAM:
4450 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4453 case FRUIT_META_NETATALK:
4454 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4458 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4462 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4466 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4468 SMB_STRUCT_STAT *sbuf)
4470 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4473 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4475 SMB_STRUCT_STAT *sbuf)
4477 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4480 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4482 SMB_STRUCT_STAT *sbuf)
4484 struct adouble *ad = NULL;
4487 /* Populate the stat struct with info from the base file. */
4488 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4493 ad = ad_get(talloc_tos(), handle,
4494 fsp->base_fsp->fsp_name,
4497 DBG_ERR("ad_get [%s] failed [%s]\n",
4498 fsp_str_dbg(fsp), strerror(errno));
4502 *sbuf = fsp->base_fsp->fsp_name->st;
4503 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4504 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4510 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4511 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4515 switch (fio->config->rsrc) {
4516 case FRUIT_RSRC_STREAM:
4517 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4520 case FRUIT_RSRC_ADFILE:
4521 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4524 case FRUIT_RSRC_XATTR:
4525 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4529 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4536 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4537 SMB_STRUCT_STAT *sbuf)
4539 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4543 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4546 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4548 if (fio->type == ADOUBLE_META) {
4549 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4551 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4555 sbuf->st_ex_mode &= ~S_IFMT;
4556 sbuf->st_ex_mode |= S_IFREG;
4557 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4560 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4561 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4565 static NTSTATUS fruit_streaminfo_meta_stream(
4566 vfs_handle_struct *handle,
4567 struct files_struct *fsp,
4568 const struct smb_filename *smb_fname,
4569 TALLOC_CTX *mem_ctx,
4570 unsigned int *pnum_streams,
4571 struct stream_struct **pstreams)
4573 struct stream_struct *stream = *pstreams;
4574 unsigned int num_streams = *pnum_streams;
4575 struct smb_filename *sname = NULL;
4580 for (i = 0; i < num_streams; i++) {
4581 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4586 if (i == num_streams) {
4587 return NT_STATUS_OK;
4590 if (stream[i].size == AFP_INFO_SIZE) {
4591 return NT_STATUS_OK;
4594 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4595 "from [%s]\n", (intmax_t)stream[i].size,
4596 smb_fname_str_dbg(smb_fname));
4598 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4600 return NT_STATUS_INTERNAL_ERROR;
4603 sname = synthetic_smb_fname(talloc_tos(),
4604 smb_fname->base_name,
4605 AFPINFO_STREAM_NAME,
4607 if (sname == NULL) {
4608 return NT_STATUS_NO_MEMORY;
4611 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4614 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4615 return map_nt_error_from_unix(errno);
4618 return NT_STATUS_OK;
4621 static NTSTATUS fruit_streaminfo_meta_netatalk(
4622 vfs_handle_struct *handle,
4623 struct files_struct *fsp,
4624 const struct smb_filename *smb_fname,
4625 TALLOC_CTX *mem_ctx,
4626 unsigned int *pnum_streams,
4627 struct stream_struct **pstreams)
4629 struct stream_struct *stream = *pstreams;
4630 unsigned int num_streams = *pnum_streams;
4631 struct adouble *ad = NULL;
4636 /* Remove the Netatalk xattr from the list */
4637 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4638 ":" NETATALK_META_XATTR ":$DATA");
4640 return NT_STATUS_NO_MEMORY;
4644 * Check if there's a AFPINFO_STREAM from the VFS streams
4645 * backend and if yes, remove it from the list
4647 for (i = 0; i < num_streams; i++) {
4648 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4653 if (i < num_streams) {
4654 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4655 smb_fname_str_dbg(smb_fname));
4657 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4660 return NT_STATUS_INTERNAL_ERROR;
4664 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4666 return NT_STATUS_OK;
4669 is_fi_empty = ad_empty_finderinfo(ad);
4673 return NT_STATUS_OK;
4676 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4677 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4678 smb_roundup(handle->conn, AFP_INFO_SIZE));
4680 return NT_STATUS_NO_MEMORY;
4683 return NT_STATUS_OK;
4686 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4687 struct files_struct *fsp,
4688 const struct smb_filename *smb_fname,
4689 TALLOC_CTX *mem_ctx,
4690 unsigned int *pnum_streams,
4691 struct stream_struct **pstreams)
4693 struct fruit_config_data *config = NULL;
4696 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4697 return NT_STATUS_INTERNAL_ERROR);
4699 switch (config->meta) {
4700 case FRUIT_META_NETATALK:
4701 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4702 mem_ctx, pnum_streams,
4706 case FRUIT_META_STREAM:
4707 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4708 mem_ctx, pnum_streams,
4713 return NT_STATUS_INTERNAL_ERROR;
4719 static NTSTATUS fruit_streaminfo_rsrc_stream(
4720 vfs_handle_struct *handle,
4721 struct files_struct *fsp,
4722 const struct smb_filename *smb_fname,
4723 TALLOC_CTX *mem_ctx,
4724 unsigned int *pnum_streams,
4725 struct stream_struct **pstreams)
4729 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4731 DBG_ERR("Filtering resource stream failed\n");
4732 return NT_STATUS_INTERNAL_ERROR;
4734 return NT_STATUS_OK;
4737 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4738 vfs_handle_struct *handle,
4739 struct files_struct *fsp,
4740 const struct smb_filename *smb_fname,
4741 TALLOC_CTX *mem_ctx,
4742 unsigned int *pnum_streams,
4743 struct stream_struct **pstreams)
4747 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4749 DBG_ERR("Filtering resource stream failed\n");
4750 return NT_STATUS_INTERNAL_ERROR;
4752 return NT_STATUS_OK;
4755 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4756 vfs_handle_struct *handle,
4757 struct files_struct *fsp,
4758 const struct smb_filename *smb_fname,
4759 TALLOC_CTX *mem_ctx,
4760 unsigned int *pnum_streams,
4761 struct stream_struct **pstreams)
4763 struct stream_struct *stream = *pstreams;
4764 unsigned int num_streams = *pnum_streams;
4765 struct adouble *ad = NULL;
4771 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4772 * and if yes, remove it from the list
4774 for (i = 0; i < num_streams; i++) {
4775 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4780 if (i < num_streams) {
4781 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4782 smb_fname_str_dbg(smb_fname));
4784 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4785 AFPRESOURCE_STREAM);
4787 return NT_STATUS_INTERNAL_ERROR;
4791 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4793 return NT_STATUS_OK;
4796 rlen = ad_getentrylen(ad, ADEID_RFORK);
4800 return NT_STATUS_OK;
4803 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4804 AFPRESOURCE_STREAM_NAME, rlen,
4805 smb_roundup(handle->conn, rlen));
4807 return NT_STATUS_NO_MEMORY;
4810 return NT_STATUS_OK;
4813 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4814 struct files_struct *fsp,
4815 const struct smb_filename *smb_fname,
4816 TALLOC_CTX *mem_ctx,
4817 unsigned int *pnum_streams,
4818 struct stream_struct **pstreams)
4820 struct fruit_config_data *config = NULL;
4823 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4824 return NT_STATUS_INTERNAL_ERROR);
4826 switch (config->rsrc) {
4827 case FRUIT_RSRC_STREAM:
4828 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4829 mem_ctx, pnum_streams,
4833 case FRUIT_RSRC_XATTR:
4834 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4835 mem_ctx, pnum_streams,
4839 case FRUIT_RSRC_ADFILE:
4840 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4841 mem_ctx, pnum_streams,
4846 return NT_STATUS_INTERNAL_ERROR;
4852 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4853 struct files_struct *fsp,
4854 const struct smb_filename *smb_fname,
4855 TALLOC_CTX *mem_ctx,
4856 unsigned int *pnum_streams,
4857 struct stream_struct **pstreams)
4859 struct fruit_config_data *config = NULL;
4862 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4863 return NT_STATUS_UNSUCCESSFUL);
4865 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4867 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4868 pnum_streams, pstreams);
4869 if (!NT_STATUS_IS_OK(status)) {
4873 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4874 mem_ctx, pnum_streams, pstreams);
4875 if (!NT_STATUS_IS_OK(status)) {
4879 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4880 mem_ctx, pnum_streams, pstreams);
4881 if (!NT_STATUS_IS_OK(status)) {
4885 return NT_STATUS_OK;
4888 static int fruit_ntimes(vfs_handle_struct *handle,
4889 const struct smb_filename *smb_fname,
4890 struct smb_file_time *ft)
4893 struct adouble *ad = NULL;
4894 struct fruit_config_data *config = NULL;
4896 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4899 if ((config->meta != FRUIT_META_NETATALK) ||
4900 null_timespec(ft->create_time))
4902 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4905 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4906 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4908 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4913 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4914 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4916 rc = ad_set(ad, smb_fname);
4922 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4925 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4928 static int fruit_fallocate(struct vfs_handle_struct *handle,
4929 struct files_struct *fsp,
4934 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4937 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4940 /* Let the pwrite code path handle it. */
4945 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4946 struct files_struct *fsp,
4950 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4953 #ifdef HAVE_ATTROPEN
4954 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4959 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4960 struct files_struct *fsp,
4964 struct adouble *ad = NULL;
4967 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4969 DBG_DEBUG("ad_get [%s] failed [%s]\n",
4970 fsp_str_dbg(fsp), strerror(errno));
4974 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4976 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset + ad_off);
4982 ad_setentrylen(ad, ADEID_RFORK, offset);
4984 rc = ad_fset(ad, fsp);
4986 DBG_ERR("ad_fset [%s] failed [%s]\n",
4987 fsp_str_dbg(fsp), strerror(errno));
4996 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4997 struct files_struct *fsp,
5001 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5004 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5007 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5008 struct files_struct *fsp,
5011 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5014 switch (fio->config->rsrc) {
5015 case FRUIT_RSRC_XATTR:
5016 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5019 case FRUIT_RSRC_ADFILE:
5020 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5023 case FRUIT_RSRC_STREAM:
5024 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5028 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5036 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5037 struct files_struct *fsp,
5041 DBG_WARNING("ftruncate %s to %jd",
5042 fsp_str_dbg(fsp), (intmax_t)offset);
5043 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5048 /* OS X returns success but does nothing */
5049 DBG_INFO("ignoring ftruncate %s to %jd\n",
5050 fsp_str_dbg(fsp), (intmax_t)offset);
5054 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5055 struct files_struct *fsp,
5058 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5061 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5065 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5068 if (fio->type == ADOUBLE_META) {
5069 ret = fruit_ftruncate_meta(handle, fsp, offset);
5071 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5074 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5078 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5079 struct smb_request *req,
5080 uint16_t root_dir_fid,
5081 struct smb_filename *smb_fname,
5082 uint32_t access_mask,
5083 uint32_t share_access,
5084 uint32_t create_disposition,
5085 uint32_t create_options,
5086 uint32_t file_attributes,
5087 uint32_t oplock_request,
5088 struct smb2_lease *lease,
5089 uint64_t allocation_size,
5090 uint32_t private_flags,
5091 struct security_descriptor *sd,
5092 struct ea_list *ea_list,
5093 files_struct **result,
5095 const struct smb2_create_blobs *in_context_blobs,
5096 struct smb2_create_blobs *out_context_blobs)
5099 struct fruit_config_data *config = NULL;
5100 files_struct *fsp = NULL;
5102 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5103 if (!NT_STATUS_IS_OK(status)) {
5107 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5108 return NT_STATUS_UNSUCCESSFUL);
5110 status = SMB_VFS_NEXT_CREATE_FILE(
5111 handle, req, root_dir_fid, smb_fname,
5112 access_mask, share_access,
5113 create_disposition, create_options,
5114 file_attributes, oplock_request,
5116 allocation_size, private_flags,
5117 sd, ea_list, result,
5118 pinfo, in_context_blobs, out_context_blobs);
5119 if (!NT_STATUS_IS_OK(status)) {
5125 if (global_fruit_config.nego_aapl) {
5126 if (config->copyfile_enabled) {
5128 * Set a flag in the fsp. Gets used in
5129 * copychunk to check whether the special
5130 * Apple copyfile semantics for copychunk
5131 * should be allowed in a copychunk request
5132 * with a count of 0.
5134 fsp->aapl_copyfile_supported = true;
5137 if (config->posix_rename && fsp->is_directory) {
5139 * Enable POSIX directory rename behaviour
5141 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5146 * If this is a plain open for existing files, opening an 0
5147 * byte size resource fork MUST fail with
5148 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5150 * Cf the vfs_fruit torture tests in test_rfork_create().
5152 if (is_afpresource_stream(fsp->fsp_name) &&
5153 create_disposition == FILE_OPEN)
5155 if (fsp->fsp_name->st.st_ex_size == 0) {
5156 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5161 if (is_ntfs_stream_smb_fname(smb_fname)
5162 || fsp->is_directory) {
5166 if (config->locking == FRUIT_LOCKING_NETATALK) {
5167 status = fruit_check_access(
5170 map_share_mode_to_deny_mode(share_access, 0));
5171 if (!NT_STATUS_IS_OK(status)) {
5179 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5182 close_file(req, fsp, ERROR_CLOSE);
5183 *result = fsp = NULL;
5189 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5190 const struct smb_filename *fname,
5191 TALLOC_CTX *mem_ctx,
5192 struct readdir_attr_data **pattr_data)
5194 struct fruit_config_data *config = NULL;
5195 struct readdir_attr_data *attr_data;
5198 SMB_VFS_HANDLE_GET_DATA(handle, config,
5199 struct fruit_config_data,
5200 return NT_STATUS_UNSUCCESSFUL);
5202 if (!global_fruit_config.nego_aapl) {
5203 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5206 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5208 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5209 if (*pattr_data == NULL) {
5210 return NT_STATUS_UNSUCCESSFUL;
5212 attr_data = *pattr_data;
5213 attr_data->type = RDATTR_AAPL;
5216 * Mac metadata: compressed FinderInfo, resource fork length
5219 status = readdir_attr_macmeta(handle, fname, attr_data);
5220 if (!NT_STATUS_IS_OK(status)) {
5222 * Error handling is tricky: if we return failure from
5223 * this function, the corresponding directory entry
5224 * will to be passed to the client, so we really just
5225 * want to error out on fatal errors.
5227 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5235 if (config->unix_info_enabled) {
5236 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5242 if (!config->readdir_attr_max_access) {
5243 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5245 status = smbd_calculate_access_mask(
5249 SEC_FLAG_MAXIMUM_ALLOWED,
5250 &attr_data->attr_data.aapl.max_access);
5251 if (!NT_STATUS_IS_OK(status)) {
5256 return NT_STATUS_OK;
5259 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5260 fname->base_name, nt_errstr(status)));
5261 TALLOC_FREE(*pattr_data);
5265 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5267 uint32_t security_info,
5268 TALLOC_CTX *mem_ctx,
5269 struct security_descriptor **ppdesc)
5272 struct security_ace ace;
5274 struct fruit_config_data *config;
5276 SMB_VFS_HANDLE_GET_DATA(handle, config,
5277 struct fruit_config_data,
5278 return NT_STATUS_UNSUCCESSFUL);
5280 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5282 if (!NT_STATUS_IS_OK(status)) {
5287 * Add MS NFS style ACEs with uid, gid and mode
5289 if (!config->unix_info_enabled) {
5290 return NT_STATUS_OK;
5293 /* MS NFS style mode */
5294 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5295 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5296 status = security_descriptor_dacl_add(*ppdesc, &ace);
5297 if (!NT_STATUS_IS_OK(status)) {
5298 DEBUG(1,("failed to add MS NFS style ACE\n"));
5302 /* MS NFS style uid */
5303 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5304 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5305 status = security_descriptor_dacl_add(*ppdesc, &ace);
5306 if (!NT_STATUS_IS_OK(status)) {
5307 DEBUG(1,("failed to add MS NFS style ACE\n"));
5311 /* MS NFS style gid */
5312 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5313 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5314 status = security_descriptor_dacl_add(*ppdesc, &ace);
5315 if (!NT_STATUS_IS_OK(status)) {
5316 DEBUG(1,("failed to add MS NFS style ACE\n"));
5320 return NT_STATUS_OK;
5323 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5325 uint32_t security_info_sent,
5326 const struct security_descriptor *psd)
5330 mode_t ms_nfs_mode = 0;
5333 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5335 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5336 if (!NT_STATUS_IS_OK(status)) {
5337 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5341 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5342 if (!NT_STATUS_IS_OK(status)) {
5343 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5348 if (fsp->fh->fd != -1) {
5349 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5351 result = SMB_VFS_CHMOD(fsp->conn,
5357 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5358 result, (unsigned)ms_nfs_mode,
5360 status = map_nt_error_from_unix(errno);
5365 return NT_STATUS_OK;
5368 struct fruit_copy_chunk_state {
5369 struct vfs_handle_struct *handle;
5371 struct files_struct *src_fsp;
5372 struct files_struct *dst_fsp;
5376 static void fruit_copy_chunk_done(struct tevent_req *subreq);
5377 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
5378 TALLOC_CTX *mem_ctx,
5379 struct tevent_context *ev,
5380 struct files_struct *src_fsp,
5382 struct files_struct *dest_fsp,
5387 struct tevent_req *req, *subreq;
5388 struct fruit_copy_chunk_state *fruit_copy_chunk_state;
5390 struct fruit_config_data *config;
5391 off_t to_copy = num;
5393 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5394 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5396 SMB_VFS_HANDLE_GET_DATA(handle, config,
5397 struct fruit_config_data,
5400 req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
5401 struct fruit_copy_chunk_state);
5405 fruit_copy_chunk_state->handle = handle;
5406 fruit_copy_chunk_state->src_fsp = src_fsp;
5407 fruit_copy_chunk_state->dst_fsp = dest_fsp;
5410 * Check if this a OS X copyfile style copychunk request with
5411 * a requested chunk count of 0 that was translated to a
5412 * copy_chunk_send VFS call overloading the parameters src_off
5413 * = dest_off = num = 0.
5415 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
5416 src_fsp->aapl_copyfile_supported &&
5417 dest_fsp->aapl_copyfile_supported)
5419 status = vfs_stat_fsp(src_fsp);
5420 if (tevent_req_nterror(req, status)) {
5421 return tevent_req_post(req, ev);
5424 to_copy = src_fsp->fsp_name->st.st_ex_size;
5425 fruit_copy_chunk_state->is_copyfile = true;
5428 subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
5437 if (tevent_req_nomem(subreq, req)) {
5438 return tevent_req_post(req, ev);
5441 tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
5445 static void fruit_copy_chunk_done(struct tevent_req *subreq)
5447 struct tevent_req *req = tevent_req_callback_data(
5448 subreq, struct tevent_req);
5449 struct fruit_copy_chunk_state *state = tevent_req_data(
5450 req, struct fruit_copy_chunk_state);
5452 unsigned int num_streams = 0;
5453 struct stream_struct *streams = NULL;
5455 struct smb_filename *src_fname_tmp = NULL;
5456 struct smb_filename *dst_fname_tmp = NULL;
5458 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
5461 TALLOC_FREE(subreq);
5462 if (tevent_req_nterror(req, status)) {
5466 if (!state->is_copyfile) {
5467 tevent_req_done(req);
5472 * Now copy all remaining streams. We know the share supports
5473 * streams, because we're in vfs_fruit. We don't do this async
5474 * because streams are few and small.
5476 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5477 state->src_fsp->fsp_name,
5478 req, &num_streams, &streams);
5479 if (tevent_req_nterror(req, status)) {
5483 if (num_streams == 1) {
5484 /* There is always one stream, ::$DATA. */
5485 tevent_req_done(req);
5489 for (i = 0; i < num_streams; i++) {
5490 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5491 __func__, streams[i].name, (size_t)streams[i].size));
5493 src_fname_tmp = synthetic_smb_fname(
5495 state->src_fsp->fsp_name->base_name,
5498 state->src_fsp->fsp_name->flags);
5499 if (tevent_req_nomem(src_fname_tmp, req)) {
5503 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5504 TALLOC_FREE(src_fname_tmp);
5508 dst_fname_tmp = synthetic_smb_fname(
5510 state->dst_fsp->fsp_name->base_name,
5513 state->dst_fsp->fsp_name->flags);
5514 if (tevent_req_nomem(dst_fname_tmp, req)) {
5515 TALLOC_FREE(src_fname_tmp);
5519 status = copy_file(req,
5520 state->handle->conn,
5523 OPENX_FILE_CREATE_IF_NOT_EXIST,
5525 if (!NT_STATUS_IS_OK(status)) {
5526 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5527 smb_fname_str_dbg(src_fname_tmp),
5528 smb_fname_str_dbg(dst_fname_tmp),
5529 nt_errstr(status)));
5530 TALLOC_FREE(src_fname_tmp);
5531 TALLOC_FREE(dst_fname_tmp);
5532 tevent_req_nterror(req, status);
5536 TALLOC_FREE(src_fname_tmp);
5537 TALLOC_FREE(dst_fname_tmp);
5540 TALLOC_FREE(streams);
5541 TALLOC_FREE(src_fname_tmp);
5542 TALLOC_FREE(dst_fname_tmp);
5543 tevent_req_done(req);
5546 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
5547 struct tevent_req *req,
5550 struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
5551 req, struct fruit_copy_chunk_state);
5554 if (tevent_req_is_nterror(req, &status)) {
5555 DEBUG(1, ("server side copy chunk failed: %s\n",
5556 nt_errstr(status)));
5558 tevent_req_received(req);
5562 *copied = fruit_copy_chunk_state->copied;
5563 tevent_req_received(req);
5565 return NT_STATUS_OK;
5568 static struct vfs_fn_pointers vfs_fruit_fns = {
5569 .connect_fn = fruit_connect,
5571 /* File operations */
5572 .chmod_fn = fruit_chmod,
5573 .chown_fn = fruit_chown,
5574 .unlink_fn = fruit_unlink,
5575 .rename_fn = fruit_rename,
5576 .rmdir_fn = fruit_rmdir,
5577 .open_fn = fruit_open,
5578 .pread_fn = fruit_pread,
5579 .pwrite_fn = fruit_pwrite,
5580 .pread_send_fn = fruit_pread_send,
5581 .pread_recv_fn = fruit_pread_recv,
5582 .pwrite_send_fn = fruit_pwrite_send,
5583 .pwrite_recv_fn = fruit_pwrite_recv,
5584 .stat_fn = fruit_stat,
5585 .lstat_fn = fruit_lstat,
5586 .fstat_fn = fruit_fstat,
5587 .streaminfo_fn = fruit_streaminfo,
5588 .ntimes_fn = fruit_ntimes,
5589 .ftruncate_fn = fruit_ftruncate,
5590 .fallocate_fn = fruit_fallocate,
5591 .create_file_fn = fruit_create_file,
5592 .readdir_attr_fn = fruit_readdir_attr,
5593 .copy_chunk_send_fn = fruit_copy_chunk_send,
5594 .copy_chunk_recv_fn = fruit_copy_chunk_recv,
5596 /* NT ACL operations */
5597 .fget_nt_acl_fn = fruit_fget_nt_acl,
5598 .fset_nt_acl_fn = fruit_fset_nt_acl,
5601 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
5602 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5604 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5606 if (!NT_STATUS_IS_OK(ret)) {
5610 vfs_fruit_debug_level = debug_add_class("fruit");
5611 if (vfs_fruit_debug_level == -1) {
5612 vfs_fruit_debug_level = DBGC_VFS;
5613 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5616 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5617 "vfs_fruit_init","fruit",vfs_fruit_debug_level));