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