Debian Codegraph @

options.c

raw symbols SCIP
1 /*
2 * Command-line (and received via daemon-socket) option parsing.
3 *
4 * Copyright (C) 1998-2001 Andrew Tridgell <[email protected]>
5 * Copyright (C) 2000, 2001, 2002 Martin Pool <[email protected]>
6 * Copyright (C) 2002-2023 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22 #include "rsync.h"
23 #include "itypes.h"
24 #include "ifuncs.h"
25 #include <popt.h>
26
27 extern int module_id;
28 extern int local_server;
29 extern int sanitize_paths;
30 extern int operator_path_resolve;
31 extern int trust_sender_args;
32 extern int trust_sender_filter;
33 extern unsigned int module_dirlen;
34 extern filter_rule_list filter_list;
35 extern filter_rule_list daemon_filter_list;
36
37 int make_backups = 0;
38
39 /**
40 * If 1, send the whole file as literal data rather than trying to
41 * create an incremental diff.
42 *
43 * If -1, then look at whether we're local or remote and go by that.
44 *
45 * @sa disable_deltas_p()
46 **/
47 int whole_file = -1;
48
49 int append_mode = 0;
50 int keep_dirlinks = 0;
51 int copy_dirlinks = 0;
52 int copy_links = 0;
53 int copy_devices = 0;
54 int write_devices = 0;
55 int preserve_links = 0;
56 int preserve_hard_links = 0;
57 int preserve_acls = 0;
58 int preserve_xattrs = 0;
59 int preserve_perms = 0;
60 int preserve_executability = 0;
61 int preserve_devices = 0;
62 int preserve_specials = 0;
63 int drop_devices = 0;
64 char *confine_root = NULL; /* --confine-root: see syscall.c */
65 unsigned int confine_rootlen = 0;
66 int preserve_uid = 0;
67 int preserve_gid = 0;
68 int preserve_mtimes = 0;
69 int preserve_atimes = 0;
70 int preserve_crtimes = 0;
71 int omit_dir_times = 0;
72 int omit_link_times = 0;
73 int trust_sender = 0;
74 int update_only = 0;
75 int open_noatime = 0;
76 int cvs_exclude = 0;
77 int dry_run = 0;
78 int do_xfers = 1;
79 int do_fsync = 0;
80 int ignore_times = 0;
81 int delete_mode = 0;
82 int delete_during = 0;
83 int delete_before = 0;
84 int delete_after = 0;
85 int delete_excluded = 0;
86 int remove_source_files = 0;
87 int one_file_system = 0;
88 int protocol_version = PROTOCOL_VERSION;
89 int sparse_files = 0;
90 int preallocate_files = 0;
91 int do_compression = 0;
92 int do_compression_level = CLVL_NOT_SPECIFIED;
93 int do_compression_threads = 0; /*n = 0 use rsync thread, n >= 1 spawn n threads for compression */
94 #define MAX_DAEMON_COMPRESSION_THREADS 8
95 int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */
96 int am_server = 0;
97 int am_sender = 0;
98 int am_starting_up = 1;
99 int relative_paths = -1;
100 int implied_dirs = 1;
101 int missing_args = 0; /* 0 = FERROR_XFER, 1 = ignore, 2 = delete */
102 int numeric_ids = 0;
103 int msgs2stderr = 2; /* Default: send errors to stderr for local & remote-shell transfers */
104 int saw_stderr_opt = 0;
105 int allow_8bit_chars = 0;
106 int force_delete = 0;
107 int io_timeout = 0;
108 int prune_empty_dirs = 0;
109 int use_qsort = 0;
110 char *files_from = NULL;
111 int filesfrom_fd = -1;
112 char *filesfrom_host = NULL;
113 int eol_nulls = 0;
114 int protect_args = -1;
115 int old_style_args = -1;
116 int human_readable = 1;
117 int recurse = 0;
118 int mkpath_dest_arg = 0;
119 int allow_inc_recurse = 1;
120 int xfer_dirs = -1;
121 int am_daemon = 0;
122 /* Set after a successful per-module chroot ("use chroot = yes") in
123 * clientserver.c. NOT set for the daemon-level "daemon chroot = /X"
124 * chroot: that confines path resolution to /X, but module paths
125 * /X/modA, /X/modB, etc. are not chroot boundaries, so the per-module
126 * symlink-race defenses (secure_relative_open() / do_*_at() in
127 * syscall.c, gated by `am_daemon && !am_chrooted`) must still fire
128 * even when the daemon is inside a daemon chroot. */
129 int am_chrooted = 0;
130 int connect_timeout = 0;
131 int keep_partial = 0;
132 int safe_symlinks = 0;
133 int copy_unsafe_links = 0;
134 int insecure_links = 0;
135 int munge_symlinks = 0;
136 int use_secure_symlinks = 0;
137 int size_only = 0;
138 int daemon_bwlimit = 0;
139 int bwlimit = 0;
140 int fuzzy_basis = 0;
141 size_t bwlimit_writemax = 0;
142 int ignore_existing = 0;
143 int ignore_non_existing = 0;
144 int need_messages_from_generator = 0;
145 int max_delete = INT_MIN;
146 OFF_T max_size = -1;
147 OFF_T min_size = -1;
148 int ignore_errors = 0;
149 int modify_window = 0;
150 int blocking_io = -1;
151 int checksum_seed = 0;
152 int inplace = 0;
153 int delay_updates = 0;
154 int32 block_size = 0;
155 time_t stop_at_utime = 0;
156 char *skip_compress = NULL;
157 char *copy_as = NULL;
158 item_list dparam_list = EMPTY_ITEM_LIST;
159
160 /** Network address family. **/
161 int default_af_hint
162 #ifdef INET6
163 = 0; /* Any protocol */
164 #else
165 = AF_INET; /* Must use IPv4 */
166 # ifdef AF_INET6
167 # undef AF_INET6
168 # endif
169 # define AF_INET6 AF_INET /* make -6 option a no-op */
170 #endif
171
172 /** Do not go into the background when run as --daemon. Good
173 * for debugging and required for running as a service on W32,
174 * or under Unix process-monitors. **/
175 int no_detach
176 #if defined _WIN32 || defined __WIN32__
177 = 1;
178 #else
179 = 0;
180 #endif
181
182 int write_batch = 0;
183 int read_batch = 0;
184 int backup_dir_len = 0;
185 int backup_suffix_len;
186 unsigned int backup_dir_remainder;
187
188 char *backup_suffix = NULL;
189 char *tmpdir = NULL;
190 char *partial_dir = NULL;
191 char *basis_dir[MAX_BASIS_DIRS+1];
192 char *config_file = NULL;
193 char *shell_cmd = NULL;
194 char *logfile_name = NULL;
195 char *logfile_format = NULL;
196 char *stdout_format = NULL;
197 char *password_file = NULL;
198 char *early_input_file = NULL;
199 char *rsync_path = RSYNC_PATH;
200 char *backup_dir = NULL;
201 char backup_dir_buf[MAXPATHLEN];
202 char *sockopts = NULL;
203 char *usermap = NULL;
204 char *groupmap = NULL;
205 int rsync_port = 0;
206 int alt_dest_type = 0;
207 int basis_dir_cnt = 0;
208
209 #define DEFAULT_MAX_ALLOC (1024L * 1024 * 1024)
210 size_t max_alloc = DEFAULT_MAX_ALLOC;
211 char *max_alloc_arg;
212
213 static int version_opt_cnt = 0;
214 static int remote_option_alloc = 0;
215 int remote_option_cnt = 0;
216 const char **remote_options = NULL;
217 const char *checksum_choice = NULL;
218 const char *compress_choice = NULL;
219 static const char *empty_argv[1];
220
221 int quiet = 0;
222 int output_motd = 1;
223 int log_before_transfer = 0;
224 int stdout_format_has_i = 0;
225 int stdout_format_has_o_or_i = 0;
226 int logfile_format_has_i = 0;
227 int logfile_format_has_o_or_i = 0;
228 int always_checksum = 0;
229 int list_only = 0;
230
231 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
232 char *batch_name = NULL;
233
234 int need_unsorted_flist = 0;
235 char *iconv_opt =
236 #ifdef ICONV_OPTION
237 ICONV_OPTION;
238 #else
239 NULL;
240 #endif
241
242 struct chmod_mode_struct *chmod_modes = NULL;
243
244 static const char *const debug_verbosity[] = {
245 /*0*/ NULL,
246 /*1*/ NULL,
247 /*2*/ "BIND,CMD,CONNECT,DEL,DELTASUM,DUP,FILTER,FLIST,ICONV",
248 /*3*/ "ACL,BACKUP,CONNECT2,DELTASUM2,DEL2,EXIT,FILTER2,FLIST2,FUZZY,GENR,OWN,RECV,SEND,TIME",
249 /*4*/ "CMD2,DELTASUM3,DEL3,EXIT2,FLIST3,ICONV2,OWN2,PROTO,TIME2",
250 /*5*/ "CHDIR,DELTASUM4,FLIST4,FUZZY2,HASH,HLINK",
251 };
252
253 #define MAX_VERBOSITY ((int)(sizeof debug_verbosity / sizeof debug_verbosity[0]) - 1)
254
255 static const char *const info_verbosity[1+MAX_VERBOSITY] = {
256 /*0*/ "NONREG",
257 /*1*/ "COPY,DEL,FLIST,MISC,NAME,STATS,SYMSAFE",
258 /*2*/ "BACKUP,MISC2,MOUNT,NAME2,REMOVE,SKIP",
259 };
260
261 #define MAX_OUT_LEVEL 4 /* The largest N allowed for any flagN word. */
262
263 short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG];
264
265 #define DEFAULT_PRIORITY 0 /* Default/implied/--verbose set values. */
266 #define HELP_PRIORITY 1 /* The help output uses this level. */
267 #define USER_PRIORITY 2 /* User-specified via --info or --debug */
268 #define LIMIT_PRIORITY 3 /* Overriding priority when limiting values. */
269
270 #define W_CLI (1<<0) /* client side */
271 #define W_SRV (1<<1) /* server side */
272 #define W_SND (1<<2) /* sending side */
273 #define W_REC (1<<3) /* receiving side */
274
275 struct output_struct {
276 char *name; /* The name of the info/debug flag. */
277 char *help; /* The description of the info/debug flag. */
278 uchar namelen; /* The length of the name string. */
279 uchar flag; /* The flag's value, for consistency check. */
280 uchar where; /* Bits indicating where the flag is used. */
281 uchar priority; /* See *_PRIORITY defines. */
282 };
283
284 #define INFO_WORD(flag, where, help) { #flag, help, sizeof #flag - 1, INFO_##flag, where, 0 }
285
286 static struct output_struct info_words[COUNT_INFO+1] = {
287 INFO_WORD(BACKUP, W_REC, "Mention files backed up"),
288 INFO_WORD(COPY, W_REC, "Mention files copied locally on the receiving side"),
289 INFO_WORD(DEL, W_REC, "Mention deletions on the receiving side"),
290 INFO_WORD(FLIST, W_CLI, "Mention file-list receiving/sending (levels 1-2)"),
291 INFO_WORD(MISC, W_SND|W_REC, "Mention miscellaneous information (levels 1-2)"),
292 INFO_WORD(MOUNT, W_SND|W_REC, "Mention mounts that were found or skipped"),
293 INFO_WORD(NAME, W_SND|W_REC, "Mention 1) updated file/dir names, 2) unchanged names"),
294 INFO_WORD(NONREG, W_REC, "Mention skipped non-regular files (default 1, 0 disables)"),
295 INFO_WORD(PROGRESS, W_CLI, "Mention 1) per-file progress or 2) total transfer progress"),
296 INFO_WORD(REMOVE, W_SND, "Mention files removed on the sending side"),
297 INFO_WORD(SKIP, W_REC, "Mention files skipped due to transfer overrides (levels 1-2)"),
298 INFO_WORD(STATS, W_CLI|W_SRV, "Mention statistics at end of run (levels 1-3)"),
299 INFO_WORD(SYMSAFE, W_SND|W_REC, "Mention symlinks that are unsafe"),
300 { NULL, "--info", 0, 0, 0, 0 }
301 };
302
303 #define DEBUG_WORD(flag, where, help) { #flag, help, sizeof #flag - 1, DEBUG_##flag, where, 0 }
304
305 static struct output_struct debug_words[COUNT_DEBUG+1] = {
306 DEBUG_WORD(ACL, W_SND|W_REC, "Debug extra ACL info"),
307 DEBUG_WORD(BACKUP, W_REC, "Debug backup actions (levels 1-2)"),
308 DEBUG_WORD(BIND, W_CLI, "Debug socket bind actions"),
309 DEBUG_WORD(CHDIR, W_CLI|W_SRV, "Debug when the current directory changes"),
310 DEBUG_WORD(CONNECT, W_CLI, "Debug connection events (levels 1-2)"),
311 DEBUG_WORD(CMD, W_CLI, "Debug commands+options that are issued (levels 1-2)"),
312 DEBUG_WORD(DEL, W_REC, "Debug delete actions (levels 1-3)"),
313 DEBUG_WORD(DELTASUM, W_SND|W_REC, "Debug delta-transfer checksumming (levels 1-4)"),
314 DEBUG_WORD(DUP, W_REC, "Debug weeding of duplicate names"),
315 DEBUG_WORD(EXIT, W_CLI|W_SRV, "Debug exit events (levels 1-3)"),
316 DEBUG_WORD(FILTER, W_SND|W_REC, "Debug filter actions (levels 1-3)"),
317 DEBUG_WORD(FLIST, W_SND|W_REC, "Debug file-list operations (levels 1-4)"),
318 DEBUG_WORD(FUZZY, W_REC, "Debug fuzzy scoring (levels 1-2)"),
319 DEBUG_WORD(GENR, W_REC, "Debug generator functions"),
320 DEBUG_WORD(HASH, W_SND|W_REC, "Debug hashtable code"),
321 DEBUG_WORD(HLINK, W_SND|W_REC, "Debug hard-link actions (levels 1-3)"),
322 DEBUG_WORD(ICONV, W_CLI|W_SRV, "Debug iconv character conversions (levels 1-2)"),
323 DEBUG_WORD(IO, W_CLI|W_SRV, "Debug I/O routines (levels 1-4)"),
324 DEBUG_WORD(NSTR, W_CLI|W_SRV, "Debug negotiation strings"),
325 DEBUG_WORD(OWN, W_REC, "Debug ownership changes in users & groups (levels 1-2)"),
326 DEBUG_WORD(PROTO, W_CLI|W_SRV, "Debug protocol information"),
327 DEBUG_WORD(RECV, W_REC, "Debug receiver functions"),
328 DEBUG_WORD(SEND, W_SND, "Debug sender functions"),
329 DEBUG_WORD(TIME, W_REC, "Debug setting of modified times (levels 1-2)"),
330 { NULL, "--debug", 0, 0, 0, 0 }
331 };
332
333 static int verbose = 0;
334 static int do_stats = 0;
335 static int do_progress = 0;
336 static int daemon_opt; /* sets am_daemon after option error-reporting */
337 static int F_option_cnt = 0;
338 static int modify_window_set;
339 static int itemize_changes = 0;
340 static int refused_delete, refused_archive_part, refused_compress;
341 static int refused_partial, refused_progress, refused_delete_before;
342 static int refused_delete_during;
343 static int refused_inplace, refused_no_iconv;
344 static BOOL usermap_via_chown, groupmap_via_chown;
345 static char *outbuf_mode;
346 static char *bwlimit_arg, *max_size_arg, *min_size_arg;
347 static char tmp_partialdir[] = ".~tmp~";
348
349 /** Local address to bind. As a character string because it's
350 * interpreted by the IPv6 layer: should be a numeric IP4 or IP6
351 * address, or a hostname. **/
352 char *bind_address;
353
354 static void output_item_help(struct output_struct *words);
355
356 /* This constructs a string that represents all the options set for either
357 * the --info or --debug setting, skipping any implied options (by -v, etc.).
358 * This is used both when conveying the user's options to the server, and
359 * when the help output wants to tell the user what options are implied. */
360 static char *make_output_option(struct output_struct *words, short *levels, uchar where)
361 {
362 char *str = words == info_words ? "--info=" : "--debug=";
363 int j, counts[MAX_OUT_LEVEL+1], pos, skipped = 0, len = 0, max = 0, lev = 0;
364 int word_count = words == info_words ? COUNT_INFO : COUNT_DEBUG;
365 char *buf;
366
367 memset(counts, 0, sizeof counts);
368
369 for (j = 0; words[j].name; j++) {
370 if (words[j].flag != j) {
371 rprintf(FERROR, "rsync: internal error on %s%s: %d != %d\n",
372 words == info_words ? "INFO_" : "DEBUG_",
373 words[j].name, words[j].flag, j);
374 exit_cleanup(RERR_UNSUPPORTED);
375 }
376 if (!(words[j].where & where))
377 continue;
378 if (words[j].priority == DEFAULT_PRIORITY) {
379 /* Implied items don't need to be mentioned. */
380 skipped++;
381 continue;
382 }
383 len += len ? 1 : strlen(str);
384 len += strlen(words[j].name);
385 len += levels[j] == 1 ? 0 : 1;
386
387 if (words[j].priority == HELP_PRIORITY)
388 continue; /* no abbreviating for help */
389
390 assert(levels[j] <= MAX_OUT_LEVEL);
391 if (++counts[levels[j]] > max) {
392 /* Determine which level has the most items. */
393 lev = levels[j];
394 max = counts[lev];
395 }
396 }
397
398 /* Sanity check the COUNT_* define against the length of the table. */
399 if (j != word_count) {
400 rprintf(FERROR, "rsync: internal error: %s is wrong! (%d != %d)\n",
401 words == info_words ? "COUNT_INFO" : "COUNT_DEBUG",
402 j, word_count);
403 exit_cleanup(RERR_UNSUPPORTED);
404 }
405
406 if (!len)
407 return NULL;
408
409 len++;
410 buf = new_array(char, len);
411 pos = 0;
412
413 if (skipped || max < 5)
414 lev = -1;
415 else {
416 if (lev == 0)
417 pos += snprintf(buf, len, "%sNONE", str);
418 else if (lev == 1)
419 pos += snprintf(buf, len, "%sALL", str);
420 else
421 pos += snprintf(buf, len, "%sALL%d", str, lev);
422 }
423
424 for (j = 0; words[j].name && pos < len; j++) {
425 if (words[j].priority == DEFAULT_PRIORITY || levels[j] == lev || !(words[j].where & where))
426 continue;
427 if (pos)
428 buf[pos++] = ',';
429 else
430 pos += strlcpy(buf+pos, str, len-pos);
431 if (pos < len)
432 pos += strlcpy(buf+pos, words[j].name, len-pos);
433 /* Level 1 is implied by the name alone. */
434 if (levels[j] != 1 && pos < len)
435 buf[pos++] = '0' + levels[j];
436 }
437
438 buf[pos] = '\0';
439
440 return buf;
441 }
442
443 static void parse_output_words(struct output_struct *words, short *levels, const char *str, uchar priority)
444 {
445 const char *s;
446 int j, len, lev;
447
448 for ( ; str; str = s) {
449 if ((s = strchr(str, ',')) != NULL)
450 len = s++ - str;
451 else
452 len = strlen(str);
453 if (!len)
454 continue;
455 if (!isDigit(str)) {
456 while (len && isDigit(str+len-1))
457 len--;
458 }
459 lev = isDigit(str+len) ? atoi(str+len) : 1;
460 /* atoi() of an overflowing positive digit string can return a
461 * negative int (LONG_MAX truncated on LP64); a negative lev
462 * here later indexes counts[lev] in make_output_option(). */
463 if (lev > MAX_OUT_LEVEL || lev < 0)
464 lev = MAX_OUT_LEVEL;
465 if (len == 4 && strncasecmp(str, "help", 4) == 0) {
466 output_item_help(words);
467 exit_cleanup(0);
468 }
469 if (len == 4 && strncasecmp(str, "none", 4) == 0)
470 len = lev = 0;
471 else if (len == 3 && strncasecmp(str, "all", 3) == 0)
472 len = 0;
473 for (j = 0; words[j].name; j++) {
474 if (!len
475 || (len == words[j].namelen && strncasecmp(str, words[j].name, len) == 0)) {
476 if (priority >= words[j].priority) {
477 words[j].priority = priority;
478 levels[j] = lev;
479 }
480 if (len)
481 break;
482 }
483 }
484 if (len && !words[j].name && !am_server) {
485 rprintf(FERROR, "Unknown %s item: \"%.*s\"\n",
486 words[j].help, len, str);
487 exit_cleanup(RERR_SYNTAX);
488 }
489 }
490 }
491
492 /* Tell the user what all the info or debug flags mean. */
493 static void output_item_help(struct output_struct *words)
494 {
495 short *levels = words == info_words ? info_levels : debug_levels;
496 const char *const*verbosity = words == info_words ? info_verbosity : debug_verbosity;
497 char buf[128], *opt, *fmt = "%-10s %s\n";
498 int j;
499
500 reset_output_levels();
501
502 rprintf(FINFO, "Use OPT or OPT1 for level 1 output, OPT2 for level 2, etc.; OPT0 silences.\n");
503 rprintf(FINFO, "\n");
504 for (j = 0; words[j].name; j++)
505 rprintf(FINFO, fmt, words[j].name, words[j].help);
506 rprintf(FINFO, "\n");
507
508 snprintf(buf, sizeof buf, "Set all %s options (e.g. all%d)",
509 words[j].help, MAX_OUT_LEVEL);
510 rprintf(FINFO, fmt, "ALL", buf);
511
512 snprintf(buf, sizeof buf, "Silence all %s options (same as all0)",
513 words[j].help);
514 rprintf(FINFO, fmt, "NONE", buf);
515
516 rprintf(FINFO, fmt, "HELP", "Output this help message");
517 rprintf(FINFO, "\n");
518 rprintf(FINFO, "Options added at each level of verbosity:\n");
519
520 for (j = 0; j <= MAX_VERBOSITY; j++) {
521 parse_output_words(words, levels, verbosity[j], HELP_PRIORITY);
522 opt = make_output_option(words, levels, W_CLI|W_SRV|W_SND|W_REC);
523 if (opt) {
524 rprintf(FINFO, "%d) %s\n", j, strchr(opt, '=')+1);
525 free(opt);
526 }
527 reset_output_levels();
528 }
529 }
530
531 /* The --verbose option now sets info+debug flags. */
532 static void set_output_verbosity(int level, uchar priority)
533 {
534 int j;
535
536 if (level > MAX_VERBOSITY)
537 level = MAX_VERBOSITY;
538
539 for (j = 0; j <= level; j++) {
540 parse_output_words(info_words, info_levels, info_verbosity[j], priority);
541 parse_output_words(debug_words, debug_levels, debug_verbosity[j], priority);
542 }
543 }
544
545 /* Limit the info+debug flag levels given a verbose-option level limit. */
546 void limit_output_verbosity(int level)
547 {
548 short info_limits[COUNT_INFO], debug_limits[COUNT_DEBUG];
549 int j;
550
551 if (level > MAX_VERBOSITY)
552 return;
553
554 memset(info_limits, 0, sizeof info_limits);
555 memset(debug_limits, 0, sizeof debug_limits);
556
557 /* Compute the level limits in the above arrays. */
558 for (j = 0; j <= level; j++) {
559 parse_output_words(info_words, info_limits, info_verbosity[j], LIMIT_PRIORITY);
560 parse_output_words(debug_words, debug_limits, debug_verbosity[j], LIMIT_PRIORITY);
561 }
562
563 for (j = 0; j < COUNT_INFO; j++) {
564 if (info_levels[j] > info_limits[j])
565 info_levels[j] = info_limits[j];
566 }
567
568 for (j = 0; j < COUNT_DEBUG; j++) {
569 if (debug_levels[j] > debug_limits[j])
570 debug_levels[j] = debug_limits[j];
571 }
572 }
573
574 void reset_output_levels(void)
575 {
576 int j;
577
578 memset(info_levels, 0, sizeof info_levels);
579 memset(debug_levels, 0, sizeof debug_levels);
580
581 for (j = 0; j < COUNT_INFO; j++)
582 info_words[j].priority = DEFAULT_PRIORITY;
583
584 for (j = 0; j < COUNT_DEBUG; j++)
585 debug_words[j].priority = DEFAULT_PRIORITY;
586 }
587
588 void negate_output_levels(void)
589 {
590 int j;
591
592 for (j = 0; j < COUNT_INFO; j++)
593 info_levels[j] *= -1;
594
595 for (j = 0; j < COUNT_DEBUG; j++)
596 debug_levels[j] *= -1;
597 }
598
599 enum {OPT_SERVER = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
600 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
601 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
602 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
603 OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG, OPT_BLOCK_SIZE,
604 OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT, OPT_STDERR,
605 OPT_OLD_COMPRESS, OPT_NEW_COMPRESS, OPT_NO_COMPRESS, OPT_OLD_ARGS,
606 OPT_STOP_AFTER, OPT_STOP_AT,
607 OPT_REFUSED_BASE = 9000};
608
609 static struct poptOption long_options[] = {
610 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
611 {"help", 0, POPT_ARG_NONE, 0, OPT_HELP, 0, 0 },
612 {"version", 'V', POPT_ARG_NONE, 0, 'V', 0, 0},
613 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
614 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
615 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
616 {"info", 0, POPT_ARG_STRING, 0, OPT_INFO, 0, 0 },
617 {"debug", 0, POPT_ARG_STRING, 0, OPT_DEBUG, 0, 0 },
618 {"stderr", 0, POPT_ARG_STRING, 0, OPT_STDERR, 0, 0 },
619 {"msgs2stderr", 0, POPT_ARG_VAL, &msgs2stderr, 1, 0, 0 },
620 {"no-msgs2stderr", 0, POPT_ARG_VAL, &msgs2stderr, 0, 0, 0 },
621 {"quiet", 'q', POPT_ARG_NONE, 0, 'q', 0, 0 },
622 {"motd", 0, POPT_ARG_VAL, &output_motd, 1, 0, 0 },
623 {"no-motd", 0, POPT_ARG_VAL, &output_motd, 0, 0, 0 },
624 {"stats", 0, POPT_ARG_NONE, &do_stats, 0, 0, 0 },
625 {"human-readable", 'h', POPT_ARG_NONE, 0, 'h', 0, 0},
626 {"no-human-readable",0, POPT_ARG_VAL, &human_readable, 0, 0, 0},
627 {"no-h", 0, POPT_ARG_VAL, &human_readable, 0, 0, 0},
628 {"dry-run", 'n', POPT_ARG_NONE, &dry_run, 0, 0, 0 },
629 {"archive", 'a', POPT_ARG_NONE, 0, 'a', 0, 0 },
630 {"recursive", 'r', POPT_ARG_VAL, &recurse, 2, 0, 0 },
631 {"no-recursive", 0, POPT_ARG_VAL, &recurse, 0, 0, 0 },
632 {"no-r", 0, POPT_ARG_VAL, &recurse, 0, 0, 0 },
633 {"inc-recursive", 0, POPT_ARG_VAL, &allow_inc_recurse, 1, 0, 0 },
634 {"no-inc-recursive", 0, POPT_ARG_VAL, &allow_inc_recurse, 0, 0, 0 },
635 {"i-r", 0, POPT_ARG_VAL, &allow_inc_recurse, 1, 0, 0 },
636 {"no-i-r", 0, POPT_ARG_VAL, &allow_inc_recurse, 0, 0, 0 },
637 {"dirs", 'd', POPT_ARG_VAL, &xfer_dirs, 2, 0, 0 },
638 {"no-dirs", 0, POPT_ARG_VAL, &xfer_dirs, 0, 0, 0 },
639 {"no-d", 0, POPT_ARG_VAL, &xfer_dirs, 0, 0, 0 },
640 {"old-dirs", 0, POPT_ARG_VAL, &xfer_dirs, 4, 0, 0 },
641 {"old-d", 0, POPT_ARG_VAL, &xfer_dirs, 4, 0, 0 },
642 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
643 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
644 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
645 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
646 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
647 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
648 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
649 {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
650 {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
651 {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
652 {"times", 't', POPT_ARG_VAL, &preserve_mtimes, 1, 0, 0 },
653 {"no-times", 0, POPT_ARG_VAL, &preserve_mtimes, 0, 0, 0 },
654 {"no-t", 0, POPT_ARG_VAL, &preserve_mtimes, 0, 0, 0 },
655 {"atimes", 'U', POPT_ARG_NONE, 0, 'U', 0, 0 },
656 {"no-atimes", 0, POPT_ARG_VAL, &preserve_atimes, 0, 0, 0 },
657 {"no-U", 0, POPT_ARG_VAL, &preserve_atimes, 0, 0, 0 },
658 {"open-noatime", 0, POPT_ARG_VAL, &open_noatime, 1, 0, 0 },
659 {"no-open-noatime", 0, POPT_ARG_VAL, &open_noatime, 0, 0, 0 },
660 {"crtimes", 'N', POPT_ARG_VAL, &preserve_crtimes, 1, 0, 0 },
661 {"no-crtimes", 0, POPT_ARG_VAL, &preserve_crtimes, 0, 0, 0 },
662 {"no-N", 0, POPT_ARG_VAL, &preserve_crtimes, 0, 0, 0 },
663 {"omit-dir-times", 'O', POPT_ARG_VAL, &omit_dir_times, 1, 0, 0 },
664 {"no-omit-dir-times",0, POPT_ARG_VAL, &omit_dir_times, 0, 0, 0 },
665 {"no-O", 0, POPT_ARG_VAL, &omit_dir_times, 0, 0, 0 },
666 {"omit-link-times", 'J', POPT_ARG_VAL, &omit_link_times, 1, 0, 0 },
667 {"no-omit-link-times",0, POPT_ARG_VAL, &omit_link_times, 0, 0, 0 },
668 {"no-J", 0, POPT_ARG_VAL, &omit_link_times, 0, 0, 0 },
669 {"modify-window", '@', POPT_ARG_INT, &modify_window, OPT_MODIFY_WINDOW, 0, 0 },
670 {"super", 0, POPT_ARG_VAL, &am_root, 2, 0, 0 },
671 {"no-super", 0, POPT_ARG_VAL, &am_root, 0, 0, 0 },
672 {"fake-super", 0, POPT_ARG_VAL, &am_root, -1, 0, 0 },
673 {"owner", 'o', POPT_ARG_VAL, &preserve_uid, 1, 0, 0 },
674 {"no-owner", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
675 {"no-o", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
676 {"group", 'g', POPT_ARG_VAL, &preserve_gid, 1, 0, 0 },
677 {"no-group", 0, POPT_ARG_VAL, &preserve_gid, 0, 0, 0 },
678 {"no-g", 0, POPT_ARG_VAL, &preserve_gid, 0, 0, 0 },
679 {0, 'D', POPT_ARG_NONE, 0, 'D', 0, 0 },
680 {"no-D", 0, POPT_ARG_NONE, 0, OPT_NO_D, 0, 0 },
681 {"devices", 0, POPT_ARG_VAL, &preserve_devices, 1, 0, 0 },
682 {"no-devices", 0, POPT_ARG_VAL, &preserve_devices, 0, 0, 0 },
683 {"copy-devices", 0, POPT_ARG_NONE, &copy_devices, 0, 0, 0 },
684 {"write-devices", 0, POPT_ARG_VAL, &write_devices, 1, 0, 0 },
685 {"no-write-devices", 0, POPT_ARG_VAL, &write_devices, 0, 0, 0 },
686 {"specials", 0, POPT_ARG_VAL, &preserve_specials, 1, 0, 0 },
687 {"no-specials", 0, POPT_ARG_VAL, &preserve_specials, 0, 0, 0 },
688 {"drop-D", 0, POPT_ARG_VAL, &drop_devices, 1, 0, 0 },
689 {"no-drop-D", 0, POPT_ARG_VAL, &drop_devices, 0, 0, 0 },
690 {"confine-root", 0, POPT_ARG_STRING, &confine_root, 0, 0, 0 },
691 {"links", 'l', POPT_ARG_VAL, &preserve_links, 1, 0, 0 },
692 {"no-links", 0, POPT_ARG_VAL, &preserve_links, 0, 0, 0 },
693 {"no-l", 0, POPT_ARG_VAL, &preserve_links, 0, 0, 0 },
694 {"copy-links", 'L', POPT_ARG_NONE, &copy_links, 0, 0, 0 },
695 {"copy-unsafe-links",0, POPT_ARG_NONE, &copy_unsafe_links, 0, 0, 0 },
696 {"safe-links", 0, POPT_ARG_NONE, &safe_symlinks, 0, 0, 0 },
697 {"insecure-links", 0, POPT_ARG_VAL, &insecure_links, 1, 0, 0 },
698 {"no-insecure-links",0, POPT_ARG_VAL, &insecure_links, 0, 0, 0 },
699 {"munge-links", 0, POPT_ARG_VAL, &munge_symlinks, 1, 0, 0 },
700 {"no-munge-links", 0, POPT_ARG_VAL, &munge_symlinks, 0, 0, 0 },
701 {"copy-dirlinks", 'k', POPT_ARG_NONE, &copy_dirlinks, 0, 0, 0 },
702 {"keep-dirlinks", 'K', POPT_ARG_NONE, &keep_dirlinks, 0, 0, 0 },
703 {"hard-links", 'H', POPT_ARG_NONE, 0, 'H', 0, 0 },
704 {"no-hard-links", 0, POPT_ARG_VAL, &preserve_hard_links, 0, 0, 0 },
705 {"no-H", 0, POPT_ARG_VAL, &preserve_hard_links, 0, 0, 0 },
706 {"relative", 'R', POPT_ARG_VAL, &relative_paths, 1, 0, 0 },
707 {"no-relative", 0, POPT_ARG_VAL, &relative_paths, 0, 0, 0 },
708 {"no-R", 0, POPT_ARG_VAL, &relative_paths, 0, 0, 0 },
709 {"implied-dirs", 0, POPT_ARG_VAL, &implied_dirs, 1, 0, 0 },
710 {"no-implied-dirs", 0, POPT_ARG_VAL, &implied_dirs, 0, 0, 0 },
711 {"i-d", 0, POPT_ARG_VAL, &implied_dirs, 1, 0, 0 },
712 {"no-i-d", 0, POPT_ARG_VAL, &implied_dirs, 0, 0, 0 },
713 {"chmod", 0, POPT_ARG_STRING, 0, OPT_CHMOD, 0, 0 },
714 {"ignore-times", 'I', POPT_ARG_NONE, &ignore_times, 0, 0, 0 },
715 {"size-only", 0, POPT_ARG_NONE, &size_only, 0, 0, 0 },
716 {"one-file-system", 'x', POPT_ARG_NONE, 0, 'x', 0, 0 },
717 {"no-one-file-system",0, POPT_ARG_VAL, &one_file_system, 0, 0, 0 },
718 {"no-x", 0, POPT_ARG_VAL, &one_file_system, 0, 0, 0 },
719 {"update", 'u', POPT_ARG_NONE, &update_only, 0, 0, 0 },
720 {"existing", 0, POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
721 {"ignore-non-existing",0,POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
722 {"ignore-existing", 0, POPT_ARG_NONE, &ignore_existing, 0, 0, 0 },
723 {"max-size", 0, POPT_ARG_STRING, &max_size_arg, OPT_MAX_SIZE, 0, 0 },
724 {"min-size", 0, POPT_ARG_STRING, &min_size_arg, OPT_MIN_SIZE, 0, 0 },
725 {"max-alloc", 0, POPT_ARG_STRING, &max_alloc_arg, 0, 0, 0 },
726 {"sparse", 'S', POPT_ARG_VAL, &sparse_files, 1, 0, 0 },
727 {"no-sparse", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
728 {"no-S", 0, POPT_ARG_VAL, &sparse_files, 0, 0, 0 },
729 {"preallocate", 0, POPT_ARG_NONE, &preallocate_files, 0, 0, 0},
730 {"inplace", 0, POPT_ARG_VAL, &inplace, 1, 0, 0 },
731 {"no-inplace", 0, POPT_ARG_VAL, &inplace, 0, 0, 0 },
732 {"append", 0, POPT_ARG_NONE, 0, OPT_APPEND, 0, 0 },
733 {"append-verify", 0, POPT_ARG_VAL, &append_mode, 2, 0, 0 },
734 {"no-append", 0, POPT_ARG_VAL, &append_mode, 0, 0, 0 },
735 {"del", 0, POPT_ARG_NONE, &delete_during, 0, 0, 0 },
736 {"delete", 0, POPT_ARG_NONE, &delete_mode, 0, 0, 0 },
737 {"delete-before", 0, POPT_ARG_NONE, &delete_before, 0, 0, 0 },
738 {"delete-during", 0, POPT_ARG_VAL, &delete_during, 1, 0, 0 },
739 {"delete-delay", 0, POPT_ARG_VAL, &delete_during, 2, 0, 0 },
740 {"delete-after", 0, POPT_ARG_NONE, &delete_after, 0, 0, 0 },
741 {"delete-excluded", 0, POPT_ARG_NONE, &delete_excluded, 0, 0, 0 },
742 {"delete-missing-args",0,POPT_BIT_SET, &missing_args, 2, 0, 0 },
743 {"ignore-missing-args",0,POPT_BIT_SET, &missing_args, 1, 0, 0 },
744 {"remove-sent-files",0, POPT_ARG_VAL, &remove_source_files, 2, 0, 0 }, /* deprecated */
745 {"remove-source-files",0,POPT_ARG_VAL, &remove_source_files, 1, 0, 0 },
746 {"force", 0, POPT_ARG_VAL, &force_delete, 1, 0, 0 },
747 {"no-force", 0, POPT_ARG_VAL, &force_delete, 0, 0, 0 },
748 {"ignore-errors", 0, POPT_ARG_VAL, &ignore_errors, 1, 0, 0 },
749 {"no-ignore-errors", 0, POPT_ARG_VAL, &ignore_errors, 0, 0, 0 },
750 {"max-delete", 0, POPT_ARG_INT, &max_delete, 0, 0, 0 },
751 {0, 'F', POPT_ARG_NONE, 0, 'F', 0, 0 },
752 {"filter", 'f', POPT_ARG_STRING, 0, OPT_FILTER, 0, 0 },
753 {"exclude", 0, POPT_ARG_STRING, 0, OPT_EXCLUDE, 0, 0 },
754 {"include", 0, POPT_ARG_STRING, 0, OPT_INCLUDE, 0, 0 },
755 {"exclude-from", 0, POPT_ARG_STRING, 0, OPT_EXCLUDE_FROM, 0, 0 },
756 {"include-from", 0, POPT_ARG_STRING, 0, OPT_INCLUDE_FROM, 0, 0 },
757 {"cvs-exclude", 'C', POPT_ARG_NONE, &cvs_exclude, 0, 0, 0 },
758 {"whole-file", 'W', POPT_ARG_VAL, &whole_file, 1, 0, 0 },
759 {"no-whole-file", 0, POPT_ARG_VAL, &whole_file, 0, 0, 0 },
760 {"no-W", 0, POPT_ARG_VAL, &whole_file, 0, 0, 0 },
761 {"checksum", 'c', POPT_ARG_VAL, &always_checksum, 1, 0, 0 },
762 {"no-checksum", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
763 {"no-c", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
764 {"checksum-choice", 0, POPT_ARG_STRING, &checksum_choice, 0, 0, 0 },
765 {"cc", 0, POPT_ARG_STRING, &checksum_choice, 0, 0, 0 },
766 {"block-size", 'B', POPT_ARG_STRING, 0, OPT_BLOCK_SIZE, 0, 0 },
767 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
768 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
769 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
770 {"fuzzy", 'y', POPT_ARG_NONE, 0, 'y', 0, 0 },
771 {"no-fuzzy", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
772 {"no-y", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
773 {"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
774 {"old-compress", 0, POPT_ARG_NONE, 0, OPT_OLD_COMPRESS, 0, 0 },
775 {"new-compress", 0, POPT_ARG_NONE, 0, OPT_NEW_COMPRESS, 0, 0 },
776 {"no-compress", 0, POPT_ARG_NONE, 0, OPT_NO_COMPRESS, 0, 0 },
777 {"no-z", 0, POPT_ARG_NONE, 0, OPT_NO_COMPRESS, 0, 0 },
778 {"compress-choice", 0, POPT_ARG_STRING, &compress_choice, 0, 0, 0 },
779 {"zc", 0, POPT_ARG_STRING, &compress_choice, 0, 0, 0 },
780 {"skip-compress", 0, POPT_ARG_STRING, &skip_compress, 0, 0, 0 },
781 {"compress-level", 0, POPT_ARG_INT, &do_compression_level, 0, 0, 0 },
782 {"zl", 0, POPT_ARG_INT, &do_compression_level, 0, 0, 0 },
783 {"compress-threads", 0, POPT_ARG_INT, &do_compression_threads, 0, 0, 0 },
784 {"zt", 0, POPT_ARG_INT, &do_compression_threads, 0, 0, 0 },
785 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
786 {"progress", 0, POPT_ARG_VAL, &do_progress, 1, 0, 0 },
787 {"no-progress", 0, POPT_ARG_VAL, &do_progress, 0, 0, 0 },
788 {"partial", 0, POPT_ARG_VAL, &keep_partial, 1, 0, 0 },
789 {"no-partial", 0, POPT_ARG_VAL, &keep_partial, 0, 0, 0 },
790 {"partial-dir", 0, POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
791 {"delay-updates", 0, POPT_ARG_VAL, &delay_updates, 1, 0, 0 },
792 {"no-delay-updates", 0, POPT_ARG_VAL, &delay_updates, 0, 0, 0 },
793 {"prune-empty-dirs",'m', POPT_ARG_VAL, &prune_empty_dirs, 1, 0, 0 },
794 {"no-prune-empty-dirs",0,POPT_ARG_VAL, &prune_empty_dirs, 0, 0, 0 },
795 {"no-m", 0, POPT_ARG_VAL, &prune_empty_dirs, 0, 0, 0 },
796 {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
797 {"log-file-format", 0, POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
798 {"out-format", 0, POPT_ARG_STRING, &stdout_format, 0, 0, 0 },
799 {"log-format", 0, POPT_ARG_STRING, &stdout_format, 0, 0, 0 }, /* DEPRECATED */
800 {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 },
801 {"no-itemize-changes",0, POPT_ARG_VAL, &itemize_changes, 0, 0, 0 },
802 {"no-i", 0, POPT_ARG_VAL, &itemize_changes, 0, 0, 0 },
803 {"bwlimit", 0, POPT_ARG_STRING, &bwlimit_arg, OPT_BWLIMIT, 0, 0 },
804 {"no-bwlimit", 0, POPT_ARG_VAL, &bwlimit, 0, 0, 0 },
805 {"backup", 'b', POPT_ARG_VAL, &make_backups, 1, 0, 0 },
806 {"no-backup", 0, POPT_ARG_VAL, &make_backups, 0, 0, 0 },
807 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
808 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
809 {"list-only", 0, POPT_ARG_VAL, &list_only, 2, 0, 0 },
810 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
811 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
812 {"only-write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
813 {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 },
814 {"from0", '0', POPT_ARG_VAL, &eol_nulls, 1, 0, 0},
815 {"no-from0", 0, POPT_ARG_VAL, &eol_nulls, 0, 0, 0},
816 {"old-args", 0, POPT_ARG_NONE, 0, OPT_OLD_ARGS, 0, 0},
817 {"no-old-args", 0, POPT_ARG_VAL, &old_style_args, 0, 0, 0},
818 {"secluded-args", 's', POPT_ARG_VAL, &protect_args, 1, 0, 0},
819 {"no-secluded-args", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
820 {"protect-args", 0, POPT_ARG_VAL, &protect_args, 1, 0, 0},
821 {"no-protect-args", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
822 {"no-s", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
823 {"trust-sender", 0, POPT_ARG_VAL, &trust_sender, 1, 0, 0},
824 {"numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 1, 0, 0 },
825 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
826 {"usermap", 0, POPT_ARG_STRING, 0, OPT_USERMAP, 0, 0 },
827 {"groupmap", 0, POPT_ARG_STRING, 0, OPT_GROUPMAP, 0, 0 },
828 {"chown", 0, POPT_ARG_STRING, 0, OPT_CHOWN, 0, 0 },
829 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
830 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
831 {"contimeout", 0, POPT_ARG_INT, &connect_timeout, 0, 0, 0 },
832 {"no-contimeout", 0, POPT_ARG_VAL, &connect_timeout, 0, 0, 0 },
833 {"fsync", 0, POPT_ARG_NONE, &do_fsync, 0, 0, 0 },
834 {"stop-after", 0, POPT_ARG_STRING, 0, OPT_STOP_AFTER, 0, 0 },
835 {"time-limit", 0, POPT_ARG_STRING, 0, OPT_STOP_AFTER, 0, 0 }, /* earlier stop-after name */
836 {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
837 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
838 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
839 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
840 {"iconv", 0, POPT_ARG_STRING, &iconv_opt, 0, 0, 0 },
841 {"no-iconv", 0, POPT_ARG_NONE, 0, OPT_NO_ICONV, 0, 0 },
842 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
843 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
844 {"8-bit-output", '8', POPT_ARG_VAL, &allow_8bit_chars, 1, 0, 0 },
845 {"no-8-bit-output", 0, POPT_ARG_VAL, &allow_8bit_chars, 0, 0, 0 },
846 {"no-8", 0, POPT_ARG_VAL, &allow_8bit_chars, 0, 0, 0 },
847 {"mkpath", 0, POPT_ARG_VAL, &mkpath_dest_arg, 1, 0, 0 },
848 {"no-mkpath", 0, POPT_ARG_VAL, &mkpath_dest_arg, 0, 0, 0 },
849 {"qsort", 0, POPT_ARG_NONE, &use_qsort, 0, 0, 0 },
850 {"copy-as", 0, POPT_ARG_STRING, &copy_as, 0, 0, 0 },
851 {"address", 0, POPT_ARG_STRING, &bind_address, 0, 0, 0 },
852 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
853 {"sockopts", 0, POPT_ARG_STRING, &sockopts, 0, 0, 0 },
854 {"password-file", 0, POPT_ARG_STRING, &password_file, 0, 0, 0 },
855 {"early-input", 0, POPT_ARG_STRING, &early_input_file, 0, 0, 0 },
856 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
857 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
858 {"outbuf", 0, POPT_ARG_STRING, &outbuf_mode, 0, 0, 0 },
859 {"remote-option", 'M', POPT_ARG_STRING, 0, 'M', 0, 0 },
860 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
861 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
862 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
863 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
864 /* All the following options switch us into daemon-mode option-parsing. */
865 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
866 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
867 {"dparam", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
868 {"detach", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
869 {"no-detach", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
870 {0,0,0,0, 0, 0, 0}
871 };
872
873 static const struct poptOption long_daemon_options[] = {
874 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
875 {"address", 0, POPT_ARG_STRING, &bind_address, 0, 0, 0 },
876 {"bwlimit", 0, POPT_ARG_INT, &daemon_bwlimit, 0, 0, 0 },
877 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
878 {"daemon", 0, POPT_ARG_NONE, &daemon_opt, 0, 0, 0 },
879 {"dparam", 'M', POPT_ARG_STRING, 0, 'M', 0, 0 },
880 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
881 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
882 {"detach", 0, POPT_ARG_VAL, &no_detach, 0, 0, 0 },
883 {"no-detach", 0, POPT_ARG_VAL, &no_detach, 1, 0, 0 },
884 {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
885 {"log-file-format", 0, POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
886 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
887 {"sockopts", 0, POPT_ARG_STRING, &sockopts, 0, 0, 0 },
888 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
889 {"server", 0, POPT_ARG_NONE, &am_server, 0, 0, 0 },
890 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
891 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
892 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
893 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
894 {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 },
895 {0,0,0,0, 0, 0, 0}
896 };
897
898
899 static char err_buf[200];
900
901
902 /**
903 * Store the option error message, if any, so that we can log the
904 * connection attempt (which requires parsing the options), and then
905 * show the error later on.
906 **/
907 void option_error(void)
908 {
909 if (!err_buf[0]) {
910 strlcpy(err_buf, "Error parsing options: option may "
911 "be supported on client but not on server?\n",
912 sizeof err_buf);
913 }
914
915 rprintf(FERROR, RSYNC_NAME ": %s", err_buf);
916 io_flush(MSG_FLUSH);
917 msleep(20);
918 }
919
920
921 /* Does this row store a compile-time constant, and if so which?
922 *
923 * popt's `val` is not comparable across argInfo kinds. For POPT_ARG_VAL it IS
924 * the value stored in `arg`; for the others a nonzero `val` is an action code
925 * handed to the parser's switch, and POPT_ARG_NONE with a destination stores 1
926 * regardless. Comparing the raw field therefore misses aliases spelled with
927 * different table shapes -- --del is POPT_ARG_NONE/&delete_during/0 and
928 * --delete-during is POPT_ARG_VAL/&delete_during/1, and both set it to 1. */
929 static int refuse_const_assign(const struct poptOption *op, int *valp)
930 {
931 if (!op->arg)
932 return 0;
933 if (op->argInfo == POPT_ARG_VAL) {
934 *valp = op->val;
935 return 1;
936 }
937 /* A nonzero val here means the row ALSO runs a parser action, so it is
938 * not merely an assignment and must not be folded in with one. */
939 if (op->argInfo == POPT_ARG_NONE && op->val == 0) {
940 *valp = 1;
941 return 1;
942 }
943 return 0;
944 }
945
946 /* Do two table rows name the same capability? An exact refuse rule names a
947 * capability, not one spelling of it. */
948 static int same_refuse_action(const struct poptOption *a, const struct poptOption *b)
949 {
950 int a_val, b_val;
951
952 /* Constant assignments: same destination, same resulting value. The
953 * value check keeps opposite switches such as --foo and --no-foo apart,
954 * since they differ only in what they store. */
955 if (refuse_const_assign(a, &a_val) && refuse_const_assign(b, &b_val))
956 return a->arg == b->arg && a_val == b_val;
957
958 /* Anything else has to match as a table entry: a row storing a runtime
959 * value (POPT_ARG_INT, POPT_ARG_STRING) needs the same destination and
960 * action code, and an action-only row the same nonzero code. */
961 if (a->argInfo != b->argInfo || a->val != b->val)
962 return 0;
963 return a->arg ? a->arg == b->arg : !b->arg && a->val != 0;
964 }
965
966 static void parse_one_refuse_match(int negated, const char *ref, const struct poptOption *list_end)
967 {
968 struct poptOption *op, *matched_op = NULL;
969 char shortName[2];
970 int is_wild = strpbrk(ref, "*?[") != NULL;
971 int found_match = 0;
972
973 shortName[1] = '\0';
974
975 if (strcmp("a", ref) == 0 || strcmp("archive", ref) == 0) {
976 ref = "[ardlptgoD]";
977 is_wild = 1;
978 }
979
980 for (op = long_options; op != list_end; op++) {
981 *shortName = op->shortName;
982 if ((op->longName && wildmatch(ref, op->longName))
983 || (*shortName && wildmatch(ref, shortName))) {
984 if (op->descrip[1] == '*')
985 op->descrip = negated ? "a*" : "r*";
986 else if (!is_wild)
987 op->descrip = negated ? "a=" : "r=";
988 found_match = 1;
989 if (!is_wild) {
990 matched_op = op;
991 break;
992 }
993 }
994 }
995
996 if (matched_op) {
997 for (op = long_options; op != list_end; op++) {
998 if (op == matched_op || !same_refuse_action(op, matched_op))
999 continue;
1000 if (op->descrip[1] == '*')
1001 op->descrip = negated ? "a*" : "r*";
1002 else
1003 op->descrip = negated ? "a=" : "r=";
1004 }
1005 }
1006
1007 if (!found_match)
1008 rprintf(FLOG, "No match for refuse-options string \"%s\"\n", ref);
1009 }
1010
1011
1012 /**
1013 * Tweak the option table to disable all options that the rsyncd.conf
1014 * file has told us to refuse.
1015 **/
1016 static void set_refuse_options(void)
1017 {
1018 struct poptOption *op, *list_end = NULL;
1019 char *cp, *ref = lp_refuse_options(module_id);
1020 int negated;
1021
1022 if (!ref)
1023 ref = "";
1024
1025 if (!am_daemon)
1026 ref = "";
1027
1028 /* We abuse the descrip field in poptOption to make it easy to flag which options
1029 * are refused (since we don't use it otherwise). Start by marking all options
1030 * as "a"ccepted with a few options also marked as non-wild. */
1031 for (op = long_options; ; op++) {
1032 const char *longName = op->longName ? op->longName : "";
1033 if (!op->longName && !op->shortName) {
1034 list_end = op;
1035 break;
1036 }
1037 if (!am_daemon
1038 || op->shortName == 'e' /* Required for compatibility flags */
1039 || op->shortName == '0' /* --from0 just modifies --files-from, so refuse that instead (or not) */
1040 || op->shortName == 's' /* --secluded-args is always OK */
1041 || op->shortName == 'n' /* --dry-run is always OK */
1042 || strcmp("iconv", longName) == 0
1043 || strcmp("no-iconv", longName) == 0
1044 || strcmp("checksum-seed", longName) == 0
1045 || strcmp("copy-devices", longName) == 0 /* disable wild-match (it gets refused below) */
1046 || strcmp("write-devices", longName) == 0 /* disable wild-match (it gets refused below) */
1047 || strcmp("log-format", longName) == 0 /* aka out-format (NOT log-file-format) */
1048 || strcmp("sender", longName) == 0
1049 || strcmp("server", longName) == 0)
1050 op->descrip = "a="; /* exact-match only */
1051 else
1052 op->descrip = "a*"; /* wild-card-able */
1053 }
1054 assert(list_end != NULL);
1055
1056 if (am_daemon) { /* Refused by default, but can be accepted via a negated exact match. */
1057 parse_one_refuse_match(0, "copy-devices", list_end);
1058 parse_one_refuse_match(0, "write-devices", list_end);
1059 }
1060
1061 while (1) {
1062 while (*ref == ' ') ref++;
1063 if (!*ref)
1064 break;
1065 if ((cp = strchr(ref, ' ')) != NULL)
1066 *cp = '\0';
1067 negated = *ref == '!';
1068 if (negated && ref[1])
1069 ref++;
1070 parse_one_refuse_match(negated, ref, list_end);
1071 if (!cp)
1072 break;
1073 *cp = ' ';
1074 ref = cp + 1;
1075 }
1076
1077 if (am_daemon) {
1078 #ifdef ICONV_OPTION
1079 if (!*lp_charset(module_id))
1080 parse_one_refuse_match(0, "iconv", list_end);
1081 #endif
1082 parse_one_refuse_match(0, "log-file*", list_end);
1083 /* A client must never disable the daemon's symlink confinement:
1084 * --insecure-links is a local-only flag, so the daemon hard-refuses it
1085 * (dropping the connection). The daemon's own opt-out is the
1086 * "insecure links" module parameter, not this flag. */
1087 parse_one_refuse_match(0, "insecure-links", list_end);
1088 }
1089
1090 #ifndef SUPPORT_ATIMES
1091 parse_one_refuse_match(0, "atimes", list_end);
1092 #endif
1093 #ifndef SUPPORT_HARD_LINKS
1094 parse_one_refuse_match(0, "link-dest", list_end);
1095 #endif
1096 #ifndef HAVE_MKTIME
1097 parse_one_refuse_match(0, "stop-at", list_end);
1098 #endif
1099 #ifndef ICONV_OPTION
1100 parse_one_refuse_match(0, "iconv", list_end);
1101 #endif
1102 #ifndef HAVE_SETVBUF
1103 parse_one_refuse_match(0, "outbuf", list_end);
1104 #endif
1105 #ifndef SUPPORT_CRTIMES
1106 parse_one_refuse_match(0, "crtimes", list_end);
1107 #endif
1108
1109 /* Now we use the descrip values to actually mark the options for refusal. */
1110 for (op = long_options; op != list_end; op++) {
1111 int refused = op->descrip[0] == 'r';
1112 op->descrip = NULL;
1113 if (!refused)
1114 continue;
1115 if (op->argInfo == POPT_ARG_VAL)
1116 op->argInfo = POPT_ARG_NONE;
1117 op->val = (op - long_options) + OPT_REFUSED_BASE;
1118 /* The following flags are set to let us easily check an implied option later in the code. */
1119 switch (op->shortName) {
1120 case 'r': case 'd': case 'l': case 'p':
1121 case 't': case 'g': case 'o': case 'D':
1122 refused_archive_part = op->val;
1123 break;
1124 case 'z':
1125 refused_compress = op->val;
1126 break;
1127 case '\0':
1128 if (strcmp("delete", op->longName) == 0)
1129 refused_delete = op->val;
1130 else if (strcmp("delete-before", op->longName) == 0)
1131 refused_delete_before = op->val;
1132 else if (strcmp("delete-during", op->longName) == 0)
1133 refused_delete_during = op->val;
1134 else if (strcmp("partial", op->longName) == 0)
1135 refused_partial = op->val;
1136 else if (strcmp("progress", op->longName) == 0)
1137 refused_progress = op->val;
1138 else if (strcmp("inplace", op->longName) == 0)
1139 refused_inplace = op->val;
1140 else if (strcmp("no-iconv", op->longName) == 0)
1141 refused_no_iconv = op->val;
1142 break;
1143 }
1144 }
1145 }
1146
1147
1148 static int count_args(const char **argv)
1149 {
1150 int i = 0;
1151
1152 if (argv) {
1153 while (argv[i] != NULL)
1154 i++;
1155 }
1156
1157 return i;
1158 }
1159
1160 /* If the size_arg is an invalid string or the value is < min_value, an error
1161 * is put into err_buf & the return is -1. Note that this parser does NOT
1162 * support negative numbers, so a min_value < 0 doesn't make any sense. */
1163 static ssize_t parse_size_arg(const char *size_arg, char def_suf, const char *opt_name,
1164 ssize_t min_value, ssize_t max_value, BOOL unlimited_0)
1165 {
1166 int reps, mult, len;
1167 const char *arg, *err = "invalid", *min_max = NULL;
1168 ssize_t limit = -1, size = 1;
1169 ssize_t size_max = max_value >= 0 ? max_value : (ssize_t)(SIZE_MAX / 2);
1170 double dsize;
1171
1172 for (arg = size_arg; isDigit(arg); arg++) {}
1173 if (*arg == '.' || *arg == get_decimal_point()) /* backward compatibility: always allow '.' */
1174 for (arg++; isDigit(arg); arg++) {}
1175 switch (*arg && *arg != '+' && *arg != '-' ? *arg++ : def_suf) {
1176 case 'b': case 'B':
1177 reps = 0;
1178 break;
1179 case 'k': case 'K':
1180 reps = 1;
1181 break;
1182 case 'm': case 'M':
1183 reps = 2;
1184 break;
1185 case 'g': case 'G':
1186 reps = 3;
1187 break;
1188 case 't': case 'T':
1189 reps = 4;
1190 break;
1191 case 'p': case 'P':
1192 reps = 5;
1193 break;
1194 default:
1195 goto failure;
1196 }
1197 if (*arg == 'b' || *arg == 'B')
1198 mult = 1000, arg++;
1199 else if (!*arg || *arg == '+' || *arg == '-')
1200 mult = 1024;
1201 else if (strncasecmp(arg, "ib", 2) == 0)
1202 mult = 1024, arg += 2;
1203 else
1204 goto failure;
1205 while (reps--) {
1206 if (size > size_max / mult) {
1207 err = "too large";
1208 min_max = "max";
1209 limit = max_value;
1210 goto failure;
1211 }
1212 size *= mult;
1213 }
1214 errno = 0;
1215 dsize = strtod(size_arg, NULL);
1216 if (errno == ERANGE || dsize < 0 || dsize > (double)size_max / size
1217 || (max_value < 0 && dsize >= (double)size_max / size)) {
1218 err = "too large";
1219 min_max = "max";
1220 limit = max_value;
1221 goto failure;
1222 }
1223 size = (ssize_t)(dsize * size);
1224 if ((*arg == '+' || *arg == '-') && arg[1] == '1' && arg != size_arg) {
1225 if (*arg == '+') {
1226 if (size == size_max) {
1227 err = "too large";
1228 min_max = "max";
1229 limit = max_value;
1230 goto failure;
1231 }
1232 size++;
1233 } else
1234 size--;
1235 arg += 2;
1236 }
1237 if (*arg)
1238 goto failure;
1239 if (size < 0 || (max_value >= 0 && size > max_value)) {
1240 err = "too large";
1241 min_max = "max";
1242 limit = max_value;
1243 goto failure;
1244 }
1245 if (size < min_value && (!unlimited_0 || size != 0)) {
1246 err = "too small";
1247 min_max = "min";
1248 limit = min_value;
1249 goto failure;
1250 }
1251 return size;
1252
1253 failure:
1254 len = snprintf(err_buf, sizeof err_buf - 1, "--%s=%s is %s", opt_name, size_arg, err);
1255 if (min_max && limit >= 0 && len < (int)sizeof err_buf - 10) {
1256 len += snprintf(err_buf + len, sizeof err_buf - len - 1, " (%s: %s%s)",
1257 min_max, do_big_num(limit, 3, NULL),
1258 unlimited_0 && min_max[1] == 'i' ? " or 0 for unlimited" : "");
1259 }
1260 if (len < 0 || len > (int)sizeof err_buf - 2)
1261 len = sizeof err_buf - 2;
1262 err_buf[len] = '\n';
1263 err_buf[len+1] = '\0';
1264 return -1;
1265 }
1266
1267 #ifdef HAVE_MKTIME
1268 /* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
1269 * also allowing abbreviated data. For instance, if the time is omitted,
1270 * it defaults to midnight. If the date is omitted, it defaults to the
1271 * next possible date in the future with the specified time. Even the
1272 * year or year-month can be omitted, again defaulting to the next date
1273 * in the future that matches the specified information. A 2-digit year
1274 * is also OK, as is using '/' instead of '-'. */
1275 static time_t parse_time(const char *arg)
1276 {
1277 const char *cp;
1278 time_t val, now = time(NULL);
1279 struct tm t, tmp, *today = localtime_r(&now, &tmp);
1280 int in_date, old_mday, n;
1281
1282 memset(&t, 0, sizeof t);
1283 t.tm_year = t.tm_mon = t.tm_mday = -1;
1284 t.tm_hour = t.tm_min = t.tm_isdst = -1;
1285 cp = arg;
1286 if (*cp == 'T' || *cp == 't' || *cp == ':') {
1287 in_date = *cp == ':' ? 0 : -1;
1288 cp++;
1289 } else
1290 in_date = 1;
1291 for ( ; ; cp++) {
1292 if (!isDigit(cp))
1293 return (time_t)-1;
1294 n = 0;
1295 do {
1296 n = n * 10 + *cp++ - '0';
1297 } while (isDigit(cp));
1298 if (*cp == ':')
1299 in_date = 0;
1300 if (in_date > 0) {
1301 if (t.tm_year != -1)
1302 return (time_t)-1;
1303 t.tm_year = t.tm_mon;
1304 t.tm_mon = t.tm_mday;
1305 t.tm_mday = n;
1306 if (!*cp)
1307 break;
1308 if (*cp == 'T' || *cp == 't') {
1309 if (!cp[1])
1310 break;
1311 in_date = -1;
1312 } else if (*cp != '-' && *cp != '/')
1313 return (time_t)-1;
1314 continue;
1315 }
1316 if (t.tm_hour != -1)
1317 return (time_t)-1;
1318 t.tm_hour = t.tm_min;
1319 t.tm_min = n;
1320 if (!*cp) {
1321 if (in_date < 0)
1322 return (time_t)-1;
1323 break;
1324 }
1325 if (*cp != ':')
1326 return (time_t)-1;
1327 in_date = 0;
1328 }
1329
1330 in_date = 0;
1331 if (t.tm_year < 0) {
1332 t.tm_year = today->tm_year;
1333 in_date = 1;
1334 } else if (t.tm_year < 100) {
1335 while (t.tm_year < today->tm_year)
1336 t.tm_year += 100;
1337 } else
1338 t.tm_year -= 1900;
1339 if (t.tm_mon < 0) {
1340 t.tm_mon = today->tm_mon;
1341 in_date = 2;
1342 } else
1343 t.tm_mon--;
1344 if (t.tm_mday < 0) {
1345 t.tm_mday = today->tm_mday;
1346 in_date = 3;
1347 }
1348
1349 n = 0;
1350 if (t.tm_min < 0) {
1351 t.tm_hour = t.tm_min = 0;
1352 } else if (t.tm_hour < 0) {
1353 if (in_date != 3)
1354 return (time_t)-1;
1355 in_date = 0;
1356 t.tm_hour = today->tm_hour;
1357 n = 60*60;
1358 }
1359
1360 /* Note that mktime() might change a too-large tm_mday into the start of
1361 * the following month which we need to undo in the following code! */
1362 old_mday = t.tm_mday;
1363 if (t.tm_hour > 23 || t.tm_min > 59
1364 || t.tm_mon < 0 || t.tm_mon >= 12
1365 || t.tm_mday < 1 || t.tm_mday > 31
1366 || (val = mktime(&t)) == (time_t)-1)
1367 return (time_t)-1;
1368
1369 while (in_date && (val <= now || t.tm_mday < old_mday)) {
1370 switch (in_date) {
1371 case 3:
1372 old_mday = ++t.tm_mday;
1373 break;
1374 case 2:
1375 if (t.tm_mday < old_mday)
1376 t.tm_mday = old_mday; /* The month already got bumped forward */
1377 else if (++t.tm_mon == 12) {
1378 t.tm_mon = 0;
1379 t.tm_year++;
1380 }
1381 break;
1382 case 1:
1383 if (t.tm_mday < old_mday) {
1384 /* mon==1 mday==29 got bumped to mon==2 */
1385 if (t.tm_mon != 2 || old_mday != 29)
1386 return (time_t)-1;
1387 t.tm_mon = 1;
1388 t.tm_mday = 29;
1389 }
1390 t.tm_year++;
1391 break;
1392 }
1393 if ((val = mktime(&t)) == (time_t)-1) {
1394 /* This code shouldn't be needed, as mktime() should auto-round to the next month. */
1395 if (in_date != 3 || t.tm_mday <= 28)
1396 return (time_t)-1;
1397 t.tm_mday = old_mday = 1;
1398 in_date = 2;
1399 }
1400 }
1401 if (n) {
1402 while (val <= now)
1403 val += n;
1404 }
1405 return val;
1406 }
1407 #endif
1408
1409 static void create_refuse_error(int which)
1410 {
1411 const char *msg;
1412 if (am_daemon)
1413 msg = "The server is configured to refuse";
1414 else if (am_server)
1415 msg = "The server does not support";
1416 else
1417 msg = "This rsync does not support";
1418
1419 /* The "which" value is the index + OPT_REFUSED_BASE. */
1420 struct poptOption *op = &long_options[which - OPT_REFUSED_BASE];
1421 int n = snprintf(err_buf, sizeof err_buf, "%s --%s\n", msg, op->longName) - 1;
1422 if (op->shortName)
1423 snprintf(err_buf + n, sizeof err_buf - n, " (-%c)\n", op->shortName);
1424 }
1425
1426 /* This is used to make sure that --daemon & --server cannot be aliased to
1427 * something else. These options have always disabled popt aliases for the
1428 * parsing of a daemon or server command-line, but we have to make sure that
1429 * these options cannot vanish so that the alias disabling can take effect. */
1430 static void popt_unalias(poptContext con, const char *opt)
1431 {
1432 struct poptAlias unalias;
1433
1434 memset(&unalias, 0, sizeof unalias);
1435
1436 unalias.longName = opt + 2; /* point past the leading "--" */
1437 unalias.argc = 1;
1438 unalias.argv = new_array0(const char*, 2);
1439 unalias.argv[0] = strdup(opt);
1440
1441 poptAddAlias(con, unalias, 0);
1442 }
1443
1444 char *alt_dest_opt(int type)
1445 {
1446 if (!type)
1447 type = alt_dest_type;
1448
1449 switch (type) {
1450 case COMPARE_DEST:
1451 return "--compare-dest";
1452 case COPY_DEST:
1453 return "--copy-dest";
1454 case LINK_DEST:
1455 return "--link-dest";
1456 default:
1457 NOISY_DEATH("Unknown alt_dest_opt type");
1458 }
1459 }
1460
1461 /**
1462 * Process command line arguments. Called on both local and remote.
1463 *
1464 * @retval 1 if all options are OK; with globals set to appropriate
1465 * values
1466 *
1467 * @retval 0 on error, with err_buf containing an explanation
1468 **/
1469 int parse_arguments(int *argc_p, const char ***argv_p)
1470 {
1471 poptContext pc;
1472 const char *arg, **argv = *argv_p;
1473 int argc = *argc_p;
1474 int opt, want_dest_type;
1475 int orig_protect_args = protect_args;
1476
1477 if (argc == 0) {
1478 strlcpy(err_buf, "argc is zero!\n", sizeof err_buf);
1479 return 0;
1480 }
1481
1482 set_refuse_options();
1483
1484 #ifdef ICONV_OPTION
1485 if (!am_daemon && protect_args <= 0 && (arg = getenv("RSYNC_ICONV")) != NULL && *arg)
1486 iconv_opt = strdup(arg);
1487 #endif
1488
1489 /* TODO: Call poptReadDefaultConfig; handle errors. */
1490
1491 pc = poptGetContext(RSYNC_NAME, argc, argv, long_options, 0);
1492 if (pc == NULL) {
1493 strlcpy(err_buf, "poptGetContext returned NULL\n", sizeof err_buf);
1494 return 0;
1495 }
1496 if (!am_server) {
1497 poptReadDefaultConfig(pc, 0);
1498 popt_unalias(pc, "--daemon");
1499 popt_unalias(pc, "--server");
1500 }
1501
1502 while ((opt = poptGetNextOpt(pc)) != -1) {
1503 /* most options are handled automatically by popt;
1504 * only special cases are returned and listed here. */
1505
1506 switch (opt) {
1507 case 'V':
1508 version_opt_cnt++;
1509 break;
1510
1511 case OPT_SERVER:
1512 if (!am_server) {
1513 /* Disable popt aliases on the server side and
1514 * then start parsing the options again. */
1515 poptFreeContext(pc);
1516 pc = poptGetContext(RSYNC_NAME, argc, argv, long_options, 0);
1517 am_server = 1;
1518 }
1519 #ifdef ICONV_OPTION
1520 iconv_opt = NULL;
1521 #endif
1522 break;
1523
1524 case OPT_SENDER:
1525 if (!am_server) {
1526 usage(FERROR);
1527 exit_cleanup(RERR_SYNTAX);
1528 }
1529 am_sender = 1;
1530 break;
1531
1532 case OPT_DAEMON:
1533 if (am_daemon) {
1534 strlcpy(err_buf,
1535 "Attempt to hack rsync thwarted!\n",
1536 sizeof err_buf);
1537 goto cleanup;
1538 }
1539 #ifdef ICONV_OPTION
1540 iconv_opt = NULL;
1541 #endif
1542 protect_args = 0;
1543 poptFreeContext(pc);
1544 pc = poptGetContext(RSYNC_NAME, argc, argv, long_daemon_options, 0);
1545 while ((opt = poptGetNextOpt(pc)) != -1) {
1546 char **cpp;
1547 switch (opt) {
1548 case 'h':
1549 daemon_usage(FINFO);
1550 exit_cleanup(0);
1551
1552 case 'M':
1553 arg = poptGetOptArg(pc);
1554 if (!strchr(arg, '=')) {
1555 rprintf(FERROR,
1556 "--dparam value is missing an '=': %s\n",
1557 arg);
1558 goto daemon_error;
1559 }
1560 cpp = EXPAND_ITEM_LIST(&dparam_list, char *, 4);
1561 *cpp = strdup(arg);
1562 break;
1563
1564 case 'v':
1565 verbose++;
1566 break;
1567
1568 default:
1569 rprintf(FERROR,
1570 "rsync: %s: %s (in daemon mode)\n",
1571 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1572 poptStrerror(opt));
1573 goto daemon_error;
1574 }
1575 }
1576
1577 if (dparam_list.count && !set_dparams(1))
1578 exit_cleanup(RERR_SYNTAX);
1579
1580 if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
1581 snprintf(err_buf, sizeof err_buf,
1582 "the --temp-dir path is WAY too long.\n");
1583 goto cleanup;
1584 }
1585
1586 if (!daemon_opt) {
1587 rprintf(FERROR, "Daemon option(s) used without --daemon.\n");
1588 daemon_error:
1589 rprintf(FERROR, "(Type \"rsync --daemon --help\" for assistance with daemon mode.)\n");
1590 exit_cleanup(RERR_SYNTAX);
1591 }
1592
1593 argv = poptGetArgs(pc);
1594 argc = count_args(argv);
1595 if (!argc) {
1596 *argv_p = empty_argv;
1597 *argc_p = 0;
1598 } else if (poptDupArgv(argc, argv, argc_p, argv_p) != 0)
1599 out_of_memory("parse_arguments");
1600 poptFreeContext(pc);
1601
1602 am_starting_up = 0;
1603 daemon_opt = 0;
1604 am_daemon = 1;
1605 return 1;
1606
1607 case OPT_MODIFY_WINDOW:
1608 /* The value has already been set by popt, but
1609 * we need to remember that we're using a
1610 * non-default setting. */
1611 modify_window_set = 1;
1612 break;
1613
1614 case OPT_FILTER:
1615 parse_filter_str(&filter_list, poptGetOptArg(pc),
1616 rule_template(0), 0);
1617 break;
1618
1619 case OPT_EXCLUDE:
1620 parse_filter_str(&filter_list, poptGetOptArg(pc),
1621 rule_template(0), XFLG_OLD_PREFIXES);
1622 break;
1623
1624 case OPT_INCLUDE:
1625 parse_filter_str(&filter_list, poptGetOptArg(pc),
1626 rule_template(FILTRULE_INCLUDE), XFLG_OLD_PREFIXES);
1627 break;
1628
1629 case OPT_EXCLUDE_FROM:
1630 case OPT_INCLUDE_FROM:
1631 arg = poptGetOptArg(pc);
1632 if (sanitize_paths)
1633 arg = sanitize_path(NULL, arg, NULL, 0, SP_DEFAULT);
1634 if (daemon_filter_list.head) {
1635 int rej;
1636 char *cp = strdup(arg);
1637 if (!*cp)
1638 rej = 1;
1639 else {
1640 char *dir = cp + (*cp == '/' ? module_dirlen : 0);
1641 clean_fname(dir, CFN_COLLAPSE_DOT_DOT_DIRS);
1642 rej = check_filter(&daemon_filter_list, FLOG, dir, 0) < 0;
1643 }
1644 free(cp);
1645 if (rej)
1646 goto options_rejected;
1647 }
1648 parse_filter_file(&filter_list, arg,
1649 rule_template(opt == OPT_INCLUDE_FROM ? FILTRULE_INCLUDE : 0),
1650 XFLG_FATAL_ERRORS | XFLG_OLD_PREFIXES);
1651 break;
1652
1653 case 'a':
1654 if (refused_archive_part) {
1655 create_refuse_error(refused_archive_part);
1656 goto cleanup;
1657 }
1658 if (!recurse) /* preserve recurse == 2 */
1659 recurse = 1;
1660 #ifdef SUPPORT_LINKS
1661 preserve_links = 1;
1662 #endif
1663 preserve_perms = 1;
1664 preserve_mtimes = 1;
1665 preserve_gid = 1;
1666 preserve_uid = 1;
1667 preserve_devices = 1;
1668 preserve_specials = 1;
1669 break;
1670
1671 case 'D':
1672 preserve_devices = preserve_specials = 1;
1673 break;
1674
1675 case OPT_NO_D:
1676 preserve_devices = preserve_specials = 0;
1677 break;
1678
1679 case 'h':
1680 human_readable++;
1681 break;
1682
1683 case 'H':
1684 preserve_hard_links++;
1685 break;
1686
1687 case 'i':
1688 itemize_changes++;
1689 break;
1690
1691 case 'U':
1692 if (++preserve_atimes > 1)
1693 open_noatime = 1;
1694 break;
1695
1696 case 'v':
1697 verbose++;
1698 break;
1699
1700 case 'y':
1701 fuzzy_basis++;
1702 break;
1703
1704 case 'q':
1705 quiet++;
1706 break;
1707
1708 case 'x':
1709 one_file_system++;
1710 break;
1711
1712 case 'F':
1713 switch (++F_option_cnt) {
1714 case 1:
1715 parse_filter_str(&filter_list,": /.rsync-filter",rule_template(0),0);
1716 break;
1717 case 2:
1718 parse_filter_str(&filter_list,"- .rsync-filter",rule_template(0),0);
1719 break;
1720 }
1721 break;
1722
1723 case 'P':
1724 if (refused_partial || refused_progress) {
1725 create_refuse_error(refused_partial ? refused_partial : refused_progress);
1726 goto cleanup;
1727 }
1728 do_progress = 1;
1729 keep_partial = 1;
1730 break;
1731
1732 case 'z':
1733 do_compression++;
1734 break;
1735
1736 case OPT_OLD_COMPRESS:
1737 compress_choice = "zlib";
1738 break;
1739
1740 case OPT_NEW_COMPRESS:
1741 compress_choice = "zlibx";
1742 break;
1743
1744 case OPT_NO_COMPRESS:
1745 do_compression = 0;
1746 compress_choice = NULL;
1747 break;
1748
1749 case OPT_OLD_ARGS:
1750 if (old_style_args <= 0)
1751 old_style_args = 1;
1752 else
1753 old_style_args++;
1754 break;
1755
1756 case 'M':
1757 arg = poptGetOptArg(pc);
1758 if (*arg != '-') {
1759 snprintf(err_buf, sizeof err_buf,
1760 "Remote option must start with a dash: %s\n", arg);
1761 goto cleanup;
1762 }
1763 if (remote_option_cnt+2 >= remote_option_alloc) {
1764 remote_option_alloc += 16;
1765 remote_options = realloc_array(remote_options,
1766 const char *, remote_option_alloc);
1767 if (!remote_option_cnt)
1768 remote_options[0] = "ARG0";
1769 }
1770 remote_options[++remote_option_cnt] = arg;
1771 remote_options[remote_option_cnt+1] = NULL;
1772 break;
1773
1774 case OPT_WRITE_BATCH:
1775 /* batch_name is already set */
1776 write_batch = 1;
1777 break;
1778
1779 case OPT_ONLY_WRITE_BATCH:
1780 /* batch_name is already set */
1781 write_batch = -1;
1782 break;
1783
1784 case OPT_READ_BATCH:
1785 /* batch_name is already set */
1786 read_batch = 1;
1787 break;
1788
1789 case OPT_NO_ICONV:
1790 #ifdef ICONV_OPTION
1791 iconv_opt = NULL;
1792 #endif
1793 break;
1794
1795 case OPT_BLOCK_SIZE: {
1796 /* We may not know the real protocol_version at this point if this is the client
1797 * option parsing, but we still want to check it so that the client can specify
1798 * a --protocol=29 option with a larger block size. */
1799 int max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
1800 ssize_t size;
1801 arg = poptGetOptArg(pc);
1802 if ((size = parse_size_arg(arg, 'b', "block-size", 0, max_blength, False)) < 0)
1803 goto cleanup;
1804 block_size = (int32)size;
1805 break;
1806 }
1807
1808 case OPT_MAX_SIZE:
1809 if ((max_size = parse_size_arg(max_size_arg, 'b', "max-size", 0, -1, False)) < 0)
1810 goto cleanup;
1811 max_size_arg = strdup(do_big_num(max_size, 0, NULL));
1812 break;
1813
1814 case OPT_MIN_SIZE:
1815 if ((min_size = parse_size_arg(min_size_arg, 'b', "min-size", 0, -1, False)) < 0)
1816 goto cleanup;
1817 min_size_arg = strdup(do_big_num(min_size, 0, NULL));
1818 break;
1819
1820 case OPT_BWLIMIT: {
1821 ssize_t size = parse_size_arg(bwlimit_arg, 'K', "bwlimit", 512, -1, True);
1822 if (size < 0)
1823 goto cleanup;
1824 bwlimit_arg = strdup(do_big_num(size, 0, NULL));
1825 bwlimit = (size + 512) / 1024;
1826 break;
1827 }
1828
1829 case OPT_APPEND:
1830 if (am_server)
1831 append_mode++;
1832 else
1833 append_mode = 1;
1834 break;
1835
1836 case OPT_LINK_DEST:
1837 want_dest_type = LINK_DEST;
1838 goto set_dest_dir;
1839
1840 case OPT_COPY_DEST:
1841 want_dest_type = COPY_DEST;
1842 goto set_dest_dir;
1843
1844 case OPT_COMPARE_DEST:
1845 want_dest_type = COMPARE_DEST;
1846
1847 set_dest_dir:
1848 if (alt_dest_type && alt_dest_type != want_dest_type) {
1849 snprintf(err_buf, sizeof err_buf,
1850 "ERROR: the %s option conflicts with the %s option\n",
1851 alt_dest_opt(want_dest_type), alt_dest_opt(0));
1852 goto cleanup;
1853 }
1854 alt_dest_type = want_dest_type;
1855
1856 if (basis_dir_cnt >= MAX_BASIS_DIRS) {
1857 snprintf(err_buf, sizeof err_buf,
1858 "ERROR: at most %d %s args may be specified\n",
1859 MAX_BASIS_DIRS, alt_dest_opt(0));
1860 goto cleanup;
1861 }
1862 /* We defer sanitizing this arg until we know what
1863 * our destination directory is going to be. */
1864 basis_dir[basis_dir_cnt++] = (char *)poptGetOptArg(pc);
1865 break;
1866
1867 case OPT_CHMOD:
1868 arg = poptGetOptArg(pc);
1869 if (!parse_chmod(arg, &chmod_modes)) {
1870 snprintf(err_buf, sizeof err_buf,
1871 "Invalid argument passed to --chmod (%s)\n",
1872 arg);
1873 goto cleanup;
1874 }
1875 break;
1876
1877 case OPT_INFO:
1878 arg = poptGetOptArg(pc);
1879 parse_output_words(info_words, info_levels, arg, USER_PRIORITY);
1880 break;
1881
1882 case OPT_DEBUG:
1883 arg = poptGetOptArg(pc);
1884 parse_output_words(debug_words, debug_levels, arg, USER_PRIORITY);
1885 break;
1886
1887 case OPT_USERMAP:
1888 if (usermap) {
1889 if (usermap_via_chown) {
1890 snprintf(err_buf, sizeof err_buf,
1891 "--usermap conflicts with prior --chown.\n");
1892 goto cleanup;
1893 }
1894 snprintf(err_buf, sizeof err_buf,
1895 "You can only specify --usermap once.\n");
1896 goto cleanup;
1897 }
1898 usermap = (char *)poptGetOptArg(pc);
1899 usermap_via_chown = False;
1900 preserve_uid = 1;
1901 break;
1902
1903 case OPT_GROUPMAP:
1904 if (groupmap) {
1905 if (groupmap_via_chown) {
1906 snprintf(err_buf, sizeof err_buf,
1907 "--groupmap conflicts with prior --chown.\n");
1908 goto cleanup;
1909 }
1910 snprintf(err_buf, sizeof err_buf,
1911 "You can only specify --groupmap once.\n");
1912 goto cleanup;
1913 }
1914 groupmap = (char *)poptGetOptArg(pc);
1915 groupmap_via_chown = False;
1916 preserve_gid = 1;
1917 break;
1918
1919 case OPT_CHOWN: {
1920 const char *chown = poptGetOptArg(pc);
1921 int len;
1922 if ((arg = strchr(chown, ':')) != NULL)
1923 len = arg++ - chown;
1924 else
1925 len = strlen(chown);
1926 if (len) {
1927 if (usermap) {
1928 if (!usermap_via_chown) {
1929 snprintf(err_buf, sizeof err_buf,
1930 "--chown conflicts with prior --usermap.\n");
1931 goto cleanup;
1932 }
1933 snprintf(err_buf, sizeof err_buf,
1934 "You can only specify a user-affecting --chown once.\n");
1935 goto cleanup;
1936 }
1937 if (asprintf(&usermap, "*:%.*s", len, chown) < 0)
1938 out_of_memory("parse_arguments");
1939 usermap_via_chown = True;
1940 preserve_uid = 1;
1941 }
1942 if (arg && *arg) {
1943 if (groupmap) {
1944 if (!groupmap_via_chown) {
1945 snprintf(err_buf, sizeof err_buf,
1946 "--chown conflicts with prior --groupmap.\n");
1947 goto cleanup;
1948 }
1949 snprintf(err_buf, sizeof err_buf,
1950 "You can only specify a group-affecting --chown once.\n");
1951 goto cleanup;
1952 }
1953 if (asprintf(&groupmap, "*:%s", arg) < 0)
1954 out_of_memory("parse_arguments");
1955 groupmap_via_chown = True;
1956 preserve_gid = 1;
1957 }
1958 break;
1959 }
1960
1961 case OPT_HELP:
1962 usage(FINFO);
1963 exit_cleanup(0);
1964
1965 case 'A':
1966 #ifdef SUPPORT_ACLS
1967 preserve_acls = 1;
1968 preserve_perms = 1;
1969 break;
1970 #else
1971 /* FIXME: this should probably be ignored with a
1972 * warning and then countermeasures taken to
1973 * restrict group and other access in the presence
1974 * of any more restrictive ACLs, but this is safe
1975 * for now */
1976 snprintf(err_buf,sizeof(err_buf),
1977 "ACLs are not supported on this %s\n",
1978 am_server ? "server" : "client");
1979 goto cleanup;
1980 #endif
1981
1982 case 'X':
1983 #ifdef SUPPORT_XATTRS
1984 preserve_xattrs++;
1985 break;
1986 #else
1987 snprintf(err_buf,sizeof(err_buf),
1988 "extended attributes are not supported on this %s\n",
1989 am_server ? "server" : "client");
1990 goto cleanup;
1991 #endif
1992
1993 case OPT_STOP_AFTER: {
1994 long val;
1995 arg = poptGetOptArg(pc);
1996 stop_at_utime = time(NULL);
1997 if ((val = atol(arg) * 60) <= 0 || LONG_MAX - val < stop_at_utime || (long)(time_t)val != val) {
1998 snprintf(err_buf, sizeof err_buf, "invalid --stop-after value: %s\n", arg);
1999 goto cleanup;
2000 }
2001 stop_at_utime += val;
2002 break;
2003 }
2004
2005 #ifdef HAVE_MKTIME
2006 case OPT_STOP_AT:
2007 arg = poptGetOptArg(pc);
2008 if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
2009 snprintf(err_buf, sizeof err_buf, "invalid --stop-at format: %s\n", arg);
2010 goto cleanup;
2011 }
2012 if (stop_at_utime <= time(NULL)) {
2013 snprintf(err_buf, sizeof err_buf, "--stop-at time is not in the future: %s\n", arg);
2014 goto cleanup;
2015 }
2016 break;
2017 #endif
2018
2019 case OPT_STDERR: {
2020 int len;
2021 arg = poptGetOptArg(pc);
2022 len = strlen(arg);
2023 if (len && strncmp("errors", arg, len) == 0)
2024 msgs2stderr = 2;
2025 else if (len && strncmp("all", arg, len) == 0)
2026 msgs2stderr = 1;
2027 else if (len && strncmp("client", arg, len) == 0)
2028 msgs2stderr = 0;
2029 else {
2030 snprintf(err_buf, sizeof err_buf,
2031 "--stderr mode \"%s\" is not one of errors, all, or client\n", arg);
2032 goto cleanup;
2033 }
2034 saw_stderr_opt = 1;
2035 break;
2036 }
2037
2038 default:
2039 /* A large opt value means that set_refuse_options()
2040 * turned this option off. */
2041 if (opt >= OPT_REFUSED_BASE) {
2042 create_refuse_error(opt);
2043 goto cleanup;
2044 }
2045 snprintf(err_buf, sizeof err_buf, "%s%s: %s\n",
2046 am_server ? "on remote machine: " : "",
2047 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2048 poptStrerror(opt));
2049 goto cleanup;
2050 }
2051 }
2052
2053 if (msgs2stderr != 2)
2054 saw_stderr_opt = 1;
2055
2056 if (version_opt_cnt) {
2057 print_rsync_version(version_opt_cnt > 1 && !am_server ? FNONE : FINFO);
2058 exit_cleanup(0);
2059 }
2060
2061 if (!max_alloc_arg) {
2062 max_alloc_arg = getenv("RSYNC_MAX_ALLOC");
2063 if (max_alloc_arg && !*max_alloc_arg)
2064 max_alloc_arg = NULL;
2065 }
2066 if (max_alloc_arg) {
2067 ssize_t size = parse_size_arg(max_alloc_arg, 'B', "max-alloc", 1024*1024, -1, True);
2068 if (size < 0)
2069 goto cleanup;
2070 if (size == 0) {
2071 snprintf(err_buf, sizeof err_buf, "max-alloc must be greater than zero\n");
2072 goto cleanup;
2073 }
2074 max_alloc = size;
2075 }
2076 if (!max_alloc)
2077 max_alloc = SIZE_MAX;
2078
2079 if (old_style_args < 0) {
2080 if (!am_server && protect_args <= 0 && (arg = getenv("RSYNC_OLD_ARGS")) != NULL && *arg) {
2081 protect_args = 0;
2082 old_style_args = atoi(arg);
2083 } else
2084 old_style_args = 0;
2085 } else if (old_style_args) {
2086 if (protect_args > 0) {
2087 snprintf(err_buf, sizeof err_buf,
2088 "--secluded-args conflicts with --old-args.\n");
2089 goto cleanup;
2090 }
2091 protect_args = 0;
2092 }
2093
2094 if (protect_args < 0) {
2095 if (am_server)
2096 protect_args = 0;
2097 else if ((arg = getenv("RSYNC_PROTECT_ARGS")) != NULL && *arg)
2098 protect_args = atoi(arg) ? 1 : 0;
2099 else {
2100 #ifdef RSYNC_USE_SECLUDED_ARGS
2101 protect_args = 1;
2102 #else
2103 protect_args = 0;
2104 #endif
2105 }
2106 }
2107
2108 if (checksum_choice && strcasecmp(checksum_choice, "auto") != 0 && strcasecmp(checksum_choice, "auto,auto") != 0) {
2109 /* Call this early to verify the args and figure out if we need to force
2110 * --whole-file. Note that the parse function will get called again later,
2111 * just in case an "auto" choice needs to know the protocol_version. */
2112 parse_checksum_choice(0);
2113 } else
2114 checksum_choice = NULL;
2115
2116 if (human_readable > 1 && argc == 2 && !am_server) {
2117 /* Allow the old meaning of 'h' (--help) on its own. */
2118 usage(FINFO);
2119 exit_cleanup(0);
2120 }
2121
2122 if (!compress_choice && do_compression > 1)
2123 compress_choice = "zlibx";
2124 if (compress_choice && strcasecmp(compress_choice, "auto") != 0)
2125 parse_compress_choice(0); /* Twiddles do_compression and can possibly NULL-out compress_choice. */
2126 else
2127 compress_choice = NULL;
2128
2129 if (do_compression || do_compression_level != CLVL_NOT_SPECIFIED) {
2130 if (!do_compression)
2131 do_compression = CPRES_AUTO;
2132 if (do_compression && refused_compress) {
2133 create_refuse_error(refused_compress);
2134 goto cleanup;
2135 }
2136 if (do_compression_threads < 0)
2137 do_compression_threads = 0;
2138 /* A daemon client controls the server-side sender arguments. Keep one
2139 * unauthenticated connection from asking Zstandard to materialize its
2140 * implementation maximum (currently hundreds) of worker threads. Local
2141 * and remote-shell invocations retain the operator-requested value. */
2142 if (am_daemon && do_compression_threads > MAX_DAEMON_COMPRESSION_THREADS)
2143 do_compression_threads = MAX_DAEMON_COMPRESSION_THREADS;
2144 }
2145
2146 #ifdef HAVE_SETVBUF
2147 if (outbuf_mode && !am_server) {
2148 int mode = *(uchar *)outbuf_mode;
2149 if (islower(mode))
2150 mode = toupper(mode);
2151 fflush(stdout); /* Just in case... */
2152 switch (mode) {
2153 case 'N': /* None */
2154 case 'U': /* Unbuffered */
2155 mode = _IONBF;
2156 break;
2157 case 'L': /* Line */
2158 mode = _IOLBF;
2159 break;
2160 case 'B': /* Block */
2161 case 'F': /* Full */
2162 mode = _IOFBF;
2163 break;
2164 default:
2165 snprintf(err_buf, sizeof err_buf,
2166 "Invalid --outbuf setting -- specify N, L, or B.\n");
2167 goto cleanup;
2168 }
2169 setvbuf(stdout, (char *)NULL, mode, 0);
2170 }
2171
2172 if (msgs2stderr == 1) { /* Are all messages going to stderr? */
2173 /* Make stderr line buffered for better sharing of the stream. */
2174 fflush(stderr); /* Just in case... */
2175 setvbuf(stderr, (char *)NULL, _IOLBF, 0);
2176 }
2177 #endif
2178
2179 set_output_verbosity(verbose, DEFAULT_PRIORITY);
2180
2181 if (do_stats) {
2182 parse_output_words(info_words, info_levels,
2183 verbose > 1 ? "stats3" : "stats2", DEFAULT_PRIORITY);
2184 }
2185
2186 #ifdef ICONV_OPTION
2187 if (iconv_opt && protect_args != 2) {
2188 if (!am_server && strcmp(iconv_opt, "-") == 0)
2189 iconv_opt = NULL;
2190 else
2191 need_unsorted_flist = 1;
2192 }
2193 if (refused_no_iconv && !iconv_opt) {
2194 create_refuse_error(refused_no_iconv);
2195 goto cleanup;
2196 }
2197 #endif
2198
2199 if (fuzzy_basis > 1)
2200 fuzzy_basis = basis_dir_cnt + 1;
2201
2202 /* Don't let the client reset protect_args if it was already processed */
2203 if (orig_protect_args == 2 && am_server)
2204 protect_args = orig_protect_args;
2205
2206 if (protect_args == 1 && am_server) {
2207 poptFreeContext(pc);
2208 return 1;
2209 }
2210
2211 /* Because popt 1.19 has started to free the returned args data, we now
2212 * make a copy of the array and then do an immediate cleanup. */
2213 argv = poptGetArgs(pc);
2214 argc = count_args(argv);
2215 if (!argc) {
2216 *argv_p = empty_argv;
2217 *argc_p = 0;
2218 } else if (poptDupArgv(argc, argv, argc_p, argv_p) != 0)
2219 out_of_memory("parse_arguments");
2220 argv = *argv_p;
2221 poptFreeContext(pc);
2222 pc = NULL;
2223
2224 #ifndef SUPPORT_LINKS
2225 if (preserve_links && !am_sender) {
2226 snprintf(err_buf, sizeof err_buf,
2227 "symlinks are not supported on this %s\n",
2228 am_server ? "server" : "client");
2229 goto cleanup;
2230 }
2231 #endif
2232
2233 #ifndef SUPPORT_HARD_LINKS
2234 if (preserve_hard_links) {
2235 snprintf(err_buf, sizeof err_buf,
2236 "hard links are not supported on this %s\n",
2237 am_server ? "server" : "client");
2238 goto cleanup;
2239 }
2240 #endif
2241
2242 #ifdef SUPPORT_XATTRS
2243 if (am_root < 0 && preserve_xattrs > 1) {
2244 snprintf(err_buf, sizeof err_buf,
2245 "--fake-super conflicts with -XX\n");
2246 goto cleanup;
2247 }
2248 #else
2249 if (am_root < 0) {
2250 snprintf(err_buf, sizeof err_buf,
2251 "--fake-super requires an rsync with extended attributes enabled\n");
2252 goto cleanup;
2253 }
2254 #endif
2255
2256 if (write_batch && read_batch) {
2257 snprintf(err_buf, sizeof err_buf,
2258 "--write-batch and --read-batch can not be used together\n");
2259 goto cleanup;
2260 }
2261 if (write_batch > 0 || read_batch) {
2262 if (am_server) {
2263 rprintf(FINFO,
2264 "ignoring --%s-batch option sent to server\n",
2265 write_batch ? "write" : "read");
2266 /* We don't actually exit_cleanup(), so that we can
2267 * still service older version clients that still send
2268 * batch args to server. */
2269 read_batch = write_batch = 0;
2270 batch_name = NULL;
2271 } else if (dry_run)
2272 write_batch = 0;
2273 } else if (write_batch < 0 && dry_run)
2274 write_batch = 0;
2275 if (read_batch && files_from) {
2276 snprintf(err_buf, sizeof err_buf,
2277 "--read-batch cannot be used with --files-from\n");
2278 goto cleanup;
2279 }
2280 if (read_batch && remove_source_files) {
2281 snprintf(err_buf, sizeof err_buf,
2282 "--read-batch cannot be used with --remove-%s-files\n",
2283 remove_source_files == 1 ? "source" : "sent");
2284 goto cleanup;
2285 }
2286 if (batch_name && strlen(batch_name) > MAX_BATCH_NAME_LEN) {
2287 snprintf(err_buf, sizeof err_buf,
2288 "the batch-file name must be %d characters or less.\n",
2289 MAX_BATCH_NAME_LEN);
2290 goto cleanup;
2291 }
2292
2293 if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
2294 snprintf(err_buf, sizeof err_buf,
2295 "the --temp-dir path is WAY too long.\n");
2296 goto cleanup;
2297 }
2298
2299 if (max_delete < 0 && max_delete != INT_MIN) {
2300 /* Negative numbers are treated as "no deletions". */
2301 max_delete = 0;
2302 }
2303
2304 if (files_from) {
2305 if (recurse == 1) /* preserve recurse == 2 */
2306 recurse = 0;
2307 if (xfer_dirs < 0)
2308 xfer_dirs = 1;
2309 }
2310
2311 if (argc < 2 && !read_batch && !am_server)
2312 list_only |= 1;
2313
2314 if (xfer_dirs >= 4) {
2315 parse_filter_str(&filter_list, "- /*/*", rule_template(0), 0);
2316 recurse = xfer_dirs = 1;
2317 } else if (recurse)
2318 xfer_dirs = 1;
2319 else if (xfer_dirs < 0)
2320 xfer_dirs = list_only ? 1 : 0;
2321
2322 if (relative_paths < 0)
2323 relative_paths = files_from? 1 : 0;
2324 if (!relative_paths)
2325 implied_dirs = 0;
2326
2327 if (delete_before + !!delete_during + delete_after > 1) {
2328 snprintf(err_buf, sizeof err_buf,
2329 "You may not combine multiple --delete-WHEN options.\n");
2330 goto cleanup;
2331 }
2332 if (delete_before || delete_during || delete_after)
2333 delete_mode = 1;
2334 else if (delete_mode || delete_excluded) {
2335 /* Only choose now between before & during if one is refused. */
2336 if (refused_delete_before) {
2337 if (!refused_delete_during)
2338 delete_during = 1;
2339 else {
2340 create_refuse_error(refused_delete_before);
2341 goto cleanup;
2342 }
2343 } else if (refused_delete_during)
2344 delete_before = 1;
2345 delete_mode = 1;
2346 }
2347 if (!xfer_dirs && delete_mode) {
2348 snprintf(err_buf, sizeof err_buf,
2349 "--delete does not work without --recursive (-r) or --dirs (-d).\n");
2350 goto cleanup;
2351 }
2352
2353 if (missing_args == 3) /* simplify if both options were specified */
2354 missing_args = 2;
2355 if (refused_delete && (delete_mode || missing_args == 2)) {
2356 create_refuse_error(refused_delete);
2357 goto cleanup;
2358 }
2359
2360 if (remove_source_files) {
2361 /* We only want to infer this refusal of --remove-source-files
2362 * via the refusal of "delete", not any of the "delete-FOO"
2363 * options. */
2364 if (refused_delete && am_sender) {
2365 create_refuse_error(refused_delete);
2366 goto cleanup;
2367 }
2368 need_messages_from_generator = 1;
2369 }
2370
2371 if (munge_symlinks && !am_daemon) {
2372 STRUCT_STAT st;
2373 char prefix[SYMLINK_PREFIX_LEN]; /* NOT +1 ! */
2374 strlcpy(prefix, SYMLINK_PREFIX, sizeof prefix); /* trim the trailing slash */
2375 if (do_stat(prefix, &st) == 0 && S_ISDIR(st.st_mode)) {
2376 rprintf(FERROR, "Symlink munging is unsafe when a %s directory exists.\n",
2377 prefix);
2378 exit_cleanup(RERR_UNSUPPORTED);
2379 }
2380 }
2381
2382 if (confine_root) {
2383 /* A daemon already has module_dir for this job, and honouring a
2384 * peer-supplied root there could only loosen the module boundary. */
2385 if (am_daemon)
2386 confine_root = NULL;
2387 else if (*confine_root != '/') {
2388 snprintf(err_buf, sizeof err_buf,
2389 "--confine-root must be an absolute path\n");
2390 return 0;
2391 } else if (insecure_links) {
2392 /* The opt-out restores the legacy open, which short-circuits the
2393 * walk that enforces the root -- so the pair would silently mean
2394 * no confinement at all. Say so instead. */
2395 snprintf(err_buf, sizeof err_buf,
2396 "--insecure-links cannot be combined with --confine-root\n");
2397 return 0;
2398 } else
2399 confine_root = normalize_path(confine_root, True, &confine_rootlen);
2400 }
2401
2402 if (sanitize_paths) {
2403 int i;
2404 for (i = argc; i-- > 0; )
2405 argv[i] = sanitize_path(NULL, argv[i], "", 0, SP_KEEP_DOT_DIRS);
2406 if (tmpdir)
2407 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, SP_DEFAULT);
2408 if (backup_dir)
2409 backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, SP_DEFAULT);
2410 }
2411 if (daemon_filter_list.head && !am_sender) {
2412 filter_rule_list *elp = &daemon_filter_list;
2413 /* Strip the module-dir prefix to get the module-relative name, but keep a
2414 * leading "/" for a "path = /" module (module_dirlen <= 1) so an absolute
2415 * (module-rooted) filter rule still matches. */
2416 if (tmpdir) {
2417 char clean[MAXPATHLEN], *dir;
2418 if (!*tmpdir)
2419 goto options_rejected;
2420 if (!sanitize_path(clean, tmpdir, "/", 0, SP_DEFAULT))
2421 strlcpy(clean, tmpdir, sizeof clean);
2422 dir = clean + (*clean == '/' && module_dirlen > 1 ? module_dirlen : 0);
2423 if (check_filter(elp, FLOG, dir, 1) < 0)
2424 goto options_rejected;
2425 }
2426 if (backup_dir) {
2427 char clean[MAXPATHLEN], *dir;
2428 if (!*backup_dir)
2429 goto options_rejected;
2430 if (!sanitize_path(clean, backup_dir, "/", 0, SP_DEFAULT))
2431 strlcpy(clean, backup_dir, sizeof clean);
2432 dir = clean + (*clean == '/' && module_dirlen > 1 ? module_dirlen : 0);
2433 if (check_filter(elp, FLOG, dir, 1) < 0)
2434 goto options_rejected;
2435 }
2436 }
2437
2438 if (!backup_suffix)
2439 backup_suffix = backup_dir ? "" : BACKUP_SUFFIX;
2440 backup_suffix_len = strlen(backup_suffix);
2441 if (strchr(backup_suffix, '/') != NULL) {
2442 snprintf(err_buf, sizeof err_buf,
2443 "--suffix cannot contain slashes: %s\n",
2444 backup_suffix);
2445 goto cleanup;
2446 }
2447 if (backup_dir) {
2448 size_t len;
2449 make_backups = 1; /* --backup-dir implies --backup */
2450 while (*backup_dir == '.' && backup_dir[1] == '/')
2451 backup_dir += 2;
2452 if (*backup_dir == '.' && backup_dir[1] == '\0')
2453 backup_dir++;
2454 len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
2455 if (len > sizeof backup_dir_buf - 128) {
2456 snprintf(err_buf, sizeof err_buf,
2457 "the --backup-dir path is WAY too long.\n");
2458 goto cleanup;
2459 }
2460 backup_dir_len = (int)len;
2461 if (!backup_dir_len) {
2462 backup_dir_len = -1;
2463 backup_dir = NULL;
2464 } else if (backup_dir_buf[backup_dir_len - 1] != '/') {
2465 backup_dir_buf[backup_dir_len++] = '/';
2466 backup_dir_buf[backup_dir_len] = '\0';
2467 }
2468 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
2469 }
2470 if (backup_dir) {
2471 /* No need for a suffix or a protect rule. */
2472 } else if (!backup_suffix_len && (!am_server || !am_sender)) {
2473 snprintf(err_buf, sizeof err_buf,
2474 "--suffix cannot be empty %s\n", backup_dir_len < 0
2475 ? "when --backup-dir is the same as the dest dir"
2476 : "without a --backup-dir");
2477 goto cleanup;
2478 } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
2479 snprintf(backup_dir_buf, sizeof backup_dir_buf,
2480 "P *%s", backup_suffix);
2481 parse_filter_str(&filter_list, backup_dir_buf, rule_template(0), 0);
2482 }
2483
2484 if (make_backups && !backup_dir)
2485 omit_dir_times = -1; /* Implied, so avoid -O to sender. */
2486
2487 if (stdout_format) {
2488 if (am_server && log_format_has(stdout_format, 'I'))
2489 stdout_format_has_i = 2;
2490 else if (log_format_has(stdout_format, 'i'))
2491 stdout_format_has_i = itemize_changes | 1;
2492 if (!log_format_has(stdout_format, 'b')
2493 && !log_format_has(stdout_format, 'c')
2494 && !log_format_has(stdout_format, 'C'))
2495 log_before_transfer = !am_server;
2496 } else if (itemize_changes) {
2497 stdout_format = "%i %n%L";
2498 stdout_format_has_i = itemize_changes;
2499 log_before_transfer = !am_server;
2500 }
2501
2502 if (do_progress && !am_server) {
2503 if (!log_before_transfer && INFO_EQ(NAME, 0))
2504 parse_output_words(info_words, info_levels, "name", DEFAULT_PRIORITY);
2505 parse_output_words(info_words, info_levels, "FLIST2,PROGRESS", DEFAULT_PRIORITY);
2506 }
2507
2508 if (dry_run)
2509 do_xfers = 0;
2510
2511 set_io_timeout(io_timeout);
2512
2513 if (INFO_GTE(NAME, 1) && !stdout_format) {
2514 stdout_format = "%n%L";
2515 log_before_transfer = !am_server;
2516 }
2517 if (stdout_format_has_i || log_format_has(stdout_format, 'o'))
2518 stdout_format_has_o_or_i = 1;
2519
2520 if (logfile_name && !am_daemon) {
2521 if (!logfile_format) {
2522 logfile_format = "%i %n%L";
2523 logfile_format_has_i = logfile_format_has_o_or_i = 1;
2524 } else {
2525 if (log_format_has(logfile_format, 'i'))
2526 logfile_format_has_i = 1;
2527 if (logfile_format_has_i || log_format_has(logfile_format, 'o'))
2528 logfile_format_has_o_or_i = 1;
2529 }
2530 log_init(0);
2531 } else if (!am_daemon)
2532 logfile_format = NULL;
2533
2534 if (daemon_bwlimit && (!bwlimit || bwlimit > daemon_bwlimit))
2535 bwlimit = daemon_bwlimit;
2536 if (bwlimit) {
2537 bwlimit_writemax = (size_t)bwlimit * 128;
2538 if (bwlimit_writemax < 512)
2539 bwlimit_writemax = 512;
2540 }
2541
2542 if (append_mode) {
2543 if (whole_file > 0) {
2544 snprintf(err_buf, sizeof err_buf,
2545 "--append cannot be used with --whole-file\n");
2546 goto cleanup;
2547 }
2548 if (refused_inplace) {
2549 create_refuse_error(refused_inplace);
2550 goto cleanup;
2551 }
2552 inplace = 1;
2553 }
2554
2555 if (write_devices) {
2556 if (refused_inplace) {
2557 create_refuse_error(refused_inplace);
2558 goto cleanup;
2559 }
2560 inplace = 1;
2561 }
2562
2563 if (delay_updates && !partial_dir)
2564 partial_dir = tmp_partialdir;
2565
2566 if (inplace) {
2567 #ifdef HAVE_FTRUNCATE
2568 if (partial_dir) {
2569 snprintf(err_buf, sizeof err_buf,
2570 "--%s cannot be used with --%s\n",
2571 append_mode ? "append" : "inplace",
2572 delay_updates ? "delay-updates" : "partial-dir");
2573 goto cleanup;
2574 }
2575 /* --inplace implies --partial for refusal purposes, but we
2576 * clear the keep_partial flag for internal logic purposes. */
2577 if (refused_partial) {
2578 create_refuse_error(refused_partial);
2579 goto cleanup;
2580 }
2581 keep_partial = 0;
2582 #else
2583 snprintf(err_buf, sizeof err_buf,
2584 "--%s is not supported on this %s\n",
2585 append_mode ? "append" : "inplace",
2586 am_server ? "server" : "client");
2587 goto cleanup;
2588 #endif
2589 } else {
2590 if (keep_partial && !partial_dir && !am_server) {
2591 if ((arg = getenv("RSYNC_PARTIAL_DIR")) != NULL && *arg)
2592 partial_dir = strdup(arg);
2593 }
2594 if (partial_dir) {
2595 if (*partial_dir)
2596 clean_fname(partial_dir, CFN_COLLAPSE_DOT_DOT_DIRS);
2597 if (!*partial_dir || strcmp(partial_dir, ".") == 0)
2598 partial_dir = NULL;
2599 if (!partial_dir && refused_partial) {
2600 create_refuse_error(refused_partial);
2601 goto cleanup;
2602 }
2603 keep_partial = 1;
2604 }
2605 }
2606
2607 if (files_from) {
2608 char *h, *p;
2609 int q = 0;
2610 if (argc > 2 || (!am_daemon && !am_server && argc == 1)) {
2611 usage(FERROR);
2612 exit_cleanup(RERR_SYNTAX);
2613 }
2614 if (strcmp(files_from, "-") == 0) {
2615 filesfrom_fd = 0;
2616 if (am_server)
2617 filesfrom_host = ""; /* reading from socket */
2618 } else if ((p = check_for_hostspec(files_from, &h, &q)) != 0) {
2619 if (am_server) {
2620 snprintf(err_buf, sizeof err_buf,
2621 "The --files-from sent to the server cannot specify a host.\n");
2622 goto cleanup;
2623 }
2624 files_from = p;
2625 filesfrom_host = h;
2626 if (strcmp(files_from, "-") == 0) {
2627 snprintf(err_buf, sizeof err_buf,
2628 "Invalid --files-from remote filename\n");
2629 goto cleanup;
2630 }
2631 } else {
2632 if (sanitize_paths)
2633 files_from = sanitize_path(NULL, files_from, NULL, 0, SP_DEFAULT);
2634 if (daemon_filter_list.head) {
2635 char *dir;
2636 if (!*files_from)
2637 goto options_rejected;
2638 dir = files_from + (*files_from == '/' ? module_dirlen : 0);
2639 clean_fname(dir, CFN_COLLAPSE_DOT_DOT_DIRS);
2640 if (check_filter(&daemon_filter_list, FLOG, dir, 0) < 0)
2641 goto options_rejected;
2642 }
2643 /* Operator-supplied path that may transit attacker-writable
2644 * parents; refuse symlinks not owned by uid 0 or our euid,
2645 * as for --exclude-from/--include-from/--filter in exclude.c.
2646 * A daemon reads this list from a CLIENT-requested path
2647 * (--files-from=:LIST) and it must stay inside the module:
2648 * operator_path_resolve makes the ownership walk also refuse a
2649 * (trusted-owned) symlink that redirects the list outside the
2650 * module root -- e.g. a root-owned backup symlink. No-op off a
2651 * daemon (the module-root check only fires when am_daemon). */
2652 int save_opr = operator_path_resolve;
2653 operator_path_resolve = 1;
2654 filesfrom_fd = open_no_attacker_symlinks(files_from, O_RDONLY|O_BINARY, 0);
2655 operator_path_resolve = save_opr;
2656 if (filesfrom_fd < 0) {
2657 snprintf(err_buf, sizeof err_buf,
2658 "failed to open files-from file %s: %s\n",
2659 files_from, strerror(errno));
2660 goto cleanup;
2661 }
2662 }
2663 }
2664
2665 if (trust_sender || am_server || read_batch)
2666 trust_sender_args = trust_sender_filter = 1;
2667 else if (old_style_args || filesfrom_host != NULL)
2668 trust_sender_args = 1;
2669
2670 am_starting_up = 0;
2671
2672 return 1;
2673
2674 options_rejected:
2675 snprintf(err_buf, sizeof err_buf,
2676 "Your options have been rejected by the server.\n");
2677 cleanup:
2678 if (pc)
2679 poptFreeContext(pc);
2680 return 0;
2681 }
2682
2683
2684 static char SPLIT_ARG_WHEN_OLD[1];
2685
2686 /**
2687 * Do backslash quoting of any weird chars in "arg", append the resulting
2688 * string to the end of the "opt" (which gets a "=" appended if it is not
2689 * an empty or NULL string), and return the (perhaps malloced) result.
2690 * If opt is NULL, arg is considered a filename arg that allows wildcards.
2691 * If it is "" or any other value, it is considered an option.
2692 **/
2693 char *safe_arg(const char *opt, const char *arg)
2694 {
2695 #define SHELL_CHARS "!#$&;|<>(){}\"\'` \t\n\r\\"
2696 #define WILD_CHARS "*?[]" /* We don't allow remote brace expansion */
2697 BOOL is_filename_arg = !opt;
2698 char *escapes = is_filename_arg ? SHELL_CHARS : WILD_CHARS SHELL_CHARS;
2699 BOOL escape_leading_dash = is_filename_arg && *arg == '-';
2700 BOOL escape_leading_tilde = 0;
2701 int len1 = opt && *opt ? strlen(opt) + 1 : 0;
2702 int len2 = strlen(arg);
2703 int extras = escape_leading_dash ? 2 : 0;
2704 char *ret;
2705 if (!protect_args && old_style_args < 2 && (!old_style_args || (!is_filename_arg && opt != SPLIT_ARG_WHEN_OLD))) {
2706 const char *f;
2707 if (*arg == '~' && is_filename_arg && !am_sender && !trust_sender_args
2708 && ((relative_paths && !strstr(arg, "/./"))
2709 || !strchr(arg, '/'))) {
2710 extras++;
2711 escape_leading_tilde = 1;
2712 }
2713 for (f = arg; *f; f++) {
2714 if (*f == '\\') {
2715 /* Mirror the writer below: in filename mode a backslash
2716 * before a wildcard is not doubled, so don't reserve a slot
2717 * for it. The "f[1] &&" also avoids the strchr(WILD_CHARS,
2718 * '\0') footgun (which matches the terminator) on a trailing
2719 * backslash -- otherwise the counter and writer disagree and
2720 * an uninitialized heap byte leaks into the result. */
2721 if (!is_filename_arg || !(f[1] && strchr(WILD_CHARS, f[1])))
2722 extras++;
2723 } else if (strchr(escapes, *f))
2724 extras++;
2725 }
2726 }
2727 if (!len1 && !extras)
2728 return (char*)arg;
2729 ret = new_array(char, len1 + len2 + extras + 1);
2730 if (len1) {
2731 memcpy(ret, opt, len1-1);
2732 ret[len1-1] = '=';
2733 }
2734 if (escape_leading_dash) {
2735 ret[len1++] = '.';
2736 ret[len1++] = '/';
2737 extras -= 2;
2738 }
2739 if (!extras)
2740 memcpy(ret + len1, arg, len2);
2741 else {
2742 const char *f = arg;
2743 char *t = ret + len1;
2744 if (escape_leading_tilde)
2745 *t++ = '\\';
2746 while (*f) {
2747 if (*f == '\\') {
2748 if (!is_filename_arg || !(f[1] && strchr(WILD_CHARS, f[1])))
2749 *t++ = '\\';
2750 } else if (strchr(escapes, *f))
2751 *t++ = '\\';
2752 *t++ = *f++;
2753 }
2754 }
2755 ret[len1+len2+extras] = '\0';
2756 return ret;
2757 }
2758
2759
2760 /**
2761 * Construct a filtered list of options to pass through from the
2762 * client to the server.
2763 *
2764 * This involves setting options that will tell the server how to
2765 * behave, and also filtering out options that are processed only
2766 * locally.
2767 **/
2768 void server_options(char **args, int *argc_p)
2769 {
2770 static char argstr[64];
2771 int ac = *argc_p;
2772 uchar where;
2773 char *arg;
2774 int i, x;
2775
2776 /* This should always remain first on the server's command-line. */
2777 args[ac++] = "--server";
2778
2779 if (!am_sender)
2780 args[ac++] = "--sender";
2781
2782 x = 1;
2783 argstr[0] = '-';
2784
2785 if (protect_args)
2786 argstr[x++] = 's';
2787
2788 /* `verbose` is unbounded (one increment per -v on our own command
2789 * line), so an uncapped loop walks past argstr[64]. Anything beyond
2790 * level ~5 is meaningless to the server anyway. */
2791 for (i = 0; i < verbose && i < 9; i++)
2792 argstr[x++] = 'v';
2793
2794 if (quiet && msgs2stderr)
2795 argstr[x++] = 'q';
2796 if (make_backups)
2797 argstr[x++] = 'b';
2798 if (update_only)
2799 argstr[x++] = 'u';
2800 if (!do_xfers) /* Note: NOT "dry_run"! */
2801 argstr[x++] = 'n';
2802 if (preserve_links)
2803 argstr[x++] = 'l';
2804 if ((xfer_dirs >= 2 && xfer_dirs < 4)
2805 || (xfer_dirs && !recurse && (list_only || (delete_mode && am_sender))))
2806 argstr[x++] = 'd';
2807 if (am_sender) {
2808 if (keep_dirlinks)
2809 argstr[x++] = 'K';
2810 if (prune_empty_dirs)
2811 argstr[x++] = 'm';
2812 if (omit_dir_times > 0)
2813 argstr[x++] = 'O';
2814 if (omit_link_times)
2815 argstr[x++] = 'J';
2816 if (fuzzy_basis) {
2817 argstr[x++] = 'y';
2818 if (fuzzy_basis > 1)
2819 argstr[x++] = 'y';
2820 }
2821 } else {
2822 if (copy_links)
2823 argstr[x++] = 'L';
2824 if (copy_dirlinks)
2825 argstr[x++] = 'k';
2826 }
2827
2828 if (whole_file > 0)
2829 argstr[x++] = 'W';
2830 /* We don't need to send --no-whole-file, because it's the
2831 * default for remote transfers, and in any case old versions
2832 * of rsync will not understand it. */
2833
2834 if (preserve_hard_links) {
2835 argstr[x++] = 'H';
2836 if (preserve_hard_links > 1)
2837 argstr[x++] = 'H';
2838 }
2839 if (preserve_uid)
2840 argstr[x++] = 'o';
2841 if (preserve_gid)
2842 argstr[x++] = 'g';
2843 if (preserve_devices) /* ignore preserve_specials here */
2844 argstr[x++] = 'D';
2845 if (preserve_mtimes)
2846 argstr[x++] = 't';
2847 if (preserve_atimes) {
2848 argstr[x++] = 'U';
2849 if (preserve_atimes > 1)
2850 argstr[x++] = 'U';
2851 }
2852 #ifdef SUPPORT_CRTIMES
2853 if (preserve_crtimes)
2854 argstr[x++] = 'N';
2855 #endif
2856 if (preserve_perms)
2857 argstr[x++] = 'p';
2858 else if (preserve_executability && am_sender)
2859 argstr[x++] = 'E';
2860 #ifdef SUPPORT_ACLS
2861 if (preserve_acls)
2862 argstr[x++] = 'A';
2863 #endif
2864 #ifdef SUPPORT_XATTRS
2865 if (preserve_xattrs) {
2866 argstr[x++] = 'X';
2867 if (preserve_xattrs > 1)
2868 argstr[x++] = 'X';
2869 }
2870 #endif
2871 if (recurse)
2872 argstr[x++] = 'r';
2873 if (always_checksum)
2874 argstr[x++] = 'c';
2875 if (cvs_exclude)
2876 argstr[x++] = 'C';
2877 if (ignore_times)
2878 argstr[x++] = 'I';
2879 if (relative_paths)
2880 argstr[x++] = 'R';
2881 if (one_file_system) {
2882 argstr[x++] = 'x';
2883 if (one_file_system > 1)
2884 argstr[x++] = 'x';
2885 }
2886 if (sparse_files)
2887 argstr[x++] = 'S';
2888 if (do_compression == CPRES_ZLIB)
2889 argstr[x++] = 'z';
2890
2891 set_allow_inc_recurse();
2892
2893 /* This '\0'-terminates argstr and makes sure it didn't overflow. */
2894 x += maybe_add_e_option(argstr + x, (int)sizeof argstr - x);
2895
2896 if (x > 1)
2897 args[ac++] = argstr;
2898
2899 #ifdef ICONV_OPTION
2900 if (iconv_opt) {
2901 char *set = strchr(iconv_opt, ',');
2902 if (set)
2903 set++;
2904 else
2905 set = iconv_opt;
2906 args[ac++] = safe_arg("--iconv", set);
2907 }
2908 #endif
2909
2910 if (protect_args && !local_server) /* unprotected args stop here */
2911 args[ac++] = NULL;
2912
2913 if (list_only > 1)
2914 args[ac++] = "--list-only";
2915
2916 /* This makes sure that the remote rsync can handle deleting with -d
2917 * sans -r because the --no-r option was added at the same time. */
2918 if (xfer_dirs && !recurse && delete_mode && am_sender)
2919 args[ac++] = "--no-r";
2920
2921 if (do_compression && do_compression_level != CLVL_NOT_SPECIFIED) {
2922 if (asprintf(&arg, "--compress-level=%d", do_compression_level) < 0)
2923 goto oom;
2924 args[ac++] = arg;
2925 }
2926
2927 if (preserve_devices) {
2928 /* Note: sending "--devices" would not be backward-compatible. */
2929 if (!preserve_specials)
2930 args[ac++] = "--no-specials"; /* -D is already set. */
2931 } else if (preserve_specials)
2932 args[ac++] = "--specials";
2933
2934 /* The server side doesn't use our log-format, but in certain
2935 * circumstances they need to know a little about the option. */
2936 if (stdout_format && am_sender) {
2937 /* Use --log-format, not --out-format, for compatibility. */
2938 if (stdout_format_has_i > 1)
2939 args[ac++] = "--log-format=%i%I";
2940 else if (stdout_format_has_i)
2941 args[ac++] = "--log-format=%i";
2942 else if (stdout_format_has_o_or_i)
2943 args[ac++] = "--log-format=%o";
2944 else if (!verbose)
2945 args[ac++] = "--log-format=X";
2946 }
2947
2948 if (msgs2stderr == 1)
2949 args[ac++] = "--msgs2stderr";
2950 else if (msgs2stderr == 0)
2951 args[ac++] = "--no-msgs2stderr";
2952
2953 if (block_size) {
2954 if (asprintf(&arg, "-B%u", (int)block_size) < 0)
2955 goto oom;
2956 args[ac++] = arg;
2957 }
2958
2959 if (io_timeout) {
2960 if (asprintf(&arg, "--timeout=%d", io_timeout) < 0)
2961 goto oom;
2962 args[ac++] = arg;
2963 }
2964
2965 if (bwlimit) {
2966 if (asprintf(&arg, "--bwlimit=%d", bwlimit) < 0)
2967 goto oom;
2968 args[ac++] = arg;
2969 }
2970
2971 if (backup_dir) {
2972 /* This split idiom allows for ~/path expansion via the shell. */
2973 args[ac++] = "--backup-dir";
2974 args[ac++] = safe_arg("", backup_dir);
2975 }
2976
2977 /* Only send --suffix if it specifies a non-default value. */
2978 if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0)
2979 args[ac++] = safe_arg("--suffix", backup_suffix);
2980
2981 if (checksum_choice)
2982 args[ac++] = safe_arg("--checksum-choice", checksum_choice);
2983
2984 if (do_compression == CPRES_ZLIBX)
2985 args[ac++] = "--new-compress";
2986 else if (compress_choice && do_compression == CPRES_ZLIB)
2987 args[ac++] = "--old-compress";
2988 else if (compress_choice)
2989 args[ac++] = safe_arg("--compress-choice", compress_choice);
2990
2991 if (am_sender) {
2992 if (max_delete > 0) {
2993 if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)
2994 goto oom;
2995 args[ac++] = arg;
2996 } else if (max_delete == 0)
2997 args[ac++] = "--max-delete=-1";
2998 if (min_size >= 0)
2999 args[ac++] = safe_arg("--min-size", min_size_arg);
3000 if (max_size >= 0)
3001 args[ac++] = safe_arg("--max-size", max_size_arg);
3002 if (delete_before)
3003 args[ac++] = "--delete-before";
3004 else if (delete_during == 2)
3005 args[ac++] = "--delete-delay";
3006 else if (delete_during)
3007 args[ac++] = "--delete-during";
3008 else if (delete_after)
3009 args[ac++] = "--delete-after";
3010 else if (delete_mode && !delete_excluded)
3011 args[ac++] = "--delete";
3012 if (delete_excluded)
3013 args[ac++] = "--delete-excluded";
3014 if (force_delete)
3015 args[ac++] = "--force";
3016 if (write_batch < 0)
3017 args[ac++] = "--only-write-batch=X";
3018 if (am_root > 1)
3019 args[ac++] = "--super";
3020 if (size_only)
3021 args[ac++] = "--size-only";
3022 if (do_stats)
3023 args[ac++] = "--stats";
3024 } else {
3025 if (skip_compress)
3026 args[ac++] = safe_arg("--skip-compress", skip_compress);
3027 }
3028
3029 if (max_alloc_arg && max_alloc != DEFAULT_MAX_ALLOC)
3030 args[ac++] = safe_arg("--max-alloc", max_alloc_arg);
3031
3032 /* --delete-missing-args needs the cooperation of both sides, but
3033 * the sender can handle --ignore-missing-args by itself. */
3034 if (missing_args == 2)
3035 args[ac++] = "--delete-missing-args";
3036 else if (missing_args == 1 && !am_sender)
3037 args[ac++] = "--ignore-missing-args";
3038
3039 if (modify_window_set && am_sender) {
3040 char *fmt = modify_window < 0 ? "-@%d" : "--modify-window=%d";
3041 if (asprintf(&arg, fmt, modify_window) < 0)
3042 goto oom;
3043 args[ac++] = arg;
3044 }
3045
3046 if (checksum_seed) {
3047 if (asprintf(&arg, "--checksum-seed=%d", checksum_seed) < 0)
3048 goto oom;
3049 args[ac++] = arg;
3050 }
3051
3052 if (partial_dir && am_sender) {
3053 if (partial_dir != tmp_partialdir) {
3054 args[ac++] = "--partial-dir";
3055 args[ac++] = safe_arg("", partial_dir);
3056 }
3057 if (delay_updates)
3058 args[ac++] = "--delay-updates";
3059 } else if (keep_partial && am_sender)
3060 args[ac++] = "--partial";
3061
3062 if (ignore_errors)
3063 args[ac++] = "--ignore-errors";
3064
3065 if (copy_unsafe_links)
3066 args[ac++] = "--copy-unsafe-links";
3067
3068 /* --insecure-links is NOT forwarded: it is a local-only opt-out. A daemon
3069 * governs its own confinement via the "insecure links" module parameter and
3070 * drops a connection that sends --insecure-links; a remote-shell peer that
3071 * wants it must be given it on its own side (e.g. via --rsync-path). */
3072
3073 if (safe_symlinks)
3074 args[ac++] = "--safe-links";
3075
3076 if (numeric_ids)
3077 args[ac++] = "--numeric-ids";
3078
3079 if (use_qsort)
3080 args[ac++] = "--use-qsort";
3081
3082 if (am_sender) {
3083 if (usermap)
3084 args[ac++] = safe_arg("--usermap", usermap);
3085
3086 if (groupmap)
3087 args[ac++] = safe_arg("--groupmap", groupmap);
3088
3089 if (ignore_existing)
3090 args[ac++] = "--ignore-existing";
3091
3092 /* Backward compatibility: send --existing, not --ignore-non-existing. */
3093 if (ignore_non_existing)
3094 args[ac++] = "--existing";
3095
3096 if (tmpdir) {
3097 args[ac++] = "--temp-dir";
3098 args[ac++] = safe_arg("", tmpdir);
3099 }
3100
3101 if (do_fsync)
3102 args[ac++] = "--fsync";
3103
3104 if (basis_dir[0]) {
3105 /* the server only needs this option if it is not the sender,
3106 * and it may be an older version that doesn't know this
3107 * option, so don't send it if client is the sender.
3108 */
3109 for (i = 0; i < basis_dir_cnt; i++) {
3110 args[ac++] = alt_dest_opt(0);
3111 args[ac++] = safe_arg("", basis_dir[i]);
3112 }
3113 }
3114 }
3115
3116 /* What flags do we need to send to the other side? */
3117 where = (am_server ? W_CLI : W_SRV) | (am_sender ? W_REC : W_SND);
3118 arg = make_output_option(info_words, info_levels, where);
3119 if (arg)
3120 args[ac++] = arg;
3121
3122 if (append_mode) {
3123 if (append_mode > 1)
3124 args[ac++] = "--append";
3125 args[ac++] = "--append";
3126 } else if (inplace) {
3127 args[ac++] = "--inplace";
3128 /* Work around a bug in older rsync versions (on the remote side) for --inplace --sparse */
3129 if (sparse_files && !whole_file && am_sender)
3130 args[ac++] = "--no-W";
3131 }
3132
3133 if (files_from && (!am_sender || filesfrom_host)) {
3134 if (filesfrom_host) {
3135 args[ac++] = "--files-from";
3136 args[ac++] = safe_arg("", files_from);
3137 if (eol_nulls)
3138 args[ac++] = "--from0";
3139 } else {
3140 args[ac++] = "--files-from=-";
3141 args[ac++] = "--from0";
3142 }
3143 if (!relative_paths)
3144 args[ac++] = "--no-relative";
3145 }
3146 /* It's OK that this checks the upper-bound of the protocol_version. */
3147 if (relative_paths && !implied_dirs && (!am_sender || protocol_version >= 30))
3148 args[ac++] = "--no-implied-dirs";
3149
3150 if (write_devices && am_sender)
3151 args[ac++] = "--write-devices";
3152
3153 if (remove_source_files == 1)
3154 args[ac++] = "--remove-source-files";
3155 else if (remove_source_files)
3156 args[ac++] = "--remove-sent-files";
3157
3158 if (copy_devices && !am_sender)
3159 args[ac++] = "--copy-devices";
3160
3161 if (preallocate_files && am_sender)
3162 args[ac++] = "--preallocate";
3163
3164 if (open_noatime && preserve_atimes <= 1)
3165 args[ac++] = "--open-noatime";
3166
3167 if (mkpath_dest_arg && am_sender)
3168 args[ac++] = "--mkpath";
3169
3170 if (ac > MAX_SERVER_ARGS) { /* Not possible... */
3171 rprintf(FERROR, "argc overflow in server_options().\n");
3172 exit_cleanup(RERR_MALLOC);
3173 }
3174
3175 if (remote_option_cnt) {
3176 int j;
3177 if (ac + remote_option_cnt > MAX_SERVER_ARGS) {
3178 rprintf(FERROR, "too many remote options specified.\n");
3179 exit_cleanup(RERR_SYNTAX);
3180 }
3181 for (j = 1; j <= remote_option_cnt; j++)
3182 args[ac++] = safe_arg(SPLIT_ARG_WHEN_OLD, remote_options[j]);
3183 }
3184
3185 *argc_p = ac;
3186 return;
3187
3188 oom:
3189 out_of_memory("server_options");
3190 }
3191
3192 int maybe_add_e_option(char *buf, int buf_len)
3193 {
3194 int x = 0;
3195
3196 /* We don't really know the actual protocol_version at this point,
3197 * but checking the pre-negotiated value allows the user to use a
3198 * --protocol=29 override to avoid the use of this -eFLAGS opt. */
3199 if (protocol_version >= 30 && buf_len > 0) {
3200 /* We make use of the -e option to let the server know about
3201 * any pre-release protocol version && some behavior flags. */
3202 buf[x++] = 'e';
3203
3204 #if SUBPROTOCOL_VERSION != 0
3205 if (protocol_version == PROTOCOL_VERSION)
3206 x += snprintf(buf + x, buf_len - x, "%d.%d", PROTOCOL_VERSION, SUBPROTOCOL_VERSION);
3207 else
3208 #endif
3209 buf[x++] = '.';
3210 if (allow_inc_recurse)
3211 buf[x++] = 'i';
3212 #ifdef CAN_SET_SYMLINK_TIMES
3213 buf[x++] = 'L'; /* symlink time-setting support */
3214 #endif
3215 #ifdef ICONV_OPTION
3216 buf[x++] = 's'; /* symlink iconv translation support */
3217 #endif
3218 buf[x++] = 'f'; /* flist I/O-error safety support */
3219 buf[x++] = 'x'; /* xattr hardlink optimization not desired */
3220 buf[x++] = 'C'; /* support checksum seed order fix */
3221 buf[x++] = 'I'; /* support inplace_partial behavior */
3222 buf[x++] = 'v'; /* use varint for flist & compat flags; negotiate checksum */
3223 buf[x++] = 'u'; /* include name of uid 0 & gid 0 in the id map */
3224
3225 /* NOTE: Avoid using 'V' -- it was represented with the high bit of a write_byte() that became a write_varint(). */
3226 }
3227
3228 if (x >= buf_len) { /* Not possible... */
3229 rprintf(FERROR, "overflow in add_e_flags().\n");
3230 exit_cleanup(RERR_MALLOC);
3231 }
3232
3233 buf[x] = '\0';
3234
3235 return x;
3236 }
3237
3238 /* If str points to a valid hostspec, return allocated memory containing the
3239 * [USER@]HOST part of the string, and set the path_start_ptr to the part of
3240 * the string after the host part. Otherwise, return NULL. If port_ptr is
3241 * non-NULL, we must be parsing an rsync:// URL hostname, and we will set
3242 * *port_ptr if a port number is found. Note that IPv6 IPs will have their
3243 * (required for parsing) [ and ] chars elided from the returned string. */
3244 static char *parse_hostspec(char *str, char **path_start_ptr, int *port_ptr)
3245 {
3246 char *s, *host_start = str;
3247 int hostlen = 0, userlen = 0;
3248 char *ret;
3249
3250 for (s = str; ; s++) {
3251 if (!*s) {
3252 /* It is only OK if we run out of string with rsync:// */
3253 if (!port_ptr)
3254 return NULL;
3255 if (!hostlen)
3256 hostlen = s - host_start;
3257 break;
3258 }
3259 if (*s == ':' || *s == '/') {
3260 if (!hostlen)
3261 hostlen = s - host_start;
3262 if (*s++ == '/') {
3263 if (!port_ptr)
3264 return NULL;
3265 } else if (port_ptr) {
3266 *port_ptr = atoi(s);
3267 while (isDigit(s)) s++;
3268 if (*s && *s++ != '/')
3269 return NULL;
3270 }
3271 break;
3272 }
3273 if (*s == '@') {
3274 userlen = s - str + 1;
3275 host_start = s + 1;
3276 } else if (*s == '[') {
3277 if (s != host_start++)
3278 return NULL;
3279 while (*s && *s != ']' && *s != '/') s++; /*SHARED ITERATOR*/
3280 hostlen = s - host_start;
3281 if (*s != ']' || (s[1] && s[1] != '/' && s[1] != ':') || !hostlen)
3282 return NULL;
3283 }
3284 }
3285
3286 *path_start_ptr = s;
3287 ret = new_array(char, userlen + hostlen + 1);
3288 if (userlen)
3289 strlcpy(ret, str, userlen + 1);
3290 strlcpy(ret + userlen, host_start, hostlen + 1);
3291 return ret;
3292 }
3293
3294 /* Look for a HOST specification of the form "HOST:PATH", "HOST::PATH", or
3295 * "rsync://HOST:PORT/PATH". If found, *host_ptr will be set to some allocated
3296 * memory with the HOST. If a daemon-accessing spec was specified, the value
3297 * of *port_ptr will contain a non-0 port number, otherwise it will be set to
3298 * 0. The return value is a pointer to the PATH. Note that the HOST spec can
3299 * be an IPv6 literal address enclosed in '[' and ']' (such as "[::1]" or
3300 * "[::ffff:127.0.0.1]") which is returned without the '[' and ']'. */
3301 char *check_for_hostspec(char *s, char **host_ptr, int *port_ptr)
3302 {
3303 char *path;
3304
3305 if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
3306 *host_ptr = parse_hostspec(s + strlen(URL_PREFIX), &path, port_ptr);
3307 if (*host_ptr) {
3308 if (!*port_ptr)
3309 *port_ptr = -1; /* -1 indicates they want the default */
3310 return path;
3311 }
3312 }
3313
3314 *host_ptr = parse_hostspec(s, &path, NULL);
3315 if (!*host_ptr)
3316 return NULL;
3317
3318 if (*path == ':') {
3319 if (port_ptr && !*port_ptr)
3320 *port_ptr = -1;
3321 return path + 1;
3322 }
3323 if (port_ptr)
3324 *port_ptr = 0;
3325
3326 return path;
3327 }