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