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

Annotation of src/usr.bin/oldrdist/server.c, Revision 1.9

1.9     ! mickey      1: /*     $OpenBSD: server.c,v 1.8 1997/02/09 19:24:59 deraadt Exp $      */
1.3       deraadt     2:
1.1       dm          3: /*
                      4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  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: /* from: static char sccsid[] = "@(#)server.c  8.1 (Berkeley) 6/9/93"; */
1.9     ! mickey     38: static char *rcsid = "$OpenBSD: server.c,v 1.8 1997/02/09 19:24:59 deraadt Exp $";
1.1       dm         39: #endif /* not lint */
                     40:
                     41: #include <sys/wait.h>
                     42: #include "defs.h"
                     43:
                     44: #define        ack()   (void) write(rem, "\0\n", 2)
                     45: #define        err()   (void) write(rem, "\1\n", 2)
                     46:
                     47: struct linkbuf *ihead;         /* list of files with more than one link */
                     48: char   buf[BUFSIZ];            /* general purpose buffer */
                     49: char   target[BUFSIZ];         /* target/source directory name */
                     50: char   *tp;                    /* pointer to end of target name */
                     51: char   *Tdest;                 /* pointer to last T dest*/
                     52: int    catname;                /* cat name to target name */
                     53: char   *stp[32];               /* stack of saved tp's for directories */
                     54: int    oumask;                 /* old umask for creating files */
                     55:
                     56: extern FILE *lfp;              /* log file for mailing changes */
                     57:
                     58: static int     chkparent __P((char *));
                     59: static void    clean __P((char *));
                     60: static void    comment __P((char *));
                     61: static void    dospecial __P((char *));
                     62: static int     fchog __P((int, char *, char *, char *, int));
                     63: static void    hardlink __P((char *));
                     64: static void    note __P((const char *, ...));
                     65: static void    query __P((char *));
                     66: static void    recvf __P((char *, int));
                     67: static void    removeit __P((struct stat *));
                     68: static int     response __P((void));
                     69: static void    rmchk __P((int));
                     70: static struct linkbuf *
                     71:                    savelink __P((struct stat *));
                     72: static void    sendf __P((char *, int));
                     73: static int     update __P((char *, int, struct stat *));
                     74:
                     75: /*
                     76:  * Server routine to read requests and process them.
                     77:  * Commands are:
                     78:  *     Tname   - Transmit file if out of date
                     79:  *     Vname   - Verify if file out of date or not
                     80:  *     Qname   - Query if file exists. Return mtime & size if it does.
                     81:  */
                     82: void
                     83: server()
                     84: {
                     85:        char cmdbuf[BUFSIZ];
                     86:        register char *cp;
                     87:
                     88:        signal(SIGHUP, cleanup);
                     89:        signal(SIGINT, cleanup);
                     90:        signal(SIGQUIT, cleanup);
                     91:        signal(SIGTERM, cleanup);
                     92:        signal(SIGPIPE, cleanup);
                     93:
                     94:        rem = 0;
                     95:        oumask = umask(0);
1.6       millert    96:        (void) snprintf(buf, sizeof(buf), "V%d\n", VERSION);
1.1       dm         97:        (void) write(rem, buf, strlen(buf));
                     98:
1.6       millert    99: #if    !defined(DIRECT_RCMD)
1.4       millert   100:        if (getuid() != geteuid()) {
                    101:                error("This version of rdist should not be installed setuid.\n");
                    102:                return;
                    103:        }
1.6       millert   104: #endif /* DIRECT_RCMD */
1.4       millert   105:
1.1       dm        106:        for (;;) {
                    107:                cp = cmdbuf;
                    108:                if (read(rem, cp, 1) <= 0)
                    109:                        return;
                    110:                if (*cp++ == '\n') {
                    111:                        error("server: expected control record\n");
                    112:                        continue;
                    113:                }
                    114:                do {
                    115:                        if (read(rem, cp, 1) != 1)
                    116:                                cleanup(0);
                    117:                } while (*cp++ != '\n' && cp < &cmdbuf[BUFSIZ]);
                    118:                *--cp = '\0';
                    119:                cp = cmdbuf;
                    120:                switch (*cp++) {
                    121:                case 'T':  /* init target file/directory name */
                    122:                        catname = 1;    /* target should be directory */
                    123:                        goto dotarget;
                    124:
                    125:                case 't':  /* init target file/directory name */
                    126:                        catname = 0;
                    127:                dotarget:
1.8       deraadt   128:                        if (exptilde(target, cp, sizeof (target)) == NULL)
1.1       dm        129:                                continue;
                    130:                        tp = target;
                    131:                        while (*tp)
                    132:                                tp++;
                    133:                        ack();
                    134:                        continue;
                    135:
                    136:                case 'R':  /* Transfer a regular file. */
                    137:                        recvf(cp, S_IFREG);
                    138:                        continue;
                    139:
                    140:                case 'D':  /* Transfer a directory. */
                    141:                        recvf(cp, S_IFDIR);
                    142:                        continue;
                    143:
                    144:                case 'K':  /* Transfer symbolic link. */
                    145:                        recvf(cp, S_IFLNK);
                    146:                        continue;
                    147:
                    148:                case 'k':  /* Transfer hard link. */
                    149:                        hardlink(cp);
                    150:                        continue;
                    151:
                    152:                case 'E':  /* End. (of directory) */
                    153:                        *tp = '\0';
                    154:                        if (catname <= 0) {
                    155:                                error("server: too many 'E's\n");
                    156:                                continue;
                    157:                        }
                    158:                        tp = stp[--catname];
                    159:                        *tp = '\0';
                    160:                        ack();
                    161:                        continue;
                    162:
                    163:                case 'C':  /* Clean. Cleanup a directory */
                    164:                        clean(cp);
                    165:                        continue;
                    166:
                    167:                case 'Q':  /* Query. Does the file/directory exist? */
                    168:                        query(cp);
                    169:                        continue;
                    170:
                    171:                case 'S':  /* Special. Execute commands */
                    172:                        dospecial(cp);
                    173:                        continue;
                    174:
                    175: #ifdef notdef
                    176:                /*
                    177:                 * These entries are reserved but not currently used.
                    178:                 * The intent is to allow remote hosts to have master copies.
                    179:                 * Currently, only the host rdist runs on can have masters.
                    180:                 */
                    181:                case 'X':  /* start a new list of files to exclude */
                    182:                        except = bp = NULL;
                    183:                case 'x':  /* add name to list of files to exclude */
                    184:                        if (*cp == '\0') {
                    185:                                ack();
                    186:                                continue;
                    187:                        }
                    188:                        if (*cp == '~') {
1.8       deraadt   189:                                if (exptilde(buf, cp, sizeof (buf)) == NULL)
1.1       dm        190:                                        continue;
                    191:                                cp = buf;
                    192:                        }
                    193:                        if (bp == NULL)
                    194:                                except = bp = expand(makeblock(NAME, cp), E_VARS);
                    195:                        else
                    196:                                bp->b_next = expand(makeblock(NAME, cp), E_VARS);
                    197:                        while (bp->b_next != NULL)
                    198:                                bp = bp->b_next;
                    199:                        ack();
                    200:                        continue;
                    201:
                    202:                case 'I':  /* Install. Transfer file if out of date. */
                    203:                        opts = 0;
                    204:                        while (*cp >= '0' && *cp <= '7')
                    205:                                opts = (opts << 3) | (*cp++ - '0');
                    206:                        if (*cp++ != ' ') {
                    207:                                error("server: options not delimited\n");
                    208:                                return;
                    209:                        }
                    210:                        install(cp, opts);
                    211:                        continue;
                    212:
                    213:                case 'L':  /* Log. save message in log file */
                    214:                        log(lfp, cp);
                    215:                        continue;
                    216: #endif
                    217:
                    218:                case '\1':
                    219:                        nerrs++;
                    220:                        continue;
                    221:
                    222:                case '\2':
                    223:                        return;
                    224:
                    225:                default:
                    226:                        error("server: unknown command '%s'\n", cp);
                    227:                case '\0':
                    228:                        continue;
                    229:                }
                    230:        }
                    231: }
                    232:
                    233: /*
                    234:  * Update the file(s) if they are different.
                    235:  * destdir = 1 if destination should be a directory
                    236:  * (i.e., more than one source is being copied to the same destination).
                    237:  */
                    238: void
                    239: install(src, dest, destdir, opts)
                    240:        char *src, *dest;
                    241:        int destdir, opts;
                    242: {
                    243:        char *rname;
                    244:        char destcopy[BUFSIZ];
                    245:
                    246:        if (dest == NULL) {
                    247:                opts &= ~WHOLE; /* WHOLE mode only useful if renaming */
                    248:                dest = src;
                    249:        }
                    250:
                    251:        if (nflag || debug) {
                    252:                printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
                    253:                        opts & WHOLE ? " -w" : "",
                    254:                        opts & YOUNGER ? " -y" : "",
                    255:                        opts & COMPARE ? " -b" : "",
                    256:                        opts & REMOVE ? " -R" : "", src, dest);
                    257:                if (nflag)
                    258:                        return;
                    259:        }
                    260:
