rsh.c revision 1.34 1 1.34 enami /* $NetBSD: rsh.c,v 1.34 2014/06/08 01:44:52 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.34 enami __RCSID("$NetBSD: rsh.c,v 1.34 2014/06/08 01:44:52 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.33 joerg static void sendsig(int);
80 1.33 joerg static int checkfd(struct pollfd *, int);
81 1.33 joerg static void talk(int, sigset_t *, pid_t, int);
82 1.33 joerg __dead static void usage(void);
83 1.12 christos #ifdef IN_RCMD
84 1.15 wiz int orcmd(char **, int, const char *,
85 1.15 wiz const char *, const char *, int *);
86 1.15 wiz int orcmd_af(char **, int, const char *,
87 1.15 wiz const char *, const char *, int *, int);
88 1.12 christos #endif
89 1.5 mrg
90 1.5 mrg int
91 1.15 wiz main(int argc, char **argv)
92 1.1 cgd {
93 1.1 cgd struct passwd *pw;
94 1.1 cgd struct servent *sp;
95 1.10 christos sigset_t oset, nset;
96 1.20 joff struct protoent *proto;
97 1.10 christos
98 1.7 mrg #ifdef IN_RCMD
99 1.7 mrg char *locuser = 0, *loop;
100 1.7 mrg #endif /* IN_RCMD */
101 1.31 lukem int argoff, asrsh, ch, dflag, nflag, one, rem;
102 1.31 lukem size_t i;
103 1.26 ginsbach int family = AF_UNSPEC;
104 1.5 mrg pid_t pid;
105 1.5 mrg uid_t uid;
106 1.9 mrg char *args, *host, *p, *user, *name;
107 1.1 cgd
108 1.1 cgd argoff = asrsh = dflag = nflag = 0;
109 1.1 cgd one = 1;
110 1.1 cgd host = user = NULL;
111 1.17 hubertf sp = NULL;
112 1.1 cgd
113 1.8 mrg #ifndef IN_RCMD
114 1.7 mrg /*
115 1.8 mrg * If called as something other than "rsh" use it as the host name,
116 1.8 mrg * only for rsh.
117 1.7 mrg */
118 1.14 cgd if (strcmp(getprogname(), "rsh") == 0)
119 1.7 mrg asrsh = 1;
120 1.14 cgd else {
121 1.14 cgd host = strdup(getprogname());
122 1.14 cgd if (host == NULL)
123 1.14 cgd err(1, NULL);
124 1.14 cgd }
125 1.8 mrg #endif /* IN_RCMD */
126 1.1 cgd
127 1.1 cgd /* handle "rsh host flags" */
128 1.1 cgd if (!host && argc > 2 && argv[1][0] != '-') {
129 1.1 cgd host = argv[1];
130 1.1 cgd argoff = 1;
131 1.1 cgd }
132 1.1 cgd
133 1.7 mrg #ifdef IN_RCMD
134 1.7 mrg if ((loop = getenv("RCMD_LOOP")) && strcmp(loop, "YES") == 0)
135 1.7 mrg warnx("rcmd appears to be looping!");
136 1.7 mrg
137 1.32 gson setenv("RCMD_LOOP", "YES", 1);
138 1.7 mrg
139 1.26 ginsbach # define OPTIONS "468KLdel:np:u:w"
140 1.7 mrg
141 1.7 mrg #else /* IN_RCMD */
142 1.7 mrg
143 1.26 ginsbach # define OPTIONS "468KLdel:np:w"
144 1.7 mrg
145 1.7 mrg #endif /* IN_RCMD */
146 1.7 mrg
147 1.7 mrg if (!(pw = getpwuid(uid = getuid())))
148 1.7 mrg errx(1, "unknown user id");
149 1.7 mrg
150 1.9 mrg if ((name = strdup(pw->pw_name)) == NULL)
151 1.9 mrg err(1, "malloc");
152 1.10 christos while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
153 1.1 cgd switch(ch) {
154 1.26 ginsbach case '4':
155 1.26 ginsbach family = AF_INET;
156 1.26 ginsbach break;
157 1.26 ginsbach case '6':
158 1.26 ginsbach family = AF_INET6;
159 1.26 ginsbach break;
160 1.1 cgd case 'K':
161 1.1 cgd break;
162 1.1 cgd case 'L': /* -8Lew are ignored to allow rlogin aliases */
163 1.1 cgd case 'e':
164 1.1 cgd case 'w':
165 1.1 cgd case '8':
166 1.1 cgd break;
167 1.1 cgd case 'd':
168 1.1 cgd dflag = 1;
169 1.1 cgd break;
170 1.1 cgd case 'l':
171 1.1 cgd user = optarg;
172 1.1 cgd break;
173 1.1 cgd case 'n':
174 1.1 cgd nflag = 1;
175 1.1 cgd break;
176 1.17 hubertf case 'p':
177 1.23 christos sp = getport(optarg, "tcp");
178 1.17 hubertf break;
179 1.7 mrg #ifdef IN_RCMD
180 1.7 mrg case 'u':
181 1.9 mrg if (getuid() != 0 && optarg && name &&
182 1.9 mrg strcmp(name, optarg) != 0)
183 1.7 mrg errx(1,"only super user can use the -u option");
184 1.7 mrg locuser = optarg;
185 1.7 mrg break;
186 1.7 mrg #endif /* IN_RCMD */
187 1.1 cgd case '?':
188 1.1 cgd default:
189 1.1 cgd usage();
190 1.1 cgd }
191 1.1 cgd optind += argoff;
192 1.1 cgd
193 1.1 cgd /* if haven't gotten a host yet, do so */
194 1.1 cgd if (!host && !(host = argv[optind++]))
195 1.1 cgd usage();
196 1.1 cgd
197 1.1 cgd /* if no further arguments, must have been called as rlogin. */
198 1.1 cgd if (!argv[optind]) {
199 1.7 mrg #ifdef IN_RCMD
200 1.7 mrg usage();
201 1.7 mrg #else
202 1.1 cgd if (asrsh)
203 1.23 christos *argv = __UNCONST("rlogin");
204 1.1 cgd execv(_PATH_RLOGIN, argv);
205 1.5 mrg err(1, "can't exec %s", _PATH_RLOGIN);
206 1.7 mrg #endif
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd argc -= optind;
210 1.1 cgd argv += optind;
211 1.1 cgd
212 1.5 mrg /* Accept user1@host format, though "-l user2" overrides user1 */
213 1.5 mrg p = strchr(host, '@');
214 1.5 mrg if (p) {
215 1.5 mrg *p = '\0';
216 1.5 mrg if (!user && p > host)
217 1.5 mrg user = host;
218 1.5 mrg host = p + 1;
219 1.5 mrg if (*host == '\0')
220 1.5 mrg usage();
221 1.1 cgd }
222 1.1 cgd if (!user)
223 1.9 mrg user = name;
224 1.1 cgd
225 1.1 cgd
226 1.1 cgd args = copyargs(argv);
227 1.1 cgd
228 1.1 cgd if (sp == NULL)
229 1.1 cgd sp = getservbyname("shell", "tcp");
230 1.5 mrg if (sp == NULL)
231 1.5 mrg errx(1, "shell/tcp: unknown service");
232 1.1 cgd
233 1.7 mrg
234 1.7 mrg #ifdef IN_RCMD
235 1.13 itojun rem = orcmd_af(&host, sp->s_port, locuser ? locuser :
236 1.1 cgd #else
237 1.13 itojun rem = rcmd_af(&host, sp->s_port,
238 1.1 cgd #endif
239 1.26 ginsbach name, user, args, &remerr, family);
240 1.9 mrg (void)free(name);
241 1.1 cgd
242 1.1 cgd if (rem < 0)
243 1.1 cgd exit(1);
244 1.1 cgd
245 1.10 christos if (remerr < 0)
246 1.5 mrg errx(1, "can't establish stderr");
247 1.1 cgd if (dflag) {
248 1.1 cgd if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
249 1.1 cgd sizeof(one)) < 0)
250 1.7 mrg warn("setsockopt remote");
251 1.10 christos if (setsockopt(remerr, SOL_SOCKET, SO_DEBUG, &one,
252 1.1 cgd sizeof(one)) < 0)
253 1.7 mrg warn("setsockopt stderr");
254 1.1 cgd }
255 1.20 joff proto = getprotobyname("tcp");
256 1.20 joff setsockopt(rem, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
257 1.20 joff setsockopt(remerr, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
258 1.20 joff
259 1.1 cgd
260 1.25 ginsbach (void)setuid(uid);
261 1.10 christos
262 1.25 ginsbach (void)sigemptyset(&nset);
263 1.10 christos for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++)
264 1.25 ginsbach (void)sigaddset(&nset, sigs[i]);
265 1.10 christos
266 1.25 ginsbach (void)sigprocmask(SIG_BLOCK, &nset, &oset);
267 1.10 christos
268 1.10 christos for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++) {
269 1.10 christos struct sigaction sa;
270 1.10 christos
271 1.10 christos if (sa.sa_handler != SIG_IGN) {
272 1.25 ginsbach sa.sa_handler = sendsig;
273 1.25 ginsbach (void)sigaction(sigs[i], &sa, NULL);
274 1.10 christos }
275 1.10 christos }
276 1.1 cgd
277 1.1 cgd if (!nflag) {
278 1.1 cgd pid = fork();
279 1.5 mrg if (pid < 0)
280 1.5 mrg err(1, "fork");
281 1.1 cgd }
282 1.10 christos else
283 1.10 christos pid = -1;
284 1.1 cgd
285 1.34 enami (void)ioctl(remerr, FIONBIO, &one);
286 1.34 enami (void)ioctl(rem, FIONBIO, &one);
287 1.1 cgd
288 1.10 christos talk(nflag, &oset, pid, rem);
289 1.1 cgd
290 1.1 cgd if (!nflag)
291 1.1 cgd (void)kill(pid, SIGKILL);
292 1.1 cgd exit(0);
293 1.1 cgd }
294 1.1 cgd
295 1.33 joerg static int
296 1.15 wiz checkfd(struct pollfd *fdp, int outfd)
297 1.10 christos {
298 1.10 christos int nr, nw;
299 1.10 christos char buf[BUFSIZ];
300 1.10 christos
301 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
302 1.10 christos return -1;
303 1.10 christos
304 1.10 christos if ((fdp->revents & POLLIN) == 0)
305 1.10 christos return 0;
306 1.10 christos
307 1.10 christos errno = 0;
308 1.34 enami nr = read(fdp->fd, buf, sizeof buf);
309 1.10 christos
310 1.10 christos if (nr <= 0) {
311 1.10 christos if (errno != EAGAIN)
312 1.10 christos return -1;
313 1.10 christos else
314 1.10 christos return 0;
315 1.10 christos }
316 1.10 christos else {
317 1.10 christos char *bc = buf;
318 1.10 christos while (nr) {
319 1.10 christos if ((nw = write(outfd, bc, nr)) <= 0)
320 1.10 christos return -1;
321 1.10 christos nr -= nw;
322 1.10 christos bc += nw;
323 1.10 christos }
324 1.10 christos return 0;
325 1.10 christos }
326 1.10 christos }
327 1.10 christos
328 1.33 joerg static void
329 1.15 wiz talk(int nflag, sigset_t *oset, __pid_t pid, int rem)
330 1.1 cgd {
331 1.10 christos int nr, nw, nfds;
332 1.7 mrg struct pollfd fds[2], *fdp = &fds[0];
333 1.5 mrg char *bp, buf[BUFSIZ];
334 1.1 cgd
335 1.1 cgd if (!nflag && pid == 0) {
336 1.10 christos (void)close(remerr);
337 1.1 cgd
338 1.10 christos fdp->events = POLLOUT|POLLNVAL|POLLERR|POLLHUP;
339 1.10 christos fdp->fd = rem;
340 1.10 christos nr = 0;
341 1.1 cgd bp = buf;
342 1.1 cgd
343 1.10 christos for (;;) {
344 1.10 christos errno = 0;
345 1.10 christos
346 1.10 christos if (nr == 0) {
347 1.10 christos if ((nr = read(0, buf, sizeof buf)) == 0)
348 1.10 christos goto done;
349 1.10 christos if (nr == -1) {
350 1.10 christos if (errno == EIO)
351 1.10 christos goto done;
352 1.28 ginsbach if (errno == EINTR) {
353 1.28 ginsbach nr = 0;
354 1.10 christos continue;
355 1.28 ginsbach }
356 1.10 christos err(1, "read");
357 1.10 christos }
358 1.10 christos bp = buf;
359 1.10 christos }
360 1.10 christos
361 1.10 christos rewrite: if (poll(fdp, 1, INFTIM) == -1) {
362 1.10 christos if (errno != EINTR)
363 1.10 christos err(1, "poll");
364 1.10 christos goto rewrite;
365 1.10 christos }
366 1.10 christos
367 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
368 1.7 mrg err(1, "poll");
369 1.10 christos
370 1.10 christos if ((fdp->revents & POLLOUT) == 0)
371 1.10 christos goto rewrite;
372 1.10 christos
373 1.34 enami nw = write(rem, bp, nr);
374 1.10 christos
375 1.10 christos if (nw < 0) {
376 1.10 christos if (errno == EAGAIN)
377 1.10 christos continue;
378 1.10 christos err(1, "write");
379 1.10 christos }
380 1.10 christos bp += nw;
381 1.10 christos nr -= nw;
382 1.1 cgd }
383 1.1 cgd done:
384 1.1 cgd (void)shutdown(rem, 1);
385 1.1 cgd exit(0);
386 1.1 cgd }
387 1.1 cgd
388 1.25 ginsbach (void)sigprocmask(SIG_SETMASK, oset, NULL);
389 1.10 christos fds[0].events = fds[1].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
390 1.10 christos fds[0].fd = remerr;
391 1.7 mrg fds[1].fd = rem;
392 1.7 mrg fdp = &fds[0];
393 1.7 mrg nfds = 2;
394 1.1 cgd do {
395 1.10 christos if (poll(fdp, nfds, INFTIM) == -1) {
396 1.5 mrg if (errno != EINTR)
397 1.7 mrg err(1, "poll");
398 1.1 cgd continue;
399 1.1 cgd }
400 1.10 christos if (fds[0].events != 0 && checkfd(&fds[0], 2) == -1) {
401 1.10 christos nfds--;
402 1.10 christos fds[0].events = 0;
403 1.10 christos fdp = &fds[1];
404 1.1 cgd }
405 1.10 christos if (fds[1].events != 0 && checkfd(&fds[1], 1) == -1) {
406 1.10 christos nfds--;
407 1.10 christos fds[1].events = 0;
408 1.1 cgd }
409 1.10 christos }
410 1.10 christos while (nfds);
411 1.1 cgd }
412 1.1 cgd
413 1.33 joerg static void
414 1.15 wiz sendsig(int sig)
415 1.5 mrg {
416 1.1 cgd char signo;
417 1.5 mrg
418 1.5 mrg signo = sig;
419 1.10 christos (void)write(remerr, &signo, 1);
420 1.1 cgd }
421 1.1 cgd
422 1.1 cgd
423 1.33 joerg static char *
424 1.15 wiz copyargs(char **argv)
425 1.1 cgd {
426 1.5 mrg int cc;
427 1.18 itojun char **ap, *args, *p, *ep;
428 1.1 cgd
429 1.1 cgd cc = 0;
430 1.1 cgd for (ap = argv; *ap; ++ap)
431 1.1 cgd cc += strlen(*ap) + 1;
432 1.5 mrg if (!(args = malloc((u_int)cc)))
433 1.10 christos err(1, "malloc");
434 1.18 itojun ep = args + cc;
435 1.7 mrg for (p = args, *p = '\0', ap = argv; *ap; ++ap) {
436 1.18 itojun (void)strlcpy(p, *ap, ep - p);
437 1.7 mrg p += strlen(p);
438 1.1 cgd if (ap[1])
439 1.1 cgd *p++ = ' ';
440 1.1 cgd }
441 1.7 mrg *p = '\0';
442 1.5 mrg return (args);
443 1.1 cgd }
444 1.1 cgd
445 1.33 joerg static void
446 1.15 wiz usage(void)
447 1.1 cgd {
448 1.5 mrg
449 1.1 cgd (void)fprintf(stderr,
450 1.29 wiz "usage: %s [-46dn] [-l login] [-p port]%s [login@]host command\n",
451 1.29 wiz getprogname(),
452 1.7 mrg #ifdef IN_RCMD
453 1.29 wiz " [-u locuser]"
454 1.1 cgd #else
455 1.29 wiz ""
456 1.1 cgd #endif
457 1.7 mrg );
458 1.1 cgd exit(1);
459 1.1 cgd }
460