vfs_fruit: Don't unlink the main file
[Samba.git] / source3 / modules / vfs_fruit.c
blob1102059bbee90a74cb49e6434a1d55f7858a81cc
1 /*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/util/sys_rw.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "lib/util/tevent_unix.h"
35 #include "offload_token.h"
36 #include "string_replace.h"
39 * Enhanced OS X and Netatalk compatibility
40 * ========================================
42 * This modules takes advantage of vfs_streams_xattr and
43 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
44 * loaded in the correct order:
46 * vfs modules = catia fruit streams_xattr
48 * The module intercepts the OS X special streams "AFP_AfpInfo" and
49 * "AFP_Resource" and handles them in a special way. All other named
50 * streams are deferred to vfs_streams_xattr.
52 * The OS X client maps all NTFS illegal characters to the Unicode
53 * private range. This module optionally stores the charcters using
54 * their native ASCII encoding using vfs_catia. If you're not enabling
55 * this feature, you can skip catia from vfs modules.
57 * Finally, open modes are optionally checked against Netatalk AFP
58 * share modes.
60 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
61 * extended metadata for files and directories. This module optionally
62 * reads and stores this metadata in a way compatible with Netatalk 3
63 * which stores the metadata in an EA "org.netatalk.metadata". Cf
64 * source3/include/MacExtensions.h for a description of the binary
65 * blobs content.
67 * The "AFP_Resource" named stream may be arbitrarily large, thus it
68 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
69 * the only available filesystem where xattrs can be of any size and
70 * the OS supports using the file APIs for xattrs.
72 * The AFP_Resource stream is stored in an AppleDouble file prepending
73 * "._" to the filename. On Solaris with ZFS the stream is optionally
74 * stored in an EA "org.netatalk.resource".
77 * Extended Attributes
78 * ===================
80 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
81 * other protocols you may want to adjust the xattr names the VFS
82 * module vfs_streams_xattr uses for storing ADS's. This defaults to
83 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
84 * these module parameters:
86 * streams_xattr:prefix = user.
87 * streams_xattr:store_stream_type = false
90 * TODO
91 * ====
93 * - log diagnostic if any needed VFS module is not loaded
94 * (eg with lp_vfs_objects())
95 * - add tests
98 static int vfs_fruit_debug_level = DBGC_VFS;
100 static struct global_fruit_config {
101 bool nego_aapl; /* client negotiated AAPL */
103 } global_fruit_config;
105 #undef DBGC_CLASS
106 #define DBGC_CLASS vfs_fruit_debug_level
108 #define FRUIT_PARAM_TYPE_NAME "fruit"
109 #define ADOUBLE_NAME_PREFIX "._"
111 #define NETATALK_META_XATTR "org.netatalk.Metadata"
112 #define NETATALK_RSRC_XATTR "org.netatalk.ResourceFork"
114 #if defined(HAVE_ATTROPEN)
115 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
116 #define AFPRESOURCE_EA_NETATALK NETATALK_RSRC_XATTR
117 #else
118 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
119 #define AFPRESOURCE_EA_NETATALK "user." NETATALK_RSRC_XATTR
120 #endif
122 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
124 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
125 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
126 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
127 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
129 struct fruit_config_data {
130 enum fruit_rsrc rsrc;
131 enum fruit_meta meta;
132 enum fruit_locking locking;
133 enum fruit_encoding encoding;
134 bool use_aapl; /* config from smb.conf */
135 bool use_copyfile;
136 bool readdir_attr_enabled;
137 bool unix_info_enabled;
138 bool copyfile_enabled;
139 bool veto_appledouble;
140 bool posix_rename;
141 bool aapl_zero_file_id;
142 const char *model;
143 bool time_machine;
144 off_t time_machine_max_size;
147 * Additional options, all enabled by default,
148 * possibly useful for analyzing performance. The associated
149 * operations with each of them may be expensive, so having
150 * the chance to disable them individually gives a chance
151 * tweaking the setup for the particular usecase.
153 bool readdir_attr_rsize;
154 bool readdir_attr_finder_info;
155 bool readdir_attr_max_access;
158 static const struct enum_list fruit_rsrc[] = {
159 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
160 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
161 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
162 { -1, NULL}
165 static const struct enum_list fruit_meta[] = {
166 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
167 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
168 { -1, NULL}
171 static const struct enum_list fruit_locking[] = {
172 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
173 {FRUIT_LOCKING_NONE, "none"},
174 { -1, NULL}
177 static const struct enum_list fruit_encoding[] = {
178 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
179 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
180 { -1, NULL}
183 static const char *fruit_catia_maps =
184 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
185 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
186 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
187 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
188 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
189 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
190 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
191 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
192 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
193 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
194 "0x0d:0xf00d";
196 /*****************************************************************************
197 * Defines, functions and data structures that deal with AppleDouble
198 *****************************************************************************/
201 * There are two AppleDouble blobs we deal with:
203 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
204 * metadata in an xattr
206 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
207 * ._ files
209 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
211 /* Version info */
212 #define AD_VERSION2 0x00020000
213 #define AD_VERSION AD_VERSION2
216 * AppleDouble entry IDs.
218 #define ADEID_DFORK 1
219 #define ADEID_RFORK 2
220 #define ADEID_NAME 3
221 #define ADEID_COMMENT 4
222 #define ADEID_ICONBW 5
223 #define ADEID_ICONCOL 6
224 #define ADEID_FILEI 7
225 #define ADEID_FILEDATESI 8
226 #define ADEID_FINDERI 9
227 #define ADEID_MACFILEI 10
228 #define ADEID_PRODOSFILEI 11
229 #define ADEID_MSDOSFILEI 12
230 #define ADEID_SHORTNAME 13
231 #define ADEID_AFPFILEI 14
232 #define ADEID_DID 15
234 /* Private Netatalk entries */
235 #define ADEID_PRIVDEV 16
236 #define ADEID_PRIVINO 17
237 #define ADEID_PRIVSYN 18
238 #define ADEID_PRIVID 19
239 #define ADEID_MAX (ADEID_PRIVID + 1)
242 * These are the real ids for the private entries,
243 * as stored in the adouble file
245 #define AD_DEV 0x80444556
246 #define AD_INO 0x80494E4F
247 #define AD_SYN 0x8053594E
248 #define AD_ID 0x8053567E
250 /* Number of actually used entries */
251 #define ADEID_NUM_XATTR 8
252 #define ADEID_NUM_DOT_UND 2
253 #define ADEID_NUM_RSRC_XATTR 1
255 /* AppleDouble magic */
256 #define AD_APPLESINGLE_MAGIC 0x00051600
257 #define AD_APPLEDOUBLE_MAGIC 0x00051607
258 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
260 /* Sizes of relevant entry bits */
261 #define ADEDLEN_MAGIC 4
262 #define ADEDLEN_VERSION 4
263 #define ADEDLEN_FILLER 16
264 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
265 #define ADEDLEN_NENTRIES 2
266 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
267 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
268 #define AD_ENTRY_LEN_EID 4
269 #define AD_ENTRY_LEN_OFF 4
270 #define AD_ENTRY_LEN_LEN 4
271 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
273 /* Field widths */
274 #define ADEDLEN_NAME 255
275 #define ADEDLEN_COMMENT 200
276 #define ADEDLEN_FILEI 16
277 #define ADEDLEN_FINDERI 32
278 #define ADEDLEN_FILEDATESI 16
279 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
280 #define ADEDLEN_AFPFILEI 4
281 #define ADEDLEN_MACFILEI 4
282 #define ADEDLEN_PRODOSFILEI 8
283 #define ADEDLEN_MSDOSFILEI 2
284 #define ADEDLEN_DID 4
285 #define ADEDLEN_PRIVDEV 8
286 #define ADEDLEN_PRIVINO 8
287 #define ADEDLEN_PRIVSYN 8
288 #define ADEDLEN_PRIVID 4
290 /* Offsets */
291 #define ADEDOFF_MAGIC 0
292 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
293 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
294 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
296 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
297 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
298 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
299 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
300 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
301 ADEDLEN_FILEDATESI)
302 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
303 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
304 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
305 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
307 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
308 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
309 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
311 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
312 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
313 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
314 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
315 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
316 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
318 #if AD_DATASZ_XATTR != 402
319 #error bad size for AD_DATASZ_XATTR
320 #endif
322 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
323 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
324 ADEDLEN_FINDERI)
325 #if AD_DATASZ_DOT_UND != 82
326 #error bad size for AD_DATASZ_DOT_UND
327 #endif
330 * Sharemode locks fcntl() offsets
332 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
333 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
334 #else
335 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
336 #endif
337 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
339 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
340 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
341 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
342 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
343 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
344 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
345 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
346 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
347 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
348 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
350 /* Time stuff we overload the bits a little */
351 #define AD_DATE_CREATE 0
352 #define AD_DATE_MODIFY 4
353 #define AD_DATE_BACKUP 8
354 #define AD_DATE_ACCESS 12
355 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
356 AD_DATE_BACKUP | AD_DATE_ACCESS)
357 #define AD_DATE_UNIX (1 << 10)
358 #define AD_DATE_START 0x80000000
359 #define AD_DATE_DELTA 946684800
360 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
361 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
363 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
364 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
365 #define AD_XATTR_HDR_SIZE 36
366 #define AD_XATTR_MAX_HDR_SIZE 65536
368 /* Accessor macros */
369 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
370 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
371 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
372 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
375 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
376 * representation as well as the on-disk format.
378 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
379 * the length of the FinderInfo entry is larger then 32 bytes. It is then
380 * preceeded with 2 bytes padding.
382 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
385 struct ad_xattr_header {
386 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
387 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
388 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
389 uint32_t adx_data_start; /* file offset to attribute data area */
390 uint32_t adx_data_length; /* length of attribute data area */
391 uint32_t adx_reserved[3];
392 uint16_t adx_flags;
393 uint16_t adx_num_attrs;
396 /* On-disk entries are aligned on 4 byte boundaries */
397 struct ad_xattr_entry {
398 uint32_t adx_offset; /* file offset to data */
399 uint32_t adx_length; /* size of attribute data */
400 uint16_t adx_flags;
401 uint8_t adx_namelen; /* included the NULL terminator */
402 char *adx_name; /* NULL-terminated UTF-8 name */
405 struct ad_entry {
406 size_t ade_off;
407 size_t ade_len;
410 struct adouble {
411 vfs_handle_struct *ad_handle;
412 int ad_fd;
413 bool ad_opened;
414 adouble_type_t ad_type;
415 uint32_t ad_magic;
416 uint32_t ad_version;
417 struct ad_entry ad_eid[ADEID_MAX];
418 char *ad_data;
419 struct ad_xattr_header adx_header;
420 struct ad_xattr_entry *adx_entries;
423 struct ad_entry_order {
424 uint32_t id, offset, len;
427 /* Netatalk AppleDouble metadata xattr */
428 static const
429 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
430 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
431 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
432 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
433 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
434 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
435 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
436 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
437 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
438 {0, 0, 0}
441 /* AppleDouble resource fork file (the ones prefixed by "._") */
442 static const
443 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
444 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
445 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
446 {0, 0, 0}
450 * Fake AppleDouble entry oder for resource fork xattr. The xattr
451 * isn't an AppleDouble file, it simply contains the resource data,
452 * but in order to be able to use some API calls like ad_getentryoff()
453 * we build a fake/helper struct adouble with this entry order struct.
455 static const
456 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
457 {ADEID_RFORK, 0, 0},
458 {0, 0, 0}
461 /* Conversion from enumerated id to on-disk AppleDouble id */
462 #define AD_EID_DISK(a) (set_eid[a])
463 static const uint32_t set_eid[] = {
464 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
465 AD_DEV, AD_INO, AD_SYN, AD_ID
468 struct fio {
469 /* tcon config handle */
470 struct fruit_config_data *config;
472 /* Denote stream type, meta or rsrc */
473 adouble_type_t type;
477 * Forward declarations
479 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
480 adouble_type_t type);
481 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
482 static int ad_fset(struct adouble *ad, files_struct *fsp);
483 static int adouble_path(TALLOC_CTX *ctx,
484 const struct smb_filename *smb_fname__in,
485 struct smb_filename **ppsmb_fname_out);
486 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
487 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
488 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
492 * Return a pointer to an AppleDouble entry
494 * Returns NULL if the entry is not present
496 static char *ad_get_entry(const struct adouble *ad, int eid)
498 off_t off = ad_getentryoff(ad, eid);
499 size_t len = ad_getentrylen(ad, eid);
501 if (off == 0 || len == 0) {
502 return NULL;
505 return ad->ad_data + off;
509 * Get a date
511 static int ad_getdate(const struct adouble *ad,
512 unsigned int dateoff,
513 uint32_t *date)
515 bool xlate = (dateoff & AD_DATE_UNIX);
516 char *p = NULL;
518 dateoff &= AD_DATE_MASK;
519 p = ad_get_entry(ad, ADEID_FILEDATESI);
520 if (p == NULL) {
521 return -1;
524 if (dateoff > AD_DATE_ACCESS) {
525 return -1;
528 memcpy(date, p + dateoff, sizeof(uint32_t));
530 if (xlate) {
531 *date = AD_DATE_TO_UNIX(*date);
533 return 0;
537 * Set a date
539 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
541 bool xlate = (dateoff & AD_DATE_UNIX);
542 char *p = NULL;
544 p = ad_get_entry(ad, ADEID_FILEDATESI);
545 if (p == NULL) {
546 return -1;
549 dateoff &= AD_DATE_MASK;
550 if (xlate) {
551 date = AD_DATE_FROM_UNIX(date);
554 if (dateoff > AD_DATE_ACCESS) {
555 return -1;
558 memcpy(p + dateoff, &date, sizeof(date));
560 return 0;
565 * Map on-disk AppleDouble id to enumerated id
567 static uint32_t get_eid(uint32_t eid)
569 if (eid <= 15) {
570 return eid;
573 switch (eid) {
574 case AD_DEV:
575 return ADEID_PRIVDEV;
576 case AD_INO:
577 return ADEID_PRIVINO;
578 case AD_SYN:
579 return ADEID_PRIVSYN;
580 case AD_ID:
581 return ADEID_PRIVID;
582 default:
583 break;
586 return 0;
590 * Pack AppleDouble structure into data buffer
592 static bool ad_pack(struct adouble *ad)
594 uint32_t eid;
595 uint16_t nent;
596 uint32_t bufsize;
597 uint32_t offset = 0;
599 bufsize = talloc_get_size(ad->ad_data);
600 if (bufsize < AD_DATASZ_DOT_UND) {
601 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
602 return false;
605 if (offset + ADEDLEN_MAGIC < offset ||
606 offset + ADEDLEN_MAGIC >= bufsize) {
607 return false;
609 RSIVAL(ad->ad_data, offset, ad->ad_magic);
610 offset += ADEDLEN_MAGIC;
612 if (offset + ADEDLEN_VERSION < offset ||
613 offset + ADEDLEN_VERSION >= bufsize) {
614 return false;
616 RSIVAL(ad->ad_data, offset, ad->ad_version);
617 offset += ADEDLEN_VERSION;
619 if (offset + ADEDLEN_FILLER < offset ||
620 offset + ADEDLEN_FILLER >= bufsize) {
621 return false;
623 if (ad->ad_type == ADOUBLE_RSRC) {
624 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
626 offset += ADEDLEN_FILLER;
628 if (offset + ADEDLEN_NENTRIES < offset ||
629 offset + ADEDLEN_NENTRIES >= bufsize) {
630 return false;
632 offset += ADEDLEN_NENTRIES;
634 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
635 if (ad->ad_eid[eid].ade_off == 0) {
637 * ade_off is also used as indicator whether a
638 * specific entry is used or not
640 continue;
643 if (offset + AD_ENTRY_LEN_EID < offset ||
644 offset + AD_ENTRY_LEN_EID >= bufsize) {
645 return false;
647 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
648 offset += AD_ENTRY_LEN_EID;
650 if (offset + AD_ENTRY_LEN_OFF < offset ||
651 offset + AD_ENTRY_LEN_OFF >= bufsize) {
652 return false;
654 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
655 offset += AD_ENTRY_LEN_OFF;
657 if (offset + AD_ENTRY_LEN_LEN < offset ||
658 offset + AD_ENTRY_LEN_LEN >= bufsize) {
659 return false;
661 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
662 offset += AD_ENTRY_LEN_LEN;
664 nent++;
667 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
668 return false;
670 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
672 return true;
675 static bool ad_unpack_xattrs(struct adouble *ad)
677 struct ad_xattr_header *h = &ad->adx_header;
678 const char *p = ad->ad_data;
679 uint32_t hoff;
680 uint32_t i;
682 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
683 return true;
686 /* 2 bytes padding */
687 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
689 h->adx_magic = RIVAL(p, hoff + 0);
690 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
691 h->adx_total_size = RIVAL(p, hoff + 8);
692 h->adx_data_start = RIVAL(p, hoff + 12);
693 h->adx_data_length = RIVAL(p, hoff + 16);
694 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
695 h->adx_num_attrs = RSVAL(p, hoff + 34);
697 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
698 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
699 return false;
702 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
703 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
704 return false;
706 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
707 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
708 return false;
711 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
712 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
713 return false;
716 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
717 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
718 return false;
720 if ((h->adx_data_start + h->adx_data_length) >
721 ad->adx_header.adx_total_size)
723 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
724 return false;
727 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
728 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
729 return false;
732 if (h->adx_num_attrs == 0) {
733 return true;
736 ad->adx_entries = talloc_zero_array(
737 ad, struct ad_xattr_entry, h->adx_num_attrs);
738 if (ad->adx_entries == NULL) {
739 return false;
742 hoff += AD_XATTR_HDR_SIZE;
744 for (i = 0; i < h->adx_num_attrs; i++) {
745 struct ad_xattr_entry *e = &ad->adx_entries[i];
747 hoff = (hoff + 3) & ~3;
749 e->adx_offset = RIVAL(p, hoff + 0);
750 e->adx_length = RIVAL(p, hoff + 4);
751 e->adx_flags = RSVAL(p, hoff + 8);
752 e->adx_namelen = *(p + hoff + 10);
754 if (e->adx_offset >= ad->adx_header.adx_total_size) {
755 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
756 e->adx_offset);
757 return false;
760 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
761 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
762 e->adx_length);
763 return false;
766 if ((e->adx_offset + e->adx_length) >
767 ad->adx_header.adx_total_size)
769 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
770 e->adx_length);
771 return false;
774 if (e->adx_namelen == 0) {
775 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
776 e->adx_namelen);
777 return false;
779 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
780 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
781 e->adx_namelen);
782 return false;
784 if ((hoff + 11 + e->adx_namelen) >
785 ad->adx_header.adx_data_start)
787 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
788 e->adx_namelen);
789 return false;
792 e->adx_name = talloc_strndup(ad->adx_entries,
793 p + hoff + 11,
794 e->adx_namelen);
795 if (e->adx_name == NULL) {
796 return false;
799 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
800 e->adx_name, e->adx_offset, e->adx_length);
801 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
802 e->adx_length);
804 hoff += 11 + e->adx_namelen;
807 return true;
811 * Unpack an AppleDouble blob into a struct adoble
813 static bool ad_unpack(struct adouble *ad, const size_t nentries,
814 size_t filesize)
816 size_t bufsize = talloc_get_size(ad->ad_data);
817 size_t adentries, i;
818 uint32_t eid, len, off;
819 bool ok;
822 * The size of the buffer ad->ad_data is checked when read, so
823 * we wouldn't have to check our own offsets, a few extra
824 * checks won't hurt though. We have to check the offsets we
825 * read from the buffer anyway.
828 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
829 DEBUG(1, ("bad size\n"));
830 return false;
833 ad->ad_magic = RIVAL(ad->ad_data, 0);
834 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
835 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
836 DEBUG(1, ("wrong magic or version\n"));
837 return false;
840 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
841 if (adentries != nentries) {
842 DEBUG(1, ("invalid number of entries: %zu\n",
843 adentries));
844 return false;
847 /* now, read in the entry bits */
848 for (i = 0; i < adentries; i++) {
849 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
850 eid = get_eid(eid);
851 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
852 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
854 if (!eid || eid >= ADEID_MAX) {
855 DEBUG(1, ("bogus eid %d\n", eid));
856 return false;
860 * All entries other than the resource fork are
861 * expected to be read into the ad_data buffer, so
862 * ensure the specified offset is within that bound
864 if ((off > bufsize) && (eid != ADEID_RFORK)) {
865 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
866 eid, off, len));
867 return false;
871 * All entries besides FinderInfo and resource fork
872 * must fit into the buffer. FinderInfo is special as
873 * it may be larger then the default 32 bytes (if it
874 * contains marshalled xattrs), but we will fixup that
875 * in ad_convert(). And the resource fork is never
876 * accessed directly by the ad_data buf (also see
877 * comment above) anyway.
879 if ((eid != ADEID_RFORK) &&
880 (eid != ADEID_FINDERI) &&
881 ((off + len) > bufsize)) {
882 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
883 eid, off, len));
884 return false;
888 * That would be obviously broken
890 if (off > filesize) {
891 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
892 eid, off, len));
893 return false;
897 * Check for any entry that has its end beyond the
898 * filesize.
900 if (off + len < off) {
901 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
902 ", len: %" PRIu32 "\n",
903 eid, off, len));
904 return false;
907 if (off + len > filesize) {
909 * If this is the resource fork entry, we fix
910 * up the length, for any other entry we bail
911 * out.
913 if (eid != ADEID_RFORK) {
914 DEBUG(1, ("bogus eid %d: off: %" PRIu32
915 ", len: %" PRIu32 "\n",
916 eid, off, len));
917 return false;
921 * Fixup the resource fork entry by limiting
922 * the size to entryoffset - filesize.
924 len = filesize - off;
925 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
926 ", len: %" PRIu32 "\n", off, len));
929 ad->ad_eid[eid].ade_off = off;
930 ad->ad_eid[eid].ade_len = len;
933 ok = ad_unpack_xattrs(ad);
934 if (!ok) {
935 return false;
938 return true;
941 static bool ad_convert_xattr(struct adouble *ad,
942 const struct smb_filename *smb_fname,
943 char *map)
945 static struct char_mappings **string_replace_cmaps = NULL;
946 uint16_t i;
947 int saved_errno = 0;
948 NTSTATUS status;
950 if (ad->adx_header.adx_num_attrs == 0) {
951 return true;
954 if (string_replace_cmaps == NULL) {
955 const char **mappings = NULL;
957 mappings = str_list_make_v3_const(
958 talloc_tos(), fruit_catia_maps, NULL);
959 if (mappings == NULL) {
960 return false;
962 string_replace_cmaps = string_replace_init_map(mappings);
963 TALLOC_FREE(mappings);
966 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
967 struct ad_xattr_entry *e = &ad->adx_entries[i];
968 char *mapped_name = NULL;
969 char *tmp = NULL;
970 struct smb_filename *stream_name = NULL;
971 files_struct *fsp = NULL;
972 ssize_t nwritten;
974 status = string_replace_allocate(ad->ad_handle->conn,
975 e->adx_name,
976 string_replace_cmaps,
977 talloc_tos(),
978 &mapped_name,
979 vfs_translate_to_windows);
980 if (!NT_STATUS_IS_OK(status) &&
981 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
983 DBG_ERR("string_replace_allocate failed\n");
984 return -1;
987 tmp = mapped_name;
988 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
989 TALLOC_FREE(tmp);
990 if (mapped_name == NULL) {
991 return -1;
994 stream_name = synthetic_smb_fname(talloc_tos(),
995 smb_fname->base_name,
996 mapped_name,
997 NULL,
998 smb_fname->flags);
999 TALLOC_FREE(mapped_name);
1000 if (stream_name == NULL) {
1001 DBG_ERR("synthetic_smb_fname failed\n");
1002 return -1;
1005 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1007 status = SMB_VFS_CREATE_FILE(
1008 ad->ad_handle->conn, /* conn */
1009 NULL, /* req */
1010 0, /* root_dir_fid */
1011 stream_name, /* fname */
1012 FILE_GENERIC_WRITE, /* access_mask */
1013 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1014 FILE_OPEN_IF, /* create_disposition */
1015 0, /* create_options */
1016 0, /* file_attributes */
1017 INTERNAL_OPEN_ONLY, /* oplock_request */
1018 NULL, /* lease */
1019 0, /* allocation_size */
1020 0, /* private_flags */
1021 NULL, /* sd */
1022 NULL, /* ea_list */
1023 &fsp, /* result */
1024 NULL, /* psbuf */
1025 NULL, NULL); /* create context */
1026 TALLOC_FREE(stream_name);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1029 return -1;
1032 nwritten = SMB_VFS_PWRITE(fsp,
1033 map + e->adx_offset,
1034 e->adx_length,
1036 if (nwritten == -1) {
1037 DBG_ERR("SMB_VFS_PWRITE failed\n");
1038 saved_errno = errno;
1039 close_file(NULL, fsp, ERROR_CLOSE);
1040 errno = saved_errno;
1041 return -1;
1044 status = close_file(NULL, fsp, NORMAL_CLOSE);
1045 if (!NT_STATUS_IS_OK(status)) {
1046 return -1;
1048 fsp = NULL;
1051 return true;
1055 * Convert from Apple's ._ file to Netatalk
1057 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1058 * bytes containing packed xattrs. Netatalk can't deal with that, so
1059 * we simply discard the packed xattrs.
1061 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1062 * otherwise
1064 static int ad_convert(struct adouble *ad,
1065 const struct smb_filename *smb_fname,
1066 int fd)
1068 int rc = 0;
1069 char *map = MAP_FAILED;
1070 size_t origlen;
1071 bool ok;
1073 origlen = ad_getentryoff(ad, ADEID_RFORK) +
1074 ad_getentrylen(ad, ADEID_RFORK);
1076 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1077 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1078 if (map == MAP_FAILED) {
1079 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
1080 rc = -1;
1081 goto exit;
1084 ok = ad_convert_xattr(ad, smb_fname, map);
1085 if (!ok) {
1086 munmap(map, origlen);
1087 return -1;
1090 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
1091 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
1092 map + ad_getentryoff(ad, ADEID_RFORK),
1093 ad_getentrylen(ad, ADEID_RFORK));
1096 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1097 ad_setentryoff(ad, ADEID_RFORK,
1098 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
1101 * FIXME: direct ftruncate(), but we don't have a fsp for the
1102 * VFS call
1104 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
1105 + ad_getentrylen(ad, ADEID_RFORK));
1107 exit:
1108 if (map != MAP_FAILED) {
1109 munmap(map, origlen);
1111 return rc;
1115 * Read and parse Netatalk AppleDouble metadata xattr
1117 static ssize_t ad_read_meta(struct adouble *ad,
1118 const struct smb_filename *smb_fname)
1120 int rc = 0;
1121 ssize_t ealen;
1122 bool ok;
1124 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1126 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1127 AFPINFO_EA_NETATALK, ad->ad_data,
1128 AD_DATASZ_XATTR);
1129 if (ealen == -1) {
1130 switch (errno) {
1131 case ENOATTR:
1132 case ENOENT:
1133 if (errno == ENOATTR) {
1134 errno = ENOENT;
1136 rc = -1;
1137 goto exit;
1138 default:
1139 DEBUG(2, ("error reading meta xattr: %s\n",
1140 strerror(errno)));
1141 rc = -1;
1142 goto exit;
1145 if (ealen != AD_DATASZ_XATTR) {
1146 DEBUG(2, ("bad size %zd\n", ealen));
1147 errno = EINVAL;
1148 rc = -1;
1149 goto exit;
1152 /* Now parse entries */
1153 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1154 if (!ok) {
1155 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1156 errno = EINVAL;
1157 rc = -1;
1158 goto exit;
1161 if (!ad_getentryoff(ad, ADEID_FINDERI)
1162 || !ad_getentryoff(ad, ADEID_COMMENT)
1163 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1164 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1165 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1166 || !ad_getentryoff(ad, ADEID_PRIVINO)
1167 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1168 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1169 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1170 errno = EINVAL;
1171 rc = -1;
1172 goto exit;
1175 exit:
1176 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1177 smb_fname->base_name, rc));
1179 if (rc != 0) {
1180 ealen = -1;
1181 if (errno == EINVAL) {
1182 become_root();
1183 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1184 unbecome_root();
1185 errno = ENOENT;
1188 return ealen;
1191 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1192 int flags,
1193 mode_t mode)
1195 #ifdef HAVE_ATTROPEN
1196 /* FIXME: direct Solaris xattr syscall */
1197 return attropen(smb_fname->base_name,
1198 AFPRESOURCE_EA_NETATALK, flags, mode);
1199 #else
1200 errno = ENOSYS;
1201 return -1;
1202 #endif
1205 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1206 int flags,
1207 mode_t mode)
1209 int ret;
1210 int fd;
1211 struct smb_filename *adp_smb_fname = NULL;
1213 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1214 if (ret != 0) {
1215 return -1;
1218 fd = open(adp_smb_fname->base_name, flags, mode);
1219 TALLOC_FREE(adp_smb_fname);
1221 return fd;
1224 static int ad_open_rsrc(vfs_handle_struct *handle,
1225 const struct smb_filename *smb_fname,
1226 int flags,
1227 mode_t mode)
1229 struct fruit_config_data *config = NULL;
1230 int fd;
1232 SMB_VFS_HANDLE_GET_DATA(handle, config,
1233 struct fruit_config_data, return -1);
1235 if (config->rsrc == FRUIT_RSRC_XATTR) {
1236 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1237 } else {
1238 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1241 return fd;
1245 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1246 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1247 * for file IO on the ._ file.
1249 static int ad_open(vfs_handle_struct *handle,
1250 struct adouble *ad,
1251 files_struct *fsp,
1252 const struct smb_filename *smb_fname,
1253 int flags,
1254 mode_t mode)
1256 int fd;
1258 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1259 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1261 if (ad->ad_type == ADOUBLE_META) {
1262 return 0;
1265 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1266 ad->ad_fd = fsp->fh->fd;
1267 ad->ad_opened = false;
1268 return 0;
1271 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1272 if (fd == -1) {
1273 return -1;
1275 ad->ad_opened = true;
1276 ad->ad_fd = fd;
1278 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1279 smb_fname->base_name,
1280 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1282 return 0;
1285 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1287 int ret;
1288 SMB_STRUCT_STAT st;
1290 /* FIXME: direct sys_fstat(), don't have an fsp */
1291 ret = sys_fstat(ad->ad_fd, &st,
1292 lp_fake_directory_create_times(
1293 SNUM(ad->ad_handle->conn)));
1294 if (ret != 0) {
1295 return -1;
1298 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1299 return st.st_ex_size;
1302 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1303 const struct smb_filename *smb_fname)
1305 SMB_STRUCT_STAT sbuf;
1306 char *p_ad = NULL;
1307 AfpInfo *ai = NULL;
1308 DATA_BLOB aiblob;
1309 struct smb_filename *stream_name = NULL;
1310 files_struct *fsp = NULL;
1311 ssize_t len;
1312 size_t size;
1313 ssize_t nwritten;
1314 NTSTATUS status;
1315 int saved_errno = 0;
1316 int ret;
1317 bool ok;
1319 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1320 SNUM(ad->ad_handle->conn)));
1321 if (ret != 0) {
1322 return -1;
1326 * AppleDouble file header content and size, two cases:
1328 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1329 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1331 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1333 size = sbuf.st_ex_size;
1334 if (size > talloc_array_length(ad->ad_data)) {
1335 if (size > AD_XATTR_MAX_HDR_SIZE) {
1336 size = AD_XATTR_MAX_HDR_SIZE;
1338 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1339 if (p_ad == NULL) {
1340 return -1;
1342 ad->ad_data = p_ad;
1345 len = sys_pread(ad->ad_fd, ad->ad_data,
1346 talloc_array_length(ad->ad_data), 0);
1347 if (len != talloc_array_length(ad->ad_data)) {
1348 DBG_NOTICE("%s %s: bad size: %zd\n",
1349 smb_fname->base_name, strerror(errno), len);
1350 return -1;
1353 /* Now parse entries */
1354 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1355 if (!ok) {
1356 DBG_ERR("invalid AppleDouble resource %s\n",
1357 smb_fname->base_name);
1358 errno = EINVAL;
1359 return -1;
1362 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1363 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1364 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1365 DBG_ERR("invalid AppleDouble resource %s\n",
1366 smb_fname->base_name);
1367 errno = EINVAL;
1368 return -1;
1371 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1372 return len;
1376 * Try to fixup AppleDouble files created by OS X with xattrs
1377 * appended to the ADEID_FINDERI entry. We simply remove the
1378 * xattrs blob, this means any fancy xattr that was stored
1379 * there is lost.
1382 ret = ad_convert(ad, smb_fname, ad->ad_fd);
1383 if (ret != 0) {
1384 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1385 return len;
1388 ok = ad_pack(ad);
1389 if (!ok) {
1390 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1391 return -1;
1394 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1395 if (len != AD_DATASZ_DOT_UND) {
1396 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1397 return -1;
1400 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1401 if (p_ad == NULL) {
1402 return -1;
1405 ai = afpinfo_new(talloc_tos());
1406 if (ai == NULL) {
1407 return -1;
1410 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1412 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1413 if (aiblob.data == NULL) {
1414 TALLOC_FREE(ai);
1415 return -1;
1418 size = afpinfo_pack(ai, (char *)aiblob.data);
1419 TALLOC_FREE(ai);
1420 if (size != AFP_INFO_SIZE) {
1421 return -1;
1424 stream_name = synthetic_smb_fname(talloc_tos(),
1425 smb_fname->base_name,
1426 AFPINFO_STREAM,
1427 NULL,
1428 smb_fname->flags);
1429 if (stream_name == NULL) {
1430 data_blob_free(&aiblob);
1431 DBG_ERR("synthetic_smb_fname failed\n");
1432 return -1;
1435 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1437 status = SMB_VFS_CREATE_FILE(
1438 ad->ad_handle->conn, /* conn */
1439 NULL, /* req */
1440 0, /* root_dir_fid */
1441 stream_name, /* fname */
1442 FILE_GENERIC_WRITE, /* access_mask */
1443 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1444 FILE_OPEN_IF, /* create_disposition */
1445 0, /* create_options */
1446 0, /* file_attributes */
1447 INTERNAL_OPEN_ONLY, /* oplock_request */
1448 NULL, /* lease */
1449 0, /* allocation_size */
1450 0, /* private_flags */
1451 NULL, /* sd */
1452 NULL, /* ea_list */
1453 &fsp, /* result */
1454 NULL, /* psbuf */
1455 NULL, NULL); /* create context */
1456 TALLOC_FREE(stream_name);
1457 if (!NT_STATUS_IS_OK(status)) {
1458 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1459 return -1;
1462 nwritten = SMB_VFS_PWRITE(fsp,
1463 aiblob.data,
1464 aiblob.length,
1466 if (nwritten == -1) {
1467 DBG_ERR("SMB_VFS_PWRITE failed\n");
1468 saved_errno = errno;
1469 close_file(NULL, fsp, ERROR_CLOSE);
1470 errno = saved_errno;
1471 return -1;
1474 status = close_file(NULL, fsp, NORMAL_CLOSE);
1475 if (!NT_STATUS_IS_OK(status)) {
1476 return -1;
1478 fsp = NULL;
1480 return len;
1484 * Read and parse resource fork, either ._ AppleDouble file or xattr
1486 static ssize_t ad_read_rsrc(struct adouble *ad,
1487 const struct smb_filename *smb_fname)
1489 struct fruit_config_data *config = NULL;
1490 ssize_t len;
1492 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1493 struct fruit_config_data, return -1);
1495 if (config->rsrc == FRUIT_RSRC_XATTR) {
1496 len = ad_read_rsrc_xattr(ad);
1497 } else {
1498 len = ad_read_rsrc_adouble(ad, smb_fname);
1501 return len;
1505 * Read and unpack an AppleDouble metadata xattr or resource
1507 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1509 switch (ad->ad_type) {
1510 case ADOUBLE_META:
1511 return ad_read_meta(ad, smb_fname);
1512 case ADOUBLE_RSRC:
1513 return ad_read_rsrc(ad, smb_fname);
1514 default:
1515 return -1;
1519 static int adouble_destructor(struct adouble *ad)
1521 if ((ad->ad_fd != -1) && ad->ad_opened) {
1522 close(ad->ad_fd);
1523 ad->ad_fd = -1;
1525 return 0;
1529 * Allocate a struct adouble without initialiing it
1531 * The struct is either hang of the fsp extension context or if fsp is
1532 * NULL from ctx.
1534 * @param[in] ctx talloc context
1535 * @param[in] handle vfs handle
1536 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1538 * @return adouble handle
1540 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1541 adouble_type_t type)
1543 int rc = 0;
1544 size_t adsize = 0;
1545 struct adouble *ad;
1546 struct fruit_config_data *config;
1548 SMB_VFS_HANDLE_GET_DATA(handle, config,
1549 struct fruit_config_data, return NULL);
1551 switch (type) {
1552 case ADOUBLE_META:
1553 adsize = AD_DATASZ_XATTR;
1554 break;
1555 case ADOUBLE_RSRC:
1556 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1557 adsize = AD_DATASZ_DOT_UND;
1559 break;
1560 default:
1561 return NULL;
1564 ad = talloc_zero(ctx, struct adouble);
1565 if (ad == NULL) {
1566 rc = -1;
1567 goto exit;
1570 if (adsize) {
1571 ad->ad_data = talloc_zero_array(ad, char, adsize);
1572 if (ad->ad_data == NULL) {
1573 rc = -1;
1574 goto exit;
1578 ad->ad_handle = handle;
1579 ad->ad_type = type;
1580 ad->ad_magic = AD_MAGIC;
1581 ad->ad_version = AD_VERSION;
1582 ad->ad_fd = -1;
1584 talloc_set_destructor(ad, adouble_destructor);
1586 exit:
1587 if (rc != 0) {
1588 TALLOC_FREE(ad);
1590 return ad;
1594 * Allocate and initialize a new struct adouble
1596 * @param[in] ctx talloc context
1597 * @param[in] handle vfs handle
1598 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1600 * @return adouble handle, initialized
1602 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1603 adouble_type_t type)
1605 int rc = 0;
1606 const struct ad_entry_order *eid;
1607 struct adouble *ad = NULL;
1608 struct fruit_config_data *config;
1609 time_t t = time(NULL);
1611 SMB_VFS_HANDLE_GET_DATA(handle, config,
1612 struct fruit_config_data, return NULL);
1614 switch (type) {
1615 case ADOUBLE_META:
1616 eid = entry_order_meta_xattr;
1617 break;
1618 case ADOUBLE_RSRC:
1619 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1620 eid = entry_order_dot_und;
1621 } else {
1622 eid = entry_order_rsrc_xattr;
1624 break;
1625 default:
1626 return NULL;
1629 ad = ad_alloc(ctx, handle, type);
1630 if (ad == NULL) {
1631 return NULL;
1634 while (eid->id) {
1635 ad->ad_eid[eid->id].ade_off = eid->offset;
1636 ad->ad_eid[eid->id].ade_len = eid->len;
1637 eid++;
1640 /* put something sane in the date fields */
1641 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1642 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1643 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1644 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1646 if (rc != 0) {
1647 TALLOC_FREE(ad);
1649 return ad;
1652 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1653 vfs_handle_struct *handle,
1654 files_struct *fsp,
1655 const struct smb_filename *smb_fname,
1656 adouble_type_t type)
1658 int rc = 0;
1659 ssize_t len;
1660 struct adouble *ad = NULL;
1661 int mode;
1663 if (fsp != NULL) {
1664 smb_fname = fsp->base_fsp->fsp_name;
1667 DEBUG(10, ("ad_get(%s) called for %s\n",
1668 type == ADOUBLE_META ? "meta" : "rsrc",
1669 smb_fname->base_name));
1671 ad = ad_alloc(ctx, handle, type);
1672 if (ad == NULL) {
1673 rc = -1;
1674 goto exit;
1677 /* Try rw first so we can use the fd in ad_convert() */
1678 mode = O_RDWR;
1680 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1681 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1682 mode = O_RDONLY;
1683 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1685 if (rc == -1) {
1686 DBG_DEBUG("ad_open [%s] error [%s]\n",
1687 smb_fname->base_name, strerror(errno));
1688 goto exit;
1692 len = ad_read(ad, smb_fname);
1693 if (len == -1) {
1694 DEBUG(10, ("error reading AppleDouble for %s\n",
1695 smb_fname->base_name));
1696 rc = -1;
1697 goto exit;
1700 exit:
1701 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1702 type == ADOUBLE_META ? "meta" : "rsrc",
1703 smb_fname->base_name, rc));
1705 if (rc != 0) {
1706 TALLOC_FREE(ad);
1708 return ad;
1712 * Return AppleDouble data for a file
1714 * @param[in] ctx talloc context
1715 * @param[in] handle vfs handle
1716 * @param[in] smb_fname pathname to file or directory
1717 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1719 * @return talloced struct adouble or NULL on error
1721 static struct adouble *ad_get(TALLOC_CTX *ctx,
1722 vfs_handle_struct *handle,
1723 const struct smb_filename *smb_fname,
1724 adouble_type_t type)
1726 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1730 * Return AppleDouble data for a file
1732 * @param[in] ctx talloc context
1733 * @param[in] handle vfs handle
1734 * @param[in] fsp fsp to use for IO
1735 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1737 * @return talloced struct adouble or NULL on error
1739 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1740 files_struct *fsp, adouble_type_t type)
1742 return ad_get_internal(ctx, handle, fsp, NULL, type);
1746 * Set AppleDouble metadata on a file or directory
1748 * @param[in] ad adouble handle
1750 * @param[in] smb_fname pathname to file or directory
1752 * @return status code, 0 means success
1754 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1756 bool ok;
1757 int ret;
1759 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1761 if (ad->ad_type != ADOUBLE_META) {
1762 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1763 smb_fname->base_name);
1764 return -1;
1767 ok = ad_pack(ad);
1768 if (!ok) {
1769 return -1;
1772 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1773 smb_fname,
1774 AFPINFO_EA_NETATALK,
1775 ad->ad_data,
1776 AD_DATASZ_XATTR, 0);
1778 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1780 return ret;
1784 * Set AppleDouble metadata on a file or directory
1786 * @param[in] ad adouble handle
1787 * @param[in] fsp file handle
1789 * @return status code, 0 means success
1791 static int ad_fset(struct adouble *ad, files_struct *fsp)
1793 int rc = -1;
1794 ssize_t len;
1795 bool ok;
1797 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1799 if ((fsp == NULL)
1800 || (fsp->fh == NULL)
1801 || (fsp->fh->fd == -1))
1803 smb_panic("bad fsp");
1806 ok = ad_pack(ad);
1807 if (!ok) {
1808 return -1;
1811 switch (ad->ad_type) {
1812 case ADOUBLE_META:
1813 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1814 fsp->fsp_name,
1815 AFPINFO_EA_NETATALK,
1816 ad->ad_data,
1817 AD_DATASZ_XATTR, 0);
1818 break;
1820 case ADOUBLE_RSRC:
1821 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1822 fsp,
1823 ad->ad_data,
1824 AD_DATASZ_DOT_UND,
1826 if (len != AD_DATASZ_DOT_UND) {
1827 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1828 return -1;
1830 rc = 0;
1831 break;
1833 default:
1834 return -1;
1837 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1839 return rc;
1842 /*****************************************************************************
1843 * Helper functions
1844 *****************************************************************************/
1846 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1848 if (strncasecmp_m(smb_fname->stream_name,
1849 AFPINFO_STREAM_NAME,
1850 strlen(AFPINFO_STREAM_NAME)) == 0) {
1851 return true;
1853 return false;
1856 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1858 if (strncasecmp_m(smb_fname->stream_name,
1859 AFPRESOURCE_STREAM_NAME,
1860 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1861 return true;
1863 return false;
1867 * Test whether stream is an Apple stream, not used atm
1869 #if 0
1870 static bool is_apple_stream(const struct smb_filename *smb_fname)
1872 if (is_afpinfo_stream(smb_fname)) {
1873 return true;
1875 if (is_afpresource_stream(smb_fname)) {
1876 return true;
1878 return false;
1880 #endif
1883 * Initialize config struct from our smb.conf config parameters
1885 static int init_fruit_config(vfs_handle_struct *handle)
1887 struct fruit_config_data *config;
1888 int enumval;
1889 const char *tm_size_str = NULL;
1891 config = talloc_zero(handle->conn, struct fruit_config_data);
1892 if (!config) {
1893 DEBUG(1, ("talloc_zero() failed\n"));
1894 errno = ENOMEM;
1895 return -1;
1899 * Versions up to Samba 4.5.x had a spelling bug in the
1900 * fruit:resource option calling lp_parm_enum with
1901 * "res*s*ource" (ie two s).
1903 * In Samba 4.6 we accept both the wrong and the correct
1904 * spelling, in Samba 4.7 the bad spelling will be removed.
1906 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1907 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1908 if (enumval == -1) {
1909 DEBUG(1, ("value for %s: resource type unknown\n",
1910 FRUIT_PARAM_TYPE_NAME));
1911 return -1;
1913 config->rsrc = (enum fruit_rsrc)enumval;
1915 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1916 "resource", fruit_rsrc, enumval);
1917 if (enumval == -1) {
1918 DEBUG(1, ("value for %s: resource type unknown\n",
1919 FRUIT_PARAM_TYPE_NAME));
1920 return -1;
1922 config->rsrc = (enum fruit_rsrc)enumval;
1924 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1925 "metadata", fruit_meta, FRUIT_META_NETATALK);
1926 if (enumval == -1) {
1927 DEBUG(1, ("value for %s: metadata type unknown\n",
1928 FRUIT_PARAM_TYPE_NAME));
1929 return -1;
1931 config->meta = (enum fruit_meta)enumval;
1933 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1934 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1935 if (enumval == -1) {
1936 DEBUG(1, ("value for %s: locking type unknown\n",
1937 FRUIT_PARAM_TYPE_NAME));
1938 return -1;
1940 config->locking = (enum fruit_locking)enumval;
1942 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1943 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1944 if (enumval == -1) {
1945 DEBUG(1, ("value for %s: encoding type unknown\n",
1946 FRUIT_PARAM_TYPE_NAME));
1947 return -1;
1949 config->encoding = (enum fruit_encoding)enumval;
1951 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1952 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
1953 FRUIT_PARAM_TYPE_NAME,
1954 "veto_appledouble",
1955 true);
1958 config->use_aapl = lp_parm_bool(
1959 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1961 config->time_machine = lp_parm_bool(
1962 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
1964 config->unix_info_enabled = lp_parm_bool(
1965 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1967 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1968 "copyfile", false);
1970 config->posix_rename = lp_parm_bool(
1971 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1973 config->aapl_zero_file_id =
1974 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
1976 config->readdir_attr_rsize = lp_parm_bool(
1977 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1979 config->readdir_attr_finder_info = lp_parm_bool(
1980 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1982 config->readdir_attr_max_access = lp_parm_bool(
1983 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1985 config->model = lp_parm_const_string(
1986 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
1988 tm_size_str = lp_parm_const_string(
1989 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1990 "time machine max size", NULL);
1991 if (tm_size_str != NULL) {
1992 config->time_machine_max_size = conv_str_size(tm_size_str);
1995 SMB_VFS_HANDLE_SET_DATA(handle, config,
1996 NULL, struct fruit_config_data,
1997 return -1);
1999 return 0;
2003 * Prepend "._" to a basename
2004 * Return a new struct smb_filename with stream_name == NULL.
2006 static int adouble_path(TALLOC_CTX *ctx,
2007 const struct smb_filename *smb_fname_in,
2008 struct smb_filename **pp_smb_fname_out)
2010 char *parent;
2011 const char *base;
2012 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2013 smb_fname_in);
2015 if (smb_fname == NULL) {
2016 return -1;
2019 /* We need streamname to be NULL */
2020 TALLOC_FREE(smb_fname->stream_name);
2022 /* And we're replacing base_name. */
2023 TALLOC_FREE(smb_fname->base_name);
2025 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2026 &parent, &base)) {
2027 TALLOC_FREE(smb_fname);
2028 return -1;
2031 smb_fname->base_name = talloc_asprintf(smb_fname,
2032 "%s/._%s", parent, base);
2033 if (smb_fname->base_name == NULL) {
2034 TALLOC_FREE(smb_fname);
2035 return -1;
2038 *pp_smb_fname_out = smb_fname;
2040 return 0;
2044 * Allocate and initialize an AfpInfo struct
2046 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2048 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2049 if (ai == NULL) {
2050 return NULL;
2052 ai->afpi_Signature = AFP_Signature;
2053 ai->afpi_Version = AFP_Version;
2054 ai->afpi_BackupTime = AD_DATE_START;
2055 return ai;
2059 * Pack an AfpInfo struct into a buffer
2061 * Buffer size must be at least AFP_INFO_SIZE
2062 * Returns size of packed buffer
2064 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2066 memset(buf, 0, AFP_INFO_SIZE);
2068 RSIVAL(buf, 0, ai->afpi_Signature);
2069 RSIVAL(buf, 4, ai->afpi_Version);
2070 RSIVAL(buf, 12, ai->afpi_BackupTime);
2071 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2073 return AFP_INFO_SIZE;
2077 * Unpack a buffer into a AfpInfo structure
2079 * Buffer size must be at least AFP_INFO_SIZE
2080 * Returns allocated AfpInfo struct
2082 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2084 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2085 if (ai == NULL) {
2086 return NULL;
2089 ai->afpi_Signature = RIVAL(data, 0);
2090 ai->afpi_Version = RIVAL(data, 4);
2091 ai->afpi_BackupTime = RIVAL(data, 12);
2092 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2093 sizeof(ai->afpi_FinderInfo));
2095 if (ai->afpi_Signature != AFP_Signature
2096 || ai->afpi_Version != AFP_Version) {
2097 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2098 TALLOC_FREE(ai);
2101 return ai;
2105 * Fake an inode number from the md5 hash of the (xattr) name
2107 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2109 MD5_CTX ctx;
2110 unsigned char hash[16];
2111 SMB_INO_T result;
2112 char *upper_sname;
2114 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2115 SMB_ASSERT(upper_sname != NULL);
2117 MD5Init(&ctx);
2118 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2119 sizeof(sbuf->st_ex_dev));
2120 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2121 sizeof(sbuf->st_ex_ino));
2122 MD5Update(&ctx, (unsigned char *)upper_sname,
2123 talloc_get_size(upper_sname)-1);
2124 MD5Final(hash, &ctx);
2126 TALLOC_FREE(upper_sname);
2128 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2129 memcpy(&result, hash, sizeof(result));
2131 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2132 sname, (unsigned long long)result));
2134 return result;
2137 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2138 struct stream_struct **streams,
2139 const char *name, off_t size,
2140 off_t alloc_size)
2142 struct stream_struct *tmp;
2144 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2145 (*num_streams)+1);
2146 if (tmp == NULL) {
2147 return false;
2150 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2151 if (tmp[*num_streams].name == NULL) {
2152 return false;
2155 tmp[*num_streams].size = size;
2156 tmp[*num_streams].alloc_size = alloc_size;
2158 *streams = tmp;
2159 *num_streams += 1;
2160 return true;
2163 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2164 struct stream_struct **streams)
2166 struct stream_struct *tmp = *streams;
2167 unsigned int i;
2169 if (*num_streams == 0) {
2170 return true;
2173 for (i = 0; i < *num_streams; i++) {
2174 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2175 break;
2179 if (i == *num_streams) {
2180 return true;
2183 if (tmp[i].size > 0) {
2184 return true;
2187 TALLOC_FREE(tmp[i].name);
2188 if (*num_streams - 1 > i) {
2189 memmove(&tmp[i], &tmp[i+1],
2190 (*num_streams - i - 1) * sizeof(struct stream_struct));
2193 *num_streams -= 1;
2194 return true;
2197 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2198 struct stream_struct **streams,
2199 const char *name)
2201 struct stream_struct *tmp = *streams;
2202 unsigned int i;
2204 if (*num_streams == 0) {
2205 return true;
2208 for (i = 0; i < *num_streams; i++) {
2209 if (strequal_m(tmp[i].name, name)) {
2210 break;
2214 if (i == *num_streams) {
2215 return true;
2218 TALLOC_FREE(tmp[i].name);
2219 if (*num_streams - 1 > i) {
2220 memmove(&tmp[i], &tmp[i+1],
2221 (*num_streams - i - 1) * sizeof(struct stream_struct));
2224 *num_streams -= 1;
2225 return true;
2228 static bool ad_empty_finderinfo(const struct adouble *ad)
2230 int cmp;
2231 char emptybuf[ADEDLEN_FINDERI] = {0};
2232 char *fi = NULL;
2234 fi = ad_get_entry(ad, ADEID_FINDERI);
2235 if (fi == NULL) {
2236 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2237 return false;
2240 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2241 return (cmp == 0);
2244 static bool ai_empty_finderinfo(const AfpInfo *ai)
2246 int cmp;
2247 char emptybuf[ADEDLEN_FINDERI] = {0};
2249 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2250 return (cmp == 0);
2254 * Update btime with btime from Netatalk
2256 static void update_btime(vfs_handle_struct *handle,
2257 struct smb_filename *smb_fname)
2259 uint32_t t;
2260 struct timespec creation_time = {0};
2261 struct adouble *ad;
2262 struct fruit_config_data *config = NULL;
2264 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2265 return);
2267 switch (config->meta) {
2268 case FRUIT_META_STREAM:
2269 return;
2270 case FRUIT_META_NETATALK:
2271 /* Handled below */
2272 break;
2273 default:
2274 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2275 return;
2278 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2279 if (ad == NULL) {
2280 return;
2282 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2283 TALLOC_FREE(ad);
2284 return;
2286 TALLOC_FREE(ad);
2288 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2289 update_stat_ex_create_time(&smb_fname->st, creation_time);
2291 return;
2295 * Map an access mask to a Netatalk single byte byte range lock
2297 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2298 uint32_t access_mask)
2300 off_t offset;
2302 switch (access_mask) {
2303 case FILE_READ_DATA:
2304 offset = AD_FILELOCK_OPEN_RD;
2305 break;
2307 case FILE_WRITE_DATA:
2308 case FILE_APPEND_DATA:
2309 offset = AD_FILELOCK_OPEN_WR;
2310 break;
2312 default:
2313 offset = AD_FILELOCK_OPEN_NONE;
2314 break;
2317 if (fork_type == APPLE_FORK_RSRC) {
2318 if (offset == AD_FILELOCK_OPEN_NONE) {
2319 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2320 } else {
2321 offset += 2;
2325 return offset;
2329 * Map a deny mode to a Netatalk brl
2331 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2332 uint32_t deny_mode)
2334 off_t offset;
2336 switch (deny_mode) {
2337 case DENY_READ:
2338 offset = AD_FILELOCK_DENY_RD;
2339 break;
2341 case DENY_WRITE:
2342 offset = AD_FILELOCK_DENY_WR;
2343 break;
2345 default:
2346 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2349 if (fork_type == APPLE_FORK_RSRC) {
2350 offset += 2;
2353 return offset;
2357 * Call fcntl() with an exclusive F_GETLK request in order to
2358 * determine if there's an exisiting shared lock
2360 * @return true if the requested lock was found or any error occurred
2361 * false if the lock was not found
2363 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2365 bool result;
2366 off_t offset = in_offset;
2367 off_t len = 1;
2368 int type = F_WRLCK;
2369 pid_t pid;
2371 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2372 if (result == false) {
2373 return true;
2376 if (type != F_UNLCK) {
2377 return true;
2380 return false;
2383 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2384 files_struct *fsp,
2385 uint32_t access_mask,
2386 uint32_t deny_mode)
2388 NTSTATUS status = NT_STATUS_OK;
2389 bool open_for_reading, open_for_writing, deny_read, deny_write;
2390 off_t off;
2391 bool have_read = false;
2392 int flags;
2394 /* FIXME: hardcoded data fork, add resource fork */
2395 enum apple_fork fork_type = APPLE_FORK_DATA;
2397 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2398 fsp_str_dbg(fsp),
2399 access_mask & FILE_READ_DATA ? "READ" :"-",
2400 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2401 deny_mode & DENY_READ ? "DENY_READ" : "-",
2402 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2404 if (fsp->fh->fd == -1) {
2405 return NT_STATUS_OK;
2408 flags = fcntl(fsp->fh->fd, F_GETFL);
2409 if (flags == -1) {
2410 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2411 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2412 return map_nt_error_from_unix(errno);
2415 if (flags & (O_RDONLY|O_RDWR)) {
2417 * Applying fcntl read locks requires an fd opened for
2418 * reading. This means we won't be applying locks for
2419 * files openend write-only, but what can we do...
2421 have_read = true;
2425 * Check read access and deny read mode
2427 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2428 /* Check access */
2429 open_for_reading = test_netatalk_lock(
2430 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2432 deny_read = test_netatalk_lock(
2433 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2435 DEBUG(10, ("read: %s, deny_write: %s\n",
2436 open_for_reading == true ? "yes" : "no",
2437 deny_read == true ? "yes" : "no"));
2439 if (((access_mask & FILE_READ_DATA) && deny_read)
2440 || ((deny_mode & DENY_READ) && open_for_reading)) {
2441 return NT_STATUS_SHARING_VIOLATION;
2444 /* Set locks */
2445 if ((access_mask & FILE_READ_DATA) && have_read) {
2446 struct byte_range_lock *br_lck = NULL;
2448 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2449 br_lck = do_lock(
2450 handle->conn->sconn->msg_ctx, fsp,
2451 fsp->op->global->open_persistent_id, 1, off,
2452 READ_LOCK, POSIX_LOCK, false,
2453 &status, NULL);
2455 TALLOC_FREE(br_lck);
2457 if (!NT_STATUS_IS_OK(status)) {
2458 return status;
2462 if ((deny_mode & DENY_READ) && have_read) {
2463 struct byte_range_lock *br_lck = NULL;
2465 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2466 br_lck = do_lock(
2467 handle->conn->sconn->msg_ctx, fsp,
2468 fsp->op->global->open_persistent_id, 1, off,
2469 READ_LOCK, POSIX_LOCK, false,
2470 &status, NULL);
2472 TALLOC_FREE(br_lck);
2474 if (!NT_STATUS_IS_OK(status)) {
2475 return status;
2481 * Check write access and deny write mode
2483 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2484 /* Check access */
2485 open_for_writing = test_netatalk_lock(
2486 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2488 deny_write = test_netatalk_lock(
2489 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2491 DEBUG(10, ("write: %s, deny_write: %s\n",
2492 open_for_writing == true ? "yes" : "no",
2493 deny_write == true ? "yes" : "no"));
2495 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2496 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2497 return NT_STATUS_SHARING_VIOLATION;
2500 /* Set locks */
2501 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2502 struct byte_range_lock *br_lck = NULL;
2504 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2505 br_lck = do_lock(
2506 handle->conn->sconn->msg_ctx, fsp,
2507 fsp->op->global->open_persistent_id, 1, off,
2508 READ_LOCK, POSIX_LOCK, false,
2509 &status, NULL);
2511 TALLOC_FREE(br_lck);
2513 if (!NT_STATUS_IS_OK(status)) {
2514 return status;
2517 if ((deny_mode & DENY_WRITE) && have_read) {
2518 struct byte_range_lock *br_lck = NULL;
2520 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2521 br_lck = do_lock(
2522 handle->conn->sconn->msg_ctx, fsp,
2523 fsp->op->global->open_persistent_id, 1, off,
2524 READ_LOCK, POSIX_LOCK, false,
2525 &status, NULL);
2527 TALLOC_FREE(br_lck);
2529 if (!NT_STATUS_IS_OK(status)) {
2530 return status;
2535 return status;
2538 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2539 struct smb_request *req,
2540 const struct smb2_create_blobs *in_context_blobs,
2541 struct smb2_create_blobs *out_context_blobs)
2543 struct fruit_config_data *config;
2544 NTSTATUS status;
2545 struct smb2_create_blob *aapl = NULL;
2546 uint32_t cmd;
2547 bool ok;
2548 uint8_t p[16];
2549 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2550 uint64_t req_bitmap, client_caps;
2551 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2552 smb_ucs2_t *model;
2553 size_t modellen;
2555 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2556 return NT_STATUS_UNSUCCESSFUL);
2558 if (!config->use_aapl
2559 || in_context_blobs == NULL
2560 || out_context_blobs == NULL) {
2561 return NT_STATUS_OK;
2564 aapl = smb2_create_blob_find(in_context_blobs,
2565 SMB2_CREATE_TAG_AAPL);
2566 if (aapl == NULL) {
2567 return NT_STATUS_OK;
2570 if (aapl->data.length != 24) {
2571 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2572 (uintmax_t)aapl->data.length));
2573 return NT_STATUS_INVALID_PARAMETER;
2576 cmd = IVAL(aapl->data.data, 0);
2577 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2578 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2579 return NT_STATUS_INVALID_PARAMETER;
2582 req_bitmap = BVAL(aapl->data.data, 8);
2583 client_caps = BVAL(aapl->data.data, 16);
2585 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2586 SIVAL(p, 4, 0);
2587 SBVAL(p, 8, req_bitmap);
2588 ok = data_blob_append(req, &blob, p, 16);
2589 if (!ok) {
2590 return NT_STATUS_UNSUCCESSFUL;
2593 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2594 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2595 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2596 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2597 config->readdir_attr_enabled = true;
2600 if (config->use_copyfile) {
2601 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2602 config->copyfile_enabled = true;
2606 * The client doesn't set the flag, so we can't check
2607 * for it and just set it unconditionally
2609 if (config->unix_info_enabled) {
2610 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2613 SBVAL(p, 0, server_caps);
2614 ok = data_blob_append(req, &blob, p, 8);
2615 if (!ok) {
2616 return NT_STATUS_UNSUCCESSFUL;
2620 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2621 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2622 uint64_t caps = 0;
2624 switch (val) {
2625 case Auto:
2626 break;
2628 case True:
2629 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2630 break;
2632 default:
2633 break;
2636 if (config->time_machine) {
2637 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2640 SBVAL(p, 0, caps);
2642 ok = data_blob_append(req, &blob, p, 8);
2643 if (!ok) {
2644 return NT_STATUS_UNSUCCESSFUL;
2648 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2649 ok = convert_string_talloc(req,
2650 CH_UNIX, CH_UTF16LE,
2651 config->model, strlen(config->model),
2652 &model, &modellen);
2653 if (!ok) {
2654 return NT_STATUS_UNSUCCESSFUL;
2657 SIVAL(p, 0, 0);
2658 SIVAL(p + 4, 0, modellen);
2659 ok = data_blob_append(req, &blob, p, 8);
2660 if (!ok) {
2661 talloc_free(model);
2662 return NT_STATUS_UNSUCCESSFUL;
2665 ok = data_blob_append(req, &blob, model, modellen);
2666 talloc_free(model);
2667 if (!ok) {
2668 return NT_STATUS_UNSUCCESSFUL;
2672 status = smb2_create_blob_add(out_context_blobs,
2673 out_context_blobs,
2674 SMB2_CREATE_TAG_AAPL,
2675 blob);
2676 if (NT_STATUS_IS_OK(status)) {
2677 global_fruit_config.nego_aapl = true;
2678 if (config->aapl_zero_file_id) {
2679 aapl_force_zero_file_id(handle->conn->sconn);
2683 return status;
2686 static bool readdir_attr_meta_finderi_stream(
2687 struct vfs_handle_struct *handle,
2688 const struct smb_filename *smb_fname,
2689 AfpInfo *ai)
2691 struct smb_filename *stream_name = NULL;
2692 files_struct *fsp = NULL;
2693 ssize_t nread;
2694 NTSTATUS status;
2695 int ret;
2696 bool ok;
2697 uint8_t buf[AFP_INFO_SIZE];
2699 stream_name = synthetic_smb_fname(talloc_tos(),
2700 smb_fname->base_name,
2701 AFPINFO_STREAM_NAME,
2702 NULL, smb_fname->flags);
2703 if (stream_name == NULL) {
2704 return false;
2707 ret = SMB_VFS_STAT(handle->conn, stream_name);
2708 if (ret != 0) {
2709 return false;
2712 status = SMB_VFS_CREATE_FILE(
2713 handle->conn, /* conn */
2714 NULL, /* req */
2715 0, /* root_dir_fid */
2716 stream_name, /* fname */
2717 FILE_READ_DATA, /* access_mask */
2718 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2719 FILE_SHARE_DELETE),
2720 FILE_OPEN, /* create_disposition*/
2721 0, /* create_options */
2722 0, /* file_attributes */
2723 INTERNAL_OPEN_ONLY, /* oplock_request */
2724 NULL, /* lease */
2725 0, /* allocation_size */
2726 0, /* private_flags */
2727 NULL, /* sd */
2728 NULL, /* ea_list */
2729 &fsp, /* result */
2730 NULL, /* pinfo */
2731 NULL, NULL); /* create context */
2733 TALLOC_FREE(stream_name);
2735 if (!NT_STATUS_IS_OK(status)) {
2736 return false;
2739 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2740 if (nread != AFP_INFO_SIZE) {
2741 DBG_ERR("short read [%s] [%zd/%d]\n",
2742 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2743 ok = false;
2744 goto fail;
2747 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2748 AFP_FinderSize);
2750 ok = true;
2752 fail:
2753 if (fsp != NULL) {
2754 close_file(NULL, fsp, NORMAL_CLOSE);
2757 return ok;
2760 static bool readdir_attr_meta_finderi_netatalk(
2761 struct vfs_handle_struct *handle,
2762 const struct smb_filename *smb_fname,
2763 AfpInfo *ai)
2765 struct adouble *ad = NULL;
2766 char *p = NULL;
2768 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2769 if (ad == NULL) {
2770 return false;
2773 p = ad_get_entry(ad, ADEID_FINDERI);
2774 if (p == NULL) {
2775 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2776 TALLOC_FREE(ad);
2777 return false;
2780 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2781 TALLOC_FREE(ad);
2782 return true;
2785 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2786 const struct smb_filename *smb_fname,
2787 struct readdir_attr_data *attr_data)
2789 struct fruit_config_data *config = NULL;
2790 uint32_t date_added;
2791 AfpInfo ai = {0};
2792 bool ok;
2794 SMB_VFS_HANDLE_GET_DATA(handle, config,
2795 struct fruit_config_data,
2796 return false);
2798 switch (config->meta) {
2799 case FRUIT_META_NETATALK:
2800 ok = readdir_attr_meta_finderi_netatalk(
2801 handle, smb_fname, &ai);
2802 break;
2804 case FRUIT_META_STREAM:
2805 ok = readdir_attr_meta_finderi_stream(
2806 handle, smb_fname, &ai);
2807 break;
2809 default:
2810 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2811 return false;
2814 if (!ok) {
2815 /* Don't bother with errors, it's likely ENOENT */
2816 return true;
2819 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2820 /* finder_type */
2821 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2822 &ai.afpi_FinderInfo[0], 4);
2824 /* finder_creator */
2825 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2826 &ai.afpi_FinderInfo[4], 4);
2829 /* finder_flags */
2830 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2831 &ai.afpi_FinderInfo[8], 2);
2833 /* finder_ext_flags */
2834 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2835 &ai.afpi_FinderInfo[24], 2);
2837 /* creation date */
2838 date_added = convert_time_t_to_uint32_t(
2839 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2841 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2843 return true;
2846 static uint64_t readdir_attr_rfork_size_adouble(
2847 struct vfs_handle_struct *handle,
2848 const struct smb_filename *smb_fname)
2850 struct adouble *ad = NULL;
2851 uint64_t rfork_size;
2853 ad = ad_get(talloc_tos(), handle, smb_fname,
2854 ADOUBLE_RSRC);
2855 if (ad == NULL) {
2856 return 0;
2859 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2860 TALLOC_FREE(ad);
2862 return rfork_size;
2865 static uint64_t readdir_attr_rfork_size_stream(
2866 struct vfs_handle_struct *handle,
2867 const struct smb_filename *smb_fname)
2869 struct smb_filename *stream_name = NULL;
2870 int ret;
2871 uint64_t rfork_size;
2873 stream_name = synthetic_smb_fname(talloc_tos(),
2874 smb_fname->base_name,
2875 AFPRESOURCE_STREAM_NAME,
2876 NULL, 0);
2877 if (stream_name == NULL) {
2878 return 0;
2881 ret = SMB_VFS_STAT(handle->conn, stream_name);
2882 if (ret != 0) {
2883 TALLOC_FREE(stream_name);
2884 return 0;
2887 rfork_size = stream_name->st.st_ex_size;
2888 TALLOC_FREE(stream_name);
2890 return rfork_size;
2893 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2894 const struct smb_filename *smb_fname)
2896 struct fruit_config_data *config = NULL;
2897 uint64_t rfork_size;
2899 SMB_VFS_HANDLE_GET_DATA(handle, config,
2900 struct fruit_config_data,
2901 return 0);
2903 switch (config->rsrc) {
2904 case FRUIT_RSRC_ADFILE:
2905 case FRUIT_RSRC_XATTR:
2906 rfork_size = readdir_attr_rfork_size_adouble(handle,
2907 smb_fname);
2908 break;
2910 case FRUIT_META_STREAM:
2911 rfork_size = readdir_attr_rfork_size_stream(handle,
2912 smb_fname);
2913 break;
2915 default:
2916 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
2917 rfork_size = 0;
2918 break;
2921 return rfork_size;
2924 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
2925 const struct smb_filename *smb_fname,
2926 struct readdir_attr_data *attr_data)
2928 NTSTATUS status = NT_STATUS_OK;
2929 struct fruit_config_data *config = NULL;
2930 bool ok;
2932 SMB_VFS_HANDLE_GET_DATA(handle, config,
2933 struct fruit_config_data,
2934 return NT_STATUS_UNSUCCESSFUL);
2937 /* Ensure we return a default value in the creation_date field */
2938 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
2941 * Resource fork length
2944 if (config->readdir_attr_rsize) {
2945 uint64_t rfork_size;
2947 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
2948 attr_data->attr_data.aapl.rfork_size = rfork_size;
2952 * FinderInfo
2955 if (config->readdir_attr_finder_info) {
2956 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
2957 if (!ok) {
2958 status = NT_STATUS_INTERNAL_ERROR;
2962 return status;
2965 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
2967 NTSTATUS status;
2968 uint32_t i;
2970 if (psd->dacl == NULL) {
2971 return NT_STATUS_OK;
2974 for (i = 0; i < psd->dacl->num_aces; i++) {
2975 /* MS NFS style mode/uid/gid */
2976 int cmp = dom_sid_compare_domain(
2977 &global_sid_Unix_NFS,
2978 &psd->dacl->aces[i].trustee);
2979 if (cmp != 0) {
2980 /* Normal ACE entry. */
2981 continue;
2985 * security_descriptor_dacl_del()
2986 * *must* return NT_STATUS_OK as we know
2987 * we have something to remove.
2990 status = security_descriptor_dacl_del(psd,
2991 &psd->dacl->aces[i].trustee);
2992 if (!NT_STATUS_IS_OK(status)) {
2993 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
2994 nt_errstr(status));
2995 return status;
2999 * security_descriptor_dacl_del() may delete more
3000 * then one entry subsequent to this one if the
3001 * SID matches, but we only need to ensure that
3002 * we stay looking at the same element in the array.
3004 i--;
3006 return NT_STATUS_OK;
3009 /* Search MS NFS style ACE with UNIX mode */
3010 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3011 files_struct *fsp,
3012 struct security_descriptor *psd,
3013 mode_t *pmode,
3014 bool *pdo_chmod)
3016 uint32_t i;
3017 struct fruit_config_data *config = NULL;
3019 *pdo_chmod = false;
3021 SMB_VFS_HANDLE_GET_DATA(handle, config,
3022 struct fruit_config_data,
3023 return NT_STATUS_UNSUCCESSFUL);
3025 if (!global_fruit_config.nego_aapl) {
3026 return NT_STATUS_OK;
3028 if (psd->dacl == NULL || !config->unix_info_enabled) {
3029 return NT_STATUS_OK;
3032 for (i = 0; i < psd->dacl->num_aces; i++) {
3033 if (dom_sid_compare_domain(
3034 &global_sid_Unix_NFS_Mode,
3035 &psd->dacl->aces[i].trustee) == 0) {
3036 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3037 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3038 *pdo_chmod = true;
3040 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3041 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3042 break;
3047 * Remove any incoming virtual ACE entries generated by
3048 * fruit_fget_nt_acl().
3051 return remove_virtual_nfs_aces(psd);
3054 /****************************************************************************
3055 * VFS ops
3056 ****************************************************************************/
3058 static int fruit_connect(vfs_handle_struct *handle,
3059 const char *service,
3060 const char *user)
3062 int rc;
3063 char *list = NULL, *newlist = NULL;
3064 struct fruit_config_data *config;
3066 DEBUG(10, ("fruit_connect\n"));
3068 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3069 if (rc < 0) {
3070 return rc;
3073 rc = init_fruit_config(handle);
3074 if (rc != 0) {
3075 return rc;
3078 SMB_VFS_HANDLE_GET_DATA(handle, config,
3079 struct fruit_config_data, return -1);
3081 if (config->veto_appledouble) {
3082 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3084 if (list) {
3085 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3086 newlist = talloc_asprintf(
3087 list,
3088 "%s/" ADOUBLE_NAME_PREFIX "*/",
3089 list);
3090 lp_do_parameter(SNUM(handle->conn),
3091 "veto files",
3092 newlist);
3094 } else {
3095 lp_do_parameter(SNUM(handle->conn),
3096 "veto files",
3097 "/" ADOUBLE_NAME_PREFIX "*/");
3100 TALLOC_FREE(list);
3103 if (config->encoding == FRUIT_ENC_NATIVE) {
3104 lp_do_parameter(SNUM(handle->conn),
3105 "catia:mappings",
3106 fruit_catia_maps);
3109 if (config->time_machine) {
3110 DBG_NOTICE("Enabling durable handles for Time Machine "
3111 "support on [%s]\n", service);
3112 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3113 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3114 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3115 if (!lp_strict_sync(SNUM(handle->conn))) {
3116 DBG_WARNING("Time Machine without strict sync is not "
3117 "recommended!\n");
3119 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3122 return rc;
3125 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3126 struct smb_filename *smb_fname,
3127 files_struct *fsp,
3128 int flags,
3129 mode_t mode)
3131 AfpInfo *ai = NULL;
3132 char afpinfo_buf[AFP_INFO_SIZE];
3133 ssize_t len, written;
3134 int hostfd = -1;
3135 int rc = -1;
3137 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3138 if (hostfd == -1) {
3139 return -1;
3142 if (!(flags & (O_CREAT | O_TRUNC))) {
3143 return hostfd;
3146 ai = afpinfo_new(talloc_tos());
3147 if (ai == NULL) {
3148 rc = -1;
3149 goto fail;
3152 len = afpinfo_pack(ai, afpinfo_buf);
3153 if (len != AFP_INFO_SIZE) {
3154 rc = -1;
3155 goto fail;
3158 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3159 fsp->fh->fd = hostfd;
3161 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3162 AFP_INFO_SIZE, 0);
3163 fsp->fh->fd = -1;
3164 if (written != AFP_INFO_SIZE) {
3165 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3166 rc = -1;
3167 goto fail;
3170 rc = 0;
3172 fail:
3173 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3175 if (rc != 0) {
3176 int saved_errno = errno;
3177 if (hostfd >= 0) {
3178 fsp->fh->fd = hostfd;
3179 SMB_VFS_NEXT_CLOSE(handle, fsp);
3181 hostfd = -1;
3182 errno = saved_errno;
3184 return hostfd;
3187 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3188 struct smb_filename *smb_fname,
3189 files_struct *fsp,
3190 int flags,
3191 mode_t mode)
3193 int rc;
3194 int fakefd = -1;
3195 struct adouble *ad = NULL;
3196 int fds[2];
3198 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3201 * Return a valid fd, but ensure any attempt to use it returns an error
3202 * (EPIPE). All operations on the smb_fname or the fsp will use path
3203 * based syscalls.
3205 rc = pipe(fds);
3206 if (rc != 0) {
3207 goto exit;
3209 fakefd = fds[0];
3210 close(fds[1]);
3212 if (flags & (O_CREAT | O_TRUNC)) {
3214 * The attribute does not exist or needs to be truncated,
3215 * create an AppleDouble EA
3217 ad = ad_init(fsp, handle, ADOUBLE_META);
3218 if (ad == NULL) {
3219 rc = -1;
3220 goto exit;
3223 rc = ad_set(ad, fsp->fsp_name);
3224 if (rc != 0) {
3225 rc = -1;
3226 goto exit;
3229 TALLOC_FREE(ad);
3232 exit:
3233 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3234 if (rc != 0) {
3235 int saved_errno = errno;
3236 if (fakefd >= 0) {
3237 close(fakefd);
3239 fakefd = -1;
3240 errno = saved_errno;
3242 return fakefd;
3245 static int fruit_open_meta(vfs_handle_struct *handle,
3246 struct smb_filename *smb_fname,
3247 files_struct *fsp, int flags, mode_t mode)
3249 int fd;
3250 struct fruit_config_data *config = NULL;
3251 struct fio *fio = NULL;
3253 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3255 SMB_VFS_HANDLE_GET_DATA(handle, config,
3256 struct fruit_config_data, return -1);
3258 switch (config->meta) {
3259 case FRUIT_META_STREAM:
3260 fd = fruit_open_meta_stream(handle, smb_fname,
3261 fsp, flags, mode);
3262 break;
3264 case FRUIT_META_NETATALK:
3265 fd = fruit_open_meta_netatalk(handle, smb_fname,
3266 fsp, flags, mode);
3267 break;
3269 default:
3270 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3271 return -1;
3274 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3276 if (fd == -1) {
3277 return -1;
3280 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3281 fio->type = ADOUBLE_META;
3282 fio->config = config;
3284 return fd;
3287 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3288 struct smb_filename *smb_fname,
3289 files_struct *fsp,
3290 int flags,
3291 mode_t mode)
3293 int rc = 0;
3294 struct adouble *ad = NULL;
3295 struct smb_filename *smb_fname_base = NULL;
3296 struct fruit_config_data *config = NULL;
3297 int hostfd = -1;
3299 SMB_VFS_HANDLE_GET_DATA(handle, config,
3300 struct fruit_config_data, return -1);
3302 if ((!(flags & O_CREAT)) &&
3303 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3305 /* sorry, but directories don't habe a resource fork */
3306 rc = -1;
3307 goto exit;
3310 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3311 if (rc != 0) {
3312 goto exit;
3315 /* Sanitize flags */
3316 if (flags & O_WRONLY) {
3317 /* We always need read access for the metadata header too */
3318 flags &= ~O_WRONLY;
3319 flags |= O_RDWR;
3322 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3323 flags, mode);
3324 if (hostfd == -1) {
3325 rc = -1;
3326 goto exit;
3329 if (flags & (O_CREAT | O_TRUNC)) {
3330 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3331 if (ad == NULL) {
3332 rc = -1;
3333 goto exit;
3336 fsp->fh->fd = hostfd;
3338 rc = ad_fset(ad, fsp);
3339 fsp->fh->fd = -1;
3340 if (rc != 0) {
3341 rc = -1;
3342 goto exit;
3344 TALLOC_FREE(ad);
3347 exit:
3349 TALLOC_FREE(smb_fname_base);
3351 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3352 if (rc != 0) {
3353 int saved_errno = errno;
3354 if (hostfd >= 0) {
3356 * BUGBUGBUG -- we would need to call
3357 * fd_close_posix here, but we don't have a
3358 * full fsp yet
3360 fsp->fh->fd = hostfd;
3361 SMB_VFS_CLOSE(fsp);
3363 hostfd = -1;
3364 errno = saved_errno;
3366 return hostfd;
3369 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3370 struct smb_filename *smb_fname,
3371 files_struct *fsp,
3372 int flags,
3373 mode_t mode)
3375 #ifdef HAVE_ATTROPEN
3376 int fd = -1;
3378 fd = attropen(smb_fname->base_name,
3379 AFPRESOURCE_EA_NETATALK,
3380 flags,
3381 mode);
3382 if (fd == -1) {
3383 return -1;
3386 return fd;
3388 #else
3389 errno = ENOSYS;
3390 return -1;
3391 #endif
3394 static int fruit_open_rsrc(vfs_handle_struct *handle,
3395 struct smb_filename *smb_fname,
3396 files_struct *fsp, int flags, mode_t mode)
3398 int fd;
3399 struct fruit_config_data *config = NULL;
3400 struct fio *fio = NULL;
3402 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3404 SMB_VFS_HANDLE_GET_DATA(handle, config,
3405 struct fruit_config_data, return -1);
3407 if (((flags & O_ACCMODE) == O_RDONLY)
3408 && (flags & O_CREAT)
3409 && !VALID_STAT(fsp->fsp_name->st))
3412 * This means the stream doesn't exist. macOS SMB server fails
3413 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3414 * 12565 and the test for this combination in
3415 * test_rfork_create().
3417 errno = ENOENT;
3418 return -1;
3421 switch (config->rsrc) {
3422 case FRUIT_RSRC_STREAM:
3423 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3424 break;
3426 case FRUIT_RSRC_ADFILE:
3427 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3428 fsp, flags, mode);
3429 break;
3431 case FRUIT_RSRC_XATTR:
3432 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3433 fsp, flags, mode);
3434 break;
3436 default:
3437 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3438 return -1;
3441 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3443 if (fd == -1) {
3444 return -1;
3447 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3448 fio->type = ADOUBLE_RSRC;
3449 fio->config = config;
3451 return fd;
3454 static int fruit_open(vfs_handle_struct *handle,
3455 struct smb_filename *smb_fname,
3456 files_struct *fsp, int flags, mode_t mode)
3458 int fd;
3460 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3462 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3463 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3466 if (is_afpinfo_stream(smb_fname)) {
3467 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3468 } else if (is_afpresource_stream(smb_fname)) {
3469 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3470 } else {
3471 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3474 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3476 return fd;
3479 static int fruit_rename(struct vfs_handle_struct *handle,
3480 const struct smb_filename *smb_fname_src,
3481 const struct smb_filename *smb_fname_dst)
3483 int rc = -1;
3484 struct fruit_config_data *config = NULL;
3485 struct smb_filename *src_adp_smb_fname = NULL;
3486 struct smb_filename *dst_adp_smb_fname = NULL;
3488 SMB_VFS_HANDLE_GET_DATA(handle, config,
3489 struct fruit_config_data, return -1);
3491 if (!VALID_STAT(smb_fname_src->st)) {
3492 DBG_ERR("Need valid stat for [%s]\n",
3493 smb_fname_str_dbg(smb_fname_src));
3494 return -1;
3497 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3498 if (rc != 0) {
3499 return -1;
3502 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3503 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3505 return 0;
3508 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3509 if (rc != 0) {
3510 goto done;
3513 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3514 if (rc != 0) {
3515 goto done;
3518 DBG_DEBUG("%s -> %s\n",
3519 smb_fname_str_dbg(src_adp_smb_fname),
3520 smb_fname_str_dbg(dst_adp_smb_fname));
3522 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3523 if (errno == ENOENT) {
3524 rc = 0;
3527 done:
3528 TALLOC_FREE(src_adp_smb_fname);
3529 TALLOC_FREE(dst_adp_smb_fname);
3530 return rc;
3533 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3534 const struct smb_filename *smb_fname)
3536 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3539 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3540 const struct smb_filename *smb_fname)
3542 return SMB_VFS_REMOVEXATTR(handle->conn,
3543 smb_fname,
3544 AFPINFO_EA_NETATALK);
3547 static int fruit_unlink_meta(vfs_handle_struct *handle,
3548 const struct smb_filename *smb_fname)
3550 struct fruit_config_data *config = NULL;
3551 int rc;
3553 SMB_VFS_HANDLE_GET_DATA(handle, config,
3554 struct fruit_config_data, return -1);
3556 switch (config->meta) {
3557 case FRUIT_META_STREAM:
3558 rc = fruit_unlink_meta_stream(handle, smb_fname);
3559 break;
3561 case FRUIT_META_NETATALK:
3562 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3563 break;
3565 default:
3566 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3567 return -1;
3570 return rc;
3573 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3574 const struct smb_filename *smb_fname,
3575 bool force_unlink)
3577 int ret;
3579 if (!force_unlink) {
3580 struct smb_filename *smb_fname_cp = NULL;
3581 off_t size;
3583 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3584 if (smb_fname_cp == NULL) {
3585 return -1;
3589 * 0 byte resource fork streams are not listed by
3590 * vfs_streaminfo, as a result stream cleanup/deletion of file
3591 * deletion doesn't remove the resourcefork stream.
3594 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3595 if (ret != 0) {
3596 TALLOC_FREE(smb_fname_cp);
3597 DBG_ERR("stat [%s] failed [%s]\n",
3598 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3599 return -1;
3602 size = smb_fname_cp->st.st_ex_size;
3603 TALLOC_FREE(smb_fname_cp);
3605 if (size > 0) {
3606 /* OS X ignores resource fork stream delete requests */
3607 return 0;
3611 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3612 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3613 ret = 0;
3616 return ret;
3619 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3620 const struct smb_filename *smb_fname,
3621 bool force_unlink)
3623 int rc;
3624 struct adouble *ad = NULL;
3625 struct smb_filename *adp_smb_fname = NULL;
3627 if (!force_unlink) {
3628 ad = ad_get(talloc_tos(), handle, smb_fname,
3629 ADOUBLE_RSRC);
3630 if (ad == NULL) {
3631 errno = ENOENT;
3632 return -1;
3637 * 0 byte resource fork streams are not listed by
3638 * vfs_streaminfo, as a result stream cleanup/deletion of file
3639 * deletion doesn't remove the resourcefork stream.
3642 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3643 /* OS X ignores resource fork stream delete requests */
3644 TALLOC_FREE(ad);
3645 return 0;
3648 TALLOC_FREE(ad);
3651 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3652 if (rc != 0) {
3653 return -1;
3656 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3657 TALLOC_FREE(adp_smb_fname);
3658 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3659 rc = 0;
3662 return rc;
3665 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3666 const struct smb_filename *smb_fname,
3667 bool force_unlink)
3670 * OS X ignores resource fork stream delete requests, so nothing to do
3671 * here. Removing the file will remove the xattr anyway, so we don't
3672 * have to take care of removing 0 byte resource forks that could be
3673 * left behind.
3675 return 0;
3678 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3679 const struct smb_filename *smb_fname,
3680 bool force_unlink)
3682 struct fruit_config_data *config = NULL;
3683 int rc;
3685 SMB_VFS_HANDLE_GET_DATA(handle, config,
3686 struct fruit_config_data, return -1);
3688 switch (config->rsrc) {
3689 case FRUIT_RSRC_STREAM:
3690 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3691 break;
3693 case FRUIT_RSRC_ADFILE:
3694 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3695 break;
3697 case FRUIT_RSRC_XATTR:
3698 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3699 break;
3701 default:
3702 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3703 return -1;
3706 return rc;
3709 static int fruit_unlink(vfs_handle_struct *handle,
3710 const struct smb_filename *smb_fname)
3712 int rc;
3713 struct fruit_config_data *config = NULL;
3714 struct smb_filename *rsrc_smb_fname = NULL;
3716 SMB_VFS_HANDLE_GET_DATA(handle, config,
3717 struct fruit_config_data, return -1);
3719 if (is_afpinfo_stream(smb_fname)) {
3720 return fruit_unlink_meta(handle, smb_fname);
3721 } else if (is_afpresource_stream(smb_fname)) {
3722 return fruit_unlink_rsrc(handle, smb_fname, false);
3723 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3724 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3728 * A request to delete the base file. Because 0 byte resource
3729 * fork streams are not listed by fruit_streaminfo,
3730 * delete_all_streams() can't remove 0 byte resource fork
3731 * streams, so we have to cleanup this here.
3733 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3734 smb_fname->base_name,
3735 AFPRESOURCE_STREAM_NAME,
3736 NULL,
3737 smb_fname->flags);
3738 if (rsrc_smb_fname == NULL) {
3739 return -1;
3742 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3743 if ((rc != 0) && (errno != ENOENT)) {
3744 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3745 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3746 TALLOC_FREE(rsrc_smb_fname);
3747 return -1;
3749 TALLOC_FREE(rsrc_smb_fname);
3751 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3754 static int fruit_chmod(vfs_handle_struct *handle,
3755 const struct smb_filename *smb_fname,
3756 mode_t mode)
3758 int rc = -1;
3759 struct fruit_config_data *config = NULL;
3760 struct smb_filename *smb_fname_adp = NULL;
3762 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3763 if (rc != 0) {
3764 return rc;
3767 SMB_VFS_HANDLE_GET_DATA(handle, config,
3768 struct fruit_config_data, return -1);
3770 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3771 return 0;
3774 if (!VALID_STAT(smb_fname->st)) {
3775 return 0;
3778 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3779 return 0;
3782 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3783 if (rc != 0) {
3784 return -1;
3787 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3789 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3790 if (errno == ENOENT) {
3791 rc = 0;
3794 TALLOC_FREE(smb_fname_adp);
3795 return rc;
3798 static int fruit_chown(vfs_handle_struct *handle,
3799 const struct smb_filename *smb_fname,
3800 uid_t uid,
3801 gid_t gid)
3803 int rc = -1;
3804 struct fruit_config_data *config = NULL;
3805 struct smb_filename *adp_smb_fname = NULL;
3807 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3808 if (rc != 0) {
3809 return rc;
3812 SMB_VFS_HANDLE_GET_DATA(handle, config,
3813 struct fruit_config_data, return -1);
3815 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3816 return 0;
3819 if (!VALID_STAT(smb_fname->st)) {
3820 return 0;
3823 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3824 return 0;
3827 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3828 if (rc != 0) {
3829 goto done;
3832 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3834 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3835 if (errno == ENOENT) {
3836 rc = 0;
3839 done:
3840 TALLOC_FREE(adp_smb_fname);
3841 return rc;
3844 static int fruit_rmdir(struct vfs_handle_struct *handle,
3845 const struct smb_filename *smb_fname)
3847 DIR *dh = NULL;
3848 struct dirent *de;
3849 struct fruit_config_data *config;
3851 SMB_VFS_HANDLE_GET_DATA(handle, config,
3852 struct fruit_config_data, return -1);
3854 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3855 goto exit_rmdir;
3859 * Due to there is no way to change bDeleteVetoFiles variable
3860 * from this module, need to clean up ourselves
3863 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3864 if (dh == NULL) {
3865 goto exit_rmdir;
3868 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3869 int match;
3870 struct adouble *ad = NULL;
3871 char *p = NULL;
3872 struct smb_filename *ad_smb_fname = NULL;
3873 int ret;
3875 match = strncmp(de->d_name,
3876 ADOUBLE_NAME_PREFIX,
3877 strlen(ADOUBLE_NAME_PREFIX));
3878 if (match != 0) {
3879 continue;
3882 p = talloc_asprintf(talloc_tos(), "%s/%s",
3883 smb_fname->base_name, de->d_name);
3884 if (p == NULL) {
3885 DBG_ERR("talloc_asprintf failed\n");
3886 return -1;
3889 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3890 NULL, NULL,
3891 smb_fname->flags);
3892 TALLOC_FREE(p);
3893 if (ad_smb_fname == NULL) {
3894 DBG_ERR("synthetic_smb_fname failed\n");
3895 return -1;
3899 * Check whether it's a valid AppleDouble file, if
3900 * yes, delete it, ignore it otherwise.
3902 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3903 if (ad == NULL) {
3904 TALLOC_FREE(ad_smb_fname);
3905 TALLOC_FREE(p);
3906 continue;
3908 TALLOC_FREE(ad);
3910 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
3911 if (ret != 0) {
3912 DBG_ERR("Deleting [%s] failed\n",
3913 smb_fname_str_dbg(ad_smb_fname));
3915 TALLOC_FREE(ad_smb_fname);
3918 exit_rmdir:
3919 if (dh) {
3920 SMB_VFS_CLOSEDIR(handle->conn, dh);
3922 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
3925 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
3926 files_struct *fsp, void *data,
3927 size_t n, off_t offset)
3929 ssize_t nread;
3930 int ret;
3932 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
3934 if (nread == n) {
3935 return nread;
3938 DBG_ERR("Removing [%s] after short read [%zd]\n",
3939 fsp_str_dbg(fsp), nread);
3941 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
3942 if (ret != 0) {
3943 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
3944 return -1;
3947 errno = EINVAL;
3948 return -1;
3951 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
3952 files_struct *fsp, void *data,
3953 size_t n, off_t offset)
3955 AfpInfo *ai = NULL;
3956 struct adouble *ad = NULL;
3957 char afpinfo_buf[AFP_INFO_SIZE];
3958 char *p = NULL;
3959 ssize_t nread;
3961 ai = afpinfo_new(talloc_tos());
3962 if (ai == NULL) {
3963 return -1;
3966 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
3967 if (ad == NULL) {
3968 nread = -1;
3969 goto fail;
3972 p = ad_get_entry(ad, ADEID_FINDERI);
3973 if (p == NULL) {
3974 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
3975 nread = -1;
3976 goto fail;
3979 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
3981 nread = afpinfo_pack(ai, afpinfo_buf);
3982 if (nread != AFP_INFO_SIZE) {
3983 nread = -1;
3984 goto fail;
3987 memcpy(data, afpinfo_buf, n);
3988 nread = n;
3990 fail:
3991 TALLOC_FREE(ai);
3992 return nread;
3995 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
3996 files_struct *fsp, void *data,
3997 size_t n, off_t offset)
3999 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4000 ssize_t nread;
4001 ssize_t to_return;
4004 * OS X has a off-by-1 error in the offset calculation, so we're
4005 * bug compatible here. It won't hurt, as any relevant real
4006 * world read requests from the AFP_AfpInfo stream will be
4007 * offset=0 n=60. offset is ignored anyway, see below.
4009 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4010 return 0;
4013 if (fio == NULL) {
4014 DBG_ERR("Failed to fetch fsp extension");
4015 return -1;
4018 /* Yes, macOS always reads from offset 0 */
4019 offset = 0;
4020 to_return = MIN(n, AFP_INFO_SIZE);
4022 switch (fio->config->meta) {
4023 case FRUIT_META_STREAM:
4024 nread = fruit_pread_meta_stream(handle, fsp, data,
4025 to_return, offset);
4026 break;
4028 case FRUIT_META_NETATALK:
4029 nread = fruit_pread_meta_adouble(handle, fsp, data,
4030 to_return, offset);
4031 break;
4033 default:
4034 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4035 return -1;
4038 return nread;
4041 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4042 files_struct *fsp, void *data,
4043 size_t n, off_t offset)
4045 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4048 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4049 files_struct *fsp, void *data,
4050 size_t n, off_t offset)
4052 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4055 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4056 files_struct *fsp, void *data,
4057 size_t n, off_t offset)
4059 struct adouble *ad = NULL;
4060 ssize_t nread;
4062 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4063 if (ad == NULL) {
4064 return -1;
4067 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4068 offset + ad_getentryoff(ad, ADEID_RFORK));
4070 TALLOC_FREE(ad);
4071 return nread;
4074 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4075 files_struct *fsp, void *data,
4076 size_t n, off_t offset)
4078 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4079 ssize_t nread;
4081 if (fio == NULL) {
4082 errno = EINVAL;
4083 return -1;
4086 switch (fio->config->rsrc) {
4087 case FRUIT_RSRC_STREAM:
4088 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4089 break;
4091 case FRUIT_RSRC_ADFILE:
4092 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4093 break;
4095 case FRUIT_RSRC_XATTR:
4096 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4097 break;
4099 default:
4100 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4101 return -1;
4104 return nread;
4107 static ssize_t fruit_pread(vfs_handle_struct *handle,
4108 files_struct *fsp, void *data,
4109 size_t n, off_t offset)
4111 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4112 ssize_t nread;
4114 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4115 fsp_str_dbg(fsp), (intmax_t)offset, n);
4117 if (fio == NULL) {
4118 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4121 if (fio->type == ADOUBLE_META) {
4122 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4123 } else {
4124 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4127 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4128 return nread;
4131 static bool fruit_must_handle_aio_stream(struct fio *fio)
4133 if (fio == NULL) {
4134 return false;
4137 if ((fio->type == ADOUBLE_META) &&
4138 (fio->config->meta == FRUIT_META_NETATALK))
4140 return true;
4143 if ((fio->type == ADOUBLE_RSRC) &&
4144 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4146 return true;
4149 return false;
4152 struct fruit_pread_state {
4153 ssize_t nread;
4154 struct vfs_aio_state vfs_aio_state;
4157 static void fruit_pread_done(struct tevent_req *subreq);
4159 static struct tevent_req *fruit_pread_send(
4160 struct vfs_handle_struct *handle,
4161 TALLOC_CTX *mem_ctx,
4162 struct tevent_context *ev,
4163 struct files_struct *fsp,
4164 void *data,
4165 size_t n, off_t offset)
4167 struct tevent_req *req = NULL;
4168 struct tevent_req *subreq = NULL;
4169 struct fruit_pread_state *state = NULL;
4170 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4172 req = tevent_req_create(mem_ctx, &state,
4173 struct fruit_pread_state);
4174 if (req == NULL) {
4175 return NULL;
4178 if (fruit_must_handle_aio_stream(fio)) {
4179 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4180 if (state->nread != n) {
4181 if (state->nread != -1) {
4182 errno = EIO;
4184 tevent_req_error(req, errno);
4185 return tevent_req_post(req, ev);
4187 tevent_req_done(req);
4188 return tevent_req_post(req, ev);
4191 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4192 data, n, offset);
4193 if (tevent_req_nomem(req, subreq)) {
4194 return tevent_req_post(req, ev);
4196 tevent_req_set_callback(subreq, fruit_pread_done, req);
4197 return req;
4200 static void fruit_pread_done(struct tevent_req *subreq)
4202 struct tevent_req *req = tevent_req_callback_data(
4203 subreq, struct tevent_req);
4204 struct fruit_pread_state *state = tevent_req_data(
4205 req, struct fruit_pread_state);
4207 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4208 TALLOC_FREE(subreq);
4210 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4211 return;
4213 tevent_req_done(req);
4216 static ssize_t fruit_pread_recv(struct tevent_req *req,
4217 struct vfs_aio_state *vfs_aio_state)
4219 struct fruit_pread_state *state = tevent_req_data(
4220 req, struct fruit_pread_state);
4222 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4223 return -1;
4226 *vfs_aio_state = state->vfs_aio_state;
4227 return state->nread;
4230 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4231 files_struct *fsp, const void *data,
4232 size_t n, off_t offset)
4234 AfpInfo *ai = NULL;
4235 size_t nwritten;
4236 bool ok;
4238 ai = afpinfo_unpack(talloc_tos(), data);
4239 if (ai == NULL) {
4240 return -1;
4243 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4244 if (nwritten != n) {
4245 return -1;
4248 if (!ai_empty_finderinfo(ai)) {
4249 return n;
4252 ok = set_delete_on_close(
4253 fsp,
4254 true,
4255 handle->conn->session_info->security_token,
4256 handle->conn->session_info->unix_token);
4257 if (!ok) {
4258 DBG_ERR("set_delete_on_close on [%s] failed\n",
4259 fsp_str_dbg(fsp));
4260 return -1;
4263 return n;
4266 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4267 files_struct *fsp, const void *data,
4268 size_t n, off_t offset)
4270 struct adouble *ad = NULL;
4271 AfpInfo *ai = NULL;
4272 char *p = NULL;
4273 int ret;
4274 bool ok;
4276 ai = afpinfo_unpack(talloc_tos(), data);
4277 if (ai == NULL) {
4278 return -1;
4281 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4282 if (ad == NULL) {
4283 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4284 if (ad == NULL) {
4285 return -1;
4288 p = ad_get_entry(ad, ADEID_FINDERI);
4289 if (p == NULL) {
4290 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4291 TALLOC_FREE(ad);
4292 return -1;
4295 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4297 ret = ad_fset(ad, fsp);
4298 if (ret != 0) {
4299 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4300 TALLOC_FREE(ad);
4301 return -1;
4304 TALLOC_FREE(ad);
4306 if (!ai_empty_finderinfo(ai)) {
4307 return n;
4310 ok = set_delete_on_close(
4311 fsp,
4312 true,
4313 handle->conn->session_info->security_token,
4314 handle->conn->session_info->unix_token);
4315 if (!ok) {
4316 DBG_ERR("set_delete_on_close on [%s] failed\n",
4317 fsp_str_dbg(fsp));
4318 return -1;
4321 return n;
4324 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4325 files_struct *fsp, const void *data,
4326 size_t n, off_t offset)
4328 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4329 ssize_t nwritten;
4332 * Writing an all 0 blob to the metadata stream
4333 * results in the stream being removed on a macOS
4334 * server. This ensures we behave the same and it
4335 * verified by the "delete AFP_AfpInfo by writing all
4336 * 0" test.
4338 if (n != AFP_INFO_SIZE || offset != 0) {
4339 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4340 (intmax_t)offset, (intmax_t)n);
4341 return -1;
4344 if (fio == NULL) {
4345 DBG_ERR("Failed to fetch fsp extension");
4346 return -1;
4349 switch (fio->config->meta) {
4350 case FRUIT_META_STREAM:
4351 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4352 n, offset);
4353 break;
4355 case FRUIT_META_NETATALK:
4356 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4357 n, offset);
4358 break;
4360 default:
4361 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4362 return -1;
4365 return nwritten;
4368 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4369 files_struct *fsp, const void *data,
4370 size_t n, off_t offset)
4372 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4375 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4376 files_struct *fsp, const void *data,
4377 size_t n, off_t offset)
4379 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4382 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4383 files_struct *fsp, const void *data,
4384 size_t n, off_t offset)
4386 struct adouble *ad = NULL;
4387 ssize_t nwritten;
4388 int ret;
4390 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4391 if (ad == NULL) {
4392 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4393 return -1;
4396 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4397 offset + ad_getentryoff(ad, ADEID_RFORK));
4398 if (nwritten != n) {
4399 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4400 fsp_str_dbg(fsp), nwritten, n);
4401 TALLOC_FREE(ad);
4402 return -1;
4405 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4406 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4407 ret = ad_fset(ad, fsp);
4408 if (ret != 0) {
4409 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4410 TALLOC_FREE(ad);
4411 return -1;
4415 TALLOC_FREE(ad);
4416 return n;
4419 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4420 files_struct *fsp, const void *data,
4421 size_t n, off_t offset)
4423 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4424 ssize_t nwritten;
4426 if (fio == NULL) {
4427 DBG_ERR("Failed to fetch fsp extension");
4428 return -1;
4431 switch (fio->config->rsrc) {
4432 case FRUIT_RSRC_STREAM:
4433 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4434 break;
4436 case FRUIT_RSRC_ADFILE:
4437 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4438 break;
4440 case FRUIT_RSRC_XATTR:
4441 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4442 break;
4444 default:
4445 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4446 return -1;
4449 return nwritten;
4452 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4453 files_struct *fsp, const void *data,
4454 size_t n, off_t offset)
4456 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4457 ssize_t nwritten;
4459 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4460 fsp_str_dbg(fsp), (intmax_t)offset, n);
4462 if (fio == NULL) {
4463 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4466 if (fio->type == ADOUBLE_META) {
4467 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4468 } else {
4469 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4472 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4473 return nwritten;
4476 struct fruit_pwrite_state {
4477 ssize_t nwritten;
4478 struct vfs_aio_state vfs_aio_state;
4481 static void fruit_pwrite_done(struct tevent_req *subreq);
4483 static struct tevent_req *fruit_pwrite_send(
4484 struct vfs_handle_struct *handle,
4485 TALLOC_CTX *mem_ctx,
4486 struct tevent_context *ev,
4487 struct files_struct *fsp,
4488 const void *data,
4489 size_t n, off_t offset)
4491 struct tevent_req *req = NULL;
4492 struct tevent_req *subreq = NULL;
4493 struct fruit_pwrite_state *state = NULL;
4494 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4496 req = tevent_req_create(mem_ctx, &state,
4497 struct fruit_pwrite_state);
4498 if (req == NULL) {
4499 return NULL;
4502 if (fruit_must_handle_aio_stream(fio)) {
4503 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4504 if (state->nwritten != n) {
4505 if (state->nwritten != -1) {
4506 errno = EIO;
4508 tevent_req_error(req, errno);
4509 return tevent_req_post(req, ev);
4511 tevent_req_done(req);
4512 return tevent_req_post(req, ev);
4515 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4516 data, n, offset);
4517 if (tevent_req_nomem(req, subreq)) {
4518 return tevent_req_post(req, ev);
4520 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4521 return req;
4524 static void fruit_pwrite_done(struct tevent_req *subreq)
4526 struct tevent_req *req = tevent_req_callback_data(
4527 subreq, struct tevent_req);
4528 struct fruit_pwrite_state *state = tevent_req_data(
4529 req, struct fruit_pwrite_state);
4531 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4532 TALLOC_FREE(subreq);
4534 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4535 return;
4537 tevent_req_done(req);
4540 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4541 struct vfs_aio_state *vfs_aio_state)
4543 struct fruit_pwrite_state *state = tevent_req_data(
4544 req, struct fruit_pwrite_state);
4546 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4547 return -1;
4550 *vfs_aio_state = state->vfs_aio_state;
4551 return state->nwritten;
4555 * Helper to stat/lstat the base file of an smb_fname.
4557 static int fruit_stat_base(vfs_handle_struct *handle,
4558 struct smb_filename *smb_fname,
4559 bool follow_links)
4561 char *tmp_stream_name;
4562 int rc;
4564 tmp_stream_name = smb_fname->stream_name;
4565 smb_fname->stream_name = NULL;
4566 if (follow_links) {
4567 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4568 } else {
4569 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4571 smb_fname->stream_name = tmp_stream_name;
4572 return rc;
4575 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4576 struct smb_filename *smb_fname,
4577 bool follow_links)
4579 int ret;
4581 if (follow_links) {
4582 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4583 } else {
4584 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4587 return ret;
4590 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4591 struct smb_filename *smb_fname,
4592 bool follow_links)
4594 struct adouble *ad = NULL;
4596 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4597 if (ad == NULL) {
4598 DBG_INFO("fruit_stat_meta %s: %s\n",
4599 smb_fname_str_dbg(smb_fname), strerror(errno));
4600 errno = ENOENT;
4601 return -1;
4603 TALLOC_FREE(ad);
4605 /* Populate the stat struct with info from the base file. */
4606 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4607 return -1;
4609 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4610 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4611 smb_fname->stream_name);
4612 return 0;
4615 static int fruit_stat_meta(vfs_handle_struct *handle,
4616 struct smb_filename *smb_fname,
4617 bool follow_links)
4619 struct fruit_config_data *config = NULL;
4620 int ret;
4622 SMB_VFS_HANDLE_GET_DATA(handle, config,
4623 struct fruit_config_data, return -1);
4625 switch (config->meta) {
4626 case FRUIT_META_STREAM:
4627 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4628 break;
4630 case FRUIT_META_NETATALK:
4631 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4632 break;
4634 default:
4635 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4636 return -1;
4639 return ret;
4642 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4643 struct smb_filename *smb_fname,
4644 bool follow_links)
4646 struct adouble *ad = NULL;
4647 int ret;
4649 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4650 if (ad == NULL) {
4651 errno = ENOENT;
4652 return -1;
4655 /* Populate the stat struct with info from the base file. */
4656 ret = fruit_stat_base(handle, smb_fname, follow_links);
4657 if (ret != 0) {
4658 TALLOC_FREE(ad);
4659 return -1;
4662 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4663 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4664 smb_fname->stream_name);
4665 TALLOC_FREE(ad);
4666 return 0;
4669 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4670 struct smb_filename *smb_fname,
4671 bool follow_links)
4673 int ret;
4675 if (follow_links) {
4676 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4677 } else {
4678 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4681 return ret;
4684 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4685 struct smb_filename *smb_fname,
4686 bool follow_links)
4688 #ifdef HAVE_ATTROPEN
4689 int ret;
4690 int fd = -1;
4692 /* Populate the stat struct with info from the base file. */
4693 ret = fruit_stat_base(handle, smb_fname, follow_links);
4694 if (ret != 0) {
4695 return -1;
4698 fd = attropen(smb_fname->base_name,
4699 AFPRESOURCE_EA_NETATALK,
4700 O_RDONLY);
4701 if (fd == -1) {
4702 return 0;
4705 ret = sys_fstat(fd, &smb_fname->st, false);
4706 if (ret != 0) {
4707 close(fd);
4708 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4709 AFPRESOURCE_EA_NETATALK);
4710 return -1;
4712 close(fd);
4713 fd = -1;
4715 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4716 smb_fname->stream_name);
4718 return ret;
4720 #else
4721 errno = ENOSYS;
4722 return -1;
4723 #endif
4726 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4727 struct smb_filename *smb_fname,
4728 bool follow_links)
4730 struct fruit_config_data *config = NULL;
4731 int ret;
4733 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4735 SMB_VFS_HANDLE_GET_DATA(handle, config,
4736 struct fruit_config_data, return -1);
4738 switch (config->rsrc) {
4739 case FRUIT_RSRC_STREAM:
4740 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4741 break;
4743 case FRUIT_RSRC_XATTR:
4744 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4745 break;
4747 case FRUIT_RSRC_ADFILE:
4748 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4749 break;
4751 default:
4752 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4753 return -1;
4756 return ret;
4759 static int fruit_stat(vfs_handle_struct *handle,
4760 struct smb_filename *smb_fname)
4762 int rc = -1;
4764 DEBUG(10, ("fruit_stat called for %s\n",
4765 smb_fname_str_dbg(smb_fname)));
4767 if (!is_ntfs_stream_smb_fname(smb_fname)
4768 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4769 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4770 if (rc == 0) {
4771 update_btime(handle, smb_fname);
4773 return rc;
4777 * Note if lp_posix_paths() is true, we can never
4778 * get here as is_ntfs_stream_smb_fname() is
4779 * always false. So we never need worry about
4780 * not following links here.
4783 if (is_afpinfo_stream(smb_fname)) {
4784 rc = fruit_stat_meta(handle, smb_fname, true);
4785 } else if (is_afpresource_stream(smb_fname)) {
4786 rc = fruit_stat_rsrc(handle, smb_fname, true);
4787 } else {
4788 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4791 if (rc == 0) {
4792 update_btime(handle, smb_fname);
4793 smb_fname->st.st_ex_mode &= ~S_IFMT;
4794 smb_fname->st.st_ex_mode |= S_IFREG;
4795 smb_fname->st.st_ex_blocks =
4796 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4798 return rc;
4801 static int fruit_lstat(vfs_handle_struct *handle,
4802 struct smb_filename *smb_fname)
4804 int rc = -1;
4806 DEBUG(10, ("fruit_lstat called for %s\n",
4807 smb_fname_str_dbg(smb_fname)));
4809 if (!is_ntfs_stream_smb_fname(smb_fname)
4810 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4811 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4812 if (rc == 0) {
4813 update_btime(handle, smb_fname);
4815 return rc;
4818 if (is_afpinfo_stream(smb_fname)) {
4819 rc = fruit_stat_meta(handle, smb_fname, false);
4820 } else if (is_afpresource_stream(smb_fname)) {
4821 rc = fruit_stat_rsrc(handle, smb_fname, false);
4822 } else {
4823 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4826 if (rc == 0) {
4827 update_btime(handle, smb_fname);
4828 smb_fname->st.st_ex_mode &= ~S_IFMT;
4829 smb_fname->st.st_ex_mode |= S_IFREG;
4830 smb_fname->st.st_ex_blocks =
4831 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4833 return rc;
4836 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4837 files_struct *fsp,
4838 SMB_STRUCT_STAT *sbuf)
4840 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4843 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4844 files_struct *fsp,
4845 SMB_STRUCT_STAT *sbuf)
4847 int ret;
4849 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4850 if (ret != 0) {
4851 return -1;
4854 *sbuf = fsp->base_fsp->fsp_name->st;
4855 sbuf->st_ex_size = AFP_INFO_SIZE;
4856 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4858 return 0;
4861 static int fruit_fstat_meta(vfs_handle_struct *handle,
4862 files_struct *fsp,
4863 SMB_STRUCT_STAT *sbuf,
4864 struct fio *fio)
4866 int ret;
4868 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4870 switch (fio->config->meta) {
4871 case FRUIT_META_STREAM:
4872 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4873 break;
4875 case FRUIT_META_NETATALK:
4876 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4877 break;
4879 default:
4880 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4881 return -1;
4884 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4885 return ret;
4888 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4889 files_struct *fsp,
4890 SMB_STRUCT_STAT *sbuf)
4892 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4895 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4896 files_struct *fsp,
4897 SMB_STRUCT_STAT *sbuf)
4899 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4902 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4903 files_struct *fsp,
4904 SMB_STRUCT_STAT *sbuf)
4906 struct adouble *ad = NULL;
4907 int ret;
4909 /* Populate the stat struct with info from the base file. */
4910 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4911 if (ret == -1) {
4912 return -1;
4915 ad = ad_get(talloc_tos(), handle,
4916 fsp->base_fsp->fsp_name,
4917 ADOUBLE_RSRC);
4918 if (ad == NULL) {
4919 DBG_ERR("ad_get [%s] failed [%s]\n",
4920 fsp_str_dbg(fsp), strerror(errno));
4921 return -1;
4924 *sbuf = fsp->base_fsp->fsp_name->st;
4925 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4926 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4928 TALLOC_FREE(ad);
4929 return 0;
4932 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
4933 SMB_STRUCT_STAT *sbuf, struct fio *fio)
4935 int ret;
4937 switch (fio->config->rsrc) {
4938 case FRUIT_RSRC_STREAM:
4939 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
4940 break;
4942 case FRUIT_RSRC_ADFILE:
4943 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
4944 break;
4946 case FRUIT_RSRC_XATTR:
4947 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
4948 break;
4950 default:
4951 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4952 return -1;
4955 return ret;
4958 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
4959 SMB_STRUCT_STAT *sbuf)
4961 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4962 int rc;
4964 if (fio == NULL) {
4965 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4968 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4970 if (fio->type == ADOUBLE_META) {
4971 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
4972 } else {
4973 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
4976 if (rc == 0) {
4977 sbuf->st_ex_mode &= ~S_IFMT;
4978 sbuf->st_ex_mode |= S_IFREG;
4979 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
4982 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
4983 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
4984 return rc;
4987 static NTSTATUS delete_invalid_meta_stream(
4988 vfs_handle_struct *handle,
4989 const struct smb_filename *smb_fname,
4990 TALLOC_CTX *mem_ctx,
4991 unsigned int *pnum_streams,
4992 struct stream_struct **pstreams)
4994 struct smb_filename *sname = NULL;
4995 int ret;
4996 bool ok;
4998 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
4999 if (!ok) {
5000 return NT_STATUS_INTERNAL_ERROR;
5003 sname = synthetic_smb_fname(talloc_tos(),
5004 smb_fname->base_name,
5005 AFPINFO_STREAM_NAME,
5006 NULL, 0);
5007 if (sname == NULL) {
5008 return NT_STATUS_NO_MEMORY;
5011 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5012 TALLOC_FREE(sname);
5013 if (ret != 0) {
5014 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5015 return map_nt_error_from_unix(errno);
5018 return NT_STATUS_OK;
5021 static NTSTATUS fruit_streaminfo_meta_stream(
5022 vfs_handle_struct *handle,
5023 struct files_struct *fsp,
5024 const struct smb_filename *smb_fname,
5025 TALLOC_CTX *mem_ctx,
5026 unsigned int *pnum_streams,
5027 struct stream_struct **pstreams)
5029 struct stream_struct *stream = *pstreams;
5030 unsigned int num_streams = *pnum_streams;
5031 struct smb_filename *sname = NULL;
5032 char *full_name = NULL;
5033 uint32_t name_hash;
5034 struct share_mode_lock *lck = NULL;
5035 struct file_id id = {0};
5036 bool delete_on_close_set;
5037 int i;
5038 int ret;
5039 NTSTATUS status;
5040 bool ok;
5042 for (i = 0; i < num_streams; i++) {
5043 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5044 break;
5048 if (i == num_streams) {
5049 return NT_STATUS_OK;
5052 if (stream[i].size != AFP_INFO_SIZE) {
5053 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5054 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5056 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5057 pnum_streams, pstreams);
5061 * Now check if there's a delete-on-close pending on the stream. If so,
5062 * hide the stream. This behaviour was verified against a macOS 10.12
5063 * SMB server.
5066 sname = synthetic_smb_fname(talloc_tos(),
5067 smb_fname->base_name,
5068 AFPINFO_STREAM_NAME,
5069 NULL, 0);
5070 if (sname == NULL) {
5071 status = NT_STATUS_NO_MEMORY;
5072 goto out;
5075 ret = SMB_VFS_NEXT_STAT(handle, sname);
5076 if (ret != 0) {
5077 status = map_nt_error_from_unix(errno);
5078 goto out;
5081 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5083 lck = get_existing_share_mode_lock(talloc_tos(), id);
5084 if (lck == NULL) {
5085 status = NT_STATUS_OK;
5086 goto out;
5089 full_name = talloc_asprintf(talloc_tos(),
5090 "%s%s",
5091 sname->base_name,
5092 AFPINFO_STREAM);
5093 if (full_name == NULL) {
5094 status = NT_STATUS_NO_MEMORY;
5095 goto out;
5098 status = file_name_hash(handle->conn, full_name, &name_hash);
5099 if (!NT_STATUS_IS_OK(status)) {
5100 goto out;
5103 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5104 if (delete_on_close_set) {
5105 ok = del_fruit_stream(mem_ctx,
5106 pnum_streams,
5107 pstreams,
5108 AFPINFO_STREAM);
5109 if (!ok) {
5110 status = NT_STATUS_INTERNAL_ERROR;
5111 goto out;
5115 status = NT_STATUS_OK;
5117 out:
5118 TALLOC_FREE(sname);
5119 TALLOC_FREE(lck);
5120 TALLOC_FREE(full_name);
5121 return status;
5124 static NTSTATUS fruit_streaminfo_meta_netatalk(
5125 vfs_handle_struct *handle,
5126 struct files_struct *fsp,
5127 const struct smb_filename *smb_fname,
5128 TALLOC_CTX *mem_ctx,
5129 unsigned int *pnum_streams,
5130 struct stream_struct **pstreams)
5132 struct stream_struct *stream = *pstreams;
5133 unsigned int num_streams = *pnum_streams;
5134 struct adouble *ad = NULL;
5135 bool is_fi_empty;
5136 int i;
5137 bool ok;
5139 /* Remove the Netatalk xattr from the list */
5140 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5141 ":" NETATALK_META_XATTR ":$DATA");
5142 if (!ok) {
5143 return NT_STATUS_NO_MEMORY;
5147 * Check if there's a AFPINFO_STREAM from the VFS streams
5148 * backend and if yes, remove it from the list
5150 for (i = 0; i < num_streams; i++) {
5151 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5152 break;
5156 if (i < num_streams) {
5157 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5158 smb_fname_str_dbg(smb_fname));
5160 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5161 AFPINFO_STREAM);
5162 if (!ok) {
5163 return NT_STATUS_INTERNAL_ERROR;
5167 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5168 if (ad == NULL) {
5169 return NT_STATUS_OK;
5172 is_fi_empty = ad_empty_finderinfo(ad);
5173 TALLOC_FREE(ad);
5175 if (is_fi_empty) {
5176 return NT_STATUS_OK;
5179 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5180 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5181 smb_roundup(handle->conn, AFP_INFO_SIZE));
5182 if (!ok) {
5183 return NT_STATUS_NO_MEMORY;
5186 return NT_STATUS_OK;
5189 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5190 struct files_struct *fsp,
5191 const struct smb_filename *smb_fname,
5192 TALLOC_CTX *mem_ctx,
5193 unsigned int *pnum_streams,
5194 struct stream_struct **pstreams)
5196 struct fruit_config_data *config = NULL;
5197 NTSTATUS status;
5199 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5200 return NT_STATUS_INTERNAL_ERROR);
5202 switch (config->meta) {
5203 case FRUIT_META_NETATALK:
5204 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5205 mem_ctx, pnum_streams,
5206 pstreams);
5207 break;
5209 case FRUIT_META_STREAM:
5210 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5211 mem_ctx, pnum_streams,
5212 pstreams);
5213 break;
5215 default:
5216 return NT_STATUS_INTERNAL_ERROR;
5219 return status;
5222 static NTSTATUS fruit_streaminfo_rsrc_stream(
5223 vfs_handle_struct *handle,
5224 struct files_struct *fsp,
5225 const struct smb_filename *smb_fname,
5226 TALLOC_CTX *mem_ctx,
5227 unsigned int *pnum_streams,
5228 struct stream_struct **pstreams)
5230 bool ok;
5232 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5233 if (!ok) {
5234 DBG_ERR("Filtering resource stream failed\n");
5235 return NT_STATUS_INTERNAL_ERROR;
5237 return NT_STATUS_OK;
5240 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5241 vfs_handle_struct *handle,
5242 struct files_struct *fsp,
5243 const struct smb_filename *smb_fname,
5244 TALLOC_CTX *mem_ctx,
5245 unsigned int *pnum_streams,
5246 struct stream_struct **pstreams)
5248 bool ok;
5250 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5251 if (!ok) {
5252 DBG_ERR("Filtering resource stream failed\n");
5253 return NT_STATUS_INTERNAL_ERROR;
5255 return NT_STATUS_OK;
5258 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5259 vfs_handle_struct *handle,
5260 struct files_struct *fsp,
5261 const struct smb_filename *smb_fname,
5262 TALLOC_CTX *mem_ctx,
5263 unsigned int *pnum_streams,
5264 struct stream_struct **pstreams)
5266 struct stream_struct *stream = *pstreams;
5267 unsigned int num_streams = *pnum_streams;
5268 struct adouble *ad = NULL;
5269 bool ok;
5270 size_t rlen;
5271 int i;
5274 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5275 * and if yes, remove it from the list
5277 for (i = 0; i < num_streams; i++) {
5278 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5279 break;
5283 if (i < num_streams) {
5284 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5285 smb_fname_str_dbg(smb_fname));
5287 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5288 AFPRESOURCE_STREAM);
5289 if (!ok) {
5290 return NT_STATUS_INTERNAL_ERROR;
5294 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5295 if (ad == NULL) {
5296 return NT_STATUS_OK;
5299 rlen = ad_getentrylen(ad, ADEID_RFORK);
5300 TALLOC_FREE(ad);
5302 if (rlen == 0) {
5303 return NT_STATUS_OK;
5306 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5307 AFPRESOURCE_STREAM_NAME, rlen,
5308 smb_roundup(handle->conn, rlen));
5309 if (!ok) {
5310 return NT_STATUS_NO_MEMORY;
5313 return NT_STATUS_OK;
5316 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5317 struct files_struct *fsp,
5318 const struct smb_filename *smb_fname,
5319 TALLOC_CTX *mem_ctx,
5320 unsigned int *pnum_streams,
5321 struct stream_struct **pstreams)
5323 struct fruit_config_data *config = NULL;
5324 NTSTATUS status;
5326 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5327 return NT_STATUS_INTERNAL_ERROR);
5329 switch (config->rsrc) {
5330 case FRUIT_RSRC_STREAM:
5331 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5332 mem_ctx, pnum_streams,
5333 pstreams);
5334 break;
5336 case FRUIT_RSRC_XATTR:
5337 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5338 mem_ctx, pnum_streams,
5339 pstreams);
5340 break;
5342 case FRUIT_RSRC_ADFILE:
5343 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5344 mem_ctx, pnum_streams,
5345 pstreams);
5346 break;
5348 default:
5349 return NT_STATUS_INTERNAL_ERROR;
5352 return status;
5355 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5356 struct files_struct *fsp,
5357 const struct smb_filename *smb_fname,
5358 TALLOC_CTX *mem_ctx,
5359 unsigned int *pnum_streams,
5360 struct stream_struct **pstreams)
5362 struct fruit_config_data *config = NULL;
5363 NTSTATUS status;
5365 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5366 return NT_STATUS_UNSUCCESSFUL);
5368 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5370 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5371 pnum_streams, pstreams);
5372 if (!NT_STATUS_IS_OK(status)) {
5373 return status;
5376 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5377 mem_ctx, pnum_streams, pstreams);
5378 if (!NT_STATUS_IS_OK(status)) {
5379 return status;
5382 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5383 mem_ctx, pnum_streams, pstreams);
5384 if (!NT_STATUS_IS_OK(status)) {
5385 return status;
5388 return NT_STATUS_OK;
5391 static int fruit_ntimes(vfs_handle_struct *handle,
5392 const struct smb_filename *smb_fname,
5393 struct smb_file_time *ft)
5395 int rc = 0;
5396 struct adouble *ad = NULL;
5397 struct fruit_config_data *config = NULL;
5399 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5400 return -1);
5402 if ((config->meta != FRUIT_META_NETATALK) ||
5403 null_timespec(ft->create_time))
5405 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5408 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5409 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5411 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5412 if (ad == NULL) {
5413 goto exit;
5416 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5417 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5419 rc = ad_set(ad, smb_fname);
5421 exit:
5423 TALLOC_FREE(ad);
5424 if (rc != 0) {
5425 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5426 return -1;
5428 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5431 static int fruit_fallocate(struct vfs_handle_struct *handle,
5432 struct files_struct *fsp,
5433 uint32_t mode,
5434 off_t offset,
5435 off_t len)
5437 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5439 if (fio == NULL) {
5440 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5443 /* Let the pwrite code path handle it. */
5444 errno = ENOSYS;
5445 return -1;
5448 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5449 struct files_struct *fsp,
5450 off_t offset)
5452 if (offset == 0) {
5453 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5456 #ifdef HAVE_ATTROPEN
5457 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5458 #endif
5459 return 0;
5462 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5463 struct files_struct *fsp,
5464 off_t offset)
5466 int rc;
5467 struct adouble *ad = NULL;
5468 off_t ad_off;
5470 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5471 if (ad == NULL) {
5472 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5473 fsp_str_dbg(fsp), strerror(errno));
5474 return -1;
5477 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5479 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5480 if (rc != 0) {
5481 TALLOC_FREE(ad);
5482 return -1;
5485 ad_setentrylen(ad, ADEID_RFORK, offset);
5487 rc = ad_fset(ad, fsp);
5488 if (rc != 0) {
5489 DBG_ERR("ad_fset [%s] failed [%s]\n",
5490 fsp_str_dbg(fsp), strerror(errno));
5491 TALLOC_FREE(ad);
5492 return -1;
5495 TALLOC_FREE(ad);
5496 return 0;
5499 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5500 struct files_struct *fsp,
5501 off_t offset)
5503 if (offset == 0) {
5504 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5507 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5510 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5511 struct files_struct *fsp,
5512 off_t offset)
5514 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5515 int ret;
5517 if (fio == NULL) {
5518 DBG_ERR("Failed to fetch fsp extension");
5519 return -1;
5522 switch (fio->config->rsrc) {
5523 case FRUIT_RSRC_XATTR:
5524 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5525 break;
5527 case FRUIT_RSRC_ADFILE:
5528 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5529 break;
5531 case FRUIT_RSRC_STREAM:
5532 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5533 break;
5535 default:
5536 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5537 return -1;
5541 return ret;
5544 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5545 struct files_struct *fsp,
5546 off_t offset)
5548 if (offset > 60) {
5549 DBG_WARNING("ftruncate %s to %jd",
5550 fsp_str_dbg(fsp), (intmax_t)offset);
5551 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5552 errno = EOVERFLOW;
5553 return -1;
5556 /* OS X returns success but does nothing */
5557 DBG_INFO("ignoring ftruncate %s to %jd\n",
5558 fsp_str_dbg(fsp), (intmax_t)offset);
5559 return 0;
5562 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5563 struct files_struct *fsp,
5564 off_t offset)
5566 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5567 int ret;
5569 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5570 (intmax_t)offset);
5572 if (fio == NULL) {
5573 if (offset == 0 &&
5574 global_fruit_config.nego_aapl &&
5575 is_ntfs_stream_smb_fname(fsp->fsp_name) &&
5576 !is_ntfs_default_stream_smb_fname(fsp->fsp_name))
5578 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5580 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5583 if (fio->type == ADOUBLE_META) {
5584 ret = fruit_ftruncate_meta(handle, fsp, offset);
5585 } else {
5586 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5589 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5590 return ret;
5593 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5594 struct smb_request *req,
5595 uint16_t root_dir_fid,
5596 struct smb_filename *smb_fname,
5597 uint32_t access_mask,
5598 uint32_t share_access,
5599 uint32_t create_disposition,
5600 uint32_t create_options,
5601 uint32_t file_attributes,
5602 uint32_t oplock_request,
5603 struct smb2_lease *lease,
5604 uint64_t allocation_size,
5605 uint32_t private_flags,
5606 struct security_descriptor *sd,
5607 struct ea_list *ea_list,
5608 files_struct **result,
5609 int *pinfo,
5610 const struct smb2_create_blobs *in_context_blobs,
5611 struct smb2_create_blobs *out_context_blobs)
5613 NTSTATUS status;
5614 struct fruit_config_data *config = NULL;
5615 files_struct *fsp = NULL;
5617 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5618 if (!NT_STATUS_IS_OK(status)) {
5619 goto fail;
5622 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5623 return NT_STATUS_UNSUCCESSFUL);
5625 status = SMB_VFS_NEXT_CREATE_FILE(
5626 handle, req, root_dir_fid, smb_fname,
5627 access_mask, share_access,
5628 create_disposition, create_options,
5629 file_attributes, oplock_request,
5630 lease,
5631 allocation_size, private_flags,
5632 sd, ea_list, result,
5633 pinfo, in_context_blobs, out_context_blobs);
5634 if (!NT_STATUS_IS_OK(status)) {
5635 return status;
5638 fsp = *result;
5640 if (global_fruit_config.nego_aapl) {
5641 if (config->posix_rename && fsp->is_directory) {
5643 * Enable POSIX directory rename behaviour
5645 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5650 * If this is a plain open for existing files, opening an 0
5651 * byte size resource fork MUST fail with
5652 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5654 * Cf the vfs_fruit torture tests in test_rfork_create().
5656 if (is_afpresource_stream(fsp->fsp_name) &&
5657 create_disposition == FILE_OPEN)
5659 if (fsp->fsp_name->st.st_ex_size == 0) {
5660 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5661 goto fail;
5665 if (is_ntfs_stream_smb_fname(smb_fname)
5666 || fsp->is_directory) {
5667 return status;
5670 if (config->locking == FRUIT_LOCKING_NETATALK) {
5671 status = fruit_check_access(
5672 handle, *result,
5673 access_mask,
5674 map_share_mode_to_deny_mode(share_access, 0));
5675 if (!NT_STATUS_IS_OK(status)) {
5676 goto fail;
5680 return status;
5682 fail:
5683 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5685 if (fsp) {
5686 close_file(req, fsp, ERROR_CLOSE);
5687 *result = fsp = NULL;
5690 return status;
5693 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5694 const struct smb_filename *fname,
5695 TALLOC_CTX *mem_ctx,
5696 struct readdir_attr_data **pattr_data)
5698 struct fruit_config_data *config = NULL;
5699 struct readdir_attr_data *attr_data;
5700 NTSTATUS status;
5702 SMB_VFS_HANDLE_GET_DATA(handle, config,
5703 struct fruit_config_data,
5704 return NT_STATUS_UNSUCCESSFUL);
5706 if (!global_fruit_config.nego_aapl) {
5707 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5710 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5712 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5713 if (*pattr_data == NULL) {
5714 return NT_STATUS_UNSUCCESSFUL;
5716 attr_data = *pattr_data;
5717 attr_data->type = RDATTR_AAPL;
5720 * Mac metadata: compressed FinderInfo, resource fork length
5721 * and creation date
5723 status = readdir_attr_macmeta(handle, fname, attr_data);
5724 if (!NT_STATUS_IS_OK(status)) {
5726 * Error handling is tricky: if we return failure from
5727 * this function, the corresponding directory entry
5728 * will to be passed to the client, so we really just
5729 * want to error out on fatal errors.
5731 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5732 goto fail;
5737 * UNIX mode
5739 if (config->unix_info_enabled) {
5740 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5744 * max_access
5746 if (!config->readdir_attr_max_access) {
5747 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5748 } else {
5749 status = smbd_calculate_access_mask(
5750 handle->conn,
5751 fname,
5752 false,
5753 SEC_FLAG_MAXIMUM_ALLOWED,
5754 &attr_data->attr_data.aapl.max_access);
5755 if (!NT_STATUS_IS_OK(status)) {
5756 goto fail;
5760 return NT_STATUS_OK;
5762 fail:
5763 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5764 fname->base_name, nt_errstr(status)));
5765 TALLOC_FREE(*pattr_data);
5766 return status;
5769 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5770 files_struct *fsp,
5771 uint32_t security_info,
5772 TALLOC_CTX *mem_ctx,
5773 struct security_descriptor **ppdesc)
5775 NTSTATUS status;
5776 struct security_ace ace;
5777 struct dom_sid sid;
5778 struct fruit_config_data *config;
5780 SMB_VFS_HANDLE_GET_DATA(handle, config,
5781 struct fruit_config_data,
5782 return NT_STATUS_UNSUCCESSFUL);
5784 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5785 mem_ctx, ppdesc);
5786 if (!NT_STATUS_IS_OK(status)) {
5787 return status;
5791 * Add MS NFS style ACEs with uid, gid and mode
5793 if (!global_fruit_config.nego_aapl) {
5794 return NT_STATUS_OK;
5796 if (!config->unix_info_enabled) {
5797 return NT_STATUS_OK;
5800 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5801 status = remove_virtual_nfs_aces(*ppdesc);
5802 if (!NT_STATUS_IS_OK(status)) {
5803 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5804 return status;
5807 /* MS NFS style mode */
5808 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5809 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5810 status = security_descriptor_dacl_add(*ppdesc, &ace);
5811 if (!NT_STATUS_IS_OK(status)) {
5812 DEBUG(1,("failed to add MS NFS style ACE\n"));
5813 return status;
5816 /* MS NFS style uid */
5817 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5818 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5819 status = security_descriptor_dacl_add(*ppdesc, &ace);
5820 if (!NT_STATUS_IS_OK(status)) {
5821 DEBUG(1,("failed to add MS NFS style ACE\n"));
5822 return status;
5825 /* MS NFS style gid */
5826 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5827 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5828 status = security_descriptor_dacl_add(*ppdesc, &ace);
5829 if (!NT_STATUS_IS_OK(status)) {
5830 DEBUG(1,("failed to add MS NFS style ACE\n"));
5831 return status;
5834 return NT_STATUS_OK;
5837 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5838 files_struct *fsp,
5839 uint32_t security_info_sent,
5840 const struct security_descriptor *orig_psd)
5842 NTSTATUS status;
5843 bool do_chmod;
5844 mode_t ms_nfs_mode = 0;
5845 int result;
5846 struct security_descriptor *psd = NULL;
5847 uint32_t orig_num_aces = 0;
5849 if (orig_psd->dacl != NULL) {
5850 orig_num_aces = orig_psd->dacl->num_aces;
5853 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5854 if (psd == NULL) {
5855 return NT_STATUS_NO_MEMORY;
5858 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5860 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5861 if (!NT_STATUS_IS_OK(status)) {
5862 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5863 TALLOC_FREE(psd);
5864 return status;
5868 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5869 * sent/present flags correctly now we've removed them.
5872 if (orig_num_aces != 0) {
5874 * Are there any ACE's left ?
5876 if (psd->dacl->num_aces == 0) {
5877 /* No - clear the DACL sent/present flags. */
5878 security_info_sent &= ~SECINFO_DACL;
5879 psd->type &= ~SEC_DESC_DACL_PRESENT;
5883 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5884 if (!NT_STATUS_IS_OK(status)) {
5885 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5886 TALLOC_FREE(psd);
5887 return status;
5890 if (do_chmod) {
5891 if (fsp->fh->fd != -1) {
5892 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5893 } else {
5894 result = SMB_VFS_CHMOD(fsp->conn,
5895 fsp->fsp_name,
5896 ms_nfs_mode);
5899 if (result != 0) {
5900 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5901 result, (unsigned)ms_nfs_mode,
5902 strerror(errno)));
5903 status = map_nt_error_from_unix(errno);
5904 TALLOC_FREE(psd);
5905 return status;
5909 TALLOC_FREE(psd);
5910 return NT_STATUS_OK;
5913 static struct vfs_offload_ctx *fruit_offload_ctx;
5915 struct fruit_offload_read_state {
5916 struct vfs_handle_struct *handle;
5917 struct tevent_context *ev;
5918 files_struct *fsp;
5919 uint32_t fsctl;
5920 DATA_BLOB token;
5923 static void fruit_offload_read_done(struct tevent_req *subreq);
5925 static struct tevent_req *fruit_offload_read_send(
5926 TALLOC_CTX *mem_ctx,
5927 struct tevent_context *ev,
5928 struct vfs_handle_struct *handle,
5929 files_struct *fsp,
5930 uint32_t fsctl,
5931 uint32_t ttl,
5932 off_t offset,
5933 size_t to_copy)
5935 struct tevent_req *req = NULL;
5936 struct tevent_req *subreq = NULL;
5937 struct fruit_offload_read_state *state = NULL;
5939 req = tevent_req_create(mem_ctx, &state,
5940 struct fruit_offload_read_state);
5941 if (req == NULL) {
5942 return NULL;
5944 *state = (struct fruit_offload_read_state) {
5945 .handle = handle,
5946 .ev = ev,
5947 .fsp = fsp,
5948 .fsctl = fsctl,
5951 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
5952 fsctl, ttl, offset, to_copy);
5953 if (tevent_req_nomem(subreq, req)) {
5954 return tevent_req_post(req, ev);
5956 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
5957 return req;
5960 static void fruit_offload_read_done(struct tevent_req *subreq)
5962 struct tevent_req *req = tevent_req_callback_data(
5963 subreq, struct tevent_req);
5964 struct fruit_offload_read_state *state = tevent_req_data(
5965 req, struct fruit_offload_read_state);
5966 NTSTATUS status;
5968 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
5969 state->handle,
5970 state,
5971 &state->token);
5972 TALLOC_FREE(subreq);
5973 if (tevent_req_nterror(req, status)) {
5974 return;
5977 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
5978 tevent_req_done(req);
5979 return;
5982 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
5983 &fruit_offload_ctx);
5984 if (tevent_req_nterror(req, status)) {
5985 return;
5988 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
5989 state->fsp,
5990 &state->token);
5991 if (tevent_req_nterror(req, status)) {
5992 return;
5995 tevent_req_done(req);
5996 return;
5999 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6000 struct vfs_handle_struct *handle,
6001 TALLOC_CTX *mem_ctx,
6002 DATA_BLOB *token)
6004 struct fruit_offload_read_state *state = tevent_req_data(
6005 req, struct fruit_offload_read_state);
6006 NTSTATUS status;
6008 if (tevent_req_is_nterror(req, &status)) {
6009 tevent_req_received(req);
6010 return status;
6013 token->length = state->token.length;
6014 token->data = talloc_move(mem_ctx, &state->token.data);
6016 tevent_req_received(req);
6017 return NT_STATUS_OK;
6020 struct fruit_offload_write_state {
6021 struct vfs_handle_struct *handle;
6022 off_t copied;
6023 struct files_struct *src_fsp;
6024 struct files_struct *dst_fsp;
6025 bool is_copyfile;
6028 static void fruit_offload_write_done(struct tevent_req *subreq);
6029 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6030 TALLOC_CTX *mem_ctx,
6031 struct tevent_context *ev,
6032 uint32_t fsctl,
6033 DATA_BLOB *token,
6034 off_t transfer_offset,
6035 struct files_struct *dest_fsp,
6036 off_t dest_off,
6037 off_t num)
6039 struct tevent_req *req, *subreq;
6040 struct fruit_offload_write_state *state;
6041 NTSTATUS status;
6042 struct fruit_config_data *config;
6043 off_t src_off = transfer_offset;
6044 files_struct *src_fsp = NULL;
6045 off_t to_copy = num;
6046 bool copyfile_enabled = false;
6048 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6049 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6051 SMB_VFS_HANDLE_GET_DATA(handle, config,
6052 struct fruit_config_data,
6053 return NULL);
6055 req = tevent_req_create(mem_ctx, &state,
6056 struct fruit_offload_write_state);
6057 if (req == NULL) {
6058 return NULL;
6060 state->handle = handle;
6061 state->dst_fsp = dest_fsp;
6063 switch (fsctl) {
6064 case FSCTL_SRV_COPYCHUNK:
6065 case FSCTL_SRV_COPYCHUNK_WRITE:
6066 copyfile_enabled = config->copyfile_enabled;
6067 break;
6068 default:
6069 break;
6073 * Check if this a OS X copyfile style copychunk request with
6074 * a requested chunk count of 0 that was translated to a
6075 * offload_write_send VFS call overloading the parameters src_off
6076 * = dest_off = num = 0.
6078 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6079 status = vfs_offload_token_db_fetch_fsp(
6080 fruit_offload_ctx, token, &src_fsp);
6081 if (tevent_req_nterror(req, status)) {
6082 return tevent_req_post(req, ev);
6084 state->src_fsp = src_fsp;
6086 status = vfs_stat_fsp(src_fsp);
6087 if (tevent_req_nterror(req, status)) {
6088 return tevent_req_post(req, ev);
6091 to_copy = src_fsp->fsp_name->st.st_ex_size;
6092 state->is_copyfile = true;
6095 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6096 mem_ctx,
6098 fsctl,
6099 token,
6100 transfer_offset,
6101 dest_fsp,
6102 dest_off,
6103 to_copy);
6104 if (tevent_req_nomem(subreq, req)) {
6105 return tevent_req_post(req, ev);
6108 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6109 return req;
6112 static void fruit_offload_write_done(struct tevent_req *subreq)
6114 struct tevent_req *req = tevent_req_callback_data(
6115 subreq, struct tevent_req);
6116 struct fruit_offload_write_state *state = tevent_req_data(
6117 req, struct fruit_offload_write_state);
6118 NTSTATUS status;
6119 unsigned int num_streams = 0;
6120 struct stream_struct *streams = NULL;
6121 unsigned int i;
6122 struct smb_filename *src_fname_tmp = NULL;
6123 struct smb_filename *dst_fname_tmp = NULL;
6125 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6126 subreq,
6127 &state->copied);
6128 TALLOC_FREE(subreq);
6129 if (tevent_req_nterror(req, status)) {
6130 return;
6133 if (!state->is_copyfile) {
6134 tevent_req_done(req);
6135 return;
6139 * Now copy all remaining streams. We know the share supports
6140 * streams, because we're in vfs_fruit. We don't do this async
6141 * because streams are few and small.
6143 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6144 state->src_fsp->fsp_name,
6145 req, &num_streams, &streams);
6146 if (tevent_req_nterror(req, status)) {
6147 return;
6150 if (num_streams == 1) {
6151 /* There is always one stream, ::$DATA. */
6152 tevent_req_done(req);
6153 return;
6156 for (i = 0; i < num_streams; i++) {
6157 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6158 __func__, streams[i].name, (size_t)streams[i].size));
6160 src_fname_tmp = synthetic_smb_fname(
6161 req,
6162 state->src_fsp->fsp_name->base_name,
6163 streams[i].name,
6164 NULL,
6165 state->src_fsp->fsp_name->flags);
6166 if (tevent_req_nomem(src_fname_tmp, req)) {
6167 return;
6170 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6171 TALLOC_FREE(src_fname_tmp);
6172 continue;
6175 dst_fname_tmp = synthetic_smb_fname(
6176 req,
6177 state->dst_fsp->fsp_name->base_name,
6178 streams[i].name,
6179 NULL,
6180 state->dst_fsp->fsp_name->flags);
6181 if (tevent_req_nomem(dst_fname_tmp, req)) {
6182 TALLOC_FREE(src_fname_tmp);
6183 return;
6186 status = copy_file(req,
6187 state->handle->conn,
6188 src_fname_tmp,
6189 dst_fname_tmp,
6190 OPENX_FILE_CREATE_IF_NOT_EXIST,
6191 0, false);
6192 if (!NT_STATUS_IS_OK(status)) {
6193 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6194 smb_fname_str_dbg(src_fname_tmp),
6195 smb_fname_str_dbg(dst_fname_tmp),
6196 nt_errstr(status)));
6197 TALLOC_FREE(src_fname_tmp);
6198 TALLOC_FREE(dst_fname_tmp);
6199 tevent_req_nterror(req, status);
6200 return;
6203 TALLOC_FREE(src_fname_tmp);
6204 TALLOC_FREE(dst_fname_tmp);
6207 TALLOC_FREE(streams);
6208 TALLOC_FREE(src_fname_tmp);
6209 TALLOC_FREE(dst_fname_tmp);
6210 tevent_req_done(req);
6213 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6214 struct tevent_req *req,
6215 off_t *copied)
6217 struct fruit_offload_write_state *state = tevent_req_data(
6218 req, struct fruit_offload_write_state);
6219 NTSTATUS status;
6221 if (tevent_req_is_nterror(req, &status)) {
6222 DEBUG(1, ("server side copy chunk failed: %s\n",
6223 nt_errstr(status)));
6224 *copied = 0;
6225 tevent_req_received(req);
6226 return status;
6229 *copied = state->copied;
6230 tevent_req_received(req);
6232 return NT_STATUS_OK;
6235 static char *fruit_get_bandsize_line(char **lines, int numlines)
6237 static regex_t re;
6238 static bool re_initialized = false;
6239 int i;
6240 int ret;
6242 if (!re_initialized) {
6243 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6244 if (ret != 0) {
6245 return NULL;
6247 re_initialized = true;
6250 for (i = 0; i < numlines; i++) {
6251 regmatch_t matches[1];
6253 ret = regexec(&re, lines[i], 1, matches, 0);
6254 if (ret == 0) {
6256 * Check if the match was on the last line, sa we want
6257 * the subsequent line.
6259 if (i + 1 == numlines) {
6260 return NULL;
6262 return lines[i + 1];
6264 if (ret != REG_NOMATCH) {
6265 return NULL;
6269 return NULL;
6272 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6274 static regex_t re;
6275 static bool re_initialized = false;
6276 regmatch_t matches[2];
6277 uint64_t band_size;
6278 int ret;
6279 bool ok;
6281 if (!re_initialized) {
6282 ret = regcomp(&re,
6283 "^[[:blank:]]*"
6284 "<integer>\\([[:digit:]]*\\)</integer>$",
6286 if (ret != 0) {
6287 return false;
6289 re_initialized = true;
6292 ret = regexec(&re, line, 2, matches, 0);
6293 if (ret != 0) {
6294 DBG_ERR("regex failed [%s]\n", line);
6295 return false;
6298 line[matches[1].rm_eo] = '\0';
6300 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6301 if (!ok) {
6302 return false;
6304 *_band_size = (size_t)band_size;
6305 return true;
6309 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6310 * "band-size" key and value.
6312 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6313 const char *dir,
6314 size_t *band_size)
6316 #define INFO_PLIST_MAX_SIZE 64*1024
6317 char *plist = NULL;
6318 struct smb_filename *smb_fname = NULL;
6319 files_struct *fsp = NULL;
6320 uint8_t *file_data = NULL;
6321 char **lines = NULL;
6322 char *band_size_line = NULL;
6323 size_t plist_file_size;
6324 ssize_t nread;
6325 int numlines;
6326 int ret;
6327 bool ok = false;
6328 NTSTATUS status;
6330 plist = talloc_asprintf(talloc_tos(),
6331 "%s/%s/Info.plist",
6332 handle->conn->connectpath,
6333 dir);
6334 if (plist == NULL) {
6335 ok = false;
6336 goto out;
6339 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6340 if (smb_fname == NULL) {
6341 ok = false;
6342 goto out;
6345 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6346 if (ret != 0) {
6347 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6348 ok = true;
6349 goto out;
6352 plist_file_size = smb_fname->st.st_ex_size;
6354 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6355 DBG_INFO("%s is too large, ignoring\n", plist);
6356 ok = true;
6357 goto out;
6360 status = SMB_VFS_NEXT_CREATE_FILE(
6361 handle, /* conn */
6362 NULL, /* req */
6363 0, /* root_dir_fid */
6364 smb_fname, /* fname */
6365 FILE_GENERIC_READ, /* access_mask */
6366 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6367 FILE_OPEN, /* create_disposition */
6368 0, /* create_options */
6369 0, /* file_attributes */
6370 INTERNAL_OPEN_ONLY, /* oplock_request */
6371 NULL, /* lease */
6372 0, /* allocation_size */
6373 0, /* private_flags */
6374 NULL, /* sd */
6375 NULL, /* ea_list */
6376 &fsp, /* result */
6377 NULL, /* psbuf */
6378 NULL, NULL); /* create context */
6379 if (!NT_STATUS_IS_OK(status)) {
6380 DBG_INFO("Opening [%s] failed [%s]\n",
6381 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6382 ok = false;
6383 goto out;
6386 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6387 if (file_data == NULL) {
6388 ok = false;
6389 goto out;
6392 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6393 if (nread != plist_file_size) {
6394 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6395 fsp_str_dbg(fsp), nread, plist_file_size);
6396 ok = false;
6397 goto out;
6401 status = close_file(NULL, fsp, NORMAL_CLOSE);
6402 fsp = NULL;
6403 if (!NT_STATUS_IS_OK(status)) {
6404 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6405 ok = false;
6406 goto out;
6409 lines = file_lines_parse((char *)file_data,
6410 plist_file_size,
6411 &numlines,
6412 talloc_tos());
6413 if (lines == NULL) {
6414 ok = false;
6415 goto out;
6418 band_size_line = fruit_get_bandsize_line(lines, numlines);
6419 if (band_size_line == NULL) {
6420 DBG_ERR("Didn't find band-size key in [%s]\n",
6421 smb_fname_str_dbg(smb_fname));
6422 ok = false;
6423 goto out;
6426 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6427 if (!ok) {
6428 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6429 goto out;
6432 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6434 out:
6435 if (fsp != NULL) {
6436 status = close_file(NULL, fsp, NORMAL_CLOSE);
6437 if (!NT_STATUS_IS_OK(status)) {
6438 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6440 fsp = NULL;
6442 TALLOC_FREE(plist);
6443 TALLOC_FREE(smb_fname);
6444 TALLOC_FREE(file_data);
6445 TALLOC_FREE(lines);
6446 return ok;
6449 struct fruit_disk_free_state {
6450 off_t total_size;
6453 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6454 char *bundle,
6455 size_t *_nbands)
6457 char *path = NULL;
6458 struct smb_filename *bands_dir = NULL;
6459 DIR *d = NULL;
6460 struct dirent *e = NULL;
6461 size_t nbands;
6462 int ret;
6464 path = talloc_asprintf(talloc_tos(),
6465 "%s/%s/bands",
6466 handle->conn->connectpath,
6467 bundle);
6468 if (path == NULL) {
6469 return false;
6472 bands_dir = synthetic_smb_fname(talloc_tos(),
6473 path,
6474 NULL,
6475 NULL,
6477 TALLOC_FREE(path);
6478 if (bands_dir == NULL) {
6479 return false;
6482 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6483 if (d == NULL) {
6484 TALLOC_FREE(bands_dir);
6485 return false;
6488 nbands = 0;
6490 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6491 e != NULL;
6492 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6494 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6495 continue;
6497 nbands++;
6500 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6501 if (ret != 0) {
6502 TALLOC_FREE(bands_dir);
6503 return false;
6506 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6508 TALLOC_FREE(bands_dir);
6510 *_nbands = nbands;
6511 return true;
6514 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6515 struct fruit_disk_free_state *state,
6516 struct dirent *e)
6518 bool ok;
6519 char *p = NULL;
6520 size_t sparsebundle_strlen = strlen("sparsebundle");
6521 size_t bandsize = 0;
6522 size_t nbands;
6523 off_t tm_size;
6525 p = strstr(e->d_name, "sparsebundle");
6526 if (p == NULL) {
6527 return true;
6530 if (p[sparsebundle_strlen] != '\0') {
6531 return true;
6534 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6536 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6537 if (!ok) {
6539 * Beware of race conditions: this may be an uninitialized
6540 * Info.plist that a client is just creating. We don't want let
6541 * this to trigger complete failure.
6543 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6544 return true;
6547 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6548 if (!ok) {
6550 * Beware of race conditions: this may be a backup sparsebundle
6551 * in an early stage lacking a bands subdirectory. We don't want
6552 * let this to trigger complete failure.
6554 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6555 return true;
6558 if (bandsize > SIZE_MAX/nbands) {
6559 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6560 bandsize, nbands);
6561 return false;
6563 tm_size = bandsize * nbands;
6565 if (state->total_size + tm_size < state->total_size) {
6566 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6567 bandsize, nbands);
6568 return false;
6571 state->total_size += tm_size;
6573 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6574 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6576 return true;
6580 * Calculate used size of a TimeMachine volume
6582 * This assumes that the volume is used only for TimeMachine.
6584 * - readdir(basedir of share), then
6585 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6586 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6587 * - count band files in "\1.sparsebundle/bands/"
6588 * - calculate used size of all bands: band_count * band_size
6590 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6591 const struct smb_filename *smb_fname,
6592 uint64_t *_bsize,
6593 uint64_t *_dfree,
6594 uint64_t *_dsize)
6596 struct fruit_config_data *config = NULL;
6597 struct fruit_disk_free_state state = {0};
6598 DIR *d = NULL;
6599 struct dirent *e = NULL;
6600 uint64_t dfree;
6601 uint64_t dsize;
6602 int ret;
6603 bool ok;
6605 SMB_VFS_HANDLE_GET_DATA(handle, config,
6606 struct fruit_config_data,
6607 return UINT64_MAX);
6609 if (!config->time_machine ||
6610 config->time_machine_max_size == 0)
6612 return SMB_VFS_NEXT_DISK_FREE(handle,
6613 smb_fname,
6614 _bsize,
6615 _dfree,
6616 _dsize);
6619 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6620 if (d == NULL) {
6621 return UINT64_MAX;
6624 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6625 e != NULL;
6626 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6628 ok = fruit_tmsize_do_dirent(handle, &state, e);
6629 if (!ok) {
6630 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6631 return UINT64_MAX;
6635 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6636 if (ret != 0) {
6637 return UINT64_MAX;
6640 dsize = config->time_machine_max_size / 512;
6641 dfree = dsize - (state.total_size / 512);
6642 if (dfree > dsize) {
6643 dfree = 0;
6646 *_bsize = 512;
6647 *_dsize = dsize;
6648 *_dfree = dfree;
6649 return dfree / 2;
6652 static struct vfs_fn_pointers vfs_fruit_fns = {
6653 .connect_fn = fruit_connect,
6654 .disk_free_fn = fruit_disk_free,
6656 /* File operations */
6657 .chmod_fn = fruit_chmod,
6658 .chown_fn = fruit_chown,
6659 .unlink_fn = fruit_unlink,
6660 .rename_fn = fruit_rename,
6661 .rmdir_fn = fruit_rmdir,
6662 .open_fn = fruit_open,
6663 .pread_fn = fruit_pread,
6664 .pwrite_fn = fruit_pwrite,
6665 .pread_send_fn = fruit_pread_send,
6666 .pread_recv_fn = fruit_pread_recv,
6667 .pwrite_send_fn = fruit_pwrite_send,
6668 .pwrite_recv_fn = fruit_pwrite_recv,
6669 .stat_fn = fruit_stat,
6670 .lstat_fn = fruit_lstat,
6671 .fstat_fn = fruit_fstat,
6672 .streaminfo_fn = fruit_streaminfo,
6673 .ntimes_fn = fruit_ntimes,
6674 .ftruncate_fn = fruit_ftruncate,
6675 .fallocate_fn = fruit_fallocate,
6676 .create_file_fn = fruit_create_file,
6677 .readdir_attr_fn = fruit_readdir_attr,
6678 .offload_read_send_fn = fruit_offload_read_send,
6679 .offload_read_recv_fn = fruit_offload_read_recv,
6680 .offload_write_send_fn = fruit_offload_write_send,
6681 .offload_write_recv_fn = fruit_offload_write_recv,
6683 /* NT ACL operations */
6684 .fget_nt_acl_fn = fruit_fget_nt_acl,
6685 .fset_nt_acl_fn = fruit_fset_nt_acl,
6688 static_decl_vfs;
6689 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6691 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6692 &vfs_fruit_fns);
6693 if (!NT_STATUS_IS_OK(ret)) {
6694 return ret;
6697 vfs_fruit_debug_level = debug_add_class("fruit");
6698 if (vfs_fruit_debug_level == -1) {
6699 vfs_fruit_debug_level = DBGC_VFS;
6700 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6701 "vfs_fruit_init"));
6702 } else {
6703 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6704 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
6707 return ret;