replmd: check for duplicate values in MOD_REPLACE case
[Samba.git] / source3 / modules / vfs_fruit.c
blobb0dd0f062bd16c9b1f044d90db09eb72abe10633
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;
143 bool time_machine;
146 * Additional options, all enabled by default,
147 * possibly useful for analyzing performance. The associated
148 * operations with each of them may be expensive, so having
149 * the chance to disable them individually gives a chance
150 * tweaking the setup for the particular usecase.
152 bool readdir_attr_rsize;
153 bool readdir_attr_finder_info;
154 bool readdir_attr_max_access;
157 static const struct enum_list fruit_rsrc[] = {
158 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
159 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
160 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
161 { -1, NULL}
164 static const struct enum_list fruit_meta[] = {
165 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
166 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
167 { -1, NULL}
170 static const struct enum_list fruit_locking[] = {
171 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
172 {FRUIT_LOCKING_NONE, "none"},
173 { -1, NULL}
176 static const struct enum_list fruit_encoding[] = {
177 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
178 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
179 { -1, NULL}
182 static const char *fruit_catia_maps =
183 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
184 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
185 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
186 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
187 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
188 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
189 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
190 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
191 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
192 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
193 "0x0d:0xf00d";
195 /*****************************************************************************
196 * Defines, functions and data structures that deal with AppleDouble
197 *****************************************************************************/
200 * There are two AppleDouble blobs we deal with:
202 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
203 * metadata in an xattr
205 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
206 * ._ files
208 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
210 /* Version info */
211 #define AD_VERSION2 0x00020000
212 #define AD_VERSION AD_VERSION2
215 * AppleDouble entry IDs.
217 #define ADEID_DFORK 1
218 #define ADEID_RFORK 2
219 #define ADEID_NAME 3
220 #define ADEID_COMMENT 4
221 #define ADEID_ICONBW 5
222 #define ADEID_ICONCOL 6
223 #define ADEID_FILEI 7
224 #define ADEID_FILEDATESI 8
225 #define ADEID_FINDERI 9
226 #define ADEID_MACFILEI 10
227 #define ADEID_PRODOSFILEI 11
228 #define ADEID_MSDOSFILEI 12
229 #define ADEID_SHORTNAME 13
230 #define ADEID_AFPFILEI 14
231 #define ADEID_DID 15
233 /* Private Netatalk entries */
234 #define ADEID_PRIVDEV 16
235 #define ADEID_PRIVINO 17
236 #define ADEID_PRIVSYN 18
237 #define ADEID_PRIVID 19
238 #define ADEID_MAX (ADEID_PRIVID + 1)
241 * These are the real ids for the private entries,
242 * as stored in the adouble file
244 #define AD_DEV 0x80444556
245 #define AD_INO 0x80494E4F
246 #define AD_SYN 0x8053594E
247 #define AD_ID 0x8053567E
249 /* Number of actually used entries */
250 #define ADEID_NUM_XATTR 8
251 #define ADEID_NUM_DOT_UND 2
252 #define ADEID_NUM_RSRC_XATTR 1
254 /* AppleDouble magic */
255 #define AD_APPLESINGLE_MAGIC 0x00051600
256 #define AD_APPLEDOUBLE_MAGIC 0x00051607
257 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
259 /* Sizes of relevant entry bits */
260 #define ADEDLEN_MAGIC 4
261 #define ADEDLEN_VERSION 4
262 #define ADEDLEN_FILLER 16
263 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
264 #define ADEDLEN_NENTRIES 2
265 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
266 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
267 #define AD_ENTRY_LEN_EID 4
268 #define AD_ENTRY_LEN_OFF 4
269 #define AD_ENTRY_LEN_LEN 4
270 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
272 /* Field widths */
273 #define ADEDLEN_NAME 255
274 #define ADEDLEN_COMMENT 200
275 #define ADEDLEN_FILEI 16
276 #define ADEDLEN_FINDERI 32
277 #define ADEDLEN_FILEDATESI 16
278 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
279 #define ADEDLEN_AFPFILEI 4
280 #define ADEDLEN_MACFILEI 4
281 #define ADEDLEN_PRODOSFILEI 8
282 #define ADEDLEN_MSDOSFILEI 2
283 #define ADEDLEN_DID 4
284 #define ADEDLEN_PRIVDEV 8
285 #define ADEDLEN_PRIVINO 8
286 #define ADEDLEN_PRIVSYN 8
287 #define ADEDLEN_PRIVID 4
289 /* Offsets */
290 #define ADEDOFF_MAGIC 0
291 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
292 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
293 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
295 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
296 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
297 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
298 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
299 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
300 ADEDLEN_FILEDATESI)
301 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
302 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
303 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
304 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
306 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
307 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
308 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
310 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
311 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
312 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
313 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
314 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
315 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
317 #if AD_DATASZ_XATTR != 402
318 #error bad size for AD_DATASZ_XATTR
319 #endif
321 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
322 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
323 ADEDLEN_FINDERI)
324 #if AD_DATASZ_DOT_UND != 82
325 #error bad size for AD_DATASZ_DOT_UND
326 #endif
329 * Sharemode locks fcntl() offsets
331 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
332 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
333 #else
334 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
335 #endif
336 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
338 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
339 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
340 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
341 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
342 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
343 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
344 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
345 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
346 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
347 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
349 /* Time stuff we overload the bits a little */
350 #define AD_DATE_CREATE 0
351 #define AD_DATE_MODIFY 4
352 #define AD_DATE_BACKUP 8
353 #define AD_DATE_ACCESS 12
354 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
355 AD_DATE_BACKUP | AD_DATE_ACCESS)
356 #define AD_DATE_UNIX (1 << 10)
357 #define AD_DATE_START 0x80000000
358 #define AD_DATE_DELTA 946684800
359 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
360 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
362 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
363 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
364 #define AD_XATTR_HDR_SIZE 36
365 #define AD_XATTR_MAX_HDR_SIZE 65536
367 /* Accessor macros */
368 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
369 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
370 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
371 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
374 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
375 * representation as well as the on-disk format.
377 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
378 * the length of the FinderInfo entry is larger then 32 bytes. It is then
379 * preceeded with 2 bytes padding.
381 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
384 struct ad_xattr_header {
385 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
386 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
387 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
388 uint32_t adx_data_start; /* file offset to attribute data area */
389 uint32_t adx_data_length; /* length of attribute data area */
390 uint32_t adx_reserved[3];
391 uint16_t adx_flags;
392 uint16_t adx_num_attrs;
395 /* On-disk entries are aligned on 4 byte boundaries */
396 struct ad_xattr_entry {
397 uint32_t adx_offset; /* file offset to data */
398 uint32_t adx_length; /* size of attribute data */
399 uint16_t adx_flags;
400 uint8_t adx_namelen; /* included the NULL terminator */
401 char *adx_name; /* NULL-terminated UTF-8 name */
404 struct ad_entry {
405 size_t ade_off;
406 size_t ade_len;
409 struct adouble {
410 vfs_handle_struct *ad_handle;
411 int ad_fd;
412 bool ad_opened;
413 adouble_type_t ad_type;
414 uint32_t ad_magic;
415 uint32_t ad_version;
416 struct ad_entry ad_eid[ADEID_MAX];
417 char *ad_data;
418 struct ad_xattr_header adx_header;
419 struct ad_xattr_entry *adx_entries;
422 struct ad_entry_order {
423 uint32_t id, offset, len;
426 /* Netatalk AppleDouble metadata xattr */
427 static const
428 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
429 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
430 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
431 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
432 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
433 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
434 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
435 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
436 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
437 {0, 0, 0}
440 /* AppleDouble resource fork file (the ones prefixed by "._") */
441 static const
442 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
443 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
444 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
445 {0, 0, 0}
449 * Fake AppleDouble entry oder for resource fork xattr. The xattr
450 * isn't an AppleDouble file, it simply contains the resource data,
451 * but in order to be able to use some API calls like ad_getentryoff()
452 * we build a fake/helper struct adouble with this entry order struct.
454 static const
455 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
456 {ADEID_RFORK, 0, 0},
457 {0, 0, 0}
460 /* Conversion from enumerated id to on-disk AppleDouble id */
461 #define AD_EID_DISK(a) (set_eid[a])
462 static const uint32_t set_eid[] = {
463 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
464 AD_DEV, AD_INO, AD_SYN, AD_ID
467 struct fio {
468 /* tcon config handle */
469 struct fruit_config_data *config;
471 /* Denote stream type, meta or rsrc */
472 adouble_type_t type;
476 * Forward declarations
478 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
479 adouble_type_t type);
480 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
481 static int ad_fset(struct adouble *ad, files_struct *fsp);
482 static int adouble_path(TALLOC_CTX *ctx,
483 const struct smb_filename *smb_fname__in,
484 struct smb_filename **ppsmb_fname_out);
487 * Return a pointer to an AppleDouble entry
489 * Returns NULL if the entry is not present
491 static char *ad_get_entry(const struct adouble *ad, int eid)
493 off_t off = ad_getentryoff(ad, eid);
494 size_t len = ad_getentrylen(ad, eid);
496 if (off == 0 || len == 0) {
497 return NULL;
500 return ad->ad_data + off;
504 * Get a date
506 static int ad_getdate(const struct adouble *ad,
507 unsigned int dateoff,
508 uint32_t *date)
510 bool xlate = (dateoff & AD_DATE_UNIX);
511 char *p = NULL;
513 dateoff &= AD_DATE_MASK;
514 p = ad_get_entry(ad, ADEID_FILEDATESI);
515 if (p == NULL) {
516 return -1;
519 if (dateoff > AD_DATE_ACCESS) {
520 return -1;
523 memcpy(date, p + dateoff, sizeof(uint32_t));
525 if (xlate) {
526 *date = AD_DATE_TO_UNIX(*date);
528 return 0;
532 * Set a date
534 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
536 bool xlate = (dateoff & AD_DATE_UNIX);
537 char *p = NULL;
539 p = ad_get_entry(ad, ADEID_FILEDATESI);
540 if (p == NULL) {
541 return -1;
544 dateoff &= AD_DATE_MASK;
545 if (xlate) {
546 date = AD_DATE_FROM_UNIX(date);
549 if (dateoff > AD_DATE_ACCESS) {
550 return -1;
553 memcpy(p + dateoff, &date, sizeof(date));
555 return 0;
560 * Map on-disk AppleDouble id to enumerated id
562 static uint32_t get_eid(uint32_t eid)
564 if (eid <= 15) {
565 return eid;
568 switch (eid) {
569 case AD_DEV:
570 return ADEID_PRIVDEV;
571 case AD_INO:
572 return ADEID_PRIVINO;
573 case AD_SYN:
574 return ADEID_PRIVSYN;
575 case AD_ID:
576 return ADEID_PRIVID;
577 default:
578 break;
581 return 0;
585 * Pack AppleDouble structure into data buffer
587 static bool ad_pack(struct adouble *ad)
589 uint32_t eid;
590 uint16_t nent;
591 uint32_t bufsize;
592 uint32_t offset = 0;
594 bufsize = talloc_get_size(ad->ad_data);
595 if (bufsize < AD_DATASZ_DOT_UND) {
596 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
597 return false;
600 if (offset + ADEDLEN_MAGIC < offset ||
601 offset + ADEDLEN_MAGIC >= bufsize) {
602 return false;
604 RSIVAL(ad->ad_data, offset, ad->ad_magic);
605 offset += ADEDLEN_MAGIC;
607 if (offset + ADEDLEN_VERSION < offset ||
608 offset + ADEDLEN_VERSION >= bufsize) {
609 return false;
611 RSIVAL(ad->ad_data, offset, ad->ad_version);
612 offset += ADEDLEN_VERSION;
614 if (offset + ADEDLEN_FILLER < offset ||
615 offset + ADEDLEN_FILLER >= bufsize) {
616 return false;
618 if (ad->ad_type == ADOUBLE_RSRC) {
619 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
621 offset += ADEDLEN_FILLER;
623 if (offset + ADEDLEN_NENTRIES < offset ||
624 offset + ADEDLEN_NENTRIES >= bufsize) {
625 return false;
627 offset += ADEDLEN_NENTRIES;
629 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
630 if (ad->ad_eid[eid].ade_off == 0) {
632 * ade_off is also used as indicator whether a
633 * specific entry is used or not
635 continue;
638 if (offset + AD_ENTRY_LEN_EID < offset ||
639 offset + AD_ENTRY_LEN_EID >= bufsize) {
640 return false;
642 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
643 offset += AD_ENTRY_LEN_EID;
645 if (offset + AD_ENTRY_LEN_OFF < offset ||
646 offset + AD_ENTRY_LEN_OFF >= bufsize) {
647 return false;
649 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
650 offset += AD_ENTRY_LEN_OFF;
652 if (offset + AD_ENTRY_LEN_LEN < offset ||
653 offset + AD_ENTRY_LEN_LEN >= bufsize) {
654 return false;
656 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
657 offset += AD_ENTRY_LEN_LEN;
659 nent++;
662 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
663 return false;
665 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
667 return true;
670 static bool ad_unpack_xattrs(struct adouble *ad)
672 struct ad_xattr_header *h = &ad->adx_header;
673 const char *p = ad->ad_data;
674 uint32_t hoff;
675 uint32_t i;
677 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
678 return true;
681 /* 2 bytes padding */
682 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
684 h->adx_magic = RIVAL(p, hoff + 0);
685 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
686 h->adx_total_size = RIVAL(p, hoff + 8);
687 h->adx_data_start = RIVAL(p, hoff + 12);
688 h->adx_data_length = RIVAL(p, hoff + 16);
689 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
690 h->adx_num_attrs = RSVAL(p, hoff + 34);
692 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
693 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
694 return false;
697 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
698 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
699 return false;
701 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
702 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
703 return false;
706 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
707 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
708 return false;
711 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
712 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
713 return false;
715 if ((h->adx_data_start + h->adx_data_length) >
716 ad->adx_header.adx_total_size)
718 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
719 return false;
722 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
723 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
724 return false;
727 if (h->adx_num_attrs == 0) {
728 return true;
731 ad->adx_entries = talloc_zero_array(
732 ad, struct ad_xattr_entry, h->adx_num_attrs);
733 if (ad->adx_entries == NULL) {
734 return false;
737 hoff += AD_XATTR_HDR_SIZE;
739 for (i = 0; i < h->adx_num_attrs; i++) {
740 struct ad_xattr_entry *e = &ad->adx_entries[i];
742 hoff = (hoff + 3) & ~3;
744 e->adx_offset = RIVAL(p, hoff + 0);
745 e->adx_length = RIVAL(p, hoff + 4);
746 e->adx_flags = RSVAL(p, hoff + 8);
747 e->adx_namelen = *(p + hoff + 10);
749 if (e->adx_offset >= ad->adx_header.adx_total_size) {
750 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
751 e->adx_offset);
752 return false;
755 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
756 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
757 e->adx_length);
758 return false;
761 if ((e->adx_offset + e->adx_length) >
762 ad->adx_header.adx_total_size)
764 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
765 e->adx_length);
766 return false;
769 if (e->adx_namelen == 0) {
770 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
771 e->adx_namelen);
772 return false;
774 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
775 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
776 e->adx_namelen);
777 return false;
779 if ((hoff + 11 + e->adx_namelen) >
780 ad->adx_header.adx_data_start)
782 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
783 e->adx_namelen);
784 return false;
787 e->adx_name = talloc_strndup(ad->adx_entries,
788 p + hoff + 11,
789 e->adx_namelen);
790 if (e->adx_name == NULL) {
791 return false;
794 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
795 e->adx_name, e->adx_offset, e->adx_length);
796 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
797 e->adx_length);
799 hoff += 11 + e->adx_namelen;
802 return true;
806 * Unpack an AppleDouble blob into a struct adoble
808 static bool ad_unpack(struct adouble *ad, const size_t nentries,
809 size_t filesize)
811 size_t bufsize = talloc_get_size(ad->ad_data);
812 size_t adentries, i;
813 uint32_t eid, len, off;
814 bool ok;
817 * The size of the buffer ad->ad_data is checked when read, so
818 * we wouldn't have to check our own offsets, a few extra
819 * checks won't hurt though. We have to check the offsets we
820 * read from the buffer anyway.
823 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
824 DEBUG(1, ("bad size\n"));
825 return false;
828 ad->ad_magic = RIVAL(ad->ad_data, 0);
829 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
830 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
831 DEBUG(1, ("wrong magic or version\n"));
832 return false;
835 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
836 if (adentries != nentries) {
837 DEBUG(1, ("invalid number of entries: %zu\n",
838 adentries));
839 return false;
842 /* now, read in the entry bits */
843 for (i = 0; i < adentries; i++) {
844 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
845 eid = get_eid(eid);
846 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
847 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
849 if (!eid || eid >= ADEID_MAX) {
850 DEBUG(1, ("bogus eid %d\n", eid));
851 return false;
855 * All entries other than the resource fork are
856 * expected to be read into the ad_data buffer, so
857 * ensure the specified offset is within that bound
859 if ((off > bufsize) && (eid != ADEID_RFORK)) {
860 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
861 eid, off, len));
862 return false;
866 * All entries besides FinderInfo and resource fork
867 * must fit into the buffer. FinderInfo is special as
868 * it may be larger then the default 32 bytes (if it
869 * contains marshalled xattrs), but we will fixup that
870 * in ad_convert(). And the resource fork is never
871 * accessed directly by the ad_data buf (also see
872 * comment above) anyway.
874 if ((eid != ADEID_RFORK) &&
875 (eid != ADEID_FINDERI) &&
876 ((off + len) > bufsize)) {
877 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
878 eid, off, len));
879 return false;
883 * That would be obviously broken
885 if (off > filesize) {
886 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
887 eid, off, len));
888 return false;
892 * Check for any entry that has its end beyond the
893 * filesize.
895 if (off + len < off) {
896 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
897 ", len: %" PRIu32 "\n",
898 eid, off, len));
899 return false;
902 if (off + len > filesize) {
904 * If this is the resource fork entry, we fix
905 * up the length, for any other entry we bail
906 * out.
908 if (eid != ADEID_RFORK) {
909 DEBUG(1, ("bogus eid %d: off: %" PRIu32
910 ", len: %" PRIu32 "\n",
911 eid, off, len));
912 return false;
916 * Fixup the resource fork entry by limiting
917 * the size to entryoffset - filesize.
919 len = filesize - off;
920 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
921 ", len: %" PRIu32 "\n", off, len));
924 ad->ad_eid[eid].ade_off = off;
925 ad->ad_eid[eid].ade_len = len;
928 ok = ad_unpack_xattrs(ad);
929 if (!ok) {
930 return false;
933 return true;
936 static bool ad_convert_xattr(struct adouble *ad,
937 const struct smb_filename *smb_fname,
938 char *map)
940 static struct char_mappings **string_replace_cmaps = NULL;
941 uint16_t i;
942 int saved_errno = 0;
943 NTSTATUS status;
945 if (ad->adx_header.adx_num_attrs == 0) {
946 return true;
949 if (string_replace_cmaps == NULL) {
950 const char **mappings = NULL;
952 mappings = str_list_make_v3_const(
953 talloc_tos(), fruit_catia_maps, NULL);
954 if (mappings == NULL) {
955 return false;
957 string_replace_cmaps = string_replace_init_map(mappings);
958 TALLOC_FREE(mappings);
961 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
962 struct ad_xattr_entry *e = &ad->adx_entries[i];
963 char *mapped_name = NULL;
964 char *tmp = NULL;
965 struct smb_filename *stream_name = NULL;
966 files_struct *fsp = NULL;
967 ssize_t nwritten;
969 status = string_replace_allocate(ad->ad_handle->conn,
970 e->adx_name,
971 string_replace_cmaps,
972 talloc_tos(),
973 &mapped_name,
974 vfs_translate_to_windows);
975 if (!NT_STATUS_IS_OK(status) &&
976 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
978 DBG_ERR("string_replace_allocate failed\n");
979 return -1;
982 tmp = mapped_name;
983 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
984 TALLOC_FREE(tmp);
985 if (mapped_name == NULL) {
986 return -1;
989 stream_name = synthetic_smb_fname(talloc_tos(),
990 smb_fname->base_name,
991 mapped_name,
992 NULL,
993 smb_fname->flags);
994 TALLOC_FREE(mapped_name);
995 if (stream_name == NULL) {
996 DBG_ERR("synthetic_smb_fname failed\n");
997 return -1;
1000 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1002 status = SMB_VFS_CREATE_FILE(
1003 ad->ad_handle->conn, /* conn */
1004 NULL, /* req */
1005 0, /* root_dir_fid */
1006 stream_name, /* fname */
1007 FILE_GENERIC_WRITE, /* access_mask */
1008 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1009 FILE_OPEN_IF, /* create_disposition */
1010 0, /* create_options */
1011 0, /* file_attributes */
1012 INTERNAL_OPEN_ONLY, /* oplock_request */
1013 NULL, /* lease */
1014 0, /* allocation_size */
1015 0, /* private_flags */
1016 NULL, /* sd */
1017 NULL, /* ea_list */
1018 &fsp, /* result */
1019 NULL, /* psbuf */
1020 NULL, NULL); /* create context */
1021 TALLOC_FREE(stream_name);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1024 return -1;
1027 nwritten = SMB_VFS_PWRITE(fsp,
1028 map + e->adx_offset,
1029 e->adx_length,
1031 if (nwritten == -1) {
1032 DBG_ERR("SMB_VFS_PWRITE failed\n");
1033 saved_errno = errno;
1034 close_file(NULL, fsp, ERROR_CLOSE);
1035 errno = saved_errno;
1036 return -1;
1039 status = close_file(NULL, fsp, NORMAL_CLOSE);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 return -1;
1043 fsp = NULL;
1046 return true;
1050 * Convert from Apple's ._ file to Netatalk
1052 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1053 * bytes containing packed xattrs. Netatalk can't deal with that, so
1054 * we simply discard the packed xattrs.
1056 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1057 * otherwise
1059 static int ad_convert(struct adouble *ad,
1060 const struct smb_filename *smb_fname,
1061 int fd)
1063 int rc = 0;
1064 char *map = MAP_FAILED;
1065 size_t origlen;
1066 bool ok;
1068 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1069 ad_getentrylen(ad, ADEID_RFORK);
1071 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1072 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1073 if (map == MAP_FAILED) {
1074 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1075 rc = -1;
1076 goto exit;
1079 ok = ad_convert_xattr(ad, smb_fname, map);
1080 if (!ok) {
1081 return -1;
1084 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1085 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1086 map + ad_getentryoff(ad, ADEID_RFORK),
1087 ad_getentrylen(ad, ADEID_RFORK));
1090 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1091 ad_setentryoff(ad, ADEID_RFORK,
1092 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1095 * FIXME: direct ftruncate(), but we don't have a fsp for the
1096 * VFS call
1098 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
1099 + ad_getentrylen(ad, ADEID_RFORK));
1101 exit:
1102 if (map != MAP_FAILED) {
1103 munmap(map, origlen);
1105 return rc;
1109 * Read and parse Netatalk AppleDouble metadata xattr
1111 static ssize_t ad_read_meta(struct adouble *ad,
1112 const struct smb_filename *smb_fname)
1114 int rc = 0;
1115 ssize_t ealen;
1116 bool ok;
1118 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1120 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1121 AFPINFO_EA_NETATALK, ad->ad_data,
1122 AD_DATASZ_XATTR);
1123 if (ealen == -1) {
1124 switch (errno) {
1125 case ENOATTR:
1126 case ENOENT:
1127 if (errno == ENOATTR) {
1128 errno = ENOENT;
1130 rc = -1;
1131 goto exit;
1132 default:
1133 DEBUG(2, ("error reading meta xattr: %s\n",
1134 strerror(errno)));
1135 rc = -1;
1136 goto exit;
1139 if (ealen != AD_DATASZ_XATTR) {
1140 DEBUG(2, ("bad size %zd\n", ealen));
1141 errno = EINVAL;
1142 rc = -1;
1143 goto exit;
1146 /* Now parse entries */
1147 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1148 if (!ok) {
1149 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1150 errno = EINVAL;
1151 rc = -1;
1152 goto exit;
1155 if (!ad_getentryoff(ad, ADEID_FINDERI)
1156 || !ad_getentryoff(ad, ADEID_COMMENT)
1157 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1158 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1159 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1160 || !ad_getentryoff(ad, ADEID_PRIVINO)
1161 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1162 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1163 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1164 errno = EINVAL;
1165 rc = -1;
1166 goto exit;
1169 exit:
1170 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1171 smb_fname->base_name, rc));
1173 if (rc != 0) {
1174 ealen = -1;
1175 if (errno == EINVAL) {
1176 become_root();
1177 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1178 unbecome_root();
1179 errno = ENOENT;
1182 return ealen;
1185 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1186 int flags,
1187 mode_t mode)
1189 #ifdef HAVE_ATTROPEN
1190 /* FIXME: direct Solaris xattr syscall */
1191 return attropen(smb_fname->base_name,
1192 AFPRESOURCE_EA_NETATALK, flags, mode);
1193 #else
1194 errno = ENOSYS;
1195 return -1;
1196 #endif
1199 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1200 int flags,
1201 mode_t mode)
1203 int ret;
1204 int fd;
1205 struct smb_filename *adp_smb_fname = NULL;
1207 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1208 if (ret != 0) {
1209 return -1;
1212 fd = open(adp_smb_fname->base_name, flags, mode);
1213 TALLOC_FREE(adp_smb_fname);
1215 return fd;
1218 static int ad_open_rsrc(vfs_handle_struct *handle,
1219 const struct smb_filename *smb_fname,
1220 int flags,
1221 mode_t mode)
1223 struct fruit_config_data *config = NULL;
1224 int fd;
1226 SMB_VFS_HANDLE_GET_DATA(handle, config,
1227 struct fruit_config_data, return -1);
1229 if (config->rsrc == FRUIT_RSRC_XATTR) {
1230 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1231 } else {
1232 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1235 return fd;
1239 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1240 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1241 * for file IO on the ._ file.
1243 static int ad_open(vfs_handle_struct *handle,
1244 struct adouble *ad,
1245 files_struct *fsp,
1246 const struct smb_filename *smb_fname,
1247 int flags,
1248 mode_t mode)
1250 int fd;
1252 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1253 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1255 if (ad->ad_type == ADOUBLE_META) {
1256 return 0;
1259 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1260 ad->ad_fd = fsp->fh->fd;
1261 ad->ad_opened = false;
1262 return 0;
1265 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1266 if (fd == -1) {
1267 return -1;
1269 ad->ad_opened = true;
1270 ad->ad_fd = fd;
1272 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1273 smb_fname->base_name,
1274 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1276 return 0;
1279 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1281 int ret;
1282 SMB_STRUCT_STAT st;
1284 /* FIXME: direct sys_fstat(), don't have an fsp */
1285 ret = sys_fstat(ad->ad_fd, &st,
1286 lp_fake_directory_create_times(
1287 SNUM(ad->ad_handle->conn)));
1288 if (ret != 0) {
1289 return -1;
1292 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1293 return st.st_ex_size;
1296 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1297 const struct smb_filename *smb_fname)
1299 struct adouble *meta_ad = NULL;
1300 SMB_STRUCT_STAT sbuf;
1301 char *p_ad = NULL;
1302 char *p_meta_ad = NULL;
1303 ssize_t len;
1304 size_t size;
1305 int ret;
1306 bool ok;
1308 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1309 SNUM(ad->ad_handle->conn)));
1310 if (ret != 0) {
1311 return -1;
1315 * AppleDouble file header content and size, two cases:
1317 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1318 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1320 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1322 size = sbuf.st_ex_size;
1323 if (size > talloc_array_length(ad->ad_data)) {
1324 if (size > AD_XATTR_MAX_HDR_SIZE) {
1325 size = AD_XATTR_MAX_HDR_SIZE;
1327 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1328 if (p_ad == NULL) {
1329 return -1;
1331 ad->ad_data = p_ad;
1334 len = sys_pread(ad->ad_fd, ad->ad_data,
1335 talloc_array_length(ad->ad_data), 0);
1336 if (len != talloc_array_length(ad->ad_data)) {
1337 DBG_NOTICE("%s %s: bad size: %zd\n",
1338 smb_fname->base_name, strerror(errno), len);
1339 return -1;
1342 /* Now parse entries */
1343 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1344 if (!ok) {
1345 DBG_ERR("invalid AppleDouble resource %s\n",
1346 smb_fname->base_name);
1347 errno = EINVAL;
1348 return -1;
1351 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1352 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1353 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1354 DBG_ERR("invalid AppleDouble resource %s\n",
1355 smb_fname->base_name);
1356 errno = EINVAL;
1357 return -1;
1360 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1361 return len;
1365 * Try to fixup AppleDouble files created by OS X with xattrs
1366 * appended to the ADEID_FINDERI entry. We simply remove the
1367 * xattrs blob, this means any fancy xattr that was stored
1368 * there is lost.
1371 ret = ad_convert(ad, smb_fname, ad->ad_fd);
1372 if (ret != 0) {
1373 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1374 return len;
1377 ok = ad_pack(ad);
1378 if (!ok) {
1379 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1380 return -1;
1383 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1384 if (len != AD_DATASZ_DOT_UND) {
1385 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1386 return -1;
1389 meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
1390 if (meta_ad == NULL) {
1391 return -1;
1394 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1395 if (p_ad == NULL) {
1396 TALLOC_FREE(meta_ad);
1397 return -1;
1399 p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
1400 if (p_meta_ad == NULL) {
1401 TALLOC_FREE(meta_ad);
1402 return -1;
1405 memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
1407 ret = ad_set(meta_ad, smb_fname);
1408 TALLOC_FREE(meta_ad);
1409 if (ret != 0) {
1410 return -1;
1413 return len;
1417 * Read and parse resource fork, either ._ AppleDouble file or xattr
1419 static ssize_t ad_read_rsrc(struct adouble *ad,
1420 const struct smb_filename *smb_fname)
1422 struct fruit_config_data *config = NULL;
1423 ssize_t len;
1425 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1426 struct fruit_config_data, return -1);
1428 if (config->rsrc == FRUIT_RSRC_XATTR) {
1429 len = ad_read_rsrc_xattr(ad);
1430 } else {
1431 len = ad_read_rsrc_adouble(ad, smb_fname);
1434 return len;
1438 * Read and unpack an AppleDouble metadata xattr or resource
1440 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1442 switch (ad->ad_type) {
1443 case ADOUBLE_META:
1444 return ad_read_meta(ad, smb_fname);
1445 case ADOUBLE_RSRC:
1446 return ad_read_rsrc(ad, smb_fname);
1447 default:
1448 return -1;
1452 static int adouble_destructor(struct adouble *ad)
1454 if ((ad->ad_fd != -1) && ad->ad_opened) {
1455 close(ad->ad_fd);
1456 ad->ad_fd = -1;
1458 return 0;
1462 * Allocate a struct adouble without initialiing it
1464 * The struct is either hang of the fsp extension context or if fsp is
1465 * NULL from ctx.
1467 * @param[in] ctx talloc context
1468 * @param[in] handle vfs handle
1469 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1471 * @return adouble handle
1473 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1474 adouble_type_t type)
1476 int rc = 0;
1477 size_t adsize = 0;
1478 struct adouble *ad;
1479 struct fruit_config_data *config;
1481 SMB_VFS_HANDLE_GET_DATA(handle, config,
1482 struct fruit_config_data, return NULL);
1484 switch (type) {
1485 case ADOUBLE_META:
1486 adsize = AD_DATASZ_XATTR;
1487 break;
1488 case ADOUBLE_RSRC:
1489 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1490 adsize = AD_DATASZ_DOT_UND;
1492 break;
1493 default:
1494 return NULL;
1497 ad = talloc_zero(ctx, struct adouble);
1498 if (ad == NULL) {
1499 rc = -1;
1500 goto exit;
1503 if (adsize) {
1504 ad->ad_data = talloc_zero_array(ad, char, adsize);
1505 if (ad->ad_data == NULL) {
1506 rc = -1;
1507 goto exit;
1511 ad->ad_handle = handle;
1512 ad->ad_type = type;
1513 ad->ad_magic = AD_MAGIC;
1514 ad->ad_version = AD_VERSION;
1515 ad->ad_fd = -1;
1517 talloc_set_destructor(ad, adouble_destructor);
1519 exit:
1520 if (rc != 0) {
1521 TALLOC_FREE(ad);
1523 return ad;
1527 * Allocate and initialize a new struct adouble
1529 * @param[in] ctx talloc context
1530 * @param[in] handle vfs handle
1531 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1533 * @return adouble handle, initialized
1535 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1536 adouble_type_t type)
1538 int rc = 0;
1539 const struct ad_entry_order *eid;
1540 struct adouble *ad = NULL;
1541 struct fruit_config_data *config;
1542 time_t t = time(NULL);
1544 SMB_VFS_HANDLE_GET_DATA(handle, config,
1545 struct fruit_config_data, return NULL);
1547 switch (type) {
1548 case ADOUBLE_META:
1549 eid = entry_order_meta_xattr;
1550 break;
1551 case ADOUBLE_RSRC:
1552 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1553 eid = entry_order_dot_und;
1554 } else {
1555 eid = entry_order_rsrc_xattr;
1557 break;
1558 default:
1559 return NULL;
1562 ad = ad_alloc(ctx, handle, type);
1563 if (ad == NULL) {
1564 return NULL;
1567 while (eid->id) {
1568 ad->ad_eid[eid->id].ade_off = eid->offset;
1569 ad->ad_eid[eid->id].ade_len = eid->len;
1570 eid++;
1573 /* put something sane in the date fields */
1574 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1575 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1576 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1577 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1579 if (rc != 0) {
1580 TALLOC_FREE(ad);
1582 return ad;
1585 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1586 vfs_handle_struct *handle,
1587 files_struct *fsp,
1588 const struct smb_filename *smb_fname,
1589 adouble_type_t type)
1591 int rc = 0;
1592 ssize_t len;
1593 struct adouble *ad = NULL;
1594 int mode;
1596 if (fsp != NULL) {
1597 smb_fname = fsp->base_fsp->fsp_name;
1600 DEBUG(10, ("ad_get(%s) called for %s\n",
1601 type == ADOUBLE_META ? "meta" : "rsrc",
1602 smb_fname->base_name));
1604 ad = ad_alloc(ctx, handle, type);
1605 if (ad == NULL) {
1606 rc = -1;
1607 goto exit;
1610 /* Try rw first so we can use the fd in ad_convert() */
1611 mode = O_RDWR;
1613 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1614 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1615 mode = O_RDONLY;
1616 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1618 if (rc == -1) {
1619 DBG_DEBUG("ad_open [%s] error [%s]\n",
1620 smb_fname->base_name, strerror(errno));
1621 goto exit;
1625 len = ad_read(ad, smb_fname);
1626 if (len == -1) {
1627 DEBUG(10, ("error reading AppleDouble for %s\n",
1628 smb_fname->base_name));
1629 rc = -1;
1630 goto exit;
1633 exit:
1634 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1635 type == ADOUBLE_META ? "meta" : "rsrc",
1636 smb_fname->base_name, rc));
1638 if (rc != 0) {
1639 TALLOC_FREE(ad);
1641 return ad;
1645 * Return AppleDouble data for a file
1647 * @param[in] ctx talloc context
1648 * @param[in] handle vfs handle
1649 * @param[in] smb_fname pathname to file or directory
1650 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1652 * @return talloced struct adouble or NULL on error
1654 static struct adouble *ad_get(TALLOC_CTX *ctx,
1655 vfs_handle_struct *handle,
1656 const struct smb_filename *smb_fname,
1657 adouble_type_t type)
1659 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1663 * Return AppleDouble data for a file
1665 * @param[in] ctx talloc context
1666 * @param[in] handle vfs handle
1667 * @param[in] fsp fsp to use for IO
1668 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1670 * @return talloced struct adouble or NULL on error
1672 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1673 files_struct *fsp, adouble_type_t type)
1675 return ad_get_internal(ctx, handle, fsp, NULL, type);
1679 * Set AppleDouble metadata on a file or directory
1681 * @param[in] ad adouble handle
1683 * @param[in] smb_fname pathname to file or directory
1685 * @return status code, 0 means success
1687 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1689 bool ok;
1690 int ret;
1692 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1694 if (ad->ad_type != ADOUBLE_META) {
1695 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1696 smb_fname->base_name);
1697 return -1;
1700 ok = ad_pack(ad);
1701 if (!ok) {
1702 return -1;
1705 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1706 smb_fname,
1707 AFPINFO_EA_NETATALK,
1708 ad->ad_data,
1709 AD_DATASZ_XATTR, 0);
1711 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1713 return ret;
1717 * Set AppleDouble metadata on a file or directory
1719 * @param[in] ad adouble handle
1720 * @param[in] fsp file handle
1722 * @return status code, 0 means success
1724 static int ad_fset(struct adouble *ad, files_struct *fsp)
1726 int rc = -1;
1727 ssize_t len;
1728 bool ok;
1730 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1732 if ((fsp == NULL)
1733 || (fsp->fh == NULL)
1734 || (fsp->fh->fd == -1))
1736 smb_panic("bad fsp");
1739 ok = ad_pack(ad);
1740 if (!ok) {
1741 return -1;
1744 switch (ad->ad_type) {
1745 case ADOUBLE_META:
1746 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1747 fsp->fsp_name,
1748 AFPINFO_EA_NETATALK,
1749 ad->ad_data,
1750 AD_DATASZ_XATTR, 0);
1751 break;
1753 case ADOUBLE_RSRC:
1754 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1755 fsp,
1756 ad->ad_data,
1757 AD_DATASZ_DOT_UND,
1759 if (len != AD_DATASZ_DOT_UND) {
1760 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1761 return -1;
1763 rc = 0;
1764 break;
1766 default:
1767 return -1;
1770 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1772 return rc;
1775 /*****************************************************************************
1776 * Helper functions
1777 *****************************************************************************/
1779 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1781 if (strncasecmp_m(smb_fname->stream_name,
1782 AFPINFO_STREAM_NAME,
1783 strlen(AFPINFO_STREAM_NAME)) == 0) {
1784 return true;
1786 return false;
1789 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1791 if (strncasecmp_m(smb_fname->stream_name,
1792 AFPRESOURCE_STREAM_NAME,
1793 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1794 return true;
1796 return false;
1800 * Test whether stream is an Apple stream, not used atm
1802 #if 0
1803 static bool is_apple_stream(const struct smb_filename *smb_fname)
1805 if (is_afpinfo_stream(smb_fname)) {
1806 return true;
1808 if (is_afpresource_stream(smb_fname)) {
1809 return true;
1811 return false;
1813 #endif
1816 * Initialize config struct from our smb.conf config parameters
1818 static int init_fruit_config(vfs_handle_struct *handle)
1820 struct fruit_config_data *config;
1821 int enumval;
1823 config = talloc_zero(handle->conn, struct fruit_config_data);
1824 if (!config) {
1825 DEBUG(1, ("talloc_zero() failed\n"));
1826 errno = ENOMEM;
1827 return -1;
1831 * Versions up to Samba 4.5.x had a spelling bug in the
1832 * fruit:resource option calling lp_parm_enum with
1833 * "res*s*ource" (ie two s).
1835 * In Samba 4.6 we accept both the wrong and the correct
1836 * spelling, in Samba 4.7 the bad spelling will be removed.
1838 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1839 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1840 if (enumval == -1) {
1841 DEBUG(1, ("value for %s: resource type unknown\n",
1842 FRUIT_PARAM_TYPE_NAME));
1843 return -1;
1845 config->rsrc = (enum fruit_rsrc)enumval;
1847 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1848 "resource", fruit_rsrc, enumval);
1849 if (enumval == -1) {
1850 DEBUG(1, ("value for %s: resource type unknown\n",
1851 FRUIT_PARAM_TYPE_NAME));
1852 return -1;
1854 config->rsrc = (enum fruit_rsrc)enumval;
1856 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1857 "metadata", fruit_meta, FRUIT_META_NETATALK);
1858 if (enumval == -1) {
1859 DEBUG(1, ("value for %s: metadata type unknown\n",
1860 FRUIT_PARAM_TYPE_NAME));
1861 return -1;
1863 config->meta = (enum fruit_meta)enumval;
1865 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1866 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1867 if (enumval == -1) {
1868 DEBUG(1, ("value for %s: locking type unknown\n",
1869 FRUIT_PARAM_TYPE_NAME));
1870 return -1;
1872 config->locking = (enum fruit_locking)enumval;
1874 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1875 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1876 if (enumval == -1) {
1877 DEBUG(1, ("value for %s: encoding type unknown\n",
1878 FRUIT_PARAM_TYPE_NAME));
1879 return -1;
1881 config->encoding = (enum fruit_encoding)enumval;
1883 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1884 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1885 FRUIT_PARAM_TYPE_NAME,
1886 "veto_appledouble",
1887 true);
1890 config->use_aapl = lp_parm_bool(
1891 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1893 config->time_machine = lp_parm_bool(
1894 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
1896 config->unix_info_enabled = lp_parm_bool(
1897 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1899 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1900 "copyfile", false);
1902 config->posix_rename = lp_parm_bool(
1903 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1905 config->aapl_zero_file_id =
1906 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1908 config->readdir_attr_rsize = lp_parm_bool(
1909 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1911 config->readdir_attr_finder_info = lp_parm_bool(
1912 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1914 config->readdir_attr_max_access = lp_parm_bool(
1915 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1917 config->model = lp_parm_const_string(
1918 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1920 SMB_VFS_HANDLE_SET_DATA(handle, config,
1921 NULL, struct fruit_config_data,
1922 return -1);
1924 return 0;
1928 * Prepend "._" to a basename
1929 * Return a new struct smb_filename with stream_name == NULL.
1931 static int adouble_path(TALLOC_CTX *ctx,
1932 const struct smb_filename *smb_fname_in,
1933 struct smb_filename **pp_smb_fname_out)
1935 char *parent;
1936 const char *base;
1937 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1938 smb_fname_in);
1940 if (smb_fname == NULL) {
1941 return -1;
1944 /* We need streamname to be NULL */
1945 TALLOC_FREE(smb_fname->stream_name);
1947 /* And we're replacing base_name. */
1948 TALLOC_FREE(smb_fname->base_name);
1950 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
1951 &parent, &base)) {
1952 TALLOC_FREE(smb_fname);
1953 return -1;
1956 smb_fname->base_name = talloc_asprintf(smb_fname,
1957 "%s/._%s", parent, base);
1958 if (smb_fname->base_name == NULL) {
1959 TALLOC_FREE(smb_fname);
1960 return -1;
1963 *pp_smb_fname_out = smb_fname;
1965 return 0;
1969 * Allocate and initialize an AfpInfo struct
1971 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1973 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1974 if (ai == NULL) {
1975 return NULL;
1977 ai->afpi_Signature = AFP_Signature;
1978 ai->afpi_Version = AFP_Version;
1979 ai->afpi_BackupTime = AD_DATE_START;
1980 return ai;
1984 * Pack an AfpInfo struct into a buffer
1986 * Buffer size must be at least AFP_INFO_SIZE
1987 * Returns size of packed buffer
1989 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1991 memset(buf, 0, AFP_INFO_SIZE);
1993 RSIVAL(buf, 0, ai->afpi_Signature);
1994 RSIVAL(buf, 4, ai->afpi_Version);
1995 RSIVAL(buf, 12, ai->afpi_BackupTime);
1996 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1998 return AFP_INFO_SIZE;
2002 * Unpack a buffer into a AfpInfo structure
2004 * Buffer size must be at least AFP_INFO_SIZE
2005 * Returns allocated AfpInfo struct
2007 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2009 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2010 if (ai == NULL) {
2011 return NULL;
2014 ai->afpi_Signature = RIVAL(data, 0);
2015 ai->afpi_Version = RIVAL(data, 4);
2016 ai->afpi_BackupTime = RIVAL(data, 12);
2017 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2018 sizeof(ai->afpi_FinderInfo));
2020 if (ai->afpi_Signature != AFP_Signature
2021 || ai->afpi_Version != AFP_Version) {
2022 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2023 TALLOC_FREE(ai);
2026 return ai;
2030 * Fake an inode number from the md5 hash of the (xattr) name
2032 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2034 MD5_CTX ctx;
2035 unsigned char hash[16];
2036 SMB_INO_T result;
2037 char *upper_sname;
2039 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2040 SMB_ASSERT(upper_sname != NULL);
2042 MD5Init(&ctx);
2043 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2044 sizeof(sbuf->st_ex_dev));
2045 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2046 sizeof(sbuf->st_ex_ino));
2047 MD5Update(&ctx, (unsigned char *)upper_sname,
2048 talloc_get_size(upper_sname)-1);
2049 MD5Final(hash, &ctx);
2051 TALLOC_FREE(upper_sname);
2053 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2054 memcpy(&result, hash, sizeof(result));
2056 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2057 sname, (unsigned long long)result));
2059 return result;
2062 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2063 struct stream_struct **streams,
2064 const char *name, off_t size,
2065 off_t alloc_size)
2067 struct stream_struct *tmp;
2069 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2070 (*num_streams)+1);
2071 if (tmp == NULL) {
2072 return false;
2075 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2076 if (tmp[*num_streams].name == NULL) {
2077 return false;
2080 tmp[*num_streams].size = size;
2081 tmp[*num_streams].alloc_size = alloc_size;
2083 *streams = tmp;
2084 *num_streams += 1;
2085 return true;
2088 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2089 struct stream_struct **streams)
2091 struct stream_struct *tmp = *streams;
2092 unsigned int i;
2094 if (*num_streams == 0) {
2095 return true;
2098 for (i = 0; i < *num_streams; i++) {
2099 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2100 break;
2104 if (i == *num_streams) {
2105 return true;
2108 if (tmp[i].size > 0) {
2109 return true;
2112 TALLOC_FREE(tmp[i].name);
2113 if (*num_streams - 1 > i) {
2114 memmove(&tmp[i], &tmp[i+1],
2115 (*num_streams - i - 1) * sizeof(struct stream_struct));
2118 *num_streams -= 1;
2119 return true;
2122 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2123 struct stream_struct **streams,
2124 const char *name)
2126 struct stream_struct *tmp = *streams;
2127 unsigned int i;
2129 if (*num_streams == 0) {
2130 return true;
2133 for (i = 0; i < *num_streams; i++) {
2134 if (strequal_m(tmp[i].name, name)) {
2135 break;
2139 if (i == *num_streams) {
2140 return true;
2143 TALLOC_FREE(tmp[i].name);
2144 if (*num_streams - 1 > i) {
2145 memmove(&tmp[i], &tmp[i+1],
2146 (*num_streams - i - 1) * sizeof(struct stream_struct));
2149 *num_streams -= 1;
2150 return true;
2153 static bool ad_empty_finderinfo(const struct adouble *ad)
2155 int cmp;
2156 char emptybuf[ADEDLEN_FINDERI] = {0};
2157 char *fi = NULL;
2159 fi = ad_get_entry(ad, ADEID_FINDERI);
2160 if (fi == NULL) {
2161 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2162 return false;
2165 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2166 return (cmp == 0);
2169 static bool ai_empty_finderinfo(const AfpInfo *ai)
2171 int cmp;
2172 char emptybuf[ADEDLEN_FINDERI] = {0};
2174 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2175 return (cmp == 0);
2179 * Update btime with btime from Netatalk
2181 static void update_btime(vfs_handle_struct *handle,
2182 struct smb_filename *smb_fname)
2184 uint32_t t;
2185 struct timespec creation_time = {0};
2186 struct adouble *ad;
2187 struct fruit_config_data *config = NULL;
2189 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2190 return);
2192 switch (config->meta) {
2193 case FRUIT_META_STREAM:
2194 return;
2195 case FRUIT_META_NETATALK:
2196 /* Handled below */
2197 break;
2198 default:
2199 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2200 return;
2203 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2204 if (ad == NULL) {
2205 return;
2207 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2208 TALLOC_FREE(ad);
2209 return;
2211 TALLOC_FREE(ad);
2213 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2214 update_stat_ex_create_time(&smb_fname->st, creation_time);
2216 return;
2220 * Map an access mask to a Netatalk single byte byte range lock
2222 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2223 uint32_t access_mask)
2225 off_t offset;
2227 switch (access_mask) {
2228 case FILE_READ_DATA:
2229 offset = AD_FILELOCK_OPEN_RD;
2230 break;
2232 case FILE_WRITE_DATA:
2233 case FILE_APPEND_DATA:
2234 offset = AD_FILELOCK_OPEN_WR;
2235 break;
2237 default:
2238 offset = AD_FILELOCK_OPEN_NONE;
2239 break;
2242 if (fork_type == APPLE_FORK_RSRC) {
2243 if (offset == AD_FILELOCK_OPEN_NONE) {
2244 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2245 } else {
2246 offset += 2;
2250 return offset;
2254 * Map a deny mode to a Netatalk brl
2256 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2257 uint32_t deny_mode)
2259 off_t offset;
2261 switch (deny_mode) {
2262 case DENY_READ:
2263 offset = AD_FILELOCK_DENY_RD;
2264 break;
2266 case DENY_WRITE:
2267 offset = AD_FILELOCK_DENY_WR;
2268 break;
2270 default:
2271 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2274 if (fork_type == APPLE_FORK_RSRC) {
2275 offset += 2;
2278 return offset;
2282 * Call fcntl() with an exclusive F_GETLK request in order to
2283 * determine if there's an exisiting shared lock
2285 * @return true if the requested lock was found or any error occurred
2286 * false if the lock was not found
2288 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2290 bool result;
2291 off_t offset = in_offset;
2292 off_t len = 1;
2293 int type = F_WRLCK;
2294 pid_t pid;
2296 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2297 if (result == false) {
2298 return true;
2301 if (type != F_UNLCK) {
2302 return true;
2305 return false;
2308 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2309 files_struct *fsp,
2310 uint32_t access_mask,
2311 uint32_t deny_mode)
2313 NTSTATUS status = NT_STATUS_OK;
2314 struct byte_range_lock *br_lck = NULL;
2315 bool open_for_reading, open_for_writing, deny_read, deny_write;
2316 off_t off;
2317 bool have_read = false;
2318 int flags;
2320 /* FIXME: hardcoded data fork, add resource fork */
2321 enum apple_fork fork_type = APPLE_FORK_DATA;
2323 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2324 fsp_str_dbg(fsp),
2325 access_mask & FILE_READ_DATA ? "READ" :"-",
2326 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2327 deny_mode & DENY_READ ? "DENY_READ" : "-",
2328 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2330 if (fsp->fh->fd == -1) {
2331 return NT_STATUS_OK;
2334 flags = fcntl(fsp->fh->fd, F_GETFL);
2335 if (flags == -1) {
2336 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2337 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2338 return map_nt_error_from_unix(errno);
2341 if (flags & (O_RDONLY|O_RDWR)) {
2343 * Applying fcntl read locks requires an fd opened for
2344 * reading. This means we won't be applying locks for
2345 * files openend write-only, but what can we do...
2347 have_read = true;
2351 * Check read access and deny read mode
2353 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2354 /* Check access */
2355 open_for_reading = test_netatalk_lock(
2356 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2358 deny_read = test_netatalk_lock(
2359 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2361 DEBUG(10, ("read: %s, deny_write: %s\n",
2362 open_for_reading == true ? "yes" : "no",
2363 deny_read == true ? "yes" : "no"));
2365 if (((access_mask & FILE_READ_DATA) && deny_read)
2366 || ((deny_mode & DENY_READ) && open_for_reading)) {
2367 return NT_STATUS_SHARING_VIOLATION;
2370 /* Set locks */
2371 if ((access_mask & FILE_READ_DATA) && have_read) {
2372 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2373 br_lck = do_lock(
2374 handle->conn->sconn->msg_ctx, fsp,
2375 fsp->op->global->open_persistent_id, 1, off,
2376 READ_LOCK, POSIX_LOCK, false,
2377 &status, NULL);
2379 if (!NT_STATUS_IS_OK(status)) {
2380 return status;
2382 TALLOC_FREE(br_lck);
2385 if ((deny_mode & DENY_READ) && have_read) {
2386 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2387 br_lck = do_lock(
2388 handle->conn->sconn->msg_ctx, fsp,
2389 fsp->op->global->open_persistent_id, 1, off,
2390 READ_LOCK, POSIX_LOCK, false,
2391 &status, NULL);
2393 if (!NT_STATUS_IS_OK(status)) {
2394 return status;
2396 TALLOC_FREE(br_lck);
2401 * Check write access and deny write mode
2403 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2404 /* Check access */
2405 open_for_writing = test_netatalk_lock(
2406 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2408 deny_write = test_netatalk_lock(
2409 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2411 DEBUG(10, ("write: %s, deny_write: %s\n",
2412 open_for_writing == true ? "yes" : "no",
2413 deny_write == true ? "yes" : "no"));
2415 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2416 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2417 return NT_STATUS_SHARING_VIOLATION;
2420 /* Set locks */
2421 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2422 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2423 br_lck = do_lock(
2424 handle->conn->sconn->msg_ctx, fsp,
2425 fsp->op->global->open_persistent_id, 1, off,
2426 READ_LOCK, POSIX_LOCK, false,
2427 &status, NULL);
2429 if (!NT_STATUS_IS_OK(status)) {
2430 return status;
2432 TALLOC_FREE(br_lck);
2435 if ((deny_mode & DENY_WRITE) && have_read) {
2436 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2437 br_lck = do_lock(
2438 handle->conn->sconn->msg_ctx, fsp,
2439 fsp->op->global->open_persistent_id, 1, off,
2440 READ_LOCK, POSIX_LOCK, false,
2441 &status, NULL);
2443 if (!NT_STATUS_IS_OK(status)) {
2444 return status;
2446 TALLOC_FREE(br_lck);
2450 TALLOC_FREE(br_lck);
2452 return status;
2455 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2456 struct smb_request *req,
2457 const struct smb2_create_blobs *in_context_blobs,
2458 struct smb2_create_blobs *out_context_blobs)
2460 struct fruit_config_data *config;
2461 NTSTATUS status;
2462 struct smb2_create_blob *aapl = NULL;
2463 uint32_t cmd;
2464 bool ok;
2465 uint8_t p[16];
2466 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2467 uint64_t req_bitmap, client_caps;
2468 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2469 smb_ucs2_t *model;
2470 size_t modellen;
2472 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2473 return NT_STATUS_UNSUCCESSFUL);
2475 if (!config->use_aapl
2476 || in_context_blobs == NULL
2477 || out_context_blobs == NULL) {
2478 return NT_STATUS_OK;
2481 aapl = smb2_create_blob_find(in_context_blobs,
2482 SMB2_CREATE_TAG_AAPL);
2483 if (aapl == NULL) {
2484 return NT_STATUS_OK;
2487 if (aapl->data.length != 24) {
2488 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2489 (uintmax_t)aapl->data.length));
2490 return NT_STATUS_INVALID_PARAMETER;
2493 cmd = IVAL(aapl->data.data, 0);
2494 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2495 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2496 return NT_STATUS_INVALID_PARAMETER;
2499 req_bitmap = BVAL(aapl->data.data, 8);
2500 client_caps = BVAL(aapl->data.data, 16);
2502 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2503 SIVAL(p, 4, 0);
2504 SBVAL(p, 8, req_bitmap);
2505 ok = data_blob_append(req, &blob, p, 16);
2506 if (!ok) {
2507 return NT_STATUS_UNSUCCESSFUL;
2510 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2511 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2512 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2513 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2514 config->readdir_attr_enabled = true;
2517 if (config->use_copyfile) {
2518 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2519 config->copyfile_enabled = true;
2523 * The client doesn't set the flag, so we can't check
2524 * for it and just set it unconditionally
2526 if (config->unix_info_enabled) {
2527 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2530 SBVAL(p, 0, server_caps);
2531 ok = data_blob_append(req, &blob, p, 8);
2532 if (!ok) {
2533 return NT_STATUS_UNSUCCESSFUL;
2537 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2538 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2539 uint64_t caps = 0;
2541 switch (val) {
2542 case Auto:
2543 break;
2545 case True:
2546 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2547 break;
2549 default:
2550 break;
2553 if (config->time_machine) {
2554 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2557 SBVAL(p, 0, caps);
2559 ok = data_blob_append(req, &blob, p, 8);
2560 if (!ok) {
2561 return NT_STATUS_UNSUCCESSFUL;
2565 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2566 ok = convert_string_talloc(req,
2567 CH_UNIX, CH_UTF16LE,
2568 config->model, strlen(config->model),
2569 &model, &modellen);
2570 if (!ok) {
2571 return NT_STATUS_UNSUCCESSFUL;
2574 SIVAL(p, 0, 0);
2575 SIVAL(p + 4, 0, modellen);
2576 ok = data_blob_append(req, &blob, p, 8);
2577 if (!ok) {
2578 talloc_free(model);
2579 return NT_STATUS_UNSUCCESSFUL;
2582 ok = data_blob_append(req, &blob, model, modellen);
2583 talloc_free(model);
2584 if (!ok) {
2585 return NT_STATUS_UNSUCCESSFUL;
2589 status = smb2_create_blob_add(out_context_blobs,
2590 out_context_blobs,
2591 SMB2_CREATE_TAG_AAPL,
2592 blob);
2593 if (NT_STATUS_IS_OK(status)) {
2594 global_fruit_config.nego_aapl = true;
2595 if (config->aapl_zero_file_id) {
2596 aapl_force_zero_file_id(handle->conn->sconn);
2600 return status;
2603 static bool readdir_attr_meta_finderi_stream(
2604 struct vfs_handle_struct *handle,
2605 const struct smb_filename *smb_fname,
2606 AfpInfo *ai)
2608 struct smb_filename *stream_name = NULL;
2609 files_struct *fsp = NULL;
2610 ssize_t nread;
2611 NTSTATUS status;
2612 int ret;
2613 bool ok;
2614 uint8_t buf[AFP_INFO_SIZE];
2616 stream_name = synthetic_smb_fname(talloc_tos(),
2617 smb_fname->base_name,
2618 AFPINFO_STREAM_NAME,
2619 NULL, smb_fname->flags);
2620 if (stream_name == NULL) {
2621 return false;
2624 ret = SMB_VFS_STAT(handle->conn, stream_name);
2625 if (ret != 0) {
2626 return false;
2629 status = SMB_VFS_CREATE_FILE(
2630 handle->conn, /* conn */
2631 NULL, /* req */
2632 0, /* root_dir_fid */
2633 stream_name, /* fname */
2634 FILE_READ_DATA, /* access_mask */
2635 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2636 FILE_SHARE_DELETE),
2637 FILE_OPEN, /* create_disposition*/
2638 0, /* create_options */
2639 0, /* file_attributes */
2640 INTERNAL_OPEN_ONLY, /* oplock_request */
2641 NULL, /* lease */
2642 0, /* allocation_size */
2643 0, /* private_flags */
2644 NULL, /* sd */
2645 NULL, /* ea_list */
2646 &fsp, /* result */
2647 NULL, /* pinfo */
2648 NULL, NULL); /* create context */
2650 TALLOC_FREE(stream_name);
2652 if (!NT_STATUS_IS_OK(status)) {
2653 return false;
2656 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2657 if (nread != AFP_INFO_SIZE) {
2658 DBG_ERR("short read [%s] [%zd/%d]\n",
2659 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2660 ok = false;
2661 goto fail;
2664 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2665 AFP_FinderSize);
2667 ok = true;
2669 fail:
2670 if (fsp != NULL) {
2671 close_file(NULL, fsp, NORMAL_CLOSE);
2674 return ok;
2677 static bool readdir_attr_meta_finderi_netatalk(
2678 struct vfs_handle_struct *handle,
2679 const struct smb_filename *smb_fname,
2680 AfpInfo *ai)
2682 struct adouble *ad = NULL;
2683 char *p = NULL;
2685 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2686 if (ad == NULL) {
2687 return false;
2690 p = ad_get_entry(ad, ADEID_FINDERI);
2691 if (p == NULL) {
2692 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2693 TALLOC_FREE(ad);
2694 return false;
2697 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2698 TALLOC_FREE(ad);
2699 return true;
2702 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2703 const struct smb_filename *smb_fname,
2704 struct readdir_attr_data *attr_data)
2706 struct fruit_config_data *config = NULL;
2707 uint32_t date_added;
2708 AfpInfo ai = {0};
2709 bool ok;
2711 SMB_VFS_HANDLE_GET_DATA(handle, config,
2712 struct fruit_config_data,
2713 return false);
2715 switch (config->meta) {
2716 case FRUIT_META_NETATALK:
2717 ok = readdir_attr_meta_finderi_netatalk(
2718 handle, smb_fname, &ai);
2719 break;
2721 case FRUIT_META_STREAM:
2722 ok = readdir_attr_meta_finderi_stream(
2723 handle, smb_fname, &ai);
2724 break;
2726 default:
2727 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2728 return false;
2731 if (!ok) {
2732 /* Don't bother with errors, it's likely ENOENT */
2733 return true;
2736 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2737 /* finder_type */
2738 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2739 &ai.afpi_FinderInfo[0], 4);
2741 /* finder_creator */
2742 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2743 &ai.afpi_FinderInfo[4], 4);
2746 /* finder_flags */
2747 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2748 &ai.afpi_FinderInfo[8], 2);
2750 /* finder_ext_flags */
2751 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2752 &ai.afpi_FinderInfo[24], 2);
2754 /* creation date */
2755 date_added = convert_time_t_to_uint32_t(
2756 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2758 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2760 return true;
2763 static uint64_t readdir_attr_rfork_size_adouble(
2764 struct vfs_handle_struct *handle,
2765 const struct smb_filename *smb_fname)
2767 struct adouble *ad = NULL;
2768 uint64_t rfork_size;
2770 ad = ad_get(talloc_tos(), handle, smb_fname,
2771 ADOUBLE_RSRC);
2772 if (ad == NULL) {
2773 return 0;
2776 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2777 TALLOC_FREE(ad);
2779 return rfork_size;
2782 static uint64_t readdir_attr_rfork_size_stream(
2783 struct vfs_handle_struct *handle,
2784 const struct smb_filename *smb_fname)
2786 struct smb_filename *stream_name = NULL;
2787 int ret;
2788 uint64_t rfork_size;
2790 stream_name = synthetic_smb_fname(talloc_tos(),
2791 smb_fname->base_name,
2792 AFPRESOURCE_STREAM_NAME,
2793 NULL, 0);
2794 if (stream_name == NULL) {
2795 return 0;
2798 ret = SMB_VFS_STAT(handle->conn, stream_name);
2799 if (ret != 0) {
2800 TALLOC_FREE(stream_name);
2801 return 0;
2804 rfork_size = stream_name->st.st_ex_size;
2805 TALLOC_FREE(stream_name);
2807 return rfork_size;
2810 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2811 const struct smb_filename *smb_fname)
2813 struct fruit_config_data *config = NULL;
2814 uint64_t rfork_size;
2816 SMB_VFS_HANDLE_GET_DATA(handle, config,
2817 struct fruit_config_data,
2818 return 0);
2820 switch (config->rsrc) {
2821 case FRUIT_RSRC_ADFILE:
2822 case FRUIT_RSRC_XATTR:
2823 rfork_size = readdir_attr_rfork_size_adouble(handle,
2824 smb_fname);
2825 break;
2827 case FRUIT_META_STREAM:
2828 rfork_size = readdir_attr_rfork_size_stream(handle,
2829 smb_fname);
2830 break;
2832 default:
2833 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2834 rfork_size = 0;
2835 break;
2838 return rfork_size;
2841 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2842 const struct smb_filename *smb_fname,
2843 struct readdir_attr_data *attr_data)
2845 NTSTATUS status = NT_STATUS_OK;
2846 struct fruit_config_data *config = NULL;
2847 bool ok;
2849 SMB_VFS_HANDLE_GET_DATA(handle, config,
2850 struct fruit_config_data,
2851 return NT_STATUS_UNSUCCESSFUL);
2854 /* Ensure we return a default value in the creation_date field */
2855 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2858 * Resource fork length
2861 if (config->readdir_attr_rsize) {
2862 uint64_t rfork_size;
2864 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2865 attr_data->attr_data.aapl.rfork_size = rfork_size;
2869 * FinderInfo
2872 if (config->readdir_attr_finder_info) {
2873 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2874 if (!ok) {
2875 status = NT_STATUS_INTERNAL_ERROR;
2879 return status;
2882 /* Search MS NFS style ACE with UNIX mode */
2883 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2884 files_struct *fsp,
2885 const struct security_descriptor *psd,
2886 mode_t *pmode,
2887 bool *pdo_chmod)
2889 uint32_t i;
2890 struct fruit_config_data *config = NULL;
2892 *pdo_chmod = false;
2894 SMB_VFS_HANDLE_GET_DATA(handle, config,
2895 struct fruit_config_data,
2896 return NT_STATUS_UNSUCCESSFUL);
2898 if (!global_fruit_config.nego_aapl) {
2899 return NT_STATUS_OK;
2901 if (psd->dacl == NULL || !config->unix_info_enabled) {
2902 return NT_STATUS_OK;
2905 for (i = 0; i < psd->dacl->num_aces; i++) {
2906 if (dom_sid_compare_domain(
2907 &global_sid_Unix_NFS_Mode,
2908 &psd->dacl->aces[i].trustee) == 0) {
2909 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2910 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2911 *pdo_chmod = true;
2913 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2914 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2915 break;
2919 return NT_STATUS_OK;
2922 /****************************************************************************
2923 * VFS ops
2924 ****************************************************************************/
2926 static int fruit_connect(vfs_handle_struct *handle,
2927 const char *service,
2928 const char *user)
2930 int rc;
2931 char *list = NULL, *newlist = NULL;
2932 struct fruit_config_data *config;
2934 DEBUG(10, ("fruit_connect\n"));
2936 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2937 if (rc < 0) {
2938 return rc;
2941 rc = init_fruit_config(handle);
2942 if (rc != 0) {
2943 return rc;
2946 SMB_VFS_HANDLE_GET_DATA(handle, config,
2947 struct fruit_config_data, return -1);
2949 if (config->veto_appledouble) {
2950 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2952 if (list) {
2953 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2954 newlist = talloc_asprintf(
2955 list,
2956 "%s/" ADOUBLE_NAME_PREFIX "*/",
2957 list);
2958 lp_do_parameter(SNUM(handle->conn),
2959 "veto files",
2960 newlist);
2962 } else {
2963 lp_do_parameter(SNUM(handle->conn),
2964 "veto files",
2965 "/" ADOUBLE_NAME_PREFIX "*/");
2968 TALLOC_FREE(list);
2971 if (config->encoding == FRUIT_ENC_NATIVE) {
2972 lp_do_parameter(SNUM(handle->conn),
2973 "catia:mappings",
2974 fruit_catia_maps);
2977 if (config->time_machine) {
2978 DBG_NOTICE("Enabling durable handles for Time Machine "
2979 "support on [%s]\n", service);
2980 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
2981 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
2982 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
2983 if (!lp_strict_sync(SNUM(handle->conn))) {
2984 DBG_WARNING("Time Machine without strict sync is not "
2985 "recommended!\n");
2987 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
2990 return rc;
2993 static int fruit_open_meta_stream(vfs_handle_struct *handle,
2994 struct smb_filename *smb_fname,
2995 files_struct *fsp,
2996 int flags,
2997 mode_t mode)
2999 AfpInfo *ai = NULL;
3000 char afpinfo_buf[AFP_INFO_SIZE];
3001 ssize_t len, written;
3002 int hostfd = -1;
3003 int rc = -1;
3005 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3006 if (hostfd == -1) {
3007 return -1;
3010 if (!(flags & (O_CREAT | O_TRUNC))) {
3011 return hostfd;
3014 ai = afpinfo_new(talloc_tos());
3015 if (ai == NULL) {
3016 rc = -1;
3017 goto fail;
3020 len = afpinfo_pack(ai, afpinfo_buf);
3021 if (len != AFP_INFO_SIZE) {
3022 rc = -1;
3023 goto fail;
3026 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3027 fsp->fh->fd = hostfd;
3029 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3030 AFP_INFO_SIZE, 0);
3031 fsp->fh->fd = -1;
3032 if (written != AFP_INFO_SIZE) {
3033 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3034 rc = -1;
3035 goto fail;
3038 rc = 0;
3040 fail:
3041 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3043 if (rc != 0) {
3044 int saved_errno = errno;
3045 if (hostfd >= 0) {
3046 fsp->fh->fd = hostfd;
3047 SMB_VFS_NEXT_CLOSE(handle, fsp);
3049 hostfd = -1;
3050 errno = saved_errno;
3052 return hostfd;
3055 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3056 struct smb_filename *smb_fname,
3057 files_struct *fsp,
3058 int flags,
3059 mode_t mode)
3061 int rc;
3062 int fakefd = -1;
3063 struct adouble *ad = NULL;
3064 int fds[2];
3066 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3069 * Return a valid fd, but ensure any attempt to use it returns an error
3070 * (EPIPE). All operations on the smb_fname or the fsp will use path
3071 * based syscalls.
3073 rc = pipe(fds);
3074 if (rc != 0) {
3075 goto exit;
3077 fakefd = fds[0];
3078 close(fds[1]);
3080 if (flags & (O_CREAT | O_TRUNC)) {
3082 * The attribute does not exist or needs to be truncated,
3083 * create an AppleDouble EA
3085 ad = ad_init(fsp, handle, ADOUBLE_META);
3086 if (ad == NULL) {
3087 rc = -1;
3088 goto exit;
3091 rc = ad_set(ad, fsp->fsp_name);
3092 if (rc != 0) {
3093 rc = -1;
3094 goto exit;
3097 TALLOC_FREE(ad);
3100 exit:
3101 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3102 if (rc != 0) {
3103 int saved_errno = errno;
3104 if (fakefd >= 0) {
3105 close(fakefd);
3107 fakefd = -1;
3108 errno = saved_errno;
3110 return fakefd;
3113 static int fruit_open_meta(vfs_handle_struct *handle,
3114 struct smb_filename *smb_fname,
3115 files_struct *fsp, int flags, mode_t mode)
3117 int fd;
3118 struct fruit_config_data *config = NULL;
3119 struct fio *fio = NULL;
3121 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3123 SMB_VFS_HANDLE_GET_DATA(handle, config,
3124 struct fruit_config_data, return -1);
3126 switch (config->meta) {
3127 case FRUIT_META_STREAM:
3128 fd = fruit_open_meta_stream(handle, smb_fname,
3129 fsp, flags, mode);
3130 break;
3132 case FRUIT_META_NETATALK:
3133 fd = fruit_open_meta_netatalk(handle, smb_fname,
3134 fsp, flags, mode);
3135 break;
3137 default:
3138 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3139 return -1;
3142 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3144 if (fd == -1) {
3145 return -1;
3148 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3149 fio->type = ADOUBLE_META;
3150 fio->config = config;
3152 return fd;
3155 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3156 struct smb_filename *smb_fname,
3157 files_struct *fsp,
3158 int flags,
3159 mode_t mode)
3161 int rc = 0;
3162 struct adouble *ad = NULL;
3163 struct smb_filename *smb_fname_base = NULL;
3164 struct fruit_config_data *config = NULL;
3165 int hostfd = -1;
3167 SMB_VFS_HANDLE_GET_DATA(handle, config,
3168 struct fruit_config_data, return -1);
3170 if ((!(flags & O_CREAT)) &&
3171 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3173 /* sorry, but directories don't habe a resource fork */
3174 rc = -1;
3175 goto exit;
3178 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3179 if (rc != 0) {
3180 goto exit;
3183 /* Sanitize flags */
3184 if (flags & O_WRONLY) {
3185 /* We always need read access for the metadata header too */
3186 flags &= ~O_WRONLY;
3187 flags |= O_RDWR;
3190 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3191 flags, mode);
3192 if (hostfd == -1) {
3193 rc = -1;
3194 goto exit;
3197 if (flags & (O_CREAT | O_TRUNC)) {
3198 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3199 if (ad == NULL) {
3200 rc = -1;
3201 goto exit;
3204 fsp->fh->fd = hostfd;
3206 rc = ad_fset(ad, fsp);
3207 fsp->fh->fd = -1;
3208 if (rc != 0) {
3209 rc = -1;
3210 goto exit;
3212 TALLOC_FREE(ad);
3215 exit:
3217 TALLOC_FREE(smb_fname_base);
3219 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3220 if (rc != 0) {
3221 int saved_errno = errno;
3222 if (hostfd >= 0) {
3224 * BUGBUGBUG -- we would need to call
3225 * fd_close_posix here, but we don't have a
3226 * full fsp yet
3228 fsp->fh->fd = hostfd;
3229 SMB_VFS_CLOSE(fsp);
3231 hostfd = -1;
3232 errno = saved_errno;
3234 return hostfd;
3237 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3238 struct smb_filename *smb_fname,
3239 files_struct *fsp,
3240 int flags,
3241 mode_t mode)
3243 #ifdef HAVE_ATTROPEN
3244 int fd = -1;
3246 fd = attropen(smb_fname->base_name,
3247 AFPRESOURCE_EA_NETATALK,
3248 flags,
3249 mode);
3250 if (fd == -1) {
3251 return -1;
3254 return fd;
3256 #else
3257 errno = ENOSYS;
3258 return -1;
3259 #endif
3262 static int fruit_open_rsrc(vfs_handle_struct *handle,
3263 struct smb_filename *smb_fname,
3264 files_struct *fsp, int flags, mode_t mode)
3266 int fd;
3267 struct fruit_config_data *config = NULL;
3268 struct fio *fio = NULL;
3270 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3272 SMB_VFS_HANDLE_GET_DATA(handle, config,
3273 struct fruit_config_data, return -1);
3275 if (((flags & O_ACCMODE) == O_RDONLY)
3276 && (flags & O_CREAT)
3277 && !VALID_STAT(fsp->fsp_name->st))
3280 * This means the stream doesn't exist. macOS SMB server fails
3281 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3282 * 12565 and the test for this combination in
3283 * test_rfork_create().
3285 errno = ENOENT;
3286 return -1;
3289 switch (config->rsrc) {
3290 case FRUIT_RSRC_STREAM:
3291 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3292 break;
3294 case FRUIT_RSRC_ADFILE:
3295 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3296 fsp, flags, mode);
3297 break;
3299 case FRUIT_RSRC_XATTR:
3300 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3301 fsp, flags, mode);
3302 break;
3304 default:
3305 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3306 return -1;
3309 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3311 if (fd == -1) {
3312 return -1;
3315 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3316 fio->type = ADOUBLE_RSRC;
3317 fio->config = config;
3319 return fd;
3322 static int fruit_open(vfs_handle_struct *handle,
3323 struct smb_filename *smb_fname,
3324 files_struct *fsp, int flags, mode_t mode)
3326 int fd;
3328 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3330 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3331 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3334 if (is_afpinfo_stream(smb_fname)) {
3335 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3336 } else if (is_afpresource_stream(smb_fname)) {
3337 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3338 } else {
3339 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3342 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3344 return fd;
3347 static int fruit_rename(struct vfs_handle_struct *handle,
3348 const struct smb_filename *smb_fname_src,
3349 const struct smb_filename *smb_fname_dst)
3351 int rc = -1;
3352 struct fruit_config_data *config = NULL;
3353 struct smb_filename *src_adp_smb_fname = NULL;
3354 struct smb_filename *dst_adp_smb_fname = NULL;
3356 SMB_VFS_HANDLE_GET_DATA(handle, config,
3357 struct fruit_config_data, return -1);
3359 if (!VALID_STAT(smb_fname_src->st)) {
3360 DBG_ERR("Need valid stat for [%s]\n",
3361 smb_fname_str_dbg(smb_fname_src));
3362 return -1;
3365 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3366 if (rc != 0) {
3367 return -1;
3370 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3371 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3373 return 0;
3376 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3377 if (rc != 0) {
3378 goto done;
3381 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3382 if (rc != 0) {
3383 goto done;
3386 DBG_DEBUG("%s -> %s\n",
3387 smb_fname_str_dbg(src_adp_smb_fname),
3388 smb_fname_str_dbg(dst_adp_smb_fname));
3390 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3391 if (errno == ENOENT) {
3392 rc = 0;
3395 done:
3396 TALLOC_FREE(src_adp_smb_fname);
3397 TALLOC_FREE(dst_adp_smb_fname);
3398 return rc;
3401 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3402 const struct smb_filename *smb_fname)
3404 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3407 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3408 const struct smb_filename *smb_fname)
3410 return SMB_VFS_REMOVEXATTR(handle->conn,
3411 smb_fname,
3412 AFPINFO_EA_NETATALK);
3415 static int fruit_unlink_meta(vfs_handle_struct *handle,
3416 const struct smb_filename *smb_fname)
3418 struct fruit_config_data *config = NULL;
3419 int rc;
3421 SMB_VFS_HANDLE_GET_DATA(handle, config,
3422 struct fruit_config_data, return -1);
3424 switch (config->meta) {
3425 case FRUIT_META_STREAM:
3426 rc = fruit_unlink_meta_stream(handle, smb_fname);
3427 break;
3429 case FRUIT_META_NETATALK:
3430 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3431 break;
3433 default:
3434 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3435 return -1;
3438 return rc;
3441 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3442 const struct smb_filename *smb_fname,
3443 bool force_unlink)
3445 int ret;
3447 if (!force_unlink) {
3448 struct smb_filename *smb_fname_cp = NULL;
3449 off_t size;
3451 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3452 if (smb_fname_cp == NULL) {
3453 return -1;
3457 * 0 byte resource fork streams are not listed by
3458 * vfs_streaminfo, as a result stream cleanup/deletion of file
3459 * deletion doesn't remove the resourcefork stream.
3462 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3463 if (ret != 0) {
3464 TALLOC_FREE(smb_fname_cp);
3465 DBG_ERR("stat [%s] failed [%s]\n",
3466 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3467 return -1;
3470 size = smb_fname_cp->st.st_ex_size;
3471 TALLOC_FREE(smb_fname_cp);
3473 if (size > 0) {
3474 /* OS X ignores resource fork stream delete requests */
3475 return 0;
3479 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3480 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3481 ret = 0;
3484 return ret;
3487 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3488 const struct smb_filename *smb_fname,
3489 bool force_unlink)
3491 int rc;
3492 struct adouble *ad = NULL;
3493 struct smb_filename *adp_smb_fname = NULL;
3495 if (!force_unlink) {
3496 ad = ad_get(talloc_tos(), handle, smb_fname,
3497 ADOUBLE_RSRC);
3498 if (ad == NULL) {
3499 errno = ENOENT;
3500 return -1;
3505 * 0 byte resource fork streams are not listed by
3506 * vfs_streaminfo, as a result stream cleanup/deletion of file
3507 * deletion doesn't remove the resourcefork stream.
3510 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3511 /* OS X ignores resource fork stream delete requests */
3512 TALLOC_FREE(ad);
3513 return 0;
3516 TALLOC_FREE(ad);
3519 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3520 if (rc != 0) {
3521 return -1;
3524 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3525 TALLOC_FREE(adp_smb_fname);
3526 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3527 rc = 0;
3530 return rc;
3533 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3534 const struct smb_filename *smb_fname,
3535 bool force_unlink)
3538 * OS X ignores resource fork stream delete requests, so nothing to do
3539 * here. Removing the file will remove the xattr anyway, so we don't
3540 * have to take care of removing 0 byte resource forks that could be
3541 * left behind.
3543 return 0;
3546 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3547 const struct smb_filename *smb_fname,
3548 bool force_unlink)
3550 struct fruit_config_data *config = NULL;
3551 int rc;
3553 SMB_VFS_HANDLE_GET_DATA(handle, config,
3554 struct fruit_config_data, return -1);
3556 switch (config->rsrc) {
3557 case FRUIT_RSRC_STREAM:
3558 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3559 break;
3561 case FRUIT_RSRC_ADFILE:
3562 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3563 break;
3565 case FRUIT_RSRC_XATTR:
3566 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3567 break;
3569 default:
3570 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3571 return -1;
3574 return rc;
3577 static int fruit_unlink(vfs_handle_struct *handle,
3578 const struct smb_filename *smb_fname)
3580 int rc;
3581 struct fruit_config_data *config = NULL;
3582 struct smb_filename *rsrc_smb_fname = NULL;
3584 SMB_VFS_HANDLE_GET_DATA(handle, config,
3585 struct fruit_config_data, return -1);
3587 if (is_afpinfo_stream(smb_fname)) {
3588 return fruit_unlink_meta(handle, smb_fname);
3589 } else if (is_afpresource_stream(smb_fname)) {
3590 return fruit_unlink_rsrc(handle, smb_fname, false);
3591 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3592 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3596 * A request to delete the base file. Because 0 byte resource
3597 * fork streams are not listed by fruit_streaminfo,
3598 * delete_all_streams() can't remove 0 byte resource fork
3599 * streams, so we have to cleanup this here.
3601 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3602 smb_fname->base_name,
3603 AFPRESOURCE_STREAM_NAME,
3604 NULL,
3605 smb_fname->flags);
3606 if (rsrc_smb_fname == NULL) {
3607 return -1;
3610 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3611 if ((rc != 0) && (errno != ENOENT)) {
3612 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3613 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3614 TALLOC_FREE(rsrc_smb_fname);
3615 return -1;
3617 TALLOC_FREE(rsrc_smb_fname);
3619 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3622 static int fruit_chmod(vfs_handle_struct *handle,
3623 const struct smb_filename *smb_fname,
3624 mode_t mode)
3626 int rc = -1;
3627 struct fruit_config_data *config = NULL;
3628 struct smb_filename *smb_fname_adp = NULL;
3630 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3631 if (rc != 0) {
3632 return rc;
3635 SMB_VFS_HANDLE_GET_DATA(handle, config,
3636 struct fruit_config_data, return -1);
3638 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3639 return 0;
3642 if (!VALID_STAT(smb_fname->st)) {
3643 return 0;
3646 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3647 return 0;
3650 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3651 if (rc != 0) {
3652 return -1;
3655 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3657 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3658 if (errno == ENOENT) {
3659 rc = 0;
3662 TALLOC_FREE(smb_fname_adp);
3663 return rc;
3666 static int fruit_chown(vfs_handle_struct *handle,
3667 const struct smb_filename *smb_fname,
3668 uid_t uid,
3669 gid_t gid)
3671 int rc = -1;
3672 struct fruit_config_data *config = NULL;
3673 struct smb_filename *adp_smb_fname = NULL;
3675 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3676 if (rc != 0) {
3677 return rc;
3680 SMB_VFS_HANDLE_GET_DATA(handle, config,
3681 struct fruit_config_data, return -1);
3683 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3684 return 0;
3687 if (!VALID_STAT(smb_fname->st)) {
3688 return 0;
3691 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3692 return 0;
3695 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3696 if (rc != 0) {
3697 goto done;
3700 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3702 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3703 if (errno == ENOENT) {
3704 rc = 0;
3707 done:
3708 TALLOC_FREE(adp_smb_fname);
3709 return rc;
3712 static int fruit_rmdir(struct vfs_handle_struct *handle,
3713 const struct smb_filename *smb_fname)
3715 DIR *dh = NULL;
3716 struct dirent *de;
3717 struct fruit_config_data *config;
3719 SMB_VFS_HANDLE_GET_DATA(handle, config,
3720 struct fruit_config_data, return -1);
3722 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3723 goto exit_rmdir;
3727 * Due to there is no way to change bDeleteVetoFiles variable
3728 * from this module, need to clean up ourselves
3731 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3732 if (dh == NULL) {
3733 goto exit_rmdir;
3736 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3737 int match;
3738 struct adouble *ad = NULL;
3739 char *p = NULL;
3740 struct smb_filename *ad_smb_fname = NULL;
3741 int ret;
3743 match = strncmp(de->d_name,
3744 ADOUBLE_NAME_PREFIX,
3745 strlen(ADOUBLE_NAME_PREFIX));
3746 if (match != 0) {
3747 continue;
3750 p = talloc_asprintf(talloc_tos(), "%s/%s",
3751 smb_fname->base_name, de->d_name);
3752 if (p == NULL) {
3753 DBG_ERR("talloc_asprintf failed\n");
3754 return -1;
3757 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3758 NULL, NULL,
3759 smb_fname->flags);
3760 TALLOC_FREE(p);
3761 if (ad_smb_fname == NULL) {
3762 DBG_ERR("synthetic_smb_fname failed\n");
3763 return -1;
3767 * Check whether it's a valid AppleDouble file, if
3768 * yes, delete it, ignore it otherwise.
3770 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3771 if (ad == NULL) {
3772 TALLOC_FREE(ad_smb_fname);
3773 TALLOC_FREE(p);
3774 continue;
3776 TALLOC_FREE(ad);
3778 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3779 TALLOC_FREE(ad_smb_fname);
3780 if (ret != 0) {
3781 DBG_ERR("Deleting [%s] failed\n",
3782 smb_fname_str_dbg(ad_smb_fname));
3786 exit_rmdir:
3787 if (dh) {
3788 SMB_VFS_CLOSEDIR(handle->conn, dh);
3790 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3793 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3794 files_struct *fsp, void *data,
3795 size_t n, off_t offset)
3797 ssize_t nread;
3798 int ret;
3800 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3802 if (nread == n) {
3803 return nread;
3806 DBG_ERR("Removing [%s] after short read [%zd]\n",
3807 fsp_str_dbg(fsp), nread);
3809 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3810 if (ret != 0) {
3811 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3812 return -1;
3815 errno = EINVAL;
3816 return -1;
3819 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3820 files_struct *fsp, void *data,
3821 size_t n, off_t offset)
3823 AfpInfo *ai = NULL;
3824 struct adouble *ad = NULL;
3825 char afpinfo_buf[AFP_INFO_SIZE];
3826 char *p = NULL;
3827 ssize_t nread;
3829 ai = afpinfo_new(talloc_tos());
3830 if (ai == NULL) {
3831 return -1;
3834 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3835 if (ad == NULL) {
3836 nread = -1;
3837 goto fail;
3840 p = ad_get_entry(ad, ADEID_FINDERI);
3841 if (p == NULL) {
3842 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3843 nread = -1;
3844 goto fail;
3847 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3849 nread = afpinfo_pack(ai, afpinfo_buf);
3850 if (nread != AFP_INFO_SIZE) {
3851 nread = -1;
3852 goto fail;
3855 memcpy(data, afpinfo_buf, n);
3856 nread = n;
3858 fail:
3859 TALLOC_FREE(ai);
3860 return nread;
3863 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3864 files_struct *fsp, void *data,
3865 size_t n, off_t offset)
3867 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3868 ssize_t nread;
3869 ssize_t to_return;
3872 * OS X has a off-by-1 error in the offset calculation, so we're
3873 * bug compatible here. It won't hurt, as any relevant real
3874 * world read requests from the AFP_AfpInfo stream will be
3875 * offset=0 n=60. offset is ignored anyway, see below.
3877 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
3878 return 0;
3881 /* Yes, macOS always reads from offset 0 */
3882 offset = 0;
3883 to_return = MIN(n, AFP_INFO_SIZE);
3885 switch (fio->config->meta) {
3886 case FRUIT_META_STREAM:
3887 nread = fruit_pread_meta_stream(handle, fsp, data,
3888 to_return, offset);
3889 break;
3891 case FRUIT_META_NETATALK:
3892 nread = fruit_pread_meta_adouble(handle, fsp, data,
3893 to_return, offset);
3894 break;
3896 default:
3897 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
3898 return -1;
3901 return nread;
3904 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
3905 files_struct *fsp, void *data,
3906 size_t n, off_t offset)
3908 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3911 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
3912 files_struct *fsp, void *data,
3913 size_t n, off_t offset)
3915 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3918 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
3919 files_struct *fsp, void *data,
3920 size_t n, off_t offset)
3922 struct adouble *ad = NULL;
3923 ssize_t nread;
3925 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
3926 if (ad == NULL) {
3927 return -1;
3930 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
3931 offset + ad_getentryoff(ad, ADEID_RFORK));
3933 TALLOC_FREE(ad);
3934 return nread;
3937 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
3938 files_struct *fsp, void *data,
3939 size_t n, off_t offset)
3941 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3942 ssize_t nread;
3944 switch (fio->config->rsrc) {
3945 case FRUIT_RSRC_STREAM:
3946 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
3947 break;
3949 case FRUIT_RSRC_ADFILE:
3950 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
3951 break;
3953 case FRUIT_RSRC_XATTR:
3954 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
3955 break;
3957 default:
3958 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
3959 return -1;
3962 return nread;
3965 static ssize_t fruit_pread(vfs_handle_struct *handle,
3966 files_struct *fsp, void *data,
3967 size_t n, off_t offset)
3969 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3970 ssize_t nread;
3972 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
3973 fsp_str_dbg(fsp), (intmax_t)offset, n);
3975 if (fio == NULL) {
3976 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3979 if (fio->type == ADOUBLE_META) {
3980 nread = fruit_pread_meta(handle, fsp, data, n, offset);
3981 } else {
3982 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
3985 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
3986 return nread;
3989 static bool fruit_must_handle_aio_stream(struct fio *fio)
3991 if (fio == NULL) {
3992 return false;
3995 if ((fio->type == ADOUBLE_META) &&
3996 (fio->config->meta == FRUIT_META_NETATALK))
3998 return true;
4001 if ((fio->type == ADOUBLE_RSRC) &&
4002 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4004 return true;
4007 return false;
4010 struct fruit_pread_state {
4011 ssize_t nread;
4012 struct vfs_aio_state vfs_aio_state;
4015 static void fruit_pread_done(struct tevent_req *subreq);
4017 static struct tevent_req *fruit_pread_send(
4018 struct vfs_handle_struct *handle,
4019 TALLOC_CTX *mem_ctx,
4020 struct tevent_context *ev,
4021 struct files_struct *fsp,
4022 void *data,
4023 size_t n, off_t offset)
4025 struct tevent_req *req = NULL;
4026 struct tevent_req *subreq = NULL;
4027 struct fruit_pread_state *state = NULL;
4028 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4030 req = tevent_req_create(mem_ctx, &state,
4031 struct fruit_pread_state);
4032 if (req == NULL) {
4033 return NULL;
4036 if (fruit_must_handle_aio_stream(fio)) {
4037 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4038 if (state->nread != n) {
4039 if (state->nread != -1) {
4040 errno = EIO;
4042 tevent_req_error(req, errno);
4043 return tevent_req_post(req, ev);
4045 tevent_req_done(req);
4046 return tevent_req_post(req, ev);
4049 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4050 data, n, offset);
4051 if (tevent_req_nomem(req, subreq)) {
4052 return tevent_req_post(req, ev);
4054 tevent_req_set_callback(subreq, fruit_pread_done, req);
4055 return req;
4058 static void fruit_pread_done(struct tevent_req *subreq)
4060 struct tevent_req *req = tevent_req_callback_data(
4061 subreq, struct tevent_req);
4062 struct fruit_pread_state *state = tevent_req_data(
4063 req, struct fruit_pread_state);
4065 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4066 TALLOC_FREE(subreq);
4068 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4069 return;
4071 tevent_req_done(req);
4074 static ssize_t fruit_pread_recv(struct tevent_req *req,
4075 struct vfs_aio_state *vfs_aio_state)
4077 struct fruit_pread_state *state = tevent_req_data(
4078 req, struct fruit_pread_state);
4080 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4081 return -1;
4084 *vfs_aio_state = state->vfs_aio_state;
4085 return state->nread;
4088 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4089 files_struct *fsp, const void *data,
4090 size_t n, off_t offset)
4092 AfpInfo *ai = NULL;
4093 int ret;
4095 ai = afpinfo_unpack(talloc_tos(), data);
4096 if (ai == NULL) {
4097 return -1;
4100 if (ai_empty_finderinfo(ai)) {
4101 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4102 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
4103 DBG_ERR("Can't delete metadata for %s: %s\n",
4104 fsp_str_dbg(fsp), strerror(errno));
4105 TALLOC_FREE(ai);
4106 return -1;
4109 return n;
4112 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4115 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4116 files_struct *fsp, const void *data,
4117 size_t n, off_t offset)
4119 struct adouble *ad = NULL;
4120 AfpInfo *ai = NULL;
4121 char *p = NULL;
4122 int ret;
4124 ai = afpinfo_unpack(talloc_tos(), data);
4125 if (ai == NULL) {
4126 return -1;
4129 if (ai_empty_finderinfo(ai)) {
4130 ret = SMB_VFS_REMOVEXATTR(handle->conn,
4131 fsp->fsp_name,
4132 AFPINFO_EA_NETATALK);
4134 if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
4135 DBG_ERR("Can't delete metadata for %s: %s\n",
4136 fsp_str_dbg(fsp), strerror(errno));
4137 return -1;
4140 return n;
4143 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4144 if (ad == NULL) {
4145 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4146 if (ad == NULL) {
4147 return -1;
4150 p = ad_get_entry(ad, ADEID_FINDERI);
4151 if (p == NULL) {
4152 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4153 TALLOC_FREE(ad);
4154 return -1;
4157 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4159 ret = ad_fset(ad, fsp);
4160 if (ret != 0) {
4161 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4162 TALLOC_FREE(ad);
4163 return -1;
4166 TALLOC_FREE(ad);
4167 return n;
4170 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4171 files_struct *fsp, const void *data,
4172 size_t n, off_t offset)
4174 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4175 ssize_t nwritten;
4178 * Writing an all 0 blob to the metadata stream
4179 * results in the stream being removed on a macOS
4180 * server. This ensures we behave the same and it
4181 * verified by the "delete AFP_AfpInfo by writing all
4182 * 0" test.
4184 if (n != AFP_INFO_SIZE || offset != 0) {
4185 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4186 (intmax_t)offset, (intmax_t)n);
4187 return -1;
4190 switch (fio->config->meta) {
4191 case FRUIT_META_STREAM:
4192 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4193 n, offset);
4194 break;
4196 case FRUIT_META_NETATALK:
4197 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4198 n, offset);
4199 break;
4201 default:
4202 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4203 return -1;
4206 return nwritten;
4209 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4210 files_struct *fsp, const void *data,
4211 size_t n, off_t offset)
4213 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4216 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4217 files_struct *fsp, const void *data,
4218 size_t n, off_t offset)
4220 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4223 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4224 files_struct *fsp, const void *data,
4225 size_t n, off_t offset)
4227 struct adouble *ad = NULL;
4228 ssize_t nwritten;
4229 int ret;
4231 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4232 if (ad == NULL) {
4233 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4234 return -1;
4237 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4238 offset + ad_getentryoff(ad, ADEID_RFORK));
4239 if (nwritten != n) {
4240 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4241 fsp_str_dbg(fsp), nwritten, n);
4242 TALLOC_FREE(ad);
4243 return -1;
4246 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4247 ad_setentrylen(ad, ADEID_RFORK, n + offset);
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;
4256 TALLOC_FREE(ad);
4257 return n;
4260 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4261 files_struct *fsp, const void *data,
4262 size_t n, off_t offset)
4264 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4265 ssize_t nwritten;
4267 switch (fio->config->rsrc) {
4268 case FRUIT_RSRC_STREAM:
4269 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4270 break;
4272 case FRUIT_RSRC_ADFILE:
4273 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4274 break;
4276 case FRUIT_RSRC_XATTR:
4277 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4278 break;
4280 default:
4281 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4282 return -1;
4285 return nwritten;
4288 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4289 files_struct *fsp, const void *data,
4290 size_t n, off_t offset)
4292 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4293 ssize_t nwritten;
4295 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4296 fsp_str_dbg(fsp), (intmax_t)offset, n);
4298 if (fio == NULL) {
4299 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4302 if (fio->type == ADOUBLE_META) {
4303 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4304 } else {
4305 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4308 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4309 return nwritten;
4312 struct fruit_pwrite_state {
4313 ssize_t nwritten;
4314 struct vfs_aio_state vfs_aio_state;
4317 static void fruit_pwrite_done(struct tevent_req *subreq);
4319 static struct tevent_req *fruit_pwrite_send(
4320 struct vfs_handle_struct *handle,
4321 TALLOC_CTX *mem_ctx,
4322 struct tevent_context *ev,
4323 struct files_struct *fsp,
4324 const void *data,
4325 size_t n, off_t offset)
4327 struct tevent_req *req = NULL;
4328 struct tevent_req *subreq = NULL;
4329 struct fruit_pwrite_state *state = NULL;
4330 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4332 req = tevent_req_create(mem_ctx, &state,
4333 struct fruit_pwrite_state);
4334 if (req == NULL) {
4335 return NULL;
4338 if (fruit_must_handle_aio_stream(fio)) {
4339 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4340 if (state->nwritten != n) {
4341 if (state->nwritten != -1) {
4342 errno = EIO;
4344 tevent_req_error(req, errno);
4345 return tevent_req_post(req, ev);
4347 tevent_req_done(req);
4348 return tevent_req_post(req, ev);
4351 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4352 data, n, offset);
4353 if (tevent_req_nomem(req, subreq)) {
4354 return tevent_req_post(req, ev);
4356 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4357 return req;
4360 static void fruit_pwrite_done(struct tevent_req *subreq)
4362 struct tevent_req *req = tevent_req_callback_data(
4363 subreq, struct tevent_req);
4364 struct fruit_pwrite_state *state = tevent_req_data(
4365 req, struct fruit_pwrite_state);
4367 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4368 TALLOC_FREE(subreq);
4370 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4371 return;
4373 tevent_req_done(req);
4376 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4377 struct vfs_aio_state *vfs_aio_state)
4379 struct fruit_pwrite_state *state = tevent_req_data(
4380 req, struct fruit_pwrite_state);
4382 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4383 return -1;
4386 *vfs_aio_state = state->vfs_aio_state;
4387 return state->nwritten;
4391 * Helper to stat/lstat the base file of an smb_fname.
4393 static int fruit_stat_base(vfs_handle_struct *handle,
4394 struct smb_filename *smb_fname,
4395 bool follow_links)
4397 char *tmp_stream_name;
4398 int rc;
4400 tmp_stream_name = smb_fname->stream_name;
4401 smb_fname->stream_name = NULL;
4402 if (follow_links) {
4403 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4404 } else {
4405 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4407 smb_fname->stream_name = tmp_stream_name;
4408 return rc;
4411 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4412 struct smb_filename *smb_fname,
4413 bool follow_links)
4415 int ret;
4417 if (follow_links) {
4418 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4419 } else {
4420 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4423 return ret;
4426 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4427 struct smb_filename *smb_fname,
4428 bool follow_links)
4430 struct adouble *ad = NULL;
4432 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4433 if (ad == NULL) {
4434 DBG_INFO("fruit_stat_meta %s: %s\n",
4435 smb_fname_str_dbg(smb_fname), strerror(errno));
4436 errno = ENOENT;
4437 return -1;
4439 TALLOC_FREE(ad);
4441 /* Populate the stat struct with info from the base file. */
4442 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4443 return -1;
4445 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4446 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4447 smb_fname->stream_name);
4448 return 0;
4451 static int fruit_stat_meta(vfs_handle_struct *handle,
4452 struct smb_filename *smb_fname,
4453 bool follow_links)
4455 struct fruit_config_data *config = NULL;
4456 int ret;
4458 SMB_VFS_HANDLE_GET_DATA(handle, config,
4459 struct fruit_config_data, return -1);
4461 switch (config->meta) {
4462 case FRUIT_META_STREAM:
4463 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4464 break;
4466 case FRUIT_META_NETATALK:
4467 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4468 break;
4470 default:
4471 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4472 return -1;
4475 return ret;
4478 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4479 struct smb_filename *smb_fname,
4480 bool follow_links)
4482 struct adouble *ad = NULL;
4483 int ret;
4485 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4486 if (ad == NULL) {
4487 errno = ENOENT;
4488 return -1;
4491 /* Populate the stat struct with info from the base file. */
4492 ret = fruit_stat_base(handle, smb_fname, follow_links);
4493 if (ret != 0) {
4494 TALLOC_FREE(ad);
4495 return -1;
4498 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4499 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4500 smb_fname->stream_name);
4501 TALLOC_FREE(ad);
4502 return 0;
4505 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4506 struct smb_filename *smb_fname,
4507 bool follow_links)
4509 int ret;
4511 if (follow_links) {
4512 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4513 } else {
4514 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4517 return ret;
4520 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4521 struct smb_filename *smb_fname,
4522 bool follow_links)
4524 #ifdef HAVE_ATTROPEN
4525 int ret;
4526 int fd = -1;
4528 /* Populate the stat struct with info from the base file. */
4529 ret = fruit_stat_base(handle, smb_fname, follow_links);
4530 if (ret != 0) {
4531 return -1;
4534 fd = attropen(smb_fname->base_name,
4535 AFPRESOURCE_EA_NETATALK,
4536 O_RDONLY);
4537 if (fd == -1) {
4538 return 0;
4541 ret = sys_fstat(fd, &smb_fname->st, false);
4542 if (ret != 0) {
4543 close(fd);
4544 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4545 AFPRESOURCE_EA_NETATALK);
4546 return -1;
4548 close(fd);
4549 fd = -1;
4551 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4552 smb_fname->stream_name);
4554 return ret;
4556 #else
4557 errno = ENOSYS;
4558 return -1;
4559 #endif
4562 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4563 struct smb_filename *smb_fname,
4564 bool follow_links)
4566 struct fruit_config_data *config = NULL;
4567 int ret;
4569 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4571 SMB_VFS_HANDLE_GET_DATA(handle, config,
4572 struct fruit_config_data, return -1);
4574 switch (config->rsrc) {
4575 case FRUIT_RSRC_STREAM:
4576 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4577 break;
4579 case FRUIT_RSRC_XATTR:
4580 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4581 break;
4583 case FRUIT_RSRC_ADFILE:
4584 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4585 break;
4587 default:
4588 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4589 return -1;
4592 return ret;
4595 static int fruit_stat(vfs_handle_struct *handle,
4596 struct smb_filename *smb_fname)
4598 int rc = -1;
4600 DEBUG(10, ("fruit_stat called for %s\n",
4601 smb_fname_str_dbg(smb_fname)));
4603 if (!is_ntfs_stream_smb_fname(smb_fname)
4604 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4605 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4606 if (rc == 0) {
4607 update_btime(handle, smb_fname);
4609 return rc;
4613 * Note if lp_posix_paths() is true, we can never
4614 * get here as is_ntfs_stream_smb_fname() is
4615 * always false. So we never need worry about
4616 * not following links here.
4619 if (is_afpinfo_stream(smb_fname)) {
4620 rc = fruit_stat_meta(handle, smb_fname, true);
4621 } else if (is_afpresource_stream(smb_fname)) {
4622 rc = fruit_stat_rsrc(handle, smb_fname, true);
4623 } else {
4624 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4627 if (rc == 0) {
4628 update_btime(handle, smb_fname);
4629 smb_fname->st.st_ex_mode &= ~S_IFMT;
4630 smb_fname->st.st_ex_mode |= S_IFREG;
4631 smb_fname->st.st_ex_blocks =
4632 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4634 return rc;
4637 static int fruit_lstat(vfs_handle_struct *handle,
4638 struct smb_filename *smb_fname)
4640 int rc = -1;
4642 DEBUG(10, ("fruit_lstat called for %s\n",
4643 smb_fname_str_dbg(smb_fname)));
4645 if (!is_ntfs_stream_smb_fname(smb_fname)
4646 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4647 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4648 if (rc == 0) {
4649 update_btime(handle, smb_fname);
4651 return rc;
4654 if (is_afpinfo_stream(smb_fname)) {
4655 rc = fruit_stat_meta(handle, smb_fname, false);
4656 } else if (is_afpresource_stream(smb_fname)) {
4657 rc = fruit_stat_rsrc(handle, smb_fname, false);
4658 } else {
4659 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4662 if (rc == 0) {
4663 update_btime(handle, smb_fname);
4664 smb_fname->st.st_ex_mode &= ~S_IFMT;
4665 smb_fname->st.st_ex_mode |= S_IFREG;
4666 smb_fname->st.st_ex_blocks =
4667 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4669 return rc;
4672 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4673 files_struct *fsp,
4674 SMB_STRUCT_STAT *sbuf)
4676 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4679 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4680 files_struct *fsp,
4681 SMB_STRUCT_STAT *sbuf)
4683 int ret;
4685 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4686 if (ret != 0) {
4687 return -1;
4690 *sbuf = fsp->base_fsp->fsp_name->st;
4691 sbuf->st_ex_size = AFP_INFO_SIZE;
4692 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4694 return 0;
4697 static int fruit_fstat_meta(vfs_handle_struct *handle,
4698 files_struct *fsp,
4699 SMB_STRUCT_STAT *sbuf,
4700 struct fio *fio)
4702 int ret;
4704 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4706 switch (fio->config->meta) {
4707 case FRUIT_META_STREAM:
4708 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4709 break;
4711 case FRUIT_META_NETATALK:
4712 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4713 break;
4715 default:
4716 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4717 return -1;
4720 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4721 return ret;
4724 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4725 files_struct *fsp,
4726 SMB_STRUCT_STAT *sbuf)
4728 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4731 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4732 files_struct *fsp,
4733 SMB_STRUCT_STAT *sbuf)
4735 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4738 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4739 files_struct *fsp,
4740 SMB_STRUCT_STAT *sbuf)
4742 struct adouble *ad = NULL;
4743 int ret;
4745 /* Populate the stat struct with info from the base file. */
4746 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4747 if (ret == -1) {
4748 return -1;
4751 ad = ad_get(talloc_tos(), handle,
4752 fsp->base_fsp->fsp_name,
4753 ADOUBLE_RSRC);
4754 if (ad == NULL) {
4755 DBG_ERR("ad_get [%s] failed [%s]\n",
4756 fsp_str_dbg(fsp), strerror(errno));
4757 return -1;
4760 *sbuf = fsp->base_fsp->fsp_name->st;
4761 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4762 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4764 TALLOC_FREE(ad);
4765 return 0;
4768 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4769 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4771 int ret;
4773 switch (fio->config->rsrc) {
4774 case FRUIT_RSRC_STREAM:
4775 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4776 break;
4778 case FRUIT_RSRC_ADFILE:
4779 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4780 break;
4782 case FRUIT_RSRC_XATTR:
4783 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4784 break;
4786 default:
4787 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4788 return -1;
4791 return ret;
4794 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4795 SMB_STRUCT_STAT *sbuf)
4797 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4798 int rc;
4800 if (fio == NULL) {
4801 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4804 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4806 if (fio->type == ADOUBLE_META) {
4807 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4808 } else {
4809 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4812 if (rc == 0) {
4813 sbuf->st_ex_mode &= ~S_IFMT;
4814 sbuf->st_ex_mode |= S_IFREG;
4815 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4818 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4819 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4820 return rc;
4823 static NTSTATUS fruit_streaminfo_meta_stream(
4824 vfs_handle_struct *handle,
4825 struct files_struct *fsp,
4826 const struct smb_filename *smb_fname,
4827 TALLOC_CTX *mem_ctx,
4828 unsigned int *pnum_streams,
4829 struct stream_struct **pstreams)
4831 struct stream_struct *stream = *pstreams;
4832 unsigned int num_streams = *pnum_streams;
4833 struct smb_filename *sname = NULL;
4834 int i;
4835 int ret;
4836 bool ok;
4838 for (i = 0; i < num_streams; i++) {
4839 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4840 break;
4844 if (i == num_streams) {
4845 return NT_STATUS_OK;
4848 if (stream[i].size == AFP_INFO_SIZE) {
4849 return NT_STATUS_OK;
4852 DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
4853 "from [%s]\n", (intmax_t)stream[i].size,
4854 smb_fname_str_dbg(smb_fname));
4856 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4857 if (!ok) {
4858 return NT_STATUS_INTERNAL_ERROR;
4861 sname = synthetic_smb_fname(talloc_tos(),
4862 smb_fname->base_name,
4863 AFPINFO_STREAM_NAME,
4864 NULL, 0);
4865 if (sname == NULL) {
4866 return NT_STATUS_NO_MEMORY;
4869 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4870 TALLOC_FREE(sname);
4871 if (ret != 0) {
4872 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4873 return map_nt_error_from_unix(errno);
4876 return NT_STATUS_OK;
4879 static NTSTATUS fruit_streaminfo_meta_netatalk(
4880 vfs_handle_struct *handle,
4881 struct files_struct *fsp,
4882 const struct smb_filename *smb_fname,
4883 TALLOC_CTX *mem_ctx,
4884 unsigned int *pnum_streams,
4885 struct stream_struct **pstreams)
4887 struct stream_struct *stream = *pstreams;
4888 unsigned int num_streams = *pnum_streams;
4889 struct adouble *ad = NULL;
4890 bool is_fi_empty;
4891 int i;
4892 bool ok;
4894 /* Remove the Netatalk xattr from the list */
4895 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4896 ":" NETATALK_META_XATTR ":$DATA");
4897 if (!ok) {
4898 return NT_STATUS_NO_MEMORY;
4902 * Check if there's a AFPINFO_STREAM from the VFS streams
4903 * backend and if yes, remove it from the list
4905 for (i = 0; i < num_streams; i++) {
4906 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
4907 break;
4911 if (i < num_streams) {
4912 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
4913 smb_fname_str_dbg(smb_fname));
4915 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
4916 AFPINFO_STREAM);
4917 if (!ok) {
4918 return NT_STATUS_INTERNAL_ERROR;
4922 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4923 if (ad == NULL) {
4924 return NT_STATUS_OK;
4927 is_fi_empty = ad_empty_finderinfo(ad);
4928 TALLOC_FREE(ad);
4930 if (is_fi_empty) {
4931 return NT_STATUS_OK;
4934 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
4935 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
4936 smb_roundup(handle->conn, AFP_INFO_SIZE));
4937 if (!ok) {
4938 return NT_STATUS_NO_MEMORY;
4941 return NT_STATUS_OK;
4944 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
4945 struct files_struct *fsp,
4946 const struct smb_filename *smb_fname,
4947 TALLOC_CTX *mem_ctx,
4948 unsigned int *pnum_streams,
4949 struct stream_struct **pstreams)
4951 struct fruit_config_data *config = NULL;
4952 NTSTATUS status;
4954 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
4955 return NT_STATUS_INTERNAL_ERROR);
4957 switch (config->meta) {
4958 case FRUIT_META_NETATALK:
4959 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
4960 mem_ctx, pnum_streams,
4961 pstreams);
4962 break;
4964 case FRUIT_META_STREAM:
4965 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
4966 mem_ctx, pnum_streams,
4967 pstreams);
4968 break;
4970 default:
4971 return NT_STATUS_INTERNAL_ERROR;
4974 return status;
4977 static NTSTATUS fruit_streaminfo_rsrc_stream(
4978 vfs_handle_struct *handle,
4979 struct files_struct *fsp,
4980 const struct smb_filename *smb_fname,
4981 TALLOC_CTX *mem_ctx,
4982 unsigned int *pnum_streams,
4983 struct stream_struct **pstreams)
4985 bool ok;
4987 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
4988 if (!ok) {
4989 DBG_ERR("Filtering resource stream failed\n");
4990 return NT_STATUS_INTERNAL_ERROR;
4992 return NT_STATUS_OK;
4995 static NTSTATUS fruit_streaminfo_rsrc_xattr(
4996 vfs_handle_struct *handle,
4997 struct files_struct *fsp,
4998 const struct smb_filename *smb_fname,
4999 TALLOC_CTX *mem_ctx,
5000 unsigned int *pnum_streams,
5001 struct stream_struct **pstreams)
5003 bool ok;
5005 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5006 if (!ok) {
5007 DBG_ERR("Filtering resource stream failed\n");
5008 return NT_STATUS_INTERNAL_ERROR;
5010 return NT_STATUS_OK;
5013 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5014 vfs_handle_struct *handle,
5015 struct files_struct *fsp,
5016 const struct smb_filename *smb_fname,
5017 TALLOC_CTX *mem_ctx,
5018 unsigned int *pnum_streams,
5019 struct stream_struct **pstreams)
5021 struct stream_struct *stream = *pstreams;
5022 unsigned int num_streams = *pnum_streams;
5023 struct adouble *ad = NULL;
5024 bool ok;
5025 size_t rlen;
5026 int i;
5029 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5030 * and if yes, remove it from the list
5032 for (i = 0; i < num_streams; i++) {
5033 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5034 break;
5038 if (i < num_streams) {
5039 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5040 smb_fname_str_dbg(smb_fname));
5042 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5043 AFPRESOURCE_STREAM);
5044 if (!ok) {
5045 return NT_STATUS_INTERNAL_ERROR;
5049 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5050 if (ad == NULL) {
5051 return NT_STATUS_OK;
5054 rlen = ad_getentrylen(ad, ADEID_RFORK);
5055 TALLOC_FREE(ad);
5057 if (rlen == 0) {
5058 return NT_STATUS_OK;
5061 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5062 AFPRESOURCE_STREAM_NAME, rlen,
5063 smb_roundup(handle->conn, rlen));
5064 if (!ok) {
5065 return NT_STATUS_NO_MEMORY;
5068 return NT_STATUS_OK;
5071 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5072 struct files_struct *fsp,
5073 const struct smb_filename *smb_fname,
5074 TALLOC_CTX *mem_ctx,
5075 unsigned int *pnum_streams,
5076 struct stream_struct **pstreams)
5078 struct fruit_config_data *config = NULL;
5079 NTSTATUS status;
5081 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5082 return NT_STATUS_INTERNAL_ERROR);
5084 switch (config->rsrc) {
5085 case FRUIT_RSRC_STREAM:
5086 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5087 mem_ctx, pnum_streams,
5088 pstreams);
5089 break;
5091 case FRUIT_RSRC_XATTR:
5092 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5093 mem_ctx, pnum_streams,
5094 pstreams);
5095 break;
5097 case FRUIT_RSRC_ADFILE:
5098 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5099 mem_ctx, pnum_streams,
5100 pstreams);
5101 break;
5103 default:
5104 return NT_STATUS_INTERNAL_ERROR;
5107 return status;
5110 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5111 struct files_struct *fsp,
5112 const struct smb_filename *smb_fname,
5113 TALLOC_CTX *mem_ctx,
5114 unsigned int *pnum_streams,
5115 struct stream_struct **pstreams)
5117 struct fruit_config_data *config = NULL;
5118 NTSTATUS status;
5120 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5121 return NT_STATUS_UNSUCCESSFUL);
5123 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5125 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5126 pnum_streams, pstreams);
5127 if (!NT_STATUS_IS_OK(status)) {
5128 return status;
5131 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5132 mem_ctx, pnum_streams, pstreams);
5133 if (!NT_STATUS_IS_OK(status)) {
5134 return status;
5137 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5138 mem_ctx, pnum_streams, pstreams);
5139 if (!NT_STATUS_IS_OK(status)) {
5140 return status;
5143 return NT_STATUS_OK;
5146 static int fruit_ntimes(vfs_handle_struct *handle,
5147 const struct smb_filename *smb_fname,
5148 struct smb_file_time *ft)
5150 int rc = 0;
5151 struct adouble *ad = NULL;
5152 struct fruit_config_data *config = NULL;
5154 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5155 return -1);
5157 if ((config->meta != FRUIT_META_NETATALK) ||
5158 null_timespec(ft->create_time))
5160 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5163 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5164 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5166 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5167 if (ad == NULL) {
5168 goto exit;
5171 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5172 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5174 rc = ad_set(ad, smb_fname);
5176 exit:
5178 TALLOC_FREE(ad);
5179 if (rc != 0) {
5180 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5181 return -1;
5183 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5186 static int fruit_fallocate(struct vfs_handle_struct *handle,
5187 struct files_struct *fsp,
5188 uint32_t mode,
5189 off_t offset,
5190 off_t len)
5192 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5194 if (fio == NULL) {
5195 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5198 /* Let the pwrite code path handle it. */
5199 errno = ENOSYS;
5200 return -1;
5203 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5204 struct files_struct *fsp,
5205 off_t offset)
5207 if (offset == 0) {
5208 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5211 #ifdef HAVE_ATTROPEN
5212 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5213 #endif
5214 return 0;
5217 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5218 struct files_struct *fsp,
5219 off_t offset)
5221 int rc;
5222 struct adouble *ad = NULL;
5223 off_t ad_off;
5225 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5226 if (ad == NULL) {
5227 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5228 fsp_str_dbg(fsp), strerror(errno));
5229 return -1;
5232 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5234 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5235 if (rc != 0) {
5236 TALLOC_FREE(ad);
5237 return -1;
5240 ad_setentrylen(ad, ADEID_RFORK, offset);
5242 rc = ad_fset(ad, fsp);
5243 if (rc != 0) {
5244 DBG_ERR("ad_fset [%s] failed [%s]\n",
5245 fsp_str_dbg(fsp), strerror(errno));
5246 TALLOC_FREE(ad);
5247 return -1;
5250 TALLOC_FREE(ad);
5251 return 0;
5254 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5255 struct files_struct *fsp,
5256 off_t offset)
5258 if (offset == 0) {
5259 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5262 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5265 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5266 struct files_struct *fsp,
5267 off_t offset)
5269 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5270 int ret;
5272 switch (fio->config->rsrc) {
5273 case FRUIT_RSRC_XATTR:
5274 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5275 break;
5277 case FRUIT_RSRC_ADFILE:
5278 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5279 break;
5281 case FRUIT_RSRC_STREAM:
5282 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5283 break;
5285 default:
5286 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5287 return -1;
5291 return ret;
5294 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5295 struct files_struct *fsp,
5296 off_t offset)
5298 if (offset > 60) {
5299 DBG_WARNING("ftruncate %s to %jd",
5300 fsp_str_dbg(fsp), (intmax_t)offset);
5301 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5302 errno = EOVERFLOW;
5303 return -1;
5306 /* OS X returns success but does nothing */
5307 DBG_INFO("ignoring ftruncate %s to %jd\n",
5308 fsp_str_dbg(fsp), (intmax_t)offset);
5309 return 0;
5312 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5313 struct files_struct *fsp,
5314 off_t offset)
5316 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5317 int ret;
5319 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5320 (intmax_t)offset);
5322 if (fio == NULL) {
5323 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5326 if (fio->type == ADOUBLE_META) {
5327 ret = fruit_ftruncate_meta(handle, fsp, offset);
5328 } else {
5329 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5332 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5333 return ret;
5336 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5337 struct smb_request *req,
5338 uint16_t root_dir_fid,
5339 struct smb_filename *smb_fname,
5340 uint32_t access_mask,
5341 uint32_t share_access,
5342 uint32_t create_disposition,
5343 uint32_t create_options,
5344 uint32_t file_attributes,
5345 uint32_t oplock_request,
5346 struct smb2_lease *lease,
5347 uint64_t allocation_size,
5348 uint32_t private_flags,
5349 struct security_descriptor *sd,
5350 struct ea_list *ea_list,
5351 files_struct **result,
5352 int *pinfo,
5353 const struct smb2_create_blobs *in_context_blobs,
5354 struct smb2_create_blobs *out_context_blobs)
5356 NTSTATUS status;
5357 struct fruit_config_data *config = NULL;
5358 files_struct *fsp = NULL;
5360 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5361 if (!NT_STATUS_IS_OK(status)) {
5362 goto fail;
5365 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5366 return NT_STATUS_UNSUCCESSFUL);
5368 status = SMB_VFS_NEXT_CREATE_FILE(
5369 handle, req, root_dir_fid, smb_fname,
5370 access_mask, share_access,
5371 create_disposition, create_options,
5372 file_attributes, oplock_request,
5373 lease,
5374 allocation_size, private_flags,
5375 sd, ea_list, result,
5376 pinfo, in_context_blobs, out_context_blobs);
5377 if (!NT_STATUS_IS_OK(status)) {
5378 return status;
5381 fsp = *result;
5383 if (global_fruit_config.nego_aapl) {
5384 if (config->posix_rename && fsp->is_directory) {
5386 * Enable POSIX directory rename behaviour
5388 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5393 * If this is a plain open for existing files, opening an 0
5394 * byte size resource fork MUST fail with
5395 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5397 * Cf the vfs_fruit torture tests in test_rfork_create().
5399 if (is_afpresource_stream(fsp->fsp_name) &&
5400 create_disposition == FILE_OPEN)
5402 if (fsp->fsp_name->st.st_ex_size == 0) {
5403 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5404 goto fail;
5408 if (is_ntfs_stream_smb_fname(smb_fname)
5409 || fsp->is_directory) {
5410 return status;
5413 if (config->locking == FRUIT_LOCKING_NETATALK) {
5414 status = fruit_check_access(
5415 handle, *result,
5416 access_mask,
5417 map_share_mode_to_deny_mode(share_access, 0));
5418 if (!NT_STATUS_IS_OK(status)) {
5419 goto fail;
5423 return status;
5425 fail:
5426 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5428 if (fsp) {
5429 close_file(req, fsp, ERROR_CLOSE);
5430 *result = fsp = NULL;
5433 return status;
5436 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5437 const struct smb_filename *fname,
5438 TALLOC_CTX *mem_ctx,
5439 struct readdir_attr_data **pattr_data)
5441 struct fruit_config_data *config = NULL;
5442 struct readdir_attr_data *attr_data;
5443 NTSTATUS status;
5445 SMB_VFS_HANDLE_GET_DATA(handle, config,
5446 struct fruit_config_data,
5447 return NT_STATUS_UNSUCCESSFUL);
5449 if (!global_fruit_config.nego_aapl) {
5450 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5453 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5455 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5456 if (*pattr_data == NULL) {
5457 return NT_STATUS_UNSUCCESSFUL;
5459 attr_data = *pattr_data;
5460 attr_data->type = RDATTR_AAPL;
5463 * Mac metadata: compressed FinderInfo, resource fork length
5464 * and creation date
5466 status = readdir_attr_macmeta(handle, fname, attr_data);
5467 if (!NT_STATUS_IS_OK(status)) {
5469 * Error handling is tricky: if we return failure from
5470 * this function, the corresponding directory entry
5471 * will to be passed to the client, so we really just
5472 * want to error out on fatal errors.
5474 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5475 goto fail;
5480 * UNIX mode
5482 if (config->unix_info_enabled) {
5483 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5487 * max_access
5489 if (!config->readdir_attr_max_access) {
5490 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5491 } else {
5492 status = smbd_calculate_access_mask(
5493 handle->conn,
5494 fname,
5495 false,
5496 SEC_FLAG_MAXIMUM_ALLOWED,
5497 &attr_data->attr_data.aapl.max_access);
5498 if (!NT_STATUS_IS_OK(status)) {
5499 goto fail;
5503 return NT_STATUS_OK;
5505 fail:
5506 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5507 fname->base_name, nt_errstr(status)));
5508 TALLOC_FREE(*pattr_data);
5509 return status;
5512 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5513 files_struct *fsp,
5514 uint32_t security_info,
5515 TALLOC_CTX *mem_ctx,
5516 struct security_descriptor **ppdesc)
5518 NTSTATUS status;
5519 struct security_ace ace;
5520 struct dom_sid sid;
5521 struct fruit_config_data *config;
5523 SMB_VFS_HANDLE_GET_DATA(handle, config,
5524 struct fruit_config_data,
5525 return NT_STATUS_UNSUCCESSFUL);
5527 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5528 mem_ctx, ppdesc);
5529 if (!NT_STATUS_IS_OK(status)) {
5530 return status;
5534 * Add MS NFS style ACEs with uid, gid and mode
5536 if (!global_fruit_config.nego_aapl) {
5537 return NT_STATUS_OK;
5539 if (!config->unix_info_enabled) {
5540 return NT_STATUS_OK;
5543 /* MS NFS style mode */
5544 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5545 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5546 status = security_descriptor_dacl_add(*ppdesc, &ace);
5547 if (!NT_STATUS_IS_OK(status)) {
5548 DEBUG(1,("failed to add MS NFS style ACE\n"));
5549 return status;
5552 /* MS NFS style uid */
5553 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5554 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5555 status = security_descriptor_dacl_add(*ppdesc, &ace);
5556 if (!NT_STATUS_IS_OK(status)) {
5557 DEBUG(1,("failed to add MS NFS style ACE\n"));
5558 return status;
5561 /* MS NFS style gid */
5562 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5563 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5564 status = security_descriptor_dacl_add(*ppdesc, &ace);
5565 if (!NT_STATUS_IS_OK(status)) {
5566 DEBUG(1,("failed to add MS NFS style ACE\n"));
5567 return status;
5570 return NT_STATUS_OK;
5573 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5574 files_struct *fsp,
5575 uint32_t security_info_sent,
5576 const struct security_descriptor *psd)
5578 NTSTATUS status;
5579 bool do_chmod;
5580 mode_t ms_nfs_mode = 0;
5581 int result;
5583 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5585 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5586 if (!NT_STATUS_IS_OK(status)) {
5587 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5588 return status;
5591 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5592 if (!NT_STATUS_IS_OK(status)) {
5593 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5594 return status;
5597 if (do_chmod) {
5598 if (fsp->fh->fd != -1) {
5599 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5600 } else {
5601 result = SMB_VFS_CHMOD(fsp->conn,
5602 fsp->fsp_name,
5603 ms_nfs_mode);
5606 if (result != 0) {
5607 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5608 result, (unsigned)ms_nfs_mode,
5609 strerror(errno)));
5610 status = map_nt_error_from_unix(errno);
5611 return status;
5615 return NT_STATUS_OK;
5618 static struct vfs_offload_ctx *fruit_offload_ctx;
5620 struct fruit_offload_read_state {
5621 struct vfs_handle_struct *handle;
5622 struct tevent_context *ev;
5623 files_struct *fsp;
5624 uint32_t fsctl;
5625 DATA_BLOB token;
5628 static void fruit_offload_read_done(struct tevent_req *subreq);
5630 static struct tevent_req *fruit_offload_read_send(
5631 TALLOC_CTX *mem_ctx,
5632 struct tevent_context *ev,
5633 struct vfs_handle_struct *handle,
5634 files_struct *fsp,
5635 uint32_t fsctl,
5636 uint32_t ttl,
5637 off_t offset,
5638 size_t to_copy)
5640 struct tevent_req *req = NULL;
5641 struct tevent_req *subreq = NULL;
5642 struct fruit_offload_read_state *state = NULL;
5644 req = tevent_req_create(mem_ctx, &state,
5645 struct fruit_offload_read_state);
5646 if (req == NULL) {
5647 return NULL;
5649 *state = (struct fruit_offload_read_state) {
5650 .handle = handle,
5651 .ev = ev,
5652 .fsp = fsp,
5653 .fsctl = fsctl,
5656 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5657 fsctl, ttl, offset, to_copy);
5658 if (tevent_req_nomem(subreq, req)) {
5659 return tevent_req_post(req, ev);
5661 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5662 return req;
5665 static void fruit_offload_read_done(struct tevent_req *subreq)
5667 struct tevent_req *req = tevent_req_callback_data(
5668 subreq, struct tevent_req);
5669 struct fruit_offload_read_state *state = tevent_req_data(
5670 req, struct fruit_offload_read_state);
5671 NTSTATUS status;
5673 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5674 state->handle,
5675 state,
5676 &state->token);
5677 TALLOC_FREE(subreq);
5678 if (tevent_req_nterror(req, status)) {
5679 return;
5682 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5683 tevent_req_done(req);
5684 return;
5687 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5688 &fruit_offload_ctx);
5689 if (tevent_req_nterror(req, status)) {
5690 return;
5693 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5694 state->fsp,
5695 &state->token);
5696 if (tevent_req_nterror(req, status)) {
5697 return;
5700 tevent_req_done(req);
5701 return;
5704 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5705 struct vfs_handle_struct *handle,
5706 TALLOC_CTX *mem_ctx,
5707 DATA_BLOB *token)
5709 struct fruit_offload_read_state *state = tevent_req_data(
5710 req, struct fruit_offload_read_state);
5711 NTSTATUS status;
5713 if (tevent_req_is_nterror(req, &status)) {
5714 tevent_req_received(req);
5715 return status;
5718 token->length = state->token.length;
5719 token->data = talloc_move(mem_ctx, &state->token.data);
5721 tevent_req_received(req);
5722 return NT_STATUS_OK;
5725 struct fruit_offload_write_state {
5726 struct vfs_handle_struct *handle;
5727 off_t copied;
5728 struct files_struct *src_fsp;
5729 struct files_struct *dst_fsp;
5730 bool is_copyfile;
5733 static void fruit_offload_write_done(struct tevent_req *subreq);
5734 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
5735 TALLOC_CTX *mem_ctx,
5736 struct tevent_context *ev,
5737 uint32_t fsctl,
5738 DATA_BLOB *token,
5739 off_t transfer_offset,
5740 struct files_struct *dest_fsp,
5741 off_t dest_off,
5742 off_t num)
5744 struct tevent_req *req, *subreq;
5745 struct fruit_offload_write_state *state;
5746 NTSTATUS status;
5747 struct fruit_config_data *config;
5748 off_t src_off = transfer_offset;
5749 files_struct *src_fsp = NULL;
5750 off_t to_copy = num;
5751 bool copyfile_enabled = false;
5753 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
5754 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
5756 SMB_VFS_HANDLE_GET_DATA(handle, config,
5757 struct fruit_config_data,
5758 return NULL);
5760 req = tevent_req_create(mem_ctx, &state,
5761 struct fruit_offload_write_state);
5762 if (req == NULL) {
5763 return NULL;
5765 state->handle = handle;
5766 state->dst_fsp = dest_fsp;
5768 switch (fsctl) {
5769 case FSCTL_SRV_COPYCHUNK:
5770 case FSCTL_SRV_COPYCHUNK_WRITE:
5771 copyfile_enabled = config->copyfile_enabled;
5772 break;
5773 default:
5774 break;
5778 * Check if this a OS X copyfile style copychunk request with
5779 * a requested chunk count of 0 that was translated to a
5780 * offload_write_send VFS call overloading the parameters src_off
5781 * = dest_off = num = 0.
5783 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
5784 status = vfs_offload_token_db_fetch_fsp(
5785 fruit_offload_ctx, token, &src_fsp);
5786 if (tevent_req_nterror(req, status)) {
5787 return tevent_req_post(req, ev);
5789 state->src_fsp = src_fsp;
5791 status = vfs_stat_fsp(src_fsp);
5792 if (tevent_req_nterror(req, status)) {
5793 return tevent_req_post(req, ev);
5796 to_copy = src_fsp->fsp_name->st.st_ex_size;
5797 state->is_copyfile = true;
5800 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
5801 mem_ctx,
5803 fsctl,
5804 token,
5805 transfer_offset,
5806 dest_fsp,
5807 dest_off,
5808 to_copy);
5809 if (tevent_req_nomem(subreq, req)) {
5810 return tevent_req_post(req, ev);
5813 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
5814 return req;
5817 static void fruit_offload_write_done(struct tevent_req *subreq)
5819 struct tevent_req *req = tevent_req_callback_data(
5820 subreq, struct tevent_req);
5821 struct fruit_offload_write_state *state = tevent_req_data(
5822 req, struct fruit_offload_write_state);
5823 NTSTATUS status;
5824 unsigned int num_streams = 0;
5825 struct stream_struct *streams = NULL;
5826 unsigned int i;
5827 struct smb_filename *src_fname_tmp = NULL;
5828 struct smb_filename *dst_fname_tmp = NULL;
5830 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
5831 subreq,
5832 &state->copied);
5833 TALLOC_FREE(subreq);
5834 if (tevent_req_nterror(req, status)) {
5835 return;
5838 if (!state->is_copyfile) {
5839 tevent_req_done(req);
5840 return;
5844 * Now copy all remaining streams. We know the share supports
5845 * streams, because we're in vfs_fruit. We don't do this async
5846 * because streams are few and small.
5848 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
5849 state->src_fsp->fsp_name,
5850 req, &num_streams, &streams);
5851 if (tevent_req_nterror(req, status)) {
5852 return;
5855 if (num_streams == 1) {
5856 /* There is always one stream, ::$DATA. */
5857 tevent_req_done(req);
5858 return;
5861 for (i = 0; i < num_streams; i++) {
5862 DEBUG(10, ("%s: stream: '%s'/%zu\n",
5863 __func__, streams[i].name, (size_t)streams[i].size));
5865 src_fname_tmp = synthetic_smb_fname(
5866 req,
5867 state->src_fsp->fsp_name->base_name,
5868 streams[i].name,
5869 NULL,
5870 state->src_fsp->fsp_name->flags);
5871 if (tevent_req_nomem(src_fname_tmp, req)) {
5872 return;
5875 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
5876 TALLOC_FREE(src_fname_tmp);
5877 continue;
5880 dst_fname_tmp = synthetic_smb_fname(
5881 req,
5882 state->dst_fsp->fsp_name->base_name,
5883 streams[i].name,
5884 NULL,
5885 state->dst_fsp->fsp_name->flags);
5886 if (tevent_req_nomem(dst_fname_tmp, req)) {
5887 TALLOC_FREE(src_fname_tmp);
5888 return;
5891 status = copy_file(req,
5892 state->handle->conn,
5893 src_fname_tmp,
5894 dst_fname_tmp,
5895 OPENX_FILE_CREATE_IF_NOT_EXIST,
5896 0, false);
5897 if (!NT_STATUS_IS_OK(status)) {
5898 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
5899 smb_fname_str_dbg(src_fname_tmp),
5900 smb_fname_str_dbg(dst_fname_tmp),
5901 nt_errstr(status)));
5902 TALLOC_FREE(src_fname_tmp);
5903 TALLOC_FREE(dst_fname_tmp);
5904 tevent_req_nterror(req, status);
5905 return;
5908 TALLOC_FREE(src_fname_tmp);
5909 TALLOC_FREE(dst_fname_tmp);
5912 TALLOC_FREE(streams);
5913 TALLOC_FREE(src_fname_tmp);
5914 TALLOC_FREE(dst_fname_tmp);
5915 tevent_req_done(req);
5918 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
5919 struct tevent_req *req,
5920 off_t *copied)
5922 struct fruit_offload_write_state *state = tevent_req_data(
5923 req, struct fruit_offload_write_state);
5924 NTSTATUS status;
5926 if (tevent_req_is_nterror(req, &status)) {
5927 DEBUG(1, ("server side copy chunk failed: %s\n",
5928 nt_errstr(status)));
5929 *copied = 0;
5930 tevent_req_received(req);
5931 return status;
5934 *copied = state->copied;
5935 tevent_req_received(req);
5937 return NT_STATUS_OK;
5940 static struct vfs_fn_pointers vfs_fruit_fns = {
5941 .connect_fn = fruit_connect,
5943 /* File operations */
5944 .chmod_fn = fruit_chmod,
5945 .chown_fn = fruit_chown,
5946 .unlink_fn = fruit_unlink,
5947 .rename_fn = fruit_rename,
5948 .rmdir_fn = fruit_rmdir,
5949 .open_fn = fruit_open,
5950 .pread_fn = fruit_pread,
5951 .pwrite_fn = fruit_pwrite,
5952 .pread_send_fn = fruit_pread_send,
5953 .pread_recv_fn = fruit_pread_recv,
5954 .pwrite_send_fn = fruit_pwrite_send,
5955 .pwrite_recv_fn = fruit_pwrite_recv,
5956 .stat_fn = fruit_stat,
5957 .lstat_fn = fruit_lstat,
5958 .fstat_fn = fruit_fstat,
5959 .streaminfo_fn = fruit_streaminfo,
5960 .ntimes_fn = fruit_ntimes,
5961 .ftruncate_fn = fruit_ftruncate,
5962 .fallocate_fn = fruit_fallocate,
5963 .create_file_fn = fruit_create_file,
5964 .readdir_attr_fn = fruit_readdir_attr,
5965 .offload_read_send_fn = fruit_offload_read_send,
5966 .offload_read_recv_fn = fruit_offload_read_recv,
5967 .offload_write_send_fn = fruit_offload_write_send,
5968 .offload_write_recv_fn = fruit_offload_write_recv,
5970 /* NT ACL operations */
5971 .fget_nt_acl_fn = fruit_fget_nt_acl,
5972 .fset_nt_acl_fn = fruit_fset_nt_acl,
5975 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
5976 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
5978 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
5979 &vfs_fruit_fns);
5980 if (!NT_STATUS_IS_OK(ret)) {
5981 return ret;
5984 vfs_fruit_debug_level = debug_add_class("fruit");
5985 if (vfs_fruit_debug_level == -1) {
5986 vfs_fruit_debug_level = DBGC_VFS;
5987 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5988 "vfs_fruit_init"));
5989 } else {
5990 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5991 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
5994 return ret;