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