blob: 513909d5f83a7c81f7f1852a108039aeb30342f8 [file] [log] [blame]
Glenn L McGrath95ebf612001-10-25 14:18:08 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Glenn L McGrath6aa52232004-02-17 11:55:06 +000015 *
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +000016 * FIXME:
Eric Andersenaff114c2004-04-14 17:51:38 +000017 * In privileged mode if uname and gname map to a uid and gid then use the
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +000018 * mapped value instead of the uid/gid values in tar header
Glenn L McGrathb0e163a2004-02-19 08:48:30 +000019 *
20 * References:
21 * GNU tar and star man pages,
22 * Opengroup's ustar interchange format,
23 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
Glenn L McGrath95ebf612001-10-25 14:18:08 +000024 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include "unarchive.h"
30#include "libbb.h"
31
Glenn L McGrath21110a02003-01-28 01:45:48 +000032#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000033static char *longname = NULL;
34static char *linkname = NULL;
35#endif
36
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000037extern char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000038{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039 file_header_t *file_header = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000040 union {
Glenn L McGrathef91bf62003-09-12 06:31:28 +000041 /* ustar header, Posix 1003.1 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000042 unsigned char raw[512];
43 struct {
Glenn L McGrath99b12542002-08-22 17:47:09 +000044 char name[100]; /* 0-99 */
45 char mode[8]; /* 100-107 */
46 char uid[8]; /* 108-115 */
47 char gid[8]; /* 116-123 */
48 char size[12]; /* 124-135 */
49 char mtime[12]; /* 136-147 */
50 char chksum[8]; /* 148-155 */
51 char typeflag; /* 156-156 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000052 char linkname[100]; /* 157-256 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000053 char magic[6]; /* 257-262 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000054 char version[2]; /* 263-264 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000055 char uname[32]; /* 265-296 */
56 char gname[32]; /* 297-328 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000057 char devmajor[8]; /* 329-336 */
58 char devminor[8]; /* 337-344 */
59 char prefix[155]; /* 345-499 */
60 char padding[12]; /* 500-512 */
61 } formated;
62 } tar;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000063 long sum = 0;
64 long i;
Paul Fox94ff9f12005-07-20 19:24:13 +000065 static int end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000066
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000067 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000068 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000069
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000070 if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
Glenn L McGrath7f2a9532002-11-05 02:56:57 +000071 /* Assume end of file */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000072 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000073 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000074 archive_handle->offset += 512;
75
76 /* If there is no filename its an empty header */
77 if (tar.formated.name[0] == 0) {
Paul Fox94ff9f12005-07-20 19:24:13 +000078 if (end) {
79 /* This is the second consecutive empty header! End of archive!
80 * Read until the end to empty the pipe from gz or bz2
81 */
82 while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512);
83 return(EXIT_FAILURE);
84 }
85 end = 1;
Glenn L McGrathe57feeb2003-11-14 12:57:14 +000086 return(EXIT_SUCCESS);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000087 }
Paul Fox94ff9f12005-07-20 19:24:13 +000088 end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000089
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000090 /* Check header has valid magic, "ustar" is for the proper tar
91 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +000092 */
93 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
Glenn L McGrath21110a02003-01-28 01:45:48 +000094#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000095 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
96#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 bb_error_msg_and_die("Invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000098 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000099 /* Do checksum on headers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000100 for (i = 0; i < 148 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000101 sum += tar.raw[i];
102 }
103 sum += ' ' * 8;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000104 for (i = 156; i < 512 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000105 sum += tar.raw[i];
106 }
107 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_error_msg("Invalid tar header checksum");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000109 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000110 }
111
Glenn L McGrath21110a02003-01-28 01:45:48 +0000112#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000113 if (longname) {
114 file_header->name = longname;
115 longname = NULL;
116 }
117 else if (linkname) {
118 file_header->name = linkname;
119 linkname = NULL;
120 } else
121#endif
Glenn L McGrath75762702002-08-22 11:50:31 +0000122 if (tar.formated.prefix[0] == 0) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000123 file_header->name = strdup(tar.formated.name);
Glenn L McGrath75762702002-08-22 11:50:31 +0000124 } else {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000125 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
Glenn L McGrath75762702002-08-22 11:50:31 +0000126 }
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +0000127
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000128 file_header->uid = strtol(tar.formated.uid, NULL, 8);
129 file_header->gid = strtol(tar.formated.gid, NULL, 8);
130 file_header->size = strtol(tar.formated.size, NULL, 8);
131 file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000132 file_header->link_name = (tar.formated.linkname[0] != '\0') ?
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 bb_xstrdup(tar.formated.linkname) : NULL;
Eric Andersen4f807a82004-07-26 09:11:12 +0000134 file_header->device = makedev(strtol(tar.formated.devmajor, NULL, 8),
135 strtol(tar.formated.devminor, NULL, 8));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000136
Glenn L McGrath916ba532004-02-20 02:34:42 +0000137 /* Set bits 0-11 of the files mode */
138 file_header->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
139
140 /* Set bits 12-15 of the files mode */
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000141 switch (tar.formated.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000142 /* busybox identifies hard links as being regular files with 0 size and a link name */
143 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000144 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000145 break;
146 case 'x':
147 case 'g':
148 bb_error_msg_and_die("pax is not tar");
149 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000150 case '7':
151 /* Reserved for high performance files, treat as normal file */
Glenn L McGrath99b12542002-08-22 17:47:09 +0000152 case 0:
Glenn L McGrath21110a02003-01-28 01:45:48 +0000153 case '0':
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000154#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000155 if (last_char_is(file_header->name, '/')) {
156 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000157 } else
158#endif
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000159 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000160 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000161 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000162 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000163 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000164 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000165 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000166 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000167 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000168 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000169 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000170 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000171 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000172 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000173 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000174 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000175 break;
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000176#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000177 case 'L': {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000178 longname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000179 archive_xread_all(archive_handle, longname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000180 longname[file_header->size] = '\0';
181 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000182
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000183 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000184 }
185 case 'K': {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000186 linkname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000187 archive_xread_all(archive_handle, linkname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000188 linkname[file_header->size] = '\0';
189 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000190
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000191 file_header->name = linkname;
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000192 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000193 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000194 case 'D': /* GNU dump dir */
195 case 'M': /* Continuation of multi volume archive*/
196 case 'N': /* Old GNU for names > 100 characters */
197 case 'S': /* Sparse file */
198 case 'V': /* Volume header */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000199 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000200#endif
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000201 default:
202 bb_error_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000203 }
204 { /* Strip trailing '/' in directories */
205 /* Must be done after mode is set as '/' is used to check if its a directory */
206 char *tmp = last_char_is(file_header->name, '/');
207 if (tmp) {
208 *tmp = '\0';
209 }
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000210 }
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000211
Glenn L McGrath8e940982002-11-04 23:47:31 +0000212 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000213 archive_handle->action_header(archive_handle->file_header);
214 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
215 archive_handle->action_data(archive_handle);
Glenn L McGrathf074afc2003-11-17 21:58:00 +0000216 archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000217 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000218 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000219 }
220 archive_handle->offset += file_header->size;
221
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000222 free(file_header->link_name);
223
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000224 return(EXIT_SUCCESS);
Eric Andersen2276d832002-07-11 11:11:56 +0000225}