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

Annotation of src/usr.bin/rdist/common.c, Revision 1.29

1.29    ! guenther    1: /*     $OpenBSD: common.c,v 1.28 2014/07/05 06:18:58 guenther Exp $    */
1.3       deraadt     2:
1.1       dm          3: /*
                      4:  * Copyright (c) 1983 Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.21      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:
1.20      millert    32: #include "defs.h"
1.1       dm         33:
                     34: /*
                     35:  * Things common to both the client and server.
                     36:  */
                     37:
                     38: #if    defined(NEED_UTIME_H)
                     39: #include <utime.h>
                     40: #endif /* defined(NEED_UTIME_H) */
1.20      millert    41: #include <sys/wait.h>
1.6       millert    42: #include <sys/socket.h>
1.1       dm         43:
                     44: /*
                     45:  * Variables common to both client and server
                     46:  */
                     47: char                   host[MAXHOSTNAMELEN];   /* Name of this host */
1.29    ! guenther   48: uid_t                  userid = (uid_t)-1;     /* User's UID */
        !            49: gid_t                  groupid = (gid_t)-1;    /* User's GID */
1.1       dm         50: char                  *homedir = NULL;         /* User's $HOME */
                     51: char                  *locuser = NULL;         /* Local User's name */
                     52: int                    isserver = FALSE;       /* We're the server */
                     53: int                    amchild = 0;            /* This PID is a child */
                     54: int                    do_fork = 1;            /* Fork child process */
                     55: char                  *currenthost = NULL;     /* Current client hostname */
                     56: char                  *progname = NULL;        /* Name of this program */
                     57: int                    rem_r = -1;             /* Client file descriptor */
                     58: int                    rem_w = -1;             /* Client file descriptor */
                     59: struct passwd         *pw = NULL;              /* Local user's pwd entry */
1.20      millert    60: volatile sig_atomic_t  contimedout = FALSE;    /* Connection timed out */
1.1       dm         61: int                    proto_version = -1;     /* Protocol version */
                     62: int                    rtimeout = RTIMEOUT;    /* Response time out */
                     63: jmp_buf                        finish_jmpbuf;          /* Finish() jmp buffer */
1.2       dm         64: int                    setjmp_ok = FALSE;      /* setjmp()/longjmp() status */
1.1       dm         65: char                 **realargv;               /* Real main() argv */
                     66: int                    realargc;               /* Real main() argc */
                     67: opt_t                  options = 0;            /* Global install options */
1.20      millert    68: char                   defowner[64] = "bin";   /* Default owner */
                     69: char                   defgroup[64] = "bin";   /* Default group */
                     70:
                     71: static int sendcmdmsg(int, char *, size_t);
1.24      krw        72: static ssize_t remread(int, u_char *, size_t);
1.20      millert    73: static int remmore(void);
1.1       dm         74:
                     75: /*
                     76:  * Front end to write() that handles partial write() requests.
                     77:  */
1.20      millert    78: WRITE_RETURN_T
                     79: xwrite(int fd, void *buf, WRITE_AMT_T len)
1.1       dm         80: {
                     81:        WRITE_AMT_T nleft = len;
                     82:        WRITE_RETURN_T nwritten;
1.12      mpech      83:        char *ptr = buf;
1.1       dm         84:
                     85:        while (nleft > 0) {
                     86:                if ((nwritten = write(fd, ptr, nleft)) <= 0) {
                     87:                        return nwritten;
                     88:                }
                     89:                nleft -= nwritten;
                     90:                ptr += nwritten;
                     91:        }
                     92:
                     93:        return len;
                     94: }
                     95:
                     96: /*
                     97:  * Do run-time initialization
                     98:  */
1.20      millert    99: int
                    100: init(int argc, char **argv, char **envp)
