[BACK]Return to rcp.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / bin / rcp

Annotation of src/bin/rcp/rcp.c, Revision 1.49.36.1

1.49.36.1! martin      1: /*     $NetBSD: rcp.c,v 1.49 2012/05/07 15:22:54 chs Exp $     */
1.9       cgd         2:
1.1       cgd         3: /*
1.9       cgd         4:  * Copyright (c) 1983, 1990, 1992, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.33      agc        15:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
1.15      christos   32: #include <sys/cdefs.h>
1.1       cgd        33: #ifndef lint
1.47      lukem      34: __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1992, 1993\
                     35:  The Regents of the University of California.  All rights reserved.");
1.1       cgd        36: #endif /* not lint */
                     37:
                     38: #ifndef lint
1.9       cgd        39: #if 0
                     40: static char sccsid[] = "@(#)rcp.c      8.2 (Berkeley) 4/2/94";
                     41: #else
1.49.36.1! martin     42: __RCSID("$NetBSD: rcp.c,v 1.49 2012/05/07 15:22:54 chs Exp $");
1.9       cgd        43: #endif
1.1       cgd        44: #endif /* not lint */
                     45:
                     46: #include <sys/param.h>
                     47: #include <sys/stat.h>
                     48: #include <sys/time.h>
                     49: #include <sys/socket.h>
                     50: #include <netinet/in.h>
                     51: #include <netinet/in_systm.h>
                     52: #include <netinet/ip.h>
1.9       cgd        53:
                     54: #include <ctype.h>
1.1       cgd        55: #include <dirent.h>
1.9       cgd        56: #include <err.h>
                     57: #include <errno.h>
1.1       cgd        58: #include <fcntl.h>
1.46      christos   59: #include <locale.h>
1.9       cgd        60: #include <netdb.h>
                     61: #include <pwd.h>
1.1       cgd        62: #include <signal.h>
                     63: #include <stdio.h>
                     64: #include <stdlib.h>
                     65: #include <string.h>
1.9       cgd        66: #include <unistd.h>
                     67:
1.1       cgd        68: #include "pathnames.h"
1.9       cgd        69: #include "extern.h"
1.1       cgd        70:
1.37      ginsbach   71: #define        OPTIONS "46dfprt"
1.1       cgd        72:
                     73: struct passwd *pwd;
1.19      christos   74: char *pwname;
1.1       cgd        75: u_short        port;
                     76: uid_t  userid;
                     77: int errs, rem;
                     78: int pflag, iamremote, iamrecursive, targetshouldbedirectory;
1.37      ginsbach   79: int family = AF_UNSPEC;
1.40      christos   80: static char dot[] = ".";
1.1       cgd        81:
                     82: #define        CMDNEEDS        64
                     83: char cmd[CMDNEEDS];            /* must hold "rcp -r -p -d\0" */
                     84:
1.36      xtraeme    85: int     response(void);
                     86: void    rsource(char *, struct stat *);
                     87: void    sink(int, char *[]);
                     88: void    source(int, char *[]);
                     89: void    tolocal(int, char *[]);
                     90: void    toremote(char *, int, char *[]);
                     91: void    usage(void);
