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