1.1       dm        101: {
1.12      mpech     102:        int i;
1.1       dm        103:
                    104:        /*
                    105:         * Save a copy of our argc and argv before setargs() overwrites them
                    106:         */
                    107:        realargc = argc;
                    108:        realargv = (char **) xmalloc(sizeof(char *) * (argc+1));
                    109:        for (i = 0; i < argc; i++)
1.8       millert   110:                realargv[i] = xstrdup(argv[i]);
1.1       dm        111:
1.6       millert   112: #if    defined(SETARGS)
1.1       dm        113:        setargs_settup(argc, argv, envp);
                    114: #endif /* SETARGS */
                    115:
                    116:        pw = getpwuid(userid = getuid());
                    117:        if (pw == NULL) {
1.16      deraadt   118:                error("Your user id (%u) is not known to this system.",
1.1       dm        119:                      getuid());
                    120:                return(-1);
                    121:        }
                    122:
1.20      millert   123:        debugmsg(DM_MISC, "UserID = %u pwname = '%s' home = '%s'\n",
1.1       dm        124:                 userid, pw->pw_name, pw->pw_dir);
1.8       millert   125:        homedir = xstrdup(pw->pw_dir);
                    126:        locuser = xstrdup(pw->pw_name);
1.1       dm        127:        groupid = pw->pw_gid;
                    128:        gethostname(host, sizeof(host));
1.20      millert   129: #if 0
1.1       dm        130:        if ((cp = strchr(host, '.')) != NULL)
                    131:                *cp = CNULL;
1.20      millert   132: #endif
1.1       dm        133:
                    134:        /*
                    135:         * If we're not root, disable paranoid ownership checks
                    136:         * since normal users cannot chown() files.
                    137:         */
                    138:        if (!isserver && userid != 0) {
                    139:                FLAG_ON(options, DO_NOCHKOWNER);
                    140:                FLAG_ON(options, DO_NOCHKGROUP);
                    141:        }
                    142:
                    143:        return(0);
                    144: }
                    145:
                    146: /*
                    147:  * Finish things up before ending.
                    148:  */
1.20      millert   149: void
                    150: finish(void)
1.1       dm        151: {
                    152:        extern jmp_buf finish_jmpbuf;
                    153:
                    154:        debugmsg(DM_CALL,
                    155:                 "finish() called: do_fork = %d amchild = %d isserver = %d",
                    156:                 do_fork, amchild, isserver);
1.20      millert   157:        cleanup(0);
1.1       dm        158:
                    159:        /*
                    160:         * There's no valid finish_jmpbuf for the rdist master parent.
                    161:         */
                    162:        if (!do_fork || amchild || isserver) {
1.2       dm        163:
                    164:                if (!setjmp_ok) {
                    165: #ifdef DEBUG_SETJMP
                    166:                        error("attemping longjmp() without target");
                    167:                        abort();
                    168: #else
                    169:                        exit(1);
                    170: #endif
                    171:                }
                    172:
1.1       dm        173:                longjmp(finish_jmpbuf, 1);
                    174:                /*NOTREACHED*/
                    175:                error("Unexpected failure of longjmp() in finish()");
                    176:                exit(2);
                    177:        } else
                    178:                exit(1);
                    179: }
                    180:
                    181: /*
                    182:  * Handle lost connections
                    183:  */
1.20      millert   184: void
                    185: lostconn(void)
1.1       dm        186: {
                    187:        /* Prevent looping */
                    188:        (void) signal(SIGPIPE, SIG_IGN);
                    189:
                    190:        rem_r = rem_w = -1;     /* Ensure we don't try to send to server */
                    191:        checkhostname();
                    192:        error("Lost connection to %s",
                    193:              (currenthost) ? currenthost : "(unknown)");
                    194:
                    195:        finish();
                    196: }
                    197:
                    198: /*
                    199:  * General signal handler
                    200:  */
1.20      millert   201: void
                    202: sighandler(int sig)
1.1       dm        203: {
1.7       millert   204:        int save_errno = errno;
                    205:
1.26      deraadt   206:        /* XXX signal race */
1.1       dm        207:        debugmsg(DM_CALL, "sighandler() received signal %d\n", sig);
                    208:
                    209:        switch (sig) {
                    210:        case SIGALRM:
                    211:                contimedout = TRUE;
1.26      deraadt   212:                /* XXX signal race */
1.1       dm        213:                checkhostname();
                    214:                error("Response time out");
                    215:                finish();
                    216:                break;
                    217:
                    218:        case SIGPIPE:
1.26      deraadt   219:                /* XXX signal race */
1.1       dm        220:                lostconn();
                    221:                break;
                    222:
                    223:        case SIGFPE:
                    224:                debug = !debug;
                    225:                break;
                    226:
                    227:        case SIGHUP:
                    228:        case SIGINT:
                    229:        case SIGQUIT:
                    230:        case SIGTERM:
1.26      deraadt   231:                /* XXX signal race */
1.1       dm        232:                finish();
                    233:                break;
                    234:
                    235:        default:
1.26      deraadt   236:                /* XXX signal race */
1.1       dm        237:                fatalerr("No signal handler defined for signal %d.", sig);
                    238:        }
1.7       millert   239:        errno = save_errno;
1.1       dm        240: }
                    241:
                    242: /*
                    243:  * Function to actually send the command char and message to the
                    244:  * remote host.
                    245:  */
