Comments.
[gnulib.git] / lib / file-has-acl.c
1 /* Test whether a file has a nontrivial access control list.
2
3    Copyright (C) 2002-2003, 2005-2008 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 and Andreas Gruenbacher.  */
19
20 #include <config.h>
21
22 #include "acl.h"
23
24 #include "acl-internal.h"
25
26
27 #if USE_ACL && HAVE_ACL_GET_FILE
28
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.  */
33 int
34 acl_access_nontrivial (acl_t acl)
35 {
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 */
43
44   acl_entry_t ace;
45   int at_end;
46
47   for (at_end = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
48        !at_end;
49        at_end = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
50     {
51       acl_tag_t tag;
52       if (acl_get_tag_type (ace, &tag) < 0)
53         return -1;
54       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
55         return 1;
56     }
57   return 0;
58
59 #  else /* IRIX, Tru64 */
60 #   if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
61   /* Don't use acl_get_entry: it is undocumented.  */
62
63   int count = acl->acl_cnt;
64   int i;
65
66   for (i = 0; i < count; i++)
67     {
68       acl_entry_t ace = &acl->acl_entry[i];
69       acl_tag_t tag = ace->ae_tag;
70
71       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
72             || tag == ACL_OTHER_OBJ))
73         return 1;
74     }
75   return 0;
76
77 #   endif
78 #   if HAVE_ACL_FREE_TEXT /* Tru64 */
79   /* Don't use acl_get_entry: it takes only one argument and does not work.  */
80
81   int count = acl->acl_num;
82   acl_entry_t ace;
83
84   for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
85     {
86       acl_tag_t tag;
87       acl_perm_t perm;
88
89       tag = ace->entry->acl_type;
90       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
91         return 1;
92
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)
97         return 1;
98     }
99   return 0;
100
101 #   endif
102 #  endif
103 # else /* MacOS X */
104
105   /* acl is non-trivial if it is non-empty.  */
106   return (acl_entries (acl) > 0);
107 # endif
108 }
109
110 #endif
111
112
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.  */
116
117 int
118 file_has_acl (char const *name, struct stat const *sb)
119 {
120 #if USE_ACL
121   if (! S_ISLNK (sb->st_mode))
122     {
123 # if HAVE_ACL_GET_FILE
124
125       /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
126       /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
127       int ret;
128
129       if (HAVE_ACL_EXTENDED_FILE) /* Linux */
130         {
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
133              ACL_TYPE_DEFAULT.  */
134           ret = acl_extended_file (name);
135         }
136       else /* FreeBSD, MacOS X, IRIX, Tru64 */
137         {
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);
145           if (acl)
146             {
147               ret = (0 < acl_entries (acl));
148               acl_free (acl);
149             }
150           else
151             ret = -1;
152 #  else /* FreeBSD, IRIX, Tru64 */
153           acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
154           if (acl)
155             {
156               int saved_errno;
157
158               ret = acl_access_nontrivial (acl);
159               saved_errno = errno;
160               acl_free (acl);
161               errno = saved_errno;
162 #   if HAVE_ACL_FREE_TEXT /* Tru64 */
163               /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
164                  returns NULL with errno not set.  There is no point in
165                  making this call.  */
166 #   else /* FreeBSD, IRIX */
167               /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
168                  and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
169                  either both succeed or both fail; it depends on the
170                  filesystem.  Therefore there is no point in making the second
171                  call if the first one already failed.  */
172               if (ret == 0 && S_ISDIR (sb->st_mode))
173                 {
174                   acl = acl_get_file (name, ACL_TYPE_DEFAULT);
175                   if (acl)
176                     {
177                       ret = (0 < acl_entries (acl));
178                       acl_free (acl);
179                     }
180                   else
181                     ret = -1;
182                 }
183 #   endif
184             }
185           else
186             ret = -1;
187 #  endif
188         }
189       if (ret < 0)
190         return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
191       return ret;
192
193 # elif HAVE_ACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
194
195 #  if HAVE_ACL_TRIVIAL
196
197       /* Solaris 10, which also has NFSv4 and ZFS style ACLs.  */
198       return acl_trivial (name);
199
200 #  else /* Solaris, Cygwin, general case */
201
202       /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
203          of Unixware.  The acl() call returns the access and default ACL both
204          at once.  */
205       int n = acl (name, GETACLCNT, 0, NULL);
206       return n < 0 ? (errno == ENOSYS ? 0 : -1) : (MIN_ACL_ENTRIES < n);
207
208 #  endif
209
210 # endif
211     }
212 #endif
213
214   /* FIXME: Add support for AIX.  Please see Samba's
215      source/lib/sysacls.c file for fix-related ideas.  */
216
217   return 0;
218 }