26dfad55f8c12dd72f702b3598f2ddc3c89b3c9e
[oweals/busybox.git] / archival / libunarchive / get_header_tar.c
1 /*
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.
15  *
16  *  FIXME:
17  *    In privileged mode if uname and gname map to a uid and gid then use the
18  *    mapped value instead of the uid/gid values in tar header
19  *
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
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/sysmacros.h>      /* For makedev */
30 #include "unarchive.h"
31 #include "libbb.h"
32
33 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
34 static char *longname = NULL;
35 static char *linkname = NULL;
36 #endif
37
38 extern char get_header_tar(archive_handle_t *archive_handle)
39 {
40         file_header_t *file_header = archive_handle->file_header;
41         union {
42                 /* ustar header, Posix 1003.1 */
43                 unsigned char raw[512];
44                 struct {
45                         char name[100]; /*   0-99 */
46                         char mode[8];   /* 100-107 */
47                         char uid[8];    /* 108-115 */
48                         char gid[8];    /* 116-123 */
49                         char size[12];  /* 124-135 */
50                         char mtime[12]; /* 136-147 */
51                         char chksum[8]; /* 148-155 */
52                         char typeflag;  /* 156-156 */
53                         char linkname[100];     /* 157-256 */
54                         char magic[6];  /* 257-262 */
55                         char version[2];        /* 263-264 */
56                         char uname[32]; /* 265-296 */
57                         char gname[32]; /* 297-328 */
58                         char devmajor[8];       /* 329-336 */
59                         char devminor[8];       /* 337-344 */
60                         char prefix[155];       /* 345-499 */
61                         char padding[12];       /* 500-512 */
62                 } formated;
63         } tar;
64         long sum = 0;
65         long i;
66         static int end = 0;
67
68         /* Align header */
69         data_align(archive_handle, 512);
70
71         if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
72                 /* Assume end of file */
73                 return(EXIT_FAILURE);
74         }
75         archive_handle->offset += 512;
76
77         /* If there is no filename its an empty header */
78         if (tar.formated.name[0] == 0) {
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                          */
83                         while (bb_full_read(archive_handle->src_fd, tar.raw, 512) == 512);
84                         return(EXIT_FAILURE);
85                 }
86                 end = 1;
87                 return(EXIT_SUCCESS);
88         }
89         end = 0;
90
91         /* Check header has valid magic, "ustar" is for the proper tar
92          * 0's are for the old tar format
93          */
94         if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
95 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
96                 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
97 #endif
98                         bb_error_msg_and_die("Invalid tar magic");
99         }
100         /* Do checksum on headers */
101         for (i =  0; i < 148 ; i++) {
102                 sum += tar.raw[i];
103         }
104         sum += ' ' * 8;
105         for (i =  156; i < 512 ; i++) {
106                 sum += tar.raw[i];
107         }
108         if (sum != strtol(tar.formated.chksum, NULL, 8)) {
109                 bb_error_msg("Invalid tar header checksum");
110                 return(EXIT_FAILURE);
111         }
112
113 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
114         if (longname) {
115                 file_header->name = longname;
116                 longname = NULL;
117         }
118         else if (linkname) {
119                 file_header->name = linkname;
120                 linkname = NULL;
121         } else
122 #endif
123         if (tar.formated.prefix[0] == 0) {
124                 file_header->name = strdup(tar.formated.name);
125         } else {
126                 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
127         }
128
129         file_header->uid = strtol(tar.formated.uid, NULL, 8);
130         file_header->gid = strtol(tar.formated.gid, NULL, 8);
131         file_header->size = strtol(tar.formated.size, NULL, 8);
132         file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
133         file_header->link_name = (tar.formated.linkname[0] != '\0') ?
134             bb_xstrdup(tar.formated.linkname) : NULL;
135         file_header->device = makedev(strtol(tar.formated.devmajor, NULL, 8),
136                 strtol(tar.formated.devminor, NULL, 8));
137
138         /* Set bits 0-11 of the files mode */
139         file_header->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
140
141         /* Set bits 12-15 of the files mode */
142         switch (tar.formated.typeflag) {
143         /* busybox identifies hard links as being regular files with 0 size and a link name */
144         case '1':
145                 file_header->mode |= S_IFREG;
146                 break;
147         case 'x':
148         case 'g':
149                 bb_error_msg_and_die("pax is not tar");
150                 break;
151         case '7':
152                 /* Reserved for high performance files, treat as normal file */
153         case 0:
154         case '0':
155 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
156                 if (last_char_is(file_header->name, '/')) {
157                         file_header->mode |= S_IFDIR;
158                 } else
159 #endif
160                         file_header->mode |= S_IFREG;
161                 break;
162         case '2':
163                 file_header->mode |= S_IFLNK;
164                 break;
165         case '3':
166                 file_header->mode |= S_IFCHR;
167                 break;
168         case '4':
169                 file_header->mode |= S_IFBLK;
170                 break;
171         case '5':
172                 file_header->mode |= S_IFDIR;
173                 break;
174         case '6':
175                 file_header->mode |= S_IFIFO;
176                 break;
177 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
178         case 'L': {
179                         longname = xmalloc(file_header->size + 1);
180                         archive_xread_all(archive_handle, longname, file_header->size);
181                         longname[file_header->size] = '\0';
182                         archive_handle->offset += file_header->size;
183
184                         return(get_header_tar(archive_handle));
185                 }
186         case 'K': {
187                         linkname = xmalloc(file_header->size + 1);
188                         archive_xread_all(archive_handle, linkname, file_header->size);
189                         linkname[file_header->size] = '\0';
190                         archive_handle->offset += file_header->size;
191
192                         file_header->name = linkname;
193                         return(get_header_tar(archive_handle));
194                 }
195         case 'D':       /* GNU dump dir */
196         case 'M':       /* Continuation of multi volume archive*/
197         case 'N':       /* Old GNU for names > 100 characters */
198         case 'S':       /* Sparse file */
199         case 'V':       /* Volume header */
200                 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
201 #endif
202         default:
203                 bb_error_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
204         }
205         {       /* Strip trailing '/' in directories */
206                 /* Must be done after mode is set as '/' is used to check if its a directory */
207                 char *tmp = last_char_is(file_header->name, '/');
208                 if (tmp) {
209                         *tmp = '\0';
210                 }
211         }
212
213         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
214                 archive_handle->action_header(archive_handle->file_header);
215                 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
216                 archive_handle->action_data(archive_handle);
217                 archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
218         } else {
219                 data_skip(archive_handle);
220         }
221         archive_handle->offset += file_header->size;
222
223         free(file_header->link_name);
224
225         return(EXIT_SUCCESS);
226 }