1 /* $OpenBSD: client.c,v 1.17 2021/06/30 13:10:04 claudio 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
19 #include <assert.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <err.h>
25
26 #include "extern.h"
27
28 /*
29 * The rsync client runs on the operator's local machine.
30 * It can either be in sender or receiver mode.
31 * In the former, it synchronises local files from a remote sink.
32 * In the latter, the remote sink synchronses to the local files.
33 * Returns exit code 0 on success, 1 on failure, 2 on failure with
34 * incompatible protocols.
35 */
36 int
rsync_client(const struct opts * opts,int fd,const struct fargs * f)37 rsync_client(const struct opts *opts, int fd, const struct fargs *f)
38 {
39 struct sess sess;
40 int rc = 1;
41
42 /* Standard rsync preamble, sender side. */
43
44 if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
45 NULL) == -1)
46 err(ERR_IPC, "pledge");
47
48 memset(&sess, 0, sizeof(struct sess));
49 sess.opts = opts;
50 sess.lver = RSYNC_PROTOCOL;
51
52 if (!io_write_int(&sess, fd, sess.lver)) {
53 ERRX1("io_write_int");
54 goto out;
55 } else if (!io_read_int(&sess, fd, &sess.rver)) {
56 ERRX1("io_read_int");
57 goto out;
58 } else if (!io_read_int(&sess, fd, &sess.seed)) {
59 ERRX1("io_read_int");
60 goto out;
61 }
62
63 if (sess.rver < sess.lver) {
64 ERRX("remote protocol %d is older than our own %d: unsupported",
65 sess.rver, sess.lver);
66 rc = 2;
67 goto out;
68 }
69
70 LOG2("client detected client version %d, server version %d, seed %d",
71 sess.lver, sess.rver, sess.seed);
72
73 sess.mplex_reads = 1;
74
75 /*
76 * Now we need to get our list of files.
77 * Senders (and locals) send; receivers receive.
78 */
79
80 if (f->mode != FARGS_RECEIVER) {
81 LOG2("client starting sender: %s",
82 f->host == NULL ? "(local)" : f->host);
83 if (!rsync_sender(&sess, fd, fd, f->sourcesz,
84 f->sources)) {
85 ERRX1("rsync_sender");
86 goto out;
87 }
88 } else {
89 LOG2("client starting receiver: %s",
90 f->host == NULL ? "(local)" : f->host);
91 if (!rsync_receiver(&sess, fd, fd, f->sink)) {
92 ERRX1("rsync_receiver");
93 goto out;
94 }
95 }
96
97 #if 0
98 /* Probably the EOF. */
99 if (io_read_check(&sess, fd))
100 WARNX("data remains in read pipe");
101 #endif
102
103 rc = 0;
104 out:
105 return rc;
106 }
107