Don't hose up perms for files that happen to have symlinks
[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
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "unarchive.h"
21 #include "libbb.h"
22
23 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
24 static char *longname = NULL;
25 static char *linkname = NULL;
26 #endif
27
28 extern char get_header_tar(archive_handle_t *archive_handle)
29 {
30         file_header_t *file_header = archive_handle->file_header;
31         union {
32                 /* ustar header, Posix 1003.1 */
33                 unsigned char raw[512];
34                 struct {
35                         char name[100]; /*   0-99 */
36                         char mode[8];   /* 100-107 */
37                         char uid[8];    /* 108-115 */
38                         char gid[8];    /* 116-123 */
39                         char size[12];  /* 124-135 */
40                         char mtime[12]; /* 136-147 */
41                         char chksum[8]; /* 148-155 */
42                         char typeflag;  /* 156-156 */
43                         char linkname[100];     /* 157-256 */
44                         char magic[6];  /* 257-262 */
45                         char version[2];        /* 263-264 */
46                         char uname[32]; /* 265-296 */
47                         char gname[32]; /* 297-328 */
48                         char devmajor[8];       /* 329-336 */
49                         char devminor[8];       /* 337-344 */
50                         char prefix[155];       /* 345-499 */
51                         char padding[12];       /* 500-512 */
52                 } formated;
53         } tar;
54         long sum = 0;
55         long i;
56
57         /* Align header */
58         data_align(archive_handle, 512);
59
60         if (bb_full_read(archive_handle->src_fd, tar.raw, 512) != 512) {
61                 /* Assume end of file */
62                 return(EXIT_FAILURE);
63         }
64         archive_handle->offset += 512;
65
66         /* If there is no filename its an empty header */
67         if (tar.formated.name[0] == 0) {
68                 return(EXIT_SUCCESS);
69         }
70
71         /* Check header has valid magic, "ustar" is for the proper tar
72          * 0's are for the old tar format
73          */
74         if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
75 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
76                 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
77 #endif
78                         bb_error_msg_and_die("Invalid tar magic");
79         }
80         /* Do checksum on headers */
81         for (i =  0; i < 148 ; i++) {
82                 sum += tar.raw[i];
83         }
84         sum += ' ' * 8;
85         for (i =  156; i < 512 ; i++) {
86                 sum += tar.raw[i];
87         }
88         if (sum != strtol(tar.formated.chksum, NULL, 8)) {
89                 bb_error_msg("Invalid tar header checksum");
90                 return(EXIT_FAILURE);
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         if (tar.formated.prefix[0] == 0) {
104                 file_header->name = strdup(tar.formated.name);
105         } else {
106                 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
107         }
108
109         {       /* Strip trailing '/' in directories */
110                 char *tmp = last_char_is(file_header->name, '/');
111                 if (tmp) {
112                         *tmp = '\0';
113                 }
114         }
115
116
117         file_header->mode = strtol(tar.formated.mode, NULL, 8);
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 = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
125                                  strtol(tar.formated.devminor, NULL, 8));
126
127         /* Fix mode, used by the old format */
128         switch (tar.formated.typeflag) {
129 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
130         case 0:
131         case '0':
132                 if (last_char_is(file_header->name, '/')) {
133                         file_header->mode |= S_IFDIR;
134                 } else {
135                         file_header->mode |= S_IFREG;
136                 }
137                 break;
138         case '2':
139                 file_header->mode |= S_IFLNK;
140                 break;
141         case '3':
142                 file_header->mode |= S_IFCHR;
143                 break;
144         case '4':
145                 file_header->mode |= S_IFBLK;
146                 break;
147         case '5':
148                 file_header->mode |= S_IFDIR;
149                 break;
150         case '6':
151                 file_header->mode |= S_IFIFO;
152                 break;
153 # endif
154         /* hard links are detected as regular files with 0 size and a link name */
155         case '1':
156                 file_header->mode &= (S_IFREG | 07777);
157                 break;
158 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
159         case 'L': {
160                         longname = xmalloc(file_header->size + 1);
161                         archive_xread_all(archive_handle, longname, file_header->size);
162                         longname[file_header->size] = '\0';
163                         archive_handle->offset += file_header->size;
164
165                         return(get_header_tar(archive_handle));
166                 }
167         case 'K': {
168                         linkname = xmalloc(file_header->size + 1);
169                         archive_xread_all(archive_handle, linkname, file_header->size);
170                         linkname[file_header->size] = '\0';
171                         archive_handle->offset += file_header->size;
172
173                         file_header->name = linkname;
174                         return(get_header_tar(archive_handle));
175                 }
176         case 'D':
177         case 'M':
178         case 'N':
179         case 'S':
180         case 'V':
181                 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
182 # endif
183         }
184
185         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
186                 archive_handle->action_header(archive_handle->file_header);
187                 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
188                 archive_handle->action_data(archive_handle);
189                 archive_handle->passed = llist_add_to(archive_handle->passed, file_header->name);
190         } else {
191                 data_skip(archive_handle);                      
192         }
193         archive_handle->offset += file_header->size;
194
195         free(file_header->link_name);
196
197         return(EXIT_SUCCESS);
198 }