1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7 * Reserved.  This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License').  You may not use this file
10 * except in compliance with the License.  Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1983, 1993
26 *	The Regents of the University of California.  All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 *    notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 *    notice, this list of conditions and the following disclaimer in the
35 *    documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 *    must display the following acknowledgement:
38 *	This product includes software developed by the University of
39 *	California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 *    may be used to endorse or promote products derived from this software
42 *    without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#include <sys/cdefs.h>
58#ifndef lint
59__unused static char copyright[] =
60"@(#) Copyright (c) 1983, 1993\n\
61	The Regents of the University of California.  All rights reserved.\n";
62#endif /* not lint */
63
64#ifndef lint
65__unused static char sccsid[] = "@(#)rexecd.c	8.1 (Berkeley) 6/4/93";
66#endif /* not lint */
67
68#include <sys/param.h>
69#include <sys/ioctl.h>
70#include <sys/socket.h>
71#include <sys/time.h>
72
73#include <netinet/in.h>
74
75#include <errno.h>
76#include <netdb.h>
77#include <paths.h>
78#include <pwd.h>
79#include <signal.h>
80#include <stdarg.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>
84#include <unistd.h>
85
86void doit() __dead2;
87void error(const char *, ...);
88void getstr();
89
90/*
91 * remote execute server:
92 *	username\0
93 *	password\0
94 *	command\0
95 *	data
96 */
97/*ARGSUSED*/
98int
99main(argc, argv)
100	int argc;
101	char **argv;
102{
103	struct sockaddr_in from;
104	socklen_t fromlen;
105
106	fromlen = sizeof (from);
107	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
108		(void)fprintf(stderr,
109		    "rexecd: getpeername: %s\n", strerror(errno));
110		exit(1);
111	}
112	doit(0, &from);
113}
114
115char	username[20] = "USER=";
116char	homedir[64] = "HOME=";
117char	shell[64] = "SHELL=";
118char	path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
119char	*envinit[] =
120	    {homedir, shell, path, username, 0};
121#ifdef __APPLE__
122extern
123#endif
124char	**environ;
125
126struct	sockaddr_in a_sin = { AF_INET };
127
128void
129doit(f, fromp)
130	int f;
131	struct sockaddr_in *fromp;
132{
133	char cmdbuf[NCARGS+1], *cp, *namep;
134	char user[16], pass[16];
135	struct passwd *pwd;
136	int s = -1;
137	u_short port;
138	int pv[2], pid, ready, readfrom, cc;
139	char buf[BUFSIZ], sig;
140	int one = 1;
141
142	(void) signal(SIGINT, SIG_DFL);
143	(void) signal(SIGQUIT, SIG_DFL);
144	(void) signal(SIGTERM, SIG_DFL);
145#ifdef DEBUG
146	{ int t = open(_PATH_TTY, 2);
147	  if (t >= 0) {
148		ioctl(t, TIOCNOTTY, (char *)0);
149		(void) close(t);
150	  }
151	}
152#endif
153	dup2(f, 0);
154	dup2(f, 1);
155	dup2(f, 2);
156	(void) alarm(60);
157	port = 0;
158	for (;;) {
159		char c;
160		if (read(f, &c, 1) != 1)
161			exit(1);
162		if (c == 0)
163			break;
164		port = port * 10 + c - '0';
165	}
166	(void) alarm(0);
167	if (port != 0) {
168		s = socket(AF_INET, SOCK_STREAM, 0);
169		if (s < 0)
170			exit(1);
171		if (bind(s, (struct sockaddr *)&a_sin, sizeof (a_sin)) < 0)
172			exit(1);
173		(void) alarm(60);
174		fromp->sin_port = htons(port);
175		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
176			exit(1);
177		(void) alarm(0);
178	}
179	getstr(user, sizeof(user), "username");
180	getstr(pass, sizeof(pass), "password");
181	getstr(cmdbuf, sizeof(cmdbuf), "command");
182	setpwent();
183	pwd = getpwnam(user);
184	if (pwd == NULL) {
185		error("Login incorrect.\n");
186		exit(1);
187	}
188	endpwent();
189	if (*pwd->pw_passwd != '\0') {
190		namep = crypt(pass, pwd->pw_passwd);
191		if (strcmp(namep, pwd->pw_passwd)) {
192			error("Password incorrect.\n");
193			exit(1);
194		}
195	}
196	if (chdir(pwd->pw_dir) < 0) {
197		error("No remote directory.\n");
198		exit(1);
199	}
200	(void) write(2, "\0", 1);
201	if (port) {
202		(void) pipe(pv);
203		pid = fork();
204		if (pid == -1)  {
205			error("Try again.\n");
206			exit(1);
207		}
208		if (pid) {
209			(void) close(0); (void) close(1); (void) close(2);
210			(void) close(f); (void) close(pv[1]);
211			readfrom = (1<<s) | (1<<pv[0]);
212			ioctl(pv[1], FIONBIO, (char *)&one);
213			/* should set s nbio! */
214			do {
215				ready = readfrom;
216				(void) select(16, (fd_set *)&ready,
217				    (fd_set *)NULL, (fd_set *)NULL,
218				    (struct timeval *)NULL);
219				if (ready & (1<<s)) {
220					if (read(s, &sig, 1) <= 0)
221						readfrom &= ~(1<<s);
222					else
223						killpg(pid, sig);
224				}
225				if (ready & (1<<pv[0])) {
226					cc = read(pv[0], buf, sizeof (buf));
227					if (cc <= 0) {
228						shutdown(s, 1+1);
229						readfrom &= ~(1<<pv[0]);
230					} else
231						(void) write(s, buf, cc);
232				}
233			} while (readfrom);
234			exit(0);
235		}
236		setpgid(0, getpid());
237		(void) close(s); (void)close(pv[0]);
238		dup2(pv[1], 2);
239	}
240	if (*pwd->pw_shell == '\0')
241		pwd->pw_shell = _PATH_BSHELL;
242	if (f > 2)
243		(void) close(f);
244	(void) setgid((gid_t)pwd->pw_gid);
245	initgroups(pwd->pw_name, pwd->pw_gid);
246	(void) setuid((uid_t)pwd->pw_uid);
247	(void)strcat(path, _PATH_DEFPATH);
248	environ = envinit;
249	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
250	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
251	strncat(username, pwd->pw_name, sizeof(username)-6);
252	cp = strrchr(pwd->pw_shell, '/');
253	if (cp)
254		cp++;
255	else
256		cp = pwd->pw_shell;
257	execl(pwd->pw_shell, cp, "-c", cmdbuf, NULL);
258	perror(pwd->pw_shell);
259	exit(1);
260}
261
262void
263error(const char *fmt, ...)
264{
265	char buf[BUFSIZ];
266	va_list ap;
267
268	buf[0] = 1;
269	va_start(ap, fmt);
270	(void) vsnprintf(buf+1, sizeof(buf) - 1, fmt, ap);
271	va_end(ap);
272	(void) write(2, buf, strlen(buf));
273}
274
275void
276getstr(buf, cnt, err)
277	char *buf;
278	int cnt;
279	char *err;
280{
281	char c;
282
283	do {
284		if (read(0, &c, 1) != 1)
285			exit(1);
286		*buf++ = c;
287		if (--cnt == 0) {
288			error("%s too long\n", err);
289			exit(1);
290		}
291	} while (c != 0);
292}
293