blob: 8896764f259282b606a908d4bce8dbedaae56d35 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2** Copyright 1998-2003 University of Illinois Board of Trustees
3** Copyright 1998-2003 Mark D. Roth
4** All rights reserved.
5**
6** append.c - libtar code to append files to a tar archive
7**
8** Mark D. Roth <roth@uiuc.edu>
9** Campus Information Technologies and Educational Services
10** University of Illinois at Urbana-Champaign
11*/
12
13#include <internal.h>
14
15#include <stdio.h>
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050016#include <stdlib.h>
17#include <string.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050018#include <errno.h>
19#include <fcntl.h>
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050020#include <time.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021#include <sys/param.h>
22#include <sys/types.h>
Ethan Yonker79f88bd2016-12-09 14:52:12 -060023#include <stdbool.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050024
Ethan Yonker71187742017-01-13 13:30:10 -060025#include <sys/capability.h>
26#include <sys/xattr.h>
27#include <linux/xattr.h>
28
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#ifdef STDC_HEADERS
30# include <stdlib.h>
31# include <string.h>
32#endif
33
34#ifdef HAVE_UNISTD_H
35# include <unistd.h>
36#endif
37
Matt Mower87413642017-01-17 21:14:46 -060038#include <selinux/selinux.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050039
Ethan Yonker79f88bd2016-12-09 14:52:12 -060040#ifdef HAVE_EXT4_CRYPT
41# include "ext4crypt_tar.h"
42#endif
Ethan Yonker8d039f72017-02-03 14:26:15 -060043#include "android_utils.h"
Ethan Yonker79f88bd2016-12-09 14:52:12 -060044
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045struct tar_dev
46{
47 dev_t td_dev;
48 libtar_hash_t *td_h;
49};
50typedef struct tar_dev tar_dev_t;
51
52struct tar_ino
53{
54 ino_t ti_ino;
55 char ti_name[MAXPATHLEN];
56};
57typedef struct tar_ino tar_ino_t;
58
59
60/* free memory associated with a tar_dev_t */
61void
62tar_dev_free(tar_dev_t *tdp)
63{
64 libtar_hash_free(tdp->td_h, free);
65 free(tdp);
66}
67
68
69/* appends a file to the tar archive */
70int
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050071tar_append_file(TAR *t, const char *realname, const char *savename)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050072{
73 struct stat s;
74 int i;
75 libtar_hashptr_t hp;
76 tar_dev_t *td = NULL;
77 tar_ino_t *ti = NULL;
78 char path[MAXPATHLEN];
79
80#ifdef DEBUG
81 printf("==> tar_append_file(TAR=0x%lx (\"%s\"), realname=\"%s\", "
82 "savename=\"%s\")\n", t, t->pathname, realname,
83 (savename ? savename : "[NULL]"));
84#endif
85
86 if (lstat(realname, &s) != 0)
87 {
88#ifdef DEBUG
89 perror("lstat()");
90#endif
91 return -1;
92 }
93
94 /* set header block */
95#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050096 puts("tar_append_file(): setting header block...");
bigbiff bigbiff9c754052013-01-09 09:09:08 -050097#endif
98 memset(&(t->th_buf), 0, sizeof(struct tar_header));
99 th_set_from_stat(t, &s);
100
101 /* set the header path */
102#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500103 puts("tar_append_file(): setting header path...");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500104#endif
105 th_set_path(t, (savename ? savename : realname));
106
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200107 /* get selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500108 if (t->options & TAR_STORE_SELINUX)
109 {
110 if (t->th_buf.selinux_context != NULL)
111 {
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200112 free(t->th_buf.selinux_context);
113 t->th_buf.selinux_context = NULL;
114 }
115
116 security_context_t selinux_context = NULL;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500117 if (lgetfilecon(realname, &selinux_context) >= 0)
118 {
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200119 t->th_buf.selinux_context = strdup(selinux_context);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500120 printf(" ==> set selinux context: %s\n", selinux_context);
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200121 freecon(selinux_context);
122 }
123 else
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500124 {
125#ifdef DEBUG
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200126 perror("Failed to get selinux context");
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500127#endif
128 }
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200129 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500130
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600131#ifdef HAVE_EXT4_CRYPT
132 if (TH_ISDIR(t) && t->options & TAR_STORE_EXT4_POL)
133 {
134 if (t->th_buf.e4crypt_policy != NULL)
135 {
136 free(t->th_buf.e4crypt_policy);
137 t->th_buf.e4crypt_policy = NULL;
138 }
139
140 char e4crypt_policy[EXT4_KEY_DESCRIPTOR_SIZE];
141 if (e4crypt_policy_get(realname, e4crypt_policy, EXT4_KEY_DESCRIPTOR_SIZE, 0))
142 {
143 char tar_policy[EXT4_KEY_DESCRIPTOR_SIZE];
144 memset(tar_policy, 0, sizeof(tar_policy));
145 char policy_hex[EXT4_KEY_DESCRIPTOR_HEX];
146 policy_to_hex(e4crypt_policy, policy_hex);
147 if (lookup_ref_key(e4crypt_policy, &tar_policy)) {
148 printf("found policy '%s' - '%s' - '%s'\n", realname, tar_policy, policy_hex);
149 t->th_buf.e4crypt_policy = strdup(tar_policy);
150 } else {
151 printf("failed to lookup tar policy for '%s' - '%s'\n", realname, policy_hex);
152 return -1;
153 }
154 } // else no policy found, but this is not an error as not all dirs will have a policy
155 }
156#endif
157
Ethan Yonker71187742017-01-13 13:30:10 -0600158 /* get posix file capabilities */
159 if (TH_ISREG(t) && t->options & TAR_STORE_POSIX_CAP)
160 {
161 if (t->th_buf.has_cap_data)
162 {
163 memset(&t->th_buf.cap_data, 0, sizeof(struct vfs_cap_data));
164 t->th_buf.has_cap_data = 0;
165 }
166
167 if (getxattr(realname, XATTR_NAME_CAPS, &t->th_buf.cap_data, sizeof(struct vfs_cap_data)) >= 0)
168 {
169 t->th_buf.has_cap_data = 1;
170#if 1 //def DEBUG
171 print_caps(&t->th_buf.cap_data);
172#endif
173 }
174 }
175
Ethan Yonker8d039f72017-02-03 14:26:15 -0600176 /* get android user.default xattr */
177 if (TH_ISDIR(t) && t->options & TAR_STORE_ANDROID_USER_XATTR)
178 {
179 if (getxattr(realname, "user.default", NULL, 0) >= 0)
180 {
181 t->th_buf.has_user_default = 1;
182#if 1 //def DEBUG
183 printf("storing xattr user.default\n");
184#endif
185 }
186 if (getxattr(realname, "user.inode_cache", NULL, 0) >= 0)
187 {
188 t->th_buf.has_user_cache = 1;
189#if 1 //def DEBUG
190 printf("storing xattr user.inode_cache\n");
191#endif
192 }
193 if (getxattr(realname, "user.inode_code_cache", NULL, 0) >= 0)
194 {
195 t->th_buf.has_user_code_cache = 1;
196#if 1 //def DEBUG
197 printf("storing xattr user.inode_code_cache\n");
198#endif
199 }
200 }
201
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500202 /* check if it's a hardlink */
203#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500204 puts("tar_append_file(): checking inode cache for hardlink...");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500205#endif
206 libtar_hashptr_reset(&hp);
207 if (libtar_hash_getkey(t->h, &hp, &(s.st_dev),
208 (libtar_matchfunc_t)dev_match) != 0)
209 td = (tar_dev_t *)libtar_hashptr_data(&hp);
210 else
211 {
212#ifdef DEBUG
213 printf("+++ adding hash for device (0x%lx, 0x%lx)...\n",
214 major(s.st_dev), minor(s.st_dev));
215#endif
216 td = (tar_dev_t *)calloc(1, sizeof(tar_dev_t));
217 td->td_dev = s.st_dev;
218 td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash);
219 if (td->td_h == NULL)
220 return -1;
221 if (libtar_hash_add(t->h, td) == -1)
222 return -1;
223 }
224 libtar_hashptr_reset(&hp);
225 if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino),
226 (libtar_matchfunc_t)ino_match) != 0)
227 {
228 ti = (tar_ino_t *)libtar_hashptr_data(&hp);
229#ifdef DEBUG
230 printf(" tar_append_file(): encoding hard link \"%s\" "
231 "to \"%s\"...\n", realname, ti->ti_name);
232#endif
233 t->th_buf.typeflag = LNKTYPE;
234 th_set_link(t, ti->ti_name);
235 }
236 else
237 {
238#ifdef DEBUG
239 printf("+++ adding entry: device (0x%lx,0x%lx), inode %ld "
240 "(\"%s\")...\n", major(s.st_dev), minor(s.st_dev),
241 s.st_ino, realname);
242#endif
243 ti = (tar_ino_t *)calloc(1, sizeof(tar_ino_t));
244 if (ti == NULL)
245 return -1;
246 ti->ti_ino = s.st_ino;
247 snprintf(ti->ti_name, sizeof(ti->ti_name), "%s",
248 savename ? savename : realname);
249 libtar_hash_add(td->td_h, ti);
250 }
251
252 /* check if it's a symlink */
253 if (TH_ISSYM(t))
254 {
255 i = readlink(realname, path, sizeof(path));
256 if (i == -1)
257 return -1;
258 if (i >= MAXPATHLEN)
259 i = MAXPATHLEN - 1;
260 path[i] = '\0';
261#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500262 printf("tar_append_file(): encoding symlink \"%s\" -> "
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500263 "\"%s\"...\n", realname, path);
264#endif
265 th_set_link(t, path);
266 }
267
268 /* print file info */
269 if (t->options & TAR_VERBOSE)
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500270 printf("%s\n", th_get_pathname(t));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500271
272#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500273 puts("tar_append_file(): writing header");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500274#endif
275 /* write header */
276 if (th_write(t) != 0)
277 {
278#ifdef DEBUG
279 printf("t->fd = %d\n", t->fd);
280#endif
281 return -1;
282 }
283#ifdef DEBUG
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500284 puts("tar_append_file(): back from th_write()");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500285#endif
286
287 /* if it's a regular file, write the contents as well */
288 if (TH_ISREG(t) && tar_append_regfile(t, realname) != 0)
289 return -1;
290
291 return 0;
292}
293
294
295/* write EOF indicator */
296int
297tar_append_eof(TAR *t)
298{
299 int i, j;
300 char block[T_BLOCKSIZE];
301
302 memset(&block, 0, T_BLOCKSIZE);
303 for (j = 0; j < 2; j++)
304 {
305 i = tar_block_write(t, &block);
306 if (i != T_BLOCKSIZE)
307 {
308 if (i != -1)
309 errno = EINVAL;
310 return -1;
311 }
312 }
313
314 return 0;
315}
316
317
318/* add file contents to a tarchive */
319int
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500320tar_append_regfile(TAR *t, const char *realname)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500321{
322 char block[T_BLOCKSIZE];
323 int filefd;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500324 int64_t i, size;
325 ssize_t j;
326 int rv = -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500327
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500328#if defined(O_BINARY)
329 filefd = open(realname, O_RDONLY|O_BINARY);
330#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500331 filefd = open(realname, O_RDONLY);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500332#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500333 if (filefd == -1)
334 {
335#ifdef DEBUG
336 perror("open()");
337#endif
338 return -1;
339 }
340
341 size = th_get_size(t);
342 for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
343 {
344 j = read(filefd, &block, T_BLOCKSIZE);
345 if (j != T_BLOCKSIZE)
346 {
347 if (j != -1)
348 errno = EINVAL;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500349 goto fail;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500350 }
351 if (tar_block_write(t, &block) == -1)
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500352 goto fail;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500353 }
354
355 if (i > 0)
356 {
357 j = read(filefd, &block, i);
358 if (j == -1)
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500359 goto fail;
360 memset(&(block[i]), 0, T_BLOCKSIZE - i);
361 if (tar_block_write(t, &block) == -1)
362 goto fail;
363 }
364
365 /* success! */
366 rv = 0;
367fail:
368 close(filefd);
369
370 return rv;
371}
372
373
374/* add file contents to a tarchive */
375int
376tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
377 uid_t uid, gid_t gid, void *buf, size_t len)
378{
379 struct stat st;
380
381 memset(&st, 0, sizeof(st));
382 st.st_mode = S_IFREG | mode;
383 st.st_uid = uid;
384 st.st_gid = gid;
385 st.st_mtime = time(NULL);
386 st.st_size = len;
387
388 th_set_from_stat(t, &st);
389 th_set_path(t, savename);
390
391 /* write header */
392 if (th_write(t) != 0)
393 {
394#ifdef DEBUG
395 fprintf(stderr, "tar_append_file_contents(): could not write header, t->fd = %d\n", t->fd);
396#endif
397 return -1;
398 }
399
400 return tar_append_buffer(t, buf, len);
401}
402
403int
404tar_append_buffer(TAR *t, void *buf, size_t len)
405{
406 char block[T_BLOCKSIZE];
407 int filefd;
408 int i, j;
409 size_t size = len;
410
411 for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE)
412 {
413 if (tar_block_write(t, buf) == -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500414 return -1;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500415 buf = (char *)buf + T_BLOCKSIZE;
416 }
417
418 if (i > 0)
419 {
420 memcpy(block, buf, i);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500421 memset(&(block[i]), 0, T_BLOCKSIZE - i);
422 if (tar_block_write(t, &block) == -1)
423 return -1;
424 }
425
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500426 return 0;
427}
428