source: vendor/current/source3/modules/vfs_fruit.c

Last change on this file was 989, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.7

File size: 94.4 KB
Line 
1/*
2 * OS X and Netatalk interoperability VFS module for Samba-3.x
3 *
4 * Copyright (C) Ralph Boehme, 2013, 2014
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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/util/sys_rw.h"
33#include "lib/util/tevent_ntstatus.h"
34
35/*
36 * Enhanced OS X and Netatalk compatibility
37 * ========================================
38 *
39 * This modules takes advantage of vfs_streams_xattr and
40 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
41 * loaded in the correct order:
42 *
43 * vfs modules = catia fruit streams_xattr
44 *
45 * The module intercepts the OS X special streams "AFP_AfpInfo" and
46 * "AFP_Resource" and handles them in a special way. All other named
47 * streams are deferred to vfs_streams_xattr.
48 *
49 * The OS X client maps all NTFS illegal characters to the Unicode
50 * private range. This module optionally stores the charcters using
51 * their native ASCII encoding using vfs_catia. If you're not enabling
52 * this feature, you can skip catia from vfs modules.
53 *
54 * Finally, open modes are optionally checked against Netatalk AFP
55 * share modes.
56 *
57 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
58 * extended metadata for files and directories. This module optionally
59 * reads and stores this metadata in a way compatible with Netatalk 3
60 * which stores the metadata in an EA "org.netatalk.metadata". Cf
61 * source3/include/MacExtensions.h for a description of the binary
62 * blobs content.
63 *
64 * The "AFP_Resource" named stream may be arbitrarily large, thus it
65 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
66 * the only available filesystem where xattrs can be of any size and
67 * the OS supports using the file APIs for xattrs.
68 *
69 * The AFP_Resource stream is stored in an AppleDouble file prepending
70 * "._" to the filename. On Solaris with ZFS the stream is optionally
71 * stored in an EA "org.netatalk.ressource".
72 *
73 *
74 * Extended Attributes
75 * ===================
76 *
77 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
78 * other protocols you may want to adjust the xattr names the VFS
79 * module vfs_streams_xattr uses for storing ADS's. This defaults to
80 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
81 * these module parameters:
82 *
83 * streams_xattr:prefix = user.
84 * streams_xattr:store_stream_type = false
85 *
86 *
87 * TODO
88 * ====
89 *
90 * - log diagnostic if any needed VFS module is not loaded
91 * (eg with lp_vfs_objects())
92 * - add tests
93 */
94
95static int vfs_fruit_debug_level = DBGC_VFS;
96
97#undef DBGC_CLASS
98#define DBGC_CLASS vfs_fruit_debug_level
99
100#define FRUIT_PARAM_TYPE_NAME "fruit"
101#define ADOUBLE_NAME_PREFIX "._"
102
103/*
104 * REVIEW:
105 * This is hokey, but what else can we do?
106 */
107#define NETATALK_META_XATTR "org.netatalk.Metadata"
108#if defined(HAVE_ATTROPEN) || defined(FREEBSD)
109#define AFPINFO_EA_NETATALK NETATALK_META_XATTR
110#define AFPRESOURCE_EA_NETATALK "org.netatalk.ResourceFork"
111#else
112#define AFPINFO_EA_NETATALK "user." NETATALK_META_XATTR
113#define AFPRESOURCE_EA_NETATALK "user.org.netatalk.ResourceFork"
114#endif
115
116enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
117
118enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
119enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
120enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
121enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
122
123struct fruit_config_data {
124 enum fruit_rsrc rsrc;
125 enum fruit_meta meta;
126 enum fruit_locking locking;
127 enum fruit_encoding encoding;
128 bool use_aapl; /* config from smb.conf */
129 bool nego_aapl; /* client negotiated AAPL */
130 bool use_copyfile;
131 bool readdir_attr_enabled;
132 bool unix_info_enabled;
133 bool copyfile_enabled;
134 bool veto_appledouble;
135 bool posix_rename;
136
137 /*
138 * Additional options, all enabled by default,
139 * possibly useful for analyzing performance. The associated
140 * operations with each of them may be expensive, so having
141 * the chance to disable them individually gives a chance
142 * tweaking the setup for the particular usecase.
143 */
144 bool readdir_attr_rsize;
145 bool readdir_attr_finder_info;
146 bool readdir_attr_max_access;
147};
148
149static const struct enum_list fruit_rsrc[] = {
150 {FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
151 {FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
152 {FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
153 { -1, NULL}
154};
155
156static const struct enum_list fruit_meta[] = {
157 {FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
159 { -1, NULL}
160};
161
162static const struct enum_list fruit_locking[] = {
163 {FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
164 {FRUIT_LOCKING_NONE, "none"},
165 { -1, NULL}
166};
167
168static const struct enum_list fruit_encoding[] = {
169 {FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
170 {FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
171 { -1, NULL}
172};
173
174/*****************************************************************************
175 * Defines, functions and data structures that deal with AppleDouble
176 *****************************************************************************/
177
178/*
179 * There are two AppleDouble blobs we deal with:
180 *
181 * - ADOUBLE_META - AppleDouble blob used by Netatalk for storing
182 * metadata in an xattr
183 *
184 * - ADOUBLE_RSRC - AppleDouble blob used by OS X and Netatalk in
185 * ._ files
186 */
187typedef enum {ADOUBLE_META, ADOUBLE_RSRC} adouble_type_t;
188
189/* Version info */
190#define AD_VERSION2 0x00020000
191#define AD_VERSION AD_VERSION2
192
193/*
194 * AppleDouble entry IDs.
195 */
196#define ADEID_DFORK 1
197#define ADEID_RFORK 2
198#define ADEID_NAME 3
199#define ADEID_COMMENT 4
200#define ADEID_ICONBW 5
201#define ADEID_ICONCOL 6
202#define ADEID_FILEI 7
203#define ADEID_FILEDATESI 8
204#define ADEID_FINDERI 9
205#define ADEID_MACFILEI 10
206#define ADEID_PRODOSFILEI 11
207#define ADEID_MSDOSFILEI 12
208#define ADEID_SHORTNAME 13
209#define ADEID_AFPFILEI 14
210#define ADEID_DID 15
211
212/* Private Netatalk entries */
213#define ADEID_PRIVDEV 16
214#define ADEID_PRIVINO 17
215#define ADEID_PRIVSYN 18
216#define ADEID_PRIVID 19
217#define ADEID_MAX (ADEID_PRIVID + 1)
218
219/*
220 * These are the real ids for the private entries,
221 * as stored in the adouble file
222 */
223#define AD_DEV 0x80444556
224#define AD_INO 0x80494E4F
225#define AD_SYN 0x8053594E
226#define AD_ID 0x8053567E
227
228/* Number of actually used entries */
229#define ADEID_NUM_XATTR 8
230#define ADEID_NUM_DOT_UND 2
231#define ADEID_NUM_RSRC_XATTR 1
232
233/* AppleDouble magic */
234#define AD_APPLESINGLE_MAGIC 0x00051600
235#define AD_APPLEDOUBLE_MAGIC 0x00051607
236#define AD_MAGIC AD_APPLEDOUBLE_MAGIC
237
238/* Sizes of relevant entry bits */
239#define ADEDLEN_MAGIC 4
240#define ADEDLEN_VERSION 4
241#define ADEDLEN_FILLER 16
242#define AD_FILLER_TAG "Netatalk " /* should be 16 bytes */
243#define ADEDLEN_NENTRIES 2
244#define AD_HEADER_LEN (ADEDLEN_MAGIC + ADEDLEN_VERSION + \
245 ADEDLEN_FILLER + ADEDLEN_NENTRIES) /* 26 */
246#define AD_ENTRY_LEN_EID 4
247#define AD_ENTRY_LEN_OFF 4
248#define AD_ENTRY_LEN_LEN 4
249#define AD_ENTRY_LEN (AD_ENTRY_LEN_EID + AD_ENTRY_LEN_OFF + AD_ENTRY_LEN_LEN)
250
251/* Field widths */
252#define ADEDLEN_NAME 255
253#define ADEDLEN_COMMENT 200
254#define ADEDLEN_FILEI 16
255#define ADEDLEN_FINDERI 32
256#define ADEDLEN_FILEDATESI 16
257#define ADEDLEN_SHORTNAME 12 /* length up to 8.3 */
258#define ADEDLEN_AFPFILEI 4
259#define ADEDLEN_MACFILEI 4
260#define ADEDLEN_PRODOSFILEI 8
261#define ADEDLEN_MSDOSFILEI 2
262#define ADEDLEN_DID 4
263#define ADEDLEN_PRIVDEV 8
264#define ADEDLEN_PRIVINO 8
265#define ADEDLEN_PRIVSYN 8
266#define ADEDLEN_PRIVID 4
267
268/* Offsets */
269#define ADEDOFF_MAGIC 0
270#define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
271#define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
272#define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
273
274#define ADEDOFF_FINDERI_XATTR (AD_HEADER_LEN + \
275 (ADEID_NUM_XATTR * AD_ENTRY_LEN))
276#define ADEDOFF_COMMENT_XATTR (ADEDOFF_FINDERI_XATTR + ADEDLEN_FINDERI)
277#define ADEDOFF_FILEDATESI_XATTR (ADEDOFF_COMMENT_XATTR + ADEDLEN_COMMENT)
278#define ADEDOFF_AFPFILEI_XATTR (ADEDOFF_FILEDATESI_XATTR + \
279 ADEDLEN_FILEDATESI)
280#define ADEDOFF_PRIVDEV_XATTR (ADEDOFF_AFPFILEI_XATTR + ADEDLEN_AFPFILEI)
281#define ADEDOFF_PRIVINO_XATTR (ADEDOFF_PRIVDEV_XATTR + ADEDLEN_PRIVDEV)
282#define ADEDOFF_PRIVSYN_XATTR (ADEDOFF_PRIVINO_XATTR + ADEDLEN_PRIVINO)
283#define ADEDOFF_PRIVID_XATTR (ADEDOFF_PRIVSYN_XATTR + ADEDLEN_PRIVSYN)
284
285#define ADEDOFF_FINDERI_DOT_UND (AD_HEADER_LEN + \
286 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN))
287#define ADEDOFF_RFORK_DOT_UND (ADEDOFF_FINDERI_DOT_UND + ADEDLEN_FINDERI)
288
289#define AD_DATASZ_XATTR (AD_HEADER_LEN + \
290 (ADEID_NUM_XATTR * AD_ENTRY_LEN) + \
291 ADEDLEN_FINDERI + ADEDLEN_COMMENT + \
292 ADEDLEN_FILEDATESI + ADEDLEN_AFPFILEI + \
293 ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
294 ADEDLEN_PRIVSYN + ADEDLEN_PRIVID)
295
296#if AD_DATASZ_XATTR != 402
297#error bad size for AD_DATASZ_XATTR
298#endif
299
300#define AD_DATASZ_DOT_UND (AD_HEADER_LEN + \
301 (ADEID_NUM_DOT_UND * AD_ENTRY_LEN) + \
302 ADEDLEN_FINDERI)
303#if AD_DATASZ_DOT_UND != 82
304#error bad size for AD_DATASZ_DOT_UND
305#endif
306
307/*
308 * Sharemode locks fcntl() offsets
309 */
310#if _FILE_OFFSET_BITS == 64 || defined(HAVE_LARGEFILE)
311#define AD_FILELOCK_BASE (UINT64_C(0x7FFFFFFFFFFFFFFF) - 9)
312#else
313#define AD_FILELOCK_BASE (UINT32_C(0x7FFFFFFF) - 9)
314#endif
315#define BYTELOCK_MAX (AD_FILELOCK_BASE - 1)
316
317#define AD_FILELOCK_OPEN_WR (AD_FILELOCK_BASE + 0)
318#define AD_FILELOCK_OPEN_RD (AD_FILELOCK_BASE + 1)
319#define AD_FILELOCK_RSRC_OPEN_WR (AD_FILELOCK_BASE + 2)
320#define AD_FILELOCK_RSRC_OPEN_RD (AD_FILELOCK_BASE + 3)
321#define AD_FILELOCK_DENY_WR (AD_FILELOCK_BASE + 4)
322#define AD_FILELOCK_DENY_RD (AD_FILELOCK_BASE + 5)
323#define AD_FILELOCK_RSRC_DENY_WR (AD_FILELOCK_BASE + 6)
324#define AD_FILELOCK_RSRC_DENY_RD (AD_FILELOCK_BASE + 7)
325#define AD_FILELOCK_OPEN_NONE (AD_FILELOCK_BASE + 8)
326#define AD_FILELOCK_RSRC_OPEN_NONE (AD_FILELOCK_BASE + 9)
327
328/* Time stuff we overload the bits a little */
329#define AD_DATE_CREATE 0
330#define AD_DATE_MODIFY 4
331#define AD_DATE_BACKUP 8
332#define AD_DATE_ACCESS 12
333#define AD_DATE_MASK (AD_DATE_CREATE | AD_DATE_MODIFY | \
334 AD_DATE_BACKUP | AD_DATE_ACCESS)
335#define AD_DATE_UNIX (1 << 10)
336#define AD_DATE_START 0x80000000
337#define AD_DATE_DELTA 946684800
338#define AD_DATE_FROM_UNIX(x) (htonl((x) - AD_DATE_DELTA))
339#define AD_DATE_TO_UNIX(x) (ntohl(x) + AD_DATE_DELTA)
340
341/* Accessor macros */
342#define ad_getentrylen(ad,eid) ((ad)->ad_eid[(eid)].ade_len)
343#define ad_getentryoff(ad,eid) ((ad)->ad_eid[(eid)].ade_off)
344#define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
345#define ad_setentryoff(ad,eid,off) ((ad)->ad_eid[(eid)].ade_off = (off))
346#define ad_entry(ad,eid) ((ad)->ad_data + ad_getentryoff((ad),(eid)))
347
348struct ad_entry {
349 size_t ade_off;
350 size_t ade_len;
351};
352
353struct adouble {
354 vfs_handle_struct *ad_handle;
355 files_struct *ad_fsp;
356 adouble_type_t ad_type;
357 uint32_t ad_magic;
358 uint32_t ad_version;
359 struct ad_entry ad_eid[ADEID_MAX];
360 char *ad_data;
361};
362
363struct ad_entry_order {
364 uint32_t id, offset, len;
365};
366
367/* Netatalk AppleDouble metadata xattr */
368static const
369struct ad_entry_order entry_order_meta_xattr[ADEID_NUM_XATTR + 1] = {
370 {ADEID_FINDERI, ADEDOFF_FINDERI_XATTR, ADEDLEN_FINDERI},
371 {ADEID_COMMENT, ADEDOFF_COMMENT_XATTR, 0},
372 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_XATTR, ADEDLEN_FILEDATESI},
373 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_XATTR, ADEDLEN_AFPFILEI},
374 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV_XATTR, 0},
375 {ADEID_PRIVINO, ADEDOFF_PRIVINO_XATTR, 0},
376 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN_XATTR, 0},
377 {ADEID_PRIVID, ADEDOFF_PRIVID_XATTR, 0},
378 {0, 0, 0}
379};
380
381/* AppleDouble ressource fork file (the ones prefixed by "._") */
382static const
383struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
384 {ADEID_FINDERI, ADEDOFF_FINDERI_DOT_UND, ADEDLEN_FINDERI},
385 {ADEID_RFORK, ADEDOFF_RFORK_DOT_UND, 0},
386 {0, 0, 0}
387};
388
389/*
390 * Fake AppleDouble entry oder for ressource fork xattr. The xattr
391 * isn't an AppleDouble file, it simply contains the ressource data,
392 * but in order to be able to use some API calls like ad_getentryoff()
393 * we build a fake/helper struct adouble with this entry order struct.
394 */
395static const
396struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
397 {ADEID_RFORK, 0, 0},
398 {0, 0, 0}
399};
400
401/* Conversion from enumerated id to on-disk AppleDouble id */
402#define AD_EID_DISK(a) (set_eid[a])
403static const uint32_t set_eid[] = {
404 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
405 AD_DEV, AD_INO, AD_SYN, AD_ID
406};
407
408/*
409 * Forward declarations
410 */
411static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
412 adouble_type_t type, files_struct *fsp);
413static int ad_write(struct adouble *ad, const char *path);
414static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
415
416/**
417 * Get a date
418 **/
419static int ad_getdate(const struct adouble *ad,
420 unsigned int dateoff,
421 uint32_t *date)
422{
423 bool xlate = (dateoff & AD_DATE_UNIX);
424
425 dateoff &= AD_DATE_MASK;
426 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
427 return -1;
428 }
429
430 if (dateoff > AD_DATE_ACCESS) {
431 return -1;
432 }
433 memcpy(date,
434 ad_entry(ad, ADEID_FILEDATESI) + dateoff,
435 sizeof(uint32_t));
436
437 if (xlate) {
438 *date = AD_DATE_TO_UNIX(*date);
439 }
440 return 0;
441}
442
443/**
444 * Set a date
445 **/
446static int ad_setdate(struct adouble *ad, unsigned int dateoff, uint32_t date)
447{
448 bool xlate = (dateoff & AD_DATE_UNIX);
449
450 if (!ad_getentryoff(ad, ADEID_FILEDATESI)) {
451 return 0;
452 }
453
454 dateoff &= AD_DATE_MASK;
455 if (xlate) {
456 date = AD_DATE_FROM_UNIX(date);
457 }
458
459 if (dateoff > AD_DATE_ACCESS) {
460 return -1;
461 }
462
463 memcpy(ad_entry(ad, ADEID_FILEDATESI) + dateoff, &date, sizeof(date));
464
465 return 0;
466}
467
468
469/**
470 * Map on-disk AppleDouble id to enumerated id
471 **/
472static uint32_t get_eid(uint32_t eid)
473{
474 if (eid <= 15) {
475 return eid;
476 }
477
478 switch (eid) {
479 case AD_DEV:
480 return ADEID_PRIVDEV;
481 case AD_INO:
482 return ADEID_PRIVINO;
483 case AD_SYN:
484 return ADEID_PRIVSYN;
485 case AD_ID:
486 return ADEID_PRIVID;
487 default:
488 break;
489 }
490
491 return 0;
492}
493
494/**
495 * Pack AppleDouble structure into data buffer
496 **/
497static bool ad_pack(struct adouble *ad)
498{
499 uint32_t eid;
500 uint16_t nent;
501 uint32_t bufsize;
502 uint32_t offset = 0;
503
504 bufsize = talloc_get_size(ad->ad_data);
505
506 if (offset + ADEDLEN_MAGIC < offset ||
507 offset + ADEDLEN_MAGIC >= bufsize) {
508 return false;
509 }
510 RSIVAL(ad->ad_data, offset, ad->ad_magic);
511 offset += ADEDLEN_MAGIC;
512
513 if (offset + ADEDLEN_VERSION < offset ||
514 offset + ADEDLEN_VERSION >= bufsize) {
515 return false;
516 }
517 RSIVAL(ad->ad_data, offset, ad->ad_version);
518 offset += ADEDLEN_VERSION;
519
520 if (offset + ADEDLEN_FILLER < offset ||
521 offset + ADEDLEN_FILLER >= bufsize) {
522 return false;
523 }
524 if (ad->ad_type == ADOUBLE_RSRC) {
525 memcpy(ad->ad_data + offset, AD_FILLER_TAG, ADEDLEN_FILLER);
526 }
527 offset += ADEDLEN_FILLER;
528
529 if (offset + ADEDLEN_NENTRIES < offset ||
530 offset + ADEDLEN_NENTRIES >= bufsize) {
531 return false;
532 }
533 offset += ADEDLEN_NENTRIES;
534
535 for (eid = 0, nent = 0; eid < ADEID_MAX; eid++) {
536 if (ad->ad_eid[eid].ade_off == 0) {
537 /*
538 * ade_off is also used as indicator whether a
539 * specific entry is used or not
540 */
541 continue;
542 }
543
544 if (offset + AD_ENTRY_LEN_EID < offset ||
545 offset + AD_ENTRY_LEN_EID >= bufsize) {
546 return false;
547 }
548 RSIVAL(ad->ad_data, offset, AD_EID_DISK(eid));
549 offset += AD_ENTRY_LEN_EID;
550
551 if (offset + AD_ENTRY_LEN_OFF < offset ||
552 offset + AD_ENTRY_LEN_OFF >= bufsize) {
553 return false;
554 }
555 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_off);
556 offset += AD_ENTRY_LEN_OFF;
557
558 if (offset + AD_ENTRY_LEN_LEN < offset ||
559 offset + AD_ENTRY_LEN_LEN >= bufsize) {
560 return false;
561 }
562 RSIVAL(ad->ad_data, offset, ad->ad_eid[eid].ade_len);
563 offset += AD_ENTRY_LEN_LEN;
564
565 nent++;
566 }
567
568 if (ADEDOFF_NENTRIES + 2 >= bufsize) {
569 return false;
570 }
571 RSSVAL(ad->ad_data, ADEDOFF_NENTRIES, nent);
572
573 return true;
574}
575
576/**
577 * Unpack an AppleDouble blob into a struct adoble
578 **/
579static bool ad_unpack(struct adouble *ad, const int nentries, size_t filesize)
580{
581 size_t bufsize = talloc_get_size(ad->ad_data);
582 int adentries, i;
583 uint32_t eid, len, off;
584
585 /*
586 * The size of the buffer ad->ad_data is checked when read, so
587 * we wouldn't have to check our own offsets, a few extra
588 * checks won't hurt though. We have to check the offsets we
589 * read from the buffer anyway.
590 */
591
592 if (bufsize < (AD_HEADER_LEN + (AD_ENTRY_LEN * nentries))) {
593 DEBUG(1, ("bad size\n"));
594 return false;
595 }
596
597 ad->ad_magic = RIVAL(ad->ad_data, 0);
598 ad->ad_version = RIVAL(ad->ad_data, ADEDOFF_VERSION);
599 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION)) {
600 DEBUG(1, ("wrong magic or version\n"));
601 return false;
602 }
603
604 adentries = RSVAL(ad->ad_data, ADEDOFF_NENTRIES);
605 if (adentries != nentries) {
606 DEBUG(1, ("invalid number of entries: %d\n", adentries));
607 return false;
608 }
609
610 /* now, read in the entry bits */
611 for (i = 0; i < adentries; i++) {
612 eid = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN));
613 eid = get_eid(eid);
614 off = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 4);
615 len = RIVAL(ad->ad_data, AD_HEADER_LEN + (i * AD_ENTRY_LEN) + 8);
616
617 if (!eid || eid > ADEID_MAX) {
618 DEBUG(1, ("bogus eid %d\n", eid));
619 return false;
620 }
621
622 /*
623 * All entries other than the resource fork are
624 * expected to be read into the ad_data buffer, so
625 * ensure the specified offset is within that bound
626 */
627 if ((off > bufsize) && (eid != ADEID_RFORK)) {
628 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
629 eid, off, len));
630 return false;
631 }
632
633 /*
634 * All entries besides FinderInfo and resource fork
635 * must fit into the buffer. FinderInfo is special as
636 * it may be larger then the default 32 bytes (if it
637 * contains marshalled xattrs), but we will fixup that
638 * in ad_convert(). And the resource fork is never
639 * accessed directly by the ad_data buf (also see
640 * comment above) anyway.
641 */
642 if ((eid != ADEID_RFORK) &&
643 (eid != ADEID_FINDERI) &&
644 ((off + len) > bufsize)) {
645 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
646 eid, off, len));
647 return false;
648 }
649
650 /*
651 * That would be obviously broken
652 */
653 if (off > filesize) {
654 DEBUG(1, ("bogus eid %d: off: %" PRIu32 ", len: %" PRIu32 "\n",
655 eid, off, len));
656 return false;
657 }
658
659 /*
660 * Check for any entry that has its end beyond the
661 * filesize.
662 */
663 if (off + len < off) {
664 DEBUG(1, ("offset wrap in eid %d: off: %" PRIu32
665 ", len: %" PRIu32 "\n",
666 eid, off, len));
667 return false;
668
669 }
670 if (off + len > filesize) {
671 /*
672 * If this is the resource fork entry, we fix
673 * up the length, for any other entry we bail
674 * out.
675 */
676 if (eid != ADEID_RFORK) {
677 DEBUG(1, ("bogus eid %d: off: %" PRIu32
678 ", len: %" PRIu32 "\n",
679 eid, off, len));
680 return false;
681 }
682
683 /*
684 * Fixup the resource fork entry by limiting
685 * the size to entryoffset - filesize.
686 */
687 len = filesize - off;
688 DEBUG(1, ("Limiting ADEID_RFORK: off: %" PRIu32
689 ", len: %" PRIu32 "\n", off, len));
690 }
691
692 ad->ad_eid[eid].ade_off = off;
693 ad->ad_eid[eid].ade_len = len;
694 }
695
696 return true;
697}
698
699/**
700 * Convert from Apple's ._ file to Netatalk
701 *
702 * Apple's AppleDouble may contain a FinderInfo entry longer then 32
703 * bytes containing packed xattrs. Netatalk can't deal with that, so
704 * we simply discard the packed xattrs.
705 *
706 * @return -1 in case an error occured, 0 if no conversion was done, 1
707 * otherwise
708 **/
709static int ad_convert(struct adouble *ad, int fd)
710{
711 int rc = 0;
712 char *map = MAP_FAILED;
713 size_t origlen;
714
715 origlen = ad_getentryoff(ad, ADEID_RFORK) +
716 ad_getentrylen(ad, ADEID_RFORK);
717
718 /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
719 map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
720 if (map == MAP_FAILED) {
721 DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
722 rc = -1;
723 goto exit;
724 }
725
726 if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
727 memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
728 map + ad_getentryoff(ad, ADEID_RFORK),
729 ad_getentrylen(ad, ADEID_RFORK));
730 }
731
732 ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
733 ad_setentryoff(ad, ADEID_RFORK,
734 ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
735
736 /*
737 * FIXME: direct ftruncate(), but we don't have a fsp for the
738 * VFS call
739 */
740 rc = ftruncate(fd, ad_getentryoff(ad, ADEID_RFORK)
741 + ad_getentrylen(ad, ADEID_RFORK));
742
743exit:
744 if (map != MAP_FAILED) {
745 munmap(map, origlen);
746 }
747 return rc;
748}
749
750/**
751 * Read and parse Netatalk AppleDouble metadata xattr
752 **/
753static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
754{
755 int rc = 0;
756 ssize_t ealen;
757 bool ok;
758
759 DEBUG(10, ("reading meta xattr for %s\n", path));
760
761 ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
762 AFPINFO_EA_NETATALK, ad->ad_data,
763 AD_DATASZ_XATTR);
764 if (ealen == -1) {
765 switch (errno) {
766 case ENOATTR:
767 case ENOENT:
768 if (errno == ENOATTR) {
769 errno = ENOENT;
770 }
771 rc = -1;
772 goto exit;
773 default:
774 DEBUG(2, ("error reading meta xattr: %s\n",
775 strerror(errno)));
776 rc = -1;
777 goto exit;
778 }
779 }
780 if (ealen != AD_DATASZ_XATTR) {
781 DEBUG(2, ("bad size %zd\n", ealen));
782 errno = EINVAL;
783 rc = -1;
784 goto exit;
785 }
786
787 /* Now parse entries */
788 ok = ad_unpack(ad, ADEID_NUM_XATTR, AD_DATASZ_XATTR);
789 if (!ok) {
790 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
791 errno = EINVAL;
792 rc = -1;
793 goto exit;
794 }
795
796 if (!ad_getentryoff(ad, ADEID_FINDERI)
797 || !ad_getentryoff(ad, ADEID_COMMENT)
798 || !ad_getentryoff(ad, ADEID_FILEDATESI)
799 || !ad_getentryoff(ad, ADEID_AFPFILEI)
800 || !ad_getentryoff(ad, ADEID_PRIVDEV)
801 || !ad_getentryoff(ad, ADEID_PRIVINO)
802 || !ad_getentryoff(ad, ADEID_PRIVSYN)
803 || !ad_getentryoff(ad, ADEID_PRIVID)) {
804 DEBUG(2, ("invalid AppleDouble metadata xattr\n"));
805 errno = EINVAL;
806 rc = -1;
807 goto exit;
808 }
809
810exit:
811 DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
812
813 if (rc != 0) {
814 ealen = -1;
815 if (errno == EINVAL) {
816 become_root();
817 removexattr(path, AFPINFO_EA_NETATALK);
818 unbecome_root();
819 errno = ENOENT;
820 }
821 }
822 return ealen;
823}
824
825/**
826 * Read and parse resource fork, either ._ AppleDouble file or xattr
827 **/
828static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
829{
830 struct fruit_config_data *config = NULL;
831 int fd = -1;
832 int rc = 0;
833 ssize_t len;
834 char *adpath = NULL;
835 bool opened = false;
836 int mode;
837 struct adouble *meta_ad = NULL;
838 SMB_STRUCT_STAT sbuf;
839 bool ok;
840 int saved_errno = 0;
841
842 SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
843 struct fruit_config_data, return -1);
844
845 /* Try rw first so we can use the fd in ad_convert() */
846 mode = O_RDWR;
847
848 if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
849 fd = ad->ad_fsp->fh->fd;
850 } else {
851 if (config->rsrc == FRUIT_RSRC_XATTR) {
852 adpath = talloc_strdup(talloc_tos(), path);
853 } else {
854 rc = adouble_path(talloc_tos(), path, &adpath);
855 if (rc != 0) {
856 goto exit;
857 }
858 }
859
860 retry:
861 if (config->rsrc == FRUIT_RSRC_XATTR) {
862#ifndef HAVE_ATTROPEN
863 errno = ENOSYS;
864 rc = -1;
865 goto exit;
866#else
867 /* FIXME: direct Solaris xattr syscall */
868 fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
869 mode, 0);
870#endif
871 } else {
872 /* FIXME: direct open(), don't have an fsp */
873 fd = open(adpath, mode);
874 }
875
876 if (fd == -1) {
877 switch (errno) {
878 case EROFS:
879 case EACCES:
880 if (mode == O_RDWR) {
881 mode = O_RDONLY;
882 goto retry;
883 }
884 /* fall through ... */
885 default:
886 DEBUG(2, ("open AppleDouble: %s, %s\n",
887 adpath, strerror(errno)));
888 rc = -1;
889 goto exit;
890 }
891 }
892 opened = true;
893 }
894
895 if (config->rsrc == FRUIT_RSRC_XATTR) {
896 /* FIXME: direct sys_fstat(), don't have an fsp */
897 rc = sys_fstat(
898 fd, &sbuf,
899 lp_fake_directory_create_times(
900 SNUM(ad->ad_handle->conn)));
901 if (rc != 0) {
902 goto exit;
903 }
904 len = sbuf.st_ex_size;
905 ad_setentrylen(ad, ADEID_RFORK, len);
906 } else {
907 /* FIXME: direct sys_pread(), don't have an fsp */
908 len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
909 if (len != AD_DATASZ_DOT_UND) {
910 DEBUG(2, ("%s: bad size: %zd\n",
911 strerror(errno), len));
912 rc = -1;
913 goto exit;
914 }
915
916 /* FIXME: direct sys_fstat(), we don't have an fsp */
917 rc = sys_fstat(fd, &sbuf,
918 lp_fake_directory_create_times(
919 SNUM(ad->ad_handle->conn)));
920 if (rc != 0) {
921 goto exit;
922 }
923
924 /* Now parse entries */
925 ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
926 if (!ok) {
927 DEBUG(1, ("invalid AppleDouble ressource %s\n", path));
928 errno = EINVAL;
929 rc = -1;
930 goto exit;
931 }
932
933 if ((ad_getentryoff(ad, ADEID_FINDERI)
934 != ADEDOFF_FINDERI_DOT_UND)
935 || (ad_getentrylen(ad, ADEID_FINDERI)
936 < ADEDLEN_FINDERI)
937 || (ad_getentryoff(ad, ADEID_RFORK)
938 < ADEDOFF_RFORK_DOT_UND)) {
939 DEBUG(2, ("invalid AppleDouble ressource %s\n", path));
940 errno = EINVAL;
941 rc = -1;
942 goto exit;
943 }
944
945 if ((mode == O_RDWR)
946 && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
947 rc = ad_convert(ad, fd);
948 if (rc != 0) {
949 rc = -1;
950 goto exit;
951 }
952 /*
953 * Can't use ad_write() because we might not have a fsp
954 */
955 ok = ad_pack(ad);
956 if (!ok) {
957 rc = -1;
958 goto exit;
959 }
960 /* FIXME: direct sys_pwrite(), don't have an fsp */
961 len = sys_pwrite(fd, ad->ad_data,
962 AD_DATASZ_DOT_UND, 0);
963 if (len != AD_DATASZ_DOT_UND) {
964 DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
965 rc = -1;
966 goto exit;
967 }
968
969 meta_ad = ad_init(talloc_tos(), ad->ad_handle,
970 ADOUBLE_META, NULL);
971 if (meta_ad == NULL) {
972 rc = -1;
973 goto exit;
974 }
975
976 memcpy(ad_entry(meta_ad, ADEID_FINDERI),
977 ad_entry(ad, ADEID_FINDERI),
978 ADEDLEN_FINDERI);
979
980 rc = ad_write(meta_ad, path);
981 if (rc != 0) {
982 rc = -1;
983 goto exit;
984 }
985 }
986 }
987
988 DEBUG(10, ("opened AppleDouble: %s\n", path));
989
990exit:
991 if (rc != 0) {
992 saved_errno = errno;
993 len = -1;
994 }
995 if (opened && fd != -1) {
996 close(fd);
997 }
998 TALLOC_FREE(adpath);
999 TALLOC_FREE(meta_ad);
1000 if (rc != 0) {
1001 errno = saved_errno;
1002 }
1003 return len;
1004}
1005
1006/**
1007 * Read and unpack an AppleDouble metadata xattr or resource
1008 **/
1009static ssize_t ad_read(struct adouble *ad, const char *path)
1010{
1011 switch (ad->ad_type) {
1012 case ADOUBLE_META:
1013 return ad_header_read_meta(ad, path);
1014 case ADOUBLE_RSRC:
1015 return ad_header_read_rsrc(ad, path);
1016 default:
1017 return -1;
1018 }
1019}
1020
1021/**
1022 * Allocate a struct adouble without initialiing it
1023 *
1024 * The struct is either hang of the fsp extension context or if fsp is
1025 * NULL from ctx.
1026 *
1027 * @param[in] ctx talloc context
1028 * @param[in] handle vfs handle
1029 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1030
1031 * @param[in] fsp if not NULL (for stream IO), the adouble handle is
1032 * added as an fsp extension
1033 *
1034 * @return adouble handle
1035 **/
1036static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1037 adouble_type_t type, files_struct *fsp)
1038{
1039 int rc = 0;
1040 size_t adsize = 0;
1041 struct adouble *ad;
1042 struct fruit_config_data *config;
1043
1044 SMB_VFS_HANDLE_GET_DATA(handle, config,
1045 struct fruit_config_data, return NULL);
1046
1047 switch (type) {
1048 case ADOUBLE_META:
1049 adsize = AD_DATASZ_XATTR;
1050 break;
1051 case ADOUBLE_RSRC:
1052 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1053 adsize = AD_DATASZ_DOT_UND;
1054 }
1055 break;
1056 default:
1057 return NULL;
1058 }
1059
1060 if (!fsp) {
1061 ad = talloc_zero(ctx, struct adouble);
1062 if (ad == NULL) {
1063 rc = -1;
1064 goto exit;
1065 }
1066 if (adsize) {
1067 ad->ad_data = talloc_zero_array(ad, char, adsize);
1068 }
1069 } else {
1070 ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
1071 struct adouble,
1072 NULL);
1073 if (ad == NULL) {
1074 rc = -1;
1075 goto exit;
1076 }
1077 if (adsize) {
1078 ad->ad_data = talloc_zero_array(
1079 VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
1080 char, adsize);
1081 }
1082 ad->ad_fsp = fsp;
1083 }
1084
1085 if (adsize && ad->ad_data == NULL) {
1086 rc = -1;
1087 goto exit;
1088 }
1089 ad->ad_handle = handle;
1090 ad->ad_type = type;
1091 ad->ad_magic = AD_MAGIC;
1092 ad->ad_version = AD_VERSION;
1093
1094exit:
1095 if (rc != 0) {
1096 TALLOC_FREE(ad);
1097 }
1098 return ad;
1099}
1100
1101/**
1102 * Allocate and initialize a new struct adouble
1103 *
1104 * @param[in] ctx talloc context
1105 * @param[in] handle vfs handle
1106 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1107 * @param[in] fsp file handle, may be NULL for a type of e_ad_meta
1108 *
1109 * @return adouble handle, initialized
1110 **/
1111static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1112 adouble_type_t type, files_struct *fsp)
1113{
1114 int rc = 0;
1115 const struct ad_entry_order *eid;
1116 struct adouble *ad = NULL;
1117 struct fruit_config_data *config;
1118 time_t t = time(NULL);
1119
1120 SMB_VFS_HANDLE_GET_DATA(handle, config,
1121 struct fruit_config_data, return NULL);
1122
1123 switch (type) {
1124 case ADOUBLE_META:
1125 eid = entry_order_meta_xattr;
1126 break;
1127 case ADOUBLE_RSRC:
1128 if (config->rsrc == FRUIT_RSRC_ADFILE) {
1129 eid = entry_order_dot_und;
1130 } else {
1131 eid = entry_order_rsrc_xattr;
1132 }
1133 break;
1134 default:
1135 return NULL;
1136 }
1137
1138 ad = ad_alloc(ctx, handle, type, fsp);
1139 if (ad == NULL) {
1140 return NULL;
1141 }
1142
1143 while (eid->id) {
1144 ad->ad_eid[eid->id].ade_off = eid->offset;
1145 ad->ad_eid[eid->id].ade_len = eid->len;
1146 eid++;
1147 }
1148
1149 /* put something sane in the date fields */
1150 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, t);
1151 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, t);
1152 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, t);
1153 ad_setdate(ad, AD_DATE_BACKUP, htonl(AD_DATE_START));
1154
1155 if (rc != 0) {
1156 TALLOC_FREE(ad);
1157 }
1158 return ad;
1159}
1160
1161/**
1162 * Return AppleDouble data for a file
1163 *
1164 * @param[in] ctx talloc context
1165 * @param[in] handle vfs handle
1166 * @param[in] path pathname to file or directory
1167 * @param[in] type type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
1168 *
1169 * @return talloced struct adouble or NULL on error
1170 **/
1171static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
1172 const char *path, adouble_type_t type)
1173{
1174 int rc = 0;
1175 ssize_t len;
1176 struct adouble *ad = NULL;
1177
1178 DEBUG(10, ("ad_get(%s) called for %s\n",
1179 type == ADOUBLE_META ? "meta" : "rsrc", path));
1180
1181 ad = ad_alloc(ctx, handle, type, NULL);
1182 if (ad == NULL) {
1183 rc = -1;
1184 goto exit;
1185 }
1186
1187 len = ad_read(ad, path);
1188 if (len == -1) {
1189 DEBUG(10, ("error reading AppleDouble for %s\n", path));
1190 rc = -1;
1191 goto exit;
1192 }
1193
1194exit:
1195 DEBUG(10, ("ad_get(%s) for %s returning %d\n",
1196 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
1197
1198 if (rc != 0) {
1199 TALLOC_FREE(ad);
1200 }
1201 return ad;
1202}
1203
1204/**
1205 * Set AppleDouble metadata on a file or directory
1206 *
1207 * @param[in] ad adouble handle
1208
1209 * @param[in] path pathname to file or directory, may be NULL for a
1210 * resource fork
1211 *
1212 * @return status code, 0 means success
1213 **/
1214static int ad_write(struct adouble *ad, const char *path)
1215{
1216 int rc = 0;
1217 ssize_t len;
1218 bool ok;
1219
1220 ok = ad_pack(ad);
1221 if (!ok) {
1222 return -1;
1223 }
1224
1225 switch (ad->ad_type) {
1226 case ADOUBLE_META:
1227 rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
1228 AFPINFO_EA_NETATALK, ad->ad_data,
1229 AD_DATASZ_XATTR, 0);
1230 break;
1231 case ADOUBLE_RSRC:
1232 if ((ad->ad_fsp == NULL)
1233 || (ad->ad_fsp->fh == NULL)
1234 || (ad->ad_fsp->fh->fd == -1)) {
1235 rc = -1;
1236 goto exit;
1237 }
1238 /* FIXME: direct sys_pwrite(), don't have an fsp */
1239 len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
1240 talloc_get_size(ad->ad_data), 0);
1241 if (len != talloc_get_size(ad->ad_data)) {
1242 DEBUG(1, ("short write on %s: %zd",
1243 fsp_str_dbg(ad->ad_fsp), len));
1244 rc = -1;
1245 goto exit;
1246 }
1247 break;
1248 default:
1249 return -1;
1250 }
1251exit:
1252 return rc;
1253}
1254
1255/*****************************************************************************
1256 * Helper functions
1257 *****************************************************************************/
1258
1259static bool is_afpinfo_stream(const struct smb_filename *smb_fname)
1260{
1261 if (strncasecmp_m(smb_fname->stream_name,
1262 AFPINFO_STREAM_NAME,
1263 strlen(AFPINFO_STREAM_NAME)) == 0) {
1264 return true;
1265 }
1266 return false;
1267}
1268
1269static bool is_afpresource_stream(const struct smb_filename *smb_fname)
1270{
1271 if (strncasecmp_m(smb_fname->stream_name,
1272 AFPRESOURCE_STREAM_NAME,
1273 strlen(AFPRESOURCE_STREAM_NAME)) == 0) {
1274 return true;
1275 }
1276 return false;
1277}
1278
1279/**
1280 * Test whether stream is an Apple stream, not used atm
1281 **/
1282#if 0
1283static bool is_apple_stream(const struct smb_filename *smb_fname)
1284{
1285 if (is_afpinfo_stream(smb_fname)) {
1286 return true;
1287 }
1288 if (is_afpresource_stream(smb_fname)) {
1289 return true;
1290 }
1291 return false;
1292}
1293#endif
1294
1295/**
1296 * Initialize config struct from our smb.conf config parameters
1297 **/
1298static int init_fruit_config(vfs_handle_struct *handle)
1299{
1300 struct fruit_config_data *config;
1301 int enumval;
1302
1303 config = talloc_zero(handle->conn, struct fruit_config_data);
1304 if (!config) {
1305 DEBUG(1, ("talloc_zero() failed\n"));
1306 errno = ENOMEM;
1307 return -1;
1308 }
1309
1310 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1311 "ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
1312 if (enumval == -1) {
1313 DEBUG(1, ("value for %s: ressource type unknown\n",
1314 FRUIT_PARAM_TYPE_NAME));
1315 return -1;
1316 }
1317 config->rsrc = (enum fruit_rsrc)enumval;
1318
1319 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1320 "metadata", fruit_meta, FRUIT_META_NETATALK);
1321 if (enumval == -1) {
1322 DEBUG(1, ("value for %s: metadata type unknown\n",
1323 FRUIT_PARAM_TYPE_NAME));
1324 return -1;
1325 }
1326 config->meta = (enum fruit_meta)enumval;
1327
1328 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1329 "locking", fruit_locking, FRUIT_LOCKING_NONE);
1330 if (enumval == -1) {
1331 DEBUG(1, ("value for %s: locking type unknown\n",
1332 FRUIT_PARAM_TYPE_NAME));
1333 return -1;
1334 }
1335 config->locking = (enum fruit_locking)enumval;
1336
1337 enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1338 "encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
1339 if (enumval == -1) {
1340 DEBUG(1, ("value for %s: encoding type unknown\n",
1341 FRUIT_PARAM_TYPE_NAME));
1342 return -1;
1343 }
1344 config->encoding = (enum fruit_encoding)enumval;
1345
1346 config->veto_appledouble = lp_parm_bool(
1347 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
1348 "veto_appledouble", true);
1349
1350 config->use_aapl = lp_parm_bool(
1351 -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
1352
1353 config->unix_info_enabled = lp_parm_bool(
1354 -1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
1355
1356 config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
1357 "copyfile", false);
1358
1359 config->posix_rename = lp_parm_bool(
1360 SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
1361
1362 config->readdir_attr_rsize = lp_parm_bool(
1363 SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
1364
1365 config->readdir_attr_finder_info = lp_parm_bool(
1366 SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
1367
1368 config->readdir_attr_max_access = lp_parm_bool(
1369 SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
1370
1371 SMB_VFS_HANDLE_SET_DATA(handle, config,
1372 NULL, struct fruit_config_data,
1373 return -1);
1374
1375 return 0;
1376}
1377
1378/**
1379 * Prepend "._" to a basename
1380 **/
1381static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
1382{
1383 char *parent;
1384 const char *base;
1385
1386 if (!parent_dirname(ctx, path_in, &parent, &base)) {
1387 return -1;
1388 }
1389
1390 *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
1391 if (*path_out == NULL) {
1392 return -1;
1393 }
1394
1395 return 0;
1396}
1397
1398/**
1399 * Allocate and initialize an AfpInfo struct
1400 **/
1401static AfpInfo *afpinfo_new(TALLOC_CTX *ctx)
1402{
1403 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1404 if (ai == NULL) {
1405 return NULL;
1406 }
1407 ai->afpi_Signature = AFP_Signature;
1408 ai->afpi_Version = AFP_Version;
1409 ai->afpi_BackupTime = AD_DATE_START;
1410 return ai;
1411}
1412
1413/**
1414 * Pack an AfpInfo struct into a buffer
1415 *
1416 * Buffer size must be at least AFP_INFO_SIZE
1417 * Returns size of packed buffer
1418 **/
1419static ssize_t afpinfo_pack(const AfpInfo *ai, char *buf)
1420{
1421 memset(buf, 0, AFP_INFO_SIZE);
1422
1423 RSIVAL(buf, 0, ai->afpi_Signature);
1424 RSIVAL(buf, 4, ai->afpi_Version);
1425 RSIVAL(buf, 12, ai->afpi_BackupTime);
1426 memcpy(buf + 16, ai->afpi_FinderInfo, sizeof(ai->afpi_FinderInfo));
1427
1428 return AFP_INFO_SIZE;
1429}
1430
1431/**
1432 * Unpack a buffer into a AfpInfo structure
1433 *
1434 * Buffer size must be at least AFP_INFO_SIZE
1435 * Returns allocated AfpInfo struct
1436 **/
1437static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
1438{
1439 AfpInfo *ai = talloc_zero(ctx, AfpInfo);
1440 if (ai == NULL) {
1441 return NULL;
1442 }
1443
1444 ai->afpi_Signature = RIVAL(data, 0);
1445 ai->afpi_Version = RIVAL(data, 4);
1446 ai->afpi_BackupTime = RIVAL(data, 12);
1447 memcpy(ai->afpi_FinderInfo, (const char *)data + 16,
1448 sizeof(ai->afpi_FinderInfo));
1449
1450 if (ai->afpi_Signature != AFP_Signature
1451 || ai->afpi_Version != AFP_Version) {
1452 DEBUG(1, ("Bad AfpInfo signature or version\n"));
1453 TALLOC_FREE(ai);
1454 }
1455
1456 return ai;
1457}
1458
1459/**
1460 * Fake an inode number from the md5 hash of the (xattr) name
1461 **/
1462static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
1463{
1464 MD5_CTX ctx;
1465 unsigned char hash[16];
1466 SMB_INO_T result;
1467 char *upper_sname;
1468
1469 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
1470 SMB_ASSERT(upper_sname != NULL);
1471
1472 MD5Init(&ctx);
1473 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
1474 sizeof(sbuf->st_ex_dev));
1475 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
1476 sizeof(sbuf->st_ex_ino));
1477 MD5Update(&ctx, (unsigned char *)upper_sname,
1478 talloc_get_size(upper_sname)-1);
1479 MD5Final(hash, &ctx);
1480
1481 TALLOC_FREE(upper_sname);
1482
1483 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
1484 memcpy(&result, hash, sizeof(result));
1485
1486 DEBUG(10, ("fruit_inode \"%s\": ino=0x%llu\n",
1487 sname, (unsigned long long)result));
1488
1489 return result;
1490}
1491
1492/**
1493 * Ensure ad_fsp is still valid
1494 **/
1495static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
1496{
1497 if (ad->ad_fsp == fsp) {
1498 return true;
1499 }
1500 ad->ad_fsp = fsp;
1501
1502 return true;
1503}
1504
1505static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1506 struct stream_struct **streams,
1507 const char *name, off_t size,
1508 off_t alloc_size)
1509{
1510 struct stream_struct *tmp;
1511
1512 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1513 (*num_streams)+1);
1514 if (tmp == NULL) {
1515 return false;
1516 }
1517
1518 tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
1519 if (tmp[*num_streams].name == NULL) {
1520 return false;
1521 }
1522
1523 tmp[*num_streams].size = size;
1524 tmp[*num_streams].alloc_size = alloc_size;
1525
1526 *streams = tmp;
1527 *num_streams += 1;
1528 return true;
1529}
1530
1531static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1532 struct stream_struct **streams,
1533 const char *name)
1534{
1535 struct stream_struct *tmp = *streams;
1536 int i;
1537
1538 if (*num_streams == 0) {
1539 return true;
1540 }
1541
1542 for (i = 0; i < *num_streams; i++) {
1543 if (strequal_m(tmp[i].name, name)) {
1544 break;
1545 }
1546 }
1547
1548 if (i == *num_streams) {
1549 return true;
1550 }
1551
1552 TALLOC_FREE(tmp[i].name);
1553 if (*num_streams - 1 > i) {
1554 memmove(&tmp[i], &tmp[i+1],
1555 (*num_streams - i - 1) * sizeof(struct stream_struct));
1556 }
1557
1558 *num_streams -= 1;
1559 return true;
1560}
1561
1562static bool empty_finderinfo(const struct adouble *ad)
1563{
1564
1565 char emptybuf[ADEDLEN_FINDERI] = {0};
1566 if (memcmp(emptybuf,
1567 ad_entry(ad, ADEID_FINDERI),
1568 ADEDLEN_FINDERI) == 0) {
1569 return true;
1570 }
1571 return false;
1572}
1573
1574/**
1575 * Update btime with btime from Netatalk
1576 **/
1577static void update_btime(vfs_handle_struct *handle,
1578 struct smb_filename *smb_fname)
1579{
1580 uint32_t t;
1581 struct timespec creation_time = {0};
1582 struct adouble *ad;
1583
1584 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
1585 if (ad == NULL) {
1586 return;
1587 }
1588 if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
1589 TALLOC_FREE(ad);
1590 return;
1591 }
1592 TALLOC_FREE(ad);
1593
1594 creation_time.tv_sec = convert_uint32_t_to_time_t(t);
1595 update_stat_ex_create_time(&smb_fname->st, creation_time);
1596
1597 return;
1598}
1599
1600/**
1601 * Map an access mask to a Netatalk single byte byte range lock
1602 **/
1603static off_t access_to_netatalk_brl(enum apple_fork fork_type,
1604 uint32_t access_mask)
1605{
1606 off_t offset;
1607
1608 switch (access_mask) {
1609 case FILE_READ_DATA:
1610 offset = AD_FILELOCK_OPEN_RD;
1611 break;
1612
1613 case FILE_WRITE_DATA:
1614 case FILE_APPEND_DATA:
1615 offset = AD_FILELOCK_OPEN_WR;
1616 break;
1617
1618 default:
1619 offset = AD_FILELOCK_OPEN_NONE;
1620 break;
1621 }
1622
1623 if (fork_type == APPLE_FORK_RSRC) {
1624 if (offset == AD_FILELOCK_OPEN_NONE) {
1625 offset = AD_FILELOCK_RSRC_OPEN_NONE;
1626 } else {
1627 offset += 2;
1628 }
1629 }
1630
1631 return offset;
1632}
1633
1634/**
1635 * Map a deny mode to a Netatalk brl
1636 **/
1637static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
1638 uint32_t deny_mode)
1639{
1640 off_t offset;
1641
1642 switch (deny_mode) {
1643 case DENY_READ:
1644 offset = AD_FILELOCK_DENY_RD;
1645 break;
1646
1647 case DENY_WRITE:
1648 offset = AD_FILELOCK_DENY_WR;
1649 break;
1650
1651 default:
1652 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
1653 }
1654
1655 if (fork_type == APPLE_FORK_RSRC) {
1656 offset += 2;
1657 }
1658
1659 return offset;
1660}
1661
1662/**
1663 * Call fcntl() with an exclusive F_GETLK request in order to
1664 * determine if there's an exisiting shared lock
1665 *
1666 * @return true if the requested lock was found or any error occured
1667 * false if the lock was not found
1668 **/
1669static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
1670{
1671 bool result;
1672 off_t offset = in_offset;
1673 off_t len = 1;
1674 int type = F_WRLCK;
1675 pid_t pid;
1676
1677 result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
1678 if (result == false) {
1679 return true;
1680 }
1681
1682 if (type != F_UNLCK) {
1683 return true;
1684 }
1685
1686 return false;
1687}
1688
1689static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
1690 files_struct *fsp,
1691 uint32_t access_mask,
1692 uint32_t deny_mode)
1693{
1694 NTSTATUS status = NT_STATUS_OK;
1695 struct byte_range_lock *br_lck = NULL;
1696 bool open_for_reading, open_for_writing, deny_read, deny_write;
1697 off_t off;
1698
1699 /* FIXME: hardcoded data fork, add resource fork */
1700 enum apple_fork fork_type = APPLE_FORK_DATA;
1701
1702 DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
1703 fsp_str_dbg(fsp),
1704 access_mask & FILE_READ_DATA ? "READ" :"-",
1705 access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
1706 deny_mode & DENY_READ ? "DENY_READ" : "-",
1707 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
1708
1709 /*
1710 * Check read access and deny read mode
1711 */
1712 if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
1713 /* Check access */
1714 open_for_reading = test_netatalk_lock(
1715 fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
1716
1717 deny_read = test_netatalk_lock(
1718 fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
1719
1720 DEBUG(10, ("read: %s, deny_write: %s\n",
1721 open_for_reading == true ? "yes" : "no",
1722 deny_read == true ? "yes" : "no"));
1723
1724 if (((access_mask & FILE_READ_DATA) && deny_read)
1725 || ((deny_mode & DENY_READ) && open_for_reading)) {
1726 return NT_STATUS_SHARING_VIOLATION;
1727 }
1728
1729 /* Set locks */
1730 if (access_mask & FILE_READ_DATA) {
1731 off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
1732 br_lck = do_lock(
1733 handle->conn->sconn->msg_ctx, fsp,
1734 fsp->op->global->open_persistent_id, 1, off,
1735 READ_LOCK, POSIX_LOCK, false,
1736 &status, NULL);
1737
1738 if (!NT_STATUS_IS_OK(status)) {
1739 return status;
1740 }
1741 TALLOC_FREE(br_lck);
1742 }
1743
1744 if (deny_mode & DENY_READ) {
1745 off = denymode_to_netatalk_brl(fork_type, DENY_READ);
1746 br_lck = do_lock(
1747 handle->conn->sconn->msg_ctx, fsp,
1748 fsp->op->global->open_persistent_id, 1, off,
1749 READ_LOCK, POSIX_LOCK, false,
1750 &status, NULL);
1751
1752 if (!NT_STATUS_IS_OK(status)) {
1753 return status;
1754 }
1755 TALLOC_FREE(br_lck);
1756 }
1757 }
1758
1759 /*
1760 * Check write access and deny write mode
1761 */
1762 if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
1763 /* Check access */
1764 open_for_writing = test_netatalk_lock(
1765 fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
1766
1767 deny_write = test_netatalk_lock(
1768 fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
1769
1770 DEBUG(10, ("write: %s, deny_write: %s\n",
1771 open_for_writing == true ? "yes" : "no",
1772 deny_write == true ? "yes" : "no"));
1773
1774 if (((access_mask & FILE_WRITE_DATA) && deny_write)
1775 || ((deny_mode & DENY_WRITE) && open_for_writing)) {
1776 return NT_STATUS_SHARING_VIOLATION;
1777 }
1778
1779 /* Set locks */
1780 if (access_mask & FILE_WRITE_DATA) {
1781 off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
1782 br_lck = do_lock(
1783 handle->conn->sconn->msg_ctx, fsp,
1784 fsp->op->global->open_persistent_id, 1, off,
1785 READ_LOCK, POSIX_LOCK, false,
1786 &status, NULL);
1787
1788 if (!NT_STATUS_IS_OK(status)) {
1789 return status;
1790 }
1791 TALLOC_FREE(br_lck);
1792
1793 }
1794 if (deny_mode & DENY_WRITE) {
1795 off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
1796 br_lck = do_lock(
1797 handle->conn->sconn->msg_ctx, fsp,
1798 fsp->op->global->open_persistent_id, 1, off,
1799 READ_LOCK, POSIX_LOCK, false,
1800 &status, NULL);
1801
1802 if (!NT_STATUS_IS_OK(status)) {
1803 return status;
1804 }
1805 TALLOC_FREE(br_lck);
1806 }
1807 }
1808
1809 TALLOC_FREE(br_lck);
1810
1811 return status;
1812}
1813
1814static NTSTATUS check_aapl(vfs_handle_struct *handle,
1815 struct smb_request *req,
1816 const struct smb2_create_blobs *in_context_blobs,
1817 struct smb2_create_blobs *out_context_blobs)
1818{
1819 struct fruit_config_data *config;
1820 NTSTATUS status;
1821 struct smb2_create_blob *aapl = NULL;
1822 uint32_t cmd;
1823 bool ok;
1824 uint8_t p[16];
1825 DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
1826 uint64_t req_bitmap, client_caps;
1827 uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
1828 smb_ucs2_t *model;
1829 size_t modellen;
1830
1831 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
1832 return NT_STATUS_UNSUCCESSFUL);
1833
1834 if (!config->use_aapl
1835 || in_context_blobs == NULL
1836 || out_context_blobs == NULL) {
1837 return NT_STATUS_OK;
1838 }
1839
1840 aapl = smb2_create_blob_find(in_context_blobs,
1841 SMB2_CREATE_TAG_AAPL);
1842 if (aapl == NULL) {
1843 return NT_STATUS_OK;
1844 }
1845
1846 if (aapl->data.length != 24) {
1847 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
1848 (uintmax_t)aapl->data.length));
1849 return NT_STATUS_INVALID_PARAMETER;
1850 }
1851
1852 cmd = IVAL(aapl->data.data, 0);
1853 if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
1854 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
1855 return NT_STATUS_INVALID_PARAMETER;
1856 }
1857
1858 req_bitmap = BVAL(aapl->data.data, 8);
1859 client_caps = BVAL(aapl->data.data, 16);
1860
1861 SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
1862 SIVAL(p, 4, 0);
1863 SBVAL(p, 8, req_bitmap);
1864 ok = data_blob_append(req, &blob, p, 16);
1865 if (!ok) {
1866 return NT_STATUS_UNSUCCESSFUL;
1867 }
1868
1869 if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
1870 if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
1871 (handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
1872 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
1873 config->readdir_attr_enabled = true;
1874 }
1875
1876 if (config->use_copyfile) {
1877 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
1878 config->copyfile_enabled = true;
1879 }
1880
1881 /*
1882 * The client doesn't set the flag, so we can't check
1883 * for it and just set it unconditionally
1884 */
1885 if (config->unix_info_enabled) {
1886 server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
1887 }
1888
1889 SBVAL(p, 0, server_caps);
1890 ok = data_blob_append(req, &blob, p, 8);
1891 if (!ok) {
1892 return NT_STATUS_UNSUCCESSFUL;
1893 }
1894 }
1895
1896 if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
1897 SBVAL(p, 0,
1898 lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
1899 SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
1900 ok = data_blob_append(req, &blob, p, 8);
1901 if (!ok) {
1902 return NT_STATUS_UNSUCCESSFUL;
1903 }
1904 }
1905
1906 if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
1907 ok = convert_string_talloc(req,
1908 CH_UNIX, CH_UTF16LE,
1909 "Samba", strlen("Samba"),
1910 &model, &modellen);
1911 if (!ok) {
1912 return NT_STATUS_UNSUCCESSFUL;
1913 }
1914
1915 SIVAL(p, 0, 0);
1916 SIVAL(p + 4, 0, modellen);
1917 ok = data_blob_append(req, &blob, p, 8);
1918 if (!ok) {
1919 talloc_free(model);
1920 return NT_STATUS_UNSUCCESSFUL;
1921 }
1922
1923 ok = data_blob_append(req, &blob, model, modellen);
1924 talloc_free(model);
1925 if (!ok) {
1926 return NT_STATUS_UNSUCCESSFUL;
1927 }
1928 }
1929
1930 status = smb2_create_blob_add(out_context_blobs,
1931 out_context_blobs,
1932 SMB2_CREATE_TAG_AAPL,
1933 blob);
1934 if (NT_STATUS_IS_OK(status)) {
1935 config->nego_aapl = true;
1936 }
1937
1938 return status;
1939}
1940
1941static NTSTATUS readdir_attr_macmeta(struct vfs_handle_struct *handle,
1942 const struct smb_filename *smb_fname,
1943 struct readdir_attr_data *attr_data)
1944{
1945 NTSTATUS status = NT_STATUS_OK;
1946 uint32_t date_added;
1947 struct adouble *ad = NULL;
1948 struct fruit_config_data *config = NULL;
1949
1950 SMB_VFS_HANDLE_GET_DATA(handle, config,
1951 struct fruit_config_data,
1952 return NT_STATUS_UNSUCCESSFUL);
1953
1954
1955 /* Ensure we return a default value in the creation_date field */
1956 RSIVAL(&attr_data->attr_data.aapl.finder_info, 12, AD_DATE_START);
1957
1958 /*
1959 * Resource fork length
1960 */
1961
1962 if (config->readdir_attr_rsize) {
1963 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1964 ADOUBLE_RSRC);
1965 if (ad) {
1966 attr_data->attr_data.aapl.rfork_size = ad_getentrylen(
1967 ad, ADEID_RFORK);
1968 TALLOC_FREE(ad);
1969 }
1970 }
1971
1972 /*
1973 * FinderInfo
1974 */
1975
1976 if (config->readdir_attr_finder_info) {
1977 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
1978 ADOUBLE_META);
1979 if (ad) {
1980 if (S_ISREG(smb_fname->st.st_ex_mode)) {
1981 /* finder_type */
1982 memcpy(&attr_data->attr_data.aapl.finder_info[0],
1983 ad_entry(ad, ADEID_FINDERI), 4);
1984
1985 /* finder_creator */
1986 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 4,
1987 ad_entry(ad, ADEID_FINDERI) + 4, 4);
1988 }
1989
1990 /* finder_flags */
1991 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 8,
1992 ad_entry(ad, ADEID_FINDERI) + 8, 2);
1993
1994 /* finder_ext_flags */
1995 memcpy(&attr_data->attr_data.aapl.finder_info[0] + 10,
1996 ad_entry(ad, ADEID_FINDERI) + 24, 2);
1997
1998 /* creation date */
1999 date_added = convert_time_t_to_uint32_t(
2000 smb_fname->st.st_ex_btime.tv_sec - AD_DATE_DELTA);
2001 RSIVAL(&attr_data->attr_data.aapl.finder_info[0], 12, date_added);
2002
2003 TALLOC_FREE(ad);
2004 }
2005 }
2006
2007 TALLOC_FREE(ad);
2008 return status;
2009}
2010
2011/* Search MS NFS style ACE with UNIX mode */
2012static NTSTATUS check_ms_nfs(vfs_handle_struct *handle,
2013 files_struct *fsp,
2014 const struct security_descriptor *psd,
2015 mode_t *pmode,
2016 bool *pdo_chmod)
2017{
2018 int i;
2019 struct fruit_config_data *config = NULL;
2020
2021 *pdo_chmod = false;
2022
2023 SMB_VFS_HANDLE_GET_DATA(handle, config,
2024 struct fruit_config_data,
2025 return NT_STATUS_UNSUCCESSFUL);
2026
2027 if (psd->dacl == NULL || !config->unix_info_enabled) {
2028 return NT_STATUS_OK;
2029 }
2030
2031 for (i = 0; i < psd->dacl->num_aces; i++) {
2032 if (dom_sid_compare_domain(
2033 &global_sid_Unix_NFS_Mode,
2034 &psd->dacl->aces[i].trustee) == 0) {
2035 *pmode = (mode_t)psd->dacl->aces[i].trustee.sub_auths[2];
2036 *pmode &= (S_IRWXU | S_IRWXG | S_IRWXO);
2037 *pdo_chmod = true;
2038
2039 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
2040 fsp_str_dbg(fsp), (unsigned)(*pmode)));
2041 break;
2042 }
2043 }
2044
2045 return NT_STATUS_OK;
2046}
2047
2048/****************************************************************************
2049 * VFS ops
2050 ****************************************************************************/
2051
2052static int fruit_connect(vfs_handle_struct *handle,
2053 const char *service,
2054 const char *user)
2055{
2056 int rc;
2057 char *list = NULL, *newlist = NULL;
2058 struct fruit_config_data *config;
2059
2060 DEBUG(10, ("fruit_connect\n"));
2061
2062 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
2063 if (rc < 0) {
2064 return rc;
2065 }
2066
2067 rc = init_fruit_config(handle);
2068 if (rc != 0) {
2069 return rc;
2070 }
2071
2072 SMB_VFS_HANDLE_GET_DATA(handle, config,
2073 struct fruit_config_data, return -1);
2074
2075 if (config->veto_appledouble) {
2076 list = lp_veto_files(talloc_tos(), SNUM(handle->conn));
2077
2078 if (list) {
2079 if (strstr(list, "/" ADOUBLE_NAME_PREFIX "*/") == NULL) {
2080 newlist = talloc_asprintf(
2081 list,
2082 "%s/" ADOUBLE_NAME_PREFIX "*/",
2083 list);
2084 lp_do_parameter(SNUM(handle->conn),
2085 "veto files",
2086 newlist);
2087 }
2088 } else {
2089 lp_do_parameter(SNUM(handle->conn),
2090 "veto files",
2091 "/" ADOUBLE_NAME_PREFIX "*/");
2092 }
2093
2094 TALLOC_FREE(list);
2095 }
2096
2097 if (config->encoding == FRUIT_ENC_NATIVE) {
2098 lp_do_parameter(
2099 SNUM(handle->conn),
2100 "catia:mappings",
2101 "0x01:0xf001,0x02:0xf002,0x03:0xf003,0x04:0xf004,"
2102 "0x05:0xf005,0x06:0xf006,0x07:0xf007,0x08:0xf008,"
2103 "0x09:0xf009,0x0a:0xf00a,0x0b:0xf00b,0x0c:0xf00c,"
2104 "0x0d:0xf00d,0x0e:0xf00e,0x0f:0xf00f,0x10:0xf010,"
2105 "0x11:0xf011,0x12:0xf012,0x13:0xf013,0x14:0xf014,"
2106 "0x15:0xf015,0x16:0xf016,0x17:0xf017,0x18:0xf018,"
2107 "0x19:0xf019,0x1a:0xf01a,0x1b:0xf01b,0x1c:0xf01c,"
2108 "0x1d:0xf01d,0x1e:0xf01e,0x1f:0xf01f,"
2109 "0x22:0xf020,0x2a:0xf021,0x3a:0xf022,0x3c:0xf023,"
2110 "0x3e:0xf024,0x3f:0xf025,0x5c:0xf026,0x7c:0xf027,"
2111 "0x0d:0xf00d");
2112 }
2113
2114 return rc;
2115}
2116
2117static int fruit_open_meta(vfs_handle_struct *handle,
2118 struct smb_filename *smb_fname,
2119 files_struct *fsp, int flags, mode_t mode)
2120{
2121 int rc = 0;
2122 struct fruit_config_data *config = NULL;
2123 struct smb_filename *smb_fname_base = NULL;
2124 int baseflags;
2125 int hostfd = -1;
2126 struct adouble *ad = NULL;
2127
2128 DEBUG(10, ("fruit_open_meta for %s\n", smb_fname_str_dbg(smb_fname)));
2129
2130 SMB_VFS_HANDLE_GET_DATA(handle, config,
2131 struct fruit_config_data, return -1);
2132
2133 if (config->meta == FRUIT_META_STREAM) {
2134 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2135 }
2136
2137 /* Create an smb_filename with stream_name == NULL. */
2138 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2139 smb_fname->base_name, NULL, NULL);
2140
2141 if (smb_fname_base == NULL) {
2142 errno = ENOMEM;
2143 rc = -1;
2144 goto exit;
2145 }
2146
2147 /*
2148 * We use baseflags to turn off nasty side-effects when opening the
2149 * underlying file.
2150 */
2151 baseflags = flags;
2152 baseflags &= ~O_TRUNC;
2153 baseflags &= ~O_EXCL;
2154 baseflags &= ~O_CREAT;
2155
2156 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2157 baseflags, mode);
2158
2159 /*
2160 * It is legit to open a stream on a directory, but the base
2161 * fd has to be read-only.
2162 */
2163 if ((hostfd == -1) && (errno == EISDIR)) {
2164 baseflags &= ~O_ACCMODE;
2165 baseflags |= O_RDONLY;
2166 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2167 baseflags, mode);
2168 }
2169
2170 TALLOC_FREE(smb_fname_base);
2171
2172 if (hostfd == -1) {
2173 rc = -1;
2174 goto exit;
2175 }
2176
2177 if (flags & (O_CREAT | O_TRUNC)) {
2178 /*
2179 * The attribute does not exist or needs to be truncated,
2180 * create an AppleDouble EA
2181 */
2182 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2183 handle, ADOUBLE_META, fsp);
2184 if (ad == NULL) {
2185 rc = -1;
2186 goto exit;
2187 }
2188
2189 rc = ad_write(ad, smb_fname->base_name);
2190 if (rc != 0) {
2191 rc = -1;
2192 goto exit;
2193 }
2194 } else {
2195 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2196 handle, ADOUBLE_META, fsp);
2197 if (ad == NULL) {
2198 rc = -1;
2199 goto exit;
2200 }
2201 if (ad_read(ad, smb_fname->base_name) == -1) {
2202 rc = -1;
2203 goto exit;
2204 }
2205 }
2206
2207exit:
2208 DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, hostfd));
2209 if (rc != 0) {
2210 int saved_errno = errno;
2211 if (hostfd >= 0) {
2212 /*
2213 * BUGBUGBUG -- we would need to call
2214 * fd_close_posix here, but we don't have a
2215 * full fsp yet
2216 */
2217 fsp->fh->fd = hostfd;
2218 SMB_VFS_CLOSE(fsp);
2219 }
2220 hostfd = -1;
2221 errno = saved_errno;
2222 }
2223 return hostfd;
2224}
2225
2226static int fruit_open_rsrc(vfs_handle_struct *handle,
2227 struct smb_filename *smb_fname,
2228 files_struct *fsp, int flags, mode_t mode)
2229{
2230 int rc = 0;
2231 struct fruit_config_data *config = NULL;
2232 struct adouble *ad = NULL;
2233 struct smb_filename *smb_fname_base = NULL;
2234 char *adpath = NULL;
2235 int hostfd = -1;
2236
2237 DEBUG(10, ("fruit_open_rsrc for %s\n", smb_fname_str_dbg(smb_fname)));
2238
2239 SMB_VFS_HANDLE_GET_DATA(handle, config,
2240 struct fruit_config_data, return -1);
2241
2242 switch (config->rsrc) {
2243 case FRUIT_RSRC_STREAM:
2244 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2245 case FRUIT_RSRC_XATTR:
2246#ifdef HAVE_ATTROPEN
2247 hostfd = attropen(smb_fname->base_name,
2248 AFPRESOURCE_EA_NETATALK, flags, mode);
2249 if (hostfd == -1) {
2250 return -1;
2251 }
2252 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2253 handle, ADOUBLE_RSRC, fsp);
2254 if (ad == NULL) {
2255 rc = -1;
2256 goto exit;
2257 }
2258 goto exit;
2259#else
2260 errno = ENOTSUP;
2261 return -1;
2262#endif
2263 default:
2264 break;
2265 }
2266
2267 if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
2268 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2269 if (rc != 0) {
2270 rc = -1;
2271 goto exit;
2272 }
2273 }
2274
2275 if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
2276 /* sorry, but directories don't habe a resource fork */
2277 rc = -1;
2278 goto exit;
2279 }
2280
2281 rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
2282 if (rc != 0) {
2283 goto exit;
2284 }
2285
2286 /* Create an smb_filename with stream_name == NULL. */
2287 smb_fname_base = synthetic_smb_fname(talloc_tos(),
2288 adpath, NULL, NULL);
2289 if (smb_fname_base == NULL) {
2290 errno = ENOMEM;
2291 rc = -1;
2292 goto exit;
2293 }
2294
2295 /* Sanitize flags */
2296 if (flags & O_WRONLY) {
2297 /* We always need read access for the metadata header too */
2298 flags &= ~O_WRONLY;
2299 flags |= O_RDWR;
2300 }
2301
2302 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
2303 flags, mode);
2304 if (hostfd == -1) {
2305 rc = -1;
2306 goto exit;
2307 }
2308
2309 /* REVIEW: we need this in ad_write() */
2310 fsp->fh->fd = hostfd;
2311
2312 if (flags & (O_CREAT | O_TRUNC)) {
2313 ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2314 handle, ADOUBLE_RSRC, fsp);
2315 if (ad == NULL) {
2316 rc = -1;
2317 goto exit;
2318 }
2319 rc = ad_write(ad, smb_fname->base_name);
2320 if (rc != 0) {
2321 rc = -1;
2322 goto exit;
2323 }
2324 } else {
2325 ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
2326 handle, ADOUBLE_RSRC, fsp);
2327 if (ad == NULL) {
2328 rc = -1;
2329 goto exit;
2330 }
2331 if (ad_read(ad, smb_fname->base_name) == -1) {
2332 rc = -1;
2333 goto exit;
2334 }
2335 }
2336
2337exit:
2338
2339 TALLOC_FREE(adpath);
2340 TALLOC_FREE(smb_fname_base);
2341
2342 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
2343 if (rc != 0) {
2344 int saved_errno = errno;
2345 if (hostfd >= 0) {
2346 /*
2347 * BUGBUGBUG -- we would need to call
2348 * fd_close_posix here, but we don't have a
2349 * full fsp yet
2350 */
2351 fsp->fh->fd = hostfd;
2352 SMB_VFS_CLOSE(fsp);
2353 }
2354 hostfd = -1;
2355 errno = saved_errno;
2356 }
2357 return hostfd;
2358}
2359
2360static int fruit_open(vfs_handle_struct *handle,
2361 struct smb_filename *smb_fname,
2362 files_struct *fsp, int flags, mode_t mode)
2363{
2364 DEBUG(10, ("fruit_open called for %s\n",
2365 smb_fname_str_dbg(smb_fname)));
2366
2367 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2368 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2369 }
2370
2371 if (is_afpinfo_stream(smb_fname)) {
2372 return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
2373 } else if (is_afpresource_stream(smb_fname)) {
2374 return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
2375 }
2376
2377 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
2378}
2379
2380static int fruit_rename(struct vfs_handle_struct *handle,
2381 const struct smb_filename *smb_fname_src,
2382 const struct smb_filename *smb_fname_dst)
2383{
2384 int rc = -1;
2385 char *src_adouble_path = NULL;
2386 char *dst_adouble_path = NULL;
2387 struct fruit_config_data *config = NULL;
2388
2389 rc = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
2390
2391 if (!VALID_STAT(smb_fname_src->st)
2392 || !S_ISREG(smb_fname_src->st.st_ex_mode)) {
2393 return rc;
2394 }
2395
2396 SMB_VFS_HANDLE_GET_DATA(handle, config,
2397 struct fruit_config_data, return -1);
2398
2399 if (config->rsrc == FRUIT_RSRC_XATTR) {
2400 return rc;
2401 }
2402
2403 rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
2404 &src_adouble_path);
2405 if (rc != 0) {
2406 goto done;
2407 }
2408 rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
2409 &dst_adouble_path);
2410 if (rc != 0) {
2411 goto done;
2412 }
2413
2414 DEBUG(10, ("fruit_rename: %s -> %s\n",
2415 src_adouble_path, dst_adouble_path));
2416
2417 rc = rename(src_adouble_path, dst_adouble_path);
2418 if (errno == ENOENT) {
2419 rc = 0;
2420 }
2421
2422 TALLOC_FREE(src_adouble_path);
2423 TALLOC_FREE(dst_adouble_path);
2424
2425done:
2426 return rc;
2427}
2428
2429static int fruit_unlink(vfs_handle_struct *handle,
2430 const struct smb_filename *smb_fname)
2431{
2432 int rc = -1;
2433 struct fruit_config_data *config = NULL;
2434
2435 SMB_VFS_HANDLE_GET_DATA(handle, config,
2436 struct fruit_config_data, return -1);
2437
2438 if (!is_ntfs_stream_smb_fname(smb_fname)) {
2439 char *adp = NULL;
2440
2441 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2442 if (rc != 0) {
2443 return -1;
2444 }
2445
2446 if (config->rsrc != FRUIT_RSRC_ADFILE) {
2447 return 0;
2448 }
2449
2450 /*
2451 * 0 byte resource fork streams are not listed by
2452 * vfs_streaminfo, as a result stream cleanup/deletion of file
2453 * deletion doesn't remove the resourcefork stream.
2454 */
2455 rc = adouble_path(talloc_tos(),
2456 smb_fname->base_name, &adp);
2457 if (rc != 0) {
2458 return -1;
2459 }
2460
2461 /* FIXME: direct unlink(), missing smb_fname */
2462 DBG_DEBUG("fruit_unlink: %s\n", adp);
2463 rc = unlink(adp);
2464 if ((rc == -1) && (errno == ENOENT)) {
2465 rc = 0;
2466 }
2467
2468 TALLOC_FREE(adp);
2469 return 0;
2470 }
2471
2472 if (is_afpinfo_stream(smb_fname)) {
2473 if (config->meta == FRUIT_META_STREAM) {
2474 rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2475 } else {
2476 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2477 smb_fname->base_name,
2478 AFPINFO_EA_NETATALK);
2479 }
2480
2481 return rc;
2482 }
2483
2484 if (is_afpresource_stream(smb_fname)) {
2485 /* OS X ignores deletes on the AFP_Resource stream */
2486 return 0;
2487 }
2488
2489 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
2490
2491
2492 return 0;
2493}
2494
2495static int fruit_chmod(vfs_handle_struct *handle,
2496 const char *path,
2497 mode_t mode)
2498{
2499 int rc = -1;
2500 char *adp = NULL;
2501 struct fruit_config_data *config = NULL;
2502 SMB_STRUCT_STAT sb;
2503
2504 rc = SMB_VFS_NEXT_CHMOD(handle, path, mode);
2505 if (rc != 0) {
2506 return rc;
2507 }
2508
2509 SMB_VFS_HANDLE_GET_DATA(handle, config,
2510 struct fruit_config_data, return -1);
2511
2512 if (config->rsrc == FRUIT_RSRC_XATTR) {
2513 return 0;
2514 }
2515
2516 /* FIXME: direct sys_lstat(), missing smb_fname */
2517 rc = sys_lstat(path, &sb, false);
2518 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2519 return rc;
2520 }
2521
2522 rc = adouble_path(talloc_tos(), path, &adp);
2523 if (rc != 0) {
2524 return -1;
2525 }
2526
2527 DEBUG(10, ("fruit_chmod: %s\n", adp));
2528
2529 rc = SMB_VFS_NEXT_CHMOD(handle, adp, mode);
2530 if (errno == ENOENT) {
2531 rc = 0;
2532 }
2533
2534 TALLOC_FREE(adp);
2535 return rc;
2536}
2537
2538static int fruit_chown(vfs_handle_struct *handle,
2539 const char *path,
2540 uid_t uid,
2541 gid_t gid)
2542{
2543 int rc = -1;
2544 char *adp = NULL;
2545 struct fruit_config_data *config = NULL;
2546 SMB_STRUCT_STAT sb;
2547
2548 rc = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
2549 if (rc != 0) {
2550 return rc;
2551 }
2552
2553 SMB_VFS_HANDLE_GET_DATA(handle, config,
2554 struct fruit_config_data, return -1);
2555
2556 if (config->rsrc == FRUIT_RSRC_XATTR) {
2557 return rc;
2558 }
2559
2560 /* FIXME: direct sys_lstat(), missing smb_fname */
2561 rc = sys_lstat(path, &sb, false);
2562 if (rc != 0 || !S_ISREG(sb.st_ex_mode)) {
2563 return rc;
2564 }
2565
2566 rc = adouble_path(talloc_tos(), path, &adp);
2567 if (rc != 0) {
2568 goto done;
2569 }
2570
2571 DEBUG(10, ("fruit_chown: %s\n", adp));
2572
2573 rc = SMB_VFS_NEXT_CHOWN(handle, adp, uid, gid);
2574 if (errno == ENOENT) {
2575 rc = 0;
2576 }
2577
2578 done:
2579 TALLOC_FREE(adp);
2580 return rc;
2581}
2582
2583static int fruit_rmdir(struct vfs_handle_struct *handle, const char *path)
2584{
2585 DIR *dh = NULL;
2586 struct dirent *de;
2587 struct fruit_config_data *config;
2588
2589 SMB_VFS_HANDLE_GET_DATA(handle, config,
2590 struct fruit_config_data, return -1);
2591
2592 if (!handle->conn->cwd || !path || (config->rsrc == FRUIT_RSRC_XATTR)) {
2593 goto exit_rmdir;
2594 }
2595
2596 /*
2597 * Due to there is no way to change bDeleteVetoFiles variable
2598 * from this module, need to clean up ourselves
2599 */
2600 dh = opendir(path);
2601 if (dh == NULL) {
2602 goto exit_rmdir;
2603 }
2604
2605 while ((de = readdir(dh)) != NULL) {
2606 if ((strncmp(de->d_name,
2607 ADOUBLE_NAME_PREFIX,
2608 strlen(ADOUBLE_NAME_PREFIX))) == 0) {
2609 char *p = talloc_asprintf(talloc_tos(),
2610 "%s/%s",
2611 path, de->d_name);
2612 if (p == NULL) {
2613 goto exit_rmdir;
2614 }
2615 DEBUG(10, ("fruit_rmdir: delete %s\n", p));
2616 (void)unlink(p);
2617 TALLOC_FREE(p);
2618 }
2619 }
2620
2621exit_rmdir:
2622 if (dh) {
2623 closedir(dh);
2624 }
2625 return SMB_VFS_NEXT_RMDIR(handle, path);
2626}
2627
2628static ssize_t fruit_pread(vfs_handle_struct *handle,
2629 files_struct *fsp, void *data,
2630 size_t n, off_t offset)
2631{
2632 int rc = 0;
2633 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2634 handle, fsp);
2635 struct fruit_config_data *config = NULL;
2636 AfpInfo *ai = NULL;
2637 ssize_t len;
2638 char *name = NULL;
2639 char *tmp_base_name = NULL;
2640 NTSTATUS status;
2641
2642 DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
2643
2644 if (!fsp->base_fsp) {
2645 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2646 }
2647
2648 SMB_VFS_HANDLE_GET_DATA(handle, config,
2649 struct fruit_config_data, return -1);
2650
2651 /* fsp_name is not converted with vfs_catia */
2652 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2653 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2654 fsp->base_fsp->fsp_name->base_name,
2655 vfs_translate_to_unix,
2656 talloc_tos(), &name);
2657 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2658 name = talloc_strdup(talloc_tos(), tmp_base_name);
2659 if (name == NULL) {
2660 rc = -1;
2661 goto exit;
2662 }
2663 } else if (!NT_STATUS_IS_OK(status)) {
2664 errno = map_errno_from_nt_status(status);
2665 rc = -1;
2666 goto exit;
2667 }
2668 fsp->base_fsp->fsp_name->base_name = name;
2669
2670 if (ad == NULL) {
2671 len = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2672 if (len == -1) {
2673 rc = -1;
2674 goto exit;
2675 }
2676 goto exit;
2677 }
2678
2679 if (!fruit_fsp_recheck(ad, fsp)) {
2680 rc = -1;
2681 goto exit;
2682 }
2683
2684 if (ad->ad_type == ADOUBLE_META) {
2685 char afpinfo_buf[AFP_INFO_SIZE];
2686 size_t to_return;
2687
2688 /*
2689 * OS X has a off-by-1 error in the offset calculation, so we're
2690 * bug compatible here. It won't hurt, as any relevant real
2691 * world read requests from the AFP_AfpInfo stream will be
2692 * offset=0 n=60. offset is ignored anyway, see below.
2693 */
2694 if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
2695 len = 0;
2696 rc = 0;
2697 goto exit;
2698 }
2699
2700 to_return = MIN(n, AFP_INFO_SIZE);
2701
2702 ai = afpinfo_new(talloc_tos());
2703 if (ai == NULL) {
2704 rc = -1;
2705 goto exit;
2706 }
2707
2708 len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
2709 if (len == -1) {
2710 rc = -1;
2711 goto exit;
2712 }
2713
2714 memcpy(&ai->afpi_FinderInfo[0],
2715 ad_entry(ad, ADEID_FINDERI),
2716 ADEDLEN_FINDERI);
2717 len = afpinfo_pack(ai, afpinfo_buf);
2718 if (len != AFP_INFO_SIZE) {
2719 rc = -1;
2720 goto exit;
2721 }
2722
2723 /*
2724 * OS X ignores offset when reading from AFP_AfpInfo stream!
2725 */
2726 memcpy(data, afpinfo_buf, to_return);
2727 len = to_return;
2728 } else {
2729 len = SMB_VFS_NEXT_PREAD(
2730 handle, fsp, data, n,
2731 offset + ad_getentryoff(ad, ADEID_RFORK));
2732 if (len == -1) {
2733 rc = -1;
2734 goto exit;
2735 }
2736 }
2737exit:
2738 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2739 TALLOC_FREE(name);
2740 TALLOC_FREE(ai);
2741 if (rc != 0) {
2742 len = -1;
2743 }
2744 DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
2745 return len;
2746}
2747
2748static ssize_t fruit_pwrite(vfs_handle_struct *handle,
2749 files_struct *fsp, const void *data,
2750 size_t n, off_t offset)
2751{
2752 int rc = 0;
2753 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
2754 handle, fsp);
2755 struct fruit_config_data *config = NULL;
2756 AfpInfo *ai = NULL;
2757 ssize_t len;
2758 char *name = NULL;
2759 char *tmp_base_name = NULL;
2760 NTSTATUS status;
2761
2762 DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
2763
2764 if (!fsp->base_fsp) {
2765 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2766 }
2767
2768 SMB_VFS_HANDLE_GET_DATA(handle, config,
2769 struct fruit_config_data, return -1);
2770
2771 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
2772 status = SMB_VFS_TRANSLATE_NAME(handle->conn,
2773 fsp->base_fsp->fsp_name->base_name,
2774 vfs_translate_to_unix,
2775 talloc_tos(), &name);
2776 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
2777 name = talloc_strdup(talloc_tos(), tmp_base_name);
2778 if (name == NULL) {
2779 rc = -1;
2780 goto exit;
2781 }
2782 } else if (!NT_STATUS_IS_OK(status)) {
2783 errno = map_errno_from_nt_status(status);
2784 rc = -1;
2785 goto exit;
2786 }
2787 fsp->base_fsp->fsp_name->base_name = name;
2788
2789 if (ad == NULL) {
2790 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2791 if (len != n) {
2792 rc = -1;
2793 goto exit;
2794 }
2795 goto exit;
2796 }
2797
2798 if (!fruit_fsp_recheck(ad, fsp)) {
2799 rc = -1;
2800 goto exit;
2801 }
2802
2803 if (ad->ad_type == ADOUBLE_META) {
2804 if (n != AFP_INFO_SIZE || offset != 0) {
2805 DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
2806 (intmax_t)offset, (intmax_t)n));
2807 rc = -1;
2808 goto exit;
2809 }
2810 ai = afpinfo_unpack(talloc_tos(), data);
2811 if (ai == NULL) {
2812 rc = -1;
2813 goto exit;
2814 }
2815 memcpy(ad_entry(ad, ADEID_FINDERI),
2816 &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
2817 if (empty_finderinfo(ad)) {
2818 /* Discard metadata */
2819 if (config->meta == FRUIT_META_STREAM) {
2820 rc = SMB_VFS_FTRUNCATE(fsp, 0);
2821 } else {
2822 rc = SMB_VFS_REMOVEXATTR(handle->conn,
2823 fsp->fsp_name->base_name,
2824 AFPINFO_EA_NETATALK);
2825 }
2826 if (rc != 0 && errno != ENOENT && errno != ENOATTR) {
2827 DBG_WARNING("Can't delete metadata for %s: %s\n",
2828 fsp->fsp_name->base_name, strerror(errno));
2829 goto exit;
2830 }
2831 rc = 0;
2832 goto exit;
2833 }
2834 rc = ad_write(ad, name);
2835 } else {
2836 len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
2837 offset + ad_getentryoff(ad, ADEID_RFORK));
2838 if (len != n) {
2839 rc = -1;
2840 goto exit;
2841 }
2842
2843 if (config->rsrc == FRUIT_RSRC_ADFILE) {
2844 rc = ad_read(ad, name);
2845 if (rc == -1) {
2846 goto exit;
2847 }
2848 rc = 0;
2849
2850 if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
2851 ad_setentrylen(ad, ADEID_RFORK, len + offset);
2852 rc = ad_write(ad, name);
2853 }
2854 }
2855 }
2856
2857exit:
2858 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
2859 TALLOC_FREE(name);
2860 TALLOC_FREE(ai);
2861 if (rc != 0) {
2862 return -1;
2863 }
2864 return n;
2865}
2866
2867/**
2868 * Helper to stat/lstat the base file of an smb_fname.
2869 */
2870static int fruit_stat_base(vfs_handle_struct *handle,
2871 struct smb_filename *smb_fname,
2872 bool follow_links)
2873{
2874 char *tmp_stream_name;
2875 int rc;
2876
2877 tmp_stream_name = smb_fname->stream_name;
2878 smb_fname->stream_name = NULL;
2879 if (follow_links) {
2880 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2881 } else {
2882 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2883 }
2884 smb_fname->stream_name = tmp_stream_name;
2885 return rc;
2886}
2887
2888static int fruit_stat_meta(vfs_handle_struct *handle,
2889 struct smb_filename *smb_fname,
2890 bool follow_links)
2891{
2892 struct adouble *ad = NULL;
2893
2894 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
2895 if (ad == NULL) {
2896 DBG_INFO("fruit_stat_meta %s: %s\n",
2897 smb_fname_str_dbg(smb_fname), strerror(errno));
2898 errno = ENOENT;
2899 return -1;
2900 }
2901 TALLOC_FREE(ad);
2902
2903 /* Populate the stat struct with info from the base file. */
2904 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2905 return -1;
2906 }
2907 smb_fname->st.st_ex_size = AFP_INFO_SIZE;
2908 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2909 smb_fname->stream_name);
2910 return 0;
2911}
2912
2913static int fruit_stat_rsrc(vfs_handle_struct *handle,
2914 struct smb_filename *smb_fname,
2915 bool follow_links)
2916
2917{
2918 struct adouble *ad = NULL;
2919
2920 DEBUG(10, ("fruit_stat_rsrc called for %s\n",
2921 smb_fname_str_dbg(smb_fname)));
2922
2923 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
2924 if (ad == NULL) {
2925 errno = ENOENT;
2926 return -1;
2927 }
2928
2929 /* Populate the stat struct with info from the base file. */
2930 if (fruit_stat_base(handle, smb_fname, follow_links) == -1) {
2931 TALLOC_FREE(ad);
2932 return -1;
2933 }
2934
2935 smb_fname->st.st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
2936 smb_fname->st.st_ex_ino = fruit_inode(&smb_fname->st,
2937 smb_fname->stream_name);
2938 TALLOC_FREE(ad);
2939 return 0;
2940}
2941
2942static int fruit_stat(vfs_handle_struct *handle,
2943 struct smb_filename *smb_fname)
2944{
2945 int rc = -1;
2946
2947 DEBUG(10, ("fruit_stat called for %s\n",
2948 smb_fname_str_dbg(smb_fname)));
2949
2950 if (!is_ntfs_stream_smb_fname(smb_fname)
2951 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2952 rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
2953 if (rc == 0) {
2954 update_btime(handle, smb_fname);
2955 }
2956 return rc;
2957 }
2958
2959 /*
2960 * Note if lp_posix_paths() is true, we can never
2961 * get here as is_ntfs_stream_smb_fname() is
2962 * always false. So we never need worry about
2963 * not following links here.
2964 */
2965
2966 if (is_afpinfo_stream(smb_fname)) {
2967 rc = fruit_stat_meta(handle, smb_fname, true);
2968 } else if (is_afpresource_stream(smb_fname)) {
2969 rc = fruit_stat_rsrc(handle, smb_fname, true);
2970 } else {
2971 return SMB_VFS_NEXT_STAT(handle, smb_fname);
2972 }
2973
2974 if (rc == 0) {
2975 update_btime(handle, smb_fname);
2976 smb_fname->st.st_ex_mode &= ~S_IFMT;
2977 smb_fname->st.st_ex_mode |= S_IFREG;
2978 smb_fname->st.st_ex_blocks =
2979 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
2980 }
2981 return rc;
2982}
2983
2984static int fruit_lstat(vfs_handle_struct *handle,
2985 struct smb_filename *smb_fname)
2986{
2987 int rc = -1;
2988
2989 DEBUG(10, ("fruit_lstat called for %s\n",
2990 smb_fname_str_dbg(smb_fname)));
2991
2992 if (!is_ntfs_stream_smb_fname(smb_fname)
2993 || is_ntfs_default_stream_smb_fname(smb_fname)) {
2994 rc = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
2995 if (rc == 0) {
2996 update_btime(handle, smb_fname);
2997 }
2998 return rc;
2999 }
3000
3001 if (is_afpinfo_stream(smb_fname)) {
3002 rc = fruit_stat_meta(handle, smb_fname, false);
3003 } else if (is_afpresource_stream(smb_fname)) {
3004 rc = fruit_stat_rsrc(handle, smb_fname, false);
3005 } else {
3006 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
3007 }
3008
3009 if (rc == 0) {
3010 update_btime(handle, smb_fname);
3011 smb_fname->st.st_ex_mode &= ~S_IFMT;
3012 smb_fname->st.st_ex_mode |= S_IFREG;
3013 smb_fname->st.st_ex_blocks =
3014 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
3015 }
3016 return rc;
3017}
3018
3019static int fruit_fstat_meta(vfs_handle_struct *handle,
3020 files_struct *fsp,
3021 SMB_STRUCT_STAT *sbuf)
3022{
3023 DEBUG(10, ("fruit_fstat_meta called for %s\n",
3024 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3025
3026 /* Populate the stat struct with info from the base file. */
3027 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3028 return -1;
3029 }
3030 *sbuf = fsp->base_fsp->fsp_name->st;
3031 sbuf->st_ex_size = AFP_INFO_SIZE;
3032 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3033
3034 return 0;
3035}
3036
3037static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
3038 SMB_STRUCT_STAT *sbuf)
3039{
3040 struct fruit_config_data *config;
3041 struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
3042 handle, fsp);
3043
3044 DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
3045 smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
3046
3047 SMB_VFS_HANDLE_GET_DATA(handle, config,
3048 struct fruit_config_data, return -1);
3049
3050 if (config->rsrc == FRUIT_RSRC_STREAM) {
3051 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3052 }
3053
3054 /* Populate the stat struct with info from the base file. */
3055 if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
3056 return -1;
3057 }
3058 *sbuf = fsp->base_fsp->fsp_name->st;
3059 sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
3060 sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
3061
3062 DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
3063 smb_fname_str_dbg(fsp->fsp_name),
3064 (ssize_t)sbuf->st_ex_size));
3065
3066 return 0;
3067}
3068
3069static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
3070 SMB_STRUCT_STAT *sbuf)
3071{
3072 int rc;
3073 char *name = NULL;
3074 char *tmp_base_name = NULL;
3075 NTSTATUS status;
3076 struct adouble *ad = (struct adouble *)
3077 VFS_FETCH_FSP_EXTENSION(handle, fsp);
3078
3079 DEBUG(10, ("fruit_fstat called for %s\n",
3080 smb_fname_str_dbg(fsp->fsp_name)));
3081
3082 if (fsp->base_fsp) {
3083 tmp_base_name = fsp->base_fsp->fsp_name->base_name;
3084 /* fsp_name is not converted with vfs_catia */
3085 status = SMB_VFS_TRANSLATE_NAME(
3086 handle->conn,
3087 fsp->base_fsp->fsp_name->base_name,
3088 vfs_translate_to_unix,
3089 talloc_tos(), &name);
3090
3091 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
3092 name = talloc_strdup(talloc_tos(), tmp_base_name);
3093 if (name == NULL) {
3094 rc = -1;
3095 goto exit;
3096 }
3097 } else if (!NT_STATUS_IS_OK(status)) {
3098 errno = map_errno_from_nt_status(status);
3099 rc = -1;
3100 goto exit;
3101 }
3102 fsp->base_fsp->fsp_name->base_name = name;
3103 }
3104
3105 if (ad == NULL || fsp->base_fsp == NULL) {
3106 rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
3107 goto exit;
3108 }
3109
3110 if (!fruit_fsp_recheck(ad, fsp)) {
3111 rc = -1;
3112 goto exit;
3113 }
3114
3115 switch (ad->ad_type) {
3116 case ADOUBLE_META:
3117 rc = fruit_fstat_meta(handle, fsp, sbuf);
3118 break;
3119 case ADOUBLE_RSRC:
3120 rc = fruit_fstat_rsrc(handle, fsp, sbuf);
3121 break;
3122 default:
3123 DEBUG(10, ("fruit_fstat %s: bad type\n",
3124 smb_fname_str_dbg(fsp->fsp_name)));
3125 rc = -1;
3126 goto exit;
3127 }
3128
3129 if (rc == 0) {
3130 sbuf->st_ex_mode &= ~S_IFMT;
3131 sbuf->st_ex_mode |= S_IFREG;
3132 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
3133 }
3134
3135exit:
3136 DEBUG(10, ("fruit_fstat %s, size: %zd\n",
3137 smb_fname_str_dbg(fsp->fsp_name),
3138 (ssize_t)sbuf->st_ex_size));
3139 if (tmp_base_name) {
3140 fsp->base_fsp->fsp_name->base_name = tmp_base_name;
3141 }
3142 TALLOC_FREE(name);
3143 return rc;
3144}
3145
3146static NTSTATUS fruit_streaminfo(vfs_handle_struct *handle,
3147 struct files_struct *fsp,
3148 const char *fname,
3149 TALLOC_CTX *mem_ctx,
3150 unsigned int *pnum_streams,
3151 struct stream_struct **pstreams)
3152{
3153 struct fruit_config_data *config = NULL;
3154 struct smb_filename *smb_fname = NULL;
3155 struct adouble *ad = NULL;
3156 NTSTATUS status;
3157
3158 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3159 return NT_STATUS_UNSUCCESSFUL);
3160 DEBUG(10, ("fruit_streaminfo called for %s\n", fname));
3161
3162 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL, NULL);
3163 if (smb_fname == NULL) {
3164 return NT_STATUS_NO_MEMORY;
3165 }
3166
3167 if (config->meta == FRUIT_META_NETATALK) {
3168 ad = ad_get(talloc_tos(), handle,
3169 smb_fname->base_name, ADOUBLE_META);
3170 if (ad && !empty_finderinfo(ad)) {
3171 if (!add_fruit_stream(
3172 mem_ctx, pnum_streams, pstreams,
3173 AFPINFO_STREAM_NAME, AFP_INFO_SIZE,
3174 smb_roundup(handle->conn,
3175 AFP_INFO_SIZE))) {
3176 TALLOC_FREE(ad);
3177 TALLOC_FREE(smb_fname);
3178 return NT_STATUS_NO_MEMORY;
3179 }
3180 }
3181 TALLOC_FREE(ad);
3182 }
3183
3184 if (config->rsrc != FRUIT_RSRC_STREAM) {
3185 ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
3186 ADOUBLE_RSRC);
3187 if (ad && (ad_getentrylen(ad, ADEID_RFORK) > 0)) {
3188 if (!add_fruit_stream(
3189 mem_ctx, pnum_streams, pstreams,
3190 AFPRESOURCE_STREAM_NAME,
3191 ad_getentrylen(ad, ADEID_RFORK),
3192 smb_roundup(handle->conn,
3193 ad_getentrylen(
3194 ad, ADEID_RFORK)))) {
3195 TALLOC_FREE(ad);
3196 TALLOC_FREE(smb_fname);
3197 return NT_STATUS_NO_MEMORY;
3198 }
3199 }
3200 TALLOC_FREE(ad);
3201 }
3202
3203 TALLOC_FREE(smb_fname);
3204
3205 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx,
3206 pnum_streams, pstreams);
3207 if (!NT_STATUS_IS_OK(status)) {
3208 return status;
3209 }
3210
3211 if (config->meta == FRUIT_META_NETATALK) {
3212 /* Remove the Netatalk xattr from the list */
3213 if (!del_fruit_stream(mem_ctx, pnum_streams, pstreams,
3214 ":" NETATALK_META_XATTR ":$DATA")) {
3215 TALLOC_FREE(ad);
3216 TALLOC_FREE(smb_fname);
3217 return NT_STATUS_NO_MEMORY;
3218 }
3219 }
3220
3221 return NT_STATUS_OK;
3222}
3223
3224static int fruit_ntimes(vfs_handle_struct *handle,
3225 const struct smb_filename *smb_fname,
3226 struct smb_file_time *ft)
3227{
3228 int rc = 0;
3229 struct adouble *ad = NULL;
3230
3231 if (null_timespec(ft->create_time)) {
3232 goto exit;
3233 }
3234
3235 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
3236 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
3237
3238 ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
3239 if (ad == NULL) {
3240 goto exit;
3241 }
3242
3243 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
3244 convert_time_t_to_uint32_t(ft->create_time.tv_sec));
3245
3246 rc = ad_write(ad, smb_fname->base_name);
3247
3248exit:
3249
3250 TALLOC_FREE(ad);
3251 if (rc != 0) {
3252 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname)));
3253 return -1;
3254 }
3255 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
3256}
3257
3258static int fruit_fallocate(struct vfs_handle_struct *handle,
3259 struct files_struct *fsp,
3260 uint32_t mode,
3261 off_t offset,
3262 off_t len)
3263{
3264 struct adouble *ad =
3265 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3266
3267 if (ad == NULL) {
3268 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
3269 }
3270
3271 if (!fruit_fsp_recheck(ad, fsp)) {
3272 return -1;
3273 }
3274
3275 /* Let the pwrite code path handle it. */
3276 errno = ENOSYS;
3277 return -1;
3278}
3279
3280static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
3281 struct files_struct *fsp,
3282 off_t offset,
3283 struct adouble *ad)
3284{
3285 struct fruit_config_data *config;
3286
3287 SMB_VFS_HANDLE_GET_DATA(handle, config,
3288 struct fruit_config_data, return -1);
3289
3290 if (offset > 60) {
3291 DBG_WARNING("ftruncate %s to %jd",
3292 fsp_str_dbg(fsp), (intmax_t)offset);
3293 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
3294 errno = EOVERFLOW;
3295 return -1;
3296 }
3297
3298 DBG_WARNING("ignoring ftruncate %s to %jd",
3299 fsp_str_dbg(fsp), (intmax_t)offset);
3300 /* OS X returns success but does nothing */
3301 return 0;
3302}
3303
3304static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
3305 struct files_struct *fsp,
3306 off_t offset,
3307 struct adouble *ad)
3308{
3309 int rc;
3310 struct fruit_config_data *config;
3311
3312 SMB_VFS_HANDLE_GET_DATA(handle, config,
3313 struct fruit_config_data, return -1);
3314
3315 if (config->rsrc == FRUIT_RSRC_XATTR && offset == 0) {
3316 return SMB_VFS_FREMOVEXATTR(fsp,
3317 AFPRESOURCE_EA_NETATALK);
3318 }
3319
3320 rc = SMB_VFS_NEXT_FTRUNCATE(
3321 handle, fsp,
3322 offset + ad_getentryoff(ad, ADEID_RFORK));
3323 if (rc != 0) {
3324 return -1;
3325 }
3326
3327 if (config->rsrc == FRUIT_RSRC_ADFILE) {
3328 ad_setentrylen(ad, ADEID_RFORK, offset);
3329 rc = ad_write(ad, NULL);
3330 if (rc != 0) {
3331 return -1;
3332 }
3333 DEBUG(10, ("fruit_ftruncate_rsrc file %s offset %jd\n",
3334 fsp_str_dbg(fsp), (intmax_t)offset));
3335 }
3336
3337 return 0;
3338}
3339
3340static int fruit_ftruncate(struct vfs_handle_struct *handle,
3341 struct files_struct *fsp,
3342 off_t offset)
3343{
3344 int rc = 0;
3345 struct adouble *ad =
3346 (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
3347
3348 DBG_DEBUG("fruit_ftruncate called for file %s offset %.0f\n",
3349 fsp_str_dbg(fsp), (double)offset);
3350
3351 if (ad == NULL) {
3352 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
3353 }
3354
3355 if (!fruit_fsp_recheck(ad, fsp)) {
3356 return -1;
3357 }
3358
3359 switch (ad->ad_type) {
3360 case ADOUBLE_META:
3361 rc = fruit_ftruncate_meta(handle, fsp, offset, ad);
3362 break;
3363
3364 case ADOUBLE_RSRC:
3365 rc = fruit_ftruncate_rsrc(handle, fsp, offset, ad);
3366 break;
3367
3368 default:
3369 return -1;
3370 }
3371
3372 return rc;
3373}
3374
3375static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
3376 struct smb_request *req,
3377 uint16_t root_dir_fid,
3378 struct smb_filename *smb_fname,
3379 uint32_t access_mask,
3380 uint32_t share_access,
3381 uint32_t create_disposition,
3382 uint32_t create_options,
3383 uint32_t file_attributes,
3384 uint32_t oplock_request,
3385 struct smb2_lease *lease,
3386 uint64_t allocation_size,
3387 uint32_t private_flags,
3388 struct security_descriptor *sd,
3389 struct ea_list *ea_list,
3390 files_struct **result,
3391 int *pinfo,
3392 const struct smb2_create_blobs *in_context_blobs,
3393 struct smb2_create_blobs *out_context_blobs)
3394{
3395 NTSTATUS status;
3396 struct fruit_config_data *config = NULL;
3397 files_struct *fsp = NULL;
3398
3399 status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
3400 if (!NT_STATUS_IS_OK(status)) {
3401 goto fail;
3402 }
3403
3404 SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
3405 return NT_STATUS_UNSUCCESSFUL);
3406
3407 status = SMB_VFS_NEXT_CREATE_FILE(
3408 handle, req, root_dir_fid, smb_fname,
3409 access_mask, share_access,
3410 create_disposition, create_options,
3411 file_attributes, oplock_request,
3412 lease,
3413 allocation_size, private_flags,
3414 sd, ea_list, result,
3415 pinfo, in_context_blobs, out_context_blobs);
3416 if (!NT_STATUS_IS_OK(status)) {
3417 return status;
3418 }
3419
3420 fsp = *result;
3421
3422 if (config->nego_aapl) {
3423 if (config->copyfile_enabled) {
3424 /*
3425 * Set a flag in the fsp. Gets used in
3426 * copychunk to check whether the special
3427 * Apple copyfile semantics for copychunk
3428 * should be allowed in a copychunk request
3429 * with a count of 0.
3430 */
3431 fsp->aapl_copyfile_supported = true;
3432 }
3433
3434 if (config->posix_rename && fsp->is_directory) {
3435 /*
3436 * Enable POSIX directory rename behaviour
3437 */
3438 fsp->posix_flags |= FSP_POSIX_FLAGS_RENAME;
3439 }
3440 }
3441
3442 /*
3443 * If this is a plain open for existing files, opening an 0
3444 * byte size resource fork MUST fail with
3445 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
3446 *
3447 * Cf the vfs_fruit torture tests in test_rfork_create().
3448 */
3449 if (is_afpresource_stream(fsp->fsp_name) &&
3450 create_disposition == FILE_OPEN)
3451 {
3452 if (fsp->fsp_name->st.st_ex_size == 0) {
3453 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3454 goto fail;
3455 }
3456 }
3457
3458 if (is_ntfs_stream_smb_fname(smb_fname)
3459 || fsp->is_directory) {
3460 return status;
3461 }
3462
3463 if (config->locking == FRUIT_LOCKING_NETATALK) {
3464 status = fruit_check_access(
3465 handle, *result,
3466 access_mask,
3467 map_share_mode_to_deny_mode(share_access, 0));
3468 if (!NT_STATUS_IS_OK(status)) {
3469 goto fail;
3470 }
3471 }
3472
3473 return status;
3474
3475fail:
3476 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status)));
3477
3478 if (fsp) {
3479 close_file(req, fsp, ERROR_CLOSE);
3480 *result = fsp = NULL;
3481 }
3482
3483 return status;
3484}
3485
3486static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
3487 const struct smb_filename *fname,
3488 TALLOC_CTX *mem_ctx,
3489 struct readdir_attr_data **pattr_data)
3490{
3491 struct fruit_config_data *config = NULL;
3492 struct readdir_attr_data *attr_data;
3493 NTSTATUS status;
3494
3495 SMB_VFS_HANDLE_GET_DATA(handle, config,
3496 struct fruit_config_data,
3497 return NT_STATUS_UNSUCCESSFUL);
3498
3499 if (!config->use_aapl) {
3500 return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
3501 }
3502
3503 DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
3504
3505 *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
3506 if (*pattr_data == NULL) {
3507 return NT_STATUS_UNSUCCESSFUL;
3508 }
3509 attr_data = *pattr_data;
3510 attr_data->type = RDATTR_AAPL;
3511
3512 /*
3513 * Mac metadata: compressed FinderInfo, resource fork length
3514 * and creation date
3515 */
3516 status = readdir_attr_macmeta(handle, fname, attr_data);
3517 if (!NT_STATUS_IS_OK(status)) {
3518 /*
3519 * Error handling is tricky: if we return failure from
3520 * this function, the corresponding directory entry
3521 * will to be passed to the client, so we really just
3522 * want to error out on fatal errors.
3523 */
3524 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3525 goto fail;
3526 }
3527 }
3528
3529 /*
3530 * UNIX mode
3531 */
3532 if (config->unix_info_enabled) {
3533 attr_data->attr_data.aapl.unix_mode = fname->st.st_ex_mode;
3534 }
3535
3536 /*
3537 * max_access
3538 */
3539 if (!config->readdir_attr_max_access) {
3540 attr_data->attr_data.aapl.max_access = FILE_GENERIC_ALL;
3541 } else {
3542 status = smbd_calculate_access_mask(
3543 handle->conn,
3544 fname,
3545 false,
3546 SEC_FLAG_MAXIMUM_ALLOWED,
3547 &attr_data->attr_data.aapl.max_access);
3548 if (!NT_STATUS_IS_OK(status)) {
3549 goto fail;
3550 }
3551 }
3552
3553 return NT_STATUS_OK;
3554
3555fail:
3556 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
3557 fname->base_name, nt_errstr(status)));
3558 TALLOC_FREE(*pattr_data);
3559 return status;
3560}
3561
3562static NTSTATUS fruit_fget_nt_acl(vfs_handle_struct *handle,
3563 files_struct *fsp,
3564 uint32_t security_info,
3565 TALLOC_CTX *mem_ctx,
3566 struct security_descriptor **ppdesc)
3567{
3568 NTSTATUS status;
3569 struct security_ace ace;
3570 struct dom_sid sid;
3571 struct fruit_config_data *config;
3572
3573 SMB_VFS_HANDLE_GET_DATA(handle, config,
3574 struct fruit_config_data,
3575 return NT_STATUS_UNSUCCESSFUL);
3576
3577 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
3578 mem_ctx, ppdesc);
3579 if (!NT_STATUS_IS_OK(status)) {
3580 return status;
3581 }
3582
3583 /*
3584 * Add MS NFS style ACEs with uid, gid and mode
3585 */
3586 if (!config->unix_info_enabled) {
3587 return NT_STATUS_OK;
3588 }
3589
3590 /* MS NFS style mode */
3591 sid_compose(&sid, &global_sid_Unix_NFS_Mode, fsp->fsp_name->st.st_ex_mode);
3592 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3593 status = security_descriptor_dacl_add(*ppdesc, &ace);
3594 if (!NT_STATUS_IS_OK(status)) {
3595 DEBUG(1,("failed to add MS NFS style ACE\n"));
3596 return status;
3597 }
3598
3599 /* MS NFS style uid */
3600 sid_compose(&sid, &global_sid_Unix_NFS_Users, fsp->fsp_name->st.st_ex_uid);
3601 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3602 status = security_descriptor_dacl_add(*ppdesc, &ace);
3603 if (!NT_STATUS_IS_OK(status)) {
3604 DEBUG(1,("failed to add MS NFS style ACE\n"));
3605 return status;
3606 }
3607
3608 /* MS NFS style gid */
3609 sid_compose(&sid, &global_sid_Unix_NFS_Groups, fsp->fsp_name->st.st_ex_gid);
3610 init_sec_ace(&ace, &sid, SEC_ACE_TYPE_ACCESS_DENIED, 0, 0);
3611 status = security_descriptor_dacl_add(*ppdesc, &ace);
3612 if (!NT_STATUS_IS_OK(status)) {
3613 DEBUG(1,("failed to add MS NFS style ACE\n"));
3614 return status;
3615 }
3616
3617 return NT_STATUS_OK;
3618}
3619
3620static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
3621 files_struct *fsp,
3622 uint32_t security_info_sent,
3623 const struct security_descriptor *psd)
3624{
3625 NTSTATUS status;
3626 bool do_chmod;
3627 mode_t ms_nfs_mode;
3628 int result;
3629
3630 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp));
3631
3632 status = check_ms_nfs(handle, fsp, psd, &ms_nfs_mode, &do_chmod);
3633 if (!NT_STATUS_IS_OK(status)) {
3634 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp)));
3635 return status;
3636 }
3637
3638 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
3639 if (!NT_STATUS_IS_OK(status)) {
3640 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp)));
3641 return status;
3642 }
3643
3644 if (do_chmod) {
3645 if (fsp->fh->fd != -1) {
3646 result = SMB_VFS_FCHMOD(fsp, ms_nfs_mode);
3647 } else {
3648 result = SMB_VFS_CHMOD(fsp->conn,
3649 fsp->fsp_name->base_name,
3650 ms_nfs_mode);
3651 }
3652
3653 if (result != 0) {
3654 DEBUG(1, ("chmod: %s, result: %d, %04o error %s\n", fsp_str_dbg(fsp),
3655 result, (unsigned)ms_nfs_mode,
3656 strerror(errno)));
3657 status = map_nt_error_from_unix(errno);
3658 return status;
3659 }
3660 }
3661
3662 return NT_STATUS_OK;
3663}
3664
3665struct fruit_copy_chunk_state {
3666 struct vfs_handle_struct *handle;
3667 off_t copied;
3668 struct files_struct *src_fsp;
3669 struct files_struct *dst_fsp;
3670 bool is_copyfile;
3671};
3672
3673static void fruit_copy_chunk_done(struct tevent_req *subreq);
3674static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle,
3675 TALLOC_CTX *mem_ctx,
3676 struct tevent_context *ev,
3677 struct files_struct *src_fsp,
3678 off_t src_off,
3679 struct files_struct *dest_fsp,
3680 off_t dest_off,
3681 off_t num)
3682{
3683 struct tevent_req *req, *subreq;
3684 struct fruit_copy_chunk_state *fruit_copy_chunk_state;
3685 NTSTATUS status;
3686 struct fruit_config_data *config;
3687 off_t to_copy = num;
3688
3689 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
3690 (uintmax_t)src_off, (uintmax_t)dest_off, (uintmax_t)num));
3691
3692 SMB_VFS_HANDLE_GET_DATA(handle, config,
3693 struct fruit_config_data,
3694 return NULL);
3695
3696 req = tevent_req_create(mem_ctx, &fruit_copy_chunk_state,
3697 struct fruit_copy_chunk_state);
3698 if (req == NULL) {
3699 return NULL;
3700 }
3701 fruit_copy_chunk_state->handle = handle;
3702 fruit_copy_chunk_state->src_fsp = src_fsp;
3703 fruit_copy_chunk_state->dst_fsp = dest_fsp;
3704
3705 /*
3706 * Check if this a OS X copyfile style copychunk request with
3707 * a requested chunk count of 0 that was translated to a
3708 * copy_chunk_send VFS call overloading the parameters src_off
3709 * = dest_off = num = 0.
3710 */
3711 if ((src_off == 0) && (dest_off == 0) && (num == 0) &&
3712 src_fsp->aapl_copyfile_supported &&
3713 dest_fsp->aapl_copyfile_supported)
3714 {
3715 status = vfs_stat_fsp(src_fsp);
3716 if (tevent_req_nterror(req, status)) {
3717 return tevent_req_post(req, ev);
3718 }
3719
3720 to_copy = src_fsp->fsp_name->st.st_ex_size;
3721 fruit_copy_chunk_state->is_copyfile = true;
3722 }
3723
3724 subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
3725 mem_ctx,
3726 ev,
3727 src_fsp,
3728 src_off,
3729 dest_fsp,
3730 dest_off,
3731 to_copy);
3732 if (tevent_req_nomem(subreq, req)) {
3733 return tevent_req_post(req, ev);
3734 }
3735
3736 tevent_req_set_callback(subreq, fruit_copy_chunk_done, req);
3737 return req;
3738}
3739
3740static void fruit_copy_chunk_done(struct tevent_req *subreq)
3741{
3742 struct tevent_req *req = tevent_req_callback_data(
3743 subreq, struct tevent_req);
3744 struct fruit_copy_chunk_state *state = tevent_req_data(
3745 req, struct fruit_copy_chunk_state);
3746 NTSTATUS status;
3747 unsigned int num_streams = 0;
3748 struct stream_struct *streams = NULL;
3749 int i;
3750 struct smb_filename *src_fname_tmp = NULL;
3751 struct smb_filename *dst_fname_tmp = NULL;
3752
3753 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(state->handle,
3754 subreq,
3755 &state->copied);
3756 TALLOC_FREE(subreq);
3757 if (tevent_req_nterror(req, status)) {
3758 return;
3759 }
3760
3761 if (!state->is_copyfile) {
3762 tevent_req_done(req);
3763 return;
3764 }
3765
3766 /*
3767 * Now copy all reamining streams. We know the share supports
3768 * streams, because we're in vfs_fruit. We don't do this async
3769 * because streams are few and small.
3770 */
3771 status = vfs_streaminfo(state->handle->conn, NULL,
3772 state->src_fsp->fsp_name->base_name,
3773 req, &num_streams, &streams);
3774 if (tevent_req_nterror(req, status)) {
3775 return;
3776 }
3777
3778 if (num_streams == 1) {
3779 /* There is always one stream, ::$DATA. */
3780 tevent_req_done(req);
3781 return;
3782 }
3783
3784 for (i = 0; i < num_streams; i++) {
3785 DEBUG(10, ("%s: stream: '%s'/%ju\n",
3786 __func__, streams[i].name,
3787 (uintmax_t)streams[i].size));
3788
3789 src_fname_tmp = synthetic_smb_fname(
3790 req,
3791 state->src_fsp->fsp_name->base_name,
3792 streams[i].name,
3793 NULL);
3794 if (tevent_req_nomem(src_fname_tmp, req)) {
3795 return;
3796 }
3797
3798 if (is_ntfs_default_stream_smb_fname(src_fname_tmp)) {
3799 TALLOC_FREE(src_fname_tmp);
3800 continue;
3801 }
3802
3803 dst_fname_tmp = synthetic_smb_fname(
3804 req,
3805 state->dst_fsp->fsp_name->base_name,
3806 streams[i].name,
3807 NULL);
3808 if (tevent_req_nomem(dst_fname_tmp, req)) {
3809 TALLOC_FREE(src_fname_tmp);
3810 return;
3811 }
3812
3813 status = copy_file(req,
3814 state->handle->conn,
3815 src_fname_tmp,
3816 dst_fname_tmp,
3817 OPENX_FILE_CREATE_IF_NOT_EXIST,
3818 0, false);
3819 if (!NT_STATUS_IS_OK(status)) {
3820 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__,
3821 smb_fname_str_dbg(src_fname_tmp),
3822 smb_fname_str_dbg(dst_fname_tmp),
3823 nt_errstr(status)));
3824 TALLOC_FREE(src_fname_tmp);
3825 TALLOC_FREE(dst_fname_tmp);
3826 tevent_req_nterror(req, status);
3827 return;
3828 }
3829
3830 TALLOC_FREE(src_fname_tmp);
3831 TALLOC_FREE(dst_fname_tmp);
3832 }
3833
3834 TALLOC_FREE(streams);
3835 TALLOC_FREE(src_fname_tmp);
3836 TALLOC_FREE(dst_fname_tmp);
3837 tevent_req_done(req);
3838}
3839
3840static NTSTATUS fruit_copy_chunk_recv(struct vfs_handle_struct *handle,
3841 struct tevent_req *req,
3842 off_t *copied)
3843{
3844 struct fruit_copy_chunk_state *fruit_copy_chunk_state = tevent_req_data(
3845 req, struct fruit_copy_chunk_state);
3846 NTSTATUS status;
3847
3848 if (tevent_req_is_nterror(req, &status)) {
3849 DEBUG(1, ("server side copy chunk failed: %s\n",
3850 nt_errstr(status)));
3851 *copied = 0;
3852 tevent_req_received(req);
3853 return status;
3854 }
3855
3856 *copied = fruit_copy_chunk_state->copied;
3857 tevent_req_received(req);
3858
3859 return NT_STATUS_OK;
3860}
3861
3862static struct vfs_fn_pointers vfs_fruit_fns = {
3863 .connect_fn = fruit_connect,
3864
3865 /* File operations */
3866 .chmod_fn = fruit_chmod,
3867 .chown_fn = fruit_chown,
3868 .unlink_fn = fruit_unlink,
3869 .rename_fn = fruit_rename,
3870 .rmdir_fn = fruit_rmdir,
3871 .open_fn = fruit_open,
3872 .pread_fn = fruit_pread,
3873 .pwrite_fn = fruit_pwrite,
3874 .stat_fn = fruit_stat,
3875 .lstat_fn = fruit_lstat,
3876 .fstat_fn = fruit_fstat,
3877 .streaminfo_fn = fruit_streaminfo,
3878 .ntimes_fn = fruit_ntimes,
3879 .ftruncate_fn = fruit_ftruncate,
3880 .fallocate_fn = fruit_fallocate,
3881 .create_file_fn = fruit_create_file,
3882 .readdir_attr_fn = fruit_readdir_attr,
3883 .copy_chunk_send_fn = fruit_copy_chunk_send,
3884 .copy_chunk_recv_fn = fruit_copy_chunk_recv,
3885
3886 /* NT ACL operations */
3887 .fget_nt_acl_fn = fruit_fget_nt_acl,
3888 .fset_nt_acl_fn = fruit_fset_nt_acl,
3889};
3890
3891NTSTATUS vfs_fruit_init(void);
3892NTSTATUS vfs_fruit_init(void)
3893{
3894 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
3895 &vfs_fruit_fns);
3896 if (!NT_STATUS_IS_OK(ret)) {
3897 return ret;
3898 }
3899
3900 vfs_fruit_debug_level = debug_add_class("fruit");
3901 if (vfs_fruit_debug_level == -1) {
3902 vfs_fruit_debug_level = DBGC_VFS;
3903 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
3904 "vfs_fruit_init"));
3905 } else {
3906 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
3907 "vfs_fruit_init","fruit",vfs_fruit_debug_level));
3908 }
3909
3910 return ret;
3911}
Note: See TracBrowser for help on using the repository browser.