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.3 2004/07/24 19:45:10 eirikn Exp $
10 #include <sys/types.h>
11 #include <sys/socket.h>
19 #if !defined(DIRECT_RCMD)
26 ret
= strrchr(s
, '/');
34 * This is a replacement rcmd() function that uses the rsh(1c)
35 * program in place of a direct rcmd() function call so as to
36 * avoid having to be root.
39 rshrcmd(char **ahost
, u_short port
, char *luser
, char *ruser
, char *cmd
, int *fd2p
)
44 /* insure that we are indeed being used as we thought. */
47 /* validate remote hostname. */
48 hp
= gethostbyname(*ahost
);
50 error("%s: unknown host", *ahost
);
54 /* get a socketpair we'll use for stdin and stdout. */
55 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sp
) < 0) {
56 error("socketpair(AF_UNIX, SOCK_STREAM, 0) failed: %s.",
63 error("fork failed: %s.", strerror(errno
));
64 return -1; /* error. */
67 /* child. we use sp[1] to be stdin/stdout, and close
70 if (dup2(sp
[1], 0) < 0 || dup2(0,1) < 0) {
71 error("dup2 failed: %s.", strerror(errno
));
74 /* fork again to lose parent. */
77 error("fork to lose parent failed: %s.", strerror(errno
));
82 /* in grandchild here. */
85 * If we are rdist'ing to "localhost" as the same user
86 * as we are, then avoid running remote shell for efficiency.
88 if (strcmp(*ahost
, "localhost") == 0 &&
89 strcmp(luser
, ruser
) == 0) {
90 execlp(_PATH_BSHELL
, xbasename(_PATH_BSHELL
), "-c",
92 error("execlp %s failed: %s.", _PATH_BSHELL
, strerror(errno
));
94 execlp(path_rsh
, xbasename(path_rsh
),
95 *ahost
, "-l", ruser
, cmd
, NULL
);
96 error("execlp %s failed: %s.", path_rsh
,
102 /* parent. close sp[1], return sp[0]. */
112 #endif /* !DIRECT_RCMD */