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