1 /* copy-acl.c - copy access control list from one file to another file
3 Copyright (C) 2002-2003, 2005-2012 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. */
24 #include "acl-internal.h"
27 #define _(msgid) gettext (msgid)
30 /* Copy access control lists from one file to another. If SOURCE_DESC is
31 a valid file descriptor, use file descriptor operations, else use
32 filename based operations on SRC_NAME. Likewise for DEST_DESC and
34 If access control lists are not available, fchmod the target file to
35 MODE. Also sets the non-permission bits of the destination file
36 (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
37 Return 0 if successful.
38 Return -2 and set errno for an error relating to the source file.
39 Return -1 and set errno for an error relating to the destination file. */
42 qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
43 int dest_desc, mode_t mode)
45 #if USE_ACL && HAVE_ACL_GET_FILE
46 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
47 /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
48 # if !HAVE_ACL_TYPE_EXTENDED
49 /* Linux, FreeBSD, IRIX, Tru64 */
54 if (HAVE_ACL_GET_FD && source_desc != -1)
55 acl = acl_get_fd (source_desc);
57 acl = acl_get_file (src_name, ACL_TYPE_ACCESS);
60 if (ACL_NOT_WELL_SUPPORTED (errno))
61 return qset_acl (dst_name, dest_desc, mode);
66 if (HAVE_ACL_SET_FD && dest_desc != -1)
67 ret = acl_set_fd (dest_desc, acl);
69 ret = acl_set_file (dst_name, ACL_TYPE_ACCESS, acl);
72 int saved_errno = errno;
74 if (ACL_NOT_WELL_SUPPORTED (errno) && !acl_access_nontrivial (acl))
77 return chmod_or_fchmod (dst_name, dest_desc, mode);
82 chmod_or_fchmod (dst_name, dest_desc, mode);
90 if (!MODE_INSIDE_ACL || (mode & (S_ISUID | S_ISGID | S_ISVTX)))
92 /* We did not call chmod so far, and either the mode and the ACL are
93 separate or special bits are to be set which don't fit into ACLs. */
95 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
101 acl = acl_get_file (src_name, ACL_TYPE_DEFAULT);
105 if (acl_set_file (dst_name, ACL_TYPE_DEFAULT, acl))
107 int saved_errno = errno;
118 # else /* HAVE_ACL_TYPE_EXTENDED */
121 /* On Mac OS X, acl_get_file (name, ACL_TYPE_ACCESS)
122 and acl_get_file (name, ACL_TYPE_DEFAULT)
123 always return NULL / EINVAL. You have to use
124 acl_get_file (name, ACL_TYPE_EXTENDED)
125 or acl_get_fd (open (name, ...))
128 acl_set_file (name, ACL_TYPE_ACCESS, acl)
129 and acl_set_file (name, ACL_TYPE_DEFAULT, acl)
130 have the same effect as
131 acl_set_file (name, ACL_TYPE_EXTENDED, acl):
132 Each of these calls sets the file's ACL. */
137 if (HAVE_ACL_GET_FD && source_desc != -1)
138 acl = acl_get_fd (source_desc);
140 acl = acl_get_file (src_name, ACL_TYPE_EXTENDED);
143 if (ACL_NOT_WELL_SUPPORTED (errno))
144 return qset_acl (dst_name, dest_desc, mode);
149 if (HAVE_ACL_SET_FD && dest_desc != -1)
150 ret = acl_set_fd (dest_desc, acl);
152 ret = acl_set_file (dst_name, ACL_TYPE_EXTENDED, acl);
155 int saved_errno = errno;
157 if (ACL_NOT_WELL_SUPPORTED (errno) && !acl_extended_nontrivial (acl))
160 return chmod_or_fchmod (dst_name, dest_desc, mode);
165 chmod_or_fchmod (dst_name, dest_desc, mode);
173 /* Since !MODE_INSIDE_ACL, we have to call chmod explicitly. */
174 return chmod_or_fchmod (dst_name, dest_desc, mode);
178 #elif USE_ACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
180 /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
181 of Unixware. The acl() call returns the access and default ACL both
194 /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
195 file systems (whereas the other ones are used in UFS file systems).
197 pathconf (name, _PC_ACL_ENABLED)
198 fpathconf (desc, _PC_ACL_ENABLED)
199 that allows to determine which of the two kinds of ACLs is supported
200 for the given file. But some file systems may implement this call
201 incorrectly, so better not use it.
202 When fetching the source ACL, we simply fetch both ACL types.
203 When setting the destination ACL, we try either ACL types, assuming
204 that the kernel will translate the ACL from one form to the other.
205 (See in <http://docs.sun.com/app/docs/doc/819-2241/6n4huc7ia?l=en&a=view>
206 the description of ENOTSUP.) */
209 ace_count = (source_desc != -1
210 ? facl (source_desc, ACE_GETACLCNT, 0, NULL)
211 : acl (src_name, ACE_GETACLCNT, 0, NULL));
215 if (errno == ENOSYS || errno == EINVAL)
231 ace_entries = (ace_t *) malloc (ace_count * sizeof (ace_t));
232 if (ace_entries == NULL)
238 ret = (source_desc != -1
239 ? facl (source_desc, ACE_GETACL, ace_count, ace_entries)
240 : acl (src_name, ACE_GETACL, ace_count, ace_entries));
244 if (errno == ENOSYS || errno == EINVAL)
253 if (ret == ace_count)
255 /* Huh? The number of ACL entries changed since the last call.
262 count = (source_desc != -1
263 ? facl (source_desc, GETACLCNT, 0, NULL)
264 : acl (src_name, GETACLCNT, 0, NULL));
268 if (errno == ENOSYS || errno == ENOTSUP || errno == EOPNOTSUPP)
284 entries = (aclent_t *) malloc (count * sizeof (aclent_t));
291 if ((source_desc != -1
292 ? facl (source_desc, GETACL, count, entries)
293 : acl (src_name, GETACL, count, entries))
296 /* Huh? The number of ACL entries changed since the last call.
300 /* Is there an ACL of either kind? */
305 return qset_acl (dst_name, dest_desc, mode);
307 did_chmod = 0; /* set to 1 once the mode bits in 0777 have been set */
308 saved_errno = 0; /* the first non-ignorable error code */
310 if (!MODE_INSIDE_ACL)
312 /* On Cygwin, it is necessary to call chmod before acl, because
313 chmod can change the contents of the ACL (in ways that don't
314 change the allowed accesses, but still visible). */
315 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
320 /* If both ace_entries and entries are available, try SETACL before
321 ACE_SETACL, because SETACL cannot fail with ENOTSUP whereas ACE_SETACL
326 ret = (dest_desc != -1
327 ? facl (dest_desc, SETACL, count, entries)
328 : acl (dst_name, SETACL, count, entries));
329 if (ret < 0 && saved_errno == 0)
332 if ((errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
333 && !acl_nontrivial (count, entries))
344 ret = (dest_desc != -1
345 ? facl (dest_desc, ACE_SETACL, ace_count, ace_entries)
346 : acl (dst_name, ACE_SETACL, ace_count, ace_entries));
347 if (ret < 0 && saved_errno == 0)
350 if ((errno == ENOSYS || errno == EINVAL || errno == ENOTSUP)
351 && !acl_ace_nontrivial (ace_count, ace_entries))
359 && did_chmod <= ((mode & (S_ISUID | S_ISGID | S_ISVTX)) ? 1 : 0))
361 /* We did not call chmod so far, and either the mode and the ACL are
362 separate or special bits are to be set which don't fit into ACLs. */
364 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
366 if (saved_errno == 0)
378 #elif USE_ACL && HAVE_GETACL /* HP-UX */
380 struct acl_entry entries[NACLENTRIES];
383 struct acl aclv_entries[NACLVENTRIES];
390 count = (source_desc != -1
391 ? fgetacl (source_desc, NACLENTRIES, entries)
392 : getacl (src_name, NACLENTRIES, entries));
396 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP)
403 if (count > NACLENTRIES)
404 /* If NACLENTRIES cannot be trusted, use dynamic memory allocation. */
409 aclv_count = acl ((char *) src_name, ACL_GET, NACLVENTRIES, aclv_entries);
413 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
418 else if (aclv_count > 0)
420 if (aclv_count > NACLVENTRIES)
421 /* If NACLVENTRIES cannot be trusted, use dynamic memory allocation. */
430 return qset_acl (dst_name, dest_desc, mode);
432 did_chmod = 0; /* set to 1 once the mode bits in 0777 have been set */
433 saved_errno = 0; /* the first non-ignorable error code */
437 ret = (dest_desc != -1
438 ? fsetacl (dest_desc, count, entries)
439 : setacl (dst_name, count, entries));
440 if (ret < 0 && saved_errno == 0)
443 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP)
445 struct stat source_statbuf;
447 if ((source_desc != -1
448 ? fstat (source_desc, &source_statbuf)
449 : stat (src_name, &source_statbuf)) == 0)
451 if (!acl_nontrivial (count, entries, &source_statbuf))
465 ret = acl ((char *) dst_name, ACL_SET, aclv_count, aclv_entries);
466 if (ret < 0 && saved_errno == 0)
469 if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
471 if (!aclv_nontrivial (aclv_count, aclv_entries))
480 if (did_chmod <= ((mode & (S_ISUID | S_ISGID | S_ISVTX)) ? 1 : 0))
482 /* We did not call chmod so far, and special bits are to be set which
483 don't fit into ACLs. */
485 if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
487 if (saved_errno == 0)
499 #elif USE_ACL && HAVE_ACLX_GET && 0 /* AIX */
503 #elif USE_ACL && HAVE_STATACL /* older AIX */
505 union { struct acl a; char room[4096]; } u;
508 if ((source_desc != -1
509 ? fstatacl (source_desc, STX_NORMAL, &u.a, sizeof (u))
510 : statacl (src_name, STX_NORMAL, &u.a, sizeof (u)))
514 ret = (dest_desc != -1
515 ? fchacl (dest_desc, &u.a, u.a.acl_len)
516 : chacl (dst_name, &u.a, u.a.acl_len));
519 int saved_errno = errno;
521 chmod_or_fchmod (dst_name, dest_desc, mode);
526 /* No need to call chmod_or_fchmod at this point, since the mode bits
527 S_ISUID, S_ISGID, S_ISVTX are also stored in the ACL. */
531 #elif USE_ACL && HAVE_ACLSORT /* NonStop Kernel */
533 struct acl entries[NACLENTRIES];
537 count = acl ((char *) src_name, ACL_GET, NACLENTRIES, entries);
548 if (count > NACLENTRIES)
549 /* If NACLENTRIES cannot be trusted, use dynamic memory allocation. */
554 return qset_acl (dst_name, dest_desc, mode);
556 ret = acl ((char *) dst_name, ACL_SET, count, entries);
559 int saved_errno = errno;
563 if (!acl_nontrivial (count, entries))
564 return chmod_or_fchmod (dst_name, dest_desc, mode);
567 chmod_or_fchmod (dst_name, dest_desc, mode);
572 if (mode & (S_ISUID | S_ISGID | S_ISVTX))
574 /* We did not call chmod so far, and either the mode and the ACL are
575 separate or special bits are to be set which don't fit into ACLs. */
577 return chmod_or_fchmod (dst_name, dest_desc, mode);
583 return qset_acl (dst_name, dest_desc, mode);
589 /* Copy access control lists from one file to another. If SOURCE_DESC is
590 a valid file descriptor, use file descriptor operations, else use
591 filename based operations on SRC_NAME. Likewise for DEST_DESC and
593 If access control lists are not available, fchmod the target file to
594 MODE. Also sets the non-permission bits of the destination file
595 (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
596 Return 0 if successful, otherwise output a diagnostic and return a
597 negative error code. */
600 copy_acl (const char *src_name, int source_desc, const char *dst_name,
601 int dest_desc, mode_t mode)
603 int ret = qcopy_acl (src_name, source_desc, dst_name, dest_desc, mode);
607 error (0, errno, "%s", quote (src_name));
611 error (0, errno, _("preserving permissions for %s"), quote (dst_name));