vfs_fruit: delete 0 byte size streams if AAPL is enabled
[Samba.git] / source3 / modules / vfs_fruit.c
blob47034deaca5ac82605fbabfa9c785aa04c9b8d09
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"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
58 * share modes.
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
65 * blobs content.
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
77 * Extended Attributes
78 * ===================
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
90 * TODO
91 * ====
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
95 * - add tests
98 static int vfs_fruit_debug_level = DBGC_VFS;
100 static struct global_fruit_config {
101 bool nego_aapl; /* client negotiated AAPL */
103 } global_fruit_config;
105 #undef DBGC_CLASS
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
117 #else
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
120 #endif
122 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
124 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
125 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
126 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
127 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
129 struct fruit_config_data {
130 enum fruit_rsrc rsrc;
131 enum fruit_meta meta;
132 enum fruit_locking locking;
133 enum fruit_encoding encoding;
134 bool use_aapl; /* config from smb.conf */
135 bool use_copyfile;
136 bool readdir_attr_enabled;
137 bool unix_info_enabled;
138 bool copyfile_enabled;
139 bool veto_appledouble;
140 bool posix_rename;
141 bool aapl_zero_file_id;
142 const char *model;
145 * Additional options, all enabled by default,
146 * possibly useful for analyzing performance. The associated
147 * operations with each of them may be expensive, so having
148 * the chance to disable them individually gives a chance
149 * tweaking the setup for the particular usecase.
151 bool readdir_attr_rsize;
152 bool readdir_attr_finder_info;
153 bool readdir_attr_max_access;
156 static const struct enum_list fruit_rsrc[] = {
157 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
159 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
160 { -1, NULL}
163 static const struct enum_list fruit_meta[] = {
164 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
165 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
166 { -1, NULL}
169 static const struct enum_list fruit_locking[] = {
170 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
171 {FRUIT_LOCKING_NONE, "none"},
172 { -1, NULL}
175 static const struct enum_list fruit_encoding[] = {
176 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
177 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
178 { -1, NULL}
181 static const char *fruit_catia_maps =
182 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
183 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
184 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
185 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
186 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
187 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
188 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
189 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
190 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
191 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
192 "0x0d:0xf00d";
194 /*****************************************************************************
195 * Defines, functions and data structures that deal with AppleDouble
196 *****************************************************************************/
199 * There are two AppleDouble blobs we deal with:
201 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
202 * metadata in an xattr
204 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
205 * ._ files
207 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
209 /* Version info */
210 #define AD_VERSION2 0x00020000
211 #define AD_VERSION AD_VERSION2
214 * AppleDouble entry IDs.
216 #define ADEID_DFORK 1
217 #define ADEID_RFORK 2
218 #define ADEID_NAME 3
219 #define ADEID_COMMENT 4
220 #define ADEID_ICONBW 5
221 #define ADEID_ICONCOL 6
222 #define ADEID_FILEI 7
223 #define ADEID_FILEDATESI 8
224 #define ADEID_FINDERI 9
225 #define ADEID_MACFILEI 10
226 #define ADEID_PRODOSFILEI 11
227 #define ADEID_MSDOSFILEI 12
228 #define ADEID_SHORTNAME 13
229 #define ADEID_AFPFILEI 14
230 #define ADEID_DID 15
232 /* Private Netatalk entries */
233 #define ADEID_PRIVDEV 16
234 #define ADEID_PRIVINO 17
235 #define ADEID_PRIVSYN 18
236 #define ADEID_PRIVID 19
237 #define ADEID_MAX (ADEID_PRIVID + 1)
240 * These are the real ids for the private entries,
241 * as stored in the adouble file
243 #define AD_DEV 0x80444556
244 #define AD_INO 0x80494E4F
245 #define AD_SYN 0x8053594E
246 #define AD_ID 0x8053567E
248 /* Number of actually used entries */
249 #define ADEID_NUM_XATTR 8
250 #define ADEID_NUM_DOT_UND 2
251 #define ADEID_NUM_RSRC_XATTR 1
253 /* AppleDouble magic */
254 #define AD_APPLESINGLE_MAGIC 0x00051600
255 #define AD_APPLEDOUBLE_MAGIC 0x00051607
256 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
258 /* Sizes of relevant entry bits */
259 #define ADEDLEN_MAGIC 4
260 #define ADEDLEN_VERSION 4
261 #define ADEDLEN_FILLER 16
262 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
263 #define ADEDLEN_NENTRIES 2
264 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
265 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
266 #define AD_ENTRY_LEN_EID 4
267 #define AD_ENTRY_LEN_OFF 4
268 #define AD_ENTRY_LEN_LEN 4
269 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
271 /* Field widths */
272 #define ADEDLEN_NAME 255
273 #define ADEDLEN_COMMENT 200
274 #define ADEDLEN_FILEI 16
275 #define ADEDLEN_FINDERI 32
276 #define ADEDLEN_FILEDATESI 16
277 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
278 #define ADEDLEN_AFPFILEI 4
279 #define ADEDLEN_MACFILEI 4
280 #define ADEDLEN_PRODOSFILEI 8
281 #define ADEDLEN_MSDOSFILEI 2
282 #define ADEDLEN_DID 4
283 #define ADEDLEN_PRIVDEV 8
284 #define ADEDLEN_PRIVINO 8
285 #define ADEDLEN_PRIVSYN 8
286 #define ADEDLEN_PRIVID 4
288 /* Offsets */
289 #define ADEDOFF_MAGIC 0
290 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
291 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
292 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
294 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
295 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
296 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
297 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
298 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
299 ADEDLEN_FILEDATESI)
300 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
301 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
302 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
303 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
305 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
306 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
307 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
309 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
310 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
311 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
312 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
313 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
314 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
316 #if AD_DATASZ_XATTR != 402
317 #error bad size for AD_DATASZ_XATTR
318 #endif
320 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
321 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
322 ADEDLEN_FINDERI)
323 #if AD_DATASZ_DOT_UND != 82
324 #error bad size for AD_DATASZ_DOT_UND
325 #endif
328 * Sharemode locks fcntl() offsets
330 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
331 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
332 #else
333 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
334 #endif
335 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
337 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
338 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
339 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
340 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
341 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
342 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
343 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
344 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
345 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
346 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
348 /* Time stuff we overload the bits a little */
349 #define AD_DATE_CREATE 0
350 #define AD_DATE_MODIFY 4
351 #define AD_DATE_BACKUP 8
352 #define AD_DATE_ACCESS 12
353 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
354 AD_DATE_BACKUP | AD_DATE_ACCESS)
355 #define AD_DATE_UNIX (1 << 10)
356 #define AD_DATE_START 0x80000000
357 #define AD_DATE_DELTA 946684800
358 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
359 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
361 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
362 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
363 #define AD_XATTR_HDR_SIZE 36
364 #define AD_XATTR_MAX_HDR_SIZE 65536
366 /* Accessor macros */
367 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
368 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
369 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
370 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
373 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
374 * representation as well as the on-disk format.
376 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
377 * the length of the FinderInfo entry is larger then 32 bytes. It is then
378 * preceeded with 2 bytes padding.
380 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
383 struct ad_xattr_header {
384 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
385 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
386 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
387 uint32_t adx_data_start; /* file offset to attribute data area */
388 uint32_t adx_data_length; /* length of attribute data area */
389 uint32_t adx_reserved[3];
390 uint16_t adx_flags;
391 uint16_t adx_num_attrs;
394 /* On-disk entries are aligned on 4 byte boundaries */
395 struct ad_xattr_entry {
396 uint32_t adx_offset; /* file offset to data */
397 uint32_t adx_length; /* size of attribute data */
398 uint16_t adx_flags;
399 uint8_t adx_namelen; /* included the NULL terminator */
400 char *adx_name; /* NULL-terminated UTF-8 name */
403 struct ad_entry {
404 size_t ade_off;
405 size_t ade_len;
408 struct adouble {
409 vfs_handle_struct *ad_handle;
410 int ad_fd;
411 bool ad_opened;
412 adouble_type_t ad_type;
413 uint32_t ad_magic;
414 uint32_t ad_version;
415 struct ad_entry ad_eid[ADEID_MAX];
416 char *ad_data;
417 struct ad_xattr_header adx_header;
418 struct ad_xattr_entry *adx_entries;
421 struct ad_entry_order {
422 uint32_t id, offset, len;
425 /* Netatalk AppleDouble metadata xattr */
426 static const
427 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
428 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
429 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
430 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
431 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
432 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
433 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
434 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
435 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
436 {0, 0, 0}
439 /* AppleDouble resource fork file (the ones prefixed by "._") */
440 static const
441 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
442 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
443 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
444 {0, 0, 0}
448 * Fake AppleDouble entry oder for resource fork xattr. The xattr
449 * isn't an AppleDouble file, it simply contains the resource data,
450 * but in order to be able to use some API calls like ad_getentryoff()
451 * we build a fake/helper struct adouble with this entry order struct.
453 static const
454 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
455 {ADEID_RFORK, 0, 0},
456 {0, 0, 0}
459 /* Conversion from enumerated id to on-disk AppleDouble id */
460 #define AD_EID_DISK(a) (set_eid[a])
461 static const uint32_t set_eid[] = {
462 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
463 AD_DEV, AD_INO, AD_SYN, AD_ID
466 struct fio {
467 /* tcon config handle */
468 struct fruit_config_data *config;
470 /* Denote stream type, meta or rsrc */
471 adouble_type_t type;
475 * Forward declarations
477 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
478 adouble_type_t type);
479 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
480 static int ad_fset(struct adouble *ad, files_struct *fsp);
481 static int adouble_path(TALLOC_CTX *ctx,
482 const struct smb_filename *smb_fname__in,
483 struct smb_filename **ppsmb_fname_out);
484 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
485 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
486 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
490 * Return a pointer to an AppleDouble entry
492 * Returns NULL if the entry is not present
494 static char *ad_get_entry(const struct adouble *ad, int eid)
496 off_t off = ad_getentryoff(ad, eid);
497 size_t len = ad_getentrylen(ad, eid);
499 if (off == 0 || len == 0) {
500 return NULL;
503 return ad->ad_data + off;
507 * Get a date
509 static int ad_getdate(const struct adouble *ad,
510 unsigned int dateoff,
511 uint32_t *date)
513 bool xlate = (dateoff & AD_DATE_UNIX);
514 char *p = NULL;
516 dateoff &= AD_DATE_MASK;
517 p = ad_get_entry(ad, ADEID_FILEDATESI);
518 if (p == NULL) {
519 return -1;
522 if (dateoff > AD_DATE_ACCESS) {
523 return -1;
526 memcpy(date, p + dateoff, sizeof(uint32_t));
528 if (xlate) {
529 *date = AD_DATE_TO_UNIX(*date);
531 return 0;
535 * Set a date
537 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
539 bool xlate = (dateoff & AD_DATE_UNIX);
540 char *p = NULL;
542 p = ad_get_entry(ad, ADEID_FILEDATESI);
543 if (p == NULL) {
544 return -1;
547 dateoff &= AD_DATE_MASK;
548 if (xlate) {
549 date = AD_DATE_FROM_UNIX(date);
552 if (dateoff > AD_DATE_ACCESS) {
553 return -1;
556 memcpy(p + dateoff, &date, sizeof(date));
558 return 0;
563 * Map on-disk AppleDouble id to enumerated id
565 static uint32_t get_eid(uint32_t eid)
567 if (eid <= 15) {
568 return eid;
571 switch (eid) {
572 case AD_DEV:
573 return ADEID_PRIVDEV;
574 case AD_INO:
575 return ADEID_PRIVINO;
576 case AD_SYN:
577 return ADEID_PRIVSYN;
578 case AD_ID:
579 return ADEID_PRIVID;
580 default:
581 break;
584 return 0;
588 * Pack AppleDouble structure into data buffer
590 static bool ad_pack(struct adouble *ad)
592 uint32_t eid;
593 uint16_t nent;
594 uint32_t bufsize;
595 uint32_t offset = 0;
597 bufsize = talloc_get_size(ad->ad_data);
598 if (bufsize < AD_DATASZ_DOT_UND) {
599 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
600 return false;
603 if (offset + ADEDLEN_MAGIC < offset ||
604 offset + ADEDLEN_MAGIC >= bufsize) {
605 return false;
607 RSIVAL(ad->ad_data, offset, ad->ad_magic);
608 offset += ADEDLEN_MAGIC;
610 if (offset + ADEDLEN_VERSION < offset ||
611 offset + ADEDLEN_VERSION >= bufsize) {
612 return false;
614 RSIVAL(ad->ad_data, offset, ad->ad_version);
615 offset += ADEDLEN_VERSION;
617 if (offset + ADEDLEN_FILLER < offset ||
618 offset + ADEDLEN_FILLER >= bufsize) {
619 return false;
621 if (ad->ad_type == ADOUBLE_RSRC) {
622 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
624 offset += ADEDLEN_FILLER;
626 if (offset + ADEDLEN_NENTRIES < offset ||
627 offset + ADEDLEN_NENTRIES >= bufsize) {
628 return false;
630 offset += ADEDLEN_NENTRIES;
632 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
633 if (ad->ad_eid[eid].ade_off == 0) {
635 * ade_off is also used as indicator whether a
636 * specific entry is used or not
638 continue;
641 if (offset + AD_ENTRY_LEN_EID < offset ||
642 offset + AD_ENTRY_LEN_EID >= bufsize) {
643 return false;
645 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
646 offset += AD_ENTRY_LEN_EID;
648 if (offset + AD_ENTRY_LEN_OFF < offset ||
649 offset + AD_ENTRY_LEN_OFF >= bufsize) {
650 return false;
652 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
653 offset += AD_ENTRY_LEN_OFF;
655 if (offset + AD_ENTRY_LEN_LEN < offset ||
656 offset + AD_ENTRY_LEN_LEN >= bufsize) {
657 return false;
659 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
660 offset += AD_ENTRY_LEN_LEN;
662 nent++;
665 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
666 return false;
668 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
670 return true;
673 static bool ad_unpack_xattrs(struct adouble *ad)
675 struct ad_xattr_header *h = &ad->adx_header;
676 const char *p = ad->ad_data;
677 uint32_t hoff;
678 uint32_t i;
680 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
681 return true;
684 /* 2 bytes padding */
685 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
687 h->adx_magic = RIVAL(p, hoff + 0);
688 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
689 h->adx_total_size = RIVAL(p, hoff + 8);
690 h->adx_data_start = RIVAL(p, hoff + 12);
691 h->adx_data_length = RIVAL(p, hoff + 16);
692 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
693 h->adx_num_attrs = RSVAL(p, hoff + 34);
695 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
696 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
697 return false;
700 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
701 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
702 return false;
704 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
705 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
706 return false;
709 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
710 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
711 return false;
714 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
715 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
716 return false;
718 if ((h->adx_data_start + h->adx_data_length) >
719 ad->adx_header.adx_total_size)
721 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
722 return false;
725 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
726 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
727 return false;
730 if (h->adx_num_attrs == 0) {
731 return true;
734 ad->adx_entries = talloc_zero_array(
735 ad, struct ad_xattr_entry, h->adx_num_attrs);
736 if (ad->adx_entries == NULL) {
737 return false;
740 hoff += AD_XATTR_HDR_SIZE;
742 for (i = 0; i < h->adx_num_attrs; i++) {
743 struct ad_xattr_entry *e = &ad->adx_entries[i];
745 hoff = (hoff + 3) & ~3;
747 e->adx_offset = RIVAL(p, hoff + 0);
748 e->adx_length = RIVAL(p, hoff + 4);
749 e->adx_flags = RSVAL(p, hoff + 8);
750 e->adx_namelen = *(p + hoff + 10);
752 if (e->adx_offset >= ad->adx_header.adx_total_size) {
753 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
754 e->adx_offset);
755 return false;
758 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
759 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
760 e->adx_length);
761 return false;
764 if ((e->adx_offset + e->adx_length) >
765 ad->adx_header.adx_total_size)
767 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
768 e->adx_length);
769 return false;
772 if (e->adx_namelen == 0) {
773 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
774 e->adx_namelen);
775 return false;
777 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
778 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
779 e->adx_namelen);
780 return false;
782 if ((hoff + 11 + e->adx_namelen) >
783 ad->adx_header.adx_data_start)
785 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
786 e->adx_namelen);
787 return false;
790 e->adx_name = talloc_strndup(ad->adx_entries,
791 p + hoff + 11,
792 e->adx_namelen);
793 if (e->adx_name == NULL) {
794 return false;
797 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
798 e->adx_name, e->adx_offset, e->adx_length);
799 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
800 e->adx_length);
802 hoff += 11 + e->adx_namelen;
805 return true;
809 * Unpack an AppleDouble blob into a struct adoble
811 static bool ad_unpack(struct adouble *ad, const size_t nentries,
812 size_t filesize)
814 size_t bufsize = talloc_get_size(ad->ad_data);
815 size_t adentries, i;
816 uint32_t eid, len, off;
817 bool ok;
820 * The size of the buffer ad->ad_data is checked when read, so
821 * we wouldn't have to check our own offsets, a few extra
822 * checks won't hurt though. We have to check the offsets we
823 * read from the buffer anyway.
826 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
827 DEBUG(1, ("bad size\n"));
828 return false;
831 ad->ad_magic = RIVAL(ad->ad_data, 0);
832 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
833 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
834 DEBUG(1, ("wrong magic or version\n"));
835 return false;
838 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
839 if (adentries != nentries) {
840 DEBUG(1, ("invalid number of entries: %zu\n",
841 adentries));
842 return false;
845 /* now, read in the entry bits */
846 for (i = 0; i < adentries; i++) {
847 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
848 eid = get_eid(eid);
849 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
850 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
852 if (!eid || eid > ADEID_MAX) {
853 DEBUG(1, ("bogus eid %d\n", eid));
854 return false;
858 * All entries other than the resource fork are
859 * expected to be read into the ad_data buffer, so
860 * ensure the specified offset is within that bound
862 if ((off > bufsize) && (eid != ADEID_RFORK)) {
863 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
864 eid, off, len));
865 return false;
869 * All entries besides FinderInfo and resource fork
870 * must fit into the buffer. FinderInfo is special as
871 * it may be larger then the default 32 bytes (if it
872 * contains marshalled xattrs), but we will fixup that
873 * in ad_convert(). And the resource fork is never
874 * accessed directly by the ad_data buf (also see
875 * comment above) anyway.
877 if ((eid != ADEID_RFORK) &&
878 (eid != ADEID_FINDERI) &&
879 ((off + len) > bufsize)) {
880 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
881 eid, off, len));
882 return false;
886 * That would be obviously broken
888 if (off > filesize) {
889 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
890 eid, off, len));
891 return false;
895 * Check for any entry that has its end beyond the
896 * filesize.
898 if (off + len < off) {
899 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
900 ", len: %" PRIu32 "\n",
901 eid, off, len));
902 return false;
905 if (off + len > filesize) {
907 * If this is the resource fork entry, we fix
908 * up the length, for any other entry we bail
909 * out.
911 if (eid != ADEID_RFORK) {
912 DEBUG(1, ("bogus eid %d: off: %" PRIu32
913 ", len: %" PRIu32 "\n",
914 eid, off, len));
915 return false;
919 * Fixup the resource fork entry by limiting
920 * the size to entryoffset - filesize.
922 len = filesize - off;
923 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
924 ", len: %" PRIu32 "\n", off, len));
927 ad->ad_eid[eid].ade_off = off;
928 ad->ad_eid[eid].ade_len = len;
931 ok = ad_unpack_xattrs(ad);
932 if (!ok) {
933 return false;
936 return true;
939 static bool ad_convert_xattr(struct adouble *ad,
940 const struct smb_filename *smb_fname,
941 char *map)
943 static struct char_mappings **string_replace_cmaps = NULL;
944 uint16_t i;
945 int saved_errno = 0;
946 NTSTATUS status;
948 if (ad->adx_header.adx_num_attrs == 0) {
949 return true;
952 if (string_replace_cmaps == NULL) {
953 const char **mappings = NULL;
955 mappings = str_list_make_v3_const(
956 talloc_tos(), fruit_catia_maps, NULL);
957 if (mappings == NULL) {
958 return false;
960 string_replace_cmaps = string_replace_init_map(mappings);
961 TALLOC_FREE(mappings);
964 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
965 struct ad_xattr_entry *e = &ad->adx_entries[i];
966 char *mapped_name = NULL;
967 char *tmp = NULL;
968 struct smb_filename *stream_name = NULL;
969 files_struct *fsp = NULL;
970 ssize_t nwritten;
972 status = string_replace_allocate(ad->ad_handle->conn,
973 e->adx_name,
974 string_replace_cmaps,
975 talloc_tos(),
976 &mapped_name,
977 vfs_translate_to_windows);
978 if (!NT_STATUS_IS_OK(status) &&
979 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
981 DBG_ERR("string_replace_allocate failed\n");
982 return -1;
985 tmp = mapped_name;
986 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
987 TALLOC_FREE(tmp);
988 if (mapped_name == NULL) {
989 return -1;
992 stream_name = synthetic_smb_fname(talloc_tos(),
993 smb_fname->base_name,
994 mapped_name,
995 NULL,
996 smb_fname->flags);
997 TALLOC_FREE(mapped_name);
998 if (stream_name == NULL) {
999 DBG_ERR("synthetic_smb_fname failed\n");
1000 return -1;
1003 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1005 status = SMB_VFS_CREATE_FILE(
1006 ad->ad_handle->conn, /* conn */
1007 NULL, /* req */
1008 0, /* root_dir_fid */
1009 stream_name, /* fname */
1010 FILE_GENERIC_WRITE, /* access_mask */
1011 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1012 FILE_OPEN_IF, /* create_disposition */
1013 0, /* create_options */
1014 0, /* file_attributes */
1015 INTERNAL_OPEN_ONLY, /* oplock_request */
1016 NULL, /* lease */
1017 0, /* allocation_size */
1018 0, /* private_flags */
1019 NULL, /* sd */
1020 NULL, /* ea_list */
1021 &fsp, /* result */
1022 NULL, /* psbuf */
1023 NULL, NULL); /* create context */
1024 TALLOC_FREE(stream_name);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1027 return -1;
1030 nwritten = SMB_VFS_PWRITE(fsp,
1031 map + e->adx_offset,
1032 e->adx_length,
1034 if (nwritten == -1) {
1035 DBG_ERR("SMB_VFS_PWRITE failed\n");
1036 saved_errno = errno;
1037 close_file(NULL, fsp, ERROR_CLOSE);
1038 errno = saved_errno;
1039 return -1;
1042 status = close_file(NULL, fsp, NORMAL_CLOSE);
1043 if (!NT_STATUS_IS_OK(status)) {
1044 return -1;
1046 fsp = NULL;
1049 return true;
1053 * Convert from Apple's ._ file to Netatalk
1055 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1056 * bytes containing packed xattrs. Netatalk can't deal with that, so
1057 * we simply discard the packed xattrs.
1059 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1060 * otherwise
1062 static int ad_convert(struct adouble *ad,
1063 const struct smb_filename *smb_fname,
1064 int fd)
1066 int rc = 0;
1067 char *map = MAP_FAILED;
1068 size_t origlen;
1069 bool ok;
1071 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1072 ad_getentrylen(ad, ADEID_RFORK);
1074 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1075 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1076 if (map == MAP_FAILED) {
1077 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1078 rc = -1;
1079 goto exit;
1082 ok = ad_convert_xattr(ad, smb_fname, map);
1083 if (!ok) {
1084 return -1;
1087 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1088 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1089 map + ad_getentryoff(ad, ADEID_RFORK),
1090 ad_getentrylen(ad, ADEID_RFORK));
1093 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1094 ad_setentryoff(ad, ADEID_RFORK,
1095 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1098 * FIXME: direct ftruncate(), but we don't have a fsp for the
1099 * VFS call
1101 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
1102 + ad_getentrylen(ad, ADEID_RFORK));
1104 exit:
1105 if (map != MAP_FAILED) {
1106 munmap(map, origlen);
1108 return rc;
1112 * Read and parse Netatalk AppleDouble metadata xattr
1114 static ssize_t ad_read_meta(struct adouble *ad,
1115 const struct smb_filename *smb_fname)
1117 int rc = 0;
1118 ssize_t ealen;
1119 bool ok;
1121 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1123 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1124 AFPINFO_EA_NETATALK, ad->ad_data,
1125 AD_DATASZ_XATTR);
1126 if (ealen == -1) {
1127 switch (errno) {
1128 case ENOATTR:
1129 case ENOENT:
1130 if (errno == ENOATTR) {
1131 errno = ENOENT;
1133 rc = -1;
1134 goto exit;
1135 default:
1136 DEBUG(2, ("error reading meta xattr: %s\n",
1137 strerror(errno)));
1138 rc = -1;
1139 goto exit;
1142 if (ealen != AD_DATASZ_XATTR) {
1143 DEBUG(2, ("bad size %zd\n", ealen));
1144 errno = EINVAL;
1145 rc = -1;
1146 goto exit;
1149 /* Now parse entries */
1150 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1151 if (!ok) {
1152 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1153 errno = EINVAL;
1154 rc = -1;
1155 goto exit;
1158 if (!ad_getentryoff(ad, ADEID_FINDERI)
1159 || !ad_getentryoff(ad, ADEID_COMMENT)
1160 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1161 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1162 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1163 || !ad_getentryoff(ad, ADEID_PRIVINO)
1164 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1165 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1166 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1167 errno = EINVAL;
1168 rc = -1;
1169 goto exit;
1172 exit:
1173 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1174 smb_fname->base_name, rc));
1176 if (rc != 0) {
1177 ealen = -1;
1178 if (errno == EINVAL) {
1179 become_root();
1180 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1181 unbecome_root();
1182 errno = ENOENT;
1185 return ealen;
1188 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1189 int flags,
1190 mode_t mode)
1192 #ifdef HAVE_ATTROPEN
1193 /* FIXME: direct Solaris xattr syscall */
1194 return attropen(smb_fname->base_name,
1195 AFPRESOURCE_EA_NETATALK, flags, mode);
1196 #else
1197 errno = ENOSYS;
1198 return -1;
1199 #endif
1202 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1203 int flags,
1204 mode_t mode)
1206 int ret;
1207 int fd;
1208 struct smb_filename *adp_smb_fname = NULL;
1210 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1211 if (ret != 0) {
1212 return -1;
1215 fd = open(adp_smb_fname->base_name, flags, mode);
1216 TALLOC_FREE(adp_smb_fname);
1218 return fd;
1221 static int ad_open_rsrc(vfs_handle_struct *handle,
1222 const struct smb_filename *smb_fname,
1223 int flags,
1224 mode_t mode)
1226 struct fruit_config_data *config = NULL;
1227 int fd;
1229 SMB_VFS_HANDLE_GET_DATA(handle, config,
1230 struct fruit_config_data, return -1);
1232 if (config->rsrc == FRUIT_RSRC_XATTR) {
1233 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1234 } else {
1235 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1238 return fd;
1242 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1243 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1244 * for file IO on the ._ file.
1246 static int ad_open(vfs_handle_struct *handle,
1247 struct adouble *ad,
1248 files_struct *fsp,
1249 const struct smb_filename *smb_fname,
1250 int flags,
1251 mode_t mode)
1253 int fd;
1255 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1256 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1258 if (ad->ad_type == ADOUBLE_META) {
1259 return 0;
1262 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1263 ad->ad_fd = fsp->fh->fd;
1264 ad->ad_opened = false;
1265 return 0;
1268 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1269 if (fd == -1) {
1270 return -1;
1272 ad->ad_opened = true;
1273 ad->ad_fd = fd;
1275 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1276 smb_fname->base_name,
1277 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1279 return 0;
1282 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1284 int ret;
1285 SMB_STRUCT_STAT st;
1287 /* FIXME: direct sys_fstat(), don't have an fsp */
1288 ret = sys_fstat(ad->ad_fd, &st,
1289 lp_fake_directory_create_times(
1290 SNUM(ad->ad_handle->conn)));
1291 if (ret != 0) {
1292 return -1;
1295 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1296 return st.st_ex_size;
1299 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1300 const struct smb_filename *smb_fname)
1302 SMB_STRUCT_STAT sbuf;
1303 char *p_ad = NULL;
1304 AfpInfo *ai = NULL;
1305 DATA_BLOB aiblob;
1306 struct smb_filename *stream_name = NULL;
1307 files_struct *fsp = NULL;
1308 ssize_t len;
1309 size_t size;
1310 ssize_t nwritten;
1311 NTSTATUS status;
1312 int saved_errno = 0;
1313 int ret;
1314 bool ok;
1316 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1317 SNUM(ad->ad_handle->conn)));
1318 if (ret != 0) {
1319 return -1;
1323 * AppleDouble file header content and size, two cases:
1325 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1326 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1328 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1330 size = sbuf.st_ex_size;
1331 if (size > talloc_array_length(ad->ad_data)) {
1332 if (size > AD_XATTR_MAX_HDR_SIZE) {
1333 size = AD_XATTR_MAX_HDR_SIZE;
1335 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1336 if (p_ad == NULL) {
1337 return -1;
1339 ad->ad_data = p_ad;
1342 len = sys_pread(ad->ad_fd, ad->ad_data,
1343 talloc_array_length(ad->ad_data), 0);
1344 if (len != talloc_array_length(ad->ad_data)) {
1345 DBG_NOTICE("%s %s: bad size: %zd\n",
1346 smb_fname->base_name, strerror(errno), len);
1347 return -1;
1350 /* Now parse entries */
1351 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1352 if (!ok) {
1353 DBG_ERR("invalid AppleDouble resource %s\n",
1354 smb_fname->base_name);
1355 errno = EINVAL;
1356 return -1;
1359 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1360 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1361 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1362 DBG_ERR("invalid AppleDouble resource %s\n",
1363 smb_fname->base_name);
1364 errno = EINVAL;
1365 return -1;
1368 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1369 return len;
1373 * Try to fixup AppleDouble files created by OS X with xattrs
1374 * appended to the ADEID_FINDERI entry. We simply remove the
1375 * xattrs blob, this means any fancy xattr that was stored
1376 * there is lost.
1379 ret = ad_convert(ad, smb_fname, ad->ad_fd);
1380 if (ret != 0) {
1381 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1382 return len;
1385 ok = ad_pack(ad);
1386 if (!ok) {
1387 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1388 return -1;
1391 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1392 if (len != AD_DATASZ_DOT_UND) {
1393 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1394 return -1;
1397 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1398 if (p_ad == NULL) {
1399 return -1;
1402 ai = afpinfo_new(talloc_tos());
1403 if (ai == NULL) {
1404 return -1;
1407 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1409 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1410 if (aiblob.data == NULL) {
1411 TALLOC_FREE(ai);
1412 return -1;
1415 size = afpinfo_pack(ai, (char *)aiblob.data);
1416 TALLOC_FREE(ai);
1417 if (size != AFP_INFO_SIZE) {
1418 return -1;
1421 stream_name = synthetic_smb_fname(talloc_tos(),
1422 smb_fname->base_name,
1423 AFPINFO_STREAM,
1424 NULL,
1425 smb_fname->flags);
1426 if (stream_name == NULL) {
1427 data_blob_free(&aiblob);
1428 DBG_ERR("synthetic_smb_fname failed\n");
1429 return -1;
1432 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1434 status = SMB_VFS_CREATE_FILE(
1435 ad->ad_handle->conn, /* conn */
1436 NULL, /* req */
1437 0, /* root_dir_fid */
1438 stream_name, /* fname */
1439 FILE_GENERIC_WRITE, /* access_mask */
1440 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1441 FILE_OPEN_IF, /* create_disposition */
1442 0, /* create_options */
1443 0, /* file_attributes */
1444 INTERNAL_OPEN_ONLY, /* oplock_request */
1445 NULL, /* lease */
1446 0, /* allocation_size */
1447 0, /* private_flags */
1448 NULL, /* sd */
1449 NULL, /* ea_list */
1450 &fsp, /* result */
1451 NULL, /* psbuf */
1452 NULL, NULL); /* create context */
1453 TALLOC_FREE(stream_name);
1454 if (!NT_STATUS_IS_OK(status)) {
1455 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1456 return -1;
1459 nwritten = SMB_VFS_PWRITE(fsp,
1460 aiblob.data,
1461 aiblob.length,
1463 if (nwritten == -1) {
1464 DBG_ERR("SMB_VFS_PWRITE failed\n");
1465 saved_errno = errno;
1466 close_file(NULL, fsp, ERROR_CLOSE);
1467 errno = saved_errno;
1468 return -1;
1471 status = close_file(NULL, fsp, NORMAL_CLOSE);
1472 if (!NT_STATUS_IS_OK(status)) {
1473 return -1;
1475 fsp = NULL;
1477 return len;
1481 * Read and parse resource fork, either ._ AppleDouble file or xattr
1483 static ssize_t ad_read_rsrc(struct adouble *ad,
1484 const struct smb_filename *smb_fname)
1486 struct fruit_config_data *config = NULL;
1487 ssize_t len;
1489 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1490 struct fruit_config_data, return -1);
1492 if (config->rsrc == FRUIT_RSRC_XATTR) {
1493 len = ad_read_rsrc_xattr(ad);
1494 } else {
1495 len = ad_read_rsrc_adouble(ad, smb_fname);
1498 return len;
1502 * Read and unpack an AppleDouble metadata xattr or resource
1504 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1506 switch (ad->ad_type) {
1507 case ADOUBLE_META:
1508 return ad_read_meta(ad, smb_fname);
1509 case ADOUBLE_RSRC:
1510 return ad_read_rsrc(ad, smb_fname);
1511 default:
1512 return -1;
1516 static int adouble_destructor(struct adouble *ad)
1518 if ((ad->ad_fd != -1) && ad->ad_opened) {
1519 close(ad->ad_fd);
1520 ad->ad_fd = -1;
1522 return 0;
1526 * Allocate a struct adouble without initialiing it
1528 * The struct is either hang of the fsp extension context or if fsp is
1529 * NULL from ctx.
1531 * @param[in] ctx talloc context
1532 * @param[in] handle vfs handle
1533 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1535 * @return adouble handle
1537 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1538 adouble_type_t type)
1540 int rc = 0;
1541 size_t adsize = 0;
1542 struct adouble *ad;
1543 struct fruit_config_data *config;
1545 SMB_VFS_HANDLE_GET_DATA(handle, config,
1546 struct fruit_config_data, return NULL);
1548 switch (type) {
1549 case ADOUBLE_META:
1550 adsize = AD_DATASZ_XATTR;
1551 break;
1552 case ADOUBLE_RSRC:
1553 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1554 adsize = AD_DATASZ_DOT_UND;
1556 break;
1557 default:
1558 return NULL;
1561 ad = talloc_zero(ctx, struct adouble);
1562 if (ad == NULL) {
1563 rc = -1;
1564 goto exit;
1567 if (adsize) {
1568 ad->ad_data = talloc_zero_array(ad, char, adsize);
1569 if (ad->ad_data == NULL) {
1570 rc = -1;
1571 goto exit;
1575 ad->ad_handle = handle;
1576 ad->ad_type = type;
1577 ad->ad_magic = AD_MAGIC;
1578 ad->ad_version = AD_VERSION;
1579 ad->ad_fd = -1;
1581 talloc_set_destructor(ad, adouble_destructor);
1583 exit:
1584 if (rc != 0) {
1585 TALLOC_FREE(ad);
1587 return ad;
1591 * Allocate and initialize a new struct adouble
1593 * @param[in] ctx talloc context
1594 * @param[in] handle vfs handle
1595 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1597 * @return adouble handle, initialized
1599 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1600 adouble_type_t type)
1602 int rc = 0;
1603 const struct ad_entry_order *eid;
1604 struct adouble *ad = NULL;
1605 struct fruit_config_data *config;
1606 time_t t = time(NULL);
1608 SMB_VFS_HANDLE_GET_DATA(handle, config,
1609 struct fruit_config_data, return NULL);
1611 switch (type) {
1612 case ADOUBLE_META:
1613 eid = entry_order_meta_xattr;
1614 break;
1615 case ADOUBLE_RSRC:
1616 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1617 eid = entry_order_dot_und;
1618 } else {
1619 eid = entry_order_rsrc_xattr;
1621 break;
1622 default:
1623 return NULL;
1626 ad = ad_alloc(ctx, handle, type);
1627 if (ad == NULL) {
1628 return NULL;
1631 while (eid->id) {
1632 ad->ad_eid[eid->id].ade_off = eid->offset;
1633 ad->ad_eid[eid->id].ade_len = eid->len;
1634 eid++;
1637 /* put something sane in the date fields */
1638 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1639 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1640 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1641 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1643 if (rc != 0) {
1644 TALLOC_FREE(ad);
1646 return ad;
1649 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1650 vfs_handle_struct *handle,
1651 files_struct *fsp,
1652 const struct smb_filename *smb_fname,
1653 adouble_type_t type)
1655 int rc = 0;
1656 ssize_t len;
1657 struct adouble *ad = NULL;
1658 int mode;
1660 if (fsp != NULL) {
1661 smb_fname = fsp->base_fsp->fsp_name;
1664 DEBUG(10, ("ad_get(%s) called for %s\n",
1665 type == ADOUBLE_META ? "meta" : "rsrc",
1666 smb_fname->base_name));
1668 ad = ad_alloc(ctx, handle, type);
1669 if (ad == NULL) {
1670 rc = -1;
1671 goto exit;
1674 /* Try rw first so we can use the fd in ad_convert() */
1675 mode = O_RDWR;
1677 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1678 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1679 mode = O_RDONLY;
1680 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1682 if (rc == -1) {
1683 DBG_DEBUG("ad_open [%s] error [%s]\n",
1684 smb_fname->base_name, strerror(errno));
1685 goto exit;
1689 len = ad_read(ad, smb_fname);
1690 if (len == -1) {
1691 DEBUG(10, ("error reading AppleDouble for %s\n",
1692 smb_fname->base_name));
1693 rc = -1;
1694 goto exit;
1697 exit:
1698 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1699 type == ADOUBLE_META ? "meta" : "rsrc",
1700 smb_fname->base_name, rc));
1702 if (rc != 0) {
1703 TALLOC_FREE(ad);
1705 return ad;
1709 * Return AppleDouble data for a file
1711 * @param[in] ctx talloc context
1712 * @param[in] handle vfs handle
1713 * @param[in] smb_fname pathname to file or directory
1714 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1716 * @return talloced struct adouble or NULL on error
1718 static struct adouble *ad_get(TALLOC_CTX *ctx,
1719 vfs_handle_struct *handle,
1720 const struct smb_filename *smb_fname,
1721 adouble_type_t type)
1723 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1727 * Return AppleDouble data for a file
1729 * @param[in] ctx talloc context
1730 * @param[in] handle vfs handle
1731 * @param[in] fsp fsp to use for IO
1732 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1734 * @return talloced struct adouble or NULL on error
1736 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1737 files_struct *fsp, adouble_type_t type)
1739 return ad_get_internal(ctx, handle, fsp, NULL, type);
1743 * Set AppleDouble metadata on a file or directory
1745 * @param[in] ad adouble handle
1747 * @param[in] smb_fname pathname to file or directory
1749 * @return status code, 0 means success
1751 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1753 bool ok;
1754 int ret;
1756 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1758 if (ad->ad_type != ADOUBLE_META) {
1759 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1760 smb_fname->base_name);
1761 return -1;
1764 ok = ad_pack(ad);
1765 if (!ok) {
1766 return -1;
1769 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1770 smb_fname,
1771 AFPINFO_EA_NETATALK,
1772 ad->ad_data,
1773 AD_DATASZ_XATTR, 0);
1775 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1777 return ret;
1781 * Set AppleDouble metadata on a file or directory
1783 * @param[in] ad adouble handle
1784 * @param[in] fsp file handle
1786 * @return status code, 0 means success
1788 static int ad_fset(struct adouble *ad, files_struct *fsp)
1790 int rc = -1;
1791 ssize_t len;
1792 bool ok;
1794 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1796 if ((fsp == NULL)
1797 || (fsp->fh == NULL)
1798 || (fsp->fh->fd == -1))
1800 smb_panic("bad fsp");
1803 ok = ad_pack(ad);
1804 if (!ok) {
1805 return -1;
1808 switch (ad->ad_type) {
1809 case ADOUBLE_META:
1810 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1811 fsp->fsp_name,
1812 AFPINFO_EA_NETATALK,
1813 ad->ad_data,
1814 AD_DATASZ_XATTR, 0);
1815 break;
1817 case ADOUBLE_RSRC:
1818 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1819 fsp,
1820 ad->ad_data,
1821 AD_DATASZ_DOT_UND,
1823 if (len != AD_DATASZ_DOT_UND) {
1824 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1825 return -1;
1827 rc = 0;
1828 break;
1830 default:
1831 return -1;
1834 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1836 return rc;
1839 /*****************************************************************************
1840 * Helper functions
1841 *****************************************************************************/
1843 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1845 if (strncasecmp_m(smb_fname->stream_name,
1846 AFPINFO_STREAM_NAME,
1847 strlen(AFPINFO_STREAM_NAME)) == 0) {
1848 return true;
1850 return false;
1853 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1855 if (strncasecmp_m(smb_fname->stream_name,
1856 AFPRESOURCE_STREAM_NAME,
1857 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1858 return true;
1860 return false;
1864 * Test whether stream is an Apple stream, not used atm
1866 #if 0
1867 static bool is_apple_stream(const struct smb_filename *smb_fname)
1869 if (is_afpinfo_stream(smb_fname)) {
1870 return true;
1872 if (is_afpresource_stream(smb_fname)) {
1873 return true;
1875 return false;
1877 #endif
1880 * Initialize config struct from our smb.conf config parameters
1882 static int init_fruit_config(vfs_handle_struct *handle)
1884 struct fruit_config_data *config;
1885 int enumval;
1887 config = talloc_zero(handle->conn, struct fruit_config_data);
1888 if (!config) {
1889 DEBUG(1, ("talloc_zero() failed\n"));
1890 errno = ENOMEM;
1891 return -1;
1895 * Versions up to Samba 4.5.x had a spelling bug in the
1896 * fruit:resource option calling lp_parm_enum with
1897 * "res*s*ource" (ie two s).
1899 * In Samba 4.6 we accept both the wrong and the correct
1900 * spelling, in Samba 4.7 the bad spelling will be removed.
1902 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1903 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1904 if (enumval == -1) {
1905 DEBUG(1, ("value for %s: resource type unknown\n",
1906 FRUIT_PARAM_TYPE_NAME));
1907 return -1;
1909 config->rsrc = (enum fruit_rsrc)enumval;
1911 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1912 "resource", fruit_rsrc, enumval);
1913 if (enumval == -1) {
1914 DEBUG(1, ("value for %s: resource type unknown\n",
1915 FRUIT_PARAM_TYPE_NAME));
1916 return -1;
1918 config->rsrc = (enum fruit_rsrc)enumval;
1920 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1921 "metadata", fruit_meta, FRUIT_META_NETATALK);
1922 if (enumval == -1) {
1923 DEBUG(1, ("value for %s: metadata type unknown\n",
1924 FRUIT_PARAM_TYPE_NAME));
1925 return -1;
1927 config->meta = (enum fruit_meta)enumval;
1929 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1930 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1931 if (enumval == -1) {
1932 DEBUG(1, ("value for %s: locking type unknown\n",
1933 FRUIT_PARAM_TYPE_NAME));
1934 return -1;
1936 config->locking = (enum fruit_locking)enumval;
1938 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1939 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1940 if (enumval == -1) {
1941 DEBUG(1, ("value for %s: encoding type unknown\n",
1942 FRUIT_PARAM_TYPE_NAME));
1943 return -1;
1945 config->encoding = (enum fruit_encoding)enumval;
1947 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1948 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1949 FRUIT_PARAM_TYPE_NAME,
1950 "veto_appledouble",
1951 true);
1954 config->use_aapl = lp_parm_bool(
1955 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1957 config->unix_info_enabled = lp_parm_bool(
1958 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1960 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1961 "copyfile", false);
1963 config->posix_rename = lp_parm_bool(
1964 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1966 config->aapl_zero_file_id =
1967 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1969 config->readdir_attr_rsize = lp_parm_bool(
1970 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1972 config->readdir_attr_finder_info = lp_parm_bool(
1973 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1975 config->readdir_attr_max_access = lp_parm_bool(
1976 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1978 config->model = lp_parm_const_string(
1979 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1981 SMB_VFS_HANDLE_SET_DATA(handle, config,
1982 NULL, struct fruit_config_data,
1983 return -1);
1985 return 0;
1989 * Prepend "._" to a basename
1990 * Return a new struct smb_filename with stream_name == NULL.
1992 static int adouble_path(TALLOC_CTX *ctx,
1993 const struct smb_filename *smb_fname_in,
1994 struct smb_filename **pp_smb_fname_out)
1996 char *parent;
1997 const char *base;
1998 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1999 smb_fname_in);
2001 if (smb_fname == NULL) {
2002 return -1;
2005 /* We need streamname to be NULL */
2006 TALLOC_FREE(smb_fname->stream_name);
2008 /* And we're replacing base_name. */
2009 TALLOC_FREE(smb_fname->base_name);
2011 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2012 &parent, &base)) {
2013 TALLOC_FREE(smb_fname);
2014 return -1;
2017 smb_fname->base_name = talloc_asprintf(smb_fname,
2018 "%s/._%s", parent, base);
2019 if (smb_fname->base_name == NULL) {
2020 TALLOC_FREE(smb_fname);
2021 return -1;
2024 *pp_smb_fname_out = smb_fname;
2026 return 0;
2030 * Allocate and initialize an AfpInfo struct
2032 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2034 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2035 if (ai == NULL) {
2036 return NULL;
2038 ai->afpi_Signature = AFP_Signature;
2039 ai->afpi_Version = AFP_Version;
2040 ai->afpi_BackupTime = AD_DATE_START;
2041 return ai;
2045 * Pack an AfpInfo struct into a buffer
2047 * Buffer size must be at least AFP_INFO_SIZE
2048 * Returns size of packed buffer
2050 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2052 memset(buf, 0, AFP_INFO_SIZE);
2054 RSIVAL(buf, 0, ai->afpi_Signature);
2055 RSIVAL(buf, 4, ai->afpi_Version);
2056 RSIVAL(buf, 12, ai->afpi_BackupTime);
2057 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2059 return AFP_INFO_SIZE;
2063 * Unpack a buffer into a AfpInfo structure
2065 * Buffer size must be at least AFP_INFO_SIZE
2066 * Returns allocated AfpInfo struct
2068 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2070 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2071 if (ai == NULL) {
2072 return NULL;
2075 ai->afpi_Signature = RIVAL(data, 0);
2076 ai->afpi_Version = RIVAL(data, 4);
2077 ai->afpi_BackupTime = RIVAL(data, 12);
2078 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2079 sizeof(ai->afpi_FinderInfo));
2081 if (ai->afpi_Signature != AFP_Signature
2082 || ai->afpi_Version != AFP_Version) {
2083 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2084 TALLOC_FREE(ai);
2087 return ai;
2091 * Fake an inode number from the md5 hash of the (xattr) name
2093 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2095 MD5_CTX ctx;
2096 unsigned char hash[16];
2097 SMB_INO_T result;
2098 char *upper_sname;
2100 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2101 SMB_ASSERT(upper_sname != NULL);
2103 MD5Init(&ctx);
2104 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2105 sizeof(sbuf->st_ex_dev));
2106 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2107 sizeof(sbuf->st_ex_ino));
2108 MD5Update(&ctx, (unsigned char *)upper_sname,
2109 talloc_get_size(upper_sname)-1);
2110 MD5Final(hash, &ctx);
2112 TALLOC_FREE(upper_sname);
2114 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2115 memcpy(&result, hash, sizeof(result));
2117 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2118 sname, (unsigned long long)result));
2120 return result;
2123 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2124 struct stream_struct **streams,
2125 const char *name, off_t size,
2126 off_t alloc_size)
2128 struct stream_struct *tmp;
2130 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2131 (*num_streams)+1);
2132 if (tmp == NULL) {
2133 return false;
2136 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2137 if (tmp[*num_streams].name == NULL) {
2138 return false;
2141 tmp[*num_streams].size = size;
2142 tmp[*num_streams].alloc_size = alloc_size;
2144 *streams = tmp;
2145 *num_streams += 1;
2146 return true;
2149 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2150 struct stream_struct **streams)
2152 struct stream_struct *tmp = *streams;
2153 unsigned int i;
2155 if (*num_streams == 0) {
2156 return true;
2159 for (i = 0; i < *num_streams; i++) {
2160 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2161 break;
2165 if (i == *num_streams) {
2166 return true;
2169 if (tmp[i].size > 0) {
2170 return true;
2173 TALLOC_FREE(tmp[i].name);
2174 if (*num_streams - 1 > i) {
2175 memmove(&tmp[i], &tmp[i+1],
2176 (*num_streams - i - 1) * sizeof(struct stream_struct));
2179 *num_streams -= 1;
2180 return true;
2183 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2184 struct stream_struct **streams,
2185 const char *name)
2187 struct stream_struct *tmp = *streams;
2188 unsigned int i;
2190 if (*num_streams == 0) {
2191 return true;
2194 for (i = 0; i < *num_streams; i++) {
2195 if (strequal_m(tmp[i].name, name)) {
2196 break;
2200 if (i == *num_streams) {
2201 return true;
2204 TALLOC_FREE(tmp[i].name);
2205 if (*num_streams - 1 > i) {
2206 memmove(&tmp[i], &tmp[i+1],
2207 (*num_streams - i - 1) * sizeof(struct stream_struct));
2210 *num_streams -= 1;
2211 return true;
2214 static bool ad_empty_finderinfo(const struct adouble *ad)
2216 int cmp;
2217 char emptybuf[ADEDLEN_FINDERI] = {0};
2218 char *fi = NULL;
2220 fi = ad_get_entry(ad, ADEID_FINDERI);
2221 if (fi == NULL) {
2222 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2223 return false;
2226 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2227 return (cmp == 0);
2230 static bool ai_empty_finderinfo(const AfpInfo *ai)
2232 int cmp;
2233 char emptybuf[ADEDLEN_FINDERI] = {0};
2235 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2236 return (cmp == 0);
2240 * Update btime with btime from Netatalk
2242 static void update_btime(vfs_handle_struct *handle,
2243 struct smb_filename *smb_fname)
2245 uint32_t t;
2246 struct timespec creation_time = {0};
2247 struct adouble *ad;
2248 struct fruit_config_data *config = NULL;
2250 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2251 return);
2253 switch (config->meta) {
2254 case FRUIT_META_STREAM:
2255 return;
2256 case FRUIT_META_NETATALK:
2257 /* Handled below */
2258 break;
2259 default:
2260 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2261 return;
2264 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2265 if (ad == NULL) {
2266 return;
2268 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2269 TALLOC_FREE(ad);
2270 return;
2272 TALLOC_FREE(ad);
2274 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2275 update_stat_ex_create_time(&smb_fname->st, creation_time);
2277 return;
2281 * Map an access mask to a Netatalk single byte byte range lock
2283 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2284 uint32_t access_mask)
2286 off_t offset;
2288 switch (access_mask) {
2289 case FILE_READ_DATA:
2290 offset = AD_FILELOCK_OPEN_RD;
2291 break;
2293 case FILE_WRITE_DATA:
2294 case FILE_APPEND_DATA:
2295 offset = AD_FILELOCK_OPEN_WR;
2296 break;
2298 default:
2299 offset = AD_FILELOCK_OPEN_NONE;
2300 break;
2303 if (fork_type == APPLE_FORK_RSRC) {
2304 if (offset == AD_FILELOCK_OPEN_NONE) {
2305 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2306 } else {
2307 offset += 2;
2311 return offset;
2315 * Map a deny mode to a Netatalk brl
2317 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2318 uint32_t deny_mode)
2320 off_t offset;
2322 switch (deny_mode) {
2323 case DENY_READ:
2324 offset = AD_FILELOCK_DENY_RD;
2325 break;
2327 case DENY_WRITE:
2328 offset = AD_FILELOCK_DENY_WR;
2329 break;
2331 default:
2332 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2335 if (fork_type == APPLE_FORK_RSRC) {
2336 offset += 2;
2339 return offset;
2343 * Call fcntl() with an exclusive F_GETLK request in order to
2344 * determine if there's an exisiting shared lock
2346 * @return true if the requested lock was found or any error occurred
2347 * false if the lock was not found
2349 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2351 bool result;
2352 off_t offset = in_offset;
2353 off_t len = 1;
2354 int type = F_WRLCK;
2355 pid_t pid;
2357 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2358 if (result == false) {
2359 return true;
2362 if (type != F_UNLCK) {
2363 return true;
2366 return false;
2369 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2370 files_struct *fsp,
2371 uint32_t access_mask,
2372 uint32_t deny_mode)
2374 NTSTATUS status = NT_STATUS_OK;
2375 struct byte_range_lock *br_lck = NULL;
2376 bool open_for_reading, open_for_writing, deny_read, deny_write;
2377 off_t off;
2378 bool have_read = false;
2379 int flags;
2381 /* FIXME: hardcoded data fork, add resource fork */
2382 enum apple_fork fork_type = APPLE_FORK_DATA;
2384 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2385 fsp_str_dbg(fsp),
2386 access_mask & FILE_READ_DATA ? "READ" :"-",
2387 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2388 deny_mode & DENY_READ ? "DENY_READ" : "-",
2389 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2391 if (fsp->fh->fd == -1) {
2392 return NT_STATUS_OK;
2395 flags = fcntl(fsp->fh->fd, F_GETFL);
2396 if (flags == -1) {
2397 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2398 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2399 return map_nt_error_from_unix(errno);
2402 if (flags & (O_RDONLY|O_RDWR)) {
2404 * Applying fcntl read locks requires an fd opened for
2405 * reading. This means we won't be applying locks for
2406 * files openend write-only, but what can we do...
2408 have_read = true;
2412 * Check read access and deny read mode
2414 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2415 /* Check access */
2416 open_for_reading = test_netatalk_lock(
2417 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2419 deny_read = test_netatalk_lock(
2420 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2422 DEBUG(10, ("read: %s, deny_write: %s\n",
2423 open_for_reading == true ? "yes" : "no",
2424 deny_read == true ? "yes" : "no"));
2426 if (((access_mask & FILE_READ_DATA) && deny_read)
2427 || ((deny_mode & DENY_READ) && open_for_reading)) {
2428 return NT_STATUS_SHARING_VIOLATION;
2431 /* Set locks */
2432 if ((access_mask & FILE_READ_DATA) && have_read) {
2433 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2434 br_lck = do_lock(
2435 handle->conn->sconn->msg_ctx, fsp,
2436 fsp->op->global->open_persistent_id, 1, off,
2437 READ_LOCK, POSIX_LOCK, false,
2438 &status, NULL);
2440 if (!NT_STATUS_IS_OK(status)) {
2441 return status;
2443 TALLOC_FREE(br_lck);
2446 if ((deny_mode & DENY_READ) && have_read) {
2447 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2448 br_lck = do_lock(
2449 handle->conn->sconn->msg_ctx, fsp,
2450 fsp->op->global->open_persistent_id, 1, off,
2451 READ_LOCK, POSIX_LOCK, false,
2452 &status, NULL);
2454 if (!NT_STATUS_IS_OK(status)) {
2455 return status;
2457 TALLOC_FREE(br_lck);
2462 * Check write access and deny write mode
2464 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2465 /* Check access */
2466 open_for_writing = test_netatalk_lock(
2467 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2469 deny_write = test_netatalk_lock(
2470 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2472 DEBUG(10, ("write: %s, deny_write: %s\n",
2473 open_for_writing == true ? "yes" : "no",
2474 deny_write == true ? "yes" : "no"));
2476 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2477 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2478 return NT_STATUS_SHARING_VIOLATION;
2481 /* Set locks */
2482 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2483 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2484 br_lck = do_lock(
2485 handle->conn->sconn->msg_ctx, fsp,
2486 fsp->op->global->open_persistent_id, 1, off,
2487 READ_LOCK, POSIX_LOCK, false,
2488 &status, NULL);
2490 if (!NT_STATUS_IS_OK(status)) {
2491 return status;
2493 TALLOC_FREE(br_lck);
2496 if ((deny_mode & DENY_WRITE) && have_read) {
2497 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2498 br_lck = do_lock(
2499 handle->conn->sconn->msg_ctx, fsp,
2500 fsp->op->global->open_persistent_id, 1, off,
2501 READ_LOCK, POSIX_LOCK, false,
2502 &status, NULL);
2504 if (!NT_STATUS_IS_OK(status)) {
2505 return status;
2507 TALLOC_FREE(br_lck);
2511 TALLOC_FREE(br_lck);
2513 return status;
2516 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2517 struct smb_request *req,
2518 const struct smb2_create_blobs *in_context_blobs,
2519 struct smb2_create_blobs *out_context_blobs)
2521 struct fruit_config_data *config;
2522 NTSTATUS status;
2523 struct smb2_create_blob *aapl = NULL;
2524 uint32_t cmd;
2525 bool ok;
2526 uint8_t p[16];
2527 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2528 uint64_t req_bitmap, client_caps;
2529 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2530 smb_ucs2_t *model;
2531 size_t modellen;
2533 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2534 return NT_STATUS_UNSUCCESSFUL);
2536 if (!config->use_aapl
2537 || in_context_blobs == NULL
2538 || out_context_blobs == NULL) {
2539 return NT_STATUS_OK;
2542 aapl = smb2_create_blob_find(in_context_blobs,
2543 SMB2_CREATE_TAG_AAPL);
2544 if (aapl == NULL) {
2545 return NT_STATUS_OK;
2548 if (aapl->data.length != 24) {
2549 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2550 (uintmax_t)aapl->data.length));
2551 return NT_STATUS_INVALID_PARAMETER;
2554 cmd = IVAL(aapl->data.data, 0);
2555 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2556 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2557 return NT_STATUS_INVALID_PARAMETER;
2560 req_bitmap = BVAL(aapl->data.data, 8);
2561 client_caps = BVAL(aapl->data.data, 16);
2563 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2564 SIVAL(p, 4, 0);
2565 SBVAL(p, 8, req_bitmap);
2566 ok = data_blob_append(req, &blob, p, 16);
2567 if (!ok) {
2568 return NT_STATUS_UNSUCCESSFUL;
2571 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2572 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2573 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2574 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2575 config->readdir_attr_enabled = true;
2578 if (config->use_copyfile) {
2579 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2580 config->copyfile_enabled = true;
2584 * The client doesn't set the flag, so we can't check
2585 * for it and just set it unconditionally
2587 if (config->unix_info_enabled) {
2588 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2591 SBVAL(p, 0, server_caps);
2592 ok = data_blob_append(req, &blob, p, 8);
2593 if (!ok) {
2594 return NT_STATUS_UNSUCCESSFUL;
2598 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2599 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2600 uint64_t caps = 0;
2602 switch (val) {
2603 case Auto:
2604 break;
2606 case True:
2607 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2608 break;
2610 default:
2611 break;
2614 SBVAL(p, 0, caps);
2616 ok = data_blob_append(req, &blob, p, 8);
2617 if (!ok) {
2618 return NT_STATUS_UNSUCCESSFUL;
2622 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2623 ok = convert_string_talloc(req,
2624 CH_UNIX, CH_UTF16LE,
2625 config->model, strlen(config->model),
2626 &model, &modellen);
2627 if (!ok) {
2628 return NT_STATUS_UNSUCCESSFUL;
2631 SIVAL(p, 0, 0);
2632 SIVAL(p + 4, 0, modellen);
2633 ok = data_blob_append(req, &blob, p, 8);
2634 if (!ok) {
2635 talloc_free(model);
2636 return NT_STATUS_UNSUCCESSFUL;
2639 ok = data_blob_append(req, &blob, model, modellen);
2640 talloc_free(model);
2641 if (!ok) {
2642 return NT_STATUS_UNSUCCESSFUL;
2646 status = smb2_create_blob_add(out_context_blobs,
2647 out_context_blobs,
2648 SMB2_CREATE_TAG_AAPL,
2649 blob);
2650 if (NT_STATUS_IS_OK(status)) {
2651 global_fruit_config.nego_aapl = true;
2652 if (config->aapl_zero_file_id) {
2653 aapl_force_zero_file_id(handle->conn->sconn);
2657 return status;
2660 static bool readdir_attr_meta_finderi_stream(
2661 struct vfs_handle_struct *handle,
2662 const struct smb_filename *smb_fname,
2663 AfpInfo *ai)
2665 struct smb_filename *stream_name = NULL;
2666 files_struct *fsp = NULL;
2667 ssize_t nread;
2668 NTSTATUS status;
2669 int ret;
2670 bool ok;
2671 uint8_t buf[AFP_INFO_SIZE];
2673 stream_name = synthetic_smb_fname(talloc_tos(),
2674 smb_fname->base_name,
2675 AFPINFO_STREAM_NAME,
2676 NULL, smb_fname->flags);
2677 if (stream_name == NULL) {
2678 return false;
2681 ret = SMB_VFS_STAT(handle->conn, stream_name);
2682 if (ret != 0) {
2683 return false;
2686 status = SMB_VFS_CREATE_FILE(
2687 handle->conn, /* conn */
2688 NULL, /* req */
2689 0, /* root_dir_fid */
2690 stream_name, /* fname */
2691 FILE_READ_DATA, /* access_mask */
2692 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2693 FILE_SHARE_DELETE),
2694 FILE_OPEN, /* create_disposition*/
2695 0, /* create_options */
2696 0, /* file_attributes */
2697 INTERNAL_OPEN_ONLY, /* oplock_request */
2698 NULL, /* lease */
2699 0, /* allocation_size */
2700 0, /* private_flags */
2701 NULL, /* sd */
2702 NULL, /* ea_list */
2703 &fsp, /* result */
2704 NULL, /* pinfo */
2705 NULL, NULL); /* create context */
2707 TALLOC_FREE(stream_name);
2709 if (!NT_STATUS_IS_OK(status)) {
2710 return false;
2713 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2714 if (nread != AFP_INFO_SIZE) {
2715 DBG_ERR("short read [%s] [%zd/%d]\n",
2716 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2717 ok = false;
2718 goto fail;
2721 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2722 AFP_FinderSize);
2724 ok = true;
2726 fail:
2727 if (fsp != NULL) {
2728 close_file(NULL, fsp, NORMAL_CLOSE);
2731 return ok;
2734 static bool readdir_attr_meta_finderi_netatalk(
2735 struct vfs_handle_struct *handle,
2736 const struct smb_filename *smb_fname,
2737 AfpInfo *ai)
2739 struct adouble *ad = NULL;
2740 char *p = NULL;
2742 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2743 if (ad == NULL) {
2744 return false;
2747 p = ad_get_entry(ad, ADEID_FINDERI);
2748 if (p == NULL) {
2749 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2750 TALLOC_FREE(ad);
2751 return false;
2754 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2755 TALLOC_FREE(ad);
2756 return true;
2759 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2760 const struct smb_filename *smb_fname,
2761 struct readdir_attr_data *attr_data)
2763 struct fruit_config_data *config = NULL;
2764 uint32_t date_added;
2765 AfpInfo ai = {0};
2766 bool ok;
2768 SMB_VFS_HANDLE_GET_DATA(handle, config,
2769 struct fruit_config_data,
2770 return false);
2772 switch (config->meta) {
2773 case FRUIT_META_NETATALK:
2774 ok = readdir_attr_meta_finderi_netatalk(
2775 handle, smb_fname, &ai);
2776 break;
2778 case FRUIT_META_STREAM:
2779 ok = readdir_attr_meta_finderi_stream(
2780 handle, smb_fname, &ai);
2781 break;
2783 default:
2784 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2785 return false;
2788 if (!ok) {
2789 /* Don't bother with errors, it's likely ENOENT */
2790 return true;
2793 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2794 /* finder_type */
2795 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2796 &ai.afpi_FinderInfo[0], 4);
2798 /* finder_creator */
2799 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2800 &ai.afpi_FinderInfo[4], 4);
2803 /* finder_flags */
2804 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2805 &ai.afpi_FinderInfo[8], 2);
2807 /* finder_ext_flags */
2808 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2809 &ai.afpi_FinderInfo[24], 2);
2811 /* creation date */
2812 date_added = convert_time_t_to_uint32_t(
2813 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2815 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2817 return true;
2820 static uint64_t readdir_attr_rfork_size_adouble(
2821 struct vfs_handle_struct *handle,
2822 const struct smb_filename *smb_fname)
2824 struct adouble *ad = NULL;
2825 uint64_t rfork_size;
2827 ad = ad_get(talloc_tos(), handle, smb_fname,
2828 ADOUBLE_RSRC);
2829 if (ad == NULL) {
2830 return 0;
2833 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2834 TALLOC_FREE(ad);
2836 return rfork_size;
2839 static uint64_t readdir_attr_rfork_size_stream(
2840 struct vfs_handle_struct *handle,
2841 const struct smb_filename *smb_fname)
2843 struct smb_filename *stream_name = NULL;
2844 int ret;
2845 uint64_t rfork_size;
2847 stream_name = synthetic_smb_fname(talloc_tos(),
2848 smb_fname->base_name,
2849 AFPRESOURCE_STREAM_NAME,
2850 NULL, 0);
2851 if (stream_name == NULL) {
2852 return 0;
2855 ret = SMB_VFS_STAT(handle->conn, stream_name);
2856 if (ret != 0) {
2857 TALLOC_FREE(stream_name);
2858 return 0;
2861 rfork_size = stream_name->st.st_ex_size;
2862 TALLOC_FREE(stream_name);
2864 return rfork_size;
2867 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2868 const struct smb_filename *smb_fname)
2870 struct fruit_config_data *config = NULL;
2871 uint64_t rfork_size;
2873 SMB_VFS_HANDLE_GET_DATA(handle, config,
2874 struct fruit_config_data,
2875 return 0);
2877 switch (config->rsrc) {
2878 case FRUIT_RSRC_ADFILE:
2879 case FRUIT_RSRC_XATTR:
2880 rfork_size = readdir_attr_rfork_size_adouble(handle,
2881 smb_fname);
2882 break;
2884 case FRUIT_META_STREAM:
2885 rfork_size = readdir_attr_rfork_size_stream(handle,
2886 smb_fname);
2887 break;
2889 default:
2890 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2891 rfork_size = 0;
2892 break;
2895 return rfork_size;
2898 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2899 const struct smb_filename *smb_fname,
2900 struct readdir_attr_data *attr_data)
2902 NTSTATUS status = NT_STATUS_OK;
2903 struct fruit_config_data *config = NULL;
2904 bool ok;
2906 SMB_VFS_HANDLE_GET_DATA(handle, config,
2907 struct fruit_config_data,
2908 return NT_STATUS_UNSUCCESSFUL);
2911 /* Ensure we return a default value in the creation_date field */
2912 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2915 * Resource fork length
2918 if (config->readdir_attr_rsize) {
2919 uint64_t rfork_size;
2921 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2922 attr_data->attr_data.aapl.rfork_size = rfork_size;
2926 * FinderInfo
2929 if (config->readdir_attr_finder_info) {
2930 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2931 if (!ok) {
2932 status = NT_STATUS_INTERNAL_ERROR;
2936 return status;
2939 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
2941 NTSTATUS status;
2942 uint32_t i;
2944 if (psd->dacl == NULL) {
2945 return NT_STATUS_OK;
2948 for (i = 0; i < psd->dacl->num_aces; i++) {
2949 /* MS NFS style mode/uid/gid */
2950 int cmp = dom_sid_compare_domain(
2951 &global_sid_Unix_NFS,
2952 &psd->dacl->aces[i].trustee);
2953 if (cmp != 0) {
2954 /* Normal ACE entry. */
2955 continue;
2959 * security_descriptor_dacl_del()
2960 * *must* return NT_STATUS_OK as we know
2961 * we have something to remove.
2964 status = security_descriptor_dacl_del(psd,
2965 &psd->dacl->aces[i].trustee);
2966 if (!NT_STATUS_IS_OK(status)) {
2967 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
2968 nt_errstr(status));
2969 return status;
2973 * security_descriptor_dacl_del() may delete more
2974 * then one entry subsequent to this one if the
2975 * SID matches, but we only need to ensure that
2976 * we stay looking at the same element in the array.
2978 i--;
2980 return NT_STATUS_OK;
2983 /* Search MS NFS style ACE with UNIX mode */
2984 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2985 files_struct *fsp,
2986 struct security_descriptor *psd,
2987 mode_t *pmode,
2988 bool *pdo_chmod)
2990 uint32_t i;
2991 struct fruit_config_data *config = NULL;
2993 *pdo_chmod = false;
2995 SMB_VFS_HANDLE_GET_DATA(handle, config,
2996 struct fruit_config_data,
2997 return NT_STATUS_UNSUCCESSFUL);
2999 if (!global_fruit_config.nego_aapl) {
3000 return NT_STATUS_OK;
3002 if (psd->dacl == NULL || !config->unix_info_enabled) {
3003 return NT_STATUS_OK;
3006 for (i = 0; i < psd->dacl->num_aces; i++) {
3007 if (dom_sid_compare_domain(
3008 &global_sid_Unix_NFS_Mode,
3009 &psd->dacl->aces[i].trustee) == 0) {
3010 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3011 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3012 *pdo_chmod = true;
3014 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3015 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3016 break;
3021 * Remove any incoming virtual ACE entries generated by
3022 * fruit_fget_nt_acl().
3025 return remove_virtual_nfs_aces(psd);
3028 /****************************************************************************
3029 * VFS ops
3030 ****************************************************************************/
3032 static int fruit_connect(vfs_handle_struct *handle,
3033 const char *service,
3034 const char *user)
3036 int rc;
3037 char *list = NULL, *newlist = NULL;
3038 struct fruit_config_data *config;
3040 DEBUG(10, ("fruit_connect\n"));
3042 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3043 if (rc < 0) {
3044 return rc;
3047 rc = init_fruit_config(handle);
3048 if (rc != 0) {
3049 return rc;
3052 SMB_VFS_HANDLE_GET_DATA(handle, config,
3053 struct fruit_config_data, return -1);
3055 if (config->veto_appledouble) {
3056 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3058 if (list) {
3059 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3060 newlist = talloc_asprintf(
3061 list,
3062 "%s/" ADOUBLE_NAME_PREFIX "*/",
3063 list);
3064 lp_do_parameter(SNUM(handle->conn),
3065 "veto files",
3066 newlist);
3068 } else {
3069 lp_do_parameter(SNUM(handle->conn),
3070 "veto files",
3071 "/" ADOUBLE_NAME_PREFIX "*/");
3074 TALLOC_FREE(list);
3077 if (config->encoding == FRUIT_ENC_NATIVE) {
3078 lp_do_parameter(SNUM(handle->conn),
3079 "catia:mappings",
3080 fruit_catia_maps);
3083 return rc;
3086 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3087 struct smb_filename *smb_fname,
3088 files_struct *fsp,
3089 int flags,
3090 mode_t mode)
3092 AfpInfo *ai = NULL;
3093 char afpinfo_buf[AFP_INFO_SIZE];
3094 ssize_t len, written;
3095 int hostfd = -1;
3096 int rc = -1;
3098 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3099 if (hostfd == -1) {
3100 return -1;
3103 if (!(flags & (O_CREAT | O_TRUNC))) {
3104 return hostfd;
3107 ai = afpinfo_new(talloc_tos());
3108 if (ai == NULL) {
3109 rc = -1;
3110 goto fail;
3113 len = afpinfo_pack(ai, afpinfo_buf);
3114 if (len != AFP_INFO_SIZE) {
3115 rc = -1;
3116 goto fail;
3119 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3120 fsp->fh->fd = hostfd;
3122 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3123 AFP_INFO_SIZE, 0);
3124 fsp->fh->fd = -1;
3125 if (written != AFP_INFO_SIZE) {
3126 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3127 rc = -1;
3128 goto fail;
3131 rc = 0;
3133 fail:
3134 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3136 if (rc != 0) {
3137 int saved_errno = errno;
3138 if (hostfd >= 0) {
3139 fsp->fh->fd = hostfd;
3140 SMB_VFS_NEXT_CLOSE(handle, fsp);
3142 hostfd = -1;
3143 errno = saved_errno;
3145 return hostfd;
3148 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3149 struct smb_filename *smb_fname,
3150 files_struct *fsp,
3151 int flags,
3152 mode_t mode)
3154 int rc;
3155 int fakefd = -1;
3156 struct adouble *ad = NULL;
3157 int fds[2];
3159 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3162 * Return a valid fd, but ensure any attempt to use it returns an error
3163 * (EPIPE). All operations on the smb_fname or the fsp will use path
3164 * based syscalls.
3166 rc = pipe(fds);
3167 if (rc != 0) {
3168 goto exit;
3170 fakefd = fds[0];
3171 close(fds[1]);
3173 if (flags & (O_CREAT | O_TRUNC)) {
3175 * The attribute does not exist or needs to be truncated,
3176 * create an AppleDouble EA
3178 ad = ad_init(fsp, handle, ADOUBLE_META);
3179 if (ad == NULL) {
3180 rc = -1;
3181 goto exit;
3184 rc = ad_set(ad, fsp->fsp_name);
3185 if (rc != 0) {
3186 rc = -1;
3187 goto exit;
3190 TALLOC_FREE(ad);
3193 exit:
3194 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3195 if (rc != 0) {
3196 int saved_errno = errno;
3197 if (fakefd >= 0) {
3198 close(fakefd);
3200 fakefd = -1;
3201 errno = saved_errno;
3203 return fakefd;
3206 static int fruit_open_meta(vfs_handle_struct *handle,
3207 struct smb_filename *smb_fname,
3208 files_struct *fsp, int flags, mode_t mode)
3210 int fd;
3211 struct fruit_config_data *config = NULL;
3212 struct fio *fio = NULL;
3214 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3216 SMB_VFS_HANDLE_GET_DATA(handle, config,
3217 struct fruit_config_data, return -1);
3219 switch (config->meta) {
3220 case FRUIT_META_STREAM:
3221 fd = fruit_open_meta_stream(handle, smb_fname,
3222 fsp, flags, mode);
3223 break;
3225 case FRUIT_META_NETATALK:
3226 fd = fruit_open_meta_netatalk(handle, smb_fname,
3227 fsp, flags, mode);
3228 break;
3230 default:
3231 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3232 return -1;
3235 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3237 if (fd == -1) {
3238 return -1;
3241 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3242 fio->type = ADOUBLE_META;
3243 fio->config = config;
3245 return fd;
3248 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3249 struct smb_filename *smb_fname,
3250 files_struct *fsp,
3251 int flags,
3252 mode_t mode)
3254 int rc = 0;
3255 struct adouble *ad = NULL;
3256 struct smb_filename *smb_fname_base = NULL;
3257 struct fruit_config_data *config = NULL;
3258 int hostfd = -1;
3260 SMB_VFS_HANDLE_GET_DATA(handle, config,
3261 struct fruit_config_data, return -1);
3263 if ((!(flags & O_CREAT)) &&
3264 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3266 /* sorry, but directories don't habe a resource fork */
3267 rc = -1;
3268 goto exit;
3271 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3272 if (rc != 0) {
3273 goto exit;
3276 /* Sanitize flags */
3277 if (flags & O_WRONLY) {
3278 /* We always need read access for the metadata header too */
3279 flags &= ~O_WRONLY;
3280 flags |= O_RDWR;
3283 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3284 flags, mode);
3285 if (hostfd == -1) {
3286 rc = -1;
3287 goto exit;
3290 if (flags & (O_CREAT | O_TRUNC)) {
3291 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3292 if (ad == NULL) {
3293 rc = -1;
3294 goto exit;
3297 fsp->fh->fd = hostfd;
3299 rc = ad_fset(ad, fsp);
3300 fsp->fh->fd = -1;
3301 if (rc != 0) {
3302 rc = -1;
3303 goto exit;
3305 TALLOC_FREE(ad);
3308 exit:
3310 TALLOC_FREE(smb_fname_base);
3312 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3313 if (rc != 0) {
3314 int saved_errno = errno;
3315 if (hostfd >= 0) {
3317 * BUGBUGBUG -- we would need to call
3318 * fd_close_posix here, but we don't have a
3319 * full fsp yet
3321 fsp->fh->fd = hostfd;
3322 SMB_VFS_CLOSE(fsp);
3324 hostfd = -1;
3325 errno = saved_errno;
3327 return hostfd;
3330 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3331 struct smb_filename *smb_fname,
3332 files_struct *fsp,
3333 int flags,
3334 mode_t mode)
3336 #ifdef HAVE_ATTROPEN
3337 int fd = -1;
3339 fd = attropen(smb_fname->base_name,
3340 AFPRESOURCE_EA_NETATALK,
3341 flags,
3342 mode);
3343 if (fd == -1) {
3344 return -1;
3347 return fd;
3349 #else
3350 errno = ENOSYS;
3351 return -1;
3352 #endif
3355 static int fruit_open_rsrc(vfs_handle_struct *handle,
3356 struct smb_filename *smb_fname,
3357 files_struct *fsp, int flags, mode_t mode)
3359 int fd;
3360 struct fruit_config_data *config = NULL;
3361 struct fio *fio = NULL;
3363 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3365 SMB_VFS_HANDLE_GET_DATA(handle, config,
3366 struct fruit_config_data, return -1);
3368 if (((flags & O_ACCMODE) == O_RDONLY)
3369 && (flags & O_CREAT)
3370 && !VALID_STAT(fsp->fsp_name->st))
3373 * This means the stream doesn't exist. macOS SMB server fails
3374 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3375 * 12565 and the test for this combination in
3376 * test_rfork_create().
3378 errno = ENOENT;
3379 return -1;
3382 switch (config->rsrc) {
3383 case FRUIT_RSRC_STREAM:
3384 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3385 break;
3387 case FRUIT_RSRC_ADFILE:
3388 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3389 fsp, flags, mode);
3390 break;
3392 case FRUIT_RSRC_XATTR:
3393 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3394 fsp, flags, mode);
3395 break;
3397 default:
3398 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3399 return -1;
3402 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3404 if (fd == -1) {
3405 return -1;
3408 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3409 fio->type = ADOUBLE_RSRC;
3410 fio->config = config;
3412 return fd;
3415 static int fruit_open(vfs_handle_struct *handle,
3416 struct smb_filename *smb_fname,
3417 files_struct *fsp, int flags, mode_t mode)
3419 int fd;
3421 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3423 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3424 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3427 if (is_afpinfo_stream(smb_fname)) {
3428 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3429 } else if (is_afpresource_stream(smb_fname)) {
3430 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3431 } else {
3432 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3435 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3437 return fd;
3440 static int fruit_rename(struct vfs_handle_struct *handle,
3441 const struct smb_filename *smb_fname_src,
3442 const struct smb_filename *smb_fname_dst)
3444 int rc = -1;
3445 struct fruit_config_data *config = NULL;
3446 struct smb_filename *src_adp_smb_fname = NULL;
3447 struct smb_filename *dst_adp_smb_fname = NULL;
3449 SMB_VFS_HANDLE_GET_DATA(handle, config,
3450 struct fruit_config_data, return -1);
3452 if (!VALID_STAT(smb_fname_src->st)) {
3453 DBG_ERR("Need valid stat for [%s]\n",
3454 smb_fname_str_dbg(smb_fname_src));
3455 return -1;
3458 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3459 if (rc != 0) {
3460 return -1;
3463 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3464 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3466 return 0;
3469 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3470 if (rc != 0) {
3471 goto done;
3474 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3475 if (rc != 0) {
3476 goto done;
3479 DBG_DEBUG("%s -> %s\n",
3480 smb_fname_str_dbg(src_adp_smb_fname),
3481 smb_fname_str_dbg(dst_adp_smb_fname));
3483 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3484 if (errno == ENOENT) {
3485 rc = 0;
3488 done:
3489 TALLOC_FREE(src_adp_smb_fname);
3490 TALLOC_FREE(dst_adp_smb_fname);
3491 return rc;
3494 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3495 const struct smb_filename *smb_fname)
3497 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3500 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3501 const struct smb_filename *smb_fname)
3503 return SMB_VFS_REMOVEXATTR(handle->conn,
3504 smb_fname,
3505 AFPINFO_EA_NETATALK);
3508 static int fruit_unlink_meta(vfs_handle_struct *handle,
3509 const struct smb_filename *smb_fname)
3511 struct fruit_config_data *config = NULL;
3512 int rc;
3514 SMB_VFS_HANDLE_GET_DATA(handle, config,
3515 struct fruit_config_data, return -1);
3517 switch (config->meta) {
3518 case FRUIT_META_STREAM:
3519 rc = fruit_unlink_meta_stream(handle, smb_fname);
3520 break;
3522 case FRUIT_META_NETATALK:
3523 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3524 break;
3526 default:
3527 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3528 return -1;
3531 return rc;
3534 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3535 const struct smb_filename *smb_fname,
3536 bool force_unlink)
3538 int ret;
3540 if (!force_unlink) {
3541 struct smb_filename *smb_fname_cp = NULL;
3542 off_t size;
3544 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3545 if (smb_fname_cp == NULL) {
3546 return -1;
3550 * 0 byte resource fork streams are not listed by
3551 * vfs_streaminfo, as a result stream cleanup/deletion of file
3552 * deletion doesn't remove the resourcefork stream.
3555 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3556 if (ret != 0) {
3557 TALLOC_FREE(smb_fname_cp);
3558 DBG_ERR("stat [%s] failed [%s]\n",
3559 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3560 return -1;
3563 size = smb_fname_cp->st.st_ex_size;
3564 TALLOC_FREE(smb_fname_cp);
3566 if (size > 0) {
3567 /* OS X ignores resource fork stream delete requests */
3568 return 0;
3572 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3573 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3574 ret = 0;
3577 return ret;
3580 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3581 const struct smb_filename *smb_fname,
3582 bool force_unlink)
3584 int rc;
3585 struct adouble *ad = NULL;
3586 struct smb_filename *adp_smb_fname = NULL;
3588 if (!force_unlink) {
3589 ad = ad_get(talloc_tos(), handle, smb_fname,
3590 ADOUBLE_RSRC);
3591 if (ad == NULL) {
3592 errno = ENOENT;
3593 return -1;
3598 * 0 byte resource fork streams are not listed by
3599 * vfs_streaminfo, as a result stream cleanup/deletion of file
3600 * deletion doesn't remove the resourcefork stream.
3603 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3604 /* OS X ignores resource fork stream delete requests */
3605 TALLOC_FREE(ad);
3606 return 0;
3609 TALLOC_FREE(ad);
3612 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3613 if (rc != 0) {
3614 return -1;
3617 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3618 TALLOC_FREE(adp_smb_fname);
3619 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3620 rc = 0;
3623 return rc;
3626 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3627 const struct smb_filename *smb_fname,
3628 bool force_unlink)
3631 * OS X ignores resource fork stream delete requests, so nothing to do
3632 * here. Removing the file will remove the xattr anyway, so we don't
3633 * have to take care of removing 0 byte resource forks that could be
3634 * left behind.
3636 return 0;
3639 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3640 const struct smb_filename *smb_fname,
3641 bool force_unlink)
3643 struct fruit_config_data *config = NULL;
3644 int rc;
3646 SMB_VFS_HANDLE_GET_DATA(handle, config,
3647 struct fruit_config_data, return -1);
3649 switch (config->rsrc) {
3650 case FRUIT_RSRC_STREAM:
3651 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3652 break;
3654 case FRUIT_RSRC_ADFILE:
3655 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3656 break;
3658 case FRUIT_RSRC_XATTR:
3659 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3660 break;
3662 default:
3663 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3664 return -1;
3667 return rc;
3670 static int fruit_unlink(vfs_handle_struct *handle,
3671 const struct smb_filename *smb_fname)
3673 int rc;
3674 struct fruit_config_data *config = NULL;
3675 struct smb_filename *rsrc_smb_fname = NULL;
3677 SMB_VFS_HANDLE_GET_DATA(handle, config,
3678 struct fruit_config_data, return -1);
3680 if (is_afpinfo_stream(smb_fname)) {
3681 return fruit_unlink_meta(handle, smb_fname);
3682 } else if (is_afpresource_stream(smb_fname)) {
3683 return fruit_unlink_rsrc(handle, smb_fname, false);
3684 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3685 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3689 * A request to delete the base file. Because 0 byte resource
3690 * fork streams are not listed by fruit_streaminfo,
3691 * delete_all_streams() can't remove 0 byte resource fork
3692 * streams, so we have to cleanup this here.
3694 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3695 smb_fname->base_name,
3696 AFPRESOURCE_STREAM_NAME,
3697 NULL,
3698 smb_fname->flags);
3699 if (rsrc_smb_fname == NULL) {
3700 return -1;
3703 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3704 if ((rc != 0) && (errno != ENOENT)) {
3705 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3706 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3707 TALLOC_FREE(rsrc_smb_fname);
3708 return -1;
3710 TALLOC_FREE(rsrc_smb_fname);
3712 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3715 static int fruit_chmod(vfs_handle_struct *handle,
3716 const struct smb_filename *smb_fname,
3717 mode_t mode)
3719 int rc = -1;
3720 struct fruit_config_data *config = NULL;
3721 struct smb_filename *smb_fname_adp = NULL;
3723 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3724 if (rc != 0) {
3725 return rc;
3728 SMB_VFS_HANDLE_GET_DATA(handle, config,
3729 struct fruit_config_data, return -1);
3731 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3732 return 0;
3735 if (!VALID_STAT(smb_fname->st)) {
3736 return 0;
3739 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3740 return 0;
3743 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3744 if (rc != 0) {
3745 return -1;
3748 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3750 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3751 if (errno == ENOENT) {
3752 rc = 0;
3755 TALLOC_FREE(smb_fname_adp);
3756 return rc;
3759 static int fruit_chown(vfs_handle_struct *handle,
3760 const struct smb_filename *smb_fname,
3761 uid_t uid,
3762 gid_t gid)
3764 int rc = -1;
3765 struct fruit_config_data *config = NULL;
3766 struct smb_filename *adp_smb_fname = NULL;
3768 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3769 if (rc != 0) {
3770 return rc;
3773 SMB_VFS_HANDLE_GET_DATA(handle, config,
3774 struct fruit_config_data, return -1);
3776 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3777 return 0;
3780 if (!VALID_STAT(smb_fname->st)) {
3781 return 0;
3784 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3785 return 0;
3788 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3789 if (rc != 0) {
3790 goto done;
3793 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3795 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3796 if (errno == ENOENT) {
3797 rc = 0;
3800 done:
3801 TALLOC_FREE(adp_smb_fname);
3802 return rc;
3805 static int fruit_rmdir(struct vfs_handle_struct *handle,
3806 const struct smb_filename *smb_fname)
3808 DIR *dh = NULL;
3809 struct dirent *de;
3810 struct fruit_config_data *config;
3812 SMB_VFS_HANDLE_GET_DATA(handle, config,
3813 struct fruit_config_data, return -1);
3815 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3816 goto exit_rmdir;
3820 * Due to there is no way to change bDeleteVetoFiles variable
3821 * from this module, need to clean up ourselves
3824 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3825 if (dh == NULL) {
3826 goto exit_rmdir;
3829 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3830 int match;
3831 struct adouble *ad = NULL;
3832 char *p = NULL;
3833 struct smb_filename *ad_smb_fname = NULL;
3834 int ret;
3836 match = strncmp(de->d_name,
3837 ADOUBLE_NAME_PREFIX,
3838 strlen(ADOUBLE_NAME_PREFIX));
3839 if (match != 0) {
3840 continue;
3843 p = talloc_asprintf(talloc_tos(), "%s/%s",
3844 smb_fname->base_name, de->d_name);
3845 if (p == NULL) {
3846 DBG_ERR("talloc_asprintf failed\n");
3847 return -1;
3850 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3851 NULL, NULL,
3852 smb_fname->flags);
3853 TALLOC_FREE(p);
3854 if (ad_smb_fname == NULL) {
3855 DBG_ERR("synthetic_smb_fname failed\n");
3856 return -1;
3860 * Check whether it's a valid AppleDouble file, if
3861 * yes, delete it, ignore it otherwise.
3863 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3864 if (ad == NULL) {
3865 TALLOC_FREE(ad_smb_fname);
3866 TALLOC_FREE(p);
3867 continue;
3869 TALLOC_FREE(ad);
3871 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3872 TALLOC_FREE(ad_smb_fname);
3873 if (ret != 0) {
3874 DBG_ERR("Deleting [%s] failed\n",
3875 smb_fname_str_dbg(ad_smb_fname));
3879 exit_rmdir:
3880 if (dh) {
3881 SMB_VFS_CLOSEDIR(handle->conn, dh);
3883 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3886 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3887 files_struct *fsp, void *data,
3888 size_t n, off_t offset)
3890 ssize_t nread;
3891 int ret;
3893 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3895 if (nread == n) {
3896 return nread;
3899 DBG_ERR("Removing [%s] after short read [%zd]\n",
3900 fsp_str_dbg(fsp), nread);
3902 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3903 if (ret != 0) {
3904 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3905 return -1;
3908 errno = EINVAL;
3909 return -1;
3912 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3913 files_struct *fsp, void *data,
3914 size_t n, off_t offset)
3916 AfpInfo *ai = NULL;
3917 struct adouble *ad = NULL;
3918 char afpinfo_buf[AFP_INFO_SIZE];
3919 char *p = NULL;
3920 ssize_t nread;
3922 ai = afpinfo_new(talloc_tos());
3923 if (ai == NULL) {
3924 return -1;
3927 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3928 if (ad == NULL) {
3929 nread = -1;
3930 goto fail;
3933 p = ad_get_entry(ad, ADEID_FINDERI);
3934 if (p == NULL) {
3935 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3936 nread = -1;
3937 goto fail;
3940 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3942 nread = afpinfo_pack(ai, afpinfo_buf);
3943 if (nread != AFP_INFO_SIZE) {
3944 nread = -1;
3945 goto fail;
3948 memcpy(data, afpinfo_buf, n);
3949 nread = n;
3951 fail:
3952 TALLOC_FREE(ai);
3953 return nread;
3956 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3957 files_struct *fsp, void *data,
3958 size_t n, off_t offset)
3960 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3961 ssize_t nread;
3962 ssize_t to_return;
3965 * OS X has a off-by-1 error in the offset calculation, so we're
3966 * bug compatible here. It won't hurt, as any relevant real
3967 * world read requests from the AFP_AfpInfo stream will be
3968 * offset=0 n=60. offset is ignored anyway, see below.
3970 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3971 return 0;
3974 /* Yes, macOS always reads from offset 0 */
3975 offset = 0;
3976 to_return = MIN(n, AFP_INFO_SIZE);
3978 switch (fio->config->meta) {
3979 case FRUIT_META_STREAM:
3980 nread = fruit_pread_meta_stream(handle, fsp, data,
3981 to_return, offset);
3982 break;
3984 case FRUIT_META_NETATALK:
3985 nread = fruit_pread_meta_adouble(handle, fsp, data,
3986 to_return, offset);
3987 break;
3989 default:
3990 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3991 return -1;
3994 return nread;
3997 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3998 files_struct *fsp, void *data,
3999 size_t n, off_t offset)
4001 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4004 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4005 files_struct *fsp, void *data,
4006 size_t n, off_t offset)
4008 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4011 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4012 files_struct *fsp, void *data,
4013 size_t n, off_t offset)
4015 struct adouble *ad = NULL;
4016 ssize_t nread;
4018 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4019 if (ad == NULL) {
4020 return -1;
4023 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4024 offset + ad_getentryoff(ad, ADEID_RFORK));
4026 TALLOC_FREE(ad);
4027 return nread;
4030 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4031 files_struct *fsp, void *data,
4032 size_t n, off_t offset)
4034 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4035 ssize_t nread;
4037 switch (fio->config->rsrc) {
4038 case FRUIT_RSRC_STREAM:
4039 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4040 break;
4042 case FRUIT_RSRC_ADFILE:
4043 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4044 break;
4046 case FRUIT_RSRC_XATTR:
4047 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4048 break;
4050 default:
4051 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4052 return -1;
4055 return nread;
4058 static ssize_t fruit_pread(vfs_handle_struct *handle,
4059 files_struct *fsp, void *data,
4060 size_t n, off_t offset)
4062 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4063 ssize_t nread;
4065 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4066 fsp_str_dbg(fsp), (intmax_t)offset, n);
4068 if (fio == NULL) {
4069 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4072 if (fio->type == ADOUBLE_META) {
4073 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4074 } else {
4075 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4078 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4079 return nread;
4082 static bool fruit_must_handle_aio_stream(struct fio *fio)
4084 if (fio == NULL) {
4085 return false;
4088 if ((fio->type == ADOUBLE_META) &&
4089 (fio->config->meta == FRUIT_META_NETATALK))
4091 return true;
4094 if ((fio->type == ADOUBLE_RSRC) &&
4095 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4097 return true;
4100 return false;
4103 struct fruit_pread_state {
4104 ssize_t nread;
4105 struct vfs_aio_state vfs_aio_state;
4108 static void fruit_pread_done(struct tevent_req *subreq);
4110 static struct tevent_req *fruit_pread_send(
4111 struct vfs_handle_struct *handle,
4112 TALLOC_CTX *mem_ctx,
4113 struct tevent_context *ev,
4114 struct files_struct *fsp,
4115 void *data,
4116 size_t n, off_t offset)
4118 struct tevent_req *req = NULL;
4119 struct tevent_req *subreq = NULL;
4120 struct fruit_pread_state *state = NULL;
4121 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4123 req = tevent_req_create(mem_ctx, &state,
4124 struct fruit_pread_state);
4125 if (req == NULL) {
4126 return NULL;
4129 if (fruit_must_handle_aio_stream(fio)) {
4130 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4131 if (state->nread != n) {
4132 if (state->nread != -1) {
4133 errno = EIO;
4135 tevent_req_error(req, errno);
4136 return tevent_req_post(req, ev);
4138 tevent_req_done(req);
4139 return tevent_req_post(req, ev);
4142 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4143 data, n, offset);
4144 if (tevent_req_nomem(req, subreq)) {
4145 return tevent_req_post(req, ev);
4147 tevent_req_set_callback(subreq, fruit_pread_done, req);
4148 return req;
4151 static void fruit_pread_done(struct tevent_req *subreq)
4153 struct tevent_req *req = tevent_req_callback_data(
4154 subreq, struct tevent_req);
4155 struct fruit_pread_state *state = tevent_req_data(
4156 req, struct fruit_pread_state);
4158 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4159 TALLOC_FREE(subreq);
4161 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4162 return;
4164 tevent_req_done(req);
4167 static ssize_t fruit_pread_recv(struct tevent_req *req,
4168 struct vfs_aio_state *vfs_aio_state)
4170 struct fruit_pread_state *state = tevent_req_data(
4171 req, struct fruit_pread_state);
4173 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4174 return -1;
4177 *vfs_aio_state = state->vfs_aio_state;
4178 return state->nread;
4181 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4182 files_struct *fsp, const void *data,
4183 size_t n, off_t offset)
4185 AfpInfo *ai = NULL;
4186 size_t nwritten;
4187 bool ok;
4189 ai = afpinfo_unpack(talloc_tos(), data);
4190 if (ai == NULL) {
4191 return -1;
4194 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4195 if (nwritten != n) {
4196 return -1;
4199 if (!ai_empty_finderinfo(ai)) {
4200 return n;
4203 ok = set_delete_on_close(
4204 fsp,
4205 true,
4206 handle->conn->session_info->security_token,
4207 handle->conn->session_info->unix_token);
4208 if (!ok) {
4209 DBG_ERR("set_delete_on_close on [%s] failed\n",
4210 fsp_str_dbg(fsp));
4211 return -1;
4214 return n;
4217 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4218 files_struct *fsp, const void *data,
4219 size_t n, off_t offset)
4221 struct adouble *ad = NULL;
4222 AfpInfo *ai = NULL;
4223 char *p = NULL;
4224 int ret;
4225 bool ok;
4227 ai = afpinfo_unpack(talloc_tos(), data);
4228 if (ai == NULL) {
4229 return -1;
4232 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4233 if (ad == NULL) {
4234 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4235 if (ad == NULL) {
4236 return -1;
4239 p = ad_get_entry(ad, ADEID_FINDERI);
4240 if (p == NULL) {
4241 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4242 TALLOC_FREE(ad);
4243 return -1;
4246 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4248 ret = ad_fset(ad, fsp);
4249 if (ret != 0) {
4250 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4251 TALLOC_FREE(ad);
4252 return -1;
4255 TALLOC_FREE(ad);
4257 if (!ai_empty_finderinfo(ai)) {
4258 return n;
4261 ok = set_delete_on_close(
4262 fsp,
4263 true,
4264 handle->conn->session_info->security_token,
4265 handle->conn->session_info->unix_token);
4266 if (!ok) {
4267 DBG_ERR("set_delete_on_close on [%s] failed\n",
4268 fsp_str_dbg(fsp));
4269 return -1;
4272 return n;
4275 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4276 files_struct *fsp, const void *data,
4277 size_t n, off_t offset)
4279 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4280 ssize_t nwritten;
4283 * Writing an all 0 blob to the metadata stream
4284 * results in the stream being removed on a macOS
4285 * server. This ensures we behave the same and it
4286 * verified by the "delete AFP_AfpInfo by writing all
4287 * 0" test.
4289 if (n != AFP_INFO_SIZE || offset != 0) {
4290 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4291 (intmax_t)offset, (intmax_t)n);
4292 return -1;
4295 switch (fio->config->meta) {
4296 case FRUIT_META_STREAM:
4297 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4298 n, offset);
4299 break;
4301 case FRUIT_META_NETATALK:
4302 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4303 n, offset);
4304 break;
4306 default:
4307 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4308 return -1;
4311 return nwritten;
4314 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4315 files_struct *fsp, const void *data,
4316 size_t n, off_t offset)
4318 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4321 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4322 files_struct *fsp, const void *data,
4323 size_t n, off_t offset)
4325 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4328 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4329 files_struct *fsp, const void *data,
4330 size_t n, off_t offset)
4332 struct adouble *ad = NULL;
4333 ssize_t nwritten;
4334 int ret;
4336 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4337 if (ad == NULL) {
4338 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4339 return -1;
4342 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4343 offset + ad_getentryoff(ad, ADEID_RFORK));
4344 if (nwritten != n) {
4345 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4346 fsp_str_dbg(fsp), nwritten, n);
4347 TALLOC_FREE(ad);
4348 return -1;
4351 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4352 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4353 ret = ad_fset(ad, fsp);
4354 if (ret != 0) {
4355 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4356 TALLOC_FREE(ad);
4357 return -1;
4361 TALLOC_FREE(ad);
4362 return n;
4365 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4366 files_struct *fsp, const void *data,
4367 size_t n, off_t offset)
4369 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4370 ssize_t nwritten;
4372 switch (fio->config->rsrc) {
4373 case FRUIT_RSRC_STREAM:
4374 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4375 break;
4377 case FRUIT_RSRC_ADFILE:
4378 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4379 break;
4381 case FRUIT_RSRC_XATTR:
4382 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4383 break;
4385 default:
4386 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4387 return -1;
4390 return nwritten;
4393 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4394 files_struct *fsp, const void *data,
4395 size_t n, off_t offset)
4397 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4398 ssize_t nwritten;
4400 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4401 fsp_str_dbg(fsp), (intmax_t)offset, n);
4403 if (fio == NULL) {
4404 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4407 if (fio->type == ADOUBLE_META) {
4408 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4409 } else {
4410 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4413 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4414 return nwritten;
4417 struct fruit_pwrite_state {
4418 ssize_t nwritten;
4419 struct vfs_aio_state vfs_aio_state;
4422 static void fruit_pwrite_done(struct tevent_req *subreq);
4424 static struct tevent_req *fruit_pwrite_send(
4425 struct vfs_handle_struct *handle,
4426 TALLOC_CTX *mem_ctx,
4427 struct tevent_context *ev,
4428 struct files_struct *fsp,
4429 const void *data,
4430 size_t n, off_t offset)
4432 struct tevent_req *req = NULL;
4433 struct tevent_req *subreq = NULL;
4434 struct fruit_pwrite_state *state = NULL;
4435 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4437 req = tevent_req_create(mem_ctx, &state,
4438 struct fruit_pwrite_state);
4439 if (req == NULL) {
4440 return NULL;
4443 if (fruit_must_handle_aio_stream(fio)) {
4444 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4445 if (state->nwritten != n) {
4446 if (state->nwritten != -1) {
4447 errno = EIO;
4449 tevent_req_error(req, errno);
4450 return tevent_req_post(req, ev);
4452 tevent_req_done(req);
4453 return tevent_req_post(req, ev);
4456 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4457 data, n, offset);
4458 if (tevent_req_nomem(req, subreq)) {
4459 return tevent_req_post(req, ev);
4461 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4462 return req;
4465 static void fruit_pwrite_done(struct tevent_req *subreq)
4467 struct tevent_req *req = tevent_req_callback_data(
4468 subreq, struct tevent_req);
4469 struct fruit_pwrite_state *state = tevent_req_data(
4470 req, struct fruit_pwrite_state);
4472 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4473 TALLOC_FREE(subreq);
4475 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4476 return;
4478 tevent_req_done(req);
4481 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4482 struct vfs_aio_state *vfs_aio_state)
4484 struct fruit_pwrite_state *state = tevent_req_data(
4485 req, struct fruit_pwrite_state);
4487 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4488 return -1;
4491 *vfs_aio_state = state->vfs_aio_state;
4492 return state->nwritten;
4496 * Helper to stat/lstat the base file of an smb_fname.
4498 static int fruit_stat_base(vfs_handle_struct *handle,
4499 struct smb_filename *smb_fname,
4500 bool follow_links)
4502 char *tmp_stream_name;
4503 int rc;
4505 tmp_stream_name = smb_fname->stream_name;
4506 smb_fname->stream_name = NULL;
4507 if (follow_links) {
4508 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4509 } else {
4510 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4512 smb_fname->stream_name = tmp_stream_name;
4513 return rc;
4516 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4517 struct smb_filename *smb_fname,
4518 bool follow_links)
4520 int ret;
4522 if (follow_links) {
4523 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4524 } else {
4525 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4528 return ret;
4531 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4532 struct smb_filename *smb_fname,
4533 bool follow_links)
4535 struct adouble *ad = NULL;
4537 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4538 if (ad == NULL) {
4539 DBG_INFO("fruit_stat_meta %s: %s\n",
4540 smb_fname_str_dbg(smb_fname), strerror(errno));
4541 errno = ENOENT;
4542 return -1;
4544 TALLOC_FREE(ad);
4546 /* Populate the stat struct with info from the base file. */
4547 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4548 return -1;
4550 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4551 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4552 smb_fname->stream_name);
4553 return 0;
4556 static int fruit_stat_meta(vfs_handle_struct *handle,
4557 struct smb_filename *smb_fname,
4558 bool follow_links)
4560 struct fruit_config_data *config = NULL;
4561 int ret;
4563 SMB_VFS_HANDLE_GET_DATA(handle, config,
4564 struct fruit_config_data, return -1);
4566 switch (config->meta) {
4567 case FRUIT_META_STREAM:
4568 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4569 break;
4571 case FRUIT_META_NETATALK:
4572 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4573 break;
4575 default:
4576 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4577 return -1;
4580 return ret;
4583 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4584 struct smb_filename *smb_fname,
4585 bool follow_links)
4587 struct adouble *ad = NULL;
4588 int ret;
4590 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4591 if (ad == NULL) {
4592 errno = ENOENT;
4593 return -1;
4596 /* Populate the stat struct with info from the base file. */
4597 ret = fruit_stat_base(handle, smb_fname, follow_links);
4598 if (ret != 0) {
4599 TALLOC_FREE(ad);
4600 return -1;
4603 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4604 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4605 smb_fname->stream_name);
4606 TALLOC_FREE(ad);
4607 return 0;
4610 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4611 struct smb_filename *smb_fname,
4612 bool follow_links)
4614 int ret;
4616 if (follow_links) {
4617 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4618 } else {
4619 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4622 return ret;
4625 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4626 struct smb_filename *smb_fname,
4627 bool follow_links)
4629 #ifdef HAVE_ATTROPEN
4630 int ret;
4631 int fd = -1;
4633 /* Populate the stat struct with info from the base file. */
4634 ret = fruit_stat_base(handle, smb_fname, follow_links);
4635 if (ret != 0) {
4636 return -1;
4639 fd = attropen(smb_fname->base_name,
4640 AFPRESOURCE_EA_NETATALK,
4641 O_RDONLY);
4642 if (fd == -1) {
4643 return 0;
4646 ret = sys_fstat(fd, &smb_fname->st, false);
4647 if (ret != 0) {
4648 close(fd);
4649 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4650 AFPRESOURCE_EA_NETATALK);
4651 return -1;
4653 close(fd);
4654 fd = -1;
4656 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4657 smb_fname->stream_name);
4659 return ret;
4661 #else
4662 errno = ENOSYS;
4663 return -1;
4664 #endif
4667 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4668 struct smb_filename *smb_fname,
4669 bool follow_links)
4671 struct fruit_config_data *config = NULL;
4672 int ret;
4674 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4676 SMB_VFS_HANDLE_GET_DATA(handle, config,
4677 struct fruit_config_data, return -1);
4679 switch (config->rsrc) {
4680 case FRUIT_RSRC_STREAM:
4681 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4682 break;
4684 case FRUIT_RSRC_XATTR:
4685 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4686 break;
4688 case FRUIT_RSRC_ADFILE:
4689 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4690 break;
4692 default:
4693 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4694 return -1;
4697 return ret;
4700 static int fruit_stat(vfs_handle_struct *handle,
4701 struct smb_filename *smb_fname)
4703 int rc = -1;
4705 DEBUG(10, ("fruit_stat called for %s\n",
4706 smb_fname_str_dbg(smb_fname)));
4708 if (!is_ntfs_stream_smb_fname(smb_fname)
4709 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4710 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4711 if (rc == 0) {
4712 update_btime(handle, smb_fname);
4714 return rc;
4718 * Note if lp_posix_paths() is true, we can never
4719 * get here as is_ntfs_stream_smb_fname() is
4720 * always false. So we never need worry about
4721 * not following links here.
4724 if (is_afpinfo_stream(smb_fname)) {
4725 rc = fruit_stat_meta(handle, smb_fname, true);
4726 } else if (is_afpresource_stream(smb_fname)) {
4727 rc = fruit_stat_rsrc(handle, smb_fname, true);
4728 } else {
4729 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4732 if (rc == 0) {
4733 update_btime(handle, smb_fname);
4734 smb_fname->st.st_ex_mode &= ~S_IFMT;
4735 smb_fname->st.st_ex_mode |= S_IFREG;
4736 smb_fname->st.st_ex_blocks =
4737 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4739 return rc;
4742 static int fruit_lstat(vfs_handle_struct *handle,
4743 struct smb_filename *smb_fname)
4745 int rc = -1;
4747 DEBUG(10, ("fruit_lstat called for %s\n",
4748 smb_fname_str_dbg(smb_fname)));
4750 if (!is_ntfs_stream_smb_fname(smb_fname)
4751 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4752 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4753 if (rc == 0) {
4754 update_btime(handle, smb_fname);
4756 return rc;
4759 if (is_afpinfo_stream(smb_fname)) {
4760 rc = fruit_stat_meta(handle, smb_fname, false);
4761 } else if (is_afpresource_stream(smb_fname)) {
4762 rc = fruit_stat_rsrc(handle, smb_fname, false);
4763 } else {
4764 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4767 if (rc == 0) {
4768 update_btime(handle, smb_fname);
4769 smb_fname->st.st_ex_mode &= ~S_IFMT;
4770 smb_fname->st.st_ex_mode |= S_IFREG;
4771 smb_fname->st.st_ex_blocks =
4772 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4774 return rc;
4777 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4778 files_struct *fsp,
4779 SMB_STRUCT_STAT *sbuf)
4781 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4784 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4785 files_struct *fsp,
4786 SMB_STRUCT_STAT *sbuf)
4788 int ret;
4790 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4791 if (ret != 0) {
4792 return -1;
4795 *sbuf = fsp->base_fsp->fsp_name->st;
4796 sbuf->st_ex_size = AFP_INFO_SIZE;
4797 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4799 return 0;
4802 static int fruit_fstat_meta(vfs_handle_struct *handle,
4803 files_struct *fsp,
4804 SMB_STRUCT_STAT *sbuf,
4805 struct fio *fio)
4807 int ret;
4809 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4811 switch (fio->config->meta) {
4812 case FRUIT_META_STREAM:
4813 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4814 break;
4816 case FRUIT_META_NETATALK:
4817 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4818 break;
4820 default:
4821 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4822 return -1;
4825 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4826 return ret;
4829 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4830 files_struct *fsp,
4831 SMB_STRUCT_STAT *sbuf)
4833 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4836 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4837 files_struct *fsp,
4838 SMB_STRUCT_STAT *sbuf)
4840 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4843 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4844 files_struct *fsp,
4845 SMB_STRUCT_STAT *sbuf)
4847 struct adouble *ad = NULL;
4848 int ret;
4850 /* Populate the stat struct with info from the base file. */
4851 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4852 if (ret == -1) {
4853 return -1;
4856 ad = ad_get(talloc_tos(), handle,
4857 fsp->base_fsp->fsp_name,
4858 ADOUBLE_RSRC);
4859 if (ad == NULL) {
4860 DBG_ERR("ad_get [%s] failed [%s]\n",
4861 fsp_str_dbg(fsp), strerror(errno));
4862 return -1;
4865 *sbuf = fsp->base_fsp->fsp_name->st;
4866 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4867 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4869 TALLOC_FREE(ad);
4870 return 0;
4873 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4874 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4876 int ret;
4878 switch (fio->config->rsrc) {
4879 case FRUIT_RSRC_STREAM:
4880 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4881 break;
4883 case FRUIT_RSRC_ADFILE:
4884 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4885 break;
4887 case FRUIT_RSRC_XATTR:
4888 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4889 break;
4891 default:
4892 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4893 return -1;
4896 return ret;
4899 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4900 SMB_STRUCT_STAT *sbuf)
4902 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4903 int rc;
4905 if (fio == NULL) {
4906 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4909 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4911 if (fio->type == ADOUBLE_META) {
4912 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4913 } else {
4914 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4917 if (rc == 0) {
4918 sbuf->st_ex_mode &= ~S_IFMT;
4919 sbuf->st_ex_mode |= S_IFREG;
4920 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4923 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4924 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4925 return rc;
4928 static NTSTATUS delete_invalid_meta_stream(
4929 vfs_handle_struct *handle,
4930 const struct smb_filename *smb_fname,
4931 TALLOC_CTX *mem_ctx,
4932 unsigned int *pnum_streams,
4933 struct stream_struct **pstreams)
4935 struct smb_filename *sname = NULL;
4936 int ret;
4937 bool ok;
4939 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4940 if (!ok) {
4941 return NT_STATUS_INTERNAL_ERROR;
4944 sname = synthetic_smb_fname(talloc_tos(),
4945 smb_fname->base_name,
4946 AFPINFO_STREAM_NAME,
4947 NULL, 0);
4948 if (sname == NULL) {
4949 return NT_STATUS_NO_MEMORY;
4952 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4953 TALLOC_FREE(sname);
4954 if (ret != 0) {
4955 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4956 return map_nt_error_from_unix(errno);
4959 return NT_STATUS_OK;
4962 static NTSTATUS fruit_streaminfo_meta_stream(
4963 vfs_handle_struct *handle,
4964 struct files_struct *fsp,
4965 const struct smb_filename *smb_fname,
4966 TALLOC_CTX *mem_ctx,
4967 unsigned int *pnum_streams,
4968 struct stream_struct **pstreams)
4970 struct stream_struct *stream = *pstreams;
4971 unsigned int num_streams = *pnum_streams;
4972 struct smb_filename *sname = NULL;
4973 char *full_name = NULL;
4974 uint32_t name_hash;
4975 struct share_mode_lock *lck = NULL;
4976 struct file_id id = {0};
4977 bool delete_on_close_set;
4978 int i;
4979 int ret;
4980 NTSTATUS status;
4981 bool ok;
4983 for (i = 0; i < num_streams; i++) {
4984 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4985 break;
4989 if (i == num_streams) {
4990 return NT_STATUS_OK;
4993 if (stream[i].size != AFP_INFO_SIZE) {
4994 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
4995 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
4997 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
4998 pnum_streams, pstreams);
5002 * Now check if there's a delete-on-close pending on the stream. If so,
5003 * hide the stream. This behaviour was verified against a macOS 10.12
5004 * SMB server.
5007 sname = synthetic_smb_fname(talloc_tos(),
5008 smb_fname->base_name,
5009 AFPINFO_STREAM_NAME,
5010 NULL, 0);
5011 if (sname == NULL) {
5012 status = NT_STATUS_NO_MEMORY;
5013 goto out;
5016 ret = SMB_VFS_NEXT_STAT(handle, sname);
5017 if (ret != 0) {
5018 status = map_nt_error_from_unix(errno);
5019 goto out;
5022 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5024 lck = get_existing_share_mode_lock(talloc_tos(), id);
5025 if (lck == NULL) {
5026 status = NT_STATUS_OK;
5027 goto out;
5030 full_name = talloc_asprintf(talloc_tos(),
5031 "%s%s",
5032 sname->base_name,
5033 AFPINFO_STREAM);
5034 if (full_name == NULL) {
5035 status = NT_STATUS_NO_MEMORY;
5036 goto out;
5039 status = file_name_hash(handle->conn, full_name, &name_hash);
5040 if (!NT_STATUS_IS_OK(status)) {
5041 goto out;
5044 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5045 if (delete_on_close_set) {
5046 ok = del_fruit_stream(mem_ctx,
5047 pnum_streams,
5048 pstreams,
5049 AFPINFO_STREAM);
5050 if (!ok) {
5051 status = NT_STATUS_INTERNAL_ERROR;
5052 goto out;
5056 status = NT_STATUS_OK;
5058 out:
5059 TALLOC_FREE(sname);
5060 TALLOC_FREE(lck);
5061 TALLOC_FREE(full_name);
5062 return status;
5065 static NTSTATUS fruit_streaminfo_meta_netatalk(
5066 vfs_handle_struct *handle,
5067 struct files_struct *fsp,
5068 const struct smb_filename *smb_fname,
5069 TALLOC_CTX *mem_ctx,
5070 unsigned int *pnum_streams,
5071 struct stream_struct **pstreams)
5073 struct stream_struct *stream = *pstreams;
5074 unsigned int num_streams = *pnum_streams;
5075 struct adouble *ad = NULL;
5076 bool is_fi_empty;
5077 int i;
5078 bool ok;
5080 /* Remove the Netatalk xattr from the list */
5081 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5082 ":" NETATALK_META_XATTR ":$DATA");
5083 if (!ok) {
5084 return NT_STATUS_NO_MEMORY;
5088 * Check if there's a AFPINFO_STREAM from the VFS streams
5089 * backend and if yes, remove it from the list
5091 for (i = 0; i < num_streams; i++) {
5092 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5093 break;
5097 if (i < num_streams) {
5098 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5099 smb_fname_str_dbg(smb_fname));
5101 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5102 AFPINFO_STREAM);
5103 if (!ok) {
5104 return NT_STATUS_INTERNAL_ERROR;
5108 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5109 if (ad == NULL) {
5110 return NT_STATUS_OK;
5113 is_fi_empty = ad_empty_finderinfo(ad);
5114 TALLOC_FREE(ad);
5116 if (is_fi_empty) {
5117 return NT_STATUS_OK;
5120 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5121 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5122 smb_roundup(handle->conn, AFP_INFO_SIZE));
5123 if (!ok) {
5124 return NT_STATUS_NO_MEMORY;
5127 return NT_STATUS_OK;
5130 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5131 struct files_struct *fsp,
5132 const struct smb_filename *smb_fname,
5133 TALLOC_CTX *mem_ctx,
5134 unsigned int *pnum_streams,
5135 struct stream_struct **pstreams)
5137 struct fruit_config_data *config = NULL;
5138 NTSTATUS status;
5140 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5141 return NT_STATUS_INTERNAL_ERROR);
5143 switch (config->meta) {
5144 case FRUIT_META_NETATALK:
5145 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5146 mem_ctx, pnum_streams,
5147 pstreams);
5148 break;
5150 case FRUIT_META_STREAM:
5151 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5152 mem_ctx, pnum_streams,
5153 pstreams);
5154 break;
5156 default:
5157 return NT_STATUS_INTERNAL_ERROR;
5160 return status;
5163 static NTSTATUS fruit_streaminfo_rsrc_stream(
5164 vfs_handle_struct *handle,
5165 struct files_struct *fsp,
5166 const struct smb_filename *smb_fname,
5167 TALLOC_CTX *mem_ctx,
5168 unsigned int *pnum_streams,
5169 struct stream_struct **pstreams)
5171 bool ok;
5173 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5174 if (!ok) {
5175 DBG_ERR("Filtering resource stream failed\n");
5176 return NT_STATUS_INTERNAL_ERROR;
5178 return NT_STATUS_OK;
5181 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5182 vfs_handle_struct *handle,
5183 struct files_struct *fsp,
5184 const struct smb_filename *smb_fname,
5185 TALLOC_CTX *mem_ctx,
5186 unsigned int *pnum_streams,
5187 struct stream_struct **pstreams)
5189 bool ok;
5191 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5192 if (!ok) {
5193 DBG_ERR("Filtering resource stream failed\n");
5194 return NT_STATUS_INTERNAL_ERROR;
5196 return NT_STATUS_OK;
5199 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5200 vfs_handle_struct *handle,
5201 struct files_struct *fsp,
5202 const struct smb_filename *smb_fname,
5203 TALLOC_CTX *mem_ctx,
5204 unsigned int *pnum_streams,
5205 struct stream_struct **pstreams)
5207 struct stream_struct *stream = *pstreams;
5208 unsigned int num_streams = *pnum_streams;
5209 struct adouble *ad = NULL;
5210 bool ok;
5211 size_t rlen;
5212 int i;
5215 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5216 * and if yes, remove it from the list
5218 for (i = 0; i < num_streams; i++) {
5219 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5220 break;
5224 if (i < num_streams) {
5225 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5226 smb_fname_str_dbg(smb_fname));
5228 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5229 AFPRESOURCE_STREAM);
5230 if (!ok) {
5231 return NT_STATUS_INTERNAL_ERROR;
5235 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5236 if (ad == NULL) {
5237 return NT_STATUS_OK;
5240 rlen = ad_getentrylen(ad, ADEID_RFORK);
5241 TALLOC_FREE(ad);
5243 if (rlen == 0) {
5244 return NT_STATUS_OK;
5247 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5248 AFPRESOURCE_STREAM_NAME, rlen,
5249 smb_roundup(handle->conn, rlen));
5250 if (!ok) {
5251 return NT_STATUS_NO_MEMORY;
5254 return NT_STATUS_OK;
5257 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5258 struct files_struct *fsp,
5259 const struct smb_filename *smb_fname,
5260 TALLOC_CTX *mem_ctx,
5261 unsigned int *pnum_streams,
5262 struct stream_struct **pstreams)
5264 struct fruit_config_data *config = NULL;
5265 NTSTATUS status;
5267 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5268 return NT_STATUS_INTERNAL_ERROR);
5270 switch (config->rsrc) {
5271 case FRUIT_RSRC_STREAM:
5272 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5273 mem_ctx, pnum_streams,
5274 pstreams);
5275 break;
5277 case FRUIT_RSRC_XATTR:
5278 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5279 mem_ctx, pnum_streams,
5280 pstreams);
5281 break;
5283 case FRUIT_RSRC_ADFILE:
5284 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5285 mem_ctx, pnum_streams,
5286 pstreams);
5287 break;
5289 default:
5290 return NT_STATUS_INTERNAL_ERROR;
5293 return status;
5296 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5297 struct files_struct *fsp,
5298 const struct smb_filename *smb_fname,
5299 TALLOC_CTX *mem_ctx,
5300 unsigned int *pnum_streams,
5301 struct stream_struct **pstreams)
5303 struct fruit_config_data *config = NULL;
5304 NTSTATUS status;
5306 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5307 return NT_STATUS_UNSUCCESSFUL);
5309 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5311 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5312 pnum_streams, pstreams);
5313 if (!NT_STATUS_IS_OK(status)) {
5314 return status;
5317 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5318 mem_ctx, pnum_streams, pstreams);
5319 if (!NT_STATUS_IS_OK(status)) {
5320 return status;
5323 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5324 mem_ctx, pnum_streams, pstreams);
5325 if (!NT_STATUS_IS_OK(status)) {
5326 return status;
5329 return NT_STATUS_OK;
5332 static int fruit_ntimes(vfs_handle_struct *handle,
5333 const struct smb_filename *smb_fname,
5334 struct smb_file_time *ft)
5336 int rc = 0;
5337 struct adouble *ad = NULL;
5338 struct fruit_config_data *config = NULL;
5340 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5341 return -1);
5343 if ((config->meta != FRUIT_META_NETATALK) ||
5344 null_timespec(ft->create_time))
5346 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5349 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5350 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5352 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5353 if (ad == NULL) {
5354 goto exit;
5357 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5358 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5360 rc = ad_set(ad, smb_fname);
5362 exit:
5364 TALLOC_FREE(ad);
5365 if (rc != 0) {
5366 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5367 return -1;
5369 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5372 static int fruit_fallocate(struct vfs_handle_struct *handle,
5373 struct files_struct *fsp,
5374 uint32_t mode,
5375 off_t offset,
5376 off_t len)
5378 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5380 if (fio == NULL) {
5381 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5384 /* Let the pwrite code path handle it. */
5385 errno = ENOSYS;
5386 return -1;
5389 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5390 struct files_struct *fsp,
5391 off_t offset)
5393 if (offset == 0) {
5394 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5397 #ifdef HAVE_ATTROPEN
5398 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5399 #endif
5400 return 0;
5403 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5404 struct files_struct *fsp,
5405 off_t offset)
5407 int rc;
5408 struct adouble *ad = NULL;
5409 off_t ad_off;
5411 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5412 if (ad == NULL) {
5413 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5414 fsp_str_dbg(fsp), strerror(errno));
5415 return -1;
5418 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5420 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5421 if (rc != 0) {
5422 TALLOC_FREE(ad);
5423 return -1;
5426 ad_setentrylen(ad, ADEID_RFORK, offset);
5428 rc = ad_fset(ad, fsp);
5429 if (rc != 0) {
5430 DBG_ERR("ad_fset [%s] failed [%s]\n",
5431 fsp_str_dbg(fsp), strerror(errno));
5432 TALLOC_FREE(ad);
5433 return -1;
5436 TALLOC_FREE(ad);
5437 return 0;
5440 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5441 struct files_struct *fsp,
5442 off_t offset)
5444 if (offset == 0) {
5445 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5448 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5451 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5452 struct files_struct *fsp,
5453 off_t offset)
5455 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5456 int ret;
5458 switch (fio->config->rsrc) {
5459 case FRUIT_RSRC_XATTR:
5460 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5461 break;
5463 case FRUIT_RSRC_ADFILE:
5464 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5465 break;
5467 case FRUIT_RSRC_STREAM:
5468 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5469 break;
5471 default:
5472 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5473 return -1;
5477 return ret;
5480 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5481 struct files_struct *fsp,
5482 off_t offset)
5484 if (offset > 60) {
5485 DBG_WARNING("ftruncate %s to %jd",
5486 fsp_str_dbg(fsp), (intmax_t)offset);
5487 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5488 errno = EOVERFLOW;
5489 return -1;
5492 /* OS X returns success but does nothing */
5493 DBG_INFO("ignoring ftruncate %s to %jd\n",
5494 fsp_str_dbg(fsp), (intmax_t)offset);
5495 return 0;
5498 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5499 struct files_struct *fsp,
5500 off_t offset)
5502 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5503 int ret;
5505 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5506 (intmax_t)offset);
5508 if (fio == NULL) {
5509 if (offset == 0 && global_fruit_config.nego_aapl) {
5510 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5512 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5515 if (fio->type == ADOUBLE_META) {
5516 ret = fruit_ftruncate_meta(handle, fsp, offset);
5517 } else {
5518 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5521 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5522 return ret;
5525 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5526 struct smb_request *req,
5527 uint16_t root_dir_fid,
5528 struct smb_filename *smb_fname,
5529 uint32_t access_mask,
5530 uint32_t share_access,
5531 uint32_t create_disposition,
5532 uint32_t create_options,
5533 uint32_t file_attributes,
5534 uint32_t oplock_request,
5535 struct smb2_lease *lease,
5536 uint64_t allocation_size,
5537 uint32_t private_flags,
5538 struct security_descriptor *sd,
5539 struct ea_list *ea_list,
5540 files_struct **result,
5541 int *pinfo,
5542 const struct smb2_create_blobs *in_context_blobs,
5543 struct smb2_create_blobs *out_context_blobs)
5545 NTSTATUS status;
5546 struct fruit_config_data *config = NULL;
5547 files_struct *fsp = NULL;
5549 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5550 if (!NT_STATUS_IS_OK(status)) {
5551 goto fail;
5554 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5555 return NT_STATUS_UNSUCCESSFUL);
5557 status = SMB_VFS_NEXT_CREATE_FILE(
5558 handle, req, root_dir_fid, smb_fname,
5559 access_mask, share_access,
5560 create_disposition, create_options,
5561 file_attributes, oplock_request,
5562 lease,
5563 allocation_size, private_flags,
5564 sd, ea_list, result,
5565 pinfo, in_context_blobs, out_context_blobs);
5566 if (!NT_STATUS_IS_OK(status)) {
5567 return status;
5570 fsp = *result;
5572 if (global_fruit_config.nego_aapl) {
5573 if (config->posix_rename && fsp->is_directory) {
5575 * Enable POSIX directory rename behaviour
5577 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5582 * If this is a plain open for existing files, opening an 0
5583 * byte size resource fork MUST fail with
5584 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5586 * Cf the vfs_fruit torture tests in test_rfork_create().
5588 if (is_afpresource_stream(fsp->fsp_name) &&
5589 create_disposition == FILE_OPEN)
5591 if (fsp->fsp_name->st.st_ex_size == 0) {
5592 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5593 goto fail;
5597 if (is_ntfs_stream_smb_fname(smb_fname)
5598 || fsp->is_directory) {
5599 return status;
5602 if (config->locking == FRUIT_LOCKING_NETATALK) {
5603 status = fruit_check_access(
5604 handle, *result,
5605 access_mask,
5606 map_share_mode_to_deny_mode(share_access, 0));
5607 if (!NT_STATUS_IS_OK(status)) {
5608 goto fail;
5612 return status;
5614 fail:
5615 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5617 if (fsp) {
5618 close_file(req, fsp, ERROR_CLOSE);
5619 *result = fsp = NULL;
5622 return status;
5625 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5626 const struct smb_filename *fname,
5627 TALLOC_CTX *mem_ctx,
5628 struct readdir_attr_data **pattr_data)
5630 struct fruit_config_data *config = NULL;
5631 struct readdir_attr_data *attr_data;
5632 NTSTATUS status;
5634 SMB_VFS_HANDLE_GET_DATA(handle, config,
5635 struct fruit_config_data,
5636 return NT_STATUS_UNSUCCESSFUL);
5638 if (!global_fruit_config.nego_aapl) {
5639 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5642 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5644 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5645 if (*pattr_data == NULL) {
5646 return NT_STATUS_UNSUCCESSFUL;
5648 attr_data = *pattr_data;
5649 attr_data->type = RDATTR_AAPL;
5652 * Mac metadata: compressed FinderInfo, resource fork length
5653 * and creation date
5655 status = readdir_attr_macmeta(handle, fname, attr_data);
5656 if (!NT_STATUS_IS_OK(status)) {
5658 * Error handling is tricky: if we return failure from
5659 * this function, the corresponding directory entry
5660 * will to be passed to the client, so we really just
5661 * want to error out on fatal errors.
5663 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5664 goto fail;
5669 * UNIX mode
5671 if (config->unix_info_enabled) {
5672 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5676 * max_access
5678 if (!config->readdir_attr_max_access) {
5679 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5680 } else {
5681 status = smbd_calculate_access_mask(
5682 handle->conn,
5683 fname,
5684 false,
5685 SEC_FLAG_MAXIMUM_ALLOWED,
5686 &attr_data->attr_data.aapl.max_access);
5687 if (!NT_STATUS_IS_OK(status)) {
5688 goto fail;
5692 return NT_STATUS_OK;
5694 fail:
5695 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5696 fname->base_name, nt_errstr(status)));
5697 TALLOC_FREE(*pattr_data);
5698 return status;
5701 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5702 files_struct *fsp,
5703 uint32_t security_info,
5704 TALLOC_CTX *mem_ctx,
5705 struct security_descriptor **ppdesc)
5707 NTSTATUS status;
5708 struct security_ace ace;
5709 struct dom_sid sid;
5710 struct fruit_config_data *config;
5712 SMB_VFS_HANDLE_GET_DATA(handle, config,
5713 struct fruit_config_data,
5714 return NT_STATUS_UNSUCCESSFUL);
5716 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5717 mem_ctx, ppdesc);
5718 if (!NT_STATUS_IS_OK(status)) {
5719 return status;
5723 * Add MS NFS style ACEs with uid, gid and mode
5725 if (!global_fruit_config.nego_aapl) {
5726 return NT_STATUS_OK;
5728 if (!config->unix_info_enabled) {
5729 return NT_STATUS_OK;
5732 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5733 status = remove_virtual_nfs_aces(*ppdesc);
5734 if (!NT_STATUS_IS_OK(status)) {
5735 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5736 return status;
5739 /* MS NFS style mode */
5740 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5741 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5742 status = security_descriptor_dacl_add(*ppdesc, &ace);
5743 if (!NT_STATUS_IS_OK(status)) {
5744 DEBUG(1,("failed to add MS NFS style ACE\n"));
5745 return status;
5748 /* MS NFS style uid */
5749 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5750 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5751 status = security_descriptor_dacl_add(*ppdesc, &ace);
5752 if (!NT_STATUS_IS_OK(status)) {
5753 DEBUG(1,("failed to add MS NFS style ACE\n"));
5754 return status;
5757 /* MS NFS style gid */
5758 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5759 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5760 status = security_descriptor_dacl_add(*ppdesc, &ace);
5761 if (!NT_STATUS_IS_OK(status)) {
5762 DEBUG(1,("failed to add MS NFS style ACE\n"));
5763 return status;
5766 return NT_STATUS_OK;
5769 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5770 files_struct *fsp,
5771 uint32_t security_info_sent,
5772 const struct security_descriptor *orig_psd)
5774 NTSTATUS status;
5775 bool do_chmod;
5776 mode_t ms_nfs_mode = 0;
5777 int result;
5778 struct security_descriptor *psd = NULL;
5779 uint32_t orig_num_aces = 0;
5781 if (orig_psd->dacl != NULL) {
5782 orig_num_aces = orig_psd->dacl->num_aces;
5785 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5786 if (psd == NULL) {
5787 return NT_STATUS_NO_MEMORY;
5790 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5792 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5793 if (!NT_STATUS_IS_OK(status)) {
5794 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5795 TALLOC_FREE(psd);
5796 return status;
5800 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5801 * sent/present flags correctly now we've removed them.
5804 if (orig_num_aces != 0) {
5806 * Are there any ACE's left ?
5808 if (psd->dacl->num_aces == 0) {
5809 /* No - clear the DACL sent/present flags. */
5810 security_info_sent &= ~SECINFO_DACL;
5811 psd->type &= ~SEC_DESC_DACL_PRESENT;
5815 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5816 if (!NT_STATUS_IS_OK(status)) {
5817 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5818 TALLOC_FREE(psd);
5819 return status;
5822 if (do_chmod) {
5823 if (fsp->fh->fd != -1) {
5824 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5825 } else {
5826 result = SMB_VFS_CHMOD(fsp->conn,
5827 fsp->fsp_name,
5828 ms_nfs_mode);
5831 if (result != 0) {
5832 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5833 result, (unsigned)ms_nfs_mode,
5834 strerror(errno)));
5835 status = map_nt_error_from_unix(errno);
5836 TALLOC_FREE(psd);
5837 return status;
5841 TALLOC_FREE(psd);
5842 return NT_STATUS_OK;
5845 static struct vfs_offload_ctx *fruit_offload_ctx;
5847 struct fruit_offload_read_state {
5848 struct vfs_handle_struct *handle;
5849 struct tevent_context *ev;
5850 files_struct *fsp;
5851 uint32_t fsctl;
5852 DATA_BLOB token;
5855 static void fruit_offload_read_done(struct tevent_req *subreq);
5857 static struct tevent_req *fruit_offload_read_send(
5858 TALLOC_CTX *mem_ctx,
5859 struct tevent_context *ev,
5860 struct vfs_handle_struct *handle,
5861 files_struct *fsp,
5862 uint32_t fsctl,
5863 uint32_t ttl,
5864 off_t offset,
5865 size_t to_copy)
5867 struct tevent_req *req = NULL;
5868 struct tevent_req *subreq = NULL;
5869 struct fruit_offload_read_state *state = NULL;
5871 req = tevent_req_create(mem_ctx, &state,
5872 struct fruit_offload_read_state);
5873 if (req == NULL) {
5874 return NULL;
5876 *state = (struct fruit_offload_read_state) {
5877 .handle = handle,
5878 .ev = ev,
5879 .fsp = fsp,
5880 .fsctl = fsctl,
5883 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5884 fsctl, ttl, offset, to_copy);
5885 if (tevent_req_nomem(subreq, req)) {
5886 return tevent_req_post(req, ev);
5888 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5889 return req;
5892 static void fruit_offload_read_done(struct tevent_req *subreq)
5894 struct tevent_req *req = tevent_req_callback_data(
5895 subreq, struct tevent_req);
5896 struct fruit_offload_read_state *state = tevent_req_data(
5897 req, struct fruit_offload_read_state);
5898 NTSTATUS status;
5900 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5901 state->handle,
5902 state,
5903 &state->token);
5904 TALLOC_FREE(subreq);
5905 if (tevent_req_nterror(req, status)) {
5906 return;
5909 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5910 tevent_req_done(req);
5911 return;
5914 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5915 &fruit_offload_ctx);
5916 if (tevent_req_nterror(req, status)) {
5917 return;
5920 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5921 state->fsp,
5922 &state->token);
5923 if (tevent_req_nterror(req, status)) {
5924 return;
5927 tevent_req_done(req);
5928 return;
5931 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5932 struct vfs_handle_struct *handle,
5933 TALLOC_CTX *mem_ctx,
5934 DATA_BLOB *token)
5936 struct fruit_offload_read_state *state = tevent_req_data(
5937 req, struct fruit_offload_read_state);
5938 NTSTATUS status;
5940 if (tevent_req_is_nterror(req, &status)) {
5941 tevent_req_received(req);
5942 return status;
5945 token->length = state->token.length;
5946 token->data = talloc_move(mem_ctx, &state->token.data);
5948 tevent_req_received(req);
5949 return NT_STATUS_OK;
5952 struct fruit_offload_write_state {
5953 struct vfs_handle_struct *handle;
5954 off_t copied;
5955 struct files_struct *src_fsp;
5956 struct files_struct *dst_fsp;
5957 bool is_copyfile;
5960 static void fruit_offload_write_done(struct tevent_req *subreq);
5961 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
5962 TALLOC_CTX *mem_ctx,
5963 struct tevent_context *ev,
5964 uint32_t fsctl,
5965 DATA_BLOB *token,
5966 off_t transfer_offset,
5967 struct files_struct *dest_fsp,
5968 off_t dest_off,
5969 off_t num)
5971 struct tevent_req *req, *subreq;
5972 struct fruit_offload_write_state *state;
5973 NTSTATUS status;
5974 struct fruit_config_data *config;
5975 off_t src_off = transfer_offset;
5976 files_struct *src_fsp = NULL;
5977 off_t to_copy = num;
5978 bool copyfile_enabled = false;
5980 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5981 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5983 SMB_VFS_HANDLE_GET_DATA(handle, config,
5984 struct fruit_config_data,
5985 return NULL);
5987 req = tevent_req_create(mem_ctx, &state,
5988 struct fruit_offload_write_state);
5989 if (req == NULL) {
5990 return NULL;
5992 state->handle = handle;
5993 state->dst_fsp = dest_fsp;
5995 switch (fsctl) {
5996 case FSCTL_SRV_COPYCHUNK:
5997 case FSCTL_SRV_COPYCHUNK_WRITE:
5998 copyfile_enabled = config->copyfile_enabled;
5999 break;
6000 default:
6001 break;
6005 * Check if this a OS X copyfile style copychunk request with
6006 * a requested chunk count of 0 that was translated to a
6007 * offload_write_send VFS call overloading the parameters src_off
6008 * = dest_off = num = 0.
6010 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6011 status = vfs_offload_token_db_fetch_fsp(
6012 fruit_offload_ctx, token, &src_fsp);
6013 if (tevent_req_nterror(req, status)) {
6014 return tevent_req_post(req, ev);
6016 state->src_fsp = src_fsp;
6018 status = vfs_stat_fsp(src_fsp);
6019 if (tevent_req_nterror(req, status)) {
6020 return tevent_req_post(req, ev);
6023 to_copy = src_fsp->fsp_name->st.st_ex_size;
6024 state->is_copyfile = true;
6027 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6028 mem_ctx,
6030 fsctl,
6031 token,
6032 transfer_offset,
6033 dest_fsp,
6034 dest_off,
6035 to_copy);
6036 if (tevent_req_nomem(subreq, req)) {
6037 return tevent_req_post(req, ev);
6040 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6041 return req;
6044 static void fruit_offload_write_done(struct tevent_req *subreq)
6046 struct tevent_req *req = tevent_req_callback_data(
6047 subreq, struct tevent_req);
6048 struct fruit_offload_write_state *state = tevent_req_data(
6049 req, struct fruit_offload_write_state);
6050 NTSTATUS status;
6051 unsigned int num_streams = 0;
6052 struct stream_struct *streams = NULL;
6053 unsigned int i;
6054 struct smb_filename *src_fname_tmp = NULL;
6055 struct smb_filename *dst_fname_tmp = NULL;
6057 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6058 subreq,
6059 &state->copied);
6060 TALLOC_FREE(subreq);
6061 if (tevent_req_nterror(req, status)) {
6062 return;
6065 if (!state->is_copyfile) {
6066 tevent_req_done(req);
6067 return;
6071 * Now copy all remaining streams. We know the share supports
6072 * streams, because we're in vfs_fruit. We don't do this async
6073 * because streams are few and small.
6075 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6076 state->src_fsp->fsp_name,
6077 req, &num_streams, &streams);
6078 if (tevent_req_nterror(req, status)) {
6079 return;
6082 if (num_streams == 1) {
6083 /* There is always one stream, ::$DATA. */
6084 tevent_req_done(req);
6085 return;
6088 for (i = 0; i < num_streams; i++) {
6089 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6090 __func__, streams[i].name, (size_t)streams[i].size));
6092 src_fname_tmp = synthetic_smb_fname(
6093 req,
6094 state->src_fsp->fsp_name->base_name,
6095 streams[i].name,
6096 NULL,
6097 state->src_fsp->fsp_name->flags);
6098 if (tevent_req_nomem(src_fname_tmp, req)) {
6099 return;
6102 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6103 TALLOC_FREE(src_fname_tmp);
6104 continue;
6107 dst_fname_tmp = synthetic_smb_fname(
6108 req,
6109 state->dst_fsp->fsp_name->base_name,
6110 streams[i].name,
6111 NULL,
6112 state->dst_fsp->fsp_name->flags);
6113 if (tevent_req_nomem(dst_fname_tmp, req)) {
6114 TALLOC_FREE(src_fname_tmp);
6115 return;
6118 status = copy_file(req,
6119 state->handle->conn,
6120 src_fname_tmp,
6121 dst_fname_tmp,
6122 OPENX_FILE_CREATE_IF_NOT_EXIST,
6123 0, false);
6124 if (!NT_STATUS_IS_OK(status)) {
6125 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6126 smb_fname_str_dbg(src_fname_tmp),
6127 smb_fname_str_dbg(dst_fname_tmp),
6128 nt_errstr(status)));
6129 TALLOC_FREE(src_fname_tmp);
6130 TALLOC_FREE(dst_fname_tmp);
6131 tevent_req_nterror(req, status);
6132 return;
6135 TALLOC_FREE(src_fname_tmp);
6136 TALLOC_FREE(dst_fname_tmp);
6139 TALLOC_FREE(streams);
6140 TALLOC_FREE(src_fname_tmp);
6141 TALLOC_FREE(dst_fname_tmp);
6142 tevent_req_done(req);
6145 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6146 struct tevent_req *req,
6147 off_t *copied)
6149 struct fruit_offload_write_state *state = tevent_req_data(
6150 req, struct fruit_offload_write_state);
6151 NTSTATUS status;
6153 if (tevent_req_is_nterror(req, &status)) {
6154 DEBUG(1, ("server side copy chunk failed: %s\n",
6155 nt_errstr(status)));
6156 *copied = 0;
6157 tevent_req_received(req);
6158 return status;
6161 *copied = state->copied;
6162 tevent_req_received(req);
6164 return NT_STATUS_OK;
6167 static struct vfs_fn_pointers vfs_fruit_fns = {
6168 .connect_fn = fruit_connect,
6170 /* File operations */
6171 .chmod_fn = fruit_chmod,
6172 .chown_fn = fruit_chown,
6173 .unlink_fn = fruit_unlink,
6174 .rename_fn = fruit_rename,
6175 .rmdir_fn = fruit_rmdir,
6176 .open_fn = fruit_open,
6177 .pread_fn = fruit_pread,
6178 .pwrite_fn = fruit_pwrite,
6179 .pread_send_fn = fruit_pread_send,
6180 .pread_recv_fn = fruit_pread_recv,
6181 .pwrite_send_fn = fruit_pwrite_send,
6182 .pwrite_recv_fn = fruit_pwrite_recv,
6183 .stat_fn = fruit_stat,
6184 .lstat_fn = fruit_lstat,
6185 .fstat_fn = fruit_fstat,
6186 .streaminfo_fn = fruit_streaminfo,
6187 .ntimes_fn = fruit_ntimes,
6188 .ftruncate_fn = fruit_ftruncate,
6189 .fallocate_fn = fruit_fallocate,
6190 .create_file_fn = fruit_create_file,
6191 .readdir_attr_fn = fruit_readdir_attr,
6192 .offload_read_send_fn = fruit_offload_read_send,
6193 .offload_read_recv_fn = fruit_offload_read_recv,
6194 .offload_write_send_fn = fruit_offload_write_send,
6195 .offload_write_recv_fn = fruit_offload_write_recv,
6197 /* NT ACL operations */
6198 .fget_nt_acl_fn = fruit_fget_nt_acl,
6199 .fset_nt_acl_fn = fruit_fset_nt_acl,
6202 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
6203 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6205 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6206 &vfs_fruit_fns);
6207 if (!NT_STATUS_IS_OK(ret)) {
6208 return ret;
6211 vfs_fruit_debug_level = debug_add_class("fruit");
6212 if (vfs_fruit_debug_level == -1) {
6213 vfs_fruit_debug_level = DBGC_VFS;
6214 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6215 "vfs_fruit_init"));
6216 } else {
6217 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6218 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
6221 return ret;