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