bsdtar.c 53 KB

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