1.8       deraadt   261:        rname = exptilde(target, src, sizeof(target));
1.1       dm        262:        if (rname == NULL)
                    263:                return;
                    264:        tp = target;
                    265:        while (*tp)
                    266:                tp++;
                    267:        /*
                    268:         * If we are renaming a directory and we want to preserve
                    269:         * the directory heirarchy (-w), we must strip off the leading
                    270:         * directory name and preserve the rest.
                    271:         */
                    272:        if (opts & WHOLE) {
                    273:                while (*rname == '/')
                    274:                        rname++;
                    275:                destdir = 1;
                    276:        } else {
1.4       millert   277:                rname = xbasename(target);
1.1       dm        278:        }
                    279:        if (debug)
                    280:                printf("target = %s, rname = %s\n", target, rname);
                    281:        /*
                    282:         * Pass the destination file/directory name to remote.
                    283:         */
1.6       millert   284:        (void) snprintf(buf, sizeof(buf), "%c%s\n", destdir ? 'T' : 't', dest);
1.1       dm        285:        if (debug)
                    286:                printf("buf = %s", buf);
                    287:        (void) write(rem, buf, strlen(buf));
                    288:        if (response() < 0)
                    289:                return;
                    290:
                    291:        if (destdir) {
                    292:                strcpy(destcopy, dest);
                    293:                Tdest = destcopy;
                    294:        }
                    295:        sendf(rname, opts);
                    296:        Tdest = 0;
                    297: }
                    298:
                    299: #define protoname() (pw ? pw->pw_name : user)
                    300: #define protogroup() (gr ? gr->gr_name : group)
                    301: /*
                    302:  * Transfer the file or directory in target[].
                    303:  * rname is the name of the file on the remote host.
                    304:  */
                    305: static void
                    306: sendf(rname, opts)
                    307:        char *rname;
                    308:        int opts;
                    309: {
                    310:        register struct subcmd *sc;
                    311:        struct stat stb;
                    312:        int sizerr, f, u, len;
                    313:        off_t i;
                    314:        DIR *d;
                    315:        struct direct *dp;
                    316:        char *otp, *cp;
                    317:        extern struct subcmd *subcmds;
                    318:        static char user[15], group[15];
                    319:
                    320:        if (debug)
                    321:                printf("sendf(%s, %x)\n", rname, opts);
                    322:
                    323:        if (except(target))
                    324:                return;
                    325:        if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
                    326:                error("%s: %s\n", target, strerror(errno));
                    327:                return;
                    328:        }
                    329:        if ((u = update(rname, opts, &stb)) == 0) {
                    330:                if ((stb.st_mode & S_IFMT) == S_IFREG && stb.st_nlink > 1)
                    331:                        (void) savelink(&stb);
                    332:                return;
                    333:        }
                    334:
                    335:        if (pw == NULL || pw->pw_uid != stb.st_uid)
                    336:                if ((pw = getpwuid(stb.st_uid)) == NULL) {
                    337:                        log(lfp, "%s: no password entry for uid %d \n",
                    338:                                target, stb.st_uid);
                    339:                        pw = NULL;
1.5       millert   340:                        (void) snprintf(user, sizeof(user), ":%lu", stb.st_uid);
1.1       dm        341:                }
                    342:        if (gr == NULL || gr->gr_gid != stb.st_gid)
                    343:                if ((gr = getgrgid(stb.st_gid)) == NULL) {
                    344:                        log(lfp, "%s: no name for group %d\n",
                    345:                                target, stb.st_gid);
                    346:                        gr = NULL;
1.6       millert   347:                        (void) snprintf(group, sizeof(group), ":%lu",
                    348:                                stb.st_gid);
1.1       dm        349:                }
                    350:        if (u == 1) {
                    351:                if (opts & VERIFY) {
                    352:                        log(lfp, "need to install: %s\n", target);
                    353:                        goto dospecial;
                    354:                }
                    355:                log(lfp, "installing: %s\n", target);
                    356:                opts &= ~(COMPARE|REMOVE);
                    357:        }
                    358:
                    359:        switch (stb.st_mode & S_IFMT) {
                    360:        case S_IFDIR:
                    361:                if ((d = opendir(target)) == NULL) {
                    362:                        error("%s: %s\n", target, strerror(errno));
                    363:                        return;
                    364:                }
1.6       millert   365:                (void) snprintf(buf, sizeof(buf), "D%o %04o 0 0 %s %s %s\n",
                    366:                        opts, stb.st_mode & 07777, protoname(), protogroup(),
                    367:                        rname);
1.1       dm        368:                if (debug)
                    369:                        printf("buf = %s", buf);
                    370:                (void) write(rem, buf, strlen(buf));
                    371:                if (response() < 0) {
                    372:                        closedir(d);
                    373:                        return;
                    374:                }
                    375:
                    376:                if (opts & REMOVE)
                    377:                        rmchk(opts);
                    378:
                    379:                otp = tp;
                    380:                len = tp - target;
                    381:                while (dp = readdir(d)) {
                    382:                        if (!strcmp(dp->d_name, ".") ||
                    383:                            !strcmp(dp->d_name, ".."))
                    384:                                continue;
                    385:                        if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
                    386:                                error("%s/%s: Name too long\n", target,
                    387:                                        dp->d_name);
                    388:                                continue;
                    389:                        }
                    390:                        tp = otp;
                    391:                        *tp++ = '/';
                    392:                        cp = dp->d_name;
                    393:                        while (*tp++ = *cp++)
                    394:                                ;
                    395:                        tp--;
                    396:                        sendf(dp->d_name, opts);
                    397:                }
                    398:                closedir(d);
                    399:                (void) write(rem, "E\n", 2);
                    400:                (void) response();
                    401:                tp = otp;
                    402:                *tp = '\0';
                    403:                return;
                    404:
                    405:        case S_IFLNK:
                    406:                if (u != 1)
                    407:                        opts |= COMPARE;
                    408:                if (stb.st_nlink > 1) {
                    409:                        struct linkbuf *lp;
                    410:
                    411:                        if ((lp = savelink(&stb)) != NULL) {
                    412:                                /* install link */
                    413:                                if (*lp->target == 0)
1.6       millert   414:                                (void) snprintf(buf, sizeof(buf), "k%o %s %s\n",
1.5       millert   415:                                        opts, lp->pathname, rname);
1.1       dm        416:                                else
1.6       millert   417:                                (void) snprintf(buf, sizeof(buf),
                    418:                                        "k%o %s/%s %s\n", opts, lp->target,
                    419:                                        lp->pathname, rname);
1.1       dm        420:                                if (debug)
                    421:                                        printf("buf = %s", buf);
                    422:                                (void) write(rem, buf, strlen(buf));
                    423:                                (void) response();
                    424:                                return;
                    425:                        }
                    426:                }
