Minor change.
[tar.git] / src / xattrs.c
blobabe7b589b6b00c3e40a13fba7ccb35b2fdc83cae
1 /* Support for extended attributes.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software
4 Foundation, Inc.
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 3, or (at your option) any later
11 version.
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. */
22 #include <config.h>
23 #include <system.h>
25 #include <fnmatch.h>
26 #include <quotearg.h>
28 #include "common.h"
30 #include "xattr-at.h"
31 #include "selinux-at.h"
33 struct xattrs_mask_map
35 const char **masks;
36 int size;
37 int used;
40 /* list of fnmatch patterns */
41 static struct
43 /* lists of fnmatch patterns */
44 struct xattrs_mask_map incl;
45 struct xattrs_mask_map excl;
46 } xattrs_setup;
48 /* disable posix acls when problem found in gnulib script m4/acl.m4 */
49 #if ! USE_ACL
50 # undef HAVE_POSIX_ACLS
51 #endif
53 #ifdef HAVE_POSIX_ACLS
54 # include "acl.h"
55 # include <sys/acl.h>
56 #endif
58 #ifdef HAVE_POSIX_ACLS
60 /* acl-at wrappers, TODO: move to gnulib in future? */
61 acl_t acl_get_file_at (int dirfd, const char *file, acl_type_t type);
62 int acl_set_file_at (int dirfd, const char *file, acl_type_t type, acl_t acl);
63 int file_has_acl_at (int dirfd, char const *, struct stat const *);
65 /* acl_get_file_at */
66 #define AT_FUNC_NAME acl_get_file_at
67 #define AT_FUNC_RESULT acl_t
68 #define AT_FUNC_FAIL (acl_t)NULL
69 #define AT_FUNC_F1 acl_get_file
70 #define AT_FUNC_POST_FILE_PARAM_DECLS , acl_type_t type
71 #define AT_FUNC_POST_FILE_ARGS , type
72 #include "at-func.c"
73 #undef AT_FUNC_NAME
74 #undef AT_FUNC_F1
75 #undef AT_FUNC_RESULT
76 #undef AT_FUNC_FAIL
77 #undef AT_FUNC_POST_FILE_PARAM_DECLS
78 #undef AT_FUNC_POST_FILE_ARGS
80 /* acl_set_file_at */
81 #define AT_FUNC_NAME acl_set_file_at
82 #define AT_FUNC_F1 acl_set_file
83 #define AT_FUNC_POST_FILE_PARAM_DECLS , acl_type_t type, acl_t acl
84 #define AT_FUNC_POST_FILE_ARGS , type, acl
85 #include "at-func.c"
86 #undef AT_FUNC_NAME
87 #undef AT_FUNC_F1
88 #undef AT_FUNC_POST_FILE_PARAM_DECLS
89 #undef AT_FUNC_POST_FILE_ARGS
91 /* gnulib file_has_acl_at */
92 #define AT_FUNC_NAME file_has_acl_at
93 #define AT_FUNC_F1 file_has_acl
94 #define AT_FUNC_POST_FILE_PARAM_DECLS , struct stat const *st
95 #define AT_FUNC_POST_FILE_ARGS , st
96 #include "at-func.c"
97 #undef AT_FUNC_NAME
98 #undef AT_FUNC_F1
99 #undef AT_FUNC_POST_FILE_PARAM_DECLS
100 #undef AT_FUNC_POST_FILE_ARGS
102 /* convert unix permissions into an ACL ... needed due to "default" ACLs */
103 static acl_t
104 perms2acl (int perms)
106 char val[] = "user::---,group::---,other::---";
107 /* 0123456789 123456789 123456789 123456789 */
109 /* user */
110 if (perms & 0400)
111 val[6] = 'r';
112 if (perms & 0200)
113 val[7] = 'w';
114 if (perms & 0100)
115 val[8] = 'x';
117 /* group */
118 if (perms & 0040)
119 val[17] = 'r';
120 if (perms & 0020)
121 val[18] = 'w';
122 if (perms & 0010)
123 val[19] = 'x';
125 /* other */
126 if (perms & 0004)
127 val[28] = 'r';
128 if (perms & 0002)
129 val[29] = 'w';
130 if (perms & 0001)
131 val[30] = 'x';
133 return acl_from_text (val);
136 static char *
137 skip_to_ext_fields (char *ptr)
139 ptr += strcspn (ptr, ":,\n"); /* skip tag name. Ie. user/group/default/mask */
141 if (*ptr != ':')
142 return ptr; /* error? no user/group field */
143 ++ptr;
145 ptr += strcspn (ptr, ":,\n"); /* skip user/group name */
147 if (*ptr != ':')
148 return ptr; /* error? no perms field */
149 ++ptr;
151 ptr += strcspn (ptr, ":,\n"); /* skip perms */
153 if (*ptr != ':')
154 return ptr; /* no extra fields */
156 return ptr;
159 /* The POSIX draft allows extra fields after the three main ones. Star
160 uses this to add a fourth field for user/group which is the numeric ID.
161 We just skip all extra fields atm. */
162 static const char *
163 fixup_extra_acl_fields (const char *ptr)
165 char *src = (char *) ptr;
166 char *dst = (char *) ptr;
168 while (*src)
170 const char *old = src;
171 size_t len = 0;
173 src = skip_to_ext_fields (src);
174 len = src - old;
175 if (old != dst)
176 memmove (dst, old, len);
177 dst += len;
179 if (*src == ':') /* We have extra fields, skip them all */
180 src += strcspn (src, "\n,");
182 if ((*src == '\n') || (*src == ','))
183 *dst++ = *src++; /* also done when dst == src, but that's ok */
185 if (src != dst)
186 *dst = 0;
188 return ptr;
191 static void
192 xattrs__acls_set (struct tar_stat_info const *st,
193 char const *file_name, int type,
194 const char *ptr, size_t len, bool def)
195 { /* "system.posix_acl_access" */
196 acl_t acl;
198 if (ptr)
200 /* assert (strlen (ptr) == len); */
201 ptr = fixup_extra_acl_fields (ptr);
203 acl = acl_from_text (ptr);
204 acls_option = 1;
206 else if (acls_option > 0)
207 acl = perms2acl (st->stat.st_mode);
208 else
209 return; /* don't call acl functions unless we first hit an ACL, or
210 --acls was passed explicitly */
212 if (acl == (acl_t) NULL)
214 call_arg_warn ("acl_from_text", file_name);
215 return;
218 if (acl_set_file_at (chdir_fd, file_name, type, acl) == -1)
219 /* warn even if filesystem does not support acls */
220 WARNOPT (WARN_XATTR_WRITE,
221 (0, errno,
222 _("acl_set_file_at: Cannot set POSIX ACLs for file '%s'"),
223 file_name));
225 acl_free (acl);
228 static void
229 xattrs__acls_get_a (int parentfd, const char *file_name,
230 struct tar_stat_info *st,
231 char **ret_ptr, size_t * ret_len)
232 { /* "system.posix_acl_access" */
233 char *val = NULL;
234 ssize_t len;
235 acl_t acl;
237 if ((acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_ACCESS))
238 == (acl_t) NULL)
240 if (errno != ENOTSUP)
241 call_arg_warn ("acl_get_file_at", file_name);
242 return;
245 val = acl_to_text (acl, &len);
246 acl_free (acl);
248 if (val == NULL)
250 call_arg_warn ("acl_to_text", file_name);
251 return;
254 *ret_ptr = xstrdup (val);
255 *ret_len = len;
257 acl_free (val);
260 static void
261 xattrs__acls_get_d (int parentfd, char const *file_name,
262 struct tar_stat_info *st,
263 char **ret_ptr, size_t * ret_len)
264 { /* "system.posix_acl_default" */
265 char *val = NULL;
266 ssize_t len;
267 acl_t acl;
269 if ((acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_DEFAULT))
270 == (acl_t) NULL)
272 if (errno != ENOTSUP)
273 call_arg_warn ("acl_get_file_at", file_name);
274 return;
277 val = acl_to_text (acl, &len);
278 acl_free (acl);
280 if (val == NULL)
282 call_arg_warn ("acl_to_text", file_name);
283 return;
286 *ret_ptr = xstrdup (val);
287 *ret_len = len;
289 acl_free (val);
291 #endif /* HAVE_POSIX_ACLS */
293 static void
294 acls_one_line (const char *prefix, char delim,
295 const char *aclstring, size_t len)
297 /* support both long and short text representation of posix acls */
298 struct obstack stk;
299 obstack_init (&stk);
300 int pref_len = strlen (prefix);
301 const char *oldstring = aclstring;
303 if (!aclstring || !len)
304 return;
306 int pos = 0;
307 while (pos <= len)
309 int move = strcspn (aclstring, ",\n");
310 if (!move)
311 break;
313 if (oldstring != aclstring)
314 obstack_1grow (&stk, delim);
316 obstack_grow (&stk, prefix, pref_len);
317 obstack_grow (&stk, aclstring, move);
319 aclstring += move + 1;
322 obstack_1grow (&stk, '\0');
323 const char *toprint = obstack_finish (&stk);
325 fprintf (stdlis, "%s", toprint);
327 obstack_free (&stk, NULL);
330 void
331 xattrs_acls_get (int parentfd, char const *file_name,
332 struct tar_stat_info *st, int fd, int xisfile)
334 if (acls_option > 0)
336 #ifndef HAVE_POSIX_ACLS
337 static int done = 0;
338 if (!done)
339 WARN ((0, 0, _("POSIX ACL support is not available")));
340 done = 1;
341 #else
342 int err = file_has_acl_at (parentfd, file_name, &st->stat);
343 if (err == 0)
344 return;
345 if (err == -1)
347 call_arg_warn ("file_has_acl_at", file_name);
348 return;
351 xattrs__acls_get_a (parentfd, file_name, st,
352 &st->acls_a_ptr, &st->acls_a_len);
353 if (!xisfile)
354 xattrs__acls_get_d (parentfd, file_name, st,
355 &st->acls_d_ptr, &st->acls_d_len);
356 #endif
360 void
361 xattrs_acls_set (struct tar_stat_info const *st,
362 char const *file_name, char typeflag)
364 if ((acls_option > 0) && (typeflag != SYMTYPE))
366 #ifndef HAVE_POSIX_ACLS
367 static int done = 0;
368 if (!done)
369 WARN ((0, 0, _("POSIX ACL support is not available")));
370 done = 1;
371 #else
372 xattrs__acls_set (st, file_name, ACL_TYPE_ACCESS,
373 st->acls_a_ptr, st->acls_a_len, false);
374 if ((typeflag == DIRTYPE) || (typeflag == GNUTYPE_DUMPDIR))
375 xattrs__acls_set (st, file_name, ACL_TYPE_DEFAULT,
376 st->acls_d_ptr, st->acls_d_len, true);
377 #endif
381 static void
382 mask_map_realloc (struct xattrs_mask_map *map)
384 if (map->size == 0)
386 map->size = 4;
387 map->masks = xmalloc (16 * sizeof (char *));
388 return;
391 if (map->size <= map->used)
393 map->size *= 2;
394 map->masks = xrealloc (map->masks, map->size * sizeof (char *));
395 return;
399 void
400 xattrs_mask_add (const char *mask, bool incl)
402 struct xattrs_mask_map *mask_map = incl ? &xattrs_setup.incl
403 : &xattrs_setup.excl;
404 /* ensure there is enough space */
405 mask_map_realloc (mask_map);
406 /* just assign pointers -- we silently expect that pointer "mask" is valid
407 through the whole program (pointer to argv array) */
408 mask_map->masks[mask_map->used++] = mask;
411 static void
412 clear_mask_map (struct xattrs_mask_map *mask_map)
414 if (mask_map->size)
415 free (mask_map->masks);
418 void
419 xattrs_clear_setup ()
421 clear_mask_map (&xattrs_setup.incl);
422 clear_mask_map (&xattrs_setup.excl);
425 /* get all xattrs from file given by FILE_NAME or FD (when non-zero). This
426 includes all the user.*, security.*, system.*, etc. available domains */
427 void
428 xattrs_xattrs_get (int parentfd, char const *file_name,
429 struct tar_stat_info *st, int fd)
431 if (xattrs_option > 0)
433 #ifndef HAVE_XATTRS
434 static int done = 0;
435 if (!done)
436 WARN ((0, 0, _("XATTR support is not available")));
437 done = 1;
438 #else
439 static ssize_t xsz = 1024;
440 static char *xatrs = NULL;
441 ssize_t xret = -1;
443 if (!xatrs)
444 xatrs = xmalloc (xsz);
446 while (((fd == 0) ?
447 ((xret =
448 llistxattrat (parentfd, file_name, xatrs, xsz)) == -1) :
449 ((xret = flistxattr (fd, xatrs, xsz)) == -1))
450 && (errno == ERANGE))
452 xsz <<= 1;
453 xatrs = xrealloc (xatrs, xsz);
456 if (xret == -1)
457 call_arg_warn ((fd == 0) ? "llistxattrat" : "flistxattr", file_name);
458 else
460 const char *attr = xatrs;
461 static ssize_t asz = 1024;
462 static char *val = NULL;
464 if (!val)
465 val = xmalloc (asz);
467 while (xret > 0)
469 size_t len = strlen (attr);
470 ssize_t aret = 0;
472 /* Archive all xattrs during creation, decide at extraction time
473 * which ones are of interest/use for the target filesystem. */
474 while (((fd == 0)
475 ? ((aret = lgetxattrat (parentfd, file_name, attr,
476 val, asz)) == -1)
477 : ((aret = fgetxattr (fd, attr, val, asz)) == -1))
478 && (errno == ERANGE))
480 asz <<= 1;
481 val = xrealloc (val, asz);
484 if (aret != -1)
485 xheader_xattr_add (st, attr, val, aret);
486 else if (errno != ENOATTR)
487 call_arg_warn ((fd == 0) ? "lgetxattrat"
488 : "fgetxattr", file_name);
490 attr += len + 1;
491 xret -= len + 1;
494 #endif
498 static void
499 xattrs__fd_set (struct tar_stat_info const *st,
500 char const *file_name, char typeflag,
501 const char *attr, const char *ptr, size_t len)
503 if (ptr)
505 const char *sysname = "setxattrat";
506 int ret = -1;
508 if (typeflag != SYMTYPE)
509 ret = setxattrat (chdir_fd, file_name, attr, ptr, len, 0);
510 else
512 sysname = "lsetxattr";
513 ret = lsetxattrat (chdir_fd, file_name, attr, ptr, len, 0);
516 if (ret == -1)
517 WARNOPT (WARN_XATTR_WRITE,
518 (0, errno,
519 _("%s: Cannot set '%s' extended attribute for file '%s'"),
520 sysname, attr, file_name));
524 /* lgetfileconat is called against FILE_NAME iff the FD parameter is set to
525 zero, otherwise the fgetfileconat is used against correct file descriptor */
526 void
527 xattrs_selinux_get (int parentfd, char const *file_name,
528 struct tar_stat_info *st, int fd)
530 if (selinux_context_option > 0)
532 #if HAVE_SELINUX_SELINUX_H != 1
533 static int done = 0;
534 if (!done)
535 WARN ((0, 0, _("SELinux support is not available")));
536 done = 1;
537 #else
538 int result = (fd ? fgetfilecon (fd, &st->cntx_name)
539 : lgetfileconat (parentfd, file_name, &st->cntx_name));
541 if (result == -1 && errno != ENODATA && errno != ENOTSUP)
542 call_arg_warn (fd ? "fgetfilecon" : "lgetfileconat", file_name);
543 #endif
547 void
548 xattrs_selinux_set (struct tar_stat_info const *st,
549 char const *file_name, char typeflag)
551 if (selinux_context_option > 0)
553 #if HAVE_SELINUX_SELINUX_H != 1
554 static int done = 0;
555 if (!done)
556 WARN ((0, 0, _("SELinux support is not available")));
557 done = 1;
558 #else
559 const char *sysname = "setfilecon";
560 int ret;
562 if (!st->cntx_name)
563 return;
565 if (typeflag != SYMTYPE)
567 ret = setfileconat (chdir_fd, file_name, st->cntx_name);
568 sysname = "setfileconat";
570 else
572 ret = lsetfileconat (chdir_fd, file_name, st->cntx_name);
573 sysname = "lsetfileconat";
576 if (ret == -1)
577 WARNOPT (WARN_XATTR_WRITE,
578 (0, errno,
579 _("%s: Cannot set SELinux context for file '%s'"),
580 sysname, file_name));
581 #endif
585 static bool
586 xattrs_matches_mask (const char *kw, struct xattrs_mask_map *mm)
588 int i;
590 if (!mm->size)
591 return false;
593 for (i = 0; i < mm->used; i++)
594 if (fnmatch (mm->masks[i], kw, 0) == 0)
595 return true;
597 return false;
600 static bool
601 xattrs_kw_included (const char *kw, bool archiving)
603 if (xattrs_setup.incl.size)
604 return xattrs_matches_mask (kw, &xattrs_setup.incl);
605 else
607 if (archiving)
608 return true;
609 else
610 return strncmp (kw, "user.", strlen ("user.")) == 0;
614 static bool
615 xattrs_kw_excluded (const char *kw, bool archiving)
617 if (!xattrs_setup.excl.size)
618 return false;
620 return xattrs_matches_mask (kw, &xattrs_setup.excl);
623 /* Check whether the xattr with keyword KW should be discarded from list of
624 attributes that are going to be archived/excluded (set ARCHIVING=true for
625 archiving, false for excluding) */
626 static bool
627 xattrs_masked_out (const char *kw, bool archiving)
629 if (!xattrs_kw_included (kw, archiving))
630 return true;
632 return xattrs_kw_excluded (kw, archiving);
635 void
636 xattrs_xattrs_set (struct tar_stat_info const *st,
637 char const *file_name, char typeflag, int later_run)
639 if (xattrs_option > 0)
641 #ifndef HAVE_XATTRS
642 static int done = 0;
643 if (!done)
644 WARN ((0, 0, _("XATTR support is not available")));
645 done = 1;
646 #else
647 size_t scan = 0;
649 if (!st->xattr_map_size)
650 return;
652 for (; scan < st->xattr_map_size; ++scan)
654 char *keyword = st->xattr_map[scan].xkey;
655 keyword += strlen ("SCHILY.xattr.");
657 /* TODO: this 'later_run' workaround is temporary solution -> once
658 capabilities should become fully supported by it's API and there
659 should exist something like xattrs_capabilities_set() call.
660 For a regular files: all extended attributes are restored during
661 the first run except 'security.capability' which is restored in
662 'later_run == 1'. */
663 if (typeflag == REGTYPE
664 && later_run == !!strcmp (keyword, "security.capability"))
665 continue;
667 if (xattrs_masked_out (keyword, false /* extracting */ ))
668 /* we don't want to restore this keyword */
669 continue;
671 xattrs__fd_set (st, file_name, typeflag, keyword,
672 st->xattr_map[scan].xval_ptr,
673 st->xattr_map[scan].xval_len);
675 #endif
679 void
680 xattrs_print_char (struct tar_stat_info const *st, char *output)
682 int i;
684 if (verbose_option < 2)
686 *output = 0;
687 return;
690 if (xattrs_option > 0 || selinux_context_option > 0 || acls_option > 0)
692 /* placeholders */
693 *output = ' ';
694 *(output + 1) = 0;
697 if (xattrs_option > 0 && st->xattr_map_size)
698 for (i = 0; i < st->xattr_map_size; ++i)
700 char *keyword = st->xattr_map[i].xkey + strlen ("SCHILY.xattr.");
701 if (xattrs_masked_out (keyword, false /* like extracting */ ))
702 continue;
703 *output = '*';
704 break;
707 if (selinux_context_option > 0 && st->cntx_name)
708 *output = '.';
710 if (acls_option && (st->acls_a_len || st->acls_d_len))
711 *output = '+';
714 void
715 xattrs_print (struct tar_stat_info const *st)
717 if (verbose_option < 3)
718 return;
720 /* selinux */
721 if (selinux_context_option && st->cntx_name)
722 fprintf (stdlis, " s: %s\n", st->cntx_name);
724 /* acls */
725 if (acls_option && (st->acls_a_len || st->acls_d_len))
727 fprintf (stdlis, " a: ");
728 acls_one_line ("", ',', st->acls_a_ptr, st->acls_a_len);
729 acls_one_line ("default:", ',', st->acls_d_ptr, st->acls_d_len);
730 fprintf (stdlis, "\n");
733 /* xattrs */
734 if (xattrs_option && st->xattr_map_size)
736 int i;
737 for (i = 0; i < st->xattr_map_size; ++i)
739 char *keyword = st->xattr_map[i].xkey + strlen ("SCHILY.xattr.");
740 if (xattrs_masked_out (keyword, false /* like extracting */ ))
741 continue;
742 fprintf (stdlis, " x: %lu %s\n",
743 (unsigned long) st->xattr_map[i].xval_len, keyword);