rsh.c revision 1.16 1 /* $NetBSD: rsh.c,v 1.16 2002/11/16 13:47:34 itojun Exp $ */
2
3 /*-
4 * Copyright (c) 1983, 1990, 1993, 1994
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, 1990, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)rsh.c 8.4 (Berkeley) 4/29/95";
45 #else
46 __RCSID("$NetBSD: rsh.c,v 1.16 2002/11/16 13:47:34 itojun Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <sys/ioctl.h>
53 #include <sys/file.h>
54 #include <poll.h>
55
56 #include <netinet/in.h>
57 #include <netdb.h>
58
59 #include <err.h>
60 #include <errno.h>
61 #include <pwd.h>
62 #include <signal.h>
63 #include <stdarg.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68
69 #include "pathnames.h"
70
71 #ifdef KERBEROS
72 #include <kerberosIV/des.h>
73 #include <kerberosIV/krb.h>
74
75 CREDENTIALS cred;
76 Key_schedule schedule;
77 int use_kerberos = 1, doencrypt;
78 char dst_realm_buf[REALM_SZ], *dest_realm;
79
80 void warning(const char *, ...);
81 #endif
82
83 /*
84 * rsh - remote shell
85 */
86 int remerr;
87
88 static int sigs[] = { SIGINT, SIGTERM, SIGQUIT };
89
90 char *copyargs(char **);
91 void sendsig(int);
92 int checkfd(struct pollfd *, int);
93 void talk(int, sigset_t *, pid_t, int);
94 void usage(void);
95 int main(int, char **);
96 #ifdef IN_RCMD
97 int orcmd(char **, int, const char *,
98 const char *, const char *, int *);
99 int orcmd_af(char **, int, const char *,
100 const char *, const char *, int *, int);
101 #endif
102
103 int
104 main(int argc, char **argv)
105 {
106 struct passwd *pw;
107 struct servent *sp;
108 sigset_t oset, nset;
109
110 #ifdef IN_RCMD
111 char *locuser = 0, *loop;
112 #endif /* IN_RCMD */
113 int argoff, asrsh, ch, dflag, nflag, one, rem, i;
114 pid_t pid;
115 uid_t uid;
116 char *args, *host, *p, *user, *name;
117
118 argoff = asrsh = dflag = nflag = 0;
119 one = 1;
120 host = user = NULL;
121
122 #ifndef IN_RCMD
123 /*
124 * If called as something other than "rsh" use it as the host name,
125 * only for rsh.
126 */
127 if (strcmp(getprogname(), "rsh") == 0)
128 asrsh = 1;
129 else {
130 host = strdup(getprogname());
131 if (host == NULL)
132 err(1, NULL);
133 }
134 #endif /* IN_RCMD */
135
136 /* handle "rsh host flags" */
137 if (!host && argc > 2 && argv[1][0] != '-') {
138 host = argv[1];
139 argoff = 1;
140 }
141
142 #ifdef IN_RCMD
143 if ((loop = getenv("RCMD_LOOP")) && strcmp(loop, "YES") == 0)
144 warnx("rcmd appears to be looping!");
145
146 putenv("RCMD_LOOP=YES");
147
148 # ifdef KERBEROS
149 # ifdef CRYPT
150 # define OPTIONS "8KLdek:l:nu:wx"
151 # else
152 # define OPTIONS "8KLdek:l:nu:w"
153 # endif
154 # else
155 # define OPTIONS "8KLdel:nu:w"
156 # endif
157
158 #else /* IN_RCMD */
159
160 # ifdef KERBEROS
161 # ifdef CRYPT
162 # define OPTIONS "8KLdek:l:nwx"
163 # else
164 # define OPTIONS "8KLdek:l:nw"
165 # endif
166 # else
167 # define OPTIONS "8KLdel:nw"
168 # endif
169
170 #endif /* IN_RCMD */
171
172 if (!(pw = getpwuid(uid = getuid())))
173 errx(1, "unknown user id");
174
175 if ((name = strdup(pw->pw_name)) == NULL)
176 err(1, "malloc");
177 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
178 switch(ch) {
179 case 'K':
180 #ifdef KERBEROS
181 use_kerberos = 0;
182 #endif
183 break;
184 case 'L': /* -8Lew are ignored to allow rlogin aliases */
185 case 'e':
186 case 'w':
187 case '8':
188 break;
189 case 'd':
190 dflag = 1;
191 break;
192 case 'l':
193 user = optarg;
194 break;
195 #ifdef KERBEROS
196 case 'k':
197 strlcpy(dest_realm_buf, optarg, sizeof(dest_realm_buf));
198 dest_realm = dst_realm_buf;
199 break;
200 #endif
201 case 'n':
202 nflag = 1;
203 break;
204 #ifdef IN_RCMD
205 case 'u':
206 if (getuid() != 0 && optarg && name &&
207 strcmp(name, optarg) != 0)
208 errx(1,"only super user can use the -u option");
209 locuser = optarg;
210 break;
211 #endif /* IN_RCMD */
212 #ifdef KERBEROS
213 #ifdef CRYPT
214 case 'x':
215 doencrypt = 1;
216 des_set_key((des_cblock *) cred.session, schedule);
217 break;
218 #endif
219 #endif
220 case '?':
221 default:
222 usage();
223 }
224 optind += argoff;
225
226 /* if haven't gotten a host yet, do so */
227 if (!host && !(host = argv[optind++]))
228 usage();
229
230 /* if no further arguments, must have been called as rlogin. */
231 if (!argv[optind]) {
232 #ifdef IN_RCMD
233 usage();
234 #else
235 if (asrsh)
236 *argv = "rlogin";
237 execv(_PATH_RLOGIN, argv);
238 err(1, "can't exec %s", _PATH_RLOGIN);
239 #endif
240 }
241
242 argc -= optind;
243 argv += optind;
244
245 /* Accept user1@host format, though "-l user2" overrides user1 */
246 p = strchr(host, '@');
247 if (p) {
248 *p = '\0';
249 if (!user && p > host)
250 user = host;
251 host = p + 1;
252 if (*host == '\0')
253 usage();
254 }
255 if (!user)
256 user = name;
257
258 #ifdef KERBEROS
259 #ifdef CRYPT
260 /* -x turns off -n */
261 if (doencrypt)
262 nflag = 0;
263 #endif
264 #endif
265
266 args = copyargs(argv);
267
268 sp = NULL;
269 #ifdef KERBEROS
270 if (use_kerberos) {
271 sp = getservbyname((doencrypt ? "ekshell" : "kshell"), "tcp");
272 if (sp == NULL) {
273 use_kerberos = 0;
274 warning("can't get entry for %s/tcp service",
275 doencrypt ? "ekshell" : "kshell");
276 }
277 }
278 #endif
279 if (sp == NULL)
280 sp = getservbyname("shell", "tcp");
281 if (sp == NULL)
282 errx(1, "shell/tcp: unknown service");
283
284 #ifdef KERBEROS
285 try_connect:
286 if (use_kerberos) {
287 #if 1
288 struct hostent *hp;
289
290 /* fully qualify hostname (needed for krb_realmofhost) */
291 hp = gethostbyname(host);
292 if (hp != NULL && !(host = strdup(hp->h_name)))
293 err(1, "strdup");
294 #endif
295
296 rem = KSUCCESS;
297 errno = 0;
298 if (dest_realm == NULL)
299 dest_realm = krb_realmofhost(host);
300
301 #ifdef CRYPT
302 if (doencrypt)
303 rem = krcmd_mutual(&host, sp->s_port, user, args,
304 &remerr, dest_realm, &cred, schedule);
305 else
306 #endif
307 rem = krcmd(&host, sp->s_port, user, args, &remerr,
308 dest_realm);
309 if (rem < 0) {
310 use_kerberos = 0;
311 sp = getservbyname("shell", "tcp");
312 if (sp == NULL)
313 errx(1, "shell/tcp: unknown service");
314 if (errno == ECONNREFUSED)
315 warning("remote host doesn't support Kerberos");
316 if (errno == ENOENT)
317 warning("can't provide Kerberos auth data");
318 goto try_connect;
319 }
320 } else {
321 if (doencrypt)
322 errx(1, "the -x flag requires Kerberos authentication.");
323 #ifdef IN_RCMD
324 rem = orcmd_af(&host, sp->s_port, locuser ? locuser :
325 #else
326 rem = rcmd_af(&host, sp->s_port,
327 #endif
328 name,
329 user, args, &remerr, PF_UNSPEC);
330 }
331 #else /* KERBEROS */
332
333 #ifdef IN_RCMD
334 rem = orcmd_af(&host, sp->s_port, locuser ? locuser :
335 #else
336 rem = rcmd_af(&host, sp->s_port,
337 #endif
338 name, user, args, &remerr, PF_UNSPEC);
339 #endif /* KERBEROS */
340 (void)free(name);
341
342 if (rem < 0)
343 exit(1);
344
345 if (remerr < 0)
346 errx(1, "can't establish stderr");
347 if (dflag) {
348 if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
349 sizeof(one)) < 0)
350 warn("setsockopt remote");
351 if (setsockopt(remerr, SOL_SOCKET, SO_DEBUG, &one,
352 sizeof(one)) < 0)
353 warn("setsockopt stderr");
354 }
355
356 (void) setuid(uid);
357
358 (void) sigemptyset(&nset);
359 for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++)
360 (void) sigaddset(&nset, sigs[i]);
361
362 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
363
364 for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++) {
365 struct sigaction sa;
366
367 if (sa.sa_handler != SIG_IGN) {
368 sa.sa_handler = sendsig;
369 (void) sigaction(sigs[i], &sa, NULL);
370 }
371 }
372
373 if (!nflag) {
374 pid = fork();
375 if (pid < 0)
376 err(1, "fork");
377 }
378 else
379 pid = -1;
380
381 #if defined(KERBEROS) && defined(CRYPT)
382 if (!doencrypt)
383 #endif
384 {
385 (void)ioctl(remerr, FIONBIO, &one);
386 (void)ioctl(rem, FIONBIO, &one);
387 }
388
389 talk(nflag, &oset, pid, rem);
390
391 if (!nflag)
392 (void)kill(pid, SIGKILL);
393 exit(0);
394 }
395
396 int
397 checkfd(struct pollfd *fdp, int outfd)
398 {
399 int nr, nw;
400 char buf[BUFSIZ];
401
402 if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
403 return -1;
404
405 if ((fdp->revents & POLLIN) == 0)
406 return 0;
407
408 errno = 0;
409 #if defined(KERBEROS) && defined(CRYPT)
410 if (doencrypt)
411 nr = des_read(fdp->fd, buf, sizeof buf);
412 else
413 #endif
414 nr = read(fdp->fd, buf, sizeof buf);
415
416 if (nr <= 0) {
417 if (errno != EAGAIN)
418 return -1;
419 else
420 return 0;
421 }
422 else {
423 char *bc = buf;
424 while (nr) {
425 if ((nw = write(outfd, bc, nr)) <= 0)
426 return -1;
427 nr -= nw;
428 bc += nw;
429 }
430 return 0;
431 }
432 }
433
434 void
435 talk(int nflag, sigset_t *oset, __pid_t pid, int rem)
436 {
437 int nr, nw, nfds;
438 struct pollfd fds[2], *fdp = &fds[0];
439 char *bp, buf[BUFSIZ];
440
441
442 if (!nflag && pid == 0) {
443 (void)close(remerr);
444
445 fdp->events = POLLOUT|POLLNVAL|POLLERR|POLLHUP;
446 fdp->fd = rem;
447 nr = 0;
448 bp = buf;
449
450 for (;;) {
451 errno = 0;
452
453 if (nr == 0) {
454 if ((nr = read(0, buf, sizeof buf)) == 0)
455 goto done;
456 if (nr == -1) {
457 if (errno == EIO)
458 goto done;
459 if (errno == EINTR)
460 continue;
461 err(1, "read");
462 }
463 bp = buf;
464 }
465
466 rewrite: if (poll(fdp, 1, INFTIM) == -1) {
467 if (errno != EINTR)
468 err(1, "poll");
469 goto rewrite;
470 }
471
472 if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
473 err(1, "poll");
474
475 if ((fdp->revents & POLLOUT) == 0)
476 goto rewrite;
477
478 #if defined(KERBEROS) && defined(CRYPT)
479 if (doencrypt)
480 nw = des_write(rem, bp, nr);
481 else
482 #endif
483 nw = write(rem, bp, nr);
484
485 if (nw < 0) {
486 if (errno == EAGAIN)
487 continue;
488 err(1, "write");
489 }
490 bp += nw;
491 nr -= nw;
492 }
493 done:
494 (void)shutdown(rem, 1);
495 exit(0);
496 }
497
498 (void) sigprocmask(SIG_SETMASK, oset, NULL);
499 fds[0].events = fds[1].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
500 fds[0].fd = remerr;
501 fds[1].fd = rem;
502 fdp = &fds[0];
503 nfds = 2;
504 do {
505 if (poll(fdp, nfds, INFTIM) == -1) {
506 if (errno != EINTR)
507 err(1, "poll");
508 continue;
509 }
510 if (fds[0].events != 0 && checkfd(&fds[0], 2) == -1) {
511 nfds--;
512 fds[0].events = 0;
513 fdp = &fds[1];
514 }
515 if (fds[1].events != 0 && checkfd(&fds[1], 1) == -1) {
516 nfds--;
517 fds[1].events = 0;
518 }
519 }
520 while (nfds);
521 }
522
523 void
524 sendsig(int sig)
525 {
526 char signo;
527
528 signo = sig;
529 #ifdef KERBEROS
530 #ifdef CRYPT
531 if (doencrypt)
532 (void)des_write(remerr, &signo, 1);
533 else
534 #endif
535 #endif
536 (void)write(remerr, &signo, 1);
537 }
538
539 #ifdef KERBEROS
540 /* VARARGS */
541 void
542 warning(const char *fmt, ...)
543 {
544 va_list ap;
545
546 va_start(ap, fmt);
547 (void) fprintf(stderr, "%s: warning, using standard rsh: ",
548 getprogname());
549 (void) vfprintf(stderr, fmt, ap);
550 va_end(ap);
551 (void) fprintf(stderr, ".\n");
552 }
553 #endif
554
555 char *
556 copyargs(char **argv)
557 {
558 int cc;
559 char **ap, *args, *p;
560
561 cc = 0;
562 for (ap = argv; *ap; ++ap)
563 cc += strlen(*ap) + 1;
564 if (!(args = malloc((u_int)cc)))
565 err(1, "malloc");
566 for (p = args, *p = '\0', ap = argv; *ap; ++ap) {
567 (void)strcpy(p, *ap);
568 p += strlen(p);
569 if (ap[1])
570 *p++ = ' ';
571 }
572 *p = '\0';
573 return (args);
574 }
575
576 void
577 usage(void)
578 {
579
580 (void)fprintf(stderr,
581 "usage: %s [-nd%s]%s[-l login]%s [login@]host %s\n", getprogname(),
582 #ifdef KERBEROS
583 #ifdef CRYPT
584 "x", " [-k realm] ",
585 #else
586 "", " [-k realm] ",
587 #endif
588 #else
589 "", " ",
590 #endif
591 #ifdef IN_RCMD
592 " [-u locuser]", "command"
593 #else
594 "", "[command]"
595 #endif
596 );
597 exit(1);
598 }
599