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