get_header_tar.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include "libbb.h"
  14. #include "unarchive.h"
  15. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  16. static char *longname;
  17. static char *linkname;
  18. #else
  19. enum {
  20. longname = 0,
  21. linkname = 0,
  22. };
  23. #endif
  24. /* NB: _DESTROYS_ str[len] character! */
  25. static unsigned long long getOctal(char *str, int len)
  26. {
  27. unsigned long long v;
  28. /* Actually, tar header allows leading spaces also.
  29. * Oh well, we will be liberal and skip this...
  30. * The only downside probably is that we allow "-123" too :)
  31. if (*str < '0' || *str > '7')
  32. bb_error_msg_and_die("corrupted octal value in tar header");
  33. */
  34. str[len] = '\0';
  35. v = strtoull(str, &str, 8);
  36. if (*str)
  37. bb_error_msg_and_die("corrupted octal value in tar header");
  38. return v;
  39. }
  40. #define GET_OCTAL(a) getOctal((a), sizeof(a))
  41. void BUG_tar_header_size(void);
  42. char get_header_tar(archive_handle_t *archive_handle)
  43. {
  44. static int end;
  45. file_header_t *file_header = archive_handle->file_header;
  46. struct {
  47. /* ustar header, Posix 1003.1 */
  48. char name[100]; /* 0-99 */
  49. char mode[8]; /* 100-107 */
  50. char uid[8]; /* 108-115 */
  51. char gid[8]; /* 116-123 */
  52. char size[12]; /* 124-135 */
  53. char mtime[12]; /* 136-147 */
  54. char chksum[8]; /* 148-155 */
  55. char typeflag; /* 156-156 */
  56. char linkname[100]; /* 157-256 */
  57. char magic[6]; /* 257-262 */
  58. char version[2]; /* 263-264 */
  59. char uname[32]; /* 265-296 */
  60. char gname[32]; /* 297-328 */
  61. char devmajor[8]; /* 329-336 */
  62. char devminor[8]; /* 337-344 */
  63. char prefix[155]; /* 345-499 */
  64. char padding[12]; /* 500-512 */
  65. } tar;
  66. char *cp;
  67. int sum, i;
  68. int parse_names;
  69. if (sizeof(tar) != 512)
  70. BUG_tar_header_size();
  71. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  72. again:
  73. #endif
  74. /* Align header */
  75. data_align(archive_handle, 512);
  76. again_after_align:
  77. xread(archive_handle->src_fd, &tar, 512);
  78. archive_handle->offset += 512;
  79. /* If there is no filename its an empty header */
  80. if (tar.name[0] == 0) {
  81. if (end) {
  82. /* This is the second consecutive empty header! End of archive!
  83. * Read until the end to empty the pipe from gz or bz2
  84. */
  85. while (full_read(archive_handle->src_fd, &tar, 512) == 512)
  86. /* repeat */;
  87. return EXIT_FAILURE;
  88. }
  89. end = 1;
  90. return EXIT_SUCCESS;
  91. }
  92. end = 0;
  93. /* Check header has valid magic, "ustar" is for the proper tar
  94. * 0's are for the old tar format
  95. */
  96. if (strncmp(tar.magic, "ustar", 5) != 0) {
  97. #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  98. if (memcmp(tar.magic, "\0\0\0\0", 5) != 0)
  99. #endif
  100. bb_error_msg_and_die("invalid tar magic");
  101. }
  102. /* Do checksum on headers */
  103. sum = ' ' * sizeof(tar.chksum);
  104. for (i = 0; i < 148 ; i++) {
  105. sum += ((char*)&tar)[i];
  106. }
  107. for (i = 156; i < 512 ; i++) {
  108. sum += ((char*)&tar)[i];
  109. }
  110. /* This field does not need special treatment (getOctal) */
  111. if (sum != xstrtoul(tar.chksum, 8)) {
  112. bb_error_msg_and_die("invalid tar header checksum");
  113. }
  114. /* 0 is reserved for high perf file, treat as normal file */
  115. if (!tar.typeflag) tar.typeflag = '0';
  116. parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
  117. /* getOctal trashes subsequent field, therefore we call it
  118. * on fields in reverse order */
  119. if (tar.devmajor[0]) {
  120. unsigned minor = GET_OCTAL(tar.devminor);
  121. unsigned major = GET_OCTAL(tar.devmajor);
  122. file_header->device = makedev(major, minor);
  123. }
  124. file_header->link_name = NULL;
  125. if (!linkname && parse_names && tar.linkname[0]) {
  126. /* we trash magic[0] here, it's ok */
  127. tar.linkname[sizeof(tar.linkname)] = '\0';
  128. file_header->link_name = xstrdup(tar.linkname);
  129. /* FIXME: what if we have non-link object with link_name? */
  130. /* Will link_name be free()ed? */
  131. }
  132. file_header->mtime = GET_OCTAL(tar.mtime);
  133. file_header->size = GET_OCTAL(tar.size);
  134. file_header->gid = GET_OCTAL(tar.gid);
  135. file_header->uid = GET_OCTAL(tar.uid);
  136. /* Set bits 0-11 of the files mode */
  137. file_header->mode = 07777 & GET_OCTAL(tar.mode);
  138. file_header->name = NULL;
  139. if (!longname && parse_names) {
  140. /* we trash mode[0] here, it's ok */
  141. tar.name[sizeof(tar.name)] = '\0';
  142. if (tar.prefix[0]) {
  143. /* and padding[0] */
  144. tar.prefix[sizeof(tar.prefix)] = '\0';
  145. file_header->name = concat_path_file(tar.prefix, tar.name);
  146. } else
  147. file_header->name = xstrdup(tar.name);
  148. }
  149. /* Set bits 12-15 of the files mode */
  150. /* (typeflag was not trashed because chksum does not use getOctal) */
  151. switch (tar.typeflag) {
  152. /* busybox identifies hard links as being regular files with 0 size and a link name */
  153. case '1':
  154. file_header->mode |= S_IFREG;
  155. break;
  156. case '7':
  157. /* case 0: */
  158. case '0':
  159. #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  160. if (last_char_is(file_header->name, '/')) {
  161. file_header->mode |= S_IFDIR;
  162. } else
  163. #endif
  164. file_header->mode |= S_IFREG;
  165. break;
  166. case '2':
  167. file_header->mode |= S_IFLNK;
  168. break;
  169. case '3':
  170. file_header->mode |= S_IFCHR;
  171. break;
  172. case '4':
  173. file_header->mode |= S_IFBLK;
  174. break;
  175. case '5':
  176. file_header->mode |= S_IFDIR;
  177. break;
  178. case '6':
  179. file_header->mode |= S_IFIFO;
  180. break;
  181. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  182. case 'L':
  183. /* free: paranoia: tar with several consecutive longnames */
  184. free(longname);
  185. /* For paranoia reasons we allocate extra NUL char */
  186. longname = xzalloc(file_header->size + 1);
  187. /* We read ASCIZ string, including NUL */
  188. xread(archive_handle->src_fd, longname, file_header->size);
  189. archive_handle->offset += file_header->size;
  190. /* return get_header_tar(archive_handle); */
  191. /* gcc 4.1.1 didn't optimize it into jump */
  192. /* so we will do it ourself, this also saves stack */
  193. goto again;
  194. case 'K':
  195. free(linkname);
  196. linkname = xzalloc(file_header->size + 1);
  197. xread(archive_handle->src_fd, linkname, file_header->size);
  198. archive_handle->offset += file_header->size;
  199. /* return get_header_tar(archive_handle); */
  200. goto again;
  201. case 'D': /* GNU dump dir */
  202. case 'M': /* Continuation of multi volume archive */
  203. case 'N': /* Old GNU for names > 100 characters */
  204. case 'S': /* Sparse file */
  205. case 'V': /* Volume header */
  206. #endif
  207. case 'g': /* pax global header */
  208. case 'x': { /* pax extended header */
  209. off_t sz;
  210. bb_error_msg("warning: skipping header '%c'", tar.typeflag);
  211. sz = (file_header->size + 511) & ~(off_t)511;
  212. archive_handle->offset += sz;
  213. sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
  214. while (sz--)
  215. xread(archive_handle->src_fd, &tar, 512);
  216. /* return get_header_tar(archive_handle); */
  217. goto again_after_align;
  218. }
  219. default:
  220. bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
  221. }
  222. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  223. if (longname) {
  224. file_header->name = longname;
  225. longname = NULL;
  226. }
  227. if (linkname) {
  228. file_header->link_name = linkname;
  229. linkname = NULL;
  230. }
  231. #endif
  232. if (!strncmp(file_header->name, "/../"+1, 3)
  233. || strstr(file_header->name, "/../")
  234. ) {
  235. bb_error_msg_and_die("name with '..' encountered: '%s'",
  236. file_header->name);
  237. }
  238. /* Strip trailing '/' in directories */
  239. /* Must be done after mode is set as '/' is used to check if its a directory */
  240. cp = last_char_is(file_header->name, '/');
  241. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  242. archive_handle->action_header(archive_handle->file_header);
  243. /* Note that we kill the '/' only after action_header() */
  244. /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
  245. if (cp) *cp = '\0';
  246. archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
  247. archive_handle->action_data(archive_handle);
  248. llist_add_to(&(archive_handle->passed), file_header->name);
  249. } else {
  250. data_skip(archive_handle);
  251. free(file_header->name);
  252. }
  253. archive_handle->offset += file_header->size;
  254. free(file_header->link_name);
  255. /* Do not free(file_header->name)! */
  256. return EXIT_SUCCESS;
  257. }