1.6       millert   427:                (void) snprintf(buf, sizeof(buf), "K%o %o %qd %ld %s %s %s\n",
1.5       millert   428:                        opts, stb.st_mode & 07777, stb.st_size, stb.st_mtime,
1.1       dm        429:                        protoname(), protogroup(), rname);
                    430:                if (debug)
                    431:                        printf("buf = %s", buf);
                    432:                (void) write(rem, buf, strlen(buf));
                    433:                if (response() < 0)
                    434:                        return;
                    435:                sizerr = (readlink(target, buf, BUFSIZ) != stb.st_size);
                    436:                (void) write(rem, buf, stb.st_size);
                    437:                if (debug)
                    438:                        printf("readlink = %.*s\n", (int)stb.st_size, buf);
                    439:                goto done;
                    440:
                    441:        case S_IFREG:
                    442:                break;
                    443:
                    444:        default:
                    445:                error("%s: not a file or directory\n", target);
                    446:                return;
                    447:        }
                    448:
                    449:        if (u == 2) {
                    450:                if (opts & VERIFY) {
                    451:                        log(lfp, "need to update: %s\n", target);
                    452:                        goto dospecial;
                    453:                }
                    454:                log(lfp, "updating: %s\n", target);
                    455:        }
                    456:
                    457:        if (stb.st_nlink > 1) {
                    458:                struct linkbuf *lp;
                    459:
                    460:                if ((lp = savelink(&stb)) != NULL) {
                    461:                        /* install link */
                    462:                        if (*lp->target == 0)
1.6       millert   463:                        (void) snprintf(buf, sizeof(buf), "k%o %s %s\n", opts,
1.1       dm        464:                                lp->pathname, rname);
                    465:                        else
1.6       millert   466:                        (void) snprintf(buf, sizeof(buf), "k%o %s/%s %s\n",
1.5       millert   467:                                opts, lp->target, lp->pathname, rname);
1.1       dm        468:                        if (debug)
                    469:                                printf("buf = %s", buf);
                    470:                        (void) write(rem, buf, strlen(buf));
                    471:                        (void) response();
                    472:                        return;
                    473:                }
                    474:        }
                    475:
                    476:        if ((f = open(target, O_RDONLY, 0)) < 0) {
                    477:                error("%s: %s\n", target, strerror(errno));
                    478:                return;
                    479:        }
1.6       millert   480:        (void) snprintf(buf, sizeof(buf), "R%o %o %qd %ld %s %s %s\n", opts,
1.1       dm        481:                stb.st_mode & 07777, stb.st_size, stb.st_mtime,
                    482:                protoname(), protogroup(), rname);
                    483:        if (debug)
                    484:                printf("buf = %s", buf);
                    485:        (void) write(rem, buf, strlen(buf));
                    486:        if (response() < 0) {
                    487:                (void) close(f);
                    488:                return;
                    489:        }
                    490:        sizerr = 0;
                    491:        for (i = 0; i < stb.st_size; i += BUFSIZ) {
                    492:                int amt = BUFSIZ;
                    493:                if (i + amt > stb.st_size)
                    494:                        amt = stb.st_size - i;
                    495:                if (sizerr == 0 && read(f, buf, amt) != amt)
                    496:                        sizerr = 1;
                    497:                (void) write(rem, buf, amt);
                    498:        }
                    499:        (void) close(f);
                    500: done:
                    501:        if (sizerr) {
                    502:                error("%s: file changed size\n", target);
                    503:                err();
                    504:        } else
                    505:                ack();
                    506:        f = response();
                    507:        if (f < 0 || f == 0 && (opts & COMPARE))
                    508:                return;
                    509: dospecial:
                    510:        for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
                    511:                if (sc->sc_type != SPECIAL)
                    512:                        continue;
                    513:                if (sc->sc_args != NULL && !inlist(sc->sc_args, target))
                    514:                        continue;
                    515:                log(lfp, "special \"%s\"\n", sc->sc_name);
                    516:                if (opts & VERIFY)
                    517:                        continue;