1.20      millert   246: static int
                    247: sendcmdmsg(int cmd, char *msg, size_t msgsize)
1.1       dm        248: {
                    249:        int len;
                    250:
                    251:        if (rem_w < 0)
                    252:                return(-1);
                    253:
                    254:        /*
                    255:         * All commands except C_NONE should have a newline
                    256:         */
                    257:        if (cmd != C_NONE && !strchr(msg + 1, '\n'))
1.18      millert   258:                (void) strlcat(msg + 1, "\n", msgsize - 1);
1.1       dm        259:
                    260:        if (cmd == C_NONE)
                    261:                len = strlen(msg);
                    262:        else {
                    263:                len = strlen(msg + 1) + 1;
                    264:                msg[0] = cmd;
                    265:        }
                    266:
                    267:        debugmsg(DM_PROTO, ">>> Cmd = %c (\\%3.3o) Msg = \"%.*s\"",
                    268:                 cmd, cmd,
                    269:                 (cmd == C_NONE) ? len-1 : len-2,
                    270:                 (cmd == C_NONE) ? msg : msg + 1);
                    271:
                    272:        return(!(xwrite(rem_w, msg, len) == len));
                    273: }
                    274:
                    275: /*
                    276:  * Send a command message to the remote host.
                    277:  * Called as sendcmd(char cmdchar, char *fmt, arg1, arg2, ...)
1.28      guenther  278:  * The fmt may be NULL, in which case there are no args.
1.1       dm        279:  */
1.20      millert   280: int
1.28      guenther  281: sendcmd(char cmd, const char *fmt, ...)
1.1       dm        282: {
                    283:        static char buf[BUFSIZ];
                    284:        va_list args;
                    285:
                    286:        va_start(args, fmt);
                    287:        if (fmt)
1.18      millert   288:                (void) vsnprintf(buf + (cmd != C_NONE),
1.20      millert   289:                                 sizeof(buf) - (cmd != C_NONE), fmt, args);
1.1       dm        290:        else
                    291:                buf[1] = CNULL;
                    292:        va_end(args);
                    293:
1.18      millert   294:        return(sendcmdmsg(cmd, buf, sizeof(buf)));
1.1       dm        295: }
                    296:
                    297: /*
                    298:  * Internal variables and routines for reading lines from the remote.
                    299:  */
                    300: static u_char rembuf[BUFSIZ];
                    301: static u_char *remptr;
1.24      krw       302: static ssize_t remleft;
1.1       dm        303:
                    304: #define remc() (--remleft < 0 ? remmore() : *remptr++)
                    305:
                    306: /*
                    307:  * Back end to remote read()
                    308:  */
1.24      krw       309: static ssize_t
                    310: remread(int fd, u_char *buf, size_t bufsiz)
1.1       dm        311: {
                    312:        return(read(fd, (char *)buf, bufsiz));
                    313: }
                    314:
1.20      millert   315: static int
                    316: remmore(void)
1.1       dm        317: {
                    318:        (void) signal(SIGALRM, sighandler);
                    319:        (void) alarm(rtimeout);
                    320:
                    321:        remleft = remread(rem_r, rembuf, sizeof(rembuf));
                    322:
                    323:        (void) alarm(0);
                    324:
                    325:        if (remleft < 0)
                    326:                return (-2);    /* error */
                    327:        if (remleft == 0)
                    328:                return (-1);    /* EOF */
                    329:        remptr = rembuf;
                    330:        remleft--;
                    331:        return (*remptr++);
                    332: }
                    333:
                    334: /*
                    335:  * Read an input line from the remote.  Return the number of bytes
                    336:  * stored (equivalent to strlen(p)).  If `cleanup' is set, EOF at
                    337:  * the beginning of a line is returned as EOF (-1); other EOFs, or
                    338:  * errors, call cleanup() or lostconn().  In other words, unless
                    339:  * the third argument is nonzero, this routine never returns failure.
                    340:  */
