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