copy-acl: ignore ENOTSUP on HP-UX
[gnulib.git] / lib / acl-internal.h
1 /* Internal implementation of access control lists.
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 "acl.h"
21
22 #include <stdbool.h>
23 #include <stdlib.h>
24
25 /* All systems define the ACL related API in <sys/acl.h>.  */
26 #if HAVE_SYS_ACL_H
27 # include <sys/acl.h>
28 #endif
29 #if defined HAVE_ACL && ! defined GETACLCNT && defined ACL_CNT
30 # define GETACLCNT ACL_CNT
31 #endif
32
33 /* On Linux, additional ACL related API is available in <acl/libacl.h>.  */
34 #ifdef HAVE_ACL_LIBACL_H
35 # include <acl/libacl.h>
36 #endif
37
38 #include "error.h"
39 #include "quote.h"
40
41 #include <errno.h>
42 #ifndef ENOSYS
43 # define ENOSYS (-1)
44 #endif
45 #ifndef ENOTSUP
46 # define ENOTSUP (-1)
47 #endif
48
49 #ifndef HAVE_FCHMOD
50 # define HAVE_FCHMOD false
51 # define fchmod(fd, mode) (-1)
52 #endif
53
54 /* Recognize some common errors such as from an NFS mount that does
55    not support ACLs, even when local drives do.  */
56 #if defined __APPLE__ && defined __MACH__ /* MacOS X */
57 # define ACL_NOT_WELL_SUPPORTED(Err) \
58      ((Err) == ENOTSUP || (Err) == ENOSYS || (Err) == EINVAL || (Err) == EBUSY || (Err) == ENOENT)
59 #elif defined EOPNOTSUPP /* Tru64 NFS */
60 # define ACL_NOT_WELL_SUPPORTED(Err) \
61      ((Err) == ENOTSUP || (Err) == ENOSYS || (Err) == EINVAL || (Err) == EBUSY || (Err) == EOPNOTSUPP)
62 #else
63 # define ACL_NOT_WELL_SUPPORTED(Err) \
64      ((Err) == ENOTSUP || (Err) == ENOSYS || (Err) == EINVAL || (Err) == EBUSY)
65 #endif
66
67 #if USE_ACL
68
69 # if HAVE_ACL_GET_FILE
70 /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
71 /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
72
73 #  ifndef MIN_ACL_ENTRIES
74 #   define MIN_ACL_ENTRIES 4
75 #  endif
76
77 /* POSIX 1003.1e (draft 17) */
78 #  ifdef HAVE_ACL_GET_FD
79 /* Most platforms have a 1-argument acl_get_fd, only OSF/1 has a 2-argument
80    macro(!).  */
81 #   if HAVE_ACL_FREE_TEXT /* OSF/1 */
82 static inline acl_t
83 rpl_acl_get_fd (int fd)
84 {
85   return acl_get_fd (fd, ACL_TYPE_ACCESS);
86 }
87 #    undef acl_get_fd
88 #    define acl_get_fd rpl_acl_get_fd
89 #   endif
90 #  else
91 #   define HAVE_ACL_GET_FD false
92 #   undef acl_get_fd
93 #   define acl_get_fd(fd) (NULL)
94 #  endif
95
96 /* POSIX 1003.1e (draft 17) */
97 #  ifdef HAVE_ACL_SET_FD
98 /* Most platforms have a 2-argument acl_set_fd, only OSF/1 has a 3-argument
99    macro(!).  */
100 #   if HAVE_ACL_FREE_TEXT /* OSF/1 */
101 static inline int
102 rpl_acl_set_fd (int fd, acl_t acl)
103 {
104   return acl_set_fd (fd, ACL_TYPE_ACCESS, acl);
105 }
106 #    undef acl_set_fd
107 #    define acl_set_fd rpl_acl_set_fd
108 #   endif
109 #  else
110 #   define HAVE_ACL_SET_FD false
111 #   undef acl_set_fd
112 #   define acl_set_fd(fd, acl) (-1)
113 #  endif
114
115 /* POSIX 1003.1e (draft 13) */
116 #  if ! HAVE_ACL_FREE_TEXT
117 #   define acl_free_text(buf) acl_free (buf)
118 #  endif
119
120 /* Linux-specific */
121 #  ifndef HAVE_ACL_EXTENDED_FILE
122 #   define HAVE_ACL_EXTENDED_FILE false
123 #   define acl_extended_file(name) (-1)
124 #  endif
125
126 /* Linux-specific */
127 #  ifndef HAVE_ACL_FROM_MODE
128 #   define HAVE_ACL_FROM_MODE false
129 #   define acl_from_mode(mode) (NULL)
130 #  endif
131
132 /* Set to 1 if a file's mode is implicit by the ACL.
133    Set to 0 if a file's mode is stored independently from the ACL.  */
134 #  if HAVE_ACL_COPY_EXT_NATIVE && HAVE_ACL_CREATE_ENTRY_NP /* MacOS X */
135 #   define MODE_INSIDE_ACL 0
136 #  else
137 #   define MODE_INSIDE_ACL 1
138 #  endif
139
140 /* Return the number of entries in ACL.
141    Return -1 and set errno upon failure to determine it.  */
142 /* Define a replacement for acl_entries if needed. (Only Linux has it.)  */
143 #  if !HAVE_ACL_ENTRIES
144 #   define acl_entries rpl_acl_entries
145 extern int acl_entries (acl_t);
146 #  endif
147
148 #  if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
149 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
150    Return 1 if the given ACL is non-trivial.
151    Return 0 if it is trivial.  */
152 extern int acl_extended_nontrivial (acl_t);
153 #  else
154 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
155    Return 1 if the given ACL is non-trivial.
156    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
157    Return -1 and set errno upon failure to determine it.  */
158 extern int acl_access_nontrivial (acl_t);
159 #  endif
160
161 # elif HAVE_ACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
162
163 /* Set to 1 if a file's mode is implicit by the ACL.
164    Set to 0 if a file's mode is stored independently from the ACL.  */
165 #  if defined __CYGWIN__ /* Cygwin */
166 #   define MODE_INSIDE_ACL 0
167 #  else /* Solaris */
168 #   define MODE_INSIDE_ACL 1
169 #  endif
170
171 #  if !defined ACL_NO_TRIVIAL /* Solaris <= 10, Cygwin */
172
173 /* Return 1 if the given ACL is non-trivial.
174    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
175 extern int acl_nontrivial (int count, aclent_t *entries);
176
177 #   ifdef ACE_GETACL /* Solaris 10 */
178
179 /* Test an ACL retrieved with ACE_GETACL.
180    Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
181    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
182 extern int acl_ace_nontrivial (int count, ace_t *entries);
183
184 /* Definitions for when the built executable is executed on Solaris 10
185    (newer version) or Solaris 11.  */
186 /* For a_type.  */
187 #    define ACE_ACCESS_ALLOWED_ACE_TYPE 0 /* replaces ALLOW */
188 #    define ACE_ACCESS_DENIED_ACE_TYPE  1 /* replaces DENY */
189 /* For a_flags.  */
190 #    define NEW_ACE_OWNER            0x1000
191 #    define NEW_ACE_GROUP            0x2000
192 #    define NEW_ACE_IDENTIFIER_GROUP 0x0040
193 #    define ACE_EVERYONE             0x4000
194 /* For a_access_mask.  */
195 #    define NEW_ACE_READ_DATA  0x001 /* corresponds to 'r' */
196 #    define NEW_ACE_WRITE_DATA 0x002 /* corresponds to 'w' */
197 #    define NEW_ACE_EXECUTE    0x004 /* corresponds to 'x' */
198
199 #   endif
200
201 #  endif
202
203 # elif HAVE_GETACL /* HP-UX */
204
205 /* Return 1 if the given ACL is non-trivial.
206    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
207 extern int acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb);
208
209 # elif HAVE_ACLX_GET && 0 /* AIX */
210
211 /* TODO */
212
213 # elif HAVE_STATACL /* older AIX */
214
215 /* Return 1 if the given ACL is non-trivial.
216    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
217 extern int acl_nontrivial (struct acl *a);
218
219 # endif
220
221 #endif