bsdtar.c 59 KB

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