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