1 /* Support for extended attributes.
3 Copyright (C) 2006-2014, 2016-2017 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Written by James Antill, on 2006-07-27. */
31 #include "selinux-at.h"
33 struct xattrs_mask_map
40 /* list of fnmatch patterns */
43 /* lists of fnmatch patterns */
44 struct xattrs_mask_map incl
;
45 struct xattrs_mask_map excl
;
48 /* disable posix acls when problem found in gnulib script m4/acl.m4 */
50 # undef HAVE_POSIX_ACLS
53 #ifdef HAVE_POSIX_ACLS
58 #ifdef HAVE_POSIX_ACLS
60 /* acl-at wrappers, TODO: move to gnulib in future? */
61 static acl_t
acl_get_file_at (int, const char *, acl_type_t
);
62 static int acl_set_file_at (int, const char *, acl_type_t
, acl_t
);
63 static int file_has_acl_at (int, char const *, struct stat
const *);
64 static int acl_delete_def_file_at (int, char const *);
67 #define AT_FUNC_NAME acl_get_file_at
68 #define AT_FUNC_RESULT acl_t
69 #define AT_FUNC_FAIL (acl_t)NULL
70 #define AT_FUNC_F1 acl_get_file
71 #define AT_FUNC_POST_FILE_PARAM_DECLS , acl_type_t type
72 #define AT_FUNC_POST_FILE_ARGS , type
78 #undef AT_FUNC_POST_FILE_PARAM_DECLS
79 #undef AT_FUNC_POST_FILE_ARGS
82 #define AT_FUNC_NAME acl_set_file_at
83 #define AT_FUNC_F1 acl_set_file
84 #define AT_FUNC_POST_FILE_PARAM_DECLS , acl_type_t type, acl_t acl
85 #define AT_FUNC_POST_FILE_ARGS , type, acl
89 #undef AT_FUNC_POST_FILE_PARAM_DECLS
90 #undef AT_FUNC_POST_FILE_ARGS
92 /* acl_delete_def_file_at */
93 #define AT_FUNC_NAME acl_delete_def_file_at
94 #define AT_FUNC_F1 acl_delete_def_file
95 #define AT_FUNC_POST_FILE_PARAM_DECLS
96 #define AT_FUNC_POST_FILE_ARGS
100 #undef AT_FUNC_POST_FILE_PARAM_DECLS
101 #undef AT_FUNC_POST_FILE_ARGS
103 /* gnulib file_has_acl_at */
104 #define AT_FUNC_NAME file_has_acl_at
105 #define AT_FUNC_F1 file_has_acl
106 #define AT_FUNC_POST_FILE_PARAM_DECLS , struct stat const *st
107 #define AT_FUNC_POST_FILE_ARGS , st
111 #undef AT_FUNC_POST_FILE_PARAM_DECLS
112 #undef AT_FUNC_POST_FILE_ARGS
114 /* convert unix permissions into an ACL ... needed due to "default" ACLs */
116 perms2acl (int perms
)
118 char val
[] = "user::---,group::---,other::---";
119 /* 0123456789 123456789 123456789 123456789 */
145 return acl_from_text (val
);
149 skip_to_ext_fields (char *ptr
)
151 /* skip tag name (user/group/default/mask) */
152 ptr
+= strcspn (ptr
, ":,\n");
158 ptr
+= strcspn (ptr
, ":,\n"); /* skip user/group name */
164 ptr
+= strcspn (ptr
, ":,\n"); /* skip perms */
169 /* The POSIX draft allows extra fields after the three main ones. Star
170 uses this to add a fourth field for user/group which is the numeric ID.
171 This function removes such extra fields by overwriting them with the
172 characters that follow. */
174 fixup_extra_acl_fields (char *ptr
)
181 const char *old
= src
;
184 src
= skip_to_ext_fields (src
);
187 memmove (dst
, old
, len
);
190 if (*src
== ':') /* We have extra fields, skip them all */
191 src
+= strcspn (src
, "\n,");
193 if ((*src
== '\n') || (*src
== ','))
194 *dst
++ = *src
++; /* also done when dst == src, but that's ok */
202 /* Set the "system.posix_acl_access/system.posix_acl_default" extended
203 attribute. Called only when acls_option > 0. */
205 xattrs__acls_set (struct tar_stat_info
const *st
,
206 char const *file_name
, int type
,
207 char *ptr
, size_t len
, bool def
)
213 /* assert (strlen (ptr) == len); */
214 ptr
= fixup_extra_acl_fields (ptr
);
215 acl
= acl_from_text (ptr
);
219 /* No "default" IEEE 1003.1e ACL set for directory. At this moment,
220 FILE_NAME may already have inherited default acls from parent
221 directory; clean them up. */
222 if (acl_delete_def_file_at (chdir_fd
, file_name
))
223 WARNOPT (WARN_XATTR_WRITE
,
225 _("acl_delete_def_file_at: Cannot drop default POSIX ACLs "
231 acl
= perms2acl (st
->stat
.st_mode
);
235 call_arg_warn ("acl_from_text", file_name
);
239 if (acl_set_file_at (chdir_fd
, file_name
, type
, acl
) == -1)
240 /* warn even if filesystem does not support acls */
241 WARNOPT (WARN_XATTR_WRITE
,
243 _ ("acl_set_file_at: Cannot set POSIX ACLs for file '%s'"),
249 /* Cleanup textual representation of the ACL in VAL by eliminating tab
250 characters and comments */
252 xattrs_acls_cleanup (char *val
, size_t *plen
)
256 p
= q
= val
+ strcspn (val
, "#\t");
274 xattrs__acls_get_a (int parentfd
, const char *file_name
,
275 struct tar_stat_info
*st
,
276 char **ret_ptr
, size_t * ret_len
)
281 if (!(acl
= acl_get_file_at (parentfd
, file_name
, ACL_TYPE_ACCESS
)))
283 if (errno
!= ENOTSUP
)
284 call_arg_warn ("acl_get_file_at", file_name
);
288 val
= acl_to_text (acl
, NULL
);
293 call_arg_warn ("acl_to_text", file_name
);
297 *ret_ptr
= xstrdup (val
);
298 xattrs_acls_cleanup (*ret_ptr
, ret_len
);
302 /* "system.posix_acl_default" */
304 xattrs__acls_get_d (int parentfd
, char const *file_name
,
305 struct tar_stat_info
*st
,
306 char **ret_ptr
, size_t * ret_len
)
311 if (!(acl
= acl_get_file_at (parentfd
, file_name
, ACL_TYPE_DEFAULT
)))
313 if (errno
!= ENOTSUP
)
314 call_arg_warn ("acl_get_file_at", file_name
);
318 val
= acl_to_text (acl
, NULL
);
323 call_arg_warn ("acl_to_text", file_name
);
327 *ret_ptr
= xstrdup (val
);
328 xattrs_acls_cleanup (*ret_ptr
, ret_len
);
331 #endif /* HAVE_POSIX_ACLS */
334 acls_one_line (const char *prefix
, char delim
,
335 const char *aclstring
, size_t len
)
337 /* support both long and short text representation of posix acls */
339 int pref_len
= strlen (prefix
);
340 const char *oldstring
= aclstring
;
343 if (!aclstring
|| !len
)
349 int move
= strcspn (aclstring
, ",\n");
353 if (oldstring
!= aclstring
)
354 obstack_1grow (&stk
, delim
);
356 obstack_grow (&stk
, prefix
, pref_len
);
357 obstack_grow (&stk
, aclstring
, move
);
360 aclstring
+= move
+ 1;
363 obstack_1grow (&stk
, '\0');
365 fprintf (stdlis
, "%s", (char *) obstack_finish (&stk
));
367 obstack_free (&stk
, NULL
);
371 xattrs_acls_get (int parentfd
, char const *file_name
,
372 struct tar_stat_info
*st
, int fd
, int xisfile
)
376 #ifndef HAVE_POSIX_ACLS
379 WARN ((0, 0, _("POSIX ACL support is not available")));
382 int err
= file_has_acl_at (parentfd
, file_name
, &st
->stat
);
387 call_arg_warn ("file_has_acl_at", file_name
);
391 xattrs__acls_get_a (parentfd
, file_name
, st
,
392 &st
->acls_a_ptr
, &st
->acls_a_len
);
394 xattrs__acls_get_d (parentfd
, file_name
, st
,
395 &st
->acls_d_ptr
, &st
->acls_d_len
);
401 xattrs_acls_set (struct tar_stat_info
const *st
,
402 char const *file_name
, char typeflag
)
404 if (acls_option
> 0 && typeflag
!= SYMTYPE
)
406 #ifndef HAVE_POSIX_ACLS
409 WARN ((0, 0, _("POSIX ACL support is not available")));
412 xattrs__acls_set (st
, file_name
, ACL_TYPE_ACCESS
,
413 st
->acls_a_ptr
, st
->acls_a_len
, false);
414 if (typeflag
== DIRTYPE
|| typeflag
== GNUTYPE_DUMPDIR
)
415 xattrs__acls_set (st
, file_name
, ACL_TYPE_DEFAULT
,
416 st
->acls_d_ptr
, st
->acls_d_len
, true);
422 mask_map_realloc (struct xattrs_mask_map
*map
)
424 if (map
->used
== map
->size
)
428 map
->masks
= x2nrealloc (map
->masks
, &map
->size
, sizeof (map
->masks
[0]));
433 xattrs_mask_add (const char *mask
, bool incl
)
435 struct xattrs_mask_map
*mask_map
=
436 incl
? &xattrs_setup
.incl
: &xattrs_setup
.excl
;
437 /* ensure there is enough space */
438 mask_map_realloc (mask_map
);
439 /* just assign pointers -- we silently expect that pointer "mask" is valid
440 through the whole program (pointer to argv array) */
441 mask_map
->masks
[mask_map
->used
++] = mask
;
445 clear_mask_map (struct xattrs_mask_map
*mask_map
)
448 free (mask_map
->masks
);
452 xattrs_clear_setup (void)
454 clear_mask_map (&xattrs_setup
.incl
);
455 clear_mask_map (&xattrs_setup
.excl
);
458 static bool xattrs_masked_out (const char *kw
, bool archiving
);
460 /* get xattrs from file given by FILE_NAME or FD (when non-zero)
461 xattrs are checked against the user supplied include/exclude mask
462 if no mask is given this includes all the user.*, security.*, system.*,
463 etc. available domains */
465 xattrs_xattrs_get (int parentfd
, char const *file_name
,
466 struct tar_stat_info
*st
, int fd
)
468 if (xattrs_option
> 0)
473 WARN ((0, 0, _("XATTR support is not available")));
476 static size_t xsz
= 1024;
477 static char *xatrs
= NULL
;
481 xatrs
= x2nrealloc (xatrs
, &xsz
, 1);
485 llistxattrat (parentfd
, file_name
, xatrs
, xsz
)) == -1) :
486 ((xret
= flistxattr (fd
, xatrs
, xsz
)) == -1))
487 && (errno
== ERANGE
))
489 xatrs
= x2nrealloc (xatrs
, &xsz
, 1);
493 call_arg_warn ((fd
== 0) ? "llistxattrat" : "flistxattr", file_name
);
496 const char *attr
= xatrs
;
497 static size_t asz
= 1024;
498 static char *val
= NULL
;
501 val
= x2nrealloc (val
, &asz
, 1);
505 size_t len
= strlen (attr
);
509 ? ((aret
= lgetxattrat (parentfd
, file_name
, attr
,
511 : ((aret
= fgetxattr (fd
, attr
, val
, asz
)) == -1))
512 && (errno
== ERANGE
))
514 val
= x2nrealloc (val
, &asz
, 1);
519 if (!xattrs_masked_out (attr
, true))
520 xheader_xattr_add (st
, attr
, val
, aret
);
522 else if (errno
!= ENOATTR
)
523 call_arg_warn ((fd
== 0) ? "lgetxattrat"
524 : "fgetxattr", file_name
);
536 xattrs__fd_set (struct tar_stat_info
const *st
,
537 char const *file_name
, char typeflag
,
538 const char *attr
, const char *ptr
, size_t len
)
542 const char *sysname
= "setxattrat";
545 if (typeflag
!= SYMTYPE
)
546 ret
= setxattrat (chdir_fd
, file_name
, attr
, ptr
, len
, 0);
549 sysname
= "lsetxattr";
550 ret
= lsetxattrat (chdir_fd
, file_name
, attr
, ptr
, len
, 0);
554 WARNOPT (WARN_XATTR_WRITE
,
556 _("%s: Cannot set '%s' extended attribute for file '%s'"),
557 sysname
, attr
, file_name
));
562 /* lgetfileconat is called against FILE_NAME iff the FD parameter is set to
563 zero, otherwise the fgetfileconat is used against correct file descriptor */
565 xattrs_selinux_get (int parentfd
, char const *file_name
,
566 struct tar_stat_info
*st
, int fd
)
568 if (selinux_context_option
> 0)
570 #if HAVE_SELINUX_SELINUX_H != 1
573 WARN ((0, 0, _("SELinux support is not available")));
577 fgetfilecon (fd
, &st
->cntx_name
)
578 : lgetfileconat (parentfd
, file_name
, &st
->cntx_name
);
580 if (result
== -1 && errno
!= ENODATA
&& errno
!= ENOTSUP
)
581 call_arg_warn (fd
? "fgetfilecon" : "lgetfileconat", file_name
);
587 xattrs_selinux_set (struct tar_stat_info
const *st
,
588 char const *file_name
, char typeflag
)
590 if (selinux_context_option
> 0)
592 #if HAVE_SELINUX_SELINUX_H != 1
595 WARN ((0, 0, _("SELinux support is not available")));
598 const char *sysname
= "setfilecon";
604 if (typeflag
!= SYMTYPE
)
606 ret
= setfileconat (chdir_fd
, file_name
, st
->cntx_name
);
607 sysname
= "setfileconat";
611 ret
= lsetfileconat (chdir_fd
, file_name
, st
->cntx_name
);
612 sysname
= "lsetfileconat";
616 WARNOPT (WARN_XATTR_WRITE
,
618 _("%s: Cannot set SELinux context for file '%s'"),
619 sysname
, file_name
));
625 xattrs_matches_mask (const char *kw
, struct xattrs_mask_map
*mm
)
632 for (i
= 0; i
< mm
->used
; i
++)
633 if (fnmatch (mm
->masks
[i
], kw
, 0) == 0)
639 #define USER_DOT_PFX "user."
642 xattrs_kw_included (const char *kw
, bool archiving
)
644 if (xattrs_setup
.incl
.size
)
645 return xattrs_matches_mask (kw
, &xattrs_setup
.incl
);
649 return strncmp (kw
, USER_DOT_PFX
, sizeof (USER_DOT_PFX
) - 1) == 0;
653 xattrs_kw_excluded (const char *kw
, bool archiving
)
655 return xattrs_setup
.excl
.size
?
656 xattrs_matches_mask (kw
, &xattrs_setup
.excl
) : false;
659 /* Check whether the xattr with keyword KW should be discarded from list of
660 attributes that are going to be archived/excluded (set ARCHIVING=true for
661 archiving, false for excluding) */
663 xattrs_masked_out (const char *kw
, bool archiving
)
665 return xattrs_kw_included (kw
, archiving
) ?
666 xattrs_kw_excluded (kw
, archiving
) : true;
670 xattrs_xattrs_set (struct tar_stat_info
const *st
,
671 char const *file_name
, char typeflag
, int later_run
)
673 if (xattrs_option
> 0)
678 WARN ((0, 0, _("XATTR support is not available")));
683 if (!st
->xattr_map_size
)
686 for (; scan
< st
->xattr_map_size
; ++scan
)
688 char *keyword
= st
->xattr_map
[scan
].xkey
;
689 keyword
+= strlen ("SCHILY.xattr.");
691 /* TODO: this 'later_run' workaround is temporary solution -> once
692 capabilities should become fully supported by it's API and there
693 should exist something like xattrs_capabilities_set() call.
694 For a regular files: all extended attributes are restored during
695 the first run except 'security.capability' which is restored in
697 if (typeflag
== REGTYPE
698 && later_run
== !!strcmp (keyword
, "security.capability"))
701 if (xattrs_masked_out (keyword
, false /* extracting */ ))
702 /* we don't want to restore this keyword */
705 xattrs__fd_set (st
, file_name
, typeflag
, keyword
,
706 st
->xattr_map
[scan
].xval_ptr
,
707 st
->xattr_map
[scan
].xval_len
);
714 xattrs_print_char (struct tar_stat_info
const *st
, char *output
)
718 if (verbose_option
< 2)
724 if (xattrs_option
> 0 || selinux_context_option
> 0 || acls_option
> 0)
731 if (xattrs_option
> 0 && st
->xattr_map_size
)
732 for (i
= 0; i
< st
->xattr_map_size
; ++i
)
734 char *keyword
= st
->xattr_map
[i
].xkey
+ strlen ("SCHILY.xattr.");
735 if (!xattrs_masked_out (keyword
, false /* like extracting */ ))
742 if (selinux_context_option
> 0 && st
->cntx_name
)
745 if (acls_option
> 0 && (st
->acls_a_len
|| st
->acls_d_len
))
750 xattrs_print (struct tar_stat_info
const *st
)
752 if (verbose_option
< 3)
756 if (selinux_context_option
> 0 && st
->cntx_name
)
757 fprintf (stdlis
, " s: %s\n", st
->cntx_name
);
760 if (acls_option
> 0 && (st
->acls_a_len
|| st
->acls_d_len
))
762 fprintf (stdlis
, " a: ");
763 acls_one_line ("", ',', st
->acls_a_ptr
, st
->acls_a_len
);
764 if (st
->acls_a_len
&& st
->acls_d_len
)
765 fprintf (stdlis
, ",");
766 acls_one_line ("default:", ',', st
->acls_d_ptr
, st
->acls_d_len
);
767 fprintf (stdlis
, "\n");
771 if (xattrs_option
> 0 && st
->xattr_map_size
)
775 for (i
= 0; i
< st
->xattr_map_size
; ++i
)
777 char *keyword
= st
->xattr_map
[i
].xkey
+ strlen ("SCHILY.xattr.");
778 if (!xattrs_masked_out (keyword
, false /* like extracting */ ))
779 fprintf (stdlis
, " x: %lu %s\n",
780 (unsigned long) st
->xattr_map
[i
].xval_len
, keyword
);