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