Annotation of src/usr.bin/rsync/receiver.c, Revision 1.10
1.10 ! deraadt 1: /* $Id: receiver.c,v 1.9 2019/02/16 05:06:30 deraadt Exp $ */
1.1 benno 2:
3: /*
4: * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #include <sys/mman.h>
19: #include <sys/stat.h>
20:
21: #include <assert.h>
22: #include <errno.h>
23: #include <fcntl.h>
24: #include <inttypes.h>
25: #include <math.h>
26: #include <poll.h>
27: #include <stdio.h>
28: #include <stdlib.h>
29: #include <string.h>
30: #include <time.h>
31: #include <unistd.h>
32:
33: #include "extern.h"
34:
35: enum pfdt {
36: PFD_SENDER_IN = 0, /* input from the sender */
37: PFD_UPLOADER_IN, /* uploader input from a local file */
38: PFD_DOWNLOADER_IN, /* downloader input from a local file */
39: PFD_SENDER_OUT, /* output to the sender */
40: PFD__MAX
41: };
1.7 florian 42:
43: int
44: rsync_set_metadata(struct sess *sess, int newfile,
45: int fd, const struct flist *f, const char *path)
46: {
1.8 florian 47: uid_t uid = (uid_t)-1;
48: gid_t gid = (gid_t)-1;
1.7 florian 49: struct timespec tv[2];
50:
51: /*
52: * Conditionally adjust identifiers.
53: * If we have an EPERM, report it but continue on: this just
54: * means that we're mapping into an unknown (or disallowed)
55: * group identifier.
56: */
57:
1.8 florian 58: if (getuid() == 0 && sess->opts->preserve_uids)
59: uid = f->st.uid;
60: if (sess->opts->preserve_gids)
61: gid = f->st.gid;
62:
63: if (uid != (uid_t)-1 || gid != (gid_t)-1) {
1.7 florian 64: if (fchown(fd, uid, gid) == -1) {
65: if (errno != EPERM) {
66: ERR(sess, "%s: fchown", path);
67: return 0;
68: }
69: WARNX(sess, "%s: identity unknown or not available "
70: "to user.group: %u.%u", f->path, uid, gid);
71: } else
72: LOG4(sess, "%s: updated uid and/or gid", f->path);
73: }
74:
75: /* Conditionally adjust file modification time. */
76:
77: if (sess->opts->preserve_times) {
78: tv[0].tv_sec = time(NULL);
79: tv[0].tv_nsec = 0;
80: tv[1].tv_sec = f->st.mtime;
81: tv[1].tv_nsec = 0;
82: if (futimens(fd, tv) == -1) {
83: ERR(sess, "%s: futimens", path);
84: return 0;
85: }
86: LOG4(sess, "%s: updated date", f->path);
87: }
88:
89: return 1;
90: }
1.1 benno 91:
1.2 benno 92: /*
1.1 benno 93: * Pledges: unveil, rpath, cpath, wpath, stdio, fattr.
94: * Pledges (dry-run): -cpath, -wpath, -fattr.
95: */
96: int
1.9 deraadt 97: rsync_receiver(struct sess *sess, int fdin, int fdout, const char *root)
1.1 benno 98: {
99: struct flist *fl = NULL, *dfl = NULL;
100: size_t i, flsz = 0, dflsz = 0, excl;
101: char *tofree;
102: int rc = 0, dfd = -1, phase = 0, c;
1.2 benno 103: int32_t ioerror;
1.1 benno 104: struct pollfd pfd[PFD__MAX];
105: struct download *dl = NULL;
1.2 benno 106: struct upload *ul = NULL;
1.1 benno 107: mode_t oumask;
108:
1.6 benno 109: if (pledge("stdio rpath wpath cpath fattr getpw unveil", NULL) == -1) {
1.1 benno 110: ERR(sess, "pledge");
111: goto out;
112: }
113:
114: /* Client sends zero-length exclusions. */
115:
1.4 deraadt 116: if (!sess->opts->server &&
117: !io_write_int(sess, fdout, 0)) {
1.1 benno 118: ERRX1(sess, "io_write_int");
119: goto out;
120: }
121:
122: if (sess->opts->server && sess->opts->del) {
1.4 deraadt 123: if (!io_read_size(sess, fdin, &excl)) {
1.1 benno 124: ERRX1(sess, "io_read_size");
125: goto out;
126: } else if (0 != excl) {
127: ERRX(sess, "exclusion list is non-empty");
128: goto out;
129: }
130: }
131:
132: /*
133: * Start by receiving the file list and our mystery number.
134: * These we're going to be touching on our local system.
135: */
136:
1.4 deraadt 137: if (!flist_recv(sess, fdin, &fl, &flsz)) {
1.1 benno 138: ERRX1(sess, "flist_recv");
139: goto out;
1.2 benno 140: }
141:
1.1 benno 142: /* The IO error is sent after the file list. */
143:
1.4 deraadt 144: if (!io_read_int(sess, fdin, &ioerror)) {
1.1 benno 145: ERRX1(sess, "io_read_int");
146: goto out;
147: } else if (0 != ioerror) {
148: ERRX1(sess, "io_error is non-zero");
149: goto out;
150: }
151:
1.5 deraadt 152: if (flsz == 0 && !sess->opts->server) {
1.1 benno 153: WARNX(sess, "receiver has empty file list: exiting");
154: rc = 1;
155: goto out;
1.4 deraadt 156: } else if (!sess->opts->server)
1.1 benno 157: LOG1(sess, "Transfer starting: %zu files", flsz);
158:
159: LOG2(sess, "%s: receiver destination", root);
160:
161: /*
162: * Create the path for our destination directory, if we're not
163: * in dry-run mode (which would otherwise crash w/the pledge).
164: * This uses our current umask: we might set the permissions on
165: * this directory in post_dir().
166: */
167:
1.4 deraadt 168: if (!sess->opts->dry_run) {
1.5 deraadt 169: if ((tofree = strdup(root)) == NULL) {
1.1 benno 170: ERR(sess, "strdup");
171: goto out;
172: } else if (mkpath(sess, tofree) < 0) {
173: ERRX1(sess, "%s: mkpath", root);
174: free(tofree);
175: goto out;
176: }
177: free(tofree);
178: }
179:
180: /*
181: * Disable umask() so we can set permissions fully.
182: * Then open the directory iff we're not in dry_run.
183: */
184:
185: oumask = umask(0);
186:
1.4 deraadt 187: if (!sess->opts->dry_run) {
1.1 benno 188: dfd = open(root, O_RDONLY | O_DIRECTORY, 0);
1.5 deraadt 189: if (dfd == -1) {
1.1 benno 190: ERR(sess, "%s: open", root);
191: goto out;
192: }
193: }
194:
195: /*
196: * Begin by conditionally getting all files we have currently
197: * available in our destination.
198: */
199:
1.2 benno 200: if (sess->opts->del &&
1.1 benno 201: sess->opts->recursive &&
1.4 deraadt 202: !flist_gen_dels(sess, root, &dfl, &dflsz, fl, flsz)) {
1.1 benno 203: ERRX1(sess, "flist_gen_local");
204: goto out;
205: }
206:
207: /*
208: * Make our entire view of the file-system be limited to what's
209: * in the root directory.
210: * This prevents us from accidentally (or "under the influence")
211: * writing into other parts of the file-system.
212: */
213:
1.5 deraadt 214: if (unveil(root, "rwc") == -1) {
1.1 benno 215: ERR(sess, "%s: unveil", root);
216: goto out;
1.5 deraadt 217: } else if (unveil(NULL, NULL) == -1) {
1.1 benno 218: ERR(sess, "%s: unveil", root);
219: goto out;
220: }
221:
222: /* If we have a local set, go for the deletion. */
223:
1.4 deraadt 224: if (!flist_del(sess, dfd, dfl, dflsz)) {
1.1 benno 225: ERRX1(sess, "flist_del");
226: goto out;
227: }
228:
229: /* Initialise poll events to listen from the sender. */
230:
231: pfd[PFD_SENDER_IN].fd = fdin;
232: pfd[PFD_UPLOADER_IN].fd = -1;
233: pfd[PFD_DOWNLOADER_IN].fd = -1;
234: pfd[PFD_SENDER_OUT].fd = fdout;
235:
236: pfd[PFD_SENDER_IN].events = POLLIN;
237: pfd[PFD_UPLOADER_IN].events = POLLIN;
238: pfd[PFD_DOWNLOADER_IN].events = POLLIN;
239: pfd[PFD_SENDER_OUT].events = POLLOUT;
240:
1.2 benno 241: ul = upload_alloc(sess, dfd, fdout,
1.1 benno 242: CSUM_LENGTH_PHASE1, fl, flsz, oumask);
1.5 deraadt 243: if (ul == NULL) {
1.1 benno 244: ERRX1(sess, "upload_alloc");
245: goto out;
246: }
247:
248: dl = download_alloc(sess, fdin, fl, flsz, dfd);
1.5 deraadt 249: if (dl == NULL) {
1.1 benno 250: ERRX1(sess, "download_alloc");
251: goto out;
252: }
253:
254: LOG2(sess, "%s: ready for phase 1 data", root);
255:
256: for (;;) {
1.5 deraadt 257: if ((c = poll(pfd, PFD__MAX, INFTIM)) == -1) {
1.1 benno 258: ERR(sess, "poll");
259: goto out;
1.2 benno 260: }
1.1 benno 261:
1.2 benno 262: for (i = 0; i < PFD__MAX; i++)
1.1 benno 263: if (pfd[i].revents & (POLLERR|POLLNVAL)) {
264: ERRX(sess, "poll: bad fd");
265: goto out;
266: } else if (pfd[i].revents & POLLHUP) {
267: ERRX(sess, "poll: hangup");
268: goto out;
269: }
270:
271: /*
272: * If we have a read event and we're multiplexing, we
273: * might just have error messages in the pipe.
274: * It's important to flush these out so that we don't
275: * clog the pipe.
276: * Unset our polling status if there's nothing that
277: * remains in the pipe.
278: */
279:
280: if (sess->mplex_reads &&
281: (POLLIN & pfd[PFD_SENDER_IN].revents)) {
1.4 deraadt 282: if (!io_read_flush(sess, fdin)) {
1.1 benno 283: ERRX1(sess, "io_read_flush");
284: goto out;
1.5 deraadt 285: } else if (sess->mplex_read_remain == 0)
1.1 benno 286: pfd[PFD_SENDER_IN].revents &= ~POLLIN;
287: }
288:
289:
290: /*
291: * We run the uploader if we have files left to examine
292: * (i < flsz) or if we have a file that we've opened and
293: * is read to mmap.
294: */
295:
296: if ((POLLIN & pfd[PFD_UPLOADER_IN].revents) ||
297: (POLLOUT & pfd[PFD_SENDER_OUT].revents)) {
1.2 benno 298: c = rsync_uploader(ul,
299: &pfd[PFD_UPLOADER_IN].fd,
1.1 benno 300: sess, &pfd[PFD_SENDER_OUT].fd);
301: if (c < 0) {
302: ERRX1(sess, "rsync_uploader");
303: goto out;
304: }
305: }
306:
1.2 benno 307: /*
1.1 benno 308: * We need to run the downloader when we either have
309: * read events from the sender or an asynchronous local
310: * open is ready.
311: * XXX: we don't disable PFD_SENDER_IN like with the
312: * uploader because we might stop getting error
313: * messages, which will otherwise clog up the pipes.
314: */
315:
1.2 benno 316: if ((POLLIN & pfd[PFD_SENDER_IN].revents) ||
1.1 benno 317: (POLLIN & pfd[PFD_DOWNLOADER_IN].revents)) {
1.2 benno 318: c = rsync_downloader(dl, sess,
1.1 benno 319: &pfd[PFD_DOWNLOADER_IN].fd);
320: if (c < 0) {
321: ERRX1(sess, "rsync_downloader");
322: goto out;
1.5 deraadt 323: } else if (c == 0) {
324: assert(phase == 0);
1.1 benno 325: phase++;
326: LOG2(sess, "%s: receiver ready "
327: "for phase 2 data", root);
328: break;
329: }
330:
331: /*
332: * FIXME: if we have any errors during the
333: * download, most notably files getting out of
334: * sync between the send and the receiver, then
335: * here we should bump our checksum length and
336: * go into the second phase.
337: */
1.2 benno 338: }
1.1 benno 339: }
340:
341: /* Properly close us out by progressing through the phases. */
342:
1.5 deraadt 343: if (phase == 1) {
1.4 deraadt 344: if (!io_write_int(sess, fdout, -1)) {
1.1 benno 345: ERRX1(sess, "io_write_int");
346: goto out;
1.4 deraadt 347: } else if (!io_read_int(sess, fdin, &ioerror)) {
1.1 benno 348: ERRX1(sess, "io_read_int");
349: goto out;
1.5 deraadt 350: } else if (ioerror != -1) {
1.1 benno 351: ERRX(sess, "expected phase ack");
352: goto out;
353: }
354: }
355:
356: /*
357: * Now all of our transfers are complete, so we can fix up our
358: * directory permissions.
359: */
360:
1.4 deraadt 361: if (!rsync_uploader_tail(ul, sess)) {
1.1 benno 362: ERRX1(sess, "rsync_uploader_tail");
363: goto out;
364: }
365:
366: /* Process server statistics and say good-bye. */
367:
1.4 deraadt 368: if (!sess_stats_recv(sess, fdin)) {
1.1 benno 369: ERRX1(sess, "sess_stats_recv");
370: goto out;
1.4 deraadt 371: } else if (!io_write_int(sess, fdout, -1)) {
1.1 benno 372: ERRX1(sess, "io_write_int");
373: goto out;
374: }
375:
376: LOG2(sess, "receiver finished updating");
377: rc = 1;
378: out:
1.5 deraadt 379: if (dfd != -1)
1.1 benno 380: close(dfd);
381: upload_free(ul);
382: download_free(dl);
383: flist_free(fl, flsz);
384: flist_free(dfl, dflsz);
385: return rc;
386: }