1.20      millert   341: int
                    342: remline(u_char *buffer, int space, int doclean)
1.1       dm        343: {
1.12      mpech     344:        int c, left = space;
                    345:        u_char *p = buffer;
1.1       dm        346:
                    347:        if (rem_r < 0) {
                    348:                error("Cannot read remote input: Remote descriptor not open.");
                    349:                return(-1);
                    350:        }
                    351:
                    352:        while (left > 0) {
                    353:                if ((c = remc()) < -1) {        /* error */
                    354:                        if (doclean) {
                    355:                                finish();
                    356:                                /*NOTREACHED*/
                    357:                        }
                    358:                        lostconn();
                    359:                        /*NOTREACHED*/
                    360:                }
                    361:                if (c == -1) {                  /* got EOF */
                    362:                        if (doclean) {
                    363:                                if (left == space)
                    364:                                        return (-1);/* signal proper EOF */
                    365:                                finish();       /* improper EOF */
                    366:                                /*NOTREACHED*/
                    367:                        }
                    368:                        lostconn();
                    369:                        /*NOTREACHED*/
                    370:                }
                    371:                if (c == '\n') {
                    372:                        *p = CNULL;
                    373:
                    374:                        if (debug) {
                    375:                                static char mbuf[BUFSIZ];
                    376:
1.20      millert   377:                                (void) snprintf(mbuf, sizeof(mbuf),
1.1       dm        378:                                        "<<< Cmd = %c (\\%3.3o) Msg = \"%s\"",
                    379:                                               buffer[0], buffer[0],
                    380:                                               buffer + 1);
                    381:
                    382:                                debugmsg(DM_PROTO, "%s", mbuf);
                    383:                        }
                    384:
                    385:                        return (space - left);
                    386:                }
                    387:                *p++ = c;
                    388:                left--;
                    389:        }
                    390:
                    391:        /* this will probably blow the entire session */
                    392:        error("remote input line too long");
                    393:        p[-1] = CNULL;          /* truncate */
                    394:        return (space);
                    395: }
                    396:
                    397: /*
                    398:  * Non-line-oriented remote read.
                    399:  */
1.24      krw       400: ssize_t
                    401: readrem(char *p, ssize_t space)
1.1       dm        402: {
                    403:        if (remleft <= 0) {
                    404:                /*
                    405:                 * Set remote time out alarm.
                    406:                 */
                    407:                (void) signal(SIGALRM, sighandler);
                    408:                (void) alarm(rtimeout);
                    409:
                    410:                remleft = remread(rem_r, rembuf, sizeof(rembuf));
                    411:
                    412:                (void) alarm(0);
                    413:                remptr = rembuf;
                    414:        }
                    415:
                    416:        if (remleft <= 0)
                    417:                return (remleft);
                    418:        if (remleft < space)
                    419:                space = remleft;
                    420:
1.20      millert   421:        memcpy(p, remptr, space);
1.1       dm        422:
                    423:        remptr += space;
                    424:        remleft -= space;
                    425:
                    426:        return (space);
                    427: }
                    428:
                    429: /*
                    430:  * Get the user name for the uid.
                    431:  */
1.20      millert   432: char *
1.29    ! guenther  433: getusername(uid_t uid, char *file, opt_t opts)
1.1       dm        434: {
                    435:        static char buf[100];
1.29    ! guenther  436:        static uid_t lastuid = (uid_t)-1;
1.1       dm        437:        struct passwd *pwd = NULL;
                    438:
                    439:        /*
                    440:         * The value of opts may have changed so we always
                    441:         * do the opts check.
                    442:         */
                    443:        if (IS_ON(opts, DO_NUMCHKOWNER)) {
1.20      millert   444:                (void) snprintf(buf, sizeof(buf), ":%u", uid);
1.1       dm        445:                return(buf);
                    446:        }
                    447:
                    448:        /*
                    449:         * Try to avoid getpwuid() call.
                    450:         */
1.6       millert   451:        if (lastuid == uid && buf[0] != '\0' && buf[0] != ':')
1.1       dm        452:                return(buf);
                    453:
                    454:        lastuid = uid;
                    455:
                    456:        if ((pwd = getpwuid(uid)) == NULL) {
1.20      millert   457:                if (IS_ON(opts, DO_DEFOWNER) && !isserver)
                    458:                        (void) strlcpy(buf, defowner, sizeof(buf));
                    459:                else {
                    460:                        message(MT_WARNING,
                    461:                                "%s: No password entry for uid %u", file, uid);
                    462:                        (void) snprintf(buf, sizeof(buf), ":%u", uid);
                    463:                }
                    464:        } else {
                    465:                (void) strlcpy(buf, pwd->pw_name, sizeof(buf));
                    466:        }
1.1       dm        467:
                    468:        return(buf);
                    469: }
                    470:
                    471: /*
                    472:  * Get the group name for the gid.
                    473:  */
