1 /* copy-acl.c - copy access control list from one file to another file
3 Copyright (C) 2002-2003, 2005-2014 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 /* Copy access control lists from one file to another. If SOURCE_DESC is
28 a valid file descriptor, use file descriptor operations, else use
29 filename based operations on SRC_NAME. Likewise for DEST_DESC and
31 If access control lists are not available, fchmod the target file to
32 MODE. Also sets the non-permission bits of the destination file
33 (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
34 Return 0 if successful.
35 Return -2 and set errno for an error relating to the source file.
36 Return -1 and set errno for an error relating to the destination file. */
39 qcopy_acl (const char *src_name
, int source_desc
, const char *dst_name
,
40 int dest_desc
, mode_t mode
)
42 #if USE_ACL && HAVE_ACL_GET_FILE
43 /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
44 /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
45 # if !HAVE_ACL_TYPE_EXTENDED
46 /* Linux, FreeBSD, IRIX, Tru64 */
51 if (HAVE_ACL_GET_FD
&& source_desc
!= -1)
52 acl
= acl_get_fd (source_desc
);
54 acl
= acl_get_file (src_name
, ACL_TYPE_ACCESS
);
57 if (! acl_errno_valid (errno
))
58 return qset_acl (dst_name
, dest_desc
, mode
);
63 if (HAVE_ACL_SET_FD
&& dest_desc
!= -1)
64 ret
= acl_set_fd (dest_desc
, acl
);
66 ret
= acl_set_file (dst_name
, ACL_TYPE_ACCESS
, acl
);
69 int saved_errno
= errno
;
71 if (! acl_errno_valid (errno
) && !acl_access_nontrivial (acl
))
74 return chmod_or_fchmod (dst_name
, dest_desc
, mode
);
79 chmod_or_fchmod (dst_name
, dest_desc
, mode
);
87 if (!MODE_INSIDE_ACL
|| (mode
& (S_ISUID
| S_ISGID
| S_ISVTX
)))
89 /* We did not call chmod so far, and either the mode and the ACL are
90 separate or special bits are to be set which don't fit into ACLs. */
92 if (chmod_or_fchmod (dst_name
, dest_desc
, mode
) != 0)
98 acl
= acl_get_file (src_name
, ACL_TYPE_DEFAULT
);
102 if (acl_set_file (dst_name
, ACL_TYPE_DEFAULT
, acl
))
104 int saved_errno
= errno
;
115 # else /* HAVE_ACL_TYPE_EXTENDED */
118 /* On Mac OS X, acl_get_file (name, ACL_TYPE_ACCESS)
119 and acl_get_file (name, ACL_TYPE_DEFAULT)
120 always return NULL / EINVAL. You have to use
121 acl_get_file (name, ACL_TYPE_EXTENDED)
122 or acl_get_fd (open (name, ...))
125 acl_set_file (name, ACL_TYPE_ACCESS, acl)
126 and acl_set_file (name, ACL_TYPE_DEFAULT, acl)
127 have the same effect as
128 acl_set_file (name, ACL_TYPE_EXTENDED, acl):
129 Each of these calls sets the file's ACL. */
134 if (HAVE_ACL_GET_FD
&& source_desc
!= -1)
135 acl
= acl_get_fd (source_desc
);
137 acl
= acl_get_file (src_name
, ACL_TYPE_EXTENDED
);
140 if (!acl_errno_valid (errno
))
141 return qset_acl (dst_name
, dest_desc
, mode
);
146 if (HAVE_ACL_SET_FD
&& dest_desc
!= -1)
147 ret
= acl_set_fd (dest_desc
, acl
);
149 ret
= acl_set_file (dst_name
, ACL_TYPE_EXTENDED
, acl
);
152 int saved_errno
= errno
;
154 if (!acl_errno_valid (saved_errno
) && !acl_extended_nontrivial (acl
))
157 return chmod_or_fchmod (dst_name
, dest_desc
, mode
);
162 chmod_or_fchmod (dst_name
, dest_desc
, mode
);
170 /* Since !MODE_INSIDE_ACL, we have to call chmod explicitly. */
171 return chmod_or_fchmod (dst_name
, dest_desc
, mode
);
175 #elif USE_ACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
177 /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
178 of Unixware. The acl() call returns the access and default ACL both
191 /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
192 file systems (whereas the other ones are used in UFS file systems).
194 pathconf (name, _PC_ACL_ENABLED)
195 fpathconf (desc, _PC_ACL_ENABLED)
196 that allows to determine which of the two kinds of ACLs is supported
197 for the given file. But some file systems may implement this call
198 incorrectly, so better not use it.
199 When fetching the source ACL, we simply fetch both ACL types.
200 When setting the destination ACL, we try either ACL types, assuming
201 that the kernel will translate the ACL from one form to the other.
202 (See in <http://docs.sun.com/app/docs/doc/819-2241/6n4huc7ia?l=en&a=view>
203 the description of ENOTSUP.) */
206 ace_count
= (source_desc
!= -1
207 ? facl (source_desc
, ACE_GETACLCNT
, 0, NULL
)
208 : acl (src_name
, ACE_GETACLCNT
, 0, NULL
));
212 if (errno
== ENOSYS
|| errno
== EINVAL
)
228 ace_entries
= (ace_t
*) malloc (ace_count
* sizeof (ace_t
));
229 if (ace_entries
== NULL
)
235 ret
= (source_desc
!= -1
236 ? facl (source_desc
, ACE_GETACL
, ace_count
, ace_entries
)
237 : acl (src_name
, ACE_GETACL
, ace_count
, ace_entries
));
241 if (errno
== ENOSYS
|| errno
== EINVAL
)
250 if (ret
== ace_count
)
252 /* Huh? The number of ACL entries changed since the last call.
259 count
= (source_desc
!= -1
260 ? facl (source_desc
, GETACLCNT
, 0, NULL
)
261 : acl (src_name
, GETACLCNT
, 0, NULL
));
265 if (errno
== ENOSYS
|| errno
== ENOTSUP
|| errno
== EOPNOTSUPP
)
281 entries
= (aclent_t
*) malloc (count
* sizeof (aclent_t
));
288 if ((source_desc
!= -1
289 ? facl (source_desc
, GETACL
, count
, entries
)
290 : acl (src_name
, GETACL
, count
, entries
))
293 /* Huh? The number of ACL entries changed since the last call.
297 /* Is there an ACL of either kind? */
302 return qset_acl (dst_name
, dest_desc
, mode
);
304 did_chmod
= 0; /* set to 1 once the mode bits in 0777 have been set */
305 saved_errno
= 0; /* the first non-ignorable error code */
307 if (!MODE_INSIDE_ACL
)
309 /* On Cygwin, it is necessary to call chmod before acl, because
310 chmod can change the contents of the ACL (in ways that don't
311 change the allowed accesses, but still visible). */
312 if (chmod_or_fchmod (dst_name
, dest_desc
, mode
) != 0)
317 /* If both ace_entries and entries are available, try SETACL before
318 ACE_SETACL, because SETACL cannot fail with ENOTSUP whereas ACE_SETACL
323 ret
= (dest_desc
!= -1
324 ? facl (dest_desc
, SETACL
, count
, entries
)
325 : acl (dst_name
, SETACL
, count
, entries
));
326 if (ret
< 0 && saved_errno
== 0)
329 if ((errno
== ENOSYS
|| errno
== EOPNOTSUPP
|| errno
== EINVAL
)
330 && !acl_nontrivial (count
, entries
))
341 ret
= (dest_desc
!= -1
342 ? facl (dest_desc
, ACE_SETACL
, ace_count
, ace_entries
)
343 : acl (dst_name
, ACE_SETACL
, ace_count
, ace_entries
));
344 if (ret
< 0 && saved_errno
== 0)
347 if ((errno
== ENOSYS
|| errno
== EINVAL
|| errno
== ENOTSUP
)
348 && !acl_ace_nontrivial (ace_count
, ace_entries
))
356 && did_chmod
<= ((mode
& (S_ISUID
| S_ISGID
| S_ISVTX
)) ? 1 : 0))
358 /* We did not call chmod so far, and either the mode and the ACL are
359 separate or special bits are to be set which don't fit into ACLs. */
361 if (chmod_or_fchmod (dst_name
, dest_desc
, mode
) != 0)
363 if (saved_errno
== 0)
375 #elif USE_ACL && HAVE_GETACL /* HP-UX */
377 struct acl_entry entries
[NACLENTRIES
];
380 struct acl aclv_entries
[NACLVENTRIES
];
387 count
= (source_desc
!= -1
388 ? fgetacl (source_desc
, NACLENTRIES
, entries
)
389 : getacl (src_name
, NACLENTRIES
, entries
));
393 if (errno
== ENOSYS
|| errno
== EOPNOTSUPP
|| errno
== ENOTSUP
)
400 if (count
> NACLENTRIES
)
401 /* If NACLENTRIES cannot be trusted, use dynamic memory allocation. */
406 aclv_count
= acl ((char *) src_name
, ACL_GET
, NACLVENTRIES
, aclv_entries
);
410 if (errno
== ENOSYS
|| errno
== EOPNOTSUPP
|| errno
== EINVAL
)
415 else if (aclv_count
> 0)
417 if (aclv_count
> NACLVENTRIES
)
418 /* If NACLVENTRIES cannot be trusted, use dynamic memory allocation. */
427 return qset_acl (dst_name
, dest_desc
, mode
);
429 did_chmod
= 0; /* set to 1 once the mode bits in 0777 have been set */
430 saved_errno
= 0; /* the first non-ignorable error code */
434 ret
= (dest_desc
!= -1
435 ? fsetacl (dest_desc
, count
, entries
)
436 : setacl (dst_name
, count
, entries
));
437 if (ret
< 0 && saved_errno
== 0)
440 if (errno
== ENOSYS
|| errno
== EOPNOTSUPP
|| errno
== ENOTSUP
)
442 struct stat source_statbuf
;
444 if ((source_desc
!= -1
445 ? fstat (source_desc
, &source_statbuf
)
446 : stat (src_name
, &source_statbuf
)) == 0)
448 if (!acl_nontrivial (count
, entries
, &source_statbuf
))
462 ret
= acl ((char *) dst_name
, ACL_SET
, aclv_count
, aclv_entries
);
463 if (ret
< 0 && saved_errno
== 0)
466 if (errno
== ENOSYS
|| errno
== EOPNOTSUPP
|| errno
== EINVAL
)
468 if (!aclv_nontrivial (aclv_count
, aclv_entries
))
477 if (did_chmod
<= ((mode
& (S_ISUID
| S_ISGID
| S_ISVTX
)) ? 1 : 0))
479 /* We did not call chmod so far, and special bits are to be set which
480 don't fit into ACLs. */
482 if (chmod_or_fchmod (dst_name
, dest_desc
, mode
) != 0)
484 if (saved_errno
== 0)
496 #elif USE_ACL && HAVE_ACLX_GET && 0 /* AIX */
500 #elif USE_ACL && HAVE_STATACL /* older AIX */
502 union { struct acl a
; char room
[4096]; } u
;
505 if ((source_desc
!= -1
506 ? fstatacl (source_desc
, STX_NORMAL
, &u
.a
, sizeof (u
))
507 : statacl (src_name
, STX_NORMAL
, &u
.a
, sizeof (u
)))
511 ret
= (dest_desc
!= -1
512 ? fchacl (dest_desc
, &u
.a
, u
.a
.acl_len
)
513 : chacl (dst_name
, &u
.a
, u
.a
.acl_len
));
516 int saved_errno
= errno
;
518 chmod_or_fchmod (dst_name
, dest_desc
, mode
);
523 /* No need to call chmod_or_fchmod at this point, since the mode bits
524 S_ISUID, S_ISGID, S_ISVTX are also stored in the ACL. */
528 #elif USE_ACL && HAVE_ACLSORT /* NonStop Kernel */
530 struct acl entries
[NACLENTRIES
];
534 count
= acl ((char *) src_name
, ACL_GET
, NACLENTRIES
, entries
);
545 if (count
> NACLENTRIES
)
546 /* If NACLENTRIES cannot be trusted, use dynamic memory allocation. */
551 return qset_acl (dst_name
, dest_desc
, mode
);
553 ret
= acl ((char *) dst_name
, ACL_SET
, count
, entries
);
556 int saved_errno
= errno
;
560 if (!acl_nontrivial (count
, entries
))
561 return chmod_or_fchmod (dst_name
, dest_desc
, mode
);
564 chmod_or_fchmod (dst_name
, dest_desc
, mode
);
569 if (mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
571 /* We did not call chmod so far, and either the mode and the ACL are
572 separate or special bits are to be set which don't fit into ACLs. */
574 return chmod_or_fchmod (dst_name
, dest_desc
, mode
);
580 return qset_acl (dst_name
, dest_desc
, mode
);