tarfn.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Functions for extracting tar archives.
  3. * Bruce Perens, April-May 1995
  4. * Copyright © 1995 Bruce Perens
  5. * This is free software under the GNU General Public License.
  6. */
  7. #include <config.h>
  8. #include <compat.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include <pwd.h>
  14. #include <grp.h>
  15. #include <errno.h>
  16. #include <tarfn.h>
  17. #include <dpkg.h>
  18. #include <dpkg-priv.h>
  19. #define TAR_MAGIC_USTAR "ustar\0" "00"
  20. #define TAR_MAGIC_GNU "ustar " " \0"
  21. struct TarHeader {
  22. char Name[100];
  23. char Mode[8];
  24. char UserID[8];
  25. char GroupID[8];
  26. char Size[12];
  27. char ModificationTime[12];
  28. char Checksum[8];
  29. char LinkFlag;
  30. char LinkName[100];
  31. char MagicNumber[8];
  32. char UserName[32];
  33. char GroupName[32];
  34. char MajorDevice[8];
  35. char MinorDevice[8];
  36. char Prefix[155]; /* Only valid on ustar. */
  37. };
  38. typedef struct TarHeader TarHeader;
  39. static const size_t TarChecksumOffset = offsetof(TarHeader, Checksum);
  40. /* Octal-ASCII-to-long */
  41. static long
  42. OtoL(const char * s, int size)
  43. {
  44. int n = 0;
  45. while ( *s == ' ' ) {
  46. s++;
  47. size--;
  48. }
  49. while ( --size >= 0 && *s >= '0' && *s <= '7' )
  50. n = (n * 010) + (*s++ - '0');
  51. return n;
  52. }
  53. /* String block to C null-terminated string */
  54. static char *
  55. StoC(const char *s, int size)
  56. {
  57. int len;
  58. char * str;
  59. len = strnlen(s, size);
  60. str = m_malloc(len + 1);
  61. memcpy(str, s, len);
  62. str[len] = 0;
  63. return str;
  64. }
  65. /* FIXME: Rewrite using varbuf, once it supports the needed functionality. */
  66. static char *
  67. get_prefix_name(TarHeader *h)
  68. {
  69. char *prefix, *name, *s;
  70. /* The size is not going to be bigger than that. */
  71. s = m_malloc(257);
  72. prefix = StoC(h->Prefix, sizeof(h->Prefix));
  73. name = StoC(h->Name, sizeof(h->Name));
  74. strcpy(s, prefix);
  75. strcat(s, "/");
  76. strcat(s, name);
  77. free(prefix);
  78. free(name);
  79. return s;
  80. }
  81. static int
  82. DecodeTarHeader(char * block, TarInfo * d)
  83. {
  84. TarHeader * h = (TarHeader *)block;
  85. unsigned char * s = (unsigned char *)block;
  86. struct passwd * passwd = NULL;
  87. struct group * group = NULL;
  88. unsigned int i;
  89. long sum;
  90. long checksum;
  91. if (memcmp(h->MagicNumber, TAR_MAGIC_GNU, 6) == 0)
  92. d->format = tar_format_gnu;
  93. else if (memcmp(h->MagicNumber, TAR_MAGIC_USTAR, 6) == 0)
  94. d->format = tar_format_ustar;
  95. else
  96. d->format = tar_format_old;
  97. if ( *h->UserName )
  98. passwd = getpwnam(h->UserName);
  99. if ( *h->GroupName )
  100. group = getgrnam(h->GroupName);
  101. /* Concatenate prefix and name to support ustar style long names. */
  102. if (d->format == tar_format_ustar && h->Prefix[0] != '\0')
  103. d->Name = get_prefix_name(h);
  104. else
  105. d->Name = StoC(h->Name, sizeof(h->Name));
  106. d->LinkName = StoC(h->LinkName, sizeof(h->LinkName));
  107. d->Mode = (mode_t)OtoL(h->Mode, sizeof(h->Mode));
  108. d->Size = (size_t)OtoL(h->Size, sizeof(h->Size));
  109. d->ModTime = (time_t)OtoL(h->ModificationTime
  110. ,sizeof(h->ModificationTime));
  111. d->Device = ((OtoL(h->MajorDevice, sizeof(h->MajorDevice)) & 0xff) << 8)
  112. | (OtoL(h->MinorDevice, sizeof(h->MinorDevice)) & 0xff);
  113. checksum = OtoL(h->Checksum, sizeof(h->Checksum));
  114. d->UserID = (uid_t)OtoL(h->UserID, sizeof(h->UserID));
  115. d->GroupID = (gid_t)OtoL(h->GroupID, sizeof(h->GroupID));
  116. d->Type = (TarFileType)h->LinkFlag;
  117. if ( passwd )
  118. d->UserID = passwd->pw_uid;
  119. if ( group )
  120. d->GroupID = group->gr_gid;
  121. sum = ' ' * sizeof(h->Checksum);/* Treat checksum field as all blank */
  122. for ( i = TarChecksumOffset; i > 0; i-- )
  123. sum += *s++;
  124. s += sizeof(h->Checksum); /* Skip the real checksum field */
  125. for ( i = (512 - TarChecksumOffset - sizeof(h->Checksum)); i > 0; i-- )
  126. sum += *s++;
  127. return ( sum == checksum );
  128. }
  129. typedef struct symlinkList {
  130. TarInfo h;
  131. struct symlinkList *next;
  132. } symlinkList;
  133. int
  134. TarExtractor(
  135. void * userData
  136. ,const TarFunctions * functions)
  137. {
  138. int status;
  139. char buffer[512];
  140. TarInfo h;
  141. char *next_long_name, *next_long_link;
  142. char *bp;
  143. char **longp;
  144. int long_read;
  145. symlinkList *symListTop, *symListBottom, *symListPointer;
  146. next_long_name = NULL;
  147. next_long_link = NULL;
  148. long_read = 0;
  149. symListBottom = symListPointer = symListTop = m_malloc(sizeof(symlinkList));
  150. symListTop->next = NULL;
  151. h.Name = NULL;
  152. h.LinkName = NULL;
  153. h.UserData = userData;
  154. while ( (status = functions->Read(userData, buffer, 512)) == 512 ) {
  155. int nameLength;
  156. if ( !DecodeTarHeader(buffer, &h) ) {
  157. if ( h.Name[0] == '\0' ) {
  158. status = 0; /* End of tape */
  159. } else {
  160. errno = 0; /* Indicates broken tarfile */
  161. status = -1; /* Header checksum error */
  162. }
  163. break;
  164. }
  165. if ( h.Type != GNU_LONGLINK && h.Type != GNU_LONGNAME ) {
  166. if (next_long_name) {
  167. h.Name = next_long_name;
  168. }
  169. if (next_long_link) {
  170. h.LinkName = next_long_link;
  171. }
  172. next_long_link = NULL;
  173. next_long_name = NULL;
  174. }
  175. if ( h.Name[0] == '\0' ) {
  176. errno = 0; /* Indicates broken tarfile */
  177. status = -1; /* Bad header data */
  178. break;
  179. }
  180. nameLength = strlen(h.Name);
  181. switch ( h.Type ) {
  182. case NormalFile0:
  183. case NormalFile1:
  184. /* Compatibility with pre-ANSI ustar */
  185. if ( h.Name[nameLength - 1] != '/' ) {
  186. status = (*functions->ExtractFile)(&h);
  187. break;
  188. }
  189. /* Else, Fall Through */
  190. case Directory:
  191. if ( h.Name[nameLength - 1] == '/' ) {
  192. h.Name[nameLength - 1] = '\0';
  193. }
  194. status = (*functions->MakeDirectory)(&h);
  195. break;
  196. case HardLink:
  197. status = (*functions->MakeHardLink)(&h);
  198. break;
  199. case SymbolicLink:
  200. memcpy(&symListBottom->h, &h, sizeof(TarInfo));
  201. symListBottom->h.Name = m_strdup(h.Name);
  202. symListBottom->h.LinkName = m_strdup(h.LinkName);
  203. symListBottom->next = m_malloc(sizeof(symlinkList));
  204. symListBottom = symListBottom->next;
  205. symListBottom->next = NULL;
  206. status = 0;
  207. break;
  208. case CharacterDevice:
  209. case BlockDevice:
  210. case FIFO:
  211. status = (*functions->MakeSpecialFile)(&h);
  212. break;
  213. case GNU_LONGLINK:
  214. case GNU_LONGNAME:
  215. // set longp to the location of the long filename or link
  216. // we're trying to deal with
  217. longp = ((h.Type == GNU_LONGNAME)
  218. ? &next_long_name
  219. : &next_long_link);
  220. if (*longp)
  221. free(*longp);
  222. *longp = m_malloc(h.Size);
  223. bp = *longp;
  224. // the way the GNU long{link,name} stuff works is like this:
  225. // The first header is a "dummy" header that contains the size
  226. // of the filename. The next N headers contain the filename.
  227. // After the headers with the filename comes the "real" header
  228. // with a bogus name or link.
  229. for (long_read = h.Size; long_read > 0;
  230. long_read -= 512) {
  231. int copysize;
  232. status = functions->Read(userData, buffer, 512);
  233. // if we didn't get 512 bytes read, punt
  234. if (512 != status) {
  235. if ( status > 0 ) { /* Read partial header record */
  236. errno = 0;
  237. status = -1;
  238. }
  239. break;
  240. }
  241. copysize = min(long_read, 512);
  242. memcpy (bp, buffer, copysize);
  243. bp += copysize;
  244. };
  245. // This decode function expects status to be 0 after
  246. // the case statement if we successfully decoded. I
  247. // guess what we just did was successful.
  248. status = 0;
  249. break;
  250. default:
  251. errno = 0; /* Indicates broken tarfile */
  252. status = -1; /* Bad header field */
  253. }
  254. if ( status != 0 )
  255. break; /* Pass on status from coroutine */
  256. }
  257. while(symListPointer->next) {
  258. if ( status == 0 )
  259. status = (*functions->MakeSymbolicLink)(&symListPointer->h);
  260. symListBottom = symListPointer->next;
  261. free(symListPointer->h.Name);
  262. free(symListPointer->h.LinkName);
  263. free(symListPointer);
  264. symListPointer = symListBottom;
  265. }
  266. free(symListPointer);
  267. free(h.Name);
  268. free(h.LinkName);
  269. if ( status > 0 ) { /* Read partial header record */
  270. errno = 0; /* Indicates broken tarfile */
  271. return -1;
  272. } else {
  273. return status; /* Whatever I/O function returned */
  274. }
  275. }