1 /* set-mode-acl.c - set access control list equivalent to a mode
3 Copyright (C) 2002-2003, 2005-2008 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 and Andreas Gruenbacher, and Bruno Haible. */
24 #include "acl-internal.h"
26 /* If DESC is a valid file descriptor use fchmod to change the
27 file's mode to MODE on systems that have fchown. On systems
28 that don't have fchown and if DESC is invalid, use chown on
30 Return 0 if successful. Return -1 and set errno upon failure. */
33 chmod_or_fchmod (const char *name, int desc, mode_t mode)
35 if (HAVE_FCHMOD && desc != -1)
36 return fchmod (desc, mode);
38 return chmod (name, mode);
41 /* Set the access control lists of a file. If DESC is a valid file
42 descriptor, use file descriptor operations where available, else use
43 filename based operations on NAME. If access control lists are not
44 available, fchmod the target file to MODE. Also sets the
45 non-permission bits of the destination file (S_ISUID, S_ISGID, S_ISVTX)
46 to those from MODE if any are set.
47 Return 0 if successful. Return -1 and set errno upon failure. */
50 qset_acl (char const *name, int desc, mode_t mode)
53 # if HAVE_ACL_GET_FILE
54 /* POSIX 1003.1e draft 17 (abandoned) specific version. */
55 /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
57 /* Linux, FreeBSD, IRIX, Tru64 */
59 /* We must also have acl_from_text and acl_delete_def_file.
60 (acl_delete_def_file could be emulated with acl_init followed
61 by acl_set_file, but acl_set_file with an empty acl is
64 # ifndef HAVE_ACL_FROM_TEXT
65 # error Must have acl_from_text (see POSIX 1003.1e draft 17).
67 # ifndef HAVE_ACL_DELETE_DEF_FILE
68 # error Must have acl_delete_def_file (see POSIX 1003.1e draft 17).
74 if (HAVE_ACL_FROM_MODE) /* Linux */
76 acl = acl_from_mode (mode);
80 else /* FreeBSD, IRIX, Tru64 */
82 /* If we were to create the ACL using the functions acl_init(),
83 acl_create_entry(), acl_set_tag_type(), acl_set_qualifier(),
84 acl_get_permset(), acl_clear_perm[s](), acl_add_perm(), we
85 would need to create a qualifier. I don't know how to do this.
86 So create it using acl_from_text(). */
88 # if HAVE_ACL_FREE_TEXT /* Tru64 */
89 char acl_text[] = "u::---,g::---,o::---,";
90 # else /* FreeBSD, IRIX */
91 char acl_text[] = "u::---,g::---,o::---";
94 if (mode & S_IRUSR) acl_text[ 3] = 'r';
95 if (mode & S_IWUSR) acl_text[ 4] = 'w';
96 if (mode & S_IXUSR) acl_text[ 5] = 'x';
97 if (mode & S_IRGRP) acl_text[10] = 'r';
98 if (mode & S_IWGRP) acl_text[11] = 'w';
99 if (mode & S_IXGRP) acl_text[12] = 'x';
100 if (mode & S_IROTH) acl_text[17] = 'r';
101 if (mode & S_IWOTH) acl_text[18] = 'w';
102 if (mode & S_IXOTH) acl_text[19] = 'x';
104 acl = acl_from_text (acl_text);
108 if (HAVE_ACL_SET_FD && desc != -1)
109 ret = acl_set_fd (desc, acl);
111 ret = acl_set_file (name, ACL_TYPE_ACCESS, acl);
114 int saved_errno = errno;
117 if (ACL_NOT_WELL_SUPPORTED (errno))
118 return chmod_or_fchmod (name, desc, mode);
128 if (S_ISDIR (mode) && acl_delete_def_file (name))
131 if (mode & (S_ISUID | S_ISGID | S_ISVTX))
133 /* We did not call chmod so far, so the special bits have not yet
135 return chmod_or_fchmod (name, desc, mode);
139 # else /* !MODE_INSIDE_ACL */
142 # if !HAVE_ACL_TYPE_EXTENDED
143 # error Must have ACL_TYPE_EXTENDED
146 /* On MacOS X, acl_get_file (name, ACL_TYPE_ACCESS)
147 and acl_get_file (name, ACL_TYPE_DEFAULT)
148 always return NULL / EINVAL. You have to use
149 acl_get_file (name, ACL_TYPE_EXTENDED)
150 or acl_get_fd (open (name, ...))
153 acl_set_file (name, ACL_TYPE_ACCESS, acl)
154 and acl_set_file (name, ACL_TYPE_DEFAULT, acl)
155 have the same effect as
156 acl_set_file (name, ACL_TYPE_EXTENDED, acl):
157 Each of these calls sets the file's ACL. */
162 /* Remove the ACL if the file has ACLs. */
163 if (HAVE_ACL_GET_FD && desc != -1)
164 acl = acl_get_fd (desc);
166 acl = acl_get_file (name, ACL_TYPE_EXTENDED);
174 if (HAVE_ACL_SET_FD && desc != -1)
175 ret = acl_set_fd (desc, acl);
177 ret = acl_set_file (name, ACL_TYPE_EXTENDED, acl);
180 int saved_errno = errno;
184 if (ACL_NOT_WELL_SUPPORTED (saved_errno))
185 return chmod_or_fchmod (name, desc, mode);
196 /* Since !MODE_INSIDE_ACL, we have to call chmod explicitly. */
197 return chmod_or_fchmod (name, desc, mode);
200 # elif HAVE_ACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
202 # if defined ACL_NO_TRIVIAL
203 /* Solaris 10 (newer version), which has additional API declared in
204 <sys/acl.h> (acl_t) and implemented in libsec (acl_set, acl_trivial,
205 acl_fromtext, ...). */
208 char acl_text[] = "user::---,group::---,mask:---,other:---";
212 if (mode & S_IRUSR) acl_text[ 6] = 'r';
213 if (mode & S_IWUSR) acl_text[ 7] = 'w';
214 if (mode & S_IXUSR) acl_text[ 8] = 'x';
215 if (mode & S_IRGRP) acl_text[17] = acl_text[26] = 'r';
216 if (mode & S_IWGRP) acl_text[18] = acl_text[27] = 'w';
217 if (mode & S_IXGRP) acl_text[19] = acl_text[28] = 'x';
218 if (mode & S_IROTH) acl_text[36] = 'r';
219 if (mode & S_IWOTH) acl_text[37] = 'w';
220 if (mode & S_IXOTH) acl_text[38] = 'x';
222 if (acl_fromtext (acl_text, &aclp) != 0)
228 ret = (desc < 0 ? acl_set (name, aclp) : facl_set (desc, aclp));
233 if (saved_errno == ENOSYS)
234 return chmod_or_fchmod (name, desc, mode);
239 if (mode & (S_ISUID | S_ISGID | S_ISVTX))
241 /* We did not call chmod so far, so the special bits have not yet
243 return chmod_or_fchmod (name, desc, mode);
247 # else /* Solaris, Cygwin, general case */
250 /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
251 file systems (whereas the other ones are used in UFS file systems). */
256 entries[0].a_type = ALLOW;
257 entries[0].a_flags = ACE_OWNER;
258 entries[0].a_who = 0; /* irrelevant */
259 entries[0].a_access_mask = (mode >> 6) & 7;
260 entries[1].a_type = ALLOW;
261 entries[1].a_flags = ACE_GROUP;
262 entries[1].a_who = 0; /* irrelevant */
263 entries[1].a_access_mask = (mode >> 3) & 7;
264 entries[2].a_type = ALLOW;
265 entries[2].a_flags = ACE_OTHER;
266 entries[2].a_who = 0;
267 entries[2].a_access_mask = mode & 7;
270 ret = facl (desc, ACE_SETACL, sizeof (entries) / sizeof (aclent_t), entries);
272 ret = acl (name, ACE_SETACL, sizeof (entries) / sizeof (aclent_t), entries);
273 if (ret < 0 && errno != EINVAL && errno != ENOTSUP)
276 return chmod_or_fchmod (name, desc, mode);
286 entries[0].a_type = USER_OBJ;
287 entries[0].a_id = 0; /* irrelevant */
288 entries[0].a_perm = (mode >> 6) & 7;
289 entries[1].a_type = GROUP_OBJ;
290 entries[1].a_id = 0; /* irrelevant */
291 entries[1].a_perm = (mode >> 3) & 7;
292 entries[2].a_type = OTHER_OBJ;
294 entries[2].a_perm = mode & 7;
297 ret = facl (desc, SETACL, sizeof (entries) / sizeof (aclent_t), entries);
299 ret = acl (name, SETACL, sizeof (entries) / sizeof (aclent_t), entries);
303 return chmod_or_fchmod (name, desc, mode);
308 if (!MODE_INSIDE_ACL || (mode & (S_ISUID | S_ISGID | S_ISVTX)))
310 /* We did not call chmod so far, so the special bits have not yet
312 return chmod_or_fchmod (name, desc, mode);
318 # elif HAVE_GETACL /* HP-UX */
321 struct acl_entry entries[3];
325 ret = fstat (desc, &statbuf);
327 ret = stat (name, &statbuf);
331 entries[0].uid = statbuf.st_uid;
332 entries[0].gid = ACL_NSGROUP;
333 entries[0].mode = (mode >> 6) & 7;
334 entries[1].uid = ACL_NSUSER;
335 entries[1].gid = statbuf.st_gid;
336 entries[1].mode = (mode >> 3) & 7;
337 entries[2].uid = ACL_NSUSER;
338 entries[2].gid = ACL_NSGROUP;
339 entries[2].mode = mode & 7;
342 ret = fsetacl (desc, sizeof (entries) / sizeof (struct acl_entry), entries);
344 ret = setacl (name, sizeof (entries) / sizeof (struct acl_entry), entries);
347 if (errno == ENOSYS || errno == EOPNOTSUPP)
348 return chmod_or_fchmod (name, desc, mode);
352 if (mode & (S_ISUID | S_ISGID | S_ISVTX))
354 /* We did not call chmod so far, so the special bits have not yet
356 return chmod_or_fchmod (name, desc, mode);
360 # elif HAVE_ACLX_GET && 0 /* AIX */
362 /* TODO: use aclx_fput or aclx_put, respectively */
364 # elif HAVE_STATACL /* older AIX */
366 union { struct acl a; char room[128]; } u;
369 u.a.acl_len = (char *) &u.a.acl_ext[0] - (char *) &u.a; /* no entries */
370 u.a.acl_mode = mode & ~(S_IXACL | 0777);
371 u.a.u_access = (mode >> 6) & 7;
372 u.a.g_access = (mode >> 3) & 7;
373 u.a.o_access = mode & 7;
376 ret = fchacl (desc, &u.a, u.a.acl_len);
378 ret = chacl (name, &u.a, u.a.acl_len);
380 if (ret < 0 && errno == ENOSYS)
381 return chmod_or_fchmod (name, desc, mode);
385 # else /* Unknown flavor of ACLs */
386 return chmod_or_fchmod (name, desc, mode);
389 return chmod_or_fchmod (name, desc, mode);
393 /* As with qset_acl, but also output a diagnostic on failure. */
396 set_acl (char const *name, int desc, mode_t mode)
398 int r = qset_acl (name, desc, mode);
400 error (0, errno, _("setting permissions for %s"), quote (name));