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 $
10 #include <sys/socket.h>
18 #if !defined(DIRECT_RCMD)
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(char **ahost
, u_short port
, char *luser
, char *ruser
, char *cmd
, int *fd2p
)
43 /* insure that we are indeed being used as we thought. */
46 /* validate remote hostname. */
47 hp
= gethostbyname(*ahost
);
49 error("%s: unknown host", *ahost
);
53 /* get a socketpair we'll use for stdin and stdout. */
54 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sp
) < 0) {
55 error("socketpair(AF_UNIX, SOCK_STREAM, 0) failed: %s.",
62 error("fork failed: %s.", strerror(errno
));
63 return -1; /* error. */
66 /* child. we use sp[1] to be stdin/stdout, and close
69 if (dup2(sp
[1], 0) < 0 || dup2(0,1) < 0) {
70 error("dup2 failed: %s.", strerror(errno
));
73 /* fork again to lose parent. */
76 error("fork to lose parent failed: %s.", strerror(errno
));
81 /* in grandchild here. */
84 * If we are rdist'ing to "localhost" as the same user
85 * as we are, then avoid running remote shell for efficiency.
87 if (strcmp(*ahost
, "localhost") == 0 &&
88 strcmp(luser
, ruser
) == 0) {
89 execlp(_PATH_BSHELL
, xbasename(_PATH_BSHELL
), "-c",
91 error("execlp %s failed: %s.", _PATH_BSHELL
, strerror(errno
));
93 execlp(path_rsh
, xbasename(path_rsh
),
94 *ahost
, "-l", ruser
, cmd
, NULL
);
95 error("execlp %s failed: %s.", path_rsh
,
101 /* parent. close sp[1], return sp[0]. */
111 #endif /* !DIRECT_RCMD */