s3/vfs/nfs4_acls: avoid a stat
[Samba.git] / source3 / modules / vfs_fruit.c
blob6e7899aaad785de4d8757bbf6ec074ea62fee766
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; /* config from smb.conf */
129 bool nego_aapl; /* client negotiated AAPL */
130 bool use_copyfile;
131 bool readdir_attr_enabled;
132 bool unix_info_enabled;
133 bool copyfile_enabled;
134 bool veto_appledouble;
135 bool posix_rename;
138 * Additional options, all enabled by default,
139 * possibly useful for analyzing performance. The associated
140 * operations with each of them may be expensive, so having
141 * the chance to disable them individually gives a chance
142 * tweaking the setup for the particular usecase.
144 bool readdir_attr_rsize;
145 bool readdir_attr_finder_info;
146 bool readdir_attr_max_access;
149 static const struct enum_list fruit_rsrc[] = {
150 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
151 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
152 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
153 { -1, NULL}
156 static const struct enum_list fruit_meta[] = {
157 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
159 { -1, NULL}
162 static const struct enum_list fruit_locking[] = {
163 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
164 {FRUIT_LOCKING_NONE, "none"},
165 { -1, NULL}
168 static const struct enum_list fruit_encoding[] = {
169 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
170 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
171 { -1, NULL}
174 /*****************************************************************************
175 * Defines, functions and data structures that deal with AppleDouble
176 *****************************************************************************/
179 * There are two AppleDouble blobs we deal with:
181 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
182 * metadata in an xattr
184 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
185 * ._ files
187 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
189 /* Version info */
190 #define AD_VERSION2 0x00020000
191 #define AD_VERSION AD_VERSION2
194 * AppleDouble entry IDs.
196 #define ADEID_DFORK 1
197 #define ADEID_RFORK 2
198 #define ADEID_NAME 3
199 #define ADEID_COMMENT 4
200 #define ADEID_ICONBW 5
201 #define ADEID_ICONCOL 6
202 #define ADEID_FILEI 7
203 #define ADEID_FILEDATESI 8
204 #define ADEID_FINDERI 9
205 #define ADEID_MACFILEI 10
206 #define ADEID_PRODOSFILEI 11
207 #define ADEID_MSDOSFILEI 12
208 #define ADEID_SHORTNAME 13
209 #define ADEID_AFPFILEI 14
210 #define ADEID_DID 15
212 /* Private Netatalk entries */
213 #define ADEID_PRIVDEV 16
214 #define ADEID_PRIVINO 17
215 #define ADEID_PRIVSYN 18
216 #define ADEID_PRIVID 19
217 #define ADEID_MAX (ADEID_PRIVID + 1)
220 * These are the real ids for the private entries,
221 * as stored in the adouble file
223 #define AD_DEV 0x80444556
224 #define AD_INO 0x80494E4F
225 #define AD_SYN 0x8053594E
226 #define AD_ID 0x8053567E
228 /* Number of actually used entries */
229 #define ADEID_NUM_XATTR 8
230 #define ADEID_NUM_DOT_UND 2
231 #define ADEID_NUM_RSRC_XATTR 1
233 /* AppleDouble magic */
234 #define AD_APPLESINGLE_MAGIC 0x00051600
235 #define AD_APPLEDOUBLE_MAGIC 0x00051607
236 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
238 /* Sizes of relevant entry bits */
239 #define ADEDLEN_MAGIC 4
240 #define ADEDLEN_VERSION 4
241 #define ADEDLEN_FILLER 16
242 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
243 #define ADEDLEN_NENTRIES 2
244 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
245 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
246 #define AD_ENTRY_LEN_EID 4
247 #define AD_ENTRY_LEN_OFF 4
248 #define AD_ENTRY_LEN_LEN 4
249 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
251 /* Field widths */
252 #define ADEDLEN_NAME 255
253 #define ADEDLEN_COMMENT 200
254 #define ADEDLEN_FILEI 16
255 #define ADEDLEN_FINDERI 32
256 #define ADEDLEN_FILEDATESI 16
257 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
258 #define ADEDLEN_AFPFILEI 4
259 #define ADEDLEN_MACFILEI 4
260 #define ADEDLEN_PRODOSFILEI 8
261 #define ADEDLEN_MSDOSFILEI 2
262 #define ADEDLEN_DID 4
263 #define ADEDLEN_PRIVDEV 8
264 #define ADEDLEN_PRIVINO 8
265 #define ADEDLEN_PRIVSYN 8
266 #define ADEDLEN_PRIVID 4
268 /* Offsets */
269 #define ADEDOFF_MAGIC 0
270 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
271 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
272 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
274 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
275 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
276 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
277 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
278 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
279 ADEDLEN_FILEDATESI)
280 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
281 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
282 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
283 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
285 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
286 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
287 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
289 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
290 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
291 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
292 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
293 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
294 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
296 #if AD_DATASZ_XATTR != 402
297 #error bad size for AD_DATASZ_XATTR
298 #endif
300 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
301 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
302 ADEDLEN_FINDERI)
303 #if AD_DATASZ_DOT_UND != 82
304 #error bad size for AD_DATASZ_DOT_UND
305 #endif
308 * Sharemode locks fcntl() offsets
310 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
311 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
312 #else
313 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
314 #endif
315 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
317 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
318 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
319 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
320 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
321 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
322 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
323 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
324 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
325 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
326 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
328 /* Time stuff we overload the bits a little */
329 #define AD_DATE_CREATE 0
330 #define AD_DATE_MODIFY 4
331 #define AD_DATE_BACKUP 8
332 #define AD_DATE_ACCESS 12
333 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
334 AD_DATE_BACKUP | AD_DATE_ACCESS)
335 #define AD_DATE_UNIX (1 << 10)
336 #define AD_DATE_START 0x80000000
337 #define AD_DATE_DELTA 946684800
338 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
339 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
341 /* Accessor macros */
342 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
343 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
344 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
345 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
346 #define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
348 struct ad_entry {
349 size_t ade_off;
350 size_t ade_len;
353 struct adouble {
354 vfs_handle_struct *ad_handle;
355 files_struct *ad_fsp;
356 adouble_type_t ad_type;
357 uint32_t ad_magic;
358 uint32_t ad_version;
359 struct ad_entry ad_eid[ADEID_MAX];
360 char *ad_data;
363 struct ad_entry_order {
364 uint32_t id, offset, len;
367 /* Netatalk AppleDouble metadata xattr */
368 static const
369 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
370 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
371 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
372 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
373 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
374 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
375 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
376 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
377 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
378 {0, 0, 0}
381 /* AppleDouble ressource fork file (the ones prefixed by "._") */
382 static const
383 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
384 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
385 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
386 {0, 0, 0}
390 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
391 * isn't an AppleDouble file, it simply contains the ressource data,
392 * but in order to be able to use some API calls like ad_getentryoff()
393 * we build a fake/helper struct adouble with this entry order struct.
395 static const
396 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
397 {ADEID_RFORK, 0, 0},
398 {0, 0, 0}
401 /* Conversion from enumerated id to on-disk AppleDouble id */
402 #define AD_EID_DISK(a) (set_eid[a])
403 static const uint32_t set_eid[] = {
404 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
405 AD_DEV, AD_INO, AD_SYN, AD_ID
409 * Forward declarations
411 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
412 adouble_type_t type, files_struct *fsp);
413 static int ad_write(struct adouble *ad, const char *path);
414 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
417 * Get a date
419 static int ad_getdate(const struct adouble *ad,
420 unsigned int dateoff,
421 uint32_t *date)
423 bool xlate = (dateoff & AD_DATE_UNIX);
425 dateoff &= AD_DATE_MASK;
426 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
427 return -1;
430 if (dateoff > AD_DATE_ACCESS) {
431 return -1;
433 memcpy(date,
434 ad_entry(ad, ADEID_FILEDATESI) + dateoff,
435 sizeof(uint32_t));
437 if (xlate) {
438 *date = AD_DATE_TO_UNIX(*date);
440 return 0;
444 * Set a date
446 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
448 bool xlate = (dateoff & AD_DATE_UNIX);
450 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
451 return 0;
454 dateoff &= AD_DATE_MASK;
455 if (xlate) {
456 date = AD_DATE_FROM_UNIX(date);
459 if (dateoff > AD_DATE_ACCESS) {
460 return -1;
463 memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
465 return 0;
470 * Map on-disk AppleDouble id to enumerated id
472 static uint32_t get_eid(uint32_t eid)
474 if (eid <= 15) {
475 return eid;
478 switch (eid) {
479 case AD_DEV:
480 return ADEID_PRIVDEV;
481 case AD_INO:
482 return ADEID_PRIVINO;
483 case AD_SYN:
484 return ADEID_PRIVSYN;
485 case AD_ID:
486 return ADEID_PRIVID;
487 default:
488 break;
491 return 0;
495 * Pack AppleDouble structure into data buffer
497 static bool ad_pack(struct adouble *ad)
499 uint32_t eid;
500 uint16_t nent;
501 uint32_t bufsize;
502 uint32_t offset = 0;
504 bufsize = talloc_get_size(ad->ad_data);
506 if (offset + ADEDLEN_MAGIC < offset ||
507 offset + ADEDLEN_MAGIC >= bufsize) {
508 return false;
510 RSIVAL(ad->ad_data, offset, ad->ad_magic);
511 offset += ADEDLEN_MAGIC;
513 if (offset + ADEDLEN_VERSION < offset ||
514 offset + ADEDLEN_VERSION >= bufsize) {
515 return false;
517 RSIVAL(ad->ad_data, offset, ad->ad_version);
518 offset += ADEDLEN_VERSION;
520 if (offset + ADEDLEN_FILLER < offset ||
521 offset + ADEDLEN_FILLER >= bufsize) {
522 return false;
524 if (ad->ad_type == ADOUBLE_RSRC) {
525 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
527 offset += ADEDLEN_FILLER;
529 if (offset + ADEDLEN_NENTRIES < offset ||
530 offset + ADEDLEN_NENTRIES >= bufsize) {
531 return false;
533 offset += ADEDLEN_NENTRIES;
535 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
536 if (ad->ad_eid[eid].ade_off == 0) {
538 * ade_off is also used as indicator whether a
539 * specific entry is used or not
541 continue;
544 if (offset + AD_ENTRY_LEN_EID < offset ||
545 offset + AD_ENTRY_LEN_EID >= bufsize) {
546 return false;
548 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
549 offset += AD_ENTRY_LEN_EID;
551 if (offset + AD_ENTRY_LEN_OFF < offset ||
552 offset + AD_ENTRY_LEN_OFF >= bufsize) {
553 return false;
555 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
556 offset += AD_ENTRY_LEN_OFF;
558 if (offset + AD_ENTRY_LEN_LEN < offset ||
559 offset + AD_ENTRY_LEN_LEN >= bufsize) {
560 return false;
562 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
563 offset += AD_ENTRY_LEN_LEN;
565 nent++;
568 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
569 return false;
571 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
573 return true;
577 * Unpack an AppleDouble blob into a struct adoble
579 static bool ad_unpack(struct adouble *ad, const size_t nentries,
580 size_t filesize)
582 size_t bufsize = talloc_get_size(ad->ad_data);
583 size_t adentries, i;
584 uint32_t eid, len, off;
587 * The size of the buffer ad->ad_data is checked when read, so
588 * we wouldn't have to check our own offsets, a few extra
589 * checks won't hurt though. We have to check the offsets we
590 * read from the buffer anyway.
593 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
594 DEBUG(1, ("bad size\n"));
595 return false;
598 ad->ad_magic = RIVAL(ad->ad_data, 0);
599 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
600 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
601 DEBUG(1, ("wrong magic or version\n"));
602 return false;
605 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
606 if (adentries != nentries) {
607 DEBUG(1, ("invalid number of entries: %zu\n",
608 adentries));
609 return false;
612 /* now, read in the entry bits */
613 for (i = 0; i < adentries; i++) {
614 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
615 eid = get_eid(eid);
616 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
617 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
619 if (!eid || eid > ADEID_MAX) {
620 DEBUG(1, ("bogus eid %d\n", eid));
621 return false;
625 * All entries other than the resource fork are
626 * expected to be read into the ad_data buffer, so
627 * ensure the specified offset is within that bound
629 if ((off > bufsize) && (eid != ADEID_RFORK)) {
630 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
631 eid, off, len));
632 return false;
636 * All entries besides FinderInfo and resource fork
637 * must fit into the buffer. FinderInfo is special as
638 * it may be larger then the default 32 bytes (if it
639 * contains marshalled xattrs), but we will fixup that
640 * in ad_convert(). And the resource fork is never
641 * accessed directly by the ad_data buf (also see
642 * comment above) anyway.
644 if ((eid != ADEID_RFORK) &&
645 (eid != ADEID_FINDERI) &&
646 ((off + len) > bufsize)) {
647 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
648 eid, off, len));
649 return false;
653 * That would be obviously broken
655 if (off > filesize) {
656 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
657 eid, off, len));
658 return false;
662 * Check for any entry that has its end beyond the
663 * filesize.
665 if (off + len < off) {
666 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
667 ", len: %" PRIu32 "\n",
668 eid, off, len));
669 return false;
672 if (off + len > filesize) {
674 * If this is the resource fork entry, we fix
675 * up the length, for any other entry we bail
676 * out.
678 if (eid != ADEID_RFORK) {
679 DEBUG(1, ("bogus eid %d: off: %" PRIu32
680 ", len: %" PRIu32 "\n",
681 eid, off, len));
682 return false;
686 * Fixup the resource fork entry by limiting
687 * the size to entryoffset - filesize.
689 len = filesize - off;
690 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
691 ", len: %" PRIu32 "\n", off, len));
694 ad->ad_eid[eid].ade_off = off;
695 ad->ad_eid[eid].ade_len = len;
698 return true;
702 * Convert from Apple's ._ file to Netatalk
704 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
705 * bytes containing packed xattrs. Netatalk can't deal with that, so
706 * we simply discard the packed xattrs.
708 * @return -1 in case an error occured, 0 if no conversion was done, 1
709 * otherwise
711 static int ad_convert(struct adouble *ad, int fd)
713 int rc = 0;
714 char *map = MAP_FAILED;
715 size_t origlen;
717 origlen = ad_getentryoff(ad, ADEID_RFORK) +
718 ad_getentrylen(ad, ADEID_RFORK);
720 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
721 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
722 if (map == MAP_FAILED) {
723 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
724 rc = -1;
725 goto exit;
728 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
729 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
730 map + ad_getentryoff(ad, ADEID_RFORK),
731 ad_getentrylen(ad, ADEID_RFORK));
734 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
735 ad_setentryoff(ad, ADEID_RFORK,
736 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
739 * FIXME: direct ftruncate(), but we don't have a fsp for the
740 * VFS call
742 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
743 + ad_getentrylen(ad, ADEID_RFORK));
745 exit:
746 if (map != MAP_FAILED) {
747 munmap(map, origlen);
749 return rc;
753 * Read and parse Netatalk AppleDouble metadata xattr
755 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
757 int rc = 0;
758 ssize_t ealen;
759 bool ok;
761 DEBUG(10, ("reading meta xattr for %s\n", path));
763 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
764 AFPINFO_EA_NETATALK, ad->ad_data,
765 AD_DATASZ_XATTR);
766 if (ealen == -1) {
767 switch (errno) {
768 case ENOATTR:
769 case ENOENT:
770 if (errno == ENOATTR) {
771 errno = ENOENT;
773 rc = -1;
774 goto exit;
775 default:
776 DEBUG(2, ("error reading meta xattr: %s\n",
777 strerror(errno)));
778 rc = -1;
779 goto exit;
782 if (ealen != AD_DATASZ_XATTR) {
783 DEBUG(2, ("bad size %zd\n", ealen));
784 errno = EINVAL;
785 rc = -1;
786 goto exit;
789 /* Now parse entries */
790 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
791 if (!ok) {
792 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
793 errno = EINVAL;
794 rc = -1;
795 goto exit;
798 if (!ad_getentryoff(ad, ADEID_FINDERI)
799 || !ad_getentryoff(ad, ADEID_COMMENT)
800 || !ad_getentryoff(ad, ADEID_FILEDATESI)
801 || !ad_getentryoff(ad, ADEID_AFPFILEI)
802 || !ad_getentryoff(ad, ADEID_PRIVDEV)
803 || !ad_getentryoff(ad, ADEID_PRIVINO)
804 || !ad_getentryoff(ad, ADEID_PRIVSYN)
805 || !ad_getentryoff(ad, ADEID_PRIVID)) {
806 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
807 errno = EINVAL;
808 rc = -1;
809 goto exit;
812 exit:
813 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
815 if (rc != 0) {
816 ealen = -1;
817 if (errno == EINVAL) {
818 become_root();
819 removexattr(path, AFPINFO_EA_NETATALK);
820 unbecome_root();
821 errno = ENOENT;
824 return ealen;
828 * Read and parse resource fork, either ._ AppleDouble file or xattr
830 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
832 struct fruit_config_data *config = NULL;
833 int fd = -1;
834 int rc = 0;
835 ssize_t len;
836 char *adpath = NULL;
837 bool opened = false;
838 int mode;
839 struct adouble *meta_ad = NULL;
840 SMB_STRUCT_STAT sbuf;
841 bool ok;
842 int saved_errno = 0;
844 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
845 struct fruit_config_data, return -1);
847 /* Try rw first so we can use the fd in ad_convert() */
848 mode = O_RDWR;
850 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
851 fd = ad->ad_fsp->fh->fd;
852 } else {
853 if (config->rsrc == FRUIT_RSRC_XATTR) {
854 adpath = talloc_strdup(talloc_tos(), path);
855 } else {
856 rc = adouble_path(talloc_tos(), path, &adpath);
857 if (rc != 0) {
858 goto exit;
862 retry:
863 if (config->rsrc == FRUIT_RSRC_XATTR) {
864 #ifndef HAVE_ATTROPEN
865 errno = ENOSYS;
866 rc = -1;
867 goto exit;
868 #else
869 /* FIXME: direct Solaris xattr syscall */
870 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
871 mode, 0);
872 #endif
873 } else {
874 /* FIXME: direct open(), don't have an fsp */
875 fd = open(adpath, mode);
878 if (fd == -1) {
879 switch (errno) {
880 case EROFS:
881 case EACCES:
882 if (mode == O_RDWR) {
883 mode = O_RDONLY;
884 goto retry;
886 /* fall through ... */
887 default:
888 DEBUG(2, ("open AppleDouble: %s, %s\n",
889 adpath, strerror(errno)));
890 rc = -1;
891 goto exit;
894 opened = true;
897 if (config->rsrc == FRUIT_RSRC_XATTR) {
898 /* FIXME: direct sys_fstat(), don't have an fsp */
899 rc = sys_fstat(
900 fd, &sbuf,
901 lp_fake_directory_create_times(
902 SNUM(ad->ad_handle->conn)));
903 if (rc != 0) {
904 goto exit;
906 len = sbuf.st_ex_size;
907 ad_setentrylen(ad, ADEID_RFORK, len);
908 } else {
909 /* FIXME: direct sys_pread(), don't have an fsp */
910 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
911 if (len != AD_DATASZ_DOT_UND) {
912 DEBUG(2, ("%s: bad size: %zd\n",
913 strerror(errno), len));
914 rc = -1;
915 goto exit;
918 /* FIXME: direct sys_fstat(), we don't have an fsp */
919 rc = sys_fstat(fd, &sbuf,
920 lp_fake_directory_create_times(
921 SNUM(ad->ad_handle->conn)));
922 if (rc != 0) {
923 goto exit;
926 /* Now parse entries */
927 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
928 if (!ok) {
929 DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
930 errno = EINVAL;
931 rc = -1;
932 goto exit;
935 if ((ad_getentryoff(ad, ADEID_FINDERI)
936 != ADEDOFF_FINDERI_DOT_UND)
937 || (ad_getentrylen(ad, ADEID_FINDERI)
938 < ADEDLEN_FINDERI)
939 || (ad_getentryoff(ad, ADEID_RFORK)
940 < ADEDOFF_RFORK_DOT_UND)) {
941 DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
942 errno = EINVAL;
943 rc = -1;
944 goto exit;
947 if ((mode == O_RDWR)
948 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
949 rc = ad_convert(ad, fd);
950 if (rc != 0) {
951 rc = -1;
952 goto exit;
955 * Can't use ad_write() because we might not have a fsp
957 ok = ad_pack(ad);
958 if (!ok) {
959 rc = -1;
960 goto exit;
962 /* FIXME: direct sys_pwrite(), don't have an fsp */
963 len = sys_pwrite(fd, ad->ad_data,
964 AD_DATASZ_DOT_UND, 0);
965 if (len != AD_DATASZ_DOT_UND) {
966 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
967 rc = -1;
968 goto exit;
971 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
972 ADOUBLE_META, NULL);
973 if (meta_ad == NULL) {
974 rc = -1;
975 goto exit;
978 memcpy(ad_entry(meta_ad, ADEID_FINDERI),
979 ad_entry(ad, ADEID_FINDERI),
980 ADEDLEN_FINDERI);
982 rc = ad_write(meta_ad, path);
983 if (rc != 0) {
984 rc = -1;
985 goto exit;
990 DEBUG(10, ("opened AppleDouble: %s\n", path));
992 exit:
993 if (rc != 0) {
994 saved_errno = errno;
995 len = -1;
997 if (opened && fd != -1) {
998 close(fd);
1000 TALLOC_FREE(adpath);
1001 TALLOC_FREE(meta_ad);
1002 if (rc != 0) {
1003 errno = saved_errno;
1005 return len;
1009 * Read and unpack an AppleDouble metadata xattr or resource
1011 static ssize_t ad_read(struct adouble *ad, const char *path)
1013 switch (ad->ad_type) {
1014 case ADOUBLE_META:
1015 return ad_header_read_meta(ad, path);
1016 case ADOUBLE_RSRC:
1017 return ad_header_read_rsrc(ad, path);
1018 default:
1019 return -1;
1024 * Allocate a struct adouble without initialiing it
1026 * The struct is either hang of the fsp extension context or if fsp is
1027 * NULL from ctx.
1029 * @param[in] ctx talloc context
1030 * @param[in] handle vfs handle
1031 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1033 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1034 * added as an fsp extension
1036 * @return adouble handle
1038 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1039 adouble_type_t type, files_struct *fsp)
1041 int rc = 0;
1042 size_t adsize = 0;
1043 struct adouble *ad;
1044 struct fruit_config_data *config;
1046 SMB_VFS_HANDLE_GET_DATA(handle, config,
1047 struct fruit_config_data, return NULL);
1049 switch (type) {
1050 case ADOUBLE_META:
1051 adsize = AD_DATASZ_XATTR;
1052 break;
1053 case ADOUBLE_RSRC:
1054 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1055 adsize = AD_DATASZ_DOT_UND;
1057 break;
1058 default:
1059 return NULL;
1062 if (!fsp) {
1063 ad = talloc_zero(ctx, struct adouble);
1064 if (ad == NULL) {
1065 rc = -1;
1066 goto exit;
1068 if (adsize) {
1069 ad->ad_data = talloc_zero_array(ad, char, adsize);
1071 } else {
1072 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1073 struct adouble,
1074 NULL);
1075 if (ad == NULL) {
1076 rc = -1;
1077 goto exit;
1079 if (adsize) {
1080 ad->ad_data = talloc_zero_array(
1081 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1082 char, adsize);
1084 ad->ad_fsp = fsp;
1087 if (adsize && ad->ad_data == NULL) {
1088 rc = -1;
1089 goto exit;
1091 ad->ad_handle = handle;
1092 ad->ad_type = type;
1093 ad->ad_magic = AD_MAGIC;
1094 ad->ad_version = AD_VERSION;
1096 exit:
1097 if (rc != 0) {
1098 TALLOC_FREE(ad);
1100 return ad;
1104 * Allocate and initialize a new struct adouble
1106 * @param[in] ctx talloc context
1107 * @param[in] handle vfs handle
1108 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1109 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1111 * @return adouble handle, initialized
1113 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1114 adouble_type_t type, files_struct *fsp)
1116 int rc = 0;
1117 const struct ad_entry_order *eid;
1118 struct adouble *ad = NULL;
1119 struct fruit_config_data *config;
1120 time_t t = time(NULL);
1122 SMB_VFS_HANDLE_GET_DATA(handle, config,
1123 struct fruit_config_data, return NULL);
1125 switch (type) {
1126 case ADOUBLE_META:
1127 eid = entry_order_meta_xattr;
1128 break;
1129 case ADOUBLE_RSRC:
1130 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1131 eid = entry_order_dot_und;
1132 } else {
1133 eid = entry_order_rsrc_xattr;
1135 break;
1136 default:
1137 return NULL;
1140 ad = ad_alloc(ctx, handle, type, fsp);
1141 if (ad == NULL) {
1142 return NULL;
1145 while (eid->id) {
1146 ad->ad_eid[eid->id].ade_off = eid->offset;
1147 ad->ad_eid[eid->id].ade_len = eid->len;
1148 eid++;
1151 /* put something sane in the date fields */
1152 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1153 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1154 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1155 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1157 if (rc != 0) {
1158 TALLOC_FREE(ad);
1160 return ad;
1164 * Return AppleDouble data for a file
1166 * @param[in] ctx talloc context
1167 * @param[in] handle vfs handle
1168 * @param[in] path pathname to file or directory
1169 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1171 * @return talloced struct adouble or NULL on error
1173 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1174 const char *path, adouble_type_t type)
1176 int rc = 0;
1177 ssize_t len;
1178 struct adouble *ad = NULL;
1180 DEBUG(10, ("ad_get(%s) called for %s\n",
1181 type == ADOUBLE_META ? "meta" : "rsrc", path));
1183 ad = ad_alloc(ctx, handle, type, NULL);
1184 if (ad == NULL) {
1185 rc = -1;
1186 goto exit;
1189 len = ad_read(ad, path);
1190 if (len == -1) {
1191 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1192 rc = -1;
1193 goto exit;
1196 exit:
1197 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1198 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1200 if (rc != 0) {
1201 TALLOC_FREE(ad);
1203 return ad;
1207 * Set AppleDouble metadata on a file or directory
1209 * @param[in] ad adouble handle
1211 * @param[in] path pathname to file or directory, may be NULL for a
1212 * resource fork
1214 * @return status code, 0 means success
1216 static int ad_write(struct adouble *ad, const char *path)
1218 int rc = 0;
1219 ssize_t len;
1220 bool ok;
1222 ok = ad_pack(ad);
1223 if (!ok) {
1224 return -1;
1227 switch (ad->ad_type) {
1228 case ADOUBLE_META:
1229 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1230 AFPINFO_EA_NETATALK, ad->ad_data,
1231 AD_DATASZ_XATTR, 0);
1232 break;
1233 case ADOUBLE_RSRC:
1234 if ((ad->ad_fsp == NULL)
1235 || (ad->ad_fsp->fh == NULL)
1236 || (ad->ad_fsp->fh->fd == -1)) {
1237 rc = -1;
1238 goto exit;
1240 /* FIXME: direct sys_pwrite(), don't have an fsp */
1241 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1242 talloc_get_size(ad->ad_data), 0);
1243 if (len != talloc_get_size(ad->ad_data)) {
1244 DEBUG(1, ("short write on %s: %zd",
1245 fsp_str_dbg(ad->ad_fsp), len));
1246 rc = -1;
1247 goto exit;
1249 break;
1250 default:
1251 return -1;
1253 exit:
1254 return rc;
1257 /*****************************************************************************
1258 * Helper functions
1259 *****************************************************************************/
1261 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1263 if (strncasecmp_m(smb_fname->stream_name,
1264 AFPINFO_STREAM_NAME,
1265 strlen(AFPINFO_STREAM_NAME)) == 0) {
1266 return true;
1268 return false;
1271 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1273 if (strncasecmp_m(smb_fname->stream_name,
1274 AFPRESOURCE_STREAM_NAME,
1275 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1276 return true;
1278 return false;
1282 * Test whether stream is an Apple stream, not used atm
1284 #if 0
1285 static bool is_apple_stream(const struct smb_filename *smb_fname)
1287 if (is_afpinfo_stream(smb_fname)) {
1288 return true;
1290 if (is_afpresource_stream(smb_fname)) {
1291 return true;
1293 return false;
1295 #endif
1298 * Initialize config struct from our smb.conf config parameters
1300 static int init_fruit_config(vfs_handle_struct *handle)
1302 struct fruit_config_data *config;
1303 int enumval;
1305 config = talloc_zero(handle->conn, struct fruit_config_data);
1306 if (!config) {
1307 DEBUG(1, ("talloc_zero() failed\n"));
1308 errno = ENOMEM;
1309 return -1;
1312 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1313 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1314 if (enumval == -1) {
1315 DEBUG(1, ("value for %s: ressource type unknown\n",
1316 FRUIT_PARAM_TYPE_NAME));
1317 return -1;
1319 config->rsrc = (enum fruit_rsrc)enumval;
1321 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1322 "metadata", fruit_meta, FRUIT_META_NETATALK);
1323 if (enumval == -1) {
1324 DEBUG(1, ("value for %s: metadata type unknown\n",
1325 FRUIT_PARAM_TYPE_NAME));
1326 return -1;
1328 config->meta = (enum fruit_meta)enumval;
1330 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1331 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1332 if (enumval == -1) {
1333 DEBUG(1, ("value for %s: locking type unknown\n",
1334 FRUIT_PARAM_TYPE_NAME));
1335 return -1;
1337 config->locking = (enum fruit_locking)enumval;
1339 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1340 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1341 if (enumval == -1) {
1342 DEBUG(1, ("value for %s: encoding type unknown\n",
1343 FRUIT_PARAM_TYPE_NAME));
1344 return -1;
1346 config->encoding = (enum fruit_encoding)enumval;
1348 config->veto_appledouble = lp_parm_bool(
1349 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1350 "veto_appledouble", true);
1352 config->use_aapl = lp_parm_bool(
1353 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1355 config->unix_info_enabled = lp_parm_bool(
1356 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1358 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1359 "copyfile", false);
1361 config->posix_rename = lp_parm_bool(
1362 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1364 config->readdir_attr_rsize = lp_parm_bool(
1365 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1367 config->readdir_attr_finder_info = lp_parm_bool(
1368 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1370 config->readdir_attr_max_access = lp_parm_bool(
1371 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1373 SMB_VFS_HANDLE_SET_DATA(handle, config,
1374 NULL, struct fruit_config_data,
1375 return -1);
1377 return 0;
1381 * Prepend "._" to a basename
1383 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1385 char *parent;
1386 const char *base;
1388 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1389 return -1;
1392 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1393 if (*path_out == NULL) {
1394 return -1;
1397 return 0;
1401 * Allocate and initialize an AfpInfo struct
1403 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1405 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1406 if (ai == NULL) {
1407 return NULL;
1409 ai->afpi_Signature = AFP_Signature;
1410 ai->afpi_Version = AFP_Version;
1411 ai->afpi_BackupTime = AD_DATE_START;
1412 return ai;
1416 * Pack an AfpInfo struct into a buffer
1418 * Buffer size must be at least AFP_INFO_SIZE
1419 * Returns size of packed buffer
1421 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1423 memset(buf, 0, AFP_INFO_SIZE);
1425 RSIVAL(buf, 0, ai->afpi_Signature);
1426 RSIVAL(buf, 4, ai->afpi_Version);
1427 RSIVAL(buf, 12, ai->afpi_BackupTime);
1428 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1430 return AFP_INFO_SIZE;
1434 * Unpack a buffer into a AfpInfo structure
1436 * Buffer size must be at least AFP_INFO_SIZE
1437 * Returns allocated AfpInfo struct
1439 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1441 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1442 if (ai == NULL) {
1443 return NULL;
1446 ai->afpi_Signature = RIVAL(data, 0);
1447 ai->afpi_Version = RIVAL(data, 4);
1448 ai->afpi_BackupTime = RIVAL(data, 12);
1449 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1450 sizeof(ai->afpi_FinderInfo));
1452 if (ai->afpi_Signature != AFP_Signature
1453 || ai->afpi_Version != AFP_Version) {
1454 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1455 TALLOC_FREE(ai);
1458 return ai;
1462 * Fake an inode number from the md5 hash of the (xattr) name
1464 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1466 MD5_CTX ctx;
1467 unsigned char hash[16];
1468 SMB_INO_T result;
1469 char *upper_sname;
1471 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1472 SMB_ASSERT(upper_sname != NULL);
1474 MD5Init(&ctx);
1475 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1476 sizeof(sbuf->st_ex_dev));
1477 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1478 sizeof(sbuf->st_ex_ino));
1479 MD5Update(&ctx, (unsigned char *)upper_sname,
1480 talloc_get_size(upper_sname)-1);
1481 MD5Final(hash, &ctx);
1483 TALLOC_FREE(upper_sname);
1485 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1486 memcpy(&result, hash, sizeof(result));
1488 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1489 sname, (unsigned long long)result));
1491 return result;
1495 * Ensure ad_fsp is still valid
1497 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1499 if (ad->ad_fsp == fsp) {
1500 return true;
1502 ad->ad_fsp = fsp;
1504 return true;
1507 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1508 struct stream_struct **streams,
1509 const char *name, off_t size,
1510 off_t alloc_size)
1512 struct stream_struct *tmp;
1514 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1515 (*num_streams)+1);
1516 if (tmp == NULL) {
1517 return false;
1520 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1521 if (tmp[*num_streams].name == NULL) {
1522 return false;
1525 tmp[*num_streams].size = size;
1526 tmp[*num_streams].alloc_size = alloc_size;
1528 *streams = tmp;
1529 *num_streams += 1;
1530 return true;
1533 static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1534 struct stream_struct **streams,
1535 const char *name)
1537 struct stream_struct *tmp = *streams;
1538 unsigned int i;
1540 if (*num_streams == 0) {
1541 return true;
1544 for (i = 0; i < *num_streams; i++) {
1545 if (strequal_m(tmp[i].name, name)) {
1546 break;
1550 if (i == *num_streams) {
1551 return true;
1554 TALLOC_FREE(tmp[i].name);
1555 if (*num_streams - 1 > i) {
1556 memmove(&tmp[i], &tmp[i+1],
1557 (*num_streams - i - 1) * sizeof(struct stream_struct));
1560 *num_streams -= 1;
1561 return true;
1564 static bool empty_finderinfo(const struct adouble *ad)
1567 char emptybuf[ADEDLEN_FINDERI] = {0};
1568 if (memcmp(emptybuf,
1569 ad_entry(ad, ADEID_FINDERI),
1570 ADEDLEN_FINDERI) == 0) {
1571 return true;
1573 return false;
1577 * Update btime with btime from Netatalk
1579 static void update_btime(vfs_handle_struct *handle,
1580 struct smb_filename *smb_fname)
1582 uint32_t t;
1583 struct timespec creation_time = {0};
1584 struct adouble *ad;
1586 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1587 if (ad == NULL) {
1588 return;
1590 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1591 TALLOC_FREE(ad);
1592 return;
1594 TALLOC_FREE(ad);
1596 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1597 update_stat_ex_create_time(&smb_fname->st, creation_time);
1599 return;
1603 * Map an access mask to a Netatalk single byte byte range lock
1605 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1606 uint32_t access_mask)
1608 off_t offset;
1610 switch (access_mask) {
1611 case FILE_READ_DATA:
1612 offset = AD_FILELOCK_OPEN_RD;
1613 break;
1615 case FILE_WRITE_DATA:
1616 case FILE_APPEND_DATA:
1617 offset = AD_FILELOCK_OPEN_WR;
1618 break;
1620 default:
1621 offset = AD_FILELOCK_OPEN_NONE;
1622 break;
1625 if (fork_type == APPLE_FORK_RSRC) {
1626 if (offset == AD_FILELOCK_OPEN_NONE) {
1627 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1628 } else {
1629 offset += 2;
1633 return offset;
1637 * Map a deny mode to a Netatalk brl
1639 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1640 uint32_t deny_mode)
1642 off_t offset;
1644 switch (deny_mode) {
1645 case DENY_READ:
1646 offset = AD_FILELOCK_DENY_RD;
1647 break;
1649 case DENY_WRITE:
1650 offset = AD_FILELOCK_DENY_WR;
1651 break;
1653 default:
1654 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1657 if (fork_type == APPLE_FORK_RSRC) {
1658 offset += 2;
1661 return offset;
1665 * Call fcntl() with an exclusive F_GETLK request in order to
1666 * determine if there's an exisiting shared lock
1668 * @return true if the requested lock was found or any error occured
1669 * false if the lock was not found
1671 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1673 bool result;
1674 off_t offset = in_offset;
1675 off_t len = 1;
1676 int type = F_WRLCK;
1677 pid_t pid;
1679 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1680 if (result == false) {
1681 return true;
1684 if (type != F_UNLCK) {
1685 return true;
1688 return false;
1691 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1692 files_struct *fsp,
1693 uint32_t access_mask,
1694 uint32_t deny_mode)
1696 NTSTATUS status = NT_STATUS_OK;
1697 struct byte_range_lock *br_lck = NULL;
1698 bool open_for_reading, open_for_writing, deny_read, deny_write;
1699 off_t off;
1701 /* FIXME: hardcoded data fork, add resource fork */
1702 enum apple_fork fork_type = APPLE_FORK_DATA;
1704 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1705 fsp_str_dbg(fsp),
1706 access_mask & FILE_READ_DATA ? "READ" :"-",
1707 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1708 deny_mode & DENY_READ ? "DENY_READ" : "-",
1709 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1712 * Check read access and deny read mode
1714 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1715 /* Check access */
1716 open_for_reading = test_netatalk_lock(
1717 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1719 deny_read = test_netatalk_lock(
1720 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1722 DEBUG(10, ("read: %s, deny_write: %s\n",
1723 open_for_reading == true ? "yes" : "no",
1724 deny_read == true ? "yes" : "no"));
1726 if (((access_mask & FILE_READ_DATA) && deny_read)
1727 || ((deny_mode & DENY_READ) && open_for_reading)) {
1728 return NT_STATUS_SHARING_VIOLATION;
1731 /* Set locks */
1732 if (access_mask & FILE_READ_DATA) {
1733 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1734 br_lck = do_lock(
1735 handle->conn->sconn->msg_ctx, fsp,
1736 fsp->op->global->open_persistent_id, 1, off,
1737 READ_LOCK, POSIX_LOCK, false,
1738 &status, NULL);
1740 if (!NT_STATUS_IS_OK(status)) {
1741 return status;
1743 TALLOC_FREE(br_lck);
1746 if (deny_mode & DENY_READ) {
1747 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1748 br_lck = do_lock(
1749 handle->conn->sconn->msg_ctx, fsp,
1750 fsp->op->global->open_persistent_id, 1, off,
1751 READ_LOCK, POSIX_LOCK, false,
1752 &status, NULL);
1754 if (!NT_STATUS_IS_OK(status)) {
1755 return status;
1757 TALLOC_FREE(br_lck);
1762 * Check write access and deny write mode
1764 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1765 /* Check access */
1766 open_for_writing = test_netatalk_lock(
1767 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1769 deny_write = test_netatalk_lock(
1770 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1772 DEBUG(10, ("write: %s, deny_write: %s\n",
1773 open_for_writing == true ? "yes" : "no",
1774 deny_write == true ? "yes" : "no"));
1776 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1777 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1778 return NT_STATUS_SHARING_VIOLATION;
1781 /* Set locks */
1782 if (access_mask & FILE_WRITE_DATA) {
1783 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1784 br_lck = do_lock(
1785 handle->conn->sconn->msg_ctx, fsp,
1786 fsp->op->global->open_persistent_id, 1, off,
1787 READ_LOCK, POSIX_LOCK, false,
1788 &status, NULL);
1790 if (!NT_STATUS_IS_OK(status)) {
1791 return status;
1793 TALLOC_FREE(br_lck);
1796 if (deny_mode & DENY_WRITE) {
1797 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1798 br_lck = do_lock(
1799 handle->conn->sconn->msg_ctx, fsp,
1800 fsp->op->global->open_persistent_id, 1, off,
1801 READ_LOCK, POSIX_LOCK, false,
1802 &status, NULL);
1804 if (!NT_STATUS_IS_OK(status)) {
1805 return status;
1807 TALLOC_FREE(br_lck);
1811 TALLOC_FREE(br_lck);
1813 return status;
1816 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1817 struct smb_request *req,
1818 const struct smb2_create_blobs *in_context_blobs,
1819 struct smb2_create_blobs *out_context_blobs)
1821 struct fruit_config_data *config;
1822 NTSTATUS status;
1823 struct smb2_create_blob *aapl = NULL;
1824 uint32_t cmd;
1825 bool ok;
1826 uint8_t p[16];
1827 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1828 uint64_t req_bitmap, client_caps;
1829 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1830 smb_ucs2_t *model;
1831 size_t modellen;
1833 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1834 return NT_STATUS_UNSUCCESSFUL);
1836 if (!config->use_aapl
1837 || in_context_blobs == NULL
1838 || out_context_blobs == NULL) {
1839 return NT_STATUS_OK;
1842 aapl = smb2_create_blob_find(in_context_blobs,
1843 SMB2_CREATE_TAG_AAPL);
1844 if (aapl == NULL) {
1845 return NT_STATUS_OK;
1848 if (aapl->data.length != 24) {
1849 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
1850 (uintmax_t)aapl->data.length));
1851 return NT_STATUS_INVALID_PARAMETER;
1854 cmd = IVAL(aapl->data.data, 0);
1855 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1856 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1857 return NT_STATUS_INVALID_PARAMETER;
1860 req_bitmap = BVAL(aapl->data.data, 8);
1861 client_caps = BVAL(aapl->data.data, 16);
1863 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1864 SIVAL(p, 4, 0);
1865 SBVAL(p, 8, req_bitmap);
1866 ok = data_blob_append(req, &blob, p, 16);
1867 if (!ok) {
1868 return NT_STATUS_UNSUCCESSFUL;
1871 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1872 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1873 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1874 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1875 config->readdir_attr_enabled = true;
1878 if (config->use_copyfile) {
1879 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
1880 config->copyfile_enabled = true;
1884 * The client doesn't set the flag, so we can't check
1885 * for it and just set it unconditionally
1887 if (config->unix_info_enabled) {
1888 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1891 SBVAL(p, 0, server_caps);
1892 ok = data_blob_append(req, &blob, p, 8);
1893 if (!ok) {
1894 return NT_STATUS_UNSUCCESSFUL;
1898 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1899 SBVAL(p, 0,
1900 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1901 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1902 ok = data_blob_append(req, &blob, p, 8);
1903 if (!ok) {
1904 return NT_STATUS_UNSUCCESSFUL;
1908 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1909 ok = convert_string_talloc(req,
1910 CH_UNIX, CH_UTF16LE,
1911 "Samba", strlen("Samba"),
1912 &model, &modellen);
1913 if (!ok) {
1914 return NT_STATUS_UNSUCCESSFUL;
1917 SIVAL(p, 0, 0);
1918 SIVAL(p + 4, 0, modellen);
1919 ok = data_blob_append(req, &blob, p, 8);
1920 if (!ok) {
1921 talloc_free(model);
1922 return NT_STATUS_UNSUCCESSFUL;
1925 ok = data_blob_append(req, &blob, model, modellen);
1926 talloc_free(model);
1927 if (!ok) {
1928 return NT_STATUS_UNSUCCESSFUL;
1932 status = smb2_create_blob_add(out_context_blobs,
1933 out_context_blobs,
1934 SMB2_CREATE_TAG_AAPL,
1935 blob);
1936 if (NT_STATUS_IS_OK(status)) {
1937 config->nego_aapl = true;
1940 return status;
1943 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1944 const struct smb_filename *smb_fname,
1945 struct readdir_attr_data *attr_data)
1947 NTSTATUS status = NT_STATUS_OK;
1948 uint32_t date_added;
1949 struct adouble *ad = NULL;
1950 struct fruit_config_data *config = NULL;
1952 SMB_VFS_HANDLE_GET_DATA(handle, config,
1953 struct fruit_config_data,
1954 return NT_STATUS_UNSUCCESSFUL);
1957 /* Ensure we return a default value in the creation_date field */
1958 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1961 * Resource fork length
1964 if (config->readdir_attr_rsize) {
1965 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1966 ADOUBLE_RSRC);
1967 if (ad) {
1968 attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1969 ad, ADEID_RFORK);
1970 TALLOC_FREE(ad);
1975 * FinderInfo
1978 if (config->readdir_attr_finder_info) {
1979 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1980 ADOUBLE_META);
1981 if (ad) {
1982 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1983 /* finder_type */
1984 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1985 ad_entry(ad, ADEID_FINDERI), 4);
1987 /* finder_creator */
1988 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1989 ad_entry(ad, ADEID_FINDERI) + 4, 4);
1992 /* finder_flags */
1993 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1994 ad_entry(ad, ADEID_FINDERI) + 8, 2);
1996 /* finder_ext_flags */
1997 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1998 ad_entry(ad, ADEID_FINDERI) + 24, 2);
2000 /* creation date */
2001 date_added = convert_time_t_to_uint32_t(
2002 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2003 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2005 TALLOC_FREE(ad);
2009 TALLOC_FREE(ad);
2010 return status;
2013 /* Search MS NFS style ACE with UNIX mode */
2014 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2015 files_struct *fsp,
2016 const struct security_descriptor *psd,
2017 mode_t *pmode,
2018 bool *pdo_chmod)
2020 uint32_t i;
2021 struct fruit_config_data *config = NULL;
2023 *pdo_chmod = false;
2025 SMB_VFS_HANDLE_GET_DATA(handle, config,
2026 struct fruit_config_data,
2027 return NT_STATUS_UNSUCCESSFUL);
2029 if (psd->dacl == NULL || !config->unix_info_enabled) {
2030 return NT_STATUS_OK;
2033 for (i = 0; i < psd->dacl->num_aces; i++) {
2034 if (dom_sid_compare_domain(
2035 &global_sid_Unix_NFS_Mode,
2036 &psd->dacl->aces[i].trustee) == 0) {
2037 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2038 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2039 *pdo_chmod = true;
2041 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2042 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2043 break;
2047 return NT_STATUS_OK;
2050 /****************************************************************************
2051 * VFS ops
2052 ****************************************************************************/
2054 static int fruit_connect(vfs_handle_struct *handle,
2055 const char *service,
2056 const char *user)
2058 int rc;
2059 char *list = NULL, *newlist = NULL;
2060 struct fruit_config_data *config;
2062 DEBUG(10, ("fruit_connect\n"));
2064 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2065 if (rc < 0) {
2066 return rc;
2069 rc = init_fruit_config(handle);
2070 if (rc != 0) {
2071 return rc;
2074 SMB_VFS_HANDLE_GET_DATA(handle, config,
2075 struct fruit_config_data, return -1);
2077 if (config->veto_appledouble) {
2078 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2080 if (list) {
2081 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2082 newlist = talloc_asprintf(
2083 list,
2084 "%s/" ADOUBLE_NAME_PREFIX "*/",
2085 list);
2086 lp_do_parameter(SNUM(handle->conn),
2087 "veto files",
2088 newlist);
2090 } else {
2091 lp_do_parameter(SNUM(handle->conn),
2092 "veto files",
2093 "/" ADOUBLE_NAME_PREFIX "*/");
2096 TALLOC_FREE(list);
2099 if (config->encoding == FRUIT_ENC_NATIVE) {
2100 lp_do_parameter(
2101 SNUM(handle->conn),
2102 "catia:mappings",
2103 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2104 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2105 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2106 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2107 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2108 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2109 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2110 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2111 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2112 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2113 "0x0d:0xf00d");
2116 return rc;
2119 static int fruit_open_meta(vfs_handle_struct *handle,
2120 struct smb_filename *smb_fname,
2121 files_struct *fsp, int flags, mode_t mode)
2123 int rc = 0;
2124 struct fruit_config_data *config = NULL;
2125 struct smb_filename *smb_fname_base = NULL;
2126 int baseflags;
2127 int hostfd = -1;
2128 struct adouble *ad = NULL;
2130 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
2132 SMB_VFS_HANDLE_GET_DATA(handle, config,
2133 struct fruit_config_data, return -1);
2135 if (config->meta == FRUIT_META_STREAM) {
2136 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2139 /* Create an smb_filename with stream_name == NULL. */
2140 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2141 smb_fname->base_name,
2142 NULL,
2143 NULL,
2144 smb_fname->flags);
2146 if (smb_fname_base == NULL) {
2147 errno = ENOMEM;
2148 rc = -1;
2149 goto exit;
2153 * We use baseflags to turn off nasty side-effects when opening the
2154 * underlying file.
2156 baseflags = flags;
2157 baseflags &= ~O_TRUNC;
2158 baseflags &= ~O_EXCL;
2159 baseflags &= ~O_CREAT;
2161 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2162 baseflags, mode);
2165 * It is legit to open a stream on a directory, but the base
2166 * fd has to be read-only.
2168 if ((hostfd == -1) && (errno == EISDIR)) {
2169 baseflags &= ~O_ACCMODE;
2170 baseflags |= O_RDONLY;
2171 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2172 baseflags, mode);
2175 TALLOC_FREE(smb_fname_base);
2177 if (hostfd == -1) {
2178 rc = -1;
2179 goto exit;
2182 if (flags & (O_CREAT | O_TRUNC)) {
2184 * The attribute does not exist or needs to be truncated,
2185 * create an AppleDouble EA
2187 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2188 handle, ADOUBLE_META, fsp);
2189 if (ad == NULL) {
2190 rc = -1;
2191 goto exit;
2194 rc = ad_write(ad, smb_fname->base_name);
2195 if (rc != 0) {
2196 rc = -1;
2197 goto exit;
2199 } else {
2200 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2201 handle, ADOUBLE_META, fsp);
2202 if (ad == NULL) {
2203 rc = -1;
2204 goto exit;
2206 if (ad_read(ad, smb_fname->base_name) == -1) {
2207 rc = -1;
2208 goto exit;
2212 exit:
2213 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2214 if (rc != 0) {
2215 int saved_errno = errno;
2216 if (hostfd >= 0) {
2218 * BUGBUGBUG -- we would need to call
2219 * fd_close_posix here, but we don't have a
2220 * full fsp yet
2222 fsp->fh->fd = hostfd;
2223 SMB_VFS_CLOSE(fsp);
2225 hostfd = -1;
2226 errno = saved_errno;
2228 return hostfd;
2231 static int fruit_open_rsrc(vfs_handle_struct *handle,
2232 struct smb_filename *smb_fname,
2233 files_struct *fsp, int flags, mode_t mode)
2235 int rc = 0;
2236 struct fruit_config_data *config = NULL;
2237 struct adouble *ad = NULL;
2238 struct smb_filename *smb_fname_base = NULL;
2239 char *adpath = NULL;
2240 int hostfd = -1;
2242 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2244 SMB_VFS_HANDLE_GET_DATA(handle, config,
2245 struct fruit_config_data, return -1);
2247 switch (config->rsrc) {
2248 case FRUIT_RSRC_STREAM:
2249 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2250 case FRUIT_RSRC_XATTR:
2251 #ifdef HAVE_ATTROPEN
2252 hostfd = attropen(smb_fname->base_name,
2253 AFPRESOURCE_EA_NETATALK, flags, mode);
2254 if (hostfd == -1) {
2255 return -1;
2257 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2258 handle, ADOUBLE_RSRC, fsp);
2259 if (ad == NULL) {
2260 rc = -1;
2261 goto exit;
2263 goto exit;
2264 #else
2265 errno = ENOTSUP;
2266 return -1;
2267 #endif
2268 default:
2269 break;
2272 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2273 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2274 if (rc != 0) {
2275 rc = -1;
2276 goto exit;
2280 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2281 /* sorry, but directories don't habe a resource fork */
2282 rc = -1;
2283 goto exit;
2286 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2287 if (rc != 0) {
2288 goto exit;
2291 /* Create an smb_filename with stream_name == NULL. */
2292 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2293 adpath,
2294 NULL,
2295 NULL,
2296 smb_fname->flags);
2297 if (smb_fname_base == NULL) {
2298 errno = ENOMEM;
2299 rc = -1;
2300 goto exit;
2303 /* Sanitize flags */
2304 if (flags & O_WRONLY) {
2305 /* We always need read access for the metadata header too */
2306 flags &= ~O_WRONLY;
2307 flags |= O_RDWR;
2310 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2311 flags, mode);
2312 if (hostfd == -1) {
2313 rc = -1;
2314 goto exit;
2317 /* REVIEW: we need this in ad_write() */
2318 fsp->fh->fd = hostfd;
2320 if (flags & (O_CREAT | O_TRUNC)) {
2321 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2322 handle, ADOUBLE_RSRC, fsp);
2323 if (ad == NULL) {
2324 rc = -1;
2325 goto exit;
2327 rc = ad_write(ad, smb_fname->base_name);
2328 if (rc != 0) {
2329 rc = -1;
2330 goto exit;
2332 } else {
2333 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2334 handle, ADOUBLE_RSRC, fsp);
2335 if (ad == NULL) {
2336 rc = -1;
2337 goto exit;
2339 if (ad_read(ad, smb_fname->base_name) == -1) {
2340 rc = -1;
2341 goto exit;
2345 exit:
2347 TALLOC_FREE(adpath);
2348 TALLOC_FREE(smb_fname_base);
2350 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2351 if (rc != 0) {
2352 int saved_errno = errno;
2353 if (hostfd >= 0) {
2355 * BUGBUGBUG -- we would need to call
2356 * fd_close_posix here, but we don't have a
2357 * full fsp yet
2359 fsp->fh->fd = hostfd;
2360 SMB_VFS_CLOSE(fsp);
2362 hostfd = -1;
2363 errno = saved_errno;
2365 return hostfd;
2368 static int fruit_open(vfs_handle_struct *handle,
2369 struct smb_filename *smb_fname,
2370 files_struct *fsp, int flags, mode_t mode)
2372 DEBUG(10, ("fruit_open called for %s\n",
2373 smb_fname_str_dbg(smb_fname)));
2375 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2376 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2379 if (is_afpinfo_stream(smb_fname)) {
2380 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2381 } else if (is_afpresource_stream(smb_fname)) {
2382 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2385 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2388 static int fruit_rename(struct vfs_handle_struct *handle,
2389 const struct smb_filename *smb_fname_src,
2390 const struct smb_filename *smb_fname_dst)
2392 int rc = -1;
2393 char *src_adouble_path = NULL;
2394 char *dst_adouble_path = NULL;
2395 struct fruit_config_data *config = NULL;
2397 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2399 if (!VALID_STAT(smb_fname_src->st)
2400 || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2401 return rc;
2404 SMB_VFS_HANDLE_GET_DATA(handle, config,
2405 struct fruit_config_data, return -1);
2407 if (config->rsrc == FRUIT_RSRC_XATTR) {
2408 return rc;
2411 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2412 &src_adouble_path);
2413 if (rc != 0) {
2414 goto done;
2416 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2417 &dst_adouble_path);
2418 if (rc != 0) {
2419 goto done;
2422 DEBUG(10, ("fruit_rename: %s -> %s\n",
2423 src_adouble_path, dst_adouble_path));
2425 rc = rename(src_adouble_path, dst_adouble_path);
2426 if (errno == ENOENT) {
2427 rc = 0;
2430 TALLOC_FREE(src_adouble_path);
2431 TALLOC_FREE(dst_adouble_path);
2433 done:
2434 return rc;
2437 static int fruit_unlink(vfs_handle_struct *handle,
2438 const struct smb_filename *smb_fname)
2440 int rc = -1;
2441 struct fruit_config_data *config = NULL;
2443 SMB_VFS_HANDLE_GET_DATA(handle, config,
2444 struct fruit_config_data, return -1);
2446 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2447 char *adp = NULL;
2449 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2450 if (rc != 0) {
2451 return -1;
2454 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2455 return 0;
2459 * 0 byte resource fork streams are not listed by
2460 * vfs_streaminfo, as a result stream cleanup/deletion of file
2461 * deletion doesn't remove the resourcefork stream.
2463 rc = adouble_path(talloc_tos(),
2464 smb_fname->base_name, &adp);
2465 if (rc != 0) {
2466 return -1;
2469 /* FIXME: direct unlink(), missing smb_fname */
2470 DBG_DEBUG("fruit_unlink: %s\n", adp);
2471 rc = unlink(adp);
2472 if ((rc == -1) && (errno == ENOENT)) {
2473 rc = 0;
2476 TALLOC_FREE(adp);
2477 return 0;
2480 if (is_afpinfo_stream(smb_fname)) {
2481 if (config->meta == FRUIT_META_STREAM) {
2482 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2483 } else {
2484 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2485 smb_fname->base_name,
2486 AFPINFO_EA_NETATALK);
2489 return rc;
2492 if (is_afpresource_stream(smb_fname)) {
2493 /* OS X ignores deletes on the AFP_Resource stream */
2494 return 0;
2497 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2500 return 0;
2503 static int fruit_chmod(vfs_handle_struct *handle,
2504 const struct smb_filename *smb_fname,
2505 mode_t mode)
2507 int rc = -1;
2508 char *adp = NULL;
2509 struct fruit_config_data *config = NULL;
2510 SMB_STRUCT_STAT sb;
2511 const char *path = smb_fname->base_name;
2512 struct smb_filename *smb_fname_adp = NULL;
2514 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
2515 if (rc != 0) {
2516 return rc;
2519 SMB_VFS_HANDLE_GET_DATA(handle, config,
2520 struct fruit_config_data, return -1);
2522 if (config->rsrc == FRUIT_RSRC_XATTR) {
2523 return 0;
2526 /* FIXME: direct sys_lstat(), missing smb_fname */
2527 rc = sys_lstat(path, &sb, false);
2528 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2529 return rc;
2532 rc = adouble_path(talloc_tos(), path, &adp);
2533 if (rc != 0) {
2534 return -1;
2537 DEBUG(10, ("fruit_chmod: %s\n", adp));
2539 smb_fname_adp = synthetic_smb_fname(talloc_tos(),
2540 adp,
2541 NULL,
2542 NULL,
2543 smb_fname->flags);
2544 if (smb_fname_adp == NULL) {
2545 TALLOC_FREE(adp);
2546 errno = ENOMEM;
2547 return -1;
2550 rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
2551 if (errno == ENOENT) {
2552 rc = 0;
2555 TALLOC_FREE(smb_fname_adp);
2556 TALLOC_FREE(adp);
2557 return rc;
2560 static int fruit_chown(vfs_handle_struct *handle,
2561 const struct smb_filename *smb_fname,
2562 uid_t uid,
2563 gid_t gid)
2565 int rc = -1;
2566 char *adp = NULL;
2567 struct fruit_config_data *config = NULL;
2568 struct smb_filename *adp_smb_fname = NULL;
2569 SMB_STRUCT_STAT sb;
2571 rc = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
2572 if (rc != 0) {
2573 return rc;
2576 SMB_VFS_HANDLE_GET_DATA(handle, config,
2577 struct fruit_config_data, return -1);
2579 if (config->rsrc == FRUIT_RSRC_XATTR) {
2580 return rc;
2583 /* FIXME: direct sys_lstat(), need non-const smb_fname */
2584 rc = sys_lstat(smb_fname->base_name, &sb, false);
2585 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2586 return rc;
2589 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
2590 if (rc != 0) {
2591 goto done;
2594 DEBUG(10, ("fruit_chown: %s\n", adp));
2596 adp_smb_fname = synthetic_smb_fname(talloc_tos(),
2597 adp,
2598 NULL,
2599 NULL,
2600 smb_fname->flags);
2601 if (adp_smb_fname == NULL) {
2602 errno = ENOMEM;
2603 rc = -1;
2604 goto done;
2607 rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
2608 if (errno == ENOENT) {
2609 rc = 0;
2612 done:
2613 TALLOC_FREE(adp);
2614 TALLOC_FREE(adp_smb_fname);
2615 return rc;
2618 static int fruit_rmdir(struct vfs_handle_struct *handle,
2619 const struct smb_filename *smb_fname)
2621 DIR *dh = NULL;
2622 struct dirent *de;
2623 struct fruit_config_data *config;
2624 const char *path = smb_fname->base_name;
2626 SMB_VFS_HANDLE_GET_DATA(handle, config,
2627 struct fruit_config_data, return -1);
2629 if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2630 goto exit_rmdir;
2634 * Due to there is no way to change bDeleteVetoFiles variable
2635 * from this module, need to clean up ourselves
2637 dh = opendir(path);
2638 if (dh == NULL) {
2639 goto exit_rmdir;
2642 while ((de = readdir(dh)) != NULL) {
2643 if ((strncmp(de->d_name,
2644 ADOUBLE_NAME_PREFIX,
2645 strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2646 char *p = talloc_asprintf(talloc_tos(),
2647 "%s/%s",
2648 path, de->d_name);
2649 if (p == NULL) {
2650 goto exit_rmdir;
2652 DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2653 (void)unlink(p);
2654 TALLOC_FREE(p);
2658 exit_rmdir:
2659 if (dh) {
2660 closedir(dh);
2662 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
2665 static ssize_t fruit_pread(vfs_handle_struct *handle,
2666 files_struct *fsp, void *data,
2667 size_t n, off_t offset)
2669 int rc = 0;
2670 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2671 handle, fsp);
2672 struct fruit_config_data *config = NULL;
2673 AfpInfo *ai = NULL;
2674 ssize_t len = -1;
2675 char *name = NULL;
2676 char *tmp_base_name = NULL;
2677 NTSTATUS status;
2679 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2681 if (!fsp->base_fsp) {
2682 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2685 SMB_VFS_HANDLE_GET_DATA(handle, config,
2686 struct fruit_config_data, return -1);
2688 /* fsp_name is not converted with vfs_catia */
2689 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2690 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2691 fsp->base_fsp->fsp_name->base_name,
2692 vfs_translate_to_unix,
2693 talloc_tos(), &name);
2694 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2695 name = talloc_strdup(talloc_tos(), tmp_base_name);
2696 if (name == NULL) {
2697 rc = -1;
2698 goto exit;
2700 } else if (!NT_STATUS_IS_OK(status)) {
2701 errno = map_errno_from_nt_status(status);
2702 rc = -1;
2703 goto exit;
2705 fsp->base_fsp->fsp_name->base_name = name;
2707 if (ad == NULL) {
2708 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2709 if (len == -1) {
2710 rc = -1;
2711 goto exit;
2713 goto exit;
2716 if (!fruit_fsp_recheck(ad, fsp)) {
2717 rc = -1;
2718 goto exit;
2721 if (ad->ad_type == ADOUBLE_META) {
2722 char afpinfo_buf[AFP_INFO_SIZE];
2723 size_t to_return;
2726 * OS X has a off-by-1 error in the offset calculation, so we're
2727 * bug compatible here. It won't hurt, as any relevant real
2728 * world read requests from the AFP_AfpInfo stream will be
2729 * offset=0 n=60. offset is ignored anyway, see below.
2731 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2732 len = 0;
2733 rc = 0;
2734 goto exit;
2737 to_return = MIN(n, AFP_INFO_SIZE);
2739 ai = afpinfo_new(talloc_tos());
2740 if (ai == NULL) {
2741 rc = -1;
2742 goto exit;
2745 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2746 if (len == -1) {
2747 rc = -1;
2748 goto exit;
2751 memcpy(&ai->afpi_FinderInfo[0],
2752 ad_entry(ad, ADEID_FINDERI),
2753 ADEDLEN_FINDERI);
2754 len = afpinfo_pack(ai, afpinfo_buf);
2755 if (len != AFP_INFO_SIZE) {
2756 rc = -1;
2757 goto exit;
2761 * OS X ignores offset when reading from AFP_AfpInfo stream!
2763 memcpy(data, afpinfo_buf, to_return);
2764 len = to_return;
2765 } else {
2766 len = SMB_VFS_NEXT_PREAD(
2767 handle, fsp, data, n,
2768 offset + ad_getentryoff(ad, ADEID_RFORK));
2769 if (len == -1) {
2770 rc = -1;
2771 goto exit;
2774 exit:
2775 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2776 TALLOC_FREE(name);
2777 TALLOC_FREE(ai);
2778 if (rc != 0) {
2779 len = -1;
2781 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2782 return len;
2785 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2786 files_struct *fsp, const void *data,
2787 size_t n, off_t offset)
2789 int rc = 0;
2790 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2791 handle, fsp);
2792 struct fruit_config_data *config = NULL;
2793 AfpInfo *ai = NULL;
2794 ssize_t len;
2795 char *name = NULL;
2796 char *tmp_base_name = NULL;
2797 NTSTATUS status;
2799 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2801 if (!fsp->base_fsp) {
2802 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2805 SMB_VFS_HANDLE_GET_DATA(handle, config,
2806 struct fruit_config_data, return -1);
2808 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2809 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2810 fsp->base_fsp->fsp_name->base_name,
2811 vfs_translate_to_unix,
2812 talloc_tos(), &name);
2813 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2814 name = talloc_strdup(talloc_tos(), tmp_base_name);
2815 if (name == NULL) {
2816 rc = -1;
2817 goto exit;
2819 } else if (!NT_STATUS_IS_OK(status)) {
2820 errno = map_errno_from_nt_status(status);
2821 rc = -1;
2822 goto exit;
2824 fsp->base_fsp->fsp_name->base_name = name;
2826 if (ad == NULL) {
2827 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2828 if (len != n) {
2829 rc = -1;
2830 goto exit;
2832 goto exit;
2835 if (!fruit_fsp_recheck(ad, fsp)) {
2836 rc = -1;
2837 goto exit;
2840 if (ad->ad_type == ADOUBLE_META) {
2841 if (n != AFP_INFO_SIZE || offset != 0) {
2842 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2843 (intmax_t)offset, (intmax_t)n));
2844 rc = -1;
2845 goto exit;
2847 ai = afpinfo_unpack(talloc_tos(), data);
2848 if (ai == NULL) {
2849 rc = -1;
2850 goto exit;
2852 memcpy(ad_entry(ad, ADEID_FINDERI),
2853 &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2854 if (empty_finderinfo(ad)) {
2855 /* Discard metadata */
2856 if (config->meta == FRUIT_META_STREAM) {
2857 rc = SMB_VFS_FTRUNCATE(fsp, 0);
2858 } else {
2859 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2860 fsp->fsp_name->base_name,
2861 AFPINFO_EA_NETATALK);
2863 if (rc != 0 && errno != ENOENT && errno != ENOATTR) {
2864 DBG_WARNING("Can't delete metadata for %s: %s\n",
2865 fsp->fsp_name->base_name, strerror(errno));
2866 goto exit;
2868 rc = 0;
2869 goto exit;
2871 rc = ad_write(ad, name);
2872 } else {
2873 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2874 offset + ad_getentryoff(ad, ADEID_RFORK));
2875 if (len != n) {
2876 rc = -1;
2877 goto exit;
2880 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2881 rc = ad_read(ad, name);
2882 if (rc == -1) {
2883 goto exit;
2885 rc = 0;
2887 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2888 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2889 rc = ad_write(ad, name);
2894 exit:
2895 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2896 TALLOC_FREE(name);
2897 TALLOC_FREE(ai);
2898 if (rc != 0) {
2899 return -1;
2901 return n;
2905 * Helper to stat/lstat the base file of an smb_fname.
2907 static int fruit_stat_base(vfs_handle_struct *handle,
2908 struct smb_filename *smb_fname,
2909 bool follow_links)
2911 char *tmp_stream_name;
2912 int rc;
2914 tmp_stream_name = smb_fname->stream_name;
2915 smb_fname->stream_name = NULL;
2916 if (follow_links) {
2917 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2918 } else {
2919 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2921 smb_fname->stream_name = tmp_stream_name;
2922 return rc;
2925 static int fruit_stat_meta(vfs_handle_struct *handle,
2926 struct smb_filename *smb_fname,
2927 bool follow_links)
2929 struct adouble *ad = NULL;
2931 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
2932 if (ad == NULL) {
2933 DBG_INFO("fruit_stat_meta %s: %s\n",
2934 smb_fname_str_dbg(smb_fname), strerror(errno));
2935 errno = ENOENT;
2936 return -1;
2938 TALLOC_FREE(ad);
2940 /* Populate the stat struct with info from the base file. */
2941 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2942 return -1;
2944 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2945 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2946 smb_fname->stream_name);
2947 return 0;
2950 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2951 struct smb_filename *smb_fname,
2952 bool follow_links)
2955 struct adouble *ad = NULL;
2957 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2958 smb_fname_str_dbg(smb_fname)));
2960 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2961 if (ad == NULL) {
2962 errno = ENOENT;
2963 return -1;
2966 /* Populate the stat struct with info from the base file. */
2967 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2968 TALLOC_FREE(ad);
2969 return -1;
2972 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2973 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2974 smb_fname->stream_name);
2975 TALLOC_FREE(ad);
2976 return 0;
2979 static int fruit_stat(vfs_handle_struct *handle,
2980 struct smb_filename *smb_fname)
2982 int rc = -1;
2984 DEBUG(10, ("fruit_stat called for %s\n",
2985 smb_fname_str_dbg(smb_fname)));
2987 if (!is_ntfs_stream_smb_fname(smb_fname)
2988 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2989 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2990 if (rc == 0) {
2991 update_btime(handle, smb_fname);
2993 return rc;
2997 * Note if lp_posix_paths() is true, we can never
2998 * get here as is_ntfs_stream_smb_fname() is
2999 * always false. So we never need worry about
3000 * not following links here.
3003 if (is_afpinfo_stream(smb_fname)) {
3004 rc = fruit_stat_meta(handle, smb_fname, true);
3005 } else if (is_afpresource_stream(smb_fname)) {
3006 rc = fruit_stat_rsrc(handle, smb_fname, true);
3007 } else {
3008 return SMB_VFS_NEXT_STAT(handle, smb_fname);
3011 if (rc == 0) {
3012 update_btime(handle, smb_fname);
3013 smb_fname->st.st_ex_mode &= ~S_IFMT;
3014 smb_fname->st.st_ex_mode |= S_IFREG;
3015 smb_fname->st.st_ex_blocks =
3016 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3018 return rc;
3021 static int fruit_lstat(vfs_handle_struct *handle,
3022 struct smb_filename *smb_fname)
3024 int rc = -1;
3026 DEBUG(10, ("fruit_lstat called for %s\n",
3027 smb_fname_str_dbg(smb_fname)));
3029 if (!is_ntfs_stream_smb_fname(smb_fname)
3030 || is_ntfs_default_stream_smb_fname(smb_fname)) {
3031 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3032 if (rc == 0) {
3033 update_btime(handle, smb_fname);
3035 return rc;
3038 if (is_afpinfo_stream(smb_fname)) {
3039 rc = fruit_stat_meta(handle, smb_fname, false);
3040 } else if (is_afpresource_stream(smb_fname)) {
3041 rc = fruit_stat_rsrc(handle, smb_fname, false);
3042 } else {
3043 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3046 if (rc == 0) {
3047 update_btime(handle, smb_fname);
3048 smb_fname->st.st_ex_mode &= ~S_IFMT;
3049 smb_fname->st.st_ex_mode |= S_IFREG;
3050 smb_fname->st.st_ex_blocks =
3051 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3053 return rc;
3056 static int fruit_fstat_meta(vfs_handle_struct *handle,
3057 files_struct *fsp,
3058 SMB_STRUCT_STAT *sbuf)
3060 DEBUG(10, ("fruit_fstat_meta called for %s\n",
3061 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3063 /* Populate the stat struct with info from the base file. */
3064 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3065 return -1;
3067 *sbuf = fsp->base_fsp->fsp_name->st;
3068 sbuf->st_ex_size = AFP_INFO_SIZE;
3069 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3071 return 0;
3074 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3075 SMB_STRUCT_STAT *sbuf)
3077 struct fruit_config_data *config;
3078 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3079 handle, fsp);
3081 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
3082 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3084 SMB_VFS_HANDLE_GET_DATA(handle, config,
3085 struct fruit_config_data, return -1);
3087 if (config->rsrc == FRUIT_RSRC_STREAM) {
3088 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3091 /* Populate the stat struct with info from the base file. */
3092 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3093 return -1;
3095 *sbuf = fsp->base_fsp->fsp_name->st;
3096 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3097 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3099 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3100 smb_fname_str_dbg(fsp->fsp_name),
3101 (ssize_t)sbuf->st_ex_size));
3103 return 0;
3106 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3107 SMB_STRUCT_STAT *sbuf)
3109 int rc;
3110 char *name = NULL;
3111 char *tmp_base_name = NULL;
3112 NTSTATUS status;
3113 struct adouble *ad = (struct adouble *)
3114 VFS_FETCH_FSP_EXTENSION(handle, fsp);
3116 DEBUG(10, ("fruit_fstat called for %s\n",
3117 smb_fname_str_dbg(fsp->fsp_name)));
3119 if (fsp->base_fsp) {
3120 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3121 /* fsp_name is not converted with vfs_catia */
3122 status = SMB_VFS_TRANSLATE_NAME(
3123 handle->conn,
3124 fsp->base_fsp->fsp_name->base_name,
3125 vfs_translate_to_unix,
3126 talloc_tos(), &name);
3128 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3129 name = talloc_strdup(talloc_tos(), tmp_base_name);
3130 if (name == NULL) {
3131 rc = -1;
3132 goto exit;
3134 } else if (!NT_STATUS_IS_OK(status)) {
3135 errno = map_errno_from_nt_status(status);
3136 rc = -1;
3137 goto exit;
3139 fsp->base_fsp->fsp_name->base_name = name;
3142 if (ad == NULL || fsp->base_fsp == NULL) {
3143 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3144 goto exit;
3147 if (!fruit_fsp_recheck(ad, fsp)) {
3148 rc = -1;
3149 goto exit;
3152 switch (ad->ad_type) {
3153 case ADOUBLE_META:
3154 rc = fruit_fstat_meta(handle, fsp, sbuf);
3155 break;
3156 case ADOUBLE_RSRC:
3157 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
3158 break;
3159 default:
3160 DEBUG(10, ("fruit_fstat %s: bad type\n",
3161 smb_fname_str_dbg(fsp->fsp_name)));
3162 rc = -1;
3163 goto exit;
3166 if (rc == 0) {
3167 sbuf->st_ex_mode &= ~S_IFMT;
3168 sbuf->st_ex_mode |= S_IFREG;
3169 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3172 exit:
3173 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3174 smb_fname_str_dbg(fsp->fsp_name),
3175 (ssize_t)sbuf->st_ex_size));
3176 if (tmp_base_name) {
3177 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3179 TALLOC_FREE(name);
3180 return rc;
3183 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3184 struct files_struct *fsp,
3185 const struct smb_filename *smb_fname,
3186 TALLOC_CTX *mem_ctx,
3187 unsigned int *pnum_streams,
3188 struct stream_struct **pstreams)
3190 struct fruit_config_data *config = NULL;
3191 struct adouble *ad = NULL;
3192 NTSTATUS status;
3194 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3195 return NT_STATUS_UNSUCCESSFUL);
3196 DEBUG(10, ("fruit_streaminfo called for %s\n", smb_fname->base_name));
3198 if (config->meta == FRUIT_META_NETATALK) {
3199 ad = ad_get(talloc_tos(), handle,
3200 smb_fname->base_name, ADOUBLE_META);
3201 if (ad && !empty_finderinfo(ad)) {
3202 if (!add_fruit_stream(
3203 mem_ctx, pnum_streams, pstreams,
3204 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3205 smb_roundup(handle->conn,
3206 AFP_INFO_SIZE))) {
3207 TALLOC_FREE(ad);
3208 return NT_STATUS_NO_MEMORY;
3211 TALLOC_FREE(ad);
3214 if (config->rsrc != FRUIT_RSRC_STREAM) {
3215 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3216 ADOUBLE_RSRC);
3217 if (ad && (ad_getentrylen(ad, ADEID_RFORK) > 0)) {
3218 if (!add_fruit_stream(
3219 mem_ctx, pnum_streams, pstreams,
3220 AFPRESOURCE_STREAM_NAME,
3221 ad_getentrylen(ad, ADEID_RFORK),
3222 smb_roundup(handle->conn,
3223 ad_getentrylen(
3224 ad, ADEID_RFORK)))) {
3225 TALLOC_FREE(ad);
3226 return NT_STATUS_NO_MEMORY;
3229 TALLOC_FREE(ad);
3232 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
3233 pnum_streams, pstreams);
3234 if (!NT_STATUS_IS_OK(status)) {
3235 return status;
3238 if (config->meta == FRUIT_META_NETATALK) {
3239 /* Remove the Netatalk xattr from the list */
3240 if (!del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3241 ":" NETATALK_META_XATTR ":$DATA")) {
3242 TALLOC_FREE(ad);
3243 return NT_STATUS_NO_MEMORY;
3247 return NT_STATUS_OK;
3250 static int fruit_ntimes(vfs_handle_struct *handle,
3251 const struct smb_filename *smb_fname,
3252 struct smb_file_time *ft)
3254 int rc = 0;
3255 struct adouble *ad = NULL;
3257 if (null_timespec(ft->create_time)) {
3258 goto exit;
3261 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3262 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3264 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3265 if (ad == NULL) {
3266 goto exit;
3269 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3270 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3272 rc = ad_write(ad, smb_fname->base_name);
3274 exit:
3276 TALLOC_FREE(ad);
3277 if (rc != 0) {
3278 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3279 return -1;
3281 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3284 static int fruit_fallocate(struct vfs_handle_struct *handle,
3285 struct files_struct *fsp,
3286 uint32_t mode,
3287 off_t offset,
3288 off_t len)
3290 struct adouble *ad =
3291 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3293 if (ad == NULL) {
3294 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3297 if (!fruit_fsp_recheck(ad, fsp)) {
3298 return -1;
3301 /* Let the pwrite code path handle it. */
3302 errno = ENOSYS;
3303 return -1;
3306 static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3307 struct files_struct *fsp,
3308 off_t offset,
3309 struct adouble *ad)
3311 struct fruit_config_data *config;
3313 SMB_VFS_HANDLE_GET_DATA(handle, config,
3314 struct fruit_config_data, return -1);
3316 if (offset > 60) {
3317 DBG_WARNING("ftruncate %s to %jd",
3318 fsp_str_dbg(fsp), (intmax_t)offset);
3319 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
3320 errno = EOVERFLOW;
3321 return -1;
3324 DBG_WARNING("ignoring ftruncate %s to %jd",
3325 fsp_str_dbg(fsp), (intmax_t)offset);
3326 /* OS X returns success but does nothing */
3327 return 0;
3330 static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3331 struct files_struct *fsp,
3332 off_t offset,
3333 struct adouble *ad)
3335 int rc;
3336 struct fruit_config_data *config;
3338 SMB_VFS_HANDLE_GET_DATA(handle, config,
3339 struct fruit_config_data, return -1);
3341 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3342 return SMB_VFS_FREMOVEXATTR(fsp,
3343 AFPRESOURCE_EA_NETATALK);
3346 rc = SMB_VFS_NEXT_FTRUNCATE(
3347 handle, fsp,
3348 offset + ad_getentryoff(ad, ADEID_RFORK));
3349 if (rc != 0) {
3350 return -1;
3353 if (config->rsrc == FRUIT_RSRC_ADFILE) {
3354 ad_setentrylen(ad, ADEID_RFORK, offset);
3355 rc = ad_write(ad, NULL);
3356 if (rc != 0) {
3357 return -1;
3359 DEBUG(10, ("fruit_ftruncate_rsrc file %s offset %jd\n",
3360 fsp_str_dbg(fsp), (intmax_t)offset));
3363 return 0;
3366 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3367 struct files_struct *fsp,
3368 off_t offset)
3370 int rc = 0;
3371 struct adouble *ad =
3372 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3374 DBG_DEBUG("fruit_ftruncate called for file %s offset %.0f\n",
3375 fsp_str_dbg(fsp), (double)offset);
3377 if (ad == NULL) {
3378 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3381 if (!fruit_fsp_recheck(ad, fsp)) {
3382 return -1;
3385 switch (ad->ad_type) {
3386 case ADOUBLE_META:
3387 rc = fruit_ftruncate_meta(handle, fsp, offset, ad);
3388 break;
3390 case ADOUBLE_RSRC:
3391 rc = fruit_ftruncate_rsrc(handle, fsp, offset, ad);
3392 break;
3394 default:
3395 return -1;
3398 return rc;
3401 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3402 struct smb_request *req,
3403 uint16_t root_dir_fid,
3404 struct smb_filename *smb_fname,
3405 uint32_t access_mask,
3406 uint32_t share_access,
3407 uint32_t create_disposition,
3408 uint32_t create_options,
3409 uint32_t file_attributes,
3410 uint32_t oplock_request,
3411 struct smb2_lease *lease,
3412 uint64_t allocation_size,
3413 uint32_t private_flags,
3414 struct security_descriptor *sd,
3415 struct ea_list *ea_list,
3416 files_struct **result,
3417 int *pinfo,
3418 const struct smb2_create_blobs *in_context_blobs,
3419 struct smb2_create_blobs *out_context_blobs)
3421 NTSTATUS status;
3422 struct fruit_config_data *config = NULL;
3423 files_struct *fsp = NULL;
3425 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3426 if (!NT_STATUS_IS_OK(status)) {
3427 goto fail;
3430 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3431 return NT_STATUS_UNSUCCESSFUL);
3433 status = SMB_VFS_NEXT_CREATE_FILE(
3434 handle, req, root_dir_fid, smb_fname,
3435 access_mask, share_access,
3436 create_disposition, create_options,
3437 file_attributes, oplock_request,
3438 lease,
3439 allocation_size, private_flags,
3440 sd, ea_list, result,
3441 pinfo, in_context_blobs, out_context_blobs);
3442 if (!NT_STATUS_IS_OK(status)) {
3443 return status;
3446 fsp = *result;
3448 if (config->nego_aapl) {
3449 if (config->copyfile_enabled) {
3451 * Set a flag in the fsp. Gets used in
3452 * copychunk to check whether the special
3453 * Apple copyfile semantics for copychunk
3454 * should be allowed in a copychunk request
3455 * with a count of 0.
3457 fsp->aapl_copyfile_supported = true;
3460 if (config->posix_rename && fsp->is_directory) {
3462 * Enable POSIX directory rename behaviour
3464 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
3469 * If this is a plain open for existing files, opening an 0
3470 * byte size resource fork MUST fail with
3471 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
3473 * Cf the vfs_fruit torture tests in test_rfork_create().
3475 if (is_afpresource_stream(fsp->fsp_name) &&
3476 create_disposition == FILE_OPEN)
3478 if (fsp->fsp_name->st.st_ex_size == 0) {
3479 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3480 goto fail;
3484 if (is_ntfs_stream_smb_fname(smb_fname)
3485 || fsp->is_directory) {
3486 return status;
3489 if (config->locking == FRUIT_LOCKING_NETATALK) {
3490 status = fruit_check_access(
3491 handle, *result,
3492 access_mask,
3493 map_share_mode_to_deny_mode(share_access, 0));
3494 if (!NT_STATUS_IS_OK(status)) {
3495 goto fail;
3499 return status;
3501 fail:
3502 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
3504 if (fsp) {
3505 close_file(req, fsp, ERROR_CLOSE);
3506 *result = fsp = NULL;
3509 return status;
3512 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3513 const struct smb_filename *fname,
3514 TALLOC_CTX *mem_ctx,
3515 struct readdir_attr_data **pattr_data)
3517 struct fruit_config_data *config = NULL;
3518 struct readdir_attr_data *attr_data;
3519 NTSTATUS status;
3521 SMB_VFS_HANDLE_GET_DATA(handle, config,
3522 struct fruit_config_data,
3523 return NT_STATUS_UNSUCCESSFUL);
3525 if (!config->use_aapl) {
3526 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3529 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3531 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3532 if (*pattr_data == NULL) {
3533 return NT_STATUS_UNSUCCESSFUL;
3535 attr_data = *pattr_data;
3536 attr_data->type = RDATTR_AAPL;
3539 * Mac metadata: compressed FinderInfo, resource fork length
3540 * and creation date
3542 status = readdir_attr_macmeta(handle, fname, attr_data);
3543 if (!NT_STATUS_IS_OK(status)) {
3545 * Error handling is tricky: if we return failure from
3546 * this function, the corresponding directory entry
3547 * will to be passed to the client, so we really just
3548 * want to error out on fatal errors.
3550 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3551 goto fail;
3556 * UNIX mode
3558 if (config->unix_info_enabled) {
3559 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3563 * max_access
3565 if (!config->readdir_attr_max_access) {
3566 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3567 } else {
3568 status = smbd_calculate_access_mask(
3569 handle->conn,
3570 fname,
3571 false,
3572 SEC_FLAG_MAXIMUM_ALLOWED,
3573 &attr_data->attr_data.aapl.max_access);
3574 if (!NT_STATUS_IS_OK(status)) {
3575 goto fail;
3579 return NT_STATUS_OK;
3581 fail:
3582 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3583 fname->base_name, nt_errstr(status)));
3584 TALLOC_FREE(*pattr_data);
3585 return status;
3588 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3589 files_struct *fsp,
3590 uint32_t security_info,
3591 TALLOC_CTX *mem_ctx,
3592 struct security_descriptor **ppdesc)
3594 NTSTATUS status;
3595 struct security_ace ace;
3596 struct dom_sid sid;
3597 struct fruit_config_data *config;
3599 SMB_VFS_HANDLE_GET_DATA(handle, config,
3600 struct fruit_config_data,
3601 return NT_STATUS_UNSUCCESSFUL);
3603 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3604 mem_ctx, ppdesc);
3605 if (!NT_STATUS_IS_OK(status)) {
3606 return status;
3610 * Add MS NFS style ACEs with uid, gid and mode
3612 if (!config->unix_info_enabled) {
3613 return NT_STATUS_OK;
3616 /* MS NFS style mode */
3617 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3618 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3619 status = security_descriptor_dacl_add(*ppdesc, &ace);
3620 if (!NT_STATUS_IS_OK(status)) {
3621 DEBUG(1,("failed to add MS NFS style ACE\n"));
3622 return status;
3625 /* MS NFS style uid */
3626 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3627 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3628 status = security_descriptor_dacl_add(*ppdesc, &ace);
3629 if (!NT_STATUS_IS_OK(status)) {
3630 DEBUG(1,("failed to add MS NFS style ACE\n"));
3631 return status;
3634 /* MS NFS style gid */
3635 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3636 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3637 status = security_descriptor_dacl_add(*ppdesc, &ace);
3638 if (!NT_STATUS_IS_OK(status)) {
3639 DEBUG(1,("failed to add MS NFS style ACE\n"));
3640 return status;
3643 return NT_STATUS_OK;
3646 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3647 files_struct *fsp,
3648 uint32_t security_info_sent,
3649 const struct security_descriptor *psd)
3651 NTSTATUS status;
3652 bool do_chmod;
3653 mode_t ms_nfs_mode = 0;
3654 int result;
3656 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
3658 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3659 if (!NT_STATUS_IS_OK(status)) {
3660 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3661 return status;
3664 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3665 if (!NT_STATUS_IS_OK(status)) {
3666 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3667 return status;
3670 if (do_chmod) {
3671 if (fsp->fh->fd != -1) {
3672 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3673 } else {
3674 result = SMB_VFS_CHMOD(fsp->conn,
3675 fsp->fsp_name,
3676 ms_nfs_mode);
3679 if (result != 0) {
3680 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3681 result, (unsigned)ms_nfs_mode,
3682 strerror(errno)));
3683 status = map_nt_error_from_unix(errno);
3684 return status;
3688 return NT_STATUS_OK;
3691 struct fruit_copy_chunk_state {
3692 struct vfs_handle_struct *handle;
3693 off_t copied;
3694 struct files_struct *src_fsp;
3695 struct files_struct *dst_fsp;
3696 bool is_copyfile;
3699 static void fruit_copy_chunk_done(struct tevent_req *subreq);
3700 static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
3701 TALLOC_CTX *mem_ctx,
3702 struct tevent_context *ev,
3703 struct files_struct *src_fsp,
3704 off_t src_off,
3705 struct files_struct *dest_fsp,
3706 off_t dest_off,
3707 off_t num)
3709 struct tevent_req *req, *subreq;
3710 struct fruit_copy_chunk_state *fruit_copy_chunk_state;
3711 NTSTATUS status;
3712 struct fruit_config_data *config;
3713 off_t to_copy = num;
3715 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
3716 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
3718 SMB_VFS_HANDLE_GET_DATA(handle, config,
3719 struct fruit_config_data,
3720 return NULL);
3722 req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
3723 struct fruit_copy_chunk_state);
3724 if (req == NULL) {
3725 return NULL;
3727 fruit_copy_chunk_state->handle = handle;
3728 fruit_copy_chunk_state->src_fsp = src_fsp;
3729 fruit_copy_chunk_state->dst_fsp = dest_fsp;
3732 * Check if this a OS X copyfile style copychunk request with
3733 * a requested chunk count of 0 that was translated to a
3734 * copy_chunk_send VFS call overloading the parameters src_off
3735 * = dest_off = num = 0.
3737 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
3738 src_fsp->aapl_copyfile_supported &&
3739 dest_fsp->aapl_copyfile_supported)
3741 status = vfs_stat_fsp(src_fsp);
3742 if (tevent_req_nterror(req, status)) {
3743 return tevent_req_post(req, ev);
3746 to_copy = src_fsp->fsp_name->st.st_ex_size;
3747 fruit_copy_chunk_state->is_copyfile = true;
3750 subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
3751 mem_ctx,
3753 src_fsp,
3754 src_off,
3755 dest_fsp,
3756 dest_off,
3757 to_copy);
3758 if (tevent_req_nomem(subreq, req)) {
3759 return tevent_req_post(req, ev);
3762 tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
3763 return req;
3766 static void fruit_copy_chunk_done(struct tevent_req *subreq)
3768 struct tevent_req *req = tevent_req_callback_data(
3769 subreq, struct tevent_req);
3770 struct fruit_copy_chunk_state *state = tevent_req_data(
3771 req, struct fruit_copy_chunk_state);
3772 NTSTATUS status;
3773 unsigned int num_streams = 0;
3774 struct stream_struct *streams = NULL;
3775 unsigned int i;
3776 struct smb_filename *src_fname_tmp = NULL;
3777 struct smb_filename *dst_fname_tmp = NULL;
3779 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
3780 subreq,
3781 &state->copied);
3782 TALLOC_FREE(subreq);
3783 if (tevent_req_nterror(req, status)) {
3784 return;
3787 if (!state->is_copyfile) {
3788 tevent_req_done(req);
3789 return;
3793 * Now copy all reamining streams. We know the share supports
3794 * streams, because we're in vfs_fruit. We don't do this async
3795 * because streams are few and small.
3797 status = vfs_streaminfo(state->handle->conn, state->src_fsp,
3798 state->src_fsp->fsp_name,
3799 req, &num_streams, &streams);
3800 if (tevent_req_nterror(req, status)) {
3801 return;
3804 if (num_streams == 1) {
3805 /* There is always one stream, ::$DATA. */
3806 tevent_req_done(req);
3807 return;
3810 for (i = 0; i < num_streams; i++) {
3811 DEBUG(10, ("%s: stream: '%s'/%zu\n",
3812 __func__, streams[i].name, (size_t)streams[i].size));
3814 src_fname_tmp = synthetic_smb_fname(
3815 req,
3816 state->src_fsp->fsp_name->base_name,
3817 streams[i].name,
3818 NULL,
3819 state->src_fsp->fsp_name->flags);
3820 if (tevent_req_nomem(src_fname_tmp, req)) {
3821 return;
3824 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
3825 TALLOC_FREE(src_fname_tmp);
3826 continue;
3829 dst_fname_tmp = synthetic_smb_fname(
3830 req,
3831 state->dst_fsp->fsp_name->base_name,
3832 streams[i].name,
3833 NULL,
3834 state->dst_fsp->fsp_name->flags);
3835 if (tevent_req_nomem(dst_fname_tmp, req)) {
3836 TALLOC_FREE(src_fname_tmp);
3837 return;
3840 status = copy_file(req,
3841 state->handle->conn,
3842 src_fname_tmp,
3843 dst_fname_tmp,
3844 OPENX_FILE_CREATE_IF_NOT_EXIST,
3845 0, false);
3846 if (!NT_STATUS_IS_OK(status)) {
3847 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
3848 smb_fname_str_dbg(src_fname_tmp),
3849 smb_fname_str_dbg(dst_fname_tmp),
3850 nt_errstr(status)));
3851 TALLOC_FREE(src_fname_tmp);
3852 TALLOC_FREE(dst_fname_tmp);
3853 tevent_req_nterror(req, status);
3854 return;
3857 TALLOC_FREE(src_fname_tmp);
3858 TALLOC_FREE(dst_fname_tmp);
3861 TALLOC_FREE(streams);
3862 TALLOC_FREE(src_fname_tmp);
3863 TALLOC_FREE(dst_fname_tmp);
3864 tevent_req_done(req);
3867 static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
3868 struct tevent_req *req,
3869 off_t *copied)
3871 struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
3872 req, struct fruit_copy_chunk_state);
3873 NTSTATUS status;
3875 if (tevent_req_is_nterror(req, &status)) {
3876 DEBUG(1, ("server side copy chunk failed: %s\n",
3877 nt_errstr(status)));
3878 *copied = 0;
3879 tevent_req_received(req);
3880 return status;
3883 *copied = fruit_copy_chunk_state->copied;
3884 tevent_req_received(req);
3886 return NT_STATUS_OK;
3889 static struct vfs_fn_pointers vfs_fruit_fns = {
3890 .connect_fn = fruit_connect,
3892 /* File operations */
3893 .chmod_fn = fruit_chmod,
3894 .chown_fn = fruit_chown,
3895 .unlink_fn = fruit_unlink,
3896 .rename_fn = fruit_rename,
3897 .rmdir_fn = fruit_rmdir,
3898 .open_fn = fruit_open,
3899 .pread_fn = fruit_pread,
3900 .pwrite_fn = fruit_pwrite,
3901 .stat_fn = fruit_stat,
3902 .lstat_fn = fruit_lstat,
3903 .fstat_fn = fruit_fstat,
3904 .streaminfo_fn = fruit_streaminfo,
3905 .ntimes_fn = fruit_ntimes,
3906 .ftruncate_fn = fruit_ftruncate,
3907 .fallocate_fn = fruit_fallocate,
3908 .create_file_fn = fruit_create_file,
3909 .readdir_attr_fn = fruit_readdir_attr,
3910 .copy_chunk_send_fn = fruit_copy_chunk_send,
3911 .copy_chunk_recv_fn = fruit_copy_chunk_recv,
3913 /* NT ACL operations */
3914 .fget_nt_acl_fn = fruit_fget_nt_acl,
3915 .fset_nt_acl_fn = fruit_fset_nt_acl,
3918 NTSTATUS vfs_fruit_init(void);
3919 NTSTATUS vfs_fruit_init(void)
3921 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3922 &vfs_fruit_fns);
3923 if (!NT_STATUS_IS_OK(ret)) {
3924 return ret;
3927 vfs_fruit_debug_level = debug_add_class("fruit");
3928 if (vfs_fruit_debug_level == -1) {
3929 vfs_fruit_debug_level = DBGC_VFS;
3930 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3931 "vfs_fruit_init"));
3932 } else {
3933 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3934 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3937 return ret;