3 * This is an rcmd() replacement originally by
4 * Chris Siebenmann <cks@utcc.utoronto.ca>.
8 static const char rcsid[] =
9 "$FreeBSD: src/usr.bin/rdist/rshrcmd.c,v 1.6 1999/08/28 01:05:08 peter Exp $";
14 #if !defined(DIRECT_RCMD)
16 #include <sys/socket.h>
27 ret = strrchr(s, '/');
35 * This is a replacement rcmd() function that uses the rsh(1c)
36 * program in place of a direct rcmd() function call so as to
37 * avoid having to be root.
40 rshrcmd(ahost, port, luser, ruser, cmd, fd2p)
43 char *luser, *ruser, *cmd;
50 /* insure that we are indeed being used as we thought. */
53 /* validate remote hostname. */
54 hp = gethostbyname(*ahost);
56 error("%s: unknown host", *ahost);
59 /* *ahost = hp->h_name; *//* This makes me nervous. */
61 /* get a socketpair we'll use for stdin and stdout. */
62 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) {
63 error("socketpair(AF_UNIX, SOCK_STREAM, 0) failed: %s.",
70 error("fork failed: %s.", strerror(errno));
71 return -1; /* error. */
74 /* child. we use sp[1] to be stdin/stdout, and close
77 if (dup2(sp[1], 0) < 0 || dup2(0,1) < 0) {
78 error("dup2 failed: %s.", strerror(errno));
81 /* fork again to lose parent. */
84 error("fork to lose parent failed: %s.", strerror(errno));
89 /* in grandchild here. */
92 * If we are rdist'ing to "localhost" as the same user
93 * as we are, then avoid running remote shell for efficiency.
95 if (strcmp(*ahost, "localhost") == 0 &&
96 strcmp(luser, ruser) == 0) {
97 execlp(_PATH_BSHELL, xbasename(_PATH_BSHELL), "-c",
99 error("execlp %s failed: %s.", _PATH_BSHELL, strerror(errno));
101 execlp(path_rsh, xbasename(path_rsh),
102 *ahost, "-l", ruser, cmd, (char *) NULL);
103 error("execlp %s failed: %s.", path_rsh,
109 /* parent. close sp[1], return sp[0]. */
119 #endif /* !DIRECT_RCMD */