1.1       cgd        92:
1.6       cgd        93: int
1.36      xtraeme    94: main(int argc, char *argv[])
1.1       cgd        95: {
                     96:        struct servent *sp;
                     97:        int ch, fflag, tflag;
1.40      christos   98:        char *targ;
                     99:        const char *shell;
1.1       cgd       100:
1.46      christos  101:        setprogname(argv[0]);
                    102:        (void)setlocale(LC_ALL, "");
                    103:
1.1       cgd       104:        fflag = tflag = 0;
1.17      lukem     105:        while ((ch = getopt(argc, argv, OPTIONS)) != -1)
1.9       cgd       106:                switch(ch) {                    /* User-visible flags. */
1.37      ginsbach  107:                case '4':
                    108:                        family = AF_INET;
                    109:                        break;
                    110:                case '6':
                    111:                        family = AF_INET6;
                    112:                        break;
1.9       cgd       113:                case 'K':
1.1       cgd       114:                        break;
1.9       cgd       115:                case 'p':
                    116:                        pflag = 1;
                    117:                        break;
                    118:                case 'r':
                    119:                        iamrecursive = 1;
                    120:                        break;
                    121:                                                /* Server options. */
1.1       cgd       122:                case 'd':
                    123:                        targetshouldbedirectory = 1;
                    124:                        break;
                    125:                case 'f':                       /* "from" */
                    126:                        iamremote = 1;
                    127:                        fflag = 1;
                    128:                        break;
                    129:                case 't':                       /* "to" */
                    130:                        iamremote = 1;
                    131:                        tflag = 1;
                    132:                        break;
                    133:                case '?':
                    134:                default:
                    135:                        usage();
                    136:                }
                    137:        argc -= optind;
                    138:        argv += optind;
                    139:
                    140:        sp = getservbyname(shell = "shell", "tcp");
1.9       cgd       141:        if (sp == NULL)
                    142:                errx(1, "%s/tcp: unknown service", shell);
1.1       cgd       143:        port = sp->s_port;
                    144:
1.9       cgd       145:        if ((pwd = getpwuid(userid = getuid())) == NULL)
                    146:                errx(1, "unknown user %d", (int)userid);
                    147:
1.19      christos  148:        if ((pwname = strdup(pwd->pw_name)) == NULL)
1.26      drochner  149:                err(1, NULL);
1.19      christos  150:
1.9       cgd       151:        rem = STDIN_FILENO;             /* XXX */
1.1       cgd       152:
1.9       cgd       153:        if (fflag) {                    /* Follow "protocol", send data. */
1.1       cgd       154:                (void)response();
                    155:                source(argc, argv);
                    156:                exit(errs);
                    157:        }
                    158:
1.9       cgd       159:        if (tflag) {                    /* Receive data. */
1.1       cgd       160:                sink(argc, argv);
                    161:                exit(errs);
                    162:        }
                    163:
                    164:        if (argc < 2)
                    165:                usage();
                    166:        if (argc > 2)
                    167:                targetshouldbedirectory = 1;
                    168:
                    169:        rem = -1;
1.9       cgd       170:        /* Command to be executed on remote system using "rsh". */
1.1       cgd       171:        (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
                    172:            iamrecursive ? " -r" : "", pflag ? " -p" : "",
                    173:            targetshouldbedirectory ? " -d" : "");
                    174:
                    175:        (void)signal(SIGPIPE, lostconn);
                    176:
1.15      christos  177:        if ((targ = colon(argv[argc - 1])) != NULL)/* Dest is remote host. */
1.9       cgd       178:                toremote(targ, argc, argv);
1.1       cgd       179:        else {
1.9       cgd       180:                tolocal(argc, argv);            /* Dest is local host. */
1.1       cgd       181:                if (targetshouldbedirectory)
                    182:                        verifydir(argv[argc - 1]);
                    183:        }
                    184:        exit(errs);
1.21      mycroft   185:        /* NOTREACHED */
1.1       cgd       186: }
                    187:
