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