[BACK]Return to socket.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rsync

Annotation of src/usr.bin/rsync/socket.c, Revision 1.30

1.30    ! claudio     1: /*     $Id: socket.c,v 1.29 2021/03/31 19:45:16 job Exp $ */
1.1       benno       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>
1.19      deraadt    33: #include <err.h>
1.1       benno      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 {
1.2       benno      42:        int              family; /* PF_INET or PF_INET6 */
                     43:        char             ip[INET6_ADDRSTRLEN]; /* formatted string */
1.1       benno      44:        struct sockaddr_storage sa; /* socket */
                     45:        socklen_t        salen; /* length of socket buffer */
                     46: };
                     47:
                     48: /*
1.26      claudio    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: /*
1.1       benno      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
1.26      claudio    75: inet_connect(int *sd, const struct source *src, const char *host,
                     76:     const struct source *bsrc, size_t bsrcsz)
1.1       benno      77: {
                     78:        int      c, flags;
                     79:
1.5       deraadt    80:        if (*sd != -1)
1.1       benno      81:                close(*sd);
                     82:
1.23      benno      83:        LOG2("trying: %s, %s", src->ip, host);
1.1       benno      84:
1.5       deraadt    85:        if ((*sd = socket(src->family, SOCK_STREAM, 0)) == -1) {
1.23      benno      86:                ERR("socket");
1.1       benno      87:                return -1;
                     88:        }
                     89:
1.26      claudio    90:        if (inet_bind(*sd, src->family, bsrc, bsrcsz) == -1) {
                     91:                ERR("bind");
                     92:                return -1;
                     93:        }
                     94:
1.1       benno      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:
1.7       deraadt   102:        c = connect(*sd, (const struct sockaddr *)&src->sa, src->salen);
1.5       deraadt   103:        if (c == -1) {
1.28      kn        104:                if (errno == EADDRNOTAVAIL)
                    105:                        return 0;
1.5       deraadt   106:                if (errno == ECONNREFUSED || errno == EHOSTUNREACH) {
1.23      benno     107:                        WARNX("connect refused: %s, %s",
1.7       deraadt   108:                            src->ip, host);
1.1       benno     109:                        return 0;
                    110:                }
1.23      benno     111:                ERR("connect");
1.1       benno     112:                return -1;
                    113:        }
                    114:
                    115:        /* Set up non-blocking mode. */
                    116:
