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 and Andreas Gruenbacher. */
24 #include "acl-internal.h"
27 #if USE_ACL && HAVE_ACL_GET_FILE
29 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
30 Return 1 if the given ACL is non-trivial.
31 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
32 Return -1 and set errno upon failure to determine it. */
34 acl_access_nontrivial (acl_t acl)
36 # if MODE_INSIDE_ACL /* Linux, FreeBSD, IRIX, Tru64 */
37 /* acl is non-trivial if it has some entries other than for "user::",
38 "group::", and "other::". Normally these three should be present
39 at least, allowing us to write
40 return (3 < acl_entries (acl));
41 but the following code is more robust. */
42 # if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD */
47 for (at_end = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
49 at_end = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
52 if (acl_get_tag_type (ace, &tag) < 0)
54 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
59 # else /* IRIX, Tru64 */
60 # if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
61 /* Don't use acl_get_entry: it is undocumented. */
63 int count = acl->acl_cnt;
66 for (i = 0; i < count; i++)
68 acl_entry_t ace = &acl->acl_entry[i];
69 acl_tag_t tag = ace->ae_tag;
71 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
72 || tag == ACL_OTHER_OBJ))
78 # if HAVE_ACL_FREE_TEXT /* Tru64 */
79 /* Don't use acl_get_entry: it takes only one argument and does not work. */
81 int count = acl->acl_num;
84 for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
89 tag = ace->entry->acl_type;
90 if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
93 perm = ace->entry->acl_perm;
94 /* On Tru64, perm can also contain non-standard bits such as
95 PERM_INSERT, PERM_DELETE, PERM_MODIFY, PERM_LOOKUP, ... */
96 if ((perm & ~(ACL_READ | ACL_WRITE | ACL_EXECUTE)) != 0)
105 /* acl is non-trivial if it is non-empty. */
106 return (acl_entries (acl) > 0);
113 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
114 only has no or a base access control list, and -1 (setting errno)
115 on error. SB must be set to the stat buffer of FILE. */
118 file_has_acl (char const *name, struct stat const *sb)
121 if (! S_ISLNK (sb->st_mode))
123 # if HAVE_ACL_GET_FILE
125 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
126 /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
129 if (HAVE_ACL_EXTENDED_FILE)
131 /* On Linux, acl_extended_file is an optimized function: It only
132 makes two calls to getxattr(), one for ACL_TYPE_ACCESS, one for
134 ret = acl_extended_file (name);
138 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
139 /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
140 and acl_get_file (name, ACL_TYPE_DEFAULT)
141 always return NULL / EINVAL. There is no point in making
142 these two useless calls. The real ACL is retrieved through
143 acl_get_file (name, ACL_TYPE_EXTENDED). */
144 acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
147 ret = (0 < acl_entries (acl));
152 # else /* FreeBSD, IRIX, Tru64 */
153 acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
158 ret = acl_access_nontrivial (acl);
162 if (ret == 0 && S_ISDIR (sb->st_mode))
164 acl = acl_get_file (name, ACL_TYPE_DEFAULT);
167 ret = (0 < acl_entries (acl));
179 return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
182 # elif HAVE_ACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
184 # if HAVE_ACL_TRIVIAL
186 /* Solaris 10, which also has NFSv4 and ZFS style ACLs. */
187 return acl_trivial (name);
189 # else /* Solaris, Cygwin, general case */
191 /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
192 of Unixware. The acl() call returns the access and default ACL both
194 int n = acl (name, GETACLCNT, 0, NULL);
195 return n < 0 ? (errno == ENOSYS ? 0 : -1) : (MIN_ACL_ENTRIES < n);
203 /* FIXME: Add support for AIX. Please see Samba's
204 source/lib/sysacls.c file for fix-related ideas. */