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