blob: 19ddd06b16b2262ef77a4bb5104245dc3798252a [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** libtar.h - header file for libtar library
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#ifndef LIBTAR_H
14#define LIBTAR_H
15
16#include <sys/types.h>
17#include <sys/stat.h>
Ethan Yonker71187742017-01-13 13:30:10 -060018#include <linux/capability.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050019#include "tar.h"
20
21#include "libtar_listhash.h"
22
Ethan Yonker79f88bd2016-12-09 14:52:12 -060023#ifdef HAVE_EXT4_CRYPT
Ethan Yonkerfefe5912017-09-30 22:22:13 -050024# include "ext4crypt_tar.h"
Ethan Yonker79f88bd2016-12-09 14:52:12 -060025#endif
26
bigbiff7ba75002020-04-11 20:47:09 -040027#ifdef USE_FSCRYPT
28#include "fscrypt_policy.h"
29#endif
30
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#ifdef __cplusplus
32extern "C"
33{
34#endif
35
36
37/* useful constants */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050038/* see FIXME note in block.c regarding T_BLOCKSIZE */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050039#define T_BLOCKSIZE 512
40#define T_NAMELEN 100
41#define T_PREFIXLEN 155
42#define T_MAXPATHLEN (T_NAMELEN + T_PREFIXLEN)
43
44/* GNU extensions for typeflag */
45#define GNU_LONGNAME_TYPE 'L'
46#define GNU_LONGLINK_TYPE 'K'
47
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020048/* extended metadata for next file - used to store selinux_context */
49#define TH_EXT_TYPE 'x'
Ethan Yonker71187742017-01-13 13:30:10 -060050#define TH_POL_TYPE_DO_NOT_USE 'p'
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020051
bigbiff bigbiff9c754052013-01-09 09:09:08 -050052/* our version of the tar header structure */
53struct tar_header
54{
55 char name[100];
56 char mode[8];
57 char uid[8];
58 char gid[8];
59 char size[12];
60 char mtime[12];
61 char chksum[8];
62 char typeflag;
63 char linkname[100];
64 char magic[6];
65 char version[2];
66 char uname[32];
67 char gname[32];
68 char devmajor[8];
69 char devminor[8];
70 char prefix[155];
71 char padding[12];
72 char *gnu_longname;
73 char *gnu_longlink;
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020074 char *selinux_context;
Ethan Yonker79f88bd2016-12-09 14:52:12 -060075#ifdef HAVE_EXT4_CRYPT
Ethan Yonkerfefe5912017-09-30 22:22:13 -050076 struct ext4_encryption_policy *eep;
Ethan Yonker79f88bd2016-12-09 14:52:12 -060077#endif
bigbiff7ba75002020-04-11 20:47:09 -040078#ifdef USE_FSCRYPT
79 struct fscrypt_encryption_policy *fep;
80#endif
Ethan Yonker71187742017-01-13 13:30:10 -060081 int has_cap_data;
82 struct vfs_cap_data cap_data;
Ethan Yonker8d039f72017-02-03 14:26:15 -060083 int has_user_default;
84 int has_user_cache;
85 int has_user_code_cache;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050086};
87
88
89/***** handle.c ************************************************************/
90
91typedef int (*openfunc_t)(const char *, int, ...);
92typedef int (*closefunc_t)(int);
93typedef ssize_t (*readfunc_t)(int, void *, size_t);
94typedef ssize_t (*writefunc_t)(int, const void *, size_t);
95
96typedef struct
97{
98 openfunc_t openfunc;
99 closefunc_t closefunc;
100 readfunc_t readfunc;
101 writefunc_t writefunc;
102}
103tartype_t;
104
105typedef struct
106{
107 tartype_t *type;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500108 const char *pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500109 long fd;
110 int oflags;
111 int options;
112 struct tar_header th_buf;
113 libtar_hash_t *h;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500114
115 /* introduced in libtar 1.2.21 */
116 char *th_pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500117}
118TAR;
119
120/* constant values for the TAR options field */
121#define TAR_GNU 1 /* use GNU extensions */
122#define TAR_VERBOSE 2 /* output file info to stdout */
123#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
124#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
125#define TAR_CHECK_MAGIC 16 /* check magic in file header */
126#define TAR_CHECK_VERSION 32 /* check version in file header */
127#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200128#define TAR_STORE_SELINUX 128 /* store selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500129#define TAR_USE_NUMERIC_ID 256 /* favor numeric owner over names */
bigbiff7ba75002020-04-11 20:47:09 -0400130#ifdef HAVE_EXT4_CRYPT
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600131#define TAR_STORE_EXT4_POL 512 /* store ext4 crypto policy */
bigbiff7ba75002020-04-11 20:47:09 -0400132#endif
133#ifdef USE_FSCRYPT
134#define TAR_STORE_FSCRYPT_POL 512 /* store fscrypt crypto policy */
135#endif
Ethan Yonker71187742017-01-13 13:30:10 -0600136#define TAR_STORE_POSIX_CAP 1024 /* store posix file capabilities */
Ethan Yonker8d039f72017-02-03 14:26:15 -0600137#define TAR_STORE_ANDROID_USER_XATTR 2048 /* store android user.* xattr */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500138
139/* this is obsolete - it's here for backwards-compatibility only */
140#define TAR_IGNORE_MAGIC 0
141
142extern const char libtar_version[];
143
144
145/* open a new tarfile handle */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500146int tar_open(TAR **t, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500147 int oflags, int mode, int options);
148
149/* make a tarfile handle out of a previously-opened descriptor */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500150int tar_fdopen(TAR **t, int fd, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500151 int oflags, int mode, int options);
152
153/* returns the descriptor associated with t */
154int tar_fd(TAR *t);
155
156/* close tarfile handle */
157int tar_close(TAR *t);
158
159
160/***** append.c ************************************************************/
161
162/* forward declaration to appease the compiler */
163struct tar_dev;
164
165/* cleanup function */
166void tar_dev_free(struct tar_dev *tdp);
167
168/* Appends a file to the tar archive.
169 * Arguments:
170 * t = TAR handle to append to
171 * realname = path of file to append
172 * savename = name to save the file under in the archive
173 */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500174int tar_append_file(TAR *t, const char *realname, const char *savename);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500175
176/* write EOF indicator */
177int tar_append_eof(TAR *t);
178
179/* add file contents to a tarchive */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500180int tar_append_regfile(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500181
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500182/* Appends in-memory file contents to a tarchive.
183 * Arguments:
184 * t = TAR handle to append to
185 * savename = name to save the file under in the archive
186 * mode = mode
187 * uid, gid = owner
188 * buf, len = in-memory buffer
189 */
190int tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
191 uid_t uid, gid_t gid, void *buf, size_t len);
192
193/* add buffer to a tarchive */
194int tar_append_buffer(TAR *t, void *buf, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500195
196/***** block.c *************************************************************/
197
198/* macros for reading/writing tarchive blocks */
199#define tar_block_read(t, buf) \
200 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
201#define tar_block_write(t, buf) \
202 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
203
204/* read/write a header block */
205int th_read(TAR *t);
206int th_write(TAR *t);
207
208
209/***** decode.c ************************************************************/
210
211/* determine file type */
212#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
213 || (t)->th_buf.typeflag == AREGTYPE \
214 || (t)->th_buf.typeflag == CONTTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500215 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500216 && (t)->th_buf.typeflag != LNKTYPE))
217#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
218#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500219 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500220#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500221 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500222#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500223 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500224#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500225 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500226 || ((t)->th_buf.typeflag == AREGTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500227 && strnlen((t)->th_buf.name, T_NAMELEN) \
228 && ((t)->th_buf.name[strnlen((t)->th_buf.name, T_NAMELEN) - 1] == '/')))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500229#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500230 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500231#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
232#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200233#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
Ethan Yonker71187742017-01-13 13:30:10 -0600234#define TH_ISPOLHEADER(t) ((t)->th_buf.typeflag == TH_POL_TYPE_DO_NOT_USE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500235
236/* decode tar header info */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500237#define th_get_crc(t) oct_to_int((t)->th_buf.chksum, sizeof((t)->th_buf.chksum))
238#define th_get_size(t) oct_to_int_ex((t)->th_buf.size, sizeof((t)->th_buf.size))
239#define th_get_mtime(t) oct_to_int_ex((t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
240#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor, sizeof((t)->th_buf.devmajor))
241#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor, sizeof((t)->th_buf.devminor))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500242#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
243 ? (t)->th_buf.gnu_longlink \
244 : (t)->th_buf.linkname)
245char *th_get_pathname(TAR *t);
246mode_t th_get_mode(TAR *t);
247uid_t th_get_uid(TAR *t);
248gid_t th_get_gid(TAR *t);
249
250
251/***** encode.c ************************************************************/
252
253/* encode file info in th_header */
254void th_set_type(TAR *t, mode_t mode);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500255void th_set_path(TAR *t, const char *pathname);
256void th_set_link(TAR *t, const char *linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500257void th_set_device(TAR *t, dev_t device);
258void th_set_user(TAR *t, uid_t uid);
259void th_set_group(TAR *t, gid_t gid);
260void th_set_mode(TAR *t, mode_t fmode);
261#define th_set_mtime(t, fmtime) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500262 int_to_oct_ex((fmtime), (t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500263#define th_set_size(t, fsize) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500264 int_to_oct_ex((fsize), (t)->th_buf.size, sizeof((t)->th_buf.size))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500265
266/* encode everything at once (except the pathname and linkname) */
267void th_set_from_stat(TAR *t, struct stat *s);
268
269/* encode magic, version, and crc - must be done after everything else is set */
270void th_finish(TAR *t);
271
272
273/***** extract.c ***********************************************************/
274
275/* sequentially extract next file from t */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500276int tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500277
278/* extract different file types */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500279int tar_extract_dir(TAR *t, const char *realname);
280int tar_extract_hardlink(TAR *t, const char *realname, const char *prefix);
281int tar_extract_symlink(TAR *t, const char *realname);
282int tar_extract_chardev(TAR *t, const char *realname);
283int tar_extract_blockdev(TAR *t, const char *realname);
284int tar_extract_fifo(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500285
286/* for regfiles, we need to extract the content blocks as well */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500287int tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500288int tar_skip_regfile(TAR *t);
289
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500290/* extract regfile to buffer */
291int tar_extract_file_contents(TAR *t, void *buf, size_t *lenp);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500292
293/***** output.c ************************************************************/
294
295/* print the tar header */
296void th_print(TAR *t);
297
298/* print "ls -l"-like output for the file described by th */
299void th_print_long_ls(TAR *t);
300
301
302/***** util.c *************************************************************/
303
304/* hashing function for pathnames */
305int path_hashfunc(char *key, int numbuckets);
306
307/* matching function for dev_t's */
308int dev_match(dev_t *dev1, dev_t *dev2);
309
310/* matching function for ino_t's */
311int ino_match(ino_t *ino1, ino_t *ino2);
312
313/* hashing function for dev_t's */
314int dev_hash(dev_t *dev);
315
316/* hashing function for ino_t's */
317int ino_hash(ino_t *inode);
318
319/* create any necessary dirs */
320int mkdirhier(char *path);
321
322/* calculate header checksum */
323int th_crc_calc(TAR *t);
324
325/* calculate a signed header checksum */
326int th_signed_crc_calc(TAR *t);
327
328/* compare checksums in a forgiving way */
329#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
330
331/* string-octal to integer conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500332int64_t oct_to_int(char *oct, size_t len);
333
334/* string-octal or binary to integer conversion */
335int64_t oct_to_int_ex(char *oct, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500336
337/* integer to NULL-terminated string-octal conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500338void int_to_oct(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500339
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500340/* integer to string-octal conversion, or binary as necessary */
341void int_to_oct_ex(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500342
Ethan Yonker71187742017-01-13 13:30:10 -0600343/* prints posix file capabilities */
344void print_caps(struct vfs_cap_data *cap_data);
345
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500346
347/***** wrapper.c **********************************************************/
348
349/* extract groups of files */
350int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500351int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500352
353/* add a whole tree of files */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500354int tar_append_tree(TAR *t, char *realdir, char *savedir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500355
n0d33b511632013-03-06 21:14:15 +0200356/* find an entry */
357int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500358
359#ifdef __cplusplus
360}
361#endif
362
363#endif /* ! LIBTAR_H */
364