89a63a658c6d3b0108e0638fedb775817b443a8a
[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-2012 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 /* Without this pragma, gcc 4.7.0 20120126 may suggest that the
21    file_has_acl function might be candidate for attribute 'const'  */
22 #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
23 # pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
24 #endif
25
26 #include <config.h>
27
28 #include "acl.h"
29
30 #include "acl-internal.h"
31
32
33 #if USE_ACL && HAVE_ACL_GET_FILE
34
35 # if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
36
37 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
38    Return 1 if the given ACL is non-trivial.
39    Return 0 if it is trivial.  */
40 int
41 acl_extended_nontrivial (acl_t acl)
42 {
43   /* acl is non-trivial if it is non-empty.  */
44   return (acl_entries (acl) > 0);
45 }
46
47 # else /* Linux, FreeBSD, IRIX, Tru64 */
48
49 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
50    Return 1 if the given ACL is non-trivial.
51    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
52    Return -1 and set errno upon failure to determine it.  */
53 int
54 acl_access_nontrivial (acl_t acl)
55 {
56   /* acl is non-trivial if it has some entries other than for "user::",
57      "group::", and "other::".  Normally these three should be present
58      at least, allowing us to write
59         return (3 < acl_entries (acl));
60      but the following code is more robust.  */
61 #  if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD */
62
63   acl_entry_t ace;
64   int got_one;
65
66   for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
67        got_one > 0;
68        got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
69     {
70       acl_tag_t tag;
71       if (acl_get_tag_type (ace, &tag) < 0)
72         return -1;
73       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
74         return 1;
75     }
76   return got_one;
77
78 #  else /* IRIX, Tru64 */
79 #   if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
80   /* Don't use acl_get_entry: it is undocumented.  */
81
82   int count = acl->acl_cnt;
83   int i;
84
85   for (i = 0; i < count; i++)
86     {
87       acl_entry_t ace = &acl->acl_entry[i];
88       acl_tag_t tag = ace->ae_tag;
89
90       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
91             || tag == ACL_OTHER_OBJ))
92         return 1;
93     }
94   return 0;
95
96 #   endif
97 #   if HAVE_ACL_FREE_TEXT /* Tru64 */
98   /* Don't use acl_get_entry: it takes only one argument and does not work.  */
99
100   int count = acl->acl_num;
101   acl_entry_t ace;
102
103   for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
104     {
105       acl_tag_t tag;
106       acl_perm_t perm;
107
108       tag = ace->entry->acl_type;
109       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
110         return 1;
111
112       perm = ace->entry->acl_perm;
113       /* On Tru64, perm can also contain non-standard bits such as
114          PERM_INSERT, PERM_DELETE, PERM_MODIFY, PERM_LOOKUP, ... */
115       if ((perm & ~(ACL_READ | ACL_WRITE | ACL_EXECUTE)) != 0)
116         return 1;
117     }
118   return 0;
119
120 #   endif
121 #  endif
122 }
123
124 # endif
125
126
127 #elif USE_ACL && HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
128
129 /* Test an ACL retrieved with GETACL.
130    Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
131    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
132 int
133 acl_nontrivial (int count, aclent_t *entries)
134 {
135   int i;
136
137   for (i = 0; i < count; i++)
138     {
139       aclent_t *ace = &entries[i];
140
141       /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
142          If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
143          We don't need to check ace->a_id in these cases.  */
144       if (!(ace->a_type == USER_OBJ
145             || ace->a_type == GROUP_OBJ
146             || ace->a_type == OTHER_OBJ
147             /* Note: Cygwin does not return a CLASS_OBJ ("mask:") entry
148                sometimes.  */
149             || ace->a_type == CLASS_OBJ))
150         return 1;
151     }
152   return 0;
153 }
154
155 # ifdef ACE_GETACL
156
157 /* Test an ACL retrieved with ACE_GETACL.
158    Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
159    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
160 int
161 acl_ace_nontrivial (int count, ace_t *entries)
162 {
163   int i;
164
165   /* The flags in the ace_t structure changed in a binary incompatible way
166      when ACL_NO_TRIVIAL etc. were introduced in <sys/acl.h> version 1.15.
167      How to distinguish the two conventions at runtime?
168      In the old convention, usually three ACEs have a_flags = ACE_OWNER /
169      ACE_GROUP / ACE_OTHER, in the range 0x0100..0x0400.  In the new
170      convention, these values are not used.  */
171   int old_convention = 0;
172
173   for (i = 0; i < count; i++)
174     if (entries[i].a_flags & (OLD_ACE_OWNER | OLD_ACE_GROUP | OLD_ACE_OTHER))
175       {
176         old_convention = 1;
177         break;
178       }
179
180   if (old_convention)
181     /* Running on Solaris 10.  */
182     for (i = 0; i < count; i++)
183       {
184         ace_t *ace = &entries[i];
185
186         /* Note:
187            If ace->a_flags = ACE_OWNER, ace->a_who is the st_uid from stat().
188            If ace->a_flags = ACE_GROUP, ace->a_who is the st_gid from stat().
189            We don't need to check ace->a_who in these cases.  */
190         if (!(ace->a_type == OLD_ALLOW
191               && (ace->a_flags == OLD_ACE_OWNER
192                   || ace->a_flags == OLD_ACE_GROUP
193                   || ace->a_flags == OLD_ACE_OTHER)))
194           return 1;
195       }
196   else
197     {
198       /* Running on Solaris 10 (newer version) or Solaris 11.  */
199       unsigned int access_masks[6] =
200         {
201           0, /* owner@ deny */
202           0, /* owner@ allow */
203           0, /* group@ deny */
204           0, /* group@ allow */
205           0, /* everyone@ deny */
206           0  /* everyone@ allow */
207         };
208
209       for (i = 0; i < count; i++)
210         {
211           ace_t *ace = &entries[i];
212           unsigned int index1;
213           unsigned int index2;
214
215           if (ace->a_type == NEW_ACE_ACCESS_ALLOWED_ACE_TYPE)
216             index1 = 1;
217           else if (ace->a_type == NEW_ACE_ACCESS_DENIED_ACE_TYPE)
218             index1 = 0;
219           else
220             return 1;
221
222           if (ace->a_flags == NEW_ACE_OWNER)
223             index2 = 0;
224           else if (ace->a_flags == (NEW_ACE_GROUP | NEW_ACE_IDENTIFIER_GROUP))
225             index2 = 2;
226           else if (ace->a_flags == NEW_ACE_EVERYONE)
227             index2 = 4;
228           else
229             return 1;
230
231           access_masks[index1 + index2] |= ace->a_access_mask;
232         }
233
234       /* The same bit shouldn't be both allowed and denied.  */
235       if (access_masks[0] & access_masks[1])
236         return 1;
237       if (access_masks[2] & access_masks[3])
238         return 1;
239       if (access_masks[4] & access_masks[5])
240         return 1;
241
242       /* Check minimum masks.  */
243       if ((NEW_ACE_WRITE_NAMED_ATTRS
244            | NEW_ACE_WRITE_ATTRIBUTES
245            | NEW_ACE_WRITE_ACL
246            | NEW_ACE_WRITE_OWNER)
247           & ~ access_masks[1])
248         return 1;
249       access_masks[1] &= ~(NEW_ACE_WRITE_NAMED_ATTRS
250                            | NEW_ACE_WRITE_ATTRIBUTES
251                            | NEW_ACE_WRITE_ACL
252                            | NEW_ACE_WRITE_OWNER);
253       if ((NEW_ACE_WRITE_NAMED_ATTRS
254            | NEW_ACE_WRITE_ATTRIBUTES
255            | NEW_ACE_WRITE_ACL
256            | NEW_ACE_WRITE_OWNER)
257           & ~ access_masks[4])
258         return 1;
259       access_masks[4] &= ~(NEW_ACE_WRITE_NAMED_ATTRS
260                            | NEW_ACE_WRITE_ATTRIBUTES
261                            | NEW_ACE_WRITE_ACL
262                            | NEW_ACE_WRITE_OWNER);
263       if ((NEW_ACE_READ_NAMED_ATTRS
264            | NEW_ACE_READ_ATTRIBUTES
265            | NEW_ACE_READ_ACL
266            | NEW_ACE_SYNCHRONIZE)
267           & ~ access_masks[5])
268         return 1;
269       access_masks[5] &= ~(NEW_ACE_READ_NAMED_ATTRS
270                            | NEW_ACE_READ_ATTRIBUTES
271                            | NEW_ACE_READ_ACL
272                            | NEW_ACE_SYNCHRONIZE);
273
274       /* Check the allowed or denied bits.  */
275       if ((access_masks[0] | access_masks[1])
276           != (NEW_ACE_READ_DATA
277               | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
278               | NEW_ACE_EXECUTE))
279         return 1;
280       if ((access_masks[2] | access_masks[3])
281           != (NEW_ACE_READ_DATA
282               | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
283               | NEW_ACE_EXECUTE))
284         return 1;
285       if ((access_masks[4] | access_masks[5])
286           != (NEW_ACE_READ_DATA
287               | NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA
288               | NEW_ACE_EXECUTE))
289         return 1;
290
291       /* Check that the NEW_ACE_WRITE_DATA and NEW_ACE_APPEND_DATA bits are
292          either both allowed or both denied.  */
293       if (((access_masks[0] & NEW_ACE_WRITE_DATA) != 0)
294           != ((access_masks[0] & NEW_ACE_APPEND_DATA) != 0))
295         return 1;
296       if (((access_masks[2] & NEW_ACE_WRITE_DATA) != 0)
297           != ((access_masks[2] & NEW_ACE_APPEND_DATA) != 0))
298         return 1;
299       if (((access_masks[4] & NEW_ACE_WRITE_DATA) != 0)
300           != ((access_masks[4] & NEW_ACE_APPEND_DATA) != 0))
301         return 1;
302     }
303
304   return 0;
305 }
306
307 # endif
308
309 #elif USE_ACL && HAVE_GETACL /* HP-UX */
310
311 /* Return 1 if the given ACL is non-trivial.
312    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
313 int
314 acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb)
315 {
316   int i;
317
318   for (i = 0; i < count; i++)
319     {
320       struct acl_entry *ace = &entries[i];
321
322       if (!((ace->uid == sb->st_uid && ace->gid == ACL_NSGROUP)
323             || (ace->uid == ACL_NSUSER && ace->gid == sb->st_gid)
324             || (ace->uid == ACL_NSUSER && ace->gid == ACL_NSGROUP)))
325         return 1;
326     }
327   return 0;
328 }
329
330 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
331
332 /* Return 1 if the given ACL is non-trivial.
333    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
334 int
335 aclv_nontrivial (int count, struct acl *entries)
336 {
337   int i;
338
339   for (i = 0; i < count; i++)
340     {
341       struct acl *ace = &entries[i];
342
343       /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
344          If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
345          We don't need to check ace->a_id in these cases.  */
346       if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
347             || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
348             || ace->a_type == CLASS_OBJ
349             || ace->a_type == OTHER_OBJ))
350         return 1;
351     }
352   return 0;
353 }
354
355 # endif
356
357 #elif USE_ACL && (HAVE_ACLX_GET || HAVE_STATACL) /* AIX */
358
359 /* Return 1 if the given ACL is non-trivial.
360    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
361 int
362 acl_nontrivial (struct acl *a)
363 {
364   /* The normal way to iterate through an ACL is like this:
365        struct acl_entry *ace;
366        for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
367          {
368            struct ace_id *aei;
369            switch (ace->ace_type)
370              {
371              case ACC_PERMIT:
372              case ACC_DENY:
373              case ACC_SPECIFY:
374                ...;
375              }
376            for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
377              ...
378          }
379    */
380   return (acl_last (a) != a->acl_ext ? 1 : 0);
381 }
382
383 # if HAVE_ACLX_GET && defined ACL_AIX_WIP /* newer AIX */
384
385 /* Return 1 if the given ACL is non-trivial.
386    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
387 int
388 acl_nfs4_nontrivial (nfs4_acl_int_t *a)
389 {
390 #  if 1 /* let's try this first */
391   return (a->aclEntryN > 0 ? 1 : 0);
392 #  else
393   int count = a->aclEntryN;
394   int i;
395
396   for (i = 0; i < count; i++)
397     {
398       nfs4_ace_int_t *ace = &a->aclEntry[i];
399
400       if (!((ace->flags & ACE4_ID_SPECIAL) != 0
401             && (ace->aceWho.special_whoid == ACE4_WHO_OWNER
402                 || ace->aceWho.special_whoid == ACE4_WHO_GROUP
403                 || ace->aceWho.special_whoid == ACE4_WHO_EVERYONE)
404             && ace->aceType == ACE4_ACCESS_ALLOWED_ACE_TYPE
405             && ace->aceFlags == 0
406             && (ace->aceMask & ~(ACE4_READ_DATA | ACE4_LIST_DIRECTORY
407                                  | ACE4_WRITE_DATA | ACE4_ADD_FILE
408                                  | ACE4_EXECUTE)) == 0))
409         return 1;
410     }
411   return 0;
412 #  endif
413 }
414
415 # endif
416
417 #elif USE_ACL && HAVE_ACLSORT /* NonStop Kernel */
418
419 /* Test an ACL retrieved with ACL_GET.
420    Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
421    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
422 int
423 acl_nontrivial (int count, struct acl *entries)
424 {
425   int i;
426
427   for (i = 0; i < count; i++)
428     {
429       struct acl *ace = &entries[i];
430
431       /* Note: If ace->a_type = USER_OBJ, ace->a_id is the st_uid from stat().
432          If ace->a_type = GROUP_OBJ, ace->a_id is the st_gid from stat().
433          We don't need to check ace->a_id in these cases.  */
434       if (!(ace->a_type == USER_OBJ /* no need to check ace->a_id here */
435             || ace->a_type == GROUP_OBJ /* no need to check ace->a_id here */
436             || ace->a_type == CLASS_OBJ
437             || ace->a_type == OTHER_OBJ))
438         return 1;
439     }
440   return 0;
441 }
442
443 #endif
444
445
446 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
447    only has no or a base access control list, and -1 (setting errno)
448    on error.  SB must be set to the stat buffer of NAME, obtained
449    through stat() or lstat().  */
450
451 int
452 file_has_acl (char const *name, struct stat const *sb)
453 {
454 #if USE_ACL
455   if (! S_ISLNK (sb->st_mode))
456     {
457 # if HAVE_ACL_GET_FILE
458
459       /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
460       /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
461       int ret;
462
463       if (HAVE_ACL_EXTENDED_FILE) /* Linux */
464         {
465           /* On Linux, acl_extended_file is an optimized function: It only
466              makes two calls to getxattr(), one for ACL_TYPE_ACCESS, one for
467              ACL_TYPE_DEFAULT.  */
468           ret = acl_extended_file (name);
469         }
470       else /* FreeBSD, MacOS X, IRIX, Tru64 */
471         {
472 #  if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
473           /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
474              and acl_get_file (name, ACL_TYPE_DEFAULT)
475              always return NULL / EINVAL.  There is no point in making
476              these two useless calls.  The real ACL is retrieved through
477              acl_get_file (name, ACL_TYPE_EXTENDED).  */
478           acl_t acl = acl_get_file (name, ACL_TYPE_EXTENDED);
479           if (acl)
480             {
481               ret = acl_extended_nontrivial (acl);
482               acl_free (acl);
483             }
484           else
485             ret = -1;
486 #  else /* FreeBSD, IRIX, Tru64 */
487           acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
488           if (acl)
489             {
490               int saved_errno;
491
492               ret = acl_access_nontrivial (acl);
493               saved_errno = errno;
494               acl_free (acl);
495               errno = saved_errno;
496 #   if HAVE_ACL_FREE_TEXT /* Tru64 */
497               /* On OSF/1, acl_get_file (name, ACL_TYPE_DEFAULT) always
498                  returns NULL with errno not set.  There is no point in
499                  making this call.  */
500 #   else /* FreeBSD, IRIX */
501               /* On Linux, FreeBSD, IRIX, acl_get_file (name, ACL_TYPE_ACCESS)
502                  and acl_get_file (name, ACL_TYPE_DEFAULT) on a directory
503                  either both succeed or both fail; it depends on the
504                  file system.  Therefore there is no point in making the second
505                  call if the first one already failed.  */
506               if (ret == 0 && S_ISDIR (sb->st_mode))
507                 {
508                   acl = acl_get_file (name, ACL_TYPE_DEFAULT);
509                   if (acl)
510                     {
511                       ret = (0 < acl_entries (acl));
512                       acl_free (acl);
513                     }
514                   else
515                     ret = -1;
516                 }
517 #   endif
518             }
519           else
520             ret = -1;
521 #  endif
522         }
523       if (ret < 0)
524         return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
525       return ret;
526
527 # elif HAVE_FACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
528
529 #  if defined ACL_NO_TRIVIAL
530
531       /* Solaris 10 (newer version), which has additional API declared in
532          <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
533          acl_fromtext, ...).  */
534       return acl_trivial (name);
535
536 #  else /* Solaris, Cygwin, general case */
537
538       /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
539          of Unixware.  The acl() call returns the access and default ACL both
540          at once.  */
541       int count;
542       {
543         aclent_t *entries;
544
545         for (;;)
546           {
547             count = acl (name, GETACLCNT, 0, NULL);
548
549             if (count < 0)
550               {
551                 if (errno == ENOSYS || errno == ENOTSUP)
552                   break;
553                 else
554                   return -1;
555               }
556
557             if (count == 0)
558               break;
559
560             /* Don't use MIN_ACL_ENTRIES:  It's set to 4 on Cygwin, but Cygwin
561                returns only 3 entries for files with no ACL.  But this is safe:
562                If there are more than 4 entries, there cannot be only the
563                "user::", "group::", "other:", and "mask:" entries.  */
564             if (count > 4)
565               return 1;
566
567             entries = (aclent_t *) malloc (count * sizeof (aclent_t));
568             if (entries == NULL)
569               {
570                 errno = ENOMEM;
571                 return -1;
572               }
573             if (acl (name, GETACL, count, entries) == count)
574               {
575                 if (acl_nontrivial (count, entries))
576                   {
577                     free (entries);
578                     return 1;
579                   }
580                 free (entries);
581                 break;
582               }
583             /* Huh? The number of ACL entries changed since the last call.
584                Repeat.  */
585             free (entries);
586           }
587       }
588
589 #   ifdef ACE_GETACL
590       /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
591          file systems (whereas the other ones are used in UFS file systems).  */
592       {
593         ace_t *entries;
594
595         for (;;)
596           {
597             count = acl (name, ACE_GETACLCNT, 0, NULL);
598
599             if (count < 0)
600               {
601                 if (errno == ENOSYS || errno == EINVAL)
602                   break;
603                 else
604                   return -1;
605               }
606
607             if (count == 0)
608               break;
609
610             /* In the old (original Solaris 10) convention:
611                If there are more than 3 entries, there cannot be only the
612                ACE_OWNER, ACE_GROUP, ACE_OTHER entries.
613                In the newer Solaris 10 and Solaris 11 convention:
614                If there are more than 6 entries, there cannot be only the
615                ACE_OWNER, ACE_GROUP, ACE_EVERYONE entries, each once with
616                NEW_ACE_ACCESS_ALLOWED_ACE_TYPE and once with
617                NEW_ACE_ACCESS_DENIED_ACE_TYPE.  */
618             if (count > 6)
619               return 1;
620
621             entries = (ace_t *) malloc (count * sizeof (ace_t));
622             if (entries == NULL)
623               {
624                 errno = ENOMEM;
625                 return -1;
626               }
627             if (acl (name, ACE_GETACL, count, entries) == count)
628               {
629                 if (acl_ace_nontrivial (count, entries))
630                   {
631                     free (entries);
632                     return 1;
633                   }
634                 free (entries);
635                 break;
636               }
637             /* Huh? The number of ACL entries changed since the last call.
638                Repeat.  */
639             free (entries);
640           }
641       }
642 #   endif
643
644       return 0;
645 #  endif
646
647 # elif HAVE_GETACL /* HP-UX */
648
649       for (;;)
650         {
651           int count;
652           struct acl_entry entries[NACLENTRIES];
653
654           count = getacl (name, 0, NULL);
655
656           if (count < 0)
657             {
658               /* ENOSYS is seen on newer HP-UX versions.
659                  EOPNOTSUPP is typically seen on NFS mounts.
660                  ENOTSUP was seen on Quantum StorNext file systems (cvfs).  */
661               if (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP)
662                 break;
663               else
664                 return -1;
665             }
666
667           if (count == 0)
668             return 0;
669
670           if (count > NACLENTRIES)
671             /* If NACLENTRIES cannot be trusted, use dynamic memory
672                allocation.  */
673             abort ();
674
675           /* If there are more than 3 entries, there cannot be only the
676              (uid,%), (%,gid), (%,%) entries.  */
677           if (count > 3)
678             return 1;
679
680           if (getacl (name, count, entries) == count)
681             {
682               struct stat statbuf;
683
684               if (stat (name, &statbuf) < 0)
685                 return -1;
686
687               return acl_nontrivial (count, entries, &statbuf);
688             }
689           /* Huh? The number of ACL entries changed since the last call.
690              Repeat.  */
691         }
692
693 #  if HAVE_ACLV_H /* HP-UX >= 11.11 */
694
695       for (;;)
696         {
697           int count;
698           struct acl entries[NACLVENTRIES];
699
700           count = acl ((char *) name, ACL_CNT, NACLVENTRIES, entries);
701
702           if (count < 0)
703             {
704               /* EOPNOTSUPP is seen on NFS in HP-UX 11.11, 11.23.
705                  EINVAL is seen on NFS in HP-UX 11.31.  */
706               if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
707                 break;
708               else
709                 return -1;
710             }
711
712           if (count == 0)
713             return 0;
714
715           if (count > NACLVENTRIES)
716             /* If NACLVENTRIES cannot be trusted, use dynamic memory
717                allocation.  */
718             abort ();
719
720           /* If there are more than 4 entries, there cannot be only the
721              four base ACL entries.  */
722           if (count > 4)
723             return 1;
724
725           if (acl ((char *) name, ACL_GET, count, entries) == count)
726             return aclv_nontrivial (count, entries);
727           /* Huh? The number of ACL entries changed since the last call.
728              Repeat.  */
729         }
730
731 #  endif
732
733 # elif HAVE_ACLX_GET && defined ACL_AIX_WIP /* AIX */
734
735       acl_type_t type;
736       char aclbuf[1024];
737       void *acl = aclbuf;
738       size_t aclsize = sizeof (aclbuf);
739       mode_t mode;
740
741       for (;;)
742         {
743           /* The docs say that type being 0 is equivalent to ACL_ANY, but it
744              is not true, in AIX 5.3.  */
745           type.u64 = ACL_ANY;
746           if (aclx_get (name, 0, &type, aclbuf, &aclsize, &mode) >= 0)
747             break;
748           if (errno == ENOSYS)
749             return 0;
750           if (errno != ENOSPC)
751             {
752               if (acl != aclbuf)
753                 {
754                   int saved_errno = errno;
755                   free (acl);
756                   errno = saved_errno;
757                 }
758               return -1;
759             }
760           aclsize = 2 * aclsize;
761           if (acl != aclbuf)
762             free (acl);
763           acl = malloc (aclsize);
764           if (acl == NULL)
765             {
766               errno = ENOMEM;
767               return -1;
768             }
769         }
770
771       if (type.u64 == ACL_AIXC)
772         {
773           int result = acl_nontrivial ((struct acl *) acl);
774           if (acl != aclbuf)
775             free (acl);
776           return result;
777         }
778       else if (type.u64 == ACL_NFS4)
779         {
780           int result = acl_nfs4_nontrivial ((nfs4_acl_int_t *) acl);
781           if (acl != aclbuf)
782             free (acl);
783           return result;
784         }
785       else
786         {
787           /* A newer type of ACL has been introduced in the system.
788              We should better support it.  */
789           if (acl != aclbuf)
790             free (acl);
791           errno = EINVAL;
792           return -1;
793         }
794
795 # elif HAVE_STATACL /* older AIX */
796
797       union { struct acl a; char room[4096]; } u;
798
799       if (statacl (name, STX_NORMAL, &u.a, sizeof (u)) < 0)
800         return -1;
801
802       return acl_nontrivial (&u.a);
803
804 # elif HAVE_ACLSORT /* NonStop Kernel */
805
806       int count;
807       struct acl entries[NACLENTRIES];
808
809       for (;;)
810         {
811           count = acl ((char *) name, ACL_CNT, NACLENTRIES, NULL);
812
813           if (count < 0)
814             {
815               if (errno == ENOSYS || errno == ENOTSUP)
816                 break;
817               else
818                 return -1;
819             }
820
821           if (count == 0)
822             return 0;
823
824           if (count > NACLENTRIES)
825             /* If NACLENTRIES cannot be trusted, use dynamic memory
826                allocation.  */
827             abort ();
828
829           /* If there are more than 4 entries, there cannot be only the
830              four base ACL entries.  */
831           if (count > 4)
832             return 1;
833
834           if (acl ((char *) name, ACL_GET, count, entries) == count)
835             return acl_nontrivial (count, entries);
836           /* Huh? The number of ACL entries changed since the last call.
837              Repeat.  */
838         }
839
840 # endif
841     }
842 #endif
843
844   return 0;
845 }