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