archive_write_disk.c 71 KB

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