1 /* Test whether a file has a nontrivial access control list.
3 Copyright (C) 2002-2003, 2005-2011 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. */
24 #include "acl-internal.h"
27 #if USE_ACL && HAVE_ACL_GET_FILE
29 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
31 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
32 Return 1 if the given ACL is non-trivial.
33 Return 0 if it is trivial. */
35 acl_extended_nontrivial (acl_t acl)
37 /* acl is non-trivial if it is non-empty. */
38 return (acl_entries (acl) > 0);
41 # else /* Linux, FreeBSD, IRIX, Tru64 */
43 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
44 Return 1 if the given ACL is non-trivial.
45 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
46 Return -1 and set errno upon failure to determine it. */
48 acl_access_nontrivial (acl_t acl)
50 /* acl is non-trivial if it has some entries other than for "user::",
51 "group::", and "other::". Normally these three should be present
52 at least, allowing us to write
53 return (3 < acl_entries (acl));
54 but the following code is more robust. */
55 # if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD */
60 for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
62 got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
65 if (acl_get_tag_type (ace, &tag) < 0)
67 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
72 # else /* IRIX, Tru64 */
73 # if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
74 /* Don't use acl_get_entry: it is undocumented. */
76 int count = acl->acl_cnt;
79 for (i = 0; i < count; i++)
81 acl_entry_t ace = &acl->acl_entry[i];
82 acl_tag_t tag = ace->ae_tag;
84 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
85 || tag == ACL_OTHER_OBJ))
91 # if HAVE_ACL_FREE_TEXT /* Tru64 */
92 /* Don't use acl_get_entry: it takes only one argument and does not work. */
94 int count = acl->acl_num;
97 for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
102 tag = ace->entry->acl_type;
103 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
106 perm = ace->entry->acl_perm;
107 /* On Tru64, perm can also contain non-standard bits such as
108 PERM_INSERT, PERM_DELETE, PERM_MODIFY, PERM_LOOKUP, ... */
109 if ((perm & ~(ACL_READ | ACL_WRITE | ACL_EXECUTE)) != 0)
121 #elif USE_ACL && HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
123 /* Test an ACL retrieved with GETACL.
124 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
125 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
127 acl_nontrivial (int count, aclent_t *entries)
131 for (i = 0; i < count; i++)
133 aclent_t *ace = &entries[i];
135 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
136 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
137 We don't need to check ace->a_id in these cases. */
138 if (!(ace->a_type == USER_OBJ
139 || ace->a_type == GROUP_OBJ
140 || ace->a_type == OTHER_OBJ
141 /* Note: Cygwin does not return a CLASS_OBJ ("mask:") entry
143 || ace->a_type == CLASS_OBJ))
151 /* Test an ACL retrieved with ACE_GETACL.
152 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
153 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
155 acl_ace_nontrivial (int count, ace_t *entries)
159 /* The flags in the ace_t structure changed in a binary incompatible way
160 when ACL_NO_TRIVIAL etc. were introduced in <sys/acl.h> version 1.15.
161 How to distinguish the two conventions at runtime?
162 In the old convention, usually three ACEs have a_flags = ACE_OWNER /
163 ACE_GROUP / ACE_OTHER, in the range 0x0100..0x0400. In the new
164 convention, these values are not used. */
165 int old_convention = 0;
167 for (i = 0; i < count; i++)
168 if (entries[i].a_flags & (OLD_ACE_OWNER | OLD_ACE_GROUP | OLD_ACE_OTHER))
175 /* Running on Solaris 10. */
176 for (i = 0; i < count; i++)
178 ace_t *ace = &entries[i];
181 If ace->a_flags = ACE_OWNER, ace->a_who is the st_uid from stat().
182 If ace->a_flags = ACE_GROUP, ace->a_who is the st_gid from stat().
183 We don't need to check ace->a_who in these cases. */
184 if (!(ace->a_type == OLD_ALLOW
185 && (ace->a_flags == OLD_ACE_OWNER
186 || ace->a_flags == OLD_ACE_GROUP
187 || ace->a_flags == OLD_ACE_OTHER)))
192 /* Running on Solaris 10 (newer version) or Solaris 11. */
193 unsigned int access_masks[6] =
196 0, /* owner@ allow */
198 0, /* group@ allow */
199 0, /* everyone@ deny */
200 0 /* everyone@ allow */
203 for (i = 0; i < count; i++)
205 ace_t *ace = &entries[i];
209 if (ace->a_type == NEW_ACE_ACCESS_ALLOWED_ACE_TYPE)
211 else if (ace->a_type == NEW_ACE_ACCESS_DENIED_ACE_TYPE)
216 if (ace->a_flags == NEW_ACE_OWNER)
218 else if (ace->a_flags == (NEW_ACE_GROUP | NEW_ACE_IDENTIFIER_GROUP))
220 else if (ace->a_flags == ACE_EVERYONE)
225 access_masks[index1 + index2] |= ace->a_access_mask;
228 /* The same bit shouldn't be both allowed and denied. */
229 if (access_masks[0] & access_masks[1])
231 if (access_masks[2] & access_masks[3])
233 if (access_masks[4] & access_masks[5])
236 /* Check minimum masks. */
237 if ((NEW_ACE_WRITE_NAMED_ATTRS
238 | NEW_ACE_WRITE_ATTRIBUTES
240 | NEW_ACE_WRITE_OWNER)
243 access_masks[1] &= ~(NEW_ACE_WRITE_NAMED_ATTRS
244 | NEW_ACE_WRITE_ATTRIBUTES
246 | NEW_ACE_WRITE_OWNER);
247 if ((NEW_ACE_WRITE_NAMED_ATTRS
248 | NEW_ACE_WRITE_ATTRIBUTES
250 | NEW_ACE_WRITE_OWNER)
253 access_masks[4] &= ~(NEW_ACE_WRITE_NAMED_ATTRS
254 | NEW_ACE_WRITE_ATTRIBUTES
256 | NEW_ACE_WRITE_OWNER);
257 if ((NEW_ACE_READ_NAMED_ATTRS
258 | NEW_ACE_READ_ATTRIBUTES
260 | NEW_ACE_SYNCHRONIZE)
263 access_masks[5] &= ~(NEW_ACE_READ_NAMED_ATTRS
264 | NEW_ACE_READ_ATTRIBUTES
266 | NEW_ACE_SYNCHRONIZE);
268 /* Check the allowed or denied bits. */
269 if ((access_masks[0] | access_masks[1])
270 != (NEW_ACE_READ_DATA
271 | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
274 if ((access_masks[2] | access_masks[3])
275 != (NEW_ACE_READ_DATA
276 | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
279 if ((access_masks[4] | access_masks[5])
280 != (NEW_ACE_READ_DATA
281 | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
285 /* Check that the NEW_ACE_WRITE_DATA and NEW_ACE_APPEND_DATA bits are
286 either both allowed or both denied. */
287 if (((access_masks[0] & NEW_ACE_WRITE_DATA) != 0)
288 != ((access_masks[0] & NEW_ACE_APPEND_DATA) != 0))
290 if (((access_masks[2] & NEW_ACE_WRITE_DATA) != 0)
291 != ((access_masks[2] & NEW_ACE_APPEND_DATA) != 0))
293 if (((access_masks[4] & NEW_ACE_WRITE_DATA) != 0)
294 != ((access_masks[4] & NEW_ACE_APPEND_DATA) != 0))
303 #elif USE_ACL && HAVE_GETACL /* HP-UX */
305 /* Return 1 if the given ACL is non-trivial.
306 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
308 acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb)
312 for (i = 0; i < count; i++)
314 struct acl_entry *ace = &entries[i];
316 if (!((ace->uid == sb->st_uid && ace->gid == ACL_NSGROUP)
317 || (ace->uid == ACL_NSUSER && ace->gid == sb->st_gid)
318 || (ace->uid == ACL_NSUSER && ace->gid == ACL_NSGROUP)))
324 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
326 /* Return 1 if the given ACL is non-trivial.
327 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
329 aclv_nontrivial (int count, struct acl *entries)
333 for (i = 0; i < count; i++)
335 struct acl *ace = &entries[i];
337 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
338 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
339 We don't need to check ace->a_id in these cases. */
340 if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
341 || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
342 || ace->a_type == CLASS_OBJ
343 || ace->a_type == OTHER_OBJ))
351 #elif USE_ACL && (HAVE_ACLX_GET || HAVE_STATACL) /* AIX */
353 /* Return 1 if the given ACL is non-trivial.
354 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
356 acl_nontrivial (struct acl *a)
358 /* The normal way to iterate through an ACL is like this:
359 struct acl_entry *ace;
360 for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
363 switch (ace->ace_type)
370 for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
374 return (acl_last (a) != a->acl_ext ? 1 : 0);
377 # if HAVE_ACLX_GET && defined ACL_AIX_WIP /* newer AIX */
379 /* Return 1 if the given ACL is non-trivial.
380 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
382 acl_nfs4_nontrivial (nfs4_acl_int_t *a)
384 # if 1 /* let's try this first */
385 return (a->aclEntryN > 0 ? 1 : 0);
387 int count = a->aclEntryN;
390 for (i = 0; i < count; i++)
392 nfs4_ace_int_t *ace = &a->aclEntry[i];
394 if (!((ace->flags & ACE4_ID_SPECIAL) != 0
395 && (ace->aceWho.special_whoid == ACE4_WHO_OWNER
396 || ace->aceWho.special_whoid == ACE4_WHO_GROUP
397 || ace->aceWho.special_whoid == ACE4_WHO_EVERYONE)
398 && ace->aceType == ACE4_ACCESS_ALLOWED_ACE_TYPE
399 && ace->aceFlags == 0
400 && (ace->aceMask & ~(ACE4_READ_DATA | ACE4_LIST_DIRECTORY
401 | ACE4_WRITE_DATA | ACE4_ADD_FILE
402 | ACE4_EXECUTE)) == 0))
411 #elif USE_ACL && HAVE_ACLSORT /* NonStop Kernel */
413 /* Test an ACL retrieved with ACL_GET.
414 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
415 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
417 acl_nontrivial (int count, struct acl *entries)
421 for (i = 0; i < count; i++)
423 struct acl *ace = &entries[i];
425 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
426 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
427 We don't need to check ace->a_id in these cases. */
428 if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
429 || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
430 || ace->a_type == CLASS_OBJ
431 || ace->a_type == OTHER_OBJ))
440 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
441 only has no or a base access control list, and -1 (setting errno)
442 on error. SB must be set to the stat buffer of FILE. */
445 file_has_acl (char const *name, struct stat const *sb)
448 if (! S_ISLNK (sb->st_mode))
450 # if HAVE_ACL_GET_FILE
452 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
453 /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
456 if (HAVE_ACL_EXTENDED_FILE || HAVE_ACL_EXTENDED_FILE_NOFOLLOW) /* Linux */
458 # if HAVE_ACL_EXTENDED_FILE_NOFOLLOW
459 /* acl_extended_file_nofollow() uses lgetxattr() in order to prevent
460 unnecessary mounts, but it returns the same result as we already
461 know that NAME is not a symbolic link at this point (modulo the
462 TOCTTOU race condition). */
463 ret = acl_extended_file_nofollow (name);
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);
471 else /* FreeBSD, MacOS X, IRIX, Tru64 */
473 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
474 /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
475 and acl_get_file (name, ACL_TYPE_DEFAULT)
476 always return NULL / EINVAL. There is no point in making
477 these two useless calls. The real ACL is retrieved through
478 acl_get_file (name, ACL_TYPE_EXTENDED). */
479 acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
482 ret = acl_extended_nontrivial (acl);
487 # else /* FreeBSD, IRIX, Tru64 */
488 acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
493 ret = acl_access_nontrivial (acl);
497 # if HAVE_ACL_FREE_TEXT /* Tru64 */
498 /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
499 returns NULL with errno not set. There is no point in
501 # else /* FreeBSD, IRIX */
502 /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
503 and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
504 either both succeed or both fail; it depends on the
505 file system. Therefore there is no point in making the second
506 call if the first one already failed. */
507 if (ret == 0 && S_ISDIR (sb->st_mode))
509 acl = acl_get_file (name, ACL_TYPE_DEFAULT);
512 ret = (0 < acl_entries (acl));
525 return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
528 # elif HAVE_FACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
530 # if defined ACL_NO_TRIVIAL
532 /* Solaris 10 (newer version), which has additional API declared in
533 <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
534 acl_fromtext, ...). */
535 return acl_trivial (name);
537 # else /* Solaris, Cygwin, general case */
539 /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
540 of Unixware. The acl() call returns the access and default ACL both
548 count = acl (name, GETACLCNT, 0, NULL);
552 if (errno == ENOSYS || errno == ENOTSUP)
561 /* Don't use MIN_ACL_ENTRIES: It's set to 4 on Cygwin, but Cygwin
562 returns only 3 entries for files with no ACL. But this is safe:
563 If there are more than 4 entries, there cannot be only the
564 "user::", "group::", "other:", and "mask:" entries. */
568 entries = (aclent_t *) malloc (count * sizeof (aclent_t));
574 if (acl (name, GETACL, count, entries) == count)
576 if (acl_nontrivial (count, entries))
584 /* Huh? The number of ACL entries changed since the last call.
591 /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
592 file systems (whereas the other ones are used in UFS file systems). */
598 count = acl (name, ACE_GETACLCNT, 0, NULL);
602 if (errno == ENOSYS || errno == EINVAL)
611 /* In the old (original Solaris 10) convention:
612 If there are more than 3 entries, there cannot be only the
613 ACE_OWNER, ACE_GROUP, ACE_OTHER entries.
614 In the newer Solaris 10 and Solaris 11 convention:
615 If there are more than 6 entries, there cannot be only the
616 ACE_OWNER, ACE_GROUP, ACE_EVERYONE entries, each once with
617 NEW_ACE_ACCESS_ALLOWED_ACE_TYPE and once with
618 NEW_ACE_ACCESS_DENIED_ACE_TYPE. */
622 entries = (ace_t *) malloc (count * sizeof (ace_t));
628 if (acl (name, ACE_GETACL, count, entries) == count)
630 if (acl_ace_nontrivial (count, entries))
638 /* Huh? The number of ACL entries changed since the last call.
648 # elif HAVE_GETACL /* HP-UX */
653 struct acl_entry entries[NACLENTRIES];
655 count = getacl (name, 0, NULL);
659 /* ENOSYS is seen on newer HP-UX versions.
660 EOPNOTSUPP is typically seen on NFS mounts.
661 ENOTSUP was seen on Quantum StorNext file systems (cvfs). */
662 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP)
671 if (count > NACLENTRIES)
672 /* If NACLENTRIES cannot be trusted, use dynamic memory
676 /* If there are more than 3 entries, there cannot be only the
677 (uid,%), (%,gid), (%,%) entries. */
681 if (getacl (name, count, entries) == count)
685 if (stat (name, &statbuf) < 0)
688 return acl_nontrivial (count, entries, &statbuf);
690 /* Huh? The number of ACL entries changed since the last call.
694 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
699 struct acl entries[NACLVENTRIES];
701 count = acl ((char *) name, ACL_CNT, NACLVENTRIES, entries);
705 /* EOPNOTSUPP is seen on NFS in HP-UX 11.11, 11.23.
706 EINVAL is seen on NFS in HP-UX 11.31. */
707 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
716 if (count > NACLVENTRIES)
717 /* If NACLVENTRIES cannot be trusted, use dynamic memory
721 /* If there are more than 4 entries, there cannot be only the
722 four base ACL entries. */
726 if (acl ((char *) name, ACL_GET, count, entries) == count)
727 return aclv_nontrivial (count, entries);
728 /* Huh? The number of ACL entries changed since the last call.
734 # elif HAVE_ACLX_GET && defined ACL_AIX_WIP /* AIX */
739 size_t aclsize = sizeof (aclbuf);
744 /* The docs say that type being 0 is equivalent to ACL_ANY, but it
745 is not true, in AIX 5.3. */
747 if (aclx_get (name, 0, &type, aclbuf, &aclsize, &mode) >= 0)
753 int saved_errno = errno;
759 aclsize = 2 * aclsize;
762 acl = malloc (aclsize);
770 if (type.u64 == ACL_AIXC)
772 int result = acl_nontrivial ((struct acl *) acl);
777 else if (type.u64 == ACL_NFS4)
779 int result = acl_nfs4_nontrivial ((nfs4_acl_int_t *) acl);
786 /* A newer type of ACL has been introduced in the system.
787 We should better support it. */
794 # elif HAVE_STATACL /* older AIX */
796 union { struct acl a; char room[4096]; } u;
798 if (statacl (name, STX_NORMAL, &u.a, sizeof (u)) < 0)
801 return acl_nontrivial (&u.a);
803 # elif HAVE_ACLSORT /* NonStop Kernel */
806 struct acl entries[NACLENTRIES];
810 count = acl ((char *) name, ACL_CNT, NACLENTRIES, NULL);
818 if (count > NACLENTRIES)
819 /* If NACLENTRIES cannot be trusted, use dynamic memory
823 /* If there are more than 4 entries, there cannot be only the
824 four base ACL entries. */
828 if (acl ((char *) name, ACL_GET, count, entries) == count)
829 return acl_nontrivial (count, entries);
830 /* Huh? The number of ACL entries changed since the last call.