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