xref: /freebsd/lib/libc/net/rcmdsh.c (revision b740c88bfb6453416926271c089262e7164dace3)
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$");
38 
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/wait.h>
42 #include <arpa/inet.h>
43 
44 #include <errno.h>
45 #include <netdb.h>
46 #include <paths.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #ifndef _PATH_RSH
53 #define	_PATH_RSH	"/usr/bin/rsh"
54 #endif
55 
56 /*
57  * This is a replacement rcmd() function that uses the rsh(1)
58  * program in place of a direct rcmd(3) function call so as to
59  * avoid having to be root.  Note that rport is ignored.
60  */
61 int
62 rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog)
63 	char **ahost;
64 	int rport;
65 	const char *locuser, *remuser, *cmd, *rshprog;
66 {
67 	struct addrinfo hints, *res;
68 	int cpid, sp[2], error;
69 	char *p;
70 	struct passwd *pw;
71 	char num[8];
72 	static char hbuf[NI_MAXHOST];
73 
74 	/* What rsh/shell to use. */
75 	if (rshprog == NULL)
76 		rshprog = _PATH_RSH;
77 
78 	/* locuser must exist on this host. */
79 	if ((pw = getpwnam(locuser)) == NULL) {
80 		(void)fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
81 		return (-1);
82 	}
83 
84 	/* Validate remote hostname. */
85 	if (strcmp(*ahost, "localhost") != 0) {
86 		memset(&hints, 0, sizeof(hints));
87 		hints.ai_flags = AI_CANONNAME;
88 		hints.ai_family = PF_UNSPEC;
89 		hints.ai_socktype = SOCK_STREAM;
90 		(void)snprintf(num, sizeof(num), "%u",
91 		    (unsigned int)ntohs(rport));
92 		error = getaddrinfo(*ahost, num, &hints, &res);
93 		if (error) {
94 			fprintf(stderr, "rcmdsh: getaddrinfo: %s\n",
95 				gai_strerror(error));
96 			return (-1);
97 		}
98 		if (res->ai_canonname) {
99 			strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1);
100 			hbuf[sizeof(hbuf) - 1] = '\0';
101 			*ahost = hbuf;
102 		}
103 		freeaddrinfo(res);
104 	}
105 
106 	/* Get a socketpair we'll use for stdin and stdout. */
107 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
108 		perror("rcmdsh: socketpair");
109 		return (-1);
110 	}
111 
112 	cpid = fork();
113 	if (cpid == -1) {
114 		perror("rcmdsh: fork failed");
115 		return (-1);
116 	} else if (cpid == 0) {
117 		/*
118 		 * Child.  We use sp[1] to be stdin/stdout, and close sp[0].
119 		 */
120 		(void)close(sp[0]);
121 		if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) {
122 			perror("rcmdsh: dup2 failed");
123 			_exit(255);
124 		}
125 		/* Fork again to lose parent. */
126 		cpid = fork();
127 		if (cpid == -1) {
128 			perror("rcmdsh: fork to lose parent failed");
129 			_exit(255);
130 		}
131 		if (cpid > 0)
132 			_exit(0);
133 
134 		/* In grandchild here.  Become local user for rshprog. */
135 		if (setuid(pw->pw_uid) == -1) {
136 			(void)fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
137 			    pw->pw_uid, strerror(errno));
138 			_exit(255);
139 		}
140 
141 		/*
142 		 * If remote host is "localhost" and local and remote users
143 		 * are the same, avoid running remote shell for efficiency.
144 		 */
145 		if (strcmp(*ahost, "localhost") == 0 &&
146 		    strcmp(locuser, remuser) == 0) {
147 			if (pw->pw_shell[0] == '\0')
148 				rshprog = _PATH_BSHELL;
149 			else
150 				rshprog = pw->pw_shell;
151 			p = strrchr(rshprog, '/');
152 			execlp(rshprog, p ? p + 1 : rshprog, "-c", cmd,
153 			    (char *)NULL);
154 		} else {
155 			p = strrchr(rshprog, '/');
156 			execlp(rshprog, p ? p + 1 : rshprog, *ahost, "-l",
157 			    remuser, cmd, (char *)NULL);
158 		}
159 		(void)fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
160 		    rshprog, strerror(errno));
161 		_exit(255);
162 	} else {
163 		/* Parent. close sp[1], return sp[0]. */
164 		(void)close(sp[1]);
165 		/* Reap child. */
166 		(void)wait(NULL);
167 		return (sp[0]);
168 	}
169 	/* NOTREACHED */
170 }
171