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