bsdtar.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  1. /*-
  2. * Copyright 2006-2025 Tarsnap Backup Inc.
  3. * All rights reserved.
  4. *
  5. * Portions of the file below are covered by the following license:
  6. *
  7. * Copyright (c) 2003-2008 Tim Kientzle
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "bsdtar_platform.h"
  31. __FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.93 2008/11/08 04:43:24 kientzle Exp $");
  32. #ifdef HAVE_SYS_PARAM_H
  33. #include <sys/param.h>
  34. #endif
  35. #ifdef HAVE_SYS_STAT_H
  36. #include <sys/stat.h>
  37. #endif
  38. #ifdef HAVE_ERRNO_H
  39. #include <errno.h>
  40. #endif
  41. #ifdef HAVE_FCNTL_H
  42. #include <fcntl.h>
  43. #endif
  44. #ifdef HAVE_LANGINFO_H
  45. #include <langinfo.h>
  46. #endif
  47. #ifdef HAVE_LIMITS_H
  48. #include <limits.h>
  49. #endif
  50. #ifdef HAVE_LOCALE_H
  51. #include <locale.h>
  52. #endif
  53. #ifdef HAVE_PATHS_H
  54. #include <paths.h>
  55. #endif
  56. #ifdef HAVE_PWD_H
  57. #include <pwd.h>
  58. #endif
  59. #include <stdio.h>
  60. #ifdef HAVE_STDLIB_H
  61. #include <stdlib.h>
  62. #endif
  63. #ifdef HAVE_STRING_H
  64. #include <string.h>
  65. #endif
  66. #ifdef HAVE_TIME_H
  67. #include <time.h>
  68. #endif
  69. #ifdef HAVE_UNISTD_H
  70. #include <unistd.h>
  71. #endif
  72. #if HAVE_ZLIB_H
  73. #include <zlib.h>
  74. #endif
  75. #include <assert.h>
  76. #include "bsdtar.h"
  77. #include "chunks.h"
  78. #include "crypto.h"
  79. #include "dirutil.h"
  80. #include "humansize.h"
  81. #include "keyfile.h"
  82. #include "passphrase_entry.h"
  83. #include "tarsnap_opt.h"
  84. #include "tsnetwork.h"
  85. #include "warnp.h"
  86. /* Global tarsnap options declared in tarsnap_opt.h. */
  87. int tarsnap_opt_aggressive_networking = 0;
  88. int tarsnap_opt_humanize_numbers = 0;
  89. int tarsnap_opt_noisy_warnings = 0;
  90. uint64_t tarsnap_opt_checkpointbytes = (uint64_t)(-1);
  91. uint64_t tarsnap_opt_maxbytesout = (uint64_t)(-1);
  92. /* Structure for holding a delayed option. */
  93. struct delayedopt {
  94. char * opt_name;
  95. char * opt_arg;
  96. struct delayedopt * next;
  97. };
  98. /* External function to parse a date/time string (from getdate.y) */
  99. time_t get_date(time_t, const char *);
  100. static struct bsdtar *bsdtar_init(void);
  101. static void bsdtar_atexit(void);
  102. static void configfile(struct bsdtar *, const char *fname,
  103. int fromcmdline);
  104. static int configfile_helper(struct bsdtar *bsdtar,
  105. const char *line);
  106. static int archive_names_helper(struct bsdtar *bsdtar,
  107. const char *line);
  108. static void dooption(struct bsdtar *, const char *,
  109. const char *, int);
  110. static int load_keys(struct bsdtar *, const char *path);
  111. static void long_help(struct bsdtar *);
  112. static void only_mode(struct bsdtar *, const char *opt,
  113. const char *valid);
  114. static void optq_push(struct bsdtar *, const char *,
  115. const char *);
  116. static void optq_pop(struct bsdtar *);
  117. static void set_mode(struct bsdtar *, int opt, const char *optstr);
  118. static void version(void);
  119. static int argv_has_archive_directive(struct bsdtar *bsdtar);
  120. /* A basic set of security flags to request from libarchive. */
  121. #define SECURITY \
  122. (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
  123. | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
  124. static struct bsdtar bsdtar_storage;
  125. static struct bsdtar *
  126. bsdtar_init(void)
  127. {
  128. struct bsdtar * bsdtar = &bsdtar_storage;
  129. memset(bsdtar, 0, sizeof(*bsdtar));
  130. /*
  131. * Initialize pointers. memset() is insufficient since NULL is not
  132. * required to be represented in memory by zeroes.
  133. */
  134. bsdtar->tapenames = NULL;
  135. bsdtar->homedir = NULL;
  136. bsdtar->cachedir = NULL;
  137. bsdtar->pending_chdir = NULL;
  138. bsdtar->names_from_file = NULL;
  139. bsdtar->modestr = NULL;
  140. bsdtar->option_csv_filename = NULL;
  141. bsdtar->configfiles = NULL;
  142. bsdtar->archive = NULL;
  143. bsdtar->progname = NULL;
  144. bsdtar->argv = NULL;
  145. bsdtar->optarg = NULL;
  146. bsdtar->write_cookie = NULL;
  147. bsdtar->chunk_cache = NULL;
  148. bsdtar->argv_orig = NULL;
  149. bsdtar->delopt = NULL;
  150. bsdtar->delopt_tail = NULL;
  151. bsdtar->diskreader = NULL;
  152. bsdtar->resolver = NULL;
  153. bsdtar->gname_cache = NULL;
  154. bsdtar->buff = NULL;
  155. bsdtar->matching = NULL;
  156. bsdtar->security = NULL;
  157. bsdtar->uname_cache = NULL;
  158. bsdtar->siginfo = NULL;
  159. bsdtar->substitution = NULL;
  160. bsdtar->keyfile = NULL;
  161. bsdtar->conffile = NULL;
  162. bsdtar->conf_opt = NULL;
  163. bsdtar->conf_arg = NULL;
  164. bsdtar->conffile_actual = NULL;
  165. bsdtar->conffile_buffer = NULL;
  166. bsdtar->option_passphrase_arg = NULL;
  167. /* Initialize temporary tapenames array. */
  168. bsdtar->tapenames_setup = strlist_init(0);
  169. /* We don't have bsdtar->progname yet, so we can't use bsdtar_errc. */
  170. if (atexit(bsdtar_atexit)) {
  171. fprintf(stderr, "tarsnap: Could not register atexit.\n");
  172. exit(1);
  173. }
  174. return (bsdtar);
  175. }
  176. static void
  177. bsdtar_atexit(void)
  178. {
  179. struct bsdtar *bsdtar;
  180. size_t i;
  181. bsdtar = &bsdtar_storage;
  182. /* Free temporary archive names (if an error occurred before export). */
  183. if (bsdtar->tapenames_setup != NULL) {
  184. for (i = 0; i < strlist_getsize(bsdtar->tapenames_setup); i++)
  185. free(*strlist_get(bsdtar->tapenames_setup, i));
  186. strlist_free(bsdtar->tapenames_setup);
  187. }
  188. /* Free arrays containing strings allocated by strdup. */
  189. if (bsdtar->tapenames != NULL) {
  190. for (i = 0; i < bsdtar->ntapes; i++)
  191. free(bsdtar->tapenames[i]);
  192. }
  193. /* Free arrays allocated by malloc. */
  194. free(bsdtar->tapenames);
  195. free(bsdtar->configfiles);
  196. /* Free strings allocated by strdup. */
  197. free(bsdtar->cachedir);
  198. free(bsdtar->homedir);
  199. free(bsdtar->option_csv_filename);
  200. free(bsdtar->keyfile);
  201. free(bsdtar->conffile);
  202. free(bsdtar->conf_opt);
  203. free(bsdtar->conf_arg);
  204. free(bsdtar->option_passphrase_arg);
  205. /* Free file-parsing variables from util.c. */
  206. free(bsdtar->conffile_buffer);
  207. if ((bsdtar->conffile_actual != NULL) &&
  208. (bsdtar->conffile_actual != stdin) &&
  209. fclose(bsdtar->conffile_actual))
  210. bsdtar_warnc(bsdtar, errno, "fclose");
  211. /* Free matching and (if applicable) substitution patterns. */
  212. cleanup_exclusions(bsdtar);
  213. #if HAVE_REGEX_H
  214. cleanup_substitution(bsdtar);
  215. #endif
  216. /* Clean up network layer. */
  217. network_fini();
  218. }
  219. int
  220. main(int argc, char **argv)
  221. {
  222. struct bsdtar *bsdtar;
  223. int opt;
  224. char possible_help_request;
  225. char buff[16];
  226. char cachedir[PATH_MAX + 1];
  227. struct passwd *pws;
  228. const char *missingkey;
  229. time_t now;
  230. size_t i;
  231. int j;
  232. char *tapename_cmdline;
  233. char *xdg_configdir;
  234. WARNP_INIT;
  235. /* Use a pointer for consistency. */
  236. bsdtar = bsdtar_init();
  237. #if defined(_WIN32) && !defined(__CYGWIN__)
  238. /* Make sure open() function will be used with a binary mode. */
  239. /* on cygwin, we need something similar, but instead link against */
  240. /* a special startup object, binmode.o */
  241. _set_fmode(_O_BINARY);
  242. #endif
  243. /* Need bsdtar->progname before calling bsdtar_warnc. */
  244. if (*argv == NULL)
  245. bsdtar->progname = "tarsnap";
  246. else {
  247. #if defined(_WIN32) && !defined(__CYGWIN__)
  248. bsdtar->progname = strrchr(*argv, '\\');
  249. #else
  250. bsdtar->progname = strrchr(*argv, '/');
  251. #endif
  252. if (bsdtar->progname != NULL)
  253. bsdtar->progname++;
  254. else
  255. bsdtar->progname = *argv;
  256. }
  257. /* We don't have a machine # yet. */
  258. bsdtar->machinenum = (uint64_t)(-1);
  259. /* We don't have any passphrase entry method yet. */
  260. bsdtar->option_passphrase_entry = PASSPHRASE_UNSET;
  261. /* Allocate space for config file names; at most argc of them. */
  262. if ((bsdtar->configfiles = malloc(argc * sizeof(const char *))) == NULL)
  263. bsdtar_errc(bsdtar, 1, ENOMEM, "Cannot allocate memory");
  264. bsdtar->nconfigfiles = 0;
  265. time(&now);
  266. bsdtar->creationtime = now;
  267. if (setlocale(LC_ALL, "") == NULL)
  268. bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
  269. #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
  270. bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
  271. #endif
  272. possible_help_request = 0;
  273. /* Initialize key cache. We don't have any keys yet. */
  274. if (crypto_keys_init())
  275. exit(1);
  276. /*
  277. * Make stdout line-buffered (if possible) so that operations such as
  278. * "tarsnap --list-archives | more" will run more smoothly. The only
  279. * downside to this is a slight performance cost; but we don't write
  280. * enough data to stdout for that to matter.
  281. */
  282. setvbuf(stdout, NULL, _IOLBF, 0);
  283. /*
  284. * Unless specified otherwise, we consider ourselves to be
  285. * constructing a snapshot of the disk as it is right now.
  286. */
  287. /*
  288. * POSIX doesn't provide any mechanism for distinguishing between
  289. * an error and the time (time_t)(-1). Since we only use this to
  290. * avoid race conditions in the chunkification cache (i.e., so
  291. * that we can determine if a file has been modified since it was
  292. * last backed up), and hopefully nobody will have any files with
  293. * negative last-modified dates, an error return of (-1) can be
  294. * handled the same was as a legitimate return of (-1): Nothing
  295. * gets cached.
  296. */
  297. bsdtar->snaptime = time(NULL);
  298. /* Store original argument vector. */
  299. bsdtar->argc_orig = argc;
  300. bsdtar->argv_orig = argv;
  301. /* Look up the current user and his home directory. */
  302. if ((pws = getpwuid(geteuid())) != NULL)
  303. if ((bsdtar->homedir = strdup(pws->pw_dir)) == NULL)
  304. bsdtar_errc(bsdtar, 1, ENOMEM, "Cannot allocate memory");
  305. /* Look up uid of current user for future reference */
  306. bsdtar->user_uid = geteuid();
  307. /* Default: preserve mod time on extract */
  308. bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
  309. /* Default: Perform basic security checks. */
  310. bsdtar->extract_flags |= SECURITY;
  311. /* Defaults for root user: */
  312. if (bsdtar_is_privileged(bsdtar)) {
  313. /* --same-owner */
  314. bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
  315. /* -p */
  316. bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
  317. bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
  318. bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
  319. bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
  320. }
  321. bsdtar->argv = argv;
  322. bsdtar->argc = argc;
  323. /* We gather some options in a 'delayed options queue'. */
  324. bsdtar->delopt = NULL;
  325. bsdtar->delopt_tail = &bsdtar->delopt;
  326. /* Check if any argument is --dump-config; if so, print them all. */
  327. for (j = 0; j < argc; j++)
  328. if (strcmp("--dump-config", bsdtar->argv[j]) == 0)
  329. bsdtar->option_dump_config = 1;
  330. if (bsdtar->option_dump_config) {
  331. fprintf(stderr, "Command-line:\n ");
  332. for (j = 0; j < argc; j++)
  333. fprintf(stderr, " %s", argv[j]);
  334. fprintf(stderr, "\n");
  335. }
  336. /*
  337. * Comments following each option indicate where that option
  338. * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
  339. * no such comment, then I don't know of anyone else who
  340. * implements that option.
  341. */
  342. while ((opt = bsdtar_getopt(bsdtar)) != -1) {
  343. switch (opt) {
  344. case OPTION_AGGRESSIVE_NETWORKING: /* tarsnap */
  345. optq_push(bsdtar, "aggressive-networking", NULL);
  346. break;
  347. case OPTION_ARCHIVE_NAMES:
  348. if (bsdtar->option_archive_names_set)
  349. bsdtar_errc(bsdtar, 1, 0,
  350. "Two --archive-names options given");
  351. if (bsdtar->optarg == NULL)
  352. bsdtar_errc(bsdtar, 1, 0,
  353. "Argument required for --archive-names");
  354. bsdtar->option_archive_names_set = 1;
  355. /* Read tapenames from --archive_names file. */
  356. process_lines(bsdtar, bsdtar->optarg,
  357. archive_names_helper, 0);
  358. case 'B': /* GNU tar */
  359. /* libarchive doesn't need this; just ignore it. */
  360. break;
  361. case 'C': /* GNU tar */
  362. if (strlen(bsdtar->optarg) == 0)
  363. bsdtar_errc(bsdtar, 1, 0,
  364. "Meaningless option: -C ''");
  365. set_chdir(bsdtar, bsdtar->optarg);
  366. break;
  367. case 'c': /* SUSv2 */
  368. set_mode(bsdtar, opt, "-c");
  369. break;
  370. case OPTION_CACHEDIR: /* multitar */
  371. optq_push(bsdtar, "cachedir", bsdtar->optarg);
  372. break;
  373. case OPTION_CHECK_LINKS: /* GNU tar */
  374. bsdtar->option_warn_links = 1;
  375. break;
  376. case OPTION_CHECKPOINT_BYTES: /* tarsnap */
  377. optq_push(bsdtar, "checkpoint-bytes", bsdtar->optarg);
  378. break;
  379. case OPTION_CHROOT: /* NetBSD */
  380. bsdtar->option_chroot = 1;
  381. break;
  382. case OPTION_CONFIGFILE:
  383. bsdtar->configfiles[bsdtar->nconfigfiles++] =
  384. bsdtar->optarg;
  385. break;
  386. case OPTION_CREATIONTIME: /* tarsnap */
  387. errno = 0;
  388. bsdtar->creationtime = strtol(bsdtar->optarg,
  389. NULL, 0);
  390. if ((errno) || (bsdtar->creationtime == 0))
  391. bsdtar_errc(bsdtar, 1, 0,
  392. "Invalid --creationtime argument: %s",
  393. bsdtar->optarg);
  394. break;
  395. case OPTION_CSV_FILE: /* tarsnap */
  396. if (bsdtar->option_csv_filename != NULL)
  397. bsdtar_errc(bsdtar, 1, 0,
  398. "Two --csv-file options given");
  399. if ((bsdtar->option_csv_filename = strdup(
  400. bsdtar->optarg)) == NULL)
  401. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  402. break;
  403. case 'd': /* multitar */
  404. set_mode(bsdtar, opt, "-d");
  405. break;
  406. case OPTION_DEBUG_NETWORK_STATS: /* tarsnap, developers only. */
  407. tarsnap_opt_debug_network_stats = 1;
  408. break;
  409. case OPTION_DUMP_CONFIG: /* tarsnap */
  410. /* Do nothing; already handled. */
  411. break;
  412. case OPTION_DISK_PAUSE: /* tarsnap */
  413. optq_push(bsdtar, "disk-pause", bsdtar->optarg);
  414. break;
  415. case OPTION_DRYRUN: /* tarsnap */
  416. if (bsdtar->option_dryrun != 0)
  417. bsdtar_errc(bsdtar, 1, 0,
  418. "Can only specify one --dry-run* option");
  419. bsdtar->option_dryrun = 1;
  420. break;
  421. case OPTION_DRYRUN_METADATA: /* tarsnap */
  422. if (bsdtar->option_dryrun != 0)
  423. bsdtar_errc(bsdtar, 1, 0,
  424. "Can only specify one --dry-run* option");
  425. bsdtar->option_dryrun = 2;
  426. break;
  427. case OPTION_EXCLUDE: /* GNU tar */
  428. optq_push(bsdtar, "exclude", bsdtar->optarg);
  429. break;
  430. case 'f': /* multitar */
  431. if ((tapename_cmdline = strdup(bsdtar->optarg)) == NULL)
  432. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  433. if (strlist_append(bsdtar->tapenames_setup,
  434. &tapename_cmdline, 1))
  435. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  436. break;
  437. case OPTION_FORCE_RESOURCES:
  438. optq_push(bsdtar, "force-resources", NULL);
  439. break;
  440. case OPTION_FSCK: /* multitar */
  441. set_mode(bsdtar, opt, "--fsck");
  442. break;
  443. case OPTION_FSCK_PRUNE: /* multitar */
  444. set_mode(bsdtar, opt, "--fsck-prune");
  445. break;
  446. case 'H': /* BSD convention */
  447. bsdtar->symlink_mode = 'H';
  448. break;
  449. case 'h': /* Linux Standards Base, gtar; synonym for -L */
  450. bsdtar->symlink_mode = 'L';
  451. /* Hack: -h by itself is the "help" command. */
  452. possible_help_request = 1;
  453. break;
  454. case OPTION_HASHES: /* tarsnap */
  455. bsdtar->option_hashes = 1;
  456. break;
  457. case OPTION_HELP: /* GNU tar, others */
  458. long_help(bsdtar);
  459. exit(0);
  460. break;
  461. case OPTION_HUMANIZE_NUMBERS: /* tarsnap */
  462. optq_push(bsdtar, "humanize-numbers", NULL);
  463. break;
  464. case 'I': /* GNU tar */
  465. /*
  466. * TODO: Allow 'names' to come from an archive,
  467. * not just a text file. Design a good UI for
  468. * allowing names and mode/owner to be read
  469. * from an archive, with contents coming from
  470. * disk. This can be used to "refresh" an
  471. * archive or to design archives with special
  472. * permissions without having to create those
  473. * permissions on disk.
  474. */
  475. bsdtar->names_from_file = bsdtar->optarg;
  476. break;
  477. case OPTION_INCLUDE:
  478. optq_push(bsdtar, "include", bsdtar->optarg);
  479. break;
  480. case OPTION_INITIALIZE_CACHEDIR:
  481. set_mode(bsdtar, opt, "--initialize-cachedir");
  482. break;
  483. case OPTION_INSANE_FILESYSTEMS:
  484. optq_push(bsdtar, "insane-filesystems", NULL);
  485. break;
  486. case OPTION_ISO_DATES:
  487. optq_push(bsdtar, "iso-dates", NULL);
  488. break;
  489. case 'k': /* GNU tar */
  490. bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
  491. break;
  492. case OPTION_KEEP_GOING: /* tarsnap */
  493. bsdtar->option_keep_going = 1;
  494. break;
  495. case OPTION_KEEP_NEWER_FILES: /* GNU tar */
  496. bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
  497. break;
  498. case OPTION_KEYFILE: /* tarsnap */
  499. optq_push(bsdtar, "keyfile", bsdtar->optarg);
  500. break;
  501. case 'L': /* BSD convention */
  502. bsdtar->symlink_mode = 'L';
  503. break;
  504. case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
  505. /* GNU tar 1.13 used -l for --one-file-system */
  506. bsdtar->option_warn_links = 1;
  507. break;
  508. case OPTION_LIST_ARCHIVES: /* multitar */
  509. set_mode(bsdtar, opt, "--list-archives");
  510. break;
  511. case OPTION_LOWMEM: /* tarsnap */
  512. optq_push(bsdtar, "lowmem", NULL);
  513. break;
  514. case 'm': /* SUSv2 */
  515. bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
  516. break;
  517. case OPTION_MAXBW: /* tarsnap */
  518. optq_push(bsdtar, "maxbw", bsdtar->optarg);
  519. break;
  520. case OPTION_MAXBW_RATE: /* tarsnap */
  521. optq_push(bsdtar, "maxbw-rate", bsdtar->optarg);
  522. break;
  523. case OPTION_MAXBW_RATE_DOWN: /* tarsnap */
  524. optq_push(bsdtar, "maxbw-rate-down", bsdtar->optarg);
  525. break;
  526. case OPTION_MAXBW_RATE_UP: /* tarsnap */
  527. optq_push(bsdtar, "maxbw-rate-up", bsdtar->optarg);
  528. break;
  529. case 'n': /* GNU tar */
  530. bsdtar->option_no_subdirs = 1;
  531. break;
  532. /*
  533. * Selecting files by time:
  534. * --newer-?time='date' Only files newer than 'date'
  535. * --newer-?time-than='file' Only files newer than time
  536. * on specified file (useful for incremental backups)
  537. * TODO: Add corresponding "older" options to reverse these.
  538. */
  539. case OPTION_NEWER_CTIME: /* GNU tar */
  540. if ((bsdtar->newer_ctime_sec =
  541. get_date(now, bsdtar->optarg)) == -1)
  542. bsdtar_errc(bsdtar, 1, EINVAL,
  543. "Could not parse date");
  544. break;
  545. case OPTION_NEWER_CTIME_THAN:
  546. {
  547. struct stat st;
  548. if (stat(bsdtar->optarg, &st) != 0)
  549. bsdtar_errc(bsdtar, 1, 0,
  550. "Can't open file %s", bsdtar->optarg);
  551. bsdtar->newer_ctime_sec = st.st_ctime;
  552. bsdtar->newer_ctime_nsec =
  553. ARCHIVE_STAT_CTIME_NANOS(&st);
  554. }
  555. break;
  556. case OPTION_NEWER_MTIME: /* GNU tar */
  557. if ((bsdtar->newer_mtime_sec =
  558. get_date(now, bsdtar->optarg)) == -1)
  559. bsdtar_errc(bsdtar, 1, EINVAL,
  560. "Could not parse date");
  561. break;
  562. case OPTION_NEWER_MTIME_THAN:
  563. {
  564. struct stat st;
  565. if (stat(bsdtar->optarg, &st) != 0)
  566. bsdtar_errc(bsdtar, 1, 0,
  567. "Can't open file %s", bsdtar->optarg);
  568. bsdtar->newer_mtime_sec = st.st_mtime;
  569. bsdtar->newer_mtime_nsec =
  570. ARCHIVE_STAT_MTIME_NANOS(&st);
  571. }
  572. break;
  573. case OPTION_NODUMP: /* star */
  574. optq_push(bsdtar, "nodump", NULL);
  575. break;
  576. case OPTION_NOISY_WARNINGS: /* tarsnap */
  577. tarsnap_opt_noisy_warnings = 1;
  578. break;
  579. case OPTION_NORMALMEM:
  580. optq_push(bsdtar, "normalmem", NULL);
  581. break;
  582. case OPTION_NO_AGGRESSIVE_NETWORKING:
  583. optq_push(bsdtar, "no-aggressive-networking", NULL);
  584. break;
  585. case OPTION_NO_CONFIG_EXCLUDE:
  586. optq_push(bsdtar, "no-config-exclude", NULL);
  587. break;
  588. case OPTION_NO_CONFIG_INCLUDE:
  589. optq_push(bsdtar, "no-config-include", NULL);
  590. break;
  591. case OPTION_NO_DEFAULT_CONFIG:
  592. bsdtar->option_no_default_config = 1;
  593. break;
  594. case OPTION_NO_DISK_PAUSE:
  595. optq_push(bsdtar, "no-disk-pause", NULL);
  596. break;
  597. case OPTION_NO_FORCE_RESOURCES:
  598. optq_push(bsdtar, "no-force-resources", NULL);
  599. break;
  600. case OPTION_NO_HUMANIZE_NUMBERS:
  601. optq_push(bsdtar, "no-humanize-numbers", NULL);
  602. break;
  603. case OPTION_NO_INSANE_FILESYSTEMS:
  604. optq_push(bsdtar, "no-insane-filesystems", NULL);
  605. break;
  606. case OPTION_NO_ISO_DATES:
  607. optq_push(bsdtar, "no-iso-dates", NULL);
  608. break;
  609. case OPTION_NO_MAXBW:
  610. optq_push(bsdtar, "no-maxbw", NULL);
  611. break;
  612. case OPTION_NO_MAXBW_RATE_DOWN:
  613. optq_push(bsdtar, "no-maxbw-rate-down", NULL);
  614. break;
  615. case OPTION_NO_MAXBW_RATE_UP:
  616. optq_push(bsdtar, "no-maxbw-rate-up", NULL);
  617. break;
  618. case OPTION_NO_NODUMP:
  619. optq_push(bsdtar, "no-nodump", NULL);
  620. break;
  621. case OPTION_NO_PRINT_STATS:
  622. optq_push(bsdtar, "no-print-stats", NULL);
  623. break;
  624. case OPTION_NO_PROGRESS_BYTES: /* tarsnap */
  625. optq_push(bsdtar, "no-progress-bytes", NULL);
  626. break;
  627. case OPTION_NO_QUIET:
  628. optq_push(bsdtar, "no-quiet", NULL);
  629. break;
  630. case OPTION_NO_RETRY_FOREVER:
  631. optq_push(bsdtar, "no-retry-forever", NULL);
  632. break;
  633. case OPTION_NO_SAME_OWNER: /* GNU tar */
  634. bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
  635. break;
  636. case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
  637. bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
  638. bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
  639. bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
  640. bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
  641. break;
  642. case OPTION_NO_SNAPTIME:
  643. optq_push(bsdtar, "no-snaptime", NULL);
  644. break;
  645. case OPTION_NO_STORE_ATIME:
  646. optq_push(bsdtar, "no-store-atime", NULL);
  647. break;
  648. case OPTION_NO_TOTALS:
  649. optq_push(bsdtar, "no-totals", NULL);
  650. break;
  651. case OPTION_NUKE: /* tarsnap */
  652. set_mode(bsdtar, opt, "--nuke");
  653. break;
  654. case OPTION_NULL: /* GNU tar */
  655. case OPTION_NULL_INPUT: /* tarsnap */
  656. bsdtar->option_null_input++;
  657. break;
  658. case OPTION_NULL_OUTPUT: /* tarsnap */
  659. bsdtar->option_null_output++;
  660. break;
  661. case OPTION_NUMERIC_OWNER: /* GNU tar */
  662. bsdtar->option_numeric_owner++;
  663. break;
  664. case 'O': /* GNU tar */
  665. bsdtar->option_stdout = 1;
  666. break;
  667. case 'o':
  668. bsdtar->option_no_owner = 1;
  669. bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
  670. break;
  671. case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
  672. bsdtar->option_dont_traverse_mounts = 1;
  673. break;
  674. case 'P': /* GNU tar */
  675. bsdtar->extract_flags &= ~SECURITY;
  676. bsdtar->option_absolute_paths = 1;
  677. break;
  678. case 'p': /* GNU tar, star */
  679. bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
  680. bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
  681. bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
  682. bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
  683. break;
  684. case OPTION_PASSPHRASE: /* tarsnap */
  685. optq_push(bsdtar, "passphrase", bsdtar->optarg);
  686. break;
  687. case OPTION_PRINT_STATS: /* multitar */
  688. bsdtar->option_print_stats = 1;
  689. break;
  690. case OPTION_PROGRESS_BYTES: /* tarsnap */
  691. optq_push(bsdtar, "progress-bytes", bsdtar->optarg);
  692. break;
  693. case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
  694. bsdtar->option_fast_read = 1;
  695. break;
  696. case OPTION_QUIET:
  697. optq_push(bsdtar, "quiet", NULL);
  698. break;
  699. case 'r': /* multitar */
  700. set_mode(bsdtar, opt, "-r");
  701. break;
  702. case OPTION_RECOVER:
  703. set_mode(bsdtar, opt, "--recover");
  704. break;
  705. case OPTION_RESUME_EXTRACT:
  706. bsdtar->option_resume_extract = 1;
  707. break;
  708. case OPTION_RETRY_FOREVER:
  709. optq_push(bsdtar, "retry-forever", NULL);
  710. break;
  711. case OPTION_SNAPTIME: /* multitar */
  712. optq_push(bsdtar, "snaptime", bsdtar->optarg);
  713. break;
  714. case OPTION_STORE_ATIME: /* multitar */
  715. optq_push(bsdtar, "store-atime", NULL);
  716. break;
  717. case 'S': /* NetBSD pax-as-tar */
  718. bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
  719. break;
  720. case 's': /* NetBSD pax-as-tar */
  721. #if HAVE_REGEX_H
  722. add_substitution(bsdtar, bsdtar->optarg);
  723. #else
  724. bsdtar_warnc(bsdtar, 0,
  725. "-s is not supported by this version of tarsnap");
  726. usage(bsdtar);
  727. #endif
  728. break;
  729. case OPTION_SAME_OWNER: /* GNU tar */
  730. bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
  731. break;
  732. case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
  733. errno = 0;
  734. bsdtar->strip_components = strtol(bsdtar->optarg,
  735. NULL, 0);
  736. if (errno)
  737. bsdtar_errc(bsdtar, 1, 0,
  738. "Invalid --strip-components argument: %s",
  739. bsdtar->optarg);
  740. break;
  741. case 'T': /* GNU tar */
  742. bsdtar->names_from_file = bsdtar->optarg;
  743. break;
  744. case 't': /* SUSv2 */
  745. set_mode(bsdtar, opt, "-t");
  746. bsdtar->verbose++;
  747. break;
  748. case OPTION_TOTALS: /* GNU tar */
  749. optq_push(bsdtar, "totals", NULL);
  750. break;
  751. case 'U': /* GNU tar */
  752. bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
  753. bsdtar->option_unlink_first = 1;
  754. break;
  755. case 'v': /* SUSv2 */
  756. bsdtar->verbose++;
  757. break;
  758. case OPTION_VERIFY_CONFIG:
  759. set_mode(bsdtar, opt, "--verify-config");
  760. break;
  761. case OPTION_VERSION: /* GNU convention */
  762. version();
  763. break;
  764. case OPTION_VERYLOWMEM: /* tarsnap */
  765. optq_push(bsdtar, "verylowmem", NULL);
  766. break;
  767. #if 0
  768. /*
  769. * The -W longopt feature is handled inside of
  770. * bsdtar_getopt(), so -W is not available here.
  771. */
  772. case 'W': /* Obscure GNU convention. */
  773. break;
  774. #endif
  775. case 'w': /* SUSv2 */
  776. bsdtar->option_interactive = 1;
  777. break;
  778. case 'X': /* GNU tar */
  779. if (exclude_from_file(bsdtar, bsdtar->optarg))
  780. bsdtar_errc(bsdtar, 1, 0,
  781. "failed to process exclusions from file %s",
  782. bsdtar->optarg);
  783. break;
  784. case 'x': /* SUSv2 */
  785. set_mode(bsdtar, opt, "-x");
  786. break;
  787. default:
  788. usage(bsdtar);
  789. }
  790. }
  791. /*
  792. * Sanity-check options.
  793. */
  794. /*
  795. * If --print-stats was specified but no mode was set, then
  796. * --print-stats *is* the mode.
  797. */
  798. if ((bsdtar->mode == '\0') && (bsdtar->option_print_stats == 1))
  799. set_mode(bsdtar, OPTION_PRINT_STATS, "--print-stats");
  800. /*
  801. * We can reuse the --verify-config mode for --dump-config.
  802. * These lines must come after --print-stats, in case somebody wishes
  803. * to see the config that's being used for the --print-stats mode.
  804. */
  805. if ((bsdtar->mode == '\0') && (bsdtar->option_dump_config == 1))
  806. set_mode(bsdtar, OPTION_VERIFY_CONFIG, "--verify-config");
  807. /* If no "real" mode was specified, treat -h as --help. */
  808. if ((bsdtar->mode == '\0') && possible_help_request) {
  809. long_help(bsdtar);
  810. exit(0);
  811. }
  812. /*
  813. * If we're doing a dry run and the user hasn't specified an archive
  814. * name via -f, use a fake name. This will result in the statistics
  815. * printed by --print-stats being a few bytes off, since the archive
  816. * name is included in the metadata block... but we're going to be a
  817. * few bytes off anyway since the command line, including "--dry-run"
  818. * is included in the metadata.
  819. */
  820. if (bsdtar->option_dryrun &&
  821. (strlist_getsize(bsdtar->tapenames_setup) == 0)) {
  822. if ((tapename_cmdline = strdup("(dry-run)")) == NULL)
  823. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  824. if (strlist_append(bsdtar->tapenames_setup,
  825. &tapename_cmdline, 1))
  826. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  827. }
  828. /* At this point we must have a mode set. */
  829. if (bsdtar->mode == '\0')
  830. bsdtar_errc(bsdtar, 1, 0,
  831. "Must specify one of -c, -d, -r, -t, -x,"
  832. " --list-archives, --print-stats,"
  833. " --fsck, --fsck-prune, or --nuke");
  834. /* Process "delayed" command-line options which we queued earlier. */
  835. while (bsdtar->delopt != NULL) {
  836. dooption(bsdtar, bsdtar->delopt->opt_name,
  837. bsdtar->delopt->opt_arg, 0);
  838. optq_pop(bsdtar);
  839. }
  840. /* Process config files passed on the command line. */
  841. for (i = 0; i < bsdtar->nconfigfiles; i++)
  842. configfile(bsdtar, bsdtar->configfiles[i], 1);
  843. /* If we do not have --no-default-config, process default configs. */
  844. if (bsdtar->option_no_default_config == 0) {
  845. /* Process options from ~/.tarsnaprc. */
  846. if (bsdtar->homedir != NULL) {
  847. if (asprintf(&bsdtar->conffile, "%s/.tarsnaprc",
  848. bsdtar->homedir) == -1)
  849. bsdtar_errc(bsdtar, 1, errno, "No memory");
  850. configfile(bsdtar, bsdtar->conffile, 0);
  851. /* Free string allocated by asprintf. */
  852. free(bsdtar->conffile);
  853. bsdtar->conffile = NULL;
  854. }
  855. /* Check if ${XDG_CONFIG_HOME} is set. */
  856. if ((xdg_configdir = getenv("XDG_CONFIG_HOME")) != NULL) {
  857. /*
  858. * If it exists, use
  859. * ${XDG_CONFIG_HOME}/tarsnap/tarsnap.conf
  860. */
  861. if (asprintf(&bsdtar->conffile,
  862. "%s/tarsnap/tarsnap.conf", xdg_configdir) == -1)
  863. bsdtar_errc(bsdtar, 1, errno, "No memory");
  864. } else if (bsdtar->homedir != NULL) {
  865. /*
  866. * If it doesn't exist, use
  867. * $HOME/.config/tarsnap/tarsnap.conf
  868. */
  869. if (asprintf(&bsdtar->conffile,
  870. "%s/.config/tarsnap/tarsnap.conf", bsdtar->homedir)
  871. == -1)
  872. bsdtar_errc(bsdtar, 1, errno, "No memory");
  873. }
  874. /* Read tarsnap.conf (if applicable). */
  875. if (bsdtar->conffile != NULL) {
  876. configfile(bsdtar, bsdtar->conffile, 0);
  877. /* Free string allocated by asprintf. */
  878. free(bsdtar->conffile);
  879. bsdtar->conffile = NULL;
  880. }
  881. /* Process options from system-wide tarsnap.conf. */
  882. configfile(bsdtar, ETC_TARSNAP_CONF, 0);
  883. }
  884. /* Extract tapenames from tapenames_setup. */
  885. if (strlist_export(bsdtar->tapenames_setup, &bsdtar->tapenames,
  886. &bsdtar->ntapes))
  887. bsdtar_errc(bsdtar, 1, 0, "Out of memory");
  888. bsdtar->tapenames_setup = NULL;
  889. /* Continue with more sanity-checking. */
  890. if ((bsdtar->ntapes == 0) &&
  891. (bsdtar->mode != OPTION_PRINT_STATS &&
  892. bsdtar->mode != OPTION_LIST_ARCHIVES &&
  893. bsdtar->mode != OPTION_RECOVER &&
  894. bsdtar->mode != OPTION_FSCK &&
  895. bsdtar->mode != OPTION_FSCK_PRUNE &&
  896. bsdtar->mode != OPTION_INITIALIZE_CACHEDIR &&
  897. bsdtar->mode != OPTION_NUKE &&
  898. bsdtar->mode != OPTION_VERIFY_CONFIG))
  899. bsdtar_errc(bsdtar, 1, 0,
  900. "Archive name must be specified");
  901. if ((bsdtar->ntapes > 1) &&
  902. (bsdtar->mode != OPTION_PRINT_STATS &&
  903. bsdtar->mode != OPTION_LIST_ARCHIVES &&
  904. bsdtar->mode != 'd'))
  905. bsdtar_errc(bsdtar, 1, 0,
  906. "Option -f may only be specified once in mode %s",
  907. bsdtar->modestr);
  908. if ((bsdtar->mode == 'c') &&
  909. (strlen(bsdtar->tapenames[0]) > 1023))
  910. bsdtar_errc(bsdtar, 1, 0,
  911. "Cannot create an archive with a name > 1023 characters");
  912. if ((bsdtar->mode == 'c') &&
  913. (strlen(bsdtar->tapenames[0]) == 0))
  914. bsdtar_errc(bsdtar, 1, 0,
  915. "Cannot create an archive with an empty name");
  916. if ((bsdtar->cachedir == NULL) &&
  917. (((bsdtar->mode == 'c') && (!bsdtar->option_dryrun)) ||
  918. bsdtar->mode == 'd' ||
  919. bsdtar->mode == OPTION_RECOVER ||
  920. bsdtar->mode == OPTION_FSCK ||
  921. bsdtar->mode == OPTION_FSCK_PRUNE ||
  922. bsdtar->mode == OPTION_INITIALIZE_CACHEDIR ||
  923. bsdtar->mode == OPTION_PRINT_STATS))
  924. bsdtar_errc(bsdtar, 1, 0,
  925. "Cache directory must be specified for %s",
  926. bsdtar->modestr);
  927. if (bsdtar->mode == 'd' ||
  928. bsdtar->mode == OPTION_RECOVER ||
  929. bsdtar->mode == OPTION_PRINT_STATS) {
  930. switch (chunks_directory_exists(bsdtar->cachedir)) {
  931. case 1:
  932. /* Exists; good. */
  933. break;
  934. case 0:
  935. /* Does not exist; error and exit. */
  936. bsdtar_errc(bsdtar, 1, 0,
  937. "Cache directory must already exist for %s;"
  938. " check --cachedir and/or run --fsck",
  939. bsdtar->modestr);
  940. case -1:
  941. /* Could not check; error and exit. */
  942. bsdtar_errc(bsdtar, 1, errno,
  943. "Could not check if cachedir exists");
  944. }
  945. }
  946. if (tarsnap_opt_aggressive_networking != 0) {
  947. if ((bsdtar->bwlimit_rate_up != 0) ||
  948. (bsdtar->bwlimit_rate_down != 0)) {
  949. bsdtar_warnc(bsdtar, 0,
  950. "--aggressive-networking is incompatible with"
  951. " --maxbw-rate options;\n"
  952. " disabling --aggressive-networking");
  953. tarsnap_opt_aggressive_networking = 0;
  954. }
  955. }
  956. if ((bsdtar->option_dryrun == 2) && bsdtar->option_print_stats)
  957. bsdtar_errc(bsdtar, 1, 0, "--dry-run-metadata is "
  958. "incompatible with --print-stats");
  959. /*
  960. * The -f option doesn't make sense for --fsck, --fsck-prune, or
  961. * --nuke. It can be used with --list-archives --hash, but not
  962. * --list-archives on its own; sanity-checking that detail is
  963. * done in tarsnap_mode_list_archives().
  964. */
  965. if ((bsdtar->ntapes > 0) &&
  966. (bsdtar->mode != OPTION_LIST_ARCHIVES) &&
  967. (bsdtar->mode != OPTION_PRINT_STATS))
  968. only_mode(bsdtar, "-f", "cxtdr");
  969. /*
  970. * These options don't make sense for the "delete" and "convert to
  971. * tar" modes.
  972. */
  973. if (bsdtar->pending_chdir)
  974. only_mode(bsdtar, "-C", "cxt");
  975. if (bsdtar->names_from_file)
  976. only_mode(bsdtar, "-T", "cxt");
  977. if (bsdtar->newer_ctime_sec || bsdtar->newer_ctime_nsec)
  978. only_mode(bsdtar, "--newer", "cxt");
  979. if (bsdtar->newer_mtime_sec || bsdtar->newer_mtime_nsec)
  980. only_mode(bsdtar, "--newer-mtime", "cxt");
  981. if (bsdtar->option_absolute_paths)
  982. only_mode(bsdtar, "-P", "cxt");
  983. if (bsdtar->option_null_input)
  984. only_mode(bsdtar, "--null-input", "cxt");
  985. if (bsdtar->option_null_output) {
  986. if (bsdtar->mode != OPTION_LIST_ARCHIVES)
  987. only_mode(bsdtar, "--null-output", "xt");
  988. }
  989. /* We should only have remaining args in -c, -t, and -x modes. */
  990. if (bsdtar->argc != 0) {
  991. if (!((bsdtar->mode == 'c') || (bsdtar->mode == 't') ||
  992. (bsdtar->mode == 'x'))) {
  993. bsdtar_errc(bsdtar, 1, 0, "Cannot have unused "
  994. "command-line args in this mode.");
  995. }
  996. }
  997. /* Check options only permitted in certain modes. */
  998. if (bsdtar->option_dont_traverse_mounts)
  999. only_mode(bsdtar, "--one-file-system", "c");
  1000. if (bsdtar->option_fast_read)
  1001. only_mode(bsdtar, "--fast-read", "xt");
  1002. if (bsdtar->option_no_subdirs)
  1003. only_mode(bsdtar, "-n", "c");
  1004. if (bsdtar->option_no_owner)
  1005. only_mode(bsdtar, "-o", "x");
  1006. if (bsdtar->option_stdout)
  1007. only_mode(bsdtar, "-O", "xt");
  1008. if (bsdtar->option_unlink_first)
  1009. only_mode(bsdtar, "-U", "x");
  1010. if (bsdtar->option_warn_links)
  1011. only_mode(bsdtar, "--check-links", "c");
  1012. if (bsdtar->option_dryrun == 1)
  1013. only_mode(bsdtar, "--dry-run", "c");
  1014. if (bsdtar->option_dryrun == 2)
  1015. only_mode(bsdtar, "--dry-run-metadata", "c");
  1016. /* Check other parameters only permitted in certain modes. */
  1017. if (bsdtar->symlink_mode != '\0') {
  1018. strcpy(buff, "-?");
  1019. buff[1] = bsdtar->symlink_mode;
  1020. only_mode(bsdtar, buff, "c");
  1021. }
  1022. if (bsdtar->strip_components != 0)
  1023. only_mode(bsdtar, "--strip-components", "xt");
  1024. /* The configuration provided is syntactically correct. */
  1025. if (bsdtar->mode == OPTION_VERIFY_CONFIG)
  1026. exit(0);
  1027. /*
  1028. * Special case: if we're doing a dryrun and the keyfile came
  1029. * from a config file, ignore a non-existent keyfile.
  1030. */
  1031. if (bsdtar->keyfile && bsdtar->keyfile_from_config &&
  1032. bsdtar->option_dryrun && (access(bsdtar->keyfile, F_OK) == -1)) {
  1033. free(bsdtar->keyfile);
  1034. bsdtar->keyfile = NULL;
  1035. bsdtar->config_file_keyfile_failed = 1;
  1036. }
  1037. /* Attempt to load keyfile. */
  1038. if (bsdtar->keyfile != NULL) {
  1039. if (load_keys(bsdtar, bsdtar->keyfile) == 0)
  1040. bsdtar->have_keys = 1;
  1041. else {
  1042. bsdtar_errc(bsdtar, 1, errno,
  1043. "Cannot read key file: %s", bsdtar->keyfile);
  1044. }
  1045. }
  1046. /*
  1047. * If the keyfile in the config file is invalid but we're doing a
  1048. * dryrun, continue anyway (and don't use a cachedir).
  1049. */
  1050. if (bsdtar->config_file_keyfile_failed && bsdtar->option_dryrun &&
  1051. bsdtar->cachedir != NULL) {
  1052. bsdtar_warnc(bsdtar, 0,
  1053. "Ignoring cachedir due to missing or invalid "
  1054. "keyfile in config file.");
  1055. free(bsdtar->cachedir);
  1056. bsdtar->cachedir = NULL;
  1057. }
  1058. /*
  1059. * Canonicalize the path to the cache directories. This is
  1060. * necessary since the tar code can change directories.
  1061. */
  1062. if (bsdtar->cachedir != NULL) {
  1063. if (build_dir(bsdtar->cachedir, "--cachedir") != 0)
  1064. bsdtar_errc(bsdtar, 1, 0,
  1065. "Failed to ensure that cachedir exists");
  1066. if (realpath(bsdtar->cachedir, cachedir) == NULL)
  1067. bsdtar_errc(bsdtar, 1, errno, "realpath(%s)",
  1068. bsdtar->cachedir);
  1069. free(bsdtar->cachedir);
  1070. if ((bsdtar->cachedir = strdup(cachedir)) == NULL)
  1071. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1072. }
  1073. /* If we're running --fsck, figure out which key to use. */
  1074. if (bsdtar->mode == OPTION_FSCK) {
  1075. if (crypto_keys_missing(CRYPTO_KEYMASK_AUTH_PUT) == NULL)
  1076. bsdtar->mode = OPTION_FSCK_WRITE;
  1077. else if (crypto_keys_missing(CRYPTO_KEYMASK_AUTH_DELETE) == NULL)
  1078. bsdtar->mode = OPTION_FSCK_DELETE;
  1079. else
  1080. bsdtar_errc(bsdtar, 1, 0,
  1081. "The write or delete authorization key is"
  1082. " required for --fsck but is not available");
  1083. }
  1084. /* If we're running --recover, figure out which key to use. */
  1085. if (bsdtar->mode == OPTION_RECOVER) {
  1086. if (crypto_keys_missing(CRYPTO_KEYMASK_AUTH_PUT) == NULL)
  1087. bsdtar->mode = OPTION_RECOVER_WRITE;
  1088. else if (crypto_keys_missing(CRYPTO_KEYMASK_AUTH_DELETE) == NULL)
  1089. bsdtar->mode = OPTION_RECOVER_DELETE;
  1090. else
  1091. bsdtar_errc(bsdtar, 1, 0,
  1092. "The write or delete authorization key is"
  1093. " required for --recover but is not available");
  1094. }
  1095. /* Make sure we have whatever keys we're going to need. */
  1096. if (bsdtar->have_keys == 0) {
  1097. if (!bsdtar->option_dryrun) {
  1098. bsdtar_errc(bsdtar, 1, 0,
  1099. "Keys must be provided via --keyfile option");
  1100. } else {
  1101. if (bsdtar->cachedir != NULL) {
  1102. bsdtar_errc(bsdtar, 1, 0,
  1103. "Option mismatch for --dry-run: cachedir"
  1104. " specified but no keyfile");
  1105. }
  1106. if (crypto_keys_generate(CRYPTO_KEYMASK_USER))
  1107. bsdtar_errc(bsdtar, 1, 0,
  1108. "Error generating keys");
  1109. if (bsdtar->option_print_stats)
  1110. bsdtar_warnc(bsdtar, 0,
  1111. "Performing dry-run archival without keys\n"
  1112. " (sizes may be slightly "
  1113. "inaccurate)");
  1114. }
  1115. }
  1116. missingkey = NULL;
  1117. switch (bsdtar->mode) {
  1118. case 'c':
  1119. if (argv_has_archive_directive(bsdtar))
  1120. missingkey = crypto_keys_missing(CRYPTO_KEYMASK_WRITE | CRYPTO_KEYMASK_READ);
  1121. else
  1122. missingkey = crypto_keys_missing(CRYPTO_KEYMASK_WRITE);
  1123. break;
  1124. case OPTION_RECOVER_WRITE:
  1125. missingkey = crypto_keys_missing(CRYPTO_KEYMASK_WRITE);
  1126. break;
  1127. case 'd':
  1128. case OPTION_FSCK_PRUNE:
  1129. case OPTION_FSCK_DELETE:
  1130. missingkey = crypto_keys_missing(CRYPTO_KEYMASK_READ |
  1131. CRYPTO_KEYMASK_AUTH_DELETE);
  1132. break;
  1133. case OPTION_FSCK_WRITE:
  1134. missingkey = crypto_keys_missing(CRYPTO_KEYMASK_READ |
  1135. CRYPTO_KEYMASK_AUTH_PUT);
  1136. break;
  1137. case OPTION_NUKE:
  1138. case OPTION_RECOVER_DELETE:
  1139. missingkey = crypto_keys_missing(CRYPTO_KEYMASK_AUTH_DELETE);
  1140. break;
  1141. case OPTION_PRINT_STATS:
  1142. /* We don't need keys for printing global stats. */
  1143. if (bsdtar->ntapes == 0)
  1144. break;
  1145. /* FALLTHROUGH */
  1146. case OPTION_LIST_ARCHIVES:
  1147. case 'r':
  1148. case 't':
  1149. case 'x':
  1150. missingkey = crypto_keys_missing(CRYPTO_KEYMASK_READ);
  1151. break;
  1152. }
  1153. if (missingkey != NULL)
  1154. bsdtar_errc(bsdtar, 1, 0,
  1155. "The %s key is required for %s but is not available",
  1156. missingkey, bsdtar->modestr);
  1157. /* Tell the network layer how much bandwidth to use. */
  1158. if (bsdtar->bwlimit_rate_up == 0)
  1159. bsdtar->bwlimit_rate_up = 1000000000.;
  1160. if (bsdtar->bwlimit_rate_down == 0)
  1161. bsdtar->bwlimit_rate_down = 1000000000.;
  1162. network_bwlimit(bsdtar->bwlimit_rate_down, bsdtar->bwlimit_rate_up);
  1163. /* Perform the requested operation. */
  1164. switch(bsdtar->mode) {
  1165. case 'c':
  1166. tarsnap_mode_c(bsdtar);
  1167. break;
  1168. case 'd':
  1169. tarsnap_mode_d(bsdtar);
  1170. break;
  1171. case OPTION_FSCK_DELETE:
  1172. tarsnap_mode_fsck(bsdtar, 0, 1);
  1173. break;
  1174. case OPTION_FSCK_PRUNE:
  1175. tarsnap_mode_fsck(bsdtar, 1, 1);
  1176. break;
  1177. case OPTION_FSCK_WRITE:
  1178. tarsnap_mode_fsck(bsdtar, 0, 0);
  1179. break;
  1180. case OPTION_INITIALIZE_CACHEDIR:
  1181. tarsnap_mode_initialize_cachedir(bsdtar);
  1182. break;
  1183. case OPTION_PRINT_STATS:
  1184. tarsnap_mode_print_stats(bsdtar);
  1185. break;
  1186. case OPTION_RECOVER_DELETE:
  1187. tarsnap_mode_recover(bsdtar, 1);
  1188. break;
  1189. case OPTION_RECOVER_WRITE:
  1190. tarsnap_mode_recover(bsdtar, 0);
  1191. break;
  1192. case OPTION_LIST_ARCHIVES:
  1193. tarsnap_mode_list_archives(bsdtar, bsdtar->option_hashes);
  1194. break;
  1195. case OPTION_NUKE:
  1196. tarsnap_mode_nuke(bsdtar);
  1197. break;
  1198. case 'r':
  1199. tarsnap_mode_r(bsdtar);
  1200. break;
  1201. case 't':
  1202. tarsnap_mode_t(bsdtar);
  1203. break;
  1204. case 'x':
  1205. tarsnap_mode_x(bsdtar);
  1206. break;
  1207. }
  1208. #ifdef DEBUG_SELECTSTATS
  1209. double N, mu, va, max;
  1210. network_getselectstats(&N, &mu, &va, &max);
  1211. fprintf(stderr, "Time-between-select-calls statistics:\n");
  1212. fprintf(stderr, "N = %6g mu = %12g ms "
  1213. "va = %12g ms^2 max = %12g ms\n",
  1214. N, mu * 1000, va * 1000000, max * 1000);
  1215. #endif
  1216. #ifdef PROFILE
  1217. /*
  1218. * If we're compiling with profiling turned on, chdir to a directory
  1219. * into which we're likely to be able to write to before exiting.
  1220. */
  1221. if (bsdtar->cachedir != NULL)
  1222. chdir(cachedir);
  1223. #endif
  1224. if (bsdtar->return_value != 0) {
  1225. bsdtar_warnc(bsdtar, 0,
  1226. "Error exit delayed from previous errors.");
  1227. /* If we modified the storage, return 2 instead, and warn. */
  1228. if (bsdtar->storage_modified) {
  1229. bsdtar->return_value = 2;
  1230. bsdtar_warnc(bsdtar, 0,
  1231. "Data on server was modified, but it might not"
  1232. "be exactly what you requested");
  1233. }
  1234. }
  1235. return (bsdtar->return_value);
  1236. }
  1237. static void
  1238. set_mode(struct bsdtar * bsdtar, int opt, const char *optstr)
  1239. {
  1240. /* Make sure we're not asking tarsnap to do two things at once. */
  1241. if (bsdtar->mode != 0)
  1242. bsdtar_errc(bsdtar, 1, 0,
  1243. "Can't specify both %s and %s", optstr, bsdtar->modestr);
  1244. /* Set mode. */
  1245. bsdtar->mode = opt;
  1246. bsdtar->modestr = optstr;
  1247. }
  1248. /*
  1249. * Verify that the mode is correct.
  1250. */
  1251. static void
  1252. only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
  1253. {
  1254. if (strchr(valid_modes, bsdtar->mode) == NULL)
  1255. bsdtar_errc(bsdtar, 1, 0,
  1256. "Option %s is not permitted in mode %s",
  1257. opt, bsdtar->modestr);
  1258. }
  1259. void
  1260. usage(struct bsdtar *bsdtar)
  1261. {
  1262. const char *p;
  1263. p = bsdtar->progname;
  1264. fprintf(stderr, "Usage:\n");
  1265. fprintf(stderr, " List: %s [options...] -tf <archive>\n", p);
  1266. fprintf(stderr, " Extract: %s [options...] -xf <archive>\n", p);
  1267. fprintf(stderr, " Create: %s [options...] -cf <archive>"
  1268. " [filenames...]\n", p);
  1269. fprintf(stderr, " Delete: %s [options...] -df <archive>\n", p);
  1270. fprintf(stderr, " Tar output: %s [options...] -rf <archive>\n", p);
  1271. fprintf(stderr, " List archives: %s [options...] --list-archives\n", p);
  1272. fprintf(stderr, " Print stats: %s [options...] --print-stats\n", p);
  1273. fprintf(stderr, " Help: %s --help\n", p);
  1274. exit(1);
  1275. }
  1276. static void
  1277. version(void)
  1278. {
  1279. fprintf(stdout, "tarsnap %s\n", PACKAGE_VERSION);
  1280. exit(0);
  1281. }
  1282. static const char *long_help_msg =
  1283. "First option must be a mode specifier:\n"
  1284. " -c Create -d Delete -r Output as tar file -t List -x Extract\n"
  1285. " --list-archives List archives --print-stats Print archive statistics\n"
  1286. "Common Options:\n"
  1287. " -f <archive> Archive name\n"
  1288. " --keyfile <file> Key file\n"
  1289. " --cachedir <directory> Cache directory\n"
  1290. " -v Verbose\n"
  1291. " -w Interactive\n"
  1292. "Create: %p -c [options] [<file> | <dir> | @@<archive> | -C <dir>] ...\n"
  1293. " <file>, <dir> add these items to archive\n"
  1294. " --exclude <pattern> Skip files that match pattern\n"
  1295. " -C <dir> Change to <dir> before processing remaining files\n"
  1296. " @@<archive> Add entries from tarsnap archive <archive>\n"
  1297. "List: %p -t [options] [<patterns>]\n"
  1298. " <patterns> If specified, list only entries that match\n"
  1299. "Extract: %p -x [options] [<patterns>]\n"
  1300. " <patterns> If specified, extract only entries that match\n"
  1301. " -k Keep (don't overwrite) existing files\n"
  1302. " -m Don't restore modification times\n"
  1303. " -O Write entries to stdout, don't restore to disk\n"
  1304. " -p Restore permissions (including ACLs, owner, file flags)\n";
  1305. static void
  1306. long_help(struct bsdtar *bsdtar)
  1307. {
  1308. const char *prog;
  1309. const char *p;
  1310. prog = bsdtar->progname;
  1311. fflush(stderr);
  1312. p = (strcmp(prog, "tarsnap") != 0) ? "(tarsnap)" : "";
  1313. fprintf(stdout, "%s%s: create and manipulate archives on the "
  1314. "Tarsnap backup service\n", prog, p);
  1315. for (p = long_help_msg; *p != '\0'; p++) {
  1316. if (*p == '%') {
  1317. if (p[1] == 'p') {
  1318. fputs(prog, stdout);
  1319. p++;
  1320. } else
  1321. putchar('%');
  1322. } else
  1323. putchar(*p);
  1324. }
  1325. version();
  1326. }
  1327. /* Process options from the specified file, if it exists. */
  1328. static void
  1329. configfile(struct bsdtar *bsdtar, const char *fname, int fromcmdline)
  1330. {
  1331. struct stat sb;
  1332. /* Print config filename if given --dump-config. */
  1333. if (bsdtar->option_dump_config)
  1334. fprintf(stderr, "Reading from config file: %s\n", fname);
  1335. /*
  1336. * If we had --no-config-exclude (or --no-config-include) earlier,
  1337. * we do not want to process any --exclude (or --include) options
  1338. * from now onwards.
  1339. */
  1340. bsdtar->option_no_config_exclude_set =
  1341. bsdtar->option_no_config_exclude;
  1342. bsdtar->option_no_config_include_set =
  1343. bsdtar->option_no_config_include;
  1344. if (stat(fname, &sb)) {
  1345. /* Missing file. */
  1346. if (errno == ENOENT) {
  1347. if (fromcmdline) {
  1348. bsdtar_errc(bsdtar, 1, errno,
  1349. "Cannot read config file: %s", fname);
  1350. } else {
  1351. /*
  1352. * If the file wasn't specified on the
  1353. * command-line, do nothing.
  1354. */
  1355. return;
  1356. }
  1357. }
  1358. /*
  1359. * Something bad happened. Note that this could occur if
  1360. * there is no configuration file and part of the path to
  1361. * where we're looking for a configuration file exists and
  1362. * is a non-directory (e.g., if /usr/local/etc is a file);
  1363. * we're going to error out if this happens, since reporting
  1364. * a spurious error in such an odd circumstance is better
  1365. * than failing to report an error if there really is a
  1366. * configuration file.
  1367. */
  1368. bsdtar_errc(bsdtar, 1, errno, "stat(%s)", fname);
  1369. }
  1370. /* Process the file. */
  1371. process_lines(bsdtar, fname, configfile_helper, 0);
  1372. }
  1373. /* Process a line of configuration file. */
  1374. static int
  1375. configfile_helper(struct bsdtar *bsdtar, const char *line)
  1376. {
  1377. char * conf_arg;
  1378. size_t optlen;
  1379. size_t len;
  1380. /* Skip any leading whitespace. */
  1381. while ((line[0] == ' ') || (line[0] == '\t'))
  1382. line++;
  1383. /* Ignore comments and blank lines. */
  1384. if ((line[0] == '#') || (line[0] == '\0'))
  1385. return (0);
  1386. /* Print line if given --dump-config. */
  1387. if (bsdtar->option_dump_config)
  1388. fprintf(stderr, " %s\n", line);
  1389. /* Duplicate line. */
  1390. if ((bsdtar->conf_opt = strdup(line)) == NULL)
  1391. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1392. /*
  1393. * Detect any trailing whitespace. This could happen before string
  1394. * duplication, but to reduce the number of diffs to a later version,
  1395. * we'll do it here.
  1396. */
  1397. len = strlen(bsdtar->conf_opt);
  1398. if ((len > 0) &&
  1399. ((bsdtar->conf_opt[len - 1] == ' ') ||
  1400. (bsdtar->conf_opt[len - 1] == '\t'))) {
  1401. bsdtar_warnc(bsdtar, 0,
  1402. "option contains trailing whitespace; future behaviour"
  1403. " may change for:\n %s", line);
  1404. }
  1405. /* Split line into option and argument if possible. */
  1406. optlen = strcspn(bsdtar->conf_opt, " \t");
  1407. /* Is there an argument? */
  1408. if (bsdtar->conf_opt[optlen]) {
  1409. /* NUL-terminate the option name. */
  1410. bsdtar->conf_opt[optlen] = '\0';
  1411. /* Find the start of the argument. */
  1412. conf_arg = bsdtar->conf_opt + optlen + 1;
  1413. conf_arg += strspn(conf_arg, " \t");
  1414. /*
  1415. * If the line is whitespace-terminated, there might not be
  1416. * an argument here after all.
  1417. */
  1418. if (conf_arg[0] == '\0')
  1419. conf_arg = NULL;
  1420. } else {
  1421. /* No argument. */
  1422. conf_arg = NULL;
  1423. }
  1424. /*
  1425. * If we have an argument which starts with ~, and the password
  1426. * database lists a home directory for the user, expand ~ to that
  1427. * value.
  1428. */
  1429. if ((conf_arg != NULL) && (conf_arg[0] == '~') &&
  1430. (bsdtar->homedir != NULL)) {
  1431. /* Construct expanded argument string. */
  1432. if (asprintf(&bsdtar->conf_arg, "%s%s",
  1433. bsdtar->homedir, &conf_arg[1]) == -1)
  1434. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1435. /* Use the expanded argument string hereafter. */
  1436. conf_arg = bsdtar->conf_arg;
  1437. } else {
  1438. bsdtar->conf_arg = NULL;
  1439. }
  1440. /* Process the configuration option. */
  1441. dooption(bsdtar, bsdtar->conf_opt, conf_arg, 1);
  1442. /* Free expanded argument or NULL. */
  1443. free(bsdtar->conf_arg);
  1444. bsdtar->conf_arg = NULL;
  1445. /* Free memory allocated by strdup. */
  1446. free(bsdtar->conf_opt);
  1447. bsdtar->conf_opt = NULL;
  1448. return (0);
  1449. }
  1450. /* Process a line of configuration file. */
  1451. static int
  1452. archive_names_helper(struct bsdtar *bsdtar, const char *line)
  1453. {
  1454. char * name;
  1455. /* Ignore blank lines. */
  1456. if (line[0] == '\0')
  1457. return (0);
  1458. /* Duplicate line. */
  1459. if ((name = strdup(line)) == NULL)
  1460. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1461. /* Record archive name. */
  1462. if (strlist_append(bsdtar->tapenames_setup, &name, 1))
  1463. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1464. /* Success! */
  1465. return (0);
  1466. }
  1467. /* Add a command-line option to the delayed options queue. */
  1468. static void
  1469. optq_push(struct bsdtar *bsdtar, const char * opt_name, const char * opt_arg)
  1470. {
  1471. struct delayedopt * opt;
  1472. /* Create a delayed option structure. */
  1473. if ((opt = malloc(sizeof(struct delayedopt))) == NULL)
  1474. goto enomem;
  1475. if ((opt->opt_name = strdup(opt_name)) == NULL)
  1476. goto enomem;
  1477. if (opt_arg == NULL)
  1478. opt->opt_arg = NULL;
  1479. else if ((opt->opt_arg = strdup(opt_arg)) == NULL)
  1480. goto enomem;
  1481. opt->next = NULL;
  1482. /* Add to queue. */
  1483. *(bsdtar->delopt_tail) = opt;
  1484. bsdtar->delopt_tail = &opt->next;
  1485. /* Success! */
  1486. return;
  1487. enomem:
  1488. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1489. }
  1490. /* Remove the first item from the delayed options queue. */
  1491. static void
  1492. optq_pop(struct bsdtar *bsdtar)
  1493. {
  1494. struct delayedopt * opt = bsdtar->delopt;
  1495. /* Remove from linked list. */
  1496. bsdtar->delopt = opt->next;
  1497. /* Free item. */
  1498. free(opt->opt_name);
  1499. free(opt->opt_arg);
  1500. free(opt);
  1501. }
  1502. /* Process a line of configuration file or a command-line option. */
  1503. static void
  1504. dooption(struct bsdtar *bsdtar, const char * conf_opt,
  1505. const char * conf_arg, int fromconffile)
  1506. {
  1507. struct stat st;
  1508. const char * str;
  1509. char *eptr;
  1510. if (strcmp(conf_opt, "aggressive-networking") == 0) {
  1511. if (bsdtar->mode != 'c')
  1512. goto badmode;
  1513. if (bsdtar->option_aggressive_networking_set)
  1514. goto optset;
  1515. tarsnap_opt_aggressive_networking = 1;
  1516. bsdtar->option_aggressive_networking_set = 1;
  1517. } else if (strcmp(conf_opt, "cachedir") == 0) {
  1518. if (bsdtar->cachedir != NULL)
  1519. goto optset;
  1520. if (conf_arg == NULL)
  1521. goto needarg;
  1522. if ((bsdtar->cachedir = strdup(conf_arg)) == NULL)
  1523. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1524. } else if (strcmp(conf_opt, "checkpoint-bytes") == 0) {
  1525. if (bsdtar->mode != 'c')
  1526. goto badmode;
  1527. if (tarsnap_opt_checkpointbytes != (uint64_t)(-1))
  1528. goto optset;
  1529. if (conf_arg == NULL)
  1530. goto needarg;
  1531. if (humansize_parse(conf_arg, &tarsnap_opt_checkpointbytes))
  1532. bsdtar_errc(bsdtar, 1, 0,
  1533. "Cannot parse #bytes per checkpoint: %s",
  1534. conf_arg);
  1535. if (tarsnap_opt_checkpointbytes < 1000000)
  1536. bsdtar_errc(bsdtar, 1, 0,
  1537. "checkpoint-bytes value must be at least 1M");
  1538. } else if (strcmp(conf_opt, "disk-pause") == 0) {
  1539. if (bsdtar->mode != 'c')
  1540. goto badmode;
  1541. if (bsdtar->option_disk_pause_set)
  1542. goto optset;
  1543. if (conf_arg == NULL)
  1544. goto needarg;
  1545. bsdtar->disk_pause = strtol(conf_arg, NULL, 0);
  1546. if (bsdtar->disk_pause > 1000)
  1547. bsdtar_errc(bsdtar, 1, 0,
  1548. "disk-pause value must be <= 1000");
  1549. if (bsdtar->disk_pause < 0)
  1550. bsdtar_errc(bsdtar, 1, 0,
  1551. "disk-pause value must be >= 0");
  1552. bsdtar->option_disk_pause_set = 1;
  1553. } else if (strcmp(conf_opt, "exclude") == 0) {
  1554. if (bsdtar->option_no_config_exclude_set)
  1555. goto optset;
  1556. if (conf_arg == NULL)
  1557. goto needarg;
  1558. if (exclude(bsdtar, conf_arg))
  1559. bsdtar_errc(bsdtar, 1, 0,
  1560. "Couldn't exclude %s", conf_arg);
  1561. } else if (strcmp(conf_opt, "force-resources") == 0) {
  1562. if (bsdtar->option_force_resources_set)
  1563. goto optset;
  1564. bsdtar->option_force_resources = 1;
  1565. bsdtar->option_force_resources_set = 1;
  1566. } else if (strcmp(conf_opt, "humanize-numbers") == 0) {
  1567. if (bsdtar->option_humanize_numbers_set)
  1568. goto optset;
  1569. tarsnap_opt_humanize_numbers = 1;
  1570. bsdtar->option_humanize_numbers_set = 1;
  1571. } else if (strcmp(conf_opt, "include") == 0) {
  1572. if (bsdtar->option_no_config_include_set)
  1573. goto optset;
  1574. if (conf_arg == NULL)
  1575. goto needarg;
  1576. if (include(bsdtar, conf_arg))
  1577. bsdtar_errc(bsdtar, 1, 0,
  1578. "Failed to add %s to inclusion list", conf_arg);
  1579. } else if (strcmp(conf_opt, "insane-filesystems") == 0) {
  1580. if (bsdtar->option_insane_filesystems_set)
  1581. goto optset;
  1582. bsdtar->option_insane_filesystems = 1;
  1583. bsdtar->option_insane_filesystems_set = 1;
  1584. } else if (strcmp(conf_opt, "iso-dates") == 0) {
  1585. if (bsdtar->option_iso_dates_set)
  1586. goto optset;
  1587. bsdtar->option_iso_dates = 1;
  1588. bsdtar->option_iso_dates_set = 1;
  1589. } else if (strcmp(conf_opt, "keyfile") == 0) {
  1590. if (bsdtar->keyfile != NULL)
  1591. goto optset;
  1592. if (conf_arg == NULL)
  1593. goto needarg;
  1594. if ((bsdtar->keyfile = strdup(conf_arg)) == NULL)
  1595. bsdtar_errc(bsdtar, 1, errno, "Out of memory");
  1596. bsdtar->keyfile_from_config = fromconffile;
  1597. } else if (strcmp(conf_opt, "lowmem") == 0) {
  1598. if (bsdtar->mode != 'c')
  1599. goto badmode;
  1600. if (bsdtar->option_cachecrunch_set)
  1601. goto optset;
  1602. bsdtar->cachecrunch = 1;
  1603. bsdtar->option_cachecrunch_set = 1;
  1604. } else if (strcmp(conf_opt, "maxbw") == 0) {
  1605. if (bsdtar->mode != 'c')
  1606. goto badmode;
  1607. if (bsdtar->option_maxbw_set)
  1608. goto optset;
  1609. if (conf_arg == NULL)
  1610. goto needarg;
  1611. if (humansize_parse(conf_arg, &tarsnap_opt_maxbytesout))
  1612. bsdtar_errc(bsdtar, 1, 0,
  1613. "Cannot parse bandwidth limit: %s", conf_arg);
  1614. bsdtar->option_maxbw_set = 1;
  1615. } else if (strcmp(conf_opt, "maxbw-rate") == 0) {
  1616. dooption(bsdtar, "maxbw-rate-down", conf_arg, fromconffile);
  1617. dooption(bsdtar, "maxbw-rate-up", conf_arg, fromconffile);
  1618. } else if (strcmp(conf_opt, "maxbw-rate-down") == 0) {
  1619. if (bsdtar->option_maxbw_rate_down_set)
  1620. goto optset;
  1621. if (conf_arg == NULL)
  1622. goto needarg;
  1623. bsdtar->bwlimit_rate_down = strtod(conf_arg, &eptr);
  1624. if ((*eptr != '\0') ||
  1625. (bsdtar->bwlimit_rate_down < 8000) ||
  1626. (bsdtar->bwlimit_rate_down > 1000000000.))
  1627. bsdtar_errc(bsdtar, 1, 0,
  1628. "Invalid bandwidth rate limit: %s", conf_arg);
  1629. bsdtar->option_maxbw_rate_down_set = 1;
  1630. } else if (strcmp(conf_opt, "maxbw-rate-up") == 0) {
  1631. if (bsdtar->option_maxbw_rate_up_set)
  1632. goto optset;
  1633. if (conf_arg == NULL)
  1634. goto needarg;
  1635. bsdtar->bwlimit_rate_up = strtod(conf_arg, &eptr);
  1636. if ((*eptr != '\0') ||
  1637. (bsdtar->bwlimit_rate_up < 8000) ||
  1638. (bsdtar->bwlimit_rate_up > 1000000000.))
  1639. bsdtar_errc(bsdtar, 1, 0,
  1640. "Invalid bandwidth rate limit: %s", conf_arg);
  1641. bsdtar->option_maxbw_rate_up_set = 1;
  1642. } else if (strcmp(conf_opt, "nodump") == 0) {
  1643. if (bsdtar->mode != 'c')
  1644. goto badmode;
  1645. if (bsdtar->option_nodump_set)
  1646. goto optset;
  1647. bsdtar->option_honor_nodump = 1;
  1648. bsdtar->option_nodump_set = 1;
  1649. } else if (strcmp(conf_opt, "normalmem") == 0) {
  1650. if (bsdtar->mode != 'c')
  1651. goto badmode;
  1652. if (bsdtar->option_cachecrunch_set)
  1653. goto optset;
  1654. bsdtar->option_cachecrunch_set = 1;
  1655. } else if (strcmp(conf_opt, "no-aggressive-networking") == 0) {
  1656. if (bsdtar->option_aggressive_networking_set)
  1657. goto optset;
  1658. bsdtar->option_aggressive_networking_set = 1;
  1659. } else if (strcmp(conf_opt, "no-config-exclude") == 0) {
  1660. if (bsdtar->option_no_config_exclude)
  1661. goto optset;
  1662. bsdtar->option_no_config_exclude = 1;
  1663. } else if (strcmp(conf_opt, "no-config-include") == 0) {
  1664. if (bsdtar->option_no_config_include)
  1665. goto optset;
  1666. bsdtar->option_no_config_include = 1;
  1667. } else if (strcmp(conf_opt, "no-disk-pause") == 0) {
  1668. if (bsdtar->option_disk_pause_set)
  1669. goto optset;
  1670. bsdtar->option_disk_pause_set = 1;
  1671. } else if (strcmp(conf_opt, "no-force-resources") == 0) {
  1672. if (bsdtar->option_force_resources_set)
  1673. goto optset;
  1674. bsdtar->option_force_resources_set = 1;
  1675. } else if (strcmp(conf_opt, "no-humanize-numbers") == 0) {
  1676. if (bsdtar->option_humanize_numbers_set)
  1677. goto optset;
  1678. bsdtar->option_humanize_numbers_set = 1;
  1679. } else if (strcmp(conf_opt, "no-insane-filesystems") == 0) {
  1680. if (bsdtar->option_insane_filesystems_set)
  1681. goto optset;
  1682. bsdtar->option_insane_filesystems_set = 1;
  1683. } else if (strcmp(conf_opt, "no-iso-dates") == 0) {
  1684. if (bsdtar->option_iso_dates_set)
  1685. goto optset;
  1686. bsdtar->option_iso_dates_set = 1;
  1687. } else if (strcmp(conf_opt, "no-maxbw") == 0) {
  1688. if (bsdtar->option_maxbw_set)
  1689. goto optset;
  1690. bsdtar->option_maxbw_set = 1;
  1691. } else if (strcmp(conf_opt, "no-maxbw-rate-down") == 0) {
  1692. if (bsdtar->option_maxbw_rate_down_set)
  1693. goto optset;
  1694. bsdtar->option_maxbw_rate_down_set = 1;
  1695. } else if (strcmp(conf_opt, "no-maxbw-rate-up") == 0) {
  1696. if (bsdtar->option_maxbw_rate_up_set)
  1697. goto optset;
  1698. bsdtar->option_maxbw_rate_up_set = 1;
  1699. } else if (strcmp(conf_opt, "no-nodump") == 0) {
  1700. if (bsdtar->option_nodump_set)
  1701. goto optset;
  1702. bsdtar->option_nodump_set = 1;
  1703. } else if (strcmp(conf_opt, "no-print-stats") == 0) {
  1704. if (bsdtar->option_print_stats_set)
  1705. goto optset;
  1706. bsdtar->option_print_stats_set = 1;
  1707. } else if (strcmp(conf_opt, "no-progress-bytes") == 0) {
  1708. if (bsdtar->option_progress_bytes_set)
  1709. goto optset;
  1710. bsdtar->option_progress_bytes_set = 1;
  1711. } else if (strcmp(conf_opt, "no-quiet") == 0) {
  1712. if (bsdtar->option_quiet_set)
  1713. goto optset;
  1714. bsdtar->option_quiet_set = 1;
  1715. } else if (strcmp(conf_opt, "no-retry-forever") == 0) {
  1716. if (bsdtar->option_retry_forever_set)
  1717. goto optset;
  1718. bsdtar->option_retry_forever_set = 1;
  1719. } else if (strcmp(conf_opt, "no-snaptime") == 0) {
  1720. if (bsdtar->option_snaptime_set)
  1721. goto optset;
  1722. bsdtar->option_snaptime_set = 1;
  1723. } else if (strcmp(conf_opt, "no-store-atime") == 0) {
  1724. if (bsdtar->option_store_atime_set)
  1725. goto optset;
  1726. bsdtar->option_store_atime_set = 1;
  1727. } else if (strcmp(conf_opt, "no-totals") == 0) {
  1728. if (bsdtar->option_totals_set)
  1729. goto optset;
  1730. bsdtar->option_totals_set = 1;
  1731. } else if (strcmp(conf_opt, "passphrase") == 0) {
  1732. if (bsdtar->option_passphrase_entry != PASSPHRASE_UNSET)
  1733. goto optset;
  1734. if (passphrase_entry_parse(conf_arg,
  1735. &bsdtar->option_passphrase_entry, &str))
  1736. bsdtar_errc(bsdtar, 1, 0, "Cannot parse passphrase"
  1737. "entry method: %s", conf_arg);
  1738. if ((bsdtar->option_passphrase_arg = strdup(str)) == NULL)
  1739. bsdtar_errc(bsdtar, 1, ENOMEM,
  1740. "Cannot allocate memory");
  1741. } else if (strcmp(conf_opt, "print-stats") == 0) {
  1742. if ((bsdtar->mode != 'c') && (bsdtar->mode != 'd'))
  1743. goto badmode;
  1744. if (bsdtar->option_print_stats_set)
  1745. goto optset;
  1746. bsdtar->option_print_stats = 1;
  1747. bsdtar->option_print_stats_set = 1;
  1748. } else if (strcmp(conf_opt, "progress-bytes") == 0) {
  1749. if (!((bsdtar->mode == 'c') || (bsdtar->mode == 'x')))
  1750. goto badmode;
  1751. if (bsdtar->option_progress_bytes_set)
  1752. goto optset;
  1753. if (conf_arg == NULL)
  1754. goto needarg;
  1755. if (humansize_parse(conf_arg, &bsdtar->option_progress_bytes))
  1756. bsdtar_errc(bsdtar, 1, 0, "Cannot parse #bytes per"
  1757. " progress message: %s", conf_arg);
  1758. if (bsdtar->option_progress_bytes < 1000)
  1759. bsdtar_errc(bsdtar, 1, 0, "progress-bytes value"
  1760. " must be at least 1000");
  1761. bsdtar->option_progress_bytes_set = 1;
  1762. } else if (strcmp(conf_opt, "quiet") == 0) {
  1763. if (bsdtar->option_quiet_set)
  1764. goto optset;
  1765. bsdtar->option_quiet = 1;
  1766. bsdtar->option_quiet_set = 1;
  1767. } else if (strcmp(conf_opt, "retry-forever") == 0) {
  1768. if (bsdtar->option_retry_forever_set)
  1769. goto optset;
  1770. tarsnap_opt_retry_forever = 1;
  1771. bsdtar->option_retry_forever_set = 1;
  1772. } else if (strcmp(conf_opt, "snaptime") == 0) {
  1773. if (bsdtar->mode != 'c')
  1774. goto badmode;
  1775. if (bsdtar->option_snaptime_set)
  1776. goto optset;
  1777. if (conf_arg == NULL)
  1778. goto needarg;
  1779. if (stat(conf_arg, &st) != 0)
  1780. bsdtar_errc(bsdtar, 1, 0,
  1781. "Can't stat file %s", conf_arg);
  1782. bsdtar->snaptime = st.st_ctime;
  1783. bsdtar->option_snaptime_set = 1;
  1784. } else if (strcmp(conf_opt, "store-atime") == 0) {
  1785. if (bsdtar->mode != 'c')
  1786. goto badmode;
  1787. if (bsdtar->option_store_atime_set)
  1788. goto optset;
  1789. bsdtar->option_store_atime = 1;
  1790. bsdtar->option_store_atime_set = 1;
  1791. } else if (strcmp(conf_opt, "totals") == 0) {
  1792. if (bsdtar->mode != 'c')
  1793. goto badmode;
  1794. if (bsdtar->option_totals_set)
  1795. goto optset;
  1796. bsdtar->option_totals = 1;
  1797. bsdtar->option_totals_set = 1;
  1798. } else if (strcmp(conf_opt, "verylowmem") == 0) {
  1799. if (bsdtar->mode != 'c')
  1800. goto badmode;
  1801. if (bsdtar->option_cachecrunch_set)
  1802. goto optset;
  1803. bsdtar->cachecrunch = 2;
  1804. bsdtar->option_cachecrunch_set = 1;
  1805. } else {
  1806. goto badopt;
  1807. }
  1808. return;
  1809. badmode:
  1810. /* Option not relevant in this mode. */
  1811. if (fromconffile == 0) {
  1812. bsdtar_errc(bsdtar, 1, 0,
  1813. "Option --%s is not permitted in mode %s",
  1814. conf_opt, bsdtar->modestr);
  1815. }
  1816. return;
  1817. optset:
  1818. /* Option specified multiple times. */
  1819. if (fromconffile == 0) {
  1820. usage(bsdtar);
  1821. }
  1822. return;
  1823. needarg:
  1824. /* Option needs an argument. */
  1825. bsdtar_errc(bsdtar, 1, 0,
  1826. "Argument required for configuration file option: %s", conf_opt);
  1827. badopt:
  1828. /* No such option. */
  1829. bsdtar_errc(bsdtar, 1, 0,
  1830. "Unrecognized configuration file option: \"%s\"", conf_opt);
  1831. }
  1832. /* Load keys from the specified file. Return success or failure. */
  1833. static int
  1834. load_keys(struct bsdtar *bsdtar, const char *path)
  1835. {
  1836. uint64_t machinenum;
  1837. /* Set passphrase entry method (if unset). */
  1838. if (bsdtar->option_passphrase_entry == PASSPHRASE_UNSET)
  1839. bsdtar->option_passphrase_entry = PASSPHRASE_TTY_STDIN;
  1840. /* Load the key file. */
  1841. if (keyfile_read(path, &machinenum, ~0, bsdtar->option_force_resources,
  1842. bsdtar->option_passphrase_entry, bsdtar->option_passphrase_arg))
  1843. goto err0;
  1844. /* Check the machine number. */
  1845. if ((bsdtar->machinenum != (uint64_t)(-1)) &&
  1846. (machinenum != bsdtar->machinenum))
  1847. bsdtar_errc(bsdtar, 1, 0,
  1848. "Key file belongs to wrong machine: %s", path);
  1849. bsdtar->machinenum = machinenum;
  1850. /* Success! */
  1851. return (0);
  1852. err0:
  1853. /* Failure! */
  1854. return (-1);
  1855. }
  1856. static int
  1857. argv_has_archive_directive(struct bsdtar *bsdtar)
  1858. {
  1859. int i;
  1860. const char *arg;
  1861. /* Find "@@*", but don't trigger on "-C @@foo". */
  1862. for (i = 0; i < bsdtar->argc; i++) {
  1863. /* Improves code legibility. */
  1864. arg = bsdtar->argv[i];
  1865. /* Detect "-C" by itself. */
  1866. if ((arg[0] == '-') && (arg[1] == 'C') && (arg[2] == '\0')) {
  1867. i++;
  1868. continue;
  1869. }
  1870. /* Detect any remaining "@@*". */
  1871. if ((arg[0] == '@') && (arg[1] == '@')) {
  1872. return (1);
  1873. }
  1874. }
  1875. return (0);
  1876. }