1.6       millert   518:                (void) snprintf(buf, sizeof(buf), "SFILE=%s;%s\n", target,
1.5       millert   519:                        sc->sc_name);
1.1       dm        520:                if (debug)
                    521:                        printf("buf = %s", buf);
                    522:                (void) write(rem, buf, strlen(buf));
                    523:                while (response() > 0)
                    524:                        ;
                    525:        }
                    526: }
                    527:
                    528: static struct linkbuf *
                    529: savelink(stp)
                    530:        struct stat *stp;
                    531: {
                    532:        struct linkbuf *lp;
                    533:
                    534:        for (lp = ihead; lp != NULL; lp = lp->nextp)
                    535:                if (lp->inum == stp->st_ino && lp->devnum == stp->st_dev) {
                    536:                        lp->count--;
                    537:                        return(lp);
                    538:                }
                    539:        lp = (struct linkbuf *) malloc(sizeof(*lp));
                    540:        if (lp == NULL)
                    541:                log(lfp, "out of memory, link information lost\n");
                    542:        else {
                    543:                lp->nextp = ihead;
                    544:                ihead = lp;
                    545:                lp->inum = stp->st_ino;
                    546:                lp->devnum = stp->st_dev;
                    547:                lp->count = stp->st_nlink - 1;
                    548:                strcpy(lp->pathname, target);
                    549:                if (Tdest)
                    550:                        strcpy(lp->target, Tdest);
                    551:                else
                    552:                        *lp->target = 0;
                    553:        }
                    554:        return(NULL);
                    555: }
                    556:
                    557: /*
                    558:  * Check to see if file needs to be updated on the remote machine.
                    559:  * Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date
                    560:  * and 3 if comparing binaries to determine if out of date.
                    561:  */
                    562: static int
                    563: update(rname, opts, stp)
                    564:        char *rname;
                    565:        int opts;
                    566:        struct stat *stp;
                    567: {
                    568:        register char *cp, *s;
                    569:        register off_t size;
                    570:        register time_t mtime;
                    571:
                    572:        if (debug)
                    573:                printf("update(%s, %x, %x)\n", rname, opts, stp);
                    574:
                    575:        /*
                    576:         * Check to see if the file exists on the remote machine.
                    577:         */
1.6       millert   578:        (void) snprintf(buf, sizeof(buf), "Q%s\n", rname);
1.1       dm        579:        if (debug)
                    580:                printf("buf = %s", buf);
                    581:        (void) write(rem, buf, strlen(buf));
                    582: again:
                    583:        cp = s = buf;
                    584:        do {
                    585:                if (read(rem, cp, 1) != 1)
                    586:                        lostconn(0);
                    587:        } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
                    588:
                    589:        switch (*s++) {
                    590:        case 'Y':
                    591:                break;
                    592:
                    593:        case 'N':  /* file doesn't exist so install it */
                    594:                return(1);
                    595:
                    596:        case '\1':
                    597:                nerrs++;
                    598:                if (*s != '\n') {
                    599:                        if (!iamremote) {
                    600:                                fflush(stdout);
                    601:                                (void) write(2, s, cp - s);
                    602:                        }
                    603:                        if (lfp != NULL)
                    604:                                (void) fwrite(s, 1, cp - s, lfp);
                    605:                }
                    606:                return(0);
                    607:
                    608:        case '\3':
                    609:                *--cp = '\0';
                    610:                if (lfp != NULL)
                    611:                        log(lfp, "update: note: %s\n", s);
                    612:                goto again;
                    613:
                    614:        default:
                    615:                *--cp = '\0';
                    616:                error("update: unexpected response '%s'\n", s);
                    617:                return(0);
                    618:        }
                    619:
                    620:        if (*s == '\n')
                    621:                return(2);
                    622:
                    623:        if (opts & COMPARE)
                    624:                return(3);
                    625:
                    626:        size = 0;
                    627:        while (isdigit(*s))
                    628:                size = size * 10 + (*s++ - '0');
                    629:        if (*s++ != ' ') {
                    630:                error("update: size not delimited\n");
                    631:                return(0);
                    632:        }
                    633:        mtime = 0;
                    634:        while (isdigit(*s))
                    635:                mtime = mtime * 10 + (*s++ - '0');
                    636:        if (*s != '\n') {
                    637:                error("update: mtime not delimited\n");
                    638:                return(0);
                    639:        }
                    640:        /*
                    641:         * File needs to be updated?
                    642:         */
                    643:        if (opts & YOUNGER) {
                    644:                if (stp->st_mtime == mtime)
                    645:                        return(0);
                    646:                if (stp->st_mtime < mtime) {
                    647:                        log(lfp, "Warning: %s: remote copy is newer\n", target);
                    648:                        return(0);
                    649:                }
                    650:        } else if (stp->st_mtime == mtime && stp->st_size == size)
                    651:                return(0);
                    652:        return(2);
                    653: }
                    654:
                    655: /*
                    656:  * Query. Check to see if file exists. Return one of the following:
                    657:  *     N\n             - doesn't exist
                    658:  *     Ysize mtime\n   - exists and its a regular file (size & mtime of file)
                    659:  *     Y\n             - exists and its a directory or symbolic link
                    660:  *     ^Aerror message\n
                    661:  */
                    662: static void
                    663: query(name)
                    664:        char *name;
                    665: {
                    666:        struct stat stb;
                    667:
                    668:        if (catname)
1.6       millert   669:                (void) snprintf(tp, sizeof(target) - (tp - target),
                    670:                        "/%s", name);
1.1       dm        671:
                    672:        if (lstat(target, &stb) < 0) {
                    673:                if (errno == ENOENT)
                    674:                        (void) write(rem, "N\n", 2);
                    675:                else
                    676:                        error("%s:%s: %s\n", host, target, strerror(errno));
                    677:                *tp = '\0';
                    678:                return;
                    679:        }
                    680:
                    681:        switch (stb.st_mode & S_IFMT) {
                    682:        case S_IFREG:
1.6       millert   683:                (void) snprintf(buf, sizeof(buf), "Y%qd %ld\n", stb.st_size,
                    684:                        stb.st_mtime);
1.1       dm        685:                (void) write(rem, buf, strlen(buf));
                    686:                break;
                    687:
                    688:        case S_IFLNK:
                    689:        case S_IFDIR:
                    690:                (void) write(rem, "Y\n", 2);
                    691:                break;
                    692:
                    693:        default:
                    694:                error("%s: not a file or directory\n", name);
                    695:                break;
                    696:        }
                    697:        *tp = '\0';
                    698: }
                    699:
                    700: static void
                    701: recvf(cmd, type)
                    702:        char *cmd;
                    703:        int type;
                    704: {
1.2       deraadt   705:        register char *cp = cmd;
                    706:        int f = -1, mode, opts = 0, wrerr, olderrno;
1.1       dm        707:        off_t i, size;
                    708:        time_t mtime;
                    709:        struct stat stb;
                    710:        struct timeval tvp[2];
                    711:        char *owner, *group;
                    712:        char new[BUFSIZ];
                    713:        extern char *tempname;
                    714:
                    715:        while (*cp >= '0' && *cp <= '7')
                    716:                opts = (opts << 3) | (*cp++ - '0');
                    717:        if (*cp++ != ' ') {
                    718:                error("recvf: options not delimited\n");
                    719:                return;
                    720:        }
                    721:        mode = 0;
                    722:        while (*cp >= '0' && *cp <= '7')
                    723:                mode = (mode << 3) | (*cp++ - '0');
                    724:        if (*cp++ != ' ') {
                    725:                error("recvf: mode not delimited\n");
                    726:                return;
                    727:        }
                    728:        size = 0;
                    729:        while (isdigit(*cp))
                    730:                size = size * 10 + (*cp++ - '0');
                    731:        if (*cp++ != ' ') {
                    732:                error("recvf: size not delimited\n");
                    733:                return;
                    734:        }
                    735:        mtime = 0;
                    736:        while (isdigit(*cp))
                    737:                mtime = mtime * 10 + (*cp++ - '0');
                    738:        if (*cp++ != ' ') {
                    739:                error("recvf: mtime not delimited\n");
                    740:                return;
                    741:        }
                    742:        owner = cp;
                    743:        while (*cp && *cp != ' ')
                    744:                cp++;
                    745:        if (*cp != ' ') {
                    746:                error("recvf: owner name not delimited\n");
                    747:                return;
                    748:        }
                    749:        *cp++ = '\0';
                    750:        group = cp;
                    751:        while (*cp && *cp != ' ')
                    752:                cp++;
                    753:        if (*cp != ' ') {
                    754:                error("recvf: group name not delimited\n");
                    755:                return;
                    756:        }
                    757:        *cp++ = '\0';
                    758:
                    759:        if (type == S_IFDIR) {
                    760:                if (catname >= sizeof(stp)) {
                    761:                        error("%s:%s: too many directory levels\n",
                    762:                                host, target);
                    763:                        return;
                    764:                }
                    765:                stp[catname] = tp;
                    766:                if (catname++) {
                    767:                        *tp++ = '/';
                    768:                        while (*tp++ = *cp++)
                    769:                                ;
                    770:                        tp--;
                    771:                }
                    772:                if (opts & VERIFY) {
                    773:                        ack();
                    774:                        return;
                    775:                }
                    776:                if (lstat(target, &stb) == 0) {
                    777:                        if (ISDIR(stb.st_mode)) {
                    778:                                if ((stb.st_mode & 07777) == mode) {
                    779:                                        ack();
                    780:                                        return;
                    781:                                }
                    782:                                buf[0] = '\0';
1.6       millert   783:                                (void) snprintf(buf + 1, sizeof(buf) - 1,
1.1       dm        784:                                        "%s: Warning: remote mode %o != local mode %o\n",
                    785:                                        target, stb.st_mode & 07777, mode);
                    786:                                (void) write(rem, buf, strlen(buf + 1) + 1);
                    787:                                return;
                    788:                        }
                    789:                        errno = ENOTDIR;
                    790:                } else if (errno == ENOENT && (mkdir(target, mode) == 0 ||
                    791:                    chkparent(target) == 0 && mkdir(target, mode) == 0)) {
                    792:                        if (fchog(-1, target, owner, group, mode) == 0)
                    793:                                ack();
                    794:                        return;
                    795:                }
                    796:                error("%s:%s: %s\n", host, target, strerror(errno));
                    797:                tp = stp[--catname];
                    798:                *tp = '\0';
                    799:                return;
                    800:        }
                    801:
                    802:        if (catname)
1.6       millert   803:                (void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
1.4       millert   804:        cp = strrchr(target, '/');
1.1       dm        805:        if (cp == NULL)
                    806:                strcpy(new, tempname);
                    807:        else if (cp == target)
1.5       millert   808:                (void) snprintf(new, sizeof(new), "/%s", tempname);
1.1       dm        809:        else {
                    810:                *cp = '\0';
1.5       millert   811:                (void) snprintf(new, sizeof(new), "%s/%s", target, tempname);
1.1       dm        812:                *cp = '/';
                    813:        }
                    814:
                    815:        if (type == S_IFLNK) {
                    816:                int j;
                    817:
                    818:                ack();
                    819:                cp = buf;
                    820:                for (i = 0; i < size; i += j) {
                    821:                        if ((j = read(rem, cp, size - i)) <= 0)
                    822:                                cleanup(0);
                    823:                        cp += j;
                    824:                }
                    825:                *cp = '\0';
                    826:                if (response() < 0) {
                    827:                        err();
                    828:                        return;
                    829:                }
                    830:                if (symlink(buf, new) < 0) {
                    831:                        if (errno != ENOENT || chkparent(new) < 0 ||
                    832:                            symlink(buf, new) < 0)
                    833:                                goto badnew1;
                    834:                }
                    835:                mode &= 0777;
                    836:                if (opts & COMPARE) {
                    837:                        char tbuf[BUFSIZ];
                    838:
                    839:                        if ((i = readlink(target, tbuf, BUFSIZ)) >= 0 &&
                    840:                            i == size && strncmp(buf, tbuf, size) == 0) {
                    841:                                (void) unlink(new);
                    842:                                ack();
                    843:                                return;
                    844:                        }
                    845:                        if (opts & VERIFY)
                    846:                                goto differ;
                    847:                }
                    848:                goto fixup;
                    849:        }
                    850:
                    851:        if ((f = creat(new, mode)) < 0) {
                    852:                if (errno != ENOENT || chkparent(new) < 0 ||
                    853:                    (f = creat(new, mode)) < 0)
                    854:                        goto badnew1;
                    855:        }
                    856:
                    857:        ack();
                    858:        wrerr = 0;
                    859:        for (i = 0; i < size; i += BUFSIZ) {
                    860:                int amt = BUFSIZ;
                    861:
                    862:                cp = buf;
                    863:                if (i + amt > size)
                    864:                        amt = size - i;
                    865:                do {
                    866:                        int j = read(rem, cp, amt);
                    867:
                    868:                        if (j <= 0) {
                    869:                                (void) close(f);
                    870:                                (void) unlink(new);
                    871:                                cleanup(0);
                    872:                        }
                    873:                        amt -= j;
                    874:                        cp += j;
                    875:                } while (amt > 0);
                    876:                amt = BUFSIZ;
                    877:                if (i + amt > size)
                    878:                        amt = size - i;
                    879:                if (wrerr == 0 && write(f, buf, amt) != amt) {
                    880:                        olderrno = errno;
                    881:                        wrerr++;
                    882:                }
                    883:        }
                    884:        if (response() < 0) {
                    885:                err();
                    886:                goto badnew2;
                    887:        }
                    888:        if (wrerr)
                    889:                goto badnew1;
                    890:        if (opts & COMPARE) {
                    891:                FILE *f1, *f2;
                    892:                int c;
                    893:
                    894:                if ((f1 = fopen(target, "r")) == NULL)
                    895:                        goto badtarget;
                    896:                if ((f2 = fopen(new, "r")) == NULL) {
                    897: badnew1:               error("%s:%s: %s\n", host, new, strerror(errno));
                    898:                        goto badnew2;
                    899:                }
                    900:                while ((c = getc(f1)) == getc(f2))
                    901:                        if (c == EOF) {
                    902:                                (void) fclose(f1);
                    903:                                (void) fclose(f2);
                    904:                                ack();
                    905:                                goto badnew2;
                    906:                        }
                    907:                (void) fclose(f1);
                    908:                (void) fclose(f2);
                    909:                if (opts & VERIFY) {
                    910: differ:                        buf[0] = '\0';
1.6       millert   911:                        (void) snprintf(buf + 1, sizeof(buf) - 1,
1.5       millert   912:                                "need to update: %s\n",target);
1.1       dm        913:                        (void) write(rem, buf, strlen(buf + 1) + 1);
                    914:                        goto badnew2;
                    915:                }
                    916:        }
                    917:
                    918:        /*
                    919:         * Set last modified time
                    920:         */
                    921:        tvp[0].tv_sec = time(0);
                    922:        tvp[0].tv_usec = 0;
                    923:        tvp[1].tv_sec = mtime;
                    924:        tvp[1].tv_usec = 0;
                    925:        if (utimes(new, tvp) < 0)
                    926:                note("%s: utimes failed %s: %s\n", host, new, strerror(errno));
                    927:
                    928:        if (fchog(f, new, owner, group, mode) < 0) {
                    929: badnew2:
1.6       millert   930:                if (f >= 0)
1.1       dm        931:                        (void) close(f);
                    932:                (void) unlink(new);
                    933:                return;
                    934:        }
                    935:        (void) close(f);
                    936:
                    937: fixup: if (rename(new, target) < 0) {
                    938: badtarget:     error("%s:%s: %s\n", host, target, strerror(errno));
                    939:                (void) unlink(new);
                    940:                return;
                    941:        }
                    942:
                    943:        if (opts & COMPARE) {
                    944:                buf[0] = '\0';
1.6       millert   945:                (void) snprintf(buf + 1, sizeof(buf) - 1,
1.5       millert   946:                        "updated %s\n", target);
1.1       dm        947:                (void) write(rem, buf, strlen(buf + 1) + 1);
                    948:        } else
                    949:                ack();
                    950: }
                    951:
                    952: /*
                    953:  * Creat a hard link to existing file.
                    954:  */
                    955: static void
                    956: hardlink(cmd)
                    957:        char *cmd;
                    958: {
1.6       millert   959:        register char *cp = cmd;
1.1       dm        960:        struct stat stb;
                    961:        char *oldname;
1.6       millert   962:        int opts = 0, exists = 0;
1.1       dm        963:
                    964:        while (*cp >= '0' && *cp <= '7')
                    965:                opts = (opts << 3) | (*cp++ - '0');
                    966:        if (*cp++ != ' ') {
                    967:                error("hardlink: options not delimited\n");
                    968:                return;
                    969:        }
                    970:        oldname = cp;
                    971:        while (*cp && *cp != ' ')
                    972:                cp++;
                    973:        if (*cp != ' ') {
                    974:                error("hardlink: oldname name not delimited\n");
                    975:                return;
                    976:        }
                    977:        *cp++ = '\0';
                    978:
                    979:        if (catname) {
1.6       millert   980:                (void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
1.1       dm        981:        }
                    982:        if (lstat(target, &stb) == 0) {
                    983:                int mode = stb.st_mode & S_IFMT;
                    984:                if (mode != S_IFREG && mode != S_IFLNK) {
                    985:                        error("%s:%s: not a regular file\n", host, target);
                    986:                        return;
                    987:                }
                    988:                exists = 1;
                    989:        }
                    990:        if (chkparent(target) < 0 ) {
                    991:                error("%s:%s: %s (no parent)\n",
                    992:                        host, target, strerror(errno));
                    993:                return;
                    994:        }
                    995:        if (exists && (unlink(target) < 0)) {
                    996:                error("%s:%s: %s (unlink)\n",
                    997:                        host, target, strerror(errno));
                    998:                return;
                    999:        }
                   1000:        if (link(oldname, target) < 0) {
                   1001:                error("%s:can't link %s to %s\n",
                   1002:                        host, target, oldname);
                   1003:                return;
                   1004:        }
                   1005:        ack();
                   1006: }
                   1007:
                   1008: /*
                   1009:  * Check to see if parent directory exists and create one if not.
                   1010:  */
                   1011: static int
                   1012: chkparent(name)
                   1013:        char *name;
                   1014: {
                   1015:        register char *cp;
                   1016:        struct stat stb;
                   1017:
1.4       millert  1018:        cp = strrchr(name, '/');
1.1       dm       1019:        if (cp == NULL || cp == name)
                   1020:                return(0);
                   1021:        *cp = '\0';
                   1022:        if (lstat(name, &stb) < 0) {
                   1023:                if (errno == ENOENT && chkparent(name) >= 0 &&
                   1024:                    mkdir(name, 0777 & ~oumask) >= 0) {
                   1025:                        *cp = '/';
                   1026:                        return(0);
                   1027:                }
                   1028:        } else if (ISDIR(stb.st_mode)) {
                   1029:                *cp = '/';
                   1030:                return(0);
                   1031:        }
                   1032:        *cp = '/';
                   1033:        return(-1);
                   1034: }
                   1035:
                   1036: /*
                   1037:  * Change owner, group and mode of file.
                   1038:  */
                   1039: static int
                   1040: fchog(fd, file, owner, group, mode)
                   1041:        int fd;
                   1042:        char *file, *owner, *group;
                   1043:        int mode;
                   1044: {
                   1045:        register int i;
                   1046:        int uid, gid;
                   1047:        extern char user[];
                   1048:        extern int userid;
                   1049:
                   1050:        uid = userid;
                   1051:        if (userid == 0) {
                   1052:                if (*owner == ':') {
                   1053:                        uid = atoi(owner + 1);
                   1054:                } else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
                   1055:                        if ((pw = getpwnam(owner)) == NULL) {
                   1056:                                if (mode & 04000) {
                   1057:                                        note("%s:%s: unknown login name, clearing setuid",
                   1058:                                                host, owner);
                   1059:                                        mode &= ~04000;
                   1060:                                        uid = 0;
                   1061:                                }
                   1062:                        } else
                   1063:                                uid = pw->pw_uid;
                   1064:                } else
                   1065:                        uid = pw->pw_uid;
                   1066:                if (*group == ':') {
                   1067:                        gid = atoi(group + 1);
                   1068:                        goto ok;
                   1069:                }
                   1070:        } else if ((mode & 04000) && strcmp(user, owner) != 0)
                   1071:                mode &= ~04000;
                   1072:        gid = -1;
                   1073:        if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
                   1074:                if ((*group == ':' && (getgrgid(gid = atoi(group + 1)) == NULL))
                   1075:                   || ((gr = getgrnam(group)) == NULL)) {
                   1076:                        if (mode & 02000) {
                   1077:                                note("%s:%s: unknown group", host, group);
                   1078:                                mode &= ~02000;
                   1079:                        }
                   1080:                } else
                   1081:                        gid = gr->gr_gid;
                   1082:        } else
                   1083:                gid = gr->gr_gid;
                   1084:        if (userid && gid >= 0) {
                   1085:                if (gr) for (i = 0; gr->gr_mem[i] != NULL; i++)
                   1086:                        if (!(strcmp(user, gr->gr_mem[i])))
                   1087:                                goto ok;
                   1088:                mode &= ~02000;
                   1089:                gid = -1;
                   1090:        }
                   1091: ok:    if (fd != -1 && fchown(fd, uid, gid) < 0 || chown(file, uid, gid) < 0)
                   1092:                note("%s: %s chown: %s", host, file, strerror(errno));
                   1093:        else if (mode & 07000 &&
                   1094:           (fd != -1 && fchmod(fd, mode) < 0 || chmod(file, mode) < 0))
                   1095:                note("%s: %s chmod: %s", host, file, strerror(errno));
                   1096:        return(0);
                   1097: }
                   1098:
                   1099: /*
                   1100:  * Check for files on the machine being updated that are not on the master
                   1101:  * machine and remove them.
                   1102:  */
                   1103: static void
                   1104: rmchk(opts)
                   1105:        int opts;
                   1106: {
                   1107:        register char *cp, *s;
                   1108:        struct stat stb;
                   1109:
                   1110:        if (debug)
                   1111:                printf("rmchk()\n");
                   1112:
                   1113:        /*
                   1114:         * Tell the remote to clean the files from the last directory sent.
                   1115:         */
1.6       millert  1116:        (void) snprintf(buf, sizeof(buf), "C%o\n", opts & VERIFY);
1.1       dm       1117:        if (debug)
                   1118:                printf("buf = %s", buf);
                   1119:        (void) write(rem, buf, strlen(buf));
                   1120:        if (response() < 0)
                   1121:                return;
                   1122:        for (;;) {
                   1123:                cp = s = buf;
                   1124:                do {
                   1125:                        if (read(rem, cp, 1) != 1)
                   1126:                                lostconn(0);
                   1127:                } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
                   1128:
                   1129:                switch (*s++) {
                   1130:                case 'Q': /* Query if file should be removed */
                   1131:                        /*
                   1132:                         * Return the following codes to remove query.
                   1133:                         * N\n -- file exists - DON'T remove.
                   1134:                         * Y\n -- file doesn't exist - REMOVE.
                   1135:                         */
                   1136:                        *--cp = '\0';
1.6       millert  1137:                        (void) snprintf(tp, sizeof(target) - (tp - target),
                   1138:                                "/%s", s);
1.1       dm       1139:                        if (debug)
                   1140:                                printf("check %s\n", target);
                   1141:                        if (except(target))
                   1142:                                (void) write(rem, "N\n", 2);
                   1143:                        else if (lstat(target, &stb) < 0)
                   1144:                                (void) write(rem, "Y\n", 2);
                   1145:                        else
                   1146:                                (void) write(rem, "N\n", 2);
                   1147:                        break;
                   1148:
                   1149:                case '\0':
                   1150:                        *--cp = '\0';
                   1151:                        if (*s != '\0')
                   1152:                                log(lfp, "%s\n", s);
                   1153:                        break;
                   1154:
                   1155:                case 'E':
                   1156:                        *tp = '\0';
                   1157:                        ack();
                   1158:                        return;
                   1159:
                   1160:                case '\1':
                   1161:                case '\2':
                   1162:                        nerrs++;
                   1163:                        if (*s != '\n') {
                   1164:                                if (!iamremote) {
                   1165:                                        fflush(stdout);
                   1166:                                        (void) write(2, s, cp - s);
                   1167:                                }
                   1168:                                if (lfp != NULL)
                   1169:                                        (void) fwrite(s, 1, cp - s, lfp);
                   1170:                        }
                   1171:                        if (buf[0] == '\2')
                   1172:                                lostconn(0);
                   1173:                        break;
                   1174:
                   1175:                default:
                   1176:                        error("rmchk: unexpected response '%s'\n", buf);
                   1177:                        err();
                   1178:                }
                   1179:        }
                   1180: }
                   1181:
                   1182: /*
                   1183:  * Check the current directory (initialized by the 'T' command to server())
                   1184:  * for extraneous files and remove them.
                   1185:  */
                   1186: static void
                   1187: clean(cp)
                   1188:        register char *cp;
                   1189: {
                   1190:        DIR *d;
                   1191:        register struct direct *dp;
                   1192:        struct stat stb;
                   1193:        char *otp;
                   1194:        int len, opts;
                   1195:
                   1196:        opts = 0;
                   1197:        while (*cp >= '0' && *cp <= '7')
                   1198:                opts = (opts << 3) | (*cp++ - '0');
                   1199:        if (*cp != '\0') {
                   1200:                error("clean: options not delimited\n");
                   1201:                return;
                   1202:        }
                   1203:        if ((d = opendir(target)) == NULL) {
                   1204:                error("%s:%s: %s\n", host, target, strerror(errno));
                   1205:                return;
                   1206:        }
                   1207:        ack();
                   1208:
                   1209:        otp = tp;
                   1210:        len = tp - target;
                   1211:        while (dp = readdir(d)) {
                   1212:                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                   1213:                        continue;
                   1214:                if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
                   1215:                        error("%s:%s/%s: Name too long\n",
                   1216:                                host, target, dp->d_name);
                   1217:                        continue;
                   1218:                }
                   1219:                tp = otp;
                   1220:                *tp++ = '/';
                   1221:                cp = dp->d_name;;
                   1222:                while (*tp++ = *cp++)
                   1223:                        ;
                   1224:                tp--;
                   1225:                if (lstat(target, &stb) < 0) {
                   1226:                        error("%s:%s: %s\n", host, target, strerror(errno));
                   1227:                        continue;
                   1228:                }
1.6       millert  1229:                (void) snprintf(buf, sizeof(buf), "Q%s\n", dp->d_name);
1.1       dm       1230:                (void) write(rem, buf, strlen(buf));
                   1231:                cp = buf;
                   1232:                do {
                   1233:                        if (read(rem, cp, 1) != 1)
                   1234:                                cleanup(0);
                   1235:                } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
                   1236:                *--cp = '\0';
                   1237:                cp = buf;
                   1238:                if (*cp != 'Y')
                   1239:                        continue;
                   1240:                if (opts & VERIFY) {
                   1241:                        cp = buf;
                   1242:                        *cp++ = '\0';
1.6       millert  1243:                        (void) snprintf(cp, sizeof(buf) - 1,
1.5       millert  1244:                                "need to remove: %s\n", target);
1.1       dm       1245:                        (void) write(rem, buf, strlen(cp) + 1);
                   1246:                } else
                   1247:                        removeit(&stb);
                   1248:        }
                   1249:        closedir(d);
                   1250:        (void) write(rem, "E\n", 2);
                   1251:        (void) response();
                   1252:        tp = otp;
                   1253:        *tp = '\0';
                   1254: }
                   1255:
                   1256: /*
                   1257:  * Remove a file or directory (recursively) and send back an acknowledge
                   1258:  * or an error message.
                   1259:  */
                   1260: static void
                   1261: removeit(stp)
                   1262:        struct stat *stp;
                   1263: {
                   1264:        DIR *d;
                   1265:        struct direct *dp;
                   1266:        register char *cp;
                   1267:        struct stat stb;
                   1268:        char *otp;
                   1269:        int len;
                   1270:
                   1271:        switch (stp->st_mode & S_IFMT) {
                   1272:        case S_IFREG:
                   1273:        case S_IFLNK:
                   1274:                if (unlink(target) < 0)
                   1275:                        goto bad;
                   1276:                goto removed;
                   1277:
                   1278:        case S_IFDIR:
                   1279:                break;
                   1280:
                   1281:        default:
                   1282:                error("%s:%s: not a plain file\n", host, target);
                   1283:                return;
                   1284:        }
                   1285:
                   1286:        if ((d = opendir(target)) == NULL)
                   1287:                goto bad;
                   1288:
                   1289:        otp = tp;
                   1290:        len = tp - target;
                   1291:        while (dp = readdir(d)) {
                   1292:                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                   1293:                        continue;
                   1294:                if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
                   1295:                        error("%s:%s/%s: Name too long\n",
                   1296:                                host, target, dp->d_name);
                   1297:                        continue;
                   1298:                }
                   1299:                tp = otp;
                   1300:                *tp++ = '/';
                   1301:                cp = dp->d_name;;
                   1302:                while (*tp++ = *cp++)
                   1303:                        ;
                   1304:                tp--;
                   1305:                if (lstat(target, &stb) < 0) {
                   1306:                        error("%s:%s: %s\n", host, target, strerror(errno));
                   1307:                        continue;
                   1308:                }
                   1309:                removeit(&stb);
                   1310:        }
                   1311:        closedir(d);
                   1312:        tp = otp;
                   1313:        *tp = '\0';
                   1314:        if (rmdir(target) < 0) {
                   1315: bad:
                   1316:                error("%s:%s: %s\n", host, target, strerror(errno));
                   1317:                return;
                   1318:        }
                   1319: removed:
                   1320:        cp = buf;
                   1321:        *cp++ = '\0';
