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