blob: cd38f4560e54e9b5d8e4bf57622a7b02a02dcc8b [file] [log] [blame]
Rob Landley06249fe2006-02-20 19:28:53 +00001/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath6aa52232004-02-17 11:55:06 +00002 *
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00003 * FIXME:
Eric Andersenaff114c2004-04-14 17:51:38 +00004 * In privileged mode if uname and gname map to a uid and gid then use the
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00005 * mapped value instead of the uid/gid values in tar header
Glenn L McGrathb0e163a2004-02-19 08:48:30 +00006 *
7 * References:
8 * GNU tar and star man pages,
9 * Opengroup's ustar interchange format,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000010 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
Glenn L McGrath95ebf612001-10-25 14:18:08 +000011 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
Rob Landley161aae72005-10-11 20:17:30 +000016#include <sys/sysmacros.h> /* For makedev */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000017#include "unarchive.h"
18#include "libbb.h"
19
Glenn L McGrath21110a02003-01-28 01:45:48 +000020#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000021static char *longname = NULL;
22static char *linkname = NULL;
23#endif
24
Rob Landleydfba7412006-03-06 20:47:33 +000025char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000026{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000027 file_header_t *file_header = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000028 union {
Glenn L McGrathef91bf62003-09-12 06:31:28 +000029 /* ustar header, Posix 1003.1 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000030 unsigned char raw[512];
31 struct {
Glenn L McGrath99b12542002-08-22 17:47:09 +000032 char name[100]; /* 0-99 */
33 char mode[8]; /* 100-107 */
34 char uid[8]; /* 108-115 */
35 char gid[8]; /* 116-123 */
36 char size[12]; /* 124-135 */
37 char mtime[12]; /* 136-147 */
38 char chksum[8]; /* 148-155 */
39 char typeflag; /* 156-156 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000040 char linkname[100]; /* 157-256 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000041 char magic[6]; /* 257-262 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000042 char version[2]; /* 263-264 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000043 char uname[32]; /* 265-296 */
44 char gname[32]; /* 297-328 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000045 char devmajor[8]; /* 329-336 */
46 char devminor[8]; /* 337-344 */
47 char prefix[155]; /* 345-499 */
48 char padding[12]; /* 500-512 */
49 } formated;
50 } tar;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000051 long sum = 0;
52 long i;
Paul Fox94ff9f12005-07-20 19:24:13 +000053 static int end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000054
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000055 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000056 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000057
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000058 if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
Glenn L McGrath7f2a9532002-11-05 02:56:57 +000059 /* Assume end of file */
Rob Landley06249fe2006-02-20 19:28:53 +000060 bb_error_msg_and_die("Short header");
61 //return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000062 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000063 archive_handle->offset += 512;
64
65 /* If there is no filename its an empty header */
66 if (tar.formated.name[0] == 0) {
Paul Fox94ff9f12005-07-20 19:24:13 +000067 if (end) {
68 /* This is the second consecutive empty header! End of archive!
69 * Read until the end to empty the pipe from gz or bz2
70 */
71 while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512);
72 return(EXIT_FAILURE);
73 }
74 end = 1;
Glenn L McGrathe57feeb2003-11-14 12:57:14 +000075 return(EXIT_SUCCESS);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000076 }
Paul Fox94ff9f12005-07-20 19:24:13 +000077 end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000078
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000079 /* Check header has valid magic, "ustar" is for the proper tar
80 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +000081 */
82 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +000083#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000084 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
85#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 bb_error_msg_and_die("Invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000087 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000088 /* Do checksum on headers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000089 for (i = 0; i < 148 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000090 sum += tar.raw[i];
91 }
92 sum += ' ' * 8;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000093 for (i = 156; i < 512 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000094 sum += tar.raw[i];
95 }
96 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 bb_error_msg("Invalid tar header checksum");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000098 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000099 }
100
Glenn L McGrath21110a02003-01-28 01:45:48 +0000101#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000102 if (longname) {
103 file_header->name = longname;
104 longname = NULL;
105 }
106 else if (linkname) {
107 file_header->name = linkname;
108 linkname = NULL;
109 } else
110#endif
Rob Landley06249fe2006-02-20 19:28:53 +0000111 {
"Vladimir N. Oleynik"325da7b2006-03-01 05:39:41 +0000112 file_header->name = bb_xstrndup(tar.formated.name,100);
Rob Landley06249fe2006-02-20 19:28:53 +0000113
114 if (tar.formated.prefix[0]) {
115 char *temp = file_header->name;
116 file_header->name = concat_path_file(tar.formated.prefix, temp);
117 free(temp);
118 }
Glenn L McGrath75762702002-08-22 11:50:31 +0000119 }
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +0000120
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000121 file_header->uid = strtol(tar.formated.uid, NULL, 8);
122 file_header->gid = strtol(tar.formated.gid, NULL, 8);
123 file_header->size = strtol(tar.formated.size, NULL, 8);
124 file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000125 file_header->link_name = (tar.formated.linkname[0] != '\0') ?
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 bb_xstrdup(tar.formated.linkname) : NULL;
Eric Andersen4f807a82004-07-26 09:11:12 +0000127 file_header->device = makedev(strtol(tar.formated.devmajor, NULL, 8),
128 strtol(tar.formated.devminor, NULL, 8));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000129
Glenn L McGrath916ba532004-02-20 02:34:42 +0000130 /* Set bits 0-11 of the files mode */
131 file_header->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
132
133 /* Set bits 12-15 of the files mode */
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000134 switch (tar.formated.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000135 /* busybox identifies hard links as being regular files with 0 size and a link name */
136 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000137 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000138 break;
139 case 'x':
140 case 'g':
141 bb_error_msg_and_die("pax is not tar");
142 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000143 case '7':
144 /* Reserved for high performance files, treat as normal file */
Glenn L McGrath99b12542002-08-22 17:47:09 +0000145 case 0:
Glenn L McGrath21110a02003-01-28 01:45:48 +0000146 case '0':
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +0000147#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000148 if (last_char_is(file_header->name, '/')) {
149 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000150 } else
151#endif
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000152 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000153 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000154 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000155 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000156 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000157 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000158 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000159 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000160 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000161 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000162 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000163 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000164 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000165 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000166 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000167 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000168 break;
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000169#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000170 case 'L': {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000171 longname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000172 archive_xread_all(archive_handle, longname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000173 longname[file_header->size] = '\0';
174 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000175
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000176 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000177 }
178 case 'K': {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000179 linkname = xmalloc(file_header->size + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000180 archive_xread_all(archive_handle, linkname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000181 linkname[file_header->size] = '\0';
182 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000183
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000184 file_header->name = linkname;
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000185 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000186 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000187 case 'D': /* GNU dump dir */
188 case 'M': /* Continuation of multi volume archive*/
189 case 'N': /* Old GNU for names > 100 characters */
190 case 'S': /* Sparse file */
191 case 'V': /* Volume header */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000192 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000193#endif
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000194 default:
195 bb_error_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000196 }
197 { /* Strip trailing '/' in directories */
198 /* Must be done after mode is set as '/' is used to check if its a directory */
199 char *tmp = last_char_is(file_header->name, '/');
200 if (tmp) {
201 *tmp = '\0';
202 }
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000203 }
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000204
Glenn L McGrath8e940982002-11-04 23:47:31 +0000205 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000206 archive_handle->action_header(archive_handle->file_header);
207 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
208 archive_handle->action_data(archive_handle);
Glenn L McGrathf074afc2003-11-17 21:58:00 +0000209 archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000210 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000211 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000212 }
213 archive_handle->offset += file_header->size;
214
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000215 free(file_header->link_name);
216
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000217 return(EXIT_SUCCESS);
Eric Andersen2276d832002-07-11 11:11:56 +0000218}