tarfn.c 7.6 KB

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