tar2qfile.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /* $OpenBSD: tar.h,v 1.7 2003/06/02 23:32:09 millert Exp $ */
  2. /* $NetBSD: tar.h,v 1.3 1995/03/21 09:07:51 cgd Exp $ */
  3. /*-
  4. * Copyright (c) 1992 Keith Muller.
  5. * Copyright (c) 1992, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This code is derived from software contributed to Berkeley by
  9. * Keith Muller of the University of California, San Diego.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)tar.h 8.2 (Berkeley) 4/18/94
  36. */
  37. #define _GNU_SOURCE /* For O_NOFOLLOW. */
  38. #include <errno.h>
  39. #include <ioall.h>
  40. #include <fcntl.h>
  41. #include <sys/time.h>
  42. #include <sys/stat.h>
  43. #include <stdlib.h>
  44. #include <unistd.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <qfile-utils.h>
  48. /***************************************************
  49. * Most routines extracted from the PAX project (tar.c...) *
  50. ***************************************************/
  51. /*
  52. * BSD PAX global data structures and constants.
  53. */
  54. #define MAXBLK 64512 /* MAX blocksize supported (posix SPEC) */
  55. /* WARNING: increasing MAXBLK past 32256 */
  56. /* will violate posix spec. */
  57. #define MAXBLK_POSIX 32256 /* MAX blocksize supported as per POSIX */
  58. #define BLKMULT 512 /* blocksize must be even mult of 512 bytes */
  59. /* Don't even think of changing this */
  60. #define DEVBLK 8192 /* default read blksize for devices */
  61. #define FILEBLK 10240 /* default read blksize for files */
  62. #define PAXPATHLEN 3072 /* maximium path length for pax. MUST be */
  63. /*
  64. * defines and data structures common to all tar formats
  65. */
  66. #define CHK_LEN 8 /* length of checksum field */
  67. #define TNMSZ 100 /* size of name field */
  68. #define NULLCNT 2 /* number of null blocks in trailer */
  69. #define CHK_OFFSET 148 /* start of chksum field */
  70. #define BLNKSUM 256L /* sum of checksum field using ' ' */
  71. /*
  72. * General Defines
  73. */
  74. #define HEX 16
  75. #define OCT 8
  76. #define _PAX_ 1
  77. #define _TFILE_BASE "paxXXXXXXXXXX"
  78. /*
  79. * General Macros
  80. */
  81. #ifndef MIN
  82. #define MIN(a,b) (((a)<(b))?(a):(b))
  83. #endif
  84. #ifndef MAX
  85. #define MAX(a,b) (((a)>(b))?(a):(b))
  86. #endif
  87. #define MAJOR(x) major(x)
  88. #define MINOR(x) minor(x)
  89. #define TODEV(x, y) makedev((x), (y))
  90. /*
  91. * Values used in typeflag field in all tar formats
  92. * (only REGTYPE, LNKTYPE and SYMTYPE are used in old bsd tar headers)
  93. */
  94. #define REGTYPE '0' /* Regular File */
  95. #define AREGTYPE '\0' /* Regular File */
  96. #define LNKTYPE '1' /* Link */
  97. #define SYMTYPE '2' /* Symlink */
  98. #define CHRTYPE '3' /* Character Special File */
  99. #define BLKTYPE '4' /* Block Special File */
  100. #define DIRTYPE '5' /* Directory */
  101. #define FIFOTYPE '6' /* FIFO */
  102. #define CONTTYPE '7' /* high perf file */
  103. /*
  104. * GNU tar compatibility;
  105. */
  106. #define LONGLINKTYPE 'K' /* Long Symlink */
  107. #define LONGNAMETYPE 'L' /* Long File */
  108. #define EXTHEADERTYPE 'x' /* Extended header */
  109. /*
  110. * Pad with a bit mask, much faster than doing a mod but only works on powers
  111. * of 2. Macro below is for block of 512 bytes.
  112. */
  113. #define TAR_PAD(x) ((512 - ((x) & 511)) & 511)
  114. /*
  115. * Data Interchange Format - Extended tar header format - POSIX 1003.1-1990
  116. */
  117. #define TPFSZ 155
  118. #define TMAGIC "ustar" /* ustar and a null */
  119. #define TMAGLEN 6
  120. #define TVERSION "00" /* 00 and no null */
  121. #define TVERSLEN 2
  122. typedef struct {
  123. char name[TNMSZ]; /* name of entry */
  124. char mode[8]; /* mode */
  125. char uid[8]; /* uid */
  126. char gid[8]; /* gid */
  127. char size[12]; /* size */
  128. char mtime[12]; /* modification time */
  129. char chksum[CHK_LEN]; /* checksum */
  130. char typeflag; /* type of file. */
  131. char linkname[TNMSZ]; /* linked to name */
  132. char magic[TMAGLEN]; /* magic cookie */
  133. char version[TVERSLEN]; /* version */
  134. char uname[32]; /* ascii owner name */
  135. char gname[32]; /* ascii group name */
  136. char devmajor[8]; /* major device number */
  137. char devminor[8]; /* minor device number */
  138. char prefix[TPFSZ]; /* linked to name */
  139. } HD_USTAR;
  140. /*
  141. * Routines for manipulating headers, trailers:
  142. * asc_ul()
  143. * tar_trail()
  144. * tar_chksm()
  145. * ustar_id()
  146. */
  147. static unsigned long tar_chksm (char *, int);
  148. char *gnu_hack_string; /* GNU ././@LongLink hackery */
  149. char untrusted_namebuf[MAX_PATH_LENGTH];
  150. extern int ignore_quota_error;
  151. struct filters {
  152. int filters_count;
  153. char **filters;
  154. int *filters_matches;
  155. int matched_filters;
  156. };
  157. /*
  158. * asc_ul()
  159. * convert hex/octal character string into a u_long. We do not have to
  160. * check for overflow! (the headers in all supported formats are not large
  161. * enough to create an overflow).
  162. * NOTE: strings passed to us are NOT TERMINATED.
  163. * Return:
  164. * unsigned long value
  165. */
  166. u_long
  167. asc_ul (char *str, int len, int base)
  168. {
  169. char *stop;
  170. u_long tval = 0;
  171. stop = str + len;
  172. /*
  173. * skip over leading blanks and zeros
  174. */
  175. while ((str < stop) && ((*str == ' ') || (*str == '0')))
  176. ++str;
  177. /*
  178. * for each valid digit, shift running value (tval) over to next digit
  179. * and add next digit
  180. */
  181. if (base == HEX)
  182. {
  183. while (str < stop)
  184. {
  185. if ((*str >= '0') && (*str <= '9'))
  186. tval = (tval << 4) + (*str++ - '0');
  187. else if ((*str >= 'A') && (*str <= 'F'))
  188. tval = (tval << 4) + 10 + (*str++ - 'A');
  189. else if ((*str >= 'a') && (*str <= 'f'))
  190. tval = (tval << 4) + 10 + (*str++ - 'a');
  191. else
  192. break;
  193. }
  194. }
  195. else
  196. {
  197. while ((str < stop) && (*str >= '0') && (*str <= '7'))
  198. tval = (tval << 3) + (*str++ - '0');
  199. }
  200. return (tval);
  201. }
  202. /*
  203. * tar_trail()
  204. * Called to determine if a header block is a valid trailer. We are passed
  205. * the block, the in_sync flag (which tells us we are in resync mode;
  206. * looking for a valid header), and cnt (which starts at zero) which is
  207. * used to count the number of empty blocks we have seen so far.
  208. * Return:
  209. * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
  210. * could never contain a header.
  211. */
  212. int
  213. tar_trail (char *buf,
  214. int in_resync, int *cnt)
  215. {
  216. register int i;
  217. /*
  218. * look for all zero, trailer is two consecutive blocks of zero
  219. */
  220. for (i = 0; i < BLKMULT; ++i)
  221. {
  222. if (buf[i] != '\0')
  223. break;
  224. }
  225. /*
  226. * if not all zero it is not a trailer, but MIGHT be a header.
  227. */
  228. if (i != BLKMULT)
  229. return (-1);
  230. /*
  231. * When given a zero block, we must be careful!
  232. * If we are not in resync mode, check for the trailer. Have to watch
  233. * out that we do not mis-identify file data as the trailer, so we do
  234. * NOT try to id a trailer during resync mode. During resync mode we
  235. * might as well throw this block out since a valid header can NEVER be
  236. * a block of all 0 (we must have a valid file name).
  237. */
  238. if (!in_resync && (++*cnt >= NULLCNT))
  239. return (0);
  240. return (1);
  241. }
  242. /*
  243. * tar_chksm()
  244. * calculate the checksum for a tar block counting the checksum field as
  245. * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
  246. * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
  247. * pad headers with 0.
  248. * Return:
  249. * unsigned long checksum
  250. */
  251. static unsigned long
  252. tar_chksm (char *blk, int len)
  253. {
  254. char *stop;
  255. char *pt;
  256. unsigned int chksm = BLNKSUM; /* initial value is checksum field sum */
  257. /*
  258. * add the part of the block before the checksum field
  259. */
  260. pt = blk;
  261. stop = blk + CHK_OFFSET;
  262. while (pt < stop)
  263. chksm += (*pt++ & 0xff);
  264. /*
  265. * move past the checksum field and keep going, spec counts the
  266. * checksum field as the sum of 8 blanks (which is pre-computed as
  267. * BLNKSUM).
  268. * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
  269. * starts, no point in summing zero's)
  270. */
  271. pt += CHK_LEN;
  272. stop = blk + len;
  273. while (pt < stop)
  274. chksm += (*pt++ & 0xff);
  275. return chksm;
  276. }
  277. /*
  278. * ustar_id()
  279. * determine if a block given to us is a valid ustar header. We have to
  280. * be on the lookout for those pesky blocks of all zero's
  281. * Return:
  282. * 0 if a ustar header, -1 otherwise
  283. */
  284. int
  285. ustar_id (char *blk, size_t size)
  286. {
  287. HD_USTAR *hd;
  288. if (size < BLKMULT)
  289. return (-1);
  290. hd = (HD_USTAR *) blk;
  291. /*
  292. * check for block of zero's first, a simple and fast test then check
  293. * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
  294. * programs are fouled up and create archives missing the \0. Last we
  295. * check the checksum. If ok we have to assume it is a valid header.
  296. */
  297. if (hd->name[0] == '\0')
  298. return (-1);
  299. if (strncmp (hd->magic, TMAGIC, TMAGLEN - 1) != 0)
  300. return (-1);
  301. if (asc_ul (hd->chksum, sizeof (hd->chksum), OCT) !=
  302. tar_chksm (blk, BLKMULT))
  303. return (-1);
  304. return (0);
  305. }
  306. /*
  307. * Routines for reading tar files
  308. // Source: http://www.mkssoftware.com/docs/man4/pax.4.asp
  309. struct file_header { // PAX header is similar as file_header and can be completely ignored
  310. unsigned char[100] name;
  311. unsigned char[8] mode;
  312. unsigned char[8] uid; // unused
  313. unsigned char[8] gid; // unused
  314. unsigned char[12] size; // 0 if file is a link
  315. unsigned char[12] mtime;
  316. unsigned char[8] chksum;
  317. unsigned char[1] typeflag;
  318. unsigned char[100] linkname;
  319. unsigned char[6] magic; //ustar
  320. unsigned char[2] version; // 00
  321. unsigned char[32] uname; // unused
  322. unsigned char[32] gname; // unused
  323. unsigned char[8] devmajor; // unused ?
  324. unsigned char[8] devminor; // unused ?
  325. unsigned char[155] prefix; // only used for files > 100 characters. could be unused ?
  326. };
  327. enum {
  328. TYPE_REGULAR, //0
  329. TYPE_ARCHIVE_LINK, //1
  330. TYPE_SYMLINK, //2
  331. TYPE_CHARACTER_DEVICE, //3
  332. TYPE_BLOCK_DEVICE, //4
  333. TYPE_DIRECTORY, //5
  334. TYPE_FIFO, //6
  335. // Other types:
  336. TYPE_EXTENDED_USAGE, //xxxxx
  337. // A-Z are available for custom usage
  338. };
  339. // Extended attribute:
  340. // length keyword=value
  341. // atime, charset, comment, gname, linkpath, mtime, path, size, uname
  342. */
  343. enum {
  344. NEED_NOTHING,
  345. NEED_SKIP,
  346. NEED_SKIP_FILE, // distinguish between skipped file and unwanted blocks (extended headers etc)
  347. NEED_READ,
  348. NEED_SYNC_TRAIL,
  349. INVALID_HEADER,
  350. MEMORY_ALLOC_FAILED,
  351. };
  352. /*
  353. * ustar_rd()
  354. * extract the values out of block already determined to be a ustar header.
  355. * store the values in the ARCHD parameter.
  356. * Return:
  357. * 0
  358. */
  359. int n_dirs = 0;
  360. char ** dirs_headers_sent = NULL;
  361. int
  362. ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * sb, struct filters *filters)
  363. {
  364. register HD_USTAR *hd;
  365. register char *dest;
  366. register int cnt = 0;
  367. int ret;
  368. int i;
  369. int should_extract;
  370. /* DISABLED: unused
  371. dev_t devmajor;
  372. dev_t devminor;
  373. */
  374. /*
  375. * we only get proper sized buffers
  376. */
  377. fprintf(stderr,"Checking if valid header\n");
  378. if (ustar_id (buf, BLKMULT) < 0) {
  379. fprintf (stderr,"Invalid header\n");
  380. return INVALID_HEADER;
  381. }
  382. fprintf(stderr,"Valid header!\n");
  383. /* DISABLED: Internal to PAX
  384. arcn->org_name = arcn->name;
  385. arcn->sb.st_nlink = 1;
  386. arcn->pat = NULL;
  387. arcn->nlen = 0;
  388. */
  389. untrusted_hdr->namelen = 0;
  390. hd = (HD_USTAR *) buf;
  391. /*
  392. * see if the filename is split into two parts. if, so joint the parts.
  393. * we copy the prefix first and add a / between the prefix and name.
  394. */
  395. dest = untrusted_namebuf;
  396. if (*(hd->prefix) != '\0')
  397. {
  398. cnt = strlen(strncpy (dest, hd->prefix,
  399. MIN(sizeof (untrusted_namebuf) - 1,TPFSZ+1)));
  400. dest += cnt;
  401. *dest++ = '/';
  402. cnt++;
  403. }
  404. if (gnu_hack_string)
  405. {
  406. untrusted_hdr->namelen = cnt + strlen(strncpy (dest, gnu_hack_string,
  407. MIN(TNMSZ+1, sizeof (untrusted_namebuf) - cnt)));
  408. free(gnu_hack_string);
  409. gnu_hack_string = NULL;
  410. } else
  411. untrusted_hdr->namelen = cnt + strlen(strncpy (dest, hd->name,
  412. MIN(TNMSZ+1, sizeof (untrusted_namebuf) - cnt)));
  413. // qfile count the \0 in the namelen
  414. untrusted_hdr->namelen += 1;
  415. fprintf(stderr,"Retrieved name len: %d\n",untrusted_hdr->namelen);
  416. fprintf(stderr,"Retrieved name: %s\n",untrusted_namebuf);
  417. /*
  418. * follow the spec to the letter. we should only have mode bits, strip
  419. * off all other crud we may be passed.
  420. */
  421. sb->st_mode = (mode_t) (asc_ul (hd->mode, sizeof (hd->mode), OCT) &
  422. 0xfff);
  423. untrusted_hdr->mode = sb->st_mode;
  424. #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
  425. sb->st_size = (off_t) asc_uqd (hd->size, sizeof (hd->size), OCT);
  426. #else
  427. sb->st_size = (off_t) asc_ul (hd->size, sizeof (hd->size), OCT);
  428. #endif
  429. untrusted_hdr->filelen = sb->st_size;
  430. untrusted_hdr->atime = (time_t) asc_ul (hd->mtime, sizeof (hd->mtime), OCT);
  431. untrusted_hdr->mtime = untrusted_hdr->atime;
  432. untrusted_hdr->atime_nsec = untrusted_hdr->mtime_nsec = 0;
  433. sb->st_mtime = (time_t) asc_ul (hd->mtime, sizeof (hd->mtime), OCT);
  434. sb->st_ctime = sb->st_atime = sb->st_mtime;
  435. /*
  436. * If we can find the ascii names for gname and uname in the password
  437. * and group files we will use the uid's and gid they bind. Otherwise
  438. * we use the uid and gid values stored in the header. (This is what
  439. * the posix spec wants).
  440. */
  441. /* DISABLED: unused
  442. hd->gname[sizeof (hd->gname) - 1] = '\0';
  443. if (gid_name (hd->gname, &(arcn->sb.st_gid)) < 0)
  444. arcn->sb.st_gid = (gid_t) asc_ul (hd->gid, sizeof (hd->gid), OCT);
  445. hd->uname[sizeof (hd->uname) - 1] = '\0';
  446. if (uid_name (hd->uname, &(arcn->sb.st_uid)) < 0)
  447. arcn->sb.st_uid = (uid_t) asc_ul (hd->uid, sizeof (hd->uid), OCT);
  448. */
  449. /*
  450. * set the defaults, these may be changed depending on the file type
  451. */
  452. /* Disabled: pax specific
  453. arcn->ln_name[0] = '\0';
  454. arcn->ln_nlen = 0;
  455. arcn->pad = 0;
  456. arcn->skip = 0;
  457. arcn->sb.st_rdev = (dev_t) 0;
  458. */
  459. /*
  460. * set the mode and PAX type according to the typeflag in the header
  461. */
  462. switch (hd->typeflag)
  463. {
  464. case FIFOTYPE:
  465. fprintf(stderr,"File is FIFOTYPE\n");
  466. /* DISABLED: unused
  467. arcn->type = PAX_FIF;
  468. arcn->sb.st_mode |= S_IFIFO;
  469. */
  470. break;
  471. case DIRTYPE:
  472. fprintf(stderr,"File is DIRTYPE\n");
  473. /* DISABLED: unused
  474. arcn->type = PAX_DIR;
  475. arcn->sb.st_mode |= S_IFDIR;
  476. arcn->sb.st_nlink = 2;
  477. */
  478. /*
  479. * Some programs that create ustar archives append a '/'
  480. * to the pathname for directories. This clearly violates
  481. * ustar specs, but we will silently strip it off anyway.
  482. */
  483. /*
  484. if (arcn->name[arcn->nlen - 1] == '/')
  485. arcn->name[--arcn->nlen] = '\0';
  486. */
  487. break;
  488. case BLKTYPE:
  489. fprintf(stderr,"File is BLKTYPE\n");
  490. break;
  491. case CHRTYPE:
  492. fprintf(stderr,"File is CHRTYPE\n");
  493. /*
  494. * this type requires the rdev field to be set.
  495. */
  496. if (hd->typeflag == BLKTYPE)
  497. {
  498. /*
  499. arcn->type = PAX_BLK;
  500. arcn->sb.st_mode |= S_IFBLK;
  501. */
  502. }
  503. else
  504. {
  505. /*
  506. arcn->type = PAX_CHR;
  507. arcn->sb.st_mode |= S_IFCHR;
  508. */
  509. }
  510. /* DISABLED: unused
  511. devmajor = (dev_t) asc_ul (hd->devmajor, sizeof (hd->devmajor), OCT);
  512. devminor = (dev_t) asc_ul (hd->devminor, sizeof (hd->devminor), OCT);
  513. */
  514. // arcn->sb.st_rdev = TODEV (devmajor, devminor);
  515. break;
  516. case SYMTYPE:
  517. fprintf(stderr,"File is SYMTYPE\n");
  518. break;
  519. case LNKTYPE:
  520. fprintf(stderr,"File is LNKTYPE\n");
  521. if (hd->typeflag == SYMTYPE)
  522. {
  523. // arcn->type = PAX_SLK;
  524. // arcn->sb.st_mode |= S_IFLNK;
  525. }
  526. else
  527. {
  528. // arcn->type = PAX_HLK;
  529. /*
  530. * so printing looks better
  531. */
  532. // arcn->sb.st_mode |= S_IFREG;
  533. // arcn->sb.st_nlink = 2;
  534. }
  535. /*
  536. * copy the link name
  537. */
  538. // arcn->ln_nlen = strlcpy (arcn->ln_name, hd->linkname,
  539. // MIN(TNMSZ+1,sizeof (arcn->ln_name)));
  540. break;
  541. case LONGLINKTYPE:
  542. fprintf(stderr,"File is LONGLINKTYPE\n");
  543. break;
  544. case LONGNAMETYPE:
  545. fprintf(stderr,"File is LONGNAMETYPE\n");
  546. /*
  547. * GNU long link/file; we tag these here and let the
  548. * pax internals deal with it -- too ugly otherwise.
  549. */
  550. // arcn->type =
  551. // hd->typeflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF;
  552. // arcn->pad = TAR_PAD(arcn->sb.st_size);
  553. // arcn->skip = arcn->sb.st_size;
  554. // arcn->ln_name[0] = '\0';
  555. // arcn->ln_nlen = 0;
  556. break;
  557. case CONTTYPE:
  558. fprintf(stderr,"File is CONTTYPE\n");
  559. break;
  560. case AREGTYPE:
  561. fprintf(stderr,"File is AREGTYPE\n");
  562. break;
  563. case REGTYPE:
  564. fprintf(stderr,"File is REGTYPE of size %ld\n",sb->st_size);
  565. // Check if user want to extract this file
  566. should_extract = 1;
  567. for (i=0; i < filters->filters_count; i++) {
  568. should_extract = 0;
  569. fprintf(stderr, "Comparing with filter %s\n", filters->filters[i]);
  570. if (strncmp(untrusted_namebuf, filters->filters[i], strlen(filters->filters[i])) == 0) {
  571. fprintf(stderr, "Match (%d)\n", filters->filters_matches[i]);
  572. should_extract = 1;
  573. filters->filters_matches[i]++;
  574. if (filters->filters_matches[i] == 1) {
  575. // first match
  576. filters->matched_filters++;
  577. }
  578. break;
  579. }
  580. }
  581. if (should_extract != 1) {
  582. fprintf(stderr, "File should be filtered.. Skipping\n");
  583. return NEED_SKIP_FILE;
  584. }
  585. // Create a copy of untrusted_namebuf to be used for strtok
  586. char * dirbuf;
  587. dirbuf = malloc(sizeof (char) * (untrusted_hdr->namelen));
  588. if (dirbuf == NULL)
  589. return MEMORY_ALLOC_FAILED;
  590. dirbuf = strncpy(dirbuf, untrusted_namebuf, untrusted_hdr->namelen);
  591. int i = 0;
  592. int dir_found = 0;
  593. size_t pathsize = 0;
  594. char * path = NULL;
  595. struct file_header dir_header;
  596. // Split the path in directories and recompose it incrementally
  597. char * last_token = strtok(dirbuf,"/");
  598. char * token = strtok(NULL, "/");
  599. while (token != NULL) {
  600. fprintf(stderr,"Found directory %s (last:%s)\n",token,last_token);
  601. // Recompose the path based on last discovered directory
  602. if (path == NULL) {
  603. path = malloc(sizeof (char) * (strlen(last_token)+1));
  604. if (path == NULL)
  605. return MEMORY_ALLOC_FAILED;
  606. path = strncpy(path, last_token, strlen(last_token));
  607. path[strlen(last_token)] = '\0';
  608. } else {
  609. pathsize = strlen(path);
  610. path = realloc(path, sizeof (char) * (strlen(path)+1+strlen(last_token)+1));
  611. if (path == NULL)
  612. return MEMORY_ALLOC_FAILED;
  613. path[pathsize] = '/';
  614. strncpy(path+pathsize+1, last_token, strlen(last_token));
  615. path[pathsize+strlen(last_token)+1] = '\0';
  616. }
  617. fprintf(stderr,"Path is %s\n",path);
  618. fprintf(stderr,"Checking from i=0 i<%d\n",n_dirs);
  619. // Verify if qfile headers for the current path have already been sent based on the dirs_headers_sent table
  620. dir_found = 0;
  621. for (i = 0; i < n_dirs; ++i) {
  622. fprintf(stderr,"Comparing with %d %d %s %s\n",i,n_dirs,dirs_headers_sent[i],path);
  623. if (strcmp(dirs_headers_sent[i],path)==0) {
  624. fprintf(stderr,"Directory headers already sent\n");
  625. dir_found=1;
  626. }
  627. }
  628. if (dir_found == 0) {
  629. // Register the current path as being sent in the dirs_headers_sent table
  630. fprintf(stderr,"Inserting %s into register\n",path);
  631. dirs_headers_sent = realloc(dirs_headers_sent, sizeof (char*) * n_dirs++);
  632. if (dirs_headers_sent == NULL)
  633. return MEMORY_ALLOC_FAILED;
  634. dirs_headers_sent[n_dirs-1] = malloc(sizeof (char) * (strlen(path)+1));
  635. if (dirs_headers_sent[n_dirs-1] == NULL)
  636. return MEMORY_ALLOC_FAILED;
  637. strncpy(dirs_headers_sent[n_dirs-1], path, strlen(path)+1);
  638. // Initialize the qfile headers for the current directory path
  639. dir_header.namelen = strlen(path)+1;
  640. dir_header.atime = untrusted_hdr->atime;
  641. dir_header.atime_nsec = untrusted_hdr->atime_nsec;
  642. dir_header.mtime = untrusted_hdr->mtime;
  643. dir_header.mtime_nsec = untrusted_hdr->mtime_nsec;
  644. dir_header.mode = untrusted_hdr->mode | S_IFDIR;
  645. dir_header.filelen = 0;
  646. fprintf(stderr,"Sending directory headers for %s\n",path);
  647. // Send the qfile headers for the current directory path
  648. write_headers(&dir_header, path);
  649. }
  650. last_token = token;
  651. token = strtok(NULL, "/");
  652. }
  653. free(path);
  654. free(dirbuf);
  655. fprintf(stderr,"End of directory checks\n");
  656. // Restore POSIX stat file mode (because PAX format use its own file type)
  657. untrusted_hdr->mode |= S_IFREG;
  658. fprintf(stderr,"Writing file header\n");
  659. // Send header and file content
  660. write_headers(untrusted_hdr, untrusted_namebuf);
  661. fprintf(stderr,"Writing file content\n");
  662. ret = copy_file(1, fd, untrusted_hdr->filelen, &crc32_sum);
  663. fprintf(stderr,"Copyfile returned with error %d\n",ret);
  664. if (ret != COPY_FILE_OK) {
  665. if (ret != COPY_FILE_WRITE_ERROR)
  666. gui_fatal("Copying file %s: %s", untrusted_namebuf,
  667. copy_file_status_to_str(ret));
  668. else {
  669. fprintf(stderr,"UNKNOWN ERROR RETURN STATUS:%d\n.. Waiting...\n",ret);
  670. set_block(0);
  671. wait_for_result();
  672. exit(1);
  673. }
  674. }
  675. // Extract extra padding
  676. fprintf(stderr,"Need to remove pad:%lld %lld\n",untrusted_hdr->filelen,BLKMULT-(untrusted_hdr->filelen%BLKMULT));
  677. if (untrusted_hdr->filelen%BLKMULT > 0)
  678. ret = read(fd, buf, BLKMULT-(untrusted_hdr->filelen%BLKMULT));
  679. fprintf(stderr,"Removed %d bytes of padding\n",ret);
  680. // Resync trailing headers in order to find next file chunck in the tar file
  681. return NEED_SYNC_TRAIL;
  682. break;
  683. case EXTHEADERTYPE:
  684. fprintf(stderr,"Extended HEADER encountered\n");
  685. return NEED_SKIP;
  686. break;
  687. default:
  688. fprintf(stderr,"Default type detected:%c\n",hd->typeflag);
  689. return NEED_SKIP;
  690. /*
  691. * these types have file data that follows. Set the skip and
  692. * pad fields.
  693. */
  694. // arcn->type = PAX_REG;
  695. // arcn->pad = TAR_PAD (arcn->sb.st_size);
  696. // arcn->skip = arcn->sb.st_size;
  697. // arcn->sb.st_mode |= S_IFREG;
  698. break;
  699. }
  700. return NEED_SKIP;
  701. }
  702. void tar_file_processor(int fd, struct filters *filters)
  703. {
  704. int ret;
  705. int i;
  706. int current;
  707. struct file_header hdr;
  708. struct stat sb; /* stat buffer see stat(2) */
  709. char buf[BLKMULT+1];
  710. size_t size;
  711. i=0;
  712. current = NEED_READ;
  713. size_t to_skip = 0;
  714. int sync_count = 0;
  715. while ((size = read(fd, &buf, BLKMULT))) {
  716. if (size != -1) {
  717. fprintf(stderr,"Read %ld bytes\n",size);
  718. ret = 0;
  719. if (current==NEED_SYNC_TRAIL) {
  720. ret = tar_trail (buf, 1, &sync_count);
  721. fprintf(stderr,"Synchronizing trail: %d %d\n",ret,sync_count);
  722. if (ret != 1) {
  723. current = NEED_READ;
  724. }
  725. }
  726. if (current==NEED_READ) {
  727. current = ustar_rd(fd, &hdr, buf, &sb, filters);
  728. fprintf(stderr,"Return %d\n",ret);
  729. }
  730. if (current==NEED_SKIP || current==NEED_SKIP_FILE) {
  731. if (current==NEED_SKIP_FILE &&
  732. filters->filters_count > 0 &&
  733. filters->filters_count == filters->matched_filters) {
  734. // This assume that either:
  735. // a) files are sorted (using full path as sort key)
  736. // b) all the directory content is in
  737. // consecutive block and only directories
  738. // are given as filters
  739. // This is true for backups prepared by qvm-backup
  740. #ifdef DEBUG
  741. fprintf(stderr, "All filters matched at least once - assuming end of requested data\n");
  742. #endif
  743. return;
  744. }
  745. #ifdef DEBUG
  746. fprintf(stderr,"Need to skip %lld bytes (matched filters %d < %d)\n",
  747. hdr.filelen, filters->matched_filters, filters->filters_count);
  748. #endif
  749. to_skip = hdr.filelen;
  750. while (to_skip > 0) {
  751. to_skip -= read(fd, &buf, MIN(to_skip,BLKMULT));
  752. }
  753. // Extract extra padding
  754. fprintf(stderr,"Need to remove pad:%ld %lld %lld\n",to_skip,hdr.filelen,BLKMULT-(hdr.filelen%BLKMULT));
  755. if (hdr.filelen%BLKMULT > 0) {
  756. ret = read(fd, &buf, BLKMULT-(hdr.filelen%BLKMULT));
  757. fprintf(stderr,"Removed %d bytes of padding\n",ret);
  758. }
  759. current = NEED_SYNC_TRAIL;
  760. }
  761. i++;
  762. }
  763. //if (i >= 10)
  764. // exit(0);
  765. }
  766. }
  767. int main(int argc, char **argv)
  768. {
  769. int i;
  770. char *entry;
  771. int fd;
  772. int use_stdin = 1;
  773. struct filters filters;
  774. signal(SIGPIPE, SIG_IGN);
  775. // this will allow checking for possible feedback packet in the middle of transfer
  776. // if disabled, the copy_file process could hang
  777. notify_progress(0, PROGRESS_FLAG_INIT);
  778. //set_size_limit(1500000000, 2048);
  779. crc32_sum = 0;
  780. /* when extracting backup header, dom0 will terminate the transfer with
  781. * EDQUOT just after getting qubes.xml */
  782. ignore_quota_error = 1;
  783. for (i = 1; i < argc; i++) {
  784. set_nonblock(0);
  785. if (strcmp(argv[i], "--ignore-symlinks")==0) {
  786. ignore_symlinks = 1;
  787. continue;
  788. } else if (strcmp(argv[i], "-")==0) {
  789. use_stdin = 1;
  790. i++;
  791. break;
  792. } else {
  793. // Parse tar file
  794. use_stdin = 0;
  795. entry = argv[i];
  796. fprintf(stderr,"Parsing file %s\n",entry);
  797. fd = open(entry, O_RDONLY);
  798. if (fd < 0) {
  799. fprintf(stderr,"Error opening file %s\n",entry);
  800. exit(2);
  801. }
  802. i++;
  803. break;
  804. }
  805. }
  806. filters.filters_count = argc-i;
  807. filters.filters = argv+i;
  808. filters.filters_matches = calloc(filters.filters_count, sizeof(int));
  809. if (filters.filters_matches == NULL) {
  810. perror("calloc");
  811. exit(1);
  812. }
  813. filters.matched_filters = 0;
  814. if (use_stdin == 1) {
  815. // No argument specified. Use STDIN
  816. fprintf(stderr,"Using STDIN\n");
  817. set_block(0);
  818. fd = 0;
  819. }
  820. tar_file_processor(fd, &filters);
  821. notify_end_and_wait_for_result();
  822. notify_progress(0, PROGRESS_FLAG_DONE);
  823. return 0;
  824. }