1 /* $Id: server.c,v 1.12 2019/05/08 21:30:11 benno 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 <fcntl.h> 21 #include <inttypes.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <unistd.h> 25 #include <err.h> 26 27 #include "extern.h" 28 29 static int 30 fcntl_nonblock(int fd) 31 { 32 int fl; 33 34 if ((fl = fcntl(fd, F_GETFL, 0)) == -1) 35 ERR("fcntl: F_GETFL"); 36 else if (fcntl(fd, F_SETFL, fl|O_NONBLOCK) == -1) 37 ERR("fcntl: F_SETFL"); 38 else 39 return 1; 40 41 return 0; 42 } 43 44 /* 45 * The server (remote) side of the system. 46 * This parses the arguments given it by the remote shell then moves 47 * into receiver or sender mode depending upon those arguments. 48 * Returns exit code 0 on success, 1 on failure, 2 on failure with 49 * incompatible protocols. 50 */ 51 int 52 rsync_server(const struct opts *opts, size_t argc, char *argv[]) 53 { 54 struct sess sess; 55 int fdin = STDIN_FILENO, 56 fdout = STDOUT_FILENO, rc = 1; 57 58 if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil", 59 NULL) == -1) 60 err(1, "pledge"); 61 62 memset(&sess, 0, sizeof(struct sess)); 63 sess.opts = opts; 64 65 /* Begin by making descriptors non-blocking. */ 66 67 if (!fcntl_nonblock(fdin) || 68 !fcntl_nonblock(fdout)) { 69 ERRX1("fcntl_nonblock"); 70 goto out; 71 } 72 73 /* Standard rsync preamble, server side. */ 74 75 sess.lver = RSYNC_PROTOCOL; 76 sess.seed = arc4random(); 77 78 if (!io_read_int(&sess, fdin, &sess.rver)) { 79 ERRX1("io_read_int"); 80 goto out; 81 } else if (!io_write_int(&sess, fdout, sess.lver)) { 82 ERRX1("io_write_int"); 83 goto out; 84 } else if (!io_write_int(&sess, fdout, sess.seed)) { 85 ERRX1("io_write_int"); 86 goto out; 87 } 88 89 sess.mplex_writes = 1; 90 91 if (sess.rver < sess.lver) { 92 ERRX("remote protocol %d is older than our own %d: unsupported", 93 sess.rver, sess.lver); 94 rc = 2; 95 goto out; 96 } 97 98 LOG2("server detected client version %d, server version %d, seed %d", 99 sess.rver, sess.lver, sess.seed); 100 101 if (sess.opts->sender) { 102 LOG2("server starting sender"); 103 104 /* 105 * At this time, I always get a period as the first 106 * argument of the command line. 107 * Let's make it a requirement until I figure out when 108 * that differs. 109 * rsync [flags] "." <source> <...> 110 */ 111 112 if (strcmp(argv[0], ".")) { 113 ERRX("first argument must be a standalone period"); 114 goto out; 115 } 116 argv++; 117 argc--; 118 if (argc == 0) { 119 ERRX("must have arguments"); 120 goto out; 121 } 122 123 if (!rsync_sender(&sess, fdin, fdout, argc, argv)) { 124 ERRX1("rsync_sender"); 125 goto out; 126 } 127 } else { 128 LOG2("server starting receiver"); 129 130 /* 131 * I don't understand why this calling convention 132 * exists, but we must adhere to it. 133 * rsync [flags] "." <destination> 134 */ 135 136 if (argc != 2) { 137 ERRX("server receiver mode requires two argument"); 138 goto out; 139 } else if (strcmp(argv[0], ".")) { 140 ERRX("first argument must be a standalone period"); 141 goto out; 142 } 143 144 if (!rsync_receiver(&sess, fdin, fdout, argv[1])) { 145 ERRX1("rsync_receiver"); 146 goto out; 147 } 148 } 149 150 #if 0 151 /* Probably the EOF. */ 152 if (io_read_check(&sess, fdin)) 153 WARNX("data remains in read pipe"); 154 #endif 155 156 rc = 0; 157 out: 158 return rc; 159 } 160