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

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