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