1 /* Internal implementation of access control lists. -*- coding: utf-8 -*-
3 Copyright (C) 2002-2003, 2005-2017 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 <http://www.gnu.org/licenses/>.
18 Written by Paul Eggert, Andreas Grünbacher, and Bruno Haible. */
25 /* All systems define the ACL related API in <sys/acl.h>. */
29 #if defined HAVE_FACL && ! defined GETACLCNT && defined ACL_CNT
30 # define GETACLCNT ACL_CNT
33 /* On Linux, additional ACL related API is available in <acl/libacl.h>. */
34 #ifdef HAVE_ACL_LIBACL_H
35 # include <acl/libacl.h>
38 /* On HP-UX >= 11.11, additional ACL API is available in <aclv.h>. */
40 # include <sys/types.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
*);
51 # define MIN(a,b) ((a) < (b) ? (a) : (b))
55 # define SIZE_MAX ((size_t) -1)
59 # define HAVE_FCHMOD false
60 # define fchmod(fd, mode) (-1)
63 #ifndef _GL_INLINE_HEADER_BEGIN
64 #error "Please include config.h first."
66 _GL_INLINE_HEADER_BEGIN
67 #ifndef ACL_INTERNAL_INLINE
68 # define ACL_INTERNAL_INLINE _GL_INLINE
73 # if HAVE_ACL_GET_FILE
74 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
75 /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
77 # ifndef MIN_ACL_ENTRIES
78 # define MIN_ACL_ENTRIES 4
81 /* POSIX 1003.1e (draft 17) */
82 # ifdef HAVE_ACL_GET_FD
83 /* Most platforms have a 1-argument acl_get_fd, only OSF/1 has a 2-argument
85 # if HAVE_ACL_FREE_TEXT /* OSF/1 */
86 ACL_INTERNAL_INLINE acl_t
87 rpl_acl_get_fd (int fd
)
89 return acl_get_fd (fd
, ACL_TYPE_ACCESS
);
92 # define acl_get_fd rpl_acl_get_fd
95 # define HAVE_ACL_GET_FD false
97 # define acl_get_fd(fd) (NULL)
100 /* POSIX 1003.1e (draft 17) */
101 # ifdef HAVE_ACL_SET_FD
102 /* Most platforms have a 2-argument acl_set_fd, only OSF/1 has a 3-argument
104 # if HAVE_ACL_FREE_TEXT /* OSF/1 */
105 ACL_INTERNAL_INLINE
int
106 rpl_acl_set_fd (int fd
, acl_t acl
)
108 return acl_set_fd (fd
, ACL_TYPE_ACCESS
, acl
);
111 # define acl_set_fd rpl_acl_set_fd
114 # define HAVE_ACL_SET_FD false
116 # define acl_set_fd(fd, acl) (-1)
119 /* POSIX 1003.1e (draft 13) */
120 # if ! HAVE_ACL_FREE_TEXT
121 # define acl_free_text(buf) acl_free (buf)
125 # ifndef HAVE_ACL_EXTENDED_FILE
126 # define HAVE_ACL_EXTENDED_FILE false
127 # define acl_extended_file(name) (-1)
130 # if ! defined HAVE_ACL_FROM_MODE && ! defined HAVE_ACL_FROM_TEXT
131 # define acl_from_mode (NULL)
134 /* Set to 0 if a file's mode is stored independently from the ACL. */
135 # if (HAVE_ACL_COPY_EXT_NATIVE && HAVE_ACL_CREATE_ENTRY_NP) || defined __sgi /* Mac OS X, IRIX */
136 # define MODE_INSIDE_ACL 0
139 /* Return the number of entries in ACL.
140 Return -1 and set errno upon failure to determine it. */
141 /* Define a replacement for acl_entries if needed. (Only Linux has it.) */
142 # if !HAVE_ACL_ENTRIES
143 # define acl_entries rpl_acl_entries
144 extern int acl_entries (acl_t
);
147 # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
148 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
149 Return 1 if the given ACL is non-trivial.
150 Return 0 if it is trivial. */
151 extern int acl_extended_nontrivial (acl_t
);
153 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
154 Return 1 if the given ACL is non-trivial.
155 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
156 Return -1 and set errno upon failure to determine it. */
157 extern int acl_access_nontrivial (acl_t
);
159 /* ACL is an ACL, from a file, stored as type ACL_TYPE_DEFAULT.
160 Return 1 if the given ACL is non-trivial.
161 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
162 Return -1 and set errno upon failure to determine it. */
163 extern int acl_default_nontrivial (acl_t
);
166 # elif HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
168 /* Set to 0 if a file's mode is stored independently from the ACL. */
169 # if defined __CYGWIN__ /* Cygwin */
170 # define MODE_INSIDE_ACL 0
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
) _GL_ATTRIBUTE_PURE
;
177 # ifdef ACE_GETACL /* Solaris 10 */
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
) _GL_ATTRIBUTE_PURE
;
184 /* Definitions for when the built executable is executed on Solaris 10
185 (newer version) or Solaris 11. */
189 # define NEW_ACE_ACCESS_ALLOWED_ACE_TYPE 0 /* replaces ALLOW */
190 # define NEW_ACE_ACCESS_DENIED_ACE_TYPE 1 /* replaces DENY */
192 # define OLD_ACE_OWNER 0x0100
193 # define OLD_ACE_GROUP 0x0200
194 # define OLD_ACE_OTHER 0x0400
195 # define NEW_ACE_OWNER 0x1000
196 # define NEW_ACE_GROUP 0x2000
197 # define NEW_ACE_IDENTIFIER_GROUP 0x0040
198 # define NEW_ACE_EVERYONE 0x4000
199 /* For a_access_mask. */
200 # define NEW_ACE_READ_DATA 0x001 /* corresponds to 'r' */
201 # define NEW_ACE_WRITE_DATA 0x002 /* corresponds to 'w' */
202 # define NEW_ACE_APPEND_DATA 0x004
203 # define NEW_ACE_READ_NAMED_ATTRS 0x008
204 # define NEW_ACE_WRITE_NAMED_ATTRS 0x010
205 # define NEW_ACE_EXECUTE 0x020
206 # define NEW_ACE_DELETE_CHILD 0x040
207 # define NEW_ACE_READ_ATTRIBUTES 0x080
208 # define NEW_ACE_WRITE_ATTRIBUTES 0x100
209 # define NEW_ACE_DELETE 0x10000
210 # define NEW_ACE_READ_ACL 0x20000
211 # define NEW_ACE_WRITE_ACL 0x40000
212 # define NEW_ACE_WRITE_OWNER 0x80000
213 # define NEW_ACE_SYNCHRONIZE 0x100000
217 # elif HAVE_GETACL /* HP-UX */
219 /* Return 1 if the given ACL is non-trivial.
220 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
221 extern int acl_nontrivial (int count
, struct acl_entry
*entries
);
223 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
225 /* Return 1 if the given ACL is non-trivial.
226 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
227 extern int aclv_nontrivial (int count
, struct acl
*entries
);
231 # elif HAVE_ACLX_GET && 0 /* AIX */
235 # elif HAVE_STATACL /* older AIX */
237 /* Return 1 if the given ACL is non-trivial.
238 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
239 extern int acl_nontrivial (struct acl
*a
);
241 # elif HAVE_ACLSORT /* NonStop Kernel */
243 /* Return 1 if the given ACL is non-trivial.
244 Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
245 extern int acl_nontrivial (int count
, struct acl
*entries
);
249 /* Set to 1 if a file's mode is implicit by the ACL. */
250 # ifndef MODE_INSIDE_ACL
251 # define MODE_INSIDE_ACL 1
256 struct permission_context
{
259 # if HAVE_ACL_GET_FILE /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
261 # if !HAVE_ACL_TYPE_EXTENDED
264 bool acls_not_supported
;
266 # elif defined GETACL /* Solaris, Cygwin */
274 # elif HAVE_GETACL /* HP-UX */
275 struct acl_entry entries
[NACLENTRIES
];
278 struct acl aclv_entries
[NACLVENTRIES
];
282 # elif HAVE_STATACL /* older AIX */
283 union { struct acl a
; char room
[4096]; } u
;
286 # elif HAVE_ACLSORT /* NonStop Kernel */
287 struct acl entries
[NACLENTRIES
];
294 int get_permissions (const char *, int, mode_t
, struct permission_context
*);
295 int set_permissions (struct permission_context
*, const char *, int);
296 void free_permission_context (struct permission_context
*)
297 #if ! (defined USE_ACL && (HAVE_ACL_GET_FILE || defined GETACL))
302 _GL_INLINE_HEADER_END