maint: update all copyright year number ranges
[gnulib.git] / lib / acl-internal.h
1 /* Internal implementation of access control lists.
2
3    Copyright (C) 2002-2003, 2005-2013 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_FACL && ! 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 /* On HP-UX >= 11.11, additional ACL API is available in <aclv.h>.  */
39 #if HAVE_ACLV_H
40 # include <sys/types.h>
41 # include <aclv.h>
42 /* HP-UX 11.11 lacks these declarations.  */
43 extern int acl (char *, int, int, struct acl *);
44 extern int aclsort (int, int, struct acl *);
45 #endif
46
47 #include "error.h"
48 #include "quote.h"
49
50 #include <errno.h>
51 #ifndef ENOSYS
52 # define ENOSYS (-1)
53 #endif
54 #ifndef ENOTSUP
55 # define ENOTSUP (-1)
56 #endif
57
58 #include <limits.h>
59 #ifndef MIN
60 # define MIN(a,b) ((a) < (b) ? (a) : (b))
61 #endif
62
63 #ifndef SIZE_MAX
64 # define SIZE_MAX ((size_t) -1)
65 #endif
66
67 #ifndef HAVE_FCHMOD
68 # define HAVE_FCHMOD false
69 # define fchmod(fd, mode) (-1)
70 #endif
71
72 /* Recognize some common errors such as from an NFS mount that does
73    not support ACLs, even when local drives do.  */
74 #if defined __APPLE__ && defined __MACH__ /* Mac OS X */
75 # define ACL_NOT_WELL_SUPPORTED(Err) \
76      ((Err) == ENOTSUP || (Err) == ENOSYS || (Err) == EINVAL || (Err) == EBUSY || (Err) == ENOENT)
77 #elif defined EOPNOTSUPP /* Tru64 NFS */
78 # define ACL_NOT_WELL_SUPPORTED(Err) \
79      ((Err) == ENOTSUP || (Err) == ENOSYS || (Err) == EINVAL || (Err) == EBUSY || (Err) == EOPNOTSUPP)
80 #else
81 # define ACL_NOT_WELL_SUPPORTED(Err) \
82      ((Err) == ENOTSUP || (Err) == ENOSYS || (Err) == EINVAL || (Err) == EBUSY)
83 #endif
84
85 _GL_INLINE_HEADER_BEGIN
86 #ifndef ACL_INTERNAL_INLINE
87 # define ACL_INTERNAL_INLINE _GL_INLINE
88 #endif
89
90 #if USE_ACL
91
92 # if HAVE_ACL_GET_FILE
93 /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
94 /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
95
96 #  ifndef MIN_ACL_ENTRIES
97 #   define MIN_ACL_ENTRIES 4
98 #  endif
99
100 /* POSIX 1003.1e (draft 17) */
101 #  ifdef HAVE_ACL_GET_FD
102 /* Most platforms have a 1-argument acl_get_fd, only OSF/1 has a 2-argument
103    macro(!).  */
104 #   if HAVE_ACL_FREE_TEXT /* OSF/1 */
105 ACL_INTERNAL_INLINE acl_t
106 rpl_acl_get_fd (int fd)
107 {
108   return acl_get_fd (fd, ACL_TYPE_ACCESS);
109 }
110 #    undef acl_get_fd
111 #    define acl_get_fd rpl_acl_get_fd
112 #   endif
113 #  else
114 #   define HAVE_ACL_GET_FD false
115 #   undef acl_get_fd
116 #   define acl_get_fd(fd) (NULL)
117 #  endif
118
119 /* POSIX 1003.1e (draft 17) */
120 #  ifdef HAVE_ACL_SET_FD
121 /* Most platforms have a 2-argument acl_set_fd, only OSF/1 has a 3-argument
122    macro(!).  */
123 #   if HAVE_ACL_FREE_TEXT /* OSF/1 */
124 ACL_INTERNAL_INLINE int
125 rpl_acl_set_fd (int fd, acl_t acl)
126 {
127   return acl_set_fd (fd, ACL_TYPE_ACCESS, acl);
128 }
129 #    undef acl_set_fd
130 #    define acl_set_fd rpl_acl_set_fd
131 #   endif
132 #  else
133 #   define HAVE_ACL_SET_FD false
134 #   undef acl_set_fd
135 #   define acl_set_fd(fd, acl) (-1)
136 #  endif
137
138 /* POSIX 1003.1e (draft 13) */
139 #  if ! HAVE_ACL_FREE_TEXT
140 #   define acl_free_text(buf) acl_free (buf)
141 #  endif
142
143 /* Linux-specific */
144 #  ifndef HAVE_ACL_EXTENDED_FILE
145 #   define HAVE_ACL_EXTENDED_FILE false
146 #   define acl_extended_file(name) (-1)
147 #  endif
148
149 /* Linux-specific */
150 #  ifndef HAVE_ACL_FROM_MODE
151 #   define HAVE_ACL_FROM_MODE false
152 #   define acl_from_mode(mode) (NULL)
153 #  endif
154
155 /* Set to 1 if a file's mode is implicit by the ACL.
156    Set to 0 if a file's mode is stored independently from the ACL.  */
157 #  if (HAVE_ACL_COPY_EXT_NATIVE && HAVE_ACL_CREATE_ENTRY_NP) || defined __sgi /* Mac OS X, IRIX */
158 #   define MODE_INSIDE_ACL 0
159 #  else
160 #   define MODE_INSIDE_ACL 1
161 #  endif
162
163 /* Return the number of entries in ACL.
164    Return -1 and set errno upon failure to determine it.  */
165 /* Define a replacement for acl_entries if needed. (Only Linux has it.)  */
166 #  if !HAVE_ACL_ENTRIES
167 #   define acl_entries rpl_acl_entries
168 extern int acl_entries (acl_t);
169 #  endif
170
171 #  if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
172 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
173    Return 1 if the given ACL is non-trivial.
174    Return 0 if it is trivial.  */
175 extern int acl_extended_nontrivial (acl_t);
176 #  else
177 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
178    Return 1 if the given ACL is non-trivial.
179    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
180    Return -1 and set errno upon failure to determine it.  */
181 extern int acl_access_nontrivial (acl_t);
182 #  endif
183
184 # elif HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
185
186 /* Set to 1 if a file's mode is implicit by the ACL.
187    Set to 0 if a file's mode is stored independently from the ACL.  */
188 #  if defined __CYGWIN__ /* Cygwin */
189 #   define MODE_INSIDE_ACL 0
190 #  else /* Solaris */
191 #   define MODE_INSIDE_ACL 1
192 #  endif
193
194 /* Return 1 if the given ACL is non-trivial.
195    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
196 extern int acl_nontrivial (int count, aclent_t *entries);
197
198 #  ifdef ACE_GETACL /* Solaris 10 */
199
200 /* Test an ACL retrieved with ACE_GETACL.
201    Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
202    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
203 extern int acl_ace_nontrivial (int count, ace_t *entries);
204
205 /* Definitions for when the built executable is executed on Solaris 10
206    (newer version) or Solaris 11.  */
207 /* For a_type.  */
208 #   define OLD_ALLOW 0
209 #   define OLD_DENY  1
210 #   define NEW_ACE_ACCESS_ALLOWED_ACE_TYPE 0 /* replaces ALLOW */
211 #   define NEW_ACE_ACCESS_DENIED_ACE_TYPE  1 /* replaces DENY */
212 /* For a_flags.  */
213 #   define OLD_ACE_OWNER            0x0100
214 #   define OLD_ACE_GROUP            0x0200
215 #   define OLD_ACE_OTHER            0x0400
216 #   define NEW_ACE_OWNER            0x1000
217 #   define NEW_ACE_GROUP            0x2000
218 #   define NEW_ACE_IDENTIFIER_GROUP 0x0040
219 #   define NEW_ACE_EVERYONE         0x4000
220 /* For a_access_mask.  */
221 #   define NEW_ACE_READ_DATA         0x001 /* corresponds to 'r' */
222 #   define NEW_ACE_WRITE_DATA        0x002 /* corresponds to 'w' */
223 #   define NEW_ACE_APPEND_DATA       0x004
224 #   define NEW_ACE_READ_NAMED_ATTRS  0x008
225 #   define NEW_ACE_WRITE_NAMED_ATTRS 0x010
226 #   define NEW_ACE_EXECUTE           0x020
227 #   define NEW_ACE_DELETE_CHILD      0x040
228 #   define NEW_ACE_READ_ATTRIBUTES   0x080
229 #   define NEW_ACE_WRITE_ATTRIBUTES  0x100
230 #   define NEW_ACE_DELETE          0x10000
231 #   define NEW_ACE_READ_ACL        0x20000
232 #   define NEW_ACE_WRITE_ACL       0x40000
233 #   define NEW_ACE_WRITE_OWNER     0x80000
234 #   define NEW_ACE_SYNCHRONIZE    0x100000
235
236 #  endif
237
238 # elif HAVE_GETACL /* HP-UX */
239
240 /* Return 1 if the given ACL is non-trivial.
241    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
242 extern int acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb);
243
244 #  if HAVE_ACLV_H /* HP-UX >= 11.11 */
245
246 /* Return 1 if the given ACL is non-trivial.
247    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
248 extern int aclv_nontrivial (int count, struct acl *entries);
249
250 #  endif
251
252 # elif HAVE_ACLX_GET && 0 /* AIX */
253
254 /* TODO */
255
256 # elif HAVE_STATACL /* older AIX */
257
258 /* Return 1 if the given ACL is non-trivial.
259    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
260 extern int acl_nontrivial (struct acl *a);
261
262 # elif HAVE_ACLSORT /* NonStop Kernel */
263
264 /* Return 1 if the given ACL is non-trivial.
265    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.  */
266 extern int acl_nontrivial (int count, struct acl *entries);
267
268 # endif
269
270 #endif
271
272 _GL_INLINE_HEADER_END