vfs_fruit: allocate ad_data buffer up to AD_XATTR_MAX_HDR_SIZE bytes
[Samba.git] / source3 / modules / vfs_fruit.c
blob3dcaa60d033e559ca0dd01c3241e1f0607de38e0
1 /*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
38 * Enhanced OS X and Netatalk compatibility
39 * ========================================
41 * This modules takes advantage of vfs_streams_xattr and
42 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
43 * loaded in the correct order:
45 * vfs modules = catia fruit streams_xattr
47 * The module intercepts the OS X special streams "AFP_AfpInfo" and
48 * "AFP_Resource" and handles them in a special way. All other named
49 * streams are deferred to vfs_streams_xattr.
51 * The OS X client maps all NTFS illegal characters to the Unicode
52 * private range. This module optionally stores the charcters using
53 * their native ASCII encoding using vfs_catia. If you're not enabling
54 * this feature, you can skip catia from vfs modules.
56 * Finally, open modes are optionally checked against Netatalk AFP
57 * share modes.
59 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
60 * extended metadata for files and directories. This module optionally
61 * reads and stores this metadata in a way compatible with Netatalk 3
62 * which stores the metadata in an EA "org.netatalk.metadata". Cf
63 * source3/include/MacExtensions.h for a description of the binary
64 * blobs content.
66 * The "AFP_Resource" named stream may be arbitrarily large, thus it
67 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
68 * the only available filesystem where xattrs can be of any size and
69 * the OS supports using the file APIs for xattrs.
71 * The AFP_Resource stream is stored in an AppleDouble file prepending
72 * "._" to the filename. On Solaris with ZFS the stream is optionally
73 * stored in an EA "org.netatalk.resource".
76 * Extended Attributes
77 * ===================
79 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
80 * other protocols you may want to adjust the xattr names the VFS
81 * module vfs_streams_xattr uses for storing ADS's. This defaults to
82 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
83 * these module parameters:
85 * streams_xattr:prefix = user.
86 * streams_xattr:store_stream_type = false
89 * TODO
90 * ====
92 * - log diagnostic if any needed VFS module is not loaded
93 * (eg with lp_vfs_objects())
94 * - add tests
97 static int vfs_fruit_debug_level = DBGC_VFS;
99 static struct global_fruit_config {
100 bool nego_aapl; /* client negotiated AAPL */
102 } global_fruit_config;
104 #undef DBGC_CLASS
105 #define DBGC_CLASS vfs_fruit_debug_level
107 #define FRUIT_PARAM_TYPE_NAME "fruit"
108 #define ADOUBLE_NAME_PREFIX "._"
110 #define NETATALK_META_XATTR "org.netatalk.Metadata"
111 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
113 #if defined(HAVE_ATTROPEN)
114 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
115 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
116 #else
117 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
118 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
119 #endif
121 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
123 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
124 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
125 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
126 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
128 struct fruit_config_data {
129 enum fruit_rsrc rsrc;
130 enum fruit_meta meta;
131 enum fruit_locking locking;
132 enum fruit_encoding encoding;
133 bool use_aapl; /* config from smb.conf */
134 bool use_copyfile;
135 bool readdir_attr_enabled;
136 bool unix_info_enabled;
137 bool copyfile_enabled;
138 bool veto_appledouble;
139 bool posix_rename;
140 bool aapl_zero_file_id;
141 const char *model;
144 * Additional options, all enabled by default,
145 * possibly useful for analyzing performance. The associated
146 * operations with each of them may be expensive, so having
147 * the chance to disable them individually gives a chance
148 * tweaking the setup for the particular usecase.
150 bool readdir_attr_rsize;
151 bool readdir_attr_finder_info;
152 bool readdir_attr_max_access;
155 static const struct enum_list fruit_rsrc[] = {
156 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
157 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
158 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
159 { -1, NULL}
162 static const struct enum_list fruit_meta[] = {
163 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
164 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
165 { -1, NULL}
168 static const struct enum_list fruit_locking[] = {
169 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
170 {FRUIT_LOCKING_NONE, "none"},
171 { -1, NULL}
174 static const struct enum_list fruit_encoding[] = {
175 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
176 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
177 { -1, NULL}
180 /*****************************************************************************
181 * Defines, functions and data structures that deal with AppleDouble
182 *****************************************************************************/
185 * There are two AppleDouble blobs we deal with:
187 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
188 * metadata in an xattr
190 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
191 * ._ files
193 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
195 /* Version info */
196 #define AD_VERSION2 0x00020000
197 #define AD_VERSION AD_VERSION2
200 * AppleDouble entry IDs.
202 #define ADEID_DFORK 1
203 #define ADEID_RFORK 2
204 #define ADEID_NAME 3
205 #define ADEID_COMMENT 4
206 #define ADEID_ICONBW 5
207 #define ADEID_ICONCOL 6
208 #define ADEID_FILEI 7
209 #define ADEID_FILEDATESI 8
210 #define ADEID_FINDERI 9
211 #define ADEID_MACFILEI 10
212 #define ADEID_PRODOSFILEI 11
213 #define ADEID_MSDOSFILEI 12
214 #define ADEID_SHORTNAME 13
215 #define ADEID_AFPFILEI 14
216 #define ADEID_DID 15
218 /* Private Netatalk entries */
219 #define ADEID_PRIVDEV 16
220 #define ADEID_PRIVINO 17
221 #define ADEID_PRIVSYN 18
222 #define ADEID_PRIVID 19
223 #define ADEID_MAX (ADEID_PRIVID + 1)
226 * These are the real ids for the private entries,
227 * as stored in the adouble file
229 #define AD_DEV 0x80444556
230 #define AD_INO 0x80494E4F
231 #define AD_SYN 0x8053594E
232 #define AD_ID 0x8053567E
234 /* Number of actually used entries */
235 #define ADEID_NUM_XATTR 8
236 #define ADEID_NUM_DOT_UND 2
237 #define ADEID_NUM_RSRC_XATTR 1
239 /* AppleDouble magic */
240 #define AD_APPLESINGLE_MAGIC 0x00051600
241 #define AD_APPLEDOUBLE_MAGIC 0x00051607
242 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
244 /* Sizes of relevant entry bits */
245 #define ADEDLEN_MAGIC 4
246 #define ADEDLEN_VERSION 4
247 #define ADEDLEN_FILLER 16
248 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
249 #define ADEDLEN_NENTRIES 2
250 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
251 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
252 #define AD_ENTRY_LEN_EID 4
253 #define AD_ENTRY_LEN_OFF 4
254 #define AD_ENTRY_LEN_LEN 4
255 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
257 /* Field widths */
258 #define ADEDLEN_NAME 255
259 #define ADEDLEN_COMMENT 200
260 #define ADEDLEN_FILEI 16
261 #define ADEDLEN_FINDERI 32
262 #define ADEDLEN_FILEDATESI 16
263 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
264 #define ADEDLEN_AFPFILEI 4
265 #define ADEDLEN_MACFILEI 4
266 #define ADEDLEN_PRODOSFILEI 8
267 #define ADEDLEN_MSDOSFILEI 2
268 #define ADEDLEN_DID 4
269 #define ADEDLEN_PRIVDEV 8
270 #define ADEDLEN_PRIVINO 8
271 #define ADEDLEN_PRIVSYN 8
272 #define ADEDLEN_PRIVID 4
274 /* Offsets */
275 #define ADEDOFF_MAGIC 0
276 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
277 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
278 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
280 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
281 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
282 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
283 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
284 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
285 ADEDLEN_FILEDATESI)
286 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
287 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
288 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
289 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
291 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
292 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
293 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
295 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
296 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
297 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
298 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
299 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
300 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
302 #if AD_DATASZ_XATTR != 402
303 #error bad size for AD_DATASZ_XATTR
304 #endif
306 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
307 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
308 ADEDLEN_FINDERI)
309 #if AD_DATASZ_DOT_UND != 82
310 #error bad size for AD_DATASZ_DOT_UND
311 #endif
314 * Sharemode locks fcntl() offsets
316 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
317 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
318 #else
319 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
320 #endif
321 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
323 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
324 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
325 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
326 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
327 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
328 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
329 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
330 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
331 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
332 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
334 /* Time stuff we overload the bits a little */
335 #define AD_DATE_CREATE 0
336 #define AD_DATE_MODIFY 4
337 #define AD_DATE_BACKUP 8
338 #define AD_DATE_ACCESS 12
339 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
340 AD_DATE_BACKUP | AD_DATE_ACCESS)
341 #define AD_DATE_UNIX (1 << 10)
342 #define AD_DATE_START 0x80000000
343 #define AD_DATE_DELTA 946684800
344 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
345 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
347 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
348 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
349 #define AD_XATTR_HDR_SIZE 36
350 #define AD_XATTR_MAX_HDR_SIZE 65536
352 /* Accessor macros */
353 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
354 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
355 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
356 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
359 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
360 * representation as well as the on-disk format.
362 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
363 * the length of the FinderInfo entry is larger then 32 bytes. It is then
364 * preceeded with 2 bytes padding.
366 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
369 struct ad_xattr_header {
370 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
371 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
372 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
373 uint32_t adx_data_start; /* file offset to attribute data area */
374 uint32_t adx_data_length; /* length of attribute data area */
375 uint32_t adx_reserved[3];
376 uint16_t adx_flags;
377 uint16_t adx_num_attrs;
380 /* On-disk entries are aligned on 4 byte boundaries */
381 struct ad_xattr_entry {
382 uint32_t adx_offset; /* file offset to data */
383 uint32_t adx_length; /* size of attribute data */
384 uint16_t adx_flags;
385 uint8_t adx_namelen; /* included the NULL terminator */
386 char *adx_name; /* NULL-terminated UTF-8 name */
389 struct ad_entry {
390 size_t ade_off;
391 size_t ade_len;
394 struct adouble {
395 vfs_handle_struct *ad_handle;
396 int ad_fd;
397 bool ad_opened;
398 adouble_type_t ad_type;
399 uint32_t ad_magic;
400 uint32_t ad_version;
401 struct ad_entry ad_eid[ADEID_MAX];
402 char *ad_data;
403 struct ad_xattr_header adx_header;
404 struct ad_xattr_entry *adx_entries;
407 struct ad_entry_order {
408 uint32_t id, offset, len;
411 /* Netatalk AppleDouble metadata xattr */
412 static const
413 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
414 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
415 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
416 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
417 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
418 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
419 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
420 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
421 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
422 {0, 0, 0}
425 /* AppleDouble resource fork file (the ones prefixed by "._") */
426 static const
427 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
428 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
429 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
430 {0, 0, 0}
434 * Fake AppleDouble entry oder for resource fork xattr. The xattr
435 * isn't an AppleDouble file, it simply contains the resource data,
436 * but in order to be able to use some API calls like ad_getentryoff()
437 * we build a fake/helper struct adouble with this entry order struct.
439 static const
440 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
441 {ADEID_RFORK, 0, 0},
442 {0, 0, 0}
445 /* Conversion from enumerated id to on-disk AppleDouble id */
446 #define AD_EID_DISK(a) (set_eid[a])
447 static const uint32_t set_eid[] = {
448 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
449 AD_DEV, AD_INO, AD_SYN, AD_ID
452 struct fio {
453 /* tcon config handle */
454 struct fruit_config_data *config;
456 /* Denote stream type, meta or rsrc */
457 adouble_type_t type;
461 * Forward declarations
463 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
464 adouble_type_t type);
465 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
466 static int ad_fset(struct adouble *ad, files_struct *fsp);
467 static int adouble_path(TALLOC_CTX *ctx,
468 const struct smb_filename *smb_fname__in,
469 struct smb_filename **ppsmb_fname_out);
472 * Return a pointer to an AppleDouble entry
474 * Returns NULL if the entry is not present
476 static char *ad_get_entry(const struct adouble *ad, int eid)
478 off_t off = ad_getentryoff(ad, eid);
479 size_t len = ad_getentrylen(ad, eid);
481 if (off == 0 || len == 0) {
482 return NULL;
485 return ad->ad_data + off;
489 * Get a date
491 static int ad_getdate(const struct adouble *ad,
492 unsigned int dateoff,
493 uint32_t *date)
495 bool xlate = (dateoff & AD_DATE_UNIX);
496 char *p = NULL;
498 dateoff &= AD_DATE_MASK;
499 p = ad_get_entry(ad, ADEID_FILEDATESI);
500 if (p == NULL) {
501 return -1;
504 if (dateoff > AD_DATE_ACCESS) {
505 return -1;
508 memcpy(date, p + dateoff, sizeof(uint32_t));
510 if (xlate) {
511 *date = AD_DATE_TO_UNIX(*date);
513 return 0;
517 * Set a date
519 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
521 bool xlate = (dateoff & AD_DATE_UNIX);
522 char *p = NULL;
524 p = ad_get_entry(ad, ADEID_FILEDATESI);
525 if (p == NULL) {
526 return -1;
529 dateoff &= AD_DATE_MASK;
530 if (xlate) {
531 date = AD_DATE_FROM_UNIX(date);
534 if (dateoff > AD_DATE_ACCESS) {
535 return -1;
538 memcpy(p + dateoff, &date, sizeof(date));
540 return 0;
545 * Map on-disk AppleDouble id to enumerated id
547 static uint32_t get_eid(uint32_t eid)
549 if (eid <= 15) {
550 return eid;
553 switch (eid) {
554 case AD_DEV:
555 return ADEID_PRIVDEV;
556 case AD_INO:
557 return ADEID_PRIVINO;
558 case AD_SYN:
559 return ADEID_PRIVSYN;
560 case AD_ID:
561 return ADEID_PRIVID;
562 default:
563 break;
566 return 0;
570 * Pack AppleDouble structure into data buffer
572 static bool ad_pack(struct adouble *ad)
574 uint32_t eid;
575 uint16_t nent;
576 uint32_t bufsize;
577 uint32_t offset = 0;
579 bufsize = talloc_get_size(ad->ad_data);
580 if (bufsize < AD_DATASZ_DOT_UND) {
581 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
582 return false;
585 if (offset + ADEDLEN_MAGIC < offset ||
586 offset + ADEDLEN_MAGIC >= bufsize) {
587 return false;
589 RSIVAL(ad->ad_data, offset, ad->ad_magic);
590 offset += ADEDLEN_MAGIC;
592 if (offset + ADEDLEN_VERSION < offset ||
593 offset + ADEDLEN_VERSION >= bufsize) {
594 return false;
596 RSIVAL(ad->ad_data, offset, ad->ad_version);
597 offset += ADEDLEN_VERSION;
599 if (offset + ADEDLEN_FILLER < offset ||
600 offset + ADEDLEN_FILLER >= bufsize) {
601 return false;
603 if (ad->ad_type == ADOUBLE_RSRC) {
604 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
606 offset += ADEDLEN_FILLER;
608 if (offset + ADEDLEN_NENTRIES < offset ||
609 offset + ADEDLEN_NENTRIES >= bufsize) {
610 return false;
612 offset += ADEDLEN_NENTRIES;
614 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
615 if (ad->ad_eid[eid].ade_off == 0) {
617 * ade_off is also used as indicator whether a
618 * specific entry is used or not
620 continue;
623 if (offset + AD_ENTRY_LEN_EID < offset ||
624 offset + AD_ENTRY_LEN_EID >= bufsize) {
625 return false;
627 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
628 offset += AD_ENTRY_LEN_EID;
630 if (offset + AD_ENTRY_LEN_OFF < offset ||
631 offset + AD_ENTRY_LEN_OFF >= bufsize) {
632 return false;
634 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
635 offset += AD_ENTRY_LEN_OFF;
637 if (offset + AD_ENTRY_LEN_LEN < offset ||
638 offset + AD_ENTRY_LEN_LEN >= bufsize) {
639 return false;
641 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
642 offset += AD_ENTRY_LEN_LEN;
644 nent++;
647 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
648 return false;
650 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
652 return true;
656 * Unpack an AppleDouble blob into a struct adoble
658 static bool ad_unpack(struct adouble *ad, const size_t nentries,
659 size_t filesize)
661 size_t bufsize = talloc_get_size(ad->ad_data);
662 size_t adentries, i;
663 uint32_t eid, len, off;
666 * The size of the buffer ad->ad_data is checked when read, so
667 * we wouldn't have to check our own offsets, a few extra
668 * checks won't hurt though. We have to check the offsets we
669 * read from the buffer anyway.
672 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
673 DEBUG(1, ("bad size\n"));
674 return false;
677 ad->ad_magic = RIVAL(ad->ad_data, 0);
678 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
679 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
680 DEBUG(1, ("wrong magic or version\n"));
681 return false;
684 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
685 if (adentries != nentries) {
686 DEBUG(1, ("invalid number of entries: %zu\n",
687 adentries));
688 return false;
691 /* now, read in the entry bits */
692 for (i = 0; i < adentries; i++) {
693 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
694 eid = get_eid(eid);
695 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
696 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
698 if (!eid || eid > ADEID_MAX) {
699 DEBUG(1, ("bogus eid %d\n", eid));
700 return false;
704 * All entries other than the resource fork are
705 * expected to be read into the ad_data buffer, so
706 * ensure the specified offset is within that bound
708 if ((off > bufsize) && (eid != ADEID_RFORK)) {
709 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
710 eid, off, len));
711 return false;
715 * All entries besides FinderInfo and resource fork
716 * must fit into the buffer. FinderInfo is special as
717 * it may be larger then the default 32 bytes (if it
718 * contains marshalled xattrs), but we will fixup that
719 * in ad_convert(). And the resource fork is never
720 * accessed directly by the ad_data buf (also see
721 * comment above) anyway.
723 if ((eid != ADEID_RFORK) &&
724 (eid != ADEID_FINDERI) &&
725 ((off + len) > bufsize)) {
726 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
727 eid, off, len));
728 return false;
732 * That would be obviously broken
734 if (off > filesize) {
735 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
736 eid, off, len));
737 return false;
741 * Check for any entry that has its end beyond the
742 * filesize.
744 if (off + len < off) {
745 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
746 ", len: %" PRIu32 "\n",
747 eid, off, len));
748 return false;
751 if (off + len > filesize) {
753 * If this is the resource fork entry, we fix
754 * up the length, for any other entry we bail
755 * out.
757 if (eid != ADEID_RFORK) {
758 DEBUG(1, ("bogus eid %d: off: %" PRIu32
759 ", len: %" PRIu32 "\n",
760 eid, off, len));
761 return false;
765 * Fixup the resource fork entry by limiting
766 * the size to entryoffset - filesize.
768 len = filesize - off;
769 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
770 ", len: %" PRIu32 "\n", off, len));
773 ad->ad_eid[eid].ade_off = off;
774 ad->ad_eid[eid].ade_len = len;
777 return true;
781 * Convert from Apple's ._ file to Netatalk
783 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
784 * bytes containing packed xattrs. Netatalk can't deal with that, so
785 * we simply discard the packed xattrs.
787 * @return -1 in case an error occurred, 0 if no conversion was done, 1
788 * otherwise
790 static int ad_convert(struct adouble *ad, int fd)
792 int rc = 0;
793 char *map = MAP_FAILED;
794 size_t origlen;
796 origlen = ad_getentryoff(ad, ADEID_RFORK) +
797 ad_getentrylen(ad, ADEID_RFORK);
799 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
800 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
801 if (map == MAP_FAILED) {
802 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
803 rc = -1;
804 goto exit;
807 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
808 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
809 map + ad_getentryoff(ad, ADEID_RFORK),
810 ad_getentrylen(ad, ADEID_RFORK));
813 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
814 ad_setentryoff(ad, ADEID_RFORK,
815 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
818 * FIXME: direct ftruncate(), but we don't have a fsp for the
819 * VFS call
821 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
822 + ad_getentrylen(ad, ADEID_RFORK));
824 exit:
825 if (map != MAP_FAILED) {
826 munmap(map, origlen);
828 return rc;
832 * Read and parse Netatalk AppleDouble metadata xattr
834 static ssize_t ad_read_meta(struct adouble *ad,
835 const struct smb_filename *smb_fname)
837 int rc = 0;
838 ssize_t ealen;
839 bool ok;
841 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
843 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
844 AFPINFO_EA_NETATALK, ad->ad_data,
845 AD_DATASZ_XATTR);
846 if (ealen == -1) {
847 switch (errno) {
848 case ENOATTR:
849 case ENOENT:
850 if (errno == ENOATTR) {
851 errno = ENOENT;
853 rc = -1;
854 goto exit;
855 default:
856 DEBUG(2, ("error reading meta xattr: %s\n",
857 strerror(errno)));
858 rc = -1;
859 goto exit;
862 if (ealen != AD_DATASZ_XATTR) {
863 DEBUG(2, ("bad size %zd\n", ealen));
864 errno = EINVAL;
865 rc = -1;
866 goto exit;
869 /* Now parse entries */
870 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
871 if (!ok) {
872 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
873 errno = EINVAL;
874 rc = -1;
875 goto exit;
878 if (!ad_getentryoff(ad, ADEID_FINDERI)
879 || !ad_getentryoff(ad, ADEID_COMMENT)
880 || !ad_getentryoff(ad, ADEID_FILEDATESI)
881 || !ad_getentryoff(ad, ADEID_AFPFILEI)
882 || !ad_getentryoff(ad, ADEID_PRIVDEV)
883 || !ad_getentryoff(ad, ADEID_PRIVINO)
884 || !ad_getentryoff(ad, ADEID_PRIVSYN)
885 || !ad_getentryoff(ad, ADEID_PRIVID)) {
886 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
887 errno = EINVAL;
888 rc = -1;
889 goto exit;
892 exit:
893 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
894 smb_fname->base_name, rc));
896 if (rc != 0) {
897 ealen = -1;
898 if (errno == EINVAL) {
899 become_root();
900 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
901 unbecome_root();
902 errno = ENOENT;
905 return ealen;
908 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
909 int flags,
910 mode_t mode)
912 #ifdef HAVE_ATTROPEN
913 /* FIXME: direct Solaris xattr syscall */
914 return attropen(smb_fname->base_name,
915 AFPRESOURCE_EA_NETATALK, flags, mode);
916 #else
917 errno = ENOSYS;
918 return -1;
919 #endif
922 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
923 int flags,
924 mode_t mode)
926 int ret;
927 int fd;
928 struct smb_filename *adp_smb_fname = NULL;
930 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
931 if (ret != 0) {
932 return -1;
935 fd = open(adp_smb_fname->base_name, flags, mode);
936 TALLOC_FREE(adp_smb_fname);
938 return fd;
941 static int ad_open_rsrc(vfs_handle_struct *handle,
942 const struct smb_filename *smb_fname,
943 int flags,
944 mode_t mode)
946 struct fruit_config_data *config = NULL;
947 int fd;
949 SMB_VFS_HANDLE_GET_DATA(handle, config,
950 struct fruit_config_data, return -1);
952 if (config->rsrc == FRUIT_RSRC_XATTR) {
953 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
954 } else {
955 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
958 return fd;
962 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
963 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
964 * for file IO on the ._ file.
966 static int ad_open(vfs_handle_struct *handle,
967 struct adouble *ad,
968 files_struct *fsp,
969 const struct smb_filename *smb_fname,
970 int flags,
971 mode_t mode)
973 int fd;
975 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
976 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
978 if (ad->ad_type == ADOUBLE_META) {
979 return 0;
982 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
983 ad->ad_fd = fsp->fh->fd;
984 ad->ad_opened = false;
985 return 0;
988 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
989 if (fd == -1) {
990 return -1;
992 ad->ad_opened = true;
993 ad->ad_fd = fd;
995 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
996 smb_fname->base_name,
997 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
999 return 0;
1002 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1004 int ret;
1005 SMB_STRUCT_STAT st;
1007 /* FIXME: direct sys_fstat(), don't have an fsp */
1008 ret = sys_fstat(ad->ad_fd, &st,
1009 lp_fake_directory_create_times(
1010 SNUM(ad->ad_handle->conn)));
1011 if (ret != 0) {
1012 return -1;
1015 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1016 return st.st_ex_size;
1019 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1020 const struct smb_filename *smb_fname)
1022 struct adouble *meta_ad = NULL;
1023 SMB_STRUCT_STAT sbuf;
1024 char *p_ad = NULL;
1025 char *p_meta_ad = NULL;
1026 ssize_t len;
1027 size_t size;
1028 int ret;
1029 bool ok;
1031 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1032 SNUM(ad->ad_handle->conn)));
1033 if (ret != 0) {
1034 return -1;
1038 * AppleDouble file header content and size, two cases:
1040 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1041 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1043 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1045 size = sbuf.st_ex_size;
1046 if (size > talloc_array_length(ad->ad_data)) {
1047 if (size > AD_XATTR_MAX_HDR_SIZE) {
1048 size = AD_XATTR_MAX_HDR_SIZE;
1050 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1051 if (p_ad == NULL) {
1052 return -1;
1054 ad->ad_data = p_ad;
1057 len = sys_pread(ad->ad_fd, ad->ad_data,
1058 talloc_array_length(ad->ad_data), 0);
1059 if (len != talloc_array_length(ad->ad_data)) {
1060 DBG_NOTICE("%s %s: bad size: %zd\n",
1061 smb_fname->base_name, strerror(errno), len);
1062 return -1;
1065 /* Now parse entries */
1066 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1067 if (!ok) {
1068 DBG_ERR("invalid AppleDouble resource %s\n",
1069 smb_fname->base_name);
1070 errno = EINVAL;
1071 return -1;
1074 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1075 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1076 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1077 DBG_ERR("invalid AppleDouble resource %s\n",
1078 smb_fname->base_name);
1079 errno = EINVAL;
1080 return -1;
1083 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1084 return len;
1088 * Try to fixup AppleDouble files created by OS X with xattrs
1089 * appended to the ADEID_FINDERI entry. We simply remove the
1090 * xattrs blob, this means any fancy xattr that was stored
1091 * there is lost.
1094 ret = ad_convert(ad, ad->ad_fd);
1095 if (ret != 0) {
1096 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1097 return len;
1100 ok = ad_pack(ad);
1101 if (!ok) {
1102 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1103 return -1;
1106 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1107 if (len != AD_DATASZ_DOT_UND) {
1108 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1109 return -1;
1112 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1113 if (meta_ad == NULL) {
1114 return -1;
1117 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1118 if (p_ad == NULL) {
1119 TALLOC_FREE(meta_ad);
1120 return -1;
1122 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1123 if (p_meta_ad == NULL) {
1124 TALLOC_FREE(meta_ad);
1125 return -1;
1128 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1130 ret = ad_set(meta_ad, smb_fname);
1131 TALLOC_FREE(meta_ad);
1132 if (ret != 0) {
1133 return -1;
1136 return len;
1140 * Read and parse resource fork, either ._ AppleDouble file or xattr
1142 static ssize_t ad_read_rsrc(struct adouble *ad,
1143 const struct smb_filename *smb_fname)
1145 struct fruit_config_data *config = NULL;
1146 ssize_t len;
1148 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1149 struct fruit_config_data, return -1);
1151 if (config->rsrc == FRUIT_RSRC_XATTR) {
1152 len = ad_read_rsrc_xattr(ad);
1153 } else {
1154 len = ad_read_rsrc_adouble(ad, smb_fname);
1157 return len;
1161 * Read and unpack an AppleDouble metadata xattr or resource
1163 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1165 switch (ad->ad_type) {
1166 case ADOUBLE_META:
1167 return ad_read_meta(ad, smb_fname);
1168 case ADOUBLE_RSRC:
1169 return ad_read_rsrc(ad, smb_fname);
1170 default:
1171 return -1;
1175 static int adouble_destructor(struct adouble *ad)
1177 if ((ad->ad_fd != -1) && ad->ad_opened) {
1178 close(ad->ad_fd);
1179 ad->ad_fd = -1;
1181 return 0;
1185 * Allocate a struct adouble without initialiing it
1187 * The struct is either hang of the fsp extension context or if fsp is
1188 * NULL from ctx.
1190 * @param[in] ctx talloc context
1191 * @param[in] handle vfs handle
1192 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1194 * @return adouble handle
1196 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1197 adouble_type_t type)
1199 int rc = 0;
1200 size_t adsize = 0;
1201 struct adouble *ad;
1202 struct fruit_config_data *config;
1204 SMB_VFS_HANDLE_GET_DATA(handle, config,
1205 struct fruit_config_data, return NULL);
1207 switch (type) {
1208 case ADOUBLE_META:
1209 adsize = AD_DATASZ_XATTR;
1210 break;
1211 case ADOUBLE_RSRC:
1212 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1213 adsize = AD_DATASZ_DOT_UND;
1215 break;
1216 default:
1217 return NULL;
1220 ad = talloc_zero(ctx, struct adouble);
1221 if (ad == NULL) {
1222 rc = -1;
1223 goto exit;
1226 if (adsize) {
1227 ad->ad_data = talloc_zero_array(ad, char, adsize);
1228 if (ad->ad_data == NULL) {
1229 rc = -1;
1230 goto exit;
1234 ad->ad_handle = handle;
1235 ad->ad_type = type;
1236 ad->ad_magic = AD_MAGIC;
1237 ad->ad_version = AD_VERSION;
1238 ad->ad_fd = -1;
1240 talloc_set_destructor(ad, adouble_destructor);
1242 exit:
1243 if (rc != 0) {
1244 TALLOC_FREE(ad);
1246 return ad;
1250 * Allocate and initialize a new struct adouble
1252 * @param[in] ctx talloc context
1253 * @param[in] handle vfs handle
1254 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1256 * @return adouble handle, initialized
1258 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1259 adouble_type_t type)
1261 int rc = 0;
1262 const struct ad_entry_order *eid;
1263 struct adouble *ad = NULL;
1264 struct fruit_config_data *config;
1265 time_t t = time(NULL);
1267 SMB_VFS_HANDLE_GET_DATA(handle, config,
1268 struct fruit_config_data, return NULL);
1270 switch (type) {
1271 case ADOUBLE_META:
1272 eid = entry_order_meta_xattr;
1273 break;
1274 case ADOUBLE_RSRC:
1275 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1276 eid = entry_order_dot_und;
1277 } else {
1278 eid = entry_order_rsrc_xattr;
1280 break;
1281 default:
1282 return NULL;
1285 ad = ad_alloc(ctx, handle, type);
1286 if (ad == NULL) {
1287 return NULL;
1290 while (eid->id) {
1291 ad->ad_eid[eid->id].ade_off = eid->offset;
1292 ad->ad_eid[eid->id].ade_len = eid->len;
1293 eid++;
1296 /* put something sane in the date fields */
1297 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1298 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1299 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1300 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1302 if (rc != 0) {
1303 TALLOC_FREE(ad);
1305 return ad;
1308 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1309 vfs_handle_struct *handle,
1310 files_struct *fsp,
1311 const struct smb_filename *smb_fname,
1312 adouble_type_t type)
1314 int rc = 0;
1315 ssize_t len;
1316 struct adouble *ad = NULL;
1317 int mode;
1319 if (fsp != NULL) {
1320 smb_fname = fsp->base_fsp->fsp_name;
1323 DEBUG(10, ("ad_get(%s) called for %s\n",
1324 type == ADOUBLE_META ? "meta" : "rsrc",
1325 smb_fname->base_name));
1327 ad = ad_alloc(ctx, handle, type);
1328 if (ad == NULL) {
1329 rc = -1;
1330 goto exit;
1333 /* Try rw first so we can use the fd in ad_convert() */
1334 mode = O_RDWR;
1336 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1337 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1338 mode = O_RDONLY;
1339 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1341 if (rc == -1) {
1342 DBG_DEBUG("ad_open [%s] error [%s]\n",
1343 smb_fname->base_name, strerror(errno));
1344 goto exit;
1348 len = ad_read(ad, smb_fname);
1349 if (len == -1) {
1350 DEBUG(10, ("error reading AppleDouble for %s\n",
1351 smb_fname->base_name));
1352 rc = -1;
1353 goto exit;
1356 exit:
1357 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1358 type == ADOUBLE_META ? "meta" : "rsrc",
1359 smb_fname->base_name, rc));
1361 if (rc != 0) {
1362 TALLOC_FREE(ad);
1364 return ad;
1368 * Return AppleDouble data for a file
1370 * @param[in] ctx talloc context
1371 * @param[in] handle vfs handle
1372 * @param[in] smb_fname pathname to file or directory
1373 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1375 * @return talloced struct adouble or NULL on error
1377 static struct adouble *ad_get(TALLOC_CTX *ctx,
1378 vfs_handle_struct *handle,
1379 const struct smb_filename *smb_fname,
1380 adouble_type_t type)
1382 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1386 * Return AppleDouble data for a file
1388 * @param[in] ctx talloc context
1389 * @param[in] handle vfs handle
1390 * @param[in] fsp fsp to use for IO
1391 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1393 * @return talloced struct adouble or NULL on error
1395 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1396 files_struct *fsp, adouble_type_t type)
1398 return ad_get_internal(ctx, handle, fsp, NULL, type);
1402 * Set AppleDouble metadata on a file or directory
1404 * @param[in] ad adouble handle
1406 * @param[in] smb_fname pathname to file or directory
1408 * @return status code, 0 means success
1410 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1412 bool ok;
1413 int ret;
1415 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1417 if (ad->ad_type != ADOUBLE_META) {
1418 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1419 smb_fname->base_name);
1420 return -1;
1423 ok = ad_pack(ad);
1424 if (!ok) {
1425 return -1;
1428 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1429 smb_fname,
1430 AFPINFO_EA_NETATALK,
1431 ad->ad_data,
1432 AD_DATASZ_XATTR, 0);
1434 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1436 return ret;
1440 * Set AppleDouble metadata on a file or directory
1442 * @param[in] ad adouble handle
1443 * @param[in] fsp file handle
1445 * @return status code, 0 means success
1447 static int ad_fset(struct adouble *ad, files_struct *fsp)
1449 int rc = -1;
1450 ssize_t len;
1451 bool ok;
1453 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1455 if ((fsp == NULL)
1456 || (fsp->fh == NULL)
1457 || (fsp->fh->fd == -1))
1459 smb_panic("bad fsp");
1462 ok = ad_pack(ad);
1463 if (!ok) {
1464 return -1;
1467 switch (ad->ad_type) {
1468 case ADOUBLE_META:
1469 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1470 fsp->fsp_name,
1471 AFPINFO_EA_NETATALK,
1472 ad->ad_data,
1473 AD_DATASZ_XATTR, 0);
1474 break;
1476 case ADOUBLE_RSRC:
1477 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1478 fsp,
1479 ad->ad_data,
1480 AD_DATASZ_DOT_UND,
1482 if (len != AD_DATASZ_DOT_UND) {
1483 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1484 return -1;
1486 rc = 0;
1487 break;
1489 default:
1490 return -1;
1493 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1495 return rc;
1498 /*****************************************************************************
1499 * Helper functions
1500 *****************************************************************************/
1502 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1504 if (strncasecmp_m(smb_fname->stream_name,
1505 AFPINFO_STREAM_NAME,
1506 strlen(AFPINFO_STREAM_NAME)) == 0) {
1507 return true;
1509 return false;
1512 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1514 if (strncasecmp_m(smb_fname->stream_name,
1515 AFPRESOURCE_STREAM_NAME,
1516 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1517 return true;
1519 return false;
1523 * Test whether stream is an Apple stream, not used atm
1525 #if 0
1526 static bool is_apple_stream(const struct smb_filename *smb_fname)
1528 if (is_afpinfo_stream(smb_fname)) {
1529 return true;
1531 if (is_afpresource_stream(smb_fname)) {
1532 return true;
1534 return false;
1536 #endif
1539 * Initialize config struct from our smb.conf config parameters
1541 static int init_fruit_config(vfs_handle_struct *handle)
1543 struct fruit_config_data *config;
1544 int enumval;
1546 config = talloc_zero(handle->conn, struct fruit_config_data);
1547 if (!config) {
1548 DEBUG(1, ("talloc_zero() failed\n"));
1549 errno = ENOMEM;
1550 return -1;
1554 * Versions up to Samba 4.5.x had a spelling bug in the
1555 * fruit:resource option calling lp_parm_enum with
1556 * "res*s*ource" (ie two s).
1558 * In Samba 4.6 we accept both the wrong and the correct
1559 * spelling, in Samba 4.7 the bad spelling will be removed.
1561 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1562 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1563 if (enumval == -1) {
1564 DEBUG(1, ("value for %s: resource type unknown\n",
1565 FRUIT_PARAM_TYPE_NAME));
1566 return -1;
1568 config->rsrc = (enum fruit_rsrc)enumval;
1570 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1571 "resource", fruit_rsrc, enumval);
1572 if (enumval == -1) {
1573 DEBUG(1, ("value for %s: resource type unknown\n",
1574 FRUIT_PARAM_TYPE_NAME));
1575 return -1;
1577 config->rsrc = (enum fruit_rsrc)enumval;
1579 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1580 "metadata", fruit_meta, FRUIT_META_NETATALK);
1581 if (enumval == -1) {
1582 DEBUG(1, ("value for %s: metadata type unknown\n",
1583 FRUIT_PARAM_TYPE_NAME));
1584 return -1;
1586 config->meta = (enum fruit_meta)enumval;
1588 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1589 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1590 if (enumval == -1) {
1591 DEBUG(1, ("value for %s: locking type unknown\n",
1592 FRUIT_PARAM_TYPE_NAME));
1593 return -1;
1595 config->locking = (enum fruit_locking)enumval;
1597 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1598 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1599 if (enumval == -1) {
1600 DEBUG(1, ("value for %s: encoding type unknown\n",
1601 FRUIT_PARAM_TYPE_NAME));
1602 return -1;
1604 config->encoding = (enum fruit_encoding)enumval;
1606 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1607 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1608 FRUIT_PARAM_TYPE_NAME,
1609 "veto_appledouble",
1610 true);
1613 config->use_aapl = lp_parm_bool(
1614 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1616 config->unix_info_enabled = lp_parm_bool(
1617 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1619 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1620 "copyfile", false);
1622 config->posix_rename = lp_parm_bool(
1623 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1625 config->aapl_zero_file_id =
1626 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1628 config->readdir_attr_rsize = lp_parm_bool(
1629 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1631 config->readdir_attr_finder_info = lp_parm_bool(
1632 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1634 config->readdir_attr_max_access = lp_parm_bool(
1635 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1637 config->model = lp_parm_const_string(
1638 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1640 SMB_VFS_HANDLE_SET_DATA(handle, config,
1641 NULL, struct fruit_config_data,
1642 return -1);
1644 return 0;
1648 * Prepend "._" to a basename
1649 * Return a new struct smb_filename with stream_name == NULL.
1651 static int adouble_path(TALLOC_CTX *ctx,
1652 const struct smb_filename *smb_fname_in,
1653 struct smb_filename **pp_smb_fname_out)
1655 char *parent;
1656 const char *base;
1657 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1658 smb_fname_in);
1660 if (smb_fname == NULL) {
1661 return -1;
1664 /* We need streamname to be NULL */
1665 TALLOC_FREE(smb_fname->stream_name);
1667 /* And we're replacing base_name. */
1668 TALLOC_FREE(smb_fname->base_name);
1670 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
1671 &parent, &base)) {
1672 TALLOC_FREE(smb_fname);
1673 return -1;
1676 smb_fname->base_name = talloc_asprintf(smb_fname,
1677 "%s/._%s", parent, base);
1678 if (smb_fname->base_name == NULL) {
1679 TALLOC_FREE(smb_fname);
1680 return -1;
1683 *pp_smb_fname_out = smb_fname;
1685 return 0;
1689 * Allocate and initialize an AfpInfo struct
1691 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1693 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1694 if (ai == NULL) {
1695 return NULL;
1697 ai->afpi_Signature = AFP_Signature;
1698 ai->afpi_Version = AFP_Version;
1699 ai->afpi_BackupTime = AD_DATE_START;
1700 return ai;
1704 * Pack an AfpInfo struct into a buffer
1706 * Buffer size must be at least AFP_INFO_SIZE
1707 * Returns size of packed buffer
1709 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1711 memset(buf, 0, AFP_INFO_SIZE);
1713 RSIVAL(buf, 0, ai->afpi_Signature);
1714 RSIVAL(buf, 4, ai->afpi_Version);
1715 RSIVAL(buf, 12, ai->afpi_BackupTime);
1716 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1718 return AFP_INFO_SIZE;
1722 * Unpack a buffer into a AfpInfo structure
1724 * Buffer size must be at least AFP_INFO_SIZE
1725 * Returns allocated AfpInfo struct
1727 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1729 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1730 if (ai == NULL) {
1731 return NULL;
1734 ai->afpi_Signature = RIVAL(data, 0);
1735 ai->afpi_Version = RIVAL(data, 4);
1736 ai->afpi_BackupTime = RIVAL(data, 12);
1737 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1738 sizeof(ai->afpi_FinderInfo));
1740 if (ai->afpi_Signature != AFP_Signature
1741 || ai->afpi_Version != AFP_Version) {
1742 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1743 TALLOC_FREE(ai);
1746 return ai;
1750 * Fake an inode number from the md5 hash of the (xattr) name
1752 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1754 MD5_CTX ctx;
1755 unsigned char hash[16];
1756 SMB_INO_T result;
1757 char *upper_sname;
1759 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1760 SMB_ASSERT(upper_sname != NULL);
1762 MD5Init(&ctx);
1763 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1764 sizeof(sbuf->st_ex_dev));
1765 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1766 sizeof(sbuf->st_ex_ino));
1767 MD5Update(&ctx, (unsigned char *)upper_sname,
1768 talloc_get_size(upper_sname)-1);
1769 MD5Final(hash, &ctx);
1771 TALLOC_FREE(upper_sname);
1773 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1774 memcpy(&result, hash, sizeof(result));
1776 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1777 sname, (unsigned long long)result));
1779 return result;
1782 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1783 struct stream_struct **streams,
1784 const char *name, off_t size,
1785 off_t alloc_size)
1787 struct stream_struct *tmp;
1789 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1790 (*num_streams)+1);
1791 if (tmp == NULL) {
1792 return false;
1795 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1796 if (tmp[*num_streams].name == NULL) {
1797 return false;
1800 tmp[*num_streams].size = size;
1801 tmp[*num_streams].alloc_size = alloc_size;
1803 *streams = tmp;
1804 *num_streams += 1;
1805 return true;
1808 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
1809 struct stream_struct **streams)
1811 struct stream_struct *tmp = *streams;
1812 unsigned int i;
1814 if (*num_streams == 0) {
1815 return true;
1818 for (i = 0; i < *num_streams; i++) {
1819 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
1820 break;
1824 if (i == *num_streams) {
1825 return true;
1828 if (tmp[i].size > 0) {
1829 return true;
1832 TALLOC_FREE(tmp[i].name);
1833 if (*num_streams - 1 > i) {
1834 memmove(&tmp[i], &tmp[i+1],
1835 (*num_streams - i - 1) * sizeof(struct stream_struct));
1838 *num_streams -= 1;
1839 return true;
1842 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1843 struct stream_struct **streams,
1844 const char *name)
1846 struct stream_struct *tmp = *streams;
1847 unsigned int i;
1849 if (*num_streams == 0) {
1850 return true;
1853 for (i = 0; i < *num_streams; i++) {
1854 if (strequal_m(tmp[i].name, name)) {
1855 break;
1859 if (i == *num_streams) {
1860 return true;
1863 TALLOC_FREE(tmp[i].name);
1864 if (*num_streams - 1 > i) {
1865 memmove(&tmp[i], &tmp[i+1],
1866 (*num_streams - i - 1) * sizeof(struct stream_struct));
1869 *num_streams -= 1;
1870 return true;
1873 static bool ad_empty_finderinfo(const struct adouble *ad)
1875 int cmp;
1876 char emptybuf[ADEDLEN_FINDERI] = {0};
1877 char *fi = NULL;
1879 fi = ad_get_entry(ad, ADEID_FINDERI);
1880 if (fi == NULL) {
1881 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
1882 return false;
1885 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
1886 return (cmp == 0);
1889 static bool ai_empty_finderinfo(const AfpInfo *ai)
1891 int cmp;
1892 char emptybuf[ADEDLEN_FINDERI] = {0};
1894 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
1895 return (cmp == 0);
1899 * Update btime with btime from Netatalk
1901 static void update_btime(vfs_handle_struct *handle,
1902 struct smb_filename *smb_fname)
1904 uint32_t t;
1905 struct timespec creation_time = {0};
1906 struct adouble *ad;
1907 struct fruit_config_data *config = NULL;
1909 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1910 return);
1912 switch (config->meta) {
1913 case FRUIT_META_STREAM:
1914 return;
1915 case FRUIT_META_NETATALK:
1916 /* Handled below */
1917 break;
1918 default:
1919 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
1920 return;
1923 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
1924 if (ad == NULL) {
1925 return;
1927 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1928 TALLOC_FREE(ad);
1929 return;
1931 TALLOC_FREE(ad);
1933 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1934 update_stat_ex_create_time(&smb_fname->st, creation_time);
1936 return;
1940 * Map an access mask to a Netatalk single byte byte range lock
1942 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1943 uint32_t access_mask)
1945 off_t offset;
1947 switch (access_mask) {
1948 case FILE_READ_DATA:
1949 offset = AD_FILELOCK_OPEN_RD;
1950 break;
1952 case FILE_WRITE_DATA:
1953 case FILE_APPEND_DATA:
1954 offset = AD_FILELOCK_OPEN_WR;
1955 break;
1957 default:
1958 offset = AD_FILELOCK_OPEN_NONE;
1959 break;
1962 if (fork_type == APPLE_FORK_RSRC) {
1963 if (offset == AD_FILELOCK_OPEN_NONE) {
1964 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1965 } else {
1966 offset += 2;
1970 return offset;
1974 * Map a deny mode to a Netatalk brl
1976 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1977 uint32_t deny_mode)
1979 off_t offset;
1981 switch (deny_mode) {
1982 case DENY_READ:
1983 offset = AD_FILELOCK_DENY_RD;
1984 break;
1986 case DENY_WRITE:
1987 offset = AD_FILELOCK_DENY_WR;
1988 break;
1990 default:
1991 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1994 if (fork_type == APPLE_FORK_RSRC) {
1995 offset += 2;
1998 return offset;
2002 * Call fcntl() with an exclusive F_GETLK request in order to
2003 * determine if there's an exisiting shared lock
2005 * @return true if the requested lock was found or any error occurred
2006 * false if the lock was not found
2008 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2010 bool result;
2011 off_t offset = in_offset;
2012 off_t len = 1;
2013 int type = F_WRLCK;
2014 pid_t pid;
2016 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2017 if (result == false) {
2018 return true;
2021 if (type != F_UNLCK) {
2022 return true;
2025 return false;
2028 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2029 files_struct *fsp,
2030 uint32_t access_mask,
2031 uint32_t deny_mode)
2033 NTSTATUS status = NT_STATUS_OK;
2034 struct byte_range_lock *br_lck = NULL;
2035 bool open_for_reading, open_for_writing, deny_read, deny_write;
2036 off_t off;
2037 bool have_read = false;
2038 int flags;
2040 /* FIXME: hardcoded data fork, add resource fork */
2041 enum apple_fork fork_type = APPLE_FORK_DATA;
2043 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2044 fsp_str_dbg(fsp),
2045 access_mask & FILE_READ_DATA ? "READ" :"-",
2046 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2047 deny_mode & DENY_READ ? "DENY_READ" : "-",
2048 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2050 if (fsp->fh->fd == -1) {
2051 return NT_STATUS_OK;
2054 flags = fcntl(fsp->fh->fd, F_GETFL);
2055 if (flags == -1) {
2056 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2057 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2058 return map_nt_error_from_unix(errno);
2061 if (flags & (O_RDONLY|O_RDWR)) {
2063 * Applying fcntl read locks requires an fd opened for
2064 * reading. This means we won't be applying locks for
2065 * files openend write-only, but what can we do...
2067 have_read = true;
2071 * Check read access and deny read mode
2073 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2074 /* Check access */
2075 open_for_reading = test_netatalk_lock(
2076 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2078 deny_read = test_netatalk_lock(
2079 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2081 DEBUG(10, ("read: %s, deny_write: %s\n",
2082 open_for_reading == true ? "yes" : "no",
2083 deny_read == true ? "yes" : "no"));
2085 if (((access_mask & FILE_READ_DATA) && deny_read)
2086 || ((deny_mode & DENY_READ) && open_for_reading)) {
2087 return NT_STATUS_SHARING_VIOLATION;
2090 /* Set locks */
2091 if ((access_mask & FILE_READ_DATA) && have_read) {
2092 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2093 br_lck = do_lock(
2094 handle->conn->sconn->msg_ctx, fsp,
2095 fsp->op->global->open_persistent_id, 1, off,
2096 READ_LOCK, POSIX_LOCK, false,
2097 &status, NULL);
2099 if (!NT_STATUS_IS_OK(status)) {
2100 return status;
2102 TALLOC_FREE(br_lck);
2105 if ((deny_mode & DENY_READ) && have_read) {
2106 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2107 br_lck = do_lock(
2108 handle->conn->sconn->msg_ctx, fsp,
2109 fsp->op->global->open_persistent_id, 1, off,
2110 READ_LOCK, POSIX_LOCK, false,
2111 &status, NULL);
2113 if (!NT_STATUS_IS_OK(status)) {
2114 return status;
2116 TALLOC_FREE(br_lck);
2121 * Check write access and deny write mode
2123 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2124 /* Check access */
2125 open_for_writing = test_netatalk_lock(
2126 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2128 deny_write = test_netatalk_lock(
2129 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2131 DEBUG(10, ("write: %s, deny_write: %s\n",
2132 open_for_writing == true ? "yes" : "no",
2133 deny_write == true ? "yes" : "no"));
2135 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2136 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2137 return NT_STATUS_SHARING_VIOLATION;
2140 /* Set locks */
2141 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2142 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2143 br_lck = do_lock(
2144 handle->conn->sconn->msg_ctx, fsp,
2145 fsp->op->global->open_persistent_id, 1, off,
2146 READ_LOCK, POSIX_LOCK, false,
2147 &status, NULL);
2149 if (!NT_STATUS_IS_OK(status)) {
2150 return status;
2152 TALLOC_FREE(br_lck);
2155 if ((deny_mode & DENY_WRITE) && have_read) {
2156 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2157 br_lck = do_lock(
2158 handle->conn->sconn->msg_ctx, fsp,
2159 fsp->op->global->open_persistent_id, 1, off,
2160 READ_LOCK, POSIX_LOCK, false,
2161 &status, NULL);
2163 if (!NT_STATUS_IS_OK(status)) {
2164 return status;
2166 TALLOC_FREE(br_lck);
2170 TALLOC_FREE(br_lck);
2172 return status;
2175 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2176 struct smb_request *req,
2177 const struct smb2_create_blobs *in_context_blobs,
2178 struct smb2_create_blobs *out_context_blobs)
2180 struct fruit_config_data *config;
2181 NTSTATUS status;
2182 struct smb2_create_blob *aapl = NULL;
2183 uint32_t cmd;
2184 bool ok;
2185 uint8_t p[16];
2186 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2187 uint64_t req_bitmap, client_caps;
2188 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2189 smb_ucs2_t *model;
2190 size_t modellen;
2192 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2193 return NT_STATUS_UNSUCCESSFUL);
2195 if (!config->use_aapl
2196 || in_context_blobs == NULL
2197 || out_context_blobs == NULL) {
2198 return NT_STATUS_OK;
2201 aapl = smb2_create_blob_find(in_context_blobs,
2202 SMB2_CREATE_TAG_AAPL);
2203 if (aapl == NULL) {
2204 return NT_STATUS_OK;
2207 if (aapl->data.length != 24) {
2208 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2209 (uintmax_t)aapl->data.length));
2210 return NT_STATUS_INVALID_PARAMETER;
2213 cmd = IVAL(aapl->data.data, 0);
2214 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2215 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2216 return NT_STATUS_INVALID_PARAMETER;
2219 req_bitmap = BVAL(aapl->data.data, 8);
2220 client_caps = BVAL(aapl->data.data, 16);
2222 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2223 SIVAL(p, 4, 0);
2224 SBVAL(p, 8, req_bitmap);
2225 ok = data_blob_append(req, &blob, p, 16);
2226 if (!ok) {
2227 return NT_STATUS_UNSUCCESSFUL;
2230 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2231 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2232 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2233 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2234 config->readdir_attr_enabled = true;
2237 if (config->use_copyfile) {
2238 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2239 config->copyfile_enabled = true;
2243 * The client doesn't set the flag, so we can't check
2244 * for it and just set it unconditionally
2246 if (config->unix_info_enabled) {
2247 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2250 SBVAL(p, 0, server_caps);
2251 ok = data_blob_append(req, &blob, p, 8);
2252 if (!ok) {
2253 return NT_STATUS_UNSUCCESSFUL;
2257 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2258 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2259 uint64_t caps = 0;
2261 switch (val) {
2262 case Auto:
2263 break;
2265 case True:
2266 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2267 break;
2269 default:
2270 break;
2273 SBVAL(p, 0, caps);
2275 ok = data_blob_append(req, &blob, p, 8);
2276 if (!ok) {
2277 return NT_STATUS_UNSUCCESSFUL;
2281 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2282 ok = convert_string_talloc(req,
2283 CH_UNIX, CH_UTF16LE,
2284 config->model, strlen(config->model),
2285 &model, &modellen);
2286 if (!ok) {
2287 return NT_STATUS_UNSUCCESSFUL;
2290 SIVAL(p, 0, 0);
2291 SIVAL(p + 4, 0, modellen);
2292 ok = data_blob_append(req, &blob, p, 8);
2293 if (!ok) {
2294 talloc_free(model);
2295 return NT_STATUS_UNSUCCESSFUL;
2298 ok = data_blob_append(req, &blob, model, modellen);
2299 talloc_free(model);
2300 if (!ok) {
2301 return NT_STATUS_UNSUCCESSFUL;
2305 status = smb2_create_blob_add(out_context_blobs,
2306 out_context_blobs,
2307 SMB2_CREATE_TAG_AAPL,
2308 blob);
2309 if (NT_STATUS_IS_OK(status)) {
2310 global_fruit_config.nego_aapl = true;
2311 if (config->aapl_zero_file_id) {
2312 aapl_force_zero_file_id(handle->conn->sconn);
2316 return status;
2319 static bool readdir_attr_meta_finderi_stream(
2320 struct vfs_handle_struct *handle,
2321 const struct smb_filename *smb_fname,
2322 AfpInfo *ai)
2324 struct smb_filename *stream_name = NULL;
2325 files_struct *fsp = NULL;
2326 ssize_t nread;
2327 NTSTATUS status;
2328 int ret;
2329 bool ok;
2330 uint8_t buf[AFP_INFO_SIZE];
2332 stream_name = synthetic_smb_fname(talloc_tos(),
2333 smb_fname->base_name,
2334 AFPINFO_STREAM_NAME,
2335 NULL, smb_fname->flags);
2336 if (stream_name == NULL) {
2337 return false;
2340 ret = SMB_VFS_STAT(handle->conn, stream_name);
2341 if (ret != 0) {
2342 return false;
2345 status = SMB_VFS_CREATE_FILE(
2346 handle->conn, /* conn */
2347 NULL, /* req */
2348 0, /* root_dir_fid */
2349 stream_name, /* fname */
2350 FILE_READ_DATA, /* access_mask */
2351 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2352 FILE_SHARE_DELETE),
2353 FILE_OPEN, /* create_disposition*/
2354 0, /* create_options */
2355 0, /* file_attributes */
2356 INTERNAL_OPEN_ONLY, /* oplock_request */
2357 NULL, /* lease */
2358 0, /* allocation_size */
2359 0, /* private_flags */
2360 NULL, /* sd */
2361 NULL, /* ea_list */
2362 &fsp, /* result */
2363 NULL, /* pinfo */
2364 NULL, NULL); /* create context */
2366 TALLOC_FREE(stream_name);
2368 if (!NT_STATUS_IS_OK(status)) {
2369 return false;
2372 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2373 if (nread != AFP_INFO_SIZE) {
2374 DBG_ERR("short read [%s] [%zd/%d]\n",
2375 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2376 ok = false;
2377 goto fail;
2380 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2381 AFP_FinderSize);
2383 ok = true;
2385 fail:
2386 if (fsp != NULL) {
2387 close_file(NULL, fsp, NORMAL_CLOSE);
2390 return ok;
2393 static bool readdir_attr_meta_finderi_netatalk(
2394 struct vfs_handle_struct *handle,
2395 const struct smb_filename *smb_fname,
2396 AfpInfo *ai)
2398 struct adouble *ad = NULL;
2399 char *p = NULL;
2401 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2402 if (ad == NULL) {
2403 return false;
2406 p = ad_get_entry(ad, ADEID_FINDERI);
2407 if (p == NULL) {
2408 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2409 TALLOC_FREE(ad);
2410 return false;
2413 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2414 TALLOC_FREE(ad);
2415 return true;
2418 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2419 const struct smb_filename *smb_fname,
2420 struct readdir_attr_data *attr_data)
2422 struct fruit_config_data *config = NULL;
2423 uint32_t date_added;
2424 AfpInfo ai = {0};
2425 bool ok;
2427 SMB_VFS_HANDLE_GET_DATA(handle, config,
2428 struct fruit_config_data,
2429 return false);
2431 switch (config->meta) {
2432 case FRUIT_META_NETATALK:
2433 ok = readdir_attr_meta_finderi_netatalk(
2434 handle, smb_fname, &ai);
2435 break;
2437 case FRUIT_META_STREAM:
2438 ok = readdir_attr_meta_finderi_stream(
2439 handle, smb_fname, &ai);
2440 break;
2442 default:
2443 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2444 return false;
2447 if (!ok) {
2448 /* Don't bother with errors, it's likely ENOENT */
2449 return true;
2452 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2453 /* finder_type */
2454 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2455 &ai.afpi_FinderInfo[0], 4);
2457 /* finder_creator */
2458 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2459 &ai.afpi_FinderInfo[4], 4);
2462 /* finder_flags */
2463 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2464 &ai.afpi_FinderInfo[8], 2);
2466 /* finder_ext_flags */
2467 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2468 &ai.afpi_FinderInfo[24], 2);
2470 /* creation date */
2471 date_added = convert_time_t_to_uint32_t(
2472 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2474 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2476 return true;
2479 static uint64_t readdir_attr_rfork_size_adouble(
2480 struct vfs_handle_struct *handle,
2481 const struct smb_filename *smb_fname)
2483 struct adouble *ad = NULL;
2484 uint64_t rfork_size;
2486 ad = ad_get(talloc_tos(), handle, smb_fname,
2487 ADOUBLE_RSRC);
2488 if (ad == NULL) {
2489 return 0;
2492 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2493 TALLOC_FREE(ad);
2495 return rfork_size;
2498 static uint64_t readdir_attr_rfork_size_stream(
2499 struct vfs_handle_struct *handle,
2500 const struct smb_filename *smb_fname)
2502 struct smb_filename *stream_name = NULL;
2503 int ret;
2504 uint64_t rfork_size;
2506 stream_name = synthetic_smb_fname(talloc_tos(),
2507 smb_fname->base_name,
2508 AFPRESOURCE_STREAM_NAME,
2509 NULL, 0);
2510 if (stream_name == NULL) {
2511 return 0;
2514 ret = SMB_VFS_STAT(handle->conn, stream_name);
2515 if (ret != 0) {
2516 TALLOC_FREE(stream_name);
2517 return 0;
2520 rfork_size = stream_name->st.st_ex_size;
2521 TALLOC_FREE(stream_name);
2523 return rfork_size;
2526 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2527 const struct smb_filename *smb_fname)
2529 struct fruit_config_data *config = NULL;
2530 uint64_t rfork_size;
2532 SMB_VFS_HANDLE_GET_DATA(handle, config,
2533 struct fruit_config_data,
2534 return 0);
2536 switch (config->rsrc) {
2537 case FRUIT_RSRC_ADFILE:
2538 case FRUIT_RSRC_XATTR:
2539 rfork_size = readdir_attr_rfork_size_adouble(handle,
2540 smb_fname);
2541 break;
2543 case FRUIT_META_STREAM:
2544 rfork_size = readdir_attr_rfork_size_stream(handle,
2545 smb_fname);
2546 break;
2548 default:
2549 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2550 rfork_size = 0;
2551 break;
2554 return rfork_size;
2557 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2558 const struct smb_filename *smb_fname,
2559 struct readdir_attr_data *attr_data)
2561 NTSTATUS status = NT_STATUS_OK;
2562 struct fruit_config_data *config = NULL;
2563 bool ok;
2565 SMB_VFS_HANDLE_GET_DATA(handle, config,
2566 struct fruit_config_data,
2567 return NT_STATUS_UNSUCCESSFUL);
2570 /* Ensure we return a default value in the creation_date field */
2571 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2574 * Resource fork length
2577 if (config->readdir_attr_rsize) {
2578 uint64_t rfork_size;
2580 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2581 attr_data->attr_data.aapl.rfork_size = rfork_size;
2585 * FinderInfo
2588 if (config->readdir_attr_finder_info) {
2589 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2590 if (!ok) {
2591 status = NT_STATUS_INTERNAL_ERROR;
2595 return status;
2598 /* Search MS NFS style ACE with UNIX mode */
2599 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2600 files_struct *fsp,
2601 const struct security_descriptor *psd,
2602 mode_t *pmode,
2603 bool *pdo_chmod)
2605 uint32_t i;
2606 struct fruit_config_data *config = NULL;
2608 *pdo_chmod = false;
2610 SMB_VFS_HANDLE_GET_DATA(handle, config,
2611 struct fruit_config_data,
2612 return NT_STATUS_UNSUCCESSFUL);
2614 if (!global_fruit_config.nego_aapl) {
2615 return NT_STATUS_OK;
2617 if (psd->dacl == NULL || !config->unix_info_enabled) {
2618 return NT_STATUS_OK;
2621 for (i = 0; i < psd->dacl->num_aces; i++) {
2622 if (dom_sid_compare_domain(
2623 &global_sid_Unix_NFS_Mode,
2624 &psd->dacl->aces[i].trustee) == 0) {
2625 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2626 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2627 *pdo_chmod = true;
2629 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2630 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2631 break;
2635 return NT_STATUS_OK;
2638 /****************************************************************************
2639 * VFS ops
2640 ****************************************************************************/
2642 static int fruit_connect(vfs_handle_struct *handle,
2643 const char *service,
2644 const char *user)
2646 int rc;
2647 char *list = NULL, *newlist = NULL;
2648 struct fruit_config_data *config;
2650 DEBUG(10, ("fruit_connect\n"));
2652 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2653 if (rc < 0) {
2654 return rc;
2657 rc = init_fruit_config(handle);
2658 if (rc != 0) {
2659 return rc;
2662 SMB_VFS_HANDLE_GET_DATA(handle, config,
2663 struct fruit_config_data, return -1);
2665 if (config->veto_appledouble) {
2666 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2668 if (list) {
2669 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2670 newlist = talloc_asprintf(
2671 list,
2672 "%s/" ADOUBLE_NAME_PREFIX "*/",
2673 list);
2674 lp_do_parameter(SNUM(handle->conn),
2675 "veto files",
2676 newlist);
2678 } else {
2679 lp_do_parameter(SNUM(handle->conn),
2680 "veto files",
2681 "/" ADOUBLE_NAME_PREFIX "*/");
2684 TALLOC_FREE(list);
2687 if (config->encoding == FRUIT_ENC_NATIVE) {
2688 lp_do_parameter(
2689 SNUM(handle->conn),
2690 "catia:mappings",
2691 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2692 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2693 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2694 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2695 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2696 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2697 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2698 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2699 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2700 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2701 "0x0d:0xf00d");
2704 return rc;
2707 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2708 struct smb_filename *smb_fname,
2709 files_struct *fsp,
2710 int flags,
2711 mode_t mode)
2713 AfpInfo *ai = NULL;
2714 char afpinfo_buf[AFP_INFO_SIZE];
2715 ssize_t len, written;
2716 int hostfd = -1;
2717 int rc = -1;
2719 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2720 if (hostfd == -1) {
2721 return -1;
2724 if (!(flags & (O_CREAT | O_TRUNC))) {
2725 return hostfd;
2728 ai = afpinfo_new(talloc_tos());
2729 if (ai == NULL) {
2730 rc = -1;
2731 goto fail;
2734 len = afpinfo_pack(ai, afpinfo_buf);
2735 if (len != AFP_INFO_SIZE) {
2736 rc = -1;
2737 goto fail;
2740 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
2741 fsp->fh->fd = hostfd;
2743 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
2744 AFP_INFO_SIZE, 0);
2745 fsp->fh->fd = -1;
2746 if (written != AFP_INFO_SIZE) {
2747 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
2748 rc = -1;
2749 goto fail;
2752 rc = 0;
2754 fail:
2755 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
2757 if (rc != 0) {
2758 int saved_errno = errno;
2759 if (hostfd >= 0) {
2760 fsp->fh->fd = hostfd;
2761 SMB_VFS_NEXT_CLOSE(handle, fsp);
2763 hostfd = -1;
2764 errno = saved_errno;
2766 return hostfd;
2769 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
2770 struct smb_filename *smb_fname,
2771 files_struct *fsp,
2772 int flags,
2773 mode_t mode)
2775 int rc;
2776 int fakefd = -1;
2777 struct adouble *ad = NULL;
2778 int fds[2];
2780 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2783 * Return a valid fd, but ensure any attempt to use it returns an error
2784 * (EPIPE). All operations on the smb_fname or the fsp will use path
2785 * based syscalls.
2787 rc = pipe(fds);
2788 if (rc != 0) {
2789 goto exit;
2791 fakefd = fds[0];
2792 close(fds[1]);
2794 if (flags & (O_CREAT | O_TRUNC)) {
2796 * The attribute does not exist or needs to be truncated,
2797 * create an AppleDouble EA
2799 ad = ad_init(fsp, handle, ADOUBLE_META);
2800 if (ad == NULL) {
2801 rc = -1;
2802 goto exit;
2805 rc = ad_set(ad, fsp->fsp_name);
2806 if (rc != 0) {
2807 rc = -1;
2808 goto exit;
2811 TALLOC_FREE(ad);
2814 exit:
2815 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
2816 if (rc != 0) {
2817 int saved_errno = errno;
2818 if (fakefd >= 0) {
2819 close(fakefd);
2821 fakefd = -1;
2822 errno = saved_errno;
2824 return fakefd;
2827 static int fruit_open_meta(vfs_handle_struct *handle,
2828 struct smb_filename *smb_fname,
2829 files_struct *fsp, int flags, mode_t mode)
2831 int fd;
2832 struct fruit_config_data *config = NULL;
2833 struct fio *fio = NULL;
2835 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
2837 SMB_VFS_HANDLE_GET_DATA(handle, config,
2838 struct fruit_config_data, return -1);
2840 switch (config->meta) {
2841 case FRUIT_META_STREAM:
2842 fd = fruit_open_meta_stream(handle, smb_fname,
2843 fsp, flags, mode);
2844 break;
2846 case FRUIT_META_NETATALK:
2847 fd = fruit_open_meta_netatalk(handle, smb_fname,
2848 fsp, flags, mode);
2849 break;
2851 default:
2852 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2853 return -1;
2856 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
2858 if (fd == -1) {
2859 return -1;
2862 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
2863 fio->type = ADOUBLE_META;
2864 fio->config = config;
2866 return fd;
2869 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
2870 struct smb_filename *smb_fname,
2871 files_struct *fsp,
2872 int flags,
2873 mode_t mode)
2875 int rc = 0;
2876 struct adouble *ad = NULL;
2877 struct smb_filename *smb_fname_base = NULL;
2878 struct fruit_config_data *config = NULL;
2879 int hostfd = -1;
2881 SMB_VFS_HANDLE_GET_DATA(handle, config,
2882 struct fruit_config_data, return -1);
2884 if ((!(flags & O_CREAT)) &&
2885 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
2887 /* sorry, but directories don't habe a resource fork */
2888 rc = -1;
2889 goto exit;
2892 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
2893 if (rc != 0) {
2894 goto exit;
2897 /* Sanitize flags */
2898 if (flags & O_WRONLY) {
2899 /* We always need read access for the metadata header too */
2900 flags &= ~O_WRONLY;
2901 flags |= O_RDWR;
2904 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
2905 flags, mode);
2906 if (hostfd == -1) {
2907 rc = -1;
2908 goto exit;
2911 if (flags & (O_CREAT | O_TRUNC)) {
2912 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
2913 if (ad == NULL) {
2914 rc = -1;
2915 goto exit;
2918 fsp->fh->fd = hostfd;
2920 rc = ad_fset(ad, fsp);
2921 fsp->fh->fd = -1;
2922 if (rc != 0) {
2923 rc = -1;
2924 goto exit;
2926 TALLOC_FREE(ad);
2929 exit:
2931 TALLOC_FREE(smb_fname_base);
2933 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2934 if (rc != 0) {
2935 int saved_errno = errno;
2936 if (hostfd >= 0) {
2938 * BUGBUGBUG -- we would need to call
2939 * fd_close_posix here, but we don't have a
2940 * full fsp yet
2942 fsp->fh->fd = hostfd;
2943 SMB_VFS_CLOSE(fsp);
2945 hostfd = -1;
2946 errno = saved_errno;
2948 return hostfd;
2951 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
2952 struct smb_filename *smb_fname,
2953 files_struct *fsp,
2954 int flags,
2955 mode_t mode)
2957 #ifdef HAVE_ATTROPEN
2958 int fd = -1;
2960 fd = attropen(smb_fname->base_name,
2961 AFPRESOURCE_EA_NETATALK,
2962 flags,
2963 mode);
2964 if (fd == -1) {
2965 return -1;
2968 return fd;
2970 #else
2971 errno = ENOSYS;
2972 return -1;
2973 #endif
2976 static int fruit_open_rsrc(vfs_handle_struct *handle,
2977 struct smb_filename *smb_fname,
2978 files_struct *fsp, int flags, mode_t mode)
2980 int fd;
2981 struct fruit_config_data *config = NULL;
2982 struct fio *fio = NULL;
2984 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
2986 SMB_VFS_HANDLE_GET_DATA(handle, config,
2987 struct fruit_config_data, return -1);
2989 if (((flags & O_ACCMODE) == O_RDONLY)
2990 && (flags & O_CREAT)
2991 && !VALID_STAT(fsp->fsp_name->st))
2994 * This means the stream doesn't exist. macOS SMB server fails
2995 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
2996 * 12565 and the test for this combination in
2997 * test_rfork_create().
2999 errno = ENOENT;
3000 return -1;
3003 switch (config->rsrc) {
3004 case FRUIT_RSRC_STREAM:
3005 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3006 break;
3008 case FRUIT_RSRC_ADFILE:
3009 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3010 fsp, flags, mode);
3011 break;
3013 case FRUIT_RSRC_XATTR:
3014 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3015 fsp, flags, mode);
3016 break;
3018 default:
3019 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3020 return -1;
3023 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3025 if (fd == -1) {
3026 return -1;
3029 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3030 fio->type = ADOUBLE_RSRC;
3031 fio->config = config;
3033 return fd;
3036 static int fruit_open(vfs_handle_struct *handle,
3037 struct smb_filename *smb_fname,
3038 files_struct *fsp, int flags, mode_t mode)
3040 int fd;
3042 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3044 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3045 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3048 if (is_afpinfo_stream(smb_fname)) {
3049 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3050 } else if (is_afpresource_stream(smb_fname)) {
3051 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3052 } else {
3053 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3056 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3058 return fd;
3061 static int fruit_rename(struct vfs_handle_struct *handle,
3062 const struct smb_filename *smb_fname_src,
3063 const struct smb_filename *smb_fname_dst)
3065 int rc = -1;
3066 struct fruit_config_data *config = NULL;
3067 struct smb_filename *src_adp_smb_fname = NULL;
3068 struct smb_filename *dst_adp_smb_fname = NULL;
3070 SMB_VFS_HANDLE_GET_DATA(handle, config,
3071 struct fruit_config_data, return -1);
3073 if (!VALID_STAT(smb_fname_src->st)) {
3074 DBG_ERR("Need valid stat for [%s]\n",
3075 smb_fname_str_dbg(smb_fname_src));
3076 return -1;
3079 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3080 if (rc != 0) {
3081 return -1;
3084 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3085 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3087 return 0;
3090 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3091 if (rc != 0) {
3092 goto done;
3095 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3096 if (rc != 0) {
3097 goto done;
3100 DBG_DEBUG("%s -> %s\n",
3101 smb_fname_str_dbg(src_adp_smb_fname),
3102 smb_fname_str_dbg(dst_adp_smb_fname));
3104 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3105 if (errno == ENOENT) {
3106 rc = 0;
3109 done:
3110 TALLOC_FREE(src_adp_smb_fname);
3111 TALLOC_FREE(dst_adp_smb_fname);
3112 return rc;
3115 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3116 const struct smb_filename *smb_fname)
3118 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3121 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3122 const struct smb_filename *smb_fname)
3124 return SMB_VFS_REMOVEXATTR(handle->conn,
3125 smb_fname,
3126 AFPINFO_EA_NETATALK);
3129 static int fruit_unlink_meta(vfs_handle_struct *handle,
3130 const struct smb_filename *smb_fname)
3132 struct fruit_config_data *config = NULL;
3133 int rc;
3135 SMB_VFS_HANDLE_GET_DATA(handle, config,
3136 struct fruit_config_data, return -1);
3138 switch (config->meta) {
3139 case FRUIT_META_STREAM:
3140 rc = fruit_unlink_meta_stream(handle, smb_fname);
3141 break;
3143 case FRUIT_META_NETATALK:
3144 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3145 break;
3147 default:
3148 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3149 return -1;
3152 return rc;
3155 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3156 const struct smb_filename *smb_fname,
3157 bool force_unlink)
3159 int ret;
3161 if (!force_unlink) {
3162 struct smb_filename *smb_fname_cp = NULL;
3163 off_t size;
3165 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3166 if (smb_fname_cp == NULL) {
3167 return -1;
3171 * 0 byte resource fork streams are not listed by
3172 * vfs_streaminfo, as a result stream cleanup/deletion of file
3173 * deletion doesn't remove the resourcefork stream.
3176 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3177 if (ret != 0) {
3178 TALLOC_FREE(smb_fname_cp);
3179 DBG_ERR("stat [%s] failed [%s]\n",
3180 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3181 return -1;
3184 size = smb_fname_cp->st.st_ex_size;
3185 TALLOC_FREE(smb_fname_cp);
3187 if (size > 0) {
3188 /* OS X ignores resource fork stream delete requests */
3189 return 0;
3193 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3194 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3195 ret = 0;
3198 return ret;
3201 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3202 const struct smb_filename *smb_fname,
3203 bool force_unlink)
3205 int rc;
3206 struct adouble *ad = NULL;
3207 struct smb_filename *adp_smb_fname = NULL;
3209 if (!force_unlink) {
3210 ad = ad_get(talloc_tos(), handle, smb_fname,
3211 ADOUBLE_RSRC);
3212 if (ad == NULL) {
3213 errno = ENOENT;
3214 return -1;
3219 * 0 byte resource fork streams are not listed by
3220 * vfs_streaminfo, as a result stream cleanup/deletion of file
3221 * deletion doesn't remove the resourcefork stream.
3224 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3225 /* OS X ignores resource fork stream delete requests */
3226 TALLOC_FREE(ad);
3227 return 0;
3230 TALLOC_FREE(ad);
3233 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3234 if (rc != 0) {
3235 return -1;
3238 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3239 TALLOC_FREE(adp_smb_fname);
3240 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3241 rc = 0;
3244 return rc;
3247 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3248 const struct smb_filename *smb_fname,
3249 bool force_unlink)
3252 * OS X ignores resource fork stream delete requests, so nothing to do
3253 * here. Removing the file will remove the xattr anyway, so we don't
3254 * have to take care of removing 0 byte resource forks that could be
3255 * left behind.
3257 return 0;
3260 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3261 const struct smb_filename *smb_fname,
3262 bool force_unlink)
3264 struct fruit_config_data *config = NULL;
3265 int rc;
3267 SMB_VFS_HANDLE_GET_DATA(handle, config,
3268 struct fruit_config_data, return -1);
3270 switch (config->rsrc) {
3271 case FRUIT_RSRC_STREAM:
3272 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3273 break;
3275 case FRUIT_RSRC_ADFILE:
3276 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3277 break;
3279 case FRUIT_RSRC_XATTR:
3280 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3281 break;
3283 default:
3284 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3285 return -1;
3288 return rc;
3291 static int fruit_unlink(vfs_handle_struct *handle,
3292 const struct smb_filename *smb_fname)
3294 int rc;
3295 struct fruit_config_data *config = NULL;
3296 struct smb_filename *rsrc_smb_fname = NULL;
3298 SMB_VFS_HANDLE_GET_DATA(handle, config,
3299 struct fruit_config_data, return -1);
3301 if (is_afpinfo_stream(smb_fname)) {
3302 return fruit_unlink_meta(handle, smb_fname);
3303 } else if (is_afpresource_stream(smb_fname)) {
3304 return fruit_unlink_rsrc(handle, smb_fname, false);
3305 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3306 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3310 * A request to delete the base file. Because 0 byte resource
3311 * fork streams are not listed by fruit_streaminfo,
3312 * delete_all_streams() can't remove 0 byte resource fork
3313 * streams, so we have to cleanup this here.
3315 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3316 smb_fname->base_name,
3317 AFPRESOURCE_STREAM_NAME,
3318 NULL,
3319 smb_fname->flags);
3320 if (rsrc_smb_fname == NULL) {
3321 return -1;
3324 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3325 if ((rc != 0) && (errno != ENOENT)) {
3326 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3327 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3328 TALLOC_FREE(rsrc_smb_fname);
3329 return -1;
3331 TALLOC_FREE(rsrc_smb_fname);
3333 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3336 static int fruit_chmod(vfs_handle_struct *handle,
3337 const struct smb_filename *smb_fname,
3338 mode_t mode)
3340 int rc = -1;
3341 struct fruit_config_data *config = NULL;
3342 struct smb_filename *smb_fname_adp = NULL;
3344 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3345 if (rc != 0) {
3346 return rc;
3349 SMB_VFS_HANDLE_GET_DATA(handle, config,
3350 struct fruit_config_data, return -1);
3352 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3353 return 0;
3356 if (!VALID_STAT(smb_fname->st)) {
3357 return 0;
3360 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3361 return 0;
3364 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3365 if (rc != 0) {
3366 return -1;
3369 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3371 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3372 if (errno == ENOENT) {
3373 rc = 0;
3376 TALLOC_FREE(smb_fname_adp);
3377 return rc;
3380 static int fruit_chown(vfs_handle_struct *handle,
3381 const struct smb_filename *smb_fname,
3382 uid_t uid,
3383 gid_t gid)
3385 int rc = -1;
3386 struct fruit_config_data *config = NULL;
3387 struct smb_filename *adp_smb_fname = NULL;
3389 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3390 if (rc != 0) {
3391 return rc;
3394 SMB_VFS_HANDLE_GET_DATA(handle, config,
3395 struct fruit_config_data, return -1);
3397 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3398 return 0;
3401 if (!VALID_STAT(smb_fname->st)) {
3402 return 0;
3405 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3406 return 0;
3409 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3410 if (rc != 0) {
3411 goto done;
3414 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3416 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3417 if (errno == ENOENT) {
3418 rc = 0;
3421 done:
3422 TALLOC_FREE(adp_smb_fname);
3423 return rc;
3426 static int fruit_rmdir(struct vfs_handle_struct *handle,
3427 const struct smb_filename *smb_fname)
3429 DIR *dh = NULL;
3430 struct dirent *de;
3431 struct fruit_config_data *config;
3433 SMB_VFS_HANDLE_GET_DATA(handle, config,
3434 struct fruit_config_data, return -1);
3436 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3437 goto exit_rmdir;
3441 * Due to there is no way to change bDeleteVetoFiles variable
3442 * from this module, need to clean up ourselves
3445 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3446 if (dh == NULL) {
3447 goto exit_rmdir;
3450 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3451 int match;
3452 struct adouble *ad = NULL;
3453 char *p = NULL;
3454 struct smb_filename *ad_smb_fname = NULL;
3455 int ret;
3457 match = strncmp(de->d_name,
3458 ADOUBLE_NAME_PREFIX,
3459 strlen(ADOUBLE_NAME_PREFIX));
3460 if (match != 0) {
3461 continue;
3464 p = talloc_asprintf(talloc_tos(), "%s/%s",
3465 smb_fname->base_name, de->d_name);
3466 if (p == NULL) {
3467 DBG_ERR("talloc_asprintf failed\n");
3468 return -1;
3471 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3472 NULL, NULL,
3473 smb_fname->flags);
3474 TALLOC_FREE(p);
3475 if (ad_smb_fname == NULL) {
3476 DBG_ERR("synthetic_smb_fname failed\n");
3477 return -1;
3481 * Check whether it's a valid AppleDouble file, if
3482 * yes, delete it, ignore it otherwise.
3484 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3485 if (ad == NULL) {
3486 TALLOC_FREE(ad_smb_fname);
3487 TALLOC_FREE(p);
3488 continue;
3490 TALLOC_FREE(ad);
3492 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3493 TALLOC_FREE(ad_smb_fname);
3494 if (ret != 0) {
3495 DBG_ERR("Deleting [%s] failed\n",
3496 smb_fname_str_dbg(ad_smb_fname));
3500 exit_rmdir:
3501 if (dh) {
3502 closedir(dh);
3504 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3507 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3508 files_struct *fsp, void *data,
3509 size_t n, off_t offset)
3511 ssize_t nread;
3512 int ret;
3514 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3516 if (nread == n) {
3517 return nread;
3520 DBG_ERR("Removing [%s] after short read [%zd]\n",
3521 fsp_str_dbg(fsp), nread);
3523 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3524 if (ret != 0) {
3525 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3526 return -1;
3529 errno = EINVAL;
3530 return -1;
3533 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3534 files_struct *fsp, void *data,
3535 size_t n, off_t offset)
3537 AfpInfo *ai = NULL;
3538 struct adouble *ad = NULL;
3539 char afpinfo_buf[AFP_INFO_SIZE];
3540 char *p = NULL;
3541 ssize_t nread;
3543 ai = afpinfo_new(talloc_tos());
3544 if (ai == NULL) {
3545 return -1;
3548 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3549 if (ad == NULL) {
3550 nread = -1;
3551 goto fail;
3554 p = ad_get_entry(ad, ADEID_FINDERI);
3555 if (p == NULL) {
3556 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3557 nread = -1;
3558 goto fail;
3561 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3563 nread = afpinfo_pack(ai, afpinfo_buf);
3564 if (nread != AFP_INFO_SIZE) {
3565 nread = -1;
3566 goto fail;
3569 memcpy(data, afpinfo_buf, n);
3570 nread = n;
3572 fail:
3573 TALLOC_FREE(ai);
3574 return nread;
3577 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3578 files_struct *fsp, void *data,
3579 size_t n, off_t offset)
3581 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3582 ssize_t nread;
3583 ssize_t to_return;
3586 * OS X has a off-by-1 error in the offset calculation, so we're
3587 * bug compatible here. It won't hurt, as any relevant real
3588 * world read requests from the AFP_AfpInfo stream will be
3589 * offset=0 n=60. offset is ignored anyway, see below.
3591 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3592 return 0;
3595 /* Yes, macOS always reads from offset 0 */
3596 offset = 0;
3597 to_return = MIN(n, AFP_INFO_SIZE);
3599 switch (fio->config->meta) {
3600 case FRUIT_META_STREAM:
3601 nread = fruit_pread_meta_stream(handle, fsp, data,
3602 to_return, offset);
3603 break;
3605 case FRUIT_META_NETATALK:
3606 nread = fruit_pread_meta_adouble(handle, fsp, data,
3607 to_return, offset);
3608 break;
3610 default:
3611 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3612 return -1;
3615 return nread;
3618 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3619 files_struct *fsp, void *data,
3620 size_t n, off_t offset)
3622 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3625 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3626 files_struct *fsp, void *data,
3627 size_t n, off_t offset)
3629 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3632 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3633 files_struct *fsp, void *data,
3634 size_t n, off_t offset)
3636 struct adouble *ad = NULL;
3637 ssize_t nread;
3639 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3640 if (ad == NULL) {
3641 return -1;
3644 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3645 offset + ad_getentryoff(ad, ADEID_RFORK));
3647 TALLOC_FREE(ad);
3648 return nread;
3651 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3652 files_struct *fsp, void *data,
3653 size_t n, off_t offset)
3655 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3656 ssize_t nread;
3658 switch (fio->config->rsrc) {
3659 case FRUIT_RSRC_STREAM:
3660 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3661 break;
3663 case FRUIT_RSRC_ADFILE:
3664 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3665 break;
3667 case FRUIT_RSRC_XATTR:
3668 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3669 break;
3671 default:
3672 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3673 return -1;
3676 return nread;
3679 static ssize_t fruit_pread(vfs_handle_struct *handle,
3680 files_struct *fsp, void *data,
3681 size_t n, off_t offset)
3683 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3684 ssize_t nread;
3686 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3687 fsp_str_dbg(fsp), (intmax_t)offset, n);
3689 if (fio == NULL) {
3690 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3693 if (fio->type == ADOUBLE_META) {
3694 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3695 } else {
3696 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3699 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3700 return nread;
3703 static bool fruit_must_handle_aio_stream(struct fio *fio)
3705 if (fio == NULL) {
3706 return false;
3709 if ((fio->type == ADOUBLE_META) &&
3710 (fio->config->meta == FRUIT_META_NETATALK))
3712 return true;
3715 if ((fio->type == ADOUBLE_RSRC) &&
3716 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
3718 return true;
3721 return false;
3724 struct fruit_pread_state {
3725 ssize_t nread;
3726 struct vfs_aio_state vfs_aio_state;
3729 static void fruit_pread_done(struct tevent_req *subreq);
3731 static struct tevent_req *fruit_pread_send(
3732 struct vfs_handle_struct *handle,
3733 TALLOC_CTX *mem_ctx,
3734 struct tevent_context *ev,
3735 struct files_struct *fsp,
3736 void *data,
3737 size_t n, off_t offset)
3739 struct tevent_req *req = NULL;
3740 struct tevent_req *subreq = NULL;
3741 struct fruit_pread_state *state = NULL;
3742 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3744 req = tevent_req_create(mem_ctx, &state,
3745 struct fruit_pread_state);
3746 if (req == NULL) {
3747 return NULL;
3750 if (fruit_must_handle_aio_stream(fio)) {
3751 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
3752 if (state->nread != n) {
3753 if (state->nread != -1) {
3754 errno = EIO;
3756 tevent_req_error(req, errno);
3757 return tevent_req_post(req, ev);
3759 tevent_req_done(req);
3760 return tevent_req_post(req, ev);
3763 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
3764 data, n, offset);
3765 if (tevent_req_nomem(req, subreq)) {
3766 return tevent_req_post(req, ev);
3768 tevent_req_set_callback(subreq, fruit_pread_done, req);
3769 return req;
3772 static void fruit_pread_done(struct tevent_req *subreq)
3774 struct tevent_req *req = tevent_req_callback_data(
3775 subreq, struct tevent_req);
3776 struct fruit_pread_state *state = tevent_req_data(
3777 req, struct fruit_pread_state);
3779 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
3780 TALLOC_FREE(subreq);
3782 if (tevent_req_error(req, state->vfs_aio_state.error)) {
3783 return;
3785 tevent_req_done(req);
3788 static ssize_t fruit_pread_recv(struct tevent_req *req,
3789 struct vfs_aio_state *vfs_aio_state)
3791 struct fruit_pread_state *state = tevent_req_data(
3792 req, struct fruit_pread_state);
3794 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3795 return -1;
3798 *vfs_aio_state = state->vfs_aio_state;
3799 return state->nread;
3802 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
3803 files_struct *fsp, const void *data,
3804 size_t n, off_t offset)
3806 AfpInfo *ai = NULL;
3807 int ret;
3809 ai = afpinfo_unpack(talloc_tos(), data);
3810 if (ai == NULL) {
3811 return -1;
3814 if (ai_empty_finderinfo(ai)) {
3815 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3816 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3817 DBG_ERR("Can't delete metadata for %s: %s\n",
3818 fsp_str_dbg(fsp), strerror(errno));
3819 TALLOC_FREE(ai);
3820 return -1;
3823 return n;
3826 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3829 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
3830 files_struct *fsp, const void *data,
3831 size_t n, off_t offset)
3833 struct adouble *ad = NULL;
3834 AfpInfo *ai = NULL;
3835 char *p = NULL;
3836 int ret;
3838 ai = afpinfo_unpack(talloc_tos(), data);
3839 if (ai == NULL) {
3840 return -1;
3843 if (ai_empty_finderinfo(ai)) {
3844 ret = SMB_VFS_REMOVEXATTR(handle->conn,
3845 fsp->fsp_name,
3846 AFPINFO_EA_NETATALK);
3848 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
3849 DBG_ERR("Can't delete metadata for %s: %s\n",
3850 fsp_str_dbg(fsp), strerror(errno));
3851 return -1;
3854 return n;
3857 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3858 if (ad == NULL) {
3859 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
3860 if (ad == NULL) {
3861 return -1;
3864 p = ad_get_entry(ad, ADEID_FINDERI);
3865 if (p == NULL) {
3866 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3867 TALLOC_FREE(ad);
3868 return -1;
3871 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
3873 ret = ad_fset(ad, fsp);
3874 if (ret != 0) {
3875 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3876 TALLOC_FREE(ad);
3877 return -1;
3880 TALLOC_FREE(ad);
3881 return n;
3884 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
3885 files_struct *fsp, const void *data,
3886 size_t n, off_t offset)
3888 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3889 ssize_t nwritten;
3892 * Writing an all 0 blob to the metadata stream
3893 * results in the stream being removed on a macOS
3894 * server. This ensures we behave the same and it
3895 * verified by the "delete AFP_AfpInfo by writing all
3896 * 0" test.
3898 if (n != AFP_INFO_SIZE || offset != 0) {
3899 DBG_ERR("unexpected offset=%jd or size=%jd\n",
3900 (intmax_t)offset, (intmax_t)n);
3901 return -1;
3904 switch (fio->config->meta) {
3905 case FRUIT_META_STREAM:
3906 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
3907 n, offset);
3908 break;
3910 case FRUIT_META_NETATALK:
3911 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
3912 n, offset);
3913 break;
3915 default:
3916 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3917 return -1;
3920 return nwritten;
3923 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
3924 files_struct *fsp, const void *data,
3925 size_t n, off_t offset)
3927 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3930 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
3931 files_struct *fsp, const void *data,
3932 size_t n, off_t offset)
3934 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
3937 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
3938 files_struct *fsp, const void *data,
3939 size_t n, off_t offset)
3941 struct adouble *ad = NULL;
3942 ssize_t nwritten;
3943 int ret;
3945 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3946 if (ad == NULL) {
3947 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
3948 return -1;
3951 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
3952 offset + ad_getentryoff(ad, ADEID_RFORK));
3953 if (nwritten != n) {
3954 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
3955 fsp_str_dbg(fsp), nwritten, n);
3956 TALLOC_FREE(ad);
3957 return -1;
3960 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
3961 ad_setentrylen(ad, ADEID_RFORK, n + offset);
3962 ret = ad_fset(ad, fsp);
3963 if (ret != 0) {
3964 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
3965 TALLOC_FREE(ad);
3966 return -1;
3970 TALLOC_FREE(ad);
3971 return n;
3974 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
3975 files_struct *fsp, const void *data,
3976 size_t n, off_t offset)
3978 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3979 ssize_t nwritten;
3981 switch (fio->config->rsrc) {
3982 case FRUIT_RSRC_STREAM:
3983 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
3984 break;
3986 case FRUIT_RSRC_ADFILE:
3987 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
3988 break;
3990 case FRUIT_RSRC_XATTR:
3991 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
3992 break;
3994 default:
3995 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3996 return -1;
3999 return nwritten;
4002 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4003 files_struct *fsp, const void *data,
4004 size_t n, off_t offset)
4006 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4007 ssize_t nwritten;
4009 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4010 fsp_str_dbg(fsp), (intmax_t)offset, n);
4012 if (fio == NULL) {
4013 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4016 if (fio->type == ADOUBLE_META) {
4017 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4018 } else {
4019 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4022 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4023 return nwritten;
4026 struct fruit_pwrite_state {
4027 ssize_t nwritten;
4028 struct vfs_aio_state vfs_aio_state;
4031 static void fruit_pwrite_done(struct tevent_req *subreq);
4033 static struct tevent_req *fruit_pwrite_send(
4034 struct vfs_handle_struct *handle,
4035 TALLOC_CTX *mem_ctx,
4036 struct tevent_context *ev,
4037 struct files_struct *fsp,
4038 const void *data,
4039 size_t n, off_t offset)
4041 struct tevent_req *req = NULL;
4042 struct tevent_req *subreq = NULL;
4043 struct fruit_pwrite_state *state = NULL;
4044 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4046 req = tevent_req_create(mem_ctx, &state,
4047 struct fruit_pwrite_state);
4048 if (req == NULL) {
4049 return NULL;
4052 if (fruit_must_handle_aio_stream(fio)) {
4053 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4054 if (state->nwritten != n) {
4055 if (state->nwritten != -1) {
4056 errno = EIO;
4058 tevent_req_error(req, errno);
4059 return tevent_req_post(req, ev);
4061 tevent_req_done(req);
4062 return tevent_req_post(req, ev);
4065 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4066 data, n, offset);
4067 if (tevent_req_nomem(req, subreq)) {
4068 return tevent_req_post(req, ev);
4070 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4071 return req;
4074 static void fruit_pwrite_done(struct tevent_req *subreq)
4076 struct tevent_req *req = tevent_req_callback_data(
4077 subreq, struct tevent_req);
4078 struct fruit_pwrite_state *state = tevent_req_data(
4079 req, struct fruit_pwrite_state);
4081 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4082 TALLOC_FREE(subreq);
4084 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4085 return;
4087 tevent_req_done(req);
4090 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4091 struct vfs_aio_state *vfs_aio_state)
4093 struct fruit_pwrite_state *state = tevent_req_data(
4094 req, struct fruit_pwrite_state);
4096 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4097 return -1;
4100 *vfs_aio_state = state->vfs_aio_state;
4101 return state->nwritten;
4105 * Helper to stat/lstat the base file of an smb_fname.
4107 static int fruit_stat_base(vfs_handle_struct *handle,
4108 struct smb_filename *smb_fname,
4109 bool follow_links)
4111 char *tmp_stream_name;
4112 int rc;
4114 tmp_stream_name = smb_fname->stream_name;
4115 smb_fname->stream_name = NULL;
4116 if (follow_links) {
4117 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4118 } else {
4119 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4121 smb_fname->stream_name = tmp_stream_name;
4122 return rc;
4125 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4126 struct smb_filename *smb_fname,
4127 bool follow_links)
4129 int ret;
4131 if (follow_links) {
4132 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4133 } else {
4134 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4137 return ret;
4140 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4141 struct smb_filename *smb_fname,
4142 bool follow_links)
4144 struct adouble *ad = NULL;
4146 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4147 if (ad == NULL) {
4148 DBG_INFO("fruit_stat_meta %s: %s\n",
4149 smb_fname_str_dbg(smb_fname), strerror(errno));
4150 errno = ENOENT;
4151 return -1;
4153 TALLOC_FREE(ad);
4155 /* Populate the stat struct with info from the base file. */
4156 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4157 return -1;
4159 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4160 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4161 smb_fname->stream_name);
4162 return 0;
4165 static int fruit_stat_meta(vfs_handle_struct *handle,
4166 struct smb_filename *smb_fname,
4167 bool follow_links)
4169 struct fruit_config_data *config = NULL;
4170 int ret;
4172 SMB_VFS_HANDLE_GET_DATA(handle, config,
4173 struct fruit_config_data, return -1);
4175 switch (config->meta) {
4176 case FRUIT_META_STREAM:
4177 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4178 break;
4180 case FRUIT_META_NETATALK:
4181 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4182 break;
4184 default:
4185 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4186 return -1;
4189 return ret;
4192 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4193 struct smb_filename *smb_fname,
4194 bool follow_links)
4196 struct adouble *ad = NULL;
4197 int ret;
4199 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4200 if (ad == NULL) {
4201 errno = ENOENT;
4202 return -1;
4205 /* Populate the stat struct with info from the base file. */
4206 ret = fruit_stat_base(handle, smb_fname, follow_links);
4207 if (ret != 0) {
4208 TALLOC_FREE(ad);
4209 return -1;
4212 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4213 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4214 smb_fname->stream_name);
4215 TALLOC_FREE(ad);
4216 return 0;
4219 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4220 struct smb_filename *smb_fname,
4221 bool follow_links)
4223 int ret;
4225 if (follow_links) {
4226 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4227 } else {
4228 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4231 return ret;
4234 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4235 struct smb_filename *smb_fname,
4236 bool follow_links)
4238 #ifdef HAVE_ATTROPEN
4239 int ret;
4240 int fd = -1;
4242 /* Populate the stat struct with info from the base file. */
4243 ret = fruit_stat_base(handle, smb_fname, follow_links);
4244 if (ret != 0) {
4245 return -1;
4248 fd = attropen(smb_fname->base_name,
4249 AFPRESOURCE_EA_NETATALK,
4250 O_RDONLY);
4251 if (fd == -1) {
4252 return 0;
4255 ret = sys_fstat(fd, &smb_fname->st, false);
4256 if (ret != 0) {
4257 close(fd);
4258 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4259 AFPRESOURCE_EA_NETATALK);
4260 return -1;
4262 close(fd);
4263 fd = -1;
4265 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4266 smb_fname->stream_name);
4268 return ret;
4270 #else
4271 errno = ENOSYS;
4272 return -1;
4273 #endif
4276 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4277 struct smb_filename *smb_fname,
4278 bool follow_links)
4280 struct fruit_config_data *config = NULL;
4281 int ret;
4283 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4285 SMB_VFS_HANDLE_GET_DATA(handle, config,
4286 struct fruit_config_data, return -1);
4288 switch (config->rsrc) {
4289 case FRUIT_RSRC_STREAM:
4290 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4291 break;
4293 case FRUIT_RSRC_XATTR:
4294 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4295 break;
4297 case FRUIT_RSRC_ADFILE:
4298 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4299 break;
4301 default:
4302 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4303 return -1;
4306 return ret;
4309 static int fruit_stat(vfs_handle_struct *handle,
4310 struct smb_filename *smb_fname)
4312 int rc = -1;
4314 DEBUG(10, ("fruit_stat called for %s\n",
4315 smb_fname_str_dbg(smb_fname)));
4317 if (!is_ntfs_stream_smb_fname(smb_fname)
4318 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4319 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4320 if (rc == 0) {
4321 update_btime(handle, smb_fname);
4323 return rc;
4327 * Note if lp_posix_paths() is true, we can never
4328 * get here as is_ntfs_stream_smb_fname() is
4329 * always false. So we never need worry about
4330 * not following links here.
4333 if (is_afpinfo_stream(smb_fname)) {
4334 rc = fruit_stat_meta(handle, smb_fname, true);
4335 } else if (is_afpresource_stream(smb_fname)) {
4336 rc = fruit_stat_rsrc(handle, smb_fname, true);
4337 } else {
4338 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4341 if (rc == 0) {
4342 update_btime(handle, smb_fname);
4343 smb_fname->st.st_ex_mode &= ~S_IFMT;
4344 smb_fname->st.st_ex_mode |= S_IFREG;
4345 smb_fname->st.st_ex_blocks =
4346 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4348 return rc;
4351 static int fruit_lstat(vfs_handle_struct *handle,
4352 struct smb_filename *smb_fname)
4354 int rc = -1;
4356 DEBUG(10, ("fruit_lstat called for %s\n",
4357 smb_fname_str_dbg(smb_fname)));
4359 if (!is_ntfs_stream_smb_fname(smb_fname)
4360 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4361 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4362 if (rc == 0) {
4363 update_btime(handle, smb_fname);
4365 return rc;
4368 if (is_afpinfo_stream(smb_fname)) {
4369 rc = fruit_stat_meta(handle, smb_fname, false);
4370 } else if (is_afpresource_stream(smb_fname)) {
4371 rc = fruit_stat_rsrc(handle, smb_fname, false);
4372 } else {
4373 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4376 if (rc == 0) {
4377 update_btime(handle, smb_fname);
4378 smb_fname->st.st_ex_mode &= ~S_IFMT;
4379 smb_fname->st.st_ex_mode |= S_IFREG;
4380 smb_fname->st.st_ex_blocks =
4381 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4383 return rc;
4386 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4387 files_struct *fsp,
4388 SMB_STRUCT_STAT *sbuf)
4390 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4393 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4394 files_struct *fsp,
4395 SMB_STRUCT_STAT *sbuf)
4397 int ret;
4399 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4400 if (ret != 0) {
4401 return -1;
4404 *sbuf = fsp->base_fsp->fsp_name->st;
4405 sbuf->st_ex_size = AFP_INFO_SIZE;
4406 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4408 return 0;
4411 static int fruit_fstat_meta(vfs_handle_struct *handle,
4412 files_struct *fsp,
4413 SMB_STRUCT_STAT *sbuf,
4414 struct fio *fio)
4416 int ret;
4418 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4420 switch (fio->config->meta) {
4421 case FRUIT_META_STREAM:
4422 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4423 break;
4425 case FRUIT_META_NETATALK:
4426 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4427 break;
4429 default:
4430 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4431 return -1;
4434 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4435 return ret;
4438 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4439 files_struct *fsp,
4440 SMB_STRUCT_STAT *sbuf)
4442 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4445 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4446 files_struct *fsp,
4447 SMB_STRUCT_STAT *sbuf)
4449 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4452 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4453 files_struct *fsp,
4454 SMB_STRUCT_STAT *sbuf)
4456 struct adouble *ad = NULL;
4457 int ret;
4459 /* Populate the stat struct with info from the base file. */
4460 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4461 if (ret == -1) {
4462 return -1;
4465 ad = ad_get(talloc_tos(), handle,
4466 fsp->base_fsp->fsp_name,
4467 ADOUBLE_RSRC);
4468 if (ad == NULL) {
4469 DBG_ERR("ad_get [%s] failed [%s]\n",
4470 fsp_str_dbg(fsp), strerror(errno));
4471 return -1;
4474 *sbuf = fsp->base_fsp->fsp_name->st;
4475 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4476 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4478 TALLOC_FREE(ad);
4479 return 0;
4482 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4483 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4485 int ret;
4487 switch (fio->config->rsrc) {
4488 case FRUIT_RSRC_STREAM:
4489 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4490 break;
4492 case FRUIT_RSRC_ADFILE:
4493 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4494 break;
4496 case FRUIT_RSRC_XATTR:
4497 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4498 break;
4500 default:
4501 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4502 return -1;
4505 return ret;
4508 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4509 SMB_STRUCT_STAT *sbuf)
4511 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4512 int rc;
4514 if (fio == NULL) {
4515 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4518 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4520 if (fio->type == ADOUBLE_META) {
4521 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4522 } else {
4523 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4526 if (rc == 0) {
4527 sbuf->st_ex_mode &= ~S_IFMT;
4528 sbuf->st_ex_mode |= S_IFREG;
4529 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4532 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4533 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4534 return rc;
4537 static NTSTATUS fruit_streaminfo_meta_stream(
4538 vfs_handle_struct *handle,
4539 struct files_struct *fsp,
4540 const struct smb_filename *smb_fname,
4541 TALLOC_CTX *mem_ctx,
4542 unsigned int *pnum_streams,
4543 struct stream_struct **pstreams)
4545 struct stream_struct *stream = *pstreams;
4546 unsigned int num_streams = *pnum_streams;
4547 struct smb_filename *sname = NULL;
4548 int i;
4549 int ret;
4550 bool ok;
4552 for (i = 0; i < num_streams; i++) {
4553 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4554 break;
4558 if (i == num_streams) {
4559 return NT_STATUS_OK;
4562 if (stream[i].size == AFP_INFO_SIZE) {
4563 return NT_STATUS_OK;
4566 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4567 "from [%s]\n", (intmax_t)stream[i].size,
4568 smb_fname_str_dbg(smb_fname));
4570 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4571 if (!ok) {
4572 return NT_STATUS_INTERNAL_ERROR;
4575 sname = synthetic_smb_fname(talloc_tos(),
4576 smb_fname->base_name,
4577 AFPINFO_STREAM_NAME,
4578 NULL, 0);
4579 if (sname == NULL) {
4580 return NT_STATUS_NO_MEMORY;
4583 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4584 TALLOC_FREE(sname);
4585 if (ret != 0) {
4586 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4587 return map_nt_error_from_unix(errno);
4590 return NT_STATUS_OK;
4593 static NTSTATUS fruit_streaminfo_meta_netatalk(
4594 vfs_handle_struct *handle,
4595 struct files_struct *fsp,
4596 const struct smb_filename *smb_fname,
4597 TALLOC_CTX *mem_ctx,
4598 unsigned int *pnum_streams,
4599 struct stream_struct **pstreams)
4601 struct stream_struct *stream = *pstreams;
4602 unsigned int num_streams = *pnum_streams;
4603 struct adouble *ad = NULL;
4604 bool is_fi_empty;
4605 int i;
4606 bool ok;
4608 /* Remove the Netatalk xattr from the list */
4609 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4610 ":" NETATALK_META_XATTR ":$DATA");
4611 if (!ok) {
4612 return NT_STATUS_NO_MEMORY;
4616 * Check if there's a AFPINFO_STREAM from the VFS streams
4617 * backend and if yes, remove it from the list
4619 for (i = 0; i < num_streams; i++) {
4620 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4621 break;
4625 if (i < num_streams) {
4626 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4627 smb_fname_str_dbg(smb_fname));
4629 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4630 AFPINFO_STREAM);
4631 if (!ok) {
4632 return NT_STATUS_INTERNAL_ERROR;
4636 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4637 if (ad == NULL) {
4638 return NT_STATUS_OK;
4641 is_fi_empty = ad_empty_finderinfo(ad);
4642 TALLOC_FREE(ad);
4644 if (is_fi_empty) {
4645 return NT_STATUS_OK;
4648 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4649 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4650 smb_roundup(handle->conn, AFP_INFO_SIZE));
4651 if (!ok) {
4652 return NT_STATUS_NO_MEMORY;
4655 return NT_STATUS_OK;
4658 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4659 struct files_struct *fsp,
4660 const struct smb_filename *smb_fname,
4661 TALLOC_CTX *mem_ctx,
4662 unsigned int *pnum_streams,
4663 struct stream_struct **pstreams)
4665 struct fruit_config_data *config = NULL;
4666 NTSTATUS status;
4668 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4669 return NT_STATUS_INTERNAL_ERROR);
4671 switch (config->meta) {
4672 case FRUIT_META_NETATALK:
4673 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4674 mem_ctx, pnum_streams,
4675 pstreams);
4676 break;
4678 case FRUIT_META_STREAM:
4679 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4680 mem_ctx, pnum_streams,
4681 pstreams);
4682 break;
4684 default:
4685 return NT_STATUS_INTERNAL_ERROR;
4688 return status;
4691 static NTSTATUS fruit_streaminfo_rsrc_stream(
4692 vfs_handle_struct *handle,
4693 struct files_struct *fsp,
4694 const struct smb_filename *smb_fname,
4695 TALLOC_CTX *mem_ctx,
4696 unsigned int *pnum_streams,
4697 struct stream_struct **pstreams)
4699 bool ok;
4701 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4702 if (!ok) {
4703 DBG_ERR("Filtering resource stream failed\n");
4704 return NT_STATUS_INTERNAL_ERROR;
4706 return NT_STATUS_OK;
4709 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4710 vfs_handle_struct *handle,
4711 struct files_struct *fsp,
4712 const struct smb_filename *smb_fname,
4713 TALLOC_CTX *mem_ctx,
4714 unsigned int *pnum_streams,
4715 struct stream_struct **pstreams)
4717 bool ok;
4719 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4720 if (!ok) {
4721 DBG_ERR("Filtering resource stream failed\n");
4722 return NT_STATUS_INTERNAL_ERROR;
4724 return NT_STATUS_OK;
4727 static NTSTATUS fruit_streaminfo_rsrc_adouble(
4728 vfs_handle_struct *handle,
4729 struct files_struct *fsp,
4730 const struct smb_filename *smb_fname,
4731 TALLOC_CTX *mem_ctx,
4732 unsigned int *pnum_streams,
4733 struct stream_struct **pstreams)
4735 struct stream_struct *stream = *pstreams;
4736 unsigned int num_streams = *pnum_streams;
4737 struct adouble *ad = NULL;
4738 bool ok;
4739 size_t rlen;
4740 int i;
4743 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
4744 * and if yes, remove it from the list
4746 for (i = 0; i < num_streams; i++) {
4747 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
4748 break;
4752 if (i < num_streams) {
4753 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
4754 smb_fname_str_dbg(smb_fname));
4756 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4757 AFPRESOURCE_STREAM);
4758 if (!ok) {
4759 return NT_STATUS_INTERNAL_ERROR;
4763 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4764 if (ad == NULL) {
4765 return NT_STATUS_OK;
4768 rlen = ad_getentrylen(ad, ADEID_RFORK);
4769 TALLOC_FREE(ad);
4771 if (rlen == 0) {
4772 return NT_STATUS_OK;
4775 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4776 AFPRESOURCE_STREAM_NAME, rlen,
4777 smb_roundup(handle->conn, rlen));
4778 if (!ok) {
4779 return NT_STATUS_NO_MEMORY;
4782 return NT_STATUS_OK;
4785 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
4786 struct files_struct *fsp,
4787 const struct smb_filename *smb_fname,
4788 TALLOC_CTX *mem_ctx,
4789 unsigned int *pnum_streams,
4790 struct stream_struct **pstreams)
4792 struct fruit_config_data *config = NULL;
4793 NTSTATUS status;
4795 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4796 return NT_STATUS_INTERNAL_ERROR);
4798 switch (config->rsrc) {
4799 case FRUIT_RSRC_STREAM:
4800 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
4801 mem_ctx, pnum_streams,
4802 pstreams);
4803 break;
4805 case FRUIT_RSRC_XATTR:
4806 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
4807 mem_ctx, pnum_streams,
4808 pstreams);
4809 break;
4811 case FRUIT_RSRC_ADFILE:
4812 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
4813 mem_ctx, pnum_streams,
4814 pstreams);
4815 break;
4817 default:
4818 return NT_STATUS_INTERNAL_ERROR;
4821 return status;
4824 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
4825 struct files_struct *fsp,
4826 const struct smb_filename *smb_fname,
4827 TALLOC_CTX *mem_ctx,
4828 unsigned int *pnum_streams,
4829 struct stream_struct **pstreams)
4831 struct fruit_config_data *config = NULL;
4832 NTSTATUS status;
4834 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4835 return NT_STATUS_UNSUCCESSFUL);
4837 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4839 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
4840 pnum_streams, pstreams);
4841 if (!NT_STATUS_IS_OK(status)) {
4842 return status;
4845 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
4846 mem_ctx, pnum_streams, pstreams);
4847 if (!NT_STATUS_IS_OK(status)) {
4848 return status;
4851 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
4852 mem_ctx, pnum_streams, pstreams);
4853 if (!NT_STATUS_IS_OK(status)) {
4854 return status;
4857 return NT_STATUS_OK;
4860 static int fruit_ntimes(vfs_handle_struct *handle,
4861 const struct smb_filename *smb_fname,
4862 struct smb_file_time *ft)
4864 int rc = 0;
4865 struct adouble *ad = NULL;
4866 struct fruit_config_data *config = NULL;
4868 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4869 return -1);
4871 if ((config->meta != FRUIT_META_NETATALK) ||
4872 null_timespec(ft->create_time))
4874 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4877 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
4878 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
4880 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4881 if (ad == NULL) {
4882 goto exit;
4885 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
4886 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
4888 rc = ad_set(ad, smb_fname);
4890 exit:
4892 TALLOC_FREE(ad);
4893 if (rc != 0) {
4894 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
4895 return -1;
4897 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
4900 static int fruit_fallocate(struct vfs_handle_struct *handle,
4901 struct files_struct *fsp,
4902 uint32_t mode,
4903 off_t offset,
4904 off_t len)
4906 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4908 if (fio == NULL) {
4909 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
4912 /* Let the pwrite code path handle it. */
4913 errno = ENOSYS;
4914 return -1;
4917 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
4918 struct files_struct *fsp,
4919 off_t offset)
4921 if (offset == 0) {
4922 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
4925 #ifdef HAVE_ATTROPEN
4926 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4927 #endif
4928 return 0;
4931 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
4932 struct files_struct *fsp,
4933 off_t offset)
4935 int rc;
4936 struct adouble *ad = NULL;
4937 off_t ad_off;
4939 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4940 if (ad == NULL) {
4941 DBG_DEBUG("ad_get [%s] failed [%s]\n",
4942 fsp_str_dbg(fsp), strerror(errno));
4943 return -1;
4946 ad_off = ad_getentryoff(ad, ADEID_RFORK);
4948 rc = ftruncate(fsp->fh->fd, offset + ad_off);
4949 if (rc != 0) {
4950 TALLOC_FREE(ad);
4951 return -1;
4954 ad_setentrylen(ad, ADEID_RFORK, offset);
4956 rc = ad_fset(ad, fsp);
4957 if (rc != 0) {
4958 DBG_ERR("ad_fset [%s] failed [%s]\n",
4959 fsp_str_dbg(fsp), strerror(errno));
4960 TALLOC_FREE(ad);
4961 return -1;
4964 TALLOC_FREE(ad);
4965 return 0;
4968 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
4969 struct files_struct *fsp,
4970 off_t offset)
4972 if (offset == 0) {
4973 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4976 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
4979 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
4980 struct files_struct *fsp,
4981 off_t offset)
4983 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4984 int ret;
4986 switch (fio->config->rsrc) {
4987 case FRUIT_RSRC_XATTR:
4988 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
4989 break;
4991 case FRUIT_RSRC_ADFILE:
4992 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
4993 break;
4995 case FRUIT_RSRC_STREAM:
4996 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
4997 break;
4999 default:
5000 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5001 return -1;
5005 return ret;
5008 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5009 struct files_struct *fsp,
5010 off_t offset)
5012 if (offset > 60) {
5013 DBG_WARNING("ftruncate %s to %jd",
5014 fsp_str_dbg(fsp), (intmax_t)offset);
5015 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5016 errno = EOVERFLOW;
5017 return -1;
5020 /* OS X returns success but does nothing */
5021 DBG_INFO("ignoring ftruncate %s to %jd\n",
5022 fsp_str_dbg(fsp), (intmax_t)offset);
5023 return 0;
5026 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5027 struct files_struct *fsp,
5028 off_t offset)
5030 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5031 int ret;
5033 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5034 (intmax_t)offset);
5036 if (fio == NULL) {
5037 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5040 if (fio->type == ADOUBLE_META) {
5041 ret = fruit_ftruncate_meta(handle, fsp, offset);
5042 } else {
5043 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5046 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5047 return ret;
5050 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5051 struct smb_request *req,
5052 uint16_t root_dir_fid,
5053 struct smb_filename *smb_fname,
5054 uint32_t access_mask,
5055 uint32_t share_access,
5056 uint32_t create_disposition,
5057 uint32_t create_options,
5058 uint32_t file_attributes,
5059 uint32_t oplock_request,
5060 struct smb2_lease *lease,
5061 uint64_t allocation_size,
5062 uint32_t private_flags,
5063 struct security_descriptor *sd,
5064 struct ea_list *ea_list,
5065 files_struct **result,
5066 int *pinfo,
5067 const struct smb2_create_blobs *in_context_blobs,
5068 struct smb2_create_blobs *out_context_blobs)
5070 NTSTATUS status;
5071 struct fruit_config_data *config = NULL;
5072 files_struct *fsp = NULL;
5074 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5075 if (!NT_STATUS_IS_OK(status)) {
5076 goto fail;
5079 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5080 return NT_STATUS_UNSUCCESSFUL);
5082 status = SMB_VFS_NEXT_CREATE_FILE(
5083 handle, req, root_dir_fid, smb_fname,
5084 access_mask, share_access,
5085 create_disposition, create_options,
5086 file_attributes, oplock_request,
5087 lease,
5088 allocation_size, private_flags,
5089 sd, ea_list, result,
5090 pinfo, in_context_blobs, out_context_blobs);
5091 if (!NT_STATUS_IS_OK(status)) {
5092 return status;
5095 fsp = *result;
5097 if (global_fruit_config.nego_aapl) {
5098 if (config->posix_rename && fsp->is_directory) {
5100 * Enable POSIX directory rename behaviour
5102 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5107 * If this is a plain open for existing files, opening an 0
5108 * byte size resource fork MUST fail with
5109 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5111 * Cf the vfs_fruit torture tests in test_rfork_create().
5113 if (is_afpresource_stream(fsp->fsp_name) &&
5114 create_disposition == FILE_OPEN)
5116 if (fsp->fsp_name->st.st_ex_size == 0) {
5117 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5118 goto fail;
5122 if (is_ntfs_stream_smb_fname(smb_fname)
5123 || fsp->is_directory) {
5124 return status;
5127 if (config->locking == FRUIT_LOCKING_NETATALK) {
5128 status = fruit_check_access(
5129 handle, *result,
5130 access_mask,
5131 map_share_mode_to_deny_mode(share_access, 0));
5132 if (!NT_STATUS_IS_OK(status)) {
5133 goto fail;
5137 return status;
5139 fail:
5140 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5142 if (fsp) {
5143 close_file(req, fsp, ERROR_CLOSE);
5144 *result = fsp = NULL;
5147 return status;
5150 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5151 const struct smb_filename *fname,
5152 TALLOC_CTX *mem_ctx,
5153 struct readdir_attr_data **pattr_data)
5155 struct fruit_config_data *config = NULL;
5156 struct readdir_attr_data *attr_data;
5157 NTSTATUS status;
5159 SMB_VFS_HANDLE_GET_DATA(handle, config,
5160 struct fruit_config_data,
5161 return NT_STATUS_UNSUCCESSFUL);
5163 if (!global_fruit_config.nego_aapl) {
5164 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5167 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5169 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5170 if (*pattr_data == NULL) {
5171 return NT_STATUS_UNSUCCESSFUL;
5173 attr_data = *pattr_data;
5174 attr_data->type = RDATTR_AAPL;
5177 * Mac metadata: compressed FinderInfo, resource fork length
5178 * and creation date
5180 status = readdir_attr_macmeta(handle, fname, attr_data);
5181 if (!NT_STATUS_IS_OK(status)) {
5183 * Error handling is tricky: if we return failure from
5184 * this function, the corresponding directory entry
5185 * will to be passed to the client, so we really just
5186 * want to error out on fatal errors.
5188 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5189 goto fail;
5194 * UNIX mode
5196 if (config->unix_info_enabled) {
5197 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5201 * max_access
5203 if (!config->readdir_attr_max_access) {
5204 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5205 } else {
5206 status = smbd_calculate_access_mask(
5207 handle->conn,
5208 fname,
5209 false,
5210 SEC_FLAG_MAXIMUM_ALLOWED,
5211 &attr_data->attr_data.aapl.max_access);
5212 if (!NT_STATUS_IS_OK(status)) {
5213 goto fail;
5217 return NT_STATUS_OK;
5219 fail:
5220 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5221 fname->base_name, nt_errstr(status)));
5222 TALLOC_FREE(*pattr_data);
5223 return status;
5226 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5227 files_struct *fsp,
5228 uint32_t security_info,
5229 TALLOC_CTX *mem_ctx,
5230 struct security_descriptor **ppdesc)
5232 NTSTATUS status;
5233 struct security_ace ace;
5234 struct dom_sid sid;
5235 struct fruit_config_data *config;
5237 SMB_VFS_HANDLE_GET_DATA(handle, config,
5238 struct fruit_config_data,
5239 return NT_STATUS_UNSUCCESSFUL);
5241 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5242 mem_ctx, ppdesc);
5243 if (!NT_STATUS_IS_OK(status)) {
5244 return status;
5248 * Add MS NFS style ACEs with uid, gid and mode
5250 if (!global_fruit_config.nego_aapl) {
5251 return NT_STATUS_OK;
5253 if (!config->unix_info_enabled) {
5254 return NT_STATUS_OK;
5257 /* MS NFS style mode */
5258 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5259 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5260 status = security_descriptor_dacl_add(*ppdesc, &ace);
5261 if (!NT_STATUS_IS_OK(status)) {
5262 DEBUG(1,("failed to add MS NFS style ACE\n"));
5263 return status;
5266 /* MS NFS style uid */
5267 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5268 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5269 status = security_descriptor_dacl_add(*ppdesc, &ace);
5270 if (!NT_STATUS_IS_OK(status)) {
5271 DEBUG(1,("failed to add MS NFS style ACE\n"));
5272 return status;
5275 /* MS NFS style gid */
5276 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5277 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5278 status = security_descriptor_dacl_add(*ppdesc, &ace);
5279 if (!NT_STATUS_IS_OK(status)) {
5280 DEBUG(1,("failed to add MS NFS style ACE\n"));
5281 return status;
5284 return NT_STATUS_OK;
5287 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5288 files_struct *fsp,
5289 uint32_t security_info_sent,
5290 const struct security_descriptor *psd)
5292 NTSTATUS status;
5293 bool do_chmod;
5294 mode_t ms_nfs_mode = 0;
5295 int result;
5297 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5299 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5300 if (!NT_STATUS_IS_OK(status)) {
5301 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5302 return status;
5305 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5306 if (!NT_STATUS_IS_OK(status)) {
5307 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5308 return status;
5311 if (do_chmod) {
5312 if (fsp->fh->fd != -1) {
5313 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5314 } else {
5315 result = SMB_VFS_CHMOD(fsp->conn,
5316 fsp->fsp_name,
5317 ms_nfs_mode);
5320 if (result != 0) {
5321 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5322 result, (unsigned)ms_nfs_mode,
5323 strerror(errno)));
5324 status = map_nt_error_from_unix(errno);
5325 return status;
5329 return NT_STATUS_OK;
5332 static struct vfs_offload_ctx *fruit_offload_ctx;
5334 struct fruit_offload_read_state {
5335 struct vfs_handle_struct *handle;
5336 struct tevent_context *ev;
5337 files_struct *fsp;
5338 uint32_t fsctl;
5339 DATA_BLOB token;
5342 static void fruit_offload_read_done(struct tevent_req *subreq);
5344 static struct tevent_req *fruit_offload_read_send(
5345 TALLOC_CTX *mem_ctx,
5346 struct tevent_context *ev,
5347 struct vfs_handle_struct *handle,
5348 files_struct *fsp,
5349 uint32_t fsctl,
5350 uint32_t ttl,
5351 off_t offset,
5352 size_t to_copy)
5354 struct tevent_req *req = NULL;
5355 struct tevent_req *subreq = NULL;
5356 struct fruit_offload_read_state *state = NULL;
5358 req = tevent_req_create(mem_ctx, &state,
5359 struct fruit_offload_read_state);
5360 if (req == NULL) {
5361 return NULL;
5363 *state = (struct fruit_offload_read_state) {
5364 .handle = handle,
5365 .ev = ev,
5366 .fsp = fsp,
5367 .fsctl = fsctl,
5370 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5371 fsctl, ttl, offset, to_copy);
5372 if (tevent_req_nomem(subreq, req)) {
5373 return tevent_req_post(req, ev);
5375 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5376 return req;
5379 static void fruit_offload_read_done(struct tevent_req *subreq)
5381 struct tevent_req *req = tevent_req_callback_data(
5382 subreq, struct tevent_req);
5383 struct fruit_offload_read_state *state = tevent_req_data(
5384 req, struct fruit_offload_read_state);
5385 NTSTATUS status;
5387 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5388 state->handle,
5389 state,
5390 &state->token);
5391 TALLOC_FREE(subreq);
5392 if (tevent_req_nterror(req, status)) {
5393 return;
5396 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5397 tevent_req_done(req);
5398 return;
5401 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5402 &fruit_offload_ctx);
5403 if (tevent_req_nterror(req, status)) {
5404 return;
5407 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5408 state->fsp,
5409 &state->token);
5410 if (tevent_req_nterror(req, status)) {
5411 return;
5414 tevent_req_done(req);
5415 return;
5418 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5419 struct vfs_handle_struct *handle,
5420 TALLOC_CTX *mem_ctx,
5421 DATA_BLOB *token)
5423 struct fruit_offload_read_state *state = tevent_req_data(
5424 req, struct fruit_offload_read_state);
5425 NTSTATUS status;
5427 if (tevent_req_is_nterror(req, &status)) {
5428 tevent_req_received(req);
5429 return status;
5432 token->length = state->token.length;
5433 token->data = talloc_move(mem_ctx, &state->token.data);
5435 tevent_req_received(req);
5436 return NT_STATUS_OK;
5439 struct fruit_offload_write_state {
5440 struct vfs_handle_struct *handle;
5441 off_t copied;
5442 struct files_struct *src_fsp;
5443 struct files_struct *dst_fsp;
5444 bool is_copyfile;
5447 static void fruit_offload_write_done(struct tevent_req *subreq);
5448 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
5449 TALLOC_CTX *mem_ctx,
5450 struct tevent_context *ev,
5451 uint32_t fsctl,
5452 DATA_BLOB *token,
5453 off_t transfer_offset,
5454 struct files_struct *dest_fsp,
5455 off_t dest_off,
5456 off_t num)
5458 struct tevent_req *req, *subreq;
5459 struct fruit_offload_write_state *state;
5460 NTSTATUS status;
5461 struct fruit_config_data *config;
5462 off_t src_off = transfer_offset;
5463 files_struct *src_fsp = NULL;
5464 off_t to_copy = num;
5465 bool copyfile_enabled = false;
5467 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5468 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5470 SMB_VFS_HANDLE_GET_DATA(handle, config,
5471 struct fruit_config_data,
5472 return NULL);
5474 req = tevent_req_create(mem_ctx, &state,
5475 struct fruit_offload_write_state);
5476 if (req == NULL) {
5477 return NULL;
5479 state->handle = handle;
5480 state->dst_fsp = dest_fsp;
5482 switch (fsctl) {
5483 case FSCTL_SRV_COPYCHUNK:
5484 case FSCTL_SRV_COPYCHUNK_WRITE:
5485 copyfile_enabled = config->copyfile_enabled;
5486 break;
5487 default:
5488 break;
5492 * Check if this a OS X copyfile style copychunk request with
5493 * a requested chunk count of 0 that was translated to a
5494 * offload_write_send VFS call overloading the parameters src_off
5495 * = dest_off = num = 0.
5497 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
5498 status = vfs_offload_token_db_fetch_fsp(
5499 fruit_offload_ctx, token, &src_fsp);
5500 if (tevent_req_nterror(req, status)) {
5501 return tevent_req_post(req, ev);
5503 state->src_fsp = src_fsp;
5505 status = vfs_stat_fsp(src_fsp);
5506 if (tevent_req_nterror(req, status)) {
5507 return tevent_req_post(req, ev);
5510 to_copy = src_fsp->fsp_name->st.st_ex_size;
5511 state->is_copyfile = true;
5514 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
5515 mem_ctx,
5517 fsctl,
5518 token,
5519 transfer_offset,
5520 dest_fsp,
5521 dest_off,
5522 to_copy);
5523 if (tevent_req_nomem(subreq, req)) {
5524 return tevent_req_post(req, ev);
5527 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
5528 return req;
5531 static void fruit_offload_write_done(struct tevent_req *subreq)
5533 struct tevent_req *req = tevent_req_callback_data(
5534 subreq, struct tevent_req);
5535 struct fruit_offload_write_state *state = tevent_req_data(
5536 req, struct fruit_offload_write_state);
5537 NTSTATUS status;
5538 unsigned int num_streams = 0;
5539 struct stream_struct *streams = NULL;
5540 unsigned int i;
5541 struct smb_filename *src_fname_tmp = NULL;
5542 struct smb_filename *dst_fname_tmp = NULL;
5544 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
5545 subreq,
5546 &state->copied);
5547 TALLOC_FREE(subreq);
5548 if (tevent_req_nterror(req, status)) {
5549 return;
5552 if (!state->is_copyfile) {
5553 tevent_req_done(req);
5554 return;
5558 * Now copy all remaining streams. We know the share supports
5559 * streams, because we're in vfs_fruit. We don't do this async
5560 * because streams are few and small.
5562 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5563 state->src_fsp->fsp_name,
5564 req, &num_streams, &streams);
5565 if (tevent_req_nterror(req, status)) {
5566 return;
5569 if (num_streams == 1) {
5570 /* There is always one stream, ::$DATA. */
5571 tevent_req_done(req);
5572 return;
5575 for (i = 0; i < num_streams; i++) {
5576 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5577 __func__, streams[i].name, (size_t)streams[i].size));
5579 src_fname_tmp = synthetic_smb_fname(
5580 req,
5581 state->src_fsp->fsp_name->base_name,
5582 streams[i].name,
5583 NULL,
5584 state->src_fsp->fsp_name->flags);
5585 if (tevent_req_nomem(src_fname_tmp, req)) {
5586 return;
5589 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5590 TALLOC_FREE(src_fname_tmp);
5591 continue;
5594 dst_fname_tmp = synthetic_smb_fname(
5595 req,
5596 state->dst_fsp->fsp_name->base_name,
5597 streams[i].name,
5598 NULL,
5599 state->dst_fsp->fsp_name->flags);
5600 if (tevent_req_nomem(dst_fname_tmp, req)) {
5601 TALLOC_FREE(src_fname_tmp);
5602 return;
5605 status = copy_file(req,
5606 state->handle->conn,
5607 src_fname_tmp,
5608 dst_fname_tmp,
5609 OPENX_FILE_CREATE_IF_NOT_EXIST,
5610 0, false);
5611 if (!NT_STATUS_IS_OK(status)) {
5612 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5613 smb_fname_str_dbg(src_fname_tmp),
5614 smb_fname_str_dbg(dst_fname_tmp),
5615 nt_errstr(status)));
5616 TALLOC_FREE(src_fname_tmp);
5617 TALLOC_FREE(dst_fname_tmp);
5618 tevent_req_nterror(req, status);
5619 return;
5622 TALLOC_FREE(src_fname_tmp);
5623 TALLOC_FREE(dst_fname_tmp);
5626 TALLOC_FREE(streams);
5627 TALLOC_FREE(src_fname_tmp);
5628 TALLOC_FREE(dst_fname_tmp);
5629 tevent_req_done(req);
5632 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
5633 struct tevent_req *req,
5634 off_t *copied)
5636 struct fruit_offload_write_state *state = tevent_req_data(
5637 req, struct fruit_offload_write_state);
5638 NTSTATUS status;
5640 if (tevent_req_is_nterror(req, &status)) {
5641 DEBUG(1, ("server side copy chunk failed: %s\n",
5642 nt_errstr(status)));
5643 *copied = 0;
5644 tevent_req_received(req);
5645 return status;
5648 *copied = state->copied;
5649 tevent_req_received(req);
5651 return NT_STATUS_OK;
5654 static struct vfs_fn_pointers vfs_fruit_fns = {
5655 .connect_fn = fruit_connect,
5657 /* File operations */
5658 .chmod_fn = fruit_chmod,
5659 .chown_fn = fruit_chown,
5660 .unlink_fn = fruit_unlink,
5661 .rename_fn = fruit_rename,
5662 .rmdir_fn = fruit_rmdir,
5663 .open_fn = fruit_open,
5664 .pread_fn = fruit_pread,
5665 .pwrite_fn = fruit_pwrite,
5666 .pread_send_fn = fruit_pread_send,
5667 .pread_recv_fn = fruit_pread_recv,
5668 .pwrite_send_fn = fruit_pwrite_send,
5669 .pwrite_recv_fn = fruit_pwrite_recv,
5670 .stat_fn = fruit_stat,
5671 .lstat_fn = fruit_lstat,
5672 .fstat_fn = fruit_fstat,
5673 .streaminfo_fn = fruit_streaminfo,
5674 .ntimes_fn = fruit_ntimes,
5675 .ftruncate_fn = fruit_ftruncate,
5676 .fallocate_fn = fruit_fallocate,
5677 .create_file_fn = fruit_create_file,
5678 .readdir_attr_fn = fruit_readdir_attr,
5679 .offload_read_send_fn = fruit_offload_read_send,
5680 .offload_read_recv_fn = fruit_offload_read_recv,
5681 .offload_write_send_fn = fruit_offload_write_send,
5682 .offload_write_recv_fn = fruit_offload_write_recv,
5684 /* NT ACL operations */
5685 .fget_nt_acl_fn = fruit_fget_nt_acl,
5686 .fset_nt_acl_fn = fruit_fset_nt_acl,
5689 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
5690 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5692 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5693 &vfs_fruit_fns);
5694 if (!NT_STATUS_IS_OK(ret)) {
5695 return ret;
5698 vfs_fruit_debug_level = debug_add_class("fruit");
5699 if (vfs_fruit_debug_level == -1) {
5700 vfs_fruit_debug_level = DBGC_VFS;
5701 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5702 "vfs_fruit_init"));
5703 } else {
5704 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5705 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5708 return ret;