rsh.c revision 1.29 1 1.29 wiz /* $NetBSD: rsh.c,v 1.29 2006/03/23 23:49:07 wiz 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.11 christos __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993, 1994\n\
35 1.11 christos The Regents of the University of California. All rights reserved.\n");
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.29 wiz __RCSID("$NetBSD: rsh.c,v 1.29 2006/03/23 23:49:07 wiz 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.10 christos int argoff, asrsh, ch, dflag, nflag, one, rem, 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.7 mrg putenv("RCMD_LOOP=YES");
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.10 christos #if defined(KERBEROS) && defined(CRYPT)
286 1.1 cgd if (!doencrypt)
287 1.1 cgd #endif
288 1.1 cgd {
289 1.10 christos (void)ioctl(remerr, FIONBIO, &one);
290 1.1 cgd (void)ioctl(rem, FIONBIO, &one);
291 1.1 cgd }
292 1.1 cgd
293 1.10 christos talk(nflag, &oset, pid, rem);
294 1.1 cgd
295 1.1 cgd if (!nflag)
296 1.1 cgd (void)kill(pid, SIGKILL);
297 1.1 cgd exit(0);
298 1.1 cgd }
299 1.1 cgd
300 1.10 christos int
301 1.15 wiz checkfd(struct pollfd *fdp, int outfd)
302 1.10 christos {
303 1.10 christos int nr, nw;
304 1.10 christos char buf[BUFSIZ];
305 1.10 christos
306 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
307 1.10 christos return -1;
308 1.10 christos
309 1.10 christos if ((fdp->revents & POLLIN) == 0)
310 1.10 christos return 0;
311 1.10 christos
312 1.10 christos errno = 0;
313 1.10 christos #if defined(KERBEROS) && defined(CRYPT)
314 1.10 christos if (doencrypt)
315 1.10 christos nr = des_read(fdp->fd, buf, sizeof buf);
316 1.10 christos else
317 1.10 christos #endif
318 1.10 christos nr = read(fdp->fd, buf, sizeof buf);
319 1.10 christos
320 1.10 christos if (nr <= 0) {
321 1.10 christos if (errno != EAGAIN)
322 1.10 christos return -1;
323 1.10 christos else
324 1.10 christos return 0;
325 1.10 christos }
326 1.10 christos else {
327 1.10 christos char *bc = buf;
328 1.10 christos while (nr) {
329 1.10 christos if ((nw = write(outfd, bc, nr)) <= 0)
330 1.10 christos return -1;
331 1.10 christos nr -= nw;
332 1.10 christos bc += nw;
333 1.10 christos }
334 1.10 christos return 0;
335 1.10 christos }
336 1.10 christos }
337 1.10 christos
338 1.5 mrg void
339 1.15 wiz talk(int nflag, sigset_t *oset, __pid_t pid, int rem)
340 1.1 cgd {
341 1.10 christos int nr, nw, nfds;
342 1.7 mrg struct pollfd fds[2], *fdp = &fds[0];
343 1.5 mrg char *bp, buf[BUFSIZ];
344 1.1 cgd
345 1.1 cgd if (!nflag && pid == 0) {
346 1.10 christos (void)close(remerr);
347 1.1 cgd
348 1.10 christos fdp->events = POLLOUT|POLLNVAL|POLLERR|POLLHUP;
349 1.10 christos fdp->fd = rem;
350 1.10 christos nr = 0;
351 1.1 cgd bp = buf;
352 1.1 cgd
353 1.10 christos for (;;) {
354 1.10 christos errno = 0;
355 1.10 christos
356 1.10 christos if (nr == 0) {
357 1.10 christos if ((nr = read(0, buf, sizeof buf)) == 0)
358 1.10 christos goto done;
359 1.10 christos if (nr == -1) {
360 1.10 christos if (errno == EIO)
361 1.10 christos goto done;
362 1.28 ginsbach if (errno == EINTR) {
363 1.28 ginsbach nr = 0;
364 1.10 christos continue;
365 1.28 ginsbach }
366 1.10 christos err(1, "read");
367 1.10 christos }
368 1.10 christos bp = buf;
369 1.10 christos }
370 1.10 christos
371 1.10 christos rewrite: if (poll(fdp, 1, INFTIM) == -1) {
372 1.10 christos if (errno != EINTR)
373 1.10 christos err(1, "poll");
374 1.10 christos goto rewrite;
375 1.10 christos }
376 1.10 christos
377 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
378 1.7 mrg err(1, "poll");
379 1.10 christos
380 1.10 christos if ((fdp->revents & POLLOUT) == 0)
381 1.10 christos goto rewrite;
382 1.10 christos
383 1.10 christos #if defined(KERBEROS) && defined(CRYPT)
384 1.10 christos if (doencrypt)
385 1.10 christos nw = des_write(rem, bp, nr);
386 1.10 christos else
387 1.1 cgd #endif
388 1.10 christos nw = write(rem, bp, nr);
389 1.10 christos
390 1.10 christos if (nw < 0) {
391 1.10 christos if (errno == EAGAIN)
392 1.10 christos continue;
393 1.10 christos err(1, "write");
394 1.10 christos }
395 1.10 christos bp += nw;
396 1.10 christos nr -= nw;
397 1.1 cgd }
398 1.1 cgd done:
399 1.1 cgd (void)shutdown(rem, 1);
400 1.1 cgd exit(0);
401 1.1 cgd }
402 1.1 cgd
403 1.25 ginsbach (void)sigprocmask(SIG_SETMASK, oset, NULL);
404 1.10 christos fds[0].events = fds[1].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
405 1.10 christos fds[0].fd = remerr;
406 1.7 mrg fds[1].fd = rem;
407 1.7 mrg fdp = &fds[0];
408 1.7 mrg nfds = 2;
409 1.1 cgd do {
410 1.10 christos if (poll(fdp, nfds, INFTIM) == -1) {
411 1.5 mrg if (errno != EINTR)
412 1.7 mrg err(1, "poll");
413 1.1 cgd continue;
414 1.1 cgd }
415 1.10 christos if (fds[0].events != 0 && checkfd(&fds[0], 2) == -1) {
416 1.10 christos nfds--;
417 1.10 christos fds[0].events = 0;
418 1.10 christos fdp = &fds[1];
419 1.1 cgd }
420 1.10 christos if (fds[1].events != 0 && checkfd(&fds[1], 1) == -1) {
421 1.10 christos nfds--;
422 1.10 christos fds[1].events = 0;
423 1.1 cgd }
424 1.10 christos }
425 1.10 christos while (nfds);
426 1.1 cgd }
427 1.1 cgd
428 1.1 cgd void
429 1.15 wiz sendsig(int sig)
430 1.5 mrg {
431 1.1 cgd char signo;
432 1.5 mrg
433 1.5 mrg signo = sig;
434 1.10 christos (void)write(remerr, &signo, 1);
435 1.1 cgd }
436 1.1 cgd
437 1.1 cgd
438 1.1 cgd char *
439 1.15 wiz copyargs(char **argv)
440 1.1 cgd {
441 1.5 mrg int cc;
442 1.18 itojun char **ap, *args, *p, *ep;
443 1.1 cgd
444 1.1 cgd cc = 0;
445 1.1 cgd for (ap = argv; *ap; ++ap)
446 1.1 cgd cc += strlen(*ap) + 1;
447 1.5 mrg if (!(args = malloc((u_int)cc)))
448 1.10 christos err(1, "malloc");
449 1.18 itojun ep = args + cc;
450 1.7 mrg for (p = args, *p = '\0', ap = argv; *ap; ++ap) {
451 1.18 itojun (void)strlcpy(p, *ap, ep - p);
452 1.7 mrg p += strlen(p);
453 1.1 cgd if (ap[1])
454 1.1 cgd *p++ = ' ';
455 1.1 cgd }
456 1.7 mrg *p = '\0';
457 1.5 mrg return (args);
458 1.1 cgd }
459 1.1 cgd
460 1.5 mrg void
461 1.15 wiz usage(void)
462 1.1 cgd {
463 1.5 mrg
464 1.1 cgd (void)fprintf(stderr,
465 1.29 wiz "usage: %s [-46dn] [-l login] [-p port]%s [login@]host command\n",
466 1.29 wiz getprogname(),
467 1.7 mrg #ifdef IN_RCMD
468 1.29 wiz " [-u locuser]"
469 1.1 cgd #else
470 1.29 wiz ""
471 1.1 cgd #endif
472 1.7 mrg );
473 1.1 cgd exit(1);
474 1.1 cgd }
475