1.6       millert  1322:        (void) snprintf(cp, sizeof(buf) - 1, "removed %s\n", target);
1.1       dm       1323:        (void) write(rem, buf, strlen(cp) + 1);
                   1324: }
                   1325:
                   1326: /*
                   1327:  * Execute a shell command to handle special cases.
                   1328:  */
                   1329: static void
                   1330: dospecial(cmd)
                   1331:        char *cmd;
                   1332: {
                   1333:        int fd[2], status, pid, i;
                   1334:        register char *cp, *s;
                   1335:        char sbuf[BUFSIZ];
                   1336:        extern int userid, groupid;
                   1337:
                   1338:        if (pipe(fd) < 0) {
                   1339:                error("%s\n", strerror(errno));
                   1340:                return;
                   1341:        }
                   1342:        if ((pid = fork()) == 0) {
                   1343:                /*
                   1344:                 * Return everything the shell commands print.
                   1345:                 */
                   1346:                (void) close(0);
                   1347:                (void) close(1);
                   1348:                (void) close(2);
                   1349:                (void) open(_PATH_DEVNULL, O_RDONLY);
                   1350:                (void) dup(fd[1]);
                   1351:                (void) dup(fd[1]);
                   1352:                (void) close(fd[0]);
                   1353:                (void) close(fd[1]);
1.4       millert  1354: #if    defined(DIRECT_RCMD)
1.7       tholo    1355:                setegid(groupid);
1.1       dm       1356:                setgid(groupid);
1.7       tholo    1357:                seteuid(userid);
1.1       dm       1358:                setuid(userid);
1.4       millert  1359: #endif /* DIRECT_RCMD */
1.1       dm       1360:                execl(_PATH_BSHELL, "sh", "-c", cmd, 0);
                   1361:                _exit(127);
                   1362:        }
                   1363:        (void) close(fd[1]);
                   1364:        s = sbuf;
                   1365:        *s++ = '\0';
1.6       millert  1366:        while ((i = read(fd[0], buf, sizeof(buf))) > 0) {
1.1       dm       1367:                cp = buf;
                   1368:                do {
                   1369:                        *s++ = *cp++;
                   1370:                        if (cp[-1] != '\n') {
                   1371:                                if (s < &sbuf[sizeof(sbuf)-1])
                   1372:                                        continue;
                   1373:                                *s++ = '\n';
                   1374:                        }
                   1375:                        /*
                   1376:                         * Throw away blank lines.
                   1377:                         */
                   1378:                        if (s == &sbuf[2]) {
                   1379:                                s--;
                   1380:                                continue;
                   1381:                        }
                   1382:                        (void) write(rem, sbuf, s - sbuf);
                   1383:                        s = &sbuf[1];
                   1384:                } while (--i);
                   1385:        }
                   1386:        if (s > &sbuf[1]) {
                   1387:                *s++ = '\n';
                   1388:                (void) write(rem, sbuf, s - sbuf);
                   1389:        }
                   1390:        while ((i = wait(&status)) != pid && i != -1)
                   1391:                ;
                   1392:        if (i == -1)
                   1393:                status = -1;
                   1394:        (void) close(fd[0]);
                   1395:        if (status)
                   1396:                error("shell returned %d\n", status);
                   1397:        else
                   1398:                ack();
                   1399: }
                   1400:
