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