auth: Log the transport connection for the authorization
[Samba.git] / source3 / modules / vfs_fruit.c
blob0b5558a4487486a35475ba47b35b431de69a3315
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"
36 * Enhanced OS X and Netatalk compatibility
37 * ========================================
39 * This modules takes advantage of vfs_streams_xattr and
40 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
41 * loaded in the correct order:
43 * vfs modules = catia fruit streams_xattr
45 * The module intercepts the OS X special streams "AFP_AfpInfo" and
46 * "AFP_Resource" and handles them in a special way. All other named
47 * streams are deferred to vfs_streams_xattr.
49 * The OS X client maps all NTFS illegal characters to the Unicode
50 * private range. This module optionally stores the charcters using
51 * their native ASCII encoding using vfs_catia. If you're not enabling
52 * this feature, you can skip catia from vfs modules.
54 * Finally, open modes are optionally checked against Netatalk AFP
55 * share modes.
57 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
58 * extended metadata for files and directories. This module optionally
59 * reads and stores this metadata in a way compatible with Netatalk 3
60 * which stores the metadata in an EA "org.netatalk.metadata". Cf
61 * source3/include/MacExtensions.h for a description of the binary
62 * blobs content.
64 * The "AFP_Resource" named stream may be arbitrarily large, thus it
65 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
66 * the only available filesystem where xattrs can be of any size and
67 * the OS supports using the file APIs for xattrs.
69 * The AFP_Resource stream is stored in an AppleDouble file prepending
70 * "._" to the filename. On Solaris with ZFS the stream is optionally
71 * stored in an EA "org.netatalk.resource".
74 * Extended Attributes
75 * ===================
77 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
78 * other protocols you may want to adjust the xattr names the VFS
79 * module vfs_streams_xattr uses for storing ADS's. This defaults to
80 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
81 * these module parameters:
83 * streams_xattr:prefix = user.
84 * streams_xattr:store_stream_type = false
87 * TODO
88 * ====
90 * - log diagnostic if any needed VFS module is not loaded
91 * (eg with lp_vfs_objects())
92 * - add tests
95 static int vfs_fruit_debug_level = DBGC_VFS;
97 static struct global_fruit_config {
98 bool nego_aapl; /* client negotiated AAPL */
100 } global_fruit_config;
102 #undef DBGC_CLASS
103 #define DBGC_CLASS vfs_fruit_debug_level
105 #define FRUIT_PARAM_TYPE_NAME "fruit"
106 #define ADOUBLE_NAME_PREFIX "._"
108 #define NETATALK_META_XATTR "org.netatalk.Metadata"
109 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
111 #if defined(HAVE_ATTROPEN)
112 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
113 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
114 #else
115 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
117 #endif
119 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
121 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
122 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
123 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
124 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
126 struct fruit_config_data {
127 enum fruit_rsrc rsrc;
128 enum fruit_meta meta;
129 enum fruit_locking locking;
130 enum fruit_encoding encoding;
131 bool use_aapl; /* config from smb.conf */
132 bool use_copyfile;
133 bool readdir_attr_enabled;
134 bool unix_info_enabled;
135 bool copyfile_enabled;
136 bool veto_appledouble;
137 bool posix_rename;
138 bool aapl_zero_file_id;
141 * Additional options, all enabled by default,
142 * possibly useful for analyzing performance. The associated
143 * operations with each of them may be expensive, so having
144 * the chance to disable them individually gives a chance
145 * tweaking the setup for the particular usecase.
147 bool readdir_attr_rsize;
148 bool readdir_attr_finder_info;
149 bool readdir_attr_max_access;
152 static const struct enum_list fruit_rsrc[] = {
153 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
154 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
155 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
156 { -1, NULL}
159 static const struct enum_list fruit_meta[] = {
160 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
161 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
162 { -1, NULL}
165 static const struct enum_list fruit_locking[] = {
166 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
167 {FRUIT_LOCKING_NONE, "none"},
168 { -1, NULL}
171 static const struct enum_list fruit_encoding[] = {
172 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
173 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
174 { -1, NULL}
177 /*****************************************************************************
178 * Defines, functions and data structures that deal with AppleDouble
179 *****************************************************************************/
182 * There are two AppleDouble blobs we deal with:
184 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
185 * metadata in an xattr
187 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
188 * ._ files
190 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
192 /* Version info */
193 #define AD_VERSION2 0x00020000
194 #define AD_VERSION AD_VERSION2
197 * AppleDouble entry IDs.
199 #define ADEID_DFORK 1
200 #define ADEID_RFORK 2
201 #define ADEID_NAME 3
202 #define ADEID_COMMENT 4
203 #define ADEID_ICONBW 5
204 #define ADEID_ICONCOL 6
205 #define ADEID_FILEI 7
206 #define ADEID_FILEDATESI 8
207 #define ADEID_FINDERI 9
208 #define ADEID_MACFILEI 10
209 #define ADEID_PRODOSFILEI 11
210 #define ADEID_MSDOSFILEI 12
211 #define ADEID_SHORTNAME 13
212 #define ADEID_AFPFILEI 14
213 #define ADEID_DID 15
215 /* Private Netatalk entries */
216 #define ADEID_PRIVDEV 16
217 #define ADEID_PRIVINO 17
218 #define ADEID_PRIVSYN 18
219 #define ADEID_PRIVID 19
220 #define ADEID_MAX (ADEID_PRIVID + 1)
223 * These are the real ids for the private entries,
224 * as stored in the adouble file
226 #define AD_DEV 0x80444556
227 #define AD_INO 0x80494E4F
228 #define AD_SYN 0x8053594E
229 #define AD_ID 0x8053567E
231 /* Number of actually used entries */
232 #define ADEID_NUM_XATTR 8
233 #define ADEID_NUM_DOT_UND 2
234 #define ADEID_NUM_RSRC_XATTR 1
236 /* AppleDouble magic */
237 #define AD_APPLESINGLE_MAGIC 0x00051600
238 #define AD_APPLEDOUBLE_MAGIC 0x00051607
239 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
241 /* Sizes of relevant entry bits */
242 #define ADEDLEN_MAGIC 4
243 #define ADEDLEN_VERSION 4
244 #define ADEDLEN_FILLER 16
245 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
246 #define ADEDLEN_NENTRIES 2
247 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
248 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
249 #define AD_ENTRY_LEN_EID 4
250 #define AD_ENTRY_LEN_OFF 4
251 #define AD_ENTRY_LEN_LEN 4
252 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
254 /* Field widths */
255 #define ADEDLEN_NAME 255
256 #define ADEDLEN_COMMENT 200
257 #define ADEDLEN_FILEI 16
258 #define ADEDLEN_FINDERI 32
259 #define ADEDLEN_FILEDATESI 16
260 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
261 #define ADEDLEN_AFPFILEI 4
262 #define ADEDLEN_MACFILEI 4
263 #define ADEDLEN_PRODOSFILEI 8
264 #define ADEDLEN_MSDOSFILEI 2
265 #define ADEDLEN_DID 4
266 #define ADEDLEN_PRIVDEV 8
267 #define ADEDLEN_PRIVINO 8
268 #define ADEDLEN_PRIVSYN 8
269 #define ADEDLEN_PRIVID 4
271 /* Offsets */
272 #define ADEDOFF_MAGIC 0
273 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
274 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
275 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
277 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
278 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
279 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
280 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
281 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
282 ADEDLEN_FILEDATESI)
283 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
284 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
285 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
286 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
288 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
289 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
290 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
292 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
293 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
294 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
295 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
296 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
297 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
299 #if AD_DATASZ_XATTR != 402
300 #error bad size for AD_DATASZ_XATTR
301 #endif
303 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
304 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
305 ADEDLEN_FINDERI)
306 #if AD_DATASZ_DOT_UND != 82
307 #error bad size for AD_DATASZ_DOT_UND
308 #endif
311 * Sharemode locks fcntl() offsets
313 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
314 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
315 #else
316 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
317 #endif
318 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
320 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
321 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
322 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
323 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
324 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
325 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
326 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
327 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
328 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
329 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
331 /* Time stuff we overload the bits a little */
332 #define AD_DATE_CREATE 0
333 #define AD_DATE_MODIFY 4
334 #define AD_DATE_BACKUP 8
335 #define AD_DATE_ACCESS 12
336 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
337 AD_DATE_BACKUP | AD_DATE_ACCESS)
338 #define AD_DATE_UNIX (1 << 10)
339 #define AD_DATE_START 0x80000000
340 #define AD_DATE_DELTA 946684800
341 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
342 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
344 /* Accessor macros */
345 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
346 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
347 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
348 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
350 struct ad_entry {
351 size_t ade_off;
352 size_t ade_len;
355 struct adouble {
356 vfs_handle_struct *ad_handle;
357 int ad_fd;
358 bool ad_opened;
359 adouble_type_t ad_type;
360 uint32_t ad_magic;
361 uint32_t ad_version;
362 struct ad_entry ad_eid[ADEID_MAX];
363 char *ad_data;
366 struct ad_entry_order {
367 uint32_t id, offset, len;
370 /* Netatalk AppleDouble metadata xattr */
371 static const
372 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
373 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
374 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
375 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
376 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
377 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
378 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
379 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
380 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
381 {0, 0, 0}
384 /* AppleDouble resource fork file (the ones prefixed by "._") */
385 static const
386 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
387 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
388 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
389 {0, 0, 0}
393 * Fake AppleDouble entry oder for resource fork xattr. The xattr
394 * isn't an AppleDouble file, it simply contains the resource data,
395 * but in order to be able to use some API calls like ad_getentryoff()
396 * we build a fake/helper struct adouble with this entry order struct.
398 static const
399 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
400 {ADEID_RFORK, 0, 0},
401 {0, 0, 0}
404 /* Conversion from enumerated id to on-disk AppleDouble id */
405 #define AD_EID_DISK(a) (set_eid[a])
406 static const uint32_t set_eid[] = {
407 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
408 AD_DEV, AD_INO, AD_SYN, AD_ID
411 struct fio {
412 /* tcon config handle */
413 struct fruit_config_data *config;
415 /* Denote stream type, meta or rsrc */
416 adouble_type_t type;
420 * Forward declarations
422 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
423 adouble_type_t type);
424 static int ad_set(struct adouble *ad, const char *path);
425 static int ad_fset(struct adouble *ad, files_struct *fsp);
426 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
429 * Return a pointer to an AppleDouble entry
431 * Returns NULL if the entry is not present
433 static char *ad_get_entry(const struct adouble *ad, int eid)
435 off_t off = ad_getentryoff(ad, eid);
436 size_t len = ad_getentrylen(ad, eid);
438 if (off == 0 || len == 0) {
439 return NULL;
442 return ad->ad_data + off;
446 * Get a date
448 static int ad_getdate(const struct adouble *ad,
449 unsigned int dateoff,
450 uint32_t *date)
452 bool xlate = (dateoff & AD_DATE_UNIX);
453 char *p = NULL;
455 dateoff &= AD_DATE_MASK;
456 p = ad_get_entry(ad, ADEID_FILEDATESI);
457 if (p == NULL) {
458 return -1;
461 if (dateoff > AD_DATE_ACCESS) {
462 return -1;
465 memcpy(date, p + dateoff, sizeof(uint32_t));
467 if (xlate) {
468 *date = AD_DATE_TO_UNIX(*date);
470 return 0;
474 * Set a date
476 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
478 bool xlate = (dateoff & AD_DATE_UNIX);
479 char *p = NULL;
481 p = ad_get_entry(ad, ADEID_FILEDATESI);
482 if (p == NULL) {
483 return -1;
486 dateoff &= AD_DATE_MASK;
487 if (xlate) {
488 date = AD_DATE_FROM_UNIX(date);
491 if (dateoff > AD_DATE_ACCESS) {
492 return -1;
495 memcpy(p + dateoff, &date, sizeof(date));
497 return 0;
502 * Map on-disk AppleDouble id to enumerated id
504 static uint32_t get_eid(uint32_t eid)
506 if (eid <= 15) {
507 return eid;
510 switch (eid) {
511 case AD_DEV:
512 return ADEID_PRIVDEV;
513 case AD_INO:
514 return ADEID_PRIVINO;
515 case AD_SYN:
516 return ADEID_PRIVSYN;
517 case AD_ID:
518 return ADEID_PRIVID;
519 default:
520 break;
523 return 0;
527 * Pack AppleDouble structure into data buffer
529 static bool ad_pack(struct adouble *ad)
531 uint32_t eid;
532 uint16_t nent;
533 uint32_t bufsize;
534 uint32_t offset = 0;
536 bufsize = talloc_get_size(ad->ad_data);
538 if (offset + ADEDLEN_MAGIC < offset ||
539 offset + ADEDLEN_MAGIC >= bufsize) {
540 return false;
542 RSIVAL(ad->ad_data, offset, ad->ad_magic);
543 offset += ADEDLEN_MAGIC;
545 if (offset + ADEDLEN_VERSION < offset ||
546 offset + ADEDLEN_VERSION >= bufsize) {
547 return false;
549 RSIVAL(ad->ad_data, offset, ad->ad_version);
550 offset += ADEDLEN_VERSION;
552 if (offset + ADEDLEN_FILLER < offset ||
553 offset + ADEDLEN_FILLER >= bufsize) {
554 return false;
556 if (ad->ad_type == ADOUBLE_RSRC) {
557 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
559 offset += ADEDLEN_FILLER;
561 if (offset + ADEDLEN_NENTRIES < offset ||
562 offset + ADEDLEN_NENTRIES >= bufsize) {
563 return false;
565 offset += ADEDLEN_NENTRIES;
567 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
568 if (ad->ad_eid[eid].ade_off == 0) {
570 * ade_off is also used as indicator whether a
571 * specific entry is used or not
573 continue;
576 if (offset + AD_ENTRY_LEN_EID < offset ||
577 offset + AD_ENTRY_LEN_EID >= bufsize) {
578 return false;
580 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
581 offset += AD_ENTRY_LEN_EID;
583 if (offset + AD_ENTRY_LEN_OFF < offset ||
584 offset + AD_ENTRY_LEN_OFF >= bufsize) {
585 return false;
587 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
588 offset += AD_ENTRY_LEN_OFF;
590 if (offset + AD_ENTRY_LEN_LEN < offset ||
591 offset + AD_ENTRY_LEN_LEN >= bufsize) {
592 return false;
594 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
595 offset += AD_ENTRY_LEN_LEN;
597 nent++;
600 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
601 return false;
603 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
605 return true;
609 * Unpack an AppleDouble blob into a struct adoble
611 static bool ad_unpack(struct adouble *ad, const size_t nentries,
612 size_t filesize)
614 size_t bufsize = talloc_get_size(ad->ad_data);
615 size_t adentries, i;
616 uint32_t eid, len, off;
619 * The size of the buffer ad->ad_data is checked when read, so
620 * we wouldn't have to check our own offsets, a few extra
621 * checks won't hurt though. We have to check the offsets we
622 * read from the buffer anyway.
625 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
626 DEBUG(1, ("bad size\n"));
627 return false;
630 ad->ad_magic = RIVAL(ad->ad_data, 0);
631 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
632 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
633 DEBUG(1, ("wrong magic or version\n"));
634 return false;
637 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
638 if (adentries != nentries) {
639 DEBUG(1, ("invalid number of entries: %zu\n",
640 adentries));
641 return false;
644 /* now, read in the entry bits */
645 for (i = 0; i < adentries; i++) {
646 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
647 eid = get_eid(eid);
648 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
649 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
651 if (!eid || eid > ADEID_MAX) {
652 DEBUG(1, ("bogus eid %d\n", eid));
653 return false;
657 * All entries other than the resource fork are
658 * expected to be read into the ad_data buffer, so
659 * ensure the specified offset is within that bound
661 if ((off > bufsize) && (eid != ADEID_RFORK)) {
662 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
663 eid, off, len));
664 return false;
668 * All entries besides FinderInfo and resource fork
669 * must fit into the buffer. FinderInfo is special as
670 * it may be larger then the default 32 bytes (if it
671 * contains marshalled xattrs), but we will fixup that
672 * in ad_convert(). And the resource fork is never
673 * accessed directly by the ad_data buf (also see
674 * comment above) anyway.
676 if ((eid != ADEID_RFORK) &&
677 (eid != ADEID_FINDERI) &&
678 ((off + len) > bufsize)) {
679 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
680 eid, off, len));
681 return false;
685 * That would be obviously broken
687 if (off > filesize) {
688 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
689 eid, off, len));
690 return false;
694 * Check for any entry that has its end beyond the
695 * filesize.
697 if (off + len < off) {
698 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
699 ", len: %" PRIu32 "\n",
700 eid, off, len));
701 return false;
704 if (off + len > filesize) {
706 * If this is the resource fork entry, we fix
707 * up the length, for any other entry we bail
708 * out.
710 if (eid != ADEID_RFORK) {
711 DEBUG(1, ("bogus eid %d: off: %" PRIu32
712 ", len: %" PRIu32 "\n",
713 eid, off, len));
714 return false;
718 * Fixup the resource fork entry by limiting
719 * the size to entryoffset - filesize.
721 len = filesize - off;
722 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
723 ", len: %" PRIu32 "\n", off, len));
726 ad->ad_eid[eid].ade_off = off;
727 ad->ad_eid[eid].ade_len = len;
730 return true;
734 * Convert from Apple's ._ file to Netatalk
736 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
737 * bytes containing packed xattrs. Netatalk can't deal with that, so
738 * we simply discard the packed xattrs.
740 * @return -1 in case an error occurred, 0 if no conversion was done, 1
741 * otherwise
743 static int ad_convert(struct adouble *ad, int fd)
745 int rc = 0;
746 char *map = MAP_FAILED;
747 size_t origlen;
749 origlen = ad_getentryoff(ad, ADEID_RFORK) +
750 ad_getentrylen(ad, ADEID_RFORK);
752 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
753 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
754 if (map == MAP_FAILED) {
755 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
756 rc = -1;
757 goto exit;
760 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
761 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
762 map + ad_getentryoff(ad, ADEID_RFORK),
763 ad_getentrylen(ad, ADEID_RFORK));
766 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
767 ad_setentryoff(ad, ADEID_RFORK,
768 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
771 * FIXME: direct ftruncate(), but we don't have a fsp for the
772 * VFS call
774 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
775 + ad_getentrylen(ad, ADEID_RFORK));
777 exit:
778 if (map != MAP_FAILED) {
779 munmap(map, origlen);
781 return rc;
785 * Read and parse Netatalk AppleDouble metadata xattr
787 static ssize_t ad_read_meta(struct adouble *ad, const char *path)
789 int rc = 0;
790 ssize_t ealen;
791 bool ok;
793 DEBUG(10, ("reading meta xattr for %s\n", path));
795 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
796 AFPINFO_EA_NETATALK, ad->ad_data,
797 AD_DATASZ_XATTR);
798 if (ealen == -1) {
799 switch (errno) {
800 case ENOATTR:
801 case ENOENT:
802 if (errno == ENOATTR) {
803 errno = ENOENT;
805 rc = -1;
806 goto exit;
807 default:
808 DEBUG(2, ("error reading meta xattr: %s\n",
809 strerror(errno)));
810 rc = -1;
811 goto exit;
814 if (ealen != AD_DATASZ_XATTR) {
815 DEBUG(2, ("bad size %zd\n", ealen));
816 errno = EINVAL;
817 rc = -1;
818 goto exit;
821 /* Now parse entries */
822 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
823 if (!ok) {
824 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
825 errno = EINVAL;
826 rc = -1;
827 goto exit;
830 if (!ad_getentryoff(ad, ADEID_FINDERI)
831 || !ad_getentryoff(ad, ADEID_COMMENT)
832 || !ad_getentryoff(ad, ADEID_FILEDATESI)
833 || !ad_getentryoff(ad, ADEID_AFPFILEI)
834 || !ad_getentryoff(ad, ADEID_PRIVDEV)
835 || !ad_getentryoff(ad, ADEID_PRIVINO)
836 || !ad_getentryoff(ad, ADEID_PRIVSYN)
837 || !ad_getentryoff(ad, ADEID_PRIVID)) {
838 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
839 errno = EINVAL;
840 rc = -1;
841 goto exit;
844 exit:
845 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
847 if (rc != 0) {
848 ealen = -1;
849 if (errno == EINVAL) {
850 become_root();
851 removexattr(path, AFPINFO_EA_NETATALK);
852 unbecome_root();
853 errno = ENOENT;
856 return ealen;
859 static int ad_open_meta(const char *path, int flags, mode_t mode)
861 return open(path, flags, mode);
864 static int ad_open_rsrc_xattr(const char *path, int flags, mode_t mode)
866 #ifdef HAVE_ATTROPEN
867 /* FIXME: direct Solaris xattr syscall */
868 return attropen(path, AFPRESOURCE_EA_NETATALK, flags, mode);
869 #else
870 errno = ENOSYS;
871 return -1;
872 #endif
875 static int ad_open_rsrc_adouble(const char *path, int flags, mode_t mode)
877 char *adp = NULL;
878 int ret;
879 int fd;
881 ret = adouble_path(talloc_tos(), path, &adp);
882 if (ret != 0) {
883 return -1;
886 fd = open(adp, flags, mode);
887 TALLOC_FREE(adp);
889 return fd;
892 static int ad_open_rsrc(vfs_handle_struct *handle,
893 const char *path,
894 int flags,
895 mode_t mode)
897 struct fruit_config_data *config = NULL;
898 int fd;
900 SMB_VFS_HANDLE_GET_DATA(handle, config,
901 struct fruit_config_data, return -1);
903 if (config->rsrc == FRUIT_RSRC_XATTR) {
904 fd = ad_open_rsrc_xattr(path, flags, mode);
905 } else {
906 fd = ad_open_rsrc_adouble(path, flags, mode);
909 return fd;
912 static int ad_open(vfs_handle_struct *handle,
913 struct adouble *ad,
914 const char *path,
915 adouble_type_t t,
916 int flags,
917 mode_t mode)
919 int fd;
921 DBG_DEBUG("Path [%s] type [%s]\n",
922 path, t == ADOUBLE_META ? "meta" : "rsrc");
924 if (t == ADOUBLE_META) {
925 fd = ad_open_meta(path, flags, mode);
926 } else {
927 fd = ad_open_rsrc(handle, path, flags, mode);
930 if (fd != -1) {
931 ad->ad_opened = true;
932 ad->ad_fd = fd;
935 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
936 path, t == ADOUBLE_META ? "meta" : "rsrc", fd);
938 return fd;
941 static ssize_t ad_read_rsrc_xattr(struct adouble *ad,
942 const char *path)
944 int ret;
945 SMB_STRUCT_STAT st;
947 /* FIXME: direct sys_fstat(), don't have an fsp */
948 ret = sys_fstat(ad->ad_fd, &st,
949 lp_fake_directory_create_times(
950 SNUM(ad->ad_handle->conn)));
951 if (ret != 0) {
952 return -1;
955 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
956 return st.st_ex_size;
959 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
960 const char *path)
962 struct adouble *meta_ad = NULL;
963 SMB_STRUCT_STAT sbuf;
964 char *p_ad = NULL;
965 char *p_meta_ad = NULL;
966 ssize_t len;
967 int ret;
968 bool ok;
970 len = sys_pread(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
971 if (len != AD_DATASZ_DOT_UND) {
972 DBG_NOTICE("%s %s: bad size: %zd\n",
973 path, strerror(errno), len);
974 return -1;
977 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
978 SNUM(ad->ad_handle->conn)));
979 if (ret != 0) {
980 return -1;
983 /* Now parse entries */
984 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
985 if (!ok) {
986 DBG_ERR("invalid AppleDouble resource %s\n", path);
987 errno = EINVAL;
988 return -1;
991 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
992 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
993 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
994 DBG_ERR("invalid AppleDouble resource %s\n", path);
995 errno = EINVAL;
996 return -1;
999 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1000 return len;
1004 * Try to fixup AppleDouble files created by OS X with xattrs
1005 * appended to the ADEID_FINDERI entry. We simply remove the
1006 * xattrs blob, this means any fancy xattr that was stored
1007 * there is lost.
1010 ret = ad_convert(ad, ad->ad_fd);
1011 if (ret != 0) {
1012 DBG_WARNING("Failed to convert [%s]\n", path);
1013 return len;
1016 ok = ad_pack(ad);
1017 if (!ok) {
1018 DBG_WARNING("ad_pack [%s] failed\n", path);
1019 return -1;
1022 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1023 if (len != AD_DATASZ_DOT_UND) {
1024 DBG_ERR("%s: bad size: %zd\n", path, len);
1025 return -1;
1028 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1029 if (meta_ad == NULL) {
1030 return -1;
1033 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1034 if (p_ad == NULL) {
1035 TALLOC_FREE(meta_ad);
1036 return -1;
1038 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1039 if (p_meta_ad == NULL) {
1040 TALLOC_FREE(meta_ad);
1041 return -1;
1044 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1046 ret = ad_set(meta_ad, path);
1047 TALLOC_FREE(meta_ad);
1048 if (ret != 0) {
1049 return -1;
1052 return len;
1056 * Read and parse resource fork, either ._ AppleDouble file or xattr
1058 static ssize_t ad_read_rsrc(struct adouble *ad,
1059 const char *path)
1061 struct fruit_config_data *config = NULL;
1062 ssize_t len;
1064 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1065 struct fruit_config_data, return -1);
1067 if (config->rsrc == FRUIT_RSRC_XATTR) {
1068 len = ad_read_rsrc_xattr(ad, path);
1069 } else {
1070 len = ad_read_rsrc_adouble(ad, path);
1073 return len;
1077 * Read and unpack an AppleDouble metadata xattr or resource
1079 static ssize_t ad_read(struct adouble *ad, const char *path)
1081 switch (ad->ad_type) {
1082 case ADOUBLE_META:
1083 return ad_read_meta(ad, path);
1084 case ADOUBLE_RSRC:
1085 return ad_read_rsrc(ad, path);
1086 default:
1087 return -1;
1091 static int adouble_destructor(struct adouble *ad)
1093 if ((ad->ad_fd != -1) && ad->ad_opened) {
1094 close(ad->ad_fd);
1095 ad->ad_fd = -1;
1097 return 0;
1101 * Allocate a struct adouble without initialiing it
1103 * The struct is either hang of the fsp extension context or if fsp is
1104 * NULL from ctx.
1106 * @param[in] ctx talloc context
1107 * @param[in] handle vfs handle
1108 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1110 * @return adouble handle
1112 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1113 adouble_type_t type)
1115 int rc = 0;
1116 size_t adsize = 0;
1117 struct adouble *ad;
1118 struct fruit_config_data *config;
1120 SMB_VFS_HANDLE_GET_DATA(handle, config,
1121 struct fruit_config_data, return NULL);
1123 switch (type) {
1124 case ADOUBLE_META:
1125 adsize = AD_DATASZ_XATTR;
1126 break;
1127 case ADOUBLE_RSRC:
1128 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1129 adsize = AD_DATASZ_DOT_UND;
1131 break;
1132 default:
1133 return NULL;
1136 ad = talloc_zero(ctx, struct adouble);
1137 if (ad == NULL) {
1138 rc = -1;
1139 goto exit;
1142 if (adsize) {
1143 ad->ad_data = talloc_zero_array(ad, char, adsize);
1144 if (ad->ad_data == NULL) {
1145 rc = -1;
1146 goto exit;
1150 ad->ad_handle = handle;
1151 ad->ad_type = type;
1152 ad->ad_magic = AD_MAGIC;
1153 ad->ad_version = AD_VERSION;
1154 ad->ad_fd = -1;
1156 talloc_set_destructor(ad, adouble_destructor);
1158 exit:
1159 if (rc != 0) {
1160 TALLOC_FREE(ad);
1162 return ad;
1166 * Allocate and initialize a new struct adouble
1168 * @param[in] ctx talloc context
1169 * @param[in] handle vfs handle
1170 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1172 * @return adouble handle, initialized
1174 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1175 adouble_type_t type)
1177 int rc = 0;
1178 const struct ad_entry_order *eid;
1179 struct adouble *ad = NULL;
1180 struct fruit_config_data *config;
1181 time_t t = time(NULL);
1183 SMB_VFS_HANDLE_GET_DATA(handle, config,
1184 struct fruit_config_data, return NULL);
1186 switch (type) {
1187 case ADOUBLE_META:
1188 eid = entry_order_meta_xattr;
1189 break;
1190 case ADOUBLE_RSRC:
1191 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1192 eid = entry_order_dot_und;
1193 } else {
1194 eid = entry_order_rsrc_xattr;
1196 break;
1197 default:
1198 return NULL;
1201 ad = ad_alloc(ctx, handle, type);
1202 if (ad == NULL) {
1203 return NULL;
1206 while (eid->id) {
1207 ad->ad_eid[eid->id].ade_off = eid->offset;
1208 ad->ad_eid[eid->id].ade_len = eid->len;
1209 eid++;
1212 /* put something sane in the date fields */
1213 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1214 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1215 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1216 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1218 if (rc != 0) {
1219 TALLOC_FREE(ad);
1221 return ad;
1225 * Return AppleDouble data for a file
1227 * @param[in] ctx talloc context
1228 * @param[in] handle vfs handle
1229 * @param[in] path pathname to file or directory
1230 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1232 * @return talloced struct adouble or NULL on error
1234 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1235 const char *path, adouble_type_t type)
1237 int rc = 0;
1238 ssize_t len;
1239 struct adouble *ad = NULL;
1240 int fd;
1241 int mode;
1243 DEBUG(10, ("ad_get(%s) called for %s\n",
1244 type == ADOUBLE_META ? "meta" : "rsrc", path));
1246 ad = ad_alloc(ctx, handle, type);
1247 if (ad == NULL) {
1248 rc = -1;
1249 goto exit;
1253 * Here's the deal: for ADOUBLE_META we can do without an fd
1254 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1255 * however we need a full-fledged fd for file IO on the ._
1256 * file.
1258 if (type == ADOUBLE_RSRC) {
1259 /* Try rw first so we can use the fd in ad_convert() */
1260 mode = O_RDWR;
1262 fd = ad_open(handle, ad, path, ADOUBLE_RSRC, mode, 0);
1263 if (fd == -1 && ((errno == EROFS) || (errno == EACCES))) {
1264 mode = O_RDONLY;
1265 fd = ad_open(handle, ad, path, ADOUBLE_RSRC, mode, 0);
1268 if (fd == -1) {
1269 DBG_DEBUG("ad_open [%s] error [%s]\n",
1270 path, strerror(errno));
1271 rc = -1;
1272 goto exit;
1276 len = ad_read(ad, path);
1277 if (len == -1) {
1278 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1279 rc = -1;
1280 goto exit;
1283 exit:
1284 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1285 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1287 if (rc != 0) {
1288 TALLOC_FREE(ad);
1290 return ad;
1294 * Return AppleDouble data for a file
1296 * @param[in] ctx talloc context
1297 * @param[in] handle vfs handle
1298 * @param[in] fsp fsp to use for IO
1299 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1301 * @return talloced struct adouble or NULL on error
1303 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1304 files_struct *fsp, adouble_type_t type)
1306 int rc = 0;
1307 ssize_t len;
1308 struct adouble *ad = NULL;
1309 char *path = fsp->base_fsp->fsp_name->base_name;
1311 DBG_DEBUG("ad_get(%s) path [%s]\n",
1312 type == ADOUBLE_META ? "meta" : "rsrc",
1313 fsp_str_dbg(fsp));
1315 ad = ad_alloc(ctx, handle, type);
1316 if (ad == NULL) {
1317 rc = -1;
1318 goto exit;
1321 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1322 ad->ad_fd = fsp->fh->fd;
1323 } else {
1325 * Here's the deal: for ADOUBLE_META we can do without an fd
1326 * as we can issue path based xattr calls. For ADOUBLE_RSRC
1327 * however we need a full-fledged fd for file IO on the ._
1328 * file.
1330 int fd;
1331 int mode;
1333 if (type == ADOUBLE_RSRC) {
1334 /* Try rw first so we can use the fd in ad_convert() */
1335 mode = O_RDWR;
1337 fd = ad_open(handle, ad, path, ADOUBLE_RSRC, mode, 0);
1338 if (fd == -1 &&
1339 ((errno == EROFS) || (errno == EACCES)))
1341 mode = O_RDONLY;
1342 fd = ad_open(handle, ad, path, ADOUBLE_RSRC,
1343 mode, 0);
1346 if (fd == -1) {
1347 DBG_DEBUG("error opening AppleDouble for %s\n", path);
1348 rc = -1;
1349 goto exit;
1354 len = ad_read(ad, path);
1355 if (len == -1) {
1356 DBG_DEBUG("error reading AppleDouble for %s\n", path);
1357 rc = -1;
1358 goto exit;
1361 exit:
1362 DBG_DEBUG("ad_get(%s) path [%s] rc [%d]\n",
1363 type == ADOUBLE_META ? "meta" : "rsrc",
1364 fsp_str_dbg(fsp), rc);
1366 if (rc != 0) {
1367 TALLOC_FREE(ad);
1369 return ad;
1373 * Set AppleDouble metadata on a file or directory
1375 * @param[in] ad adouble handle
1377 * @param[in] path pathname to file or directory
1379 * @return status code, 0 means success
1381 static int ad_set(struct adouble *ad, const char *path)
1383 bool ok;
1384 int ret;
1386 DBG_DEBUG("Path [%s]\n", path);
1388 if (ad->ad_type != ADOUBLE_META) {
1389 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n", path);
1390 return -1;
1393 ok = ad_pack(ad);
1394 if (!ok) {
1395 return -1;
1398 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1399 path,
1400 AFPINFO_EA_NETATALK,
1401 ad->ad_data,
1402 AD_DATASZ_XATTR, 0);
1404 DBG_DEBUG("Path [%s] ret [%d]\n", path, ret);
1406 return ret;
1410 * Set AppleDouble metadata on a file or directory
1412 * @param[in] ad adouble handle
1413 * @param[in] fsp file handle
1415 * @return status code, 0 means success
1417 static int ad_fset(struct adouble *ad, files_struct *fsp)
1419 int rc = -1;
1420 ssize_t len;
1421 bool ok;
1423 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1425 if ((fsp == NULL)
1426 || (fsp->fh == NULL)
1427 || (fsp->fh->fd == -1))
1429 smb_panic("bad fsp");
1432 ok = ad_pack(ad);
1433 if (!ok) {
1434 return -1;
1437 switch (ad->ad_type) {
1438 case ADOUBLE_META:
1439 rc = SMB_VFS_NEXT_FSETXATTR(ad->ad_handle,
1440 fsp,
1441 AFPINFO_EA_NETATALK,
1442 ad->ad_data,
1443 AD_DATASZ_XATTR, 0);
1444 break;
1446 case ADOUBLE_RSRC:
1447 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1448 fsp,
1449 ad->ad_data,
1450 talloc_get_size(ad->ad_data),
1452 if (len != (ssize_t)talloc_get_size(ad->ad_data)) {
1453 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1454 return -1;
1456 rc = 0;
1457 break;
1459 default:
1460 return -1;
1463 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1465 return rc;
1468 /*****************************************************************************
1469 * Helper functions
1470 *****************************************************************************/
1472 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1474 if (strncasecmp_m(smb_fname->stream_name,
1475 AFPINFO_STREAM_NAME,
1476 strlen(AFPINFO_STREAM_NAME)) == 0) {
1477 return true;
1479 return false;
1482 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1484 if (strncasecmp_m(smb_fname->stream_name,
1485 AFPRESOURCE_STREAM_NAME,
1486 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1487 return true;
1489 return false;
1493 * Test whether stream is an Apple stream, not used atm
1495 #if 0
1496 static bool is_apple_stream(const struct smb_filename *smb_fname)
1498 if (is_afpinfo_stream(smb_fname)) {
1499 return true;
1501 if (is_afpresource_stream(smb_fname)) {
1502 return true;
1504 return false;
1506 #endif
1509 * Initialize config struct from our smb.conf config parameters
1511 static int init_fruit_config(vfs_handle_struct *handle)
1513 struct fruit_config_data *config;
1514 int enumval;
1516 config = talloc_zero(handle->conn, struct fruit_config_data);
1517 if (!config) {
1518 DEBUG(1, ("talloc_zero() failed\n"));
1519 errno = ENOMEM;
1520 return -1;
1524 * Versions up to Samba 4.5.x had a spelling bug in the
1525 * fruit:resource option calling lp_parm_enum with
1526 * "res*s*ource" (ie two s).
1528 * In Samba 4.6 we accept both the wrong and the correct
1529 * spelling, in Samba 4.7 the bad spelling will be removed.
1531 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1532 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1533 if (enumval == -1) {
1534 DEBUG(1, ("value for %s: resource type unknown\n",
1535 FRUIT_PARAM_TYPE_NAME));
1536 return -1;
1538 config->rsrc = (enum fruit_rsrc)enumval;
1540 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1541 "resource", fruit_rsrc, enumval);
1542 if (enumval == -1) {
1543 DEBUG(1, ("value for %s: resource type unknown\n",
1544 FRUIT_PARAM_TYPE_NAME));
1545 return -1;
1547 config->rsrc = (enum fruit_rsrc)enumval;
1549 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1550 "metadata", fruit_meta, FRUIT_META_NETATALK);
1551 if (enumval == -1) {
1552 DEBUG(1, ("value for %s: metadata type unknown\n",
1553 FRUIT_PARAM_TYPE_NAME));
1554 return -1;
1556 config->meta = (enum fruit_meta)enumval;
1558 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1559 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1560 if (enumval == -1) {
1561 DEBUG(1, ("value for %s: locking type unknown\n",
1562 FRUIT_PARAM_TYPE_NAME));
1563 return -1;
1565 config->locking = (enum fruit_locking)enumval;
1567 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1568 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1569 if (enumval == -1) {
1570 DEBUG(1, ("value for %s: encoding type unknown\n",
1571 FRUIT_PARAM_TYPE_NAME));
1572 return -1;
1574 config->encoding = (enum fruit_encoding)enumval;
1576 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1577 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1578 FRUIT_PARAM_TYPE_NAME,
1579 "veto_appledouble",
1580 true);
1583 config->use_aapl = lp_parm_bool(
1584 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1586 config->unix_info_enabled = lp_parm_bool(
1587 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1589 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1590 "copyfile", false);
1592 config->posix_rename = lp_parm_bool(
1593 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1595 config->aapl_zero_file_id =
1596 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1598 config->readdir_attr_rsize = lp_parm_bool(
1599 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1601 config->readdir_attr_finder_info = lp_parm_bool(
1602 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1604 config->readdir_attr_max_access = lp_parm_bool(
1605 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1607 SMB_VFS_HANDLE_SET_DATA(handle, config,
1608 NULL, struct fruit_config_data,
1609 return -1);
1611 return 0;
1615 * Prepend "._" to a basename
1617 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1619 char *parent;
1620 const char *base;
1622 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1623 return -1;
1626 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1627 if (*path_out == NULL) {
1628 return -1;
1631 return 0;
1635 * Allocate and initialize an AfpInfo struct
1637 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1639 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1640 if (ai == NULL) {
1641 return NULL;
1643 ai->afpi_Signature = AFP_Signature;
1644 ai->afpi_Version = AFP_Version;
1645 ai->afpi_BackupTime = AD_DATE_START;
1646 return ai;
1650 * Pack an AfpInfo struct into a buffer
1652 * Buffer size must be at least AFP_INFO_SIZE
1653 * Returns size of packed buffer
1655 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1657 memset(buf, 0, AFP_INFO_SIZE);
1659 RSIVAL(buf, 0, ai->afpi_Signature);
1660 RSIVAL(buf, 4, ai->afpi_Version);
1661 RSIVAL(buf, 12, ai->afpi_BackupTime);
1662 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1664 return AFP_INFO_SIZE;
1668 * Unpack a buffer into a AfpInfo structure
1670 * Buffer size must be at least AFP_INFO_SIZE
1671 * Returns allocated AfpInfo struct
1673 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1675 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1676 if (ai == NULL) {
1677 return NULL;
1680 ai->afpi_Signature = RIVAL(data, 0);
1681 ai->afpi_Version = RIVAL(data, 4);
1682 ai->afpi_BackupTime = RIVAL(data, 12);
1683 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1684 sizeof(ai->afpi_FinderInfo));
1686 if (ai->afpi_Signature != AFP_Signature
1687 || ai->afpi_Version != AFP_Version) {
1688 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1689 TALLOC_FREE(ai);
1692 return ai;
1696 * Fake an inode number from the md5 hash of the (xattr) name
1698 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1700 MD5_CTX ctx;
1701 unsigned char hash[16];
1702 SMB_INO_T result;
1703 char *upper_sname;
1705 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1706 SMB_ASSERT(upper_sname != NULL);
1708 MD5Init(&ctx);
1709 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1710 sizeof(sbuf->st_ex_dev));
1711 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1712 sizeof(sbuf->st_ex_ino));
1713 MD5Update(&ctx, (unsigned char *)upper_sname,
1714 talloc_get_size(upper_sname)-1);
1715 MD5Final(hash, &ctx);
1717 TALLOC_FREE(upper_sname);
1719 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1720 memcpy(&result, hash, sizeof(result));
1722 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1723 sname, (unsigned long long)result));
1725 return result;
1728 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1729 struct stream_struct **streams,
1730 const char *name, off_t size,
1731 off_t alloc_size)
1733 struct stream_struct *tmp;
1735 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1736 (*num_streams)+1);
1737 if (tmp == NULL) {
1738 return false;
1741 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1742 if (tmp[*num_streams].name == NULL) {
1743 return false;
1746 tmp[*num_streams].size = size;
1747 tmp[*num_streams].alloc_size = alloc_size;
1749 *streams = tmp;
1750 *num_streams += 1;
1751 return true;
1754 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1755 struct stream_struct **streams)
1757 struct stream_struct *tmp = *streams;
1758 unsigned int i;
1760 if (*num_streams == 0) {
1761 return true;
1764 for (i = 0; i < *num_streams; i++) {
1765 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1766 break;
1770 if (i == *num_streams) {
1771 return true;
1774 if (tmp[i].size > 0) {
1775 return true;
1778 TALLOC_FREE(tmp[i].name);
1779 if (*num_streams - 1 > i) {
1780 memmove(&tmp[i], &tmp[i+1],
1781 (*num_streams - i - 1) * sizeof(struct stream_struct));
1784 *num_streams -= 1;
1785 return true;
1788 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1789 struct stream_struct **streams,
1790 const char *name)
1792 struct stream_struct *tmp = *streams;
1793 unsigned int i;
1795 if (*num_streams == 0) {
1796 return true;
1799 for (i = 0; i < *num_streams; i++) {
1800 if (strequal_m(tmp[i].name, name)) {
1801 break;
1805 if (i == *num_streams) {
1806 return true;
1809 TALLOC_FREE(tmp[i].name);
1810 if (*num_streams - 1 > i) {
1811 memmove(&tmp[i], &tmp[i+1],
1812 (*num_streams - i - 1) * sizeof(struct stream_struct));
1815 *num_streams -= 1;
1816 return true;
1819 static bool ad_empty_finderinfo(const struct adouble *ad)
1821 int cmp;
1822 char emptybuf[ADEDLEN_FINDERI] = {0};
1823 char *fi = NULL;
1825 fi = ad_get_entry(ad, ADEID_FINDERI);
1826 if (fi == NULL) {
1827 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1828 return false;
1831 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1832 return (cmp == 0);
1835 static bool ai_empty_finderinfo(const AfpInfo *ai)
1837 int cmp;
1838 char emptybuf[ADEDLEN_FINDERI] = {0};
1840 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1841 return (cmp == 0);
1845 * Update btime with btime from Netatalk
1847 static void update_btime(vfs_handle_struct *handle,
1848 struct smb_filename *smb_fname)
1850 uint32_t t;
1851 struct timespec creation_time = {0};
1852 struct adouble *ad;
1853 struct fruit_config_data *config = NULL;
1855 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1856 return);
1858 switch (config->meta) {
1859 case FRUIT_META_STREAM:
1860 return;
1861 case FRUIT_META_NETATALK:
1862 /* Handled below */
1863 break;
1864 default:
1865 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1866 return;
1869 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1870 if (ad == NULL) {
1871 return;
1873 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1874 TALLOC_FREE(ad);
1875 return;
1877 TALLOC_FREE(ad);
1879 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1880 update_stat_ex_create_time(&smb_fname->st, creation_time);
1882 return;
1886 * Map an access mask to a Netatalk single byte byte range lock
1888 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1889 uint32_t access_mask)
1891 off_t offset;
1893 switch (access_mask) {
1894 case FILE_READ_DATA:
1895 offset = AD_FILELOCK_OPEN_RD;
1896 break;
1898 case FILE_WRITE_DATA:
1899 case FILE_APPEND_DATA:
1900 offset = AD_FILELOCK_OPEN_WR;
1901 break;
1903 default:
1904 offset = AD_FILELOCK_OPEN_NONE;
1905 break;
1908 if (fork_type == APPLE_FORK_RSRC) {
1909 if (offset == AD_FILELOCK_OPEN_NONE) {
1910 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1911 } else {
1912 offset += 2;
1916 return offset;
1920 * Map a deny mode to a Netatalk brl
1922 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1923 uint32_t deny_mode)
1925 off_t offset;
1927 switch (deny_mode) {
1928 case DENY_READ:
1929 offset = AD_FILELOCK_DENY_RD;
1930 break;
1932 case DENY_WRITE:
1933 offset = AD_FILELOCK_DENY_WR;
1934 break;
1936 default:
1937 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1940 if (fork_type == APPLE_FORK_RSRC) {
1941 offset += 2;
1944 return offset;
1948 * Call fcntl() with an exclusive F_GETLK request in order to
1949 * determine if there's an exisiting shared lock
1951 * @return true if the requested lock was found or any error occurred
1952 * false if the lock was not found
1954 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1956 bool result;
1957 off_t offset = in_offset;
1958 off_t len = 1;
1959 int type = F_WRLCK;
1960 pid_t pid;
1962 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1963 if (result == false) {
1964 return true;
1967 if (type != F_UNLCK) {
1968 return true;
1971 return false;
1974 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1975 files_struct *fsp,
1976 uint32_t access_mask,
1977 uint32_t deny_mode)
1979 NTSTATUS status = NT_STATUS_OK;
1980 struct byte_range_lock *br_lck = NULL;
1981 bool open_for_reading, open_for_writing, deny_read, deny_write;
1982 off_t off;
1983 bool have_read = false;
1984 int flags;
1986 /* FIXME: hardcoded data fork, add resource fork */
1987 enum apple_fork fork_type = APPLE_FORK_DATA;
1989 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1990 fsp_str_dbg(fsp),
1991 access_mask & FILE_READ_DATA ? "READ" :"-",
1992 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1993 deny_mode & DENY_READ ? "DENY_READ" : "-",
1994 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1996 if (fsp->fh->fd == -1) {
1997 return NT_STATUS_OK;
2000 flags = fcntl(fsp->fh->fd, F_GETFL);
2001 if (flags == -1) {
2002 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2003 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2004 return map_nt_error_from_unix(errno);
2007 if (flags & (O_RDONLY|O_RDWR)) {
2009 * Applying fcntl read locks requires an fd opened for
2010 * reading. This means we won't be applying locks for
2011 * files openend write-only, but what can we do...
2013 have_read = true;
2017 * Check read access and deny read mode
2019 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2020 /* Check access */
2021 open_for_reading = test_netatalk_lock(
2022 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2024 deny_read = test_netatalk_lock(
2025 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2027 DEBUG(10, ("read: %s, deny_write: %s\n",
2028 open_for_reading == true ? "yes" : "no",
2029 deny_read == true ? "yes" : "no"));
2031 if (((access_mask & FILE_READ_DATA) && deny_read)
2032 || ((deny_mode & DENY_READ) && open_for_reading)) {
2033 return NT_STATUS_SHARING_VIOLATION;
2036 /* Set locks */
2037 if ((access_mask & FILE_READ_DATA) && have_read) {
2038 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2039 br_lck = do_lock(
2040 handle->conn->sconn->msg_ctx, fsp,
2041 fsp->op->global->open_persistent_id, 1, off,
2042 READ_LOCK, POSIX_LOCK, false,
2043 &status, NULL);
2045 if (!NT_STATUS_IS_OK(status)) {
2046 return status;
2048 TALLOC_FREE(br_lck);
2051 if ((deny_mode & DENY_READ) && have_read) {
2052 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2053 br_lck = do_lock(
2054 handle->conn->sconn->msg_ctx, fsp,
2055 fsp->op->global->open_persistent_id, 1, off,
2056 READ_LOCK, POSIX_LOCK, false,
2057 &status, NULL);
2059 if (!NT_STATUS_IS_OK(status)) {
2060 return status;
2062 TALLOC_FREE(br_lck);
2067 * Check write access and deny write mode
2069 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2070 /* Check access */
2071 open_for_writing = test_netatalk_lock(
2072 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2074 deny_write = test_netatalk_lock(
2075 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2077 DEBUG(10, ("write: %s, deny_write: %s\n",
2078 open_for_writing == true ? "yes" : "no",
2079 deny_write == true ? "yes" : "no"));
2081 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2082 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2083 return NT_STATUS_SHARING_VIOLATION;
2086 /* Set locks */
2087 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2088 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2089 br_lck = do_lock(
2090 handle->conn->sconn->msg_ctx, fsp,
2091 fsp->op->global->open_persistent_id, 1, off,
2092 READ_LOCK, POSIX_LOCK, false,
2093 &status, NULL);
2095 if (!NT_STATUS_IS_OK(status)) {
2096 return status;
2098 TALLOC_FREE(br_lck);
2101 if ((deny_mode & DENY_WRITE) && have_read) {
2102 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2103 br_lck = do_lock(
2104 handle->conn->sconn->msg_ctx, fsp,
2105 fsp->op->global->open_persistent_id, 1, off,
2106 READ_LOCK, POSIX_LOCK, false,
2107 &status, NULL);
2109 if (!NT_STATUS_IS_OK(status)) {
2110 return status;
2112 TALLOC_FREE(br_lck);
2116 TALLOC_FREE(br_lck);
2118 return status;
2121 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2122 struct smb_request *req,
2123 const struct smb2_create_blobs *in_context_blobs,
2124 struct smb2_create_blobs *out_context_blobs)
2126 struct fruit_config_data *config;
2127 NTSTATUS status;
2128 struct smb2_create_blob *aapl = NULL;
2129 uint32_t cmd;
2130 bool ok;
2131 uint8_t p[16];
2132 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2133 uint64_t req_bitmap, client_caps;
2134 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2135 smb_ucs2_t *model;
2136 size_t modellen;
2138 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2139 return NT_STATUS_UNSUCCESSFUL);
2141 if (!config->use_aapl
2142 || in_context_blobs == NULL
2143 || out_context_blobs == NULL) {
2144 return NT_STATUS_OK;
2147 aapl = smb2_create_blob_find(in_context_blobs,
2148 SMB2_CREATE_TAG_AAPL);
2149 if (aapl == NULL) {
2150 return NT_STATUS_OK;
2153 if (aapl->data.length != 24) {
2154 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2155 (uintmax_t)aapl->data.length));
2156 return NT_STATUS_INVALID_PARAMETER;
2159 cmd = IVAL(aapl->data.data, 0);
2160 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2161 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2162 return NT_STATUS_INVALID_PARAMETER;
2165 req_bitmap = BVAL(aapl->data.data, 8);
2166 client_caps = BVAL(aapl->data.data, 16);
2168 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2169 SIVAL(p, 4, 0);
2170 SBVAL(p, 8, req_bitmap);
2171 ok = data_blob_append(req, &blob, p, 16);
2172 if (!ok) {
2173 return NT_STATUS_UNSUCCESSFUL;
2176 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2177 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2178 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2179 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2180 config->readdir_attr_enabled = true;
2183 if (config->use_copyfile) {
2184 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2185 config->copyfile_enabled = true;
2189 * The client doesn't set the flag, so we can't check
2190 * for it and just set it unconditionally
2192 if (config->unix_info_enabled) {
2193 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2196 SBVAL(p, 0, server_caps);
2197 ok = data_blob_append(req, &blob, p, 8);
2198 if (!ok) {
2199 return NT_STATUS_UNSUCCESSFUL;
2203 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2204 SBVAL(p, 0,
2205 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
2206 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
2207 ok = data_blob_append(req, &blob, p, 8);
2208 if (!ok) {
2209 return NT_STATUS_UNSUCCESSFUL;
2213 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2214 ok = convert_string_talloc(req,
2215 CH_UNIX, CH_UTF16LE,
2216 "Samba", strlen("Samba"),
2217 &model, &modellen);
2218 if (!ok) {
2219 return NT_STATUS_UNSUCCESSFUL;
2222 SIVAL(p, 0, 0);
2223 SIVAL(p + 4, 0, modellen);
2224 ok = data_blob_append(req, &blob, p, 8);
2225 if (!ok) {
2226 talloc_free(model);
2227 return NT_STATUS_UNSUCCESSFUL;
2230 ok = data_blob_append(req, &blob, model, modellen);
2231 talloc_free(model);
2232 if (!ok) {
2233 return NT_STATUS_UNSUCCESSFUL;
2237 status = smb2_create_blob_add(out_context_blobs,
2238 out_context_blobs,
2239 SMB2_CREATE_TAG_AAPL,
2240 blob);
2241 if (NT_STATUS_IS_OK(status)) {
2242 global_fruit_config.nego_aapl = true;
2243 if (config->aapl_zero_file_id) {
2244 aapl_force_zero_file_id(handle->conn->sconn);
2248 return status;
2251 static bool readdir_attr_meta_finderi_stream(
2252 struct vfs_handle_struct *handle,
2253 const struct smb_filename *smb_fname,
2254 AfpInfo *ai)
2256 struct smb_filename *stream_name = NULL;
2257 files_struct *fsp = NULL;
2258 ssize_t nread;
2259 NTSTATUS status;
2260 int ret;
2261 bool ok;
2262 uint8_t buf[AFP_INFO_SIZE];
2264 stream_name = synthetic_smb_fname(talloc_tos(),
2265 smb_fname->base_name,
2266 AFPINFO_STREAM_NAME,
2267 NULL, smb_fname->flags);
2268 if (stream_name == NULL) {
2269 return false;
2272 ret = SMB_VFS_STAT(handle->conn, stream_name);
2273 if (ret != 0) {
2274 return false;
2277 status = SMB_VFS_CREATE_FILE(
2278 handle->conn, /* conn */
2279 NULL, /* req */
2280 0, /* root_dir_fid */
2281 stream_name, /* fname */
2282 FILE_READ_DATA, /* access_mask */
2283 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2284 FILE_SHARE_DELETE),
2285 FILE_OPEN, /* create_disposition*/
2286 0, /* create_options */
2287 0, /* file_attributes */
2288 INTERNAL_OPEN_ONLY, /* oplock_request */
2289 NULL, /* lease */
2290 0, /* allocation_size */
2291 0, /* private_flags */
2292 NULL, /* sd */
2293 NULL, /* ea_list */
2294 &fsp, /* result */
2295 NULL, /* pinfo */
2296 NULL, NULL); /* create context */
2298 TALLOC_FREE(stream_name);
2300 if (!NT_STATUS_IS_OK(status)) {
2301 return false;
2304 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2305 if (nread != AFP_INFO_SIZE) {
2306 DBG_ERR("short read [%s] [%zd/%d]\n",
2307 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2308 ok = false;
2309 goto fail;
2312 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2313 AFP_FinderSize);
2315 ok = true;
2317 fail:
2318 if (fsp != NULL) {
2319 close_file(NULL, fsp, NORMAL_CLOSE);
2322 return ok;
2325 static bool readdir_attr_meta_finderi_netatalk(
2326 struct vfs_handle_struct *handle,
2327 const struct smb_filename *smb_fname,
2328 AfpInfo *ai)
2330 struct adouble *ad = NULL;
2331 char *p = NULL;
2333 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
2334 if (ad == NULL) {
2335 return false;
2338 p = ad_get_entry(ad, ADEID_FINDERI);
2339 if (p == NULL) {
2340 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2341 TALLOC_FREE(ad);
2342 return false;
2345 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2346 TALLOC_FREE(ad);
2347 return true;
2350 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2351 const struct smb_filename *smb_fname,
2352 struct readdir_attr_data *attr_data)
2354 struct fruit_config_data *config = NULL;
2355 uint32_t date_added;
2356 AfpInfo ai = {0};
2357 bool ok;
2359 SMB_VFS_HANDLE_GET_DATA(handle, config,
2360 struct fruit_config_data,
2361 return false);
2363 switch (config->meta) {
2364 case FRUIT_META_NETATALK:
2365 ok = readdir_attr_meta_finderi_netatalk(
2366 handle, smb_fname, &ai);
2367 break;
2369 case FRUIT_META_STREAM:
2370 ok = readdir_attr_meta_finderi_stream(
2371 handle, smb_fname, &ai);
2372 break;
2374 default:
2375 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2376 return false;
2379 if (!ok) {
2380 /* Don't bother with errors, it's likely ENOENT */
2381 return true;
2384 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2385 /* finder_type */
2386 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2387 &ai.afpi_FinderInfo[0], 4);
2389 /* finder_creator */
2390 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2391 &ai.afpi_FinderInfo[4], 4);
2394 /* finder_flags */
2395 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2396 &ai.afpi_FinderInfo[8], 2);
2398 /* finder_ext_flags */
2399 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2400 &ai.afpi_FinderInfo[24], 2);
2402 /* creation date */
2403 date_added = convert_time_t_to_uint32_t(
2404 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2406 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2408 return true;
2411 static uint64_t readdir_attr_rfork_size_adouble(
2412 struct vfs_handle_struct *handle,
2413 const struct smb_filename *smb_fname)
2415 struct adouble *ad = NULL;
2416 uint64_t rfork_size;
2418 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
2419 ADOUBLE_RSRC);
2420 if (ad == NULL) {
2421 return 0;
2424 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2425 TALLOC_FREE(ad);
2427 return rfork_size;
2430 static uint64_t readdir_attr_rfork_size_stream(
2431 struct vfs_handle_struct *handle,
2432 const struct smb_filename *smb_fname)
2434 struct smb_filename *stream_name = NULL;
2435 int ret;
2436 uint64_t rfork_size;
2438 stream_name = synthetic_smb_fname(talloc_tos(),
2439 smb_fname->base_name,
2440 AFPRESOURCE_STREAM_NAME,
2441 NULL, 0);
2442 if (stream_name == NULL) {
2443 return 0;
2446 ret = SMB_VFS_STAT(handle->conn, stream_name);
2447 if (ret != 0) {
2448 TALLOC_FREE(stream_name);
2449 return 0;
2452 rfork_size = stream_name->st.st_ex_size;
2453 TALLOC_FREE(stream_name);
2455 return rfork_size;
2458 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2459 const struct smb_filename *smb_fname)
2461 struct fruit_config_data *config = NULL;
2462 uint64_t rfork_size;
2464 SMB_VFS_HANDLE_GET_DATA(handle, config,
2465 struct fruit_config_data,
2466 return 0);
2468 switch (config->rsrc) {
2469 case FRUIT_RSRC_ADFILE:
2470 case FRUIT_RSRC_XATTR:
2471 rfork_size = readdir_attr_rfork_size_adouble(handle,
2472 smb_fname);
2473 break;
2475 case FRUIT_META_STREAM:
2476 rfork_size = readdir_attr_rfork_size_stream(handle,
2477 smb_fname);
2478 break;
2480 default:
2481 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2482 rfork_size = 0;
2483 break;
2486 return rfork_size;
2489 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2490 const struct smb_filename *smb_fname,
2491 struct readdir_attr_data *attr_data)
2493 NTSTATUS status = NT_STATUS_OK;
2494 struct fruit_config_data *config = NULL;
2495 bool ok;
2497 SMB_VFS_HANDLE_GET_DATA(handle, config,
2498 struct fruit_config_data,
2499 return NT_STATUS_UNSUCCESSFUL);
2502 /* Ensure we return a default value in the creation_date field */
2503 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2506 * Resource fork length
2509 if (config->readdir_attr_rsize) {
2510 uint64_t rfork_size;
2512 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2513 attr_data->attr_data.aapl.rfork_size = rfork_size;
2517 * FinderInfo
2520 if (config->readdir_attr_finder_info) {
2521 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2522 if (!ok) {
2523 status = NT_STATUS_INTERNAL_ERROR;
2527 return status;
2530 /* Search MS NFS style ACE with UNIX mode */
2531 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2532 files_struct *fsp,
2533 const struct security_descriptor *psd,
2534 mode_t *pmode,
2535 bool *pdo_chmod)
2537 uint32_t i;
2538 struct fruit_config_data *config = NULL;
2540 *pdo_chmod = false;
2542 SMB_VFS_HANDLE_GET_DATA(handle, config,
2543 struct fruit_config_data,
2544 return NT_STATUS_UNSUCCESSFUL);
2546 if (psd->dacl == NULL || !config->unix_info_enabled) {
2547 return NT_STATUS_OK;
2550 for (i = 0; i < psd->dacl->num_aces; i++) {
2551 if (dom_sid_compare_domain(
2552 &global_sid_Unix_NFS_Mode,
2553 &psd->dacl->aces[i].trustee) == 0) {
2554 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2555 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2556 *pdo_chmod = true;
2558 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2559 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2560 break;
2564 return NT_STATUS_OK;
2567 /****************************************************************************
2568 * VFS ops
2569 ****************************************************************************/
2571 static int fruit_connect(vfs_handle_struct *handle,
2572 const char *service,
2573 const char *user)
2575 int rc;
2576 char *list = NULL, *newlist = NULL;
2577 struct fruit_config_data *config;
2579 DEBUG(10, ("fruit_connect\n"));
2581 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2582 if (rc < 0) {
2583 return rc;
2586 rc = init_fruit_config(handle);
2587 if (rc != 0) {
2588 return rc;
2591 SMB_VFS_HANDLE_GET_DATA(handle, config,
2592 struct fruit_config_data, return -1);
2594 if (config->veto_appledouble) {
2595 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2597 if (list) {
2598 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2599 newlist = talloc_asprintf(
2600 list,
2601 "%s/" ADOUBLE_NAME_PREFIX "*/",
2602 list);
2603 lp_do_parameter(SNUM(handle->conn),
2604 "veto files",
2605 newlist);
2607 } else {
2608 lp_do_parameter(SNUM(handle->conn),
2609 "veto files",
2610 "/" ADOUBLE_NAME_PREFIX "*/");
2613 TALLOC_FREE(list);
2616 if (config->encoding == FRUIT_ENC_NATIVE) {
2617 lp_do_parameter(
2618 SNUM(handle->conn),
2619 "catia:mappings",
2620 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2621 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2622 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2623 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2624 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2625 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2626 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2627 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2628 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2629 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2630 "0x0d:0xf00d");
2633 return rc;
2636 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2637 struct smb_filename *smb_fname,
2638 files_struct *fsp,
2639 int flags,
2640 mode_t mode)
2642 AfpInfo *ai = NULL;
2643 char afpinfo_buf[AFP_INFO_SIZE];
2644 ssize_t len, written;
2645 int hostfd = -1;
2646 int rc = -1;
2648 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2649 if (hostfd == -1) {
2650 return -1;
2653 if (!(flags & (O_CREAT | O_TRUNC))) {
2654 return hostfd;
2657 ai = afpinfo_new(talloc_tos());
2658 if (ai == NULL) {
2659 rc = -1;
2660 goto fail;
2663 len = afpinfo_pack(ai, afpinfo_buf);
2664 if (len != AFP_INFO_SIZE) {
2665 rc = -1;
2666 goto fail;
2669 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2670 fsp->fh->fd = hostfd;
2672 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2673 AFP_INFO_SIZE, 0);
2674 fsp->fh->fd = -1;
2675 if (written != AFP_INFO_SIZE) {
2676 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2677 rc = -1;
2678 goto fail;
2681 rc = 0;
2683 fail:
2684 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2686 if (rc != 0) {
2687 int saved_errno = errno;
2688 if (hostfd >= 0) {
2689 fsp->fh->fd = hostfd;
2690 SMB_VFS_NEXT_CLOSE(handle, fsp);
2692 hostfd = -1;
2693 errno = saved_errno;
2695 return hostfd;
2698 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2699 struct smb_filename *smb_fname,
2700 files_struct *fsp,
2701 int flags,
2702 mode_t mode)
2704 int rc = 0;
2705 struct smb_filename *smb_fname_base = NULL;
2706 int baseflags;
2707 int hostfd = -1;
2708 struct adouble *ad = NULL;
2710 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2712 /* Create an smb_filename with stream_name == NULL. */
2713 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2714 smb_fname->base_name,
2715 NULL,
2716 NULL,
2717 smb_fname->flags);
2719 if (smb_fname_base == NULL) {
2720 errno = ENOMEM;
2721 rc = -1;
2722 goto exit;
2726 * We use baseflags to turn off nasty side-effects when opening the
2727 * underlying file.
2729 baseflags = flags;
2730 baseflags &= ~O_TRUNC;
2731 baseflags &= ~O_EXCL;
2732 baseflags &= ~O_CREAT;
2734 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2735 baseflags, mode);
2738 * It is legit to open a stream on a directory, but the base
2739 * fd has to be read-only.
2741 if ((hostfd == -1) && (errno == EISDIR)) {
2742 baseflags &= ~O_ACCMODE;
2743 baseflags |= O_RDONLY;
2744 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2745 baseflags, mode);
2748 TALLOC_FREE(smb_fname_base);
2750 if (hostfd == -1) {
2751 rc = -1;
2752 goto exit;
2755 if (flags & (O_CREAT | O_TRUNC)) {
2757 * The attribute does not exist or needs to be truncated,
2758 * create an AppleDouble EA
2760 ad = ad_init(fsp, handle, ADOUBLE_META);
2761 if (ad == NULL) {
2762 rc = -1;
2763 goto exit;
2766 fsp->fh->fd = hostfd;
2768 rc = ad_fset(ad, fsp);
2769 fsp->fh->fd = -1;
2770 if (rc != 0) {
2771 rc = -1;
2772 goto exit;
2775 TALLOC_FREE(ad);
2778 exit:
2779 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2780 if (rc != 0) {
2781 int saved_errno = errno;
2782 if (hostfd >= 0) {
2784 * BUGBUGBUG -- we would need to call
2785 * fd_close_posix here, but we don't have a
2786 * full fsp yet
2788 fsp->fh->fd = hostfd;
2789 SMB_VFS_NEXT_CLOSE(handle, fsp);
2791 hostfd = -1;
2792 errno = saved_errno;
2794 return hostfd;
2797 static int fruit_open_meta(vfs_handle_struct *handle,
2798 struct smb_filename *smb_fname,
2799 files_struct *fsp, int flags, mode_t mode)
2801 int fd;
2802 struct fruit_config_data *config = NULL;
2803 struct fio *fio = NULL;
2805 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2807 SMB_VFS_HANDLE_GET_DATA(handle, config,
2808 struct fruit_config_data, return -1);
2810 switch (config->meta) {
2811 case FRUIT_META_STREAM:
2812 fd = fruit_open_meta_stream(handle, smb_fname,
2813 fsp, flags, mode);
2814 break;
2816 case FRUIT_META_NETATALK:
2817 fd = fruit_open_meta_netatalk(handle, smb_fname,
2818 fsp, flags, mode);
2819 break;
2821 default:
2822 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2823 return -1;
2826 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2828 if (fd == -1) {
2829 return -1;
2832 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
2833 fio->type = ADOUBLE_META;
2834 fio->config = config;
2836 return fd;
2839 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2840 struct smb_filename *smb_fname,
2841 files_struct *fsp,
2842 int flags,
2843 mode_t mode)
2845 int rc = 0;
2846 struct adouble *ad = NULL;
2847 struct smb_filename *smb_fname_base = NULL;
2848 struct fruit_config_data *config = NULL;
2849 char *adpath = NULL;
2850 int hostfd = -1;
2852 SMB_VFS_HANDLE_GET_DATA(handle, config,
2853 struct fruit_config_data, return -1);
2855 if ((!(flags & O_CREAT)) &&
2856 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
2858 /* sorry, but directories don't habe a resource fork */
2859 rc = -1;
2860 goto exit;
2863 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2864 if (rc != 0) {
2865 goto exit;
2868 /* Create an smb_filename with stream_name == NULL. */
2869 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2870 adpath,
2871 NULL,
2872 NULL,
2873 smb_fname->flags);
2874 if (smb_fname_base == NULL) {
2875 errno = ENOMEM;
2876 rc = -1;
2877 goto exit;
2880 /* Sanitize flags */
2881 if (flags & O_WRONLY) {
2882 /* We always need read access for the metadata header too */
2883 flags &= ~O_WRONLY;
2884 flags |= O_RDWR;
2887 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2888 flags, mode);
2889 if (hostfd == -1) {
2890 rc = -1;
2891 goto exit;
2894 if (flags & (O_CREAT | O_TRUNC)) {
2895 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
2896 if (ad == NULL) {
2897 rc = -1;
2898 goto exit;
2901 fsp->fh->fd = hostfd;
2903 rc = ad_fset(ad, fsp);
2904 fsp->fh->fd = -1;
2905 if (rc != 0) {
2906 rc = -1;
2907 goto exit;
2909 TALLOC_FREE(ad);
2912 exit:
2914 TALLOC_FREE(adpath);
2915 TALLOC_FREE(smb_fname_base);
2917 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2918 if (rc != 0) {
2919 int saved_errno = errno;
2920 if (hostfd >= 0) {
2922 * BUGBUGBUG -- we would need to call
2923 * fd_close_posix here, but we don't have a
2924 * full fsp yet
2926 fsp->fh->fd = hostfd;
2927 SMB_VFS_CLOSE(fsp);
2929 hostfd = -1;
2930 errno = saved_errno;
2932 return hostfd;
2935 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2936 struct smb_filename *smb_fname,
2937 files_struct *fsp,
2938 int flags,
2939 mode_t mode)
2941 #ifdef HAVE_ATTROPEN
2942 int fd = -1;
2944 fd = attropen(smb_fname->base_name,
2945 AFPRESOURCE_EA_NETATALK,
2946 flags,
2947 mode);
2948 if (fd == -1) {
2949 return -1;
2952 return fd;
2954 #else
2955 errno = ENOSYS;
2956 return -1;
2957 #endif
2960 static int fruit_open_rsrc(vfs_handle_struct *handle,
2961 struct smb_filename *smb_fname,
2962 files_struct *fsp, int flags, mode_t mode)
2964 int fd;
2965 struct fruit_config_data *config = NULL;
2966 struct fio *fio = NULL;
2968 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2970 SMB_VFS_HANDLE_GET_DATA(handle, config,
2971 struct fruit_config_data, return -1);
2973 switch (config->rsrc) {
2974 case FRUIT_RSRC_STREAM:
2975 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2976 break;
2978 case FRUIT_RSRC_ADFILE:
2979 fd = fruit_open_rsrc_adouble(handle, smb_fname,
2980 fsp, flags, mode);
2981 break;
2983 case FRUIT_RSRC_XATTR:
2984 fd = fruit_open_rsrc_xattr(handle, smb_fname,
2985 fsp, flags, mode);
2986 break;
2988 default:
2989 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2990 return -1;
2993 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2995 if (fd == -1) {
2996 return -1;
2999 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3000 fio->type = ADOUBLE_RSRC;
3001 fio->config = config;
3003 return fd;
3006 static int fruit_open(vfs_handle_struct *handle,
3007 struct smb_filename *smb_fname,
3008 files_struct *fsp, int flags, mode_t mode)
3010 int fd;
3012 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3014 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3015 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3018 if (is_afpinfo_stream(smb_fname)) {
3019 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3020 } else if (is_afpresource_stream(smb_fname)) {
3021 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3022 } else {
3023 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3026 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3028 return fd;
3031 static int fruit_rename(struct vfs_handle_struct *handle,
3032 const struct smb_filename *smb_fname_src,
3033 const struct smb_filename *smb_fname_dst)
3035 int rc = -1;
3036 char *src_adouble_path = NULL;
3037 char *dst_adouble_path = NULL;
3038 struct fruit_config_data *config = NULL;
3039 struct smb_filename *src_adp_smb_fname = NULL;
3040 struct smb_filename *dst_adp_smb_fname = NULL;
3042 SMB_VFS_HANDLE_GET_DATA(handle, config,
3043 struct fruit_config_data, return -1);
3045 if (!VALID_STAT(smb_fname_src->st)) {
3046 DBG_ERR("Need valid stat for [%s]\n",
3047 smb_fname_str_dbg(smb_fname_src));
3048 return -1;
3051 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3052 if (rc != 0) {
3053 return -1;
3056 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3057 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3059 return 0;
3062 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
3063 &src_adouble_path);
3064 if (rc != 0) {
3065 goto done;
3067 src_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
3068 src_adouble_path,
3069 NULL, NULL,
3070 smb_fname_src->flags);
3071 TALLOC_FREE(src_adouble_path);
3072 if (src_adp_smb_fname == NULL) {
3073 rc = -1;
3074 goto done;
3077 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
3078 &dst_adouble_path);
3079 if (rc != 0) {
3080 goto done;
3082 dst_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
3083 dst_adouble_path,
3084 NULL, NULL,
3085 smb_fname_dst->flags);
3086 TALLOC_FREE(dst_adouble_path);
3087 if (dst_adp_smb_fname == NULL) {
3088 rc = -1;
3089 goto done;
3092 DBG_DEBUG("%s -> %s\n",
3093 smb_fname_str_dbg(src_adp_smb_fname),
3094 smb_fname_str_dbg(dst_adp_smb_fname));
3096 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3097 if (errno == ENOENT) {
3098 rc = 0;
3101 done:
3102 TALLOC_FREE(src_adp_smb_fname);
3103 TALLOC_FREE(dst_adp_smb_fname);
3104 return rc;
3107 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3108 const struct smb_filename *smb_fname)
3110 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3113 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3114 const struct smb_filename *smb_fname)
3116 return SMB_VFS_REMOVEXATTR(handle->conn,
3117 smb_fname->base_name,
3118 AFPINFO_EA_NETATALK);
3121 static int fruit_unlink_meta(vfs_handle_struct *handle,
3122 const struct smb_filename *smb_fname)
3124 struct fruit_config_data *config = NULL;
3125 int rc;
3127 SMB_VFS_HANDLE_GET_DATA(handle, config,
3128 struct fruit_config_data, return -1);
3130 switch (config->meta) {
3131 case FRUIT_META_STREAM:
3132 rc = fruit_unlink_meta_stream(handle, smb_fname);
3133 break;
3135 case FRUIT_META_NETATALK:
3136 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3137 break;
3139 default:
3140 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3141 return -1;
3144 return rc;
3147 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3148 const struct smb_filename *smb_fname,
3149 bool force_unlink)
3151 int ret;
3153 if (!force_unlink) {
3154 struct smb_filename *smb_fname_cp = NULL;
3155 off_t size;
3157 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3158 if (smb_fname_cp == NULL) {
3159 return -1;
3163 * 0 byte resource fork streams are not listed by
3164 * vfs_streaminfo, as a result stream cleanup/deletion of file
3165 * deletion doesn't remove the resourcefork stream.
3168 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3169 if (ret != 0) {
3170 TALLOC_FREE(smb_fname_cp);
3171 DBG_ERR("stat [%s] failed [%s]\n",
3172 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3173 return -1;
3176 size = smb_fname_cp->st.st_ex_size;
3177 TALLOC_FREE(smb_fname_cp);
3179 if (size > 0) {
3180 /* OS X ignores resource fork stream delete requests */
3181 return 0;
3185 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3186 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3187 ret = 0;
3190 return ret;
3193 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3194 const struct smb_filename *smb_fname,
3195 bool force_unlink)
3197 int rc;
3198 char *adp = NULL;
3199 struct adouble *ad = NULL;
3200 struct smb_filename *adp_smb_fname = NULL;
3202 if (!force_unlink) {
3203 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3204 ADOUBLE_RSRC);
3205 if (ad == NULL) {
3206 errno = ENOENT;
3207 return -1;
3212 * 0 byte resource fork streams are not listed by
3213 * vfs_streaminfo, as a result stream cleanup/deletion of file
3214 * deletion doesn't remove the resourcefork stream.
3217 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3218 /* OS X ignores resource fork stream delete requests */
3219 TALLOC_FREE(ad);
3220 return 0;
3223 TALLOC_FREE(ad);
3226 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
3227 if (rc != 0) {
3228 return -1;
3231 adp_smb_fname = synthetic_smb_fname(talloc_tos(), adp,
3232 NULL, NULL,
3233 smb_fname->flags);
3234 TALLOC_FREE(adp);
3235 if (adp_smb_fname == NULL) {
3236 return -1;
3239 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3240 TALLOC_FREE(adp_smb_fname);
3241 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3242 rc = 0;
3245 return rc;
3248 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3249 const struct smb_filename *smb_fname,
3250 bool force_unlink)
3253 * OS X ignores resource fork stream delete requests, so nothing to do
3254 * here. Removing the file will remove the xattr anyway, so we don't
3255 * have to take care of removing 0 byte resource forks that could be
3256 * left behind.
3258 return 0;
3261 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3262 const struct smb_filename *smb_fname,
3263 bool force_unlink)
3265 struct fruit_config_data *config = NULL;
3266 int rc;
3268 SMB_VFS_HANDLE_GET_DATA(handle, config,
3269 struct fruit_config_data, return -1);
3271 switch (config->rsrc) {
3272 case FRUIT_RSRC_STREAM:
3273 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3274 break;
3276 case FRUIT_RSRC_ADFILE:
3277 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3278 break;
3280 case FRUIT_RSRC_XATTR:
3281 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3282 break;
3284 default:
3285 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3286 return -1;
3289 return rc;
3292 static int fruit_unlink(vfs_handle_struct *handle,
3293 const struct smb_filename *smb_fname)
3295 int rc;
3296 struct fruit_config_data *config = NULL;
3297 struct smb_filename *rsrc_smb_fname = NULL;
3299 SMB_VFS_HANDLE_GET_DATA(handle, config,
3300 struct fruit_config_data, return -1);
3302 if (is_afpinfo_stream(smb_fname)) {
3303 return fruit_unlink_meta(handle, smb_fname);
3304 } else if (is_afpresource_stream(smb_fname)) {
3305 return fruit_unlink_rsrc(handle, smb_fname, false);
3306 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3307 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3311 * A request to delete the base file. Because 0 byte resource
3312 * fork streams are not listed by fruit_streaminfo,
3313 * delete_all_streams() can't remove 0 byte resource fork
3314 * streams, so we have to cleanup this here.
3316 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3317 smb_fname->base_name,
3318 AFPRESOURCE_STREAM_NAME,
3319 NULL,
3320 smb_fname->flags);
3321 if (rsrc_smb_fname == NULL) {
3322 return -1;
3325 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3326 if ((rc != 0) && (errno != ENOENT)) {
3327 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3328 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3329 TALLOC_FREE(rsrc_smb_fname);
3330 return -1;
3332 TALLOC_FREE(rsrc_smb_fname);
3334 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3337 static int fruit_chmod(vfs_handle_struct *handle,
3338 const struct smb_filename *smb_fname,
3339 mode_t mode)
3341 int rc = -1;
3342 char *adp = NULL;
3343 struct fruit_config_data *config = NULL;
3344 const char *path = smb_fname->base_name;
3345 struct smb_filename *smb_fname_adp = NULL;
3347 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3348 if (rc != 0) {
3349 return rc;
3352 SMB_VFS_HANDLE_GET_DATA(handle, config,
3353 struct fruit_config_data, return -1);
3355 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3356 return 0;
3359 if (!VALID_STAT(smb_fname->st)) {
3360 return 0;
3363 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3364 return 0;
3367 rc = adouble_path(talloc_tos(), path, &adp);
3368 if (rc != 0) {
3369 return -1;
3372 DEBUG(10, ("fruit_chmod: %s\n", adp));
3374 smb_fname_adp = synthetic_smb_fname(talloc_tos(),
3375 adp,
3376 NULL,
3377 NULL,
3378 smb_fname->flags);
3379 if (smb_fname_adp == NULL) {
3380 TALLOC_FREE(adp);
3381 errno = ENOMEM;
3382 return -1;
3385 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3386 if (errno == ENOENT) {
3387 rc = 0;
3390 TALLOC_FREE(smb_fname_adp);
3391 TALLOC_FREE(adp);
3392 return rc;
3395 static int fruit_chown(vfs_handle_struct *handle,
3396 const struct smb_filename *smb_fname,
3397 uid_t uid,
3398 gid_t gid)
3400 int rc = -1;
3401 char *adp = NULL;
3402 struct fruit_config_data *config = NULL;
3403 struct smb_filename *adp_smb_fname = NULL;
3405 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3406 if (rc != 0) {
3407 return rc;
3410 SMB_VFS_HANDLE_GET_DATA(handle, config,
3411 struct fruit_config_data, return -1);
3413 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3414 return 0;
3417 if (!VALID_STAT(smb_fname->st)) {
3418 return 0;
3421 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3422 return 0;
3425 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
3426 if (rc != 0) {
3427 goto done;
3430 DEBUG(10, ("fruit_chown: %s\n", adp));
3432 adp_smb_fname = synthetic_smb_fname(talloc_tos(),
3433 adp,
3434 NULL,
3435 NULL,
3436 smb_fname->flags);
3437 if (adp_smb_fname == NULL) {
3438 errno = ENOMEM;
3439 rc = -1;
3440 goto done;
3443 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3444 if (errno == ENOENT) {
3445 rc = 0;
3448 done:
3449 TALLOC_FREE(adp);
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;
3500 * Check whether it's a valid AppleDouble file, if
3501 * yes, delete it, ignore it otherwise.
3503 ad = ad_get(talloc_tos(), handle, p, ADOUBLE_RSRC);
3504 if (ad == NULL) {
3505 TALLOC_FREE(p);
3506 continue;
3508 TALLOC_FREE(ad);
3510 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3511 NULL, NULL,
3512 smb_fname->flags);
3513 TALLOC_FREE(p);
3514 if (ad_smb_fname == NULL) {
3515 DBG_ERR("synthetic_smb_fname failed\n");
3516 return -1;
3519 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3520 TALLOC_FREE(ad_smb_fname);
3521 if (ret != 0) {
3522 DBG_ERR("Deleting [%s] failed\n",
3523 smb_fname_str_dbg(ad_smb_fname));
3527 exit_rmdir:
3528 if (dh) {
3529 closedir(dh);
3531 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3534 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3535 files_struct *fsp, void *data,
3536 size_t n, off_t offset)
3538 ssize_t nread;
3539 int ret;
3541 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3543 if (nread == n) {
3544 return nread;
3547 DBG_ERR("Removing [%s] after short read [%zd]\n",
3548 fsp_str_dbg(fsp), nread);
3550 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3551 if (ret != 0) {
3552 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3553 return -1;
3556 errno = EINVAL;
3557 return -1;
3560 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3561 files_struct *fsp, void *data,
3562 size_t n, off_t offset)
3564 AfpInfo *ai = NULL;
3565 struct adouble *ad = NULL;
3566 char afpinfo_buf[AFP_INFO_SIZE];
3567 char *p = NULL;
3568 ssize_t nread;
3570 ai = afpinfo_new(talloc_tos());
3571 if (ai == NULL) {
3572 return -1;
3575 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3576 if (ad == NULL) {
3577 nread = -1;
3578 goto fail;
3581 p = ad_get_entry(ad, ADEID_FINDERI);
3582 if (p == NULL) {
3583 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3584 nread = -1;
3585 goto fail;
3588 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3590 nread = afpinfo_pack(ai, afpinfo_buf);
3591 if (nread != AFP_INFO_SIZE) {
3592 nread = -1;
3593 goto fail;
3596 memcpy(data, afpinfo_buf, n);
3597 nread = n;
3599 fail:
3600 TALLOC_FREE(ai);
3601 return nread;
3604 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3605 files_struct *fsp, void *data,
3606 size_t n, off_t offset)
3608 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3609 ssize_t nread;
3610 ssize_t to_return;
3613 * OS X has a off-by-1 error in the offset calculation, so we're
3614 * bug compatible here. It won't hurt, as any relevant real
3615 * world read requests from the AFP_AfpInfo stream will be
3616 * offset=0 n=60. offset is ignored anyway, see below.
3618 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3619 return 0;
3622 /* Yes, macOS always reads from offset 0 */
3623 offset = 0;
3624 to_return = MIN(n, AFP_INFO_SIZE);
3626 switch (fio->config->meta) {
3627 case FRUIT_META_STREAM:
3628 nread = fruit_pread_meta_stream(handle, fsp, data,
3629 to_return, offset);
3630 break;
3632 case FRUIT_META_NETATALK:
3633 nread = fruit_pread_meta_adouble(handle, fsp, data,
3634 to_return, offset);
3635 break;
3637 default:
3638 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3639 return -1;
3642 return nread;
3645 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3646 files_struct *fsp, void *data,
3647 size_t n, off_t offset)
3649 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3652 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3653 files_struct *fsp, void *data,
3654 size_t n, off_t offset)
3656 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3659 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3660 files_struct *fsp, void *data,
3661 size_t n, off_t offset)
3663 struct adouble *ad = NULL;
3664 ssize_t nread;
3666 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3667 if (ad == NULL) {
3668 return -1;
3671 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3672 offset + ad_getentryoff(ad, ADEID_RFORK));
3674 TALLOC_FREE(ad);
3675 return nread;
3678 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3679 files_struct *fsp, void *data,
3680 size_t n, off_t offset)
3682 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3683 ssize_t nread;
3685 switch (fio->config->rsrc) {
3686 case FRUIT_RSRC_STREAM:
3687 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3688 break;
3690 case FRUIT_RSRC_ADFILE:
3691 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3692 break;
3694 case FRUIT_RSRC_XATTR:
3695 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3696 break;
3698 default:
3699 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3700 return -1;
3703 return nread;
3706 static ssize_t fruit_pread(vfs_handle_struct *handle,
3707 files_struct *fsp, void *data,
3708 size_t n, off_t offset)
3710 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3711 ssize_t nread;
3713 DBG_DEBUG("Path [%s] offset=%zd, size=%zd\n",
3714 fsp_str_dbg(fsp), offset, n);
3716 if (fio == NULL) {
3717 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3720 if (fio->type == ADOUBLE_META) {
3721 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3722 } else {
3723 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3726 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3727 return nread;
3730 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
3731 files_struct *fsp, const void *data,
3732 size_t n, off_t offset)
3734 AfpInfo *ai = NULL;
3735 int ret;
3737 ai = afpinfo_unpack(talloc_tos(), data);
3738 if (ai == NULL) {
3739 return -1;
3742 if (ai_empty_finderinfo(ai)) {
3743 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3744 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3745 DBG_ERR("Can't delete metadata for %s: %s\n",
3746 fsp_str_dbg(fsp), strerror(errno));
3747 TALLOC_FREE(ai);
3748 return -1;
3751 return n;
3754 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3757 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
3758 files_struct *fsp, const void *data,
3759 size_t n, off_t offset)
3761 struct adouble *ad = NULL;
3762 AfpInfo *ai = NULL;
3763 char *p = NULL;
3764 int ret;
3766 ai = afpinfo_unpack(talloc_tos(), data);
3767 if (ai == NULL) {
3768 return -1;
3771 if (ai_empty_finderinfo(ai)) {
3772 ret = SMB_VFS_REMOVEXATTR(handle->conn,
3773 fsp->fsp_name->base_name,
3774 AFPINFO_EA_NETATALK);
3776 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3777 DBG_ERR("Can't delete metadata for %s: %s\n",
3778 fsp_str_dbg(fsp), strerror(errno));
3779 return -1;
3782 return n;
3785 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3786 if (ad == NULL) {
3787 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
3788 if (ad == NULL) {
3789 return -1;
3792 p = ad_get_entry(ad, ADEID_FINDERI);
3793 if (p == NULL) {
3794 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3795 TALLOC_FREE(ad);
3796 return -1;
3799 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3801 ret = ad_fset(ad, fsp);
3802 if (ret != 0) {
3803 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3804 TALLOC_FREE(ad);
3805 return -1;
3808 TALLOC_FREE(ad);
3809 return n;
3812 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
3813 files_struct *fsp, const void *data,
3814 size_t n, off_t offset)
3816 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3817 ssize_t nwritten;
3820 * Writing an all 0 blob to the metadata stream
3821 * results in the stream being removed on a macOS
3822 * server. This ensures we behave the same and it
3823 * verified by the "delete AFP_AfpInfo by writing all
3824 * 0" test.
3826 if (n != AFP_INFO_SIZE || offset != 0) {
3827 DBG_ERR("unexpected offset=%jd or size=%jd\n",
3828 (intmax_t)offset, (intmax_t)n);
3829 return -1;
3832 switch (fio->config->meta) {
3833 case FRUIT_META_STREAM:
3834 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
3835 n, offset);
3836 break;
3838 case FRUIT_META_NETATALK:
3839 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
3840 n, offset);
3841 break;
3843 default:
3844 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3845 return -1;
3848 return nwritten;
3851 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
3852 files_struct *fsp, const void *data,
3853 size_t n, off_t offset)
3855 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3858 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
3859 files_struct *fsp, const void *data,
3860 size_t n, off_t offset)
3862 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3865 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
3866 files_struct *fsp, const void *data,
3867 size_t n, off_t offset)
3869 struct adouble *ad = NULL;
3870 ssize_t nwritten;
3871 int ret;
3873 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3874 if (ad == NULL) {
3875 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
3876 return -1;
3879 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3880 offset + ad_getentryoff(ad, ADEID_RFORK));
3881 if (nwritten != n) {
3882 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
3883 fsp_str_dbg(fsp), nwritten, n);
3884 TALLOC_FREE(ad);
3885 return -1;
3888 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3889 ad_setentrylen(ad, ADEID_RFORK, n + offset);
3890 ret = ad_fset(ad, fsp);
3891 if (ret != 0) {
3892 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3893 TALLOC_FREE(ad);
3894 return -1;
3898 TALLOC_FREE(ad);
3899 return n;
3902 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
3903 files_struct *fsp, const void *data,
3904 size_t n, off_t offset)
3906 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3907 ssize_t nwritten;
3909 switch (fio->config->rsrc) {
3910 case FRUIT_RSRC_STREAM:
3911 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
3912 break;
3914 case FRUIT_RSRC_ADFILE:
3915 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
3916 break;
3918 case FRUIT_RSRC_XATTR:
3919 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
3920 break;
3922 default:
3923 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3924 return -1;
3927 return nwritten;
3930 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
3931 files_struct *fsp, const void *data,
3932 size_t n, off_t offset)
3934 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3935 ssize_t nwritten;
3937 DBG_DEBUG("Path [%s] offset=%zd, size=%zd\n",
3938 fsp_str_dbg(fsp), offset, n);
3940 if (fio == NULL) {
3941 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3944 if (fio->type == ADOUBLE_META) {
3945 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
3946 } else {
3947 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
3950 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
3951 return nwritten;
3955 * Helper to stat/lstat the base file of an smb_fname.
3957 static int fruit_stat_base(vfs_handle_struct *handle,
3958 struct smb_filename *smb_fname,
3959 bool follow_links)
3961 char *tmp_stream_name;
3962 int rc;
3964 tmp_stream_name = smb_fname->stream_name;
3965 smb_fname->stream_name = NULL;
3966 if (follow_links) {
3967 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
3968 } else {
3969 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3971 smb_fname->stream_name = tmp_stream_name;
3972 return rc;
3975 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
3976 struct smb_filename *smb_fname,
3977 bool follow_links)
3979 int ret;
3981 if (follow_links) {
3982 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
3983 } else {
3984 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3987 return ret;
3990 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
3991 struct smb_filename *smb_fname,
3992 bool follow_links)
3994 struct adouble *ad = NULL;
3996 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3997 if (ad == NULL) {
3998 DBG_INFO("fruit_stat_meta %s: %s\n",
3999 smb_fname_str_dbg(smb_fname), strerror(errno));
4000 errno = ENOENT;
4001 return -1;
4003 TALLOC_FREE(ad);
4005 /* Populate the stat struct with info from the base file. */
4006 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4007 return -1;
4009 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4010 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4011 smb_fname->stream_name);
4012 return 0;
4015 static int fruit_stat_meta(vfs_handle_struct *handle,
4016 struct smb_filename *smb_fname,
4017 bool follow_links)
4019 struct fruit_config_data *config = NULL;
4020 int ret;
4022 SMB_VFS_HANDLE_GET_DATA(handle, config,
4023 struct fruit_config_data, return -1);
4025 switch (config->meta) {
4026 case FRUIT_META_STREAM:
4027 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4028 break;
4030 case FRUIT_META_NETATALK:
4031 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4032 break;
4034 default:
4035 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4036 return -1;
4039 return ret;
4042 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4043 struct smb_filename *smb_fname,
4044 bool follow_links)
4046 struct adouble *ad = NULL;
4047 int ret;
4049 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
4050 if (ad == NULL) {
4051 errno = ENOENT;
4052 return -1;
4055 /* Populate the stat struct with info from the base file. */
4056 ret = fruit_stat_base(handle, smb_fname, follow_links);
4057 if (ret != 0) {
4058 TALLOC_FREE(ad);
4059 return -1;
4062 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4063 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4064 smb_fname->stream_name);
4065 TALLOC_FREE(ad);
4066 return 0;
4069 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4070 struct smb_filename *smb_fname,
4071 bool follow_links)
4073 int ret;
4075 if (follow_links) {
4076 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4077 } else {
4078 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4081 return ret;
4084 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4085 struct smb_filename *smb_fname,
4086 bool follow_links)
4088 #ifdef HAVE_ATTROPEN
4089 int ret;
4090 int fd = -1;
4092 /* Populate the stat struct with info from the base file. */
4093 ret = fruit_stat_base(handle, smb_fname, follow_links);
4094 if (ret != 0) {
4095 return -1;
4098 fd = attropen(smb_fname->base_name,
4099 AFPRESOURCE_EA_NETATALK,
4100 O_RDONLY);
4101 if (fd == -1) {
4102 return 0;
4105 ret = sys_fstat(fd, &smb_fname->st, false);
4106 if (ret != 0) {
4107 close(fd);
4108 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4109 AFPRESOURCE_EA_NETATALK);
4110 return -1;
4112 close(fd);
4113 fd = -1;
4115 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4116 smb_fname->stream_name);
4118 return ret;
4120 #else
4121 errno = ENOSYS;
4122 return -1;
4123 #endif
4126 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4127 struct smb_filename *smb_fname,
4128 bool follow_links)
4130 struct fruit_config_data *config = NULL;
4131 int ret;
4133 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4135 SMB_VFS_HANDLE_GET_DATA(handle, config,
4136 struct fruit_config_data, return -1);
4138 switch (config->rsrc) {
4139 case FRUIT_RSRC_STREAM:
4140 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4141 break;
4143 case FRUIT_RSRC_XATTR:
4144 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4145 break;
4147 case FRUIT_RSRC_ADFILE:
4148 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4149 break;
4151 default:
4152 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4153 return -1;
4156 return ret;
4159 static int fruit_stat(vfs_handle_struct *handle,
4160 struct smb_filename *smb_fname)
4162 int rc = -1;
4164 DEBUG(10, ("fruit_stat called for %s\n",
4165 smb_fname_str_dbg(smb_fname)));
4167 if (!is_ntfs_stream_smb_fname(smb_fname)
4168 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4169 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4170 if (rc == 0) {
4171 update_btime(handle, smb_fname);
4173 return rc;
4177 * Note if lp_posix_paths() is true, we can never
4178 * get here as is_ntfs_stream_smb_fname() is
4179 * always false. So we never need worry about
4180 * not following links here.
4183 if (is_afpinfo_stream(smb_fname)) {
4184 rc = fruit_stat_meta(handle, smb_fname, true);
4185 } else if (is_afpresource_stream(smb_fname)) {
4186 rc = fruit_stat_rsrc(handle, smb_fname, true);
4187 } else {
4188 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4191 if (rc == 0) {
4192 update_btime(handle, smb_fname);
4193 smb_fname->st.st_ex_mode &= ~S_IFMT;
4194 smb_fname->st.st_ex_mode |= S_IFREG;
4195 smb_fname->st.st_ex_blocks =
4196 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4198 return rc;
4201 static int fruit_lstat(vfs_handle_struct *handle,
4202 struct smb_filename *smb_fname)
4204 int rc = -1;
4206 DEBUG(10, ("fruit_lstat called for %s\n",
4207 smb_fname_str_dbg(smb_fname)));
4209 if (!is_ntfs_stream_smb_fname(smb_fname)
4210 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4211 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4212 if (rc == 0) {
4213 update_btime(handle, smb_fname);
4215 return rc;
4218 if (is_afpinfo_stream(smb_fname)) {
4219 rc = fruit_stat_meta(handle, smb_fname, false);
4220 } else if (is_afpresource_stream(smb_fname)) {
4221 rc = fruit_stat_rsrc(handle, smb_fname, false);
4222 } else {
4223 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4226 if (rc == 0) {
4227 update_btime(handle, smb_fname);
4228 smb_fname->st.st_ex_mode &= ~S_IFMT;
4229 smb_fname->st.st_ex_mode |= S_IFREG;
4230 smb_fname->st.st_ex_blocks =
4231 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4233 return rc;
4236 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4237 files_struct *fsp,
4238 SMB_STRUCT_STAT *sbuf)
4240 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4243 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4244 files_struct *fsp,
4245 SMB_STRUCT_STAT *sbuf)
4247 int ret;
4249 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4250 if (ret != 0) {
4251 return -1;
4254 *sbuf = fsp->base_fsp->fsp_name->st;
4255 sbuf->st_ex_size = AFP_INFO_SIZE;
4256 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4258 return 0;
4261 static int fruit_fstat_meta(vfs_handle_struct *handle,
4262 files_struct *fsp,
4263 SMB_STRUCT_STAT *sbuf,
4264 struct fio *fio)
4266 int ret;
4268 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4270 switch (fio->config->meta) {
4271 case FRUIT_META_STREAM:
4272 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4273 break;
4275 case FRUIT_META_NETATALK:
4276 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4277 break;
4279 default:
4280 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4281 return -1;
4284 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4285 return ret;
4288 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4289 files_struct *fsp,
4290 SMB_STRUCT_STAT *sbuf)
4292 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4295 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4296 files_struct *fsp,
4297 SMB_STRUCT_STAT *sbuf)
4299 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4302 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4303 files_struct *fsp,
4304 SMB_STRUCT_STAT *sbuf)
4306 struct adouble *ad = NULL;
4307 int ret;
4309 /* Populate the stat struct with info from the base file. */
4310 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4311 if (ret == -1) {
4312 return -1;
4315 ad = ad_get(talloc_tos(), handle,
4316 fsp->base_fsp->fsp_name->base_name,
4317 ADOUBLE_RSRC);
4318 if (ad == NULL) {
4319 DBG_ERR("ad_get [%s] failed [%s]\n",
4320 fsp_str_dbg(fsp), strerror(errno));
4321 return -1;
4324 *sbuf = fsp->base_fsp->fsp_name->st;
4325 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4326 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4328 TALLOC_FREE(ad);
4329 return 0;
4332 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4333 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4335 int ret;
4337 switch (fio->config->rsrc) {
4338 case FRUIT_RSRC_STREAM:
4339 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4340 break;
4342 case FRUIT_RSRC_ADFILE:
4343 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4344 break;
4346 case FRUIT_RSRC_XATTR:
4347 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4348 break;
4350 default:
4351 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4352 return -1;
4355 return ret;
4358 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4359 SMB_STRUCT_STAT *sbuf)
4361 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4362 int rc;
4364 if (fio == NULL) {
4365 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4368 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4370 if (fio->type == ADOUBLE_META) {
4371 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4372 } else {
4373 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4376 if (rc == 0) {
4377 sbuf->st_ex_mode &= ~S_IFMT;
4378 sbuf->st_ex_mode |= S_IFREG;
4379 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4382 DBG_DEBUG("Path [%s] rc [%d] size [%zd]\n",
4383 fsp_str_dbg(fsp), rc, sbuf->st_ex_size);
4384 return rc;
4387 static NTSTATUS fruit_streaminfo_meta_stream(
4388 vfs_handle_struct *handle,
4389 struct files_struct *fsp,
4390 const struct smb_filename *smb_fname,
4391 TALLOC_CTX *mem_ctx,
4392 unsigned int *pnum_streams,
4393 struct stream_struct **pstreams)
4395 struct stream_struct *stream = *pstreams;
4396 unsigned int num_streams = *pnum_streams;
4397 struct smb_filename *sname = NULL;
4398 int i;
4399 int ret;
4400 bool ok;
4402 for (i = 0; i < num_streams; i++) {
4403 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4404 break;
4408 if (i == num_streams) {
4409 return NT_STATUS_OK;
4412 if (stream[i].size == AFP_INFO_SIZE) {
4413 return NT_STATUS_OK;
4416 DBG_ERR("Removing invalid AFPINFO_STREAM size [%zd] from [%s]\n",
4417 stream[i].size, smb_fname_str_dbg(smb_fname));
4419 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4420 if (!ok) {
4421 return NT_STATUS_INTERNAL_ERROR;
4424 sname = synthetic_smb_fname(talloc_tos(),
4425 smb_fname->base_name,
4426 AFPINFO_STREAM_NAME,
4427 NULL, 0);
4428 if (sname == NULL) {
4429 return NT_STATUS_NO_MEMORY;
4432 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4433 TALLOC_FREE(sname);
4434 if (ret != 0) {
4435 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4436 return map_nt_error_from_unix(errno);
4439 return NT_STATUS_OK;
4442 static NTSTATUS fruit_streaminfo_meta_netatalk(
4443 vfs_handle_struct *handle,
4444 struct files_struct *fsp,
4445 const struct smb_filename *smb_fname,
4446 TALLOC_CTX *mem_ctx,
4447 unsigned int *pnum_streams,
4448 struct stream_struct **pstreams)
4450 struct stream_struct *stream = *pstreams;
4451 unsigned int num_streams = *pnum_streams;
4452 struct adouble *ad = NULL;
4453 bool is_fi_empty;
4454 int i;
4455 bool ok;
4457 /* Remove the Netatalk xattr from the list */
4458 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4459 ":" NETATALK_META_XATTR ":$DATA");
4460 if (!ok) {
4461 return NT_STATUS_NO_MEMORY;
4465 * Check if there's a AFPINFO_STREAM from the VFS streams
4466 * backend and if yes, remove it from the list
4468 for (i = 0; i < num_streams; i++) {
4469 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4470 break;
4474 if (i < num_streams) {
4475 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4476 smb_fname_str_dbg(smb_fname));
4478 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4479 AFPINFO_STREAM);
4480 if (!ok) {
4481 return NT_STATUS_INTERNAL_ERROR;
4485 ad = ad_get(talloc_tos(), handle,
4486 smb_fname->base_name, ADOUBLE_META);
4487 if (ad == NULL) {
4488 return NT_STATUS_OK;
4491 is_fi_empty = ad_empty_finderinfo(ad);
4492 TALLOC_FREE(ad);
4494 if (is_fi_empty) {
4495 return NT_STATUS_OK;
4498 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4499 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4500 smb_roundup(handle->conn, AFP_INFO_SIZE));
4501 if (!ok) {
4502 return NT_STATUS_NO_MEMORY;
4505 return NT_STATUS_OK;
4508 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4509 struct files_struct *fsp,
4510 const struct smb_filename *smb_fname,
4511 TALLOC_CTX *mem_ctx,
4512 unsigned int *pnum_streams,
4513 struct stream_struct **pstreams)
4515 struct fruit_config_data *config = NULL;
4516 NTSTATUS status;
4518 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4519 return NT_STATUS_INTERNAL_ERROR);
4521 switch (config->meta) {
4522 case FRUIT_META_NETATALK:
4523 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4524 mem_ctx, pnum_streams,
4525 pstreams);
4526 break;
4528 case FRUIT_META_STREAM:
4529 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4530 mem_ctx, pnum_streams,
4531 pstreams);
4532 break;
4534 default:
4535 return NT_STATUS_INTERNAL_ERROR;
4538 return status;
4541 static NTSTATUS fruit_streaminfo_rsrc_stream(
4542 vfs_handle_struct *handle,
4543 struct files_struct *fsp,
4544 const struct smb_filename *smb_fname,
4545 TALLOC_CTX *mem_ctx,
4546 unsigned int *pnum_streams,
4547 struct stream_struct **pstreams)
4549 bool ok;
4551 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4552 if (!ok) {
4553 DBG_ERR("Filtering resource stream failed\n");
4554 return NT_STATUS_INTERNAL_ERROR;
4556 return NT_STATUS_OK;
4559 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4560 vfs_handle_struct *handle,
4561 struct files_struct *fsp,
4562 const struct smb_filename *smb_fname,
4563 TALLOC_CTX *mem_ctx,
4564 unsigned int *pnum_streams,
4565 struct stream_struct **pstreams)
4567 bool ok;
4569 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4570 if (!ok) {
4571 DBG_ERR("Filtering resource stream failed\n");
4572 return NT_STATUS_INTERNAL_ERROR;
4574 return NT_STATUS_OK;
4577 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4578 vfs_handle_struct *handle,
4579 struct files_struct *fsp,
4580 const struct smb_filename *smb_fname,
4581 TALLOC_CTX *mem_ctx,
4582 unsigned int *pnum_streams,
4583 struct stream_struct **pstreams)
4585 struct stream_struct *stream = *pstreams;
4586 unsigned int num_streams = *pnum_streams;
4587 struct adouble *ad = NULL;
4588 bool ok;
4589 size_t rlen;
4590 int i;
4593 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4594 * and if yes, remove it from the list
4596 for (i = 0; i < num_streams; i++) {
4597 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4598 break;
4602 if (i < num_streams) {
4603 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4604 smb_fname_str_dbg(smb_fname));
4606 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4607 AFPRESOURCE_STREAM);
4608 if (!ok) {
4609 return NT_STATUS_INTERNAL_ERROR;
4613 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
4614 ADOUBLE_RSRC);
4615 if (ad == NULL) {
4616 return NT_STATUS_OK;
4619 rlen = ad_getentrylen(ad, ADEID_RFORK);
4620 TALLOC_FREE(ad);
4622 if (rlen == 0) {
4623 return NT_STATUS_OK;
4626 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4627 AFPRESOURCE_STREAM_NAME, rlen,
4628 smb_roundup(handle->conn, rlen));
4629 if (!ok) {
4630 return NT_STATUS_NO_MEMORY;
4633 return NT_STATUS_OK;
4636 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4637 struct files_struct *fsp,
4638 const struct smb_filename *smb_fname,
4639 TALLOC_CTX *mem_ctx,
4640 unsigned int *pnum_streams,
4641 struct stream_struct **pstreams)
4643 struct fruit_config_data *config = NULL;
4644 NTSTATUS status;
4646 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4647 return NT_STATUS_INTERNAL_ERROR);
4649 switch (config->rsrc) {
4650 case FRUIT_RSRC_STREAM:
4651 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4652 mem_ctx, pnum_streams,
4653 pstreams);
4654 break;
4656 case FRUIT_RSRC_XATTR:
4657 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4658 mem_ctx, pnum_streams,
4659 pstreams);
4660 break;
4662 case FRUIT_RSRC_ADFILE:
4663 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4664 mem_ctx, pnum_streams,
4665 pstreams);
4666 break;
4668 default:
4669 return NT_STATUS_INTERNAL_ERROR;
4672 return status;
4675 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4676 struct files_struct *fsp,
4677 const struct smb_filename *smb_fname,
4678 TALLOC_CTX *mem_ctx,
4679 unsigned int *pnum_streams,
4680 struct stream_struct **pstreams)
4682 struct fruit_config_data *config = NULL;
4683 NTSTATUS status;
4685 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4686 return NT_STATUS_UNSUCCESSFUL);
4688 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4690 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4691 pnum_streams, pstreams);
4692 if (!NT_STATUS_IS_OK(status)) {
4693 return status;
4696 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4697 mem_ctx, pnum_streams, pstreams);
4698 if (!NT_STATUS_IS_OK(status)) {
4699 return status;
4702 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4703 mem_ctx, pnum_streams, pstreams);
4704 if (!NT_STATUS_IS_OK(status)) {
4705 return status;
4708 return NT_STATUS_OK;
4711 static int fruit_ntimes(vfs_handle_struct *handle,
4712 const struct smb_filename *smb_fname,
4713 struct smb_file_time *ft)
4715 int rc = 0;
4716 struct adouble *ad = NULL;
4717 struct fruit_config_data *config = NULL;
4719 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4720 return -1);
4722 if ((config->meta != FRUIT_META_NETATALK) ||
4723 null_timespec(ft->create_time))
4725 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4728 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4729 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4731 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
4732 if (ad == NULL) {
4733 goto exit;
4736 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4737 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4739 rc = ad_set(ad, smb_fname->base_name);
4741 exit:
4743 TALLOC_FREE(ad);
4744 if (rc != 0) {
4745 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4746 return -1;
4748 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4751 static int fruit_fallocate(struct vfs_handle_struct *handle,
4752 struct files_struct *fsp,
4753 uint32_t mode,
4754 off_t offset,
4755 off_t len)
4757 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4759 if (fio == NULL) {
4760 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4763 /* Let the pwrite code path handle it. */
4764 errno = ENOSYS;
4765 return -1;
4768 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4769 struct files_struct *fsp,
4770 off_t offset)
4772 if (offset == 0) {
4773 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4776 #ifdef HAVE_ATTROPEN
4777 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4778 #endif
4779 return 0;
4782 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4783 struct files_struct *fsp,
4784 off_t offset)
4786 int rc;
4787 struct adouble *ad = NULL;
4788 off_t ad_off;
4790 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4791 if (ad == NULL) {
4792 DBG_DEBUG("ad_get [%s] failed [%s]\n",
4793 fsp_str_dbg(fsp), strerror(errno));
4794 return -1;
4797 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4799 rc = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset + ad_off);
4800 if (rc != 0) {
4801 TALLOC_FREE(ad);
4802 return -1;
4805 ad_setentrylen(ad, ADEID_RFORK, offset);
4807 rc = ad_fset(ad, fsp);
4808 if (rc != 0) {
4809 DBG_ERR("ad_fset [%s] failed [%s]\n",
4810 fsp_str_dbg(fsp), strerror(errno));
4811 TALLOC_FREE(ad);
4812 return -1;
4815 TALLOC_FREE(ad);
4816 return 0;
4819 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4820 struct files_struct *fsp,
4821 off_t offset)
4823 if (offset == 0) {
4824 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4827 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4830 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4831 struct files_struct *fsp,
4832 off_t offset)
4834 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4835 int ret;
4837 switch (fio->config->rsrc) {
4838 case FRUIT_RSRC_XATTR:
4839 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4840 break;
4842 case FRUIT_RSRC_ADFILE:
4843 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4844 break;
4846 case FRUIT_RSRC_STREAM:
4847 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4848 break;
4850 default:
4851 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4852 return -1;
4856 return ret;
4859 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
4860 struct files_struct *fsp,
4861 off_t offset)
4863 if (offset > 60) {
4864 DBG_WARNING("ftruncate %s to %jd",
4865 fsp_str_dbg(fsp), (intmax_t)offset);
4866 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4867 errno = EOVERFLOW;
4868 return -1;
4871 /* OS X returns success but does nothing */
4872 DBG_INFO("ignoring ftruncate %s to %jd\n",
4873 fsp_str_dbg(fsp), (intmax_t)offset);
4874 return 0;
4877 static int fruit_ftruncate(struct vfs_handle_struct *handle,
4878 struct files_struct *fsp,
4879 off_t offset)
4881 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4882 int ret;
4884 DBG_DEBUG("Path [%s] offset [%zd]\n", fsp_str_dbg(fsp), offset);
4886 if (fio == NULL) {
4887 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4890 if (fio->type == ADOUBLE_META) {
4891 ret = fruit_ftruncate_meta(handle, fsp, offset);
4892 } else {
4893 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
4896 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
4897 return ret;
4900 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
4901 struct smb_request *req,
4902 uint16_t root_dir_fid,
4903 struct smb_filename *smb_fname,
4904 uint32_t access_mask,
4905 uint32_t share_access,
4906 uint32_t create_disposition,
4907 uint32_t create_options,
4908 uint32_t file_attributes,
4909 uint32_t oplock_request,
4910 struct smb2_lease *lease,
4911 uint64_t allocation_size,
4912 uint32_t private_flags,
4913 struct security_descriptor *sd,
4914 struct ea_list *ea_list,
4915 files_struct **result,
4916 int *pinfo,
4917 const struct smb2_create_blobs *in_context_blobs,
4918 struct smb2_create_blobs *out_context_blobs)
4920 NTSTATUS status;
4921 struct fruit_config_data *config = NULL;
4922 files_struct *fsp = NULL;
4924 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
4925 if (!NT_STATUS_IS_OK(status)) {
4926 goto fail;
4929 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4930 return NT_STATUS_UNSUCCESSFUL);
4932 status = SMB_VFS_NEXT_CREATE_FILE(
4933 handle, req, root_dir_fid, smb_fname,
4934 access_mask, share_access,
4935 create_disposition, create_options,
4936 file_attributes, oplock_request,
4937 lease,
4938 allocation_size, private_flags,
4939 sd, ea_list, result,
4940 pinfo, in_context_blobs, out_context_blobs);
4941 if (!NT_STATUS_IS_OK(status)) {
4942 return status;
4945 fsp = *result;
4947 if (global_fruit_config.nego_aapl) {
4948 if (config->copyfile_enabled) {
4950 * Set a flag in the fsp. Gets used in
4951 * copychunk to check whether the special
4952 * Apple copyfile semantics for copychunk
4953 * should be allowed in a copychunk request
4954 * with a count of 0.
4956 fsp->aapl_copyfile_supported = true;
4959 if (config->posix_rename && fsp->is_directory) {
4961 * Enable POSIX directory rename behaviour
4963 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
4968 * If this is a plain open for existing files, opening an 0
4969 * byte size resource fork MUST fail with
4970 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4972 * Cf the vfs_fruit torture tests in test_rfork_create().
4974 if (is_afpresource_stream(fsp->fsp_name) &&
4975 create_disposition == FILE_OPEN)
4977 if (fsp->fsp_name->st.st_ex_size == 0) {
4978 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4979 goto fail;
4983 if (is_ntfs_stream_smb_fname(smb_fname)
4984 || fsp->is_directory) {
4985 return status;
4988 if (config->locking == FRUIT_LOCKING_NETATALK) {
4989 status = fruit_check_access(
4990 handle, *result,
4991 access_mask,
4992 map_share_mode_to_deny_mode(share_access, 0));
4993 if (!NT_STATUS_IS_OK(status)) {
4994 goto fail;
4998 return status;
5000 fail:
5001 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5003 if (fsp) {
5004 close_file(req, fsp, ERROR_CLOSE);
5005 *result = fsp = NULL;
5008 return status;
5011 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5012 const struct smb_filename *fname,
5013 TALLOC_CTX *mem_ctx,
5014 struct readdir_attr_data **pattr_data)
5016 struct fruit_config_data *config = NULL;
5017 struct readdir_attr_data *attr_data;
5018 NTSTATUS status;
5020 SMB_VFS_HANDLE_GET_DATA(handle, config,
5021 struct fruit_config_data,
5022 return NT_STATUS_UNSUCCESSFUL);
5024 if (!global_fruit_config.nego_aapl) {
5025 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5028 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5030 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5031 if (*pattr_data == NULL) {
5032 return NT_STATUS_UNSUCCESSFUL;
5034 attr_data = *pattr_data;
5035 attr_data->type = RDATTR_AAPL;
5038 * Mac metadata: compressed FinderInfo, resource fork length
5039 * and creation date
5041 status = readdir_attr_macmeta(handle, fname, attr_data);
5042 if (!NT_STATUS_IS_OK(status)) {
5044 * Error handling is tricky: if we return failure from
5045 * this function, the corresponding directory entry
5046 * will to be passed to the client, so we really just
5047 * want to error out on fatal errors.
5049 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5050 goto fail;
5055 * UNIX mode
5057 if (config->unix_info_enabled) {
5058 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5062 * max_access
5064 if (!config->readdir_attr_max_access) {
5065 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5066 } else {
5067 status = smbd_calculate_access_mask(
5068 handle->conn,
5069 fname,
5070 false,
5071 SEC_FLAG_MAXIMUM_ALLOWED,
5072 &attr_data->attr_data.aapl.max_access);
5073 if (!NT_STATUS_IS_OK(status)) {
5074 goto fail;
5078 return NT_STATUS_OK;
5080 fail:
5081 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5082 fname->base_name, nt_errstr(status)));
5083 TALLOC_FREE(*pattr_data);
5084 return status;
5087 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5088 files_struct *fsp,
5089 uint32_t security_info,
5090 TALLOC_CTX *mem_ctx,
5091 struct security_descriptor **ppdesc)
5093 NTSTATUS status;
5094 struct security_ace ace;
5095 struct dom_sid sid;
5096 struct fruit_config_data *config;
5098 SMB_VFS_HANDLE_GET_DATA(handle, config,
5099 struct fruit_config_data,
5100 return NT_STATUS_UNSUCCESSFUL);
5102 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5103 mem_ctx, ppdesc);
5104 if (!NT_STATUS_IS_OK(status)) {
5105 return status;
5109 * Add MS NFS style ACEs with uid, gid and mode
5111 if (!config->unix_info_enabled) {
5112 return NT_STATUS_OK;
5115 /* MS NFS style mode */
5116 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5117 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5118 status = security_descriptor_dacl_add(*ppdesc, &ace);
5119 if (!NT_STATUS_IS_OK(status)) {
5120 DEBUG(1,("failed to add MS NFS style ACE\n"));
5121 return status;
5124 /* MS NFS style uid */
5125 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5126 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5127 status = security_descriptor_dacl_add(*ppdesc, &ace);
5128 if (!NT_STATUS_IS_OK(status)) {
5129 DEBUG(1,("failed to add MS NFS style ACE\n"));
5130 return status;
5133 /* MS NFS style gid */
5134 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5135 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5136 status = security_descriptor_dacl_add(*ppdesc, &ace);
5137 if (!NT_STATUS_IS_OK(status)) {
5138 DEBUG(1,("failed to add MS NFS style ACE\n"));
5139 return status;
5142 return NT_STATUS_OK;
5145 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5146 files_struct *fsp,
5147 uint32_t security_info_sent,
5148 const struct security_descriptor *psd)
5150 NTSTATUS status;
5151 bool do_chmod;
5152 mode_t ms_nfs_mode = 0;
5153 int result;
5155 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5157 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5158 if (!NT_STATUS_IS_OK(status)) {
5159 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5160 return status;
5163 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5164 if (!NT_STATUS_IS_OK(status)) {
5165 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5166 return status;
5169 if (do_chmod) {
5170 if (fsp->fh->fd != -1) {
5171 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5172 } else {
5173 result = SMB_VFS_CHMOD(fsp->conn,
5174 fsp->fsp_name,
5175 ms_nfs_mode);
5178 if (result != 0) {
5179 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5180 result, (unsigned)ms_nfs_mode,
5181 strerror(errno)));
5182 status = map_nt_error_from_unix(errno);
5183 return status;
5187 return NT_STATUS_OK;
5190 struct fruit_copy_chunk_state {
5191 struct vfs_handle_struct *handle;
5192 off_t copied;
5193 struct files_struct *src_fsp;
5194 struct files_struct *dst_fsp;
5195 bool is_copyfile;
5198 static void fruit_copy_chunk_done(struct tevent_req *subreq);
5199 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
5200 TALLOC_CTX *mem_ctx,
5201 struct tevent_context *ev,
5202 struct files_struct *src_fsp,
5203 off_t src_off,
5204 struct files_struct *dest_fsp,
5205 off_t dest_off,
5206 off_t num)
5208 struct tevent_req *req, *subreq;
5209 struct fruit_copy_chunk_state *fruit_copy_chunk_state;
5210 NTSTATUS status;
5211 struct fruit_config_data *config;
5212 off_t to_copy = num;
5214 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5215 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5217 SMB_VFS_HANDLE_GET_DATA(handle, config,
5218 struct fruit_config_data,
5219 return NULL);
5221 req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
5222 struct fruit_copy_chunk_state);
5223 if (req == NULL) {
5224 return NULL;
5226 fruit_copy_chunk_state->handle = handle;
5227 fruit_copy_chunk_state->src_fsp = src_fsp;
5228 fruit_copy_chunk_state->dst_fsp = dest_fsp;
5231 * Check if this a OS X copyfile style copychunk request with
5232 * a requested chunk count of 0 that was translated to a
5233 * copy_chunk_send VFS call overloading the parameters src_off
5234 * = dest_off = num = 0.
5236 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
5237 src_fsp->aapl_copyfile_supported &&
5238 dest_fsp->aapl_copyfile_supported)
5240 status = vfs_stat_fsp(src_fsp);
5241 if (tevent_req_nterror(req, status)) {
5242 return tevent_req_post(req, ev);
5245 to_copy = src_fsp->fsp_name->st.st_ex_size;
5246 fruit_copy_chunk_state->is_copyfile = true;
5249 subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
5250 mem_ctx,
5252 src_fsp,
5253 src_off,
5254 dest_fsp,
5255 dest_off,
5256 to_copy);
5257 if (tevent_req_nomem(subreq, req)) {
5258 return tevent_req_post(req, ev);
5261 tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
5262 return req;
5265 static void fruit_copy_chunk_done(struct tevent_req *subreq)
5267 struct tevent_req *req = tevent_req_callback_data(
5268 subreq, struct tevent_req);
5269 struct fruit_copy_chunk_state *state = tevent_req_data(
5270 req, struct fruit_copy_chunk_state);
5271 NTSTATUS status;
5272 unsigned int num_streams = 0;
5273 struct stream_struct *streams = NULL;
5274 unsigned int i;
5275 struct smb_filename *src_fname_tmp = NULL;
5276 struct smb_filename *dst_fname_tmp = NULL;
5278 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
5279 subreq,
5280 &state->copied);
5281 TALLOC_FREE(subreq);
5282 if (tevent_req_nterror(req, status)) {
5283 return;
5286 if (!state->is_copyfile) {
5287 tevent_req_done(req);
5288 return;
5292 * Now copy all remaining streams. We know the share supports
5293 * streams, because we're in vfs_fruit. We don't do this async
5294 * because streams are few and small.
5296 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5297 state->src_fsp->fsp_name,
5298 req, &num_streams, &streams);
5299 if (tevent_req_nterror(req, status)) {
5300 return;
5303 if (num_streams == 1) {
5304 /* There is always one stream, ::$DATA. */
5305 tevent_req_done(req);
5306 return;
5309 for (i = 0; i < num_streams; i++) {
5310 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5311 __func__, streams[i].name, (size_t)streams[i].size));
5313 src_fname_tmp = synthetic_smb_fname(
5314 req,
5315 state->src_fsp->fsp_name->base_name,
5316 streams[i].name,
5317 NULL,
5318 state->src_fsp->fsp_name->flags);
5319 if (tevent_req_nomem(src_fname_tmp, req)) {
5320 return;
5323 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5324 TALLOC_FREE(src_fname_tmp);
5325 continue;
5328 dst_fname_tmp = synthetic_smb_fname(
5329 req,
5330 state->dst_fsp->fsp_name->base_name,
5331 streams[i].name,
5332 NULL,
5333 state->dst_fsp->fsp_name->flags);
5334 if (tevent_req_nomem(dst_fname_tmp, req)) {
5335 TALLOC_FREE(src_fname_tmp);
5336 return;
5339 status = copy_file(req,
5340 state->handle->conn,
5341 src_fname_tmp,
5342 dst_fname_tmp,
5343 OPENX_FILE_CREATE_IF_NOT_EXIST,
5344 0, false);
5345 if (!NT_STATUS_IS_OK(status)) {
5346 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5347 smb_fname_str_dbg(src_fname_tmp),
5348 smb_fname_str_dbg(dst_fname_tmp),
5349 nt_errstr(status)));
5350 TALLOC_FREE(src_fname_tmp);
5351 TALLOC_FREE(dst_fname_tmp);
5352 tevent_req_nterror(req, status);
5353 return;
5356 TALLOC_FREE(src_fname_tmp);
5357 TALLOC_FREE(dst_fname_tmp);
5360 TALLOC_FREE(streams);
5361 TALLOC_FREE(src_fname_tmp);
5362 TALLOC_FREE(dst_fname_tmp);
5363 tevent_req_done(req);
5366 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
5367 struct tevent_req *req,
5368 off_t *copied)
5370 struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
5371 req, struct fruit_copy_chunk_state);
5372 NTSTATUS status;
5374 if (tevent_req_is_nterror(req, &status)) {
5375 DEBUG(1, ("server side copy chunk failed: %s\n",
5376 nt_errstr(status)));
5377 *copied = 0;
5378 tevent_req_received(req);
5379 return status;
5382 *copied = fruit_copy_chunk_state->copied;
5383 tevent_req_received(req);
5385 return NT_STATUS_OK;
5388 static struct vfs_fn_pointers vfs_fruit_fns = {
5389 .connect_fn = fruit_connect,
5391 /* File operations */
5392 .chmod_fn = fruit_chmod,
5393 .chown_fn = fruit_chown,
5394 .unlink_fn = fruit_unlink,
5395 .rename_fn = fruit_rename,
5396 .rmdir_fn = fruit_rmdir,
5397 .open_fn = fruit_open,
5398 .pread_fn = fruit_pread,
5399 .pwrite_fn = fruit_pwrite,
5400 .stat_fn = fruit_stat,
5401 .lstat_fn = fruit_lstat,
5402 .fstat_fn = fruit_fstat,
5403 .streaminfo_fn = fruit_streaminfo,
5404 .ntimes_fn = fruit_ntimes,
5405 .ftruncate_fn = fruit_ftruncate,
5406 .fallocate_fn = fruit_fallocate,
5407 .create_file_fn = fruit_create_file,
5408 .readdir_attr_fn = fruit_readdir_attr,
5409 .copy_chunk_send_fn = fruit_copy_chunk_send,
5410 .copy_chunk_recv_fn = fruit_copy_chunk_recv,
5412 /* NT ACL operations */
5413 .fget_nt_acl_fn = fruit_fget_nt_acl,
5414 .fset_nt_acl_fn = fruit_fset_nt_acl,
5417 NTSTATUS vfs_fruit_init(void);
5418 NTSTATUS vfs_fruit_init(void)
5420 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5421 &vfs_fruit_fns);
5422 if (!NT_STATUS_IS_OK(ret)) {
5423 return ret;
5426 vfs_fruit_debug_level = debug_add_class("fruit");
5427 if (vfs_fruit_debug_level == -1) {
5428 vfs_fruit_debug_level = DBGC_VFS;
5429 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5430 "vfs_fruit_init"));
5431 } else {
5432 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5433 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5436 return ret;