1.9     ! mickey   1401: #ifdef __STDC__
1.1       dm       1402: #include <stdarg.h>
                   1403: #else
                   1404: #include <varargs.h>
                   1405: #endif
                   1406:
                   1407: void
1.9     ! mickey   1408: #ifdef __STDC__
1.1       dm       1409: log(FILE *fp, const char *fmt, ...)
                   1410: #else
                   1411: log(fp, fmt, va_alist)
                   1412:        FILE *fp;
                   1413:        char *fmt;
                   1414:         va_dcl
                   1415: #endif
                   1416: {
                   1417:        va_list ap;
1.9     ! mickey   1418: #ifdef __STDC__
1.1       dm       1419:        va_start(ap, fmt);
                   1420: #else
                   1421:        va_start(ap);
                   1422: #endif
                   1423:        /* Print changes locally if not quiet mode */
                   1424:        if (!qflag)
1.5       millert  1425:                (void) vprintf(fmt, ap);
1.1       dm       1426:
                   1427:        /* Save changes (for mailing) if really updating files */
                   1428:        if (!(options & VERIFY) && fp != NULL)
1.5       millert  1429:                (void) vfprintf(fp, fmt, ap);
1.1       dm       1430:        va_end(ap);
                   1431: }
                   1432:
                   1433: void
