3
0

get_header_tar.c 6.2 KB

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