testprogs: Set functional domain level to 2003.
[Samba.git] / source3 / modules / vfs_fruit.c
blobebafe3a75dd7eeca4727b1593d1839ddecf13c40
1 /*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
34 * Enhanced OS X and Netatalk compatibility
35 * ========================================
37 * This modules takes advantage of vfs_streams_xattr and
38 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
39 * loaded in the correct order:
41 * vfs modules = catia fruit streams_xattr
43 * The module intercepts the OS X special streams "AFP_AfpInfo" and
44 * "AFP_Resource" and handles them in a special way. All other named
45 * streams are deferred to vfs_streams_xattr.
47 * The OS X client maps all NTFS illegal characters to the Unicode
48 * private range. This module optionally stores the charcters using
49 * their native ASCII encoding using vfs_catia. If you're not enabling
50 * this feature, you can skip catia from vfs modules.
52 * Finally, open modes are optionally checked against Netatalk AFP
53 * share modes.
55 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
56 * extended metadata for files and directories. This module optionally
57 * reads and stores this metadata in a way compatible with Netatalk 3
58 * which stores the metadata in an EA "org.netatalk.metadata". Cf
59 * source3/include/MacExtensions.h for a description of the binary
60 * blobs content.
62 * The "AFP_Resource" named stream may be arbitrarily large, thus it
63 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
64 * the only available filesystem where xattrs can be of any size and
65 * the OS supports using the file APIs for xattrs.
67 * The AFP_Resource stream is stored in an AppleDouble file prepending
68 * "._" to the filename. On Solaris with ZFS the stream is optionally
69 * stored in an EA "org.netatalk.ressource".
72 * Extended Attributes
73 * ===================
75 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
76 * other protocols you may want to adjust the xattr names the VFS
77 * module vfs_streams_xattr uses for storing ADS's. This defaults to
78 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
79 * these module parameters:
81 * streams_xattr:prefix = user.
82 * streams_xattr:store_stream_type = false
85 * TODO
86 * ====
88 * - log diagnostic if any needed VFS module is not loaded
89 * (eg with lp_vfs_objects())
90 * - add tests
93 static int vfs_fruit_debug_level = DBGC_VFS;
95 #undef DBGC_CLASS
96 #define DBGC_CLASS vfs_fruit_debug_level
98 #define FRUIT_PARAM_TYPE_NAME "fruit"
99 #define ADOUBLE_NAME_PREFIX "._"
102 * REVIEW:
103 * This is hokey, but what else can we do?
105 #if defined(HAVE_ATTROPEN) || defined(FREEBSD)
106 #define AFPINFO_EA_NETATALK "org.netatalk.Metadata"
107 #define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
108 #else
109 #define AFPINFO_EA_NETATALK "user.org.netatalk.Metadata"
110 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
111 #endif
113 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
115 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
116 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
117 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
118 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
120 struct fruit_config_data {
121 enum fruit_rsrc rsrc;
122 enum fruit_meta meta;
123 enum fruit_locking locking;
124 enum fruit_encoding encoding;
125 bool use_aapl;
126 bool readdir_attr_enabled;
127 bool unix_info_enabled;
130 * Additional undocumented options, all enabled by default,
131 * possibly useful for analyzing performance. The associated
132 * operations with each of them may be expensive, so having
133 * the chance to disable them individually gives a chance
134 * tweaking the setup for the particular usecase.
136 bool readdir_attr_rsize;
137 bool readdir_attr_finder_info;
138 bool readdir_attr_max_access;
141 static const struct enum_list fruit_rsrc[] = {
142 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
143 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
144 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
145 { -1, NULL}
148 static const struct enum_list fruit_meta[] = {
149 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
150 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
151 { -1, NULL}
154 static const struct enum_list fruit_locking[] = {
155 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
156 {FRUIT_LOCKING_NONE, "none"},
157 { -1, NULL}
160 static const struct enum_list fruit_encoding[] = {
161 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
162 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
163 { -1, NULL}
166 /*****************************************************************************
167 * Defines, functions and data structures that deal with AppleDouble
168 *****************************************************************************/
171 * There are two AppleDouble blobs we deal with:
173 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
174 * metadata in an xattr
176 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
177 * ._ files
179 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
181 /* Version info */
182 #define AD_VERSION2 0x00020000
183 #define AD_VERSION AD_VERSION2
186 * AppleDouble entry IDs.
188 #define ADEID_DFORK 1
189 #define ADEID_RFORK 2
190 #define ADEID_NAME 3
191 #define ADEID_COMMENT 4
192 #define ADEID_ICONBW 5
193 #define ADEID_ICONCOL 6
194 #define ADEID_FILEI 7
195 #define ADEID_FILEDATESI 8
196 #define ADEID_FINDERI 9
197 #define ADEID_MACFILEI 10
198 #define ADEID_PRODOSFILEI 11
199 #define ADEID_MSDOSFILEI 12
200 #define ADEID_SHORTNAME 13
201 #define ADEID_AFPFILEI 14
202 #define ADEID_DID 15
204 /* Private Netatalk entries */
205 #define ADEID_PRIVDEV 16
206 #define ADEID_PRIVINO 17
207 #define ADEID_PRIVSYN 18
208 #define ADEID_PRIVID 19
209 #define ADEID_MAX (ADEID_PRIVID + 1)
212 * These are the real ids for the private entries,
213 * as stored in the adouble file
215 #define AD_DEV 0x80444556
216 #define AD_INO 0x80494E4F
217 #define AD_SYN 0x8053594E
218 #define AD_ID 0x8053567E
220 /* Number of actually used entries */
221 #define ADEID_NUM_XATTR 8
222 #define ADEID_NUM_DOT_UND 2
223 #define ADEID_NUM_RSRC_XATTR 1
225 /* AppleDouble magic */
226 #define AD_APPLESINGLE_MAGIC 0x00051600
227 #define AD_APPLEDOUBLE_MAGIC 0x00051607
228 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
230 /* Sizes of relevant entry bits */
231 #define ADEDLEN_MAGIC 4
232 #define ADEDLEN_VERSION 4
233 #define ADEDLEN_FILLER 16
234 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
235 #define ADEDLEN_NENTRIES 2
236 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
237 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
238 #define AD_ENTRY_LEN_EID 4
239 #define AD_ENTRY_LEN_OFF 4
240 #define AD_ENTRY_LEN_LEN 4
241 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
243 /* Field widths */
244 #define ADEDLEN_NAME 255
245 #define ADEDLEN_COMMENT 200
246 #define ADEDLEN_FILEI 16
247 #define ADEDLEN_FINDERI 32
248 #define ADEDLEN_FILEDATESI 16
249 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
250 #define ADEDLEN_AFPFILEI 4
251 #define ADEDLEN_MACFILEI 4
252 #define ADEDLEN_PRODOSFILEI 8
253 #define ADEDLEN_MSDOSFILEI 2
254 #define ADEDLEN_DID 4
255 #define ADEDLEN_PRIVDEV 8
256 #define ADEDLEN_PRIVINO 8
257 #define ADEDLEN_PRIVSYN 8
258 #define ADEDLEN_PRIVID 4
260 /* Offsets */
261 #define ADEDOFF_MAGIC 0
262 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
263 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
264 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
266 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
267 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
268 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
269 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
270 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
271 ADEDLEN_FILEDATESI)
272 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
273 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
274 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
275 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
277 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
278 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
279 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
281 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
282 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
283 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
284 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
285 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
286 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
288 #if AD_DATASZ_XATTR != 402
289 #error bad size for AD_DATASZ_XATTR
290 #endif
292 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
293 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
294 ADEDLEN_FINDERI)
295 #if AD_DATASZ_DOT_UND != 82
296 #error bad size for AD_DATASZ_DOT_UND
297 #endif
300 * Sharemode locks fcntl() offsets
302 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
303 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
304 #else
305 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
306 #endif
307 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
309 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
310 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
311 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
312 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
313 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
314 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
315 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
316 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
317 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
318 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
320 /* Time stuff we overload the bits a little */
321 #define AD_DATE_CREATE 0
322 #define AD_DATE_MODIFY 4
323 #define AD_DATE_BACKUP 8
324 #define AD_DATE_ACCESS 12
325 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
326 AD_DATE_BACKUP | AD_DATE_ACCESS)
327 #define AD_DATE_UNIX (1 << 10)
328 #define AD_DATE_START 0x80000000
329 #define AD_DATE_DELTA 946684800
330 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
331 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
333 /* Accessor macros */
334 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
335 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
336 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
337 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
338 #define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
340 struct ad_entry {
341 size_t ade_off;
342 size_t ade_len;
345 struct adouble {
346 vfs_handle_struct *ad_handle;
347 files_struct *ad_fsp;
348 adouble_type_t ad_type;
349 uint32_t ad_magic;
350 uint32_t ad_version;
351 struct ad_entry ad_eid[ADEID_MAX];
352 char *ad_data;
355 struct ad_entry_order {
356 uint32_t id, offset, len;
359 /* Netatalk AppleDouble metadata xattr */
360 static const
361 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
362 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
363 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
364 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
365 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
366 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
367 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
368 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
369 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
370 {0, 0, 0}
373 /* AppleDouble ressource fork file (the ones prefixed by "._") */
374 static const
375 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
376 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
377 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
378 {0, 0, 0}
382 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
383 * isn't an AppleDouble file, it simply contains the ressource data,
384 * but in order to be able to use some API calls like ad_getentryoff()
385 * we build a fake/helper struct adouble with this entry order struct.
387 static const
388 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
389 {ADEID_RFORK, 0, 0},
390 {0, 0, 0}
393 /* Conversion from enumerated id to on-disk AppleDouble id */
394 #define AD_EID_DISK(a) (set_eid[a])
395 static const uint32_t set_eid[] = {
396 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
397 AD_DEV, AD_INO, AD_SYN, AD_ID
401 * Forward declarations
403 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
404 adouble_type_t type, files_struct *fsp);
405 static int ad_write(struct adouble *ad, const char *path);
406 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
409 * Get a date
411 static int ad_getdate(const struct adouble *ad,
412 unsigned int dateoff,
413 uint32_t *date)
415 bool xlate = (dateoff & AD_DATE_UNIX);
417 dateoff &= AD_DATE_MASK;
418 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
419 return -1;
422 if (dateoff > AD_DATE_ACCESS) {
423 return -1;
425 memcpy(date,
426 ad_entry(ad, ADEID_FILEDATESI) + dateoff,
427 sizeof(uint32_t));
429 if (xlate) {
430 *date = AD_DATE_TO_UNIX(*date);
432 return 0;
436 * Set a date
438 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
440 bool xlate = (dateoff & AD_DATE_UNIX);
442 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
443 return 0;
446 dateoff &= AD_DATE_MASK;
447 if (xlate) {
448 date = AD_DATE_FROM_UNIX(date);
451 if (dateoff > AD_DATE_ACCESS) {
452 return -1;
455 memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
457 return 0;
462 * Map on-disk AppleDouble id to enumerated id
464 static uint32_t get_eid(uint32_t eid)
466 if (eid <= 15) {
467 return eid;
470 switch (eid) {
471 case AD_DEV:
472 return ADEID_PRIVDEV;
473 case AD_INO:
474 return ADEID_PRIVINO;
475 case AD_SYN:
476 return ADEID_PRIVSYN;
477 case AD_ID:
478 return ADEID_PRIVID;
479 default:
480 break;
483 return 0;
487 * Pack AppleDouble structure into data buffer
489 static bool ad_pack(struct adouble *ad)
491 uint32_t eid;
492 uint16_t nent;
493 uint32_t bufsize;
494 uint32_t offset = 0;
496 bufsize = talloc_get_size(ad->ad_data);
498 if (offset + ADEDLEN_MAGIC < offset ||
499 offset + ADEDLEN_MAGIC >= bufsize) {
500 return false;
502 RSIVAL(ad->ad_data, offset, ad->ad_magic);
503 offset += ADEDLEN_MAGIC;
505 if (offset + ADEDLEN_VERSION < offset ||
506 offset + ADEDLEN_VERSION >= bufsize) {
507 return false;
509 RSIVAL(ad->ad_data, offset, ad->ad_version);
510 offset += ADEDLEN_VERSION;
512 if (offset + ADEDLEN_FILLER < offset ||
513 offset + ADEDLEN_FILLER >= bufsize) {
514 return false;
516 if (ad->ad_type == ADOUBLE_RSRC) {
517 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
519 offset += ADEDLEN_FILLER;
521 if (offset + ADEDLEN_NENTRIES < offset ||
522 offset + ADEDLEN_NENTRIES >= bufsize) {
523 return false;
525 offset += ADEDLEN_NENTRIES;
527 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
528 if ((ad->ad_eid[eid].ade_off == 0)) {
530 * ade_off is also used as indicator whether a
531 * specific entry is used or not
533 continue;
536 if (offset + AD_ENTRY_LEN_EID < offset ||
537 offset + AD_ENTRY_LEN_EID >= bufsize) {
538 return false;
540 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
541 offset += AD_ENTRY_LEN_EID;
543 if (offset + AD_ENTRY_LEN_OFF < offset ||
544 offset + AD_ENTRY_LEN_OFF >= bufsize) {
545 return false;
547 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
548 offset += AD_ENTRY_LEN_OFF;
550 if (offset + AD_ENTRY_LEN_LEN < offset ||
551 offset + AD_ENTRY_LEN_LEN >= bufsize) {
552 return false;
554 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
555 offset += AD_ENTRY_LEN_LEN;
557 nent++;
560 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
561 return false;
563 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
565 return 0;
569 * Unpack an AppleDouble blob into a struct adoble
571 static bool ad_unpack(struct adouble *ad, const int nentries)
573 size_t bufsize = talloc_get_size(ad->ad_data);
574 int adentries, i;
575 uint32_t eid, len, off;
578 * The size of the buffer ad->ad_data is checked when read, so
579 * we wouldn't have to check our own offsets, a few extra
580 * checks won't hurt though. We have to check the offsets we
581 * read from the buffer anyway.
584 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
585 DEBUG(1, ("bad size\n"));
586 return false;
589 ad->ad_magic = RIVAL(ad->ad_data, 0);
590 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
591 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
592 DEBUG(1, ("wrong magic or version\n"));
593 return false;
596 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
597 if (adentries != nentries) {
598 DEBUG(1, ("invalid number of entries: %d\n", adentries));
599 return false;
602 /* now, read in the entry bits */
603 for (i = 0; i < adentries; i++) {
604 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
605 eid = get_eid(eid);
606 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
607 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
609 if (!eid || eid > ADEID_MAX) {
610 DEBUG(1, ("bogus eid %d\n", eid));
611 return false;
614 if ((off > bufsize) && (eid != ADEID_RFORK)) {
615 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
616 eid, off, len));
617 return false;
619 if ((eid != ADEID_RFORK) &&
620 (eid != ADEID_FINDERI) &&
621 ((off + len) > bufsize)) {
622 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
623 eid, off, len));
624 return false;
627 ad->ad_eid[eid].ade_off = off;
628 ad->ad_eid[eid].ade_len = len;
631 return true;
635 * Convert from Apple's ._ file to Netatalk
637 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
638 * bytes containing packed xattrs. Netatalk can't deal with that, so
639 * we simply discard the packed xattrs.
641 * @return -1 in case an error occured, 0 if no conversion was done, 1
642 * otherwise
644 static int ad_convert(struct adouble *ad, int fd)
646 int rc = 0;
647 char *map = MAP_FAILED;
648 size_t origlen;
650 origlen = ad_getentryoff(ad, ADEID_RFORK) +
651 ad_getentrylen(ad, ADEID_RFORK);
653 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
654 map = mmap(NULL, origlen, PROT_WRITE, MAP_SHARED, fd, 0);
655 if (map == MAP_FAILED) {
656 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
657 rc = -1;
658 goto exit;
661 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
662 map + ad_getentryoff(ad, ADEID_RFORK),
663 ad_getentrylen(ad, ADEID_RFORK));
665 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
666 ad_setentryoff(ad, ADEID_RFORK,
667 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
670 * FIXME: direct ftruncate(), but we don't have a fsp for the
671 * VFS call
673 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
674 + ad_getentrylen(ad, ADEID_RFORK));
676 exit:
677 if (map != MAP_FAILED) {
678 munmap(map, origlen);
680 return rc;
684 * Read and parse Netatalk AppleDouble metadata xattr
686 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
688 int rc = 0;
689 ssize_t ealen;
690 bool ok;
692 DEBUG(10, ("reading meta xattr for %s\n", path));
694 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
695 AFPINFO_EA_NETATALK, ad->ad_data,
696 AD_DATASZ_XATTR);
697 if (ealen == -1) {
698 switch (errno) {
699 case ENOATTR:
700 case ENOENT:
701 if (errno == ENOATTR) {
702 errno = ENOENT;
704 rc = -1;
705 goto exit;
706 default:
707 DEBUG(2, ("error reading meta xattr: %s\n",
708 strerror(errno)));
709 rc = -1;
710 goto exit;
713 if (ealen != AD_DATASZ_XATTR) {
714 DEBUG(2, ("bad size %zd\n", ealen));
715 errno = EINVAL;
716 rc = -1;
717 goto exit;
720 /* Now parse entries */
721 ok = ad_unpack(ad, ADEID_NUM_XATTR);
722 if (!ok) {
723 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
724 errno = EINVAL;
725 rc = -1;
726 goto exit;
729 if (!ad_getentryoff(ad, ADEID_FINDERI)
730 || !ad_getentryoff(ad, ADEID_COMMENT)
731 || !ad_getentryoff(ad, ADEID_FILEDATESI)
732 || !ad_getentryoff(ad, ADEID_AFPFILEI)
733 || !ad_getentryoff(ad, ADEID_PRIVDEV)
734 || !ad_getentryoff(ad, ADEID_PRIVINO)
735 || !ad_getentryoff(ad, ADEID_PRIVSYN)
736 || !ad_getentryoff(ad, ADEID_PRIVID)) {
737 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
738 errno = EINVAL;
739 rc = -1;
740 goto exit;
743 exit:
744 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
746 if (rc != 0) {
747 ealen = -1;
748 if (errno == EINVAL) {
749 become_root();
750 removexattr(path, AFPINFO_EA_NETATALK);
751 unbecome_root();
752 errno = ENOENT;
755 return ealen;
759 * Read and parse resource fork, either ._ AppleDouble file or xattr
761 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
763 struct fruit_config_data *config = NULL;
764 int fd = -1;
765 int rc = 0;
766 ssize_t len;
767 char *adpath = NULL;
768 bool opened = false;
769 int mode;
770 struct adouble *meta_ad = NULL;
771 SMB_STRUCT_STAT sbuf;
772 bool ok;
773 int saved_errno = 0;
775 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
776 struct fruit_config_data, return -1);
778 /* Try rw first so we can use the fd in ad_convert() */
779 mode = O_RDWR;
781 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
782 fd = ad->ad_fsp->fh->fd;
783 } else {
784 if (config->rsrc == FRUIT_RSRC_XATTR) {
785 adpath = talloc_strdup(talloc_tos(), path);
786 } else {
787 rc = adouble_path(talloc_tos(), path, &adpath);
788 if (rc != 0) {
789 goto exit;
793 retry:
794 if (config->rsrc == FRUIT_RSRC_XATTR) {
795 #ifndef HAVE_ATTROPEN
796 errno = ENOSYS;
797 rc = -1;
798 goto exit;
799 #else
800 /* FIXME: direct Solaris xattr syscall */
801 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
802 mode, 0);
803 #endif
804 } else {
805 /* FIXME: direct open(), don't have an fsp */
806 fd = open(adpath, mode);
809 if (fd == -1) {
810 switch (errno) {
811 case EROFS:
812 case EACCES:
813 if (mode == O_RDWR) {
814 mode = O_RDONLY;
815 goto retry;
817 /* fall through ... */
818 default:
819 DEBUG(2, ("open AppleDouble: %s, %s\n",
820 adpath, strerror(errno)));
821 rc = -1;
822 goto exit;
825 opened = true;
828 if (config->rsrc == FRUIT_RSRC_XATTR) {
829 /* FIXME: direct sys_fstat(), don't have an fsp */
830 rc = sys_fstat(
831 fd, &sbuf,
832 lp_fake_directory_create_times(
833 SNUM(ad->ad_handle->conn)));
834 if (rc != 0) {
835 goto exit;
837 ad_setentrylen(ad, ADEID_RFORK, sbuf.st_ex_size);
838 } else {
839 /* FIXME: direct sys_pread(), don't have an fsp */
840 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
841 if (len != AD_DATASZ_DOT_UND) {
842 DEBUG(2, ("%s: bad size: %zd\n",
843 strerror(errno), len));
844 rc = -1;
845 goto exit;
848 /* Now parse entries */
849 ok = ad_unpack(ad, ADEID_NUM_DOT_UND);
850 if (!ok) {
851 DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
852 errno = EINVAL;
853 rc = -1;
854 goto exit;
857 if ((ad_getentryoff(ad, ADEID_FINDERI)
858 != ADEDOFF_FINDERI_DOT_UND)
859 || (ad_getentrylen(ad, ADEID_FINDERI)
860 < ADEDLEN_FINDERI)
861 || (ad_getentryoff(ad, ADEID_RFORK)
862 < ADEDOFF_RFORK_DOT_UND)) {
863 DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
864 errno = EINVAL;
865 rc = -1;
866 goto exit;
869 if ((mode == O_RDWR)
870 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
871 rc = ad_convert(ad, fd);
872 if (rc != 0) {
873 rc = -1;
874 goto exit;
877 * Can't use ad_write() because we might not have a fsp
879 rc = ad_pack(ad);
880 if (rc != 0) {
881 goto exit;
883 /* FIXME: direct sys_pwrite(), don't have an fsp */
884 len = sys_pwrite(fd, ad->ad_data,
885 AD_DATASZ_DOT_UND, 0);
886 if (len != AD_DATASZ_DOT_UND) {
887 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
888 rc = -1;
889 goto exit;
892 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
893 ADOUBLE_META, NULL);
894 if (meta_ad == NULL) {
895 rc = -1;
896 goto exit;
899 memcpy(ad_entry(meta_ad, ADEID_FINDERI),
900 ad_entry(ad, ADEID_FINDERI),
901 ADEDLEN_FINDERI);
903 rc = ad_write(meta_ad, path);
904 if (rc != 0) {
905 rc = -1;
906 goto exit;
911 DEBUG(10, ("opened AppleDouble: %s\n", path));
913 exit:
914 if (rc != 0) {
915 saved_errno = errno;
916 len = -1;
918 if (opened && fd != -1) {
919 close(fd);
921 TALLOC_FREE(adpath);
922 TALLOC_FREE(meta_ad);
923 if (rc != 0) {
924 errno = saved_errno;
926 return len;
930 * Read and unpack an AppleDouble metadata xattr or resource
932 static ssize_t ad_read(struct adouble *ad, const char *path)
934 switch (ad->ad_type) {
935 case ADOUBLE_META:
936 return ad_header_read_meta(ad, path);
937 case ADOUBLE_RSRC:
938 return ad_header_read_rsrc(ad, path);
939 default:
940 return -1;
945 * Allocate a struct adouble without initialiing it
947 * The struct is either hang of the fsp extension context or if fsp is
948 * NULL from ctx.
950 * @param[in] ctx talloc context
951 * @param[in] handle vfs handle
952 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
954 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
955 * added as an fsp extension
957 * @return adouble handle
959 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
960 adouble_type_t type, files_struct *fsp)
962 int rc = 0;
963 size_t adsize = 0;
964 struct adouble *ad;
965 struct fruit_config_data *config;
967 SMB_VFS_HANDLE_GET_DATA(handle, config,
968 struct fruit_config_data, return NULL);
970 switch (type) {
971 case ADOUBLE_META:
972 adsize = AD_DATASZ_XATTR;
973 break;
974 case ADOUBLE_RSRC:
975 if (config->rsrc == FRUIT_RSRC_ADFILE) {
976 adsize = AD_DATASZ_DOT_UND;
978 break;
979 default:
980 return NULL;
983 if (!fsp) {
984 ad = talloc_zero(ctx, struct adouble);
985 if (ad == NULL) {
986 rc = -1;
987 goto exit;
989 if (adsize) {
990 ad->ad_data = talloc_zero_array(ad, char, adsize);
992 } else {
993 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
994 struct adouble,
995 NULL);
996 if (ad == NULL) {
997 rc = -1;
998 goto exit;
1000 if (adsize) {
1001 ad->ad_data = talloc_zero_array(
1002 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1003 char, adsize);
1005 ad->ad_fsp = fsp;
1008 if (adsize && ad->ad_data == NULL) {
1009 rc = -1;
1010 goto exit;
1012 ad->ad_handle = handle;
1013 ad->ad_type = type;
1014 ad->ad_magic = AD_MAGIC;
1015 ad->ad_version = AD_VERSION;
1017 exit:
1018 if (rc != 0) {
1019 TALLOC_FREE(ad);
1021 return ad;
1025 * Allocate and initialize a new struct adouble
1027 * @param[in] ctx talloc context
1028 * @param[in] handle vfs handle
1029 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1030 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1032 * @return adouble handle, initialized
1034 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1035 adouble_type_t type, files_struct *fsp)
1037 int rc = 0;
1038 const struct ad_entry_order *eid;
1039 struct adouble *ad = NULL;
1040 struct fruit_config_data *config;
1041 time_t t = time(NULL);
1043 SMB_VFS_HANDLE_GET_DATA(handle, config,
1044 struct fruit_config_data, return NULL);
1046 switch (type) {
1047 case ADOUBLE_META:
1048 eid = entry_order_meta_xattr;
1049 break;
1050 case ADOUBLE_RSRC:
1051 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1052 eid = entry_order_dot_und;
1053 } else {
1054 eid = entry_order_rsrc_xattr;
1056 break;
1057 default:
1058 return NULL;
1061 ad = ad_alloc(ctx, handle, type, fsp);
1062 if (ad == NULL) {
1063 return NULL;
1066 while (eid->id) {
1067 ad->ad_eid[eid->id].ade_off = eid->offset;
1068 ad->ad_eid[eid->id].ade_len = eid->len;
1069 eid++;
1072 /* put something sane in the date fields */
1073 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1074 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1075 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1076 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1078 if (rc != 0) {
1079 TALLOC_FREE(ad);
1081 return ad;
1085 * Return AppleDouble data for a file
1087 * @param[in] ctx talloc context
1088 * @param[in] handle vfs handle
1089 * @param[in] path pathname to file or directory
1090 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1092 * @return talloced struct adouble or NULL on error
1094 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1095 const char *path, adouble_type_t type)
1097 int rc = 0;
1098 ssize_t len;
1099 struct adouble *ad = NULL;
1101 DEBUG(10, ("ad_get(%s) called for %s\n",
1102 type == ADOUBLE_META ? "meta" : "rsrc", path));
1104 ad = ad_alloc(ctx, handle, type, NULL);
1105 if (ad == NULL) {
1106 rc = -1;
1107 goto exit;
1110 len = ad_read(ad, path);
1111 if (len == -1) {
1112 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1113 rc = -1;
1114 goto exit;
1117 exit:
1118 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1119 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1121 if (rc != 0) {
1122 TALLOC_FREE(ad);
1124 return ad;
1128 * Set AppleDouble metadata on a file or directory
1130 * @param[in] ad adouble handle
1132 * @param[in] path pathname to file or directory, may be NULL for a
1133 * resource fork
1135 * @return status code, 0 means success
1137 static int ad_write(struct adouble *ad, const char *path)
1139 int rc = 0;
1140 ssize_t len;
1142 rc = ad_pack(ad);
1143 if (rc != 0) {
1144 goto exit;
1147 switch (ad->ad_type) {
1148 case ADOUBLE_META:
1149 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1150 AFPINFO_EA_NETATALK, ad->ad_data,
1151 AD_DATASZ_XATTR, 0);
1152 break;
1153 case ADOUBLE_RSRC:
1154 if ((ad->ad_fsp == NULL)
1155 || (ad->ad_fsp->fh == NULL)
1156 || (ad->ad_fsp->fh->fd == -1)) {
1157 rc = -1;
1158 goto exit;
1160 /* FIXME: direct sys_pwrite(), don't have an fsp */
1161 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1162 talloc_get_size(ad->ad_data), 0);
1163 if (len != talloc_get_size(ad->ad_data)) {
1164 DEBUG(1, ("short write on %s: %zd",
1165 fsp_str_dbg(ad->ad_fsp), len));
1166 rc = -1;
1167 goto exit;
1169 break;
1170 default:
1171 return -1;
1173 exit:
1174 return rc;
1177 /*****************************************************************************
1178 * Helper functions
1179 *****************************************************************************/
1181 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1183 if (strncasecmp_m(smb_fname->stream_name,
1184 AFPINFO_STREAM_NAME,
1185 strlen(AFPINFO_STREAM_NAME)) == 0) {
1186 return true;
1188 return false;
1191 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1193 if (strncasecmp_m(smb_fname->stream_name,
1194 AFPRESOURCE_STREAM_NAME,
1195 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1196 return true;
1198 return false;
1202 * Test whether stream is an Apple stream, not used atm
1204 #if 0
1205 static bool is_apple_stream(const struct smb_filename *smb_fname)
1207 if (is_afpinfo_stream(smb_fname)) {
1208 return true;
1210 if (is_afpresource_stream(smb_fname)) {
1211 return true;
1213 return false;
1215 #endif
1218 * Initialize config struct from our smb.conf config parameters
1220 static int init_fruit_config(vfs_handle_struct *handle)
1222 struct fruit_config_data *config;
1223 int enumval;
1225 config = talloc_zero(handle->conn, struct fruit_config_data);
1226 if (!config) {
1227 DEBUG(1, ("talloc_zero() failed\n"));
1228 errno = ENOMEM;
1229 return -1;
1232 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1233 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1234 if (enumval == -1) {
1235 DEBUG(1, ("value for %s: ressource type unknown\n",
1236 FRUIT_PARAM_TYPE_NAME));
1237 return -1;
1239 config->rsrc = (enum fruit_rsrc)enumval;
1241 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1242 "metadata", fruit_meta, FRUIT_META_NETATALK);
1243 if (enumval == -1) {
1244 DEBUG(1, ("value for %s: metadata type unknown\n",
1245 FRUIT_PARAM_TYPE_NAME));
1246 return -1;
1248 config->meta = (enum fruit_meta)enumval;
1250 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1251 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1252 if (enumval == -1) {
1253 DEBUG(1, ("value for %s: locking type unknown\n",
1254 FRUIT_PARAM_TYPE_NAME));
1255 return -1;
1257 config->locking = (enum fruit_locking)enumval;
1259 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1260 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1261 if (enumval == -1) {
1262 DEBUG(1, ("value for %s: encoding type unknown\n",
1263 FRUIT_PARAM_TYPE_NAME));
1264 return -1;
1266 config->encoding = (enum fruit_encoding)enumval;
1268 if (lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "aapl", true)) {
1269 config->use_aapl = true;
1272 if (lp_parm_bool(SNUM(handle->conn),
1273 "readdir_attr", "aapl_rsize", true)) {
1274 config->readdir_attr_rsize = true;
1277 if (lp_parm_bool(SNUM(handle->conn),
1278 "readdir_attr", "aapl_finder_info", true)) {
1279 config->readdir_attr_finder_info = true;
1282 if (lp_parm_bool(SNUM(handle->conn),
1283 "readdir_attr", "aapl_max_access", true)) {
1284 config->readdir_attr_max_access = true;
1287 SMB_VFS_HANDLE_SET_DATA(handle, config,
1288 NULL, struct fruit_config_data,
1289 return -1);
1291 return 0;
1295 * Prepend "._" to a basename
1297 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1299 char *parent;
1300 const char *base;
1302 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1303 return -1;
1306 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1307 if (*path_out == NULL) {
1308 return -1;
1311 return 0;
1315 * Allocate and initialize an AfpInfo struct
1317 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1319 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1320 if (ai == NULL) {
1321 return NULL;
1323 ai->afpi_Signature = AFP_Signature;
1324 ai->afpi_Version = AFP_Version;
1325 ai->afpi_BackupTime = AD_DATE_START;
1326 return ai;
1330 * Pack an AfpInfo struct into a buffer
1332 * Buffer size must be at least AFP_INFO_SIZE
1333 * Returns size of packed buffer
1335 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1337 memset(buf, 0, AFP_INFO_SIZE);
1339 RSIVAL(buf, 0, ai->afpi_Signature);
1340 RSIVAL(buf, 4, ai->afpi_Version);
1341 RSIVAL(buf, 12, ai->afpi_BackupTime);
1342 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1344 return AFP_INFO_SIZE;
1348 * Unpack a buffer into a AfpInfo structure
1350 * Buffer size must be at least AFP_INFO_SIZE
1351 * Returns allocated AfpInfo struct
1353 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1355 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1356 if (ai == NULL) {
1357 return NULL;
1360 ai->afpi_Signature = RIVAL(data, 0);
1361 ai->afpi_Version = RIVAL(data, 4);
1362 ai->afpi_BackupTime = RIVAL(data, 12);
1363 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1364 sizeof(ai->afpi_FinderInfo));
1366 if (ai->afpi_Signature != AFP_Signature
1367 || ai->afpi_Version != AFP_Version) {
1368 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1369 TALLOC_FREE(ai);
1372 return ai;
1376 * Fake an inode number from the md5 hash of the (xattr) name
1378 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1380 MD5_CTX ctx;
1381 unsigned char hash[16];
1382 SMB_INO_T result;
1383 char *upper_sname;
1385 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1386 SMB_ASSERT(upper_sname != NULL);
1388 MD5Init(&ctx);
1389 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1390 sizeof(sbuf->st_ex_dev));
1391 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1392 sizeof(sbuf->st_ex_ino));
1393 MD5Update(&ctx, (unsigned char *)upper_sname,
1394 talloc_get_size(upper_sname)-1);
1395 MD5Final(hash, &ctx);
1397 TALLOC_FREE(upper_sname);
1399 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1400 memcpy(&result, hash, sizeof(result));
1402 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1403 sname, (unsigned long long)result));
1405 return result;
1409 * Ensure ad_fsp is still valid
1411 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1413 if (ad->ad_fsp == fsp) {
1414 return true;
1416 ad->ad_fsp = fsp;
1418 return true;
1421 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1422 struct stream_struct **streams,
1423 const char *name, off_t size,
1424 off_t alloc_size)
1426 struct stream_struct *tmp;
1428 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1429 (*num_streams)+1);
1430 if (tmp == NULL) {
1431 return false;
1434 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1435 if (tmp[*num_streams].name == NULL) {
1436 return false;
1439 tmp[*num_streams].size = size;
1440 tmp[*num_streams].alloc_size = alloc_size;
1442 *streams = tmp;
1443 *num_streams += 1;
1444 return true;
1447 static bool empty_finderinfo(const struct adouble *ad)
1450 char emptybuf[ADEDLEN_FINDERI] = {0};
1451 if (memcmp(emptybuf,
1452 ad_entry(ad, ADEID_FINDERI),
1453 ADEDLEN_FINDERI) == 0) {
1454 return true;
1456 return false;
1460 * Update btime with btime from Netatalk
1462 static void update_btime(vfs_handle_struct *handle,
1463 struct smb_filename *smb_fname)
1465 uint32_t t;
1466 struct timespec creation_time = {0};
1467 struct adouble *ad;
1469 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1470 if (ad == NULL) {
1471 return;
1473 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1474 TALLOC_FREE(ad);
1475 return;
1477 TALLOC_FREE(ad);
1479 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1480 update_stat_ex_create_time(&smb_fname->st, creation_time);
1482 return;
1486 * Map an access mask to a Netatalk single byte byte range lock
1488 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1489 uint32_t access_mask)
1491 off_t offset;
1493 switch (access_mask) {
1494 case FILE_READ_DATA:
1495 offset = AD_FILELOCK_OPEN_RD;
1496 break;
1498 case FILE_WRITE_DATA:
1499 case FILE_APPEND_DATA:
1500 offset = AD_FILELOCK_OPEN_WR;
1501 break;
1503 default:
1504 offset = AD_FILELOCK_OPEN_NONE;
1505 break;
1508 if (fork_type == APPLE_FORK_RSRC) {
1509 if (offset == AD_FILELOCK_OPEN_NONE) {
1510 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1511 } else {
1512 offset += 2;
1516 return offset;
1520 * Map a deny mode to a Netatalk brl
1522 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1523 uint32_t deny_mode)
1525 off_t offset;
1527 switch (deny_mode) {
1528 case DENY_READ:
1529 offset = AD_FILELOCK_DENY_RD;
1530 break;
1532 case DENY_WRITE:
1533 offset = AD_FILELOCK_DENY_WR;
1534 break;
1536 default:
1537 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1540 if (fork_type == APPLE_FORK_RSRC) {
1541 offset += 2;
1544 return offset;
1548 * Call fcntl() with an exclusive F_GETLK request in order to
1549 * determine if there's an exisiting shared lock
1551 * @return true if the requested lock was found or any error occured
1552 * false if the lock was not found
1554 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1556 bool result;
1557 off_t offset = in_offset;
1558 off_t len = 1;
1559 int type = F_WRLCK;
1560 pid_t pid;
1562 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1563 if (result == false) {
1564 return true;
1567 if (type != F_UNLCK) {
1568 return true;
1571 return false;
1574 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1575 files_struct *fsp,
1576 uint32_t access_mask,
1577 uint32_t deny_mode)
1579 NTSTATUS status = NT_STATUS_OK;
1580 struct byte_range_lock *br_lck = NULL;
1581 bool open_for_reading, open_for_writing, deny_read, deny_write;
1582 off_t off;
1584 /* FIXME: hardcoded data fork, add resource fork */
1585 enum apple_fork fork_type = APPLE_FORK_DATA;
1587 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1588 fsp_str_dbg(fsp),
1589 access_mask & FILE_READ_DATA ? "READ" :"-",
1590 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1591 deny_mode & DENY_READ ? "DENY_READ" : "-",
1592 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1595 * Check read access and deny read mode
1597 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1598 /* Check access */
1599 open_for_reading = test_netatalk_lock(
1600 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1602 deny_read = test_netatalk_lock(
1603 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1605 DEBUG(10, ("read: %s, deny_write: %s\n",
1606 open_for_reading == true ? "yes" : "no",
1607 deny_read == true ? "yes" : "no"));
1609 if (((access_mask & FILE_READ_DATA) && deny_read)
1610 || ((deny_mode & DENY_READ) && open_for_reading)) {
1611 return NT_STATUS_SHARING_VIOLATION;
1614 /* Set locks */
1615 if (access_mask & FILE_READ_DATA) {
1616 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1617 br_lck = do_lock(
1618 handle->conn->sconn->msg_ctx, fsp,
1619 fsp->op->global->open_persistent_id, 1, off,
1620 READ_LOCK, POSIX_LOCK, false,
1621 &status, NULL);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 return status;
1626 TALLOC_FREE(br_lck);
1629 if (deny_mode & DENY_READ) {
1630 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1631 br_lck = do_lock(
1632 handle->conn->sconn->msg_ctx, fsp,
1633 fsp->op->global->open_persistent_id, 1, off,
1634 READ_LOCK, POSIX_LOCK, false,
1635 &status, NULL);
1637 if (!NT_STATUS_IS_OK(status)) {
1638 return status;
1640 TALLOC_FREE(br_lck);
1645 * Check write access and deny write mode
1647 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1648 /* Check access */
1649 open_for_writing = test_netatalk_lock(
1650 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1652 deny_write = test_netatalk_lock(
1653 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1655 DEBUG(10, ("write: %s, deny_write: %s\n",
1656 open_for_writing == true ? "yes" : "no",
1657 deny_write == true ? "yes" : "no"));
1659 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1660 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1661 return NT_STATUS_SHARING_VIOLATION;
1664 /* Set locks */
1665 if (access_mask & FILE_WRITE_DATA) {
1666 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1667 br_lck = do_lock(
1668 handle->conn->sconn->msg_ctx, fsp,
1669 fsp->op->global->open_persistent_id, 1, off,
1670 READ_LOCK, POSIX_LOCK, false,
1671 &status, NULL);
1673 if (!NT_STATUS_IS_OK(status)) {
1674 return status;
1676 TALLOC_FREE(br_lck);
1679 if (deny_mode & DENY_WRITE) {
1680 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1681 br_lck = do_lock(
1682 handle->conn->sconn->msg_ctx, fsp,
1683 fsp->op->global->open_persistent_id, 1, off,
1684 READ_LOCK, POSIX_LOCK, false,
1685 &status, NULL);
1687 if (!NT_STATUS_IS_OK(status)) {
1688 return status;
1690 TALLOC_FREE(br_lck);
1694 TALLOC_FREE(br_lck);
1696 return status;
1699 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1700 struct smb_request *req,
1701 const struct smb2_create_blobs *in_context_blobs,
1702 struct smb2_create_blobs *out_context_blobs)
1704 struct fruit_config_data *config;
1705 NTSTATUS status;
1706 struct smb2_create_blob *aapl = NULL;
1707 uint32_t cmd;
1708 bool ok;
1709 uint8_t p[16];
1710 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1711 uint64_t req_bitmap, client_caps;
1712 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1713 smb_ucs2_t *model;
1714 size_t modellen;
1716 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1717 return NT_STATUS_UNSUCCESSFUL);
1719 if (!config->use_aapl
1720 || in_context_blobs == NULL
1721 || out_context_blobs == NULL) {
1722 return NT_STATUS_OK;
1725 aapl = smb2_create_blob_find(in_context_blobs,
1726 SMB2_CREATE_TAG_AAPL);
1727 if (aapl == NULL) {
1728 return NT_STATUS_OK;
1731 if (aapl->data.length != 24) {
1732 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1733 (uintmax_t)aapl->data.length));
1734 return NT_STATUS_INVALID_PARAMETER;
1737 cmd = IVAL(aapl->data.data, 0);
1738 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1739 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1740 return NT_STATUS_INVALID_PARAMETER;
1743 req_bitmap = BVAL(aapl->data.data, 8);
1744 client_caps = BVAL(aapl->data.data, 16);
1746 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1747 SIVAL(p, 4, 0);
1748 SBVAL(p, 8, req_bitmap);
1749 ok = data_blob_append(req, &blob, p, 16);
1750 if (!ok) {
1751 return NT_STATUS_UNSUCCESSFUL;
1754 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1755 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1756 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1757 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1758 config->readdir_attr_enabled = true;
1762 * The client doesn't set the flag, so we can't check
1763 * for it and just set it unconditionally
1765 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1766 config->unix_info_enabled = true;
1768 SBVAL(p, 0, server_caps);
1769 ok = data_blob_append(req, &blob, p, 8);
1770 if (!ok) {
1771 return NT_STATUS_UNSUCCESSFUL;
1775 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1776 SBVAL(p, 0,
1777 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1778 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1779 ok = data_blob_append(req, &blob, p, 8);
1780 if (!ok) {
1781 return NT_STATUS_UNSUCCESSFUL;
1785 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1786 ok = convert_string_talloc(req,
1787 CH_UNIX, CH_UTF16LE,
1788 "Samba", strlen("Samba"),
1789 &model, &modellen);
1790 if (!ok) {
1791 return NT_STATUS_UNSUCCESSFUL;
1794 SIVAL(p, 0, 0);
1795 SIVAL(p + 4, 0, modellen);
1796 ok = data_blob_append(req, &blob, p, 8);
1797 if (!ok) {
1798 talloc_free(model);
1799 return NT_STATUS_UNSUCCESSFUL;
1802 ok = data_blob_append(req, &blob, model, modellen);
1803 talloc_free(model);
1804 if (!ok) {
1805 return NT_STATUS_UNSUCCESSFUL;
1809 status = smb2_create_blob_add(out_context_blobs,
1810 out_context_blobs,
1811 SMB2_CREATE_TAG_AAPL,
1812 blob);
1814 return status;
1817 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1818 const struct smb_filename *smb_fname,
1819 struct readdir_attr_data *attr_data)
1821 NTSTATUS status = NT_STATUS_OK;
1822 uint32_t date_added;
1823 struct adouble *ad = NULL;
1824 struct fruit_config_data *config = NULL;
1826 SMB_VFS_HANDLE_GET_DATA(handle, config,
1827 struct fruit_config_data,
1828 return NT_STATUS_UNSUCCESSFUL);
1831 /* Ensure we return a default value in the creation_date field */
1832 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1835 * Resource fork length
1838 if (config->readdir_attr_rsize) {
1839 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1840 ADOUBLE_RSRC);
1841 if (ad) {
1842 attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1843 ad, ADEID_RFORK);
1844 TALLOC_FREE(ad);
1849 * FinderInfo
1852 if (config->readdir_attr_finder_info) {
1853 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1854 ADOUBLE_META);
1855 if (ad) {
1856 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1857 /* finder_type */
1858 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1859 ad_entry(ad, ADEID_FINDERI), 4);
1861 /* finder_creator */
1862 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1863 ad_entry(ad, ADEID_FINDERI) + 4, 4);
1866 /* finder_flags */
1867 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1868 ad_entry(ad, ADEID_FINDERI) + 8, 2);
1870 /* finder_ext_flags */
1871 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1872 ad_entry(ad, ADEID_FINDERI) + 24, 2);
1874 /* creation date */
1875 date_added = convert_time_t_to_uint32_t(
1876 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1877 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1879 TALLOC_FREE(ad);
1883 TALLOC_FREE(ad);
1884 return status;
1887 /* Search MS NFS style ACE with UNIX mode */
1888 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1889 files_struct *fsp,
1890 const struct security_descriptor *psd,
1891 mode_t *pmode,
1892 bool *pdo_chmod)
1894 int i;
1895 struct fruit_config_data *config = NULL;
1897 *pdo_chmod = false;
1899 SMB_VFS_HANDLE_GET_DATA(handle, config,
1900 struct fruit_config_data,
1901 return NT_STATUS_UNSUCCESSFUL);
1903 if (psd->dacl == NULL || !config->unix_info_enabled) {
1904 return NT_STATUS_OK;
1907 for (i = 0; i < psd->dacl->num_aces; i++) {
1908 if (dom_sid_compare_domain(
1909 &global_sid_Unix_NFS_Mode,
1910 &psd->dacl->aces[i].trustee) == 0) {
1911 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1912 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1913 *pdo_chmod = true;
1915 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1916 fsp_str_dbg(fsp), *pmode));
1917 break;
1921 return NT_STATUS_OK;
1924 /****************************************************************************
1925 * VFS ops
1926 ****************************************************************************/
1928 static int fruit_connect(vfs_handle_struct *handle,
1929 const char *service,
1930 const char *user)
1932 int rc;
1933 char *list = NULL, *newlist = NULL;
1934 struct fruit_config_data *config;
1936 DEBUG(10, ("fruit_connect\n"));
1938 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1939 if (rc < 0) {
1940 return rc;
1943 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
1945 if (list) {
1946 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1947 newlist = talloc_asprintf(
1948 list,
1949 "%s/" ADOUBLE_NAME_PREFIX "*/",
1950 list);
1951 lp_do_parameter(SNUM(handle->conn),
1952 "veto files",
1953 newlist);
1955 } else {
1956 lp_do_parameter(SNUM(handle->conn),
1957 "veto files",
1958 "/" ADOUBLE_NAME_PREFIX "*/");
1961 TALLOC_FREE(list);
1963 rc = init_fruit_config(handle);
1964 if (rc != 0) {
1965 return rc;
1968 SMB_VFS_HANDLE_GET_DATA(handle, config,
1969 struct fruit_config_data, return -1);
1971 if (config->encoding == FRUIT_ENC_NATIVE) {
1972 lp_do_parameter(
1973 SNUM(handle->conn),
1974 "catia:mappings",
1975 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
1976 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
1977 "0x0d:0xf00d");
1980 return rc;
1983 static int fruit_open_meta(vfs_handle_struct *handle,
1984 struct smb_filename *smb_fname,
1985 files_struct *fsp, int flags, mode_t mode)
1987 int rc = 0;
1988 struct fruit_config_data *config = NULL;
1989 struct smb_filename *smb_fname_base = NULL;
1990 int baseflags;
1991 int hostfd = -1;
1992 struct adouble *ad = NULL;
1994 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
1996 SMB_VFS_HANDLE_GET_DATA(handle, config,
1997 struct fruit_config_data, return -1);
1999 if (config->meta == FRUIT_META_STREAM) {
2000 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2003 /* Create an smb_filename with stream_name == NULL. */
2004 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2005 smb_fname->base_name, NULL, NULL);
2007 if (smb_fname_base == NULL) {
2008 errno = ENOMEM;
2009 rc = -1;
2010 goto exit;
2014 * We use baseflags to turn off nasty side-effects when opening the
2015 * underlying file.
2017 baseflags = flags;
2018 baseflags &= ~O_TRUNC;
2019 baseflags &= ~O_EXCL;
2020 baseflags &= ~O_CREAT;
2022 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2023 baseflags, mode);
2026 * It is legit to open a stream on a directory, but the base
2027 * fd has to be read-only.
2029 if ((hostfd == -1) && (errno == EISDIR)) {
2030 baseflags &= ~O_ACCMODE;
2031 baseflags |= O_RDONLY;
2032 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2033 baseflags, mode);
2036 TALLOC_FREE(smb_fname_base);
2038 if (hostfd == -1) {
2039 rc = -1;
2040 goto exit;
2043 if (flags & (O_CREAT | O_TRUNC)) {
2045 * The attribute does not exist or needs to be truncated,
2046 * create an AppleDouble EA
2048 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2049 handle, ADOUBLE_META, fsp);
2050 if (ad == NULL) {
2051 rc = -1;
2052 goto exit;
2055 rc = ad_write(ad, smb_fname->base_name);
2056 if (rc != 0) {
2057 rc = -1;
2058 goto exit;
2060 } else {
2061 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2062 handle, ADOUBLE_META, fsp);
2063 if (ad == NULL) {
2064 rc = -1;
2065 goto exit;
2067 if (ad_read(ad, smb_fname->base_name) == -1) {
2068 rc = -1;
2069 goto exit;
2073 exit:
2074 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2075 if (rc != 0) {
2076 int saved_errno = errno;
2077 if (hostfd >= 0) {
2079 * BUGBUGBUG -- we would need to call
2080 * fd_close_posix here, but we don't have a
2081 * full fsp yet
2083 fsp->fh->fd = hostfd;
2084 SMB_VFS_CLOSE(fsp);
2086 hostfd = -1;
2087 errno = saved_errno;
2089 return hostfd;
2092 static int fruit_open_rsrc(vfs_handle_struct *handle,
2093 struct smb_filename *smb_fname,
2094 files_struct *fsp, int flags, mode_t mode)
2096 int rc = 0;
2097 struct fruit_config_data *config = NULL;
2098 struct adouble *ad = NULL;
2099 struct smb_filename *smb_fname_base = NULL;
2100 char *adpath = NULL;
2101 int hostfd = -1;
2103 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2105 SMB_VFS_HANDLE_GET_DATA(handle, config,
2106 struct fruit_config_data, return -1);
2108 switch (config->rsrc) {
2109 case FRUIT_RSRC_STREAM:
2110 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2111 case FRUIT_RSRC_XATTR:
2112 #ifdef HAVE_ATTROPEN
2113 hostfd = attropen(smb_fname->base_name,
2114 AFPRESOURCE_EA_NETATALK, flags, mode);
2115 if (hostfd == -1) {
2116 return -1;
2118 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2119 handle, ADOUBLE_RSRC, fsp);
2120 if (ad == NULL) {
2121 rc = -1;
2122 goto exit;
2124 goto exit;
2125 #else
2126 errno = ENOTSUP;
2127 return -1;
2128 #endif
2129 default:
2130 break;
2133 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2134 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2135 if (rc != 0) {
2136 rc = -1;
2137 goto exit;
2141 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2142 /* sorry, but directories don't habe a resource fork */
2143 rc = -1;
2144 goto exit;
2147 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2148 if (rc != 0) {
2149 goto exit;
2152 /* Create an smb_filename with stream_name == NULL. */
2153 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2154 adpath, NULL, NULL);
2155 if (smb_fname_base == NULL) {
2156 errno = ENOMEM;
2157 rc = -1;
2158 goto exit;
2161 /* Sanitize flags */
2162 if (flags & O_WRONLY) {
2163 /* We always need read access for the metadata header too */
2164 flags &= ~O_WRONLY;
2165 flags |= O_RDWR;
2168 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2169 flags, mode);
2170 if (hostfd == -1) {
2171 rc = -1;
2172 goto exit;
2175 /* REVIEW: we need this in ad_write() */
2176 fsp->fh->fd = hostfd;
2178 if (flags & (O_CREAT | O_TRUNC)) {
2179 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2180 handle, ADOUBLE_RSRC, fsp);
2181 if (ad == NULL) {
2182 rc = -1;
2183 goto exit;
2185 rc = ad_write(ad, smb_fname->base_name);
2186 if (rc != 0) {
2187 rc = -1;
2188 goto exit;
2190 } else {
2191 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2192 handle, ADOUBLE_RSRC, fsp);
2193 if (ad == NULL) {
2194 rc = -1;
2195 goto exit;
2197 if (ad_read(ad, smb_fname->base_name) == -1) {
2198 rc = -1;
2199 goto exit;
2203 exit:
2205 TALLOC_FREE(adpath);
2206 TALLOC_FREE(smb_fname_base);
2208 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2209 if (rc != 0) {
2210 int saved_errno = errno;
2211 if (hostfd >= 0) {
2213 * BUGBUGBUG -- we would need to call
2214 * fd_close_posix here, but we don't have a
2215 * full fsp yet
2217 fsp->fh->fd = hostfd;
2218 SMB_VFS_CLOSE(fsp);
2220 hostfd = -1;
2221 errno = saved_errno;
2223 return hostfd;
2226 static int fruit_open(vfs_handle_struct *handle,
2227 struct smb_filename *smb_fname,
2228 files_struct *fsp, int flags, mode_t mode)
2230 DEBUG(10, ("fruit_open called for %s\n",
2231 smb_fname_str_dbg(smb_fname)));
2233 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2234 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2237 if (is_afpinfo_stream(smb_fname)) {
2238 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2239 } else if (is_afpresource_stream(smb_fname)) {
2240 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2243 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2246 static int fruit_rename(struct vfs_handle_struct *handle,
2247 const struct smb_filename *smb_fname_src,
2248 const struct smb_filename *smb_fname_dst)
2250 int rc = -1;
2251 char *src_adouble_path = NULL;
2252 char *dst_adouble_path = NULL;
2253 struct fruit_config_data *config = NULL;
2255 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2257 if (!VALID_STAT(smb_fname_src->st)
2258 || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2259 return rc;
2262 SMB_VFS_HANDLE_GET_DATA(handle, config,
2263 struct fruit_config_data, return -1);
2265 if (config->rsrc == FRUIT_RSRC_XATTR) {
2266 return rc;
2269 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2270 &src_adouble_path);
2271 if (rc != 0) {
2272 goto done;
2274 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2275 &dst_adouble_path);
2276 if (rc != 0) {
2277 goto done;
2280 DEBUG(10, ("fruit_rename: %s -> %s\n",
2281 src_adouble_path, dst_adouble_path));
2283 rc = rename(src_adouble_path, dst_adouble_path);
2284 if (errno == ENOENT) {
2285 rc = 0;
2288 TALLOC_FREE(src_adouble_path);
2289 TALLOC_FREE(dst_adouble_path);
2291 done:
2292 return rc;
2295 static int fruit_unlink(vfs_handle_struct *handle,
2296 const struct smb_filename *smb_fname)
2298 int rc = -1;
2299 struct fruit_config_data *config = NULL;
2300 char *adp = NULL;
2302 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2303 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2306 SMB_VFS_HANDLE_GET_DATA(handle, config,
2307 struct fruit_config_data, return -1);
2309 if (is_afpinfo_stream(smb_fname)) {
2310 if (config->meta == FRUIT_META_STREAM) {
2311 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2312 } else {
2313 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2314 smb_fname->base_name,
2315 AFPINFO_EA_NETATALK);
2317 } else if (is_afpresource_stream(smb_fname)) {
2318 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2319 rc = adouble_path(talloc_tos(),
2320 smb_fname->base_name, &adp);
2321 if (rc != 0) {
2322 return -1;
2324 /* FIXME: direct unlink(), missing smb_fname */
2325 rc = unlink(adp);
2326 if ((rc == -1) && (errno == ENOENT)) {
2327 rc = 0;
2329 } else {
2330 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2331 smb_fname->base_name,
2332 AFPRESOURCE_EA_NETATALK);
2334 } else {
2335 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2338 TALLOC_FREE(adp);
2339 return rc;
2342 static int fruit_chmod(vfs_handle_struct *handle,
2343 const char *path,
2344 mode_t mode)
2346 int rc = -1;
2347 char *adp = NULL;
2348 struct fruit_config_data *config = NULL;
2349 SMB_STRUCT_STAT sb;
2351 rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2352 if (rc != 0) {
2353 return rc;
2356 SMB_VFS_HANDLE_GET_DATA(handle, config,
2357 struct fruit_config_data, return -1);
2359 if (config->rsrc == FRUIT_RSRC_XATTR) {
2360 return 0;
2363 /* FIXME: direct sys_lstat(), missing smb_fname */
2364 rc = sys_lstat(path, &sb, false);
2365 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2366 return rc;
2369 rc = adouble_path(talloc_tos(), path, &adp);
2370 if (rc != 0) {
2371 return -1;
2374 DEBUG(10, ("fruit_chmod: %s\n", adp));
2376 rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2377 if (errno == ENOENT) {
2378 rc = 0;
2381 TALLOC_FREE(adp);
2382 return rc;
2385 static int fruit_chown(vfs_handle_struct *handle,
2386 const char *path,
2387 uid_t uid,
2388 gid_t gid)
2390 int rc = -1;
2391 char *adp = NULL;
2392 struct fruit_config_data *config = NULL;
2393 SMB_STRUCT_STAT sb;
2395 rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2396 if (rc != 0) {
2397 return rc;
2400 SMB_VFS_HANDLE_GET_DATA(handle, config,
2401 struct fruit_config_data, return -1);
2403 if (config->rsrc == FRUIT_RSRC_XATTR) {
2404 return rc;
2407 /* FIXME: direct sys_lstat(), missing smb_fname */
2408 rc = sys_lstat(path, &sb, false);
2409 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2410 return rc;
2413 rc = adouble_path(talloc_tos(), path, &adp);
2414 if (rc != 0) {
2415 goto done;
2418 DEBUG(10, ("fruit_chown: %s\n", adp));
2420 rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2421 if (errno == ENOENT) {
2422 rc = 0;
2425 done:
2426 TALLOC_FREE(adp);
2427 return rc;
2430 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2432 DIR *dh = NULL;
2433 struct dirent *de;
2434 struct fruit_config_data *config;
2436 SMB_VFS_HANDLE_GET_DATA(handle, config,
2437 struct fruit_config_data, return -1);
2439 if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2440 goto exit_rmdir;
2444 * Due to there is no way to change bDeleteVetoFiles variable
2445 * from this module, need to clean up ourselves
2447 dh = opendir(path);
2448 if (dh == NULL) {
2449 goto exit_rmdir;
2452 while ((de = readdir(dh)) != NULL) {
2453 if ((strncmp(de->d_name,
2454 ADOUBLE_NAME_PREFIX,
2455 strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2456 char *p = talloc_asprintf(talloc_tos(),
2457 "%s/%s",
2458 path, de->d_name);
2459 if (p == NULL) {
2460 goto exit_rmdir;
2462 DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2463 (void)unlink(p);
2464 TALLOC_FREE(p);
2468 exit_rmdir:
2469 if (dh) {
2470 closedir(dh);
2472 return SMB_VFS_NEXT_RMDIR(handle, path);
2475 static ssize_t fruit_pread(vfs_handle_struct *handle,
2476 files_struct *fsp, void *data,
2477 size_t n, off_t offset)
2479 int rc = 0;
2480 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2481 handle, fsp);
2482 struct fruit_config_data *config = NULL;
2483 AfpInfo *ai = NULL;
2484 ssize_t len;
2485 char *name = NULL;
2486 char *tmp_base_name = NULL;
2487 NTSTATUS status;
2489 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2491 if (!fsp->base_fsp) {
2492 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2495 SMB_VFS_HANDLE_GET_DATA(handle, config,
2496 struct fruit_config_data, return -1);
2498 /* fsp_name is not converted with vfs_catia */
2499 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2500 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2501 fsp->base_fsp->fsp_name->base_name,
2502 vfs_translate_to_unix,
2503 talloc_tos(), &name);
2504 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2505 name = talloc_strdup(talloc_tos(), tmp_base_name);
2506 if (name == NULL) {
2507 rc = -1;
2508 goto exit;
2510 } else if (!NT_STATUS_IS_OK(status)) {
2511 errno = map_errno_from_nt_status(status);
2512 rc = -1;
2513 goto exit;
2515 fsp->base_fsp->fsp_name->base_name = name;
2517 if (ad == NULL) {
2518 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2519 if (len == -1) {
2520 rc = -1;
2521 goto exit;
2523 goto exit;
2526 if (!fruit_fsp_recheck(ad, fsp)) {
2527 rc = -1;
2528 goto exit;
2531 if (ad->ad_type == ADOUBLE_META) {
2532 ai = afpinfo_new(talloc_tos());
2533 if (ai == NULL) {
2534 rc = -1;
2535 goto exit;
2538 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2539 if (len == -1) {
2540 rc = -1;
2541 goto exit;
2544 memcpy(&ai->afpi_FinderInfo[0],
2545 ad_entry(ad, ADEID_FINDERI),
2546 ADEDLEN_FINDERI);
2547 len = afpinfo_pack(ai, data);
2548 if (len != AFP_INFO_SIZE) {
2549 rc = -1;
2550 goto exit;
2552 } else {
2553 len = SMB_VFS_NEXT_PREAD(
2554 handle, fsp, data, n,
2555 offset + ad_getentryoff(ad, ADEID_RFORK));
2556 if (len == -1) {
2557 rc = -1;
2558 goto exit;
2561 exit:
2562 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2563 TALLOC_FREE(name);
2564 TALLOC_FREE(ai);
2565 if (rc != 0) {
2566 len = -1;
2568 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2569 return len;
2572 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2573 files_struct *fsp, const void *data,
2574 size_t n, off_t offset)
2576 int rc = 0;
2577 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2578 handle, fsp);
2579 struct fruit_config_data *config = NULL;
2580 AfpInfo *ai = NULL;
2581 ssize_t len;
2582 char *name = NULL;
2583 char *tmp_base_name = NULL;
2584 NTSTATUS status;
2586 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2588 if (!fsp->base_fsp) {
2589 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2592 SMB_VFS_HANDLE_GET_DATA(handle, config,
2593 struct fruit_config_data, return -1);
2595 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2596 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2597 fsp->base_fsp->fsp_name->base_name,
2598 vfs_translate_to_unix,
2599 talloc_tos(), &name);
2600 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2601 name = talloc_strdup(talloc_tos(), tmp_base_name);
2602 if (name == NULL) {
2603 rc = -1;
2604 goto exit;
2606 } else if (!NT_STATUS_IS_OK(status)) {
2607 errno = map_errno_from_nt_status(status);
2608 rc = -1;
2609 goto exit;
2611 fsp->base_fsp->fsp_name->base_name = name;
2613 if (ad == NULL) {
2614 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2615 if (len != n) {
2616 rc = -1;
2617 goto exit;
2619 goto exit;
2622 if (!fruit_fsp_recheck(ad, fsp)) {
2623 rc = -1;
2624 goto exit;
2627 if (ad->ad_type == ADOUBLE_META) {
2628 if (n != AFP_INFO_SIZE || offset != 0) {
2629 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2630 (intmax_t)offset, (intmax_t)n));
2631 rc = -1;
2632 goto exit;
2634 ai = afpinfo_unpack(talloc_tos(), data);
2635 if (ai == NULL) {
2636 rc = -1;
2637 goto exit;
2639 memcpy(ad_entry(ad, ADEID_FINDERI),
2640 &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2641 rc = ad_write(ad, name);
2642 } else {
2643 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2644 offset + ad_getentryoff(ad, ADEID_RFORK));
2645 if (len != n) {
2646 rc = -1;
2647 goto exit;
2650 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2651 rc = ad_read(ad, name);
2652 if (rc == -1) {
2653 goto exit;
2655 rc = 0;
2657 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2658 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2659 rc = ad_write(ad, name);
2664 exit:
2665 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2666 TALLOC_FREE(name);
2667 TALLOC_FREE(ai);
2668 if (rc != 0) {
2669 return -1;
2671 return n;
2675 * Helper to stat/lstat the base file of an smb_fname.
2677 static int fruit_stat_base(vfs_handle_struct *handle,
2678 struct smb_filename *smb_fname,
2679 bool follow_links)
2681 char *tmp_stream_name;
2682 int rc;
2684 tmp_stream_name = smb_fname->stream_name;
2685 smb_fname->stream_name = NULL;
2686 if (follow_links) {
2687 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2688 } else {
2689 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2691 smb_fname->stream_name = tmp_stream_name;
2692 return rc;
2695 static int fruit_stat_meta(vfs_handle_struct *handle,
2696 struct smb_filename *smb_fname,
2697 bool follow_links)
2699 /* Populate the stat struct with info from the base file. */
2700 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2701 return -1;
2703 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2704 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2705 smb_fname->stream_name);
2706 return 0;
2709 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2710 struct smb_filename *smb_fname,
2711 bool follow_links)
2714 struct adouble *ad = NULL;
2716 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2717 smb_fname_str_dbg(smb_fname)));
2719 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2720 if (ad == NULL) {
2721 errno = ENOENT;
2722 return -1;
2725 /* Populate the stat struct with info from the base file. */
2726 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2727 TALLOC_FREE(ad);
2728 return -1;
2731 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2732 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2733 smb_fname->stream_name);
2734 TALLOC_FREE(ad);
2735 return 0;
2738 static int fruit_stat(vfs_handle_struct *handle,
2739 struct smb_filename *smb_fname)
2741 int rc = -1;
2743 DEBUG(10, ("fruit_stat called for %s\n",
2744 smb_fname_str_dbg(smb_fname)));
2746 if (!is_ntfs_stream_smb_fname(smb_fname)
2747 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2748 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2749 if (rc == 0) {
2750 update_btime(handle, smb_fname);
2752 return rc;
2756 * Note if lp_posix_paths() is true, we can never
2757 * get here as is_ntfs_stream_smb_fname() is
2758 * always false. So we never need worry about
2759 * not following links here.
2762 if (is_afpinfo_stream(smb_fname)) {
2763 rc = fruit_stat_meta(handle, smb_fname, true);
2764 } else if (is_afpresource_stream(smb_fname)) {
2765 rc = fruit_stat_rsrc(handle, smb_fname, true);
2766 } else {
2767 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2770 if (rc == 0) {
2771 update_btime(handle, smb_fname);
2772 smb_fname->st.st_ex_mode &= ~S_IFMT;
2773 smb_fname->st.st_ex_mode |= S_IFREG;
2774 smb_fname->st.st_ex_blocks =
2775 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2777 return rc;
2780 static int fruit_lstat(vfs_handle_struct *handle,
2781 struct smb_filename *smb_fname)
2783 int rc = -1;
2785 DEBUG(10, ("fruit_lstat called for %s\n",
2786 smb_fname_str_dbg(smb_fname)));
2788 if (!is_ntfs_stream_smb_fname(smb_fname)
2789 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2790 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2791 if (rc == 0) {
2792 update_btime(handle, smb_fname);
2794 return rc;
2797 if (is_afpinfo_stream(smb_fname)) {
2798 rc = fruit_stat_meta(handle, smb_fname, false);
2799 } else if (is_afpresource_stream(smb_fname)) {
2800 rc = fruit_stat_rsrc(handle, smb_fname, false);
2801 } else {
2802 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2805 if (rc == 0) {
2806 update_btime(handle, smb_fname);
2807 smb_fname->st.st_ex_mode &= ~S_IFMT;
2808 smb_fname->st.st_ex_mode |= S_IFREG;
2809 smb_fname->st.st_ex_blocks =
2810 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2812 return rc;
2815 static int fruit_fstat_meta(vfs_handle_struct *handle,
2816 files_struct *fsp,
2817 SMB_STRUCT_STAT *sbuf)
2819 DEBUG(10, ("fruit_fstat_meta called for %s\n",
2820 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2822 /* Populate the stat struct with info from the base file. */
2823 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2824 return -1;
2826 *sbuf = fsp->base_fsp->fsp_name->st;
2827 sbuf->st_ex_size = AFP_INFO_SIZE;
2828 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2830 return 0;
2833 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
2834 SMB_STRUCT_STAT *sbuf)
2836 struct fruit_config_data *config;
2837 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2838 handle, fsp);
2840 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
2841 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2843 SMB_VFS_HANDLE_GET_DATA(handle, config,
2844 struct fruit_config_data, return -1);
2846 if (config->rsrc == FRUIT_RSRC_STREAM) {
2847 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2850 /* Populate the stat struct with info from the base file. */
2851 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2852 return -1;
2854 *sbuf = fsp->base_fsp->fsp_name->st;
2855 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2856 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2858 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
2859 smb_fname_str_dbg(fsp->fsp_name),
2860 (ssize_t)sbuf->st_ex_size));
2862 return 0;
2865 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
2866 SMB_STRUCT_STAT *sbuf)
2868 int rc;
2869 char *name = NULL;
2870 char *tmp_base_name = NULL;
2871 NTSTATUS status;
2872 struct adouble *ad = (struct adouble *)
2873 VFS_FETCH_FSP_EXTENSION(handle, fsp);
2875 DEBUG(10, ("fruit_fstat called for %s\n",
2876 smb_fname_str_dbg(fsp->fsp_name)));
2878 if (fsp->base_fsp) {
2879 tmp_base_name = fsp->fsp_name->base_name;
2880 /* fsp_name is not converted with vfs_catia */
2881 status = SMB_VFS_TRANSLATE_NAME(
2882 handle->conn,
2883 fsp->base_fsp->fsp_name->base_name,
2884 vfs_translate_to_unix,
2885 talloc_tos(), &name);
2887 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2888 name = talloc_strdup(talloc_tos(), tmp_base_name);
2889 if (name == NULL) {
2890 rc = -1;
2891 goto exit;
2893 } else if (!NT_STATUS_IS_OK(status)) {
2894 errno = map_errno_from_nt_status(status);
2895 rc = -1;
2896 goto exit;
2898 fsp->base_fsp->fsp_name->base_name = name;
2901 if (ad == NULL || fsp->base_fsp == NULL) {
2902 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2903 goto exit;
2906 if (!fruit_fsp_recheck(ad, fsp)) {
2907 rc = -1;
2908 goto exit;
2911 switch (ad->ad_type) {
2912 case ADOUBLE_META:
2913 rc = fruit_fstat_meta(handle, fsp, sbuf);
2914 break;
2915 case ADOUBLE_RSRC:
2916 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
2917 break;
2918 default:
2919 DEBUG(10, ("fruit_fstat %s: bad type\n",
2920 smb_fname_str_dbg(fsp->fsp_name)));
2921 rc = -1;
2922 goto exit;
2925 if (rc == 0) {
2926 sbuf->st_ex_mode &= ~S_IFMT;
2927 sbuf->st_ex_mode |= S_IFREG;
2928 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
2931 exit:
2932 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
2933 smb_fname_str_dbg(fsp->fsp_name),
2934 (ssize_t)sbuf->st_ex_size));
2935 if (tmp_base_name) {
2936 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2938 TALLOC_FREE(name);
2939 return rc;
2942 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
2943 struct files_struct *fsp,
2944 const char *fname,
2945 TALLOC_CTX *mem_ctx,
2946 unsigned int *pnum_streams,
2947 struct stream_struct **pstreams)
2949 struct fruit_config_data *config = NULL;
2950 struct smb_filename *smb_fname = NULL;
2951 struct adouble *ad = NULL;
2953 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2954 return NT_STATUS_UNSUCCESSFUL);
2955 DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
2957 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
2958 if (smb_fname == NULL) {
2959 return NT_STATUS_NO_MEMORY;
2962 if (config->meta == FRUIT_META_NETATALK) {
2963 ad = ad_get(talloc_tos(), handle,
2964 smb_fname->base_name, ADOUBLE_META);
2965 if (ad && !empty_finderinfo(ad)) {
2966 if (!add_fruit_stream(
2967 mem_ctx, pnum_streams, pstreams,
2968 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
2969 smb_roundup(handle->conn,
2970 AFP_INFO_SIZE))) {
2971 TALLOC_FREE(ad);
2972 TALLOC_FREE(smb_fname);
2973 return NT_STATUS_NO_MEMORY;
2976 TALLOC_FREE(ad);
2979 if (config->rsrc != FRUIT_RSRC_STREAM) {
2980 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
2981 ADOUBLE_RSRC);
2982 if (ad) {
2983 if (!add_fruit_stream(
2984 mem_ctx, pnum_streams, pstreams,
2985 AFPRESOURCE_STREAM_NAME,
2986 ad_getentrylen(ad, ADEID_RFORK),
2987 smb_roundup(handle->conn,
2988 ad_getentrylen(
2989 ad, ADEID_RFORK)))) {
2990 TALLOC_FREE(ad);
2991 TALLOC_FREE(smb_fname);
2992 return NT_STATUS_NO_MEMORY;
2995 TALLOC_FREE(ad);
2998 TALLOC_FREE(smb_fname);
3000 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3001 pnum_streams, pstreams);
3004 static int fruit_ntimes(vfs_handle_struct *handle,
3005 const struct smb_filename *smb_fname,
3006 struct smb_file_time *ft)
3008 int rc = 0;
3009 struct adouble *ad = NULL;
3011 if (null_timespec(ft->create_time)) {
3012 goto exit;
3015 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3016 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3018 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3019 if (ad == NULL) {
3020 goto exit;
3023 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3024 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3026 rc = ad_write(ad, smb_fname->base_name);
3028 exit:
3030 TALLOC_FREE(ad);
3031 if (rc != 0) {
3032 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3033 return -1;
3035 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3038 static int fruit_fallocate(struct vfs_handle_struct *handle,
3039 struct files_struct *fsp,
3040 enum vfs_fallocate_mode mode,
3041 off_t offset,
3042 off_t len)
3044 struct adouble *ad =
3045 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3047 if (ad == NULL) {
3048 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3051 if (!fruit_fsp_recheck(ad, fsp)) {
3052 return errno;
3055 /* Let the pwrite code path handle it. */
3056 return ENOSYS;
3059 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3060 struct files_struct *fsp,
3061 off_t offset)
3063 int rc = 0;
3064 struct adouble *ad =
3065 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3066 struct fruit_config_data *config;
3068 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3069 fsp_str_dbg(fsp), (double)offset));
3071 if (ad == NULL) {
3072 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3075 if (!fruit_fsp_recheck(ad, fsp)) {
3076 return -1;
3079 SMB_VFS_HANDLE_GET_DATA(handle, config,
3080 struct fruit_config_data, return -1);
3082 switch (ad->ad_type) {
3083 case ADOUBLE_META:
3085 * As this request hasn't been seen in the wild,
3086 * the only sensible use I can imagine is the client
3087 * truncating the stream to 0 bytes size.
3088 * We simply remove the metadata on such a request.
3090 if (offset == 0) {
3091 rc = SMB_VFS_FREMOVEXATTR(fsp,
3092 AFPRESOURCE_EA_NETATALK);
3094 break;
3095 case ADOUBLE_RSRC:
3096 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3097 rc = SMB_VFS_FREMOVEXATTR(fsp,
3098 AFPRESOURCE_EA_NETATALK);
3099 } else {
3100 rc = SMB_VFS_NEXT_FTRUNCATE(
3101 handle, fsp,
3102 offset + ad_getentryoff(ad, ADEID_RFORK));
3103 if (rc != 0) {
3104 return -1;
3106 ad_setentrylen(ad, ADEID_RFORK, offset);
3107 rc = ad_write(ad, NULL);
3108 if (rc != 0) {
3109 return -1;
3112 break;
3113 default:
3114 return -1;
3117 return rc;
3120 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3121 struct smb_request *req,
3122 uint16_t root_dir_fid,
3123 struct smb_filename *smb_fname,
3124 uint32_t access_mask,
3125 uint32_t share_access,
3126 uint32_t create_disposition,
3127 uint32_t create_options,
3128 uint32_t file_attributes,
3129 uint32_t oplock_request,
3130 struct smb2_lease *lease,
3131 uint64_t allocation_size,
3132 uint32_t private_flags,
3133 struct security_descriptor *sd,
3134 struct ea_list *ea_list,
3135 files_struct **result,
3136 int *pinfo,
3137 const struct smb2_create_blobs *in_context_blobs,
3138 struct smb2_create_blobs *out_context_blobs)
3140 NTSTATUS status;
3141 struct fruit_config_data *config = NULL;
3143 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3144 if (!NT_STATUS_IS_OK(status)) {
3145 return status;
3148 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3149 return NT_STATUS_UNSUCCESSFUL);
3151 status = SMB_VFS_NEXT_CREATE_FILE(
3152 handle, req, root_dir_fid, smb_fname,
3153 access_mask, share_access,
3154 create_disposition, create_options,
3155 file_attributes, oplock_request,
3156 lease,
3157 allocation_size, private_flags,
3158 sd, ea_list, result,
3159 pinfo, in_context_blobs, out_context_blobs);
3160 if (!NT_STATUS_IS_OK(status)) {
3161 return status;
3164 if (is_ntfs_stream_smb_fname(smb_fname)
3165 || (*result == NULL)
3166 || ((*result)->is_directory)) {
3167 return status;
3170 if (config->locking == FRUIT_LOCKING_NETATALK) {
3171 status = fruit_check_access(
3172 handle, *result,
3173 access_mask,
3174 map_share_mode_to_deny_mode(share_access, 0));
3175 if (!NT_STATUS_IS_OK(status)) {
3176 goto fail;
3180 return status;
3182 fail:
3183 DEBUG(1, ("fruit_create_file: %s\n", nt_errstr(status)));
3185 if (*result) {
3186 close_file(req, *result, ERROR_CLOSE);
3187 *result = NULL;
3190 return status;
3193 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3194 const struct smb_filename *fname,
3195 TALLOC_CTX *mem_ctx,
3196 struct readdir_attr_data **pattr_data)
3198 struct fruit_config_data *config = NULL;
3199 struct readdir_attr_data *attr_data;
3200 NTSTATUS status;
3202 SMB_VFS_HANDLE_GET_DATA(handle, config,
3203 struct fruit_config_data,
3204 return NT_STATUS_UNSUCCESSFUL);
3206 if (!config->use_aapl) {
3207 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3210 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3212 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3213 if (*pattr_data == NULL) {
3214 return NT_STATUS_UNSUCCESSFUL;
3216 attr_data = *pattr_data;
3217 attr_data->type = RDATTR_AAPL;
3220 * Mac metadata: compressed FinderInfo, resource fork length
3221 * and creation date
3223 status = readdir_attr_macmeta(handle, fname, attr_data);
3224 if (!NT_STATUS_IS_OK(status)) {
3226 * Error handling is tricky: if we return failure from
3227 * this function, the corresponding directory entry
3228 * will to be passed to the client, so we really just
3229 * want to error out on fatal errors.
3231 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3232 goto fail;
3237 * UNIX mode
3239 if (config->unix_info_enabled) {
3240 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3244 * max_access
3246 if (!config->readdir_attr_max_access) {
3247 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3248 } else {
3249 status = smbd_calculate_access_mask(
3250 handle->conn,
3251 fname,
3252 false,
3253 SEC_FLAG_MAXIMUM_ALLOWED,
3254 &attr_data->attr_data.aapl.max_access);
3255 if (!NT_STATUS_IS_OK(status)) {
3256 goto fail;
3260 return NT_STATUS_OK;
3262 fail:
3263 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3264 fname->base_name, nt_errstr(status)));
3265 TALLOC_FREE(*pattr_data);
3266 return status;
3269 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3270 files_struct *fsp,
3271 uint32 security_info,
3272 TALLOC_CTX *mem_ctx,
3273 struct security_descriptor **ppdesc)
3275 NTSTATUS status;
3276 struct security_ace ace;
3277 struct dom_sid sid;
3278 struct fruit_config_data *config;
3280 SMB_VFS_HANDLE_GET_DATA(handle, config,
3281 struct fruit_config_data,
3282 return NT_STATUS_UNSUCCESSFUL);
3284 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3285 mem_ctx, ppdesc);
3286 if (!NT_STATUS_IS_OK(status)) {
3287 return status;
3291 * Add MS NFS style ACEs with uid, gid and mode
3293 if (!config->unix_info_enabled) {
3294 return NT_STATUS_OK;
3297 /* MS NFS style mode */
3298 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3299 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3300 status = security_descriptor_dacl_add(*ppdesc, &ace);
3301 if (!NT_STATUS_IS_OK(status)) {
3302 DEBUG(1,("failed to add MS NFS style ACE\n"));
3303 return status;
3306 /* MS NFS style uid */
3307 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3308 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3309 status = security_descriptor_dacl_add(*ppdesc, &ace);
3310 if (!NT_STATUS_IS_OK(status)) {
3311 DEBUG(1,("failed to add MS NFS style ACE\n"));
3312 return status;
3315 /* MS NFS style gid */
3316 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3317 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3318 status = security_descriptor_dacl_add(*ppdesc, &ace);
3319 if (!NT_STATUS_IS_OK(status)) {
3320 DEBUG(1,("failed to add MS NFS style ACE\n"));
3321 return status;
3324 return NT_STATUS_OK;
3327 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3328 files_struct *fsp,
3329 uint32 security_info_sent,
3330 const struct security_descriptor *psd)
3332 NTSTATUS status;
3333 bool do_chmod;
3334 mode_t ms_nfs_mode;
3335 int result;
3337 DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp)));
3339 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3340 if (!NT_STATUS_IS_OK(status)) {
3341 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3342 return status;
3345 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3346 if (!NT_STATUS_IS_OK(status)) {
3347 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3348 return status;
3351 if (do_chmod) {
3352 if (fsp->fh->fd != -1) {
3353 DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp)));
3354 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3355 } else {
3356 DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp)));
3357 result = SMB_VFS_CHMOD(fsp->conn,
3358 fsp->fsp_name->base_name,
3359 ms_nfs_mode);
3362 if (result != 0) {
3363 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3364 result, ms_nfs_mode, strerror(errno)));
3365 status = map_nt_error_from_unix(errno);
3366 return status;
3370 return NT_STATUS_OK;
3373 static struct vfs_fn_pointers vfs_fruit_fns = {
3374 .connect_fn = fruit_connect,
3376 /* File operations */
3377 .chmod_fn = fruit_chmod,
3378 .chown_fn = fruit_chown,
3379 .unlink_fn = fruit_unlink,
3380 .rename_fn = fruit_rename,
3381 .rmdir_fn = fruit_rmdir,
3382 .open_fn = fruit_open,
3383 .pread_fn = fruit_pread,
3384 .pwrite_fn = fruit_pwrite,
3385 .stat_fn = fruit_stat,
3386 .lstat_fn = fruit_lstat,
3387 .fstat_fn = fruit_fstat,
3388 .streaminfo_fn = fruit_streaminfo,
3389 .ntimes_fn = fruit_ntimes,
3390 .unlink_fn = fruit_unlink,
3391 .ftruncate_fn = fruit_ftruncate,
3392 .fallocate_fn = fruit_fallocate,
3393 .create_file_fn = fruit_create_file,
3394 .readdir_attr_fn = fruit_readdir_attr,
3396 /* NT ACL operations */
3397 .fget_nt_acl_fn = fruit_fget_nt_acl,
3398 .fset_nt_acl_fn = fruit_fset_nt_acl,
3401 NTSTATUS vfs_fruit_init(void);
3402 NTSTATUS vfs_fruit_init(void)
3404 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3405 &vfs_fruit_fns);
3406 if (!NT_STATUS_IS_OK(ret)) {
3407 return ret;
3410 vfs_fruit_debug_level = debug_add_class("fruit");
3411 if (vfs_fruit_debug_level == -1) {
3412 vfs_fruit_debug_level = DBGC_VFS;
3413 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3414 "vfs_fruit_init"));
3415 } else {
3416 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3417 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3420 return ret;