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