Fix some minor races in hosts lacking mkostemp.
[bpt/emacs.git] / lib / file-has-acl.c
1 /* Test whether a file has a nontrivial access control list.
2
3 Copyright (C) 2002-2003, 2005-2013 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 Written by Paul Eggert, Andreas Grünbacher, and Bruno Haible. */
19
20 /* Without this pragma, gcc 4.7.0 20120126 may suggest that the
21 file_has_acl function might be candidate for attribute 'const' */
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
32
33 #if USE_ACL && HAVE_ACL_GET_FILE
34
35 # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
36
37 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
38 Return 1 if the given ACL is non-trivial.
39 Return 0 if it is trivial. */
40 int
41 acl_extended_nontrivial (acl_t acl)
42 {
43 /* acl is non-trivial if it is non-empty. */
44 return (acl_entries (acl) > 0);
45 }
46
47 # else /* Linux, FreeBSD, IRIX, Tru64 */
48
49 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
50 Return 1 if the given ACL is non-trivial.
51 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
52 Return -1 and set errno upon failure to determine it. */
53 int
54 acl_access_nontrivial (acl_t acl)
55 {
56 /* acl is non-trivial if it has some entries other than for "user::",
57 "group::", and "other::". Normally these three should be present
58 at least, allowing us to write
59 return (3 < acl_entries (acl));
60 but the following code is more robust. */
61 # if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD */
62
63 acl_entry_t ace;
64 int got_one;
65
66 for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
67 got_one > 0;
68 got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
69 {
70 acl_tag_t tag;
71 if (acl_get_tag_type (ace, &tag) < 0)
72 return -1;
73 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
74 return 1;
75 }
76 return got_one;
77
78 # else /* IRIX, Tru64 */
79 # if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
80 /* Don't use acl_get_entry: it is undocumented. */
81
82 int count = acl->acl_cnt;
83 int i;
84
85 for (i = 0; i < count; i++)
86 {
87 acl_entry_t ace = &acl->acl_entry[i];
88 acl_tag_t tag = ace->ae_tag;
89
90 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
91 || tag == ACL_OTHER_OBJ))
92 return 1;
93 }
94 return 0;
95
96 # endif
97 # if HAVE_ACL_FREE_TEXT /* Tru64 */
98 /* Don't use acl_get_entry: it takes only one argument and does not work. */
99
100 int count = acl->acl_num;
101 acl_entry_t ace;
102
103 for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
104 {
105 acl_tag_t tag;
106 acl_perm_t perm;
107
108 tag = ace->entry->acl_type;
109 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
110 return 1;
111
112 perm = ace->entry->acl_perm;
113 /* On Tru64, perm can also contain non-standard bits such as
114 PERM_INSERT, PERM_DELETE, PERM_MODIFY, PERM_LOOKUP, ... */
115 if ((perm & ~(ACL_READ | ACL_WRITE | ACL_EXECUTE)) != 0)
116 return 1;
117 }
118 return 0;
119
120 # endif
121 # endif
122 }
123
124 # endif
125
126
127 #elif USE_ACL && HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
128
129 /* Test an ACL retrieved with GETACL.
130 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
131 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
132 int
133 acl_nontrivial (int count, aclent_t *entries)
134 {
135 int i;
136
137 for (i = 0; i < count; i++)
138 {
139 aclent_t *ace = &entries[i];
140
141 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
142 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
143 We don't need to check ace->a_id in these cases. */
144 if (!(ace->a_type == USER_OBJ
145 || ace->a_type == GROUP_OBJ
146 || ace->a_type == OTHER_OBJ
147 /* Note: Cygwin does not return a CLASS_OBJ ("mask:") entry
148 sometimes. */
149 || ace->a_type == CLASS_OBJ))
150 return 1;
151 }
152 return 0;
153 }
154
155 # ifdef ACE_GETACL
156
157 /* A shortcut for a bitmask. */
158 # define NEW_ACE_WRITEA_DATA (NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA)
159
160 /* Test an ACL retrieved with ACE_GETACL.
161 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
162 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
163 int
164 acl_ace_nontrivial (int count, ace_t *entries)
165 {
166 int i;
167
168 /* The flags in the ace_t structure changed in a binary incompatible way
169 when ACL_NO_TRIVIAL etc. were introduced in <sys/acl.h> version 1.15.
170 How to distinguish the two conventions at runtime?
171 In the old convention, usually three ACEs have a_flags = ACE_OWNER /
172 ACE_GROUP / ACE_OTHER, in the range 0x0100..0x0400. In the new
173 convention, these values are not used. */
174 int old_convention = 0;
175
176 for (i = 0; i < count; i++)
177 if (entries[i].a_flags & (OLD_ACE_OWNER | OLD_ACE_GROUP | OLD_ACE_OTHER))
178 {
179 old_convention = 1;
180 break;
181 }
182
183 if (old_convention)
184 /* Running on Solaris 10. */
185 for (i = 0; i < count; i++)
186 {
187 ace_t *ace = &entries[i];
188
189 /* Note:
190 If ace->a_flags = ACE_OWNER, ace->a_who is the st_uid from stat().
191 If ace->a_flags = ACE_GROUP, ace->a_who is the st_gid from stat().
192 We don't need to check ace->a_who in these cases. */
193 if (!(ace->a_type == OLD_ALLOW
194 && (ace->a_flags == OLD_ACE_OWNER
195 || ace->a_flags == OLD_ACE_GROUP
196 || ace->a_flags == OLD_ACE_OTHER)))
197 return 1;
198 }
199 else
200 {
201 /* Running on Solaris 10 (newer version) or Solaris 11. */
202 unsigned int access_masks[6] =
203 {
204 0, /* owner@ deny */
205 0, /* owner@ allow */
206 0, /* group@ deny */
207 0, /* group@ allow */
208 0, /* everyone@ deny */
209 0 /* everyone@ allow */
210 };
211
212 for (i = 0; i < count; i++)
213 {
214 ace_t *ace = &entries[i];
215 unsigned int index1;
216 unsigned int index2;
217
218 if (ace->a_type == NEW_ACE_ACCESS_ALLOWED_ACE_TYPE)
219 index1 = 1;
220 else if (ace->a_type == NEW_ACE_ACCESS_DENIED_ACE_TYPE)
221 index1 = 0;
222 else
223 return 1;
224
225 if (ace->a_flags == NEW_ACE_OWNER)
226 index2 = 0;
227 else if (ace->a_flags == (NEW_ACE_GROUP | NEW_ACE_IDENTIFIER_GROUP))
228 index2 = 2;
229 else if (ace->a_flags == NEW_ACE_EVERYONE)
230 index2 = 4;
231 else
232 return 1;
233
234 access_masks[index1 + index2] |= ace->a_access_mask;
235 }
236
237 /* The same bit shouldn't be both allowed and denied. */
238 if (access_masks[0] & access_masks[1])
239 return 1;
240 if (access_masks[2] & access_masks[3])
241 return 1;
242 if (access_masks[4] & access_masks[5])
243 return 1;
244
245 /* Check minimum masks. */
246 if ((NEW_ACE_WRITE_NAMED_ATTRS
247 | NEW_ACE_WRITE_ATTRIBUTES
248 | NEW_ACE_WRITE_ACL
249 | NEW_ACE_WRITE_OWNER)
250 & ~ access_masks[1])
251 return 1;
252 access_masks[1] &= ~(NEW_ACE_WRITE_NAMED_ATTRS
253 | NEW_ACE_WRITE_ATTRIBUTES
254 | NEW_ACE_WRITE_ACL
255 | NEW_ACE_WRITE_OWNER);
256 if ((NEW_ACE_READ_NAMED_ATTRS
257 | NEW_ACE_READ_ATTRIBUTES
258 | NEW_ACE_READ_ACL
259 | NEW_ACE_SYNCHRONIZE)
260 & ~ access_masks[5])
261 return 1;
262 access_masks[5] &= ~(NEW_ACE_READ_NAMED_ATTRS
263 | NEW_ACE_READ_ATTRIBUTES
264 | NEW_ACE_READ_ACL
265 | NEW_ACE_SYNCHRONIZE);
266
267 /* Check the allowed or denied bits. */
268 switch ((access_masks[0] | access_masks[1])
269 & ~(NEW_ACE_READ_NAMED_ATTRS
270 | NEW_ACE_READ_ATTRIBUTES
271 | NEW_ACE_READ_ACL
272 | NEW_ACE_SYNCHRONIZE))
273 {
274 case 0:
275 case NEW_ACE_READ_DATA:
276 case NEW_ACE_WRITEA_DATA:
277 case NEW_ACE_READ_DATA | NEW_ACE_WRITEA_DATA:
278 case NEW_ACE_EXECUTE:
279 case NEW_ACE_READ_DATA | NEW_ACE_EXECUTE:
280 case NEW_ACE_WRITEA_DATA | NEW_ACE_EXECUTE:
281 case NEW_ACE_READ_DATA | NEW_ACE_WRITEA_DATA | NEW_ACE_EXECUTE:
282 break;
283 default:
284 return 1;
285 }
286 switch ((access_masks[2] | access_masks[3])
287 & ~(NEW_ACE_READ_NAMED_ATTRS
288 | NEW_ACE_READ_ATTRIBUTES
289 | NEW_ACE_READ_ACL
290 | NEW_ACE_SYNCHRONIZE))
291 {
292 case 0:
293 case NEW_ACE_READ_DATA:
294 case NEW_ACE_WRITEA_DATA:
295 case NEW_ACE_READ_DATA | NEW_ACE_WRITEA_DATA:
296 case NEW_ACE_EXECUTE:
297 case NEW_ACE_READ_DATA | NEW_ACE_EXECUTE:
298 case NEW_ACE_WRITEA_DATA | NEW_ACE_EXECUTE:
299 case NEW_ACE_READ_DATA | NEW_ACE_WRITEA_DATA | NEW_ACE_EXECUTE:
300 break;
301 default:
302 return 1;
303 }
304 switch ((access_masks[4] | access_masks[5])
305 & ~(NEW_ACE_WRITE_NAMED_ATTRS
306 | NEW_ACE_WRITE_ATTRIBUTES
307 | NEW_ACE_WRITE_ACL
308 | NEW_ACE_WRITE_OWNER))
309 {
310 case 0:
311 case NEW_ACE_READ_DATA:
312 case NEW_ACE_WRITEA_DATA:
313 case NEW_ACE_READ_DATA | NEW_ACE_WRITEA_DATA:
314 case NEW_ACE_EXECUTE:
315 case NEW_ACE_READ_DATA | NEW_ACE_EXECUTE:
316 case NEW_ACE_WRITEA_DATA | NEW_ACE_EXECUTE:
317 case NEW_ACE_READ_DATA | NEW_ACE_WRITEA_DATA | NEW_ACE_EXECUTE:
318 break;
319 default:
320 return 1;
321 }
322
323 /* Check that the NEW_ACE_WRITE_DATA and NEW_ACE_APPEND_DATA bits are
324 either both allowed or both denied. */
325 if (((access_masks[0] & NEW_ACE_WRITE_DATA) != 0)
326 != ((access_masks[0] & NEW_ACE_APPEND_DATA) != 0))
327 return 1;
328 if (((access_masks[2] & NEW_ACE_WRITE_DATA) != 0)
329 != ((access_masks[2] & NEW_ACE_APPEND_DATA) != 0))
330 return 1;
331 if (((access_masks[4] & NEW_ACE_WRITE_DATA) != 0)
332 != ((access_masks[4] & NEW_ACE_APPEND_DATA) != 0))
333 return 1;
334 }
335
336 return 0;
337 }
338
339 # endif
340
341 #elif USE_ACL && HAVE_GETACL /* HP-UX */
342
343 /* Return 1 if the given ACL is non-trivial.
344 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
345 int
346 acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb)
347 {
348 int i;
349
350 for (i = 0; i < count; i++)
351 {
352 struct acl_entry *ace = &entries[i];
353
354 if (!((ace->uid == sb->st_uid && ace->gid == ACL_NSGROUP)
355 || (ace->uid == ACL_NSUSER && ace->gid == sb->st_gid)
356 || (ace->uid == ACL_NSUSER && ace->gid == ACL_NSGROUP)))
357 return 1;
358 }
359 return 0;
360 }
361
362 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
363
364 /* Return 1 if the given ACL is non-trivial.
365 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
366 int
367 aclv_nontrivial (int count, struct acl *entries)
368 {
369 int i;
370
371 for (i = 0; i < count; i++)
372 {
373 struct acl *ace = &entries[i];
374
375 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
376 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
377 We don't need to check ace->a_id in these cases. */
378 if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
379 || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
380 || ace->a_type == CLASS_OBJ
381 || ace->a_type == OTHER_OBJ))
382 return 1;
383 }
384 return 0;
385 }
386
387 # endif
388
389 #elif USE_ACL && (HAVE_ACLX_GET || HAVE_STATACL) /* AIX */
390
391 /* Return 1 if the given ACL is non-trivial.
392 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
393 int
394 acl_nontrivial (struct acl *a)
395 {
396 /* The normal way to iterate through an ACL is like this:
397 struct acl_entry *ace;
398 for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
399 {
400 struct ace_id *aei;
401 switch (ace->ace_type)
402 {
403 case ACC_PERMIT:
404 case ACC_DENY:
405 case ACC_SPECIFY:
406 ...;
407 }
408 for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
409 ...
410 }
411 */
412 return (acl_last (a) != a->acl_ext ? 1 : 0);
413 }
414
415 # if HAVE_ACLX_GET && defined ACL_AIX_WIP /* newer AIX */
416
417 /* Return 1 if the given ACL is non-trivial.
418 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
419 int
420 acl_nfs4_nontrivial (nfs4_acl_int_t *a)
421 {
422 # if 1 /* let's try this first */
423 return (a->aclEntryN > 0 ? 1 : 0);
424 # else
425 int count = a->aclEntryN;
426 int i;
427
428 for (i = 0; i < count; i++)
429 {
430 nfs4_ace_int_t *ace = &a->aclEntry[i];
431
432 if (!((ace->flags & ACE4_ID_SPECIAL) != 0
433 && (ace->aceWho.special_whoid == ACE4_WHO_OWNER
434 || ace->aceWho.special_whoid == ACE4_WHO_GROUP
435 || ace->aceWho.special_whoid == ACE4_WHO_EVERYONE)
436 && ace->aceType == ACE4_ACCESS_ALLOWED_ACE_TYPE
437 && ace->aceFlags == 0
438 && (ace->aceMask & ~(ACE4_READ_DATA | ACE4_LIST_DIRECTORY
439 | ACE4_WRITE_DATA | ACE4_ADD_FILE
440 | ACE4_EXECUTE)) == 0))
441 return 1;
442 }
443 return 0;
444 # endif
445 }
446
447 # endif
448
449 #elif USE_ACL && HAVE_ACLSORT /* NonStop Kernel */
450
451 /* Test an ACL retrieved with ACL_GET.
452 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
453 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
454 int
455 acl_nontrivial (int count, struct acl *entries)
456 {
457 int i;
458
459 for (i = 0; i < count; i++)
460 {
461 struct acl *ace = &entries[i];
462
463 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
464 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
465 We don't need to check ace->a_id in these cases. */
466 if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
467 || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
468 || ace->a_type == CLASS_OBJ
469 || ace->a_type == OTHER_OBJ))
470 return 1;
471 }
472 return 0;
473 }
474
475 #endif
476
477
478 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
479 only has no or a base access control list, and -1 (setting errno)
480 on error. SB must be set to the stat buffer of NAME, obtained
481 through stat() or lstat(). */
482
483 int
484 file_has_acl (char const *name, struct stat const *sb)
485 {
486 #if USE_ACL
487 if (! S_ISLNK (sb->st_mode))
488 {
489 # if HAVE_ACL_GET_FILE
490
491 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
492 /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
493 int ret;
494
495 if (HAVE_ACL_EXTENDED_FILE) /* Linux */
496 {
497 /* On Linux, acl_extended_file is an optimized function: It only
498 makes two calls to getxattr(), one for ACL_TYPE_ACCESS, one for
499 ACL_TYPE_DEFAULT. */
500 ret = acl_extended_file (name);
501 }
502 else /* FreeBSD, Mac OS X, IRIX, Tru64 */
503 {
504 # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
505 /* On Mac OS X, acl_get_file (name, ACL_TYPE_ACCESS)
506 and acl_get_file (name, ACL_TYPE_DEFAULT)
507 always return NULL / EINVAL. There is no point in making
508 these two useless calls. The real ACL is retrieved through
509 acl_get_file (name, ACL_TYPE_EXTENDED). */
510 acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
511 if (acl)
512 {
513 ret = acl_extended_nontrivial (acl);
514 acl_free (acl);
515 }
516 else
517 ret = -1;
518 # else /* FreeBSD, IRIX, Tru64 */
519 acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
520 if (acl)
521 {
522 int saved_errno;
523
524 ret = acl_access_nontrivial (acl);
525 saved_errno = errno;
526 acl_free (acl);
527 errno = saved_errno;
528 # if HAVE_ACL_FREE_TEXT /* Tru64 */
529 /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
530 returns NULL with errno not set. There is no point in
531 making this call. */
532 # else /* FreeBSD, IRIX */
533 /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
534 and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
535 either both succeed or both fail; it depends on the
536 file system. Therefore there is no point in making the second
537 call if the first one already failed. */
538 if (ret == 0 && S_ISDIR (sb->st_mode))
539 {
540 acl = acl_get_file (name, ACL_TYPE_DEFAULT);
541 if (acl)
542 {
543 ret = (0 < acl_entries (acl));
544 acl_free (acl);
545 }
546 else
547 ret = -1;
548 }
549 # endif
550 }
551 else
552 ret = -1;
553 # endif
554 }
555 if (ret < 0)
556 return - acl_errno_valid (errno);
557 return ret;
558
559 # elif HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
560
561 # if defined ACL_NO_TRIVIAL
562
563 /* Solaris 10 (newer version), which has additional API declared in
564 <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
565 acl_fromtext, ...). */
566 return acl_trivial (name);
567
568 # else /* Solaris, Cygwin, general case */
569
570 /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
571 of Unixware. The acl() call returns the access and default ACL both
572 at once. */
573 {
574 /* Initially, try to read the entries into a stack-allocated buffer.
575 Use malloc if it does not fit. */
576 enum
577 {
578 alloc_init = 4000 / sizeof (aclent_t), /* >= 3 */
579 alloc_max = MIN (INT_MAX, SIZE_MAX / sizeof (aclent_t))
580 };
581 aclent_t buf[alloc_init];
582 size_t alloc = alloc_init;
583 aclent_t *entries = buf;
584 aclent_t *malloced = NULL;
585 int count;
586
587 for (;;)
588 {
589 count = acl (name, GETACL, alloc, entries);
590 if (count < 0 && errno == ENOSPC)
591 {
592 /* Increase the size of the buffer. */
593 free (malloced);
594 if (alloc > alloc_max / 2)
595 {
596 errno = ENOMEM;
597 return -1;
598 }
599 alloc = 2 * alloc; /* <= alloc_max */
600 entries = malloced =
601 (aclent_t *) malloc (alloc * sizeof (aclent_t));
602 if (entries == NULL)
603 {
604 errno = ENOMEM;
605 return -1;
606 }
607 continue;
608 }
609 break;
610 }
611 if (count < 0)
612 {
613 if (errno == ENOSYS || errno == ENOTSUP)
614 ;
615 else
616 {
617 int saved_errno = errno;
618 free (malloced);
619 errno = saved_errno;
620 return -1;
621 }
622 }
623 else if (count == 0)
624 ;
625 else
626 {
627 /* Don't use MIN_ACL_ENTRIES: It's set to 4 on Cygwin, but Cygwin
628 returns only 3 entries for files with no ACL. But this is safe:
629 If there are more than 4 entries, there cannot be only the
630 "user::", "group::", "other:", and "mask:" entries. */
631 if (count > 4)
632 {
633 free (malloced);
634 return 1;
635 }
636
637 if (acl_nontrivial (count, entries))
638 {
639 free (malloced);
640 return 1;
641 }
642 }
643 free (malloced);
644 }
645
646 # ifdef ACE_GETACL
647 /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
648 file systems (whereas the other ones are used in UFS file systems). */
649 {
650 /* Initially, try to read the entries into a stack-allocated buffer.
651 Use malloc if it does not fit. */
652 enum
653 {
654 alloc_init = 4000 / sizeof (ace_t), /* >= 3 */
655 alloc_max = MIN (INT_MAX, SIZE_MAX / sizeof (ace_t))
656 };
657 ace_t buf[alloc_init];
658 size_t alloc = alloc_init;
659 ace_t *entries = buf;
660 ace_t *malloced = NULL;
661 int count;
662
663 for (;;)
664 {
665 count = acl (name, ACE_GETACL, alloc, entries);
666 if (count < 0 && errno == ENOSPC)
667 {
668 /* Increase the size of the buffer. */
669 free (malloced);
670 if (alloc > alloc_max / 2)
671 {
672 errno = ENOMEM;
673 return -1;
674 }
675 alloc = 2 * alloc; /* <= alloc_max */
676 entries = malloced = (ace_t *) malloc (alloc * sizeof (ace_t));
677 if (entries == NULL)
678 {
679 errno = ENOMEM;
680 return -1;
681 }
682 continue;
683 }
684 break;
685 }
686 if (count < 0)
687 {
688 if (errno == ENOSYS || errno == EINVAL)
689 ;
690 else
691 {
692 int saved_errno = errno;
693 free (malloced);
694 errno = saved_errno;
695 return -1;
696 }
697 }
698 else if (count == 0)
699 ;
700 else
701 {
702 /* In the old (original Solaris 10) convention:
703 If there are more than 3 entries, there cannot be only the
704 ACE_OWNER, ACE_GROUP, ACE_OTHER entries.
705 In the newer Solaris 10 and Solaris 11 convention:
706 If there are more than 6 entries, there cannot be only the
707 ACE_OWNER, ACE_GROUP, ACE_EVERYONE entries, each once with
708 NEW_ACE_ACCESS_ALLOWED_ACE_TYPE and once with
709 NEW_ACE_ACCESS_DENIED_ACE_TYPE. */
710 if (count > 6)
711 {
712 free (malloced);
713 return 1;
714 }
715
716 if (acl_ace_nontrivial (count, entries))
717 {
718 free (malloced);
719 return 1;
720 }
721 }
722 free (malloced);
723 }
724 # endif
725
726 return 0;
727 # endif
728
729 # elif HAVE_GETACL /* HP-UX */
730
731 {
732 struct acl_entry entries[NACLENTRIES];
733 int count;
734
735 count = getacl (name, NACLENTRIES, entries);
736
737 if (count < 0)
738 {
739 /* ENOSYS is seen on newer HP-UX versions.
740 EOPNOTSUPP is typically seen on NFS mounts.
741 ENOTSUP was seen on Quantum StorNext file systems (cvfs). */
742 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP)
743 ;
744 else
745 return -1;
746 }
747 else if (count == 0)
748 return 0;
749 else /* count > 0 */
750 {
751 if (count > NACLENTRIES)
752 /* If NACLENTRIES cannot be trusted, use dynamic memory
753 allocation. */
754 abort ();
755
756 /* If there are more than 3 entries, there cannot be only the
757 (uid,%), (%,gid), (%,%) entries. */
758 if (count > 3)
759 return 1;
760
761 {
762 struct stat statbuf;
763
764 if (stat (name, &statbuf) < 0)
765 return -1;
766
767 return acl_nontrivial (count, entries, &statbuf);
768 }
769 }
770 }
771
772 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
773
774 {
775 struct acl entries[NACLVENTRIES];
776 int count;
777
778 count = acl ((char *) name, ACL_GET, NACLVENTRIES, entries);
779
780 if (count < 0)
781 {
782 /* EOPNOTSUPP is seen on NFS in HP-UX 11.11, 11.23.
783 EINVAL is seen on NFS in HP-UX 11.31. */
784 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
785 ;
786 else
787 return -1;
788 }
789 else if (count == 0)
790 return 0;
791 else /* count > 0 */
792 {
793 if (count > NACLVENTRIES)
794 /* If NACLVENTRIES cannot be trusted, use dynamic memory
795 allocation. */
796 abort ();
797
798 /* If there are more than 4 entries, there cannot be only the
799 four base ACL entries. */
800 if (count > 4)
801 return 1;
802
803 return aclv_nontrivial (count, entries);
804 }
805 }
806
807 # endif
808
809 # elif HAVE_ACLX_GET && defined ACL_AIX_WIP /* AIX */
810
811 acl_type_t type;
812 char aclbuf[1024];
813 void *acl = aclbuf;
814 size_t aclsize = sizeof (aclbuf);
815 mode_t mode;
816
817 for (;;)
818 {
819 /* The docs say that type being 0 is equivalent to ACL_ANY, but it
820 is not true, in AIX 5.3. */
821 type.u64 = ACL_ANY;
822 if (aclx_get (name, 0, &type, aclbuf, &aclsize, &mode) >= 0)
823 break;
824 if (errno == ENOSYS)
825 return 0;
826 if (errno != ENOSPC)
827 {
828 if (acl != aclbuf)
829 {
830 int saved_errno = errno;
831 free (acl);
832 errno = saved_errno;
833 }
834 return -1;
835 }
836 aclsize = 2 * aclsize;
837 if (acl != aclbuf)
838 free (acl);
839 acl = malloc (aclsize);
840 if (acl == NULL)
841 {
842 errno = ENOMEM;
843 return -1;
844 }
845 }
846
847 if (type.u64 == ACL_AIXC)
848 {
849 int result = acl_nontrivial ((struct acl *) acl);
850 if (acl != aclbuf)
851 free (acl);
852 return result;
853 }
854 else if (type.u64 == ACL_NFS4)
855 {
856 int result = acl_nfs4_nontrivial ((nfs4_acl_int_t *) acl);
857 if (acl != aclbuf)
858 free (acl);
859 return result;
860 }
861 else
862 {
863 /* A newer type of ACL has been introduced in the system.
864 We should better support it. */
865 if (acl != aclbuf)
866 free (acl);
867 errno = EINVAL;
868 return -1;
869 }
870
871 # elif HAVE_STATACL /* older AIX */
872
873 union { struct acl a; char room[4096]; } u;
874
875 if (statacl (name, STX_NORMAL, &u.a, sizeof (u)) < 0)
876 return -1;
877
878 return acl_nontrivial (&u.a);
879
880 # elif HAVE_ACLSORT /* NonStop Kernel */
881
882 {
883 struct acl entries[NACLENTRIES];
884 int count;
885
886 count = acl ((char *) name, ACL_GET, NACLENTRIES, entries);
887
888 if (count < 0)
889 {
890 if (errno == ENOSYS || errno == ENOTSUP)
891 ;
892 else
893 return -1;
894 }
895 else if (count == 0)
896 return 0;
897 else /* count > 0 */
898 {
899 if (count > NACLENTRIES)
900 /* If NACLENTRIES cannot be trusted, use dynamic memory
901 allocation. */
902 abort ();
903
904 /* If there are more than 4 entries, there cannot be only the
905 four base ACL entries. */
906 if (count > 4)
907 return 1;
908
909 return acl_nontrivial (count, entries);
910 }
911 }
912
913 # endif
914 }
915 #endif
916
917 return 0;
918 }