timevar: import from Bison.
[gnulib.git] / lib / acl-internal.h
blob2da7c5a036622bb85c5617117049545775f6c985
1 /* Internal implementation of access control lists. -*- coding: utf-8 -*-
3 Copyright (C) 2002-2003, 2005-2018 Free Software Foundation, Inc.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
18 Written by Paul Eggert, Andreas Grünbacher, and Bruno Haible. */
20 #include "acl.h"
22 #include <stdbool.h>
23 #include <stdlib.h>
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
33 /* On Linux and Cygwin >= 2.5, additional ACL related API is available in
34 <acl/libacl.h>. */
35 #ifdef HAVE_ACL_LIBACL_H
36 # include <acl/libacl.h>
37 #endif
39 /* On HP-UX >= 11.11, additional ACL API is available in <aclv.h>. */
40 #if HAVE_ACLV_H
41 # include <sys/types.h>
42 # include <aclv.h>
43 /* HP-UX 11.11 lacks these declarations. */
44 extern int acl (char *, int, int, struct acl *);
45 extern int aclsort (int, int, struct acl *);
46 #endif
48 #include <errno.h>
50 #include <limits.h>
51 #ifndef MIN
52 # define MIN(a,b) ((a) < (b) ? (a) : (b))
53 #endif
55 #ifndef SIZE_MAX
56 # define SIZE_MAX ((size_t) -1)
57 #endif
59 #ifndef HAVE_FCHMOD
60 # define HAVE_FCHMOD false
61 # define fchmod(fd, mode) (-1)
62 #endif
64 #ifndef _GL_INLINE_HEADER_BEGIN
65 #error "Please include config.h first."
66 #endif
67 _GL_INLINE_HEADER_BEGIN
68 #ifndef ACL_INTERNAL_INLINE
69 # define ACL_INTERNAL_INLINE _GL_INLINE
70 #endif
72 #if USE_ACL
74 # if HAVE_ACL_GET_FILE
75 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
76 /* Linux, FreeBSD, Mac OS X, IRIX, Tru64, Cygwin >= 2.5 */
78 # ifndef MIN_ACL_ENTRIES
79 # define MIN_ACL_ENTRIES 4
80 # endif
82 /* POSIX 1003.1e (draft 17) */
83 # ifdef HAVE_ACL_GET_FD
84 /* Most platforms have a 1-argument acl_get_fd, only OSF/1 has a 2-argument
85 macro(!). */
86 # if HAVE_ACL_FREE_TEXT /* OSF/1 */
87 ACL_INTERNAL_INLINE acl_t
88 rpl_acl_get_fd (int fd)
90 return acl_get_fd (fd, ACL_TYPE_ACCESS);
92 # undef acl_get_fd
93 # define acl_get_fd rpl_acl_get_fd
94 # endif
95 # else
96 # define HAVE_ACL_GET_FD false
97 # undef acl_get_fd
98 # define acl_get_fd(fd) (NULL)
99 # endif
101 /* POSIX 1003.1e (draft 17) */
102 # ifdef HAVE_ACL_SET_FD
103 /* Most platforms have a 2-argument acl_set_fd, only OSF/1 has a 3-argument
104 macro(!). */
105 # if HAVE_ACL_FREE_TEXT /* OSF/1 */
106 ACL_INTERNAL_INLINE int
107 rpl_acl_set_fd (int fd, acl_t acl)
109 return acl_set_fd (fd, ACL_TYPE_ACCESS, acl);
111 # undef acl_set_fd
112 # define acl_set_fd rpl_acl_set_fd
113 # endif
114 # else
115 # define HAVE_ACL_SET_FD false
116 # undef acl_set_fd
117 # define acl_set_fd(fd, acl) (-1)
118 # endif
120 /* POSIX 1003.1e (draft 13) */
121 # if ! HAVE_ACL_FREE_TEXT
122 # define acl_free_text(buf) acl_free (buf)
123 # endif
125 /* Linux-specific */
126 /* Cygwin >= 2.5 implements this function, but it returns 1 for all
127 directories, thus is unusable. */
128 # if !defined HAVE_ACL_EXTENDED_FILE || defined __CYGWIN__
129 # undef HAVE_ACL_EXTENDED_FILE
130 # define HAVE_ACL_EXTENDED_FILE false
131 # define acl_extended_file(name) (-1)
132 # endif
134 # if ! defined HAVE_ACL_FROM_MODE && ! defined HAVE_ACL_FROM_TEXT
135 # define acl_from_mode (NULL)
136 # endif
138 /* Set to 0 if a file's mode is stored independently from the ACL. */
139 # if (HAVE_ACL_COPY_EXT_NATIVE && HAVE_ACL_CREATE_ENTRY_NP) || defined __sgi /* Mac OS X, IRIX */
140 # define MODE_INSIDE_ACL 0
141 # endif
143 /* Return the number of entries in ACL.
144 Return -1 and set errno upon failure to determine it. */
145 /* Define a replacement for acl_entries if needed. (Only Linux has it.) */
146 # if !HAVE_ACL_ENTRIES
147 # define acl_entries rpl_acl_entries
148 extern int acl_entries (acl_t);
149 # endif
151 # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
152 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
153 Return 1 if the given ACL is non-trivial.
154 Return 0 if it is trivial. */
155 extern int acl_extended_nontrivial (acl_t);
156 # else
157 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
158 Return 1 if the given ACL is non-trivial.
159 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
160 Return -1 and set errno upon failure to determine it. */
161 extern int acl_access_nontrivial (acl_t);
163 /* ACL is an ACL, from a file, stored as type ACL_TYPE_DEFAULT.
164 Return 1 if the given ACL is non-trivial.
165 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
166 Return -1 and set errno upon failure to determine it. */
167 extern int acl_default_nontrivial (acl_t);
168 # endif
170 # elif HAVE_FACL && defined GETACL /* Solaris, Cygwin < 2.5, not HP-UX */
172 /* Set to 0 if a file's mode is stored independently from the ACL. */
173 # if defined __CYGWIN__ /* Cygwin */
174 # define MODE_INSIDE_ACL 0
175 # endif
177 /* Return 1 if the given ACL is non-trivial.
178 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
179 extern int acl_nontrivial (int count, aclent_t *entries) _GL_ATTRIBUTE_PURE;
181 # ifdef ACE_GETACL /* Solaris 10 */
183 /* Test an ACL retrieved with ACE_GETACL.
184 Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
185 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
186 extern int acl_ace_nontrivial (int count, ace_t *entries) _GL_ATTRIBUTE_PURE;
188 /* Definitions for when the built executable is executed on Solaris 10
189 (newer version) or Solaris 11. */
190 /* For a_type. */
191 # define OLD_ALLOW 0
192 # define OLD_DENY 1
193 # define NEW_ACE_ACCESS_ALLOWED_ACE_TYPE 0 /* replaces ALLOW */
194 # define NEW_ACE_ACCESS_DENIED_ACE_TYPE 1 /* replaces DENY */
195 /* For a_flags. */
196 # define OLD_ACE_OWNER 0x0100
197 # define OLD_ACE_GROUP 0x0200
198 # define OLD_ACE_OTHER 0x0400
199 # define NEW_ACE_OWNER 0x1000
200 # define NEW_ACE_GROUP 0x2000
201 # define NEW_ACE_IDENTIFIER_GROUP 0x0040
202 # define NEW_ACE_EVERYONE 0x4000
203 /* For a_access_mask. */
204 # define NEW_ACE_READ_DATA 0x001 /* corresponds to 'r' */
205 # define NEW_ACE_WRITE_DATA 0x002 /* corresponds to 'w' */
206 # define NEW_ACE_APPEND_DATA 0x004
207 # define NEW_ACE_READ_NAMED_ATTRS 0x008
208 # define NEW_ACE_WRITE_NAMED_ATTRS 0x010
209 # define NEW_ACE_EXECUTE 0x020
210 # define NEW_ACE_DELETE_CHILD 0x040
211 # define NEW_ACE_READ_ATTRIBUTES 0x080
212 # define NEW_ACE_WRITE_ATTRIBUTES 0x100
213 # define NEW_ACE_DELETE 0x10000
214 # define NEW_ACE_READ_ACL 0x20000
215 # define NEW_ACE_WRITE_ACL 0x40000
216 # define NEW_ACE_WRITE_OWNER 0x80000
217 # define NEW_ACE_SYNCHRONIZE 0x100000
219 # endif
221 # elif HAVE_GETACL /* HP-UX */
223 /* Return 1 if the given ACL is non-trivial.
224 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
225 extern int acl_nontrivial (int count, struct acl_entry *entries);
227 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
229 /* Return 1 if the given ACL is non-trivial.
230 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
231 extern int aclv_nontrivial (int count, struct acl *entries);
233 # endif
235 # elif HAVE_ACLX_GET && 0 /* AIX */
237 /* TODO */
239 # elif HAVE_STATACL /* older AIX */
241 /* Return 1 if the given ACL is non-trivial.
242 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
243 extern int acl_nontrivial (struct acl *a);
245 # elif HAVE_ACLSORT /* NonStop Kernel */
247 /* Return 1 if the given ACL is non-trivial.
248 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
249 extern int acl_nontrivial (int count, struct acl *entries);
251 # endif
253 /* Set to 1 if a file's mode is implicit by the ACL. */
254 # ifndef MODE_INSIDE_ACL
255 # define MODE_INSIDE_ACL 1
256 # endif
258 #endif
260 struct permission_context {
261 mode_t mode;
262 #if USE_ACL
263 # if HAVE_ACL_GET_FILE /* Linux, FreeBSD, Mac OS X, IRIX, Tru64, Cygwin >= 2.5 */
264 acl_t acl;
265 # if !HAVE_ACL_TYPE_EXTENDED
266 acl_t default_acl;
267 # endif
268 bool acls_not_supported;
270 # elif defined GETACL /* Solaris, Cygwin < 2.5 */
271 int count;
272 aclent_t *entries;
273 # ifdef ACE_GETACL
274 int ace_count;
275 ace_t *ace_entries;
276 # endif
278 # elif HAVE_GETACL /* HP-UX */
279 struct acl_entry entries[NACLENTRIES];
280 int count;
281 # if HAVE_ACLV_H
282 struct acl aclv_entries[NACLVENTRIES];
283 int aclv_count;
284 # endif
286 # elif HAVE_STATACL /* older AIX */
287 union { struct acl a; char room[4096]; } u;
288 bool have_u;
290 # elif HAVE_ACLSORT /* NonStop Kernel */
291 struct acl entries[NACLENTRIES];
292 int count;
294 # endif
295 #endif
298 int get_permissions (const char *, int, mode_t, struct permission_context *);
299 int set_permissions (struct permission_context *, const char *, int);
300 void free_permission_context (struct permission_context *);
302 _GL_INLINE_HEADER_END