rcmdsh.c revision 94757
1/*	$OpenBSD: rcmdsh.c,v 1.5 1998/04/25 16:23:58 millert Exp $	*/
2
3/*
4 * Copyright (c) 2001, MagniComp
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in
14 *    the documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the MagniComp nor the names of its contributors may
16 *    be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * This is an rcmd() replacement originally by
33 * Chris Siebenmann <cks@utcc.utoronto.ca>.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/lib/libc/net/rcmdsh.c 94757 2002-04-15 12:46:07Z nectar $");
38
39#include <sys/types.h>
40#include <sys/socket.h>
41#include <sys/wait.h>
42
43#include <errno.h>
44#include <netdb.h>
45#include <paths.h>
46#include <pwd.h>
47#include <stdio.h>
48#include <string.h>
49#include <unistd.h>
50
51#ifndef _PATH_RSH
52#define	_PATH_RSH	"/usr/bin/rsh"
53#endif
54
55/*
56 * This is a replacement rcmd() function that uses the rsh(1)
57 * program in place of a direct rcmd(3) function call so as to
58 * avoid having to be root.  Note that rport is ignored.
59 */
60/* ARGSUSED */
61int
62rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog)
63	char **ahost;
64	int rport __unused;
65	const char *locuser, *remuser, *cmd, *rshprog;
66{
67	struct hostent *hp;
68	int cpid, sp[2];
69	char *p;
70	struct passwd *pw;
71
72	/* What rsh/shell to use. */
73	if (rshprog == NULL)
74		rshprog = _PATH_RSH;
75
76	/* locuser must exist on this host. */
77	if ((pw = getpwnam(locuser)) == NULL) {
78		(void)fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
79		return (-1);
80	}
81
82	/* Validate remote hostname. */
83	if (strcmp(*ahost, "localhost") != 0) {
84		if ((hp = gethostbyname(*ahost)) == NULL) {
85			herror(*ahost);
86			return (-1);
87		}
88		*ahost = hp->h_name;
89	}
90
91	/* Get a socketpair we'll use for stdin and stdout. */
92	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
93		perror("rcmdsh: socketpair");
94		return (-1);
95	}
96
97	cpid = fork();
98	if (cpid == -1) {
99		perror("rcmdsh: fork failed");
100		return (-1);
101	} else if (cpid == 0) {
102		/*
103		 * Child.  We use sp[1] to be stdin/stdout, and close sp[0].
104		 */
105		(void)close(sp[0]);
106		if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) {
107			perror("rcmdsh: dup2 failed");
108			_exit(255);
109		}
110		/* Fork again to lose parent. */
111		cpid = fork();
112		if (cpid == -1) {
113			perror("rcmdsh: fork to lose parent failed");
114			_exit(255);
115		}
116		if (cpid > 0)
117			_exit(0);
118
119		/* In grandchild here.  Become local user for rshprog. */
120		if (setuid(pw->pw_uid) == -1) {
121			(void)fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
122			    pw->pw_uid, strerror(errno));
123			_exit(255);
124		}
125
126		/*
127		 * If remote host is "localhost" and local and remote users
128		 * are the same, avoid running remote shell for efficiency.
129		 */
130		if (strcmp(*ahost, "localhost") == 0 &&
131		    strcmp(locuser, remuser) == 0) {
132			if (pw->pw_shell[0] == '\0')
133				rshprog = _PATH_BSHELL;
134			else
135				rshprog = pw->pw_shell;
136			p = strrchr(rshprog, '/');
137			execlp(rshprog, p ? p + 1 : rshprog, "-c", cmd,
138			    (char *)NULL);
139		} else {
140			p = strrchr(rshprog, '/');
141			execlp(rshprog, p ? p + 1 : rshprog, *ahost, "-l",
142			    remuser, cmd, (char *)NULL);
143		}
144		(void)fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
145		    rshprog, strerror(errno));
146		_exit(255);
147	} else {
148		/* Parent. close sp[1], return sp[0]. */
149		(void)close(sp[1]);
150		/* Reap child. */
151		(void)wait(NULL);
152		return (sp[0]);
153	}
154	/* NOTREACHED */
155}
156