1.6       cgd       188: void
1.36      xtraeme   189: toremote(char *targ, int argc, char *argv[])
1.1       cgd       190: {
1.44      christos  191:        int i;
                    192:        size_t len;
1.19      christos  193:        char *bp, *host, *src, *suser, *thost, *tuser;
1.1       cgd       194:
                    195:        *targ++ = 0;
                    196:        if (*targ == 0)
1.40      christos  197:                targ = dot;
1.1       cgd       198:
1.15      christos  199:        if ((thost = strchr(argv[argc - 1], '@')) != NULL) {
1.1       cgd       200:                /* user@host */
                    201:                *thost++ = 0;
                    202:                tuser = argv[argc - 1];
                    203:                if (*tuser == '\0')
                    204:                        tuser = NULL;
1.22      mycroft   205:                else if (!okname(tuser))
1.1       cgd       206:                        exit(1);
                    207:        } else {
                    208:                thost = argv[argc - 1];
                    209:                tuser = NULL;
                    210:        }
1.39      ginsbach  211:        thost = unbracket(thost);
1.1       cgd       212:
                    213:        for (i = 0; i < argc - 1; i++) {
                    214:                src = colon(argv[i]);
                    215:                if (src) {                      /* remote to remote */
                    216:                        *src++ = 0;
                    217:                        if (*src == 0)
1.40      christos  218:                                src = dot;
1.9       cgd       219:                        host = strchr(argv[i], '@');
1.1       cgd       220:                        len = strlen(_PATH_RSH) + strlen(argv[i]) +
                    221:                            strlen(src) + (tuser ? strlen(tuser) : 0) +
                    222:                            strlen(thost) + strlen(targ) + CMDNEEDS + 20;
                    223:                        if (!(bp = malloc(len)))
1.26      drochner  224:                                err(1, NULL);
1.1       cgd       225:                        if (host) {
                    226:                                *host++ = 0;
1.39      ginsbach  227:                                host = unbracket(host);
1.1       cgd       228:                                suser = argv[i];
                    229:                                if (*suser == '\0')
1.19      christos  230:                                        suser = pwname;
1.41      ginsbach  231:                                else if (!okname(suser)) {
                    232:                                        (void)free(bp);
1.1       cgd       233:                                        continue;
1.41      ginsbach  234:                                }
1.1       cgd       235:                                (void)snprintf(bp, len,
                    236:                                    "%s %s -l %s -n %s %s '%s%s%s:%s'",
                    237:                                    _PATH_RSH, host, suser, cmd, src,
                    238:                                    tuser ? tuser : "", tuser ? "@" : "",
                    239:                                    thost, targ);
1.39      ginsbach  240:                        } else {
                    241:                                host = unbracket(argv[i]);
1.1       cgd       242:                                (void)snprintf(bp, len,
1.9       cgd       243:                                    "exec %s %s -n %s %s '%s%s%s:%s'",
1.1       cgd       244:                                    _PATH_RSH, argv[i], cmd, src,
                    245:                                    tuser ? tuser : "", tuser ? "@" : "",
                    246:                                    thost, targ);
1.39      ginsbach  247:                        }
1.12      mrg       248:                        (void)susystem(bp);
1.1       cgd       249:                        (void)free(bp);
                    250:                } else {                        /* local to remote */
                    251:                        if (rem == -1) {
                    252:                                len = strlen(targ) + CMDNEEDS + 20;
                    253:                                if (!(bp = malloc(len)))
1.26      drochner  254:                                        err(1, NULL);
1.1       cgd       255:                                (void)snprintf(bp, len, "%s -t %s", cmd, targ);
                    256:                                host = thost;
1.34      jdolecek  257:                                        rem = rcmd_af(&host, port, pwname,
1.19      christos  258:                                            tuser ? tuser : pwname,
1.37      ginsbach  259:                                            bp, NULL, family);
1.22      mycroft   260:                                if (rem < 0)
1.1       cgd       261:                                        exit(1);
1.22      mycroft   262:                                if (response() < 0)
1.1       cgd       263:                                        exit(1);
                    264:                                (void)free(bp);
                    265:                        }
                    266:                        source(1, argv+i);
                    267:                }
                    268:        }
                    269: }
                    270:
1.6       cgd       271: void
1.36      xtraeme   272: tolocal(int argc, char *argv[])
1.1       cgd       273: {
1.44      christos  274:        int i;
                    275:        size_t len;
1.1       cgd       276:        char *bp, *host, *src, *suser;
                    277:
                    278:        for (i = 0; i < argc - 1; i++) {
1.9       cgd       279:                if (!(src = colon(argv[i]))) {          /* Local to local. */
1.1       cgd       280:                        len = strlen(_PATH_CP) + strlen(argv[i]) +
                    281:                            strlen(argv[argc - 1]) + 20;
                    282:                        if (!(bp = malloc(len)))
1.26      drochner  283:                                err(1, NULL);
1.9       cgd       284:                        (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
1.1       cgd       285:                            iamrecursive ? " -r" : "", pflag ? " -p" : "",
                    286:                            argv[i], argv[argc - 1]);
1.12      mrg       287:                        if (susystem(bp))
1.9       cgd       288:                                ++errs;
1.1       cgd       289:                        (void)free(bp);
                    290:                        continue;
                    291:                }
                    292:                *src++ = 0;
                    293:                if (*src == 0)
1.40      christos  294:                        src = dot;
1.9       cgd       295:                if ((host = strchr(argv[i], '@')) == NULL) {
                    296:                        host = argv[i];
1.19      christos  297:                        suser = pwname;
1.9       cgd       298:                } else {
1.1       cgd       299:                        *host++ = 0;
                    300:                        suser = argv[i];
                    301:                        if (*suser == '\0')
1.19      christos  302:                                suser = pwname;
1.1       cgd       303:                        else if (!okname(suser))
                    304:                                continue;
                    305:                }
1.39      ginsbach  306:                host = unbracket(host);
1.1       cgd       307:                len = strlen(src) + CMDNEEDS + 20;
1.9       cgd       308:                if ((bp = malloc(len)) == NULL)
1.26      drochner  309:                        err(1, NULL);
1.1       cgd       310:                (void)snprintf(bp, len, "%s -f %s", cmd, src);
1.9       cgd       311:                rem =
1.37      ginsbach  312:                        rcmd_af(&host, port, pwname, suser, bp, NULL, family);
1.1       cgd       313:                (void)free(bp);
1.9       cgd       314:                if (rem < 0) {
                    315:                        ++errs;
1.1       cgd       316:                        continue;
1.9       cgd       317:                }
1.1       cgd       318:                sink(1, argv + argc - 1);
                    319:                (void)close(rem);
                    320:                rem = -1;
                    321:        }
                    322: }
                    323:
