1/* Test whether a file has a nontrivial access control list.
2
3   Copyright (C) 2002-2003, 2005-2010 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.  */
34int
35acl_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.  */
47int
48acl_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 got_one;
59
60  for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
61       got_one > 0;
62       got_one = 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 got_one;
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# if !defined ACL_NO_TRIVIAL /* Solaris <= 10, Cygwin */
124
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.  */
128int
129acl_nontrivial (int count, aclent_t *entries)
130{
131  int i;
132
133  for (i = 0; i < count; i++)
134    {
135      aclent_t *ace = &entries[i];
136
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
144               sometimes.  */
145            || ace->a_type == CLASS_OBJ))
146        return 1;
147    }
148  return 0;
149}
150
151#  ifdef ACE_GETACL
152
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.  */
156int
157acl_ace_nontrivial (int count, ace_t *entries)
158{
159  int i;
160
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;
168
169  for (i = 0; i < count; i++)
170    if (entries[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_OTHER))
171      {
172        old_convention = 1;
173        break;
174      }
175
176  if (old_convention)
177    /* Running on Solaris 10.  */
178    for (i = 0; i < count; i++)
179      {
180        ace_t *ace = &entries[i];
181
182        /* Note:
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)))
190          return 1;
191      }
192  else
193    /* Running on Solaris 10 (newer version) or Solaris 11.  */
194    for (i = 0; i < count; i++)
195      {
196        ace_t *ace = &entries[i];
197
198        if (!(ace->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE
199              && (ace->a_flags == NEW_ACE_OWNER
200                  || ace->a_flags
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))
205                 == 0))
206          return 1;
207      }
208
209  return 0;
210}
211
212#  endif
213
214# endif
215
216#elif USE_ACL && HAVE_GETACL /* HP-UX */
217
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.  */
220int
221acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb)
222{
223  int i;
224
225  for (i = 0; i < count; i++)
226    {
227      struct acl_entry *ace = &entries[i];
228
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)))
232        return 1;
233    }
234  return 0;
235}
236
237#elif USE_ACL && (HAVE_ACLX_GET || HAVE_STATACL) /* AIX */
238
239/* Return 1 if the given ACL is non-trivial.
240   Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
241int
242acl_nontrivial (struct acl *a)
243{
244  /* The normal way to iterate through an ACL is like this:
245       struct acl_entry *ace;
246       for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
247         {
248           struct ace_id *aei;
249           switch (ace->ace_type)
250             {
251             case ACC_PERMIT:
252             case ACC_DENY:
253             case ACC_SPECIFY:
254               ...;
255             }
256           for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
257             ...
258         }
259   */
260  return (acl_last (a) != a->acl_ext ? 1 : 0);
261}
262
263# if HAVE_ACLX_GET && defined ACL_AIX_WIP /* newer AIX */
264
265/* Return 1 if the given ACL is non-trivial.
266   Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
267int
268acl_nfs4_nontrivial (nfs4_acl_int_t *a)
269{
270#  if 1 /* let's try this first */
271  return (a->aclEntryN > 0 ? 1 : 0);
272#  else
273  int count = a->aclEntryN;
274  int i;
275
276  for (i = 0; i < count; i++)
277    {
278      nfs4_ace_int_t *ace = &a->aclEntry[i];
279
280      if (!((ace->flags & ACE4_ID_SPECIAL) != 0
281            && (ace->aceWho.special_whoid == ACE4_WHO_OWNER
282                || ace->aceWho.special_whoid == ACE4_WHO_GROUP
283                || ace->aceWho.special_whoid == ACE4_WHO_EVERYONE)
284            && ace->aceType == ACE4_ACCESS_ALLOWED_ACE_TYPE
285            && ace->aceFlags == 0
286            && (ace->aceMask & ~(ACE4_READ_DATA | ACE4_LIST_DIRECTORY
287                                 | ACE4_WRITE_DATA | ACE4_ADD_FILE
288                                 | ACE4_EXECUTE)) == 0))
289        return 1;
290    }
291  return 0;
292#  endif
293}
294
295# endif
296
297#endif
298
299
300/* Return 1 if NAME has a nontrivial access control list, 0 if NAME
301   only has no or a base access control list, and -1 (setting errno)
302   on error.  SB must be set to the stat buffer of FILE.  */
303
304int
305file_has_acl (char const *name, struct stat const *sb)
306{
307#if USE_ACL
308  if (! S_ISLNK (sb->st_mode))
309    {
310# if HAVE_ACL_GET_FILE
311
312      /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
313      /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
314      int ret;
315
316      if (HAVE_ACL_EXTENDED_FILE) /* Linux */
317        {
318          /* On Linux, acl_extended_file is an optimized function: It only
319             makes two calls to getxattr(), one for ACL_TYPE_ACCESS, one for
320             ACL_TYPE_DEFAULT.  */
321          ret = acl_extended_file (name);
322        }
323      else /* FreeBSD, MacOS X, IRIX, Tru64 */
324        {
325#  if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
326          /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
327             and acl_get_file (name, ACL_TYPE_DEFAULT)
328             always return NULL / EINVAL.  There is no point in making
329             these two useless calls.  The real ACL is retrieved through
330             acl_get_file (name, ACL_TYPE_EXTENDED).  */
331          acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
332          if (acl)
333            {
334              ret = acl_extended_nontrivial (acl);
335              acl_free (acl);
336            }
337          else
338            ret = -1;
339#  else /* FreeBSD, IRIX, Tru64 */
340          acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
341          if (acl)
342            {
343              int saved_errno;
344
345              ret = acl_access_nontrivial (acl);
346              saved_errno = errno;
347              acl_free (acl);
348              errno = saved_errno;
349#   if HAVE_ACL_FREE_TEXT /* Tru64 */
350              /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
351                 returns NULL with errno not set.  There is no point in
352                 making this call.  */
353#   else /* FreeBSD, IRIX */
354              /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
355                 and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
356                 either both succeed or both fail; it depends on the
357                 file system.  Therefore there is no point in making the second
358                 call if the first one already failed.  */
359              if (ret == 0 && S_ISDIR (sb->st_mode))
360                {
361                  acl = acl_get_file (name, ACL_TYPE_DEFAULT);
362                  if (acl)
363                    {
364                      ret = (0 < acl_entries (acl));
365                      acl_free (acl);
366                    }
367                  else
368                    ret = -1;
369                }
370#   endif
371            }
372          else
373            ret = -1;
374#  endif
375        }
376      if (ret < 0)
377        return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
378      return ret;
379
380# elif HAVE_ACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
381
382#  if defined ACL_NO_TRIVIAL
383
384      /* Solaris 10 (newer version), which has additional API declared in
385         <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
386         acl_fromtext, ...).  */
387      return acl_trivial (name);
388
389#  else /* Solaris, Cygwin, general case */
390
391      /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
392         of Unixware.  The acl() call returns the access and default ACL both
393         at once.  */
394      int count;
395      {
396        aclent_t *entries;
397
398        for (;;)
399          {
400            count = acl (name, GETACLCNT, 0, NULL);
401
402            if (count < 0)
403              {
404                if (errno == ENOSYS || errno == ENOTSUP)
405                  break;
406                else
407                  return -1;
408              }
409
410            if (count == 0)
411              break;
412
413            /* Don't use MIN_ACL_ENTRIES:  It's set to 4 on Cygwin, but Cygwin
414               returns only 3 entries for files with no ACL.  But this is safe:
415               If there are more than 4 entries, there cannot be only the
416               "user::", "group::", "other:", and "mask:" entries.  */
417            if (count > 4)
418              return 1;
419
420            entries = (aclent_t *) malloc (count * sizeof (aclent_t));
421            if (entries == NULL)
422              {
423                errno = ENOMEM;
424                return -1;
425              }
426            if (acl (name, GETACL, count, entries) == count)
427              {
428                if (acl_nontrivial (count, entries))
429                  {
430                    free (entries);
431                    return 1;
432                  }
433                free (entries);
434                break;
435              }
436            /* Huh? The number of ACL entries changed since the last call.
437               Repeat.  */
438            free (entries);
439          }
440      }
441
442#   ifdef ACE_GETACL
443      /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
444         file systems (whereas the other ones are used in UFS file systems).  */
445      {
446        ace_t *entries;
447
448        for (;;)
449          {
450            count = acl (name, ACE_GETACLCNT, 0, NULL);
451
452            if (count < 0)
453              {
454                if (errno == ENOSYS || errno == EINVAL)
455                  break;
456                else
457                  return -1;
458              }
459
460            if (count == 0)
461              break;
462
463            /* If there are more than 3 entries, there cannot be only the
464               ACE_OWNER, ACE_GROUP, ACE_OTHER entries.  */
465            if (count > 3)
466              return 1;
467
468            entries = (ace_t *) malloc (count * sizeof (ace_t));
469            if (entries == NULL)
470              {
471                errno = ENOMEM;
472                return -1;
473              }
474            if (acl (name, ACE_GETACL, count, entries) == count)
475              {
476                if (acl_ace_nontrivial (count, entries))
477                  {
478                    free (entries);
479                    return 1;
480                  }
481                free (entries);
482                break;
483              }
484            /* Huh? The number of ACL entries changed since the last call.
485               Repeat.  */
486            free (entries);
487          }
488      }
489#   endif
490
491      return 0;
492#  endif
493
494# elif HAVE_GETACL /* HP-UX */
495
496      int count;
497      struct acl_entry entries[NACLENTRIES];
498
499      for (;;)
500        {
501          count = getacl (name, 0, NULL);
502
503          if (count < 0)
504            return (errno == ENOSYS || errno == EOPNOTSUPP ? 0 : -1);
505
506          if (count == 0)
507            return 0;
508
509          if (count > NACLENTRIES)
510            /* If NACLENTRIES cannot be trusted, use dynamic memory
511               allocation.  */
512            abort ();
513
514          /* If there are more than 3 entries, there cannot be only the
515             (uid,%), (%,gid), (%,%) entries.  */
516          if (count > 3)
517            return 1;
518
519          if (getacl (name, count, entries) == count)
520            {
521              struct stat statbuf;
522
523              if (stat (name, &statbuf) < 0)
524                return -1;
525
526              return acl_nontrivial (count, entries, &statbuf);
527            }
528          /* Huh? The number of ACL entries changed since the last call.
529             Repeat.  */
530        }
531
532# elif HAVE_ACLX_GET && defined ACL_AIX_WIP /* AIX */
533
534      acl_type_t type;
535      char aclbuf[1024];
536      void *acl = aclbuf;
537      size_t aclsize = sizeof (aclbuf);
538      mode_t mode;
539
540      for (;;)
541        {
542          /* The docs say that type being 0 is equivalent to ACL_ANY, but it
543             is not true, in AIX 5.3.  */
544          type.u64 = ACL_ANY;
545          if (aclx_get (name, 0, &type, aclbuf, &aclsize, &mode) >= 0)
546            break;
547          if (errno != ENOSPC)
548            {
549              if (acl != aclbuf)
550                {
551                  int saved_errno = errno;
552                  free (acl);
553                  errno = saved_errno;
554                }
555              return -1;
556            }
557          aclsize = 2 * aclsize;
558          if (acl != aclbuf)
559            free (acl);
560          acl = malloc (aclsize);
561          if (acl == NULL)
562            {
563              errno = ENOMEM;
564              return -1;
565            }
566        }
567
568      if (type.u64 == ACL_AIXC)
569        {
570          int result = acl_nontrivial ((struct acl *) acl);
571          if (acl != aclbuf)
572            free (acl);
573          return result;
574        }
575      else if (type.u64 == ACL_NFS4)
576        {
577          int result = acl_nfs4_nontrivial ((nfs4_acl_int_t *) acl);
578          if (acl != aclbuf)
579            free (acl);
580          return result;
581        }
582      else
583        {
584          /* A newer type of ACL has been introduced in the system.
585             We should better support it.  */
586          if (acl != aclbuf)
587            free (acl);
588          errno = EINVAL;
589          return -1;
590        }
591
592# elif HAVE_STATACL /* older AIX */
593
594      union { struct acl a; char room[4096]; } u;
595
596      if (statacl (name, STX_NORMAL, &u.a, sizeof (u)) < 0)
597        return -1;
598
599      return acl_nontrivial (&u.a);
600
601# endif
602    }
603#endif
604
605  return 0;
606}
607