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