1 /* Test whether a file has a nontrivial access control list.
3 Copyright (C) 2002-2003, 2005-2012 Free Software Foundation, Inc.
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.
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.
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/>.
18 Written by Paul Eggert, Andreas Grünbacher, and Bruno Haible. */
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"
30 #include "acl-internal.h"
33 #if USE_ACL && HAVE_ACL_GET_FILE
35 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
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. */
41 acl_extended_nontrivial (acl_t acl)
43 /* acl is non-trivial if it is non-empty. */
44 return (acl_entries (acl) > 0);
47 # else /* Linux, FreeBSD, IRIX, Tru64 */
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. */
54 acl_access_nontrivial (acl_t acl)
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 */
66 for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
68 got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
71 if (acl_get_tag_type (ace, &tag) < 0)
73 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
78 # else /* IRIX, Tru64 */
79 # if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
80 /* Don't use acl_get_entry: it is undocumented. */
82 int count = acl->acl_cnt;
85 for (i = 0; i < count; i++)
87 acl_entry_t ace = &acl->acl_entry[i];
88 acl_tag_t tag = ace->ae_tag;
90 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
91 || tag == ACL_OTHER_OBJ))
97 # if HAVE_ACL_FREE_TEXT /* Tru64 */
98 /* Don't use acl_get_entry: it takes only one argument and does not work. */
100 int count = acl->acl_num;
103 for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
108 tag = ace->entry->acl_type;
109 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
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)
127 #elif USE_ACL && HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
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. */
133 acl_nontrivial (int count, aclent_t *entries)
137 for (i = 0; i < count; i++)
139 aclent_t *ace = &entries[i];
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
149 || ace->a_type == CLASS_OBJ))
157 /* Test an ACL retrieved with ACE_GETACL.
158 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
159 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
161 acl_ace_nontrivial (int count, ace_t *entries)
165 /* The flags in the ace_t structure changed in a binary incompatible way
166 when ACL_NO_TRIVIAL etc. were introduced in <sys/acl.h> version 1.15.
167 How to distinguish the two conventions at runtime?
168 In the old convention, usually three ACEs have a_flags = ACE_OWNER /
169 ACE_GROUP / ACE_OTHER, in the range 0x0100..0x0400. In the new
170 convention, these values are not used. */
171 int old_convention = 0;
173 for (i = 0; i < count; i++)
174 if (entries[i].a_flags & (OLD_ACE_OWNER | OLD_ACE_GROUP | OLD_ACE_OTHER))
181 /* Running on Solaris 10. */
182 for (i = 0; i < count; i++)
184 ace_t *ace = &entries[i];
187 If ace->a_flags = ACE_OWNER, ace->a_who is the st_uid from stat().
188 If ace->a_flags = ACE_GROUP, ace->a_who is the st_gid from stat().
189 We don't need to check ace->a_who in these cases. */
190 if (!(ace->a_type == OLD_ALLOW
191 && (ace->a_flags == OLD_ACE_OWNER
192 || ace->a_flags == OLD_ACE_GROUP
193 || ace->a_flags == OLD_ACE_OTHER)))
198 /* Running on Solaris 10 (newer version) or Solaris 11. */
199 unsigned int access_masks[6] =
202 0, /* owner@ allow */
204 0, /* group@ allow */
205 0, /* everyone@ deny */
206 0 /* everyone@ allow */
209 for (i = 0; i < count; i++)
211 ace_t *ace = &entries[i];
215 if (ace->a_type == NEW_ACE_ACCESS_ALLOWED_ACE_TYPE)
217 else if (ace->a_type == NEW_ACE_ACCESS_DENIED_ACE_TYPE)
222 if (ace->a_flags == NEW_ACE_OWNER)
224 else if (ace->a_flags == (NEW_ACE_GROUP | NEW_ACE_IDENTIFIER_GROUP))
226 else if (ace->a_flags == NEW_ACE_EVERYONE)
231 access_masks[index1 + index2] |= ace->a_access_mask;
234 /* The same bit shouldn't be both allowed and denied. */
235 if (access_masks[0] & access_masks[1])
237 if (access_masks[2] & access_masks[3])
239 if (access_masks[4] & access_masks[5])
242 /* Check minimum masks. */
243 if ((NEW_ACE_WRITE_NAMED_ATTRS
244 | NEW_ACE_WRITE_ATTRIBUTES
246 | NEW_ACE_WRITE_OWNER)
249 access_masks[1] &= ~(NEW_ACE_WRITE_NAMED_ATTRS
250 | NEW_ACE_WRITE_ATTRIBUTES
252 | NEW_ACE_WRITE_OWNER);
253 if ((NEW_ACE_WRITE_NAMED_ATTRS
254 | NEW_ACE_WRITE_ATTRIBUTES
256 | NEW_ACE_WRITE_OWNER)
259 access_masks[4] &= ~(NEW_ACE_WRITE_NAMED_ATTRS
260 | NEW_ACE_WRITE_ATTRIBUTES
262 | NEW_ACE_WRITE_OWNER);
263 if ((NEW_ACE_READ_NAMED_ATTRS
264 | NEW_ACE_READ_ATTRIBUTES
266 | NEW_ACE_SYNCHRONIZE)
269 access_masks[5] &= ~(NEW_ACE_READ_NAMED_ATTRS
270 | NEW_ACE_READ_ATTRIBUTES
272 | NEW_ACE_SYNCHRONIZE);
274 /* Check the allowed or denied bits. */
275 if ((access_masks[0] | access_masks[1])
276 != (NEW_ACE_READ_DATA
277 | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
280 if ((access_masks[2] | access_masks[3])
281 != (NEW_ACE_READ_DATA
282 | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
285 if ((access_masks[4] | access_masks[5])
286 != (NEW_ACE_READ_DATA
287 | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
291 /* Check that the NEW_ACE_WRITE_DATA and NEW_ACE_APPEND_DATA bits are
292 either both allowed or both denied. */
293 if (((access_masks[0] & NEW_ACE_WRITE_DATA) != 0)
294 != ((access_masks[0] & NEW_ACE_APPEND_DATA) != 0))
296 if (((access_masks[2] & NEW_ACE_WRITE_DATA) != 0)
297 != ((access_masks[2] & NEW_ACE_APPEND_DATA) != 0))
299 if (((access_masks[4] & NEW_ACE_WRITE_DATA) != 0)
300 != ((access_masks[4] & NEW_ACE_APPEND_DATA) != 0))
309 #elif USE_ACL && HAVE_GETACL /* HP-UX */
311 /* Return 1 if the given ACL is non-trivial.
312 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
314 acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb)
318 for (i = 0; i < count; i++)
320 struct acl_entry *ace = &entries[i];
322 if (!((ace->uid == sb->st_uid && ace->gid == ACL_NSGROUP)
323 || (ace->uid == ACL_NSUSER && ace->gid == sb->st_gid)
324 || (ace->uid == ACL_NSUSER && ace->gid == ACL_NSGROUP)))
330 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
332 /* Return 1 if the given ACL is non-trivial.
333 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
335 aclv_nontrivial (int count, struct acl *entries)
339 for (i = 0; i < count; i++)
341 struct acl *ace = &entries[i];
343 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
344 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
345 We don't need to check ace->a_id in these cases. */
346 if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
347 || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
348 || ace->a_type == CLASS_OBJ
349 || ace->a_type == OTHER_OBJ))
357 #elif USE_ACL && (HAVE_ACLX_GET || HAVE_STATACL) /* AIX */
359 /* Return 1 if the given ACL is non-trivial.
360 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
362 acl_nontrivial (struct acl *a)
364 /* The normal way to iterate through an ACL is like this:
365 struct acl_entry *ace;
366 for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
369 switch (ace->ace_type)
376 for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
380 return (acl_last (a) != a->acl_ext ? 1 : 0);
383 # if HAVE_ACLX_GET && defined ACL_AIX_WIP /* newer AIX */
385 /* Return 1 if the given ACL is non-trivial.
386 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
388 acl_nfs4_nontrivial (nfs4_acl_int_t *a)
390 # if 1 /* let's try this first */
391 return (a->aclEntryN > 0 ? 1 : 0);
393 int count = a->aclEntryN;
396 for (i = 0; i < count; i++)
398 nfs4_ace_int_t *ace = &a->aclEntry[i];
400 if (!((ace->flags & ACE4_ID_SPECIAL) != 0
401 && (ace->aceWho.special_whoid == ACE4_WHO_OWNER
402 || ace->aceWho.special_whoid == ACE4_WHO_GROUP
403 || ace->aceWho.special_whoid == ACE4_WHO_EVERYONE)
404 && ace->aceType == ACE4_ACCESS_ALLOWED_ACE_TYPE
405 && ace->aceFlags == 0
406 && (ace->aceMask & ~(ACE4_READ_DATA | ACE4_LIST_DIRECTORY
407 | ACE4_WRITE_DATA | ACE4_ADD_FILE
408 | ACE4_EXECUTE)) == 0))
417 #elif USE_ACL && HAVE_ACLSORT /* NonStop Kernel */
419 /* Test an ACL retrieved with ACL_GET.
420 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
421 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
423 acl_nontrivial (int count, struct acl *entries)
427 for (i = 0; i < count; i++)
429 struct acl *ace = &entries[i];
431 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
432 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
433 We don't need to check ace->a_id in these cases. */
434 if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
435 || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
436 || ace->a_type == CLASS_OBJ
437 || ace->a_type == OTHER_OBJ))
446 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
447 only has no or a base access control list, and -1 (setting errno)
448 on error. SB must be set to the stat buffer of NAME, obtained
449 through stat() or lstat(). */
452 file_has_acl (char const *name, struct stat const *sb)
455 if (! S_ISLNK (sb->st_mode))
457 # if HAVE_ACL_GET_FILE
459 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
460 /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
463 if (HAVE_ACL_EXTENDED_FILE) /* Linux */
465 /* On Linux, acl_extended_file is an optimized function: It only
466 makes two calls to getxattr(), one for ACL_TYPE_ACCESS, one for
468 ret = acl_extended_file (name);
470 else /* FreeBSD, MacOS X, IRIX, Tru64 */
472 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
473 /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
474 and acl_get_file (name, ACL_TYPE_DEFAULT)
475 always return NULL / EINVAL. There is no point in making
476 these two useless calls. The real ACL is retrieved through
477 acl_get_file (name, ACL_TYPE_EXTENDED). */
478 acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
481 ret = acl_extended_nontrivial (acl);
486 # else /* FreeBSD, IRIX, Tru64 */
487 acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
492 ret = acl_access_nontrivial (acl);
496 # if HAVE_ACL_FREE_TEXT /* Tru64 */
497 /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
498 returns NULL with errno not set. There is no point in
500 # else /* FreeBSD, IRIX */
501 /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
502 and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
503 either both succeed or both fail; it depends on the
504 file system. Therefore there is no point in making the second
505 call if the first one already failed. */
506 if (ret == 0 && S_ISDIR (sb->st_mode))
508 acl = acl_get_file (name, ACL_TYPE_DEFAULT);
511 ret = (0 < acl_entries (acl));
524 return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
527 # elif HAVE_FACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
529 # if defined ACL_NO_TRIVIAL
531 /* Solaris 10 (newer version), which has additional API declared in
532 <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
533 acl_fromtext, ...). */
534 return acl_trivial (name);
536 # else /* Solaris, Cygwin, general case */
538 /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
539 of Unixware. The acl() call returns the access and default ACL both
547 count = acl (name, GETACLCNT, 0, NULL);
551 if (errno == ENOSYS || errno == ENOTSUP)
560 /* Don't use MIN_ACL_ENTRIES: It's set to 4 on Cygwin, but Cygwin
561 returns only 3 entries for files with no ACL. But this is safe:
562 If there are more than 4 entries, there cannot be only the
563 "user::", "group::", "other:", and "mask:" entries. */
567 entries = (aclent_t *) malloc (count * sizeof (aclent_t));
573 if (acl (name, GETACL, count, entries) == count)
575 if (acl_nontrivial (count, entries))
583 /* Huh? The number of ACL entries changed since the last call.
590 /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
591 file systems (whereas the other ones are used in UFS file systems). */
597 count = acl (name, ACE_GETACLCNT, 0, NULL);
601 if (errno == ENOSYS || errno == EINVAL)
610 /* In the old (original Solaris 10) convention:
611 If there are more than 3 entries, there cannot be only the
612 ACE_OWNER, ACE_GROUP, ACE_OTHER entries.
613 In the newer Solaris 10 and Solaris 11 convention:
614 If there are more than 6 entries, there cannot be only the
615 ACE_OWNER, ACE_GROUP, ACE_EVERYONE entries, each once with
616 NEW_ACE_ACCESS_ALLOWED_ACE_TYPE and once with
617 NEW_ACE_ACCESS_DENIED_ACE_TYPE. */
621 entries = (ace_t *) malloc (count * sizeof (ace_t));
627 if (acl (name, ACE_GETACL, count, entries) == count)
629 if (acl_ace_nontrivial (count, entries))
637 /* Huh? The number of ACL entries changed since the last call.
647 # elif HAVE_GETACL /* HP-UX */
652 struct acl_entry entries[NACLENTRIES];
654 count = getacl (name, 0, NULL);
658 /* ENOSYS is seen on newer HP-UX versions.
659 EOPNOTSUPP is typically seen on NFS mounts.
660 ENOTSUP was seen on Quantum StorNext file systems (cvfs). */
661 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP)
670 if (count > NACLENTRIES)
671 /* If NACLENTRIES cannot be trusted, use dynamic memory
675 /* If there are more than 3 entries, there cannot be only the
676 (uid,%), (%,gid), (%,%) entries. */
680 if (getacl (name, count, entries) == count)
684 if (stat (name, &statbuf) < 0)
687 return acl_nontrivial (count, entries, &statbuf);
689 /* Huh? The number of ACL entries changed since the last call.
693 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
698 struct acl entries[NACLVENTRIES];
700 count = acl ((char *) name, ACL_CNT, NACLVENTRIES, entries);
704 /* EOPNOTSUPP is seen on NFS in HP-UX 11.11, 11.23.
705 EINVAL is seen on NFS in HP-UX 11.31. */
706 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
715 if (count > NACLVENTRIES)
716 /* If NACLVENTRIES cannot be trusted, use dynamic memory
720 /* If there are more than 4 entries, there cannot be only the
721 four base ACL entries. */
725 if (acl ((char *) name, ACL_GET, count, entries) == count)
726 return aclv_nontrivial (count, entries);
727 /* Huh? The number of ACL entries changed since the last call.
733 # elif HAVE_ACLX_GET && defined ACL_AIX_WIP /* AIX */
738 size_t aclsize = sizeof (aclbuf);
743 /* The docs say that type being 0 is equivalent to ACL_ANY, but it
744 is not true, in AIX 5.3. */
746 if (aclx_get (name, 0, &type, aclbuf, &aclsize, &mode) >= 0)
754 int saved_errno = errno;
760 aclsize = 2 * aclsize;
763 acl = malloc (aclsize);
771 if (type.u64 == ACL_AIXC)
773 int result = acl_nontrivial ((struct acl *) acl);
778 else if (type.u64 == ACL_NFS4)
780 int result = acl_nfs4_nontrivial ((nfs4_acl_int_t *) acl);
787 /* A newer type of ACL has been introduced in the system.
788 We should better support it. */
795 # elif HAVE_STATACL /* older AIX */
797 union { struct acl a; char room[4096]; } u;
799 if (statacl (name, STX_NORMAL, &u.a, sizeof (u)) < 0)
802 return acl_nontrivial (&u.a);
804 # elif HAVE_ACLSORT /* NonStop Kernel */
807 struct acl entries[NACLENTRIES];
811 count = acl ((char *) name, ACL_CNT, NACLENTRIES, NULL);
815 if (errno == ENOSYS || errno == ENOTSUP)
824 if (count > NACLENTRIES)
825 /* If NACLENTRIES cannot be trusted, use dynamic memory
829 /* If there are more than 4 entries, there cannot be only the
830 four base ACL entries. */
834 if (acl ((char *) name, ACL_GET, count, entries) == count)
835 return acl_nontrivial (count, entries);
836 /* Huh? The number of ACL entries changed since the last call.