1.6       cgd       324: void
1.36      xtraeme   325: source(int argc, char *argv[])
1.1       cgd       326: {
                    327:        struct stat stb;
                    328:        static BUF buffer;
                    329:        BUF *bp;
                    330:        off_t i;
1.44      christos  331:        off_t amt;
                    332:        int fd, haderr, indx, result;
1.1       cgd       333:        char *last, *name, buf[BUFSIZ];
                    334:
1.9       cgd       335:        for (indx = 0; indx < argc; ++indx) {
1.37      ginsbach  336:                name = argv[indx];
1.9       cgd       337:                if ((fd = open(name, O_RDONLY, 0)) < 0)
                    338:                        goto syserr;
                    339:                if (fstat(fd, &stb)) {
                    340: syserr:                        run_err("%s: %s", name, strerror(errno));
                    341:                        goto next;
1.1       cgd       342:                }
1.7       mycroft   343:                switch (stb.st_mode & S_IFMT) {
1.1       cgd       344:                case S_IFREG:
                    345:                        break;
                    346:                case S_IFDIR:
                    347:                        if (iamrecursive) {
                    348:                                rsource(name, &stb);
1.9       cgd       349:                                goto next;
1.1       cgd       350:                        }
                    351:                        /* FALLTHROUGH */
                    352:                default:
1.9       cgd       353:                        run_err("%s: not a regular file", name);
                    354:                        goto next;
1.1       cgd       355:                }
1.9       cgd       356:                if ((last = strrchr(name, '/')) == NULL)
1.1       cgd       357:                        last = name;
                    358:                else
1.9       cgd       359:                        ++last;
1.1       cgd       360:                if (pflag) {
                    361:                        /*
                    362:                         * Make it compatible with possible future
                    363:                         * versions expecting microseconds.
                    364:                         */
1.45      dholland  365:                        (void)snprintf(buf, sizeof(buf), "T%lld %ld %lld %ld\n",
                    366:                            (long long)stb.st_mtimespec.tv_sec,
1.18      mycroft   367:                            (long)stb.st_mtimespec.tv_nsec / 1000,
1.45      dholland  368:                            (long long)stb.st_atimespec.tv_sec,
1.18      mycroft   369:                            (long)stb.st_atimespec.tv_nsec / 1000);
1.9       cgd       370:                        (void)write(rem, buf, strlen(buf));
                    371:                        if (response() < 0)
                    372:                                goto next;
                    373:                }
1.23      christos  374: #define        RCPMODEMASK     (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
1.27      lukem     375:                (void)snprintf(buf, sizeof(buf), "C%04o %lld %s\n",
1.23      christos  376:                    stb.st_mode & RCPMODEMASK, (long long)stb.st_size, last);
1.9       cgd       377:                (void)write(rem, buf, strlen(buf));
                    378:                if (response() < 0)
                    379:                        goto next;
                    380:                if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
                    381: next:                  (void)close(fd);
1.1       cgd       382:                        continue;
                    383:                }
1.9       cgd       384:
                    385:                /* Keep writing after an error so that we stay sync'd up. */
1.44      christos  386:                haderr = 0;
                    387:                for (i = 0; i < stb.st_size; i += bp->cnt) {
1.1       cgd       388:                        amt = bp->cnt;
                    389:                        if (i + amt > stb.st_size)
                    390:                                amt = stb.st_size - i;
1.9       cgd       391:                        if (!haderr) {
1.44      christos  392:                                result = read(fd, bp->buf, (size_t)amt);
1.9       cgd       393:                                if (result != amt)
                    394:                                        haderr = result >= 0 ? EIO : errno;
                    395:                        }
                    396:                        if (haderr)
1.44      christos  397:                                (void)write(rem, bp->buf, (size_t)amt);
1.9       cgd       398:                        else {
1.44      christos  399:                                result = write(rem, bp->buf, (size_t)amt);
1.9       cgd       400:                                if (result != amt)
                    401:                                        haderr = result >= 0 ? EIO : errno;
                    402:                        }
1.1       cgd       403:                }
1.9       cgd       404:                if (close(fd) && !haderr)
                    405:                        haderr = errno;
                    406:                if (!haderr)
1.1       cgd       407:                        (void)write(rem, "", 1);
                    408:                else
1.9       cgd       409:                        run_err("%s: %s", name, strerror(haderr));
1.1       cgd       410:                (void)response();
                    411:        }
                    412: }
                    413:
1.6       cgd       414: void
1.36      xtraeme   415: rsource(char *name, struct stat *statp)
1.1       cgd       416: {
                    417:        DIR *dirp;
                    418:        struct dirent *dp;
                    419:        char *last, *vect[1], path[MAXPATHLEN];
                    420:
                    421:        if (!(dirp = opendir(name))) {
1.9       cgd       422:                run_err("%s: %s", name, strerror(errno));
1.1       cgd       423:                return;
                    424:        }
1.9       cgd       425:        last = strrchr(name, '/');
1.1       cgd       426:        if (last == 0)
                    427:                last = name;
                    428:        else
                    429:                last++;
                    430:        if (pflag) {
1.45      dholland  431:                (void)snprintf(path, sizeof(path), "T%lld %ld %lld %ld\n",
                    432:                    (long long)statp->st_mtimespec.tv_sec,
1.18      mycroft   433:                    (long)statp->st_mtimespec.tv_nsec / 1000,
1.45      dholland  434:                    (long long)statp->st_atimespec.tv_sec,
1.18      mycroft   435:                    (long)statp->st_atimespec.tv_nsec / 1000);
1.9       cgd       436:                (void)write(rem, path, strlen(path));
1.1       cgd       437:                if (response() < 0) {
1.44      christos  438:                        (void)closedir(dirp);
1.1       cgd       439:                        return;
                    440:                }
                    441:        }
                    442:        (void)snprintf(path, sizeof(path),
1.24      christos  443:            "D%04o %d %s\n", statp->st_mode & RCPMODEMASK, 0, last);
1.9       cgd       444:        (void)write(rem, path, strlen(path));
1.1       cgd       445:        if (response() < 0) {
1.44      christos  446:                (void)closedir(dirp);
1.1       cgd       447:                return;
                    448:        }
1.15      christos  449:        while ((dp = readdir(dirp)) != NULL) {
1.1       cgd       450:                if (dp->d_ino == 0)
                    451:                        continue;
1.40      christos  452:                if (!strcmp(dp->d_name, dot) || !strcmp(dp->d_name, ".."))
1.1       cgd       453:                        continue;
                    454:                if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) {
1.9       cgd       455:                        run_err("%s/%s: name too long", name, dp->d_name);
1.1       cgd       456:                        continue;
                    457:                }
                    458:                (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
                    459:                vect[0] = path;
                    460:                source(1, vect);
                    461:        }
1.9       cgd       462:        (void)closedir(dirp);
1.1       cgd       463:        (void)write(rem, "E\n", 2);
                    464:        (void)response();
                    465: }
                    466:
1.6       cgd       467: void
1.36      xtraeme   468: sink(int argc, char *argv[])
1.1       cgd       469: {
                    470:        static BUF buffer;
                    471:        struct stat stb;
                    472:        struct timeval tv[2];
1.9       cgd       473:        BUF *bp;
1.44      christos  474:        ssize_t j;
                    475:        off_t i;
                    476:        off_t amt;
                    477:        off_t count;
                    478:        int exists, first, ofd;
                    479:        mode_t mask;
                    480:        mode_t mode;
                    481:        mode_t omode;
1.49.36.1! martin    482:        int setimes, targisdir, wrerr;
1.15      christos  483:        int wrerrno = 0;        /* pacify gcc */
1.49.36.1! martin    484:        const char *wrcontext = NULL;
1.40      christos  485:        char ch, *cp, *np, *targ, *vect[1], buf[BUFSIZ];
                    486:        const char *why;
1.30      ragge     487:        off_t size;
1.49      chs       488:        char *namebuf = NULL;
                    489:        size_t cursize = 0;
1.1       cgd       490:
                    491: #define        atime   tv[0]
                    492: #define        mtime   tv[1]
                    493: #define        SCREWUP(str)    { why = str; goto screwup; }
1.20      mycroft   494:
1.1       cgd       495:        setimes = targisdir = 0;
                    496:        mask = umask(0);
                    497:        if (!pflag)
                    498:                (void)umask(mask);
                    499:        if (argc != 1) {
1.9       cgd       500:                run_err("ambiguous target");
1.1       cgd       501:                exit(1);
                    502:        }
                    503:        targ = *argv;
                    504:        if (targetshouldbedirectory)
                    505:                verifydir(targ);
                    506:        (void)write(rem, "", 1);
1.8       mycroft   507:        if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
1.1       cgd       508:                targisdir = 1;
                    509:        for (first = 1;; first = 0) {
                    510:                cp = buf;
                    511:                if (read(rem, cp, 1) <= 0)
1.49      chs       512:                        goto out;
1.1       cgd       513:                if (*cp++ == '\n')
                    514:                        SCREWUP("unexpected <newline>");
                    515:                do {
                    516:                        if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
                    517:                                SCREWUP("lost connection");
                    518:                        *cp++ = ch;
                    519:                } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
                    520:                *cp = 0;
                    521:
                    522:                if (buf[0] == '\01' || buf[0] == '\02') {
                    523:                        if (iamremote == 0)
1.9       cgd       524:                                (void)write(STDERR_FILENO,
                    525:                                    buf + 1, strlen(buf + 1));
1.22      mycroft   526:                        if (buf[0] == '\02')
1.1       cgd       527:                                exit(1);
1.9       cgd       528:                        ++errs;
1.1       cgd       529:                        continue;
                    530:                }
                    531:                if (buf[0] == 'E') {
                    532:                        (void)write(rem, "", 1);
1.49      chs       533:                        goto out;
1.1       cgd       534:                }
                    535:
                    536:                if (ch == '\n')
                    537:                        *--cp = 0;
                    538:
1.23      christos  539: #define getnum(t) (t) = 0; while (isdigit((unsigned char)*cp)) (t) = (t) * 10 + (*cp++ - '0');
1.1       cgd       540:                cp = buf;
                    541:                if (*cp == 'T') {
                    542:                        setimes++;
                    543:                        cp++;
                    544:                        getnum(mtime.tv_sec);
                    545:                        if (*cp++ != ' ')
                    546:                                SCREWUP("mtime.sec not delimited");
                    547:                        getnum(mtime.tv_usec);
                    548:                        if (*cp++ != ' ')
                    549:                                SCREWUP("mtime.usec not delimited");
                    550:                        getnum(atime.tv_sec);
                    551:                        if (*cp++ != ' ')
                    552:                                SCREWUP("atime.sec not delimited");
                    553:                        getnum(atime.tv_usec);
                    554:                        if (*cp++ != '\0')
                    555:                                SCREWUP("atime.usec not delimited");
                    556:                        (void)write(rem, "", 1);
                    557:                        continue;
                    558:                }
                    559:                if (*cp != 'C' && *cp != 'D') {
                    560:                        /*
                    561:                         * Check for the case "rcp remote:foo\* local:bar".
                    562:                         * In this case, the line "No match." can be returned
                    563:                         * by the shell before the rcp command on the remote is
                    564:                         * executed so the ^Aerror_message convention isn't
                    565:                         * followed.
                    566:                         */
                    567:                        if (first) {
1.9       cgd       568:                                run_err("%s", cp);
1.1       cgd       569:                                exit(1);
                    570:                        }
                    571:                        SCREWUP("expected control record");
                    572:                }
                    573:                mode = 0;
                    574:                for (++cp; cp < buf + 5; cp++) {
                    575:                        if (*cp < '0' || *cp > '7')
                    576:                                SCREWUP("bad mode");
                    577:                        mode = (mode << 3) | (*cp - '0');
                    578:                }
                    579:                if (*cp++ != ' ')
                    580:                        SCREWUP("mode not delimited");
1.9       cgd       581:
1.23      christos  582:                for (size = 0; isdigit((unsigned char)*cp);)
1.1       cgd       583:                        size = size * 10 + (*cp++ - '0');
                    584:                if (*cp++ != ' ')
                    585:                        SCREWUP("size not delimited");
                    586:                if (targisdir) {
1.48      dholland  587:                        char *newnamebuf;
1.1       cgd       588:                        size_t need;
                    589:
1.49      chs       590:                        need = strlen(targ) + strlen(cp) + 2;
1.1       cgd       591:                        if (need > cursize) {
1.49      chs       592:                                need += 256;
1.48      dholland  593:                                newnamebuf = realloc(namebuf, need);
                    594:                                if (newnamebuf != NULL) {
                    595:                                        namebuf = newnamebuf;
                    596:                                        cursize = need;
                    597:                                } else {
1.9       cgd       598:                                        run_err("%s", strerror(errno));
1.49      chs       599:                                        exit(1);
1.48      dholland  600:                                }
1.1       cgd       601:                        }
1.48      dholland  602:                        (void)snprintf(namebuf, cursize, "%s%s%s", targ,
1.1       cgd       603:                            *targ ? "/" : "", cp);
                    604:                        np = namebuf;
1.9       cgd       605:                } else
1.1       cgd       606:                        np = targ;
                    607:                exists = stat(np, &stb) == 0;
                    608:                if (buf[0] == 'D') {
1.9       cgd       609:                        int mod_flag = pflag;
1.1       cgd       610:                        if (exists) {
1.8       mycroft   611:                                if (!S_ISDIR(stb.st_mode)) {
1.1       cgd       612:                                        errno = ENOTDIR;
                    613:                                        goto bad;
                    614:                                }
                    615:                                if (pflag)
                    616:                                        (void)chmod(np, mode);
1.9       cgd       617:                        } else {
                    618:                                /* Handle copying from a read-only directory */
                    619:                                mod_flag = 1;
                    620:                                if (mkdir(np, mode | S_IRWXU) < 0)
                    621:                                        goto bad;
                    622:                        }
1.1       cgd       623:                        vect[0] = np;
                    624:                        sink(1, vect);
                    625:                        if (setimes) {
                    626:                                setimes = 0;
1.49.36.1! martin    627:                                (void) utimes(np, tv);
1.1       cgd       628:                        }
1.9       cgd       629:                        if (mod_flag)
                    630:                                (void)chmod(np, mode);
1.1       cgd       631:                        continue;
                    632:                }
1.9       cgd       633:                omode = mode;
                    634:                mode |= S_IWRITE;
1.1       cgd       635:                if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1.9       cgd       636: bad:                   run_err("%s: %s", np, strerror(errno));
1.1       cgd       637:                        continue;
                    638:                }
                    639:                (void)write(rem, "", 1);
1.9       cgd       640:                if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
1.1       cgd       641:                        (void)close(ofd);
                    642:                        continue;
                    643:                }
                    644:                cp = bp->buf;
