forked from smbecker/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvfs_fruit.c
5251 lines (4415 loc) · 119 KB
/
vfs_fruit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OS X and Netatalk interoperability VFS module for Samba-3.x
*
* Copyright (C) Ralph Boehme, 2013, 2014
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "MacExtensions.h"
#include "smbd/smbd.h"
#include "system/filesys.h"
#include "lib/util/time.h"
#include "system/shmem.h"
#include "locking/proto.h"
#include "smbd/globals.h"
#include "messages.h"
#include "libcli/security/security.h"
#include "../libcli/smb/smb2_create_ctx.h"
#include "lib/util/tevent_ntstatus.h"
#include "lib/util/tevent_unix.h"
#include "offload_token.h"
#include "string_replace.h"
#include "hash_inode.h"
#include "lib/adouble.h"
#include "lib/util_macstreams.h"
/*
* Enhanced OS X and Netatalk compatibility
* ========================================
*
* This modules takes advantage of vfs_streams_xattr and
* vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
* loaded in the correct order:
*
* vfs modules = catia fruit streams_xattr
*
* The module intercepts the OS X special streams "AFP_AfpInfo" and
* "AFP_Resource" and handles them in a special way. All other named
* streams are deferred to vfs_streams_xattr.
*
* The OS X client maps all NTFS illegal characters to the Unicode
* private range. This module optionally stores the characters using
* their native ASCII encoding using vfs_catia. If you're not enabling
* this feature, you can skip catia from vfs modules.
*
* Finally, open modes are optionally checked against Netatalk AFP
* share modes.
*
* The "AFP_AfpInfo" named stream is a binary blob containing OS X
* extended metadata for files and directories. This module optionally
* reads and stores this metadata in a way compatible with Netatalk 3
* which stores the metadata in an EA "org.netatalk.metadata". Cf
* source3/include/MacExtensions.h for a description of the binary
* blobs content.
*
* The "AFP_Resource" named stream may be arbitrarily large, thus it
* can't be stored in an xattr on most filesystem. ZFS on Solaris is
* the only available filesystem where xattrs can be of any size and
* the OS supports using the file APIs for xattrs.
*
* The AFP_Resource stream is stored in an AppleDouble file prepending
* "._" to the filename. On Solaris with ZFS the stream is optionally
* stored in an EA "org.netatalk.resource".
*
*
* Extended Attributes
* ===================
*
* The OS X SMB client sends xattrs as ADS too. For xattr interop with
* other protocols you may want to adjust the xattr names the VFS
* module vfs_streams_xattr uses for storing ADS's. This defaults to
* user.DosStream.ADS_NAME:$DATA and can be changed by specifying
* these module parameters:
*
* streams_xattr:prefix = user.
* streams_xattr:store_stream_type = false
*
*
* TODO
* ====
*
* - log diagnostic if any needed VFS module is not loaded
* (eg with lp_vfs_objects())
* - add tests
*/
static int vfs_fruit_debug_level = DBGC_VFS;
static struct global_fruit_config {
bool nego_aapl; /* client negotiated AAPL */
} global_fruit_config;
#undef DBGC_CLASS
#define DBGC_CLASS vfs_fruit_debug_level
#define FRUIT_PARAM_TYPE_NAME "fruit"
enum apple_fork {APPLE_FORK_DATA, APPLE_FORK_RSRC};
enum fruit_rsrc {FRUIT_RSRC_STREAM, FRUIT_RSRC_ADFILE, FRUIT_RSRC_XATTR};
enum fruit_meta {FRUIT_META_STREAM, FRUIT_META_NETATALK};
enum fruit_locking {FRUIT_LOCKING_NETATALK, FRUIT_LOCKING_NONE};
enum fruit_encoding {FRUIT_ENC_NATIVE, FRUIT_ENC_PRIVATE};
struct fruit_config_data {
enum fruit_rsrc rsrc;
enum fruit_meta meta;
enum fruit_locking locking;
enum fruit_encoding encoding;
bool use_aapl; /* config from smb.conf */
bool use_copyfile;
bool readdir_attr_enabled;
bool unix_info_enabled;
bool copyfile_enabled;
bool veto_appledouble;
bool posix_rename;
bool aapl_zero_file_id;
const char *model;
bool time_machine;
off_t time_machine_max_size;
bool wipe_intentionally_left_blank_rfork;
bool delete_empty_adfiles;
/*
* Additional options, all enabled by default,
* possibly useful for analyzing performance. The associated
* operations with each of them may be expensive, so having
* the chance to disable them individually gives a chance
* tweaking the setup for the particular usecase.
*/
bool readdir_attr_rsize;
bool readdir_attr_finder_info;
bool readdir_attr_max_access;
};
static const struct enum_list fruit_rsrc[] = {
{FRUIT_RSRC_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
{FRUIT_RSRC_ADFILE, "file"}, /* ._ AppleDouble file */
{FRUIT_RSRC_XATTR, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
{ -1, NULL}
};
static const struct enum_list fruit_meta[] = {
{FRUIT_META_STREAM, "stream"}, /* pass on to vfs_streams_xattr */
{FRUIT_META_NETATALK, "netatalk"}, /* Netatalk compatible xattr */
{ -1, NULL}
};
static const struct enum_list fruit_locking[] = {
{FRUIT_LOCKING_NETATALK, "netatalk"}, /* synchronize locks with Netatalk */
{FRUIT_LOCKING_NONE, "none"},
{ -1, NULL}
};
static const struct enum_list fruit_encoding[] = {
{FRUIT_ENC_NATIVE, "native"}, /* map unicode private chars to ASCII */
{FRUIT_ENC_PRIVATE, "private"}, /* keep unicode private chars */
{ -1, NULL}
};
struct fio {
vfs_handle_struct *handle;
files_struct *fsp; /* backlink to itself */
/* tcon config handle */
struct fruit_config_data *config;
/* Backend fsp for AppleDouble file, can be NULL */
files_struct *ad_fsp;
/* link from adouble_open_from_base_fsp() to fio */
struct fio *real_fio;
/* Denote stream type, meta or rsrc */
adouble_type_t type;
/*
* AFP_AfpInfo stream created, but not written yet, thus still a fake
* pipe fd. This is set to true in fruit_open_meta if there was no
* existing stream but the caller requested O_CREAT. It is later set to
* false when we get a write on the stream that then does open and
* create the stream.
*/
bool fake_fd;
int flags;
int mode;
};
/*****************************************************************************
* Helper functions
*****************************************************************************/
static struct fio *fruit_get_complete_fio(vfs_handle_struct *handle,
files_struct *fsp)
{
struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
if (fio == NULL) {
return NULL;
}
if (fio->real_fio != NULL) {
/*
* This is an fsp from adouble_open_from_base_fsp()
* we should just pass this to the next
* module.
*/
return NULL;
}
return fio;
}
/**
* Initialize config struct from our smb.conf config parameters
**/
static int init_fruit_config(vfs_handle_struct *handle)
{
struct fruit_config_data *config;
int enumval;
const char *tm_size_str = NULL;
config = talloc_zero(handle->conn, struct fruit_config_data);
if (!config) {
DEBUG(1, ("talloc_zero() failed\n"));
errno = ENOMEM;
return -1;
}
/*
* Versions up to Samba 4.5.x had a spelling bug in the
* fruit:resource option calling lp_parm_enum with
* "res*s*ource" (ie two s).
*
* In Samba 4.6 we accept both the wrong and the correct
* spelling, in Samba 4.7 the bad spelling will be removed.
*/
enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"ressource", fruit_rsrc, FRUIT_RSRC_ADFILE);
if (enumval == -1) {
DEBUG(1, ("value for %s: resource type unknown\n",
FRUIT_PARAM_TYPE_NAME));
return -1;
}
config->rsrc = (enum fruit_rsrc)enumval;
enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"resource", fruit_rsrc, enumval);
if (enumval == -1) {
DEBUG(1, ("value for %s: resource type unknown\n",
FRUIT_PARAM_TYPE_NAME));
return -1;
}
config->rsrc = (enum fruit_rsrc)enumval;
enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"metadata", fruit_meta, FRUIT_META_NETATALK);
if (enumval == -1) {
DEBUG(1, ("value for %s: metadata type unknown\n",
FRUIT_PARAM_TYPE_NAME));
return -1;
}
config->meta = (enum fruit_meta)enumval;
enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"locking", fruit_locking, FRUIT_LOCKING_NONE);
if (enumval == -1) {
DEBUG(1, ("value for %s: locking type unknown\n",
FRUIT_PARAM_TYPE_NAME));
return -1;
}
config->locking = (enum fruit_locking)enumval;
enumval = lp_parm_enum(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"encoding", fruit_encoding, FRUIT_ENC_PRIVATE);
if (enumval == -1) {
DEBUG(1, ("value for %s: encoding type unknown\n",
FRUIT_PARAM_TYPE_NAME));
return -1;
}
config->encoding = (enum fruit_encoding)enumval;
if (config->rsrc == FRUIT_RSRC_ADFILE) {
config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
FRUIT_PARAM_TYPE_NAME,
"veto_appledouble",
true);
}
config->use_aapl = lp_parm_bool(
-1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
config->time_machine = lp_parm_bool(
SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "time machine", false);
config->unix_info_enabled = lp_parm_bool(
-1, FRUIT_PARAM_TYPE_NAME, "nfs_aces", true);
config->use_copyfile = lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME,
"copyfile", false);
config->posix_rename = lp_parm_bool(
SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
config->aapl_zero_file_id =
lp_parm_bool(SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"zero_file_id", false);
config->readdir_attr_rsize = lp_parm_bool(
SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
config->readdir_attr_finder_info = lp_parm_bool(
SNUM(handle->conn), "readdir_attr", "aapl_finder_info", true);
config->readdir_attr_max_access = lp_parm_bool(
SNUM(handle->conn), "readdir_attr", "aapl_max_access", true);
config->model = lp_parm_const_string(
-1, FRUIT_PARAM_TYPE_NAME, "model", "MacSamba");
tm_size_str = lp_parm_const_string(
SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"time machine max size", NULL);
if (tm_size_str != NULL) {
config->time_machine_max_size = conv_str_size(tm_size_str);
}
config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"wipe_intentionally_left_blank_rfork", false);
config->delete_empty_adfiles = lp_parm_bool(
SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
"delete_empty_adfiles", false);
SMB_VFS_HANDLE_SET_DATA(handle, config,
NULL, struct fruit_config_data,
return -1);
return 0;
}
static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
struct stream_struct **streams,
const char *name, off_t size,
off_t alloc_size)
{
struct stream_struct *tmp;
tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
(*num_streams)+1);
if (tmp == NULL) {
return false;
}
tmp[*num_streams].name = talloc_asprintf(tmp, "%s:$DATA", name);
if (tmp[*num_streams].name == NULL) {
return false;
}
tmp[*num_streams].size = size;
tmp[*num_streams].alloc_size = alloc_size;
*streams = tmp;
*num_streams += 1;
return true;
}
static bool filter_empty_rsrc_stream(unsigned int *num_streams,
struct stream_struct **streams)
{
struct stream_struct *tmp = *streams;
unsigned int i;
if (*num_streams == 0) {
return true;
}
for (i = 0; i < *num_streams; i++) {
if (strequal_m(tmp[i].name, AFPRESOURCE_STREAM)) {
break;
}
}
if (i == *num_streams) {
return true;
}
if (tmp[i].size > 0) {
return true;
}
TALLOC_FREE(tmp[i].name);
ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
*num_streams -= 1;
return true;
}
static bool del_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
struct stream_struct **streams,
const char *name)
{
struct stream_struct *tmp = *streams;
unsigned int i;
if (*num_streams == 0) {
return true;
}
for (i = 0; i < *num_streams; i++) {
if (strequal_m(tmp[i].name, name)) {
break;
}
}
if (i == *num_streams) {
return true;
}
TALLOC_FREE(tmp[i].name);
ARRAY_DEL_ELEMENT(tmp, i, *num_streams);
*num_streams -= 1;
return true;
}
static bool ad_empty_finderinfo(const struct adouble *ad)
{
int cmp;
char emptybuf[ADEDLEN_FINDERI] = {0};
char *fi = NULL;
fi = ad_get_entry(ad, ADEID_FINDERI);
if (fi == NULL) {
DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad);
return false;
}
cmp = memcmp(emptybuf, fi, ADEDLEN_FINDERI);
return (cmp == 0);
}
static bool ai_empty_finderinfo(const AfpInfo *ai)
{
int cmp;
char emptybuf[ADEDLEN_FINDERI] = {0};
cmp = memcmp(emptybuf, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
return (cmp == 0);
}
/**
* Update btime with btime from Netatalk
**/
static void update_btime(vfs_handle_struct *handle,
struct smb_filename *smb_fname)
{
uint32_t t;
struct timespec creation_time = {0};
struct adouble *ad;
struct fruit_config_data *config = NULL;
SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
return);
switch (config->meta) {
case FRUIT_META_STREAM:
return;
case FRUIT_META_NETATALK:
/* Handled below */
break;
default:
DBG_ERR("Unexpected meta config [%d]\n", config->meta);
return;
}
ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
if (ad == NULL) {
return;
}
if (ad_getdate(ad, AD_DATE_UNIX | AD_DATE_CREATE, &t) != 0) {
TALLOC_FREE(ad);
return;
}
TALLOC_FREE(ad);
creation_time.tv_sec = convert_uint32_t_to_time_t(t);
update_stat_ex_create_time(&smb_fname->st, creation_time);
return;
}
/**
* Map an access mask to a Netatalk single byte byte range lock
**/
static off_t access_to_netatalk_brl(enum apple_fork fork_type,
uint32_t access_mask)
{
off_t offset;
switch (access_mask) {
case FILE_READ_DATA:
offset = AD_FILELOCK_OPEN_RD;
break;
case FILE_WRITE_DATA:
case FILE_APPEND_DATA:
offset = AD_FILELOCK_OPEN_WR;
break;
default:
offset = AD_FILELOCK_OPEN_NONE;
break;
}
if (fork_type == APPLE_FORK_RSRC) {
if (offset == AD_FILELOCK_OPEN_NONE) {
offset = AD_FILELOCK_RSRC_OPEN_NONE;
} else {
offset += 2;
}
}
return offset;
}
/**
* Map a deny mode to a Netatalk brl
**/
static off_t denymode_to_netatalk_brl(enum apple_fork fork_type,
uint32_t deny_mode)
{
off_t offset = 0;
switch (deny_mode) {
case DENY_READ:
offset = AD_FILELOCK_DENY_RD;
break;
case DENY_WRITE:
offset = AD_FILELOCK_DENY_WR;
break;
default:
smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
}
if (fork_type == APPLE_FORK_RSRC) {
offset += 2;
}
return offset;
}
/**
* Call fcntl() with an exclusive F_GETLK request in order to
* determine if there's an existing shared lock
*
* @return true if the requested lock was found or any error occurred
* false if the lock was not found
**/
static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
{
bool result;
off_t offset = in_offset;
off_t len = 1;
int type = F_WRLCK;
pid_t pid = 0;
result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
if (result == false) {
return true;
}
if (type != F_UNLCK) {
return true;
}
return false;
}
static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
files_struct *fsp,
uint32_t access_mask,
uint32_t share_mode)
{
NTSTATUS status = NT_STATUS_OK;
off_t off;
bool share_for_read = (share_mode & FILE_SHARE_READ);
bool share_for_write = (share_mode & FILE_SHARE_WRITE);
bool netatalk_already_open_for_reading = false;
bool netatalk_already_open_for_writing = false;
bool netatalk_already_open_with_deny_read = false;
bool netatalk_already_open_with_deny_write = false;
struct GUID req_guid = GUID_random();
/* FIXME: hardcoded data fork, add resource fork */
enum apple_fork fork_type = APPLE_FORK_DATA;
DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
fsp_str_dbg(fsp),
access_mask & FILE_READ_DATA ? "READ" :"-",
access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
share_mode);
if (fsp_get_io_fd(fsp) == -1) {
return NT_STATUS_OK;
}
/* Read NetATalk opens and deny modes on the file. */
netatalk_already_open_for_reading = test_netatalk_lock(fsp,
access_to_netatalk_brl(fork_type,
FILE_READ_DATA));
netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
denymode_to_netatalk_brl(fork_type,
DENY_READ));
netatalk_already_open_for_writing = test_netatalk_lock(fsp,
access_to_netatalk_brl(fork_type,
FILE_WRITE_DATA));
netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
denymode_to_netatalk_brl(fork_type,
DENY_WRITE));
/* If there are any conflicts - sharing violation. */
if ((access_mask & FILE_READ_DATA) &&
netatalk_already_open_with_deny_read) {
return NT_STATUS_SHARING_VIOLATION;
}
if (!share_for_read &&
netatalk_already_open_for_reading) {
return NT_STATUS_SHARING_VIOLATION;
}
if ((access_mask & FILE_WRITE_DATA) &&
netatalk_already_open_with_deny_write) {
return NT_STATUS_SHARING_VIOLATION;
}
if (!share_for_write &&
netatalk_already_open_for_writing) {
return NT_STATUS_SHARING_VIOLATION;
}
if (!(access_mask & FILE_READ_DATA)) {
/*
* Nothing we can do here, we need read access
* to set locks.
*/
return NT_STATUS_OK;
}
/* Set NetAtalk locks matching our access */
if (access_mask & FILE_READ_DATA) {
off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
req_guid.time_hi_and_version = __LINE__;
status = do_lock(
fsp,
talloc_tos(),
&req_guid,
fsp->op->global->open_persistent_id,
1,
off,
READ_LOCK,
POSIX_LOCK,
NULL,
NULL);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
}
if (!share_for_read) {
off = denymode_to_netatalk_brl(fork_type, DENY_READ);
req_guid.time_hi_and_version = __LINE__;
status = do_lock(
fsp,
talloc_tos(),
&req_guid,
fsp->op->global->open_persistent_id,
1,
off,
READ_LOCK,
POSIX_LOCK,
NULL,
NULL);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
}
if (access_mask & FILE_WRITE_DATA) {
off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
req_guid.time_hi_and_version = __LINE__;
status = do_lock(
fsp,
talloc_tos(),
&req_guid,
fsp->op->global->open_persistent_id,
1,
off,
READ_LOCK,
POSIX_LOCK,
NULL,
NULL);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
}
if (!share_for_write) {
off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
req_guid.time_hi_and_version = __LINE__;
status = do_lock(
fsp,
talloc_tos(),
&req_guid,
fsp->op->global->open_persistent_id,
1,
off,
READ_LOCK,
POSIX_LOCK,
NULL,
NULL);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
}
return NT_STATUS_OK;
}
static NTSTATUS check_aapl(vfs_handle_struct *handle,
struct smb_request *req,
const struct smb2_create_blobs *in_context_blobs,
struct smb2_create_blobs *out_context_blobs)
{
struct fruit_config_data *config;
NTSTATUS status;
struct smb2_create_blob *aapl = NULL;
uint32_t cmd;
bool ok;
uint8_t p[16];
DATA_BLOB blob = data_blob_talloc(req, NULL, 0);
uint64_t req_bitmap, client_caps;
uint64_t server_caps = SMB2_CRTCTX_AAPL_UNIX_BASED;
smb_ucs2_t *model;
size_t modellen;
SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
return NT_STATUS_UNSUCCESSFUL);
if (!config->use_aapl
|| in_context_blobs == NULL
|| out_context_blobs == NULL) {
return NT_STATUS_OK;
}
aapl = smb2_create_blob_find(in_context_blobs,
SMB2_CREATE_TAG_AAPL);
if (aapl == NULL) {
return NT_STATUS_OK;
}
if (aapl->data.length != 24) {
DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
(uintmax_t)aapl->data.length));
return NT_STATUS_INVALID_PARAMETER;
}
cmd = IVAL(aapl->data.data, 0);
if (cmd != SMB2_CRTCTX_AAPL_SERVER_QUERY) {
DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd));
return NT_STATUS_INVALID_PARAMETER;
}
req_bitmap = BVAL(aapl->data.data, 8);
client_caps = BVAL(aapl->data.data, 16);
SIVAL(p, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY);
SIVAL(p, 4, 0);
SBVAL(p, 8, req_bitmap);
ok = data_blob_append(req, &blob, p, 16);
if (!ok) {
return NT_STATUS_UNSUCCESSFUL;
}
if (req_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) {
if ((client_caps & SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) &&
(handle->conn->tcon->compat->fs_capabilities & FILE_NAMED_STREAMS)) {
server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR;
config->readdir_attr_enabled = true;
}
if (config->use_copyfile) {
server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE;
config->copyfile_enabled = true;
}
/*
* The client doesn't set the flag, so we can't check
* for it and just set it unconditionally
*/
if (config->unix_info_enabled) {
server_caps |= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE;
}
SBVAL(p, 0, server_caps);
ok = data_blob_append(req, &blob, p, 8);
if (!ok) {
return NT_STATUS_UNSUCCESSFUL;
}
}
if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
uint64_t caps = 0;
switch (val) {
case Auto:
break;
case True:
caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
break;
default:
break;
}
if (config->time_machine) {
caps |= SMB2_CRTCTX_AAPL_FULL_SYNC;
}
SBVAL(p, 0, caps);
ok = data_blob_append(req, &blob, p, 8);
if (!ok) {
return NT_STATUS_UNSUCCESSFUL;
}
}
if (req_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) {
ok = convert_string_talloc(req,
CH_UNIX, CH_UTF16LE,
config->model, strlen(config->model),
&model, &modellen);
if (!ok) {
return NT_STATUS_UNSUCCESSFUL;
}
SIVAL(p, 0, 0);
SIVAL(p + 4, 0, modellen);
ok = data_blob_append(req, &blob, p, 8);
if (!ok) {
talloc_free(model);
return NT_STATUS_UNSUCCESSFUL;
}
ok = data_blob_append(req, &blob, model, modellen);
talloc_free(model);
if (!ok) {
return NT_STATUS_UNSUCCESSFUL;
}
}
status = smb2_create_blob_add(out_context_blobs,
out_context_blobs,
SMB2_CREATE_TAG_AAPL,
blob);
if (NT_STATUS_IS_OK(status)) {
global_fruit_config.nego_aapl = true;
}
return status;
}
static bool readdir_attr_meta_finderi_stream(
struct vfs_handle_struct *handle,
const struct smb_filename *smb_fname,
AfpInfo *ai)
{
struct smb_filename *stream_name = NULL;
files_struct *fsp = NULL;
ssize_t nread;
NTSTATUS status;
bool ok;
uint8_t buf[AFP_INFO_SIZE];
status = synthetic_pathref(talloc_tos(),
handle->conn->cwd_fsp,
smb_fname->base_name,
AFPINFO_STREAM_NAME,
NULL,
smb_fname->twrp,
smb_fname->flags,
&stream_name);
if (!NT_STATUS_IS_OK(status)) {
return false;
}
status = SMB_VFS_CREATE_FILE(
handle->conn, /* conn */
NULL, /* req */
stream_name, /* fname */
FILE_READ_DATA, /* access_mask */
(FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
FILE_SHARE_DELETE),
FILE_OPEN, /* create_disposition*/
0, /* create_options */
0, /* file_attributes */
INTERNAL_OPEN_ONLY, /* oplock_request */
NULL, /* lease */
0, /* allocation_size */
0, /* private_flags */
NULL, /* sd */
NULL, /* ea_list */
&fsp, /* result */
NULL, /* pinfo */
NULL, NULL); /* create context */
TALLOC_FREE(stream_name);
if (!NT_STATUS_IS_OK(status)) {
return false;
}
nread = SMB_VFS_PREAD(fsp, &buf[0], AFP_INFO_SIZE, 0);
if (nread != AFP_INFO_SIZE) {
DBG_ERR("short read [%s] [%zd/%d]\n",
smb_fname_str_dbg(stream_name), nread, AFP_INFO_SIZE);
ok = false;
goto fail;
}
memcpy(&ai->afpi_FinderInfo[0], &buf[AFP_OFF_FinderInfo],
AFP_FinderSize);
ok = true;
fail:
if (fsp != NULL) {
close_file(NULL, fsp, NORMAL_CLOSE);
}
return ok;
}
static bool readdir_attr_meta_finderi_netatalk(
struct vfs_handle_struct *handle,
const struct smb_filename *smb_fname,
AfpInfo *ai)
{
struct adouble *ad = NULL;
char *p = NULL;
ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
if (ad == NULL) {
return false;
}
p = ad_get_entry(ad, ADEID_FINDERI);
if (p == NULL) {
DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname->base_name);
TALLOC_FREE(ad);
return false;
}
memcpy(&ai->afpi_FinderInfo[0], p, AFP_FinderSize);
TALLOC_FREE(ad);
return true;
}
static bool readdir_attr_meta_finderi(struct vfs_handle_struct *handle,
const struct smb_filename *smb_fname,
struct readdir_attr_data *attr_data)
{
struct fruit_config_data *config = NULL;
uint32_t date_added;
AfpInfo ai = {0};
bool ok;