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