archive_write_disk.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_disk.c,v 1.42 2008/12/06 05:55:46 kientzle Exp $");
  28. #ifdef HAVE_SYS_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31. #ifdef HAVE_SYS_ACL_H
  32. #include <sys/acl.h>
  33. #endif
  34. #ifdef HAVE_SYS_EXTATTR_H
  35. #include <sys/extattr.h>
  36. #endif
  37. #ifdef HAVE_SYS_XATTR_H
  38. #include <sys/xattr.h>
  39. #endif
  40. #ifdef HAVE_ATTR_XATTR_H
  41. #include <attr/xattr.h>
  42. #endif
  43. #ifdef HAVE_SYS_IOCTL_H
  44. #include <sys/ioctl.h>
  45. #endif
  46. #ifdef HAVE_SYS_STAT_H
  47. #include <sys/stat.h>
  48. #endif
  49. #ifdef HAVE_SYS_TIME_H
  50. #include <sys/time.h>
  51. #endif
  52. #ifdef HAVE_SYS_UTIME_H
  53. #include <sys/utime.h>
  54. #endif
  55. #ifdef HAVE_ERRNO_H
  56. #include <errno.h>
  57. #endif
  58. #ifdef HAVE_FCNTL_H
  59. #include <fcntl.h>
  60. #endif
  61. #ifdef HAVE_GRP_H
  62. #include <grp.h>
  63. #endif
  64. #ifdef HAVE_LINUX_FS_H
  65. #include <linux/fs.h> /* for Linux file flags */
  66. #endif
  67. /*
  68. * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
  69. * As the include guards don't agree, the order of include is important.
  70. */
  71. #ifdef HAVE_LINUX_EXT2_FS_H
  72. #include <linux/ext2_fs.h> /* for Linux file flags */
  73. #endif
  74. #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
  75. #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
  76. #endif
  77. #ifdef HAVE_LIMITS_H
  78. #include <limits.h>
  79. #endif
  80. #ifdef HAVE_PWD_H
  81. #include <pwd.h>
  82. #endif
  83. #include <stdio.h>
  84. #ifdef HAVE_STDLIB_H
  85. #include <stdlib.h>
  86. #endif
  87. #ifdef HAVE_STRING_H
  88. #include <string.h>
  89. #endif
  90. #ifdef HAVE_UNISTD_H
  91. #include <unistd.h>
  92. #endif
  93. #ifdef HAVE_UTIME_H
  94. #include <utime.h>
  95. #endif
  96. #include "archive.h"
  97. #include "archive_string.h"
  98. #include "archive_entry.h"
  99. #include "archive_private.h"
  100. #ifndef O_BINARY
  101. #define O_BINARY 0
  102. #endif
  103. struct fixup_entry {
  104. struct fixup_entry *next;
  105. mode_t mode;
  106. int64_t atime;
  107. int64_t birthtime;
  108. int64_t mtime;
  109. unsigned long atime_nanos;
  110. unsigned long birthtime_nanos;
  111. unsigned long mtime_nanos;
  112. unsigned long fflags_set;
  113. int fixup; /* bitmask of what needs fixing */
  114. char *name;
  115. };
  116. /*
  117. * We use a bitmask to track which operations remain to be done for
  118. * this file. In particular, this helps us avoid unnecessary
  119. * operations when it's possible to take care of one step as a
  120. * side-effect of another. For example, mkdir() can specify the mode
  121. * for the newly-created object but symlink() cannot. This means we
  122. * can skip chmod() if mkdir() succeeded, but we must explicitly
  123. * chmod() if we're trying to create a directory that already exists
  124. * (mkdir() failed) or if we're restoring a symlink. Similarly, we
  125. * need to verify UID/GID before trying to restore SUID/SGID bits;
  126. * that verification can occur explicitly through a stat() call or
  127. * implicitly because of a successful chown() call.
  128. */
  129. #define TODO_MODE_FORCE 0x40000000
  130. #define TODO_MODE_BASE 0x20000000
  131. #define TODO_SUID 0x10000000
  132. #define TODO_SUID_CHECK 0x08000000
  133. #define TODO_SGID 0x04000000
  134. #define TODO_SGID_CHECK 0x02000000
  135. #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
  136. #define TODO_TIMES ARCHIVE_EXTRACT_TIME
  137. #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
  138. #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
  139. #define TODO_ACLS ARCHIVE_EXTRACT_ACL
  140. #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
  141. struct archive_write_disk {
  142. struct archive archive;
  143. mode_t user_umask;
  144. struct fixup_entry *fixup_list;
  145. struct fixup_entry *current_fixup;
  146. uid_t user_uid;
  147. dev_t skip_file_dev;
  148. ino_t skip_file_ino;
  149. time_t start_time;
  150. gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid);
  151. void (*cleanup_gid)(void *private);
  152. void *lookup_gid_data;
  153. uid_t (*lookup_uid)(void *private, const char *gname, gid_t gid);
  154. void (*cleanup_uid)(void *private);
  155. void *lookup_uid_data;
  156. /*
  157. * Full path of last file to satisfy symlink checks.
  158. */
  159. struct archive_string path_safe;
  160. /*
  161. * Cached stat data from disk for the current entry.
  162. * If this is valid, pst points to st. Otherwise,
  163. * pst is null.
  164. */
  165. struct stat st;
  166. struct stat *pst;
  167. /* Information about the object being restored right now. */
  168. struct archive_entry *entry; /* Entry being extracted. */
  169. char *name; /* Name of entry, possibly edited. */
  170. struct archive_string _name_data; /* backing store for 'name' */
  171. /* Tasks remaining for this object. */
  172. int todo;
  173. /* Tasks deferred until end-of-archive. */
  174. int deferred;
  175. /* Options requested by the client. */
  176. int flags;
  177. /* Handle for the file we're restoring. */
  178. int fd;
  179. /* Current offset for writing data to the file. */
  180. off_t offset;
  181. /* Last offset actually written to disk. */
  182. off_t fd_offset;
  183. /* Maximum size of file, -1 if unknown. */
  184. off_t filesize;
  185. /* Dir we were in before this restore; only for deep paths. */
  186. int restore_pwd;
  187. /* Mode we should use for this entry; affected by _PERM and umask. */
  188. mode_t mode;
  189. /* UID/GID to use in restoring this entry. */
  190. uid_t uid;
  191. gid_t gid;
  192. };
  193. /*
  194. * Default mode for dirs created automatically (will be modified by umask).
  195. * Note that POSIX specifies 0777 for implicity-created dirs, "modified
  196. * by the process' file creation mask."
  197. */
  198. #define DEFAULT_DIR_MODE 0777
  199. /*
  200. * Dir modes are restored in two steps: During the extraction, the permissions
  201. * in the archive are modified to match the following limits. During
  202. * the post-extract fixup pass, the permissions from the archive are
  203. * applied.
  204. */
  205. #define MINIMUM_DIR_MODE 0700
  206. #define MAXIMUM_DIR_MODE 0775
  207. static int check_symlinks(struct archive_write_disk *);
  208. static int create_filesystem_object(struct archive_write_disk *);
  209. static struct fixup_entry *current_fixup(struct archive_write_disk *, const char *pathname);
  210. #ifdef HAVE_FCHDIR
  211. static void edit_deep_directories(struct archive_write_disk *ad);
  212. #endif
  213. static int cleanup_pathname(struct archive_write_disk *);
  214. static int create_dir(struct archive_write_disk *, char *);
  215. static int create_parent_dir(struct archive_write_disk *, char *);
  216. static int older(struct stat *, struct archive_entry *);
  217. static int restore_entry(struct archive_write_disk *);
  218. #ifdef HAVE_POSIX_ACL
  219. static int set_acl(struct archive_write_disk *, int fd, struct archive_entry *,
  220. acl_type_t, int archive_entry_acl_type, const char *tn);
  221. #endif
  222. static int set_acls(struct archive_write_disk *);
  223. static int set_xattrs(struct archive_write_disk *);
  224. static int set_fflags(struct archive_write_disk *);
  225. static int set_fflags_platform(struct archive_write_disk *, int fd,
  226. const char *name, mode_t mode,
  227. unsigned long fflags_set, unsigned long fflags_clear);
  228. static int set_ownership(struct archive_write_disk *);
  229. static int set_mode(struct archive_write_disk *, int mode);
  230. static int set_time(int, int, const char *, time_t, long, time_t, long);
  231. static int set_times(struct archive_write_disk *);
  232. static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
  233. static gid_t trivial_lookup_gid(void *, const char *, gid_t);
  234. static uid_t trivial_lookup_uid(void *, const char *, uid_t);
  235. static ssize_t write_data_block(struct archive_write_disk *,
  236. const char *, size_t);
  237. static struct archive_vtable *archive_write_disk_vtable(void);
  238. static int _archive_write_close(struct archive *);
  239. static int _archive_write_finish(struct archive *);
  240. static int _archive_write_header(struct archive *, struct archive_entry *);
  241. static int _archive_write_finish_entry(struct archive *);
  242. static ssize_t _archive_write_data(struct archive *, const void *, size_t);
  243. static ssize_t _archive_write_data_block(struct archive *, const void *, size_t, off_t);
  244. static int
  245. _archive_write_disk_lazy_stat(struct archive_write_disk *a)
  246. {
  247. if (a->pst != NULL) {
  248. /* Already have stat() data available. */
  249. return (ARCHIVE_OK);
  250. }
  251. #ifdef HAVE_FSTAT
  252. if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) {
  253. a->pst = &a->st;
  254. return (ARCHIVE_OK);
  255. }
  256. #endif
  257. /*
  258. * XXX At this point, symlinks should not be hit, otherwise
  259. * XXX a race occured. Do we want to check explicitly for that?
  260. */
  261. if (lstat(a->name, &a->st) == 0) {
  262. a->pst = &a->st;
  263. return (ARCHIVE_OK);
  264. }
  265. archive_set_error(&a->archive, errno, "Couldn't stat file");
  266. return (ARCHIVE_WARN);
  267. }
  268. static struct archive_vtable *
  269. archive_write_disk_vtable(void)
  270. {
  271. static struct archive_vtable av;
  272. static int inited = 0;
  273. if (!inited) {
  274. av.archive_close = _archive_write_close;
  275. av.archive_finish = _archive_write_finish;
  276. av.archive_write_header = _archive_write_header;
  277. av.archive_write_finish_entry = _archive_write_finish_entry;
  278. av.archive_write_data = _archive_write_data;
  279. av.archive_write_data_block = _archive_write_data_block;
  280. }
  281. return (&av);
  282. }
  283. int
  284. archive_write_disk_set_options(struct archive *_a, int flags)
  285. {
  286. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  287. a->flags = flags;
  288. return (ARCHIVE_OK);
  289. }
  290. /*
  291. * Extract this entry to disk.
  292. *
  293. * TODO: Validate hardlinks. According to the standards, we're
  294. * supposed to check each extracted hardlink and squawk if it refers
  295. * to a file that we didn't restore. I'm not entirely convinced this
  296. * is a good idea, but more importantly: Is there any way to validate
  297. * hardlinks without keeping a complete list of filenames from the
  298. * entire archive?? Ugh.
  299. *
  300. */
  301. static int
  302. _archive_write_header(struct archive *_a, struct archive_entry *entry)
  303. {
  304. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  305. struct fixup_entry *fe;
  306. int ret, r;
  307. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  308. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  309. "archive_write_disk_header");
  310. archive_clear_error(&a->archive);
  311. if (a->archive.state & ARCHIVE_STATE_DATA) {
  312. r = _archive_write_finish_entry(&a->archive);
  313. if (r == ARCHIVE_FATAL)
  314. return (r);
  315. }
  316. /* Set up for this particular entry. */
  317. a->pst = NULL;
  318. a->current_fixup = NULL;
  319. a->deferred = 0;
  320. if (a->entry) {
  321. archive_entry_free(a->entry);
  322. a->entry = NULL;
  323. }
  324. a->entry = archive_entry_clone(entry);
  325. a->fd = -1;
  326. a->fd_offset = 0;
  327. a->offset = 0;
  328. a->uid = a->user_uid;
  329. a->mode = archive_entry_mode(a->entry);
  330. if (archive_entry_size_is_set(a->entry))
  331. a->filesize = archive_entry_size(a->entry);
  332. else
  333. a->filesize = -1;
  334. archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
  335. a->name = a->_name_data.s;
  336. archive_clear_error(&a->archive);
  337. /*
  338. * Clean up the requested path. This is necessary for correct
  339. * dir restores; the dir restore logic otherwise gets messed
  340. * up by nonsense like "dir/.".
  341. */
  342. ret = cleanup_pathname(a);
  343. if (ret != ARCHIVE_OK)
  344. return (ret);
  345. /*
  346. * Set the umask to zero so we get predictable mode settings.
  347. * This gets done on every call to _write_header in case the
  348. * user edits their umask during the extraction for some
  349. * reason. This will be reset before we return. Note that we
  350. * don't need to do this in _finish_entry, as the chmod(), etc,
  351. * system calls don't obey umask.
  352. */
  353. a->user_umask = umask(0);
  354. /* From here on, early exit requires "goto done" to clean up. */
  355. /* Figure out what we need to do for this entry. */
  356. a->todo = TODO_MODE_BASE;
  357. if (a->flags & ARCHIVE_EXTRACT_PERM) {
  358. a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
  359. /*
  360. * SGID requires an extra "check" step because we
  361. * cannot easily predict the GID that the system will
  362. * assign. (Different systems assign GIDs to files
  363. * based on a variety of criteria, including process
  364. * credentials and the gid of the enclosing
  365. * directory.) We can only restore the SGID bit if
  366. * the file has the right GID, and we only know the
  367. * GID if we either set it (see set_ownership) or if
  368. * we've actually called stat() on the file after it
  369. * was restored. Since there are several places at
  370. * which we might verify the GID, we need a TODO bit
  371. * to keep track.
  372. */
  373. if (a->mode & S_ISGID)
  374. a->todo |= TODO_SGID | TODO_SGID_CHECK;
  375. /*
  376. * Verifying the SUID is simpler, but can still be
  377. * done in multiple ways, hence the separate "check" bit.
  378. */
  379. if (a->mode & S_ISUID)
  380. a->todo |= TODO_SUID | TODO_SUID_CHECK;
  381. } else {
  382. /*
  383. * User didn't request full permissions, so don't
  384. * restore SUID, SGID bits and obey umask.
  385. */
  386. a->mode &= ~S_ISUID;
  387. a->mode &= ~S_ISGID;
  388. a->mode &= ~S_ISVTX;
  389. a->mode &= ~a->user_umask;
  390. }
  391. #if !defined(_WIN32) || defined(__CYGWIN__)
  392. if (a->flags & ARCHIVE_EXTRACT_OWNER)
  393. a->todo |= TODO_OWNER;
  394. #endif
  395. if (a->flags & ARCHIVE_EXTRACT_TIME)
  396. a->todo |= TODO_TIMES;
  397. if (a->flags & ARCHIVE_EXTRACT_ACL)
  398. a->todo |= TODO_ACLS;
  399. if (a->flags & ARCHIVE_EXTRACT_XATTR)
  400. a->todo |= TODO_XATTR;
  401. if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
  402. a->todo |= TODO_FFLAGS;
  403. if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
  404. ret = check_symlinks(a);
  405. if (ret != ARCHIVE_OK)
  406. goto done;
  407. }
  408. #ifdef HAVE_FCHDIR
  409. /* If path exceeds PATH_MAX, shorten the path. */
  410. edit_deep_directories(a);
  411. #endif
  412. ret = restore_entry(a);
  413. /*
  414. * On the GNU tar mailing list, some people working with new
  415. * Linux filesystems observed that system xattrs used as
  416. * layout hints need to be restored before the file contents
  417. * are written, so this can't be done at file close.
  418. */
  419. if (a->todo & TODO_XATTR) {
  420. int r2 = set_xattrs(a);
  421. if (r2 < ret) ret = r2;
  422. }
  423. #ifdef HAVE_FCHDIR
  424. /* If we changed directory above, restore it here. */
  425. if (a->restore_pwd >= 0) {
  426. r = fchdir(a->restore_pwd);
  427. if (r != 0) {
  428. archive_set_error(&a->archive, errno, "chdir() failure");
  429. ret = ARCHIVE_FATAL;
  430. }
  431. close(a->restore_pwd);
  432. a->restore_pwd = -1;
  433. }
  434. #endif
  435. /*
  436. * Fixup uses the unedited pathname from archive_entry_pathname(),
  437. * because it is relative to the base dir and the edited path
  438. * might be relative to some intermediate dir as a result of the
  439. * deep restore logic.
  440. */
  441. if (a->deferred & TODO_MODE) {
  442. fe = current_fixup(a, archive_entry_pathname(entry));
  443. fe->fixup |= TODO_MODE_BASE;
  444. fe->mode = a->mode;
  445. }
  446. if ((a->deferred & TODO_TIMES)
  447. && (archive_entry_mtime_is_set(entry)
  448. || archive_entry_atime_is_set(entry))) {
  449. fe = current_fixup(a, archive_entry_pathname(entry));
  450. fe->fixup |= TODO_TIMES;
  451. if (archive_entry_atime_is_set(entry)) {
  452. fe->atime = archive_entry_atime(entry);
  453. fe->atime_nanos = archive_entry_atime_nsec(entry);
  454. } else {
  455. /* If atime is unset, use start time. */
  456. fe->atime = a->start_time;
  457. fe->atime_nanos = 0;
  458. }
  459. if (archive_entry_mtime_is_set(entry)) {
  460. fe->mtime = archive_entry_mtime(entry);
  461. fe->mtime_nanos = archive_entry_mtime_nsec(entry);
  462. } else {
  463. /* If mtime is unset, use start time. */
  464. fe->mtime = a->start_time;
  465. fe->mtime_nanos = 0;
  466. }
  467. if (archive_entry_birthtime_is_set(entry)) {
  468. fe->birthtime = archive_entry_birthtime(entry);
  469. fe->birthtime_nanos = archive_entry_birthtime_nsec(entry);
  470. } else {
  471. /* If birthtime is unset, use mtime. */
  472. fe->birthtime = fe->mtime;
  473. fe->birthtime_nanos = fe->mtime_nanos;
  474. }
  475. }
  476. if (a->deferred & TODO_FFLAGS) {
  477. fe = current_fixup(a, archive_entry_pathname(entry));
  478. fe->fixup |= TODO_FFLAGS;
  479. /* TODO: Complete this.. defer fflags from below. */
  480. }
  481. /* We've created the object and are ready to pour data into it. */
  482. if (ret >= ARCHIVE_WARN)
  483. a->archive.state = ARCHIVE_STATE_DATA;
  484. /*
  485. * If it's not open, tell our client not to try writing.
  486. * In particular, dirs, links, etc, don't get written to.
  487. */
  488. if (a->fd < 0) {
  489. archive_entry_set_size(entry, 0);
  490. a->filesize = 0;
  491. }
  492. done:
  493. /* Restore the user's umask before returning. */
  494. umask(a->user_umask);
  495. return (ret);
  496. }
  497. int
  498. archive_write_disk_set_skip_file(struct archive *_a, dev_t d, ino_t i)
  499. {
  500. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  501. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  502. ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
  503. a->skip_file_dev = d;
  504. a->skip_file_ino = i;
  505. return (ARCHIVE_OK);
  506. }
  507. static ssize_t
  508. write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
  509. {
  510. uint64_t start_size = size;
  511. ssize_t bytes_written = 0;
  512. ssize_t block_size = 0, bytes_to_write;
  513. if (size == 0)
  514. return (ARCHIVE_OK);
  515. if (a->filesize == 0 || a->fd < 0) {
  516. archive_set_error(&a->archive, 0,
  517. "Attempt to write to an empty file");
  518. return (ARCHIVE_WARN);
  519. }
  520. if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
  521. #if HAVE_STRUCT_STAT_ST_BLKSIZE
  522. int r;
  523. if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
  524. return (r);
  525. block_size = a->pst->st_blksize;
  526. #else
  527. /* XXX TODO XXX Is there a more appropriate choice here ? */
  528. /* This needn't match the filesystem allocation size. */
  529. block_size = 16*1024;
  530. #endif
  531. }
  532. /* If this write would run beyond the file size, truncate it. */
  533. if (a->filesize >= 0 && (off_t)(a->offset + size) > a->filesize)
  534. start_size = size = (size_t)(a->filesize - a->offset);
  535. /* Write the data. */
  536. while (size > 0) {
  537. if (block_size == 0) {
  538. bytes_to_write = size;
  539. } else {
  540. /* We're sparsifying the file. */
  541. const char *p, *end;
  542. off_t block_end;
  543. /* Skip leading zero bytes. */
  544. for (p = buff, end = buff + size; p < end; ++p) {
  545. if (*p != '\0')
  546. break;
  547. }
  548. a->offset += p - buff;
  549. size -= p - buff;
  550. buff = p;
  551. if (size == 0)
  552. break;
  553. /* Calculate next block boundary after offset. */
  554. block_end
  555. = (a->offset / block_size + 1) * block_size;
  556. /* If the adjusted write would cross block boundary,
  557. * truncate it to the block boundary. */
  558. bytes_to_write = size;
  559. if (a->offset + bytes_to_write > block_end)
  560. bytes_to_write = block_end - a->offset;
  561. }
  562. /* Seek if necessary to the specified offset. */
  563. if (a->offset != a->fd_offset) {
  564. if (lseek(a->fd, a->offset, SEEK_SET) < 0) {
  565. archive_set_error(&a->archive, errno,
  566. "Seek failed");
  567. return (ARCHIVE_FATAL);
  568. }
  569. a->fd_offset = a->offset;
  570. a->archive.file_position = a->offset;
  571. a->archive.raw_position = a->offset;
  572. }
  573. bytes_written = write(a->fd, buff, bytes_to_write);
  574. if (bytes_written < 0) {
  575. archive_set_error(&a->archive, errno, "Write failed");
  576. return (ARCHIVE_WARN);
  577. }
  578. buff += bytes_written;
  579. size -= bytes_written;
  580. a->offset += bytes_written;
  581. a->archive.file_position += bytes_written;
  582. a->archive.raw_position += bytes_written;
  583. a->fd_offset = a->offset;
  584. }
  585. return (start_size - size);
  586. }
  587. static ssize_t
  588. _archive_write_data_block(struct archive *_a,
  589. const void *buff, size_t size, off_t offset)
  590. {
  591. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  592. ssize_t r;
  593. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  594. ARCHIVE_STATE_DATA, "archive_write_disk_block");
  595. a->offset = offset;
  596. r = write_data_block(a, buff, size);
  597. if (r < ARCHIVE_OK)
  598. return (r);
  599. if ((size_t)r < size) {
  600. archive_set_error(&a->archive, 0,
  601. "Write request too large");
  602. return (ARCHIVE_WARN);
  603. }
  604. return (ARCHIVE_OK);
  605. }
  606. static ssize_t
  607. _archive_write_data(struct archive *_a, const void *buff, size_t size)
  608. {
  609. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  610. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  611. ARCHIVE_STATE_DATA, "archive_write_data");
  612. return (write_data_block(a, buff, size));
  613. }
  614. static int
  615. _archive_write_finish_entry(struct archive *_a)
  616. {
  617. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  618. int ret = ARCHIVE_OK;
  619. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  620. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  621. "archive_write_finish_entry");
  622. if (a->archive.state & ARCHIVE_STATE_HEADER)
  623. return (ARCHIVE_OK);
  624. archive_clear_error(&a->archive);
  625. /* Pad or truncate file to the right size. */
  626. if (a->fd < 0) {
  627. /* There's no file. */
  628. } else if (a->filesize < 0) {
  629. /* File size is unknown, so we can't set the size. */
  630. } else if (a->fd_offset == a->filesize) {
  631. /* Last write ended at exactly the filesize; we're done. */
  632. /* Hopefully, this is the common case. */
  633. } else {
  634. #if HAVE_FTRUNCATE
  635. if (ftruncate(a->fd, a->filesize) == -1 &&
  636. a->filesize == 0) {
  637. archive_set_error(&a->archive, errno,
  638. "File size could not be restored");
  639. return (ARCHIVE_FAILED);
  640. }
  641. #endif
  642. /*
  643. * Not all platforms implement the XSI option to
  644. * extend files via ftruncate. Stat() the file again
  645. * to see what happened.
  646. */
  647. a->pst = NULL;
  648. if ((ret = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
  649. return (ret);
  650. /* We can use lseek()/write() to extend the file if
  651. * ftruncate didn't work or isn't available. */
  652. if (a->st.st_size < a->filesize) {
  653. const char nul = '\0';
  654. if (lseek(a->fd, a->filesize - 1, SEEK_SET) < 0) {
  655. archive_set_error(&a->archive, errno,
  656. "Seek failed");
  657. return (ARCHIVE_FATAL);
  658. }
  659. if (write(a->fd, &nul, 1) < 0) {
  660. archive_set_error(&a->archive, errno,
  661. "Write to restore size failed");
  662. return (ARCHIVE_FATAL);
  663. }
  664. a->pst = NULL;
  665. }
  666. }
  667. /* Restore metadata. */
  668. /*
  669. * Look up the "real" UID only if we're going to need it.
  670. * TODO: the TODO_SGID condition can be dropped here, can't it?
  671. */
  672. if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
  673. a->uid = a->lookup_uid(a->lookup_uid_data,
  674. archive_entry_uname(a->entry),
  675. archive_entry_uid(a->entry));
  676. }
  677. /* Look up the "real" GID only if we're going to need it. */
  678. /* TODO: the TODO_SUID condition can be dropped here, can't it? */
  679. if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
  680. a->gid = a->lookup_gid(a->lookup_gid_data,
  681. archive_entry_gname(a->entry),
  682. archive_entry_gid(a->entry));
  683. }
  684. /*
  685. * If restoring ownership, do it before trying to restore suid/sgid
  686. * bits. If we set the owner, we know what it is and can skip
  687. * a stat() call to examine the ownership of the file on disk.
  688. */
  689. if (a->todo & TODO_OWNER)
  690. ret = set_ownership(a);
  691. if (a->todo & TODO_MODE) {
  692. int r2 = set_mode(a, a->mode);
  693. if (r2 < ret) ret = r2;
  694. }
  695. if (a->todo & TODO_ACLS) {
  696. int r2 = set_acls(a);
  697. if (r2 < ret) ret = r2;
  698. }
  699. /*
  700. * Some flags prevent file modification; they must be restored after
  701. * file contents are written.
  702. */
  703. if (a->todo & TODO_FFLAGS) {
  704. int r2 = set_fflags(a);
  705. if (r2 < ret) ret = r2;
  706. }
  707. /*
  708. * Time has to be restored after all other metadata;
  709. * otherwise atime will get changed.
  710. */
  711. if (a->todo & TODO_TIMES) {
  712. int r2 = set_times(a);
  713. if (r2 < ret) ret = r2;
  714. }
  715. /* If there's an fd, we can close it now. */
  716. if (a->fd >= 0) {
  717. close(a->fd);
  718. a->fd = -1;
  719. }
  720. /* If there's an entry, we can release it now. */
  721. if (a->entry) {
  722. archive_entry_free(a->entry);
  723. a->entry = NULL;
  724. }
  725. a->archive.state = ARCHIVE_STATE_HEADER;
  726. return (ret);
  727. }
  728. int
  729. archive_write_disk_set_group_lookup(struct archive *_a,
  730. void *private_data,
  731. gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid),
  732. void (*cleanup_gid)(void *private))
  733. {
  734. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  735. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  736. ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
  737. a->lookup_gid = lookup_gid;
  738. a->cleanup_gid = cleanup_gid;
  739. a->lookup_gid_data = private_data;
  740. return (ARCHIVE_OK);
  741. }
  742. int
  743. archive_write_disk_set_user_lookup(struct archive *_a,
  744. void *private_data,
  745. uid_t (*lookup_uid)(void *private, const char *uname, uid_t uid),
  746. void (*cleanup_uid)(void *private))
  747. {
  748. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  749. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  750. ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
  751. a->lookup_uid = lookup_uid;
  752. a->cleanup_uid = cleanup_uid;
  753. a->lookup_uid_data = private_data;
  754. return (ARCHIVE_OK);
  755. }
  756. /*
  757. * Create a new archive_write_disk object and initialize it with global state.
  758. */
  759. struct archive *
  760. archive_write_disk_new(void)
  761. {
  762. struct archive_write_disk *a;
  763. a = (struct archive_write_disk *)malloc(sizeof(*a));
  764. if (a == NULL)
  765. return (NULL);
  766. memset(a, 0, sizeof(*a));
  767. a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
  768. /* We're ready to write a header immediately. */
  769. a->archive.state = ARCHIVE_STATE_HEADER;
  770. a->archive.vtable = archive_write_disk_vtable();
  771. a->lookup_uid = trivial_lookup_uid;
  772. a->lookup_gid = trivial_lookup_gid;
  773. a->start_time = time(NULL);
  774. #ifdef HAVE_GETEUID
  775. a->user_uid = geteuid();
  776. #endif /* HAVE_GETEUID */
  777. if (archive_string_ensure(&a->path_safe, 512) == NULL) {
  778. free(a);
  779. return (NULL);
  780. }
  781. return (&a->archive);
  782. }
  783. /*
  784. * If pathname is longer than PATH_MAX, chdir to a suitable
  785. * intermediate dir and edit the path down to a shorter suffix. Note
  786. * that this routine never returns an error; if the chdir() attempt
  787. * fails for any reason, we just go ahead with the long pathname. The
  788. * object creation is likely to fail, but any error will get handled
  789. * at that time.
  790. */
  791. #ifdef HAVE_FCHDIR
  792. static void
  793. edit_deep_directories(struct archive_write_disk *a)
  794. {
  795. int ret;
  796. char *tail = a->name;
  797. a->restore_pwd = -1;
  798. /* If path is short, avoid the open() below. */
  799. if (strlen(tail) <= PATH_MAX)
  800. return;
  801. /* Try to record our starting dir. */
  802. a->restore_pwd = open(".", O_RDONLY | O_BINARY);
  803. if (a->restore_pwd < 0)
  804. return;
  805. /* As long as the path is too long... */
  806. while (strlen(tail) > PATH_MAX) {
  807. /* Locate a dir prefix shorter than PATH_MAX. */
  808. tail += PATH_MAX - 8;
  809. while (tail > a->name && *tail != '/')
  810. tail--;
  811. /* Exit if we find a too-long path component. */
  812. if (tail <= a->name)
  813. return;
  814. /* Create the intermediate dir and chdir to it. */
  815. *tail = '\0'; /* Terminate dir portion */
  816. ret = create_dir(a, a->name);
  817. if (ret == ARCHIVE_OK && chdir(a->name) != 0)
  818. ret = ARCHIVE_FAILED;
  819. *tail = '/'; /* Restore the / we removed. */
  820. if (ret != ARCHIVE_OK)
  821. return;
  822. tail++;
  823. /* The chdir() succeeded; we've now shortened the path. */
  824. a->name = tail;
  825. }
  826. return;
  827. }
  828. #endif
  829. /*
  830. * The main restore function.
  831. */
  832. static int
  833. restore_entry(struct archive_write_disk *a)
  834. {
  835. int ret = ARCHIVE_OK, en;
  836. if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
  837. /*
  838. * TODO: Fix this. Apparently, there are platforms
  839. * that still allow root to hose the entire filesystem
  840. * by unlinking a dir. The S_ISDIR() test above
  841. * prevents us from using unlink() here if the new
  842. * object is a dir, but that doesn't mean the old
  843. * object isn't a dir.
  844. */
  845. if (unlink(a->name) == 0) {
  846. /* We removed it, reset cached stat. */
  847. a->pst = NULL;
  848. } else if (errno == ENOENT) {
  849. /* File didn't exist, that's just as good. */
  850. } else if (rmdir(a->name) == 0) {
  851. /* It was a dir, but now it's gone. */
  852. a->pst = NULL;
  853. } else {
  854. /* We tried, but couldn't get rid of it. */
  855. archive_set_error(&a->archive, errno,
  856. "Could not unlink");
  857. return(ARCHIVE_FAILED);
  858. }
  859. }
  860. /* Try creating it first; if this fails, we'll try to recover. */
  861. en = create_filesystem_object(a);
  862. if ((en == ENOTDIR || en == ENOENT)
  863. && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
  864. /* If the parent dir doesn't exist, try creating it. */
  865. create_parent_dir(a, a->name);
  866. /* Now try to create the object again. */
  867. en = create_filesystem_object(a);
  868. }
  869. if ((en == EISDIR || en == EEXIST)
  870. && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  871. /* If we're not overwriting, we're done. */
  872. archive_set_error(&a->archive, en, "Already exists");
  873. return (ARCHIVE_FAILED);
  874. }
  875. /*
  876. * Some platforms return EISDIR if you call
  877. * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
  878. * return EEXIST. POSIX is ambiguous, requiring EISDIR
  879. * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
  880. * on an existing item.
  881. */
  882. if (en == EISDIR) {
  883. /* A dir is in the way of a non-dir, rmdir it. */
  884. if (rmdir(a->name) != 0) {
  885. archive_set_error(&a->archive, errno,
  886. "Can't remove already-existing dir");
  887. return (ARCHIVE_FAILED);
  888. }
  889. a->pst = NULL;
  890. /* Try again. */
  891. en = create_filesystem_object(a);
  892. } else if (en == EEXIST) {
  893. /*
  894. * We know something is in the way, but we don't know what;
  895. * we need to find out before we go any further.
  896. */
  897. int r = 0;
  898. /*
  899. * The SECURE_SYMLINK logic has already removed a
  900. * symlink to a dir if the client wants that. So
  901. * follow the symlink if we're creating a dir.
  902. */
  903. if (S_ISDIR(a->mode))
  904. r = stat(a->name, &a->st);
  905. /*
  906. * If it's not a dir (or it's a broken symlink),
  907. * then don't follow it.
  908. */
  909. if (r != 0 || !S_ISDIR(a->mode))
  910. r = lstat(a->name, &a->st);
  911. if (r != 0) {
  912. archive_set_error(&a->archive, errno,
  913. "Can't stat existing object");
  914. return (ARCHIVE_FAILED);
  915. }
  916. /*
  917. * NO_OVERWRITE_NEWER doesn't apply to directories.
  918. */
  919. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
  920. && !S_ISDIR(a->st.st_mode)) {
  921. if (!older(&(a->st), a->entry)) {
  922. archive_set_error(&a->archive, 0,
  923. "File on disk is not older; skipping.");
  924. return (ARCHIVE_FAILED);
  925. }
  926. }
  927. /* If it's our archive, we're done. */
  928. if (a->skip_file_dev > 0 &&
  929. a->skip_file_ino > 0 &&
  930. a->st.st_dev == a->skip_file_dev &&
  931. a->st.st_ino == a->skip_file_ino) {
  932. archive_set_error(&a->archive, 0, "Refusing to overwrite archive");
  933. return (ARCHIVE_FAILED);
  934. }
  935. if (!S_ISDIR(a->st.st_mode)) {
  936. /* A non-dir is in the way, unlink it. */
  937. if (unlink(a->name) != 0) {
  938. archive_set_error(&a->archive, errno,
  939. "Can't unlink already-existing object");
  940. return (ARCHIVE_FAILED);
  941. }
  942. a->pst = NULL;
  943. /* Try again. */
  944. en = create_filesystem_object(a);
  945. } else if (!S_ISDIR(a->mode)) {
  946. /* A dir is in the way of a non-dir, rmdir it. */
  947. if (rmdir(a->name) != 0) {
  948. archive_set_error(&a->archive, errno,
  949. "Can't remove already-existing dir");
  950. return (ARCHIVE_FAILED);
  951. }
  952. /* Try again. */
  953. en = create_filesystem_object(a);
  954. } else {
  955. /*
  956. * There's a dir in the way of a dir. Don't
  957. * waste time with rmdir()/mkdir(), just fix
  958. * up the permissions on the existing dir.
  959. * Note that we don't change perms on existing
  960. * dirs unless _EXTRACT_PERM is specified.
  961. */
  962. if ((a->mode != a->st.st_mode)
  963. && (a->todo & TODO_MODE_FORCE))
  964. a->deferred |= (a->todo & TODO_MODE);
  965. /* Ownership doesn't need deferred fixup. */
  966. en = 0; /* Forget the EEXIST. */
  967. }
  968. }
  969. if (en) {
  970. /* Everything failed; give up here. */
  971. archive_set_error(&a->archive, en, "Can't create '%s'",
  972. a->name);
  973. return (ARCHIVE_FAILED);
  974. }
  975. a->pst = NULL; /* Cached stat data no longer valid. */
  976. return (ret);
  977. }
  978. /*
  979. * Returns 0 if creation succeeds, or else returns errno value from
  980. * the failed system call. Note: This function should only ever perform
  981. * a single system call.
  982. */
  983. static int
  984. create_filesystem_object(struct archive_write_disk *a)
  985. {
  986. /* Create the entry. */
  987. const char *linkname;
  988. mode_t final_mode, mode;
  989. int r;
  990. /* We identify hard/symlinks according to the link names. */
  991. /* Since link(2) and symlink(2) don't handle modes, we're done here. */
  992. linkname = archive_entry_hardlink(a->entry);
  993. if (linkname != NULL) {
  994. #if !HAVE_LINK
  995. return (EPERM);
  996. #else
  997. r = link(linkname, a->name) ? errno : 0;
  998. /*
  999. * New cpio and pax formats allow hardlink entries
  1000. * to carry data, so we may have to open the file
  1001. * for hardlink entries.
  1002. *
  1003. * If the hardlink was successfully created and
  1004. * the archive doesn't have carry data for it,
  1005. * consider it to be non-authoritive for meta data.
  1006. * This is consistent with GNU tar and BSD pax.
  1007. * If the hardlink does carry data, let the last
  1008. * archive entry decide ownership.
  1009. */
  1010. if (r == 0 && a->filesize <= 0) {
  1011. a->todo = 0;
  1012. a->deferred = 0;
  1013. } if (r == 0 && a->filesize > 0) {
  1014. a->fd = open(a->name, O_WRONLY | O_TRUNC | O_BINARY);
  1015. if (a->fd < 0)
  1016. r = errno;
  1017. }
  1018. return (r);
  1019. #endif
  1020. }
  1021. linkname = archive_entry_symlink(a->entry);
  1022. if (linkname != NULL) {
  1023. #if HAVE_SYMLINK
  1024. return symlink(linkname, a->name) ? errno : 0;
  1025. #else
  1026. return (EPERM);
  1027. #endif
  1028. }
  1029. /*
  1030. * The remaining system calls all set permissions, so let's
  1031. * try to take advantage of that to avoid an extra chmod()
  1032. * call. (Recall that umask is set to zero right now!)
  1033. */
  1034. /* Mode we want for the final restored object (w/o file type bits). */
  1035. final_mode = a->mode & 07777;
  1036. /*
  1037. * The mode that will actually be restored in this step. Note
  1038. * that SUID, SGID, etc, require additional work to ensure
  1039. * security, so we never restore them at this point.
  1040. */
  1041. mode = final_mode & 0777;
  1042. switch (a->mode & AE_IFMT) {
  1043. default:
  1044. /* POSIX requires that we fall through here. */
  1045. /* FALLTHROUGH */
  1046. case AE_IFREG:
  1047. a->fd = open(a->name,
  1048. O_WRONLY | O_CREAT | O_EXCL | O_BINARY, mode);
  1049. r = (a->fd < 0);
  1050. break;
  1051. case AE_IFCHR:
  1052. #ifdef HAVE_MKNOD
  1053. /* Note: we use AE_IFCHR for the case label, and
  1054. * S_IFCHR for the mknod() call. This is correct. */
  1055. r = mknod(a->name, mode | S_IFCHR,
  1056. archive_entry_rdev(a->entry));
  1057. #else
  1058. /* TODO: Find a better way to warn about our inability
  1059. * to restore a char device node. */
  1060. return (EINVAL);
  1061. #endif /* HAVE_MKNOD */
  1062. break;
  1063. case AE_IFBLK:
  1064. #ifdef HAVE_MKNOD
  1065. r = mknod(a->name, mode | S_IFBLK,
  1066. archive_entry_rdev(a->entry));
  1067. #else
  1068. /* TODO: Find a better way to warn about our inability
  1069. * to restore a block device node. */
  1070. return (EINVAL);
  1071. #endif /* HAVE_MKNOD */
  1072. break;
  1073. case AE_IFDIR:
  1074. mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
  1075. r = mkdir(a->name, mode);
  1076. if (r == 0) {
  1077. /* Defer setting dir times. */
  1078. a->deferred |= (a->todo & TODO_TIMES);
  1079. a->todo &= ~TODO_TIMES;
  1080. /* Never use an immediate chmod(). */
  1081. /* We can't avoid the chmod() entirely if EXTRACT_PERM
  1082. * because of SysV SGID inheritance. */
  1083. if ((mode != final_mode)
  1084. || (a->flags & ARCHIVE_EXTRACT_PERM))
  1085. a->deferred |= (a->todo & TODO_MODE);
  1086. a->todo &= ~TODO_MODE;
  1087. }
  1088. break;
  1089. case AE_IFIFO:
  1090. #ifdef HAVE_MKFIFO
  1091. r = mkfifo(a->name, mode);
  1092. #else
  1093. /* TODO: Find a better way to warn about our inability
  1094. * to restore a fifo. */
  1095. return (EINVAL);
  1096. #endif /* HAVE_MKFIFO */
  1097. break;
  1098. }
  1099. /* All the system calls above set errno on failure. */
  1100. if (r)
  1101. return (errno);
  1102. /* If we managed to set the final mode, we've avoided a chmod(). */
  1103. if (mode == final_mode)
  1104. a->todo &= ~TODO_MODE;
  1105. return (0);
  1106. }
  1107. /*
  1108. * Cleanup function for archive_extract. Mostly, this involves processing
  1109. * the fixup list, which is used to address a number of problems:
  1110. * * Dir permissions might prevent us from restoring a file in that
  1111. * dir, so we restore the dir with minimum 0700 permissions first,
  1112. * then correct the mode at the end.
  1113. * * Similarly, the act of restoring a file touches the directory
  1114. * and changes the timestamp on the dir, so we have to touch-up dir
  1115. * timestamps at the end as well.
  1116. * * Some file flags can interfere with the restore by, for example,
  1117. * preventing the creation of hardlinks to those files.
  1118. *
  1119. * Note that tar/cpio do not require that archives be in a particular
  1120. * order; there is no way to know when the last file has been restored
  1121. * within a directory, so there's no way to optimize the memory usage
  1122. * here by fixing up the directory any earlier than the
  1123. * end-of-archive.
  1124. *
  1125. * XXX TODO: Directory ACLs should be restored here, for the same
  1126. * reason we set directory perms here. XXX
  1127. */
  1128. static int
  1129. _archive_write_close(struct archive *_a)
  1130. {
  1131. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1132. struct fixup_entry *next, *p;
  1133. int ret;
  1134. __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1135. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1136. "archive_write_disk_close");
  1137. ret = _archive_write_finish_entry(&a->archive);
  1138. /* Sort dir list so directories are fixed up in depth-first order. */
  1139. p = sort_dir_list(a->fixup_list);
  1140. while (p != NULL) {
  1141. a->pst = NULL; /* Mark stat cache as out-of-date. */
  1142. if (p->fixup & TODO_TIMES) {
  1143. #ifdef HAVE_UTIMES
  1144. /* {f,l,}utimes() are preferred, when available. */
  1145. #if defined(_WIN32) && !defined(__CYGWIN__)
  1146. struct __timeval times[2];
  1147. #else
  1148. struct timeval times[2];
  1149. #endif
  1150. times[0].tv_sec = p->atime;
  1151. times[0].tv_usec = p->atime_nanos / 1000;
  1152. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  1153. /* if it's valid and not mtime, push the birthtime first */
  1154. if (((times[1].tv_sec = p->birthtime) < p->mtime) &&
  1155. (p->birthtime > 0))
  1156. {
  1157. times[1].tv_usec = p->birthtime_nanos / 1000;
  1158. utimes(p->name, times);
  1159. }
  1160. #endif
  1161. times[1].tv_sec = p->mtime;
  1162. times[1].tv_usec = p->mtime_nanos / 1000;
  1163. #ifdef HAVE_LUTIMES
  1164. lutimes(p->name, times);
  1165. #else
  1166. utimes(p->name, times);
  1167. #endif
  1168. #else
  1169. /* utime() is more portable, but less precise. */
  1170. struct utimbuf times;
  1171. times.modtime = p->mtime;
  1172. times.actime = p->atime;
  1173. utime(p->name, &times);
  1174. #endif
  1175. }
  1176. if (p->fixup & TODO_MODE_BASE)
  1177. chmod(p->name, p->mode);
  1178. if (p->fixup & TODO_FFLAGS)
  1179. set_fflags_platform(a, -1, p->name,
  1180. p->mode, p->fflags_set, 0);
  1181. next = p->next;
  1182. free(p->name);
  1183. free(p);
  1184. p = next;
  1185. }
  1186. a->fixup_list = NULL;
  1187. return (ret);
  1188. }
  1189. static int
  1190. _archive_write_finish(struct archive *_a)
  1191. {
  1192. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1193. int ret;
  1194. ret = _archive_write_close(&a->archive);
  1195. if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
  1196. (a->cleanup_gid)(a->lookup_gid_data);
  1197. if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
  1198. (a->cleanup_uid)(a->lookup_uid_data);
  1199. if (a->entry)
  1200. archive_entry_free(a->entry);
  1201. archive_string_free(&a->_name_data);
  1202. archive_string_free(&a->archive.error_string);
  1203. archive_string_free(&a->path_safe);
  1204. free(a);
  1205. return (ret);
  1206. }
  1207. /*
  1208. * Simple O(n log n) merge sort to order the fixup list. In
  1209. * particular, we want to restore dir timestamps depth-first.
  1210. */
  1211. static struct fixup_entry *
  1212. sort_dir_list(struct fixup_entry *p)
  1213. {
  1214. struct fixup_entry *a, *b, *t;
  1215. if (p == NULL)
  1216. return (NULL);
  1217. /* A one-item list is already sorted. */
  1218. if (p->next == NULL)
  1219. return (p);
  1220. /* Step 1: split the list. */
  1221. t = p;
  1222. a = p->next->next;
  1223. while (a != NULL) {
  1224. /* Step a twice, t once. */
  1225. a = a->next;
  1226. if (a != NULL)
  1227. a = a->next;
  1228. t = t->next;
  1229. }
  1230. /* Now, t is at the mid-point, so break the list here. */
  1231. b = t->next;
  1232. t->next = NULL;
  1233. a = p;
  1234. /* Step 2: Recursively sort the two sub-lists. */
  1235. a = sort_dir_list(a);
  1236. b = sort_dir_list(b);
  1237. /* Step 3: Merge the returned lists. */
  1238. /* Pick the first element for the merged list. */
  1239. if (strcmp(a->name, b->name) > 0) {
  1240. t = p = a;
  1241. a = a->next;
  1242. } else {
  1243. t = p = b;
  1244. b = b->next;
  1245. }
  1246. /* Always put the later element on the list first. */
  1247. while (a != NULL && b != NULL) {
  1248. if (strcmp(a->name, b->name) > 0) {
  1249. t->next = a;
  1250. a = a->next;
  1251. } else {
  1252. t->next = b;
  1253. b = b->next;
  1254. }
  1255. t = t->next;
  1256. }
  1257. /* Only one list is non-empty, so just splice it on. */
  1258. if (a != NULL)
  1259. t->next = a;
  1260. if (b != NULL)
  1261. t->next = b;
  1262. return (p);
  1263. }
  1264. /*
  1265. * Returns a new, initialized fixup entry.
  1266. *
  1267. * TODO: Reduce the memory requirements for this list by using a tree
  1268. * structure rather than a simple list of names.
  1269. */
  1270. static struct fixup_entry *
  1271. new_fixup(struct archive_write_disk *a, const char *pathname)
  1272. {
  1273. struct fixup_entry *fe;
  1274. fe = (struct fixup_entry *)malloc(sizeof(struct fixup_entry));
  1275. if (fe == NULL)
  1276. return (NULL);
  1277. fe->next = a->fixup_list;
  1278. a->fixup_list = fe;
  1279. fe->fixup = 0;
  1280. fe->name = strdup(pathname);
  1281. return (fe);
  1282. }
  1283. /*
  1284. * Returns a fixup structure for the current entry.
  1285. */
  1286. static struct fixup_entry *
  1287. current_fixup(struct archive_write_disk *a, const char *pathname)
  1288. {
  1289. if (a->current_fixup == NULL)
  1290. a->current_fixup = new_fixup(a, pathname);
  1291. return (a->current_fixup);
  1292. }
  1293. /* TODO: Make this work. */
  1294. /*
  1295. * TODO: The deep-directory support bypasses this; disable deep directory
  1296. * support if we're doing symlink checks.
  1297. */
  1298. /*
  1299. * TODO: Someday, integrate this with the deep dir support; they both
  1300. * scan the path and both can be optimized by comparing against other
  1301. * recent paths.
  1302. */
  1303. /* TODO: Extend this to support symlinks on Windows Vista and later. */
  1304. static int
  1305. check_symlinks(struct archive_write_disk *a)
  1306. {
  1307. #if !defined(HAVE_LSTAT)
  1308. /* Platform doesn't have lstat, so we can't look for symlinks. */
  1309. (void)a; /* UNUSED */
  1310. return (ARCHIVE_OK);
  1311. #else
  1312. char *pn, *p;
  1313. char c;
  1314. int r;
  1315. struct stat st;
  1316. /*
  1317. * Guard against symlink tricks. Reject any archive entry whose
  1318. * destination would be altered by a symlink.
  1319. */
  1320. /* Whatever we checked last time doesn't need to be re-checked. */
  1321. pn = a->name;
  1322. p = a->path_safe.s;
  1323. while ((*pn != '\0') && (*p == *pn))
  1324. ++p, ++pn;
  1325. c = pn[0];
  1326. /* Keep going until we've checked the entire name. */
  1327. while (pn[0] != '\0' && (pn[0] != '/' || pn[1] != '\0')) {
  1328. /* Skip the next path element. */
  1329. while (*pn != '\0' && *pn != '/')
  1330. ++pn;
  1331. c = pn[0];
  1332. pn[0] = '\0';
  1333. /* Check that we haven't hit a symlink. */
  1334. r = lstat(a->name, &st);
  1335. if (r != 0) {
  1336. /* We've hit a dir that doesn't exist; stop now. */
  1337. if (errno == ENOENT)
  1338. break;
  1339. } else if (S_ISLNK(st.st_mode)) {
  1340. if (c == '\0') {
  1341. /*
  1342. * Last element is symlink; remove it
  1343. * so we can overwrite it with the
  1344. * item being extracted.
  1345. */
  1346. if (unlink(a->name)) {
  1347. archive_set_error(&a->archive, errno,
  1348. "Could not remove symlink %s",
  1349. a->name);
  1350. pn[0] = c;
  1351. return (ARCHIVE_FAILED);
  1352. }
  1353. a->pst = NULL;
  1354. /*
  1355. * Even if we did remove it, a warning
  1356. * is in order. The warning is silly,
  1357. * though, if we're just replacing one
  1358. * symlink with another symlink.
  1359. */
  1360. if (!S_ISLNK(a->mode)) {
  1361. archive_set_error(&a->archive, 0,
  1362. "Removing symlink %s",
  1363. a->name);
  1364. }
  1365. /* Symlink gone. No more problem! */
  1366. pn[0] = c;
  1367. return (0);
  1368. } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
  1369. /* User asked us to remove problems. */
  1370. if (unlink(a->name) != 0) {
  1371. archive_set_error(&a->archive, 0,
  1372. "Cannot remove intervening symlink %s",
  1373. a->name);
  1374. pn[0] = c;
  1375. return (ARCHIVE_FAILED);
  1376. }
  1377. a->pst = NULL;
  1378. } else {
  1379. archive_set_error(&a->archive, 0,
  1380. "Cannot extract through symlink %s",
  1381. a->name);
  1382. pn[0] = c;
  1383. return (ARCHIVE_FAILED);
  1384. }
  1385. }
  1386. }
  1387. pn[0] = c;
  1388. /* We've checked and/or cleaned the whole path, so remember it. */
  1389. archive_strcpy(&a->path_safe, a->name);
  1390. return (ARCHIVE_OK);
  1391. #endif
  1392. }
  1393. #if defined(_WIN32) || defined(__CYGWIN__)
  1394. /*
  1395. * 1. Convert a path separator from '\' to '/' .
  1396. * We shouldn't check multi-byte character directly because some
  1397. * character-set have been using the '\' character for a part of
  1398. * its multibyte character code.
  1399. * 2. Replace unusable characters in Windows with underscore('_').
  1400. * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx
  1401. */
  1402. static void
  1403. cleanup_pathname_win(struct archive_write_disk *a)
  1404. {
  1405. wchar_t wc;
  1406. char *p;
  1407. size_t alen, l;
  1408. alen = 0;
  1409. l = 0;
  1410. for (p = a->name; *p != '\0'; p++) {
  1411. ++alen;
  1412. if (*p == '\\')
  1413. l = 1;
  1414. /* Rewrite the path name if its character is a unusable. */
  1415. if (*p == ':' || *p == '*' || *p == '?' || *p == '"' ||
  1416. *p == '<' || *p == '>' || *p == '|')
  1417. *p = '_';
  1418. }
  1419. if (alen == 0 || l == 0)
  1420. return;
  1421. /*
  1422. * Convert path separator.
  1423. */
  1424. p = a->name;
  1425. while (*p != '\0' && alen) {
  1426. l = mbtowc(&wc, p, alen);
  1427. if (l == -1) {
  1428. while (*p != '\0') {
  1429. if (*p == '\\')
  1430. *p = '/';
  1431. ++p;
  1432. }
  1433. break;
  1434. }
  1435. if (l == 1 && wc == L'\\')
  1436. *p = '/';
  1437. p += l;
  1438. alen -= l;
  1439. }
  1440. }
  1441. #endif
  1442. /*
  1443. * Canonicalize the pathname. In particular, this strips duplicate
  1444. * '/' characters, '.' elements, and trailing '/'. It also raises an
  1445. * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
  1446. * set) any '..' in the path.
  1447. */
  1448. static int
  1449. cleanup_pathname(struct archive_write_disk *a)
  1450. {
  1451. char *dest, *src;
  1452. char separator = '\0';
  1453. dest = src = a->name;
  1454. if (*src == '\0') {
  1455. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1456. "Invalid empty pathname");
  1457. return (ARCHIVE_FAILED);
  1458. }
  1459. #if defined(_WIN32) || defined(__CYGWIN__)
  1460. cleanup_pathname_win(a);
  1461. #endif
  1462. /* Skip leading '/'. */
  1463. if (*src == '/')
  1464. separator = *src++;
  1465. /* Scan the pathname one element at a time. */
  1466. for (;;) {
  1467. /* src points to first char after '/' */
  1468. if (src[0] == '\0') {
  1469. break;
  1470. } else if (src[0] == '/') {
  1471. /* Found '//', ignore second one. */
  1472. src++;
  1473. continue;
  1474. } else if (src[0] == '.') {
  1475. if (src[1] == '\0') {
  1476. /* Ignore trailing '.' */
  1477. break;
  1478. } else if (src[1] == '/') {
  1479. /* Skip './'. */
  1480. src += 2;
  1481. continue;
  1482. } else if (src[1] == '.') {
  1483. if (src[2] == '/' || src[2] == '\0') {
  1484. /* Conditionally warn about '..' */
  1485. if (a->flags & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
  1486. archive_set_error(&a->archive,
  1487. ARCHIVE_ERRNO_MISC,
  1488. "Path contains '..'");
  1489. return (ARCHIVE_FAILED);
  1490. }
  1491. }
  1492. /*
  1493. * Note: Under no circumstances do we
  1494. * remove '..' elements. In
  1495. * particular, restoring
  1496. * '/foo/../bar/' should create the
  1497. * 'foo' dir as a side-effect.
  1498. */
  1499. }
  1500. }
  1501. /* Copy current element, including leading '/'. */
  1502. if (separator)
  1503. *dest++ = '/';
  1504. while (*src != '\0' && *src != '/') {
  1505. *dest++ = *src++;
  1506. }
  1507. if (*src == '\0')
  1508. break;
  1509. /* Skip '/' separator. */
  1510. separator = *src++;
  1511. }
  1512. /*
  1513. * We've just copied zero or more path elements, not including the
  1514. * final '/'.
  1515. */
  1516. if (dest == a->name) {
  1517. /*
  1518. * Nothing got copied. The path must have been something
  1519. * like '.' or '/' or './' or '/././././/./'.
  1520. */
  1521. if (separator)
  1522. *dest++ = '/';
  1523. else
  1524. *dest++ = '.';
  1525. }
  1526. /* Terminate the result. */
  1527. *dest = '\0';
  1528. return (ARCHIVE_OK);
  1529. }
  1530. /*
  1531. * Create the parent directory of the specified path, assuming path
  1532. * is already in mutable storage.
  1533. */
  1534. static int
  1535. create_parent_dir(struct archive_write_disk *a, char *path)
  1536. {
  1537. char *slash;
  1538. int r;
  1539. /* Remove tail element to obtain parent name. */
  1540. slash = strrchr(path, '/');
  1541. if (slash == NULL)
  1542. return (ARCHIVE_OK);
  1543. *slash = '\0';
  1544. r = create_dir(a, path);
  1545. *slash = '/';
  1546. return (r);
  1547. }
  1548. /*
  1549. * Create the specified dir, recursing to create parents as necessary.
  1550. *
  1551. * Returns ARCHIVE_OK if the path exists when we're done here.
  1552. * Otherwise, returns ARCHIVE_FAILED.
  1553. * Assumes path is in mutable storage; path is unchanged on exit.
  1554. */
  1555. static int
  1556. create_dir(struct archive_write_disk *a, char *path)
  1557. {
  1558. struct stat st;
  1559. struct fixup_entry *le;
  1560. char *slash, *base;
  1561. mode_t mode_final, mode;
  1562. int r;
  1563. r = ARCHIVE_OK;
  1564. /* Check for special names and just skip them. */
  1565. slash = strrchr(path, '/');
  1566. if (slash == NULL)
  1567. base = path;
  1568. else
  1569. base = slash + 1;
  1570. if (base[0] == '\0' ||
  1571. (base[0] == '.' && base[1] == '\0') ||
  1572. (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
  1573. /* Don't bother trying to create null path, '.', or '..'. */
  1574. if (slash != NULL) {
  1575. *slash = '\0';
  1576. r = create_dir(a, path);
  1577. *slash = '/';
  1578. return (r);
  1579. }
  1580. return (ARCHIVE_OK);
  1581. }
  1582. /*
  1583. * Yes, this should be stat() and not lstat(). Using lstat()
  1584. * here loses the ability to extract through symlinks. Also note
  1585. * that this should not use the a->st cache.
  1586. */
  1587. if (stat(path, &st) == 0) {
  1588. if (S_ISDIR(st.st_mode))
  1589. return (ARCHIVE_OK);
  1590. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  1591. archive_set_error(&a->archive, EEXIST,
  1592. "Can't create directory '%s'", path);
  1593. return (ARCHIVE_FAILED);
  1594. }
  1595. if (unlink(path) != 0) {
  1596. archive_set_error(&a->archive, errno,
  1597. "Can't create directory '%s': "
  1598. "Conflicting file cannot be removed");
  1599. return (ARCHIVE_FAILED);
  1600. }
  1601. } else if (errno != ENOENT && errno != ENOTDIR) {
  1602. /* Stat failed? */
  1603. archive_set_error(&a->archive, errno, "Can't test directory '%s'", path);
  1604. return (ARCHIVE_FAILED);
  1605. } else if (slash != NULL) {
  1606. *slash = '\0';
  1607. r = create_dir(a, path);
  1608. *slash = '/';
  1609. if (r != ARCHIVE_OK)
  1610. return (r);
  1611. }
  1612. /*
  1613. * Mode we want for the final restored directory. Per POSIX,
  1614. * implicitly-created dirs must be created obeying the umask.
  1615. * There's no mention whether this is different for privileged
  1616. * restores (which the rest of this code handles by pretending
  1617. * umask=0). I've chosen here to always obey the user's umask for
  1618. * implicit dirs, even if _EXTRACT_PERM was specified.
  1619. */
  1620. mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
  1621. /* Mode we want on disk during the restore process. */
  1622. mode = mode_final;
  1623. mode |= MINIMUM_DIR_MODE;
  1624. mode &= MAXIMUM_DIR_MODE;
  1625. if (mkdir(path, mode) == 0) {
  1626. if (mode != mode_final) {
  1627. le = new_fixup(a, path);
  1628. le->fixup |=TODO_MODE_BASE;
  1629. le->mode = mode_final;
  1630. }
  1631. return (ARCHIVE_OK);
  1632. }
  1633. /*
  1634. * Without the following check, a/b/../b/c/d fails at the
  1635. * second visit to 'b', so 'd' can't be created. Note that we
  1636. * don't add it to the fixup list here, as it's already been
  1637. * added.
  1638. */
  1639. if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
  1640. return (ARCHIVE_OK);
  1641. archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
  1642. path);
  1643. return (ARCHIVE_FAILED);
  1644. }
  1645. /*
  1646. * Note: Although we can skip setting the user id if the desired user
  1647. * id matches the current user, we cannot skip setting the group, as
  1648. * many systems set the gid based on the containing directory. So
  1649. * we have to perform a chown syscall if we want to set the SGID
  1650. * bit. (The alternative is to stat() and then possibly chown(); it's
  1651. * more efficient to skip the stat() and just always chown().) Note
  1652. * that a successful chown() here clears the TODO_SGID_CHECK bit, which
  1653. * allows set_mode to skip the stat() check for the GID.
  1654. */
  1655. static int
  1656. set_ownership(struct archive_write_disk *a)
  1657. {
  1658. #ifndef __CYGWIN__
  1659. /* unfortunately, on win32 there is no 'root' user with uid 0,
  1660. so we just have to try the chown and see if it works */
  1661. /* If we know we can't change it, don't bother trying. */
  1662. if (a->user_uid != 0 && a->user_uid != a->uid) {
  1663. archive_set_error(&a->archive, errno,
  1664. "Can't set UID=%d", a->uid);
  1665. return (ARCHIVE_WARN);
  1666. }
  1667. #endif
  1668. #ifdef HAVE_FCHOWN
  1669. /* If we have an fd, we can avoid a race. */
  1670. if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
  1671. /* We've set owner and know uid/gid are correct. */
  1672. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  1673. return (ARCHIVE_OK);
  1674. }
  1675. #endif
  1676. /* We prefer lchown() but will use chown() if that's all we have. */
  1677. /* Of course, if we have neither, this will always fail. */
  1678. #ifdef HAVE_LCHOWN
  1679. if (lchown(a->name, a->uid, a->gid) == 0) {
  1680. /* We've set owner and know uid/gid are correct. */
  1681. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  1682. return (ARCHIVE_OK);
  1683. }
  1684. #elif HAVE_CHOWN
  1685. if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
  1686. /* We've set owner and know uid/gid are correct. */
  1687. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  1688. return (ARCHIVE_OK);
  1689. }
  1690. #endif
  1691. archive_set_error(&a->archive, errno,
  1692. "Can't set user=%d/group=%d for %s", a->uid, a->gid,
  1693. a->name);
  1694. return (ARCHIVE_WARN);
  1695. }
  1696. #ifdef HAVE_UTIMES
  1697. /*
  1698. * The utimes()-family functions provide high resolution and
  1699. * a way to set time on an fd or a symlink. We prefer them
  1700. * when they're available.
  1701. */
  1702. static int
  1703. set_time(int fd, int mode, const char *name,
  1704. time_t atime, long atime_nsec,
  1705. time_t mtime, long mtime_nsec)
  1706. {
  1707. #if defined(_WIN32) && !defined(__CYGWIN__)
  1708. struct __timeval times[2];
  1709. #else
  1710. struct timeval times[2];
  1711. #endif
  1712. times[0].tv_sec = atime;
  1713. times[0].tv_usec = atime_nsec / 1000;
  1714. times[1].tv_sec = mtime;
  1715. times[1].tv_usec = mtime_nsec / 1000;
  1716. #ifdef HAVE_FUTIMES
  1717. if (fd >= 0)
  1718. return (futimes(fd, times));
  1719. #else
  1720. (void)fd; /* UNUSED */
  1721. #endif
  1722. #ifdef HAVE_LUTIMES
  1723. (void)mode; /* UNUSED */
  1724. return (lutimes(name, times));
  1725. #else
  1726. if (S_ISLNK(mode))
  1727. return (0);
  1728. return (utimes(name, times));
  1729. #endif
  1730. }
  1731. #elif defined(HAVE_UTIME)
  1732. /*
  1733. * utime() is an older, more standard interface that we'll use
  1734. * if utimes() isn't available.
  1735. */
  1736. static int
  1737. set_time(int fd, int mode, const char *name,
  1738. time_t atime, long atime_nsec,
  1739. time_t mtime, long mtime_nsec)
  1740. {
  1741. struct utimbuf times;
  1742. (void)fd; /* UNUSED */
  1743. (void)name; /* UNUSED */
  1744. (void)atime_nsec; /* UNUSED */
  1745. (void)mtime_nsec; /* UNUSED */
  1746. times.actime = atime;
  1747. times.modtime = mtime;
  1748. if (S_ISLNK(mode))
  1749. return (ARCHIVE_OK);
  1750. return (utime(name, &times));
  1751. }
  1752. #else
  1753. static int
  1754. set_time(int fd, int mode, const char *name,
  1755. time_t atime, long atime_nsec,
  1756. time_t mtime, long mtime_nsec)
  1757. {
  1758. return (ARCHIVE_WARN);
  1759. }
  1760. #endif
  1761. static int
  1762. set_times(struct archive_write_disk *a)
  1763. {
  1764. time_t atime = a->start_time, mtime = a->start_time;
  1765. long atime_nsec = 0, mtime_nsec = 0;
  1766. /* If no time was provided, we're done. */
  1767. if (!archive_entry_atime_is_set(a->entry)
  1768. #if HAVE_STRUCT_STAT_ST_BIRTHTIME
  1769. && !archive_entry_birthtime_is_set(a->entry)
  1770. #endif
  1771. && !archive_entry_mtime_is_set(a->entry))
  1772. return (ARCHIVE_OK);
  1773. /* If no atime was specified, use start time instead. */
  1774. /* In theory, it would be marginally more correct to use
  1775. * time(NULL) here, but that would cost us an extra syscall
  1776. * for little gain. */
  1777. if (archive_entry_atime_is_set(a->entry)) {
  1778. atime = archive_entry_atime(a->entry);
  1779. atime_nsec = archive_entry_atime_nsec(a->entry);
  1780. }
  1781. /*
  1782. * If you have struct stat.st_birthtime, we assume BSD birthtime
  1783. * semantics, in which {f,l,}utimes() updates birthtime to earliest
  1784. * mtime. So we set the time twice, first using the birthtime,
  1785. * then using the mtime.
  1786. */
  1787. #if HAVE_STRUCT_STAT_ST_BIRTHTIME
  1788. /* If birthtime is set, flush that through to disk first. */
  1789. if (archive_entry_birthtime_is_set(a->entry))
  1790. if (set_time(a->fd, a->mode, a->name, atime, atime_nsec,
  1791. archive_entry_birthtime(a->entry),
  1792. archive_entry_birthtime_nsec(a->entry))) {
  1793. archive_set_error(&a->archive, errno,
  1794. "Can't update time for %s",
  1795. a->name);
  1796. return (ARCHIVE_WARN);
  1797. }
  1798. #endif
  1799. if (archive_entry_mtime_is_set(a->entry)) {
  1800. mtime = archive_entry_mtime(a->entry);
  1801. mtime_nsec = archive_entry_mtime_nsec(a->entry);
  1802. }
  1803. if (set_time(a->fd, a->mode, a->name,
  1804. atime, atime_nsec, mtime, mtime_nsec)) {
  1805. archive_set_error(&a->archive, errno,
  1806. "Can't update time for %s",
  1807. a->name);
  1808. return (ARCHIVE_WARN);
  1809. }
  1810. /*
  1811. * Note: POSIX does not provide a portable way to restore ctime.
  1812. * (Apart from resetting the system clock, which is distasteful.)
  1813. * So, any restoration of ctime will necessarily be OS-specific.
  1814. */
  1815. return (ARCHIVE_OK);
  1816. }
  1817. static int
  1818. set_mode(struct archive_write_disk *a, int mode)
  1819. {
  1820. int r = ARCHIVE_OK;
  1821. mode &= 07777; /* Strip off file type bits. */
  1822. if (a->todo & TODO_SGID_CHECK) {
  1823. /*
  1824. * If we don't know the GID is right, we must stat()
  1825. * to verify it. We can't just check the GID of this
  1826. * process, since systems sometimes set GID from
  1827. * the enclosing dir or based on ACLs.
  1828. */
  1829. if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
  1830. return (r);
  1831. if (a->pst->st_gid != a->gid) {
  1832. mode &= ~ S_ISGID;
  1833. #if !defined(_WIN32) || defined(__CYGWIN__)
  1834. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  1835. /*
  1836. * This is only an error if you
  1837. * requested owner restore. If you
  1838. * didn't, we'll try to restore
  1839. * sgid/suid, but won't consider it a
  1840. * problem if we can't.
  1841. */
  1842. archive_set_error(&a->archive, -1,
  1843. "Can't restore SGID bit");
  1844. r = ARCHIVE_WARN;
  1845. }
  1846. #endif
  1847. }
  1848. /* While we're here, double-check the UID. */
  1849. if (a->pst->st_uid != a->uid
  1850. && (a->todo & TODO_SUID)) {
  1851. mode &= ~ S_ISUID;
  1852. #if !defined(_WIN32) || defined(__CYGWIN__)
  1853. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  1854. archive_set_error(&a->archive, -1,
  1855. "Can't restore SUID bit");
  1856. r = ARCHIVE_WARN;
  1857. }
  1858. #endif
  1859. }
  1860. a->todo &= ~TODO_SGID_CHECK;
  1861. a->todo &= ~TODO_SUID_CHECK;
  1862. } else if (a->todo & TODO_SUID_CHECK) {
  1863. /*
  1864. * If we don't know the UID is right, we can just check
  1865. * the user, since all systems set the file UID from
  1866. * the process UID.
  1867. */
  1868. if (a->user_uid != a->uid) {
  1869. mode &= ~ S_ISUID;
  1870. #if !defined(_WIN32) || defined(__CYGWIN__)
  1871. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  1872. archive_set_error(&a->archive, -1,
  1873. "Can't make file SUID");
  1874. r = ARCHIVE_WARN;
  1875. }
  1876. #endif
  1877. }
  1878. a->todo &= ~TODO_SUID_CHECK;
  1879. }
  1880. if (S_ISLNK(a->mode)) {
  1881. #ifdef HAVE_LCHMOD
  1882. /*
  1883. * If this is a symlink, use lchmod(). If the
  1884. * platform doesn't support lchmod(), just skip it. A
  1885. * platform that doesn't provide a way to set
  1886. * permissions on symlinks probably ignores
  1887. * permissions on symlinks, so a failure here has no
  1888. * impact.
  1889. */
  1890. if (lchmod(a->name, mode) != 0) {
  1891. archive_set_error(&a->archive, errno,
  1892. "Can't set permissions to 0%o", (int)mode);
  1893. r = ARCHIVE_WARN;
  1894. }
  1895. #endif
  1896. } else if (!S_ISDIR(a->mode)) {
  1897. /*
  1898. * If it's not a symlink and not a dir, then use
  1899. * fchmod() or chmod(), depending on whether we have
  1900. * an fd. Dirs get their perms set during the
  1901. * post-extract fixup, which is handled elsewhere.
  1902. */
  1903. #ifdef HAVE_FCHMOD
  1904. if (a->fd >= 0) {
  1905. if (fchmod(a->fd, mode) != 0) {
  1906. archive_set_error(&a->archive, errno,
  1907. "Can't set permissions to 0%o", (int)mode);
  1908. r = ARCHIVE_WARN;
  1909. }
  1910. } else
  1911. #endif
  1912. /* If this platform lacks fchmod(), then
  1913. * we'll just use chmod(). */
  1914. if (chmod(a->name, mode) != 0) {
  1915. archive_set_error(&a->archive, errno,
  1916. "Can't set permissions to 0%o", (int)mode);
  1917. r = ARCHIVE_WARN;
  1918. }
  1919. }
  1920. return (r);
  1921. }
  1922. static int
  1923. set_fflags(struct archive_write_disk *a)
  1924. {
  1925. struct fixup_entry *le;
  1926. unsigned long set, clear;
  1927. int r;
  1928. int critical_flags;
  1929. mode_t mode = archive_entry_mode(a->entry);
  1930. /*
  1931. * Make 'critical_flags' hold all file flags that can't be
  1932. * immediately restored. For example, on BSD systems,
  1933. * SF_IMMUTABLE prevents hardlinks from being created, so
  1934. * should not be set until after any hardlinks are created. To
  1935. * preserve some semblance of portability, this uses #ifdef
  1936. * extensively. Ugly, but it works.
  1937. *
  1938. * Yes, Virginia, this does create a security race. It's mitigated
  1939. * somewhat by the practice of creating dirs 0700 until the extract
  1940. * is done, but it would be nice if we could do more than that.
  1941. * People restoring critical file systems should be wary of
  1942. * other programs that might try to muck with files as they're
  1943. * being restored.
  1944. */
  1945. /* Hopefully, the compiler will optimize this mess into a constant. */
  1946. critical_flags = 0;
  1947. #ifdef SF_IMMUTABLE
  1948. critical_flags |= SF_IMMUTABLE;
  1949. #endif
  1950. #ifdef UF_IMMUTABLE
  1951. critical_flags |= UF_IMMUTABLE;
  1952. #endif
  1953. #ifdef SF_APPEND
  1954. critical_flags |= SF_APPEND;
  1955. #endif
  1956. #ifdef UF_APPEND
  1957. critical_flags |= UF_APPEND;
  1958. #endif
  1959. #ifdef EXT2_APPEND_FL
  1960. critical_flags |= EXT2_APPEND_FL;
  1961. #endif
  1962. #ifdef EXT2_IMMUTABLE_FL
  1963. critical_flags |= EXT2_IMMUTABLE_FL;
  1964. #endif
  1965. if (a->todo & TODO_FFLAGS) {
  1966. archive_entry_fflags(a->entry, &set, &clear);
  1967. /*
  1968. * The first test encourages the compiler to eliminate
  1969. * all of this if it's not necessary.
  1970. */
  1971. if ((critical_flags != 0) && (set & critical_flags)) {
  1972. le = current_fixup(a, a->name);
  1973. le->fixup |= TODO_FFLAGS;
  1974. le->fflags_set = set;
  1975. /* Store the mode if it's not already there. */
  1976. if ((le->fixup & TODO_MODE) == 0)
  1977. le->mode = mode;
  1978. } else {
  1979. r = set_fflags_platform(a, a->fd,
  1980. a->name, mode, set, clear);
  1981. if (r != ARCHIVE_OK)
  1982. return (r);
  1983. }
  1984. }
  1985. return (ARCHIVE_OK);
  1986. }
  1987. #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
  1988. /*
  1989. * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
  1990. */
  1991. static int
  1992. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  1993. mode_t mode, unsigned long set, unsigned long clear)
  1994. {
  1995. int r;
  1996. (void)mode; /* UNUSED */
  1997. if (set == 0 && clear == 0)
  1998. return (ARCHIVE_OK);
  1999. /*
  2000. * XXX Is the stat here really necessary? Or can I just use
  2001. * the 'set' flags directly? In particular, I'm not sure
  2002. * about the correct approach if we're overwriting an existing
  2003. * file that already has flags on it. XXX
  2004. */
  2005. if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
  2006. return (r);
  2007. a->st.st_flags &= ~clear;
  2008. a->st.st_flags |= set;
  2009. #ifdef HAVE_FCHFLAGS
  2010. /* If platform has fchflags() and we were given an fd, use it. */
  2011. if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
  2012. return (ARCHIVE_OK);
  2013. #endif
  2014. /*
  2015. * If we can't use the fd to set the flags, we'll use the
  2016. * pathname to set flags. We prefer lchflags() but will use
  2017. * chflags() if we must.
  2018. */
  2019. #ifdef HAVE_LCHFLAGS
  2020. if (lchflags(name, a->st.st_flags) == 0)
  2021. return (ARCHIVE_OK);
  2022. #elif defined(HAVE_CHFLAGS)
  2023. if (S_ISLNK(a->st.st_mode)) {
  2024. archive_set_error(&a->archive, errno,
  2025. "Can't set file flags on symlink.");
  2026. return (ARCHIVE_WARN);
  2027. }
  2028. if (chflags(name, a->st.st_flags) == 0)
  2029. return (ARCHIVE_OK);
  2030. #endif
  2031. archive_set_error(&a->archive, errno,
  2032. "Failed to set file flags");
  2033. return (ARCHIVE_WARN);
  2034. }
  2035. #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS)
  2036. /*
  2037. * Linux uses ioctl() to read and write file flags.
  2038. */
  2039. static int
  2040. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  2041. mode_t mode, unsigned long set, unsigned long clear)
  2042. {
  2043. int ret;
  2044. int myfd = fd;
  2045. unsigned long newflags, oldflags;
  2046. unsigned long sf_mask = 0;
  2047. if (set == 0 && clear == 0)
  2048. return (ARCHIVE_OK);
  2049. /* Only regular files and dirs can have flags. */
  2050. if (!S_ISREG(mode) && !S_ISDIR(mode))
  2051. return (ARCHIVE_OK);
  2052. /* If we weren't given an fd, open it ourselves. */
  2053. if (myfd < 0)
  2054. myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY);
  2055. if (myfd < 0)
  2056. return (ARCHIVE_OK);
  2057. /*
  2058. * Linux has no define for the flags that are only settable by
  2059. * the root user. This code may seem a little complex, but
  2060. * there seem to be some Linux systems that lack these
  2061. * defines. (?) The code below degrades reasonably gracefully
  2062. * if sf_mask is incomplete.
  2063. */
  2064. #ifdef EXT2_IMMUTABLE_FL
  2065. sf_mask |= EXT2_IMMUTABLE_FL;
  2066. #endif
  2067. #ifdef EXT2_APPEND_FL
  2068. sf_mask |= EXT2_APPEND_FL;
  2069. #endif
  2070. /*
  2071. * XXX As above, this would be way simpler if we didn't have
  2072. * to read the current flags from disk. XXX
  2073. */
  2074. ret = ARCHIVE_OK;
  2075. /* Try setting the flags as given. */
  2076. if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
  2077. newflags = (oldflags & ~clear) | set;
  2078. if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
  2079. goto cleanup;
  2080. if (errno != EPERM)
  2081. goto fail;
  2082. }
  2083. /* If we couldn't set all the flags, try again with a subset. */
  2084. if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
  2085. newflags &= ~sf_mask;
  2086. oldflags &= sf_mask;
  2087. newflags |= oldflags;
  2088. if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
  2089. goto cleanup;
  2090. }
  2091. /* We couldn't set the flags, so report the failure. */
  2092. fail:
  2093. archive_set_error(&a->archive, errno,
  2094. "Failed to set file flags");
  2095. ret = ARCHIVE_WARN;
  2096. cleanup:
  2097. if (fd < 0)
  2098. close(myfd);
  2099. return (ret);
  2100. }
  2101. #else
  2102. /*
  2103. * Of course, some systems have neither BSD chflags() nor Linux' flags
  2104. * support through ioctl().
  2105. */
  2106. static int
  2107. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  2108. mode_t mode, unsigned long set, unsigned long clear)
  2109. {
  2110. (void)a; /* UNUSED */
  2111. (void)fd; /* UNUSED */
  2112. (void)name; /* UNUSED */
  2113. (void)mode; /* UNUSED */
  2114. (void)set; /* UNUSED */
  2115. (void)clear; /* UNUSED */
  2116. return (ARCHIVE_OK);
  2117. }
  2118. #endif /* __linux */
  2119. #ifndef HAVE_POSIX_ACL
  2120. /* Default empty function body to satisfy mainline code. */
  2121. static int
  2122. set_acls(struct archive_write_disk *a)
  2123. {
  2124. (void)a; /* UNUSED */
  2125. return (ARCHIVE_OK);
  2126. }
  2127. #else
  2128. /*
  2129. * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
  2130. */
  2131. static int
  2132. set_acls(struct archive_write_disk *a)
  2133. {
  2134. int ret;
  2135. ret = set_acl(a, a->fd, a->entry, ACL_TYPE_ACCESS,
  2136. ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
  2137. if (ret != ARCHIVE_OK)
  2138. return (ret);
  2139. ret = set_acl(a, a->fd, a->entry, ACL_TYPE_DEFAULT,
  2140. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
  2141. return (ret);
  2142. }
  2143. static int
  2144. set_acl(struct archive_write_disk *a, int fd, struct archive_entry *entry,
  2145. acl_type_t acl_type, int ae_requested_type, const char *tname)
  2146. {
  2147. acl_t acl;
  2148. acl_entry_t acl_entry;
  2149. acl_permset_t acl_permset;
  2150. int ret;
  2151. int ae_type, ae_permset, ae_tag, ae_id;
  2152. uid_t ae_uid;
  2153. gid_t ae_gid;
  2154. const char *ae_name;
  2155. int entries;
  2156. const char *name;
  2157. ret = ARCHIVE_OK;
  2158. entries = archive_entry_acl_reset(entry, ae_requested_type);
  2159. if (entries == 0)
  2160. return (ARCHIVE_OK);
  2161. acl = acl_init(entries);
  2162. while (archive_entry_acl_next(entry, ae_requested_type, &ae_type,
  2163. &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
  2164. acl_create_entry(&acl, &acl_entry);
  2165. switch (ae_tag) {
  2166. case ARCHIVE_ENTRY_ACL_USER:
  2167. acl_set_tag_type(acl_entry, ACL_USER);
  2168. ae_uid = a->lookup_uid(a->lookup_uid_data,
  2169. ae_name, ae_id);
  2170. acl_set_qualifier(acl_entry, &ae_uid);
  2171. break;
  2172. case ARCHIVE_ENTRY_ACL_GROUP:
  2173. acl_set_tag_type(acl_entry, ACL_GROUP);
  2174. ae_gid = a->lookup_gid(a->lookup_gid_data,
  2175. ae_name, ae_id);
  2176. acl_set_qualifier(acl_entry, &ae_gid);
  2177. break;
  2178. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  2179. acl_set_tag_type(acl_entry, ACL_USER_OBJ);
  2180. break;
  2181. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  2182. acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
  2183. break;
  2184. case ARCHIVE_ENTRY_ACL_MASK:
  2185. acl_set_tag_type(acl_entry, ACL_MASK);
  2186. break;
  2187. case ARCHIVE_ENTRY_ACL_OTHER:
  2188. acl_set_tag_type(acl_entry, ACL_OTHER);
  2189. break;
  2190. default:
  2191. /* XXX */
  2192. break;
  2193. }
  2194. acl_get_permset(acl_entry, &acl_permset);
  2195. acl_clear_perms(acl_permset);
  2196. if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE)
  2197. acl_add_perm(acl_permset, ACL_EXECUTE);
  2198. if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE)
  2199. acl_add_perm(acl_permset, ACL_WRITE);
  2200. if (ae_permset & ARCHIVE_ENTRY_ACL_READ)
  2201. acl_add_perm(acl_permset, ACL_READ);
  2202. }
  2203. name = archive_entry_pathname(entry);
  2204. /* Try restoring the ACL through 'fd' if we can. */
  2205. #if HAVE_ACL_SET_FD
  2206. if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
  2207. ret = ARCHIVE_OK;
  2208. else
  2209. #else
  2210. #if HAVE_ACL_SET_FD_NP
  2211. if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
  2212. ret = ARCHIVE_OK;
  2213. else
  2214. #endif
  2215. #endif
  2216. if (acl_set_file(name, acl_type, acl) != 0) {
  2217. archive_set_error(&a->archive, errno, "Failed to set %s acl", tname);
  2218. ret = ARCHIVE_WARN;
  2219. }
  2220. acl_free(acl);
  2221. return (ret);
  2222. }
  2223. #endif
  2224. #if HAVE_LSETXATTR
  2225. /*
  2226. * Restore extended attributes - Linux implementation
  2227. */
  2228. static int
  2229. set_xattrs(struct archive_write_disk *a)
  2230. {
  2231. struct archive_entry *entry = a->entry;
  2232. static int warning_done = 0;
  2233. int ret = ARCHIVE_OK;
  2234. int i = archive_entry_xattr_reset(entry);
  2235. while (i--) {
  2236. const char *name;
  2237. const void *value;
  2238. size_t size;
  2239. archive_entry_xattr_next(entry, &name, &value, &size);
  2240. if (name != NULL &&
  2241. strncmp(name, "xfsroot.", 8) != 0 &&
  2242. strncmp(name, "system.", 7) != 0) {
  2243. int e;
  2244. #if HAVE_FSETXATTR
  2245. if (a->fd >= 0)
  2246. e = fsetxattr(a->fd, name, value, size, 0);
  2247. else
  2248. #endif
  2249. {
  2250. e = lsetxattr(archive_entry_pathname(entry),
  2251. name, value, size, 0);
  2252. }
  2253. if (e == -1) {
  2254. if (errno == ENOTSUP) {
  2255. if (!warning_done) {
  2256. warning_done = 1;
  2257. archive_set_error(&a->archive, errno,
  2258. "Cannot restore extended "
  2259. "attributes on this file "
  2260. "system");
  2261. }
  2262. } else
  2263. archive_set_error(&a->archive, errno,
  2264. "Failed to set extended attribute");
  2265. ret = ARCHIVE_WARN;
  2266. }
  2267. } else {
  2268. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2269. "Invalid extended attribute encountered");
  2270. ret = ARCHIVE_WARN;
  2271. }
  2272. }
  2273. return (ret);
  2274. }
  2275. #elif HAVE_EXTATTR_SET_FILE
  2276. /*
  2277. * Restore extended attributes - FreeBSD implementation
  2278. */
  2279. static int
  2280. set_xattrs(struct archive_write_disk *a)
  2281. {
  2282. struct archive_entry *entry = a->entry;
  2283. static int warning_done = 0;
  2284. int ret = ARCHIVE_OK;
  2285. int i = archive_entry_xattr_reset(entry);
  2286. while (i--) {
  2287. const char *name;
  2288. const void *value;
  2289. size_t size;
  2290. archive_entry_xattr_next(entry, &name, &value, &size);
  2291. if (name != NULL) {
  2292. int e;
  2293. int namespace;
  2294. if (strncmp(name, "user.", 5) == 0) {
  2295. /* "user." attributes go to user namespace */
  2296. name += 5;
  2297. namespace = EXTATTR_NAMESPACE_USER;
  2298. } else {
  2299. /* Warn about other extended attributes. */
  2300. archive_set_error(&a->archive,
  2301. ARCHIVE_ERRNO_FILE_FORMAT,
  2302. "Can't restore extended attribute ``%s''",
  2303. name);
  2304. ret = ARCHIVE_WARN;
  2305. continue;
  2306. }
  2307. errno = 0;
  2308. #if HAVE_EXTATTR_SET_FD
  2309. if (a->fd >= 0)
  2310. e = extattr_set_fd(a->fd, namespace, name, value, size);
  2311. else
  2312. #endif
  2313. /* TODO: should we use extattr_set_link() instead? */
  2314. {
  2315. e = extattr_set_file(archive_entry_pathname(entry),
  2316. namespace, name, value, size);
  2317. }
  2318. if (e != (int)size) {
  2319. if (errno == ENOTSUP) {
  2320. if (!warning_done) {
  2321. warning_done = 1;
  2322. archive_set_error(&a->archive, errno,
  2323. "Cannot restore extended "
  2324. "attributes on this file "
  2325. "system");
  2326. }
  2327. } else {
  2328. archive_set_error(&a->archive, errno,
  2329. "Failed to set extended attribute");
  2330. }
  2331. ret = ARCHIVE_WARN;
  2332. }
  2333. }
  2334. }
  2335. return (ret);
  2336. }
  2337. #else
  2338. /*
  2339. * Restore extended attributes - stub implementation for unsupported systems
  2340. */
  2341. static int
  2342. set_xattrs(struct archive_write_disk *a)
  2343. {
  2344. static int warning_done = 0;
  2345. /* If there aren't any extended attributes, then it's okay not
  2346. * to extract them, otherwise, issue a single warning. */
  2347. if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
  2348. warning_done = 1;
  2349. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2350. "Cannot restore extended attributes on this system");
  2351. return (ARCHIVE_WARN);
  2352. }
  2353. /* Warning was already emitted; suppress further warnings. */
  2354. return (ARCHIVE_OK);
  2355. }
  2356. #endif
  2357. /*
  2358. * Trivial implementations of gid/uid lookup functions.
  2359. * These are normally overridden by the client, but these stub
  2360. * versions ensure that we always have something that works.
  2361. */
  2362. static gid_t
  2363. trivial_lookup_gid(void *private_data, const char *gname, gid_t gid)
  2364. {
  2365. (void)private_data; /* UNUSED */
  2366. (void)gname; /* UNUSED */
  2367. return (gid);
  2368. }
  2369. static uid_t
  2370. trivial_lookup_uid(void *private_data, const char *uname, uid_t uid)
  2371. {
  2372. (void)private_data; /* UNUSED */
  2373. (void)uname; /* UNUSED */
  2374. return (uid);
  2375. }
  2376. /*
  2377. * Test if file on disk is older than entry.
  2378. */
  2379. static int
  2380. older(struct stat *st, struct archive_entry *entry)
  2381. {
  2382. /* First, test the seconds and return if we have a definite answer. */
  2383. /* Definitely older. */
  2384. if (st->st_mtime < archive_entry_mtime(entry))
  2385. return (1);
  2386. /* Definitely younger. */
  2387. if (st->st_mtime > archive_entry_mtime(entry))
  2388. return (0);
  2389. /* If this platform supports fractional seconds, try those. */
  2390. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  2391. /* Definitely older. */
  2392. if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
  2393. return (1);
  2394. #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  2395. /* Definitely older. */
  2396. if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
  2397. return (1);
  2398. #elif HAVE_STRUCT_STAT_ST_MTIME_N
  2399. /* older. */
  2400. if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
  2401. return (1);
  2402. #elif HAVE_STRUCT_STAT_ST_UMTIME
  2403. /* older. */
  2404. if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
  2405. return (1);
  2406. #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
  2407. /* older. */
  2408. if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
  2409. return (1);
  2410. #else
  2411. /* This system doesn't have high-res timestamps. */
  2412. #endif
  2413. /* Same age or newer, so not older. */
  2414. return (0);
  2415. }