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