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/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
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 #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"
112 #define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
113 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
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
;
130 bool readdir_attr_enabled
;
131 bool unix_info_enabled
;
132 bool copyfile_enabled
;
133 bool veto_appledouble
;
136 * Additional options, all enabled by default,
137 * possibly useful for analyzing performance. The associated
138 * operations with each of them may be expensive, so having
139 * the chance to disable them individually gives a chance
140 * tweaking the setup for the particular usecase.
142 bool readdir_attr_rsize
;
143 bool readdir_attr_finder_info
;
144 bool readdir_attr_max_access
;
147 static const struct enum_list fruit_rsrc
[] = {
148 {FRUIT_RSRC_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
149 {FRUIT_RSRC_ADFILE
, "file"}, /* ._ AppleDouble file */
150 {FRUIT_RSRC_XATTR
, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
154 static const struct enum_list fruit_meta
[] = {
155 {FRUIT_META_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
156 {FRUIT_META_NETATALK
, "netatalk"}, /* Netatalk compatible xattr */
160 static const struct enum_list fruit_locking
[] = {
161 {FRUIT_LOCKING_NETATALK
, "netatalk"}, /* synchronize locks with Netatalk */
162 {FRUIT_LOCKING_NONE
, "none"},
166 static const struct enum_list fruit_encoding
[] = {
167 {FRUIT_ENC_NATIVE
, "native"}, /* map unicode private chars to ASCII */
168 {FRUIT_ENC_PRIVATE
, "private"}, /* keep unicode private chars */
172 /*****************************************************************************
173 * Defines, functions and data structures that deal with AppleDouble
174 *****************************************************************************/
177 * There are two AppleDouble blobs we deal with:
179 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
180 * metadata in an xattr
182 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
185 typedef enum {ADOUBLE_META
, ADOUBLE_RSRC
} adouble_type_t
;
188 #define AD_VERSION2 0x00020000
189 #define AD_VERSION AD_VERSION2
192 * AppleDouble entry IDs.
194 #define ADEID_DFORK 1
195 #define ADEID_RFORK 2
197 #define ADEID_COMMENT 4
198 #define ADEID_ICONBW 5
199 #define ADEID_ICONCOL 6
200 #define ADEID_FILEI 7
201 #define ADEID_FILEDATESI 8
202 #define ADEID_FINDERI 9
203 #define ADEID_MACFILEI 10
204 #define ADEID_PRODOSFILEI 11
205 #define ADEID_MSDOSFILEI 12
206 #define ADEID_SHORTNAME 13
207 #define ADEID_AFPFILEI 14
210 /* Private Netatalk entries */
211 #define ADEID_PRIVDEV 16
212 #define ADEID_PRIVINO 17
213 #define ADEID_PRIVSYN 18
214 #define ADEID_PRIVID 19
215 #define ADEID_MAX (ADEID_PRIVID + 1)
218 * These are the real ids for the private entries,
219 * as stored in the adouble file
221 #define AD_DEV 0x80444556
222 #define AD_INO 0x80494E4F
223 #define AD_SYN 0x8053594E
224 #define AD_ID 0x8053567E
226 /* Number of actually used entries */
227 #define ADEID_NUM_XATTR 8
228 #define ADEID_NUM_DOT_UND 2
229 #define ADEID_NUM_RSRC_XATTR 1
231 /* AppleDouble magic */
232 #define AD_APPLESINGLE_MAGIC 0x00051600
233 #define AD_APPLEDOUBLE_MAGIC 0x00051607
234 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
236 /* Sizes of relevant entry bits */
237 #define ADEDLEN_MAGIC 4
238 #define ADEDLEN_VERSION 4
239 #define ADEDLEN_FILLER 16
240 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
241 #define ADEDLEN_NENTRIES 2
242 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
243 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
244 #define AD_ENTRY_LEN_EID 4
245 #define AD_ENTRY_LEN_OFF 4
246 #define AD_ENTRY_LEN_LEN 4
247 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
250 #define ADEDLEN_NAME 255
251 #define ADEDLEN_COMMENT 200
252 #define ADEDLEN_FILEI 16
253 #define ADEDLEN_FINDERI 32
254 #define ADEDLEN_FILEDATESI 16
255 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
256 #define ADEDLEN_AFPFILEI 4
257 #define ADEDLEN_MACFILEI 4
258 #define ADEDLEN_PRODOSFILEI 8
259 #define ADEDLEN_MSDOSFILEI 2
260 #define ADEDLEN_DID 4
261 #define ADEDLEN_PRIVDEV 8
262 #define ADEDLEN_PRIVINO 8
263 #define ADEDLEN_PRIVSYN 8
264 #define ADEDLEN_PRIVID 4
267 #define ADEDOFF_MAGIC 0
268 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
269 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
270 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
272 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
273 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
274 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
275 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
276 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
278 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
279 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
280 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
281 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
283 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
284 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
285 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
287 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
288 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
289 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
290 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
291 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
292 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
294 #if AD_DATASZ_XATTR != 402
295 #error bad size for AD_DATASZ_XATTR
298 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
299 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
301 #if AD_DATASZ_DOT_UND != 82
302 #error bad size for AD_DATASZ_DOT_UND
306 * Sharemode locks fcntl() offsets
308 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
309 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
311 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
313 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
315 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
316 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
317 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
318 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
319 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
320 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
321 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
322 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
323 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
324 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
326 /* Time stuff we overload the bits a little */
327 #define AD_DATE_CREATE 0
328 #define AD_DATE_MODIFY 4
329 #define AD_DATE_BACKUP 8
330 #define AD_DATE_ACCESS 12
331 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
332 AD_DATE_BACKUP | AD_DATE_ACCESS)
333 #define AD_DATE_UNIX (1 << 10)
334 #define AD_DATE_START 0x80000000
335 #define AD_DATE_DELTA 946684800
336 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
337 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
339 /* Accessor macros */
340 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
341 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
342 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
343 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
344 #define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
352 vfs_handle_struct
*ad_handle
;
353 files_struct
*ad_fsp
;
354 adouble_type_t ad_type
;
357 struct ad_entry ad_eid
[ADEID_MAX
];
361 struct ad_entry_order
{
362 uint32_t id
, offset
, len
;
365 /* Netatalk AppleDouble metadata xattr */
367 struct ad_entry_order entry_order_meta_xattr
[ADEID_NUM_XATTR
+ 1] = {
368 {ADEID_FINDERI
, ADEDOFF_FINDERI_XATTR
, ADEDLEN_FINDERI
},
369 {ADEID_COMMENT
, ADEDOFF_COMMENT_XATTR
, 0},
370 {ADEID_FILEDATESI
, ADEDOFF_FILEDATESI_XATTR
, ADEDLEN_FILEDATESI
},
371 {ADEID_AFPFILEI
, ADEDOFF_AFPFILEI_XATTR
, ADEDLEN_AFPFILEI
},
372 {ADEID_PRIVDEV
, ADEDOFF_PRIVDEV_XATTR
, 0},
373 {ADEID_PRIVINO
, ADEDOFF_PRIVINO_XATTR
, 0},
374 {ADEID_PRIVSYN
, ADEDOFF_PRIVSYN_XATTR
, 0},
375 {ADEID_PRIVID
, ADEDOFF_PRIVID_XATTR
, 0},
379 /* AppleDouble ressource fork file (the ones prefixed by "._") */
381 struct ad_entry_order entry_order_dot_und
[ADEID_NUM_DOT_UND
+ 1] = {
382 {ADEID_FINDERI
, ADEDOFF_FINDERI_DOT_UND
, ADEDLEN_FINDERI
},
383 {ADEID_RFORK
, ADEDOFF_RFORK_DOT_UND
, 0},
388 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
389 * isn't an AppleDouble file, it simply contains the ressource data,
390 * but in order to be able to use some API calls like ad_getentryoff()
391 * we build a fake/helper struct adouble with this entry order struct.
394 struct ad_entry_order entry_order_rsrc_xattr
[ADEID_NUM_RSRC_XATTR
+ 1] = {
399 /* Conversion from enumerated id to on-disk AppleDouble id */
400 #define AD_EID_DISK(a) (set_eid[a])
401 static const uint32_t set_eid
[] = {
402 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
403 AD_DEV
, AD_INO
, AD_SYN
, AD_ID
407 * Forward declarations
409 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
410 adouble_type_t type
, files_struct
*fsp
);
411 static int ad_write(struct adouble
*ad
, const char *path
);
412 static int adouble_path(TALLOC_CTX
*ctx
, const char *path_in
, char **path_out
);
417 static int ad_getdate(const struct adouble
*ad
,
418 unsigned int dateoff
,
421 bool xlate
= (dateoff
& AD_DATE_UNIX
);
423 dateoff
&= AD_DATE_MASK
;
424 if (!ad_getentryoff(ad
, ADEID_FILEDATESI
)) {
428 if (dateoff
> AD_DATE_ACCESS
) {
432 ad_entry(ad
, ADEID_FILEDATESI
) + dateoff
,
436 *date
= AD_DATE_TO_UNIX(*date
);
444 static int ad_setdate(struct adouble
*ad
, unsigned int dateoff
, uint32_t date
)
446 bool xlate
= (dateoff
& AD_DATE_UNIX
);
448 if (!ad_getentryoff(ad
, ADEID_FILEDATESI
)) {
452 dateoff
&= AD_DATE_MASK
;
454 date
= AD_DATE_FROM_UNIX(date
);
457 if (dateoff
> AD_DATE_ACCESS
) {
461 memcpy(ad_entry(ad
, ADEID_FILEDATESI
) + dateoff
, &date
, sizeof(date
));
468 * Map on-disk AppleDouble id to enumerated id
470 static uint32_t get_eid(uint32_t eid
)
478 return ADEID_PRIVDEV
;
480 return ADEID_PRIVINO
;
482 return ADEID_PRIVSYN
;
493 * Pack AppleDouble structure into data buffer
495 static bool ad_pack(struct adouble
*ad
)
502 bufsize
= talloc_get_size(ad
->ad_data
);
504 if (offset
+ ADEDLEN_MAGIC
< offset
||
505 offset
+ ADEDLEN_MAGIC
>= bufsize
) {
508 RSIVAL(ad
->ad_data
, offset
, ad
->ad_magic
);
509 offset
+= ADEDLEN_MAGIC
;
511 if (offset
+ ADEDLEN_VERSION
< offset
||
512 offset
+ ADEDLEN_VERSION
>= bufsize
) {
515 RSIVAL(ad
->ad_data
, offset
, ad
->ad_version
);
516 offset
+= ADEDLEN_VERSION
;
518 if (offset
+ ADEDLEN_FILLER
< offset
||
519 offset
+ ADEDLEN_FILLER
>= bufsize
) {
522 if (ad
->ad_type
== ADOUBLE_RSRC
) {
523 memcpy(ad
->ad_data
+ offset
, AD_FILLER_TAG
, ADEDLEN_FILLER
);
525 offset
+= ADEDLEN_FILLER
;
527 if (offset
+ ADEDLEN_NENTRIES
< offset
||
528 offset
+ ADEDLEN_NENTRIES
>= bufsize
) {
531 offset
+= ADEDLEN_NENTRIES
;
533 for (eid
= 0, nent
= 0; eid
< ADEID_MAX
; eid
++) {
534 if (ad
->ad_eid
[eid
].ade_off
== 0) {
536 * ade_off is also used as indicator whether a
537 * specific entry is used or not
542 if (offset
+ AD_ENTRY_LEN_EID
< offset
||
543 offset
+ AD_ENTRY_LEN_EID
>= bufsize
) {
546 RSIVAL(ad
->ad_data
, offset
, AD_EID_DISK(eid
));
547 offset
+= AD_ENTRY_LEN_EID
;
549 if (offset
+ AD_ENTRY_LEN_OFF
< offset
||
550 offset
+ AD_ENTRY_LEN_OFF
>= bufsize
) {
553 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_off
);
554 offset
+= AD_ENTRY_LEN_OFF
;
556 if (offset
+ AD_ENTRY_LEN_LEN
< offset
||
557 offset
+ AD_ENTRY_LEN_LEN
>= bufsize
) {
560 RSIVAL(ad
->ad_data
, offset
, ad
->ad_eid
[eid
].ade_len
);
561 offset
+= AD_ENTRY_LEN_LEN
;
566 if (ADEDOFF_NENTRIES
+ 2 >= bufsize
) {
569 RSSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
, nent
);
575 * Unpack an AppleDouble blob into a struct adoble
577 static bool ad_unpack(struct adouble
*ad
, const int nentries
, size_t filesize
)
579 size_t bufsize
= talloc_get_size(ad
->ad_data
);
581 uint32_t eid
, len
, off
;
584 * The size of the buffer ad->ad_data is checked when read, so
585 * we wouldn't have to check our own offsets, a few extra
586 * checks won't hurt though. We have to check the offsets we
587 * read from the buffer anyway.
590 if (bufsize
< (AD_HEADER_LEN
+ (AD_ENTRY_LEN
* nentries
))) {
591 DEBUG(1, ("bad size\n"));
595 ad
->ad_magic
= RIVAL(ad
->ad_data
, 0);
596 ad
->ad_version
= RIVAL(ad
->ad_data
, ADEDOFF_VERSION
);
597 if ((ad
->ad_magic
!= AD_MAGIC
) || (ad
->ad_version
!= AD_VERSION
)) {
598 DEBUG(1, ("wrong magic or version\n"));
602 adentries
= RSVAL(ad
->ad_data
, ADEDOFF_NENTRIES
);
603 if (adentries
!= nentries
) {
604 DEBUG(1, ("invalid number of entries: %d\n", adentries
));
608 /* now, read in the entry bits */
609 for (i
= 0; i
< adentries
; i
++) {
610 eid
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
));
612 off
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 4);
613 len
= RIVAL(ad
->ad_data
, AD_HEADER_LEN
+ (i
* AD_ENTRY_LEN
) + 8);
615 if (!eid
|| eid
> ADEID_MAX
) {
616 DEBUG(1, ("bogus eid %d\n", eid
));
621 * All entries other than the resource fork are
622 * expected to be read into the ad_data buffer, so
623 * ensure the specified offset is within that bound
625 if ((off
> bufsize
) && (eid
!= ADEID_RFORK
)) {
626 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
632 * All entries besides FinderInfo and resource fork
633 * must fit into the buffer. FinderInfo is special as
634 * it may be larger then the default 32 bytes (if it
635 * contains marshalled xattrs), but we will fixup that
636 * in ad_convert(). And the resource fork is never
637 * accessed directly by the ad_data buf (also see
638 * comment above) anyway.
640 if ((eid
!= ADEID_RFORK
) &&
641 (eid
!= ADEID_FINDERI
) &&
642 ((off
+ len
) > bufsize
)) {
643 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
649 * That would be obviously broken
651 if (off
> filesize
) {
652 DEBUG(1, ("bogus eid %d: off: %" PRIu32
", len: %" PRIu32
"\n",
658 * Check for any entry that has its end beyond the
661 if (off
+ len
< off
) {
662 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
663 ", len: %" PRIu32
"\n",
668 if (off
+ len
> filesize
) {
670 * If this is the resource fork entry, we fix
671 * up the length, for any other entry we bail
674 if (eid
!= ADEID_RFORK
) {
675 DEBUG(1, ("bogus eid %d: off: %" PRIu32
676 ", len: %" PRIu32
"\n",
682 * Fixup the resource fork entry by limiting
683 * the size to entryoffset - filesize.
685 len
= filesize
- off
;
686 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
687 ", len: %" PRIu32
"\n", off
, len
));
690 ad
->ad_eid
[eid
].ade_off
= off
;
691 ad
->ad_eid
[eid
].ade_len
= len
;
698 * Convert from Apple's ._ file to Netatalk
700 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
701 * bytes containing packed xattrs. Netatalk can't deal with that, so
702 * we simply discard the packed xattrs.
704 * @return -1 in case an error occured, 0 if no conversion was done, 1
707 static int ad_convert(struct adouble
*ad
, int fd
)
710 char *map
= MAP_FAILED
;
713 origlen
= ad_getentryoff(ad
, ADEID_RFORK
) +
714 ad_getentrylen(ad
, ADEID_RFORK
);
716 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
717 map
= mmap(NULL
, origlen
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
718 if (map
== MAP_FAILED
) {
719 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno
)));
724 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
725 memmove(map
+ ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
,
726 map
+ ad_getentryoff(ad
, ADEID_RFORK
),
727 ad_getentrylen(ad
, ADEID_RFORK
));
730 ad_setentrylen(ad
, ADEID_FINDERI
, ADEDLEN_FINDERI
);
731 ad_setentryoff(ad
, ADEID_RFORK
,
732 ad_getentryoff(ad
, ADEID_FINDERI
) + ADEDLEN_FINDERI
);
735 * FIXME: direct ftruncate(), but we don't have a fsp for the
738 rc
= ftruncate(fd
, ad_getentryoff(ad
, ADEID_RFORK
)
739 + ad_getentrylen(ad
, ADEID_RFORK
));
742 if (map
!= MAP_FAILED
) {
743 munmap(map
, origlen
);
749 * Read and parse Netatalk AppleDouble metadata xattr
751 static ssize_t
ad_header_read_meta(struct adouble
*ad
, const char *path
)
757 DEBUG(10, ("reading meta xattr for %s\n", path
));
759 ealen
= SMB_VFS_GETXATTR(ad
->ad_handle
->conn
, path
,
760 AFPINFO_EA_NETATALK
, ad
->ad_data
,
766 if (errno
== ENOATTR
) {
772 DEBUG(2, ("error reading meta xattr: %s\n",
778 if (ealen
!= AD_DATASZ_XATTR
) {
779 DEBUG(2, ("bad size %zd\n", ealen
));
785 /* Now parse entries */
786 ok
= ad_unpack(ad
, ADEID_NUM_XATTR
, AD_DATASZ_XATTR
);
788 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
794 if (!ad_getentryoff(ad
, ADEID_FINDERI
)
795 || !ad_getentryoff(ad
, ADEID_COMMENT
)
796 || !ad_getentryoff(ad
, ADEID_FILEDATESI
)
797 || !ad_getentryoff(ad
, ADEID_AFPFILEI
)
798 || !ad_getentryoff(ad
, ADEID_PRIVDEV
)
799 || !ad_getentryoff(ad
, ADEID_PRIVINO
)
800 || !ad_getentryoff(ad
, ADEID_PRIVSYN
)
801 || !ad_getentryoff(ad
, ADEID_PRIVID
)) {
802 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
809 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path
, rc
));
813 if (errno
== EINVAL
) {
815 removexattr(path
, AFPINFO_EA_NETATALK
);
824 * Read and parse resource fork, either ._ AppleDouble file or xattr
826 static ssize_t
ad_header_read_rsrc(struct adouble
*ad
, const char *path
)
828 struct fruit_config_data
*config
= NULL
;
835 struct adouble
*meta_ad
= NULL
;
836 SMB_STRUCT_STAT sbuf
;
840 SMB_VFS_HANDLE_GET_DATA(ad
->ad_handle
, config
,
841 struct fruit_config_data
, return -1);
843 /* Try rw first so we can use the fd in ad_convert() */
846 if (ad
->ad_fsp
&& ad
->ad_fsp
->fh
&& (ad
->ad_fsp
->fh
->fd
!= -1)) {
847 fd
= ad
->ad_fsp
->fh
->fd
;
849 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
850 adpath
= talloc_strdup(talloc_tos(), path
);
852 rc
= adouble_path(talloc_tos(), path
, &adpath
);
859 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
860 #ifndef HAVE_ATTROPEN
865 /* FIXME: direct Solaris xattr syscall */
866 fd
= attropen(adpath
, AFPRESOURCE_EA_NETATALK
,
870 /* FIXME: direct open(), don't have an fsp */
871 fd
= open(adpath
, mode
);
878 if (mode
== O_RDWR
) {
882 /* fall through ... */
884 DEBUG(2, ("open AppleDouble: %s, %s\n",
885 adpath
, strerror(errno
)));
893 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
894 /* FIXME: direct sys_fstat(), don't have an fsp */
897 lp_fake_directory_create_times(
898 SNUM(ad
->ad_handle
->conn
)));
902 len
= sbuf
.st_ex_size
;
903 ad_setentrylen(ad
, ADEID_RFORK
, len
);
905 /* FIXME: direct sys_pread(), don't have an fsp */
906 len
= sys_pread(fd
, ad
->ad_data
, AD_DATASZ_DOT_UND
, 0);
907 if (len
!= AD_DATASZ_DOT_UND
) {
908 DEBUG(2, ("%s: bad size: %zd\n",
909 strerror(errno
), len
));
914 /* FIXME: direct sys_fstat(), we don't have an fsp */
915 rc
= sys_fstat(fd
, &sbuf
,
916 lp_fake_directory_create_times(
917 SNUM(ad
->ad_handle
->conn
)));
922 /* Now parse entries */
923 ok
= ad_unpack(ad
, ADEID_NUM_DOT_UND
, sbuf
.st_ex_size
);
925 DEBUG(1, ("invalid AppleDouble ressource %s\n", path
));
931 if ((ad_getentryoff(ad
, ADEID_FINDERI
)
932 != ADEDOFF_FINDERI_DOT_UND
)
933 || (ad_getentrylen(ad
, ADEID_FINDERI
)
935 || (ad_getentryoff(ad
, ADEID_RFORK
)
936 < ADEDOFF_RFORK_DOT_UND
)) {
937 DEBUG(2, ("invalid AppleDouble ressource %s\n", path
));
944 && (ad_getentrylen(ad
, ADEID_FINDERI
) > ADEDLEN_FINDERI
)) {
945 rc
= ad_convert(ad
, fd
);
951 * Can't use ad_write() because we might not have a fsp
958 /* FIXME: direct sys_pwrite(), don't have an fsp */
959 len
= sys_pwrite(fd
, ad
->ad_data
,
960 AD_DATASZ_DOT_UND
, 0);
961 if (len
!= AD_DATASZ_DOT_UND
) {
962 DEBUG(2, ("%s: bad size: %zd\n", adpath
, len
));
967 meta_ad
= ad_init(talloc_tos(), ad
->ad_handle
,
969 if (meta_ad
== NULL
) {
974 memcpy(ad_entry(meta_ad
, ADEID_FINDERI
),
975 ad_entry(ad
, ADEID_FINDERI
),
978 rc
= ad_write(meta_ad
, path
);
986 DEBUG(10, ("opened AppleDouble: %s\n", path
));
993 if (opened
&& fd
!= -1) {
997 TALLOC_FREE(meta_ad
);
1005 * Read and unpack an AppleDouble metadata xattr or resource
1007 static ssize_t
ad_read(struct adouble
*ad
, const char *path
)
1009 switch (ad
->ad_type
) {
1011 return ad_header_read_meta(ad
, path
);
1013 return ad_header_read_rsrc(ad
, path
);
1020 * Allocate a struct adouble without initialiing it
1022 * The struct is either hang of the fsp extension context or if fsp is
1025 * @param[in] ctx talloc context
1026 * @param[in] handle vfs handle
1027 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1029 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1030 * added as an fsp extension
1032 * @return adouble handle
1034 static struct adouble
*ad_alloc(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1035 adouble_type_t type
, files_struct
*fsp
)
1040 struct fruit_config_data
*config
;
1042 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1043 struct fruit_config_data
, return NULL
);
1047 adsize
= AD_DATASZ_XATTR
;
1050 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1051 adsize
= AD_DATASZ_DOT_UND
;
1059 ad
= talloc_zero(ctx
, struct adouble
);
1065 ad
->ad_data
= talloc_zero_array(ad
, char, adsize
);
1068 ad
= (struct adouble
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
1076 ad
->ad_data
= talloc_zero_array(
1077 VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
1083 if (adsize
&& ad
->ad_data
== NULL
) {
1087 ad
->ad_handle
= handle
;
1089 ad
->ad_magic
= AD_MAGIC
;
1090 ad
->ad_version
= AD_VERSION
;
1100 * Allocate and initialize a new struct adouble
1102 * @param[in] ctx talloc context
1103 * @param[in] handle vfs handle
1104 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1105 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1107 * @return adouble handle, initialized
1109 static struct adouble
*ad_init(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1110 adouble_type_t type
, files_struct
*fsp
)
1113 const struct ad_entry_order
*eid
;
1114 struct adouble
*ad
= NULL
;
1115 struct fruit_config_data
*config
;
1116 time_t t
= time(NULL
);
1118 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1119 struct fruit_config_data
, return NULL
);
1123 eid
= entry_order_meta_xattr
;
1126 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
1127 eid
= entry_order_dot_und
;
1129 eid
= entry_order_rsrc_xattr
;
1136 ad
= ad_alloc(ctx
, handle
, type
, fsp
);
1142 ad
->ad_eid
[eid
->id
].ade_off
= eid
->offset
;
1143 ad
->ad_eid
[eid
->id
].ade_len
= eid
->len
;
1147 /* put something sane in the date fields */
1148 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
, t
);
1149 ad_setdate(ad
, AD_DATE_MODIFY
| AD_DATE_UNIX
, t
);
1150 ad_setdate(ad
, AD_DATE_ACCESS
| AD_DATE_UNIX
, t
);
1151 ad_setdate(ad
, AD_DATE_BACKUP
, htonl(AD_DATE_START
));
1160 * Return AppleDouble data for a file
1162 * @param[in] ctx talloc context
1163 * @param[in] handle vfs handle
1164 * @param[in] path pathname to file or directory
1165 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1167 * @return talloced struct adouble or NULL on error
1169 static struct adouble
*ad_get(TALLOC_CTX
*ctx
, vfs_handle_struct
*handle
,
1170 const char *path
, adouble_type_t type
)
1174 struct adouble
*ad
= NULL
;
1176 DEBUG(10, ("ad_get(%s) called for %s\n",
1177 type
== ADOUBLE_META
? "meta" : "rsrc", path
));
1179 ad
= ad_alloc(ctx
, handle
, type
, NULL
);
1185 len
= ad_read(ad
, path
);
1187 DEBUG(10, ("error reading AppleDouble for %s\n", path
));
1193 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1194 type
== ADOUBLE_META
? "meta" : "rsrc", path
, rc
));
1203 * Set AppleDouble metadata on a file or directory
1205 * @param[in] ad adouble handle
1207 * @param[in] path pathname to file or directory, may be NULL for a
1210 * @return status code, 0 means success
1212 static int ad_write(struct adouble
*ad
, const char *path
)
1223 switch (ad
->ad_type
) {
1225 rc
= SMB_VFS_SETXATTR(ad
->ad_handle
->conn
, path
,
1226 AFPINFO_EA_NETATALK
, ad
->ad_data
,
1227 AD_DATASZ_XATTR
, 0);
1230 if ((ad
->ad_fsp
== NULL
)
1231 || (ad
->ad_fsp
->fh
== NULL
)
1232 || (ad
->ad_fsp
->fh
->fd
== -1)) {
1236 /* FIXME: direct sys_pwrite(), don't have an fsp */
1237 len
= sys_pwrite(ad
->ad_fsp
->fh
->fd
, ad
->ad_data
,
1238 talloc_get_size(ad
->ad_data
), 0);
1239 if (len
!= talloc_get_size(ad
->ad_data
)) {
1240 DEBUG(1, ("short write on %s: %zd",
1241 fsp_str_dbg(ad
->ad_fsp
), len
));
1253 /*****************************************************************************
1255 *****************************************************************************/
1257 static bool is_afpinfo_stream(const struct smb_filename
*smb_fname
)
1259 if (strncasecmp_m(smb_fname
->stream_name
,
1260 AFPINFO_STREAM_NAME
,
1261 strlen(AFPINFO_STREAM_NAME
)) == 0) {
1267 static bool is_afpresource_stream(const struct smb_filename
*smb_fname
)
1269 if (strncasecmp_m(smb_fname
->stream_name
,
1270 AFPRESOURCE_STREAM_NAME
,
1271 strlen(AFPRESOURCE_STREAM_NAME
)) == 0) {
1278 * Test whether stream is an Apple stream, not used atm
1281 static bool is_apple_stream(const struct smb_filename
*smb_fname
)
1283 if (is_afpinfo_stream(smb_fname
)) {
1286 if (is_afpresource_stream(smb_fname
)) {
1294 * Initialize config struct from our smb.conf config parameters
1296 static int init_fruit_config(vfs_handle_struct
*handle
)
1298 struct fruit_config_data
*config
;
1301 config
= talloc_zero(handle
->conn
, struct fruit_config_data
);
1303 DEBUG(1, ("talloc_zero() failed\n"));
1308 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1309 "ressource", fruit_rsrc
, FRUIT_RSRC_ADFILE
);
1310 if (enumval
== -1) {
1311 DEBUG(1, ("value for %s: ressource type unknown\n",
1312 FRUIT_PARAM_TYPE_NAME
));
1315 config
->rsrc
= (enum fruit_rsrc
)enumval
;
1317 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1318 "metadata", fruit_meta
, FRUIT_META_NETATALK
);
1319 if (enumval
== -1) {
1320 DEBUG(1, ("value for %s: metadata type unknown\n",
1321 FRUIT_PARAM_TYPE_NAME
));
1324 config
->meta
= (enum fruit_meta
)enumval
;
1326 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1327 "locking", fruit_locking
, FRUIT_LOCKING_NONE
);
1328 if (enumval
== -1) {
1329 DEBUG(1, ("value for %s: locking type unknown\n",
1330 FRUIT_PARAM_TYPE_NAME
));
1333 config
->locking
= (enum fruit_locking
)enumval
;
1335 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1336 "encoding", fruit_encoding
, FRUIT_ENC_PRIVATE
);
1337 if (enumval
== -1) {
1338 DEBUG(1, ("value for %s: encoding type unknown\n",
1339 FRUIT_PARAM_TYPE_NAME
));
1342 config
->encoding
= (enum fruit_encoding
)enumval
;
1344 config
->veto_appledouble
= lp_parm_bool(
1345 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
1346 "veto_appledouble", true);
1348 config
->use_aapl
= lp_parm_bool(
1349 -1, FRUIT_PARAM_TYPE_NAME
, "aapl", true);
1351 config
->unix_info_enabled
= lp_parm_bool(
1352 -1, FRUIT_PARAM_TYPE_NAME
, "nfs_aces", true);
1354 config
->use_copyfile
= lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
,
1357 config
->readdir_attr_rsize
= lp_parm_bool(
1358 SNUM(handle
->conn
), "readdir_attr", "aapl_rsize", true);
1360 config
->readdir_attr_finder_info
= lp_parm_bool(
1361 SNUM(handle
->conn
), "readdir_attr", "aapl_finder_info", true);
1363 config
->readdir_attr_max_access
= lp_parm_bool(
1364 SNUM(handle
->conn
), "readdir_attr", "aapl_max_access", true);
1366 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
1367 NULL
, struct fruit_config_data
,
1374 * Prepend "._" to a basename
1376 static int adouble_path(TALLOC_CTX
*ctx
, const char *path_in
, char **path_out
)
1381 if (!parent_dirname(ctx
, path_in
, &parent
, &base
)) {
1385 *path_out
= talloc_asprintf(ctx
, "%s/._%s", parent
, base
);
1386 if (*path_out
== NULL
) {
1394 * Allocate and initialize an AfpInfo struct
1396 static AfpInfo
*afpinfo_new(TALLOC_CTX
*ctx
)
1398 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
1402 ai
->afpi_Signature
= AFP_Signature
;
1403 ai
->afpi_Version
= AFP_Version
;
1404 ai
->afpi_BackupTime
= AD_DATE_START
;
1409 * Pack an AfpInfo struct into a buffer
1411 * Buffer size must be at least AFP_INFO_SIZE
1412 * Returns size of packed buffer
1414 static ssize_t
afpinfo_pack(const AfpInfo
*ai
, char *buf
)
1416 memset(buf
, 0, AFP_INFO_SIZE
);
1418 RSIVAL(buf
, 0, ai
->afpi_Signature
);
1419 RSIVAL(buf
, 4, ai
->afpi_Version
);
1420 RSIVAL(buf
, 12, ai
->afpi_BackupTime
);
1421 memcpy(buf
+ 16, ai
->afpi_FinderInfo
, sizeof(ai
->afpi_FinderInfo
));
1423 return AFP_INFO_SIZE
;
1427 * Unpack a buffer into a AfpInfo structure
1429 * Buffer size must be at least AFP_INFO_SIZE
1430 * Returns allocated AfpInfo struct
1432 static AfpInfo
*afpinfo_unpack(TALLOC_CTX
*ctx
, const void *data
)
1434 AfpInfo
*ai
= talloc_zero(ctx
, AfpInfo
);
1439 ai
->afpi_Signature
= RIVAL(data
, 0);
1440 ai
->afpi_Version
= RIVAL(data
, 4);
1441 ai
->afpi_BackupTime
= RIVAL(data
, 12);
1442 memcpy(ai
->afpi_FinderInfo
, (const char *)data
+ 16,
1443 sizeof(ai
->afpi_FinderInfo
));
1445 if (ai
->afpi_Signature
!= AFP_Signature
1446 || ai
->afpi_Version
!= AFP_Version
) {
1447 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1455 * Fake an inode number from the md5 hash of the (xattr) name
1457 static SMB_INO_T
fruit_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
1460 unsigned char hash
[16];
1464 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
1465 SMB_ASSERT(upper_sname
!= NULL
);
1468 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_dev
),
1469 sizeof(sbuf
->st_ex_dev
));
1470 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_ino
),
1471 sizeof(sbuf
->st_ex_ino
));
1472 MD5Update(&ctx
, (unsigned char *)upper_sname
,
1473 talloc_get_size(upper_sname
)-1);
1474 MD5Final(hash
, &ctx
);
1476 TALLOC_FREE(upper_sname
);
1478 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1479 memcpy(&result
, hash
, sizeof(result
));
1481 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1482 sname
, (unsigned long long)result
));
1488 * Ensure ad_fsp is still valid
1490 static bool fruit_fsp_recheck(struct adouble
*ad
, files_struct
*fsp
)
1492 if (ad
->ad_fsp
== fsp
) {
1500 static bool add_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
1501 struct stream_struct
**streams
,
1502 const char *name
, off_t size
,
1505 struct stream_struct
*tmp
;
1507 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
1513 tmp
[*num_streams
].name
= talloc_asprintf(tmp
, "%s:$DATA", name
);
1514 if (tmp
[*num_streams
].name
== NULL
) {
1518 tmp
[*num_streams
].size
= size
;
1519 tmp
[*num_streams
].alloc_size
= alloc_size
;
1526 static bool del_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
1527 struct stream_struct
**streams
,
1530 struct stream_struct
*tmp
= *streams
;
1533 if (*num_streams
== 0) {
1537 for (i
= 0; i
< *num_streams
; i
++) {
1538 if (strequal_m(tmp
[i
].name
, name
)) {
1543 if (i
== *num_streams
) {
1547 TALLOC_FREE(tmp
[i
].name
);
1548 if (*num_streams
- 1 > i
) {
1549 memmove(&tmp
[i
], &tmp
[i
+1],
1550 (*num_streams
- i
- 1) * sizeof(struct stream_struct
));
1557 static bool empty_finderinfo(const struct adouble
*ad
)
1560 char emptybuf
[ADEDLEN_FINDERI
] = {0};
1561 if (memcmp(emptybuf
,
1562 ad_entry(ad
, ADEID_FINDERI
),
1563 ADEDLEN_FINDERI
) == 0) {
1570 * Update btime with btime from Netatalk
1572 static void update_btime(vfs_handle_struct
*handle
,
1573 struct smb_filename
*smb_fname
)
1576 struct timespec creation_time
= {0};
1579 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
, ADOUBLE_META
);
1583 if (ad_getdate(ad
, AD_DATE_UNIX
| AD_DATE_CREATE
, &t
) != 0) {
1589 creation_time
.tv_sec
= convert_uint32_t_to_time_t(t
);
1590 update_stat_ex_create_time(&smb_fname
->st
, creation_time
);
1596 * Map an access mask to a Netatalk single byte byte range lock
1598 static off_t
access_to_netatalk_brl(enum apple_fork fork_type
,
1599 uint32_t access_mask
)
1603 switch (access_mask
) {
1604 case FILE_READ_DATA
:
1605 offset
= AD_FILELOCK_OPEN_RD
;
1608 case FILE_WRITE_DATA
:
1609 case FILE_APPEND_DATA
:
1610 offset
= AD_FILELOCK_OPEN_WR
;
1614 offset
= AD_FILELOCK_OPEN_NONE
;
1618 if (fork_type
== APPLE_FORK_RSRC
) {
1619 if (offset
== AD_FILELOCK_OPEN_NONE
) {
1620 offset
= AD_FILELOCK_RSRC_OPEN_NONE
;
1630 * Map a deny mode to a Netatalk brl
1632 static off_t
denymode_to_netatalk_brl(enum apple_fork fork_type
,
1637 switch (deny_mode
) {
1639 offset
= AD_FILELOCK_DENY_RD
;
1643 offset
= AD_FILELOCK_DENY_WR
;
1647 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1650 if (fork_type
== APPLE_FORK_RSRC
) {
1658 * Call fcntl() with an exclusive F_GETLK request in order to
1659 * determine if there's an exisiting shared lock
1661 * @return true if the requested lock was found or any error occured
1662 * false if the lock was not found
1664 static bool test_netatalk_lock(files_struct
*fsp
, off_t in_offset
)
1667 off_t offset
= in_offset
;
1672 result
= SMB_VFS_GETLOCK(fsp
, &offset
, &len
, &type
, &pid
);
1673 if (result
== false) {
1677 if (type
!= F_UNLCK
) {
1684 static NTSTATUS
fruit_check_access(vfs_handle_struct
*handle
,
1686 uint32_t access_mask
,
1689 NTSTATUS status
= NT_STATUS_OK
;
1690 struct byte_range_lock
*br_lck
= NULL
;
1691 bool open_for_reading
, open_for_writing
, deny_read
, deny_write
;
1694 /* FIXME: hardcoded data fork, add resource fork */
1695 enum apple_fork fork_type
= APPLE_FORK_DATA
;
1697 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1699 access_mask
& FILE_READ_DATA
? "READ" :"-",
1700 access_mask
& FILE_WRITE_DATA
? "WRITE" : "-",
1701 deny_mode
& DENY_READ
? "DENY_READ" : "-",
1702 deny_mode
& DENY_WRITE
? "DENY_WRITE" : "-"));
1705 * Check read access and deny read mode
1707 if ((access_mask
& FILE_READ_DATA
) || (deny_mode
& DENY_READ
)) {
1709 open_for_reading
= test_netatalk_lock(
1710 fsp
, access_to_netatalk_brl(fork_type
, FILE_READ_DATA
));
1712 deny_read
= test_netatalk_lock(
1713 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_READ
));
1715 DEBUG(10, ("read: %s, deny_write: %s\n",
1716 open_for_reading
== true ? "yes" : "no",
1717 deny_read
== true ? "yes" : "no"));
1719 if (((access_mask
& FILE_READ_DATA
) && deny_read
)
1720 || ((deny_mode
& DENY_READ
) && open_for_reading
)) {
1721 return NT_STATUS_SHARING_VIOLATION
;
1725 if (access_mask
& FILE_READ_DATA
) {
1726 off
= access_to_netatalk_brl(fork_type
, FILE_READ_DATA
);
1728 handle
->conn
->sconn
->msg_ctx
, fsp
,
1729 fsp
->op
->global
->open_persistent_id
, 1, off
,
1730 READ_LOCK
, POSIX_LOCK
, false,
1733 if (!NT_STATUS_IS_OK(status
)) {
1736 TALLOC_FREE(br_lck
);
1739 if (deny_mode
& DENY_READ
) {
1740 off
= denymode_to_netatalk_brl(fork_type
, DENY_READ
);
1742 handle
->conn
->sconn
->msg_ctx
, fsp
,
1743 fsp
->op
->global
->open_persistent_id
, 1, off
,
1744 READ_LOCK
, POSIX_LOCK
, false,
1747 if (!NT_STATUS_IS_OK(status
)) {
1750 TALLOC_FREE(br_lck
);
1755 * Check write access and deny write mode
1757 if ((access_mask
& FILE_WRITE_DATA
) || (deny_mode
& DENY_WRITE
)) {
1759 open_for_writing
= test_netatalk_lock(
1760 fsp
, access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
));
1762 deny_write
= test_netatalk_lock(
1763 fsp
, denymode_to_netatalk_brl(fork_type
, DENY_WRITE
));
1765 DEBUG(10, ("write: %s, deny_write: %s\n",
1766 open_for_writing
== true ? "yes" : "no",
1767 deny_write
== true ? "yes" : "no"));
1769 if (((access_mask
& FILE_WRITE_DATA
) && deny_write
)
1770 || ((deny_mode
& DENY_WRITE
) && open_for_writing
)) {
1771 return NT_STATUS_SHARING_VIOLATION
;
1775 if (access_mask
& FILE_WRITE_DATA
) {
1776 off
= access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
);
1778 handle
->conn
->sconn
->msg_ctx
, fsp
,
1779 fsp
->op
->global
->open_persistent_id
, 1, off
,
1780 READ_LOCK
, POSIX_LOCK
, false,
1783 if (!NT_STATUS_IS_OK(status
)) {
1786 TALLOC_FREE(br_lck
);
1789 if (deny_mode
& DENY_WRITE
) {
1790 off
= denymode_to_netatalk_brl(fork_type
, DENY_WRITE
);
1792 handle
->conn
->sconn
->msg_ctx
, fsp
,
1793 fsp
->op
->global
->open_persistent_id
, 1, off
,
1794 READ_LOCK
, POSIX_LOCK
, false,
1797 if (!NT_STATUS_IS_OK(status
)) {
1800 TALLOC_FREE(br_lck
);
1804 TALLOC_FREE(br_lck
);
1809 static NTSTATUS
check_aapl(vfs_handle_struct
*handle
,
1810 struct smb_request
*req
,
1811 const struct smb2_create_blobs
*in_context_blobs
,
1812 struct smb2_create_blobs
*out_context_blobs
)
1814 struct fruit_config_data
*config
;
1816 struct smb2_create_blob
*aapl
= NULL
;
1820 DATA_BLOB blob
= data_blob_talloc(req
, NULL
, 0);
1821 uint64_t req_bitmap
, client_caps
;
1822 uint64_t server_caps
= SMB2_CRTCTX_AAPL_UNIX_BASED
;
1826 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
1827 return NT_STATUS_UNSUCCESSFUL
);
1829 if (!config
->use_aapl
1830 || in_context_blobs
== NULL
1831 || out_context_blobs
== NULL
) {
1832 return NT_STATUS_OK
;
1835 aapl
= smb2_create_blob_find(in_context_blobs
,
1836 SMB2_CREATE_TAG_AAPL
);
1838 return NT_STATUS_OK
;
1841 if (aapl
->data
.length
!= 24) {
1842 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1843 (uintmax_t)aapl
->data
.length
));
1844 return NT_STATUS_INVALID_PARAMETER
;
1847 cmd
= IVAL(aapl
->data
.data
, 0);
1848 if (cmd
!= SMB2_CRTCTX_AAPL_SERVER_QUERY
) {
1849 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd
));
1850 return NT_STATUS_INVALID_PARAMETER
;
1853 req_bitmap
= BVAL(aapl
->data
.data
, 8);
1854 client_caps
= BVAL(aapl
->data
.data
, 16);
1856 SIVAL(p
, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY
);
1858 SBVAL(p
, 8, req_bitmap
);
1859 ok
= data_blob_append(req
, &blob
, p
, 16);
1861 return NT_STATUS_UNSUCCESSFUL
;
1864 if (req_bitmap
& SMB2_CRTCTX_AAPL_SERVER_CAPS
) {
1865 if ((client_caps
& SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
) &&
1866 (handle
->conn
->tcon
->compat
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
1867 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
;
1868 config
->readdir_attr_enabled
= true;
1871 if (config
->use_copyfile
) {
1872 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE
;
1873 config
->copyfile_enabled
= true;
1877 * The client doesn't set the flag, so we can't check
1878 * for it and just set it unconditionally
1880 if (config
->unix_info_enabled
) {
1881 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE
;
1884 SBVAL(p
, 0, server_caps
);
1885 ok
= data_blob_append(req
, &blob
, p
, 8);
1887 return NT_STATUS_UNSUCCESSFUL
;
1891 if (req_bitmap
& SMB2_CRTCTX_AAPL_VOLUME_CAPS
) {
1893 lp_case_sensitive(SNUM(handle
->conn
->tcon
->compat
)) ?
1894 SMB2_CRTCTX_AAPL_CASE_SENSITIVE
: 0);
1895 ok
= data_blob_append(req
, &blob
, p
, 8);
1897 return NT_STATUS_UNSUCCESSFUL
;
1901 if (req_bitmap
& SMB2_CRTCTX_AAPL_MODEL_INFO
) {
1902 ok
= convert_string_talloc(req
,
1903 CH_UNIX
, CH_UTF16LE
,
1904 "Samba", strlen("Samba"),
1907 return NT_STATUS_UNSUCCESSFUL
;
1911 SIVAL(p
+ 4, 0, modellen
);
1912 ok
= data_blob_append(req
, &blob
, p
, 8);
1915 return NT_STATUS_UNSUCCESSFUL
;
1918 ok
= data_blob_append(req
, &blob
, model
, modellen
);
1921 return NT_STATUS_UNSUCCESSFUL
;
1925 status
= smb2_create_blob_add(out_context_blobs
,
1927 SMB2_CREATE_TAG_AAPL
,
1933 static NTSTATUS
readdir_attr_macmeta(struct vfs_handle_struct
*handle
,
1934 const struct smb_filename
*smb_fname
,
1935 struct readdir_attr_data
*attr_data
)
1937 NTSTATUS status
= NT_STATUS_OK
;
1938 uint32_t date_added
;
1939 struct adouble
*ad
= NULL
;
1940 struct fruit_config_data
*config
= NULL
;
1942 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1943 struct fruit_config_data
,
1944 return NT_STATUS_UNSUCCESSFUL
);
1947 /* Ensure we return a default value in the creation_date field */
1948 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
, 12, AD_DATE_START
);
1951 * Resource fork length
1954 if (config
->readdir_attr_rsize
) {
1955 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
,
1958 attr_data
->attr_data
.aapl
.rfork_size
= ad_getentrylen(
1968 if (config
->readdir_attr_finder_info
) {
1969 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
,
1972 if (S_ISREG(smb_fname
->st
.st_ex_mode
)) {
1974 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0],
1975 ad_entry(ad
, ADEID_FINDERI
), 4);
1977 /* finder_creator */
1978 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 4,
1979 ad_entry(ad
, ADEID_FINDERI
) + 4, 4);
1983 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 8,
1984 ad_entry(ad
, ADEID_FINDERI
) + 8, 2);
1986 /* finder_ext_flags */
1987 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 10,
1988 ad_entry(ad
, ADEID_FINDERI
) + 24, 2);
1991 date_added
= convert_time_t_to_uint32_t(
1992 smb_fname
->st
.st_ex_btime
.tv_sec
- AD_DATE_DELTA
);
1993 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
[0], 12, date_added
);
2003 /* Search MS NFS style ACE with UNIX mode */
2004 static NTSTATUS
check_ms_nfs(vfs_handle_struct
*handle
,
2006 const struct security_descriptor
*psd
,
2011 struct fruit_config_data
*config
= NULL
;
2015 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2016 struct fruit_config_data
,
2017 return NT_STATUS_UNSUCCESSFUL
);
2019 if (psd
->dacl
== NULL
|| !config
->unix_info_enabled
) {
2020 return NT_STATUS_OK
;
2023 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
2024 if (dom_sid_compare_domain(
2025 &global_sid_Unix_NFS_Mode
,
2026 &psd
->dacl
->aces
[i
].trustee
) == 0) {
2027 *pmode
= (mode_t
)psd
->dacl
->aces
[i
].trustee
.sub_auths
[2];
2028 *pmode
&= (S_IRWXU
| S_IRWXG
| S_IRWXO
);
2031 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2032 fsp_str_dbg(fsp
), (unsigned)(*pmode
)));
2037 return NT_STATUS_OK
;
2040 /****************************************************************************
2042 ****************************************************************************/
2044 static int fruit_connect(vfs_handle_struct
*handle
,
2045 const char *service
,
2049 char *list
= NULL
, *newlist
= NULL
;
2050 struct fruit_config_data
*config
;
2052 DEBUG(10, ("fruit_connect\n"));
2054 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
2059 rc
= init_fruit_config(handle
);
2064 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2065 struct fruit_config_data
, return -1);
2067 if (config
->veto_appledouble
) {
2068 list
= lp_veto_files(talloc_tos(), SNUM(handle
->conn
));
2071 if (strstr(list
, "/" ADOUBLE_NAME_PREFIX
"*/") == NULL
) {
2072 newlist
= talloc_asprintf(
2074 "%s/" ADOUBLE_NAME_PREFIX
"*/",
2076 lp_do_parameter(SNUM(handle
->conn
),
2081 lp_do_parameter(SNUM(handle
->conn
),
2083 "/" ADOUBLE_NAME_PREFIX
"*/");
2089 if (config
->encoding
== FRUIT_ENC_NATIVE
) {
2093 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2094 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2095 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2096 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2097 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2098 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2099 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2100 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2101 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2102 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2109 static int fruit_open_meta(vfs_handle_struct
*handle
,
2110 struct smb_filename
*smb_fname
,
2111 files_struct
*fsp
, int flags
, mode_t mode
)
2114 struct fruit_config_data
*config
= NULL
;
2115 struct smb_filename
*smb_fname_base
= NULL
;
2118 struct adouble
*ad
= NULL
;
2120 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname
)));
2122 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2123 struct fruit_config_data
, return -1);
2125 if (config
->meta
== FRUIT_META_STREAM
) {
2126 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2129 /* Create an smb_filename with stream_name == NULL. */
2130 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
2131 smb_fname
->base_name
, NULL
, NULL
);
2133 if (smb_fname_base
== NULL
) {
2140 * We use baseflags to turn off nasty side-effects when opening the
2144 baseflags
&= ~O_TRUNC
;
2145 baseflags
&= ~O_EXCL
;
2146 baseflags
&= ~O_CREAT
;
2148 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
2152 * It is legit to open a stream on a directory, but the base
2153 * fd has to be read-only.
2155 if ((hostfd
== -1) && (errno
== EISDIR
)) {
2156 baseflags
&= ~O_ACCMODE
;
2157 baseflags
|= O_RDONLY
;
2158 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
2162 TALLOC_FREE(smb_fname_base
);
2169 if (flags
& (O_CREAT
| O_TRUNC
)) {
2171 * The attribute does not exist or needs to be truncated,
2172 * create an AppleDouble EA
2174 ad
= ad_init(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2175 handle
, ADOUBLE_META
, fsp
);
2181 rc
= ad_write(ad
, smb_fname
->base_name
);
2187 ad
= ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2188 handle
, ADOUBLE_META
, fsp
);
2193 if (ad_read(ad
, smb_fname
->base_name
) == -1) {
2200 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc
, hostfd
));
2202 int saved_errno
= errno
;
2205 * BUGBUGBUG -- we would need to call
2206 * fd_close_posix here, but we don't have a
2209 fsp
->fh
->fd
= hostfd
;
2213 errno
= saved_errno
;
2218 static int fruit_open_rsrc(vfs_handle_struct
*handle
,
2219 struct smb_filename
*smb_fname
,
2220 files_struct
*fsp
, int flags
, mode_t mode
)
2223 struct fruit_config_data
*config
= NULL
;
2224 struct adouble
*ad
= NULL
;
2225 struct smb_filename
*smb_fname_base
= NULL
;
2226 char *adpath
= NULL
;
2229 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname
)));
2231 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2232 struct fruit_config_data
, return -1);
2234 switch (config
->rsrc
) {
2235 case FRUIT_RSRC_STREAM
:
2236 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2237 case FRUIT_RSRC_XATTR
:
2238 #ifdef HAVE_ATTROPEN
2239 hostfd
= attropen(smb_fname
->base_name
,
2240 AFPRESOURCE_EA_NETATALK
, flags
, mode
);
2244 ad
= ad_init(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2245 handle
, ADOUBLE_RSRC
, fsp
);
2259 if (!(flags
& O_CREAT
) && !VALID_STAT(smb_fname
->st
)) {
2260 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2267 if (VALID_STAT(smb_fname
->st
) && S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2268 /* sorry, but directories don't habe a resource fork */
2273 rc
= adouble_path(talloc_tos(), smb_fname
->base_name
, &adpath
);
2278 /* Create an smb_filename with stream_name == NULL. */
2279 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
2280 adpath
, NULL
, NULL
);
2281 if (smb_fname_base
== NULL
) {
2287 /* Sanitize flags */
2288 if (flags
& O_WRONLY
) {
2289 /* We always need read access for the metadata header too */
2294 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
2301 /* REVIEW: we need this in ad_write() */
2302 fsp
->fh
->fd
= hostfd
;
2304 if (flags
& (O_CREAT
| O_TRUNC
)) {
2305 ad
= ad_init(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2306 handle
, ADOUBLE_RSRC
, fsp
);
2311 rc
= ad_write(ad
, smb_fname
->base_name
);
2317 ad
= ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
2318 handle
, ADOUBLE_RSRC
, fsp
);
2323 if (ad_read(ad
, smb_fname
->base_name
) == -1) {
2331 TALLOC_FREE(adpath
);
2332 TALLOC_FREE(smb_fname_base
);
2334 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc
, hostfd
));
2336 int saved_errno
= errno
;
2339 * BUGBUGBUG -- we would need to call
2340 * fd_close_posix here, but we don't have a
2343 fsp
->fh
->fd
= hostfd
;
2347 errno
= saved_errno
;
2352 static int fruit_open(vfs_handle_struct
*handle
,
2353 struct smb_filename
*smb_fname
,
2354 files_struct
*fsp
, int flags
, mode_t mode
)
2356 DEBUG(10, ("fruit_open called for %s\n",
2357 smb_fname_str_dbg(smb_fname
)));
2359 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
2360 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2363 if (is_afpinfo_stream(smb_fname
)) {
2364 return fruit_open_meta(handle
, smb_fname
, fsp
, flags
, mode
);
2365 } else if (is_afpresource_stream(smb_fname
)) {
2366 return fruit_open_rsrc(handle
, smb_fname
, fsp
, flags
, mode
);
2369 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
2372 static int fruit_rename(struct vfs_handle_struct
*handle
,
2373 const struct smb_filename
*smb_fname_src
,
2374 const struct smb_filename
*smb_fname_dst
)
2377 char *src_adouble_path
= NULL
;
2378 char *dst_adouble_path
= NULL
;
2379 struct fruit_config_data
*config
= NULL
;
2381 rc
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
2383 if (!VALID_STAT(smb_fname_src
->st
)
2384 || !S_ISREG(smb_fname_src
->st
.st_ex_mode
)) {
2388 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2389 struct fruit_config_data
, return -1);
2391 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
2395 rc
= adouble_path(talloc_tos(), smb_fname_src
->base_name
,
2400 rc
= adouble_path(talloc_tos(), smb_fname_dst
->base_name
,
2406 DEBUG(10, ("fruit_rename: %s -> %s\n",
2407 src_adouble_path
, dst_adouble_path
));
2409 rc
= rename(src_adouble_path
, dst_adouble_path
);
2410 if (errno
== ENOENT
) {
2414 TALLOC_FREE(src_adouble_path
);
2415 TALLOC_FREE(dst_adouble_path
);
2421 static int fruit_unlink(vfs_handle_struct
*handle
,
2422 const struct smb_filename
*smb_fname
)
2425 struct fruit_config_data
*config
= NULL
;
2427 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2428 struct fruit_config_data
, return -1);
2430 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
2433 rc
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
2438 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
2443 * 0 byte resource fork streams are not listed by
2444 * vfs_streaminfo, as a result stream cleanup/deletion of file
2445 * deletion doesn't remove the resourcefork stream.
2447 rc
= adouble_path(talloc_tos(),
2448 smb_fname
->base_name
, &adp
);
2453 /* FIXME: direct unlink(), missing smb_fname */
2454 DEBUG(1,("fruit_unlink: %s\n", adp
));
2456 if ((rc
== -1) && (errno
== ENOENT
)) {
2464 if (is_afpinfo_stream(smb_fname
)) {
2465 if (config
->meta
== FRUIT_META_STREAM
) {
2466 rc
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
2468 rc
= SMB_VFS_REMOVEXATTR(handle
->conn
,
2469 smb_fname
->base_name
,
2470 AFPINFO_EA_NETATALK
);
2476 if (is_afpresource_stream(smb_fname
)) {
2477 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
2480 rc
= adouble_path(talloc_tos(),
2481 smb_fname
->base_name
, &adp
);
2485 /* FIXME: direct unlink(), missing smb_fname */
2487 if ((rc
== -1) && (errno
== ENOENT
)) {
2492 rc
= SMB_VFS_REMOVEXATTR(handle
->conn
,
2493 smb_fname
->base_name
,
2494 AFPRESOURCE_EA_NETATALK
);
2500 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
2506 static int fruit_chmod(vfs_handle_struct
*handle
,
2512 struct fruit_config_data
*config
= NULL
;
2515 rc
= SMB_VFS_NEXT_CHMOD(handle
, path
, mode
);
2520 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2521 struct fruit_config_data
, return -1);
2523 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
2527 /* FIXME: direct sys_lstat(), missing smb_fname */
2528 rc
= sys_lstat(path
, &sb
, false);
2529 if (rc
!= 0 || !S_ISREG(sb
.st_ex_mode
)) {
2533 rc
= adouble_path(talloc_tos(), path
, &adp
);
2538 DEBUG(10, ("fruit_chmod: %s\n", adp
));
2540 rc
= SMB_VFS_NEXT_CHMOD(handle
, adp
, mode
);
2541 if (errno
== ENOENT
) {
2549 static int fruit_chown(vfs_handle_struct
*handle
,
2556 struct fruit_config_data
*config
= NULL
;
2559 rc
= SMB_VFS_NEXT_CHOWN(handle
, path
, uid
, gid
);
2564 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2565 struct fruit_config_data
, return -1);
2567 if (config
->rsrc
== FRUIT_RSRC_XATTR
) {
2571 /* FIXME: direct sys_lstat(), missing smb_fname */
2572 rc
= sys_lstat(path
, &sb
, false);
2573 if (rc
!= 0 || !S_ISREG(sb
.st_ex_mode
)) {
2577 rc
= adouble_path(talloc_tos(), path
, &adp
);
2582 DEBUG(10, ("fruit_chown: %s\n", adp
));
2584 rc
= SMB_VFS_NEXT_CHOWN(handle
, adp
, uid
, gid
);
2585 if (errno
== ENOENT
) {
2594 static int fruit_rmdir(struct vfs_handle_struct
*handle
, const char *path
)
2598 struct fruit_config_data
*config
;
2600 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2601 struct fruit_config_data
, return -1);
2603 if (!handle
->conn
->cwd
|| !path
|| (config
->rsrc
== FRUIT_RSRC_XATTR
)) {
2608 * Due to there is no way to change bDeleteVetoFiles variable
2609 * from this module, need to clean up ourselves
2616 while ((de
= readdir(dh
)) != NULL
) {
2617 if ((strncmp(de
->d_name
,
2618 ADOUBLE_NAME_PREFIX
,
2619 strlen(ADOUBLE_NAME_PREFIX
))) == 0) {
2620 char *p
= talloc_asprintf(talloc_tos(),
2626 DEBUG(10, ("fruit_rmdir: delete %s\n", p
));
2636 return SMB_VFS_NEXT_RMDIR(handle
, path
);
2639 static ssize_t
fruit_pread(vfs_handle_struct
*handle
,
2640 files_struct
*fsp
, void *data
,
2641 size_t n
, off_t offset
)
2644 struct adouble
*ad
= (struct adouble
*)VFS_FETCH_FSP_EXTENSION(
2646 struct fruit_config_data
*config
= NULL
;
2650 char *tmp_base_name
= NULL
;
2653 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset
, (int)n
));
2655 if (!fsp
->base_fsp
) {
2656 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2659 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2660 struct fruit_config_data
, return -1);
2662 /* fsp_name is not converted with vfs_catia */
2663 tmp_base_name
= fsp
->base_fsp
->fsp_name
->base_name
;
2664 status
= SMB_VFS_TRANSLATE_NAME(handle
->conn
,
2665 fsp
->base_fsp
->fsp_name
->base_name
,
2666 vfs_translate_to_unix
,
2667 talloc_tos(), &name
);
2668 if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
2669 name
= talloc_strdup(talloc_tos(), tmp_base_name
);
2674 } else if (!NT_STATUS_IS_OK(status
)) {
2675 errno
= map_errno_from_nt_status(status
);
2679 fsp
->base_fsp
->fsp_name
->base_name
= name
;
2682 len
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2690 if (!fruit_fsp_recheck(ad
, fsp
)) {
2695 if (ad
->ad_type
== ADOUBLE_META
) {
2696 char afpinfo_buf
[AFP_INFO_SIZE
];
2699 if ((offset
< 0) || (offset
>= AFP_INFO_SIZE
)) {
2705 to_return
= AFP_INFO_SIZE
- offset
;
2707 ai
= afpinfo_new(talloc_tos());
2713 len
= ad_read(ad
, fsp
->base_fsp
->fsp_name
->base_name
);
2719 memcpy(&ai
->afpi_FinderInfo
[0],
2720 ad_entry(ad
, ADEID_FINDERI
),
2722 len
= afpinfo_pack(ai
, afpinfo_buf
);
2723 if (len
!= AFP_INFO_SIZE
) {
2728 memcpy(data
, afpinfo_buf
+ offset
, to_return
);
2731 len
= SMB_VFS_NEXT_PREAD(
2732 handle
, fsp
, data
, n
,
2733 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2740 fsp
->base_fsp
->fsp_name
->base_name
= tmp_base_name
;
2746 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc
, len
));
2750 static ssize_t
fruit_pwrite(vfs_handle_struct
*handle
,
2751 files_struct
*fsp
, const void *data
,
2752 size_t n
, off_t offset
)
2755 struct adouble
*ad
= (struct adouble
*)VFS_FETCH_FSP_EXTENSION(
2757 struct fruit_config_data
*config
= NULL
;
2761 char *tmp_base_name
= NULL
;
2764 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset
, (int)n
));
2766 if (!fsp
->base_fsp
) {
2767 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2770 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2771 struct fruit_config_data
, return -1);
2773 tmp_base_name
= fsp
->base_fsp
->fsp_name
->base_name
;
2774 status
= SMB_VFS_TRANSLATE_NAME(handle
->conn
,
2775 fsp
->base_fsp
->fsp_name
->base_name
,
2776 vfs_translate_to_unix
,
2777 talloc_tos(), &name
);
2778 if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
2779 name
= talloc_strdup(talloc_tos(), tmp_base_name
);
2784 } else if (!NT_STATUS_IS_OK(status
)) {
2785 errno
= map_errno_from_nt_status(status
);
2789 fsp
->base_fsp
->fsp_name
->base_name
= name
;
2792 len
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2800 if (!fruit_fsp_recheck(ad
, fsp
)) {
2805 if (ad
->ad_type
== ADOUBLE_META
) {
2806 if (n
!= AFP_INFO_SIZE
|| offset
!= 0) {
2807 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2808 (intmax_t)offset
, (intmax_t)n
));
2812 ai
= afpinfo_unpack(talloc_tos(), data
);
2817 memcpy(ad_entry(ad
, ADEID_FINDERI
),
2818 &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
2819 rc
= ad_write(ad
, name
);
2821 len
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
,
2822 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2828 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
2829 rc
= ad_read(ad
, name
);
2835 if ((len
+ offset
) > ad_getentrylen(ad
, ADEID_RFORK
)) {
2836 ad_setentrylen(ad
, ADEID_RFORK
, len
+ offset
);
2837 rc
= ad_write(ad
, name
);
2843 fsp
->base_fsp
->fsp_name
->base_name
= tmp_base_name
;
2853 * Helper to stat/lstat the base file of an smb_fname.
2855 static int fruit_stat_base(vfs_handle_struct
*handle
,
2856 struct smb_filename
*smb_fname
,
2859 char *tmp_stream_name
;
2862 tmp_stream_name
= smb_fname
->stream_name
;
2863 smb_fname
->stream_name
= NULL
;
2865 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2867 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2869 smb_fname
->stream_name
= tmp_stream_name
;
2873 static int fruit_stat_meta(vfs_handle_struct
*handle
,
2874 struct smb_filename
*smb_fname
,
2877 /* Populate the stat struct with info from the base file. */
2878 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
2881 smb_fname
->st
.st_ex_size
= AFP_INFO_SIZE
;
2882 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
2883 smb_fname
->stream_name
);
2887 static int fruit_stat_rsrc(vfs_handle_struct
*handle
,
2888 struct smb_filename
*smb_fname
,
2892 struct adouble
*ad
= NULL
;
2894 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2895 smb_fname_str_dbg(smb_fname
)));
2897 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
, ADOUBLE_RSRC
);
2903 /* Populate the stat struct with info from the base file. */
2904 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
2909 smb_fname
->st
.st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
2910 smb_fname
->st
.st_ex_ino
= fruit_inode(&smb_fname
->st
,
2911 smb_fname
->stream_name
);
2916 static int fruit_stat(vfs_handle_struct
*handle
,
2917 struct smb_filename
*smb_fname
)
2921 DEBUG(10, ("fruit_stat called for %s\n",
2922 smb_fname_str_dbg(smb_fname
)));
2924 if (!is_ntfs_stream_smb_fname(smb_fname
)
2925 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
2926 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2928 update_btime(handle
, smb_fname
);
2934 * Note if lp_posix_paths() is true, we can never
2935 * get here as is_ntfs_stream_smb_fname() is
2936 * always false. So we never need worry about
2937 * not following links here.
2940 if (is_afpinfo_stream(smb_fname
)) {
2941 rc
= fruit_stat_meta(handle
, smb_fname
, true);
2942 } else if (is_afpresource_stream(smb_fname
)) {
2943 rc
= fruit_stat_rsrc(handle
, smb_fname
, true);
2945 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2949 update_btime(handle
, smb_fname
);
2950 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
2951 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
2952 smb_fname
->st
.st_ex_blocks
=
2953 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
2958 static int fruit_lstat(vfs_handle_struct
*handle
,
2959 struct smb_filename
*smb_fname
)
2963 DEBUG(10, ("fruit_lstat called for %s\n",
2964 smb_fname_str_dbg(smb_fname
)));
2966 if (!is_ntfs_stream_smb_fname(smb_fname
)
2967 || is_ntfs_default_stream_smb_fname(smb_fname
)) {
2968 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2970 update_btime(handle
, smb_fname
);
2975 if (is_afpinfo_stream(smb_fname
)) {
2976 rc
= fruit_stat_meta(handle
, smb_fname
, false);
2977 } else if (is_afpresource_stream(smb_fname
)) {
2978 rc
= fruit_stat_rsrc(handle
, smb_fname
, false);
2980 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2984 update_btime(handle
, smb_fname
);
2985 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
2986 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
2987 smb_fname
->st
.st_ex_blocks
=
2988 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
2993 static int fruit_fstat_meta(vfs_handle_struct
*handle
,
2995 SMB_STRUCT_STAT
*sbuf
)
2997 DEBUG(10, ("fruit_fstat_meta called for %s\n",
2998 smb_fname_str_dbg(fsp
->base_fsp
->fsp_name
)));
3000 /* Populate the stat struct with info from the base file. */
3001 if (fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false) == -1) {
3004 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3005 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
3006 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3011 static int fruit_fstat_rsrc(vfs_handle_struct
*handle
, files_struct
*fsp
,
3012 SMB_STRUCT_STAT
*sbuf
)
3014 struct fruit_config_data
*config
;
3015 struct adouble
*ad
= (struct adouble
*)VFS_FETCH_FSP_EXTENSION(
3018 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
3019 smb_fname_str_dbg(fsp
->base_fsp
->fsp_name
)));
3021 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3022 struct fruit_config_data
, return -1);
3024 if (config
->rsrc
== FRUIT_RSRC_STREAM
) {
3025 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3028 /* Populate the stat struct with info from the base file. */
3029 if (fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false) == -1) {
3032 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3033 sbuf
->st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
3034 sbuf
->st_ex_ino
= fruit_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3036 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3037 smb_fname_str_dbg(fsp
->fsp_name
),
3038 (ssize_t
)sbuf
->st_ex_size
));
3043 static int fruit_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
3044 SMB_STRUCT_STAT
*sbuf
)
3048 char *tmp_base_name
= NULL
;
3050 struct adouble
*ad
= (struct adouble
*)
3051 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3053 DEBUG(10, ("fruit_fstat called for %s\n",
3054 smb_fname_str_dbg(fsp
->fsp_name
)));
3056 if (fsp
->base_fsp
) {
3057 tmp_base_name
= fsp
->base_fsp
->fsp_name
->base_name
;
3058 /* fsp_name is not converted with vfs_catia */
3059 status
= SMB_VFS_TRANSLATE_NAME(
3061 fsp
->base_fsp
->fsp_name
->base_name
,
3062 vfs_translate_to_unix
,
3063 talloc_tos(), &name
);
3065 if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
3066 name
= talloc_strdup(talloc_tos(), tmp_base_name
);
3071 } else if (!NT_STATUS_IS_OK(status
)) {
3072 errno
= map_errno_from_nt_status(status
);
3076 fsp
->base_fsp
->fsp_name
->base_name
= name
;
3079 if (ad
== NULL
|| fsp
->base_fsp
== NULL
) {
3080 rc
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3084 if (!fruit_fsp_recheck(ad
, fsp
)) {
3089 switch (ad
->ad_type
) {
3091 rc
= fruit_fstat_meta(handle
, fsp
, sbuf
);
3094 rc
= fruit_fstat_rsrc(handle
, fsp
, sbuf
);
3097 DEBUG(10, ("fruit_fstat %s: bad type\n",
3098 smb_fname_str_dbg(fsp
->fsp_name
)));
3104 sbuf
->st_ex_mode
&= ~S_IFMT
;
3105 sbuf
->st_ex_mode
|= S_IFREG
;
3106 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3110 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3111 smb_fname_str_dbg(fsp
->fsp_name
),
3112 (ssize_t
)sbuf
->st_ex_size
));
3113 if (tmp_base_name
) {
3114 fsp
->base_fsp
->fsp_name
->base_name
= tmp_base_name
;
3120 static NTSTATUS
fruit_streaminfo(vfs_handle_struct
*handle
,
3121 struct files_struct
*fsp
,
3123 TALLOC_CTX
*mem_ctx
,
3124 unsigned int *pnum_streams
,
3125 struct stream_struct
**pstreams
)
3127 struct fruit_config_data
*config
= NULL
;
3128 struct smb_filename
*smb_fname
= NULL
;
3129 struct adouble
*ad
= NULL
;
3132 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3133 return NT_STATUS_UNSUCCESSFUL
);
3134 DEBUG(10, ("fruit_streaminfo called for %s\n", fname
));
3136 smb_fname
= synthetic_smb_fname(talloc_tos(), fname
, NULL
, NULL
);
3137 if (smb_fname
== NULL
) {
3138 return NT_STATUS_NO_MEMORY
;
3141 if (config
->meta
== FRUIT_META_NETATALK
) {
3142 ad
= ad_get(talloc_tos(), handle
,
3143 smb_fname
->base_name
, ADOUBLE_META
);
3144 if (ad
&& !empty_finderinfo(ad
)) {
3145 if (!add_fruit_stream(
3146 mem_ctx
, pnum_streams
, pstreams
,
3147 AFPINFO_STREAM_NAME
, AFP_INFO_SIZE
,
3148 smb_roundup(handle
->conn
,
3151 TALLOC_FREE(smb_fname
);
3152 return NT_STATUS_NO_MEMORY
;
3158 if (config
->rsrc
!= FRUIT_RSRC_STREAM
) {
3159 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
,
3161 if (ad
&& (ad_getentrylen(ad
, ADEID_RFORK
) > 0)) {
3162 if (!add_fruit_stream(
3163 mem_ctx
, pnum_streams
, pstreams
,
3164 AFPRESOURCE_STREAM_NAME
,
3165 ad_getentrylen(ad
, ADEID_RFORK
),
3166 smb_roundup(handle
->conn
,
3168 ad
, ADEID_RFORK
)))) {
3170 TALLOC_FREE(smb_fname
);
3171 return NT_STATUS_NO_MEMORY
;
3177 TALLOC_FREE(smb_fname
);
3179 status
= SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, fname
, mem_ctx
,
3180 pnum_streams
, pstreams
);
3181 if (!NT_STATUS_IS_OK(status
)) {
3185 if (config
->meta
== FRUIT_META_NETATALK
) {
3186 /* Remove the Netatalk xattr from the list */
3187 if (!del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3188 ":" NETATALK_META_XATTR
":$DATA")) {
3190 TALLOC_FREE(smb_fname
);
3191 return NT_STATUS_NO_MEMORY
;
3195 return NT_STATUS_OK
;
3198 static int fruit_ntimes(vfs_handle_struct
*handle
,
3199 const struct smb_filename
*smb_fname
,
3200 struct smb_file_time
*ft
)
3203 struct adouble
*ad
= NULL
;
3205 if (null_timespec(ft
->create_time
)) {
3209 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname
),
3210 time_to_asc(convert_timespec_to_time_t(ft
->create_time
))));
3212 ad
= ad_get(talloc_tos(), handle
, smb_fname
->base_name
, ADOUBLE_META
);
3217 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
,
3218 convert_time_t_to_uint32_t(ft
->create_time
.tv_sec
));
3220 rc
= ad_write(ad
, smb_fname
->base_name
);
3226 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname
)));
3229 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
3232 static int fruit_fallocate(struct vfs_handle_struct
*handle
,
3233 struct files_struct
*fsp
,
3238 struct adouble
*ad
=
3239 (struct adouble
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3242 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
3245 if (!fruit_fsp_recheck(ad
, fsp
)) {
3249 /* Let the pwrite code path handle it. */
3254 static int fruit_ftruncate_meta(struct vfs_handle_struct
*handle
,
3255 struct files_struct
*fsp
,
3260 * As this request hasn't been seen in the wild,
3261 * the only sensible use I can imagine is the client
3262 * truncating the stream to 0 bytes size.
3263 * We simply remove the metadata on such a request.
3266 DBG_WARNING("ftruncate %s to %jd",
3267 fsp_str_dbg(fsp
), (intmax_t)offset
);
3271 return SMB_VFS_FREMOVEXATTR(fsp
, AFPRESOURCE_EA_NETATALK
);
3274 static int fruit_ftruncate_rsrc(struct vfs_handle_struct
*handle
,
3275 struct files_struct
*fsp
,
3280 struct fruit_config_data
*config
;
3282 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3283 struct fruit_config_data
, return -1);
3285 if (config
->rsrc
== FRUIT_RSRC_XATTR
&& offset
== 0) {
3286 return SMB_VFS_FREMOVEXATTR(fsp
,
3287 AFPRESOURCE_EA_NETATALK
);
3290 rc
= SMB_VFS_NEXT_FTRUNCATE(
3292 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
3297 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
3298 ad_setentrylen(ad
, ADEID_RFORK
, offset
);
3299 rc
= ad_write(ad
, NULL
);
3303 DEBUG(10, ("fruit_ftruncate_rsrc file %s offset %jd\n",
3304 fsp_str_dbg(fsp
), (intmax_t)offset
));
3310 static int fruit_ftruncate(struct vfs_handle_struct
*handle
,
3311 struct files_struct
*fsp
,
3315 struct adouble
*ad
=
3316 (struct adouble
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3318 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3319 fsp_str_dbg(fsp
), (double)offset
));
3322 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
3325 if (!fruit_fsp_recheck(ad
, fsp
)) {
3329 switch (ad
->ad_type
) {
3331 rc
= fruit_ftruncate_meta(handle
, fsp
, offset
, ad
);
3335 rc
= fruit_ftruncate_rsrc(handle
, fsp
, offset
, ad
);
3345 static NTSTATUS
fruit_create_file(vfs_handle_struct
*handle
,
3346 struct smb_request
*req
,
3347 uint16_t root_dir_fid
,
3348 struct smb_filename
*smb_fname
,
3349 uint32_t access_mask
,
3350 uint32_t share_access
,
3351 uint32_t create_disposition
,
3352 uint32_t create_options
,
3353 uint32_t file_attributes
,
3354 uint32_t oplock_request
,
3355 struct smb2_lease
*lease
,
3356 uint64_t allocation_size
,
3357 uint32_t private_flags
,
3358 struct security_descriptor
*sd
,
3359 struct ea_list
*ea_list
,
3360 files_struct
**result
,
3362 const struct smb2_create_blobs
*in_context_blobs
,
3363 struct smb2_create_blobs
*out_context_blobs
)
3366 struct fruit_config_data
*config
= NULL
;
3367 files_struct
*fsp
= NULL
;
3369 status
= check_aapl(handle
, req
, in_context_blobs
, out_context_blobs
);
3370 if (!NT_STATUS_IS_OK(status
)) {
3374 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3375 return NT_STATUS_UNSUCCESSFUL
);
3377 status
= SMB_VFS_NEXT_CREATE_FILE(
3378 handle
, req
, root_dir_fid
, smb_fname
,
3379 access_mask
, share_access
,
3380 create_disposition
, create_options
,
3381 file_attributes
, oplock_request
,
3383 allocation_size
, private_flags
,
3384 sd
, ea_list
, result
,
3385 pinfo
, in_context_blobs
, out_context_blobs
);
3386 if (!NT_STATUS_IS_OK(status
)) {
3391 if (config
->copyfile_enabled
) {
3393 * Set a flag in the fsp. Gets used in copychunk to
3394 * check whether the special Apple copyfile semantics
3395 * for copychunk should be allowed in a copychunk
3396 * request with a count of 0.
3398 fsp
->aapl_copyfile_supported
= true;
3402 * If this is a plain open for existing files, opening an 0
3403 * byte size resource fork MUST fail with
3404 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
3406 * Cf the vfs_fruit torture tests in test_rfork_create().
3408 if (is_afpresource_stream(fsp
->fsp_name
) &&
3409 create_disposition
== FILE_OPEN
)
3411 if (fsp
->fsp_name
->st
.st_ex_size
== 0) {
3412 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3417 if (is_ntfs_stream_smb_fname(smb_fname
)
3418 || fsp
->is_directory
) {
3422 if (config
->locking
== FRUIT_LOCKING_NETATALK
) {
3423 status
= fruit_check_access(
3426 map_share_mode_to_deny_mode(share_access
, 0));
3427 if (!NT_STATUS_IS_OK(status
)) {
3435 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status
)));
3438 close_file(req
, fsp
, ERROR_CLOSE
);
3439 *result
= fsp
= NULL
;
3445 static NTSTATUS
fruit_readdir_attr(struct vfs_handle_struct
*handle
,
3446 const struct smb_filename
*fname
,
3447 TALLOC_CTX
*mem_ctx
,
3448 struct readdir_attr_data
**pattr_data
)
3450 struct fruit_config_data
*config
= NULL
;
3451 struct readdir_attr_data
*attr_data
;
3454 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3455 struct fruit_config_data
,
3456 return NT_STATUS_UNSUCCESSFUL
);
3458 if (!config
->use_aapl
) {
3459 return SMB_VFS_NEXT_READDIR_ATTR(handle
, fname
, mem_ctx
, pattr_data
);
3462 DEBUG(10, ("fruit_readdir_attr %s\n", fname
->base_name
));
3464 *pattr_data
= talloc_zero(mem_ctx
, struct readdir_attr_data
);
3465 if (*pattr_data
== NULL
) {
3466 return NT_STATUS_UNSUCCESSFUL
;
3468 attr_data
= *pattr_data
;
3469 attr_data
->type
= RDATTR_AAPL
;
3472 * Mac metadata: compressed FinderInfo, resource fork length
3475 status
= readdir_attr_macmeta(handle
, fname
, attr_data
);
3476 if (!NT_STATUS_IS_OK(status
)) {
3478 * Error handling is tricky: if we return failure from
3479 * this function, the corresponding directory entry
3480 * will to be passed to the client, so we really just
3481 * want to error out on fatal errors.
3483 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
3491 if (config
->unix_info_enabled
) {
3492 attr_data
->attr_data
.aapl
.unix_mode
= fname
->st
.st_ex_mode
;
3498 if (!config
->readdir_attr_max_access
) {
3499 attr_data
->attr_data
.aapl
.max_access
= FILE_GENERIC_ALL
;
3501 status
= smbd_calculate_access_mask(
3505 SEC_FLAG_MAXIMUM_ALLOWED
,
3506 &attr_data
->attr_data
.aapl
.max_access
);
3507 if (!NT_STATUS_IS_OK(status
)) {
3512 return NT_STATUS_OK
;
3515 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3516 fname
->base_name
, nt_errstr(status
)));
3517 TALLOC_FREE(*pattr_data
);
3521 static NTSTATUS
fruit_fget_nt_acl(vfs_handle_struct
*handle
,
3523 uint32_t security_info
,
3524 TALLOC_CTX
*mem_ctx
,
3525 struct security_descriptor
**ppdesc
)
3528 struct security_ace ace
;
3530 struct fruit_config_data
*config
;
3532 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3533 struct fruit_config_data
,
3534 return NT_STATUS_UNSUCCESSFUL
);
3536 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
3538 if (!NT_STATUS_IS_OK(status
)) {
3543 * Add MS NFS style ACEs with uid, gid and mode
3545 if (!config
->unix_info_enabled
) {
3546 return NT_STATUS_OK
;
3549 /* MS NFS style mode */
3550 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
, fsp
->fsp_name
->st
.st_ex_mode
);
3551 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
3552 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
3553 if (!NT_STATUS_IS_OK(status
)) {
3554 DEBUG(1,("failed to add MS NFS style ACE\n"));
3558 /* MS NFS style uid */
3559 sid_compose(&sid
, &global_sid_Unix_NFS_Users
, fsp
->fsp_name
->st
.st_ex_uid
);
3560 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
3561 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
3562 if (!NT_STATUS_IS_OK(status
)) {
3563 DEBUG(1,("failed to add MS NFS style ACE\n"));
3567 /* MS NFS style gid */
3568 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
, fsp
->fsp_name
->st
.st_ex_gid
);
3569 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
3570 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
3571 if (!NT_STATUS_IS_OK(status
)) {
3572 DEBUG(1,("failed to add MS NFS style ACE\n"));
3576 return NT_STATUS_OK
;
3579 static NTSTATUS
fruit_fset_nt_acl(vfs_handle_struct
*handle
,
3581 uint32_t security_info_sent
,
3582 const struct security_descriptor
*psd
)
3589 DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp
)));
3591 status
= check_ms_nfs(handle
, fsp
, psd
, &ms_nfs_mode
, &do_chmod
);
3592 if (!NT_STATUS_IS_OK(status
)) {
3593 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp
)));
3597 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
3598 if (!NT_STATUS_IS_OK(status
)) {
3599 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp
)));
3604 if (fsp
->fh
->fd
!= -1) {
3605 DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp
)));
3606 result
= SMB_VFS_FCHMOD(fsp
, ms_nfs_mode
);
3608 DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp
)));
3609 result
= SMB_VFS_CHMOD(fsp
->conn
,
3610 fsp
->fsp_name
->base_name
,
3615 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp
),
3616 result
, (unsigned)ms_nfs_mode
,
3618 status
= map_nt_error_from_unix(errno
);
3623 return NT_STATUS_OK
;
3626 struct fruit_copy_chunk_state
{
3627 struct vfs_handle_struct
*handle
;
3629 struct files_struct
*src_fsp
;
3630 struct files_struct
*dst_fsp
;
3634 static void fruit_copy_chunk_done(struct tevent_req
*subreq
);
3635 static struct tevent_req
*fruit_copy_chunk_send(struct vfs_handle_struct
*handle
,
3636 TALLOC_CTX
*mem_ctx
,
3637 struct tevent_context
*ev
,
3638 struct files_struct
*src_fsp
,
3640 struct files_struct
*dest_fsp
,
3644 struct tevent_req
*req
, *subreq
;
3645 struct fruit_copy_chunk_state
*fruit_copy_chunk_state
;
3647 struct fruit_config_data
*config
;
3648 off_t to_copy
= num
;
3650 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
3651 (uintmax_t)src_off
, (uintmax_t)dest_off
, (uintmax_t)num
));
3653 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3654 struct fruit_config_data
,
3657 req
= tevent_req_create(mem_ctx
, &fruit_copy_chunk_state
,
3658 struct fruit_copy_chunk_state
);
3662 fruit_copy_chunk_state
->handle
= handle
;
3663 fruit_copy_chunk_state
->src_fsp
= src_fsp
;
3664 fruit_copy_chunk_state
->dst_fsp
= dest_fsp
;
3667 * Check if this a OS X copyfile style copychunk request with
3668 * a requested chunk count of 0 that was translated to a
3669 * copy_chunk_send VFS call overloading the parameters src_off
3670 * = dest_off = num = 0.
3672 if ((src_off
== 0) && (dest_off
== 0) && (num
== 0) &&
3673 src_fsp
->aapl_copyfile_supported
&&
3674 dest_fsp
->aapl_copyfile_supported
)
3676 status
= vfs_stat_fsp(src_fsp
);
3677 if (tevent_req_nterror(req
, status
)) {
3678 return tevent_req_post(req
, ev
);
3681 to_copy
= src_fsp
->fsp_name
->st
.st_ex_size
;
3682 fruit_copy_chunk_state
->is_copyfile
= true;
3685 subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
3693 if (tevent_req_nomem(subreq
, req
)) {
3694 return tevent_req_post(req
, ev
);
3697 tevent_req_set_callback(subreq
, fruit_copy_chunk_done
, req
);
3701 static void fruit_copy_chunk_done(struct tevent_req
*subreq
)
3703 struct tevent_req
*req
= tevent_req_callback_data(
3704 subreq
, struct tevent_req
);
3705 struct fruit_copy_chunk_state
*state
= tevent_req_data(
3706 req
, struct fruit_copy_chunk_state
);
3708 unsigned int num_streams
= 0;
3709 struct stream_struct
*streams
= NULL
;
3711 struct smb_filename
*src_fname_tmp
= NULL
;
3712 struct smb_filename
*dst_fname_tmp
= NULL
;
3714 status
= SMB_VFS_NEXT_COPY_CHUNK_RECV(state
->handle
,
3717 TALLOC_FREE(subreq
);
3718 if (tevent_req_nterror(req
, status
)) {
3722 if (!state
->is_copyfile
) {
3723 tevent_req_done(req
);
3728 * Now copy all reamining streams. We know the share supports
3729 * streams, because we're in vfs_fruit. We don't do this async
3730 * because streams are few and small.
3732 status
= vfs_streaminfo(state
->handle
->conn
, NULL
,
3733 state
->src_fsp
->fsp_name
->base_name
,
3734 req
, &num_streams
, &streams
);
3735 if (tevent_req_nterror(req
, status
)) {
3739 if (num_streams
== 1) {
3740 /* There is always one stream, ::$DATA. */
3741 tevent_req_done(req
);
3745 for (i
= 0; i
< num_streams
; i
++) {
3746 DEBUG(10, ("%s: stream: '%s'/%ju\n",
3747 __func__
, streams
[i
].name
,
3748 (uintmax_t)streams
[i
].size
));
3750 src_fname_tmp
= synthetic_smb_fname(
3752 state
->src_fsp
->fsp_name
->base_name
,
3755 if (tevent_req_nomem(src_fname_tmp
, req
)) {
3759 if (is_ntfs_default_stream_smb_fname(src_fname_tmp
)) {
3760 TALLOC_FREE(src_fname_tmp
);
3764 dst_fname_tmp
= synthetic_smb_fname(
3766 state
->dst_fsp
->fsp_name
->base_name
,
3769 if (tevent_req_nomem(dst_fname_tmp
, req
)) {
3770 TALLOC_FREE(src_fname_tmp
);
3774 status
= copy_file(req
,
3775 state
->handle
->conn
,
3778 OPENX_FILE_CREATE_IF_NOT_EXIST
,
3780 if (!NT_STATUS_IS_OK(status
)) {
3781 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__
,
3782 smb_fname_str_dbg(src_fname_tmp
),
3783 smb_fname_str_dbg(dst_fname_tmp
),
3784 nt_errstr(status
)));
3785 TALLOC_FREE(src_fname_tmp
);
3786 TALLOC_FREE(dst_fname_tmp
);
3787 tevent_req_nterror(req
, status
);
3791 TALLOC_FREE(src_fname_tmp
);
3792 TALLOC_FREE(dst_fname_tmp
);
3795 TALLOC_FREE(streams
);
3796 TALLOC_FREE(src_fname_tmp
);
3797 TALLOC_FREE(dst_fname_tmp
);
3798 tevent_req_done(req
);
3801 static NTSTATUS
fruit_copy_chunk_recv(struct vfs_handle_struct
*handle
,
3802 struct tevent_req
*req
,
3805 struct fruit_copy_chunk_state
*fruit_copy_chunk_state
= tevent_req_data(
3806 req
, struct fruit_copy_chunk_state
);
3809 if (tevent_req_is_nterror(req
, &status
)) {
3810 DEBUG(1, ("server side copy chunk failed: %s\n",
3811 nt_errstr(status
)));
3813 tevent_req_received(req
);
3817 *copied
= fruit_copy_chunk_state
->copied
;
3818 tevent_req_received(req
);
3820 return NT_STATUS_OK
;
3823 static struct vfs_fn_pointers vfs_fruit_fns
= {
3824 .connect_fn
= fruit_connect
,
3826 /* File operations */
3827 .chmod_fn
= fruit_chmod
,
3828 .chown_fn
= fruit_chown
,
3829 .unlink_fn
= fruit_unlink
,
3830 .rename_fn
= fruit_rename
,
3831 .rmdir_fn
= fruit_rmdir
,
3832 .open_fn
= fruit_open
,
3833 .pread_fn
= fruit_pread
,
3834 .pwrite_fn
= fruit_pwrite
,
3835 .stat_fn
= fruit_stat
,
3836 .lstat_fn
= fruit_lstat
,
3837 .fstat_fn
= fruit_fstat
,
3838 .streaminfo_fn
= fruit_streaminfo
,
3839 .ntimes_fn
= fruit_ntimes
,
3840 .ftruncate_fn
= fruit_ftruncate
,
3841 .fallocate_fn
= fruit_fallocate
,
3842 .create_file_fn
= fruit_create_file
,
3843 .readdir_attr_fn
= fruit_readdir_attr
,
3844 .copy_chunk_send_fn
= fruit_copy_chunk_send
,
3845 .copy_chunk_recv_fn
= fruit_copy_chunk_recv
,
3847 /* NT ACL operations */
3848 .fget_nt_acl_fn
= fruit_fget_nt_acl
,
3849 .fset_nt_acl_fn
= fruit_fset_nt_acl
,
3852 NTSTATUS
vfs_fruit_init(void);
3853 NTSTATUS
vfs_fruit_init(void)
3855 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fruit",
3857 if (!NT_STATUS_IS_OK(ret
)) {
3861 vfs_fruit_debug_level
= debug_add_class("fruit");
3862 if (vfs_fruit_debug_level
== -1) {
3863 vfs_fruit_debug_level
= DBGC_VFS
;
3864 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3867 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3868 "vfs_fruit_init","fruit",vfs_fruit_debug_level
));