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