get_header_tar.c 8.8 KB

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