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

Annotation of src/usr.bin/rdist/client.c, Revision 1.3

1.3     ! deraadt     1: /*     $OpenBSD$       */
        !             2:
1.1       dm          3: /*
                      4:  * Copyright (c) 1983 Regents of the University of California.
                      5:  * All rights reserved.
                      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.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char RCSid[] =
1.3     ! deraadt    38: "$OpenBSD: client.c,v 1.2 1996/03/05 03:15:57 dm Exp $";
1.1       dm         39:
                     40: static char sccsid[] = "@(#)client.c";
                     41:
                     42: static char copyright[] =
                     43: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
                     44:  All rights reserved.\n";
                     45: #endif /* not lint */
                     46:
                     47: /*
                     48:  * Routines used in client mode to communicate with remove server.
                     49:  */
                     50:
                     51: #include "defs.h"
                     52: #include "y.tab.h"
                     53:
                     54: /*
                     55:  * Update status
                     56:  */
                     57: #define US_NOTHING     0       /* No update needed */
                     58: #define US_NOENT       1       /* Entry does not exist */
                     59: #define US_OUTDATE     2       /* Entry is out of date */
                     60: #define US_DOCOMP      3       /* Do a binary comparison */
                     61: #define US_MODE                4       /* Modes of file differ */
                     62:
                     63: struct linkbuf *ihead = NULL;  /* list of files with more than one link */
                     64: char   buf[BUFSIZ];            /* general purpose buffer */
                     65: u_char respbuff[BUFSIZ];       /* Response buffer */
                     66: char   target[BUFSIZ];         /* target/source directory name */
                     67: char   source[BUFSIZ];         /* source directory name */
                     68: char   *ptarget;               /* pointer to end of target name */
                     69: char   *Tdest;                 /* pointer to last T dest*/
                     70: struct namelist        *updfilelist = NULL; /* List of updated files */
                     71:
                     72: static int sendit();
                     73:
                     74: /*
                     75:  * return remote file pathname (relative from target)
                     76:  */
                     77: char *remfilename(src, dest, path, rname, destdir)
                     78:        char *src, *dest, *path, *rname;
                     79:        int destdir;
                     80: {
                     81:        extern struct namelist *filelist;
                     82:        register char *lname, *cp;
                     83:        static char buff[BUFSIZ];
                     84:        int srclen, pathlen;
                     85:        char *p;
                     86:
                     87:
                     88:        debugmsg(DM_MISC,
                     89:                 "remfilename: src=%s dest=%s path=%s rname=%s destdir=%d\n",
                     90:                A(src), A(dest), A(path), A(rname), destdir);
                     91:
                     92:        if (!dest) {
                     93:                debugmsg(DM_MISC, "remfilename: remote filename=%s\n", path);
                     94:                return(path);
                     95:        }
                     96:
                     97:        if (!destdir) {
                     98:                debugmsg(DM_MISC, "remfilename: remote filename=%s\n", dest);
                     99:                return(dest);
                    100:        }
                    101:
                    102:        buff[0] = CNULL;
                    103:        lname = buff;
                    104:        if (path && *path) {
                    105:                cp = strrchr(path, '/');
                    106:                if (cp == NULL)
                    107:                        (void) sprintf(buff, "%s/%s", dest, path);
                    108:                else {
                    109:                        srclen = strlen(src);
                    110:                        pathlen = strlen(path);
                    111:                        if (srclen >= pathlen)
                    112:                                cp++; /* xbasename(path) */
                    113:                        else {
                    114:                                if (filelist && filelist->n_next == NULL)
                    115:                                        /* path relative to src */
                    116:                                        cp = path + srclen;
                    117:                                else {
                    118:                                        if ((p = strrchr(src, '/')))
                    119:                                                cp = path + srclen - strlen(p);
                    120:                                        else
                    121:                                                cp = path;
                    122:                                }
                    123:                        }
                    124:                        if ((*cp != '/') && *cp)
                    125:                                (void) sprintf(buff, "%s/%s", dest, cp);
                    126:                        else
                    127:                                (void) sprintf(buff, "%s%s", dest, cp);
                    128:                }
                    129:        } else
                    130:                strcpy(lname, dest);
                    131:
                    132:        debugmsg(DM_MISC, "remfilename: remote filename=%s\n", lname);
                    133:
                    134:        return(lname);
                    135: }
                    136:
                    137: /*
                    138:  * Return true if name is in the list.
                    139:  */
                    140: int inlist(list, file)
                    141:        struct namelist *list;
                    142:        char *file;
                    143: {
                    144:        register struct namelist *nl;
                    145:
                    146:        for (nl = list; nl != NULL; nl = nl->n_next)
                    147:                if (strcmp(file, nl->n_name) == 0)
                    148:                        return(1);
                    149:        return(0);
                    150: }
                    151:
                    152: /*
                    153:  * Run any special commands for this file
                    154:  */
                    155: static void runspecial(starget, opts, rname, destdir)
                    156:        char *starget;
                    157:        opt_t opts;
                    158:        char *rname;
                    159:        int destdir;
                    160: {
                    161:        register struct subcmd *sc;
                    162:        extern struct subcmd *subcmds;
                    163:        char *rfile;
                    164:
                    165:        rfile = remfilename(source, Tdest, target, rname, destdir);
                    166:
                    167:        for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
                    168:                if (sc->sc_type != SPECIAL)
                    169:                        continue;
                    170:                if (sc->sc_args != NULL && !inlist(sc->sc_args, starget))
                    171:                        continue;
                    172:                message(MT_CHANGE, "special \"%s\"", sc->sc_name);
                    173:                if (IS_ON(opts, DO_VERIFY))
                    174:                        continue;
                    175:                (void) sendcmd(C_SPECIAL,
                    176:                        "%s=%s;%s=%s;%s=%s;export %s %s %s;%s",
                    177:                        E_LOCFILE, starget,
                    178:                        E_REMFILE, rfile,
                    179:                        E_BASEFILE, xbasename(rfile),
                    180:                        E_LOCFILE, E_REMFILE, E_BASEFILE,
                    181:                        sc->sc_name);
                    182:                while (response() > 0)
                    183:                        ;
                    184:        }
                    185: }
                    186:
                    187: /*
                    188:  * If we're doing a target with a "cmdspecial" in it, then
                    189:  * save the name of the file being updated for use with "cmdspecial".
                    190:  */
                    191: static void addcmdspecialfile(starget, rname, destdir)
                    192:        char *starget;
                    193:        char *rname;
                    194:        int destdir;
                    195: {
                    196:        char *rfile;
                    197:        struct namelist *new;
                    198:        register struct subcmd *sc;
                    199:        extern struct subcmd *subcmds;
                    200:        int isokay = 0;
                    201:
                    202:        rfile = remfilename(source, Tdest, target, rname, destdir);
                    203:
                    204:        for (sc = subcmds; sc != NULL && !isokay; sc = sc->sc_next) {
                    205:                if (sc->sc_type != CMDSPECIAL)
                    206:                        continue;
                    207:                if (sc->sc_args != NULL && !inlist(sc->sc_args, starget))
                    208:                        continue;
                    209:                isokay = TRUE;
                    210:        }
                    211:
                    212:        if (isokay) {
                    213:                new = (struct namelist *) xmalloc(sizeof(struct namelist));
                    214:                new->n_name = strdup(rfile);
                    215:                new->n_next = updfilelist;
                    216:                updfilelist = new;
                    217:        }
                    218: }
                    219:
                    220: /*
                    221:  * Free the file list
                    222:  */
                    223: static void freecmdspecialfiles()
                    224: {
                    225:        register struct namelist *ptr, *save;
                    226:
                    227:        for (ptr = updfilelist; ptr; ) {
                    228:                if (ptr->n_name) (void) free(ptr->n_name);
                    229:                save = ptr->n_next;
                    230:                (void) free(ptr);
                    231:                if (save)
                    232:                        ptr = save->n_next;
                    233:                else
                    234:                        ptr = NULL;
                    235:        }
                    236:        updfilelist = NULL;
                    237: }
                    238:
                    239: /*
                    240:  * Run commands for an entire cmd
                    241:  */
                    242: extern void runcmdspecial(cmd, filev, opts)
                    243:        struct cmd *cmd;
                    244:        char **filev;
                    245:        opt_t opts;
                    246: {
                    247:        register struct subcmd *sc;
                    248:        register struct namelist *f;
                    249:        register char **cpp;
                    250:        char *file;
                    251:        int first = TRUE;
                    252:
                    253:        for (sc = cmd->c_cmds; sc != NULL; sc = sc->sc_next) {
                    254:                if (sc->sc_type != CMDSPECIAL)
                    255:                        continue;
                    256:                message(MT_CHANGE, "cmdspecial \"%s\"", sc->sc_name);
                    257:                if (IS_ON(opts, DO_VERIFY))
                    258:                        continue;
                    259:                /* Send all the file names */
                    260:                for (f = updfilelist; f != NULL; f = f->n_next) {
                    261:                        if (first) {
                    262:                                (void) sendcmd(C_CMDSPECIAL, NULL);
                    263:                                if (response() < 0)
                    264:                                        return;
                    265:                                first = FALSE;
                    266:                        }
                    267:                        (void) sendcmd(RC_FILE, f->n_name);
                    268:                        if (response() < 0)
                    269:                                return;
                    270:                }
                    271:                if (first) {
                    272:                        (void) sendcmd(C_CMDSPECIAL, NULL);
                    273:                        if (response() < 0)
                    274:                                return;
                    275:                        first = FALSE;
                    276:                }
                    277:                /* Send command to run and wait for it to complete */
                    278:                (void) sendcmd(RC_COMMAND, sc->sc_name);
                    279:                while (response() > 0)
                    280:                        ;
                    281:                first = TRUE;   /* Reset in case there are more CMDSPECIAL's */
                    282:        }
                    283:        freecmdspecialfiles();
                    284: }
                    285:
                    286: /*
                    287:  * For security, reject filenames that contains a newline
                    288:  */
                    289: int checkfilename(name)
                    290:        char *name;
                    291: {
                    292:        register char *cp;
                    293:
                    294:        if (strchr(name, '\n')) {
                    295:                for (cp = name; *cp; cp++)
                    296:                        if (*cp == '\n')
                    297:                                *cp = '?';
                    298:                message(MT_NERROR,
                    299:                        "Refuse to handle filename containing newline: %s",
                    300:                        name);
                    301:                return(-1);
                    302:        }
                    303:
                    304:        return(0);
                    305: }
                    306:
                    307: /*
                    308:  * Save and retrieve hard link info
                    309:  */
                    310: static struct linkbuf *linkinfo(statp)
                    311:        struct stat *statp;
                    312: {
                    313:        struct linkbuf *lp;
                    314:
                    315:        for (lp = ihead; lp != NULL; lp = lp->nextp)
                    316:                if (lp->inum == statp->st_ino && lp->devnum == statp->st_dev) {
                    317:                        lp->count--;
                    318:                        return(lp);
                    319:                }
                    320:
                    321:        lp = (struct linkbuf *) xmalloc(sizeof(*lp));
                    322:        lp->nextp = ihead;
                    323:        ihead = lp;
                    324:        lp->inum = statp->st_ino;
                    325:        lp->devnum = statp->st_dev;
                    326:        lp->count = statp->st_nlink - 1;
                    327:        (void) strcpy(lp->pathname, target);
                    328:        (void) strcpy(lp->src, source);
                    329:        if (Tdest)
                    330:                (void) strcpy(lp->target, Tdest);
                    331:        else
                    332:                *lp->target = CNULL;
                    333:
                    334:        return((struct linkbuf *) NULL);
                    335: }
                    336:
                    337: /*
                    338:  * Send a hardlink
                    339:  */
                    340: static int sendhardlink(opts, lp, rname, destdir)
                    341:        opt_t opts;
                    342:        struct linkbuf *lp;
                    343:        char *rname;
                    344:        int destdir;
                    345: {
                    346:        static char buff[MAXPATHLEN];
                    347:        char *lname;    /* name of file to link to */
                    348:
                    349:        debugmsg(DM_MISC,
                    350:               "sendhardlink: rname='%s' pathname='%s' src='%s' target='%s'\n",
                    351:                 rname, lp->pathname, lp->src, lp->target);
                    352:
                    353:        if (*lp->target == CNULL)
                    354:                (void) sendcmd(C_RECVHARDLINK, "%o %s %s",
                    355:                               opts, lp->pathname, rname);
                    356:        else {
                    357:                lname = buff;
                    358:                strcpy(lname, remfilename(lp->src, lp->target,
                    359:                                          lp->pathname, rname,
                    360:                                          destdir));
                    361:                debugmsg(DM_MISC, "sendhardlink: lname=%s\n", lname);
                    362:                (void) sendcmd(C_RECVHARDLINK, "%o %s %s",
                    363:                               opts, lname, rname);
                    364:        }
                    365:
                    366:        return(response());
                    367: }
                    368:
                    369: /*
                    370:  * Send a file
                    371:  */
                    372: static int sendfile(rname, opts, stb, user, group, destdir)
                    373:        char *rname;
                    374:        opt_t opts;
                    375:        struct stat *stb;
                    376:        char *user, *group;
                    377:        int destdir;
                    378: {
                    379:        int goterr, f;
                    380:        off_t i;
                    381:
                    382:        if (stb->st_nlink > 1) {
                    383:                struct linkbuf *lp;
                    384:
                    385:                if ((lp = linkinfo(stb)) != NULL)
                    386:                        return(sendhardlink(opts, lp, rname, destdir));
                    387:        }
                    388:
                    389:        if ((f = open(target, O_RDONLY)) < 0) {
                    390:                error("%s: open for read failed: %s", target, SYSERR);
                    391:                return(-1);
                    392:        }
                    393:
                    394:        /*
                    395:         * Send file info
                    396:         */
                    397:        (void) sendcmd(C_RECVREG, "%o %04o %ld %ld %ld %s %s %s",
                    398:                       opts, stb->st_mode & 07777,
                    399:                       (long) stb->st_size,
                    400:                       stb->st_mtime, stb->st_atime,
                    401:                       user, group, rname);
                    402:        if (response() < 0) {
                    403:                (void) close(f);
                    404:                return(-1);
                    405:        }
                    406:
                    407:        debugmsg(DM_MISC, "Send file '%s' %d bytes\n",
                    408:                 rname, (long) stb->st_size);
                    409:
                    410:        /*
                    411:         * Set remote time out alarm handler.
                    412:         */
                    413:        (void) signal(SIGALRM, sighandler);
                    414:
                    415:        /*
                    416:         * Actually transfer the file
                    417:         */
                    418:        goterr = 0;
                    419:        for (i = 0; i < stb->st_size; i += BUFSIZ) {
                    420:                int amt = BUFSIZ;
                    421:
                    422:                (void) alarm(rtimeout);
                    423:                if (i + amt > stb->st_size)
                    424:                        amt = stb->st_size - i;
                    425:                if (read(f, buf, amt) != amt) {
                    426:                        error("%s: File changed size", target);
                    427:                        err();
                    428:                        ++goterr;
                    429:                        /*
                    430:                         * XXX - We have to keep going because the
                    431:                         * server expects to receive a fixed number
                    432:                         * of bytes that we specified as the file size.
                    433:                         * We need Out Of Band communication to handle
                    434:                         * this situation gracefully.
                    435:                         */
                    436:                }
                    437:                if (xwrite(rem_w, buf, amt) < 0) {
                    438:                        error("%s: Error writing to client: %s",
                    439:                              target, SYSERR);
                    440:                        err();
                    441:                        ++goterr;
                    442:                        break;
                    443:                }
                    444:                (void) alarm(0);
                    445:        }
                    446:
                    447:        (void) alarm(0);        /* Insure alarm is off */
                    448:        (void) close(f);
                    449:
                    450:        debugmsg(DM_MISC, "Send file '%s' %s.\n",
                    451:                 (goterr) ? "failed" : "complete", rname);
                    452:
                    453:        /*
                    454:         * Check for errors and end send
                    455:         */
                    456:        if (goterr)
                    457:                return(-1);
                    458:        else {
                    459:                ack();
                    460:                f = response();
                    461:                if (f < 0)
                    462:                        return(-1);
                    463:                else if (f == 0 && IS_ON(opts, DO_COMPARE))
                    464:                        return(0);
                    465:
                    466:                runspecial(target, opts, rname, destdir);
                    467:                addcmdspecialfile(target, rname, destdir);
                    468:
                    469:                return(0);
                    470:        }
                    471: }
                    472:
                    473: /*
                    474:  * Check for files on the machine being updated that are not on the master
                    475:  * machine and remove them.
                    476:  *
                    477:  * Return < 0 on error.
                    478:  * Return 0 if nothing happened.
                    479:  * Return > 0 if anything is updated.
                    480:  */
                    481: static int rmchk(opts)
                    482:        opt_t opts;
                    483: {
                    484:        register u_char *s;
                    485:        struct stat stb;
                    486:        int didupdate = 0;
                    487:        int n;
                    488:
                    489:        debugmsg(DM_CALL, "rmchk()\n");
                    490:
                    491:        /*
                    492:         * Tell the remote to clean the files from the last directory sent.
                    493:         */
                    494:        (void) sendcmd(C_CLEAN, "%o", IS_ON(opts, DO_VERIFY));
                    495:        if (response() < 0)
                    496:                return(-1);
                    497:
                    498:        for ( ; ; ) {
                    499:                n = remline(s = respbuff, sizeof(respbuff), TRUE);
                    500:                if (n <= 0) {
                    501:                        error("rmchk: unexpected control record");
                    502:                        return(didupdate);
                    503:                }
                    504:
                    505:                switch (*s++) {
                    506:                case CC_QUERY: /* Query if file should be removed */
                    507:                        /*
                    508:                         * Return the following codes to remove query.
                    509:                         * CC_NO -- file exists - DON'T remove.
                    510:                         * CC_YES -- file doesn't exist - REMOVE.
                    511:                         */
                    512:                        (void) sprintf(ptarget, "%s%s",
                    513:                                       (ptarget[-1] == '/' ? "" : "/"), s);
                    514:                        debugmsg(DM_MISC, "check %s\n", target);
                    515:                        if (except(target))
                    516:                                (void) sendcmd(CC_NO, NULL);
                    517:                        else if (lstat(target, &stb) < 0) {
                    518:                                if (sendcmd(CC_YES, NULL) == 0)
                    519:                                        didupdate = 1;
                    520:                        } else
                    521:                                (void) sendcmd(CC_NO, NULL);
                    522:                        break;
                    523:
                    524:                case CC_END:
                    525:                        *ptarget = CNULL;
                    526:                        ack();
                    527:                        return(didupdate);
                    528:
                    529:                case C_LOGMSG:
                    530:                        if (n > 0)
                    531:                                message(MT_INFO, "%s", s);
                    532:                        break;
                    533:
                    534:                case C_ERRMSG:
                    535:                        message(MT_NERROR, "%s", s);
                    536:                        return(didupdate);
                    537:
                    538:                case C_FERRMSG:
                    539:                        message(MT_FERROR, "%s", s);
                    540:                        finish();
                    541:
                    542:                default:
                    543:                        error("rmchk: unexpected response '%s'", respbuff);
                    544:                        err();
                    545:                }
                    546:        }
                    547:        /*NOTREACHED*/
                    548: }
                    549:
                    550: /*
                    551:  * Send a directory
                    552:  *
                    553:  * Return < 0 on error.
                    554:  * Return 0 if nothing happened.
                    555:  * Return > 0 if anything is updated.
                    556:  */
                    557: static int senddir(rname, opts, stb, user, group, destdir)
                    558:        char *rname;
                    559:        opt_t opts;
                    560:        struct stat *stb;
                    561:        char *user, *group;
                    562:        int destdir;
                    563: {
                    564:        DIRENTRY *dp;
                    565:        DIR *d;
                    566:        char *optarget, *cp;
                    567:        int len;
                    568:        int didupdate = 0;
                    569:
                    570:        /*
1.2       dm        571:         * Don't descend into directory
                    572:         */
                    573:        if (IS_ON(opts, DO_NODESCEND))
                    574:                return(0);
                    575:
                    576:        if ((d = opendir(target)) == NULL) {
                    577:                error("%s: opendir failed: %s", target, SYSERR);
                    578:                return(-1);
                    579:        }
                    580:
                    581:        /*
1.1       dm        582:         * Send recvdir command in recvit() format.
                    583:         */
                    584:        (void) sendcmd(C_RECVDIR, "%o %04o 0 0 0 %s %s %s",
                    585:                       opts, stb->st_mode & 07777, user, group, rname);
                    586:        if (response() < 0)
                    587:                return(-1);
                    588:
                    589:        if (IS_ON(opts, DO_REMOVE))
                    590:                if (rmchk(opts) > 0)
                    591:                        ++didupdate;
                    592:
                    593:        optarget = ptarget;
                    594:        len = ptarget - target;
                    595:        while (dp = readdir(d)) {
                    596:                if (!strcmp(dp->d_name, ".") ||
                    597:                    !strcmp(dp->d_name, ".."))
                    598:                        continue;
                    599:                if (len + 1 + (int) strlen(dp->d_name) >= MAXPATHLEN - 1) {
                    600:                        error("%s/%s: Name too long", target,
                    601:                              dp->d_name);
                    602:                        continue;
                    603:                }
                    604:                ptarget = optarget;
                    605:                if (ptarget[-1] != '/')
                    606:                        *ptarget++ = '/';
                    607:                cp = dp->d_name;
                    608:                while (*ptarget++ = *cp++)
                    609:                        ;
                    610:                ptarget--;
                    611:                if (sendit(dp->d_name, opts, destdir) > 0)
                    612:                        didupdate = 1;
                    613:        }
                    614:        (void) closedir(d);
                    615:
                    616:        (void) sendcmd(C_END, NULL);
                    617:        (void) response();
                    618:
                    619:        ptarget = optarget;
                    620:        *ptarget = CNULL;
                    621:
                    622:        return(didupdate);
                    623: }
                    624:
                    625: /*
                    626:  * Send a link
                    627:  */
                    628: static int sendlink(rname, opts, stb, user, group, destdir)
                    629:        char *rname;
                    630:        opt_t opts;
                    631:        struct stat *stb;
                    632:        char *user;
                    633:        char *group;
                    634:        int destdir;
                    635: {
                    636:        int sizerr, f, n;
                    637:        static char tbuf[BUFSIZ];
                    638:        char lbuf[MAXPATHLEN];
                    639:        u_char *s;
                    640:
                    641:        debugmsg(DM_CALL, "sendlink(%s, %x, stb, %d)\n", rname, opts, destdir);
                    642:
                    643:        if (stb->st_nlink > 1) {
                    644:                struct linkbuf *lp;
                    645:
                    646:                if ((lp = linkinfo(stb)) != NULL)
                    647:                        return(sendhardlink(opts, lp, rname, destdir));
                    648:        }
                    649:
                    650:        /*
                    651:         * Gather and send basic link info
                    652:         */
                    653:        (void) sendcmd(C_RECVSYMLINK, "%o %04o %ld %ld %ld %s %s %s",
                    654:                       opts, stb->st_mode & 07777,
                    655:                       (long) stb->st_size,
                    656:                       stb->st_mtime, stb->st_atime,
                    657:                       user, group, rname);
                    658:        if (response() < 0)
                    659:                return(-1);
                    660:
                    661:        /*
                    662:         * Gather and send additional link info
                    663:         */
                    664:        sizerr = (readlink(target, lbuf, sizeof(lbuf)) != stb->st_size);
                    665:        (void) sprintf(tbuf, "%.*s", (int) stb->st_size, lbuf);
                    666:        (void) sendcmd(C_NONE, "%s\n", tbuf);
                    667:
                    668:        if (sizerr) {
                    669:                error("%s: file changed size", target);
                    670:                err();
                    671:        } else
                    672:                ack();
                    673:
                    674:        /*
                    675:         * Check response
                    676:         */
                    677:        f = response();
                    678:        if (f < 0)
                    679:                return(-1);
                    680:        else if (f == 0 && IS_ON(opts, DO_COMPARE))
                    681:                return(0);
                    682:
                    683:        /*
                    684:         * Read and process responses from server.
                    685:         * The server may send multiple messages regarding
                    686:         * file deletes if the remote target is a directory.
                    687:         */
                    688:        for (;;) {
                    689:                n = remline(s = respbuff, sizeof(respbuff), TRUE);
                    690:                if (n == -1)    /* normal EOF */
                    691:                        return(0);
                    692:                if (n == 0) {
                    693:                        error("expected control record");
                    694:                        continue;
                    695:                }
                    696:
                    697:                switch (*s++) {
                    698:                case C_END:     /* End of send operation */
                    699:                        *ptarget = CNULL;
                    700:                        ack();
                    701:                        runspecial(target, opts, rname, destdir);
                    702:                        addcmdspecialfile(target, rname, destdir);
                    703:                        return(0);
                    704:
                    705:                case C_LOGMSG:
                    706:                        if (n > 0)
                    707:                                message(MT_INFO, "%s", s);
                    708:                        break;
                    709:
                    710:                case C_ERRMSG:
                    711:                        message(MT_NERROR, "%s", s);
                    712:                        return(-1);
                    713:
                    714:                case C_FERRMSG:
                    715:                        message(MT_FERROR, "%s", s);
                    716:                        finish();
                    717:
                    718:                default:
                    719:                        error("install link: unexpected response '%s'",
                    720:                              respbuff);
                    721:                        err();
                    722:                }
                    723:        }
                    724:        /*NOTREACHED*/
                    725: }
                    726:
                    727: /*
                    728:  * Check to see if file needs to be updated on the remote machine.
                    729:  * Returns:
                    730:  *     US_NOTHING      - no update
                    731:  *     US_NOENT        - remote doesn't exist
                    732:  *     US_OUTDATE      - out of date
                    733:  *     US_DOCOMP       - comparing binaries to determine if out of date
                    734:  *     US_MODE         - File modes do not match
                    735:  */
                    736: static int update(rname, opts, statp)
                    737:        char *rname;
                    738:        opt_t opts;
                    739:        struct stat *statp;
                    740: {
                    741:        register off_t size;
                    742:        register time_t mtime;
                    743:        unsigned short lmode;
                    744:        unsigned short rmode;
                    745:        char *owner = NULL, *group = NULL;
                    746:        int done, n;
                    747:        u_char *cp;
                    748:
                    749:        debugmsg(DM_CALL, "update(%s, 0x%x, 0x%x)\n", rname, opts, statp);
                    750:
                    751:        if (IS_ON(opts, DO_NOEXEC))
                    752:                if (isexec(target, statp)) {
                    753:                        debugmsg(DM_MISC, "%s is an executable\n", target);
                    754:                        return(US_NOTHING);
                    755:                }
                    756:
                    757:        /*
                    758:         * Check to see if the file exists on the remote machine.
                    759:         */
                    760:        (void) sendcmd(C_QUERY, "%s", rname);
                    761:
                    762:        for (done = 0; !done;) {
                    763:                n = remline(cp = respbuff, sizeof(respbuff), TRUE);
                    764:                if (n <= 0) {
                    765:                        error("update: unexpected control record in response to query");
                    766:                        return(US_NOTHING);
                    767:                }
                    768:
                    769:                switch (*cp++) {
                    770:                case QC_ONNFS:  /* Resides on a NFS */
                    771:                        debugmsg(DM_MISC,
                    772:                                 "update: %s is on a NFS.  Skipping...\n",
                    773:                                 rname);
                    774:                        return(US_NOTHING);
                    775:
                    776:                case QC_SYM:  /* Is a symbolic link */
                    777:                        debugmsg(DM_MISC,
                    778:                                 "update: %s is a symlink.  Skipping...\n",
                    779:                                 rname);
                    780:                        return(US_NOTHING);
                    781:
                    782:                case QC_ONRO:  /* Resides on a Read-Only fs */
                    783:                        debugmsg(DM_MISC,
                    784:                                 "update: %s is on a RO fs.  Skipping...\n",
                    785:                                 rname);
                    786:                        return(US_NOTHING);
                    787:
                    788:                case QC_YES:
                    789:                        done = 1;
                    790:                        break;
                    791:
                    792:                case QC_NO:  /* file doesn't exist so install it */
                    793:                        return(US_NOENT);
                    794:
                    795:                case C_ERRMSG:
                    796:                        if (cp)
                    797:                                message(MT_NERROR, "%s", cp);
                    798:                        return(US_NOTHING);
                    799:
                    800:                case C_FERRMSG:
                    801:                        if (cp)
                    802:                                message(MT_FERROR, "%s", cp);
                    803:                        finish();
                    804:
                    805:                case C_NOTEMSG:
                    806:                        if (cp)
                    807:                                message(MT_NOTICE, "%s", cp);
                    808:                        break;
                    809:                        /* Goto top of loop */
                    810:
                    811:                default:
                    812:                        error("update: unexpected response to query '%s'", cp);
                    813:                        return(US_NOTHING);
                    814:                }
                    815:        }
                    816:
                    817:        /*
                    818:         * Target exists, but no other info passed
                    819:         */
                    820:        if (n <= 1 || !S_ISREG(statp->st_mode))
                    821:                return(US_OUTDATE);
                    822:
                    823:        if (IS_ON(opts, DO_COMPARE))
                    824:                return(US_DOCOMP);
                    825:
                    826:        /*
                    827:         * Parse size
                    828:         */
                    829:        size = strtol(cp, &cp, 10);
                    830:        if (*cp++ != ' ') {
                    831:                error("update: size not delimited");
                    832:                return(US_NOTHING);
                    833:        }
                    834:
                    835:        /*
                    836:         * Parse mtime
                    837:         */
                    838:        mtime = strtol(cp, &cp, 10);
                    839:        if (*cp++ != ' ') {
                    840:                error("update: mtime not delimited");
                    841:                return(US_NOTHING);
                    842:        }
                    843:
                    844:        /*
                    845:         * Parse remote file mode
                    846:         */
                    847:        rmode = strtol(cp, &cp, 8);
                    848:        if (cp && *cp)
                    849:                ++cp;
                    850:
                    851:        /*
                    852:         * Be backwards compatible
                    853:         */
                    854:        if (cp && *cp != CNULL) {
                    855:                /*
                    856:                 * Parse remote file owner
                    857:                 */
                    858:                owner = strtok((char *)cp, " ");
                    859:                if (owner == NULL) {
                    860:                        error("update: owner not delimited");
                    861:                        return(US_NOTHING);
                    862:                }
                    863:
                    864:                /*
                    865:                 * Parse remote file group
                    866:                 */
                    867:                group = strtok((char *) NULL, " ");
                    868:                if (group == NULL) {
                    869:                        error("update: group not delimited");
                    870:                        return(US_NOTHING);
                    871:                }
                    872:        }
                    873:
                    874:        /*
                    875:         * File needs to be updated?
                    876:         */
                    877:        lmode = statp->st_mode & 07777;
                    878:
                    879:        debugmsg(DM_MISC, "update(%s,) local mode %04o remote mode %04o\n",
                    880:                 rname, lmode, rmode);
                    881:        debugmsg(DM_MISC, "update(%s,) size %d mtime %d owner '%s' grp '%s'\n",
                    882:                 rname, (int) size, mtime, owner, group);
                    883:
                    884:        if (statp->st_mtime != mtime) {
                    885:                if (statp->st_mtime < mtime && IS_ON(opts, DO_YOUNGER)) {
                    886:                        message(MT_WARNING,
                    887:                                "%s: Warning: remote copy is newer",
                    888:                                target);
                    889:                        return(US_NOTHING);
                    890:                }
                    891:                return(US_OUTDATE);
                    892:        }
                    893:
                    894:        /*
                    895:         * If the mode of a file does not match the local mode, the
                    896:         * whole file is updated.  This is done both to insure that
                    897:         * a bogus version of the file has not been installed and to
                    898:         * avoid having to handle weird cases of chmod'ing symlinks
                    899:         * and such.
                    900:         */
                    901:        if (!IS_ON(opts, DO_NOCHKMODE) && lmode != rmode) {
                    902:                debugmsg(DM_MISC, "modes do not match (%04o != %04o).\n",
                    903:                         lmode, rmode);
                    904:                return(US_OUTDATE);
                    905:        }
                    906:
                    907:        if (statp->st_size != size) {
                    908:                debugmsg(DM_MISC, "size does not match (%d != %d).\n",
                    909:                         (int) statp->st_size, size);
                    910:                return(US_OUTDATE);
                    911:        }
                    912:
                    913:        /*
                    914:         * Check ownership
                    915:         */
                    916:        if (!IS_ON(opts, DO_NOCHKOWNER) && owner) {
                    917:                if (!IS_ON(opts, DO_NUMCHKOWNER)) {
                    918:                        /* Check by string compare */
                    919:                        if (strcmp(owner, getusername(statp->st_uid,
                    920:                                                      target, opts)) != 0) {
                    921:                                debugmsg(DM_MISC,
                    922:                                         "owner does not match (%s != %s).\n",
                    923:                                         getusername(statp->st_uid,
                    924:                                                     target, opts), owner);
                    925:                                return(US_OUTDATE);
                    926:                        }
                    927:                } else {
                    928:                        /*
                    929:                         * Check numerically.
                    930:                         * Allow negative numbers.
                    931:                         */
                    932:                        while (*owner && !isdigit(*owner) && (*owner != '-'))
                    933:                                ++owner;
                    934:                        if (owner && atoi(owner) != statp->st_uid) {
                    935:                                debugmsg(DM_MISC,
                    936:                                         "owner does not match (%d != %s).\n",
                    937:                                         statp->st_uid, owner);
                    938:                                return(US_OUTDATE);
                    939:                        }
                    940:                }
                    941:        }
                    942:
                    943:        if (!IS_ON(opts, DO_NOCHKGROUP) && group) {
                    944:                if (!IS_ON(opts, DO_NUMCHKGROUP)) {
                    945:                        /* Check by string compare */
                    946:                        if (strcmp(group, getgroupname(statp->st_gid,
                    947:                                                       target, opts)) != 0) {
                    948:                                debugmsg(DM_MISC,
                    949:                                         "group does not match (%s != %s).\n",
                    950:                                         getgroupname(statp->st_gid,
                    951:                                                      target, opts), group);
                    952:                                return(US_OUTDATE);
                    953:                        }
                    954:                } else {
                    955:                        /* Check numerically */
                    956:                        /* Allow negative gid */
                    957:                        while (*group && !isdigit(*group) && (*group != '-'))
                    958:                                ++group;
                    959:                        if (group && atoi(group) != statp->st_gid) {
                    960:                                debugmsg(DM_MISC,
                    961:                                         "group does not match (%d != %s).\n",
                    962:                                         statp->st_gid, group);
                    963:                                return(US_OUTDATE);
                    964:                        }
                    965:                }
                    966:        }
                    967:
                    968:        return(US_NOTHING);
                    969: }
                    970:
                    971: /*
                    972:  * Stat a file
                    973:  */
                    974: static int dostat(file, statbuf, opts)
                    975:        char *file;
                    976:        struct stat *statbuf;
                    977:        opt_t opts;
                    978: {
                    979:        int s;
                    980:
                    981:        if (IS_ON(opts, DO_FOLLOW))
                    982:                s = stat(file, statbuf);
                    983:        else
                    984:                s = lstat(file, statbuf);
                    985:
                    986:        if (s < 0)
                    987:                error("%s: %s failed: %s", file,
                    988:                      IS_ON(opts, DO_FOLLOW) ? "stat" : "lstat", SYSERR);
                    989:        return(s);
                    990: }
                    991:
                    992: /*
                    993:  * Transfer the file or directory in target[].
                    994:  * rname is the name of the file on the remote host.
                    995:  *
                    996:  * Return < 0 on error.
                    997:  * Return 0 if nothing happened.
                    998:  * Return > 0 if anything is updated.
                    999:  */
                   1000: static int sendit(rname, opts, destdir)
                   1001:        char *rname;
                   1002:        opt_t opts;
                   1003:        int destdir;
                   1004: {
                   1005:        static struct stat stb;
                   1006:        extern struct subcmd *subcmds;
                   1007:        char *user, *group;
                   1008:        int u, len;
                   1009:        int didupdate = 0;
                   1010:
                   1011:        /*
                   1012:         * Remove possible accidental newline
                   1013:         */
                   1014:        len = strlen(rname);
                   1015:        if (len > 0 && rname[len-1] == '\n')
                   1016:                rname[len-1] = CNULL;
                   1017:
                   1018:        if (checkfilename(rname) != 0)
                   1019:                return(-1);
                   1020:
                   1021:        debugmsg(DM_CALL, "sendit(%s, 0x%x) called\n", rname, opts);
                   1022:
                   1023:        if (except(target))
                   1024:                return(0);
                   1025:
                   1026:        if (dostat(target, &stb, opts) < 0)
                   1027:                return(-1);
                   1028:
                   1029:        /*
                   1030:         * Does rname need updating?
                   1031:         */
                   1032:        u = update(rname, opts, &stb);
                   1033:        debugmsg(DM_MISC, "sendit(%s, 0x%x): update status of %s is %d\n",
                   1034:                 rname, opts, target, u);
                   1035:
                   1036:        /*
                   1037:         * Don't need to update the file, but we may need to save hardlink
                   1038:         * info.
                   1039:         */
                   1040:        if (u == US_NOTHING) {
                   1041:                if (S_ISREG(stb.st_mode) && stb.st_nlink > 1)
                   1042:                        (void) linkinfo(&stb);
                   1043:                return(0);
                   1044:        }
                   1045:
                   1046:        /*
                   1047:         * File mode needs changing
                   1048:         */
                   1049:        if (u == US_MODE) {
                   1050:                if (IS_ON(opts, DO_VERIFY)) {
                   1051:                        message(MT_INFO, "%s: need to chmod to %04o",
                   1052:                                target, stb.st_mode & 07777);
                   1053:                        runspecial(target, opts, rname, destdir);
                   1054:                        return(1);
                   1055:                }
                   1056:                message(MT_CHANGE, "%s: chmod to %04o",
                   1057:                        target, stb.st_mode & 07777);
                   1058:                (void) sendcmd(C_CHMOD, "%o %04o %s",
                   1059:                               opts, stb.st_mode & 07777, rname);
                   1060:                (void) response();
                   1061:                return(1);
                   1062:        }
                   1063:
                   1064:        user = getusername(stb.st_uid, target, opts);
                   1065:        group = getgroupname(stb.st_gid, target, opts);
                   1066:
                   1067:        /*
                   1068:         * No entry - need to install
                   1069:         */
                   1070:        if (u == US_NOENT) {
                   1071:                if (IS_ON(opts, DO_VERIFY)) {
                   1072:                        message(MT_INFO, "%s: need to install", target);
                   1073:                        runspecial(target, opts, rname, destdir);
                   1074:                        return(1);
                   1075:                }
                   1076:                if (!IS_ON(opts, DO_QUIET))
                   1077:                        message(MT_CHANGE, "%s: installing", target);
                   1078:                FLAG_OFF(opts, (DO_COMPARE|DO_REMOVE));
                   1079:        }
                   1080:
                   1081:        /*
                   1082:         * Handle special file types, including directories and symlinks
                   1083:         */
                   1084:        if (S_ISDIR(stb.st_mode)) {
                   1085:                if (senddir(rname, opts, &stb, user, group, destdir) > 0)
                   1086:                        didupdate = 1;
                   1087:        } else if (S_ISLNK(stb.st_mode)) {
                   1088:                if (u != US_NOENT)
                   1089:                        FLAG_ON(opts, DO_COMPARE);
                   1090:                /*
                   1091:                 * Since we always send link info to the server
                   1092:                 * so the server can determine if the remote link
                   1093:                 * is correct, we never get any acknowledge meant
                   1094:                 * from the server whether the link was really
                   1095:                 * updated or not.
                   1096:                 */
                   1097:                (void) sendlink(rname, opts, &stb, user, group, destdir);
                   1098:        } else if (S_ISREG(stb.st_mode)) {
                   1099:                if (u == US_OUTDATE) {
                   1100:                        if (IS_ON(opts, DO_VERIFY)) {
                   1101:                                message(MT_INFO, "%s: need to update", target);
                   1102:                                runspecial(target, opts, rname, destdir);
                   1103:                                return(1);
                   1104:                        }
                   1105:                        if (!IS_ON(opts, DO_QUIET))
                   1106:                                message(MT_CHANGE, "%s: updating", target);
                   1107:                }
                   1108:                if (sendfile(rname, opts, &stb, user, group, destdir) == 0)
                   1109:                        didupdate = 1;
                   1110:        } else
                   1111:                error("%s: unknown file type", target);
                   1112:
                   1113:        return(didupdate);
                   1114: }
                   1115:
                   1116: /*
                   1117:  * Remove temporary files and do any cleanup operations before exiting.
                   1118:  */
                   1119: extern void cleanup()
                   1120: {
                   1121:        char *file;
                   1122: #ifdef USE_STATDB
                   1123:        extern char statfile[];
                   1124:
                   1125:        (void) unlink(statfile);
                   1126: #endif
                   1127:
                   1128:        if (file = getnotifyfile())
                   1129:                (void) unlink(file);
                   1130: }
                   1131:
                   1132: /*
                   1133:  * Update the file(s) if they are different.
                   1134:  * destdir = 1 if destination should be a directory
                   1135:  * (i.e., more than one source is being copied to the same destination).
                   1136:  *
                   1137:  * Return < 0 on error.
                   1138:  * Return 0 if nothing updated.
                   1139:  * Return > 0 if something was updated.
                   1140:  */
                   1141: extern int install(src, dest, ddir, destdir, opts)
                   1142:        char *src, *dest;
                   1143:        int ddir, destdir;
                   1144:        opt_t opts;
                   1145: {
                   1146:        static char destcopy[MAXPATHLEN];
                   1147:        char *rname;
                   1148:        int didupdate = 0;
                   1149:
                   1150:        debugmsg(DM_CALL,
                   1151:                "install(src=%s,dest=%s,ddir=%d,destdir=%d,opts=%d) start\n",
                   1152:                (src?src:"NULL"), (dest?dest:"NULL"), ddir, destdir, opts);
                   1153:        /*
                   1154:         * Save source name
                   1155:         */
                   1156:        if (IS_ON(opts, DO_WHOLE))
                   1157:                source[0] = CNULL;
                   1158:        else
                   1159:                (void) strcpy(source, src);
                   1160:
                   1161:        if (dest == NULL) {
                   1162:                FLAG_OFF(opts, DO_WHOLE); /* WHOLE only useful if renaming */
                   1163:                dest = src;
                   1164:        }
                   1165:
                   1166:        if (checkfilename(dest) != 0)
                   1167:                return(-1);
                   1168:
                   1169:        if (nflag || debug) {
                   1170:                static char buff[BUFSIZ];
                   1171:                char *cp;
                   1172:
                   1173:                cp = getondistoptlist(opts);
                   1174:                (void) sprintf(buff, "%s%s%s %s %s",
                   1175:                               IS_ON(opts, DO_VERIFY) ? "verify" : "install",
                   1176:                               (cp) ? " -o" : "", (cp) ? cp : "",
                   1177:                               src, dest);
                   1178:                if (nflag) {
                   1179:                        printf("%s\n", buff);
                   1180:                        return(0);
                   1181:                } else
                   1182:                        debugmsg(DM_MISC, "%s\n", buff);
                   1183:        }
                   1184:
                   1185:        rname = exptilde(target, src);
                   1186:        if (rname == NULL)
                   1187:                return(-1);
                   1188:        ptarget = target;
                   1189:        while (*ptarget)
                   1190:                ptarget++;
                   1191:        /*
                   1192:         * If we are renaming a directory and we want to preserve
                   1193:         * the directory heirarchy (-w), we must strip off the leading
                   1194:         * directory name and preserve the rest.
                   1195:         */
                   1196:        if (IS_ON(opts, DO_WHOLE)) {
                   1197:                while (*rname == '/')
                   1198:                        rname++;
                   1199:                ddir = 1;
                   1200:                destdir = 1;
                   1201:        } else {
                   1202:                rname = strrchr(target, '/');
                   1203:                /* Check if no '/' or target ends in '/' */
                   1204:                if (rname == NULL ||
                   1205:                    rname+1 == NULL ||
                   1206:                    *(rname+1) == CNULL)
                   1207:                        rname = target;
                   1208:                else
                   1209:                        rname++;
                   1210:        }
                   1211:
                   1212:        debugmsg(DM_MISC,
                   1213:        "install: target=%s src=%s rname=%s dest='%s' destdir=%d, ddir=%d\n",
                   1214:                 target, source, rname, dest, destdir, ddir);
                   1215:
                   1216:        /*
                   1217:         * Pass the destination file/directory name to remote.
                   1218:         */
                   1219:        if (ddir)
                   1220:                (void) sendcmd(C_DIRTARGET, "%o %s", opts, dest);
                   1221:        else
                   1222:                (void) sendcmd(C_TARGET, "%o %s", opts, dest);
                   1223:        if (response() < 0)
                   1224:                return(-1);
                   1225:
                   1226:        /*
                   1227:         * Save the name of the remote target destination if we are
                   1228:         * in WHOLE mode (destdir > 0) or if the source and destination
                   1229:         * are not the same.  This info will be used later for maintaining
                   1230:         * hardlink info.
                   1231:         */
                   1232:        if (destdir || (src && dest && strcmp(src, dest))) {
                   1233:                (void) strcpy(destcopy, dest);
                   1234:                Tdest = destcopy;
                   1235:        }
                   1236:
                   1237:        didupdate = sendit(rname, opts, destdir);
                   1238:        Tdest = 0;
                   1239:
                   1240:        return(didupdate);
                   1241: }