xref: /openbsd-src/usr.bin/rsync/client.c (revision b00915975b09768d864990a42fb77354c5f1ad82)
1*b0091597Sclaudio /*	$OpenBSD: client.c,v 1.17 2021/06/30 13:10:04 claudio Exp $ */
260a32ee9Sbenno /*
360a32ee9Sbenno  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
460a32ee9Sbenno  *
560a32ee9Sbenno  * Permission to use, copy, modify, and distribute this software for any
660a32ee9Sbenno  * purpose with or without fee is hereby granted, provided that the above
760a32ee9Sbenno  * copyright notice and this permission notice appear in all copies.
860a32ee9Sbenno  *
960a32ee9Sbenno  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1060a32ee9Sbenno  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1160a32ee9Sbenno  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1260a32ee9Sbenno  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1360a32ee9Sbenno  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1460a32ee9Sbenno  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1560a32ee9Sbenno  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1660a32ee9Sbenno  */
1760a32ee9Sbenno #include <sys/stat.h>
1860a32ee9Sbenno 
1960a32ee9Sbenno #include <assert.h>
2060a32ee9Sbenno #include <inttypes.h>
2160a32ee9Sbenno #include <stdlib.h>
2260a32ee9Sbenno #include <string.h>
2360a32ee9Sbenno #include <unistd.h>
24ef859540Sderaadt #include <err.h>
2560a32ee9Sbenno 
2660a32ee9Sbenno #include "extern.h"
2760a32ee9Sbenno 
2860a32ee9Sbenno /*
2960a32ee9Sbenno  * The rsync client runs on the operator's local machine.
3060a32ee9Sbenno  * It can either be in sender or receiver mode.
3160a32ee9Sbenno  * In the former, it synchronises local files from a remote sink.
3260a32ee9Sbenno  * In the latter, the remote sink synchronses to the local files.
3383649630Sderaadt  * Returns exit code 0 on success, 1 on failure, 2 on failure with
3483649630Sderaadt  * incompatible protocols.
3560a32ee9Sbenno  */
3660a32ee9Sbenno int
rsync_client(const struct opts * opts,int fd,const struct fargs * f)3760a32ee9Sbenno rsync_client(const struct opts *opts, int fd, const struct fargs *f)
3860a32ee9Sbenno {
3960a32ee9Sbenno 	struct sess	 sess;
4083649630Sderaadt 	int		 rc = 1;
4160a32ee9Sbenno 
4260a32ee9Sbenno 	/* Standard rsync preamble, sender side. */
4360a32ee9Sbenno 
44ef859540Sderaadt 	if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
45ef859540Sderaadt 	    NULL) == -1)
46dd135e81Sclaudio 		err(ERR_IPC, "pledge");
47ef859540Sderaadt 
4860a32ee9Sbenno 	memset(&sess, 0, sizeof(struct sess));
4960a32ee9Sbenno 	sess.opts = opts;
5060a32ee9Sbenno 	sess.lver = RSYNC_PROTOCOL;
5160a32ee9Sbenno 
5260a32ee9Sbenno 	if (!io_write_int(&sess, fd, sess.lver)) {
53b2a7eac7Sbenno 		ERRX1("io_write_int");
5460a32ee9Sbenno 		goto out;
5560a32ee9Sbenno 	} else if (!io_read_int(&sess, fd, &sess.rver)) {
56b2a7eac7Sbenno 		ERRX1("io_read_int");
5760a32ee9Sbenno 		goto out;
5860a32ee9Sbenno 	} else if (!io_read_int(&sess, fd, &sess.seed)) {
59b2a7eac7Sbenno 		ERRX1("io_read_int");
6060a32ee9Sbenno 		goto out;
6160a32ee9Sbenno 	}
6260a32ee9Sbenno 
6360a32ee9Sbenno 	if (sess.rver < sess.lver) {
64b2a7eac7Sbenno 		ERRX("remote protocol %d is older than our own %d: unsupported",
6560a32ee9Sbenno 		    sess.rver, sess.lver);
6683649630Sderaadt 		rc = 2;
6760a32ee9Sbenno 		goto out;
6860a32ee9Sbenno 	}
6960a32ee9Sbenno 
70b2a7eac7Sbenno 	LOG2("client detected client version %d, server version %d, seed %d",
7160a32ee9Sbenno 	    sess.lver, sess.rver, sess.seed);
7260a32ee9Sbenno 
7360a32ee9Sbenno 	sess.mplex_reads = 1;
7460a32ee9Sbenno 
7560a32ee9Sbenno 	/*
7660a32ee9Sbenno 	 * Now we need to get our list of files.
7760a32ee9Sbenno 	 * Senders (and locals) send; receivers receive.
7860a32ee9Sbenno 	 */
7960a32ee9Sbenno 
8008a7862dSderaadt 	if (f->mode != FARGS_RECEIVER) {
81b2a7eac7Sbenno 		LOG2("client starting sender: %s",
82f1dcb30aSderaadt 		    f->host == NULL ? "(local)" : f->host);
83b1d34d51Sderaadt 		if (!rsync_sender(&sess, fd, fd, f->sourcesz,
84b1d34d51Sderaadt 		    f->sources)) {
85b2a7eac7Sbenno 			ERRX1("rsync_sender");
8660a32ee9Sbenno 			goto out;
8760a32ee9Sbenno 		}
8860a32ee9Sbenno 	} else {
89b2a7eac7Sbenno 		LOG2("client starting receiver: %s",
90f1dcb30aSderaadt 		    f->host == NULL ? "(local)" : f->host);
9160a32ee9Sbenno 		if (!rsync_receiver(&sess, fd, fd, f->sink)) {
92b2a7eac7Sbenno 			ERRX1("rsync_receiver");
9360a32ee9Sbenno 			goto out;
9460a32ee9Sbenno 		}
9560a32ee9Sbenno 	}
9660a32ee9Sbenno 
9760a32ee9Sbenno #if 0
9860a32ee9Sbenno 	/* Probably the EOF. */
9960a32ee9Sbenno 	if (io_read_check(&sess, fd))
100b2a7eac7Sbenno 		WARNX("data remains in read pipe");
10160a32ee9Sbenno #endif
10260a32ee9Sbenno 
10383649630Sderaadt 	rc = 0;
10460a32ee9Sbenno out:
10560a32ee9Sbenno 	return rc;
10660a32ee9Sbenno }
107