1.20      millert   474: char *
1.29    ! guenther  475: getgroupname(gid_t gid, char *file, opt_t opts)
1.1       dm        476: {
                    477:        static char buf[100];
1.29    ! guenther  478:        static gid_t lastgid = (gid_t)-1;
1.1       dm        479:        struct group *grp = NULL;
                    480:
                    481:        /*
                    482:         * The value of opts may have changed so we always
                    483:         * do the opts check.
                    484:         */
                    485:        if (IS_ON(opts, DO_NUMCHKGROUP)) {
1.20      millert   486:                (void) snprintf(buf, sizeof(buf), ":%u", gid);
1.1       dm        487:                return(buf);
                    488:        }
                    489:
                    490:        /*
                    491:         * Try to avoid getgrgid() call.
                    492:         */
1.6       millert   493:        if (lastgid == gid && buf[0] != '\0' && buf[0] != ':')
1.1       dm        494:                return(buf);
                    495:
                    496:        lastgid = gid;
                    497:
                    498:        if ((grp = (struct group *)getgrgid(gid)) == NULL) {
1.20      millert   499:                if (IS_ON(opts, DO_DEFGROUP) && !isserver)
                    500:                        (void) strlcpy(buf, defgroup, sizeof(buf));
                    501:                else {
                    502:                        message(MT_WARNING, "%s: No name for group %u",
                    503:                                file, gid);
                    504:                        (void) snprintf(buf, sizeof(buf), ":%u", gid);
                    505:                }
1.1       dm        506:        } else
1.20      millert   507:                (void) strlcpy(buf, grp->gr_name, sizeof(buf));
1.1       dm        508:
                    509:        return(buf);
                    510: }
                    511:
                    512: /*
                    513:  * Read a response from the remote host.
                    514:  */
1.20      millert   515: int
                    516: response(void)
1.1       dm        517: {
                    518:        static u_char resp[BUFSIZ];
                    519:        u_char *s;
                    520:        int n;
                    521:
                    522:        debugmsg(DM_CALL, "response() start\n");
                    523:
                    524:        n = remline(s = resp, sizeof(resp), 0);
                    525:
                    526:        n--;
                    527:        switch (*s++) {
                    528:         case C_ACK:
                    529:                debugmsg(DM_PROTO, "received ACK\n");
                    530:                return(0);
                    531:        case C_LOGMSG:
                    532:                if (n > 0) {
                    533:                        message(MT_CHANGE, "%s", s);
                    534:                        return(1);
                    535:                }
                    536:                debugmsg(DM_PROTO, "received EMPTY logmsg\n");
                    537:                return(0);
                    538:        case C_NOTEMSG:
                    539:                if (s)
                    540:                        message(MT_NOTICE, "%s", s);
                    541:                return(response());
                    542:
                    543:        default:
                    544:                s--;
                    545:                n++;
                    546:                /* fall into... */
                    547:
                    548:        case C_ERRMSG:  /* Normal error message */
                    549:                if (s)
                    550:                        message(MT_NERROR, "%s", s);
                    551:                return(-1);
                    552:
                    553:        case C_FERRMSG: /* Fatal error message */
                    554:                if (s)
                    555:                        message(MT_FERROR, "%s", s);
                    556:                finish();
1.20      millert   557:                return(-1);
1.1       dm        558:        }
                    559:        /*NOTREACHED*/
                    560: }
                    561:
                    562: /*
                    563:  * This should be in expand.c but the other routines call other modules
                    564:  * that we don't want to load in.
                    565:  *
                    566:  * Expand file names beginning with `~' into the
                    567:  * user's home directory path name. Return a pointer in buf to the
                    568:  * part corresponding to `file'.
                    569:  */
