Annotation of src/usr.bin/rsync/main.c, Revision 1.34
1.34 ! deraadt 1: /* $Id: main.c,v 1.33 2019/03/18 08:11:11 jmc Exp $ */
1.1 benno 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:
30: #include "extern.h"
31:
32: /*
33: * A remote host is has a colon before the first path separator.
34: * This works for rsh remote hosts (host:/foo/bar), implicit rsync
35: * remote hosts (host::/foo/bar), and explicit (rsync://host/foo).
36: * Return zero if local, non-zero if remote.
37: */
38: static int
39: fargs_is_remote(const char *v)
40: {
41: size_t pos;
42:
43: pos = strcspn(v, ":/");
1.6 deraadt 44: return v[pos] == ':';
1.1 benno 45: }
46:
47: /*
48: * Test whether a remote host is specifically an rsync daemon.
49: * Return zero if not, non-zero if so.
50: */
51: static int
52: fargs_is_daemon(const char *v)
53: {
54: size_t pos;
55:
1.6 deraadt 56: if (strncasecmp(v, "rsync://", 8) == 0)
1.1 benno 57: return 1;
58:
59: pos = strcspn(v, ":/");
1.6 deraadt 60: return v[pos] == ':' && v[pos + 1] == ':';
1.1 benno 61: }
62:
63: /*
64: * Take the command-line filenames (e.g., rsync foo/ bar/ baz/) and
65: * determine our operating mode.
66: * For example, if the first argument is a remote file, this means that
67: * we're going to transfer from the remote to the local.
68: * We also make sure that the arguments are consistent, that is, if
69: * we're going to transfer from the local to the remote, that no
70: * filenames for the local transfer indicate remote hosts.
71: * Always returns the parsed and sanitised options.
72: */
73: static struct fargs *
1.26 deraadt 74: fargs_parse(size_t argc, char *argv[], struct opts *opts)
1.1 benno 75: {
76: struct fargs *f = NULL;
1.26 deraadt 77: char *cp, *ccp;
1.1 benno 78: size_t i, j, len = 0;
79:
80: /* Allocations. */
81:
1.6 deraadt 82: if ((f = calloc(1, sizeof(struct fargs))) == NULL)
1.29 benno 83: err(1, "calloc");
1.1 benno 84:
85: f->sourcesz = argc - 1;
1.6 deraadt 86: if ((f->sources = calloc(f->sourcesz, sizeof(char *))) == NULL)
1.29 benno 87: err(1, "calloc");
1.1 benno 88:
89: for (i = 0; i < argc - 1; i++)
1.6 deraadt 90: if ((f->sources[i] = strdup(argv[i])) == NULL)
1.29 benno 91: err(1, "strdup");
1.1 benno 92:
1.6 deraadt 93: if ((f->sink = strdup(argv[i])) == NULL)
1.29 benno 94: err(1, "strdup");
1.1 benno 95:
96: /*
97: * Test files for its locality.
98: * If the last is a remote host, then we're sending from the
99: * local to the remote host ("sender" mode).
100: * If the first, remote to local ("receiver" mode).
101: * If neither, a local transfer in sender style.
102: */
103:
104: f->mode = FARGS_SENDER;
105:
106: if (fargs_is_remote(f->sink)) {
107: f->mode = FARGS_SENDER;
1.6 deraadt 108: if ((f->host = strdup(f->sink)) == NULL)
1.29 benno 109: err(1, "strdup");
1.1 benno 110: }
111:
112: if (fargs_is_remote(f->sources[0])) {
1.6 deraadt 113: if (f->host != NULL)
1.29 benno 114: errx(1, "both source and "
1.1 benno 115: "destination cannot be remote files");
116: f->mode = FARGS_RECEIVER;
1.6 deraadt 117: if ((f->host = strdup(f->sources[0])) == NULL)
1.29 benno 118: err(1, "strdup");
1.1 benno 119: }
120:
1.6 deraadt 121: if (f->host != NULL) {
122: if (strncasecmp(f->host, "rsync://", 8) == 0) {
1.30 benno 123: /* rsync://host[:port]/module[/path] */
1.1 benno 124: f->remote = 1;
125: len = strlen(f->host) - 8 + 1;
126: memmove(f->host, f->host + 8, len);
1.6 deraadt 127: if ((cp = strchr(f->host, '/')) == NULL)
1.30 benno 128: errx(1, "rsync protocol requires a module "
129: "name");
1.1 benno 130: *cp++ = '\0';
131: f->module = cp;
1.6 deraadt 132: if ((cp = strchr(f->module, '/')) != NULL)
1.1 benno 133: *cp = '\0';
1.29 benno 134: if ((cp = strchr(f->host, ':'))) {
1.26 deraadt 135: /* host:port --> extract port */
136: *cp++ = '\0';
137: opts->port = cp;
138: }
1.1 benno 139: } else {
140: /* host:[/path] */
141: cp = strchr(f->host, ':');
1.6 deraadt 142: assert(cp != NULL);
1.1 benno 143: *cp++ = '\0';
1.6 deraadt 144: if (*cp == ':') {
1.1 benno 145: /* host::module[/path] */
146: f->remote = 1;
147: f->module = ++cp;
148: cp = strchr(f->module, '/');
1.6 deraadt 149: if (cp != NULL)
1.1 benno 150: *cp = '\0';
151: }
152: }
1.6 deraadt 153: if ((len = strlen(f->host)) == 0)
1.29 benno 154: errx(1, "empty remote host");
1.6 deraadt 155: if (f->remote && strlen(f->module) == 0)
1.29 benno 156: errx(1, "empty remote module");
1.1 benno 157: }
158:
159: /* Make sure we have the same "hostspec" for all files. */
160:
1.4 deraadt 161: if (!f->remote) {
1.6 deraadt 162: if (f->mode == FARGS_SENDER)
1.1 benno 163: for (i = 0; i < f->sourcesz; i++) {
1.4 deraadt 164: if (!fargs_is_remote(f->sources[i]))
1.1 benno 165: continue;
1.29 benno 166: errx(1, "remote file in "
1.1 benno 167: "list of local sources: %s",
168: f->sources[i]);
169: }
1.6 deraadt 170: if (f->mode == FARGS_RECEIVER)
1.1 benno 171: for (i = 0; i < f->sourcesz; i++) {
172: if (fargs_is_remote(f->sources[i]) &&
1.4 deraadt 173: !fargs_is_daemon(f->sources[i]))
1.1 benno 174: continue;
175: if (fargs_is_daemon(f->sources[i]))
1.29 benno 176: errx(1, "remote "
1.1 benno 177: "daemon in list of "
178: "remote sources: %s",
179: f->sources[i]);
1.29 benno 180: errx(1, "local file in "
1.1 benno 181: "list of remote sources: %s",
182: f->sources[i]);
183: }
184: } else {
1.6 deraadt 185: if (f->mode != FARGS_RECEIVER)
1.29 benno 186: errx(1, "sender mode for remote "
1.1 benno 187: "daemon receivers not yet supported");
188: for (i = 0; i < f->sourcesz; i++) {
189: if (fargs_is_daemon(f->sources[i]))
190: continue;
1.29 benno 191: errx(1, "non-remote daemon file "
1.1 benno 192: "in list of remote daemon sources: "
193: "%s", f->sources[i]);
194: }
195: }
196:
197: /*
198: * If we're not remote and a sender, strip our hostname.
199: * Then exit if we're a sender or a local connection.
200: */
201:
1.4 deraadt 202: if (!f->remote) {
1.6 deraadt 203: if (f->host == NULL)
1.1 benno 204: return f;
1.6 deraadt 205: if (f->mode == FARGS_SENDER) {
206: assert(f->host != NULL);
1.1 benno 207: assert(len > 0);
208: j = strlen(f->sink);
209: memmove(f->sink, f->sink + len + 1, j - len);
210: return f;
1.6 deraadt 211: } else if (f->mode != FARGS_RECEIVER)
1.1 benno 212: return f;
213: }
214:
215: /*
216: * Now strip the hostnames from the remote host.
217: * rsync://host/module/path -> module/path
218: * host::module/path -> module/path
219: * host:path -> path
220: * Also make sure that the remote hosts are the same.
221: */
222:
1.6 deraadt 223: assert(f->host != NULL);
1.1 benno 224: assert(len > 0);
225:
226: for (i = 0; i < f->sourcesz; i++) {
227: cp = f->sources[i];
228: j = strlen(cp);
229: if (f->remote &&
1.6 deraadt 230: strncasecmp(cp, "rsync://", 8) == 0) {
1.1 benno 231: /* rsync://path */
232: cp += 8;
1.29 benno 233: if ((ccp = strchr(cp, ':'))) /* skip :port */
1.26 deraadt 234: *ccp = '\0';
1.1 benno 235: if (strncmp(cp, f->host, len) ||
1.11 benno 236: (cp[len] != '/' && cp[len] != '\0'))
1.29 benno 237: errx(1, "different remote "
1.1 benno 238: "host: %s", f->sources[i]);
239: memmove(f->sources[i],
240: f->sources[i] + len + 8 + 1,
241: j - len - 8);
1.6 deraadt 242: } else if (f->remote && strncmp(cp, "::", 2) == 0) {
1.1 benno 243: /* ::path */
244: memmove(f->sources[i],
245: f->sources[i] + 2, j - 1);
246: } else if (f->remote) {
247: /* host::path */
248: if (strncmp(cp, f->host, len) ||
1.6 deraadt 249: (cp[len] != ':' && cp[len] != '\0'))
1.29 benno 250: errx(1, "different remote "
1.1 benno 251: "host: %s", f->sources[i]);
1.11 benno 252: memmove(f->sources[i], f->sources[i] + len + 2,
253: j - len - 1);
1.6 deraadt 254: } else if (cp[0] == ':') {
1.1 benno 255: /* :path */
256: memmove(f->sources[i], f->sources[i] + 1, j);
257: } else {
258: /* host:path */
259: if (strncmp(cp, f->host, len) ||
1.6 deraadt 260: (cp[len] != ':' && cp[len] != '\0'))
1.29 benno 261: errx(1, "different remote "
1.1 benno 262: "host: %s", f->sources[i]);
263: memmove(f->sources[i],
264: f->sources[i] + len + 1, j - len);
265: }
266: }
267:
268: return f;
269: }
270:
271: int
272: main(int argc, char *argv[])
273: {
274: struct opts opts;
1.2 benno 275: pid_t child;
1.34 ! deraadt 276: int fds[2], rc, c, st, i;
1.32 deraadt 277: struct sess sess;
1.1 benno 278: struct fargs *fargs;
1.32 deraadt 279: char **args;
1.1 benno 280: struct option lopts[] = {
1.26 deraadt 281: { "port", required_argument, NULL, 3 },
1.21 deraadt 282: { "rsh", required_argument, NULL, 'e' },
1.8 deraadt 283: { "rsync-path", required_argument, NULL, 1 },
284: { "sender", no_argument, &opts.sender, 1 },
285: { "server", no_argument, &opts.server, 1 },
1.20 deraadt 286: { "dry-run", no_argument, &opts.dry_run, 1 },
287: { "version", no_argument, NULL, 2 },
1.23 deraadt 288: { "archive", no_argument, NULL, 'a' },
1.21 deraadt 289: { "help", no_argument, NULL, 'h' },
1.20 deraadt 290: { "delete", no_argument, &opts.del, 1 },
291: { "no-delete", no_argument, &opts.del, 0 },
292: { "devices", no_argument, &opts.devices, 1 },
293: { "no-devices", no_argument, &opts.devices, 0 },
294: { "group", no_argument, &opts.preserve_gids, 1 },
295: { "no-group", no_argument, &opts.preserve_gids, 0 },
1.8 deraadt 296: { "links", no_argument, &opts.preserve_links, 1 },
1.20 deraadt 297: { "no-links", no_argument, &opts.preserve_links, 0 },
298: { "owner", no_argument, &opts.preserve_uids, 1 },
299: { "no-owner", no_argument, &opts.preserve_uids, 0 },
1.8 deraadt 300: { "perms", no_argument, &opts.preserve_perms, 1 },
1.20 deraadt 301: { "no-perms", no_argument, &opts.preserve_perms, 0 },
1.31 benno 302: { "numeric-ids", no_argument, &opts.numeric_ids, 1 },
1.8 deraadt 303: { "recursive", no_argument, &opts.recursive, 1 },
1.20 deraadt 304: { "no-recursive", no_argument, &opts.recursive, 0 },
1.19 florian 305: { "specials", no_argument, &opts.specials, 1 },
1.21 deraadt 306: { "no-specials", no_argument, &opts.specials, 0 },
1.20 deraadt 307: { "times", no_argument, &opts.preserve_times, 1 },
308: { "no-times", no_argument, &opts.preserve_times, 0 },
309: { "verbose", no_argument, &opts.verbose, 1 },
310: { "no-verbose", no_argument, &opts.verbose, 0 },
1.8 deraadt 311: { NULL, 0, NULL, 0 }};
1.1 benno 312:
313: /* Global pledge. */
314:
1.19 florian 315: if (pledge("stdio unix rpath wpath cpath dpath inet fattr chown dns getpw proc exec unveil",
1.6 deraadt 316: NULL) == -1)
1.29 benno 317: err(1, "pledge");
1.1 benno 318:
319: memset(&opts, 0, sizeof(struct opts));
320:
1.30 benno 321: while ((c = getopt_long(argc, argv, "Dae:ghlnoprtv", lopts, NULL))
322: != -1) {
1.1 benno 323: switch (c) {
1.19 florian 324: case 'D':
325: opts.devices = 1;
326: opts.specials = 1;
327: break;
328: case 'a':
329: opts.recursive = 1;
330: opts.preserve_links = 1;
331: opts.preserve_perms = 1;
332: opts.preserve_times = 1;
333: opts.preserve_gids = 1;
334: opts.preserve_uids = 1;
335: opts.devices = 1;
336: opts.specials = 1;
337: break;
1.1 benno 338: case 'e':
1.9 deraadt 339: opts.ssh_prog = optarg;
1.1 benno 340: break;
1.10 benno 341: case 'g':
342: opts.preserve_gids = 1;
343: break;
1.1 benno 344: case 'l':
345: opts.preserve_links = 1;
346: break;
347: case 'n':
348: opts.dry_run = 1;
349: break;
1.13 florian 350: case 'o':
351: opts.preserve_uids = 1;
352: break;
1.1 benno 353: case 'p':
354: opts.preserve_perms = 1;
355: break;
356: case 'r':
357: opts.recursive = 1;
358: break;
359: case 't':
360: opts.preserve_times = 1;
361: break;
362: case 'v':
363: opts.verbose++;
364: break;
365: case 0:
366: /* Non-NULL flag values (e.g., --sender). */
367: break;
368: case 1:
369: opts.rsync_path = optarg;
370: break;
1.17 deraadt 371: case 2:
372: fprintf(stderr, "openrsync: protocol version %u\n",
373: RSYNC_PROTOCOL);
374: exit(0);
1.26 deraadt 375: case 3:
376: opts.port = optarg;
377: break;
1.21 deraadt 378: case 'h':
1.1 benno 379: default:
380: goto usage;
381: }
382: }
383:
384: argc -= optind;
385: argv += optind;
386:
387: /* FIXME: reference implementation rsync accepts this. */
388:
389: if (argc < 2)
390: goto usage;
391:
1.26 deraadt 392: if (opts.port == NULL)
1.29 benno 393: opts.port = "rsync";
1.26 deraadt 394:
1.1 benno 395: /*
396: * This is what happens when we're started with the "hidden"
397: * --server option, which is invoked for the rsync on the remote
398: * host by the parent.
399: */
400:
1.32 deraadt 401: if (opts.server)
402: exit(rsync_server(&opts, (size_t)argc, argv));
1.1 benno 403:
404: /*
405: * Now we know that we're the client on the local machine
406: * invoking rsync(1).
407: * At this point, we need to start the client and server
408: * initiation logic.
409: * The client is what we continue running on this host; the
410: * server is what we'll use to connect to the remote and
411: * invoke rsync with the --server option.
412: */
413:
1.26 deraadt 414: fargs = fargs_parse(argc, argv, &opts);
1.6 deraadt 415: assert(fargs != NULL);
1.1 benno 416:
417: /*
418: * If we're contacting an rsync:// daemon, then we don't need to
419: * fork, because we won't start a server ourselves.
420: * Route directly into the socket code, in that case.
421: */
422:
423: if (fargs->remote) {
1.6 deraadt 424: assert(fargs->mode == FARGS_RECEIVER);
1.32 deraadt 425: exit(rsync_socket(&opts, fargs));
1.1 benno 426: }
427:
428: /* Drop the dns/inet possibility. */
429:
1.19 florian 430: if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw proc exec unveil",
1.6 deraadt 431: NULL) == -1)
1.29 benno 432: err(1, "pledge");
1.1 benno 433:
434: /* Create a bidirectional socket and start our child. */
435:
1.6 deraadt 436: if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds) == -1)
1.29 benno 437: err(1, "socketpair");
1.1 benno 438:
1.32 deraadt 439: switch ((child = fork())) {
440: case -1:
441: err(1, "fork");
442: case 0:
1.1 benno 443: close(fds[0]);
1.32 deraadt 444: if (pledge("stdio exec", NULL) == -1)
445: err(1, "pledge");
446:
447: memset(&sess, 0, sizeof(struct sess));
448: sess.opts = &opts;
1.1 benno 449:
1.32 deraadt 450: if ((args = fargs_cmdline(&sess, fargs)) == NULL) {
451: ERRX1(&sess, "fargs_cmdline");
452: _exit(1);
453: }
1.1 benno 454:
1.32 deraadt 455: for (i = 0; args[i] != NULL; i++)
456: LOG2(&sess, "exec[%d] = %s", i, args[i]);
1.1 benno 457:
1.32 deraadt 458: /* Make sure the child's stdin is from the sender. */
459: if (dup2(fds[1], STDIN_FILENO) == -1) {
460: ERR(&sess, "dup2");
461: _exit(1);
462: } if (dup2(fds[1], STDOUT_FILENO) == -1) {
463: ERR(&sess, "dup2");
464: _exit(1);
465: }
466: execvp(args[0], args);
467: _exit(1);
1.1 benno 468: /* NOTREACHED */
1.32 deraadt 469: default:
470: close(fds[1]);
471: rc = rsync_client(&opts, fds[0], fargs);
472: break;
1.1 benno 473: }
474:
475: /*
476: * If the client has an error and exits, the server may be
477: * sitting around waiting to get data while we waitpid().
478: * So close the connection here so that they don't hang.
479: */
480:
1.34 ! deraadt 481: if (rc)
1.1 benno 482: close(fds[0]);
483:
1.6 deraadt 484: if (waitpid(child, &st, 0) == -1)
1.29 benno 485: err(1, "waitpid");
1.34 ! deraadt 486:
! 487: /*
! 488: * If we don't already have an error (rc == 0), then inherit the
! 489: * error code of rsync_server() if it has exited.
! 490: * If it hasn't exited, it overrides our return value.
! 491: */
! 492:
! 493: if (WIFEXITED(st) && rc == 0)
! 494: rc = WEXITSTATUS(st);
! 495: else if (!WIFEXITED(st))
! 496: rc = 1;
! 497:
1.32 deraadt 498: exit(rc);
1.1 benno 499: usage:
1.33 jmc 500: fprintf(stderr, "usage: %s [-aDglnoprtv] [-e program] [--archive] [--delete] [--devices]\n"
501: "\t[--group] [--links] [--dry-run] [--owner] [--perms]\n"
502: "\t[--port=portnumber] [--recursive] [--rsh=program]\n"
503: "\t[--rsync-path=program] [--specials] [--times] [--verbose]\n"
504: "\t[--version] source ... directory\n",
1.32 deraadt 505: getprogname());
506: exit(1);
1.1 benno 507: }