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