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/>.
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"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/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
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
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".
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
90 * - log diagnostic if any needed VFS module is not loaded
91 * (eg with lp_vfs_objects())
95 static int vfs_fruit_debug_level
= DBGC_VFS
;
98 #define DBGC_CLASS vfs_fruit_debug_level
100 #define FRUIT_PARAM_TYPE_NAME "fruit"
101 #define ADOUBLE_NAME_PREFIX "._"
105 * This is hokey, but what else can we do?
107 #if defined(HAVE_ATTROPEN) || defined(FREEBSD)
108 #define AFPINFO_EA_NETATALK "org.netatalk.Metadata"
109 #define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
111 #define AFPINFO_EA_NETATALK "user.org.netatalk.Metadata"
112 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
115 enum apple_fork
{APPLE_FORK_DATA
, APPLE_FORK_RSRC
};
117 enum fruit_rsrc
{FRUIT_RSRC_STREAM
, FRUIT_RSRC_ADFILE
, FRUIT_RSRC_XATTR
};
118 enum fruit_meta
{FRUIT_META_STREAM
, FRUIT_META_NETATALK
};
119 enum fruit_locking
{FRUIT_LOCKING_NETATALK
, FRUIT_LOCKING_NONE
};
120 enum fruit_encoding
{FRUIT_ENC_NATIVE
, FRUIT_ENC_PRIVATE
};
122 struct fruit_config_data
{
123 enum fruit_rsrc rsrc
;
124 enum fruit_meta meta
;
125 enum fruit_locking locking
;
126 enum fruit_encoding encoding
;
129 bool readdir_attr_enabled
;
130 bool unix_info_enabled
;
131 bool copyfile_enabled
;
132 bool veto_appledouble
;
135 * Additional options, all enabled by default,
136 * possibly useful for analyzing performance. The associated
137 * operations with each of them may be expensive, so having
138 * the chance to disable them individually gives a chance
139 * tweaking the setup for the particular usecase.
141 bool readdir_attr_rsize
;
142 bool readdir_attr_finder_info
;
143 bool readdir_attr_max_access
;
146 static const struct enum_list fruit_rsrc
[] = {
147 {FRUIT_RSRC_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
148 {FRUIT_RSRC_ADFILE
, "file"}, /* ._ AppleDouble file */
149 {FRUIT_RSRC_XATTR
, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
153 static const struct enum_list fruit_meta
[] = {
154 {FRUIT_META_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
155 {FRUIT_META_NETATALK
, "netatalk"}, /* Netatalk compatible xattr */
159 static const struct enum_list fruit_locking
[] = {
160 {FRUIT_LOCKING_NETATALK
, "netatalk"}, /* synchronize locks with Netatalk */
161 {FRUIT_LOCKING_NONE
, "none"},
165 static const struct enum_list fruit_encoding
[] = {
166 {FRUIT_ENC_NATIVE
, "native"}, /* map unicode private chars to ASCII */
167 {FRUIT_ENC_PRIVATE
, "private"}, /* keep unicode private chars */
171 /*****************************************************************************
172 * Defines, functions and data structures that deal with AppleDouble
173 *****************************************************************************/
176 * There are two AppleDouble blobs we deal with:
178 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
179 * metadata in an xattr
181 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
184 typedef enum {ADOUBLE_META
, ADOUBLE_RSRC
} adouble_type_t
;
187 #define AD_VERSION2 0x00020000
188 #define AD_VERSION AD_VERSION2
191 * AppleDouble entry IDs.
193 #define ADEID_DFORK 1
194 #define ADEID_RFORK 2
196 #define ADEID_COMMENT 4
197 #define ADEID_ICONBW 5
198 #define ADEID_ICONCOL 6
199 #define ADEID_FILEI 7
200 #define ADEID_FILEDATESI 8
201 #define ADEID_FINDERI 9
202 #define ADEID_MACFILEI 10
203 #define ADEID_PRODOSFILEI 11
204 #define ADEID_MSDOSFILEI 12
205 #define ADEID_SHORTNAME 13
206 #define ADEID_AFPFILEI 14
209 /* Private Netatalk entries */
210 #define ADEID_PRIVDEV 16
211 #define ADEID_PRIVINO 17
212 #define ADEID_PRIVSYN 18
213 #define ADEID_PRIVID 19
214 #define ADEID_MAX (ADEID_PRIVID + 1)
217 * These are the real ids for the private entries,
218 * as stored in the adouble file
220 #define AD_DEV 0x80444556
221 #define AD_INO 0x80494E4F
222 #define AD_SYN 0x8053594E
223 #define AD_ID 0x8053567E
225 /* Number of actually used entries */
226 #define ADEID_NUM_XATTR 8
227 #define ADEID_NUM_DOT_UND 2
228 #define ADEID_NUM_RSRC_XATTR 1
230 /* AppleDouble magic */
231 #define AD_APPLESINGLE_MAGIC 0x00051600
232 #define AD_APPLEDOUBLE_MAGIC 0x00051607
233 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
235 /* Sizes of relevant entry bits */
236 #define ADEDLEN_MAGIC 4
237 #define ADEDLEN_VERSION 4
238 #define ADEDLEN_FILLER 16
239 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
240 #define ADEDLEN_NENTRIES 2
241 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
242 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
243 #define AD_ENTRY_LEN_EID 4
244 #define AD_ENTRY_LEN_OFF 4
245 #define AD_ENTRY_LEN_LEN 4
246 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
249 #define ADEDLEN_NAME 255
250 #define ADEDLEN_COMMENT 200
251 #define ADEDLEN_FILEI 16
252 #define ADEDLEN_FINDERI 32
253 #define ADEDLEN_FILEDATESI 16
254 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
255 #define ADEDLEN_AFPFILEI 4
256 #define ADEDLEN_MACFILEI 4
257 #define ADEDLEN_PRODOSFILEI 8
258 #define ADEDLEN_MSDOSFILEI 2
259 #define ADEDLEN_DID 4
260 #define ADEDLEN_PRIVDEV 8
261 #define ADEDLEN_PRIVINO 8
262 #define ADEDLEN_PRIVSYN 8
263 #define ADEDLEN_PRIVID 4
266 #define ADEDOFF_MAGIC 0
267 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
268 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
269 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
271 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
272 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
273 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
274 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
275 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
277 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
278 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
279 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
280 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
282 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
283 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
284 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
286 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
287 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
288 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
289 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
290 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
291 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
293 #if AD_DATASZ_XATTR != 402
294 #error bad size for AD_DATASZ_XATTR
297 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
298 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
300 #if AD_DATASZ_DOT_UND != 82
301 #error bad size for AD_DATASZ_DOT_UND
305 * Sharemode locks fcntl() offsets
307 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
308 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
310 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
312 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
314 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
315 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
316 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
317 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
318 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
319 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
320 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
321 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
322 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
323 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
325 /* Time stuff we overload the bits a little */
326 #define AD_DATE_CREATE 0
327 #define AD_DATE_MODIFY 4
328 #define AD_DATE_BACKUP 8
329 #define AD_DATE_ACCESS 12
330 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
331 AD_DATE_BACKUP | AD_DATE_ACCESS)
332 #define AD_DATE_UNIX (1 << 10)
333 #define AD_DATE_START 0x80000000
334 #define AD_DATE_DELTA 946684800
335 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
336 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
338 /* Accessor macros */
339 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
340 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
341 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
342 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
343 #define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
351 vfs_handle_struct
*ad_handle
;
352 files_struct
*ad_fsp
;
353 adouble_type_t ad_type
;
356 struct ad_entry ad_eid
[ADEID_MAX
];
360 struct ad_entry_order
{
361 uint32_t id
, offset
, len
;
364 /* Netatalk AppleDouble metadata xattr */
366 struct ad_entry_order entry_order_meta_xattr
[ADEID_NUM_XATTR
+ 1] = {
367 {ADEID_FINDERI
, ADEDOFF_FINDERI_XATTR
, ADEDLEN_FINDERI
},
368 {ADEID_COMMENT
, ADEDOFF_COMMENT_XATTR
, 0},
369 {ADEID_FILEDATESI
, ADEDOFF_FILEDATESI_XATTR
, ADEDLEN_FILEDATESI
},
370 {ADEID_AFPFILEI
, ADEDOFF_AFPFILEI_XATTR
, ADEDLEN_AFPFILEI
},
371 {ADEID_PRIVDEV
, ADEDOFF_PRIVDEV_XATTR
, 0},
372 {ADEID_PRIVINO
, ADEDOFF_PRIVINO_XATTR
, 0},
373 {ADEID_PRIVSYN
, ADEDOFF_PRIVSYN_XATTR
, 0},
374 {ADEID_PRIVID
, ADEDOFF_PRIVID_XATTR
, 0},
378 /* AppleDouble ressource fork file (the ones prefixed by "._") */
380 struct ad_entry_order entry_order_dot_und
[ADEID_NUM_DOT_UND
+ 1] = {
381 {ADEID_FINDERI
, ADEDOFF_FINDERI_DOT_UND
, ADEDLEN_FINDERI
},
382 {ADEID_RFORK
, ADEDOFF_RFORK_DOT_UND
, 0},
387 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
388 * isn't an AppleDouble file, it simply contains the ressource data,
389 * but in order to be able to use some API calls like ad_getentryoff()
390 * we build a fake/helper struct adouble with this entry order struct.
393 struct ad_entry_order entry_order_rsrc_xattr
[ADEID_NUM_RSRC_XATTR
+ 1] = {
398 /* Conversion from enumerated id to on-disk AppleDouble id */
399 #define AD_EID_DISK(a) (set_eid[a])
400 static const uint32_t set_eid
[] = {
401 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
402 AD_DEV
, AD_INO
, AD_SYN
, AD_ID
406 * Forward declarations
408 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
409 adouble_type_t type
, files_struct
*fsp
);
410 static int ad_write(struct adouble
*ad
, const char *path
);
411 static int adouble_path(TALLOC_CTX
*ctx
, const char *path_in
, char **path_out
);
416 static int ad_getdate(const struct adouble
*ad
,
417 unsigned int dateoff
,
420 bool xlate
= (dateoff
& AD_DATE_UNIX
);
422 dateoff
&= AD_DATE_MASK
;
423 if (!ad_getentryoff(ad
, ADEID_FILEDATESI
)) {
427 if (dateoff
> AD_DATE_ACCESS
) {
431 ad_entry(ad
, ADEID_FILEDATESI
) + dateoff
,
435 *date
= AD_DATE_TO_UNIX(*date
);
443 static int ad_setdate(struct adouble
*ad
, unsigned int dateoff
, uint32_t date
)
445 bool xlate
= (dateoff
& AD_DATE_UNIX
);
447 if (!ad_getentryoff(ad
, ADEID_FILEDATESI
)) {
451 dateoff
&= AD_DATE_MASK
;
453 date
= AD_DATE_FROM_UNIX(date
);
456 if (dateoff
> AD_DATE_ACCESS
) {
460 memcpy(ad_entry(ad
, ADEID_FILEDATESI
) + dateoff
, &date
, sizeof(date
));
467 * Map on-disk AppleDouble id to enumerated id
469 static uint32_t get_eid(uint32_t eid
)
477 return ADEID_PRIVDEV
;
479 return ADEID_PRIVINO
;
481 return ADEID_PRIVSYN
;
492 * Pack AppleDouble structure into data buffer
494 static bool ad_pack(struct adouble
*ad
)
501 bufsize
= talloc_get_size(ad
->ad_data
);
503 if (offset
+ ADEDLEN_MAGIC
< offset
||
504 offset
+ ADEDLEN_MAGIC
>= bufsize
) {
507 RSIVAL(ad
->ad_data
, offset
, ad
->ad_magic
);
508 offset
+= ADEDLEN_MAGIC
;
510 if (offset
+ ADEDLEN_VERSION
< offset
||
511 offset
+ ADEDLEN_VERSION
>= bufsize
) {
514 RSIVAL(ad
->ad_data
, offset
, ad
->ad_version
);
515 offset
+= ADEDLEN_VERSION
;
517 if (offset
+ ADEDLEN_FILLER
< offset
||
518 offset
+ ADEDLEN_FILLER
>= bufsize
) {
521 if (ad
->ad_type
== ADOUBLE_RSRC
) {
522 memcpy(ad
->ad_data
+ offset
, AD_FILLER_TAG
, ADEDLEN_FILLER
);
524 offset
+= ADEDLEN_FILLER
;
526 if (offset
+ ADEDLEN_NENTRIES
< offset
||
527 offset
+ ADEDLEN_NENTRIES
>= bufsize
) {
530 offset
+= ADEDLEN_NENTRIES
;
532 for (eid
= 0, nent
= 0; eid
< ADEID_MAX
; eid
++) {
533 if (ad
->ad_eid
[eid
].ade_off
== 0) {
535 * ade_off is also used as indicator whether a
536 * specific entry is used or not
541 if (offset
+ AD_ENTRY_LEN_EID
< offset
||
542 offset
+ AD_ENTRY_LEN_EID
>= bufsize
) {
545 RSIVAL(ad
->ad_data
, offset
, AD_EID_DISK(eid
));
546 offset
+= AD_ENTRY_LEN_EID
;
548 if (offset
+ AD_ENTRY_LEN_OFF
< offset
||
549 offset
+ AD_ENTRY_LEN_OFF
>= bufsize
) {
552 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_off
);
553 offset
+= AD_ENTRY_LEN_OFF
;
555 if (offset
+ AD_ENTRY_LEN_LEN
< offset
||
556 offset
+ AD_ENTRY_LEN_LEN
>= bufsize
) {
559 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_len
);
560 offset
+= AD_ENTRY_LEN_LEN
;
565 if (ADEDOFF_NENTRIES
+ 2 >= bufsize
) {
568 RSSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
, nent
);
574 * Unpack an AppleDouble blob into a struct adoble
576 static bool ad_unpack(struct adouble
*ad
, const int nentries
, size_t filesize
)
578 size_t bufsize
= talloc_get_size(ad
->ad_data
);
580 uint32_t eid
, len
, off
;
583 * The size of the buffer ad->ad_data is checked when read, so
584 * we wouldn't have to check our own offsets, a few extra
585 * checks won't hurt though. We have to check the offsets we
586 * read from the buffer anyway.
589 if (bufsize
< (AD_HEADER_LEN
+ (AD_ENTRY_LEN
* nentries
))) {
590 DEBUG(1, ("bad size\n"));
594 ad
->ad_magic
= RIVAL(ad
->ad_data
, 0);
595 ad
->ad_version
= RIVAL(ad
->ad_data
, ADEDOFF_VERSION
);
596 if ((ad
->ad_magic
!= AD_MAGIC
) || (ad
->ad_version
!= AD_VERSION
)) {
597 DEBUG(1, ("wrong magic or version\n"));
601 adentries
= RSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
);
602 if (adentries
!= nentries
) {
603 DEBUG(1, ("invalid number of entries: %d\n", adentries
));
607 /* now, read in the entry bits */
608 for (i
= 0; i
< adentries
; i
++) {
609 eid
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
));
611 off
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 4);
612 len
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 8);
614 if (!eid
|| eid
> ADEID_MAX
) {
615 DEBUG(1, ("bogus eid %d\n", eid
));
620 * All entries other than the resource fork are
621 * expected to be read into the ad_data buffer, so
622 * ensure the specified offset is within that bound
624 if ((off
> bufsize
) && (eid
!= ADEID_RFORK
)) {
625 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
631 * All entries besides FinderInfo and resource fork
632 * must fit into the buffer. FinderInfo is special as
633 * it may be larger then the default 32 bytes (if it
634 * contains marshalled xattrs), but we will fixup that
635 * in ad_convert(). And the resource fork is never
636 * accessed directly by the ad_data buf (also see
637 * comment above) anyway.
639 if ((eid
!= ADEID_RFORK
) &&
640 (eid
!= ADEID_FINDERI
) &&
641 ((off
+ len
) > bufsize
)) {
642 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
648 * That would be obviously broken
650 if (off
> filesize
) {
651 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
657 * Check for any entry that has its end beyond the
660 if (off
+ len
< off
) {
661 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
662 ", len: %" PRIu32
"\n",
667 if (off
+ len
> filesize
) {
669 * If this is the resource fork entry, we fix
670 * up the length, for any other entry we bail
673 if (eid
!= ADEID_RFORK
) {
674 DEBUG(1, ("bogus eid %d: off: %" PRIu32
675 ", len: %" PRIu32
"\n",
681 * Fixup the resource fork entry by limiting
682 * the size to entryoffset - filesize.
684 len
= filesize
- off
;
685 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
686 ", len: %" PRIu32
"\n", off
, len
));
689 ad
->ad_eid
[eid
].ade_off
= off
;
690 ad
->ad_eid
[eid
].ade_len
= len
;
697 * Convert from Apple's ._ file to Netatalk
699 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
700 * bytes containing packed xattrs. Netatalk can't deal with that, so
701 * we simply discard the packed xattrs.
703 * @return -1 in case an error occured, 0 if no conversion was done, 1
706 static int ad_convert(struct adouble
*ad
, int fd
)
709 char *map
= MAP_FAILED
;
712 origlen
= ad_getentryoff(ad
, ADEID_RFORK
) +
713 ad_getentrylen(ad
, ADEID_RFORK
);
715 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
716 map
= mmap(NULL
, origlen
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
717 if (map
== MAP_FAILED
) {
718 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno
)));
723 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
724 memmove(map
+ ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
,
725 map
+ ad_getentryoff(ad
, ADEID_RFORK
),
726 ad_getentrylen(ad
, ADEID_RFORK
));
729 ad_setentrylen(ad
, ADEID_FINDERI
, ADEDLEN_FINDERI
);
730 ad_setentryoff(ad
, ADEID_RFORK
,
731 ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
);
734 * FIXME: direct ftruncate(), but we don't have a fsp for the
737 rc
= ftruncate(fd
, ad_getentryoff(ad
, ADEID_RFORK
)
738 + ad_getentrylen(ad
, ADEID_RFORK
));
741 if (map
!= MAP_FAILED
) {
742 munmap(map
, origlen
);
748 * Read and parse Netatalk AppleDouble metadata xattr
750 static ssize_t
ad_header_read_meta(struct adouble
*ad
, const char *path
)
756 DEBUG(10, ("reading meta xattr for %s\n", path
));
758 ealen
= SMB_VFS_GETXATTR(ad
->ad_handle
->conn
, path
,
759 AFPINFO_EA_NETATALK
, ad
->ad_data
,
765 if (errno
== ENOATTR
) {
771 DEBUG(2, ("error reading meta xattr: %s\n",
777 if (ealen
!= AD_DATASZ_XATTR
) {
778 DEBUG(2, ("bad size %zd\n", ealen
));
784 /* Now parse entries */
785 ok
= ad_unpack(ad
, ADEID_NUM_XATTR
, AD_DATASZ_XATTR
);
787 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
793 if (!ad_getentryoff(ad
, ADEID_FINDERI
)
794 || !ad_getentryoff(ad
, ADEID_COMMENT
)
795 || !ad_getentryoff(ad
, ADEID_FILEDATESI
)
796 || !ad_getentryoff(ad
, ADEID_AFPFILEI
)
797 || !ad_getentryoff(ad
, ADEID_PRIVDEV
)
798 || !ad_getentryoff(ad
, ADEID_PRIVINO
)
799 || !ad_getentryoff(ad
, ADEID_PRIVSYN
)
800 || !ad_getentryoff(ad
, ADEID_PRIVID
)) {
801 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
808 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path
, rc
));
812 if (errno
== EINVAL
) {
814 removexattr(path
, AFPINFO_EA_NETATALK
);
823 * Read and parse resource fork, either ._ AppleDouble file or xattr
825 static ssize_t
ad_header_read_rsrc(struct adouble
*ad
, const char *path
)
827 struct fruit_config_data
*config
= NULL
;
834 struct adouble
*meta_ad
= NULL
;
835 SMB_STRUCT_STAT sbuf
;
839 SMB_VFS_HANDLE_GET_DATA(ad
->ad_handle
, config
,
840 struct fruit_config_data
, return -1);
842 /* Try rw first so we can use the fd in ad_convert() */
845 if (ad
->ad_fsp
&& ad
->ad_fsp
->fh
&& (ad
->ad_fsp
->fh
->fd
!= -1)) {
846 fd
= ad
->ad_fsp
->fh
->fd
;
848 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
849 adpath
= talloc_strdup(talloc_tos(), path
);
851 rc
= adouble_path(talloc_tos(), path
, &adpath
);
858 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
859 #ifndef HAVE_ATTROPEN
864 /* FIXME: direct Solaris xattr syscall */
865 fd
= attropen(adpath
, AFPRESOURCE_EA_NETATALK
,
869 /* FIXME: direct open(), don't have an fsp */
870 fd
= open(adpath
, mode
);
877 if (mode
== O_RDWR
) {
881 /* fall through ... */
883 DEBUG(2, ("open AppleDouble: %s, %s\n",
884 adpath
, strerror(errno
)));
892 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
893 /* FIXME: direct sys_fstat(), don't have an fsp */
896 lp_fake_directory_create_times(
897 SNUM(ad
->ad_handle
->conn
)));
901 len
= sbuf
.st_ex_size
;
902 ad_setentrylen(ad
, ADEID_RFORK
, len
);
904 /* FIXME: direct sys_pread(), don't have an fsp */
905 len
= sys_pread(fd
, ad
->ad_data
, AD_DATASZ_DOT_UND
, 0);
906 if (len
!= AD_DATASZ_DOT_UND
) {
907 DEBUG(2, ("%s: bad size: %zd\n",
908 strerror(errno
), len
));
913 /* FIXME: direct sys_fstat(), we don't have an fsp */
914 rc
= sys_fstat(fd
, &sbuf
,
915 lp_fake_directory_create_times(
916 SNUM(ad
->ad_handle
->conn
)));
921 /* Now parse entries */
922 ok
= ad_unpack(ad
, ADEID_NUM_DOT_UND
, sbuf
.st_ex_size
);
924 DEBUG(1, ("invalid AppleDouble ressource %s\n", path
));
930 if ((ad_getentryoff(ad
, ADEID_FINDERI
)
931 != ADEDOFF_FINDERI_DOT_UND
)
932 || (ad_getentrylen(ad
, ADEID_FINDERI
)
934 || (ad_getentryoff(ad
, ADEID_RFORK
)
935 < ADEDOFF_RFORK_DOT_UND
)) {
936 DEBUG(2, ("invalid AppleDouble ressource %s\n", path
));
943 && (ad_getentrylen(ad
, ADEID_FINDERI
) > ADEDLEN_FINDERI
)) {
944 rc
= ad_convert(ad
, fd
);
950 * Can't use ad_write() because we might not have a fsp
956 /* FIXME: direct sys_pwrite(), don't have an fsp */
957 len
= sys_pwrite(fd
, ad
->ad_data
,
958 AD_DATASZ_DOT_UND
, 0);
959 if (len
!= AD_DATASZ_DOT_UND
) {
960 DEBUG(2, ("%s: bad size: %zd\n", adpath
, len
));
965 meta_ad
= ad_init(talloc_tos(), ad
->ad_handle
,
967 if (meta_ad
== NULL
) {
972 memcpy(ad_entry(meta_ad
, ADEID_FINDERI
),
973 ad_entry(ad
, ADEID_FINDERI
),
976 rc
= ad_write(meta_ad
, path
);
984 DEBUG(10, ("opened AppleDouble: %s\n", path
));
991 if (opened
&& fd
!= -1) {
995 TALLOC_FREE(meta_ad
);
1003 * Read and unpack an AppleDouble metadata xattr or resource
1005 static ssize_t
ad_read(struct adouble
*ad
, const char *path
)
1007 switch (ad
->ad_type
) {
1009 return ad_header_read_meta(ad
, path
);
1011 return ad_header_read_rsrc(ad
, path
);
1018 * Allocate a struct adouble without initialiing it
1020 * The struct is either hang of the fsp extension context or if fsp is
1023 * @param[in] ctx talloc context
1024 * @param[in] handle vfs handle
1025 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1027 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1028 * added as an fsp extension
1030 * @return adouble handle
1032 static struct adouble
*ad_alloc(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1033 adouble_type_t type
, files_struct
*fsp
)
1038 struct fruit_config_data
*config
;
1040 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1041 struct fruit_config_data
, return NULL
);
1045 adsize
= AD_DATASZ_XATTR
;
1048 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1049 adsize
= AD_DATASZ_DOT_UND
;
1057 ad
= talloc_zero(ctx
, struct adouble
);
1063 ad
->ad_data
= talloc_zero_array(ad
, char, adsize
);
1066 ad
= (struct adouble
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
1074 ad
->ad_data
= talloc_zero_array(
1075 VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
1081 if (adsize
&& ad
->ad_data
== NULL
) {
1085 ad
->ad_handle
= handle
;
1087 ad
->ad_magic
= AD_MAGIC
;
1088 ad
->ad_version
= AD_VERSION
;
1098 * Allocate and initialize a new struct adouble
1100 * @param[in] ctx talloc context
1101 * @param[in] handle vfs handle
1102 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1103 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1105 * @return adouble handle, initialized
1107 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1108 adouble_type_t type
, files_struct
*fsp
)
1111 const struct ad_entry_order
*eid
;
1112 struct adouble
*ad
= NULL
;
1113 struct fruit_config_data
*config
;
1114 time_t t
= time(NULL
);
1116 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1117 struct fruit_config_data
, return NULL
);
1121 eid
= entry_order_meta_xattr
;
1124 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1125 eid
= entry_order_dot_und
;
1127 eid
= entry_order_rsrc_xattr
;
1134 ad
= ad_alloc(ctx
, handle
, type
, fsp
);
1140 ad
->ad_eid
[eid
->id
].ade_off
= eid
->offset
;
1141 ad
->ad_eid
[eid
->id
].ade_len
= eid
->len
;
1145 /* put something sane in the date fields */
1146 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
, t
);
1147 ad_setdate(ad
, AD_DATE_MODIFY
| AD_DATE_UNIX
, t
);
1148 ad_setdate(ad
, AD_DATE_ACCESS
| AD_DATE_UNIX
, t
);
1149 ad_setdate(ad
, AD_DATE_BACKUP
, htonl(AD_DATE_START
));
1158 * Return AppleDouble data for a file
1160 * @param[in] ctx talloc context
1161 * @param[in] handle vfs handle
1162 * @param[in] path pathname to file or directory
1163 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1165 * @return talloced struct adouble or NULL on error
1167 static struct adouble
*ad_get(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1168 const char *path
, adouble_type_t type
)
1172 struct adouble
*ad
= NULL
;
1174 DEBUG(10, ("ad_get(%s) called for %s\n",
1175 type
== ADOUBLE_META
? "meta" : "rsrc", path
));
1177 ad
= ad_alloc(ctx
, handle
, type
, NULL
);
1183 len
= ad_read(ad
, path
);
1185 DEBUG(10, ("error reading AppleDouble for %s\n", path
));
1191 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1192 type
== ADOUBLE_META
? "meta" : "rsrc", path
, rc
));
1201 * Set AppleDouble metadata on a file or directory
1203 * @param[in] ad adouble handle
1205 * @param[in] path pathname to file or directory, may be NULL for a
1208 * @return status code, 0 means success
1210 static int ad_write(struct adouble
*ad
, const char *path
)
1220 switch (ad
->ad_type
) {
1222 rc
= SMB_VFS_SETXATTR(ad
->ad_handle
->conn
, path
,
1223 AFPINFO_EA_NETATALK
, ad
->ad_data
,
1224 AD_DATASZ_XATTR
, 0);
1227 if ((ad
->ad_fsp
== NULL
)
1228 || (ad
->ad_fsp
->fh
== NULL
)
1229 || (ad
->ad_fsp
->fh
->fd
== -1)) {
1233 /* FIXME: direct sys_pwrite(), don't have an fsp */
1234 len
= sys_pwrite(ad
->ad_fsp
->fh
->fd
, ad
->ad_data
,
1235 talloc_get_size(ad
->ad_data
), 0);
1236 if (len
!= talloc_get_size(ad
->ad_data
)) {
1237 DEBUG(1, ("short write on %s: %zd",
1238 fsp_str_dbg(ad
->ad_fsp
), len
));
1250 /*****************************************************************************
1252 *****************************************************************************/
1254 static bool is_afpinfo_stream(const struct smb_filename
*smb_fname
)
1256 if (strncasecmp_m(smb_fname
->stream_name
,
1257 AFPINFO_STREAM_NAME
,
1258 strlen(AFPINFO_STREAM_NAME
)) == 0) {
1264 static bool is_afpresource_stream(const struct smb_filename
*smb_fname
)
1266 if (strncasecmp_m(smb_fname
->stream_name
,
1267 AFPRESOURCE_STREAM_NAME
,
1268 strlen(AFPRESOURCE_STREAM_NAME
)) == 0) {
1275 * Test whether stream is an Apple stream, not used atm
1278 static bool is_apple_stream(const struct smb_filename
*smb_fname
)
1280 if (is_afpinfo_stream(smb_fname
)) {
1283 if (is_afpresource_stream(smb_fname
)) {
1291 * Initialize config struct from our smb.conf config parameters
1293 static int init_fruit_config(vfs_handle_struct
*handle
)
1295 struct fruit_config_data
*config
;
1298 config
= talloc_zero(handle
->conn
, struct fruit_config_data
);
1300 DEBUG(1, ("talloc_zero() failed\n"));
1305 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1306 "ressource", fruit_rsrc
, FRUIT_RSRC_ADFILE
);
1307 if (enumval
== -1) {
1308 DEBUG(1, ("value for %s: ressource type unknown\n",
1309 FRUIT_PARAM_TYPE_NAME
));
1312 config
->rsrc
= (enum fruit_rsrc
)enumval
;
1314 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1315 "metadata", fruit_meta
, FRUIT_META_NETATALK
);
1316 if (enumval
== -1) {
1317 DEBUG(1, ("value for %s: metadata type unknown\n",
1318 FRUIT_PARAM_TYPE_NAME
));
1321 config
->meta
= (enum fruit_meta
)enumval
;
1323 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1324 "locking", fruit_locking
, FRUIT_LOCKING_NONE
);
1325 if (enumval
== -1) {
1326 DEBUG(1, ("value for %s: locking type unknown\n",
1327 FRUIT_PARAM_TYPE_NAME
));
1330 config
->locking
= (enum fruit_locking
)enumval
;
1332 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1333 "encoding", fruit_encoding
, FRUIT_ENC_PRIVATE
);
1334 if (enumval
== -1) {
1335 DEBUG(1, ("value for %s: encoding type unknown\n",
1336 FRUIT_PARAM_TYPE_NAME
));
1339 config
->encoding
= (enum fruit_encoding
)enumval
;
1341 config
->veto_appledouble
= lp_parm_bool(
1342 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1343 "veto_appledouble", true);
1345 config
->use_aapl
= lp_parm_bool(
1346 -1, FRUIT_PARAM_TYPE_NAME
, "aapl", true);
1348 config
->unix_info_enabled
= lp_parm_bool(
1349 -1, FRUIT_PARAM_TYPE_NAME
, "nfs_aces", true);
1351 config
->use_copyfile
= lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
,
1354 config
->readdir_attr_rsize
= lp_parm_bool(
1355 SNUM(handle
->conn
), "readdir_attr", "aapl_rsize", true);
1357 config
->readdir_attr_finder_info
= lp_parm_bool(
1358 SNUM(handle
->conn
), "readdir_attr", "aapl_finder_info", true);
1360 config
->readdir_attr_max_access
= lp_parm_bool(
1361 SNUM(handle
->conn
), "readdir_attr", "aapl_max_access", true);
1363 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
1364 NULL
, struct fruit_config_data
,
1371 * Prepend "._" to a basename
1373 static int adouble_path(TALLOC_CTX
*ctx
, const char *path_in
, char **path_out
)
1378 if (!parent_dirname(ctx
, path_in
, &parent
, &base
)) {
1382 *path_out
= talloc_asprintf(ctx
, "%s/._%s", parent
, base
);
1383 if (*path_out
== NULL
) {
1391 * Allocate and initialize an AfpInfo struct
1393 static AfpInfo
*afpinfo_new(TALLOC_CTX
*ctx
)
1395 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
1399 ai
->afpi_Signature
= AFP_Signature
;
1400 ai
->afpi_Version
= AFP_Version
;
1401 ai
->afpi_BackupTime
= AD_DATE_START
;
1406 * Pack an AfpInfo struct into a buffer
1408 * Buffer size must be at least AFP_INFO_SIZE
1409 * Returns size of packed buffer
1411 static ssize_t
afpinfo_pack(const AfpInfo
*ai
, char *buf
)
1413 memset(buf
, 0, AFP_INFO_SIZE
);
1415 RSIVAL(buf
, 0, ai
->afpi_Signature
);
1416 RSIVAL(buf
, 4, ai
->afpi_Version
);
1417 RSIVAL(buf
, 12, ai
->afpi_BackupTime
);
1418 memcpy(buf
+ 16, ai
->afpi_FinderInfo
, sizeof(ai
->afpi_FinderInfo
));
1420 return AFP_INFO_SIZE
;
1424 * Unpack a buffer into a AfpInfo structure
1426 * Buffer size must be at least AFP_INFO_SIZE
1427 * Returns allocated AfpInfo struct
1429 static AfpInfo
*afpinfo_unpack(TALLOC_CTX
*ctx
, const void *data
)
1431 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
1436 ai
->afpi_Signature
= RIVAL(data
, 0);
1437 ai
->afpi_Version
= RIVAL(data
, 4);
1438 ai
->afpi_BackupTime
= RIVAL(data
, 12);
1439 memcpy(ai
->afpi_FinderInfo
, (const char *)data
+ 16,
1440 sizeof(ai
->afpi_FinderInfo
));
1442 if (ai
->afpi_Signature
!= AFP_Signature
1443 || ai
->afpi_Version
!= AFP_Version
) {
1444 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1452 * Fake an inode number from the md5 hash of the (xattr) name
1454 static SMB_INO_T
fruit_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
1457 unsigned char hash
[16];
1461 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
1462 SMB_ASSERT(upper_sname
!= NULL
);
1465 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_dev
),
1466 sizeof(sbuf
->st_ex_dev
));
1467 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_ino
),
1468 sizeof(sbuf
->st_ex_ino
));
1469 MD5Update(&ctx
, (unsigned char *)upper_sname
,
1470 talloc_get_size(upper_sname
)-1);
1471 MD5Final(hash
, &ctx
);
1473 TALLOC_FREE(upper_sname
);
1475 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1476 memcpy(&result
, hash
, sizeof(result
));
1478 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1479 sname
, (unsigned long long)result
));
1485 * Ensure ad_fsp is still valid
1487 static bool fruit_fsp_recheck(struct adouble
*ad
, files_struct
*fsp
)
1489 if (ad
->ad_fsp
== fsp
) {
1497 static bool add_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
1498 struct stream_struct
**streams
,
1499 const char *name
, off_t size
,
1502 struct stream_struct
*tmp
;
1504 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
1510 tmp
[*num_streams
].name
= talloc_asprintf(tmp
, "%s:$DATA", name
);
1511 if (tmp
[*num_streams
].name
== NULL
) {
1515 tmp
[*num_streams
].size
= size
;
1516 tmp
[*num_streams
].alloc_size
= alloc_size
;
1523 static bool empty_finderinfo(const struct adouble
*ad
)
1526 char emptybuf
[ADEDLEN_FINDERI
] = {0};
1527 if (memcmp(emptybuf
,
1528 ad_entry(ad
, ADEID_FINDERI
),
1529 ADEDLEN_FINDERI
) == 0) {
1536 * Update btime with btime from Netatalk
1538 static void update_btime(vfs_handle_struct
*handle
,
1539 struct smb_filename
*smb_fname
)
1542 struct timespec creation_time
= {0};
1545 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
, ADOUBLE_META
);
1549 if (ad_getdate(ad
, AD_DATE_UNIX
| AD_DATE_CREATE
, &t
) != 0) {
1555 creation_time
.tv_sec
= convert_uint32_t_to_time_t(t
);
1556 update_stat_ex_create_time(&smb_fname
->st
, creation_time
);
1562 * Map an access mask to a Netatalk single byte byte range lock
1564 static off_t
access_to_netatalk_brl(enum apple_fork fork_type
,
1565 uint32_t access_mask
)
1569 switch (access_mask
) {
1570 case FILE_READ_DATA
:
1571 offset
= AD_FILELOCK_OPEN_RD
;
1574 case FILE_WRITE_DATA
:
1575 case FILE_APPEND_DATA
:
1576 offset
= AD_FILELOCK_OPEN_WR
;
1580 offset
= AD_FILELOCK_OPEN_NONE
;
1584 if (fork_type
== APPLE_FORK_RSRC
) {
1585 if (offset
== AD_FILELOCK_OPEN_NONE
) {
1586 offset
= AD_FILELOCK_RSRC_OPEN_NONE
;
1596 * Map a deny mode to a Netatalk brl
1598 static off_t
denymode_to_netatalk_brl(enum apple_fork fork_type
,
1603 switch (deny_mode
) {
1605 offset
= AD_FILELOCK_DENY_RD
;
1609 offset
= AD_FILELOCK_DENY_WR
;
1613 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1616 if (fork_type
== APPLE_FORK_RSRC
) {
1624 * Call fcntl() with an exclusive F_GETLK request in order to
1625 * determine if there's an exisiting shared lock
1627 * @return true if the requested lock was found or any error occured
1628 * false if the lock was not found
1630 static bool test_netatalk_lock(files_struct
*fsp
, off_t in_offset
)
1633 off_t offset
= in_offset
;
1638 result
= SMB_VFS_GETLOCK(fsp
, &offset
, &len
, &type
, &pid
);
1639 if (result
== false) {
1643 if (type
!= F_UNLCK
) {
1650 static NTSTATUS
fruit_check_access(vfs_handle_struct
*handle
,
1652 uint32_t access_mask
,
1655 NTSTATUS status
= NT_STATUS_OK
;
1656 struct byte_range_lock
*br_lck
= NULL
;
1657 bool open_for_reading
, open_for_writing
, deny_read
, deny_write
;
1660 /* FIXME: hardcoded data fork, add resource fork */
1661 enum apple_fork fork_type
= APPLE_FORK_DATA
;
1663 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1665 access_mask
& FILE_READ_DATA
? "READ" :"-",
1666 access_mask
& FILE_WRITE_DATA
? "WRITE" : "-",
1667 deny_mode
& DENY_READ
? "DENY_READ" : "-",
1668 deny_mode
& DENY_WRITE
? "DENY_WRITE" : "-"));
1671 * Check read access and deny read mode
1673 if ((access_mask
& FILE_READ_DATA
) || (deny_mode
& DENY_READ
)) {
1675 open_for_reading
= test_netatalk_lock(
1676 fsp
, access_to_netatalk_brl(fork_type
, FILE_READ_DATA
));
1678 deny_read
= test_netatalk_lock(
1679 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_READ
));
1681 DEBUG(10, ("read: %s, deny_write: %s\n",
1682 open_for_reading
== true ? "yes" : "no",
1683 deny_read
== true ? "yes" : "no"));
1685 if (((access_mask
& FILE_READ_DATA
) && deny_read
)
1686 || ((deny_mode
& DENY_READ
) && open_for_reading
)) {
1687 return NT_STATUS_SHARING_VIOLATION
;
1691 if (access_mask
& FILE_READ_DATA
) {
1692 off
= access_to_netatalk_brl(fork_type
, FILE_READ_DATA
);
1694 handle
->conn
->sconn
->msg_ctx
, fsp
,
1695 fsp
->op
->global
->open_persistent_id
, 1, off
,
1696 READ_LOCK
, POSIX_LOCK
, false,
1699 if (!NT_STATUS_IS_OK(status
)) {
1702 TALLOC_FREE(br_lck
);
1705 if (deny_mode
& DENY_READ
) {
1706 off
= denymode_to_netatalk_brl(fork_type
, DENY_READ
);
1708 handle
->conn
->sconn
->msg_ctx
, fsp
,
1709 fsp
->op
->global
->open_persistent_id
, 1, off
,
1710 READ_LOCK
, POSIX_LOCK
, false,
1713 if (!NT_STATUS_IS_OK(status
)) {
1716 TALLOC_FREE(br_lck
);
1721 * Check write access and deny write mode
1723 if ((access_mask
& FILE_WRITE_DATA
) || (deny_mode
& DENY_WRITE
)) {
1725 open_for_writing
= test_netatalk_lock(
1726 fsp
, access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
));
1728 deny_write
= test_netatalk_lock(
1729 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_WRITE
));
1731 DEBUG(10, ("write: %s, deny_write: %s\n",
1732 open_for_writing
== true ? "yes" : "no",
1733 deny_write
== true ? "yes" : "no"));
1735 if (((access_mask
& FILE_WRITE_DATA
) && deny_write
)
1736 || ((deny_mode
& DENY_WRITE
) && open_for_writing
)) {
1737 return NT_STATUS_SHARING_VIOLATION
;
1741 if (access_mask
& FILE_WRITE_DATA
) {
1742 off
= access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
);
1744 handle
->conn
->sconn
->msg_ctx
, fsp
,
1745 fsp
->op
->global
->open_persistent_id
, 1, off
,
1746 READ_LOCK
, POSIX_LOCK
, false,
1749 if (!NT_STATUS_IS_OK(status
)) {
1752 TALLOC_FREE(br_lck
);
1755 if (deny_mode
& DENY_WRITE
) {
1756 off
= denymode_to_netatalk_brl(fork_type
, DENY_WRITE
);
1758 handle
->conn
->sconn
->msg_ctx
, fsp
,
1759 fsp
->op
->global
->open_persistent_id
, 1, off
,
1760 READ_LOCK
, POSIX_LOCK
, false,
1763 if (!NT_STATUS_IS_OK(status
)) {
1766 TALLOC_FREE(br_lck
);
1770 TALLOC_FREE(br_lck
);
1775 static NTSTATUS
check_aapl(vfs_handle_struct
*handle
,
1776 struct smb_request
*req
,
1777 const struct smb2_create_blobs
*in_context_blobs
,
1778 struct smb2_create_blobs
*out_context_blobs
)
1780 struct fruit_config_data
*config
;
1782 struct smb2_create_blob
*aapl
= NULL
;
1786 DATA_BLOB blob
= data_blob_talloc(req
, NULL
, 0);
1787 uint64_t req_bitmap
, client_caps
;
1788 uint64_t server_caps
= SMB2_CRTCTX_AAPL_UNIX_BASED
;
1792 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
1793 return NT_STATUS_UNSUCCESSFUL
);
1795 if (!config
->use_aapl
1796 || in_context_blobs
== NULL
1797 || out_context_blobs
== NULL
) {
1798 return NT_STATUS_OK
;
1801 aapl
= smb2_create_blob_find(in_context_blobs
,
1802 SMB2_CREATE_TAG_AAPL
);
1804 return NT_STATUS_OK
;
1807 if (aapl
->data
.length
!= 24) {
1808 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1809 (uintmax_t)aapl
->data
.length
));
1810 return NT_STATUS_INVALID_PARAMETER
;
1813 cmd
= IVAL(aapl
->data
.data
, 0);
1814 if (cmd
!= SMB2_CRTCTX_AAPL_SERVER_QUERY
) {
1815 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd
));
1816 return NT_STATUS_INVALID_PARAMETER
;
1819 req_bitmap
= BVAL(aapl
->data
.data
, 8);
1820 client_caps
= BVAL(aapl
->data
.data
, 16);
1822 SIVAL(p
, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY
);
1824 SBVAL(p
, 8, req_bitmap
);
1825 ok
= data_blob_append(req
, &blob
, p
, 16);
1827 return NT_STATUS_UNSUCCESSFUL
;
1830 if (req_bitmap
& SMB2_CRTCTX_AAPL_SERVER_CAPS
) {
1831 if ((client_caps
& SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
) &&
1832 (handle
->conn
->tcon
->compat
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
1833 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
;
1834 config
->readdir_attr_enabled
= true;
1837 if (config
->use_copyfile
) {
1838 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE
;
1839 config
->copyfile_enabled
= true;
1843 * The client doesn't set the flag, so we can't check
1844 * for it and just set it unconditionally
1846 if (config
->unix_info_enabled
) {
1847 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE
;
1850 SBVAL(p
, 0, server_caps
);
1851 ok
= data_blob_append(req
, &blob
, p
, 8);
1853 return NT_STATUS_UNSUCCESSFUL
;
1857 if (req_bitmap
& SMB2_CRTCTX_AAPL_VOLUME_CAPS
) {
1859 lp_case_sensitive(SNUM(handle
->conn
->tcon
->compat
)) ?
1860 SMB2_CRTCTX_AAPL_CASE_SENSITIVE
: 0);
1861 ok
= data_blob_append(req
, &blob
, p
, 8);
1863 return NT_STATUS_UNSUCCESSFUL
;
1867 if (req_bitmap
& SMB2_CRTCTX_AAPL_MODEL_INFO
) {
1868 ok
= convert_string_talloc(req
,
1869 CH_UNIX
, CH_UTF16LE
,
1870 "Samba", strlen("Samba"),
1873 return NT_STATUS_UNSUCCESSFUL
;
1877 SIVAL(p
+ 4, 0, modellen
);
1878 ok
= data_blob_append(req
, &blob
, p
, 8);
1881 return NT_STATUS_UNSUCCESSFUL
;
1884 ok
= data_blob_append(req
, &blob
, model
, modellen
);
1887 return NT_STATUS_UNSUCCESSFUL
;
1891 status
= smb2_create_blob_add(out_context_blobs
,
1893 SMB2_CREATE_TAG_AAPL
,
1899 static NTSTATUS
readdir_attr_macmeta(struct vfs_handle_struct
*handle
,
1900 const struct smb_filename
*smb_fname
,
1901 struct readdir_attr_data
*attr_data
)
1903 NTSTATUS status
= NT_STATUS_OK
;
1904 uint32_t date_added
;
1905 struct adouble
*ad
= NULL
;
1906 struct fruit_config_data
*config
= NULL
;
1908 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1909 struct fruit_config_data
,
1910 return NT_STATUS_UNSUCCESSFUL
);
1913 /* Ensure we return a default value in the creation_date field */
1914 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
, 12, AD_DATE_START
);
1917 * Resource fork length
1920 if (config
->readdir_attr_rsize
) {
1921 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
,
1924 attr_data
->attr_data
.aapl
.rfork_size
= ad_getentrylen(
1934 if (config
->readdir_attr_finder_info
) {
1935 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
,
1938 if (S_ISREG(smb_fname
->st
.st_ex_mode
)) {
1940 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0],
1941 ad_entry(ad
, ADEID_FINDERI
), 4);
1943 /* finder_creator */
1944 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 4,
1945 ad_entry(ad
, ADEID_FINDERI
) + 4, 4);
1949 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 8,
1950 ad_entry(ad
, ADEID_FINDERI
) + 8, 2);
1952 /* finder_ext_flags */
1953 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 10,
1954 ad_entry(ad
, ADEID_FINDERI
) + 24, 2);
1957 date_added
= convert_time_t_to_uint32_t(
1958 smb_fname
->st
.st_ex_btime
.tv_sec
- AD_DATE_DELTA
);
1959 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
[0], 12, date_added
);
1969 /* Search MS NFS style ACE with UNIX mode */
1970 static NTSTATUS
check_ms_nfs(vfs_handle_struct
*handle
,
1972 const struct security_descriptor
*psd
,
1977 struct fruit_config_data
*config
= NULL
;
1981 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1982 struct fruit_config_data
,
1983 return NT_STATUS_UNSUCCESSFUL
);
1985 if (psd
->dacl
== NULL
|| !config
->unix_info_enabled
) {
1986 return NT_STATUS_OK
;
1989 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
1990 if (dom_sid_compare_domain(
1991 &global_sid_Unix_NFS_Mode
,
1992 &psd
->dacl
->aces
[i
].trustee
) == 0) {
1993 *pmode
= (mode_t
)psd
->dacl
->aces
[i
].trustee
.sub_auths
[2];
1994 *pmode
&= (S_IRWXU
| S_IRWXG
| S_IRWXO
);
1997 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1998 fsp_str_dbg(fsp
), (unsigned)(*pmode
)));
2003 return NT_STATUS_OK
;
2006 /****************************************************************************
2008 ****************************************************************************/
2010 static int fruit_connect(vfs_handle_struct
*handle
,
2011 const char *service
,
2015 char *list
= NULL
, *newlist
= NULL
;
2016 struct fruit_config_data
*config
;
2018 DEBUG(10, ("fruit_connect\n"));
2020 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
2025 rc
= init_fruit_config(handle
);
2030 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2031 struct fruit_config_data
, return -1);
2033 if (config
->veto_appledouble
) {
2034 list
= lp_veto_files(talloc_tos(), SNUM(handle
->conn
));
2037 if (strstr(list
, "/" ADOUBLE_NAME_PREFIX
"*/") == NULL
) {
2038 newlist
= talloc_asprintf(
2040 "%s/" ADOUBLE_NAME_PREFIX
"*/",
2042 lp_do_parameter(SNUM(handle
->conn
),
2047 lp_do_parameter(SNUM(handle
->conn
),
2049 "/" ADOUBLE_NAME_PREFIX
"*/");
2055 if (config
->encoding
== FRUIT_ENC_NATIVE
) {
2059 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2060 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2061 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2062 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2063 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2064 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2065 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2066 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2067 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2068 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2075 static int fruit_open_meta(vfs_handle_struct
*handle
,
2076 struct smb_filename
*smb_fname
,
2077 files_struct
*fsp
, int flags
, mode_t mode
)
2080 struct fruit_config_data
*config
= NULL
;
2081 struct smb_filename
*smb_fname_base
= NULL
;
2084 struct adouble
*ad
= NULL
;
2086 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname
)));
2088 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2089 struct fruit_config_data
, return -1);
2091 if (config
->meta
== FRUIT_META_STREAM
) {
2092 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2095 /* Create an smb_filename with stream_name == NULL. */
2096 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
2097 smb_fname
->base_name
, NULL
, NULL
);
2099 if (smb_fname_base
== NULL
) {
2106 * We use baseflags to turn off nasty side-effects when opening the
2110 baseflags
&= ~O_TRUNC
;
2111 baseflags
&= ~O_EXCL
;
2112 baseflags
&= ~O_CREAT
;
2114 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
2118 * It is legit to open a stream on a directory, but the base
2119 * fd has to be read-only.
2121 if ((hostfd
== -1) && (errno
== EISDIR
)) {
2122 baseflags
&= ~O_ACCMODE
;
2123 baseflags
|= O_RDONLY
;
2124 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
2128 TALLOC_FREE(smb_fname_base
);
2135 if (flags
& (O_CREAT
| O_TRUNC
)) {
2137 * The attribute does not exist or needs to be truncated,
2138 * create an AppleDouble EA
2140 ad
= ad_init(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2141 handle
, ADOUBLE_META
, fsp
);
2147 rc
= ad_write(ad
, smb_fname
->base_name
);
2153 ad
= ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2154 handle
, ADOUBLE_META
, fsp
);
2159 if (ad_read(ad
, smb_fname
->base_name
) == -1) {
2166 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc
, hostfd
));
2168 int saved_errno
= errno
;
2171 * BUGBUGBUG -- we would need to call
2172 * fd_close_posix here, but we don't have a
2175 fsp
->fh
->fd
= hostfd
;
2179 errno
= saved_errno
;
2184 static int fruit_open_rsrc(vfs_handle_struct
*handle
,
2185 struct smb_filename
*smb_fname
,
2186 files_struct
*fsp
, int flags
, mode_t mode
)
2189 struct fruit_config_data
*config
= NULL
;
2190 struct adouble
*ad
= NULL
;
2191 struct smb_filename
*smb_fname_base
= NULL
;
2192 char *adpath
= NULL
;
2195 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname
)));
2197 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2198 struct fruit_config_data
, return -1);
2200 switch (config
->rsrc
) {
2201 case FRUIT_RSRC_STREAM
:
2202 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2203 case FRUIT_RSRC_XATTR
:
2204 #ifdef HAVE_ATTROPEN
2205 hostfd
= attropen(smb_fname
->base_name
,
2206 AFPRESOURCE_EA_NETATALK
, flags
, mode
);
2210 ad
= ad_init(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2211 handle
, ADOUBLE_RSRC
, fsp
);
2225 if (!(flags
& O_CREAT
) && !VALID_STAT(smb_fname
->st
)) {
2226 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2233 if (VALID_STAT(smb_fname
->st
) && S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2234 /* sorry, but directories don't habe a resource fork */
2239 rc
= adouble_path(talloc_tos(), smb_fname
->base_name
, &adpath
);
2244 /* Create an smb_filename with stream_name == NULL. */
2245 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
2246 adpath
, NULL
, NULL
);
2247 if (smb_fname_base
== NULL
) {
2253 /* Sanitize flags */
2254 if (flags
& O_WRONLY
) {
2255 /* We always need read access for the metadata header too */
2260 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
2267 /* REVIEW: we need this in ad_write() */
2268 fsp
->fh
->fd
= hostfd
;
2270 if (flags
& (O_CREAT
| O_TRUNC
)) {
2271 ad
= ad_init(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2272 handle
, ADOUBLE_RSRC
, fsp
);
2277 rc
= ad_write(ad
, smb_fname
->base_name
);
2283 ad
= ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2284 handle
, ADOUBLE_RSRC
, fsp
);
2289 if (ad_read(ad
, smb_fname
->base_name
) == -1) {
2297 TALLOC_FREE(adpath
);
2298 TALLOC_FREE(smb_fname_base
);
2300 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc
, hostfd
));
2302 int saved_errno
= errno
;
2305 * BUGBUGBUG -- we would need to call
2306 * fd_close_posix here, but we don't have a
2309 fsp
->fh
->fd
= hostfd
;
2313 errno
= saved_errno
;
2318 static int fruit_open(vfs_handle_struct
*handle
,
2319 struct smb_filename
*smb_fname
,
2320 files_struct
*fsp
, int flags
, mode_t mode
)
2322 DEBUG(10, ("fruit_open called for %s\n",
2323 smb_fname_str_dbg(smb_fname
)));
2325 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
2326 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2329 if (is_afpinfo_stream(smb_fname
)) {
2330 return fruit_open_meta(handle
, smb_fname
, fsp
, flags
, mode
);
2331 } else if (is_afpresource_stream(smb_fname
)) {
2332 return fruit_open_rsrc(handle
, smb_fname
, fsp
, flags
, mode
);
2335 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2338 static int fruit_rename(struct vfs_handle_struct
*handle
,
2339 const struct smb_filename
*smb_fname_src
,
2340 const struct smb_filename
*smb_fname_dst
)
2343 char *src_adouble_path
= NULL
;
2344 char *dst_adouble_path
= NULL
;
2345 struct fruit_config_data
*config
= NULL
;
2347 rc
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
2349 if (!VALID_STAT(smb_fname_src
->st
)
2350 || !S_ISREG(smb_fname_src
->st
.st_ex_mode
)) {
2354 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2355 struct fruit_config_data
, return -1);
2357 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
2361 rc
= adouble_path(talloc_tos(), smb_fname_src
->base_name
,
2366 rc
= adouble_path(talloc_tos(), smb_fname_dst
->base_name
,
2372 DEBUG(10, ("fruit_rename: %s -> %s\n",
2373 src_adouble_path
, dst_adouble_path
));
2375 rc
= rename(src_adouble_path
, dst_adouble_path
);
2376 if (errno
== ENOENT
) {
2380 TALLOC_FREE(src_adouble_path
);
2381 TALLOC_FREE(dst_adouble_path
);
2387 static int fruit_unlink(vfs_handle_struct
*handle
,
2388 const struct smb_filename
*smb_fname
)
2391 struct fruit_config_data
*config
= NULL
;
2394 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
2395 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
2398 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2399 struct fruit_config_data
, return -1);
2401 if (is_afpinfo_stream(smb_fname
)) {
2402 if (config
->meta
== FRUIT_META_STREAM
) {
2403 rc
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
2405 rc
= SMB_VFS_REMOVEXATTR(handle
->conn
,
2406 smb_fname
->base_name
,
2407 AFPINFO_EA_NETATALK
);
2409 } else if (is_afpresource_stream(smb_fname
)) {
2410 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
2411 rc
= adouble_path(talloc_tos(),
2412 smb_fname
->base_name
, &adp
);
2416 /* FIXME: direct unlink(), missing smb_fname */
2418 if ((rc
== -1) && (errno
== ENOENT
)) {
2422 rc
= SMB_VFS_REMOVEXATTR(handle
->conn
,
2423 smb_fname
->base_name
,
2424 AFPRESOURCE_EA_NETATALK
);
2427 rc
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
2434 static int fruit_chmod(vfs_handle_struct
*handle
,
2440 struct fruit_config_data
*config
= NULL
;
2443 rc
= SMB_VFS_NEXT_CHMOD(handle
, path
, mode
);
2448 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2449 struct fruit_config_data
, return -1);
2451 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
2455 /* FIXME: direct sys_lstat(), missing smb_fname */
2456 rc
= sys_lstat(path
, &sb
, false);
2457 if (rc
!= 0 || !S_ISREG(sb
.st_ex_mode
)) {
2461 rc
= adouble_path(talloc_tos(), path
, &adp
);
2466 DEBUG(10, ("fruit_chmod: %s\n", adp
));
2468 rc
= SMB_VFS_NEXT_CHMOD(handle
, adp
, mode
);
2469 if (errno
== ENOENT
) {
2477 static int fruit_chown(vfs_handle_struct
*handle
,
2484 struct fruit_config_data
*config
= NULL
;
2487 rc
= SMB_VFS_NEXT_CHOWN(handle
, path
, uid
, gid
);
2492 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2493 struct fruit_config_data
, return -1);
2495 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
2499 /* FIXME: direct sys_lstat(), missing smb_fname */
2500 rc
= sys_lstat(path
, &sb
, false);
2501 if (rc
!= 0 || !S_ISREG(sb
.st_ex_mode
)) {
2505 rc
= adouble_path(talloc_tos(), path
, &adp
);
2510 DEBUG(10, ("fruit_chown: %s\n", adp
));
2512 rc
= SMB_VFS_NEXT_CHOWN(handle
, adp
, uid
, gid
);
2513 if (errno
== ENOENT
) {
2522 static int fruit_rmdir(struct vfs_handle_struct
*handle
, const char *path
)
2526 struct fruit_config_data
*config
;
2528 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2529 struct fruit_config_data
, return -1);
2531 if (!handle
->conn
->cwd
|| !path
|| (config
->rsrc
== FRUIT_RSRC_XATTR
)) {
2536 * Due to there is no way to change bDeleteVetoFiles variable
2537 * from this module, need to clean up ourselves
2544 while ((de
= readdir(dh
)) != NULL
) {
2545 if ((strncmp(de
->d_name
,
2546 ADOUBLE_NAME_PREFIX
,
2547 strlen(ADOUBLE_NAME_PREFIX
))) == 0) {
2548 char *p
= talloc_asprintf(talloc_tos(),
2554 DEBUG(10, ("fruit_rmdir: delete %s\n", p
));
2564 return SMB_VFS_NEXT_RMDIR(handle
, path
);
2567 static ssize_t
fruit_pread(vfs_handle_struct
*handle
,
2568 files_struct
*fsp
, void *data
,
2569 size_t n
, off_t offset
)
2572 struct adouble
*ad
= (struct adouble
*)VFS_FETCH_FSP_EXTENSION(
2574 struct fruit_config_data
*config
= NULL
;
2578 char *tmp_base_name
= NULL
;
2581 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset
, (int)n
));
2583 if (!fsp
->base_fsp
) {
2584 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2587 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2588 struct fruit_config_data
, return -1);
2590 /* fsp_name is not converted with vfs_catia */
2591 tmp_base_name
= fsp
->base_fsp
->fsp_name
->base_name
;
2592 status
= SMB_VFS_TRANSLATE_NAME(handle
->conn
,
2593 fsp
->base_fsp
->fsp_name
->base_name
,
2594 vfs_translate_to_unix
,
2595 talloc_tos(), &name
);
2596 if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
2597 name
= talloc_strdup(talloc_tos(), tmp_base_name
);
2602 } else if (!NT_STATUS_IS_OK(status
)) {
2603 errno
= map_errno_from_nt_status(status
);
2607 fsp
->base_fsp
->fsp_name
->base_name
= name
;
2610 len
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2618 if (!fruit_fsp_recheck(ad
, fsp
)) {
2623 if (ad
->ad_type
== ADOUBLE_META
) {
2624 ai
= afpinfo_new(talloc_tos());
2630 len
= ad_read(ad
, fsp
->base_fsp
->fsp_name
->base_name
);
2636 memcpy(&ai
->afpi_FinderInfo
[0],
2637 ad_entry(ad
, ADEID_FINDERI
),
2639 len
= afpinfo_pack(ai
, data
);
2640 if (len
!= AFP_INFO_SIZE
) {
2645 len
= SMB_VFS_NEXT_PREAD(
2646 handle
, fsp
, data
, n
,
2647 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2654 fsp
->base_fsp
->fsp_name
->base_name
= tmp_base_name
;
2660 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc
, len
));
2664 static ssize_t
fruit_pwrite(vfs_handle_struct
*handle
,
2665 files_struct
*fsp
, const void *data
,
2666 size_t n
, off_t offset
)
2669 struct adouble
*ad
= (struct adouble
*)VFS_FETCH_FSP_EXTENSION(
2671 struct fruit_config_data
*config
= NULL
;
2675 char *tmp_base_name
= NULL
;
2678 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset
, (int)n
));
2680 if (!fsp
->base_fsp
) {
2681 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2684 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2685 struct fruit_config_data
, return -1);
2687 tmp_base_name
= fsp
->base_fsp
->fsp_name
->base_name
;
2688 status
= SMB_VFS_TRANSLATE_NAME(handle
->conn
,
2689 fsp
->base_fsp
->fsp_name
->base_name
,
2690 vfs_translate_to_unix
,
2691 talloc_tos(), &name
);
2692 if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
2693 name
= talloc_strdup(talloc_tos(), tmp_base_name
);
2698 } else if (!NT_STATUS_IS_OK(status
)) {
2699 errno
= map_errno_from_nt_status(status
);
2703 fsp
->base_fsp
->fsp_name
->base_name
= name
;
2706 len
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2714 if (!fruit_fsp_recheck(ad
, fsp
)) {
2719 if (ad
->ad_type
== ADOUBLE_META
) {
2720 if (n
!= AFP_INFO_SIZE
|| offset
!= 0) {
2721 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2722 (intmax_t)offset
, (intmax_t)n
));
2726 ai
= afpinfo_unpack(talloc_tos(), data
);
2731 memcpy(ad_entry(ad
, ADEID_FINDERI
),
2732 &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
2733 rc
= ad_write(ad
, name
);
2735 len
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
,
2736 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2742 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
2743 rc
= ad_read(ad
, name
);
2749 if ((len
+ offset
) > ad_getentrylen(ad
, ADEID_RFORK
)) {
2750 ad_setentrylen(ad
, ADEID_RFORK
, len
+ offset
);
2751 rc
= ad_write(ad
, name
);
2757 fsp
->base_fsp
->fsp_name
->base_name
= tmp_base_name
;
2767 * Helper to stat/lstat the base file of an smb_fname.
2769 static int fruit_stat_base(vfs_handle_struct
*handle
,
2770 struct smb_filename
*smb_fname
,
2773 char *tmp_stream_name
;
2776 tmp_stream_name
= smb_fname
->stream_name
;
2777 smb_fname
->stream_name
= NULL
;
2779 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2781 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2783 smb_fname
->stream_name
= tmp_stream_name
;
2787 static int fruit_stat_meta(vfs_handle_struct
*handle
,
2788 struct smb_filename
*smb_fname
,
2791 /* Populate the stat struct with info from the base file. */
2792 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
2795 smb_fname
->st
.st_ex_size
= AFP_INFO_SIZE
;
2796 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
2797 smb_fname
->stream_name
);
2801 static int fruit_stat_rsrc(vfs_handle_struct
*handle
,
2802 struct smb_filename
*smb_fname
,
2806 struct adouble
*ad
= NULL
;
2808 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2809 smb_fname_str_dbg(smb_fname
)));
2811 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
, ADOUBLE_RSRC
);
2817 /* Populate the stat struct with info from the base file. */
2818 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
2823 smb_fname
->st
.st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
2824 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
2825 smb_fname
->stream_name
);
2830 static int fruit_stat(vfs_handle_struct
*handle
,
2831 struct smb_filename
*smb_fname
)
2835 DEBUG(10, ("fruit_stat called for %s\n",
2836 smb_fname_str_dbg(smb_fname
)));
2838 if (!is_ntfs_stream_smb_fname(smb_fname
)
2839 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
2840 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2842 update_btime(handle
, smb_fname
);
2848 * Note if lp_posix_paths() is true, we can never
2849 * get here as is_ntfs_stream_smb_fname() is
2850 * always false. So we never need worry about
2851 * not following links here.
2854 if (is_afpinfo_stream(smb_fname
)) {
2855 rc
= fruit_stat_meta(handle
, smb_fname
, true);
2856 } else if (is_afpresource_stream(smb_fname
)) {
2857 rc
= fruit_stat_rsrc(handle
, smb_fname
, true);
2859 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2863 update_btime(handle
, smb_fname
);
2864 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
2865 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
2866 smb_fname
->st
.st_ex_blocks
=
2867 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
2872 static int fruit_lstat(vfs_handle_struct
*handle
,
2873 struct smb_filename
*smb_fname
)
2877 DEBUG(10, ("fruit_lstat called for %s\n",
2878 smb_fname_str_dbg(smb_fname
)));
2880 if (!is_ntfs_stream_smb_fname(smb_fname
)
2881 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
2882 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2884 update_btime(handle
, smb_fname
);
2889 if (is_afpinfo_stream(smb_fname
)) {
2890 rc
= fruit_stat_meta(handle
, smb_fname
, false);
2891 } else if (is_afpresource_stream(smb_fname
)) {
2892 rc
= fruit_stat_rsrc(handle
, smb_fname
, false);
2894 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2898 update_btime(handle
, smb_fname
);
2899 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
2900 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
2901 smb_fname
->st
.st_ex_blocks
=
2902 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
2907 static int fruit_fstat_meta(vfs_handle_struct
*handle
,
2909 SMB_STRUCT_STAT
*sbuf
)
2911 DEBUG(10, ("fruit_fstat_meta called for %s\n",
2912 smb_fname_str_dbg(fsp
->base_fsp
->fsp_name
)));
2914 /* Populate the stat struct with info from the base file. */
2915 if (fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false) == -1) {
2918 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
2919 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
2920 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
2925 static int fruit_fstat_rsrc(vfs_handle_struct
*handle
, files_struct
*fsp
,
2926 SMB_STRUCT_STAT
*sbuf
)
2928 struct fruit_config_data
*config
;
2929 struct adouble
*ad
= (struct adouble
*)VFS_FETCH_FSP_EXTENSION(
2932 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
2933 smb_fname_str_dbg(fsp
->base_fsp
->fsp_name
)));
2935 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2936 struct fruit_config_data
, return -1);
2938 if (config
->rsrc
== FRUIT_RSRC_STREAM
) {
2939 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
2942 /* Populate the stat struct with info from the base file. */
2943 if (fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false) == -1) {
2946 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
2947 sbuf
->st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
2948 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
2950 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
2951 smb_fname_str_dbg(fsp
->fsp_name
),
2952 (ssize_t
)sbuf
->st_ex_size
));
2957 static int fruit_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
2958 SMB_STRUCT_STAT
*sbuf
)
2962 char *tmp_base_name
= NULL
;
2964 struct adouble
*ad
= (struct adouble
*)
2965 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2967 DEBUG(10, ("fruit_fstat called for %s\n",
2968 smb_fname_str_dbg(fsp
->fsp_name
)));
2970 if (fsp
->base_fsp
) {
2971 tmp_base_name
= fsp
->base_fsp
->fsp_name
->base_name
;
2972 /* fsp_name is not converted with vfs_catia */
2973 status
= SMB_VFS_TRANSLATE_NAME(
2975 fsp
->base_fsp
->fsp_name
->base_name
,
2976 vfs_translate_to_unix
,
2977 talloc_tos(), &name
);
2979 if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
2980 name
= talloc_strdup(talloc_tos(), tmp_base_name
);
2985 } else if (!NT_STATUS_IS_OK(status
)) {
2986 errno
= map_errno_from_nt_status(status
);
2990 fsp
->base_fsp
->fsp_name
->base_name
= name
;
2993 if (ad
== NULL
|| fsp
->base_fsp
== NULL
) {
2994 rc
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
2998 if (!fruit_fsp_recheck(ad
, fsp
)) {
3003 switch (ad
->ad_type
) {
3005 rc
= fruit_fstat_meta(handle
, fsp
, sbuf
);
3008 rc
= fruit_fstat_rsrc(handle
, fsp
, sbuf
);
3011 DEBUG(10, ("fruit_fstat %s: bad type\n",
3012 smb_fname_str_dbg(fsp
->fsp_name
)));
3018 sbuf
->st_ex_mode
&= ~S_IFMT
;
3019 sbuf
->st_ex_mode
|= S_IFREG
;
3020 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3024 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3025 smb_fname_str_dbg(fsp
->fsp_name
),
3026 (ssize_t
)sbuf
->st_ex_size
));
3027 if (tmp_base_name
) {
3028 fsp
->base_fsp
->fsp_name
->base_name
= tmp_base_name
;
3034 static NTSTATUS
fruit_streaminfo(vfs_handle_struct
*handle
,
3035 struct files_struct
*fsp
,
3037 TALLOC_CTX
*mem_ctx
,
3038 unsigned int *pnum_streams
,
3039 struct stream_struct
**pstreams
)
3041 struct fruit_config_data
*config
= NULL
;
3042 struct smb_filename
*smb_fname
= NULL
;
3043 struct adouble
*ad
= NULL
;
3045 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3046 return NT_STATUS_UNSUCCESSFUL
);
3047 DEBUG(10, ("fruit_streaminfo called for %s\n", fname
));
3049 smb_fname
= synthetic_smb_fname(talloc_tos(), fname
, NULL
, NULL
);
3050 if (smb_fname
== NULL
) {
3051 return NT_STATUS_NO_MEMORY
;
3054 if (config
->meta
== FRUIT_META_NETATALK
) {
3055 ad
= ad_get(talloc_tos(), handle
,
3056 smb_fname
->base_name
, ADOUBLE_META
);
3057 if (ad
&& !empty_finderinfo(ad
)) {
3058 if (!add_fruit_stream(
3059 mem_ctx
, pnum_streams
, pstreams
,
3060 AFPINFO_STREAM_NAME
, AFP_INFO_SIZE
,
3061 smb_roundup(handle
->conn
,
3064 TALLOC_FREE(smb_fname
);
3065 return NT_STATUS_NO_MEMORY
;
3071 if (config
->rsrc
!= FRUIT_RSRC_STREAM
) {
3072 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
,
3075 if (!add_fruit_stream(
3076 mem_ctx
, pnum_streams
, pstreams
,
3077 AFPRESOURCE_STREAM_NAME
,
3078 ad_getentrylen(ad
, ADEID_RFORK
),
3079 smb_roundup(handle
->conn
,
3081 ad
, ADEID_RFORK
)))) {
3083 TALLOC_FREE(smb_fname
);
3084 return NT_STATUS_NO_MEMORY
;
3090 TALLOC_FREE(smb_fname
);
3092 return SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, fname
, mem_ctx
,
3093 pnum_streams
, pstreams
);
3096 static int fruit_ntimes(vfs_handle_struct
*handle
,
3097 const struct smb_filename
*smb_fname
,
3098 struct smb_file_time
*ft
)
3101 struct adouble
*ad
= NULL
;
3103 if (null_timespec(ft
->create_time
)) {
3107 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname
),
3108 time_to_asc(convert_timespec_to_time_t(ft
->create_time
))));
3110 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
, ADOUBLE_META
);
3115 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
,
3116 convert_time_t_to_uint32_t(ft
->create_time
.tv_sec
));
3118 rc
= ad_write(ad
, smb_fname
->base_name
);
3124 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname
)));
3127 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
3130 static int fruit_fallocate(struct vfs_handle_struct
*handle
,
3131 struct files_struct
*fsp
,
3136 struct adouble
*ad
=
3137 (struct adouble
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3140 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
3143 if (!fruit_fsp_recheck(ad
, fsp
)) {
3147 /* Let the pwrite code path handle it. */
3152 static int fruit_ftruncate(struct vfs_handle_struct
*handle
,
3153 struct files_struct
*fsp
,
3157 struct adouble
*ad
=
3158 (struct adouble
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3159 struct fruit_config_data
*config
;
3161 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3162 fsp_str_dbg(fsp
), (double)offset
));
3165 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
3168 if (!fruit_fsp_recheck(ad
, fsp
)) {
3172 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3173 struct fruit_config_data
, return -1);
3175 switch (ad
->ad_type
) {
3178 * As this request hasn't been seen in the wild,
3179 * the only sensible use I can imagine is the client
3180 * truncating the stream to 0 bytes size.
3181 * We simply remove the metadata on such a request.
3184 rc
= SMB_VFS_FREMOVEXATTR(fsp
,
3185 AFPRESOURCE_EA_NETATALK
);
3189 if (config
->rsrc
== FRUIT_RSRC_XATTR
&& offset
== 0) {
3190 rc
= SMB_VFS_FREMOVEXATTR(fsp
,
3191 AFPRESOURCE_EA_NETATALK
);
3193 rc
= SMB_VFS_NEXT_FTRUNCATE(
3195 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
3199 ad_setentrylen(ad
, ADEID_RFORK
, offset
);
3200 rc
= ad_write(ad
, NULL
);
3213 static NTSTATUS
fruit_create_file(vfs_handle_struct
*handle
,
3214 struct smb_request
*req
,
3215 uint16_t root_dir_fid
,
3216 struct smb_filename
*smb_fname
,
3217 uint32_t access_mask
,
3218 uint32_t share_access
,
3219 uint32_t create_disposition
,
3220 uint32_t create_options
,
3221 uint32_t file_attributes
,
3222 uint32_t oplock_request
,
3223 struct smb2_lease
*lease
,
3224 uint64_t allocation_size
,
3225 uint32_t private_flags
,
3226 struct security_descriptor
*sd
,
3227 struct ea_list
*ea_list
,
3228 files_struct
**result
,
3230 const struct smb2_create_blobs
*in_context_blobs
,
3231 struct smb2_create_blobs
*out_context_blobs
)
3234 struct fruit_config_data
*config
= NULL
;
3236 status
= check_aapl(handle
, req
, in_context_blobs
, out_context_blobs
);
3237 if (!NT_STATUS_IS_OK(status
)) {
3241 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3242 return NT_STATUS_UNSUCCESSFUL
);
3244 status
= SMB_VFS_NEXT_CREATE_FILE(
3245 handle
, req
, root_dir_fid
, smb_fname
,
3246 access_mask
, share_access
,
3247 create_disposition
, create_options
,
3248 file_attributes
, oplock_request
,
3250 allocation_size
, private_flags
,
3251 sd
, ea_list
, result
,
3252 pinfo
, in_context_blobs
, out_context_blobs
);
3253 if (!NT_STATUS_IS_OK(status
)) {
3257 if (config
->copyfile_enabled
) {
3259 * Set a flag in the fsp. Gets used in copychunk to
3260 * check whether the special Apple copyfile semantics
3261 * for copychunk should be allowed in a copychunk
3262 * request with a count of 0.
3264 (*result
)->aapl_copyfile_supported
= true;
3266 if (is_ntfs_stream_smb_fname(smb_fname
)
3267 || (*result
== NULL
)
3268 || ((*result
)->is_directory
)) {
3272 if (config
->locking
== FRUIT_LOCKING_NETATALK
) {
3273 status
= fruit_check_access(
3276 map_share_mode_to_deny_mode(share_access
, 0));
3277 if (!NT_STATUS_IS_OK(status
)) {
3285 DEBUG(1, ("fruit_create_file: %s\n", nt_errstr(status
)));
3288 close_file(req
, *result
, ERROR_CLOSE
);
3295 static NTSTATUS
fruit_readdir_attr(struct vfs_handle_struct
*handle
,
3296 const struct smb_filename
*fname
,
3297 TALLOC_CTX
*mem_ctx
,
3298 struct readdir_attr_data
**pattr_data
)
3300 struct fruit_config_data
*config
= NULL
;
3301 struct readdir_attr_data
*attr_data
;
3304 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3305 struct fruit_config_data
,
3306 return NT_STATUS_UNSUCCESSFUL
);
3308 if (!config
->use_aapl
) {
3309 return SMB_VFS_NEXT_READDIR_ATTR(handle
, fname
, mem_ctx
, pattr_data
);
3312 DEBUG(10, ("fruit_readdir_attr %s\n", fname
->base_name
));
3314 *pattr_data
= talloc_zero(mem_ctx
, struct readdir_attr_data
);
3315 if (*pattr_data
== NULL
) {
3316 return NT_STATUS_UNSUCCESSFUL
;
3318 attr_data
= *pattr_data
;
3319 attr_data
->type
= RDATTR_AAPL
;
3322 * Mac metadata: compressed FinderInfo, resource fork length
3325 status
= readdir_attr_macmeta(handle
, fname
, attr_data
);
3326 if (!NT_STATUS_IS_OK(status
)) {
3328 * Error handling is tricky: if we return failure from
3329 * this function, the corresponding directory entry
3330 * will to be passed to the client, so we really just
3331 * want to error out on fatal errors.
3333 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
3341 if (config
->unix_info_enabled
) {
3342 attr_data
->attr_data
.aapl
.unix_mode
= fname
->st
.st_ex_mode
;
3348 if (!config
->readdir_attr_max_access
) {
3349 attr_data
->attr_data
.aapl
.max_access
= FILE_GENERIC_ALL
;
3351 status
= smbd_calculate_access_mask(
3355 SEC_FLAG_MAXIMUM_ALLOWED
,
3356 &attr_data
->attr_data
.aapl
.max_access
);
3357 if (!NT_STATUS_IS_OK(status
)) {
3362 return NT_STATUS_OK
;
3365 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3366 fname
->base_name
, nt_errstr(status
)));
3367 TALLOC_FREE(*pattr_data
);
3371 static NTSTATUS
fruit_fget_nt_acl(vfs_handle_struct
*handle
,
3373 uint32_t security_info
,
3374 TALLOC_CTX
*mem_ctx
,
3375 struct security_descriptor
**ppdesc
)
3378 struct security_ace ace
;
3380 struct fruit_config_data
*config
;
3382 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3383 struct fruit_config_data
,
3384 return NT_STATUS_UNSUCCESSFUL
);
3386 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
3388 if (!NT_STATUS_IS_OK(status
)) {
3393 * Add MS NFS style ACEs with uid, gid and mode
3395 if (!config
->unix_info_enabled
) {
3396 return NT_STATUS_OK
;
3399 /* MS NFS style mode */
3400 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
, fsp
->fsp_name
->st
.st_ex_mode
);
3401 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
3402 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
3403 if (!NT_STATUS_IS_OK(status
)) {
3404 DEBUG(1,("failed to add MS NFS style ACE\n"));
3408 /* MS NFS style uid */
3409 sid_compose(&sid
, &global_sid_Unix_NFS_Users
, fsp
->fsp_name
->st
.st_ex_uid
);
3410 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
3411 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
3412 if (!NT_STATUS_IS_OK(status
)) {
3413 DEBUG(1,("failed to add MS NFS style ACE\n"));
3417 /* MS NFS style gid */
3418 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
, fsp
->fsp_name
->st
.st_ex_gid
);
3419 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
3420 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
3421 if (!NT_STATUS_IS_OK(status
)) {
3422 DEBUG(1,("failed to add MS NFS style ACE\n"));
3426 return NT_STATUS_OK
;
3429 static NTSTATUS
fruit_fset_nt_acl(vfs_handle_struct
*handle
,
3431 uint32_t security_info_sent
,
3432 const struct security_descriptor
*psd
)
3439 DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp
)));
3441 status
= check_ms_nfs(handle
, fsp
, psd
, &ms_nfs_mode
, &do_chmod
);
3442 if (!NT_STATUS_IS_OK(status
)) {
3443 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp
)));
3447 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
3448 if (!NT_STATUS_IS_OK(status
)) {
3449 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp
)));
3454 if (fsp
->fh
->fd
!= -1) {
3455 DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp
)));
3456 result
= SMB_VFS_FCHMOD(fsp
, ms_nfs_mode
);
3458 DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp
)));
3459 result
= SMB_VFS_CHMOD(fsp
->conn
,
3460 fsp
->fsp_name
->base_name
,
3465 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp
),
3466 result
, (unsigned)ms_nfs_mode
,
3468 status
= map_nt_error_from_unix(errno
);
3473 return NT_STATUS_OK
;
3476 struct fruit_copy_chunk_state
{
3477 struct vfs_handle_struct
*handle
;
3479 struct files_struct
*src_fsp
;
3480 struct files_struct
*dst_fsp
;
3484 static void fruit_copy_chunk_done(struct tevent_req
*subreq
);
3485 static struct tevent_req
*fruit_copy_chunk_send(struct vfs_handle_struct
*handle
,
3486 TALLOC_CTX
*mem_ctx
,
3487 struct tevent_context
*ev
,
3488 struct files_struct
*src_fsp
,
3490 struct files_struct
*dest_fsp
,
3494 struct tevent_req
*req
, *subreq
;
3495 struct fruit_copy_chunk_state
*fruit_copy_chunk_state
;
3497 struct fruit_config_data
*config
;
3498 off_t to_copy
= num
;
3500 DEBUG(10,("soff: %zd, doff: %zd, len: %zd\n",
3501 src_off
, dest_off
, num
));
3503 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3504 struct fruit_config_data
,
3507 req
= tevent_req_create(mem_ctx
, &fruit_copy_chunk_state
,
3508 struct fruit_copy_chunk_state
);
3512 fruit_copy_chunk_state
->handle
= handle
;
3513 fruit_copy_chunk_state
->src_fsp
= src_fsp
;
3514 fruit_copy_chunk_state
->dst_fsp
= dest_fsp
;
3517 * Check if this a OS X copyfile style copychunk request with
3518 * a requested chunk count of 0 that was translated to a
3519 * copy_chunk_send VFS call overloading the parameters src_off
3520 * = dest_off = num = 0.
3522 if ((src_off
== 0) && (dest_off
== 0) && (num
== 0) &&
3523 src_fsp
->aapl_copyfile_supported
&&
3524 dest_fsp
->aapl_copyfile_supported
)
3526 status
= vfs_stat_fsp(src_fsp
);
3527 if (tevent_req_nterror(req
, status
)) {
3528 return tevent_req_post(req
, ev
);
3531 to_copy
= src_fsp
->fsp_name
->st
.st_ex_size
;
3532 fruit_copy_chunk_state
->is_copyfile
= true;
3535 subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
3543 if (tevent_req_nomem(subreq
, req
)) {
3544 return tevent_req_post(req
, ev
);
3547 tevent_req_set_callback(subreq
, fruit_copy_chunk_done
, req
);
3551 static void fruit_copy_chunk_done(struct tevent_req
*subreq
)
3553 struct tevent_req
*req
= tevent_req_callback_data(
3554 subreq
, struct tevent_req
);
3555 struct fruit_copy_chunk_state
*state
= tevent_req_data(
3556 req
, struct fruit_copy_chunk_state
);
3558 unsigned int num_streams
= 0;
3559 struct stream_struct
*streams
= NULL
;
3561 struct smb_filename
*src_fname_tmp
= NULL
;
3562 struct smb_filename
*dst_fname_tmp
= NULL
;
3564 status
= SMB_VFS_NEXT_COPY_CHUNK_RECV(state
->handle
,
3567 TALLOC_FREE(subreq
);
3568 if (tevent_req_nterror(req
, status
)) {
3572 if (!state
->is_copyfile
) {
3573 tevent_req_done(req
);
3578 * Now copy all reamining streams. We know the share supports
3579 * streams, because we're in vfs_fruit. We don't do this async
3580 * because streams are few and small.
3582 status
= vfs_streaminfo(state
->handle
->conn
, NULL
,
3583 state
->src_fsp
->fsp_name
->base_name
,
3584 req
, &num_streams
, &streams
);
3585 if (tevent_req_nterror(req
, status
)) {
3589 if (num_streams
== 1) {
3590 /* There is always one stream, ::$DATA. */
3591 tevent_req_done(req
);
3595 for (i
= 0; i
< num_streams
; i
++) {
3596 DEBUG(10, ("%s: stream: '%s'/%zd\n",
3597 __func__
, streams
[i
].name
, streams
[i
].size
));
3599 src_fname_tmp
= synthetic_smb_fname(
3601 state
->src_fsp
->fsp_name
->base_name
,
3604 if (tevent_req_nomem(src_fname_tmp
, req
)) {
3608 if (is_ntfs_default_stream_smb_fname(src_fname_tmp
)) {
3609 TALLOC_FREE(src_fname_tmp
);
3613 dst_fname_tmp
= synthetic_smb_fname(
3615 state
->dst_fsp
->fsp_name
->base_name
,
3618 if (tevent_req_nomem(dst_fname_tmp
, req
)) {
3619 TALLOC_FREE(src_fname_tmp
);
3623 status
= copy_file(req
,
3624 state
->handle
->conn
,
3627 OPENX_FILE_CREATE_IF_NOT_EXIST
,
3629 if (!NT_STATUS_IS_OK(status
)) {
3630 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__
,
3631 smb_fname_str_dbg(src_fname_tmp
),
3632 smb_fname_str_dbg(dst_fname_tmp
),
3633 nt_errstr(status
)));
3634 TALLOC_FREE(src_fname_tmp
);
3635 TALLOC_FREE(dst_fname_tmp
);
3636 tevent_req_nterror(req
, status
);
3640 TALLOC_FREE(src_fname_tmp
);
3641 TALLOC_FREE(dst_fname_tmp
);
3644 TALLOC_FREE(streams
);
3645 TALLOC_FREE(src_fname_tmp
);
3646 TALLOC_FREE(dst_fname_tmp
);
3647 tevent_req_done(req
);
3650 static NTSTATUS
fruit_copy_chunk_recv(struct vfs_handle_struct
*handle
,
3651 struct tevent_req
*req
,
3654 struct fruit_copy_chunk_state
*fruit_copy_chunk_state
= tevent_req_data(
3655 req
, struct fruit_copy_chunk_state
);
3658 if (tevent_req_is_nterror(req
, &status
)) {
3659 DEBUG(1, ("server side copy chunk failed: %s\n",
3660 nt_errstr(status
)));
3662 tevent_req_received(req
);
3666 *copied
= fruit_copy_chunk_state
->copied
;
3667 tevent_req_received(req
);
3669 return NT_STATUS_OK
;
3672 static struct vfs_fn_pointers vfs_fruit_fns
= {
3673 .connect_fn
= fruit_connect
,
3675 /* File operations */
3676 .chmod_fn
= fruit_chmod
,
3677 .chown_fn
= fruit_chown
,
3678 .unlink_fn
= fruit_unlink
,
3679 .rename_fn
= fruit_rename
,
3680 .rmdir_fn
= fruit_rmdir
,
3681 .open_fn
= fruit_open
,
3682 .pread_fn
= fruit_pread
,
3683 .pwrite_fn
= fruit_pwrite
,
3684 .stat_fn
= fruit_stat
,
3685 .lstat_fn
= fruit_lstat
,
3686 .fstat_fn
= fruit_fstat
,
3687 .streaminfo_fn
= fruit_streaminfo
,
3688 .ntimes_fn
= fruit_ntimes
,
3689 .ftruncate_fn
= fruit_ftruncate
,
3690 .fallocate_fn
= fruit_fallocate
,
3691 .create_file_fn
= fruit_create_file
,
3692 .readdir_attr_fn
= fruit_readdir_attr
,
3693 .copy_chunk_send_fn
= fruit_copy_chunk_send
,
3694 .copy_chunk_recv_fn
= fruit_copy_chunk_recv
,
3696 /* NT ACL operations */
3697 .fget_nt_acl_fn
= fruit_fget_nt_acl
,
3698 .fset_nt_acl_fn
= fruit_fset_nt_acl
,
3701 NTSTATUS
vfs_fruit_init(void);
3702 NTSTATUS
vfs_fruit_init(void)
3704 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fruit",
3706 if (!NT_STATUS_IS_OK(ret
)) {
3710 vfs_fruit_debug_level
= debug_add_class("fruit");
3711 if (vfs_fruit_debug_level
== -1) {
3712 vfs_fruit_debug_level
= DBGC_VFS
;
3713 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3716 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3717 "vfs_fruit_init","fruit",vfs_fruit_debug_level
));