1 /* $OpenBSD: main.c,v 1.65 2022/08/02 20:01:12 tb Exp $ */ 2 /* 3 * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 #include <sys/stat.h> 18 #include <sys/socket.h> 19 #include <sys/wait.h> 20 21 #include <assert.h> 22 #include <err.h> 23 #include <getopt.h> 24 #include <stdint.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 #include <util.h> 30 31 #include "extern.h" 32 33 int verbose; 34 int poll_contimeout; 35 int poll_timeout; 36 37 /* 38 * A remote host is has a colon before the first path separator. 39 * This works for rsh remote hosts (host:/foo/bar), implicit rsync 40 * remote hosts (host::/foo/bar), and explicit (rsync://host/foo). 41 * Return zero if local, non-zero if remote. 42 */ 43 static int 44 fargs_is_remote(const char *v) 45 { 46 size_t pos; 47 48 pos = strcspn(v, ":/"); 49 return v[pos] == ':'; 50 } 51 52 /* 53 * Test whether a remote host is specifically an rsync daemon. 54 * Return zero if not, non-zero if so. 55 */ 56 static int 57 fargs_is_daemon(const char *v) 58 { 59 size_t pos; 60 61 if (strncasecmp(v, "rsync://", 8) == 0) 62 return 1; 63 64 pos = strcspn(v, ":/"); 65 return v[pos] == ':' && v[pos + 1] == ':'; 66 } 67 68 /* 69 * Take the command-line filenames (e.g., rsync foo/ bar/ baz/) and 70 * determine our operating mode. 71 * For example, if the first argument is a remote file, this means that 72 * we're going to transfer from the remote to the local. 73 * We also make sure that the arguments are consistent, that is, if 74 * we're going to transfer from the local to the remote, that no 75 * filenames for the local transfer indicate remote hosts. 76 * Always returns the parsed and sanitised options. 77 */ 78 static struct fargs * 79 fargs_parse(size_t argc, char *argv[], struct opts *opts) 80 { 81 struct fargs *f = NULL; 82 char *cp, *ccp; 83 size_t i, j, len = 0; 84 85 /* Allocations. */ 86 87 if ((f = calloc(1, sizeof(struct fargs))) == NULL) 88 err(ERR_NOMEM, NULL); 89 90 f->sourcesz = argc - 1; 91 if ((f->sources = calloc(f->sourcesz, sizeof(char *))) == NULL) 92 err(ERR_NOMEM, NULL); 93 94 for (i = 0; i < argc - 1; i++) 95 if ((f->sources[i] = strdup(argv[i])) == NULL) 96 err(ERR_NOMEM, NULL); 97 98 if ((f->sink = strdup(argv[i])) == NULL) 99 err(ERR_NOMEM, NULL); 100 101 /* 102 * Test files for its locality. 103 * If the last is a remote host, then we're sending from the 104 * local to the remote host ("sender" mode). 105 * If the first, remote to local ("receiver" mode). 106 * If neither, a local transfer in sender style. 107 */ 108 109 f->mode = FARGS_SENDER; 110 111 if (fargs_is_remote(f->sink)) { 112 f->mode = FARGS_SENDER; 113 if ((f->host = strdup(f->sink)) == NULL) 114 err(ERR_NOMEM, NULL); 115 } 116 117 if (fargs_is_remote(f->sources[0])) { 118 if (f->host != NULL) 119 errx(ERR_SYNTAX, "both source and destination " 120 "cannot be remote files"); 121 f->mode = FARGS_RECEIVER; 122 if ((f->host = strdup(f->sources[0])) == NULL) 123 err(ERR_NOMEM, NULL); 124 } 125 126 if (f->host != NULL) { 127 if (strncasecmp(f->host, "rsync://", 8) == 0) { 128 /* rsync://host[:port]/module[/path] */ 129 f->remote = 1; 130 len = strlen(f->host) - 8 + 1; 131 memmove(f->host, f->host + 8, len); 132 if ((cp = strchr(f->host, '/')) == NULL) 133 errx(ERR_SYNTAX, 134 "rsync protocol requires a module name"); 135 *cp++ = '\0'; 136 f->module = cp; 137 if ((cp = strchr(f->module, '/')) != NULL) 138 *cp = '\0'; 139 if ((cp = strchr(f->host, ':')) != NULL) { 140 /* host:port --> extract port */ 141 *cp++ = '\0'; 142 opts->port = cp; 143 } 144 } else { 145 /* host:[/path] */ 146 cp = strchr(f->host, ':'); 147 assert(cp != NULL); 148 *cp++ = '\0'; 149 if (*cp == ':') { 150 /* host::module[/path] */ 151 f->remote = 1; 152 f->module = ++cp; 153 cp = strchr(f->module, '/'); 154 if (cp != NULL) 155 *cp = '\0'; 156 } 157 } 158 if ((len = strlen(f->host)) == 0) 159 errx(ERR_SYNTAX, "empty remote host"); 160 if (f->remote && strlen(f->module) == 0) 161 errx(ERR_SYNTAX, "empty remote module"); 162 } 163 164 /* Make sure we have the same "hostspec" for all files. */ 165 166 if (!f->remote) { 167 if (f->mode == FARGS_SENDER) 168 for (i = 0; i < f->sourcesz; i++) { 169 if (!fargs_is_remote(f->sources[i])) 170 continue; 171 errx(ERR_SYNTAX, 172 "remote file in list of local sources: %s", 173 f->sources[i]); 174 } 175 if (f->mode == FARGS_RECEIVER) 176 for (i = 0; i < f->sourcesz; i++) { 177 if (fargs_is_remote(f->sources[i]) && 178 !fargs_is_daemon(f->sources[i])) 179 continue; 180 if (fargs_is_daemon(f->sources[i])) 181 errx(ERR_SYNTAX, 182 "remote daemon in list of remote " 183 "sources: %s", f->sources[i]); 184 errx(ERR_SYNTAX, "local file in list of " 185 "remote sources: %s", f->sources[i]); 186 } 187 } else { 188 if (f->mode != FARGS_RECEIVER) 189 errx(ERR_SYNTAX, "sender mode for remote " 190 "daemon receivers not yet supported"); 191 for (i = 0; i < f->sourcesz; i++) { 192 if (fargs_is_daemon(f->sources[i])) 193 continue; 194 errx(ERR_SYNTAX, "non-remote daemon file " 195 "in list of remote daemon sources: " 196 "%s", f->sources[i]); 197 } 198 } 199 200 /* 201 * If we're not remote and a sender, strip our hostname. 202 * Then exit if we're a sender or a local connection. 203 */ 204 205 if (!f->remote) { 206 if (f->host == NULL) 207 return f; 208 if (f->mode == FARGS_SENDER) { 209 assert(f->host != NULL); 210 assert(len > 0); 211 j = strlen(f->sink); 212 memmove(f->sink, f->sink + len + 1, j - len); 213 return f; 214 } else if (f->mode != FARGS_RECEIVER) 215 return f; 216 } 217 218 /* 219 * Now strip the hostnames from the remote host. 220 * rsync://host/module/path -> module/path 221 * host::module/path -> module/path 222 * host:path -> path 223 * Also make sure that the remote hosts are the same. 224 */ 225 226 assert(f->host != NULL); 227 assert(len > 0); 228 229 for (i = 0; i < f->sourcesz; i++) { 230 cp = f->sources[i]; 231 j = strlen(cp); 232 if (f->remote && 233 strncasecmp(cp, "rsync://", 8) == 0) { 234 /* rsync://path */ 235 cp += 8; 236 if ((ccp = strchr(cp, ':'))) /* skip :port */ 237 *ccp = '\0'; 238 if (strncmp(cp, f->host, len) || 239 (cp[len] != '/' && cp[len] != '\0')) 240 errx(ERR_SYNTAX, "different remote host: %s", 241 f->sources[i]); 242 memmove(f->sources[i], 243 f->sources[i] + len + 8 + 1, 244 j - len - 8); 245 } else if (f->remote && strncmp(cp, "::", 2) == 0) { 246 /* ::path */ 247 memmove(f->sources[i], 248 f->sources[i] + 2, j - 1); 249 } else if (f->remote) { 250 /* host::path */ 251 if (strncmp(cp, f->host, len) || 252 (cp[len] != ':' && cp[len] != '\0')) 253 errx(ERR_SYNTAX, "different remote host: %s", 254 f->sources[i]); 255 memmove(f->sources[i], f->sources[i] + len + 2, 256 j - len - 1); 257 } else if (cp[0] == ':') { 258 /* :path */ 259 memmove(f->sources[i], f->sources[i] + 1, j); 260 } else { 261 /* host:path */ 262 if (strncmp(cp, f->host, len) || 263 (cp[len] != ':' && cp[len] != '\0')) 264 errx(ERR_SYNTAX, "different remote host: %s", 265 f->sources[i]); 266 memmove(f->sources[i], 267 f->sources[i] + len + 1, j - len); 268 } 269 } 270 271 return f; 272 } 273 274 static struct opts opts; 275 276 #define OP_ADDRESS 1000 277 #define OP_PORT 1001 278 #define OP_RSYNCPATH 1002 279 #define OP_TIMEOUT 1003 280 #define OP_VERSION 1004 281 #define OP_EXCLUDE 1005 282 #define OP_INCLUDE 1006 283 #define OP_EXCLUDE_FROM 1007 284 #define OP_INCLUDE_FROM 1008 285 #define OP_COMP_DEST 1009 286 #define OP_COPY_DEST 1010 287 #define OP_LINK_DEST 1011 288 #define OP_MAX_SIZE 1012 289 #define OP_MIN_SIZE 1013 290 #define OP_CONTIMEOUT 1014 291 292 const struct option lopts[] = { 293 { "address", required_argument, NULL, OP_ADDRESS }, 294 { "archive", no_argument, NULL, 'a' }, 295 { "compare-dest", required_argument, NULL, OP_COMP_DEST }, 296 #if 0 297 { "copy-dest", required_argument, NULL, OP_COPY_DEST }, 298 { "link-dest", required_argument, NULL, OP_LINK_DEST }, 299 #endif 300 { "compress", no_argument, NULL, 'z' }, 301 { "contimeout", required_argument, NULL, OP_CONTIMEOUT }, 302 { "del", no_argument, &opts.del, 1 }, 303 { "delete", no_argument, &opts.del, 1 }, 304 { "devices", no_argument, &opts.devices, 1 }, 305 { "no-devices", no_argument, &opts.devices, 0 }, 306 { "dry-run", no_argument, &opts.dry_run, 1 }, 307 { "exclude", required_argument, NULL, OP_EXCLUDE }, 308 { "exclude-from", required_argument, NULL, OP_EXCLUDE_FROM }, 309 { "group", no_argument, &opts.preserve_gids, 1 }, 310 { "no-group", no_argument, &opts.preserve_gids, 0 }, 311 { "help", no_argument, NULL, 'h' }, 312 { "include", required_argument, NULL, OP_INCLUDE }, 313 { "include-from", required_argument, NULL, OP_INCLUDE_FROM }, 314 { "links", no_argument, &opts.preserve_links, 1 }, 315 { "max-size", required_argument, NULL, OP_MAX_SIZE }, 316 { "min-size", required_argument, NULL, OP_MIN_SIZE }, 317 { "no-links", no_argument, &opts.preserve_links, 0 }, 318 { "no-motd", no_argument, &opts.no_motd, 1 }, 319 { "numeric-ids", no_argument, &opts.numeric_ids, 1 }, 320 { "owner", no_argument, &opts.preserve_uids, 1 }, 321 { "no-owner", no_argument, &opts.preserve_uids, 0 }, 322 { "perms", no_argument, &opts.preserve_perms, 1 }, 323 { "no-perms", no_argument, &opts.preserve_perms, 0 }, 324 { "port", required_argument, NULL, OP_PORT }, 325 { "recursive", no_argument, &opts.recursive, 1 }, 326 { "no-recursive", no_argument, &opts.recursive, 0 }, 327 { "rsh", required_argument, NULL, 'e' }, 328 { "rsync-path", required_argument, NULL, OP_RSYNCPATH }, 329 { "sender", no_argument, &opts.sender, 1 }, 330 { "server", no_argument, &opts.server, 1 }, 331 { "specials", no_argument, &opts.specials, 1 }, 332 { "no-specials", no_argument, &opts.specials, 0 }, 333 { "timeout", required_argument, NULL, OP_TIMEOUT }, 334 { "times", no_argument, &opts.preserve_times, 1 }, 335 { "no-times", no_argument, &opts.preserve_times, 0 }, 336 { "verbose", no_argument, &verbose, 1 }, 337 { "no-verbose", no_argument, &verbose, 0 }, 338 { "version", no_argument, NULL, OP_VERSION }, 339 { NULL, 0, NULL, 0 } 340 }; 341 342 int 343 main(int argc, char *argv[]) 344 { 345 pid_t child; 346 int fds[2], sd = -1, rc, c, st, i, lidx; 347 size_t basedir_cnt = 0; 348 struct sess sess; 349 struct fargs *fargs; 350 char **args; 351 const char *errstr; 352 353 /* Global pledge. */ 354 355 if (pledge("stdio unix rpath wpath cpath dpath inet fattr chown dns getpw proc exec unveil", 356 NULL) == -1) 357 err(ERR_IPC, "pledge"); 358 359 opts.max_size = opts.min_size = -1; 360 361 while ((c = getopt_long(argc, argv, "Dae:ghlnoprtvxz", lopts, &lidx)) 362 != -1) { 363 switch (c) { 364 case 'D': 365 opts.devices = 1; 366 opts.specials = 1; 367 break; 368 case 'a': 369 opts.recursive = 1; 370 opts.preserve_links = 1; 371 opts.preserve_perms = 1; 372 opts.preserve_times = 1; 373 opts.preserve_gids = 1; 374 opts.preserve_uids = 1; 375 opts.devices = 1; 376 opts.specials = 1; 377 break; 378 case 'e': 379 opts.ssh_prog = optarg; 380 break; 381 case 'g': 382 opts.preserve_gids = 1; 383 break; 384 case 'l': 385 opts.preserve_links = 1; 386 break; 387 case 'n': 388 opts.dry_run = 1; 389 break; 390 case 'o': 391 opts.preserve_uids = 1; 392 break; 393 case 'p': 394 opts.preserve_perms = 1; 395 break; 396 case 'r': 397 opts.recursive = 1; 398 break; 399 case 't': 400 opts.preserve_times = 1; 401 break; 402 case 'v': 403 verbose++; 404 break; 405 case 'x': 406 opts.one_file_system++; 407 break; 408 case 'z': 409 fprintf(stderr, "%s: -z not supported yet\n", getprogname()); 410 break; 411 case 0: 412 /* Non-NULL flag values (e.g., --sender). */ 413 break; 414 case OP_ADDRESS: 415 opts.address = optarg; 416 break; 417 case OP_CONTIMEOUT: 418 poll_contimeout = strtonum(optarg, 0, 60*60, &errstr); 419 if (errstr != NULL) 420 errx(ERR_SYNTAX, "timeout is %s: %s", 421 errstr, optarg); 422 break; 423 case OP_PORT: 424 opts.port = optarg; 425 break; 426 case OP_RSYNCPATH: 427 opts.rsync_path = optarg; 428 break; 429 case OP_TIMEOUT: 430 poll_timeout = strtonum(optarg, 0, 60*60, &errstr); 431 if (errstr != NULL) 432 errx(ERR_SYNTAX, "timeout is %s: %s", 433 errstr, optarg); 434 break; 435 case OP_EXCLUDE: 436 if (parse_rule(optarg, RULE_EXCLUDE) == -1) 437 errx(ERR_SYNTAX, "syntax error in exclude: %s", 438 optarg); 439 break; 440 case OP_INCLUDE: 441 if (parse_rule(optarg, RULE_INCLUDE) == -1) 442 errx(ERR_SYNTAX, "syntax error in include: %s", 443 optarg); 444 break; 445 case OP_EXCLUDE_FROM: 446 parse_file(optarg, RULE_EXCLUDE); 447 break; 448 case OP_INCLUDE_FROM: 449 parse_file(optarg, RULE_INCLUDE); 450 break; 451 case OP_COMP_DEST: 452 if (opts.alt_base_mode !=0 && 453 opts.alt_base_mode != BASE_MODE_COMPARE) { 454 errx(1, "option --%s conflicts with %s", 455 lopts[lidx].name, 456 alt_base_mode(opts.alt_base_mode)); 457 } 458 opts.alt_base_mode = BASE_MODE_COMPARE; 459 #if 0 460 goto basedir; 461 case OP_COPY_DEST: 462 if (opts.alt_base_mode !=0 && 463 opts.alt_base_mode != BASE_MODE_COPY) { 464 errx(1, "option --%s conflicts with %s", 465 lopts[lidx].name, 466 alt_base_mode(opts.alt_base_mode)); 467 } 468 opts.alt_base_mode = BASE_MODE_COPY; 469 goto basedir; 470 case OP_LINK_DEST: 471 if (opts.alt_base_mode !=0 && 472 opts.alt_base_mode != BASE_MODE_LINK) { 473 errx(1, "option --%s conflicts with %s", 474 lopts[lidx].name, 475 alt_base_mode(opts.alt_base_mode)); 476 } 477 opts.alt_base_mode = BASE_MODE_LINK; 478 479 basedir: 480 #endif 481 if (basedir_cnt >= MAX_BASEDIR) 482 errx(1, "too many --%s directories specified", 483 lopts[lidx].name); 484 opts.basedir[basedir_cnt++] = optarg; 485 break; 486 case OP_MAX_SIZE: 487 if (scan_scaled(optarg, &opts.max_size) == -1) 488 err(1, "bad max-size"); 489 break; 490 case OP_MIN_SIZE: 491 if (scan_scaled(optarg, &opts.min_size) == -1) 492 err(1, "bad min-size"); 493 break; 494 case OP_VERSION: 495 fprintf(stderr, "openrsync: protocol version %u\n", 496 RSYNC_PROTOCOL); 497 exit(0); 498 case 'h': 499 default: 500 goto usage; 501 } 502 } 503 504 argc -= optind; 505 argv += optind; 506 507 /* FIXME: reference implementation rsync accepts this. */ 508 509 if (argc < 2) 510 goto usage; 511 512 if (opts.port == NULL) 513 opts.port = "rsync"; 514 515 /* by default and for --contimeout=0 disable poll_contimeout */ 516 if (poll_contimeout == 0) 517 poll_contimeout = -1; 518 else 519 poll_contimeout *= 1000; 520 521 /* by default and for --timeout=0 disable poll_timeout */ 522 if (poll_timeout == 0) 523 poll_timeout = -1; 524 else 525 poll_timeout *= 1000; 526 527 /* 528 * This is what happens when we're started with the "hidden" 529 * --server option, which is invoked for the rsync on the remote 530 * host by the parent. 531 */ 532 533 if (opts.server) 534 exit(rsync_server(&opts, (size_t)argc, argv)); 535 536 /* 537 * Now we know that we're the client on the local machine 538 * invoking rsync(1). 539 * At this point, we need to start the client and server 540 * initiation logic. 541 * The client is what we continue running on this host; the 542 * server is what we'll use to connect to the remote and 543 * invoke rsync with the --server option. 544 */ 545 546 fargs = fargs_parse(argc, argv, &opts); 547 assert(fargs != NULL); 548 549 /* 550 * If we're contacting an rsync:// daemon, then we don't need to 551 * fork, because we won't start a server ourselves. 552 * Route directly into the socket code, unless a remote shell 553 * has explicitly been specified. 554 */ 555 556 if (fargs->remote && opts.ssh_prog == NULL) { 557 assert(fargs->mode == FARGS_RECEIVER); 558 if ((rc = rsync_connect(&opts, &sd, fargs)) == 0) { 559 rc = rsync_socket(&opts, sd, fargs); 560 close(sd); 561 } 562 exit(rc); 563 } 564 565 /* Drop the dns/inet possibility. */ 566 567 if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw proc exec unveil", 568 NULL) == -1) 569 err(ERR_IPC, "pledge"); 570 571 /* Create a bidirectional socket and start our child. */ 572 573 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds) == -1) 574 err(ERR_IPC, "socketpair"); 575 576 switch ((child = fork())) { 577 case -1: 578 err(ERR_IPC, "fork"); 579 case 0: 580 close(fds[0]); 581 if (pledge("stdio exec", NULL) == -1) 582 err(ERR_IPC, "pledge"); 583 584 memset(&sess, 0, sizeof(struct sess)); 585 sess.opts = &opts; 586 587 args = fargs_cmdline(&sess, fargs, NULL); 588 589 for (i = 0; args[i] != NULL; i++) 590 LOG2("exec[%d] = %s", i, args[i]); 591 592 /* Make sure the child's stdin is from the sender. */ 593 if (dup2(fds[1], STDIN_FILENO) == -1) 594 err(ERR_IPC, "dup2"); 595 if (dup2(fds[1], STDOUT_FILENO) == -1) 596 err(ERR_IPC, "dup2"); 597 execvp(args[0], args); 598 _exit(ERR_IPC); 599 /* NOTREACHED */ 600 default: 601 close(fds[1]); 602 if (!fargs->remote) 603 rc = rsync_client(&opts, fds[0], fargs); 604 else 605 rc = rsync_socket(&opts, fds[0], fargs); 606 break; 607 } 608 609 close(fds[0]); 610 611 if (waitpid(child, &st, 0) == -1) 612 err(ERR_WAITPID, "waitpid"); 613 614 /* 615 * If we don't already have an error (rc == 0), then inherit the 616 * error code of rsync_server() if it has exited. 617 * If it hasn't exited, it overrides our return value. 618 */ 619 620 if (rc == 0) { 621 if (WIFEXITED(st)) 622 rc = WEXITSTATUS(st); 623 else if (WIFSIGNALED(st)) 624 rc = ERR_TERMIMATED; 625 else 626 rc = ERR_WAITPID; 627 } 628 629 exit(rc); 630 usage: 631 fprintf(stderr, "usage: %s" 632 " [-aDglnoprtvx] [-e program] [--address=sourceaddr]\n" 633 "\t[--contimeout=seconds] [--compare-dest=dir] [--del] [--exclude]\n" 634 "\t[--exclude-from=file] [--include] [--include-from=file]\n" 635 "\t[--no-motd] [--numeric-ids] [--port=portnumber]\n" 636 "\t[--rsync-path=program] [--timeout=seconds] [--version]\n" 637 "\tsource ... directory\n", 638 getprogname()); 639 exit(ERR_SYNTAX); 640 } 641