heimdal: Fix CID 1273430 Double free
[Samba.git] / source3 / modules / vfs_fruit.c
blob4eace1eb35491f45e84df582457f2c85c66703e2
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)
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;
615 if ((off > bufsize) && (eid != ADEID_RFORK)) {
616 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
617 eid, off, len));
618 return false;
620 if ((eid != ADEID_RFORK) &&
621 (eid != ADEID_FINDERI) &&
622 ((off + len) > bufsize)) {
623 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
624 eid, off, len));
625 return false;
628 ad->ad_eid[eid].ade_off = off;
629 ad->ad_eid[eid].ade_len = len;
632 return true;
636 * Convert from Apple's ._ file to Netatalk
638 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
639 * bytes containing packed xattrs. Netatalk can't deal with that, so
640 * we simply discard the packed xattrs.
642 * @return -1 in case an error occured, 0 if no conversion was done, 1
643 * otherwise
645 static int ad_convert(struct adouble *ad, int fd)
647 int rc = 0;
648 char *map = MAP_FAILED;
649 size_t origlen;
651 origlen = ad_getentryoff(ad, ADEID_RFORK) +
652 ad_getentrylen(ad, ADEID_RFORK);
654 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
655 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
656 if (map == MAP_FAILED) {
657 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
658 rc = -1;
659 goto exit;
662 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
663 map + ad_getentryoff(ad, ADEID_RFORK),
664 ad_getentrylen(ad, ADEID_RFORK));
666 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
667 ad_setentryoff(ad, ADEID_RFORK,
668 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
671 * FIXME: direct ftruncate(), but we don't have a fsp for the
672 * VFS call
674 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
675 + ad_getentrylen(ad, ADEID_RFORK));
677 exit:
678 if (map != MAP_FAILED) {
679 munmap(map, origlen);
681 return rc;
685 * Read and parse Netatalk AppleDouble metadata xattr
687 static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
689 int rc = 0;
690 ssize_t ealen;
691 bool ok;
693 DEBUG(10, ("reading meta xattr for %s\n", path));
695 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
696 AFPINFO_EA_NETATALK, ad->ad_data,
697 AD_DATASZ_XATTR);
698 if (ealen == -1) {
699 switch (errno) {
700 case ENOATTR:
701 case ENOENT:
702 if (errno == ENOATTR) {
703 errno = ENOENT;
705 rc = -1;
706 goto exit;
707 default:
708 DEBUG(2, ("error reading meta xattr: %s\n",
709 strerror(errno)));
710 rc = -1;
711 goto exit;
714 if (ealen != AD_DATASZ_XATTR) {
715 DEBUG(2, ("bad size %zd\n", ealen));
716 errno = EINVAL;
717 rc = -1;
718 goto exit;
721 /* Now parse entries */
722 ok = ad_unpack(ad, ADEID_NUM_XATTR);
723 if (!ok) {
724 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
725 errno = EINVAL;
726 rc = -1;
727 goto exit;
730 if (!ad_getentryoff(ad, ADEID_FINDERI)
731 || !ad_getentryoff(ad, ADEID_COMMENT)
732 || !ad_getentryoff(ad, ADEID_FILEDATESI)
733 || !ad_getentryoff(ad, ADEID_AFPFILEI)
734 || !ad_getentryoff(ad, ADEID_PRIVDEV)
735 || !ad_getentryoff(ad, ADEID_PRIVINO)
736 || !ad_getentryoff(ad, ADEID_PRIVSYN)
737 || !ad_getentryoff(ad, ADEID_PRIVID)) {
738 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
739 errno = EINVAL;
740 rc = -1;
741 goto exit;
744 exit:
745 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
747 if (rc != 0) {
748 ealen = -1;
749 if (errno == EINVAL) {
750 become_root();
751 removexattr(path, AFPINFO_EA_NETATALK);
752 unbecome_root();
753 errno = ENOENT;
756 return ealen;
760 * Read and parse resource fork, either ._ AppleDouble file or xattr
762 static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
764 struct fruit_config_data *config = NULL;
765 int fd = -1;
766 int rc = 0;
767 ssize_t len;
768 char *adpath = NULL;
769 bool opened = false;
770 int mode;
771 struct adouble *meta_ad = NULL;
772 SMB_STRUCT_STAT sbuf;
773 bool ok;
774 int saved_errno = 0;
776 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
777 struct fruit_config_data, return -1);
779 /* Try rw first so we can use the fd in ad_convert() */
780 mode = O_RDWR;
782 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
783 fd = ad->ad_fsp->fh->fd;
784 } else {
785 if (config->rsrc == FRUIT_RSRC_XATTR) {
786 adpath = talloc_strdup(talloc_tos(), path);
787 } else {
788 rc = adouble_path(talloc_tos(), path, &adpath);
789 if (rc != 0) {
790 goto exit;
794 retry:
795 if (config->rsrc == FRUIT_RSRC_XATTR) {
796 #ifndef HAVE_ATTROPEN
797 errno = ENOSYS;
798 rc = -1;
799 goto exit;
800 #else
801 /* FIXME: direct Solaris xattr syscall */
802 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
803 mode, 0);
804 #endif
805 } else {
806 /* FIXME: direct open(), don't have an fsp */
807 fd = open(adpath, mode);
810 if (fd == -1) {
811 switch (errno) {
812 case EROFS:
813 case EACCES:
814 if (mode == O_RDWR) {
815 mode = O_RDONLY;
816 goto retry;
818 /* fall through ... */
819 default:
820 DEBUG(2, ("open AppleDouble: %s, %s\n",
821 adpath, strerror(errno)));
822 rc = -1;
823 goto exit;
826 opened = true;
829 if (config->rsrc == FRUIT_RSRC_XATTR) {
830 /* FIXME: direct sys_fstat(), don't have an fsp */
831 rc = sys_fstat(
832 fd, &sbuf,
833 lp_fake_directory_create_times(
834 SNUM(ad->ad_handle->conn)));
835 if (rc != 0) {
836 goto exit;
838 ad_setentrylen(ad, ADEID_RFORK, sbuf.st_ex_size);
839 } else {
840 /* FIXME: direct sys_pread(), don't have an fsp */
841 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
842 if (len != AD_DATASZ_DOT_UND) {
843 DEBUG(2, ("%s: bad size: %zd\n",
844 strerror(errno), len));
845 rc = -1;
846 goto exit;
849 /* Now parse entries */
850 ok = ad_unpack(ad, ADEID_NUM_DOT_UND);
851 if (!ok) {
852 DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
853 errno = EINVAL;
854 rc = -1;
855 goto exit;
858 if ((ad_getentryoff(ad, ADEID_FINDERI)
859 != ADEDOFF_FINDERI_DOT_UND)
860 || (ad_getentrylen(ad, ADEID_FINDERI)
861 < ADEDLEN_FINDERI)
862 || (ad_getentryoff(ad, ADEID_RFORK)
863 < ADEDOFF_RFORK_DOT_UND)) {
864 DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
865 errno = EINVAL;
866 rc = -1;
867 goto exit;
870 if ((mode == O_RDWR)
871 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
872 rc = ad_convert(ad, fd);
873 if (rc != 0) {
874 rc = -1;
875 goto exit;
878 * Can't use ad_write() because we might not have a fsp
880 rc = ad_pack(ad);
881 if (rc != 0) {
882 goto exit;
884 /* FIXME: direct sys_pwrite(), don't have an fsp */
885 len = sys_pwrite(fd, ad->ad_data,
886 AD_DATASZ_DOT_UND, 0);
887 if (len != AD_DATASZ_DOT_UND) {
888 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
889 rc = -1;
890 goto exit;
893 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
894 ADOUBLE_META, NULL);
895 if (meta_ad == NULL) {
896 rc = -1;
897 goto exit;
900 memcpy(ad_entry(meta_ad, ADEID_FINDERI),
901 ad_entry(ad, ADEID_FINDERI),
902 ADEDLEN_FINDERI);
904 rc = ad_write(meta_ad, path);
905 if (rc != 0) {
906 rc = -1;
907 goto exit;
912 DEBUG(10, ("opened AppleDouble: %s\n", path));
914 exit:
915 if (rc != 0) {
916 saved_errno = errno;
917 len = -1;
919 if (opened && fd != -1) {
920 close(fd);
922 TALLOC_FREE(adpath);
923 TALLOC_FREE(meta_ad);
924 if (rc != 0) {
925 errno = saved_errno;
927 return len;
931 * Read and unpack an AppleDouble metadata xattr or resource
933 static ssize_t ad_read(struct adouble *ad, const char *path)
935 switch (ad->ad_type) {
936 case ADOUBLE_META:
937 return ad_header_read_meta(ad, path);
938 case ADOUBLE_RSRC:
939 return ad_header_read_rsrc(ad, path);
940 default:
941 return -1;
946 * Allocate a struct adouble without initialiing it
948 * The struct is either hang of the fsp extension context or if fsp is
949 * NULL from ctx.
951 * @param[in] ctx talloc context
952 * @param[in] handle vfs handle
953 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
955 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
956 * added as an fsp extension
958 * @return adouble handle
960 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
961 adouble_type_t type, files_struct *fsp)
963 int rc = 0;
964 size_t adsize = 0;
965 struct adouble *ad;
966 struct fruit_config_data *config;
968 SMB_VFS_HANDLE_GET_DATA(handle, config,
969 struct fruit_config_data, return NULL);
971 switch (type) {
972 case ADOUBLE_META:
973 adsize = AD_DATASZ_XATTR;
974 break;
975 case ADOUBLE_RSRC:
976 if (config->rsrc == FRUIT_RSRC_ADFILE) {
977 adsize = AD_DATASZ_DOT_UND;
979 break;
980 default:
981 return NULL;
984 if (!fsp) {
985 ad = talloc_zero(ctx, struct adouble);
986 if (ad == NULL) {
987 rc = -1;
988 goto exit;
990 if (adsize) {
991 ad->ad_data = talloc_zero_array(ad, char, adsize);
993 } else {
994 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
995 struct adouble,
996 NULL);
997 if (ad == NULL) {
998 rc = -1;
999 goto exit;
1001 if (adsize) {
1002 ad->ad_data = talloc_zero_array(
1003 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1004 char, adsize);
1006 ad->ad_fsp = fsp;
1009 if (adsize && ad->ad_data == NULL) {
1010 rc = -1;
1011 goto exit;
1013 ad->ad_handle = handle;
1014 ad->ad_type = type;
1015 ad->ad_magic = AD_MAGIC;
1016 ad->ad_version = AD_VERSION;
1018 exit:
1019 if (rc != 0) {
1020 TALLOC_FREE(ad);
1022 return ad;
1026 * Allocate and initialize a new struct adouble
1028 * @param[in] ctx talloc context
1029 * @param[in] handle vfs handle
1030 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1031 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1033 * @return adouble handle, initialized
1035 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1036 adouble_type_t type, files_struct *fsp)
1038 int rc = 0;
1039 const struct ad_entry_order *eid;
1040 struct adouble *ad = NULL;
1041 struct fruit_config_data *config;
1042 time_t t = time(NULL);
1044 SMB_VFS_HANDLE_GET_DATA(handle, config,
1045 struct fruit_config_data, return NULL);
1047 switch (type) {
1048 case ADOUBLE_META:
1049 eid = entry_order_meta_xattr;
1050 break;
1051 case ADOUBLE_RSRC:
1052 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1053 eid = entry_order_dot_und;
1054 } else {
1055 eid = entry_order_rsrc_xattr;
1057 break;
1058 default:
1059 return NULL;
1062 ad = ad_alloc(ctx, handle, type, fsp);
1063 if (ad == NULL) {
1064 return NULL;
1067 while (eid->id) {
1068 ad->ad_eid[eid->id].ade_off = eid->offset;
1069 ad->ad_eid[eid->id].ade_len = eid->len;
1070 eid++;
1073 /* put something sane in the date fields */
1074 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1075 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1076 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1077 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1079 if (rc != 0) {
1080 TALLOC_FREE(ad);
1082 return ad;
1086 * Return AppleDouble data for a file
1088 * @param[in] ctx talloc context
1089 * @param[in] handle vfs handle
1090 * @param[in] path pathname to file or directory
1091 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1093 * @return talloced struct adouble or NULL on error
1095 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1096 const char *path, adouble_type_t type)
1098 int rc = 0;
1099 ssize_t len;
1100 struct adouble *ad = NULL;
1102 DEBUG(10, ("ad_get(%s) called for %s\n",
1103 type == ADOUBLE_META ? "meta" : "rsrc", path));
1105 ad = ad_alloc(ctx, handle, type, NULL);
1106 if (ad == NULL) {
1107 rc = -1;
1108 goto exit;
1111 len = ad_read(ad, path);
1112 if (len == -1) {
1113 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1114 rc = -1;
1115 goto exit;
1118 exit:
1119 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1120 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1122 if (rc != 0) {
1123 TALLOC_FREE(ad);
1125 return ad;
1129 * Set AppleDouble metadata on a file or directory
1131 * @param[in] ad adouble handle
1133 * @param[in] path pathname to file or directory, may be NULL for a
1134 * resource fork
1136 * @return status code, 0 means success
1138 static int ad_write(struct adouble *ad, const char *path)
1140 int rc = 0;
1141 ssize_t len;
1143 rc = ad_pack(ad);
1144 if (rc != 0) {
1145 goto exit;
1148 switch (ad->ad_type) {
1149 case ADOUBLE_META:
1150 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1151 AFPINFO_EA_NETATALK, ad->ad_data,
1152 AD_DATASZ_XATTR, 0);
1153 break;
1154 case ADOUBLE_RSRC:
1155 if ((ad->ad_fsp == NULL)
1156 || (ad->ad_fsp->fh == NULL)
1157 || (ad->ad_fsp->fh->fd == -1)) {
1158 rc = -1;
1159 goto exit;
1161 /* FIXME: direct sys_pwrite(), don't have an fsp */
1162 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1163 talloc_get_size(ad->ad_data), 0);
1164 if (len != talloc_get_size(ad->ad_data)) {
1165 DEBUG(1, ("short write on %s: %zd",
1166 fsp_str_dbg(ad->ad_fsp), len));
1167 rc = -1;
1168 goto exit;
1170 break;
1171 default:
1172 return -1;
1174 exit:
1175 return rc;
1178 /*****************************************************************************
1179 * Helper functions
1180 *****************************************************************************/
1182 static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1184 if (strncasecmp_m(smb_fname->stream_name,
1185 AFPINFO_STREAM_NAME,
1186 strlen(AFPINFO_STREAM_NAME)) == 0) {
1187 return true;
1189 return false;
1192 static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1194 if (strncasecmp_m(smb_fname->stream_name,
1195 AFPRESOURCE_STREAM_NAME,
1196 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1197 return true;
1199 return false;
1203 * Test whether stream is an Apple stream, not used atm
1205 #if 0
1206 static bool is_apple_stream(const struct smb_filename *smb_fname)
1208 if (is_afpinfo_stream(smb_fname)) {
1209 return true;
1211 if (is_afpresource_stream(smb_fname)) {
1212 return true;
1214 return false;
1216 #endif
1219 * Initialize config struct from our smb.conf config parameters
1221 static int init_fruit_config(vfs_handle_struct *handle)
1223 struct fruit_config_data *config;
1224 int enumval;
1226 config = talloc_zero(handle->conn, struct fruit_config_data);
1227 if (!config) {
1228 DEBUG(1, ("talloc_zero() failed\n"));
1229 errno = ENOMEM;
1230 return -1;
1233 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1234 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1235 if (enumval == -1) {
1236 DEBUG(1, ("value for %s: ressource type unknown\n",
1237 FRUIT_PARAM_TYPE_NAME));
1238 return -1;
1240 config->rsrc = (enum fruit_rsrc)enumval;
1242 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1243 "metadata", fruit_meta, FRUIT_META_NETATALK);
1244 if (enumval == -1) {
1245 DEBUG(1, ("value for %s: metadata type unknown\n",
1246 FRUIT_PARAM_TYPE_NAME));
1247 return -1;
1249 config->meta = (enum fruit_meta)enumval;
1251 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1252 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1253 if (enumval == -1) {
1254 DEBUG(1, ("value for %s: locking type unknown\n",
1255 FRUIT_PARAM_TYPE_NAME));
1256 return -1;
1258 config->locking = (enum fruit_locking)enumval;
1260 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1261 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1262 if (enumval == -1) {
1263 DEBUG(1, ("value for %s: encoding type unknown\n",
1264 FRUIT_PARAM_TYPE_NAME));
1265 return -1;
1267 config->encoding = (enum fruit_encoding)enumval;
1269 if (lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "aapl", true)) {
1270 config->use_aapl = true;
1273 if (lp_parm_bool(SNUM(handle->conn),
1274 "readdir_attr", "aapl_rsize", true)) {
1275 config->readdir_attr_rsize = true;
1278 if (lp_parm_bool(SNUM(handle->conn),
1279 "readdir_attr", "aapl_finder_info", true)) {
1280 config->readdir_attr_finder_info = true;
1283 if (lp_parm_bool(SNUM(handle->conn),
1284 "readdir_attr", "aapl_max_access", true)) {
1285 config->readdir_attr_max_access = true;
1288 SMB_VFS_HANDLE_SET_DATA(handle, config,
1289 NULL, struct fruit_config_data,
1290 return -1);
1292 return 0;
1296 * Prepend "._" to a basename
1298 static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1300 char *parent;
1301 const char *base;
1303 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1304 return -1;
1307 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1308 if (*path_out == NULL) {
1309 return -1;
1312 return 0;
1316 * Allocate and initialize an AfpInfo struct
1318 static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1320 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1321 if (ai == NULL) {
1322 return NULL;
1324 ai->afpi_Signature = AFP_Signature;
1325 ai->afpi_Version = AFP_Version;
1326 ai->afpi_BackupTime = AD_DATE_START;
1327 return ai;
1331 * Pack an AfpInfo struct into a buffer
1333 * Buffer size must be at least AFP_INFO_SIZE
1334 * Returns size of packed buffer
1336 static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1338 memset(buf, 0, AFP_INFO_SIZE);
1340 RSIVAL(buf, 0, ai->afpi_Signature);
1341 RSIVAL(buf, 4, ai->afpi_Version);
1342 RSIVAL(buf, 12, ai->afpi_BackupTime);
1343 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1345 return AFP_INFO_SIZE;
1349 * Unpack a buffer into a AfpInfo structure
1351 * Buffer size must be at least AFP_INFO_SIZE
1352 * Returns allocated AfpInfo struct
1354 static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1356 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1357 if (ai == NULL) {
1358 return NULL;
1361 ai->afpi_Signature = RIVAL(data, 0);
1362 ai->afpi_Version = RIVAL(data, 4);
1363 ai->afpi_BackupTime = RIVAL(data, 12);
1364 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1365 sizeof(ai->afpi_FinderInfo));
1367 if (ai->afpi_Signature != AFP_Signature
1368 || ai->afpi_Version != AFP_Version) {
1369 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1370 TALLOC_FREE(ai);
1373 return ai;
1377 * Fake an inode number from the md5 hash of the (xattr) name
1379 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1381 MD5_CTX ctx;
1382 unsigned char hash[16];
1383 SMB_INO_T result;
1384 char *upper_sname;
1386 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1387 SMB_ASSERT(upper_sname != NULL);
1389 MD5Init(&ctx);
1390 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1391 sizeof(sbuf->st_ex_dev));
1392 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1393 sizeof(sbuf->st_ex_ino));
1394 MD5Update(&ctx, (unsigned char *)upper_sname,
1395 talloc_get_size(upper_sname)-1);
1396 MD5Final(hash, &ctx);
1398 TALLOC_FREE(upper_sname);
1400 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1401 memcpy(&result, hash, sizeof(result));
1403 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1404 sname, (unsigned long long)result));
1406 return result;
1410 * Ensure ad_fsp is still valid
1412 static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1414 if (ad->ad_fsp == fsp) {
1415 return true;
1417 ad->ad_fsp = fsp;
1419 return true;
1422 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1423 struct stream_struct **streams,
1424 const char *name, off_t size,
1425 off_t alloc_size)
1427 struct stream_struct *tmp;
1429 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1430 (*num_streams)+1);
1431 if (tmp == NULL) {
1432 return false;
1435 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1436 if (tmp[*num_streams].name == NULL) {
1437 return false;
1440 tmp[*num_streams].size = size;
1441 tmp[*num_streams].alloc_size = alloc_size;
1443 *streams = tmp;
1444 *num_streams += 1;
1445 return true;
1448 static bool empty_finderinfo(const struct adouble *ad)
1451 char emptybuf[ADEDLEN_FINDERI] = {0};
1452 if (memcmp(emptybuf,
1453 ad_entry(ad, ADEID_FINDERI),
1454 ADEDLEN_FINDERI) == 0) {
1455 return true;
1457 return false;
1461 * Update btime with btime from Netatalk
1463 static void update_btime(vfs_handle_struct *handle,
1464 struct smb_filename *smb_fname)
1466 uint32_t t;
1467 struct timespec creation_time = {0};
1468 struct adouble *ad;
1470 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1471 if (ad == NULL) {
1472 return;
1474 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1475 TALLOC_FREE(ad);
1476 return;
1478 TALLOC_FREE(ad);
1480 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1481 update_stat_ex_create_time(&smb_fname->st, creation_time);
1483 return;
1487 * Map an access mask to a Netatalk single byte byte range lock
1489 static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1490 uint32_t access_mask)
1492 off_t offset;
1494 switch (access_mask) {
1495 case FILE_READ_DATA:
1496 offset = AD_FILELOCK_OPEN_RD;
1497 break;
1499 case FILE_WRITE_DATA:
1500 case FILE_APPEND_DATA:
1501 offset = AD_FILELOCK_OPEN_WR;
1502 break;
1504 default:
1505 offset = AD_FILELOCK_OPEN_NONE;
1506 break;
1509 if (fork_type == APPLE_FORK_RSRC) {
1510 if (offset == AD_FILELOCK_OPEN_NONE) {
1511 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1512 } else {
1513 offset += 2;
1517 return offset;
1521 * Map a deny mode to a Netatalk brl
1523 static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1524 uint32_t deny_mode)
1526 off_t offset;
1528 switch (deny_mode) {
1529 case DENY_READ:
1530 offset = AD_FILELOCK_DENY_RD;
1531 break;
1533 case DENY_WRITE:
1534 offset = AD_FILELOCK_DENY_WR;
1535 break;
1537 default:
1538 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1541 if (fork_type == APPLE_FORK_RSRC) {
1542 offset += 2;
1545 return offset;
1549 * Call fcntl() with an exclusive F_GETLK request in order to
1550 * determine if there's an exisiting shared lock
1552 * @return true if the requested lock was found or any error occured
1553 * false if the lock was not found
1555 static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1557 bool result;
1558 off_t offset = in_offset;
1559 off_t len = 1;
1560 int type = F_WRLCK;
1561 pid_t pid;
1563 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1564 if (result == false) {
1565 return true;
1568 if (type != F_UNLCK) {
1569 return true;
1572 return false;
1575 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1576 files_struct *fsp,
1577 uint32_t access_mask,
1578 uint32_t deny_mode)
1580 NTSTATUS status = NT_STATUS_OK;
1581 struct byte_range_lock *br_lck = NULL;
1582 bool open_for_reading, open_for_writing, deny_read, deny_write;
1583 off_t off;
1585 /* FIXME: hardcoded data fork, add resource fork */
1586 enum apple_fork fork_type = APPLE_FORK_DATA;
1588 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1589 fsp_str_dbg(fsp),
1590 access_mask & FILE_READ_DATA ? "READ" :"-",
1591 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1592 deny_mode & DENY_READ ? "DENY_READ" : "-",
1593 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1596 * Check read access and deny read mode
1598 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1599 /* Check access */
1600 open_for_reading = test_netatalk_lock(
1601 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1603 deny_read = test_netatalk_lock(
1604 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1606 DEBUG(10, ("read: %s, deny_write: %s\n",
1607 open_for_reading == true ? "yes" : "no",
1608 deny_read == true ? "yes" : "no"));
1610 if (((access_mask & FILE_READ_DATA) && deny_read)
1611 || ((deny_mode & DENY_READ) && open_for_reading)) {
1612 return NT_STATUS_SHARING_VIOLATION;
1615 /* Set locks */
1616 if (access_mask & FILE_READ_DATA) {
1617 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1618 br_lck = do_lock(
1619 handle->conn->sconn->msg_ctx, fsp,
1620 fsp->op->global->open_persistent_id, 1, off,
1621 READ_LOCK, POSIX_LOCK, false,
1622 &status, NULL);
1624 if (!NT_STATUS_IS_OK(status)) {
1625 return status;
1627 TALLOC_FREE(br_lck);
1630 if (deny_mode & DENY_READ) {
1631 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1632 br_lck = do_lock(
1633 handle->conn->sconn->msg_ctx, fsp,
1634 fsp->op->global->open_persistent_id, 1, off,
1635 READ_LOCK, POSIX_LOCK, false,
1636 &status, NULL);
1638 if (!NT_STATUS_IS_OK(status)) {
1639 return status;
1641 TALLOC_FREE(br_lck);
1646 * Check write access and deny write mode
1648 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1649 /* Check access */
1650 open_for_writing = test_netatalk_lock(
1651 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1653 deny_write = test_netatalk_lock(
1654 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1656 DEBUG(10, ("write: %s, deny_write: %s\n",
1657 open_for_writing == true ? "yes" : "no",
1658 deny_write == true ? "yes" : "no"));
1660 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1661 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1662 return NT_STATUS_SHARING_VIOLATION;
1665 /* Set locks */
1666 if (access_mask & FILE_WRITE_DATA) {
1667 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1668 br_lck = do_lock(
1669 handle->conn->sconn->msg_ctx, fsp,
1670 fsp->op->global->open_persistent_id, 1, off,
1671 READ_LOCK, POSIX_LOCK, false,
1672 &status, NULL);
1674 if (!NT_STATUS_IS_OK(status)) {
1675 return status;
1677 TALLOC_FREE(br_lck);
1680 if (deny_mode & DENY_WRITE) {
1681 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1682 br_lck = do_lock(
1683 handle->conn->sconn->msg_ctx, fsp,
1684 fsp->op->global->open_persistent_id, 1, off,
1685 READ_LOCK, POSIX_LOCK, false,
1686 &status, NULL);
1688 if (!NT_STATUS_IS_OK(status)) {
1689 return status;
1691 TALLOC_FREE(br_lck);
1695 TALLOC_FREE(br_lck);
1697 return status;
1700 static NTSTATUS check_aapl(vfs_handle_struct *handle,
1701 struct smb_request *req,
1702 const struct smb2_create_blobs *in_context_blobs,
1703 struct smb2_create_blobs *out_context_blobs)
1705 struct fruit_config_data *config;
1706 NTSTATUS status;
1707 struct smb2_create_blob *aapl = NULL;
1708 uint32_t cmd;
1709 bool ok;
1710 uint8_t p[16];
1711 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1712 uint64_t req_bitmap, client_caps;
1713 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1714 smb_ucs2_t *model;
1715 size_t modellen;
1717 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1718 return NT_STATUS_UNSUCCESSFUL);
1720 if (!config->use_aapl
1721 || in_context_blobs == NULL
1722 || out_context_blobs == NULL) {
1723 return NT_STATUS_OK;
1726 aapl = smb2_create_blob_find(in_context_blobs,
1727 SMB2_CREATE_TAG_AAPL);
1728 if (aapl == NULL) {
1729 return NT_STATUS_OK;
1732 if (aapl->data.length != 24) {
1733 DEBUG(1, ("unexpected AAPL ctxt legnth: %ju\n",
1734 (uintmax_t)aapl->data.length));
1735 return NT_STATUS_INVALID_PARAMETER;
1738 cmd = IVAL(aapl->data.data, 0);
1739 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1740 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1741 return NT_STATUS_INVALID_PARAMETER;
1744 req_bitmap = BVAL(aapl->data.data, 8);
1745 client_caps = BVAL(aapl->data.data, 16);
1747 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1748 SIVAL(p, 4, 0);
1749 SBVAL(p, 8, req_bitmap);
1750 ok = data_blob_append(req, &blob, p, 16);
1751 if (!ok) {
1752 return NT_STATUS_UNSUCCESSFUL;
1755 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1756 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1757 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1758 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1759 config->readdir_attr_enabled = true;
1763 * The client doesn't set the flag, so we can't check
1764 * for it and just set it unconditionally
1766 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1767 config->unix_info_enabled = true;
1769 SBVAL(p, 0, server_caps);
1770 ok = data_blob_append(req, &blob, p, 8);
1771 if (!ok) {
1772 return NT_STATUS_UNSUCCESSFUL;
1776 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1777 SBVAL(p, 0,
1778 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1779 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1780 ok = data_blob_append(req, &blob, p, 8);
1781 if (!ok) {
1782 return NT_STATUS_UNSUCCESSFUL;
1786 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1787 ok = convert_string_talloc(req,
1788 CH_UNIX, CH_UTF16LE,
1789 "Samba", strlen("Samba"),
1790 &model, &modellen);
1791 if (!ok) {
1792 return NT_STATUS_UNSUCCESSFUL;
1795 SIVAL(p, 0, 0);
1796 SIVAL(p + 4, 0, modellen);
1797 ok = data_blob_append(req, &blob, p, 8);
1798 if (!ok) {
1799 talloc_free(model);
1800 return NT_STATUS_UNSUCCESSFUL;
1803 ok = data_blob_append(req, &blob, model, modellen);
1804 talloc_free(model);
1805 if (!ok) {
1806 return NT_STATUS_UNSUCCESSFUL;
1810 status = smb2_create_blob_add(out_context_blobs,
1811 out_context_blobs,
1812 SMB2_CREATE_TAG_AAPL,
1813 blob);
1815 return status;
1818 static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1819 const struct smb_filename *smb_fname,
1820 struct readdir_attr_data *attr_data)
1822 NTSTATUS status = NT_STATUS_OK;
1823 uint32_t date_added;
1824 struct adouble *ad = NULL;
1825 struct fruit_config_data *config = NULL;
1827 SMB_VFS_HANDLE_GET_DATA(handle, config,
1828 struct fruit_config_data,
1829 return NT_STATUS_UNSUCCESSFUL);
1832 /* Ensure we return a default value in the creation_date field */
1833 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1836 * Resource fork length
1839 if (config->readdir_attr_rsize) {
1840 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1841 ADOUBLE_RSRC);
1842 if (ad) {
1843 attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1844 ad, ADEID_RFORK);
1845 TALLOC_FREE(ad);
1850 * FinderInfo
1853 if (config->readdir_attr_finder_info) {
1854 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1855 ADOUBLE_META);
1856 if (ad) {
1857 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1858 /* finder_type */
1859 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1860 ad_entry(ad, ADEID_FINDERI), 4);
1862 /* finder_creator */
1863 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1864 ad_entry(ad, ADEID_FINDERI) + 4, 4);
1867 /* finder_flags */
1868 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1869 ad_entry(ad, ADEID_FINDERI) + 8, 2);
1871 /* finder_ext_flags */
1872 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1873 ad_entry(ad, ADEID_FINDERI) + 24, 2);
1875 /* creation date */
1876 date_added = convert_time_t_to_uint32_t(
1877 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
1878 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
1880 TALLOC_FREE(ad);
1884 TALLOC_FREE(ad);
1885 return status;
1888 /* Search MS NFS style ACE with UNIX mode */
1889 static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
1890 files_struct *fsp,
1891 const struct security_descriptor *psd,
1892 mode_t *pmode,
1893 bool *pdo_chmod)
1895 int i;
1896 struct fruit_config_data *config = NULL;
1898 *pdo_chmod = false;
1900 SMB_VFS_HANDLE_GET_DATA(handle, config,
1901 struct fruit_config_data,
1902 return NT_STATUS_UNSUCCESSFUL);
1904 if (psd->dacl == NULL || !config->unix_info_enabled) {
1905 return NT_STATUS_OK;
1908 for (i = 0; i < psd->dacl->num_aces; i++) {
1909 if (dom_sid_compare_domain(
1910 &global_sid_Unix_NFS_Mode,
1911 &psd->dacl->aces[i].trustee) == 0) {
1912 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
1913 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
1914 *pdo_chmod = true;
1916 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1917 fsp_str_dbg(fsp), *pmode));
1918 break;
1922 return NT_STATUS_OK;
1925 /****************************************************************************
1926 * VFS ops
1927 ****************************************************************************/
1929 static int fruit_connect(vfs_handle_struct *handle,
1930 const char *service,
1931 const char *user)
1933 int rc;
1934 char *list = NULL, *newlist = NULL;
1935 struct fruit_config_data *config;
1937 DEBUG(10, ("fruit_connect\n"));
1939 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1940 if (rc < 0) {
1941 return rc;
1944 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
1946 if (list) {
1947 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
1948 newlist = talloc_asprintf(
1949 list,
1950 "%s/" ADOUBLE_NAME_PREFIX "*/",
1951 list);
1952 lp_do_parameter(SNUM(handle->conn),
1953 "veto files",
1954 newlist);
1956 } else {
1957 lp_do_parameter(SNUM(handle->conn),
1958 "veto files",
1959 "/" ADOUBLE_NAME_PREFIX "*/");
1962 TALLOC_FREE(list);
1964 rc = init_fruit_config(handle);
1965 if (rc != 0) {
1966 return rc;
1969 SMB_VFS_HANDLE_GET_DATA(handle, config,
1970 struct fruit_config_data, return -1);
1972 if (config->encoding == FRUIT_ENC_NATIVE) {
1973 lp_do_parameter(
1974 SNUM(handle->conn),
1975 "catia:mappings",
1976 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
1977 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
1978 "0x0d:0xf00d");
1981 return rc;
1984 static int fruit_open_meta(vfs_handle_struct *handle,
1985 struct smb_filename *smb_fname,
1986 files_struct *fsp, int flags, mode_t mode)
1988 int rc = 0;
1989 struct fruit_config_data *config = NULL;
1990 struct smb_filename *smb_fname_base = NULL;
1991 int baseflags;
1992 int hostfd = -1;
1993 struct adouble *ad = NULL;
1995 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
1997 SMB_VFS_HANDLE_GET_DATA(handle, config,
1998 struct fruit_config_data, return -1);
2000 if (config->meta == FRUIT_META_STREAM) {
2001 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2004 /* Create an smb_filename with stream_name == NULL. */
2005 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2006 smb_fname->base_name, NULL, NULL);
2008 if (smb_fname_base == NULL) {
2009 errno = ENOMEM;
2010 rc = -1;
2011 goto exit;
2015 * We use baseflags to turn off nasty side-effects when opening the
2016 * underlying file.
2018 baseflags = flags;
2019 baseflags &= ~O_TRUNC;
2020 baseflags &= ~O_EXCL;
2021 baseflags &= ~O_CREAT;
2023 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2024 baseflags, mode);
2027 * It is legit to open a stream on a directory, but the base
2028 * fd has to be read-only.
2030 if ((hostfd == -1) && (errno == EISDIR)) {
2031 baseflags &= ~O_ACCMODE;
2032 baseflags |= O_RDONLY;
2033 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2034 baseflags, mode);
2037 TALLOC_FREE(smb_fname_base);
2039 if (hostfd == -1) {
2040 rc = -1;
2041 goto exit;
2044 if (flags & (O_CREAT | O_TRUNC)) {
2046 * The attribute does not exist or needs to be truncated,
2047 * create an AppleDouble EA
2049 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2050 handle, ADOUBLE_META, fsp);
2051 if (ad == NULL) {
2052 rc = -1;
2053 goto exit;
2056 rc = ad_write(ad, smb_fname->base_name);
2057 if (rc != 0) {
2058 rc = -1;
2059 goto exit;
2061 } else {
2062 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2063 handle, ADOUBLE_META, fsp);
2064 if (ad == NULL) {
2065 rc = -1;
2066 goto exit;
2068 if (ad_read(ad, smb_fname->base_name) == -1) {
2069 rc = -1;
2070 goto exit;
2074 exit:
2075 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2076 if (rc != 0) {
2077 int saved_errno = errno;
2078 if (hostfd >= 0) {
2080 * BUGBUGBUG -- we would need to call
2081 * fd_close_posix here, but we don't have a
2082 * full fsp yet
2084 fsp->fh->fd = hostfd;
2085 SMB_VFS_CLOSE(fsp);
2087 hostfd = -1;
2088 errno = saved_errno;
2090 return hostfd;
2093 static int fruit_open_rsrc(vfs_handle_struct *handle,
2094 struct smb_filename *smb_fname,
2095 files_struct *fsp, int flags, mode_t mode)
2097 int rc = 0;
2098 struct fruit_config_data *config = NULL;
2099 struct adouble *ad = NULL;
2100 struct smb_filename *smb_fname_base = NULL;
2101 char *adpath = NULL;
2102 int hostfd = -1;
2104 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2106 SMB_VFS_HANDLE_GET_DATA(handle, config,
2107 struct fruit_config_data, return -1);
2109 switch (config->rsrc) {
2110 case FRUIT_RSRC_STREAM:
2111 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2112 case FRUIT_RSRC_XATTR:
2113 #ifdef HAVE_ATTROPEN
2114 hostfd = attropen(smb_fname->base_name,
2115 AFPRESOURCE_EA_NETATALK, flags, mode);
2116 if (hostfd == -1) {
2117 return -1;
2119 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2120 handle, ADOUBLE_RSRC, fsp);
2121 if (ad == NULL) {
2122 rc = -1;
2123 goto exit;
2125 goto exit;
2126 #else
2127 errno = ENOTSUP;
2128 return -1;
2129 #endif
2130 default:
2131 break;
2134 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2135 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2136 if (rc != 0) {
2137 rc = -1;
2138 goto exit;
2142 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2143 /* sorry, but directories don't habe a resource fork */
2144 rc = -1;
2145 goto exit;
2148 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2149 if (rc != 0) {
2150 goto exit;
2153 /* Create an smb_filename with stream_name == NULL. */
2154 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2155 adpath, NULL, NULL);
2156 if (smb_fname_base == NULL) {
2157 errno = ENOMEM;
2158 rc = -1;
2159 goto exit;
2162 /* Sanitize flags */
2163 if (flags & O_WRONLY) {
2164 /* We always need read access for the metadata header too */
2165 flags &= ~O_WRONLY;
2166 flags |= O_RDWR;
2169 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2170 flags, mode);
2171 if (hostfd == -1) {
2172 rc = -1;
2173 goto exit;
2176 /* REVIEW: we need this in ad_write() */
2177 fsp->fh->fd = hostfd;
2179 if (flags & (O_CREAT | O_TRUNC)) {
2180 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2181 handle, ADOUBLE_RSRC, fsp);
2182 if (ad == NULL) {
2183 rc = -1;
2184 goto exit;
2186 rc = ad_write(ad, smb_fname->base_name);
2187 if (rc != 0) {
2188 rc = -1;
2189 goto exit;
2191 } else {
2192 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2193 handle, ADOUBLE_RSRC, fsp);
2194 if (ad == NULL) {
2195 rc = -1;
2196 goto exit;
2198 if (ad_read(ad, smb_fname->base_name) == -1) {
2199 rc = -1;
2200 goto exit;
2204 exit:
2206 TALLOC_FREE(adpath);
2207 TALLOC_FREE(smb_fname_base);
2209 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2210 if (rc != 0) {
2211 int saved_errno = errno;
2212 if (hostfd >= 0) {
2214 * BUGBUGBUG -- we would need to call
2215 * fd_close_posix here, but we don't have a
2216 * full fsp yet
2218 fsp->fh->fd = hostfd;
2219 SMB_VFS_CLOSE(fsp);
2221 hostfd = -1;
2222 errno = saved_errno;
2224 return hostfd;
2227 static int fruit_open(vfs_handle_struct *handle,
2228 struct smb_filename *smb_fname,
2229 files_struct *fsp, int flags, mode_t mode)
2231 DEBUG(10, ("fruit_open called for %s\n",
2232 smb_fname_str_dbg(smb_fname)));
2234 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2235 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2238 if (is_afpinfo_stream(smb_fname)) {
2239 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2240 } else if (is_afpresource_stream(smb_fname)) {
2241 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2244 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2247 static int fruit_rename(struct vfs_handle_struct *handle,
2248 const struct smb_filename *smb_fname_src,
2249 const struct smb_filename *smb_fname_dst)
2251 int rc = -1;
2252 char *src_adouble_path = NULL;
2253 char *dst_adouble_path = NULL;
2254 struct fruit_config_data *config = NULL;
2256 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2258 if (!VALID_STAT(smb_fname_src->st)
2259 || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2260 return rc;
2263 SMB_VFS_HANDLE_GET_DATA(handle, config,
2264 struct fruit_config_data, return -1);
2266 if (config->rsrc == FRUIT_RSRC_XATTR) {
2267 return rc;
2270 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2271 &src_adouble_path);
2272 if (rc != 0) {
2273 goto done;
2275 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2276 &dst_adouble_path);
2277 if (rc != 0) {
2278 goto done;
2281 DEBUG(10, ("fruit_rename: %s -> %s\n",
2282 src_adouble_path, dst_adouble_path));
2284 rc = rename(src_adouble_path, dst_adouble_path);
2285 if (errno == ENOENT) {
2286 rc = 0;
2289 TALLOC_FREE(src_adouble_path);
2290 TALLOC_FREE(dst_adouble_path);
2292 done:
2293 return rc;
2296 static int fruit_unlink(vfs_handle_struct *handle,
2297 const struct smb_filename *smb_fname)
2299 int rc = -1;
2300 struct fruit_config_data *config = NULL;
2301 char *adp = NULL;
2303 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2304 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2307 SMB_VFS_HANDLE_GET_DATA(handle, config,
2308 struct fruit_config_data, return -1);
2310 if (is_afpinfo_stream(smb_fname)) {
2311 if (config->meta == FRUIT_META_STREAM) {
2312 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2313 } else {
2314 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2315 smb_fname->base_name,
2316 AFPINFO_EA_NETATALK);
2318 } else if (is_afpresource_stream(smb_fname)) {
2319 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2320 rc = adouble_path(talloc_tos(),
2321 smb_fname->base_name, &adp);
2322 if (rc != 0) {
2323 return -1;
2325 /* FIXME: direct unlink(), missing smb_fname */
2326 rc = unlink(adp);
2327 if ((rc == -1) && (errno == ENOENT)) {
2328 rc = 0;
2330 } else {
2331 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2332 smb_fname->base_name,
2333 AFPRESOURCE_EA_NETATALK);
2335 } else {
2336 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2339 TALLOC_FREE(adp);
2340 return rc;
2343 static int fruit_chmod(vfs_handle_struct *handle,
2344 const char *path,
2345 mode_t mode)
2347 int rc = -1;
2348 char *adp = NULL;
2349 struct fruit_config_data *config = NULL;
2350 SMB_STRUCT_STAT sb;
2352 rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2353 if (rc != 0) {
2354 return rc;
2357 SMB_VFS_HANDLE_GET_DATA(handle, config,
2358 struct fruit_config_data, return -1);
2360 if (config->rsrc == FRUIT_RSRC_XATTR) {
2361 return 0;
2364 /* FIXME: direct sys_lstat(), missing smb_fname */
2365 rc = sys_lstat(path, &sb, false);
2366 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2367 return rc;
2370 rc = adouble_path(talloc_tos(), path, &adp);
2371 if (rc != 0) {
2372 return -1;
2375 DEBUG(10, ("fruit_chmod: %s\n", adp));
2377 rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2378 if (errno == ENOENT) {
2379 rc = 0;
2382 TALLOC_FREE(adp);
2383 return rc;
2386 static int fruit_chown(vfs_handle_struct *handle,
2387 const char *path,
2388 uid_t uid,
2389 gid_t gid)
2391 int rc = -1;
2392 char *adp = NULL;
2393 struct fruit_config_data *config = NULL;
2394 SMB_STRUCT_STAT sb;
2396 rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2397 if (rc != 0) {
2398 return rc;
2401 SMB_VFS_HANDLE_GET_DATA(handle, config,
2402 struct fruit_config_data, return -1);
2404 if (config->rsrc == FRUIT_RSRC_XATTR) {
2405 return rc;
2408 /* FIXME: direct sys_lstat(), missing smb_fname */
2409 rc = sys_lstat(path, &sb, false);
2410 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2411 return rc;
2414 rc = adouble_path(talloc_tos(), path, &adp);
2415 if (rc != 0) {
2416 goto done;
2419 DEBUG(10, ("fruit_chown: %s\n", adp));
2421 rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2422 if (errno == ENOENT) {
2423 rc = 0;
2426 done:
2427 TALLOC_FREE(adp);
2428 return rc;
2431 static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2433 DIR *dh = NULL;
2434 struct dirent *de;
2435 struct fruit_config_data *config;
2437 SMB_VFS_HANDLE_GET_DATA(handle, config,
2438 struct fruit_config_data, return -1);
2440 if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2441 goto exit_rmdir;
2445 * Due to there is no way to change bDeleteVetoFiles variable
2446 * from this module, need to clean up ourselves
2448 dh = opendir(path);
2449 if (dh == NULL) {
2450 goto exit_rmdir;
2453 while ((de = readdir(dh)) != NULL) {
2454 if ((strncmp(de->d_name,
2455 ADOUBLE_NAME_PREFIX,
2456 strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2457 char *p = talloc_asprintf(talloc_tos(),
2458 "%s/%s",
2459 path, de->d_name);
2460 if (p == NULL) {
2461 goto exit_rmdir;
2463 DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2464 (void)unlink(p);
2465 TALLOC_FREE(p);
2469 exit_rmdir:
2470 if (dh) {
2471 closedir(dh);
2473 return SMB_VFS_NEXT_RMDIR(handle, path);
2476 static ssize_t fruit_pread(vfs_handle_struct *handle,
2477 files_struct *fsp, void *data,
2478 size_t n, off_t offset)
2480 int rc = 0;
2481 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2482 handle, fsp);
2483 struct fruit_config_data *config = NULL;
2484 AfpInfo *ai = NULL;
2485 ssize_t len;
2486 char *name = NULL;
2487 char *tmp_base_name = NULL;
2488 NTSTATUS status;
2490 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2492 if (!fsp->base_fsp) {
2493 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2496 SMB_VFS_HANDLE_GET_DATA(handle, config,
2497 struct fruit_config_data, return -1);
2499 /* fsp_name is not converted with vfs_catia */
2500 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2501 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2502 fsp->base_fsp->fsp_name->base_name,
2503 vfs_translate_to_unix,
2504 talloc_tos(), &name);
2505 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2506 name = talloc_strdup(talloc_tos(), tmp_base_name);
2507 if (name == NULL) {
2508 rc = -1;
2509 goto exit;
2511 } else if (!NT_STATUS_IS_OK(status)) {
2512 errno = map_errno_from_nt_status(status);
2513 rc = -1;
2514 goto exit;
2516 fsp->base_fsp->fsp_name->base_name = name;
2518 if (ad == NULL) {
2519 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2520 if (len == -1) {
2521 rc = -1;
2522 goto exit;
2524 goto exit;
2527 if (!fruit_fsp_recheck(ad, fsp)) {
2528 rc = -1;
2529 goto exit;
2532 if (ad->ad_type == ADOUBLE_META) {
2533 ai = afpinfo_new(talloc_tos());
2534 if (ai == NULL) {
2535 rc = -1;
2536 goto exit;
2539 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2540 if (len == -1) {
2541 rc = -1;
2542 goto exit;
2545 memcpy(&ai->afpi_FinderInfo[0],
2546 ad_entry(ad, ADEID_FINDERI),
2547 ADEDLEN_FINDERI);
2548 len = afpinfo_pack(ai, data);
2549 if (len != AFP_INFO_SIZE) {
2550 rc = -1;
2551 goto exit;
2553 } else {
2554 len = SMB_VFS_NEXT_PREAD(
2555 handle, fsp, data, n,
2556 offset + ad_getentryoff(ad, ADEID_RFORK));
2557 if (len == -1) {
2558 rc = -1;
2559 goto exit;
2562 exit:
2563 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2564 TALLOC_FREE(name);
2565 TALLOC_FREE(ai);
2566 if (rc != 0) {
2567 len = -1;
2569 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2570 return len;
2573 static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2574 files_struct *fsp, const void *data,
2575 size_t n, off_t offset)
2577 int rc = 0;
2578 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2579 handle, fsp);
2580 struct fruit_config_data *config = NULL;
2581 AfpInfo *ai = NULL;
2582 ssize_t len;
2583 char *name = NULL;
2584 char *tmp_base_name = NULL;
2585 NTSTATUS status;
2587 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2589 if (!fsp->base_fsp) {
2590 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2593 SMB_VFS_HANDLE_GET_DATA(handle, config,
2594 struct fruit_config_data, return -1);
2596 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2597 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2598 fsp->base_fsp->fsp_name->base_name,
2599 vfs_translate_to_unix,
2600 talloc_tos(), &name);
2601 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2602 name = talloc_strdup(talloc_tos(), tmp_base_name);
2603 if (name == NULL) {
2604 rc = -1;
2605 goto exit;
2607 } else if (!NT_STATUS_IS_OK(status)) {
2608 errno = map_errno_from_nt_status(status);
2609 rc = -1;
2610 goto exit;
2612 fsp->base_fsp->fsp_name->base_name = name;
2614 if (ad == NULL) {
2615 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2616 if (len != n) {
2617 rc = -1;
2618 goto exit;
2620 goto exit;
2623 if (!fruit_fsp_recheck(ad, fsp)) {
2624 rc = -1;
2625 goto exit;
2628 if (ad->ad_type == ADOUBLE_META) {
2629 if (n != AFP_INFO_SIZE || offset != 0) {
2630 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2631 (intmax_t)offset, (intmax_t)n));
2632 rc = -1;
2633 goto exit;
2635 ai = afpinfo_unpack(talloc_tos(), data);
2636 if (ai == NULL) {
2637 rc = -1;
2638 goto exit;
2640 memcpy(ad_entry(ad, ADEID_FINDERI),
2641 &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2642 rc = ad_write(ad, name);
2643 } else {
2644 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2645 offset + ad_getentryoff(ad, ADEID_RFORK));
2646 if (len != n) {
2647 rc = -1;
2648 goto exit;
2651 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2652 rc = ad_read(ad, name);
2653 if (rc == -1) {
2654 goto exit;
2656 rc = 0;
2658 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2659 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2660 rc = ad_write(ad, name);
2665 exit:
2666 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2667 TALLOC_FREE(name);
2668 TALLOC_FREE(ai);
2669 if (rc != 0) {
2670 return -1;
2672 return n;
2676 * Helper to stat/lstat the base file of an smb_fname.
2678 static int fruit_stat_base(vfs_handle_struct *handle,
2679 struct smb_filename *smb_fname,
2680 bool follow_links)
2682 char *tmp_stream_name;
2683 int rc;
2685 tmp_stream_name = smb_fname->stream_name;
2686 smb_fname->stream_name = NULL;
2687 if (follow_links) {
2688 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2689 } else {
2690 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2692 smb_fname->stream_name = tmp_stream_name;
2693 return rc;
2696 static int fruit_stat_meta(vfs_handle_struct *handle,
2697 struct smb_filename *smb_fname,
2698 bool follow_links)
2700 /* Populate the stat struct with info from the base file. */
2701 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2702 return -1;
2704 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2705 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2706 smb_fname->stream_name);
2707 return 0;
2710 static int fruit_stat_rsrc(vfs_handle_struct *handle,
2711 struct smb_filename *smb_fname,
2712 bool follow_links)
2715 struct adouble *ad = NULL;
2717 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2718 smb_fname_str_dbg(smb_fname)));
2720 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2721 if (ad == NULL) {
2722 errno = ENOENT;
2723 return -1;
2726 /* Populate the stat struct with info from the base file. */
2727 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2728 TALLOC_FREE(ad);
2729 return -1;
2732 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2733 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2734 smb_fname->stream_name);
2735 TALLOC_FREE(ad);
2736 return 0;
2739 static int fruit_stat(vfs_handle_struct *handle,
2740 struct smb_filename *smb_fname)
2742 int rc = -1;
2744 DEBUG(10, ("fruit_stat called for %s\n",
2745 smb_fname_str_dbg(smb_fname)));
2747 if (!is_ntfs_stream_smb_fname(smb_fname)
2748 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2749 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2750 if (rc == 0) {
2751 update_btime(handle, smb_fname);
2753 return rc;
2757 * Note if lp_posix_paths() is true, we can never
2758 * get here as is_ntfs_stream_smb_fname() is
2759 * always false. So we never need worry about
2760 * not following links here.
2763 if (is_afpinfo_stream(smb_fname)) {
2764 rc = fruit_stat_meta(handle, smb_fname, true);
2765 } else if (is_afpresource_stream(smb_fname)) {
2766 rc = fruit_stat_rsrc(handle, smb_fname, true);
2767 } else {
2768 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2771 if (rc == 0) {
2772 update_btime(handle, smb_fname);
2773 smb_fname->st.st_ex_mode &= ~S_IFMT;
2774 smb_fname->st.st_ex_mode |= S_IFREG;
2775 smb_fname->st.st_ex_blocks =
2776 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2778 return rc;
2781 static int fruit_lstat(vfs_handle_struct *handle,
2782 struct smb_filename *smb_fname)
2784 int rc = -1;
2786 DEBUG(10, ("fruit_lstat called for %s\n",
2787 smb_fname_str_dbg(smb_fname)));
2789 if (!is_ntfs_stream_smb_fname(smb_fname)
2790 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2791 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2792 if (rc == 0) {
2793 update_btime(handle, smb_fname);
2795 return rc;
2798 if (is_afpinfo_stream(smb_fname)) {
2799 rc = fruit_stat_meta(handle, smb_fname, false);
2800 } else if (is_afpresource_stream(smb_fname)) {
2801 rc = fruit_stat_rsrc(handle, smb_fname, false);
2802 } else {
2803 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2806 if (rc == 0) {
2807 update_btime(handle, smb_fname);
2808 smb_fname->st.st_ex_mode &= ~S_IFMT;
2809 smb_fname->st.st_ex_mode |= S_IFREG;
2810 smb_fname->st.st_ex_blocks =
2811 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2813 return rc;
2816 static int fruit_fstat_meta(vfs_handle_struct *handle,
2817 files_struct *fsp,
2818 SMB_STRUCT_STAT *sbuf)
2820 DEBUG(10, ("fruit_fstat_meta called for %s\n",
2821 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2823 /* Populate the stat struct with info from the base file. */
2824 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2825 return -1;
2827 *sbuf = fsp->base_fsp->fsp_name->st;
2828 sbuf->st_ex_size = AFP_INFO_SIZE;
2829 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2831 return 0;
2834 static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
2835 SMB_STRUCT_STAT *sbuf)
2837 struct fruit_config_data *config;
2838 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2839 handle, fsp);
2841 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
2842 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
2844 SMB_VFS_HANDLE_GET_DATA(handle, config,
2845 struct fruit_config_data, return -1);
2847 if (config->rsrc == FRUIT_RSRC_STREAM) {
2848 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2851 /* Populate the stat struct with info from the base file. */
2852 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
2853 return -1;
2855 *sbuf = fsp->base_fsp->fsp_name->st;
2856 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2857 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
2859 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
2860 smb_fname_str_dbg(fsp->fsp_name),
2861 (ssize_t)sbuf->st_ex_size));
2863 return 0;
2866 static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
2867 SMB_STRUCT_STAT *sbuf)
2869 int rc;
2870 char *name = NULL;
2871 char *tmp_base_name = NULL;
2872 NTSTATUS status;
2873 struct adouble *ad = (struct adouble *)
2874 VFS_FETCH_FSP_EXTENSION(handle, fsp);
2876 DEBUG(10, ("fruit_fstat called for %s\n",
2877 smb_fname_str_dbg(fsp->fsp_name)));
2879 if (fsp->base_fsp) {
2880 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2881 /* fsp_name is not converted with vfs_catia */
2882 status = SMB_VFS_TRANSLATE_NAME(
2883 handle->conn,
2884 fsp->base_fsp->fsp_name->base_name,
2885 vfs_translate_to_unix,
2886 talloc_tos(), &name);
2888 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2889 name = talloc_strdup(talloc_tos(), tmp_base_name);
2890 if (name == NULL) {
2891 rc = -1;
2892 goto exit;
2894 } else if (!NT_STATUS_IS_OK(status)) {
2895 errno = map_errno_from_nt_status(status);
2896 rc = -1;
2897 goto exit;
2899 fsp->base_fsp->fsp_name->base_name = name;
2902 if (ad == NULL || fsp->base_fsp == NULL) {
2903 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
2904 goto exit;
2907 if (!fruit_fsp_recheck(ad, fsp)) {
2908 rc = -1;
2909 goto exit;
2912 switch (ad->ad_type) {
2913 case ADOUBLE_META:
2914 rc = fruit_fstat_meta(handle, fsp, sbuf);
2915 break;
2916 case ADOUBLE_RSRC:
2917 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
2918 break;
2919 default:
2920 DEBUG(10, ("fruit_fstat %s: bad type\n",
2921 smb_fname_str_dbg(fsp->fsp_name)));
2922 rc = -1;
2923 goto exit;
2926 if (rc == 0) {
2927 sbuf->st_ex_mode &= ~S_IFMT;
2928 sbuf->st_ex_mode |= S_IFREG;
2929 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
2932 exit:
2933 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
2934 smb_fname_str_dbg(fsp->fsp_name),
2935 (ssize_t)sbuf->st_ex_size));
2936 if (tmp_base_name) {
2937 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2939 TALLOC_FREE(name);
2940 return rc;
2943 static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
2944 struct files_struct *fsp,
2945 const char *fname,
2946 TALLOC_CTX *mem_ctx,
2947 unsigned int *pnum_streams,
2948 struct stream_struct **pstreams)
2950 struct fruit_config_data *config = NULL;
2951 struct smb_filename *smb_fname = NULL;
2952 struct adouble *ad = NULL;
2954 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
2955 return NT_STATUS_UNSUCCESSFUL);
2956 DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
2958 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
2959 if (smb_fname == NULL) {
2960 return NT_STATUS_NO_MEMORY;
2963 if (config->meta == FRUIT_META_NETATALK) {
2964 ad = ad_get(talloc_tos(), handle,
2965 smb_fname->base_name, ADOUBLE_META);
2966 if (ad && !empty_finderinfo(ad)) {
2967 if (!add_fruit_stream(
2968 mem_ctx, pnum_streams, pstreams,
2969 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
2970 smb_roundup(handle->conn,
2971 AFP_INFO_SIZE))) {
2972 TALLOC_FREE(ad);
2973 TALLOC_FREE(smb_fname);
2974 return NT_STATUS_NO_MEMORY;
2977 TALLOC_FREE(ad);
2980 if (config->rsrc != FRUIT_RSRC_STREAM) {
2981 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
2982 ADOUBLE_RSRC);
2983 if (ad) {
2984 if (!add_fruit_stream(
2985 mem_ctx, pnum_streams, pstreams,
2986 AFPRESOURCE_STREAM_NAME,
2987 ad_getentrylen(ad, ADEID_RFORK),
2988 smb_roundup(handle->conn,
2989 ad_getentrylen(
2990 ad, ADEID_RFORK)))) {
2991 TALLOC_FREE(ad);
2992 TALLOC_FREE(smb_fname);
2993 return NT_STATUS_NO_MEMORY;
2996 TALLOC_FREE(ad);
2999 TALLOC_FREE(smb_fname);
3001 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3002 pnum_streams, pstreams);
3005 static int fruit_ntimes(vfs_handle_struct *handle,
3006 const struct smb_filename *smb_fname,
3007 struct smb_file_time *ft)
3009 int rc = 0;
3010 struct adouble *ad = NULL;
3012 if (null_timespec(ft->create_time)) {
3013 goto exit;
3016 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3017 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3019 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3020 if (ad == NULL) {
3021 goto exit;
3024 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3025 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3027 rc = ad_write(ad, smb_fname->base_name);
3029 exit:
3031 TALLOC_FREE(ad);
3032 if (rc != 0) {
3033 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3034 return -1;
3036 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3039 static int fruit_fallocate(struct vfs_handle_struct *handle,
3040 struct files_struct *fsp,
3041 enum vfs_fallocate_mode mode,
3042 off_t offset,
3043 off_t len)
3045 struct adouble *ad =
3046 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3048 if (ad == NULL) {
3049 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3052 if (!fruit_fsp_recheck(ad, fsp)) {
3053 return -1;
3056 /* Let the pwrite code path handle it. */
3057 errno = ENOSYS;
3058 return -1;
3061 static int fruit_ftruncate(struct vfs_handle_struct *handle,
3062 struct files_struct *fsp,
3063 off_t offset)
3065 int rc = 0;
3066 struct adouble *ad =
3067 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3068 struct fruit_config_data *config;
3070 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
3071 fsp_str_dbg(fsp), (double)offset));
3073 if (ad == NULL) {
3074 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3077 if (!fruit_fsp_recheck(ad, fsp)) {
3078 return -1;
3081 SMB_VFS_HANDLE_GET_DATA(handle, config,
3082 struct fruit_config_data, return -1);
3084 switch (ad->ad_type) {
3085 case ADOUBLE_META:
3087 * As this request hasn't been seen in the wild,
3088 * the only sensible use I can imagine is the client
3089 * truncating the stream to 0 bytes size.
3090 * We simply remove the metadata on such a request.
3092 if (offset == 0) {
3093 rc = SMB_VFS_FREMOVEXATTR(fsp,
3094 AFPRESOURCE_EA_NETATALK);
3096 break;
3097 case ADOUBLE_RSRC:
3098 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3099 rc = SMB_VFS_FREMOVEXATTR(fsp,
3100 AFPRESOURCE_EA_NETATALK);
3101 } else {
3102 rc = SMB_VFS_NEXT_FTRUNCATE(
3103 handle, fsp,
3104 offset + ad_getentryoff(ad, ADEID_RFORK));
3105 if (rc != 0) {
3106 return -1;
3108 ad_setentrylen(ad, ADEID_RFORK, offset);
3109 rc = ad_write(ad, NULL);
3110 if (rc != 0) {
3111 return -1;
3114 break;
3115 default:
3116 return -1;
3119 return rc;
3122 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3123 struct smb_request *req,
3124 uint16_t root_dir_fid,
3125 struct smb_filename *smb_fname,
3126 uint32_t access_mask,
3127 uint32_t share_access,
3128 uint32_t create_disposition,
3129 uint32_t create_options,
3130 uint32_t file_attributes,
3131 uint32_t oplock_request,
3132 struct smb2_lease *lease,
3133 uint64_t allocation_size,
3134 uint32_t private_flags,
3135 struct security_descriptor *sd,
3136 struct ea_list *ea_list,
3137 files_struct **result,
3138 int *pinfo,
3139 const struct smb2_create_blobs *in_context_blobs,
3140 struct smb2_create_blobs *out_context_blobs)
3142 NTSTATUS status;
3143 struct fruit_config_data *config = NULL;
3145 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3146 if (!NT_STATUS_IS_OK(status)) {
3147 return status;
3150 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3151 return NT_STATUS_UNSUCCESSFUL);
3153 status = SMB_VFS_NEXT_CREATE_FILE(
3154 handle, req, root_dir_fid, smb_fname,
3155 access_mask, share_access,
3156 create_disposition, create_options,
3157 file_attributes, oplock_request,
3158 lease,
3159 allocation_size, private_flags,
3160 sd, ea_list, result,
3161 pinfo, in_context_blobs, out_context_blobs);
3162 if (!NT_STATUS_IS_OK(status)) {
3163 return status;
3166 if (is_ntfs_stream_smb_fname(smb_fname)
3167 || (*result == NULL)
3168 || ((*result)->is_directory)) {
3169 return status;
3172 if (config->locking == FRUIT_LOCKING_NETATALK) {
3173 status = fruit_check_access(
3174 handle, *result,
3175 access_mask,
3176 map_share_mode_to_deny_mode(share_access, 0));
3177 if (!NT_STATUS_IS_OK(status)) {
3178 goto fail;
3182 return status;
3184 fail:
3185 DEBUG(1, ("fruit_create_file: %s\n", nt_errstr(status)));
3187 if (*result) {
3188 close_file(req, *result, ERROR_CLOSE);
3189 *result = NULL;
3192 return status;
3195 static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3196 const struct smb_filename *fname,
3197 TALLOC_CTX *mem_ctx,
3198 struct readdir_attr_data **pattr_data)
3200 struct fruit_config_data *config = NULL;
3201 struct readdir_attr_data *attr_data;
3202 NTSTATUS status;
3204 SMB_VFS_HANDLE_GET_DATA(handle, config,
3205 struct fruit_config_data,
3206 return NT_STATUS_UNSUCCESSFUL);
3208 if (!config->use_aapl) {
3209 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3212 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3214 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3215 if (*pattr_data == NULL) {
3216 return NT_STATUS_UNSUCCESSFUL;
3218 attr_data = *pattr_data;
3219 attr_data->type = RDATTR_AAPL;
3222 * Mac metadata: compressed FinderInfo, resource fork length
3223 * and creation date
3225 status = readdir_attr_macmeta(handle, fname, attr_data);
3226 if (!NT_STATUS_IS_OK(status)) {
3228 * Error handling is tricky: if we return failure from
3229 * this function, the corresponding directory entry
3230 * will to be passed to the client, so we really just
3231 * want to error out on fatal errors.
3233 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3234 goto fail;
3239 * UNIX mode
3241 if (config->unix_info_enabled) {
3242 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3246 * max_access
3248 if (!config->readdir_attr_max_access) {
3249 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3250 } else {
3251 status = smbd_calculate_access_mask(
3252 handle->conn,
3253 fname,
3254 false,
3255 SEC_FLAG_MAXIMUM_ALLOWED,
3256 &attr_data->attr_data.aapl.max_access);
3257 if (!NT_STATUS_IS_OK(status)) {
3258 goto fail;
3262 return NT_STATUS_OK;
3264 fail:
3265 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3266 fname->base_name, nt_errstr(status)));
3267 TALLOC_FREE(*pattr_data);
3268 return status;
3271 static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3272 files_struct *fsp,
3273 uint32 security_info,
3274 TALLOC_CTX *mem_ctx,
3275 struct security_descriptor **ppdesc)
3277 NTSTATUS status;
3278 struct security_ace ace;
3279 struct dom_sid sid;
3280 struct fruit_config_data *config;
3282 SMB_VFS_HANDLE_GET_DATA(handle, config,
3283 struct fruit_config_data,
3284 return NT_STATUS_UNSUCCESSFUL);
3286 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3287 mem_ctx, ppdesc);
3288 if (!NT_STATUS_IS_OK(status)) {
3289 return status;
3293 * Add MS NFS style ACEs with uid, gid and mode
3295 if (!config->unix_info_enabled) {
3296 return NT_STATUS_OK;
3299 /* MS NFS style mode */
3300 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3301 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3302 status = security_descriptor_dacl_add(*ppdesc, &ace);
3303 if (!NT_STATUS_IS_OK(status)) {
3304 DEBUG(1,("failed to add MS NFS style ACE\n"));
3305 return status;
3308 /* MS NFS style uid */
3309 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3310 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3311 status = security_descriptor_dacl_add(*ppdesc, &ace);
3312 if (!NT_STATUS_IS_OK(status)) {
3313 DEBUG(1,("failed to add MS NFS style ACE\n"));
3314 return status;
3317 /* MS NFS style gid */
3318 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3319 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3320 status = security_descriptor_dacl_add(*ppdesc, &ace);
3321 if (!NT_STATUS_IS_OK(status)) {
3322 DEBUG(1,("failed to add MS NFS style ACE\n"));
3323 return status;
3326 return NT_STATUS_OK;
3329 static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3330 files_struct *fsp,
3331 uint32 security_info_sent,
3332 const struct security_descriptor *psd)
3334 NTSTATUS status;
3335 bool do_chmod;
3336 mode_t ms_nfs_mode;
3337 int result;
3339 DEBUG(1, ("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp)));
3341 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3342 if (!NT_STATUS_IS_OK(status)) {
3343 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3344 return status;
3347 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3348 if (!NT_STATUS_IS_OK(status)) {
3349 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3350 return status;
3353 if (do_chmod) {
3354 if (fsp->fh->fd != -1) {
3355 DEBUG(1, ("fchmod: %s\n", fsp_str_dbg(fsp)));
3356 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3357 } else {
3358 DEBUG(1, ("chmod: %s\n", fsp_str_dbg(fsp)));
3359 result = SMB_VFS_CHMOD(fsp->conn,
3360 fsp->fsp_name->base_name,
3361 ms_nfs_mode);
3364 if (result != 0) {
3365 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3366 result, ms_nfs_mode, strerror(errno)));
3367 status = map_nt_error_from_unix(errno);
3368 return status;
3372 return NT_STATUS_OK;
3375 static struct vfs_fn_pointers vfs_fruit_fns = {
3376 .connect_fn = fruit_connect,
3378 /* File operations */
3379 .chmod_fn = fruit_chmod,
3380 .chown_fn = fruit_chown,
3381 .unlink_fn = fruit_unlink,
3382 .rename_fn = fruit_rename,
3383 .rmdir_fn = fruit_rmdir,
3384 .open_fn = fruit_open,
3385 .pread_fn = fruit_pread,
3386 .pwrite_fn = fruit_pwrite,
3387 .stat_fn = fruit_stat,
3388 .lstat_fn = fruit_lstat,
3389 .fstat_fn = fruit_fstat,
3390 .streaminfo_fn = fruit_streaminfo,
3391 .ntimes_fn = fruit_ntimes,
3392 .ftruncate_fn = fruit_ftruncate,
3393 .fallocate_fn = fruit_fallocate,
3394 .create_file_fn = fruit_create_file,
3395 .readdir_attr_fn = fruit_readdir_attr,
3397 /* NT ACL operations */
3398 .fget_nt_acl_fn = fruit_fget_nt_acl,
3399 .fset_nt_acl_fn = fruit_fset_nt_acl,
3402 NTSTATUS vfs_fruit_init(void);
3403 NTSTATUS vfs_fruit_init(void)
3405 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3406 &vfs_fruit_fns);
3407 if (!NT_STATUS_IS_OK(ret)) {
3408 return ret;
3411 vfs_fruit_debug_level = debug_add_class("fruit");
3412 if (vfs_fruit_debug_level == -1) {
3413 vfs_fruit_debug_level = DBGC_VFS;
3414 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3415 "vfs_fruit_init"));
3416 } else {
3417 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3418 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3421 return ret;