This source file includes following definitions.
- have_xattr
- acl_nfs4_nontrivial
- file_has_acl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
23 # pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
24 #endif
25
26 #include <config.h>
27
28 #include "acl.h"
29
30 #include "acl-internal.h"
31 #include "attribute.h"
32 #include "minmax.h"
33
34 #if USE_ACL && HAVE_LINUX_XATTR_H && HAVE_LISTXATTR
35 # include <stdckdint.h>
36 # include <string.h>
37 # include <arpa/inet.h>
38 # include <sys/xattr.h>
39 # include <linux/xattr.h>
40 # ifndef XATTR_NAME_NFSV4_ACL
41 # define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
42 # endif
43 # ifndef XATTR_NAME_POSIX_ACL_ACCESS
44 # define XATTR_NAME_POSIX_ACL_ACCESS "system.posix_acl_access"
45 # endif
46 # ifndef XATTR_NAME_POSIX_ACL_DEFAULT
47 # define XATTR_NAME_POSIX_ACL_DEFAULT "system.posix_acl_default"
48 # endif
49
50 enum {
51
52 ACE4_ACCESS_DENIED_ACE_TYPE = 0x00000001,
53 ACE4_IDENTIFIER_GROUP = 0x00000040
54 };
55
56
57
58
59 ATTRIBUTE_PURE static bool
60 have_xattr (char const *attr, char const *listbuf, ssize_t listsize)
61 {
62 char const *blim = listbuf + listsize;
63 for (char const *b = listbuf; b < blim; b += strlen (b) + 1)
64 for (char const *a = attr; *a == *b; a++, b++)
65 if (!*a)
66 return true;
67 return false;
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82 static int
83 acl_nfs4_nontrivial (uint32_t *xattr, ssize_t nbytes)
84 {
85 enum { BYTES_PER_NETWORK_UINT = 4};
86
87
88 nbytes -= BYTES_PER_NETWORK_UINT;
89 if (nbytes < 0)
90 return -1;
91 uint32_t num_aces = ntohl (*xattr++);
92 if (6 < num_aces)
93 return 1;
94 int ace_found = 0;
95
96 for (int ace_n = 0; ace_n < num_aces; ace_n++)
97 {
98
99
100 nbytes -= 4 * BYTES_PER_NETWORK_UINT;
101 if (nbytes < 0)
102 return -1;
103 uint32_t type = ntohl (xattr[0]);
104 uint32_t flag = ntohl (xattr[1]);
105 uint32_t wholen = ntohl (xattr[3]);
106 xattr += 4;
107 int whowords = (wholen / BYTES_PER_NETWORK_UINT
108 + (wholen % BYTES_PER_NETWORK_UINT != 0));
109 int64_t wholen4 = whowords;
110 wholen4 *= BYTES_PER_NETWORK_UINT;
111
112
113
114 if (ACE4_ACCESS_DENIED_ACE_TYPE < type)
115 return 1;
116
117
118
119 if (flag & ~ACE4_IDENTIFIER_GROUP)
120 return 1;
121
122
123
124 if (nbytes - wholen4 < 0)
125 return -1;
126 nbytes -= wholen4;
127
128
129
130 int who2
131 = (wholen == 6 && memcmp (xattr, "OWNER@", 6) == 0 ? 0
132 : wholen == 6 && memcmp (xattr, "GROUP@", 6) == 0 ? 2
133 : wholen == 9 && memcmp (xattr, "EVERYONE@", 9) == 0 ? 4
134 : -1);
135 if (who2 < 0)
136 return 1;
137 int ace_found_bit = 1 << (who2 | type);
138 if (ace_found & ace_found_bit)
139 return 1;
140 ace_found |= ace_found_bit;
141
142 xattr += whowords;
143 }
144
145 return 0;
146 }
147 #endif
148
149
150
151
152
153
154
155
156 int
157 file_has_acl (char const *name, struct stat const *sb)
158 {
159 #if USE_ACL
160 if (! S_ISLNK (sb->st_mode))
161 {
162
163 # if HAVE_LINUX_XATTR_H && HAVE_LISTXATTR
164 int initial_errno = errno;
165
166
167
168
169
170
171
172 typedef uint32_t trivial_NFSv4_xattr_buf[2 * (6 + 6 + 7)];
173
174
175
176 union {
177 trivial_NFSv4_xattr_buf xattr;
178 char ch[sizeof (trivial_NFSv4_xattr_buf)];
179 } stackbuf;
180
181 char *listbuf = stackbuf.ch;
182 ssize_t listbufsize = sizeof stackbuf.ch;
183 char *heapbuf = NULL;
184 ssize_t listsize;
185
186
187
188
189 while ((listsize = listxattr (name, listbuf, listbufsize)) < 0
190 && errno == ERANGE)
191 {
192 free (heapbuf);
193 ssize_t newsize = listxattr (name, NULL, 0);
194 if (newsize <= 0)
195 return newsize;
196
197
198
199
200 bool overflow = ckd_add (&listbufsize, listbufsize, listbufsize >> 1);
201 listbufsize = MAX (listbufsize, newsize);
202 if (overflow || SIZE_MAX < listbufsize)
203 {
204 errno = ENOMEM;
205 return -1;
206 }
207
208 listbuf = heapbuf = malloc (listbufsize);
209 if (!listbuf)
210 return -1;
211 }
212
213
214
215
216
217 bool nfsv4_acl
218 = 0 < listsize && have_xattr (XATTR_NAME_NFSV4_ACL, listbuf, listsize);
219 int ret
220 = (listsize <= 0 ? listsize
221 : (nfsv4_acl
222 || have_xattr (XATTR_NAME_POSIX_ACL_ACCESS, listbuf, listsize)
223 || (S_ISDIR (sb->st_mode)
224 && have_xattr (XATTR_NAME_POSIX_ACL_DEFAULT,
225 listbuf, listsize))));
226 free (heapbuf);
227
228
229
230 if (nfsv4_acl)
231 {
232 ret = getxattr (name, XATTR_NAME_NFSV4_ACL,
233 stackbuf.xattr, sizeof stackbuf.xattr);
234 if (ret < 0)
235 switch (errno)
236 {
237 case ENODATA: return 0;
238 case ERANGE : return 1;
239 }
240 else
241 {
242
243 ret = acl_nfs4_nontrivial (stackbuf.xattr, ret);
244 if (ret < 0)
245 {
246 errno = EINVAL;
247 return ret;
248 }
249 errno = initial_errno;
250 }
251 }
252 if (ret < 0)
253 return - acl_errno_valid (errno);
254 return ret;
255
256 # elif HAVE_ACL_GET_FILE
257
258
259
260 int ret;
261
262 if (HAVE_ACL_EXTENDED_FILE)
263 {
264
265
266
267 ret = acl_extended_file (name);
268 }
269 else
270 {
271 # if HAVE_ACL_TYPE_EXTENDED
272
273
274
275
276
277 acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
278 if (acl)
279 {
280 ret = acl_extended_nontrivial (acl);
281 acl_free (acl);
282 }
283 else
284 ret = -1;
285 # else
286 acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
287 if (acl)
288 {
289 int saved_errno;
290
291 ret = acl_access_nontrivial (acl);
292 saved_errno = errno;
293 acl_free (acl);
294 errno = saved_errno;
295 # if HAVE_ACL_FREE_TEXT
296
297
298
299 # else
300
301
302
303
304
305 if (ret == 0 && S_ISDIR (sb->st_mode))
306 {
307 acl = acl_get_file (name, ACL_TYPE_DEFAULT);
308 if (acl)
309 {
310 # ifdef __CYGWIN__
311 ret = acl_access_nontrivial (acl);
312 saved_errno = errno;
313 acl_free (acl);
314 errno = saved_errno;
315 # else
316 ret = (0 < acl_entries (acl));
317 acl_free (acl);
318 # endif
319 }
320 else
321 ret = -1;
322 }
323 # endif
324 }
325 else
326 ret = -1;
327 # endif
328 }
329 if (ret < 0)
330 return - acl_errno_valid (errno);
331 return ret;
332
333 # elif HAVE_FACL && defined GETACL
334
335 # if defined ACL_NO_TRIVIAL
336
337
338
339
340 return acl_trivial (name);
341
342 # else
343
344
345
346
347 {
348
349
350 enum
351 {
352 alloc_init = 4000 / sizeof (aclent_t),
353 alloc_max = MIN (INT_MAX, SIZE_MAX / sizeof (aclent_t))
354 };
355 aclent_t buf[alloc_init];
356 size_t alloc = alloc_init;
357 aclent_t *entries = buf;
358 aclent_t *malloced = NULL;
359 int count;
360
361 for (;;)
362 {
363 count = acl (name, GETACL, alloc, entries);
364 if (count < 0 && errno == ENOSPC)
365 {
366
367 free (malloced);
368 if (alloc > alloc_max / 2)
369 {
370 errno = ENOMEM;
371 return -1;
372 }
373 alloc = 2 * alloc;
374 entries = malloced =
375 (aclent_t *) malloc (alloc * sizeof (aclent_t));
376 if (entries == NULL)
377 {
378 errno = ENOMEM;
379 return -1;
380 }
381 continue;
382 }
383 break;
384 }
385 if (count < 0)
386 {
387 if (errno == ENOSYS || errno == ENOTSUP)
388 ;
389 else
390 {
391 free (malloced);
392 return -1;
393 }
394 }
395 else if (count == 0)
396 ;
397 else
398 {
399
400
401
402
403 if (count > 4)
404 {
405 free (malloced);
406 return 1;
407 }
408
409 if (acl_nontrivial (count, entries))
410 {
411 free (malloced);
412 return 1;
413 }
414 }
415 free (malloced);
416 }
417
418 # ifdef ACE_GETACL
419
420
421 {
422
423
424 enum
425 {
426 alloc_init = 4000 / sizeof (ace_t),
427 alloc_max = MIN (INT_MAX, SIZE_MAX / sizeof (ace_t))
428 };
429 ace_t buf[alloc_init];
430 size_t alloc = alloc_init;
431 ace_t *entries = buf;
432 ace_t *malloced = NULL;
433 int count;
434
435 for (;;)
436 {
437 count = acl (name, ACE_GETACL, alloc, entries);
438 if (count < 0 && errno == ENOSPC)
439 {
440
441 free (malloced);
442 if (alloc > alloc_max / 2)
443 {
444 errno = ENOMEM;
445 return -1;
446 }
447 alloc = 2 * alloc;
448 entries = malloced = (ace_t *) malloc (alloc * sizeof (ace_t));
449 if (entries == NULL)
450 {
451 errno = ENOMEM;
452 return -1;
453 }
454 continue;
455 }
456 break;
457 }
458 if (count < 0)
459 {
460 if (errno == ENOSYS || errno == EINVAL)
461 ;
462 else
463 {
464 free (malloced);
465 return -1;
466 }
467 }
468 else if (count == 0)
469 ;
470 else
471 {
472
473
474
475
476
477
478
479
480 if (count > 6)
481 {
482 free (malloced);
483 return 1;
484 }
485
486 if (acl_ace_nontrivial (count, entries))
487 {
488 free (malloced);
489 return 1;
490 }
491 }
492 free (malloced);
493 }
494 # endif
495
496 return 0;
497 # endif
498
499 # elif HAVE_GETACL
500
501 {
502 struct acl_entry entries[NACLENTRIES];
503 int count;
504
505 count = getacl (name, NACLENTRIES, entries);
506
507 if (count < 0)
508 {
509
510
511
512 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP)
513 ;
514 else
515 return -1;
516 }
517 else if (count == 0)
518 return 0;
519 else
520 {
521 if (count > NACLENTRIES)
522
523
524 abort ();
525
526
527
528 if (count > 3)
529 return 1;
530
531 {
532 struct stat statbuf;
533
534 if (stat (name, &statbuf) == -1 && errno != EOVERFLOW)
535 return -1;
536
537 return acl_nontrivial (count, entries);
538 }
539 }
540 }
541
542 # if HAVE_ACLV_H
543
544 {
545 struct acl entries[NACLVENTRIES];
546 int count;
547
548 count = acl ((char *) name, ACL_GET, NACLVENTRIES, entries);
549
550 if (count < 0)
551 {
552
553
554 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
555 ;
556 else
557 return -1;
558 }
559 else if (count == 0)
560 return 0;
561 else
562 {
563 if (count > NACLVENTRIES)
564
565
566 abort ();
567
568
569
570 if (count > 4)
571 return 1;
572
573 return aclv_nontrivial (count, entries);
574 }
575 }
576
577 # endif
578
579 # elif HAVE_ACLX_GET && defined ACL_AIX_WIP
580
581 acl_type_t type;
582 char aclbuf[1024];
583 void *acl = aclbuf;
584 size_t aclsize = sizeof (aclbuf);
585 mode_t mode;
586
587 for (;;)
588 {
589
590
591 type.u64 = ACL_ANY;
592 if (aclx_get (name, 0, &type, aclbuf, &aclsize, &mode) >= 0)
593 break;
594 if (errno == ENOSYS)
595 return 0;
596 if (errno != ENOSPC)
597 {
598 if (acl != aclbuf)
599 free (acl);
600 return -1;
601 }
602 aclsize = 2 * aclsize;
603 if (acl != aclbuf)
604 free (acl);
605 acl = malloc (aclsize);
606 if (acl == NULL)
607 {
608 errno = ENOMEM;
609 return -1;
610 }
611 }
612
613 if (type.u64 == ACL_AIXC)
614 {
615 int result = acl_nontrivial ((struct acl *) acl);
616 if (acl != aclbuf)
617 free (acl);
618 return result;
619 }
620 else if (type.u64 == ACL_NFS4)
621 {
622 int result = acl_nfs4_nontrivial ((nfs4_acl_int_t *) acl);
623 if (acl != aclbuf)
624 free (acl);
625 return result;
626 }
627 else
628 {
629
630
631 if (acl != aclbuf)
632 free (acl);
633 errno = EINVAL;
634 return -1;
635 }
636
637 # elif HAVE_STATACL
638
639 union { struct acl a; char room[4096]; } u;
640
641 if (statacl ((char *) name, STX_NORMAL, &u.a, sizeof (u)) < 0)
642 return -1;
643
644 return acl_nontrivial (&u.a);
645
646 # elif HAVE_ACLSORT
647
648 {
649 struct acl entries[NACLENTRIES];
650 int count;
651
652 count = acl ((char *) name, ACL_GET, NACLENTRIES, entries);
653
654 if (count < 0)
655 {
656 if (errno == ENOSYS || errno == ENOTSUP)
657 ;
658 else
659 return -1;
660 }
661 else if (count == 0)
662 return 0;
663 else
664 {
665 if (count > NACLENTRIES)
666
667
668 abort ();
669
670
671
672 if (count > 4)
673 return 1;
674
675 return acl_nontrivial (count, entries);
676 }
677 }
678
679 # endif
680 }
681 #endif
682
683 return 0;
684 }