[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.10

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