add more guards around definition of ACE_-related code
[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, Andreas Grünbacher, and Bruno Haible.  */
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 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
30
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.  */
34 int
35 acl_extended_nontrivial (acl_t acl)
36 {
37   /* acl is non-trivial if it is non-empty.  */
38   return (acl_entries (acl) > 0);
39 }
40
41 # else /* Linux, FreeBSD, IRIX, Tru64 */
42
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.  */
47 int
48 acl_access_nontrivial (acl_t acl)
49 {
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 */
56
57   acl_entry_t ace;
58   int at_end;
59
60   for (at_end = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
61        !at_end;
62        at_end = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
63     {
64       acl_tag_t tag;
65       if (acl_get_tag_type (ace, &tag) < 0)
66         return -1;
67       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
68         return 1;
69     }
70   return 0;
71
72 #  else /* IRIX, Tru64 */
73 #   if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
74   /* Don't use acl_get_entry: it is undocumented.  */
75
76   int count = acl->acl_cnt;
77   int i;
78
79   for (i = 0; i < count; i++)
80     {
81       acl_entry_t ace = &acl->acl_entry[i];
82       acl_tag_t tag = ace->ae_tag;
83
84       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
85             || tag == ACL_OTHER_OBJ))
86         return 1;
87     }
88   return 0;
89
90 #   endif
91 #   if HAVE_ACL_FREE_TEXT /* Tru64 */
92   /* Don't use acl_get_entry: it takes only one argument and does not work.  */
93
94   int count = acl->acl_num;
95   acl_entry_t ace;
96
97   for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
98     {
99       acl_tag_t tag;
100       acl_perm_t perm;
101
102       tag = ace->entry->acl_type;
103       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
104         return 1;
105
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)
110         return 1;
111     }
112   return 0;
113
114 #   endif
115 #  endif
116 }
117
118 # endif
119
120
121 #elif USE_ACL && HAVE_ACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
122
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.  */
126 int
127 acl_nontrivial (int count, aclent_t *entries)
128 {
129   int i;
130
131   for (i = 0; i < count; i++)
132     {
133       aclent_t *ace = &entries[i];
134
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
142                sometimes.  */
143             || ace->a_type == CLASS_OBJ))
144         return 1;
145     }
146   return 0;
147 }
148
149 # if defined ACE_GETACL && defined ALLOW && defined ACE_OWNER
150
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.  */
154 int
155 acl_ace_nontrivial (int count, ace_t *entries)
156 {
157   int i;
158
159   for (i = 0; i < count; i++)
160     {
161       ace_t *ace = &entries[i];
162
163       /* Note: If ace->a_flags = ACE_OWNER, ace->a_who is the st_uid from
164          stat().  If ace->a_flags = ACE_GROUP, ace->a_who is the st_gid from
165          stat().  We don't need to check ace->a_who in these cases.  */
166       if (!(ace->a_type == ALLOW
167             && (ace->a_flags == ACE_OWNER
168                 || ace->a_flags == ACE_GROUP
169                 || ace->a_flags == ACE_OTHER)))
170         return 1;
171     }
172   return 0;
173 }
174
175 # endif
176
177 #elif USE_ACL && HAVE_GETACL /* HP-UX */
178
179 /* Return 1 if the given ACL is non-trivial.
180    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
181 int
182 acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb)
183 {
184   int i;
185
186   for (i = 0; i < count; i++)
187     {
188       struct acl_entry *ace = &entries[i];
189
190       if (!((ace->uid == sb->st_uid && ace->gid == ACL_NSGROUP)
191             || (ace->uid == ACL_NSUSER && ace->gid == sb->st_gid)
192             || (ace->uid == ACL_NSUSER && ace->gid == ACL_NSGROUP)))
193         return 1;
194     }
195   return 0;
196 }
197
198 #elif USE_ACL && HAVE_ACLX_GET && 0 /* AIX */
199
200 /* TODO */
201
202 #elif USE_ACL && HAVE_STATACL /* older AIX */
203
204 /* Return 1 if the given ACL is non-trivial.
205    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
206 int
207 acl_nontrivial (struct acl *a)
208 {
209   /* The normal way to iterate through an ACL is like this:
210        struct acl_entry *ace;
211        for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
212          {
213            struct ace_id *aei;
214            switch (ace->ace_type)
215              {
216              case ACC_PERMIT:
217              case ACC_DENY:
218              case ACC_SPECIFY:
219                ...;
220              }
221            for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
222              ...
223          }
224    */
225   return (acl_last (a) != a->acl_ext ? 1 : 0);
226 }
227
228 #endif
229
230
231 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
232    only has no or a base access control list, and -1 (setting errno)
233    on error.  SB must be set to the stat buffer of FILE.  */
234
235 int
236 file_has_acl (char const *name, struct stat const *sb)
237 {
238 #if USE_ACL
239   if (! S_ISLNK (sb->st_mode))
240     {
241 # if HAVE_ACL_GET_FILE
242
243       /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
244       /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
245       int ret;
246
247       if (HAVE_ACL_EXTENDED_FILE) /* Linux */
248         {
249           /* On Linux, acl_extended_file is an optimized function: It only
250              makes two calls to getxattr(), one for ACL_TYPE_ACCESS, one for
251              ACL_TYPE_DEFAULT.  */
252           ret = acl_extended_file (name);
253         }
254       else /* FreeBSD, MacOS X, IRIX, Tru64 */
255         {
256 #  if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
257           /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
258              and acl_get_file (name, ACL_TYPE_DEFAULT)
259              always return NULL / EINVAL.  There is no point in making
260              these two useless calls.  The real ACL is retrieved through
261              acl_get_file (name, ACL_TYPE_EXTENDED).  */
262           acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
263           if (acl)
264             {
265               ret = acl_extended_nontrivial (acl);
266               acl_free (acl);
267             }
268           else
269             ret = -1;
270 #  else /* FreeBSD, IRIX, Tru64 */
271           acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
272           if (acl)
273             {
274               int saved_errno;
275
276               ret = acl_access_nontrivial (acl);
277               saved_errno = errno;
278               acl_free (acl);
279               errno = saved_errno;
280 #   if HAVE_ACL_FREE_TEXT /* Tru64 */
281               /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
282                  returns NULL with errno not set.  There is no point in
283                  making this call.  */
284 #   else /* FreeBSD, IRIX */
285               /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
286                  and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
287                  either both succeed or both fail; it depends on the
288                  filesystem.  Therefore there is no point in making the second
289                  call if the first one already failed.  */
290               if (ret == 0 && S_ISDIR (sb->st_mode))
291                 {
292                   acl = acl_get_file (name, ACL_TYPE_DEFAULT);
293                   if (acl)
294                     {
295                       ret = (0 < acl_entries (acl));
296                       acl_free (acl);
297                     }
298                   else
299                     ret = -1;
300                 }
301 #   endif
302             }
303           else
304             ret = -1;
305 #  endif
306         }
307       if (ret < 0)
308         return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
309       return ret;
310
311 # elif HAVE_ACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
312
313 #  if HAVE_ACL_TRIVIAL
314
315       /* Solaris 10 (newer version), which has additional API declared in
316          <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
317          acl_fromtext, ...).  */
318       return acl_trivial (name);
319
320 #  else /* Solaris, Cygwin, general case */
321
322       /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
323          of Unixware.  The acl() call returns the access and default ACL both
324          at once.  */
325       int count;
326       {
327         aclent_t *entries;
328
329         for (;;)
330           {
331             count = acl (name, GETACLCNT, 0, NULL);
332
333             if (count < 0)
334               {
335                 if (errno == ENOSYS || errno == ENOTSUP)
336                   break;
337                 else
338                   return -1;
339               }
340
341             if (count == 0)
342               break;
343
344             /* Don't use MIN_ACL_ENTRIES:  It's set to 4 on Cygwin, but Cygwin
345                returns only 3 entries for files with no ACL.  But this is safe:
346                If there are more than 4 entries, there cannot be only the
347                "user::", "group::", "other:", and "mask:" entries.  */
348             if (count > 4)
349               return 1;
350
351             entries = (aclent_t *) malloc (count * sizeof (aclent_t));
352             if (entries == NULL)
353               {
354                 errno = ENOMEM;
355                 return -1;
356               }
357             if (acl (name, GETACL, count, entries) == count)
358               {
359                 if (acl_nontrivial (count, entries))
360                   {
361                     free (entries);
362                     return 1;
363                   }
364                 free (entries);
365                 break;
366               }
367             /* Huh? The number of ACL entries changed since the last call.
368                Repeat.  */
369             free (entries);
370           }
371       }
372
373 #   ifdef ACE_GETACL
374       /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
375          file systems (whereas the other ones are used in UFS file systems).  */
376       {
377         ace_t *entries;
378
379         for (;;)
380           {
381             count = acl (name, ACE_GETACLCNT, 0, NULL);
382
383             if (count < 0)
384               {
385                 if (errno == ENOSYS || errno == EINVAL)
386                   break;
387                 else
388                   return -1;
389               }
390
391             if (count == 0)
392               break;
393
394             /* If there are more than 3 entries, there cannot be only the
395                ACE_OWNER, ACE_GROUP, ACE_OTHER entries.  */
396             if (count > 3)
397               return 1;
398
399             entries = (ace_t *) malloc (count * sizeof (ace_t));
400             if (entries == NULL)
401               {
402                 errno = ENOMEM;
403                 return -1;
404               }
405             if (acl (name, ACE_GETACL, count, entries) == count)
406               {
407                 if (acl_ace_nontrivial (count, entries))
408                   {
409                     free (entries);
410                     return 1;
411                   }
412                 free (entries);
413                 break;
414               }
415             /* Huh? The number of ACL entries changed since the last call.
416                Repeat.  */
417             free (entries);
418           }
419       }
420 #   endif
421
422       return 0;
423 #  endif
424
425 # elif HAVE_GETACL /* HP-UX */
426
427       int count;
428       struct acl_entry entries[NACLENTRIES];
429
430       for (;;)
431         {
432           count = getacl (name, 0, NULL);
433
434           if (count < 0)
435             return (errno == ENOSYS || errno == EOPNOTSUPP ? 0 : -1);
436
437           if (count == 0)
438             return 0;
439
440           if (count > NACLENTRIES)
441             /* If NACLENTRIES cannot be trusted, use dynamic memory
442                allocation.  */
443             abort ();
444
445           /* If there are more than 3 entries, there cannot be only the
446              (uid,%), (%,gid), (%,%) entries.  */
447           if (count > 3)
448             return 1;
449
450           if (getacl (name, count, entries) == count)
451             {
452               struct stat statbuf;
453
454               if (stat (name, &statbuf) < 0)
455                 return -1;
456
457               return acl_nontrivial (count, entries, &statbuf);
458             }
459           /* Huh? The number of ACL entries changed since the last call.
460              Repeat.  */
461         }
462
463 # elif HAVE_ACLX_GET && 0 /* AIX */
464
465       /* TODO: use aclx_get(), and then?  */
466
467 # elif HAVE_STATACL /* older AIX */
468
469       union { struct acl a; char room[4096]; } u;
470
471       if (statacl (name, STX_NORMAL, &u.a, sizeof (u)) < 0)
472         return -1;
473
474       return acl_nontrivial (&u.a);
475
476 # endif
477     }
478 #endif
479
480   return 0;
481 }