Don't include the checksum_seed in the full-file MD5 checksum.
[rsync/rsync.git] / options.c
... / ...
CommitLineData
1/*
2 * Command-line (and received via daemon-socket) option parsing.
3 *
4 * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2000, 2001, 2002 Martin Pool <mbp@samba.org>
6 * Copyright (C) 2002-2007 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23#include <popt.h>
24#include "zlib/zlib.h"
25
26extern int module_id;
27extern int sanitize_paths;
28extern int daemon_over_rsh;
29extern struct filter_list_struct filter_list;
30extern struct filter_list_struct server_filter_list;
31
32int make_backups = 0;
33
34/**
35 * If 1, send the whole file as literal data rather than trying to
36 * create an incremental diff.
37 *
38 * If -1, then look at whether we're local or remote and go by that.
39 *
40 * @sa disable_deltas_p()
41 **/
42int whole_file = -1;
43
44int append_mode = 0;
45int keep_dirlinks = 0;
46int copy_dirlinks = 0;
47int copy_links = 0;
48int preserve_links = 0;
49int preserve_hard_links = 0;
50int preserve_acls = 0;
51int preserve_xattrs = 0;
52int preserve_perms = 0;
53int preserve_executability = 0;
54int preserve_devices = 0;
55int preserve_specials = 0;
56int preserve_uid = 0;
57int preserve_gid = 0;
58int preserve_times = 0;
59int update_only = 0;
60int cvs_exclude = 0;
61int dry_run = 0;
62int do_xfers = 1;
63int ignore_times = 0;
64int delete_mode = 0;
65int delete_during = 0;
66int delete_before = 0;
67int delete_after = 0;
68int delete_excluded = 0;
69int remove_source_files = 0;
70int one_file_system = 0;
71int protocol_version = PROTOCOL_VERSION;
72int sparse_files = 0;
73int do_compression = 0;
74int def_compress_level = Z_DEFAULT_COMPRESSION;
75int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */
76int am_server = 0;
77int am_sender = 0;
78int am_generator = 0;
79int am_starting_up = 1;
80int relative_paths = -1;
81int implied_dirs = 1;
82int numeric_ids = 0;
83int allow_8bit_chars = 0;
84int force_delete = 0;
85int io_timeout = 0;
86int allowed_lull = 0;
87int prune_empty_dirs = 0;
88int use_qsort = 0;
89char *files_from = NULL;
90int filesfrom_fd = -1;
91char *filesfrom_host = NULL;
92int eol_nulls = 0;
93int human_readable = 0;
94int recurse = 0;
95int allow_inc_recurse = 1;
96int xfer_dirs = -1;
97int am_daemon = 0;
98int do_stats = 0;
99int do_progress = 0;
100int keep_partial = 0;
101int safe_symlinks = 0;
102int copy_unsafe_links = 0;
103int size_only = 0;
104int daemon_bwlimit = 0;
105int bwlimit = 0;
106int fuzzy_basis = 0;
107size_t bwlimit_writemax = 0;
108int ignore_existing = 0;
109int ignore_non_existing = 0;
110int need_messages_from_generator = 0;
111int max_delete = INT_MIN;
112OFF_T max_size = 0;
113OFF_T min_size = 0;
114int ignore_errors = 0;
115int modify_window = 0;
116int blocking_io = -1;
117int checksum_seed = 0;
118int inplace = 0;
119int delay_updates = 0;
120long block_size = 0; /* "long" because popt can't set an int32. */
121char *skip_compress = NULL;
122
123/** Network address family. **/
124int default_af_hint
125#ifdef INET6
126 = 0; /* Any protocol */
127#else
128 = AF_INET; /* Must use IPv4 */
129# ifdef AF_INET6
130# undef AF_INET6
131# endif
132# define AF_INET6 AF_INET /* make -6 option a no-op */
133#endif
134
135/** Do not go into the background when run as --daemon. Good
136 * for debugging and required for running as a service on W32,
137 * or under Unix process-monitors. **/
138int no_detach
139#if defined _WIN32 || defined __WIN32__
140 = 1;
141#else
142 = 0;
143#endif
144
145int write_batch = 0;
146int read_batch = 0;
147int backup_dir_len = 0;
148int backup_suffix_len;
149unsigned int backup_dir_remainder;
150
151char *backup_suffix = NULL;
152char *tmpdir = NULL;
153char *partial_dir = NULL;
154char *basis_dir[MAX_BASIS_DIRS+1];
155char *config_file = NULL;
156char *shell_cmd = NULL;
157char *logfile_name = NULL;
158char *logfile_format = NULL;
159char *stdout_format = NULL;
160char *password_file = NULL;
161char *rsync_path = RSYNC_PATH;
162char *backup_dir = NULL;
163char backup_dir_buf[MAXPATHLEN];
164char *sockopts = NULL;
165int rsync_port = 0;
166int compare_dest = 0;
167int copy_dest = 0;
168int link_dest = 0;
169int basis_dir_cnt = 0;
170char *dest_option = NULL;
171
172int verbose = 0;
173int quiet = 0;
174int output_motd = 1;
175int log_before_transfer = 0;
176int stdout_format_has_i = 0;
177int stdout_format_has_o_or_i = 0;
178int logfile_format_has_i = 0;
179int logfile_format_has_o_or_i = 0;
180int always_checksum = 0;
181int list_only = 0;
182
183#define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
184char *batch_name = NULL;
185
186#ifdef ICONV_OPTION
187int need_unsorted_flist = 0;
188char *iconv_opt = ICONV_OPTION;
189#endif
190
191struct chmod_mode_struct *chmod_modes = NULL;
192
193static int daemon_opt; /* sets am_daemon after option error-reporting */
194static int omit_dir_times = 0;
195static int F_option_cnt = 0;
196static int modify_window_set;
197static int itemize_changes = 0;
198static int refused_delete, refused_archive_part, refused_compress;
199static int refused_partial, refused_progress, refused_delete_before;
200static int refused_delete_during;
201static int refused_inplace;
202static char *max_size_arg, *min_size_arg;
203static char tmp_partialdir[] = ".~tmp~";
204
205/** Local address to bind. As a character string because it's
206 * interpreted by the IPv6 layer: should be a numeric IP4 or IP6
207 * address, or a hostname. **/
208char *bind_address;
209
210
211static void print_rsync_version(enum logcode f)
212{
213 char *subprotocol = "";
214 char const *got_socketpair = "no ";
215 char const *have_inplace = "no ";
216 char const *hardlinks = "no ";
217 char const *acls = "no ";
218 char const *xattrs = "no ";
219 char const *links = "no ";
220 char const *iconv = "no ";
221 char const *ipv6 = "no ";
222 STRUCT_STAT *dumstat;
223
224#if SUBPROTOCOL_VERSION != 0
225 asprintf(&subprotocol, ".PR%d", SUBPROTOCOL_VERSION);
226#endif
227#ifdef HAVE_SOCKETPAIR
228 got_socketpair = "";
229#endif
230#ifdef HAVE_FTRUNCATE
231 have_inplace = "";
232#endif
233#ifdef SUPPORT_HARD_LINKS
234 hardlinks = "";
235#endif
236#ifdef SUPPORT_ACLS
237 acls = "";
238#endif
239#ifdef SUPPORT_XATTRS
240 xattrs = "";
241#endif
242#ifdef SUPPORT_LINKS
243 links = "";
244#endif
245#ifdef INET6
246 ipv6 = "";
247#endif
248#ifdef ICONV_OPTION
249 iconv = "";
250#endif
251
252 rprintf(f, "%s version %s protocol version %d%s\n",
253 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
254 rprintf(f, "Copyright (C) 1996-2007 by Andrew Tridgell, Wayne Davison, and others.\n");
255 rprintf(f, "Web site: http://rsync.samba.org/\n");
256 rprintf(f, "Capabilities:\n");
257 rprintf(f, " %d-bit files, %d-bit inums, %d-bit timestamps, %d-bit long ints,\n",
258 (int)(sizeof (OFF_T) * 8),
259 (int)(sizeof dumstat->st_ino * 8), /* Don't check ino_t! */
260 (int)(sizeof (time_t) * 8),
261 (int)(sizeof (int64) * 8));
262 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
263 got_socketpair, hardlinks, links, ipv6, have_inplace);
264 rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv\n",
265 have_inplace, acls, xattrs, iconv);
266
267#ifdef MAINTAINER_MODE
268 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
269#endif
270
271#if SIZEOF_INT64 < 8
272 rprintf(f, "WARNING: no 64-bit integers on this platform!\n");
273#endif
274 if (sizeof (int64) != SIZEOF_INT64) {
275 rprintf(f,
276 "WARNING: size mismatch in SIZEOF_INT64 define (%d != %d)\n",
277 (int) SIZEOF_INT64, (int) sizeof (int64));
278 }
279
280 rprintf(f,"\n");
281 rprintf(f,"rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n");
282 rprintf(f,"are welcome to redistribute it under certain conditions. See the GNU\n");
283 rprintf(f,"General Public Licence for details.\n");
284}
285
286
287void usage(enum logcode F)
288{
289 print_rsync_version(F);
290
291 rprintf(F,"\n");
292 rprintf(F,"rsync is a file transfer program capable of efficient remote update\n");
293 rprintf(F,"via a fast differencing algorithm.\n");
294
295 rprintf(F,"\n");
296 rprintf(F,"Usage: rsync [OPTION]... SRC [SRC]... DEST\n");
297 rprintf(F," or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST\n");
298 rprintf(F," or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST\n");
299 rprintf(F," or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST\n");
300 rprintf(F," or rsync [OPTION]... [USER@]HOST:SRC [DEST]\n");
301 rprintf(F," or rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
302 rprintf(F," or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
303 rprintf(F,"The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect\n");
304 rprintf(F,"to an rsync daemon, and require SRC or DEST to start with a module name.\n");
305 rprintf(F,"\n");
306 rprintf(F,"Options\n");
307 rprintf(F," -v, --verbose increase verbosity\n");
308 rprintf(F," -q, --quiet suppress non-error messages\n");
309 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
310 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
311 rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
312 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
313 rprintf(F," -r, --recursive recurse into directories\n");
314 rprintf(F," -R, --relative use relative path names\n");
315 rprintf(F," --no-implied-dirs don't send implied dirs with --relative\n");
316 rprintf(F," -b, --backup make backups (see --suffix & --backup-dir)\n");
317 rprintf(F," --backup-dir=DIR make backups into hierarchy based in DIR\n");
318 rprintf(F," --suffix=SUFFIX set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
319 rprintf(F," -u, --update skip files that are newer on the receiver\n");
320 rprintf(F," --inplace update destination files in-place (SEE MAN PAGE)\n");
321 rprintf(F," --append append data onto shorter files\n");
322 rprintf(F," --append-verify like --append, but with old data in file checksum\n");
323 rprintf(F," -d, --dirs transfer directories without recursing\n");
324 rprintf(F," -l, --links copy symlinks as symlinks\n");
325 rprintf(F," -L, --copy-links transform symlink into referent file/dir\n");
326 rprintf(F," --copy-unsafe-links only \"unsafe\" symlinks are transformed\n");
327 rprintf(F," --safe-links ignore symlinks that point outside the source tree\n");
328 rprintf(F," -k, --copy-dirlinks transform symlink to a dir into referent dir\n");
329 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
330 rprintf(F," -H, --hard-links preserve hard links\n");
331 rprintf(F," -p, --perms preserve permissions\n");
332 rprintf(F," -E, --executability preserve the file's executability\n");
333 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
334#ifdef SUPPORT_ACLS
335 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
336#endif
337#ifdef SUPPORT_XATTRS
338 rprintf(F," -X, --xattrs preserve extended attributes\n");
339#endif
340 rprintf(F," -o, --owner preserve owner (super-user only)\n");
341 rprintf(F," -g, --group preserve group\n");
342 rprintf(F," --devices preserve device files (super-user only)\n");
343 rprintf(F," --specials preserve special files\n");
344 rprintf(F," -D same as --devices --specials\n");
345 rprintf(F," -t, --times preserve modification times\n");
346 rprintf(F," -O, --omit-dir-times omit directories from --times\n");
347 rprintf(F," --super receiver attempts super-user activities\n");
348#ifdef SUPPORT_XATTRS
349 rprintf(F," --fake-super store/recover privileged attrs using xattrs\n");
350#endif
351 rprintf(F," -S, --sparse handle sparse files efficiently\n");
352 rprintf(F," -n, --dry-run show what would have been transferred\n");
353 rprintf(F," -W, --whole-file copy files whole (without rsync algorithm)\n");
354 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
355 rprintf(F," -B, --block-size=SIZE force a fixed checksum block-size\n");
356 rprintf(F," -e, --rsh=COMMAND specify the remote shell to use\n");
357 rprintf(F," --rsync-path=PROGRAM specify the rsync to run on the remote machine\n");
358 rprintf(F," --existing skip creating new files on receiver\n");
359 rprintf(F," --ignore-existing skip updating files that already exist on receiver\n");
360 rprintf(F," --remove-source-files sender removes synchronized files (non-dirs)\n");
361 rprintf(F," --del an alias for --delete-during\n");
362 rprintf(F," --delete delete extraneous files from destination dirs\n");
363 rprintf(F," --delete-before receiver deletes before transfer, not during\n");
364 rprintf(F," --delete-during receiver deletes during transfer (default)\n");
365 rprintf(F," --delete-delay find deletions during, delete after\n");
366 rprintf(F," --delete-after receiver deletes after transfer, not during\n");
367 rprintf(F," --delete-excluded also delete excluded files from destination dirs\n");
368 rprintf(F," --ignore-errors delete even if there are I/O errors\n");
369 rprintf(F," --force force deletion of directories even if not empty\n");
370 rprintf(F," --max-delete=NUM don't delete more than NUM files\n");
371 rprintf(F," --max-size=SIZE don't transfer any file larger than SIZE\n");
372 rprintf(F," --min-size=SIZE don't transfer any file smaller than SIZE\n");
373 rprintf(F," --partial keep partially transferred files\n");
374 rprintf(F," --partial-dir=DIR put a partially transferred file into DIR\n");
375 rprintf(F," --delay-updates put all updated files into place at transfer's end\n");
376 rprintf(F," -m, --prune-empty-dirs prune empty directory chains from the file-list\n");
377 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
378 rprintf(F," --timeout=TIME set I/O timeout in seconds\n");
379 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
380 rprintf(F," --size-only skip files that match in size\n");
381 rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
382 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
383 rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
384 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
385 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
386 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
387 rprintf(F," -z, --compress compress file data during the transfer\n");
388 rprintf(F," --compress-level=NUM explicitly set compression level\n");
389 rprintf(F," --skip-compress=LIST skip compressing files with a suffix in LIST\n");
390 rprintf(F," -C, --cvs-exclude auto-ignore files the same way CVS does\n");
391 rprintf(F," -f, --filter=RULE add a file-filtering RULE\n");
392 rprintf(F," -F same as --filter='dir-merge /.rsync-filter'\n");
393 rprintf(F," repeated: --filter='- .rsync-filter'\n");
394 rprintf(F," --exclude=PATTERN exclude files matching PATTERN\n");
395 rprintf(F," --exclude-from=FILE read exclude patterns from FILE\n");
396 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
397 rprintf(F," --include-from=FILE read include patterns from FILE\n");
398 rprintf(F," --files-from=FILE read list of source-file names from FILE\n");
399 rprintf(F," -0, --from0 all *-from/filter files are delimited by 0s\n");
400 rprintf(F," --address=ADDRESS bind address for outgoing socket to daemon\n");
401 rprintf(F," --port=PORT specify double-colon alternate port number\n");
402 rprintf(F," --sockopts=OPTIONS specify custom TCP options\n");
403 rprintf(F," --blocking-io use blocking I/O for the remote shell\n");
404 rprintf(F," --stats give some file-transfer stats\n");
405 rprintf(F," -8, --8-bit-output leave high-bit chars unescaped in output\n");
406 rprintf(F," -h, --human-readable output numbers in a human-readable format\n");
407 rprintf(F," --progress show progress during transfer\n");
408 rprintf(F," -P same as --partial --progress\n");
409 rprintf(F," -i, --itemize-changes output a change-summary for all updates\n");
410 rprintf(F," --out-format=FORMAT output updates using the specified FORMAT\n");
411 rprintf(F," --log-file=FILE log what we're doing to the specified FILE\n");
412 rprintf(F," --log-file-format=FMT log updates using the specified FMT\n");
413 rprintf(F," --password-file=FILE read daemon-access password from FILE\n");
414 rprintf(F," --list-only list the files instead of copying them\n");
415 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
416 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
417 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
418 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
419 rprintf(F," --protocol=NUM force an older protocol version to be used\n");
420#ifdef ICONV_OPTION
421 rprintf(F," --iconv=CONVERT_SPEC request charset conversion of filesnames\n");
422#endif
423 rprintf(F," -4, --ipv4 prefer IPv4\n");
424 rprintf(F," -6, --ipv6 prefer IPv6\n");
425 rprintf(F," --version print version number\n");
426 rprintf(F,"(-h) --help show this help (-h works with no other options)\n");
427
428 rprintf(F,"\n");
429 rprintf(F,"Use \"rsync --daemon --help\" to see the daemon-mode command-line options.\n");
430 rprintf(F,"Please see the rsync(1) and rsyncd.conf(5) man pages for full documentation.\n");
431 rprintf(F,"See http://rsync.samba.org/ for updates, bug reports, and answers\n");
432}
433
434enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
435 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
436 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
437 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
438 OPT_NO_D, OPT_APPEND,
439 OPT_SERVER, OPT_REFUSED_BASE = 9000};
440
441static struct poptOption long_options[] = {
442 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
443 {"help", 0, POPT_ARG_NONE, 0, OPT_HELP, 0, 0 },
444 {"version", 0, POPT_ARG_NONE, 0, OPT_VERSION, 0, 0},
445 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
446 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
447 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
448 {"quiet", 'q', POPT_ARG_NONE, 0, 'q', 0, 0 },
449 {"motd", 0, POPT_ARG_VAL, &output_motd, 1, 0, 0 },
450 {"no-motd", 0, POPT_ARG_VAL, &output_motd, 0, 0, 0 },
451 {"stats", 0, POPT_ARG_NONE, &do_stats, 0, 0, 0 },
452 {"human-readable", 'h', POPT_ARG_NONE, 0, 'h', 0, 0},
453 {"no-human-readable",0, POPT_ARG_VAL, &human_readable, 0, 0, 0},
454 {"no-h", 0, POPT_ARG_VAL, &human_readable, 0, 0, 0},
455 {"dry-run", 'n', POPT_ARG_NONE, &dry_run, 0, 0, 0 },
456 {"archive", 'a', POPT_ARG_NONE, 0, 'a', 0, 0 },
457 {"recursive", 'r', POPT_ARG_VAL, &recurse, 2, 0, 0 },
458 {"no-recursive", 0, POPT_ARG_VAL, &recurse, 0, 0, 0 },
459 {"no-r", 0, POPT_ARG_VAL, &recurse, 0, 0, 0 },
460 {"inc-recursive", 0, POPT_ARG_VAL, &allow_inc_recurse, 1, 0, 0 },
461 {"no-inc-recursive", 0, POPT_ARG_VAL, &allow_inc_recurse, 0, 0, 0 },
462 {"ir", 0, POPT_ARG_VAL, &allow_inc_recurse, 1, 0, 0 },
463 {"no-ir", 0, POPT_ARG_VAL, &allow_inc_recurse, 0, 0, 0 },
464 {"dirs", 'd', POPT_ARG_VAL, &xfer_dirs, 2, 0, 0 },
465 {"no-dirs", 0, POPT_ARG_VAL, &xfer_dirs, 0, 0, 0 },
466 {"no-d", 0, POPT_ARG_VAL, &xfer_dirs, 0, 0, 0 },
467 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
468 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
469 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
470 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
471 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
472 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
473 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
474 {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
475 {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
476 {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
477 {"times", 't', POPT_ARG_VAL, &preserve_times, 2, 0, 0 },
478 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
479 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
480 {"omit-dir-times", 'O', POPT_ARG_VAL, &omit_dir_times, 1, 0, 0 },
481 {"modify-window", 0, POPT_ARG_INT, &modify_window, OPT_MODIFY_WINDOW, 0, 0 },
482 {"super", 0, POPT_ARG_VAL, &am_root, 2, 0, 0 },
483 {"no-super", 0, POPT_ARG_VAL, &am_root, 0, 0, 0 },
484 {"fake-super", 0, POPT_ARG_VAL, &am_root, -1, 0, 0 },
485 {"owner", 'o', POPT_ARG_VAL, &preserve_uid, 1, 0, 0 },
486 {"no-owner", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
487 {"no-o", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
488 {"group", 'g', POPT_ARG_VAL, &preserve_gid, 1, 0, 0 },
489 {"no-group", 0, POPT_ARG_VAL, &preserve_gid, 0, 0, 0 },
490 {"no-g", 0, POPT_ARG_VAL, &preserve_gid, 0, 0, 0 },
491 {0, 'D', POPT_ARG_NONE, 0, 'D', 0, 0 },
492 {"no-D", 0, POPT_ARG_NONE, 0, OPT_NO_D, 0, 0 },
493 {"devices", 0, POPT_ARG_VAL, &preserve_devices, 1, 0, 0 },
494 {"no-devices", 0, POPT_ARG_VAL, &preserve_devices, 0, 0, 0 },
495 {"specials", 0, POPT_ARG_VAL, &preserve_specials, 1, 0, 0 },
496 {"no-specials", 0, POPT_ARG_VAL, &preserve_specials, 0, 0, 0 },
497 {"links", 'l', POPT_ARG_VAL, &preserve_links, 1, 0, 0 },
498 {"no-links", 0, POPT_ARG_VAL, &preserve_links, 0, 0, 0 },
499 {"no-l", 0, POPT_ARG_VAL, &preserve_links, 0, 0, 0 },
500 {"copy-links", 'L', POPT_ARG_NONE, &copy_links, 0, 0, 0 },
501 {"copy-unsafe-links",0, POPT_ARG_NONE, &copy_unsafe_links, 0, 0, 0 },
502 {"safe-links", 0, POPT_ARG_NONE, &safe_symlinks, 0, 0, 0 },
503 {"copy-dirlinks", 'k', POPT_ARG_NONE, &copy_dirlinks, 0, 0, 0 },
504 {"keep-dirlinks", 'K', POPT_ARG_NONE, &keep_dirlinks, 0, 0, 0 },
505 {"hard-links", 'H', POPT_ARG_NONE, 0, 'H', 0, 0 },
506 {"no-hard-links", 0, POPT_ARG_VAL, &preserve_hard_links, 0, 0, 0 },
507 {"no-H", 0, POPT_ARG_VAL, &preserve_hard_links, 0, 0, 0 },
508 {"relative", 'R', POPT_ARG_VAL, &relative_paths, 1, 0, 0 },
509 {"no-relative", 0, POPT_ARG_VAL, &relative_paths, 0, 0, 0 },
510 {"no-R", 0, POPT_ARG_VAL, &relative_paths, 0, 0, 0 },
511 {"implied-dirs", 0, POPT_ARG_VAL, &implied_dirs, 1, 0, 0 },
512 {"no-implied-dirs", 0, POPT_ARG_VAL, &implied_dirs, 0, 0, 0 },
513 {"chmod", 0, POPT_ARG_STRING, 0, OPT_CHMOD, 0, 0 },
514 {"ignore-times", 'I', POPT_ARG_NONE, &ignore_times, 0, 0, 0 },
515 {"size-only", 0, POPT_ARG_NONE, &size_only, 0, 0, 0 },
516 {"one-file-system", 'x', POPT_ARG_NONE, 0, 'x', 0, 0 },
517 {"update", 'u', POPT_ARG_NONE, &update_only, 0, 0, 0 },
518 {"existing", 0, POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
519 {"ignore-non-existing",0,POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
520 {"ignore-existing", 0, POPT_ARG_NONE, &ignore_existing, 0, 0, 0 },
521 {"max-size", 0, POPT_ARG_STRING, &max_size_arg, OPT_MAX_SIZE, 0, 0 },
522 {"min-size", 0, POPT_ARG_STRING, &min_size_arg, OPT_MIN_SIZE, 0, 0 },
523 {"sparse", 'S', POPT_ARG_NONE, &sparse_files, 0, 0, 0 },
524 {"inplace", 0, POPT_ARG_NONE, &inplace, 0, 0, 0 },
525 {"append", 0, POPT_ARG_NONE, 0, OPT_APPEND, 0, 0 },
526 {"append-verify", 0, POPT_ARG_VAL, &append_mode, 2, 0, 0 },
527 {"no-append", 0, POPT_ARG_VAL, &append_mode, 0, 0, 0 },
528 {"del", 0, POPT_ARG_NONE, &delete_during, 0, 0, 0 },
529 {"delete", 0, POPT_ARG_NONE, &delete_mode, 0, 0, 0 },
530 {"delete-before", 0, POPT_ARG_NONE, &delete_before, 0, 0, 0 },
531 {"delete-during", 0, POPT_ARG_VAL, &delete_during, 1, 0, 0 },
532 {"delete-delay", 0, POPT_ARG_VAL, &delete_during, 2, 0, 0 },
533 {"delete-after", 0, POPT_ARG_NONE, &delete_after, 0, 0, 0 },
534 {"delete-excluded", 0, POPT_ARG_NONE, &delete_excluded, 0, 0, 0 },
535 {"remove-sent-files",0, POPT_ARG_VAL, &remove_source_files, 2, 0, 0 }, /* deprecated */
536 {"remove-source-files",0,POPT_ARG_VAL, &remove_source_files, 1, 0, 0 },
537 {"force", 0, POPT_ARG_NONE, &force_delete, 0, 0, 0 },
538 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
539 {"max-delete", 0, POPT_ARG_INT, &max_delete, 0, 0, 0 },
540 {0, 'F', POPT_ARG_NONE, 0, 'F', 0, 0 },
541 {"filter", 'f', POPT_ARG_STRING, 0, OPT_FILTER, 0, 0 },
542 {"exclude", 0, POPT_ARG_STRING, 0, OPT_EXCLUDE, 0, 0 },
543 {"include", 0, POPT_ARG_STRING, 0, OPT_INCLUDE, 0, 0 },
544 {"exclude-from", 0, POPT_ARG_STRING, 0, OPT_EXCLUDE_FROM, 0, 0 },
545 {"include-from", 0, POPT_ARG_STRING, 0, OPT_INCLUDE_FROM, 0, 0 },
546 {"cvs-exclude", 'C', POPT_ARG_NONE, &cvs_exclude, 0, 0, 0 },
547 {"whole-file", 'W', POPT_ARG_VAL, &whole_file, 1, 0, 0 },
548 {"no-whole-file", 0, POPT_ARG_VAL, &whole_file, 0, 0, 0 },
549 {"no-W", 0, POPT_ARG_VAL, &whole_file, 0, 0, 0 },
550 {"checksum", 'c', POPT_ARG_VAL, &always_checksum, 1, 0, 0 },
551 {"no-checksum", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
552 {"no-c", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
553 {"block-size", 'B', POPT_ARG_LONG, &block_size, 0, 0, 0 },
554 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
555 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
556 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
557 {"fuzzy", 'y', POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
558 {"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
559 {"no-compress", 0, POPT_ARG_VAL, &do_compression, 0, 0, 0 },
560 {"skip-compress", 0, POPT_ARG_STRING, &skip_compress, 0, 0, 0 },
561 {"no-z", 0, POPT_ARG_VAL, &do_compression, 0, 0, 0 },
562 {"compress-level", 0, POPT_ARG_INT, &def_compress_level, 'z', 0, 0 },
563 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
564 {"progress", 0, POPT_ARG_VAL, &do_progress, 1, 0, 0 },
565 {"no-progress", 0, POPT_ARG_VAL, &do_progress, 0, 0, 0 },
566 {"partial", 0, POPT_ARG_VAL, &keep_partial, 1, 0, 0 },
567 {"no-partial", 0, POPT_ARG_VAL, &keep_partial, 0, 0, 0 },
568 {"partial-dir", 0, POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
569 {"delay-updates", 0, POPT_ARG_VAL, &delay_updates, 1, 0, 0 },
570 {"no-delay-updates", 0, POPT_ARG_VAL, &delay_updates, 0, 0, 0 },
571 {"prune-empty-dirs",'m', POPT_ARG_NONE, &prune_empty_dirs, 0, 0, 0 },
572 {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
573 {"log-file-format", 0, POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
574 {"out-format", 0, POPT_ARG_STRING, &stdout_format, 0, 0, 0 },
575 {"log-format", 0, POPT_ARG_STRING, &stdout_format, 0, 0, 0 }, /* DEPRECATED */
576 {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 },
577 {"no-itemize-changes",0, POPT_ARG_VAL, &itemize_changes, 0, 0, 0 },
578 {"no-i", 0, POPT_ARG_VAL, &itemize_changes, 0, 0, 0 },
579 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
580 {"no-bwlimit", 0, POPT_ARG_VAL, &bwlimit, 0, 0, 0 },
581 {"backup", 'b', POPT_ARG_VAL, &make_backups, 1, 0, 0 },
582 {"no-backup", 0, POPT_ARG_VAL, &make_backups, 0, 0, 0 },
583 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
584 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
585 {"list-only", 0, POPT_ARG_VAL, &list_only, 2, 0, 0 },
586 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
587 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
588 {"only-write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
589 {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 },
590 {"from0", '0', POPT_ARG_VAL, &eol_nulls, 1, 0, 0},
591 {"no-from0", 0, POPT_ARG_VAL, &eol_nulls, 0, 0, 0},
592 {"numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 1, 0, 0 },
593 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
594 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
595 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
596 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
597 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
598 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
599#ifdef ICONV_OPTION
600 {"iconv", 0, POPT_ARG_STRING, &iconv_opt, 0, 0, 0 },
601#endif
602 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
603 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
604 {"8-bit-output", '8', POPT_ARG_NONE, &allow_8bit_chars, 0, 0, 0 },
605 {"qsort", 0, POPT_ARG_NONE, &use_qsort, 0, 0, 0 },
606 {"address", 0, POPT_ARG_STRING, &bind_address, 0, 0, 0 },
607 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
608 {"sockopts", 0, POPT_ARG_STRING, &sockopts, 0, 0, 0 },
609 {"password-file", 0, POPT_ARG_STRING, &password_file, 0, 0, 0 },
610 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
611 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
612 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
613 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
614 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
615 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
616 /* All the following options switch us into daemon-mode option-parsing. */
617 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
618 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
619 {"detach", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
620 {"no-detach", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
621 {0,0,0,0, 0, 0, 0}
622};
623
624static void daemon_usage(enum logcode F)
625{
626 print_rsync_version(F);
627
628 rprintf(F,"\n");
629 rprintf(F,"Usage: rsync --daemon [OPTION]...\n");
630 rprintf(F," --address=ADDRESS bind to the specified address\n");
631 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
632 rprintf(F," --config=FILE specify alternate rsyncd.conf file\n");
633 rprintf(F," --no-detach do not detach from the parent\n");
634 rprintf(F," --port=PORT listen on alternate port number\n");
635 rprintf(F," --log-file=FILE override the \"log file\" setting\n");
636 rprintf(F," --log-file-format=FMT override the \"log format\" setting\n");
637 rprintf(F," --sockopts=OPTIONS specify custom TCP options\n");
638 rprintf(F," -v, --verbose increase verbosity\n");
639 rprintf(F," -4, --ipv4 prefer IPv4\n");
640 rprintf(F," -6, --ipv6 prefer IPv6\n");
641 rprintf(F," --help show this help screen\n");
642
643 rprintf(F,"\n");
644 rprintf(F,"If you were not trying to invoke rsync as a daemon, avoid using any of the\n");
645 rprintf(F,"daemon-specific rsync options. See also the rsyncd.conf(5) man page.\n");
646}
647
648static struct poptOption long_daemon_options[] = {
649 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
650 {"address", 0, POPT_ARG_STRING, &bind_address, 0, 0, 0 },
651 {"bwlimit", 0, POPT_ARG_INT, &daemon_bwlimit, 0, 0, 0 },
652 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
653 {"daemon", 0, POPT_ARG_NONE, &daemon_opt, 0, 0, 0 },
654 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
655 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
656 {"detach", 0, POPT_ARG_VAL, &no_detach, 0, 0, 0 },
657 {"no-detach", 0, POPT_ARG_VAL, &no_detach, 1, 0, 0 },
658 {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
659 {"log-file-format", 0, POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
660 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
661 {"sockopts", 0, POPT_ARG_STRING, &sockopts, 0, 0, 0 },
662 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
663 {"server", 0, POPT_ARG_NONE, &am_server, 0, 0, 0 },
664 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
665 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
666 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
667 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
668 {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 },
669 {0,0,0,0, 0, 0, 0}
670};
671
672
673static char err_buf[200];
674
675
676/**
677 * Store the option error message, if any, so that we can log the
678 * connection attempt (which requires parsing the options), and then
679 * show the error later on.
680 **/
681void option_error(void)
682{
683 if (!err_buf[0]) {
684 strlcpy(err_buf, "Error parsing options: option may "
685 "be supported on client but not on server?\n",
686 sizeof err_buf);
687 }
688
689 rprintf(FERROR, RSYNC_NAME ": %s", err_buf);
690 msleep(20);
691}
692
693
694/**
695 * Tweak the option table to disable all options that the rsyncd.conf
696 * file has told us to refuse.
697 **/
698static void set_refuse_options(char *bp)
699{
700 struct poptOption *op;
701 char *cp, shortname[2];
702 int is_wild, found_match;
703
704 shortname[1] = '\0';
705
706 while (1) {
707 while (*bp == ' ') bp++;
708 if (!*bp)
709 break;
710 if ((cp = strchr(bp, ' ')) != NULL)
711 *cp= '\0';
712 is_wild = strpbrk(bp, "*?[") != NULL;
713 found_match = 0;
714 for (op = long_options; ; op++) {
715 *shortname = op->shortName;
716 if (!op->longName && !*shortname)
717 break;
718 if ((op->longName && wildmatch(bp, op->longName))
719 || (*shortname && wildmatch(bp, shortname))) {
720 if (op->argInfo == POPT_ARG_VAL)
721 op->argInfo = POPT_ARG_NONE;
722 op->val = (op - long_options) + OPT_REFUSED_BASE;
723 found_match = 1;
724 /* These flags are set to let us easily check
725 * an implied option later in the code. */
726 switch (*shortname) {
727 case 'r': case 'd': case 'l': case 'p':
728 case 't': case 'g': case 'o': case 'D':
729 refused_archive_part = op->val;
730 break;
731 case 'z':
732 refused_compress = op->val;
733 break;
734 case '\0':
735 if (wildmatch("delete", op->longName))
736 refused_delete = op->val;
737 else if (wildmatch("delete-before", op->longName))
738 refused_delete_before = op->val;
739 else if (wildmatch("delete-during", op->longName))
740 refused_delete_during = op->val;
741 else if (wildmatch("partial", op->longName))
742 refused_partial = op->val;
743 else if (wildmatch("progress", op->longName))
744 refused_progress = op->val;
745 else if (wildmatch("inplace", op->longName))
746 refused_inplace = op->val;
747 break;
748 }
749 if (!is_wild)
750 break;
751 }
752 }
753 if (!found_match) {
754 rprintf(FLOG, "No match for refuse-options string \"%s\"\n",
755 bp);
756 }
757 if (!cp)
758 break;
759 *cp = ' ';
760 bp = cp + 1;
761 }
762}
763
764
765static int count_args(const char **argv)
766{
767 int i = 0;
768
769 if (argv) {
770 while (argv[i] != NULL)
771 i++;
772 }
773
774 return i;
775}
776
777
778static OFF_T parse_size_arg(char **size_arg, char def_suf)
779{
780 int reps, mult, make_compatible = 0;
781 const char *arg;
782 OFF_T size = 1;
783
784 for (arg = *size_arg; isDigit(arg); arg++) {}
785 if (*arg == '.')
786 for (arg++; isDigit(arg); arg++) {}
787 switch (*arg && *arg != '+' && *arg != '-' ? *arg++ : def_suf) {
788 case 'b': case 'B':
789 reps = 0;
790 break;
791 case 'k': case 'K':
792 reps = 1;
793 break;
794 case 'm': case 'M':
795 reps = 2;
796 break;
797 case 'g': case 'G':
798 reps = 3;
799 break;
800 default:
801 return -1;
802 }
803 if (*arg == 'b' || *arg == 'B')
804 mult = 1000, make_compatible = 1, arg++;
805 else if (!*arg || *arg == '+' || *arg == '-')
806 mult = 1024;
807 else if (strncasecmp(arg, "ib", 2) == 0)
808 mult = 1024, arg += 2;
809 else
810 return -1;
811 while (reps--)
812 size *= mult;
813 size *= atof(*size_arg);
814 if ((*arg == '+' || *arg == '-') && arg[1] == '1')
815 size += atoi(arg), make_compatible = 1, arg += 2;
816 if (*arg)
817 return -1;
818 if (size > 0 && make_compatible) {
819 /* We convert this manually because we may need %lld precision,
820 * and that's not a portable sprintf() escape. */
821 char buf[128], *s = buf + sizeof buf - 1;
822 OFF_T num = size;
823 *s = '\0';
824 while (num) {
825 *--s = (char)(num % 10) + '0';
826 num /= 10;
827 }
828 if (!(*size_arg = strdup(s)))
829 out_of_memory("parse_size_arg");
830 }
831 return size;
832}
833
834
835static void create_refuse_error(int which)
836{
837 /* The "which" value is the index + OPT_REFUSED_BASE. */
838 struct poptOption *op = &long_options[which - OPT_REFUSED_BASE];
839 int n = snprintf(err_buf, sizeof err_buf,
840 "The server is configured to refuse --%s\n",
841 op->longName) - 1;
842 if (op->shortName) {
843 snprintf(err_buf + n, sizeof err_buf - n,
844 " (-%c)\n", op->shortName);
845 }
846}
847
848
849/**
850 * Process command line arguments. Called on both local and remote.
851 *
852 * @retval 1 if all options are OK; with globals set to appropriate
853 * values
854 *
855 * @retval 0 on error, with err_buf containing an explanation
856 **/
857int parse_arguments(int *argc, const char ***argv, int frommain)
858{
859 int opt;
860 char *ref = lp_refuse_options(module_id);
861 const char *arg;
862 poptContext pc;
863
864 if (ref && *ref)
865 set_refuse_options(ref);
866 if (am_daemon)
867 set_refuse_options("log-file*");
868
869#ifdef ICONV_OPTION
870 if (!am_daemon && (arg = getenv("RSYNC_ICONV")) != NULL && *arg)
871 iconv_opt = strdup(arg);
872#endif
873
874 /* TODO: Call poptReadDefaultConfig; handle errors. */
875
876 /* The context leaks in case of an error, but if there's a
877 * problem we always exit anyhow. */
878 pc = poptGetContext(RSYNC_NAME, *argc, *argv, long_options, 0);
879 poptReadDefaultConfig(pc, 0);
880
881 while ((opt = poptGetNextOpt(pc)) != -1) {
882 /* most options are handled automatically by popt;
883 * only special cases are returned and listed here. */
884
885 switch (opt) {
886 case OPT_VERSION:
887 print_rsync_version(FINFO);
888 exit_cleanup(0);
889
890 case OPT_SERVER:
891 if (!am_server) {
892 /* Disable popt aliases on the server side and
893 * then start parsing the options again. */
894 poptFreeContext(pc);
895 pc = poptGetContext(RSYNC_NAME, *argc, *argv,
896 long_options, 0);
897 am_server = 1;
898 }
899#ifdef ICONV_OPTION
900 iconv_opt = NULL;
901#endif
902 break;
903
904 case OPT_SENDER:
905 if (!am_server) {
906 usage(FERROR);
907 exit_cleanup(RERR_SYNTAX);
908 }
909 am_sender = 1;
910 break;
911
912 case OPT_DAEMON:
913 if (am_daemon) {
914 strlcpy(err_buf,
915 "Attempt to hack rsync thwarted!\n",
916 sizeof err_buf);
917 return 0;
918 }
919#ifdef ICONV_OPTION
920 iconv_opt = NULL;
921#endif
922 poptFreeContext(pc);
923 pc = poptGetContext(RSYNC_NAME, *argc, *argv,
924 long_daemon_options, 0);
925 while ((opt = poptGetNextOpt(pc)) != -1) {
926 switch (opt) {
927 case 'h':
928 daemon_usage(FINFO);
929 exit_cleanup(0);
930
931 case 'v':
932 verbose++;
933 break;
934
935 default:
936 rprintf(FERROR,
937 "rsync: %s: %s (in daemon mode)\n",
938 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
939 poptStrerror(opt));
940 goto daemon_error;
941 }
942 }
943
944 if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
945 snprintf(err_buf, sizeof err_buf,
946 "the --temp-dir path is WAY too long.\n");
947 return 0;
948 }
949
950 if (!daemon_opt) {
951 rprintf(FERROR, "Daemon option(s) used without --daemon.\n");
952 daemon_error:
953 rprintf(FERROR,
954 "(Type \"rsync --daemon --help\" for assistance with daemon mode.)\n");
955 exit_cleanup(RERR_SYNTAX);
956 }
957
958 *argv = poptGetArgs(pc);
959 *argc = count_args(*argv);
960 am_starting_up = 0;
961 daemon_opt = 0;
962 am_daemon = 1;
963 return 1;
964
965 case OPT_MODIFY_WINDOW:
966 /* The value has already been set by popt, but
967 * we need to remember that we're using a
968 * non-default setting. */
969 modify_window_set = 1;
970 break;
971
972 case OPT_FILTER:
973 parse_rule(&filter_list, poptGetOptArg(pc), 0, 0);
974 break;
975
976 case OPT_EXCLUDE:
977 parse_rule(&filter_list, poptGetOptArg(pc),
978 0, XFLG_OLD_PREFIXES);
979 break;
980
981 case OPT_INCLUDE:
982 parse_rule(&filter_list, poptGetOptArg(pc),
983 MATCHFLG_INCLUDE, XFLG_OLD_PREFIXES);
984 break;
985
986 case OPT_EXCLUDE_FROM:
987 case OPT_INCLUDE_FROM:
988 arg = poptGetOptArg(pc);
989 if (sanitize_paths)
990 arg = sanitize_path(NULL, arg, NULL, 0, NULL);
991 if (server_filter_list.head) {
992 char *cp = strdup(arg);
993 if (!cp)
994 out_of_memory("parse_arguments");
995 if (!*cp)
996 goto options_rejected;
997 clean_fname(cp, 1);
998 if (check_filter(&server_filter_list, cp, 0) < 0)
999 goto options_rejected;
1000 free(cp);
1001 }
1002 parse_filter_file(&filter_list, arg,
1003 opt == OPT_INCLUDE_FROM ? MATCHFLG_INCLUDE : 0,
1004 XFLG_FATAL_ERRORS | XFLG_OLD_PREFIXES);
1005 break;
1006
1007 case 'a':
1008 if (refused_archive_part) {
1009 create_refuse_error(refused_archive_part);
1010 return 0;
1011 }
1012 if (!recurse) /* preserve recurse == 2 */
1013 recurse = 1;
1014#ifdef SUPPORT_LINKS
1015 preserve_links = 1;
1016#endif
1017 preserve_perms = 1;
1018 preserve_times = 2;
1019 preserve_gid = 1;
1020 preserve_uid = 1;
1021 preserve_devices = 1;
1022 preserve_specials = 1;
1023 break;
1024
1025 case 'D':
1026 preserve_devices = preserve_specials = 1;
1027 break;
1028
1029 case OPT_NO_D:
1030 preserve_devices = preserve_specials = 0;
1031 break;
1032
1033 case 'h':
1034 human_readable++;
1035 break;
1036
1037 case 'H':
1038 preserve_hard_links++;
1039 break;
1040
1041 case 'i':
1042 itemize_changes++;
1043 break;
1044
1045 case 'v':
1046 verbose++;
1047 break;
1048
1049 case 'q':
1050 if (frommain)
1051 quiet++;
1052 break;
1053
1054 case 'x':
1055 one_file_system++;
1056 break;
1057
1058 case 'F':
1059 switch (++F_option_cnt) {
1060 case 1:
1061 parse_rule(&filter_list,": /.rsync-filter",0,0);
1062 break;
1063 case 2:
1064 parse_rule(&filter_list,"- .rsync-filter",0,0);
1065 break;
1066 }
1067 break;
1068
1069 case 'P':
1070 if (refused_partial || refused_progress) {
1071 create_refuse_error(refused_partial
1072 ? refused_partial : refused_progress);
1073 return 0;
1074 }
1075 do_progress = 1;
1076 keep_partial = 1;
1077 break;
1078
1079 case 'z':
1080 if (def_compress_level < Z_DEFAULT_COMPRESSION
1081 || def_compress_level > Z_BEST_COMPRESSION) {
1082 snprintf(err_buf, sizeof err_buf,
1083 "--compress-level value is invalid: %d\n",
1084 def_compress_level);
1085 return 0;
1086 }
1087 do_compression = def_compress_level != Z_NO_COMPRESSION;
1088 if (do_compression && refused_compress) {
1089 create_refuse_error(refused_compress);
1090 return 0;
1091 }
1092 break;
1093
1094 case OPT_WRITE_BATCH:
1095 /* batch_name is already set */
1096 write_batch = 1;
1097 break;
1098
1099 case OPT_ONLY_WRITE_BATCH:
1100 /* batch_name is already set */
1101 write_batch = -1;
1102 break;
1103
1104 case OPT_READ_BATCH:
1105 /* batch_name is already set */
1106 read_batch = 1;
1107 break;
1108
1109 case OPT_MAX_SIZE:
1110 if ((max_size = parse_size_arg(&max_size_arg, 'b')) <= 0) {
1111 snprintf(err_buf, sizeof err_buf,
1112 "--max-size value is invalid: %s\n",
1113 max_size_arg);
1114 return 0;
1115 }
1116 break;
1117
1118 case OPT_MIN_SIZE:
1119 if ((min_size = parse_size_arg(&min_size_arg, 'b')) <= 0) {
1120 snprintf(err_buf, sizeof err_buf,
1121 "--min-size value is invalid: %s\n",
1122 min_size_arg);
1123 return 0;
1124 }
1125 break;
1126
1127 case OPT_APPEND:
1128 if (am_server)
1129 append_mode++;
1130 else
1131 append_mode = 1;
1132 break;
1133
1134 case OPT_LINK_DEST:
1135#ifdef SUPPORT_HARD_LINKS
1136 link_dest = 1;
1137 dest_option = "--link-dest";
1138 goto set_dest_dir;
1139#else
1140 snprintf(err_buf, sizeof err_buf,
1141 "hard links are not supported on this %s\n",
1142 am_server ? "server" : "client");
1143 return 0;
1144#endif
1145
1146 case OPT_COPY_DEST:
1147 copy_dest = 1;
1148 dest_option = "--copy-dest";
1149 goto set_dest_dir;
1150
1151 case OPT_COMPARE_DEST:
1152 compare_dest = 1;
1153 dest_option = "--compare-dest";
1154 set_dest_dir:
1155 if (basis_dir_cnt >= MAX_BASIS_DIRS) {
1156 snprintf(err_buf, sizeof err_buf,
1157 "ERROR: at most %d %s args may be specified\n",
1158 MAX_BASIS_DIRS, dest_option);
1159 return 0;
1160 }
1161 /* We defer sanitizing this arg until we know what
1162 * our destination directory is going to be. */
1163 basis_dir[basis_dir_cnt++] = (char *)poptGetOptArg(pc);
1164 break;
1165
1166 case OPT_CHMOD:
1167 arg = poptGetOptArg(pc);
1168 if (!parse_chmod(arg, &chmod_modes)) {
1169 snprintf(err_buf, sizeof err_buf,
1170 "Invalid argument passed to --chmod (%s)\n",
1171 arg);
1172 return 0;
1173 }
1174 break;
1175
1176 case OPT_HELP:
1177 usage(FINFO);
1178 exit_cleanup(0);
1179
1180 case 'A':
1181#ifdef SUPPORT_ACLS
1182 preserve_acls = 1;
1183 preserve_perms = 1;
1184 break;
1185#else
1186 /* FIXME: this should probably be ignored with a
1187 * warning and then countermeasures taken to
1188 * restrict group and other access in the presence
1189 * of any more restrictive ACLs, but this is safe
1190 * for now */
1191 snprintf(err_buf,sizeof(err_buf),
1192 "ACLs are not supported on this %s\n",
1193 am_server ? "server" : "client");
1194 return 0;
1195#endif
1196
1197 case 'X':
1198#ifdef SUPPORT_XATTRS
1199 preserve_xattrs++;
1200 break;
1201#else
1202 snprintf(err_buf,sizeof(err_buf),
1203 "extended attributes are not supported on this %s\n",
1204 am_server ? "server" : "client");
1205 return 0;
1206#endif
1207
1208 default:
1209 /* A large opt value means that set_refuse_options()
1210 * turned this option off. */
1211 if (opt >= OPT_REFUSED_BASE) {
1212 create_refuse_error(opt);
1213 return 0;
1214 }
1215 snprintf(err_buf, sizeof err_buf, "%s%s: %s\n",
1216 am_server ? "on remote machine: " : "",
1217 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1218 poptStrerror(opt));
1219 return 0;
1220 }
1221 }
1222
1223 if (human_readable && *argc == 2) {
1224 /* Allow the old meaning of 'h' (--help) on its own. */
1225 usage(FINFO);
1226 exit_cleanup(0);
1227 }
1228
1229#ifdef ICONV_OPTION
1230 if (iconv_opt) {
1231 if (!am_server && strcmp(iconv_opt, "-") == 0)
1232 iconv_opt = NULL;
1233 else
1234 need_unsorted_flist = 1;
1235 }
1236#endif
1237
1238#ifndef SUPPORT_LINKS
1239 if (preserve_links && !am_sender) {
1240 snprintf(err_buf, sizeof err_buf,
1241 "symlinks are not supported on this %s\n",
1242 am_server ? "server" : "client");
1243 return 0;
1244 }
1245#endif
1246
1247#ifndef SUPPORT_HARD_LINKS
1248 if (preserve_hard_links) {
1249 snprintf(err_buf, sizeof err_buf,
1250 "hard links are not supported on this %s\n",
1251 am_server ? "server" : "client");
1252 return 0;
1253 }
1254#endif
1255
1256#ifndef SUPPORT_XATTRS
1257 if (am_root < 0) {
1258 snprintf(err_buf, sizeof err_buf,
1259 "--fake-super requires an rsync with extended attributes enabled\n");
1260 return 0;
1261 }
1262#endif
1263
1264 if (write_batch && read_batch) {
1265 snprintf(err_buf, sizeof err_buf,
1266 "--write-batch and --read-batch can not be used together\n");
1267 return 0;
1268 }
1269 if (write_batch > 0 || read_batch) {
1270 if (am_server) {
1271 rprintf(FINFO,
1272 "ignoring --%s-batch option sent to server\n",
1273 write_batch ? "write" : "read");
1274 /* We don't actually exit_cleanup(), so that we can
1275 * still service older version clients that still send
1276 * batch args to server. */
1277 read_batch = write_batch = 0;
1278 batch_name = NULL;
1279 } else if (dry_run)
1280 write_batch = 0;
1281 }
1282 if (read_batch && files_from) {
1283 snprintf(err_buf, sizeof err_buf,
1284 "--read-batch cannot be used with --files-from\n");
1285 return 0;
1286 }
1287 if (batch_name && strlen(batch_name) > MAX_BATCH_NAME_LEN) {
1288 snprintf(err_buf, sizeof err_buf,
1289 "the batch-file name must be %d characters or less.\n",
1290 MAX_BATCH_NAME_LEN);
1291 return 0;
1292 }
1293
1294 if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
1295 snprintf(err_buf, sizeof err_buf,
1296 "the --temp-dir path is WAY too long.\n");
1297 return 0;
1298 }
1299
1300 if (max_delete < 0 && max_delete != INT_MIN) {
1301 /* Negative numbers are treated as "no deletions". */
1302 max_delete = 0;
1303 }
1304
1305 if (compare_dest + copy_dest + link_dest > 1) {
1306 snprintf(err_buf, sizeof err_buf,
1307 "You may not mix --compare-dest, --copy-dest, and --link-dest.\n");
1308 return 0;
1309 }
1310
1311 if (files_from) {
1312 if (recurse == 1) /* preserve recurse == 2 */
1313 recurse = 0;
1314 if (xfer_dirs < 0)
1315 xfer_dirs = 1;
1316 }
1317
1318 if (xfer_dirs < 1)
1319 xfer_dirs = recurse || list_only;
1320
1321 if (relative_paths < 0)
1322 relative_paths = files_from? 1 : 0;
1323 if (!relative_paths)
1324 implied_dirs = 0;
1325
1326 if (delete_before + !!delete_during + delete_after > 1) {
1327 snprintf(err_buf, sizeof err_buf,
1328 "You may not combine multiple --delete-WHEN options.\n");
1329 return 0;
1330 }
1331 if (delete_before || delete_during || delete_after)
1332 delete_mode = 1;
1333 else if (delete_mode || delete_excluded) {
1334 /* Only choose now between before & during if one is refused. */
1335 if (refused_delete_before) {
1336 if (!refused_delete_during)
1337 delete_during = 1;
1338 else {
1339 create_refuse_error(refused_delete_before);
1340 return 0;
1341 }
1342 } else if (refused_delete_during)
1343 delete_before = 1;
1344 delete_mode = 1;
1345 }
1346 if (!xfer_dirs && delete_mode) {
1347 snprintf(err_buf, sizeof err_buf,
1348 "--delete does not work without -r or -d.\n");
1349 return 0;
1350 }
1351
1352 if (delete_mode && refused_delete) {
1353 create_refuse_error(refused_delete);
1354 return 0;
1355 }
1356
1357 if (remove_source_files) {
1358 /* We only want to infer this refusal of --remove-source-files
1359 * via the refusal of "delete", not any of the "delete-FOO"
1360 * options. */
1361 if (refused_delete && am_sender) {
1362 create_refuse_error(refused_delete);
1363 return 0;
1364 }
1365 need_messages_from_generator = 1;
1366 }
1367
1368 *argv = poptGetArgs(pc);
1369 *argc = count_args(*argv);
1370
1371 if (sanitize_paths) {
1372 int i;
1373 for (i = *argc; i-- > 0; )
1374 (*argv)[i] = sanitize_path(NULL, (*argv)[i], "", 0, NULL);
1375 if (tmpdir)
1376 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, NULL);
1377 if (backup_dir)
1378 backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, NULL);
1379 }
1380 if (server_filter_list.head && !am_sender) {
1381 struct filter_list_struct *elp = &server_filter_list;
1382 if (tmpdir) {
1383 if (!*tmpdir)
1384 goto options_rejected;
1385 clean_fname(tmpdir, 1);
1386 if (check_filter(elp, tmpdir, 1) < 0)
1387 goto options_rejected;
1388 }
1389 if (backup_dir) {
1390 if (!*backup_dir)
1391 goto options_rejected;
1392 clean_fname(backup_dir, 1);
1393 if (check_filter(elp, backup_dir, 1) < 0)
1394 goto options_rejected;
1395 }
1396 }
1397
1398 if (omit_dir_times && preserve_times > 1)
1399 preserve_times = 1;
1400
1401 if (!backup_suffix)
1402 backup_suffix = backup_dir ? "" : BACKUP_SUFFIX;
1403 backup_suffix_len = strlen(backup_suffix);
1404 if (strchr(backup_suffix, '/') != NULL) {
1405 snprintf(err_buf, sizeof err_buf,
1406 "--suffix cannot contain slashes: %s\n",
1407 backup_suffix);
1408 return 0;
1409 }
1410 if (backup_dir) {
1411 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
1412 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
1413 if (backup_dir_remainder < 32) {
1414 snprintf(err_buf, sizeof err_buf,
1415 "the --backup-dir path is WAY too long.\n");
1416 return 0;
1417 }
1418 if (backup_dir_buf[backup_dir_len - 1] != '/') {
1419 backup_dir_buf[backup_dir_len++] = '/';
1420 backup_dir_buf[backup_dir_len] = '\0';
1421 }
1422 if (verbose > 1 && !am_sender)
1423 rprintf(FINFO, "backup_dir is %s\n", backup_dir_buf);
1424 } else if (!backup_suffix_len && (!am_server || !am_sender)) {
1425 snprintf(err_buf, sizeof err_buf,
1426 "--suffix cannot be a null string without --backup-dir\n");
1427 return 0;
1428 } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
1429 snprintf(backup_dir_buf, sizeof backup_dir_buf,
1430 "P *%s", backup_suffix);
1431 parse_rule(&filter_list, backup_dir_buf, 0, 0);
1432 }
1433 if (make_backups && !backup_dir && preserve_times > 1)
1434 preserve_times = 1;
1435
1436 if (stdout_format) {
1437 if (am_server && log_format_has(stdout_format, 'I'))
1438 stdout_format_has_i = 2;
1439 else if (log_format_has(stdout_format, 'i'))
1440 stdout_format_has_i = itemize_changes | 1;
1441 if (!log_format_has(stdout_format, 'b')
1442 && !log_format_has(stdout_format, 'c'))
1443 log_before_transfer = !am_server;
1444 } else if (itemize_changes) {
1445 stdout_format = "%i %n%L";
1446 stdout_format_has_i = itemize_changes;
1447 log_before_transfer = !am_server;
1448 }
1449
1450 if (do_progress && !verbose && !log_before_transfer && !am_server)
1451 verbose = 1;
1452
1453 if (dry_run)
1454 do_xfers = 0;
1455
1456 set_io_timeout(io_timeout);
1457
1458 if (verbose && !stdout_format) {
1459 stdout_format = "%n%L";
1460 log_before_transfer = !am_server;
1461 }
1462 if (stdout_format_has_i || log_format_has(stdout_format, 'o'))
1463 stdout_format_has_o_or_i = 1;
1464
1465 if (logfile_name && !am_daemon) {
1466 if (!logfile_format) {
1467 logfile_format = "%i %n%L";
1468 logfile_format_has_i = logfile_format_has_o_or_i = 1;
1469 } else {
1470 if (log_format_has(logfile_format, 'i'))
1471 logfile_format_has_i = 1;
1472 if (logfile_format_has_i || log_format_has(logfile_format, 'o'))
1473 logfile_format_has_o_or_i = 1;
1474 }
1475 log_init(0);
1476 } else if (!am_daemon)
1477 logfile_format = NULL;
1478
1479 if (daemon_bwlimit && (!bwlimit || bwlimit > daemon_bwlimit))
1480 bwlimit = daemon_bwlimit;
1481 if (bwlimit) {
1482 bwlimit_writemax = (size_t)bwlimit * 128;
1483 if (bwlimit_writemax < 512)
1484 bwlimit_writemax = 512;
1485 }
1486
1487 if (sparse_files && inplace) {
1488 /* Note: we don't check for this below, because --append is
1489 * OK with --sparse (as long as redos are handled right). */
1490 snprintf(err_buf, sizeof err_buf,
1491 "--sparse cannot be used with --inplace\n");
1492 return 0;
1493 }
1494
1495 if (append_mode) {
1496 if (whole_file > 0) {
1497 snprintf(err_buf, sizeof err_buf,
1498 "--append cannot be used with --whole-file\n");
1499 return 0;
1500 }
1501 if (refused_inplace) {
1502 create_refuse_error(refused_inplace);
1503 return 0;
1504 }
1505 inplace = 1;
1506 }
1507
1508 if (delay_updates && !partial_dir)
1509 partial_dir = tmp_partialdir;
1510
1511 if (inplace) {
1512#ifdef HAVE_FTRUNCATE
1513 if (partial_dir) {
1514 snprintf(err_buf, sizeof err_buf,
1515 "--%s cannot be used with --%s\n",
1516 append_mode ? "append" : "inplace",
1517 delay_updates ? "delay-updates" : "partial-dir");
1518 return 0;
1519 }
1520 /* --inplace implies --partial for refusal purposes, but we
1521 * clear the keep_partial flag for internal logic purposes. */
1522 if (refused_partial) {
1523 create_refuse_error(refused_partial);
1524 return 0;
1525 }
1526 keep_partial = 0;
1527#else
1528 snprintf(err_buf, sizeof err_buf,
1529 "--%s is not supported on this %s\n",
1530 append_mode ? "append" : "inplace",
1531 am_server ? "server" : "client");
1532 return 0;
1533#endif
1534 } else {
1535 if (keep_partial && !partial_dir && !am_server) {
1536 if ((arg = getenv("RSYNC_PARTIAL_DIR")) != NULL && *arg)
1537 partial_dir = strdup(arg);
1538 }
1539 if (partial_dir) {
1540 if (*partial_dir)
1541 clean_fname(partial_dir, 1);
1542 if (!*partial_dir || strcmp(partial_dir, ".") == 0)
1543 partial_dir = NULL;
1544 if (!partial_dir && refused_partial) {
1545 create_refuse_error(refused_partial);
1546 return 0;
1547 }
1548 keep_partial = 1;
1549 }
1550 }
1551
1552 if (files_from) {
1553 char *h, *p;
1554 int q;
1555 if (*argc > 2 || (!am_daemon && *argc == 1)) {
1556 usage(FERROR);
1557 exit_cleanup(RERR_SYNTAX);
1558 }
1559 if (strcmp(files_from, "-") == 0) {
1560 filesfrom_fd = 0;
1561 if (am_server)
1562 filesfrom_host = ""; /* reading from socket */
1563 } else if ((p = check_for_hostspec(files_from, &h, &q)) != 0) {
1564 if (am_server) {
1565 snprintf(err_buf, sizeof err_buf,
1566 "The --files-from sent to the server cannot specify a host.\n");
1567 return 0;
1568 }
1569 files_from = p;
1570 filesfrom_host = h;
1571 if (strcmp(files_from, "-") == 0) {
1572 snprintf(err_buf, sizeof err_buf,
1573 "Invalid --files-from remote filename\n");
1574 return 0;
1575 }
1576 } else {
1577 if (sanitize_paths)
1578 files_from = sanitize_path(NULL, files_from, NULL, 0, NULL);
1579 if (server_filter_list.head) {
1580 if (!*files_from)
1581 goto options_rejected;
1582 clean_fname(files_from, 1);
1583 if (check_filter(&server_filter_list, files_from, 0) < 0)
1584 goto options_rejected;
1585 }
1586 filesfrom_fd = open(files_from, O_RDONLY|O_BINARY);
1587 if (filesfrom_fd < 0) {
1588 snprintf(err_buf, sizeof err_buf,
1589 "failed to open files-from file %s: %s\n",
1590 files_from, strerror(errno));
1591 return 0;
1592 }
1593 }
1594 }
1595
1596 am_starting_up = 0;
1597
1598 return 1;
1599
1600 options_rejected:
1601 snprintf(err_buf, sizeof err_buf,
1602 "Your options have been rejected by the server.\n");
1603 return 0;
1604}
1605
1606
1607/**
1608 * Construct a filtered list of options to pass through from the
1609 * client to the server.
1610 *
1611 * This involves setting options that will tell the server how to
1612 * behave, and also filtering out options that are processed only
1613 * locally.
1614 **/
1615void server_options(char **args,int *argc)
1616{
1617 static char argstr[64];
1618 int ac = *argc;
1619 char *arg;
1620
1621 int i, x;
1622
1623 if (blocking_io == -1)
1624 blocking_io = 0;
1625
1626 /* This should always remain first on the server's command-line. */
1627 args[ac++] = "--server";
1628
1629 if (daemon_over_rsh > 0) {
1630 args[ac++] = "--daemon";
1631 *argc = ac;
1632 /* if we're passing --daemon, we're done */
1633 return;
1634 }
1635
1636 if (!am_sender)
1637 args[ac++] = "--sender";
1638
1639 x = 1;
1640 argstr[0] = '-';
1641 for (i = 0; i < verbose; i++)
1642 argstr[x++] = 'v';
1643
1644 /* the -q option is intentionally left out */
1645 if (make_backups)
1646 argstr[x++] = 'b';
1647 if (update_only)
1648 argstr[x++] = 'u';
1649 if (!do_xfers) /* Note: NOT "dry_run"! */
1650 argstr[x++] = 'n';
1651 if (preserve_links)
1652 argstr[x++] = 'l';
1653 if (xfer_dirs > (recurse || !delete_mode || !am_sender ? 1 : 0))
1654 argstr[x++] = 'd';
1655 if (am_sender) {
1656 if (keep_dirlinks)
1657 argstr[x++] = 'K';
1658 if (prune_empty_dirs)
1659 argstr[x++] = 'm';
1660 if (omit_dir_times)
1661 argstr[x++] = 'O';
1662 } else {
1663 if (copy_links)
1664 argstr[x++] = 'L';
1665 if (copy_dirlinks)
1666 argstr[x++] = 'k';
1667 }
1668
1669 if (whole_file > 0)
1670 argstr[x++] = 'W';
1671 /* We don't need to send --no-whole-file, because it's the
1672 * default for remote transfers, and in any case old versions
1673 * of rsync will not understand it. */
1674
1675 if (preserve_hard_links) {
1676 argstr[x++] = 'H';
1677 if (preserve_hard_links > 1)
1678 argstr[x++] = 'H';
1679 }
1680 if (preserve_uid)
1681 argstr[x++] = 'o';
1682 if (preserve_gid)
1683 argstr[x++] = 'g';
1684 if (preserve_devices) /* ignore preserve_specials here */
1685 argstr[x++] = 'D';
1686 if (preserve_times)
1687 argstr[x++] = 't';
1688 if (preserve_perms)
1689 argstr[x++] = 'p';
1690 else if (preserve_executability && am_sender)
1691 argstr[x++] = 'E';
1692#ifdef SUPPORT_ACLS
1693 if (preserve_acls)
1694 argstr[x++] = 'A';
1695#endif
1696#ifdef SUPPORT_XATTRS
1697 if (preserve_xattrs) {
1698 argstr[x++] = 'X';
1699 if (preserve_xattrs > 1)
1700 argstr[x++] = 'X';
1701 }
1702#endif
1703 if (recurse)
1704 argstr[x++] = 'r';
1705 if (always_checksum)
1706 argstr[x++] = 'c';
1707 if (cvs_exclude)
1708 argstr[x++] = 'C';
1709 if (ignore_times)
1710 argstr[x++] = 'I';
1711 if (relative_paths)
1712 argstr[x++] = 'R';
1713 if (one_file_system) {
1714 argstr[x++] = 'x';
1715 if (one_file_system > 1)
1716 argstr[x++] = 'x';
1717 }
1718 if (sparse_files)
1719 argstr[x++] = 'S';
1720 if (do_compression)
1721 argstr[x++] = 'z';
1722
1723 /* This is a complete hack - blame Rusty. FIXME!
1724 * This hack is only needed for older rsync versions that
1725 * don't understand the --list-only option. */
1726 if (list_only == 1 && !recurse)
1727 argstr[x++] = 'r';
1728
1729#if SUBPROTOCOL_VERSION != 0
1730 /* If we're speaking a pre-release version of a protocol, we tell
1731 * the server about this by (ab)using the -e option. */
1732 if (protocol_version == PROTOCOL_VERSION) {
1733 x += snprintf(argstr+x, sizeof argstr - x,
1734 "e%d.%d", PROTOCOL_VERSION, SUBPROTOCOL_VERSION);
1735 }
1736#endif
1737
1738 argstr[x] = '\0';
1739
1740 if (x != 1)
1741 args[ac++] = argstr;
1742
1743 if (list_only > 1)
1744 args[ac++] = "--list-only";
1745
1746 /* This makes sure that the remote rsync can handle deleting with -d
1747 * sans -r because the --no-r option was added at the same time. */
1748 if (xfer_dirs && !recurse && delete_mode && am_sender)
1749 args[ac++] = "--no-r";
1750
1751 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
1752 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
1753 goto oom;
1754 args[ac++] = arg;
1755 }
1756
1757 if (preserve_devices) {
1758 /* Note: sending "--devices" would not be backward-compatible. */
1759 if (!preserve_specials)
1760 args[ac++] = "--no-specials"; /* -D is already set. */
1761 } else if (preserve_specials)
1762 args[ac++] = "--specials";
1763
1764 /* The server side doesn't use our log-format, but in certain
1765 * circumstances they need to know a little about the option. */
1766 if (stdout_format && am_sender) {
1767 /* Use --log-format, not --out-format, for compatibility. */
1768 if (stdout_format_has_i > 1)
1769 args[ac++] = "--log-format=%i%I";
1770 else if (stdout_format_has_i)
1771 args[ac++] = "--log-format=%i";
1772 else if (stdout_format_has_o_or_i)
1773 args[ac++] = "--log-format=%o";
1774 else if (!verbose)
1775 args[ac++] = "--log-format=X";
1776 }
1777
1778#ifdef ICONV_OPTION
1779 if (iconv_opt) {
1780 char *set = strchr(iconv_opt, ',');
1781 if (set)
1782 set++;
1783 else
1784 set = iconv_opt;
1785 if (asprintf(&arg, "--iconv=%s", set) < 0)
1786 goto oom;
1787 args[ac++] = arg;
1788 }
1789#endif
1790
1791 if (block_size) {
1792 if (asprintf(&arg, "-B%lu", block_size) < 0)
1793 goto oom;
1794 args[ac++] = arg;
1795 }
1796
1797 if (io_timeout) {
1798 if (asprintf(&arg, "--timeout=%d", io_timeout) < 0)
1799 goto oom;
1800 args[ac++] = arg;
1801 }
1802
1803 if (bwlimit) {
1804 if (asprintf(&arg, "--bwlimit=%d", bwlimit) < 0)
1805 goto oom;
1806 args[ac++] = arg;
1807 }
1808
1809 if (backup_dir) {
1810 args[ac++] = "--backup-dir";
1811 args[ac++] = backup_dir;
1812 }
1813
1814 /* Only send --suffix if it specifies a non-default value. */
1815 if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
1816 /* We use the following syntax to avoid weirdness with '~'. */
1817 if (asprintf(&arg, "--suffix=%s", backup_suffix) < 0)
1818 goto oom;
1819 args[ac++] = arg;
1820 }
1821
1822 if (am_sender) {
1823 if (max_delete > 0) {
1824 if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)
1825 goto oom;
1826 args[ac++] = arg;
1827 } else if (max_delete == 0)
1828 args[ac++] = "--max_delete=-1";
1829 if (min_size) {
1830 args[ac++] = "--min-size";
1831 args[ac++] = min_size_arg;
1832 }
1833 if (max_size) {
1834 args[ac++] = "--max-size";
1835 args[ac++] = max_size_arg;
1836 }
1837 if (delete_before)
1838 args[ac++] = "--delete-before";
1839 else if (delete_during == 2)
1840 args[ac++] = "--delete-delay";
1841 else if (delete_during)
1842 args[ac++] = "--delete-during";
1843 else if (delete_after)
1844 args[ac++] = "--delete-after";
1845 else if (delete_mode && !delete_excluded)
1846 args[ac++] = "--delete";
1847 if (delete_excluded)
1848 args[ac++] = "--delete-excluded";
1849 if (force_delete)
1850 args[ac++] = "--force";
1851 if (write_batch < 0)
1852 args[ac++] = "--only-write-batch=X";
1853 if (am_root > 1)
1854 args[ac++] = "--super";
1855 if (size_only)
1856 args[ac++] = "--size-only";
1857 } else {
1858 if (skip_compress) {
1859 if (asprintf(&arg, "--skip-compress=%s", skip_compress) < 0)
1860 goto oom;
1861 args[ac++] = arg;
1862 }
1863 }
1864
1865 if (modify_window_set) {
1866 if (asprintf(&arg, "--modify-window=%d", modify_window) < 0)
1867 goto oom;
1868 args[ac++] = arg;
1869 }
1870
1871 if (checksum_seed) {
1872 if (asprintf(&arg, "--checksum-seed=%d", checksum_seed) < 0)
1873 goto oom;
1874 args[ac++] = arg;
1875 }
1876
1877 if (partial_dir && am_sender) {
1878 if (partial_dir != tmp_partialdir) {
1879 args[ac++] = "--partial-dir";
1880 args[ac++] = partial_dir;
1881 }
1882 if (delay_updates)
1883 args[ac++] = "--delay-updates";
1884 } else if (keep_partial && am_sender)
1885 args[ac++] = "--partial";
1886
1887 if (ignore_errors)
1888 args[ac++] = "--ignore-errors";
1889
1890 if (copy_unsafe_links)
1891 args[ac++] = "--copy-unsafe-links";
1892
1893 if (safe_symlinks)
1894 args[ac++] = "--safe-links";
1895
1896 if (numeric_ids)
1897 args[ac++] = "--numeric-ids";
1898
1899 if (!allow_inc_recurse)
1900 args[ac++] = "--no-ir";
1901
1902 if (am_sender) {
1903 if (ignore_existing)
1904 args[ac++] = "--ignore-existing";
1905
1906 /* Backward compatibility: send --existing, not --ignore-non-existing. */
1907 if (ignore_non_existing)
1908 args[ac++] = "--existing";
1909
1910 if (tmpdir) {
1911 args[ac++] = "--temp-dir";
1912 args[ac++] = tmpdir;
1913 }
1914
1915 if (basis_dir[0]) {
1916 /* the server only needs this option if it is not the sender,
1917 * and it may be an older version that doesn't know this
1918 * option, so don't send it if client is the sender.
1919 */
1920 int i;
1921 for (i = 0; i < basis_dir_cnt; i++) {
1922 args[ac++] = dest_option;
1923 args[ac++] = basis_dir[i];
1924 }
1925 }
1926 }
1927
1928 if (append_mode) {
1929 if (append_mode > 1)
1930 args[ac++] = "--append";
1931 args[ac++] = "--append";
1932 } else if (inplace)
1933 args[ac++] = "--inplace";
1934
1935 if (files_from && (!am_sender || filesfrom_host)) {
1936 if (filesfrom_host) {
1937 args[ac++] = "--files-from";
1938 args[ac++] = files_from;
1939 if (eol_nulls)
1940 args[ac++] = "--from0";
1941 } else {
1942 args[ac++] = "--files-from=-";
1943 args[ac++] = "--from0";
1944 }
1945 if (!relative_paths)
1946 args[ac++] = "--no-relative";
1947 }
1948 if (relative_paths && !implied_dirs && !am_sender)
1949 args[ac++] = "--no-implied-dirs";
1950
1951 if (fuzzy_basis && am_sender)
1952 args[ac++] = "--fuzzy";
1953
1954 if (remove_source_files == 1)
1955 args[ac++] = "--remove-source-files";
1956 else if (remove_source_files)
1957 args[ac++] = "--remove-sent-files";
1958
1959 *argc = ac;
1960 return;
1961
1962 oom:
1963 out_of_memory("server_options");
1964}
1965
1966/* Look for a HOST specfication of the form "HOST:PATH", "HOST::PATH", or
1967 * "rsync://HOST:PORT/PATH". If found, *host_ptr will be set to some allocated
1968 * memory with the HOST. If a daemon-accessing spec was specified, the value
1969 * of *port_ptr will contain a non-0 port number, otherwise it will be set to
1970 * 0. The return value is a pointer to the PATH. Note that the HOST spec can
1971 * be an IPv6 literal address enclosed in '[' and ']' (such as "[::1]" or
1972 * "[::ffff:127.0.0.1]") which is returned without the '[' and ']'. */
1973char *check_for_hostspec(char *s, char **host_ptr, int *port_ptr)
1974{
1975 char *p;
1976 int not_host;
1977 int hostlen;
1978
1979 if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
1980 char *path;
1981 s += strlen(URL_PREFIX);
1982 if ((p = strchr(s, '/')) != NULL) {
1983 hostlen = p - s;
1984 path = p + 1;
1985 } else {
1986 hostlen = strlen(s);
1987 path = "";
1988 }
1989 if (*s == '[' && (p = strchr(s, ']')) != NULL) {
1990 s++;
1991 hostlen = p - s;
1992 if (p[1] == ':')
1993 *port_ptr = atoi(p+2);
1994 } else {
1995 if ((p = strchr(s, ':')) != NULL && p < s + hostlen) {
1996 hostlen = p - s;
1997 *port_ptr = atoi(p+1);
1998 }
1999 }
2000 if (!*port_ptr)
2001 *port_ptr = RSYNC_PORT;
2002 *host_ptr = new_array(char, hostlen + 1);
2003 strlcpy(*host_ptr, s, hostlen + 1);
2004 return path;
2005 }
2006
2007 if (*s == '[' && (p = strchr(s, ']')) != NULL && p[1] == ':') {
2008 s++;
2009 hostlen = p - s;
2010 *p = '\0';
2011 not_host = strchr(s, '/') || !strchr(s, ':');
2012 *p = ']';
2013 if (not_host)
2014 return NULL;
2015 p++;
2016 } else {
2017 if (!(p = strchr(s, ':')))
2018 return NULL;
2019 hostlen = p - s;
2020 *p = '\0';
2021 not_host = strchr(s, '/') != NULL;
2022 *p = ':';
2023 if (not_host)
2024 return NULL;
2025 }
2026
2027 *host_ptr = new_array(char, hostlen + 1);
2028 strlcpy(*host_ptr, s, hostlen + 1);
2029
2030 if (p[1] == ':') {
2031 if (port_ptr && !*port_ptr)
2032 *port_ptr = RSYNC_PORT;
2033 return p + 2;
2034 }
2035 if (port_ptr)
2036 *port_ptr = 0;
2037
2038 return p + 1;
2039}