3 * This is an rcmd() replacement originally by
4 * Chris Siebenmann <cks@utcc.utoronto.ca>.
6 * $FreeBSD: src/usr.bin/rdist/rshrcmd.c,v 1.6 1999/08/28 01:05:08 peter Exp $
7 * $DragonFly: src/usr.bin/rdist/rshrcmd.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
12 #if !defined(DIRECT_RCMD)
14 #include <sys/socket.h>
25 ret = strrchr(s, '/');
33 * This is a replacement rcmd() function that uses the rsh(1c)
34 * program in place of a direct rcmd() function call so as to
35 * avoid having to be root.
38 rshrcmd(ahost, port, luser, ruser, cmd, fd2p)
41 char *luser, *ruser, *cmd;
48 /* insure that we are indeed being used as we thought. */
51 /* validate remote hostname. */
52 hp = gethostbyname(*ahost);
54 error("%s: unknown host", *ahost);
57 /* *ahost = hp->h_name; *//* This makes me nervous. */
59 /* get a socketpair we'll use for stdin and stdout. */
60 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) {
61 error("socketpair(AF_UNIX, SOCK_STREAM, 0) failed: %s.",
68 error("fork failed: %s.", strerror(errno));
69 return -1; /* error. */
72 /* child. we use sp[1] to be stdin/stdout, and close
75 if (dup2(sp[1], 0) < 0 || dup2(0,1) < 0) {
76 error("dup2 failed: %s.", strerror(errno));
79 /* fork again to lose parent. */
82 error("fork to lose parent failed: %s.", strerror(errno));
87 /* in grandchild here. */
90 * If we are rdist'ing to "localhost" as the same user
91 * as we are, then avoid running remote shell for efficiency.
93 if (strcmp(*ahost, "localhost") == 0 &&
94 strcmp(luser, ruser) == 0) {
95 execlp(_PATH_BSHELL, xbasename(_PATH_BSHELL), "-c",
97 error("execlp %s failed: %s.", _PATH_BSHELL, strerror(errno));
99 execlp(path_rsh, xbasename(path_rsh),
100 *ahost, "-l", ruser, cmd, (char *) NULL);
101 error("execlp %s failed: %s.", path_rsh,
107 /* parent. close sp[1], return sp[0]. */
117 #endif /* !DIRECT_RCMD */