s3: VFS: Change SMB_VFS_GETXATTR to use const struct smb_filename * instead of const...
[Samba.git] / source3 / modules / vfs_fruit.c
blobc4277b9ec34d74da4b6caf0bb16b0f67d48d31ed
1 /*
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/>.
20 #include "includes.h"
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"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
37 * Enhanced OS X and Netatalk compatibility
38 * ========================================
40 * This modules takes advantage of vfs_streams_xattr and
41 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
42 * loaded in the correct order:
44 * vfs modules = catia fruit streams_xattr
46 * The module intercepts the OS X special streams "AFP_AfpInfo" and
47 * "AFP_Resource" and handles them in a special way. All other named
48 * streams are deferred to vfs_streams_xattr.
50 * The OS X client maps all NTFS illegal characters to the Unicode
51 * private range. This module optionally stores the charcters using
52 * their native ASCII encoding using vfs_catia. If you're not enabling
53 * this feature, you can skip catia from vfs modules.
55 * Finally, open modes are optionally checked against Netatalk AFP
56 * share modes.
58 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
59 * extended metadata for files and directories. This module optionally
60 * reads and stores this metadata in a way compatible with Netatalk 3
61 * which stores the metadata in an EA "org.netatalk.metadata". Cf
62 * source3/include/MacExtensions.h for a description of the binary
63 * blobs content.
65 * The "AFP_Resource" named stream may be arbitrarily large, thus it
66 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
67 * the only available filesystem where xattrs can be of any size and
68 * the OS supports using the file APIs for xattrs.
70 * The AFP_Resource stream is stored in an AppleDouble file prepending
71 * "._" to the filename. On Solaris with ZFS the stream is optionally
72 * stored in an EA "org.netatalk.resource".
75 * Extended Attributes
76 * ===================
78 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
79 * other protocols you may want to adjust the xattr names the VFS
80 * module vfs_streams_xattr uses for storing ADS's. This defaults to
81 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
82 * these module parameters:
84 * streams_xattr:prefix = user.
85 * streams_xattr:store_stream_type = false
88 * TODO
89 * ====
91 * - log diagnostic if any needed VFS module is not loaded
92 * (eg with lp_vfs_objects())
93 * - add tests
96 static int vfs_fruit_debug_level = DBGC_VFS;
98 static struct global_fruit_config {
99 bool nego_aapl; /* client negotiated AAPL */
101 } global_fruit_config;
103 #undef DBGC_CLASS
104 #define DBGC_CLASS vfs_fruit_debug_level
106 #define FRUIT_PARAM_TYPE_NAME "fruit"
107 #define ADOUBLE_NAME_PREFIX "._"
109 #define NETATALK_META_XATTR "org.netatalk.Metadata"
110 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
112 #if defined(HAVE_ATTROPEN)
113 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
114 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
115 #else
116 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
117 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
118 #endif
120 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
122 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
123 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
124 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
125 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
127 struct fruit_config_data {
128 enum fruit_rsrc rsrc;
129 enum fruit_meta meta;
130 enum fruit_locking locking;
131 enum fruit_encoding encoding;
132 bool use_aapl; /* config from smb.conf */
133 bool use_copyfile;
134 bool readdir_attr_enabled;
135 bool unix_info_enabled;
136 bool copyfile_enabled;
137 bool veto_appledouble;
138 bool posix_rename;
139 bool aapl_zero_file_id;
142 * Additional options, all enabled by default,
143 * possibly useful for analyzing performance. The associated
144 * operations with each of them may be expensive, so having
145 * the chance to disable them individually gives a chance
146 * tweaking the setup for the particular usecase.
148 bool readdir_attr_rsize;
149 bool readdir_attr_finder_info;
150 bool readdir_attr_max_access;
153 static const struct enum_list fruit_rsrc[] = {
154 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
155 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
156 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
157 { -1, NULL}
160 static const struct enum_list fruit_meta[] = {
161 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
162 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
163 { -1, NULL}
166 static const struct enum_list fruit_locking[] = {
167 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
168 {FRUIT_LOCKING_NONE, "none"},
169 { -1, NULL}
172 static const struct enum_list fruit_encoding[] = {
173 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
174 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
175 { -1, NULL}
178 /*****************************************************************************
179 * Defines, functions and data structures that deal with AppleDouble
180 *****************************************************************************/
183 * There are two AppleDouble blobs we deal with:
185 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
186 * metadata in an xattr
188 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
189 * ._ files
191 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
193 /* Version info */
194 #define AD_VERSION2 0x00020000
195 #define AD_VERSION AD_VERSION2
198 * AppleDouble entry IDs.
200 #define ADEID_DFORK 1
201 #define ADEID_RFORK 2
202 #define ADEID_NAME 3
203 #define ADEID_COMMENT 4
204 #define ADEID_ICONBW 5
205 #define ADEID_ICONCOL 6
206 #define ADEID_FILEI 7
207 #define ADEID_FILEDATESI 8
208 #define ADEID_FINDERI 9
209 #define ADEID_MACFILEI 10
210 #define ADEID_PRODOSFILEI 11
211 #define ADEID_MSDOSFILEI 12
212 #define ADEID_SHORTNAME 13
213 #define ADEID_AFPFILEI 14
214 #define ADEID_DID 15
216 /* Private Netatalk entries */
217 #define ADEID_PRIVDEV 16
218 #define ADEID_PRIVINO 17
219 #define ADEID_PRIVSYN 18
220 #define ADEID_PRIVID 19
221 #define ADEID_MAX (ADEID_PRIVID + 1)
224 * These are the real ids for the private entries,
225 * as stored in the adouble file
227 #define AD_DEV 0x80444556
228 #define AD_INO 0x80494E4F
229 #define AD_SYN 0x8053594E
230 #define AD_ID 0x8053567E
232 /* Number of actually used entries */
233 #define ADEID_NUM_XATTR 8
234 #define ADEID_NUM_DOT_UND 2
235 #define ADEID_NUM_RSRC_XATTR 1
237 /* AppleDouble magic */
238 #define AD_APPLESINGLE_MAGIC 0x00051600
239 #define AD_APPLEDOUBLE_MAGIC 0x00051607
240 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
242 /* Sizes of relevant entry bits */
243 #define ADEDLEN_MAGIC 4
244 #define ADEDLEN_VERSION 4
245 #define ADEDLEN_FILLER 16
246 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
247 #define ADEDLEN_NENTRIES 2
248 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
249 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
250 #define AD_ENTRY_LEN_EID 4
251 #define AD_ENTRY_LEN_OFF 4
252 #define AD_ENTRY_LEN_LEN 4
253 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
255 /* Field widths */
256 #define ADEDLEN_NAME 255
257 #define ADEDLEN_COMMENT 200
258 #define ADEDLEN_FILEI 16
259 #define ADEDLEN_FINDERI 32
260 #define ADEDLEN_FILEDATESI 16
261 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
262 #define ADEDLEN_AFPFILEI 4
263 #define ADEDLEN_MACFILEI 4
264 #define ADEDLEN_PRODOSFILEI 8
265 #define ADEDLEN_MSDOSFILEI 2
266 #define ADEDLEN_DID 4
267 #define ADEDLEN_PRIVDEV 8
268 #define ADEDLEN_PRIVINO 8
269 #define ADEDLEN_PRIVSYN 8
270 #define ADEDLEN_PRIVID 4
272 /* Offsets */
273 #define ADEDOFF_MAGIC 0
274 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
275 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
276 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
278 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
279 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
280 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
281 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
282 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
283 ADEDLEN_FILEDATESI)
284 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
285 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
286 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
287 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
289 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
290 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
291 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
293 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
294 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
295 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
296 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
297 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
298 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
300 #if AD_DATASZ_XATTR != 402
301 #error bad size for AD_DATASZ_XATTR
302 #endif
304 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
305 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
306 ADEDLEN_FINDERI)
307 #if AD_DATASZ_DOT_UND != 82
308 #error bad size for AD_DATASZ_DOT_UND
309 #endif
312 * Sharemode locks fcntl() offsets
314 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
315 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
316 #else
317 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
318 #endif
319 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
321 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
322 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
323 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
324 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
325 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
326 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
327 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
328 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
329 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
330 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
332 /* Time stuff we overload the bits a little */
333 #define AD_DATE_CREATE 0
334 #define AD_DATE_MODIFY 4
335 #define AD_DATE_BACKUP 8
336 #define AD_DATE_ACCESS 12
337 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
338 AD_DATE_BACKUP | AD_DATE_ACCESS)
339 #define AD_DATE_UNIX (1 << 10)
340 #define AD_DATE_START 0x80000000
341 #define AD_DATE_DELTA 946684800
342 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
343 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
345 /* Accessor macros */
346 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
347 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
348 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
349 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
351 struct ad_entry {
352 size_t ade_off;
353 size_t ade_len;
356 struct adouble {
357 vfs_handle_struct *ad_handle;
358 int ad_fd;
359 bool ad_opened;
360 adouble_type_t ad_type;
361 uint32_t ad_magic;
362 uint32_t ad_version;
363 struct ad_entry ad_eid[ADEID_MAX];
364 char *ad_data;
367 struct ad_entry_order {
368 uint32_t id, offset, len;
371 /* Netatalk AppleDouble metadata xattr */
372 static const
373 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
374 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
375 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
376 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
377 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
378 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
379 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
380 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
381 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
382 {0, 0, 0}
385 /* AppleDouble resource fork file (the ones prefixed by "._") */
386 static const
387 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
388 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
389 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
390 {0, 0, 0}
394 * Fake AppleDouble entry oder for resource fork xattr. The xattr
395 * isn't an AppleDouble file, it simply contains the resource data,
396 * but in order to be able to use some API calls like ad_getentryoff()
397 * we build a fake/helper struct adouble with this entry order struct.
399 static const
400 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
401 {ADEID_RFORK, 0, 0},
402 {0, 0, 0}
405 /* Conversion from enumerated id to on-disk AppleDouble id */
406 #define AD_EID_DISK(a) (set_eid[a])
407 static const uint32_t set_eid[] = {
408 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
409 AD_DEV, AD_INO, AD_SYN, AD_ID
412 struct fio {
413 /* tcon config handle */
414 struct fruit_config_data *config;
416 /* Denote stream type, meta or rsrc */
417 adouble_type_t type;
421 * Forward declarations
423 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
424 adouble_type_t type);
425 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
426 static int ad_fset(struct adouble *ad, files_struct *fsp);
427 static int adouble_path(TALLOC_CTX *ctx,
428 const struct smb_filename *smb_fname__in,
429 struct smb_filename **ppsmb_fname_out);
432 * Return a pointer to an AppleDouble entry
434 * Returns NULL if the entry is not present
436 static char *ad_get_entry(const struct adouble *ad, int eid)
438 off_t off = ad_getentryoff(ad, eid);
439 size_t len = ad_getentrylen(ad, eid);
441 if (off == 0 || len == 0) {
442 return NULL;
445 return ad->ad_data + off;
449 * Get a date
451 static int ad_getdate(const struct adouble *ad,
452 unsigned int dateoff,
453 uint32_t *date)
455 bool xlate = (dateoff & AD_DATE_UNIX);
456 char *p = NULL;
458 dateoff &= AD_DATE_MASK;
459 p = ad_get_entry(ad, ADEID_FILEDATESI);
460 if (p == NULL) {
461 return -1;
464 if (dateoff > AD_DATE_ACCESS) {
465 return -1;
468 memcpy(date, p + dateoff, sizeof(uint32_t));
470 if (xlate) {
471 *date = AD_DATE_TO_UNIX(*date);
473 return 0;
477 * Set a date
479 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
481 bool xlate = (dateoff & AD_DATE_UNIX);
482 char *p = NULL;
484 p = ad_get_entry(ad, ADEID_FILEDATESI);
485 if (p == NULL) {
486 return -1;
489 dateoff &= AD_DATE_MASK;
490 if (xlate) {
491 date = AD_DATE_FROM_UNIX(date);
494 if (dateoff > AD_DATE_ACCESS) {
495 return -1;
498 memcpy(p + dateoff, &date, sizeof(date));
500 return 0;
505 * Map on-disk AppleDouble id to enumerated id
507 static uint32_t get_eid(uint32_t eid)
509 if (eid <= 15) {
510 return eid;
513 switch (eid) {
514 case AD_DEV:
515 return ADEID_PRIVDEV;
516 case AD_INO:
517 return ADEID_PRIVINO;
518 case AD_SYN:
519 return ADEID_PRIVSYN;
520 case AD_ID:
521 return ADEID_PRIVID;
522 default:
523 break;
526 return 0;
530 * Pack AppleDouble structure into data buffer
532 static bool ad_pack(struct adouble *ad)
534 uint32_t eid;
535 uint16_t nent;
536 uint32_t bufsize;
537 uint32_t offset = 0;
539 bufsize = talloc_get_size(ad->ad_data);
541 if (offset + ADEDLEN_MAGIC < offset ||
542 offset + ADEDLEN_MAGIC >= bufsize) {
543 return false;
545 RSIVAL(ad->ad_data, offset, ad->ad_magic);
546 offset += ADEDLEN_MAGIC;
548 if (offset + ADEDLEN_VERSION < offset ||
549 offset + ADEDLEN_VERSION >= bufsize) {
550 return false;
552 RSIVAL(ad->ad_data, offset, ad->ad_version);
553 offset += ADEDLEN_VERSION;
555 if (offset + ADEDLEN_FILLER < offset ||
556 offset + ADEDLEN_FILLER >= bufsize) {
557 return false;
559 if (ad->ad_type == ADOUBLE_RSRC) {
560 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
562 offset += ADEDLEN_FILLER;
564 if (offset + ADEDLEN_NENTRIES < offset ||
565 offset + ADEDLEN_NENTRIES >= bufsize) {
566 return false;
568 offset += ADEDLEN_NENTRIES;
570 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
571 if (ad->ad_eid[eid].ade_off == 0) {
573 * ade_off is also used as indicator whether a
574 * specific entry is used or not
576 continue;
579 if (offset + AD_ENTRY_LEN_EID < offset ||
580 offset + AD_ENTRY_LEN_EID >= bufsize) {
581 return false;
583 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
584 offset += AD_ENTRY_LEN_EID;
586 if (offset + AD_ENTRY_LEN_OFF < offset ||
587 offset + AD_ENTRY_LEN_OFF >= bufsize) {
588 return false;
590 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
591 offset += AD_ENTRY_LEN_OFF;
593 if (offset + AD_ENTRY_LEN_LEN < offset ||
594 offset + AD_ENTRY_LEN_LEN >= bufsize) {
595 return false;
597 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
598 offset += AD_ENTRY_LEN_LEN;
600 nent++;
603 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
604 return false;
606 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
608 return true;
612 * Unpack an AppleDouble blob into a struct adoble
614 static bool ad_unpack(struct adouble *ad, const size_t nentries,
615 size_t filesize)
617 size_t bufsize = talloc_get_size(ad->ad_data);
618 size_t adentries, i;
619 uint32_t eid, len, off;
622 * The size of the buffer ad->ad_data is checked when read, so
623 * we wouldn't have to check our own offsets, a few extra
624 * checks won't hurt though. We have to check the offsets we
625 * read from the buffer anyway.
628 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
629 DEBUG(1, ("bad size\n"));
630 return false;
633 ad->ad_magic = RIVAL(ad->ad_data, 0);
634 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
635 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
636 DEBUG(1, ("wrong magic or version\n"));
637 return false;
640 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
641 if (adentries != nentries) {
642 DEBUG(1, ("invalid number of entries: %zu\n",
643 adentries));
644 return false;
647 /* now, read in the entry bits */
648 for (i = 0; i < adentries; i++) {
649 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
650 eid = get_eid(eid);
651 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
652 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
654 if (!eid || eid > ADEID_MAX) {
655 DEBUG(1, ("bogus eid %d\n", eid));
656 return false;
660 * All entries other than the resource fork are
661 * expected to be read into the ad_data buffer, so
662 * ensure the specified offset is within that bound
664 if ((off > bufsize) && (eid != ADEID_RFORK)) {
665 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
666 eid, off, len));
667 return false;
671 * All entries besides FinderInfo and resource fork
672 * must fit into the buffer. FinderInfo is special as
673 * it may be larger then the default 32 bytes (if it
674 * contains marshalled xattrs), but we will fixup that
675 * in ad_convert(). And the resource fork is never
676 * accessed directly by the ad_data buf (also see
677 * comment above) anyway.
679 if ((eid != ADEID_RFORK) &&
680 (eid != ADEID_FINDERI) &&
681 ((off + len) > bufsize)) {
682 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
683 eid, off, len));
684 return false;
688 * That would be obviously broken
690 if (off > filesize) {
691 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
692 eid, off, len));
693 return false;
697 * Check for any entry that has its end beyond the
698 * filesize.
700 if (off + len < off) {
701 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
702 ", len: %" PRIu32 "\n",
703 eid, off, len));
704 return false;
707 if (off + len > filesize) {
709 * If this is the resource fork entry, we fix
710 * up the length, for any other entry we bail
711 * out.
713 if (eid != ADEID_RFORK) {
714 DEBUG(1, ("bogus eid %d: off: %" PRIu32
715 ", len: %" PRIu32 "\n",
716 eid, off, len));
717 return false;
721 * Fixup the resource fork entry by limiting
722 * the size to entryoffset - filesize.
724 len = filesize - off;
725 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
726 ", len: %" PRIu32 "\n", off, len));
729 ad->ad_eid[eid].ade_off = off;
730 ad->ad_eid[eid].ade_len = len;
733 return true;
737 * Convert from Apple's ._ file to Netatalk
739 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
740 * bytes containing packed xattrs. Netatalk can't deal with that, so
741 * we simply discard the packed xattrs.
743 * @return -1 in case an error occurred, 0 if no conversion was done, 1
744 * otherwise
746 static int ad_convert(struct adouble *ad, int fd)
748 int rc = 0;
749 char *map = MAP_FAILED;
750 size_t origlen;
752 origlen = ad_getentryoff(ad, ADEID_RFORK) +
753 ad_getentrylen(ad, ADEID_RFORK);
755 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
756 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
757 if (map == MAP_FAILED) {
758 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
759 rc = -1;
760 goto exit;
763 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
764 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
765 map + ad_getentryoff(ad, ADEID_RFORK),
766 ad_getentrylen(ad, ADEID_RFORK));
769 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
770 ad_setentryoff(ad, ADEID_RFORK,
771 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
774 * FIXME: direct ftruncate(), but we don't have a fsp for the
775 * VFS call
777 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
778 + ad_getentrylen(ad, ADEID_RFORK));
780 exit:
781 if (map != MAP_FAILED) {
782 munmap(map, origlen);
784 return rc;
788 * Read and parse Netatalk AppleDouble metadata xattr
790 static ssize_t ad_read_meta(struct adouble *ad,
791 const struct smb_filename *smb_fname)
793 int rc = 0;
794 ssize_t ealen;
795 bool ok;
797 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
799 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
800 AFPINFO_EA_NETATALK, ad->ad_data,
801 AD_DATASZ_XATTR);
802 if (ealen == -1) {
803 switch (errno) {
804 case ENOATTR:
805 case ENOENT:
806 if (errno == ENOATTR) {
807 errno = ENOENT;
809 rc = -1;
810 goto exit;
811 default:
812 DEBUG(2, ("error reading meta xattr: %s\n",
813 strerror(errno)));
814 rc = -1;
815 goto exit;
818 if (ealen != AD_DATASZ_XATTR) {
819 DEBUG(2, ("bad size %zd\n", ealen));
820 errno = EINVAL;
821 rc = -1;
822 goto exit;
825 /* Now parse entries */
826 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
827 if (!ok) {
828 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
829 errno = EINVAL;
830 rc = -1;
831 goto exit;
834 if (!ad_getentryoff(ad, ADEID_FINDERI)
835 || !ad_getentryoff(ad, ADEID_COMMENT)
836 || !ad_getentryoff(ad, ADEID_FILEDATESI)
837 || !ad_getentryoff(ad, ADEID_AFPFILEI)
838 || !ad_getentryoff(ad, ADEID_PRIVDEV)
839 || !ad_getentryoff(ad, ADEID_PRIVINO)
840 || !ad_getentryoff(ad, ADEID_PRIVSYN)
841 || !ad_getentryoff(ad, ADEID_PRIVID)) {
842 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
843 errno = EINVAL;
844 rc = -1;
845 goto exit;
848 exit:
849 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
850 smb_fname->base_name, rc));
852 if (rc != 0) {
853 ealen = -1;
854 if (errno == EINVAL) {
855 become_root();
856 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
857 unbecome_root();
858 errno = ENOENT;
861 return ealen;
864 static int ad_open_meta(const struct smb_filename *smb_fname,
865 int flags,
866 mode_t mode)
868 return open(smb_fname->base_name, flags, mode);
871 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
872 int flags,
873 mode_t mode)
875 #ifdef HAVE_ATTROPEN
876 /* FIXME: direct Solaris xattr syscall */
877 return attropen(smb_fname->base_name,
878 AFPRESOURCE_EA_NETATALK, flags, mode);
879 #else
880 errno = ENOSYS;
881 return -1;
882 #endif
885 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
886 int flags,
887 mode_t mode)
889 int ret;
890 int fd;
891 struct smb_filename *adp_smb_fname = NULL;
893 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
894 if (ret != 0) {
895 return -1;
898 fd = open(adp_smb_fname->base_name, flags, mode);
899 TALLOC_FREE(adp_smb_fname);
901 return fd;
904 static int ad_open_rsrc(vfs_handle_struct *handle,
905 const struct smb_filename *smb_fname,
906 int flags,
907 mode_t mode)
909 struct fruit_config_data *config = NULL;
910 int fd;
912 SMB_VFS_HANDLE_GET_DATA(handle, config,
913 struct fruit_config_data, return -1);
915 if (config->rsrc == FRUIT_RSRC_XATTR) {
916 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
917 } else {
918 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
921 return fd;
924 static int ad_open(vfs_handle_struct *handle,
925 struct adouble *ad,
926 const struct smb_filename *smb_fname,
927 adouble_type_t t,
928 int flags,
929 mode_t mode)
931 int fd;
933 DBG_DEBUG("Path [%s] type [%s]\n",
934 smb_fname->base_name, t == ADOUBLE_META ? "meta" : "rsrc");
936 if (t == ADOUBLE_META) {
937 fd = ad_open_meta(smb_fname, flags, mode);
938 } else {
939 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
942 if (fd != -1) {
943 ad->ad_opened = true;
944 ad->ad_fd = fd;
947 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
948 smb_fname->base_name,
949 t == ADOUBLE_META ? "meta" : "rsrc", fd);
951 return fd;
954 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
956 int ret;
957 SMB_STRUCT_STAT st;
959 /* FIXME: direct sys_fstat(), don't have an fsp */
960 ret = sys_fstat(ad->ad_fd, &st,
961 lp_fake_directory_create_times(
962 SNUM(ad->ad_handle->conn)));
963 if (ret != 0) {
964 return -1;
967 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
968 return st.st_ex_size;
971 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
972 const struct smb_filename *smb_fname)
974 struct adouble *meta_ad = NULL;
975 SMB_STRUCT_STAT sbuf;
976 char *p_ad = NULL;
977 char *p_meta_ad = NULL;
978 ssize_t len;
979 int ret;
980 bool ok;
982 len = sys_pread(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
983 if (len != AD_DATASZ_DOT_UND) {
984 DBG_NOTICE("%s %s: bad size: %zd\n",
985 smb_fname->base_name, strerror(errno), len);
986 return -1;
989 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
990 SNUM(ad->ad_handle->conn)));
991 if (ret != 0) {
992 return -1;
995 /* Now parse entries */
996 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
997 if (!ok) {
998 DBG_ERR("invalid AppleDouble resource %s\n",
999 smb_fname->base_name);
1000 errno = EINVAL;
1001 return -1;
1004 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1005 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1006 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1007 DBG_ERR("invalid AppleDouble resource %s\n",
1008 smb_fname->base_name);
1009 errno = EINVAL;
1010 return -1;
1013 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1014 return len;
1018 * Try to fixup AppleDouble files created by OS X with xattrs
1019 * appended to the ADEID_FINDERI entry. We simply remove the
1020 * xattrs blob, this means any fancy xattr that was stored
1021 * there is lost.
1024 ret = ad_convert(ad, ad->ad_fd);
1025 if (ret != 0) {
1026 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1027 return len;
1030 ok = ad_pack(ad);
1031 if (!ok) {
1032 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1033 return -1;
1036 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1037 if (len != AD_DATASZ_DOT_UND) {
1038 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1039 return -1;
1042 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1043 if (meta_ad == NULL) {
1044 return -1;
1047 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1048 if (p_ad == NULL) {
1049 TALLOC_FREE(meta_ad);
1050 return -1;
1052 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1053 if (p_meta_ad == NULL) {
1054 TALLOC_FREE(meta_ad);
1055 return -1;
1058 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1060 ret = ad_set(meta_ad, smb_fname);
1061 TALLOC_FREE(meta_ad);
1062 if (ret != 0) {
1063 return -1;
1066 return len;
1070 * Read and parse resource fork, either ._ AppleDouble file or xattr
1072 static ssize_t ad_read_rsrc(struct adouble *ad,
1073 const struct smb_filename *smb_fname)
1075 struct fruit_config_data *config = NULL;
1076 ssize_t len;
1078 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1079 struct fruit_config_data, return -1);
1081 if (config->rsrc == FRUIT_RSRC_XATTR) {
1082 len = ad_read_rsrc_xattr(ad);
1083 } else {
1084 len = ad_read_rsrc_adouble(ad, smb_fname);
1087 return len;
1091 * Read and unpack an AppleDouble metadata xattr or resource
1093 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1095 switch (ad->ad_type) {
1096 case ADOUBLE_META:
1097 return ad_read_meta(ad, smb_fname);
1098 case ADOUBLE_RSRC:
1099 return ad_read_rsrc(ad, smb_fname);
1100 default:
1101 return -1;
1105 static int adouble_destructor(struct adouble *ad)
1107 if ((ad->ad_fd != -1) && ad->ad_opened) {
1108 close(ad->ad_fd);
1109 ad->ad_fd = -1;
1111 return 0;
1115 * Allocate a struct adouble without initialiing it
1117 * The struct is either hang of the fsp extension context or if fsp is
1118 * NULL from ctx.
1120 * @param[in] ctx talloc context
1121 * @param[in] handle vfs handle
1122 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1124 * @return adouble handle
1126 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1127 adouble_type_t type)
1129 int rc = 0;
1130 size_t adsize = 0;
1131 struct adouble *ad;
1132 struct fruit_config_data *config;
1134 SMB_VFS_HANDLE_GET_DATA(handle, config,
1135 struct fruit_config_data, return NULL);
1137 switch (type) {
1138 case ADOUBLE_META:
1139 adsize = AD_DATASZ_XATTR;
1140 break;
1141 case ADOUBLE_RSRC:
1142 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1143 adsize = AD_DATASZ_DOT_UND;
1145 break;
1146 default:
1147 return NULL;
1150 ad = talloc_zero(ctx, struct adouble);
1151 if (ad == NULL) {
1152 rc = -1;
1153 goto exit;
1156 if (adsize) {
1157 ad->ad_data = talloc_zero_array(ad, char, adsize);
1158 if (ad->ad_data == NULL) {
1159 rc = -1;
1160 goto exit;
1164 ad->ad_handle = handle;
1165 ad->ad_type = type;
1166 ad->ad_magic = AD_MAGIC;
1167 ad->ad_version = AD_VERSION;
1168 ad->ad_fd = -1;
1170 talloc_set_destructor(ad, adouble_destructor);
1172 exit:
1173 if (rc != 0) {
1174 TALLOC_FREE(ad);
1176 return ad;
1180 * Allocate and initialize a new struct adouble
1182 * @param[in] ctx talloc context
1183 * @param[in] handle vfs handle
1184 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1186 * @return adouble handle, initialized
1188 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1189 adouble_type_t type)
1191 int rc = 0;
1192 const struct ad_entry_order *eid;
1193 struct adouble *ad = NULL;
1194 struct fruit_config_data *config;
1195 time_t t = time(NULL);
1197 SMB_VFS_HANDLE_GET_DATA(handle, config,
1198 struct fruit_config_data, return NULL);
1200 switch (type) {
1201 case ADOUBLE_META:
1202 eid = entry_order_meta_xattr;
1203 break;
1204 case ADOUBLE_RSRC:
1205 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1206 eid = entry_order_dot_und;
1207 } else {
1208 eid = entry_order_rsrc_xattr;
1210 break;
1211 default:
1212 return NULL;
1215 ad = ad_alloc(ctx, handle, type);
1216 if (ad == NULL) {
1217 return NULL;
1220 while (eid->id) {
1221 ad->ad_eid[eid->id].ade_off = eid->offset;
1222 ad->ad_eid[eid->id].ade_len = eid->len;
1223 eid++;
1226 /* put something sane in the date fields */
1227 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1228 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1229 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1230 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1232 if (rc != 0) {
1233 TALLOC_FREE(ad);
1235 return ad;
1239 * Return AppleDouble data for a file
1241 * @param[in] ctx talloc context
1242 * @param[in] handle vfs handle
1243 * @param[in] smb_fname pathname to file or directory
1244 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1246 * @return talloced struct adouble or NULL on error
1248 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1249 const struct smb_filename *smb_fname,
1250 adouble_type_t type)
1252 int rc = 0;
1253 ssize_t len;
1254 struct adouble *ad = NULL;
1255 int fd;
1256 int mode;
1258 DEBUG(10, ("ad_get(%s) called for %s\n",
1259 type == ADOUBLE_META ? "meta" : "rsrc",
1260 smb_fname->base_name));
1262 ad = ad_alloc(ctx, handle, type);
1263 if (ad == NULL) {
1264 rc = -1;
1265 goto exit;
1269 * Here's the deal: for ADOUBLE_META we can do without an fd
1270 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1271 * however we need a full-fledged fd for file IO on the ._
1272 * file.
1274 if (type == ADOUBLE_RSRC) {
1275 /* Try rw first so we can use the fd in ad_convert() */
1276 mode = O_RDWR;
1278 fd = ad_open(handle, ad, smb_fname, ADOUBLE_RSRC, mode, 0);
1279 if (fd == -1 && ((errno == EROFS) || (errno == EACCES))) {
1280 mode = O_RDONLY;
1281 fd = ad_open(handle, ad, smb_fname,
1282 ADOUBLE_RSRC, mode, 0);
1285 if (fd == -1) {
1286 DBG_DEBUG("ad_open [%s] error [%s]\n",
1287 smb_fname->base_name, strerror(errno));
1288 rc = -1;
1289 goto exit;
1293 len = ad_read(ad, smb_fname);
1294 if (len == -1) {
1295 DEBUG(10, ("error reading AppleDouble for %s\n",
1296 smb_fname->base_name));
1297 rc = -1;
1298 goto exit;
1301 exit:
1302 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1303 type == ADOUBLE_META ? "meta" : "rsrc",
1304 smb_fname->base_name, rc));
1306 if (rc != 0) {
1307 TALLOC_FREE(ad);
1309 return ad;
1313 * Return AppleDouble data for a file
1315 * @param[in] ctx talloc context
1316 * @param[in] handle vfs handle
1317 * @param[in] fsp fsp to use for IO
1318 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1320 * @return talloced struct adouble or NULL on error
1322 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1323 files_struct *fsp, adouble_type_t type)
1325 int rc = 0;
1326 ssize_t len;
1327 struct adouble *ad = NULL;
1329 DBG_DEBUG("ad_get(%s) path [%s]\n",
1330 type == ADOUBLE_META ? "meta" : "rsrc",
1331 fsp_str_dbg(fsp));
1333 ad = ad_alloc(ctx, handle, type);
1334 if (ad == NULL) {
1335 rc = -1;
1336 goto exit;
1339 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1340 ad->ad_fd = fsp->fh->fd;
1341 } else {
1343 * Here's the deal: for ADOUBLE_META we can do without an fd
1344 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1345 * however we need a full-fledged fd for file IO on the ._
1346 * file.
1348 int fd;
1349 int mode;
1351 if (type == ADOUBLE_RSRC) {
1352 /* Try rw first so we can use the fd in ad_convert() */
1353 mode = O_RDWR;
1355 fd = ad_open(handle, ad, fsp->base_fsp->fsp_name,
1356 ADOUBLE_RSRC, mode, 0);
1357 if (fd == -1 &&
1358 ((errno == EROFS) || (errno == EACCES)))
1360 mode = O_RDONLY;
1361 fd = ad_open(handle, ad,
1362 fsp->base_fsp->fsp_name, ADOUBLE_RSRC,
1363 mode, 0);
1366 if (fd == -1) {
1367 DBG_DEBUG("error opening AppleDouble for %s\n",
1368 fsp_str_dbg(fsp));
1369 rc = -1;
1370 goto exit;
1375 len = ad_read(ad, fsp->base_fsp->fsp_name);
1376 if (len == -1) {
1377 DBG_DEBUG("error reading AppleDouble for %s\n",
1378 fsp_str_dbg(fsp));
1379 rc = -1;
1380 goto exit;
1383 exit:
1384 DBG_DEBUG("ad_get(%s) path [%s] rc [%d]\n",
1385 type == ADOUBLE_META ? "meta" : "rsrc",
1386 fsp_str_dbg(fsp), rc);
1388 if (rc != 0) {
1389 TALLOC_FREE(ad);
1391 return ad;
1395 * Set AppleDouble metadata on a file or directory
1397 * @param[in] ad adouble handle
1399 * @param[in] smb_fname pathname to file or directory
1401 * @return status code, 0 means success
1403 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1405 bool ok;
1406 int ret;
1408 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1410 if (ad->ad_type != ADOUBLE_META) {
1411 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1412 smb_fname->base_name);
1413 return -1;
1416 ok = ad_pack(ad);
1417 if (!ok) {
1418 return -1;
1421 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1422 smb_fname,
1423 AFPINFO_EA_NETATALK,
1424 ad->ad_data,
1425 AD_DATASZ_XATTR, 0);
1427 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1429 return ret;
1433 * Set AppleDouble metadata on a file or directory
1435 * @param[in] ad adouble handle
1436 * @param[in] fsp file handle
1438 * @return status code, 0 means success
1440 static int ad_fset(struct adouble *ad, files_struct *fsp)
1442 int rc = -1;
1443 ssize_t len;
1444 bool ok;
1446 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1448 if ((fsp == NULL)
1449 || (fsp->fh == NULL)
1450 || (fsp->fh->fd == -1))
1452 smb_panic("bad fsp");
1455 ok = ad_pack(ad);
1456 if (!ok) {
1457 return -1;
1460 switch (ad->ad_type) {
1461 case ADOUBLE_META:
1462 rc = SMB_VFS_NEXT_FSETXATTR(ad->ad_handle,
1463 fsp,
1464 AFPINFO_EA_NETATALK,
1465 ad->ad_data,
1466 AD_DATASZ_XATTR, 0);
1467 break;
1469 case ADOUBLE_RSRC:
1470 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1471 fsp,
1472 ad->ad_data,
1473 talloc_get_size(ad->ad_data),
1475 if (len != (ssize_t)talloc_get_size(ad->ad_data)) {
1476 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1477 return -1;
1479 rc = 0;
1480 break;
1482 default:
1483 return -1;
1486 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1488 return rc;
1491 /*****************************************************************************
1492 * Helper functions
1493 *****************************************************************************/
1495 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1497 if (strncasecmp_m(smb_fname->stream_name,
1498 AFPINFO_STREAM_NAME,
1499 strlen(AFPINFO_STREAM_NAME)) == 0) {
1500 return true;
1502 return false;
1505 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1507 if (strncasecmp_m(smb_fname->stream_name,
1508 AFPRESOURCE_STREAM_NAME,
1509 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1510 return true;
1512 return false;
1516 * Test whether stream is an Apple stream, not used atm
1518 #if 0
1519 static bool is_apple_stream(const struct smb_filename *smb_fname)
1521 if (is_afpinfo_stream(smb_fname)) {
1522 return true;
1524 if (is_afpresource_stream(smb_fname)) {
1525 return true;
1527 return false;
1529 #endif
1532 * Initialize config struct from our smb.conf config parameters
1534 static int init_fruit_config(vfs_handle_struct *handle)
1536 struct fruit_config_data *config;
1537 int enumval;
1539 config = talloc_zero(handle->conn, struct fruit_config_data);
1540 if (!config) {
1541 DEBUG(1, ("talloc_zero() failed\n"));
1542 errno = ENOMEM;
1543 return -1;
1547 * Versions up to Samba 4.5.x had a spelling bug in the
1548 * fruit:resource option calling lp_parm_enum with
1549 * "res*s*ource" (ie two s).
1551 * In Samba 4.6 we accept both the wrong and the correct
1552 * spelling, in Samba 4.7 the bad spelling will be removed.
1554 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1555 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1556 if (enumval == -1) {
1557 DEBUG(1, ("value for %s: resource type unknown\n",
1558 FRUIT_PARAM_TYPE_NAME));
1559 return -1;
1561 config->rsrc = (enum fruit_rsrc)enumval;
1563 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1564 "resource", fruit_rsrc, enumval);
1565 if (enumval == -1) {
1566 DEBUG(1, ("value for %s: resource type unknown\n",
1567 FRUIT_PARAM_TYPE_NAME));
1568 return -1;
1570 config->rsrc = (enum fruit_rsrc)enumval;
1572 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1573 "metadata", fruit_meta, FRUIT_META_NETATALK);
1574 if (enumval == -1) {
1575 DEBUG(1, ("value for %s: metadata type unknown\n",
1576 FRUIT_PARAM_TYPE_NAME));
1577 return -1;
1579 config->meta = (enum fruit_meta)enumval;
1581 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1582 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1583 if (enumval == -1) {
1584 DEBUG(1, ("value for %s: locking type unknown\n",
1585 FRUIT_PARAM_TYPE_NAME));
1586 return -1;
1588 config->locking = (enum fruit_locking)enumval;
1590 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1591 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1592 if (enumval == -1) {
1593 DEBUG(1, ("value for %s: encoding type unknown\n",
1594 FRUIT_PARAM_TYPE_NAME));
1595 return -1;
1597 config->encoding = (enum fruit_encoding)enumval;
1599 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1600 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1601 FRUIT_PARAM_TYPE_NAME,
1602 "veto_appledouble",
1603 true);
1606 config->use_aapl = lp_parm_bool(
1607 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1609 config->unix_info_enabled = lp_parm_bool(
1610 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1612 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1613 "copyfile", false);
1615 config->posix_rename = lp_parm_bool(
1616 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1618 config->aapl_zero_file_id =
1619 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1621 config->readdir_attr_rsize = lp_parm_bool(
1622 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1624 config->readdir_attr_finder_info = lp_parm_bool(
1625 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1627 config->readdir_attr_max_access = lp_parm_bool(
1628 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1630 SMB_VFS_HANDLE_SET_DATA(handle, config,
1631 NULL, struct fruit_config_data,
1632 return -1);
1634 return 0;
1638 * Prepend "._" to a basename
1639 * Return a new struct smb_filename with stream_name == NULL.
1641 static int adouble_path(TALLOC_CTX *ctx,
1642 const struct smb_filename *smb_fname_in,
1643 struct smb_filename **pp_smb_fname_out)
1645 char *parent;
1646 const char *base;
1647 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1648 smb_fname_in);
1650 if (smb_fname == NULL) {
1651 return -1;
1654 /* We need streamname to be NULL */
1655 TALLOC_FREE(smb_fname->stream_name);
1657 /* And we're replacing base_name. */
1658 TALLOC_FREE(smb_fname->base_name);
1660 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
1661 &parent, &base)) {
1662 TALLOC_FREE(smb_fname);
1663 return -1;
1666 smb_fname->base_name = talloc_asprintf(smb_fname,
1667 "%s/._%s", parent, base);
1668 if (smb_fname->base_name == NULL) {
1669 TALLOC_FREE(smb_fname);
1670 return -1;
1673 *pp_smb_fname_out = smb_fname;
1675 return 0;
1679 * Allocate and initialize an AfpInfo struct
1681 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1683 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1684 if (ai == NULL) {
1685 return NULL;
1687 ai->afpi_Signature = AFP_Signature;
1688 ai->afpi_Version = AFP_Version;
1689 ai->afpi_BackupTime = AD_DATE_START;
1690 return ai;
1694 * Pack an AfpInfo struct into a buffer
1696 * Buffer size must be at least AFP_INFO_SIZE
1697 * Returns size of packed buffer
1699 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1701 memset(buf, 0, AFP_INFO_SIZE);
1703 RSIVAL(buf, 0, ai->afpi_Signature);
1704 RSIVAL(buf, 4, ai->afpi_Version);
1705 RSIVAL(buf, 12, ai->afpi_BackupTime);
1706 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1708 return AFP_INFO_SIZE;
1712 * Unpack a buffer into a AfpInfo structure
1714 * Buffer size must be at least AFP_INFO_SIZE
1715 * Returns allocated AfpInfo struct
1717 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1719 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1720 if (ai == NULL) {
1721 return NULL;
1724 ai->afpi_Signature = RIVAL(data, 0);
1725 ai->afpi_Version = RIVAL(data, 4);
1726 ai->afpi_BackupTime = RIVAL(data, 12);
1727 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1728 sizeof(ai->afpi_FinderInfo));
1730 if (ai->afpi_Signature != AFP_Signature
1731 || ai->afpi_Version != AFP_Version) {
1732 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1733 TALLOC_FREE(ai);
1736 return ai;
1740 * Fake an inode number from the md5 hash of the (xattr) name
1742 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1744 MD5_CTX ctx;
1745 unsigned char hash[16];
1746 SMB_INO_T result;
1747 char *upper_sname;
1749 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1750 SMB_ASSERT(upper_sname != NULL);
1752 MD5Init(&ctx);
1753 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1754 sizeof(sbuf->st_ex_dev));
1755 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1756 sizeof(sbuf->st_ex_ino));
1757 MD5Update(&ctx, (unsigned char *)upper_sname,
1758 talloc_get_size(upper_sname)-1);
1759 MD5Final(hash, &ctx);
1761 TALLOC_FREE(upper_sname);
1763 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1764 memcpy(&result, hash, sizeof(result));
1766 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1767 sname, (unsigned long long)result));
1769 return result;
1772 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1773 struct stream_struct **streams,
1774 const char *name, off_t size,
1775 off_t alloc_size)
1777 struct stream_struct *tmp;
1779 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1780 (*num_streams)+1);
1781 if (tmp == NULL) {
1782 return false;
1785 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1786 if (tmp[*num_streams].name == NULL) {
1787 return false;
1790 tmp[*num_streams].size = size;
1791 tmp[*num_streams].alloc_size = alloc_size;
1793 *streams = tmp;
1794 *num_streams += 1;
1795 return true;
1798 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1799 struct stream_struct **streams)
1801 struct stream_struct *tmp = *streams;
1802 unsigned int i;
1804 if (*num_streams == 0) {
1805 return true;
1808 for (i = 0; i < *num_streams; i++) {
1809 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1810 break;
1814 if (i == *num_streams) {
1815 return true;
1818 if (tmp[i].size > 0) {
1819 return true;
1822 TALLOC_FREE(tmp[i].name);
1823 if (*num_streams - 1 > i) {
1824 memmove(&tmp[i], &tmp[i+1],
1825 (*num_streams - i - 1) * sizeof(struct stream_struct));
1828 *num_streams -= 1;
1829 return true;
1832 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1833 struct stream_struct **streams,
1834 const char *name)
1836 struct stream_struct *tmp = *streams;
1837 unsigned int i;
1839 if (*num_streams == 0) {
1840 return true;
1843 for (i = 0; i < *num_streams; i++) {
1844 if (strequal_m(tmp[i].name, name)) {
1845 break;
1849 if (i == *num_streams) {
1850 return true;
1853 TALLOC_FREE(tmp[i].name);
1854 if (*num_streams - 1 > i) {
1855 memmove(&tmp[i], &tmp[i+1],
1856 (*num_streams - i - 1) * sizeof(struct stream_struct));
1859 *num_streams -= 1;
1860 return true;
1863 static bool ad_empty_finderinfo(const struct adouble *ad)
1865 int cmp;
1866 char emptybuf[ADEDLEN_FINDERI] = {0};
1867 char *fi = NULL;
1869 fi = ad_get_entry(ad, ADEID_FINDERI);
1870 if (fi == NULL) {
1871 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1872 return false;
1875 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1876 return (cmp == 0);
1879 static bool ai_empty_finderinfo(const AfpInfo *ai)
1881 int cmp;
1882 char emptybuf[ADEDLEN_FINDERI] = {0};
1884 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1885 return (cmp == 0);
1889 * Update btime with btime from Netatalk
1891 static void update_btime(vfs_handle_struct *handle,
1892 struct smb_filename *smb_fname)
1894 uint32_t t;
1895 struct timespec creation_time = {0};
1896 struct adouble *ad;
1897 struct fruit_config_data *config = NULL;
1899 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1900 return);
1902 switch (config->meta) {
1903 case FRUIT_META_STREAM:
1904 return;
1905 case FRUIT_META_NETATALK:
1906 /* Handled below */
1907 break;
1908 default:
1909 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1910 return;
1913 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1914 if (ad == NULL) {
1915 return;
1917 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1918 TALLOC_FREE(ad);
1919 return;
1921 TALLOC_FREE(ad);
1923 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1924 update_stat_ex_create_time(&smb_fname->st, creation_time);
1926 return;
1930 * Map an access mask to a Netatalk single byte byte range lock
1932 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1933 uint32_t access_mask)
1935 off_t offset;
1937 switch (access_mask) {
1938 case FILE_READ_DATA:
1939 offset = AD_FILELOCK_OPEN_RD;
1940 break;
1942 case FILE_WRITE_DATA:
1943 case FILE_APPEND_DATA:
1944 offset = AD_FILELOCK_OPEN_WR;
1945 break;
1947 default:
1948 offset = AD_FILELOCK_OPEN_NONE;
1949 break;
1952 if (fork_type == APPLE_FORK_RSRC) {
1953 if (offset == AD_FILELOCK_OPEN_NONE) {
1954 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1955 } else {
1956 offset += 2;
1960 return offset;
1964 * Map a deny mode to a Netatalk brl
1966 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1967 uint32_t deny_mode)
1969 off_t offset;
1971 switch (deny_mode) {
1972 case DENY_READ:
1973 offset = AD_FILELOCK_DENY_RD;
1974 break;
1976 case DENY_WRITE:
1977 offset = AD_FILELOCK_DENY_WR;
1978 break;
1980 default:
1981 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1984 if (fork_type == APPLE_FORK_RSRC) {
1985 offset += 2;
1988 return offset;
1992 * Call fcntl() with an exclusive F_GETLK request in order to
1993 * determine if there's an exisiting shared lock
1995 * @return true if the requested lock was found or any error occurred
1996 * false if the lock was not found
1998 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2000 bool result;
2001 off_t offset = in_offset;
2002 off_t len = 1;
2003 int type = F_WRLCK;
2004 pid_t pid;
2006 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2007 if (result == false) {
2008 return true;
2011 if (type != F_UNLCK) {
2012 return true;
2015 return false;
2018 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2019 files_struct *fsp,
2020 uint32_t access_mask,
2021 uint32_t deny_mode)
2023 NTSTATUS status = NT_STATUS_OK;
2024 struct byte_range_lock *br_lck = NULL;
2025 bool open_for_reading, open_for_writing, deny_read, deny_write;
2026 off_t off;
2027 bool have_read = false;
2028 int flags;
2030 /* FIXME: hardcoded data fork, add resource fork */
2031 enum apple_fork fork_type = APPLE_FORK_DATA;
2033 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2034 fsp_str_dbg(fsp),
2035 access_mask & FILE_READ_DATA ? "READ" :"-",
2036 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2037 deny_mode & DENY_READ ? "DENY_READ" : "-",
2038 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2040 if (fsp->fh->fd == -1) {
2041 return NT_STATUS_OK;
2044 flags = fcntl(fsp->fh->fd, F_GETFL);
2045 if (flags == -1) {
2046 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2047 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2048 return map_nt_error_from_unix(errno);
2051 if (flags & (O_RDONLY|O_RDWR)) {
2053 * Applying fcntl read locks requires an fd opened for
2054 * reading. This means we won't be applying locks for
2055 * files openend write-only, but what can we do...
2057 have_read = true;
2061 * Check read access and deny read mode
2063 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2064 /* Check access */
2065 open_for_reading = test_netatalk_lock(
2066 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2068 deny_read = test_netatalk_lock(
2069 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2071 DEBUG(10, ("read: %s, deny_write: %s\n",
2072 open_for_reading == true ? "yes" : "no",
2073 deny_read == true ? "yes" : "no"));
2075 if (((access_mask & FILE_READ_DATA) && deny_read)
2076 || ((deny_mode & DENY_READ) && open_for_reading)) {
2077 return NT_STATUS_SHARING_VIOLATION;
2080 /* Set locks */
2081 if ((access_mask & FILE_READ_DATA) && have_read) {
2082 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2083 br_lck = do_lock(
2084 handle->conn->sconn->msg_ctx, fsp,
2085 fsp->op->global->open_persistent_id, 1, off,
2086 READ_LOCK, POSIX_LOCK, false,
2087 &status, NULL);
2089 if (!NT_STATUS_IS_OK(status)) {
2090 return status;
2092 TALLOC_FREE(br_lck);
2095 if ((deny_mode & DENY_READ) && have_read) {
2096 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2097 br_lck = do_lock(
2098 handle->conn->sconn->msg_ctx, fsp,
2099 fsp->op->global->open_persistent_id, 1, off,
2100 READ_LOCK, POSIX_LOCK, false,
2101 &status, NULL);
2103 if (!NT_STATUS_IS_OK(status)) {
2104 return status;
2106 TALLOC_FREE(br_lck);
2111 * Check write access and deny write mode
2113 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2114 /* Check access */
2115 open_for_writing = test_netatalk_lock(
2116 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2118 deny_write = test_netatalk_lock(
2119 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2121 DEBUG(10, ("write: %s, deny_write: %s\n",
2122 open_for_writing == true ? "yes" : "no",
2123 deny_write == true ? "yes" : "no"));
2125 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2126 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2127 return NT_STATUS_SHARING_VIOLATION;
2130 /* Set locks */
2131 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2132 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2133 br_lck = do_lock(
2134 handle->conn->sconn->msg_ctx, fsp,
2135 fsp->op->global->open_persistent_id, 1, off,
2136 READ_LOCK, POSIX_LOCK, false,
2137 &status, NULL);
2139 if (!NT_STATUS_IS_OK(status)) {
2140 return status;
2142 TALLOC_FREE(br_lck);
2145 if ((deny_mode & DENY_WRITE) && have_read) {
2146 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2147 br_lck = do_lock(
2148 handle->conn->sconn->msg_ctx, fsp,
2149 fsp->op->global->open_persistent_id, 1, off,
2150 READ_LOCK, POSIX_LOCK, false,
2151 &status, NULL);
2153 if (!NT_STATUS_IS_OK(status)) {
2154 return status;
2156 TALLOC_FREE(br_lck);
2160 TALLOC_FREE(br_lck);
2162 return status;
2165 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2166 struct smb_request *req,
2167 const struct smb2_create_blobs *in_context_blobs,
2168 struct smb2_create_blobs *out_context_blobs)
2170 struct fruit_config_data *config;
2171 NTSTATUS status;
2172 struct smb2_create_blob *aapl = NULL;
2173 uint32_t cmd;
2174 bool ok;
2175 uint8_t p[16];
2176 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2177 uint64_t req_bitmap, client_caps;
2178 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2179 smb_ucs2_t *model;
2180 size_t modellen;
2182 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2183 return NT_STATUS_UNSUCCESSFUL);
2185 if (!config->use_aapl
2186 || in_context_blobs == NULL
2187 || out_context_blobs == NULL) {
2188 return NT_STATUS_OK;
2191 aapl = smb2_create_blob_find(in_context_blobs,
2192 SMB2_CREATE_TAG_AAPL);
2193 if (aapl == NULL) {
2194 return NT_STATUS_OK;
2197 if (aapl->data.length != 24) {
2198 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2199 (uintmax_t)aapl->data.length));
2200 return NT_STATUS_INVALID_PARAMETER;
2203 cmd = IVAL(aapl->data.data, 0);
2204 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2205 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2206 return NT_STATUS_INVALID_PARAMETER;
2209 req_bitmap = BVAL(aapl->data.data, 8);
2210 client_caps = BVAL(aapl->data.data, 16);
2212 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2213 SIVAL(p, 4, 0);
2214 SBVAL(p, 8, req_bitmap);
2215 ok = data_blob_append(req, &blob, p, 16);
2216 if (!ok) {
2217 return NT_STATUS_UNSUCCESSFUL;
2220 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2221 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2222 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2223 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2224 config->readdir_attr_enabled = true;
2227 if (config->use_copyfile) {
2228 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2229 config->copyfile_enabled = true;
2233 * The client doesn't set the flag, so we can't check
2234 * for it and just set it unconditionally
2236 if (config->unix_info_enabled) {
2237 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2240 SBVAL(p, 0, server_caps);
2241 ok = data_blob_append(req, &blob, p, 8);
2242 if (!ok) {
2243 return NT_STATUS_UNSUCCESSFUL;
2247 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2248 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2249 uint64_t caps = 0;
2251 switch (val) {
2252 case Auto:
2253 break;
2255 case True:
2256 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2257 break;
2259 default:
2260 break;
2263 SBVAL(p, 0, caps);
2265 ok = data_blob_append(req, &blob, p, 8);
2266 if (!ok) {
2267 return NT_STATUS_UNSUCCESSFUL;
2271 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2272 ok = convert_string_talloc(req,
2273 CH_UNIX, CH_UTF16LE,
2274 "Samba", strlen("Samba"),
2275 &model, &modellen);
2276 if (!ok) {
2277 return NT_STATUS_UNSUCCESSFUL;
2280 SIVAL(p, 0, 0);
2281 SIVAL(p + 4, 0, modellen);
2282 ok = data_blob_append(req, &blob, p, 8);
2283 if (!ok) {
2284 talloc_free(model);
2285 return NT_STATUS_UNSUCCESSFUL;
2288 ok = data_blob_append(req, &blob, model, modellen);
2289 talloc_free(model);
2290 if (!ok) {
2291 return NT_STATUS_UNSUCCESSFUL;
2295 status = smb2_create_blob_add(out_context_blobs,
2296 out_context_blobs,
2297 SMB2_CREATE_TAG_AAPL,
2298 blob);
2299 if (NT_STATUS_IS_OK(status)) {
2300 global_fruit_config.nego_aapl = true;
2301 if (config->aapl_zero_file_id) {
2302 aapl_force_zero_file_id(handle->conn->sconn);
2306 return status;
2309 static bool readdir_attr_meta_finderi_stream(
2310 struct vfs_handle_struct *handle,
2311 const struct smb_filename *smb_fname,
2312 AfpInfo *ai)
2314 struct smb_filename *stream_name = NULL;
2315 files_struct *fsp = NULL;
2316 ssize_t nread;
2317 NTSTATUS status;
2318 int ret;
2319 bool ok;
2320 uint8_t buf[AFP_INFO_SIZE];
2322 stream_name = synthetic_smb_fname(talloc_tos(),
2323 smb_fname->base_name,
2324 AFPINFO_STREAM_NAME,
2325 NULL, smb_fname->flags);
2326 if (stream_name == NULL) {
2327 return false;
2330 ret = SMB_VFS_STAT(handle->conn, stream_name);
2331 if (ret != 0) {
2332 return false;
2335 status = SMB_VFS_CREATE_FILE(
2336 handle->conn, /* conn */
2337 NULL, /* req */
2338 0, /* root_dir_fid */
2339 stream_name, /* fname */
2340 FILE_READ_DATA, /* access_mask */
2341 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2342 FILE_SHARE_DELETE),
2343 FILE_OPEN, /* create_disposition*/
2344 0, /* create_options */
2345 0, /* file_attributes */
2346 INTERNAL_OPEN_ONLY, /* oplock_request */
2347 NULL, /* lease */
2348 0, /* allocation_size */
2349 0, /* private_flags */
2350 NULL, /* sd */
2351 NULL, /* ea_list */
2352 &fsp, /* result */
2353 NULL, /* pinfo */
2354 NULL, NULL); /* create context */
2356 TALLOC_FREE(stream_name);
2358 if (!NT_STATUS_IS_OK(status)) {
2359 return false;
2362 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2363 if (nread != AFP_INFO_SIZE) {
2364 DBG_ERR("short read [%s] [%zd/%d]\n",
2365 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2366 ok = false;
2367 goto fail;
2370 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2371 AFP_FinderSize);
2373 ok = true;
2375 fail:
2376 if (fsp != NULL) {
2377 close_file(NULL, fsp, NORMAL_CLOSE);
2380 return ok;
2383 static bool readdir_attr_meta_finderi_netatalk(
2384 struct vfs_handle_struct *handle,
2385 const struct smb_filename *smb_fname,
2386 AfpInfo *ai)
2388 struct adouble *ad = NULL;
2389 char *p = NULL;
2391 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2392 if (ad == NULL) {
2393 return false;
2396 p = ad_get_entry(ad, ADEID_FINDERI);
2397 if (p == NULL) {
2398 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2399 TALLOC_FREE(ad);
2400 return false;
2403 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2404 TALLOC_FREE(ad);
2405 return true;
2408 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2409 const struct smb_filename *smb_fname,
2410 struct readdir_attr_data *attr_data)
2412 struct fruit_config_data *config = NULL;
2413 uint32_t date_added;
2414 AfpInfo ai = {0};
2415 bool ok;
2417 SMB_VFS_HANDLE_GET_DATA(handle, config,
2418 struct fruit_config_data,
2419 return false);
2421 switch (config->meta) {
2422 case FRUIT_META_NETATALK:
2423 ok = readdir_attr_meta_finderi_netatalk(
2424 handle, smb_fname, &ai);
2425 break;
2427 case FRUIT_META_STREAM:
2428 ok = readdir_attr_meta_finderi_stream(
2429 handle, smb_fname, &ai);
2430 break;
2432 default:
2433 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2434 return false;
2437 if (!ok) {
2438 /* Don't bother with errors, it's likely ENOENT */
2439 return true;
2442 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2443 /* finder_type */
2444 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2445 &ai.afpi_FinderInfo[0], 4);
2447 /* finder_creator */
2448 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2449 &ai.afpi_FinderInfo[4], 4);
2452 /* finder_flags */
2453 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2454 &ai.afpi_FinderInfo[8], 2);
2456 /* finder_ext_flags */
2457 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2458 &ai.afpi_FinderInfo[24], 2);
2460 /* creation date */
2461 date_added = convert_time_t_to_uint32_t(
2462 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2464 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2466 return true;
2469 static uint64_t readdir_attr_rfork_size_adouble(
2470 struct vfs_handle_struct *handle,
2471 const struct smb_filename *smb_fname)
2473 struct adouble *ad = NULL;
2474 uint64_t rfork_size;
2476 ad = ad_get(talloc_tos(), handle, smb_fname,
2477 ADOUBLE_RSRC);
2478 if (ad == NULL) {
2479 return 0;
2482 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2483 TALLOC_FREE(ad);
2485 return rfork_size;
2488 static uint64_t readdir_attr_rfork_size_stream(
2489 struct vfs_handle_struct *handle,
2490 const struct smb_filename *smb_fname)
2492 struct smb_filename *stream_name = NULL;
2493 int ret;
2494 uint64_t rfork_size;
2496 stream_name = synthetic_smb_fname(talloc_tos(),
2497 smb_fname->base_name,
2498 AFPRESOURCE_STREAM_NAME,
2499 NULL, 0);
2500 if (stream_name == NULL) {
2501 return 0;
2504 ret = SMB_VFS_STAT(handle->conn, stream_name);
2505 if (ret != 0) {
2506 TALLOC_FREE(stream_name);
2507 return 0;
2510 rfork_size = stream_name->st.st_ex_size;
2511 TALLOC_FREE(stream_name);
2513 return rfork_size;
2516 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2517 const struct smb_filename *smb_fname)
2519 struct fruit_config_data *config = NULL;
2520 uint64_t rfork_size;
2522 SMB_VFS_HANDLE_GET_DATA(handle, config,
2523 struct fruit_config_data,
2524 return 0);
2526 switch (config->rsrc) {
2527 case FRUIT_RSRC_ADFILE:
2528 case FRUIT_RSRC_XATTR:
2529 rfork_size = readdir_attr_rfork_size_adouble(handle,
2530 smb_fname);
2531 break;
2533 case FRUIT_META_STREAM:
2534 rfork_size = readdir_attr_rfork_size_stream(handle,
2535 smb_fname);
2536 break;
2538 default:
2539 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2540 rfork_size = 0;
2541 break;
2544 return rfork_size;
2547 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2548 const struct smb_filename *smb_fname,
2549 struct readdir_attr_data *attr_data)
2551 NTSTATUS status = NT_STATUS_OK;
2552 struct fruit_config_data *config = NULL;
2553 bool ok;
2555 SMB_VFS_HANDLE_GET_DATA(handle, config,
2556 struct fruit_config_data,
2557 return NT_STATUS_UNSUCCESSFUL);
2560 /* Ensure we return a default value in the creation_date field */
2561 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2564 * Resource fork length
2567 if (config->readdir_attr_rsize) {
2568 uint64_t rfork_size;
2570 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2571 attr_data->attr_data.aapl.rfork_size = rfork_size;
2575 * FinderInfo
2578 if (config->readdir_attr_finder_info) {
2579 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2580 if (!ok) {
2581 status = NT_STATUS_INTERNAL_ERROR;
2585 return status;
2588 /* Search MS NFS style ACE with UNIX mode */
2589 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2590 files_struct *fsp,
2591 const struct security_descriptor *psd,
2592 mode_t *pmode,
2593 bool *pdo_chmod)
2595 uint32_t i;
2596 struct fruit_config_data *config = NULL;
2598 *pdo_chmod = false;
2600 SMB_VFS_HANDLE_GET_DATA(handle, config,
2601 struct fruit_config_data,
2602 return NT_STATUS_UNSUCCESSFUL);
2604 if (psd->dacl == NULL || !config->unix_info_enabled) {
2605 return NT_STATUS_OK;
2608 for (i = 0; i < psd->dacl->num_aces; i++) {
2609 if (dom_sid_compare_domain(
2610 &global_sid_Unix_NFS_Mode,
2611 &psd->dacl->aces[i].trustee) == 0) {
2612 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2613 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2614 *pdo_chmod = true;
2616 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2617 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2618 break;
2622 return NT_STATUS_OK;
2625 /****************************************************************************
2626 * VFS ops
2627 ****************************************************************************/
2629 static int fruit_connect(vfs_handle_struct *handle,
2630 const char *service,
2631 const char *user)
2633 int rc;
2634 char *list = NULL, *newlist = NULL;
2635 struct fruit_config_data *config;
2637 DEBUG(10, ("fruit_connect\n"));
2639 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2640 if (rc < 0) {
2641 return rc;
2644 rc = init_fruit_config(handle);
2645 if (rc != 0) {
2646 return rc;
2649 SMB_VFS_HANDLE_GET_DATA(handle, config,
2650 struct fruit_config_data, return -1);
2652 if (config->veto_appledouble) {
2653 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2655 if (list) {
2656 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2657 newlist = talloc_asprintf(
2658 list,
2659 "%s/" ADOUBLE_NAME_PREFIX "*/",
2660 list);
2661 lp_do_parameter(SNUM(handle->conn),
2662 "veto files",
2663 newlist);
2665 } else {
2666 lp_do_parameter(SNUM(handle->conn),
2667 "veto files",
2668 "/" ADOUBLE_NAME_PREFIX "*/");
2671 TALLOC_FREE(list);
2674 if (config->encoding == FRUIT_ENC_NATIVE) {
2675 lp_do_parameter(
2676 SNUM(handle->conn),
2677 "catia:mappings",
2678 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2679 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2680 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2681 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2682 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2683 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2684 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2685 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2686 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2687 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2688 "0x0d:0xf00d");
2691 return rc;
2694 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2695 struct smb_filename *smb_fname,
2696 files_struct *fsp,
2697 int flags,
2698 mode_t mode)
2700 AfpInfo *ai = NULL;
2701 char afpinfo_buf[AFP_INFO_SIZE];
2702 ssize_t len, written;
2703 int hostfd = -1;
2704 int rc = -1;
2706 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2707 if (hostfd == -1) {
2708 return -1;
2711 if (!(flags & (O_CREAT | O_TRUNC))) {
2712 return hostfd;
2715 ai = afpinfo_new(talloc_tos());
2716 if (ai == NULL) {
2717 rc = -1;
2718 goto fail;
2721 len = afpinfo_pack(ai, afpinfo_buf);
2722 if (len != AFP_INFO_SIZE) {
2723 rc = -1;
2724 goto fail;
2727 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2728 fsp->fh->fd = hostfd;
2730 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2731 AFP_INFO_SIZE, 0);
2732 fsp->fh->fd = -1;
2733 if (written != AFP_INFO_SIZE) {
2734 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2735 rc = -1;
2736 goto fail;
2739 rc = 0;
2741 fail:
2742 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2744 if (rc != 0) {
2745 int saved_errno = errno;
2746 if (hostfd >= 0) {
2747 fsp->fh->fd = hostfd;
2748 SMB_VFS_NEXT_CLOSE(handle, fsp);
2750 hostfd = -1;
2751 errno = saved_errno;
2753 return hostfd;
2756 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2757 struct smb_filename *smb_fname,
2758 files_struct *fsp,
2759 int flags,
2760 mode_t mode)
2762 int rc = 0;
2763 struct smb_filename *smb_fname_base = NULL;
2764 int baseflags;
2765 int hostfd = -1;
2766 struct adouble *ad = NULL;
2768 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2770 /* Create an smb_filename with stream_name == NULL. */
2771 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2772 smb_fname->base_name,
2773 NULL,
2774 NULL,
2775 smb_fname->flags);
2777 if (smb_fname_base == NULL) {
2778 errno = ENOMEM;
2779 rc = -1;
2780 goto exit;
2784 * We use baseflags to turn off nasty side-effects when opening the
2785 * underlying file.
2787 baseflags = flags;
2788 baseflags &= ~O_TRUNC;
2789 baseflags &= ~O_EXCL;
2790 baseflags &= ~O_CREAT;
2792 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2793 baseflags, mode);
2796 * It is legit to open a stream on a directory, but the base
2797 * fd has to be read-only.
2799 if ((hostfd == -1) && (errno == EISDIR)) {
2800 baseflags &= ~O_ACCMODE;
2801 baseflags |= O_RDONLY;
2802 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2803 baseflags, mode);
2806 TALLOC_FREE(smb_fname_base);
2808 if (hostfd == -1) {
2809 rc = -1;
2810 goto exit;
2813 if (flags & (O_CREAT | O_TRUNC)) {
2815 * The attribute does not exist or needs to be truncated,
2816 * create an AppleDouble EA
2818 ad = ad_init(fsp, handle, ADOUBLE_META);
2819 if (ad == NULL) {
2820 rc = -1;
2821 goto exit;
2824 fsp->fh->fd = hostfd;
2826 rc = ad_fset(ad, fsp);
2827 fsp->fh->fd = -1;
2828 if (rc != 0) {
2829 rc = -1;
2830 goto exit;
2833 TALLOC_FREE(ad);
2836 exit:
2837 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2838 if (rc != 0) {
2839 int saved_errno = errno;
2840 if (hostfd >= 0) {
2842 * BUGBUGBUG -- we would need to call
2843 * fd_close_posix here, but we don't have a
2844 * full fsp yet
2846 fsp->fh->fd = hostfd;
2847 SMB_VFS_NEXT_CLOSE(handle, fsp);
2849 hostfd = -1;
2850 errno = saved_errno;
2852 return hostfd;
2855 static int fruit_open_meta(vfs_handle_struct *handle,
2856 struct smb_filename *smb_fname,
2857 files_struct *fsp, int flags, mode_t mode)
2859 int fd;
2860 struct fruit_config_data *config = NULL;
2861 struct fio *fio = NULL;
2863 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2865 SMB_VFS_HANDLE_GET_DATA(handle, config,
2866 struct fruit_config_data, return -1);
2868 switch (config->meta) {
2869 case FRUIT_META_STREAM:
2870 fd = fruit_open_meta_stream(handle, smb_fname,
2871 fsp, flags, mode);
2872 break;
2874 case FRUIT_META_NETATALK:
2875 fd = fruit_open_meta_netatalk(handle, smb_fname,
2876 fsp, flags, mode);
2877 break;
2879 default:
2880 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2881 return -1;
2884 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2886 if (fd == -1) {
2887 return -1;
2890 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
2891 fio->type = ADOUBLE_META;
2892 fio->config = config;
2894 return fd;
2897 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2898 struct smb_filename *smb_fname,
2899 files_struct *fsp,
2900 int flags,
2901 mode_t mode)
2903 int rc = 0;
2904 struct adouble *ad = NULL;
2905 struct smb_filename *smb_fname_base = NULL;
2906 struct fruit_config_data *config = NULL;
2907 int hostfd = -1;
2909 SMB_VFS_HANDLE_GET_DATA(handle, config,
2910 struct fruit_config_data, return -1);
2912 if ((!(flags & O_CREAT)) &&
2913 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
2915 /* sorry, but directories don't habe a resource fork */
2916 rc = -1;
2917 goto exit;
2920 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
2921 if (rc != 0) {
2922 goto exit;
2925 /* Sanitize flags */
2926 if (flags & O_WRONLY) {
2927 /* We always need read access for the metadata header too */
2928 flags &= ~O_WRONLY;
2929 flags |= O_RDWR;
2932 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2933 flags, mode);
2934 if (hostfd == -1) {
2935 rc = -1;
2936 goto exit;
2939 if (flags & (O_CREAT | O_TRUNC)) {
2940 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
2941 if (ad == NULL) {
2942 rc = -1;
2943 goto exit;
2946 fsp->fh->fd = hostfd;
2948 rc = ad_fset(ad, fsp);
2949 fsp->fh->fd = -1;
2950 if (rc != 0) {
2951 rc = -1;
2952 goto exit;
2954 TALLOC_FREE(ad);
2957 exit:
2959 TALLOC_FREE(smb_fname_base);
2961 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2962 if (rc != 0) {
2963 int saved_errno = errno;
2964 if (hostfd >= 0) {
2966 * BUGBUGBUG -- we would need to call
2967 * fd_close_posix here, but we don't have a
2968 * full fsp yet
2970 fsp->fh->fd = hostfd;
2971 SMB_VFS_CLOSE(fsp);
2973 hostfd = -1;
2974 errno = saved_errno;
2976 return hostfd;
2979 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2980 struct smb_filename *smb_fname,
2981 files_struct *fsp,
2982 int flags,
2983 mode_t mode)
2985 #ifdef HAVE_ATTROPEN
2986 int fd = -1;
2988 fd = attropen(smb_fname->base_name,
2989 AFPRESOURCE_EA_NETATALK,
2990 flags,
2991 mode);
2992 if (fd == -1) {
2993 return -1;
2996 return fd;
2998 #else
2999 errno = ENOSYS;
3000 return -1;
3001 #endif
3004 static int fruit_open_rsrc(vfs_handle_struct *handle,
3005 struct smb_filename *smb_fname,
3006 files_struct *fsp, int flags, mode_t mode)
3008 int fd;
3009 struct fruit_config_data *config = NULL;
3010 struct fio *fio = NULL;
3012 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3014 SMB_VFS_HANDLE_GET_DATA(handle, config,
3015 struct fruit_config_data, return -1);
3017 if (((flags & O_ACCMODE) == O_RDONLY)
3018 && (flags & O_CREAT)
3019 && !VALID_STAT(fsp->fsp_name->st))
3022 * This means the stream doesn't exist. macOS SMB server fails
3023 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3024 * 12565 and the test for this combination in
3025 * test_rfork_create().
3027 errno = ENOENT;
3028 return -1;
3031 switch (config->rsrc) {
3032 case FRUIT_RSRC_STREAM:
3033 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3034 break;
3036 case FRUIT_RSRC_ADFILE:
3037 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3038 fsp, flags, mode);
3039 break;
3041 case FRUIT_RSRC_XATTR:
3042 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3043 fsp, flags, mode);
3044 break;
3046 default:
3047 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3048 return -1;
3051 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3053 if (fd == -1) {
3054 return -1;
3057 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3058 fio->type = ADOUBLE_RSRC;
3059 fio->config = config;
3061 return fd;
3064 static int fruit_open(vfs_handle_struct *handle,
3065 struct smb_filename *smb_fname,
3066 files_struct *fsp, int flags, mode_t mode)
3068 int fd;
3070 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3072 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3073 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3076 if (is_afpinfo_stream(smb_fname)) {
3077 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3078 } else if (is_afpresource_stream(smb_fname)) {
3079 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3080 } else {
3081 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3084 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3086 return fd;
3089 static int fruit_rename(struct vfs_handle_struct *handle,
3090 const struct smb_filename *smb_fname_src,
3091 const struct smb_filename *smb_fname_dst)
3093 int rc = -1;
3094 struct fruit_config_data *config = NULL;
3095 struct smb_filename *src_adp_smb_fname = NULL;
3096 struct smb_filename *dst_adp_smb_fname = NULL;
3098 SMB_VFS_HANDLE_GET_DATA(handle, config,
3099 struct fruit_config_data, return -1);
3101 if (!VALID_STAT(smb_fname_src->st)) {
3102 DBG_ERR("Need valid stat for [%s]\n",
3103 smb_fname_str_dbg(smb_fname_src));
3104 return -1;
3107 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3108 if (rc != 0) {
3109 return -1;
3112 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3113 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3115 return 0;
3118 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3119 if (rc != 0) {
3120 goto done;
3123 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3124 if (rc != 0) {
3125 goto done;
3128 DBG_DEBUG("%s -> %s\n",
3129 smb_fname_str_dbg(src_adp_smb_fname),
3130 smb_fname_str_dbg(dst_adp_smb_fname));
3132 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3133 if (errno == ENOENT) {
3134 rc = 0;
3137 done:
3138 TALLOC_FREE(src_adp_smb_fname);
3139 TALLOC_FREE(dst_adp_smb_fname);
3140 return rc;
3143 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3144 const struct smb_filename *smb_fname)
3146 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3149 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3150 const struct smb_filename *smb_fname)
3152 return SMB_VFS_REMOVEXATTR(handle->conn,
3153 smb_fname,
3154 AFPINFO_EA_NETATALK);
3157 static int fruit_unlink_meta(vfs_handle_struct *handle,
3158 const struct smb_filename *smb_fname)
3160 struct fruit_config_data *config = NULL;
3161 int rc;
3163 SMB_VFS_HANDLE_GET_DATA(handle, config,
3164 struct fruit_config_data, return -1);
3166 switch (config->meta) {
3167 case FRUIT_META_STREAM:
3168 rc = fruit_unlink_meta_stream(handle, smb_fname);
3169 break;
3171 case FRUIT_META_NETATALK:
3172 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3173 break;
3175 default:
3176 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3177 return -1;
3180 return rc;
3183 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3184 const struct smb_filename *smb_fname,
3185 bool force_unlink)
3187 int ret;
3189 if (!force_unlink) {
3190 struct smb_filename *smb_fname_cp = NULL;
3191 off_t size;
3193 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3194 if (smb_fname_cp == NULL) {
3195 return -1;
3199 * 0 byte resource fork streams are not listed by
3200 * vfs_streaminfo, as a result stream cleanup/deletion of file
3201 * deletion doesn't remove the resourcefork stream.
3204 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3205 if (ret != 0) {
3206 TALLOC_FREE(smb_fname_cp);
3207 DBG_ERR("stat [%s] failed [%s]\n",
3208 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3209 return -1;
3212 size = smb_fname_cp->st.st_ex_size;
3213 TALLOC_FREE(smb_fname_cp);
3215 if (size > 0) {
3216 /* OS X ignores resource fork stream delete requests */
3217 return 0;
3221 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3222 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3223 ret = 0;
3226 return ret;
3229 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3230 const struct smb_filename *smb_fname,
3231 bool force_unlink)
3233 int rc;
3234 struct adouble *ad = NULL;
3235 struct smb_filename *adp_smb_fname = NULL;
3237 if (!force_unlink) {
3238 ad = ad_get(talloc_tos(), handle, smb_fname,
3239 ADOUBLE_RSRC);
3240 if (ad == NULL) {
3241 errno = ENOENT;
3242 return -1;
3247 * 0 byte resource fork streams are not listed by
3248 * vfs_streaminfo, as a result stream cleanup/deletion of file
3249 * deletion doesn't remove the resourcefork stream.
3252 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3253 /* OS X ignores resource fork stream delete requests */
3254 TALLOC_FREE(ad);
3255 return 0;
3258 TALLOC_FREE(ad);
3261 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3262 if (rc != 0) {
3263 return -1;
3266 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3267 TALLOC_FREE(adp_smb_fname);
3268 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3269 rc = 0;
3272 return rc;
3275 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3276 const struct smb_filename *smb_fname,
3277 bool force_unlink)
3280 * OS X ignores resource fork stream delete requests, so nothing to do
3281 * here. Removing the file will remove the xattr anyway, so we don't
3282 * have to take care of removing 0 byte resource forks that could be
3283 * left behind.
3285 return 0;
3288 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3289 const struct smb_filename *smb_fname,
3290 bool force_unlink)
3292 struct fruit_config_data *config = NULL;
3293 int rc;
3295 SMB_VFS_HANDLE_GET_DATA(handle, config,
3296 struct fruit_config_data, return -1);
3298 switch (config->rsrc) {
3299 case FRUIT_RSRC_STREAM:
3300 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3301 break;
3303 case FRUIT_RSRC_ADFILE:
3304 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3305 break;
3307 case FRUIT_RSRC_XATTR:
3308 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3309 break;
3311 default:
3312 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3313 return -1;
3316 return rc;
3319 static int fruit_unlink(vfs_handle_struct *handle,
3320 const struct smb_filename *smb_fname)
3322 int rc;
3323 struct fruit_config_data *config = NULL;
3324 struct smb_filename *rsrc_smb_fname = NULL;
3326 SMB_VFS_HANDLE_GET_DATA(handle, config,
3327 struct fruit_config_data, return -1);
3329 if (is_afpinfo_stream(smb_fname)) {
3330 return fruit_unlink_meta(handle, smb_fname);
3331 } else if (is_afpresource_stream(smb_fname)) {
3332 return fruit_unlink_rsrc(handle, smb_fname, false);
3333 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3334 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3338 * A request to delete the base file. Because 0 byte resource
3339 * fork streams are not listed by fruit_streaminfo,
3340 * delete_all_streams() can't remove 0 byte resource fork
3341 * streams, so we have to cleanup this here.
3343 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3344 smb_fname->base_name,
3345 AFPRESOURCE_STREAM_NAME,
3346 NULL,
3347 smb_fname->flags);
3348 if (rsrc_smb_fname == NULL) {
3349 return -1;
3352 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3353 if ((rc != 0) && (errno != ENOENT)) {
3354 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3355 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3356 TALLOC_FREE(rsrc_smb_fname);
3357 return -1;
3359 TALLOC_FREE(rsrc_smb_fname);
3361 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3364 static int fruit_chmod(vfs_handle_struct *handle,
3365 const struct smb_filename *smb_fname,
3366 mode_t mode)
3368 int rc = -1;
3369 struct fruit_config_data *config = NULL;
3370 struct smb_filename *smb_fname_adp = NULL;
3372 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3373 if (rc != 0) {
3374 return rc;
3377 SMB_VFS_HANDLE_GET_DATA(handle, config,
3378 struct fruit_config_data, return -1);
3380 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3381 return 0;
3384 if (!VALID_STAT(smb_fname->st)) {
3385 return 0;
3388 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3389 return 0;
3392 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3393 if (rc != 0) {
3394 return -1;
3397 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3399 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3400 if (errno == ENOENT) {
3401 rc = 0;
3404 TALLOC_FREE(smb_fname_adp);
3405 return rc;
3408 static int fruit_chown(vfs_handle_struct *handle,
3409 const struct smb_filename *smb_fname,
3410 uid_t uid,
3411 gid_t gid)
3413 int rc = -1;
3414 struct fruit_config_data *config = NULL;
3415 struct smb_filename *adp_smb_fname = NULL;
3417 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3418 if (rc != 0) {
3419 return rc;
3422 SMB_VFS_HANDLE_GET_DATA(handle, config,
3423 struct fruit_config_data, return -1);
3425 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3426 return 0;
3429 if (!VALID_STAT(smb_fname->st)) {
3430 return 0;
3433 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3434 return 0;
3437 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3438 if (rc != 0) {
3439 goto done;
3442 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3444 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3445 if (errno == ENOENT) {
3446 rc = 0;
3449 done:
3450 TALLOC_FREE(adp_smb_fname);
3451 return rc;
3454 static int fruit_rmdir(struct vfs_handle_struct *handle,
3455 const struct smb_filename *smb_fname)
3457 DIR *dh = NULL;
3458 struct dirent *de;
3459 struct fruit_config_data *config;
3461 SMB_VFS_HANDLE_GET_DATA(handle, config,
3462 struct fruit_config_data, return -1);
3464 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3465 goto exit_rmdir;
3469 * Due to there is no way to change bDeleteVetoFiles variable
3470 * from this module, need to clean up ourselves
3473 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3474 if (dh == NULL) {
3475 goto exit_rmdir;
3478 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3479 int match;
3480 struct adouble *ad = NULL;
3481 char *p = NULL;
3482 struct smb_filename *ad_smb_fname = NULL;
3483 int ret;
3485 match = strncmp(de->d_name,
3486 ADOUBLE_NAME_PREFIX,
3487 strlen(ADOUBLE_NAME_PREFIX));
3488 if (match != 0) {
3489 continue;
3492 p = talloc_asprintf(talloc_tos(), "%s/%s",
3493 smb_fname->base_name, de->d_name);
3494 if (p == NULL) {
3495 DBG_ERR("talloc_asprintf failed\n");
3496 return -1;
3499 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3500 NULL, NULL,
3501 smb_fname->flags);
3502 TALLOC_FREE(p);
3503 if (ad_smb_fname == NULL) {
3504 DBG_ERR("synthetic_smb_fname failed\n");
3505 return -1;
3509 * Check whether it's a valid AppleDouble file, if
3510 * yes, delete it, ignore it otherwise.
3512 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3513 if (ad == NULL) {
3514 TALLOC_FREE(ad_smb_fname);
3515 TALLOC_FREE(p);
3516 continue;
3518 TALLOC_FREE(ad);
3520 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3521 TALLOC_FREE(ad_smb_fname);
3522 if (ret != 0) {
3523 DBG_ERR("Deleting [%s] failed\n",
3524 smb_fname_str_dbg(ad_smb_fname));
3528 exit_rmdir:
3529 if (dh) {
3530 closedir(dh);
3532 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3535 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3536 files_struct *fsp, void *data,
3537 size_t n, off_t offset)
3539 ssize_t nread;
3540 int ret;
3542 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3544 if (nread == n) {
3545 return nread;
3548 DBG_ERR("Removing [%s] after short read [%zd]\n",
3549 fsp_str_dbg(fsp), nread);
3551 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3552 if (ret != 0) {
3553 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3554 return -1;
3557 errno = EINVAL;
3558 return -1;
3561 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3562 files_struct *fsp, void *data,
3563 size_t n, off_t offset)
3565 AfpInfo *ai = NULL;
3566 struct adouble *ad = NULL;
3567 char afpinfo_buf[AFP_INFO_SIZE];
3568 char *p = NULL;
3569 ssize_t nread;
3571 ai = afpinfo_new(talloc_tos());
3572 if (ai == NULL) {
3573 return -1;
3576 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3577 if (ad == NULL) {
3578 nread = -1;
3579 goto fail;
3582 p = ad_get_entry(ad, ADEID_FINDERI);
3583 if (p == NULL) {
3584 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3585 nread = -1;
3586 goto fail;
3589 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3591 nread = afpinfo_pack(ai, afpinfo_buf);
3592 if (nread != AFP_INFO_SIZE) {
3593 nread = -1;
3594 goto fail;
3597 memcpy(data, afpinfo_buf, n);
3598 nread = n;
3600 fail:
3601 TALLOC_FREE(ai);
3602 return nread;
3605 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3606 files_struct *fsp, void *data,
3607 size_t n, off_t offset)
3609 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3610 ssize_t nread;
3611 ssize_t to_return;
3614 * OS X has a off-by-1 error in the offset calculation, so we're
3615 * bug compatible here. It won't hurt, as any relevant real
3616 * world read requests from the AFP_AfpInfo stream will be
3617 * offset=0 n=60. offset is ignored anyway, see below.
3619 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3620 return 0;
3623 /* Yes, macOS always reads from offset 0 */
3624 offset = 0;
3625 to_return = MIN(n, AFP_INFO_SIZE);
3627 switch (fio->config->meta) {
3628 case FRUIT_META_STREAM:
3629 nread = fruit_pread_meta_stream(handle, fsp, data,
3630 to_return, offset);
3631 break;
3633 case FRUIT_META_NETATALK:
3634 nread = fruit_pread_meta_adouble(handle, fsp, data,
3635 to_return, offset);
3636 break;
3638 default:
3639 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3640 return -1;
3643 return nread;
3646 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3647 files_struct *fsp, void *data,
3648 size_t n, off_t offset)
3650 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3653 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3654 files_struct *fsp, void *data,
3655 size_t n, off_t offset)
3657 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3660 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3661 files_struct *fsp, void *data,
3662 size_t n, off_t offset)
3664 struct adouble *ad = NULL;
3665 ssize_t nread;
3667 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3668 if (ad == NULL) {
3669 return -1;
3672 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3673 offset + ad_getentryoff(ad, ADEID_RFORK));
3675 TALLOC_FREE(ad);
3676 return nread;
3679 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3680 files_struct *fsp, void *data,
3681 size_t n, off_t offset)
3683 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3684 ssize_t nread;
3686 switch (fio->config->rsrc) {
3687 case FRUIT_RSRC_STREAM:
3688 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3689 break;
3691 case FRUIT_RSRC_ADFILE:
3692 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3693 break;
3695 case FRUIT_RSRC_XATTR:
3696 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3697 break;
3699 default:
3700 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3701 return -1;
3704 return nread;
3707 static ssize_t fruit_pread(vfs_handle_struct *handle,
3708 files_struct *fsp, void *data,
3709 size_t n, off_t offset)
3711 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3712 ssize_t nread;
3714 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3715 fsp_str_dbg(fsp), (intmax_t)offset, n);
3717 if (fio == NULL) {
3718 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3721 if (fio->type == ADOUBLE_META) {
3722 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3723 } else {
3724 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3727 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3728 return nread;
3731 static bool fruit_must_handle_aio_stream(struct fio *fio)
3733 if (fio == NULL) {
3734 return false;
3737 if ((fio->type == ADOUBLE_META) &&
3738 (fio->config->meta == FRUIT_META_NETATALK))
3740 return true;
3743 if ((fio->type == ADOUBLE_RSRC) &&
3744 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
3746 return true;
3749 return false;
3752 struct fruit_pread_state {
3753 ssize_t nread;
3754 struct vfs_aio_state vfs_aio_state;
3757 static void fruit_pread_done(struct tevent_req *subreq);
3759 static struct tevent_req *fruit_pread_send(
3760 struct vfs_handle_struct *handle,
3761 TALLOC_CTX *mem_ctx,
3762 struct tevent_context *ev,
3763 struct files_struct *fsp,
3764 void *data,
3765 size_t n, off_t offset)
3767 struct tevent_req *req = NULL;
3768 struct tevent_req *subreq = NULL;
3769 struct fruit_pread_state *state = NULL;
3770 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3772 req = tevent_req_create(mem_ctx, &state,
3773 struct fruit_pread_state);
3774 if (req == NULL) {
3775 return NULL;
3778 if (fruit_must_handle_aio_stream(fio)) {
3779 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
3780 if (state->nread != n) {
3781 if (state->nread != -1) {
3782 errno = EIO;
3784 tevent_req_error(req, errno);
3785 return tevent_req_post(req, ev);
3787 tevent_req_done(req);
3788 return tevent_req_post(req, ev);
3791 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
3792 data, n, offset);
3793 if (tevent_req_nomem(req, subreq)) {
3794 return tevent_req_post(req, ev);
3796 tevent_req_set_callback(subreq, fruit_pread_done, req);
3797 return req;
3800 static void fruit_pread_done(struct tevent_req *subreq)
3802 struct tevent_req *req = tevent_req_callback_data(
3803 subreq, struct tevent_req);
3804 struct fruit_pread_state *state = tevent_req_data(
3805 req, struct fruit_pread_state);
3807 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
3808 TALLOC_FREE(subreq);
3810 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3811 return;
3813 tevent_req_done(req);
3816 static ssize_t fruit_pread_recv(struct tevent_req *req,
3817 struct vfs_aio_state *vfs_aio_state)
3819 struct fruit_pread_state *state = tevent_req_data(
3820 req, struct fruit_pread_state);
3822 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3823 return -1;
3826 *vfs_aio_state = state->vfs_aio_state;
3827 return state->nread;
3830 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
3831 files_struct *fsp, const void *data,
3832 size_t n, off_t offset)
3834 AfpInfo *ai = NULL;
3835 int ret;
3837 ai = afpinfo_unpack(talloc_tos(), data);
3838 if (ai == NULL) {
3839 return -1;
3842 if (ai_empty_finderinfo(ai)) {
3843 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3844 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3845 DBG_ERR("Can't delete metadata for %s: %s\n",
3846 fsp_str_dbg(fsp), strerror(errno));
3847 TALLOC_FREE(ai);
3848 return -1;
3851 return n;
3854 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3857 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
3858 files_struct *fsp, const void *data,
3859 size_t n, off_t offset)
3861 struct adouble *ad = NULL;
3862 AfpInfo *ai = NULL;
3863 char *p = NULL;
3864 int ret;
3866 ai = afpinfo_unpack(talloc_tos(), data);
3867 if (ai == NULL) {
3868 return -1;
3871 if (ai_empty_finderinfo(ai)) {
3872 ret = SMB_VFS_REMOVEXATTR(handle->conn,
3873 fsp->fsp_name,
3874 AFPINFO_EA_NETATALK);
3876 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3877 DBG_ERR("Can't delete metadata for %s: %s\n",
3878 fsp_str_dbg(fsp), strerror(errno));
3879 return -1;
3882 return n;
3885 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3886 if (ad == NULL) {
3887 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
3888 if (ad == NULL) {
3889 return -1;
3892 p = ad_get_entry(ad, ADEID_FINDERI);
3893 if (p == NULL) {
3894 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3895 TALLOC_FREE(ad);
3896 return -1;
3899 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3901 ret = ad_fset(ad, fsp);
3902 if (ret != 0) {
3903 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3904 TALLOC_FREE(ad);
3905 return -1;
3908 TALLOC_FREE(ad);
3909 return n;
3912 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
3913 files_struct *fsp, const void *data,
3914 size_t n, off_t offset)
3916 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3917 ssize_t nwritten;
3920 * Writing an all 0 blob to the metadata stream
3921 * results in the stream being removed on a macOS
3922 * server. This ensures we behave the same and it
3923 * verified by the "delete AFP_AfpInfo by writing all
3924 * 0" test.
3926 if (n != AFP_INFO_SIZE || offset != 0) {
3927 DBG_ERR("unexpected offset=%jd or size=%jd\n",
3928 (intmax_t)offset, (intmax_t)n);
3929 return -1;
3932 switch (fio->config->meta) {
3933 case FRUIT_META_STREAM:
3934 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
3935 n, offset);
3936 break;
3938 case FRUIT_META_NETATALK:
3939 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
3940 n, offset);
3941 break;
3943 default:
3944 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3945 return -1;
3948 return nwritten;
3951 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
3952 files_struct *fsp, const void *data,
3953 size_t n, off_t offset)
3955 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3958 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
3959 files_struct *fsp, const void *data,
3960 size_t n, off_t offset)
3962 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3965 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
3966 files_struct *fsp, const void *data,
3967 size_t n, off_t offset)
3969 struct adouble *ad = NULL;
3970 ssize_t nwritten;
3971 int ret;
3973 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3974 if (ad == NULL) {
3975 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
3976 return -1;
3979 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3980 offset + ad_getentryoff(ad, ADEID_RFORK));
3981 if (nwritten != n) {
3982 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
3983 fsp_str_dbg(fsp), nwritten, n);
3984 TALLOC_FREE(ad);
3985 return -1;
3988 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3989 ad_setentrylen(ad, ADEID_RFORK, n + offset);
3990 ret = ad_fset(ad, fsp);
3991 if (ret != 0) {
3992 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3993 TALLOC_FREE(ad);
3994 return -1;
3998 TALLOC_FREE(ad);
3999 return n;
4002 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4003 files_struct *fsp, const void *data,
4004 size_t n, off_t offset)
4006 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4007 ssize_t nwritten;
4009 switch (fio->config->rsrc) {
4010 case FRUIT_RSRC_STREAM:
4011 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4012 break;
4014 case FRUIT_RSRC_ADFILE:
4015 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4016 break;
4018 case FRUIT_RSRC_XATTR:
4019 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4020 break;
4022 default:
4023 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4024 return -1;
4027 return nwritten;
4030 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4031 files_struct *fsp, const void *data,
4032 size_t n, off_t offset)
4034 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4035 ssize_t nwritten;
4037 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4038 fsp_str_dbg(fsp), (intmax_t)offset, n);
4040 if (fio == NULL) {
4041 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4044 if (fio->type == ADOUBLE_META) {
4045 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4046 } else {
4047 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4050 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4051 return nwritten;
4054 struct fruit_pwrite_state {
4055 ssize_t nwritten;
4056 struct vfs_aio_state vfs_aio_state;
4059 static void fruit_pwrite_done(struct tevent_req *subreq);
4061 static struct tevent_req *fruit_pwrite_send(
4062 struct vfs_handle_struct *handle,
4063 TALLOC_CTX *mem_ctx,
4064 struct tevent_context *ev,
4065 struct files_struct *fsp,
4066 const void *data,
4067 size_t n, off_t offset)
4069 struct tevent_req *req = NULL;
4070 struct tevent_req *subreq = NULL;
4071 struct fruit_pwrite_state *state = NULL;
4072 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4074 req = tevent_req_create(mem_ctx, &state,
4075 struct fruit_pwrite_state);
4076 if (req == NULL) {
4077 return NULL;
4080 if (fruit_must_handle_aio_stream(fio)) {
4081 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4082 if (state->nwritten != n) {
4083 if (state->nwritten != -1) {
4084 errno = EIO;
4086 tevent_req_error(req, errno);
4087 return tevent_req_post(req, ev);
4089 tevent_req_done(req);
4090 return tevent_req_post(req, ev);
4093 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4094 data, n, offset);
4095 if (tevent_req_nomem(req, subreq)) {
4096 return tevent_req_post(req, ev);
4098 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4099 return req;
4102 static void fruit_pwrite_done(struct tevent_req *subreq)
4104 struct tevent_req *req = tevent_req_callback_data(
4105 subreq, struct tevent_req);
4106 struct fruit_pwrite_state *state = tevent_req_data(
4107 req, struct fruit_pwrite_state);
4109 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4110 TALLOC_FREE(subreq);
4112 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4113 return;
4115 tevent_req_done(req);
4118 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4119 struct vfs_aio_state *vfs_aio_state)
4121 struct fruit_pwrite_state *state = tevent_req_data(
4122 req, struct fruit_pwrite_state);
4124 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4125 return -1;
4128 *vfs_aio_state = state->vfs_aio_state;
4129 return state->nwritten;
4133 * Helper to stat/lstat the base file of an smb_fname.
4135 static int fruit_stat_base(vfs_handle_struct *handle,
4136 struct smb_filename *smb_fname,
4137 bool follow_links)
4139 char *tmp_stream_name;
4140 int rc;
4142 tmp_stream_name = smb_fname->stream_name;
4143 smb_fname->stream_name = NULL;
4144 if (follow_links) {
4145 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4146 } else {
4147 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4149 smb_fname->stream_name = tmp_stream_name;
4150 return rc;
4153 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4154 struct smb_filename *smb_fname,
4155 bool follow_links)
4157 int ret;
4159 if (follow_links) {
4160 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4161 } else {
4162 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4165 return ret;
4168 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4169 struct smb_filename *smb_fname,
4170 bool follow_links)
4172 struct adouble *ad = NULL;
4174 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4175 if (ad == NULL) {
4176 DBG_INFO("fruit_stat_meta %s: %s\n",
4177 smb_fname_str_dbg(smb_fname), strerror(errno));
4178 errno = ENOENT;
4179 return -1;
4181 TALLOC_FREE(ad);
4183 /* Populate the stat struct with info from the base file. */
4184 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4185 return -1;
4187 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4188 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4189 smb_fname->stream_name);
4190 return 0;
4193 static int fruit_stat_meta(vfs_handle_struct *handle,
4194 struct smb_filename *smb_fname,
4195 bool follow_links)
4197 struct fruit_config_data *config = NULL;
4198 int ret;
4200 SMB_VFS_HANDLE_GET_DATA(handle, config,
4201 struct fruit_config_data, return -1);
4203 switch (config->meta) {
4204 case FRUIT_META_STREAM:
4205 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4206 break;
4208 case FRUIT_META_NETATALK:
4209 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4210 break;
4212 default:
4213 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4214 return -1;
4217 return ret;
4220 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4221 struct smb_filename *smb_fname,
4222 bool follow_links)
4224 struct adouble *ad = NULL;
4225 int ret;
4227 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4228 if (ad == NULL) {
4229 errno = ENOENT;
4230 return -1;
4233 /* Populate the stat struct with info from the base file. */
4234 ret = fruit_stat_base(handle, smb_fname, follow_links);
4235 if (ret != 0) {
4236 TALLOC_FREE(ad);
4237 return -1;
4240 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4241 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4242 smb_fname->stream_name);
4243 TALLOC_FREE(ad);
4244 return 0;
4247 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4248 struct smb_filename *smb_fname,
4249 bool follow_links)
4251 int ret;
4253 if (follow_links) {
4254 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4255 } else {
4256 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4259 return ret;
4262 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4263 struct smb_filename *smb_fname,
4264 bool follow_links)
4266 #ifdef HAVE_ATTROPEN
4267 int ret;
4268 int fd = -1;
4270 /* Populate the stat struct with info from the base file. */
4271 ret = fruit_stat_base(handle, smb_fname, follow_links);
4272 if (ret != 0) {
4273 return -1;
4276 fd = attropen(smb_fname->base_name,
4277 AFPRESOURCE_EA_NETATALK,
4278 O_RDONLY);
4279 if (fd == -1) {
4280 return 0;
4283 ret = sys_fstat(fd, &smb_fname->st, false);
4284 if (ret != 0) {
4285 close(fd);
4286 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4287 AFPRESOURCE_EA_NETATALK);
4288 return -1;
4290 close(fd);
4291 fd = -1;
4293 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4294 smb_fname->stream_name);
4296 return ret;
4298 #else
4299 errno = ENOSYS;
4300 return -1;
4301 #endif
4304 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4305 struct smb_filename *smb_fname,
4306 bool follow_links)
4308 struct fruit_config_data *config = NULL;
4309 int ret;
4311 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4313 SMB_VFS_HANDLE_GET_DATA(handle, config,
4314 struct fruit_config_data, return -1);
4316 switch (config->rsrc) {
4317 case FRUIT_RSRC_STREAM:
4318 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4319 break;
4321 case FRUIT_RSRC_XATTR:
4322 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4323 break;
4325 case FRUIT_RSRC_ADFILE:
4326 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4327 break;
4329 default:
4330 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4331 return -1;
4334 return ret;
4337 static int fruit_stat(vfs_handle_struct *handle,
4338 struct smb_filename *smb_fname)
4340 int rc = -1;
4342 DEBUG(10, ("fruit_stat called for %s\n",
4343 smb_fname_str_dbg(smb_fname)));
4345 if (!is_ntfs_stream_smb_fname(smb_fname)
4346 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4347 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4348 if (rc == 0) {
4349 update_btime(handle, smb_fname);
4351 return rc;
4355 * Note if lp_posix_paths() is true, we can never
4356 * get here as is_ntfs_stream_smb_fname() is
4357 * always false. So we never need worry about
4358 * not following links here.
4361 if (is_afpinfo_stream(smb_fname)) {
4362 rc = fruit_stat_meta(handle, smb_fname, true);
4363 } else if (is_afpresource_stream(smb_fname)) {
4364 rc = fruit_stat_rsrc(handle, smb_fname, true);
4365 } else {
4366 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4369 if (rc == 0) {
4370 update_btime(handle, smb_fname);
4371 smb_fname->st.st_ex_mode &= ~S_IFMT;
4372 smb_fname->st.st_ex_mode |= S_IFREG;
4373 smb_fname->st.st_ex_blocks =
4374 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4376 return rc;
4379 static int fruit_lstat(vfs_handle_struct *handle,
4380 struct smb_filename *smb_fname)
4382 int rc = -1;
4384 DEBUG(10, ("fruit_lstat called for %s\n",
4385 smb_fname_str_dbg(smb_fname)));
4387 if (!is_ntfs_stream_smb_fname(smb_fname)
4388 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4389 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4390 if (rc == 0) {
4391 update_btime(handle, smb_fname);
4393 return rc;
4396 if (is_afpinfo_stream(smb_fname)) {
4397 rc = fruit_stat_meta(handle, smb_fname, false);
4398 } else if (is_afpresource_stream(smb_fname)) {
4399 rc = fruit_stat_rsrc(handle, smb_fname, false);
4400 } else {
4401 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4404 if (rc == 0) {
4405 update_btime(handle, smb_fname);
4406 smb_fname->st.st_ex_mode &= ~S_IFMT;
4407 smb_fname->st.st_ex_mode |= S_IFREG;
4408 smb_fname->st.st_ex_blocks =
4409 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4411 return rc;
4414 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4415 files_struct *fsp,
4416 SMB_STRUCT_STAT *sbuf)
4418 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4421 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4422 files_struct *fsp,
4423 SMB_STRUCT_STAT *sbuf)
4425 int ret;
4427 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4428 if (ret != 0) {
4429 return -1;
4432 *sbuf = fsp->base_fsp->fsp_name->st;
4433 sbuf->st_ex_size = AFP_INFO_SIZE;
4434 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4436 return 0;
4439 static int fruit_fstat_meta(vfs_handle_struct *handle,
4440 files_struct *fsp,
4441 SMB_STRUCT_STAT *sbuf,
4442 struct fio *fio)
4444 int ret;
4446 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4448 switch (fio->config->meta) {
4449 case FRUIT_META_STREAM:
4450 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4451 break;
4453 case FRUIT_META_NETATALK:
4454 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4455 break;
4457 default:
4458 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4459 return -1;
4462 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4463 return ret;
4466 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4467 files_struct *fsp,
4468 SMB_STRUCT_STAT *sbuf)
4470 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4473 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4474 files_struct *fsp,
4475 SMB_STRUCT_STAT *sbuf)
4477 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4480 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4481 files_struct *fsp,
4482 SMB_STRUCT_STAT *sbuf)
4484 struct adouble *ad = NULL;
4485 int ret;
4487 /* Populate the stat struct with info from the base file. */
4488 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4489 if (ret == -1) {
4490 return -1;
4493 ad = ad_get(talloc_tos(), handle,
4494 fsp->base_fsp->fsp_name,
4495 ADOUBLE_RSRC);
4496 if (ad == NULL) {
4497 DBG_ERR("ad_get [%s] failed [%s]\n",
4498 fsp_str_dbg(fsp), strerror(errno));
4499 return -1;
4502 *sbuf = fsp->base_fsp->fsp_name->st;
4503 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4504 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4506 TALLOC_FREE(ad);
4507 return 0;
4510 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4511 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4513 int ret;
4515 switch (fio->config->rsrc) {
4516 case FRUIT_RSRC_STREAM:
4517 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4518 break;
4520 case FRUIT_RSRC_ADFILE:
4521 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4522 break;
4524 case FRUIT_RSRC_XATTR:
4525 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4526 break;
4528 default:
4529 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4530 return -1;
4533 return ret;
4536 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4537 SMB_STRUCT_STAT *sbuf)
4539 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4540 int rc;
4542 if (fio == NULL) {
4543 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4546 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4548 if (fio->type == ADOUBLE_META) {
4549 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4550 } else {
4551 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4554 if (rc == 0) {
4555 sbuf->st_ex_mode &= ~S_IFMT;
4556 sbuf->st_ex_mode |= S_IFREG;
4557 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4560 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4561 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4562 return rc;
4565 static NTSTATUS fruit_streaminfo_meta_stream(
4566 vfs_handle_struct *handle,
4567 struct files_struct *fsp,
4568 const struct smb_filename *smb_fname,
4569 TALLOC_CTX *mem_ctx,
4570 unsigned int *pnum_streams,
4571 struct stream_struct **pstreams)
4573 struct stream_struct *stream = *pstreams;
4574 unsigned int num_streams = *pnum_streams;
4575 struct smb_filename *sname = NULL;
4576 int i;
4577 int ret;
4578 bool ok;
4580 for (i = 0; i < num_streams; i++) {
4581 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4582 break;
4586 if (i == num_streams) {
4587 return NT_STATUS_OK;
4590 if (stream[i].size == AFP_INFO_SIZE) {
4591 return NT_STATUS_OK;
4594 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4595 "from [%s]\n", (intmax_t)stream[i].size,
4596 smb_fname_str_dbg(smb_fname));
4598 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4599 if (!ok) {
4600 return NT_STATUS_INTERNAL_ERROR;
4603 sname = synthetic_smb_fname(talloc_tos(),
4604 smb_fname->base_name,
4605 AFPINFO_STREAM_NAME,
4606 NULL, 0);
4607 if (sname == NULL) {
4608 return NT_STATUS_NO_MEMORY;
4611 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4612 TALLOC_FREE(sname);
4613 if (ret != 0) {
4614 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4615 return map_nt_error_from_unix(errno);
4618 return NT_STATUS_OK;
4621 static NTSTATUS fruit_streaminfo_meta_netatalk(
4622 vfs_handle_struct *handle,
4623 struct files_struct *fsp,
4624 const struct smb_filename *smb_fname,
4625 TALLOC_CTX *mem_ctx,
4626 unsigned int *pnum_streams,
4627 struct stream_struct **pstreams)
4629 struct stream_struct *stream = *pstreams;
4630 unsigned int num_streams = *pnum_streams;
4631 struct adouble *ad = NULL;
4632 bool is_fi_empty;
4633 int i;
4634 bool ok;
4636 /* Remove the Netatalk xattr from the list */
4637 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4638 ":" NETATALK_META_XATTR ":$DATA");
4639 if (!ok) {
4640 return NT_STATUS_NO_MEMORY;
4644 * Check if there's a AFPINFO_STREAM from the VFS streams
4645 * backend and if yes, remove it from the list
4647 for (i = 0; i < num_streams; i++) {
4648 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4649 break;
4653 if (i < num_streams) {
4654 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4655 smb_fname_str_dbg(smb_fname));
4657 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4658 AFPINFO_STREAM);
4659 if (!ok) {
4660 return NT_STATUS_INTERNAL_ERROR;
4664 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4665 if (ad == NULL) {
4666 return NT_STATUS_OK;
4669 is_fi_empty = ad_empty_finderinfo(ad);
4670 TALLOC_FREE(ad);
4672 if (is_fi_empty) {
4673 return NT_STATUS_OK;
4676 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4677 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4678 smb_roundup(handle->conn, AFP_INFO_SIZE));
4679 if (!ok) {
4680 return NT_STATUS_NO_MEMORY;
4683 return NT_STATUS_OK;
4686 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4687 struct files_struct *fsp,
4688 const struct smb_filename *smb_fname,
4689 TALLOC_CTX *mem_ctx,
4690 unsigned int *pnum_streams,
4691 struct stream_struct **pstreams)
4693 struct fruit_config_data *config = NULL;
4694 NTSTATUS status;
4696 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4697 return NT_STATUS_INTERNAL_ERROR);
4699 switch (config->meta) {
4700 case FRUIT_META_NETATALK:
4701 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4702 mem_ctx, pnum_streams,
4703 pstreams);
4704 break;
4706 case FRUIT_META_STREAM:
4707 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4708 mem_ctx, pnum_streams,
4709 pstreams);
4710 break;
4712 default:
4713 return NT_STATUS_INTERNAL_ERROR;
4716 return status;
4719 static NTSTATUS fruit_streaminfo_rsrc_stream(
4720 vfs_handle_struct *handle,
4721 struct files_struct *fsp,
4722 const struct smb_filename *smb_fname,
4723 TALLOC_CTX *mem_ctx,
4724 unsigned int *pnum_streams,
4725 struct stream_struct **pstreams)
4727 bool ok;
4729 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4730 if (!ok) {
4731 DBG_ERR("Filtering resource stream failed\n");
4732 return NT_STATUS_INTERNAL_ERROR;
4734 return NT_STATUS_OK;
4737 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4738 vfs_handle_struct *handle,
4739 struct files_struct *fsp,
4740 const struct smb_filename *smb_fname,
4741 TALLOC_CTX *mem_ctx,
4742 unsigned int *pnum_streams,
4743 struct stream_struct **pstreams)
4745 bool ok;
4747 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4748 if (!ok) {
4749 DBG_ERR("Filtering resource stream failed\n");
4750 return NT_STATUS_INTERNAL_ERROR;
4752 return NT_STATUS_OK;
4755 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4756 vfs_handle_struct *handle,
4757 struct files_struct *fsp,
4758 const struct smb_filename *smb_fname,
4759 TALLOC_CTX *mem_ctx,
4760 unsigned int *pnum_streams,
4761 struct stream_struct **pstreams)
4763 struct stream_struct *stream = *pstreams;
4764 unsigned int num_streams = *pnum_streams;
4765 struct adouble *ad = NULL;
4766 bool ok;
4767 size_t rlen;
4768 int i;
4771 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4772 * and if yes, remove it from the list
4774 for (i = 0; i < num_streams; i++) {
4775 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4776 break;
4780 if (i < num_streams) {
4781 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4782 smb_fname_str_dbg(smb_fname));
4784 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4785 AFPRESOURCE_STREAM);
4786 if (!ok) {
4787 return NT_STATUS_INTERNAL_ERROR;
4791 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4792 if (ad == NULL) {
4793 return NT_STATUS_OK;
4796 rlen = ad_getentrylen(ad, ADEID_RFORK);
4797 TALLOC_FREE(ad);
4799 if (rlen == 0) {
4800 return NT_STATUS_OK;
4803 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4804 AFPRESOURCE_STREAM_NAME, rlen,
4805 smb_roundup(handle->conn, rlen));
4806 if (!ok) {
4807 return NT_STATUS_NO_MEMORY;
4810 return NT_STATUS_OK;
4813 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4814 struct files_struct *fsp,
4815 const struct smb_filename *smb_fname,
4816 TALLOC_CTX *mem_ctx,
4817 unsigned int *pnum_streams,
4818 struct stream_struct **pstreams)
4820 struct fruit_config_data *config = NULL;
4821 NTSTATUS status;
4823 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4824 return NT_STATUS_INTERNAL_ERROR);
4826 switch (config->rsrc) {
4827 case FRUIT_RSRC_STREAM:
4828 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4829 mem_ctx, pnum_streams,
4830 pstreams);
4831 break;
4833 case FRUIT_RSRC_XATTR:
4834 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4835 mem_ctx, pnum_streams,
4836 pstreams);
4837 break;
4839 case FRUIT_RSRC_ADFILE:
4840 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4841 mem_ctx, pnum_streams,
4842 pstreams);
4843 break;
4845 default:
4846 return NT_STATUS_INTERNAL_ERROR;
4849 return status;
4852 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4853 struct files_struct *fsp,
4854 const struct smb_filename *smb_fname,
4855 TALLOC_CTX *mem_ctx,
4856 unsigned int *pnum_streams,
4857 struct stream_struct **pstreams)
4859 struct fruit_config_data *config = NULL;
4860 NTSTATUS status;
4862 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4863 return NT_STATUS_UNSUCCESSFUL);
4865 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4867 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4868 pnum_streams, pstreams);
4869 if (!NT_STATUS_IS_OK(status)) {
4870 return status;
4873 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4874 mem_ctx, pnum_streams, pstreams);
4875 if (!NT_STATUS_IS_OK(status)) {
4876 return status;
4879 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4880 mem_ctx, pnum_streams, pstreams);
4881 if (!NT_STATUS_IS_OK(status)) {
4882 return status;
4885 return NT_STATUS_OK;
4888 static int fruit_ntimes(vfs_handle_struct *handle,
4889 const struct smb_filename *smb_fname,
4890 struct smb_file_time *ft)
4892 int rc = 0;
4893 struct adouble *ad = NULL;
4894 struct fruit_config_data *config = NULL;
4896 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4897 return -1);
4899 if ((config->meta != FRUIT_META_NETATALK) ||
4900 null_timespec(ft->create_time))
4902 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4905 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4906 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4908 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4909 if (ad == NULL) {
4910 goto exit;
4913 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4914 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4916 rc = ad_set(ad, smb_fname);
4918 exit:
4920 TALLOC_FREE(ad);
4921 if (rc != 0) {
4922 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4923 return -1;
4925 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4928 static int fruit_fallocate(struct vfs_handle_struct *handle,
4929 struct files_struct *fsp,
4930 uint32_t mode,
4931 off_t offset,
4932 off_t len)
4934 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4936 if (fio == NULL) {
4937 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4940 /* Let the pwrite code path handle it. */
4941 errno = ENOSYS;
4942 return -1;
4945 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4946 struct files_struct *fsp,
4947 off_t offset)
4949 if (offset == 0) {
4950 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4953 #ifdef HAVE_ATTROPEN
4954 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4955 #endif
4956 return 0;
4959 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4960 struct files_struct *fsp,
4961 off_t offset)
4963 int rc;
4964 struct adouble *ad = NULL;
4965 off_t ad_off;
4967 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4968 if (ad == NULL) {
4969 DBG_DEBUG("ad_get [%s] failed [%s]\n",
4970 fsp_str_dbg(fsp), strerror(errno));
4971 return -1;
4974 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4976 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset + ad_off);
4977 if (rc != 0) {
4978 TALLOC_FREE(ad);
4979 return -1;
4982 ad_setentrylen(ad, ADEID_RFORK, offset);
4984 rc = ad_fset(ad, fsp);
4985 if (rc != 0) {
4986 DBG_ERR("ad_fset [%s] failed [%s]\n",
4987 fsp_str_dbg(fsp), strerror(errno));
4988 TALLOC_FREE(ad);
4989 return -1;
4992 TALLOC_FREE(ad);
4993 return 0;
4996 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4997 struct files_struct *fsp,
4998 off_t offset)
5000 if (offset == 0) {
5001 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5004 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5007 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5008 struct files_struct *fsp,
5009 off_t offset)
5011 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5012 int ret;
5014 switch (fio->config->rsrc) {
5015 case FRUIT_RSRC_XATTR:
5016 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5017 break;
5019 case FRUIT_RSRC_ADFILE:
5020 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5021 break;
5023 case FRUIT_RSRC_STREAM:
5024 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5025 break;
5027 default:
5028 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5029 return -1;
5033 return ret;
5036 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5037 struct files_struct *fsp,
5038 off_t offset)
5040 if (offset > 60) {
5041 DBG_WARNING("ftruncate %s to %jd",
5042 fsp_str_dbg(fsp), (intmax_t)offset);
5043 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5044 errno = EOVERFLOW;
5045 return -1;
5048 /* OS X returns success but does nothing */
5049 DBG_INFO("ignoring ftruncate %s to %jd\n",
5050 fsp_str_dbg(fsp), (intmax_t)offset);
5051 return 0;
5054 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5055 struct files_struct *fsp,
5056 off_t offset)
5058 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5059 int ret;
5061 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5062 (intmax_t)offset);
5064 if (fio == NULL) {
5065 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5068 if (fio->type == ADOUBLE_META) {
5069 ret = fruit_ftruncate_meta(handle, fsp, offset);
5070 } else {
5071 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5074 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5075 return ret;
5078 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5079 struct smb_request *req,
5080 uint16_t root_dir_fid,
5081 struct smb_filename *smb_fname,
5082 uint32_t access_mask,
5083 uint32_t share_access,
5084 uint32_t create_disposition,
5085 uint32_t create_options,
5086 uint32_t file_attributes,
5087 uint32_t oplock_request,
5088 struct smb2_lease *lease,
5089 uint64_t allocation_size,
5090 uint32_t private_flags,
5091 struct security_descriptor *sd,
5092 struct ea_list *ea_list,
5093 files_struct **result,
5094 int *pinfo,
5095 const struct smb2_create_blobs *in_context_blobs,
5096 struct smb2_create_blobs *out_context_blobs)
5098 NTSTATUS status;
5099 struct fruit_config_data *config = NULL;
5100 files_struct *fsp = NULL;
5102 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5103 if (!NT_STATUS_IS_OK(status)) {
5104 goto fail;
5107 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5108 return NT_STATUS_UNSUCCESSFUL);
5110 status = SMB_VFS_NEXT_CREATE_FILE(
5111 handle, req, root_dir_fid, smb_fname,
5112 access_mask, share_access,
5113 create_disposition, create_options,
5114 file_attributes, oplock_request,
5115 lease,
5116 allocation_size, private_flags,
5117 sd, ea_list, result,
5118 pinfo, in_context_blobs, out_context_blobs);
5119 if (!NT_STATUS_IS_OK(status)) {
5120 return status;
5123 fsp = *result;
5125 if (global_fruit_config.nego_aapl) {
5126 if (config->copyfile_enabled) {
5128 * Set a flag in the fsp. Gets used in
5129 * copychunk to check whether the special
5130 * Apple copyfile semantics for copychunk
5131 * should be allowed in a copychunk request
5132 * with a count of 0.
5134 fsp->aapl_copyfile_supported = true;
5137 if (config->posix_rename && fsp->is_directory) {
5139 * Enable POSIX directory rename behaviour
5141 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5146 * If this is a plain open for existing files, opening an 0
5147 * byte size resource fork MUST fail with
5148 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5150 * Cf the vfs_fruit torture tests in test_rfork_create().
5152 if (is_afpresource_stream(fsp->fsp_name) &&
5153 create_disposition == FILE_OPEN)
5155 if (fsp->fsp_name->st.st_ex_size == 0) {
5156 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5157 goto fail;
5161 if (is_ntfs_stream_smb_fname(smb_fname)
5162 || fsp->is_directory) {
5163 return status;
5166 if (config->locking == FRUIT_LOCKING_NETATALK) {
5167 status = fruit_check_access(
5168 handle, *result,
5169 access_mask,
5170 map_share_mode_to_deny_mode(share_access, 0));
5171 if (!NT_STATUS_IS_OK(status)) {
5172 goto fail;
5176 return status;
5178 fail:
5179 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5181 if (fsp) {
5182 close_file(req, fsp, ERROR_CLOSE);
5183 *result = fsp = NULL;
5186 return status;
5189 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5190 const struct smb_filename *fname,
5191 TALLOC_CTX *mem_ctx,
5192 struct readdir_attr_data **pattr_data)
5194 struct fruit_config_data *config = NULL;
5195 struct readdir_attr_data *attr_data;
5196 NTSTATUS status;
5198 SMB_VFS_HANDLE_GET_DATA(handle, config,
5199 struct fruit_config_data,
5200 return NT_STATUS_UNSUCCESSFUL);
5202 if (!global_fruit_config.nego_aapl) {
5203 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5206 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5208 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5209 if (*pattr_data == NULL) {
5210 return NT_STATUS_UNSUCCESSFUL;
5212 attr_data = *pattr_data;
5213 attr_data->type = RDATTR_AAPL;
5216 * Mac metadata: compressed FinderInfo, resource fork length
5217 * and creation date
5219 status = readdir_attr_macmeta(handle, fname, attr_data);
5220 if (!NT_STATUS_IS_OK(status)) {
5222 * Error handling is tricky: if we return failure from
5223 * this function, the corresponding directory entry
5224 * will to be passed to the client, so we really just
5225 * want to error out on fatal errors.
5227 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5228 goto fail;
5233 * UNIX mode
5235 if (config->unix_info_enabled) {
5236 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5240 * max_access
5242 if (!config->readdir_attr_max_access) {
5243 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5244 } else {
5245 status = smbd_calculate_access_mask(
5246 handle->conn,
5247 fname,
5248 false,
5249 SEC_FLAG_MAXIMUM_ALLOWED,
5250 &attr_data->attr_data.aapl.max_access);
5251 if (!NT_STATUS_IS_OK(status)) {
5252 goto fail;
5256 return NT_STATUS_OK;
5258 fail:
5259 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5260 fname->base_name, nt_errstr(status)));
5261 TALLOC_FREE(*pattr_data);
5262 return status;
5265 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5266 files_struct *fsp,
5267 uint32_t security_info,
5268 TALLOC_CTX *mem_ctx,
5269 struct security_descriptor **ppdesc)
5271 NTSTATUS status;
5272 struct security_ace ace;
5273 struct dom_sid sid;
5274 struct fruit_config_data *config;
5276 SMB_VFS_HANDLE_GET_DATA(handle, config,
5277 struct fruit_config_data,
5278 return NT_STATUS_UNSUCCESSFUL);
5280 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5281 mem_ctx, ppdesc);
5282 if (!NT_STATUS_IS_OK(status)) {
5283 return status;
5287 * Add MS NFS style ACEs with uid, gid and mode
5289 if (!config->unix_info_enabled) {
5290 return NT_STATUS_OK;
5293 /* MS NFS style mode */
5294 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5295 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5296 status = security_descriptor_dacl_add(*ppdesc, &ace);
5297 if (!NT_STATUS_IS_OK(status)) {
5298 DEBUG(1,("failed to add MS NFS style ACE\n"));
5299 return status;
5302 /* MS NFS style uid */
5303 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5304 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5305 status = security_descriptor_dacl_add(*ppdesc, &ace);
5306 if (!NT_STATUS_IS_OK(status)) {
5307 DEBUG(1,("failed to add MS NFS style ACE\n"));
5308 return status;
5311 /* MS NFS style gid */
5312 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5313 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5314 status = security_descriptor_dacl_add(*ppdesc, &ace);
5315 if (!NT_STATUS_IS_OK(status)) {
5316 DEBUG(1,("failed to add MS NFS style ACE\n"));
5317 return status;
5320 return NT_STATUS_OK;
5323 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5324 files_struct *fsp,
5325 uint32_t security_info_sent,
5326 const struct security_descriptor *psd)
5328 NTSTATUS status;
5329 bool do_chmod;
5330 mode_t ms_nfs_mode = 0;
5331 int result;
5333 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5335 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5336 if (!NT_STATUS_IS_OK(status)) {
5337 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5338 return status;
5341 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5342 if (!NT_STATUS_IS_OK(status)) {
5343 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5344 return status;
5347 if (do_chmod) {
5348 if (fsp->fh->fd != -1) {
5349 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5350 } else {
5351 result = SMB_VFS_CHMOD(fsp->conn,
5352 fsp->fsp_name,
5353 ms_nfs_mode);
5356 if (result != 0) {
5357 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5358 result, (unsigned)ms_nfs_mode,
5359 strerror(errno)));
5360 status = map_nt_error_from_unix(errno);
5361 return status;
5365 return NT_STATUS_OK;
5368 struct fruit_copy_chunk_state {
5369 struct vfs_handle_struct *handle;
5370 off_t copied;
5371 struct files_struct *src_fsp;
5372 struct files_struct *dst_fsp;
5373 bool is_copyfile;
5376 static void fruit_copy_chunk_done(struct tevent_req *subreq);
5377 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
5378 TALLOC_CTX *mem_ctx,
5379 struct tevent_context *ev,
5380 struct files_struct *src_fsp,
5381 off_t src_off,
5382 struct files_struct *dest_fsp,
5383 off_t dest_off,
5384 off_t num,
5385 uint32_t flags)
5387 struct tevent_req *req, *subreq;
5388 struct fruit_copy_chunk_state *fruit_copy_chunk_state;
5389 NTSTATUS status;
5390 struct fruit_config_data *config;
5391 off_t to_copy = num;
5393 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5394 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5396 SMB_VFS_HANDLE_GET_DATA(handle, config,
5397 struct fruit_config_data,
5398 return NULL);
5400 req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
5401 struct fruit_copy_chunk_state);
5402 if (req == NULL) {
5403 return NULL;
5405 fruit_copy_chunk_state->handle = handle;
5406 fruit_copy_chunk_state->src_fsp = src_fsp;
5407 fruit_copy_chunk_state->dst_fsp = dest_fsp;
5410 * Check if this a OS X copyfile style copychunk request with
5411 * a requested chunk count of 0 that was translated to a
5412 * copy_chunk_send VFS call overloading the parameters src_off
5413 * = dest_off = num = 0.
5415 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
5416 src_fsp->aapl_copyfile_supported &&
5417 dest_fsp->aapl_copyfile_supported)
5419 status = vfs_stat_fsp(src_fsp);
5420 if (tevent_req_nterror(req, status)) {
5421 return tevent_req_post(req, ev);
5424 to_copy = src_fsp->fsp_name->st.st_ex_size;
5425 fruit_copy_chunk_state->is_copyfile = true;
5428 subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
5429 mem_ctx,
5431 src_fsp,
5432 src_off,
5433 dest_fsp,
5434 dest_off,
5435 to_copy,
5436 flags);
5437 if (tevent_req_nomem(subreq, req)) {
5438 return tevent_req_post(req, ev);
5441 tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
5442 return req;
5445 static void fruit_copy_chunk_done(struct tevent_req *subreq)
5447 struct tevent_req *req = tevent_req_callback_data(
5448 subreq, struct tevent_req);
5449 struct fruit_copy_chunk_state *state = tevent_req_data(
5450 req, struct fruit_copy_chunk_state);
5451 NTSTATUS status;
5452 unsigned int num_streams = 0;
5453 struct stream_struct *streams = NULL;
5454 unsigned int i;
5455 struct smb_filename *src_fname_tmp = NULL;
5456 struct smb_filename *dst_fname_tmp = NULL;
5458 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
5459 subreq,
5460 &state->copied);
5461 TALLOC_FREE(subreq);
5462 if (tevent_req_nterror(req, status)) {
5463 return;
5466 if (!state->is_copyfile) {
5467 tevent_req_done(req);
5468 return;
5472 * Now copy all remaining streams. We know the share supports
5473 * streams, because we're in vfs_fruit. We don't do this async
5474 * because streams are few and small.
5476 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5477 state->src_fsp->fsp_name,
5478 req, &num_streams, &streams);
5479 if (tevent_req_nterror(req, status)) {
5480 return;
5483 if (num_streams == 1) {
5484 /* There is always one stream, ::$DATA. */
5485 tevent_req_done(req);
5486 return;
5489 for (i = 0; i < num_streams; i++) {
5490 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5491 __func__, streams[i].name, (size_t)streams[i].size));
5493 src_fname_tmp = synthetic_smb_fname(
5494 req,
5495 state->src_fsp->fsp_name->base_name,
5496 streams[i].name,
5497 NULL,
5498 state->src_fsp->fsp_name->flags);
5499 if (tevent_req_nomem(src_fname_tmp, req)) {
5500 return;
5503 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5504 TALLOC_FREE(src_fname_tmp);
5505 continue;
5508 dst_fname_tmp = synthetic_smb_fname(
5509 req,
5510 state->dst_fsp->fsp_name->base_name,
5511 streams[i].name,
5512 NULL,
5513 state->dst_fsp->fsp_name->flags);
5514 if (tevent_req_nomem(dst_fname_tmp, req)) {
5515 TALLOC_FREE(src_fname_tmp);
5516 return;
5519 status = copy_file(req,
5520 state->handle->conn,
5521 src_fname_tmp,
5522 dst_fname_tmp,
5523 OPENX_FILE_CREATE_IF_NOT_EXIST,
5524 0, false);
5525 if (!NT_STATUS_IS_OK(status)) {
5526 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5527 smb_fname_str_dbg(src_fname_tmp),
5528 smb_fname_str_dbg(dst_fname_tmp),
5529 nt_errstr(status)));
5530 TALLOC_FREE(src_fname_tmp);
5531 TALLOC_FREE(dst_fname_tmp);
5532 tevent_req_nterror(req, status);
5533 return;
5536 TALLOC_FREE(src_fname_tmp);
5537 TALLOC_FREE(dst_fname_tmp);
5540 TALLOC_FREE(streams);
5541 TALLOC_FREE(src_fname_tmp);
5542 TALLOC_FREE(dst_fname_tmp);
5543 tevent_req_done(req);
5546 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
5547 struct tevent_req *req,
5548 off_t *copied)
5550 struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
5551 req, struct fruit_copy_chunk_state);
5552 NTSTATUS status;
5554 if (tevent_req_is_nterror(req, &status)) {
5555 DEBUG(1, ("server side copy chunk failed: %s\n",
5556 nt_errstr(status)));
5557 *copied = 0;
5558 tevent_req_received(req);
5559 return status;
5562 *copied = fruit_copy_chunk_state->copied;
5563 tevent_req_received(req);
5565 return NT_STATUS_OK;
5568 static struct vfs_fn_pointers vfs_fruit_fns = {
5569 .connect_fn = fruit_connect,
5571 /* File operations */
5572 .chmod_fn = fruit_chmod,
5573 .chown_fn = fruit_chown,
5574 .unlink_fn = fruit_unlink,
5575 .rename_fn = fruit_rename,
5576 .rmdir_fn = fruit_rmdir,
5577 .open_fn = fruit_open,
5578 .pread_fn = fruit_pread,
5579 .pwrite_fn = fruit_pwrite,
5580 .pread_send_fn = fruit_pread_send,
5581 .pread_recv_fn = fruit_pread_recv,
5582 .pwrite_send_fn = fruit_pwrite_send,
5583 .pwrite_recv_fn = fruit_pwrite_recv,
5584 .stat_fn = fruit_stat,
5585 .lstat_fn = fruit_lstat,
5586 .fstat_fn = fruit_fstat,
5587 .streaminfo_fn = fruit_streaminfo,
5588 .ntimes_fn = fruit_ntimes,
5589 .ftruncate_fn = fruit_ftruncate,
5590 .fallocate_fn = fruit_fallocate,
5591 .create_file_fn = fruit_create_file,
5592 .readdir_attr_fn = fruit_readdir_attr,
5593 .copy_chunk_send_fn = fruit_copy_chunk_send,
5594 .copy_chunk_recv_fn = fruit_copy_chunk_recv,
5596 /* NT ACL operations */
5597 .fget_nt_acl_fn = fruit_fget_nt_acl,
5598 .fset_nt_acl_fn = fruit_fset_nt_acl,
5601 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
5602 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5604 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5605 &vfs_fruit_fns);
5606 if (!NT_STATUS_IS_OK(ret)) {
5607 return ret;
5610 vfs_fruit_debug_level = debug_add_class("fruit");
5611 if (vfs_fruit_debug_level == -1) {
5612 vfs_fruit_debug_level = DBGC_VFS;
5613 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5614 "vfs_fruit_init"));
5615 } else {
5616 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5617 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5620 return ret;