1.20      millert   570: char *
                    571: exptilde(char *ebuf, char *file, size_t ebufsize)
1.1       dm        572: {
1.18      millert   573:        char *pw_dir, *rest;
                    574:        size_t len;
1.1       dm        575:        extern char *homedir;
                    576:
                    577:        if (*file != '~') {
1.18      millert   578: notilde:
                    579:                (void) strlcpy(ebuf, file, ebufsize);
1.1       dm        580:                return(ebuf);
                    581:        }
                    582:        if (*++file == CNULL) {
1.18      millert   583:                pw_dir = homedir;
                    584:                rest = NULL;
1.1       dm        585:        } else if (*file == '/') {
1.18      millert   586:                pw_dir = homedir;
                    587:                rest = file;
1.1       dm        588:        } else {
1.18      millert   589:                rest = file;
                    590:                while (*rest && *rest != '/')
                    591:                        rest++;
                    592:                if (*rest == '/')
                    593:                        *rest = CNULL;
1.1       dm        594:                else
1.18      millert   595:                        rest = NULL;
1.1       dm        596:                if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
                    597:                        if ((pw = getpwnam(file)) == NULL) {
                    598:                                error("%s: unknown user name", file);
1.18      millert   599:                                if (rest != NULL)
                    600:                                        *rest = '/';
1.1       dm        601:                                return(NULL);
                    602:                        }
                    603:                }
1.18      millert   604:                if (rest != NULL)
                    605:                        *rest = '/';
                    606:                pw_dir = pw->pw_dir;
                    607:        }
                    608:        if ((len = strlcpy(ebuf, pw_dir, ebufsize)) >= ebufsize)
                    609:                goto notilde;
                    610:        pw_dir = ebuf + len;
                    611:        if (rest != NULL) {
                    612:                pw_dir++;
                    613:                if ((len = strlcat(ebuf, rest, ebufsize)) >= ebufsize)
                    614:                        goto notilde;
1.1       dm        615:        }
1.18      millert   616:        return(pw_dir);
1.1       dm        617: }
                    618:
                    619: #if    defined(DIRECT_RCMD)
                    620: /*
                    621:  * Set our effective user id to the user running us.
                    622:  * This should be the uid we do most of our work as.
                    623:  */
1.20      millert   624: int
                    625: becomeuser(void)
1.1       dm        626: {
                    627:        int r = 0;
                    628:
                    629: #if    defined(HAVE_SAVED_IDS)
                    630:        r = seteuid(userid);
                    631: #else
                    632:        r = setreuid(0, userid);
                    633: #endif /* HAVE_SAVED_IDS */
                    634:
                    635:        if (r < 0)
1.20      millert   636:                error("becomeuser %u failed: %s (ruid = %u euid = %u)",
1.1       dm        637:                      userid, SYSERR, getuid(), geteuid());
                    638:
                    639:        return(r);
                    640: }
                    641: #endif /* DIRECT_RCMD */
                    642:
                    643: #if    defined(DIRECT_RCMD)
                    644: /*
                    645:  * Set our effective user id to "root" (uid = 0)
                    646:  */
1.20      millert   647: int
                    648: becomeroot(void)
1.1       dm        649: {
                    650:        int r = 0;
                    651:
                    652: #if    defined(HAVE_SAVED_IDS)
                    653:        r = seteuid(0);
                    654: #else
                    655:        r = setreuid(userid, 0);
                    656: #endif /* HAVE_SAVED_IDS */
                    657:
                    658:        if (r < 0)
1.16      deraadt   659:                error("becomeroot failed: %s (ruid = %u euid = %u)",
1.1       dm        660:                      SYSERR, getuid(), geteuid());
                    661:
                    662:        return(r);
                    663: }
                    664: #endif /* DIRECT_RCMD */
                    665:
                    666: /*
                    667:  * Set access and modify times of a given file
                    668:  */
1.20      millert   669: int
                    670: setfiletime(char *file, time_t atime, time_t mtime)
