rcmdsh.c revision 86214
1/*	$OpenBSD: rcmdsh.c,v 1.5 1998/04/25 16:23:58 millert Exp $	*/
2
3/*
4 * This is an rcmd() replacement originally by
5 * Chris Siebenmann <cks@utcc.utoronto.ca>.
6 */
7
8#include <sys/cdefs.h>
9__FBSDID("$FreeBSD: head/lib/libc/net/rcmdsh.c 86214 2001-11-09 15:19:25Z ru $");
10
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <sys/wait.h>
14
15#include <errno.h>
16#include <netdb.h>
17#include <paths.h>
18#include <pwd.h>
19#include <stdio.h>
20#include <string.h>
21#include <unistd.h>
22
23#ifndef _PATH_RSH
24#define	_PATH_RSH	"/usr/bin/rsh"
25#endif
26
27/*
28 * This is a replacement rcmd() function that uses the rsh(1)
29 * program in place of a direct rcmd(3) function call so as to
30 * avoid having to be root.  Note that rport is ignored.
31 */
32/* ARGSUSED */
33int
34rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog)
35	char **ahost;
36	int rport __unused;
37	const char *locuser, *remuser, *cmd, *rshprog;
38{
39	struct hostent *hp;
40	int cpid, sp[2];
41	char *p;
42	struct passwd *pw;
43
44	/* What rsh/shell to use. */
45	if (rshprog == NULL)
46		rshprog = _PATH_RSH;
47
48	/* locuser must exist on this host. */
49	if ((pw = getpwnam(locuser)) == NULL) {
50		(void)fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
51		return (-1);
52	}
53
54	/* Validate remote hostname. */
55	if (strcmp(*ahost, "localhost") != 0) {
56		if ((hp = gethostbyname(*ahost)) == NULL) {
57			herror(*ahost);
58			return (-1);
59		}
60		*ahost = hp->h_name;
61	}
62
63	/* Get a socketpair we'll use for stdin and stdout. */
64	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
65		perror("rcmdsh: socketpair");
66		return (-1);
67	}
68
69	cpid = fork();
70	if (cpid == -1) {
71		perror("rcmdsh: fork failed");
72		return (-1);
73	} else if (cpid == 0) {
74		/*
75		 * Child.  We use sp[1] to be stdin/stdout, and close sp[0].
76		 */
77		(void)close(sp[0]);
78		if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) {
79			perror("rcmdsh: dup2 failed");
80			_exit(255);
81		}
82		/* Fork again to lose parent. */
83		cpid = fork();
84		if (cpid == -1) {
85			perror("rcmdsh: fork to lose parent failed");
86			_exit(255);
87		}
88		if (cpid > 0)
89			_exit(0);
90
91		/* In grandchild here.  Become local user for rshprog. */
92		if (setuid(pw->pw_uid) == -1) {
93			(void)fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
94			    pw->pw_uid, strerror(errno));
95			_exit(255);
96		}
97
98		/*
99		 * If remote host is "localhost" and local and remote users
100		 * are the same, avoid running remote shell for efficiency.
101		 */
102		if (strcmp(*ahost, "localhost") == 0 &&
103		    strcmp(locuser, remuser) == 0) {
104			if (pw->pw_shell[0] == '\0')
105				rshprog = _PATH_BSHELL;
106			else
107				rshprog = pw->pw_shell;
108			p = strrchr(rshprog, '/');
109			execlp(rshprog, p ? p + 1 : rshprog, "-c", cmd,
110			    (char *)NULL);
111		} else {
112			p = strrchr(rshprog, '/');
113			execlp(rshprog, p ? p + 1 : rshprog, *ahost, "-l",
114			    remuser, cmd, (char *)NULL);
115		}
116		(void)fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
117		    rshprog, strerror(errno));
118		_exit(255);
119	} else {
120		/* Parent. close sp[1], return sp[0]. */
121		(void)close(sp[1]);
122		/* Reap child. */
123		(void)wait(NULL);
124		return (sp[0]);
125	}
126	/* NOTREACHED */
127}
128