xref: /openbsd-src/usr.bin/rsync/client.c (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1 /*	$Id: client.c,v 1.4 2019/02/11 21:41:22 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 
25 #include "extern.h"
26 
27 /*
28  * The rsync client runs on the operator's local machine.
29  * It can either be in sender or receiver mode.
30  * In the former, it synchronises local files from a remote sink.
31  * In the latter, the remote sink synchronses to the local files.
32  *
33  * Pledges: stdio, rpath, wpath, cpath, unveil, fattr.
34  *
35  * Pledges (dry-run): -cpath, -wpath, -fattr.
36  * Pledges (!preserve_times): -fattr.
37  */
38 int
39 rsync_client(const struct opts *opts, int fd, const struct fargs *f)
40 {
41 	struct sess	 sess;
42 	int		 rc = 0;
43 
44 	/* Standard rsync preamble, sender side. */
45 
46 	memset(&sess, 0, sizeof(struct sess));
47 	sess.opts = opts;
48 	sess.lver = RSYNC_PROTOCOL;
49 
50 	if (!io_write_int(&sess, fd, sess.lver)) {
51 		ERRX1(&sess, "io_write_int");
52 		goto out;
53 	} else if (!io_read_int(&sess, fd, &sess.rver)) {
54 		ERRX1(&sess, "io_read_int");
55 		goto out;
56 	} else if (!io_read_int(&sess, fd, &sess.seed)) {
57 		ERRX1(&sess, "io_read_int");
58 		goto out;
59 	}
60 
61 	if (sess.rver < sess.lver) {
62 		ERRX(&sess, "remote protocol is older "
63 			"than our own (%" PRId32 " < %" PRId32 "): "
64 			"this is not supported",
65 			sess.rver, sess.lver);
66 		goto out;
67 	}
68 
69 	LOG2(&sess, "client detected client version %" PRId32
70 		", server version %" PRId32 ", seed %" PRId32,
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 (FARGS_RECEIVER != f->mode) {
81 		LOG2(&sess, "client starting sender: %s",
82 		    f->host == NULL ? "(local)" : f->host);
83 		if (!rsync_sender(&sess, fd, fd,
84 				f->sourcesz, f->sources)) {
85 			ERRX1(&sess, "rsync_sender");
86 			goto out;
87 		}
88 	} else {
89 		LOG2(&sess, "client starting receiver: %s",
90 		    f->host == NULL ? "(local)" : f->host);
91 		if (!rsync_receiver(&sess, fd, fd, f->sink)) {
92 			ERRX1(&sess, "rsync_receiver");
93 			goto out;
94 		}
95 	}
96 
97 #if 0
98 	/* Probably the EOF. */
99 	if (io_read_check(&sess, fd))
100 		WARNX(&sess, "data remains in read pipe");
101 #endif
102 
103 	rc = 1;
104 out:
105 	return rc;
106 }
107