xref: /netbsd/libexec/rexecd/rexecd.c (revision bf9ec67e)
1 /*	$NetBSD: rexecd.c,v 1.10 2002/05/26 00:02:08 wiz Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  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 the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #if 0
41 static char sccsid[] = "from: @(#)rexecd.c	8.1 (Berkeley) 6/4/93";
42 #else
43 __RCSID("$NetBSD: rexecd.c,v 1.10 2002/05/26 00:02:08 wiz Exp $");
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/ioctl.h>
49 #include <sys/poll.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 #include <sys/time.h>
53 
54 #include <netinet/in.h>
55 
56 #include <err.h>
57 #include <errno.h>
58 #include <netdb.h>
59 #include <paths.h>
60 #include <pwd.h>
61 #include <signal.h>
62 #include <stdarg.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 
68 void error __P((const char *, ...))
69      __attribute__((__format__(__printf__, 1, 2)));
70 int main __P((int, char **));
71 void doit __P((int, struct sockaddr_in *));
72 void getstr __P((char *, int, char *));
73 
74 char	username[32 + 1] = "USER=";
75 char	homedir[PATH_MAX + 1] = "HOME=";
76 char	shell[PATH_MAX + 1] = "SHELL=";
77 char	path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
78 char	*envinit[] = { homedir, shell, path, username, 0 };
79 char	**environ;
80 int	log;
81 struct	sockaddr_in asin = { AF_INET };
82 
83 /*
84  * remote execute server:
85  *	username\0
86  *	password\0
87  *	command\0
88  *	data
89  */
90 int
91 main(argc, argv)
92 	int argc;
93 	char **argv;
94 {
95 	struct sockaddr_in from;
96 	int fromlen, ch;
97 
98 	while ((ch = getopt(argc, argv, "l")) != -1)
99 		switch (ch) {
100 		case 'l':
101 			log = 1;
102 			openlog("rexecd", LOG_PID, LOG_DAEMON);
103 			break;
104 		default:
105 			exit(1);
106 		}
107 
108 	fromlen = sizeof (from);
109 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
110 		err(1, "getpeername");
111 
112 	doit(0, &from);
113 	exit(0);
114 }
115 
116 void
117 doit(f, fromp)
118 	int f;
119 	struct sockaddr_in *fromp;
120 {
121 	struct pollfd fds[2];
122 	char cmdbuf[NCARGS+1], *namep;
123 	const char *cp;
124 	char user[16], pass[16];
125 	char buf[BUFSIZ], sig;
126 	struct passwd *pwd;
127 	int s = -1; /* XXX gcc */
128 	int pv[2], pid, cc;
129 	int one = 1;
130 	in_port_t port;
131 
132 	(void)signal(SIGINT, SIG_DFL);
133 	(void)signal(SIGQUIT, SIG_DFL);
134 	(void)signal(SIGTERM, SIG_DFL);
135 	dup2(f, 0);
136 	dup2(f, 1);
137 	dup2(f, 2);
138 	(void)alarm(60);
139 	port = 0;
140 	for (;;) {
141 		char c;
142 		if (read(f, &c, 1) != 1) {
143 			if (log)
144 				syslog(LOG_ERR,
145 				    "initial read failed");
146 			exit(1);
147 		}
148 		if (c == 0)
149 			break;
150 		port = port * 10 + c - '0';
151 	}
152 	(void)alarm(0);
153 	if (port != 0) {
154 		s = socket(AF_INET, SOCK_STREAM, 0);
155 		if (s < 0) {
156 			if (log)
157 				syslog(LOG_ERR, "socket: %m");
158 			exit(1);
159 		}
160 		if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0) {
161 			if (log)
162 				syslog(LOG_ERR, "bind: %m");
163 			exit(1);
164 		}
165 		(void)alarm(60);
166 		fromp->sin_port = htons(port);
167 		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
168 			if (log)
169 				syslog(LOG_ERR, "connect: %m");
170 			exit(1);
171 		}
172 		(void)alarm(0);
173 	}
174 	getstr(user, sizeof(user), "username");
175 	getstr(pass, sizeof(pass), "password");
176 	getstr(cmdbuf, sizeof(cmdbuf), "command");
177 	setpwent();
178 	pwd = getpwnam(user);
179 	if (pwd == NULL) {
180 		error("Login incorrect.\n");
181 		if (log)
182 			syslog(LOG_ERR, "no such user %s", user);
183 		exit(1);
184 	}
185 	endpwent();
186 	if (*pwd->pw_passwd != '\0') {
187 		namep = crypt(pass, pwd->pw_passwd);
188 		if (strcmp(namep, pwd->pw_passwd)) {
189 			error("Password incorrect.\n");	/* XXX: wrong! */
190 			if (log)
191 				syslog(LOG_ERR, "incorrect password for %s",
192 				    user);
193 			exit(1);
194 		}
195 	} else
196 		(void)crypt("dummy password", "PA");	/* must always crypt */
197 	if (chdir(pwd->pw_dir) < 0) {
198 		error("No remote directory.\n");
199 		if (log)
200 			syslog(LOG_ERR, "%s does not exist for %s", pwd->pw_dir,
201 			    user);
202 		exit(1);
203 	}
204 	(void)write(2, "\0", 1);
205 	if (port) {
206 		if (pipe(pv) < 0 || (pid = fork()) == -1) {
207 			error("Try again.\n");
208 			if (log)
209 				syslog(LOG_ERR,"pipe or fork failed for %s: %m",
210 				    user);
211 			exit(1);
212 		}
213 		if (pid) {
214 			(void)close(0);
215 			(void)close(1);
216 			(void)close(2);
217 			(void)close(f);
218 			(void)close(pv[1]);
219 			fds[0].fd = s;
220 			fds[1].fd = pv[0];
221 			fds[0].events = fds[1].events = POLLIN;
222 			if (ioctl(pv[1], FIONBIO, (char *)&one) < 0)
223 				_exit(1);
224 			/* should set s nbio! */
225 			do {
226 				if (poll(fds, 2, 0) < 0) {
227 					close(s);
228 					close(pv[0]);
229 					_exit(1);
230 				}
231 				if (fds[0].revents & POLLIN) {
232 					if (read(s, &sig, 1) <= 0)
233 						fds[0].events = 0;
234 					else
235 						killpg(pid, sig);
236 				}
237 				if (fds[1].revents & POLLIN) {
238 					cc = read(pv[0], buf, sizeof (buf));
239 					if (cc <= 0) {
240 						shutdown(s, 1+1);
241 						fds[1].events = 0;
242 					} else
243 						(void)write(s, buf, cc);
244 				}
245 			} while ((fds[0].events | fds[1].events) & POLLIN);
246 			_exit(0);
247 		}
248 		(void)setpgrp(0, getpid());
249 		(void)close(s);
250 		(void)close(pv[0]);
251 		if (dup2(pv[1], 2) < 0) {
252 			error("Try again.\n");
253 			if (log)
254 				syslog(LOG_ERR, "dup2 failed for %s", user);
255 			exit(1);
256 		}
257 	}
258 	if (*pwd->pw_shell == '\0')
259 		pwd->pw_shell = _PATH_BSHELL;
260 	if (f > 2)
261 		(void)close(f);
262 	if (setlogin(pwd->pw_name) < 0 ||
263 	    initgroups(pwd->pw_name, pwd->pw_gid) < 0 ||
264 	    setgid((gid_t)pwd->pw_gid) < 0 ||
265 	    setuid((uid_t)pwd->pw_uid) < 0) {
266 		error("Try again.\n");
267 		if (log)
268 			syslog(LOG_ERR, "could not set permissions for %s: %m",
269 			    user);
270 		exit(1);
271 	}
272 	(void)strcat(path, _PATH_DEFPATH);
273 	environ = envinit;
274 	strncat(homedir, pwd->pw_dir, sizeof(homedir) - 6);
275 	strncat(shell, pwd->pw_shell, sizeof(shell) - 7);
276 	strncat(username, pwd->pw_name, sizeof(username) - 6);
277 	cp = strrchr(pwd->pw_shell, '/');
278 	if (cp)
279 		cp++;
280 	else
281 		cp = pwd->pw_shell;
282 	if (log)
283 		syslog(LOG_INFO, "running command for %s: %s", user, cmdbuf);
284 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
285 	perror(pwd->pw_shell);
286 	if (log)
287 		syslog(LOG_ERR, "execl failed for %s: %m", user);
288 	exit(1);
289 }
290 
291 void
292 error(const char *fmt, ...)
293 {
294 	char buf[BUFSIZ];
295 	va_list ap;
296 
297 	va_start(ap, fmt);
298 	buf[0] = 1;
299 	(void)vsnprintf(buf+1, sizeof(buf) - 1, fmt, ap);
300 	(void)write(2, buf, strlen(buf));
301 	va_end(ap);
302 }
303 
304 void
305 getstr(buf, cnt, err)
306 	char *buf;
307 	int cnt;
308 	char *err;
309 {
310 	char c;
311 
312 	do {
313 		if (read(0, &c, 1) != 1)
314 			exit(1);
315 		*buf++ = c;
316 		if (--cnt == 0) {
317 			error("%s too long\n", err);
318 			exit(1);
319 		}
320 	} while (c != 0);
321 }
322