d12d28a719d842d8af841563c3347ffc5189a75d
[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   {"update",          'u', POPT_ARG_NONE,   &update_only, 0, 0, 0 },
528   {"existing",         0,  POPT_ARG_NONE,   &ignore_non_existing, 0, 0, 0 },
529   {"ignore-non-existing",0,POPT_ARG_NONE,   &ignore_non_existing, 0, 0, 0 },
530   {"ignore-existing",  0,  POPT_ARG_NONE,   &ignore_existing, 0, 0, 0 },
531   {"max-size",         0,  POPT_ARG_STRING, &max_size_arg, OPT_MAX_SIZE, 0, 0 },
532   {"min-size",         0,  POPT_ARG_STRING, &min_size_arg, OPT_MIN_SIZE, 0, 0 },
533   {"sparse",          'S', POPT_ARG_NONE,   &sparse_files, 0, 0, 0 },
534   {"inplace",          0,  POPT_ARG_NONE,   &inplace, 0, 0, 0 },
535   {"append",           0,  POPT_ARG_NONE,   0, OPT_APPEND, 0, 0 },
536   {"append-verify",    0,  POPT_ARG_VAL,    &append_mode, 2, 0, 0 },
537   {"no-append",        0,  POPT_ARG_VAL,    &append_mode, 0, 0, 0 },
538   {"del",              0,  POPT_ARG_NONE,   &delete_during, 0, 0, 0 },
539   {"delete",           0,  POPT_ARG_NONE,   &delete_mode, 0, 0, 0 },
540   {"delete-before",    0,  POPT_ARG_NONE,   &delete_before, 0, 0, 0 },
541   {"delete-during",    0,  POPT_ARG_VAL,    &delete_during, 1, 0, 0 },
542   {"delete-delay",     0,  POPT_ARG_VAL,    &delete_during, 2, 0, 0 },
543   {"delete-after",     0,  POPT_ARG_NONE,   &delete_after, 0, 0, 0 },
544   {"delete-excluded",  0,  POPT_ARG_NONE,   &delete_excluded, 0, 0, 0 },
545   {"remove-sent-files",0,  POPT_ARG_VAL,    &remove_source_files, 2, 0, 0 }, /* deprecated */
546   {"remove-source-files",0,POPT_ARG_VAL,    &remove_source_files, 1, 0, 0 },
547   {"force",            0,  POPT_ARG_NONE,   &force_delete, 0, 0, 0 },
548   {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
549   {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
550   {0,                 'F', POPT_ARG_NONE,   0, 'F', 0, 0 },
551   {"filter",          'f', POPT_ARG_STRING, 0, OPT_FILTER, 0, 0 },
552   {"exclude",          0,  POPT_ARG_STRING, 0, OPT_EXCLUDE, 0, 0 },
553   {"include",          0,  POPT_ARG_STRING, 0, OPT_INCLUDE, 0, 0 },
554   {"exclude-from",     0,  POPT_ARG_STRING, 0, OPT_EXCLUDE_FROM, 0, 0 },
555   {"include-from",     0,  POPT_ARG_STRING, 0, OPT_INCLUDE_FROM, 0, 0 },
556   {"cvs-exclude",     'C', POPT_ARG_NONE,   &cvs_exclude, 0, 0, 0 },
557   {"whole-file",      'W', POPT_ARG_VAL,    &whole_file, 1, 0, 0 },
558   {"no-whole-file",    0,  POPT_ARG_VAL,    &whole_file, 0, 0, 0 },
559   {"no-W",             0,  POPT_ARG_VAL,    &whole_file, 0, 0, 0 },
560   {"checksum",        'c', POPT_ARG_VAL,    &always_checksum, 1, 0, 0 },
561   {"no-checksum",      0,  POPT_ARG_VAL,    &always_checksum, 0, 0, 0 },
562   {"no-c",             0,  POPT_ARG_VAL,    &always_checksum, 0, 0, 0 },
563   {"block-size",      'B', POPT_ARG_LONG,   &block_size, 0, 0, 0 },
564   {"compare-dest",     0,  POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
565   {"copy-dest",        0,  POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
566   {"link-dest",        0,  POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
567   {"fuzzy",           'y', POPT_ARG_NONE,   &fuzzy_basis, 0, 0, 0 },
568   {"compress",        'z', POPT_ARG_NONE,   0, 'z', 0, 0 },
569   {"no-compress",      0,  POPT_ARG_VAL,    &do_compression, 0, 0, 0 },
570   {"skip-compress",    0,  POPT_ARG_STRING, &skip_compress, 0, 0, 0 },
571   {"no-z",             0,  POPT_ARG_VAL,    &do_compression, 0, 0, 0 },
572   {"compress-level",   0,  POPT_ARG_INT,    &def_compress_level, 'z', 0, 0 },
573   {0,                 'P', POPT_ARG_NONE,   0, 'P', 0, 0 },
574   {"progress",         0,  POPT_ARG_VAL,    &do_progress, 1, 0, 0 },
575   {"no-progress",      0,  POPT_ARG_VAL,    &do_progress, 0, 0, 0 },
576   {"partial",          0,  POPT_ARG_VAL,    &keep_partial, 1, 0, 0 },
577   {"no-partial",       0,  POPT_ARG_VAL,    &keep_partial, 0, 0, 0 },
578   {"partial-dir",      0,  POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
579   {"delay-updates",    0,  POPT_ARG_VAL,    &delay_updates, 1, 0, 0 },
580   {"no-delay-updates", 0,  POPT_ARG_VAL,    &delay_updates, 0, 0, 0 },
581   {"prune-empty-dirs",'m', POPT_ARG_NONE,   &prune_empty_dirs, 0, 0, 0 },
582   {"log-file",         0,  POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
583   {"log-file-format",  0,  POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
584   {"out-format",       0,  POPT_ARG_STRING, &stdout_format, 0, 0, 0 },
585   {"log-format",       0,  POPT_ARG_STRING, &stdout_format, 0, 0, 0 }, /* DEPRECATED */
586   {"itemize-changes", 'i', POPT_ARG_NONE,   0, 'i', 0, 0 },
587   {"no-itemize-changes",0, POPT_ARG_VAL,    &itemize_changes, 0, 0, 0 },
588   {"no-i",             0,  POPT_ARG_VAL,    &itemize_changes, 0, 0, 0 },
589   {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
590   {"no-bwlimit",       0,  POPT_ARG_VAL,    &bwlimit, 0, 0, 0 },
591   {"backup",          'b', POPT_ARG_VAL,    &make_backups, 1, 0, 0 },
592   {"no-backup",        0,  POPT_ARG_VAL,    &make_backups, 0, 0, 0 },
593   {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
594   {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
595   {"list-only",        0,  POPT_ARG_VAL,    &list_only, 2, 0, 0 },
596   {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
597   {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
598   {"only-write-batch", 0,  POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
599   {"files-from",       0,  POPT_ARG_STRING, &files_from, 0, 0, 0 },
600   {"from0",           '0', POPT_ARG_VAL,    &eol_nulls, 1, 0, 0},
601   {"no-from0",         0,  POPT_ARG_VAL,    &eol_nulls, 0, 0, 0},
602   {"protect-args",    's', POPT_ARG_VAL,    &protect_args, 1, 0, 0},
603   {"no-protect-args",  0,  POPT_ARG_VAL,    &protect_args, 0, 0, 0},
604   {"no-s",             0,  POPT_ARG_VAL,    &protect_args, 0, 0, 0},
605   {"numeric-ids",      0,  POPT_ARG_VAL,    &numeric_ids, 1, 0, 0 },
606   {"no-numeric-ids",   0,  POPT_ARG_VAL,    &numeric_ids, 0, 0, 0 },
607   {"timeout",          0,  POPT_ARG_INT,    &io_timeout, 0, 0, 0 },
608   {"no-timeout",       0,  POPT_ARG_VAL,    &io_timeout, 0, 0, 0 },
609   {"contimeout",       0,  POPT_ARG_INT,    &connect_timeout, 0, 0, 0 },
610   {"rsh",             'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
611   {"rsync-path",       0,  POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
612   {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
613 #ifdef ICONV_OPTION
614   {"iconv",            0,  POPT_ARG_STRING, &iconv_opt, 0, 0, 0 },
615   {"no-iconv",         0,  POPT_ARG_NONE,   0, OPT_NO_ICONV, 0, 0 },
616 #endif
617   {"ipv4",            '4', POPT_ARG_VAL,    &default_af_hint, AF_INET, 0, 0 },
618   {"ipv6",            '6', POPT_ARG_VAL,    &default_af_hint, AF_INET6, 0, 0 },
619   {"8-bit-output",    '8', POPT_ARG_NONE,   &allow_8bit_chars, 0, 0, 0 },
620   {"qsort",            0,  POPT_ARG_NONE,   &use_qsort, 0, 0, 0 },
621   {"address",          0,  POPT_ARG_STRING, &bind_address, 0, 0, 0 },
622   {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
623   {"sockopts",         0,  POPT_ARG_STRING, &sockopts, 0, 0, 0 },
624   {"password-file",    0,  POPT_ARG_STRING, &password_file, 0, 0, 0 },
625   {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
626   {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
627   {"protocol",         0,  POPT_ARG_INT,    &protocol_version, 0, 0, 0 },
628   {"checksum-seed",    0,  POPT_ARG_INT,    &checksum_seed, 0, 0, 0 },
629   {"server",           0,  POPT_ARG_NONE,   0, OPT_SERVER, 0, 0 },
630   {"sender",           0,  POPT_ARG_NONE,   0, OPT_SENDER, 0, 0 },
631   /* All the following options switch us into daemon-mode option-parsing. */
632   {"config",           0,  POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
633   {"daemon",           0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
634   {"detach",           0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
635   {"no-detach",        0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
636   {0,0,0,0, 0, 0, 0}
637 };
638
639 static void daemon_usage(enum logcode F)
640 {
641   print_rsync_version(F);
642
643   rprintf(F,"\n");
644   rprintf(F,"Usage: rsync --daemon [OPTION]...\n");
645   rprintf(F,"     --address=ADDRESS       bind to the specified address\n");
646   rprintf(F,"     --bwlimit=KBPS          limit I/O bandwidth; KBytes per second\n");
647   rprintf(F,"     --config=FILE           specify alternate rsyncd.conf file\n");
648   rprintf(F,"     --no-detach             do not detach from the parent\n");
649   rprintf(F,"     --port=PORT             listen on alternate port number\n");
650   rprintf(F,"     --log-file=FILE         override the \"log file\" setting\n");
651   rprintf(F,"     --log-file-format=FMT   override the \"log format\" setting\n");
652   rprintf(F,"     --sockopts=OPTIONS      specify custom TCP options\n");
653   rprintf(F," -v, --verbose               increase verbosity\n");
654   rprintf(F," -4, --ipv4                  prefer IPv4\n");
655   rprintf(F," -6, --ipv6                  prefer IPv6\n");
656   rprintf(F,"     --help                  show this help screen\n");
657
658   rprintf(F,"\n");
659   rprintf(F,"If you were not trying to invoke rsync as a daemon, avoid using any of the\n");
660   rprintf(F,"daemon-specific rsync options.  See also the rsyncd.conf(5) man page.\n");
661 }
662
663 static struct poptOption long_daemon_options[] = {
664   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
665   {"address",          0,  POPT_ARG_STRING, &bind_address, 0, 0, 0 },
666   {"bwlimit",          0,  POPT_ARG_INT,    &daemon_bwlimit, 0, 0, 0 },
667   {"config",           0,  POPT_ARG_STRING, &config_file, 0, 0, 0 },
668   {"daemon",           0,  POPT_ARG_NONE,   &daemon_opt, 0, 0, 0 },
669   {"ipv4",            '4', POPT_ARG_VAL,    &default_af_hint, AF_INET, 0, 0 },
670   {"ipv6",            '6', POPT_ARG_VAL,    &default_af_hint, AF_INET6, 0, 0 },
671   {"detach",           0,  POPT_ARG_VAL,    &no_detach, 0, 0, 0 },
672   {"no-detach",        0,  POPT_ARG_VAL,    &no_detach, 1, 0, 0 },
673   {"log-file",         0,  POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
674   {"log-file-format",  0,  POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
675   {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
676   {"sockopts",         0,  POPT_ARG_STRING, &sockopts, 0, 0, 0 },
677   {"protocol",         0,  POPT_ARG_INT,    &protocol_version, 0, 0, 0 },
678   {"server",           0,  POPT_ARG_NONE,   &am_server, 0, 0, 0 },
679   {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
680   {"verbose",         'v', POPT_ARG_NONE,   0, 'v', 0, 0 },
681   {"no-verbose",       0,  POPT_ARG_VAL,    &verbose, 0, 0, 0 },
682   {"no-v",             0,  POPT_ARG_VAL,    &verbose, 0, 0, 0 },
683   {"help",            'h', POPT_ARG_NONE,   0, 'h', 0, 0 },
684   {0,0,0,0, 0, 0, 0}
685 };
686
687
688 static char err_buf[200];
689
690
691 /**
692  * Store the option error message, if any, so that we can log the
693  * connection attempt (which requires parsing the options), and then
694  * show the error later on.
695  **/
696 void option_error(void)
697 {
698         if (!err_buf[0]) {
699                 strlcpy(err_buf, "Error parsing options: option may "
700                         "be supported on client but not on server?\n",
701                         sizeof err_buf);
702         }
703
704         rprintf(FERROR, RSYNC_NAME ": %s", err_buf);
705         msleep(20);
706 }
707
708
709 /**
710  * Tweak the option table to disable all options that the rsyncd.conf
711  * file has told us to refuse.
712  **/
713 static void set_refuse_options(char *bp)
714 {
715         struct poptOption *op;
716         char *cp, shortname[2];
717         int is_wild, found_match;
718
719         shortname[1] = '\0';
720
721         while (1) {
722                 while (*bp == ' ') bp++;
723                 if (!*bp)
724                         break;
725                 if ((cp = strchr(bp, ' ')) != NULL)
726                         *cp= '\0';
727                 is_wild = strpbrk(bp, "*?[") != NULL;
728                 found_match = 0;
729                 for (op = long_options; ; op++) {
730                         *shortname = op->shortName;
731                         if (!op->longName && !*shortname)
732                                 break;
733                         if ((op->longName && wildmatch(bp, op->longName))
734                             || (*shortname && wildmatch(bp, shortname))) {
735                                 if (op->argInfo == POPT_ARG_VAL)
736                                         op->argInfo = POPT_ARG_NONE;
737                                 op->val = (op - long_options) + OPT_REFUSED_BASE;
738                                 found_match = 1;
739                                 /* These flags are set to let us easily check
740                                  * an implied option later in the code. */
741                                 switch (*shortname) {
742                                 case 'r': case 'd': case 'l': case 'p':
743                                 case 't': case 'g': case 'o': case 'D':
744                                         refused_archive_part = op->val;
745                                         break;
746                                 case 'z':
747                                         refused_compress = op->val;
748                                         break;
749                                 case '\0':
750                                         if (wildmatch("delete", op->longName))
751                                                 refused_delete = op->val;
752                                         else if (wildmatch("delete-before", op->longName))
753                                                 refused_delete_before = op->val;
754                                         else if (wildmatch("delete-during", op->longName))
755                                                 refused_delete_during = op->val;
756                                         else if (wildmatch("partial", op->longName))
757                                                 refused_partial = op->val;
758                                         else if (wildmatch("progress", op->longName))
759                                                 refused_progress = op->val;
760                                         else if (wildmatch("inplace", op->longName))
761                                                 refused_inplace = op->val;
762                                         else if (wildmatch("no-iconv", op->longName))
763                                                 refused_no_iconv = op->val;
764                                         break;
765                                 }
766                                 if (!is_wild)
767                                         break;
768                         }
769                 }
770                 if (!found_match) {
771                         rprintf(FLOG, "No match for refuse-options string \"%s\"\n",
772                                 bp);
773                 }
774                 if (!cp)
775                         break;
776                 *cp = ' ';
777                 bp = cp + 1;
778         }
779 }
780
781
782 static int count_args(const char **argv)
783 {
784         int i = 0;
785
786         if (argv) {
787                 while (argv[i] != NULL)
788                         i++;
789         }
790
791         return i;
792 }
793
794
795 static OFF_T parse_size_arg(char **size_arg, char def_suf)
796 {
797         int reps, mult, make_compatible = 0;
798         const char *arg;
799         OFF_T size = 1;
800
801         for (arg = *size_arg; isDigit(arg); arg++) {}
802         if (*arg == '.')
803                 for (arg++; isDigit(arg); arg++) {}
804         switch (*arg && *arg != '+' && *arg != '-' ? *arg++ : def_suf) {
805         case 'b': case 'B':
806                 reps = 0;
807                 break;
808         case 'k': case 'K':
809                 reps = 1;
810                 break;
811         case 'm': case 'M':
812                 reps = 2;
813                 break;
814         case 'g': case 'G':
815                 reps = 3;
816                 break;
817         default:
818                 return -1;
819         }
820         if (*arg == 'b' || *arg == 'B')
821                 mult = 1000, make_compatible = 1, arg++;
822         else if (!*arg || *arg == '+' || *arg == '-')
823                 mult = 1024;
824         else if (strncasecmp(arg, "ib", 2) == 0)
825                 mult = 1024, arg += 2;
826         else
827                 return -1;
828         while (reps--)
829                 size *= mult;
830         size *= atof(*size_arg);
831         if ((*arg == '+' || *arg == '-') && arg[1] == '1')
832                 size += atoi(arg), make_compatible = 1, arg += 2;
833         if (*arg)
834                 return -1;
835         if (size > 0 && make_compatible) {
836                 /* We convert this manually because we may need %lld precision,
837                  * and that's not a portable sprintf() escape. */
838                 char buf[128], *s = buf + sizeof buf - 1;
839                 OFF_T num = size;
840                 *s = '\0';
841                 while (num) {
842                         *--s = (char)(num % 10) + '0';
843                         num /= 10;
844                 }
845                 if (!(*size_arg = strdup(s)))
846                         out_of_memory("parse_size_arg");
847         }
848         return size;
849 }
850
851
852 static void create_refuse_error(int which)
853 {
854         /* The "which" value is the index + OPT_REFUSED_BASE. */
855         struct poptOption *op = &long_options[which - OPT_REFUSED_BASE];
856         int n = snprintf(err_buf, sizeof err_buf,
857                          "The server is configured to refuse --%s\n",
858                          op->longName) - 1;
859         if (op->shortName) {
860                 snprintf(err_buf + n, sizeof err_buf - n,
861                          " (-%c)\n", op->shortName);
862         }
863 }
864
865
866 /**
867  * Process command line arguments.  Called on both local and remote.
868  *
869  * @retval 1 if all options are OK; with globals set to appropriate
870  * values
871  *
872  * @retval 0 on error, with err_buf containing an explanation
873  **/
874 int parse_arguments(int *argc_p, const char ***argv_p, int frommain)
875 {
876         static poptContext pc;
877         char *ref = lp_refuse_options(module_id);
878         const char *arg, **argv = *argv_p;
879         int argc = *argc_p;
880         int opt;
881
882         if (ref && *ref)
883                 set_refuse_options(ref);
884         if (am_daemon) {
885                 set_refuse_options("log-file*");
886                 if (!*lp_charset(module_id))
887                         set_refuse_options("iconv");
888         }
889
890 #ifdef ICONV_OPTION
891         if (!am_daemon && !protect_args && (arg = getenv("RSYNC_ICONV")) != NULL && *arg)
892                 iconv_opt = strdup(arg);
893 #endif
894
895         /* TODO: Call poptReadDefaultConfig; handle errors. */
896
897         /* The context leaks in case of an error, but if there's a
898          * problem we always exit anyhow. */
899         if (pc)
900                 poptFreeContext(pc);
901         pc = poptGetContext(RSYNC_NAME, argc, argv, long_options, 0);
902         if (!am_server)
903                 poptReadDefaultConfig(pc, 0);
904
905         while ((opt = poptGetNextOpt(pc)) != -1) {
906                 /* most options are handled automatically by popt;
907                  * only special cases are returned and listed here. */
908
909                 switch (opt) {
910                 case OPT_VERSION:
911                         print_rsync_version(FINFO);
912                         exit_cleanup(0);
913
914                 case OPT_SERVER:
915                         if (!am_server) {
916                                 /* Disable popt aliases on the server side and
917                                  * then start parsing the options again. */
918                                 poptFreeContext(pc);
919                                 pc = poptGetContext(RSYNC_NAME, argc, argv,
920                                                     long_options, 0);
921                                 am_server = 1;
922                         }
923 #ifdef ICONV_OPTION
924                         iconv_opt = NULL;
925 #endif
926                         break;
927
928                 case OPT_SENDER:
929                         if (!am_server) {
930                                 usage(FERROR);
931                                 exit_cleanup(RERR_SYNTAX);
932                         }
933                         am_sender = 1;
934                         break;
935
936                 case OPT_DAEMON:
937                         if (am_daemon) {
938                                 strlcpy(err_buf,
939                                         "Attempt to hack rsync thwarted!\n",
940                                         sizeof err_buf);
941                                 return 0;
942                         }
943 #ifdef ICONV_OPTION
944                         iconv_opt = NULL;
945 #endif
946                         poptFreeContext(pc);
947                         pc = poptGetContext(RSYNC_NAME, argc, argv,
948                                             long_daemon_options, 0);
949                         while ((opt = poptGetNextOpt(pc)) != -1) {
950                                 switch (opt) {
951                                 case 'h':
952                                         daemon_usage(FINFO);
953                                         exit_cleanup(0);
954
955                                 case 'v':
956                                         verbose++;
957                                         break;
958
959                                 default:
960                                         rprintf(FERROR,
961                                             "rsync: %s: %s (in daemon mode)\n",
962                                             poptBadOption(pc, POPT_BADOPTION_NOALIAS),
963                                             poptStrerror(opt));
964                                         goto daemon_error;
965                                 }
966                         }
967
968                         if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
969                                 snprintf(err_buf, sizeof err_buf,
970                                          "the --temp-dir path is WAY too long.\n");
971                                 return 0;
972                         }
973
974                         if (!daemon_opt) {
975                                 rprintf(FERROR, "Daemon option(s) used without --daemon.\n");
976                             daemon_error:
977                                 rprintf(FERROR,
978                                     "(Type \"rsync --daemon --help\" for assistance with daemon mode.)\n");
979                                 exit_cleanup(RERR_SYNTAX);
980                         }
981
982                         *argv_p = argv = poptGetArgs(pc);
983                         *argc_p = argc = count_args(argv);
984                         am_starting_up = 0;
985                         daemon_opt = 0;
986                         am_daemon = 1;
987                         return 1;
988
989                 case OPT_MODIFY_WINDOW:
990                         /* The value has already been set by popt, but
991                          * we need to remember that we're using a
992                          * non-default setting. */
993                         modify_window_set = 1;
994                         break;
995
996                 case OPT_FILTER:
997                         parse_rule(&filter_list, poptGetOptArg(pc), 0, 0);
998                         break;
999
1000                 case OPT_EXCLUDE:
1001                         parse_rule(&filter_list, poptGetOptArg(pc),
1002                                    0, XFLG_OLD_PREFIXES);
1003                         break;
1004
1005                 case OPT_INCLUDE:
1006                         parse_rule(&filter_list, poptGetOptArg(pc),
1007                                    MATCHFLG_INCLUDE, XFLG_OLD_PREFIXES);
1008                         break;
1009
1010                 case OPT_EXCLUDE_FROM:
1011                 case OPT_INCLUDE_FROM:
1012                         arg = poptGetOptArg(pc);
1013                         if (sanitize_paths)
1014                                 arg = sanitize_path(NULL, arg, NULL, 0);
1015                         if (server_filter_list.head) {
1016                                 int rej;
1017                                 char *dir, *cp = strdup(arg);
1018                                 if (!cp)
1019                                         out_of_memory("parse_arguments");
1020                                 if (!*cp)
1021                                         goto options_rejected;
1022                                 dir = cp + (*cp == '/' ? module_dirlen : 0);
1023                                 clean_fname(dir, CFN_COLLAPSE_DOT_DOT_DIRS);
1024                                 rej = check_filter(&server_filter_list, dir, 0) < 0;
1025                                 free(cp);
1026                                 if (rej)
1027                                         goto options_rejected;
1028                         }
1029                         parse_filter_file(&filter_list, arg,
1030                                 opt == OPT_INCLUDE_FROM ? MATCHFLG_INCLUDE : 0,
1031                                 XFLG_FATAL_ERRORS | XFLG_OLD_PREFIXES);
1032                         break;
1033
1034                 case 'a':
1035                         if (refused_archive_part) {
1036                                 create_refuse_error(refused_archive_part);
1037                                 return 0;
1038                         }
1039                         if (!recurse) /* preserve recurse == 2 */
1040                                 recurse = 1;
1041 #ifdef SUPPORT_LINKS
1042                         preserve_links = 1;
1043 #endif
1044                         preserve_perms = 1;
1045                         preserve_times = 2;
1046                         preserve_gid = 1;
1047                         preserve_uid = 1;
1048                         preserve_devices = 1;
1049                         preserve_specials = 1;
1050                         break;
1051
1052                 case 'D':
1053                         preserve_devices = preserve_specials = 1;
1054                         break;
1055
1056                 case OPT_NO_D:
1057                         preserve_devices = preserve_specials = 0;
1058                         break;
1059
1060                 case 'h':
1061                         human_readable++;
1062                         break;
1063
1064                 case 'H':
1065                         preserve_hard_links++;
1066                         break;
1067
1068                 case 'i':
1069                         itemize_changes++;
1070                         break;
1071
1072                 case 'v':
1073                         verbose++;
1074                         break;
1075
1076                 case 'q':
1077                         if (frommain)
1078                                 quiet++;
1079                         break;
1080
1081                 case 'x':
1082                         one_file_system++;
1083                         break;
1084
1085                 case 'F':
1086                         switch (++F_option_cnt) {
1087                         case 1:
1088                                 parse_rule(&filter_list,": /.rsync-filter",0,0);
1089                                 break;
1090                         case 2:
1091                                 parse_rule(&filter_list,"- .rsync-filter",0,0);
1092                                 break;
1093                         }
1094                         break;
1095
1096                 case 'P':
1097                         if (refused_partial || refused_progress) {
1098                                 create_refuse_error(refused_partial
1099                                     ? refused_partial : refused_progress);
1100                                 return 0;
1101                         }
1102                         do_progress = 1;
1103                         keep_partial = 1;
1104                         break;
1105
1106                 case 'z':
1107                         if (def_compress_level < Z_DEFAULT_COMPRESSION
1108                          || def_compress_level > Z_BEST_COMPRESSION) {
1109                                 snprintf(err_buf, sizeof err_buf,
1110                                         "--compress-level value is invalid: %d\n",
1111                                         def_compress_level);
1112                                 return 0;
1113                         }
1114                         do_compression = def_compress_level != Z_NO_COMPRESSION;
1115                         if (do_compression && refused_compress) {
1116                                 create_refuse_error(refused_compress);
1117                                 return 0;
1118                         }
1119                         break;
1120
1121                 case OPT_WRITE_BATCH:
1122                         /* batch_name is already set */
1123                         write_batch = 1;
1124                         break;
1125
1126                 case OPT_ONLY_WRITE_BATCH:
1127                         /* batch_name is already set */
1128                         write_batch = -1;
1129                         break;
1130
1131                 case OPT_READ_BATCH:
1132                         /* batch_name is already set */
1133                         read_batch = 1;
1134                         break;
1135
1136                 case OPT_NO_ICONV:
1137                         iconv_opt = NULL;
1138                         break;
1139
1140                 case OPT_MAX_SIZE:
1141                         if ((max_size = parse_size_arg(&max_size_arg, 'b')) <= 0) {
1142                                 snprintf(err_buf, sizeof err_buf,
1143                                         "--max-size value is invalid: %s\n",
1144                                         max_size_arg);
1145                                 return 0;
1146                         }
1147                         break;
1148
1149                 case OPT_MIN_SIZE:
1150                         if ((min_size = parse_size_arg(&min_size_arg, 'b')) <= 0) {
1151                                 snprintf(err_buf, sizeof err_buf,
1152                                         "--min-size value is invalid: %s\n",
1153                                         min_size_arg);
1154                                 return 0;
1155                         }
1156                         break;
1157
1158                 case OPT_APPEND:
1159                         if (am_server)
1160                                 append_mode++;
1161                         else
1162                                 append_mode = 1;
1163                         break;
1164
1165                 case OPT_LINK_DEST:
1166 #ifdef SUPPORT_HARD_LINKS
1167                         link_dest = 1;
1168                         dest_option = "--link-dest";
1169                         goto set_dest_dir;
1170 #else
1171                         snprintf(err_buf, sizeof err_buf,
1172                                  "hard links are not supported on this %s\n",
1173                                  am_server ? "server" : "client");
1174                         return 0;
1175 #endif
1176
1177                 case OPT_COPY_DEST:
1178                         copy_dest = 1;
1179                         dest_option = "--copy-dest";
1180                         goto set_dest_dir;
1181
1182                 case OPT_COMPARE_DEST:
1183                         compare_dest = 1;
1184                         dest_option = "--compare-dest";
1185                 set_dest_dir:
1186                         if (basis_dir_cnt >= MAX_BASIS_DIRS) {
1187                                 snprintf(err_buf, sizeof err_buf,
1188                                         "ERROR: at most %d %s args may be specified\n",
1189                                         MAX_BASIS_DIRS, dest_option);
1190                                 return 0;
1191                         }
1192                         /* We defer sanitizing this arg until we know what
1193                          * our destination directory is going to be. */
1194                         basis_dir[basis_dir_cnt++] = (char *)poptGetOptArg(pc);
1195                         break;
1196
1197                 case OPT_CHMOD:
1198                         arg = poptGetOptArg(pc);
1199                         if (!parse_chmod(arg, &chmod_modes)) {
1200                                 snprintf(err_buf, sizeof err_buf,
1201                                     "Invalid argument passed to --chmod (%s)\n",
1202                                     arg);
1203                                 return 0;
1204                         }
1205                         break;
1206
1207                 case OPT_HELP:
1208                         usage(FINFO);
1209                         exit_cleanup(0);
1210
1211                 case 'A':
1212 #ifdef SUPPORT_ACLS
1213                         preserve_acls = 1;
1214                         preserve_perms = 1;
1215                         break;
1216 #else
1217                         /* FIXME: this should probably be ignored with a
1218                          * warning and then countermeasures taken to
1219                          * restrict group and other access in the presence
1220                          * of any more restrictive ACLs, but this is safe
1221                          * for now */
1222                         snprintf(err_buf,sizeof(err_buf),
1223                                  "ACLs are not supported on this %s\n",
1224                                  am_server ? "server" : "client");
1225                         return 0;
1226 #endif
1227
1228                 case 'X':
1229 #ifdef SUPPORT_XATTRS
1230                         preserve_xattrs++;
1231                         break;
1232 #else
1233                         snprintf(err_buf,sizeof(err_buf),
1234                                  "extended attributes are not supported on this %s\n",
1235                                  am_server ? "server" : "client");
1236                         return 0;
1237 #endif
1238
1239                 default:
1240                         /* A large opt value means that set_refuse_options()
1241                          * turned this option off. */
1242                         if (opt >= OPT_REFUSED_BASE) {
1243                                 create_refuse_error(opt);
1244                                 return 0;
1245                         }
1246                         snprintf(err_buf, sizeof err_buf, "%s%s: %s\n",
1247                                  am_server ? "on remote machine: " : "",
1248                                  poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1249                                  poptStrerror(opt));
1250                         return 0;
1251                 }
1252         }
1253
1254         if (human_readable && argc == 2) {
1255                 /* Allow the old meaning of 'h' (--help) on its own. */
1256                 usage(FINFO);
1257                 exit_cleanup(0);
1258         }
1259
1260 #ifdef ICONV_OPTION
1261         if (iconv_opt && protect_args != 2) {
1262                 if (!am_server && strcmp(iconv_opt, "-") == 0)
1263                         iconv_opt = NULL;
1264                 else
1265                         need_unsorted_flist = 1;
1266         }
1267         if (refused_no_iconv && !iconv_opt) {
1268                 create_refuse_error(refused_no_iconv);
1269                 return 0;
1270         }
1271 #endif
1272
1273         if (protect_args == 1) {
1274                 if (!frommain)
1275                         protect_args = 0;
1276                 else if (am_server)
1277                         return 1;
1278         }
1279
1280 #ifndef SUPPORT_LINKS
1281         if (preserve_links && !am_sender) {
1282                 snprintf(err_buf, sizeof err_buf,
1283                          "symlinks are not supported on this %s\n",
1284                          am_server ? "server" : "client");
1285                 return 0;
1286         }
1287 #endif
1288
1289 #ifndef SUPPORT_HARD_LINKS
1290         if (preserve_hard_links) {
1291                 snprintf(err_buf, sizeof err_buf,
1292                          "hard links are not supported on this %s\n",
1293                          am_server ? "server" : "client");
1294                 return 0;
1295         }
1296 #endif
1297
1298 #ifndef SUPPORT_XATTRS
1299         if (am_root < 0) {
1300                 snprintf(err_buf, sizeof err_buf,
1301                          "--fake-super requires an rsync with extended attributes enabled\n");
1302                 return 0;
1303         }
1304 #endif
1305
1306         if (write_batch && read_batch) {
1307                 snprintf(err_buf, sizeof err_buf,
1308                         "--write-batch and --read-batch can not be used together\n");
1309                 return 0;
1310         }
1311         if (write_batch > 0 || read_batch) {
1312                 if (am_server) {
1313                         rprintf(FINFO,
1314                                 "ignoring --%s-batch option sent to server\n",
1315                                 write_batch ? "write" : "read");
1316                         /* We don't actually exit_cleanup(), so that we can
1317                          * still service older version clients that still send
1318                          * batch args to server. */
1319                         read_batch = write_batch = 0;
1320                         batch_name = NULL;
1321                 } else if (dry_run)
1322                         write_batch = 0;
1323         } else if (write_batch < 0 && dry_run)
1324                 write_batch = 0;
1325         if (read_batch && files_from) {
1326                 snprintf(err_buf, sizeof err_buf,
1327                         "--read-batch cannot be used with --files-from\n");
1328                 return 0;
1329         }
1330         if (batch_name && strlen(batch_name) > MAX_BATCH_NAME_LEN) {
1331                 snprintf(err_buf, sizeof err_buf,
1332                         "the batch-file name must be %d characters or less.\n",
1333                         MAX_BATCH_NAME_LEN);
1334                 return 0;
1335         }
1336
1337         if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
1338                 snprintf(err_buf, sizeof err_buf,
1339                          "the --temp-dir path is WAY too long.\n");
1340                 return 0;
1341         }
1342
1343         if (max_delete < 0 && max_delete != INT_MIN) {
1344                 /* Negative numbers are treated as "no deletions". */
1345                 max_delete = 0;
1346         }
1347
1348         if (compare_dest + copy_dest + link_dest > 1) {
1349                 snprintf(err_buf, sizeof err_buf,
1350                         "You may not mix --compare-dest, --copy-dest, and --link-dest.\n");
1351                 return 0;
1352         }
1353
1354         if (files_from) {
1355                 if (recurse == 1) /* preserve recurse == 2 */
1356                         recurse = 0;
1357                 if (xfer_dirs < 0)
1358                         xfer_dirs = 1;
1359         }
1360
1361         if (xfer_dirs < 1)
1362                 xfer_dirs = recurse || list_only;
1363
1364         if (relative_paths < 0)
1365                 relative_paths = files_from? 1 : 0;
1366         if (!relative_paths)
1367                 implied_dirs = 0;
1368
1369         if (delete_before + !!delete_during + delete_after > 1) {
1370                 snprintf(err_buf, sizeof err_buf,
1371                         "You may not combine multiple --delete-WHEN options.\n");
1372                 return 0;
1373         }
1374         if (delete_before || delete_during || delete_after)
1375                 delete_mode = 1;
1376         else if (delete_mode || delete_excluded) {
1377                 /* Only choose now between before & during if one is refused. */
1378                 if (refused_delete_before) {
1379                         if (!refused_delete_during)
1380                                 delete_during = 1;
1381                         else {
1382                                 create_refuse_error(refused_delete_before);
1383                                 return 0;
1384                         }
1385                 } else if (refused_delete_during)
1386                         delete_before = 1;
1387                 delete_mode = 1;
1388         }
1389         if (!xfer_dirs && delete_mode) {
1390                 snprintf(err_buf, sizeof err_buf,
1391                         "--delete does not work without -r or -d.\n");
1392                 return 0;
1393         }
1394
1395         if (delete_mode && refused_delete) {
1396                 create_refuse_error(refused_delete);
1397                 return 0;
1398         }
1399
1400         if (remove_source_files) {
1401                 /* We only want to infer this refusal of --remove-source-files
1402                  * via the refusal of "delete", not any of the "delete-FOO"
1403                  * options. */
1404                 if (refused_delete && am_sender) {
1405                         create_refuse_error(refused_delete);
1406                         return 0;
1407                 }
1408                 need_messages_from_generator = 1;
1409         }
1410
1411         *argv_p = argv = poptGetArgs(pc);
1412         *argc_p = argc = count_args(argv);
1413
1414         if (sanitize_paths) {
1415                 int i;
1416                 for (i = argc; i-- > 0; )
1417                         argv[i] = sanitize_path(NULL, argv[i], "", 0);
1418                 if (tmpdir)
1419                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0);
1420                 if (backup_dir)
1421                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
1422         }
1423         if (server_filter_list.head && !am_sender) {
1424                 struct filter_list_struct *elp = &server_filter_list;
1425                 if (tmpdir) {
1426                         char *dir;
1427                         if (!*tmpdir)
1428                                 goto options_rejected;
1429                         dir = tmpdir + (*tmpdir == '/' ? module_dirlen : 0);
1430                         clean_fname(dir, CFN_COLLAPSE_DOT_DOT_DIRS);
1431                         if (check_filter(elp, dir, 1) < 0)
1432                                 goto options_rejected;
1433                 }
1434                 if (backup_dir) {
1435                         char *dir;
1436                         if (!*backup_dir)
1437                                 goto options_rejected;
1438                         dir = backup_dir + (*backup_dir == '/' ? module_dirlen : 0);
1439                         clean_fname(dir, CFN_COLLAPSE_DOT_DOT_DIRS);
1440                         if (check_filter(elp, dir, 1) < 0)
1441                                 goto options_rejected;
1442                 }
1443         }
1444
1445         if (!backup_suffix)
1446                 backup_suffix = backup_dir ? "" : BACKUP_SUFFIX;
1447         backup_suffix_len = strlen(backup_suffix);
1448         if (strchr(backup_suffix, '/') != NULL) {
1449                 snprintf(err_buf, sizeof err_buf,
1450                         "--suffix cannot contain slashes: %s\n",
1451                         backup_suffix);
1452                 return 0;
1453         }
1454         if (backup_dir) {
1455                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
1456                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
1457                 if (backup_dir_remainder < 32) {
1458                         snprintf(err_buf, sizeof err_buf,
1459                                 "the --backup-dir path is WAY too long.\n");
1460                         return 0;
1461                 }
1462                 if (backup_dir_buf[backup_dir_len - 1] != '/') {
1463                         backup_dir_buf[backup_dir_len++] = '/';
1464                         backup_dir_buf[backup_dir_len] = '\0';
1465                 }
1466                 if (verbose > 1 && !am_sender)
1467                         rprintf(FINFO, "backup_dir is %s\n", backup_dir_buf);
1468         } else if (!backup_suffix_len && (!am_server || !am_sender)) {
1469                 snprintf(err_buf, sizeof err_buf,
1470                         "--suffix cannot be a null string without --backup-dir\n");
1471                 return 0;
1472         } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
1473                 snprintf(backup_dir_buf, sizeof backup_dir_buf,
1474                         "P *%s", backup_suffix);
1475                 parse_rule(&filter_list, backup_dir_buf, 0, 0);
1476         }
1477
1478         if (make_backups && !backup_dir) {
1479                 omit_dir_times = 0; /* Implied, so avoid -O to sender. */
1480                 if (preserve_times > 1)
1481                         preserve_times = 1;
1482         } else if (omit_dir_times) {
1483                 if (preserve_times > 1)
1484                         preserve_times = 1;
1485         }
1486
1487         if (stdout_format) {
1488                 if (am_server && log_format_has(stdout_format, 'I'))
1489                         stdout_format_has_i = 2;
1490                 else if (log_format_has(stdout_format, 'i'))
1491                         stdout_format_has_i = itemize_changes | 1;
1492                 if (!log_format_has(stdout_format, 'b')
1493                  && !log_format_has(stdout_format, 'c'))
1494                         log_before_transfer = !am_server;
1495         } else if (itemize_changes) {
1496                 stdout_format = "%i %n%L";
1497                 stdout_format_has_i = itemize_changes;
1498                 log_before_transfer = !am_server;
1499         }
1500
1501         if (do_progress && !verbose && !log_before_transfer && !am_server)
1502                 verbose = 1;
1503
1504         if (dry_run)
1505                 do_xfers = 0;
1506
1507         set_io_timeout(io_timeout);
1508
1509         if (verbose && !stdout_format) {
1510                 stdout_format = "%n%L";
1511                 log_before_transfer = !am_server;
1512         }
1513         if (stdout_format_has_i || log_format_has(stdout_format, 'o'))
1514                 stdout_format_has_o_or_i = 1;
1515
1516         if (logfile_name && !am_daemon) {
1517                 if (!logfile_format) {
1518                         logfile_format = "%i %n%L";
1519                         logfile_format_has_i = logfile_format_has_o_or_i = 1;
1520                 } else {
1521                         if (log_format_has(logfile_format, 'i'))
1522                                 logfile_format_has_i = 1;
1523                         if (logfile_format_has_i || log_format_has(logfile_format, 'o'))
1524                                 logfile_format_has_o_or_i = 1;
1525                 }
1526                 log_init(0);
1527         } else if (!am_daemon)
1528                 logfile_format = NULL;
1529
1530         if (daemon_bwlimit && (!bwlimit || bwlimit > daemon_bwlimit))
1531                 bwlimit = daemon_bwlimit;
1532         if (bwlimit) {
1533                 bwlimit_writemax = (size_t)bwlimit * 128;
1534                 if (bwlimit_writemax < 512)
1535                         bwlimit_writemax = 512;
1536         }
1537
1538         if (sparse_files && inplace) {
1539                 /* Note: we don't check for this below, because --append is
1540                  * OK with --sparse (as long as redos are handled right). */
1541                 snprintf(err_buf, sizeof err_buf,
1542                          "--sparse cannot be used with --inplace\n");
1543                 return 0;
1544         }
1545
1546         if (append_mode) {
1547                 if (whole_file > 0) {
1548                         snprintf(err_buf, sizeof err_buf,
1549                                  "--append cannot be used with --whole-file\n");
1550                         return 0;
1551                 }
1552                 if (refused_inplace) {
1553                         create_refuse_error(refused_inplace);
1554                         return 0;
1555                 }
1556                 inplace = 1;
1557         }
1558
1559         if (delay_updates && !partial_dir)
1560                 partial_dir = tmp_partialdir;
1561
1562         if (inplace) {
1563 #ifdef HAVE_FTRUNCATE
1564                 if (partial_dir) {
1565                         snprintf(err_buf, sizeof err_buf,
1566                                  "--%s cannot be used with --%s\n",
1567                                  append_mode ? "append" : "inplace",
1568                                  delay_updates ? "delay-updates" : "partial-dir");
1569                         return 0;
1570                 }
1571                 /* --inplace implies --partial for refusal purposes, but we
1572                  * clear the keep_partial flag for internal logic purposes. */
1573                 if (refused_partial) {
1574                         create_refuse_error(refused_partial);
1575                         return 0;
1576                 }
1577                 keep_partial = 0;
1578 #else
1579                 snprintf(err_buf, sizeof err_buf,
1580                          "--%s is not supported on this %s\n",
1581                          append_mode ? "append" : "inplace",
1582                          am_server ? "server" : "client");
1583                 return 0;
1584 #endif
1585         } else {
1586                 if (keep_partial && !partial_dir && !am_server) {
1587                         if ((arg = getenv("RSYNC_PARTIAL_DIR")) != NULL && *arg)
1588                                 partial_dir = strdup(arg);
1589                 }
1590                 if (partial_dir) {
1591                         if (*partial_dir)
1592                                 clean_fname(partial_dir, CFN_COLLAPSE_DOT_DOT_DIRS);
1593                         if (!*partial_dir || strcmp(partial_dir, ".") == 0)
1594                                 partial_dir = NULL;
1595                         if (!partial_dir && refused_partial) {
1596                                 create_refuse_error(refused_partial);
1597                                 return 0;
1598                         }
1599                         keep_partial = 1;
1600                 }
1601         }
1602
1603         if (files_from) {
1604                 char *h, *p;
1605                 int q;
1606                 if (argc > 2 || (!am_daemon && argc == 1)) {
1607                         usage(FERROR);
1608                         exit_cleanup(RERR_SYNTAX);
1609                 }
1610                 if (strcmp(files_from, "-") == 0) {
1611                         filesfrom_fd = 0;
1612                         if (am_server)
1613                                 filesfrom_host = ""; /* reading from socket */
1614                 } else if ((p = check_for_hostspec(files_from, &h, &q)) != 0) {
1615                         if (am_server) {
1616                                 snprintf(err_buf, sizeof err_buf,
1617                                         "The --files-from sent to the server cannot specify a host.\n");
1618                                 return 0;
1619                         }
1620                         files_from = p;
1621                         filesfrom_host = h;
1622                         if (strcmp(files_from, "-") == 0) {
1623                                 snprintf(err_buf, sizeof err_buf,
1624                                         "Invalid --files-from remote filename\n");
1625                                 return 0;
1626                         }
1627                 } else {
1628                         if (sanitize_paths)
1629                                 files_from = sanitize_path(NULL, files_from, NULL, 0);
1630                         if (server_filter_list.head) {
1631                                 char *dir;
1632                                 if (!*files_from)
1633                                         goto options_rejected;
1634                                 dir = files_from + (*files_from == '/' ? module_dirlen : 0);
1635                                 clean_fname(dir, CFN_COLLAPSE_DOT_DOT_DIRS);
1636                                 if (check_filter(&server_filter_list, dir, 0) < 0)
1637                                         goto options_rejected;
1638                         }
1639                         filesfrom_fd = open(files_from, O_RDONLY|O_BINARY);
1640                         if (filesfrom_fd < 0) {
1641                                 snprintf(err_buf, sizeof err_buf,
1642                                         "failed to open files-from file %s: %s\n",
1643                                         files_from, strerror(errno));
1644                                 return 0;
1645                         }
1646                 }
1647         }
1648
1649         am_starting_up = 0;
1650
1651         return 1;
1652
1653   options_rejected:
1654         snprintf(err_buf, sizeof err_buf,
1655                 "Your options have been rejected by the server.\n");
1656         return 0;
1657 }
1658
1659
1660 /**
1661  * Construct a filtered list of options to pass through from the
1662  * client to the server.
1663  *
1664  * This involves setting options that will tell the server how to
1665  * behave, and also filtering out options that are processed only
1666  * locally.
1667  **/
1668 void server_options(char **args, int *argc_p)
1669 {
1670         static char argstr[64];
1671         int ac = *argc_p;
1672         char *arg;
1673         int i, x;
1674
1675         /* This should always remain first on the server's command-line. */
1676         args[ac++] = "--server";
1677
1678         if (daemon_over_rsh > 0) {
1679                 args[ac++] = "--daemon";
1680                 *argc_p = ac;
1681                 /* if we're passing --daemon, we're done */
1682                 return;
1683         }
1684
1685         if (!am_sender)
1686                 args[ac++] = "--sender";
1687
1688         x = 1;
1689         argstr[0] = '-';
1690
1691         if (protect_args)
1692                 argstr[x++] = 's';
1693
1694         for (i = 0; i < verbose; i++)
1695                 argstr[x++] = 'v';
1696
1697         /* the -q option is intentionally left out */
1698         if (make_backups)
1699                 argstr[x++] = 'b';
1700         if (update_only)
1701                 argstr[x++] = 'u';
1702         if (!do_xfers) /* Note: NOT "dry_run"! */
1703                 argstr[x++] = 'n';
1704         if (preserve_links)
1705                 argstr[x++] = 'l';
1706         if ((list_only && !recurse) || xfer_dirs > 1
1707          || (xfer_dirs && !recurse && delete_mode && am_sender))
1708                 argstr[x++] = 'd';
1709         if (am_sender) {
1710                 if (keep_dirlinks)
1711                         argstr[x++] = 'K';
1712                 if (prune_empty_dirs)
1713                         argstr[x++] = 'm';
1714                 if (omit_dir_times)
1715                         argstr[x++] = 'O';
1716         } else {
1717                 if (copy_links)
1718                         argstr[x++] = 'L';
1719                 if (copy_dirlinks)
1720                         argstr[x++] = 'k';
1721         }
1722
1723         if (whole_file > 0)
1724                 argstr[x++] = 'W';
1725         /* We don't need to send --no-whole-file, because it's the
1726          * default for remote transfers, and in any case old versions
1727          * of rsync will not understand it. */
1728
1729         if (preserve_hard_links) {
1730                 argstr[x++] = 'H';
1731                 if (preserve_hard_links > 1)
1732                         argstr[x++] = 'H';
1733         }
1734         if (preserve_uid)
1735                 argstr[x++] = 'o';
1736         if (preserve_gid)
1737                 argstr[x++] = 'g';
1738         if (preserve_devices) /* ignore preserve_specials here */
1739                 argstr[x++] = 'D';
1740         if (preserve_times)
1741                 argstr[x++] = 't';
1742         if (preserve_perms)
1743                 argstr[x++] = 'p';
1744         else if (preserve_executability && am_sender)
1745                 argstr[x++] = 'E';
1746 #ifdef SUPPORT_ACLS
1747         if (preserve_acls)
1748                 argstr[x++] = 'A';
1749 #endif
1750 #ifdef SUPPORT_XATTRS
1751         if (preserve_xattrs) {
1752                 argstr[x++] = 'X';
1753                 if (preserve_xattrs > 1)
1754                         argstr[x++] = 'X';
1755         }
1756 #endif
1757         if (recurse)
1758                 argstr[x++] = 'r';
1759         if (always_checksum)
1760                 argstr[x++] = 'c';
1761         if (cvs_exclude)
1762                 argstr[x++] = 'C';
1763         if (ignore_times)
1764                 argstr[x++] = 'I';
1765         if (relative_paths)
1766                 argstr[x++] = 'R';
1767         if (one_file_system) {
1768                 argstr[x++] = 'x';
1769                 if (one_file_system > 1)
1770                         argstr[x++] = 'x';
1771         }
1772         if (sparse_files)
1773                 argstr[x++] = 'S';
1774         if (do_compression)
1775                 argstr[x++] = 'z';
1776
1777         /* We make use of the -e option to let the server know about any
1778          * pre-release protocol version && our allow_inc_recurse status. */
1779         set_allow_inc_recurse();
1780 #if SUBPROTOCOL_VERSION != 0
1781         if (protocol_version == PROTOCOL_VERSION) {
1782                 x += snprintf(argstr+x, sizeof argstr - x,
1783                               "e%d.%d%s", PROTOCOL_VERSION, SUBPROTOCOL_VERSION,
1784                               allow_inc_recurse ? "i" : "");
1785         } else
1786 #endif
1787         if (allow_inc_recurse) {
1788                 argstr[x++] = 'e';
1789                 argstr[x++] = 'i';
1790         }
1791
1792         argstr[x] = '\0';
1793
1794         if (x != 1)
1795                 args[ac++] = argstr;
1796
1797 #ifdef ICONV_OPTION
1798         if (iconv_opt) {
1799                 char *set = strchr(iconv_opt, ',');
1800                 if (set)
1801                         set++;
1802                 else
1803                         set = iconv_opt;
1804                 if (asprintf(&arg, "--iconv=%s", set) < 0)
1805                         goto oom;
1806                 args[ac++] = arg;
1807         }
1808 #endif
1809
1810         if (protect_args) /* initial args break here */
1811                 args[ac++] = NULL;
1812
1813         if (list_only > 1)
1814                 args[ac++] = "--list-only";
1815
1816         /* This makes sure that the remote rsync can handle deleting with -d
1817          * sans -r because the --no-r option was added at the same time. */
1818         if (xfer_dirs && !recurse && delete_mode && am_sender)
1819                 args[ac++] = "--no-r";
1820
1821         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
1822                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
1823                         goto oom;
1824                 args[ac++] = arg;
1825         }
1826
1827         if (preserve_devices) {
1828                 /* Note: sending "--devices" would not be backward-compatible. */
1829                 if (!preserve_specials)
1830                         args[ac++] = "--no-specials"; /* -D is already set. */
1831         } else if (preserve_specials)
1832                 args[ac++] = "--specials";
1833
1834         /* The server side doesn't use our log-format, but in certain
1835          * circumstances they need to know a little about the option. */
1836         if (stdout_format && am_sender) {
1837                 /* Use --log-format, not --out-format, for compatibility. */
1838                 if (stdout_format_has_i > 1)
1839                         args[ac++] = "--log-format=%i%I";
1840                 else if (stdout_format_has_i)
1841                         args[ac++] = "--log-format=%i";
1842                 else if (stdout_format_has_o_or_i)
1843                         args[ac++] = "--log-format=%o";
1844                 else if (!verbose)
1845                         args[ac++] = "--log-format=X";
1846         }
1847
1848         if (block_size) {
1849                 if (asprintf(&arg, "-B%lu", block_size) < 0)
1850                         goto oom;
1851                 args[ac++] = arg;
1852         }
1853
1854         if (io_timeout) {
1855                 if (asprintf(&arg, "--timeout=%d", io_timeout) < 0)
1856                         goto oom;
1857                 args[ac++] = arg;
1858         }
1859
1860         if (bwlimit) {
1861                 if (asprintf(&arg, "--bwlimit=%d", bwlimit) < 0)
1862                         goto oom;
1863                 args[ac++] = arg;
1864         }
1865
1866         if (backup_dir) {
1867                 args[ac++] = "--backup-dir";
1868                 args[ac++] = backup_dir;
1869         }
1870
1871         /* Only send --suffix if it specifies a non-default value. */
1872         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
1873                 /* We use the following syntax to avoid weirdness with '~'. */
1874                 if (asprintf(&arg, "--suffix=%s", backup_suffix) < 0)
1875                         goto oom;
1876                 args[ac++] = arg;
1877         }
1878
1879         if (am_sender) {
1880                 if (max_delete > 0) {
1881                         if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)
1882                                 goto oom;
1883                         args[ac++] = arg;
1884                 } else if (max_delete == 0)
1885                         args[ac++] = "--max-delete=-1";
1886                 if (min_size) {
1887                         args[ac++] = "--min-size";
1888                         args[ac++] = min_size_arg;
1889                 }
1890                 if (max_size) {
1891                         args[ac++] = "--max-size";
1892                         args[ac++] = max_size_arg;
1893                 }
1894                 if (delete_before)
1895                         args[ac++] = "--delete-before";
1896                 else if (delete_during == 2)
1897                         args[ac++] = "--delete-delay";
1898                 else if (delete_during)
1899                         args[ac++] = "--delete-during";
1900                 else if (delete_after)
1901                         args[ac++] = "--delete-after";
1902                 else if (delete_mode && !delete_excluded)
1903                         args[ac++] = "--delete";
1904                 if (delete_excluded)
1905                         args[ac++] = "--delete-excluded";
1906                 if (force_delete)
1907                         args[ac++] = "--force";
1908                 if (write_batch < 0)
1909                         args[ac++] = "--only-write-batch=X";
1910                 if (am_root > 1)
1911                         args[ac++] = "--super";
1912                 if (size_only)
1913                         args[ac++] = "--size-only";
1914         } else {
1915                 if (skip_compress) {
1916                         if (asprintf(&arg, "--skip-compress=%s", skip_compress) < 0)
1917                                 goto oom;
1918                         args[ac++] = arg;
1919                 }
1920         }
1921
1922         if (modify_window_set) {
1923                 if (asprintf(&arg, "--modify-window=%d", modify_window) < 0)
1924                         goto oom;
1925                 args[ac++] = arg;
1926         }
1927
1928         if (checksum_seed) {
1929                 if (asprintf(&arg, "--checksum-seed=%d", checksum_seed) < 0)
1930                         goto oom;
1931                 args[ac++] = arg;
1932         }
1933
1934         if (partial_dir && am_sender) {
1935                 if (partial_dir != tmp_partialdir) {
1936                         args[ac++] = "--partial-dir";
1937                         args[ac++] = partial_dir;
1938                 }
1939                 if (delay_updates)
1940                         args[ac++] = "--delay-updates";
1941         } else if (keep_partial && am_sender)
1942                 args[ac++] = "--partial";
1943
1944         if (ignore_errors)
1945                 args[ac++] = "--ignore-errors";
1946
1947         if (copy_unsafe_links)
1948                 args[ac++] = "--copy-unsafe-links";
1949
1950         if (safe_symlinks)
1951                 args[ac++] = "--safe-links";
1952
1953         if (numeric_ids)
1954                 args[ac++] = "--numeric-ids";
1955
1956         if (use_qsort)
1957                 args[ac++] = "--use-qsort";
1958
1959         if (am_sender) {
1960                 if (ignore_existing)
1961                         args[ac++] = "--ignore-existing";
1962
1963                 /* Backward compatibility: send --existing, not --ignore-non-existing. */
1964                 if (ignore_non_existing)
1965                         args[ac++] = "--existing";
1966
1967                 if (tmpdir) {
1968                         args[ac++] = "--temp-dir";
1969                         args[ac++] = tmpdir;
1970                 }
1971
1972                 if (basis_dir[0]) {
1973                         /* the server only needs this option if it is not the sender,
1974                          *   and it may be an older version that doesn't know this
1975                          *   option, so don't send it if client is the sender.
1976                          */
1977                         int i;
1978                         for (i = 0; i < basis_dir_cnt; i++) {
1979                                 args[ac++] = dest_option;
1980                                 args[ac++] = basis_dir[i];
1981                         }
1982                 }
1983         }
1984
1985         if (append_mode) {
1986                 if (append_mode > 1)
1987                         args[ac++] = "--append";
1988                 args[ac++] = "--append";
1989         } else if (inplace)
1990                 args[ac++] = "--inplace";
1991
1992         if (files_from && (!am_sender || filesfrom_host)) {
1993                 if (filesfrom_host) {
1994                         args[ac++] = "--files-from";
1995                         args[ac++] = files_from;
1996                         if (eol_nulls)
1997                                 args[ac++] = "--from0";
1998                 } else {
1999                         args[ac++] = "--files-from=-";
2000                         args[ac++] = "--from0";
2001                 }
2002                 if (!relative_paths)
2003                         args[ac++] = "--no-relative";
2004         }
2005         /* It's OK that this checks the upper-bound of the protocol_version. */
2006         if (relative_paths && !implied_dirs && (!am_sender || protocol_version >= 30))
2007                 args[ac++] = "--no-implied-dirs";
2008
2009         if (fuzzy_basis && am_sender)
2010                 args[ac++] = "--fuzzy";
2011
2012         if (remove_source_files == 1)
2013                 args[ac++] = "--remove-source-files";
2014         else if (remove_source_files)
2015                 args[ac++] = "--remove-sent-files";
2016
2017         *argc_p = ac;
2018         return;
2019
2020     oom:
2021         out_of_memory("server_options");
2022 }
2023
2024 /* Look for a HOST specfication of the form "HOST:PATH", "HOST::PATH", or
2025  * "rsync://HOST:PORT/PATH".  If found, *host_ptr will be set to some allocated
2026  * memory with the HOST.  If a daemon-accessing spec was specified, the value
2027  * of *port_ptr will contain a non-0 port number, otherwise it will be set to
2028  * 0.  The return value is a pointer to the PATH.  Note that the HOST spec can
2029  * be an IPv6 literal address enclosed in '[' and ']' (such as "[::1]" or
2030  * "[::ffff:127.0.0.1]") which is returned without the '[' and ']'. */
2031 char *check_for_hostspec(char *s, char **host_ptr, int *port_ptr)
2032 {
2033         char *p;
2034         int not_host;
2035         int hostlen;
2036
2037         if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
2038                 char *path;
2039                 s += strlen(URL_PREFIX);
2040                 if ((p = strchr(s, '/')) != NULL) {
2041                         hostlen = p - s;
2042                         path = p + 1;
2043                 } else {
2044                         hostlen = strlen(s);
2045                         path = "";
2046                 }
2047                 if (*s == '[' && (p = strchr(s, ']')) != NULL) {
2048                         s++;
2049                         hostlen = p - s;
2050                         if (p[1] == ':')
2051                                 *port_ptr = atoi(p+2);
2052                 } else {
2053                         if ((p = strchr(s, ':')) != NULL && p < s + hostlen) {
2054                                 hostlen = p - s;
2055                                 *port_ptr = atoi(p+1);
2056                         }
2057                 }
2058                 if (!*port_ptr)
2059                         *port_ptr = RSYNC_PORT;
2060                 *host_ptr = new_array(char, hostlen + 1);
2061                 strlcpy(*host_ptr, s, hostlen + 1);
2062                 return path;
2063         }
2064
2065         if (*s == '[' && (p = strchr(s, ']')) != NULL && p[1] == ':') {
2066                 s++;
2067                 hostlen = p - s;
2068                 *p = '\0';
2069                 not_host = strchr(s, '/') || !strchr(s, ':');
2070                 *p = ']';
2071                 if (not_host)
2072                         return NULL;
2073                 p++;
2074         } else {
2075                 if (!(p = strchr(s, ':')))
2076                         return NULL;
2077                 hostlen = p - s;
2078                 *p = '\0';
2079                 not_host = strchr(s, '/') != NULL;
2080                 *p = ':';
2081                 if (not_host)
2082                         return NULL;
2083         }
2084
2085         *host_ptr = new_array(char, hostlen + 1);
2086         strlcpy(*host_ptr, s, hostlen + 1);
2087
2088         if (p[1] == ':') {
2089                 if (port_ptr && !*port_ptr)
2090                         *port_ptr = RSYNC_PORT;
2091                 return p + 2;
2092         }
2093         if (port_ptr)
2094                 *port_ptr = 0;
2095
2096         return p + 1;
2097 }