1.9     ! mickey   1434: #ifdef __STDC__
1.1       dm       1435: error(const char *fmt, ...)
                   1436: #else
                   1437: error(fmt, va_alist)
                   1438:        char *fmt;
                   1439:         va_dcl
                   1440: #endif
                   1441: {
                   1442:        static FILE *fp;
                   1443:        va_list ap;
1.9     ! mickey   1444: #ifdef __STDC__
1.1       dm       1445:        va_start(ap, fmt);
                   1446: #else
                   1447:        va_start(ap);
                   1448: #endif
                   1449:
                   1450:        ++nerrs;
                   1451:        if (iamremote) {
1.4       millert  1452:                if (!fp && (rem < 0 || !(fp = fdopen(rem, "w"))))
                   1453:                        return;
1.5       millert  1454:                (void) fprintf(fp, "%crdist: ", 0x01);
                   1455:                (void) vfprintf(fp, fmt, ap);
1.1       dm       1456:                fflush(fp);
                   1457:        }
                   1458:        else {
                   1459:                fflush(stdout);
1.5       millert  1460:                (void) fprintf(stderr, "rdist: ");
                   1461:                (void) vfprintf(stderr, fmt, ap);
1.1       dm       1462:                fflush(stderr);
                   1463:        }
                   1464:        if (lfp != NULL) {
1.5       millert  1465:                (void) fprintf(lfp, "rdist: ");
                   1466:                (void) vfprintf(lfp, fmt, ap);
1.1       dm       1467:                fflush(lfp);
                   1468:        }
                   1469:        va_end(ap);
                   1470: }
                   1471:
                   1472: void
