-
Notifications
You must be signed in to change notification settings - Fork 54
/
archive_acl.c
2097 lines (1941 loc) · 51.2 KB
/
archive_acl.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
/*-
* Copyright (c) 2003-2010 Tim Kientzle
* Copyright (c) 2016 Martin Matuska
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "archive_platform.h"
__FBSDID("$FreeBSD$");
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
#include "archive_acl_private.h"
#include "archive_entry.h"
#include "archive_private.h"
#undef max
#define max(a, b) ((a)>(b)?(a):(b))
#ifndef HAVE_WMEMCMP
/* Good enough for simple equality testing, but not for sorting. */
#define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
#endif
static int acl_special(struct archive_acl *acl,
int type, int permset, int tag);
static struct archive_acl_entry *acl_new_entry(struct archive_acl *acl,
int type, int permset, int tag, int id);
static int archive_acl_add_entry_len_l(struct archive_acl *acl,
int type, int permset, int tag, int id, const char *name,
size_t len, struct archive_string_conv *sc);
static int archive_acl_text_want_type(struct archive_acl *acl, int flags);
static ssize_t archive_acl_text_len(struct archive_acl *acl, int want_type,
int flags, int wide, struct archive *a,
struct archive_string_conv *sc);
static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
static int ismode_w(const wchar_t *start, const wchar_t *end, int *result);
static int is_nfs4_flags_w(const wchar_t *start, const wchar_t *end,
int *result);
static int is_nfs4_perms_w(const wchar_t *start, const wchar_t *end,
int *result);
static void next_field_w(const wchar_t **wp, const wchar_t **start,
const wchar_t **end, wchar_t *sep);
static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int type,
int tag, int flags, const wchar_t *wname, int perm, int id);
static void append_id_w(wchar_t **wp, int id);
static int isint(const char *start, const char *end, int *result);
static int ismode(const char *start, const char *end, int *result);
static int is_nfs4_flags(const char *start, const char *end,
int *result);
static int is_nfs4_perms(const char *start, const char *end,
int *result);
static void next_field(const char **p, const char **start,
const char **end, char *sep);
static void append_entry(char **p, const char *prefix, int type,
int tag, int flags, const char *name, int perm, int id);
static void append_id(char **p, int id);
static const struct {
const int perm;
const char c;
const wchar_t wc;
} nfsv4_acl_perm_map[] = {
{ ARCHIVE_ENTRY_ACL_READ_DATA | ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, 'r',
L'r' },
{ ARCHIVE_ENTRY_ACL_WRITE_DATA | ARCHIVE_ENTRY_ACL_ADD_FILE, 'w',
L'w' },
{ ARCHIVE_ENTRY_ACL_EXECUTE, 'x', L'x' },
{ ARCHIVE_ENTRY_ACL_APPEND_DATA | ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY,
'p', L'p' },
{ ARCHIVE_ENTRY_ACL_DELETE, 'd', L'd' },
{ ARCHIVE_ENTRY_ACL_DELETE_CHILD, 'D', L'D' },
{ ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, 'a', L'a' },
{ ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, 'A', L'A' },
{ ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, 'R', L'R' },
{ ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, 'W', L'W' },
{ ARCHIVE_ENTRY_ACL_READ_ACL, 'c', L'c' },
{ ARCHIVE_ENTRY_ACL_WRITE_ACL, 'C', L'C' },
{ ARCHIVE_ENTRY_ACL_WRITE_OWNER, 'o', L'o' },
{ ARCHIVE_ENTRY_ACL_SYNCHRONIZE, 's', L's' }
};
static const int nfsv4_acl_perm_map_size = (int)(sizeof(nfsv4_acl_perm_map) /
sizeof(nfsv4_acl_perm_map[0]));
static const struct {
const int perm;
const char c;
const wchar_t wc;
} nfsv4_acl_flag_map[] = {
{ ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, 'f', L'f' },
{ ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, 'd', L'd' },
{ ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, 'i', L'i' },
{ ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, 'n', L'n' },
{ ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS, 'S', L'S' },
{ ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS, 'F', L'F' },
{ ARCHIVE_ENTRY_ACL_ENTRY_INHERITED, 'I', L'I' }
};
static const int nfsv4_acl_flag_map_size = (int)(sizeof(nfsv4_acl_flag_map) /
sizeof(nfsv4_acl_flag_map[0]));
void
archive_acl_clear(struct archive_acl *acl)
{
struct archive_acl_entry *ap;
while (acl->acl_head != NULL) {
ap = acl->acl_head->next;
archive_mstring_clean(&acl->acl_head->name);
free(acl->acl_head);
acl->acl_head = ap;
}
free(acl->acl_text_w);
acl->acl_text_w = NULL;
free(acl->acl_text);
acl->acl_text = NULL;
acl->acl_p = NULL;
acl->acl_types = 0;
acl->acl_state = 0; /* Not counting. */
}
void
archive_acl_copy(struct archive_acl *dest, struct archive_acl *src)
{
struct archive_acl_entry *ap, *ap2;
archive_acl_clear(dest);
dest->mode = src->mode;
ap = src->acl_head;
while (ap != NULL) {
ap2 = acl_new_entry(dest,
ap->type, ap->permset, ap->tag, ap->id);
if (ap2 != NULL)
archive_mstring_copy(&ap2->name, &ap->name);
ap = ap->next;
}
}
int
archive_acl_add_entry(struct archive_acl *acl,
int type, int permset, int tag, int id, const char *name)
{
struct archive_acl_entry *ap;
if (acl_special(acl, type, permset, tag) == 0)
return ARCHIVE_OK;
ap = acl_new_entry(acl, type, permset, tag, id);
if (ap == NULL) {
/* XXX Error XXX */
return ARCHIVE_FAILED;
}
if (name != NULL && *name != '\0')
archive_mstring_copy_mbs(&ap->name, name);
else
archive_mstring_clean(&ap->name);
return ARCHIVE_OK;
}
int
archive_acl_add_entry_w_len(struct archive_acl *acl,
int type, int permset, int tag, int id, const wchar_t *name, size_t len)
{
struct archive_acl_entry *ap;
if (acl_special(acl, type, permset, tag) == 0)
return ARCHIVE_OK;
ap = acl_new_entry(acl, type, permset, tag, id);
if (ap == NULL) {
/* XXX Error XXX */
return ARCHIVE_FAILED;
}
if (name != NULL && *name != L'\0' && len > 0)
archive_mstring_copy_wcs_len(&ap->name, name, len);
else
archive_mstring_clean(&ap->name);
return ARCHIVE_OK;
}
static int
archive_acl_add_entry_len_l(struct archive_acl *acl,
int type, int permset, int tag, int id, const char *name, size_t len,
struct archive_string_conv *sc)
{
struct archive_acl_entry *ap;
int r;
if (acl_special(acl, type, permset, tag) == 0)
return ARCHIVE_OK;
ap = acl_new_entry(acl, type, permset, tag, id);
if (ap == NULL) {
/* XXX Error XXX */
return ARCHIVE_FAILED;
}
if (name != NULL && *name != '\0' && len > 0) {
r = archive_mstring_copy_mbs_len_l(&ap->name, name, len, sc);
} else {
r = 0;
archive_mstring_clean(&ap->name);
}
if (r == 0)
return (ARCHIVE_OK);
else if (errno == ENOMEM)
return (ARCHIVE_FATAL);
else
return (ARCHIVE_WARN);
}
/*
* If this ACL entry is part of the standard POSIX permissions set,
* store the permissions in the stat structure and return zero.
*/
static int
acl_special(struct archive_acl *acl, int type, int permset, int tag)
{
if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
&& ((permset & ~007) == 0)) {
switch (tag) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
acl->mode &= ~0700;
acl->mode |= (permset & 7) << 6;
return (0);
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
acl->mode &= ~0070;
acl->mode |= (permset & 7) << 3;
return (0);
case ARCHIVE_ENTRY_ACL_OTHER:
acl->mode &= ~0007;
acl->mode |= permset & 7;
return (0);
}
}
return (1);
}
/*
* Allocate and populate a new ACL entry with everything but the
* name.
*/
static struct archive_acl_entry *
acl_new_entry(struct archive_acl *acl,
int type, int permset, int tag, int id)
{
struct archive_acl_entry *ap, *aq;
/* Type argument must be a valid NFS4 or POSIX.1e type.
* The type must agree with anything already set and
* the permset must be compatible. */
if (type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
if (acl->acl_types & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
return (NULL);
}
if (permset &
~(ARCHIVE_ENTRY_ACL_PERMS_NFS4
| ARCHIVE_ENTRY_ACL_INHERITANCE_NFS4)) {
return (NULL);
}
} else if (type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
if (acl->acl_types & ~ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
return (NULL);
}
if (permset & ~ARCHIVE_ENTRY_ACL_PERMS_POSIX1E) {
return (NULL);
}
} else {
return (NULL);
}
/* Verify the tag is valid and compatible with NFS4 or POSIX.1e. */
switch (tag) {
case ARCHIVE_ENTRY_ACL_USER:
case ARCHIVE_ENTRY_ACL_USER_OBJ:
case ARCHIVE_ENTRY_ACL_GROUP:
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
/* Tags valid in both NFS4 and POSIX.1e */
break;
case ARCHIVE_ENTRY_ACL_MASK:
case ARCHIVE_ENTRY_ACL_OTHER:
/* Tags valid only in POSIX.1e. */
if (type & ~ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
return (NULL);
}
break;
case ARCHIVE_ENTRY_ACL_EVERYONE:
/* Tags valid only in NFS4. */
if (type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
return (NULL);
}
break;
default:
/* No other values are valid. */
return (NULL);
}
free(acl->acl_text_w);
acl->acl_text_w = NULL;
free(acl->acl_text);
acl->acl_text = NULL;
/*
* If there's a matching entry already in the list, overwrite it.
* NFSv4 entries may be repeated and are not overwritten.
*
* TODO: compare names of no id is provided (needs more rework)
*/
ap = acl->acl_head;
aq = NULL;
while (ap != NULL) {
if (((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0) &&
ap->type == type && ap->tag == tag && ap->id == id) {
if (id != -1 || (tag != ARCHIVE_ENTRY_ACL_USER &&
tag != ARCHIVE_ENTRY_ACL_GROUP)) {
ap->permset = permset;
return (ap);
}
}
aq = ap;
ap = ap->next;
}
/* Add a new entry to the end of the list. */
ap = (struct archive_acl_entry *)calloc(1, sizeof(*ap));
if (ap == NULL)
return (NULL);
if (aq == NULL)
acl->acl_head = ap;
else
aq->next = ap;
ap->type = type;
ap->tag = tag;
ap->id = id;
ap->permset = permset;
acl->acl_types |= type;
return (ap);
}
/*
* Return a count of entries matching "want_type".
*/
int
archive_acl_count(struct archive_acl *acl, int want_type)
{
int count;
struct archive_acl_entry *ap;
count = 0;
ap = acl->acl_head;
while (ap != NULL) {
if ((ap->type & want_type) != 0)
count++;
ap = ap->next;
}
if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
count += 3;
return (count);
}
/*
* Return a bitmask of stored ACL types in an ACL list
*/
int
archive_acl_types(struct archive_acl *acl)
{
return (acl->acl_types);
}
/*
* Prepare for reading entries from the ACL data. Returns a count
* of entries matching "want_type", or zero if there are no
* non-extended ACL entries of that type.
*/
int
archive_acl_reset(struct archive_acl *acl, int want_type)
{
int count, cutoff;
count = archive_acl_count(acl, want_type);
/*
* If the only entries are the three standard ones,
* then don't return any ACL data. (In this case,
* client can just use chmod(2) to set permissions.)
*/
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
cutoff = 3;
else
cutoff = 0;
if (count > cutoff)
acl->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
else
acl->acl_state = 0;
acl->acl_p = acl->acl_head;
return (count);
}
/*
* Return the next ACL entry in the list. Fake entries for the
* standard permissions and include them in the returned list.
*/
int
archive_acl_next(struct archive *a, struct archive_acl *acl, int want_type,
int *type, int *permset, int *tag, int *id, const char **name)
{
*name = NULL;
*id = -1;
/*
* The acl_state is either zero (no entries available), -1
* (reading from list), or an entry type (retrieve that type
* from ae_stat.aest_mode).
*/
if (acl->acl_state == 0)
return (ARCHIVE_WARN);
/* The first three access entries are special. */
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
switch (acl->acl_state) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
*permset = (acl->mode >> 6) & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
acl->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
return (ARCHIVE_OK);
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
*permset = (acl->mode >> 3) & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
acl->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
return (ARCHIVE_OK);
case ARCHIVE_ENTRY_ACL_OTHER:
*permset = acl->mode & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_OTHER;
acl->acl_state = -1;
acl->acl_p = acl->acl_head;
return (ARCHIVE_OK);
default:
break;
}
}
while (acl->acl_p != NULL && (acl->acl_p->type & want_type) == 0)
acl->acl_p = acl->acl_p->next;
if (acl->acl_p == NULL) {
acl->acl_state = 0;
*type = 0;
*permset = 0;
*tag = 0;
*id = -1;
*name = NULL;
return (ARCHIVE_EOF); /* End of ACL entries. */
}
*type = acl->acl_p->type;
*permset = acl->acl_p->permset;
*tag = acl->acl_p->tag;
*id = acl->acl_p->id;
if (archive_mstring_get_mbs(a, &acl->acl_p->name, name) != 0) {
if (errno == ENOMEM)
return (ARCHIVE_FATAL);
*name = NULL;
}
acl->acl_p = acl->acl_p->next;
return (ARCHIVE_OK);
}
/*
* Determine what type of ACL do we want
*/
static int
archive_acl_text_want_type(struct archive_acl *acl, int flags)
{
int want_type;
/* Check if ACL is NFSv4 */
if ((acl->acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
/* NFSv4 should never mix with POSIX.1e */
if ((acl->acl_types & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0)
return (0);
else
return (ARCHIVE_ENTRY_ACL_TYPE_NFS4);
}
/* Now deal with POSIX.1e ACLs */
want_type = 0;
if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
want_type |= ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
want_type |= ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
/* By default we want both access and default ACLs */
if (want_type == 0)
return (ARCHIVE_ENTRY_ACL_TYPE_POSIX1E);
return (want_type);
}
/*
* Calculate ACL text string length
*/
static ssize_t
archive_acl_text_len(struct archive_acl *acl, int want_type, int flags,
int wide, struct archive *a, struct archive_string_conv *sc) {
struct archive_acl_entry *ap;
const char *name;
const wchar_t *wname;
int count, idlen, tmp, r;
ssize_t length;
size_t len;
count = 0;
length = 0;
for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
if ((ap->type & want_type) == 0)
continue;
/*
* Filemode-mapping ACL entries are stored exclusively in
* ap->mode so they should not be in the list
*/
if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
&& (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
|| ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
|| ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
continue;
count++;
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0
&& (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
length += 8; /* "default:" */
switch (ap->tag) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
length += 6; /* "owner@" */
break;
}
/* FALLTHROUGH */
case ARCHIVE_ENTRY_ACL_USER:
case ARCHIVE_ENTRY_ACL_MASK:
length += 4; /* "user", "mask" */
break;
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
length += 6; /* "group@" */
break;
}
/* FALLTHROUGH */
case ARCHIVE_ENTRY_ACL_GROUP:
case ARCHIVE_ENTRY_ACL_OTHER:
length += 5; /* "group", "other" */
break;
case ARCHIVE_ENTRY_ACL_EVERYONE:
length += 9; /* "everyone@" */
break;
}
length += 1; /* colon after tag */
if (ap->tag == ARCHIVE_ENTRY_ACL_USER ||
ap->tag == ARCHIVE_ENTRY_ACL_GROUP) {
if (wide) {
r = archive_mstring_get_wcs(a, &ap->name,
&wname);
if (r == 0 && wname != NULL)
length += wcslen(wname);
else if (r < 0 && errno == ENOMEM)
return (0);
else
length += sizeof(uid_t) * 3 + 1;
} else {
r = archive_mstring_get_mbs_l(a, &ap->name, &name,
&len, sc);
if (r != 0)
return (0);
if (len > 0 && name != NULL)
length += len;
else
length += sizeof(uid_t) * 3 + 1;
}
length += 1; /* colon after user or group name */
} else if (want_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4)
length += 1; /* 2nd colon empty user,group or other */
if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) != 0)
&& ((want_type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0)
&& (ap->tag == ARCHIVE_ENTRY_ACL_OTHER
|| ap->tag == ARCHIVE_ENTRY_ACL_MASK)) {
/* Solaris has no colon after other: and mask: */
length = length - 1;
}
if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
/* rwxpdDaARWcCos:fdinSFI:deny */
length += 27;
if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DENY) == 0)
length += 1; /* allow, alarm, audit */
} else
length += 3; /* rwx */
if ((ap->tag == ARCHIVE_ENTRY_ACL_USER ||
ap->tag == ARCHIVE_ENTRY_ACL_GROUP) &&
(flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID) != 0) {
length += 1; /* colon */
/* ID digit count */
idlen = 1;
tmp = ap->id;
while (tmp > 9) {
tmp = tmp / 10;
idlen++;
}
length += idlen;
}
length ++; /* entry separator */
}
/* Add filemode-mapping access entries to the length */
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
if ((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) != 0) {
/* "user::rwx\ngroup::rwx\nother:rwx\n" */
length += 31;
} else {
/* "user::rwx\ngroup::rwx\nother::rwx\n" */
length += 32;
}
} else if (count == 0)
return (0);
/* The terminating character is included in count */
return (length);
}
/*
* Generate a wide text version of the ACL. The flags parameter controls
* the type and style of the generated ACL.
*/
wchar_t *
archive_acl_to_text_w(struct archive_acl *acl, ssize_t *text_len, int flags,
struct archive *a)
{
int count;
ssize_t length;
size_t len;
const wchar_t *wname;
const wchar_t *prefix;
wchar_t separator;
struct archive_acl_entry *ap;
int id, r, want_type;
wchar_t *wp, *ws;
want_type = archive_acl_text_want_type(acl, flags);
/* Both NFSv4 and POSIX.1 types found */
if (want_type == 0)
return (NULL);
if (want_type == ARCHIVE_ENTRY_ACL_TYPE_POSIX1E)
flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
length = archive_acl_text_len(acl, want_type, flags, 1, a, NULL);
if (length == 0)
return (NULL);
if (flags & ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA)
separator = L',';
else
separator = L'\n';
/* Now, allocate the string and actually populate it. */
wp = ws = (wchar_t *)malloc(length * sizeof(wchar_t));
if (wp == NULL) {
if (errno == ENOMEM)
__archive_errx(1, "No memory");
return (NULL);
}
count = 0;
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
ARCHIVE_ENTRY_ACL_USER_OBJ, flags, NULL,
acl->mode & 0700, -1);
*wp++ = separator;
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
ARCHIVE_ENTRY_ACL_GROUP_OBJ, flags, NULL,
acl->mode & 0070, -1);
*wp++ = separator;
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
ARCHIVE_ENTRY_ACL_OTHER, flags, NULL,
acl->mode & 0007, -1);
count += 3;
}
for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
if ((ap->type & want_type) == 0)
continue;
/*
* Filemode-mapping ACL entries are stored exclusively in
* ap->mode so they should not be in the list
*/
if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
&& (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
|| ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
|| ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
continue;
if (ap->type == ARCHIVE_ENTRY_ACL_TYPE_DEFAULT &&
(flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
prefix = L"default:";
else
prefix = NULL;
r = archive_mstring_get_wcs(a, &ap->name, &wname);
if (r == 0) {
if (count > 0)
*wp++ = separator;
if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
id = ap->id;
else
id = -1;
append_entry_w(&wp, prefix, ap->type, ap->tag, flags,
wname, ap->permset, id);
count++;
} else if (r < 0 && errno == ENOMEM) {
free(ws);
return (NULL);
}
}
/* Add terminating character */
*wp++ = L'\0';
len = wcslen(ws);
if ((ssize_t)len > (length - 1))
__archive_errx(1, "Buffer overrun");
if (text_len != NULL)
*text_len = len;
return (ws);
}
static void
append_id_w(wchar_t **wp, int id)
{
if (id < 0)
id = 0;
if (id > 9)
append_id_w(wp, id / 10);
*(*wp)++ = L"0123456789"[id % 10];
}
static void
append_entry_w(wchar_t **wp, const wchar_t *prefix, int type,
int tag, int flags, const wchar_t *wname, int perm, int id)
{
int i;
if (prefix != NULL) {
wcscpy(*wp, prefix);
*wp += wcslen(*wp);
}
switch (tag) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
wname = NULL;
id = -1;
if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
wcscpy(*wp, L"owner@");
break;
}
/* FALLTHROUGH */
case ARCHIVE_ENTRY_ACL_USER:
wcscpy(*wp, L"user");
break;
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
wname = NULL;
id = -1;
if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
wcscpy(*wp, L"group@");
break;
}
/* FALLTHROUGH */
case ARCHIVE_ENTRY_ACL_GROUP:
wcscpy(*wp, L"group");
break;
case ARCHIVE_ENTRY_ACL_MASK:
wcscpy(*wp, L"mask");
wname = NULL;
id = -1;
break;
case ARCHIVE_ENTRY_ACL_OTHER:
wcscpy(*wp, L"other");
wname = NULL;
id = -1;
break;
case ARCHIVE_ENTRY_ACL_EVERYONE:
wcscpy(*wp, L"everyone@");
wname = NULL;
id = -1;
break;
}
*wp += wcslen(*wp);
*(*wp)++ = L':';
if (((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) ||
tag == ARCHIVE_ENTRY_ACL_USER ||
tag == ARCHIVE_ENTRY_ACL_GROUP) {
if (wname != NULL) {
wcscpy(*wp, wname);
*wp += wcslen(*wp);
} else if (tag == ARCHIVE_ENTRY_ACL_USER
|| tag == ARCHIVE_ENTRY_ACL_GROUP) {
append_id_w(wp, id);
if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0)
id = -1;
}
/* Solaris style has no second colon after other and mask */
if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) == 0)
|| (tag != ARCHIVE_ENTRY_ACL_OTHER
&& tag != ARCHIVE_ENTRY_ACL_MASK))
*(*wp)++ = L':';
}
if ((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
/* POSIX.1e ACL perms */
*(*wp)++ = (perm & 0444) ? L'r' : L'-';
*(*wp)++ = (perm & 0222) ? L'w' : L'-';
*(*wp)++ = (perm & 0111) ? L'x' : L'-';
} else {
/* NFSv4 ACL perms */
for (i = 0; i < nfsv4_acl_perm_map_size; i++) {
if (perm & nfsv4_acl_perm_map[i].perm)
*(*wp)++ = nfsv4_acl_perm_map[i].wc;
else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
*(*wp)++ = L'-';
}
*(*wp)++ = L':';
for (i = 0; i < nfsv4_acl_flag_map_size; i++) {
if (perm & nfsv4_acl_flag_map[i].perm)
*(*wp)++ = nfsv4_acl_flag_map[i].wc;
else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
*(*wp)++ = L'-';
}
*(*wp)++ = L':';
switch (type) {
case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
wcscpy(*wp, L"allow");
break;
case ARCHIVE_ENTRY_ACL_TYPE_DENY:
wcscpy(*wp, L"deny");
break;
case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
wcscpy(*wp, L"audit");
break;
case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
wcscpy(*wp, L"alarm");
break;
default:
break;
}
*wp += wcslen(*wp);
}
if (id != -1) {
*(*wp)++ = L':';
append_id_w(wp, id);
}
}
/*
* Generate a text version of the ACL. The flags parameter controls
* the type and style of the generated ACL.
*/
char *
archive_acl_to_text_l(struct archive_acl *acl, ssize_t *text_len, int flags,
struct archive_string_conv *sc)
{
int count;
ssize_t length;
size_t len;
const char *name;
const char *prefix;
char separator;
struct archive_acl_entry *ap;
int id, r, want_type;
char *p, *s;
want_type = archive_acl_text_want_type(acl, flags);
/* Both NFSv4 and POSIX.1 types found */
if (want_type == 0)
return (NULL);
if (want_type == ARCHIVE_ENTRY_ACL_TYPE_POSIX1E)
flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
length = archive_acl_text_len(acl, want_type, flags, 0, NULL, sc);
if (length == 0)
return (NULL);
if (flags & ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA)
separator = ',';
else
separator = '\n';
/* Now, allocate the string and actually populate it. */
p = s = (char *)malloc(length * sizeof(char));
if (p == NULL) {
if (errno == ENOMEM)
__archive_errx(1, "No memory");
return (NULL);
}
count = 0;
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
ARCHIVE_ENTRY_ACL_USER_OBJ, flags, NULL,
acl->mode & 0700, -1);
*p++ = separator;
append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
ARCHIVE_ENTRY_ACL_GROUP_OBJ, flags, NULL,
acl->mode & 0070, -1);
*p++ = separator;
append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
ARCHIVE_ENTRY_ACL_OTHER, flags, NULL,
acl->mode & 0007, -1);
count += 3;
}
for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
if ((ap->type & want_type) == 0)
continue;
/*
* Filemode-mapping ACL entries are stored exclusively in
* ap->mode so they should not be in the list
*/
if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
&& (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
|| ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
|| ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
continue;
if (ap->type == ARCHIVE_ENTRY_ACL_TYPE_DEFAULT &&
(flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
prefix = "default:";
else
prefix = NULL;
r = archive_mstring_get_mbs_l(
NULL, &ap->name, &name, &len, sc);
if (r != 0) {
free(s);
return (NULL);
}
if (count > 0)
*p++ = separator;
if (name == NULL ||
(flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)) {
id = ap->id;
} else {
id = -1;
}
append_entry(&p, prefix, ap->type, ap->tag, flags, name,
ap->permset, id);
count++;
}
/* Add terminating character */
*p++ = '\0';
len = strlen(s);
if ((ssize_t)len > (length - 1))
__archive_errx(1, "Buffer overrun");
if (text_len != NULL)
*text_len = len;
return (s);