"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 | |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 15 | #include "unarchive.h" |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 16 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 17 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 18 | static char *longname = NULL; |
| 19 | static char *linkname = NULL; |
| 20 | #endif |
| 21 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 22 | /* NB: _DESTROYS_ str[len] character! */ |
| 23 | static unsigned long long getOctal(char *str, int len) |
| 24 | { |
| 25 | unsigned long long v; |
| 26 | /* Actually, tar header allows leading spaces also. |
| 27 | * Oh well, we will be liberal and skip this... |
| 28 | * The only downside probably is that we allow "-123" too :) |
| 29 | if (*str < '0' || *str > '7') |
| 30 | bb_error_msg_and_die("corrupted octal value in tar header"); |
| 31 | */ |
| 32 | str[len] = '\0'; |
| 33 | v = strtoull(str, &str, 8); |
| 34 | if (*str) |
| 35 | bb_error_msg_and_die("corrupted octal value in tar header"); |
| 36 | return v; |
| 37 | } |
| 38 | |
| 39 | void BUG_tar_header_size(void); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 40 | char get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 41 | { |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 42 | static int end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 44 | file_header_t *file_header = archive_handle->file_header; |
| 45 | struct { |
| 46 | /* ustar header, Posix 1003.1 */ |
| 47 | char name[100]; /* 0-99 */ |
| 48 | char mode[8]; /* 100-107 */ |
| 49 | char uid[8]; /* 108-115 */ |
| 50 | char gid[8]; /* 116-123 */ |
| 51 | char size[12]; /* 124-135 */ |
| 52 | char mtime[12]; /* 136-147 */ |
| 53 | char chksum[8]; /* 148-155 */ |
| 54 | char typeflag; /* 156-156 */ |
| 55 | char linkname[100]; /* 157-256 */ |
| 56 | char magic[6]; /* 257-262 */ |
| 57 | char version[2]; /* 263-264 */ |
| 58 | char uname[32]; /* 265-296 */ |
| 59 | char gname[32]; /* 297-328 */ |
| 60 | char devmajor[8]; /* 329-336 */ |
| 61 | char devminor[8]; /* 337-344 */ |
| 62 | char prefix[155]; /* 345-499 */ |
| 63 | char padding[12]; /* 500-512 */ |
| 64 | } tar; |
| 65 | char *cp; |
| 66 | int sum, i; |
| 67 | |
| 68 | if (sizeof(tar) != 512) |
| 69 | BUG_tar_header_size(); |
| 70 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 71 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 72 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 74 | xread(archive_handle->src_fd, &tar, 512); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 75 | archive_handle->offset += 512; |
| 76 | |
| 77 | /* If there is no filename its an empty header */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 78 | if (tar.name[0] == 0) { |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 79 | if (end) { |
| 80 | /* This is the second consecutive empty header! End of archive! |
| 81 | * Read until the end to empty the pipe from gz or bz2 |
| 82 | */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 83 | while (full_read(archive_handle->src_fd, &tar, 512) == 512) |
| 84 | /* repeat */; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 85 | return EXIT_FAILURE; |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 86 | } |
| 87 | end = 1; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 88 | return EXIT_SUCCESS; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 89 | } |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 90 | end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 91 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 92 | /* Check header has valid magic, "ustar" is for the proper tar |
| 93 | * 0's are for the old tar format |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 94 | */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 95 | if (strncmp(tar.magic, "ustar", 5) != 0) { |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 96 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 97 | if (memcmp(tar.magic, "\0\0\0\0", 5) != 0) |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 98 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 99 | bb_error_msg_and_die("invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 100 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 101 | /* Do checksum on headers */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 102 | sum = ' ' * sizeof(tar.chksum); |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 103 | for (i = 0; i < 148 ; i++) { |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 104 | sum += ((char*)&tar)[i]; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 105 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 106 | for (i = 156; i < 512 ; i++) { |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 107 | sum += ((char*)&tar)[i]; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 108 | } |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 109 | /* This field does not need special treatment (getOctal) */ |
| 110 | if (sum != xstrtoul(tar.chksum, 8)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 111 | bb_error_msg_and_die("invalid tar header checksum"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 114 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Glenn L McGrath | b7a76df | 2002-11-23 10:44:47 +0000 | [diff] [blame] | 115 | if (longname) { |
| 116 | file_header->name = longname; |
| 117 | longname = NULL; |
| 118 | } |
| 119 | else if (linkname) { |
| 120 | file_header->name = linkname; |
| 121 | linkname = NULL; |
| 122 | } else |
| 123 | #endif |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 124 | { |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 125 | file_header->name = xstrndup(tar.name, sizeof(tar.name)); |
| 126 | if (tar.prefix[0]) { |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 127 | char *temp = file_header->name; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 128 | file_header->name = concat_path_file(tar.prefix, temp); |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 129 | free(temp); |
| 130 | } |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 131 | } |
Glenn L McGrath | c5c1a8a | 2002-10-19 06:19:22 +0000 | [diff] [blame] | 132 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 133 | /* getOctal trashes subsequent field, therefore we call it |
| 134 | * on fields in reverse order */ |
| 135 | #define GET_OCTAL(a) getOctal((a), sizeof(a)) |
| 136 | if (tar.devmajor[0]) { |
| 137 | unsigned minor = GET_OCTAL(tar.devminor); |
| 138 | unsigned major = GET_OCTAL(tar.devmajor); |
| 139 | file_header->device = makedev(major, minor); |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 140 | } |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 141 | file_header->mtime = GET_OCTAL(tar.mtime); |
| 142 | file_header->size = GET_OCTAL(tar.size); |
| 143 | file_header->gid = GET_OCTAL(tar.gid); |
| 144 | file_header->uid = GET_OCTAL(tar.uid); |
| 145 | file_header->link_name = !tar.linkname[0] ? NULL : |
| 146 | xstrndup(tar.linkname, sizeof(tar.linkname)); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 147 | /* Set bits 0-11 of the files mode */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 148 | file_header->mode = 07777 & GET_OCTAL(tar.mode); |
| 149 | #undef GET_OCTAL |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 150 | |
| 151 | /* Set bits 12-15 of the files mode */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 152 | switch (tar.typeflag) { |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 153 | /* busybox identifies hard links as being regular files with 0 size and a link name */ |
| 154 | case '1': |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 155 | file_header->mode |= S_IFREG; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 156 | break; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 157 | case '7': |
| 158 | /* Reserved for high performance files, treat as normal file */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 159 | case 0: |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 160 | case '0': |
Bernhard Reutner-Fischer | a1bccc0 | 2006-04-02 20:17:55 +0000 | [diff] [blame] | 161 | #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 162 | if (last_char_is(file_header->name, '/')) { |
| 163 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 164 | } else |
| 165 | #endif |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 166 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 167 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 168 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 169 | file_header->mode |= S_IFLNK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 170 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 171 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 172 | file_header->mode |= S_IFCHR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 173 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 174 | case '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 175 | file_header->mode |= S_IFBLK; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 176 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 177 | case '5': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 178 | file_header->mode |= S_IFDIR; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 179 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 180 | case '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 181 | file_header->mode |= S_IFIFO; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 182 | break; |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 183 | #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 184 | case 'L': |
| 185 | longname = xzalloc(file_header->size + 1); |
| 186 | xread(archive_handle->src_fd, longname, file_header->size); |
| 187 | archive_handle->offset += file_header->size; |
| 188 | return get_header_tar(archive_handle); |
| 189 | case 'K': |
| 190 | linkname = xzalloc(file_header->size + 1); |
| 191 | xread(archive_handle->src_fd, linkname, file_header->size); |
| 192 | archive_handle->offset += file_header->size; |
| 193 | file_header->name = linkname; |
| 194 | return get_header_tar(archive_handle); |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 195 | case 'D': /* GNU dump dir */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 196 | case 'M': /* Continuation of multi volume archive */ |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 197 | case 'N': /* Old GNU for names > 100 characters */ |
| 198 | case 'S': /* Sparse file */ |
| 199 | case 'V': /* Volume header */ |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 200 | #endif |
Rob Landley | ff6e21c | 2006-07-06 20:30:19 +0000 | [diff] [blame] | 201 | case 'g': /* pax global header */ |
| 202 | case 'x': /* pax extended header */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 203 | bb_error_msg("ignoring extension type %c", tar.typeflag); |
Rob Landley | ff6e21c | 2006-07-06 20:30:19 +0000 | [diff] [blame] | 204 | break; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 205 | default: |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 206 | bb_error_msg("unknown typeflag: 0x%x", tar.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 207 | } |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame^] | 208 | |
| 209 | /* Strip trailing '/' in directories */ |
| 210 | /* Must be done after mode is set as '/' is used to check if its a directory */ |
| 211 | cp = last_char_is(file_header->name, '/'); |
| 212 | if (cp) *cp = '\0'; |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 213 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 214 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 215 | archive_handle->action_header(archive_handle->file_header); |
| 216 | archive_handle->flags |= ARCHIVE_EXTRACT_QUIET; |
| 217 | archive_handle->action_data(archive_handle); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 218 | llist_add_to(&(archive_handle->passed), file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 219 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 220 | data_skip(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 221 | } |
| 222 | archive_handle->offset += file_header->size; |
| 223 | |
Glenn L McGrath | 54ac057 | 2003-11-15 00:24:43 +0000 | [diff] [blame] | 224 | free(file_header->link_name); |
| 225 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 226 | return EXIT_SUCCESS; |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 227 | } |