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