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