tarfn.c 7.2 KB

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