1.5       deraadt   117:        if ((flags = fcntl(*sd, F_GETFL, 0)) == -1) {
1.23      benno     118:                ERR("fcntl");
1.1       benno     119:                return -1;
1.5       deraadt   120:        } else if (fcntl(*sd, F_SETFL, flags|O_NONBLOCK) == -1) {
1.23      benno     121:                ERR("fcntl");
1.1       benno     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 *
1.26      claudio   135: inet_resolve(struct sess *sess, const char *host, size_t *sz, int passive)
1.1       benno     136: {
                    137:        struct addrinfo  hints, *res0, *res;
                    138:        struct sockaddr *sa;
                    139:        struct source   *src = NULL;
1.26      claudio   140:        const char      *port = sess->opts->port;
1.1       benno     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;
1.12      deraadt   148:        hints.ai_socktype = SOCK_STREAM;
1.26      claudio   149:        if (passive) {
                    150:                hints.ai_flags = SOCK_STREAM;
                    151:                port = NULL;
                    152:        }
1.1       benno     153:
1.26      claudio   154:        error = getaddrinfo(host, port, &hints, &res0);
1.1       benno     155:
1.23      benno     156:        LOG2("resolving: %s", host);
1.1       benno     157:
                    158:        if (error == EAI_AGAIN || error == EAI_NONAME) {
1.23      benno     159:                ERRX("could not resolve hostname %s: %s",
1.7       deraadt   160:                    host, gai_strerror(error));
1.1       benno     161:                return NULL;
1.10      deraadt   162:        } else if (error == EAI_SERVICE) {
1.23      benno     163:                ERRX("could not resolve service rsync: %s",
1.18      benno     164:                    gai_strerror(error));
1.10      deraadt   165:                return NULL;
1.1       benno     166:        } else if (error) {
1.23      benno     167:                ERRX("getaddrinfo: %s: %s", host, gai_strerror(error));
1.1       benno     168:                return NULL;
                    169:        }
                    170:
                    171:        /* Allocate for all available addresses. */
                    172:
1.14      deraadt   173:        for (res = res0; res != NULL; res = res->ai_next)
1.1       benno     174:                if (res->ai_family == AF_INET ||
                    175:                    res->ai_family == AF_INET6)
                    176:                        srcsz++;
                    177:
1.5       deraadt   178:        if (srcsz == 0) {
1.23      benno     179:                ERRX("no addresses resolved: %s", host);
1.1       benno     180:                freeaddrinfo(res0);
                    181:                return NULL;
                    182:        }
                    183:
                    184:        src = calloc(srcsz, sizeof(struct source));
1.5       deraadt   185:        if (src == NULL) {
1.23      benno     186:                ERRX("calloc");
1.1       benno     187:                freeaddrinfo(res0);
                    188:                return NULL;
                    189:        }
                    190:
1.14      deraadt   191:        for (i = 0, res = res0; res != NULL; res = res->ai_next) {
1.1       benno     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;
1.5       deraadt   206:                if (res->ai_family == AF_INET) {
1.1       benno     207:                        src[i].family = PF_INET;
                    208:                        inet_ntop(AF_INET,
1.7       deraadt   209:                            &(((struct sockaddr_in *)sa)->sin_addr),
                    210:                            src[i].ip, INET6_ADDRSTRLEN);
1.1       benno     211:                } else {
                    212:                        src[i].family = PF_INET6;
                    213:                        inet_ntop(AF_INET6,
1.7       deraadt   214:                            &(((struct sockaddr_in6 *)sa)->sin6_addr),
                    215:                            src[i].ip, INET6_ADDRSTRLEN);
1.1       benno     216:                }
1.2       benno     217:
1.23      benno     218:                LOG2("hostname resolved: %s: %s", host, src[i].ip);
1.1       benno     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
1.24      benno     233: protocol_line(struct sess *sess, __attribute__((unused)) const char *host,
                    234:     const char *cp)
1.1       benno     235: {
                    236:        int     major, minor;
                    237:
                    238:        if (strncmp(cp, "@RSYNCD: ", 9)) {
1.29      job       239:                if (sess->opts->no_motd == 0)
                    240:                        LOG1("%s", cp);
1.1       benno     241:                return 0;
                    242:        }
                    243:
                    244:        cp += 9;
                    245:        while (isspace((unsigned char)*cp))
                    246:                cp++;
                    247:
                    248:        /* @RSYNCD: OK indicates that we're finished. */
                    249:
1.5       deraadt   250:        if (strcmp(cp, "OK") == 0)
1.1       benno     251:                return 1;
                    252:
                    253:        /*
                    254:         * Otherwise, all we have left is our version.
                    255:         * There are two formats: x.y (w/submodule) and x.
                    256:         */
                    257:
1.5       deraadt   258:        if (sscanf(cp, "%d.%d", &major, &minor) == 2) {
1.1       benno     259:                sess->rver = major;
                    260:                return 0;
1.5       deraadt   261:        } else if (sscanf(cp, "%d", &major) == 1) {
1.1       benno     262:                sess->rver = major;
                    263:                return 0;
                    264:        }
                    265:
1.23      benno     266:        ERRX("rsyncd protocol error: unknown command");
1.1       benno     267:        return -1;
                    268: }
                    269:
                    270: /*
1.22      naddy     271:  * Connect to a remote rsync://-enabled server sender.
                    272:  * Returns exit code 0 on success, 1 on failure.
1.1       benno     273:  */
                    274: int
1.22      naddy     275: rsync_connect(const struct opts *opts, int *sd, const struct fargs *f)
1.1       benno     276: {
                    277:        struct sess       sess;
1.26      claudio   278:        struct source    *src = NULL, *bsrc = NULL;
                    279:        size_t            i, srcsz = 0, bsrcsz = 0;
1.22      naddy     280:        int               c, rc = 1;
1.1       benno     281:
1.19      deraadt   282:        if (pledge("stdio unix rpath wpath cpath dpath inet fattr chown dns getpw unveil",
                    283:            NULL) == -1)
1.30    ! claudio   284:                err(ERR_IPC, "pledge");
1.19      deraadt   285:
1.1       benno     286:        memset(&sess, 0, sizeof(struct sess));
                    287:        sess.opts = opts;
                    288:
1.5       deraadt   289:        assert(f->host != NULL);
1.1       benno     290:
                    291:        /* Resolve all IP addresses from the host. */
                    292:
1.26      claudio   293:        if ((src = inet_resolve(&sess, f->host, &srcsz, 0)) == NULL) {
1.23      benno     294:                ERRX1("inet_resolve");
1.19      deraadt   295:                exit(1);
1.1       benno     296:        }
1.26      claudio   297:        if (opts->address != NULL)
                    298:                if ((bsrc = inet_resolve(&sess, opts->address, &bsrcsz, 1)) ==
                    299:                    NULL) {
                    300:                        ERRX1("inet_resolve bind");
                    301:                        exit(1);
                    302:                }
1.1       benno     303:
                    304:        /* Drop the DNS pledge. */
                    305:
1.19      deraadt   306:        if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw inet unveil",
                    307:            NULL) == -1) {
1.23      benno     308:                ERR("pledge");
1.19      deraadt   309:                exit(1);
1.1       benno     310:        }
                    311:
                    312:        /*
                    313:         * Iterate over all addresses, trying to connect.
                    314:         * When we succeed, then continue using the connected socket.
                    315:         */
                    316:
                    317:        assert(srcsz);
                    318:        for (i = 0; i < srcsz; i++) {
1.26      claudio   319:                c = inet_connect(sd, &src[i], f->host, bsrc, bsrcsz);
1.20      deraadt   320:                if (c < 0) {
1.23      benno     321:                        ERRX1("inet_connect");
1.1       benno     322:                        goto out;
1.20      deraadt   323:                } else if (c > 0)
1.1       benno     324:                        break;
                    325:        }
                    326:
                    327:        /* Drop the inet pledge. */
1.19      deraadt   328:        if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
                    329:            NULL) == -1) {
1.23      benno     330:                ERR("pledge");
1.1       benno     331:                goto out;
                    332:        }
                    333:
                    334:        if (i == srcsz) {
1.23      benno     335:                ERRX("cannot connect to host: %s", f->host);
1.1       benno     336:                goto out;
                    337:        }
                    338:
