tar:
[oweals/busybox.git] / archival / libunarchive / get_header_tar.c
1 /* vi: set sw=4 ts=4: */
2 /* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
3  *
4  *  FIXME:
5  *    In privileged mode if uname and gname map to a uid and gid then use the
6  *    mapped value instead of the uid/gid values in tar header
7  *
8  *  References:
9  *    GNU tar and star man pages,
10  *    Opengroup's ustar interchange format,
11  *      http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
12  */
13
14 #include "libbb.h"
15 #include "unarchive.h"
16
17 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
18 static char *longname = NULL;
19 static char *linkname = NULL;
20 #endif
21
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);
40 char get_header_tar(archive_handle_t *archive_handle)
41 {
42         static int end = 0;
43
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
71         /* Align header */
72         data_align(archive_handle, 512);
73
74         xread(archive_handle->src_fd, &tar, 512);
75         archive_handle->offset += 512;
76
77         /* If there is no filename its an empty header */
78         if (tar.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 (full_read(archive_handle->src_fd, &tar, 512) == 512)
84                                 /* repeat */;
85                         return EXIT_FAILURE;
86                 }
87                 end = 1;
88                 return EXIT_SUCCESS;
89         }
90         end = 0;
91
92         /* Check header has valid magic, "ustar" is for the proper tar
93          * 0's are for the old tar format
94          */
95         if (strncmp(tar.magic, "ustar", 5) != 0) {
96 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
97                 if (memcmp(tar.magic, "\0\0\0\0", 5) != 0)
98 #endif
99                         bb_error_msg_and_die("invalid tar magic");
100         }
101         /* Do checksum on headers */
102         sum = ' ' * sizeof(tar.chksum);
103         for (i = 0; i < 148 ; i++) {
104                 sum += ((char*)&tar)[i];
105         }
106         for (i = 156; i < 512 ; i++) {
107                 sum += ((char*)&tar)[i];
108         }
109         /* This field does not need special treatment (getOctal) */
110         if (sum != xstrtoul(tar.chksum, 8)) {
111                 bb_error_msg_and_die("invalid tar header checksum");
112         }
113
114 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
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
124         {
125                 file_header->name = xstrndup(tar.name, sizeof(tar.name));
126                 if (tar.prefix[0]) {
127                         char *temp = file_header->name;
128                         file_header->name = concat_path_file(tar.prefix, temp);
129                         free(temp);
130                 }
131         }
132
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);
140         }
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));
147         /* Set bits 0-11 of the files mode */
148         file_header->mode = 07777 & GET_OCTAL(tar.mode);
149 #undef GET_OCTAL
150
151         /* Set bits 12-15 of the files mode */
152         switch (tar.typeflag) {
153         /* busybox identifies hard links as being regular files with 0 size and a link name */
154         case '1':
155                 file_header->mode |= S_IFREG;
156                 break;
157         case '7':
158                 /* Reserved for high performance files, treat as normal file */
159         case 0:
160         case '0':
161 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
162                 if (last_char_is(file_header->name, '/')) {
163                         file_header->mode |= S_IFDIR;
164                 } else
165 #endif
166                 file_header->mode |= S_IFREG;
167                 break;
168         case '2':
169                 file_header->mode |= S_IFLNK;
170                 break;
171         case '3':
172                 file_header->mode |= S_IFCHR;
173                 break;
174         case '4':
175                 file_header->mode |= S_IFBLK;
176                 break;
177         case '5':
178                 file_header->mode |= S_IFDIR;
179                 break;
180         case '6':
181                 file_header->mode |= S_IFIFO;
182                 break;
183 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
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);
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 #endif
201         case 'g':       /* pax global header */
202         case 'x':       /* pax extended header */
203                 bb_error_msg("ignoring extension type %c", tar.typeflag);
204                 break;
205         default:
206                 bb_error_msg("unknown typeflag: 0x%x", tar.typeflag);
207         }
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';
213
214         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
215                 archive_handle->action_header(archive_handle->file_header);
216                 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
217                 archive_handle->action_data(archive_handle);
218                 llist_add_to(&(archive_handle->passed), file_header->name);
219         } else {
220                 data_skip(archive_handle);
221         }
222         archive_handle->offset += file_header->size;
223
224         free(file_header->link_name);
225
226         return EXIT_SUCCESS;
227 }