Debian Codegraph @

sender.c

raw symbols SCIP
1 /*
2 * Routines only used by the sending process.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2022 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 "inums.h"
24
25 extern int do_xfers;
26 extern int open_noatime;
27 extern int am_server;
28 extern int am_daemon;
29 extern int local_server;
30 extern int inc_recurse;
31 extern int log_before_transfer;
32 extern int stdout_format_has_i;
33 extern int logfile_format_has_i;
34 extern int want_xattr_optim;
35 extern int xfer_sum_len;
36 extern int csum_length;
37 extern int append_mode;
38 extern int copy_links;
39 extern int copy_unsafe_links;
40 extern int copy_dirlinks;
41 extern int insecure_links;
42 extern int io_error;
43 extern int flist_eof;
44 extern int whole_file;
45 extern int allowed_lull;
46 extern int copy_devices;
47 extern int preserve_xattrs;
48 extern int protocol_version;
49 extern int remove_source_files;
50 extern int updating_basis_file;
51 extern int make_backups;
52 extern int inplace;
53 extern int inplace_partial;
54 extern int batch_fd;
55 extern char *module_dir;
56 extern int module_dirfd;
57 extern int write_batch;
58 extern int file_old_total;
59 extern BOOL want_progress_now;
60 extern struct stats stats;
61 extern struct file_list *cur_flist, *first_flist, *dir_flist;
62 extern char num_dev_ino_buf[4 + 8 + 8];
63
64 BOOL extra_flist_sending_enabled;
65
66 /**
67 * @file
68 *
69 * The sender gets checksums from the generator, calculates deltas,
70 * and transmits them to the receiver. The sender process runs on the
71 * machine holding the source files.
72 **/
73
74 static int secure_sender_parent_fd(struct file_struct *file, const char *fname, const char **bname_p)
75 {
76 #ifdef AT_FDCWD
77 const char *path, *slash, *relp, *bslash, *fslash;
78 char secure_path[MAXPATHLEN];
79 int dfd, fl, slen;
80
81 if (!fname || !*fname) {
82 errno = 0;
83 return -1;
84 }
85
86 /* "insecure links = yes" / --insecure-links: restore the 3.2.7 plain re-stat
87 * by declining the confined parent (errno=0 makes the caller use do_lstat). */
88 if (symlink_optout_allowed()) {
89 errno = 0;
90 return -1;
91 }
92
93 if (!am_daemon || !module_dir || module_dir[0] != '/') {
94 /* Local (non-daemon) sender: there is no module root to anchor at, but
95 * still confine the parent via the shared held ancestor-dirfd stack
96 * (anchor = cwd, the transfer root set by change_pathname) so the
97 * --remove-source-files re-stat below won't follow an attacker-planted
98 * parent symlink. Best-effort: an uncacheable (very deep) path declines
99 * to -1 and the caller falls back to the path-based stat. */
100 const char *fslash = strrchr(fname, '/');
101 *bname_p = fslash ? fslash + 1 : fname;
102 if (fslash) {
103 char dir[MAXPATHLEN];
104 size_t dlen = (size_t)(fslash - fname);
105 if (dlen >= sizeof dir) {
106 errno = ENAMETOOLONG;
107 return -1;
108 }
109 memcpy(dir, fname, dlen);
110 dir[dlen] = '\0';
111 /* An absolute --relative name is still rooted at / after
112 * change_pathname(). Resolving its parent through the cwd-backed
113 * dirfd cache would re-anchor cleanup at the sender's working
114 * directory and can remove a same-named, unrelated entry there. */
115 if (*fname == '/') {
116 const char *rel = dir;
117 #ifdef __CYGWIN__
118 /* clean_fname() keeps exactly two leading slashes here,
119 * because //server/share is a separate UNC namespace.
120 * Stripping them and anchoring at "/" would resolve a
121 * different object entirely, so decline (errno 0) and let
122 * the caller fall back to the path-based cleanup. */
123 if (fname[1] == '/' && fname[2] != '/') {
124 errno = 0;
125 return -1;
126 }
127 #endif
128 while (*rel == '/')
129 rel++;
130 return secure_relative_open("/", rel,
131 O_RDONLY | O_DIRECTORY, 0);
132 }
133 /* held_dir_path_fd returns a cache-OWNED fd; the caller closes
134 * what we return, so hand back an owned dup and leave the cache's
135 * dirfd intact. An uncacheable (very deep) dir declines with
136 * errno 0 -- fall back to the full confined walk (an owned fd,
137 * matching the sender's content open) so deep paths stay confined
138 * too; a real error propagates. */
139 dfd = held_dir_path_fd(NULL, dir);
140 if (dfd >= 0)
141 return dup(dfd);
142 if (errno != 0)
143 return -1;
144 return secure_relative_open(NULL, dir, O_RDONLY | O_DIRECTORY, 0);
145 }
146 errno = 0; /* top-level file: no parent component to confine */
147 return -1;
148 }
149
150 /* Resolve the file's parent anchored at the absolute module root, never the
151 * process CWD: on cygwin the CWD is path-based, so a removal that trusted
152 * openat(".") could be raced (a parent-symlink flip) into unlinking outside
153 * the module. Reconstruct the module-relative path from F_PATHNAME + f_name
154 * (as send_files does) and walk it confined beneath module_dir -- the
155 * per-component O_NOFOLLOW walk refuses the flipped symlink. */
156 path = F_PATHNAME(file);
157 if (!path)
158 path = "";
159 slash = *path ? "/" : "";
160 slen = snprintf(secure_path, sizeof secure_path, "%s%s%s", path, slash, fname);
161 if (slen < 0 || slen >= (int)sizeof secure_path) {
162 errno = ENAMETOOLONG;
163 return -1;
164 }
165 relp = secure_path;
166 while (*relp == '/')
167 relp++;
168
169 bslash = strrchr(relp, '/');
170 if (bslash) {
171 char dir[MAXPATHLEN];
172 size_t dlen = (size_t)(bslash - relp);
173 if (dlen >= sizeof dir) {
174 errno = ENAMETOOLONG;
175 return -1;
176 }
177 memcpy(dir, relp, dlen);
178 dir[dlen] = '\0';
179 dfd = secure_relative_open(module_dir, dir, O_RDONLY | O_DIRECTORY, 0);
180 } else
181 dfd = secure_relative_open(module_dir, "", O_RDONLY | O_DIRECTORY, 0);
182
183 /* The leaf is the same last component either way; take it from the caller's
184 * persistent fname buffer, not the local secure_path. */
185 fslash = strrchr(fname, '/');
186 *bname_p = fslash ? fslash + 1 : fname;
187
188 if (dfd >= 0 && (fl = fcntl(dfd, F_GETFD)) >= 0)
189 fcntl(dfd, F_SETFD, fl | FD_CLOEXEC);
190 return dfd;
191 #else
192 (void)file; (void)fname; (void)bname_p;
193 errno = 0;
194 return -1;
195 #endif
196 }
197
198 /* Go through the do_*() wrapper rather than a raw unlinkat(): it carries the
199 * dry_run no-op and the read-only/list-only refusal that do_unlink() applies
200 * on the non-fd path, plus the missing-AT_FDCWD fallback. */
201 static int secure_remove_source_file(int dfd, const char *bname)
202 {
203 return do_unlink_atfd(dfd, bname, 0);
204 }
205
206 /* Open `relpath` (relative to `anchor`: NULL=cwd, else an absolute trusted root)
207 * with `flags`, opening the leaf via the shared held ancestor-dirfd stack
208 * (held_dir_path_fd) so a directory is walked once, not once per file. The leaf
209 * semantics are identical to secure_relative_open() -- it always O_NOFOLLOWs a
210 * file leaf and folds in O_NOATIME, both preserved here. An uncacheable path
211 * (held_dir_path_fd returns -1) falls back to the full confined walk. */
212 static int sender_open_confined(const char *anchor, const char *relpath, int flags)
213 {
214 #ifdef AT_FDCWD
215 const char *slash = strrchr(relpath, '/');
216 const char *bname;
217 char dirbuf[MAXPATHLEN];
218 const char *dir;
219 int dfd;
220
221 if (slash) {
222 size_t dlen = slash - relpath;
223 if (dlen >= sizeof dirbuf) {
224 errno = ENAMETOOLONG;
225 return -1;
226 }
227 memcpy(dirbuf, relpath, dlen);
228 dirbuf[dlen] = '\0';
229 dir = dirbuf;
230 bname = slash + 1;
231 } else {
232 dir = ""; /* file directly in the anchor dir */
233 bname = relpath;
234 }
235
236 #ifdef O_NOATIME
237 if (open_noatime)
238 flags |= O_NOATIME;
239 #endif
240 dfd = held_dir_path_fd(anchor, dir);
241 if (dfd < 0)
242 return secure_relative_open(anchor, relpath, flags | O_NOFOLLOW, 0);
243 return openat(dfd, bname, flags | O_NOFOLLOW, 0);
244 #else
245 /* No *at() support: secure_relative_open is a plain open() here (no walk,
246 * so nothing to amortise); use it directly to keep the anchor semantics. */
247 return secure_relative_open(anchor, relpath, flags | O_NOFOLLOW, 0);
248 #endif
249 }
250
251 /* Open the content of `relpath` for a symlink-following transfer mode (-L /
252 * --copy-unsafe-links / -k) while staying confined beneath `anchor`. The leaf
253 * O_NOFOLLOW that sender_open_confined() applies refuses an in-tree symlink the
254 * operator explicitly asked to follow, so resolve the link ourselves: read it,
255 * refuse an absolute or "../"-escaping target (a module escape), and re-resolve
256 * the relative target through secure_relative_open() -- which follows in-tree
257 * links and rejects an escape above the anchor -- looping for a symlink chain.
258 * The final open is still O_NOFOLLOW, so a raced flip at the resolved leaf is
259 * refused. This keeps the module boundary while honouring --copy-links. */
260 static int sender_open_copylinks_confined(const char *anchor, const char *relpath)
261 {
262 #if defined AT_FDCWD && defined O_NOFOLLOW
263 char cur[MAXPATHLEN];
264 int hops = 32;
265 int extra = 0;
266 #ifdef O_NOATIME
267 if (open_noatime)
268 extra |= O_NOATIME;
269 #endif
270 if (strlcpy(cur, relpath, sizeof cur) >= sizeof cur) {
271 errno = ENAMETOOLONG;
272 return -1;
273 }
274 while (hops-- > 0) {
275 const char *slash = strrchr(cur, '/');
276 const char *bname;
277 char dir[MAXPATHLEN], tgt[MAXPATHLEN];
278 int pdfd, fd, e;
279 ssize_t n;
280 if (slash) {
281 size_t dlen = slash - cur;
282 if (dlen >= sizeof dir) { errno = ENAMETOOLONG; return -1; }
283 memcpy(dir, cur, dlen);
284 dir[dlen] = '\0';
285 bname = slash + 1;
286 } else {
287 dir[0] = '\0';
288 bname = cur;
289 }
290 /* anchor is checked explicitly: the resolver treats a NULL anchor as
291 * "relative to cwd", so it is a legal argument for the else branch --
292 * only this branch would hand it to strcmp(). */
293 if (am_daemon && module_dirfd >= 0 && module_dir && anchor
294 && strcmp(anchor, module_dir) == 0)
295 pdfd = secure_relative_open_at_beneath(module_dirfd, dir,
296 O_RDONLY | O_DIRECTORY, 0);
297 else
298 pdfd = secure_relative_open(anchor, dir,
299 O_RDONLY | O_DIRECTORY, 0);
300 if (pdfd < 0)
301 return -1;
302 n = do_readlink_atfd(pdfd, bname, tgt, sizeof tgt - 1);
303 e = errno;
304 if (n < 0) {
305 /* EINVAL: not a symlink -> the resolved target file. Open it
306 * O_NOFOLLOW (a raced symlink flip is still refused). */
307 fd = e == EINVAL
308 ? openat(pdfd, bname, O_RDONLY | O_NOFOLLOW | O_BINARY | extra, 0)
309 : -1;
310 close(pdfd);
311 if (n < 0 && e != EINVAL)
312 errno = e;
313 return fd;
314 }
315 close(pdfd);
316 tgt[n] = '\0';
317 if (tgt[0] == '/') { /* absolute target escapes the module */
318 errno = ELOOP;
319 return -1;
320 }
321 if ((size_t)snprintf(cur, sizeof cur, "%s%s%s",
322 dir, *dir ? "/" : "", tgt) >= sizeof cur) {
323 errno = ENAMETOOLONG;
324 return -1;
325 }
326 }
327 errno = ELOOP;
328 return -1;
329 #else
330 return secure_relative_open(anchor, relpath, O_RDONLY | O_NOFOLLOW, 0);
331 #endif
332 }
333
334 /**
335 * Receive the checksums for a buffer
336 **/
337 static struct sum_struct *receive_sums(int f)
338 {
339 struct sum_struct *s = new(struct sum_struct);
340 int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
341 OFF_T offset = 0;
342 int32 i;
343
344 read_sum_head(f, s);
345
346 s->sums = NULL;
347
348 if (DEBUG_GTE(DELTASUM, 3)) {
349 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
350 big_num(s->count), (long)s->blength, (long)s->remainder);
351 }
352
353 if (append_mode > 0) {
354 s->flength = (OFF_T)s->count * s->blength;
355 if (s->remainder)
356 s->flength -= s->blength - s->remainder;
357 return s;
358 }
359
360 if (s->count == 0)
361 return(s);
362
363 s->sums = new_array(struct sum_buf, s->count);
364 s->sum2_array = new_array(char, (size_t)s->count * xfer_sum_len);
365
366 for (i = 0; i < s->count; i++) {
367 s->sums[i].sum1 = read_int(f);
368 read_buf(f, sum2_at(s, i), s->s2length);
369
370 s->sums[i].offset = offset;
371 s->sums[i].flags = 0;
372
373 if (i == s->count-1 && s->remainder != 0)
374 s->sums[i].len = s->remainder;
375 else
376 s->sums[i].len = s->blength;
377 offset += s->sums[i].len;
378
379 if (lull_mod && !(i % lull_mod))
380 maybe_send_keepalive(time(NULL), True);
381
382 if (DEBUG_GTE(DELTASUM, 3)) {
383 rprintf(FINFO,
384 "chunk[%d] len=%d offset=%s sum1=%08x\n",
385 i, s->sums[i].len, big_num(s->sums[i].offset),
386 s->sums[i].sum1);
387 }
388 }
389
390 s->flength = offset;
391
392 return s;
393 }
394
395 void successful_send(int ndx)
396 {
397 char fname[MAXPATHLEN];
398 char *failed_op;
399 const char *bname = NULL;
400 struct file_struct *file;
401 struct file_list *flist;
402 STRUCT_STAT st;
403 int dfd = -1, secure_errno = 0;
404
405 if (!remove_source_files)
406 return;
407
408 flist = flist_for_ndx(ndx, "successful_send");
409 if (ndx < flist->ndx_start)
410 exit_cleanup(RERR_PROTOCOL);
411 file = flist->files[ndx - flist->ndx_start];
412 if (!change_pathname(file, NULL, 0))
413 return;
414 f_name(file, fname);
415
416 dfd = secure_sender_parent_fd(file, fname, &bname);
417 if (dfd < 0)
418 secure_errno = errno;
419
420 if (dfd < 0 && secure_errno) {
421 errno = secure_errno;
422 failed_op = "secure-open-parent";
423 goto failed;
424 }
425
426 if (dfd >= 0
427 ? (copy_links ? do_stat_atfd(dfd, bname, &st) : do_lstat_atfd(dfd, bname, &st)) < 0
428 : (copy_links ? do_stat(fname, &st) : do_lstat(fname, &st)) < 0) {
429 failed_op = "re-lstat";
430 goto failed;
431 }
432
433 if (local_server
434 && (int64)st.st_dev == IVAL64(num_dev_ino_buf, 4)
435 && (int64)st.st_ino == IVAL64(num_dev_ino_buf, 4 + 8)) {
436 rprintf(FERROR_XFER, "ERROR: Skipping sender remove of destination file: %s\n", fname);
437 if (dfd >= 0)
438 close(dfd);
439 return;
440 }
441
442 if (st.st_size != F_LENGTH(file) || st.st_mtime != file->modtime
443 #ifdef ST_MTIME_NSEC
444 || (NSEC_BUMP(file) && (uint32)st.ST_MTIME_NSEC != F_MOD_NSEC(file))
445 #endif
446 ) {
447 rprintf(FERROR_XFER, "ERROR: Skipping sender remove for changed file: %s\n", fname);
448 if (dfd >= 0)
449 close(dfd);
450 return;
451 }
452
453 if (dfd >= 0 ? secure_remove_source_file(dfd, bname) < 0 : do_unlink(fname) < 0) {
454 failed_op = "remove";
455 failed:
456 if (errno == ENOENT)
457 rprintf(FINFO, "sender file already removed: %s\n", fname);
458 else
459 rsyserr(FERROR_XFER, errno, "sender failed to %s %s", failed_op, fname);
460 } else {
461 if (INFO_GTE(REMOVE, 1))
462 rprintf(FINFO, "sender removed %s\n", fname);
463 }
464 if (dfd >= 0)
465 close(dfd);
466 }
467
468 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
469 const char *fname, struct file_struct *file,
470 uchar fnamecmp_type, char *buf, int len)
471 {
472 write_ndx(f_out, ndx);
473 if (protocol_version < 29)
474 return;
475 write_shortint(f_out, iflags);
476 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
477 write_byte(f_out, fnamecmp_type);
478 if (iflags & ITEM_XNAME_FOLLOWS)
479 write_vstring(f_out, buf, len);
480 #ifdef SUPPORT_XATTRS
481 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
482 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
483 send_xattr_request(fname, file, f_out);
484 #endif
485 }
486
487 void send_files(int f_in, int f_out)
488 {
489 int fd = -1;
490 struct sum_struct *s;
491 struct map_struct *mbuf = NULL;
492 STRUCT_STAT st;
493 char fname[MAXPATHLEN], xname[MAXPATHLEN];
494 const char *path, *slash;
495 uchar fnamecmp_type;
496 int iflags, xlen;
497 struct file_struct *file;
498 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
499 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
500 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
501 int f_xfer = write_batch < 0 ? batch_fd : f_out;
502 int save_io_error = io_error;
503 int ndx, j;
504
505 if (DEBUG_GTE(SEND, 1))
506 rprintf(FINFO, "send_files starting\n");
507
508 if (whole_file < 0)
509 whole_file = 0;
510
511 progress_init();
512
513 while (1) {
514 if (inc_recurse) {
515 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
516 extra_flist_sending_enabled = !flist_eof;
517 }
518
519 /* This call also sets cur_flist. */
520 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
521 xname, &xlen);
522 extra_flist_sending_enabled = False;
523
524 if (ndx == NDX_DONE) {
525 if (!am_server && cur_flist) {
526 set_current_file_index(NULL, 0);
527 if (INFO_GTE(PROGRESS, 2))
528 end_progress(0);
529 }
530 if (inc_recurse && first_flist) {
531 file_old_total -= first_flist->used;
532 flist_free(first_flist);
533 if (first_flist) {
534 if (first_flist == cur_flist)
535 file_old_total = cur_flist->used;
536 write_ndx(f_out, NDX_DONE);
537 continue;
538 }
539 }
540 if (++phase > max_phase)
541 break;
542 if (DEBUG_GTE(SEND, 1))
543 rprintf(FINFO, "send_files phase=%d\n", phase);
544 write_ndx(f_out, NDX_DONE);
545 continue;
546 }
547
548 if (inc_recurse)
549 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
550
551 if (ndx - cur_flist->ndx_start >= 0)
552 file = cur_flist->files[ndx - cur_flist->ndx_start];
553 else if (cur_flist->parent_ndx < 0
554 || cur_flist->parent_ndx >= dir_flist->used)
555 exit_cleanup(RERR_PROTOCOL);
556 else
557 file = dir_flist->files[cur_flist->parent_ndx];
558 if (!F_IS_ACTIVE(file)) {
559 rprintf(FERROR,
560 "rsync: refusing transfer of cleared file index %d\n",
561 ndx);
562 exit_cleanup(RERR_PROTOCOL);
563 }
564 if (F_PATHNAME(file)) {
565 path = F_PATHNAME(file);
566 slash = "/";
567 } else {
568 path = slash = "";
569 }
570 if (!change_pathname(file, NULL, 0))
571 continue;
572 f_name(file, fname);
573
574 if (DEBUG_GTE(SEND, 1))
575 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
576
577 #ifdef SUPPORT_XATTRS
578 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
579 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
580 recv_xattr_request(file, f_in);
581 #endif
582
583 if (!(iflags & ITEM_TRANSFER)) {
584 maybe_log_item(file, iflags, itemizing, xname);
585 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
586 if (iflags & ITEM_IS_NEW) {
587 stats.created_files++;
588 if (S_ISREG(file->mode)) {
589 /* Nothing further to count. */
590 } else if (S_ISDIR(file->mode))
591 stats.created_dirs++;
592 #ifdef SUPPORT_LINKS
593 else if (S_ISLNK(file->mode))
594 stats.created_symlinks++;
595 #endif
596 else if (IS_DEVICE(file->mode))
597 stats.created_devices++;
598 else
599 stats.created_specials++;
600 }
601 continue;
602 }
603 if (phase == 2) {
604 rprintf(FERROR,
605 "got transfer request in phase 2 [%s]\n",
606 who_am_i());
607 exit_cleanup(RERR_PROTOCOL);
608 }
609
610 if (file->flags & FLAG_FILE_SENT) {
611 if (csum_length == SHORT_SUM_LENGTH) {
612 /* For inplace: redo phase turns off the backup
613 * flag so that we do a regular inplace send. */
614 make_backups = -make_backups;
615 append_mode = -append_mode;
616 csum_length = SUM_LENGTH;
617 }
618 } else {
619 if (csum_length != SHORT_SUM_LENGTH) {
620 make_backups = -make_backups;
621 append_mode = -append_mode;
622 csum_length = SHORT_SUM_LENGTH;
623 }
624 if (iflags & ITEM_IS_NEW)
625 stats.created_files++;
626 }
627
628 updating_basis_file = (inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR)
629 || (inplace && (protocol_version >= 29 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0));
630
631 if (!am_server)
632 set_current_file_index(file, ndx);
633 stats.xferred_files++;
634 stats.total_transferred_size += F_LENGTH(file);
635
636 remember_initial_stats();
637
638 if (!do_xfers) { /* log the transfer */
639 log_item(FCLIENT, file, iflags, NULL);
640 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
641 continue;
642 }
643
644 if (!(s = receive_sums(f_in))) {
645 io_error |= IOERR_GENERAL;
646 rprintf(FERROR_XFER, "receive_sums failed\n");
647 exit_cleanup(RERR_PROTOCOL);
648 }
649
650 if (symlink_optout_allowed()) {
651 /* Module opted out of symlink confinement ("insecure links =
652 * yes", admin-only) -- or a non-daemon --insecure-links: legacy
653 * unconfined open, restoring the pre-hardening content read
654 * (re-opening the escape for that module; documented). */
655 fd = do_open_checklinks(fname);
656 } else if (secure_relpath_active()) {
657 /* Open from module root to prevent TOCTOU race where
658 * change_pathname's chdir follows a directory symlink.
659 * Reconstruct the full path relative to module_dir
660 * from F_PATHNAME (path) and f_name (fname). */
661 char secure_path[MAXPATHLEN];
662 const char *relp;
663 int slen = snprintf(secure_path, sizeof secure_path, "%s%s%s", path, slash, fname);
664 if (slen >= (int)sizeof secure_path) {
665 io_error |= IOERR_GENERAL;
666 rprintf(FERROR_XFER, "path too long: %s%s%s\n", path, slash, fname);
667 free_sums(s);
668 if (protocol_version >= 30)
669 send_msg_int(MSG_NO_SEND, ndx);
670 continue;
671 }
672 /* A module with `path = /` makes F_PATHNAME absolute, so the
673 * joined path starts with '/'; strip leading slashes to a
674 * module-relative path that secure_relative_open accepts (#897). */
675 relp = secure_path;
676 while (*relp == '/')
677 relp++;
678 /* A symlink-following mode must follow an in-tree symlink leaf the
679 * operator asked for, still confined to the module; the default
680 * keeps the O_NOFOLLOW leaf so a raced leaf symlink is refused. */
681 if (copy_links || copy_unsafe_links || copy_dirlinks || insecure_links)
682 fd = sender_open_copylinks_confined(module_dir, relp);
683 else
684 fd = sender_open_confined(module_dir, relp, O_RDONLY);
685 } else if (!copy_links && !copy_unsafe_links && !copy_dirlinks && !insecure_links) {
686 /* Default symlink handling (no dir-link following): the scan
687 * recorded this as a regular file. Open it confined beneath the
688 * transfer root: an in-tree symlinked parent (e.g. -R keeps one in
689 * the path) is followed beneath the root, a parent raced into a
690 * symlink pointing out of the tree is refused, and O_NOFOLLOW
691 * governs the leaf so a raced leaf symlink is refused. A
692 * symlink-following mode (-L/--copy-unsafe-links/-k) or
693 * --insecure-links keeps the legacy open below. */
694 if (fname[0] == '/') {
695 /* --relative (or a --files-from absolute name) keeps the
696 * full absolute path as fname; the transfer root is then "/",
697 * so anchor the confined open there and strip the leading
698 * slash to the module-relative path the resolver wants -- it
699 * rejects an absolute relpath outright. */
700 const char *relp = fname;
701 while (*relp == '/')
702 relp++;
703 fd = sender_open_confined("/", relp, O_RDONLY);
704 } else
705 fd = sender_open_confined(NULL, fname, O_RDONLY);
706 } else {
707 fd = do_open_checklinks(fname);
708 }
709 if (fd == -1) {
710 if (errno == ENOENT) {
711 enum logcode c = am_daemon && protocol_version < 28 ? FERROR : FWARNING;
712 io_error |= IOERR_VANISHED;
713 rprintf(c, "file has vanished: %s\n",
714 full_fname(fname));
715 } else {
716 io_error |= IOERR_GENERAL;
717 rsyserr(FERROR_XFER, errno,
718 "send_files failed to open %s",
719 full_fname(fname));
720 }
721 free_sums(s);
722 if (protocol_version >= 30)
723 send_msg_int(MSG_NO_SEND, ndx);
724 continue;
725 }
726
727 /* map the local file */
728 if (do_fstat(fd, &st) != 0) {
729 io_error |= IOERR_GENERAL;
730 rsyserr(FERROR_XFER, errno, "fstat failed");
731 free_sums(s);
732 close(fd);
733 exit_cleanup(RERR_FILEIO);
734 }
735
736 if (IS_DEVICE(st.st_mode)) {
737 if (!copy_devices) {
738 rprintf(FERROR, "attempt to copy device contents without --copy-devices\n");
739 exit_cleanup(RERR_PROTOCOL);
740 }
741 if (st.st_size == 0)
742 st.st_size = get_device_size(fd, fname);
743 }
744
745 if (append_mode > 0 && st.st_size < F_LENGTH(file)) {
746 rprintf(FWARNING, "skipped diminished file: %s\n",
747 full_fname(fname));
748 free_sums(s);
749 close(fd);
750 if (protocol_version >= 30)
751 send_msg_int(MSG_NO_SEND, ndx);
752 continue;
753 }
754
755 if (st.st_size) {
756 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
757 mbuf = map_file(fd, st.st_size, read_size, s->blength);
758 } else
759 mbuf = NULL;
760
761 if (DEBUG_GTE(DELTASUM, 2)) {
762 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
763 path,slash,fname, big_num(st.st_size));
764 }
765
766 write_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);
767 write_sum_head(f_xfer, s);
768
769 if (DEBUG_GTE(DELTASUM, 2))
770 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
771
772 if (log_before_transfer)
773 log_item(FCLIENT, file, iflags, NULL);
774 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
775 rprintf(FCLIENT, "%s\n", fname);
776
777 set_compression(fname);
778
779 match_sums(f_xfer, s, mbuf, st.st_size);
780 if (INFO_GTE(PROGRESS, 1))
781 end_progress(st.st_size);
782 else if (want_progress_now)
783 instant_progress(fname);
784
785 log_item(log_code, file, iflags, NULL);
786
787 if (mbuf) {
788 j = unmap_file(mbuf);
789 if (j) {
790 io_error |= IOERR_GENERAL;
791 rsyserr(FERROR_XFER, j,
792 "read errors mapping %s",
793 full_fname(fname));
794 }
795 }
796 close(fd);
797
798 free_sums(s);
799
800 if (DEBUG_GTE(SEND, 1))
801 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
802
803 /* Flag that we actually sent this entry. */
804 file->flags |= FLAG_FILE_SENT;
805 }
806 if (make_backups < 0)
807 make_backups = -make_backups;
808
809 if (io_error != save_io_error && protocol_version >= 30)
810 send_msg_int(MSG_IO_ERROR, io_error);
811
812 if (DEBUG_GTE(SEND, 1))
813 rprintf(FINFO, "send files finished\n");
814
815 match_report();
816
817 write_ndx(f_out, NDX_DONE);
818 }