1.1       dm        671: {
                    672: #if    SETFTIME_TYPE == SETFTIME_UTIMES
                    673:        struct timeval tv[2];
                    674:
                    675:        if (atime != 0 && mtime != 0) {
                    676:                tv[0].tv_sec = atime;
                    677:                tv[1].tv_sec = mtime;
                    678:                tv[0].tv_usec = tv[1].tv_usec = (time_t) 0;
                    679:                return(utimes(file, tv));
                    680:        } else  /* Set to current time */
1.6       millert   681:                return(utimes(file, NULL));
1.1       dm        682:
                    683: #endif /* SETFTIME_UTIMES */
                    684:
                    685: #if    SETFTIME_TYPE == SETFTIME_UTIME
                    686:        struct utimbuf utbuf;
                    687:
                    688:        if (atime != 0 && mtime != 0) {
                    689:                utbuf.actime = atime;
                    690:                utbuf.modtime = mtime;
                    691:                return(utime(file, &utbuf));
                    692:        } else  /* Set to current time */
1.6       millert   693:                return(utime(file, NULL));
1.1       dm        694: #endif /* SETFTIME_UTIME */
                    695:
                    696: #if    !defined(SETFTIME_TYPE)
                    697:        There is no "SETFTIME_TYPE" defined!
                    698: #endif /* SETFTIME_TYPE */
                    699: }
                    700:
                    701: /*
                    702:  * Get version info
                    703:  */
1.20      millert   704: char *
                    705: getversion(void)
1.1       dm        706: {
                    707:        static char buff[BUFSIZ];
                    708:
1.20      millert   709:        (void) snprintf(buff, sizeof(buff),
1.1       dm        710:        "Version %s.%d (%s) - Protocol Version %d, Release %s, Patch level %d",
                    711:                       DISTVERSION, PATCHLEVEL, DISTSTATUS,
                    712:                       VERSION, DISTVERSION, PATCHLEVEL);
                    713:
                    714:        return(buff);
                    715: }
                    716:
                    717: /*
                    718:  * Execute a shell command to handle special cases.
                    719:  * This is now common to both server and client
                    720:  */
1.20      millert   721: void
                    722: runcommand(char *cmd)
1.1       dm        723: {
1.20      millert   724:        ssize_t nread;
                    725:        pid_t pid, wpid;
1.12      mpech     726:        char *cp, *s;
1.1       dm        727:        char sbuf[BUFSIZ], buf[BUFSIZ];
1.20      millert   728:        int fd[2], status;
                    729:
1.1       dm        730:        if (pipe(fd) < 0) {
                    731:                error("pipe of %s failed: %s", cmd, SYSERR);
                    732:                return;
                    733:        }
                    734:
                    735:        if ((pid = fork()) == 0) {
                    736:                /*
                    737:                 * Return everything the shell commands print.
                    738:                 */
                    739:                (void) close(0);
                    740:                (void) close(1);
                    741:                (void) close(2);
                    742:                (void) open(_PATH_DEVNULL, O_RDONLY);
                    743:                (void) dup(fd[PIPE_WRITE]);
                    744:                (void) dup(fd[PIPE_WRITE]);
                    745:                (void) close(fd[PIPE_READ]);
                    746:                (void) close(fd[PIPE_WRITE]);
1.11      deraadt   747:                (void) execl(_PATH_BSHELL, "sh", "-c", cmd, (char *)NULL);
1.1       dm        748:                _exit(127);
                    749:        }
                    750:        (void) close(fd[PIPE_WRITE]);
                    751:        s = sbuf;
                    752:        *s++ = C_LOGMSG;
1.20      millert   753:        while ((nread = read(fd[PIPE_READ], buf, sizeof(buf))) > 0) {
1.1       dm        754:                cp = buf;
                    755:                do {
                    756:                        *s++ = *cp++;
                    757:                        if (cp[-1] != '\n') {
                    758:                                if (s < (char *) &sbuf[sizeof(sbuf)-1])
                    759:                                        continue;
                    760:                                *s++ = '\n';
                    761:                        }
                    762:                        /*
                    763:                         * Throw away blank lines.
                    764:                         */
                    765:                        if (s == &sbuf[2]) {
                    766:                                s--;
                    767:                                continue;
                    768:                        }
                    769:                        if (isserver)
                    770:                                (void) xwrite(rem_w, sbuf, s - sbuf);
                    771:                        else {
                    772:                                *s = CNULL;
                    773:                                message(MT_INFO, "%s", sbuf+1);
                    774:                        }
                    775:                        s = &sbuf[1];
1.20      millert   776:                } while (--nread);
1.1       dm        777:        }
                    778:        if (s > (char *) &sbuf[1]) {
                    779:                *s++ = '\n';
                    780:                if (isserver)
                    781:                        (void) xwrite(rem_w, sbuf, s - sbuf);
                    782:                else {
                    783:                        *s = CNULL;
                    784:                        message(MT_INFO, "%s", sbuf+1);
                    785:                }
                    786:        }
1.20      millert   787:        while ((wpid = wait(&status)) != pid && wpid != -1)
1.1       dm        788:                ;
1.20      millert   789:        if (wpid == -1)
1.1       dm        790:                status = -1;
                    791:        (void) close(fd[PIPE_READ]);
                    792:        if (status)
                    793:                error("shell returned %d", status);
                    794:        else if (isserver)
                    795:                ack();
                    796: }
                    797:
                    798: /*
                    799:  * Malloc with error checking
                    800:  */
