vfs_fruit: call ad_convert_move_reso() from ad_convert_xattr()
[Samba.git] / source3 / modules / vfs_fruit.c
blobf7cba8a072812fe2ab9f85b67c934286cd2ef9e9
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 AD_FILLER_TAG_OSX "Mac OS X " /* should be 16 bytes */
266 #define ADEDLEN_NENTRIES 2
267 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
268 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
269 #define AD_ENTRY_LEN_EID 4
270 #define AD_ENTRY_LEN_OFF 4
271 #define AD_ENTRY_LEN_LEN 4
272 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
274 /* Field widths */
275 #define ADEDLEN_NAME 255
276 #define ADEDLEN_COMMENT 200
277 #define ADEDLEN_FILEI 16
278 #define ADEDLEN_FINDERI 32
279 #define ADEDLEN_FILEDATESI 16
280 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
281 #define ADEDLEN_AFPFILEI 4
282 #define ADEDLEN_MACFILEI 4
283 #define ADEDLEN_PRODOSFILEI 8
284 #define ADEDLEN_MSDOSFILEI 2
285 #define ADEDLEN_DID 4
286 #define ADEDLEN_PRIVDEV 8
287 #define ADEDLEN_PRIVINO 8
288 #define ADEDLEN_PRIVSYN 8
289 #define ADEDLEN_PRIVID 4
291 /* Offsets */
292 #define ADEDOFF_MAGIC 0
293 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
294 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
295 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
297 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
298 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
299 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
300 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
301 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
302 ADEDLEN_FILEDATESI)
303 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
304 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
305 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
306 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
308 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
309 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
310 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
312 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
313 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
314 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
315 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
316 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
317 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
319 #if AD_DATASZ_XATTR != 402
320 #error bad size for AD_DATASZ_XATTR
321 #endif
323 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
324 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
325 ADEDLEN_FINDERI)
326 #if AD_DATASZ_DOT_UND != 82
327 #error bad size for AD_DATASZ_DOT_UND
328 #endif
331 * Sharemode locks fcntl() offsets
333 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
334 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
335 #else
336 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
337 #endif
338 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
340 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
341 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
342 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
343 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
344 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
345 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
346 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
347 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
348 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
349 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
351 /* Time stuff we overload the bits a little */
352 #define AD_DATE_CREATE 0
353 #define AD_DATE_MODIFY 4
354 #define AD_DATE_BACKUP 8
355 #define AD_DATE_ACCESS 12
356 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
357 AD_DATE_BACKUP | AD_DATE_ACCESS)
358 #define AD_DATE_UNIX (1 << 10)
359 #define AD_DATE_START 0x80000000
360 #define AD_DATE_DELTA 946684800
361 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
362 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
364 #define AD_XATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
365 #define AD_XATTR_MAX_ENTRIES 1024 /* Some arbitrarily enforced limit */
366 #define AD_XATTR_HDR_SIZE 36
367 #define AD_XATTR_MAX_HDR_SIZE 65536
369 /* Accessor macros */
370 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
371 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
372 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
373 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
376 * Both struct ad_xattr_header and struct ad_xattr_entry describe the in memory
377 * representation as well as the on-disk format.
379 * The ad_xattr_header follows the FinderInfo data in the FinderInfo entry if
380 * the length of the FinderInfo entry is larger then 32 bytes. It is then
381 * preceeded with 2 bytes padding.
383 * Cf: https://opensource.apple.com/source/xnu/xnu-4570.1.46/bsd/vfs/vfs_xattr.c
386 struct ad_xattr_header {
387 uint32_t adx_magic; /* ATTR_HDR_MAGIC */
388 uint32_t adx_debug_tag; /* for debugging == file id of owning file */
389 uint32_t adx_total_size; /* file offset of end of attribute header + entries + data */
390 uint32_t adx_data_start; /* file offset to attribute data area */
391 uint32_t adx_data_length; /* length of attribute data area */
392 uint32_t adx_reserved[3];
393 uint16_t adx_flags;
394 uint16_t adx_num_attrs;
397 /* On-disk entries are aligned on 4 byte boundaries */
398 struct ad_xattr_entry {
399 uint32_t adx_offset; /* file offset to data */
400 uint32_t adx_length; /* size of attribute data */
401 uint16_t adx_flags;
402 uint8_t adx_namelen; /* included the NULL terminator */
403 char *adx_name; /* NULL-terminated UTF-8 name */
406 struct ad_entry {
407 size_t ade_off;
408 size_t ade_len;
411 struct adouble {
412 vfs_handle_struct *ad_handle;
413 int ad_fd;
414 bool ad_opened;
415 adouble_type_t ad_type;
416 uint32_t ad_magic;
417 uint32_t ad_version;
418 uint8_t ad_filler[ADEDLEN_FILLER];
419 struct ad_entry ad_eid[ADEID_MAX];
420 char *ad_data;
421 struct ad_xattr_header adx_header;
422 struct ad_xattr_entry *adx_entries;
425 struct ad_entry_order {
426 uint32_t id, offset, len;
429 /* Netatalk AppleDouble metadata xattr */
430 static const
431 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
432 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
433 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
434 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
435 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
436 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
437 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
438 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
439 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
440 {0, 0, 0}
443 /* AppleDouble resource fork file (the ones prefixed by "._") */
444 static const
445 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
446 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
447 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
448 {0, 0, 0}
452 * Fake AppleDouble entry oder for resource fork xattr. The xattr
453 * isn't an AppleDouble file, it simply contains the resource data,
454 * but in order to be able to use some API calls like ad_getentryoff()
455 * we build a fake/helper struct adouble with this entry order struct.
457 static const
458 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
459 {ADEID_RFORK, 0, 0},
460 {0, 0, 0}
463 /* Conversion from enumerated id to on-disk AppleDouble id */
464 #define AD_EID_DISK(a) (set_eid[a])
465 static const uint32_t set_eid[] = {
466 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
467 AD_DEV, AD_INO, AD_SYN, AD_ID
470 struct fio {
471 /* tcon config handle */
472 struct fruit_config_data *config;
474 /* Denote stream type, meta or rsrc */
475 adouble_type_t type;
479 * Forward declarations
481 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
482 adouble_type_t type);
483 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
484 static int ad_fset(struct adouble *ad, files_struct *fsp);
485 static int adouble_path(TALLOC_CTX *ctx,
486 const struct smb_filename *smb_fname__in,
487 struct smb_filename **ppsmb_fname_out);
488 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx);
489 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf);
490 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data);
494 * Return a pointer to an AppleDouble entry
496 * Returns NULL if the entry is not present
498 static char *ad_get_entry(const struct adouble *ad, int eid)
500 off_t off = ad_getentryoff(ad, eid);
501 size_t len = ad_getentrylen(ad, eid);
503 if (off == 0 || len == 0) {
504 return NULL;
507 return ad->ad_data + off;
511 * Get a date
513 static int ad_getdate(const struct adouble *ad,
514 unsigned int dateoff,
515 uint32_t *date)
517 bool xlate = (dateoff & AD_DATE_UNIX);
518 char *p = NULL;
520 dateoff &= AD_DATE_MASK;
521 p = ad_get_entry(ad, ADEID_FILEDATESI);
522 if (p == NULL) {
523 return -1;
526 if (dateoff > AD_DATE_ACCESS) {
527 return -1;
530 memcpy(date, p + dateoff, sizeof(uint32_t));
532 if (xlate) {
533 *date = AD_DATE_TO_UNIX(*date);
535 return 0;
539 * Set a date
541 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
543 bool xlate = (dateoff & AD_DATE_UNIX);
544 char *p = NULL;
546 p = ad_get_entry(ad, ADEID_FILEDATESI);
547 if (p == NULL) {
548 return -1;
551 dateoff &= AD_DATE_MASK;
552 if (xlate) {
553 date = AD_DATE_FROM_UNIX(date);
556 if (dateoff > AD_DATE_ACCESS) {
557 return -1;
560 memcpy(p + dateoff, &date, sizeof(date));
562 return 0;
567 * Map on-disk AppleDouble id to enumerated id
569 static uint32_t get_eid(uint32_t eid)
571 if (eid <= 15) {
572 return eid;
575 switch (eid) {
576 case AD_DEV:
577 return ADEID_PRIVDEV;
578 case AD_INO:
579 return ADEID_PRIVINO;
580 case AD_SYN:
581 return ADEID_PRIVSYN;
582 case AD_ID:
583 return ADEID_PRIVID;
584 default:
585 break;
588 return 0;
592 * Pack AppleDouble structure into data buffer
594 static bool ad_pack(struct adouble *ad)
596 uint32_t eid;
597 uint16_t nent;
598 uint32_t bufsize;
599 uint32_t offset = 0;
601 bufsize = talloc_get_size(ad->ad_data);
602 if (bufsize < AD_DATASZ_DOT_UND) {
603 DBG_ERR("bad buffer size [0x%" PRIx32 "]\n", bufsize);
604 return false;
607 if (offset + ADEDLEN_MAGIC < offset ||
608 offset + ADEDLEN_MAGIC >= bufsize) {
609 return false;
611 RSIVAL(ad->ad_data, offset, ad->ad_magic);
612 offset += ADEDLEN_MAGIC;
614 if (offset + ADEDLEN_VERSION < offset ||
615 offset + ADEDLEN_VERSION >= bufsize) {
616 return false;
618 RSIVAL(ad->ad_data, offset, ad->ad_version);
619 offset += ADEDLEN_VERSION;
621 if (offset + ADEDLEN_FILLER < offset ||
622 offset + ADEDLEN_FILLER >= bufsize) {
623 return false;
625 if (ad->ad_type == ADOUBLE_RSRC) {
626 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
628 offset += ADEDLEN_FILLER;
630 if (offset + ADEDLEN_NENTRIES < offset ||
631 offset + ADEDLEN_NENTRIES >= bufsize) {
632 return false;
634 offset += ADEDLEN_NENTRIES;
636 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
637 if (ad->ad_eid[eid].ade_off == 0) {
639 * ade_off is also used as indicator whether a
640 * specific entry is used or not
642 continue;
645 if (offset + AD_ENTRY_LEN_EID < offset ||
646 offset + AD_ENTRY_LEN_EID >= bufsize) {
647 return false;
649 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
650 offset += AD_ENTRY_LEN_EID;
652 if (offset + AD_ENTRY_LEN_OFF < offset ||
653 offset + AD_ENTRY_LEN_OFF >= bufsize) {
654 return false;
656 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
657 offset += AD_ENTRY_LEN_OFF;
659 if (offset + AD_ENTRY_LEN_LEN < offset ||
660 offset + AD_ENTRY_LEN_LEN >= bufsize) {
661 return false;
663 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
664 offset += AD_ENTRY_LEN_LEN;
666 nent++;
669 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
670 return false;
672 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
674 return true;
677 static bool ad_unpack_xattrs(struct adouble *ad)
679 struct ad_xattr_header *h = &ad->adx_header;
680 const char *p = ad->ad_data;
681 uint32_t hoff;
682 uint32_t i;
684 if (ad_getentrylen(ad, ADEID_FINDERI) <= ADEDLEN_FINDERI) {
685 return true;
688 /* 2 bytes padding */
689 hoff = ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI + 2;
691 h->adx_magic = RIVAL(p, hoff + 0);
692 h->adx_debug_tag = RIVAL(p, hoff + 4); /* Not used -> not checked */
693 h->adx_total_size = RIVAL(p, hoff + 8);
694 h->adx_data_start = RIVAL(p, hoff + 12);
695 h->adx_data_length = RIVAL(p, hoff + 16);
696 h->adx_flags = RSVAL(p, hoff + 32); /* Not used -> not checked */
697 h->adx_num_attrs = RSVAL(p, hoff + 34);
699 if (h->adx_magic != AD_XATTR_HDR_MAGIC) {
700 DBG_ERR("Bad magic: 0x%" PRIx32 "\n", h->adx_magic);
701 return false;
704 if (h->adx_total_size > ad_getentryoff(ad, ADEID_RFORK)) {
705 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
706 return false;
708 if (h->adx_total_size > AD_XATTR_MAX_HDR_SIZE) {
709 DBG_ERR("Bad total size: 0x%" PRIx32 "\n", h->adx_total_size);
710 return false;
713 if (h->adx_data_start < (hoff + AD_XATTR_HDR_SIZE)) {
714 DBG_ERR("Bad start: 0x%" PRIx32 "\n", h->adx_data_start);
715 return false;
718 if ((h->adx_data_start + h->adx_data_length) < h->adx_data_start) {
719 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
720 return false;
722 if ((h->adx_data_start + h->adx_data_length) >
723 ad->adx_header.adx_total_size)
725 DBG_ERR("Bad length: %" PRIu32 "\n", h->adx_data_length);
726 return false;
729 if (h->adx_num_attrs > AD_XATTR_MAX_ENTRIES) {
730 DBG_ERR("Bad num xattrs: %" PRIu16 "\n", h->adx_num_attrs);
731 return false;
734 if (h->adx_num_attrs == 0) {
735 return true;
738 ad->adx_entries = talloc_zero_array(
739 ad, struct ad_xattr_entry, h->adx_num_attrs);
740 if (ad->adx_entries == NULL) {
741 return false;
744 hoff += AD_XATTR_HDR_SIZE;
746 for (i = 0; i < h->adx_num_attrs; i++) {
747 struct ad_xattr_entry *e = &ad->adx_entries[i];
749 hoff = (hoff + 3) & ~3;
751 e->adx_offset = RIVAL(p, hoff + 0);
752 e->adx_length = RIVAL(p, hoff + 4);
753 e->adx_flags = RSVAL(p, hoff + 8);
754 e->adx_namelen = *(p + hoff + 10);
756 if (e->adx_offset >= ad->adx_header.adx_total_size) {
757 DBG_ERR("Bad adx_offset: %" PRIx32 "\n",
758 e->adx_offset);
759 return false;
762 if ((e->adx_offset + e->adx_length) < e->adx_offset) {
763 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
764 e->adx_length);
765 return false;
768 if ((e->adx_offset + e->adx_length) >
769 ad->adx_header.adx_total_size)
771 DBG_ERR("Bad adx_length: %" PRIx32 "\n",
772 e->adx_length);
773 return false;
776 if (e->adx_namelen == 0) {
777 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
778 e->adx_namelen);
779 return false;
781 if ((hoff + 11 + e->adx_namelen) < hoff + 11) {
782 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
783 e->adx_namelen);
784 return false;
786 if ((hoff + 11 + e->adx_namelen) >
787 ad->adx_header.adx_data_start)
789 DBG_ERR("Bad adx_namelen: %" PRIx32 "\n",
790 e->adx_namelen);
791 return false;
794 e->adx_name = talloc_strndup(ad->adx_entries,
795 p + hoff + 11,
796 e->adx_namelen);
797 if (e->adx_name == NULL) {
798 return false;
801 DBG_DEBUG("xattr [%s] offset [0x%x] size [0x%x]\n",
802 e->adx_name, e->adx_offset, e->adx_length);
803 dump_data(10, (uint8_t *)(ad->ad_data + e->adx_offset),
804 e->adx_length);
806 hoff += 11 + e->adx_namelen;
809 return true;
813 * Unpack an AppleDouble blob into a struct adoble
815 static bool ad_unpack(struct adouble *ad, const size_t nentries,
816 size_t filesize)
818 size_t bufsize = talloc_get_size(ad->ad_data);
819 size_t adentries, i;
820 uint32_t eid, len, off;
821 bool ok;
824 * The size of the buffer ad->ad_data is checked when read, so
825 * we wouldn't have to check our own offsets, a few extra
826 * checks won't hurt though. We have to check the offsets we
827 * read from the buffer anyway.
830 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
831 DEBUG(1, ("bad size\n"));
832 return false;
835 ad->ad_magic = RIVAL(ad->ad_data, 0);
836 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
837 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
838 DEBUG(1, ("wrong magic or version\n"));
839 return false;
842 memcpy(ad->ad_filler, ad->ad_data + ADEDOFF_FILLER, ADEDLEN_FILLER);
844 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
845 if (adentries != nentries) {
846 DEBUG(1, ("invalid number of entries: %zu\n",
847 adentries));
848 return false;
851 /* now, read in the entry bits */
852 for (i = 0; i < adentries; i++) {
853 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
854 eid = get_eid(eid);
855 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
856 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
858 if (!eid || eid >= ADEID_MAX) {
859 DEBUG(1, ("bogus eid %d\n", eid));
860 return false;
864 * All entries other than the resource fork are
865 * expected to be read into the ad_data buffer, so
866 * ensure the specified offset is within that bound
868 if ((off > bufsize) && (eid != ADEID_RFORK)) {
869 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
870 eid, off, len));
871 return false;
875 * All entries besides FinderInfo and resource fork
876 * must fit into the buffer. FinderInfo is special as
877 * it may be larger then the default 32 bytes (if it
878 * contains marshalled xattrs), but we will fixup that
879 * in ad_convert(). And the resource fork is never
880 * accessed directly by the ad_data buf (also see
881 * comment above) anyway.
883 if ((eid != ADEID_RFORK) &&
884 (eid != ADEID_FINDERI) &&
885 ((off + len) > bufsize)) {
886 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
887 eid, off, len));
888 return false;
892 * That would be obviously broken
894 if (off > filesize) {
895 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
896 eid, off, len));
897 return false;
901 * Check for any entry that has its end beyond the
902 * filesize.
904 if (off + len < off) {
905 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
906 ", len: %" PRIu32 "\n",
907 eid, off, len));
908 return false;
911 if (off + len > filesize) {
913 * If this is the resource fork entry, we fix
914 * up the length, for any other entry we bail
915 * out.
917 if (eid != ADEID_RFORK) {
918 DEBUG(1, ("bogus eid %d: off: %" PRIu32
919 ", len: %" PRIu32 "\n",
920 eid, off, len));
921 return false;
925 * Fixup the resource fork entry by limiting
926 * the size to entryoffset - filesize.
928 len = filesize - off;
929 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
930 ", len: %" PRIu32 "\n", off, len));
933 ad->ad_eid[eid].ade_off = off;
934 ad->ad_eid[eid].ade_len = len;
937 ok = ad_unpack_xattrs(ad);
938 if (!ok) {
939 return false;
942 return true;
945 static bool ad_convert_move_reso(struct adouble *ad,
946 const struct smb_filename *smb_fname)
948 char *map = MAP_FAILED;
949 size_t maplen;
950 ssize_t len;
951 int rc;
952 bool ok;
954 if (ad_getentrylen(ad, ADEID_RFORK) == 0) {
955 return true;
958 maplen = ad_getentryoff(ad, ADEID_RFORK) +
959 ad_getentrylen(ad, ADEID_RFORK);
961 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
962 map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
963 ad->ad_fd, 0);
964 if (map == MAP_FAILED) {
965 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
966 return false;
970 memmove(map + ADEDOFF_RFORK_DOT_UND,
971 map + ad_getentryoff(ad, ADEID_RFORK),
972 ad_getentrylen(ad, ADEID_RFORK));
974 rc = munmap(map, maplen);
975 if (rc != 0) {
976 DBG_ERR("munmap failed: %s\n", strerror(errno));
977 return false;
980 ad_setentryoff(ad, ADEID_RFORK, ADEDOFF_RFORK_DOT_UND);
982 ok = ad_pack(ad);
983 if (!ok) {
984 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
985 return false;
988 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
989 if (len != AD_DATASZ_DOT_UND) {
990 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
991 return false;
994 return true;
997 static bool ad_convert_xattr(struct adouble *ad,
998 const struct smb_filename *smb_fname)
1000 static struct char_mappings **string_replace_cmaps = NULL;
1001 char *map = MAP_FAILED;
1002 size_t maplen;
1003 uint16_t i;
1004 ssize_t len;
1005 int saved_errno = 0;
1006 NTSTATUS status;
1007 int rc;
1008 bool ok;
1010 if (ad->adx_header.adx_num_attrs == 0) {
1011 return true;
1014 if (string_replace_cmaps == NULL) {
1015 const char **mappings = NULL;
1017 mappings = str_list_make_v3_const(
1018 talloc_tos(), fruit_catia_maps, NULL);
1019 if (mappings == NULL) {
1020 return false;
1022 string_replace_cmaps = string_replace_init_map(mappings);
1023 TALLOC_FREE(mappings);
1026 maplen = ad_getentryoff(ad, ADEID_RFORK) +
1027 ad_getentrylen(ad, ADEID_RFORK);
1029 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
1030 map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
1031 ad->ad_fd, 0);
1032 if (map == MAP_FAILED) {
1033 DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
1034 return false;
1037 for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
1038 struct ad_xattr_entry *e = &ad->adx_entries[i];
1039 char *mapped_name = NULL;
1040 char *tmp = NULL;
1041 struct smb_filename *stream_name = NULL;
1042 files_struct *fsp = NULL;
1043 ssize_t nwritten;
1045 status = string_replace_allocate(ad->ad_handle->conn,
1046 e->adx_name,
1047 string_replace_cmaps,
1048 talloc_tos(),
1049 &mapped_name,
1050 vfs_translate_to_windows);
1051 if (!NT_STATUS_IS_OK(status) &&
1052 !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
1054 DBG_ERR("string_replace_allocate failed\n");
1055 ok = false;
1056 goto fail;
1059 tmp = mapped_name;
1060 mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
1061 TALLOC_FREE(tmp);
1062 if (mapped_name == NULL) {
1063 ok = false;
1064 goto fail;
1067 stream_name = synthetic_smb_fname(talloc_tos(),
1068 smb_fname->base_name,
1069 mapped_name,
1070 NULL,
1071 smb_fname->flags);
1072 TALLOC_FREE(mapped_name);
1073 if (stream_name == NULL) {
1074 DBG_ERR("synthetic_smb_fname failed\n");
1075 ok = false;
1076 goto fail;
1079 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1081 status = SMB_VFS_CREATE_FILE(
1082 ad->ad_handle->conn, /* conn */
1083 NULL, /* req */
1084 0, /* root_dir_fid */
1085 stream_name, /* fname */
1086 FILE_GENERIC_WRITE, /* access_mask */
1087 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1088 FILE_OPEN_IF, /* create_disposition */
1089 0, /* create_options */
1090 0, /* file_attributes */
1091 INTERNAL_OPEN_ONLY, /* oplock_request */
1092 NULL, /* lease */
1093 0, /* allocation_size */
1094 0, /* private_flags */
1095 NULL, /* sd */
1096 NULL, /* ea_list */
1097 &fsp, /* result */
1098 NULL, /* psbuf */
1099 NULL, NULL); /* create context */
1100 TALLOC_FREE(stream_name);
1101 if (!NT_STATUS_IS_OK(status)) {
1102 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1103 ok = false;
1104 goto fail;
1107 nwritten = SMB_VFS_PWRITE(fsp,
1108 map + e->adx_offset,
1109 e->adx_length,
1111 if (nwritten == -1) {
1112 DBG_ERR("SMB_VFS_PWRITE failed\n");
1113 saved_errno = errno;
1114 close_file(NULL, fsp, ERROR_CLOSE);
1115 errno = saved_errno;
1116 ok = false;
1117 goto fail;
1120 status = close_file(NULL, fsp, NORMAL_CLOSE);
1121 if (!NT_STATUS_IS_OK(status)) {
1122 ok = false;
1123 goto fail;
1125 fsp = NULL;
1128 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
1130 ok = ad_pack(ad);
1131 if (!ok) {
1132 DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
1133 goto fail;
1136 len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
1137 if (len != AD_DATASZ_DOT_UND) {
1138 DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
1139 ok = false;
1140 goto fail;
1143 ok = ad_convert_move_reso(ad, smb_fname);
1144 if (!ok) {
1145 goto fail;
1148 ok = true;
1150 fail:
1151 rc = munmap(map, maplen);
1152 if (rc != 0) {
1153 DBG_ERR("munmap failed: %s\n", strerror(errno));
1154 return false;
1157 return ok;
1160 static bool ad_convert_finderinfo(struct adouble *ad,
1161 const struct smb_filename *smb_fname)
1163 char *p_ad = NULL;
1164 AfpInfo *ai = NULL;
1165 DATA_BLOB aiblob;
1166 struct smb_filename *stream_name = NULL;
1167 files_struct *fsp = NULL;
1168 size_t size;
1169 ssize_t nwritten;
1170 NTSTATUS status;
1171 int saved_errno = 0;
1173 p_ad = ad_get_entry(ad, ADEID_FINDERI);
1174 if (p_ad == NULL) {
1175 return false;
1178 ai = afpinfo_new(talloc_tos());
1179 if (ai == NULL) {
1180 return false;
1183 memcpy(ai->afpi_FinderInfo, p_ad, ADEDLEN_FINDERI);
1185 aiblob = data_blob_talloc(talloc_tos(), NULL, AFP_INFO_SIZE);
1186 if (aiblob.data == NULL) {
1187 TALLOC_FREE(ai);
1188 return false;
1191 size = afpinfo_pack(ai, (char *)aiblob.data);
1192 TALLOC_FREE(ai);
1193 if (size != AFP_INFO_SIZE) {
1194 return false;
1197 stream_name = synthetic_smb_fname(talloc_tos(),
1198 smb_fname->base_name,
1199 AFPINFO_STREAM,
1200 NULL,
1201 smb_fname->flags);
1202 if (stream_name == NULL) {
1203 data_blob_free(&aiblob);
1204 DBG_ERR("synthetic_smb_fname failed\n");
1205 return false;
1208 DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
1210 status = SMB_VFS_CREATE_FILE(
1211 ad->ad_handle->conn, /* conn */
1212 NULL, /* req */
1213 0, /* root_dir_fid */
1214 stream_name, /* fname */
1215 FILE_GENERIC_WRITE, /* access_mask */
1216 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1217 FILE_OPEN_IF, /* create_disposition */
1218 0, /* create_options */
1219 0, /* file_attributes */
1220 INTERNAL_OPEN_ONLY, /* oplock_request */
1221 NULL, /* lease */
1222 0, /* allocation_size */
1223 0, /* private_flags */
1224 NULL, /* sd */
1225 NULL, /* ea_list */
1226 &fsp, /* result */
1227 NULL, /* psbuf */
1228 NULL, NULL); /* create context */
1229 TALLOC_FREE(stream_name);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
1232 return false;
1235 nwritten = SMB_VFS_PWRITE(fsp,
1236 aiblob.data,
1237 aiblob.length,
1239 if (nwritten == -1) {
1240 DBG_ERR("SMB_VFS_PWRITE failed\n");
1241 saved_errno = errno;
1242 close_file(NULL, fsp, ERROR_CLOSE);
1243 errno = saved_errno;
1244 return false;
1247 status = close_file(NULL, fsp, NORMAL_CLOSE);
1248 if (!NT_STATUS_IS_OK(status)) {
1249 return false;
1251 fsp = NULL;
1253 return true;
1256 static bool ad_convert_truncate(struct adouble *ad,
1257 const struct smb_filename *smb_fname)
1259 int rc;
1262 * FIXME: direct ftruncate(), but we don't have a fsp for the
1263 * VFS call
1265 rc = ftruncate(ad->ad_fd, ADEDOFF_RFORK_DOT_UND +
1266 ad_getentrylen(ad, ADEID_RFORK));
1267 if (rc != 0) {
1268 return false;
1271 return true;
1275 * Convert from Apple's ._ file to Netatalk
1277 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
1278 * bytes containing packed xattrs.
1280 * @return -1 in case an error occurred, 0 if no conversion was done, 1
1281 * otherwise
1283 static int ad_convert(struct adouble *ad,
1284 const struct smb_filename *smb_fname)
1286 bool ok;
1288 if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
1289 return 0;
1292 ok = ad_convert_xattr(ad, smb_fname);
1293 if (!ok) {
1294 return -1;
1297 ok = ad_convert_truncate(ad, smb_fname);
1298 if (!ok) {
1299 return -1;
1302 ok = ad_convert_finderinfo(ad, smb_fname);
1303 if (!ok) {
1304 DBG_ERR("Failed to convert [%s]\n",
1305 smb_fname_str_dbg(smb_fname));
1306 return -1;
1309 return 0;
1313 * Read and parse Netatalk AppleDouble metadata xattr
1315 static ssize_t ad_read_meta(struct adouble *ad,
1316 const struct smb_filename *smb_fname)
1318 int rc = 0;
1319 ssize_t ealen;
1320 bool ok;
1322 DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
1324 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
1325 AFPINFO_EA_NETATALK, ad->ad_data,
1326 AD_DATASZ_XATTR);
1327 if (ealen == -1) {
1328 switch (errno) {
1329 case ENOATTR:
1330 case ENOENT:
1331 if (errno == ENOATTR) {
1332 errno = ENOENT;
1334 rc = -1;
1335 goto exit;
1336 default:
1337 DEBUG(2, ("error reading meta xattr: %s\n",
1338 strerror(errno)));
1339 rc = -1;
1340 goto exit;
1343 if (ealen != AD_DATASZ_XATTR) {
1344 DEBUG(2, ("bad size %zd\n", ealen));
1345 errno = EINVAL;
1346 rc = -1;
1347 goto exit;
1350 /* Now parse entries */
1351 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
1352 if (!ok) {
1353 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1354 errno = EINVAL;
1355 rc = -1;
1356 goto exit;
1359 if (!ad_getentryoff(ad, ADEID_FINDERI)
1360 || !ad_getentryoff(ad, ADEID_COMMENT)
1361 || !ad_getentryoff(ad, ADEID_FILEDATESI)
1362 || !ad_getentryoff(ad, ADEID_AFPFILEI)
1363 || !ad_getentryoff(ad, ADEID_PRIVDEV)
1364 || !ad_getentryoff(ad, ADEID_PRIVINO)
1365 || !ad_getentryoff(ad, ADEID_PRIVSYN)
1366 || !ad_getentryoff(ad, ADEID_PRIVID)) {
1367 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
1368 errno = EINVAL;
1369 rc = -1;
1370 goto exit;
1373 exit:
1374 DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
1375 smb_fname->base_name, rc));
1377 if (rc != 0) {
1378 ealen = -1;
1379 if (errno == EINVAL) {
1380 become_root();
1381 removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
1382 unbecome_root();
1383 errno = ENOENT;
1386 return ealen;
1389 static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
1390 int flags,
1391 mode_t mode)
1393 #ifdef HAVE_ATTROPEN
1394 /* FIXME: direct Solaris xattr syscall */
1395 return attropen(smb_fname->base_name,
1396 AFPRESOURCE_EA_NETATALK, flags, mode);
1397 #else
1398 errno = ENOSYS;
1399 return -1;
1400 #endif
1403 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
1404 int flags,
1405 mode_t mode)
1407 int ret;
1408 int fd;
1409 struct smb_filename *adp_smb_fname = NULL;
1411 ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
1412 if (ret != 0) {
1413 return -1;
1416 fd = open(adp_smb_fname->base_name, flags, mode);
1417 TALLOC_FREE(adp_smb_fname);
1419 return fd;
1422 static int ad_open_rsrc(vfs_handle_struct *handle,
1423 const struct smb_filename *smb_fname,
1424 int flags,
1425 mode_t mode)
1427 struct fruit_config_data *config = NULL;
1428 int fd;
1430 SMB_VFS_HANDLE_GET_DATA(handle, config,
1431 struct fruit_config_data, return -1);
1433 if (config->rsrc == FRUIT_RSRC_XATTR) {
1434 fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
1435 } else {
1436 fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
1439 return fd;
1443 * Here's the deal: for ADOUBLE_META we can do without an fd as we can issue
1444 * path based xattr calls. For ADOUBLE_RSRC however we need a full-fledged fd
1445 * for file IO on the ._ file.
1447 static int ad_open(vfs_handle_struct *handle,
1448 struct adouble *ad,
1449 files_struct *fsp,
1450 const struct smb_filename *smb_fname,
1451 int flags,
1452 mode_t mode)
1454 int fd;
1456 DBG_DEBUG("Path [%s] type [%s]\n", smb_fname->base_name,
1457 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
1459 if (ad->ad_type == ADOUBLE_META) {
1460 return 0;
1463 if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
1464 ad->ad_fd = fsp->fh->fd;
1465 ad->ad_opened = false;
1466 return 0;
1469 fd = ad_open_rsrc(handle, smb_fname, flags, mode);
1470 if (fd == -1) {
1471 return -1;
1473 ad->ad_opened = true;
1474 ad->ad_fd = fd;
1476 DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
1477 smb_fname->base_name,
1478 ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
1480 return 0;
1483 static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
1485 int ret;
1486 SMB_STRUCT_STAT st;
1488 /* FIXME: direct sys_fstat(), don't have an fsp */
1489 ret = sys_fstat(ad->ad_fd, &st,
1490 lp_fake_directory_create_times(
1491 SNUM(ad->ad_handle->conn)));
1492 if (ret != 0) {
1493 return -1;
1496 ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
1497 return st.st_ex_size;
1500 static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
1501 const struct smb_filename *smb_fname)
1503 SMB_STRUCT_STAT sbuf;
1504 char *p_ad = NULL;
1505 size_t size;
1506 ssize_t len;
1507 int ret;
1508 bool ok;
1510 ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
1511 SNUM(ad->ad_handle->conn)));
1512 if (ret != 0) {
1513 return -1;
1517 * AppleDouble file header content and size, two cases:
1519 * - without xattrs it is exactly AD_DATASZ_DOT_UND (82) bytes large
1520 * - with embedded xattrs it can be larger, up to AD_XATTR_MAX_HDR_SIZE
1522 * Read as much as we can up to AD_XATTR_MAX_HDR_SIZE.
1524 size = sbuf.st_ex_size;
1525 if (size > talloc_array_length(ad->ad_data)) {
1526 if (size > AD_XATTR_MAX_HDR_SIZE) {
1527 size = AD_XATTR_MAX_HDR_SIZE;
1529 p_ad = talloc_realloc(ad, ad->ad_data, char, size);
1530 if (p_ad == NULL) {
1531 return -1;
1533 ad->ad_data = p_ad;
1536 len = sys_pread(ad->ad_fd, ad->ad_data,
1537 talloc_array_length(ad->ad_data), 0);
1538 if (len != talloc_array_length(ad->ad_data)) {
1539 DBG_NOTICE("%s %s: bad size: %zd\n",
1540 smb_fname->base_name, strerror(errno), len);
1541 return -1;
1544 /* Now parse entries */
1545 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
1546 if (!ok) {
1547 DBG_ERR("invalid AppleDouble resource %s\n",
1548 smb_fname->base_name);
1549 errno = EINVAL;
1550 return -1;
1553 if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
1554 || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
1555 || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
1556 DBG_ERR("invalid AppleDouble resource %s\n",
1557 smb_fname->base_name);
1558 errno = EINVAL;
1559 return -1;
1563 * Try to fixup AppleDouble files created by OS X with xattrs
1564 * appended to the ADEID_FINDERI entry.
1567 ret = ad_convert(ad, smb_fname);
1568 if (ret != 0) {
1569 DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
1570 return len;
1573 return len;
1577 * Read and parse resource fork, either ._ AppleDouble file or xattr
1579 static ssize_t ad_read_rsrc(struct adouble *ad,
1580 const struct smb_filename *smb_fname)
1582 struct fruit_config_data *config = NULL;
1583 ssize_t len;
1585 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
1586 struct fruit_config_data, return -1);
1588 if (config->rsrc == FRUIT_RSRC_XATTR) {
1589 len = ad_read_rsrc_xattr(ad);
1590 } else {
1591 len = ad_read_rsrc_adouble(ad, smb_fname);
1594 return len;
1598 * Read and unpack an AppleDouble metadata xattr or resource
1600 static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
1602 switch (ad->ad_type) {
1603 case ADOUBLE_META:
1604 return ad_read_meta(ad, smb_fname);
1605 case ADOUBLE_RSRC:
1606 return ad_read_rsrc(ad, smb_fname);
1607 default:
1608 return -1;
1612 static int adouble_destructor(struct adouble *ad)
1614 if ((ad->ad_fd != -1) && ad->ad_opened) {
1615 close(ad->ad_fd);
1616 ad->ad_fd = -1;
1618 return 0;
1622 * Allocate a struct adouble without initialiing it
1624 * The struct is either hang of the fsp extension context or if fsp is
1625 * NULL from ctx.
1627 * @param[in] ctx talloc context
1628 * @param[in] handle vfs handle
1629 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1631 * @return adouble handle
1633 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1634 adouble_type_t type)
1636 int rc = 0;
1637 size_t adsize = 0;
1638 struct adouble *ad;
1639 struct fruit_config_data *config;
1641 SMB_VFS_HANDLE_GET_DATA(handle, config,
1642 struct fruit_config_data, return NULL);
1644 switch (type) {
1645 case ADOUBLE_META:
1646 adsize = AD_DATASZ_XATTR;
1647 break;
1648 case ADOUBLE_RSRC:
1649 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1650 adsize = AD_DATASZ_DOT_UND;
1652 break;
1653 default:
1654 return NULL;
1657 ad = talloc_zero(ctx, struct adouble);
1658 if (ad == NULL) {
1659 rc = -1;
1660 goto exit;
1663 if (adsize) {
1664 ad->ad_data = talloc_zero_array(ad, char, adsize);
1665 if (ad->ad_data == NULL) {
1666 rc = -1;
1667 goto exit;
1671 ad->ad_handle = handle;
1672 ad->ad_type = type;
1673 ad->ad_magic = AD_MAGIC;
1674 ad->ad_version = AD_VERSION;
1675 ad->ad_fd = -1;
1677 talloc_set_destructor(ad, adouble_destructor);
1679 exit:
1680 if (rc != 0) {
1681 TALLOC_FREE(ad);
1683 return ad;
1687 * Allocate and initialize a new struct adouble
1689 * @param[in] ctx talloc context
1690 * @param[in] handle vfs handle
1691 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1693 * @return adouble handle, initialized
1695 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1696 adouble_type_t type)
1698 int rc = 0;
1699 const struct ad_entry_order *eid;
1700 struct adouble *ad = NULL;
1701 struct fruit_config_data *config;
1702 time_t t = time(NULL);
1704 SMB_VFS_HANDLE_GET_DATA(handle, config,
1705 struct fruit_config_data, return NULL);
1707 switch (type) {
1708 case ADOUBLE_META:
1709 eid = entry_order_meta_xattr;
1710 break;
1711 case ADOUBLE_RSRC:
1712 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1713 eid = entry_order_dot_und;
1714 } else {
1715 eid = entry_order_rsrc_xattr;
1717 break;
1718 default:
1719 return NULL;
1722 ad = ad_alloc(ctx, handle, type);
1723 if (ad == NULL) {
1724 return NULL;
1727 while (eid->id) {
1728 ad->ad_eid[eid->id].ade_off = eid->offset;
1729 ad->ad_eid[eid->id].ade_len = eid->len;
1730 eid++;
1733 /* put something sane in the date fields */
1734 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1735 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1736 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1737 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1739 if (rc != 0) {
1740 TALLOC_FREE(ad);
1742 return ad;
1745 static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
1746 vfs_handle_struct *handle,
1747 files_struct *fsp,
1748 const struct smb_filename *smb_fname,
1749 adouble_type_t type)
1751 int rc = 0;
1752 ssize_t len;
1753 struct adouble *ad = NULL;
1754 int mode;
1756 if (fsp != NULL) {
1757 smb_fname = fsp->base_fsp->fsp_name;
1760 DEBUG(10, ("ad_get(%s) called for %s\n",
1761 type == ADOUBLE_META ? "meta" : "rsrc",
1762 smb_fname->base_name));
1764 ad = ad_alloc(ctx, handle, type);
1765 if (ad == NULL) {
1766 rc = -1;
1767 goto exit;
1770 /* Try rw first so we can use the fd in ad_convert() */
1771 mode = O_RDWR;
1773 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1774 if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
1775 mode = O_RDONLY;
1776 rc = ad_open(handle, ad, fsp, smb_fname, mode, 0);
1778 if (rc == -1) {
1779 DBG_DEBUG("ad_open [%s] error [%s]\n",
1780 smb_fname->base_name, strerror(errno));
1781 goto exit;
1785 len = ad_read(ad, smb_fname);
1786 if (len == -1) {
1787 DEBUG(10, ("error reading AppleDouble for %s\n",
1788 smb_fname->base_name));
1789 rc = -1;
1790 goto exit;
1793 exit:
1794 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1795 type == ADOUBLE_META ? "meta" : "rsrc",
1796 smb_fname->base_name, rc));
1798 if (rc != 0) {
1799 TALLOC_FREE(ad);
1801 return ad;
1805 * Return AppleDouble data for a file
1807 * @param[in] ctx talloc context
1808 * @param[in] handle vfs handle
1809 * @param[in] smb_fname pathname to file or directory
1810 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1812 * @return talloced struct adouble or NULL on error
1814 static struct adouble *ad_get(TALLOC_CTX *ctx,
1815 vfs_handle_struct *handle,
1816 const struct smb_filename *smb_fname,
1817 adouble_type_t type)
1819 return ad_get_internal(ctx, handle, NULL, smb_fname, type);
1823 * Return AppleDouble data for a file
1825 * @param[in] ctx talloc context
1826 * @param[in] handle vfs handle
1827 * @param[in] fsp fsp to use for IO
1828 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1830 * @return talloced struct adouble or NULL on error
1832 static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1833 files_struct *fsp, adouble_type_t type)
1835 return ad_get_internal(ctx, handle, fsp, NULL, type);
1839 * Set AppleDouble metadata on a file or directory
1841 * @param[in] ad adouble handle
1843 * @param[in] smb_fname pathname to file or directory
1845 * @return status code, 0 means success
1847 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
1849 bool ok;
1850 int ret;
1852 DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
1854 if (ad->ad_type != ADOUBLE_META) {
1855 DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
1856 smb_fname->base_name);
1857 return -1;
1860 ok = ad_pack(ad);
1861 if (!ok) {
1862 return -1;
1865 ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
1866 smb_fname,
1867 AFPINFO_EA_NETATALK,
1868 ad->ad_data,
1869 AD_DATASZ_XATTR, 0);
1871 DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
1873 return ret;
1877 * Set AppleDouble metadata on a file or directory
1879 * @param[in] ad adouble handle
1880 * @param[in] fsp file handle
1882 * @return status code, 0 means success
1884 static int ad_fset(struct adouble *ad, files_struct *fsp)
1886 int rc = -1;
1887 ssize_t len;
1888 bool ok;
1890 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
1892 if ((fsp == NULL)
1893 || (fsp->fh == NULL)
1894 || (fsp->fh->fd == -1))
1896 smb_panic("bad fsp");
1899 ok = ad_pack(ad);
1900 if (!ok) {
1901 return -1;
1904 switch (ad->ad_type) {
1905 case ADOUBLE_META:
1906 rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
1907 fsp->fsp_name,
1908 AFPINFO_EA_NETATALK,
1909 ad->ad_data,
1910 AD_DATASZ_XATTR, 0);
1911 break;
1913 case ADOUBLE_RSRC:
1914 len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
1915 fsp,
1916 ad->ad_data,
1917 AD_DATASZ_DOT_UND,
1919 if (len != AD_DATASZ_DOT_UND) {
1920 DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
1921 return -1;
1923 rc = 0;
1924 break;
1926 default:
1927 return -1;
1930 DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
1932 return rc;
1935 /*****************************************************************************
1936 * Helper functions
1937 *****************************************************************************/
1939 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1941 if (strncasecmp_m(smb_fname->stream_name,
1942 AFPINFO_STREAM_NAME,
1943 strlen(AFPINFO_STREAM_NAME)) == 0) {
1944 return true;
1946 return false;
1949 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1951 if (strncasecmp_m(smb_fname->stream_name,
1952 AFPRESOURCE_STREAM_NAME,
1953 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1954 return true;
1956 return false;
1960 * Test whether stream is an Apple stream, not used atm
1962 #if 0
1963 static bool is_apple_stream(const struct smb_filename *smb_fname)
1965 if (is_afpinfo_stream(smb_fname)) {
1966 return true;
1968 if (is_afpresource_stream(smb_fname)) {
1969 return true;
1971 return false;
1973 #endif
1976 * Initialize config struct from our smb.conf config parameters
1978 static int init_fruit_config(vfs_handle_struct *handle)
1980 struct fruit_config_data *config;
1981 int enumval;
1982 const char *tm_size_str = NULL;
1984 config = talloc_zero(handle->conn, struct fruit_config_data);
1985 if (!config) {
1986 DEBUG(1, ("talloc_zero() failed\n"));
1987 errno = ENOMEM;
1988 return -1;
1992 * Versions up to Samba 4.5.x had a spelling bug in the
1993 * fruit:resource option calling lp_parm_enum with
1994 * "res*s*ource" (ie two s).
1996 * In Samba 4.6 we accept both the wrong and the correct
1997 * spelling, in Samba 4.7 the bad spelling will be removed.
1999 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2000 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
2001 if (enumval == -1) {
2002 DEBUG(1, ("value for %s: resource type unknown\n",
2003 FRUIT_PARAM_TYPE_NAME));
2004 return -1;
2006 config->rsrc = (enum fruit_rsrc)enumval;
2008 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2009 "resource", fruit_rsrc, enumval);
2010 if (enumval == -1) {
2011 DEBUG(1, ("value for %s: resource type unknown\n",
2012 FRUIT_PARAM_TYPE_NAME));
2013 return -1;
2015 config->rsrc = (enum fruit_rsrc)enumval;
2017 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2018 "metadata", fruit_meta, FRUIT_META_NETATALK);
2019 if (enumval == -1) {
2020 DEBUG(1, ("value for %s: metadata type unknown\n",
2021 FRUIT_PARAM_TYPE_NAME));
2022 return -1;
2024 config->meta = (enum fruit_meta)enumval;
2026 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2027 "locking", fruit_locking, FRUIT_LOCKING_NONE);
2028 if (enumval == -1) {
2029 DEBUG(1, ("value for %s: locking type unknown\n",
2030 FRUIT_PARAM_TYPE_NAME));
2031 return -1;
2033 config->locking = (enum fruit_locking)enumval;
2035 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2036 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
2037 if (enumval == -1) {
2038 DEBUG(1, ("value for %s: encoding type unknown\n",
2039 FRUIT_PARAM_TYPE_NAME));
2040 return -1;
2042 config->encoding = (enum fruit_encoding)enumval;
2044 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2045 config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
2046 FRUIT_PARAM_TYPE_NAME,
2047 "veto_appledouble",
2048 true);
2051 config->use_aapl = lp_parm_bool(
2052 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
2054 config->time_machine = lp_parm_bool(
2055 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
2057 config->unix_info_enabled = lp_parm_bool(
2058 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
2060 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
2061 "copyfile", false);
2063 config->posix_rename = lp_parm_bool(
2064 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
2066 config->aapl_zero_file_id =
2067 lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
2069 config->readdir_attr_rsize = lp_parm_bool(
2070 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
2072 config->readdir_attr_finder_info = lp_parm_bool(
2073 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
2075 config->readdir_attr_max_access = lp_parm_bool(
2076 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
2078 config->model = lp_parm_const_string(
2079 -1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
2081 tm_size_str = lp_parm_const_string(
2082 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
2083 "time machine max size", NULL);
2084 if (tm_size_str != NULL) {
2085 config->time_machine_max_size = conv_str_size(tm_size_str);
2088 SMB_VFS_HANDLE_SET_DATA(handle, config,
2089 NULL, struct fruit_config_data,
2090 return -1);
2092 return 0;
2096 * Prepend "._" to a basename
2097 * Return a new struct smb_filename with stream_name == NULL.
2099 static int adouble_path(TALLOC_CTX *ctx,
2100 const struct smb_filename *smb_fname_in,
2101 struct smb_filename **pp_smb_fname_out)
2103 char *parent;
2104 const char *base;
2105 struct smb_filename *smb_fname = cp_smb_filename(ctx,
2106 smb_fname_in);
2108 if (smb_fname == NULL) {
2109 return -1;
2112 /* We need streamname to be NULL */
2113 TALLOC_FREE(smb_fname->stream_name);
2115 /* And we're replacing base_name. */
2116 TALLOC_FREE(smb_fname->base_name);
2118 if (!parent_dirname(smb_fname, smb_fname_in->base_name,
2119 &parent, &base)) {
2120 TALLOC_FREE(smb_fname);
2121 return -1;
2124 smb_fname->base_name = talloc_asprintf(smb_fname,
2125 "%s/._%s", parent, base);
2126 if (smb_fname->base_name == NULL) {
2127 TALLOC_FREE(smb_fname);
2128 return -1;
2131 *pp_smb_fname_out = smb_fname;
2133 return 0;
2137 * Allocate and initialize an AfpInfo struct
2139 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
2141 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2142 if (ai == NULL) {
2143 return NULL;
2145 ai->afpi_Signature = AFP_Signature;
2146 ai->afpi_Version = AFP_Version;
2147 ai->afpi_BackupTime = AD_DATE_START;
2148 return ai;
2152 * Pack an AfpInfo struct into a buffer
2154 * Buffer size must be at least AFP_INFO_SIZE
2155 * Returns size of packed buffer
2157 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
2159 memset(buf, 0, AFP_INFO_SIZE);
2161 RSIVAL(buf, 0, ai->afpi_Signature);
2162 RSIVAL(buf, 4, ai->afpi_Version);
2163 RSIVAL(buf, 12, ai->afpi_BackupTime);
2164 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
2166 return AFP_INFO_SIZE;
2170 * Unpack a buffer into a AfpInfo structure
2172 * Buffer size must be at least AFP_INFO_SIZE
2173 * Returns allocated AfpInfo struct
2175 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
2177 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
2178 if (ai == NULL) {
2179 return NULL;
2182 ai->afpi_Signature = RIVAL(data, 0);
2183 ai->afpi_Version = RIVAL(data, 4);
2184 ai->afpi_BackupTime = RIVAL(data, 12);
2185 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
2186 sizeof(ai->afpi_FinderInfo));
2188 if (ai->afpi_Signature != AFP_Signature
2189 || ai->afpi_Version != AFP_Version) {
2190 DEBUG(1, ("Bad AfpInfo signature or version\n"));
2191 TALLOC_FREE(ai);
2194 return ai;
2198 * Fake an inode number from the md5 hash of the (xattr) name
2200 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
2202 MD5_CTX ctx;
2203 unsigned char hash[16];
2204 SMB_INO_T result;
2205 char *upper_sname;
2207 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
2208 SMB_ASSERT(upper_sname != NULL);
2210 MD5Init(&ctx);
2211 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
2212 sizeof(sbuf->st_ex_dev));
2213 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
2214 sizeof(sbuf->st_ex_ino));
2215 MD5Update(&ctx, (unsigned char *)upper_sname,
2216 talloc_get_size(upper_sname)-1);
2217 MD5Final(hash, &ctx);
2219 TALLOC_FREE(upper_sname);
2221 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
2222 memcpy(&result, hash, sizeof(result));
2224 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
2225 sname, (unsigned long long)result));
2227 return result;
2230 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2231 struct stream_struct **streams,
2232 const char *name, off_t size,
2233 off_t alloc_size)
2235 struct stream_struct *tmp;
2237 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
2238 (*num_streams)+1);
2239 if (tmp == NULL) {
2240 return false;
2243 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
2244 if (tmp[*num_streams].name == NULL) {
2245 return false;
2248 tmp[*num_streams].size = size;
2249 tmp[*num_streams].alloc_size = alloc_size;
2251 *streams = tmp;
2252 *num_streams += 1;
2253 return true;
2256 static bool filter_empty_rsrc_stream(unsigned int *num_streams,
2257 struct stream_struct **streams)
2259 struct stream_struct *tmp = *streams;
2260 unsigned int i;
2262 if (*num_streams == 0) {
2263 return true;
2266 for (i = 0; i < *num_streams; i++) {
2267 if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
2268 break;
2272 if (i == *num_streams) {
2273 return true;
2276 if (tmp[i].size > 0) {
2277 return true;
2280 TALLOC_FREE(tmp[i].name);
2281 if (*num_streams - 1 > i) {
2282 memmove(&tmp[i], &tmp[i+1],
2283 (*num_streams - i - 1) * sizeof(struct stream_struct));
2286 *num_streams -= 1;
2287 return true;
2290 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
2291 struct stream_struct **streams,
2292 const char *name)
2294 struct stream_struct *tmp = *streams;
2295 unsigned int i;
2297 if (*num_streams == 0) {
2298 return true;
2301 for (i = 0; i < *num_streams; i++) {
2302 if (strequal_m(tmp[i].name, name)) {
2303 break;
2307 if (i == *num_streams) {
2308 return true;
2311 TALLOC_FREE(tmp[i].name);
2312 if (*num_streams - 1 > i) {
2313 memmove(&tmp[i], &tmp[i+1],
2314 (*num_streams - i - 1) * sizeof(struct stream_struct));
2317 *num_streams -= 1;
2318 return true;
2321 static bool ad_empty_finderinfo(const struct adouble *ad)
2323 int cmp;
2324 char emptybuf[ADEDLEN_FINDERI] = {0};
2325 char *fi = NULL;
2327 fi = ad_get_entry(ad, ADEID_FINDERI);
2328 if (fi == NULL) {
2329 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
2330 return false;
2333 cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
2334 return (cmp == 0);
2337 static bool ai_empty_finderinfo(const AfpInfo *ai)
2339 int cmp;
2340 char emptybuf[ADEDLEN_FINDERI] = {0};
2342 cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2343 return (cmp == 0);
2347 * Update btime with btime from Netatalk
2349 static void update_btime(vfs_handle_struct *handle,
2350 struct smb_filename *smb_fname)
2352 uint32_t t;
2353 struct timespec creation_time = {0};
2354 struct adouble *ad;
2355 struct fruit_config_data *config = NULL;
2357 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2358 return);
2360 switch (config->meta) {
2361 case FRUIT_META_STREAM:
2362 return;
2363 case FRUIT_META_NETATALK:
2364 /* Handled below */
2365 break;
2366 default:
2367 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2368 return;
2371 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2372 if (ad == NULL) {
2373 return;
2375 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
2376 TALLOC_FREE(ad);
2377 return;
2379 TALLOC_FREE(ad);
2381 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
2382 update_stat_ex_create_time(&smb_fname->st, creation_time);
2384 return;
2388 * Map an access mask to a Netatalk single byte byte range lock
2390 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
2391 uint32_t access_mask)
2393 off_t offset;
2395 switch (access_mask) {
2396 case FILE_READ_DATA:
2397 offset = AD_FILELOCK_OPEN_RD;
2398 break;
2400 case FILE_WRITE_DATA:
2401 case FILE_APPEND_DATA:
2402 offset = AD_FILELOCK_OPEN_WR;
2403 break;
2405 default:
2406 offset = AD_FILELOCK_OPEN_NONE;
2407 break;
2410 if (fork_type == APPLE_FORK_RSRC) {
2411 if (offset == AD_FILELOCK_OPEN_NONE) {
2412 offset = AD_FILELOCK_RSRC_OPEN_NONE;
2413 } else {
2414 offset += 2;
2418 return offset;
2422 * Map a deny mode to a Netatalk brl
2424 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
2425 uint32_t deny_mode)
2427 off_t offset = 0;
2429 switch (deny_mode) {
2430 case DENY_READ:
2431 offset = AD_FILELOCK_DENY_RD;
2432 break;
2434 case DENY_WRITE:
2435 offset = AD_FILELOCK_DENY_WR;
2436 break;
2438 default:
2439 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
2442 if (fork_type == APPLE_FORK_RSRC) {
2443 offset += 2;
2446 return offset;
2450 * Call fcntl() with an exclusive F_GETLK request in order to
2451 * determine if there's an exisiting shared lock
2453 * @return true if the requested lock was found or any error occurred
2454 * false if the lock was not found
2456 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
2458 bool result;
2459 off_t offset = in_offset;
2460 off_t len = 1;
2461 int type = F_WRLCK;
2462 pid_t pid;
2464 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
2465 if (result == false) {
2466 return true;
2469 if (type != F_UNLCK) {
2470 return true;
2473 return false;
2476 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
2477 files_struct *fsp,
2478 uint32_t access_mask,
2479 uint32_t deny_mode)
2481 NTSTATUS status = NT_STATUS_OK;
2482 bool open_for_reading, open_for_writing, deny_read, deny_write;
2483 off_t off;
2484 bool have_read = false;
2485 int flags;
2487 /* FIXME: hardcoded data fork, add resource fork */
2488 enum apple_fork fork_type = APPLE_FORK_DATA;
2490 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
2491 fsp_str_dbg(fsp),
2492 access_mask & FILE_READ_DATA ? "READ" :"-",
2493 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
2494 deny_mode & DENY_READ ? "DENY_READ" : "-",
2495 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
2497 if (fsp->fh->fd == -1) {
2498 return NT_STATUS_OK;
2501 flags = fcntl(fsp->fh->fd, F_GETFL);
2502 if (flags == -1) {
2503 DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
2504 fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
2505 return map_nt_error_from_unix(errno);
2508 if (flags & (O_RDONLY|O_RDWR)) {
2510 * Applying fcntl read locks requires an fd opened for
2511 * reading. This means we won't be applying locks for
2512 * files openend write-only, but what can we do...
2514 have_read = true;
2518 * Check read access and deny read mode
2520 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
2521 /* Check access */
2522 open_for_reading = test_netatalk_lock(
2523 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
2525 deny_read = test_netatalk_lock(
2526 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
2528 DEBUG(10, ("read: %s, deny_write: %s\n",
2529 open_for_reading == true ? "yes" : "no",
2530 deny_read == true ? "yes" : "no"));
2532 if (((access_mask & FILE_READ_DATA) && deny_read)
2533 || ((deny_mode & DENY_READ) && open_for_reading)) {
2534 return NT_STATUS_SHARING_VIOLATION;
2537 /* Set locks */
2538 if ((access_mask & FILE_READ_DATA) && have_read) {
2539 struct byte_range_lock *br_lck = NULL;
2541 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
2542 br_lck = do_lock(
2543 handle->conn->sconn->msg_ctx, fsp,
2544 fsp->op->global->open_persistent_id, 1, off,
2545 READ_LOCK, POSIX_LOCK, false,
2546 &status, NULL);
2548 TALLOC_FREE(br_lck);
2550 if (!NT_STATUS_IS_OK(status)) {
2551 return status;
2555 if ((deny_mode & DENY_READ) && have_read) {
2556 struct byte_range_lock *br_lck = NULL;
2558 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
2559 br_lck = do_lock(
2560 handle->conn->sconn->msg_ctx, fsp,
2561 fsp->op->global->open_persistent_id, 1, off,
2562 READ_LOCK, POSIX_LOCK, false,
2563 &status, NULL);
2565 TALLOC_FREE(br_lck);
2567 if (!NT_STATUS_IS_OK(status)) {
2568 return status;
2574 * Check write access and deny write mode
2576 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
2577 /* Check access */
2578 open_for_writing = test_netatalk_lock(
2579 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
2581 deny_write = test_netatalk_lock(
2582 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
2584 DEBUG(10, ("write: %s, deny_write: %s\n",
2585 open_for_writing == true ? "yes" : "no",
2586 deny_write == true ? "yes" : "no"));
2588 if (((access_mask & FILE_WRITE_DATA) && deny_write)
2589 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
2590 return NT_STATUS_SHARING_VIOLATION;
2593 /* Set locks */
2594 if ((access_mask & FILE_WRITE_DATA) && have_read) {
2595 struct byte_range_lock *br_lck = NULL;
2597 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
2598 br_lck = do_lock(
2599 handle->conn->sconn->msg_ctx, fsp,
2600 fsp->op->global->open_persistent_id, 1, off,
2601 READ_LOCK, POSIX_LOCK, false,
2602 &status, NULL);
2604 TALLOC_FREE(br_lck);
2606 if (!NT_STATUS_IS_OK(status)) {
2607 return status;
2610 if ((deny_mode & DENY_WRITE) && have_read) {
2611 struct byte_range_lock *br_lck = NULL;
2613 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
2614 br_lck = do_lock(
2615 handle->conn->sconn->msg_ctx, fsp,
2616 fsp->op->global->open_persistent_id, 1, off,
2617 READ_LOCK, POSIX_LOCK, false,
2618 &status, NULL);
2620 TALLOC_FREE(br_lck);
2622 if (!NT_STATUS_IS_OK(status)) {
2623 return status;
2628 return status;
2631 static NTSTATUS check_aapl(vfs_handle_struct *handle,
2632 struct smb_request *req,
2633 const struct smb2_create_blobs *in_context_blobs,
2634 struct smb2_create_blobs *out_context_blobs)
2636 struct fruit_config_data *config;
2637 NTSTATUS status;
2638 struct smb2_create_blob *aapl = NULL;
2639 uint32_t cmd;
2640 bool ok;
2641 uint8_t p[16];
2642 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
2643 uint64_t req_bitmap, client_caps;
2644 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
2645 smb_ucs2_t *model;
2646 size_t modellen;
2648 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2649 return NT_STATUS_UNSUCCESSFUL);
2651 if (!config->use_aapl
2652 || in_context_blobs == NULL
2653 || out_context_blobs == NULL) {
2654 return NT_STATUS_OK;
2657 aapl = smb2_create_blob_find(in_context_blobs,
2658 SMB2_CREATE_TAG_AAPL);
2659 if (aapl == NULL) {
2660 return NT_STATUS_OK;
2663 if (aapl->data.length != 24) {
2664 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
2665 (uintmax_t)aapl->data.length));
2666 return NT_STATUS_INVALID_PARAMETER;
2669 cmd = IVAL(aapl->data.data, 0);
2670 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
2671 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
2672 return NT_STATUS_INVALID_PARAMETER;
2675 req_bitmap = BVAL(aapl->data.data, 8);
2676 client_caps = BVAL(aapl->data.data, 16);
2678 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
2679 SIVAL(p, 4, 0);
2680 SBVAL(p, 8, req_bitmap);
2681 ok = data_blob_append(req, &blob, p, 16);
2682 if (!ok) {
2683 return NT_STATUS_UNSUCCESSFUL;
2686 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
2687 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
2688 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
2689 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
2690 config->readdir_attr_enabled = true;
2693 if (config->use_copyfile) {
2694 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
2695 config->copyfile_enabled = true;
2699 * The client doesn't set the flag, so we can't check
2700 * for it and just set it unconditionally
2702 if (config->unix_info_enabled) {
2703 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
2706 SBVAL(p, 0, server_caps);
2707 ok = data_blob_append(req, &blob, p, 8);
2708 if (!ok) {
2709 return NT_STATUS_UNSUCCESSFUL;
2713 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
2714 int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
2715 uint64_t caps = 0;
2717 switch (val) {
2718 case Auto:
2719 break;
2721 case True:
2722 caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
2723 break;
2725 default:
2726 break;
2729 if (config->time_machine) {
2730 caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
2733 SBVAL(p, 0, caps);
2735 ok = data_blob_append(req, &blob, p, 8);
2736 if (!ok) {
2737 return NT_STATUS_UNSUCCESSFUL;
2741 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
2742 ok = convert_string_talloc(req,
2743 CH_UNIX, CH_UTF16LE,
2744 config->model, strlen(config->model),
2745 &model, &modellen);
2746 if (!ok) {
2747 return NT_STATUS_UNSUCCESSFUL;
2750 SIVAL(p, 0, 0);
2751 SIVAL(p + 4, 0, modellen);
2752 ok = data_blob_append(req, &blob, p, 8);
2753 if (!ok) {
2754 talloc_free(model);
2755 return NT_STATUS_UNSUCCESSFUL;
2758 ok = data_blob_append(req, &blob, model, modellen);
2759 talloc_free(model);
2760 if (!ok) {
2761 return NT_STATUS_UNSUCCESSFUL;
2765 status = smb2_create_blob_add(out_context_blobs,
2766 out_context_blobs,
2767 SMB2_CREATE_TAG_AAPL,
2768 blob);
2769 if (NT_STATUS_IS_OK(status)) {
2770 global_fruit_config.nego_aapl = true;
2771 if (config->aapl_zero_file_id) {
2772 aapl_force_zero_file_id(handle->conn->sconn);
2776 return status;
2779 static bool readdir_attr_meta_finderi_stream(
2780 struct vfs_handle_struct *handle,
2781 const struct smb_filename *smb_fname,
2782 AfpInfo *ai)
2784 struct smb_filename *stream_name = NULL;
2785 files_struct *fsp = NULL;
2786 ssize_t nread;
2787 NTSTATUS status;
2788 int ret;
2789 bool ok;
2790 uint8_t buf[AFP_INFO_SIZE];
2792 stream_name = synthetic_smb_fname(talloc_tos(),
2793 smb_fname->base_name,
2794 AFPINFO_STREAM_NAME,
2795 NULL, smb_fname->flags);
2796 if (stream_name == NULL) {
2797 return false;
2800 ret = SMB_VFS_STAT(handle->conn, stream_name);
2801 if (ret != 0) {
2802 return false;
2805 status = SMB_VFS_CREATE_FILE(
2806 handle->conn, /* conn */
2807 NULL, /* req */
2808 0, /* root_dir_fid */
2809 stream_name, /* fname */
2810 FILE_READ_DATA, /* access_mask */
2811 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2812 FILE_SHARE_DELETE),
2813 FILE_OPEN, /* create_disposition*/
2814 0, /* create_options */
2815 0, /* file_attributes */
2816 INTERNAL_OPEN_ONLY, /* oplock_request */
2817 NULL, /* lease */
2818 0, /* allocation_size */
2819 0, /* private_flags */
2820 NULL, /* sd */
2821 NULL, /* ea_list */
2822 &fsp, /* result */
2823 NULL, /* pinfo */
2824 NULL, NULL); /* create context */
2826 TALLOC_FREE(stream_name);
2828 if (!NT_STATUS_IS_OK(status)) {
2829 return false;
2832 nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
2833 if (nread != AFP_INFO_SIZE) {
2834 DBG_ERR("short read [%s] [%zd/%d]\n",
2835 smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
2836 ok = false;
2837 goto fail;
2840 memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
2841 AFP_FinderSize);
2843 ok = true;
2845 fail:
2846 if (fsp != NULL) {
2847 close_file(NULL, fsp, NORMAL_CLOSE);
2850 return ok;
2853 static bool readdir_attr_meta_finderi_netatalk(
2854 struct vfs_handle_struct *handle,
2855 const struct smb_filename *smb_fname,
2856 AfpInfo *ai)
2858 struct adouble *ad = NULL;
2859 char *p = NULL;
2861 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
2862 if (ad == NULL) {
2863 return false;
2866 p = ad_get_entry(ad, ADEID_FINDERI);
2867 if (p == NULL) {
2868 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
2869 TALLOC_FREE(ad);
2870 return false;
2873 memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
2874 TALLOC_FREE(ad);
2875 return true;
2878 static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
2879 const struct smb_filename *smb_fname,
2880 struct readdir_attr_data *attr_data)
2882 struct fruit_config_data *config = NULL;
2883 uint32_t date_added;
2884 AfpInfo ai = {0};
2885 bool ok;
2887 SMB_VFS_HANDLE_GET_DATA(handle, config,
2888 struct fruit_config_data,
2889 return false);
2891 switch (config->meta) {
2892 case FRUIT_META_NETATALK:
2893 ok = readdir_attr_meta_finderi_netatalk(
2894 handle, smb_fname, &ai);
2895 break;
2897 case FRUIT_META_STREAM:
2898 ok = readdir_attr_meta_finderi_stream(
2899 handle, smb_fname, &ai);
2900 break;
2902 default:
2903 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
2904 return false;
2907 if (!ok) {
2908 /* Don't bother with errors, it's likely ENOENT */
2909 return true;
2912 if (S_ISREG(smb_fname->st.st_ex_mode)) {
2913 /* finder_type */
2914 memcpy(&attr_data->attr_data.aapl.finder_info[0],
2915 &ai.afpi_FinderInfo[0], 4);
2917 /* finder_creator */
2918 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
2919 &ai.afpi_FinderInfo[4], 4);
2922 /* finder_flags */
2923 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
2924 &ai.afpi_FinderInfo[8], 2);
2926 /* finder_ext_flags */
2927 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
2928 &ai.afpi_FinderInfo[24], 2);
2930 /* creation date */
2931 date_added = convert_time_t_to_uint32_t(
2932 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2934 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2936 return true;
2939 static uint64_t readdir_attr_rfork_size_adouble(
2940 struct vfs_handle_struct *handle,
2941 const struct smb_filename *smb_fname)
2943 struct adouble *ad = NULL;
2944 uint64_t rfork_size;
2946 ad = ad_get(talloc_tos(), handle, smb_fname,
2947 ADOUBLE_RSRC);
2948 if (ad == NULL) {
2949 return 0;
2952 rfork_size = ad_getentrylen(ad, ADEID_RFORK);
2953 TALLOC_FREE(ad);
2955 return rfork_size;
2958 static uint64_t readdir_attr_rfork_size_stream(
2959 struct vfs_handle_struct *handle,
2960 const struct smb_filename *smb_fname)
2962 struct smb_filename *stream_name = NULL;
2963 int ret;
2964 uint64_t rfork_size;
2966 stream_name = synthetic_smb_fname(talloc_tos(),
2967 smb_fname->base_name,
2968 AFPRESOURCE_STREAM_NAME,
2969 NULL, 0);
2970 if (stream_name == NULL) {
2971 return 0;
2974 ret = SMB_VFS_STAT(handle->conn, stream_name);
2975 if (ret != 0) {
2976 TALLOC_FREE(stream_name);
2977 return 0;
2980 rfork_size = stream_name->st.st_ex_size;
2981 TALLOC_FREE(stream_name);
2983 return rfork_size;
2986 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
2987 const struct smb_filename *smb_fname)
2989 struct fruit_config_data *config = NULL;
2990 uint64_t rfork_size;
2992 SMB_VFS_HANDLE_GET_DATA(handle, config,
2993 struct fruit_config_data,
2994 return 0);
2996 switch (config->rsrc) {
2997 case FRUIT_RSRC_ADFILE:
2998 case FRUIT_RSRC_XATTR:
2999 rfork_size = readdir_attr_rfork_size_adouble(handle,
3000 smb_fname);
3001 break;
3003 case FRUIT_META_STREAM:
3004 rfork_size = readdir_attr_rfork_size_stream(handle,
3005 smb_fname);
3006 break;
3008 default:
3009 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3010 rfork_size = 0;
3011 break;
3014 return rfork_size;
3017 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
3018 const struct smb_filename *smb_fname,
3019 struct readdir_attr_data *attr_data)
3021 NTSTATUS status = NT_STATUS_OK;
3022 struct fruit_config_data *config = NULL;
3023 bool ok;
3025 SMB_VFS_HANDLE_GET_DATA(handle, config,
3026 struct fruit_config_data,
3027 return NT_STATUS_UNSUCCESSFUL);
3030 /* Ensure we return a default value in the creation_date field */
3031 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
3034 * Resource fork length
3037 if (config->readdir_attr_rsize) {
3038 uint64_t rfork_size;
3040 rfork_size = readdir_attr_rfork_size(handle, smb_fname);
3041 attr_data->attr_data.aapl.rfork_size = rfork_size;
3045 * FinderInfo
3048 if (config->readdir_attr_finder_info) {
3049 ok = readdir_attr_meta_finderi(handle, smb_fname, attr_data);
3050 if (!ok) {
3051 status = NT_STATUS_INTERNAL_ERROR;
3055 return status;
3058 static NTSTATUS remove_virtual_nfs_aces(struct security_descriptor *psd)
3060 NTSTATUS status;
3061 uint32_t i;
3063 if (psd->dacl == NULL) {
3064 return NT_STATUS_OK;
3067 for (i = 0; i < psd->dacl->num_aces; i++) {
3068 /* MS NFS style mode/uid/gid */
3069 int cmp = dom_sid_compare_domain(
3070 &global_sid_Unix_NFS,
3071 &psd->dacl->aces[i].trustee);
3072 if (cmp != 0) {
3073 /* Normal ACE entry. */
3074 continue;
3078 * security_descriptor_dacl_del()
3079 * *must* return NT_STATUS_OK as we know
3080 * we have something to remove.
3083 status = security_descriptor_dacl_del(psd,
3084 &psd->dacl->aces[i].trustee);
3085 if (!NT_STATUS_IS_OK(status)) {
3086 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
3087 nt_errstr(status));
3088 return status;
3092 * security_descriptor_dacl_del() may delete more
3093 * then one entry subsequent to this one if the
3094 * SID matches, but we only need to ensure that
3095 * we stay looking at the same element in the array.
3097 i--;
3099 return NT_STATUS_OK;
3102 /* Search MS NFS style ACE with UNIX mode */
3103 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
3104 files_struct *fsp,
3105 struct security_descriptor *psd,
3106 mode_t *pmode,
3107 bool *pdo_chmod)
3109 uint32_t i;
3110 struct fruit_config_data *config = NULL;
3112 *pdo_chmod = false;
3114 SMB_VFS_HANDLE_GET_DATA(handle, config,
3115 struct fruit_config_data,
3116 return NT_STATUS_UNSUCCESSFUL);
3118 if (!global_fruit_config.nego_aapl) {
3119 return NT_STATUS_OK;
3121 if (psd->dacl == NULL || !config->unix_info_enabled) {
3122 return NT_STATUS_OK;
3125 for (i = 0; i < psd->dacl->num_aces; i++) {
3126 if (dom_sid_compare_domain(
3127 &global_sid_Unix_NFS_Mode,
3128 &psd->dacl->aces[i].trustee) == 0) {
3129 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
3130 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
3131 *pdo_chmod = true;
3133 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
3134 fsp_str_dbg(fsp), (unsigned)(*pmode)));
3135 break;
3140 * Remove any incoming virtual ACE entries generated by
3141 * fruit_fget_nt_acl().
3144 return remove_virtual_nfs_aces(psd);
3147 /****************************************************************************
3148 * VFS ops
3149 ****************************************************************************/
3151 static int fruit_connect(vfs_handle_struct *handle,
3152 const char *service,
3153 const char *user)
3155 int rc;
3156 char *list = NULL, *newlist = NULL;
3157 struct fruit_config_data *config;
3159 DEBUG(10, ("fruit_connect\n"));
3161 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
3162 if (rc < 0) {
3163 return rc;
3166 rc = init_fruit_config(handle);
3167 if (rc != 0) {
3168 return rc;
3171 SMB_VFS_HANDLE_GET_DATA(handle, config,
3172 struct fruit_config_data, return -1);
3174 if (config->veto_appledouble) {
3175 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
3177 if (list) {
3178 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
3179 newlist = talloc_asprintf(
3180 list,
3181 "%s/" ADOUBLE_NAME_PREFIX "*/",
3182 list);
3183 lp_do_parameter(SNUM(handle->conn),
3184 "veto files",
3185 newlist);
3187 } else {
3188 lp_do_parameter(SNUM(handle->conn),
3189 "veto files",
3190 "/" ADOUBLE_NAME_PREFIX "*/");
3193 TALLOC_FREE(list);
3196 if (config->encoding == FRUIT_ENC_NATIVE) {
3197 lp_do_parameter(SNUM(handle->conn),
3198 "catia:mappings",
3199 fruit_catia_maps);
3202 if (config->time_machine) {
3203 DBG_NOTICE("Enabling durable handles for Time Machine "
3204 "support on [%s]\n", service);
3205 lp_do_parameter(SNUM(handle->conn), "durable handles", "yes");
3206 lp_do_parameter(SNUM(handle->conn), "kernel oplocks", "no");
3207 lp_do_parameter(SNUM(handle->conn), "kernel share modes", "no");
3208 if (!lp_strict_sync(SNUM(handle->conn))) {
3209 DBG_WARNING("Time Machine without strict sync is not "
3210 "recommended!\n");
3212 lp_do_parameter(SNUM(handle->conn), "posix locking", "no");
3215 return rc;
3218 static int fruit_open_meta_stream(vfs_handle_struct *handle,
3219 struct smb_filename *smb_fname,
3220 files_struct *fsp,
3221 int flags,
3222 mode_t mode)
3224 AfpInfo *ai = NULL;
3225 char afpinfo_buf[AFP_INFO_SIZE];
3226 ssize_t len, written;
3227 int hostfd = -1;
3228 int rc = -1;
3230 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3231 if (hostfd == -1) {
3232 return -1;
3235 if (!(flags & (O_CREAT | O_TRUNC))) {
3236 return hostfd;
3239 ai = afpinfo_new(talloc_tos());
3240 if (ai == NULL) {
3241 rc = -1;
3242 goto fail;
3245 len = afpinfo_pack(ai, afpinfo_buf);
3246 if (len != AFP_INFO_SIZE) {
3247 rc = -1;
3248 goto fail;
3251 /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
3252 fsp->fh->fd = hostfd;
3254 written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
3255 AFP_INFO_SIZE, 0);
3256 fsp->fh->fd = -1;
3257 if (written != AFP_INFO_SIZE) {
3258 DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
3259 rc = -1;
3260 goto fail;
3263 rc = 0;
3265 fail:
3266 DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
3268 if (rc != 0) {
3269 int saved_errno = errno;
3270 if (hostfd >= 0) {
3271 fsp->fh->fd = hostfd;
3272 SMB_VFS_NEXT_CLOSE(handle, fsp);
3274 hostfd = -1;
3275 errno = saved_errno;
3277 return hostfd;
3280 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
3281 struct smb_filename *smb_fname,
3282 files_struct *fsp,
3283 int flags,
3284 mode_t mode)
3286 int rc;
3287 int fakefd = -1;
3288 struct adouble *ad = NULL;
3289 int fds[2];
3291 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3294 * Return a valid fd, but ensure any attempt to use it returns an error
3295 * (EPIPE). All operations on the smb_fname or the fsp will use path
3296 * based syscalls.
3298 rc = pipe(fds);
3299 if (rc != 0) {
3300 goto exit;
3302 fakefd = fds[0];
3303 close(fds[1]);
3305 if (flags & (O_CREAT | O_TRUNC)) {
3307 * The attribute does not exist or needs to be truncated,
3308 * create an AppleDouble EA
3310 ad = ad_init(fsp, handle, ADOUBLE_META);
3311 if (ad == NULL) {
3312 rc = -1;
3313 goto exit;
3316 rc = ad_set(ad, fsp->fsp_name);
3317 if (rc != 0) {
3318 rc = -1;
3319 goto exit;
3322 TALLOC_FREE(ad);
3325 exit:
3326 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
3327 if (rc != 0) {
3328 int saved_errno = errno;
3329 if (fakefd >= 0) {
3330 close(fakefd);
3332 fakefd = -1;
3333 errno = saved_errno;
3335 return fakefd;
3338 static int fruit_open_meta(vfs_handle_struct *handle,
3339 struct smb_filename *smb_fname,
3340 files_struct *fsp, int flags, mode_t mode)
3342 int fd;
3343 struct fruit_config_data *config = NULL;
3344 struct fio *fio = NULL;
3346 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
3348 SMB_VFS_HANDLE_GET_DATA(handle, config,
3349 struct fruit_config_data, return -1);
3351 switch (config->meta) {
3352 case FRUIT_META_STREAM:
3353 fd = fruit_open_meta_stream(handle, smb_fname,
3354 fsp, flags, mode);
3355 break;
3357 case FRUIT_META_NETATALK:
3358 fd = fruit_open_meta_netatalk(handle, smb_fname,
3359 fsp, flags, mode);
3360 break;
3362 default:
3363 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
3364 return -1;
3367 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3369 if (fd == -1) {
3370 return -1;
3373 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3374 fio->type = ADOUBLE_META;
3375 fio->config = config;
3377 return fd;
3380 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
3381 struct smb_filename *smb_fname,
3382 files_struct *fsp,
3383 int flags,
3384 mode_t mode)
3386 int rc = 0;
3387 struct adouble *ad = NULL;
3388 struct smb_filename *smb_fname_base = NULL;
3389 struct fruit_config_data *config = NULL;
3390 int hostfd = -1;
3392 SMB_VFS_HANDLE_GET_DATA(handle, config,
3393 struct fruit_config_data, return -1);
3395 if ((!(flags & O_CREAT)) &&
3396 S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
3398 /* sorry, but directories don't habe a resource fork */
3399 rc = -1;
3400 goto exit;
3403 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
3404 if (rc != 0) {
3405 goto exit;
3408 /* Sanitize flags */
3409 if (flags & O_WRONLY) {
3410 /* We always need read access for the metadata header too */
3411 flags &= ~O_WRONLY;
3412 flags |= O_RDWR;
3415 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
3416 flags, mode);
3417 if (hostfd == -1) {
3418 rc = -1;
3419 goto exit;
3422 if (flags & (O_CREAT | O_TRUNC)) {
3423 ad = ad_init(fsp, handle, ADOUBLE_RSRC);
3424 if (ad == NULL) {
3425 rc = -1;
3426 goto exit;
3429 fsp->fh->fd = hostfd;
3431 rc = ad_fset(ad, fsp);
3432 fsp->fh->fd = -1;
3433 if (rc != 0) {
3434 rc = -1;
3435 goto exit;
3437 TALLOC_FREE(ad);
3440 exit:
3442 TALLOC_FREE(smb_fname_base);
3444 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
3445 if (rc != 0) {
3446 int saved_errno = errno;
3447 if (hostfd >= 0) {
3449 * BUGBUGBUG -- we would need to call
3450 * fd_close_posix here, but we don't have a
3451 * full fsp yet
3453 fsp->fh->fd = hostfd;
3454 SMB_VFS_CLOSE(fsp);
3456 hostfd = -1;
3457 errno = saved_errno;
3459 return hostfd;
3462 static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
3463 struct smb_filename *smb_fname,
3464 files_struct *fsp,
3465 int flags,
3466 mode_t mode)
3468 #ifdef HAVE_ATTROPEN
3469 int fd = -1;
3471 fd = attropen(smb_fname->base_name,
3472 AFPRESOURCE_EA_NETATALK,
3473 flags,
3474 mode);
3475 if (fd == -1) {
3476 return -1;
3479 return fd;
3481 #else
3482 errno = ENOSYS;
3483 return -1;
3484 #endif
3487 static int fruit_open_rsrc(vfs_handle_struct *handle,
3488 struct smb_filename *smb_fname,
3489 files_struct *fsp, int flags, mode_t mode)
3491 int fd;
3492 struct fruit_config_data *config = NULL;
3493 struct fio *fio = NULL;
3495 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3497 SMB_VFS_HANDLE_GET_DATA(handle, config,
3498 struct fruit_config_data, return -1);
3500 if (((flags & O_ACCMODE) == O_RDONLY)
3501 && (flags & O_CREAT)
3502 && !VALID_STAT(fsp->fsp_name->st))
3505 * This means the stream doesn't exist. macOS SMB server fails
3506 * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
3507 * 12565 and the test for this combination in
3508 * test_rfork_create().
3510 errno = ENOENT;
3511 return -1;
3514 switch (config->rsrc) {
3515 case FRUIT_RSRC_STREAM:
3516 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3517 break;
3519 case FRUIT_RSRC_ADFILE:
3520 fd = fruit_open_rsrc_adouble(handle, smb_fname,
3521 fsp, flags, mode);
3522 break;
3524 case FRUIT_RSRC_XATTR:
3525 fd = fruit_open_rsrc_xattr(handle, smb_fname,
3526 fsp, flags, mode);
3527 break;
3529 default:
3530 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
3531 return -1;
3534 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3536 if (fd == -1) {
3537 return -1;
3540 fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
3541 fio->type = ADOUBLE_RSRC;
3542 fio->config = config;
3544 return fd;
3547 static int fruit_open(vfs_handle_struct *handle,
3548 struct smb_filename *smb_fname,
3549 files_struct *fsp, int flags, mode_t mode)
3551 int fd;
3553 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
3555 if (!is_ntfs_stream_smb_fname(smb_fname)) {
3556 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3559 if (is_afpinfo_stream(smb_fname)) {
3560 fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
3561 } else if (is_afpresource_stream(smb_fname)) {
3562 fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
3563 } else {
3564 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
3567 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
3569 return fd;
3572 static int fruit_rename(struct vfs_handle_struct *handle,
3573 const struct smb_filename *smb_fname_src,
3574 const struct smb_filename *smb_fname_dst)
3576 int rc = -1;
3577 struct fruit_config_data *config = NULL;
3578 struct smb_filename *src_adp_smb_fname = NULL;
3579 struct smb_filename *dst_adp_smb_fname = NULL;
3581 SMB_VFS_HANDLE_GET_DATA(handle, config,
3582 struct fruit_config_data, return -1);
3584 if (!VALID_STAT(smb_fname_src->st)) {
3585 DBG_ERR("Need valid stat for [%s]\n",
3586 smb_fname_str_dbg(smb_fname_src));
3587 return -1;
3590 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
3591 if (rc != 0) {
3592 return -1;
3595 if ((config->rsrc != FRUIT_RSRC_ADFILE) ||
3596 (!S_ISREG(smb_fname_src->st.st_ex_mode)))
3598 return 0;
3601 rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
3602 if (rc != 0) {
3603 goto done;
3606 rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
3607 if (rc != 0) {
3608 goto done;
3611 DBG_DEBUG("%s -> %s\n",
3612 smb_fname_str_dbg(src_adp_smb_fname),
3613 smb_fname_str_dbg(dst_adp_smb_fname));
3615 rc = SMB_VFS_NEXT_RENAME(handle, src_adp_smb_fname, dst_adp_smb_fname);
3616 if (errno == ENOENT) {
3617 rc = 0;
3620 done:
3621 TALLOC_FREE(src_adp_smb_fname);
3622 TALLOC_FREE(dst_adp_smb_fname);
3623 return rc;
3626 static int fruit_unlink_meta_stream(vfs_handle_struct *handle,
3627 const struct smb_filename *smb_fname)
3629 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3632 static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
3633 const struct smb_filename *smb_fname)
3635 return SMB_VFS_REMOVEXATTR(handle->conn,
3636 smb_fname,
3637 AFPINFO_EA_NETATALK);
3640 static int fruit_unlink_meta(vfs_handle_struct *handle,
3641 const struct smb_filename *smb_fname)
3643 struct fruit_config_data *config = NULL;
3644 int rc;
3646 SMB_VFS_HANDLE_GET_DATA(handle, config,
3647 struct fruit_config_data, return -1);
3649 switch (config->meta) {
3650 case FRUIT_META_STREAM:
3651 rc = fruit_unlink_meta_stream(handle, smb_fname);
3652 break;
3654 case FRUIT_META_NETATALK:
3655 rc = fruit_unlink_meta_netatalk(handle, smb_fname);
3656 break;
3658 default:
3659 DBG_ERR("Unsupported meta config [%d]\n", config->meta);
3660 return -1;
3663 return rc;
3666 static int fruit_unlink_rsrc_stream(vfs_handle_struct *handle,
3667 const struct smb_filename *smb_fname,
3668 bool force_unlink)
3670 int ret;
3672 if (!force_unlink) {
3673 struct smb_filename *smb_fname_cp = NULL;
3674 off_t size;
3676 smb_fname_cp = cp_smb_filename(talloc_tos(), smb_fname);
3677 if (smb_fname_cp == NULL) {
3678 return -1;
3682 * 0 byte resource fork streams are not listed by
3683 * vfs_streaminfo, as a result stream cleanup/deletion of file
3684 * deletion doesn't remove the resourcefork stream.
3687 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_cp);
3688 if (ret != 0) {
3689 TALLOC_FREE(smb_fname_cp);
3690 DBG_ERR("stat [%s] failed [%s]\n",
3691 smb_fname_str_dbg(smb_fname_cp), strerror(errno));
3692 return -1;
3695 size = smb_fname_cp->st.st_ex_size;
3696 TALLOC_FREE(smb_fname_cp);
3698 if (size > 0) {
3699 /* OS X ignores resource fork stream delete requests */
3700 return 0;
3704 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3705 if ((ret != 0) && (errno == ENOENT) && force_unlink) {
3706 ret = 0;
3709 return ret;
3712 static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
3713 const struct smb_filename *smb_fname,
3714 bool force_unlink)
3716 int rc;
3717 struct adouble *ad = NULL;
3718 struct smb_filename *adp_smb_fname = NULL;
3720 if (!force_unlink) {
3721 ad = ad_get(talloc_tos(), handle, smb_fname,
3722 ADOUBLE_RSRC);
3723 if (ad == NULL) {
3724 errno = ENOENT;
3725 return -1;
3730 * 0 byte resource fork streams are not listed by
3731 * vfs_streaminfo, as a result stream cleanup/deletion of file
3732 * deletion doesn't remove the resourcefork stream.
3735 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
3736 /* OS X ignores resource fork stream delete requests */
3737 TALLOC_FREE(ad);
3738 return 0;
3741 TALLOC_FREE(ad);
3744 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3745 if (rc != 0) {
3746 return -1;
3749 rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
3750 TALLOC_FREE(adp_smb_fname);
3751 if ((rc != 0) && (errno == ENOENT) && force_unlink) {
3752 rc = 0;
3755 return rc;
3758 static int fruit_unlink_rsrc_xattr(vfs_handle_struct *handle,
3759 const struct smb_filename *smb_fname,
3760 bool force_unlink)
3763 * OS X ignores resource fork stream delete requests, so nothing to do
3764 * here. Removing the file will remove the xattr anyway, so we don't
3765 * have to take care of removing 0 byte resource forks that could be
3766 * left behind.
3768 return 0;
3771 static int fruit_unlink_rsrc(vfs_handle_struct *handle,
3772 const struct smb_filename *smb_fname,
3773 bool force_unlink)
3775 struct fruit_config_data *config = NULL;
3776 int rc;
3778 SMB_VFS_HANDLE_GET_DATA(handle, config,
3779 struct fruit_config_data, return -1);
3781 switch (config->rsrc) {
3782 case FRUIT_RSRC_STREAM:
3783 rc = fruit_unlink_rsrc_stream(handle, smb_fname, force_unlink);
3784 break;
3786 case FRUIT_RSRC_ADFILE:
3787 rc = fruit_unlink_rsrc_adouble(handle, smb_fname, force_unlink);
3788 break;
3790 case FRUIT_RSRC_XATTR:
3791 rc = fruit_unlink_rsrc_xattr(handle, smb_fname, force_unlink);
3792 break;
3794 default:
3795 DBG_ERR("Unsupported rsrc config [%d]\n", config->rsrc);
3796 return -1;
3799 return rc;
3802 static int fruit_unlink(vfs_handle_struct *handle,
3803 const struct smb_filename *smb_fname)
3805 int rc;
3806 struct fruit_config_data *config = NULL;
3807 struct smb_filename *rsrc_smb_fname = NULL;
3809 SMB_VFS_HANDLE_GET_DATA(handle, config,
3810 struct fruit_config_data, return -1);
3812 if (is_afpinfo_stream(smb_fname)) {
3813 return fruit_unlink_meta(handle, smb_fname);
3814 } else if (is_afpresource_stream(smb_fname)) {
3815 return fruit_unlink_rsrc(handle, smb_fname, false);
3816 } if (is_ntfs_stream_smb_fname(smb_fname)) {
3817 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3821 * A request to delete the base file. Because 0 byte resource
3822 * fork streams are not listed by fruit_streaminfo,
3823 * delete_all_streams() can't remove 0 byte resource fork
3824 * streams, so we have to cleanup this here.
3826 rsrc_smb_fname = synthetic_smb_fname(talloc_tos(),
3827 smb_fname->base_name,
3828 AFPRESOURCE_STREAM_NAME,
3829 NULL,
3830 smb_fname->flags);
3831 if (rsrc_smb_fname == NULL) {
3832 return -1;
3835 rc = fruit_unlink_rsrc(handle, rsrc_smb_fname, true);
3836 if ((rc != 0) && (errno != ENOENT)) {
3837 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
3838 smb_fname_str_dbg(rsrc_smb_fname), strerror(errno));
3839 TALLOC_FREE(rsrc_smb_fname);
3840 return -1;
3842 TALLOC_FREE(rsrc_smb_fname);
3844 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
3847 static int fruit_chmod(vfs_handle_struct *handle,
3848 const struct smb_filename *smb_fname,
3849 mode_t mode)
3851 int rc = -1;
3852 struct fruit_config_data *config = NULL;
3853 struct smb_filename *smb_fname_adp = NULL;
3855 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
3856 if (rc != 0) {
3857 return rc;
3860 SMB_VFS_HANDLE_GET_DATA(handle, config,
3861 struct fruit_config_data, return -1);
3863 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3864 return 0;
3867 if (!VALID_STAT(smb_fname->st)) {
3868 return 0;
3871 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3872 return 0;
3875 rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
3876 if (rc != 0) {
3877 return -1;
3880 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
3882 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
3883 if (errno == ENOENT) {
3884 rc = 0;
3887 TALLOC_FREE(smb_fname_adp);
3888 return rc;
3891 static int fruit_chown(vfs_handle_struct *handle,
3892 const struct smb_filename *smb_fname,
3893 uid_t uid,
3894 gid_t gid)
3896 int rc = -1;
3897 struct fruit_config_data *config = NULL;
3898 struct smb_filename *adp_smb_fname = NULL;
3900 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
3901 if (rc != 0) {
3902 return rc;
3905 SMB_VFS_HANDLE_GET_DATA(handle, config,
3906 struct fruit_config_data, return -1);
3908 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3909 return 0;
3912 if (!VALID_STAT(smb_fname->st)) {
3913 return 0;
3916 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
3917 return 0;
3920 rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
3921 if (rc != 0) {
3922 goto done;
3925 DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
3927 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
3928 if (errno == ENOENT) {
3929 rc = 0;
3932 done:
3933 TALLOC_FREE(adp_smb_fname);
3934 return rc;
3937 static int fruit_rmdir(struct vfs_handle_struct *handle,
3938 const struct smb_filename *smb_fname)
3940 DIR *dh = NULL;
3941 struct dirent *de;
3942 struct fruit_config_data *config;
3944 SMB_VFS_HANDLE_GET_DATA(handle, config,
3945 struct fruit_config_data, return -1);
3947 if (config->rsrc != FRUIT_RSRC_ADFILE) {
3948 goto exit_rmdir;
3952 * Due to there is no way to change bDeleteVetoFiles variable
3953 * from this module, need to clean up ourselves
3956 dh = SMB_VFS_OPENDIR(handle->conn, smb_fname, NULL, 0);
3957 if (dh == NULL) {
3958 goto exit_rmdir;
3961 while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
3962 int match;
3963 struct adouble *ad = NULL;
3964 char *p = NULL;
3965 struct smb_filename *ad_smb_fname = NULL;
3966 int ret;
3968 match = strncmp(de->d_name,
3969 ADOUBLE_NAME_PREFIX,
3970 strlen(ADOUBLE_NAME_PREFIX));
3971 if (match != 0) {
3972 continue;
3975 p = talloc_asprintf(talloc_tos(), "%s/%s",
3976 smb_fname->base_name, de->d_name);
3977 if (p == NULL) {
3978 DBG_ERR("talloc_asprintf failed\n");
3979 return -1;
3982 ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
3983 NULL, NULL,
3984 smb_fname->flags);
3985 TALLOC_FREE(p);
3986 if (ad_smb_fname == NULL) {
3987 DBG_ERR("synthetic_smb_fname failed\n");
3988 return -1;
3992 * Check whether it's a valid AppleDouble file, if
3993 * yes, delete it, ignore it otherwise.
3995 ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
3996 if (ad == NULL) {
3997 TALLOC_FREE(ad_smb_fname);
3998 TALLOC_FREE(p);
3999 continue;
4001 TALLOC_FREE(ad);
4003 ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
4004 if (ret != 0) {
4005 DBG_ERR("Deleting [%s] failed\n",
4006 smb_fname_str_dbg(ad_smb_fname));
4008 TALLOC_FREE(ad_smb_fname);
4011 exit_rmdir:
4012 if (dh) {
4013 SMB_VFS_CLOSEDIR(handle->conn, dh);
4015 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
4018 static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
4019 files_struct *fsp, void *data,
4020 size_t n, off_t offset)
4022 ssize_t nread;
4023 int ret;
4025 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4027 if (nread == n) {
4028 return nread;
4031 DBG_ERR("Removing [%s] after short read [%zd]\n",
4032 fsp_str_dbg(fsp), nread);
4034 ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
4035 if (ret != 0) {
4036 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
4037 return -1;
4040 errno = EINVAL;
4041 return -1;
4044 static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
4045 files_struct *fsp, void *data,
4046 size_t n, off_t offset)
4048 AfpInfo *ai = NULL;
4049 struct adouble *ad = NULL;
4050 char afpinfo_buf[AFP_INFO_SIZE];
4051 char *p = NULL;
4052 ssize_t nread;
4054 ai = afpinfo_new(talloc_tos());
4055 if (ai == NULL) {
4056 return -1;
4059 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4060 if (ad == NULL) {
4061 nread = -1;
4062 goto fail;
4065 p = ad_get_entry(ad, ADEID_FINDERI);
4066 if (p == NULL) {
4067 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4068 nread = -1;
4069 goto fail;
4072 memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
4074 nread = afpinfo_pack(ai, afpinfo_buf);
4075 if (nread != AFP_INFO_SIZE) {
4076 nread = -1;
4077 goto fail;
4080 memcpy(data, afpinfo_buf, n);
4081 nread = n;
4083 fail:
4084 TALLOC_FREE(ai);
4085 return nread;
4088 static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
4089 files_struct *fsp, void *data,
4090 size_t n, off_t offset)
4092 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4093 ssize_t nread;
4094 ssize_t to_return;
4097 * OS X has a off-by-1 error in the offset calculation, so we're
4098 * bug compatible here. It won't hurt, as any relevant real
4099 * world read requests from the AFP_AfpInfo stream will be
4100 * offset=0 n=60. offset is ignored anyway, see below.
4102 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
4103 return 0;
4106 if (fio == NULL) {
4107 DBG_ERR("Failed to fetch fsp extension");
4108 return -1;
4111 /* Yes, macOS always reads from offset 0 */
4112 offset = 0;
4113 to_return = MIN(n, AFP_INFO_SIZE);
4115 switch (fio->config->meta) {
4116 case FRUIT_META_STREAM:
4117 nread = fruit_pread_meta_stream(handle, fsp, data,
4118 to_return, offset);
4119 break;
4121 case FRUIT_META_NETATALK:
4122 nread = fruit_pread_meta_adouble(handle, fsp, data,
4123 to_return, offset);
4124 break;
4126 default:
4127 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4128 return -1;
4131 return nread;
4134 static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
4135 files_struct *fsp, void *data,
4136 size_t n, off_t offset)
4138 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4141 static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
4142 files_struct *fsp, void *data,
4143 size_t n, off_t offset)
4145 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4148 static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
4149 files_struct *fsp, void *data,
4150 size_t n, off_t offset)
4152 struct adouble *ad = NULL;
4153 ssize_t nread;
4155 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4156 if (ad == NULL) {
4157 return -1;
4160 nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
4161 offset + ad_getentryoff(ad, ADEID_RFORK));
4163 TALLOC_FREE(ad);
4164 return nread;
4167 static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
4168 files_struct *fsp, void *data,
4169 size_t n, off_t offset)
4171 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4172 ssize_t nread;
4174 if (fio == NULL) {
4175 errno = EINVAL;
4176 return -1;
4179 switch (fio->config->rsrc) {
4180 case FRUIT_RSRC_STREAM:
4181 nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
4182 break;
4184 case FRUIT_RSRC_ADFILE:
4185 nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
4186 break;
4188 case FRUIT_RSRC_XATTR:
4189 nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
4190 break;
4192 default:
4193 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4194 return -1;
4197 return nread;
4200 static ssize_t fruit_pread(vfs_handle_struct *handle,
4201 files_struct *fsp, void *data,
4202 size_t n, off_t offset)
4204 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4205 ssize_t nread;
4207 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4208 fsp_str_dbg(fsp), (intmax_t)offset, n);
4210 if (fio == NULL) {
4211 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
4214 if (fio->type == ADOUBLE_META) {
4215 nread = fruit_pread_meta(handle, fsp, data, n, offset);
4216 } else {
4217 nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
4220 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
4221 return nread;
4224 static bool fruit_must_handle_aio_stream(struct fio *fio)
4226 if (fio == NULL) {
4227 return false;
4230 if ((fio->type == ADOUBLE_META) &&
4231 (fio->config->meta == FRUIT_META_NETATALK))
4233 return true;
4236 if ((fio->type == ADOUBLE_RSRC) &&
4237 (fio->config->rsrc == FRUIT_RSRC_ADFILE))
4239 return true;
4242 return false;
4245 struct fruit_pread_state {
4246 ssize_t nread;
4247 struct vfs_aio_state vfs_aio_state;
4250 static void fruit_pread_done(struct tevent_req *subreq);
4252 static struct tevent_req *fruit_pread_send(
4253 struct vfs_handle_struct *handle,
4254 TALLOC_CTX *mem_ctx,
4255 struct tevent_context *ev,
4256 struct files_struct *fsp,
4257 void *data,
4258 size_t n, off_t offset)
4260 struct tevent_req *req = NULL;
4261 struct tevent_req *subreq = NULL;
4262 struct fruit_pread_state *state = NULL;
4263 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4265 req = tevent_req_create(mem_ctx, &state,
4266 struct fruit_pread_state);
4267 if (req == NULL) {
4268 return NULL;
4271 if (fruit_must_handle_aio_stream(fio)) {
4272 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
4273 if (state->nread != n) {
4274 if (state->nread != -1) {
4275 errno = EIO;
4277 tevent_req_error(req, errno);
4278 return tevent_req_post(req, ev);
4280 tevent_req_done(req);
4281 return tevent_req_post(req, ev);
4284 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
4285 data, n, offset);
4286 if (tevent_req_nomem(req, subreq)) {
4287 return tevent_req_post(req, ev);
4289 tevent_req_set_callback(subreq, fruit_pread_done, req);
4290 return req;
4293 static void fruit_pread_done(struct tevent_req *subreq)
4295 struct tevent_req *req = tevent_req_callback_data(
4296 subreq, struct tevent_req);
4297 struct fruit_pread_state *state = tevent_req_data(
4298 req, struct fruit_pread_state);
4300 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
4301 TALLOC_FREE(subreq);
4303 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4304 return;
4306 tevent_req_done(req);
4309 static ssize_t fruit_pread_recv(struct tevent_req *req,
4310 struct vfs_aio_state *vfs_aio_state)
4312 struct fruit_pread_state *state = tevent_req_data(
4313 req, struct fruit_pread_state);
4315 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4316 return -1;
4319 *vfs_aio_state = state->vfs_aio_state;
4320 return state->nread;
4323 static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
4324 files_struct *fsp, const void *data,
4325 size_t n, off_t offset)
4327 AfpInfo *ai = NULL;
4328 size_t nwritten;
4329 bool ok;
4331 ai = afpinfo_unpack(talloc_tos(), data);
4332 if (ai == NULL) {
4333 return -1;
4336 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4337 if (nwritten != n) {
4338 return -1;
4341 if (!ai_empty_finderinfo(ai)) {
4342 return n;
4345 ok = set_delete_on_close(
4346 fsp,
4347 true,
4348 handle->conn->session_info->security_token,
4349 handle->conn->session_info->unix_token);
4350 if (!ok) {
4351 DBG_ERR("set_delete_on_close on [%s] failed\n",
4352 fsp_str_dbg(fsp));
4353 return -1;
4356 return n;
4359 static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
4360 files_struct *fsp, const void *data,
4361 size_t n, off_t offset)
4363 struct adouble *ad = NULL;
4364 AfpInfo *ai = NULL;
4365 char *p = NULL;
4366 int ret;
4367 bool ok;
4369 ai = afpinfo_unpack(talloc_tos(), data);
4370 if (ai == NULL) {
4371 return -1;
4374 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
4375 if (ad == NULL) {
4376 ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
4377 if (ad == NULL) {
4378 return -1;
4381 p = ad_get_entry(ad, ADEID_FINDERI);
4382 if (p == NULL) {
4383 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
4384 TALLOC_FREE(ad);
4385 return -1;
4388 memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
4390 ret = ad_fset(ad, fsp);
4391 if (ret != 0) {
4392 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4393 TALLOC_FREE(ad);
4394 return -1;
4397 TALLOC_FREE(ad);
4399 if (!ai_empty_finderinfo(ai)) {
4400 return n;
4403 ok = set_delete_on_close(
4404 fsp,
4405 true,
4406 handle->conn->session_info->security_token,
4407 handle->conn->session_info->unix_token);
4408 if (!ok) {
4409 DBG_ERR("set_delete_on_close on [%s] failed\n",
4410 fsp_str_dbg(fsp));
4411 return -1;
4414 return n;
4417 static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
4418 files_struct *fsp, const void *data,
4419 size_t n, off_t offset)
4421 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4422 ssize_t nwritten;
4425 * Writing an all 0 blob to the metadata stream
4426 * results in the stream being removed on a macOS
4427 * server. This ensures we behave the same and it
4428 * verified by the "delete AFP_AfpInfo by writing all
4429 * 0" test.
4431 if (n != AFP_INFO_SIZE || offset != 0) {
4432 DBG_ERR("unexpected offset=%jd or size=%jd\n",
4433 (intmax_t)offset, (intmax_t)n);
4434 return -1;
4437 if (fio == NULL) {
4438 DBG_ERR("Failed to fetch fsp extension");
4439 return -1;
4442 switch (fio->config->meta) {
4443 case FRUIT_META_STREAM:
4444 nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
4445 n, offset);
4446 break;
4448 case FRUIT_META_NETATALK:
4449 nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
4450 n, offset);
4451 break;
4453 default:
4454 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4455 return -1;
4458 return nwritten;
4461 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
4462 files_struct *fsp, const void *data,
4463 size_t n, off_t offset)
4465 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4468 static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
4469 files_struct *fsp, const void *data,
4470 size_t n, off_t offset)
4472 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4475 static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
4476 files_struct *fsp, const void *data,
4477 size_t n, off_t offset)
4479 struct adouble *ad = NULL;
4480 ssize_t nwritten;
4481 int ret;
4483 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
4484 if (ad == NULL) {
4485 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
4486 return -1;
4489 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
4490 offset + ad_getentryoff(ad, ADEID_RFORK));
4491 if (nwritten != n) {
4492 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
4493 fsp_str_dbg(fsp), nwritten, n);
4494 TALLOC_FREE(ad);
4495 return -1;
4498 if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
4499 ad_setentrylen(ad, ADEID_RFORK, n + offset);
4500 ret = ad_fset(ad, fsp);
4501 if (ret != 0) {
4502 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
4503 TALLOC_FREE(ad);
4504 return -1;
4508 TALLOC_FREE(ad);
4509 return n;
4512 static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
4513 files_struct *fsp, const void *data,
4514 size_t n, off_t offset)
4516 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4517 ssize_t nwritten;
4519 if (fio == NULL) {
4520 DBG_ERR("Failed to fetch fsp extension");
4521 return -1;
4524 switch (fio->config->rsrc) {
4525 case FRUIT_RSRC_STREAM:
4526 nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
4527 break;
4529 case FRUIT_RSRC_ADFILE:
4530 nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
4531 break;
4533 case FRUIT_RSRC_XATTR:
4534 nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
4535 break;
4537 default:
4538 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
4539 return -1;
4542 return nwritten;
4545 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
4546 files_struct *fsp, const void *data,
4547 size_t n, off_t offset)
4549 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4550 ssize_t nwritten;
4552 DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
4553 fsp_str_dbg(fsp), (intmax_t)offset, n);
4555 if (fio == NULL) {
4556 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
4559 if (fio->type == ADOUBLE_META) {
4560 nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
4561 } else {
4562 nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
4565 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
4566 return nwritten;
4569 struct fruit_pwrite_state {
4570 ssize_t nwritten;
4571 struct vfs_aio_state vfs_aio_state;
4574 static void fruit_pwrite_done(struct tevent_req *subreq);
4576 static struct tevent_req *fruit_pwrite_send(
4577 struct vfs_handle_struct *handle,
4578 TALLOC_CTX *mem_ctx,
4579 struct tevent_context *ev,
4580 struct files_struct *fsp,
4581 const void *data,
4582 size_t n, off_t offset)
4584 struct tevent_req *req = NULL;
4585 struct tevent_req *subreq = NULL;
4586 struct fruit_pwrite_state *state = NULL;
4587 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
4589 req = tevent_req_create(mem_ctx, &state,
4590 struct fruit_pwrite_state);
4591 if (req == NULL) {
4592 return NULL;
4595 if (fruit_must_handle_aio_stream(fio)) {
4596 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
4597 if (state->nwritten != n) {
4598 if (state->nwritten != -1) {
4599 errno = EIO;
4601 tevent_req_error(req, errno);
4602 return tevent_req_post(req, ev);
4604 tevent_req_done(req);
4605 return tevent_req_post(req, ev);
4608 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
4609 data, n, offset);
4610 if (tevent_req_nomem(req, subreq)) {
4611 return tevent_req_post(req, ev);
4613 tevent_req_set_callback(subreq, fruit_pwrite_done, req);
4614 return req;
4617 static void fruit_pwrite_done(struct tevent_req *subreq)
4619 struct tevent_req *req = tevent_req_callback_data(
4620 subreq, struct tevent_req);
4621 struct fruit_pwrite_state *state = tevent_req_data(
4622 req, struct fruit_pwrite_state);
4624 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
4625 TALLOC_FREE(subreq);
4627 if (tevent_req_error(req, state->vfs_aio_state.error)) {
4628 return;
4630 tevent_req_done(req);
4633 static ssize_t fruit_pwrite_recv(struct tevent_req *req,
4634 struct vfs_aio_state *vfs_aio_state)
4636 struct fruit_pwrite_state *state = tevent_req_data(
4637 req, struct fruit_pwrite_state);
4639 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
4640 return -1;
4643 *vfs_aio_state = state->vfs_aio_state;
4644 return state->nwritten;
4648 * Helper to stat/lstat the base file of an smb_fname.
4650 static int fruit_stat_base(vfs_handle_struct *handle,
4651 struct smb_filename *smb_fname,
4652 bool follow_links)
4654 char *tmp_stream_name;
4655 int rc;
4657 tmp_stream_name = smb_fname->stream_name;
4658 smb_fname->stream_name = NULL;
4659 if (follow_links) {
4660 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4661 } else {
4662 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4664 smb_fname->stream_name = tmp_stream_name;
4665 return rc;
4668 static int fruit_stat_meta_stream(vfs_handle_struct *handle,
4669 struct smb_filename *smb_fname,
4670 bool follow_links)
4672 int ret;
4674 if (follow_links) {
4675 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4676 } else {
4677 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4680 return ret;
4683 static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
4684 struct smb_filename *smb_fname,
4685 bool follow_links)
4687 struct adouble *ad = NULL;
4689 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
4690 if (ad == NULL) {
4691 DBG_INFO("fruit_stat_meta %s: %s\n",
4692 smb_fname_str_dbg(smb_fname), strerror(errno));
4693 errno = ENOENT;
4694 return -1;
4696 TALLOC_FREE(ad);
4698 /* Populate the stat struct with info from the base file. */
4699 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
4700 return -1;
4702 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
4703 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4704 smb_fname->stream_name);
4705 return 0;
4708 static int fruit_stat_meta(vfs_handle_struct *handle,
4709 struct smb_filename *smb_fname,
4710 bool follow_links)
4712 struct fruit_config_data *config = NULL;
4713 int ret;
4715 SMB_VFS_HANDLE_GET_DATA(handle, config,
4716 struct fruit_config_data, return -1);
4718 switch (config->meta) {
4719 case FRUIT_META_STREAM:
4720 ret = fruit_stat_meta_stream(handle, smb_fname, follow_links);
4721 break;
4723 case FRUIT_META_NETATALK:
4724 ret = fruit_stat_meta_netatalk(handle, smb_fname, follow_links);
4725 break;
4727 default:
4728 DBG_ERR("Unexpected meta config [%d]\n", config->meta);
4729 return -1;
4732 return ret;
4735 static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
4736 struct smb_filename *smb_fname,
4737 bool follow_links)
4739 struct adouble *ad = NULL;
4740 int ret;
4742 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
4743 if (ad == NULL) {
4744 errno = ENOENT;
4745 return -1;
4748 /* Populate the stat struct with info from the base file. */
4749 ret = fruit_stat_base(handle, smb_fname, follow_links);
4750 if (ret != 0) {
4751 TALLOC_FREE(ad);
4752 return -1;
4755 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
4756 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4757 smb_fname->stream_name);
4758 TALLOC_FREE(ad);
4759 return 0;
4762 static int fruit_stat_rsrc_stream(vfs_handle_struct *handle,
4763 struct smb_filename *smb_fname,
4764 bool follow_links)
4766 int ret;
4768 if (follow_links) {
4769 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
4770 } else {
4771 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4774 return ret;
4777 static int fruit_stat_rsrc_xattr(vfs_handle_struct *handle,
4778 struct smb_filename *smb_fname,
4779 bool follow_links)
4781 #ifdef HAVE_ATTROPEN
4782 int ret;
4783 int fd = -1;
4785 /* Populate the stat struct with info from the base file. */
4786 ret = fruit_stat_base(handle, smb_fname, follow_links);
4787 if (ret != 0) {
4788 return -1;
4791 fd = attropen(smb_fname->base_name,
4792 AFPRESOURCE_EA_NETATALK,
4793 O_RDONLY);
4794 if (fd == -1) {
4795 return 0;
4798 ret = sys_fstat(fd, &smb_fname->st, false);
4799 if (ret != 0) {
4800 close(fd);
4801 DBG_ERR("fstat [%s:%s] failed\n", smb_fname->base_name,
4802 AFPRESOURCE_EA_NETATALK);
4803 return -1;
4805 close(fd);
4806 fd = -1;
4808 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
4809 smb_fname->stream_name);
4811 return ret;
4813 #else
4814 errno = ENOSYS;
4815 return -1;
4816 #endif
4819 static int fruit_stat_rsrc(vfs_handle_struct *handle,
4820 struct smb_filename *smb_fname,
4821 bool follow_links)
4823 struct fruit_config_data *config = NULL;
4824 int ret;
4826 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
4828 SMB_VFS_HANDLE_GET_DATA(handle, config,
4829 struct fruit_config_data, return -1);
4831 switch (config->rsrc) {
4832 case FRUIT_RSRC_STREAM:
4833 ret = fruit_stat_rsrc_stream(handle, smb_fname, follow_links);
4834 break;
4836 case FRUIT_RSRC_XATTR:
4837 ret = fruit_stat_rsrc_xattr(handle, smb_fname, follow_links);
4838 break;
4840 case FRUIT_RSRC_ADFILE:
4841 ret = fruit_stat_rsrc_netatalk(handle, smb_fname, follow_links);
4842 break;
4844 default:
4845 DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
4846 return -1;
4849 return ret;
4852 static int fruit_stat(vfs_handle_struct *handle,
4853 struct smb_filename *smb_fname)
4855 int rc = -1;
4857 DEBUG(10, ("fruit_stat called for %s\n",
4858 smb_fname_str_dbg(smb_fname)));
4860 if (!is_ntfs_stream_smb_fname(smb_fname)
4861 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4862 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
4863 if (rc == 0) {
4864 update_btime(handle, smb_fname);
4866 return rc;
4870 * Note if lp_posix_paths() is true, we can never
4871 * get here as is_ntfs_stream_smb_fname() is
4872 * always false. So we never need worry about
4873 * not following links here.
4876 if (is_afpinfo_stream(smb_fname)) {
4877 rc = fruit_stat_meta(handle, smb_fname, true);
4878 } else if (is_afpresource_stream(smb_fname)) {
4879 rc = fruit_stat_rsrc(handle, smb_fname, true);
4880 } else {
4881 return SMB_VFS_NEXT_STAT(handle, smb_fname);
4884 if (rc == 0) {
4885 update_btime(handle, smb_fname);
4886 smb_fname->st.st_ex_mode &= ~S_IFMT;
4887 smb_fname->st.st_ex_mode |= S_IFREG;
4888 smb_fname->st.st_ex_blocks =
4889 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4891 return rc;
4894 static int fruit_lstat(vfs_handle_struct *handle,
4895 struct smb_filename *smb_fname)
4897 int rc = -1;
4899 DEBUG(10, ("fruit_lstat called for %s\n",
4900 smb_fname_str_dbg(smb_fname)));
4902 if (!is_ntfs_stream_smb_fname(smb_fname)
4903 || is_ntfs_default_stream_smb_fname(smb_fname)) {
4904 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4905 if (rc == 0) {
4906 update_btime(handle, smb_fname);
4908 return rc;
4911 if (is_afpinfo_stream(smb_fname)) {
4912 rc = fruit_stat_meta(handle, smb_fname, false);
4913 } else if (is_afpresource_stream(smb_fname)) {
4914 rc = fruit_stat_rsrc(handle, smb_fname, false);
4915 } else {
4916 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
4919 if (rc == 0) {
4920 update_btime(handle, smb_fname);
4921 smb_fname->st.st_ex_mode &= ~S_IFMT;
4922 smb_fname->st.st_ex_mode |= S_IFREG;
4923 smb_fname->st.st_ex_blocks =
4924 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
4926 return rc;
4929 static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
4930 files_struct *fsp,
4931 SMB_STRUCT_STAT *sbuf)
4933 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4936 static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
4937 files_struct *fsp,
4938 SMB_STRUCT_STAT *sbuf)
4940 int ret;
4942 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
4943 if (ret != 0) {
4944 return -1;
4947 *sbuf = fsp->base_fsp->fsp_name->st;
4948 sbuf->st_ex_size = AFP_INFO_SIZE;
4949 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
4951 return 0;
4954 static int fruit_fstat_meta(vfs_handle_struct *handle,
4955 files_struct *fsp,
4956 SMB_STRUCT_STAT *sbuf,
4957 struct fio *fio)
4959 int ret;
4961 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
4963 switch (fio->config->meta) {
4964 case FRUIT_META_STREAM:
4965 ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
4966 break;
4968 case FRUIT_META_NETATALK:
4969 ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
4970 break;
4972 default:
4973 DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
4974 return -1;
4977 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
4978 return ret;
4981 static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
4982 files_struct *fsp,
4983 SMB_STRUCT_STAT *sbuf)
4985 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4988 static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
4989 files_struct *fsp,
4990 SMB_STRUCT_STAT *sbuf)
4992 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
4995 static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
4996 files_struct *fsp,
4997 SMB_STRUCT_STAT *sbuf)
4999 struct adouble *ad = NULL;
5000 int ret;
5002 /* Populate the stat struct with info from the base file. */
5003 ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
5004 if (ret == -1) {
5005 return -1;
5008 ad = ad_get(talloc_tos(), handle,
5009 fsp->base_fsp->fsp_name,
5010 ADOUBLE_RSRC);
5011 if (ad == NULL) {
5012 DBG_ERR("ad_get [%s] failed [%s]\n",
5013 fsp_str_dbg(fsp), strerror(errno));
5014 return -1;
5017 *sbuf = fsp->base_fsp->fsp_name->st;
5018 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
5019 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
5021 TALLOC_FREE(ad);
5022 return 0;
5025 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
5026 SMB_STRUCT_STAT *sbuf, struct fio *fio)
5028 int ret;
5030 switch (fio->config->rsrc) {
5031 case FRUIT_RSRC_STREAM:
5032 ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
5033 break;
5035 case FRUIT_RSRC_ADFILE:
5036 ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
5037 break;
5039 case FRUIT_RSRC_XATTR:
5040 ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
5041 break;
5043 default:
5044 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5045 return -1;
5048 return ret;
5051 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
5052 SMB_STRUCT_STAT *sbuf)
5054 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5055 int rc;
5057 if (fio == NULL) {
5058 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
5061 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
5063 if (fio->type == ADOUBLE_META) {
5064 rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
5065 } else {
5066 rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
5069 if (rc == 0) {
5070 sbuf->st_ex_mode &= ~S_IFMT;
5071 sbuf->st_ex_mode |= S_IFREG;
5072 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
5075 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
5076 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
5077 return rc;
5080 static NTSTATUS delete_invalid_meta_stream(
5081 vfs_handle_struct *handle,
5082 const struct smb_filename *smb_fname,
5083 TALLOC_CTX *mem_ctx,
5084 unsigned int *pnum_streams,
5085 struct stream_struct **pstreams)
5087 struct smb_filename *sname = NULL;
5088 int ret;
5089 bool ok;
5091 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
5092 if (!ok) {
5093 return NT_STATUS_INTERNAL_ERROR;
5096 sname = synthetic_smb_fname(talloc_tos(),
5097 smb_fname->base_name,
5098 AFPINFO_STREAM_NAME,
5099 NULL, 0);
5100 if (sname == NULL) {
5101 return NT_STATUS_NO_MEMORY;
5104 ret = SMB_VFS_NEXT_UNLINK(handle, sname);
5105 TALLOC_FREE(sname);
5106 if (ret != 0) {
5107 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
5108 return map_nt_error_from_unix(errno);
5111 return NT_STATUS_OK;
5114 static NTSTATUS fruit_streaminfo_meta_stream(
5115 vfs_handle_struct *handle,
5116 struct files_struct *fsp,
5117 const struct smb_filename *smb_fname,
5118 TALLOC_CTX *mem_ctx,
5119 unsigned int *pnum_streams,
5120 struct stream_struct **pstreams)
5122 struct stream_struct *stream = *pstreams;
5123 unsigned int num_streams = *pnum_streams;
5124 struct smb_filename *sname = NULL;
5125 char *full_name = NULL;
5126 uint32_t name_hash;
5127 struct share_mode_lock *lck = NULL;
5128 struct file_id id = {0};
5129 bool delete_on_close_set;
5130 int i;
5131 int ret;
5132 NTSTATUS status;
5133 bool ok;
5135 for (i = 0; i < num_streams; i++) {
5136 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5137 break;
5141 if (i == num_streams) {
5142 return NT_STATUS_OK;
5145 if (stream[i].size != AFP_INFO_SIZE) {
5146 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
5147 (intmax_t)stream[i].size, smb_fname_str_dbg(smb_fname));
5149 return delete_invalid_meta_stream(handle, smb_fname, mem_ctx,
5150 pnum_streams, pstreams);
5154 * Now check if there's a delete-on-close pending on the stream. If so,
5155 * hide the stream. This behaviour was verified against a macOS 10.12
5156 * SMB server.
5159 sname = synthetic_smb_fname(talloc_tos(),
5160 smb_fname->base_name,
5161 AFPINFO_STREAM_NAME,
5162 NULL, 0);
5163 if (sname == NULL) {
5164 status = NT_STATUS_NO_MEMORY;
5165 goto out;
5168 ret = SMB_VFS_NEXT_STAT(handle, sname);
5169 if (ret != 0) {
5170 status = map_nt_error_from_unix(errno);
5171 goto out;
5174 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
5176 lck = get_existing_share_mode_lock(talloc_tos(), id);
5177 if (lck == NULL) {
5178 status = NT_STATUS_OK;
5179 goto out;
5182 full_name = talloc_asprintf(talloc_tos(),
5183 "%s%s",
5184 sname->base_name,
5185 AFPINFO_STREAM);
5186 if (full_name == NULL) {
5187 status = NT_STATUS_NO_MEMORY;
5188 goto out;
5191 status = file_name_hash(handle->conn, full_name, &name_hash);
5192 if (!NT_STATUS_IS_OK(status)) {
5193 goto out;
5196 delete_on_close_set = is_delete_on_close_set(lck, name_hash);
5197 if (delete_on_close_set) {
5198 ok = del_fruit_stream(mem_ctx,
5199 pnum_streams,
5200 pstreams,
5201 AFPINFO_STREAM);
5202 if (!ok) {
5203 status = NT_STATUS_INTERNAL_ERROR;
5204 goto out;
5208 status = NT_STATUS_OK;
5210 out:
5211 TALLOC_FREE(sname);
5212 TALLOC_FREE(lck);
5213 TALLOC_FREE(full_name);
5214 return status;
5217 static NTSTATUS fruit_streaminfo_meta_netatalk(
5218 vfs_handle_struct *handle,
5219 struct files_struct *fsp,
5220 const struct smb_filename *smb_fname,
5221 TALLOC_CTX *mem_ctx,
5222 unsigned int *pnum_streams,
5223 struct stream_struct **pstreams)
5225 struct stream_struct *stream = *pstreams;
5226 unsigned int num_streams = *pnum_streams;
5227 struct adouble *ad = NULL;
5228 bool is_fi_empty;
5229 int i;
5230 bool ok;
5232 /* Remove the Netatalk xattr from the list */
5233 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5234 ":" NETATALK_META_XATTR ":$DATA");
5235 if (!ok) {
5236 return NT_STATUS_NO_MEMORY;
5240 * Check if there's a AFPINFO_STREAM from the VFS streams
5241 * backend and if yes, remove it from the list
5243 for (i = 0; i < num_streams; i++) {
5244 if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
5245 break;
5249 if (i < num_streams) {
5250 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
5251 smb_fname_str_dbg(smb_fname));
5253 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5254 AFPINFO_STREAM);
5255 if (!ok) {
5256 return NT_STATUS_INTERNAL_ERROR;
5260 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5261 if (ad == NULL) {
5262 return NT_STATUS_OK;
5265 is_fi_empty = ad_empty_finderinfo(ad);
5266 TALLOC_FREE(ad);
5268 if (is_fi_empty) {
5269 return NT_STATUS_OK;
5272 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5273 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
5274 smb_roundup(handle->conn, AFP_INFO_SIZE));
5275 if (!ok) {
5276 return NT_STATUS_NO_MEMORY;
5279 return NT_STATUS_OK;
5282 static NTSTATUS fruit_streaminfo_meta(vfs_handle_struct *handle,
5283 struct files_struct *fsp,
5284 const struct smb_filename *smb_fname,
5285 TALLOC_CTX *mem_ctx,
5286 unsigned int *pnum_streams,
5287 struct stream_struct **pstreams)
5289 struct fruit_config_data *config = NULL;
5290 NTSTATUS status;
5292 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5293 return NT_STATUS_INTERNAL_ERROR);
5295 switch (config->meta) {
5296 case FRUIT_META_NETATALK:
5297 status = fruit_streaminfo_meta_netatalk(handle, fsp, smb_fname,
5298 mem_ctx, pnum_streams,
5299 pstreams);
5300 break;
5302 case FRUIT_META_STREAM:
5303 status = fruit_streaminfo_meta_stream(handle, fsp, smb_fname,
5304 mem_ctx, pnum_streams,
5305 pstreams);
5306 break;
5308 default:
5309 return NT_STATUS_INTERNAL_ERROR;
5312 return status;
5315 static NTSTATUS fruit_streaminfo_rsrc_stream(
5316 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 bool ok;
5325 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5326 if (!ok) {
5327 DBG_ERR("Filtering resource stream failed\n");
5328 return NT_STATUS_INTERNAL_ERROR;
5330 return NT_STATUS_OK;
5333 static NTSTATUS fruit_streaminfo_rsrc_xattr(
5334 vfs_handle_struct *handle,
5335 struct files_struct *fsp,
5336 const struct smb_filename *smb_fname,
5337 TALLOC_CTX *mem_ctx,
5338 unsigned int *pnum_streams,
5339 struct stream_struct **pstreams)
5341 bool ok;
5343 ok = filter_empty_rsrc_stream(pnum_streams, pstreams);
5344 if (!ok) {
5345 DBG_ERR("Filtering resource stream failed\n");
5346 return NT_STATUS_INTERNAL_ERROR;
5348 return NT_STATUS_OK;
5351 static NTSTATUS fruit_streaminfo_rsrc_adouble(
5352 vfs_handle_struct *handle,
5353 struct files_struct *fsp,
5354 const struct smb_filename *smb_fname,
5355 TALLOC_CTX *mem_ctx,
5356 unsigned int *pnum_streams,
5357 struct stream_struct **pstreams)
5359 struct stream_struct *stream = *pstreams;
5360 unsigned int num_streams = *pnum_streams;
5361 struct adouble *ad = NULL;
5362 bool ok;
5363 size_t rlen;
5364 int i;
5367 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
5368 * and if yes, remove it from the list
5370 for (i = 0; i < num_streams; i++) {
5371 if (strequal_m(stream[i].name, AFPRESOURCE_STREAM)) {
5372 break;
5376 if (i < num_streams) {
5377 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
5378 smb_fname_str_dbg(smb_fname));
5380 ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
5381 AFPRESOURCE_STREAM);
5382 if (!ok) {
5383 return NT_STATUS_INTERNAL_ERROR;
5387 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
5388 if (ad == NULL) {
5389 return NT_STATUS_OK;
5392 rlen = ad_getentrylen(ad, ADEID_RFORK);
5393 TALLOC_FREE(ad);
5395 if (rlen == 0) {
5396 return NT_STATUS_OK;
5399 ok = add_fruit_stream(mem_ctx, pnum_streams, pstreams,
5400 AFPRESOURCE_STREAM_NAME, rlen,
5401 smb_roundup(handle->conn, rlen));
5402 if (!ok) {
5403 return NT_STATUS_NO_MEMORY;
5406 return NT_STATUS_OK;
5409 static NTSTATUS fruit_streaminfo_rsrc(vfs_handle_struct *handle,
5410 struct files_struct *fsp,
5411 const struct smb_filename *smb_fname,
5412 TALLOC_CTX *mem_ctx,
5413 unsigned int *pnum_streams,
5414 struct stream_struct **pstreams)
5416 struct fruit_config_data *config = NULL;
5417 NTSTATUS status;
5419 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5420 return NT_STATUS_INTERNAL_ERROR);
5422 switch (config->rsrc) {
5423 case FRUIT_RSRC_STREAM:
5424 status = fruit_streaminfo_rsrc_stream(handle, fsp, smb_fname,
5425 mem_ctx, pnum_streams,
5426 pstreams);
5427 break;
5429 case FRUIT_RSRC_XATTR:
5430 status = fruit_streaminfo_rsrc_xattr(handle, fsp, smb_fname,
5431 mem_ctx, pnum_streams,
5432 pstreams);
5433 break;
5435 case FRUIT_RSRC_ADFILE:
5436 status = fruit_streaminfo_rsrc_adouble(handle, fsp, smb_fname,
5437 mem_ctx, pnum_streams,
5438 pstreams);
5439 break;
5441 default:
5442 return NT_STATUS_INTERNAL_ERROR;
5445 return status;
5448 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
5449 struct files_struct *fsp,
5450 const struct smb_filename *smb_fname,
5451 TALLOC_CTX *mem_ctx,
5452 unsigned int *pnum_streams,
5453 struct stream_struct **pstreams)
5455 struct fruit_config_data *config = NULL;
5456 NTSTATUS status;
5458 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5459 return NT_STATUS_UNSUCCESSFUL);
5461 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
5463 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
5464 pnum_streams, pstreams);
5465 if (!NT_STATUS_IS_OK(status)) {
5466 return status;
5469 status = fruit_streaminfo_meta(handle, fsp, smb_fname,
5470 mem_ctx, pnum_streams, pstreams);
5471 if (!NT_STATUS_IS_OK(status)) {
5472 return status;
5475 status = fruit_streaminfo_rsrc(handle, fsp, smb_fname,
5476 mem_ctx, pnum_streams, pstreams);
5477 if (!NT_STATUS_IS_OK(status)) {
5478 return status;
5481 return NT_STATUS_OK;
5484 static int fruit_ntimes(vfs_handle_struct *handle,
5485 const struct smb_filename *smb_fname,
5486 struct smb_file_time *ft)
5488 int rc = 0;
5489 struct adouble *ad = NULL;
5490 struct fruit_config_data *config = NULL;
5492 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5493 return -1);
5495 if ((config->meta != FRUIT_META_NETATALK) ||
5496 null_timespec(ft->create_time))
5498 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5501 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
5502 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5504 ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
5505 if (ad == NULL) {
5506 goto exit;
5509 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
5510 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
5512 rc = ad_set(ad, smb_fname);
5514 exit:
5516 TALLOC_FREE(ad);
5517 if (rc != 0) {
5518 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
5519 return -1;
5521 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
5524 static int fruit_fallocate(struct vfs_handle_struct *handle,
5525 struct files_struct *fsp,
5526 uint32_t mode,
5527 off_t offset,
5528 off_t len)
5530 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5532 if (fio == NULL) {
5533 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
5536 /* Let the pwrite code path handle it. */
5537 errno = ENOSYS;
5538 return -1;
5541 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct *handle,
5542 struct files_struct *fsp,
5543 off_t offset)
5545 if (offset == 0) {
5546 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
5549 #ifdef HAVE_ATTROPEN
5550 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5551 #endif
5552 return 0;
5555 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
5556 struct files_struct *fsp,
5557 off_t offset)
5559 int rc;
5560 struct adouble *ad = NULL;
5561 off_t ad_off;
5563 ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
5564 if (ad == NULL) {
5565 DBG_DEBUG("ad_get [%s] failed [%s]\n",
5566 fsp_str_dbg(fsp), strerror(errno));
5567 return -1;
5570 ad_off = ad_getentryoff(ad, ADEID_RFORK);
5572 rc = ftruncate(fsp->fh->fd, offset + ad_off);
5573 if (rc != 0) {
5574 TALLOC_FREE(ad);
5575 return -1;
5578 ad_setentrylen(ad, ADEID_RFORK, offset);
5580 rc = ad_fset(ad, fsp);
5581 if (rc != 0) {
5582 DBG_ERR("ad_fset [%s] failed [%s]\n",
5583 fsp_str_dbg(fsp), strerror(errno));
5584 TALLOC_FREE(ad);
5585 return -1;
5588 TALLOC_FREE(ad);
5589 return 0;
5592 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct *handle,
5593 struct files_struct *fsp,
5594 off_t offset)
5596 if (offset == 0) {
5597 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5600 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5603 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
5604 struct files_struct *fsp,
5605 off_t offset)
5607 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5608 int ret;
5610 if (fio == NULL) {
5611 DBG_ERR("Failed to fetch fsp extension");
5612 return -1;
5615 switch (fio->config->rsrc) {
5616 case FRUIT_RSRC_XATTR:
5617 ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
5618 break;
5620 case FRUIT_RSRC_ADFILE:
5621 ret = fruit_ftruncate_rsrc_adouble(handle, fsp, offset);
5622 break;
5624 case FRUIT_RSRC_STREAM:
5625 ret = fruit_ftruncate_rsrc_stream(handle, fsp, offset);
5626 break;
5628 default:
5629 DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
5630 return -1;
5634 return ret;
5637 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
5638 struct files_struct *fsp,
5639 off_t offset)
5641 if (offset > 60) {
5642 DBG_WARNING("ftruncate %s to %jd",
5643 fsp_str_dbg(fsp), (intmax_t)offset);
5644 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
5645 errno = EOVERFLOW;
5646 return -1;
5649 /* OS X returns success but does nothing */
5650 DBG_INFO("ignoring ftruncate %s to %jd\n",
5651 fsp_str_dbg(fsp), (intmax_t)offset);
5652 return 0;
5655 static int fruit_ftruncate(struct vfs_handle_struct *handle,
5656 struct files_struct *fsp,
5657 off_t offset)
5659 struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
5660 int ret;
5662 DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
5663 (intmax_t)offset);
5665 if (fio == NULL) {
5666 if (offset == 0 &&
5667 global_fruit_config.nego_aapl &&
5668 is_ntfs_stream_smb_fname(fsp->fsp_name) &&
5669 !is_ntfs_default_stream_smb_fname(fsp->fsp_name))
5671 return SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
5673 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
5676 if (fio->type == ADOUBLE_META) {
5677 ret = fruit_ftruncate_meta(handle, fsp, offset);
5678 } else {
5679 ret = fruit_ftruncate_rsrc(handle, fsp, offset);
5682 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
5683 return ret;
5686 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
5687 struct smb_request *req,
5688 uint16_t root_dir_fid,
5689 struct smb_filename *smb_fname,
5690 uint32_t access_mask,
5691 uint32_t share_access,
5692 uint32_t create_disposition,
5693 uint32_t create_options,
5694 uint32_t file_attributes,
5695 uint32_t oplock_request,
5696 struct smb2_lease *lease,
5697 uint64_t allocation_size,
5698 uint32_t private_flags,
5699 struct security_descriptor *sd,
5700 struct ea_list *ea_list,
5701 files_struct **result,
5702 int *pinfo,
5703 const struct smb2_create_blobs *in_context_blobs,
5704 struct smb2_create_blobs *out_context_blobs)
5706 NTSTATUS status;
5707 struct fruit_config_data *config = NULL;
5708 files_struct *fsp = NULL;
5710 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
5711 if (!NT_STATUS_IS_OK(status)) {
5712 goto fail;
5715 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
5716 return NT_STATUS_UNSUCCESSFUL);
5718 status = SMB_VFS_NEXT_CREATE_FILE(
5719 handle, req, root_dir_fid, smb_fname,
5720 access_mask, share_access,
5721 create_disposition, create_options,
5722 file_attributes, oplock_request,
5723 lease,
5724 allocation_size, private_flags,
5725 sd, ea_list, result,
5726 pinfo, in_context_blobs, out_context_blobs);
5727 if (!NT_STATUS_IS_OK(status)) {
5728 return status;
5731 fsp = *result;
5733 if (global_fruit_config.nego_aapl) {
5734 if (config->posix_rename && fsp->is_directory) {
5736 * Enable POSIX directory rename behaviour
5738 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
5743 * If this is a plain open for existing files, opening an 0
5744 * byte size resource fork MUST fail with
5745 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
5747 * Cf the vfs_fruit torture tests in test_rfork_create().
5749 if (is_afpresource_stream(fsp->fsp_name) &&
5750 create_disposition == FILE_OPEN)
5752 if (fsp->fsp_name->st.st_ex_size == 0) {
5753 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5754 goto fail;
5758 if (is_ntfs_stream_smb_fname(smb_fname)
5759 || fsp->is_directory) {
5760 return status;
5763 if (config->locking == FRUIT_LOCKING_NETATALK) {
5764 status = fruit_check_access(
5765 handle, *result,
5766 access_mask,
5767 map_share_mode_to_deny_mode(share_access, 0));
5768 if (!NT_STATUS_IS_OK(status)) {
5769 goto fail;
5773 return status;
5775 fail:
5776 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
5778 if (fsp) {
5779 close_file(req, fsp, ERROR_CLOSE);
5780 *result = fsp = NULL;
5783 return status;
5786 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
5787 const struct smb_filename *fname,
5788 TALLOC_CTX *mem_ctx,
5789 struct readdir_attr_data **pattr_data)
5791 struct fruit_config_data *config = NULL;
5792 struct readdir_attr_data *attr_data;
5793 NTSTATUS status;
5795 SMB_VFS_HANDLE_GET_DATA(handle, config,
5796 struct fruit_config_data,
5797 return NT_STATUS_UNSUCCESSFUL);
5799 if (!global_fruit_config.nego_aapl) {
5800 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
5803 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
5805 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
5806 if (*pattr_data == NULL) {
5807 return NT_STATUS_UNSUCCESSFUL;
5809 attr_data = *pattr_data;
5810 attr_data->type = RDATTR_AAPL;
5813 * Mac metadata: compressed FinderInfo, resource fork length
5814 * and creation date
5816 status = readdir_attr_macmeta(handle, fname, attr_data);
5817 if (!NT_STATUS_IS_OK(status)) {
5819 * Error handling is tricky: if we return failure from
5820 * this function, the corresponding directory entry
5821 * will to be passed to the client, so we really just
5822 * want to error out on fatal errors.
5824 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5825 goto fail;
5830 * UNIX mode
5832 if (config->unix_info_enabled) {
5833 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
5837 * max_access
5839 if (!config->readdir_attr_max_access) {
5840 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
5841 } else {
5842 status = smbd_calculate_access_mask(
5843 handle->conn,
5844 fname,
5845 false,
5846 SEC_FLAG_MAXIMUM_ALLOWED,
5847 &attr_data->attr_data.aapl.max_access);
5848 if (!NT_STATUS_IS_OK(status)) {
5849 goto fail;
5853 return NT_STATUS_OK;
5855 fail:
5856 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
5857 fname->base_name, nt_errstr(status)));
5858 TALLOC_FREE(*pattr_data);
5859 return status;
5862 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
5863 files_struct *fsp,
5864 uint32_t security_info,
5865 TALLOC_CTX *mem_ctx,
5866 struct security_descriptor **ppdesc)
5868 NTSTATUS status;
5869 struct security_ace ace;
5870 struct dom_sid sid;
5871 struct fruit_config_data *config;
5873 SMB_VFS_HANDLE_GET_DATA(handle, config,
5874 struct fruit_config_data,
5875 return NT_STATUS_UNSUCCESSFUL);
5877 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
5878 mem_ctx, ppdesc);
5879 if (!NT_STATUS_IS_OK(status)) {
5880 return status;
5884 * Add MS NFS style ACEs with uid, gid and mode
5886 if (!global_fruit_config.nego_aapl) {
5887 return NT_STATUS_OK;
5889 if (!config->unix_info_enabled) {
5890 return NT_STATUS_OK;
5893 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
5894 status = remove_virtual_nfs_aces(*ppdesc);
5895 if (!NT_STATUS_IS_OK(status)) {
5896 DBG_WARNING("failed to remove MS NFS style ACEs\n");
5897 return status;
5900 /* MS NFS style mode */
5901 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
5902 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5903 status = security_descriptor_dacl_add(*ppdesc, &ace);
5904 if (!NT_STATUS_IS_OK(status)) {
5905 DEBUG(1,("failed to add MS NFS style ACE\n"));
5906 return status;
5909 /* MS NFS style uid */
5910 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
5911 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5912 status = security_descriptor_dacl_add(*ppdesc, &ace);
5913 if (!NT_STATUS_IS_OK(status)) {
5914 DEBUG(1,("failed to add MS NFS style ACE\n"));
5915 return status;
5918 /* MS NFS style gid */
5919 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
5920 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
5921 status = security_descriptor_dacl_add(*ppdesc, &ace);
5922 if (!NT_STATUS_IS_OK(status)) {
5923 DEBUG(1,("failed to add MS NFS style ACE\n"));
5924 return status;
5927 return NT_STATUS_OK;
5930 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
5931 files_struct *fsp,
5932 uint32_t security_info_sent,
5933 const struct security_descriptor *orig_psd)
5935 NTSTATUS status;
5936 bool do_chmod;
5937 mode_t ms_nfs_mode = 0;
5938 int result;
5939 struct security_descriptor *psd = NULL;
5940 uint32_t orig_num_aces = 0;
5942 if (orig_psd->dacl != NULL) {
5943 orig_num_aces = orig_psd->dacl->num_aces;
5946 psd = security_descriptor_copy(talloc_tos(), orig_psd);
5947 if (psd == NULL) {
5948 return NT_STATUS_NO_MEMORY;
5951 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
5953 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
5954 if (!NT_STATUS_IS_OK(status)) {
5955 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
5956 TALLOC_FREE(psd);
5957 return status;
5961 * If only ms_nfs ACE entries were sent, ensure we set the DACL
5962 * sent/present flags correctly now we've removed them.
5965 if (orig_num_aces != 0) {
5967 * Are there any ACE's left ?
5969 if (psd->dacl->num_aces == 0) {
5970 /* No - clear the DACL sent/present flags. */
5971 security_info_sent &= ~SECINFO_DACL;
5972 psd->type &= ~SEC_DESC_DACL_PRESENT;
5976 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
5977 if (!NT_STATUS_IS_OK(status)) {
5978 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
5979 TALLOC_FREE(psd);
5980 return status;
5983 if (do_chmod) {
5984 if (fsp->fh->fd != -1) {
5985 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
5986 } else {
5987 result = SMB_VFS_CHMOD(fsp->conn,
5988 fsp->fsp_name,
5989 ms_nfs_mode);
5992 if (result != 0) {
5993 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
5994 result, (unsigned)ms_nfs_mode,
5995 strerror(errno)));
5996 status = map_nt_error_from_unix(errno);
5997 TALLOC_FREE(psd);
5998 return status;
6002 TALLOC_FREE(psd);
6003 return NT_STATUS_OK;
6006 static struct vfs_offload_ctx *fruit_offload_ctx;
6008 struct fruit_offload_read_state {
6009 struct vfs_handle_struct *handle;
6010 struct tevent_context *ev;
6011 files_struct *fsp;
6012 uint32_t fsctl;
6013 DATA_BLOB token;
6016 static void fruit_offload_read_done(struct tevent_req *subreq);
6018 static struct tevent_req *fruit_offload_read_send(
6019 TALLOC_CTX *mem_ctx,
6020 struct tevent_context *ev,
6021 struct vfs_handle_struct *handle,
6022 files_struct *fsp,
6023 uint32_t fsctl,
6024 uint32_t ttl,
6025 off_t offset,
6026 size_t to_copy)
6028 struct tevent_req *req = NULL;
6029 struct tevent_req *subreq = NULL;
6030 struct fruit_offload_read_state *state = NULL;
6032 req = tevent_req_create(mem_ctx, &state,
6033 struct fruit_offload_read_state);
6034 if (req == NULL) {
6035 return NULL;
6037 *state = (struct fruit_offload_read_state) {
6038 .handle = handle,
6039 .ev = ev,
6040 .fsp = fsp,
6041 .fsctl = fsctl,
6044 subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
6045 fsctl, ttl, offset, to_copy);
6046 if (tevent_req_nomem(subreq, req)) {
6047 return tevent_req_post(req, ev);
6049 tevent_req_set_callback(subreq, fruit_offload_read_done, req);
6050 return req;
6053 static void fruit_offload_read_done(struct tevent_req *subreq)
6055 struct tevent_req *req = tevent_req_callback_data(
6056 subreq, struct tevent_req);
6057 struct fruit_offload_read_state *state = tevent_req_data(
6058 req, struct fruit_offload_read_state);
6059 NTSTATUS status;
6061 status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
6062 state->handle,
6063 state,
6064 &state->token);
6065 TALLOC_FREE(subreq);
6066 if (tevent_req_nterror(req, status)) {
6067 return;
6070 if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
6071 tevent_req_done(req);
6072 return;
6075 status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
6076 &fruit_offload_ctx);
6077 if (tevent_req_nterror(req, status)) {
6078 return;
6081 status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
6082 state->fsp,
6083 &state->token);
6084 if (tevent_req_nterror(req, status)) {
6085 return;
6088 tevent_req_done(req);
6089 return;
6092 static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
6093 struct vfs_handle_struct *handle,
6094 TALLOC_CTX *mem_ctx,
6095 DATA_BLOB *token)
6097 struct fruit_offload_read_state *state = tevent_req_data(
6098 req, struct fruit_offload_read_state);
6099 NTSTATUS status;
6101 if (tevent_req_is_nterror(req, &status)) {
6102 tevent_req_received(req);
6103 return status;
6106 token->length = state->token.length;
6107 token->data = talloc_move(mem_ctx, &state->token.data);
6109 tevent_req_received(req);
6110 return NT_STATUS_OK;
6113 struct fruit_offload_write_state {
6114 struct vfs_handle_struct *handle;
6115 off_t copied;
6116 struct files_struct *src_fsp;
6117 struct files_struct *dst_fsp;
6118 bool is_copyfile;
6121 static void fruit_offload_write_done(struct tevent_req *subreq);
6122 static struct tevent_req *fruit_offload_write_send(struct vfs_handle_struct *handle,
6123 TALLOC_CTX *mem_ctx,
6124 struct tevent_context *ev,
6125 uint32_t fsctl,
6126 DATA_BLOB *token,
6127 off_t transfer_offset,
6128 struct files_struct *dest_fsp,
6129 off_t dest_off,
6130 off_t num)
6132 struct tevent_req *req, *subreq;
6133 struct fruit_offload_write_state *state;
6134 NTSTATUS status;
6135 struct fruit_config_data *config;
6136 off_t src_off = transfer_offset;
6137 files_struct *src_fsp = NULL;
6138 off_t to_copy = num;
6139 bool copyfile_enabled = false;
6141 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
6142 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
6144 SMB_VFS_HANDLE_GET_DATA(handle, config,
6145 struct fruit_config_data,
6146 return NULL);
6148 req = tevent_req_create(mem_ctx, &state,
6149 struct fruit_offload_write_state);
6150 if (req == NULL) {
6151 return NULL;
6153 state->handle = handle;
6154 state->dst_fsp = dest_fsp;
6156 switch (fsctl) {
6157 case FSCTL_SRV_COPYCHUNK:
6158 case FSCTL_SRV_COPYCHUNK_WRITE:
6159 copyfile_enabled = config->copyfile_enabled;
6160 break;
6161 default:
6162 break;
6166 * Check if this a OS X copyfile style copychunk request with
6167 * a requested chunk count of 0 that was translated to a
6168 * offload_write_send VFS call overloading the parameters src_off
6169 * = dest_off = num = 0.
6171 if (copyfile_enabled && num == 0 && src_off == 0 && dest_off == 0) {
6172 status = vfs_offload_token_db_fetch_fsp(
6173 fruit_offload_ctx, token, &src_fsp);
6174 if (tevent_req_nterror(req, status)) {
6175 return tevent_req_post(req, ev);
6177 state->src_fsp = src_fsp;
6179 status = vfs_stat_fsp(src_fsp);
6180 if (tevent_req_nterror(req, status)) {
6181 return tevent_req_post(req, ev);
6184 to_copy = src_fsp->fsp_name->st.st_ex_size;
6185 state->is_copyfile = true;
6188 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
6189 mem_ctx,
6191 fsctl,
6192 token,
6193 transfer_offset,
6194 dest_fsp,
6195 dest_off,
6196 to_copy);
6197 if (tevent_req_nomem(subreq, req)) {
6198 return tevent_req_post(req, ev);
6201 tevent_req_set_callback(subreq, fruit_offload_write_done, req);
6202 return req;
6205 static void fruit_offload_write_done(struct tevent_req *subreq)
6207 struct tevent_req *req = tevent_req_callback_data(
6208 subreq, struct tevent_req);
6209 struct fruit_offload_write_state *state = tevent_req_data(
6210 req, struct fruit_offload_write_state);
6211 NTSTATUS status;
6212 unsigned int num_streams = 0;
6213 struct stream_struct *streams = NULL;
6214 unsigned int i;
6215 struct smb_filename *src_fname_tmp = NULL;
6216 struct smb_filename *dst_fname_tmp = NULL;
6218 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
6219 subreq,
6220 &state->copied);
6221 TALLOC_FREE(subreq);
6222 if (tevent_req_nterror(req, status)) {
6223 return;
6226 if (!state->is_copyfile) {
6227 tevent_req_done(req);
6228 return;
6232 * Now copy all remaining streams. We know the share supports
6233 * streams, because we're in vfs_fruit. We don't do this async
6234 * because streams are few and small.
6236 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
6237 state->src_fsp->fsp_name,
6238 req, &num_streams, &streams);
6239 if (tevent_req_nterror(req, status)) {
6240 return;
6243 if (num_streams == 1) {
6244 /* There is always one stream, ::$DATA. */
6245 tevent_req_done(req);
6246 return;
6249 for (i = 0; i < num_streams; i++) {
6250 DEBUG(10, ("%s: stream: '%s'/%zu\n",
6251 __func__, streams[i].name, (size_t)streams[i].size));
6253 src_fname_tmp = synthetic_smb_fname(
6254 req,
6255 state->src_fsp->fsp_name->base_name,
6256 streams[i].name,
6257 NULL,
6258 state->src_fsp->fsp_name->flags);
6259 if (tevent_req_nomem(src_fname_tmp, req)) {
6260 return;
6263 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
6264 TALLOC_FREE(src_fname_tmp);
6265 continue;
6268 dst_fname_tmp = synthetic_smb_fname(
6269 req,
6270 state->dst_fsp->fsp_name->base_name,
6271 streams[i].name,
6272 NULL,
6273 state->dst_fsp->fsp_name->flags);
6274 if (tevent_req_nomem(dst_fname_tmp, req)) {
6275 TALLOC_FREE(src_fname_tmp);
6276 return;
6279 status = copy_file(req,
6280 state->handle->conn,
6281 src_fname_tmp,
6282 dst_fname_tmp,
6283 OPENX_FILE_CREATE_IF_NOT_EXIST,
6284 0, false);
6285 if (!NT_STATUS_IS_OK(status)) {
6286 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
6287 smb_fname_str_dbg(src_fname_tmp),
6288 smb_fname_str_dbg(dst_fname_tmp),
6289 nt_errstr(status)));
6290 TALLOC_FREE(src_fname_tmp);
6291 TALLOC_FREE(dst_fname_tmp);
6292 tevent_req_nterror(req, status);
6293 return;
6296 TALLOC_FREE(src_fname_tmp);
6297 TALLOC_FREE(dst_fname_tmp);
6300 TALLOC_FREE(streams);
6301 TALLOC_FREE(src_fname_tmp);
6302 TALLOC_FREE(dst_fname_tmp);
6303 tevent_req_done(req);
6306 static NTSTATUS fruit_offload_write_recv(struct vfs_handle_struct *handle,
6307 struct tevent_req *req,
6308 off_t *copied)
6310 struct fruit_offload_write_state *state = tevent_req_data(
6311 req, struct fruit_offload_write_state);
6312 NTSTATUS status;
6314 if (tevent_req_is_nterror(req, &status)) {
6315 DEBUG(1, ("server side copy chunk failed: %s\n",
6316 nt_errstr(status)));
6317 *copied = 0;
6318 tevent_req_received(req);
6319 return status;
6322 *copied = state->copied;
6323 tevent_req_received(req);
6325 return NT_STATUS_OK;
6328 static char *fruit_get_bandsize_line(char **lines, int numlines)
6330 static regex_t re;
6331 static bool re_initialized = false;
6332 int i;
6333 int ret;
6335 if (!re_initialized) {
6336 ret = regcomp(&re, "^[[:blank:]]*<key>band-size</key>$", 0);
6337 if (ret != 0) {
6338 return NULL;
6340 re_initialized = true;
6343 for (i = 0; i < numlines; i++) {
6344 regmatch_t matches[1];
6346 ret = regexec(&re, lines[i], 1, matches, 0);
6347 if (ret == 0) {
6349 * Check if the match was on the last line, sa we want
6350 * the subsequent line.
6352 if (i + 1 == numlines) {
6353 return NULL;
6355 return lines[i + 1];
6357 if (ret != REG_NOMATCH) {
6358 return NULL;
6362 return NULL;
6365 static bool fruit_get_bandsize_from_line(char *line, size_t *_band_size)
6367 static regex_t re;
6368 static bool re_initialized = false;
6369 regmatch_t matches[2];
6370 uint64_t band_size;
6371 int ret;
6372 bool ok;
6374 if (!re_initialized) {
6375 ret = regcomp(&re,
6376 "^[[:blank:]]*"
6377 "<integer>\\([[:digit:]]*\\)</integer>$",
6379 if (ret != 0) {
6380 return false;
6382 re_initialized = true;
6385 ret = regexec(&re, line, 2, matches, 0);
6386 if (ret != 0) {
6387 DBG_ERR("regex failed [%s]\n", line);
6388 return false;
6391 line[matches[1].rm_eo] = '\0';
6393 ok = conv_str_u64(&line[matches[1].rm_so], &band_size);
6394 if (!ok) {
6395 return false;
6397 *_band_size = (size_t)band_size;
6398 return true;
6402 * This reads and parses an Info.plist from a TM sparsebundle looking for the
6403 * "band-size" key and value.
6405 static bool fruit_get_bandsize(vfs_handle_struct *handle,
6406 const char *dir,
6407 size_t *band_size)
6409 #define INFO_PLIST_MAX_SIZE 64*1024
6410 char *plist = NULL;
6411 struct smb_filename *smb_fname = NULL;
6412 files_struct *fsp = NULL;
6413 uint8_t *file_data = NULL;
6414 char **lines = NULL;
6415 char *band_size_line = NULL;
6416 size_t plist_file_size;
6417 ssize_t nread;
6418 int numlines;
6419 int ret;
6420 bool ok = false;
6421 NTSTATUS status;
6423 plist = talloc_asprintf(talloc_tos(),
6424 "%s/%s/Info.plist",
6425 handle->conn->connectpath,
6426 dir);
6427 if (plist == NULL) {
6428 ok = false;
6429 goto out;
6432 smb_fname = synthetic_smb_fname(talloc_tos(), plist, NULL, NULL, 0);
6433 if (smb_fname == NULL) {
6434 ok = false;
6435 goto out;
6438 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
6439 if (ret != 0) {
6440 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir);
6441 ok = true;
6442 goto out;
6445 plist_file_size = smb_fname->st.st_ex_size;
6447 if (plist_file_size > INFO_PLIST_MAX_SIZE) {
6448 DBG_INFO("%s is too large, ignoring\n", plist);
6449 ok = true;
6450 goto out;
6453 status = SMB_VFS_NEXT_CREATE_FILE(
6454 handle, /* conn */
6455 NULL, /* req */
6456 0, /* root_dir_fid */
6457 smb_fname, /* fname */
6458 FILE_GENERIC_READ, /* access_mask */
6459 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6460 FILE_OPEN, /* create_disposition */
6461 0, /* create_options */
6462 0, /* file_attributes */
6463 INTERNAL_OPEN_ONLY, /* oplock_request */
6464 NULL, /* lease */
6465 0, /* allocation_size */
6466 0, /* private_flags */
6467 NULL, /* sd */
6468 NULL, /* ea_list */
6469 &fsp, /* result */
6470 NULL, /* psbuf */
6471 NULL, NULL); /* create context */
6472 if (!NT_STATUS_IS_OK(status)) {
6473 DBG_INFO("Opening [%s] failed [%s]\n",
6474 smb_fname_str_dbg(smb_fname), nt_errstr(status));
6475 ok = false;
6476 goto out;
6479 file_data = talloc_array(talloc_tos(), uint8_t, plist_file_size);
6480 if (file_data == NULL) {
6481 ok = false;
6482 goto out;
6485 nread = SMB_VFS_NEXT_PREAD(handle, fsp, file_data, plist_file_size, 0);
6486 if (nread != plist_file_size) {
6487 DBG_ERR("Short read on [%s]: %zu/%zd\n",
6488 fsp_str_dbg(fsp), nread, plist_file_size);
6489 ok = false;
6490 goto out;
6494 status = close_file(NULL, fsp, NORMAL_CLOSE);
6495 fsp = NULL;
6496 if (!NT_STATUS_IS_OK(status)) {
6497 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6498 ok = false;
6499 goto out;
6502 lines = file_lines_parse((char *)file_data,
6503 plist_file_size,
6504 &numlines,
6505 talloc_tos());
6506 if (lines == NULL) {
6507 ok = false;
6508 goto out;
6511 band_size_line = fruit_get_bandsize_line(lines, numlines);
6512 if (band_size_line == NULL) {
6513 DBG_ERR("Didn't find band-size key in [%s]\n",
6514 smb_fname_str_dbg(smb_fname));
6515 ok = false;
6516 goto out;
6519 ok = fruit_get_bandsize_from_line(band_size_line, band_size);
6520 if (!ok) {
6521 DBG_ERR("fruit_get_bandsize_from_line failed\n");
6522 goto out;
6525 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size, plist);
6527 out:
6528 if (fsp != NULL) {
6529 status = close_file(NULL, fsp, NORMAL_CLOSE);
6530 if (!NT_STATUS_IS_OK(status)) {
6531 DBG_ERR("close_file failed: %s\n", nt_errstr(status));
6533 fsp = NULL;
6535 TALLOC_FREE(plist);
6536 TALLOC_FREE(smb_fname);
6537 TALLOC_FREE(file_data);
6538 TALLOC_FREE(lines);
6539 return ok;
6542 struct fruit_disk_free_state {
6543 off_t total_size;
6546 static bool fruit_get_num_bands(vfs_handle_struct *handle,
6547 char *bundle,
6548 size_t *_nbands)
6550 char *path = NULL;
6551 struct smb_filename *bands_dir = NULL;
6552 DIR *d = NULL;
6553 struct dirent *e = NULL;
6554 size_t nbands;
6555 int ret;
6557 path = talloc_asprintf(talloc_tos(),
6558 "%s/%s/bands",
6559 handle->conn->connectpath,
6560 bundle);
6561 if (path == NULL) {
6562 return false;
6565 bands_dir = synthetic_smb_fname(talloc_tos(),
6566 path,
6567 NULL,
6568 NULL,
6570 TALLOC_FREE(path);
6571 if (bands_dir == NULL) {
6572 return false;
6575 d = SMB_VFS_NEXT_OPENDIR(handle, bands_dir, NULL, 0);
6576 if (d == NULL) {
6577 TALLOC_FREE(bands_dir);
6578 return false;
6581 nbands = 0;
6583 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6584 e != NULL;
6585 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6587 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name)) {
6588 continue;
6590 nbands++;
6593 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6594 if (ret != 0) {
6595 TALLOC_FREE(bands_dir);
6596 return false;
6599 DBG_DEBUG("%zu bands in [%s]\n", nbands, smb_fname_str_dbg(bands_dir));
6601 TALLOC_FREE(bands_dir);
6603 *_nbands = nbands;
6604 return true;
6607 static bool fruit_tmsize_do_dirent(vfs_handle_struct *handle,
6608 struct fruit_disk_free_state *state,
6609 struct dirent *e)
6611 bool ok;
6612 char *p = NULL;
6613 size_t sparsebundle_strlen = strlen("sparsebundle");
6614 size_t bandsize = 0;
6615 size_t nbands;
6616 off_t tm_size;
6618 p = strstr(e->d_name, "sparsebundle");
6619 if (p == NULL) {
6620 return true;
6623 if (p[sparsebundle_strlen] != '\0') {
6624 return true;
6627 DBG_DEBUG("Processing sparsebundle [%s]\n", e->d_name);
6629 ok = fruit_get_bandsize(handle, e->d_name, &bandsize);
6630 if (!ok) {
6632 * Beware of race conditions: this may be an uninitialized
6633 * Info.plist that a client is just creating. We don't want let
6634 * this to trigger complete failure.
6636 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6637 return true;
6640 ok = fruit_get_num_bands(handle, e->d_name, &nbands);
6641 if (!ok) {
6643 * Beware of race conditions: this may be a backup sparsebundle
6644 * in an early stage lacking a bands subdirectory. We don't want
6645 * let this to trigger complete failure.
6647 DBG_ERR("Processing sparsebundle [%s] failed\n", e->d_name);
6648 return true;
6651 if (bandsize > SIZE_MAX/nbands) {
6652 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6653 bandsize, nbands);
6654 return false;
6656 tm_size = bandsize * nbands;
6658 if (state->total_size + tm_size < state->total_size) {
6659 DBG_ERR("tmsize overflow: bandsize [%zu] nbands [%zu]\n",
6660 bandsize, nbands);
6661 return false;
6664 state->total_size += tm_size;
6666 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
6667 e->d_name, (intmax_t)tm_size, (intmax_t)state->total_size);
6669 return true;
6673 * Calculate used size of a TimeMachine volume
6675 * This assumes that the volume is used only for TimeMachine.
6677 * - readdir(basedir of share), then
6678 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
6679 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
6680 * - count band files in "\1.sparsebundle/bands/"
6681 * - calculate used size of all bands: band_count * band_size
6683 static uint64_t fruit_disk_free(vfs_handle_struct *handle,
6684 const struct smb_filename *smb_fname,
6685 uint64_t *_bsize,
6686 uint64_t *_dfree,
6687 uint64_t *_dsize)
6689 struct fruit_config_data *config = NULL;
6690 struct fruit_disk_free_state state = {0};
6691 DIR *d = NULL;
6692 struct dirent *e = NULL;
6693 uint64_t dfree;
6694 uint64_t dsize;
6695 int ret;
6696 bool ok;
6698 SMB_VFS_HANDLE_GET_DATA(handle, config,
6699 struct fruit_config_data,
6700 return UINT64_MAX);
6702 if (!config->time_machine ||
6703 config->time_machine_max_size == 0)
6705 return SMB_VFS_NEXT_DISK_FREE(handle,
6706 smb_fname,
6707 _bsize,
6708 _dfree,
6709 _dsize);
6712 d = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, NULL, 0);
6713 if (d == NULL) {
6714 return UINT64_MAX;
6717 for (e = SMB_VFS_NEXT_READDIR(handle, d, NULL);
6718 e != NULL;
6719 e = SMB_VFS_NEXT_READDIR(handle, d, NULL))
6721 ok = fruit_tmsize_do_dirent(handle, &state, e);
6722 if (!ok) {
6723 SMB_VFS_NEXT_CLOSEDIR(handle, d);
6724 return UINT64_MAX;
6728 ret = SMB_VFS_NEXT_CLOSEDIR(handle, d);
6729 if (ret != 0) {
6730 return UINT64_MAX;
6733 dsize = config->time_machine_max_size / 512;
6734 dfree = dsize - (state.total_size / 512);
6735 if (dfree > dsize) {
6736 dfree = 0;
6739 *_bsize = 512;
6740 *_dsize = dsize;
6741 *_dfree = dfree;
6742 return dfree / 2;
6745 static struct vfs_fn_pointers vfs_fruit_fns = {
6746 .connect_fn = fruit_connect,
6747 .disk_free_fn = fruit_disk_free,
6749 /* File operations */
6750 .chmod_fn = fruit_chmod,
6751 .chown_fn = fruit_chown,
6752 .unlink_fn = fruit_unlink,
6753 .rename_fn = fruit_rename,
6754 .rmdir_fn = fruit_rmdir,
6755 .open_fn = fruit_open,
6756 .pread_fn = fruit_pread,
6757 .pwrite_fn = fruit_pwrite,
6758 .pread_send_fn = fruit_pread_send,
6759 .pread_recv_fn = fruit_pread_recv,
6760 .pwrite_send_fn = fruit_pwrite_send,
6761 .pwrite_recv_fn = fruit_pwrite_recv,
6762 .stat_fn = fruit_stat,
6763 .lstat_fn = fruit_lstat,
6764 .fstat_fn = fruit_fstat,
6765 .streaminfo_fn = fruit_streaminfo,
6766 .ntimes_fn = fruit_ntimes,
6767 .ftruncate_fn = fruit_ftruncate,
6768 .fallocate_fn = fruit_fallocate,
6769 .create_file_fn = fruit_create_file,
6770 .readdir_attr_fn = fruit_readdir_attr,
6771 .offload_read_send_fn = fruit_offload_read_send,
6772 .offload_read_recv_fn = fruit_offload_read_recv,
6773 .offload_write_send_fn = fruit_offload_write_send,
6774 .offload_write_recv_fn = fruit_offload_write_recv,
6776 /* NT ACL operations */
6777 .fget_nt_acl_fn = fruit_fget_nt_acl,
6778 .fset_nt_acl_fn = fruit_fset_nt_acl,
6781 static_decl_vfs;
6782 NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
6784 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
6785 &vfs_fruit_fns);
6786 if (!NT_STATUS_IS_OK(ret)) {
6787 return ret;
6790 vfs_fruit_debug_level = debug_add_class("fruit");
6791 if (vfs_fruit_debug_level == -1) {
6792 vfs_fruit_debug_level = DBGC_VFS;
6793 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
6794 "vfs_fruit_init"));
6795 } else {
6796 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
6797 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
6800 return ret;