rsh.c revision 1.36 1 1.36 enami /* $NetBSD: rsh.c,v 1.36 2014/06/08 02:44:15 enami Exp $ */
2 1.4 tls
3 1.1 cgd /*-
4 1.5 mrg * Copyright (c) 1983, 1990, 1993, 1994
5 1.5 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.22 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.11 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.30 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993, 1994\
35 1.30 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.11 christos #if 0
40 1.11 christos static char sccsid[] = "@(#)rsh.c 8.4 (Berkeley) 4/29/95";
41 1.11 christos #else
42 1.36 enami __RCSID("$NetBSD: rsh.c,v 1.36 2014/06/08 02:44:15 enami Exp $");
43 1.11 christos #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd #include <sys/types.h>
47 1.1 cgd #include <sys/socket.h>
48 1.1 cgd #include <sys/ioctl.h>
49 1.1 cgd #include <sys/file.h>
50 1.7 mrg #include <poll.h>
51 1.1 cgd
52 1.1 cgd #include <netinet/in.h>
53 1.20 joff #include <netinet/tcp.h>
54 1.1 cgd #include <netdb.h>
55 1.1 cgd
56 1.5 mrg #include <err.h>
57 1.5 mrg #include <errno.h>
58 1.23 christos #include <limits.h>
59 1.1 cgd #include <pwd.h>
60 1.3 jtc #include <signal.h>
61 1.15 wiz #include <stdarg.h>
62 1.1 cgd #include <stdio.h>
63 1.5 mrg #include <stdlib.h>
64 1.1 cgd #include <string.h>
65 1.5 mrg #include <unistd.h>
66 1.5 mrg
67 1.1 cgd #include "pathnames.h"
68 1.23 christos #include "getport.h"
69 1.1 cgd
70 1.1 cgd
71 1.1 cgd /*
72 1.1 cgd * rsh - remote shell
73 1.1 cgd */
74 1.10 christos int remerr;
75 1.10 christos
76 1.10 christos static int sigs[] = { SIGINT, SIGTERM, SIGQUIT };
77 1.1 cgd
78 1.33 joerg static char *copyargs(char **);
79 1.36 enami #ifndef IN_RCMD
80 1.33 joerg static void sendsig(int);
81 1.36 enami #endif
82 1.33 joerg static int checkfd(struct pollfd *, int);
83 1.33 joerg static void talk(int, sigset_t *, pid_t, int);
84 1.33 joerg __dead static void usage(void);
85 1.12 christos #ifdef IN_RCMD
86 1.15 wiz int orcmd(char **, int, const char *,
87 1.15 wiz const char *, const char *, int *);
88 1.15 wiz int orcmd_af(char **, int, const char *,
89 1.15 wiz const char *, const char *, int *, int);
90 1.12 christos #endif
91 1.5 mrg
92 1.5 mrg int
93 1.15 wiz main(int argc, char **argv)
94 1.1 cgd {
95 1.1 cgd struct passwd *pw;
96 1.1 cgd struct servent *sp;
97 1.10 christos sigset_t oset, nset;
98 1.20 joff struct protoent *proto;
99 1.10 christos
100 1.7 mrg #ifdef IN_RCMD
101 1.7 mrg char *locuser = 0, *loop;
102 1.7 mrg #endif /* IN_RCMD */
103 1.31 lukem int argoff, asrsh, ch, dflag, nflag, one, rem;
104 1.31 lukem size_t i;
105 1.26 ginsbach int family = AF_UNSPEC;
106 1.5 mrg pid_t pid;
107 1.5 mrg uid_t uid;
108 1.9 mrg char *args, *host, *p, *user, *name;
109 1.1 cgd
110 1.1 cgd argoff = asrsh = dflag = nflag = 0;
111 1.1 cgd one = 1;
112 1.1 cgd host = user = NULL;
113 1.17 hubertf sp = NULL;
114 1.1 cgd
115 1.8 mrg #ifndef IN_RCMD
116 1.7 mrg /*
117 1.8 mrg * If called as something other than "rsh" use it as the host name,
118 1.8 mrg * only for rsh.
119 1.7 mrg */
120 1.14 cgd if (strcmp(getprogname(), "rsh") == 0)
121 1.7 mrg asrsh = 1;
122 1.14 cgd else {
123 1.14 cgd host = strdup(getprogname());
124 1.14 cgd if (host == NULL)
125 1.14 cgd err(1, NULL);
126 1.14 cgd }
127 1.8 mrg #endif /* IN_RCMD */
128 1.1 cgd
129 1.1 cgd /* handle "rsh host flags" */
130 1.1 cgd if (!host && argc > 2 && argv[1][0] != '-') {
131 1.1 cgd host = argv[1];
132 1.1 cgd argoff = 1;
133 1.1 cgd }
134 1.1 cgd
135 1.7 mrg #ifdef IN_RCMD
136 1.7 mrg if ((loop = getenv("RCMD_LOOP")) && strcmp(loop, "YES") == 0)
137 1.7 mrg warnx("rcmd appears to be looping!");
138 1.7 mrg
139 1.32 gson setenv("RCMD_LOOP", "YES", 1);
140 1.7 mrg
141 1.26 ginsbach # define OPTIONS "468KLdel:np:u:w"
142 1.7 mrg
143 1.7 mrg #else /* IN_RCMD */
144 1.7 mrg
145 1.26 ginsbach # define OPTIONS "468KLdel:np:w"
146 1.7 mrg
147 1.7 mrg #endif /* IN_RCMD */
148 1.7 mrg
149 1.7 mrg if (!(pw = getpwuid(uid = getuid())))
150 1.7 mrg errx(1, "unknown user id");
151 1.7 mrg
152 1.9 mrg if ((name = strdup(pw->pw_name)) == NULL)
153 1.9 mrg err(1, "malloc");
154 1.10 christos while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
155 1.1 cgd switch(ch) {
156 1.26 ginsbach case '4':
157 1.26 ginsbach family = AF_INET;
158 1.26 ginsbach break;
159 1.26 ginsbach case '6':
160 1.26 ginsbach family = AF_INET6;
161 1.26 ginsbach break;
162 1.1 cgd case 'K':
163 1.1 cgd break;
164 1.1 cgd case 'L': /* -8Lew are ignored to allow rlogin aliases */
165 1.1 cgd case 'e':
166 1.1 cgd case 'w':
167 1.1 cgd case '8':
168 1.1 cgd break;
169 1.1 cgd case 'd':
170 1.1 cgd dflag = 1;
171 1.1 cgd break;
172 1.1 cgd case 'l':
173 1.1 cgd user = optarg;
174 1.1 cgd break;
175 1.1 cgd case 'n':
176 1.1 cgd nflag = 1;
177 1.1 cgd break;
178 1.17 hubertf case 'p':
179 1.23 christos sp = getport(optarg, "tcp");
180 1.17 hubertf break;
181 1.7 mrg #ifdef IN_RCMD
182 1.7 mrg case 'u':
183 1.9 mrg if (getuid() != 0 && optarg && name &&
184 1.9 mrg strcmp(name, optarg) != 0)
185 1.7 mrg errx(1,"only super user can use the -u option");
186 1.7 mrg locuser = optarg;
187 1.7 mrg break;
188 1.7 mrg #endif /* IN_RCMD */
189 1.1 cgd case '?':
190 1.1 cgd default:
191 1.1 cgd usage();
192 1.1 cgd }
193 1.1 cgd optind += argoff;
194 1.1 cgd
195 1.1 cgd /* if haven't gotten a host yet, do so */
196 1.1 cgd if (!host && !(host = argv[optind++]))
197 1.1 cgd usage();
198 1.1 cgd
199 1.1 cgd /* if no further arguments, must have been called as rlogin. */
200 1.1 cgd if (!argv[optind]) {
201 1.7 mrg #ifdef IN_RCMD
202 1.7 mrg usage();
203 1.7 mrg #else
204 1.1 cgd if (asrsh)
205 1.23 christos *argv = __UNCONST("rlogin");
206 1.1 cgd execv(_PATH_RLOGIN, argv);
207 1.5 mrg err(1, "can't exec %s", _PATH_RLOGIN);
208 1.7 mrg #endif
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd argc -= optind;
212 1.1 cgd argv += optind;
213 1.1 cgd
214 1.5 mrg /* Accept user1@host format, though "-l user2" overrides user1 */
215 1.5 mrg p = strchr(host, '@');
216 1.5 mrg if (p) {
217 1.5 mrg *p = '\0';
218 1.5 mrg if (!user && p > host)
219 1.5 mrg user = host;
220 1.5 mrg host = p + 1;
221 1.5 mrg if (*host == '\0')
222 1.5 mrg usage();
223 1.1 cgd }
224 1.1 cgd if (!user)
225 1.9 mrg user = name;
226 1.1 cgd
227 1.1 cgd
228 1.1 cgd args = copyargs(argv);
229 1.1 cgd
230 1.1 cgd if (sp == NULL)
231 1.1 cgd sp = getservbyname("shell", "tcp");
232 1.5 mrg if (sp == NULL)
233 1.5 mrg errx(1, "shell/tcp: unknown service");
234 1.1 cgd
235 1.7 mrg
236 1.7 mrg #ifdef IN_RCMD
237 1.13 itojun rem = orcmd_af(&host, sp->s_port, locuser ? locuser :
238 1.1 cgd #else
239 1.13 itojun rem = rcmd_af(&host, sp->s_port,
240 1.1 cgd #endif
241 1.26 ginsbach name, user, args, &remerr, family);
242 1.9 mrg (void)free(name);
243 1.1 cgd
244 1.1 cgd if (rem < 0)
245 1.1 cgd exit(1);
246 1.1 cgd
247 1.10 christos if (remerr < 0)
248 1.5 mrg errx(1, "can't establish stderr");
249 1.1 cgd if (dflag) {
250 1.1 cgd if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
251 1.1 cgd sizeof(one)) < 0)
252 1.7 mrg warn("setsockopt remote");
253 1.10 christos if (setsockopt(remerr, SOL_SOCKET, SO_DEBUG, &one,
254 1.1 cgd sizeof(one)) < 0)
255 1.7 mrg warn("setsockopt stderr");
256 1.1 cgd }
257 1.20 joff proto = getprotobyname("tcp");
258 1.20 joff setsockopt(rem, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
259 1.20 joff setsockopt(remerr, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
260 1.20 joff
261 1.1 cgd
262 1.25 ginsbach (void)setuid(uid);
263 1.10 christos
264 1.25 ginsbach (void)sigemptyset(&nset);
265 1.10 christos for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++)
266 1.25 ginsbach (void)sigaddset(&nset, sigs[i]);
267 1.10 christos
268 1.25 ginsbach (void)sigprocmask(SIG_BLOCK, &nset, &oset);
269 1.10 christos
270 1.36 enami #ifndef IN_RCMD
271 1.10 christos for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++) {
272 1.10 christos struct sigaction sa;
273 1.10 christos
274 1.10 christos if (sa.sa_handler != SIG_IGN) {
275 1.25 ginsbach sa.sa_handler = sendsig;
276 1.25 ginsbach (void)sigaction(sigs[i], &sa, NULL);
277 1.10 christos }
278 1.10 christos }
279 1.36 enami #endif
280 1.1 cgd
281 1.1 cgd if (!nflag) {
282 1.1 cgd pid = fork();
283 1.5 mrg if (pid < 0)
284 1.5 mrg err(1, "fork");
285 1.1 cgd }
286 1.10 christos else
287 1.10 christos pid = -1;
288 1.1 cgd
289 1.34 enami (void)ioctl(remerr, FIONBIO, &one);
290 1.34 enami (void)ioctl(rem, FIONBIO, &one);
291 1.1 cgd
292 1.10 christos talk(nflag, &oset, pid, rem);
293 1.1 cgd
294 1.1 cgd if (!nflag)
295 1.1 cgd (void)kill(pid, SIGKILL);
296 1.1 cgd exit(0);
297 1.1 cgd }
298 1.1 cgd
299 1.33 joerg static int
300 1.15 wiz checkfd(struct pollfd *fdp, int outfd)
301 1.10 christos {
302 1.10 christos int nr, nw;
303 1.10 christos char buf[BUFSIZ];
304 1.10 christos
305 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
306 1.10 christos return -1;
307 1.35 enami
308 1.10 christos if ((fdp->revents & POLLIN) == 0)
309 1.10 christos return 0;
310 1.10 christos
311 1.10 christos errno = 0;
312 1.34 enami nr = read(fdp->fd, buf, sizeof buf);
313 1.10 christos
314 1.10 christos if (nr <= 0) {
315 1.10 christos if (errno != EAGAIN)
316 1.10 christos return -1;
317 1.10 christos else
318 1.10 christos return 0;
319 1.10 christos }
320 1.10 christos else {
321 1.10 christos char *bc = buf;
322 1.10 christos while (nr) {
323 1.10 christos if ((nw = write(outfd, bc, nr)) <= 0)
324 1.10 christos return -1;
325 1.10 christos nr -= nw;
326 1.10 christos bc += nw;
327 1.10 christos }
328 1.10 christos return 0;
329 1.10 christos }
330 1.10 christos }
331 1.10 christos
332 1.33 joerg static void
333 1.15 wiz talk(int nflag, sigset_t *oset, __pid_t pid, int rem)
334 1.1 cgd {
335 1.10 christos int nr, nw, nfds;
336 1.36 enami struct pollfd fds[3], *fdp = &fds[0];
337 1.5 mrg char *bp, buf[BUFSIZ];
338 1.1 cgd
339 1.1 cgd if (!nflag && pid == 0) {
340 1.10 christos (void)close(remerr);
341 1.1 cgd
342 1.10 christos fdp->events = POLLOUT|POLLNVAL|POLLERR|POLLHUP;
343 1.10 christos fdp->fd = rem;
344 1.10 christos nr = 0;
345 1.1 cgd bp = buf;
346 1.1 cgd
347 1.10 christos for (;;) {
348 1.10 christos errno = 0;
349 1.10 christos
350 1.10 christos if (nr == 0) {
351 1.10 christos if ((nr = read(0, buf, sizeof buf)) == 0)
352 1.10 christos goto done;
353 1.10 christos if (nr == -1) {
354 1.10 christos if (errno == EIO)
355 1.10 christos goto done;
356 1.28 ginsbach if (errno == EINTR) {
357 1.28 ginsbach nr = 0;
358 1.10 christos continue;
359 1.28 ginsbach }
360 1.10 christos err(1, "read");
361 1.10 christos }
362 1.10 christos bp = buf;
363 1.10 christos }
364 1.10 christos
365 1.10 christos rewrite: if (poll(fdp, 1, INFTIM) == -1) {
366 1.10 christos if (errno != EINTR)
367 1.10 christos err(1, "poll");
368 1.10 christos goto rewrite;
369 1.10 christos }
370 1.10 christos
371 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
372 1.7 mrg err(1, "poll");
373 1.10 christos
374 1.10 christos if ((fdp->revents & POLLOUT) == 0)
375 1.10 christos goto rewrite;
376 1.10 christos
377 1.34 enami nw = write(rem, bp, nr);
378 1.10 christos
379 1.10 christos if (nw < 0) {
380 1.10 christos if (errno == EAGAIN)
381 1.10 christos continue;
382 1.10 christos err(1, "write");
383 1.10 christos }
384 1.10 christos bp += nw;
385 1.10 christos nr -= nw;
386 1.1 cgd }
387 1.1 cgd done:
388 1.1 cgd (void)shutdown(rem, 1);
389 1.1 cgd exit(0);
390 1.1 cgd }
391 1.1 cgd
392 1.36 enami #ifdef IN_RCMD
393 1.36 enami fdp = &fds[0];
394 1.36 enami nfds = 3;
395 1.36 enami fds[0].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
396 1.36 enami fds[0].fd = 2;
397 1.36 enami #else
398 1.25 ginsbach (void)sigprocmask(SIG_SETMASK, oset, NULL);
399 1.36 enami fdp = &fds[1];
400 1.7 mrg nfds = 2;
401 1.36 enami fds[0].events = 0;
402 1.36 enami #endif
403 1.36 enami fds[1].events = fds[2].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
404 1.36 enami fds[1].fd = remerr;
405 1.36 enami fds[2].fd = rem;
406 1.1 cgd do {
407 1.10 christos if (poll(fdp, nfds, INFTIM) == -1) {
408 1.5 mrg if (errno != EINTR)
409 1.7 mrg err(1, "poll");
410 1.1 cgd continue;
411 1.1 cgd }
412 1.36 enami if ((fds[1].events != 0 && checkfd(&fds[1], 2) == -1)
413 1.36 enami #ifdef IN_RCMD
414 1.36 enami || (fds[0].events != 0 && checkfd(&fds[0], remerr) == -1)
415 1.36 enami #endif
416 1.36 enami ) {
417 1.36 enami nfds--;
418 1.36 enami fds[1].events = 0;
419 1.36 enami #ifdef IN_RCMD
420 1.10 christos nfds--;
421 1.10 christos fds[0].events = 0;
422 1.36 enami #endif
423 1.36 enami fdp = &fds[2];
424 1.1 cgd }
425 1.36 enami if (fds[2].events != 0 && checkfd(&fds[2], 1) == -1) {
426 1.10 christos nfds--;
427 1.36 enami fds[2].events = 0;
428 1.1 cgd }
429 1.10 christos }
430 1.10 christos while (nfds);
431 1.1 cgd }
432 1.1 cgd
433 1.36 enami #ifndef IN_RCMD
434 1.33 joerg static void
435 1.15 wiz sendsig(int sig)
436 1.5 mrg {
437 1.1 cgd char signo;
438 1.5 mrg
439 1.5 mrg signo = sig;
440 1.35 enami (void)write(remerr, &signo, 1);
441 1.1 cgd }
442 1.36 enami #endif
443 1.1 cgd
444 1.1 cgd
445 1.33 joerg static char *
446 1.15 wiz copyargs(char **argv)
447 1.1 cgd {
448 1.5 mrg int cc;
449 1.18 itojun char **ap, *args, *p, *ep;
450 1.1 cgd
451 1.1 cgd cc = 0;
452 1.1 cgd for (ap = argv; *ap; ++ap)
453 1.1 cgd cc += strlen(*ap) + 1;
454 1.5 mrg if (!(args = malloc((u_int)cc)))
455 1.10 christos err(1, "malloc");
456 1.18 itojun ep = args + cc;
457 1.7 mrg for (p = args, *p = '\0', ap = argv; *ap; ++ap) {
458 1.18 itojun (void)strlcpy(p, *ap, ep - p);
459 1.7 mrg p += strlen(p);
460 1.1 cgd if (ap[1])
461 1.1 cgd *p++ = ' ';
462 1.1 cgd }
463 1.7 mrg *p = '\0';
464 1.5 mrg return (args);
465 1.1 cgd }
466 1.1 cgd
467 1.33 joerg static void
468 1.15 wiz usage(void)
469 1.1 cgd {
470 1.5 mrg
471 1.1 cgd (void)fprintf(stderr,
472 1.29 wiz "usage: %s [-46dn] [-l login] [-p port]%s [login@]host command\n",
473 1.29 wiz getprogname(),
474 1.7 mrg #ifdef IN_RCMD
475 1.29 wiz " [-u locuser]"
476 1.1 cgd #else
477 1.29 wiz ""
478 1.1 cgd #endif
479 1.7 mrg );
480 1.1 cgd exit(1);
481 1.1 cgd }
482