1.27      guenther  801: void *
1.20      millert   802: xmalloc(size_t amt)
1.1       dm        803: {
1.27      guenther  804:        void *ptr;
1.1       dm        805:
1.27      guenther  806:        if ((ptr = malloc(amt)) == NULL)
1.25      krw       807:                fatalerr("Cannot malloc %zu bytes of memory.", amt);
1.1       dm        808:
1.27      guenther  809:        return (ptr);
1.1       dm        810: }
                    811:
                    812: /*
                    813:  * realloc with error checking
                    814:  */
1.27      guenther  815: void *
                    816: xrealloc(void *baseptr, size_t amt)
1.1       dm        817: {
1.27      guenther  818:        void *new;
1.1       dm        819:
1.27      guenther  820:        if ((new = realloc(baseptr, amt)) == NULL)
1.25      krw       821:                fatalerr("Cannot realloc %zu bytes of memory.", amt);
1.1       dm        822:
1.27      guenther  823:        return (new);
1.1       dm        824: }
                    825:
                    826: /*
                    827:  * calloc with error checking
                    828:  */
1.27      guenther  829: void *
1.20      millert   830: xcalloc(size_t num, size_t esize)
1.1       dm        831: {
1.27      guenther  832:        void *ptr;
1.1       dm        833:
1.27      guenther  834:        if ((ptr = calloc(num, esize)) == NULL)
1.25      krw       835:                fatalerr("Cannot calloc %zu * %zu = %zu bytes of memory.",
1.1       dm        836:                      num, esize, num * esize);
                    837:
1.27      guenther  838:        return (ptr);
1.8       millert   839: }
                    840:
                    841: /*
                    842:  * Strdup with error checking
                    843:  */
1.20      millert   844: char *
                    845: xstrdup(const char *str)
1.8       millert   846: {
1.20      millert   847:        size_t len = strlen(str) + 1;
1.27      guenther  848:        char *nstr = xmalloc(len);
1.8       millert   849:
1.27      guenther  850:        return (memcpy(nstr, str, len));
1.1       dm        851: }
                    852:
                    853: /*
                    854:  * Private version of basename()
                    855:  */
1.20      millert   856: char *
                    857: xbasename(char *path)
1.1       dm        858: {
1.12      mpech     859:        char *cp;
1.1       dm        860:
1.20      millert   861:        if ((cp = strrchr(path, '/')) != NULL)
1.1       dm        862:                return(cp+1);
                    863:        else
                    864:                return(path);
                    865: }
                    866:
                    867: /*
1.10      provos    868:  * Take a colon (':') separated path to a file and
1.1       dm        869:  * search until a component of that path is found and
                    870:  * return the found file name.
                    871:  */
1.20      millert   872: char *
                    873: searchpath(char *path)
1.1       dm        874: {
1.12      mpech     875:        char *file;
1.19      millert   876:        char *space;
                    877:        int found;
1.1       dm        878:        struct stat statbuf;
                    879:
1.19      millert   880:        for (found = 0; !found && (file = strsep(&path, ":")) != NULL; ) {
                    881:                if ((space = strchr(file, ' ')) != NULL)
                    882:                        *space = CNULL;
                    883:                found = stat(file, &statbuf) == 0;
                    884:                if (space)
                    885:                        *space = ' ';           /* Put back what we zapped */
1.1       dm        886:        }
1.19      millert   887:        return (file);
1.1       dm        888: }