get_header_tar.c 8.8 KB

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