xref: /dragonfly/lib/libc/net/rcmdsh.c (revision 2cd2d2b5)
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  * $FreeBSD: src/lib/libc/net/rcmdsh.c,v 1.3.2.2 2002/04/22 17:38:53 ume Exp $
31  * $DragonFly: src/lib/libc/net/rcmdsh.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
32  */
33 
34 /*
35  * This is an rcmd() replacement originally by
36  * Chris Siebenmann <cks@utcc.utoronto.ca>.
37  */
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 int
61 rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog)
62 	char **ahost;
63 	int rport;
64 	const char *locuser, *remuser, *cmd, *rshprog;
65 {
66 	struct addrinfo hints, *res;
67 	int cpid, sp[2], error;
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), "%d", ntohs(rport));
90 		error = getaddrinfo(*ahost, num, &hints, &res);
91 		if (error) {
92 			fprintf(stderr, "rcmdsh: getaddrinfo: %s\n",
93 				gai_strerror(error));
94 			return (-1);
95 		}
96 		if (res->ai_canonname) {
97 			strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1);
98 			hbuf[sizeof(hbuf) - 1] = '\0';
99 			*ahost = hbuf;
100 		}
101 		freeaddrinfo(res);
102 	}
103 
104 	/* Get a socketpair we'll use for stdin and stdout. */
105 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
106 		perror("rcmdsh: socketpair");
107 		return (-1);
108 	}
109 
110 	cpid = fork();
111 	if (cpid == -1) {
112 		perror("rcmdsh: fork failed");
113 		return (-1);
114 	} else if (cpid == 0) {
115 		/*
116 		 * Child.  We use sp[1] to be stdin/stdout, and close sp[0].
117 		 */
118 		(void)close(sp[0]);
119 		if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) {
120 			perror("rcmdsh: dup2 failed");
121 			_exit(255);
122 		}
123 		/* Fork again to lose parent. */
124 		cpid = fork();
125 		if (cpid == -1) {
126 			perror("rcmdsh: fork to lose parent failed");
127 			_exit(255);
128 		}
129 		if (cpid > 0)
130 			_exit(0);
131 
132 		/* In grandchild here.  Become local user for rshprog. */
133 		if (setuid(pw->pw_uid) == -1) {
134 			(void)fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
135 			    pw->pw_uid, strerror(errno));
136 			_exit(255);
137 		}
138 
139 		/*
140 		 * If remote host is "localhost" and local and remote users
141 		 * are the same, avoid running remote shell for efficiency.
142 		 */
143 		if (strcmp(*ahost, "localhost") == 0 &&
144 		    strcmp(locuser, remuser) == 0) {
145 			if (pw->pw_shell[0] == '\0')
146 				rshprog = _PATH_BSHELL;
147 			else
148 				rshprog = pw->pw_shell;
149 			p = strrchr(rshprog, '/');
150 			execlp(rshprog, p ? p + 1 : rshprog, "-c", cmd,
151 			    (char *)NULL);
152 		} else {
153 			p = strrchr(rshprog, '/');
154 			execlp(rshprog, p ? p + 1 : rshprog, *ahost, "-l",
155 			    remuser, cmd, (char *)NULL);
156 		}
157 		(void)fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
158 		    rshprog, strerror(errno));
159 		_exit(255);
160 	} else {
161 		/* Parent. close sp[1], return sp[0]. */
162 		(void)close(sp[1]);
163 		/* Reap child. */
164 		(void)wait(NULL);
165 		return (sp[0]);
166 	}
167 	/* NOTREACHED */
168 }
169