1 /* acl.c - access control lists
3 Copyright (C) 2002, 2003, 2005, 2006, 2007 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. */
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
32 chmod_or_fchmod (const char *name, int desc, mode_t mode)
34 if (HAVE_FCHMOD && desc != -1)
35 return fchmod (desc, mode);
37 return chmod (name, mode);
40 /* Copy access control lists from one file to another. If SOURCE_DESC is
41 a valid file descriptor, use file descriptor operations, else use
42 filename based operations on SRC_NAME. Likewise for DEST_DESC and
44 If access control lists are not available, fchmod the target file to
45 MODE. Also sets the non-permission bits of the destination file
46 (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
47 Return 0 if successful, otherwise output a diagnostic and return -1. */
50 copy_acl (const char *src_name, int source_desc, const char *dst_name,
51 int dest_desc, mode_t mode)
55 #if USE_ACL && HAVE_ACL_GET_FILE && HAVE_ACL_SET_FILE && HAVE_ACL_FREE
56 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
59 if (HAVE_ACL_GET_FD && source_desc != -1)
60 acl = acl_get_fd (source_desc);
62 acl = acl_get_file (src_name, ACL_TYPE_ACCESS);
65 if (ACL_NOT_WELL_SUPPORTED (errno))
66 return set_acl (dst_name, dest_desc, mode);
69 error (0, errno, "%s", quote (src_name));
74 if (HAVE_ACL_SET_FD && dest_desc != -1)
75 ret = acl_set_fd (dest_desc, acl);
77 ret = acl_set_file (dst_name, ACL_TYPE_ACCESS, acl);
80 int saved_errno = errno;
82 if (ACL_NOT_WELL_SUPPORTED (errno))
84 int n = acl_entries (acl);
87 /* On most hosts an ACL is trivial if n == 3, and it cannot be
88 less than 3. On IRIX 6.5 it is also trivial if n == -1.
89 For simplicity and safety, assume the ACL is trivial if n <= 3.
90 Also see file-has-acl.c for some of the other possibilities;
91 it's not clear whether that complexity is needed here. */
94 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
100 chmod_or_fchmod (dst_name, dest_desc, mode);
105 chmod_or_fchmod (dst_name, dest_desc, mode);
107 error (0, saved_errno, _("preserving permissions for %s"),
114 if (mode & (S_ISUID | S_ISGID | S_ISVTX))
116 /* We did not call chmod so far, so the special bits have not yet
119 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
121 error (0, errno, _("preserving permissions for %s"),
129 acl = acl_get_file (src_name, ACL_TYPE_DEFAULT);
132 error (0, errno, "%s", quote (src_name));
136 if (acl_set_file (dst_name, ACL_TYPE_DEFAULT, acl))
138 error (0, errno, _("preserving permissions for %s"),
148 ret = chmod_or_fchmod (dst_name, dest_desc, mode);
150 error (0, errno, _("preserving permissions for %s"), quote (dst_name));
155 /* Set the access control lists of a file. If DESC is a valid file
156 descriptor, use file descriptor operations where available, else use
157 filename based operations on NAME. If access control lists are not
158 available, fchmod the target file to MODE. Also sets the
159 non-permission bits of the destination file (S_ISUID, S_ISGID, S_ISVTX)
160 to those from MODE if any are set. System call return value
164 qset_acl (char const *name, int desc, mode_t mode)
166 #if USE_ACL && HAVE_ACL_SET_FILE && HAVE_ACL_FREE
167 /* POSIX 1003.1e draft 17 (abandoned) specific version. */
169 /* We must also have have_acl_from_text and acl_delete_def_file.
170 (acl_delete_def_file could be emulated with acl_init followed
171 by acl_set_file, but acl_set_file with an empty acl is
174 # ifndef HAVE_ACL_FROM_TEXT
175 # error Must have acl_from_text (see POSIX 1003.1e draft 17).
177 # ifndef HAVE_ACL_DELETE_DEF_FILE
178 # error Must have acl_delete_def_file (see POSIX 1003.1e draft 17).
184 if (HAVE_ACL_FROM_MODE)
186 acl = acl_from_mode (mode);
192 char acl_text[] = "u::---,g::---,o::---";
194 if (mode & S_IRUSR) acl_text[ 3] = 'r';
195 if (mode & S_IWUSR) acl_text[ 4] = 'w';
196 if (mode & S_IXUSR) acl_text[ 5] = 'x';
197 if (mode & S_IRGRP) acl_text[10] = 'r';
198 if (mode & S_IWGRP) acl_text[11] = 'w';
199 if (mode & S_IXGRP) acl_text[12] = 'x';
200 if (mode & S_IROTH) acl_text[17] = 'r';
201 if (mode & S_IWOTH) acl_text[18] = 'w';
202 if (mode & S_IXOTH) acl_text[19] = 'x';
204 acl = acl_from_text (acl_text);
208 if (HAVE_ACL_SET_FD && desc != -1)
209 ret = acl_set_fd (desc, acl);
211 ret = acl_set_file (name, ACL_TYPE_ACCESS, acl);
214 int saved_errno = errno;
217 if (ACL_NOT_WELL_SUPPORTED (errno))
219 if (chmod_or_fchmod (name, desc, mode) != 0)
230 if (S_ISDIR (mode) && acl_delete_def_file (name))
233 if (mode & (S_ISUID | S_ISGID | S_ISVTX))
235 /* We did not call chmod so far, so the special bits have not yet
238 if (chmod_or_fchmod (name, desc, mode))
243 return chmod_or_fchmod (name, desc, mode);
247 /* As with qset_acl, but also output a diagnostic on failure. */
250 set_acl (char const *name, int desc, mode_t mode)
252 int r = qset_acl (name, desc, mode);
254 error (0, errno, _("setting permissions for %s"), quote (name));