bsdtar.c 61 KB

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