1*f6eb9700Sderaadt /* $OpenBSD: server.c,v 1.15 2021/11/03 14:42:12 deraadt 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 <fcntl.h>
2160a32ee9Sbenno #include <inttypes.h>
2260a32ee9Sbenno #include <stdlib.h>
2360a32ee9Sbenno #include <string.h>
2460a32ee9Sbenno #include <unistd.h>
25ef859540Sderaadt #include <err.h>
2660a32ee9Sbenno
2760a32ee9Sbenno #include "extern.h"
2860a32ee9Sbenno
2960a32ee9Sbenno static int
fcntl_nonblock(int fd)30ba617adaSbenno fcntl_nonblock(int fd)
3160a32ee9Sbenno {
3260a32ee9Sbenno int fl;
3360a32ee9Sbenno
34f1dcb30aSderaadt if ((fl = fcntl(fd, F_GETFL, 0)) == -1)
35b2a7eac7Sbenno ERR("fcntl: F_GETFL");
36f1dcb30aSderaadt else if (fcntl(fd, F_SETFL, fl|O_NONBLOCK) == -1)
37b2a7eac7Sbenno ERR("fcntl: F_SETFL");
3860a32ee9Sbenno else
3960a32ee9Sbenno return 1;
4060a32ee9Sbenno
4160a32ee9Sbenno return 0;
4260a32ee9Sbenno }
4360a32ee9Sbenno
4460a32ee9Sbenno /*
4560a32ee9Sbenno * The server (remote) side of the system.
4660a32ee9Sbenno * This parses the arguments given it by the remote shell then moves
4760a32ee9Sbenno * into receiver or sender mode depending upon those arguments.
4883649630Sderaadt * Returns exit code 0 on success, 1 on failure, 2 on failure with
4983649630Sderaadt * incompatible protocols.
5060a32ee9Sbenno */
5160a32ee9Sbenno int
rsync_server(const struct opts * opts,size_t argc,char * argv[])5260a32ee9Sbenno rsync_server(const struct opts *opts, size_t argc, char *argv[])
5360a32ee9Sbenno {
5460a32ee9Sbenno struct sess sess;
5560a32ee9Sbenno int fdin = STDIN_FILENO,
5683649630Sderaadt fdout = STDOUT_FILENO, rc = 1;
5760a32ee9Sbenno
58ef859540Sderaadt if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
59ef859540Sderaadt NULL) == -1)
60dd135e81Sclaudio err(ERR_IPC, "pledge");
61ef859540Sderaadt
6260a32ee9Sbenno memset(&sess, 0, sizeof(struct sess));
6360a32ee9Sbenno sess.opts = opts;
6460a32ee9Sbenno
6560a32ee9Sbenno /* Begin by making descriptors non-blocking. */
6660a32ee9Sbenno
67ba617adaSbenno if (!fcntl_nonblock(fdin) ||
68ba617adaSbenno !fcntl_nonblock(fdout)) {
69b2a7eac7Sbenno ERRX1("fcntl_nonblock");
7060a32ee9Sbenno goto out;
7160a32ee9Sbenno }
7260a32ee9Sbenno
7360a32ee9Sbenno /* Standard rsync preamble, server side. */
7460a32ee9Sbenno
7560a32ee9Sbenno sess.lver = RSYNC_PROTOCOL;
7660a32ee9Sbenno sess.seed = arc4random();
7760a32ee9Sbenno
7860a32ee9Sbenno if (!io_read_int(&sess, fdin, &sess.rver)) {
79b2a7eac7Sbenno ERRX1("io_read_int");
8060a32ee9Sbenno goto out;
8160a32ee9Sbenno } else if (!io_write_int(&sess, fdout, sess.lver)) {
82b2a7eac7Sbenno ERRX1("io_write_int");
8360a32ee9Sbenno goto out;
8460a32ee9Sbenno } else if (!io_write_int(&sess, fdout, sess.seed)) {
85b2a7eac7Sbenno ERRX1("io_write_int");
8660a32ee9Sbenno goto out;
8760a32ee9Sbenno }
8860a32ee9Sbenno
8960a32ee9Sbenno sess.mplex_writes = 1;
9060a32ee9Sbenno
9160a32ee9Sbenno if (sess.rver < sess.lver) {
92b2a7eac7Sbenno ERRX("remote protocol %d is older than our own %d: unsupported",
9360a32ee9Sbenno sess.rver, sess.lver);
946304135bSbenno rc = 2;
9560a32ee9Sbenno goto out;
9660a32ee9Sbenno }
9760a32ee9Sbenno
98b2a7eac7Sbenno LOG2("server detected client version %d, server version %d, seed %d",
9960a32ee9Sbenno sess.rver, sess.lver, sess.seed);
10060a32ee9Sbenno
10160a32ee9Sbenno if (sess.opts->sender) {
102b2a7eac7Sbenno LOG2("server starting sender");
10360a32ee9Sbenno
10460a32ee9Sbenno /*
10560a32ee9Sbenno * At this time, I always get a period as the first
10660a32ee9Sbenno * argument of the command line.
10760a32ee9Sbenno * Let's make it a requirement until I figure out when
10860a32ee9Sbenno * that differs.
10960a32ee9Sbenno * rsync [flags] "." <source> <...>
11060a32ee9Sbenno */
11160a32ee9Sbenno
11260a32ee9Sbenno if (strcmp(argv[0], ".")) {
113b2a7eac7Sbenno ERRX("first argument must be a standalone period");
11460a32ee9Sbenno goto out;
11560a32ee9Sbenno }
11660a32ee9Sbenno argv++;
11760a32ee9Sbenno argc--;
118f1dcb30aSderaadt if (argc == 0) {
119b2a7eac7Sbenno ERRX("must have arguments");
12060a32ee9Sbenno goto out;
12160a32ee9Sbenno }
12260a32ee9Sbenno
12360a32ee9Sbenno if (!rsync_sender(&sess, fdin, fdout, argc, argv)) {
124b2a7eac7Sbenno ERRX1("rsync_sender");
12560a32ee9Sbenno goto out;
12660a32ee9Sbenno }
12760a32ee9Sbenno } else {
128b2a7eac7Sbenno LOG2("server starting receiver");
12960a32ee9Sbenno
13060a32ee9Sbenno /*
13160a32ee9Sbenno * I don't understand why this calling convention
13260a32ee9Sbenno * exists, but we must adhere to it.
13360a32ee9Sbenno * rsync [flags] "." <destination>
13460a32ee9Sbenno */
13560a32ee9Sbenno
136f1dcb30aSderaadt if (argc != 2) {
137b2a7eac7Sbenno ERRX("server receiver mode requires two argument");
13860a32ee9Sbenno goto out;
13960a32ee9Sbenno } else if (strcmp(argv[0], ".")) {
140b2a7eac7Sbenno ERRX("first argument must be a standalone period");
14160a32ee9Sbenno goto out;
14260a32ee9Sbenno }
14360a32ee9Sbenno
14460a32ee9Sbenno if (!rsync_receiver(&sess, fdin, fdout, argv[1])) {
145b2a7eac7Sbenno ERRX1("rsync_receiver");
14660a32ee9Sbenno goto out;
14760a32ee9Sbenno }
14860a32ee9Sbenno }
14960a32ee9Sbenno
15060a32ee9Sbenno #if 0
15160a32ee9Sbenno /* Probably the EOF. */
15260a32ee9Sbenno if (io_read_check(&sess, fdin))
153b2a7eac7Sbenno WARNX("data remains in read pipe");
15460a32ee9Sbenno #endif
15560a32ee9Sbenno
15683649630Sderaadt rc = 0;
15760a32ee9Sbenno out:
1586304135bSbenno return rc;
15960a32ee9Sbenno }
160