bsdtar.c 57 KB

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