pyldb: Modernize test suite
[Samba.git] / source3 / modules / vfs_fruit.c
bloba09e2886f8338b8e608d8530ba50cd6dc570a77d
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"
36 * Enhanced OS X and Netatalk compatibility
37 * ========================================
39 * This modules takes advantage of vfs_streams_xattr and
40 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
41 * loaded in the correct order:
43 * vfs modules = catia fruit streams_xattr
45 * The module intercepts the OS X special streams "AFP_AfpInfo" and
46 * "AFP_Resource" and handles them in a special way. All other named
47 * streams are deferred to vfs_streams_xattr.
49 * The OS X client maps all NTFS illegal characters to the Unicode
50 * private range. This module optionally stores the charcters using
51 * their native ASCII encoding using vfs_catia. If you're not enabling
52 * this feature, you can skip catia from vfs modules.
54 * Finally, open modes are optionally checked against Netatalk AFP
55 * share modes.
57 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
58 * extended metadata for files and directories. This module optionally
59 * reads and stores this metadata in a way compatible with Netatalk 3
60 * which stores the metadata in an EA "org.netatalk.metadata". Cf
61 * source3/include/MacExtensions.h for a description of the binary
62 * blobs content.
64 * The "AFP_Resource" named stream may be arbitrarily large, thus it
65 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
66 * the only available filesystem where xattrs can be of any size and
67 * the OS supports using the file APIs for xattrs.
69 * The AFP_Resource stream is stored in an AppleDouble file prepending
70 * "._" to the filename. On Solaris with ZFS the stream is optionally
71 * stored in an EA "org.netatalk.ressource".
74 * Extended Attributes
75 * ===================
77 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
78 * other protocols you may want to adjust the xattr names the VFS
79 * module vfs_streams_xattr uses for storing ADS's. This defaults to
80 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
81 * these module parameters:
83 * streams_xattr:prefix = user.
84 * streams_xattr:store_stream_type = false
87 * TODO
88 * ====
90 * - log diagnostic if any needed VFS module is not loaded
91 * (eg with lp_vfs_objects())
92 * - add tests
95 static int vfs_fruit_debug_level = DBGC_VFS;
97 #undef DBGC_CLASS
98 #define DBGC_CLASS vfs_fruit_debug_level
100 #define FRUIT_PARAM_TYPE_NAME "fruit"
101 #define ADOUBLE_NAME_PREFIX "._"
104 * REVIEW:
105 * This is hokey, but what else can we do?
107 #define NETATALK_META_XATTR "org.netatalk.Metadata"
108 #if defined(HAVE_ATTROPEN) || defined(FREEBSD)
109 #define AFPINFO_EA_NETATALK NETATALK_META_XATTR
110 #define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
111 #else
112 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
113 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
114 #endif
116 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
118 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
119 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
120 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
121 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
123 struct fruit_config_data {
124 enum fruit_rsrc rsrc;
125 enum fruit_meta meta;
126 enum fruit_locking locking;
127 enum fruit_encoding encoding;
128 bool use_aapl;
129 bool use_copyfile;
130 bool readdir_attr_enabled;
131 bool unix_info_enabled;
132 bool copyfile_enabled;
133 bool veto_appledouble;
136 * Additional options, all enabled by default,
137 * possibly useful for analyzing performance. The associated
138 * operations with each of them may be expensive, so having
139 * the chance to disable them individually gives a chance
140 * tweaking the setup for the particular usecase.
142 bool readdir_attr_rsize;
143 bool readdir_attr_finder_info;
144 bool readdir_attr_max_access;
147 static const struct enum_list fruit_rsrc[] = {
148 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
149 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
150 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
151 { -1, NULL}
154 static const struct enum_list fruit_meta[] = {
155 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
156 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
157 { -1, NULL}
160 static const struct enum_list fruit_locking[] = {
161 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
162 {FRUIT_LOCKING_NONE, "none"},
163 { -1, NULL}
166 static const struct enum_list fruit_encoding[] = {
167 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
168 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
169 { -1, NULL}
172 /*****************************************************************************
173 * Defines, functions and data structures that deal with AppleDouble
174 *****************************************************************************/
177 * There are two AppleDouble blobs we deal with:
179 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
180 * metadata in an xattr
182 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
183 * ._ files
185 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
187 /* Version info */
188 #define AD_VERSION2 0x00020000
189 #define AD_VERSION AD_VERSION2
192 * AppleDouble entry IDs.
194 #define ADEID_DFORK 1
195 #define ADEID_RFORK 2
196 #define ADEID_NAME 3
197 #define ADEID_COMMENT 4
198 #define ADEID_ICONBW 5
199 #define ADEID_ICONCOL 6
200 #define ADEID_FILEI 7
201 #define ADEID_FILEDATESI 8
202 #define ADEID_FINDERI 9
203 #define ADEID_MACFILEI 10
204 #define ADEID_PRODOSFILEI 11
205 #define ADEID_MSDOSFILEI 12
206 #define ADEID_SHORTNAME 13
207 #define ADEID_AFPFILEI 14
208 #define ADEID_DID 15
210 /* Private Netatalk entries */
211 #define ADEID_PRIVDEV 16
212 #define ADEID_PRIVINO 17
213 #define ADEID_PRIVSYN 18
214 #define ADEID_PRIVID 19
215 #define ADEID_MAX (ADEID_PRIVID + 1)
218 * These are the real ids for the private entries,
219 * as stored in the adouble file
221 #define AD_DEV 0x80444556
222 #define AD_INO 0x80494E4F
223 #define AD_SYN 0x8053594E
224 #define AD_ID 0x8053567E
226 /* Number of actually used entries */
227 #define ADEID_NUM_XATTR 8
228 #define ADEID_NUM_DOT_UND 2
229 #define ADEID_NUM_RSRC_XATTR 1
231 /* AppleDouble magic */
232 #define AD_APPLESINGLE_MAGIC 0x00051600
233 #define AD_APPLEDOUBLE_MAGIC 0x00051607
234 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
236 /* Sizes of relevant entry bits */
237 #define ADEDLEN_MAGIC 4
238 #define ADEDLEN_VERSION 4
239 #define ADEDLEN_FILLER 16
240 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
241 #define ADEDLEN_NENTRIES 2
242 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
243 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
244 #define AD_ENTRY_LEN_EID 4
245 #define AD_ENTRY_LEN_OFF 4
246 #define AD_ENTRY_LEN_LEN 4
247 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
249 /* Field widths */
250 #define ADEDLEN_NAME 255
251 #define ADEDLEN_COMMENT 200
252 #define ADEDLEN_FILEI 16
253 #define ADEDLEN_FINDERI 32
254 #define ADEDLEN_FILEDATESI 16
255 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
256 #define ADEDLEN_AFPFILEI 4
257 #define ADEDLEN_MACFILEI 4
258 #define ADEDLEN_PRODOSFILEI 8
259 #define ADEDLEN_MSDOSFILEI 2
260 #define ADEDLEN_DID 4
261 #define ADEDLEN_PRIVDEV 8
262 #define ADEDLEN_PRIVINO 8
263 #define ADEDLEN_PRIVSYN 8
264 #define ADEDLEN_PRIVID 4
266 /* Offsets */
267 #define ADEDOFF_MAGIC 0
268 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
269 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
270 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
272 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
273 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
274 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
275 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
276 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
277 ADEDLEN_FILEDATESI)
278 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
279 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
280 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
281 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
283 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
284 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
285 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
287 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
288 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
289 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
290 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
291 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
292 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
294 #if AD_DATASZ_XATTR != 402
295 #error bad size for AD_DATASZ_XATTR
296 #endif
298 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
299 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
300 ADEDLEN_FINDERI)
301 #if AD_DATASZ_DOT_UND != 82
302 #error bad size for AD_DATASZ_DOT_UND
303 #endif
306 * Sharemode locks fcntl() offsets
308 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
309 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
310 #else
311 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
312 #endif
313 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
315 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
316 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
317 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
318 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
319 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
320 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
321 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
322 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
323 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
324 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
326 /* Time stuff we overload the bits a little */
327 #define AD_DATE_CREATE 0
328 #define AD_DATE_MODIFY 4
329 #define AD_DATE_BACKUP 8
330 #define AD_DATE_ACCESS 12
331 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
332 AD_DATE_BACKUP | AD_DATE_ACCESS)
333 #define AD_DATE_UNIX (1 << 10)
334 #define AD_DATE_START 0x80000000
335 #define AD_DATE_DELTA 946684800
336 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
337 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
339 /* Accessor macros */
340 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
341 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
342 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
343 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
344 #define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
346 struct ad_entry {
347 size_t ade_off;
348 size_t ade_len;
351 struct adouble {
352 vfs_handle_struct *ad_handle;
353 files_struct *ad_fsp;
354 adouble_type_t ad_type;
355 uint32_t ad_magic;
356 uint32_t ad_version;
357 struct ad_entry ad_eid[ADEID_MAX];
358 char *ad_data;
361 struct ad_entry_order {
362 uint32_t id, offset, len;
365 /* Netatalk AppleDouble metadata xattr */
366 static const
367 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
368 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
369 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
370 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
371 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
372 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
373 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
374 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
375 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
376 {0, 0, 0}
379 /* AppleDouble ressource fork file (the ones prefixed by "._") */
380 static const
381 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
382 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
383 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
384 {0, 0, 0}
388 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
389 * isn't an AppleDouble file, it simply contains the ressource data,
390 * but in order to be able to use some API calls like ad_getentryoff()
391 * we build a fake/helper struct adouble with this entry order struct.
393 static const
394 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
395 {ADEID_RFORK, 0, 0},
396 {0, 0, 0}
399 /* Conversion from enumerated id to on-disk AppleDouble id */
400 #define AD_EID_DISK(a) (set_eid[a])
401 static const uint32_t set_eid[] = {
402 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
403 AD_DEV, AD_INO, AD_SYN, AD_ID
407 * Forward declarations
409 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
410 adouble_type_t type, files_struct *fsp);
411 static int ad_write(struct adouble *ad, const char *path);
412 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
415 * Get a date
417 static int ad_getdate(const struct adouble *ad,
418 unsigned int dateoff,
419 uint32_t *date)
421 bool xlate = (dateoff & AD_DATE_UNIX);
423 dateoff &= AD_DATE_MASK;
424 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
425 return -1;
428 if (dateoff > AD_DATE_ACCESS) {
429 return -1;
431 memcpy(date,
432 ad_entry(ad, ADEID_FILEDATESI) + dateoff,
433 sizeof(uint32_t));
435 if (xlate) {
436 *date = AD_DATE_TO_UNIX(*date);
438 return 0;
442 * Set a date
444 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
446 bool xlate = (dateoff & AD_DATE_UNIX);
448 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
449 return 0;
452 dateoff &= AD_DATE_MASK;
453 if (xlate) {
454 date = AD_DATE_FROM_UNIX(date);
457 if (dateoff > AD_DATE_ACCESS) {
458 return -1;
461 memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
463 return 0;
468 * Map on-disk AppleDouble id to enumerated id
470 static uint32_t get_eid(uint32_t eid)
472 if (eid <= 15) {
473 return eid;
476 switch (eid) {
477 case AD_DEV:
478 return ADEID_PRIVDEV;
479 case AD_INO:
480 return ADEID_PRIVINO;
481 case AD_SYN:
482 return ADEID_PRIVSYN;
483 case AD_ID:
484 return ADEID_PRIVID;
485 default:
486 break;
489 return 0;
493 * Pack AppleDouble structure into data buffer
495 static bool ad_pack(struct adouble *ad)
497 uint32_t eid;
498 uint16_t nent;
499 uint32_t bufsize;
500 uint32_t offset = 0;
502 bufsize = talloc_get_size(ad->ad_data);
504 if (offset + ADEDLEN_MAGIC < offset ||
505 offset + ADEDLEN_MAGIC >= bufsize) {
506 return false;
508 RSIVAL(ad->ad_data, offset, ad->ad_magic);
509 offset += ADEDLEN_MAGIC;
511 if (offset + ADEDLEN_VERSION < offset ||
512 offset + ADEDLEN_VERSION >= bufsize) {
513 return false;
515 RSIVAL(ad->ad_data, offset, ad->ad_version);
516 offset += ADEDLEN_VERSION;
518 if (offset + ADEDLEN_FILLER < offset ||
519 offset + ADEDLEN_FILLER >= bufsize) {
520 return false;
522 if (ad->ad_type == ADOUBLE_RSRC) {
523 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
525 offset += ADEDLEN_FILLER;
527 if (offset + ADEDLEN_NENTRIES < offset ||
528 offset + ADEDLEN_NENTRIES >= bufsize) {
529 return false;
531 offset += ADEDLEN_NENTRIES;
533 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
534 if (ad->ad_eid[eid].ade_off == 0) {
536 * ade_off is also used as indicator whether a
537 * specific entry is used or not
539 continue;
542 if (offset + AD_ENTRY_LEN_EID < offset ||
543 offset + AD_ENTRY_LEN_EID >= bufsize) {
544 return false;
546 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
547 offset += AD_ENTRY_LEN_EID;
549 if (offset + AD_ENTRY_LEN_OFF < offset ||
550 offset + AD_ENTRY_LEN_OFF >= bufsize) {
551 return false;
553 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
554 offset += AD_ENTRY_LEN_OFF;
556 if (offset + AD_ENTRY_LEN_LEN < offset ||
557 offset + AD_ENTRY_LEN_LEN >= bufsize) {
558 return false;
560 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
561 offset += AD_ENTRY_LEN_LEN;
563 nent++;
566 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
567 return false;
569 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
571 return true;
575 * Unpack an AppleDouble blob into a struct adoble
577 static bool ad_unpack(struct adouble *ad, const int nentries, size_t filesize)
579 size_t bufsize = talloc_get_size(ad->ad_data);
580 int adentries, i;
581 uint32_t eid, len, off;
584 * The size of the buffer ad->ad_data is checked when read, so
585 * we wouldn't have to check our own offsets, a few extra
586 * checks won't hurt though. We have to check the offsets we
587 * read from the buffer anyway.
590 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
591 DEBUG(1, ("bad size\n"));
592 return false;
595 ad->ad_magic = RIVAL(ad->ad_data, 0);
596 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
597 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
598 DEBUG(1, ("wrong magic or version\n"));
599 return false;
602 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
603 if (adentries != nentries) {
604 DEBUG(1, ("invalid number of entries: %d\n", adentries));
605 return false;
608 /* now, read in the entry bits */
609 for (i = 0; i < adentries; i++) {
610 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
611 eid = get_eid(eid);
612 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
613 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
615 if (!eid || eid > ADEID_MAX) {
616 DEBUG(1, ("bogus eid %d\n", eid));
617 return false;
621 * All entries other than the resource fork are
622 * expected to be read into the ad_data buffer, so
623 * ensure the specified offset is within that bound
625 if ((off > bufsize) && (eid != ADEID_RFORK)) {
626 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
627 eid, off, len));
628 return false;
632 * All entries besides FinderInfo and resource fork
633 * must fit into the buffer. FinderInfo is special as
634 * it may be larger then the default 32 bytes (if it
635 * contains marshalled xattrs), but we will fixup that
636 * in ad_convert(). And the resource fork is never
637 * accessed directly by the ad_data buf (also see
638 * comment above) anyway.
640 if ((eid != ADEID_RFORK) &&
641 (eid != ADEID_FINDERI) &&
642 ((off + len) > bufsize)) {
643 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
644 eid, off, len));
645 return false;
649 * That would be obviously broken
651 if (off > filesize) {
652 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
653 eid, off, len));
654 return false;
658 * Check for any entry that has its end beyond the
659 * filesize.
661 if (off + len < off) {
662 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
663 ", len: %" PRIu32 "\n",
664 eid, off, len));
665 return false;
668 if (off + len > filesize) {
670 * If this is the resource fork entry, we fix
671 * up the length, for any other entry we bail
672 * out.
674 if (eid != ADEID_RFORK) {
675 DEBUG(1, ("bogus eid %d: off: %" PRIu32
676 ", len: %" PRIu32 "\n",
677 eid, off, len));
678 return false;
682 * Fixup the resource fork entry by limiting
683 * the size to entryoffset - filesize.
685 len = filesize - off;
686 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
687 ", len: %" PRIu32 "\n", off, len));
690 ad->ad_eid[eid].ade_off = off;
691 ad->ad_eid[eid].ade_len = len;
694 return true;
698 * Convert from Apple's ._ file to Netatalk
700 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
701 * bytes containing packed xattrs. Netatalk can't deal with that, so
702 * we simply discard the packed xattrs.
704 * @return -1 in case an error occured, 0 if no conversion was done, 1
705 * otherwise
707 static int ad_convert(struct adouble *ad, int fd)
709 int rc = 0;
710 char *map = MAP_FAILED;
711 size_t origlen;
713 origlen = ad_getentryoff(ad, ADEID_RFORK) +
714 ad_getentrylen(ad, ADEID_RFORK);
716 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
717 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
718 if (map == MAP_FAILED) {
719 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
720 rc = -1;
721 goto exit;
724 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
725 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
726 map + ad_getentryoff(ad, ADEID_RFORK),
727 ad_getentrylen(ad, ADEID_RFORK));
730 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
731 ad_setentryoff(ad, ADEID_RFORK,
732 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
735 * FIXME: direct ftruncate(), but we don't have a fsp for the
736 * VFS call
738 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
739 + ad_getentrylen(ad, ADEID_RFORK));
741 exit:
742 if (map != MAP_FAILED) {
743 munmap(map, origlen);
745 return rc;
749 * Read and parse Netatalk AppleDouble metadata xattr
751 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
753 int rc = 0;
754 ssize_t ealen;
755 bool ok;
757 DEBUG(10, ("reading meta xattr for %s\n", path));
759 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
760 AFPINFO_EA_NETATALK, ad->ad_data,
761 AD_DATASZ_XATTR);
762 if (ealen == -1) {
763 switch (errno) {
764 case ENOATTR:
765 case ENOENT:
766 if (errno == ENOATTR) {
767 errno = ENOENT;
769 rc = -1;
770 goto exit;
771 default:
772 DEBUG(2, ("error reading meta xattr: %s\n",
773 strerror(errno)));
774 rc = -1;
775 goto exit;
778 if (ealen != AD_DATASZ_XATTR) {
779 DEBUG(2, ("bad size %zd\n", ealen));
780 errno = EINVAL;
781 rc = -1;
782 goto exit;
785 /* Now parse entries */
786 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
787 if (!ok) {
788 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
789 errno = EINVAL;
790 rc = -1;
791 goto exit;
794 if (!ad_getentryoff(ad, ADEID_FINDERI)
795 || !ad_getentryoff(ad, ADEID_COMMENT)
796 || !ad_getentryoff(ad, ADEID_FILEDATESI)
797 || !ad_getentryoff(ad, ADEID_AFPFILEI)
798 || !ad_getentryoff(ad, ADEID_PRIVDEV)
799 || !ad_getentryoff(ad, ADEID_PRIVINO)
800 || !ad_getentryoff(ad, ADEID_PRIVSYN)
801 || !ad_getentryoff(ad, ADEID_PRIVID)) {
802 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
803 errno = EINVAL;
804 rc = -1;
805 goto exit;
808 exit:
809 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
811 if (rc != 0) {
812 ealen = -1;
813 if (errno == EINVAL) {
814 become_root();
815 removexattr(path, AFPINFO_EA_NETATALK);
816 unbecome_root();
817 errno = ENOENT;
820 return ealen;
824 * Read and parse resource fork, either ._ AppleDouble file or xattr
826 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
828 struct fruit_config_data *config = NULL;
829 int fd = -1;
830 int rc = 0;
831 ssize_t len;
832 char *adpath = NULL;
833 bool opened = false;
834 int mode;
835 struct adouble *meta_ad = NULL;
836 SMB_STRUCT_STAT sbuf;
837 bool ok;
838 int saved_errno = 0;
840 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
841 struct fruit_config_data, return -1);
843 /* Try rw first so we can use the fd in ad_convert() */
844 mode = O_RDWR;
846 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
847 fd = ad->ad_fsp->fh->fd;
848 } else {
849 if (config->rsrc == FRUIT_RSRC_XATTR) {
850 adpath = talloc_strdup(talloc_tos(), path);
851 } else {
852 rc = adouble_path(talloc_tos(), path, &adpath);
853 if (rc != 0) {
854 goto exit;
858 retry:
859 if (config->rsrc == FRUIT_RSRC_XATTR) {
860 #ifndef HAVE_ATTROPEN
861 errno = ENOSYS;
862 rc = -1;
863 goto exit;
864 #else
865 /* FIXME: direct Solaris xattr syscall */
866 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
867 mode, 0);
868 #endif
869 } else {
870 /* FIXME: direct open(), don't have an fsp */
871 fd = open(adpath, mode);
874 if (fd == -1) {
875 switch (errno) {
876 case EROFS:
877 case EACCES:
878 if (mode == O_RDWR) {
879 mode = O_RDONLY;
880 goto retry;
882 /* fall through ... */
883 default:
884 DEBUG(2, ("open AppleDouble: %s, %s\n",
885 adpath, strerror(errno)));
886 rc = -1;
887 goto exit;
890 opened = true;
893 if (config->rsrc == FRUIT_RSRC_XATTR) {
894 /* FIXME: direct sys_fstat(), don't have an fsp */
895 rc = sys_fstat(
896 fd, &sbuf,
897 lp_fake_directory_create_times(
898 SNUM(ad->ad_handle->conn)));
899 if (rc != 0) {
900 goto exit;
902 len = sbuf.st_ex_size;
903 ad_setentrylen(ad, ADEID_RFORK, len);
904 } else {
905 /* FIXME: direct sys_pread(), don't have an fsp */
906 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
907 if (len != AD_DATASZ_DOT_UND) {
908 DEBUG(2, ("%s: bad size: %zd\n",
909 strerror(errno), len));
910 rc = -1;
911 goto exit;
914 /* FIXME: direct sys_fstat(), we don't have an fsp */
915 rc = sys_fstat(fd, &sbuf,
916 lp_fake_directory_create_times(
917 SNUM(ad->ad_handle->conn)));
918 if (rc != 0) {
919 goto exit;
922 /* Now parse entries */
923 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
924 if (!ok) {
925 DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
926 errno = EINVAL;
927 rc = -1;
928 goto exit;
931 if ((ad_getentryoff(ad, ADEID_FINDERI)
932 != ADEDOFF_FINDERI_DOT_UND)
933 || (ad_getentrylen(ad, ADEID_FINDERI)
934 < ADEDLEN_FINDERI)
935 || (ad_getentryoff(ad, ADEID_RFORK)
936 < ADEDOFF_RFORK_DOT_UND)) {
937 DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
938 errno = EINVAL;
939 rc = -1;
940 goto exit;
943 if ((mode == O_RDWR)
944 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
945 rc = ad_convert(ad, fd);
946 if (rc != 0) {
947 rc = -1;
948 goto exit;
951 * Can't use ad_write() because we might not have a fsp
953 ok = ad_pack(ad);
954 if (!ok) {
955 rc = -1;
956 goto exit;
958 /* FIXME: direct sys_pwrite(), don't have an fsp */
959 len = sys_pwrite(fd, ad->ad_data,
960 AD_DATASZ_DOT_UND, 0);
961 if (len != AD_DATASZ_DOT_UND) {
962 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
963 rc = -1;
964 goto exit;
967 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
968 ADOUBLE_META, NULL);
969 if (meta_ad == NULL) {
970 rc = -1;
971 goto exit;
974 memcpy(ad_entry(meta_ad, ADEID_FINDERI),
975 ad_entry(ad, ADEID_FINDERI),
976 ADEDLEN_FINDERI);
978 rc = ad_write(meta_ad, path);
979 if (rc != 0) {
980 rc = -1;
981 goto exit;
986 DEBUG(10, ("opened AppleDouble: %s\n", path));
988 exit:
989 if (rc != 0) {
990 saved_errno = errno;
991 len = -1;
993 if (opened && fd != -1) {
994 close(fd);
996 TALLOC_FREE(adpath);
997 TALLOC_FREE(meta_ad);
998 if (rc != 0) {
999 errno = saved_errno;
1001 return len;
1005 * Read and unpack an AppleDouble metadata xattr or resource
1007 static ssize_t ad_read(struct adouble *ad, const char *path)
1009 switch (ad->ad_type) {
1010 case ADOUBLE_META:
1011 return ad_header_read_meta(ad, path);
1012 case ADOUBLE_RSRC:
1013 return ad_header_read_rsrc(ad, path);
1014 default:
1015 return -1;
1020 * Allocate a struct adouble without initialiing it
1022 * The struct is either hang of the fsp extension context or if fsp is
1023 * NULL from ctx.
1025 * @param[in] ctx talloc context
1026 * @param[in] handle vfs handle
1027 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1029 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1030 * added as an fsp extension
1032 * @return adouble handle
1034 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1035 adouble_type_t type, files_struct *fsp)
1037 int rc = 0;
1038 size_t adsize = 0;
1039 struct adouble *ad;
1040 struct fruit_config_data *config;
1042 SMB_VFS_HANDLE_GET_DATA(handle, config,
1043 struct fruit_config_data, return NULL);
1045 switch (type) {
1046 case ADOUBLE_META:
1047 adsize = AD_DATASZ_XATTR;
1048 break;
1049 case ADOUBLE_RSRC:
1050 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1051 adsize = AD_DATASZ_DOT_UND;
1053 break;
1054 default:
1055 return NULL;
1058 if (!fsp) {
1059 ad = talloc_zero(ctx, struct adouble);
1060 if (ad == NULL) {
1061 rc = -1;
1062 goto exit;
1064 if (adsize) {
1065 ad->ad_data = talloc_zero_array(ad, char, adsize);
1067 } else {
1068 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1069 struct adouble,
1070 NULL);
1071 if (ad == NULL) {
1072 rc = -1;
1073 goto exit;
1075 if (adsize) {
1076 ad->ad_data = talloc_zero_array(
1077 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1078 char, adsize);
1080 ad->ad_fsp = fsp;
1083 if (adsize && ad->ad_data == NULL) {
1084 rc = -1;
1085 goto exit;
1087 ad->ad_handle = handle;
1088 ad->ad_type = type;
1089 ad->ad_magic = AD_MAGIC;
1090 ad->ad_version = AD_VERSION;
1092 exit:
1093 if (rc != 0) {
1094 TALLOC_FREE(ad);
1096 return ad;
1100 * Allocate and initialize a new struct adouble
1102 * @param[in] ctx talloc context
1103 * @param[in] handle vfs handle
1104 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1105 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1107 * @return adouble handle, initialized
1109 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1110 adouble_type_t type, files_struct *fsp)
1112 int rc = 0;
1113 const struct ad_entry_order *eid;
1114 struct adouble *ad = NULL;
1115 struct fruit_config_data *config;
1116 time_t t = time(NULL);
1118 SMB_VFS_HANDLE_GET_DATA(handle, config,
1119 struct fruit_config_data, return NULL);
1121 switch (type) {
1122 case ADOUBLE_META:
1123 eid = entry_order_meta_xattr;
1124 break;
1125 case ADOUBLE_RSRC:
1126 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1127 eid = entry_order_dot_und;
1128 } else {
1129 eid = entry_order_rsrc_xattr;
1131 break;
1132 default:
1133 return NULL;
1136 ad = ad_alloc(ctx, handle, type, fsp);
1137 if (ad == NULL) {
1138 return NULL;
1141 while (eid->id) {
1142 ad->ad_eid[eid->id].ade_off = eid->offset;
1143 ad->ad_eid[eid->id].ade_len = eid->len;
1144 eid++;
1147 /* put something sane in the date fields */
1148 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1149 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1150 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1151 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1153 if (rc != 0) {
1154 TALLOC_FREE(ad);
1156 return ad;
1160 * Return AppleDouble data for a file
1162 * @param[in] ctx talloc context
1163 * @param[in] handle vfs handle
1164 * @param[in] path pathname to file or directory
1165 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1167 * @return talloced struct adouble or NULL on error
1169 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1170 const char *path, adouble_type_t type)
1172 int rc = 0;
1173 ssize_t len;
1174 struct adouble *ad = NULL;
1176 DEBUG(10, ("ad_get(%s) called for %s\n",
1177 type == ADOUBLE_META ? "meta" : "rsrc", path));
1179 ad = ad_alloc(ctx, handle, type, NULL);
1180 if (ad == NULL) {
1181 rc = -1;
1182 goto exit;
1185 len = ad_read(ad, path);
1186 if (len == -1) {
1187 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1188 rc = -1;
1189 goto exit;
1192 exit:
1193 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1194 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1196 if (rc != 0) {
1197 TALLOC_FREE(ad);
1199 return ad;
1203 * Set AppleDouble metadata on a file or directory
1205 * @param[in] ad adouble handle
1207 * @param[in] path pathname to file or directory, may be NULL for a
1208 * resource fork
1210 * @return status code, 0 means success
1212 static int ad_write(struct adouble *ad, const char *path)
1214 int rc = 0;
1215 ssize_t len;
1216 bool ok;
1218 ok = ad_pack(ad);
1219 if (!ok) {
1220 return -1;
1223 switch (ad->ad_type) {
1224 case ADOUBLE_META:
1225 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1226 AFPINFO_EA_NETATALK, ad->ad_data,
1227 AD_DATASZ_XATTR, 0);
1228 break;
1229 case ADOUBLE_RSRC:
1230 if ((ad->ad_fsp == NULL)
1231 || (ad->ad_fsp->fh == NULL)
1232 || (ad->ad_fsp->fh->fd == -1)) {
1233 rc = -1;
1234 goto exit;
1236 /* FIXME: direct sys_pwrite(), don't have an fsp */
1237 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1238 talloc_get_size(ad->ad_data), 0);
1239 if (len != talloc_get_size(ad->ad_data)) {
1240 DEBUG(1, ("short write on %s: %zd",
1241 fsp_str_dbg(ad->ad_fsp), len));
1242 rc = -1;
1243 goto exit;
1245 break;
1246 default:
1247 return -1;
1249 exit:
1250 return rc;
1253 /*****************************************************************************
1254 * Helper functions
1255 *****************************************************************************/
1257 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1259 if (strncasecmp_m(smb_fname->stream_name,
1260 AFPINFO_STREAM_NAME,
1261 strlen(AFPINFO_STREAM_NAME)) == 0) {
1262 return true;
1264 return false;
1267 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1269 if (strncasecmp_m(smb_fname->stream_name,
1270 AFPRESOURCE_STREAM_NAME,
1271 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1272 return true;
1274 return false;
1278 * Test whether stream is an Apple stream, not used atm
1280 #if 0
1281 static bool is_apple_stream(const struct smb_filename *smb_fname)
1283 if (is_afpinfo_stream(smb_fname)) {
1284 return true;
1286 if (is_afpresource_stream(smb_fname)) {
1287 return true;
1289 return false;
1291 #endif
1294 * Initialize config struct from our smb.conf config parameters
1296 static int init_fruit_config(vfs_handle_struct *handle)
1298 struct fruit_config_data *config;
1299 int enumval;
1301 config = talloc_zero(handle->conn, struct fruit_config_data);
1302 if (!config) {
1303 DEBUG(1, ("talloc_zero() failed\n"));
1304 errno = ENOMEM;
1305 return -1;
1308 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1309 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1310 if (enumval == -1) {
1311 DEBUG(1, ("value for %s: ressource type unknown\n",
1312 FRUIT_PARAM_TYPE_NAME));
1313 return -1;
1315 config->rsrc = (enum fruit_rsrc)enumval;
1317 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1318 "metadata", fruit_meta, FRUIT_META_NETATALK);
1319 if (enumval == -1) {
1320 DEBUG(1, ("value for %s: metadata type unknown\n",
1321 FRUIT_PARAM_TYPE_NAME));
1322 return -1;
1324 config->meta = (enum fruit_meta)enumval;
1326 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1327 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1328 if (enumval == -1) {
1329 DEBUG(1, ("value for %s: locking type unknown\n",
1330 FRUIT_PARAM_TYPE_NAME));
1331 return -1;
1333 config->locking = (enum fruit_locking)enumval;
1335 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1336 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1337 if (enumval == -1) {
1338 DEBUG(1, ("value for %s: encoding type unknown\n",
1339 FRUIT_PARAM_TYPE_NAME));
1340 return -1;
1342 config->encoding = (enum fruit_encoding)enumval;
1344 config->veto_appledouble = lp_parm_bool(
1345 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1346 "veto_appledouble", true);
1348 config->use_aapl = lp_parm_bool(
1349 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1351 config->unix_info_enabled = lp_parm_bool(
1352 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1354 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1355 "copyfile", false);
1357 config->readdir_attr_rsize = lp_parm_bool(
1358 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1360 config->readdir_attr_finder_info = lp_parm_bool(
1361 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1363 config->readdir_attr_max_access = lp_parm_bool(
1364 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1366 SMB_VFS_HANDLE_SET_DATA(handle, config,
1367 NULL, struct fruit_config_data,
1368 return -1);
1370 return 0;
1374 * Prepend "._" to a basename
1376 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1378 char *parent;
1379 const char *base;
1381 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1382 return -1;
1385 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1386 if (*path_out == NULL) {
1387 return -1;
1390 return 0;
1394 * Allocate and initialize an AfpInfo struct
1396 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1398 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1399 if (ai == NULL) {
1400 return NULL;
1402 ai->afpi_Signature = AFP_Signature;
1403 ai->afpi_Version = AFP_Version;
1404 ai->afpi_BackupTime = AD_DATE_START;
1405 return ai;
1409 * Pack an AfpInfo struct into a buffer
1411 * Buffer size must be at least AFP_INFO_SIZE
1412 * Returns size of packed buffer
1414 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1416 memset(buf, 0, AFP_INFO_SIZE);
1418 RSIVAL(buf, 0, ai->afpi_Signature);
1419 RSIVAL(buf, 4, ai->afpi_Version);
1420 RSIVAL(buf, 12, ai->afpi_BackupTime);
1421 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1423 return AFP_INFO_SIZE;
1427 * Unpack a buffer into a AfpInfo structure
1429 * Buffer size must be at least AFP_INFO_SIZE
1430 * Returns allocated AfpInfo struct
1432 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1434 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1435 if (ai == NULL) {
1436 return NULL;
1439 ai->afpi_Signature = RIVAL(data, 0);
1440 ai->afpi_Version = RIVAL(data, 4);
1441 ai->afpi_BackupTime = RIVAL(data, 12);
1442 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1443 sizeof(ai->afpi_FinderInfo));
1445 if (ai->afpi_Signature != AFP_Signature
1446 || ai->afpi_Version != AFP_Version) {
1447 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1448 TALLOC_FREE(ai);
1451 return ai;
1455 * Fake an inode number from the md5 hash of the (xattr) name
1457 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1459 MD5_CTX ctx;
1460 unsigned char hash[16];
1461 SMB_INO_T result;
1462 char *upper_sname;
1464 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1465 SMB_ASSERT(upper_sname != NULL);
1467 MD5Init(&ctx);
1468 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1469 sizeof(sbuf->st_ex_dev));
1470 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1471 sizeof(sbuf->st_ex_ino));
1472 MD5Update(&ctx, (unsigned char *)upper_sname,
1473 talloc_get_size(upper_sname)-1);
1474 MD5Final(hash, &ctx);
1476 TALLOC_FREE(upper_sname);
1478 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1479 memcpy(&result, hash, sizeof(result));
1481 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1482 sname, (unsigned long long)result));
1484 return result;
1488 * Ensure ad_fsp is still valid
1490 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1492 if (ad->ad_fsp == fsp) {
1493 return true;
1495 ad->ad_fsp = fsp;
1497 return true;
1500 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1501 struct stream_struct **streams,
1502 const char *name, off_t size,
1503 off_t alloc_size)
1505 struct stream_struct *tmp;
1507 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1508 (*num_streams)+1);
1509 if (tmp == NULL) {
1510 return false;
1513 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1514 if (tmp[*num_streams].name == NULL) {
1515 return false;
1518 tmp[*num_streams].size = size;
1519 tmp[*num_streams].alloc_size = alloc_size;
1521 *streams = tmp;
1522 *num_streams += 1;
1523 return true;
1526 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1527 struct stream_struct **streams,
1528 const char *name)
1530 struct stream_struct *tmp = *streams;
1531 int i;
1533 if (*num_streams == 0) {
1534 return true;
1537 for (i = 0; i < *num_streams; i++) {
1538 if (strequal_m(tmp[i].name, name)) {
1539 break;
1543 if (i == *num_streams) {
1544 return true;
1547 TALLOC_FREE(tmp[i].name);
1548 if (*num_streams - 1 > i) {
1549 memmove(&tmp[i], &tmp[i+1],
1550 (*num_streams - i - 1) * sizeof(struct stream_struct));
1553 *num_streams -= 1;
1554 return true;
1557 static bool empty_finderinfo(const struct adouble *ad)
1560 char emptybuf[ADEDLEN_FINDERI] = {0};
1561 if (memcmp(emptybuf,
1562 ad_entry(ad, ADEID_FINDERI),
1563 ADEDLEN_FINDERI) == 0) {
1564 return true;
1566 return false;
1570 * Update btime with btime from Netatalk
1572 static void update_btime(vfs_handle_struct *handle,
1573 struct smb_filename *smb_fname)
1575 uint32_t t;
1576 struct timespec creation_time = {0};
1577 struct adouble *ad;
1579 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1580 if (ad == NULL) {
1581 return;
1583 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1584 TALLOC_FREE(ad);
1585 return;
1587 TALLOC_FREE(ad);
1589 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1590 update_stat_ex_create_time(&smb_fname->st, creation_time);
1592 return;
1596 * Map an access mask to a Netatalk single byte byte range lock
1598 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1599 uint32_t access_mask)
1601 off_t offset;
1603 switch (access_mask) {
1604 case FILE_READ_DATA:
1605 offset = AD_FILELOCK_OPEN_RD;
1606 break;
1608 case FILE_WRITE_DATA:
1609 case FILE_APPEND_DATA:
1610 offset = AD_FILELOCK_OPEN_WR;
1611 break;
1613 default:
1614 offset = AD_FILELOCK_OPEN_NONE;
1615 break;
1618 if (fork_type == APPLE_FORK_RSRC) {
1619 if (offset == AD_FILELOCK_OPEN_NONE) {
1620 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1621 } else {
1622 offset += 2;
1626 return offset;
1630 * Map a deny mode to a Netatalk brl
1632 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1633 uint32_t deny_mode)
1635 off_t offset;
1637 switch (deny_mode) {
1638 case DENY_READ:
1639 offset = AD_FILELOCK_DENY_RD;
1640 break;
1642 case DENY_WRITE:
1643 offset = AD_FILELOCK_DENY_WR;
1644 break;
1646 default:
1647 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1650 if (fork_type == APPLE_FORK_RSRC) {
1651 offset += 2;
1654 return offset;
1658 * Call fcntl() with an exclusive F_GETLK request in order to
1659 * determine if there's an exisiting shared lock
1661 * @return true if the requested lock was found or any error occured
1662 * false if the lock was not found
1664 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1666 bool result;
1667 off_t offset = in_offset;
1668 off_t len = 1;
1669 int type = F_WRLCK;
1670 pid_t pid;
1672 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1673 if (result == false) {
1674 return true;
1677 if (type != F_UNLCK) {
1678 return true;
1681 return false;
1684 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1685 files_struct *fsp,
1686 uint32_t access_mask,
1687 uint32_t deny_mode)
1689 NTSTATUS status = NT_STATUS_OK;
1690 struct byte_range_lock *br_lck = NULL;
1691 bool open_for_reading, open_for_writing, deny_read, deny_write;
1692 off_t off;
1694 /* FIXME: hardcoded data fork, add resource fork */
1695 enum apple_fork fork_type = APPLE_FORK_DATA;
1697 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1698 fsp_str_dbg(fsp),
1699 access_mask & FILE_READ_DATA ? "READ" :"-",
1700 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1701 deny_mode & DENY_READ ? "DENY_READ" : "-",
1702 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1705 * Check read access and deny read mode
1707 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1708 /* Check access */
1709 open_for_reading = test_netatalk_lock(
1710 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1712 deny_read = test_netatalk_lock(
1713 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1715 DEBUG(10, ("read: %s, deny_write: %s\n",
1716 open_for_reading == true ? "yes" : "no",
1717 deny_read == true ? "yes" : "no"));
1719 if (((access_mask & FILE_READ_DATA) && deny_read)
1720 || ((deny_mode & DENY_READ) && open_for_reading)) {
1721 return NT_STATUS_SHARING_VIOLATION;
1724 /* Set locks */
1725 if (access_mask & FILE_READ_DATA) {
1726 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1727 br_lck = do_lock(
1728 handle->conn->sconn->msg_ctx, fsp,
1729 fsp->op->global->open_persistent_id, 1, off,
1730 READ_LOCK, POSIX_LOCK, false,
1731 &status, NULL);
1733 if (!NT_STATUS_IS_OK(status)) {
1734 return status;
1736 TALLOC_FREE(br_lck);
1739 if (deny_mode & DENY_READ) {
1740 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1741 br_lck = do_lock(
1742 handle->conn->sconn->msg_ctx, fsp,
1743 fsp->op->global->open_persistent_id, 1, off,
1744 READ_LOCK, POSIX_LOCK, false,
1745 &status, NULL);
1747 if (!NT_STATUS_IS_OK(status)) {
1748 return status;
1750 TALLOC_FREE(br_lck);
1755 * Check write access and deny write mode
1757 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1758 /* Check access */
1759 open_for_writing = test_netatalk_lock(
1760 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1762 deny_write = test_netatalk_lock(
1763 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1765 DEBUG(10, ("write: %s, deny_write: %s\n",
1766 open_for_writing == true ? "yes" : "no",
1767 deny_write == true ? "yes" : "no"));
1769 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1770 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1771 return NT_STATUS_SHARING_VIOLATION;
1774 /* Set locks */
1775 if (access_mask & FILE_WRITE_DATA) {
1776 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1777 br_lck = do_lock(
1778 handle->conn->sconn->msg_ctx, fsp,
1779 fsp->op->global->open_persistent_id, 1, off,
1780 READ_LOCK, POSIX_LOCK, false,
1781 &status, NULL);
1783 if (!NT_STATUS_IS_OK(status)) {
1784 return status;
1786 TALLOC_FREE(br_lck);
1789 if (deny_mode & DENY_WRITE) {
1790 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1791 br_lck = do_lock(
1792 handle->conn->sconn->msg_ctx, fsp,
1793 fsp->op->global->open_persistent_id, 1, off,
1794 READ_LOCK, POSIX_LOCK, false,
1795 &status, NULL);
1797 if (!NT_STATUS_IS_OK(status)) {
1798 return status;
1800 TALLOC_FREE(br_lck);
1804 TALLOC_FREE(br_lck);
1806 return status;
1809 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1810 struct smb_request *req,
1811 const struct smb2_create_blobs *in_context_blobs,
1812 struct smb2_create_blobs *out_context_blobs)
1814 struct fruit_config_data *config;
1815 NTSTATUS status;
1816 struct smb2_create_blob *aapl = NULL;
1817 uint32_t cmd;
1818 bool ok;
1819 uint8_t p[16];
1820 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1821 uint64_t req_bitmap, client_caps;
1822 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1823 smb_ucs2_t *model;
1824 size_t modellen;
1826 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1827 return NT_STATUS_UNSUCCESSFUL);
1829 if (!config->use_aapl
1830 || in_context_blobs == NULL
1831 || out_context_blobs == NULL) {
1832 return NT_STATUS_OK;
1835 aapl = smb2_create_blob_find(in_context_blobs,
1836 SMB2_CREATE_TAG_AAPL);
1837 if (aapl == NULL) {
1838 return NT_STATUS_OK;
1841 if (aapl->data.length != 24) {
1842 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1843 (uintmax_t)aapl->data.length));
1844 return NT_STATUS_INVALID_PARAMETER;
1847 cmd = IVAL(aapl->data.data, 0);
1848 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1849 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1850 return NT_STATUS_INVALID_PARAMETER;
1853 req_bitmap = BVAL(aapl->data.data, 8);
1854 client_caps = BVAL(aapl->data.data, 16);
1856 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1857 SIVAL(p, 4, 0);
1858 SBVAL(p, 8, req_bitmap);
1859 ok = data_blob_append(req, &blob, p, 16);
1860 if (!ok) {
1861 return NT_STATUS_UNSUCCESSFUL;
1864 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1865 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1866 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1867 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1868 config->readdir_attr_enabled = true;
1871 if (config->use_copyfile) {
1872 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
1873 config->copyfile_enabled = true;
1877 * The client doesn't set the flag, so we can't check
1878 * for it and just set it unconditionally
1880 if (config->unix_info_enabled) {
1881 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1884 SBVAL(p, 0, server_caps);
1885 ok = data_blob_append(req, &blob, p, 8);
1886 if (!ok) {
1887 return NT_STATUS_UNSUCCESSFUL;
1891 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1892 SBVAL(p, 0,
1893 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1894 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1895 ok = data_blob_append(req, &blob, p, 8);
1896 if (!ok) {
1897 return NT_STATUS_UNSUCCESSFUL;
1901 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1902 ok = convert_string_talloc(req,
1903 CH_UNIX, CH_UTF16LE,
1904 "Samba", strlen("Samba"),
1905 &model, &modellen);
1906 if (!ok) {
1907 return NT_STATUS_UNSUCCESSFUL;
1910 SIVAL(p, 0, 0);
1911 SIVAL(p + 4, 0, modellen);
1912 ok = data_blob_append(req, &blob, p, 8);
1913 if (!ok) {
1914 talloc_free(model);
1915 return NT_STATUS_UNSUCCESSFUL;
1918 ok = data_blob_append(req, &blob, model, modellen);
1919 talloc_free(model);
1920 if (!ok) {
1921 return NT_STATUS_UNSUCCESSFUL;
1925 status = smb2_create_blob_add(out_context_blobs,
1926 out_context_blobs,
1927 SMB2_CREATE_TAG_AAPL,
1928 blob);
1930 return status;
1933 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1934 const struct smb_filename *smb_fname,
1935 struct readdir_attr_data *attr_data)
1937 NTSTATUS status = NT_STATUS_OK;
1938 uint32_t date_added;
1939 struct adouble *ad = NULL;
1940 struct fruit_config_data *config = NULL;
1942 SMB_VFS_HANDLE_GET_DATA(handle, config,
1943 struct fruit_config_data,
1944 return NT_STATUS_UNSUCCESSFUL);
1947 /* Ensure we return a default value in the creation_date field */
1948 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1951 * Resource fork length
1954 if (config->readdir_attr_rsize) {
1955 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1956 ADOUBLE_RSRC);
1957 if (ad) {
1958 attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1959 ad, ADEID_RFORK);
1960 TALLOC_FREE(ad);
1965 * FinderInfo
1968 if (config->readdir_attr_finder_info) {
1969 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1970 ADOUBLE_META);
1971 if (ad) {
1972 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1973 /* finder_type */
1974 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1975 ad_entry(ad, ADEID_FINDERI), 4);
1977 /* finder_creator */
1978 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1979 ad_entry(ad, ADEID_FINDERI) + 4, 4);
1982 /* finder_flags */
1983 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1984 ad_entry(ad, ADEID_FINDERI) + 8, 2);
1986 /* finder_ext_flags */
1987 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1988 ad_entry(ad, ADEID_FINDERI) + 24, 2);
1990 /* creation date */
1991 date_added = convert_time_t_to_uint32_t(
1992 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1993 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1995 TALLOC_FREE(ad);
1999 TALLOC_FREE(ad);
2000 return status;
2003 /* Search MS NFS style ACE with UNIX mode */
2004 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2005 files_struct *fsp,
2006 const struct security_descriptor *psd,
2007 mode_t *pmode,
2008 bool *pdo_chmod)
2010 int i;
2011 struct fruit_config_data *config = NULL;
2013 *pdo_chmod = false;
2015 SMB_VFS_HANDLE_GET_DATA(handle, config,
2016 struct fruit_config_data,
2017 return NT_STATUS_UNSUCCESSFUL);
2019 if (psd->dacl == NULL || !config->unix_info_enabled) {
2020 return NT_STATUS_OK;
2023 for (i = 0; i < psd->dacl->num_aces; i++) {
2024 if (dom_sid_compare_domain(
2025 &global_sid_Unix_NFS_Mode,
2026 &psd->dacl->aces[i].trustee) == 0) {
2027 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2028 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2029 *pdo_chmod = true;
2031 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2032 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2033 break;
2037 return NT_STATUS_OK;
2040 /****************************************************************************
2041 * VFS ops
2042 ****************************************************************************/
2044 static int fruit_connect(vfs_handle_struct *handle,
2045 const char *service,
2046 const char *user)
2048 int rc;
2049 char *list = NULL, *newlist = NULL;
2050 struct fruit_config_data *config;
2052 DEBUG(10, ("fruit_connect\n"));
2054 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2055 if (rc < 0) {
2056 return rc;
2059 rc = init_fruit_config(handle);
2060 if (rc != 0) {
2061 return rc;
2064 SMB_VFS_HANDLE_GET_DATA(handle, config,
2065 struct fruit_config_data, return -1);
2067 if (config->veto_appledouble) {
2068 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2070 if (list) {
2071 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2072 newlist = talloc_asprintf(
2073 list,
2074 "%s/" ADOUBLE_NAME_PREFIX "*/",
2075 list);
2076 lp_do_parameter(SNUM(handle->conn),
2077 "veto files",
2078 newlist);
2080 } else {
2081 lp_do_parameter(SNUM(handle->conn),
2082 "veto files",
2083 "/" ADOUBLE_NAME_PREFIX "*/");
2086 TALLOC_FREE(list);
2089 if (config->encoding == FRUIT_ENC_NATIVE) {
2090 lp_do_parameter(
2091 SNUM(handle->conn),
2092 "catia:mappings",
2093 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2094 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2095 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2096 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2097 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2098 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2099 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2100 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2101 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2102 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2103 "0x0d:0xf00d");
2106 return rc;
2109 static int fruit_open_meta(vfs_handle_struct *handle,
2110 struct smb_filename *smb_fname,
2111 files_struct *fsp, int flags, mode_t mode)
2113 int rc = 0;
2114 struct fruit_config_data *config = NULL;
2115 struct smb_filename *smb_fname_base = NULL;
2116 int baseflags;
2117 int hostfd = -1;
2118 struct adouble *ad = NULL;
2120 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
2122 SMB_VFS_HANDLE_GET_DATA(handle, config,
2123 struct fruit_config_data, return -1);
2125 if (config->meta == FRUIT_META_STREAM) {
2126 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2129 /* Create an smb_filename with stream_name == NULL. */
2130 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2131 smb_fname->base_name, NULL, NULL);
2133 if (smb_fname_base == NULL) {
2134 errno = ENOMEM;
2135 rc = -1;
2136 goto exit;
2140 * We use baseflags to turn off nasty side-effects when opening the
2141 * underlying file.
2143 baseflags = flags;
2144 baseflags &= ~O_TRUNC;
2145 baseflags &= ~O_EXCL;
2146 baseflags &= ~O_CREAT;
2148 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2149 baseflags, mode);
2152 * It is legit to open a stream on a directory, but the base
2153 * fd has to be read-only.
2155 if ((hostfd == -1) && (errno == EISDIR)) {
2156 baseflags &= ~O_ACCMODE;
2157 baseflags |= O_RDONLY;
2158 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2159 baseflags, mode);
2162 TALLOC_FREE(smb_fname_base);
2164 if (hostfd == -1) {
2165 rc = -1;
2166 goto exit;
2169 if (flags & (O_CREAT | O_TRUNC)) {
2171 * The attribute does not exist or needs to be truncated,
2172 * create an AppleDouble EA
2174 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2175 handle, ADOUBLE_META, fsp);
2176 if (ad == NULL) {
2177 rc = -1;
2178 goto exit;
2181 rc = ad_write(ad, smb_fname->base_name);
2182 if (rc != 0) {
2183 rc = -1;
2184 goto exit;
2186 } else {
2187 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2188 handle, ADOUBLE_META, fsp);
2189 if (ad == NULL) {
2190 rc = -1;
2191 goto exit;
2193 if (ad_read(ad, smb_fname->base_name) == -1) {
2194 rc = -1;
2195 goto exit;
2199 exit:
2200 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2201 if (rc != 0) {
2202 int saved_errno = errno;
2203 if (hostfd >= 0) {
2205 * BUGBUGBUG -- we would need to call
2206 * fd_close_posix here, but we don't have a
2207 * full fsp yet
2209 fsp->fh->fd = hostfd;
2210 SMB_VFS_CLOSE(fsp);
2212 hostfd = -1;
2213 errno = saved_errno;
2215 return hostfd;
2218 static int fruit_open_rsrc(vfs_handle_struct *handle,
2219 struct smb_filename *smb_fname,
2220 files_struct *fsp, int flags, mode_t mode)
2222 int rc = 0;
2223 struct fruit_config_data *config = NULL;
2224 struct adouble *ad = NULL;
2225 struct smb_filename *smb_fname_base = NULL;
2226 char *adpath = NULL;
2227 int hostfd = -1;
2229 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2231 SMB_VFS_HANDLE_GET_DATA(handle, config,
2232 struct fruit_config_data, return -1);
2234 switch (config->rsrc) {
2235 case FRUIT_RSRC_STREAM:
2236 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2237 case FRUIT_RSRC_XATTR:
2238 #ifdef HAVE_ATTROPEN
2239 hostfd = attropen(smb_fname->base_name,
2240 AFPRESOURCE_EA_NETATALK, flags, mode);
2241 if (hostfd == -1) {
2242 return -1;
2244 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2245 handle, ADOUBLE_RSRC, fsp);
2246 if (ad == NULL) {
2247 rc = -1;
2248 goto exit;
2250 goto exit;
2251 #else
2252 errno = ENOTSUP;
2253 return -1;
2254 #endif
2255 default:
2256 break;
2259 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2260 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2261 if (rc != 0) {
2262 rc = -1;
2263 goto exit;
2267 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2268 /* sorry, but directories don't habe a resource fork */
2269 rc = -1;
2270 goto exit;
2273 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2274 if (rc != 0) {
2275 goto exit;
2278 /* Create an smb_filename with stream_name == NULL. */
2279 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2280 adpath, NULL, NULL);
2281 if (smb_fname_base == NULL) {
2282 errno = ENOMEM;
2283 rc = -1;
2284 goto exit;
2287 /* Sanitize flags */
2288 if (flags & O_WRONLY) {
2289 /* We always need read access for the metadata header too */
2290 flags &= ~O_WRONLY;
2291 flags |= O_RDWR;
2294 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2295 flags, mode);
2296 if (hostfd == -1) {
2297 rc = -1;
2298 goto exit;
2301 /* REVIEW: we need this in ad_write() */
2302 fsp->fh->fd = hostfd;
2304 if (flags & (O_CREAT | O_TRUNC)) {
2305 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2306 handle, ADOUBLE_RSRC, fsp);
2307 if (ad == NULL) {
2308 rc = -1;
2309 goto exit;
2311 rc = ad_write(ad, smb_fname->base_name);
2312 if (rc != 0) {
2313 rc = -1;
2314 goto exit;
2316 } else {
2317 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2318 handle, ADOUBLE_RSRC, fsp);
2319 if (ad == NULL) {
2320 rc = -1;
2321 goto exit;
2323 if (ad_read(ad, smb_fname->base_name) == -1) {
2324 rc = -1;
2325 goto exit;
2329 exit:
2331 TALLOC_FREE(adpath);
2332 TALLOC_FREE(smb_fname_base);
2334 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2335 if (rc != 0) {
2336 int saved_errno = errno;
2337 if (hostfd >= 0) {
2339 * BUGBUGBUG -- we would need to call
2340 * fd_close_posix here, but we don't have a
2341 * full fsp yet
2343 fsp->fh->fd = hostfd;
2344 SMB_VFS_CLOSE(fsp);
2346 hostfd = -1;
2347 errno = saved_errno;
2349 return hostfd;
2352 static int fruit_open(vfs_handle_struct *handle,
2353 struct smb_filename *smb_fname,
2354 files_struct *fsp, int flags, mode_t mode)
2356 DEBUG(10, ("fruit_open called for %s\n",
2357 smb_fname_str_dbg(smb_fname)));
2359 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2360 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2363 if (is_afpinfo_stream(smb_fname)) {
2364 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2365 } else if (is_afpresource_stream(smb_fname)) {
2366 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2369 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2372 static int fruit_rename(struct vfs_handle_struct *handle,
2373 const struct smb_filename *smb_fname_src,
2374 const struct smb_filename *smb_fname_dst)
2376 int rc = -1;
2377 char *src_adouble_path = NULL;
2378 char *dst_adouble_path = NULL;
2379 struct fruit_config_data *config = NULL;
2381 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2383 if (!VALID_STAT(smb_fname_src->st)
2384 || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2385 return rc;
2388 SMB_VFS_HANDLE_GET_DATA(handle, config,
2389 struct fruit_config_data, return -1);
2391 if (config->rsrc == FRUIT_RSRC_XATTR) {
2392 return rc;
2395 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2396 &src_adouble_path);
2397 if (rc != 0) {
2398 goto done;
2400 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2401 &dst_adouble_path);
2402 if (rc != 0) {
2403 goto done;
2406 DEBUG(10, ("fruit_rename: %s -> %s\n",
2407 src_adouble_path, dst_adouble_path));
2409 rc = rename(src_adouble_path, dst_adouble_path);
2410 if (errno == ENOENT) {
2411 rc = 0;
2414 TALLOC_FREE(src_adouble_path);
2415 TALLOC_FREE(dst_adouble_path);
2417 done:
2418 return rc;
2421 static int fruit_unlink(vfs_handle_struct *handle,
2422 const struct smb_filename *smb_fname)
2424 int rc = -1;
2425 struct fruit_config_data *config = NULL;
2427 SMB_VFS_HANDLE_GET_DATA(handle, config,
2428 struct fruit_config_data, return -1);
2430 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2431 char *adp = NULL;
2433 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2434 if (rc != 0) {
2435 return -1;
2438 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2439 return 0;
2443 * 0 byte resource fork streams are not listed by
2444 * vfs_streaminfo, as a result stream cleanup/deletion of file
2445 * deletion doesn't remove the resourcefork stream.
2447 rc = adouble_path(talloc_tos(),
2448 smb_fname->base_name, &adp);
2449 if (rc != 0) {
2450 return -1;
2453 /* FIXME: direct unlink(), missing smb_fname */
2454 DEBUG(1,("fruit_unlink: %s\n", adp));
2455 rc = unlink(adp);
2456 if ((rc == -1) && (errno == ENOENT)) {
2457 rc = 0;
2460 TALLOC_FREE(adp);
2461 return 0;
2464 if (is_afpinfo_stream(smb_fname)) {
2465 if (config->meta == FRUIT_META_STREAM) {
2466 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2467 } else {
2468 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2469 smb_fname->base_name,
2470 AFPINFO_EA_NETATALK);
2473 return rc;
2476 if (is_afpresource_stream(smb_fname)) {
2477 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2478 char *adp = NULL;
2480 rc = adouble_path(talloc_tos(),
2481 smb_fname->base_name, &adp);
2482 if (rc != 0) {
2483 return -1;
2485 /* FIXME: direct unlink(), missing smb_fname */
2486 rc = unlink(adp);
2487 if ((rc == -1) && (errno == ENOENT)) {
2488 rc = 0;
2490 TALLOC_FREE(adp);
2491 } else {
2492 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2493 smb_fname->base_name,
2494 AFPRESOURCE_EA_NETATALK);
2497 return rc;
2500 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2503 return 0;
2506 static int fruit_chmod(vfs_handle_struct *handle,
2507 const char *path,
2508 mode_t mode)
2510 int rc = -1;
2511 char *adp = NULL;
2512 struct fruit_config_data *config = NULL;
2513 SMB_STRUCT_STAT sb;
2515 rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2516 if (rc != 0) {
2517 return rc;
2520 SMB_VFS_HANDLE_GET_DATA(handle, config,
2521 struct fruit_config_data, return -1);
2523 if (config->rsrc == FRUIT_RSRC_XATTR) {
2524 return 0;
2527 /* FIXME: direct sys_lstat(), missing smb_fname */
2528 rc = sys_lstat(path, &sb, false);
2529 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2530 return rc;
2533 rc = adouble_path(talloc_tos(), path, &adp);
2534 if (rc != 0) {
2535 return -1;
2538 DEBUG(10, ("fruit_chmod: %s\n", adp));
2540 rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2541 if (errno == ENOENT) {
2542 rc = 0;
2545 TALLOC_FREE(adp);
2546 return rc;
2549 static int fruit_chown(vfs_handle_struct *handle,
2550 const char *path,
2551 uid_t uid,
2552 gid_t gid)
2554 int rc = -1;
2555 char *adp = NULL;
2556 struct fruit_config_data *config = NULL;
2557 SMB_STRUCT_STAT sb;
2559 rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2560 if (rc != 0) {
2561 return rc;
2564 SMB_VFS_HANDLE_GET_DATA(handle, config,
2565 struct fruit_config_data, return -1);
2567 if (config->rsrc == FRUIT_RSRC_XATTR) {
2568 return rc;
2571 /* FIXME: direct sys_lstat(), missing smb_fname */
2572 rc = sys_lstat(path, &sb, false);
2573 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2574 return rc;
2577 rc = adouble_path(talloc_tos(), path, &adp);
2578 if (rc != 0) {
2579 goto done;
2582 DEBUG(10, ("fruit_chown: %s\n", adp));
2584 rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2585 if (errno == ENOENT) {
2586 rc = 0;
2589 done:
2590 TALLOC_FREE(adp);
2591 return rc;
2594 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2596 DIR *dh = NULL;
2597 struct dirent *de;
2598 struct fruit_config_data *config;
2600 SMB_VFS_HANDLE_GET_DATA(handle, config,
2601 struct fruit_config_data, return -1);
2603 if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2604 goto exit_rmdir;
2608 * Due to there is no way to change bDeleteVetoFiles variable
2609 * from this module, need to clean up ourselves
2611 dh = opendir(path);
2612 if (dh == NULL) {
2613 goto exit_rmdir;
2616 while ((de = readdir(dh)) != NULL) {
2617 if ((strncmp(de->d_name,
2618 ADOUBLE_NAME_PREFIX,
2619 strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2620 char *p = talloc_asprintf(talloc_tos(),
2621 "%s/%s",
2622 path, de->d_name);
2623 if (p == NULL) {
2624 goto exit_rmdir;
2626 DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2627 (void)unlink(p);
2628 TALLOC_FREE(p);
2632 exit_rmdir:
2633 if (dh) {
2634 closedir(dh);
2636 return SMB_VFS_NEXT_RMDIR(handle, path);
2639 static ssize_t fruit_pread(vfs_handle_struct *handle,
2640 files_struct *fsp, void *data,
2641 size_t n, off_t offset)
2643 int rc = 0;
2644 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2645 handle, fsp);
2646 struct fruit_config_data *config = NULL;
2647 AfpInfo *ai = NULL;
2648 ssize_t len;
2649 char *name = NULL;
2650 char *tmp_base_name = NULL;
2651 NTSTATUS status;
2653 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2655 if (!fsp->base_fsp) {
2656 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2659 SMB_VFS_HANDLE_GET_DATA(handle, config,
2660 struct fruit_config_data, return -1);
2662 /* fsp_name is not converted with vfs_catia */
2663 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2664 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2665 fsp->base_fsp->fsp_name->base_name,
2666 vfs_translate_to_unix,
2667 talloc_tos(), &name);
2668 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2669 name = talloc_strdup(talloc_tos(), tmp_base_name);
2670 if (name == NULL) {
2671 rc = -1;
2672 goto exit;
2674 } else if (!NT_STATUS_IS_OK(status)) {
2675 errno = map_errno_from_nt_status(status);
2676 rc = -1;
2677 goto exit;
2679 fsp->base_fsp->fsp_name->base_name = name;
2681 if (ad == NULL) {
2682 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2683 if (len == -1) {
2684 rc = -1;
2685 goto exit;
2687 goto exit;
2690 if (!fruit_fsp_recheck(ad, fsp)) {
2691 rc = -1;
2692 goto exit;
2695 if (ad->ad_type == ADOUBLE_META) {
2696 char afpinfo_buf[AFP_INFO_SIZE];
2697 size_t to_return;
2699 if ((offset < 0) || (offset >= AFP_INFO_SIZE)) {
2700 len = 0;
2701 rc = 0;
2702 goto exit;
2705 to_return = AFP_INFO_SIZE - offset;
2707 ai = afpinfo_new(talloc_tos());
2708 if (ai == NULL) {
2709 rc = -1;
2710 goto exit;
2713 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2714 if (len == -1) {
2715 rc = -1;
2716 goto exit;
2719 memcpy(&ai->afpi_FinderInfo[0],
2720 ad_entry(ad, ADEID_FINDERI),
2721 ADEDLEN_FINDERI);
2722 len = afpinfo_pack(ai, afpinfo_buf);
2723 if (len != AFP_INFO_SIZE) {
2724 rc = -1;
2725 goto exit;
2728 memcpy(data, afpinfo_buf + offset, to_return);
2729 len = to_return;
2730 } else {
2731 len = SMB_VFS_NEXT_PREAD(
2732 handle, fsp, data, n,
2733 offset + ad_getentryoff(ad, ADEID_RFORK));
2734 if (len == -1) {
2735 rc = -1;
2736 goto exit;
2739 exit:
2740 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2741 TALLOC_FREE(name);
2742 TALLOC_FREE(ai);
2743 if (rc != 0) {
2744 len = -1;
2746 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2747 return len;
2750 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2751 files_struct *fsp, const void *data,
2752 size_t n, off_t offset)
2754 int rc = 0;
2755 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2756 handle, fsp);
2757 struct fruit_config_data *config = NULL;
2758 AfpInfo *ai = NULL;
2759 ssize_t len;
2760 char *name = NULL;
2761 char *tmp_base_name = NULL;
2762 NTSTATUS status;
2764 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2766 if (!fsp->base_fsp) {
2767 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2770 SMB_VFS_HANDLE_GET_DATA(handle, config,
2771 struct fruit_config_data, return -1);
2773 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2774 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2775 fsp->base_fsp->fsp_name->base_name,
2776 vfs_translate_to_unix,
2777 talloc_tos(), &name);
2778 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2779 name = talloc_strdup(talloc_tos(), tmp_base_name);
2780 if (name == NULL) {
2781 rc = -1;
2782 goto exit;
2784 } else if (!NT_STATUS_IS_OK(status)) {
2785 errno = map_errno_from_nt_status(status);
2786 rc = -1;
2787 goto exit;
2789 fsp->base_fsp->fsp_name->base_name = name;
2791 if (ad == NULL) {
2792 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2793 if (len != n) {
2794 rc = -1;
2795 goto exit;
2797 goto exit;
2800 if (!fruit_fsp_recheck(ad, fsp)) {
2801 rc = -1;
2802 goto exit;
2805 if (ad->ad_type == ADOUBLE_META) {
2806 if (n != AFP_INFO_SIZE || offset != 0) {
2807 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2808 (intmax_t)offset, (intmax_t)n));
2809 rc = -1;
2810 goto exit;
2812 ai = afpinfo_unpack(talloc_tos(), data);
2813 if (ai == NULL) {
2814 rc = -1;
2815 goto exit;
2817 memcpy(ad_entry(ad, ADEID_FINDERI),
2818 &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2819 rc = ad_write(ad, name);
2820 } else {
2821 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2822 offset + ad_getentryoff(ad, ADEID_RFORK));
2823 if (len != n) {
2824 rc = -1;
2825 goto exit;
2828 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2829 rc = ad_read(ad, name);
2830 if (rc == -1) {
2831 goto exit;
2833 rc = 0;
2835 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2836 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2837 rc = ad_write(ad, name);
2842 exit:
2843 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2844 TALLOC_FREE(name);
2845 TALLOC_FREE(ai);
2846 if (rc != 0) {
2847 return -1;
2849 return n;
2853 * Helper to stat/lstat the base file of an smb_fname.
2855 static int fruit_stat_base(vfs_handle_struct *handle,
2856 struct smb_filename *smb_fname,
2857 bool follow_links)
2859 char *tmp_stream_name;
2860 int rc;
2862 tmp_stream_name = smb_fname->stream_name;
2863 smb_fname->stream_name = NULL;
2864 if (follow_links) {
2865 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2866 } else {
2867 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2869 smb_fname->stream_name = tmp_stream_name;
2870 return rc;
2873 static int fruit_stat_meta(vfs_handle_struct *handle,
2874 struct smb_filename *smb_fname,
2875 bool follow_links)
2877 /* Populate the stat struct with info from the base file. */
2878 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2879 return -1;
2881 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2882 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2883 smb_fname->stream_name);
2884 return 0;
2887 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2888 struct smb_filename *smb_fname,
2889 bool follow_links)
2892 struct adouble *ad = NULL;
2894 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2895 smb_fname_str_dbg(smb_fname)));
2897 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2898 if (ad == NULL) {
2899 errno = ENOENT;
2900 return -1;
2903 /* Populate the stat struct with info from the base file. */
2904 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2905 TALLOC_FREE(ad);
2906 return -1;
2909 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2910 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2911 smb_fname->stream_name);
2912 TALLOC_FREE(ad);
2913 return 0;
2916 static int fruit_stat(vfs_handle_struct *handle,
2917 struct smb_filename *smb_fname)
2919 int rc = -1;
2921 DEBUG(10, ("fruit_stat called for %s\n",
2922 smb_fname_str_dbg(smb_fname)));
2924 if (!is_ntfs_stream_smb_fname(smb_fname)
2925 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2926 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2927 if (rc == 0) {
2928 update_btime(handle, smb_fname);
2930 return rc;
2934 * Note if lp_posix_paths() is true, we can never
2935 * get here as is_ntfs_stream_smb_fname() is
2936 * always false. So we never need worry about
2937 * not following links here.
2940 if (is_afpinfo_stream(smb_fname)) {
2941 rc = fruit_stat_meta(handle, smb_fname, true);
2942 } else if (is_afpresource_stream(smb_fname)) {
2943 rc = fruit_stat_rsrc(handle, smb_fname, true);
2944 } else {
2945 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2948 if (rc == 0) {
2949 update_btime(handle, smb_fname);
2950 smb_fname->st.st_ex_mode &= ~S_IFMT;
2951 smb_fname->st.st_ex_mode |= S_IFREG;
2952 smb_fname->st.st_ex_blocks =
2953 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2955 return rc;
2958 static int fruit_lstat(vfs_handle_struct *handle,
2959 struct smb_filename *smb_fname)
2961 int rc = -1;
2963 DEBUG(10, ("fruit_lstat called for %s\n",
2964 smb_fname_str_dbg(smb_fname)));
2966 if (!is_ntfs_stream_smb_fname(smb_fname)
2967 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2968 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2969 if (rc == 0) {
2970 update_btime(handle, smb_fname);
2972 return rc;
2975 if (is_afpinfo_stream(smb_fname)) {
2976 rc = fruit_stat_meta(handle, smb_fname, false);
2977 } else if (is_afpresource_stream(smb_fname)) {
2978 rc = fruit_stat_rsrc(handle, smb_fname, false);
2979 } else {
2980 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2983 if (rc == 0) {
2984 update_btime(handle, smb_fname);
2985 smb_fname->st.st_ex_mode &= ~S_IFMT;
2986 smb_fname->st.st_ex_mode |= S_IFREG;
2987 smb_fname->st.st_ex_blocks =
2988 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2990 return rc;
2993 static int fruit_fstat_meta(vfs_handle_struct *handle,
2994 files_struct *fsp,
2995 SMB_STRUCT_STAT *sbuf)
2997 DEBUG(10, ("fruit_fstat_meta called for %s\n",
2998 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3000 /* Populate the stat struct with info from the base file. */
3001 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3002 return -1;
3004 *sbuf = fsp->base_fsp->fsp_name->st;
3005 sbuf->st_ex_size = AFP_INFO_SIZE;
3006 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3008 return 0;
3011 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3012 SMB_STRUCT_STAT *sbuf)
3014 struct fruit_config_data *config;
3015 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3016 handle, fsp);
3018 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
3019 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3021 SMB_VFS_HANDLE_GET_DATA(handle, config,
3022 struct fruit_config_data, return -1);
3024 if (config->rsrc == FRUIT_RSRC_STREAM) {
3025 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3028 /* Populate the stat struct with info from the base file. */
3029 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3030 return -1;
3032 *sbuf = fsp->base_fsp->fsp_name->st;
3033 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3034 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3036 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3037 smb_fname_str_dbg(fsp->fsp_name),
3038 (ssize_t)sbuf->st_ex_size));
3040 return 0;
3043 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3044 SMB_STRUCT_STAT *sbuf)
3046 int rc;
3047 char *name = NULL;
3048 char *tmp_base_name = NULL;
3049 NTSTATUS status;
3050 struct adouble *ad = (struct adouble *)
3051 VFS_FETCH_FSP_EXTENSION(handle, fsp);
3053 DEBUG(10, ("fruit_fstat called for %s\n",
3054 smb_fname_str_dbg(fsp->fsp_name)));
3056 if (fsp->base_fsp) {
3057 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3058 /* fsp_name is not converted with vfs_catia */
3059 status = SMB_VFS_TRANSLATE_NAME(
3060 handle->conn,
3061 fsp->base_fsp->fsp_name->base_name,
3062 vfs_translate_to_unix,
3063 talloc_tos(), &name);
3065 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3066 name = talloc_strdup(talloc_tos(), tmp_base_name);
3067 if (name == NULL) {
3068 rc = -1;
3069 goto exit;
3071 } else if (!NT_STATUS_IS_OK(status)) {
3072 errno = map_errno_from_nt_status(status);
3073 rc = -1;
3074 goto exit;
3076 fsp->base_fsp->fsp_name->base_name = name;
3079 if (ad == NULL || fsp->base_fsp == NULL) {
3080 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3081 goto exit;
3084 if (!fruit_fsp_recheck(ad, fsp)) {
3085 rc = -1;
3086 goto exit;
3089 switch (ad->ad_type) {
3090 case ADOUBLE_META:
3091 rc = fruit_fstat_meta(handle, fsp, sbuf);
3092 break;
3093 case ADOUBLE_RSRC:
3094 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
3095 break;
3096 default:
3097 DEBUG(10, ("fruit_fstat %s: bad type\n",
3098 smb_fname_str_dbg(fsp->fsp_name)));
3099 rc = -1;
3100 goto exit;
3103 if (rc == 0) {
3104 sbuf->st_ex_mode &= ~S_IFMT;
3105 sbuf->st_ex_mode |= S_IFREG;
3106 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3109 exit:
3110 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3111 smb_fname_str_dbg(fsp->fsp_name),
3112 (ssize_t)sbuf->st_ex_size));
3113 if (tmp_base_name) {
3114 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3116 TALLOC_FREE(name);
3117 return rc;
3120 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3121 struct files_struct *fsp,
3122 const char *fname,
3123 TALLOC_CTX *mem_ctx,
3124 unsigned int *pnum_streams,
3125 struct stream_struct **pstreams)
3127 struct fruit_config_data *config = NULL;
3128 struct smb_filename *smb_fname = NULL;
3129 struct adouble *ad = NULL;
3130 NTSTATUS status;
3132 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3133 return NT_STATUS_UNSUCCESSFUL);
3134 DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
3136 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
3137 if (smb_fname == NULL) {
3138 return NT_STATUS_NO_MEMORY;
3141 if (config->meta == FRUIT_META_NETATALK) {
3142 ad = ad_get(talloc_tos(), handle,
3143 smb_fname->base_name, ADOUBLE_META);
3144 if (ad && !empty_finderinfo(ad)) {
3145 if (!add_fruit_stream(
3146 mem_ctx, pnum_streams, pstreams,
3147 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3148 smb_roundup(handle->conn,
3149 AFP_INFO_SIZE))) {
3150 TALLOC_FREE(ad);
3151 TALLOC_FREE(smb_fname);
3152 return NT_STATUS_NO_MEMORY;
3155 TALLOC_FREE(ad);
3158 if (config->rsrc != FRUIT_RSRC_STREAM) {
3159 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3160 ADOUBLE_RSRC);
3161 if (ad && (ad_getentrylen(ad, ADEID_RFORK) > 0)) {
3162 if (!add_fruit_stream(
3163 mem_ctx, pnum_streams, pstreams,
3164 AFPRESOURCE_STREAM_NAME,
3165 ad_getentrylen(ad, ADEID_RFORK),
3166 smb_roundup(handle->conn,
3167 ad_getentrylen(
3168 ad, ADEID_RFORK)))) {
3169 TALLOC_FREE(ad);
3170 TALLOC_FREE(smb_fname);
3171 return NT_STATUS_NO_MEMORY;
3174 TALLOC_FREE(ad);
3177 TALLOC_FREE(smb_fname);
3179 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3180 pnum_streams, pstreams);
3181 if (!NT_STATUS_IS_OK(status)) {
3182 return status;
3185 if (config->meta == FRUIT_META_NETATALK) {
3186 /* Remove the Netatalk xattr from the list */
3187 if (!del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3188 ":" NETATALK_META_XATTR ":$DATA")) {
3189 TALLOC_FREE(ad);
3190 TALLOC_FREE(smb_fname);
3191 return NT_STATUS_NO_MEMORY;
3195 return NT_STATUS_OK;
3198 static int fruit_ntimes(vfs_handle_struct *handle,
3199 const struct smb_filename *smb_fname,
3200 struct smb_file_time *ft)
3202 int rc = 0;
3203 struct adouble *ad = NULL;
3205 if (null_timespec(ft->create_time)) {
3206 goto exit;
3209 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3210 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3212 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3213 if (ad == NULL) {
3214 goto exit;
3217 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3218 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3220 rc = ad_write(ad, smb_fname->base_name);
3222 exit:
3224 TALLOC_FREE(ad);
3225 if (rc != 0) {
3226 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3227 return -1;
3229 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3232 static int fruit_fallocate(struct vfs_handle_struct *handle,
3233 struct files_struct *fsp,
3234 uint32_t mode,
3235 off_t offset,
3236 off_t len)
3238 struct adouble *ad =
3239 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3241 if (ad == NULL) {
3242 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3245 if (!fruit_fsp_recheck(ad, fsp)) {
3246 return -1;
3249 /* Let the pwrite code path handle it. */
3250 errno = ENOSYS;
3251 return -1;
3254 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3255 struct files_struct *fsp,
3256 off_t offset,
3257 struct adouble *ad)
3260 * As this request hasn't been seen in the wild,
3261 * the only sensible use I can imagine is the client
3262 * truncating the stream to 0 bytes size.
3263 * We simply remove the metadata on such a request.
3265 if (offset != 0) {
3266 DBG_WARNING("ftruncate %s to %jd",
3267 fsp_str_dbg(fsp), (intmax_t)offset);
3268 return -1;
3271 return SMB_VFS_FREMOVEXATTR(fsp, AFPRESOURCE_EA_NETATALK);
3274 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3275 struct files_struct *fsp,
3276 off_t offset,
3277 struct adouble *ad)
3279 int rc;
3280 struct fruit_config_data *config;
3282 SMB_VFS_HANDLE_GET_DATA(handle, config,
3283 struct fruit_config_data, return -1);
3285 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3286 return SMB_VFS_FREMOVEXATTR(fsp,
3287 AFPRESOURCE_EA_NETATALK);
3290 rc = SMB_VFS_NEXT_FTRUNCATE(
3291 handle, fsp,
3292 offset + ad_getentryoff(ad, ADEID_RFORK));
3293 if (rc != 0) {
3294 return -1;
3297 if (config->rsrc == FRUIT_RSRC_ADFILE) {
3298 ad_setentrylen(ad, ADEID_RFORK, offset);
3299 rc = ad_write(ad, NULL);
3300 if (rc != 0) {
3301 return -1;
3303 DEBUG(10, ("fruit_ftruncate_rsrc file %s offset %jd\n",
3304 fsp_str_dbg(fsp), (intmax_t)offset));
3307 return 0;
3310 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3311 struct files_struct *fsp,
3312 off_t offset)
3314 int rc = 0;
3315 struct adouble *ad =
3316 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3318 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3319 fsp_str_dbg(fsp), (double)offset));
3321 if (ad == NULL) {
3322 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3325 if (!fruit_fsp_recheck(ad, fsp)) {
3326 return -1;
3329 switch (ad->ad_type) {
3330 case ADOUBLE_META:
3331 rc = fruit_ftruncate_meta(handle, fsp, offset, ad);
3332 break;
3334 case ADOUBLE_RSRC:
3335 rc = fruit_ftruncate_rsrc(handle, fsp, offset, ad);
3336 break;
3338 default:
3339 return -1;
3342 return rc;
3345 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3346 struct smb_request *req,
3347 uint16_t root_dir_fid,
3348 struct smb_filename *smb_fname,
3349 uint32_t access_mask,
3350 uint32_t share_access,
3351 uint32_t create_disposition,
3352 uint32_t create_options,
3353 uint32_t file_attributes,
3354 uint32_t oplock_request,
3355 struct smb2_lease *lease,
3356 uint64_t allocation_size,
3357 uint32_t private_flags,
3358 struct security_descriptor *sd,
3359 struct ea_list *ea_list,
3360 files_struct **result,
3361 int *pinfo,
3362 const struct smb2_create_blobs *in_context_blobs,
3363 struct smb2_create_blobs *out_context_blobs)
3365 NTSTATUS status;
3366 struct fruit_config_data *config = NULL;
3367 files_struct *fsp = NULL;
3369 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3370 if (!NT_STATUS_IS_OK(status)) {
3371 goto fail;
3374 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3375 return NT_STATUS_UNSUCCESSFUL);
3377 status = SMB_VFS_NEXT_CREATE_FILE(
3378 handle, req, root_dir_fid, smb_fname,
3379 access_mask, share_access,
3380 create_disposition, create_options,
3381 file_attributes, oplock_request,
3382 lease,
3383 allocation_size, private_flags,
3384 sd, ea_list, result,
3385 pinfo, in_context_blobs, out_context_blobs);
3386 if (!NT_STATUS_IS_OK(status)) {
3387 return status;
3389 fsp = *result;
3391 if (config->copyfile_enabled) {
3393 * Set a flag in the fsp. Gets used in copychunk to
3394 * check whether the special Apple copyfile semantics
3395 * for copychunk should be allowed in a copychunk
3396 * request with a count of 0.
3398 fsp->aapl_copyfile_supported = true;
3402 * If this is a plain open for existing files, opening an 0
3403 * byte size resource fork MUST fail with
3404 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
3406 * Cf the vfs_fruit torture tests in test_rfork_create().
3408 if (is_afpresource_stream(fsp->fsp_name) &&
3409 create_disposition == FILE_OPEN)
3411 if (fsp->fsp_name->st.st_ex_size == 0) {
3412 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3413 goto fail;
3417 if (is_ntfs_stream_smb_fname(smb_fname)
3418 || fsp->is_directory) {
3419 return status;
3422 if (config->locking == FRUIT_LOCKING_NETATALK) {
3423 status = fruit_check_access(
3424 handle, *result,
3425 access_mask,
3426 map_share_mode_to_deny_mode(share_access, 0));
3427 if (!NT_STATUS_IS_OK(status)) {
3428 goto fail;
3432 return status;
3434 fail:
3435 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
3437 if (fsp) {
3438 close_file(req, fsp, ERROR_CLOSE);
3439 *result = fsp = NULL;
3442 return status;
3445 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3446 const struct smb_filename *fname,
3447 TALLOC_CTX *mem_ctx,
3448 struct readdir_attr_data **pattr_data)
3450 struct fruit_config_data *config = NULL;
3451 struct readdir_attr_data *attr_data;
3452 NTSTATUS status;
3454 SMB_VFS_HANDLE_GET_DATA(handle, config,
3455 struct fruit_config_data,
3456 return NT_STATUS_UNSUCCESSFUL);
3458 if (!config->use_aapl) {
3459 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3462 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3464 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3465 if (*pattr_data == NULL) {
3466 return NT_STATUS_UNSUCCESSFUL;
3468 attr_data = *pattr_data;
3469 attr_data->type = RDATTR_AAPL;
3472 * Mac metadata: compressed FinderInfo, resource fork length
3473 * and creation date
3475 status = readdir_attr_macmeta(handle, fname, attr_data);
3476 if (!NT_STATUS_IS_OK(status)) {
3478 * Error handling is tricky: if we return failure from
3479 * this function, the corresponding directory entry
3480 * will to be passed to the client, so we really just
3481 * want to error out on fatal errors.
3483 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3484 goto fail;
3489 * UNIX mode
3491 if (config->unix_info_enabled) {
3492 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3496 * max_access
3498 if (!config->readdir_attr_max_access) {
3499 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3500 } else {
3501 status = smbd_calculate_access_mask(
3502 handle->conn,
3503 fname,
3504 false,
3505 SEC_FLAG_MAXIMUM_ALLOWED,
3506 &attr_data->attr_data.aapl.max_access);
3507 if (!NT_STATUS_IS_OK(status)) {
3508 goto fail;
3512 return NT_STATUS_OK;
3514 fail:
3515 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3516 fname->base_name, nt_errstr(status)));
3517 TALLOC_FREE(*pattr_data);
3518 return status;
3521 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3522 files_struct *fsp,
3523 uint32_t security_info,
3524 TALLOC_CTX *mem_ctx,
3525 struct security_descriptor **ppdesc)
3527 NTSTATUS status;
3528 struct security_ace ace;
3529 struct dom_sid sid;
3530 struct fruit_config_data *config;
3532 SMB_VFS_HANDLE_GET_DATA(handle, config,
3533 struct fruit_config_data,
3534 return NT_STATUS_UNSUCCESSFUL);
3536 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3537 mem_ctx, ppdesc);
3538 if (!NT_STATUS_IS_OK(status)) {
3539 return status;
3543 * Add MS NFS style ACEs with uid, gid and mode
3545 if (!config->unix_info_enabled) {
3546 return NT_STATUS_OK;
3549 /* MS NFS style mode */
3550 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3551 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3552 status = security_descriptor_dacl_add(*ppdesc, &ace);
3553 if (!NT_STATUS_IS_OK(status)) {
3554 DEBUG(1,("failed to add MS NFS style ACE\n"));
3555 return status;
3558 /* MS NFS style uid */
3559 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3560 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3561 status = security_descriptor_dacl_add(*ppdesc, &ace);
3562 if (!NT_STATUS_IS_OK(status)) {
3563 DEBUG(1,("failed to add MS NFS style ACE\n"));
3564 return status;
3567 /* MS NFS style gid */
3568 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3569 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3570 status = security_descriptor_dacl_add(*ppdesc, &ace);
3571 if (!NT_STATUS_IS_OK(status)) {
3572 DEBUG(1,("failed to add MS NFS style ACE\n"));
3573 return status;
3576 return NT_STATUS_OK;
3579 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3580 files_struct *fsp,
3581 uint32_t security_info_sent,
3582 const struct security_descriptor *psd)
3584 NTSTATUS status;
3585 bool do_chmod;
3586 mode_t ms_nfs_mode;
3587 int result;
3589 DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp)));
3591 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3592 if (!NT_STATUS_IS_OK(status)) {
3593 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3594 return status;
3597 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3598 if (!NT_STATUS_IS_OK(status)) {
3599 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3600 return status;
3603 if (do_chmod) {
3604 if (fsp->fh->fd != -1) {
3605 DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp)));
3606 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3607 } else {
3608 DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp)));
3609 result = SMB_VFS_CHMOD(fsp->conn,
3610 fsp->fsp_name->base_name,
3611 ms_nfs_mode);
3614 if (result != 0) {
3615 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3616 result, (unsigned)ms_nfs_mode,
3617 strerror(errno)));
3618 status = map_nt_error_from_unix(errno);
3619 return status;
3623 return NT_STATUS_OK;
3626 struct fruit_copy_chunk_state {
3627 struct vfs_handle_struct *handle;
3628 off_t copied;
3629 struct files_struct *src_fsp;
3630 struct files_struct *dst_fsp;
3631 bool is_copyfile;
3634 static void fruit_copy_chunk_done(struct tevent_req *subreq);
3635 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
3636 TALLOC_CTX *mem_ctx,
3637 struct tevent_context *ev,
3638 struct files_struct *src_fsp,
3639 off_t src_off,
3640 struct files_struct *dest_fsp,
3641 off_t dest_off,
3642 off_t num)
3644 struct tevent_req *req, *subreq;
3645 struct fruit_copy_chunk_state *fruit_copy_chunk_state;
3646 NTSTATUS status;
3647 struct fruit_config_data *config;
3648 off_t to_copy = num;
3650 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
3651 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
3653 SMB_VFS_HANDLE_GET_DATA(handle, config,
3654 struct fruit_config_data,
3655 return NULL);
3657 req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
3658 struct fruit_copy_chunk_state);
3659 if (req == NULL) {
3660 return NULL;
3662 fruit_copy_chunk_state->handle = handle;
3663 fruit_copy_chunk_state->src_fsp = src_fsp;
3664 fruit_copy_chunk_state->dst_fsp = dest_fsp;
3667 * Check if this a OS X copyfile style copychunk request with
3668 * a requested chunk count of 0 that was translated to a
3669 * copy_chunk_send VFS call overloading the parameters src_off
3670 * = dest_off = num = 0.
3672 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
3673 src_fsp->aapl_copyfile_supported &&
3674 dest_fsp->aapl_copyfile_supported)
3676 status = vfs_stat_fsp(src_fsp);
3677 if (tevent_req_nterror(req, status)) {
3678 return tevent_req_post(req, ev);
3681 to_copy = src_fsp->fsp_name->st.st_ex_size;
3682 fruit_copy_chunk_state->is_copyfile = true;
3685 subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
3686 mem_ctx,
3688 src_fsp,
3689 src_off,
3690 dest_fsp,
3691 dest_off,
3692 to_copy);
3693 if (tevent_req_nomem(subreq, req)) {
3694 return tevent_req_post(req, ev);
3697 tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
3698 return req;
3701 static void fruit_copy_chunk_done(struct tevent_req *subreq)
3703 struct tevent_req *req = tevent_req_callback_data(
3704 subreq, struct tevent_req);
3705 struct fruit_copy_chunk_state *state = tevent_req_data(
3706 req, struct fruit_copy_chunk_state);
3707 NTSTATUS status;
3708 unsigned int num_streams = 0;
3709 struct stream_struct *streams = NULL;
3710 int i;
3711 struct smb_filename *src_fname_tmp = NULL;
3712 struct smb_filename *dst_fname_tmp = NULL;
3714 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
3715 subreq,
3716 &state->copied);
3717 TALLOC_FREE(subreq);
3718 if (tevent_req_nterror(req, status)) {
3719 return;
3722 if (!state->is_copyfile) {
3723 tevent_req_done(req);
3724 return;
3728 * Now copy all reamining streams. We know the share supports
3729 * streams, because we're in vfs_fruit. We don't do this async
3730 * because streams are few and small.
3732 status = vfs_streaminfo(state->handle->conn, NULL,
3733 state->src_fsp->fsp_name->base_name,
3734 req, &num_streams, &streams);
3735 if (tevent_req_nterror(req, status)) {
3736 return;
3739 if (num_streams == 1) {
3740 /* There is always one stream, ::$DATA. */
3741 tevent_req_done(req);
3742 return;
3745 for (i = 0; i < num_streams; i++) {
3746 DEBUG(10, ("%s: stream: '%s'/%ju\n",
3747 __func__, streams[i].name,
3748 (uintmax_t)streams[i].size));
3750 src_fname_tmp = synthetic_smb_fname(
3751 req,
3752 state->src_fsp->fsp_name->base_name,
3753 streams[i].name,
3754 NULL);
3755 if (tevent_req_nomem(src_fname_tmp, req)) {
3756 return;
3759 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
3760 TALLOC_FREE(src_fname_tmp);
3761 continue;
3764 dst_fname_tmp = synthetic_smb_fname(
3765 req,
3766 state->dst_fsp->fsp_name->base_name,
3767 streams[i].name,
3768 NULL);
3769 if (tevent_req_nomem(dst_fname_tmp, req)) {
3770 TALLOC_FREE(src_fname_tmp);
3771 return;
3774 status = copy_file(req,
3775 state->handle->conn,
3776 src_fname_tmp,
3777 dst_fname_tmp,
3778 OPENX_FILE_CREATE_IF_NOT_EXIST,
3779 0, false);
3780 if (!NT_STATUS_IS_OK(status)) {
3781 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
3782 smb_fname_str_dbg(src_fname_tmp),
3783 smb_fname_str_dbg(dst_fname_tmp),
3784 nt_errstr(status)));
3785 TALLOC_FREE(src_fname_tmp);
3786 TALLOC_FREE(dst_fname_tmp);
3787 tevent_req_nterror(req, status);
3788 return;
3791 TALLOC_FREE(src_fname_tmp);
3792 TALLOC_FREE(dst_fname_tmp);
3795 TALLOC_FREE(streams);
3796 TALLOC_FREE(src_fname_tmp);
3797 TALLOC_FREE(dst_fname_tmp);
3798 tevent_req_done(req);
3801 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
3802 struct tevent_req *req,
3803 off_t *copied)
3805 struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
3806 req, struct fruit_copy_chunk_state);
3807 NTSTATUS status;
3809 if (tevent_req_is_nterror(req, &status)) {
3810 DEBUG(1, ("server side copy chunk failed: %s\n",
3811 nt_errstr(status)));
3812 *copied = 0;
3813 tevent_req_received(req);
3814 return status;
3817 *copied = fruit_copy_chunk_state->copied;
3818 tevent_req_received(req);
3820 return NT_STATUS_OK;
3823 static struct vfs_fn_pointers vfs_fruit_fns = {
3824 .connect_fn = fruit_connect,
3826 /* File operations */
3827 .chmod_fn = fruit_chmod,
3828 .chown_fn = fruit_chown,
3829 .unlink_fn = fruit_unlink,
3830 .rename_fn = fruit_rename,
3831 .rmdir_fn = fruit_rmdir,
3832 .open_fn = fruit_open,
3833 .pread_fn = fruit_pread,
3834 .pwrite_fn = fruit_pwrite,
3835 .stat_fn = fruit_stat,
3836 .lstat_fn = fruit_lstat,
3837 .fstat_fn = fruit_fstat,
3838 .streaminfo_fn = fruit_streaminfo,
3839 .ntimes_fn = fruit_ntimes,
3840 .ftruncate_fn = fruit_ftruncate,
3841 .fallocate_fn = fruit_fallocate,
3842 .create_file_fn = fruit_create_file,
3843 .readdir_attr_fn = fruit_readdir_attr,
3844 .copy_chunk_send_fn = fruit_copy_chunk_send,
3845 .copy_chunk_recv_fn = fruit_copy_chunk_recv,
3847 /* NT ACL operations */
3848 .fget_nt_acl_fn = fruit_fget_nt_acl,
3849 .fset_nt_acl_fn = fruit_fset_nt_acl,
3852 NTSTATUS vfs_fruit_init(void);
3853 NTSTATUS vfs_fruit_init(void)
3855 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3856 &vfs_fruit_fns);
3857 if (!NT_STATUS_IS_OK(ret)) {
3858 return ret;
3861 vfs_fruit_debug_level = debug_add_class("fruit");
3862 if (vfs_fruit_debug_level == -1) {
3863 vfs_fruit_debug_level = DBGC_VFS;
3864 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3865 "vfs_fruit_init"));
3866 } else {
3867 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3868 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3871 return ret;