xref: /openbsd-src/usr.bin/rsync/client.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /*	$Id: client.c,v 1.14 2019/03/23 16:04:28 deraadt 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
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(1, "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(&sess, "io_write_int");
54 		goto out;
55 	} else if (!io_read_int(&sess, fd, &sess.rver)) {
56 		ERRX1(&sess, "io_read_int");
57 		goto out;
58 	} else if (!io_read_int(&sess, fd, &sess.seed)) {
59 		ERRX1(&sess, "io_read_int");
60 		goto out;
61 	}
62 
63 	if (sess.rver < sess.lver) {
64 		ERRX(&sess,
65 		    "remote protocol %d is older than our own %d: unsupported",
66 		    sess.rver, sess.lver);
67 		rc = 2;
68 		goto out;
69 	}
70 
71 	LOG2(&sess, "client detected client version %d, server version %d, seed %d",
72 	    sess.lver, sess.rver, sess.seed);
73 
74 	sess.mplex_reads = 1;
75 
76 	/*
77 	 * Now we need to get our list of files.
78 	 * Senders (and locals) send; receivers receive.
79 	 */
80 
81 	if (f->mode != FARGS_RECEIVER) {
82 		LOG2(&sess, "client starting sender: %s",
83 		    f->host == NULL ? "(local)" : f->host);
84 		if (!rsync_sender(&sess, fd, fd, f->sourcesz,
85 		    f->sources)) {
86 			ERRX1(&sess, "rsync_sender");
87 			goto out;
88 		}
89 	} else {
90 		LOG2(&sess, "client starting receiver: %s",
91 		    f->host == NULL ? "(local)" : f->host);
92 		if (!rsync_receiver(&sess, fd, fd, f->sink)) {
93 			ERRX1(&sess, "rsync_receiver");
94 			goto out;
95 		}
96 	}
97 
98 #if 0
99 	/* Probably the EOF. */
100 	if (io_read_check(&sess, fd))
101 		WARNX(&sess, "data remains in read pipe");
102 #endif
103 
104 	rc = 0;
105 out:
106 	return rc;
107 }
108