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

Annotation of src/usr.bin/rdist/rshrcmd.c, Revision 1.6

1.6     ! millert     1: /*     $OpenBSD: rshrcmd.c,v 1.5 1996/09/07 03:14:27 millert Exp $     */
1.3       deraadt     2:
1.1       dm          3:
                      4: /*
                      5:  * This is an rcmd() replacement originally by
                      6:  * Chris Siebenmann <cks@utcc.utoronto.ca>.
                      7:  */
                      8:
                      9: #ifndef lint
                     10: static char RCSid[] =
1.6     ! millert    11: "$OpenBSD: rshrcmd.c,v 1.5 1996/09/07 03:14:27 millert Exp $";
1.1       dm         12: #endif
                     13:
1.5       millert    14: #include       "defs.h"
1.1       dm         15:
                     16: #if    !defined(DIRECT_RCMD)
                     17:
                     18: #include      <sys/types.h>
                     19: #include      <sys/socket.h>
                     20: #include      <sys/wait.h>
                     21: #include      <signal.h>
                     22: #include      <errno.h>
                     23: #include      <netdb.h>
                     24: #include      <stdio.h>
                     25:
                     26: /*
                     27:  * This is a replacement rcmd() function that uses the rsh(1c)
                     28:  * program in place of a direct rcmd() function call so as to
                     29:  * avoid having to be root.
                     30:  */
                     31: rshrcmd(ahost, port, luser, ruser, cmd, fd2p)
                     32:        char    **ahost;
                     33:        u_short port;
                     34:        char    *luser, *ruser, *cmd;
                     35:        int     *fd2p;
                     36: {
                     37:        int             cpid;
                     38:        struct hostent  *hp;
                     39:        int             sp[2];
                     40:
                     41:        /* insure that we are indeed being used as we thought. */
                     42:        if (fd2p != 0)
                     43:                return -1;
                     44:        /* validate remote hostname. */
                     45:        hp = gethostbyname(*ahost);
                     46:        if (hp == 0) {
                     47:                error("%s: unknown host", *ahost);
                     48:                return -1;
                     49:        }
                     50:        /* *ahost = hp->h_name; /* This makes me nervous. */
                     51:
                     52:        /* get a socketpair we'll use for stdin and stdout. */
                     53:        if (getsocketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) {
                     54:                error("socketpair(AF_UNIX, SOCK_STREAM, 0) failed: %s.",
                     55:                      SYSERR);
                     56:                return -1;
                     57:        }
                     58:
                     59:        cpid = fork();
                     60:        if (cpid < 0) {
                     61:                error("fork failed: %s.", SYSERR);
                     62:                return -1;      /* error. */
                     63:        }
                     64:        if (cpid == 0) {
                     65:                /* child. we use sp[1] to be stdin/stdout, and close
                     66:                   sp[0]. */
                     67:                (void) close(sp[0]);
1.6     ! millert    68:                if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) {
1.1       dm         69:                        error("dup2 failed: %s.", SYSERR);
                     70:                        _exit(255);
                     71:                }
                     72:                /* fork again to lose parent. */
                     73:                cpid = fork();
                     74:                if (cpid < 0) {
                     75:                        error("fork to lose parent failed: %s.", SYSERR);
                     76:                        _exit(255);
                     77:                }
                     78:                if (cpid > 0)
                     79:                        _exit(0);
                     80:                /* in grandchild here. */
                     81:
                     82:                /*
                     83:                 * If we are rdist'ing to "localhost" as the same user
                     84:                 * as we are, then avoid running remote shell for efficiency.
                     85:                 */
                     86:                if (strcmp(*ahost, "localhost") == 0 &&
                     87:                    strcmp(luser, ruser) == 0) {
                     88:                        execlp(_PATH_BSHELL, xbasename(_PATH_BSHELL), "-c",
                     89:                               cmd, (char *) NULL);
                     90:                        error("execlp %s failed: %s.", _PATH_BSHELL, SYSERR);
                     91:                } else {
                     92:                        execlp(path_remsh, xbasename(path_remsh),
                     93:                               *ahost, "-l", ruser, cmd, (char *) NULL);
                     94:                        error("execlp %s failed: %s.", path_remsh, SYSERR);
                     95:                }
                     96:                _exit(255);
                     97:        }
                     98:        if (cpid > 0) {
                     99:                /* parent. close sp[1], return sp[0]. */
                    100:                (void) close(sp[1]);
                    101:                /* reap child. */
                    102:                (void) wait(0);
                    103:                return sp[0];
                    104:        }
                    105:        /*NOTREACHED*/
                    106: }
                    107:
                    108: #endif /* !DIRECT_RCMD */