1 /* $Id: socket.c,v 1.28 2020/08/19 11:10:42 kn 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 #include <sys/socket.h> 19 #include <arpa/inet.h> 20 #include <netinet/in.h> 21 22 #include <assert.h> 23 #include <ctype.h> 24 #include <errno.h> 25 #include <fcntl.h> 26 #include <inttypes.h> 27 #include <netdb.h> 28 #include <poll.h> 29 #include <resolv.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <err.h> 34 35 #include "extern.h" 36 37 /* 38 * Defines a resolved IP address for the host 39 * There can be many, IPV4 or IPV6. 40 */ 41 struct source { 42 int family; /* PF_INET or PF_INET6 */ 43 char ip[INET6_ADDRSTRLEN]; /* formatted string */ 44 struct sockaddr_storage sa; /* socket */ 45 socklen_t salen; /* length of socket buffer */ 46 }; 47 48 /* 49 * Try to bind to a local IP address matching the addres family passed. 50 * Return -1 on failure to bind to any address, 0 on success. 51 */ 52 static int 53 inet_bind(int s, sa_family_t af, const struct source *bsrc, size_t bsrcsz) 54 { 55 size_t i; 56 57 if (bsrc == NULL) 58 return 0; 59 for (i = 0; i < bsrcsz; i++) { 60 if (bsrc[i].family != af) 61 continue; 62 if (bind(s, (const struct sockaddr *)&bsrc[i].sa, 63 bsrc[i].salen) == -1) 64 continue; 65 return 0; 66 } 67 return -1; 68 } 69 70 /* 71 * Connect to an IP address representing a host. 72 * Return <0 on failure, 0 on try another address, >0 on success. 73 */ 74 static int 75 inet_connect(int *sd, const struct source *src, const char *host, 76 const struct source *bsrc, size_t bsrcsz) 77 { 78 int c, flags; 79 80 if (*sd != -1) 81 close(*sd); 82 83 LOG2("trying: %s, %s", src->ip, host); 84 85 if ((*sd = socket(src->family, SOCK_STREAM, 0)) == -1) { 86 ERR("socket"); 87 return -1; 88 } 89 90 if (inet_bind(*sd, src->family, bsrc, bsrcsz) == -1) { 91 ERR("bind"); 92 return -1; 93 } 94 95 /* 96 * Initiate blocking connection. 97 * We use the blocking connect() instead of passing NONBLOCK to 98 * the socket() function because we don't need to do anything 99 * while waiting for this to finish. 100 */ 101 102 c = connect(*sd, (const struct sockaddr *)&src->sa, src->salen); 103 if (c == -1) { 104 if (errno == EADDRNOTAVAIL) 105 return 0; 106 if (errno == ECONNREFUSED || errno == EHOSTUNREACH) { 107 WARNX("connect refused: %s, %s", 108 src->ip, host); 109 return 0; 110 } 111 ERR("connect"); 112 return -1; 113 } 114 115 /* Set up non-blocking mode. */ 116 117 if ((flags = fcntl(*sd, F_GETFL, 0)) == -1) { 118 ERR("fcntl"); 119 return -1; 120 } else if (fcntl(*sd, F_SETFL, flags|O_NONBLOCK) == -1) { 121 ERR("fcntl"); 122 return -1; 123 } 124 125 return 1; 126 } 127 128 /* 129 * Resolve the socket addresses for host, both in IPV4 and IPV6. 130 * Once completed, the "dns" pledge may be dropped. 131 * Returns the addresses on success, NULL on failure (sz is always zero, 132 * in this case). 133 */ 134 static struct source * 135 inet_resolve(struct sess *sess, const char *host, size_t *sz, int passive) 136 { 137 struct addrinfo hints, *res0, *res; 138 struct sockaddr *sa; 139 struct source *src = NULL; 140 const char *port = sess->opts->port; 141 size_t i, srcsz = 0; 142 int error; 143 144 *sz = 0; 145 146 memset(&hints, 0, sizeof(hints)); 147 hints.ai_family = PF_UNSPEC; 148 hints.ai_socktype = SOCK_STREAM; 149 if (passive) { 150 hints.ai_flags = SOCK_STREAM; 151 port = NULL; 152 } 153 154 error = getaddrinfo(host, port, &hints, &res0); 155 156 LOG2("resolving: %s", host); 157 158 if (error == EAI_AGAIN || error == EAI_NONAME) { 159 ERRX("could not resolve hostname %s: %s", 160 host, gai_strerror(error)); 161 return NULL; 162 } else if (error == EAI_SERVICE) { 163 ERRX("could not resolve service rsync: %s", 164 gai_strerror(error)); 165 return NULL; 166 } else if (error) { 167 ERRX("getaddrinfo: %s: %s", host, gai_strerror(error)); 168 return NULL; 169 } 170 171 /* Allocate for all available addresses. */ 172 173 for (res = res0; res != NULL; res = res->ai_next) 174 if (res->ai_family == AF_INET || 175 res->ai_family == AF_INET6) 176 srcsz++; 177 178 if (srcsz == 0) { 179 ERRX("no addresses resolved: %s", host); 180 freeaddrinfo(res0); 181 return NULL; 182 } 183 184 src = calloc(srcsz, sizeof(struct source)); 185 if (src == NULL) { 186 ERRX("calloc"); 187 freeaddrinfo(res0); 188 return NULL; 189 } 190 191 for (i = 0, res = res0; res != NULL; res = res->ai_next) { 192 if (res->ai_family != AF_INET && 193 res->ai_family != AF_INET6) 194 continue; 195 196 assert(i < srcsz); 197 198 /* Copy the socket address. */ 199 200 src[i].salen = res->ai_addrlen; 201 memcpy(&src[i].sa, res->ai_addr, src[i].salen); 202 203 /* Format as a string, too. */ 204 205 sa = res->ai_addr; 206 if (res->ai_family == AF_INET) { 207 src[i].family = PF_INET; 208 inet_ntop(AF_INET, 209 &(((struct sockaddr_in *)sa)->sin_addr), 210 src[i].ip, INET6_ADDRSTRLEN); 211 } else { 212 src[i].family = PF_INET6; 213 inet_ntop(AF_INET6, 214 &(((struct sockaddr_in6 *)sa)->sin6_addr), 215 src[i].ip, INET6_ADDRSTRLEN); 216 } 217 218 LOG2("hostname resolved: %s: %s", host, src[i].ip); 219 i++; 220 } 221 222 freeaddrinfo(res0); 223 *sz = srcsz; 224 return src; 225 } 226 227 /* 228 * Process an rsyncd preamble line. 229 * This is either free-form text or @RSYNCD commands. 230 * Return <0 on failure, 0 on try more lines, >0 on finished. 231 */ 232 static int 233 protocol_line(struct sess *sess, __attribute__((unused)) const char *host, 234 const char *cp) 235 { 236 int major, minor; 237 238 if (strncmp(cp, "@RSYNCD: ", 9)) { 239 LOG1("%s", cp); 240 return 0; 241 } 242 243 cp += 9; 244 while (isspace((unsigned char)*cp)) 245 cp++; 246 247 /* @RSYNCD: OK indicates that we're finished. */ 248 249 if (strcmp(cp, "OK") == 0) 250 return 1; 251 252 /* 253 * Otherwise, all we have left is our version. 254 * There are two formats: x.y (w/submodule) and x. 255 */ 256 257 if (sscanf(cp, "%d.%d", &major, &minor) == 2) { 258 sess->rver = major; 259 return 0; 260 } else if (sscanf(cp, "%d", &major) == 1) { 261 sess->rver = major; 262 return 0; 263 } 264 265 ERRX("rsyncd protocol error: unknown command"); 266 return -1; 267 } 268 269 /* 270 * Connect to a remote rsync://-enabled server sender. 271 * Returns exit code 0 on success, 1 on failure. 272 */ 273 int 274 rsync_connect(const struct opts *opts, int *sd, const struct fargs *f) 275 { 276 struct sess sess; 277 struct source *src = NULL, *bsrc = NULL; 278 size_t i, srcsz = 0, bsrcsz = 0; 279 int c, rc = 1; 280 281 if (pledge("stdio unix rpath wpath cpath dpath inet fattr chown dns getpw unveil", 282 NULL) == -1) 283 err(1, "pledge"); 284 285 memset(&sess, 0, sizeof(struct sess)); 286 sess.opts = opts; 287 288 assert(f->host != NULL); 289 290 /* Resolve all IP addresses from the host. */ 291 292 if ((src = inet_resolve(&sess, f->host, &srcsz, 0)) == NULL) { 293 ERRX1("inet_resolve"); 294 exit(1); 295 } 296 if (opts->address != NULL) 297 if ((bsrc = inet_resolve(&sess, opts->address, &bsrcsz, 1)) == 298 NULL) { 299 ERRX1("inet_resolve bind"); 300 exit(1); 301 } 302 303 /* Drop the DNS pledge. */ 304 305 if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw inet unveil", 306 NULL) == -1) { 307 ERR("pledge"); 308 exit(1); 309 } 310 311 /* 312 * Iterate over all addresses, trying to connect. 313 * When we succeed, then continue using the connected socket. 314 */ 315 316 assert(srcsz); 317 for (i = 0; i < srcsz; i++) { 318 c = inet_connect(sd, &src[i], f->host, bsrc, bsrcsz); 319 if (c < 0) { 320 ERRX1("inet_connect"); 321 goto out; 322 } else if (c > 0) 323 break; 324 } 325 326 /* Drop the inet pledge. */ 327 if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil", 328 NULL) == -1) { 329 ERR("pledge"); 330 goto out; 331 } 332 333 if (i == srcsz) { 334 ERRX("cannot connect to host: %s", f->host); 335 goto out; 336 } 337 338 LOG2("connected: %s, %s", src[i].ip, f->host); 339 340 free(src); 341 free(bsrc); 342 return 0; 343 out: 344 free(src); 345 free(bsrc); 346 if (*sd != -1) 347 close(*sd); 348 return rc; 349 } 350 351 /* 352 * Talk to a remote rsync://-enabled server sender. 353 * Returns exit code 0 on success, 1 on failure, 2 on failure with 354 * incompatible protocols. 355 */ 356 int 357 rsync_socket(const struct opts *opts, int sd, const struct fargs *f) 358 { 359 struct sess sess; 360 size_t i, skip; 361 int c, rc = 1; 362 char **args, buf[BUFSIZ]; 363 uint8_t byte; 364 365 if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil", 366 NULL) == -1) 367 err(1, "pledge"); 368 369 memset(&sess, 0, sizeof(struct sess)); 370 sess.lver = RSYNC_PROTOCOL; 371 sess.opts = opts; 372 373 assert(f->host != NULL); 374 assert(f->module != NULL); 375 376 if ((args = fargs_cmdline(&sess, f, &skip)) == NULL) { 377 ERRX1("fargs_cmdline"); 378 exit(1); 379 } 380 381 /* Initiate with the rsyncd version and module request. */ 382 383 (void)snprintf(buf, sizeof(buf), "@RSYNCD: %d", sess.lver); 384 if (!io_write_line(&sess, sd, buf)) { 385 ERRX1("io_write_line"); 386 goto out; 387 } 388 389 LOG2("requesting module: %s, %s", f->module, f->host); 390 391 if (!io_write_line(&sess, sd, f->module)) { 392 ERRX1("io_write_line"); 393 goto out; 394 } 395 396 /* 397 * Now we read the server's response, byte-by-byte, one newline 398 * terminated at a time, limited to BUFSIZ line length. 399 * For this protocol version, this consists of either @RSYNCD 400 * followed by some text (just "ok" and the remote version) or 401 * the message of the day. 402 */ 403 404 for (;;) { 405 for (i = 0; i < sizeof(buf); i++) { 406 if (!io_read_byte(&sess, sd, &byte)) { 407 ERRX1("io_read_byte"); 408 goto out; 409 } 410 if ((buf[i] = byte) == '\n') 411 break; 412 } 413 if (i == sizeof(buf)) { 414 ERRX("line buffer overrun"); 415 goto out; 416 } else if (i == 0) 417 continue; 418 419 /* 420 * The rsyncd protocol isn't very clear as to whether we 421 * get a CRLF or not: I don't actually see this being 422 * transmitted over the wire. 423 */ 424 425 assert(i > 0); 426 buf[i] = '\0'; 427 if (buf[i - 1] == '\r') 428 buf[i - 1] = '\0'; 429 430 if ((c = protocol_line(&sess, f->host, buf)) < 0) { 431 ERRX1("protocol_line"); 432 goto out; 433 } else if (c > 0) 434 break; 435 } 436 437 /* 438 * Now we've exchanged all of our protocol information. 439 * We want to send our command-line arguments over the wire, 440 * each with a newline termination. 441 * Use the same arguments when invoking the server, but leave 442 * off the binary name(s). 443 * Emit a standalone newline afterward. 444 */ 445 446 for (i = skip ; args[i] != NULL; i++) 447 if (!io_write_line(&sess, sd, args[i])) { 448 ERRX1("io_write_line"); 449 goto out; 450 } 451 if (!io_write_byte(&sess, sd, '\n')) { 452 ERRX1("io_write_line"); 453 goto out; 454 } 455 456 /* 457 * All data after this point is going to be multiplexed, so turn 458 * on the multiplexer for our reads and writes. 459 */ 460 461 /* Protocol exchange: get the random seed. */ 462 463 if (!io_read_int(&sess, sd, &sess.seed)) { 464 ERRX1("io_read_int"); 465 goto out; 466 } 467 468 /* Now we've completed the handshake. */ 469 470 if (sess.rver < sess.lver) { 471 ERRX("remote protocol is older than our own (%d < %d): " 472 "this is not supported", 473 sess.rver, sess.lver); 474 rc = 2; 475 goto out; 476 } 477 478 sess.mplex_reads = 1; 479 LOG2("read multiplexing enabled"); 480 481 LOG2("socket detected client version %d, server version %d, seed %d", 482 sess.lver, sess.rver, sess.seed); 483 484 assert(f->mode == FARGS_RECEIVER); 485 486 LOG2("client starting receiver: %s", f->host); 487 if (!rsync_receiver(&sess, sd, sd, f->sink)) { 488 ERRX1("rsync_receiver"); 489 goto out; 490 } 491 492 #if 0 493 /* Probably the EOF. */ 494 if (io_read_check(&sess, sd)) 495 WARNX("data remains in read pipe"); 496 #endif 497 498 rc = 0; 499 out: 500 free(args); 501 return rc; 502 } 503