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"
36 * Enhanced OS X and Netatalk compatibility
37 * ========================================
39 * This modules takes advantage of vfs_streams_xattr and
40 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
41 * loaded in the correct order:
43 * vfs modules = catia fruit streams_xattr
45 * The module intercepts the OS X special streams "AFP_AfpInfo" and
46 * "AFP_Resource" and handles them in a special way. All other named
47 * streams are deferred to vfs_streams_xattr.
49 * The OS X client maps all NTFS illegal characters to the Unicode
50 * private range. This module optionally stores the charcters using
51 * their native ASCII encoding using vfs_catia. If you're not enabling
52 * this feature, you can skip catia from vfs modules.
54 * Finally, open modes are optionally checked against Netatalk AFP
57 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
58 * extended metadata for files and directories. This module optionally
59 * reads and stores this metadata in a way compatible with Netatalk 3
60 * which stores the metadata in an EA "org.netatalk.metadata". Cf
61 * source3/include/MacExtensions.h for a description of the binary
64 * The "AFP_Resource" named stream may be arbitrarily large, thus it
65 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
66 * the only available filesystem where xattrs can be of any size and
67 * the OS supports using the file APIs for xattrs.
69 * The AFP_Resource stream is stored in an AppleDouble file prepending
70 * "._" to the filename. On Solaris with ZFS the stream is optionally
71 * stored in an EA "org.netatalk.resource".
77 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
78 * other protocols you may want to adjust the xattr names the VFS
79 * module vfs_streams_xattr uses for storing ADS's. This defaults to
80 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
81 * these module parameters:
83 * streams_xattr:prefix = user.
84 * streams_xattr:store_stream_type = false
90 * - log diagnostic if any needed VFS module is not loaded
91 * (eg with lp_vfs_objects())
95 static int vfs_fruit_debug_level = DBGC_VFS;
98 #define DBGC_CLASS vfs_fruit_debug_level
100 #define FRUIT_PARAM_TYPE_NAME "fruit"
101 #define ADOUBLE_NAME_PREFIX "._"
103 #define NETATALK_META_XATTR "org.netatalk.Metadata"
104 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
106 #if defined(HAVE_ATTROPEN)
107 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
108 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
110 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
111 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
114 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
116 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
117 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
118 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
119 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
121 struct fruit_config_data {
122 enum fruit_rsrc rsrc;
123 enum fruit_meta meta;
124 enum fruit_locking locking;
125 enum fruit_encoding encoding;
126 bool use_aapl; /* config from smb.conf */
127 bool nego_aapl; /* client negotiated AAPL */
129 bool readdir_attr_enabled;
130 bool unix_info_enabled;
131 bool copyfile_enabled;
132 bool veto_appledouble;
136 * Additional options, all enabled by default,
137 * possibly useful for analyzing performance. The associated
138 * operations with each of them may be expensive, so having
139 * the chance to disable them individually gives a chance
140 * tweaking the setup for the particular usecase.
142 bool readdir_attr_rsize;
143 bool readdir_attr_finder_info;
144 bool readdir_attr_max_access;
147 static const struct enum_list fruit_rsrc[] = {
148 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
149 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
150 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
154 static const struct enum_list fruit_meta[] = {
155 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
156 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
160 static const struct enum_list fruit_locking[] = {
161 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
162 {FRUIT_LOCKING_NONE, "none"},
166 static const struct enum_list fruit_encoding[] = {
167 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
168 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
172 /*****************************************************************************
173 * Defines, functions and data structures that deal with AppleDouble
174 *****************************************************************************/
177 * There are two AppleDouble blobs we deal with:
179 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
180 * metadata in an xattr
182 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
185 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
188 #define AD_VERSION2 0x00020000
189 #define AD_VERSION AD_VERSION2
192 * AppleDouble entry IDs.
194 #define ADEID_DFORK 1
195 #define ADEID_RFORK 2
197 #define ADEID_COMMENT 4
198 #define ADEID_ICONBW 5
199 #define ADEID_ICONCOL 6
200 #define ADEID_FILEI 7
201 #define ADEID_FILEDATESI 8
202 #define ADEID_FINDERI 9
203 #define ADEID_MACFILEI 10
204 #define ADEID_PRODOSFILEI 11
205 #define ADEID_MSDOSFILEI 12
206 #define ADEID_SHORTNAME 13
207 #define ADEID_AFPFILEI 14
210 /* Private Netatalk entries */
211 #define ADEID_PRIVDEV 16
212 #define ADEID_PRIVINO 17
213 #define ADEID_PRIVSYN 18
214 #define ADEID_PRIVID 19
215 #define ADEID_MAX (ADEID_PRIVID + 1)
218 * These are the real ids for the private entries,
219 * as stored in the adouble file
221 #define AD_DEV 0x80444556
222 #define AD_INO 0x80494E4F
223 #define AD_SYN 0x8053594E
224 #define AD_ID 0x8053567E
226 /* Number of actually used entries */
227 #define ADEID_NUM_XATTR 8
228 #define ADEID_NUM_DOT_UND 2
229 #define ADEID_NUM_RSRC_XATTR 1
231 /* AppleDouble magic */
232 #define AD_APPLESINGLE_MAGIC 0x00051600
233 #define AD_APPLEDOUBLE_MAGIC 0x00051607
234 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
236 /* Sizes of relevant entry bits */
237 #define ADEDLEN_MAGIC 4
238 #define ADEDLEN_VERSION 4
239 #define ADEDLEN_FILLER 16
240 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
241 #define ADEDLEN_NENTRIES 2
242 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
243 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
244 #define AD_ENTRY_LEN_EID 4
245 #define AD_ENTRY_LEN_OFF 4
246 #define AD_ENTRY_LEN_LEN 4
247 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
250 #define ADEDLEN_NAME 255
251 #define ADEDLEN_COMMENT 200
252 #define ADEDLEN_FILEI 16
253 #define ADEDLEN_FINDERI 32
254 #define ADEDLEN_FILEDATESI 16
255 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
256 #define ADEDLEN_AFPFILEI 4
257 #define ADEDLEN_MACFILEI 4
258 #define ADEDLEN_PRODOSFILEI 8
259 #define ADEDLEN_MSDOSFILEI 2
260 #define ADEDLEN_DID 4
261 #define ADEDLEN_PRIVDEV 8
262 #define ADEDLEN_PRIVINO 8
263 #define ADEDLEN_PRIVSYN 8
264 #define ADEDLEN_PRIVID 4
267 #define ADEDOFF_MAGIC 0
268 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
269 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
270 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
272 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
273 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
274 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
275 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
276 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
278 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
279 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
280 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
281 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
283 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
284 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
285 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
287 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
288 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
289 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
290 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
291 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
292 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
294 #if AD_DATASZ_XATTR != 402
295 #error bad size for AD_DATASZ_XATTR
298 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
299 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
301 #if AD_DATASZ_DOT_UND != 82
302 #error bad size for AD_DATASZ_DOT_UND
306 * Sharemode locks fcntl() offsets
308 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
309 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
311 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
313 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
315 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
316 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
317 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
318 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
319 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
320 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
321 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
322 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
323 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
324 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
326 /* Time stuff we overload the bits a little */
327 #define AD_DATE_CREATE 0
328 #define AD_DATE_MODIFY 4
329 #define AD_DATE_BACKUP 8
330 #define AD_DATE_ACCESS 12
331 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
332 AD_DATE_BACKUP | AD_DATE_ACCESS)
333 #define AD_DATE_UNIX (1 << 10)
334 #define AD_DATE_START 0x80000000
335 #define AD_DATE_DELTA 946684800
336 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
337 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
339 /* Accessor macros */
340 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
341 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
342 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
343 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
351 vfs_handle_struct *ad_handle;
352 files_struct *ad_fsp;
353 adouble_type_t ad_type;
356 struct ad_entry ad_eid[ADEID_MAX];
360 struct ad_entry_order {
361 uint32_t id, offset, len;
364 /* Netatalk AppleDouble metadata xattr */
366 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
367 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
368 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
369 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
370 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
371 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
372 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
373 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
374 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
378 /* AppleDouble resource fork file (the ones prefixed by "._") */
380 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
381 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
382 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
387 * Fake AppleDouble entry oder for resource fork xattr. The xattr
388 * isn't an AppleDouble file, it simply contains the resource data,
389 * but in order to be able to use some API calls like ad_getentryoff()
390 * we build a fake/helper struct adouble with this entry order struct.
393 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
398 /* Conversion from enumerated id to on-disk AppleDouble id */
399 #define AD_EID_DISK(a) (set_eid[a])
400 static const uint32_t set_eid[] = {
401 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
402 AD_DEV, AD_INO, AD_SYN, AD_ID
406 * Forward declarations
408 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
409 adouble_type_t type, files_struct *fsp);
410 static int ad_write(struct adouble *ad, const char *path);
411 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
414 * Return a pointer to an AppleDouble entry
416 * Returns NULL if the entry is not present
418 static char *ad_get_entry(const struct adouble *ad, int eid)
420 off_t off = ad_getentryoff(ad, eid);
421 size_t len = ad_getentrylen(ad, eid);
423 if (off == 0 || len == 0) {
427 return ad->ad_data + off;
433 static int ad_getdate(const struct adouble *ad,
434 unsigned int dateoff,
437 bool xlate = (dateoff & AD_DATE_UNIX);
440 dateoff &= AD_DATE_MASK;
441 p = ad_get_entry(ad, ADEID_FILEDATESI);
446 if (dateoff > AD_DATE_ACCESS) {
450 memcpy(date, p + dateoff, sizeof(uint32_t));
453 *date = AD_DATE_TO_UNIX(*date);
461 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
463 bool xlate = (dateoff & AD_DATE_UNIX);
466 p = ad_get_entry(ad, ADEID_FILEDATESI);
471 dateoff &= AD_DATE_MASK;
473 date = AD_DATE_FROM_UNIX(date);
476 if (dateoff > AD_DATE_ACCESS) {
480 memcpy(p + dateoff, &date, sizeof(date));
487 * Map on-disk AppleDouble id to enumerated id
489 static uint32_t get_eid(uint32_t eid)
497 return ADEID_PRIVDEV;
499 return ADEID_PRIVINO;
501 return ADEID_PRIVSYN;
512 * Pack AppleDouble structure into data buffer
514 static bool ad_pack(struct adouble *ad)
521 bufsize = talloc_get_size(ad->ad_data);
523 if (offset + ADEDLEN_MAGIC < offset ||
524 offset + ADEDLEN_MAGIC >= bufsize) {
527 RSIVAL(ad->ad_data, offset, ad->ad_magic);
528 offset += ADEDLEN_MAGIC;
530 if (offset + ADEDLEN_VERSION < offset ||
531 offset + ADEDLEN_VERSION >= bufsize) {
534 RSIVAL(ad->ad_data, offset, ad->ad_version);
535 offset += ADEDLEN_VERSION;
537 if (offset + ADEDLEN_FILLER < offset ||
538 offset + ADEDLEN_FILLER >= bufsize) {
541 if (ad->ad_type == ADOUBLE_RSRC) {
542 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
544 offset += ADEDLEN_FILLER;
546 if (offset + ADEDLEN_NENTRIES < offset ||
547 offset + ADEDLEN_NENTRIES >= bufsize) {
550 offset += ADEDLEN_NENTRIES;
552 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
553 if (ad->ad_eid[eid].ade_off == 0) {
555 * ade_off is also used as indicator whether a
556 * specific entry is used or not
561 if (offset + AD_ENTRY_LEN_EID < offset ||
562 offset + AD_ENTRY_LEN_EID >= bufsize) {
565 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
566 offset += AD_ENTRY_LEN_EID;
568 if (offset + AD_ENTRY_LEN_OFF < offset ||
569 offset + AD_ENTRY_LEN_OFF >= bufsize) {
572 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
573 offset += AD_ENTRY_LEN_OFF;
575 if (offset + AD_ENTRY_LEN_LEN < offset ||
576 offset + AD_ENTRY_LEN_LEN >= bufsize) {
579 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
580 offset += AD_ENTRY_LEN_LEN;
585 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
588 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
594 * Unpack an AppleDouble blob into a struct adoble
596 static bool ad_unpack(struct adouble *ad, const size_t nentries,
599 size_t bufsize = talloc_get_size(ad->ad_data);
601 uint32_t eid, len, off;
604 * The size of the buffer ad->ad_data is checked when read, so
605 * we wouldn't have to check our own offsets, a few extra
606 * checks won't hurt though. We have to check the offsets we
607 * read from the buffer anyway.
610 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
611 DEBUG(1, ("bad size\n"));
615 ad->ad_magic = RIVAL(ad->ad_data, 0);
616 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
617 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
618 DEBUG(1, ("wrong magic or version\n"));
622 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
623 if (adentries != nentries) {
624 DEBUG(1, ("invalid number of entries: %zu\n",
629 /* now, read in the entry bits */
630 for (i = 0; i < adentries; i++) {
631 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
633 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
634 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
636 if (!eid || eid > ADEID_MAX) {
637 DEBUG(1, ("bogus eid %d\n", eid));
642 * All entries other than the resource fork are
643 * expected to be read into the ad_data buffer, so
644 * ensure the specified offset is within that bound
646 if ((off > bufsize) && (eid != ADEID_RFORK)) {
647 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
653 * All entries besides FinderInfo and resource fork
654 * must fit into the buffer. FinderInfo is special as
655 * it may be larger then the default 32 bytes (if it
656 * contains marshalled xattrs), but we will fixup that
657 * in ad_convert(). And the resource fork is never
658 * accessed directly by the ad_data buf (also see
659 * comment above) anyway.
661 if ((eid != ADEID_RFORK) &&
662 (eid != ADEID_FINDERI) &&
663 ((off + len) > bufsize)) {
664 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
670 * That would be obviously broken
672 if (off > filesize) {
673 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
679 * Check for any entry that has its end beyond the
682 if (off + len < off) {
683 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
684 ", len: %" PRIu32 "\n",
689 if (off + len > filesize) {
691 * If this is the resource fork entry, we fix
692 * up the length, for any other entry we bail
695 if (eid != ADEID_RFORK) {
696 DEBUG(1, ("bogus eid %d: off: %" PRIu32
697 ", len: %" PRIu32 "\n",
703 * Fixup the resource fork entry by limiting
704 * the size to entryoffset - filesize.
706 len = filesize - off;
707 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
708 ", len: %" PRIu32 "\n", off, len));
711 ad->ad_eid[eid].ade_off = off;
712 ad->ad_eid[eid].ade_len = len;
719 * Convert from Apple's ._ file to Netatalk
721 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
722 * bytes containing packed xattrs. Netatalk can't deal with that, so
723 * we simply discard the packed xattrs.
725 * @return -1 in case an error occurred, 0 if no conversion was done, 1
728 static int ad_convert(struct adouble *ad, int fd)
731 char *map = MAP_FAILED;
734 origlen = ad_getentryoff(ad, ADEID_RFORK) +
735 ad_getentrylen(ad, ADEID_RFORK);
737 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
738 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
739 if (map == MAP_FAILED) {
740 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
745 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
746 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
747 map + ad_getentryoff(ad, ADEID_RFORK),
748 ad_getentrylen(ad, ADEID_RFORK));
751 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
752 ad_setentryoff(ad, ADEID_RFORK,
753 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
756 * FIXME: direct ftruncate(), but we don't have a fsp for the
759 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
760 + ad_getentrylen(ad, ADEID_RFORK));
763 if (map != MAP_FAILED) {
764 munmap(map, origlen);
770 * Read and parse Netatalk AppleDouble metadata xattr
772 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
778 DEBUG(10, ("reading meta xattr for %s\n", path));
780 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
781 AFPINFO_EA_NETATALK, ad->ad_data,
787 if (errno == ENOATTR) {
793 DEBUG(2, ("error reading meta xattr: %s\n",
799 if (ealen != AD_DATASZ_XATTR) {
800 DEBUG(2, ("bad size %zd\n", ealen));
806 /* Now parse entries */
807 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
809 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
815 if (!ad_getentryoff(ad, ADEID_FINDERI)
816 || !ad_getentryoff(ad, ADEID_COMMENT)
817 || !ad_getentryoff(ad, ADEID_FILEDATESI)
818 || !ad_getentryoff(ad, ADEID_AFPFILEI)
819 || !ad_getentryoff(ad, ADEID_PRIVDEV)
820 || !ad_getentryoff(ad, ADEID_PRIVINO)
821 || !ad_getentryoff(ad, ADEID_PRIVSYN)
822 || !ad_getentryoff(ad, ADEID_PRIVID)) {
823 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
830 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
834 if (errno == EINVAL) {
836 removexattr(path, AFPINFO_EA_NETATALK);
845 * Read and parse resource fork, either ._ AppleDouble file or xattr
847 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
849 struct fruit_config_data *config = NULL;
856 struct adouble *meta_ad = NULL;
857 SMB_STRUCT_STAT sbuf;
861 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
862 struct fruit_config_data, return -1);
864 /* Try rw first so we can use the fd in ad_convert() */
867 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
868 fd = ad->ad_fsp->fh->fd;
870 if (config->rsrc == FRUIT_RSRC_XATTR) {
871 adpath = talloc_strdup(talloc_tos(), path);
873 rc = adouble_path(talloc_tos(), path, &adpath);
880 if (config->rsrc == FRUIT_RSRC_XATTR) {
881 #ifndef HAVE_ATTROPEN
886 /* FIXME: direct Solaris xattr syscall */
887 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
891 /* FIXME: direct open(), don't have an fsp */
892 fd = open(adpath, mode);
899 if (mode == O_RDWR) {
903 /* fall through ... */
905 DEBUG(2, ("open AppleDouble: %s, %s\n",
906 adpath, strerror(errno)));
914 if (config->rsrc == FRUIT_RSRC_XATTR) {
915 /* FIXME: direct sys_fstat(), don't have an fsp */
918 lp_fake_directory_create_times(
919 SNUM(ad->ad_handle->conn)));
923 len = sbuf.st_ex_size;
924 ad_setentrylen(ad, ADEID_RFORK, len);
926 /* FIXME: direct sys_pread(), don't have an fsp */
927 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
928 if (len != AD_DATASZ_DOT_UND) {
929 DEBUG(2, ("%s: bad size: %zd\n",
930 strerror(errno), len));
935 /* FIXME: direct sys_fstat(), we don't have an fsp */
936 rc = sys_fstat(fd, &sbuf,
937 lp_fake_directory_create_times(
938 SNUM(ad->ad_handle->conn)));
943 /* Now parse entries */
944 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
946 DEBUG(1, ("invalid AppleDouble resource %s\n", path));
952 if ((ad_getentryoff(ad, ADEID_FINDERI)
953 != ADEDOFF_FINDERI_DOT_UND)
954 || (ad_getentrylen(ad, ADEID_FINDERI)
956 || (ad_getentryoff(ad, ADEID_RFORK)
957 < ADEDOFF_RFORK_DOT_UND)) {
958 DEBUG(2, ("invalid AppleDouble resource %s\n", path));
965 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
967 char *p_meta_ad = NULL;
969 rc = ad_convert(ad, fd);
975 * Can't use ad_write() because we might not have a fsp
982 /* FIXME: direct sys_pwrite(), don't have an fsp */
983 len = sys_pwrite(fd, ad->ad_data,
984 AD_DATASZ_DOT_UND, 0);
985 if (len != AD_DATASZ_DOT_UND) {
986 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
991 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
993 if (meta_ad == NULL) {
998 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1003 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1004 if (p_meta_ad == NULL) {
1009 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1011 rc = ad_write(meta_ad, path);
1019 DEBUG(10, ("opened AppleDouble: %s\n", path));
1023 saved_errno = errno;
1026 if (opened && fd != -1) {
1029 TALLOC_FREE(adpath);
1030 TALLOC_FREE(meta_ad);
1032 errno = saved_errno;
1038 * Read and unpack an AppleDouble metadata xattr or resource
1040 static ssize_t ad_read(struct adouble *ad, const char *path)
1042 switch (ad->ad_type) {
1044 return ad_header_read_meta(ad, path);
1046 return ad_header_read_rsrc(ad, path);
1053 * Allocate a struct adouble without initialiing it
1055 * The struct is either hang of the fsp extension context or if fsp is
1058 * @param[in] ctx talloc context
1059 * @param[in] handle vfs handle
1060 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1062 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1063 * added as an fsp extension
1065 * @return adouble handle
1067 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1068 adouble_type_t type, files_struct *fsp)
1073 struct fruit_config_data *config;
1075 SMB_VFS_HANDLE_GET_DATA(handle, config,
1076 struct fruit_config_data, return NULL);
1080 adsize = AD_DATASZ_XATTR;
1083 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1084 adsize = AD_DATASZ_DOT_UND;
1092 ad = talloc_zero(ctx, struct adouble);
1098 ad->ad_data = talloc_zero_array(ad, char, adsize);
1101 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1109 ad->ad_data = talloc_zero_array(
1110 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1116 if (adsize && ad->ad_data == NULL) {
1120 ad->ad_handle = handle;
1122 ad->ad_magic = AD_MAGIC;
1123 ad->ad_version = AD_VERSION;
1133 * Allocate and initialize a new struct adouble
1135 * @param[in] ctx talloc context
1136 * @param[in] handle vfs handle
1137 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1138 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1140 * @return adouble handle, initialized
1142 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1143 adouble_type_t type, files_struct *fsp)
1146 const struct ad_entry_order *eid;
1147 struct adouble *ad = NULL;
1148 struct fruit_config_data *config;
1149 time_t t = time(NULL);
1151 SMB_VFS_HANDLE_GET_DATA(handle, config,
1152 struct fruit_config_data, return NULL);
1156 eid = entry_order_meta_xattr;
1159 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1160 eid = entry_order_dot_und;
1162 eid = entry_order_rsrc_xattr;
1169 ad = ad_alloc(ctx, handle, type, fsp);
1175 ad->ad_eid[eid->id].ade_off = eid->offset;
1176 ad->ad_eid[eid->id].ade_len = eid->len;
1180 /* put something sane in the date fields */
1181 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1182 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1183 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1184 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1193 * Return AppleDouble data for a file
1195 * @param[in] ctx talloc context
1196 * @param[in] handle vfs handle
1197 * @param[in] path pathname to file or directory
1198 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1200 * @return talloced struct adouble or NULL on error
1202 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1203 const char *path, adouble_type_t type)
1207 struct adouble *ad = NULL;
1209 DEBUG(10, ("ad_get(%s) called for %s\n",
1210 type == ADOUBLE_META ? "meta" : "rsrc", path));
1212 ad = ad_alloc(ctx, handle, type, NULL);
1218 len = ad_read(ad, path);
1220 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1226 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1227 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1236 * Set AppleDouble metadata on a file or directory
1238 * @param[in] ad adouble handle
1240 * @param[in] path pathname to file or directory, may be NULL for a
1243 * @return status code, 0 means success
1245 static int ad_write(struct adouble *ad, const char *path)
1256 switch (ad->ad_type) {
1258 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1259 AFPINFO_EA_NETATALK, ad->ad_data,
1260 AD_DATASZ_XATTR, 0);
1263 if ((ad->ad_fsp == NULL)
1264 || (ad->ad_fsp->fh == NULL)
1265 || (ad->ad_fsp->fh->fd == -1)) {
1269 /* FIXME: direct sys_pwrite(), don't have an fsp */
1270 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1271 talloc_get_size(ad->ad_data), 0);
1272 if (len != talloc_get_size(ad->ad_data)) {
1273 DEBUG(1, ("short write on %s: %zd",
1274 fsp_str_dbg(ad->ad_fsp), len));
1286 /*****************************************************************************
1288 *****************************************************************************/
1290 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1292 if (strncasecmp_m(smb_fname->stream_name,
1293 AFPINFO_STREAM_NAME,
1294 strlen(AFPINFO_STREAM_NAME)) == 0) {
1300 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1302 if (strncasecmp_m(smb_fname->stream_name,
1303 AFPRESOURCE_STREAM_NAME,
1304 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1311 * Test whether stream is an Apple stream, not used atm
1314 static bool is_apple_stream(const struct smb_filename *smb_fname)
1316 if (is_afpinfo_stream(smb_fname)) {
1319 if (is_afpresource_stream(smb_fname)) {
1327 * Initialize config struct from our smb.conf config parameters
1329 static int init_fruit_config(vfs_handle_struct *handle)
1331 struct fruit_config_data *config;
1334 config = talloc_zero(handle->conn, struct fruit_config_data);
1336 DEBUG(1, ("talloc_zero() failed\n"));
1342 * Versions up to Samba 4.5.x had a spelling bug in the
1343 * fruit:resource option calling lp_parm_enum with
1344 * "res*s*ource" (ie two s).
1346 * In Samba 4.6 we accept both the wrong and the correct
1347 * spelling, in Samba 4.7 the bad spelling will be removed.
1349 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1350 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1351 if (enumval == -1) {
1352 DEBUG(1, ("value for %s: resource type unknown\n",
1353 FRUIT_PARAM_TYPE_NAME));
1356 config->rsrc = (enum fruit_rsrc)enumval;
1358 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1359 "resource", fruit_rsrc, enumval);
1360 if (enumval == -1) {
1361 DEBUG(1, ("value for %s: resource type unknown\n",
1362 FRUIT_PARAM_TYPE_NAME));
1365 config->rsrc = (enum fruit_rsrc)enumval;
1367 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1368 "metadata", fruit_meta, FRUIT_META_NETATALK);
1369 if (enumval == -1) {
1370 DEBUG(1, ("value for %s: metadata type unknown\n",
1371 FRUIT_PARAM_TYPE_NAME));
1374 config->meta = (enum fruit_meta)enumval;
1376 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1377 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1378 if (enumval == -1) {
1379 DEBUG(1, ("value for %s: locking type unknown\n",
1380 FRUIT_PARAM_TYPE_NAME));
1383 config->locking = (enum fruit_locking)enumval;
1385 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1386 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1387 if (enumval == -1) {
1388 DEBUG(1, ("value for %s: encoding type unknown\n",
1389 FRUIT_PARAM_TYPE_NAME));
1392 config->encoding = (enum fruit_encoding)enumval;
1394 config->veto_appledouble = lp_parm_bool(
1395 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1396 "veto_appledouble", true);
1398 config->use_aapl = lp_parm_bool(
1399 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1401 config->unix_info_enabled = lp_parm_bool(
1402 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1404 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1407 config->posix_rename = lp_parm_bool(
1408 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1410 config->readdir_attr_rsize = lp_parm_bool(
1411 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1413 config->readdir_attr_finder_info = lp_parm_bool(
1414 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1416 config->readdir_attr_max_access = lp_parm_bool(
1417 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1419 SMB_VFS_HANDLE_SET_DATA(handle, config,
1420 NULL, struct fruit_config_data,
1427 * Prepend "._" to a basename
1429 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1434 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1438 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1439 if (*path_out == NULL) {
1447 * Allocate and initialize an AfpInfo struct
1449 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1451 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1455 ai->afpi_Signature = AFP_Signature;
1456 ai->afpi_Version = AFP_Version;
1457 ai->afpi_BackupTime = AD_DATE_START;
1462 * Pack an AfpInfo struct into a buffer
1464 * Buffer size must be at least AFP_INFO_SIZE
1465 * Returns size of packed buffer
1467 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1469 memset(buf, 0, AFP_INFO_SIZE);
1471 RSIVAL(buf, 0, ai->afpi_Signature);
1472 RSIVAL(buf, 4, ai->afpi_Version);
1473 RSIVAL(buf, 12, ai->afpi_BackupTime);
1474 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1476 return AFP_INFO_SIZE;
1480 * Unpack a buffer into a AfpInfo structure
1482 * Buffer size must be at least AFP_INFO_SIZE
1483 * Returns allocated AfpInfo struct
1485 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1487 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1492 ai->afpi_Signature = RIVAL(data, 0);
1493 ai->afpi_Version = RIVAL(data, 4);
1494 ai->afpi_BackupTime = RIVAL(data, 12);
1495 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1496 sizeof(ai->afpi_FinderInfo));
1498 if (ai->afpi_Signature != AFP_Signature
1499 || ai->afpi_Version != AFP_Version) {
1500 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1508 * Fake an inode number from the md5 hash of the (xattr) name
1510 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1513 unsigned char hash[16];
1517 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1518 SMB_ASSERT(upper_sname != NULL);
1521 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1522 sizeof(sbuf->st_ex_dev));
1523 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1524 sizeof(sbuf->st_ex_ino));
1525 MD5Update(&ctx, (unsigned char *)upper_sname,
1526 talloc_get_size(upper_sname)-1);
1527 MD5Final(hash, &ctx);
1529 TALLOC_FREE(upper_sname);
1531 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1532 memcpy(&result, hash, sizeof(result));
1534 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1535 sname, (unsigned long long)result));
1541 * Ensure ad_fsp is still valid
1543 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1545 if (ad->ad_fsp == fsp) {
1553 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1554 struct stream_struct **streams,
1555 const char *name, off_t size,
1558 struct stream_struct *tmp;
1560 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1566 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1567 if (tmp[*num_streams].name == NULL) {
1571 tmp[*num_streams].size = size;
1572 tmp[*num_streams].alloc_size = alloc_size;
1579 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1580 struct stream_struct **streams)
1582 struct stream_struct *tmp = *streams;
1585 if (*num_streams == 0) {
1589 for (i = 0; i < *num_streams; i++) {
1590 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1595 if (i == *num_streams) {
1599 if (tmp[i].size > 0) {
1603 TALLOC_FREE(tmp[i].name);
1604 if (*num_streams - 1 > i) {
1605 memmove(&tmp[i], &tmp[i+1],
1606 (*num_streams - i - 1) * sizeof(struct stream_struct));
1613 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1614 struct stream_struct **streams,
1617 struct stream_struct *tmp = *streams;
1620 if (*num_streams == 0) {
1624 for (i = 0; i < *num_streams; i++) {
1625 if (strequal_m(tmp[i].name, name)) {
1630 if (i == *num_streams) {
1634 TALLOC_FREE(tmp[i].name);
1635 if (*num_streams - 1 > i) {
1636 memmove(&tmp[i], &tmp[i+1],
1637 (*num_streams - i - 1) * sizeof(struct stream_struct));
1644 static bool ad_empty_finderinfo(const struct adouble *ad)
1647 char emptybuf[ADEDLEN_FINDERI] = {0};
1650 fi = ad_get_entry(ad, ADEID_FINDERI);
1652 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1656 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1660 static bool ai_empty_finderinfo(const AfpInfo *ai)
1663 char emptybuf[ADEDLEN_FINDERI] = {0};
1665 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1670 * Update btime with btime from Netatalk
1672 static void update_btime(vfs_handle_struct *handle,
1673 struct smb_filename *smb_fname)
1676 struct timespec creation_time = {0};
1678 struct fruit_config_data *config = NULL;
1680 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1683 switch (config->meta) {
1684 case FRUIT_META_STREAM:
1686 case FRUIT_META_NETATALK:
1690 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1694 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1698 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1704 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1705 update_stat_ex_create_time(&smb_fname->st, creation_time);
1711 * Map an access mask to a Netatalk single byte byte range lock
1713 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1714 uint32_t access_mask)
1718 switch (access_mask) {
1719 case FILE_READ_DATA:
1720 offset = AD_FILELOCK_OPEN_RD;
1723 case FILE_WRITE_DATA:
1724 case FILE_APPEND_DATA:
1725 offset = AD_FILELOCK_OPEN_WR;
1729 offset = AD_FILELOCK_OPEN_NONE;
1733 if (fork_type == APPLE_FORK_RSRC) {
1734 if (offset == AD_FILELOCK_OPEN_NONE) {
1735 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1745 * Map a deny mode to a Netatalk brl
1747 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1752 switch (deny_mode) {
1754 offset = AD_FILELOCK_DENY_RD;
1758 offset = AD_FILELOCK_DENY_WR;
1762 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1765 if (fork_type == APPLE_FORK_RSRC) {
1773 * Call fcntl() with an exclusive F_GETLK request in order to
1774 * determine if there's an exisiting shared lock
1776 * @return true if the requested lock was found or any error occurred
1777 * false if the lock was not found
1779 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1782 off_t offset = in_offset;
1787 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1788 if (result == false) {
1792 if (type != F_UNLCK) {
1799 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1801 uint32_t access_mask,
1804 NTSTATUS status = NT_STATUS_OK;
1805 struct byte_range_lock *br_lck = NULL;
1806 bool open_for_reading, open_for_writing, deny_read, deny_write;
1809 /* FIXME: hardcoded data fork, add resource fork */
1810 enum apple_fork fork_type = APPLE_FORK_DATA;
1812 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1814 access_mask & FILE_READ_DATA ? "READ" :"-",
1815 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1816 deny_mode & DENY_READ ? "DENY_READ" : "-",
1817 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1820 * Check read access and deny read mode
1822 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1824 open_for_reading = test_netatalk_lock(
1825 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1827 deny_read = test_netatalk_lock(
1828 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1830 DEBUG(10, ("read: %s, deny_write: %s\n",
1831 open_for_reading == true ? "yes" : "no",
1832 deny_read == true ? "yes" : "no"));
1834 if (((access_mask & FILE_READ_DATA) && deny_read)
1835 || ((deny_mode & DENY_READ) && open_for_reading)) {
1836 return NT_STATUS_SHARING_VIOLATION;
1840 if (access_mask & FILE_READ_DATA) {
1841 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1843 handle->conn->sconn->msg_ctx, fsp,
1844 fsp->op->global->open_persistent_id, 1, off,
1845 READ_LOCK, POSIX_LOCK, false,
1848 if (!NT_STATUS_IS_OK(status)) {
1851 TALLOC_FREE(br_lck);
1854 if (deny_mode & DENY_READ) {
1855 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1857 handle->conn->sconn->msg_ctx, fsp,
1858 fsp->op->global->open_persistent_id, 1, off,
1859 READ_LOCK, POSIX_LOCK, false,
1862 if (!NT_STATUS_IS_OK(status)) {
1865 TALLOC_FREE(br_lck);
1870 * Check write access and deny write mode
1872 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1874 open_for_writing = test_netatalk_lock(
1875 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1877 deny_write = test_netatalk_lock(
1878 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1880 DEBUG(10, ("write: %s, deny_write: %s\n",
1881 open_for_writing == true ? "yes" : "no",
1882 deny_write == true ? "yes" : "no"));
1884 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1885 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1886 return NT_STATUS_SHARING_VIOLATION;
1890 if (access_mask & FILE_WRITE_DATA) {
1891 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1893 handle->conn->sconn->msg_ctx, fsp,
1894 fsp->op->global->open_persistent_id, 1, off,
1895 READ_LOCK, POSIX_LOCK, false,
1898 if (!NT_STATUS_IS_OK(status)) {
1901 TALLOC_FREE(br_lck);
1904 if (deny_mode & DENY_WRITE) {
1905 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1907 handle->conn->sconn->msg_ctx, fsp,
1908 fsp->op->global->open_persistent_id, 1, off,
1909 READ_LOCK, POSIX_LOCK, false,
1912 if (!NT_STATUS_IS_OK(status)) {
1915 TALLOC_FREE(br_lck);
1919 TALLOC_FREE(br_lck);
1924 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1925 struct smb_request *req,
1926 const struct smb2_create_blobs *in_context_blobs,
1927 struct smb2_create_blobs *out_context_blobs)
1929 struct fruit_config_data *config;
1931 struct smb2_create_blob *aapl = NULL;
1935 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1936 uint64_t req_bitmap, client_caps;
1937 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1941 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1942 return NT_STATUS_UNSUCCESSFUL);
1944 if (!config->use_aapl
1945 || in_context_blobs == NULL
1946 || out_context_blobs == NULL) {
1947 return NT_STATUS_OK;
1950 aapl = smb2_create_blob_find(in_context_blobs,
1951 SMB2_CREATE_TAG_AAPL);
1953 return NT_STATUS_OK;
1956 if (aapl->data.length != 24) {
1957 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
1958 (uintmax_t)aapl->data.length));
1959 return NT_STATUS_INVALID_PARAMETER;
1962 cmd = IVAL(aapl->data.data, 0);
1963 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1964 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1965 return NT_STATUS_INVALID_PARAMETER;
1968 req_bitmap = BVAL(aapl->data.data, 8);
1969 client_caps = BVAL(aapl->data.data, 16);
1971 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1973 SBVAL(p, 8, req_bitmap);
1974 ok = data_blob_append(req, &blob, p, 16);
1976 return NT_STATUS_UNSUCCESSFUL;
1979 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1980 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1981 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1982 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1983 config->readdir_attr_enabled = true;
1986 if (config->use_copyfile) {
1987 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
1988 config->copyfile_enabled = true;
1992 * The client doesn't set the flag, so we can't check
1993 * for it and just set it unconditionally
1995 if (config->unix_info_enabled) {
1996 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1999 SBVAL(p, 0, server_caps);
2000 ok = data_blob_append(req, &blob, p, 8);
2002 return NT_STATUS_UNSUCCESSFUL;
2006 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2008 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
2009 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
2010 ok = data_blob_append(req, &blob, p, 8);
2012 return NT_STATUS_UNSUCCESSFUL;
2016 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2017 ok = convert_string_talloc(req,
2018 CH_UNIX, CH_UTF16LE,
2019 "Samba", strlen("Samba"),
2022 return NT_STATUS_UNSUCCESSFUL;
2026 SIVAL(p + 4, 0, modellen);
2027 ok = data_blob_append(req, &blob, p, 8);
2030 return NT_STATUS_UNSUCCESSFUL;
2033 ok = data_blob_append(req, &blob, model, modellen);
2036 return NT_STATUS_UNSUCCESSFUL;
2040 status = smb2_create_blob_add(out_context_blobs,
2042 SMB2_CREATE_TAG_AAPL,
2044 if (NT_STATUS_IS_OK(status)) {
2045 config->nego_aapl = true;
2051 static bool readdir_attr_meta_finderi_stream(
2052 struct vfs_handle_struct *handle,
2053 const struct smb_filename *smb_fname,
2056 struct smb_filename *stream_name = NULL;
2057 files_struct *fsp = NULL;
2062 uint8_t buf[AFP_INFO_SIZE];
2064 stream_name = synthetic_smb_fname(talloc_tos(),
2065 smb_fname->base_name,
2066 AFPINFO_STREAM_NAME,
2067 NULL, smb_fname->flags);
2068 if (stream_name == NULL) {
2072 ret = SMB_VFS_STAT(handle->conn, stream_name);
2077 status = SMB_VFS_CREATE_FILE(
2078 handle->conn, /* conn */
2080 0, /* root_dir_fid */
2081 stream_name, /* fname */
2082 FILE_READ_DATA, /* access_mask */
2083 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2085 FILE_OPEN, /* create_disposition*/
2086 0, /* create_options */
2087 0, /* file_attributes */
2088 INTERNAL_OPEN_ONLY, /* oplock_request */
2090 0, /* allocation_size */
2091 0, /* private_flags */
2096 NULL, NULL); /* create context */
2098 TALLOC_FREE(stream_name);
2100 if (!NT_STATUS_IS_OK(status)) {
2104 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2105 if (nread != AFP_INFO_SIZE) {
2106 DBG_ERR("short read [%s] [%zd/%d]\n",
2107 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2112 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2119 close_file(NULL, fsp, NORMAL_CLOSE);
2125 static bool readdir_attr_meta_finderi_netatalk(
2126 struct vfs_handle_struct *handle,
2127 const struct smb_filename *smb_fname,
2130 struct adouble *ad = NULL;
2133 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
2138 p = ad_get_entry(ad, ADEID_FINDERI);
2140 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2145 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2150 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2151 const struct smb_filename *smb_fname,
2152 struct readdir_attr_data *attr_data)
2154 struct fruit_config_data *config = NULL;
2155 uint32_t date_added;
2159 SMB_VFS_HANDLE_GET_DATA(handle, config,
2160 struct fruit_config_data,
2163 switch (config->meta) {
2164 case FRUIT_META_NETATALK:
2165 ok = readdir_attr_meta_finderi_netatalk(
2166 handle, smb_fname, &ai);
2169 case FRUIT_META_STREAM:
2170 ok = readdir_attr_meta_finderi_stream(
2171 handle, smb_fname, &ai);
2175 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2180 /* Don't bother with errors, it's likely ENOENT */
2184 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2186 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2187 &ai.afpi_FinderInfo[0], 4);
2189 /* finder_creator */
2190 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2191 &ai.afpi_FinderInfo[4], 4);
2195 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2196 &ai.afpi_FinderInfo[8], 2);
2198 /* finder_ext_flags */
2199 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2200 &ai.afpi_FinderInfo[24], 2);
2203 date_added = convert_time_t_to_uint32_t(
2204 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2206 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2211 static uint64_t readdir_attr_rfork_size_adouble(
2212 struct vfs_handle_struct *handle,
2213 const struct smb_filename *smb_fname)
2215 struct adouble *ad = NULL;
2216 uint64_t rfork_size;
2218 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
2224 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2230 static uint64_t readdir_attr_rfork_size_stream(
2231 struct vfs_handle_struct *handle,
2232 const struct smb_filename *smb_fname)
2234 struct smb_filename *stream_name = NULL;
2236 uint64_t rfork_size;
2238 stream_name = synthetic_smb_fname(talloc_tos(),
2239 smb_fname->base_name,
2240 AFPRESOURCE_STREAM_NAME,
2242 if (stream_name == NULL) {
2246 ret = SMB_VFS_STAT(handle->conn, stream_name);
2248 TALLOC_FREE(stream_name);
2252 rfork_size = stream_name->st.st_ex_size;
2253 TALLOC_FREE(stream_name);
2258 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2259 const struct smb_filename *smb_fname)
2261 struct fruit_config_data *config = NULL;
2262 uint64_t rfork_size;
2264 SMB_VFS_HANDLE_GET_DATA(handle, config,
2265 struct fruit_config_data,
2268 switch (config->rsrc) {
2269 case FRUIT_RSRC_ADFILE:
2270 case FRUIT_RSRC_XATTR:
2271 rfork_size = readdir_attr_rfork_size_adouble(handle,
2275 case FRUIT_META_STREAM:
2276 rfork_size = readdir_attr_rfork_size_stream(handle,
2281 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2289 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2290 const struct smb_filename *smb_fname,
2291 struct readdir_attr_data *attr_data)
2293 NTSTATUS status = NT_STATUS_OK;
2294 struct fruit_config_data *config = NULL;
2297 SMB_VFS_HANDLE_GET_DATA(handle, config,
2298 struct fruit_config_data,
2299 return NT_STATUS_UNSUCCESSFUL);
2302 /* Ensure we return a default value in the creation_date field */
2303 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2306 * Resource fork length
2309 if (config->readdir_attr_rsize) {
2310 uint64_t rfork_size;
2312 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2313 attr_data->attr_data.aapl.rfork_size = rfork_size;
2320 if (config->readdir_attr_finder_info) {
2321 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2323 status = NT_STATUS_INTERNAL_ERROR;
2330 /* Search MS NFS style ACE with UNIX mode */
2331 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2333 const struct security_descriptor *psd,
2338 struct fruit_config_data *config = NULL;
2342 SMB_VFS_HANDLE_GET_DATA(handle, config,
2343 struct fruit_config_data,
2344 return NT_STATUS_UNSUCCESSFUL);
2346 if (psd->dacl == NULL || !config->unix_info_enabled) {
2347 return NT_STATUS_OK;
2350 for (i = 0; i < psd->dacl->num_aces; i++) {
2351 if (dom_sid_compare_domain(
2352 &global_sid_Unix_NFS_Mode,
2353 &psd->dacl->aces[i].trustee) == 0) {
2354 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2355 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2358 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2359 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2364 return NT_STATUS_OK;
2367 /****************************************************************************
2369 ****************************************************************************/
2371 static int fruit_connect(vfs_handle_struct *handle,
2372 const char *service,
2376 char *list = NULL, *newlist = NULL;
2377 struct fruit_config_data *config;
2379 DEBUG(10, ("fruit_connect\n"));
2381 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2386 rc = init_fruit_config(handle);
2391 SMB_VFS_HANDLE_GET_DATA(handle, config,
2392 struct fruit_config_data, return -1);
2394 if (config->veto_appledouble) {
2395 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2398 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2399 newlist = talloc_asprintf(
2401 "%s/" ADOUBLE_NAME_PREFIX "*/",
2403 lp_do_parameter(SNUM(handle->conn),
2408 lp_do_parameter(SNUM(handle->conn),
2410 "/" ADOUBLE_NAME_PREFIX "*/");
2416 if (config->encoding == FRUIT_ENC_NATIVE) {
2420 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2421 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2422 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2423 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2424 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2425 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2426 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2427 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2428 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2429 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2436 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2437 struct smb_filename *smb_fname,
2443 char afpinfo_buf[AFP_INFO_SIZE];
2444 ssize_t len, written;
2448 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2453 if (!(flags & (O_CREAT | O_TRUNC))) {
2457 ai = afpinfo_new(talloc_tos());
2463 len = afpinfo_pack(ai, afpinfo_buf);
2464 if (len != AFP_INFO_SIZE) {
2469 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2470 fsp->fh->fd = hostfd;
2472 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2475 if (written != AFP_INFO_SIZE) {
2476 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2484 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2487 int saved_errno = errno;
2489 fsp->fh->fd = hostfd;
2490 SMB_VFS_NEXT_CLOSE(handle, fsp);
2493 errno = saved_errno;
2498 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2499 struct smb_filename *smb_fname,
2505 struct smb_filename *smb_fname_base = NULL;
2508 struct adouble *ad = NULL;
2510 /* Create an smb_filename with stream_name == NULL. */
2511 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2512 smb_fname->base_name,
2517 if (smb_fname_base == NULL) {
2524 * We use baseflags to turn off nasty side-effects when opening the
2528 baseflags &= ~O_TRUNC;
2529 baseflags &= ~O_EXCL;
2530 baseflags &= ~O_CREAT;
2532 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2536 * It is legit to open a stream on a directory, but the base
2537 * fd has to be read-only.
2539 if ((hostfd == -1) && (errno == EISDIR)) {
2540 baseflags &= ~O_ACCMODE;
2541 baseflags |= O_RDONLY;
2542 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2546 TALLOC_FREE(smb_fname_base);
2553 if (flags & (O_CREAT | O_TRUNC)) {
2555 * The attribute does not exist or needs to be truncated,
2556 * create an AppleDouble EA
2558 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2559 handle, ADOUBLE_META, fsp);
2565 rc = ad_write(ad, smb_fname->base_name);
2571 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2572 handle, ADOUBLE_META, fsp);
2577 if (ad_read(ad, smb_fname->base_name) == -1) {
2584 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2586 int saved_errno = errno;
2589 * BUGBUGBUG -- we would need to call
2590 * fd_close_posix here, but we don't have a
2593 fsp->fh->fd = hostfd;
2597 errno = saved_errno;
2602 static int fruit_open_meta(vfs_handle_struct *handle,
2603 struct smb_filename *smb_fname,
2604 files_struct *fsp, int flags, mode_t mode)
2607 struct fruit_config_data *config = NULL;
2609 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2611 SMB_VFS_HANDLE_GET_DATA(handle, config,
2612 struct fruit_config_data, return -1);
2614 switch (config->meta) {
2615 case FRUIT_META_STREAM:
2616 rc = fruit_open_meta_stream(handle, smb_fname,
2620 case FRUIT_META_NETATALK:
2621 rc = fruit_open_meta_netatalk(handle, smb_fname,
2626 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2630 DBG_DEBUG("path [%s] rc [%d]\n", smb_fname_str_dbg(smb_fname), rc);
2634 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2635 struct smb_filename *smb_fname,
2641 struct adouble *ad = NULL;
2642 struct smb_filename *smb_fname_base = NULL;
2643 char *adpath = NULL;
2646 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2647 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2654 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2655 /* sorry, but directories don't habe a resource fork */
2660 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2665 /* Create an smb_filename with stream_name == NULL. */
2666 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2671 if (smb_fname_base == NULL) {
2677 /* Sanitize flags */
2678 if (flags & O_WRONLY) {
2679 /* We always need read access for the metadata header too */
2684 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2691 /* REVIEW: we need this in ad_write() */
2692 fsp->fh->fd = hostfd;
2694 if (flags & (O_CREAT | O_TRUNC)) {
2695 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2696 handle, ADOUBLE_RSRC, fsp);
2701 rc = ad_write(ad, smb_fname->base_name);
2707 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2708 handle, ADOUBLE_RSRC, fsp);
2713 if (ad_read(ad, smb_fname->base_name) == -1) {
2721 TALLOC_FREE(adpath);
2722 TALLOC_FREE(smb_fname_base);
2724 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2726 int saved_errno = errno;
2729 * BUGBUGBUG -- we would need to call
2730 * fd_close_posix here, but we don't have a
2733 fsp->fh->fd = hostfd;
2737 errno = saved_errno;
2742 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2743 struct smb_filename *smb_fname,
2748 #ifdef HAVE_ATTROPEN
2750 struct adouble *ad = NULL;
2752 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2753 handle, ADOUBLE_RSRC, fsp);
2758 fd = attropen(smb_fname->base_name,
2759 AFPRESOURCE_EA_NETATALK, flags, mode);
2772 static int fruit_open_rsrc(vfs_handle_struct *handle,
2773 struct smb_filename *smb_fname,
2774 files_struct *fsp, int flags, mode_t mode)
2777 struct fruit_config_data *config = NULL;
2779 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2781 SMB_VFS_HANDLE_GET_DATA(handle, config,
2782 struct fruit_config_data, return -1);
2784 switch (config->rsrc) {
2785 case FRUIT_RSRC_STREAM:
2786 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2789 case FRUIT_RSRC_ADFILE:
2790 fd = fruit_open_rsrc_adouble(handle, smb_fname,
2794 case FRUIT_RSRC_XATTR:
2795 fd = fruit_open_rsrc_xattr(handle, smb_fname,
2800 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2804 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2808 static int fruit_open(vfs_handle_struct *handle,
2809 struct smb_filename *smb_fname,
2810 files_struct *fsp, int flags, mode_t mode)
2812 DEBUG(10, ("fruit_open called for %s\n",
2813 smb_fname_str_dbg(smb_fname)));
2815 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2816 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2819 if (is_afpinfo_stream(smb_fname)) {
2820 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2821 } else if (is_afpresource_stream(smb_fname)) {
2822 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2825 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2828 static int fruit_rename(struct vfs_handle_struct *handle,
2829 const struct smb_filename *smb_fname_src,
2830 const struct smb_filename *smb_fname_dst)
2833 char *src_adouble_path = NULL;
2834 char *dst_adouble_path = NULL;
2835 struct fruit_config_data *config = NULL;
2836 struct smb_filename *src_adp_smb_fname = NULL;
2837 struct smb_filename *dst_adp_smb_fname = NULL;
2839 SMB_VFS_HANDLE_GET_DATA(handle, config,
2840 struct fruit_config_data, return -1);
2842 if (!VALID_STAT(smb_fname_src->st)) {
2843 DBG_ERR("Need valid stat for [%s]\n",
2844 smb_fname_str_dbg(smb_fname_src));
2848 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2853 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
2854 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
2859 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2864 src_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
2867 smb_fname_src->flags);
2868 TALLOC_FREE(src_adouble_path);
2869 if (src_adp_smb_fname == NULL) {
2874 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2879 dst_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
2882 smb_fname_dst->flags);
2883 TALLOC_FREE(dst_adouble_path);
2884 if (dst_adp_smb_fname == NULL) {
2889 DBG_DEBUG("%s -> %s\n",
2890 smb_fname_str_dbg(src_adp_smb_fname),
2891 smb_fname_str_dbg(dst_adp_smb_fname));
2893 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
2894 if (errno == ENOENT) {
2899 TALLOC_FREE(src_adp_smb_fname);
2900 TALLOC_FREE(dst_adp_smb_fname);
2904 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
2905 const struct smb_filename *smb_fname)
2907 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2910 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
2911 const struct smb_filename *smb_fname)
2913 return SMB_VFS_REMOVEXATTR(handle->conn,
2914 smb_fname->base_name,
2915 AFPINFO_EA_NETATALK);
2918 static int fruit_unlink_meta(vfs_handle_struct *handle,
2919 const struct smb_filename *smb_fname)
2921 struct fruit_config_data *config = NULL;
2924 SMB_VFS_HANDLE_GET_DATA(handle, config,
2925 struct fruit_config_data, return -1);
2927 switch (config->meta) {
2928 case FRUIT_META_STREAM:
2929 rc = fruit_unlink_meta_stream(handle, smb_fname);
2932 case FRUIT_META_NETATALK:
2933 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
2937 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
2944 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
2945 const struct smb_filename *smb_fname,
2950 if (!force_unlink) {
2951 struct smb_filename *smb_fname_cp = NULL;
2954 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
2955 if (smb_fname_cp == NULL) {
2960 * 0 byte resource fork streams are not listed by
2961 * vfs_streaminfo, as a result stream cleanup/deletion of file
2962 * deletion doesn't remove the resourcefork stream.
2965 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
2967 TALLOC_FREE(smb_fname_cp);
2968 DBG_ERR("stat [%s] failed [%s]\n",
2969 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
2973 size = smb_fname_cp->st.st_ex_size;
2974 TALLOC_FREE(smb_fname_cp);
2977 /* OS X ignores resource fork stream delete requests */
2982 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2983 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
2990 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
2991 const struct smb_filename *smb_fname,
2996 struct adouble *ad = NULL;
2997 struct smb_filename *adp_smb_fname = NULL;
2999 if (!force_unlink) {
3000 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3009 * 0 byte resource fork streams are not listed by
3010 * vfs_streaminfo, as a result stream cleanup/deletion of file
3011 * deletion doesn't remove the resourcefork stream.
3014 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3015 /* OS X ignores resource fork stream delete requests */
3023 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
3028 adp_smb_fname = synthetic_smb_fname(talloc_tos(), adp,
3032 if (adp_smb_fname == NULL) {
3036 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3037 TALLOC_FREE(adp_smb_fname);
3038 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3045 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3046 const struct smb_filename *smb_fname,
3050 * OS X ignores resource fork stream delete requests, so nothing to do
3051 * here. Removing the file will remove the xattr anyway, so we don't
3052 * have to take care of removing 0 byte resource forks that could be
3058 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3059 const struct smb_filename *smb_fname,
3062 struct fruit_config_data *config = NULL;
3065 SMB_VFS_HANDLE_GET_DATA(handle, config,
3066 struct fruit_config_data, return -1);
3068 switch (config->rsrc) {
3069 case FRUIT_RSRC_STREAM:
3070 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3073 case FRUIT_RSRC_ADFILE:
3074 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3077 case FRUIT_RSRC_XATTR:
3078 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3082 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3089 static int fruit_unlink(vfs_handle_struct *handle,
3090 const struct smb_filename *smb_fname)
3093 struct fruit_config_data *config = NULL;
3094 struct smb_filename *rsrc_smb_fname = NULL;
3096 SMB_VFS_HANDLE_GET_DATA(handle, config,
3097 struct fruit_config_data, return -1);
3099 if (is_afpinfo_stream(smb_fname)) {
3100 return fruit_unlink_meta(handle, smb_fname);
3101 } else if (is_afpresource_stream(smb_fname)) {
3102 return fruit_unlink_rsrc(handle, smb_fname, false);
3103 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3104 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3108 * A request to delete the base file. Because 0 byte resource
3109 * fork streams are not listed by fruit_streaminfo,
3110 * delete_all_streams() can't remove 0 byte resource fork
3111 * streams, so we have to cleanup this here.
3113 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3114 smb_fname->base_name,
3115 AFPRESOURCE_STREAM_NAME,
3118 if (rsrc_smb_fname == NULL) {
3122 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3123 if ((rc != 0) && (errno != ENOENT)) {
3124 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3125 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3126 TALLOC_FREE(rsrc_smb_fname);
3129 TALLOC_FREE(rsrc_smb_fname);
3131 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3134 static int fruit_chmod(vfs_handle_struct *handle,
3135 const struct smb_filename *smb_fname,
3140 struct fruit_config_data *config = NULL;
3141 const char *path = smb_fname->base_name;
3142 struct smb_filename *smb_fname_adp = NULL;
3144 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3149 SMB_VFS_HANDLE_GET_DATA(handle, config,
3150 struct fruit_config_data, return -1);
3152 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3156 if (!VALID_STAT(smb_fname->st)) {
3160 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3164 rc = adouble_path(talloc_tos(), path, &adp);
3169 DEBUG(10, ("fruit_chmod: %s\n", adp));
3171 smb_fname_adp = synthetic_smb_fname(talloc_tos(),
3176 if (smb_fname_adp == NULL) {
3182 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3183 if (errno == ENOENT) {
3187 TALLOC_FREE(smb_fname_adp);
3192 static int fruit_chown(vfs_handle_struct *handle,
3193 const struct smb_filename *smb_fname,
3199 struct fruit_config_data *config = NULL;
3200 struct smb_filename *adp_smb_fname = NULL;
3202 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3207 SMB_VFS_HANDLE_GET_DATA(handle, config,
3208 struct fruit_config_data, return -1);
3210 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3214 if (!VALID_STAT(smb_fname->st)) {
3218 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3222 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
3227 DEBUG(10, ("fruit_chown: %s\n", adp));
3229 adp_smb_fname = synthetic_smb_fname(talloc_tos(),
3234 if (adp_smb_fname == NULL) {
3240 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3241 if (errno == ENOENT) {
3247 TALLOC_FREE(adp_smb_fname);
3251 static int fruit_rmdir(struct vfs_handle_struct *handle,
3252 const struct smb_filename *smb_fname)
3256 struct fruit_config_data *config;
3258 SMB_VFS_HANDLE_GET_DATA(handle, config,
3259 struct fruit_config_data, return -1);
3261 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3266 * Due to there is no way to change bDeleteVetoFiles variable
3267 * from this module, need to clean up ourselves
3270 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3275 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3277 struct adouble *ad = NULL;
3279 struct smb_filename *ad_smb_fname = NULL;
3282 match = strncmp(de->d_name,
3283 ADOUBLE_NAME_PREFIX,
3284 strlen(ADOUBLE_NAME_PREFIX));
3289 p = talloc_asprintf(talloc_tos(), "%s/%s",
3290 smb_fname->base_name, de->d_name);
3292 DBG_ERR("talloc_asprintf failed\n");
3297 * Check whether it's a valid AppleDouble file, if
3298 * yes, delete it, ignore it otherwise.
3300 ad = ad_get(talloc_tos(), handle, p, ADOUBLE_RSRC);
3307 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3311 if (ad_smb_fname == NULL) {
3312 DBG_ERR("synthetic_smb_fname failed\n");
3316 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3317 TALLOC_FREE(ad_smb_fname);
3319 DBG_ERR("Deleting [%s] failed\n",
3320 smb_fname_str_dbg(ad_smb_fname));
3328 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3331 static ssize_t fruit_pread(vfs_handle_struct *handle,
3332 files_struct *fsp, void *data,
3333 size_t n, off_t offset)
3336 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3338 struct fruit_config_data *config = NULL;
3342 char *tmp_base_name = NULL;
3344 size_t to_return = n;
3346 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
3348 if (!fsp->base_fsp) {
3349 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3352 SMB_VFS_HANDLE_GET_DATA(handle, config,
3353 struct fruit_config_data, return -1);
3355 /* fsp_name is not converted with vfs_catia */
3356 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3357 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
3358 fsp->base_fsp->fsp_name->base_name,
3359 vfs_translate_to_unix,
3360 talloc_tos(), &name);
3361 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3362 name = talloc_strdup(talloc_tos(), tmp_base_name);
3367 } else if (!NT_STATUS_IS_OK(status)) {
3368 errno = map_errno_from_nt_status(status);
3372 fsp->base_fsp->fsp_name->base_name = name;
3374 if (is_afpinfo_stream(fsp->fsp_name)) {
3376 * OS X has a off-by-1 error in the offset calculation, so we're
3377 * bug compatible here. It won't hurt, as any relevant real
3378 * world read requests from the AFP_AfpInfo stream will be
3379 * offset=0 n=60. offset is ignored anyway, see below.
3381 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3387 to_return = MIN(n, AFP_INFO_SIZE);
3389 /* Yes, macOS always reads from offset 0 */
3394 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, to_return, offset);
3402 if (!fruit_fsp_recheck(ad, fsp)) {
3407 if (ad->ad_type == ADOUBLE_META) {
3408 char afpinfo_buf[AFP_INFO_SIZE];
3411 ai = afpinfo_new(talloc_tos());
3417 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
3423 p = ad_get_entry(ad, ADEID_FINDERI);
3425 DBG_ERR("No ADEID_FINDERI for [%s]\n",
3426 fsp->fsp_name->base_name);
3431 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3433 len = afpinfo_pack(ai, afpinfo_buf);
3434 if (len != AFP_INFO_SIZE) {
3440 * OS X ignores offset when reading from AFP_AfpInfo stream!
3442 memcpy(data, afpinfo_buf, to_return);
3445 len = SMB_VFS_NEXT_PREAD(
3446 handle, fsp, data, n,
3447 offset + ad_getentryoff(ad, ADEID_RFORK));
3454 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3460 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
3464 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
3465 files_struct *fsp, const void *data,
3466 size_t n, off_t offset)
3469 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3471 struct fruit_config_data *config = NULL;
3475 char *tmp_base_name = NULL;
3478 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
3480 if (!fsp->base_fsp) {
3481 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3484 SMB_VFS_HANDLE_GET_DATA(handle, config,
3485 struct fruit_config_data, return -1);
3487 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3488 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
3489 fsp->base_fsp->fsp_name->base_name,
3490 vfs_translate_to_unix,
3491 talloc_tos(), &name);
3492 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3493 name = talloc_strdup(talloc_tos(), tmp_base_name);
3498 } else if (!NT_STATUS_IS_OK(status)) {
3499 errno = map_errno_from_nt_status(status);
3503 fsp->base_fsp->fsp_name->base_name = name;
3505 if (is_afpinfo_stream(fsp->fsp_name)) {
3507 * Writing an all 0 blob to the metadata stream
3508 * results in the stream being removed on a macOS
3509 * server. This ensures we behave the same and it
3510 * verified by the "delete AFP_AfpInfo by writing all
3513 if (n != AFP_INFO_SIZE || offset != 0) {
3514 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
3515 (intmax_t)offset, (intmax_t)n));
3519 ai = afpinfo_unpack(talloc_tos(), data);
3525 if (ai_empty_finderinfo(ai)) {
3526 switch (config->meta) {
3527 case FRUIT_META_STREAM:
3528 rc = SMB_VFS_UNLINK(handle->conn, fsp->fsp_name);
3531 case FRUIT_META_NETATALK:
3532 rc = SMB_VFS_REMOVEXATTR(
3534 fsp->fsp_name->base_name,
3535 AFPINFO_EA_NETATALK);
3539 DBG_ERR("Unexpected meta config [%d]\n",
3545 if (rc != 0 && errno != ENOENT && errno != ENOATTR) {
3546 DBG_WARNING("Can't delete metadata for %s: %s\n",
3547 fsp->fsp_name->base_name, strerror(errno));
3557 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3565 if (!fruit_fsp_recheck(ad, fsp)) {
3570 if (ad->ad_type == ADOUBLE_META) {
3573 p = ad_get_entry(ad, ADEID_FINDERI);
3575 DBG_ERR("No ADEID_FINDERI for [%s]\n",
3576 fsp->fsp_name->base_name);
3581 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3582 rc = ad_write(ad, name);
3584 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3585 offset + ad_getentryoff(ad, ADEID_RFORK));
3591 if (config->rsrc == FRUIT_RSRC_ADFILE) {
3592 rc = ad_read(ad, name);
3598 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3599 ad_setentrylen(ad, ADEID_RFORK, len + offset);
3600 rc = ad_write(ad, name);
3606 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3616 * Helper to stat/lstat the base file of an smb_fname.
3618 static int fruit_stat_base(vfs_handle_struct *handle,
3619 struct smb_filename *smb_fname,
3622 char *tmp_stream_name;
3625 tmp_stream_name = smb_fname->stream_name;
3626 smb_fname->stream_name = NULL;
3628 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3630 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3632 smb_fname->stream_name = tmp_stream_name;
3636 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3637 struct smb_filename *smb_fname,
3643 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3645 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3651 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3652 struct smb_filename *smb_fname,
3655 struct adouble *ad = NULL;
3657 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3659 DBG_INFO("fruit_stat_meta %s: %s\n",
3660 smb_fname_str_dbg(smb_fname), strerror(errno));
3666 /* Populate the stat struct with info from the base file. */
3667 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
3670 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
3671 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
3672 smb_fname->stream_name);
3676 static int fruit_stat_meta(vfs_handle_struct *handle,
3677 struct smb_filename *smb_fname,
3680 struct fruit_config_data *config = NULL;
3683 SMB_VFS_HANDLE_GET_DATA(handle, config,
3684 struct fruit_config_data, return -1);
3686 switch (config->meta) {
3687 case FRUIT_META_STREAM:
3688 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
3691 case FRUIT_META_NETATALK:
3692 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
3696 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3703 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
3704 struct smb_filename *smb_fname,
3707 struct adouble *ad = NULL;
3710 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
3716 /* Populate the stat struct with info from the base file. */
3717 ret = fruit_stat_base(handle, smb_fname, follow_links);
3723 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3724 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
3725 smb_fname->stream_name);
3730 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
3731 struct smb_filename *smb_fname,
3737 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3739 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3745 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
3746 struct smb_filename *smb_fname,
3749 #ifdef HAVE_ATTROPEN
3753 /* Populate the stat struct with info from the base file. */
3754 ret = fruit_stat_base(handle, smb_fname, follow_links);
3759 fd = attropen(smb_fname->base_name,
3760 AFPRESOURCE_EA_NETATALK,
3766 ret = sys_fstat(fd, &smb_fname->st, false);
3769 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
3770 AFPRESOURCE_EA_NETATALK);
3776 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
3777 smb_fname->stream_name);
3787 static int fruit_stat_rsrc(vfs_handle_struct *handle,
3788 struct smb_filename *smb_fname,
3791 struct fruit_config_data *config = NULL;
3794 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3796 SMB_VFS_HANDLE_GET_DATA(handle, config,
3797 struct fruit_config_data, return -1);
3799 switch (config->rsrc) {
3800 case FRUIT_RSRC_STREAM:
3801 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
3804 case FRUIT_RSRC_XATTR:
3805 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
3808 case FRUIT_RSRC_ADFILE:
3809 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
3813 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3820 static int fruit_stat(vfs_handle_struct *handle,
3821 struct smb_filename *smb_fname)
3825 DEBUG(10, ("fruit_stat called for %s\n",
3826 smb_fname_str_dbg(smb_fname)));
3828 if (!is_ntfs_stream_smb_fname(smb_fname)
3829 || is_ntfs_default_stream_smb_fname(smb_fname)) {
3830 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3832 update_btime(handle, smb_fname);
3838 * Note if lp_posix_paths() is true, we can never
3839 * get here as is_ntfs_stream_smb_fname() is
3840 * always false. So we never need worry about
3841 * not following links here.
3844 if (is_afpinfo_stream(smb_fname)) {
3845 rc = fruit_stat_meta(handle, smb_fname, true);
3846 } else if (is_afpresource_stream(smb_fname)) {
3847 rc = fruit_stat_rsrc(handle, smb_fname, true);
3849 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3853 update_btime(handle, smb_fname);
3854 smb_fname->st.st_ex_mode &= ~S_IFMT;
3855 smb_fname->st.st_ex_mode |= S_IFREG;
3856 smb_fname->st.st_ex_blocks =
3857 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3862 static int fruit_lstat(vfs_handle_struct *handle,
3863 struct smb_filename *smb_fname)
3867 DEBUG(10, ("fruit_lstat called for %s\n",
3868 smb_fname_str_dbg(smb_fname)));
3870 if (!is_ntfs_stream_smb_fname(smb_fname)
3871 || is_ntfs_default_stream_smb_fname(smb_fname)) {
3872 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3874 update_btime(handle, smb_fname);
3879 if (is_afpinfo_stream(smb_fname)) {
3880 rc = fruit_stat_meta(handle, smb_fname, false);
3881 } else if (is_afpresource_stream(smb_fname)) {
3882 rc = fruit_stat_rsrc(handle, smb_fname, false);
3884 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3888 update_btime(handle, smb_fname);
3889 smb_fname->st.st_ex_mode &= ~S_IFMT;
3890 smb_fname->st.st_ex_mode |= S_IFREG;
3891 smb_fname->st.st_ex_blocks =
3892 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3897 static int fruit_fstat_meta(vfs_handle_struct *handle,
3899 SMB_STRUCT_STAT *sbuf)
3901 DEBUG(10, ("fruit_fstat_meta called for %s\n",
3902 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3904 /* Populate the stat struct with info from the base file. */
3905 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3908 *sbuf = fsp->base_fsp->fsp_name->st;
3909 sbuf->st_ex_size = AFP_INFO_SIZE;
3910 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3915 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3916 SMB_STRUCT_STAT *sbuf)
3918 struct fruit_config_data *config;
3919 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3922 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
3923 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3925 SMB_VFS_HANDLE_GET_DATA(handle, config,
3926 struct fruit_config_data, return -1);
3928 if (config->rsrc == FRUIT_RSRC_STREAM) {
3929 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3932 /* Populate the stat struct with info from the base file. */
3933 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3936 *sbuf = fsp->base_fsp->fsp_name->st;
3937 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3938 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3940 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3941 smb_fname_str_dbg(fsp->fsp_name),
3942 (ssize_t)sbuf->st_ex_size));
3947 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3948 SMB_STRUCT_STAT *sbuf)
3952 char *tmp_base_name = NULL;
3954 struct adouble *ad = (struct adouble *)
3955 VFS_FETCH_FSP_EXTENSION(handle, fsp);
3957 DEBUG(10, ("fruit_fstat called for %s\n",
3958 smb_fname_str_dbg(fsp->fsp_name)));
3960 if (fsp->base_fsp) {
3961 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3962 /* fsp_name is not converted with vfs_catia */
3963 status = SMB_VFS_TRANSLATE_NAME(
3965 fsp->base_fsp->fsp_name->base_name,
3966 vfs_translate_to_unix,
3967 talloc_tos(), &name);
3969 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3970 name = talloc_strdup(talloc_tos(), tmp_base_name);
3975 } else if (!NT_STATUS_IS_OK(status)) {
3976 errno = map_errno_from_nt_status(status);
3980 fsp->base_fsp->fsp_name->base_name = name;
3983 if (ad == NULL || fsp->base_fsp == NULL) {
3984 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3988 if (!fruit_fsp_recheck(ad, fsp)) {
3993 switch (ad->ad_type) {
3995 rc = fruit_fstat_meta(handle, fsp, sbuf);
3998 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
4001 DEBUG(10, ("fruit_fstat %s: bad type\n",
4002 smb_fname_str_dbg(fsp->fsp_name)));
4008 sbuf->st_ex_mode &= ~S_IFMT;
4009 sbuf->st_ex_mode |= S_IFREG;
4010 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4014 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
4015 smb_fname_str_dbg(fsp->fsp_name),
4016 (ssize_t)sbuf->st_ex_size));
4017 if (tmp_base_name) {
4018 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
4024 static NTSTATUS fruit_streaminfo_meta_stream(
4025 vfs_handle_struct *handle,
4026 struct files_struct *fsp,
4027 const struct smb_filename *smb_fname,
4028 TALLOC_CTX *mem_ctx,
4029 unsigned int *pnum_streams,
4030 struct stream_struct **pstreams)
4032 return NT_STATUS_OK;
4035 static NTSTATUS fruit_streaminfo_meta_netatalk(
4036 vfs_handle_struct *handle,
4037 struct files_struct *fsp,
4038 const struct smb_filename *smb_fname,
4039 TALLOC_CTX *mem_ctx,
4040 unsigned int *pnum_streams,
4041 struct stream_struct **pstreams)
4043 struct adouble *ad = NULL;
4047 /* Remove the Netatalk xattr from the list */
4048 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4049 ":" NETATALK_META_XATTR ":$DATA");
4051 return NT_STATUS_NO_MEMORY;
4054 ad = ad_get(talloc_tos(), handle,
4055 smb_fname->base_name, ADOUBLE_META);
4057 return NT_STATUS_OK;
4060 is_fi_empty = ad_empty_finderinfo(ad);
4064 return NT_STATUS_OK;
4067 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4068 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4069 smb_roundup(handle->conn, AFP_INFO_SIZE));
4071 return NT_STATUS_NO_MEMORY;
4074 return NT_STATUS_OK;
4077 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4078 struct files_struct *fsp,
4079 const struct smb_filename *smb_fname,
4080 TALLOC_CTX *mem_ctx,
4081 unsigned int *pnum_streams,
4082 struct stream_struct **pstreams)
4084 struct fruit_config_data *config = NULL;
4087 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4088 return NT_STATUS_INTERNAL_ERROR);
4090 switch (config->meta) {
4091 case FRUIT_META_NETATALK:
4092 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4093 mem_ctx, pnum_streams,
4097 case FRUIT_META_STREAM:
4098 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4099 mem_ctx, pnum_streams,
4104 return NT_STATUS_INTERNAL_ERROR;
4110 static NTSTATUS fruit_streaminfo_rsrc_stream(
4111 vfs_handle_struct *handle,
4112 struct files_struct *fsp,
4113 const struct smb_filename *smb_fname,
4114 TALLOC_CTX *mem_ctx,
4115 unsigned int *pnum_streams,
4116 struct stream_struct **pstreams)
4120 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4122 DBG_ERR("Filtering resource stream failed\n");
4123 return NT_STATUS_INTERNAL_ERROR;
4125 return NT_STATUS_OK;
4128 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4129 vfs_handle_struct *handle,
4130 struct files_struct *fsp,
4131 const struct smb_filename *smb_fname,
4132 TALLOC_CTX *mem_ctx,
4133 unsigned int *pnum_streams,
4134 struct stream_struct **pstreams)
4138 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4140 DBG_ERR("Filtering resource stream failed\n");
4141 return NT_STATUS_INTERNAL_ERROR;
4143 return NT_STATUS_OK;
4146 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4147 vfs_handle_struct *handle,
4148 struct files_struct *fsp,
4149 const struct smb_filename *smb_fname,
4150 TALLOC_CTX *mem_ctx,
4151 unsigned int *pnum_streams,
4152 struct stream_struct **pstreams)
4154 struct stream_struct *stream = *pstreams;
4155 unsigned int num_streams = *pnum_streams;
4156 struct adouble *ad = NULL;
4162 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4163 * and if yes, remove it from the list
4165 for (i = 0; i < num_streams; i++) {
4166 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4171 if (i < num_streams) {
4172 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4173 smb_fname_str_dbg(smb_fname));
4175 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4176 AFPRESOURCE_STREAM);
4178 return NT_STATUS_INTERNAL_ERROR;
4182 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
4185 return NT_STATUS_OK;
4188 rlen = ad_getentrylen(ad, ADEID_RFORK);
4192 return NT_STATUS_OK;
4195 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4196 AFPRESOURCE_STREAM_NAME, rlen,
4197 smb_roundup(handle->conn, rlen));
4199 return NT_STATUS_NO_MEMORY;
4202 return NT_STATUS_OK;
4205 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4206 struct files_struct *fsp,
4207 const struct smb_filename *smb_fname,
4208 TALLOC_CTX *mem_ctx,
4209 unsigned int *pnum_streams,
4210 struct stream_struct **pstreams)
4212 struct fruit_config_data *config = NULL;
4215 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4216 return NT_STATUS_INTERNAL_ERROR);
4218 switch (config->rsrc) {
4219 case FRUIT_RSRC_STREAM:
4220 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4221 mem_ctx, pnum_streams,
4225 case FRUIT_RSRC_XATTR:
4226 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4227 mem_ctx, pnum_streams,
4231 case FRUIT_RSRC_ADFILE:
4232 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4233 mem_ctx, pnum_streams,
4238 return NT_STATUS_INTERNAL_ERROR;
4244 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4245 struct files_struct *fsp,
4246 const struct smb_filename *smb_fname,
4247 TALLOC_CTX *mem_ctx,
4248 unsigned int *pnum_streams,
4249 struct stream_struct **pstreams)
4251 struct fruit_config_data *config = NULL;
4254 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4255 return NT_STATUS_UNSUCCESSFUL);
4257 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4259 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4260 pnum_streams, pstreams);
4261 if (!NT_STATUS_IS_OK(status)) {
4265 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4266 mem_ctx, pnum_streams, pstreams);
4267 if (!NT_STATUS_IS_OK(status)) {
4271 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4272 mem_ctx, pnum_streams, pstreams);
4273 if (!NT_STATUS_IS_OK(status)) {
4277 return NT_STATUS_OK;
4280 static int fruit_ntimes(vfs_handle_struct *handle,
4281 const struct smb_filename *smb_fname,
4282 struct smb_file_time *ft)
4285 struct adouble *ad = NULL;
4286 struct fruit_config_data *config = NULL;
4288 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4291 if ((config->meta != FRUIT_META_NETATALK) ||
4292 null_timespec(ft->create_time))
4294 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4297 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4298 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4300 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
4305 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4306 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4308 rc = ad_write(ad, smb_fname->base_name);
4314 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4317 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4320 static int fruit_fallocate(struct vfs_handle_struct *handle,
4321 struct files_struct *fsp,
4326 struct adouble *ad =
4327 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4330 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4333 if (!fruit_fsp_recheck(ad, fsp)) {
4337 /* Let the pwrite code path handle it. */
4342 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4343 struct files_struct *fsp,
4347 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4350 #ifdef HAVE_ATTROPEN
4351 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4356 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4357 struct files_struct *fsp,
4361 struct adouble *ad =
4362 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4363 off_t ad_off = ad_getentryoff(ad, ADEID_RFORK);
4365 if (!fruit_fsp_recheck(ad, fsp)) {
4369 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset + ad_off);
4374 ad_setentrylen(ad, ADEID_RFORK, offset);
4376 rc = ad_write(ad, NULL);
4378 DBG_ERR("ad_write [%s] failed [%s]\n",
4379 fsp_str_dbg(fsp), strerror(errno));
4383 DBG_DEBUG("Path [%s] offset [%jd]\n",
4384 fsp_str_dbg(fsp), (intmax_t)offset);
4389 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4390 struct files_struct *fsp,
4394 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4397 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4400 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4401 struct files_struct *fsp,
4405 struct fruit_config_data *config;
4407 SMB_VFS_HANDLE_GET_DATA(handle, config,
4408 struct fruit_config_data, return -1);
4410 switch (config->rsrc) {
4411 case FRUIT_RSRC_XATTR:
4412 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4415 case FRUIT_RSRC_ADFILE:
4416 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4419 case FRUIT_RSRC_STREAM:
4420 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4424 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4432 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4433 struct files_struct *fsp,
4436 DBG_DEBUG("fruit_ftruncate called for file %s offset %.0f\n",
4437 fsp_str_dbg(fsp), (double)offset);
4439 if (is_afpinfo_stream(fsp->fsp_name)) {
4441 DBG_WARNING("ftruncate %s to %jd",
4442 fsp_str_dbg(fsp), (intmax_t)offset);
4443 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4448 DBG_WARNING("ignoring ftruncate %s to %jd",
4449 fsp_str_dbg(fsp), (intmax_t)offset);
4450 /* OS X returns success but does nothing */
4454 if (is_afpresource_stream(fsp->fsp_name)) {
4455 return fruit_ftruncate_rsrc(handle, fsp, offset);
4458 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4461 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4462 struct smb_request *req,
4463 uint16_t root_dir_fid,
4464 struct smb_filename *smb_fname,
4465 uint32_t access_mask,
4466 uint32_t share_access,
4467 uint32_t create_disposition,
4468 uint32_t create_options,
4469 uint32_t file_attributes,
4470 uint32_t oplock_request,
4471 struct smb2_lease *lease,
4472 uint64_t allocation_size,
4473 uint32_t private_flags,
4474 struct security_descriptor *sd,
4475 struct ea_list *ea_list,
4476 files_struct **result,
4478 const struct smb2_create_blobs *in_context_blobs,
4479 struct smb2_create_blobs *out_context_blobs)
4482 struct fruit_config_data *config = NULL;
4483 files_struct *fsp = NULL;
4485 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4486 if (!NT_STATUS_IS_OK(status)) {
4490 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4491 return NT_STATUS_UNSUCCESSFUL);
4493 status = SMB_VFS_NEXT_CREATE_FILE(
4494 handle, req, root_dir_fid, smb_fname,
4495 access_mask, share_access,
4496 create_disposition, create_options,
4497 file_attributes, oplock_request,
4499 allocation_size, private_flags,
4500 sd, ea_list, result,
4501 pinfo, in_context_blobs, out_context_blobs);
4502 if (!NT_STATUS_IS_OK(status)) {
4508 if (config->nego_aapl) {
4509 if (config->copyfile_enabled) {
4511 * Set a flag in the fsp. Gets used in
4512 * copychunk to check whether the special
4513 * Apple copyfile semantics for copychunk
4514 * should be allowed in a copychunk request
4515 * with a count of 0.
4517 fsp->aapl_copyfile_supported = true;
4520 if (config->posix_rename && fsp->is_directory) {
4522 * Enable POSIX directory rename behaviour
4524 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4529 * If this is a plain open for existing files, opening an 0
4530 * byte size resource fork MUST fail with
4531 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4533 * Cf the vfs_fruit torture tests in test_rfork_create().
4535 if (is_afpresource_stream(fsp->fsp_name) &&
4536 create_disposition == FILE_OPEN)
4538 if (fsp->fsp_name->st.st_ex_size == 0) {
4539 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4544 if (is_ntfs_stream_smb_fname(smb_fname)
4545 || fsp->is_directory) {
4549 if (config->locking == FRUIT_LOCKING_NETATALK) {
4550 status = fruit_check_access(
4553 map_share_mode_to_deny_mode(share_access, 0));
4554 if (!NT_STATUS_IS_OK(status)) {
4562 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
4565 close_file(req, fsp, ERROR_CLOSE);
4566 *result = fsp = NULL;
4572 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
4573 const struct smb_filename *fname,
4574 TALLOC_CTX *mem_ctx,
4575 struct readdir_attr_data **pattr_data)
4577 struct fruit_config_data *config = NULL;
4578 struct readdir_attr_data *attr_data;
4581 SMB_VFS_HANDLE_GET_DATA(handle, config,
4582 struct fruit_config_data,
4583 return NT_STATUS_UNSUCCESSFUL);
4585 if (!config->nego_aapl) {
4586 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
4589 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
4591 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
4592 if (*pattr_data == NULL) {
4593 return NT_STATUS_UNSUCCESSFUL;
4595 attr_data = *pattr_data;
4596 attr_data->type = RDATTR_AAPL;
4599 * Mac metadata: compressed FinderInfo, resource fork length
4602 status = readdir_attr_macmeta(handle, fname, attr_data);
4603 if (!NT_STATUS_IS_OK(status)) {
4605 * Error handling is tricky: if we return failure from
4606 * this function, the corresponding directory entry
4607 * will to be passed to the client, so we really just
4608 * want to error out on fatal errors.
4610 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
4618 if (config->unix_info_enabled) {
4619 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
4625 if (!config->readdir_attr_max_access) {
4626 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
4628 status = smbd_calculate_access_mask(
4632 SEC_FLAG_MAXIMUM_ALLOWED,
4633 &attr_data->attr_data.aapl.max_access);
4634 if (!NT_STATUS_IS_OK(status)) {
4639 return NT_STATUS_OK;
4642 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4643 fname->base_name, nt_errstr(status)));
4644 TALLOC_FREE(*pattr_data);
4648 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
4650 uint32_t security_info,
4651 TALLOC_CTX *mem_ctx,
4652 struct security_descriptor **ppdesc)
4655 struct security_ace ace;
4657 struct fruit_config_data *config;
4659 SMB_VFS_HANDLE_GET_DATA(handle, config,
4660 struct fruit_config_data,
4661 return NT_STATUS_UNSUCCESSFUL);
4663 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
4665 if (!NT_STATUS_IS_OK(status)) {
4670 * Add MS NFS style ACEs with uid, gid and mode
4672 if (!config->unix_info_enabled) {
4673 return NT_STATUS_OK;
4676 /* MS NFS style mode */
4677 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
4678 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4679 status = security_descriptor_dacl_add(*ppdesc, &ace);
4680 if (!NT_STATUS_IS_OK(status)) {
4681 DEBUG(1,("failed to add MS NFS style ACE\n"));
4685 /* MS NFS style uid */
4686 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
4687 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4688 status = security_descriptor_dacl_add(*ppdesc, &ace);
4689 if (!NT_STATUS_IS_OK(status)) {
4690 DEBUG(1,("failed to add MS NFS style ACE\n"));
4694 /* MS NFS style gid */
4695 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
4696 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
4697 status = security_descriptor_dacl_add(*ppdesc, &ace);
4698 if (!NT_STATUS_IS_OK(status)) {
4699 DEBUG(1,("failed to add MS NFS style ACE\n"));
4703 return NT_STATUS_OK;
4706 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
4708 uint32_t security_info_sent,
4709 const struct security_descriptor *psd)
4713 mode_t ms_nfs_mode = 0;
4716 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
4718 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
4719 if (!NT_STATUS_IS_OK(status)) {
4720 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
4724 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
4725 if (!NT_STATUS_IS_OK(status)) {
4726 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
4731 if (fsp->fh->fd != -1) {
4732 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
4734 result = SMB_VFS_CHMOD(fsp->conn,
4740 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
4741 result, (unsigned)ms_nfs_mode,
4743 status = map_nt_error_from_unix(errno);
4748 return NT_STATUS_OK;
4751 struct fruit_copy_chunk_state {
4752 struct vfs_handle_struct *handle;
4754 struct files_struct *src_fsp;
4755 struct files_struct *dst_fsp;
4759 static void fruit_copy_chunk_done(struct tevent_req *subreq);
4760 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
4761 TALLOC_CTX *mem_ctx,
4762 struct tevent_context *ev,
4763 struct files_struct *src_fsp,
4765 struct files_struct *dest_fsp,
4769 struct tevent_req *req, *subreq;
4770 struct fruit_copy_chunk_state *fruit_copy_chunk_state;
4772 struct fruit_config_data *config;
4773 off_t to_copy = num;
4775 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4776 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
4778 SMB_VFS_HANDLE_GET_DATA(handle, config,
4779 struct fruit_config_data,
4782 req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
4783 struct fruit_copy_chunk_state);
4787 fruit_copy_chunk_state->handle = handle;
4788 fruit_copy_chunk_state->src_fsp = src_fsp;
4789 fruit_copy_chunk_state->dst_fsp = dest_fsp;
4792 * Check if this a OS X copyfile style copychunk request with
4793 * a requested chunk count of 0 that was translated to a
4794 * copy_chunk_send VFS call overloading the parameters src_off
4795 * = dest_off = num = 0.
4797 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
4798 src_fsp->aapl_copyfile_supported &&
4799 dest_fsp->aapl_copyfile_supported)
4801 status = vfs_stat_fsp(src_fsp);
4802 if (tevent_req_nterror(req, status)) {
4803 return tevent_req_post(req, ev);
4806 to_copy = src_fsp->fsp_name->st.st_ex_size;
4807 fruit_copy_chunk_state->is_copyfile = true;
4810 subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
4818 if (tevent_req_nomem(subreq, req)) {
4819 return tevent_req_post(req, ev);
4822 tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
4826 static void fruit_copy_chunk_done(struct tevent_req *subreq)
4828 struct tevent_req *req = tevent_req_callback_data(
4829 subreq, struct tevent_req);
4830 struct fruit_copy_chunk_state *state = tevent_req_data(
4831 req, struct fruit_copy_chunk_state);
4833 unsigned int num_streams = 0;
4834 struct stream_struct *streams = NULL;
4836 struct smb_filename *src_fname_tmp = NULL;
4837 struct smb_filename *dst_fname_tmp = NULL;
4839 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
4842 TALLOC_FREE(subreq);
4843 if (tevent_req_nterror(req, status)) {
4847 if (!state->is_copyfile) {
4848 tevent_req_done(req);
4853 * Now copy all remaining streams. We know the share supports
4854 * streams, because we're in vfs_fruit. We don't do this async
4855 * because streams are few and small.
4857 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
4858 state->src_fsp->fsp_name,
4859 req, &num_streams, &streams);
4860 if (tevent_req_nterror(req, status)) {
4864 if (num_streams == 1) {
4865 /* There is always one stream, ::$DATA. */
4866 tevent_req_done(req);
4870 for (i = 0; i < num_streams; i++) {
4871 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4872 __func__, streams[i].name, (size_t)streams[i].size));
4874 src_fname_tmp = synthetic_smb_fname(
4876 state->src_fsp->fsp_name->base_name,
4879 state->src_fsp->fsp_name->flags);
4880 if (tevent_req_nomem(src_fname_tmp, req)) {
4884 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
4885 TALLOC_FREE(src_fname_tmp);
4889 dst_fname_tmp = synthetic_smb_fname(
4891 state->dst_fsp->fsp_name->base_name,
4894 state->dst_fsp->fsp_name->flags);
4895 if (tevent_req_nomem(dst_fname_tmp, req)) {
4896 TALLOC_FREE(src_fname_tmp);
4900 status = copy_file(req,
4901 state->handle->conn,
4904 OPENX_FILE_CREATE_IF_NOT_EXIST,
4906 if (!NT_STATUS_IS_OK(status)) {
4907 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
4908 smb_fname_str_dbg(src_fname_tmp),
4909 smb_fname_str_dbg(dst_fname_tmp),
4910 nt_errstr(status)));
4911 TALLOC_FREE(src_fname_tmp);
4912 TALLOC_FREE(dst_fname_tmp);
4913 tevent_req_nterror(req, status);
4917 TALLOC_FREE(src_fname_tmp);
4918 TALLOC_FREE(dst_fname_tmp);
4921 TALLOC_FREE(streams);
4922 TALLOC_FREE(src_fname_tmp);
4923 TALLOC_FREE(dst_fname_tmp);
4924 tevent_req_done(req);
4927 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
4928 struct tevent_req *req,
4931 struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
4932 req, struct fruit_copy_chunk_state);
4935 if (tevent_req_is_nterror(req, &status)) {
4936 DEBUG(1, ("server side copy chunk failed: %s\n",
4937 nt_errstr(status)));
4939 tevent_req_received(req);
4943 *copied = fruit_copy_chunk_state->copied;
4944 tevent_req_received(req);
4946 return NT_STATUS_OK;
4949 static struct vfs_fn_pointers vfs_fruit_fns = {
4950 .connect_fn = fruit_connect,
4952 /* File operations */
4953 .chmod_fn = fruit_chmod,
4954 .chown_fn = fruit_chown,
4955 .unlink_fn = fruit_unlink,
4956 .rename_fn = fruit_rename,
4957 .rmdir_fn = fruit_rmdir,
4958 .open_fn = fruit_open,
4959 .pread_fn = fruit_pread,
4960 .pwrite_fn = fruit_pwrite,
4961 .stat_fn = fruit_stat,
4962 .lstat_fn = fruit_lstat,
4963 .fstat_fn = fruit_fstat,
4964 .streaminfo_fn = fruit_streaminfo,
4965 .ntimes_fn = fruit_ntimes,
4966 .ftruncate_fn = fruit_ftruncate,
4967 .fallocate_fn = fruit_fallocate,
4968 .create_file_fn = fruit_create_file,
4969 .readdir_attr_fn = fruit_readdir_attr,
4970 .copy_chunk_send_fn = fruit_copy_chunk_send,
4971 .copy_chunk_recv_fn = fruit_copy_chunk_recv,
4973 /* NT ACL operations */
4974 .fget_nt_acl_fn = fruit_fget_nt_acl,
4975 .fset_nt_acl_fn = fruit_fset_nt_acl,
4978 NTSTATUS vfs_fruit_init(void);
4979 NTSTATUS vfs_fruit_init(void)
4981 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
4983 if (!NT_STATUS_IS_OK(ret)) {
4987 vfs_fruit_debug_level = debug_add_class("fruit");
4988 if (vfs_fruit_debug_level == -1) {
4989 vfs_fruit_debug_level = DBGC_VFS;
4990 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
4993 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
4994 "vfs_fruit_init","fruit",vfs_fruit_debug_level));