xref: /netbsd-src/libexec/rexecd/rexecd.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: rexecd.c,v 1.4 1997/10/07 10:11:31 mrg 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.4 1997/10/07 10:11:31 mrg Exp $");
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/ioctl.h>
49 #include <sys/socket.h>
50 #include <sys/time.h>
51 
52 #include <netinet/in.h>
53 
54 #include <errno.h>
55 #include <netdb.h>
56 #include <paths.h>
57 #include <pwd.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 void error __P((const char *, ...));
65 int main __P((int, char **));
66 void doit __P((int, struct sockaddr_in *));
67 void getstr __P((char *, int, char *));
68 
69 /*
70  * remote execute server:
71  *	username\0
72  *	password\0
73  *	command\0
74  *	data
75  */
76 int
77 main(argc, argv)
78 	int argc;
79 	char **argv;
80 {
81 	struct sockaddr_in from;
82 	int fromlen;
83 
84 	fromlen = sizeof (from);
85 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
86 		(void)fprintf(stderr,
87 		    "rexecd: getpeername: %s\n", strerror(errno));
88 		exit(1);
89 	}
90 	doit(0, &from);
91 	exit(0);
92 }
93 
94 char	username[20] = "USER=";
95 char	homedir[64] = "HOME=";
96 char	shell[64] = "SHELL=";
97 char	path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
98 char	*envinit[] =
99 	    {homedir, shell, path, username, 0};
100 char	**environ;
101 
102 struct	sockaddr_in asin = { AF_INET };
103 
104 void
105 doit(f, fromp)
106 	int f;
107 	struct sockaddr_in *fromp;
108 {
109 	char cmdbuf[NCARGS+1], *cp, *namep;
110 	char user[16], pass[16];
111 	struct passwd *pwd;
112 	int s = -1; /* XXX gcc */
113 	u_short port;
114 	int pv[2], pid, ready, readfrom, cc;
115 	char buf[BUFSIZ], sig;
116 	int one = 1;
117 
118 	(void) signal(SIGINT, SIG_DFL);
119 	(void) signal(SIGQUIT, SIG_DFL);
120 	(void) signal(SIGTERM, SIG_DFL);
121 #ifdef DEBUG
122 	{ int t = open(_PATH_TTY, 2);
123 	  if (t >= 0) {
124 		ioctl(t, TIOCNOTTY, (char *)0);
125 		(void) close(t);
126 	  }
127 	}
128 #endif
129 	dup2(f, 0);
130 	dup2(f, 1);
131 	dup2(f, 2);
132 	(void) alarm(60);
133 	port = 0;
134 	for (;;) {
135 		char c;
136 		if (read(f, &c, 1) != 1)
137 			exit(1);
138 		if (c == 0)
139 			break;
140 		port = port * 10 + c - '0';
141 	}
142 	(void) alarm(0);
143 	if (port != 0) {
144 		s = socket(AF_INET, SOCK_STREAM, 0);
145 		if (s < 0)
146 			exit(1);
147 		if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
148 			exit(1);
149 		(void) alarm(60);
150 		fromp->sin_port = htons(port);
151 		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
152 			exit(1);
153 		(void) alarm(0);
154 	}
155 	getstr(user, sizeof(user), "username");
156 	getstr(pass, sizeof(pass), "password");
157 	getstr(cmdbuf, sizeof(cmdbuf), "command");
158 	setpwent();
159 	pwd = getpwnam(user);
160 	if (pwd == NULL) {
161 		error("Login incorrect.\n");
162 		exit(1);
163 	}
164 	endpwent();
165 	if (*pwd->pw_passwd != '\0') {
166 		namep = crypt(pass, pwd->pw_passwd);
167 		if (strcmp(namep, pwd->pw_passwd)) {
168 			error("Password incorrect.\n");
169 			exit(1);
170 		}
171 	}
172 	if (chdir(pwd->pw_dir) < 0) {
173 		error("No remote directory.\n");
174 		exit(1);
175 	}
176 	(void) write(2, "\0", 1);
177 	if (port) {
178 		(void) pipe(pv);
179 		pid = fork();
180 		if (pid == -1)  {
181 			error("Try again.\n");
182 			exit(1);
183 		}
184 		if (pid) {
185 			(void) close(0); (void) close(1); (void) close(2);
186 			(void) close(f); (void) close(pv[1]);
187 			readfrom = (1<<s) | (1<<pv[0]);
188 			ioctl(pv[1], FIONBIO, (char *)&one);
189 			/* should set s nbio! */
190 			do {
191 				ready = readfrom;
192 				(void) select(16, (fd_set *)&ready,
193 				    (fd_set *)NULL, (fd_set *)NULL,
194 				    (struct timeval *)NULL);
195 				if (ready & (1<<s)) {
196 					if (read(s, &sig, 1) <= 0)
197 						readfrom &= ~(1<<s);
198 					else
199 						killpg(pid, sig);
200 				}
201 				if (ready & (1<<pv[0])) {
202 					cc = read(pv[0], buf, sizeof (buf));
203 					if (cc <= 0) {
204 						shutdown(s, 1+1);
205 						readfrom &= ~(1<<pv[0]);
206 					} else
207 						(void) write(s, buf, cc);
208 				}
209 			} while (readfrom);
210 			exit(0);
211 		}
212 		setpgrp(0, getpid());
213 		(void) close(s); (void)close(pv[0]);
214 		dup2(pv[1], 2);
215 	}
216 	if (*pwd->pw_shell == '\0')
217 		pwd->pw_shell = _PATH_BSHELL;
218 	if (f > 2)
219 		(void) close(f);
220 	setlogin(pwd->pw_name);
221 	(void) setgid((gid_t)pwd->pw_gid);
222 	initgroups(pwd->pw_name, pwd->pw_gid);
223 	(void) setuid((uid_t)pwd->pw_uid);
224 	(void)strcat(path, _PATH_DEFPATH);
225 	environ = envinit;
226 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
227 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
228 	strncat(username, pwd->pw_name, sizeof(username)-6);
229 	cp = strrchr(pwd->pw_shell, '/');
230 	if (cp)
231 		cp++;
232 	else
233 		cp = pwd->pw_shell;
234 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
235 	perror(pwd->pw_shell);
236 	exit(1);
237 }
238 
239 #ifdef __STDC__
240 #include <stdarg.h>
241 #else
242 #include <varargs.h>
243 #endif
244 
245 void
246 #ifdef __STDC__
247 error(const char *fmt, ...)
248 #else
249 error(fmt, va_alist)
250 	char *fmt;
251 	va_dcl
252 #endif
253 {
254 	char buf[BUFSIZ];
255 	va_list ap;
256 
257 #ifdef __STDC__
258 	va_start(ap, fmt);
259 #else
260 	va_start(ap);
261 #endif
262 
263 	buf[0] = 1;
264 	(void)vsprintf(buf+1, fmt, ap);
265 	(void)write(2, buf, strlen(buf));
266 }
267 
268 void
269 getstr(buf, cnt, err)
270 	char *buf;
271 	int cnt;
272 	char *err;
273 {
274 	char c;
275 
276 	do {
277 		if (read(0, &c, 1) != 1)
278 			exit(1);
279 		*buf++ = c;
280 		if (--cnt == 0) {
281 			error("%s too long\n", err);
282 			exit(1);
283 		}
284 	} while (c != 0);
285 }
286