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