-
Notifications
You must be signed in to change notification settings - Fork 8
/
vfs_ixnas.c
1300 lines (1154 loc) · 33.8 KB
/
vfs_ixnas.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
/*
* Unix SMB/CIFS implementation.
* A dumping ground for FreeBSD-specific VFS functions. For testing case
* of reducing number enabled VFS modules to bare minimum by creating
* single large VFS module.
*
* 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 "libcli/security/security.h"
#include "smbd/smbd.h"
#include "system/filesys.h"
#include "nfs4_acls.h"
#include <sys/acl.h>
#if HAVE_LIBZFS
#include "modules/smb_libzfs.h"
#endif
static int vfs_ixnas_debug_level = DBGC_VFS;
#undef DBGC_CLASS
#define DBGC_CLASS vfs_ixnas_debug_level
struct ixnas_config_data {
struct smbacl4_vfs_params nfs4_params;
struct smblibzfshandle *libzp;
struct dataset_list *dsl;
bool posix_rename;
bool dosattrib_xattr;
bool zfs_acl_enabled;
bool zfs_acl_chmod_enabled;
struct zfs_dataset_prop *props;
};
static const struct {
uint32_t dosmode;
uint32_t flag;
} dosmode2flag[] = {
{ FILE_ATTRIBUTE_READONLY, UF_READONLY },
{ FILE_ATTRIBUTE_ARCHIVE, UF_ARCHIVE },
{ FILE_ATTRIBUTE_SYSTEM, UF_SYSTEM },
{ FILE_ATTRIBUTE_HIDDEN, UF_HIDDEN },
{ FILE_ATTRIBUTE_SPARSE, UF_SPARSE },
{ FILE_ATTRIBUTE_OFFLINE, UF_OFFLINE },
{ FILE_ATTRIBUTE_REPARSE_POINT, UF_REPARSE },
};
/*
* ACL_ENTRY_SUCCESSFUL_ACCESS and
* ACL_ENTRY_FAILED_ACCESS are omitted because
* they are not currently implemented in FreeBSD and Samba
*/
static const struct {
acl_flag_t bsdflag;
uint32_t nfs4flag;
} bsdflag2nfs4flag[] = {
{ ACL_ENTRY_FILE_INHERIT, SMB_ACE4_FILE_INHERIT_ACE },
{ ACL_ENTRY_DIRECTORY_INHERIT, SMB_ACE4_DIRECTORY_INHERIT_ACE },
{ ACL_ENTRY_NO_PROPAGATE_INHERIT, SMB_ACE4_NO_PROPAGATE_INHERIT_ACE },
{ ACL_ENTRY_INHERIT_ONLY, SMB_ACE4_INHERIT_ONLY_ACE },
{ ACL_ENTRY_INHERITED, SMB_ACE4_INHERITED_ACE },
};
static const struct {
acl_perm_t bsdperm;
uint32_t nfs4perm;
} bsdperm2nfs4perm[] = {
{ ACL_READ_DATA, SMB_ACE4_READ_DATA },
{ ACL_WRITE_DATA, SMB_ACE4_WRITE_DATA },
{ ACL_APPEND_DATA, SMB_ACE4_APPEND_DATA },
{ ACL_READ_NAMED_ATTRS, SMB_ACE4_READ_NAMED_ATTRS },
{ ACL_WRITE_NAMED_ATTRS, SMB_ACE4_WRITE_NAMED_ATTRS },
{ ACL_EXECUTE, SMB_ACE4_EXECUTE },
{ ACL_DELETE_CHILD, SMB_ACE4_DELETE_CHILD },
{ ACL_READ_ATTRIBUTES, SMB_ACE4_READ_ATTRIBUTES },
{ ACL_WRITE_ATTRIBUTES, SMB_ACE4_WRITE_ATTRIBUTES },
{ ACL_DELETE, SMB_ACE4_DELETE },
{ ACL_READ_ACL, SMB_ACE4_READ_ACL },
{ ACL_WRITE_ACL, SMB_ACE4_WRITE_ACL },
{ ACL_WRITE_OWNER, SMB_ACE4_WRITE_OWNER },
{ ACL_SYNCHRONIZE, SMB_ACE4_SYNCHRONIZE },
};
static NTSTATUS ixnas_fget_dos_attributes(struct vfs_handle_struct *handle,
struct files_struct *fsp,
uint32_t *dosmode)
{
struct ixnas_config_data *config = NULL;
int i;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct ixnas_config_data,
return NT_STATUS_INTERNAL_ERROR);
if (config->dosattrib_xattr) {
return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle,
fsp,
dosmode);
}
for (i = 0; i < ARRAY_SIZE(dosmode2flag); i++) {
if (fsp->fsp_name->st.st_ex_flags & dosmode2flag[i].flag) {
*dosmode |= dosmode2flag[i].dosmode;
}
}
if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
/*
* Windows default behavior appears to be that the archive bit
* on a directory is only explicitly set by clients. FreeBSD
* sets this bit when the directory's contents are modified.
* This is a temporary hack until we can make OS behavior
* configurable
*/
*dosmode &= ~FILE_ATTRIBUTE_ARCHIVE;
}
return NT_STATUS_OK;
}
static NTSTATUS ixnas_fset_dos_attributes(struct vfs_handle_struct *handle,
struct files_struct *fsp,
uint32_t dosmode)
{
NTSTATUS status;
struct ixnas_config_data *config = NULL;
uint32_t flags = 0;
int ret, i;
bool set_dosmode_ok = false;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct ixnas_config_data,
return NT_STATUS_INTERNAL_ERROR);
if (config->dosattrib_xattr) {
return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle,
fsp,
dosmode);
}
DBG_INFO("ixnas:set_dos_attributes: set attribute 0x%x, on file %s\n",
dosmode, fsp_str_dbg(fsp));
/*
* Optimization. This is most likely set by file owner. First try without
* performing additional permissions checks and using become_root().
*/
for (i = 0; i < ARRAY_SIZE(dosmode2flag); i++) {
if (dosmode & dosmode2flag[i].dosmode) {
flags |= dosmode2flag[i].flag;
}
}
ret = SMB_VFS_FCHFLAGS(fsp, flags);
if ((ret == -1) && (errno != EPERM)) {
DBG_WARNING("Setting dosmode failed for %s: %s\n",
fsp_str_dbg(fsp), strerror(errno));
return map_nt_error_from_unix(errno);
}
if (!CAN_WRITE(handle->conn)) {
return NT_STATUS_ACCESS_DENIED;
}
status = smbd_check_access_rights_fsp(handle->conn->cwd_fsp, fsp,
false, FILE_WRITE_ATTRIBUTES);
if (NT_STATUS_IS_OK(status)) {
set_dosmode_ok = true;
}
if (!set_dosmode_ok && lp_dos_filemode(SNUM(handle->conn))) {
set_dosmode_ok = can_write_to_fsp(fsp);
}
if (!set_dosmode_ok){
return NT_STATUS_ACCESS_DENIED;
}
/* becomeroot() because non-owners need to write flags */
become_root();
ret = SMB_VFS_FCHFLAGS(fsp, flags);
unbecome_root();
if (ret == -1) {
DBG_WARNING("Setting dosmode failed for %s: %s\n",
fsp_str_dbg(fsp), strerror(errno));
return map_nt_error_from_unix(errno);
}
return NT_STATUS_OK;
}
static acl_t fsp_get_bsdacl(files_struct *fsp)
{
int fd;
const char *proc_fd_path = NULL;
char buf[PATH_MAX];
if (!fsp->fsp_flags.is_pathref) {
return acl_get_fd_np(fsp_get_io_fd(fsp), ACL_TYPE_NFS4);
}
SMB_ASSERT(fsp->fsp_flags.have_proc_fds);
fd = fsp_get_pathref_fd(fsp);
proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
if (proc_fd_path == NULL) {
return NULL;
}
return acl_get_file(proc_fd_path, ACL_TYPE_NFS4);
}
static int fsp_set_bsdacl(files_struct *fsp, acl_t bsdacl)
{
int fd;
const char *proc_fd_path = NULL;
char buf[PATH_MAX];
if (!fsp->fsp_flags.is_pathref) {
return acl_set_fd_np(fsp_get_io_fd(fsp), bsdacl, ACL_TYPE_NFS4);
}
SMB_ASSERT(fsp->fsp_flags.have_proc_fds);
fd = fsp_get_pathref_fd(fsp);
proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
if (proc_fd_path == NULL) {
errno = EBADF;
return -1;
}
return acl_set_file(proc_fd_path, ACL_TYPE_NFS4, bsdacl);
}
static int fsp_get_acl_brand(files_struct *fsp)
{
long ret;
int saved_errno;
saved_errno = errno;
ret = fpathconf(fsp_get_pathref_fd(fsp), _PC_ACL_NFS4);
if (ret == -1) {
if (saved_errno == errno) {
return ACL_BRAND_POSIX;
}
DBG_ERR("%s: fpathconf failed: %s\n",
fsp_str_dbg(fsp), strerror(errno));
errno = saved_errno;
return ACL_BRAND_UNKNOWN;
}
return ACL_BRAND_NFS4;
}
static void bsdentry2smbace(acl_entry_t ae, SMB_ACE4PROP_T *aceprop)
{
int i;
for (i = 0; i < ARRAY_SIZE(bsdperm2nfs4perm); i++) {
if (ae->ae_perm & bsdperm2nfs4perm[i].bsdperm) {
aceprop->aceMask |= bsdperm2nfs4perm[i].nfs4perm;
}
}
for (i = 0; i < ARRAY_SIZE(bsdflag2nfs4flag); i++) {
if (ae->ae_flags & bsdflag2nfs4flag[i].bsdflag) {
aceprop->aceFlags |= bsdflag2nfs4flag[i].nfs4flag;
}
}
if (ae->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) {
aceprop->aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
aceprop->aceMask |= SMB_ACE4_SYNCHRONIZE;
} else if (ae->ae_entry_type == ACL_ENTRY_TYPE_DENY) {
aceprop->aceType = SMB_ACE4_ACCESS_DENIED_ACE_TYPE;
} else {
smb_panic("Unsupported ace type.");
}
switch(ae->ae_tag) {
case ACL_USER_OBJ:
aceprop->flags = SMB_ACE4_ID_SPECIAL;
aceprop->who.special_id = SMB_ACE4_WHO_OWNER;
break;
case ACL_GROUP_OBJ:
aceprop->flags = SMB_ACE4_ID_SPECIAL;
aceprop->who.special_id = SMB_ACE4_WHO_GROUP;
break;
case ACL_EVERYONE:
aceprop->flags = SMB_ACE4_ID_SPECIAL;
aceprop->who.special_id = SMB_ACE4_WHO_EVERYONE;
break;
case ACL_GROUP:
aceprop->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
case ACL_USER:
aceprop->who.id = ae->ae_id;
aceprop->flags = 0;
break;
default:
smb_panic("Unsupported ace tag");
}
if ((aceprop->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) &&
(aceprop->flags) && (aceprop->aceMask & SMB_ACE4_WRITE_DATA)) {
aceprop->aceMask |= (SMB_ACE4_WRITE_NAMED_ATTRS | \
SMB_ACE4_WRITE_ATTRIBUTES | \
SMB_ACE4_DELETE);
}
}
static bool smbace2bsdentry(acl_t bsdacl, SMB_ACE4PROP_T *aceprop)
{
acl_entry_t new_entry;
acl_perm_t permset = 0;
acl_entry_type_t type = 0;
acl_flag_t flags = 0;
uid_t id = ACL_UNDEFINED_ID;
acl_tag_t tag;
int i;
for (i = 0; i < ARRAY_SIZE(bsdperm2nfs4perm); i++) {
if (aceprop->aceMask & bsdperm2nfs4perm[i].nfs4perm) {
permset |= bsdperm2nfs4perm[i].bsdperm;
}
}
for (i = 0; i < ARRAY_SIZE(bsdflag2nfs4flag); i++) {
if (aceprop->aceFlags & bsdflag2nfs4flag[i].nfs4flag) {
flags |= bsdflag2nfs4flag[i].bsdflag;
}
}
if (acl_create_entry(&bsdacl, &new_entry) < 0) {
DBG_ERR("Failed to create new ACL entry: %s\n", strerror(errno));
return false;
}
if (aceprop->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
type = ACL_ENTRY_TYPE_ALLOW;
} else if (aceprop->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
type = ACL_ENTRY_TYPE_DENY;
} else {
smb_panic("Unsupported ace type.");
}
if (aceprop->flags & SMB_ACE4_ID_SPECIAL) {
switch(aceprop->who.special_id) {
case SMB_ACE4_WHO_EVERYONE:
tag = ACL_EVERYONE;
break;
case SMB_ACE4_WHO_OWNER:
tag = ACL_USER_OBJ;
break;
case SMB_ACE4_WHO_GROUP:
tag = ACL_GROUP_OBJ;
break;
default:
smb_panic("Unsupported special id.");
}
} else {
tag = aceprop->aceFlags & SMB_ACE4_IDENTIFIER_GROUP ?
ACL_GROUP : ACL_USER;
id = aceprop->who.id;
}
new_entry->ae_perm = permset;
new_entry->ae_flags = flags;
new_entry->ae_entry_type = type;
new_entry->ae_tag = tag;
new_entry->ae_id = id;
return true;
}
static NTSTATUS ixnas_get_nt_acl_nfs4_common(struct connection_struct *conn,
TALLOC_CTX *mem_ctx,
files_struct *fsp,
acl_t bsdacl,
struct SMB4ACL_T **ppacl,
struct ixnas_config_data *config)
{
int cnt, ret;
struct SMB4ACL_T *pacl = NULL;
bool inherited_is_present = false;
bool is_dir;
mem_ctx = talloc_tos();
/* create SMB4ACL data */
pacl = smb_create_smb4acl(mem_ctx);
if (pacl == NULL) {
return NT_STATUS_NO_MEMORY;
}
for (cnt=0; cnt<bsdacl->ats_acl.acl_cnt; cnt++) {
SMB_ACE4PROP_T aceprop;
ZERO_STRUCT(aceprop);
bsdentry2smbace(&bsdacl->ats_acl.acl_entry[cnt], &aceprop);
if ((aceprop.aceMask == 0) &&
(aceprop.flags == SMB_ACE4_ID_SPECIAL) &&
(aceprop.who.special_id == SMB_ACE4_WHO_EVERYONE)) {
continue;
}
if (aceprop.aceFlags & SMB_ACE4_INHERITED_ACE) {
inherited_is_present = true;
}
if (smb_add_ace4(pacl, &aceprop) == NULL) {
return NT_STATUS_NO_MEMORY;
}
}
if (!inherited_is_present) {
DBG_DEBUG("Setting SEC_DESC_DACL_PROTECTED on [%s]\n",
fsp_str_dbg(fsp));
smbacl4_set_controlflags(pacl,
SEC_DESC_DACL_PROTECTED |
SEC_DESC_SELF_RELATIVE);
}
*ppacl = pacl;
return NT_STATUS_OK;
}
static NTSTATUS ixnas_fget_nt_acl(struct vfs_handle_struct *handle,
struct files_struct *fsp,
uint32_t security_info,
TALLOC_CTX *mem_ctx,
struct security_descriptor **ppdesc)
{
struct SMB4ACL_T *pacl = NULL;
TALLOC_CTX *frame = NULL;
struct files_struct *to_check = NULL;
NTSTATUS status;
acl_t bsdacl;
struct ixnas_config_data *config = NULL;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct ixnas_config_data,
return NT_STATUS_INTERNAL_ERROR);
if (!config->zfs_acl_enabled) {
return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info, mem_ctx, ppdesc);
}
to_check = fsp->base_fsp ? fsp->base_fsp : fsp;
bsdacl = fsp_get_bsdacl(to_check);
if (bsdacl == NULL) {
if ((errno == EINVAL) &&
(fsp_get_acl_brand(fsp) == ACL_BRAND_POSIX)) {
status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info, mem_ctx, ppdesc);
if (NT_STATUS_IS_OK(status)) {
(*ppdesc)->type |= SEC_DESC_DACL_PROTECTED;
}
return status;
} else {
return map_nt_error_from_unix(errno);
}
}
frame = talloc_stackframe();
status = ixnas_get_nt_acl_nfs4_common(handle->conn,
frame,
fsp,
bsdacl,
&pacl,
config);
acl_free(bsdacl);
if (!NT_STATUS_IS_OK(status)) {
TALLOC_FREE(frame);
return status;
}
status = smb_fget_nt_acl_nfs4(fsp, NULL, security_info, mem_ctx,
ppdesc, pacl);
TALLOC_FREE(frame);
return status;
}
static bool ixnas_process_smbacl(vfs_handle_struct *handle,
files_struct *fsp,
struct SMB4ACL_T *smbacl)
{
acl_t bsdacl;
struct SMB4ACE_T *smbace = NULL;
bool has_inheritable = false;
struct ixnas_config_data *config = NULL;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct ixnas_config_data,
return False);
bsdacl = acl_init(ACL_MAX_ENTRIES);
if (bsdacl == NULL) {
DBG_ERR("%s: acl_init failed: %s\n",
fsp_str_dbg(fsp), strerror(errno));
return false;
}
for (smbace=smb_first_ace4(smbacl);
smbace!=NULL;
smbace = smb_next_ace4(smbace)) {
bool ok;
SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
ok = smbace2bsdentry(bsdacl, aceprop);
if (!ok) {
DBG_ERR("%s: failed to convert ACL entry\n",
fsp_str_dbg(fsp));
acl_free(bsdacl);
return false;
}
if ((aceprop->aceFlags & ~(SMB_ACE4_IDENTIFIER_GROUP|SMB_ACE4_INHERITED_ACE)) != 0) {
has_inheritable = true;
}
}
if (bsdacl->ats_acl.acl_cnt == 0 || has_inheritable) {
int rv;
acl_entry_t hidden_entry;
rv = acl_create_entry(&bsdacl, &hidden_entry);
if (rv == -1) {
DBG_ERR("%s: acl_create_entry() failed: %s\n",
fsp_str_dbg(fsp), strerror(errno));
acl_free(bsdacl);
return false;
}
if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
hidden_entry->ae_flags = ACL_ENTRY_DIRECTORY_INHERIT|ACL_ENTRY_FILE_INHERIT;
}
hidden_entry->ae_perm = 0;
hidden_entry->ae_entry_type = ACL_ENTRY_TYPE_ALLOW;
hidden_entry->ae_flags = ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT;
hidden_entry->ae_tag = ACL_EVERYONE;
hidden_entry->ae_id = ACL_UNDEFINED_ID;
}
if (fsp_set_bsdacl(fsp, bsdacl)) {
DBG_ERR("%s: failed to set acl: %s\n",
fsp_str_dbg(fsp), strerror(errno));
acl_free(bsdacl);
return false;
}
acl_free(bsdacl);
return True;
}
static NTSTATUS ixnas_fset_nt_acl(vfs_handle_struct *handle,
files_struct *fsp,
uint32_t security_info_sent,
const struct security_descriptor *psd)
{
struct ixnas_config_data *config = NULL;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct ixnas_config_data,
return NT_STATUS_INTERNAL_ERROR);
return smb_set_nt_acl_nfs4(handle,
fsp,
&config->nfs4_params,
security_info_sent,
psd,
ixnas_process_smbacl);
}
/*
* Functions below are related to posix1e ACLs. Logic copied from vfs_zfsacl.
*/
static SMB_ACL_T ixnas_fail__sys_acl_get_fd(vfs_handle_struct *handle,
files_struct *fsp,
SMB_ACL_TYPE_T type,
TALLOC_CTX *mem_ctx)
{
return (SMB_ACL_T)NULL;
}
static int ixnas_fail__sys_acl_set_fd(vfs_handle_struct *handle,
files_struct *fsp,
SMB_ACL_TYPE_T type,
SMB_ACL_T theacl)
{
return -1;
}
static int ixnas_fail__sys_acl_blob_get_fd(vfs_handle_struct *handle,
files_struct *fsp,
TALLOC_CTX *mem_ctx,
char **blob_description,
DATA_BLOB *blob)
{
return -1;
}
/********************************************************************
Convert chmod() requests into an appropriate non-inheriting ACL
entry. We don't rely on FreeBSD kernel behavior in this case,
because it strips some bits that we actually care about
(WRITE_ATTRIBUTES, DELETE, etc.). If DELETE is stripped, then
users will no longer be able to rename files.
********************************************************************/
static int mode_to_acl(acl_t *new_acl, mode_t mode)
{
int res = 0;
mode_t shifted_mode, other_mode, deny_mode;
acl_entry_t o_allow_entry = NULL;
acl_entry_t g_allow_entry = NULL;
acl_entry_t e_allow_entry = NULL;
acl_entry_t o_deny_entry = NULL;
acl_entry_t g_deny_entry = NULL;
acl_permset_t permset;
acl_flagset_t flagset;
/*
* convert posix mode bits to ACLs
*/
if (((mode & S_IRWXU) >> 6) < (mode & S_IRWXO)) {
shifted_mode = (mode &= S_IRWXU) >> 6;
other_mode &= S_IRWXO;
deny_mode = (shifted_mode ^ other_mode) << 6;
res = acl_create_entry(new_acl, &o_deny_entry);
if (res != 0) {
return -1;
}
acl_get_permset(o_deny_entry, &permset);
if (deny_mode & S_IRUSR) {
*permset = ACL_READ_DATA;
}
if (deny_mode & S_IWUSR) {
*permset |= ACL_WRITE_DATA;
}
if (deny_mode & S_IXUSR) {
*permset |= ACL_EXECUTE;
}
acl_get_flagset_np(o_deny_entry, &flagset);
*flagset = 0;
acl_set_entry_type_np(o_deny_entry, ACL_ENTRY_TYPE_DENY);
acl_set_tag_type(o_deny_entry, ACL_USER_OBJ);
}
if (((mode & S_IRWXG) >> 3) < (mode & S_IRWXO)) {
shifted_mode = (mode &= S_IRWXG) >> 3;
other_mode &= S_IRWXG;
deny_mode = (shifted_mode ^ other_mode) << 3;
res = acl_create_entry(new_acl, &g_deny_entry);
if (res != 0) {
return -1;
}
acl_get_permset(g_deny_entry, &permset);
if (deny_mode & S_IRGRP) {
*permset = ACL_READ_DATA;
}
if (deny_mode & S_IWGRP) {
*permset |= ACL_WRITE_DATA;
}
if (deny_mode & S_IXGRP) {
*permset |= ACL_EXECUTE;
}
acl_get_flagset_np(g_deny_entry, &flagset);
*flagset = 0;
acl_set_entry_type_np(g_deny_entry, ACL_ENTRY_TYPE_DENY);
acl_set_tag_type(g_deny_entry, ACL_GROUP_OBJ);
}
if (mode & S_IRWXU) {
res = acl_create_entry(new_acl, &o_allow_entry);
if (res != 0) {
return -1;
}
acl_get_permset(o_allow_entry, &permset);
if (mode & S_IRUSR) {
*permset = ACL_READ_SET;
}
if (mode & S_IWUSR) {
*permset |= ACL_WRITE_SET;
*permset |= ACL_DELETE;
}
if (mode & S_IXUSR) {
*permset |= ACL_EXECUTE;
}
acl_get_flagset_np(o_allow_entry, &flagset);
*flagset = 0;
acl_set_entry_type_np(o_allow_entry, ACL_ENTRY_TYPE_ALLOW);
acl_set_tag_type(o_allow_entry, ACL_USER_OBJ);
}
if (mode & S_IRWXG) {
res = acl_create_entry(new_acl, &g_allow_entry);
if (res != 0) {
return -1;
}
acl_get_permset(g_allow_entry, &permset);
if (mode & S_IRGRP) {
*permset = ACL_READ_SET;
}
if (mode & S_IWGRP) {
*permset |= ACL_WRITE_SET;
*permset |= ACL_DELETE;
}
if (mode & S_IXGRP) {
*permset |= ACL_EXECUTE;
}
acl_get_flagset_np(g_allow_entry, &flagset);
*flagset = 0;
acl_set_entry_type_np(g_allow_entry, ACL_ENTRY_TYPE_ALLOW);
acl_set_tag_type(g_allow_entry, ACL_GROUP_OBJ);
}
if (mode & S_IRWXO) {
res = acl_create_entry(new_acl, &e_allow_entry);
if (res != 0) {
return -1;
}
acl_get_permset(e_allow_entry, &permset);
if (mode & S_IROTH) {
*permset = ACL_READ_SET;
}
if (mode & S_IWOTH) {
*permset |= ACL_WRITE_SET;
*permset |= ACL_DELETE;
}
if (mode & S_IXOTH) {
*permset |= ACL_EXECUTE;
}
acl_get_flagset_np(e_allow_entry, &flagset);
*flagset = 0;
acl_set_entry_type_np(e_allow_entry, ACL_ENTRY_TYPE_ALLOW);
acl_set_tag_type(e_allow_entry, ACL_EVERYONE);
}
return 0;
}
static int recalculate_flagset(acl_flagset_t flagset)
{
/* Simply replace non-inheriting entries */
if ((*flagset & (ACL_ENTRY_DIRECTORY_INHERIT \
| ACL_ENTRY_FILE_INHERIT)) == 0){
return -1;
}
/*
* This edge case is not easily handled. It is
* unclear what user expectation should be. FreeBSD
* kernel changes to fdin, but this causes the ACL
* to inherit one deeper than it should. I think safe
* play here is to maintain the inheritance flags
* as-is and end up with wonky mode so as not to
* break expectations regarding inheritance.
*/
if (((*flagset & ACL_ENTRY_INHERIT_ONLY) == 0) &&
(*flagset & ACL_ENTRY_NO_PROPAGATE_INHERIT)) {
return 0;
}
*flagset |= ACL_ENTRY_INHERIT_ONLY;
return 0;
}
static acl_t calculate_chmod_acl(acl_t source_acl,
mode_t mode)
{
int res = 0;
acl_t new_acl, tmp_acl;
int entry_id = ACL_FIRST_ENTRY;
acl_entry_t entry, new_entry;
acl_tag_t tag = 0;
acl_flagset_t flagset;
new_acl = acl_init(ACL_MAX_ENTRIES);
tmp_acl = acl_dup(source_acl);
res = mode_to_acl(&new_acl, mode);
if (res != 0) {
DBG_ERR("Failed to convert mode to ACL\n");
goto failure;
}
/*
* Iterate through ACL, remove non-inheriting special entries.
* Append INHERIT_ONLY to inheritng special entries
*/
while (acl_get_entry(tmp_acl, entry_id, &entry) == 1) {
entry_id = ACL_NEXT_ENTRY;
res = acl_get_tag_type(entry, &tag);
if (res != 0) {
DBG_ERR("acl_get_permset() failed.\n");
return NULL;
}
switch (tag) {
case ACL_USER_OBJ:
case ACL_GROUP_OBJ:
case ACL_EVERYONE:
res = acl_get_flagset_np(entry, &flagset);
if (res != 0) {
DBG_ERR("acl_get_flagset failed\n");
return NULL;
}
res = recalculate_flagset(flagset);
if (res != 0) {
continue;
}
res = acl_create_entry(&new_acl, &new_entry);
if (res != 0) {
DBG_ERR("acl_create_entry failed\n");
goto failure;
}
res = acl_copy_entry(new_entry, entry);
if (res != 0) {
DBG_ERR("acl_copy_entry failed\n");
goto failure;
}
break;
default:
res = acl_create_entry(&new_acl, &new_entry);
if (res != 0) {
DBG_ERR("acl_create_entry failed\n");
goto failure;
}
res = acl_copy_entry(new_entry, entry);
if (res != 0) {
DBG_ERR("acl_copy_entry failed\n");
goto failure;
}
break;
}
}
acl_free(tmp_acl);
return new_acl;
failure:
acl_free(tmp_acl);
acl_free(new_acl);
return NULL;
}
static int ixnas_fchmod(vfs_handle_struct *handle,
files_struct *fsp, mode_t mode)
{
int result;
acl_t zacl, new_acl;
int trivial = 0;
struct ixnas_config_data *config = NULL;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct ixnas_config_data,
return -1);
if (!config->zfs_acl_chmod_enabled) {
return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
}
zacl = fsp_get_bsdacl(fsp);
if (zacl == NULL) {
DBG_ERR("ixnas: acl_get_fd() failed for %s: %s\n",
fsp_str_dbg(fsp), strerror(errno));
return -1;
}
result = acl_is_trivial_np(zacl, &trivial);
if (result !=0) {
DBG_ERR("acl_is_trivial_np() failed\n");
goto failure;
}
/*
* A "trivial" ACL can be expressed as a POSIX mode without
* losing information. In this case, pass on to normal
* chmod() behavior because user is probably not concerned
* about ACLs.
*/
if (trivial) {
DBG_INFO("Trivial ACL detected on file %s, "
"passing to next CHMOD function\n",
fsp_str_dbg(fsp));
acl_free(zacl);
result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
return result;
}
new_acl = calculate_chmod_acl(zacl, mode);
if (new_acl == NULL) {
DBG_ERR("Failed to generate new ACL for %s",
fsp_str_dbg(fsp));
goto failure;
}
result = fsp_set_bsdacl(fsp, new_acl);
if (result != 0) {
DBG_ERR("Failed to set new ACL on %s: %s\n",
fsp_str_dbg(fsp), strerror(errno));
}
acl_free(zacl);
acl_free(new_acl);
return result;
failure:
acl_free(zacl);
return -1;
}
/*
* Windows clients return NT_STATUS_OBJECT_NAME_COLLISION in case of
* rename in case of rename in case insensitive dataset. MacOS does
* attempts the rename. rename() in FreeBSD in this returns success, but
* does not actually rename the file. Add new logic to rename(). If
* a case_insensitive string comparison of the filenames returns 0, then
* perform two renames so that the returned filename matches client
* expectations. First rename appends a unique id generated from
* inode and generation number to the file's name. This makes the
* rename deterministic, while minimizing risk of name collisions.
*/
static uint64_t ixnas_fs_file_id(struct vfs_handle_struct *handle,
const SMB_STRUCT_STAT *psbuf);
static int ixnas_renameat(vfs_handle_struct *handle,
files_struct *srcfsp,
const struct smb_filename *smb_fname_src,
files_struct *dstfsp,
const struct smb_filename *smb_fname_dst)
{
int result = 1;
struct ixnas_config_data *config = NULL;
char *tmp_base_name = NULL;
uint64_t srcid, dstid;
SMB_VFS_HANDLE_GET_DATA(handle, config,
struct ixnas_config_data,
return -1);
if (config->props->casesens != SMBZFS_INSENSITIVE) {
return SMB_VFS_NEXT_RENAMEAT(handle,
srcfsp,
smb_fname_src,
dstfsp,
smb_fname_dst);
}
srcid = ixnas_fs_file_id(handle, &srcfsp->fsp_name->st);
dstid = ixnas_fs_file_id(handle, &dstfsp->fsp_name->st);
if (srcid == dstid) {
result = strcasecmp_m(smb_fname_src->base_name,
smb_fname_dst->base_name);
}
if (result != 0) {
return SMB_VFS_NEXT_RENAMEAT(handle,
srcfsp,
smb_fname_src,
dstfsp,
smb_fname_dst);
}
dstid = ixnas_fs_file_id(handle, &smb_fname_src->st);
tmp_base_name = talloc_asprintf(talloc_tos(), "%s_%lu",
smb_fname_src->base_name, dstid);
if (tmp_base_name == NULL) {
errno = ENOMEM;
return -1;
}
result = renameat(
fsp_get_pathref_fd(srcfsp), smb_fname_src->base_name,
fsp_get_pathref_fd(dstfsp), tmp_base_name
);
if (result != 0) {
DBG_ERR("Failed to rename %s to intermediate name %s\n",
smb_fname_src->base_name, tmp_base_name);
TALLOC_FREE(tmp_base_name);
return result;
}
result = renameat(
fsp_get_pathref_fd(dstfsp), tmp_base_name,
fsp_get_pathref_fd(srcfsp), smb_fname_dst->base_name
);
TALLOC_FREE(tmp_base_name);
return result;
}
static struct file_id ixnas_file_id_create(struct vfs_handle_struct *handle,
const SMB_STRUCT_STAT *sbuf)
{
struct file_id key = (struct file_id) {
.devid = sbuf->st_ex_dev,
.inode = sbuf->st_ex_ino,
.extid = sbuf->st_ex_gen,
};
return key;
}