1 /* Support for extended attributes.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software
6 Written by James Antill, on 2006-07-27.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
32 struct xattrs_mask_map
39 /* list of fnmatch patterns */
42 /* lists of fnmatch patterns */
43 struct xattrs_mask_map incl
;
44 struct xattrs_mask_map excl
;
47 /* disable posix acls when problem found in gnulib script m4/acl.m4 */
49 # undef HAVE_POSIX_ACLS
52 #ifdef HAVE_POSIX_ACLS
57 #ifdef HAVE_POSIX_ACLS
59 /* acl-at wrappers, TODO: move to gnulib in future? */
60 acl_t
acl_get_file_at (int dirfd
, const char *file
, acl_type_t type
);
61 int acl_set_file_at (int dirfd
, const char *file
, acl_type_t type
, acl_t acl
);
62 int file_has_acl_at (int dirfd
, char const *, struct stat
const *);
65 #define AT_FUNC_NAME acl_get_file_at
66 #define AT_FUNC_RESULT acl_t
67 #define AT_FUNC_FAIL (acl_t)NULL
68 #define AT_FUNC_F1 acl_get_file
69 #define AT_FUNC_POST_FILE_PARAM_DECLS , acl_type_t type
70 #define AT_FUNC_POST_FILE_ARGS , type
76 #undef AT_FUNC_POST_FILE_PARAM_DECLS
77 #undef AT_FUNC_POST_FILE_ARGS
80 #define AT_FUNC_NAME acl_set_file_at
81 #define AT_FUNC_F1 acl_set_file
82 #define AT_FUNC_POST_FILE_PARAM_DECLS , acl_type_t type, acl_t acl
83 #define AT_FUNC_POST_FILE_ARGS , type, acl
87 #undef AT_FUNC_POST_FILE_PARAM_DECLS
88 #undef AT_FUNC_POST_FILE_ARGS
90 /* gnulib file_has_acl_at */
91 #define AT_FUNC_NAME file_has_acl_at
92 #define AT_FUNC_F1 file_has_acl
93 #define AT_FUNC_POST_FILE_PARAM_DECLS , struct stat const *st
94 #define AT_FUNC_POST_FILE_ARGS , st
98 #undef AT_FUNC_POST_FILE_PARAM_DECLS
99 #undef AT_FUNC_POST_FILE_ARGS
101 /* convert unix permissions into an ACL ... needed due to "default" ACLs */
102 static acl_t
perms2acl (int perms
)
104 char val
[] = "user::---,group::---,other::---";
105 /* 0123456789 123456789 123456789 123456789 */
108 if (perms
& 0400) val
[ 6] = 'r';
109 if (perms
& 0200) val
[ 7] = 'w';
110 if (perms
& 0100) val
[ 8] = 'x';
113 if (perms
& 0040) val
[17] = 'r';
114 if (perms
& 0020) val
[18] = 'w';
115 if (perms
& 0010) val
[19] = 'x';
118 if (perms
& 0004) val
[28] = 'r';
119 if (perms
& 0002) val
[29] = 'w';
120 if (perms
& 0001) val
[30] = 'x';
122 return (acl_from_text (val
));
125 static char *skip_to_ext_fields (char *ptr
)
127 ptr
+= strcspn (ptr
, ":,\n"); /* skip tag name. Ie. user/group/default/mask */
130 return (ptr
); /* error? no user/group field */
133 ptr
+= strcspn (ptr
, ":,\n"); /* skip user/group name */
136 return (ptr
); /* error? no perms field */
139 ptr
+= strcspn (ptr
, ":,\n"); /* skip perms */
142 return (ptr
); /* no extra fields */
147 /* The POSIX draft allows extra fields after the three main ones. Star
148 uses this to add a fourth field for user/group which is the numeric ID.
149 We just skip all extra fields atm. */
150 static const char *fixup_extra_acl_fields (const char *ptr
)
152 char *src
= (char *)ptr
;
153 char *dst
= (char *)ptr
;
157 const char *old
= src
;
160 src
= skip_to_ext_fields (src
);
162 if (old
!= dst
) memmove (dst
, old
, len
);
165 if (*src
== ':') /* We have extra fields, skip them all */
166 src
+= strcspn (src
, "\n,");
168 if ((*src
== '\n') || (*src
== ','))
169 *dst
++ = *src
++; /* also done when dst == src, but that's ok */
177 static void xattrs__acls_set (struct tar_stat_info
const *st
,
178 char const *file_name
, int type
,
179 const char *ptr
, size_t len
, bool def
)
180 { /* "system.posix_acl_access" */
185 /* assert (strlen (ptr) == len); */
186 ptr
= fixup_extra_acl_fields (ptr
);
188 acl
= acl_from_text (ptr
);
191 else if (acls_option
> 0)
192 acl
= perms2acl (st
->stat
.st_mode
);
194 return; /* don't call acl functions unless we first hit an ACL, or
195 --acls was passed explicitly */
197 if (acl
== (acl_t
)NULL
)
199 call_arg_warn ("acl_from_text", file_name
);
203 if (acl_set_file_at (chdir_fd
, file_name
, type
, acl
) == -1)
204 /* warn even if filesystem does not support acls */
205 WARNOPT (WARN_XATTR_WRITE
, (0, errno
,
206 _("acl_set_file_at: Cannot set POSIX ACLs for file '%s'"), file_name
));
211 static void xattrs__acls_get_a (int parentfd
, const char *file_name
,
212 struct tar_stat_info
*st
,
213 char **ret_ptr
, size_t *ret_len
)
214 { /* "system.posix_acl_access" */
219 if ((acl
= acl_get_file_at (parentfd
, file_name
, ACL_TYPE_ACCESS
))
222 if (errno
!= ENOTSUP
)
223 call_arg_warn ("acl_get_file_at", file_name
);
227 val
= acl_to_text (acl
, &len
);
232 call_arg_warn ("acl_to_text", file_name
);
236 *ret_ptr
= xstrdup (val
);
242 static void xattrs__acls_get_d (int parentfd
, char const *file_name
,
243 struct tar_stat_info
*st
,
244 char **ret_ptr
, size_t *ret_len
)
245 { /* "system.posix_acl_default" */
250 if ((acl
= acl_get_file_at (parentfd
, file_name
, ACL_TYPE_DEFAULT
))
253 if (errno
!= ENOTSUP
)
254 call_arg_warn ("acl_get_file_at", file_name
);
258 val
= acl_to_text (acl
, &len
);
263 call_arg_warn ("acl_to_text", file_name
);
267 *ret_ptr
= xstrdup (val
);
272 #endif /* HAVE_POSIX_ACLS */
274 static void acls_one_line (const char *prefix
, char delim
,
275 const char *aclstring
, size_t len
)
277 /* support both long and short text representation of posix acls */
280 int pref_len
= strlen (prefix
);
281 const char *oldstring
= aclstring
;
283 if (!aclstring
|| !len
)
289 int move
= strcspn (aclstring
, ",\n");
293 if (oldstring
!= aclstring
)
294 obstack_1grow (&stk
, delim
);
296 obstack_grow (&stk
, prefix
, pref_len
);
297 obstack_grow (&stk
, aclstring
, move
);
299 aclstring
+= move
+ 1;
302 obstack_1grow (&stk
, '\0');
303 const char *toprint
= obstack_finish (&stk
);
305 fprintf (stdlis
, "%s", toprint
);
307 obstack_free (&stk
, NULL
);
310 void xattrs_acls_get (int parentfd
, char const *file_name
,
311 struct tar_stat_info
*st
, int fd
, int xisfile
)
315 #ifndef HAVE_POSIX_ACLS
318 WARN ((0, 0, _("POSIX ACL support is not available")));
321 int err
= file_has_acl_at (parentfd
, file_name
, &st
->stat
);
326 call_arg_warn ("file_has_acl_at", file_name
);
330 xattrs__acls_get_a (parentfd
, file_name
, st
,
331 &st
->acls_a_ptr
, &st
->acls_a_len
);
333 xattrs__acls_get_d (parentfd
, file_name
, st
,
334 &st
->acls_d_ptr
, &st
->acls_d_len
);
339 void xattrs_acls_set (struct tar_stat_info
const *st
,
340 char const *file_name
, char typeflag
)
342 if ((acls_option
> 0) && (typeflag
!= SYMTYPE
))
344 #ifndef HAVE_POSIX_ACLS
347 WARN ((0, 0, _("POSIX ACL support is not available")));
350 xattrs__acls_set (st
, file_name
, ACL_TYPE_ACCESS
,
351 st
->acls_a_ptr
, st
->acls_a_len
, false);
352 if ((typeflag
== DIRTYPE
) || (typeflag
== GNUTYPE_DUMPDIR
))
353 xattrs__acls_set (st
, file_name
, ACL_TYPE_DEFAULT
,
354 st
->acls_d_ptr
, st
->acls_d_len
, true);
359 static void mask_map_realloc (struct xattrs_mask_map
*map
)
364 map
->masks
= xmalloc (16 * sizeof (char *));
368 if (map
->size
<= map
->used
)
371 map
->masks
= xrealloc (map
->masks
, map
->size
* sizeof (char *));
376 void xattrs_mask_add (const char *mask
, bool incl
)
378 struct xattrs_mask_map
*mask_map
= incl
? &xattrs_setup
.incl
379 : &xattrs_setup
.excl
;
380 /* ensure there is enough space */
381 mask_map_realloc (mask_map
);
382 /* just assign pointers -- we silently expect that pointer "mask" is valid
383 through the whole program (pointer to argv array) */
384 mask_map
->masks
[mask_map
->used
++] = mask
;
387 static void clear_mask_map (struct xattrs_mask_map
*mask_map
)
390 free (mask_map
->masks
);
393 void xattrs_clear_setup ()
395 clear_mask_map (&xattrs_setup
.incl
);
396 clear_mask_map (&xattrs_setup
.excl
);
399 /* get all xattrs from file given by FILE_NAME or FD (when non-zero). This
400 includes all the user.*, security.*, system.*, etc. available domains */
401 void xattrs_xattrs_get (int parentfd
, char const *file_name
,
402 struct tar_stat_info
*st
, int fd
)
404 if (xattrs_option
> 0)
409 WARN ((0, 0, _("XATTR support is not available")));
412 static ssize_t xsz
= 1024;
413 static char *xatrs
= NULL
;
416 if (!xatrs
) xatrs
= xmalloc (xsz
);
419 ((xret
= llistxattrat (parentfd
, file_name
, xatrs
, xsz
)) == -1) :
420 ((xret
= flistxattr (fd
, xatrs
, xsz
)) == -1)) &&
424 xatrs
= xrealloc (xatrs
, xsz
);
428 call_arg_warn ((fd
== 0) ? "llistxattrat" : "flistxattr", file_name
);
431 const char *attr
= xatrs
;
432 static ssize_t asz
= 1024;
433 static char *val
= NULL
;
435 if (!val
) val
= xmalloc (asz
);
439 size_t len
= strlen (attr
);
442 /* Archive all xattrs during creation, decide at extraction time
443 * which ones are of interest/use for the target filesystem. */
445 ? ((aret
= lgetxattrat (parentfd
, file_name
, attr
,
447 : ((aret
= fgetxattr (fd
, attr
, val
, asz
)) == -1))
448 && (errno
== ERANGE
))
451 val
= xrealloc (val
, asz
);
455 xheader_xattr_add (st
, attr
, val
, aret
);
456 else if (errno
!= ENOATTR
)
457 call_arg_warn ((fd
== 0) ? "lgetxattrat"
458 : "fgetxattr", file_name
);
468 static void xattrs__fd_set (struct tar_stat_info
const *st
,
469 char const *file_name
, char typeflag
,
471 const char *ptr
, size_t len
)
475 const char *sysname
= "setxattrat";
478 if (typeflag
!= SYMTYPE
)
479 ret
= setxattrat (chdir_fd
, file_name
, attr
, ptr
, len
, 0);
482 sysname
= "lsetxattr";
483 ret
= lsetxattrat (chdir_fd
, file_name
, attr
, ptr
, len
, 0);
487 WARNOPT (WARN_XATTR_WRITE
, (0, errno
,
488 _("%s: Cannot set '%s' extended attribute for file '%s'"),
489 sysname
, attr
, file_name
));
493 static bool xattrs_matches_mask (const char *kw
, struct xattrs_mask_map
*mm
)
500 for (i
= 0; i
< mm
->used
; i
++)
501 if (fnmatch (mm
->masks
[i
], kw
, 0) == 0)
507 static bool xattrs_kw_included (const char *kw
, bool archiving
)
509 if (xattrs_setup
.incl
.size
)
510 return xattrs_matches_mask (kw
, &xattrs_setup
.incl
);
516 return strncmp (kw
, "user.", strlen ("user.")) == 0;
520 static bool xattrs_kw_excluded (const char *kw
, bool archiving
)
522 if (!xattrs_setup
.excl
.size
)
525 return xattrs_matches_mask (kw
, &xattrs_setup
.excl
);
528 /* Check whether the xattr with keyword KW should be discarded from list of
529 attributes that are going to be archived/excluded (set ARCHIVING=true for
530 archiving, false for excluding) */
531 static bool xattrs_masked_out (const char *kw
, bool archiving
)
533 if (!xattrs_kw_included (kw
, archiving
))
536 return xattrs_kw_excluded (kw
, archiving
);
539 void xattrs_xattrs_set (struct tar_stat_info
const *st
,
540 char const *file_name
, char typeflag
,
543 if (xattrs_option
> 0)
548 WARN ((0, 0, _("XATTR support is not available")));
553 if (!st
->xattr_map_size
)
556 for (; scan
< st
->xattr_map_size
; ++scan
)
558 char *keyword
= st
->xattr_map
[scan
].xkey
;
559 keyword
+= strlen ("SCHILY.xattr.");
561 /* TODO: this 'later_run' workaround is temporary solution -> once
562 capabilities should become fully supported by it's API and there
563 should exist something like xattrs_capabilities_set() call.
564 For a regular files: all extended attributes are restored during
565 the first run except 'security.capability' which is restored in
567 if (typeflag
== REGTYPE
568 && later_run
== !!strcmp (keyword
, "security.capability"))
571 if (xattrs_masked_out (keyword
, false /* extracting */ ))
572 /* we don't want to restore this keyword */
575 xattrs__fd_set (st
, file_name
, typeflag
, keyword
,
576 st
->xattr_map
[scan
].xval_ptr
,
577 st
->xattr_map
[scan
].xval_len
);
583 void xattrs_print_char (struct tar_stat_info
const *st
, char *output
)
586 if (verbose_option
< 2)
592 if (xattrs_option
> 0 || acls_option
> 0)
599 if (xattrs_option
> 0 && st
->xattr_map_size
)
600 for (i
= 0; i
< st
->xattr_map_size
; ++i
)
602 char *keyword
= st
->xattr_map
[i
].xkey
+ strlen ("SCHILY.xattr.");
603 if (xattrs_masked_out (keyword
, false /* like extracting */ ))
609 if (acls_option
&& (st
->acls_a_len
|| st
->acls_d_len
))
613 void xattrs_print (struct tar_stat_info
const *st
)
615 if (verbose_option
< 3)
619 if (acls_option
&& (st
->acls_a_len
|| st
->acls_d_len
))
621 fprintf (stdlis
, " a: ");
622 acls_one_line ("", ',', st
->acls_a_ptr
, st
->acls_a_len
);
623 acls_one_line ("default:", ',', st
->acls_d_ptr
, st
->acls_d_len
);
624 fprintf (stdlis
, "\n");
628 if (xattrs_option
&& st
->xattr_map_size
)
631 for (i
= 0; i
< st
->xattr_map_size
; ++i
)
633 char *keyword
= st
->xattr_map
[i
].xkey
+ strlen ("SCHILY.xattr.");
634 if (xattrs_masked_out (keyword
, false /* like extracting */ ))
636 fprintf (stdlis
, " x: %lu %s\n",
637 (unsigned long) st
->xattr_map
[i
].xval_len
, keyword
);