1.49.36.1! martin    645:                wrerr = 0;
        !           646:
        !           647: /*
        !           648:  * Like run_err(), but don't send any message to the remote end.
        !           649:  * Instead, record the first error and send that in the end.
        !           650:  */
        !           651: #define RUN_ERR(w_context) do { \
        !           652:        if (!wrerr) {                                                   \
        !           653:                wrerrno = errno;                                        \
        !           654:                wrcontext = w_context;                                  \
        !           655:                wrerr = 1;                                              \
        !           656:        }                                                               \
        !           657: } while(0)
        !           658:
1.44      christos  659:                count = 0;
                    660:                for (i = 0; i < size; i += BUFSIZ) {
1.1       cgd       661:                        amt = BUFSIZ;
                    662:                        if (i + amt > size)
                    663:                                amt = size - i;
                    664:                        count += amt;
                    665:                        do {
1.44      christos  666:                                j = read(rem, cp, (size_t)amt);
                    667:                                if (j == -1) {
1.9       cgd       668:                                        run_err("%s", j ? strerror(errno) :
1.1       cgd       669:                                            "dropped connection");
                    670:                                        exit(1);
                    671:                                }
                    672:                                amt -= j;
                    673:                                cp += j;
                    674:                        } while (amt > 0);
                    675:                        if (count == bp->cnt) {
1.9       cgd       676:                                /* Keep reading so we stay sync'd up. */
1.49.36.1! martin    677:                                if (!wrerr) {
1.44      christos  678:                                        j = write(ofd, bp->buf, (size_t)count);
1.9       cgd       679:                                        if (j != count) {
1.49.36.1! martin    680:                                                if (j >= 0)
        !           681:                                                        errno = EIO;
        !           682:                                                RUN_ERR("write");
1.9       cgd       683:                                        }
                    684:                                }
1.1       cgd       685:                                count = 0;
                    686:                                cp = bp->buf;
                    687:                        }
                    688:                }
1.49.36.1! martin    689:                if (count != 0 && !wrerr &&
1.44      christos  690:                    (j = write(ofd, bp->buf, (size_t)count)) != count) {
1.49.36.1! martin    691:                        if (j >= 0)
        !           692:                                errno = EIO;
        !           693:                        RUN_ERR("write");
1.1       cgd       694:                }
1.49.36.1! martin    695:                if (ftruncate(ofd, size))
        !           696:                        RUN_ERR("truncate");
        !           697:
