forked from RsyncProject/rsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysacls.c
2796 lines (2258 loc) · 65.2 KB
/
sysacls.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.
* Based on the Samba ACL support code.
* Copyright (C) Jeremy Allison 2000.
* Copyright (C) 2007-2020 Wayne Davison
*
* The permission functions have been changed to get/set all bits via
* one call. Some functions that rsync doesn't need were also removed.
*
* 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
* with this program; if not, visit the http://fsf.org website.
*/
#include "rsync.h"
#include "sysacls.h"
#ifdef SUPPORT_ACLS
#ifdef DEBUG
#undef DEBUG
#endif
#define DEBUG(x,y)
void SAFE_FREE(void *mem)
{
if (mem)
free(mem);
}
/*
This file wraps all differing system ACL interfaces into a consistent
one based on the POSIX interface. It also returns the correct errors
for older UNIX systems that don't support ACLs.
The interfaces that each ACL implementation must support are as follows :
int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
int sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p)
SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
SMB_ACL_T sys_acl_get_fd(int fd)
SMB_ACL_T sys_acl_init( int count)
int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
int sys_acl_set_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tag_type, uint32 bits, id_t u_g_id)
int sys_acl_set_access_bits(SMB_ACL_ENTRY_T entry, uint32 bits)
int sys_acl_valid( SMB_ACL_T theacl )
int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
int sys_acl_delete_def_file(const char *path)
int sys_acl_free_acl(SMB_ACL_T posix_acl)
*/
#if defined(HAVE_POSIX_ACLS) /*--------------------------------------------*/
/* Identity mapping - easy. */
int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
{
return acl_get_entry( the_acl, entry_id, entry_p);
}
int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
{
return acl_get_tag_type( entry_d, tag_type_p);
}
SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
{
return acl_get_file( path_p, type);
}
#if 0
SMB_ACL_T sys_acl_get_fd(int fd)
{
return acl_get_fd(fd);
}
#endif
#if defined(HAVE_ACL_GET_PERM_NP)
#define acl_get_perm(p, b) acl_get_perm_np(p, b)
#endif
int sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p)
{
acl_permset_t permset;
if (acl_get_tag_type(entry, tag_type_p) != 0
|| acl_get_permset(entry, &permset) != 0)
return -1;
*bits_p = (acl_get_perm(permset, ACL_READ) ? 4 : 0)
| (acl_get_perm(permset, ACL_WRITE) ? 2 : 0)
| (acl_get_perm(permset, ACL_EXECUTE) ? 1 : 0);
if (*tag_type_p == SMB_ACL_USER || *tag_type_p == SMB_ACL_GROUP) {
void *qual;
if ((qual = acl_get_qualifier(entry)) == NULL)
return -1;
*u_g_id_p = *(id_t*)qual;
acl_free(qual);
}
return 0;
}
SMB_ACL_T sys_acl_init( int count)
{
return acl_init(count);
}
int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
{
return acl_create_entry(pacl, pentry);
}
int sys_acl_set_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tag_type, uint32 bits, id_t u_g_id)
{
if (acl_set_tag_type(entry, tag_type) != 0)
return -1;
if (tag_type == SMB_ACL_USER || tag_type == SMB_ACL_GROUP) {
if (acl_set_qualifier(entry, (void*)&u_g_id) != 0)
return -1;
}
return sys_acl_set_access_bits(entry, bits);
}
int sys_acl_set_access_bits(SMB_ACL_ENTRY_T entry, uint32 bits)
{
acl_permset_t permset;
int rc;
if ((rc = acl_get_permset(entry, &permset)) != 0)
return rc;
acl_clear_perms(permset);
if (bits & 4)
acl_add_perm(permset, ACL_READ);
if (bits & 2)
acl_add_perm(permset, ACL_WRITE);
if (bits & 1)
acl_add_perm(permset, ACL_EXECUTE);
return acl_set_permset(entry, permset);
}
int sys_acl_valid( SMB_ACL_T theacl )
{
return acl_valid(theacl);
}
int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
{
return acl_set_file(name, acltype, theacl);
}
#if 0
int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
{
return acl_set_fd(fd, theacl);
}
#endif
int sys_acl_delete_def_file(const char *name)
{
return acl_delete_def_file(name);
}
int sys_acl_free_acl(SMB_ACL_T the_acl)
{
return acl_free(the_acl);
}
#elif defined(HAVE_TRU64_ACLS) /*--------------------------------------------*/
/*
* The interface to DEC/Compaq Tru64 UNIX ACLs
* is based on Draft 13 of the POSIX spec which is
* slightly different from the Draft 16 interface.
*
* Also, some of the permset manipulation functions
* such as acl_clear_perm() and acl_add_perm() appear
* to be broken on Tru64 so we have to manipulate
* the permission bits in the permset directly.
*/
int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
{
SMB_ACL_ENTRY_T entry;
if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
return -1;
}
errno = 0;
if ((entry = acl_get_entry(the_acl)) != NULL) {
*entry_p = entry;
return 1;
}
return errno ? -1 : 0;
}
int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
{
return acl_get_tag_type( entry_d, tag_type_p);
}
SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
{
return acl_get_file((char *)path_p, type);
}
#if 0
SMB_ACL_T sys_acl_get_fd(int fd)
{
return acl_get_fd(fd, ACL_TYPE_ACCESS);
}
#endif
int sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p)
{
acl_permset_t permset;
if (acl_get_tag_type(entry, tag_type_p) != 0
|| acl_get_permset(entry, &permset) != 0)
return -1;
*bits_p = *permset & 7; /* Tru64 doesn't have acl_get_perm() */
if (*tag_type_p == SMB_ACL_USER || *tag_type_p == SMB_ACL_GROUP) {
void *qual;
if ((qual = acl_get_qualifier(entry)) == NULL)
return -1;
*u_g_id_p = *(id_t*)qual;
acl_free_qualifier(qual, *tag_type_p);
}
return 0;
}
SMB_ACL_T sys_acl_init( int count)
{
return acl_init(count);
}
int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
{
SMB_ACL_ENTRY_T entry;
if ((entry = acl_create_entry(pacl)) == NULL) {
return -1;
}
*pentry = entry;
return 0;
}
int sys_acl_set_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tag_type, uint32 bits, id_t u_g_id)
{
if (acl_set_tag_type(entry, tag_type) != 0)
return -1;
if (tag_type == SMB_ACL_USER || tag_type == SMB_ACL_GROUP) {
if (acl_set_qualifier(entry, (void*)&u_g_id) != 0)
return -1;
}
return sys_acl_set_access_bits(entry, bits);
}
int sys_acl_set_access_bits(SMB_ACL_ENTRY_T entry, uint32 bits)
{
acl_permset_t permset;
int rc;
if ((rc = acl_get_permset(entry, &permset)) != 0)
return rc;
*permset = bits & 7;
return acl_set_permset(entry, permset);
}
int sys_acl_valid( SMB_ACL_T theacl )
{
acl_entry_t entry;
return acl_valid(theacl, &entry);
}
int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
{
return acl_set_file((char *)name, acltype, theacl);
}
#if 0
int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
{
return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
}
#endif
int sys_acl_delete_def_file(const char *name)
{
return acl_delete_def_file((char *)name);
}
int sys_acl_free_acl(SMB_ACL_T the_acl)
{
return acl_free(the_acl);
}
#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS) /*-----------*/
/*
* Donated by Michael Davidson <[email protected]> for UnixWare / OpenUNIX.
* Modified by Toomas Soome <[email protected]> for Solaris.
*/
/*
* Note that while this code implements sufficient functionality
* to support the sys_acl_* interfaces it does not provide all
* of the semantics of the POSIX ACL interfaces.
*
* In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
* from a call to sys_acl_get_entry() should not be assumed to be
* valid after calling any of the following functions, which may
* reorder the entries in the ACL.
*
* sys_acl_valid()
* sys_acl_set_file()
* sys_acl_set_fd()
*/
/*
* The only difference between Solaris and UnixWare / OpenUNIX is
* that the #defines for the ACL operations have different names
*/
#if defined(HAVE_UNIXWARE_ACLS)
#define SETACL ACL_SET
#define GETACL ACL_GET
#define GETACLCNT ACL_CNT
#endif
int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
{
if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
errno = EINVAL;
return -1;
}
if (entry_p == NULL) {
errno = EINVAL;
return -1;
}
if (entry_id == SMB_ACL_FIRST_ENTRY) {
acl_d->next = 0;
}
if (acl_d->next < 0) {
errno = EINVAL;
return -1;
}
if (acl_d->next >= acl_d->count) {
return 0;
}
*entry_p = &acl_d->acl[acl_d->next++];
return 1;
}
int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
{
*type_p = entry_d->a_type;
return 0;
}
/*
* There is no way of knowing what size the ACL returned by
* GETACL will be unless you first call GETACLCNT which means
* making an additional system call.
*
* In the hope of avoiding the cost of the additional system
* call in most cases, we initially allocate enough space for
* an ACL with INITIAL_ACL_SIZE entries. If this turns out to
* be too small then we use GETACLCNT to find out the actual
* size, reallocate the ACL buffer, and then call GETACL again.
*/
#define INITIAL_ACL_SIZE 16
SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
{
SMB_ACL_T acl_d;
int count; /* # of ACL entries allocated */
int naccess; /* # of access ACL entries */
int ndefault; /* # of default ACL entries */
if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
errno = EINVAL;
return NULL;
}
count = INITIAL_ACL_SIZE;
if ((acl_d = sys_acl_init(count)) == NULL) {
return NULL;
}
/*
* If there isn't enough space for the ACL entries we use
* GETACLCNT to determine the actual number of ACL entries
* reallocate and try again. This is in a loop because it
* is possible that someone else could modify the ACL and
* increase the number of entries between the call to
* GETACLCNT and the call to GETACL.
*/
while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
&& errno == ENOSPC) {
sys_acl_free_acl(acl_d);
if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
return NULL;
}
if ((acl_d = sys_acl_init(count)) == NULL) {
return NULL;
}
}
if (count < 0) {
sys_acl_free_acl(acl_d);
return NULL;
}
/*
* calculate the number of access and default ACL entries
*
* Note: we assume that the acl() system call returned a
* well formed ACL which is sorted so that all of the
* access ACL entries precede any default ACL entries
*/
for (naccess = 0; naccess < count; naccess++) {
if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
break;
}
ndefault = count - naccess;
/*
* if the caller wants the default ACL we have to copy
* the entries down to the start of the acl[] buffer
* and mask out the ACL_DEFAULT flag from the type field
*/
if (type == SMB_ACL_TYPE_DEFAULT) {
int i, j;
for (i = 0, j = naccess; i < ndefault; i++, j++) {
acl_d->acl[i] = acl_d->acl[j];
acl_d->acl[i].a_type &= ~ACL_DEFAULT;
}
acl_d->count = ndefault;
} else {
acl_d->count = naccess;
}
return acl_d;
}
#if 0
SMB_ACL_T sys_acl_get_fd(int fd)
{
SMB_ACL_T acl_d;
int count; /* # of ACL entries allocated */
int naccess; /* # of access ACL entries */
count = INITIAL_ACL_SIZE;
if ((acl_d = sys_acl_init(count)) == NULL) {
return NULL;
}
while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
&& errno == ENOSPC) {
sys_acl_free_acl(acl_d);
if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
return NULL;
}
if ((acl_d = sys_acl_init(count)) == NULL) {
return NULL;
}
}
if (count < 0) {
sys_acl_free_acl(acl_d);
return NULL;
}
/*
* calculate the number of access ACL entries
*/
for (naccess = 0; naccess < count; naccess++) {
if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
break;
}
acl_d->count = naccess;
return acl_d;
}
#endif
int sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p)
{
*tag_type_p = entry->a_type;
*bits_p = entry->a_perm;
if (*tag_type_p == SMB_ACL_USER || *tag_type_p == SMB_ACL_GROUP)
*u_g_id_p = entry->a_id;
return 0;
}
SMB_ACL_T sys_acl_init(int count)
{
SMB_ACL_T a;
if (count < 0) {
errno = EINVAL;
return NULL;
}
/*
* note that since the definition of the structure pointed
* to by the SMB_ACL_T includes the first element of the
* acl[] array, this actually allocates an ACL with room
* for (count+1) entries
*/
if ((a = (SMB_ACL_T)SMB_MALLOC(sizeof a[0] + count * sizeof (struct acl))) == NULL) {
errno = ENOMEM;
return NULL;
}
a->size = count + 1;
a->count = 0;
a->next = -1;
return a;
}
int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
{
SMB_ACL_T acl_d;
SMB_ACL_ENTRY_T entry_d;
if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
errno = EINVAL;
return -1;
}
if (acl_d->count >= acl_d->size) {
errno = ENOSPC;
return -1;
}
entry_d = &acl_d->acl[acl_d->count++];
entry_d->a_type = 0;
entry_d->a_id = -1;
entry_d->a_perm = 0;
*entry_p = entry_d;
return 0;
}
int sys_acl_set_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tag_type, uint32 bits, id_t u_g_id)
{
entry->a_type = tag_type;
if (tag_type == SMB_ACL_USER || tag_type == SMB_ACL_GROUP)
entry->a_id = u_g_id;
entry->a_perm = bits;
return 0;
}
int sys_acl_set_access_bits(SMB_ACL_ENTRY_T entry_d, uint32 bits)
{
entry_d->a_perm = bits;
return 0;
}
/*
* sort the ACL and check it for validity
*
* if it's a minimal ACL with only 4 entries then we
* need to recalculate the mask permissions to make
* sure that they are the same as the GROUP_OBJ
* permissions as required by the UnixWare acl() system call.
*
* (note: since POSIX allows minimal ACLs which only contain
* 3 entries - ie there is no mask entry - we should, in theory,
* check for this and add a mask entry if necessary - however
* we "know" that the caller of this interface always specifies
* a mask so, in practice "this never happens" (tm) - if it *does*
* happen aclsort() will fail and return an error and someone will
* have to fix it ...)
*/
static int acl_sort(SMB_ACL_T acl_d)
{
int fixmask = (acl_d->count <= 4);
if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
errno = EINVAL;
return -1;
}
return 0;
}
int sys_acl_valid(SMB_ACL_T acl_d)
{
return acl_sort(acl_d);
}
int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
{
struct stat s;
struct acl *acl_p;
int acl_count;
struct acl *acl_buf = NULL;
int ret;
if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
errno = EINVAL;
return -1;
}
if (acl_sort(acl_d) != 0) {
return -1;
}
acl_p = &acl_d->acl[0];
acl_count = acl_d->count;
/*
* if it's a directory there is extra work to do
* since the acl() system call will replace both
* the access ACLs and the default ACLs (if any)
*/
if (stat(name, &s) != 0) {
return -1;
}
if (S_ISDIR(s.st_mode)) {
SMB_ACL_T acc_acl;
SMB_ACL_T def_acl;
SMB_ACL_T tmp_acl;
int i;
if (type == SMB_ACL_TYPE_ACCESS) {
acc_acl = acl_d;
def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
} else {
def_acl = acl_d;
acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
}
if (tmp_acl == NULL) {
return -1;
}
/*
* allocate a temporary buffer for the complete ACL
*/
acl_count = acc_acl->count + def_acl->count;
acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
if (acl_buf == NULL) {
sys_acl_free_acl(tmp_acl);
errno = ENOMEM;
return -1;
}
/*
* copy the access control and default entries into the buffer
*/
memcpy(&acl_buf[0], &acc_acl->acl[0],
acc_acl->count * sizeof(acl_buf[0]));
memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
def_acl->count * sizeof(acl_buf[0]));
/*
* set the ACL_DEFAULT flag on the default entries
*/
for (i = acc_acl->count; i < acl_count; i++) {
acl_buf[i].a_type |= ACL_DEFAULT;
}
sys_acl_free_acl(tmp_acl);
} else if (type != SMB_ACL_TYPE_ACCESS) {
errno = EINVAL;
return -1;
}
ret = acl(name, SETACL, acl_count, acl_p);
SAFE_FREE(acl_buf);
return ret;
}
#if 0
int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
{
if (acl_sort(acl_d) != 0) {
return -1;
}
return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
}
#endif
int sys_acl_delete_def_file(const char *path)
{
SMB_ACL_T acl_d;
int ret;
/*
* fetching the access ACL and rewriting it has
* the effect of deleting the default ACL
*/
if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
return -1;
}
ret = acl(path, SETACL, acl_d->count, acl_d->acl);
sys_acl_free_acl(acl_d);
return ret;
}
int sys_acl_free_acl(SMB_ACL_T acl_d)
{
SAFE_FREE(acl_d);
return 0;
}
#elif defined(HAVE_HPUX_ACLS) /*---------------------------------------------*/
#include <dl.h>
/*
* Based on the Solaris/SCO code - with modifications.
*/
/*
* Note that while this code implements sufficient functionality
* to support the sys_acl_* interfaces it does not provide all
* of the semantics of the POSIX ACL interfaces.
*
* In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
* from a call to sys_acl_get_entry() should not be assumed to be
* valid after calling any of the following functions, which may
* reorder the entries in the ACL.
*
* sys_acl_valid()
* sys_acl_set_file()
* sys_acl_set_fd()
*/
/* This checks if the POSIX ACL system call is defined */
/* which basically corresponds to whether JFS 3.3 or */
/* higher is installed. If acl() was called when it */
/* isn't defined, it causes the process to core dump */
/* so it is important to check this and avoid acl() */
/* calls if it isn't there. */
static BOOL hpux_acl_call_presence(void)
{
shl_t handle = NULL;
void *value;
int ret_val=0;
static BOOL already_checked=0;
if(already_checked)
return True;
ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
if(ret_val != 0) {
DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
ret_val, errno, strerror(errno)));
DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
return False;
}
DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
already_checked = True;
return True;
}
int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
{
if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
errno = EINVAL;
return -1;
}
if (entry_p == NULL) {
errno = EINVAL;
return -1;
}
if (entry_id == SMB_ACL_FIRST_ENTRY) {
acl_d->next = 0;
}
if (acl_d->next < 0) {
errno = EINVAL;
return -1;
}
if (acl_d->next >= acl_d->count) {
return 0;
}
*entry_p = &acl_d->acl[acl_d->next++];
return 1;
}
int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
{
*type_p = entry_d->a_type;
return 0;
}
/*
* There is no way of knowing what size the ACL returned by
* ACL_GET will be unless you first call ACL_CNT which means
* making an additional system call.
*
* In the hope of avoiding the cost of the additional system
* call in most cases, we initially allocate enough space for
* an ACL with INITIAL_ACL_SIZE entries. If this turns out to
* be too small then we use ACL_CNT to find out the actual
* size, reallocate the ACL buffer, and then call ACL_GET again.
*/
#define INITIAL_ACL_SIZE 16
#ifndef NACLENTRIES
#define NACLENTRIES 0
#endif
SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
{
SMB_ACL_T acl_d;
int count; /* # of ACL entries allocated */
int naccess; /* # of access ACL entries */
int ndefault; /* # of default ACL entries */
if(hpux_acl_call_presence() == False) {
/* Looks like we don't have the acl() system call on HPUX.
* May be the system doesn't have the latest version of JFS.
*/
return NULL;
}
if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
errno = EINVAL;
return NULL;
}
count = INITIAL_ACL_SIZE;
if ((acl_d = sys_acl_init(count)) == NULL) {
return NULL;
}
/*
* If there isn't enough space for the ACL entries we use
* ACL_CNT to determine the actual number of ACL entries
* reallocate and try again. This is in a loop because it
* is possible that someone else could modify the ACL and
* increase the number of entries between the call to
* ACL_CNT and the call to ACL_GET.
*/
while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
sys_acl_free_acl(acl_d);
if ((count = acl(path_p, ACL_CNT, NACLENTRIES, NULL)) < 0) {
return NULL;
}
if ((acl_d = sys_acl_init(count)) == NULL) {
return NULL;
}
}
if (count < 0) {
sys_acl_free_acl(acl_d);
return NULL;
}
/*
* calculate the number of access and default ACL entries
*
* Note: we assume that the acl() system call returned a
* well formed ACL which is sorted so that all of the
* access ACL entries precede any default ACL entries
*/
for (naccess = 0; naccess < count; naccess++) {
if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
break;
}
ndefault = count - naccess;
/*
* if the caller wants the default ACL we have to copy
* the entries down to the start of the acl[] buffer
* and mask out the ACL_DEFAULT flag from the type field
*/
if (type == SMB_ACL_TYPE_DEFAULT) {
int i, j;
for (i = 0, j = naccess; i < ndefault; i++, j++) {
acl_d->acl[i] = acl_d->acl[j];
acl_d->acl[i].a_type &= ~ACL_DEFAULT;
}
acl_d->count = ndefault;
} else {
acl_d->count = naccess;
}
return acl_d;
}
#if 0
SMB_ACL_T sys_acl_get_fd(int fd)
{
/*
* HPUX doesn't have the facl call. Fake it using the path.... JRA.
*/
files_struct *fsp = file_find_fd(fd);
if (fsp == NULL) {
errno = EBADF;
return NULL;
}
/*
* We know we're in the same conn context. So we
* can use the relative path.
*/
return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
}
#endif
int sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p)
{
*tag_type_p = entry->a_type;
*bits_p = entry->a_perm;
if (*tag_type_p == SMB_ACL_USER || *tag_type_p == SMB_ACL_GROUP)
*u_g_id_p = entry->a_id;
return 0;
}
SMB_ACL_T sys_acl_init(int count)
{