]> Dogcows Code - chaz/tar/blob - src/xattrs.c
Support for POSIX ACLs
[chaz/tar] / src / xattrs.c
1 /* Support for extended attributes.
2
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software
4 Foundation, Inc.
5
6 Written by James Antill, on 2006-07-27.
7
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
11 version.
12
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.
17
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. */
21
22 #include <config.h>
23 #include <system.h>
24
25 #include <fnmatch.h>
26 #include <quotearg.h>
27
28 #include "common.h"
29
30 #include "xattr-at.h"
31
32 struct xattrs_mask_map
33 {
34 const char **masks;
35 int size;
36 int used;
37 };
38
39 /* list of fnmatch patterns */
40 static struct
41 {
42 /* lists of fnmatch patterns */
43 struct xattrs_mask_map incl;
44 struct xattrs_mask_map excl;
45 } xattrs_setup;
46
47 /* disable posix acls when problem found in gnulib script m4/acl.m4 */
48 #if ! USE_ACL
49 # undef HAVE_POSIX_ACLS
50 #endif
51
52 #ifdef HAVE_POSIX_ACLS
53 # include "acl.h"
54 # include <sys/acl.h>
55 #endif
56
57 #ifdef HAVE_POSIX_ACLS
58
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 *);
63
64 /* acl_get_file_at */
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
71 #include "at-func.c"
72 #undef AT_FUNC_NAME
73 #undef AT_FUNC_F1
74 #undef AT_FUNC_RESULT
75 #undef AT_FUNC_FAIL
76 #undef AT_FUNC_POST_FILE_PARAM_DECLS
77 #undef AT_FUNC_POST_FILE_ARGS
78
79 /* acl_set_file_at */
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
84 #include "at-func.c"
85 #undef AT_FUNC_NAME
86 #undef AT_FUNC_F1
87 #undef AT_FUNC_POST_FILE_PARAM_DECLS
88 #undef AT_FUNC_POST_FILE_ARGS
89
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
95 #include "at-func.c"
96 #undef AT_FUNC_NAME
97 #undef AT_FUNC_F1
98 #undef AT_FUNC_POST_FILE_PARAM_DECLS
99 #undef AT_FUNC_POST_FILE_ARGS
100
101 /* convert unix permissions into an ACL ... needed due to "default" ACLs */
102 static acl_t perms2acl (int perms)
103 {
104 char val[] = "user::---,group::---,other::---";
105 /* 0123456789 123456789 123456789 123456789 */
106
107 /* user */
108 if (perms & 0400) val[ 6] = 'r';
109 if (perms & 0200) val[ 7] = 'w';
110 if (perms & 0100) val[ 8] = 'x';
111
112 /* group */
113 if (perms & 0040) val[17] = 'r';
114 if (perms & 0020) val[18] = 'w';
115 if (perms & 0010) val[19] = 'x';
116
117 /* other */
118 if (perms & 0004) val[28] = 'r';
119 if (perms & 0002) val[29] = 'w';
120 if (perms & 0001) val[30] = 'x';
121
122 return (acl_from_text (val));
123 }
124
125 static char *skip_to_ext_fields (char *ptr)
126 {
127 ptr += strcspn (ptr, ":,\n"); /* skip tag name. Ie. user/group/default/mask */
128
129 if (*ptr != ':')
130 return (ptr); /* error? no user/group field */
131 ++ptr;
132
133 ptr += strcspn (ptr, ":,\n"); /* skip user/group name */
134
135 if (*ptr != ':')
136 return (ptr); /* error? no perms field */
137 ++ptr;
138
139 ptr += strcspn (ptr, ":,\n"); /* skip perms */
140
141 if (*ptr != ':')
142 return (ptr); /* no extra fields */
143
144 return (ptr);
145 }
146
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)
151 {
152 char *src = (char *)ptr;
153 char *dst = (char *)ptr;
154
155 while (*src)
156 {
157 const char *old = src;
158 size_t len = 0;
159
160 src = skip_to_ext_fields (src);
161 len = src - old;
162 if (old != dst) memmove (dst, old, len);
163 dst += len;
164
165 if (*src == ':') /* We have extra fields, skip them all */
166 src += strcspn (src, "\n,");
167
168 if ((*src == '\n') || (*src == ','))
169 *dst++ = *src++; /* also done when dst == src, but that's ok */
170 }
171 if (src != dst)
172 *dst = 0;
173
174 return ptr;
175 }
176
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" */
181 acl_t acl;
182
183 if (ptr)
184 {
185 /* assert (strlen (ptr) == len); */
186 ptr = fixup_extra_acl_fields (ptr);
187
188 acl = acl_from_text (ptr);
189 acls_option = 1;
190 }
191 else if (acls_option > 0)
192 acl = perms2acl (st->stat.st_mode);
193 else
194 return; /* don't call acl functions unless we first hit an ACL, or
195 --acls was passed explicitly */
196
197 if (acl == (acl_t)NULL)
198 {
199 call_arg_warn ("acl_from_text", file_name);
200 return;
201 }
202
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));
207
208 acl_free (acl);
209 }
210
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" */
215 char *val = NULL;
216 ssize_t len;
217 acl_t acl;
218
219 if ((acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_ACCESS))
220 == (acl_t)NULL)
221 {
222 if (errno != ENOTSUP)
223 call_arg_warn ("acl_get_file_at", file_name);
224 return;
225 }
226
227 val = acl_to_text (acl, &len);
228 acl_free (acl);
229
230 if (val == NULL)
231 {
232 call_arg_warn ("acl_to_text", file_name);
233 return;
234 }
235
236 *ret_ptr = xstrdup (val);
237 *ret_len = len;
238
239 acl_free (val);
240 }
241
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" */
246 char *val = NULL;
247 ssize_t len;
248 acl_t acl;
249
250 if ((acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_DEFAULT))
251 == (acl_t)NULL)
252 {
253 if (errno != ENOTSUP)
254 call_arg_warn ("acl_get_file_at", file_name);
255 return;
256 }
257
258 val = acl_to_text (acl, &len);
259 acl_free (acl);
260
261 if (val == NULL)
262 {
263 call_arg_warn ("acl_to_text", file_name);
264 return;
265 }
266
267 *ret_ptr = xstrdup (val);
268 *ret_len = len;
269
270 acl_free (val);
271 }
272 #endif /* HAVE_POSIX_ACLS */
273
274 static void acls_one_line (const char *prefix, char delim,
275 const char *aclstring, size_t len)
276 {
277 /* support both long and short text representation of posix acls */
278 struct obstack stk;
279 obstack_init (&stk);
280 int pref_len = strlen (prefix);
281 const char *oldstring = aclstring;
282
283 if (!aclstring || !len)
284 return;
285
286 int pos = 0;
287 while (pos <= len)
288 {
289 int move = strcspn (aclstring, ",\n");
290 if (!move)
291 break;
292
293 if (oldstring != aclstring)
294 obstack_1grow (&stk, delim);
295
296 obstack_grow (&stk, prefix, pref_len);
297 obstack_grow (&stk, aclstring, move);
298
299 aclstring += move + 1;
300 }
301
302 obstack_1grow (&stk, '\0');
303 const char *toprint = obstack_finish (&stk);
304
305 fprintf (stdlis, "%s", toprint);
306
307 obstack_free (&stk, NULL);
308 }
309
310 void xattrs_acls_get (int parentfd, char const *file_name,
311 struct tar_stat_info *st, int fd, int xisfile)
312 {
313 if (acls_option > 0)
314 {
315 #ifndef HAVE_POSIX_ACLS
316 static int done = 0;
317 if (!done)
318 WARN ((0, 0, _("POSIX ACL support is not available")));
319 done = 1;
320 #else
321 int err = file_has_acl_at (parentfd, file_name, &st->stat);
322 if (err == 0)
323 return;
324 if (err == -1)
325 {
326 call_arg_warn ("file_has_acl_at", file_name);
327 return;
328 }
329
330 xattrs__acls_get_a (parentfd, file_name, st,
331 &st->acls_a_ptr, &st->acls_a_len);
332 if (!xisfile)
333 xattrs__acls_get_d (parentfd, file_name, st,
334 &st->acls_d_ptr, &st->acls_d_len);
335 #endif
336 }
337 }
338
339 void xattrs_acls_set (struct tar_stat_info const *st,
340 char const *file_name, char typeflag)
341 {
342 if ((acls_option > 0) && (typeflag != SYMTYPE))
343 {
344 #ifndef HAVE_POSIX_ACLS
345 static int done = 0;
346 if (!done)
347 WARN ((0, 0, _("POSIX ACL support is not available")));
348 done = 1;
349 #else
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);
355 #endif
356 }
357 }
358
359 static void mask_map_realloc (struct xattrs_mask_map *map)
360 {
361 if (map->size == 0)
362 {
363 map->size = 4;
364 map->masks = xmalloc (16 * sizeof (char *));
365 return;
366 }
367
368 if (map->size <= map->used)
369 {
370 map->size *= 2;
371 map->masks = xrealloc (map->masks, map->size * sizeof (char *));
372 return;
373 }
374 }
375
376 void xattrs_mask_add (const char *mask, bool incl)
377 {
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;
385 }
386
387 static void clear_mask_map (struct xattrs_mask_map *mask_map)
388 {
389 if (mask_map->size)
390 free (mask_map->masks);
391 }
392
393 void xattrs_clear_setup ()
394 {
395 clear_mask_map (&xattrs_setup.incl);
396 clear_mask_map (&xattrs_setup.excl);
397 }
398
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)
403 {
404 if (xattrs_option > 0)
405 {
406 #ifndef HAVE_XATTRS
407 static int done = 0;
408 if (!done)
409 WARN ((0, 0, _("XATTR support is not available")));
410 done = 1;
411 #else
412 static ssize_t xsz = 1024;
413 static char *xatrs = NULL;
414 ssize_t xret = -1;
415
416 if (!xatrs) xatrs = xmalloc (xsz);
417
418 while (((fd == 0) ?
419 ((xret = llistxattrat (parentfd, file_name, xatrs, xsz)) == -1) :
420 ((xret = flistxattr (fd, xatrs, xsz)) == -1)) &&
421 (errno == ERANGE))
422 {
423 xsz <<= 1;
424 xatrs = xrealloc (xatrs, xsz);
425 }
426
427 if (xret == -1)
428 call_arg_warn ((fd == 0) ? "llistxattrat" : "flistxattr", file_name);
429 else
430 {
431 const char *attr = xatrs;
432 static ssize_t asz = 1024;
433 static char *val = NULL;
434
435 if (!val) val = xmalloc (asz);
436
437 while (xret > 0)
438 {
439 size_t len = strlen (attr);
440 ssize_t aret = 0;
441
442 /* Archive all xattrs during creation, decide at extraction time
443 * which ones are of interest/use for the target filesystem. */
444 while (((fd == 0)
445 ? ((aret = lgetxattrat (parentfd, file_name, attr,
446 val, asz)) == -1)
447 : ((aret = fgetxattr (fd, attr, val, asz)) == -1))
448 && (errno == ERANGE))
449 {
450 asz <<= 1;
451 val = xrealloc (val, asz);
452 }
453
454 if (aret != -1)
455 xheader_xattr_add (st, attr, val, aret);
456 else if (errno != ENOATTR)
457 call_arg_warn ((fd == 0) ? "lgetxattrat"
458 : "fgetxattr", file_name);
459
460 attr += len + 1;
461 xret -= len + 1;
462 }
463 }
464 #endif
465 }
466 }
467
468 static void xattrs__fd_set (struct tar_stat_info const *st,
469 char const *file_name, char typeflag,
470 const char *attr,
471 const char *ptr, size_t len)
472 {
473 if (ptr)
474 {
475 const char *sysname = "setxattrat";
476 int ret = -1;
477
478 if (typeflag != SYMTYPE)
479 ret = setxattrat (chdir_fd, file_name, attr, ptr, len, 0);
480 else
481 {
482 sysname = "lsetxattr";
483 ret = lsetxattrat (chdir_fd, file_name, attr, ptr, len, 0);
484 }
485
486 if (ret == -1)
487 WARNOPT (WARN_XATTR_WRITE, (0, errno,
488 _("%s: Cannot set '%s' extended attribute for file '%s'"),
489 sysname, attr, file_name));
490 }
491 }
492
493 static bool xattrs_matches_mask (const char *kw, struct xattrs_mask_map *mm)
494 {
495 int i;
496
497 if (!mm->size)
498 return false;
499
500 for (i = 0; i < mm->used; i++)
501 if (fnmatch (mm->masks[i], kw, 0) == 0)
502 return true;
503
504 return false;
505 }
506
507 static bool xattrs_kw_included (const char *kw, bool archiving)
508 {
509 if (xattrs_setup.incl.size)
510 return xattrs_matches_mask (kw, &xattrs_setup.incl);
511 else
512 {
513 if (archiving)
514 return true;
515 else
516 return strncmp (kw, "user.", strlen ("user.")) == 0;
517 }
518 }
519
520 static bool xattrs_kw_excluded (const char *kw, bool archiving)
521 {
522 if (!xattrs_setup.excl.size)
523 return false;
524
525 return xattrs_matches_mask (kw, &xattrs_setup.excl);
526 }
527
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)
532 {
533 if (!xattrs_kw_included (kw, archiving))
534 return true;
535
536 return xattrs_kw_excluded (kw, archiving);
537 }
538
539 void xattrs_xattrs_set (struct tar_stat_info const *st,
540 char const *file_name, char typeflag,
541 int later_run)
542 {
543 if (xattrs_option > 0)
544 {
545 #ifndef HAVE_XATTRS
546 static int done = 0;
547 if (!done)
548 WARN ((0, 0, _("XATTR support is not available")));
549 done = 1;
550 #else
551 size_t scan = 0;
552
553 if (!st->xattr_map_size)
554 return;
555
556 for (; scan < st->xattr_map_size; ++scan)
557 {
558 char *keyword = st->xattr_map[scan].xkey;
559 keyword += strlen ("SCHILY.xattr.");
560
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
566 'later_run == 1'. */
567 if (typeflag == REGTYPE
568 && later_run == !!strcmp (keyword, "security.capability"))
569 continue;
570
571 if (xattrs_masked_out (keyword, false /* extracting */ ))
572 /* we don't want to restore this keyword */
573 continue;
574
575 xattrs__fd_set (st, file_name, typeflag, keyword,
576 st->xattr_map[scan].xval_ptr,
577 st->xattr_map[scan].xval_len);
578 }
579 #endif
580 }
581 }
582
583 void xattrs_print_char (struct tar_stat_info const *st, char *output)
584 {
585 int i;
586 if (verbose_option < 2)
587 {
588 *output = 0;
589 return;
590 }
591
592 if (xattrs_option > 0 || acls_option > 0)
593 {
594 /* placeholders */
595 *output = ' ';
596 *(output + 1) = 0;
597 }
598
599 if (xattrs_option > 0 && st->xattr_map_size)
600 for (i = 0; i < st->xattr_map_size; ++i)
601 {
602 char *keyword = st->xattr_map[i].xkey + strlen ("SCHILY.xattr.");
603 if (xattrs_masked_out (keyword, false /* like extracting */ ))
604 continue;
605 *output = '*';
606 break;
607 }
608
609 if (acls_option && (st->acls_a_len || st->acls_d_len))
610 *output = '+';
611 }
612
613 void xattrs_print (struct tar_stat_info const *st)
614 {
615 if (verbose_option < 3)
616 return;
617
618 /* acls */
619 if (acls_option && (st->acls_a_len || st->acls_d_len))
620 {
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");
625 }
626
627 /* xattrs */
628 if (xattrs_option && st->xattr_map_size)
629 {
630 int i;
631 for (i = 0; i < st->xattr_map_size; ++i)
632 {
633 char *keyword = st->xattr_map[i].xkey + strlen ("SCHILY.xattr.");
634 if (xattrs_masked_out (keyword, false /* like extracting */ ))
635 continue;
636 fprintf (stdlis, " x: %lu %s\n",
637 (unsigned long) st->xattr_map[i].xval_len, keyword);
638 }
639 }
640 }
This page took 0.062259 seconds and 5 git commands to generate.