1.23      benno     339:        LOG2("connected: %s, %s", src[i].ip, f->host);
1.22      naddy     340:
                    341:        free(src);
1.26      claudio   342:        free(bsrc);
1.22      naddy     343:        return 0;
                    344: out:
                    345:        free(src);
1.26      claudio   346:        free(bsrc);
1.22      naddy     347:        if (*sd != -1)
                    348:                close(*sd);
                    349:        return rc;
                    350: }
                    351:
                    352: /*
                    353:  * Talk to a remote rsync://-enabled server sender.
                    354:  * Returns exit code 0 on success, 1 on failure, 2 on failure with
                    355:  * incompatible protocols.
                    356:  */
                    357: int
                    358: rsync_socket(const struct opts *opts, int sd, const struct fargs *f)
                    359: {
                    360:        struct sess       sess;
                    361:        size_t            i, skip;
                    362:        int               c, rc = 1;
                    363:        char            **args, buf[BUFSIZ];
                    364:        uint8_t           byte;
                    365:
                    366:        if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
                    367:            NULL) == -1)
1.30    ! claudio   368:                err(ERR_IPC, "pledge");
1.22      naddy     369:
                    370:        memset(&sess, 0, sizeof(struct sess));
                    371:        sess.lver = RSYNC_PROTOCOL;
                    372:        sess.opts = opts;
                    373:
                    374:        assert(f->host != NULL);
                    375:        assert(f->module != NULL);
                    376:
1.30    ! claudio   377:        args = fargs_cmdline(&sess, f, &skip);
1.22      naddy     378:
1.1       benno     379:        /* Initiate with the rsyncd version and module request. */
                    380:
                    381:        (void)snprintf(buf, sizeof(buf), "@RSYNCD: %d", sess.lver);
1.4       deraadt   382:        if (!io_write_line(&sess, sd, buf)) {
1.23      benno     383:                ERRX1("io_write_line");
1.1       benno     384:                goto out;
                    385:        }
                    386:
1.23      benno     387:        LOG2("requesting module: %s, %s", f->module, f->host);
1.1       benno     388:
1.4       deraadt   389:        if (!io_write_line(&sess, sd, f->module)) {
1.23      benno     390:                ERRX1("io_write_line");
1.1       benno     391:                goto out;
                    392:        }
                    393:
                    394:        /*
                    395:         * Now we read the server's response, byte-by-byte, one newline
                    396:         * terminated at a time, limited to BUFSIZ line length.
                    397:         * For this protocol version, this consists of either @RSYNCD
                    398:         * followed by some text (just "ok" and the remote version) or
                    399:         * the message of the day.
                    400:         */
                    401:
                    402:        for (;;) {
                    403:                for (i = 0; i < sizeof(buf); i++) {
1.4       deraadt   404:                        if (!io_read_byte(&sess, sd, &byte)) {
1.23      benno     405:                                ERRX1("io_read_byte");
1.1       benno     406:                                goto out;
                    407:                        }
1.5       deraadt   408:                        if ((buf[i] = byte) == '\n')
1.1       benno     409:                                break;
                    410:                }
                    411:                if (i == sizeof(buf)) {
1.23      benno     412:                        ERRX("line buffer overrun");
1.1       benno     413:                        goto out;
1.5       deraadt   414:                } else if (i == 0)
1.1       benno     415:                        continue;
                    416:
                    417:                /*
                    418:                 * The rsyncd protocol isn't very clear as to whether we
                    419:                 * get a CRLF or not: I don't actually see this being
                    420:                 * transmitted over the wire.
                    421:                 */
                    422:
                    423:                assert(i > 0);
                    424:                buf[i] = '\0';
1.5       deraadt   425:                if (buf[i - 1] == '\r')
1.1       benno     426:                        buf[i - 1] = '\0';
                    427:
1.20      deraadt   428:                if ((c = protocol_line(&sess, f->host, buf)) < 0) {
1.23      benno     429:                        ERRX1("protocol_line");
1.1       benno     430:                        goto out;
1.20      deraadt   431:                } else if (c > 0)
1.1       benno     432:                        break;
                    433:        }
                    434:
                    435:        /*
                    436:         * Now we've exchanged all of our protocol information.
                    437:         * We want to send our command-line arguments over the wire,
                    438:         * each with a newline termination.
                    439:         * Use the same arguments when invoking the server, but leave
                    440:         * off the binary name(s).
                    441:         * Emit a standalone newline afterward.
                    442:         */
                    443:
1.22      naddy     444:        for (i = skip ; args[i] != NULL; i++)
1.4       deraadt   445:                if (!io_write_line(&sess, sd, args[i])) {
1.23      benno     446:                        ERRX1("io_write_line");
1.1       benno     447:                        goto out;
                    448:                }
1.4       deraadt   449:        if (!io_write_byte(&sess, sd, '\n')) {
1.23      benno     450:                ERRX1("io_write_line");
1.1       benno     451:                goto out;
                    452:        }
                    453:
                    454:        /*
                    455:         * All data after this point is going to be multiplexed, so turn
                    456:         * on the multiplexer for our reads and writes.
                    457:         */
                    458:
                    459:        /* Protocol exchange: get the random seed. */
                    460:
1.4       deraadt   461:        if (!io_read_int(&sess, sd, &sess.seed)) {
1.23      benno     462:                ERRX1("io_read_int");
1.1       benno     463:                goto out;
                    464:        }
                    465:
                    466:        /* Now we've completed the handshake. */
                    467:
                    468:        if (sess.rver < sess.lver) {
1.23      benno     469:                ERRX("remote protocol is older than our own (%d < %d): "
1.21      deraadt   470:                    "this is not supported",
                    471:                    sess.rver, sess.lver);
1.20      deraadt   472:                rc = 2;
1.1       benno     473:                goto out;
                    474:        }
                    475:
                    476:        sess.mplex_reads = 1;
1.23      benno     477:        LOG2("read multiplexing enabled");
1.1       benno     478:
1.23      benno     479:        LOG2("socket detected client version %d, server version %d, seed %d",
1.21      deraadt   480:            sess.lver, sess.rver, sess.seed);
1.1       benno     481:
1.5       deraadt   482:        assert(f->mode == FARGS_RECEIVER);
1.1       benno     483:
1.23      benno     484:        LOG2("client starting receiver: %s", f->host);
1.4       deraadt   485:        if (!rsync_receiver(&sess, sd, sd, f->sink)) {
1.23      benno     486:                ERRX1("rsync_receiver");
1.1       benno     487:                goto out;
                    488:        }
                    489:
                    490: #if 0
                    491:        /* Probably the EOF. */
                    492:        if (io_read_check(&sess, sd))
1.23      benno     493:                WARNX("data remains in read pipe");
1.1       benno     494: #endif
                    495:
1.20      deraadt   496:        rc = 0;
1.1       benno     497: out:
                    498:        free(args);
                    499:        return rc;
                    500: }