1.9       cgd       698:                if (pflag) {
                    699:                        if (exists || omode != mode)
                    700:                                if (fchmod(ofd, omode))
1.49.36.1! martin    701:                                        RUN_ERR("set mode");
1.9       cgd       702:                } else {
                    703:                        if (!exists && omode != mode)
                    704:                                if (fchmod(ofd, omode & ~mask))
1.49.36.1! martin    705:                                        RUN_ERR("set mode");
1.9       cgd       706:                }
1.23      christos  707: #ifndef __SVR4
1.49.36.1! martin    708:                if (setimes && !wrerr) {
1.1       cgd       709:                        setimes = 0;
1.49.36.1! martin    710:                        if (futimes(ofd, tv) < 0)
        !           711:                                RUN_ERR("set times");
1.1       cgd       712:                }
1.23      christos  713: #endif
1.18      mycroft   714:                (void)close(ofd);
1.23      christos  715: #ifdef __SVR4
1.49.36.1! martin    716:                if (setimes && !wrerr) {
1.23      christos  717:                        setimes = 0;
1.49.36.1! martin    718:                        if (utimes(np, tv) < 0)
        !           719:                                RUN_ERR("set times");
1.23      christos  720:                }
                    721: #endif
1.18      mycroft   722:                (void)response();
1.49.36.1! martin    723:                if (wrerr)
        !           724:                        run_err("%s: %s: %s", np, wrcontext, strerror(wrerrno));
        !           725:                else
1.1       cgd       726:                        (void)write(rem, "", 1);
                    727:        }
1.49      chs       728:
                    729: out:
                    730:        if (namebuf) {
                    731:                free(namebuf);
                    732:        }
                    733:        return;
                    734:
1.1       cgd       735: screwup:
1.9       cgd       736:        run_err("protocol error: %s", why);
1.1       cgd       737:        exit(1);
1.21      mycroft   738:        /* NOTREACHED */
1.1       cgd       739: }
                    740:
                    741:
1.9       cgd       742: int
1.36      xtraeme   743: response(void)
1.1       cgd       744: {
1.9       cgd       745:        char ch, *cp, resp, rbuf[BUFSIZ];
                    746:
                    747:        if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
                    748:                lostconn(0);
1.1       cgd       749:
1.9       cgd       750:        cp = rbuf;
                    751:        switch(resp) {
                    752:        case 0:                         /* ok */
                    753:                return (0);
                    754:        default:
                    755:                *cp++ = resp;
                    756:                /* FALLTHROUGH */
                    757:        case 1:                         /* error, followed by error msg */
                    758:        case 2:                         /* fatal error, "" */
                    759:                do {
                    760:                        if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
                    761:                                lostconn(0);
                    762:                        *cp++ = ch;
                    763:                } while (cp < &rbuf[BUFSIZ] && ch != '\n');
1.1       cgd       764:
1.9       cgd       765:                if (!iamremote)
1.44      christos  766:                        (void)write(STDERR_FILENO, rbuf, (size_t)(cp - rbuf));
1.9       cgd       767:                ++errs;
                    768:                if (resp == 1)
                    769:                        return (-1);
                    770:                exit(1);
                    771:        }
                    772:        /* NOTREACHED */
1.1       cgd       773: }
                    774:
1.6       cgd       775: void
1.36      xtraeme   776: usage(void)
1.1       cgd       777: {
                    778:        (void)fprintf(stderr,
1.38      wiz       779:            "usage: rcp [-46p] f1 f2; or: rcp [-46pr] f1 ... fn directory\n");
1.1       cgd       780:        exit(1);
1.21      mycroft   781:        /* NOTREACHED */
1.1       cgd       782: }
                    783:
1.9       cgd       784: #include <stdarg.h>
                    785:
1.1       cgd       786:
1.9       cgd       787: void
                    788: run_err(const char *fmt, ...)
1.1       cgd       789: {
1.9       cgd       790:        static FILE *fp;
                    791:        va_list ap;
1.28      wiz       792:
                    793:        ++errs;
                    794:        if (fp == NULL && !(fp = fdopen(rem, "w")))
                    795:                return;
                    796:
1.9       cgd       797:        va_start(ap, fmt);
1.1       cgd       798:
1.9       cgd       799:        (void)fprintf(fp, "%c", 0x01);
                    800:        (void)fprintf(fp, "rcp: ");
                    801:        (void)vfprintf(fp, fmt, ap);
                    802:        (void)fprintf(fp, "\n");
                    803:        (void)fflush(fp);
1.28      wiz       804:        va_end(ap);
1.1       cgd       805:
1.28      wiz       806:        if (!iamremote) {
                    807:                va_start(ap, fmt);
1.9       cgd       808:                vwarnx(fmt, ap);
1.28      wiz       809:                va_end(ap);
                    810:        }
1.1       cgd       811: }

CVSweb <webmaster@jp.NetBSD.org>