"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 2 | /* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 3 | * |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 4 | * FIXME: |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * In privileged mode if uname and gname map to a uid and gid then use the |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 6 | * mapped value instead of the uid/gid values in tar header |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 7 | * |
| 8 | * References: |
| 9 | * GNU tar and star man pages, |
| 10 | * Opengroup's ustar interchange format, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 11 | * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Rob Landley | 161aae7 | 2005-10-11 20:17:30 +0000 | [diff] [blame] | 17 | #include <sys/sysmacros.h> /* For makedev */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 18 | #include "unarchive.h" |
| 19 | #include "libbb.h" |
| 20 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 21 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 22 | static char *longname = NULL; |
| 23 | static char *linkname = NULL; |
| 24 | #endif |
| 25 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 26 | char get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 27 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 28 | file_header_t *file_header = archive_handle->file_header; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 29 | union { |
Glenn L McGrath | ef91bf6 | 2003-09-12 06:31:28 +0000 | [diff] [blame] | 30 | /* ustar header, Posix 1003.1 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 31 | unsigned char raw[512]; |
| 32 | struct { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 33 | char name[100]; /* 0-99 */ |
| 34 | char mode[8]; /* 100-107 */ |
| 35 | char uid[8]; /* 108-115 */ |
| 36 | char gid[8]; /* 116-123 */ |
| 37 | char size[12]; /* 124-135 */ |
| 38 | char mtime[12]; /* 136-147 */ |
| 39 | char chksum[8]; /* 148-155 */ |
| 40 | char typeflag; /* 156-156 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 41 | char linkname[100]; /* 157-256 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 42 | char magic[6]; /* 257-262 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 43 | char version[2]; /* 263-264 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 44 | char uname[32]; /* 265-296 */ |
| 45 | char gname[32]; /* 297-328 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 46 | char devmajor[8]; /* 329-336 */ |
| 47 | char devminor[8]; /* 337-344 */ |
| 48 | char prefix[155]; /* 345-499 */ |
| 49 | char padding[12]; /* 500-512 */ |
| 50 | } formated; |
| 51 | } tar; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 52 | long sum = 0; |
| 53 | long i; |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 54 | static int end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 55 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 56 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 57 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 58 | |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 59 | if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) { |
Glenn L McGrath | 7f2a953 | 2002-11-05 02:56:57 +0000 | [diff] [blame] | 60 | /* Assume end of file */ |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 61 | bb_error_msg_and_die("Short header"); |
| 62 | //return(EXIT_FAILURE); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 63 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 64 | archive_handle->offset += 512; |
| 65 | |
| 66 | /* If there is no filename its an empty header */ |
| 67 | if (tar.formated.name[0] == 0) { |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 68 | if (end) { |
| 69 | /* This is the second consecutive empty header! End of archive! |
| 70 | * Read until the end to empty the pipe from gz or bz2 |
| 71 | */ |
| 72 | while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512); |
| 73 | return(EXIT_FAILURE); |
| 74 | } |
| 75 | end = 1; |
Glenn L McGrath | e57feeb | 2003-11-14 12:57:14 +0000 | [diff] [blame] | 76 | return(EXIT_SUCCESS); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 77 | } |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 78 | end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 79 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 80 | /* Check header has valid magic, "ustar" is for the proper tar |
| 81 | * 0's are for the old tar format |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 82 | */ |
| 83 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 84 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 85 | if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0) |
| 86 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | bb_error_msg_and_die("Invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 88 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 89 | /* Do checksum on headers */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 90 | for (i = 0; i < 148 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 91 | sum += tar.raw[i]; |
| 92 | } |
| 93 | sum += ' ' * 8; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 94 | for (i = 156; i < 512 ; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 95 | sum += tar.raw[i]; |
| 96 | } |
| 97 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | bb_error_msg("Invalid tar header checksum"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 99 | return(EXIT_FAILURE); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 102 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 103 | if (longname) { |
| 104 | file_header->name = longname; |
| 105 | longname = NULL; |
| 106 | } |
| 107 | else if (linkname) { |
| 108 | file_header->name = linkname; |
| 109 | linkname = NULL; |
| 110 | } else |
| 111 | #endif |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 112 | { |
"Vladimir N. Oleynik" | 325da7b | 2006-03-01 05:39:41 +0000 | [diff] [blame] | 113 | file_header->name = bb_xstrndup(tar.formated.name,100); |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 114 | |
| 115 | if (tar.formated.prefix[0]) { |
| 116 | char *temp = file_header->name; |
| 117 | file_header->name = concat_path_file(tar.formated.prefix, temp); |
| 118 | free(temp); |
| 119 | } |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 120 | } |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 121 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 122 | file_header->uid = strtol(tar.formated.uid, NULL, 8); |
| 123 | file_header->gid = strtol(tar.formated.gid, NULL, 8); |
| 124 | file_header->size = strtol(tar.formated.size, NULL, 8); |
| 125 | file_header->mtime = strtol(tar.formated.mtime, NULL, 8); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 126 | file_header->link_name = (tar.formated.linkname[0] != '\0') ? |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | bb_xstrdup(tar.formated.linkname) : NULL; |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 128 | file_header->device = makedev(strtol(tar.formated.devmajor, NULL, 8), |
| 129 | strtol(tar.formated.devminor, NULL, 8)); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 130 | |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 131 | /* Set bits 0-11 of the files mode */ |
| 132 | file_header->mode = 07777 & strtol(tar.formated.mode, NULL, 8); |
| 133 | |
| 134 | /* Set bits 12-15 of the files mode */ |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 135 | switch (tar.formated.typeflag) { |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 136 | /* busybox identifies hard links as being regular files with 0 size and a link name */ |
| 137 | case '1': |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 138 | file_header->mode |= S_IFREG; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 139 | break; |
| 140 | case 'x': |
| 141 | case 'g': |
| 142 | bb_error_msg_and_die("pax is not tar"); |
| 143 | break; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 144 | case '7': |
| 145 | /* Reserved for high performance files, treat as normal file */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 146 | case 0: |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 147 | case '0': |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 148 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 149 | if (last_char_is(file_header->name, '/')) { |
| 150 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 151 | } else |
| 152 | #endif |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 153 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 154 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 155 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 156 | file_header->mode |= S_IFLNK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 157 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 158 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 159 | file_header->mode |= S_IFCHR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 160 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 161 | case '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 162 | file_header->mode |= S_IFBLK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 163 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 164 | case '5': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 165 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 166 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 167 | case '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 168 | file_header->mode |= S_IFIFO; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 169 | break; |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 170 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 171 | case 'L': { |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 172 | longname = xzalloc(file_header->size + 1); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 173 | archive_xread_all(archive_handle, longname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 174 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 175 | |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 176 | return(get_header_tar(archive_handle)); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 177 | } |
| 178 | case 'K': { |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 179 | linkname = xzalloc(file_header->size + 1); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 180 | archive_xread_all(archive_handle, linkname, file_header->size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 181 | archive_handle->offset += file_header->size; |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 182 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 183 | file_header->name = linkname; |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 184 | return(get_header_tar(archive_handle)); |
Glenn L McGrath | b3c4e9a | 2002-09-15 16:54:49 +0000 | [diff] [blame] | 185 | } |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 186 | case 'D': /* GNU dump dir */ |
| 187 | case 'M': /* Continuation of multi volume archive*/ |
| 188 | case 'N': /* Old GNU for names > 100 characters */ |
| 189 | case 'S': /* Sparse file */ |
| 190 | case 'V': /* Volume header */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 191 | bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 192 | #endif |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 193 | default: |
| 194 | bb_error_msg("Unknown typeflag: 0x%x", tar.formated.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 195 | } |
| 196 | { /* Strip trailing '/' in directories */ |
| 197 | /* Must be done after mode is set as '/' is used to check if its a directory */ |
| 198 | char *tmp = last_char_is(file_header->name, '/'); |
| 199 | if (tmp) { |
| 200 | *tmp = '\0'; |
| 201 | } |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 202 | } |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 203 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 204 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 205 | archive_handle->action_header(archive_handle->file_header); |
| 206 | archive_handle->flags |= ARCHIVE_EXTRACT_QUIET; |
| 207 | archive_handle->action_data(archive_handle); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 208 | llist_add_to(&(archive_handle->passed), file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 209 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 210 | data_skip(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 211 | } |
| 212 | archive_handle->offset += file_header->size; |
| 213 | |
Glenn L McGrath | 54ac057 | 2003-11-15 00:24:43 +0000 | [diff] [blame] | 214 | free(file_header->link_name); |
| 215 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 216 | return(EXIT_SUCCESS); |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 217 | } |