tarfn.c 7.2 KB

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