bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 1998-2003 University of Illinois Board of Trustees |
| 3 | ** Copyright 1998-2003 Mark D. Roth |
| 4 | ** All rights reserved. |
| 5 | ** |
| 6 | ** extract.c - libtar code to extract a file from 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 Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 16 | #include <string.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 17 | #include <sys/param.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <errno.h> |
| 21 | #include <utime.h> |
| 22 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 23 | #include <sys/capability.h> |
| 24 | #include <sys/xattr.h> |
| 25 | #include <linux/xattr.h> |
| 26 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 27 | #ifdef STDC_HEADERS |
| 28 | # include <stdlib.h> |
| 29 | #endif |
| 30 | |
| 31 | #ifdef HAVE_UNISTD_H |
| 32 | # include <unistd.h> |
| 33 | #endif |
| 34 | |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 35 | #include <selinux/selinux.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 36 | |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 37 | #ifdef HAVE_EXT4_CRYPT |
| 38 | # include "ext4crypt_tar.h" |
| 39 | #endif |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 40 | #include "android_utils.h" |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 41 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 42 | const unsigned long long progress_size = (unsigned long long)(T_BLOCKSIZE); |
| 43 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 44 | static int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 45 | tar_set_file_perms(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 46 | { |
| 47 | mode_t mode; |
| 48 | uid_t uid; |
| 49 | gid_t gid; |
| 50 | struct utimbuf ut; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 51 | const char *filename; |
| 52 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 53 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 54 | pn = th_get_pathname(t); |
| 55 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 56 | mode = th_get_mode(t); |
| 57 | uid = th_get_uid(t); |
| 58 | gid = th_get_gid(t); |
| 59 | ut.modtime = ut.actime = th_get_mtime(t); |
| 60 | |
Dees_Troy | 7179659 | 2013-03-14 20:16:29 +0000 | [diff] [blame] | 61 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 62 | printf("tar_set_file_perms(): setting perms: %s (mode %04o, uid %d, gid %d)\n", |
| 63 | filename, mode, uid, gid); |
Dees_Troy | 7179659 | 2013-03-14 20:16:29 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 66 | /* change owner/group */ |
| 67 | if (geteuid() == 0) |
| 68 | #ifdef HAVE_LCHOWN |
| 69 | if (lchown(filename, uid, gid) == -1) |
| 70 | { |
| 71 | # ifdef DEBUG |
| 72 | fprintf(stderr, "lchown(\"%s\", %d, %d): %s\n", |
| 73 | filename, uid, gid, strerror(errno)); |
| 74 | # endif |
| 75 | #else /* ! HAVE_LCHOWN */ |
| 76 | if (!TH_ISSYM(t) && chown(filename, uid, gid) == -1) |
| 77 | { |
| 78 | # ifdef DEBUG |
| 79 | fprintf(stderr, "chown(\"%s\", %d, %d): %s\n", |
| 80 | filename, uid, gid, strerror(errno)); |
| 81 | # endif |
| 82 | #endif /* HAVE_LCHOWN */ |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | /* change access/modification time */ |
| 87 | if (!TH_ISSYM(t) && utime(filename, &ut) == -1) |
| 88 | { |
| 89 | #ifdef DEBUG |
| 90 | perror("utime()"); |
| 91 | #endif |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | /* change permissions */ |
| 96 | if (!TH_ISSYM(t) && chmod(filename, mode) == -1) |
| 97 | { |
| 98 | #ifdef DEBUG |
| 99 | perror("chmod()"); |
| 100 | #endif |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* switchboard */ |
| 109 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 110 | tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 111 | { |
| 112 | int i; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 113 | #ifdef LIBTAR_FILE_HASH |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 114 | char *lnp; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 115 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 116 | int pathname_len; |
| 117 | int realname_len; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 118 | #endif |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 119 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 120 | if (t->options & TAR_NOOVERWRITE) |
| 121 | { |
| 122 | struct stat s; |
| 123 | |
| 124 | if (lstat(realname, &s) == 0 || errno != ENOENT) |
| 125 | { |
| 126 | errno = EEXIST; |
| 127 | return -1; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (TH_ISDIR(t)) |
| 132 | { |
| 133 | i = tar_extract_dir(t, realname); |
| 134 | if (i == 1) |
| 135 | i = 0; |
| 136 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 137 | else if (TH_ISLNK(t)) |
Dees_Troy | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 138 | i = tar_extract_hardlink(t, realname, prefix); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 139 | else if (TH_ISSYM(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 140 | i = tar_extract_symlink(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 141 | else if (TH_ISCHR(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 142 | i = tar_extract_chardev(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 143 | else if (TH_ISBLK(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 144 | i = tar_extract_blockdev(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 145 | else if (TH_ISFIFO(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 146 | i = tar_extract_fifo(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 147 | else /* if (TH_ISREG(t)) */ |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 148 | i = tar_extract_regfile(t, realname, progress_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 149 | |
| 150 | if (i != 0) { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 151 | fprintf(stderr, "tar_extract_file(): failed to extract %s !!!\n", realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 152 | return i; |
| 153 | } |
| 154 | |
| 155 | i = tar_set_file_perms(t, realname); |
| 156 | if (i != 0) { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 157 | fprintf(stderr, "tar_extract_file(): failed to set permissions on %s !!!\n", realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 158 | return i; |
| 159 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 160 | |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 161 | if((t->options & TAR_STORE_SELINUX) && t->th_buf.selinux_context != NULL) |
| 162 | { |
| 163 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 164 | printf("tar_extract_file(): restoring SELinux context %s to file %s\n", t->th_buf.selinux_context, realname); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 165 | #endif |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 166 | if (lsetfilecon(realname, t->th_buf.selinux_context) < 0) |
| 167 | fprintf(stderr, "tar_extract_file(): failed to restore SELinux context %s to file %s !!!\n", t->th_buf.selinux_context, realname); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 168 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 169 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 170 | if((t->options & TAR_STORE_POSIX_CAP) && t->th_buf.has_cap_data) |
| 171 | { |
| 172 | #if 1 //def DEBUG |
| 173 | printf("tar_extract_file(): restoring posix capabilities to file %s\n", realname); |
| 174 | print_caps(&t->th_buf.cap_data); |
| 175 | #endif |
| 176 | if (setxattr(realname, XATTR_NAME_CAPS, &t->th_buf.cap_data, sizeof(struct vfs_cap_data), 0) < 0) |
| 177 | fprintf(stderr, "tar_extract_file(): failed to restore posix capabilities to file %s !!!\n", realname); |
| 178 | } |
| 179 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 180 | #ifdef LIBTAR_FILE_HASH |
| 181 | pn = th_get_pathname(t); |
| 182 | pathname_len = strlen(pn) + 1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 183 | realname_len = strlen(realname) + 1; |
| 184 | lnp = (char *)calloc(1, pathname_len + realname_len); |
| 185 | if (lnp == NULL) |
| 186 | return -1; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 187 | strcpy(&lnp[0], pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 188 | strcpy(&lnp[pathname_len], realname); |
| 189 | #ifdef DEBUG |
| 190 | printf("tar_extract_file(): calling libtar_hash_add(): key=\"%s\", " |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 191 | "value=\"%s\"\n", pn, realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 192 | #endif |
| 193 | if (libtar_hash_add(t->h, lnp) != 0) |
| 194 | return -1; |
| 195 | free(lnp); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 196 | #endif |
| 197 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /* extract regular file */ |
| 203 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 204 | tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 205 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 206 | int64_t size, i; |
| 207 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 208 | int fdout; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 209 | char buf[T_BLOCKSIZE]; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 210 | const char *filename; |
| 211 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 212 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 213 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 214 | printf(" ==> tar_extract_regfile(realname=\"%s\")\n", realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 215 | #endif |
| 216 | |
| 217 | if (!TH_ISREG(t)) |
| 218 | { |
| 219 | errno = EINVAL; |
| 220 | return -1; |
| 221 | } |
| 222 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 223 | pn = th_get_pathname(t); |
| 224 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 225 | size = th_get_size(t); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 226 | |
| 227 | if (mkdirhier(dirname(filename)) == -1) |
| 228 | return -1; |
| 229 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 230 | printf(" ==> extracting: %s (file size %lld bytes)\n", |
| 231 | filename, size); |
| 232 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 233 | fdout = open(filename, O_WRONLY | O_CREAT | O_TRUNC |
| 234 | #ifdef O_BINARY |
| 235 | | O_BINARY |
| 236 | #endif |
| 237 | , 0666); |
| 238 | if (fdout == -1) |
| 239 | { |
| 240 | #ifdef DEBUG |
| 241 | perror("open()"); |
| 242 | #endif |
| 243 | return -1; |
| 244 | } |
| 245 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 246 | /* extract the file */ |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 247 | for (i = size; i > 0; i -= T_BLOCKSIZE) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 248 | { |
| 249 | k = tar_block_read(t, buf); |
| 250 | if (k != T_BLOCKSIZE) |
| 251 | { |
| 252 | if (k != -1) |
| 253 | errno = EINVAL; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 254 | close(fdout); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | /* write block to output file */ |
| 259 | if (write(fdout, buf, |
| 260 | ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : i)) == -1) |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 261 | { |
| 262 | close(fdout); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 263 | return -1; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 264 | } |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 265 | else |
| 266 | { |
| 267 | if (*progress_fd != 0) |
| 268 | write(*progress_fd, &progress_size, sizeof(progress_size)); |
| 269 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* close output file */ |
| 273 | if (close(fdout) == -1) |
| 274 | return -1; |
| 275 | |
| 276 | #ifdef DEBUG |
| 277 | printf("### done extracting %s\n", filename); |
| 278 | #endif |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /* skip regfile */ |
| 285 | int |
| 286 | tar_skip_regfile(TAR *t) |
| 287 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 288 | int64_t size, i; |
| 289 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 290 | char buf[T_BLOCKSIZE]; |
| 291 | |
| 292 | if (!TH_ISREG(t)) |
| 293 | { |
| 294 | errno = EINVAL; |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | size = th_get_size(t); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 299 | for (i = size; i > 0; i -= T_BLOCKSIZE) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 300 | { |
| 301 | k = tar_block_read(t, buf); |
| 302 | if (k != T_BLOCKSIZE) |
| 303 | { |
| 304 | if (k != -1) |
| 305 | errno = EINVAL; |
| 306 | return -1; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /* hardlink */ |
| 315 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 316 | tar_extract_hardlink(TAR * t, const char *realname, const char *prefix) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 317 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 318 | const char *filename; |
| 319 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 320 | char *linktgt = NULL; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 321 | char *newtgt = NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 322 | char *lnp; |
| 323 | libtar_hashptr_t hp; |
| 324 | |
| 325 | if (!TH_ISLNK(t)) |
| 326 | { |
| 327 | errno = EINVAL; |
| 328 | return -1; |
| 329 | } |
| 330 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 331 | pn = th_get_pathname(t); |
| 332 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 333 | if (mkdirhier(dirname(filename)) == -1) |
| 334 | return -1; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 335 | if (unlink(filename) == -1 && errno != ENOENT) |
| 336 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 337 | libtar_hashptr_reset(&hp); |
| 338 | if (libtar_hash_getkey(t->h, &hp, th_get_linkname(t), |
| 339 | (libtar_matchfunc_t)libtar_str_match) != 0) |
| 340 | { |
| 341 | lnp = (char *)libtar_hashptr_data(&hp); |
| 342 | linktgt = &lnp[strlen(lnp) + 1]; |
| 343 | } |
| 344 | else |
| 345 | linktgt = th_get_linkname(t); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 346 | |
| 347 | newtgt = strdup(linktgt); |
Dees_Troy | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 348 | sprintf(linktgt, "%s/%s", prefix, newtgt); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 349 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 350 | printf(" ==> extracting: %s (link to %s)\n", filename, linktgt); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 351 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 352 | if (link(linktgt, filename) == -1) |
| 353 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 354 | fprintf(stderr, "tar_extract_hardlink(): failed restore of hardlink '%s' but returning as if nothing bad happened\n", filename); |
Dees_Troy | f96fb97 | 2013-03-01 22:34:25 +0000 | [diff] [blame] | 355 | return 0; // Used to be -1 |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /* symlink */ |
| 363 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 364 | tar_extract_symlink(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 365 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 366 | const char *filename; |
| 367 | char *pn; |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 368 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 369 | if (!TH_ISSYM(t)) |
| 370 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 371 | errno = EINVAL; |
| 372 | return -1; |
| 373 | } |
| 374 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 375 | pn = th_get_pathname(t); |
| 376 | filename = (realname ? realname : pn); |
| 377 | if (mkdirhier(dirname(filename)) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 378 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 379 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 380 | if (unlink(filename) == -1 && errno != ENOENT) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 381 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 382 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 383 | printf(" ==> extracting: %s (symlink to %s)\n", |
| 384 | filename, th_get_linkname(t)); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 385 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 386 | if (symlink(th_get_linkname(t), filename) == -1) |
| 387 | { |
| 388 | #ifdef DEBUG |
| 389 | perror("symlink()"); |
| 390 | #endif |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /* character device */ |
| 399 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 400 | tar_extract_chardev(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 401 | { |
| 402 | mode_t mode; |
| 403 | unsigned long devmaj, devmin; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 404 | const char *filename; |
| 405 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 406 | |
| 407 | if (!TH_ISCHR(t)) |
| 408 | { |
| 409 | errno = EINVAL; |
| 410 | return -1; |
| 411 | } |
| 412 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 413 | pn = th_get_pathname(t); |
| 414 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 415 | mode = th_get_mode(t); |
| 416 | devmaj = th_get_devmajor(t); |
| 417 | devmin = th_get_devminor(t); |
| 418 | |
| 419 | if (mkdirhier(dirname(filename)) == -1) |
| 420 | return -1; |
| 421 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 422 | printf(" ==> extracting: %s (character device %ld,%ld)\n", |
| 423 | filename, devmaj, devmin); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 424 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 425 | if (mknod(filename, mode | S_IFCHR, |
| 426 | compat_makedev(devmaj, devmin)) == -1) |
| 427 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 428 | fprintf(stderr, "tar_extract_chardev(): failed restore of character device '%s' but returning as if nothing bad happened\n", filename); |
| 429 | return 0; // Used to be -1 |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | /* block device */ |
| 437 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 438 | tar_extract_blockdev(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 439 | { |
| 440 | mode_t mode; |
| 441 | unsigned long devmaj, devmin; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 442 | const char *filename; |
| 443 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 444 | |
| 445 | if (!TH_ISBLK(t)) |
| 446 | { |
| 447 | errno = EINVAL; |
| 448 | return -1; |
| 449 | } |
| 450 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 451 | pn = th_get_pathname(t); |
| 452 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 453 | mode = th_get_mode(t); |
| 454 | devmaj = th_get_devmajor(t); |
| 455 | devmin = th_get_devminor(t); |
| 456 | |
| 457 | if (mkdirhier(dirname(filename)) == -1) |
| 458 | return -1; |
| 459 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 460 | printf(" ==> extracting: %s (block device %ld,%ld)\n", |
| 461 | filename, devmaj, devmin); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 462 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 463 | if (mknod(filename, mode | S_IFBLK, |
| 464 | compat_makedev(devmaj, devmin)) == -1) |
| 465 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 466 | fprintf(stderr, "tar_extract_blockdev(): failed restore of block device '%s' but returning as if nothing bad happened\n", filename); |
| 467 | return 0; // Used to be -1 |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 473 | /* directory */ |
| 474 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 475 | tar_extract_dir(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 476 | { |
| 477 | mode_t mode; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 478 | const char *filename; |
| 479 | char *pn; |
| 480 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 481 | if (!TH_ISDIR(t)) |
| 482 | { |
| 483 | errno = EINVAL; |
| 484 | return -1; |
| 485 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 486 | pn = th_get_pathname(t); |
| 487 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 488 | mode = th_get_mode(t); |
| 489 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 490 | if (mkdirhier(dirname(filename)) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 491 | return -1; |
| 492 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 493 | printf(" ==> extracting: %s (mode %04o, directory)\n", filename, |
| 494 | mode); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 495 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 496 | if (mkdir(filename, mode) == -1) |
| 497 | { |
| 498 | if (errno == EEXIST) |
| 499 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 500 | if (chmod(filename, mode) == -1) |
| 501 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 502 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 503 | perror("chmod()"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 504 | #endif |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 505 | return -1; |
| 506 | } |
| 507 | else |
| 508 | { |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 509 | #if 1 //def DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 510 | puts(" *** using existing directory"); |
| 511 | #endif |
| 512 | return 1; |
| 513 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 514 | } |
| 515 | else |
| 516 | { |
| 517 | #ifdef DEBUG |
| 518 | perror("mkdir()"); |
| 519 | #endif |
| 520 | return -1; |
| 521 | } |
| 522 | } |
| 523 | |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 524 | if (t->options & TAR_STORE_ANDROID_USER_XATTR) |
| 525 | { |
| 526 | if (t->th_buf.has_user_default) { |
| 527 | #if 1 //def DEBUG |
| 528 | printf("tar_extract_file(): restoring android user.default xattr to %s\n", realname); |
| 529 | #endif |
| 530 | if (setxattr(realname, "user.default", NULL, 0, 0) < 0) { |
| 531 | fprintf(stderr, "tar_extract_file(): failed to restore android user.default to file %s !!!\n", realname); |
| 532 | return -1; |
| 533 | } |
| 534 | } |
| 535 | if (t->th_buf.has_user_cache) { |
| 536 | #if 1 //def DEBUG |
| 537 | printf("tar_extract_file(): restoring android user.inode_cache xattr to %s\n", realname); |
| 538 | #endif |
| 539 | if (write_path_inode(realname, "cache", "user.inode_cache")) |
| 540 | return -1; |
| 541 | } |
| 542 | if (t->th_buf.has_user_code_cache) { |
| 543 | #if 1 //def DEBUG |
| 544 | printf("tar_extract_file(): restoring android user.inode_code_cache xattr to %s\n", realname); |
| 545 | #endif |
| 546 | if (write_path_inode(realname, "code_cache", "user.inode_code_cache")) |
| 547 | return -1; |
| 548 | } |
| 549 | } |
| 550 | |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 551 | #ifdef HAVE_EXT4_CRYPT |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 552 | if(t->th_buf.eep != NULL) |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 553 | { |
| 554 | #ifdef DEBUG |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 555 | printf("tar_extract_file(): restoring EXT4 crypt policy %s to dir %s\n", t->th_buf.eep->master_key_descriptor, realname); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 556 | #endif |
| 557 | char binary_policy[EXT4_KEY_DESCRIPTOR_SIZE]; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 558 | if (!lookup_ref_tar(t->th_buf.eep->master_key_descriptor, &binary_policy[0])) { |
| 559 | printf("error looking up proper e4crypt policy for '%s' - %s\n", realname, t->th_buf.eep->master_key_descriptor); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 560 | return -1; |
| 561 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 562 | char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX]; |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 563 | policy_to_hex(binary_policy, policy_hex); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 564 | printf("restoring policy %s > '%s' to '%s'\n", t->th_buf.eep->master_key_descriptor, policy_hex, realname); |
| 565 | memcpy(&t->th_buf.eep->master_key_descriptor, binary_policy, EXT4_KEY_DESCRIPTOR_SIZE); |
| 566 | if (!e4crypt_policy_set_struct(realname, t->th_buf.eep)) |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 567 | { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 568 | printf("tar_extract_file(): failed to restore EXT4 crypt policy to dir '%s' '%s'!!!\n", realname, policy_hex); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 569 | //return -1; // This may not be an error in some cases, so log and ignore |
| 570 | } |
| 571 | } |
| 572 | #endif |
| 573 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /* FIFO */ |
| 579 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 580 | tar_extract_fifo(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 581 | { |
| 582 | mode_t mode; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 583 | const char *filename; |
| 584 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 585 | |
| 586 | if (!TH_ISFIFO(t)) |
| 587 | { |
| 588 | errno = EINVAL; |
| 589 | return -1; |
| 590 | } |
| 591 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 592 | pn = th_get_pathname(t); |
| 593 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 594 | mode = th_get_mode(t); |
| 595 | |
| 596 | if (mkdirhier(dirname(filename)) == -1) |
| 597 | return -1; |
| 598 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 599 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 600 | printf(" ==> extracting: %s (fifo)\n", filename); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 601 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 602 | if (mkfifo(filename, mode) == -1) |
| 603 | { |
| 604 | #ifdef DEBUG |
| 605 | perror("mkfifo()"); |
| 606 | #endif |
| 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 613 | /* extract file contents from a tarchive */ |
| 614 | int |
| 615 | tar_extract_file_contents(TAR *t, void *buf, size_t *lenp) |
| 616 | { |
| 617 | char block[T_BLOCKSIZE]; |
| 618 | int64_t size, i; |
| 619 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 620 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 621 | #ifdef DEBUG |
| 622 | printf(" ==> tar_extract_file_contents\n"); |
| 623 | #endif |
| 624 | |
| 625 | if (!TH_ISREG(t)) |
| 626 | { |
| 627 | errno = EINVAL; |
| 628 | return -1; |
| 629 | } |
| 630 | |
| 631 | size = th_get_size(t); |
| 632 | if ((uint64_t)size > *lenp) |
| 633 | { |
| 634 | errno = ENOSPC; |
| 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | /* extract the file */ |
| 639 | for (i = size; i >= T_BLOCKSIZE; i -= T_BLOCKSIZE) |
| 640 | { |
| 641 | k = tar_block_read(t, buf); |
| 642 | if (k != T_BLOCKSIZE) |
| 643 | { |
| 644 | if (k != -1) |
| 645 | errno = EINVAL; |
| 646 | return -1; |
| 647 | } |
| 648 | buf = (char *)buf + T_BLOCKSIZE; |
| 649 | } |
| 650 | if (i > 0) { |
| 651 | k = tar_block_read(t, block); |
| 652 | if (k != T_BLOCKSIZE) |
| 653 | { |
| 654 | if (k != -1) |
| 655 | errno = EINVAL; |
| 656 | return -1; |
| 657 | } |
| 658 | memcpy(buf, block, i); |
| 659 | } |
| 660 | *lenp = (size_t)size; |
| 661 | |
| 662 | #ifdef DEBUG |
| 663 | printf("### done extracting contents\n"); |
| 664 | #endif |
| 665 | return 0; |
| 666 | } |