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

Annotation of src/usr.bin/rsync/client.c, Revision 1.13

1.13    ! deraadt     1: /*     $Id: client.c,v 1.12 2019/03/18 15:33:21 deraadt 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:
                     19: #include <assert.h>
                     20: #include <inttypes.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23: #include <unistd.h>
1.11      deraadt    24: #include <err.h>
1.1       benno      25:
                     26: #include "extern.h"
                     27:
                     28: /*
                     29:  * The rsync client runs on the operator's local machine.
                     30:  * It can either be in sender or receiver mode.
                     31:  * In the former, it synchronises local files from a remote sink.
                     32:  * In the latter, the remote sink synchronses to the local files.
1.13    ! deraadt    33:  * Returns exit code 0 on success, 1 on failure, 2 on failure with
        !            34:  * incompatible protocols.
1.1       benno      35:  */
                     36: int
                     37: rsync_client(const struct opts *opts, int fd, const struct fargs *f)
                     38: {
                     39:        struct sess      sess;
1.13    ! deraadt    40:        int              rc = 1;
1.1       benno      41:
                     42:        /* Standard rsync preamble, sender side. */
1.11      deraadt    43:
                     44:        if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
                     45:            NULL) == -1)
                     46:                err(1, "pledge");
1.1       benno      47:
                     48:        memset(&sess, 0, sizeof(struct sess));
                     49:        sess.opts = opts;
                     50:        sess.lver = RSYNC_PROTOCOL;
                     51:
1.3       deraadt    52:        if (!io_write_int(&sess, fd, sess.lver)) {
1.1       benno      53:                ERRX1(&sess, "io_write_int");
                     54:                goto out;
1.3       deraadt    55:        } else if (!io_read_int(&sess, fd, &sess.rver)) {
1.1       benno      56:                ERRX1(&sess, "io_read_int");
                     57:                goto out;
1.3       deraadt    58:        } else if (!io_read_int(&sess, fd, &sess.seed)) {
1.1       benno      59:                ERRX1(&sess, "io_read_int");
                     60:                goto out;
                     61:        }
                     62:
                     63:        if (sess.rver < sess.lver) {
1.13    ! deraadt    64:                ERRX(&sess,
        !            65:                    "remote protocol %d is older than our own %d: unsupported",
        !            66:                    sess.rver, sess.lver);
        !            67:                rc = 2;
1.1       benno      68:                goto out;
                     69:        }
                     70:
                     71:        LOG2(&sess, "client detected client version %" PRId32
                     72:                ", server version %" PRId32 ", seed %" PRId32,
                     73:                sess.lver, sess.rver, sess.seed);
                     74:
                     75:        sess.mplex_reads = 1;
                     76:
                     77:        /*
                     78:         * Now we need to get our list of files.
                     79:         * Senders (and locals) send; receivers receive.
                     80:         */
                     81:
1.12      deraadt    82:        if (f->mode != FARGS_RECEIVER) {
1.1       benno      83:                LOG2(&sess, "client starting sender: %s",
1.4       deraadt    84:                    f->host == NULL ? "(local)" : f->host);
1.5       deraadt    85:                if (!rsync_sender(&sess, fd, fd, f->sourcesz,
                     86:                    f->sources)) {
1.1       benno      87:                        ERRX1(&sess, "rsync_sender");
                     88:                        goto out;
                     89:                }
                     90:        } else {
                     91:                LOG2(&sess, "client starting receiver: %s",
1.4       deraadt    92:                    f->host == NULL ? "(local)" : f->host);
1.3       deraadt    93:                if (!rsync_receiver(&sess, fd, fd, f->sink)) {
1.1       benno      94:                        ERRX1(&sess, "rsync_receiver");
                     95:                        goto out;
                     96:                }
                     97:        }
                     98:
                     99: #if 0
                    100:        /* Probably the EOF. */
                    101:        if (io_read_check(&sess, fd))
                    102:                WARNX(&sess, "data remains in read pipe");
                    103: #endif
                    104:
1.13    ! deraadt   105:        rc = 0;
1.1       benno     106: out:
                    107:        return rc;
                    108: }