1.9     ! mickey   1473: #ifdef __STDC__
1.1       dm       1474: fatal(const char *fmt, ...)
                   1475: #else
                   1476: fatal(fmt, va_alist)
                   1477:        char *fmt;
                   1478:         va_dcl
                   1479: #endif
                   1480: {
                   1481:        static FILE *fp;
                   1482:        va_list ap;
1.9     ! mickey   1483: #ifdef __STDC__
1.1       dm       1484:        va_start(ap, fmt);
                   1485: #else
                   1486:        va_start(ap);
                   1487: #endif
                   1488:
                   1489:        ++nerrs;
                   1490:        if (!fp && !(fp = fdopen(rem, "w")))
                   1491:                return;
                   1492:        if (iamremote) {
1.5       millert  1493:                (void) fprintf(fp, "%crdist: ", 0x02);
                   1494:                (void) vfprintf(fp, fmt, ap);
1.1       dm       1495:                fflush(fp);
                   1496:        }
                   1497:        else {
                   1498:                fflush(stdout);
1.5       millert  1499:                (void) fprintf(stderr, "rdist: ");
                   1500:                (void) vfprintf(stderr, fmt, ap);
1.1       dm       1501:                fflush(stderr);
                   1502:        }
                   1503:        if (lfp != NULL) {
1.5       millert  1504:                (void) fprintf(lfp, "rdist: ");
                   1505:                (void) vfprintf(lfp, fmt, ap);
1.1       dm       1506:                fflush(lfp);
                   1507:        }
                   1508:        cleanup(0);
                   1509: }
                   1510:
                   1511: static int
                   1512: response()
                   1513: {
                   1514:        char *cp, *s;
                   1515:        char resp[BUFSIZ];
                   1516:
                   1517:        if (debug)
                   1518:                printf("response()\n");
                   1519:
                   1520:        cp = s = resp;
                   1521:        do {
                   1522:                if (read(rem, cp, 1) != 1)
                   1523:                        lostconn(0);
                   1524:        } while (*cp++ != '\n' && cp < &resp[BUFSIZ]);
                   1525:
                   1526:        switch (*s++) {
                   1527:        case '\0':
                   1528:                *--cp = '\0';
                   1529:                if (*s != '\0') {
                   1530:                        log(lfp, "%s\n", s);
                   1531:                        return(1);
                   1532:                }
                   1533:                return(0);
                   1534:        case '\3':
                   1535:                *--cp = '\0';
                   1536:                log(lfp, "Note: %s\n",s);
                   1537:                return(response());
                   1538:
                   1539:        default:
                   1540:                s--;
                   1541:                /* fall into... */
                   1542:        case '\1':
                   1543:        case '\2':
                   1544:                nerrs++;
                   1545:                if (*s != '\n') {
                   1546:                        if (!iamremote) {
                   1547:                                fflush(stdout);
                   1548:                                (void) write(2, s, cp - s);
                   1549:                        }
                   1550:                        if (lfp != NULL)
                   1551:                                (void) fwrite(s, 1, cp - s, lfp);
                   1552:                }
                   1553:                if (resp[0] == '\2')
                   1554:                        lostconn(0);
                   1555:                return(-1);
                   1556:        }
                   1557: }
                   1558:
                   1559: /*
                   1560:  * Remove temporary files and do any cleanup operations before exiting.
                   1561:  */
                   1562: void
                   1563: cleanup(signo)
                   1564:        int signo;
                   1565: {
                   1566:        (void) unlink(tempfile);
                   1567:        exit(1);
                   1568: }
                   1569:
                   1570: static void
1.9     ! mickey   1571: #ifdef __STDC__
1.1       dm       1572: note(const char *fmt, ...)
                   1573: #else
                   1574: note(fmt, va_alist)
                   1575:        char *fmt;
                   1576:         va_dcl
                   1577: #endif
                   1578: {
                   1579:        static char buf[BUFSIZ];
                   1580:        va_list ap;
1.9     ! mickey   1581: #ifdef __STDC__
1.1       dm       1582:        va_start(ap, fmt);
                   1583: #else
                   1584:        va_start(ap);
                   1585: #endif
1.6       millert  1586:        (void) vsnprintf(buf, sizeof(buf), fmt, ap);
1.1       dm       1587:        va_end(ap);
                   1588:        comment(buf);
                   1589: }
                   1590:
                   1591: static void
                   1592: comment(s)
                   1593:        char *s;
                   1594: {
                   1595:        char c;
                   1596:
                   1597:        c = '\3';
                   1598:        write(rem, &c, 1);
                   1599:        write(rem, s, strlen(s));
                   1600:        c = '\n';
                   1601:        write(rem, &c, 1);
                   1602: }