s3: smbd: vfs_fruit: Add remove_virtual_nfs_aces() a generic NFS ACE remover.
[Samba.git] / source3 / modules / vfs_fruit.c
blobf63d53b9a9947ef1348f814490ee62e87cdee99f
1 /*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
58 * share modes.
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
65 * blobs content.
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
77 * Extended Attributes
78 * ===================
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
90 * TODO
91 * ====
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
95 * - add tests
98 static int vfs_fruit_debug_level = DBGC_VFS;
100 static struct global_fruit_config {
101 bool nego_aapl; /* client negotiated AAPL */
103 } global_fruit_config;
105 #undef DBGC_CLASS
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
117 #else
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
120 #endif
122 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
124 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
125 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
126 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
127 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
129 struct fruit_config_data {
130 enum fruit_rsrc rsrc;
131 enum fruit_meta meta;
132 enum fruit_locking locking;
133 enum fruit_encoding encoding;
134 bool use_aapl; /* config from smb.conf */
135 bool use_copyfile;
136 bool readdir_attr_enabled;
137 bool unix_info_enabled;
138 bool copyfile_enabled;
139 bool veto_appledouble;
140 bool posix_rename;
141 bool aapl_zero_file_id;
142 const char *model;
145 * Additional options, all enabled by default,
146 * possibly useful for analyzing performance. The associated
147 * operations with each of them may be expensive, so having
148 * the chance to disable them individually gives a chance
149 * tweaking the setup for the particular usecase.
151 bool readdir_attr_rsize;
152 bool readdir_attr_finder_info;
153 bool readdir_attr_max_access;
156 static const struct enum_list fruit_rsrc[] = {
157 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
159 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
160 { -1, NULL}
163 static const struct enum_list fruit_meta[] = {
164 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
165 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
166 { -1, NULL}
169 static const struct enum_list fruit_locking[] = {
170 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
171 {FRUIT_LOCKING_NONE, "none"},
172 { -1, NULL}
175 static const struct enum_list fruit_encoding[] = {
176 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
177 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
178 { -1, NULL}
181 static const char *fruit_catia_maps =
182 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
183 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
184 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
185 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
186 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
187 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
188 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
189 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
190 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
191 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
192 "0x0d:0xf00d";
194 /*****************************************************************************
195 * Defines, functions and data structures that deal with AppleDouble
196 *****************************************************************************/
199 * There are two AppleDouble blobs we deal with:
201 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
202 * metadata in an xattr
204 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
205 * ._ files
207 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
209 /* Version info */
210 #define AD_VERSION2 0x00020000
211 #define AD_VERSION AD_VERSION2
214 * AppleDouble entry IDs.
216 #define ADEID_DFORK 1
217 #define ADEID_RFORK 2
218 #define ADEID_NAME 3
219 #define ADEID_COMMENT 4
220 #define ADEID_ICONBW 5
221 #define ADEID_ICONCOL 6
222 #define ADEID_FILEI 7
223 #define ADEID_FILEDATESI 8
224 #define ADEID_FINDERI 9
225 #define ADEID_MACFILEI 10
226 #define ADEID_PRODOSFILEI 11
227 #define ADEID_MSDOSFILEI 12
228 #define ADEID_SHORTNAME 13
229 #define ADEID_AFPFILEI 14
230 #define ADEID_DID 15
232 /* Private Netatalk entries */
233 #define ADEID_PRIVDEV 16
234 #define ADEID_PRIVINO 17
235 #define ADEID_PRIVSYN 18
236 #define ADEID_PRIVID 19
237 #define ADEID_MAX (ADEID_PRIVID + 1)
240 * These are the real ids for the private entries,
241 * as stored in the adouble file
243 #define AD_DEV 0x80444556
244 #define AD_INO 0x80494E4F
245 #define AD_SYN 0x8053594E
246 #define AD_ID 0x8053567E
248 /* Number of actually used entries */
249 #define ADEID_NUM_XATTR 8
250 #define ADEID_NUM_DOT_UND 2
251 #define ADEID_NUM_RSRC_XATTR 1
253 /* AppleDouble magic */
254 #define AD_APPLESINGLE_MAGIC 0x00051600
255 #define AD_APPLEDOUBLE_MAGIC 0x00051607
256 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
258 /* Sizes of relevant entry bits */
259 #define ADEDLEN_MAGIC 4
260 #define ADEDLEN_VERSION 4
261 #define ADEDLEN_FILLER 16
262 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
263 #define ADEDLEN_NENTRIES 2
264 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
265 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
266 #define AD_ENTRY_LEN_EID 4
267 #define AD_ENTRY_LEN_OFF 4
268 #define AD_ENTRY_LEN_LEN 4
269 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
271 /* Field widths */
272 #define ADEDLEN_NAME 255
273 #define ADEDLEN_COMMENT 200
274 #define ADEDLEN_FILEI 16
275 #define ADEDLEN_FINDERI 32
276 #define ADEDLEN_FILEDATESI 16
277 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
278 #define ADEDLEN_AFPFILEI 4
279 #define ADEDLEN_MACFILEI 4
280 #define ADEDLEN_PRODOSFILEI 8
281 #define ADEDLEN_MSDOSFILEI 2
282 #define ADEDLEN_DID 4
283 #define ADEDLEN_PRIVDEV 8
284 #define ADEDLEN_PRIVINO 8
285 #define ADEDLEN_PRIVSYN 8
286 #define ADEDLEN_PRIVID 4
288 /* Offsets */
289 #define ADEDOFF_MAGIC 0
290 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
291 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
292 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
294 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
295 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
296 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
297 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
298 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
299 ADEDLEN_FILEDATESI)
300 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
301 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
302 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
303 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
305 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
306 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
307 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
309 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
310 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
311 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
312 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
313 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
314 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
316 #if AD_DATASZ_XATTR != 402
317 #error bad size for AD_DATASZ_XATTR
318 #endif
320 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
321 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
322 ADEDLEN_FINDERI)
323 #if AD_DATASZ_DOT_UND != 82
324 #error bad size for AD_DATASZ_DOT_UND
325 #endif
328 * Sharemode locks fcntl() offsets
330 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
331 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
332 #else
333 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
334 #endif
335 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
337 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
338 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
339 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
340 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
341 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
342 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
343 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
344 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
345 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
346 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
348 /* Time stuff we overload the bits a little */
349 #define AD_DATE_CREATE 0
350 #define AD_DATE_MODIFY 4
351 #define AD_DATE_BACKUP 8
352 #define AD_DATE_ACCESS 12
353 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
354 AD_DATE_BACKUP | AD_DATE_ACCESS)
355 #define AD_DATE_UNIX (1 << 10)
356 #define AD_DATE_START 0x80000000
357 #define AD_DATE_DELTA 946684800
358 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
359 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
361 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
362 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
363 #define AD_XATTR_HDR_SIZE 36
364 #define AD_XATTR_MAX_HDR_SIZE 65536
366 /* Accessor macros */
367 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
368 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
369 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
370 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
373 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
374 * representation as well as the on-disk format.
376 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
377 * the length of the FinderInfo entry is larger then 32 bytes. It is then
378 * preceeded with 2 bytes padding.
380 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
383 struct ad_xattr_header {
384 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
385 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
386 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
387 uint32_t adx_data_start; /* file offset to attribute data area */
388 uint32_t adx_data_length; /* length of attribute data area */
389 uint32_t adx_reserved[3];
390 uint16_t adx_flags;
391 uint16_t adx_num_attrs;
394 /* On-disk entries are aligned on 4 byte boundaries */
395 struct ad_xattr_entry {
396 uint32_t adx_offset; /* file offset to data */
397 uint32_t adx_length; /* size of attribute data */
398 uint16_t adx_flags;
399 uint8_t adx_namelen; /* included the NULL terminator */
400 char *adx_name; /* NULL-terminated UTF-8 name */
403 struct ad_entry {
404 size_t ade_off;
405 size_t ade_len;
408 struct adouble {
409 vfs_handle_struct *ad_handle;
410 int ad_fd;
411 bool ad_opened;
412 adouble_type_t ad_type;
413 uint32_t ad_magic;
414 uint32_t ad_version;
415 struct ad_entry ad_eid[ADEID_MAX];
416 char *ad_data;
417 struct ad_xattr_header adx_header;
418 struct ad_xattr_entry *adx_entries;
421 struct ad_entry_order {
422 uint32_t id, offset, len;
425 /* Netatalk AppleDouble metadata xattr */
426 static const
427 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
428 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
429 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
430 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
431 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
432 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
433 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
434 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
435 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
436 {0, 0, 0}
439 /* AppleDouble resource fork file (the ones prefixed by "._") */
440 static const
441 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
442 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
443 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
444 {0, 0, 0}
448 * Fake AppleDouble entry oder for resource fork xattr. The xattr
449 * isn't an AppleDouble file, it simply contains the resource data,
450 * but in order to be able to use some API calls like ad_getentryoff()
451 * we build a fake/helper struct adouble with this entry order struct.
453 static const
454 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
455 {ADEID_RFORK, 0, 0},
456 {0, 0, 0}
459 /* Conversion from enumerated id to on-disk AppleDouble id */
460 #define AD_EID_DISK(a) (set_eid[a])
461 static const uint32_t set_eid[] = {
462 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
463 AD_DEV, AD_INO, AD_SYN, AD_ID
466 struct fio {
467 /* tcon config handle */
468 struct fruit_config_data *config;
470 /* Denote stream type, meta or rsrc */
471 adouble_type_t type;
475 * Forward declarations
477 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
478 adouble_type_t type);
479 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
480 static int ad_fset(struct adouble *ad, files_struct *fsp);
481 static int adouble_path(TALLOC_CTX *ctx,
482 const struct smb_filename *smb_fname__in,
483 struct smb_filename **ppsmb_fname_out);
484 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
485 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
486 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
490 * Return a pointer to an AppleDouble entry
492 * Returns NULL if the entry is not present
494 static char *ad_get_entry(const struct adouble *ad, int eid)
496 off_t off = ad_getentryoff(ad, eid);
497 size_t len = ad_getentrylen(ad, eid);
499 if (off == 0 || len == 0) {
500 return NULL;
503 return ad->ad_data + off;
507 * Get a date
509 static int ad_getdate(const struct adouble *ad,
510 unsigned int dateoff,
511 uint32_t *date)
513 bool xlate = (dateoff & AD_DATE_UNIX);
514 char *p = NULL;
516 dateoff &= AD_DATE_MASK;
517 p = ad_get_entry(ad, ADEID_FILEDATESI);
518 if (p == NULL) {
519 return -1;
522 if (dateoff > AD_DATE_ACCESS) {
523 return -1;
526 memcpy(date, p + dateoff, sizeof(uint32_t));
528 if (xlate) {
529 *date = AD_DATE_TO_UNIX(*date);
531 return 0;
535 * Set a date
537 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
539 bool xlate = (dateoff & AD_DATE_UNIX);
540 char *p = NULL;
542 p = ad_get_entry(ad, ADEID_FILEDATESI);
543 if (p == NULL) {
544 return -1;
547 dateoff &= AD_DATE_MASK;
548 if (xlate) {
549 date = AD_DATE_FROM_UNIX(date);
552 if (dateoff > AD_DATE_ACCESS) {
553 return -1;
556 memcpy(p + dateoff, &date, sizeof(date));
558 return 0;
563 * Map on-disk AppleDouble id to enumerated id
565 static uint32_t get_eid(uint32_t eid)
567 if (eid <= 15) {
568 return eid;
571 switch (eid) {
572 case AD_DEV:
573 return ADEID_PRIVDEV;
574 case AD_INO:
575 return ADEID_PRIVINO;
576 case AD_SYN:
577 return ADEID_PRIVSYN;
578 case AD_ID:
579 return ADEID_PRIVID;
580 default:
581 break;
584 return 0;
588 * Pack AppleDouble structure into data buffer
590 static bool ad_pack(struct adouble *ad)
592 uint32_t eid;
593 uint16_t nent;
594 uint32_t bufsize;
595 uint32_t offset = 0;
597 bufsize = talloc_get_size(ad->ad_data);
598 if (bufsize < AD_DATASZ_DOT_UND) {
599 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
600 return false;
603 if (offset + ADEDLEN_MAGIC < offset ||
604 offset + ADEDLEN_MAGIC >= bufsize) {
605 return false;
607 RSIVAL(ad->ad_data, offset, ad->ad_magic);
608 offset += ADEDLEN_MAGIC;
610 if (offset + ADEDLEN_VERSION < offset ||
611 offset + ADEDLEN_VERSION >= bufsize) {
612 return false;
614 RSIVAL(ad->ad_data, offset, ad->ad_version);
615 offset += ADEDLEN_VERSION;
617 if (offset + ADEDLEN_FILLER < offset ||
618 offset + ADEDLEN_FILLER >= bufsize) {
619 return false;
621 if (ad->ad_type == ADOUBLE_RSRC) {
622 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
624 offset += ADEDLEN_FILLER;
626 if (offset + ADEDLEN_NENTRIES < offset ||
627 offset + ADEDLEN_NENTRIES >= bufsize) {
628 return false;
630 offset += ADEDLEN_NENTRIES;
632 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
633 if (ad->ad_eid[eid].ade_off == 0) {
635 * ade_off is also used as indicator whether a
636 * specific entry is used or not
638 continue;
641 if (offset + AD_ENTRY_LEN_EID < offset ||
642 offset + AD_ENTRY_LEN_EID >= bufsize) {
643 return false;
645 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
646 offset += AD_ENTRY_LEN_EID;
648 if (offset + AD_ENTRY_LEN_OFF < offset ||
649 offset + AD_ENTRY_LEN_OFF >= bufsize) {
650 return false;
652 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
653 offset += AD_ENTRY_LEN_OFF;
655 if (offset + AD_ENTRY_LEN_LEN < offset ||
656 offset + AD_ENTRY_LEN_LEN >= bufsize) {
657 return false;
659 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
660 offset += AD_ENTRY_LEN_LEN;
662 nent++;
665 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
666 return false;
668 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
670 return true;
673 static bool ad_unpack_xattrs(struct adouble *ad)
675 struct ad_xattr_header *h = &ad->adx_header;
676 const char *p = ad->ad_data;
677 uint32_t hoff;
678 uint32_t i;
680 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
681 return true;
684 /* 2 bytes padding */
685 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
687 h->adx_magic = RIVAL(p, hoff + 0);
688 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
689 h->adx_total_size = RIVAL(p, hoff + 8);
690 h->adx_data_start = RIVAL(p, hoff + 12);
691 h->adx_data_length = RIVAL(p, hoff + 16);
692 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
693 h->adx_num_attrs = RSVAL(p, hoff + 34);
695 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
696 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
697 return false;
700 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
701 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
702 return false;
704 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
705 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
706 return false;
709 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
710 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
711 return false;
714 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
715 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
716 return false;
718 if ((h->adx_data_start + h->adx_data_length) >
719 ad->adx_header.adx_total_size)
721 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
722 return false;
725 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
726 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
727 return false;
730 if (h->adx_num_attrs == 0) {
731 return true;
734 ad->adx_entries = talloc_zero_array(
735 ad, struct ad_xattr_entry, h->adx_num_attrs);
736 if (ad->adx_entries == NULL) {
737 return false;
740 hoff += AD_XATTR_HDR_SIZE;
742 for (i = 0; i < h->adx_num_attrs; i++) {
743 struct ad_xattr_entry *e = &ad->adx_entries[i];
745 hoff = (hoff + 3) & ~3;
747 e->adx_offset = RIVAL(p, hoff + 0);
748 e->adx_length = RIVAL(p, hoff + 4);
749 e->adx_flags = RSVAL(p, hoff + 8);
750 e->adx_namelen = *(p + hoff + 10);
752 if (e->adx_offset >= ad->adx_header.adx_total_size) {
753 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
754 e->adx_offset);
755 return false;
758 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
759 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
760 e->adx_length);
761 return false;
764 if ((e->adx_offset + e->adx_length) >
765 ad->adx_header.adx_total_size)
767 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
768 e->adx_length);
769 return false;
772 if (e->adx_namelen == 0) {
773 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
774 e->adx_namelen);
775 return false;
777 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
778 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
779 e->adx_namelen);
780 return false;
782 if ((hoff + 11 + e->adx_namelen) >
783 ad->adx_header.adx_data_start)
785 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
786 e->adx_namelen);
787 return false;
790 e->adx_name = talloc_strndup(ad->adx_entries,
791 p + hoff + 11,
792 e->adx_namelen);
793 if (e->adx_name == NULL) {
794 return false;
797 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
798 e->adx_name, e->adx_offset, e->adx_length);
799 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
800 e->adx_length);
802 hoff += 11 + e->adx_namelen;
805 return true;
809 * Unpack an AppleDouble blob into a struct adoble
811 static bool ad_unpack(struct adouble *ad, const size_t nentries,
812 size_t filesize)
814 size_t bufsize = talloc_get_size(ad->ad_data);
815 size_t adentries, i;
816 uint32_t eid, len, off;
817 bool ok;
820 * The size of the buffer ad->ad_data is checked when read, so
821 * we wouldn't have to check our own offsets, a few extra
822 * checks won't hurt though. We have to check the offsets we
823 * read from the buffer anyway.
826 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
827 DEBUG(1, ("bad size\n"));
828 return false;
831 ad->ad_magic = RIVAL(ad->ad_data, 0);
832 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
833 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
834 DEBUG(1, ("wrong magic or version\n"));
835 return false;
838 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
839 if (adentries != nentries) {
840 DEBUG(1, ("invalid number of entries: %zu\n",
841 adentries));
842 return false;
845 /* now, read in the entry bits */
846 for (i = 0; i < adentries; i++) {
847 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
848 eid = get_eid(eid);
849 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
850 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
852 if (!eid || eid > ADEID_MAX) {
853 DEBUG(1, ("bogus eid %d\n", eid));
854 return false;
858 * All entries other than the resource fork are
859 * expected to be read into the ad_data buffer, so
860 * ensure the specified offset is within that bound
862 if ((off > bufsize) && (eid != ADEID_RFORK)) {
863 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
864 eid, off, len));
865 return false;
869 * All entries besides FinderInfo and resource fork
870 * must fit into the buffer. FinderInfo is special as
871 * it may be larger then the default 32 bytes (if it
872 * contains marshalled xattrs), but we will fixup that
873 * in ad_convert(). And the resource fork is never
874 * accessed directly by the ad_data buf (also see
875 * comment above) anyway.
877 if ((eid != ADEID_RFORK) &&
878 (eid != ADEID_FINDERI) &&
879 ((off + len) > bufsize)) {
880 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
881 eid, off, len));
882 return false;
886 * That would be obviously broken
888 if (off > filesize) {
889 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
890 eid, off, len));
891 return false;
895 * Check for any entry that has its end beyond the
896 * filesize.
898 if (off + len < off) {
899 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
900 ", len: %" PRIu32 "\n",
901 eid, off, len));
902 return false;
905 if (off + len > filesize) {
907 * If this is the resource fork entry, we fix
908 * up the length, for any other entry we bail
909 * out.
911 if (eid != ADEID_RFORK) {
912 DEBUG(1, ("bogus eid %d: off: %" PRIu32
913 ", len: %" PRIu32 "\n",
914 eid, off, len));
915 return false;
919 * Fixup the resource fork entry by limiting
920 * the size to entryoffset - filesize.
922 len = filesize - off;
923 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
924 ", len: %" PRIu32 "\n", off, len));
927 ad->ad_eid[eid].ade_off = off;
928 ad->ad_eid[eid].ade_len = len;
931 ok = ad_unpack_xattrs(ad);
932 if (!ok) {
933 return false;
936 return true;
939 static bool ad_convert_xattr(struct adouble *ad,
940 const struct smb_filename *smb_fname,
941 char *map)
943 static struct char_mappings **string_replace_cmaps = NULL;
944 uint16_t i;
945 int saved_errno = 0;
946 NTSTATUS status;
948 if (ad->adx_header.adx_num_attrs == 0) {
949 return true;
952 if (string_replace_cmaps == NULL) {
953 const char **mappings = NULL;
955 mappings = str_list_make_v3_const(
956 talloc_tos(), fruit_catia_maps, NULL);
957 if (mappings == NULL) {
958 return false;
960 string_replace_cmaps = string_replace_init_map(mappings);
961 TALLOC_FREE(mappings);
964 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
965 struct ad_xattr_entry *e = &ad->adx_entries[i];
966 char *mapped_name = NULL;
967 char *tmp = NULL;
968 struct smb_filename *stream_name = NULL;
969 files_struct *fsp = NULL;
970 ssize_t nwritten;
972 status = string_replace_allocate(ad->ad_handle->conn,
973 e->adx_name,
974 string_replace_cmaps,
975 talloc_tos(),
976 &mapped_name,
977 vfs_translate_to_windows);
978 if (!NT_STATUS_IS_OK(status) &&
979 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
981 DBG_ERR("string_replace_allocate failed\n");
982 return -1;
985 tmp = mapped_name;
986 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
987 TALLOC_FREE(tmp);
988 if (mapped_name == NULL) {
989 return -1;
992 stream_name = synthetic_smb_fname(talloc_tos(),
993 smb_fname->base_name,
994 mapped_name,
995 NULL,
996 smb_fname->flags);
997 TALLOC_FREE(mapped_name);
998 if (stream_name == NULL) {
999 DBG_ERR("synthetic_smb_fname failed\n");
1000 return -1;
1003 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1005 status = SMB_VFS_CREATE_FILE(
1006 ad->ad_handle->conn, /* conn */
1007 NULL, /* req */
1008 0, /* root_dir_fid */
1009 stream_name, /* fname */
1010 FILE_GENERIC_WRITE, /* access_mask */
1011 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1012 FILE_OPEN_IF, /* create_disposition */
1013 0, /* create_options */
1014 0, /* file_attributes */
1015 INTERNAL_OPEN_ONLY, /* oplock_request */
1016 NULL, /* lease */
1017 0, /* allocation_size */
1018 0, /* private_flags */
1019 NULL, /* sd */
1020 NULL, /* ea_list */
1021 &fsp, /* result */
1022 NULL, /* psbuf */
1023 NULL, NULL); /* create context */
1024 TALLOC_FREE(stream_name);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1027 return -1;
1030 nwritten = SMB_VFS_PWRITE(fsp,
1031 map + e->adx_offset,
1032 e->adx_length,
1034 if (nwritten == -1) {
1035 DBG_ERR("SMB_VFS_PWRITE failed\n");
1036 saved_errno = errno;
1037 close_file(NULL, fsp, ERROR_CLOSE);
1038 errno = saved_errno;
1039 return -1;
1042 status = close_file(NULL, fsp, NORMAL_CLOSE);
1043 if (!NT_STATUS_IS_OK(status)) {
1044 return -1;
1046 fsp = NULL;
1049 return true;
1053 * Convert from Apple's ._ file to Netatalk
1055 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1056 * bytes containing packed xattrs. Netatalk can't deal with that, so
1057 * we simply discard the packed xattrs.
1059 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1060 * otherwise
1062 static int ad_convert(struct adouble *ad,
1063 const struct smb_filename *smb_fname,
1064 int fd)
1066 int rc = 0;
1067 char *map = MAP_FAILED;
1068 size_t origlen;
1069 bool ok;
1071 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1072 ad_getentrylen(ad, ADEID_RFORK);
1074 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1075 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1076 if (map == MAP_FAILED) {
1077 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1078 rc = -1;
1079 goto exit;
1082 ok = ad_convert_xattr(ad, smb_fname, map);
1083 if (!ok) {
1084 return -1;
1087 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1088 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1089 map + ad_getentryoff(ad, ADEID_RFORK),
1090 ad_getentrylen(ad, ADEID_RFORK));
1093 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1094 ad_setentryoff(ad, ADEID_RFORK,
1095 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1098 * FIXME: direct ftruncate(), but we don't have a fsp for the
1099 * VFS call
1101 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
1102 + ad_getentrylen(ad, ADEID_RFORK));
1104 exit:
1105 if (map != MAP_FAILED) {
1106 munmap(map, origlen);
1108 return rc;
1112 * Read and parse Netatalk AppleDouble metadata xattr
1114 static ssize_t ad_read_meta(struct adouble *ad,
1115 const struct smb_filename *smb_fname)
1117 int rc = 0;
1118 ssize_t ealen;
1119 bool ok;
1121 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1123 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1124 AFPINFO_EA_NETATALK, ad->ad_data,
1125 AD_DATASZ_XATTR);
1126 if (ealen == -1) {
1127 switch (errno) {
1128 case ENOATTR:
1129 case ENOENT:
1130 if (errno == ENOATTR) {
1131 errno = ENOENT;
1133 rc = -1;
1134 goto exit;
1135 default:
1136 DEBUG(2, ("error reading meta xattr: %s\n",
1137 strerror(errno)));
1138 rc = -1;
1139 goto exit;
1142 if (ealen != AD_DATASZ_XATTR) {
1143 DEBUG(2, ("bad size %zd\n", ealen));
1144 errno = EINVAL;
1145 rc = -1;
1146 goto exit;
1149 /* Now parse entries */
1150 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1151 if (!ok) {
1152 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1153 errno = EINVAL;
1154 rc = -1;
1155 goto exit;
1158 if (!ad_getentryoff(ad, ADEID_FINDERI)
1159 || !ad_getentryoff(ad, ADEID_COMMENT)
1160 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1161 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1162 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1163 || !ad_getentryoff(ad, ADEID_PRIVINO)
1164 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1165 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1166 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1167 errno = EINVAL;
1168 rc = -1;
1169 goto exit;
1172 exit:
1173 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1174 smb_fname->base_name, rc));
1176 if (rc != 0) {
1177 ealen = -1;
1178 if (errno == EINVAL) {
1179 become_root();
1180 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1181 unbecome_root();
1182 errno = ENOENT;
1185 return ealen;
1188 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1189 int flags,
1190 mode_t mode)
1192 #ifdef HAVE_ATTROPEN
1193 /* FIXME: direct Solaris xattr syscall */
1194 return attropen(smb_fname->base_name,
1195 AFPRESOURCE_EA_NETATALK, flags, mode);
1196 #else
1197 errno = ENOSYS;
1198 return -1;
1199 #endif
1202 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1203 int flags,
1204 mode_t mode)
1206 int ret;
1207 int fd;
1208 struct smb_filename *adp_smb_fname = NULL;
1210 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1211 if (ret != 0) {
1212 return -1;
1215 fd = open(adp_smb_fname->base_name, flags, mode);
1216 TALLOC_FREE(adp_smb_fname);
1218 return fd;
1221 static int ad_open_rsrc(vfs_handle_struct *handle,
1222 const struct smb_filename *smb_fname,
1223 int flags,
1224 mode_t mode)
1226 struct fruit_config_data *config = NULL;
1227 int fd;
1229 SMB_VFS_HANDLE_GET_DATA(handle, config,
1230 struct fruit_config_data, return -1);
1232 if (config->rsrc == FRUIT_RSRC_XATTR) {
1233 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1234 } else {
1235 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1238 return fd;
1242 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1243 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1244 * for file IO on the ._ file.
1246 static int ad_open(vfs_handle_struct *handle,
1247 struct adouble *ad,
1248 files_struct *fsp,
1249 const struct smb_filename *smb_fname,
1250 int flags,
1251 mode_t mode)
1253 int fd;
1255 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1256 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1258 if (ad->ad_type == ADOUBLE_META) {
1259 return 0;
1262 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1263 ad->ad_fd = fsp->fh->fd;
1264 ad->ad_opened = false;
1265 return 0;
1268 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1269 if (fd == -1) {
1270 return -1;
1272 ad->ad_opened = true;
1273 ad->ad_fd = fd;
1275 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1276 smb_fname->base_name,
1277 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1279 return 0;
1282 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1284 int ret;
1285 SMB_STRUCT_STAT st;
1287 /* FIXME: direct sys_fstat(), don't have an fsp */
1288 ret = sys_fstat(ad->ad_fd, &st,
1289 lp_fake_directory_create_times(
1290 SNUM(ad->ad_handle->conn)));
1291 if (ret != 0) {
1292 return -1;
1295 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1296 return st.st_ex_size;
1299 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1300 const struct smb_filename *smb_fname)
1302 SMB_STRUCT_STAT sbuf;
1303 char *p_ad = NULL;
1304 AfpInfo *ai = NULL;
1305 DATA_BLOB aiblob;
1306 struct smb_filename *stream_name = NULL;
1307 files_struct *fsp = NULL;
1308 ssize_t len;
1309 size_t size;
1310 ssize_t nwritten;
1311 NTSTATUS status;
1312 int saved_errno = 0;
1313 int ret;
1314 bool ok;
1316 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1317 SNUM(ad->ad_handle->conn)));
1318 if (ret != 0) {
1319 return -1;
1323 * AppleDouble file header content and size, two cases:
1325 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1326 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1328 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1330 size = sbuf.st_ex_size;
1331 if (size > talloc_array_length(ad->ad_data)) {
1332 if (size > AD_XATTR_MAX_HDR_SIZE) {
1333 size = AD_XATTR_MAX_HDR_SIZE;
1335 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1336 if (p_ad == NULL) {
1337 return -1;
1339 ad->ad_data = p_ad;
1342 len = sys_pread(ad->ad_fd, ad->ad_data,
1343 talloc_array_length(ad->ad_data), 0);
1344 if (len != talloc_array_length(ad->ad_data)) {
1345 DBG_NOTICE("%s %s: bad size: %zd\n",
1346 smb_fname->base_name, strerror(errno), len);
1347 return -1;
1350 /* Now parse entries */
1351 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1352 if (!ok) {
1353 DBG_ERR("invalid AppleDouble resource %s\n",
1354 smb_fname->base_name);
1355 errno = EINVAL;
1356 return -1;
1359 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1360 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1361 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1362 DBG_ERR("invalid AppleDouble resource %s\n",
1363 smb_fname->base_name);
1364 errno = EINVAL;
1365 return -1;
1368 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1369 return len;
1373 * Try to fixup AppleDouble files created by OS X with xattrs
1374 * appended to the ADEID_FINDERI entry. We simply remove the
1375 * xattrs blob, this means any fancy xattr that was stored
1376 * there is lost.
1379 ret = ad_convert(ad, smb_fname, ad->ad_fd);
1380 if (ret != 0) {
1381 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1382 return len;
1385 ok = ad_pack(ad);
1386 if (!ok) {
1387 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1388 return -1;
1391 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1392 if (len != AD_DATASZ_DOT_UND) {
1393 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1394 return -1;
1397 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1398 if (p_ad == NULL) {
1399 return -1;
1402 ai = afpinfo_new(talloc_tos());
1403 if (ai == NULL) {
1404 return -1;
1407 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1409 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1410 if (aiblob.data == NULL) {
1411 TALLOC_FREE(ai);
1412 return -1;
1415 size = afpinfo_pack(ai, (char *)aiblob.data);
1416 TALLOC_FREE(ai);
1417 if (size != AFP_INFO_SIZE) {
1418 return -1;
1421 stream_name = synthetic_smb_fname(talloc_tos(),
1422 smb_fname->base_name,
1423 AFPINFO_STREAM,
1424 NULL,
1425 smb_fname->flags);
1426 if (stream_name == NULL) {
1427 data_blob_free(&aiblob);
1428 DBG_ERR("synthetic_smb_fname failed\n");
1429 return -1;
1432 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1434 status = SMB_VFS_CREATE_FILE(
1435 ad->ad_handle->conn, /* conn */
1436 NULL, /* req */
1437 0, /* root_dir_fid */
1438 stream_name, /* fname */
1439 FILE_GENERIC_WRITE, /* access_mask */
1440 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1441 FILE_OPEN_IF, /* create_disposition */
1442 0, /* create_options */
1443 0, /* file_attributes */
1444 INTERNAL_OPEN_ONLY, /* oplock_request */
1445 NULL, /* lease */
1446 0, /* allocation_size */
1447 0, /* private_flags */
1448 NULL, /* sd */
1449 NULL, /* ea_list */
1450 &fsp, /* result */
1451 NULL, /* psbuf */
1452 NULL, NULL); /* create context */
1453 TALLOC_FREE(stream_name);
1454 if (!NT_STATUS_IS_OK(status)) {
1455 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1456 return -1;
1459 nwritten = SMB_VFS_PWRITE(fsp,
1460 aiblob.data,
1461 aiblob.length,
1463 if (nwritten == -1) {
1464 DBG_ERR("SMB_VFS_PWRITE failed\n");
1465 saved_errno = errno;
1466 close_file(NULL, fsp, ERROR_CLOSE);
1467 errno = saved_errno;
1468 return -1;
1471 status = close_file(NULL, fsp, NORMAL_CLOSE);
1472 if (!NT_STATUS_IS_OK(status)) {
1473 return -1;
1475 fsp = NULL;
1477 return len;
1481 * Read and parse resource fork, either ._ AppleDouble file or xattr
1483 static ssize_t ad_read_rsrc(struct adouble *ad,
1484 const struct smb_filename *smb_fname)
1486 struct fruit_config_data *config = NULL;
1487 ssize_t len;
1489 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1490 struct fruit_config_data, return -1);
1492 if (config->rsrc == FRUIT_RSRC_XATTR) {
1493 len = ad_read_rsrc_xattr(ad);
1494 } else {
1495 len = ad_read_rsrc_adouble(ad, smb_fname);
1498 return len;
1502 * Read and unpack an AppleDouble metadata xattr or resource
1504 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1506 switch (ad->ad_type) {
1507 case ADOUBLE_META:
1508 return ad_read_meta(ad, smb_fname);
1509 case ADOUBLE_RSRC:
1510 return ad_read_rsrc(ad, smb_fname);
1511 default:
1512 return -1;
1516 static int adouble_destructor(struct adouble *ad)
1518 if ((ad->ad_fd != -1) && ad->ad_opened) {
1519 close(ad->ad_fd);
1520 ad->ad_fd = -1;
1522 return 0;
1526 * Allocate a struct adouble without initialiing it
1528 * The struct is either hang of the fsp extension context or if fsp is
1529 * NULL from ctx.
1531 * @param[in] ctx talloc context
1532 * @param[in] handle vfs handle
1533 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1535 * @return adouble handle
1537 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1538 adouble_type_t type)
1540 int rc = 0;
1541 size_t adsize = 0;
1542 struct adouble *ad;
1543 struct fruit_config_data *config;
1545 SMB_VFS_HANDLE_GET_DATA(handle, config,
1546 struct fruit_config_data, return NULL);
1548 switch (type) {
1549 case ADOUBLE_META:
1550 adsize = AD_DATASZ_XATTR;
1551 break;
1552 case ADOUBLE_RSRC:
1553 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1554 adsize = AD_DATASZ_DOT_UND;
1556 break;
1557 default:
1558 return NULL;
1561 ad = talloc_zero(ctx, struct adouble);
1562 if (ad == NULL) {
1563 rc = -1;
1564 goto exit;
1567 if (adsize) {
1568 ad->ad_data = talloc_zero_array(ad, char, adsize);
1569 if (ad->ad_data == NULL) {
1570 rc = -1;
1571 goto exit;
1575 ad->ad_handle = handle;
1576 ad->ad_type = type;
1577 ad->ad_magic = AD_MAGIC;
1578 ad->ad_version = AD_VERSION;
1579 ad->ad_fd = -1;
1581 talloc_set_destructor(ad, adouble_destructor);
1583 exit:
1584 if (rc != 0) {
1585 TALLOC_FREE(ad);
1587 return ad;
1591 * Allocate and initialize a new struct adouble
1593 * @param[in] ctx talloc context
1594 * @param[in] handle vfs handle
1595 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1597 * @return adouble handle, initialized
1599 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1600 adouble_type_t type)
1602 int rc = 0;
1603 const struct ad_entry_order *eid;
1604 struct adouble *ad = NULL;
1605 struct fruit_config_data *config;
1606 time_t t = time(NULL);
1608 SMB_VFS_HANDLE_GET_DATA(handle, config,
1609 struct fruit_config_data, return NULL);
1611 switch (type) {
1612 case ADOUBLE_META:
1613 eid = entry_order_meta_xattr;
1614 break;
1615 case ADOUBLE_RSRC:
1616 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1617 eid = entry_order_dot_und;
1618 } else {
1619 eid = entry_order_rsrc_xattr;
1621 break;
1622 default:
1623 return NULL;
1626 ad = ad_alloc(ctx, handle, type);
1627 if (ad == NULL) {
1628 return NULL;
1631 while (eid->id) {
1632 ad->ad_eid[eid->id].ade_off = eid->offset;
1633 ad->ad_eid[eid->id].ade_len = eid->len;
1634 eid++;
1637 /* put something sane in the date fields */
1638 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1639 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1640 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1641 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1643 if (rc != 0) {
1644 TALLOC_FREE(ad);
1646 return ad;
1649 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1650 vfs_handle_struct *handle,
1651 files_struct *fsp,
1652 const struct smb_filename *smb_fname,
1653 adouble_type_t type)
1655 int rc = 0;
1656 ssize_t len;
1657 struct adouble *ad = NULL;
1658 int mode;
1660 if (fsp != NULL) {
1661 smb_fname = fsp->base_fsp->fsp_name;
1664 DEBUG(10, ("ad_get(%s) called for %s\n",
1665 type == ADOUBLE_META ? "meta" : "rsrc",
1666 smb_fname->base_name));
1668 ad = ad_alloc(ctx, handle, type);
1669 if (ad == NULL) {
1670 rc = -1;
1671 goto exit;
1674 /* Try rw first so we can use the fd in ad_convert() */
1675 mode = O_RDWR;
1677 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1678 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1679 mode = O_RDONLY;
1680 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1682 if (rc == -1) {
1683 DBG_DEBUG("ad_open [%s] error [%s]\n",
1684 smb_fname->base_name, strerror(errno));
1685 goto exit;
1689 len = ad_read(ad, smb_fname);
1690 if (len == -1) {
1691 DEBUG(10, ("error reading AppleDouble for %s\n",
1692 smb_fname->base_name));
1693 rc = -1;
1694 goto exit;
1697 exit:
1698 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1699 type == ADOUBLE_META ? "meta" : "rsrc",
1700 smb_fname->base_name, rc));
1702 if (rc != 0) {
1703 TALLOC_FREE(ad);
1705 return ad;
1709 * Return AppleDouble data for a file
1711 * @param[in] ctx talloc context
1712 * @param[in] handle vfs handle
1713 * @param[in] smb_fname pathname to file or directory
1714 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1716 * @return talloced struct adouble or NULL on error
1718 static struct adouble *ad_get(TALLOC_CTX *ctx,
1719 vfs_handle_struct *handle,
1720 const struct smb_filename *smb_fname,
1721 adouble_type_t type)
1723 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1727 * Return AppleDouble data for a file
1729 * @param[in] ctx talloc context
1730 * @param[in] handle vfs handle
1731 * @param[in] fsp fsp to use for IO
1732 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1734 * @return talloced struct adouble or NULL on error
1736 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1737 files_struct *fsp, adouble_type_t type)
1739 return ad_get_internal(ctx, handle, fsp, NULL, type);
1743 * Set AppleDouble metadata on a file or directory
1745 * @param[in] ad adouble handle
1747 * @param[in] smb_fname pathname to file or directory
1749 * @return status code, 0 means success
1751 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1753 bool ok;
1754 int ret;
1756 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1758 if (ad->ad_type != ADOUBLE_META) {
1759 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1760 smb_fname->base_name);
1761 return -1;
1764 ok = ad_pack(ad);
1765 if (!ok) {
1766 return -1;
1769 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1770 smb_fname,
1771 AFPINFO_EA_NETATALK,
1772 ad->ad_data,
1773 AD_DATASZ_XATTR, 0);
1775 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1777 return ret;
1781 * Set AppleDouble metadata on a file or directory
1783 * @param[in] ad adouble handle
1784 * @param[in] fsp file handle
1786 * @return status code, 0 means success
1788 static int ad_fset(struct adouble *ad, files_struct *fsp)
1790 int rc = -1;
1791 ssize_t len;
1792 bool ok;
1794 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1796 if ((fsp == NULL)
1797 || (fsp->fh == NULL)
1798 || (fsp->fh->fd == -1))
1800 smb_panic("bad fsp");
1803 ok = ad_pack(ad);
1804 if (!ok) {
1805 return -1;
1808 switch (ad->ad_type) {
1809 case ADOUBLE_META:
1810 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1811 fsp->fsp_name,
1812 AFPINFO_EA_NETATALK,
1813 ad->ad_data,
1814 AD_DATASZ_XATTR, 0);
1815 break;
1817 case ADOUBLE_RSRC:
1818 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1819 fsp,
1820 ad->ad_data,
1821 AD_DATASZ_DOT_UND,
1823 if (len != AD_DATASZ_DOT_UND) {
1824 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1825 return -1;
1827 rc = 0;
1828 break;
1830 default:
1831 return -1;
1834 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1836 return rc;
1839 /*****************************************************************************
1840 * Helper functions
1841 *****************************************************************************/
1843 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1845 if (strncasecmp_m(smb_fname->stream_name,
1846 AFPINFO_STREAM_NAME,
1847 strlen(AFPINFO_STREAM_NAME)) == 0) {
1848 return true;
1850 return false;
1853 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1855 if (strncasecmp_m(smb_fname->stream_name,
1856 AFPRESOURCE_STREAM_NAME,
1857 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1858 return true;
1860 return false;
1864 * Test whether stream is an Apple stream, not used atm
1866 #if 0
1867 static bool is_apple_stream(const struct smb_filename *smb_fname)
1869 if (is_afpinfo_stream(smb_fname)) {
1870 return true;
1872 if (is_afpresource_stream(smb_fname)) {
1873 return true;
1875 return false;
1877 #endif
1880 * Initialize config struct from our smb.conf config parameters
1882 static int init_fruit_config(vfs_handle_struct *handle)
1884 struct fruit_config_data *config;
1885 int enumval;
1887 config = talloc_zero(handle->conn, struct fruit_config_data);
1888 if (!config) {
1889 DEBUG(1, ("talloc_zero() failed\n"));
1890 errno = ENOMEM;
1891 return -1;
1895 * Versions up to Samba 4.5.x had a spelling bug in the
1896 * fruit:resource option calling lp_parm_enum with
1897 * "res*s*ource" (ie two s).
1899 * In Samba 4.6 we accept both the wrong and the correct
1900 * spelling, in Samba 4.7 the bad spelling will be removed.
1902 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1903 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1904 if (enumval == -1) {
1905 DEBUG(1, ("value for %s: resource type unknown\n",
1906 FRUIT_PARAM_TYPE_NAME));
1907 return -1;
1909 config->rsrc = (enum fruit_rsrc)enumval;
1911 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1912 "resource", fruit_rsrc, enumval);
1913 if (enumval == -1) {
1914 DEBUG(1, ("value for %s: resource type unknown\n",
1915 FRUIT_PARAM_TYPE_NAME));
1916 return -1;
1918 config->rsrc = (enum fruit_rsrc)enumval;
1920 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1921 "metadata", fruit_meta, FRUIT_META_NETATALK);
1922 if (enumval == -1) {
1923 DEBUG(1, ("value for %s: metadata type unknown\n",
1924 FRUIT_PARAM_TYPE_NAME));
1925 return -1;
1927 config->meta = (enum fruit_meta)enumval;
1929 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1930 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1931 if (enumval == -1) {
1932 DEBUG(1, ("value for %s: locking type unknown\n",
1933 FRUIT_PARAM_TYPE_NAME));
1934 return -1;
1936 config->locking = (enum fruit_locking)enumval;
1938 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1939 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1940 if (enumval == -1) {
1941 DEBUG(1, ("value for %s: encoding type unknown\n",
1942 FRUIT_PARAM_TYPE_NAME));
1943 return -1;
1945 config->encoding = (enum fruit_encoding)enumval;
1947 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1948 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1949 FRUIT_PARAM_TYPE_NAME,
1950 "veto_appledouble",
1951 true);
1954 config->use_aapl = lp_parm_bool(
1955 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1957 config->unix_info_enabled = lp_parm_bool(
1958 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1960 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1961 "copyfile", false);
1963 config->posix_rename = lp_parm_bool(
1964 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1966 config->aapl_zero_file_id =
1967 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1969 config->readdir_attr_rsize = lp_parm_bool(
1970 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1972 config->readdir_attr_finder_info = lp_parm_bool(
1973 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1975 config->readdir_attr_max_access = lp_parm_bool(
1976 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1978 config->model = lp_parm_const_string(
1979 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1981 SMB_VFS_HANDLE_SET_DATA(handle, config,
1982 NULL, struct fruit_config_data,
1983 return -1);
1985 return 0;
1989 * Prepend "._" to a basename
1990 * Return a new struct smb_filename with stream_name == NULL.
1992 static int adouble_path(TALLOC_CTX *ctx,
1993 const struct smb_filename *smb_fname_in,
1994 struct smb_filename **pp_smb_fname_out)
1996 char *parent;
1997 const char *base;
1998 struct smb_filename *smb_fname = cp_smb_filename(ctx,
1999 smb_fname_in);
2001 if (smb_fname == NULL) {
2002 return -1;
2005 /* We need streamname to be NULL */
2006 TALLOC_FREE(smb_fname->stream_name);
2008 /* And we're replacing base_name. */
2009 TALLOC_FREE(smb_fname->base_name);
2011 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2012 &parent, &base)) {
2013 TALLOC_FREE(smb_fname);
2014 return -1;
2017 smb_fname->base_name = talloc_asprintf(smb_fname,
2018 "%s/._%s", parent, base);
2019 if (smb_fname->base_name == NULL) {
2020 TALLOC_FREE(smb_fname);
2021 return -1;
2024 *pp_smb_fname_out = smb_fname;
2026 return 0;
2030 * Allocate and initialize an AfpInfo struct
2032 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2034 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2035 if (ai == NULL) {
2036 return NULL;
2038 ai->afpi_Signature = AFP_Signature;
2039 ai->afpi_Version = AFP_Version;
2040 ai->afpi_BackupTime = AD_DATE_START;
2041 return ai;
2045 * Pack an AfpInfo struct into a buffer
2047 * Buffer size must be at least AFP_INFO_SIZE
2048 * Returns size of packed buffer
2050 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2052 memset(buf, 0, AFP_INFO_SIZE);
2054 RSIVAL(buf, 0, ai->afpi_Signature);
2055 RSIVAL(buf, 4, ai->afpi_Version);
2056 RSIVAL(buf, 12, ai->afpi_BackupTime);
2057 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2059 return AFP_INFO_SIZE;
2063 * Unpack a buffer into a AfpInfo structure
2065 * Buffer size must be at least AFP_INFO_SIZE
2066 * Returns allocated AfpInfo struct
2068 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2070 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2071 if (ai == NULL) {
2072 return NULL;
2075 ai->afpi_Signature = RIVAL(data, 0);
2076 ai->afpi_Version = RIVAL(data, 4);
2077 ai->afpi_BackupTime = RIVAL(data, 12);
2078 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2079 sizeof(ai->afpi_FinderInfo));
2081 if (ai->afpi_Signature != AFP_Signature
2082 || ai->afpi_Version != AFP_Version) {
2083 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2084 TALLOC_FREE(ai);
2087 return ai;
2091 * Fake an inode number from the md5 hash of the (xattr) name
2093 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2095 MD5_CTX ctx;
2096 unsigned char hash[16];
2097 SMB_INO_T result;
2098 char *upper_sname;
2100 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2101 SMB_ASSERT(upper_sname != NULL);
2103 MD5Init(&ctx);
2104 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2105 sizeof(sbuf->st_ex_dev));
2106 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2107 sizeof(sbuf->st_ex_ino));
2108 MD5Update(&ctx, (unsigned char *)upper_sname,
2109 talloc_get_size(upper_sname)-1);
2110 MD5Final(hash, &ctx);
2112 TALLOC_FREE(upper_sname);
2114 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2115 memcpy(&result, hash, sizeof(result));
2117 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2118 sname, (unsigned long long)result));
2120 return result;
2123 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2124 struct stream_struct **streams,
2125 const char *name, off_t size,
2126 off_t alloc_size)
2128 struct stream_struct *tmp;
2130 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2131 (*num_streams)+1);
2132 if (tmp == NULL) {
2133 return false;
2136 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2137 if (tmp[*num_streams].name == NULL) {
2138 return false;
2141 tmp[*num_streams].size = size;
2142 tmp[*num_streams].alloc_size = alloc_size;
2144 *streams = tmp;
2145 *num_streams += 1;
2146 return true;
2149 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2150 struct stream_struct **streams)
2152 struct stream_struct *tmp = *streams;
2153 unsigned int i;
2155 if (*num_streams == 0) {
2156 return true;
2159 for (i = 0; i < *num_streams; i++) {
2160 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2161 break;
2165 if (i == *num_streams) {
2166 return true;
2169 if (tmp[i].size > 0) {
2170 return true;
2173 TALLOC_FREE(tmp[i].name);
2174 if (*num_streams - 1 > i) {
2175 memmove(&tmp[i], &tmp[i+1],
2176 (*num_streams - i - 1) * sizeof(struct stream_struct));
2179 *num_streams -= 1;
2180 return true;
2183 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2184 struct stream_struct **streams,
2185 const char *name)
2187 struct stream_struct *tmp = *streams;
2188 unsigned int i;
2190 if (*num_streams == 0) {
2191 return true;
2194 for (i = 0; i < *num_streams; i++) {
2195 if (strequal_m(tmp[i].name, name)) {
2196 break;
2200 if (i == *num_streams) {
2201 return true;
2204 TALLOC_FREE(tmp[i].name);
2205 if (*num_streams - 1 > i) {
2206 memmove(&tmp[i], &tmp[i+1],
2207 (*num_streams - i - 1) * sizeof(struct stream_struct));
2210 *num_streams -= 1;
2211 return true;
2214 static bool ad_empty_finderinfo(const struct adouble *ad)
2216 int cmp;
2217 char emptybuf[ADEDLEN_FINDERI] = {0};
2218 char *fi = NULL;
2220 fi = ad_get_entry(ad, ADEID_FINDERI);
2221 if (fi == NULL) {
2222 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2223 return false;
2226 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2227 return (cmp == 0);
2230 static bool ai_empty_finderinfo(const AfpInfo *ai)
2232 int cmp;
2233 char emptybuf[ADEDLEN_FINDERI] = {0};
2235 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2236 return (cmp == 0);
2240 * Update btime with btime from Netatalk
2242 static void update_btime(vfs_handle_struct *handle,
2243 struct smb_filename *smb_fname)
2245 uint32_t t;
2246 struct timespec creation_time = {0};
2247 struct adouble *ad;
2248 struct fruit_config_data *config = NULL;
2250 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2251 return);
2253 switch (config->meta) {
2254 case FRUIT_META_STREAM:
2255 return;
2256 case FRUIT_META_NETATALK:
2257 /* Handled below */
2258 break;
2259 default:
2260 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2261 return;
2264 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2265 if (ad == NULL) {
2266 return;
2268 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2269 TALLOC_FREE(ad);
2270 return;
2272 TALLOC_FREE(ad);
2274 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2275 update_stat_ex_create_time(&smb_fname->st, creation_time);
2277 return;
2281 * Map an access mask to a Netatalk single byte byte range lock
2283 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2284 uint32_t access_mask)
2286 off_t offset;
2288 switch (access_mask) {
2289 case FILE_READ_DATA:
2290 offset = AD_FILELOCK_OPEN_RD;
2291 break;
2293 case FILE_WRITE_DATA:
2294 case FILE_APPEND_DATA:
2295 offset = AD_FILELOCK_OPEN_WR;
2296 break;
2298 default:
2299 offset = AD_FILELOCK_OPEN_NONE;
2300 break;
2303 if (fork_type == APPLE_FORK_RSRC) {
2304 if (offset == AD_FILELOCK_OPEN_NONE) {
2305 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2306 } else {
2307 offset += 2;
2311 return offset;
2315 * Map a deny mode to a Netatalk brl
2317 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2318 uint32_t deny_mode)
2320 off_t offset;
2322 switch (deny_mode) {
2323 case DENY_READ:
2324 offset = AD_FILELOCK_DENY_RD;
2325 break;
2327 case DENY_WRITE:
2328 offset = AD_FILELOCK_DENY_WR;
2329 break;
2331 default:
2332 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2335 if (fork_type == APPLE_FORK_RSRC) {
2336 offset += 2;
2339 return offset;
2343 * Call fcntl() with an exclusive F_GETLK request in order to
2344 * determine if there's an exisiting shared lock
2346 * @return true if the requested lock was found or any error occurred
2347 * false if the lock was not found
2349 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2351 bool result;
2352 off_t offset = in_offset;
2353 off_t len = 1;
2354 int type = F_WRLCK;
2355 pid_t pid;
2357 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2358 if (result == false) {
2359 return true;
2362 if (type != F_UNLCK) {
2363 return true;
2366 return false;
2369 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2370 files_struct *fsp,
2371 uint32_t access_mask,
2372 uint32_t deny_mode)
2374 NTSTATUS status = NT_STATUS_OK;
2375 struct byte_range_lock *br_lck = NULL;
2376 bool open_for_reading, open_for_writing, deny_read, deny_write;
2377 off_t off;
2378 bool have_read = false;
2379 int flags;
2381 /* FIXME: hardcoded data fork, add resource fork */
2382 enum apple_fork fork_type = APPLE_FORK_DATA;
2384 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2385 fsp_str_dbg(fsp),
2386 access_mask & FILE_READ_DATA ? "READ" :"-",
2387 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2388 deny_mode & DENY_READ ? "DENY_READ" : "-",
2389 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2391 if (fsp->fh->fd == -1) {
2392 return NT_STATUS_OK;
2395 flags = fcntl(fsp->fh->fd, F_GETFL);
2396 if (flags == -1) {
2397 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2398 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2399 return map_nt_error_from_unix(errno);
2402 if (flags & (O_RDONLY|O_RDWR)) {
2404 * Applying fcntl read locks requires an fd opened for
2405 * reading. This means we won't be applying locks for
2406 * files openend write-only, but what can we do...
2408 have_read = true;
2412 * Check read access and deny read mode
2414 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2415 /* Check access */
2416 open_for_reading = test_netatalk_lock(
2417 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2419 deny_read = test_netatalk_lock(
2420 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2422 DEBUG(10, ("read: %s, deny_write: %s\n",
2423 open_for_reading == true ? "yes" : "no",
2424 deny_read == true ? "yes" : "no"));
2426 if (((access_mask & FILE_READ_DATA) && deny_read)
2427 || ((deny_mode & DENY_READ) && open_for_reading)) {
2428 return NT_STATUS_SHARING_VIOLATION;
2431 /* Set locks */
2432 if ((access_mask & FILE_READ_DATA) && have_read) {
2433 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2434 br_lck = do_lock(
2435 handle->conn->sconn->msg_ctx, fsp,
2436 fsp->op->global->open_persistent_id, 1, off,
2437 READ_LOCK, POSIX_LOCK, false,
2438 &status, NULL);
2440 if (!NT_STATUS_IS_OK(status)) {
2441 return status;
2443 TALLOC_FREE(br_lck);
2446 if ((deny_mode & DENY_READ) && have_read) {
2447 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2448 br_lck = do_lock(
2449 handle->conn->sconn->msg_ctx, fsp,
2450 fsp->op->global->open_persistent_id, 1, off,
2451 READ_LOCK, POSIX_LOCK, false,
2452 &status, NULL);
2454 if (!NT_STATUS_IS_OK(status)) {
2455 return status;
2457 TALLOC_FREE(br_lck);
2462 * Check write access and deny write mode
2464 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2465 /* Check access */
2466 open_for_writing = test_netatalk_lock(
2467 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2469 deny_write = test_netatalk_lock(
2470 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2472 DEBUG(10, ("write: %s, deny_write: %s\n",
2473 open_for_writing == true ? "yes" : "no",
2474 deny_write == true ? "yes" : "no"));
2476 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2477 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2478 return NT_STATUS_SHARING_VIOLATION;
2481 /* Set locks */
2482 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2483 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2484 br_lck = do_lock(
2485 handle->conn->sconn->msg_ctx, fsp,
2486 fsp->op->global->open_persistent_id, 1, off,
2487 READ_LOCK, POSIX_LOCK, false,
2488 &status, NULL);
2490 if (!NT_STATUS_IS_OK(status)) {
2491 return status;
2493 TALLOC_FREE(br_lck);
2496 if ((deny_mode & DENY_WRITE) && have_read) {
2497 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2498 br_lck = do_lock(
2499 handle->conn->sconn->msg_ctx, fsp,
2500 fsp->op->global->open_persistent_id, 1, off,
2501 READ_LOCK, POSIX_LOCK, false,
2502 &status, NULL);
2504 if (!NT_STATUS_IS_OK(status)) {
2505 return status;
2507 TALLOC_FREE(br_lck);
2511 TALLOC_FREE(br_lck);
2513 return status;
2516 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2517 struct smb_request *req,
2518 const struct smb2_create_blobs *in_context_blobs,
2519 struct smb2_create_blobs *out_context_blobs)
2521 struct fruit_config_data *config;
2522 NTSTATUS status;
2523 struct smb2_create_blob *aapl = NULL;
2524 uint32_t cmd;
2525 bool ok;
2526 uint8_t p[16];
2527 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2528 uint64_t req_bitmap, client_caps;
2529 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2530 smb_ucs2_t *model;
2531 size_t modellen;
2533 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2534 return NT_STATUS_UNSUCCESSFUL);
2536 if (!config->use_aapl
2537 || in_context_blobs == NULL
2538 || out_context_blobs == NULL) {
2539 return NT_STATUS_OK;
2542 aapl = smb2_create_blob_find(in_context_blobs,
2543 SMB2_CREATE_TAG_AAPL);
2544 if (aapl == NULL) {
2545 return NT_STATUS_OK;
2548 if (aapl->data.length != 24) {
2549 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2550 (uintmax_t)aapl->data.length));
2551 return NT_STATUS_INVALID_PARAMETER;
2554 cmd = IVAL(aapl->data.data, 0);
2555 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2556 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2557 return NT_STATUS_INVALID_PARAMETER;
2560 req_bitmap = BVAL(aapl->data.data, 8);
2561 client_caps = BVAL(aapl->data.data, 16);
2563 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2564 SIVAL(p, 4, 0);
2565 SBVAL(p, 8, req_bitmap);
2566 ok = data_blob_append(req, &blob, p, 16);
2567 if (!ok) {
2568 return NT_STATUS_UNSUCCESSFUL;
2571 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2572 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2573 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2574 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2575 config->readdir_attr_enabled = true;
2578 if (config->use_copyfile) {
2579 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2580 config->copyfile_enabled = true;
2584 * The client doesn't set the flag, so we can't check
2585 * for it and just set it unconditionally
2587 if (config->unix_info_enabled) {
2588 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2591 SBVAL(p, 0, server_caps);
2592 ok = data_blob_append(req, &blob, p, 8);
2593 if (!ok) {
2594 return NT_STATUS_UNSUCCESSFUL;
2598 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2599 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2600 uint64_t caps = 0;
2602 switch (val) {
2603 case Auto:
2604 break;
2606 case True:
2607 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2608 break;
2610 default:
2611 break;
2614 SBVAL(p, 0, caps);
2616 ok = data_blob_append(req, &blob, p, 8);
2617 if (!ok) {
2618 return NT_STATUS_UNSUCCESSFUL;
2622 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2623 ok = convert_string_talloc(req,
2624 CH_UNIX, CH_UTF16LE,
2625 config->model, strlen(config->model),
2626 &model, &modellen);
2627 if (!ok) {
2628 return NT_STATUS_UNSUCCESSFUL;
2631 SIVAL(p, 0, 0);
2632 SIVAL(p + 4, 0, modellen);
2633 ok = data_blob_append(req, &blob, p, 8);
2634 if (!ok) {
2635 talloc_free(model);
2636 return NT_STATUS_UNSUCCESSFUL;
2639 ok = data_blob_append(req, &blob, model, modellen);
2640 talloc_free(model);
2641 if (!ok) {
2642 return NT_STATUS_UNSUCCESSFUL;
2646 status = smb2_create_blob_add(out_context_blobs,
2647 out_context_blobs,
2648 SMB2_CREATE_TAG_AAPL,
2649 blob);
2650 if (NT_STATUS_IS_OK(status)) {
2651 global_fruit_config.nego_aapl = true;
2652 if (config->aapl_zero_file_id) {
2653 aapl_force_zero_file_id(handle->conn->sconn);
2657 return status;
2660 static bool readdir_attr_meta_finderi_stream(
2661 struct vfs_handle_struct *handle,
2662 const struct smb_filename *smb_fname,
2663 AfpInfo *ai)
2665 struct smb_filename *stream_name = NULL;
2666 files_struct *fsp = NULL;
2667 ssize_t nread;
2668 NTSTATUS status;
2669 int ret;
2670 bool ok;
2671 uint8_t buf[AFP_INFO_SIZE];
2673 stream_name = synthetic_smb_fname(talloc_tos(),
2674 smb_fname->base_name,
2675 AFPINFO_STREAM_NAME,
2676 NULL, smb_fname->flags);
2677 if (stream_name == NULL) {
2678 return false;
2681 ret = SMB_VFS_STAT(handle->conn, stream_name);
2682 if (ret != 0) {
2683 return false;
2686 status = SMB_VFS_CREATE_FILE(
2687 handle->conn, /* conn */
2688 NULL, /* req */
2689 0, /* root_dir_fid */
2690 stream_name, /* fname */
2691 FILE_READ_DATA, /* access_mask */
2692 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2693 FILE_SHARE_DELETE),
2694 FILE_OPEN, /* create_disposition*/
2695 0, /* create_options */
2696 0, /* file_attributes */
2697 INTERNAL_OPEN_ONLY, /* oplock_request */
2698 NULL, /* lease */
2699 0, /* allocation_size */
2700 0, /* private_flags */
2701 NULL, /* sd */
2702 NULL, /* ea_list */
2703 &fsp, /* result */
2704 NULL, /* pinfo */
2705 NULL, NULL); /* create context */
2707 TALLOC_FREE(stream_name);
2709 if (!NT_STATUS_IS_OK(status)) {
2710 return false;
2713 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2714 if (nread != AFP_INFO_SIZE) {
2715 DBG_ERR("short read [%s] [%zd/%d]\n",
2716 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2717 ok = false;
2718 goto fail;
2721 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2722 AFP_FinderSize);
2724 ok = true;
2726 fail:
2727 if (fsp != NULL) {
2728 close_file(NULL, fsp, NORMAL_CLOSE);
2731 return ok;
2734 static bool readdir_attr_meta_finderi_netatalk(
2735 struct vfs_handle_struct *handle,
2736 const struct smb_filename *smb_fname,
2737 AfpInfo *ai)
2739 struct adouble *ad = NULL;
2740 char *p = NULL;
2742 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2743 if (ad == NULL) {
2744 return false;
2747 p = ad_get_entry(ad, ADEID_FINDERI);
2748 if (p == NULL) {
2749 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2750 TALLOC_FREE(ad);
2751 return false;
2754 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2755 TALLOC_FREE(ad);
2756 return true;
2759 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2760 const struct smb_filename *smb_fname,
2761 struct readdir_attr_data *attr_data)
2763 struct fruit_config_data *config = NULL;
2764 uint32_t date_added;
2765 AfpInfo ai = {0};
2766 bool ok;
2768 SMB_VFS_HANDLE_GET_DATA(handle, config,
2769 struct fruit_config_data,
2770 return false);
2772 switch (config->meta) {
2773 case FRUIT_META_NETATALK:
2774 ok = readdir_attr_meta_finderi_netatalk(
2775 handle, smb_fname, &ai);
2776 break;
2778 case FRUIT_META_STREAM:
2779 ok = readdir_attr_meta_finderi_stream(
2780 handle, smb_fname, &ai);
2781 break;
2783 default:
2784 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2785 return false;
2788 if (!ok) {
2789 /* Don't bother with errors, it's likely ENOENT */
2790 return true;
2793 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2794 /* finder_type */
2795 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2796 &ai.afpi_FinderInfo[0], 4);
2798 /* finder_creator */
2799 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2800 &ai.afpi_FinderInfo[4], 4);
2803 /* finder_flags */
2804 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2805 &ai.afpi_FinderInfo[8], 2);
2807 /* finder_ext_flags */
2808 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2809 &ai.afpi_FinderInfo[24], 2);
2811 /* creation date */
2812 date_added = convert_time_t_to_uint32_t(
2813 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2815 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2817 return true;
2820 static uint64_t readdir_attr_rfork_size_adouble(
2821 struct vfs_handle_struct *handle,
2822 const struct smb_filename *smb_fname)
2824 struct adouble *ad = NULL;
2825 uint64_t rfork_size;
2827 ad = ad_get(talloc_tos(), handle, smb_fname,
2828 ADOUBLE_RSRC);
2829 if (ad == NULL) {
2830 return 0;
2833 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2834 TALLOC_FREE(ad);
2836 return rfork_size;
2839 static uint64_t readdir_attr_rfork_size_stream(
2840 struct vfs_handle_struct *handle,
2841 const struct smb_filename *smb_fname)
2843 struct smb_filename *stream_name = NULL;
2844 int ret;
2845 uint64_t rfork_size;
2847 stream_name = synthetic_smb_fname(talloc_tos(),
2848 smb_fname->base_name,
2849 AFPRESOURCE_STREAM_NAME,
2850 NULL, 0);
2851 if (stream_name == NULL) {
2852 return 0;
2855 ret = SMB_VFS_STAT(handle->conn, stream_name);
2856 if (ret != 0) {
2857 TALLOC_FREE(stream_name);
2858 return 0;
2861 rfork_size = stream_name->st.st_ex_size;
2862 TALLOC_FREE(stream_name);
2864 return rfork_size;
2867 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2868 const struct smb_filename *smb_fname)
2870 struct fruit_config_data *config = NULL;
2871 uint64_t rfork_size;
2873 SMB_VFS_HANDLE_GET_DATA(handle, config,
2874 struct fruit_config_data,
2875 return 0);
2877 switch (config->rsrc) {
2878 case FRUIT_RSRC_ADFILE:
2879 case FRUIT_RSRC_XATTR:
2880 rfork_size = readdir_attr_rfork_size_adouble(handle,
2881 smb_fname);
2882 break;
2884 case FRUIT_META_STREAM:
2885 rfork_size = readdir_attr_rfork_size_stream(handle,
2886 smb_fname);
2887 break;
2889 default:
2890 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2891 rfork_size = 0;
2892 break;
2895 return rfork_size;
2898 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2899 const struct smb_filename *smb_fname,
2900 struct readdir_attr_data *attr_data)
2902 NTSTATUS status = NT_STATUS_OK;
2903 struct fruit_config_data *config = NULL;
2904 bool ok;
2906 SMB_VFS_HANDLE_GET_DATA(handle, config,
2907 struct fruit_config_data,
2908 return NT_STATUS_UNSUCCESSFUL);
2911 /* Ensure we return a default value in the creation_date field */
2912 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2915 * Resource fork length
2918 if (config->readdir_attr_rsize) {
2919 uint64_t rfork_size;
2921 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2922 attr_data->attr_data.aapl.rfork_size = rfork_size;
2926 * FinderInfo
2929 if (config->readdir_attr_finder_info) {
2930 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2931 if (!ok) {
2932 status = NT_STATUS_INTERNAL_ERROR;
2936 return status;
2939 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
2941 NTSTATUS status;
2942 uint32_t i;
2944 if (psd->dacl == NULL) {
2945 return NT_STATUS_OK;
2948 for (i = 0; i < psd->dacl->num_aces; i++) {
2949 /* MS NFS style mode/uid/gid */
2950 if (!dom_sid_compare_domain(
2951 &global_sid_Unix_NFS,
2952 &psd->dacl->aces[i].trustee) == 0) {
2953 /* Normal ACE entry. */
2954 continue;
2958 * security_descriptor_dacl_del()
2959 * *must* return NT_STATUS_OK as we know
2960 * we have something to remove.
2963 status = security_descriptor_dacl_del(psd,
2964 &psd->dacl->aces[i].trustee);
2965 if (!NT_STATUS_IS_OK(status)) {
2966 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
2967 nt_errstr(status));
2968 return status;
2972 * security_descriptor_dacl_del() may delete more
2973 * then one entry subsequent to this one if the
2974 * SID matches, but we only need to ensure that
2975 * we stay looking at the same element in the array.
2977 i--;
2979 return NT_STATUS_OK;
2982 /* Search MS NFS style ACE with UNIX mode */
2983 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2984 files_struct *fsp,
2985 struct security_descriptor *psd,
2986 mode_t *pmode,
2987 bool *pdo_chmod)
2989 uint32_t i;
2990 struct fruit_config_data *config = NULL;
2991 struct dom_sid sid;
2992 NTSTATUS status = NT_STATUS_OK;
2993 bool remove_ok = false;
2995 *pdo_chmod = false;
2997 SMB_VFS_HANDLE_GET_DATA(handle, config,
2998 struct fruit_config_data,
2999 return NT_STATUS_UNSUCCESSFUL);
3001 if (!global_fruit_config.nego_aapl) {
3002 return NT_STATUS_OK;
3004 if (psd->dacl == NULL || !config->unix_info_enabled) {
3005 return NT_STATUS_OK;
3008 for (i = 0; i < psd->dacl->num_aces; i++) {
3009 if (dom_sid_compare_domain(
3010 &global_sid_Unix_NFS_Mode,
3011 &psd->dacl->aces[i].trustee) == 0) {
3012 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3013 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3014 *pdo_chmod = true;
3016 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3017 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3018 break;
3023 * Remove any incoming virtual ACE entries generated by
3024 * fruit_fget_nt_acl().
3027 /* MS NFS style mode */
3028 sid_compose(&sid, &global_sid_Unix_NFS_Mode,
3029 fsp->fsp_name->st.st_ex_mode);
3030 status = security_descriptor_dacl_del(psd, &sid);
3031 remove_ok = (NT_STATUS_IS_OK(status) ||
3032 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
3033 if (!remove_ok) {
3034 DBG_WARNING("failed to remove MS NFS_mode style ACE\n");
3035 return status;
3038 /* MS NFS style uid */
3039 sid_compose(&sid, &global_sid_Unix_NFS_Users,
3040 fsp->fsp_name->st.st_ex_uid);
3041 status = security_descriptor_dacl_del(psd, &sid);
3042 remove_ok = (NT_STATUS_IS_OK(status) ||
3043 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
3044 if (!remove_ok) {
3045 DBG_WARNING("failed to remove MS NFS_users style ACE\n");
3046 return status;
3049 /* MS NFS style gid */
3050 sid_compose(&sid, &global_sid_Unix_NFS_Groups,
3051 fsp->fsp_name->st.st_ex_gid);
3052 status = security_descriptor_dacl_del(psd, &sid);
3053 remove_ok = (NT_STATUS_IS_OK(status) ||
3054 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
3055 if (!remove_ok) {
3056 DBG_WARNING("failed to remove MS NFS_groups style ACE\n");
3057 return status;
3060 return NT_STATUS_OK;
3063 /****************************************************************************
3064 * VFS ops
3065 ****************************************************************************/
3067 static int fruit_connect(vfs_handle_struct *handle,
3068 const char *service,
3069 const char *user)
3071 int rc;
3072 char *list = NULL, *newlist = NULL;
3073 struct fruit_config_data *config;
3075 DEBUG(10, ("fruit_connect\n"));
3077 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3078 if (rc < 0) {
3079 return rc;
3082 rc = init_fruit_config(handle);
3083 if (rc != 0) {
3084 return rc;
3087 SMB_VFS_HANDLE_GET_DATA(handle, config,
3088 struct fruit_config_data, return -1);
3090 if (config->veto_appledouble) {
3091 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3093 if (list) {
3094 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3095 newlist = talloc_asprintf(
3096 list,
3097 "%s/" ADOUBLE_NAME_PREFIX "*/",
3098 list);
3099 lp_do_parameter(SNUM(handle->conn),
3100 "veto files",
3101 newlist);
3103 } else {
3104 lp_do_parameter(SNUM(handle->conn),
3105 "veto files",
3106 "/" ADOUBLE_NAME_PREFIX "*/");
3109 TALLOC_FREE(list);
3112 if (config->encoding == FRUIT_ENC_NATIVE) {
3113 lp_do_parameter(SNUM(handle->conn),
3114 "catia:mappings",
3115 fruit_catia_maps);
3118 return rc;
3121 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3122 struct smb_filename *smb_fname,
3123 files_struct *fsp,
3124 int flags,
3125 mode_t mode)
3127 AfpInfo *ai = NULL;
3128 char afpinfo_buf[AFP_INFO_SIZE];
3129 ssize_t len, written;
3130 int hostfd = -1;
3131 int rc = -1;
3133 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3134 if (hostfd == -1) {
3135 return -1;
3138 if (!(flags & (O_CREAT | O_TRUNC))) {
3139 return hostfd;
3142 ai = afpinfo_new(talloc_tos());
3143 if (ai == NULL) {
3144 rc = -1;
3145 goto fail;
3148 len = afpinfo_pack(ai, afpinfo_buf);
3149 if (len != AFP_INFO_SIZE) {
3150 rc = -1;
3151 goto fail;
3154 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3155 fsp->fh->fd = hostfd;
3157 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3158 AFP_INFO_SIZE, 0);
3159 fsp->fh->fd = -1;
3160 if (written != AFP_INFO_SIZE) {
3161 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3162 rc = -1;
3163 goto fail;
3166 rc = 0;
3168 fail:
3169 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3171 if (rc != 0) {
3172 int saved_errno = errno;
3173 if (hostfd >= 0) {
3174 fsp->fh->fd = hostfd;
3175 SMB_VFS_NEXT_CLOSE(handle, fsp);
3177 hostfd = -1;
3178 errno = saved_errno;
3180 return hostfd;
3183 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3184 struct smb_filename *smb_fname,
3185 files_struct *fsp,
3186 int flags,
3187 mode_t mode)
3189 int rc;
3190 int fakefd = -1;
3191 struct adouble *ad = NULL;
3192 int fds[2];
3194 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3197 * Return a valid fd, but ensure any attempt to use it returns an error
3198 * (EPIPE). All operations on the smb_fname or the fsp will use path
3199 * based syscalls.
3201 rc = pipe(fds);
3202 if (rc != 0) {
3203 goto exit;
3205 fakefd = fds[0];
3206 close(fds[1]);
3208 if (flags & (O_CREAT | O_TRUNC)) {
3210 * The attribute does not exist or needs to be truncated,
3211 * create an AppleDouble EA
3213 ad = ad_init(fsp, handle, ADOUBLE_META);
3214 if (ad == NULL) {
3215 rc = -1;
3216 goto exit;
3219 rc = ad_set(ad, fsp->fsp_name);
3220 if (rc != 0) {
3221 rc = -1;
3222 goto exit;
3225 TALLOC_FREE(ad);
3228 exit:
3229 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3230 if (rc != 0) {
3231 int saved_errno = errno;
3232 if (fakefd >= 0) {
3233 close(fakefd);
3235 fakefd = -1;
3236 errno = saved_errno;
3238 return fakefd;
3241 static int fruit_open_meta(vfs_handle_struct *handle,
3242 struct smb_filename *smb_fname,
3243 files_struct *fsp, int flags, mode_t mode)
3245 int fd;
3246 struct fruit_config_data *config = NULL;
3247 struct fio *fio = NULL;
3249 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3251 SMB_VFS_HANDLE_GET_DATA(handle, config,
3252 struct fruit_config_data, return -1);
3254 switch (config->meta) {
3255 case FRUIT_META_STREAM:
3256 fd = fruit_open_meta_stream(handle, smb_fname,
3257 fsp, flags, mode);
3258 break;
3260 case FRUIT_META_NETATALK:
3261 fd = fruit_open_meta_netatalk(handle, smb_fname,
3262 fsp, flags, mode);
3263 break;
3265 default:
3266 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3267 return -1;
3270 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3272 if (fd == -1) {
3273 return -1;
3276 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3277 fio->type = ADOUBLE_META;
3278 fio->config = config;
3280 return fd;
3283 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3284 struct smb_filename *smb_fname,
3285 files_struct *fsp,
3286 int flags,
3287 mode_t mode)
3289 int rc = 0;
3290 struct adouble *ad = NULL;
3291 struct smb_filename *smb_fname_base = NULL;
3292 struct fruit_config_data *config = NULL;
3293 int hostfd = -1;
3295 SMB_VFS_HANDLE_GET_DATA(handle, config,
3296 struct fruit_config_data, return -1);
3298 if ((!(flags & O_CREAT)) &&
3299 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3301 /* sorry, but directories don't habe a resource fork */
3302 rc = -1;
3303 goto exit;
3306 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3307 if (rc != 0) {
3308 goto exit;
3311 /* Sanitize flags */
3312 if (flags & O_WRONLY) {
3313 /* We always need read access for the metadata header too */
3314 flags &= ~O_WRONLY;
3315 flags |= O_RDWR;
3318 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3319 flags, mode);
3320 if (hostfd == -1) {
3321 rc = -1;
3322 goto exit;
3325 if (flags & (O_CREAT | O_TRUNC)) {
3326 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3327 if (ad == NULL) {
3328 rc = -1;
3329 goto exit;
3332 fsp->fh->fd = hostfd;
3334 rc = ad_fset(ad, fsp);
3335 fsp->fh->fd = -1;
3336 if (rc != 0) {
3337 rc = -1;
3338 goto exit;
3340 TALLOC_FREE(ad);
3343 exit:
3345 TALLOC_FREE(smb_fname_base);
3347 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3348 if (rc != 0) {
3349 int saved_errno = errno;
3350 if (hostfd >= 0) {
3352 * BUGBUGBUG -- we would need to call
3353 * fd_close_posix here, but we don't have a
3354 * full fsp yet
3356 fsp->fh->fd = hostfd;
3357 SMB_VFS_CLOSE(fsp);
3359 hostfd = -1;
3360 errno = saved_errno;
3362 return hostfd;
3365 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3366 struct smb_filename *smb_fname,
3367 files_struct *fsp,
3368 int flags,
3369 mode_t mode)
3371 #ifdef HAVE_ATTROPEN
3372 int fd = -1;
3374 fd = attropen(smb_fname->base_name,
3375 AFPRESOURCE_EA_NETATALK,
3376 flags,
3377 mode);
3378 if (fd == -1) {
3379 return -1;
3382 return fd;
3384 #else
3385 errno = ENOSYS;
3386 return -1;
3387 #endif
3390 static int fruit_open_rsrc(vfs_handle_struct *handle,
3391 struct smb_filename *smb_fname,
3392 files_struct *fsp, int flags, mode_t mode)
3394 int fd;
3395 struct fruit_config_data *config = NULL;
3396 struct fio *fio = NULL;
3398 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3400 SMB_VFS_HANDLE_GET_DATA(handle, config,
3401 struct fruit_config_data, return -1);
3403 if (((flags & O_ACCMODE) == O_RDONLY)
3404 && (flags & O_CREAT)
3405 && !VALID_STAT(fsp->fsp_name->st))
3408 * This means the stream doesn't exist. macOS SMB server fails
3409 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3410 * 12565 and the test for this combination in
3411 * test_rfork_create().
3413 errno = ENOENT;
3414 return -1;
3417 switch (config->rsrc) {
3418 case FRUIT_RSRC_STREAM:
3419 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3420 break;
3422 case FRUIT_RSRC_ADFILE:
3423 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3424 fsp, flags, mode);
3425 break;
3427 case FRUIT_RSRC_XATTR:
3428 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3429 fsp, flags, mode);
3430 break;
3432 default:
3433 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3434 return -1;
3437 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3439 if (fd == -1) {
3440 return -1;
3443 fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3444 fio->type = ADOUBLE_RSRC;
3445 fio->config = config;
3447 return fd;
3450 static int fruit_open(vfs_handle_struct *handle,
3451 struct smb_filename *smb_fname,
3452 files_struct *fsp, int flags, mode_t mode)
3454 int fd;
3456 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3458 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3459 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3462 if (is_afpinfo_stream(smb_fname)) {
3463 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3464 } else if (is_afpresource_stream(smb_fname)) {
3465 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3466 } else {
3467 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3470 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3472 return fd;
3475 static int fruit_rename(struct vfs_handle_struct *handle,
3476 const struct smb_filename *smb_fname_src,
3477 const struct smb_filename *smb_fname_dst)
3479 int rc = -1;
3480 struct fruit_config_data *config = NULL;
3481 struct smb_filename *src_adp_smb_fname = NULL;
3482 struct smb_filename *dst_adp_smb_fname = NULL;
3484 SMB_VFS_HANDLE_GET_DATA(handle, config,
3485 struct fruit_config_data, return -1);
3487 if (!VALID_STAT(smb_fname_src->st)) {
3488 DBG_ERR("Need valid stat for [%s]\n",
3489 smb_fname_str_dbg(smb_fname_src));
3490 return -1;
3493 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3494 if (rc != 0) {
3495 return -1;
3498 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3499 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3501 return 0;
3504 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3505 if (rc != 0) {
3506 goto done;
3509 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3510 if (rc != 0) {
3511 goto done;
3514 DBG_DEBUG("%s -> %s\n",
3515 smb_fname_str_dbg(src_adp_smb_fname),
3516 smb_fname_str_dbg(dst_adp_smb_fname));
3518 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3519 if (errno == ENOENT) {
3520 rc = 0;
3523 done:
3524 TALLOC_FREE(src_adp_smb_fname);
3525 TALLOC_FREE(dst_adp_smb_fname);
3526 return rc;
3529 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3530 const struct smb_filename *smb_fname)
3532 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3535 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3536 const struct smb_filename *smb_fname)
3538 return SMB_VFS_REMOVEXATTR(handle->conn,
3539 smb_fname,
3540 AFPINFO_EA_NETATALK);
3543 static int fruit_unlink_meta(vfs_handle_struct *handle,
3544 const struct smb_filename *smb_fname)
3546 struct fruit_config_data *config = NULL;
3547 int rc;
3549 SMB_VFS_HANDLE_GET_DATA(handle, config,
3550 struct fruit_config_data, return -1);
3552 switch (config->meta) {
3553 case FRUIT_META_STREAM:
3554 rc = fruit_unlink_meta_stream(handle, smb_fname);
3555 break;
3557 case FRUIT_META_NETATALK:
3558 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3559 break;
3561 default:
3562 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3563 return -1;
3566 return rc;
3569 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3570 const struct smb_filename *smb_fname,
3571 bool force_unlink)
3573 int ret;
3575 if (!force_unlink) {
3576 struct smb_filename *smb_fname_cp = NULL;
3577 off_t size;
3579 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3580 if (smb_fname_cp == NULL) {
3581 return -1;
3585 * 0 byte resource fork streams are not listed by
3586 * vfs_streaminfo, as a result stream cleanup/deletion of file
3587 * deletion doesn't remove the resourcefork stream.
3590 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3591 if (ret != 0) {
3592 TALLOC_FREE(smb_fname_cp);
3593 DBG_ERR("stat [%s] failed [%s]\n",
3594 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3595 return -1;
3598 size = smb_fname_cp->st.st_ex_size;
3599 TALLOC_FREE(smb_fname_cp);
3601 if (size > 0) {
3602 /* OS X ignores resource fork stream delete requests */
3603 return 0;
3607 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3608 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3609 ret = 0;
3612 return ret;
3615 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3616 const struct smb_filename *smb_fname,
3617 bool force_unlink)
3619 int rc;
3620 struct adouble *ad = NULL;
3621 struct smb_filename *adp_smb_fname = NULL;
3623 if (!force_unlink) {
3624 ad = ad_get(talloc_tos(), handle, smb_fname,
3625 ADOUBLE_RSRC);
3626 if (ad == NULL) {
3627 errno = ENOENT;
3628 return -1;
3633 * 0 byte resource fork streams are not listed by
3634 * vfs_streaminfo, as a result stream cleanup/deletion of file
3635 * deletion doesn't remove the resourcefork stream.
3638 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3639 /* OS X ignores resource fork stream delete requests */
3640 TALLOC_FREE(ad);
3641 return 0;
3644 TALLOC_FREE(ad);
3647 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3648 if (rc != 0) {
3649 return -1;
3652 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3653 TALLOC_FREE(adp_smb_fname);
3654 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3655 rc = 0;
3658 return rc;
3661 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3662 const struct smb_filename *smb_fname,
3663 bool force_unlink)
3666 * OS X ignores resource fork stream delete requests, so nothing to do
3667 * here. Removing the file will remove the xattr anyway, so we don't
3668 * have to take care of removing 0 byte resource forks that could be
3669 * left behind.
3671 return 0;
3674 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3675 const struct smb_filename *smb_fname,
3676 bool force_unlink)
3678 struct fruit_config_data *config = NULL;
3679 int rc;
3681 SMB_VFS_HANDLE_GET_DATA(handle, config,
3682 struct fruit_config_data, return -1);
3684 switch (config->rsrc) {
3685 case FRUIT_RSRC_STREAM:
3686 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3687 break;
3689 case FRUIT_RSRC_ADFILE:
3690 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3691 break;
3693 case FRUIT_RSRC_XATTR:
3694 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3695 break;
3697 default:
3698 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3699 return -1;
3702 return rc;
3705 static int fruit_unlink(vfs_handle_struct *handle,
3706 const struct smb_filename *smb_fname)
3708 int rc;
3709 struct fruit_config_data *config = NULL;
3710 struct smb_filename *rsrc_smb_fname = NULL;
3712 SMB_VFS_HANDLE_GET_DATA(handle, config,
3713 struct fruit_config_data, return -1);
3715 if (is_afpinfo_stream(smb_fname)) {
3716 return fruit_unlink_meta(handle, smb_fname);
3717 } else if (is_afpresource_stream(smb_fname)) {
3718 return fruit_unlink_rsrc(handle, smb_fname, false);
3719 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3720 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3724 * A request to delete the base file. Because 0 byte resource
3725 * fork streams are not listed by fruit_streaminfo,
3726 * delete_all_streams() can't remove 0 byte resource fork
3727 * streams, so we have to cleanup this here.
3729 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3730 smb_fname->base_name,
3731 AFPRESOURCE_STREAM_NAME,
3732 NULL,
3733 smb_fname->flags);
3734 if (rsrc_smb_fname == NULL) {
3735 return -1;
3738 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3739 if ((rc != 0) && (errno != ENOENT)) {
3740 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3741 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3742 TALLOC_FREE(rsrc_smb_fname);
3743 return -1;
3745 TALLOC_FREE(rsrc_smb_fname);
3747 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3750 static int fruit_chmod(vfs_handle_struct *handle,
3751 const struct smb_filename *smb_fname,
3752 mode_t mode)
3754 int rc = -1;
3755 struct fruit_config_data *config = NULL;
3756 struct smb_filename *smb_fname_adp = NULL;
3758 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3759 if (rc != 0) {
3760 return rc;
3763 SMB_VFS_HANDLE_GET_DATA(handle, config,
3764 struct fruit_config_data, return -1);
3766 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3767 return 0;
3770 if (!VALID_STAT(smb_fname->st)) {
3771 return 0;
3774 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3775 return 0;
3778 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3779 if (rc != 0) {
3780 return -1;
3783 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3785 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3786 if (errno == ENOENT) {
3787 rc = 0;
3790 TALLOC_FREE(smb_fname_adp);
3791 return rc;
3794 static int fruit_chown(vfs_handle_struct *handle,
3795 const struct smb_filename *smb_fname,
3796 uid_t uid,
3797 gid_t gid)
3799 int rc = -1;
3800 struct fruit_config_data *config = NULL;
3801 struct smb_filename *adp_smb_fname = NULL;
3803 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3804 if (rc != 0) {
3805 return rc;
3808 SMB_VFS_HANDLE_GET_DATA(handle, config,
3809 struct fruit_config_data, return -1);
3811 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3812 return 0;
3815 if (!VALID_STAT(smb_fname->st)) {
3816 return 0;
3819 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3820 return 0;
3823 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3824 if (rc != 0) {
3825 goto done;
3828 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3830 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3831 if (errno == ENOENT) {
3832 rc = 0;
3835 done:
3836 TALLOC_FREE(adp_smb_fname);
3837 return rc;
3840 static int fruit_rmdir(struct vfs_handle_struct *handle,
3841 const struct smb_filename *smb_fname)
3843 DIR *dh = NULL;
3844 struct dirent *de;
3845 struct fruit_config_data *config;
3847 SMB_VFS_HANDLE_GET_DATA(handle, config,
3848 struct fruit_config_data, return -1);
3850 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3851 goto exit_rmdir;
3855 * Due to there is no way to change bDeleteVetoFiles variable
3856 * from this module, need to clean up ourselves
3859 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3860 if (dh == NULL) {
3861 goto exit_rmdir;
3864 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3865 int match;
3866 struct adouble *ad = NULL;
3867 char *p = NULL;
3868 struct smb_filename *ad_smb_fname = NULL;
3869 int ret;
3871 match = strncmp(de->d_name,
3872 ADOUBLE_NAME_PREFIX,
3873 strlen(ADOUBLE_NAME_PREFIX));
3874 if (match != 0) {
3875 continue;
3878 p = talloc_asprintf(talloc_tos(), "%s/%s",
3879 smb_fname->base_name, de->d_name);
3880 if (p == NULL) {
3881 DBG_ERR("talloc_asprintf failed\n");
3882 return -1;
3885 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3886 NULL, NULL,
3887 smb_fname->flags);
3888 TALLOC_FREE(p);
3889 if (ad_smb_fname == NULL) {
3890 DBG_ERR("synthetic_smb_fname failed\n");
3891 return -1;
3895 * Check whether it's a valid AppleDouble file, if
3896 * yes, delete it, ignore it otherwise.
3898 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3899 if (ad == NULL) {
3900 TALLOC_FREE(ad_smb_fname);
3901 TALLOC_FREE(p);
3902 continue;
3904 TALLOC_FREE(ad);
3906 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3907 TALLOC_FREE(ad_smb_fname);
3908 if (ret != 0) {
3909 DBG_ERR("Deleting [%s] failed\n",
3910 smb_fname_str_dbg(ad_smb_fname));
3914 exit_rmdir:
3915 if (dh) {
3916 SMB_VFS_CLOSEDIR(handle->conn, dh);
3918 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3921 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3922 files_struct *fsp, void *data,
3923 size_t n, off_t offset)
3925 ssize_t nread;
3926 int ret;
3928 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3930 if (nread == n) {
3931 return nread;
3934 DBG_ERR("Removing [%s] after short read [%zd]\n",
3935 fsp_str_dbg(fsp), nread);
3937 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3938 if (ret != 0) {
3939 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3940 return -1;
3943 errno = EINVAL;
3944 return -1;
3947 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3948 files_struct *fsp, void *data,
3949 size_t n, off_t offset)
3951 AfpInfo *ai = NULL;
3952 struct adouble *ad = NULL;
3953 char afpinfo_buf[AFP_INFO_SIZE];
3954 char *p = NULL;
3955 ssize_t nread;
3957 ai = afpinfo_new(talloc_tos());
3958 if (ai == NULL) {
3959 return -1;
3962 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3963 if (ad == NULL) {
3964 nread = -1;
3965 goto fail;
3968 p = ad_get_entry(ad, ADEID_FINDERI);
3969 if (p == NULL) {
3970 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3971 nread = -1;
3972 goto fail;
3975 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3977 nread = afpinfo_pack(ai, afpinfo_buf);
3978 if (nread != AFP_INFO_SIZE) {
3979 nread = -1;
3980 goto fail;
3983 memcpy(data, afpinfo_buf, n);
3984 nread = n;
3986 fail:
3987 TALLOC_FREE(ai);
3988 return nread;
3991 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3992 files_struct *fsp, void *data,
3993 size_t n, off_t offset)
3995 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3996 ssize_t nread;
3997 ssize_t to_return;
4000 * OS X has a off-by-1 error in the offset calculation, so we're
4001 * bug compatible here. It won't hurt, as any relevant real
4002 * world read requests from the AFP_AfpInfo stream will be
4003 * offset=0 n=60. offset is ignored anyway, see below.
4005 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4006 return 0;
4009 /* Yes, macOS always reads from offset 0 */
4010 offset = 0;
4011 to_return = MIN(n, AFP_INFO_SIZE);
4013 switch (fio->config->meta) {
4014 case FRUIT_META_STREAM:
4015 nread = fruit_pread_meta_stream(handle, fsp, data,
4016 to_return, offset);
4017 break;
4019 case FRUIT_META_NETATALK:
4020 nread = fruit_pread_meta_adouble(handle, fsp, data,
4021 to_return, offset);
4022 break;
4024 default:
4025 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4026 return -1;
4029 return nread;
4032 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4033 files_struct *fsp, void *data,
4034 size_t n, off_t offset)
4036 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4039 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4040 files_struct *fsp, void *data,
4041 size_t n, off_t offset)
4043 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4046 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4047 files_struct *fsp, void *data,
4048 size_t n, off_t offset)
4050 struct adouble *ad = NULL;
4051 ssize_t nread;
4053 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4054 if (ad == NULL) {
4055 return -1;
4058 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4059 offset + ad_getentryoff(ad, ADEID_RFORK));
4061 TALLOC_FREE(ad);
4062 return nread;
4065 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4066 files_struct *fsp, void *data,
4067 size_t n, off_t offset)
4069 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4070 ssize_t nread;
4072 switch (fio->config->rsrc) {
4073 case FRUIT_RSRC_STREAM:
4074 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4075 break;
4077 case FRUIT_RSRC_ADFILE:
4078 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4079 break;
4081 case FRUIT_RSRC_XATTR:
4082 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4083 break;
4085 default:
4086 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4087 return -1;
4090 return nread;
4093 static ssize_t fruit_pread(vfs_handle_struct *handle,
4094 files_struct *fsp, void *data,
4095 size_t n, off_t offset)
4097 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4098 ssize_t nread;
4100 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4101 fsp_str_dbg(fsp), (intmax_t)offset, n);
4103 if (fio == NULL) {
4104 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4107 if (fio->type == ADOUBLE_META) {
4108 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4109 } else {
4110 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4113 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4114 return nread;
4117 static bool fruit_must_handle_aio_stream(struct fio *fio)
4119 if (fio == NULL) {
4120 return false;
4123 if ((fio->type == ADOUBLE_META) &&
4124 (fio->config->meta == FRUIT_META_NETATALK))
4126 return true;
4129 if ((fio->type == ADOUBLE_RSRC) &&
4130 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4132 return true;
4135 return false;
4138 struct fruit_pread_state {
4139 ssize_t nread;
4140 struct vfs_aio_state vfs_aio_state;
4143 static void fruit_pread_done(struct tevent_req *subreq);
4145 static struct tevent_req *fruit_pread_send(
4146 struct vfs_handle_struct *handle,
4147 TALLOC_CTX *mem_ctx,
4148 struct tevent_context *ev,
4149 struct files_struct *fsp,
4150 void *data,
4151 size_t n, off_t offset)
4153 struct tevent_req *req = NULL;
4154 struct tevent_req *subreq = NULL;
4155 struct fruit_pread_state *state = NULL;
4156 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4158 req = tevent_req_create(mem_ctx, &state,
4159 struct fruit_pread_state);
4160 if (req == NULL) {
4161 return NULL;
4164 if (fruit_must_handle_aio_stream(fio)) {
4165 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4166 if (state->nread != n) {
4167 if (state->nread != -1) {
4168 errno = EIO;
4170 tevent_req_error(req, errno);
4171 return tevent_req_post(req, ev);
4173 tevent_req_done(req);
4174 return tevent_req_post(req, ev);
4177 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4178 data, n, offset);
4179 if (tevent_req_nomem(req, subreq)) {
4180 return tevent_req_post(req, ev);
4182 tevent_req_set_callback(subreq, fruit_pread_done, req);
4183 return req;
4186 static void fruit_pread_done(struct tevent_req *subreq)
4188 struct tevent_req *req = tevent_req_callback_data(
4189 subreq, struct tevent_req);
4190 struct fruit_pread_state *state = tevent_req_data(
4191 req, struct fruit_pread_state);
4193 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4194 TALLOC_FREE(subreq);
4196 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4197 return;
4199 tevent_req_done(req);
4202 static ssize_t fruit_pread_recv(struct tevent_req *req,
4203 struct vfs_aio_state *vfs_aio_state)
4205 struct fruit_pread_state *state = tevent_req_data(
4206 req, struct fruit_pread_state);
4208 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4209 return -1;
4212 *vfs_aio_state = state->vfs_aio_state;
4213 return state->nread;
4216 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4217 files_struct *fsp, const void *data,
4218 size_t n, off_t offset)
4220 AfpInfo *ai = NULL;
4221 size_t nwritten;
4222 bool ok;
4224 ai = afpinfo_unpack(talloc_tos(), data);
4225 if (ai == NULL) {
4226 return -1;
4229 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4230 if (nwritten != n) {
4231 return -1;
4234 if (!ai_empty_finderinfo(ai)) {
4235 return n;
4238 ok = set_delete_on_close(
4239 fsp,
4240 true,
4241 handle->conn->session_info->security_token,
4242 handle->conn->session_info->unix_token);
4243 if (!ok) {
4244 DBG_ERR("set_delete_on_close on [%s] failed\n",
4245 fsp_str_dbg(fsp));
4246 return -1;
4249 return n;
4252 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4253 files_struct *fsp, const void *data,
4254 size_t n, off_t offset)
4256 struct adouble *ad = NULL;
4257 AfpInfo *ai = NULL;
4258 char *p = NULL;
4259 int ret;
4260 bool ok;
4262 ai = afpinfo_unpack(talloc_tos(), data);
4263 if (ai == NULL) {
4264 return -1;
4267 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4268 if (ad == NULL) {
4269 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4270 if (ad == NULL) {
4271 return -1;
4274 p = ad_get_entry(ad, ADEID_FINDERI);
4275 if (p == NULL) {
4276 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4277 TALLOC_FREE(ad);
4278 return -1;
4281 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4283 ret = ad_fset(ad, fsp);
4284 if (ret != 0) {
4285 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4286 TALLOC_FREE(ad);
4287 return -1;
4290 TALLOC_FREE(ad);
4292 if (!ai_empty_finderinfo(ai)) {
4293 return n;
4296 ok = set_delete_on_close(
4297 fsp,
4298 true,
4299 handle->conn->session_info->security_token,
4300 handle->conn->session_info->unix_token);
4301 if (!ok) {
4302 DBG_ERR("set_delete_on_close on [%s] failed\n",
4303 fsp_str_dbg(fsp));
4304 return -1;
4307 return n;
4310 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4311 files_struct *fsp, const void *data,
4312 size_t n, off_t offset)
4314 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4315 ssize_t nwritten;
4318 * Writing an all 0 blob to the metadata stream
4319 * results in the stream being removed on a macOS
4320 * server. This ensures we behave the same and it
4321 * verified by the "delete AFP_AfpInfo by writing all
4322 * 0" test.
4324 if (n != AFP_INFO_SIZE || offset != 0) {
4325 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4326 (intmax_t)offset, (intmax_t)n);
4327 return -1;
4330 switch (fio->config->meta) {
4331 case FRUIT_META_STREAM:
4332 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4333 n, offset);
4334 break;
4336 case FRUIT_META_NETATALK:
4337 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4338 n, offset);
4339 break;
4341 default:
4342 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4343 return -1;
4346 return nwritten;
4349 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4350 files_struct *fsp, const void *data,
4351 size_t n, off_t offset)
4353 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4356 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4357 files_struct *fsp, const void *data,
4358 size_t n, off_t offset)
4360 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4363 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4364 files_struct *fsp, const void *data,
4365 size_t n, off_t offset)
4367 struct adouble *ad = NULL;
4368 ssize_t nwritten;
4369 int ret;
4371 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4372 if (ad == NULL) {
4373 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4374 return -1;
4377 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4378 offset + ad_getentryoff(ad, ADEID_RFORK));
4379 if (nwritten != n) {
4380 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4381 fsp_str_dbg(fsp), nwritten, n);
4382 TALLOC_FREE(ad);
4383 return -1;
4386 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4387 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4388 ret = ad_fset(ad, fsp);
4389 if (ret != 0) {
4390 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4391 TALLOC_FREE(ad);
4392 return -1;
4396 TALLOC_FREE(ad);
4397 return n;
4400 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4401 files_struct *fsp, const void *data,
4402 size_t n, off_t offset)
4404 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4405 ssize_t nwritten;
4407 switch (fio->config->rsrc) {
4408 case FRUIT_RSRC_STREAM:
4409 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4410 break;
4412 case FRUIT_RSRC_ADFILE:
4413 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4414 break;
4416 case FRUIT_RSRC_XATTR:
4417 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4418 break;
4420 default:
4421 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4422 return -1;
4425 return nwritten;
4428 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4429 files_struct *fsp, const void *data,
4430 size_t n, off_t offset)
4432 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4433 ssize_t nwritten;
4435 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4436 fsp_str_dbg(fsp), (intmax_t)offset, n);
4438 if (fio == NULL) {
4439 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4442 if (fio->type == ADOUBLE_META) {
4443 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4444 } else {
4445 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4448 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4449 return nwritten;
4452 struct fruit_pwrite_state {
4453 ssize_t nwritten;
4454 struct vfs_aio_state vfs_aio_state;
4457 static void fruit_pwrite_done(struct tevent_req *subreq);
4459 static struct tevent_req *fruit_pwrite_send(
4460 struct vfs_handle_struct *handle,
4461 TALLOC_CTX *mem_ctx,
4462 struct tevent_context *ev,
4463 struct files_struct *fsp,
4464 const void *data,
4465 size_t n, off_t offset)
4467 struct tevent_req *req = NULL;
4468 struct tevent_req *subreq = NULL;
4469 struct fruit_pwrite_state *state = NULL;
4470 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4472 req = tevent_req_create(mem_ctx, &state,
4473 struct fruit_pwrite_state);
4474 if (req == NULL) {
4475 return NULL;
4478 if (fruit_must_handle_aio_stream(fio)) {
4479 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4480 if (state->nwritten != n) {
4481 if (state->nwritten != -1) {
4482 errno = EIO;
4484 tevent_req_error(req, errno);
4485 return tevent_req_post(req, ev);
4487 tevent_req_done(req);
4488 return tevent_req_post(req, ev);
4491 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4492 data, n, offset);
4493 if (tevent_req_nomem(req, subreq)) {
4494 return tevent_req_post(req, ev);
4496 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4497 return req;
4500 static void fruit_pwrite_done(struct tevent_req *subreq)
4502 struct tevent_req *req = tevent_req_callback_data(
4503 subreq, struct tevent_req);
4504 struct fruit_pwrite_state *state = tevent_req_data(
4505 req, struct fruit_pwrite_state);
4507 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4508 TALLOC_FREE(subreq);
4510 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4511 return;
4513 tevent_req_done(req);
4516 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4517 struct vfs_aio_state *vfs_aio_state)
4519 struct fruit_pwrite_state *state = tevent_req_data(
4520 req, struct fruit_pwrite_state);
4522 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4523 return -1;
4526 *vfs_aio_state = state->vfs_aio_state;
4527 return state->nwritten;
4531 * Helper to stat/lstat the base file of an smb_fname.
4533 static int fruit_stat_base(vfs_handle_struct *handle,
4534 struct smb_filename *smb_fname,
4535 bool follow_links)
4537 char *tmp_stream_name;
4538 int rc;
4540 tmp_stream_name = smb_fname->stream_name;
4541 smb_fname->stream_name = NULL;
4542 if (follow_links) {
4543 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4544 } else {
4545 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4547 smb_fname->stream_name = tmp_stream_name;
4548 return rc;
4551 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4552 struct smb_filename *smb_fname,
4553 bool follow_links)
4555 int ret;
4557 if (follow_links) {
4558 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4559 } else {
4560 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4563 return ret;
4566 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4567 struct smb_filename *smb_fname,
4568 bool follow_links)
4570 struct adouble *ad = NULL;
4572 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4573 if (ad == NULL) {
4574 DBG_INFO("fruit_stat_meta %s: %s\n",
4575 smb_fname_str_dbg(smb_fname), strerror(errno));
4576 errno = ENOENT;
4577 return -1;
4579 TALLOC_FREE(ad);
4581 /* Populate the stat struct with info from the base file. */
4582 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4583 return -1;
4585 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4586 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4587 smb_fname->stream_name);
4588 return 0;
4591 static int fruit_stat_meta(vfs_handle_struct *handle,
4592 struct smb_filename *smb_fname,
4593 bool follow_links)
4595 struct fruit_config_data *config = NULL;
4596 int ret;
4598 SMB_VFS_HANDLE_GET_DATA(handle, config,
4599 struct fruit_config_data, return -1);
4601 switch (config->meta) {
4602 case FRUIT_META_STREAM:
4603 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4604 break;
4606 case FRUIT_META_NETATALK:
4607 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4608 break;
4610 default:
4611 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4612 return -1;
4615 return ret;
4618 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4619 struct smb_filename *smb_fname,
4620 bool follow_links)
4622 struct adouble *ad = NULL;
4623 int ret;
4625 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4626 if (ad == NULL) {
4627 errno = ENOENT;
4628 return -1;
4631 /* Populate the stat struct with info from the base file. */
4632 ret = fruit_stat_base(handle, smb_fname, follow_links);
4633 if (ret != 0) {
4634 TALLOC_FREE(ad);
4635 return -1;
4638 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4639 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4640 smb_fname->stream_name);
4641 TALLOC_FREE(ad);
4642 return 0;
4645 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4646 struct smb_filename *smb_fname,
4647 bool follow_links)
4649 int ret;
4651 if (follow_links) {
4652 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4653 } else {
4654 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4657 return ret;
4660 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4661 struct smb_filename *smb_fname,
4662 bool follow_links)
4664 #ifdef HAVE_ATTROPEN
4665 int ret;
4666 int fd = -1;
4668 /* Populate the stat struct with info from the base file. */
4669 ret = fruit_stat_base(handle, smb_fname, follow_links);
4670 if (ret != 0) {
4671 return -1;
4674 fd = attropen(smb_fname->base_name,
4675 AFPRESOURCE_EA_NETATALK,
4676 O_RDONLY);
4677 if (fd == -1) {
4678 return 0;
4681 ret = sys_fstat(fd, &smb_fname->st, false);
4682 if (ret != 0) {
4683 close(fd);
4684 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4685 AFPRESOURCE_EA_NETATALK);
4686 return -1;
4688 close(fd);
4689 fd = -1;
4691 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4692 smb_fname->stream_name);
4694 return ret;
4696 #else
4697 errno = ENOSYS;
4698 return -1;
4699 #endif
4702 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4703 struct smb_filename *smb_fname,
4704 bool follow_links)
4706 struct fruit_config_data *config = NULL;
4707 int ret;
4709 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4711 SMB_VFS_HANDLE_GET_DATA(handle, config,
4712 struct fruit_config_data, return -1);
4714 switch (config->rsrc) {
4715 case FRUIT_RSRC_STREAM:
4716 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4717 break;
4719 case FRUIT_RSRC_XATTR:
4720 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4721 break;
4723 case FRUIT_RSRC_ADFILE:
4724 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4725 break;
4727 default:
4728 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4729 return -1;
4732 return ret;
4735 static int fruit_stat(vfs_handle_struct *handle,
4736 struct smb_filename *smb_fname)
4738 int rc = -1;
4740 DEBUG(10, ("fruit_stat called for %s\n",
4741 smb_fname_str_dbg(smb_fname)));
4743 if (!is_ntfs_stream_smb_fname(smb_fname)
4744 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4745 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4746 if (rc == 0) {
4747 update_btime(handle, smb_fname);
4749 return rc;
4753 * Note if lp_posix_paths() is true, we can never
4754 * get here as is_ntfs_stream_smb_fname() is
4755 * always false. So we never need worry about
4756 * not following links here.
4759 if (is_afpinfo_stream(smb_fname)) {
4760 rc = fruit_stat_meta(handle, smb_fname, true);
4761 } else if (is_afpresource_stream(smb_fname)) {
4762 rc = fruit_stat_rsrc(handle, smb_fname, true);
4763 } else {
4764 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4767 if (rc == 0) {
4768 update_btime(handle, smb_fname);
4769 smb_fname->st.st_ex_mode &= ~S_IFMT;
4770 smb_fname->st.st_ex_mode |= S_IFREG;
4771 smb_fname->st.st_ex_blocks =
4772 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4774 return rc;
4777 static int fruit_lstat(vfs_handle_struct *handle,
4778 struct smb_filename *smb_fname)
4780 int rc = -1;
4782 DEBUG(10, ("fruit_lstat called for %s\n",
4783 smb_fname_str_dbg(smb_fname)));
4785 if (!is_ntfs_stream_smb_fname(smb_fname)
4786 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4787 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4788 if (rc == 0) {
4789 update_btime(handle, smb_fname);
4791 return rc;
4794 if (is_afpinfo_stream(smb_fname)) {
4795 rc = fruit_stat_meta(handle, smb_fname, false);
4796 } else if (is_afpresource_stream(smb_fname)) {
4797 rc = fruit_stat_rsrc(handle, smb_fname, false);
4798 } else {
4799 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4802 if (rc == 0) {
4803 update_btime(handle, smb_fname);
4804 smb_fname->st.st_ex_mode &= ~S_IFMT;
4805 smb_fname->st.st_ex_mode |= S_IFREG;
4806 smb_fname->st.st_ex_blocks =
4807 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4809 return rc;
4812 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4813 files_struct *fsp,
4814 SMB_STRUCT_STAT *sbuf)
4816 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4819 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4820 files_struct *fsp,
4821 SMB_STRUCT_STAT *sbuf)
4823 int ret;
4825 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4826 if (ret != 0) {
4827 return -1;
4830 *sbuf = fsp->base_fsp->fsp_name->st;
4831 sbuf->st_ex_size = AFP_INFO_SIZE;
4832 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4834 return 0;
4837 static int fruit_fstat_meta(vfs_handle_struct *handle,
4838 files_struct *fsp,
4839 SMB_STRUCT_STAT *sbuf,
4840 struct fio *fio)
4842 int ret;
4844 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4846 switch (fio->config->meta) {
4847 case FRUIT_META_STREAM:
4848 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4849 break;
4851 case FRUIT_META_NETATALK:
4852 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4853 break;
4855 default:
4856 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4857 return -1;
4860 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4861 return ret;
4864 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4865 files_struct *fsp,
4866 SMB_STRUCT_STAT *sbuf)
4868 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4871 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4872 files_struct *fsp,
4873 SMB_STRUCT_STAT *sbuf)
4875 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4878 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4879 files_struct *fsp,
4880 SMB_STRUCT_STAT *sbuf)
4882 struct adouble *ad = NULL;
4883 int ret;
4885 /* Populate the stat struct with info from the base file. */
4886 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4887 if (ret == -1) {
4888 return -1;
4891 ad = ad_get(talloc_tos(), handle,
4892 fsp->base_fsp->fsp_name,
4893 ADOUBLE_RSRC);
4894 if (ad == NULL) {
4895 DBG_ERR("ad_get [%s] failed [%s]\n",
4896 fsp_str_dbg(fsp), strerror(errno));
4897 return -1;
4900 *sbuf = fsp->base_fsp->fsp_name->st;
4901 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4902 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4904 TALLOC_FREE(ad);
4905 return 0;
4908 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4909 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4911 int ret;
4913 switch (fio->config->rsrc) {
4914 case FRUIT_RSRC_STREAM:
4915 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4916 break;
4918 case FRUIT_RSRC_ADFILE:
4919 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4920 break;
4922 case FRUIT_RSRC_XATTR:
4923 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4924 break;
4926 default:
4927 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4928 return -1;
4931 return ret;
4934 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4935 SMB_STRUCT_STAT *sbuf)
4937 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4938 int rc;
4940 if (fio == NULL) {
4941 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4944 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4946 if (fio->type == ADOUBLE_META) {
4947 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4948 } else {
4949 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4952 if (rc == 0) {
4953 sbuf->st_ex_mode &= ~S_IFMT;
4954 sbuf->st_ex_mode |= S_IFREG;
4955 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4958 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4959 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4960 return rc;
4963 static NTSTATUS delete_invalid_meta_stream(
4964 vfs_handle_struct *handle,
4965 const struct smb_filename *smb_fname,
4966 TALLOC_CTX *mem_ctx,
4967 unsigned int *pnum_streams,
4968 struct stream_struct **pstreams)
4970 struct smb_filename *sname = NULL;
4971 int ret;
4972 bool ok;
4974 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4975 if (!ok) {
4976 return NT_STATUS_INTERNAL_ERROR;
4979 sname = synthetic_smb_fname(talloc_tos(),
4980 smb_fname->base_name,
4981 AFPINFO_STREAM_NAME,
4982 NULL, 0);
4983 if (sname == NULL) {
4984 return NT_STATUS_NO_MEMORY;
4987 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
4988 TALLOC_FREE(sname);
4989 if (ret != 0) {
4990 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
4991 return map_nt_error_from_unix(errno);
4994 return NT_STATUS_OK;
4997 static NTSTATUS fruit_streaminfo_meta_stream(
4998 vfs_handle_struct *handle,
4999 struct files_struct *fsp,
5000 const struct smb_filename *smb_fname,
5001 TALLOC_CTX *mem_ctx,
5002 unsigned int *pnum_streams,
5003 struct stream_struct **pstreams)
5005 struct stream_struct *stream = *pstreams;
5006 unsigned int num_streams = *pnum_streams;
5007 struct smb_filename *sname = NULL;
5008 char *full_name = NULL;
5009 uint32_t name_hash;
5010 struct share_mode_lock *lck = NULL;
5011 struct file_id id = {0};
5012 bool delete_on_close_set;
5013 int i;
5014 int ret;
5015 NTSTATUS status;
5016 bool ok;
5018 for (i = 0; i < num_streams; i++) {
5019 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5020 break;
5024 if (i == num_streams) {
5025 return NT_STATUS_OK;
5028 if (stream[i].size != AFP_INFO_SIZE) {
5029 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5030 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5032 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5033 pnum_streams, pstreams);
5037 * Now check if there's a delete-on-close pending on the stream. If so,
5038 * hide the stream. This behaviour was verified against a macOS 10.12
5039 * SMB server.
5042 sname = synthetic_smb_fname(talloc_tos(),
5043 smb_fname->base_name,
5044 AFPINFO_STREAM_NAME,
5045 NULL, 0);
5046 if (sname == NULL) {
5047 status = NT_STATUS_NO_MEMORY;
5048 goto out;
5051 ret = SMB_VFS_NEXT_STAT(handle, sname);
5052 if (ret != 0) {
5053 status = map_nt_error_from_unix(errno);
5054 goto out;
5057 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5059 lck = get_existing_share_mode_lock(talloc_tos(), id);
5060 if (lck == NULL) {
5061 status = NT_STATUS_OK;
5062 goto out;
5065 full_name = talloc_asprintf(talloc_tos(),
5066 "%s%s",
5067 sname->base_name,
5068 AFPINFO_STREAM);
5069 if (full_name == NULL) {
5070 status = NT_STATUS_NO_MEMORY;
5071 goto out;
5074 status = file_name_hash(handle->conn, full_name, &name_hash);
5075 if (!NT_STATUS_IS_OK(status)) {
5076 goto out;
5079 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5080 if (delete_on_close_set) {
5081 ok = del_fruit_stream(mem_ctx,
5082 pnum_streams,
5083 pstreams,
5084 AFPINFO_STREAM);
5085 if (!ok) {
5086 status = NT_STATUS_INTERNAL_ERROR;
5087 goto out;
5091 status = NT_STATUS_OK;
5093 out:
5094 TALLOC_FREE(sname);
5095 TALLOC_FREE(lck);
5096 TALLOC_FREE(full_name);
5097 return status;
5100 static NTSTATUS fruit_streaminfo_meta_netatalk(
5101 vfs_handle_struct *handle,
5102 struct files_struct *fsp,
5103 const struct smb_filename *smb_fname,
5104 TALLOC_CTX *mem_ctx,
5105 unsigned int *pnum_streams,
5106 struct stream_struct **pstreams)
5108 struct stream_struct *stream = *pstreams;
5109 unsigned int num_streams = *pnum_streams;
5110 struct adouble *ad = NULL;
5111 bool is_fi_empty;
5112 int i;
5113 bool ok;
5115 /* Remove the Netatalk xattr from the list */
5116 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5117 ":" NETATALK_META_XATTR ":$DATA");
5118 if (!ok) {
5119 return NT_STATUS_NO_MEMORY;
5123 * Check if there's a AFPINFO_STREAM from the VFS streams
5124 * backend and if yes, remove it from the list
5126 for (i = 0; i < num_streams; i++) {
5127 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5128 break;
5132 if (i < num_streams) {
5133 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5134 smb_fname_str_dbg(smb_fname));
5136 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5137 AFPINFO_STREAM);
5138 if (!ok) {
5139 return NT_STATUS_INTERNAL_ERROR;
5143 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5144 if (ad == NULL) {
5145 return NT_STATUS_OK;
5148 is_fi_empty = ad_empty_finderinfo(ad);
5149 TALLOC_FREE(ad);
5151 if (is_fi_empty) {
5152 return NT_STATUS_OK;
5155 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5156 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5157 smb_roundup(handle->conn, AFP_INFO_SIZE));
5158 if (!ok) {
5159 return NT_STATUS_NO_MEMORY;
5162 return NT_STATUS_OK;
5165 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5166 struct files_struct *fsp,
5167 const struct smb_filename *smb_fname,
5168 TALLOC_CTX *mem_ctx,
5169 unsigned int *pnum_streams,
5170 struct stream_struct **pstreams)
5172 struct fruit_config_data *config = NULL;
5173 NTSTATUS status;
5175 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5176 return NT_STATUS_INTERNAL_ERROR);
5178 switch (config->meta) {
5179 case FRUIT_META_NETATALK:
5180 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5181 mem_ctx, pnum_streams,
5182 pstreams);
5183 break;
5185 case FRUIT_META_STREAM:
5186 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5187 mem_ctx, pnum_streams,
5188 pstreams);
5189 break;
5191 default:
5192 return NT_STATUS_INTERNAL_ERROR;
5195 return status;
5198 static NTSTATUS fruit_streaminfo_rsrc_stream(
5199 vfs_handle_struct *handle,
5200 struct files_struct *fsp,
5201 const struct smb_filename *smb_fname,
5202 TALLOC_CTX *mem_ctx,
5203 unsigned int *pnum_streams,
5204 struct stream_struct **pstreams)
5206 bool ok;
5208 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5209 if (!ok) {
5210 DBG_ERR("Filtering resource stream failed\n");
5211 return NT_STATUS_INTERNAL_ERROR;
5213 return NT_STATUS_OK;
5216 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5217 vfs_handle_struct *handle,
5218 struct files_struct *fsp,
5219 const struct smb_filename *smb_fname,
5220 TALLOC_CTX *mem_ctx,
5221 unsigned int *pnum_streams,
5222 struct stream_struct **pstreams)
5224 bool ok;
5226 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5227 if (!ok) {
5228 DBG_ERR("Filtering resource stream failed\n");
5229 return NT_STATUS_INTERNAL_ERROR;
5231 return NT_STATUS_OK;
5234 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5235 vfs_handle_struct *handle,
5236 struct files_struct *fsp,
5237 const struct smb_filename *smb_fname,
5238 TALLOC_CTX *mem_ctx,
5239 unsigned int *pnum_streams,
5240 struct stream_struct **pstreams)
5242 struct stream_struct *stream = *pstreams;
5243 unsigned int num_streams = *pnum_streams;
5244 struct adouble *ad = NULL;
5245 bool ok;
5246 size_t rlen;
5247 int i;
5250 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5251 * and if yes, remove it from the list
5253 for (i = 0; i < num_streams; i++) {
5254 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5255 break;
5259 if (i < num_streams) {
5260 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5261 smb_fname_str_dbg(smb_fname));
5263 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5264 AFPRESOURCE_STREAM);
5265 if (!ok) {
5266 return NT_STATUS_INTERNAL_ERROR;
5270 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5271 if (ad == NULL) {
5272 return NT_STATUS_OK;
5275 rlen = ad_getentrylen(ad, ADEID_RFORK);
5276 TALLOC_FREE(ad);
5278 if (rlen == 0) {
5279 return NT_STATUS_OK;
5282 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5283 AFPRESOURCE_STREAM_NAME, rlen,
5284 smb_roundup(handle->conn, rlen));
5285 if (!ok) {
5286 return NT_STATUS_NO_MEMORY;
5289 return NT_STATUS_OK;
5292 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5293 struct files_struct *fsp,
5294 const struct smb_filename *smb_fname,
5295 TALLOC_CTX *mem_ctx,
5296 unsigned int *pnum_streams,
5297 struct stream_struct **pstreams)
5299 struct fruit_config_data *config = NULL;
5300 NTSTATUS status;
5302 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5303 return NT_STATUS_INTERNAL_ERROR);
5305 switch (config->rsrc) {
5306 case FRUIT_RSRC_STREAM:
5307 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5308 mem_ctx, pnum_streams,
5309 pstreams);
5310 break;
5312 case FRUIT_RSRC_XATTR:
5313 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5314 mem_ctx, pnum_streams,
5315 pstreams);
5316 break;
5318 case FRUIT_RSRC_ADFILE:
5319 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5320 mem_ctx, pnum_streams,
5321 pstreams);
5322 break;
5324 default:
5325 return NT_STATUS_INTERNAL_ERROR;
5328 return status;
5331 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5332 struct files_struct *fsp,
5333 const struct smb_filename *smb_fname,
5334 TALLOC_CTX *mem_ctx,
5335 unsigned int *pnum_streams,
5336 struct stream_struct **pstreams)
5338 struct fruit_config_data *config = NULL;
5339 NTSTATUS status;
5341 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5342 return NT_STATUS_UNSUCCESSFUL);
5344 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5346 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5347 pnum_streams, pstreams);
5348 if (!NT_STATUS_IS_OK(status)) {
5349 return status;
5352 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5353 mem_ctx, pnum_streams, pstreams);
5354 if (!NT_STATUS_IS_OK(status)) {
5355 return status;
5358 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5359 mem_ctx, pnum_streams, pstreams);
5360 if (!NT_STATUS_IS_OK(status)) {
5361 return status;
5364 return NT_STATUS_OK;
5367 static int fruit_ntimes(vfs_handle_struct *handle,
5368 const struct smb_filename *smb_fname,
5369 struct smb_file_time *ft)
5371 int rc = 0;
5372 struct adouble *ad = NULL;
5373 struct fruit_config_data *config = NULL;
5375 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5376 return -1);
5378 if ((config->meta != FRUIT_META_NETATALK) ||
5379 null_timespec(ft->create_time))
5381 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5384 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5385 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5387 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5388 if (ad == NULL) {
5389 goto exit;
5392 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5393 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5395 rc = ad_set(ad, smb_fname);
5397 exit:
5399 TALLOC_FREE(ad);
5400 if (rc != 0) {
5401 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5402 return -1;
5404 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5407 static int fruit_fallocate(struct vfs_handle_struct *handle,
5408 struct files_struct *fsp,
5409 uint32_t mode,
5410 off_t offset,
5411 off_t len)
5413 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5415 if (fio == NULL) {
5416 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5419 /* Let the pwrite code path handle it. */
5420 errno = ENOSYS;
5421 return -1;
5424 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5425 struct files_struct *fsp,
5426 off_t offset)
5428 if (offset == 0) {
5429 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5432 #ifdef HAVE_ATTROPEN
5433 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5434 #endif
5435 return 0;
5438 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5439 struct files_struct *fsp,
5440 off_t offset)
5442 int rc;
5443 struct adouble *ad = NULL;
5444 off_t ad_off;
5446 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5447 if (ad == NULL) {
5448 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5449 fsp_str_dbg(fsp), strerror(errno));
5450 return -1;
5453 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5455 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5456 if (rc != 0) {
5457 TALLOC_FREE(ad);
5458 return -1;
5461 ad_setentrylen(ad, ADEID_RFORK, offset);
5463 rc = ad_fset(ad, fsp);
5464 if (rc != 0) {
5465 DBG_ERR("ad_fset [%s] failed [%s]\n",
5466 fsp_str_dbg(fsp), strerror(errno));
5467 TALLOC_FREE(ad);
5468 return -1;
5471 TALLOC_FREE(ad);
5472 return 0;
5475 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5476 struct files_struct *fsp,
5477 off_t offset)
5479 if (offset == 0) {
5480 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5483 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5486 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5487 struct files_struct *fsp,
5488 off_t offset)
5490 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5491 int ret;
5493 switch (fio->config->rsrc) {
5494 case FRUIT_RSRC_XATTR:
5495 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5496 break;
5498 case FRUIT_RSRC_ADFILE:
5499 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5500 break;
5502 case FRUIT_RSRC_STREAM:
5503 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5504 break;
5506 default:
5507 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5508 return -1;
5512 return ret;
5515 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5516 struct files_struct *fsp,
5517 off_t offset)
5519 if (offset > 60) {
5520 DBG_WARNING("ftruncate %s to %jd",
5521 fsp_str_dbg(fsp), (intmax_t)offset);
5522 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5523 errno = EOVERFLOW;
5524 return -1;
5527 /* OS X returns success but does nothing */
5528 DBG_INFO("ignoring ftruncate %s to %jd\n",
5529 fsp_str_dbg(fsp), (intmax_t)offset);
5530 return 0;
5533 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5534 struct files_struct *fsp,
5535 off_t offset)
5537 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5538 int ret;
5540 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5541 (intmax_t)offset);
5543 if (fio == NULL) {
5544 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5547 if (fio->type == ADOUBLE_META) {
5548 ret = fruit_ftruncate_meta(handle, fsp, offset);
5549 } else {
5550 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5553 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5554 return ret;
5557 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5558 struct smb_request *req,
5559 uint16_t root_dir_fid,
5560 struct smb_filename *smb_fname,
5561 uint32_t access_mask,
5562 uint32_t share_access,
5563 uint32_t create_disposition,
5564 uint32_t create_options,
5565 uint32_t file_attributes,
5566 uint32_t oplock_request,
5567 struct smb2_lease *lease,
5568 uint64_t allocation_size,
5569 uint32_t private_flags,
5570 struct security_descriptor *sd,
5571 struct ea_list *ea_list,
5572 files_struct **result,
5573 int *pinfo,
5574 const struct smb2_create_blobs *in_context_blobs,
5575 struct smb2_create_blobs *out_context_blobs)
5577 NTSTATUS status;
5578 struct fruit_config_data *config = NULL;
5579 files_struct *fsp = NULL;
5581 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5582 if (!NT_STATUS_IS_OK(status)) {
5583 goto fail;
5586 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5587 return NT_STATUS_UNSUCCESSFUL);
5589 status = SMB_VFS_NEXT_CREATE_FILE(
5590 handle, req, root_dir_fid, smb_fname,
5591 access_mask, share_access,
5592 create_disposition, create_options,
5593 file_attributes, oplock_request,
5594 lease,
5595 allocation_size, private_flags,
5596 sd, ea_list, result,
5597 pinfo, in_context_blobs, out_context_blobs);
5598 if (!NT_STATUS_IS_OK(status)) {
5599 return status;
5602 fsp = *result;
5604 if (global_fruit_config.nego_aapl) {
5605 if (config->posix_rename && fsp->is_directory) {
5607 * Enable POSIX directory rename behaviour
5609 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5614 * If this is a plain open for existing files, opening an 0
5615 * byte size resource fork MUST fail with
5616 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5618 * Cf the vfs_fruit torture tests in test_rfork_create().
5620 if (is_afpresource_stream(fsp->fsp_name) &&
5621 create_disposition == FILE_OPEN)
5623 if (fsp->fsp_name->st.st_ex_size == 0) {
5624 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5625 goto fail;
5629 if (is_ntfs_stream_smb_fname(smb_fname)
5630 || fsp->is_directory) {
5631 return status;
5634 if (config->locking == FRUIT_LOCKING_NETATALK) {
5635 status = fruit_check_access(
5636 handle, *result,
5637 access_mask,
5638 map_share_mode_to_deny_mode(share_access, 0));
5639 if (!NT_STATUS_IS_OK(status)) {
5640 goto fail;
5644 return status;
5646 fail:
5647 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5649 if (fsp) {
5650 close_file(req, fsp, ERROR_CLOSE);
5651 *result = fsp = NULL;
5654 return status;
5657 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5658 const struct smb_filename *fname,
5659 TALLOC_CTX *mem_ctx,
5660 struct readdir_attr_data **pattr_data)
5662 struct fruit_config_data *config = NULL;
5663 struct readdir_attr_data *attr_data;
5664 NTSTATUS status;
5666 SMB_VFS_HANDLE_GET_DATA(handle, config,
5667 struct fruit_config_data,
5668 return NT_STATUS_UNSUCCESSFUL);
5670 if (!global_fruit_config.nego_aapl) {
5671 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5674 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5676 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5677 if (*pattr_data == NULL) {
5678 return NT_STATUS_UNSUCCESSFUL;
5680 attr_data = *pattr_data;
5681 attr_data->type = RDATTR_AAPL;
5684 * Mac metadata: compressed FinderInfo, resource fork length
5685 * and creation date
5687 status = readdir_attr_macmeta(handle, fname, attr_data);
5688 if (!NT_STATUS_IS_OK(status)) {
5690 * Error handling is tricky: if we return failure from
5691 * this function, the corresponding directory entry
5692 * will to be passed to the client, so we really just
5693 * want to error out on fatal errors.
5695 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5696 goto fail;
5701 * UNIX mode
5703 if (config->unix_info_enabled) {
5704 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5708 * max_access
5710 if (!config->readdir_attr_max_access) {
5711 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5712 } else {
5713 status = smbd_calculate_access_mask(
5714 handle->conn,
5715 fname,
5716 false,
5717 SEC_FLAG_MAXIMUM_ALLOWED,
5718 &attr_data->attr_data.aapl.max_access);
5719 if (!NT_STATUS_IS_OK(status)) {
5720 goto fail;
5724 return NT_STATUS_OK;
5726 fail:
5727 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5728 fname->base_name, nt_errstr(status)));
5729 TALLOC_FREE(*pattr_data);
5730 return status;
5733 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5734 files_struct *fsp,
5735 uint32_t security_info,
5736 TALLOC_CTX *mem_ctx,
5737 struct security_descriptor **ppdesc)
5739 NTSTATUS status;
5740 struct security_ace ace;
5741 struct dom_sid sid;
5742 struct fruit_config_data *config;
5743 bool remove_ok = false;
5745 SMB_VFS_HANDLE_GET_DATA(handle, config,
5746 struct fruit_config_data,
5747 return NT_STATUS_UNSUCCESSFUL);
5749 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5750 mem_ctx, ppdesc);
5751 if (!NT_STATUS_IS_OK(status)) {
5752 return status;
5756 * Add MS NFS style ACEs with uid, gid and mode
5758 if (!global_fruit_config.nego_aapl) {
5759 return NT_STATUS_OK;
5761 if (!config->unix_info_enabled) {
5762 return NT_STATUS_OK;
5765 /* MS NFS style mode */
5766 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5767 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5769 /* First remove any existing ACE's with this SID. */
5770 status = security_descriptor_dacl_del(*ppdesc, &sid);
5771 remove_ok = (NT_STATUS_IS_OK(status) ||
5772 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
5773 if (!remove_ok) {
5774 DBG_WARNING("failed to remove MS NFS_mode style ACE\n");
5775 return status;
5777 status = security_descriptor_dacl_add(*ppdesc, &ace);
5778 if (!NT_STATUS_IS_OK(status)) {
5779 DEBUG(1,("failed to add MS NFS style ACE\n"));
5780 return status;
5783 /* MS NFS style uid */
5784 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5785 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5787 /* First remove any existing ACE's with this SID. */
5788 status = security_descriptor_dacl_del(*ppdesc, &sid);
5789 remove_ok = (NT_STATUS_IS_OK(status) ||
5790 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
5791 if (!remove_ok) {
5792 DBG_WARNING("failed to remove MS NFS_users style ACE\n");
5793 return status;
5795 status = security_descriptor_dacl_add(*ppdesc, &ace);
5796 if (!NT_STATUS_IS_OK(status)) {
5797 DEBUG(1,("failed to add MS NFS style ACE\n"));
5798 return status;
5801 /* MS NFS style gid */
5802 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5803 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5805 /* First remove any existing ACE's with this SID. */
5806 status = security_descriptor_dacl_del(*ppdesc, &sid);
5807 remove_ok = (NT_STATUS_IS_OK(status) ||
5808 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND));
5809 if (!remove_ok) {
5810 DBG_WARNING("failed to remove MS NFS_groups style ACE\n");
5811 return status;
5813 status = security_descriptor_dacl_add(*ppdesc, &ace);
5814 if (!NT_STATUS_IS_OK(status)) {
5815 DEBUG(1,("failed to add MS NFS style ACE\n"));
5816 return status;
5819 return NT_STATUS_OK;
5822 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5823 files_struct *fsp,
5824 uint32_t security_info_sent,
5825 const struct security_descriptor *orig_psd)
5827 NTSTATUS status;
5828 bool do_chmod;
5829 mode_t ms_nfs_mode = 0;
5830 int result;
5831 struct security_descriptor *psd = NULL;
5832 uint32_t orig_num_aces = 0;
5834 if (orig_psd->dacl != NULL) {
5835 orig_num_aces = orig_psd->dacl->num_aces;
5838 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5839 if (psd == NULL) {
5840 return NT_STATUS_NO_MEMORY;
5843 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5845 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5846 if (!NT_STATUS_IS_OK(status)) {
5847 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5848 TALLOC_FREE(psd);
5849 return status;
5853 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5854 * sent/present flags correctly now we've removed them.
5857 if (orig_num_aces != 0) {
5859 * Are there any ACE's left ?
5861 if (psd->dacl->num_aces == 0) {
5862 /* No - clear the DACL sent/present flags. */
5863 security_info_sent &= ~SECINFO_DACL;
5864 psd->type &= ~SEC_DESC_DACL_PRESENT;
5868 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5869 if (!NT_STATUS_IS_OK(status)) {
5870 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5871 TALLOC_FREE(psd);
5872 return status;
5875 if (do_chmod) {
5876 if (fsp->fh->fd != -1) {
5877 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5878 } else {
5879 result = SMB_VFS_CHMOD(fsp->conn,
5880 fsp->fsp_name,
5881 ms_nfs_mode);
5884 if (result != 0) {
5885 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5886 result, (unsigned)ms_nfs_mode,
5887 strerror(errno)));
5888 status = map_nt_error_from_unix(errno);
5889 TALLOC_FREE(psd);
5890 return status;
5894 TALLOC_FREE(psd);
5895 return NT_STATUS_OK;
5898 static struct vfs_offload_ctx *fruit_offload_ctx;
5900 struct fruit_offload_read_state {
5901 struct vfs_handle_struct *handle;
5902 struct tevent_context *ev;
5903 files_struct *fsp;
5904 uint32_t fsctl;
5905 DATA_BLOB token;
5908 static void fruit_offload_read_done(struct tevent_req *subreq);
5910 static struct tevent_req *fruit_offload_read_send(
5911 TALLOC_CTX *mem_ctx,
5912 struct tevent_context *ev,
5913 struct vfs_handle_struct *handle,
5914 files_struct *fsp,
5915 uint32_t fsctl,
5916 uint32_t ttl,
5917 off_t offset,
5918 size_t to_copy)
5920 struct tevent_req *req = NULL;
5921 struct tevent_req *subreq = NULL;
5922 struct fruit_offload_read_state *state = NULL;
5924 req = tevent_req_create(mem_ctx, &state,
5925 struct fruit_offload_read_state);
5926 if (req == NULL) {
5927 return NULL;
5929 *state = (struct fruit_offload_read_state) {
5930 .handle = handle,
5931 .ev = ev,
5932 .fsp = fsp,
5933 .fsctl = fsctl,
5936 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5937 fsctl, ttl, offset, to_copy);
5938 if (tevent_req_nomem(subreq, req)) {
5939 return tevent_req_post(req, ev);
5941 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5942 return req;
5945 static void fruit_offload_read_done(struct tevent_req *subreq)
5947 struct tevent_req *req = tevent_req_callback_data(
5948 subreq, struct tevent_req);
5949 struct fruit_offload_read_state *state = tevent_req_data(
5950 req, struct fruit_offload_read_state);
5951 NTSTATUS status;
5953 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5954 state->handle,
5955 state,
5956 &state->token);
5957 TALLOC_FREE(subreq);
5958 if (tevent_req_nterror(req, status)) {
5959 return;
5962 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5963 tevent_req_done(req);
5964 return;
5967 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5968 &fruit_offload_ctx);
5969 if (tevent_req_nterror(req, status)) {
5970 return;
5973 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5974 state->fsp,
5975 &state->token);
5976 if (tevent_req_nterror(req, status)) {
5977 return;
5980 tevent_req_done(req);
5981 return;
5984 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
5985 struct vfs_handle_struct *handle,
5986 TALLOC_CTX *mem_ctx,
5987 DATA_BLOB *token)
5989 struct fruit_offload_read_state *state = tevent_req_data(
5990 req, struct fruit_offload_read_state);
5991 NTSTATUS status;
5993 if (tevent_req_is_nterror(req, &status)) {
5994 tevent_req_received(req);
5995 return status;
5998 token->length = state->token.length;
5999 token->data = talloc_move(mem_ctx, &state->token.data);
6001 tevent_req_received(req);
6002 return NT_STATUS_OK;
6005 struct fruit_offload_write_state {
6006 struct vfs_handle_struct *handle;
6007 off_t copied;
6008 struct files_struct *src_fsp;
6009 struct files_struct *dst_fsp;
6010 bool is_copyfile;
6013 static void fruit_offload_write_done(struct tevent_req *subreq);
6014 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6015 TALLOC_CTX *mem_ctx,
6016 struct tevent_context *ev,
6017 uint32_t fsctl,
6018 DATA_BLOB *token,
6019 off_t transfer_offset,
6020 struct files_struct *dest_fsp,
6021 off_t dest_off,
6022 off_t num)
6024 struct tevent_req *req, *subreq;
6025 struct fruit_offload_write_state *state;
6026 NTSTATUS status;
6027 struct fruit_config_data *config;
6028 off_t src_off = transfer_offset;
6029 files_struct *src_fsp = NULL;
6030 off_t to_copy = num;
6031 bool copyfile_enabled = false;
6033 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6034 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6036 SMB_VFS_HANDLE_GET_DATA(handle, config,
6037 struct fruit_config_data,
6038 return NULL);
6040 req = tevent_req_create(mem_ctx, &state,
6041 struct fruit_offload_write_state);
6042 if (req == NULL) {
6043 return NULL;
6045 state->handle = handle;
6046 state->dst_fsp = dest_fsp;
6048 switch (fsctl) {
6049 case FSCTL_SRV_COPYCHUNK:
6050 case FSCTL_SRV_COPYCHUNK_WRITE:
6051 copyfile_enabled = config->copyfile_enabled;
6052 break;
6053 default:
6054 break;
6058 * Check if this a OS X copyfile style copychunk request with
6059 * a requested chunk count of 0 that was translated to a
6060 * offload_write_send VFS call overloading the parameters src_off
6061 * = dest_off = num = 0.
6063 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6064 status = vfs_offload_token_db_fetch_fsp(
6065 fruit_offload_ctx, token, &src_fsp);
6066 if (tevent_req_nterror(req, status)) {
6067 return tevent_req_post(req, ev);
6069 state->src_fsp = src_fsp;
6071 status = vfs_stat_fsp(src_fsp);
6072 if (tevent_req_nterror(req, status)) {
6073 return tevent_req_post(req, ev);
6076 to_copy = src_fsp->fsp_name->st.st_ex_size;
6077 state->is_copyfile = true;
6080 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6081 mem_ctx,
6083 fsctl,
6084 token,
6085 transfer_offset,
6086 dest_fsp,
6087 dest_off,
6088 to_copy);
6089 if (tevent_req_nomem(subreq, req)) {
6090 return tevent_req_post(req, ev);
6093 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6094 return req;
6097 static void fruit_offload_write_done(struct tevent_req *subreq)
6099 struct tevent_req *req = tevent_req_callback_data(
6100 subreq, struct tevent_req);
6101 struct fruit_offload_write_state *state = tevent_req_data(
6102 req, struct fruit_offload_write_state);
6103 NTSTATUS status;
6104 unsigned int num_streams = 0;
6105 struct stream_struct *streams = NULL;
6106 unsigned int i;
6107 struct smb_filename *src_fname_tmp = NULL;
6108 struct smb_filename *dst_fname_tmp = NULL;
6110 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6111 subreq,
6112 &state->copied);
6113 TALLOC_FREE(subreq);
6114 if (tevent_req_nterror(req, status)) {
6115 return;
6118 if (!state->is_copyfile) {
6119 tevent_req_done(req);
6120 return;
6124 * Now copy all remaining streams. We know the share supports
6125 * streams, because we're in vfs_fruit. We don't do this async
6126 * because streams are few and small.
6128 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6129 state->src_fsp->fsp_name,
6130 req, &num_streams, &streams);
6131 if (tevent_req_nterror(req, status)) {
6132 return;
6135 if (num_streams == 1) {
6136 /* There is always one stream, ::$DATA. */
6137 tevent_req_done(req);
6138 return;
6141 for (i = 0; i < num_streams; i++) {
6142 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6143 __func__, streams[i].name, (size_t)streams[i].size));
6145 src_fname_tmp = synthetic_smb_fname(
6146 req,
6147 state->src_fsp->fsp_name->base_name,
6148 streams[i].name,
6149 NULL,
6150 state->src_fsp->fsp_name->flags);
6151 if (tevent_req_nomem(src_fname_tmp, req)) {
6152 return;
6155 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6156 TALLOC_FREE(src_fname_tmp);
6157 continue;
6160 dst_fname_tmp = synthetic_smb_fname(
6161 req,
6162 state->dst_fsp->fsp_name->base_name,
6163 streams[i].name,
6164 NULL,
6165 state->dst_fsp->fsp_name->flags);
6166 if (tevent_req_nomem(dst_fname_tmp, req)) {
6167 TALLOC_FREE(src_fname_tmp);
6168 return;
6171 status = copy_file(req,
6172 state->handle->conn,
6173 src_fname_tmp,
6174 dst_fname_tmp,
6175 OPENX_FILE_CREATE_IF_NOT_EXIST,
6176 0, false);
6177 if (!NT_STATUS_IS_OK(status)) {
6178 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6179 smb_fname_str_dbg(src_fname_tmp),
6180 smb_fname_str_dbg(dst_fname_tmp),
6181 nt_errstr(status)));
6182 TALLOC_FREE(src_fname_tmp);
6183 TALLOC_FREE(dst_fname_tmp);
6184 tevent_req_nterror(req, status);
6185 return;
6188 TALLOC_FREE(src_fname_tmp);
6189 TALLOC_FREE(dst_fname_tmp);
6192 TALLOC_FREE(streams);
6193 TALLOC_FREE(src_fname_tmp);
6194 TALLOC_FREE(dst_fname_tmp);
6195 tevent_req_done(req);
6198 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6199 struct tevent_req *req,
6200 off_t *copied)
6202 struct fruit_offload_write_state *state = tevent_req_data(
6203 req, struct fruit_offload_write_state);
6204 NTSTATUS status;
6206 if (tevent_req_is_nterror(req, &status)) {
6207 DEBUG(1, ("server side copy chunk failed: %s\n",
6208 nt_errstr(status)));
6209 *copied = 0;
6210 tevent_req_received(req);
6211 return status;
6214 *copied = state->copied;
6215 tevent_req_received(req);
6217 return NT_STATUS_OK;
6220 static struct vfs_fn_pointers vfs_fruit_fns = {
6221 .connect_fn = fruit_connect,
6223 /* File operations */
6224 .chmod_fn = fruit_chmod,
6225 .chown_fn = fruit_chown,
6226 .unlink_fn = fruit_unlink,
6227 .rename_fn = fruit_rename,
6228 .rmdir_fn = fruit_rmdir,
6229 .open_fn = fruit_open,
6230 .pread_fn = fruit_pread,
6231 .pwrite_fn = fruit_pwrite,
6232 .pread_send_fn = fruit_pread_send,
6233 .pread_recv_fn = fruit_pread_recv,
6234 .pwrite_send_fn = fruit_pwrite_send,
6235 .pwrite_recv_fn = fruit_pwrite_recv,
6236 .stat_fn = fruit_stat,
6237 .lstat_fn = fruit_lstat,
6238 .fstat_fn = fruit_fstat,
6239 .streaminfo_fn = fruit_streaminfo,
6240 .ntimes_fn = fruit_ntimes,
6241 .ftruncate_fn = fruit_ftruncate,
6242 .fallocate_fn = fruit_fallocate,
6243 .create_file_fn = fruit_create_file,
6244 .readdir_attr_fn = fruit_readdir_attr,
6245 .offload_read_send_fn = fruit_offload_read_send,
6246 .offload_read_recv_fn = fruit_offload_read_recv,
6247 .offload_write_send_fn = fruit_offload_write_send,
6248 .offload_write_recv_fn = fruit_offload_write_recv,
6250 /* NT ACL operations */
6251 .fget_nt_acl_fn = fruit_fget_nt_acl,
6252 .fset_nt_acl_fn = fruit_fset_nt_acl,
6255 NTSTATUS vfs_fruit_init(TALLOC_CTX *);
6256 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6258 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6259 &vfs_fruit_fns);
6260 if (!NT_STATUS_IS_OK(ret)) {
6261 return ret;
6264 vfs_fruit_debug_level = debug_add_class("fruit");
6265 if (vfs_fruit_debug_level == -1) {
6266 vfs_fruit_debug_level = DBGC_VFS;
6267 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6268 "vfs_fruit_init"));
6269 } else {
6270 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6271 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
6274 return ret;