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