1 /* Test whether a file has a nontrivial access control list.
3 Copyright (C) 2002-2003, 2005-2008 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 (at_end = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
62 at_end = 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_ACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
123 # if !defined ACL_NO_TRIVIAL /* Solaris <= 10, Cygwin */
125 /* Test an ACL retrieved with GETACL.
126 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
127 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
129 acl_nontrivial (int count, aclent_t *entries)
133 for (i = 0; i < count; i++)
135 aclent_t *ace = &entries[i];
137 /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
138 If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
139 We don't need to check ace->a_id in these cases. */
140 if (!(ace->a_type == USER_OBJ
141 || ace->a_type == GROUP_OBJ
142 || ace->a_type == OTHER_OBJ
143 /* Note: Cygwin does not return a CLASS_OBJ ("mask:") entry
145 || ace->a_type == CLASS_OBJ))
153 /* Test an ACL retrieved with ACE_GETACL.
154 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
155 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
157 acl_ace_nontrivial (int count, ace_t *entries)
161 /* The flags in the ace_t structure changed in a binary incompatible way
162 when ACL_NO_TRIVIAL etc. were introduced in <sys/acl.h> version 1.15.
163 How to distinguish the two conventions at runtime?
164 In the old convention, usually three ACEs have a_flags = ACE_OWNER /
165 ACE_GROUP / ACE_OTHER, in the range 0x0100..0x0400. In the new
166 convention, these values are not used. */
167 int old_convention = 0;
169 for (i = 0; i < count; i++)
170 if (entries[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_OTHER))
177 /* Running on Solaris 10. */
178 for (i = 0; i < count; i++)
180 ace_t *ace = &entries[i];
183 If ace->a_flags = ACE_OWNER, ace->a_who is the st_uid from stat().
184 If ace->a_flags = ACE_GROUP, ace->a_who is the st_gid from stat().
185 We don't need to check ace->a_who in these cases. */
186 if (!(ace->a_type == ALLOW
187 && (ace->a_flags == ACE_OWNER
188 || ace->a_flags == ACE_GROUP
189 || ace->a_flags == ACE_OTHER)))
193 /* Running on Solaris 10 (newer version) or Solaris 11. */
194 for (i = 0; i < count; i++)
196 ace_t *ace = &entries[i];
198 if (!(ace->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE
199 && (ace->a_flags == NEW_ACE_OWNER
201 == (NEW_ACE_GROUP | NEW_ACE_IDENTIFIER_GROUP)
202 || ace->a_flags == ACE_EVERYONE)
203 && (ace->a_access_mask
204 & ~(NEW_ACE_READ_DATA | NEW_ACE_WRITE_DATA | NEW_ACE_EXECUTE))
216 #elif USE_ACL && HAVE_GETACL /* HP-UX */
218 /* Return 1 if the given ACL is non-trivial.
219 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
221 acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb)
225 for (i = 0; i < count; i++)
227 struct acl_entry *ace = &entries[i];
229 if (!((ace->uid == sb->st_uid && ace->gid == ACL_NSGROUP)
230 || (ace->uid == ACL_NSUSER && ace->gid == sb->st_gid)
231 || (ace->uid == ACL_NSUSER && ace->gid == ACL_NSGROUP)))
237 #elif USE_ACL && HAVE_ACLX_GET && 0 /* AIX */
241 #elif USE_ACL && HAVE_STATACL /* older AIX */
243 /* Return 1 if the given ACL is non-trivial.
244 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
246 acl_nontrivial (struct acl *a)
248 /* The normal way to iterate through an ACL is like this:
249 struct acl_entry *ace;
250 for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
253 switch (ace->ace_type)
260 for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
264 return (acl_last (a) != a->acl_ext ? 1 : 0);
270 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
271 only has no or a base access control list, and -1 (setting errno)
272 on error. SB must be set to the stat buffer of FILE. */
275 file_has_acl (char const *name, struct stat const *sb)
278 if (! S_ISLNK (sb->st_mode))
280 # if HAVE_ACL_GET_FILE
282 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
283 /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
286 if (HAVE_ACL_EXTENDED_FILE) /* Linux */
288 /* On Linux, acl_extended_file is an optimized function: It only
289 makes two calls to getxattr(), one for ACL_TYPE_ACCESS, one for
291 ret = acl_extended_file (name);
293 else /* FreeBSD, MacOS X, IRIX, Tru64 */
295 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
296 /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
297 and acl_get_file (name, ACL_TYPE_DEFAULT)
298 always return NULL / EINVAL. There is no point in making
299 these two useless calls. The real ACL is retrieved through
300 acl_get_file (name, ACL_TYPE_EXTENDED). */
301 acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
304 ret = acl_extended_nontrivial (acl);
309 # else /* FreeBSD, IRIX, Tru64 */
310 acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
315 ret = acl_access_nontrivial (acl);
319 # if HAVE_ACL_FREE_TEXT /* Tru64 */
320 /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
321 returns NULL with errno not set. There is no point in
323 # else /* FreeBSD, IRIX */
324 /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
325 and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
326 either both succeed or both fail; it depends on the
327 filesystem. Therefore there is no point in making the second
328 call if the first one already failed. */
329 if (ret == 0 && S_ISDIR (sb->st_mode))
331 acl = acl_get_file (name, ACL_TYPE_DEFAULT);
334 ret = (0 < acl_entries (acl));
347 return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
350 # elif HAVE_ACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
352 # if defined ACL_NO_TRIVIAL
354 /* Solaris 10 (newer version), which has additional API declared in
355 <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
356 acl_fromtext, ...). */
357 return acl_trivial (name);
359 # else /* Solaris, Cygwin, general case */
361 /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
362 of Unixware. The acl() call returns the access and default ACL both
370 count = acl (name, GETACLCNT, 0, NULL);
374 if (errno == ENOSYS || errno == ENOTSUP)
383 /* Don't use MIN_ACL_ENTRIES: It's set to 4 on Cygwin, but Cygwin
384 returns only 3 entries for files with no ACL. But this is safe:
385 If there are more than 4 entries, there cannot be only the
386 "user::", "group::", "other:", and "mask:" entries. */
390 entries = (aclent_t *) malloc (count * sizeof (aclent_t));
396 if (acl (name, GETACL, count, entries) == count)
398 if (acl_nontrivial (count, entries))
406 /* Huh? The number of ACL entries changed since the last call.
413 /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
414 file systems (whereas the other ones are used in UFS file systems). */
420 count = acl (name, ACE_GETACLCNT, 0, NULL);
424 if (errno == ENOSYS || errno == EINVAL)
433 /* If there are more than 3 entries, there cannot be only the
434 ACE_OWNER, ACE_GROUP, ACE_OTHER entries. */
438 entries = (ace_t *) malloc (count * sizeof (ace_t));
444 if (acl (name, ACE_GETACL, count, entries) == count)
446 if (acl_ace_nontrivial (count, entries))
454 /* Huh? The number of ACL entries changed since the last call.
464 # elif HAVE_GETACL /* HP-UX */
467 struct acl_entry entries[NACLENTRIES];
471 count = getacl (name, 0, NULL);
474 return (errno == ENOSYS || errno == EOPNOTSUPP ? 0 : -1);
479 if (count > NACLENTRIES)
480 /* If NACLENTRIES cannot be trusted, use dynamic memory
484 /* If there are more than 3 entries, there cannot be only the
485 (uid,%), (%,gid), (%,%) entries. */
489 if (getacl (name, count, entries) == count)
493 if (stat (name, &statbuf) < 0)
496 return acl_nontrivial (count, entries, &statbuf);
498 /* Huh? The number of ACL entries changed since the last call.
502 # elif HAVE_ACLX_GET && 0 /* AIX */
504 /* TODO: use aclx_get(), and then? */
506 # elif HAVE_STATACL /* older AIX */
508 union { struct acl a; char room[4096]; } u;
510 if (statacl (name, STX_NORMAL, &u.a, sizeof (u)) < 0)
513 return acl_nontrivial (&u.a);