get_header_tar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. /* NB: _DESTROYS_ str[len] character! */
  16. static unsigned long long getOctal(char *str, int len)
  17. {
  18. unsigned long long v;
  19. /* NB: leading spaces are allowed. Using strtoull to handle that.
  20. * The downside is that we accept e.g. "-123" too :)
  21. */
  22. str[len] = '\0';
  23. v = strtoull(str, &str, 8);
  24. if (*str && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY || *str != ' '))
  25. bb_error_msg_and_die("corrupted octal value in tar header");
  26. return v;
  27. }
  28. #define GET_OCTAL(a) getOctal((a), sizeof(a))
  29. void BUG_tar_header_size(void);
  30. char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
  31. {
  32. file_header_t *file_header = archive_handle->file_header;
  33. struct {
  34. /* ustar header, Posix 1003.1 */
  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. /* POSIX: "ustar" NUL "00" */
  45. /* GNU tar: "ustar " NUL */
  46. /* Normally it's defined as magic[6] followed by
  47. * version[2], but we put them together to simplify code
  48. */
  49. char magic[8]; /* 257-264 */
  50. char uname[32]; /* 265-296 */
  51. char gname[32]; /* 297-328 */
  52. char devmajor[8]; /* 329-336 */
  53. char devminor[8]; /* 337-344 */
  54. char prefix[155]; /* 345-499 */
  55. char padding[12]; /* 500-512 */
  56. } tar;
  57. char *cp;
  58. int i, sum_u, sum;
  59. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  60. int sum_s;
  61. #endif
  62. int parse_names;
  63. /* Our "private data" */
  64. #define p_end (*(smallint *)(&archive_handle->ah_priv[0]))
  65. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  66. #define p_longname (*(char* *)(&archive_handle->ah_priv[1]))
  67. #define p_linkname (*(char* *)(&archive_handle->ah_priv[2]))
  68. #else
  69. #define p_longname 0
  70. #define p_linkname 0
  71. #endif
  72. // if (!archive_handle->ah_priv_inited) {
  73. // archive_handle->ah_priv_inited = 1;
  74. // p_end = 0;
  75. // USE_FEATURE_TAR_GNU_EXTENSIONS(p_longname = NULL;)
  76. // USE_FEATURE_TAR_GNU_EXTENSIONS(p_linkname = NULL;)
  77. // }
  78. if (sizeof(tar) != 512)
  79. BUG_tar_header_size();
  80. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  81. again:
  82. #endif
  83. /* Align header */
  84. data_align(archive_handle, 512);
  85. again_after_align:
  86. #if ENABLE_DESKTOP
  87. /* to prevent misdetection of bz2 sig */
  88. *(uint32_t*)(&tar) = 0;
  89. i = full_read(archive_handle->src_fd, &tar, 512);
  90. /* If GNU tar sees EOF in above read, it says:
  91. * "tar: A lone zero block at N", where N = kilobyte
  92. * where EOF was met (not EOF block, actual EOF!),
  93. * and exits with EXIT_SUCCESS.
  94. * We will mimic exit(EXIT_SUCCESS), although we will not mimic
  95. * the message and we don't check whether we indeed
  96. * saw zero block directly before this. */
  97. if (i == 0) {
  98. xfunc_error_retval = 0;
  99. short_read:
  100. bb_error_msg_and_die("short read");
  101. }
  102. if (i != 512) {
  103. USE_FEATURE_TAR_AUTODETECT(goto autodetect;)
  104. goto short_read;
  105. }
  106. #else
  107. i = 512;
  108. xread(archive_handle->src_fd, &tar, i);
  109. #endif
  110. archive_handle->offset += i;
  111. /* If there is no filename its an empty header */
  112. if (tar.name[0] == 0 && tar.prefix[0] == 0) {
  113. if (p_end) {
  114. /* Second consecutive empty header - end of archive.
  115. * Read until the end to empty the pipe from gz or bz2
  116. */
  117. while (full_read(archive_handle->src_fd, &tar, 512) == 512)
  118. continue;
  119. return EXIT_FAILURE;
  120. }
  121. p_end = 1;
  122. return EXIT_SUCCESS;
  123. }
  124. p_end = 0;
  125. /* Check header has valid magic, "ustar" is for the proper tar,
  126. * five NULs are for the old tar format */
  127. if (strncmp(tar.magic, "ustar", 5) != 0
  128. && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  129. || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
  130. ) {
  131. #if ENABLE_FEATURE_TAR_AUTODETECT
  132. char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
  133. USE_DESKTOP(autodetect:)
  134. /* tar gz/bz autodetect: check for gz/bz2 magic.
  135. * If we see the magic, and it is the very first block,
  136. * we can switch to get_header_tar_gz/bz2/lzma().
  137. * Needs seekable fd. I wish recv(MSG_PEEK) works
  138. * on any fd... */
  139. #if ENABLE_FEATURE_SEAMLESS_GZ
  140. if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */
  141. get_header_ptr = get_header_tar_gz;
  142. } else
  143. #endif
  144. #if ENABLE_FEATURE_SEAMLESS_BZ2
  145. if (tar.name[0] == 'B' && tar.name[1] == 'Z'
  146. && tar.name[2] == 'h' && isdigit(tar.name[3])
  147. ) { /* bzip2 */
  148. get_header_ptr = get_header_tar_bz2;
  149. } else
  150. #endif
  151. goto err;
  152. /* Two different causes for lseek() != 0:
  153. * unseekable fd (would like to support that too, but...),
  154. * or not first block (false positive, it's not .gz/.bz2!) */
  155. if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
  156. goto err;
  157. while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
  158. continue;
  159. return EXIT_FAILURE;
  160. err:
  161. #endif /* FEATURE_TAR_AUTODETECT */
  162. bb_error_msg_and_die("invalid tar magic");
  163. }
  164. /* Do checksum on headers.
  165. * POSIX says that checksum is done on unsigned bytes, but
  166. * Sun and HP-UX gets it wrong... more details in
  167. * GNU tar source. */
  168. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  169. sum_s = ' ' * sizeof(tar.chksum);
  170. #endif
  171. sum_u = ' ' * sizeof(tar.chksum);
  172. for (i = 0; i < 148; i++) {
  173. sum_u += ((unsigned char*)&tar)[i];
  174. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  175. sum_s += ((signed char*)&tar)[i];
  176. #endif
  177. }
  178. for (i = 156; i < 512; i++) {
  179. sum_u += ((unsigned char*)&tar)[i];
  180. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  181. sum_s += ((signed char*)&tar)[i];
  182. #endif
  183. }
  184. #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  185. sum = strtoul(tar.chksum, &cp, 8);
  186. if ((*cp && *cp != ' ')
  187. || (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
  188. ) {
  189. bb_error_msg_and_die("invalid tar header checksum");
  190. }
  191. #else
  192. /* This field does not need special treatment (getOctal) */
  193. sum = xstrtoul(tar.chksum, 8);
  194. if (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
  195. bb_error_msg_and_die("invalid tar header checksum");
  196. }
  197. #endif
  198. /* 0 is reserved for high perf file, treat as normal file */
  199. if (!tar.typeflag) tar.typeflag = '0';
  200. parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
  201. /* getOctal trashes subsequent field, therefore we call it
  202. * on fields in reverse order */
  203. if (tar.devmajor[0]) {
  204. char t = tar.prefix[0];
  205. /* we trash prefix[0] here, but we DO need it later! */
  206. unsigned minor = GET_OCTAL(tar.devminor);
  207. unsigned major = GET_OCTAL(tar.devmajor);
  208. file_header->device = makedev(major, minor);
  209. tar.prefix[0] = t;
  210. }
  211. file_header->link_target = NULL;
  212. if (!p_linkname && parse_names && tar.linkname[0]) {
  213. file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
  214. /* FIXME: what if we have non-link object with link_target? */
  215. /* Will link_target be free()ed? */
  216. }
  217. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  218. file_header->uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
  219. file_header->gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
  220. #endif
  221. file_header->mtime = GET_OCTAL(tar.mtime);
  222. file_header->size = GET_OCTAL(tar.size);
  223. file_header->gid = GET_OCTAL(tar.gid);
  224. file_header->uid = GET_OCTAL(tar.uid);
  225. /* Set bits 0-11 of the files mode */
  226. file_header->mode = 07777 & GET_OCTAL(tar.mode);
  227. file_header->name = NULL;
  228. if (!p_longname && parse_names) {
  229. /* we trash mode[0] here, it's ok */
  230. //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
  231. tar.mode[0] = '\0';
  232. if (tar.prefix[0]) {
  233. /* and padding[0] */
  234. //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
  235. tar.padding[0] = '\0';
  236. file_header->name = concat_path_file(tar.prefix, tar.name);
  237. } else
  238. file_header->name = xstrdup(tar.name);
  239. }
  240. /* Set bits 12-15 of the files mode */
  241. /* (typeflag was not trashed because chksum does not use getOctal) */
  242. switch (tar.typeflag) {
  243. /* busybox identifies hard links as being regular files with 0 size and a link name */
  244. case '1':
  245. file_header->mode |= S_IFREG;
  246. break;
  247. case '7':
  248. /* case 0: */
  249. case '0':
  250. #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  251. if (last_char_is(file_header->name, '/')) {
  252. goto set_dir;
  253. }
  254. #endif
  255. file_header->mode |= S_IFREG;
  256. break;
  257. case '2':
  258. file_header->mode |= S_IFLNK;
  259. /* have seen tarballs with size field containing
  260. * the size of the link target's name */
  261. size0:
  262. file_header->size = 0;
  263. break;
  264. case '3':
  265. file_header->mode |= S_IFCHR;
  266. goto size0; /* paranoia */
  267. case '4':
  268. file_header->mode |= S_IFBLK;
  269. goto size0;
  270. case '5':
  271. USE_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
  272. file_header->mode |= S_IFDIR;
  273. goto size0;
  274. case '6':
  275. file_header->mode |= S_IFIFO;
  276. goto size0;
  277. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  278. case 'L':
  279. /* free: paranoia: tar with several consecutive longnames */
  280. free(p_longname);
  281. /* For paranoia reasons we allocate extra NUL char */
  282. p_longname = xzalloc(file_header->size + 1);
  283. /* We read ASCIZ string, including NUL */
  284. xread(archive_handle->src_fd, p_longname, file_header->size);
  285. archive_handle->offset += file_header->size;
  286. /* return get_header_tar(archive_handle); */
  287. /* gcc 4.1.1 didn't optimize it into jump */
  288. /* so we will do it ourself, this also saves stack */
  289. goto again;
  290. case 'K':
  291. free(p_linkname);
  292. p_linkname = xzalloc(file_header->size + 1);
  293. xread(archive_handle->src_fd, p_linkname, file_header->size);
  294. archive_handle->offset += file_header->size;
  295. /* return get_header_tar(archive_handle); */
  296. goto again;
  297. case 'D': /* GNU dump dir */
  298. case 'M': /* Continuation of multi volume archive */
  299. case 'N': /* Old GNU for names > 100 characters */
  300. case 'S': /* Sparse file */
  301. case 'V': /* Volume header */
  302. #endif
  303. case 'g': /* pax global header */
  304. case 'x': { /* pax extended header */
  305. off_t sz;
  306. bb_error_msg("warning: skipping header '%c'", tar.typeflag);
  307. sz = (file_header->size + 511) & ~(off_t)511;
  308. archive_handle->offset += sz;
  309. sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
  310. while (sz--)
  311. xread(archive_handle->src_fd, &tar, 512);
  312. /* return get_header_tar(archive_handle); */
  313. goto again_after_align;
  314. }
  315. default:
  316. bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
  317. }
  318. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  319. if (p_longname) {
  320. file_header->name = p_longname;
  321. p_longname = NULL;
  322. }
  323. if (p_linkname) {
  324. file_header->link_target = p_linkname;
  325. p_linkname = NULL;
  326. }
  327. #endif
  328. if (strncmp(file_header->name, "/../"+1, 3) == 0
  329. || strstr(file_header->name, "/../")
  330. ) {
  331. bb_error_msg_and_die("name with '..' encountered: '%s'",
  332. file_header->name);
  333. }
  334. /* Strip trailing '/' in directories */
  335. /* Must be done after mode is set as '/' is used to check if it's a directory */
  336. cp = last_char_is(file_header->name, '/');
  337. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  338. archive_handle->action_header(/*archive_handle->*/ file_header);
  339. /* Note that we kill the '/' only after action_header() */
  340. /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
  341. if (cp) *cp = '\0';
  342. archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET;
  343. archive_handle->action_data(archive_handle);
  344. llist_add_to(&(archive_handle->passed), file_header->name);
  345. } else {
  346. data_skip(archive_handle);
  347. free(file_header->name);
  348. }
  349. archive_handle->offset += file_header->size;
  350. free(file_header->link_target);
  351. /* Do not free(file_header->name)! (why?) */
  352. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  353. free(file_header->uname);
  354. free(file_header->gname);
  355. #endif
  356. return EXIT_SUCCESS;
  357. }