smbcacls: Use defines for security flags
[Samba.git] / source3 / modules / vfs_fruit.c
blob0a0c081756c0476ccfc6bd8004b8d6cb43be3e1e
1 /*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "../lib/crypto/md5.h"
26 #include "system/shmem.h"
27 #include "locking/proto.h"
28 #include "smbd/globals.h"
29 #include "messages.h"
30 #include "libcli/security/security.h"
31 #include "../libcli/smb/smb2_create_ctx.h"
32 #include "lib/sys_rw.h"
35 * Enhanced OS X and Netatalk compatibility
36 * ========================================
38 * This modules takes advantage of vfs_streams_xattr and
39 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
40 * loaded in the correct order:
42 * vfs modules = catia fruit streams_xattr
44 * The module intercepts the OS X special streams "AFP_AfpInfo" and
45 * "AFP_Resource" and handles them in a special way. All other named
46 * streams are deferred to vfs_streams_xattr.
48 * The OS X client maps all NTFS illegal characters to the Unicode
49 * private range. This module optionally stores the charcters using
50 * their native ASCII encoding using vfs_catia. If you're not enabling
51 * this feature, you can skip catia from vfs modules.
53 * Finally, open modes are optionally checked against Netatalk AFP
54 * share modes.
56 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
57 * extended metadata for files and directories. This module optionally
58 * reads and stores this metadata in a way compatible with Netatalk 3
59 * which stores the metadata in an EA "org.netatalk.metadata". Cf
60 * source3/include/MacExtensions.h for a description of the binary
61 * blobs content.
63 * The "AFP_Resource" named stream may be arbitrarily large, thus it
64 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
65 * the only available filesystem where xattrs can be of any size and
66 * the OS supports using the file APIs for xattrs.
68 * The AFP_Resource stream is stored in an AppleDouble file prepending
69 * "._" to the filename. On Solaris with ZFS the stream is optionally
70 * stored in an EA "org.netatalk.ressource".
73 * Extended Attributes
74 * ===================
76 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
77 * other protocols you may want to adjust the xattr names the VFS
78 * module vfs_streams_xattr uses for storing ADS's. This defaults to
79 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
80 * these module parameters:
82 * streams_xattr:prefix = user.
83 * streams_xattr:store_stream_type = false
86 * TODO
87 * ====
89 * - log diagnostic if any needed VFS module is not loaded
90 * (eg with lp_vfs_objects())
91 * - add tests
94 static int vfs_fruit_debug_level = DBGC_VFS;
96 #undef DBGC_CLASS
97 #define DBGC_CLASS vfs_fruit_debug_level
99 #define FRUIT_PARAM_TYPE_NAME "fruit"
100 #define ADOUBLE_NAME_PREFIX "._"
103 * REVIEW:
104 * This is hokey, but what else can we do?
106 #if defined(HAVE_ATTROPEN) || defined(FREEBSD)
107 #define AFPINFO_EA_NETATALK "org.netatalk.Metadata"
108 #define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
109 #else
110 #define AFPINFO_EA_NETATALK "user.org.netatalk.Metadata"
111 #define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
112 #endif
114 enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
116 enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
117 enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
118 enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
119 enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
121 struct fruit_config_data {
122 enum fruit_rsrc rsrc;
123 enum fruit_meta meta;
124 enum fruit_locking locking;
125 enum fruit_encoding encoding;
126 bool use_aapl;
127 bool readdir_attr_enabled;
128 bool unix_info_enabled;
131 * Additional undocumented options, all enabled by default,
132 * possibly useful for analyzing performance. The associated
133 * operations with each of them may be expensive, so having
134 * the chance to disable them individually gives a chance
135 * tweaking the setup for the particular usecase.
137 bool readdir_attr_rsize;
138 bool readdir_attr_finder_info;
139 bool readdir_attr_max_access;
142 static const struct enum_list fruit_rsrc[] = {
143 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
144 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
145 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
146 { -1, NULL}
149 static const struct enum_list fruit_meta[] = {
150 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
151 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
152 { -1, NULL}
155 static const struct enum_list fruit_locking[] = {
156 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
157 {FRUIT_LOCKING_NONE, "none"},
158 { -1, NULL}
161 static const struct enum_list fruit_encoding[] = {
162 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
163 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
164 { -1, NULL}
167 /*****************************************************************************
168 * Defines, functions and data structures that deal with AppleDouble
169 *****************************************************************************/
172 * There are two AppleDouble blobs we deal with:
174 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
175 * metadata in an xattr
177 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
178 * ._ files
180 typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
182 /* Version info */
183 #define AD_VERSION2 0x00020000
184 #define AD_VERSION AD_VERSION2
187 * AppleDouble entry IDs.
189 #define ADEID_DFORK 1
190 #define ADEID_RFORK 2
191 #define ADEID_NAME 3
192 #define ADEID_COMMENT 4
193 #define ADEID_ICONBW 5
194 #define ADEID_ICONCOL 6
195 #define ADEID_FILEI 7
196 #define ADEID_FILEDATESI 8
197 #define ADEID_FINDERI 9
198 #define ADEID_MACFILEI 10
199 #define ADEID_PRODOSFILEI 11
200 #define ADEID_MSDOSFILEI 12
201 #define ADEID_SHORTNAME 13
202 #define ADEID_AFPFILEI 14
203 #define ADEID_DID 15
205 /* Private Netatalk entries */
206 #define ADEID_PRIVDEV 16
207 #define ADEID_PRIVINO 17
208 #define ADEID_PRIVSYN 18
209 #define ADEID_PRIVID 19
210 #define ADEID_MAX (ADEID_PRIVID + 1)
213 * These are the real ids for the private entries,
214 * as stored in the adouble file
216 #define AD_DEV 0x80444556
217 #define AD_INO 0x80494E4F
218 #define AD_SYN 0x8053594E
219 #define AD_ID 0x8053567E
221 /* Number of actually used entries */
222 #define ADEID_NUM_XATTR 8
223 #define ADEID_NUM_DOT_UND 2
224 #define ADEID_NUM_RSRC_XATTR 1
226 /* AppleDouble magic */
227 #define AD_APPLESINGLE_MAGIC 0x00051600
228 #define AD_APPLEDOUBLE_MAGIC 0x00051607
229 #define AD_MAGIC AD_APPLEDOUBLE_MAGIC
231 /* Sizes of relevant entry bits */
232 #define ADEDLEN_MAGIC 4
233 #define ADEDLEN_VERSION 4
234 #define ADEDLEN_FILLER 16
235 #define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
236 #define ADEDLEN_NENTRIES 2
237 #define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
238 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
239 #define AD_ENTRY_LEN_EID 4
240 #define AD_ENTRY_LEN_OFF 4
241 #define AD_ENTRY_LEN_LEN 4
242 #define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
244 /* Field widths */
245 #define ADEDLEN_NAME 255
246 #define ADEDLEN_COMMENT 200
247 #define ADEDLEN_FILEI 16
248 #define ADEDLEN_FINDERI 32
249 #define ADEDLEN_FILEDATESI 16
250 #define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
251 #define ADEDLEN_AFPFILEI 4
252 #define ADEDLEN_MACFILEI 4
253 #define ADEDLEN_PRODOSFILEI 8
254 #define ADEDLEN_MSDOSFILEI 2
255 #define ADEDLEN_DID 4
256 #define ADEDLEN_PRIVDEV 8
257 #define ADEDLEN_PRIVINO 8
258 #define ADEDLEN_PRIVSYN 8
259 #define ADEDLEN_PRIVID 4
261 /* Offsets */
262 #define ADEDOFF_MAGIC 0
263 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
264 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
265 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
267 #define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
268 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
269 #define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
270 #define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
271 #define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
272 ADEDLEN_FILEDATESI)
273 #define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
274 #define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
275 #define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
276 #define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
278 #define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
279 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
280 #define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
282 #define AD_DATASZ_XATTR (AD_HEADER_LEN + \
283 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
284 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
285 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
286 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
287 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
289 #if AD_DATASZ_XATTR != 402
290 #error bad size for AD_DATASZ_XATTR
291 #endif
293 #define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
294 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
295 ADEDLEN_FINDERI)
296 #if AD_DATASZ_DOT_UND != 82
297 #error bad size for AD_DATASZ_DOT_UND
298 #endif
301 * Sharemode locks fcntl() offsets
303 #if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
304 #define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
305 #else
306 #define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
307 #endif
308 #define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
310 #define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
311 #define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
312 #define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
313 #define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
314 #define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
315 #define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
316 #define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
317 #define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
318 #define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
319 #define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
321 /* Time stuff we overload the bits a little */
322 #define AD_DATE_CREATE 0
323 #define AD_DATE_MODIFY 4
324 #define AD_DATE_BACKUP 8
325 #define AD_DATE_ACCESS 12
326 #define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
327 AD_DATE_BACKUP | AD_DATE_ACCESS)
328 #define AD_DATE_UNIX (1 << 10)
329 #define AD_DATE_START 0x80000000
330 #define AD_DATE_DELTA 946684800
331 #define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
332 #define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
334 /* Accessor macros */
335 #define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
336 #define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
337 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
338 #define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
339 #define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
341 struct ad_entry {
342 size_t ade_off;
343 size_t ade_len;
346 struct adouble {
347 vfs_handle_struct *ad_handle;
348 files_struct *ad_fsp;
349 adouble_type_t ad_type;
350 uint32_t ad_magic;
351 uint32_t ad_version;
352 struct ad_entry ad_eid[ADEID_MAX];
353 char *ad_data;
356 struct ad_entry_order {
357 uint32_t id, offset, len;
360 /* Netatalk AppleDouble metadata xattr */
361 static const
362 struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
363 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
364 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
365 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
366 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
367 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
368 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
369 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
370 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
371 {0, 0, 0}
374 /* AppleDouble ressource fork file (the ones prefixed by "._") */
375 static const
376 struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
377 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
378 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
379 {0, 0, 0}
383 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
384 * isn't an AppleDouble file, it simply contains the ressource data,
385 * but in order to be able to use some API calls like ad_getentryoff()
386 * we build a fake/helper struct adouble with this entry order struct.
388 static const
389 struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
390 {ADEID_RFORK, 0, 0},
391 {0, 0, 0}
394 /* Conversion from enumerated id to on-disk AppleDouble id */
395 #define AD_EID_DISK(a) (set_eid[a])
396 static const uint32_t set_eid[] = {
397 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
398 AD_DEV, AD_INO, AD_SYN, AD_ID
402 * Forward declarations
404 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
405 adouble_type_t type, files_struct *fsp);
406 static int ad_write(struct adouble *ad, const char *path);
407 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
410 * Get a date
412 static int ad_getdate(const struct adouble *ad,
413 unsigned int dateoff,
414 uint32_t *date)
416 bool xlate = (dateoff & AD_DATE_UNIX);
418 dateoff &= AD_DATE_MASK;
419 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
420 return -1;
423 if (dateoff > AD_DATE_ACCESS) {
424 return -1;
426 memcpy(date,
427 ad_entry(ad, ADEID_FILEDATESI) + dateoff,
428 sizeof(uint32_t));
430 if (xlate) {
431 *date = AD_DATE_TO_UNIX(*date);
433 return 0;
437 * Set a date
439 static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
441 bool xlate = (dateoff & AD_DATE_UNIX);
443 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
444 return 0;
447 dateoff &= AD_DATE_MASK;
448 if (xlate) {
449 date = AD_DATE_FROM_UNIX(date);
452 if (dateoff > AD_DATE_ACCESS) {
453 return -1;
456 memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
458 return 0;
463 * Map on-disk AppleDouble id to enumerated id
465 static uint32_t get_eid(uint32_t eid)
467 if (eid <= 15) {
468 return eid;
471 switch (eid) {
472 case AD_DEV:
473 return ADEID_PRIVDEV;
474 case AD_INO:
475 return ADEID_PRIVINO;
476 case AD_SYN:
477 return ADEID_PRIVSYN;
478 case AD_ID:
479 return ADEID_PRIVID;
480 default:
481 break;
484 return 0;
488 * Pack AppleDouble structure into data buffer
490 static bool ad_pack(struct adouble *ad)
492 uint32_t eid;
493 uint16_t nent;
494 uint32_t bufsize;
495 uint32_t offset = 0;
497 bufsize = talloc_get_size(ad->ad_data);
499 if (offset + ADEDLEN_MAGIC < offset ||
500 offset + ADEDLEN_MAGIC >= bufsize) {
501 return false;
503 RSIVAL(ad->ad_data, offset, ad->ad_magic);
504 offset += ADEDLEN_MAGIC;
506 if (offset + ADEDLEN_VERSION < offset ||
507 offset + ADEDLEN_VERSION >= bufsize) {
508 return false;
510 RSIVAL(ad->ad_data, offset, ad->ad_version);
511 offset += ADEDLEN_VERSION;
513 if (offset + ADEDLEN_FILLER < offset ||
514 offset + ADEDLEN_FILLER >= bufsize) {
515 return false;
517 if (ad->ad_type == ADOUBLE_RSRC) {
518 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
520 offset += ADEDLEN_FILLER;
522 if (offset + ADEDLEN_NENTRIES < offset ||
523 offset + ADEDLEN_NENTRIES >= bufsize) {
524 return false;
526 offset += ADEDLEN_NENTRIES;
528 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
529 if (ad->ad_eid[eid].ade_off == 0) {
531 * ade_off is also used as indicator whether a
532 * specific entry is used or not
534 continue;
537 if (offset + AD_ENTRY_LEN_EID < offset ||
538 offset + AD_ENTRY_LEN_EID >= bufsize) {
539 return false;
541 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
542 offset += AD_ENTRY_LEN_EID;
544 if (offset + AD_ENTRY_LEN_OFF < offset ||
545 offset + AD_ENTRY_LEN_OFF >= bufsize) {
546 return false;
548 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
549 offset += AD_ENTRY_LEN_OFF;
551 if (offset + AD_ENTRY_LEN_LEN < offset ||
552 offset + AD_ENTRY_LEN_LEN >= bufsize) {
553 return false;
555 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
556 offset += AD_ENTRY_LEN_LEN;
558 nent++;
561 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
562 return false;
564 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
566 return 0;
570 * Unpack an AppleDouble blob into a struct adoble
572 static bool ad_unpack(struct adouble *ad, const int nentries, size_t filesize)
574 size_t bufsize = talloc_get_size(ad->ad_data);
575 int adentries, i;
576 uint32_t eid, len, off;
579 * The size of the buffer ad->ad_data is checked when read, so
580 * we wouldn't have to check our own offsets, a few extra
581 * checks won't hurt though. We have to check the offsets we
582 * read from the buffer anyway.
585 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
586 DEBUG(1, ("bad size\n"));
587 return false;
590 ad->ad_magic = RIVAL(ad->ad_data, 0);
591 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
592 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
593 DEBUG(1, ("wrong magic or version\n"));
594 return false;
597 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
598 if (adentries != nentries) {
599 DEBUG(1, ("invalid number of entries: %d\n", adentries));
600 return false;
603 /* now, read in the entry bits */
604 for (i = 0; i < adentries; i++) {
605 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
606 eid = get_eid(eid);
607 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
608 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
610 if (!eid || eid > ADEID_MAX) {
611 DEBUG(1, ("bogus eid %d\n", eid));
612 return false;
616 * All entries other than the resource fork are
617 * expected to be read into the ad_data buffer, so
618 * ensure the specified offset is within that bound
620 if ((off > bufsize) && (eid != ADEID_RFORK)) {
621 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
622 eid, off, len));
623 return false;
627 * All entries besides FinderInfo and resource fork
628 * must fit into the buffer. FinderInfo is special as
629 * it may be larger then the default 32 bytes (if it
630 * contains marshalled xattrs), but we will fixup that
631 * in ad_convert(). And the resource fork is never
632 * accessed directly by the ad_data buf (also see
633 * comment above) anyway.
635 if ((eid != ADEID_RFORK) &&
636 (eid != ADEID_FINDERI) &&
637 ((off + len) > bufsize)) {
638 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
639 eid, off, len));
640 return false;
644 * That would be obviously broken
646 if (off > filesize) {
647 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
648 eid, off, len));
649 return false;
653 * Check for any entry that has its end beyond the
654 * filesize.
656 if (off + len < off) {
657 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
658 ", len: %" PRIu32 "\n",
659 eid, off, len));
660 return false;
663 if (off + len > filesize) {
665 * If this is the resource fork entry, we fix
666 * up the length, for any other entry we bail
667 * out.
669 if (eid != ADEID_RFORK) {
670 DEBUG(1, ("bogus eid %d: off: %" PRIu32
671 ", len: %" PRIu32 "\n",
672 eid, off, len));
673 return false;
677 * Fixup the resource fork entry by limiting
678 * the size to entryoffset - filesize.
680 len = filesize - off;
681 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
682 ", len: %" PRIu32 "\n", off, len));
685 ad->ad_eid[eid].ade_off = off;
686 ad->ad_eid[eid].ade_len = len;
689 return true;
693 * Convert from Apple's ._ file to Netatalk
695 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
696 * bytes containing packed xattrs. Netatalk can't deal with that, so
697 * we simply discard the packed xattrs.
699 * @return -1 in case an error occured, 0 if no conversion was done, 1
700 * otherwise
702 static int ad_convert(struct adouble *ad, int fd)
704 int rc = 0;
705 char *map = MAP_FAILED;
706 size_t origlen;
708 origlen = ad_getentryoff(ad, ADEID_RFORK) +
709 ad_getentrylen(ad, ADEID_RFORK);
711 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
712 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
713 if (map == MAP_FAILED) {
714 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
715 rc = -1;
716 goto exit;
719 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
720 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
721 map + ad_getentryoff(ad, ADEID_RFORK),
722 ad_getentrylen(ad, ADEID_RFORK));
725 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
726 ad_setentryoff(ad, ADEID_RFORK,
727 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
730 * FIXME: direct ftruncate(), but we don't have a fsp for the
731 * VFS call
733 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
734 + ad_getentrylen(ad, ADEID_RFORK));
736 exit:
737 if (map != MAP_FAILED) {
738 munmap(map, origlen);
740 return rc;
744 * Read and parse Netatalk AppleDouble metadata xattr
746 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
748 int rc = 0;
749 ssize_t ealen;
750 bool ok;
752 DEBUG(10, ("reading meta xattr for %s\n", path));
754 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
755 AFPINFO_EA_NETATALK, ad->ad_data,
756 AD_DATASZ_XATTR);
757 if (ealen == -1) {
758 switch (errno) {
759 case ENOATTR:
760 case ENOENT:
761 if (errno == ENOATTR) {
762 errno = ENOENT;
764 rc = -1;
765 goto exit;
766 default:
767 DEBUG(2, ("error reading meta xattr: %s\n",
768 strerror(errno)));
769 rc = -1;
770 goto exit;
773 if (ealen != AD_DATASZ_XATTR) {
774 DEBUG(2, ("bad size %zd\n", ealen));
775 errno = EINVAL;
776 rc = -1;
777 goto exit;
780 /* Now parse entries */
781 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
782 if (!ok) {
783 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
784 errno = EINVAL;
785 rc = -1;
786 goto exit;
789 if (!ad_getentryoff(ad, ADEID_FINDERI)
790 || !ad_getentryoff(ad, ADEID_COMMENT)
791 || !ad_getentryoff(ad, ADEID_FILEDATESI)
792 || !ad_getentryoff(ad, ADEID_AFPFILEI)
793 || !ad_getentryoff(ad, ADEID_PRIVDEV)
794 || !ad_getentryoff(ad, ADEID_PRIVINO)
795 || !ad_getentryoff(ad, ADEID_PRIVSYN)
796 || !ad_getentryoff(ad, ADEID_PRIVID)) {
797 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
798 errno = EINVAL;
799 rc = -1;
800 goto exit;
803 exit:
804 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
806 if (rc != 0) {
807 ealen = -1;
808 if (errno == EINVAL) {
809 become_root();
810 removexattr(path, AFPINFO_EA_NETATALK);
811 unbecome_root();
812 errno = ENOENT;
815 return ealen;
819 * Read and parse resource fork, either ._ AppleDouble file or xattr
821 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
823 struct fruit_config_data *config = NULL;
824 int fd = -1;
825 int rc = 0;
826 ssize_t len;
827 char *adpath = NULL;
828 bool opened = false;
829 int mode;
830 struct adouble *meta_ad = NULL;
831 SMB_STRUCT_STAT sbuf;
832 bool ok;
833 int saved_errno = 0;
835 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
836 struct fruit_config_data, return -1);
838 /* Try rw first so we can use the fd in ad_convert() */
839 mode = O_RDWR;
841 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
842 fd = ad->ad_fsp->fh->fd;
843 } else {
844 if (config->rsrc == FRUIT_RSRC_XATTR) {
845 adpath = talloc_strdup(talloc_tos(), path);
846 } else {
847 rc = adouble_path(talloc_tos(), path, &adpath);
848 if (rc != 0) {
849 goto exit;
853 retry:
854 if (config->rsrc == FRUIT_RSRC_XATTR) {
855 #ifndef HAVE_ATTROPEN
856 errno = ENOSYS;
857 rc = -1;
858 goto exit;
859 #else
860 /* FIXME: direct Solaris xattr syscall */
861 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
862 mode, 0);
863 #endif
864 } else {
865 /* FIXME: direct open(), don't have an fsp */
866 fd = open(adpath, mode);
869 if (fd == -1) {
870 switch (errno) {
871 case EROFS:
872 case EACCES:
873 if (mode == O_RDWR) {
874 mode = O_RDONLY;
875 goto retry;
877 /* fall through ... */
878 default:
879 DEBUG(2, ("open AppleDouble: %s, %s\n",
880 adpath, strerror(errno)));
881 rc = -1;
882 goto exit;
885 opened = true;
888 if (config->rsrc == FRUIT_RSRC_XATTR) {
889 /* FIXME: direct sys_fstat(), don't have an fsp */
890 rc = sys_fstat(
891 fd, &sbuf,
892 lp_fake_directory_create_times(
893 SNUM(ad->ad_handle->conn)));
894 if (rc != 0) {
895 goto exit;
897 len = sbuf.st_ex_size;
898 ad_setentrylen(ad, ADEID_RFORK, len);
899 } else {
900 /* FIXME: direct sys_pread(), don't have an fsp */
901 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
902 if (len != AD_DATASZ_DOT_UND) {
903 DEBUG(2, ("%s: bad size: %zd\n",
904 strerror(errno), len));
905 rc = -1;
906 goto exit;
909 /* FIXME: direct sys_fstat(), we don't have an fsp */
910 rc = sys_fstat(fd, &sbuf,
911 lp_fake_directory_create_times(
912 SNUM(ad->ad_handle->conn)));
913 if (rc != 0) {
914 goto exit;
917 /* Now parse entries */
918 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
919 if (!ok) {
920 DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
921 errno = EINVAL;
922 rc = -1;
923 goto exit;
926 if ((ad_getentryoff(ad, ADEID_FINDERI)
927 != ADEDOFF_FINDERI_DOT_UND)
928 || (ad_getentrylen(ad, ADEID_FINDERI)
929 < ADEDLEN_FINDERI)
930 || (ad_getentryoff(ad, ADEID_RFORK)
931 < ADEDOFF_RFORK_DOT_UND)) {
932 DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
933 errno = EINVAL;
934 rc = -1;
935 goto exit;
938 if ((mode == O_RDWR)
939 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
940 rc = ad_convert(ad, fd);
941 if (rc != 0) {
942 rc = -1;
943 goto exit;
946 * Can't use ad_write() because we might not have a fsp
948 rc = ad_pack(ad);
949 if (rc != 0) {
950 goto exit;
952 /* FIXME: direct sys_pwrite(), don't have an fsp */
953 len = sys_pwrite(fd, ad->ad_data,
954 AD_DATASZ_DOT_UND, 0);
955 if (len != AD_DATASZ_DOT_UND) {
956 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
957 rc = -1;
958 goto exit;
961 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
962 ADOUBLE_META, NULL);
963 if (meta_ad == NULL) {
964 rc = -1;
965 goto exit;
968 memcpy(ad_entry(meta_ad, ADEID_FINDERI),
969 ad_entry(ad, ADEID_FINDERI),
970 ADEDLEN_FINDERI);
972 rc = ad_write(meta_ad, path);
973 if (rc != 0) {
974 rc = -1;
975 goto exit;
980 DEBUG(10, ("opened AppleDouble: %s\n", path));
982 exit:
983 if (rc != 0) {
984 saved_errno = errno;
985 len = -1;
987 if (opened && fd != -1) {
988 close(fd);
990 TALLOC_FREE(adpath);
991 TALLOC_FREE(meta_ad);
992 if (rc != 0) {
993 errno = saved_errno;
995 return len;
999 * Read and unpack an AppleDouble metadata xattr or resource
1001 static ssize_t ad_read(struct adouble *ad, const char *path)
1003 switch (ad->ad_type) {
1004 case ADOUBLE_META:
1005 return ad_header_read_meta(ad, path);
1006 case ADOUBLE_RSRC:
1007 return ad_header_read_rsrc(ad, path);
1008 default:
1009 return -1;
1014 * Allocate a struct adouble without initialiing it
1016 * The struct is either hang of the fsp extension context or if fsp is
1017 * NULL from ctx.
1019 * @param[in] ctx talloc context
1020 * @param[in] handle vfs handle
1021 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1023 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1024 * added as an fsp extension
1026 * @return adouble handle
1028 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1029 adouble_type_t type, files_struct *fsp)
1031 int rc = 0;
1032 size_t adsize = 0;
1033 struct adouble *ad;
1034 struct fruit_config_data *config;
1036 SMB_VFS_HANDLE_GET_DATA(handle, config,
1037 struct fruit_config_data, return NULL);
1039 switch (type) {
1040 case ADOUBLE_META:
1041 adsize = AD_DATASZ_XATTR;
1042 break;
1043 case ADOUBLE_RSRC:
1044 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1045 adsize = AD_DATASZ_DOT_UND;
1047 break;
1048 default:
1049 return NULL;
1052 if (!fsp) {
1053 ad = talloc_zero(ctx, struct adouble);
1054 if (ad == NULL) {
1055 rc = -1;
1056 goto exit;
1058 if (adsize) {
1059 ad->ad_data = talloc_zero_array(ad, char, adsize);
1061 } else {
1062 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1063 struct adouble,
1064 NULL);
1065 if (ad == NULL) {
1066 rc = -1;
1067 goto exit;
1069 if (adsize) {
1070 ad->ad_data = talloc_zero_array(
1071 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1072 char, adsize);
1074 ad->ad_fsp = fsp;
1077 if (adsize && ad->ad_data == NULL) {
1078 rc = -1;
1079 goto exit;
1081 ad->ad_handle = handle;
1082 ad->ad_type = type;
1083 ad->ad_magic = AD_MAGIC;
1084 ad->ad_version = AD_VERSION;
1086 exit:
1087 if (rc != 0) {
1088 TALLOC_FREE(ad);
1090 return ad;
1094 * Allocate and initialize a new struct adouble
1096 * @param[in] ctx talloc context
1097 * @param[in] handle vfs handle
1098 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1099 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1101 * @return adouble handle, initialized
1103 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1104 adouble_type_t type, files_struct *fsp)
1106 int rc = 0;
1107 const struct ad_entry_order *eid;
1108 struct adouble *ad = NULL;
1109 struct fruit_config_data *config;
1110 time_t t = time(NULL);
1112 SMB_VFS_HANDLE_GET_DATA(handle, config,
1113 struct fruit_config_data, return NULL);
1115 switch (type) {
1116 case ADOUBLE_META:
1117 eid = entry_order_meta_xattr;
1118 break;
1119 case ADOUBLE_RSRC:
1120 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1121 eid = entry_order_dot_und;
1122 } else {
1123 eid = entry_order_rsrc_xattr;
1125 break;
1126 default:
1127 return NULL;
1130 ad = ad_alloc(ctx, handle, type, fsp);
1131 if (ad == NULL) {
1132 return NULL;
1135 while (eid->id) {
1136 ad->ad_eid[eid->id].ade_off = eid->offset;
1137 ad->ad_eid[eid->id].ade_len = eid->len;
1138 eid++;
1141 /* put something sane in the date fields */
1142 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1143 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1144 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1145 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1147 if (rc != 0) {
1148 TALLOC_FREE(ad);
1150 return ad;
1154 * Return AppleDouble data for a file
1156 * @param[in] ctx talloc context
1157 * @param[in] handle vfs handle
1158 * @param[in] path pathname to file or directory
1159 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1161 * @return talloced struct adouble or NULL on error
1163 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1164 const char *path, adouble_type_t type)
1166 int rc = 0;
1167 ssize_t len;
1168 struct adouble *ad = NULL;
1170 DEBUG(10, ("ad_get(%s) called for %s\n",
1171 type == ADOUBLE_META ? "meta" : "rsrc", path));
1173 ad = ad_alloc(ctx, handle, type, NULL);
1174 if (ad == NULL) {
1175 rc = -1;
1176 goto exit;
1179 len = ad_read(ad, path);
1180 if (len == -1) {
1181 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1182 rc = -1;
1183 goto exit;
1186 exit:
1187 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1188 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1190 if (rc != 0) {
1191 TALLOC_FREE(ad);
1193 return ad;
1197 * Set AppleDouble metadata on a file or directory
1199 * @param[in] ad adouble handle
1201 * @param[in] path pathname to file or directory, may be NULL for a
1202 * resource fork
1204 * @return status code, 0 means success
1206 static int ad_write(struct adouble *ad, const char *path)
1208 int rc = 0;
1209 ssize_t len;
1211 rc = ad_pack(ad);
1212 if (rc != 0) {
1213 goto exit;
1216 switch (ad->ad_type) {
1217 case ADOUBLE_META:
1218 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1219 AFPINFO_EA_NETATALK, ad->ad_data,
1220 AD_DATASZ_XATTR, 0);
1221 break;
1222 case ADOUBLE_RSRC:
1223 if ((ad->ad_fsp == NULL)
1224 || (ad->ad_fsp->fh == NULL)
1225 || (ad->ad_fsp->fh->fd == -1)) {
1226 rc = -1;
1227 goto exit;
1229 /* FIXME: direct sys_pwrite(), don't have an fsp */
1230 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1231 talloc_get_size(ad->ad_data), 0);
1232 if (len != talloc_get_size(ad->ad_data)) {
1233 DEBUG(1, ("short write on %s: %zd",
1234 fsp_str_dbg(ad->ad_fsp), len));
1235 rc = -1;
1236 goto exit;
1238 break;
1239 default:
1240 return -1;
1242 exit:
1243 return rc;
1246 /*****************************************************************************
1247 * Helper functions
1248 *****************************************************************************/
1250 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1252 if (strncasecmp_m(smb_fname->stream_name,
1253 AFPINFO_STREAM_NAME,
1254 strlen(AFPINFO_STREAM_NAME)) == 0) {
1255 return true;
1257 return false;
1260 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1262 if (strncasecmp_m(smb_fname->stream_name,
1263 AFPRESOURCE_STREAM_NAME,
1264 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1265 return true;
1267 return false;
1271 * Test whether stream is an Apple stream, not used atm
1273 #if 0
1274 static bool is_apple_stream(const struct smb_filename *smb_fname)
1276 if (is_afpinfo_stream(smb_fname)) {
1277 return true;
1279 if (is_afpresource_stream(smb_fname)) {
1280 return true;
1282 return false;
1284 #endif
1287 * Initialize config struct from our smb.conf config parameters
1289 static int init_fruit_config(vfs_handle_struct *handle)
1291 struct fruit_config_data *config;
1292 int enumval;
1294 config = talloc_zero(handle->conn, struct fruit_config_data);
1295 if (!config) {
1296 DEBUG(1, ("talloc_zero() failed\n"));
1297 errno = ENOMEM;
1298 return -1;
1301 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1302 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1303 if (enumval == -1) {
1304 DEBUG(1, ("value for %s: ressource type unknown\n",
1305 FRUIT_PARAM_TYPE_NAME));
1306 return -1;
1308 config->rsrc = (enum fruit_rsrc)enumval;
1310 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1311 "metadata", fruit_meta, FRUIT_META_NETATALK);
1312 if (enumval == -1) {
1313 DEBUG(1, ("value for %s: metadata type unknown\n",
1314 FRUIT_PARAM_TYPE_NAME));
1315 return -1;
1317 config->meta = (enum fruit_meta)enumval;
1319 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1320 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1321 if (enumval == -1) {
1322 DEBUG(1, ("value for %s: locking type unknown\n",
1323 FRUIT_PARAM_TYPE_NAME));
1324 return -1;
1326 config->locking = (enum fruit_locking)enumval;
1328 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1329 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1330 if (enumval == -1) {
1331 DEBUG(1, ("value for %s: encoding type unknown\n",
1332 FRUIT_PARAM_TYPE_NAME));
1333 return -1;
1335 config->encoding = (enum fruit_encoding)enumval;
1337 if (lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "aapl", true)) {
1338 config->use_aapl = true;
1341 if (lp_parm_bool(SNUM(handle->conn),
1342 "readdir_attr", "aapl_rsize", true)) {
1343 config->readdir_attr_rsize = true;
1346 if (lp_parm_bool(SNUM(handle->conn),
1347 "readdir_attr", "aapl_finder_info", true)) {
1348 config->readdir_attr_finder_info = true;
1351 if (lp_parm_bool(SNUM(handle->conn),
1352 "readdir_attr", "aapl_max_access", true)) {
1353 config->readdir_attr_max_access = true;
1356 SMB_VFS_HANDLE_SET_DATA(handle, config,
1357 NULL, struct fruit_config_data,
1358 return -1);
1360 return 0;
1364 * Prepend "._" to a basename
1366 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1368 char *parent;
1369 const char *base;
1371 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1372 return -1;
1375 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1376 if (*path_out == NULL) {
1377 return -1;
1380 return 0;
1384 * Allocate and initialize an AfpInfo struct
1386 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1388 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1389 if (ai == NULL) {
1390 return NULL;
1392 ai->afpi_Signature = AFP_Signature;
1393 ai->afpi_Version = AFP_Version;
1394 ai->afpi_BackupTime = AD_DATE_START;
1395 return ai;
1399 * Pack an AfpInfo struct into a buffer
1401 * Buffer size must be at least AFP_INFO_SIZE
1402 * Returns size of packed buffer
1404 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1406 memset(buf, 0, AFP_INFO_SIZE);
1408 RSIVAL(buf, 0, ai->afpi_Signature);
1409 RSIVAL(buf, 4, ai->afpi_Version);
1410 RSIVAL(buf, 12, ai->afpi_BackupTime);
1411 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1413 return AFP_INFO_SIZE;
1417 * Unpack a buffer into a AfpInfo structure
1419 * Buffer size must be at least AFP_INFO_SIZE
1420 * Returns allocated AfpInfo struct
1422 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1424 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1425 if (ai == NULL) {
1426 return NULL;
1429 ai->afpi_Signature = RIVAL(data, 0);
1430 ai->afpi_Version = RIVAL(data, 4);
1431 ai->afpi_BackupTime = RIVAL(data, 12);
1432 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1433 sizeof(ai->afpi_FinderInfo));
1435 if (ai->afpi_Signature != AFP_Signature
1436 || ai->afpi_Version != AFP_Version) {
1437 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1438 TALLOC_FREE(ai);
1441 return ai;
1445 * Fake an inode number from the md5 hash of the (xattr) name
1447 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1449 MD5_CTX ctx;
1450 unsigned char hash[16];
1451 SMB_INO_T result;
1452 char *upper_sname;
1454 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1455 SMB_ASSERT(upper_sname != NULL);
1457 MD5Init(&ctx);
1458 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1459 sizeof(sbuf->st_ex_dev));
1460 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1461 sizeof(sbuf->st_ex_ino));
1462 MD5Update(&ctx, (unsigned char *)upper_sname,
1463 talloc_get_size(upper_sname)-1);
1464 MD5Final(hash, &ctx);
1466 TALLOC_FREE(upper_sname);
1468 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1469 memcpy(&result, hash, sizeof(result));
1471 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1472 sname, (unsigned long long)result));
1474 return result;
1478 * Ensure ad_fsp is still valid
1480 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1482 if (ad->ad_fsp == fsp) {
1483 return true;
1485 ad->ad_fsp = fsp;
1487 return true;
1490 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1491 struct stream_struct **streams,
1492 const char *name, off_t size,
1493 off_t alloc_size)
1495 struct stream_struct *tmp;
1497 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1498 (*num_streams)+1);
1499 if (tmp == NULL) {
1500 return false;
1503 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1504 if (tmp[*num_streams].name == NULL) {
1505 return false;
1508 tmp[*num_streams].size = size;
1509 tmp[*num_streams].alloc_size = alloc_size;
1511 *streams = tmp;
1512 *num_streams += 1;
1513 return true;
1516 static bool empty_finderinfo(const struct adouble *ad)
1519 char emptybuf[ADEDLEN_FINDERI] = {0};
1520 if (memcmp(emptybuf,
1521 ad_entry(ad, ADEID_FINDERI),
1522 ADEDLEN_FINDERI) == 0) {
1523 return true;
1525 return false;
1529 * Update btime with btime from Netatalk
1531 static void update_btime(vfs_handle_struct *handle,
1532 struct smb_filename *smb_fname)
1534 uint32_t t;
1535 struct timespec creation_time = {0};
1536 struct adouble *ad;
1538 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1539 if (ad == NULL) {
1540 return;
1542 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1543 TALLOC_FREE(ad);
1544 return;
1546 TALLOC_FREE(ad);
1548 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1549 update_stat_ex_create_time(&smb_fname->st, creation_time);
1551 return;
1555 * Map an access mask to a Netatalk single byte byte range lock
1557 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1558 uint32_t access_mask)
1560 off_t offset;
1562 switch (access_mask) {
1563 case FILE_READ_DATA:
1564 offset = AD_FILELOCK_OPEN_RD;
1565 break;
1567 case FILE_WRITE_DATA:
1568 case FILE_APPEND_DATA:
1569 offset = AD_FILELOCK_OPEN_WR;
1570 break;
1572 default:
1573 offset = AD_FILELOCK_OPEN_NONE;
1574 break;
1577 if (fork_type == APPLE_FORK_RSRC) {
1578 if (offset == AD_FILELOCK_OPEN_NONE) {
1579 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1580 } else {
1581 offset += 2;
1585 return offset;
1589 * Map a deny mode to a Netatalk brl
1591 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1592 uint32_t deny_mode)
1594 off_t offset;
1596 switch (deny_mode) {
1597 case DENY_READ:
1598 offset = AD_FILELOCK_DENY_RD;
1599 break;
1601 case DENY_WRITE:
1602 offset = AD_FILELOCK_DENY_WR;
1603 break;
1605 default:
1606 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1609 if (fork_type == APPLE_FORK_RSRC) {
1610 offset += 2;
1613 return offset;
1617 * Call fcntl() with an exclusive F_GETLK request in order to
1618 * determine if there's an exisiting shared lock
1620 * @return true if the requested lock was found or any error occured
1621 * false if the lock was not found
1623 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1625 bool result;
1626 off_t offset = in_offset;
1627 off_t len = 1;
1628 int type = F_WRLCK;
1629 pid_t pid;
1631 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1632 if (result == false) {
1633 return true;
1636 if (type != F_UNLCK) {
1637 return true;
1640 return false;
1643 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1644 files_struct *fsp,
1645 uint32_t access_mask,
1646 uint32_t deny_mode)
1648 NTSTATUS status = NT_STATUS_OK;
1649 struct byte_range_lock *br_lck = NULL;
1650 bool open_for_reading, open_for_writing, deny_read, deny_write;
1651 off_t off;
1653 /* FIXME: hardcoded data fork, add resource fork */
1654 enum apple_fork fork_type = APPLE_FORK_DATA;
1656 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1657 fsp_str_dbg(fsp),
1658 access_mask & FILE_READ_DATA ? "READ" :"-",
1659 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1660 deny_mode & DENY_READ ? "DENY_READ" : "-",
1661 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1664 * Check read access and deny read mode
1666 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1667 /* Check access */
1668 open_for_reading = test_netatalk_lock(
1669 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1671 deny_read = test_netatalk_lock(
1672 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1674 DEBUG(10, ("read: %s, deny_write: %s\n",
1675 open_for_reading == true ? "yes" : "no",
1676 deny_read == true ? "yes" : "no"));
1678 if (((access_mask & FILE_READ_DATA) && deny_read)
1679 || ((deny_mode & DENY_READ) && open_for_reading)) {
1680 return NT_STATUS_SHARING_VIOLATION;
1683 /* Set locks */
1684 if (access_mask & FILE_READ_DATA) {
1685 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1686 br_lck = do_lock(
1687 handle->conn->sconn->msg_ctx, fsp,
1688 fsp->op->global->open_persistent_id, 1, off,
1689 READ_LOCK, POSIX_LOCK, false,
1690 &status, NULL);
1692 if (!NT_STATUS_IS_OK(status)) {
1693 return status;
1695 TALLOC_FREE(br_lck);
1698 if (deny_mode & DENY_READ) {
1699 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1700 br_lck = do_lock(
1701 handle->conn->sconn->msg_ctx, fsp,
1702 fsp->op->global->open_persistent_id, 1, off,
1703 READ_LOCK, POSIX_LOCK, false,
1704 &status, NULL);
1706 if (!NT_STATUS_IS_OK(status)) {
1707 return status;
1709 TALLOC_FREE(br_lck);
1714 * Check write access and deny write mode
1716 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1717 /* Check access */
1718 open_for_writing = test_netatalk_lock(
1719 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1721 deny_write = test_netatalk_lock(
1722 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1724 DEBUG(10, ("write: %s, deny_write: %s\n",
1725 open_for_writing == true ? "yes" : "no",
1726 deny_write == true ? "yes" : "no"));
1728 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1729 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1730 return NT_STATUS_SHARING_VIOLATION;
1733 /* Set locks */
1734 if (access_mask & FILE_WRITE_DATA) {
1735 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1736 br_lck = do_lock(
1737 handle->conn->sconn->msg_ctx, fsp,
1738 fsp->op->global->open_persistent_id, 1, off,
1739 READ_LOCK, POSIX_LOCK, false,
1740 &status, NULL);
1742 if (!NT_STATUS_IS_OK(status)) {
1743 return status;
1745 TALLOC_FREE(br_lck);
1748 if (deny_mode & DENY_WRITE) {
1749 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1750 br_lck = do_lock(
1751 handle->conn->sconn->msg_ctx, fsp,
1752 fsp->op->global->open_persistent_id, 1, off,
1753 READ_LOCK, POSIX_LOCK, false,
1754 &status, NULL);
1756 if (!NT_STATUS_IS_OK(status)) {
1757 return status;
1759 TALLOC_FREE(br_lck);
1763 TALLOC_FREE(br_lck);
1765 return status;
1768 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1769 struct smb_request *req,
1770 const struct smb2_create_blobs *in_context_blobs,
1771 struct smb2_create_blobs *out_context_blobs)
1773 struct fruit_config_data *config;
1774 NTSTATUS status;
1775 struct smb2_create_blob *aapl = NULL;
1776 uint32_t cmd;
1777 bool ok;
1778 uint8_t p[16];
1779 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1780 uint64_t req_bitmap, client_caps;
1781 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1782 smb_ucs2_t *model;
1783 size_t modellen;
1785 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1786 return NT_STATUS_UNSUCCESSFUL);
1788 if (!config->use_aapl
1789 || in_context_blobs == NULL
1790 || out_context_blobs == NULL) {
1791 return NT_STATUS_OK;
1794 aapl = smb2_create_blob_find(in_context_blobs,
1795 SMB2_CREATE_TAG_AAPL);
1796 if (aapl == NULL) {
1797 return NT_STATUS_OK;
1800 if (aapl->data.length != 24) {
1801 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1802 (uintmax_t)aapl->data.length));
1803 return NT_STATUS_INVALID_PARAMETER;
1806 cmd = IVAL(aapl->data.data, 0);
1807 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1808 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1809 return NT_STATUS_INVALID_PARAMETER;
1812 req_bitmap = BVAL(aapl->data.data, 8);
1813 client_caps = BVAL(aapl->data.data, 16);
1815 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1816 SIVAL(p, 4, 0);
1817 SBVAL(p, 8, req_bitmap);
1818 ok = data_blob_append(req, &blob, p, 16);
1819 if (!ok) {
1820 return NT_STATUS_UNSUCCESSFUL;
1823 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1824 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1825 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1826 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1827 config->readdir_attr_enabled = true;
1831 * The client doesn't set the flag, so we can't check
1832 * for it and just set it unconditionally
1834 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1835 config->unix_info_enabled = true;
1837 SBVAL(p, 0, server_caps);
1838 ok = data_blob_append(req, &blob, p, 8);
1839 if (!ok) {
1840 return NT_STATUS_UNSUCCESSFUL;
1844 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1845 SBVAL(p, 0,
1846 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1847 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1848 ok = data_blob_append(req, &blob, p, 8);
1849 if (!ok) {
1850 return NT_STATUS_UNSUCCESSFUL;
1854 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1855 ok = convert_string_talloc(req,
1856 CH_UNIX, CH_UTF16LE,
1857 "Samba", strlen("Samba"),
1858 &model, &modellen);
1859 if (!ok) {
1860 return NT_STATUS_UNSUCCESSFUL;
1863 SIVAL(p, 0, 0);
1864 SIVAL(p + 4, 0, modellen);
1865 ok = data_blob_append(req, &blob, p, 8);
1866 if (!ok) {
1867 talloc_free(model);
1868 return NT_STATUS_UNSUCCESSFUL;
1871 ok = data_blob_append(req, &blob, model, modellen);
1872 talloc_free(model);
1873 if (!ok) {
1874 return NT_STATUS_UNSUCCESSFUL;
1878 status = smb2_create_blob_add(out_context_blobs,
1879 out_context_blobs,
1880 SMB2_CREATE_TAG_AAPL,
1881 blob);
1883 return status;
1886 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1887 const struct smb_filename *smb_fname,
1888 struct readdir_attr_data *attr_data)
1890 NTSTATUS status = NT_STATUS_OK;
1891 uint32_t date_added;
1892 struct adouble *ad = NULL;
1893 struct fruit_config_data *config = NULL;
1895 SMB_VFS_HANDLE_GET_DATA(handle, config,
1896 struct fruit_config_data,
1897 return NT_STATUS_UNSUCCESSFUL);
1900 /* Ensure we return a default value in the creation_date field */
1901 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1904 * Resource fork length
1907 if (config->readdir_attr_rsize) {
1908 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1909 ADOUBLE_RSRC);
1910 if (ad) {
1911 attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1912 ad, ADEID_RFORK);
1913 TALLOC_FREE(ad);
1918 * FinderInfo
1921 if (config->readdir_attr_finder_info) {
1922 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1923 ADOUBLE_META);
1924 if (ad) {
1925 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1926 /* finder_type */
1927 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1928 ad_entry(ad, ADEID_FINDERI), 4);
1930 /* finder_creator */
1931 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1932 ad_entry(ad, ADEID_FINDERI) + 4, 4);
1935 /* finder_flags */
1936 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1937 ad_entry(ad, ADEID_FINDERI) + 8, 2);
1939 /* finder_ext_flags */
1940 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1941 ad_entry(ad, ADEID_FINDERI) + 24, 2);
1943 /* creation date */
1944 date_added = convert_time_t_to_uint32_t(
1945 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1946 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1948 TALLOC_FREE(ad);
1952 TALLOC_FREE(ad);
1953 return status;
1956 /* Search MS NFS style ACE with UNIX mode */
1957 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1958 files_struct *fsp,
1959 const struct security_descriptor *psd,
1960 mode_t *pmode,
1961 bool *pdo_chmod)
1963 int i;
1964 struct fruit_config_data *config = NULL;
1966 *pdo_chmod = false;
1968 SMB_VFS_HANDLE_GET_DATA(handle, config,
1969 struct fruit_config_data,
1970 return NT_STATUS_UNSUCCESSFUL);
1972 if (psd->dacl == NULL || !config->unix_info_enabled) {
1973 return NT_STATUS_OK;
1976 for (i = 0; i < psd->dacl->num_aces; i++) {
1977 if (dom_sid_compare_domain(
1978 &global_sid_Unix_NFS_Mode,
1979 &psd->dacl->aces[i].trustee) == 0) {
1980 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1981 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1982 *pdo_chmod = true;
1984 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1985 fsp_str_dbg(fsp), *pmode));
1986 break;
1990 return NT_STATUS_OK;
1993 /****************************************************************************
1994 * VFS ops
1995 ****************************************************************************/
1997 static int fruit_connect(vfs_handle_struct *handle,
1998 const char *service,
1999 const char *user)
2001 int rc;
2002 char *list = NULL, *newlist = NULL;
2003 struct fruit_config_data *config;
2005 DEBUG(10, ("fruit_connect\n"));
2007 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2008 if (rc < 0) {
2009 return rc;
2012 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2014 if (list) {
2015 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2016 newlist = talloc_asprintf(
2017 list,
2018 "%s/" ADOUBLE_NAME_PREFIX "*/",
2019 list);
2020 lp_do_parameter(SNUM(handle->conn),
2021 "veto files",
2022 newlist);
2024 } else {
2025 lp_do_parameter(SNUM(handle->conn),
2026 "veto files",
2027 "/" ADOUBLE_NAME_PREFIX "*/");
2030 TALLOC_FREE(list);
2032 rc = init_fruit_config(handle);
2033 if (rc != 0) {
2034 return rc;
2037 SMB_VFS_HANDLE_GET_DATA(handle, config,
2038 struct fruit_config_data, return -1);
2040 if (config->encoding == FRUIT_ENC_NATIVE) {
2041 lp_do_parameter(
2042 SNUM(handle->conn),
2043 "catia:mappings",
2044 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2045 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2046 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2047 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2048 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2049 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2050 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2051 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2052 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2053 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2054 "0x0d:0xf00d");
2057 return rc;
2060 static int fruit_open_meta(vfs_handle_struct *handle,
2061 struct smb_filename *smb_fname,
2062 files_struct *fsp, int flags, mode_t mode)
2064 int rc = 0;
2065 struct fruit_config_data *config = NULL;
2066 struct smb_filename *smb_fname_base = NULL;
2067 int baseflags;
2068 int hostfd = -1;
2069 struct adouble *ad = NULL;
2071 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
2073 SMB_VFS_HANDLE_GET_DATA(handle, config,
2074 struct fruit_config_data, return -1);
2076 if (config->meta == FRUIT_META_STREAM) {
2077 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2080 /* Create an smb_filename with stream_name == NULL. */
2081 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2082 smb_fname->base_name, NULL, NULL);
2084 if (smb_fname_base == NULL) {
2085 errno = ENOMEM;
2086 rc = -1;
2087 goto exit;
2091 * We use baseflags to turn off nasty side-effects when opening the
2092 * underlying file.
2094 baseflags = flags;
2095 baseflags &= ~O_TRUNC;
2096 baseflags &= ~O_EXCL;
2097 baseflags &= ~O_CREAT;
2099 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2100 baseflags, mode);
2103 * It is legit to open a stream on a directory, but the base
2104 * fd has to be read-only.
2106 if ((hostfd == -1) && (errno == EISDIR)) {
2107 baseflags &= ~O_ACCMODE;
2108 baseflags |= O_RDONLY;
2109 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2110 baseflags, mode);
2113 TALLOC_FREE(smb_fname_base);
2115 if (hostfd == -1) {
2116 rc = -1;
2117 goto exit;
2120 if (flags & (O_CREAT | O_TRUNC)) {
2122 * The attribute does not exist or needs to be truncated,
2123 * create an AppleDouble EA
2125 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2126 handle, ADOUBLE_META, fsp);
2127 if (ad == NULL) {
2128 rc = -1;
2129 goto exit;
2132 rc = ad_write(ad, smb_fname->base_name);
2133 if (rc != 0) {
2134 rc = -1;
2135 goto exit;
2137 } else {
2138 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2139 handle, ADOUBLE_META, fsp);
2140 if (ad == NULL) {
2141 rc = -1;
2142 goto exit;
2144 if (ad_read(ad, smb_fname->base_name) == -1) {
2145 rc = -1;
2146 goto exit;
2150 exit:
2151 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2152 if (rc != 0) {
2153 int saved_errno = errno;
2154 if (hostfd >= 0) {
2156 * BUGBUGBUG -- we would need to call
2157 * fd_close_posix here, but we don't have a
2158 * full fsp yet
2160 fsp->fh->fd = hostfd;
2161 SMB_VFS_CLOSE(fsp);
2163 hostfd = -1;
2164 errno = saved_errno;
2166 return hostfd;
2169 static int fruit_open_rsrc(vfs_handle_struct *handle,
2170 struct smb_filename *smb_fname,
2171 files_struct *fsp, int flags, mode_t mode)
2173 int rc = 0;
2174 struct fruit_config_data *config = NULL;
2175 struct adouble *ad = NULL;
2176 struct smb_filename *smb_fname_base = NULL;
2177 char *adpath = NULL;
2178 int hostfd = -1;
2180 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2182 SMB_VFS_HANDLE_GET_DATA(handle, config,
2183 struct fruit_config_data, return -1);
2185 switch (config->rsrc) {
2186 case FRUIT_RSRC_STREAM:
2187 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2188 case FRUIT_RSRC_XATTR:
2189 #ifdef HAVE_ATTROPEN
2190 hostfd = attropen(smb_fname->base_name,
2191 AFPRESOURCE_EA_NETATALK, flags, mode);
2192 if (hostfd == -1) {
2193 return -1;
2195 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2196 handle, ADOUBLE_RSRC, fsp);
2197 if (ad == NULL) {
2198 rc = -1;
2199 goto exit;
2201 goto exit;
2202 #else
2203 errno = ENOTSUP;
2204 return -1;
2205 #endif
2206 default:
2207 break;
2210 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2211 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2212 if (rc != 0) {
2213 rc = -1;
2214 goto exit;
2218 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2219 /* sorry, but directories don't habe a resource fork */
2220 rc = -1;
2221 goto exit;
2224 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2225 if (rc != 0) {
2226 goto exit;
2229 /* Create an smb_filename with stream_name == NULL. */
2230 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2231 adpath, NULL, NULL);
2232 if (smb_fname_base == NULL) {
2233 errno = ENOMEM;
2234 rc = -1;
2235 goto exit;
2238 /* Sanitize flags */
2239 if (flags & O_WRONLY) {
2240 /* We always need read access for the metadata header too */
2241 flags &= ~O_WRONLY;
2242 flags |= O_RDWR;
2245 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2246 flags, mode);
2247 if (hostfd == -1) {
2248 rc = -1;
2249 goto exit;
2252 /* REVIEW: we need this in ad_write() */
2253 fsp->fh->fd = hostfd;
2255 if (flags & (O_CREAT | O_TRUNC)) {
2256 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2257 handle, ADOUBLE_RSRC, fsp);
2258 if (ad == NULL) {
2259 rc = -1;
2260 goto exit;
2262 rc = ad_write(ad, smb_fname->base_name);
2263 if (rc != 0) {
2264 rc = -1;
2265 goto exit;
2267 } else {
2268 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2269 handle, ADOUBLE_RSRC, fsp);
2270 if (ad == NULL) {
2271 rc = -1;
2272 goto exit;
2274 if (ad_read(ad, smb_fname->base_name) == -1) {
2275 rc = -1;
2276 goto exit;
2280 exit:
2282 TALLOC_FREE(adpath);
2283 TALLOC_FREE(smb_fname_base);
2285 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2286 if (rc != 0) {
2287 int saved_errno = errno;
2288 if (hostfd >= 0) {
2290 * BUGBUGBUG -- we would need to call
2291 * fd_close_posix here, but we don't have a
2292 * full fsp yet
2294 fsp->fh->fd = hostfd;
2295 SMB_VFS_CLOSE(fsp);
2297 hostfd = -1;
2298 errno = saved_errno;
2300 return hostfd;
2303 static int fruit_open(vfs_handle_struct *handle,
2304 struct smb_filename *smb_fname,
2305 files_struct *fsp, int flags, mode_t mode)
2307 DEBUG(10, ("fruit_open called for %s\n",
2308 smb_fname_str_dbg(smb_fname)));
2310 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2311 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2314 if (is_afpinfo_stream(smb_fname)) {
2315 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2316 } else if (is_afpresource_stream(smb_fname)) {
2317 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2320 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2323 static int fruit_rename(struct vfs_handle_struct *handle,
2324 const struct smb_filename *smb_fname_src,
2325 const struct smb_filename *smb_fname_dst)
2327 int rc = -1;
2328 char *src_adouble_path = NULL;
2329 char *dst_adouble_path = NULL;
2330 struct fruit_config_data *config = NULL;
2332 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2334 if (!VALID_STAT(smb_fname_src->st)
2335 || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2336 return rc;
2339 SMB_VFS_HANDLE_GET_DATA(handle, config,
2340 struct fruit_config_data, return -1);
2342 if (config->rsrc == FRUIT_RSRC_XATTR) {
2343 return rc;
2346 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2347 &src_adouble_path);
2348 if (rc != 0) {
2349 goto done;
2351 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2352 &dst_adouble_path);
2353 if (rc != 0) {
2354 goto done;
2357 DEBUG(10, ("fruit_rename: %s -> %s\n",
2358 src_adouble_path, dst_adouble_path));
2360 rc = rename(src_adouble_path, dst_adouble_path);
2361 if (errno == ENOENT) {
2362 rc = 0;
2365 TALLOC_FREE(src_adouble_path);
2366 TALLOC_FREE(dst_adouble_path);
2368 done:
2369 return rc;
2372 static int fruit_unlink(vfs_handle_struct *handle,
2373 const struct smb_filename *smb_fname)
2375 int rc = -1;
2376 struct fruit_config_data *config = NULL;
2377 char *adp = NULL;
2379 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2380 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2383 SMB_VFS_HANDLE_GET_DATA(handle, config,
2384 struct fruit_config_data, return -1);
2386 if (is_afpinfo_stream(smb_fname)) {
2387 if (config->meta == FRUIT_META_STREAM) {
2388 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2389 } else {
2390 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2391 smb_fname->base_name,
2392 AFPINFO_EA_NETATALK);
2394 } else if (is_afpresource_stream(smb_fname)) {
2395 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2396 rc = adouble_path(talloc_tos(),
2397 smb_fname->base_name, &adp);
2398 if (rc != 0) {
2399 return -1;
2401 /* FIXME: direct unlink(), missing smb_fname */
2402 rc = unlink(adp);
2403 if ((rc == -1) && (errno == ENOENT)) {
2404 rc = 0;
2406 } else {
2407 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2408 smb_fname->base_name,
2409 AFPRESOURCE_EA_NETATALK);
2411 } else {
2412 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2415 TALLOC_FREE(adp);
2416 return rc;
2419 static int fruit_chmod(vfs_handle_struct *handle,
2420 const char *path,
2421 mode_t mode)
2423 int rc = -1;
2424 char *adp = NULL;
2425 struct fruit_config_data *config = NULL;
2426 SMB_STRUCT_STAT sb;
2428 rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2429 if (rc != 0) {
2430 return rc;
2433 SMB_VFS_HANDLE_GET_DATA(handle, config,
2434 struct fruit_config_data, return -1);
2436 if (config->rsrc == FRUIT_RSRC_XATTR) {
2437 return 0;
2440 /* FIXME: direct sys_lstat(), missing smb_fname */
2441 rc = sys_lstat(path, &sb, false);
2442 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2443 return rc;
2446 rc = adouble_path(talloc_tos(), path, &adp);
2447 if (rc != 0) {
2448 return -1;
2451 DEBUG(10, ("fruit_chmod: %s\n", adp));
2453 rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2454 if (errno == ENOENT) {
2455 rc = 0;
2458 TALLOC_FREE(adp);
2459 return rc;
2462 static int fruit_chown(vfs_handle_struct *handle,
2463 const char *path,
2464 uid_t uid,
2465 gid_t gid)
2467 int rc = -1;
2468 char *adp = NULL;
2469 struct fruit_config_data *config = NULL;
2470 SMB_STRUCT_STAT sb;
2472 rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2473 if (rc != 0) {
2474 return rc;
2477 SMB_VFS_HANDLE_GET_DATA(handle, config,
2478 struct fruit_config_data, return -1);
2480 if (config->rsrc == FRUIT_RSRC_XATTR) {
2481 return rc;
2484 /* FIXME: direct sys_lstat(), missing smb_fname */
2485 rc = sys_lstat(path, &sb, false);
2486 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2487 return rc;
2490 rc = adouble_path(talloc_tos(), path, &adp);
2491 if (rc != 0) {
2492 goto done;
2495 DEBUG(10, ("fruit_chown: %s\n", adp));
2497 rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2498 if (errno == ENOENT) {
2499 rc = 0;
2502 done:
2503 TALLOC_FREE(adp);
2504 return rc;
2507 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2509 DIR *dh = NULL;
2510 struct dirent *de;
2511 struct fruit_config_data *config;
2513 SMB_VFS_HANDLE_GET_DATA(handle, config,
2514 struct fruit_config_data, return -1);
2516 if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2517 goto exit_rmdir;
2521 * Due to there is no way to change bDeleteVetoFiles variable
2522 * from this module, need to clean up ourselves
2524 dh = opendir(path);
2525 if (dh == NULL) {
2526 goto exit_rmdir;
2529 while ((de = readdir(dh)) != NULL) {
2530 if ((strncmp(de->d_name,
2531 ADOUBLE_NAME_PREFIX,
2532 strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2533 char *p = talloc_asprintf(talloc_tos(),
2534 "%s/%s",
2535 path, de->d_name);
2536 if (p == NULL) {
2537 goto exit_rmdir;
2539 DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2540 (void)unlink(p);
2541 TALLOC_FREE(p);
2545 exit_rmdir:
2546 if (dh) {
2547 closedir(dh);
2549 return SMB_VFS_NEXT_RMDIR(handle, path);
2552 static ssize_t fruit_pread(vfs_handle_struct *handle,
2553 files_struct *fsp, void *data,
2554 size_t n, off_t offset)
2556 int rc = 0;
2557 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2558 handle, fsp);
2559 struct fruit_config_data *config = NULL;
2560 AfpInfo *ai = NULL;
2561 ssize_t len;
2562 char *name = NULL;
2563 char *tmp_base_name = NULL;
2564 NTSTATUS status;
2566 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2568 if (!fsp->base_fsp) {
2569 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2572 SMB_VFS_HANDLE_GET_DATA(handle, config,
2573 struct fruit_config_data, return -1);
2575 /* fsp_name is not converted with vfs_catia */
2576 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2577 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2578 fsp->base_fsp->fsp_name->base_name,
2579 vfs_translate_to_unix,
2580 talloc_tos(), &name);
2581 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2582 name = talloc_strdup(talloc_tos(), tmp_base_name);
2583 if (name == NULL) {
2584 rc = -1;
2585 goto exit;
2587 } else if (!NT_STATUS_IS_OK(status)) {
2588 errno = map_errno_from_nt_status(status);
2589 rc = -1;
2590 goto exit;
2592 fsp->base_fsp->fsp_name->base_name = name;
2594 if (ad == NULL) {
2595 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2596 if (len == -1) {
2597 rc = -1;
2598 goto exit;
2600 goto exit;
2603 if (!fruit_fsp_recheck(ad, fsp)) {
2604 rc = -1;
2605 goto exit;
2608 if (ad->ad_type == ADOUBLE_META) {
2609 ai = afpinfo_new(talloc_tos());
2610 if (ai == NULL) {
2611 rc = -1;
2612 goto exit;
2615 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2616 if (len == -1) {
2617 rc = -1;
2618 goto exit;
2621 memcpy(&ai->afpi_FinderInfo[0],
2622 ad_entry(ad, ADEID_FINDERI),
2623 ADEDLEN_FINDERI);
2624 len = afpinfo_pack(ai, data);
2625 if (len != AFP_INFO_SIZE) {
2626 rc = -1;
2627 goto exit;
2629 } else {
2630 len = SMB_VFS_NEXT_PREAD(
2631 handle, fsp, data, n,
2632 offset + ad_getentryoff(ad, ADEID_RFORK));
2633 if (len == -1) {
2634 rc = -1;
2635 goto exit;
2638 exit:
2639 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2640 TALLOC_FREE(name);
2641 TALLOC_FREE(ai);
2642 if (rc != 0) {
2643 len = -1;
2645 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2646 return len;
2649 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2650 files_struct *fsp, const void *data,
2651 size_t n, off_t offset)
2653 int rc = 0;
2654 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2655 handle, fsp);
2656 struct fruit_config_data *config = NULL;
2657 AfpInfo *ai = NULL;
2658 ssize_t len;
2659 char *name = NULL;
2660 char *tmp_base_name = NULL;
2661 NTSTATUS status;
2663 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2665 if (!fsp->base_fsp) {
2666 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2669 SMB_VFS_HANDLE_GET_DATA(handle, config,
2670 struct fruit_config_data, return -1);
2672 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2673 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2674 fsp->base_fsp->fsp_name->base_name,
2675 vfs_translate_to_unix,
2676 talloc_tos(), &name);
2677 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2678 name = talloc_strdup(talloc_tos(), tmp_base_name);
2679 if (name == NULL) {
2680 rc = -1;
2681 goto exit;
2683 } else if (!NT_STATUS_IS_OK(status)) {
2684 errno = map_errno_from_nt_status(status);
2685 rc = -1;
2686 goto exit;
2688 fsp->base_fsp->fsp_name->base_name = name;
2690 if (ad == NULL) {
2691 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2692 if (len != n) {
2693 rc = -1;
2694 goto exit;
2696 goto exit;
2699 if (!fruit_fsp_recheck(ad, fsp)) {
2700 rc = -1;
2701 goto exit;
2704 if (ad->ad_type == ADOUBLE_META) {
2705 if (n != AFP_INFO_SIZE || offset != 0) {
2706 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2707 (intmax_t)offset, (intmax_t)n));
2708 rc = -1;
2709 goto exit;
2711 ai = afpinfo_unpack(talloc_tos(), data);
2712 if (ai == NULL) {
2713 rc = -1;
2714 goto exit;
2716 memcpy(ad_entry(ad, ADEID_FINDERI),
2717 &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2718 rc = ad_write(ad, name);
2719 } else {
2720 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2721 offset + ad_getentryoff(ad, ADEID_RFORK));
2722 if (len != n) {
2723 rc = -1;
2724 goto exit;
2727 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2728 rc = ad_read(ad, name);
2729 if (rc == -1) {
2730 goto exit;
2732 rc = 0;
2734 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2735 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2736 rc = ad_write(ad, name);
2741 exit:
2742 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2743 TALLOC_FREE(name);
2744 TALLOC_FREE(ai);
2745 if (rc != 0) {
2746 return -1;
2748 return n;
2752 * Helper to stat/lstat the base file of an smb_fname.
2754 static int fruit_stat_base(vfs_handle_struct *handle,
2755 struct smb_filename *smb_fname,
2756 bool follow_links)
2758 char *tmp_stream_name;
2759 int rc;
2761 tmp_stream_name = smb_fname->stream_name;
2762 smb_fname->stream_name = NULL;
2763 if (follow_links) {
2764 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2765 } else {
2766 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2768 smb_fname->stream_name = tmp_stream_name;
2769 return rc;
2772 static int fruit_stat_meta(vfs_handle_struct *handle,
2773 struct smb_filename *smb_fname,
2774 bool follow_links)
2776 /* Populate the stat struct with info from the base file. */
2777 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2778 return -1;
2780 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2781 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2782 smb_fname->stream_name);
2783 return 0;
2786 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2787 struct smb_filename *smb_fname,
2788 bool follow_links)
2791 struct adouble *ad = NULL;
2793 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2794 smb_fname_str_dbg(smb_fname)));
2796 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2797 if (ad == NULL) {
2798 errno = ENOENT;
2799 return -1;
2802 /* Populate the stat struct with info from the base file. */
2803 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2804 TALLOC_FREE(ad);
2805 return -1;
2808 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2809 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2810 smb_fname->stream_name);
2811 TALLOC_FREE(ad);
2812 return 0;
2815 static int fruit_stat(vfs_handle_struct *handle,
2816 struct smb_filename *smb_fname)
2818 int rc = -1;
2820 DEBUG(10, ("fruit_stat called for %s\n",
2821 smb_fname_str_dbg(smb_fname)));
2823 if (!is_ntfs_stream_smb_fname(smb_fname)
2824 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2825 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2826 if (rc == 0) {
2827 update_btime(handle, smb_fname);
2829 return rc;
2833 * Note if lp_posix_paths() is true, we can never
2834 * get here as is_ntfs_stream_smb_fname() is
2835 * always false. So we never need worry about
2836 * not following links here.
2839 if (is_afpinfo_stream(smb_fname)) {
2840 rc = fruit_stat_meta(handle, smb_fname, true);
2841 } else if (is_afpresource_stream(smb_fname)) {
2842 rc = fruit_stat_rsrc(handle, smb_fname, true);
2843 } else {
2844 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2847 if (rc == 0) {
2848 update_btime(handle, smb_fname);
2849 smb_fname->st.st_ex_mode &= ~S_IFMT;
2850 smb_fname->st.st_ex_mode |= S_IFREG;
2851 smb_fname->st.st_ex_blocks =
2852 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2854 return rc;
2857 static int fruit_lstat(vfs_handle_struct *handle,
2858 struct smb_filename *smb_fname)
2860 int rc = -1;
2862 DEBUG(10, ("fruit_lstat called for %s\n",
2863 smb_fname_str_dbg(smb_fname)));
2865 if (!is_ntfs_stream_smb_fname(smb_fname)
2866 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2867 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2868 if (rc == 0) {
2869 update_btime(handle, smb_fname);
2871 return rc;
2874 if (is_afpinfo_stream(smb_fname)) {
2875 rc = fruit_stat_meta(handle, smb_fname, false);
2876 } else if (is_afpresource_stream(smb_fname)) {
2877 rc = fruit_stat_rsrc(handle, smb_fname, false);
2878 } else {
2879 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2882 if (rc == 0) {
2883 update_btime(handle, smb_fname);
2884 smb_fname->st.st_ex_mode &= ~S_IFMT;
2885 smb_fname->st.st_ex_mode |= S_IFREG;
2886 smb_fname->st.st_ex_blocks =
2887 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2889 return rc;
2892 static int fruit_fstat_meta(vfs_handle_struct *handle,
2893 files_struct *fsp,
2894 SMB_STRUCT_STAT *sbuf)
2896 DEBUG(10, ("fruit_fstat_meta called for %s\n",
2897 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2899 /* Populate the stat struct with info from the base file. */
2900 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2901 return -1;
2903 *sbuf = fsp->base_fsp->fsp_name->st;
2904 sbuf->st_ex_size = AFP_INFO_SIZE;
2905 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2907 return 0;
2910 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
2911 SMB_STRUCT_STAT *sbuf)
2913 struct fruit_config_data *config;
2914 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2915 handle, fsp);
2917 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
2918 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2920 SMB_VFS_HANDLE_GET_DATA(handle, config,
2921 struct fruit_config_data, return -1);
2923 if (config->rsrc == FRUIT_RSRC_STREAM) {
2924 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2927 /* Populate the stat struct with info from the base file. */
2928 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2929 return -1;
2931 *sbuf = fsp->base_fsp->fsp_name->st;
2932 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2933 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2935 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
2936 smb_fname_str_dbg(fsp->fsp_name),
2937 (ssize_t)sbuf->st_ex_size));
2939 return 0;
2942 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
2943 SMB_STRUCT_STAT *sbuf)
2945 int rc;
2946 char *name = NULL;
2947 char *tmp_base_name = NULL;
2948 NTSTATUS status;
2949 struct adouble *ad = (struct adouble *)
2950 VFS_FETCH_FSP_EXTENSION(handle, fsp);
2952 DEBUG(10, ("fruit_fstat called for %s\n",
2953 smb_fname_str_dbg(fsp->fsp_name)));
2955 if (fsp->base_fsp) {
2956 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2957 /* fsp_name is not converted with vfs_catia */
2958 status = SMB_VFS_TRANSLATE_NAME(
2959 handle->conn,
2960 fsp->base_fsp->fsp_name->base_name,
2961 vfs_translate_to_unix,
2962 talloc_tos(), &name);
2964 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2965 name = talloc_strdup(talloc_tos(), tmp_base_name);
2966 if (name == NULL) {
2967 rc = -1;
2968 goto exit;
2970 } else if (!NT_STATUS_IS_OK(status)) {
2971 errno = map_errno_from_nt_status(status);
2972 rc = -1;
2973 goto exit;
2975 fsp->base_fsp->fsp_name->base_name = name;
2978 if (ad == NULL || fsp->base_fsp == NULL) {
2979 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2980 goto exit;
2983 if (!fruit_fsp_recheck(ad, fsp)) {
2984 rc = -1;
2985 goto exit;
2988 switch (ad->ad_type) {
2989 case ADOUBLE_META:
2990 rc = fruit_fstat_meta(handle, fsp, sbuf);
2991 break;
2992 case ADOUBLE_RSRC:
2993 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
2994 break;
2995 default:
2996 DEBUG(10, ("fruit_fstat %s: bad type\n",
2997 smb_fname_str_dbg(fsp->fsp_name)));
2998 rc = -1;
2999 goto exit;
3002 if (rc == 0) {
3003 sbuf->st_ex_mode &= ~S_IFMT;
3004 sbuf->st_ex_mode |= S_IFREG;
3005 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3008 exit:
3009 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3010 smb_fname_str_dbg(fsp->fsp_name),
3011 (ssize_t)sbuf->st_ex_size));
3012 if (tmp_base_name) {
3013 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3015 TALLOC_FREE(name);
3016 return rc;
3019 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3020 struct files_struct *fsp,
3021 const char *fname,
3022 TALLOC_CTX *mem_ctx,
3023 unsigned int *pnum_streams,
3024 struct stream_struct **pstreams)
3026 struct fruit_config_data *config = NULL;
3027 struct smb_filename *smb_fname = NULL;
3028 struct adouble *ad = NULL;
3030 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3031 return NT_STATUS_UNSUCCESSFUL);
3032 DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
3034 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
3035 if (smb_fname == NULL) {
3036 return NT_STATUS_NO_MEMORY;
3039 if (config->meta == FRUIT_META_NETATALK) {
3040 ad = ad_get(talloc_tos(), handle,
3041 smb_fname->base_name, ADOUBLE_META);
3042 if (ad && !empty_finderinfo(ad)) {
3043 if (!add_fruit_stream(
3044 mem_ctx, pnum_streams, pstreams,
3045 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3046 smb_roundup(handle->conn,
3047 AFP_INFO_SIZE))) {
3048 TALLOC_FREE(ad);
3049 TALLOC_FREE(smb_fname);
3050 return NT_STATUS_NO_MEMORY;
3053 TALLOC_FREE(ad);
3056 if (config->rsrc != FRUIT_RSRC_STREAM) {
3057 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3058 ADOUBLE_RSRC);
3059 if (ad) {
3060 if (!add_fruit_stream(
3061 mem_ctx, pnum_streams, pstreams,
3062 AFPRESOURCE_STREAM_NAME,
3063 ad_getentrylen(ad, ADEID_RFORK),
3064 smb_roundup(handle->conn,
3065 ad_getentrylen(
3066 ad, ADEID_RFORK)))) {
3067 TALLOC_FREE(ad);
3068 TALLOC_FREE(smb_fname);
3069 return NT_STATUS_NO_MEMORY;
3072 TALLOC_FREE(ad);
3075 TALLOC_FREE(smb_fname);
3077 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3078 pnum_streams, pstreams);
3081 static int fruit_ntimes(vfs_handle_struct *handle,
3082 const struct smb_filename *smb_fname,
3083 struct smb_file_time *ft)
3085 int rc = 0;
3086 struct adouble *ad = NULL;
3088 if (null_timespec(ft->create_time)) {
3089 goto exit;
3092 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3093 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3095 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3096 if (ad == NULL) {
3097 goto exit;
3100 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3101 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3103 rc = ad_write(ad, smb_fname->base_name);
3105 exit:
3107 TALLOC_FREE(ad);
3108 if (rc != 0) {
3109 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3110 return -1;
3112 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3115 static int fruit_fallocate(struct vfs_handle_struct *handle,
3116 struct files_struct *fsp,
3117 uint32_t mode,
3118 off_t offset,
3119 off_t len)
3121 struct adouble *ad =
3122 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3124 if (ad == NULL) {
3125 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3128 if (!fruit_fsp_recheck(ad, fsp)) {
3129 return -1;
3132 /* Let the pwrite code path handle it. */
3133 errno = ENOSYS;
3134 return -1;
3137 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3138 struct files_struct *fsp,
3139 off_t offset)
3141 int rc = 0;
3142 struct adouble *ad =
3143 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3144 struct fruit_config_data *config;
3146 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3147 fsp_str_dbg(fsp), (double)offset));
3149 if (ad == NULL) {
3150 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3153 if (!fruit_fsp_recheck(ad, fsp)) {
3154 return -1;
3157 SMB_VFS_HANDLE_GET_DATA(handle, config,
3158 struct fruit_config_data, return -1);
3160 switch (ad->ad_type) {
3161 case ADOUBLE_META:
3163 * As this request hasn't been seen in the wild,
3164 * the only sensible use I can imagine is the client
3165 * truncating the stream to 0 bytes size.
3166 * We simply remove the metadata on such a request.
3168 if (offset == 0) {
3169 rc = SMB_VFS_FREMOVEXATTR(fsp,
3170 AFPRESOURCE_EA_NETATALK);
3172 break;
3173 case ADOUBLE_RSRC:
3174 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3175 rc = SMB_VFS_FREMOVEXATTR(fsp,
3176 AFPRESOURCE_EA_NETATALK);
3177 } else {
3178 rc = SMB_VFS_NEXT_FTRUNCATE(
3179 handle, fsp,
3180 offset + ad_getentryoff(ad, ADEID_RFORK));
3181 if (rc != 0) {
3182 return -1;
3184 ad_setentrylen(ad, ADEID_RFORK, offset);
3185 rc = ad_write(ad, NULL);
3186 if (rc != 0) {
3187 return -1;
3190 break;
3191 default:
3192 return -1;
3195 return rc;
3198 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3199 struct smb_request *req,
3200 uint16_t root_dir_fid,
3201 struct smb_filename *smb_fname,
3202 uint32_t access_mask,
3203 uint32_t share_access,
3204 uint32_t create_disposition,
3205 uint32_t create_options,
3206 uint32_t file_attributes,
3207 uint32_t oplock_request,
3208 struct smb2_lease *lease,
3209 uint64_t allocation_size,
3210 uint32_t private_flags,
3211 struct security_descriptor *sd,
3212 struct ea_list *ea_list,
3213 files_struct **result,
3214 int *pinfo,
3215 const struct smb2_create_blobs *in_context_blobs,
3216 struct smb2_create_blobs *out_context_blobs)
3218 NTSTATUS status;
3219 struct fruit_config_data *config = NULL;
3221 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3222 if (!NT_STATUS_IS_OK(status)) {
3223 return status;
3226 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3227 return NT_STATUS_UNSUCCESSFUL);
3229 status = SMB_VFS_NEXT_CREATE_FILE(
3230 handle, req, root_dir_fid, smb_fname,
3231 access_mask, share_access,
3232 create_disposition, create_options,
3233 file_attributes, oplock_request,
3234 lease,
3235 allocation_size, private_flags,
3236 sd, ea_list, result,
3237 pinfo, in_context_blobs, out_context_blobs);
3238 if (!NT_STATUS_IS_OK(status)) {
3239 return status;
3242 if (is_ntfs_stream_smb_fname(smb_fname)
3243 || (*result == NULL)
3244 || ((*result)->is_directory)) {
3245 return status;
3248 if (config->locking == FRUIT_LOCKING_NETATALK) {
3249 status = fruit_check_access(
3250 handle, *result,
3251 access_mask,
3252 map_share_mode_to_deny_mode(share_access, 0));
3253 if (!NT_STATUS_IS_OK(status)) {
3254 goto fail;
3258 return status;
3260 fail:
3261 DEBUG(1, ("fruit_create_file: %s\n", nt_errstr(status)));
3263 if (*result) {
3264 close_file(req, *result, ERROR_CLOSE);
3265 *result = NULL;
3268 return status;
3271 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3272 const struct smb_filename *fname,
3273 TALLOC_CTX *mem_ctx,
3274 struct readdir_attr_data **pattr_data)
3276 struct fruit_config_data *config = NULL;
3277 struct readdir_attr_data *attr_data;
3278 NTSTATUS status;
3280 SMB_VFS_HANDLE_GET_DATA(handle, config,
3281 struct fruit_config_data,
3282 return NT_STATUS_UNSUCCESSFUL);
3284 if (!config->use_aapl) {
3285 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3288 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3290 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3291 if (*pattr_data == NULL) {
3292 return NT_STATUS_UNSUCCESSFUL;
3294 attr_data = *pattr_data;
3295 attr_data->type = RDATTR_AAPL;
3298 * Mac metadata: compressed FinderInfo, resource fork length
3299 * and creation date
3301 status = readdir_attr_macmeta(handle, fname, attr_data);
3302 if (!NT_STATUS_IS_OK(status)) {
3304 * Error handling is tricky: if we return failure from
3305 * this function, the corresponding directory entry
3306 * will to be passed to the client, so we really just
3307 * want to error out on fatal errors.
3309 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3310 goto fail;
3315 * UNIX mode
3317 if (config->unix_info_enabled) {
3318 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3322 * max_access
3324 if (!config->readdir_attr_max_access) {
3325 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3326 } else {
3327 status = smbd_calculate_access_mask(
3328 handle->conn,
3329 fname,
3330 false,
3331 SEC_FLAG_MAXIMUM_ALLOWED,
3332 &attr_data->attr_data.aapl.max_access);
3333 if (!NT_STATUS_IS_OK(status)) {
3334 goto fail;
3338 return NT_STATUS_OK;
3340 fail:
3341 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3342 fname->base_name, nt_errstr(status)));
3343 TALLOC_FREE(*pattr_data);
3344 return status;
3347 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3348 files_struct *fsp,
3349 uint32 security_info,
3350 TALLOC_CTX *mem_ctx,
3351 struct security_descriptor **ppdesc)
3353 NTSTATUS status;
3354 struct security_ace ace;
3355 struct dom_sid sid;
3356 struct fruit_config_data *config;
3358 SMB_VFS_HANDLE_GET_DATA(handle, config,
3359 struct fruit_config_data,
3360 return NT_STATUS_UNSUCCESSFUL);
3362 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3363 mem_ctx, ppdesc);
3364 if (!NT_STATUS_IS_OK(status)) {
3365 return status;
3369 * Add MS NFS style ACEs with uid, gid and mode
3371 if (!config->unix_info_enabled) {
3372 return NT_STATUS_OK;
3375 /* MS NFS style mode */
3376 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3377 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3378 status = security_descriptor_dacl_add(*ppdesc, &ace);
3379 if (!NT_STATUS_IS_OK(status)) {
3380 DEBUG(1,("failed to add MS NFS style ACE\n"));
3381 return status;
3384 /* MS NFS style uid */
3385 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3386 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3387 status = security_descriptor_dacl_add(*ppdesc, &ace);
3388 if (!NT_STATUS_IS_OK(status)) {
3389 DEBUG(1,("failed to add MS NFS style ACE\n"));
3390 return status;
3393 /* MS NFS style gid */
3394 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3395 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3396 status = security_descriptor_dacl_add(*ppdesc, &ace);
3397 if (!NT_STATUS_IS_OK(status)) {
3398 DEBUG(1,("failed to add MS NFS style ACE\n"));
3399 return status;
3402 return NT_STATUS_OK;
3405 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3406 files_struct *fsp,
3407 uint32 security_info_sent,
3408 const struct security_descriptor *psd)
3410 NTSTATUS status;
3411 bool do_chmod;
3412 mode_t ms_nfs_mode;
3413 int result;
3415 DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp)));
3417 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3418 if (!NT_STATUS_IS_OK(status)) {
3419 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3420 return status;
3423 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3424 if (!NT_STATUS_IS_OK(status)) {
3425 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3426 return status;
3429 if (do_chmod) {
3430 if (fsp->fh->fd != -1) {
3431 DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp)));
3432 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3433 } else {
3434 DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp)));
3435 result = SMB_VFS_CHMOD(fsp->conn,
3436 fsp->fsp_name->base_name,
3437 ms_nfs_mode);
3440 if (result != 0) {
3441 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3442 result, ms_nfs_mode, strerror(errno)));
3443 status = map_nt_error_from_unix(errno);
3444 return status;
3448 return NT_STATUS_OK;
3451 static struct vfs_fn_pointers vfs_fruit_fns = {
3452 .connect_fn = fruit_connect,
3454 /* File operations */
3455 .chmod_fn = fruit_chmod,
3456 .chown_fn = fruit_chown,
3457 .unlink_fn = fruit_unlink,
3458 .rename_fn = fruit_rename,
3459 .rmdir_fn = fruit_rmdir,
3460 .open_fn = fruit_open,
3461 .pread_fn = fruit_pread,
3462 .pwrite_fn = fruit_pwrite,
3463 .stat_fn = fruit_stat,
3464 .lstat_fn = fruit_lstat,
3465 .fstat_fn = fruit_fstat,
3466 .streaminfo_fn = fruit_streaminfo,
3467 .ntimes_fn = fruit_ntimes,
3468 .ftruncate_fn = fruit_ftruncate,
3469 .fallocate_fn = fruit_fallocate,
3470 .create_file_fn = fruit_create_file,
3471 .readdir_attr_fn = fruit_readdir_attr,
3473 /* NT ACL operations */
3474 .fget_nt_acl_fn = fruit_fget_nt_acl,
3475 .fset_nt_acl_fn = fruit_fset_nt_acl,
3478 NTSTATUS vfs_fruit_init(void);
3479 NTSTATUS vfs_fruit_init(void)
3481 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3482 &vfs_fruit_fns);
3483 if (!NT_STATUS_IS_OK(ret)) {
3484 return ret;
3487 vfs_fruit_debug_level = debug_add_class("fruit");
3488 if (vfs_fruit_debug_level == -1) {
3489 vfs_fruit_debug_level = DBGC_VFS;
3490 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3491 "vfs_fruit_init"));
3492 } else {
3493 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3494 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3497 return ret;