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