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