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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
|
/*-
* Copyright (c) 2003-2004 Tim Kientzle
* 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
* in this position and unchanged.
* 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$");
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "archive.h"
#include "archive_entry.h"
#include "archive_private.h"
#undef max
#define max(a, b) ((a)>(b)?(a):(b))
/*
* Handle wide character (i.e., Unicode) and non-wide character
* strings transparently.
*
*/
struct aes {
const char *aes_mbs;
char *aes_mbs_alloc;
const wchar_t *aes_wcs;
wchar_t *aes_wcs_alloc;
};
struct ae_acl {
struct ae_acl *next;
int type; /* E.g., access or default */
int tag; /* E.g., user/group/other/mask */
int permset; /* r/w/x bits */
int id; /* uid/gid for user/group */
struct aes name; /* uname/gname */
};
static void aes_clean(struct aes *);
static void aes_copy(struct aes *dest, struct aes *src);
static const char * aes_get_mbs(struct aes *);
static const wchar_t * aes_get_wcs(struct aes *);
static void aes_set_mbs(struct aes *, const char *mbs);
static void aes_copy_mbs(struct aes *, const char *mbs);
/* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
const wchar_t *wname, int perm, int id);
static void append_id_w(wchar_t **wp, int id);
static int acl_special(struct archive_entry *entry,
int type, int permset, int tag);
static struct ae_acl *acl_new_entry(struct archive_entry *entry,
int type, int permset, int tag, int id);
static void next_field_w(const wchar_t **wp, const wchar_t **start,
const wchar_t **end, wchar_t *sep);
static int prefix_w(const wchar_t *start, const wchar_t *end,
const wchar_t *test);
/*
* Description of an archive entry.
*
* Basically, this is a "struct stat" with a few text fields added in.
*
* TODO: Add "comment", "charset", and possibly other entries
* that are supported by "pax interchange" format. However, GNU, ustar,
* cpio, and other variants don't support these features, so they're not an
* excruciatingly high priority right now.
*
* TODO: "pax interchange" format allows essentially arbitrary
* key/value attributes to be attached to any entry. Supporting
* such extensions may make this library useful for special
* applications (e.g., a package manager could attach special
* package-management attributes to each entry). There are tricky
* API issues involved, so this is not going to happen until
* there's a real demand for it.
*
* TODO: Design a good API for handling sparse files.
*/
struct archive_entry {
/*
* Note that ae_stat.st_mode & S_IFMT can be 0!
* This occurs when the actual file type of the underlying object is
* not in the archive. For example, 'tar' archives store hardlinks
* without marking the type of the underlying object.
*/
struct stat ae_stat;
/*
* Use aes here so that we get transparent mbs<->wcs conversions.
*/
struct aes ae_fflags; /* Text fflags per fflagstostr(3) */
struct aes ae_gname; /* Name of owning group */
struct aes ae_hardlink; /* Name of target for hardlink */
struct aes ae_pathname; /* Name of entry */
struct aes ae_symlink; /* symlink contents */
struct aes ae_uname; /* Name of owner */
struct ae_acl *acl_head;
struct ae_acl *acl_p;
int acl_state; /* See acl_next for details. */
wchar_t *acl_text_w;
};
void
aes_clean(struct aes *aes)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
memset(aes, 0, sizeof(*aes));
}
void
aes_copy(struct aes *dest, struct aes *src)
{
*dest = *src;
if (src->aes_mbs_alloc != NULL) {
dest->aes_mbs_alloc = strdup(src->aes_mbs_alloc);
dest->aes_mbs = dest->aes_mbs_alloc;
}
if (src->aes_wcs_alloc != NULL) {
dest->aes_wcs_alloc = malloc((wcslen(src->aes_wcs_alloc) + 1)
* sizeof(wchar_t));
dest->aes_wcs = dest->aes_wcs_alloc;
wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
}
}
const char *
aes_get_mbs(struct aes *aes)
{
if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
/*
* XXX Need to estimate the number of byte in the
* multi-byte form. Assume that, on average, wcs
* chars encode to no more than 3 bytes. There must
* be a better way... XXX
*/
int mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
aes->aes_mbs_alloc = malloc(mbs_length);
aes->aes_mbs = aes->aes_mbs_alloc;
wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
aes->aes_mbs_alloc[mbs_length - 1] = 0;
}
return (aes->aes_mbs);
}
const wchar_t *
aes_get_wcs(struct aes *aes)
{
if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
/*
* No single byte will be more than one wide character,
* so this length estimate will always be big enough.
*/
int wcs_length = strlen(aes->aes_mbs);
aes->aes_wcs_alloc
= malloc((wcs_length + 1) * sizeof(wchar_t));
aes->aes_wcs = aes->aes_wcs_alloc;
mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
aes->aes_wcs_alloc[wcs_length] = 0;
}
return (aes->aes_wcs);
}
void
aes_set_mbs(struct aes *aes, const char *mbs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs = mbs;
aes->aes_wcs = NULL;
}
void
aes_copy_mbs(struct aes *aes, const char *mbs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs_alloc = malloc((strlen(mbs) + 1) * sizeof(char));
strcpy(aes->aes_mbs_alloc, mbs);
aes->aes_mbs = aes->aes_mbs_alloc;
aes->aes_wcs = NULL;
}
#if 0
void
aes_set_wcs(struct aes *aes, const wchar_t *wcs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs = NULL;
aes->aes_wcs = wcs;
}
#endif
void
aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs = NULL;
aes->aes_wcs_alloc = malloc((wcslen(wcs) + 1) * sizeof(wchar_t));
wcscpy(aes->aes_wcs_alloc, wcs);
aes->aes_wcs = aes->aes_wcs_alloc;
}
struct archive_entry *
archive_entry_clear(struct archive_entry *entry)
{
aes_clean(&entry->ae_fflags);
aes_clean(&entry->ae_gname);
aes_clean(&entry->ae_hardlink);
aes_clean(&entry->ae_pathname);
aes_clean(&entry->ae_symlink);
aes_clean(&entry->ae_uname);
archive_entry_acl_clear(entry);
memset(entry, 0, sizeof(*entry));
return entry;
}
struct archive_entry *
archive_entry_clone(struct archive_entry *entry)
{
struct archive_entry *entry2;
/* Allocate new structure and copy over all of the fields. */
entry2 = malloc(sizeof(*entry2));
if(entry2 == NULL)
return (NULL);
memset(entry2, 0, sizeof(*entry2));
entry2->ae_stat = entry->ae_stat;
aes_copy(&entry2->ae_fflags ,&entry->ae_fflags);
aes_copy(&entry2->ae_gname ,&entry->ae_gname);
aes_copy(&entry2->ae_hardlink ,&entry->ae_hardlink);
aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
aes_copy(&entry2->ae_symlink ,&entry->ae_symlink);
aes_copy(&entry2->ae_uname ,&entry->ae_uname);
return (entry2);
}
void
archive_entry_free(struct archive_entry *entry)
{
archive_entry_clear(entry);
free(entry);
}
struct archive_entry *
archive_entry_new(void)
{
struct archive_entry *entry;
entry = malloc(sizeof(*entry));
if(entry == NULL)
return (NULL);
memset(entry, 0, sizeof(*entry));
return (entry);
}
/*
* Functions for reading fields from an archive_entry.
*/
dev_t
archive_entry_devmajor(struct archive_entry *entry)
{
return (major(entry->ae_stat.st_rdev));
}
dev_t
archive_entry_devminor(struct archive_entry *entry)
{
return (minor(entry->ae_stat.st_rdev));
}
const char *
archive_entry_fflags(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_fflags));
}
const char *
archive_entry_gname(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_gname));
}
const char *
archive_entry_hardlink(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_hardlink));
}
mode_t
archive_entry_mode(struct archive_entry *entry)
{
return (entry->ae_stat.st_mode);
}
time_t
archive_entry_mtime(struct archive_entry *entry)
{
return (entry->ae_stat.st_mtime);
}
long
archive_entry_mtime_nsec(struct archive_entry *entry)
{
return (entry->ae_stat.st_mtimespec.tv_nsec);
}
const char *
archive_entry_pathname(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_pathname));
}
const wchar_t *
archive_entry_pathname_w(struct archive_entry *entry)
{
return (aes_get_wcs(&entry->ae_pathname));
}
int64_t
archive_entry_size(struct archive_entry *entry)
{
return (entry->ae_stat.st_size);
}
const struct stat *
archive_entry_stat(struct archive_entry *entry)
{
return (&entry->ae_stat);
}
const char *
archive_entry_symlink(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_symlink));
}
const char *
archive_entry_uname(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_uname));
}
/*
* Functions to set archive_entry properties.
*/
/*
* Note "copy" not "set" here. The "set" functions that accept a pointer
* only store the pointer; they don't copy the underlying object.
*/
void
archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
{
entry->ae_stat = *st;
}
void
archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
{
dev_t d;
d = entry->ae_stat.st_rdev;
entry->ae_stat.st_rdev = makedev(m, minor(d));
}
void
archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
{
dev_t d;
d = entry->ae_stat.st_rdev;
entry->ae_stat.st_rdev = makedev( major(d), m);
}
void
archive_entry_set_fflags(struct archive_entry *entry, const char *flags)
{
aes_set_mbs(&entry->ae_fflags, flags);
}
void
archive_entry_copy_fflags_w(struct archive_entry *entry, const wchar_t *flags)
{
aes_copy_wcs(&entry->ae_fflags, flags);
}
void
archive_entry_set_gid(struct archive_entry *entry, gid_t g)
{
entry->ae_stat.st_gid = g;
}
void
archive_entry_set_gname(struct archive_entry *entry, const char *name)
{
aes_set_mbs(&entry->ae_gname, name);
}
void
archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
{
aes_copy_wcs(&entry->ae_gname, name);
}
void
archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
{
aes_set_mbs(&entry->ae_hardlink, target);
}
void
archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
{
aes_copy_mbs(&entry->ae_hardlink, target);
}
void
archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
{
aes_copy_wcs(&entry->ae_hardlink, target);
}
/* Set symlink if symlink is already set, else set hardlink. */
void
archive_entry_set_link(struct archive_entry *entry, const char *target)
{
if (entry->ae_symlink.aes_mbs != NULL ||
entry->ae_symlink.aes_wcs != NULL)
aes_set_mbs(&entry->ae_symlink, target);
aes_set_mbs(&entry->ae_hardlink, target);
}
void
archive_entry_set_mode(struct archive_entry *entry, mode_t m)
{
entry->ae_stat.st_mode = m;
}
void
archive_entry_set_pathname(struct archive_entry *entry, const char *name)
{
aes_set_mbs(&entry->ae_pathname, name);
}
void
archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
{
aes_copy_wcs(&entry->ae_pathname, name);
}
void
archive_entry_set_size(struct archive_entry *entry, int64_t s)
{
entry->ae_stat.st_size = s;
}
void
archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
{
aes_set_mbs(&entry->ae_symlink, linkname);
}
void
archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
{
aes_copy_wcs(&entry->ae_symlink, linkname);
}
void
archive_entry_set_uid(struct archive_entry *entry, uid_t u)
{
entry->ae_stat.st_uid = u;
}
void
archive_entry_set_uname(struct archive_entry *entry, const char *name)
{
aes_set_mbs(&entry->ae_uname, name);
}
void
archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
{
aes_copy_wcs(&entry->ae_uname, name);
}
/*
* ACL management. The following would, of course, be a lot simpler
* if: 1) the last draft of POSIX.1e were a really thorough and
* complete standard that addressed the needs of ACL archiving and 2)
* everyone followed it faithfully. Alas, neither is true, so the
* following is a lot more complex than might seem necessary to the
* uninitiated.
*/
void
archive_entry_acl_clear(struct archive_entry *entry)
{
struct ae_acl *ap;
while (entry->acl_head != NULL) {
ap = entry->acl_head->next;
aes_clean(&entry->acl_head->name);
free(entry->acl_head);
entry->acl_head = ap;
}
if (entry->acl_text_w != NULL) {
free(entry->acl_text_w);
entry->acl_text_w = NULL;
}
entry->acl_p = NULL;
entry->acl_state = 0; /* Not counting. */
}
/*
* Add a single ACL entry to the internal list of ACL data.
*/
void
archive_entry_acl_add_entry(struct archive_entry *entry,
int type, int permset, int tag, int id, const char *name)
{
struct ae_acl *ap;
if (acl_special(entry, type, permset, tag) == 0)
return;
ap = acl_new_entry(entry, type, permset, tag, id);
if (ap == NULL) {
/* XXX Error XXX */
return;
}
if (name != NULL && *name != '\0')
aes_copy_mbs(&ap->name, name);
else
aes_clean(&ap->name);
}
/*
* As above, but with a wide-character name.
*/
void
archive_entry_acl_add_entry_w(struct archive_entry *entry,
int type, int permset, int tag, int id, const wchar_t *name)
{
struct ae_acl *ap;
if (acl_special(entry, type, permset, tag) == 0)
return;
ap = acl_new_entry(entry, type, permset, tag, id);
if (ap == NULL) {
/* XXX Error XXX */
return;
}
if (name != NULL && *name != L'\0')
aes_copy_wcs(&ap->name, name);
else
aes_clean(&ap->name);
}
/*
* 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_entry *entry, int type, int permset, int tag)
{
if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
switch (tag) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
entry->ae_stat.st_mode &= 0077;
entry->ae_stat.st_mode |= (permset & 7) << 6;
return (0);
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
entry->ae_stat.st_mode &= 0707;
entry->ae_stat.st_mode |= (permset & 7) << 3;
return (0);
case ARCHIVE_ENTRY_ACL_OTHER:
entry->ae_stat.st_mode &= 0770;
entry->ae_stat.st_mode |= permset & 7;
return (0);
}
}
return (1);
}
/*
* Allocate and populate a new ACL entry with everything but the
* name.
*/
static struct ae_acl *
acl_new_entry(struct archive_entry *entry,
int type, int permset, int tag, int id)
{
struct ae_acl *ap;
if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
return (NULL);
if (entry->acl_text_w != NULL) {
free(entry->acl_text_w);
entry->acl_text_w = NULL;
}
/* XXX TODO: More sanity-checks on the arguments XXX */
/* If there's a matching entry already in the list, overwrite it. */
for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
if (ap->type == type && ap->tag == tag && ap->id == id) {
ap->permset = permset;
return (ap);
}
}
/* Add a new entry to the list. */
ap = malloc(sizeof(*ap));
memset(ap, 0, sizeof(*ap));
ap->next = entry->acl_head;
entry->acl_head = ap;
ap->type = type;
ap->tag = tag;
ap->id = id;
ap->permset = permset;
return (ap);
}
/*
* Return a count of entries matching "want_type".
*/
int
archive_entry_acl_count(struct archive_entry *entry, int want_type)
{
int count;
struct ae_acl *ap;
count = 0;
ap = entry->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);
}
/*
* 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_entry_acl_reset(struct archive_entry *entry, int want_type)
{
int count, cutoff;
count = archive_entry_acl_count(entry, 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)
entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
else
entry->acl_state = 0;
entry->acl_p = entry->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_entry_acl_next(struct archive_entry *entry, 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.st_mode).
*/
if (entry->acl_state == 0)
return (ARCHIVE_WARN);
/* The first three access entries are special. */
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
switch (entry->acl_state) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
*permset = (entry->ae_stat.st_mode >> 6) & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
return (ARCHIVE_OK);
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
*permset = (entry->ae_stat.st_mode >> 3) & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
return (ARCHIVE_OK);
case ARCHIVE_ENTRY_ACL_OTHER:
*permset = entry->ae_stat.st_mode & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_OTHER;
entry->acl_state = -1;
entry->acl_p = entry->acl_head;
return (ARCHIVE_OK);
default:
break;
}
}
while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
entry->acl_p = entry->acl_p->next;
if (entry->acl_p == NULL) {
entry->acl_state = 0;
return (ARCHIVE_WARN);
}
*type = entry->acl_p->type;
*permset = entry->acl_p->permset;
*tag = entry->acl_p->tag;
*id = entry->acl_p->id;
*name = aes_get_mbs(&entry->acl_p->name);
entry->acl_p = entry->acl_p->next;
return (ARCHIVE_OK);
}
/*
* Generate a text version of the ACL. The flags parameter controls
* the style of the generated ACL.
*/
const wchar_t *
archive_entry_acl_text_w(struct archive_entry *entry, int flags)
{
int count;
int length;
const wchar_t *wname;
const wchar_t *prefix;
wchar_t separator;
struct ae_acl *ap;
int id;
wchar_t *wp;
if (entry->acl_text_w != NULL) {
free (entry->acl_text_w);
entry->acl_text_w = NULL;
}
separator = L',';
count = 0;
length = 0;
ap = entry->acl_head;
while (ap != NULL) {
if ((ap->type & flags) != 0) {
count++;
if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
(ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
length += 8; /* "default:" */
length += 5; /* tag name */
length += 1; /* colon */
wname = aes_get_wcs(&ap->name);
if (wname != NULL)
length += wcslen(wname);
length ++; /* colon */
length += 3; /* rwx */
length += 1; /* colon */
length += max(sizeof(uid_t),sizeof(gid_t)) * 3 + 1;
length ++; /* newline */
}
ap = ap->next;
}
if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
length += 10; /* "user::rwx\n" */
length += 11; /* "group::rwx\n" */
length += 11; /* "other::rwx\n" */
}
if (count == 0)
return (NULL);
/* Now, allocate the string and actually populate it. */
wp = entry->acl_text_w = malloc(length * sizeof(wchar_t));
count = 0;
if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
entry->ae_stat.st_mode & 0700, -1);
*wp++ = ',';
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
entry->ae_stat.st_mode & 0070, -1);
*wp++ = ',';
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
entry->ae_stat.st_mode & 0007, -1);
count += 3;
ap = entry->acl_head;
while (ap != NULL) {
if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
wname = aes_get_wcs(&ap->name);
*wp++ = separator;
if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
id = ap->id;
else
id = -1;
append_entry_w(&wp, NULL, ap->tag, wname,
ap->permset, id);
count++;
}
ap = ap->next;
}
}
if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
prefix = L"default:";
else
prefix = NULL;
ap = entry->acl_head;
count = 0;
while (ap != NULL) {
if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
wname = aes_get_wcs(&ap->name);
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->tag,
wname, ap->permset, id);
count ++;
}
ap = ap->next;
}
}
return (entry->acl_text_w);
}
static void
append_id_w(wchar_t **wp, int id)
{
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 tag,
const wchar_t *wname, int perm, int id)
{
if (prefix != NULL) {
wcscpy(*wp, prefix);
*wp += wcslen(*wp);
}
switch (tag) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
wname = NULL;
id = -1;
/* FALL THROUGH */
case ARCHIVE_ENTRY_ACL_USER:
wcscpy(*wp, L"user");
break;
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
wname = NULL;
id = -1;
/* FALL THROUGH */
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;
}
*wp += wcslen(*wp);
*(*wp)++ = L':';
if (wname != NULL) {
wcscpy(*wp, wname);
*wp += wcslen(*wp);
}
*(*wp)++ = L':';
*(*wp)++ = (perm & 0444) ? L'r' : L'-';
*(*wp)++ = (perm & 0222) ? L'w' : L'-';
*(*wp)++ = (perm & 0111) ? L'x' : L'-';
if (id != -1) {
*(*wp)++ = L':';
append_id_w(wp, id);
}
**wp = L'\0';
}
/*
* Parse a textual ACL. This automatically recognizes and supports
* extensions described above. The 'type' argument is used to
* indicate the type that should be used for any entries not
* explicitly marked as "default:".
*/
int
__archive_entry_acl_parse_w(struct archive_entry *entry,
const wchar_t *text, int default_type)
{
int type, tag, permset, id;
const wchar_t *start, *end;
const wchar_t *name_start, *name_end;
wchar_t sep;
wchar_t *namebuff;
int namebuff_length;
name_start = name_end = NULL;
namebuff = NULL;
namebuff_length = 0;
while (text != NULL && *text != L'\0') {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (prefix_w(start, end, L"default")) {
type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
} else
type = default_type;
if (prefix_w(start, end, L"user")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start) {
tag = ARCHIVE_ENTRY_ACL_USER;
name_start = start;
name_end = end;
} else
tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
} else if (prefix_w(start, end, L"group")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start) {
tag = ARCHIVE_ENTRY_ACL_GROUP;
name_start = start;
name_end = end;
} else
tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
} else if (prefix_w(start, end, L"other")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start)
goto fail;
tag = ARCHIVE_ENTRY_ACL_OTHER;
} else if (prefix_w(start, end, L"mask")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start)
goto fail;
tag = ARCHIVE_ENTRY_ACL_MASK;
} else
goto fail;
next_field_w(&text, &start, &end, &sep);
permset = 0;
while (start < end) {
switch (*start++) {
case 'r': case 'R':
permset |= ARCHIVE_ENTRY_ACL_READ;
break;
case 'w': case 'W':
permset |= ARCHIVE_ENTRY_ACL_WRITE;
break;
case 'x': case 'X':
permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
break;
case '-':
break;
default:
goto fail;
}
}
/*
* Support star-compatible numeric UID/GID extension.
* This extension adds a ":" followed by the numeric
* ID so that "group:groupname:rwx", for example,
* becomes "group:groupname:rwx:999", where 999 is the
* numeric GID. This extension makes it possible, for
* example, to correctly restore ACLs on a system that
* might have a damaged passwd file or be disconnected
* from a central NIS server. This extension is compatible
* with POSIX.1e draft 17.
*/
if (sep == L':' && (tag == ARCHIVE_ENTRY_ACL_USER ||
tag == ARCHIVE_ENTRY_ACL_GROUP)) {
next_field_w(&text, &start, &end, &sep);
id = 0;
while (start < end && *start >= '0' && *start <= '9') {
if (id > (INT_MAX / 10))
id = INT_MAX;
else {
id *= 10;
id += *start - '0';
start++;
}
}
} else
id = -1; /* No id specified. */
/* Skip any additional entries. */
while (sep == L':') {
next_field_w(&text, &start, &end, &sep);
}
/* Add entry to the internal list. */
if (name_end == name_start) {
archive_entry_acl_add_entry_w(entry, type, permset,
tag, id, NULL);
} else {
if (namebuff_length <= name_end - name_start) {
if (namebuff != NULL)
free(namebuff);
namebuff_length = name_end - name_start + 256;
namebuff =
malloc(namebuff_length * sizeof(wchar_t));
}
wmemcpy(namebuff, name_start, name_end - name_start);
namebuff[name_end - name_start] = L'\0';
archive_entry_acl_add_entry_w(entry, type,
permset, tag, id, namebuff);
}
}
if (namebuff != NULL)
free(namebuff);
return (ARCHIVE_OK);
fail:
if (namebuff != NULL)
free(namebuff);
return (ARCHIVE_WARN);
}
/*
* Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
* to point to just after the separator. *start points to the first
* character of the matched text and *end just after the last
* character of the matched identifier. In particular *end - *start
* is the length of the field body, not including leading or trailing
* whitespace.
*/
static void
next_field_w(const wchar_t **wp, const wchar_t **start,
const wchar_t **end, wchar_t *sep)
{
/* Skip leading whitespace to find start of field. */
while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
(*wp)++;
}
*start = *wp;
/* Scan for the separator. */
while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
**wp != L'\n') {
(*wp)++;
}
*sep = **wp;
/* Trim trailing whitespace to locate end of field. */
*end = *wp - 1;
while (**end == L' ' || **end == L'\t' || **end == L'\n') {
(*end)--;
}
(*end)++;
/* Adjust scanner location. */
if (**wp != L'\0')
(*wp)++;
}
static int
prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
{
if (start == end)
return (0);
if (*start++ != *test++)
return (0);
while (start < end && *start++ == *test++)
;
if (start < end)
return (0);
return (1);
}
#if TEST
int
main(int argc, char **argv)
{
struct aes aes;
memset(&aes, 0, sizeof(aes));
aes_clean(&aes);
aes_set_mbs(&aes, "ÈÈÈabc");
wprintf("%S\n", L"abcdef");
wprintf("%S\n",aes_get_wcs(&aes));
return (0);
}
#endif
|